allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / netfilter / nf_conntrack_helper.c
blobaa8963ac36a5f6a47d20d3ac14f581317a722d02
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>
30 static __read_mostly LIST_HEAD(helpers);
32 struct nf_conntrack_helper *
33 __nf_ct_helper_find(const struct nf_conntrack_tuple *tuple)
35 struct nf_conntrack_helper *h;
37 list_for_each_entry(h, &helpers, list) {
38 if (nf_ct_tuple_mask_cmp(tuple, &h->tuple, &h->mask))
39 return h;
41 return NULL;
44 struct nf_conntrack_helper *
45 nf_ct_helper_find_get( const struct nf_conntrack_tuple *tuple)
47 struct nf_conntrack_helper *helper;
49 /* need nf_conntrack_lock to assure that helper exists until
50 * try_module_get() is called */
51 read_lock_bh(&nf_conntrack_lock);
53 helper = __nf_ct_helper_find(tuple);
54 if (helper) {
55 /* need to increase module usage count to assure helper will
56 * not go away while the caller is e.g. busy putting a
57 * conntrack in the hash that uses the helper */
58 if (!try_module_get(helper->me))
59 helper = NULL;
62 read_unlock_bh(&nf_conntrack_lock);
64 return helper;
66 EXPORT_SYMBOL_GPL(nf_ct_helper_find_get);
68 void nf_ct_helper_put(struct nf_conntrack_helper *helper)
70 module_put(helper->me);
72 EXPORT_SYMBOL_GPL(nf_ct_helper_put);
74 struct nf_conntrack_helper *
75 __nf_conntrack_helper_find_byname(const char *name)
77 struct nf_conntrack_helper *h;
79 list_for_each_entry(h, &helpers, list) {
80 if (!strcmp(h->name, name))
81 return h;
84 return NULL;
86 EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find_byname);
88 static inline int unhelp(struct nf_conntrack_tuple_hash *i,
89 const struct nf_conntrack_helper *me)
91 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(i);
92 struct nf_conn_help *help = nfct_help(ct);
94 if (help && help->helper == me) {
95 nf_conntrack_event(IPCT_HELPER, ct);
96 rcu_assign_pointer(help->helper, NULL);
98 return 0;
101 int nf_conntrack_helper_register(struct nf_conntrack_helper *me)
103 int size, ret;
105 BUG_ON(me->expect_policy == NULL);
106 BUG_ON(me->expect_class_max >= NF_CT_MAX_EXPECT_CLASSES);
108 size = ALIGN(sizeof(struct nf_conn), __alignof__(struct nf_conn_help)) +
109 sizeof(struct nf_conn_help);
110 ret = nf_conntrack_register_cache(NF_CT_F_HELP, "nf_conntrack:help",
111 size);
112 if (ret < 0) {
113 printk(KERN_ERR "nf_conntrack_helper_register: Unable to create slab cache for conntracks\n");
114 return ret;
116 write_lock_bh(&nf_conntrack_lock);
117 list_add(&me->list, &helpers);
118 write_unlock_bh(&nf_conntrack_lock);
120 return 0;
122 EXPORT_SYMBOL_GPL(nf_conntrack_helper_register);
124 void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)
126 unsigned int i;
127 struct nf_conntrack_tuple_hash *h;
128 struct nf_conntrack_expect *exp, *tmp;
130 /* Need write lock here, to delete helper. */
131 write_lock_bh(&nf_conntrack_lock);
132 list_del(&me->list);
134 /* Get rid of expectations */
135 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list, list) {
136 struct nf_conn_help *help = nfct_help(exp->master);
137 if ((help->helper == me || exp->helper == me) &&
138 del_timer(&exp->timeout)) {
139 nf_ct_unlink_expect(exp);
140 nf_conntrack_expect_put(exp);
144 /* Get rid of expecteds, set helpers to NULL. */
145 list_for_each_entry(h, &unconfirmed, list)
146 unhelp(h, me);
147 for (i = 0; i < nf_conntrack_htable_size; i++) {
148 list_for_each_entry(h, &nf_conntrack_hash[i], list)
149 unhelp(h, me);
151 write_unlock_bh(&nf_conntrack_lock);
153 /* Someone could be still looking at the helper in a bh. */
154 synchronize_net();
156 EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister);