nf_conntrack_helper.c revision 9457d851fc5df54522d733f72cbb1f02ab59272e
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>
6 *
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.
10 */
11
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>
23
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
30static __read_mostly LIST_HEAD(helpers);
31
32struct nf_conntrack_helper *
33__nf_ct_helper_find(const struct nf_conntrack_tuple *tuple)
34{
35	struct nf_conntrack_helper *h;
36
37	list_for_each_entry(h, &helpers, list) {
38		if (nf_ct_tuple_mask_cmp(tuple, &h->tuple, &h->mask))
39			return h;
40	}
41	return NULL;
42}
43
44struct nf_conntrack_helper *
45nf_ct_helper_find_get( const struct nf_conntrack_tuple *tuple)
46{
47	struct nf_conntrack_helper *helper;
48
49	/* need nf_conntrack_lock to assure that helper exists until
50	 * try_module_get() is called */
51	read_lock_bh(&nf_conntrack_lock);
52
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;
60	}
61
62	read_unlock_bh(&nf_conntrack_lock);
63
64	return helper;
65}
66
67void nf_ct_helper_put(struct nf_conntrack_helper *helper)
68{
69	module_put(helper->me);
70}
71
72struct nf_conntrack_helper *
73__nf_conntrack_helper_find_byname(const char *name)
74{
75	struct nf_conntrack_helper *h;
76
77	list_for_each_entry(h, &helpers, list) {
78		if (!strcmp(h->name, name))
79			return h;
80	}
81
82	return NULL;
83}
84
85static inline int unhelp(struct nf_conntrack_tuple_hash *i,
86			 const struct nf_conntrack_helper *me)
87{
88	struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(i);
89	struct nf_conn_help *help = nfct_help(ct);
90
91	if (help && help->helper == me) {
92		nf_conntrack_event(IPCT_HELPER, ct);
93		help->helper = NULL;
94	}
95	return 0;
96}
97
98int nf_conntrack_helper_register(struct nf_conntrack_helper *me)
99{
100	int size, ret;
101
102	BUG_ON(me->timeout == 0);
103
104	size = ALIGN(sizeof(struct nf_conn), __alignof__(struct nf_conn_help)) +
105	       sizeof(struct nf_conn_help);
106	ret = nf_conntrack_register_cache(NF_CT_F_HELP, "nf_conntrack:help",
107					  size);
108	if (ret < 0) {
109		printk(KERN_ERR "nf_conntrack_helper_register: Unable to create slab cache for conntracks\n");
110		return ret;
111	}
112	write_lock_bh(&nf_conntrack_lock);
113	list_add(&me->list, &helpers);
114	write_unlock_bh(&nf_conntrack_lock);
115
116	return 0;
117}
118
119void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)
120{
121	unsigned int i;
122	struct nf_conntrack_tuple_hash *h;
123	struct nf_conntrack_expect *exp, *tmp;
124
125	/* Need write lock here, to delete helper. */
126	write_lock_bh(&nf_conntrack_lock);
127	list_del(&me->list);
128
129	/* Get rid of expectations */
130	list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list, list) {
131		struct nf_conn_help *help = nfct_help(exp->master);
132		if ((help->helper == me || exp->helper == me) &&
133		    del_timer(&exp->timeout)) {
134			nf_ct_unlink_expect(exp);
135			nf_conntrack_expect_put(exp);
136		}
137	}
138
139	/* Get rid of expecteds, set helpers to NULL. */
140	list_for_each_entry(h, &unconfirmed, list)
141		unhelp(h, me);
142	for (i = 0; i < nf_conntrack_htable_size; i++) {
143		list_for_each_entry(h, &nf_conntrack_hash[i], list)
144			unhelp(h, me);
145	}
146	write_unlock_bh(&nf_conntrack_lock);
147
148	/* Someone could be still looking at the helper in a bh. */
149	synchronize_net();
150}
151