Merge to Fedora kernel-2.6.18-1.2255_FC5-vs2.0.2.2-rc9 patched with stable patch...
[linux-2.6.git] / net / ipv4 / netfilter / ip_nat_standalone.c
index c4fe69b..c508544 100644 (file)
@@ -18,7 +18,6 @@
  *     - now capable of multiple expectations for one master
  * */
 
-#include <linux/config.h>
 #include <linux/types.h>
 #include <linux/icmp.h>
 #include <linux/ip.h>
 #include <linux/module.h>
 #include <linux/skbuff.h>
 #include <linux/proc_fs.h>
+#include <net/ip.h>
 #include <net/checksum.h>
 #include <linux/spinlock.h>
 
-#define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_nat_lock)
-#define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_nat_lock)
+#define ASSERT_READ_LOCK(x)
+#define ASSERT_WRITE_LOCK(x)
 
 #include <linux/netfilter_ipv4/ip_nat.h>
 #include <linux/netfilter_ipv4/ip_nat_rule.h>
                                 : ((hooknum) == NF_IP_LOCAL_IN ? "LOCAL_IN"  \
                                    : "*ERROR*")))
 
-static inline int call_expect(struct ip_conntrack *master,
-                             struct sk_buff **pskb,
-                             unsigned int hooknum,
-                             struct ip_conntrack *ct,
-                             struct ip_nat_info *info)
+#ifdef CONFIG_XFRM
+static void nat_decode_session(struct sk_buff *skb, struct flowi *fl)
 {
-       return master->nat.info.helper->expect(pskb, hooknum, ct, info);
-}
+       struct ip_conntrack *ct;
+       struct ip_conntrack_tuple *t;
+       enum ip_conntrack_info ctinfo;
+       enum ip_conntrack_dir dir;
+       unsigned long statusbit;
+
+       ct = ip_conntrack_get(skb, &ctinfo);
+       if (ct == NULL)
+               return;
+       dir = CTINFO2DIR(ctinfo);
+       t = &ct->tuplehash[dir].tuple;
+
+       if (dir == IP_CT_DIR_ORIGINAL)
+               statusbit = IPS_DST_NAT;
+       else
+               statusbit = IPS_SRC_NAT;
+
+       if (ct->status & statusbit) {
+               fl->fl4_dst = t->dst.ip;
+               if (t->dst.protonum == IPPROTO_TCP ||
+                   t->dst.protonum == IPPROTO_UDP)
+                       fl->fl_ip_dport = t->dst.u.tcp.port;
+       }
+
+       statusbit ^= IPS_NAT_MASK;
 
+       if (ct->status & statusbit) {
+               fl->fl4_src = t->src.ip;
+               if (t->dst.protonum == IPPROTO_TCP ||
+                   t->dst.protonum == IPPROTO_UDP)
+                       fl->fl_ip_sport = t->src.u.tcp.port;
+       }
+}
+#endif
+               
 static unsigned int
 ip_nat_fn(unsigned int hooknum,
          struct sk_buff **pskb,
@@ -81,13 +110,17 @@ ip_nat_fn(unsigned int hooknum,
        IP_NF_ASSERT(!((*pskb)->nh.iph->frag_off
                       & htons(IP_MF|IP_OFFSET)));
 
-       (*pskb)->nfcache |= NFC_UNKNOWN;
+       ct = ip_conntrack_get(*pskb, &ctinfo);
+
+       /* Don't try to NAT if this packet is not conntracked */
+       if (ct == &ip_conntrack_untracked)
+               return NF_ACCEPT;
 
        /* If we had a hardware checksum before, it's now invalid */
        if ((*pskb)->ip_summed == CHECKSUM_HW)
-               (*pskb)->ip_summed = CHECKSUM_NONE;
+               if (skb_checksum_help(*pskb, (out == NULL)))
+                       return NF_DROP;
 
-       ct = ip_conntrack_get(*pskb, &ctinfo);
        /* Can't track?  It's not due to stress, or conntrack would
           have dropped it.  Hence it's the user's responsibilty to
           packet filter it out, or implement conntrack/NAT for that
@@ -97,11 +130,13 @@ ip_nat_fn(unsigned int hooknum,
                    hash table yet).  We must not let this through, in
                    case we're doing NAT to the same network. */
                if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
-                       struct icmphdr hdr;
+                       struct icmphdr _hdr, *hp;
 
-                       if (skb_copy_bits(*pskb, (*pskb)->nh.iph->ihl*4,
-                                         &hdr, sizeof(hdr)) == 0
-                           && hdr.type == ICMP_REDIRECT)
+                       hp = skb_header_pointer(*pskb,
+                                               (*pskb)->nh.iph->ihl*4,
+                                               sizeof(_hdr), &_hdr);
+                       if (hp != NULL &&
+                           hp->type == ICMP_REDIRECT)
                                return NF_DROP;
                }
                return NF_ACCEPT;
@@ -111,8 +146,8 @@ ip_nat_fn(unsigned int hooknum,
        case IP_CT_RELATED:
        case IP_CT_RELATED+IP_CT_IS_REPLY:
                if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
-                       if (!icmp_reply_translation(pskb, ct, hooknum,
-                                                   CTINFO2DIR(ctinfo)))
+                       if (!ip_nat_icmp_reply_translation(pskb, ct, maniptype,
+                                                          CTINFO2DIR(ctinfo)))
                                return NF_DROP;
                        else
                                return NF_ACCEPT;
@@ -121,47 +156,30 @@ ip_nat_fn(unsigned int hooknum,
        case IP_CT_NEW:
                info = &ct->nat.info;
 
-               WRITE_LOCK(&ip_nat_lock);
                /* Seen it before?  This can happen for loopback, retrans,
                   or local packets.. */
-               if (!(info->initialized & (1 << maniptype))
-#ifndef CONFIG_IP_NF_NAT_LOCAL
-                   /* If this session has already been confirmed we must not
-                    * touch it again even if there is no mapping set up.
-                    * Can only happen on local->local traffic with
-                    * CONFIG_IP_NF_NAT_LOCAL disabled.
-                    */
-                   && !(ct->status & IPS_CONFIRMED)
-#endif
-                   ) {
+               if (!ip_nat_initialized(ct, maniptype)) {
                        unsigned int ret;
 
-                       if (ct->master
-                           && master_ct(ct)->nat.info.helper
-                           && master_ct(ct)->nat.info.helper->expect) {
-                               ret = call_expect(master_ct(ct), pskb, 
-                                                 hooknum, ct, info);
-                       } else {
-#ifdef CONFIG_IP_NF_NAT_LOCAL
+                       if (unlikely(is_confirmed(ct)))
+                               /* NAT module was loaded late */
+                               ret = alloc_null_binding_confirmed(ct, info,
+                                                                  hooknum);
+                       else if (hooknum == NF_IP_LOCAL_IN)
                                /* LOCAL_IN hook doesn't have a chain!  */
-                               if (hooknum == NF_IP_LOCAL_IN)
-                                       ret = alloc_null_binding(ct, info,
-                                                                hooknum);
-                               else
-#endif
-                               ret = ip_nat_rule_find(pskb, hooknum, in, out,
-                                                      ct, info);
-                       }
+                               ret = alloc_null_binding(ct, info, hooknum);
+                       else
+                               ret = ip_nat_rule_find(pskb, hooknum,
+                                                      in, out, ct,
+                                                      info);
 
                        if (ret != NF_ACCEPT) {
-                               WRITE_UNLOCK(&ip_nat_lock);
                                return ret;
                        }
                } else
                        DEBUGP("Already setup manip %s for ct %p\n",
                               maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
                               ct);
-               WRITE_UNLOCK(&ip_nat_lock);
                break;
 
        default:
@@ -172,7 +190,26 @@ ip_nat_fn(unsigned int hooknum,
        }
 
        IP_NF_ASSERT(info);
-       return do_bindings(ct, ctinfo, info, hooknum, pskb);
+       return ip_nat_packet(ct, ctinfo, hooknum, pskb);
+}
+
+static unsigned int
+ip_nat_in(unsigned int hooknum,
+          struct sk_buff **pskb,
+          const struct net_device *in,
+          const struct net_device *out,
+          int (*okfn)(struct sk_buff *))
+{
+       unsigned int ret;
+       u_int32_t daddr = (*pskb)->nh.iph->daddr;
+
+       ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
+       if (ret != NF_DROP && ret != NF_STOLEN
+           && daddr != (*pskb)->nh.iph->daddr) {
+               dst_release((*pskb)->dst);
+               (*pskb)->dst = NULL;
+       }
+       return ret;
 }
 
 static unsigned int
@@ -182,32 +219,34 @@ ip_nat_out(unsigned int hooknum,
           const struct net_device *out,
           int (*okfn)(struct sk_buff *))
 {
+#ifdef CONFIG_XFRM
+       struct ip_conntrack *ct;
+       enum ip_conntrack_info ctinfo;
+#endif
+       unsigned int ret;
+
        /* root is playing with raw sockets. */
        if ((*pskb)->len < sizeof(struct iphdr)
            || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
                return NF_ACCEPT;
 
-       /* We can hit fragment here; forwarded packets get
-          defragmented by connection tracking coming in, then
-          fragmented (grr) by the forward code.
-
-          In future: If we have nfct != NULL, AND we have NAT
-          initialized, AND there is no helper, then we can do full
-          NAPT on the head, and IP-address-only NAT on the rest.
-
-          I'm starting to have nightmares about fragments.  */
-
-       if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
-               *pskb = ip_ct_gather_frags(*pskb);
-
-               if (!*pskb)
-                       return NF_STOLEN;
+       ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
+#ifdef CONFIG_XFRM
+       if (ret != NF_DROP && ret != NF_STOLEN
+           && (ct = ip_conntrack_get(*pskb, &ctinfo)) != NULL) {
+               enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
+
+               if (ct->tuplehash[dir].tuple.src.ip !=
+                   ct->tuplehash[!dir].tuple.dst.ip
+                   || ct->tuplehash[dir].tuple.src.u.all !=
+                      ct->tuplehash[!dir].tuple.dst.u.all
+                   )
+                       return ip_xfrm_me_harder(pskb) == 0 ? ret : NF_DROP;
        }
-
-       return ip_nat_fn(hooknum, pskb, in, out, okfn);
+#endif
+       return ret;
 }
 
-#ifdef CONFIG_IP_NF_NAT_LOCAL
 static unsigned int
 ip_nat_local_fn(unsigned int hooknum,
                struct sk_buff **pskb,
@@ -215,7 +254,8 @@ ip_nat_local_fn(unsigned int hooknum,
                const struct net_device *out,
                int (*okfn)(struct sk_buff *))
 {
-       u_int32_t saddr, daddr;
+       struct ip_conntrack *ct;
+       enum ip_conntrack_info ctinfo;
        unsigned int ret;
 
        /* root is playing with raw sockets. */
@@ -223,171 +263,139 @@ ip_nat_local_fn(unsigned int hooknum,
            || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
                return NF_ACCEPT;
 
-       saddr = (*pskb)->nh.iph->saddr;
-       daddr = (*pskb)->nh.iph->daddr;
-
        ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
        if (ret != NF_DROP && ret != NF_STOLEN
-           && ((*pskb)->nh.iph->saddr != saddr
-               || (*pskb)->nh.iph->daddr != daddr))
-               return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
-       return ret;
-}
+           && (ct = ip_conntrack_get(*pskb, &ctinfo)) != NULL) {
+               enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
+
+               if (ct->tuplehash[dir].tuple.dst.ip !=
+                   ct->tuplehash[!dir].tuple.src.ip
+#ifdef CONFIG_XFRM
+                   || ct->tuplehash[dir].tuple.dst.u.all !=
+                      ct->tuplehash[!dir].tuple.src.u.all
 #endif
-
-/* We must be after connection tracking and before packet filtering. */
-
-/* Before packet filtering, change destination */
-static struct nf_hook_ops ip_nat_in_ops = {
-       .hook           = ip_nat_fn,
-       .owner          = THIS_MODULE,
-       .pf             = PF_INET,
-       .hooknum        = NF_IP_PRE_ROUTING,
-       .priority       = NF_IP_PRI_NAT_DST,
-};
-
-/* After packet filtering, change source */
-static struct nf_hook_ops ip_nat_out_ops = {
-       .hook           = ip_nat_out,
-       .owner          = THIS_MODULE,
-       .pf             = PF_INET,
-       .hooknum        = NF_IP_POST_ROUTING,
-       .priority       = NF_IP_PRI_NAT_SRC,
-};
-
-#ifdef CONFIG_IP_NF_NAT_LOCAL
-/* Before packet filtering, change destination */
-static struct nf_hook_ops ip_nat_local_out_ops = {
-       .hook           = ip_nat_local_fn,
-       .owner          = THIS_MODULE,
-       .pf             = PF_INET,
-       .hooknum        = NF_IP_LOCAL_OUT,
-       .priority       = NF_IP_PRI_NAT_DST,
-};
-
-/* After packet filtering, change source for reply packets of LOCAL_OUT DNAT */
-static struct nf_hook_ops ip_nat_local_in_ops = {
-       .hook           = ip_nat_fn,
-       .owner          = THIS_MODULE,
-       .pf             = PF_INET,
-       .hooknum        = NF_IP_LOCAL_IN,
-       .priority       = NF_IP_PRI_NAT_SRC,
-};
-#endif
-
-/* Protocol registration. */
-int ip_nat_protocol_register(struct ip_nat_protocol *proto)
-{
-       int ret = 0;
-       struct list_head *i;
-
-       WRITE_LOCK(&ip_nat_lock);
-       list_for_each(i, &protos) {
-               if (((struct ip_nat_protocol *)i)->protonum
-                   == proto->protonum) {
-                       ret = -EBUSY;
-                       goto out;
-               }
+                   )
+                       if (ip_route_me_harder(pskb, RTN_UNSPEC))
+                               ret = NF_DROP;
        }
-
-       list_prepend(&protos, proto);
- out:
-       WRITE_UNLOCK(&ip_nat_lock);
        return ret;
 }
 
-/* Noone stores the protocol anywhere; simply delete it. */
-void ip_nat_protocol_unregister(struct ip_nat_protocol *proto)
+static unsigned int
+ip_nat_adjust(unsigned int hooknum,
+             struct sk_buff **pskb,
+             const struct net_device *in,
+             const struct net_device *out,
+             int (*okfn)(struct sk_buff *))
 {
-       WRITE_LOCK(&ip_nat_lock);
-       LIST_DELETE(&protos, proto);
-       WRITE_UNLOCK(&ip_nat_lock);
+       struct ip_conntrack *ct;
+       enum ip_conntrack_info ctinfo;
 
-       /* Someone could be still looking at the proto in a bh. */
-       synchronize_net();
+       ct = ip_conntrack_get(*pskb, &ctinfo);
+       if (ct && test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
+               DEBUGP("ip_nat_standalone: adjusting sequence number\n");
+               if (!ip_nat_seq_adjust(pskb, ct, ctinfo))
+                       return NF_DROP;
+       }
+       return NF_ACCEPT;
 }
 
-static int init_or_cleanup(int init)
+/* We must be after connection tracking and before packet filtering. */
+
+static struct nf_hook_ops ip_nat_ops[] = {
+       /* Before packet filtering, change destination */
+       {
+               .hook           = ip_nat_in,
+               .owner          = THIS_MODULE,
+               .pf             = PF_INET,
+               .hooknum        = NF_IP_PRE_ROUTING,
+               .priority       = NF_IP_PRI_NAT_DST,
+       },
+       /* After packet filtering, change source */
+       {
+               .hook           = ip_nat_out,
+               .owner          = THIS_MODULE,
+               .pf             = PF_INET,
+               .hooknum        = NF_IP_POST_ROUTING,
+               .priority       = NF_IP_PRI_NAT_SRC,
+       },
+       /* After conntrack, adjust sequence number */
+       {
+               .hook           = ip_nat_adjust,
+               .owner          = THIS_MODULE,
+               .pf             = PF_INET,
+               .hooknum        = NF_IP_POST_ROUTING,
+               .priority       = NF_IP_PRI_NAT_SEQ_ADJUST,
+       },
+       /* Before packet filtering, change destination */
+       {
+               .hook           = ip_nat_local_fn,
+               .owner          = THIS_MODULE,
+               .pf             = PF_INET,
+               .hooknum        = NF_IP_LOCAL_OUT,
+               .priority       = NF_IP_PRI_NAT_DST,
+       },
+       /* After packet filtering, change source */
+       {
+               .hook           = ip_nat_fn,
+               .owner          = THIS_MODULE,
+               .pf             = PF_INET,
+               .hooknum        = NF_IP_LOCAL_IN,
+               .priority       = NF_IP_PRI_NAT_SRC,
+       },
+       /* After conntrack, adjust sequence number */
+       {
+               .hook           = ip_nat_adjust,
+               .owner          = THIS_MODULE,
+               .pf             = PF_INET,
+               .hooknum        = NF_IP_LOCAL_IN,
+               .priority       = NF_IP_PRI_NAT_SEQ_ADJUST,
+       },
+};
+
+static int __init ip_nat_standalone_init(void)
 {
        int ret = 0;
 
-       need_ip_conntrack();
-
-       if (!init) goto cleanup;
+       need_conntrack();
 
+#ifdef CONFIG_XFRM
+       BUG_ON(ip_nat_decode_session != NULL);
+       ip_nat_decode_session = nat_decode_session;
+#endif
        ret = ip_nat_rule_init();
        if (ret < 0) {
                printk("ip_nat_init: can't setup rules.\n");
-               goto cleanup_nothing;
+               goto cleanup_decode_session;
        }
-       ret = ip_nat_init();
+       ret = nf_register_hooks(ip_nat_ops, ARRAY_SIZE(ip_nat_ops));
        if (ret < 0) {
-               printk("ip_nat_init: can't setup rules.\n");
+               printk("ip_nat_init: can't register hooks.\n");
                goto cleanup_rule_init;
        }
-       ret = nf_register_hook(&ip_nat_in_ops);
-       if (ret < 0) {
-               printk("ip_nat_init: can't register in hook.\n");
-               goto cleanup_nat;
-       }
-       ret = nf_register_hook(&ip_nat_out_ops);
-       if (ret < 0) {
-               printk("ip_nat_init: can't register out hook.\n");
-               goto cleanup_inops;
-       }
-#ifdef CONFIG_IP_NF_NAT_LOCAL
-       ret = nf_register_hook(&ip_nat_local_out_ops);
-       if (ret < 0) {
-               printk("ip_nat_init: can't register local out hook.\n");
-               goto cleanup_outops;
-       }
-       ret = nf_register_hook(&ip_nat_local_in_ops);
-       if (ret < 0) {
-               printk("ip_nat_init: can't register local in hook.\n");
-               goto cleanup_localoutops;
-       }
-#endif
        return ret;
 
- cleanup:
-#ifdef CONFIG_IP_NF_NAT_LOCAL
-       nf_unregister_hook(&ip_nat_local_in_ops);
- cleanup_localoutops:
-       nf_unregister_hook(&ip_nat_local_out_ops);
- cleanup_outops:
-#endif
-       nf_unregister_hook(&ip_nat_out_ops);
- cleanup_inops:
-       nf_unregister_hook(&ip_nat_in_ops);
- cleanup_nat:
-       ip_nat_cleanup();
  cleanup_rule_init:
        ip_nat_rule_cleanup();
- cleanup_nothing:
-       MUST_BE_READ_WRITE_UNLOCKED(&ip_nat_lock);
+ cleanup_decode_session:
+#ifdef CONFIG_XFRM
+       ip_nat_decode_session = NULL;
+       synchronize_net();
+#endif
        return ret;
 }
 
-static int __init init(void)
+static void __exit ip_nat_standalone_fini(void)
 {
-       return init_or_cleanup(1);
+       nf_unregister_hooks(ip_nat_ops, ARRAY_SIZE(ip_nat_ops));
+       ip_nat_rule_cleanup();
+#ifdef CONFIG_XFRM
+       ip_nat_decode_session = NULL;
+       synchronize_net();
+#endif
 }
 
-static void __exit fini(void)
-{
-       init_or_cleanup(0);
-}
+module_init(ip_nat_standalone_init);
+module_exit(ip_nat_standalone_fini);
 
-module_init(init);
-module_exit(fini);
-
-EXPORT_SYMBOL(ip_nat_setup_info);
-EXPORT_SYMBOL(ip_nat_protocol_register);
-EXPORT_SYMBOL(ip_nat_protocol_unregister);
-EXPORT_SYMBOL(ip_nat_helper_register);
-EXPORT_SYMBOL(ip_nat_helper_unregister);
-EXPORT_SYMBOL(ip_nat_cheat_check);
-EXPORT_SYMBOL(ip_nat_mangle_tcp_packet);
-EXPORT_SYMBOL(ip_nat_mangle_udp_packet);
-EXPORT_SYMBOL(ip_nat_used_tuple);
 MODULE_LICENSE("GPL");