1 /* Helper handling for netfilter. */
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>
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.
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>
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_conntrack_l3proto.h>
26 #include <net/netfilter/nf_conntrack_l4proto.h>
27 #include <net/netfilter/nf_conntrack_helper.h>
28 #include <net/netfilter/nf_conntrack_core.h>
29 #include <net/netfilter/nf_conntrack_extend.h>
31 static __read_mostly LIST_HEAD(helpers);
33 struct nf_conntrack_helper *
34 __nf_ct_helper_find(const struct nf_conntrack_tuple *tuple)
36 struct nf_conntrack_helper *h;
37 struct nf_conntrack_tuple_mask mask = { .src.u.all = htons(0xFFFF) };
39 list_for_each_entry(h, &helpers, list) {
40 if (nf_ct_tuple_src_mask_cmp(tuple, &h->tuple, &mask))
46 struct nf_conntrack_helper *
47 nf_ct_helper_find_get( const struct nf_conntrack_tuple *tuple)
49 struct nf_conntrack_helper *helper;
51 /* need nf_conntrack_lock to assure that helper exists until
52 * try_module_get() is called */
53 read_lock_bh(&nf_conntrack_lock);
55 helper = __nf_ct_helper_find(tuple);
57 /* need to increase module usage count to assure helper will
58 * not go away while the caller is e.g. busy putting a
59 * conntrack in the hash that uses the helper */
60 if (!try_module_get(helper->me))
64 read_unlock_bh(&nf_conntrack_lock);
68 EXPORT_SYMBOL_GPL(nf_ct_helper_find_get);
70 void nf_ct_helper_put(struct nf_conntrack_helper *helper)
72 module_put(helper->me);
74 EXPORT_SYMBOL_GPL(nf_ct_helper_put);
76 struct nf_conntrack_helper *
77 __nf_conntrack_helper_find_byname(const char *name)
79 struct nf_conntrack_helper *h;
81 list_for_each_entry(h, &helpers, list) {
82 if (!strcmp(h->name, name))
88 EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find_byname);
90 struct nf_conn_help *nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp)
92 struct nf_conn_help *help;
94 help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, gfp);
96 INIT_HLIST_HEAD(&help->expectations);
98 pr_debug("failed to add helper extension area");
101 EXPORT_SYMBOL_GPL(nf_ct_helper_ext_add);
103 static inline int unhelp(struct nf_conntrack_tuple_hash *i,
104 const struct nf_conntrack_helper *me)
106 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(i);
107 struct nf_conn_help *help = nfct_help(ct);
109 if (help && help->helper == me) {
110 nf_conntrack_event(IPCT_HELPER, ct);
111 rcu_assign_pointer(help->helper, NULL);
116 int nf_conntrack_helper_register(struct nf_conntrack_helper *me)
118 BUG_ON(me->timeout == 0);
120 write_lock_bh(&nf_conntrack_lock);
121 list_add(&me->list, &helpers);
122 write_unlock_bh(&nf_conntrack_lock);
126 EXPORT_SYMBOL_GPL(nf_conntrack_helper_register);
128 void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)
130 struct nf_conntrack_tuple_hash *h;
131 struct nf_conntrack_expect *exp;
132 struct hlist_node *n, *next;
135 /* Need write lock here, to delete helper. */
136 write_lock_bh(&nf_conntrack_lock);
139 /* Get rid of expectations */
140 for (i = 0; i < nf_ct_expect_hsize; i++) {
141 hlist_for_each_entry_safe(exp, n, next,
142 &nf_ct_expect_hash[i], hnode) {
143 struct nf_conn_help *help = nfct_help(exp->master);
144 if ((help->helper == me || exp->helper == me) &&
145 del_timer(&exp->timeout)) {
146 nf_ct_unlink_expect(exp);
147 nf_ct_expect_put(exp);
152 /* Get rid of expecteds, set helpers to NULL. */
153 hlist_for_each_entry(h, n, &unconfirmed, hnode)
155 for (i = 0; i < nf_conntrack_htable_size; i++) {
156 hlist_for_each_entry(h, n, &nf_conntrack_hash[i], hnode)
159 write_unlock_bh(&nf_conntrack_lock);
161 /* Someone could be still looking at the helper in a bh. */
164 EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister);
166 static struct nf_ct_ext_type helper_extend __read_mostly = {
167 .len = sizeof(struct nf_conn_help),
168 .align = __alignof__(struct nf_conn_help),
169 .id = NF_CT_EXT_HELPER,
172 int nf_conntrack_helper_init()
174 return nf_ct_extend_register(&helper_extend);
177 void nf_conntrack_helper_fini()
179 nf_ct_extend_unregister(&helper_extend);