netfilter: conntrack: remove empty pernet fini stubs
[linux-2.6-block.git] / net / netfilter / nf_conntrack_helper.c
CommitLineData
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>
f229f6ce 6 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
7e5d03bb
MJ
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/types.h>
14#include <linux/netfilter.h>
15#include <linux/module.h>
16#include <linux/skbuff.h>
17#include <linux/vmalloc.h>
18#include <linux/stddef.h>
7e5d03bb
MJ
19#include <linux/random.h>
20#include <linux/err.h>
21#include <linux/kernel.h>
22#include <linux/netdevice.h>
2ba4cc31 23#include <linux/rculist.h>
efb9a8c2 24#include <linux/rtnetlink.h>
7e5d03bb 25
7e5d03bb 26#include <net/netfilter/nf_conntrack.h>
605dcad6 27#include <net/netfilter/nf_conntrack_l4proto.h>
7e5d03bb
MJ
28#include <net/netfilter/nf_conntrack_helper.h>
29#include <net/netfilter/nf_conntrack_core.h>
ceceae1b 30#include <net/netfilter/nf_conntrack_extend.h>
b20ab9cc 31#include <net/netfilter/nf_log.h>
7e5d03bb 32
58a3c9bb 33static DEFINE_MUTEX(nf_ct_helper_mutex);
12f7a505
PNA
34struct hlist_head *nf_ct_helper_hash __read_mostly;
35EXPORT_SYMBOL_GPL(nf_ct_helper_hash);
36unsigned int nf_ct_helper_hsize __read_mostly;
37EXPORT_SYMBOL_GPL(nf_ct_helper_hsize);
b8a7fe6c 38static unsigned int nf_ct_helper_count __read_mostly;
b8a7fe6c 39
3bb398d9 40static bool nf_ct_auto_assign_helper __read_mostly = false;
a9006892
EL
41module_param_named(nf_conntrack_helper, nf_ct_auto_assign_helper, bool, 0644);
42MODULE_PARM_DESC(nf_conntrack_helper,
3bb398d9 43 "Enable automatic conntrack helper assignment (default 0)");
a9006892 44
b8a7fe6c
PM
45/* Stupid hash, but collision free for the default registrations of the
46 * helpers currently in the kernel. */
47static unsigned int helper_hash(const struct nf_conntrack_tuple *tuple)
48{
49 return (((tuple->src.l3num << 8) | tuple->dst.protonum) ^
a34c4589 50 (__force __u16)tuple->src.u.all) % nf_ct_helper_hsize;
b8a7fe6c 51}
7e5d03bb 52
226c0c0e 53static struct nf_conntrack_helper *
7e5d03bb
MJ
54__nf_ct_helper_find(const struct nf_conntrack_tuple *tuple)
55{
b8a7fe6c 56 struct nf_conntrack_helper *helper;
d4156e8c 57 struct nf_conntrack_tuple_mask mask = { .src.u.all = htons(0xFFFF) };
b8a7fe6c 58 unsigned int h;
7e5d03bb 59
b8a7fe6c
PM
60 if (!nf_ct_helper_count)
61 return NULL;
62
63 h = helper_hash(tuple);
b67bfe0d 64 hlist_for_each_entry_rcu(helper, &nf_ct_helper_hash[h], hnode) {
b8a7fe6c
PM
65 if (nf_ct_tuple_src_mask_cmp(tuple, &helper->tuple, &mask))
66 return helper;
7e5d03bb
MJ
67 }
68 return NULL;
69}
7e5d03bb
MJ
70
71struct nf_conntrack_helper *
794e6871 72__nf_conntrack_helper_find(const char *name, u16 l3num, u8 protonum)
7e5d03bb
MJ
73{
74 struct nf_conntrack_helper *h;
b8a7fe6c 75 unsigned int i;
7e5d03bb 76
b8a7fe6c 77 for (i = 0; i < nf_ct_helper_hsize; i++) {
b67bfe0d 78 hlist_for_each_entry_rcu(h, &nf_ct_helper_hash[i], hnode) {
6114cc51
FW
79 if (strcmp(h->name, name))
80 continue;
81
82 if (h->tuple.src.l3num != NFPROTO_UNSPEC &&
83 h->tuple.src.l3num != l3num)
84 continue;
85
86 if (h->tuple.dst.protonum == protonum)
b8a7fe6c
PM
87 return h;
88 }
7e5d03bb 89 }
7e5d03bb
MJ
90 return NULL;
91}
794e6871 92EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find);
7e5d03bb 93
84f3bb9a
PM
94struct nf_conntrack_helper *
95nf_conntrack_helper_try_module_get(const char *name, u16 l3num, u8 protonum)
96{
97 struct nf_conntrack_helper *h;
98
8b5995d0
GF
99 rcu_read_lock();
100
84f3bb9a
PM
101 h = __nf_conntrack_helper_find(name, l3num, protonum);
102#ifdef CONFIG_MODULES
103 if (h == NULL) {
8b5995d0
GF
104 rcu_read_unlock();
105 if (request_module("nfct-helper-%s", name) == 0) {
106 rcu_read_lock();
84f3bb9a 107 h = __nf_conntrack_helper_find(name, l3num, protonum);
8b5995d0
GF
108 } else {
109 return h;
110 }
84f3bb9a
PM
111 }
112#endif
113 if (h != NULL && !try_module_get(h->me))
114 h = NULL;
9338d7b4
LZ
115 if (h != NULL && !refcount_inc_not_zero(&h->refcnt)) {
116 module_put(h->me);
117 h = NULL;
118 }
84f3bb9a 119
8b5995d0
GF
120 rcu_read_unlock();
121
84f3bb9a
PM
122 return h;
123}
124EXPORT_SYMBOL_GPL(nf_conntrack_helper_try_module_get);
125
d91fc59c
LZ
126void nf_conntrack_helper_put(struct nf_conntrack_helper *helper)
127{
9338d7b4 128 refcount_dec(&helper->refcnt);
d91fc59c
LZ
129 module_put(helper->me);
130}
131EXPORT_SYMBOL_GPL(nf_conntrack_helper_put);
132
1afc5679 133struct nf_conn_help *
440534d3 134nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp)
b560580a
PM
135{
136 struct nf_conn_help *help;
137
9f0f3ebe 138 help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, gfp);
b560580a
PM
139 if (help)
140 INIT_HLIST_HEAD(&help->expectations);
141 else
142 pr_debug("failed to add helper extension area");
143 return help;
144}
145EXPORT_SYMBOL_GPL(nf_ct_helper_ext_add);
146
dfe75ff8
JK
147static struct nf_conntrack_helper *
148nf_ct_lookup_helper(struct nf_conn *ct, struct net *net)
149{
150 if (!net->ct.sysctl_auto_assign_helper) {
151 if (net->ct.auto_assign_helper_warned)
152 return NULL;
153 if (!__nf_ct_helper_find(&ct->tuplehash[IP_CT_DIR_REPLY].tuple))
154 return NULL;
155 pr_info("nf_conntrack: default automatic helper assignment "
156 "has been turned off for security reasons and CT-based "
157 " firewall rule not found. Use the iptables CT target "
158 "to attach helpers instead.\n");
159 net->ct.auto_assign_helper_warned = 1;
160 return NULL;
161 }
162
163 return __nf_ct_helper_find(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
164}
165
166
b2a15a60
PM
167int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl,
168 gfp_t flags)
226c0c0e 169{
b2a15a60
PM
170 struct nf_conntrack_helper *helper = NULL;
171 struct nf_conn_help *help;
a9006892 172 struct net *net = nf_ct_net(ct);
226c0c0e 173
6714cf54
PNA
174 /* We already got a helper explicitly attached. The function
175 * nf_conntrack_alter_reply - in case NAT is in use - asks for looking
176 * the helper up again. Since now the user is in full control of
177 * making consistent helper configurations, skip this automatic
178 * re-lookup, otherwise we'll lose the helper.
179 */
180 if (test_bit(IPS_HELPER_BIT, &ct->status))
181 return 0;
182
b2a15a60
PM
183 if (tmpl != NULL) {
184 help = nfct_help(tmpl);
6714cf54 185 if (help != NULL) {
b2a15a60 186 helper = help->helper;
6714cf54
PNA
187 set_bit(IPS_HELPER_BIT, &ct->status);
188 }
b2a15a60
PM
189 }
190
191 help = nfct_help(ct);
a9006892 192
226c0c0e 193 if (helper == NULL) {
dfe75ff8
JK
194 helper = nf_ct_lookup_helper(ct, net);
195 if (helper == NULL) {
196 if (help)
197 RCU_INIT_POINTER(help->helper, NULL);
198 return 0;
199 }
226c0c0e
PNA
200 }
201
202 if (help == NULL) {
440534d3 203 help = nf_ct_helper_ext_add(ct, flags);
cf71c03e
PN
204 if (help == NULL)
205 return -ENOMEM;
226c0c0e 206 } else {
32f53760
PNA
207 /* We only allow helper re-assignment of the same sort since
208 * we cannot reallocate the helper extension area.
209 */
6e2f0aa8
FW
210 struct nf_conntrack_helper *tmp = rcu_dereference(help->helper);
211
212 if (tmp && tmp->help != helper->help) {
32f53760 213 RCU_INIT_POINTER(help->helper, NULL);
cf71c03e 214 return 0;
32f53760 215 }
226c0c0e
PNA
216 }
217
cf778b00 218 rcu_assign_pointer(help->helper, helper);
cf71c03e
PN
219
220 return 0;
226c0c0e
PNA
221}
222EXPORT_SYMBOL_GPL(__nf_ct_try_assign_helper);
223
01cfa0a4 224/* appropriate ct lock protecting must be taken by caller */
ff1acc49 225static int unhelp(struct nf_conn *ct, void *me)
7e5d03bb 226{
7e5d03bb
MJ
227 struct nf_conn_help *help = nfct_help(ct);
228
ca7433df 229 if (help && rcu_dereference_raw(help->helper) == me) {
7e5d03bb 230 nf_conntrack_event(IPCT_HELPER, ct);
a9b3cd7f 231 RCU_INIT_POINTER(help->helper, NULL);
7e5d03bb 232 }
ff1acc49
LZ
233
234 /* We are not intended to delete this conntrack. */
7e5d03bb
MJ
235 return 0;
236}
237
9858a3ae
PNA
238void nf_ct_helper_destroy(struct nf_conn *ct)
239{
240 struct nf_conn_help *help = nfct_help(ct);
241 struct nf_conntrack_helper *helper;
242
243 if (help) {
244 rcu_read_lock();
245 helper = rcu_dereference(help->helper);
246 if (helper && helper->destroy)
247 helper->destroy(ct);
248 rcu_read_unlock();
249 }
250}
251
544d5c7d
PNA
252static LIST_HEAD(nf_ct_helper_expectfn_list);
253
254void nf_ct_helper_expectfn_register(struct nf_ct_helper_expectfn *n)
255{
ca7433df 256 spin_lock_bh(&nf_conntrack_expect_lock);
544d5c7d 257 list_add_rcu(&n->head, &nf_ct_helper_expectfn_list);
ca7433df 258 spin_unlock_bh(&nf_conntrack_expect_lock);
544d5c7d
PNA
259}
260EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_register);
261
262void nf_ct_helper_expectfn_unregister(struct nf_ct_helper_expectfn *n)
263{
ca7433df 264 spin_lock_bh(&nf_conntrack_expect_lock);
544d5c7d 265 list_del_rcu(&n->head);
ca7433df 266 spin_unlock_bh(&nf_conntrack_expect_lock);
544d5c7d
PNA
267}
268EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_unregister);
269
8b5995d0 270/* Caller should hold the rcu lock */
544d5c7d
PNA
271struct nf_ct_helper_expectfn *
272nf_ct_helper_expectfn_find_by_name(const char *name)
273{
274 struct nf_ct_helper_expectfn *cur;
275 bool found = false;
276
544d5c7d
PNA
277 list_for_each_entry_rcu(cur, &nf_ct_helper_expectfn_list, head) {
278 if (!strcmp(cur->name, name)) {
279 found = true;
280 break;
281 }
282 }
544d5c7d
PNA
283 return found ? cur : NULL;
284}
285EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_find_by_name);
286
8b5995d0 287/* Caller should hold the rcu lock */
544d5c7d
PNA
288struct nf_ct_helper_expectfn *
289nf_ct_helper_expectfn_find_by_symbol(const void *symbol)
290{
291 struct nf_ct_helper_expectfn *cur;
292 bool found = false;
293
544d5c7d
PNA
294 list_for_each_entry_rcu(cur, &nf_ct_helper_expectfn_list, head) {
295 if (cur->expectfn == symbol) {
296 found = true;
297 break;
298 }
299 }
544d5c7d
PNA
300 return found ? cur : NULL;
301}
302EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_find_by_symbol);
303
b20ab9cc
PNA
304__printf(3, 4)
305void nf_ct_helper_log(struct sk_buff *skb, const struct nf_conn *ct,
306 const char *fmt, ...)
307{
308 const struct nf_conn_help *help;
309 const struct nf_conntrack_helper *helper;
f9caed59
JP
310 struct va_format vaf;
311 va_list args;
312
313 va_start(args, fmt);
314
315 vaf.fmt = fmt;
316 vaf.va = &args;
b20ab9cc
PNA
317
318 /* Called from the helper function, this call never fails */
319 help = nfct_help(ct);
320
e2361cb9 321 /* rcu_read_lock()ed by nf_hook_thresh */
b20ab9cc
PNA
322 helper = rcu_dereference(help->helper);
323
30e0c6a6 324 nf_log_packet(nf_ct_net(ct), nf_ct_l3num(ct), 0, skb, NULL, NULL, NULL,
f9caed59
JP
325 "nf_ct_%s: dropping packet: %pV ", helper->name, &vaf);
326
327 va_end(args);
b20ab9cc
PNA
328}
329EXPORT_SYMBOL_GPL(nf_ct_helper_log);
330
7e5d03bb
MJ
331int nf_conntrack_helper_register(struct nf_conntrack_helper *me)
332{
893e093c 333 struct nf_conntrack_tuple_mask mask = { .src.u.all = htons(0xFFFF) };
b8a7fe6c 334 unsigned int h = helper_hash(&me->tuple);
893e093c 335 struct nf_conntrack_helper *cur;
66e5a6b1 336 int ret = 0, i;
b8a7fe6c 337
6002f266
PM
338 BUG_ON(me->expect_policy == NULL);
339 BUG_ON(me->expect_class_max >= NF_CT_MAX_EXPECT_CLASSES);
af9d32ad 340 BUG_ON(strlen(me->name) > NF_CT_HELPER_NAME_LEN - 1);
7e5d03bb 341
92f73221
GF
342 if (me->expect_policy->max_expected > NF_CT_EXPECT_MAX_CNT)
343 return -EINVAL;
344
58a3c9bb 345 mutex_lock(&nf_ct_helper_mutex);
66e5a6b1
LZ
346 for (i = 0; i < nf_ct_helper_hsize; i++) {
347 hlist_for_each_entry(cur, &nf_ct_helper_hash[i], hnode) {
348 if (!strcmp(cur->name, me->name) &&
349 (cur->tuple.src.l3num == NFPROTO_UNSPEC ||
350 cur->tuple.src.l3num == me->tuple.src.l3num) &&
351 cur->tuple.dst.protonum == me->tuple.dst.protonum) {
352 ret = -EEXIST;
353 goto out;
354 }
355 }
356 }
357
358 /* avoid unpredictable behaviour for auto_assign_helper */
359 if (!(me->flags & NF_CT_HELPER_F_USERSPACE)) {
360 hlist_for_each_entry(cur, &nf_ct_helper_hash[h], hnode) {
361 if (nf_ct_tuple_src_mask_cmp(&cur->tuple, &me->tuple,
362 &mask)) {
363 ret = -EEXIST;
364 goto out;
365 }
12f7a505
PNA
366 }
367 }
9338d7b4 368 refcount_set(&me->refcnt, 1);
58a3c9bb 369 hlist_add_head_rcu(&me->hnode, &nf_ct_helper_hash[h]);
b8a7fe6c 370 nf_ct_helper_count++;
12f7a505 371out:
58a3c9bb 372 mutex_unlock(&nf_ct_helper_mutex);
12f7a505 373 return ret;
7e5d03bb 374}
13b18339 375EXPORT_SYMBOL_GPL(nf_conntrack_helper_register);
7e5d03bb 376
ac7b8483 377static bool expect_iter_me(struct nf_conntrack_expect *exp, void *data)
7e5d03bb 378{
ac7b8483
FW
379 struct nf_conn_help *help = nfct_help(exp->master);
380 const struct nf_conntrack_helper *me = data;
381 const struct nf_conntrack_helper *this;
382
383 if (exp->helper == me)
384 return true;
436a850d 385
ac7b8483
FW
386 this = rcu_dereference_protected(help->helper,
387 lockdep_is_held(&nf_conntrack_expect_lock));
388 return this == me;
389}
390
391void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)
392{
436a850d
FW
393 mutex_lock(&nf_ct_helper_mutex);
394 hlist_del_rcu(&me->hnode);
395 nf_ct_helper_count--;
396 mutex_unlock(&nf_ct_helper_mutex);
397
398 /* Make sure every nothing is still using the helper unless its a
399 * connection in the hash.
400 */
401 synchronize_rcu();
7e5d03bb 402
ac7b8483 403 nf_ct_expect_iterate_destroy(expect_iter_me, NULL);
ff1acc49 404 nf_ct_iterate_destroy(unhelp, me);
ad9852af
GF
405
406 /* Maybe someone has gotten the helper already when unhelp above.
407 * So need to wait it.
408 */
409 synchronize_rcu();
68047937 410}
13b18339 411EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister);
ceceae1b 412
82de0be6
GF
413void nf_ct_helper_init(struct nf_conntrack_helper *helper,
414 u16 l3num, u16 protonum, const char *name,
415 u16 default_port, u16 spec_port, u32 id,
416 const struct nf_conntrack_expect_policy *exp_pol,
9f0f3ebe 417 u32 expect_class_max,
82de0be6
GF
418 int (*help)(struct sk_buff *skb, unsigned int protoff,
419 struct nf_conn *ct,
420 enum ip_conntrack_info ctinfo),
421 int (*from_nlattr)(struct nlattr *attr,
422 struct nf_conn *ct),
423 struct module *module)
424{
425 helper->tuple.src.l3num = l3num;
426 helper->tuple.dst.protonum = protonum;
427 helper->tuple.src.u.all = htons(spec_port);
428 helper->expect_policy = exp_pol;
429 helper->expect_class_max = expect_class_max;
82de0be6
GF
430 helper->help = help;
431 helper->from_nlattr = from_nlattr;
432 helper->me = module;
433
434 if (spec_port == default_port)
435 snprintf(helper->name, sizeof(helper->name), "%s", name);
436 else
437 snprintf(helper->name, sizeof(helper->name), "%s-%u", name, id);
438}
439EXPORT_SYMBOL_GPL(nf_ct_helper_init);
440
441int nf_conntrack_helpers_register(struct nf_conntrack_helper *helper,
442 unsigned int n)
443{
444 unsigned int i;
445 int err = 0;
446
447 for (i = 0; i < n; i++) {
448 err = nf_conntrack_helper_register(&helper[i]);
449 if (err < 0)
450 goto err;
451 }
452
453 return err;
454err:
455 if (i > 0)
456 nf_conntrack_helpers_unregister(helper, i);
457 return err;
458}
459EXPORT_SYMBOL_GPL(nf_conntrack_helpers_register);
460
461void nf_conntrack_helpers_unregister(struct nf_conntrack_helper *helper,
462 unsigned int n)
463{
464 while (n-- > 0)
465 nf_conntrack_helper_unregister(&helper[n]);
466}
467EXPORT_SYMBOL_GPL(nf_conntrack_helpers_unregister);
468
23f671a1 469static const struct nf_ct_ext_type helper_extend = {
ceceae1b
YK
470 .len = sizeof(struct nf_conn_help),
471 .align = __alignof__(struct nf_conn_help),
472 .id = NF_CT_EXT_HELPER,
473};
474
fc3893fd 475void nf_conntrack_helper_pernet_init(struct net *net)
ceceae1b 476{
a9006892
EL
477 net->ct.auto_assign_helper_warned = false;
478 net->ct.sysctl_auto_assign_helper = nf_ct_auto_assign_helper;
5e615b22 479}
a9006892 480
5e615b22
G
481int nf_conntrack_helper_init(void)
482{
483 int ret;
484 nf_ct_helper_hsize = 1; /* gets rounded up to use one page */
485 nf_ct_helper_hash =
486 nf_ct_alloc_hashtable(&nf_ct_helper_hsize, 0);
487 if (!nf_ct_helper_hash)
488 return -ENOMEM;
489
490 ret = nf_ct_extend_register(&helper_extend);
491 if (ret < 0) {
492 pr_err("nf_ct_helper: Unable to register helper extension.\n");
493 goto out_extend;
a9006892
EL
494 }
495
b8a7fe6c 496 return 0;
5e615b22 497out_extend:
285189c7 498 kvfree(nf_ct_helper_hash);
5e615b22 499 return ret;
ceceae1b
YK
500}
501
5e615b22 502void nf_conntrack_helper_fini(void)
ceceae1b 503{
5e615b22 504 nf_ct_extend_unregister(&helper_extend);
285189c7 505 kvfree(nf_ct_helper_hash);
ceceae1b 506}