blob: f868b7fbd9b4caee49d1e49b560ff6daeaa185bf [file] [log] [blame]
Martin Josefsson7e5d03b2006-11-29 02:34:59 +01001/* 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
Martin Josefsson7e5d03b2006-11-29 02:34:59 +010024#include <net/netfilter/nf_conntrack.h>
25#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010026#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefsson7e5d03b2006-11-29 02:34:59 +010027#include <net/netfilter/nf_conntrack_helper.h>
28#include <net/netfilter/nf_conntrack_core.h>
29
Martin Josefssone2b76062006-11-29 02:35:04 +010030static __read_mostly LIST_HEAD(helpers);
Martin Josefsson7e5d03b2006-11-29 02:34:59 +010031
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}
Patrick McHardy13b18332006-12-02 22:11:25 -080066EXPORT_SYMBOL_GPL(nf_ct_helper_find_get);
Martin Josefsson7e5d03b2006-11-29 02:34:59 +010067
68void nf_ct_helper_put(struct nf_conntrack_helper *helper)
69{
70 module_put(helper->me);
71}
Patrick McHardy13b18332006-12-02 22:11:25 -080072EXPORT_SYMBOL_GPL(nf_ct_helper_put);
Martin Josefsson7e5d03b2006-11-29 02:34:59 +010073
74struct nf_conntrack_helper *
75__nf_conntrack_helper_find_byname(const char *name)
76{
77 struct nf_conntrack_helper *h;
78
79 list_for_each_entry(h, &helpers, list) {
80 if (!strcmp(h->name, name))
81 return h;
82 }
83
84 return NULL;
85}
Patrick McHardy13b18332006-12-02 22:11:25 -080086EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find_byname);
Martin Josefsson7e5d03b2006-11-29 02:34:59 +010087
88static inline int unhelp(struct nf_conntrack_tuple_hash *i,
89 const struct nf_conntrack_helper *me)
90{
91 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(i);
92 struct nf_conn_help *help = nfct_help(ct);
93
94 if (help && help->helper == me) {
95 nf_conntrack_event(IPCT_HELPER, ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -070096 rcu_assign_pointer(help->helper, NULL);
Martin Josefsson7e5d03b2006-11-29 02:34:59 +010097 }
98 return 0;
99}
100
101int nf_conntrack_helper_register(struct nf_conntrack_helper *me)
102{
Patrick McHardyf9aae952006-12-02 22:04:50 -0800103 int size, ret;
104
Martin Josefsson7e5d03b2006-11-29 02:34:59 +0100105 BUG_ON(me->timeout == 0);
106
Patrick McHardyf9aae952006-12-02 22:04:50 -0800107 size = ALIGN(sizeof(struct nf_conn), __alignof__(struct nf_conn_help)) +
108 sizeof(struct nf_conn_help);
Martin Josefsson7e5d03b2006-11-29 02:34:59 +0100109 ret = nf_conntrack_register_cache(NF_CT_F_HELP, "nf_conntrack:help",
Patrick McHardyf9aae952006-12-02 22:04:50 -0800110 size);
Martin Josefsson7e5d03b2006-11-29 02:34:59 +0100111 if (ret < 0) {
112 printk(KERN_ERR "nf_conntrack_helper_register: Unable to create slab cache for conntracks\n");
113 return ret;
114 }
115 write_lock_bh(&nf_conntrack_lock);
116 list_add(&me->list, &helpers);
117 write_unlock_bh(&nf_conntrack_lock);
118
119 return 0;
120}
Patrick McHardy13b18332006-12-02 22:11:25 -0800121EXPORT_SYMBOL_GPL(nf_conntrack_helper_register);
Martin Josefsson7e5d03b2006-11-29 02:34:59 +0100122
123void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)
124{
125 unsigned int i;
126 struct nf_conntrack_tuple_hash *h;
127 struct nf_conntrack_expect *exp, *tmp;
128
129 /* Need write lock here, to delete helper. */
130 write_lock_bh(&nf_conntrack_lock);
131 list_del(&me->list);
132
133 /* Get rid of expectations */
134 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list, list) {
135 struct nf_conn_help *help = nfct_help(exp->master);
Patrick McHardy9457d852006-12-02 22:05:25 -0800136 if ((help->helper == me || exp->helper == me) &&
137 del_timer(&exp->timeout)) {
Martin Josefsson7e5d03b2006-11-29 02:34:59 +0100138 nf_ct_unlink_expect(exp);
139 nf_conntrack_expect_put(exp);
140 }
141 }
142
143 /* Get rid of expecteds, set helpers to NULL. */
144 list_for_each_entry(h, &unconfirmed, list)
145 unhelp(h, me);
146 for (i = 0; i < nf_conntrack_htable_size; i++) {
147 list_for_each_entry(h, &nf_conntrack_hash[i], list)
148 unhelp(h, me);
149 }
150 write_unlock_bh(&nf_conntrack_lock);
151
152 /* Someone could be still looking at the helper in a bh. */
153 synchronize_net();
154}
Patrick McHardy13b18332006-12-02 22:11:25 -0800155EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister);