Merge tag 'pci-v4.18-fixes-2' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[linux-2.6-microblaze.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(const 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 == 0) ||
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 =
406                         kmalloc_array(MAX_NF_CT_PROTO,
407                                       sizeof(struct nf_conntrack_l4proto *),
408                                       GFP_KERNEL);
409                 if (proto_array == NULL) {
410                         ret = -ENOMEM;
411                         goto out_unlock;
412                 }
413
414                 for (i = 0; i < MAX_NF_CT_PROTO; i++)
415                         RCU_INIT_POINTER(proto_array[i],
416                                          &nf_conntrack_l4proto_generic);
417
418                 /* Before making proto_array visible to lockless readers,
419                  * we must make sure its content is committed to memory.
420                  */
421                 smp_wmb();
422
423                 nf_ct_protos[l4proto->l3proto] = proto_array;
424         } else if (rcu_dereference_protected(
425                         nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
426                         lockdep_is_held(&nf_ct_proto_mutex)
427                         ) != &nf_conntrack_l4proto_generic) {
428                 ret = -EBUSY;
429                 goto out_unlock;
430         }
431
432         rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
433                            l4proto);
434 out_unlock:
435         mutex_unlock(&nf_ct_proto_mutex);
436         return ret;
437 }
438 EXPORT_SYMBOL_GPL(nf_ct_l4proto_register_one);
439
440 int nf_ct_l4proto_pernet_register_one(struct net *net,
441                                 const struct nf_conntrack_l4proto *l4proto)
442 {
443         int ret = 0;
444         struct nf_proto_net *pn = NULL;
445
446         if (l4proto->init_net) {
447                 ret = l4proto->init_net(net, l4proto->l3proto);
448                 if (ret < 0)
449                         goto out;
450         }
451
452         pn = nf_ct_l4proto_net(net, l4proto);
453         if (pn == NULL)
454                 goto out;
455
456         ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto);
457         if (ret < 0)
458                 goto out;
459
460         pn->users++;
461 out:
462         return ret;
463 }
464 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register_one);
465
466 static void __nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *l4proto)
467
468 {
469         BUG_ON(l4proto->l3proto >= ARRAY_SIZE(nf_ct_protos));
470
471         BUG_ON(rcu_dereference_protected(
472                         nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
473                         lockdep_is_held(&nf_ct_proto_mutex)
474                         ) != l4proto);
475         rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
476                            &nf_conntrack_l4proto_generic);
477 }
478
479 void nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *l4proto)
480 {
481         mutex_lock(&nf_ct_proto_mutex);
482         __nf_ct_l4proto_unregister_one(l4proto);
483         mutex_unlock(&nf_ct_proto_mutex);
484
485         synchronize_rcu();
486 }
487 EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister_one);
488
489 void nf_ct_l4proto_pernet_unregister_one(struct net *net,
490                                 const struct nf_conntrack_l4proto *l4proto)
491 {
492         struct nf_proto_net *pn = nf_ct_l4proto_net(net, l4proto);
493
494         if (pn == NULL)
495                 return;
496
497         pn->users--;
498         nf_ct_l4proto_unregister_sysctl(net, pn, l4proto);
499 }
500 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister_one);
501
502 int nf_ct_l4proto_register(const struct nf_conntrack_l4proto * const l4proto[],
503                            unsigned int num_proto)
504 {
505         int ret = -EINVAL, ver;
506         unsigned int i;
507
508         for (i = 0; i < num_proto; i++) {
509                 ret = nf_ct_l4proto_register_one(l4proto[i]);
510                 if (ret < 0)
511                         break;
512         }
513         if (i != num_proto) {
514                 ver = l4proto[i]->l3proto == PF_INET6 ? 6 : 4;
515                 pr_err("nf_conntrack_ipv%d: can't register l4 %d proto.\n",
516                        ver, l4proto[i]->l4proto);
517                 nf_ct_l4proto_unregister(l4proto, i);
518         }
519         return ret;
520 }
521 EXPORT_SYMBOL_GPL(nf_ct_l4proto_register);
522
523 int nf_ct_l4proto_pernet_register(struct net *net,
524                                   const struct nf_conntrack_l4proto *const l4proto[],
525                                   unsigned int num_proto)
526 {
527         int ret = -EINVAL;
528         unsigned int i;
529
530         for (i = 0; i < num_proto; i++) {
531                 ret = nf_ct_l4proto_pernet_register_one(net, l4proto[i]);
532                 if (ret < 0)
533                         break;
534         }
535         if (i != num_proto) {
536                 pr_err("nf_conntrack_proto_%d %d: pernet registration failed\n",
537                        l4proto[i]->l4proto,
538                        l4proto[i]->l3proto == PF_INET6 ? 6 : 4);
539                 nf_ct_l4proto_pernet_unregister(net, l4proto, i);
540         }
541         return ret;
542 }
543 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register);
544
545 void nf_ct_l4proto_unregister(const struct nf_conntrack_l4proto * const l4proto[],
546                               unsigned int num_proto)
547 {
548         mutex_lock(&nf_ct_proto_mutex);
549         while (num_proto-- != 0)
550                 __nf_ct_l4proto_unregister_one(l4proto[num_proto]);
551         mutex_unlock(&nf_ct_proto_mutex);
552
553         synchronize_net();
554         /* Remove all contrack entries for this protocol */
555         nf_ct_iterate_destroy(kill_l4proto, (void *)l4proto);
556 }
557 EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister);
558
559 void nf_ct_l4proto_pernet_unregister(struct net *net,
560                                 const struct nf_conntrack_l4proto *const l4proto[],
561                                 unsigned int num_proto)
562 {
563         while (num_proto-- != 0)
564                 nf_ct_l4proto_pernet_unregister_one(net, l4proto[num_proto]);
565 }
566 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister);
567
568 int nf_conntrack_proto_pernet_init(struct net *net)
569 {
570         int err;
571         struct nf_proto_net *pn = nf_ct_l4proto_net(net,
572                                         &nf_conntrack_l4proto_generic);
573
574         err = nf_conntrack_l4proto_generic.init_net(net,
575                                         nf_conntrack_l4proto_generic.l3proto);
576         if (err < 0)
577                 return err;
578         err = nf_ct_l4proto_register_sysctl(net,
579                                             pn,
580                                             &nf_conntrack_l4proto_generic);
581         if (err < 0)
582                 return err;
583
584         pn->users++;
585         return 0;
586 }
587
588 void nf_conntrack_proto_pernet_fini(struct net *net)
589 {
590         struct nf_proto_net *pn = nf_ct_l4proto_net(net,
591                                         &nf_conntrack_l4proto_generic);
592
593         pn->users--;
594         nf_ct_l4proto_unregister_sysctl(net,
595                                         pn,
596                                         &nf_conntrack_l4proto_generic);
597 }
598
599 int nf_conntrack_proto_init(void)
600 {
601         unsigned int i;
602         for (i = 0; i < NFPROTO_NUMPROTO; i++)
603                 rcu_assign_pointer(nf_ct_l3protos[i],
604                                    &nf_conntrack_l3proto_generic);
605         return 0;
606 }
607
608 void nf_conntrack_proto_fini(void)
609 {
610         unsigned int i;
611         /* free l3proto protocol tables */
612         for (i = 0; i < ARRAY_SIZE(nf_ct_protos); i++)
613                 kfree(nf_ct_protos[i]);
614 }