1 /* Helper handling for netfilter. */
3 /* (C) 1999-2001 Paul `Rusty' Russell
5 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
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.
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>
19 #include <linux/random.h>
20 #include <linux/err.h>
21 #include <linux/kernel.h>
22 #include <linux/netdevice.h>
23 #include <linux/rculist.h>
24 #include <linux/rtnetlink.h>
26 #include <net/netfilter/nf_conntrack.h>
27 #include <net/netfilter/nf_conntrack_l4proto.h>
28 #include <net/netfilter/nf_conntrack_helper.h>
29 #include <net/netfilter/nf_conntrack_core.h>
30 #include <net/netfilter/nf_conntrack_extend.h>
31 #include <net/netfilter/nf_log.h>
33 static DEFINE_MUTEX(nf_ct_helper_mutex);
34 struct hlist_head *nf_ct_helper_hash __read_mostly;
35 EXPORT_SYMBOL_GPL(nf_ct_helper_hash);
36 unsigned int nf_ct_helper_hsize __read_mostly;
37 EXPORT_SYMBOL_GPL(nf_ct_helper_hsize);
38 static unsigned int nf_ct_helper_count __read_mostly;
40 static bool nf_ct_auto_assign_helper __read_mostly = false;
41 module_param_named(nf_conntrack_helper, nf_ct_auto_assign_helper, bool, 0644);
42 MODULE_PARM_DESC(nf_conntrack_helper,
43 "Enable automatic conntrack helper assignment (default 0)");
45 /* Stupid hash, but collision free for the default registrations of the
46 * helpers currently in the kernel. */
47 static unsigned int helper_hash(const struct nf_conntrack_tuple *tuple)
49 return (((tuple->src.l3num << 8) | tuple->dst.protonum) ^
50 (__force __u16)tuple->src.u.all) % nf_ct_helper_hsize;
53 static struct nf_conntrack_helper *
54 __nf_ct_helper_find(const struct nf_conntrack_tuple *tuple)
56 struct nf_conntrack_helper *helper;
57 struct nf_conntrack_tuple_mask mask = { .src.u.all = htons(0xFFFF) };
60 if (!nf_ct_helper_count)
63 h = helper_hash(tuple);
64 hlist_for_each_entry_rcu(helper, &nf_ct_helper_hash[h], hnode) {
65 if (nf_ct_tuple_src_mask_cmp(tuple, &helper->tuple, &mask))
71 struct nf_conntrack_helper *
72 __nf_conntrack_helper_find(const char *name, u16 l3num, u8 protonum)
74 struct nf_conntrack_helper *h;
77 for (i = 0; i < nf_ct_helper_hsize; i++) {
78 hlist_for_each_entry_rcu(h, &nf_ct_helper_hash[i], hnode) {
79 if (strcmp(h->name, name))
82 if (h->tuple.src.l3num != NFPROTO_UNSPEC &&
83 h->tuple.src.l3num != l3num)
86 if (h->tuple.dst.protonum == protonum)
92 EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find);
94 struct nf_conntrack_helper *
95 nf_conntrack_helper_try_module_get(const char *name, u16 l3num, u8 protonum)
97 struct nf_conntrack_helper *h;
101 h = __nf_conntrack_helper_find(name, l3num, protonum);
102 #ifdef CONFIG_MODULES
105 if (request_module("nfct-helper-%s", name) == 0) {
107 h = __nf_conntrack_helper_find(name, l3num, protonum);
113 if (h != NULL && !try_module_get(h->me))
115 if (h != NULL && !refcount_inc_not_zero(&h->refcnt)) {
124 EXPORT_SYMBOL_GPL(nf_conntrack_helper_try_module_get);
126 void nf_conntrack_helper_put(struct nf_conntrack_helper *helper)
128 refcount_dec(&helper->refcnt);
129 module_put(helper->me);
131 EXPORT_SYMBOL_GPL(nf_conntrack_helper_put);
133 struct nf_conn_help *
134 nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp)
136 struct nf_conn_help *help;
138 help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, gfp);
140 INIT_HLIST_HEAD(&help->expectations);
142 pr_debug("failed to add helper extension area");
145 EXPORT_SYMBOL_GPL(nf_ct_helper_ext_add);
147 static struct nf_conntrack_helper *
148 nf_ct_lookup_helper(struct nf_conn *ct, struct net *net)
150 if (!net->ct.sysctl_auto_assign_helper) {
151 if (net->ct.auto_assign_helper_warned)
153 if (!__nf_ct_helper_find(&ct->tuplehash[IP_CT_DIR_REPLY].tuple))
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;
163 return __nf_ct_helper_find(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
167 int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl,
170 struct nf_conntrack_helper *helper = NULL;
171 struct nf_conn_help *help;
172 struct net *net = nf_ct_net(ct);
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.
180 if (test_bit(IPS_HELPER_BIT, &ct->status))
184 help = nfct_help(tmpl);
186 helper = help->helper;
187 set_bit(IPS_HELPER_BIT, &ct->status);
191 help = nfct_help(ct);
193 if (helper == NULL) {
194 helper = nf_ct_lookup_helper(ct, net);
195 if (helper == NULL) {
197 RCU_INIT_POINTER(help->helper, NULL);
203 help = nf_ct_helper_ext_add(ct, flags);
207 /* We only allow helper re-assignment of the same sort since
208 * we cannot reallocate the helper extension area.
210 struct nf_conntrack_helper *tmp = rcu_dereference(help->helper);
212 if (tmp && tmp->help != helper->help) {
213 RCU_INIT_POINTER(help->helper, NULL);
218 rcu_assign_pointer(help->helper, helper);
222 EXPORT_SYMBOL_GPL(__nf_ct_try_assign_helper);
224 /* appropriate ct lock protecting must be taken by caller */
225 static int unhelp(struct nf_conn *ct, void *me)
227 struct nf_conn_help *help = nfct_help(ct);
229 if (help && rcu_dereference_raw(help->helper) == me) {
230 nf_conntrack_event(IPCT_HELPER, ct);
231 RCU_INIT_POINTER(help->helper, NULL);
234 /* We are not intended to delete this conntrack. */
238 void nf_ct_helper_destroy(struct nf_conn *ct)
240 struct nf_conn_help *help = nfct_help(ct);
241 struct nf_conntrack_helper *helper;
245 helper = rcu_dereference(help->helper);
246 if (helper && helper->destroy)
252 static LIST_HEAD(nf_ct_helper_expectfn_list);
254 void nf_ct_helper_expectfn_register(struct nf_ct_helper_expectfn *n)
256 spin_lock_bh(&nf_conntrack_expect_lock);
257 list_add_rcu(&n->head, &nf_ct_helper_expectfn_list);
258 spin_unlock_bh(&nf_conntrack_expect_lock);
260 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_register);
262 void nf_ct_helper_expectfn_unregister(struct nf_ct_helper_expectfn *n)
264 spin_lock_bh(&nf_conntrack_expect_lock);
265 list_del_rcu(&n->head);
266 spin_unlock_bh(&nf_conntrack_expect_lock);
268 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_unregister);
270 /* Caller should hold the rcu lock */
271 struct nf_ct_helper_expectfn *
272 nf_ct_helper_expectfn_find_by_name(const char *name)
274 struct nf_ct_helper_expectfn *cur;
277 list_for_each_entry_rcu(cur, &nf_ct_helper_expectfn_list, head) {
278 if (!strcmp(cur->name, name)) {
283 return found ? cur : NULL;
285 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_find_by_name);
287 /* Caller should hold the rcu lock */
288 struct nf_ct_helper_expectfn *
289 nf_ct_helper_expectfn_find_by_symbol(const void *symbol)
291 struct nf_ct_helper_expectfn *cur;
294 list_for_each_entry_rcu(cur, &nf_ct_helper_expectfn_list, head) {
295 if (cur->expectfn == symbol) {
300 return found ? cur : NULL;
302 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_find_by_symbol);
305 void nf_ct_helper_log(struct sk_buff *skb, const struct nf_conn *ct,
306 const char *fmt, ...)
308 const struct nf_conn_help *help;
309 const struct nf_conntrack_helper *helper;
310 struct va_format vaf;
318 /* Called from the helper function, this call never fails */
319 help = nfct_help(ct);
321 /* rcu_read_lock()ed by nf_hook_thresh */
322 helper = rcu_dereference(help->helper);
324 nf_log_packet(nf_ct_net(ct), nf_ct_l3num(ct), 0, skb, NULL, NULL, NULL,
325 "nf_ct_%s: dropping packet: %pV ", helper->name, &vaf);
329 EXPORT_SYMBOL_GPL(nf_ct_helper_log);
331 int nf_conntrack_helper_register(struct nf_conntrack_helper *me)
333 struct nf_conntrack_tuple_mask mask = { .src.u.all = htons(0xFFFF) };
334 unsigned int h = helper_hash(&me->tuple);
335 struct nf_conntrack_helper *cur;
338 BUG_ON(me->expect_policy == NULL);
339 BUG_ON(me->expect_class_max >= NF_CT_MAX_EXPECT_CLASSES);
340 BUG_ON(strlen(me->name) > NF_CT_HELPER_NAME_LEN - 1);
342 if (me->expect_policy->max_expected > NF_CT_EXPECT_MAX_CNT)
345 mutex_lock(&nf_ct_helper_mutex);
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) {
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,
368 refcount_set(&me->refcnt, 1);
369 hlist_add_head_rcu(&me->hnode, &nf_ct_helper_hash[h]);
370 nf_ct_helper_count++;
372 mutex_unlock(&nf_ct_helper_mutex);
375 EXPORT_SYMBOL_GPL(nf_conntrack_helper_register);
377 static bool expect_iter_me(struct nf_conntrack_expect *exp, void *data)
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;
383 if (exp->helper == me)
386 this = rcu_dereference_protected(help->helper,
387 lockdep_is_held(&nf_conntrack_expect_lock));
391 void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)
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);
398 /* Make sure every nothing is still using the helper unless its a
399 * connection in the hash.
403 nf_ct_expect_iterate_destroy(expect_iter_me, NULL);
404 nf_ct_iterate_destroy(unhelp, me);
406 /* Maybe someone has gotten the helper already when unhelp above.
407 * So need to wait it.
411 EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister);
413 void 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,
417 u32 expect_class_max,
418 int (*help)(struct sk_buff *skb, unsigned int protoff,
420 enum ip_conntrack_info ctinfo),
421 int (*from_nlattr)(struct nlattr *attr,
423 struct module *module)
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;
431 helper->from_nlattr = from_nlattr;
434 if (spec_port == default_port)
435 snprintf(helper->name, sizeof(helper->name), "%s", name);
437 snprintf(helper->name, sizeof(helper->name), "%s-%u", name, id);
439 EXPORT_SYMBOL_GPL(nf_ct_helper_init);
441 int nf_conntrack_helpers_register(struct nf_conntrack_helper *helper,
447 for (i = 0; i < n; i++) {
448 err = nf_conntrack_helper_register(&helper[i]);
456 nf_conntrack_helpers_unregister(helper, i);
459 EXPORT_SYMBOL_GPL(nf_conntrack_helpers_register);
461 void nf_conntrack_helpers_unregister(struct nf_conntrack_helper *helper,
465 nf_conntrack_helper_unregister(&helper[n]);
467 EXPORT_SYMBOL_GPL(nf_conntrack_helpers_unregister);
469 static const struct nf_ct_ext_type helper_extend = {
470 .len = sizeof(struct nf_conn_help),
471 .align = __alignof__(struct nf_conn_help),
472 .id = NF_CT_EXT_HELPER,
475 void nf_conntrack_helper_pernet_init(struct net *net)
477 net->ct.auto_assign_helper_warned = false;
478 net->ct.sysctl_auto_assign_helper = nf_ct_auto_assign_helper;
481 int nf_conntrack_helper_init(void)
484 nf_ct_helper_hsize = 1; /* gets rounded up to use one page */
486 nf_ct_alloc_hashtable(&nf_ct_helper_hsize, 0);
487 if (!nf_ct_helper_hash)
490 ret = nf_ct_extend_register(&helper_extend);
492 pr_err("nf_ct_helper: Unable to register helper extension.\n");
498 kvfree(nf_ct_helper_hash);
502 void nf_conntrack_helper_fini(void)
504 nf_ct_extend_unregister(&helper_extend);
505 kvfree(nf_ct_helper_hash);