]>
Commit | Line | Data |
---|---|---|
7e5d03bb MJ |
1 | /* Helper handling for netfilter. */ |
2 | ||
3 | /* (C) 1999-2001 Paul `Rusty' Russell | |
4 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> | |
5 | * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | */ | |
11 | ||
12 | #include <linux/types.h> | |
13 | #include <linux/netfilter.h> | |
14 | #include <linux/module.h> | |
15 | #include <linux/skbuff.h> | |
16 | #include <linux/vmalloc.h> | |
17 | #include <linux/stddef.h> | |
18 | #include <linux/slab.h> | |
19 | #include <linux/random.h> | |
20 | #include <linux/err.h> | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/netdevice.h> | |
23 | ||
7e5d03bb MJ |
24 | #include <net/netfilter/nf_conntrack.h> |
25 | #include <net/netfilter/nf_conntrack_l3proto.h> | |
605dcad6 | 26 | #include <net/netfilter/nf_conntrack_l4proto.h> |
7e5d03bb MJ |
27 | #include <net/netfilter/nf_conntrack_helper.h> |
28 | #include <net/netfilter/nf_conntrack_core.h> | |
ceceae1b | 29 | #include <net/netfilter/nf_conntrack_extend.h> |
7e5d03bb | 30 | |
b8a7fe6c PM |
31 | static struct hlist_head *nf_ct_helper_hash __read_mostly; |
32 | static unsigned int nf_ct_helper_hsize __read_mostly; | |
33 | static unsigned int nf_ct_helper_count __read_mostly; | |
34 | static int nf_ct_helper_vmalloc; | |
35 | ||
36 | ||
37 | /* Stupid hash, but collision free for the default registrations of the | |
38 | * helpers currently in the kernel. */ | |
39 | static unsigned int helper_hash(const struct nf_conntrack_tuple *tuple) | |
40 | { | |
41 | return (((tuple->src.l3num << 8) | tuple->dst.protonum) ^ | |
42 | tuple->src.u.all) % nf_ct_helper_hsize; | |
43 | } | |
7e5d03bb MJ |
44 | |
45 | struct nf_conntrack_helper * | |
46 | __nf_ct_helper_find(const struct nf_conntrack_tuple *tuple) | |
47 | { | |
b8a7fe6c | 48 | struct nf_conntrack_helper *helper; |
d4156e8c | 49 | struct nf_conntrack_tuple_mask mask = { .src.u.all = htons(0xFFFF) }; |
b8a7fe6c PM |
50 | struct hlist_node *n; |
51 | unsigned int h; | |
7e5d03bb | 52 | |
b8a7fe6c PM |
53 | if (!nf_ct_helper_count) |
54 | return NULL; | |
55 | ||
56 | h = helper_hash(tuple); | |
57 | hlist_for_each_entry(helper, n, &nf_ct_helper_hash[h], hnode) { | |
58 | if (nf_ct_tuple_src_mask_cmp(tuple, &helper->tuple, &mask)) | |
59 | return helper; | |
7e5d03bb MJ |
60 | } |
61 | return NULL; | |
62 | } | |
63 | ||
64 | struct nf_conntrack_helper * | |
b8a7fe6c | 65 | nf_ct_helper_find_get(const struct nf_conntrack_tuple *tuple) |
7e5d03bb MJ |
66 | { |
67 | struct nf_conntrack_helper *helper; | |
68 | ||
69 | /* need nf_conntrack_lock to assure that helper exists until | |
70 | * try_module_get() is called */ | |
71 | read_lock_bh(&nf_conntrack_lock); | |
72 | ||
73 | helper = __nf_ct_helper_find(tuple); | |
74 | if (helper) { | |
75 | /* need to increase module usage count to assure helper will | |
76 | * not go away while the caller is e.g. busy putting a | |
77 | * conntrack in the hash that uses the helper */ | |
78 | if (!try_module_get(helper->me)) | |
79 | helper = NULL; | |
80 | } | |
81 | ||
82 | read_unlock_bh(&nf_conntrack_lock); | |
83 | ||
84 | return helper; | |
85 | } | |
13b18339 | 86 | EXPORT_SYMBOL_GPL(nf_ct_helper_find_get); |
7e5d03bb MJ |
87 | |
88 | void nf_ct_helper_put(struct nf_conntrack_helper *helper) | |
89 | { | |
90 | module_put(helper->me); | |
91 | } | |
13b18339 | 92 | EXPORT_SYMBOL_GPL(nf_ct_helper_put); |
7e5d03bb MJ |
93 | |
94 | struct nf_conntrack_helper * | |
95 | __nf_conntrack_helper_find_byname(const char *name) | |
96 | { | |
97 | struct nf_conntrack_helper *h; | |
b8a7fe6c PM |
98 | struct hlist_node *n; |
99 | unsigned int i; | |
7e5d03bb | 100 | |
b8a7fe6c PM |
101 | for (i = 0; i < nf_ct_helper_hsize; i++) { |
102 | hlist_for_each_entry(h, n, &nf_ct_helper_hash[i], hnode) { | |
103 | if (!strcmp(h->name, name)) | |
104 | return h; | |
105 | } | |
7e5d03bb | 106 | } |
7e5d03bb MJ |
107 | return NULL; |
108 | } | |
13b18339 | 109 | EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find_byname); |
7e5d03bb | 110 | |
b560580a PM |
111 | struct nf_conn_help *nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp) |
112 | { | |
113 | struct nf_conn_help *help; | |
114 | ||
115 | help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, gfp); | |
116 | if (help) | |
117 | INIT_HLIST_HEAD(&help->expectations); | |
118 | else | |
119 | pr_debug("failed to add helper extension area"); | |
120 | return help; | |
121 | } | |
122 | EXPORT_SYMBOL_GPL(nf_ct_helper_ext_add); | |
123 | ||
7e5d03bb MJ |
124 | static inline int unhelp(struct nf_conntrack_tuple_hash *i, |
125 | const struct nf_conntrack_helper *me) | |
126 | { | |
127 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(i); | |
128 | struct nf_conn_help *help = nfct_help(ct); | |
129 | ||
130 | if (help && help->helper == me) { | |
131 | nf_conntrack_event(IPCT_HELPER, ct); | |
3c158f7f | 132 | rcu_assign_pointer(help->helper, NULL); |
7e5d03bb MJ |
133 | } |
134 | return 0; | |
135 | } | |
136 | ||
137 | int nf_conntrack_helper_register(struct nf_conntrack_helper *me) | |
138 | { | |
b8a7fe6c PM |
139 | unsigned int h = helper_hash(&me->tuple); |
140 | ||
7e5d03bb MJ |
141 | BUG_ON(me->timeout == 0); |
142 | ||
7e5d03bb | 143 | write_lock_bh(&nf_conntrack_lock); |
b8a7fe6c PM |
144 | hlist_add_head(&me->hnode, &nf_ct_helper_hash[h]); |
145 | nf_ct_helper_count++; | |
7e5d03bb MJ |
146 | write_unlock_bh(&nf_conntrack_lock); |
147 | ||
148 | return 0; | |
149 | } | |
13b18339 | 150 | EXPORT_SYMBOL_GPL(nf_conntrack_helper_register); |
7e5d03bb MJ |
151 | |
152 | void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me) | |
153 | { | |
7e5d03bb | 154 | struct nf_conntrack_tuple_hash *h; |
31f15875 PM |
155 | struct nf_conntrack_expect *exp; |
156 | struct hlist_node *n, *next; | |
157 | unsigned int i; | |
7e5d03bb MJ |
158 | |
159 | /* Need write lock here, to delete helper. */ | |
160 | write_lock_bh(&nf_conntrack_lock); | |
b8a7fe6c PM |
161 | hlist_del(&me->hnode); |
162 | nf_ct_helper_count--; | |
7e5d03bb MJ |
163 | |
164 | /* Get rid of expectations */ | |
31f15875 PM |
165 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
166 | hlist_for_each_entry_safe(exp, n, next, | |
167 | &nf_ct_expect_hash[i], hnode) { | |
168 | struct nf_conn_help *help = nfct_help(exp->master); | |
169 | if ((help->helper == me || exp->helper == me) && | |
170 | del_timer(&exp->timeout)) { | |
171 | nf_ct_unlink_expect(exp); | |
172 | nf_ct_expect_put(exp); | |
173 | } | |
7e5d03bb MJ |
174 | } |
175 | } | |
176 | ||
177 | /* Get rid of expecteds, set helpers to NULL. */ | |
f205c5e0 | 178 | hlist_for_each_entry(h, n, &unconfirmed, hnode) |
7e5d03bb MJ |
179 | unhelp(h, me); |
180 | for (i = 0; i < nf_conntrack_htable_size; i++) { | |
f205c5e0 | 181 | hlist_for_each_entry(h, n, &nf_conntrack_hash[i], hnode) |
7e5d03bb MJ |
182 | unhelp(h, me); |
183 | } | |
184 | write_unlock_bh(&nf_conntrack_lock); | |
185 | ||
186 | /* Someone could be still looking at the helper in a bh. */ | |
187 | synchronize_net(); | |
188 | } | |
13b18339 | 189 | EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister); |
ceceae1b | 190 | |
61eb3107 | 191 | static struct nf_ct_ext_type helper_extend __read_mostly = { |
ceceae1b YK |
192 | .len = sizeof(struct nf_conn_help), |
193 | .align = __alignof__(struct nf_conn_help), | |
194 | .id = NF_CT_EXT_HELPER, | |
195 | }; | |
196 | ||
197 | int nf_conntrack_helper_init() | |
198 | { | |
b8a7fe6c PM |
199 | int err; |
200 | ||
201 | nf_ct_helper_hsize = 1; /* gets rounded up to use one page */ | |
202 | nf_ct_helper_hash = nf_ct_alloc_hashtable(&nf_ct_helper_hsize, | |
203 | &nf_ct_helper_vmalloc); | |
204 | if (!nf_ct_helper_hash) | |
205 | return -ENOMEM; | |
206 | ||
207 | err = nf_ct_extend_register(&helper_extend); | |
208 | if (err < 0) | |
209 | goto err1; | |
210 | ||
211 | return 0; | |
212 | ||
213 | err1: | |
214 | nf_ct_free_hashtable(nf_ct_helper_hash, nf_ct_helper_vmalloc, | |
215 | nf_ct_helper_hsize); | |
216 | return err; | |
ceceae1b YK |
217 | } |
218 | ||
219 | void nf_conntrack_helper_fini() | |
220 | { | |
221 | nf_ct_extend_unregister(&helper_extend); | |
b8a7fe6c PM |
222 | nf_ct_free_hashtable(nf_ct_helper_hash, nf_ct_helper_vmalloc, |
223 | nf_ct_helper_hsize); | |
ceceae1b | 224 | } |