linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / net / ipv4 / netfilter / ipt_MASQUERADE.c
index 6e4c732..12c56d3 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <linux/config.h>
 #include <linux/types.h>
+#include <linux/inetdevice.h>
 #include <linux/ip.h>
 #include <linux/timer.h>
 #include <linux/module.h>
@@ -18,6 +19,7 @@
 #include <net/protocol.h>
 #include <net/ip.h>
 #include <net/checksum.h>
+#include <net/route.h>
 #include <linux/netfilter_ipv4.h>
 #include <linux/netfilter_ipv4/ip_nat_rule.h>
 #include <linux/netfilter_ipv4/ip_tables.h>
@@ -33,17 +35,17 @@ MODULE_DESCRIPTION("iptables MASQUERADE target module");
 #endif
 
 /* Lock protects masq region inside conntrack */
-static DECLARE_RWLOCK(masq_lock);
+static DEFINE_RWLOCK(masq_lock);
 
 /* FIXME: Multiple targets. --RR */
 static int
 masquerade_check(const char *tablename,
-                const struct ipt_entry *e,
+                const void *e,
                 void *targinfo,
                 unsigned int targinfosize,
                 unsigned int hook_mask)
 {
-       const struct ip_nat_multi_range *mr = targinfo;
+       const struct ip_nat_multi_range_compat *mr = targinfo;
 
        if (strcmp(tablename, "nat") != 0) {
                DEBUGP("masquerade_check: bad table `%s'.\n", tablename);
@@ -79,22 +81,23 @@ masquerade_target(struct sk_buff **pskb,
 {
        struct ip_conntrack *ct;
        enum ip_conntrack_info ctinfo;
-       const struct ip_nat_multi_range *mr;
-       struct ip_nat_multi_range newrange;
+       const struct ip_nat_multi_range_compat *mr;
+       struct ip_nat_range newrange;
        struct rtable *rt;
        u_int32_t newsrc;
 
        IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
 
-       /* FIXME: For the moment, don't do local packets, breaks
-          testsuite for 2.3.49 --RR */
-       if ((*pskb)->sk)
-               return NF_ACCEPT;
-
        ct = ip_conntrack_get(*pskb, &ctinfo);
        IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED
                            || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
 
+       /* Source address is 0.0.0.0 - locally generated packet that is
+        * probably not supposed to be masqueraded.
+        */
+       if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip == 0)
+               return NF_ACCEPT;
+
        mr = targinfo;
        rt = (struct rtable *)(*pskb)->dst;
        newsrc = inet_select_addr(out, rt->rt_gateway, RT_SCOPE_UNIVERSE);
@@ -103,28 +106,28 @@ masquerade_target(struct sk_buff **pskb,
                return NF_DROP;
        }
 
-       WRITE_LOCK(&masq_lock);
+       write_lock_bh(&masq_lock);
        ct->nat.masq_index = out->ifindex;
-       WRITE_UNLOCK(&masq_lock);
+       write_unlock_bh(&masq_lock);
 
        /* Transfer from original range. */
-       newrange = ((struct ip_nat_multi_range)
-               { 1, { { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
-                        newsrc, newsrc,
-                        mr->range[0].min, mr->range[0].max } } });
+       newrange = ((struct ip_nat_range)
+               { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
+                 newsrc, newsrc,
+                 mr->range[0].min, mr->range[0].max });
 
        /* Hand modified range to generic setup. */
        return ip_nat_setup_info(ct, &newrange, hooknum);
 }
 
 static inline int
-device_cmp(const struct ip_conntrack *i, void *ifindex)
+device_cmp(struct ip_conntrack *i, void *ifindex)
 {
        int ret;
 
-       READ_LOCK(&masq_lock);
+       read_lock_bh(&masq_lock);
        ret = (i->nat.masq_index == (int)(long)ifindex);
-       READ_UNLOCK(&masq_lock);
+       read_unlock_bh(&masq_lock);
 
        return ret;
 }
@@ -141,7 +144,7 @@ static int masq_device_event(struct notifier_block *this,
                   and forget them. */
                IP_NF_ASSERT(dev->ifindex != 0);
 
-               ip_ct_selective_cleanup(device_cmp, (void *)(long)dev->ifindex);
+               ip_ct_iterate_cleanup(device_cmp, (void *)(long)dev->ifindex);
        }
 
        return NOTIFY_DONE;
@@ -159,7 +162,7 @@ static int masq_inet_event(struct notifier_block *this,
                   and forget them. */
                IP_NF_ASSERT(dev->ifindex != 0);
 
-               ip_ct_selective_cleanup(device_cmp, (void *)(long)dev->ifindex);
+               ip_ct_iterate_cleanup(device_cmp, (void *)(long)dev->ifindex);
        }
 
        return NOTIFY_DONE;