1 /* L3/L4 protocol support for nf_conntrack. */
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 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/types.h>
14 #include <linux/netfilter.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/mutex.h>
18 #include <linux/vmalloc.h>
19 #include <linux/stddef.h>
20 #include <linux/err.h>
21 #include <linux/percpu.h>
22 #include <linux/notifier.h>
23 #include <linux/kernel.h>
24 #include <linux/netdevice.h>
26 #include <net/netfilter/nf_conntrack.h>
27 #include <net/netfilter/nf_conntrack_l3proto.h>
28 #include <net/netfilter/nf_conntrack_l4proto.h>
29 #include <net/netfilter/nf_conntrack_core.h>
31 static struct nf_conntrack_l4proto __rcu **nf_ct_protos[PF_MAX] __read_mostly;
32 struct nf_conntrack_l3proto __rcu *nf_ct_l3protos[AF_MAX] __read_mostly;
33 EXPORT_SYMBOL_GPL(nf_ct_l3protos);
35 static DEFINE_MUTEX(nf_ct_proto_mutex);
39 nf_ct_register_sysctl(struct net *net,
40 struct ctl_table_header **header,
42 struct ctl_table *table)
44 if (*header == NULL) {
45 *header = register_net_sysctl(net, path, table);
54 nf_ct_unregister_sysctl(struct ctl_table_header **header,
55 struct ctl_table **table,
61 unregister_net_sysctl_table(*header);
68 struct nf_conntrack_l4proto *
69 __nf_ct_l4proto_find(u_int16_t l3proto, u_int8_t l4proto)
71 if (unlikely(l3proto >= AF_MAX || nf_ct_protos[l3proto] == NULL))
72 return &nf_conntrack_l4proto_generic;
74 return rcu_dereference(nf_ct_protos[l3proto][l4proto]);
76 EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find);
78 /* this is guaranteed to always return a valid protocol helper, since
79 * it falls back to generic_protocol */
80 struct nf_conntrack_l3proto *
81 nf_ct_l3proto_find_get(u_int16_t l3proto)
83 struct nf_conntrack_l3proto *p;
86 p = __nf_ct_l3proto_find(l3proto);
87 if (!try_module_get(p->me))
88 p = &nf_conntrack_l3proto_generic;
93 EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get);
96 nf_ct_l3proto_try_module_get(unsigned short l3proto)
99 struct nf_conntrack_l3proto *p;
101 retry: p = nf_ct_l3proto_find_get(l3proto);
102 if (p == &nf_conntrack_l3proto_generic) {
103 ret = request_module("nf_conntrack-%d", l3proto);
112 EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get);
114 void nf_ct_l3proto_module_put(unsigned short l3proto)
116 struct nf_conntrack_l3proto *p;
118 /* rcu_read_lock not necessary since the caller holds a reference, but
119 * taken anyways to avoid lockdep warnings in __nf_ct_l3proto_find()
122 p = __nf_ct_l3proto_find(l3proto);
126 EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put);
128 struct nf_conntrack_l4proto *
129 nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num)
131 struct nf_conntrack_l4proto *p;
134 p = __nf_ct_l4proto_find(l3num, l4num);
135 if (!try_module_get(p->me))
136 p = &nf_conntrack_l4proto_generic;
141 EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get);
143 void nf_ct_l4proto_put(struct nf_conntrack_l4proto *p)
147 EXPORT_SYMBOL_GPL(nf_ct_l4proto_put);
149 static int kill_l3proto(struct nf_conn *i, void *data)
151 return nf_ct_l3num(i) == ((struct nf_conntrack_l3proto *)data)->l3proto;
154 static int kill_l4proto(struct nf_conn *i, void *data)
156 struct nf_conntrack_l4proto *l4proto;
157 l4proto = (struct nf_conntrack_l4proto *)data;
158 return nf_ct_protonum(i) == l4proto->l4proto &&
159 nf_ct_l3num(i) == l4proto->l3proto;
162 int nf_ct_l3proto_register(struct nf_conntrack_l3proto *proto)
165 struct nf_conntrack_l3proto *old;
167 if (proto->l3proto >= AF_MAX)
170 if (proto->tuple_to_nlattr && !proto->nlattr_tuple_size)
173 mutex_lock(&nf_ct_proto_mutex);
174 old = rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
175 lockdep_is_held(&nf_ct_proto_mutex));
176 if (old != &nf_conntrack_l3proto_generic) {
181 if (proto->nlattr_tuple_size)
182 proto->nla_size = 3 * proto->nlattr_tuple_size();
184 rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto);
187 mutex_unlock(&nf_ct_proto_mutex);
191 EXPORT_SYMBOL_GPL(nf_ct_l3proto_register);
193 int nf_ct_l3proto_pernet_register(struct net *net,
194 struct nf_conntrack_l3proto *proto)
198 if (proto->init_net) {
199 ret = proto->init_net(net);
206 EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_register);
208 void nf_ct_l3proto_unregister(struct nf_conntrack_l3proto *proto)
210 BUG_ON(proto->l3proto >= AF_MAX);
212 mutex_lock(&nf_ct_proto_mutex);
213 BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
214 lockdep_is_held(&nf_ct_proto_mutex)
216 rcu_assign_pointer(nf_ct_l3protos[proto->l3proto],
217 &nf_conntrack_l3proto_generic);
218 mutex_unlock(&nf_ct_proto_mutex);
222 EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister);
224 void nf_ct_l3proto_pernet_unregister(struct net *net,
225 struct nf_conntrack_l3proto *proto)
227 /* Remove all contrack entries for this protocol */
228 nf_ct_iterate_cleanup(net, kill_l3proto, proto, 0, 0);
230 EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_unregister);
232 static struct nf_proto_net *nf_ct_l4proto_net(struct net *net,
233 struct nf_conntrack_l4proto *l4proto)
235 if (l4proto->get_net_proto) {
236 /* statically built-in protocols use static per-net */
237 return l4proto->get_net_proto(net);
238 } else if (l4proto->net_id) {
239 /* ... and loadable protocols use dynamic per-net */
240 return net_generic(net, *l4proto->net_id);
246 int nf_ct_l4proto_register_sysctl(struct net *net,
247 struct nf_proto_net *pn,
248 struct nf_conntrack_l4proto *l4proto)
253 if (pn->ctl_table != NULL) {
254 err = nf_ct_register_sysctl(net,
255 &pn->ctl_table_header,
260 kfree(pn->ctl_table);
261 pn->ctl_table = NULL;
265 #endif /* CONFIG_SYSCTL */
270 void nf_ct_l4proto_unregister_sysctl(struct net *net,
271 struct nf_proto_net *pn,
272 struct nf_conntrack_l4proto *l4proto)
275 if (pn->ctl_table_header != NULL)
276 nf_ct_unregister_sysctl(&pn->ctl_table_header,
279 #endif /* CONFIG_SYSCTL */
282 /* FIXME: Allow NULL functions and sub in pointers to generic for
284 int nf_ct_l4proto_register(struct nf_conntrack_l4proto *l4proto)
288 if (l4proto->l3proto >= PF_MAX)
291 if ((l4proto->to_nlattr && !l4proto->nlattr_size)
292 || (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size))
295 mutex_lock(&nf_ct_proto_mutex);
296 if (!nf_ct_protos[l4proto->l3proto]) {
297 /* l3proto may be loaded latter. */
298 struct nf_conntrack_l4proto __rcu **proto_array;
301 proto_array = kmalloc(MAX_NF_CT_PROTO *
302 sizeof(struct nf_conntrack_l4proto *),
304 if (proto_array == NULL) {
309 for (i = 0; i < MAX_NF_CT_PROTO; i++)
310 RCU_INIT_POINTER(proto_array[i], &nf_conntrack_l4proto_generic);
312 /* Before making proto_array visible to lockless readers,
313 * we must make sure its content is committed to memory.
317 nf_ct_protos[l4proto->l3proto] = proto_array;
318 } else if (rcu_dereference_protected(
319 nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
320 lockdep_is_held(&nf_ct_proto_mutex)
321 ) != &nf_conntrack_l4proto_generic) {
326 l4proto->nla_size = 0;
327 if (l4proto->nlattr_size)
328 l4proto->nla_size += l4proto->nlattr_size();
329 if (l4proto->nlattr_tuple_size)
330 l4proto->nla_size += 3 * l4proto->nlattr_tuple_size();
332 rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
335 mutex_unlock(&nf_ct_proto_mutex);
338 EXPORT_SYMBOL_GPL(nf_ct_l4proto_register);
340 int nf_ct_l4proto_pernet_register(struct net *net,
341 struct nf_conntrack_l4proto *l4proto)
344 struct nf_proto_net *pn = NULL;
346 if (l4proto->init_net) {
347 ret = l4proto->init_net(net, l4proto->l3proto);
352 pn = nf_ct_l4proto_net(net, l4proto);
356 ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto);
364 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register);
366 void nf_ct_l4proto_unregister(struct nf_conntrack_l4proto *l4proto)
368 BUG_ON(l4proto->l3proto >= PF_MAX);
370 mutex_lock(&nf_ct_proto_mutex);
371 BUG_ON(rcu_dereference_protected(
372 nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
373 lockdep_is_held(&nf_ct_proto_mutex)
375 rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
376 &nf_conntrack_l4proto_generic);
377 mutex_unlock(&nf_ct_proto_mutex);
381 EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister);
383 void nf_ct_l4proto_pernet_unregister(struct net *net,
384 struct nf_conntrack_l4proto *l4proto)
386 struct nf_proto_net *pn = NULL;
388 pn = nf_ct_l4proto_net(net, l4proto);
393 nf_ct_l4proto_unregister_sysctl(net, pn, l4proto);
395 /* Remove all contrack entries for this protocol */
396 nf_ct_iterate_cleanup(net, kill_l4proto, l4proto, 0, 0);
398 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister);
400 int nf_conntrack_proto_pernet_init(struct net *net)
403 struct nf_proto_net *pn = nf_ct_l4proto_net(net,
404 &nf_conntrack_l4proto_generic);
406 err = nf_conntrack_l4proto_generic.init_net(net,
407 nf_conntrack_l4proto_generic.l3proto);
410 err = nf_ct_l4proto_register_sysctl(net,
412 &nf_conntrack_l4proto_generic);
420 void nf_conntrack_proto_pernet_fini(struct net *net)
422 struct nf_proto_net *pn = nf_ct_l4proto_net(net,
423 &nf_conntrack_l4proto_generic);
426 nf_ct_l4proto_unregister_sysctl(net,
428 &nf_conntrack_l4proto_generic);
431 int nf_conntrack_proto_init(void)
434 for (i = 0; i < AF_MAX; i++)
435 rcu_assign_pointer(nf_ct_l3protos[i],
436 &nf_conntrack_l3proto_generic);
440 void nf_conntrack_proto_fini(void)
443 /* free l3proto protocol tables */
444 for (i = 0; i < PF_MAX; i++)
445 kfree(nf_ct_protos[i]);