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
[NFPROTO_NUMPROTO
] __read_mostly
;
32 struct nf_conntrack_l3proto __rcu
*nf_ct_l3protos
[NFPROTO_NUMPROTO
] __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
>= NFPROTO_NUMPROTO
|| 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 int nf_ct_netns_get(struct net
*net
, u8 nfproto
)
130 const struct nf_conntrack_l3proto
*l3proto
;
135 ret
= nf_ct_l3proto_try_module_get(nfproto
);
139 /* we already have a reference, can't fail */
141 l3proto
= __nf_ct_l3proto_find(nfproto
);
144 if (!l3proto
->net_ns_get
)
147 ret
= l3proto
->net_ns_get(net
);
149 nf_ct_l3proto_module_put(nfproto
);
153 EXPORT_SYMBOL_GPL(nf_ct_netns_get
);
155 void nf_ct_netns_put(struct net
*net
, u8 nfproto
)
157 const struct nf_conntrack_l3proto
*l3proto
;
161 /* same as nf_conntrack_netns_get(), reference assumed */
163 l3proto
= __nf_ct_l3proto_find(nfproto
);
166 if (WARN_ON(!l3proto
))
169 if (l3proto
->net_ns_put
)
170 l3proto
->net_ns_put(net
);
172 nf_ct_l3proto_module_put(nfproto
);
174 EXPORT_SYMBOL_GPL(nf_ct_netns_put
);
176 struct nf_conntrack_l4proto
*
177 nf_ct_l4proto_find_get(u_int16_t l3num
, u_int8_t l4num
)
179 struct nf_conntrack_l4proto
*p
;
182 p
= __nf_ct_l4proto_find(l3num
, l4num
);
183 if (!try_module_get(p
->me
))
184 p
= &nf_conntrack_l4proto_generic
;
189 EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get
);
191 void nf_ct_l4proto_put(const struct nf_conntrack_l4proto
*p
)
195 EXPORT_SYMBOL_GPL(nf_ct_l4proto_put
);
197 static int kill_l3proto(struct nf_conn
*i
, void *data
)
199 return nf_ct_l3num(i
) == ((struct nf_conntrack_l3proto
*)data
)->l3proto
;
202 static int kill_l4proto(struct nf_conn
*i
, void *data
)
204 struct nf_conntrack_l4proto
*l4proto
;
206 return nf_ct_protonum(i
) == l4proto
->l4proto
&&
207 nf_ct_l3num(i
) == l4proto
->l3proto
;
210 int nf_ct_l3proto_register(struct nf_conntrack_l3proto
*proto
)
213 struct nf_conntrack_l3proto
*old
;
215 if (proto
->l3proto
>= NFPROTO_NUMPROTO
)
218 if (proto
->tuple_to_nlattr
&& !proto
->nlattr_tuple_size
)
221 mutex_lock(&nf_ct_proto_mutex
);
222 old
= rcu_dereference_protected(nf_ct_l3protos
[proto
->l3proto
],
223 lockdep_is_held(&nf_ct_proto_mutex
));
224 if (old
!= &nf_conntrack_l3proto_generic
) {
229 if (proto
->nlattr_tuple_size
)
230 proto
->nla_size
= 3 * proto
->nlattr_tuple_size();
232 rcu_assign_pointer(nf_ct_l3protos
[proto
->l3proto
], proto
);
235 mutex_unlock(&nf_ct_proto_mutex
);
239 EXPORT_SYMBOL_GPL(nf_ct_l3proto_register
);
241 void nf_ct_l3proto_unregister(struct nf_conntrack_l3proto
*proto
)
243 BUG_ON(proto
->l3proto
>= NFPROTO_NUMPROTO
);
245 mutex_lock(&nf_ct_proto_mutex
);
246 BUG_ON(rcu_dereference_protected(nf_ct_l3protos
[proto
->l3proto
],
247 lockdep_is_held(&nf_ct_proto_mutex
)
249 rcu_assign_pointer(nf_ct_l3protos
[proto
->l3proto
],
250 &nf_conntrack_l3proto_generic
);
251 mutex_unlock(&nf_ct_proto_mutex
);
254 /* Remove all contrack entries for this protocol */
255 nf_ct_iterate_destroy(kill_l3proto
, proto
);
257 EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister
);
259 static struct nf_proto_net
*nf_ct_l4proto_net(struct net
*net
,
260 const struct nf_conntrack_l4proto
*l4proto
)
262 if (l4proto
->get_net_proto
) {
263 /* statically built-in protocols use static per-net */
264 return l4proto
->get_net_proto(net
);
265 } else if (l4proto
->net_id
) {
266 /* ... and loadable protocols use dynamic per-net */
267 return net_generic(net
, *l4proto
->net_id
);
273 int nf_ct_l4proto_register_sysctl(struct net
*net
,
274 struct nf_proto_net
*pn
,
275 const struct nf_conntrack_l4proto
*l4proto
)
280 if (pn
->ctl_table
!= NULL
) {
281 err
= nf_ct_register_sysctl(net
,
282 &pn
->ctl_table_header
,
287 kfree(pn
->ctl_table
);
288 pn
->ctl_table
= NULL
;
292 #endif /* CONFIG_SYSCTL */
297 void nf_ct_l4proto_unregister_sysctl(struct net
*net
,
298 struct nf_proto_net
*pn
,
299 const struct nf_conntrack_l4proto
*l4proto
)
302 if (pn
->ctl_table_header
!= NULL
)
303 nf_ct_unregister_sysctl(&pn
->ctl_table_header
,
306 #endif /* CONFIG_SYSCTL */
309 /* FIXME: Allow NULL functions and sub in pointers to generic for
311 int nf_ct_l4proto_register_one(struct nf_conntrack_l4proto
*l4proto
)
315 if (l4proto
->l3proto
>= ARRAY_SIZE(nf_ct_protos
))
318 if ((l4proto
->to_nlattr
&& !l4proto
->nlattr_size
) ||
319 (l4proto
->tuple_to_nlattr
&& !l4proto
->nlattr_tuple_size
))
322 mutex_lock(&nf_ct_proto_mutex
);
323 if (!nf_ct_protos
[l4proto
->l3proto
]) {
324 /* l3proto may be loaded latter. */
325 struct nf_conntrack_l4proto __rcu
**proto_array
;
328 proto_array
= kmalloc(MAX_NF_CT_PROTO
*
329 sizeof(struct nf_conntrack_l4proto
*),
331 if (proto_array
== NULL
) {
336 for (i
= 0; i
< MAX_NF_CT_PROTO
; i
++)
337 RCU_INIT_POINTER(proto_array
[i
],
338 &nf_conntrack_l4proto_generic
);
340 /* Before making proto_array visible to lockless readers,
341 * we must make sure its content is committed to memory.
345 nf_ct_protos
[l4proto
->l3proto
] = proto_array
;
346 } else if (rcu_dereference_protected(
347 nf_ct_protos
[l4proto
->l3proto
][l4proto
->l4proto
],
348 lockdep_is_held(&nf_ct_proto_mutex
)
349 ) != &nf_conntrack_l4proto_generic
) {
354 l4proto
->nla_size
= 0;
355 if (l4proto
->nlattr_size
)
356 l4proto
->nla_size
+= l4proto
->nlattr_size();
357 if (l4proto
->nlattr_tuple_size
)
358 l4proto
->nla_size
+= 3 * l4proto
->nlattr_tuple_size();
360 rcu_assign_pointer(nf_ct_protos
[l4proto
->l3proto
][l4proto
->l4proto
],
363 mutex_unlock(&nf_ct_proto_mutex
);
366 EXPORT_SYMBOL_GPL(nf_ct_l4proto_register_one
);
368 int nf_ct_l4proto_pernet_register_one(struct net
*net
,
369 const struct nf_conntrack_l4proto
*l4proto
)
372 struct nf_proto_net
*pn
= NULL
;
374 if (l4proto
->init_net
) {
375 ret
= l4proto
->init_net(net
, l4proto
->l3proto
);
380 pn
= nf_ct_l4proto_net(net
, l4proto
);
384 ret
= nf_ct_l4proto_register_sysctl(net
, pn
, l4proto
);
392 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register_one
);
394 static void __nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto
*l4proto
)
397 BUG_ON(l4proto
->l3proto
>= ARRAY_SIZE(nf_ct_protos
));
399 BUG_ON(rcu_dereference_protected(
400 nf_ct_protos
[l4proto
->l3proto
][l4proto
->l4proto
],
401 lockdep_is_held(&nf_ct_proto_mutex
)
403 rcu_assign_pointer(nf_ct_protos
[l4proto
->l3proto
][l4proto
->l4proto
],
404 &nf_conntrack_l4proto_generic
);
407 void nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto
*l4proto
)
409 mutex_lock(&nf_ct_proto_mutex
);
410 __nf_ct_l4proto_unregister_one(l4proto
);
411 mutex_unlock(&nf_ct_proto_mutex
);
415 EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister_one
);
417 void nf_ct_l4proto_pernet_unregister_one(struct net
*net
,
418 const struct nf_conntrack_l4proto
*l4proto
)
420 struct nf_proto_net
*pn
= nf_ct_l4proto_net(net
, l4proto
);
426 nf_ct_l4proto_unregister_sysctl(net
, pn
, l4proto
);
428 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister_one
);
430 int nf_ct_l4proto_register(struct nf_conntrack_l4proto
*l4proto
[],
431 unsigned int num_proto
)
433 int ret
= -EINVAL
, ver
;
436 for (i
= 0; i
< num_proto
; i
++) {
437 ret
= nf_ct_l4proto_register_one(l4proto
[i
]);
441 if (i
!= num_proto
) {
442 ver
= l4proto
[i
]->l3proto
== PF_INET6
? 6 : 4;
443 pr_err("nf_conntrack_ipv%d: can't register %s%d proto.\n",
444 ver
, l4proto
[i
]->name
, ver
);
445 nf_ct_l4proto_unregister(l4proto
, i
);
449 EXPORT_SYMBOL_GPL(nf_ct_l4proto_register
);
451 int nf_ct_l4proto_pernet_register(struct net
*net
,
452 struct nf_conntrack_l4proto
*const l4proto
[],
453 unsigned int num_proto
)
458 for (i
= 0; i
< num_proto
; i
++) {
459 ret
= nf_ct_l4proto_pernet_register_one(net
, l4proto
[i
]);
463 if (i
!= num_proto
) {
464 pr_err("nf_conntrack_%s%d: pernet registration failed\n",
466 l4proto
[i
]->l3proto
== PF_INET6
? 6 : 4);
467 nf_ct_l4proto_pernet_unregister(net
, l4proto
, i
);
471 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register
);
473 void nf_ct_l4proto_unregister(struct nf_conntrack_l4proto
*l4proto
[],
474 unsigned int num_proto
)
476 mutex_lock(&nf_ct_proto_mutex
);
477 while (num_proto
-- != 0)
478 __nf_ct_l4proto_unregister_one(l4proto
[num_proto
]);
479 mutex_unlock(&nf_ct_proto_mutex
);
482 /* Remove all contrack entries for this protocol */
483 nf_ct_iterate_destroy(kill_l4proto
, l4proto
);
485 EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister
);
487 void nf_ct_l4proto_pernet_unregister(struct net
*net
,
488 struct nf_conntrack_l4proto
*const l4proto
[],
489 unsigned int num_proto
)
491 while (num_proto
-- != 0)
492 nf_ct_l4proto_pernet_unregister_one(net
, l4proto
[num_proto
]);
494 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister
);
496 int nf_conntrack_proto_pernet_init(struct net
*net
)
499 struct nf_proto_net
*pn
= nf_ct_l4proto_net(net
,
500 &nf_conntrack_l4proto_generic
);
502 err
= nf_conntrack_l4proto_generic
.init_net(net
,
503 nf_conntrack_l4proto_generic
.l3proto
);
506 err
= nf_ct_l4proto_register_sysctl(net
,
508 &nf_conntrack_l4proto_generic
);
516 void nf_conntrack_proto_pernet_fini(struct net
*net
)
518 struct nf_proto_net
*pn
= nf_ct_l4proto_net(net
,
519 &nf_conntrack_l4proto_generic
);
522 nf_ct_l4proto_unregister_sysctl(net
,
524 &nf_conntrack_l4proto_generic
);
527 int nf_conntrack_proto_init(void)
530 for (i
= 0; i
< NFPROTO_NUMPROTO
; i
++)
531 rcu_assign_pointer(nf_ct_l3protos
[i
],
532 &nf_conntrack_l3proto_generic
);
536 void nf_conntrack_proto_fini(void)
539 /* free l3proto protocol tables */
540 for (i
= 0; i
< ARRAY_SIZE(nf_ct_protos
); i
++)
541 kfree(nf_ct_protos
[i
]);