]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/ipv4/netfilter/nf_defrag_ipv4.c
netfilter: nf_conntrack: add support for "conntrack zones"
[net-next-2.6.git] / net / ipv4 / netfilter / nf_defrag_ipv4.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include <linux/types.h>
10 #include <linux/ip.h>
11 #include <linux/netfilter.h>
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <net/route.h>
15 #include <net/ip.h>
16
17 #include <linux/netfilter_bridge.h>
18 #include <linux/netfilter_ipv4.h>
19 #include <net/netfilter/nf_conntrack_zones.h>
20 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
21 #include <net/netfilter/nf_conntrack.h>
22
23 /* Returns new sk_buff, or NULL */
24 static int nf_ct_ipv4_gather_frags(struct sk_buff *skb, u_int32_t user)
25 {
26         int err;
27
28         skb_orphan(skb);
29
30         local_bh_disable();
31         err = ip_defrag(skb, user);
32         local_bh_enable();
33
34         if (!err)
35                 ip_send_check(ip_hdr(skb));
36
37         return err;
38 }
39
40 static enum ip_defrag_users nf_ct_defrag_user(unsigned int hooknum,
41                                               struct sk_buff *skb)
42 {
43         u16 zone = NF_CT_DEFAULT_ZONE;
44
45         if (skb->nfct)
46                 zone = nf_ct_zone((struct nf_conn *)skb->nfct);
47
48 #ifdef CONFIG_BRIDGE_NETFILTER
49         if (skb->nf_bridge &&
50             skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)
51                 return IP_DEFRAG_CONNTRACK_BRIDGE_IN + zone;
52 #endif
53         if (hooknum == NF_INET_PRE_ROUTING)
54                 return IP_DEFRAG_CONNTRACK_IN + zone;
55         else
56                 return IP_DEFRAG_CONNTRACK_OUT + zone;
57 }
58
59 static unsigned int ipv4_conntrack_defrag(unsigned int hooknum,
60                                           struct sk_buff *skb,
61                                           const struct net_device *in,
62                                           const struct net_device *out,
63                                           int (*okfn)(struct sk_buff *))
64 {
65 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
66 #if !defined(CONFIG_NF_NAT) && !defined(CONFIG_NF_NAT_MODULE)
67         /* Previously seen (loopback)?  Ignore.  Do this before
68            fragment check. */
69         if (skb->nfct && !nf_ct_is_template((struct nf_conn *)skb->nfct))
70                 return NF_ACCEPT;
71 #endif
72 #endif
73         /* Gather fragments. */
74         if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
75                 enum ip_defrag_users user = nf_ct_defrag_user(hooknum, skb);
76                 if (nf_ct_ipv4_gather_frags(skb, user))
77                         return NF_STOLEN;
78         }
79         return NF_ACCEPT;
80 }
81
82 static struct nf_hook_ops ipv4_defrag_ops[] = {
83         {
84                 .hook           = ipv4_conntrack_defrag,
85                 .owner          = THIS_MODULE,
86                 .pf             = PF_INET,
87                 .hooknum        = NF_INET_PRE_ROUTING,
88                 .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
89         },
90         {
91                 .hook           = ipv4_conntrack_defrag,
92                 .owner          = THIS_MODULE,
93                 .pf             = PF_INET,
94                 .hooknum        = NF_INET_LOCAL_OUT,
95                 .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
96         },
97 };
98
99 static int __init nf_defrag_init(void)
100 {
101         return nf_register_hooks(ipv4_defrag_ops, ARRAY_SIZE(ipv4_defrag_ops));
102 }
103
104 static void __exit nf_defrag_fini(void)
105 {
106         nf_unregister_hooks(ipv4_defrag_ops, ARRAY_SIZE(ipv4_defrag_ops));
107 }
108
109 void nf_defrag_ipv4_enable(void)
110 {
111 }
112 EXPORT_SYMBOL_GPL(nf_defrag_ipv4_enable);
113
114 module_init(nf_defrag_init);
115 module_exit(nf_defrag_fini);
116
117 MODULE_LICENSE("GPL");