]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/nf_conntrack_helper.c
[NETFILTER]: nf_conntrack_expect: convert proc functions to hash
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / nf_conntrack_helper.c
CommitLineData
7e5d03bb
MJ
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
7e5d03bb
MJ
24#include <net/netfilter/nf_conntrack.h>
25#include <net/netfilter/nf_conntrack_l3proto.h>
605dcad6 26#include <net/netfilter/nf_conntrack_l4proto.h>
7e5d03bb
MJ
27#include <net/netfilter/nf_conntrack_helper.h>
28#include <net/netfilter/nf_conntrack_core.h>
ceceae1b 29#include <net/netfilter/nf_conntrack_extend.h>
7e5d03bb 30
e2b7606c 31static __read_mostly LIST_HEAD(helpers);
7e5d03bb
MJ
32
33struct nf_conntrack_helper *
34__nf_ct_helper_find(const struct nf_conntrack_tuple *tuple)
35{
36 struct nf_conntrack_helper *h;
d4156e8c 37 struct nf_conntrack_tuple_mask mask = { .src.u.all = htons(0xFFFF) };
7e5d03bb
MJ
38
39 list_for_each_entry(h, &helpers, list) {
d4156e8c 40 if (nf_ct_tuple_src_mask_cmp(tuple, &h->tuple, &mask))
7e5d03bb
MJ
41 return h;
42 }
43 return NULL;
44}
45
46struct nf_conntrack_helper *
47nf_ct_helper_find_get( const struct nf_conntrack_tuple *tuple)
48{
49 struct nf_conntrack_helper *helper;
50
51 /* need nf_conntrack_lock to assure that helper exists until
52 * try_module_get() is called */
53 read_lock_bh(&nf_conntrack_lock);
54
55 helper = __nf_ct_helper_find(tuple);
56 if (helper) {
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))
61 helper = NULL;
62 }
63
64 read_unlock_bh(&nf_conntrack_lock);
65
66 return helper;
67}
13b18339 68EXPORT_SYMBOL_GPL(nf_ct_helper_find_get);
7e5d03bb
MJ
69
70void nf_ct_helper_put(struct nf_conntrack_helper *helper)
71{
72 module_put(helper->me);
73}
13b18339 74EXPORT_SYMBOL_GPL(nf_ct_helper_put);
7e5d03bb
MJ
75
76struct nf_conntrack_helper *
77__nf_conntrack_helper_find_byname(const char *name)
78{
79 struct nf_conntrack_helper *h;
80
81 list_for_each_entry(h, &helpers, list) {
82 if (!strcmp(h->name, name))
83 return h;
84 }
85
86 return NULL;
87}
13b18339 88EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find_byname);
7e5d03bb
MJ
89
90static inline int unhelp(struct nf_conntrack_tuple_hash *i,
91 const struct nf_conntrack_helper *me)
92{
93 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(i);
94 struct nf_conn_help *help = nfct_help(ct);
95
96 if (help && help->helper == me) {
97 nf_conntrack_event(IPCT_HELPER, ct);
3c158f7f 98 rcu_assign_pointer(help->helper, NULL);
7e5d03bb
MJ
99 }
100 return 0;
101}
102
103int nf_conntrack_helper_register(struct nf_conntrack_helper *me)
104{
7e5d03bb
MJ
105 BUG_ON(me->timeout == 0);
106
7e5d03bb
MJ
107 write_lock_bh(&nf_conntrack_lock);
108 list_add(&me->list, &helpers);
109 write_unlock_bh(&nf_conntrack_lock);
110
111 return 0;
112}
13b18339 113EXPORT_SYMBOL_GPL(nf_conntrack_helper_register);
7e5d03bb
MJ
114
115void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)
116{
117 unsigned int i;
118 struct nf_conntrack_tuple_hash *h;
119 struct nf_conntrack_expect *exp, *tmp;
f205c5e0 120 struct hlist_node *n;
7e5d03bb
MJ
121
122 /* Need write lock here, to delete helper. */
123 write_lock_bh(&nf_conntrack_lock);
124 list_del(&me->list);
125
126 /* Get rid of expectations */
6823645d 127 list_for_each_entry_safe(exp, tmp, &nf_ct_expect_list, list) {
7e5d03bb 128 struct nf_conn_help *help = nfct_help(exp->master);
9457d851
PM
129 if ((help->helper == me || exp->helper == me) &&
130 del_timer(&exp->timeout)) {
7e5d03bb 131 nf_ct_unlink_expect(exp);
6823645d 132 nf_ct_expect_put(exp);
7e5d03bb
MJ
133 }
134 }
135
136 /* Get rid of expecteds, set helpers to NULL. */
f205c5e0 137 hlist_for_each_entry(h, n, &unconfirmed, hnode)
7e5d03bb
MJ
138 unhelp(h, me);
139 for (i = 0; i < nf_conntrack_htable_size; i++) {
f205c5e0 140 hlist_for_each_entry(h, n, &nf_conntrack_hash[i], hnode)
7e5d03bb
MJ
141 unhelp(h, me);
142 }
143 write_unlock_bh(&nf_conntrack_lock);
144
145 /* Someone could be still looking at the helper in a bh. */
146 synchronize_net();
147}
13b18339 148EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister);
ceceae1b 149
61eb3107 150static struct nf_ct_ext_type helper_extend __read_mostly = {
ceceae1b
YK
151 .len = sizeof(struct nf_conn_help),
152 .align = __alignof__(struct nf_conn_help),
153 .id = NF_CT_EXT_HELPER,
154};
155
156int nf_conntrack_helper_init()
157{
158 return nf_ct_extend_register(&helper_extend);
159}
160
161void nf_conntrack_helper_fini()
162{
163 nf_ct_extend_unregister(&helper_extend);
164}