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);
95 void nf_ct_l3proto_put(struct nf_conntrack_l3proto *p)
99 EXPORT_SYMBOL_GPL(nf_ct_l3proto_put);
102 nf_ct_l3proto_try_module_get(unsigned short l3proto)
105 struct nf_conntrack_l3proto *p;
107 retry: p = nf_ct_l3proto_find_get(l3proto);
108 if (p == &nf_conntrack_l3proto_generic) {
109 ret = request_module("nf_conntrack-%d", l3proto);
118 EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get);
120 void nf_ct_l3proto_module_put(unsigned short l3proto)
122 struct nf_conntrack_l3proto *p;
124 /* rcu_read_lock not necessary since the caller holds a reference, but
125 * taken anyways to avoid lockdep warnings in __nf_ct_l3proto_find()
128 p = __nf_ct_l3proto_find(l3proto);
132 EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put);
134 struct nf_conntrack_l4proto *
135 nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num)
137 struct nf_conntrack_l4proto *p;
140 p = __nf_ct_l4proto_find(l3num, l4num);
141 if (!try_module_get(p->me))
142 p = &nf_conntrack_l4proto_generic;
147 EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get);
149 void nf_ct_l4proto_put(struct nf_conntrack_l4proto *p)
153 EXPORT_SYMBOL_GPL(nf_ct_l4proto_put);
155 static int kill_l3proto(struct nf_conn *i, void *data)
157 return nf_ct_l3num(i) == ((struct nf_conntrack_l3proto *)data)->l3proto;
160 static int kill_l4proto(struct nf_conn *i, void *data)
162 struct nf_conntrack_l4proto *l4proto;
163 l4proto = (struct nf_conntrack_l4proto *)data;
164 return nf_ct_protonum(i) == l4proto->l4proto &&
165 nf_ct_l3num(i) == l4proto->l3proto;
168 static struct nf_ip_net *nf_ct_l3proto_net(struct net *net,
169 struct nf_conntrack_l3proto *l3proto)
171 if (l3proto->l3proto == PF_INET)
172 return &net->ct.nf_ct_proto;
177 static int nf_ct_l3proto_register_sysctl(struct net *net,
178 struct nf_conntrack_l3proto *l3proto)
181 struct nf_ip_net *in = nf_ct_l3proto_net(net, l3proto);
182 /* nf_conntrack_l3proto_ipv6 doesn't support sysctl */
186 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
187 if (in->ctl_table != NULL) {
188 err = nf_ct_register_sysctl(net,
189 &in->ctl_table_header,
190 l3proto->ctl_table_path,
193 kfree(in->ctl_table);
194 in->ctl_table = NULL;
201 static void nf_ct_l3proto_unregister_sysctl(struct net *net,
202 struct nf_conntrack_l3proto *l3proto)
204 struct nf_ip_net *in = nf_ct_l3proto_net(net, l3proto);
208 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
209 if (in->ctl_table_header != NULL)
210 nf_ct_unregister_sysctl(&in->ctl_table_header,
216 int nf_ct_l3proto_register(struct nf_conntrack_l3proto *proto)
219 struct nf_conntrack_l3proto *old;
221 if (proto->l3proto >= AF_MAX)
224 if (proto->tuple_to_nlattr && !proto->nlattr_tuple_size)
227 mutex_lock(&nf_ct_proto_mutex);
228 old = rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
229 lockdep_is_held(&nf_ct_proto_mutex));
230 if (old != &nf_conntrack_l3proto_generic) {
235 if (proto->nlattr_tuple_size)
236 proto->nla_size = 3 * proto->nlattr_tuple_size();
238 rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto);
241 mutex_unlock(&nf_ct_proto_mutex);
245 EXPORT_SYMBOL_GPL(nf_ct_l3proto_register);
247 int nf_ct_l3proto_pernet_register(struct net *net,
248 struct nf_conntrack_l3proto *proto)
252 if (proto->init_net) {
253 ret = proto->init_net(net);
258 return nf_ct_l3proto_register_sysctl(net, proto);
260 EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_register);
262 void nf_ct_l3proto_unregister(struct nf_conntrack_l3proto *proto)
264 BUG_ON(proto->l3proto >= AF_MAX);
266 mutex_lock(&nf_ct_proto_mutex);
267 BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
268 lockdep_is_held(&nf_ct_proto_mutex)
270 rcu_assign_pointer(nf_ct_l3protos[proto->l3proto],
271 &nf_conntrack_l3proto_generic);
272 mutex_unlock(&nf_ct_proto_mutex);
276 EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister);
278 void nf_ct_l3proto_pernet_unregister(struct net *net,
279 struct nf_conntrack_l3proto *proto)
281 nf_ct_l3proto_unregister_sysctl(net, proto);
283 /* Remove all contrack entries for this protocol */
284 nf_ct_iterate_cleanup(net, kill_l3proto, proto);
286 EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_unregister);
288 static struct nf_proto_net *nf_ct_l4proto_net(struct net *net,
289 struct nf_conntrack_l4proto *l4proto)
291 if (l4proto->get_net_proto) {
292 /* statically built-in protocols use static per-net */
293 return l4proto->get_net_proto(net);
294 } else if (l4proto->net_id) {
295 /* ... and loadable protocols use dynamic per-net */
296 return net_generic(net, *l4proto->net_id);
302 int nf_ct_l4proto_register_sysctl(struct net *net,
303 struct nf_proto_net *pn,
304 struct nf_conntrack_l4proto *l4proto)
309 if (pn->ctl_table != NULL) {
310 err = nf_ct_register_sysctl(net,
311 &pn->ctl_table_header,
316 kfree(pn->ctl_table);
317 pn->ctl_table = NULL;
321 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
322 if (l4proto->l3proto != AF_INET6 && pn->ctl_compat_table != NULL) {
324 nf_ct_kfree_compat_sysctl_table(pn);
327 err = nf_ct_register_sysctl(net,
328 &pn->ctl_compat_header,
329 "net/ipv4/netfilter",
330 pn->ctl_compat_table);
334 nf_ct_kfree_compat_sysctl_table(pn);
335 nf_ct_unregister_sysctl(&pn->ctl_table_header,
340 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
341 #endif /* CONFIG_SYSCTL */
346 void nf_ct_l4proto_unregister_sysctl(struct net *net,
347 struct nf_proto_net *pn,
348 struct nf_conntrack_l4proto *l4proto)
351 if (pn->ctl_table_header != NULL)
352 nf_ct_unregister_sysctl(&pn->ctl_table_header,
356 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
357 if (l4proto->l3proto != AF_INET6 && pn->ctl_compat_header != NULL)
358 nf_ct_unregister_sysctl(&pn->ctl_compat_header,
359 &pn->ctl_compat_table,
361 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
362 #endif /* CONFIG_SYSCTL */
365 /* FIXME: Allow NULL functions and sub in pointers to generic for
367 int nf_ct_l4proto_register(struct nf_conntrack_l4proto *l4proto)
371 if (l4proto->l3proto >= PF_MAX)
374 if ((l4proto->to_nlattr && !l4proto->nlattr_size)
375 || (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size))
378 mutex_lock(&nf_ct_proto_mutex);
379 if (!nf_ct_protos[l4proto->l3proto]) {
380 /* l3proto may be loaded latter. */
381 struct nf_conntrack_l4proto __rcu **proto_array;
384 proto_array = kmalloc(MAX_NF_CT_PROTO *
385 sizeof(struct nf_conntrack_l4proto *),
387 if (proto_array == NULL) {
392 for (i = 0; i < MAX_NF_CT_PROTO; i++)
393 RCU_INIT_POINTER(proto_array[i], &nf_conntrack_l4proto_generic);
395 /* Before making proto_array visible to lockless readers,
396 * we must make sure its content is committed to memory.
400 nf_ct_protos[l4proto->l3proto] = proto_array;
401 } else if (rcu_dereference_protected(
402 nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
403 lockdep_is_held(&nf_ct_proto_mutex)
404 ) != &nf_conntrack_l4proto_generic) {
409 l4proto->nla_size = 0;
410 if (l4proto->nlattr_size)
411 l4proto->nla_size += l4proto->nlattr_size();
412 if (l4proto->nlattr_tuple_size)
413 l4proto->nla_size += 3 * l4proto->nlattr_tuple_size();
415 rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
418 mutex_unlock(&nf_ct_proto_mutex);
421 EXPORT_SYMBOL_GPL(nf_ct_l4proto_register);
423 int nf_ct_l4proto_pernet_register(struct net *net,
424 struct nf_conntrack_l4proto *l4proto)
427 struct nf_proto_net *pn = NULL;
429 if (l4proto->init_net) {
430 ret = l4proto->init_net(net, l4proto->l3proto);
435 pn = nf_ct_l4proto_net(net, l4proto);
439 ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto);
447 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register);
449 void nf_ct_l4proto_unregister(struct nf_conntrack_l4proto *l4proto)
451 BUG_ON(l4proto->l3proto >= PF_MAX);
453 mutex_lock(&nf_ct_proto_mutex);
454 BUG_ON(rcu_dereference_protected(
455 nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
456 lockdep_is_held(&nf_ct_proto_mutex)
458 rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
459 &nf_conntrack_l4proto_generic);
460 mutex_unlock(&nf_ct_proto_mutex);
464 EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister);
466 void nf_ct_l4proto_pernet_unregister(struct net *net,
467 struct nf_conntrack_l4proto *l4proto)
469 struct nf_proto_net *pn = NULL;
471 pn = nf_ct_l4proto_net(net, l4proto);
476 nf_ct_l4proto_unregister_sysctl(net, pn, l4proto);
478 /* Remove all contrack entries for this protocol */
479 nf_ct_iterate_cleanup(net, kill_l4proto, l4proto);
481 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister);
483 int nf_conntrack_proto_pernet_init(struct net *net)
486 struct nf_proto_net *pn = nf_ct_l4proto_net(net,
487 &nf_conntrack_l4proto_generic);
489 err = nf_conntrack_l4proto_generic.init_net(net,
490 nf_conntrack_l4proto_generic.l3proto);
493 err = nf_ct_l4proto_register_sysctl(net,
495 &nf_conntrack_l4proto_generic);
503 void nf_conntrack_proto_pernet_fini(struct net *net)
505 struct nf_proto_net *pn = nf_ct_l4proto_net(net,
506 &nf_conntrack_l4proto_generic);
509 nf_ct_l4proto_unregister_sysctl(net,
511 &nf_conntrack_l4proto_generic);
514 int nf_conntrack_proto_init(void)
517 for (i = 0; i < AF_MAX; i++)
518 rcu_assign_pointer(nf_ct_l3protos[i],
519 &nf_conntrack_l3proto_generic);
523 void nf_conntrack_proto_fini(void)
526 /* free l3proto protocol tables */
527 for (i = 0; i < PF_MAX; i++)
528 kfree(nf_ct_protos[i]);