]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - net/netfilter/nf_conntrack_proto.c
netfilter: conntrack: compute l3proto nla size at compile time
[thirdparty/kernel/stable.git] / net / netfilter / nf_conntrack_proto.c
1 /* L3/L4 protocol support for nf_conntrack. */
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 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
7 *
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.
11 */
12
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>
25
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>
30
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);
34
35 static DEFINE_MUTEX(nf_ct_proto_mutex);
36
37 #ifdef CONFIG_SYSCTL
38 static int
39 nf_ct_register_sysctl(struct net *net,
40 struct ctl_table_header **header,
41 const char *path,
42 struct ctl_table *table)
43 {
44 if (*header == NULL) {
45 *header = register_net_sysctl(net, path, table);
46 if (*header == NULL)
47 return -ENOMEM;
48 }
49
50 return 0;
51 }
52
53 static void
54 nf_ct_unregister_sysctl(struct ctl_table_header **header,
55 struct ctl_table **table,
56 unsigned int users)
57 {
58 if (users > 0)
59 return;
60
61 unregister_net_sysctl_table(*header);
62 kfree(*table);
63 *header = NULL;
64 *table = NULL;
65 }
66 #endif
67
68 struct nf_conntrack_l4proto *
69 __nf_ct_l4proto_find(u_int16_t l3proto, u_int8_t l4proto)
70 {
71 if (unlikely(l3proto >= NFPROTO_NUMPROTO || nf_ct_protos[l3proto] == NULL))
72 return &nf_conntrack_l4proto_generic;
73
74 return rcu_dereference(nf_ct_protos[l3proto][l4proto]);
75 }
76 EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find);
77
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)
82 {
83 struct nf_conntrack_l3proto *p;
84
85 rcu_read_lock();
86 p = __nf_ct_l3proto_find(l3proto);
87 if (!try_module_get(p->me))
88 p = &nf_conntrack_l3proto_generic;
89 rcu_read_unlock();
90
91 return p;
92 }
93 EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get);
94
95 int
96 nf_ct_l3proto_try_module_get(unsigned short l3proto)
97 {
98 int ret;
99 struct nf_conntrack_l3proto *p;
100
101 retry: p = nf_ct_l3proto_find_get(l3proto);
102 if (p == &nf_conntrack_l3proto_generic) {
103 ret = request_module("nf_conntrack-%d", l3proto);
104 if (!ret)
105 goto retry;
106
107 return -EPROTOTYPE;
108 }
109
110 return 0;
111 }
112 EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get);
113
114 void nf_ct_l3proto_module_put(unsigned short l3proto)
115 {
116 struct nf_conntrack_l3proto *p;
117
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()
120 */
121 rcu_read_lock();
122 p = __nf_ct_l3proto_find(l3proto);
123 module_put(p->me);
124 rcu_read_unlock();
125 }
126 EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put);
127
128 int nf_ct_netns_get(struct net *net, u8 nfproto)
129 {
130 const struct nf_conntrack_l3proto *l3proto;
131 int ret;
132
133 might_sleep();
134
135 ret = nf_ct_l3proto_try_module_get(nfproto);
136 if (ret < 0)
137 return ret;
138
139 /* we already have a reference, can't fail */
140 rcu_read_lock();
141 l3proto = __nf_ct_l3proto_find(nfproto);
142 rcu_read_unlock();
143
144 if (!l3proto->net_ns_get)
145 return 0;
146
147 ret = l3proto->net_ns_get(net);
148 if (ret < 0)
149 nf_ct_l3proto_module_put(nfproto);
150
151 return ret;
152 }
153 EXPORT_SYMBOL_GPL(nf_ct_netns_get);
154
155 void nf_ct_netns_put(struct net *net, u8 nfproto)
156 {
157 const struct nf_conntrack_l3proto *l3proto;
158
159 might_sleep();
160
161 /* same as nf_conntrack_netns_get(), reference assumed */
162 rcu_read_lock();
163 l3proto = __nf_ct_l3proto_find(nfproto);
164 rcu_read_unlock();
165
166 if (WARN_ON(!l3proto))
167 return;
168
169 if (l3proto->net_ns_put)
170 l3proto->net_ns_put(net);
171
172 nf_ct_l3proto_module_put(nfproto);
173 }
174 EXPORT_SYMBOL_GPL(nf_ct_netns_put);
175
176 struct nf_conntrack_l4proto *
177 nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num)
178 {
179 struct nf_conntrack_l4proto *p;
180
181 rcu_read_lock();
182 p = __nf_ct_l4proto_find(l3num, l4num);
183 if (!try_module_get(p->me))
184 p = &nf_conntrack_l4proto_generic;
185 rcu_read_unlock();
186
187 return p;
188 }
189 EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get);
190
191 void nf_ct_l4proto_put(const struct nf_conntrack_l4proto *p)
192 {
193 module_put(p->me);
194 }
195 EXPORT_SYMBOL_GPL(nf_ct_l4proto_put);
196
197 static int kill_l3proto(struct nf_conn *i, void *data)
198 {
199 return nf_ct_l3num(i) == ((struct nf_conntrack_l3proto *)data)->l3proto;
200 }
201
202 static int kill_l4proto(struct nf_conn *i, void *data)
203 {
204 struct nf_conntrack_l4proto *l4proto;
205 l4proto = data;
206 return nf_ct_protonum(i) == l4proto->l4proto &&
207 nf_ct_l3num(i) == l4proto->l3proto;
208 }
209
210 int nf_ct_l3proto_register(struct nf_conntrack_l3proto *proto)
211 {
212 int ret = 0;
213 struct nf_conntrack_l3proto *old;
214
215 if (proto->l3proto >= NFPROTO_NUMPROTO)
216 return -EBUSY;
217 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
218 if (proto->tuple_to_nlattr && proto->nla_size == 0)
219 return -EINVAL;
220 #endif
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) {
225 ret = -EBUSY;
226 goto out_unlock;
227 }
228
229 rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto);
230
231 out_unlock:
232 mutex_unlock(&nf_ct_proto_mutex);
233 return ret;
234
235 }
236 EXPORT_SYMBOL_GPL(nf_ct_l3proto_register);
237
238 void nf_ct_l3proto_unregister(struct nf_conntrack_l3proto *proto)
239 {
240 BUG_ON(proto->l3proto >= NFPROTO_NUMPROTO);
241
242 mutex_lock(&nf_ct_proto_mutex);
243 BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
244 lockdep_is_held(&nf_ct_proto_mutex)
245 ) != proto);
246 rcu_assign_pointer(nf_ct_l3protos[proto->l3proto],
247 &nf_conntrack_l3proto_generic);
248 mutex_unlock(&nf_ct_proto_mutex);
249
250 synchronize_rcu();
251 /* Remove all contrack entries for this protocol */
252 nf_ct_iterate_destroy(kill_l3proto, proto);
253 }
254 EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister);
255
256 static struct nf_proto_net *nf_ct_l4proto_net(struct net *net,
257 const struct nf_conntrack_l4proto *l4proto)
258 {
259 if (l4proto->get_net_proto) {
260 /* statically built-in protocols use static per-net */
261 return l4proto->get_net_proto(net);
262 } else if (l4proto->net_id) {
263 /* ... and loadable protocols use dynamic per-net */
264 return net_generic(net, *l4proto->net_id);
265 }
266 return NULL;
267 }
268
269 static
270 int nf_ct_l4proto_register_sysctl(struct net *net,
271 struct nf_proto_net *pn,
272 const struct nf_conntrack_l4proto *l4proto)
273 {
274 int err = 0;
275
276 #ifdef CONFIG_SYSCTL
277 if (pn->ctl_table != NULL) {
278 err = nf_ct_register_sysctl(net,
279 &pn->ctl_table_header,
280 "net/netfilter",
281 pn->ctl_table);
282 if (err < 0) {
283 if (!pn->users) {
284 kfree(pn->ctl_table);
285 pn->ctl_table = NULL;
286 }
287 }
288 }
289 #endif /* CONFIG_SYSCTL */
290 return err;
291 }
292
293 static
294 void nf_ct_l4proto_unregister_sysctl(struct net *net,
295 struct nf_proto_net *pn,
296 const struct nf_conntrack_l4proto *l4proto)
297 {
298 #ifdef CONFIG_SYSCTL
299 if (pn->ctl_table_header != NULL)
300 nf_ct_unregister_sysctl(&pn->ctl_table_header,
301 &pn->ctl_table,
302 pn->users);
303 #endif /* CONFIG_SYSCTL */
304 }
305
306 /* FIXME: Allow NULL functions and sub in pointers to generic for
307 them. --RR */
308 int nf_ct_l4proto_register_one(struct nf_conntrack_l4proto *l4proto)
309 {
310 int ret = 0;
311
312 if (l4proto->l3proto >= ARRAY_SIZE(nf_ct_protos))
313 return -EBUSY;
314
315 if ((l4proto->to_nlattr && !l4proto->nlattr_size) ||
316 (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size))
317 return -EINVAL;
318
319 mutex_lock(&nf_ct_proto_mutex);
320 if (!nf_ct_protos[l4proto->l3proto]) {
321 /* l3proto may be loaded latter. */
322 struct nf_conntrack_l4proto __rcu **proto_array;
323 int i;
324
325 proto_array = kmalloc(MAX_NF_CT_PROTO *
326 sizeof(struct nf_conntrack_l4proto *),
327 GFP_KERNEL);
328 if (proto_array == NULL) {
329 ret = -ENOMEM;
330 goto out_unlock;
331 }
332
333 for (i = 0; i < MAX_NF_CT_PROTO; i++)
334 RCU_INIT_POINTER(proto_array[i],
335 &nf_conntrack_l4proto_generic);
336
337 /* Before making proto_array visible to lockless readers,
338 * we must make sure its content is committed to memory.
339 */
340 smp_wmb();
341
342 nf_ct_protos[l4proto->l3proto] = proto_array;
343 } else if (rcu_dereference_protected(
344 nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
345 lockdep_is_held(&nf_ct_proto_mutex)
346 ) != &nf_conntrack_l4proto_generic) {
347 ret = -EBUSY;
348 goto out_unlock;
349 }
350
351 l4proto->nla_size = 0;
352 if (l4proto->nlattr_size)
353 l4proto->nla_size += l4proto->nlattr_size();
354 if (l4proto->nlattr_tuple_size)
355 l4proto->nla_size += 3 * l4proto->nlattr_tuple_size();
356
357 rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
358 l4proto);
359 out_unlock:
360 mutex_unlock(&nf_ct_proto_mutex);
361 return ret;
362 }
363 EXPORT_SYMBOL_GPL(nf_ct_l4proto_register_one);
364
365 int nf_ct_l4proto_pernet_register_one(struct net *net,
366 const struct nf_conntrack_l4proto *l4proto)
367 {
368 int ret = 0;
369 struct nf_proto_net *pn = NULL;
370
371 if (l4proto->init_net) {
372 ret = l4proto->init_net(net, l4proto->l3proto);
373 if (ret < 0)
374 goto out;
375 }
376
377 pn = nf_ct_l4proto_net(net, l4proto);
378 if (pn == NULL)
379 goto out;
380
381 ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto);
382 if (ret < 0)
383 goto out;
384
385 pn->users++;
386 out:
387 return ret;
388 }
389 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register_one);
390
391 static void __nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *l4proto)
392
393 {
394 BUG_ON(l4proto->l3proto >= ARRAY_SIZE(nf_ct_protos));
395
396 BUG_ON(rcu_dereference_protected(
397 nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
398 lockdep_is_held(&nf_ct_proto_mutex)
399 ) != l4proto);
400 rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
401 &nf_conntrack_l4proto_generic);
402 }
403
404 void nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *l4proto)
405 {
406 mutex_lock(&nf_ct_proto_mutex);
407 __nf_ct_l4proto_unregister_one(l4proto);
408 mutex_unlock(&nf_ct_proto_mutex);
409
410 synchronize_rcu();
411 }
412 EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister_one);
413
414 void nf_ct_l4proto_pernet_unregister_one(struct net *net,
415 const struct nf_conntrack_l4proto *l4proto)
416 {
417 struct nf_proto_net *pn = nf_ct_l4proto_net(net, l4proto);
418
419 if (pn == NULL)
420 return;
421
422 pn->users--;
423 nf_ct_l4proto_unregister_sysctl(net, pn, l4proto);
424 }
425 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister_one);
426
427 int nf_ct_l4proto_register(struct nf_conntrack_l4proto *l4proto[],
428 unsigned int num_proto)
429 {
430 int ret = -EINVAL, ver;
431 unsigned int i;
432
433 for (i = 0; i < num_proto; i++) {
434 ret = nf_ct_l4proto_register_one(l4proto[i]);
435 if (ret < 0)
436 break;
437 }
438 if (i != num_proto) {
439 ver = l4proto[i]->l3proto == PF_INET6 ? 6 : 4;
440 pr_err("nf_conntrack_ipv%d: can't register %s%d proto.\n",
441 ver, l4proto[i]->name, ver);
442 nf_ct_l4proto_unregister(l4proto, i);
443 }
444 return ret;
445 }
446 EXPORT_SYMBOL_GPL(nf_ct_l4proto_register);
447
448 int nf_ct_l4proto_pernet_register(struct net *net,
449 struct nf_conntrack_l4proto *const l4proto[],
450 unsigned int num_proto)
451 {
452 int ret = -EINVAL;
453 unsigned int i;
454
455 for (i = 0; i < num_proto; i++) {
456 ret = nf_ct_l4proto_pernet_register_one(net, l4proto[i]);
457 if (ret < 0)
458 break;
459 }
460 if (i != num_proto) {
461 pr_err("nf_conntrack_%s%d: pernet registration failed\n",
462 l4proto[i]->name,
463 l4proto[i]->l3proto == PF_INET6 ? 6 : 4);
464 nf_ct_l4proto_pernet_unregister(net, l4proto, i);
465 }
466 return ret;
467 }
468 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register);
469
470 void nf_ct_l4proto_unregister(struct nf_conntrack_l4proto *l4proto[],
471 unsigned int num_proto)
472 {
473 mutex_lock(&nf_ct_proto_mutex);
474 while (num_proto-- != 0)
475 __nf_ct_l4proto_unregister_one(l4proto[num_proto]);
476 mutex_unlock(&nf_ct_proto_mutex);
477
478 synchronize_net();
479 /* Remove all contrack entries for this protocol */
480 nf_ct_iterate_destroy(kill_l4proto, l4proto);
481 }
482 EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister);
483
484 void nf_ct_l4proto_pernet_unregister(struct net *net,
485 struct nf_conntrack_l4proto *const l4proto[],
486 unsigned int num_proto)
487 {
488 while (num_proto-- != 0)
489 nf_ct_l4proto_pernet_unregister_one(net, l4proto[num_proto]);
490 }
491 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister);
492
493 int nf_conntrack_proto_pernet_init(struct net *net)
494 {
495 int err;
496 struct nf_proto_net *pn = nf_ct_l4proto_net(net,
497 &nf_conntrack_l4proto_generic);
498
499 err = nf_conntrack_l4proto_generic.init_net(net,
500 nf_conntrack_l4proto_generic.l3proto);
501 if (err < 0)
502 return err;
503 err = nf_ct_l4proto_register_sysctl(net,
504 pn,
505 &nf_conntrack_l4proto_generic);
506 if (err < 0)
507 return err;
508
509 pn->users++;
510 return 0;
511 }
512
513 void nf_conntrack_proto_pernet_fini(struct net *net)
514 {
515 struct nf_proto_net *pn = nf_ct_l4proto_net(net,
516 &nf_conntrack_l4proto_generic);
517
518 pn->users--;
519 nf_ct_l4proto_unregister_sysctl(net,
520 pn,
521 &nf_conntrack_l4proto_generic);
522 }
523
524 int nf_conntrack_proto_init(void)
525 {
526 unsigned int i;
527 for (i = 0; i < NFPROTO_NUMPROTO; i++)
528 rcu_assign_pointer(nf_ct_l3protos[i],
529 &nf_conntrack_l3proto_generic);
530 return 0;
531 }
532
533 void nf_conntrack_proto_fini(void)
534 {
535 unsigned int i;
536 /* free l3proto protocol tables */
537 for (i = 0; i < ARRAY_SIZE(nf_ct_protos); i++)
538 kfree(nf_ct_protos[i]);
539 }