i386: Reserve the right performance counter for the Intel PerfMon NMI watchdog
[usb.git] / net / netfilter / nf_conntrack_helper.c
blobca10df40784f960c820b90ff029a103aeac5185a
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 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;
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)
41 return (((tuple->src.l3num << 8) | tuple->dst.protonum) ^
42 tuple->src.u.all) % nf_ct_helper_hsize;
45 struct nf_conntrack_helper *
46 __nf_ct_helper_find(const struct nf_conntrack_tuple *tuple)
48 struct nf_conntrack_helper *helper;
49 struct nf_conntrack_tuple_mask mask = { .src.u.all = htons(0xFFFF) };
50 struct hlist_node *n;
51 unsigned int h;
53 if (!nf_ct_helper_count)
54 return NULL;
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;
61 return NULL;
64 struct nf_conntrack_helper *
65 nf_ct_helper_find_get(const struct nf_conntrack_tuple *tuple)
67 struct nf_conntrack_helper *helper;
69 /* need nf_conntrack_lock to assure that helper exists until
70 * try_module_get() is called */
71 read_lock_bh(&nf_conntrack_lock);
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;
82 read_unlock_bh(&nf_conntrack_lock);
84 return helper;
86 EXPORT_SYMBOL_GPL(nf_ct_helper_find_get);
88 void nf_ct_helper_put(struct nf_conntrack_helper *helper)
90 module_put(helper->me);
92 EXPORT_SYMBOL_GPL(nf_ct_helper_put);
94 struct nf_conntrack_helper *
95 __nf_conntrack_helper_find_byname(const char *name)
97 struct nf_conntrack_helper *h;
98 struct hlist_node *n;
99 unsigned int i;
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;
107 return NULL;
109 EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find_byname);
111 struct nf_conn_help *nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp)
113 struct nf_conn_help *help;
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;
122 EXPORT_SYMBOL_GPL(nf_ct_helper_ext_add);
124 static inline int unhelp(struct nf_conntrack_tuple_hash *i,
125 const struct nf_conntrack_helper *me)
127 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(i);
128 struct nf_conn_help *help = nfct_help(ct);
130 if (help && help->helper == me) {
131 nf_conntrack_event(IPCT_HELPER, ct);
132 rcu_assign_pointer(help->helper, NULL);
134 return 0;
137 int nf_conntrack_helper_register(struct nf_conntrack_helper *me)
139 unsigned int h = helper_hash(&me->tuple);
141 BUG_ON(me->timeout == 0);
143 write_lock_bh(&nf_conntrack_lock);
144 hlist_add_head(&me->hnode, &nf_ct_helper_hash[h]);
145 nf_ct_helper_count++;
146 write_unlock_bh(&nf_conntrack_lock);
148 return 0;
150 EXPORT_SYMBOL_GPL(nf_conntrack_helper_register);
152 void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)
154 struct nf_conntrack_tuple_hash *h;
155 struct nf_conntrack_expect *exp;
156 struct hlist_node *n, *next;
157 unsigned int i;
159 /* Need write lock here, to delete helper. */
160 write_lock_bh(&nf_conntrack_lock);
161 hlist_del(&me->hnode);
162 nf_ct_helper_count--;
164 /* Get rid of expectations */
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);
177 /* Get rid of expecteds, set helpers to NULL. */
178 hlist_for_each_entry(h, n, &unconfirmed, hnode)
179 unhelp(h, me);
180 for (i = 0; i < nf_conntrack_htable_size; i++) {
181 hlist_for_each_entry(h, n, &nf_conntrack_hash[i], hnode)
182 unhelp(h, me);
184 write_unlock_bh(&nf_conntrack_lock);
186 /* Someone could be still looking at the helper in a bh. */
187 synchronize_net();
189 EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister);
191 static struct nf_ct_ext_type helper_extend __read_mostly = {
192 .len = sizeof(struct nf_conn_help),
193 .align = __alignof__(struct nf_conn_help),
194 .id = NF_CT_EXT_HELPER,
197 int nf_conntrack_helper_init(void)
199 int err;
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;
207 err = nf_ct_extend_register(&helper_extend);
208 if (err < 0)
209 goto err1;
211 return 0;
213 err1:
214 nf_ct_free_hashtable(nf_ct_helper_hash, nf_ct_helper_vmalloc,
215 nf_ct_helper_hsize);
216 return err;
219 void nf_conntrack_helper_fini(void)
221 nf_ct_extend_unregister(&helper_extend);
222 nf_ct_free_hashtable(nf_ct_helper_hash, nf_ct_helper_vmalloc,
223 nf_ct_helper_hsize);