Merge tag 'ipmi-for-4.15' of git://github.com/cminyard/linux-ipmi
[sfrench/cifs-2.6.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 #include <net/netfilter/nf_log.h>
31
32 static struct nf_conntrack_l4proto __rcu **nf_ct_protos[NFPROTO_NUMPROTO] __read_mostly;
33 struct nf_conntrack_l3proto __rcu *nf_ct_l3protos[NFPROTO_NUMPROTO] __read_mostly;
34 EXPORT_SYMBOL_GPL(nf_ct_l3protos);
35
36 static DEFINE_MUTEX(nf_ct_proto_mutex);
37
38 #ifdef CONFIG_SYSCTL
39 static int
40 nf_ct_register_sysctl(struct net *net,
41                       struct ctl_table_header **header,
42                       const char *path,
43                       struct ctl_table *table)
44 {
45         if (*header == NULL) {
46                 *header = register_net_sysctl(net, path, table);
47                 if (*header == NULL)
48                         return -ENOMEM;
49         }
50
51         return 0;
52 }
53
54 static void
55 nf_ct_unregister_sysctl(struct ctl_table_header **header,
56                         struct ctl_table **table,
57                         unsigned int users)
58 {
59         if (users > 0)
60                 return;
61
62         unregister_net_sysctl_table(*header);
63         kfree(*table);
64         *header = NULL;
65         *table = NULL;
66 }
67
68 __printf(5, 6)
69 void nf_l4proto_log_invalid(const struct sk_buff *skb,
70                             struct net *net,
71                             u16 pf, u8 protonum,
72                             const char *fmt, ...)
73 {
74         struct va_format vaf;
75         va_list args;
76
77         if (net->ct.sysctl_log_invalid != protonum ||
78             net->ct.sysctl_log_invalid != IPPROTO_RAW)
79                 return;
80
81         va_start(args, fmt);
82         vaf.fmt = fmt;
83         vaf.va = &args;
84
85         nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
86                       "nf_ct_proto_%d: %pV ", protonum, &vaf);
87         va_end(args);
88 }
89 EXPORT_SYMBOL_GPL(nf_l4proto_log_invalid);
90
91 __printf(3, 4)
92 void nf_ct_l4proto_log_invalid(const struct sk_buff *skb,
93                                const struct nf_conn *ct,
94                                const char *fmt, ...)
95 {
96         struct va_format vaf;
97         struct net *net;
98         va_list args;
99
100         net = nf_ct_net(ct);
101         if (likely(net->ct.sysctl_log_invalid == 0))
102                 return;
103
104         va_start(args, fmt);
105         vaf.fmt = fmt;
106         vaf.va = &args;
107
108         nf_l4proto_log_invalid(skb, net, nf_ct_l3num(ct),
109                                nf_ct_protonum(ct), "%pV", &vaf);
110         va_end(args);
111 }
112 EXPORT_SYMBOL_GPL(nf_ct_l4proto_log_invalid);
113 #endif
114
115 const struct nf_conntrack_l4proto *
116 __nf_ct_l4proto_find(u_int16_t l3proto, u_int8_t l4proto)
117 {
118         if (unlikely(l3proto >= NFPROTO_NUMPROTO || nf_ct_protos[l3proto] == NULL))
119                 return &nf_conntrack_l4proto_generic;
120
121         return rcu_dereference(nf_ct_protos[l3proto][l4proto]);
122 }
123 EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find);
124
125 /* this is guaranteed to always return a valid protocol helper, since
126  * it falls back to generic_protocol */
127 const struct nf_conntrack_l3proto *
128 nf_ct_l3proto_find_get(u_int16_t l3proto)
129 {
130         struct nf_conntrack_l3proto *p;
131
132         rcu_read_lock();
133         p = __nf_ct_l3proto_find(l3proto);
134         if (!try_module_get(p->me))
135                 p = &nf_conntrack_l3proto_generic;
136         rcu_read_unlock();
137
138         return p;
139 }
140 EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get);
141
142 int
143 nf_ct_l3proto_try_module_get(unsigned short l3proto)
144 {
145         const struct nf_conntrack_l3proto *p;
146         int ret;
147
148 retry:  p = nf_ct_l3proto_find_get(l3proto);
149         if (p == &nf_conntrack_l3proto_generic) {
150                 ret = request_module("nf_conntrack-%d", l3proto);
151                 if (!ret)
152                         goto retry;
153
154                 return -EPROTOTYPE;
155         }
156
157         return 0;
158 }
159 EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get);
160
161 void nf_ct_l3proto_module_put(unsigned short l3proto)
162 {
163         struct nf_conntrack_l3proto *p;
164
165         /* rcu_read_lock not necessary since the caller holds a reference, but
166          * taken anyways to avoid lockdep warnings in __nf_ct_l3proto_find()
167          */
168         rcu_read_lock();
169         p = __nf_ct_l3proto_find(l3proto);
170         module_put(p->me);
171         rcu_read_unlock();
172 }
173 EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put);
174
175 static int nf_ct_netns_do_get(struct net *net, u8 nfproto)
176 {
177         const struct nf_conntrack_l3proto *l3proto;
178         int ret;
179
180         might_sleep();
181
182         ret = nf_ct_l3proto_try_module_get(nfproto);
183         if (ret < 0)
184                 return ret;
185
186         /* we already have a reference, can't fail */
187         rcu_read_lock();
188         l3proto = __nf_ct_l3proto_find(nfproto);
189         rcu_read_unlock();
190
191         if (!l3proto->net_ns_get)
192                 return 0;
193
194         ret = l3proto->net_ns_get(net);
195         if (ret < 0)
196                 nf_ct_l3proto_module_put(nfproto);
197
198         return ret;
199 }
200
201 int nf_ct_netns_get(struct net *net, u8 nfproto)
202 {
203         int err;
204
205         if (nfproto == NFPROTO_INET) {
206                 err = nf_ct_netns_do_get(net, NFPROTO_IPV4);
207                 if (err < 0)
208                         goto err1;
209                 err = nf_ct_netns_do_get(net, NFPROTO_IPV6);
210                 if (err < 0)
211                         goto err2;
212         } else {
213                 err = nf_ct_netns_do_get(net, nfproto);
214                 if (err < 0)
215                         goto err1;
216         }
217         return 0;
218
219 err2:
220         nf_ct_netns_put(net, NFPROTO_IPV4);
221 err1:
222         return err;
223 }
224 EXPORT_SYMBOL_GPL(nf_ct_netns_get);
225
226 static void nf_ct_netns_do_put(struct net *net, u8 nfproto)
227 {
228         const struct nf_conntrack_l3proto *l3proto;
229
230         might_sleep();
231
232         /* same as nf_conntrack_netns_get(), reference assumed */
233         rcu_read_lock();
234         l3proto = __nf_ct_l3proto_find(nfproto);
235         rcu_read_unlock();
236
237         if (WARN_ON(!l3proto))
238                 return;
239
240         if (l3proto->net_ns_put)
241                 l3proto->net_ns_put(net);
242
243         nf_ct_l3proto_module_put(nfproto);
244 }
245
246 void nf_ct_netns_put(struct net *net, uint8_t nfproto)
247 {
248         if (nfproto == NFPROTO_INET) {
249                 nf_ct_netns_do_put(net, NFPROTO_IPV4);
250                 nf_ct_netns_do_put(net, NFPROTO_IPV6);
251         } else
252                 nf_ct_netns_do_put(net, nfproto);
253 }
254 EXPORT_SYMBOL_GPL(nf_ct_netns_put);
255
256 const struct nf_conntrack_l4proto *
257 nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num)
258 {
259         const struct nf_conntrack_l4proto *p;
260
261         rcu_read_lock();
262         p = __nf_ct_l4proto_find(l3num, l4num);
263         if (!try_module_get(p->me))
264                 p = &nf_conntrack_l4proto_generic;
265         rcu_read_unlock();
266
267         return p;
268 }
269 EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get);
270
271 void nf_ct_l4proto_put(const struct nf_conntrack_l4proto *p)
272 {
273         module_put(p->me);
274 }
275 EXPORT_SYMBOL_GPL(nf_ct_l4proto_put);
276
277 static int kill_l3proto(struct nf_conn *i, void *data)
278 {
279         return nf_ct_l3num(i) == ((const struct nf_conntrack_l3proto *)data)->l3proto;
280 }
281
282 static int kill_l4proto(struct nf_conn *i, void *data)
283 {
284         const struct nf_conntrack_l4proto *l4proto;
285         l4proto = data;
286         return nf_ct_protonum(i) == l4proto->l4proto &&
287                nf_ct_l3num(i) == l4proto->l3proto;
288 }
289
290 int nf_ct_l3proto_register(const struct nf_conntrack_l3proto *proto)
291 {
292         int ret = 0;
293         struct nf_conntrack_l3proto *old;
294
295         if (proto->l3proto >= NFPROTO_NUMPROTO)
296                 return -EBUSY;
297 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
298         if (proto->tuple_to_nlattr && proto->nla_size == 0)
299                 return -EINVAL;
300 #endif
301         mutex_lock(&nf_ct_proto_mutex);
302         old = rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
303                                         lockdep_is_held(&nf_ct_proto_mutex));
304         if (old != &nf_conntrack_l3proto_generic) {
305                 ret = -EBUSY;
306                 goto out_unlock;
307         }
308
309         rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto);
310
311 out_unlock:
312         mutex_unlock(&nf_ct_proto_mutex);
313         return ret;
314
315 }
316 EXPORT_SYMBOL_GPL(nf_ct_l3proto_register);
317
318 void nf_ct_l3proto_unregister(const struct nf_conntrack_l3proto *proto)
319 {
320         BUG_ON(proto->l3proto >= NFPROTO_NUMPROTO);
321
322         mutex_lock(&nf_ct_proto_mutex);
323         BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
324                                          lockdep_is_held(&nf_ct_proto_mutex)
325                                          ) != proto);
326         rcu_assign_pointer(nf_ct_l3protos[proto->l3proto],
327                            &nf_conntrack_l3proto_generic);
328         mutex_unlock(&nf_ct_proto_mutex);
329
330         synchronize_rcu();
331         /* Remove all contrack entries for this protocol */
332         nf_ct_iterate_destroy(kill_l3proto, (void*)proto);
333 }
334 EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister);
335
336 static struct nf_proto_net *nf_ct_l4proto_net(struct net *net,
337                                 const struct nf_conntrack_l4proto *l4proto)
338 {
339         if (l4proto->get_net_proto) {
340                 /* statically built-in protocols use static per-net */
341                 return l4proto->get_net_proto(net);
342         } else if (l4proto->net_id) {
343                 /* ... and loadable protocols use dynamic per-net */
344                 return net_generic(net, *l4proto->net_id);
345         }
346         return NULL;
347 }
348
349 static
350 int nf_ct_l4proto_register_sysctl(struct net *net,
351                                   struct nf_proto_net *pn,
352                                   const struct nf_conntrack_l4proto *l4proto)
353 {
354         int err = 0;
355
356 #ifdef CONFIG_SYSCTL
357         if (pn->ctl_table != NULL) {
358                 err = nf_ct_register_sysctl(net,
359                                             &pn->ctl_table_header,
360                                             "net/netfilter",
361                                             pn->ctl_table);
362                 if (err < 0) {
363                         if (!pn->users) {
364                                 kfree(pn->ctl_table);
365                                 pn->ctl_table = NULL;
366                         }
367                 }
368         }
369 #endif /* CONFIG_SYSCTL */
370         return err;
371 }
372
373 static
374 void nf_ct_l4proto_unregister_sysctl(struct net *net,
375                                 struct nf_proto_net *pn,
376                                 const struct nf_conntrack_l4proto *l4proto)
377 {
378 #ifdef CONFIG_SYSCTL
379         if (pn->ctl_table_header != NULL)
380                 nf_ct_unregister_sysctl(&pn->ctl_table_header,
381                                         &pn->ctl_table,
382                                         pn->users);
383 #endif /* CONFIG_SYSCTL */
384 }
385
386 /* FIXME: Allow NULL functions and sub in pointers to generic for
387    them. --RR */
388 int nf_ct_l4proto_register_one(struct nf_conntrack_l4proto *l4proto)
389 {
390         int ret = 0;
391
392         if (l4proto->l3proto >= ARRAY_SIZE(nf_ct_protos))
393                 return -EBUSY;
394
395         if ((l4proto->to_nlattr && !l4proto->nlattr_size) ||
396             (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size))
397                 return -EINVAL;
398
399         mutex_lock(&nf_ct_proto_mutex);
400         if (!nf_ct_protos[l4proto->l3proto]) {
401                 /* l3proto may be loaded latter. */
402                 struct nf_conntrack_l4proto __rcu **proto_array;
403                 int i;
404
405                 proto_array = kmalloc(MAX_NF_CT_PROTO *
406                                       sizeof(struct nf_conntrack_l4proto *),
407                                       GFP_KERNEL);
408                 if (proto_array == NULL) {
409                         ret = -ENOMEM;
410                         goto out_unlock;
411                 }
412
413                 for (i = 0; i < MAX_NF_CT_PROTO; i++)
414                         RCU_INIT_POINTER(proto_array[i],
415                                          &nf_conntrack_l4proto_generic);
416
417                 /* Before making proto_array visible to lockless readers,
418                  * we must make sure its content is committed to memory.
419                  */
420                 smp_wmb();
421
422                 nf_ct_protos[l4proto->l3proto] = proto_array;
423         } else if (rcu_dereference_protected(
424                         nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
425                         lockdep_is_held(&nf_ct_proto_mutex)
426                         ) != &nf_conntrack_l4proto_generic) {
427                 ret = -EBUSY;
428                 goto out_unlock;
429         }
430
431         l4proto->nla_size = 0;
432         if (l4proto->nlattr_size)
433                 l4proto->nla_size += l4proto->nlattr_size();
434
435         rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
436                            l4proto);
437 out_unlock:
438         mutex_unlock(&nf_ct_proto_mutex);
439         return ret;
440 }
441 EXPORT_SYMBOL_GPL(nf_ct_l4proto_register_one);
442
443 int nf_ct_l4proto_pernet_register_one(struct net *net,
444                                 const struct nf_conntrack_l4proto *l4proto)
445 {
446         int ret = 0;
447         struct nf_proto_net *pn = NULL;
448
449         if (l4proto->init_net) {
450                 ret = l4proto->init_net(net, l4proto->l3proto);
451                 if (ret < 0)
452                         goto out;
453         }
454
455         pn = nf_ct_l4proto_net(net, l4proto);
456         if (pn == NULL)
457                 goto out;
458
459         ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto);
460         if (ret < 0)
461                 goto out;
462
463         pn->users++;
464 out:
465         return ret;
466 }
467 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register_one);
468
469 static void __nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *l4proto)
470
471 {
472         BUG_ON(l4proto->l3proto >= ARRAY_SIZE(nf_ct_protos));
473
474         BUG_ON(rcu_dereference_protected(
475                         nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
476                         lockdep_is_held(&nf_ct_proto_mutex)
477                         ) != l4proto);
478         rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
479                            &nf_conntrack_l4proto_generic);
480 }
481
482 void nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *l4proto)
483 {
484         mutex_lock(&nf_ct_proto_mutex);
485         __nf_ct_l4proto_unregister_one(l4proto);
486         mutex_unlock(&nf_ct_proto_mutex);
487
488         synchronize_rcu();
489 }
490 EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister_one);
491
492 void nf_ct_l4proto_pernet_unregister_one(struct net *net,
493                                 const struct nf_conntrack_l4proto *l4proto)
494 {
495         struct nf_proto_net *pn = nf_ct_l4proto_net(net, l4proto);
496
497         if (pn == NULL)
498                 return;
499
500         pn->users--;
501         nf_ct_l4proto_unregister_sysctl(net, pn, l4proto);
502 }
503 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister_one);
504
505 int nf_ct_l4proto_register(struct nf_conntrack_l4proto *l4proto[],
506                            unsigned int num_proto)
507 {
508         int ret = -EINVAL, ver;
509         unsigned int i;
510
511         for (i = 0; i < num_proto; i++) {
512                 ret = nf_ct_l4proto_register_one(l4proto[i]);
513                 if (ret < 0)
514                         break;
515         }
516         if (i != num_proto) {
517                 ver = l4proto[i]->l3proto == PF_INET6 ? 6 : 4;
518                 pr_err("nf_conntrack_ipv%d: can't register l4 %d proto.\n",
519                        ver, l4proto[i]->l4proto);
520                 nf_ct_l4proto_unregister(l4proto, i);
521         }
522         return ret;
523 }
524 EXPORT_SYMBOL_GPL(nf_ct_l4proto_register);
525
526 int nf_ct_l4proto_pernet_register(struct net *net,
527                                   struct nf_conntrack_l4proto *const l4proto[],
528                                   unsigned int num_proto)
529 {
530         int ret = -EINVAL;
531         unsigned int i;
532
533         for (i = 0; i < num_proto; i++) {
534                 ret = nf_ct_l4proto_pernet_register_one(net, l4proto[i]);
535                 if (ret < 0)
536                         break;
537         }
538         if (i != num_proto) {
539                 pr_err("nf_conntrack_proto_%d %d: pernet registration failed\n",
540                        l4proto[i]->l4proto,
541                        l4proto[i]->l3proto == PF_INET6 ? 6 : 4);
542                 nf_ct_l4proto_pernet_unregister(net, l4proto, i);
543         }
544         return ret;
545 }
546 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register);
547
548 void nf_ct_l4proto_unregister(struct nf_conntrack_l4proto *l4proto[],
549                               unsigned int num_proto)
550 {
551         mutex_lock(&nf_ct_proto_mutex);
552         while (num_proto-- != 0)
553                 __nf_ct_l4proto_unregister_one(l4proto[num_proto]);
554         mutex_unlock(&nf_ct_proto_mutex);
555
556         synchronize_net();
557         /* Remove all contrack entries for this protocol */
558         nf_ct_iterate_destroy(kill_l4proto, l4proto);
559 }
560 EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister);
561
562 void nf_ct_l4proto_pernet_unregister(struct net *net,
563                                 struct nf_conntrack_l4proto *const l4proto[],
564                                 unsigned int num_proto)
565 {
566         while (num_proto-- != 0)
567                 nf_ct_l4proto_pernet_unregister_one(net, l4proto[num_proto]);
568 }
569 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister);
570
571 int nf_conntrack_proto_pernet_init(struct net *net)
572 {
573         int err;
574         struct nf_proto_net *pn = nf_ct_l4proto_net(net,
575                                         &nf_conntrack_l4proto_generic);
576
577         err = nf_conntrack_l4proto_generic.init_net(net,
578                                         nf_conntrack_l4proto_generic.l3proto);
579         if (err < 0)
580                 return err;
581         err = nf_ct_l4proto_register_sysctl(net,
582                                             pn,
583                                             &nf_conntrack_l4proto_generic);
584         if (err < 0)
585                 return err;
586
587         pn->users++;
588         return 0;
589 }
590
591 void nf_conntrack_proto_pernet_fini(struct net *net)
592 {
593         struct nf_proto_net *pn = nf_ct_l4proto_net(net,
594                                         &nf_conntrack_l4proto_generic);
595
596         pn->users--;
597         nf_ct_l4proto_unregister_sysctl(net,
598                                         pn,
599                                         &nf_conntrack_l4proto_generic);
600 }
601
602 int nf_conntrack_proto_init(void)
603 {
604         unsigned int i;
605         for (i = 0; i < NFPROTO_NUMPROTO; i++)
606                 rcu_assign_pointer(nf_ct_l3protos[i],
607                                    &nf_conntrack_l3proto_generic);
608         return 0;
609 }
610
611 void nf_conntrack_proto_fini(void)
612 {
613         unsigned int i;
614         /* free l3proto protocol tables */
615         for (i = 0; i < ARRAY_SIZE(nf_ct_protos); i++)
616                 kfree(nf_ct_protos[i]);
617 }