1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * x_tables core - Backend for {ip,ip6,arp}_tables
4 *
5 * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
6 * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net>
7 *
8 * Based on existing ip_tables code which is
9 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
10 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
11 */
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/socket.h>
16#include <linux/net.h>
17#include <linux/proc_fs.h>
18#include <linux/seq_file.h>
19#include <linux/string.h>
20#include <linux/vmalloc.h>
21#include <linux/mutex.h>
22#include <linux/mm.h>
23#include <linux/slab.h>
24#include <linux/audit.h>
25#include <linux/user_namespace.h>
26#include <net/net_namespace.h>
27#include <net/netns/generic.h>
28
29#include <linux/netfilter/x_tables.h>
30#include <linux/netfilter_arp.h>
31#include <linux/netfilter_ipv4/ip_tables.h>
32#include <linux/netfilter_ipv6/ip6_tables.h>
33#include <linux/netfilter_arp/arp_tables.h>
34
35MODULE_LICENSE("GPL");
36MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
37MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
38
39#define XT_PCPU_BLOCK_SIZE 4096
40#define XT_MAX_TABLE_SIZE (512 * 1024 * 1024)
41
42struct xt_template {
43 struct list_head list;
44
45 /* called when table is needed in the given netns */
46 int (*table_init)(struct net *net);
47
48 struct module *me;
49
50 /* A unique name... */
51 char name[XT_TABLE_MAXNAMELEN];
52};
53
54static struct list_head xt_templates[NFPROTO_NUMPROTO];
55
56struct xt_pernet {
57 struct list_head tables[NFPROTO_NUMPROTO];
58};
59
60struct compat_delta {
61 unsigned int offset; /* offset in kernel */
62 int delta; /* delta in 32bit user land */
63};
64
65struct xt_af {
66 struct mutex mutex;
67 struct list_head match;
68 struct list_head target;
69#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
70 struct mutex compat_mutex;
71 struct compat_delta *compat_tab;
72 unsigned int number; /* number of slots in compat_tab[] */
73 unsigned int cur; /* number of used slots in compat_tab[] */
74#endif
75};
76
77static unsigned int xt_pernet_id __read_mostly;
78static struct xt_af *xt __read_mostly;
79
80static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
81 [NFPROTO_UNSPEC] = "x",
82 [NFPROTO_IPV4] = "ip",
83 [NFPROTO_ARP] = "arp",
84 [NFPROTO_BRIDGE] = "eb",
85 [NFPROTO_IPV6] = "ip6",
86};
87
88/* Registration hooks for targets. */
89int xt_register_target(struct xt_target *target)
90{
91 u_int8_t af = target->family;
92
93 mutex_lock(lock: &xt[af].mutex);
94 list_add(new: &target->list, head: &xt[af].target);
95 mutex_unlock(lock: &xt[af].mutex);
96 return 0;
97}
98EXPORT_SYMBOL(xt_register_target);
99
100void
101xt_unregister_target(struct xt_target *target)
102{
103 u_int8_t af = target->family;
104
105 mutex_lock(lock: &xt[af].mutex);
106 list_del(entry: &target->list);
107 mutex_unlock(lock: &xt[af].mutex);
108}
109EXPORT_SYMBOL(xt_unregister_target);
110
111int
112xt_register_targets(struct xt_target *target, unsigned int n)
113{
114 unsigned int i;
115 int err = 0;
116
117 for (i = 0; i < n; i++) {
118 err = xt_register_target(&target[i]);
119 if (err)
120 goto err;
121 }
122 return err;
123
124err:
125 if (i > 0)
126 xt_unregister_targets(target, n: i);
127 return err;
128}
129EXPORT_SYMBOL(xt_register_targets);
130
131void
132xt_unregister_targets(struct xt_target *target, unsigned int n)
133{
134 while (n-- > 0)
135 xt_unregister_target(&target[n]);
136}
137EXPORT_SYMBOL(xt_unregister_targets);
138
139int xt_register_match(struct xt_match *match)
140{
141 u_int8_t af = match->family;
142
143 mutex_lock(lock: &xt[af].mutex);
144 list_add(new: &match->list, head: &xt[af].match);
145 mutex_unlock(lock: &xt[af].mutex);
146 return 0;
147}
148EXPORT_SYMBOL(xt_register_match);
149
150void
151xt_unregister_match(struct xt_match *match)
152{
153 u_int8_t af = match->family;
154
155 mutex_lock(lock: &xt[af].mutex);
156 list_del(entry: &match->list);
157 mutex_unlock(lock: &xt[af].mutex);
158}
159EXPORT_SYMBOL(xt_unregister_match);
160
161int
162xt_register_matches(struct xt_match *match, unsigned int n)
163{
164 unsigned int i;
165 int err = 0;
166
167 for (i = 0; i < n; i++) {
168 err = xt_register_match(&match[i]);
169 if (err)
170 goto err;
171 }
172 return err;
173
174err:
175 if (i > 0)
176 xt_unregister_matches(match, n: i);
177 return err;
178}
179EXPORT_SYMBOL(xt_register_matches);
180
181void
182xt_unregister_matches(struct xt_match *match, unsigned int n)
183{
184 while (n-- > 0)
185 xt_unregister_match(&match[n]);
186}
187EXPORT_SYMBOL(xt_unregister_matches);
188
189
190/*
191 * These are weird, but module loading must not be done with mutex
192 * held (since they will register), and we have to have a single
193 * function to use.
194 */
195
196/* Find match, grabs ref. Returns ERR_PTR() on error. */
197struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
198{
199 struct xt_match *m;
200 int err = -ENOENT;
201
202 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
203 return ERR_PTR(error: -EINVAL);
204
205 mutex_lock(lock: &xt[af].mutex);
206 list_for_each_entry(m, &xt[af].match, list) {
207 if (strcmp(m->name, name) == 0) {
208 if (m->revision == revision) {
209 if (try_module_get(module: m->me)) {
210 mutex_unlock(lock: &xt[af].mutex);
211 return m;
212 }
213 } else
214 err = -EPROTOTYPE; /* Found something. */
215 }
216 }
217 mutex_unlock(lock: &xt[af].mutex);
218
219 if (af != NFPROTO_UNSPEC)
220 /* Try searching again in the family-independent list */
221 return xt_find_match(af: NFPROTO_UNSPEC, name, revision);
222
223 return ERR_PTR(error: err);
224}
225EXPORT_SYMBOL(xt_find_match);
226
227struct xt_match *
228xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
229{
230 struct xt_match *match;
231
232 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
233 return ERR_PTR(error: -EINVAL);
234
235 match = xt_find_match(nfproto, name, revision);
236 if (IS_ERR(ptr: match)) {
237 request_module("%st_%s", xt_prefix[nfproto], name);
238 match = xt_find_match(nfproto, name, revision);
239 }
240
241 return match;
242}
243EXPORT_SYMBOL_GPL(xt_request_find_match);
244
245/* Find target, grabs ref. Returns ERR_PTR() on error. */
246static struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
247{
248 struct xt_target *t;
249 int err = -ENOENT;
250
251 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
252 return ERR_PTR(error: -EINVAL);
253
254 mutex_lock(lock: &xt[af].mutex);
255 list_for_each_entry(t, &xt[af].target, list) {
256 if (strcmp(t->name, name) == 0) {
257 if (t->revision == revision) {
258 if (try_module_get(module: t->me)) {
259 mutex_unlock(lock: &xt[af].mutex);
260 return t;
261 }
262 } else
263 err = -EPROTOTYPE; /* Found something. */
264 }
265 }
266 mutex_unlock(lock: &xt[af].mutex);
267
268 if (af != NFPROTO_UNSPEC)
269 /* Try searching again in the family-independent list */
270 return xt_find_target(af: NFPROTO_UNSPEC, name, revision);
271
272 return ERR_PTR(error: err);
273}
274
275struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
276{
277 struct xt_target *target;
278
279 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
280 return ERR_PTR(error: -EINVAL);
281
282 target = xt_find_target(af, name, revision);
283 if (IS_ERR(ptr: target)) {
284 request_module("%st_%s", xt_prefix[af], name);
285 target = xt_find_target(af, name, revision);
286 }
287
288 return target;
289}
290EXPORT_SYMBOL_GPL(xt_request_find_target);
291
292
293static int xt_obj_to_user(u16 __user *psize, u16 size,
294 void __user *pname, const char *name,
295 u8 __user *prev, u8 rev)
296{
297 if (put_user(size, psize))
298 return -EFAULT;
299 if (copy_to_user(to: pname, from: name, n: strlen(name) + 1))
300 return -EFAULT;
301 if (put_user(rev, prev))
302 return -EFAULT;
303
304 return 0;
305}
306
307#define XT_OBJ_TO_USER(U, K, TYPE, C_SIZE) \
308 xt_obj_to_user(&U->u.TYPE##_size, C_SIZE ? : K->u.TYPE##_size, \
309 U->u.user.name, K->u.kernel.TYPE->name, \
310 &U->u.user.revision, K->u.kernel.TYPE->revision)
311
312int xt_data_to_user(void __user *dst, const void *src,
313 int usersize, int size, int aligned_size)
314{
315 usersize = usersize ? : size;
316 if (copy_to_user(to: dst, from: src, n: usersize))
317 return -EFAULT;
318 if (usersize != aligned_size &&
319 clear_user(to: dst + usersize, n: aligned_size - usersize))
320 return -EFAULT;
321
322 return 0;
323}
324EXPORT_SYMBOL_GPL(xt_data_to_user);
325
326#define XT_DATA_TO_USER(U, K, TYPE) \
327 xt_data_to_user(U->data, K->data, \
328 K->u.kernel.TYPE->usersize, \
329 K->u.kernel.TYPE->TYPE##size, \
330 XT_ALIGN(K->u.kernel.TYPE->TYPE##size))
331
332int xt_match_to_user(const struct xt_entry_match *m,
333 struct xt_entry_match __user *u)
334{
335 return XT_OBJ_TO_USER(u, m, match, 0) ||
336 XT_DATA_TO_USER(u, m, match);
337}
338EXPORT_SYMBOL_GPL(xt_match_to_user);
339
340int xt_target_to_user(const struct xt_entry_target *t,
341 struct xt_entry_target __user *u)
342{
343 return XT_OBJ_TO_USER(u, t, target, 0) ||
344 XT_DATA_TO_USER(u, t, target);
345}
346EXPORT_SYMBOL_GPL(xt_target_to_user);
347
348static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
349{
350 const struct xt_match *m;
351 int have_rev = 0;
352
353 mutex_lock(lock: &xt[af].mutex);
354 list_for_each_entry(m, &xt[af].match, list) {
355 if (strcmp(m->name, name) == 0) {
356 if (m->revision > *bestp)
357 *bestp = m->revision;
358 if (m->revision == revision)
359 have_rev = 1;
360 }
361 }
362 mutex_unlock(lock: &xt[af].mutex);
363
364 if (af != NFPROTO_UNSPEC && !have_rev)
365 return match_revfn(af: NFPROTO_UNSPEC, name, revision, bestp);
366
367 return have_rev;
368}
369
370static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
371{
372 const struct xt_target *t;
373 int have_rev = 0;
374
375 mutex_lock(lock: &xt[af].mutex);
376 list_for_each_entry(t, &xt[af].target, list) {
377 if (strcmp(t->name, name) == 0) {
378 if (t->revision > *bestp)
379 *bestp = t->revision;
380 if (t->revision == revision)
381 have_rev = 1;
382 }
383 }
384 mutex_unlock(lock: &xt[af].mutex);
385
386 if (af != NFPROTO_UNSPEC && !have_rev)
387 return target_revfn(af: NFPROTO_UNSPEC, name, revision, bestp);
388
389 return have_rev;
390}
391
392/* Returns true or false (if no such extension at all) */
393int xt_find_revision(u8 af, const char *name, u8 revision, int target,
394 int *err)
395{
396 int have_rev, best = -1;
397
398 if (target == 1)
399 have_rev = target_revfn(af, name, revision, bestp: &best);
400 else
401 have_rev = match_revfn(af, name, revision, bestp: &best);
402
403 /* Nothing at all? Return 0 to try loading module. */
404 if (best == -1) {
405 *err = -ENOENT;
406 return 0;
407 }
408
409 *err = best;
410 if (!have_rev)
411 *err = -EPROTONOSUPPORT;
412 return 1;
413}
414EXPORT_SYMBOL_GPL(xt_find_revision);
415
416static char *
417textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
418{
419 static const char *const inetbr_names[] = {
420 "PREROUTING", "INPUT", "FORWARD",
421 "OUTPUT", "POSTROUTING", "BROUTING",
422 };
423 static const char *const arp_names[] = {
424 "INPUT", "FORWARD", "OUTPUT",
425 };
426 const char *const *names;
427 unsigned int i, max;
428 char *p = buf;
429 bool np = false;
430 int res;
431
432 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
433 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
434 ARRAY_SIZE(inetbr_names);
435 *p = '\0';
436 for (i = 0; i < max; ++i) {
437 if (!(mask & (1 << i)))
438 continue;
439 res = snprintf(buf: p, size, fmt: "%s%s", np ? "/" : "", names[i]);
440 if (res > 0) {
441 size -= res;
442 p += res;
443 }
444 np = true;
445 }
446
447 return buf;
448}
449
450/**
451 * xt_check_proc_name - check that name is suitable for /proc file creation
452 *
453 * @name: file name candidate
454 * @size: length of buffer
455 *
456 * some x_tables modules wish to create a file in /proc.
457 * This function makes sure that the name is suitable for this
458 * purpose, it checks that name is NUL terminated and isn't a 'special'
459 * name, like "..".
460 *
461 * returns negative number on error or 0 if name is useable.
462 */
463int xt_check_proc_name(const char *name, unsigned int size)
464{
465 if (name[0] == '\0')
466 return -EINVAL;
467
468 if (strnlen(name, size) == size)
469 return -ENAMETOOLONG;
470
471 if (strcmp(name, ".") == 0 ||
472 strcmp(name, "..") == 0 ||
473 strchr(name, '/'))
474 return -EINVAL;
475
476 return 0;
477}
478EXPORT_SYMBOL(xt_check_proc_name);
479
480int xt_check_match(struct xt_mtchk_param *par,
481 unsigned int size, u16 proto, bool inv_proto)
482{
483 int ret;
484
485 if (XT_ALIGN(par->match->matchsize) != size &&
486 par->match->matchsize != -1) {
487 /*
488 * ebt_among is exempt from centralized matchsize checking
489 * because it uses a dynamic-size data set.
490 */
491 pr_err_ratelimited("%s_tables: %s.%u match: invalid size %u (kernel) != (user) %u\n",
492 xt_prefix[par->family], par->match->name,
493 par->match->revision,
494 XT_ALIGN(par->match->matchsize), size);
495 return -EINVAL;
496 }
497 if (par->match->table != NULL &&
498 strcmp(par->match->table, par->table) != 0) {
499 pr_info_ratelimited("%s_tables: %s match: only valid in %s table, not %s\n",
500 xt_prefix[par->family], par->match->name,
501 par->match->table, par->table);
502 return -EINVAL;
503 }
504 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
505 char used[64], allow[64];
506
507 pr_info_ratelimited("%s_tables: %s match: used from hooks %s, but only valid from %s\n",
508 xt_prefix[par->family], par->match->name,
509 textify_hooks(used, sizeof(used),
510 par->hook_mask, par->family),
511 textify_hooks(allow, sizeof(allow),
512 par->match->hooks,
513 par->family));
514 return -EINVAL;
515 }
516 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
517 pr_info_ratelimited("%s_tables: %s match: only valid for protocol %u\n",
518 xt_prefix[par->family], par->match->name,
519 par->match->proto);
520 return -EINVAL;
521 }
522 if (par->match->checkentry != NULL) {
523 ret = par->match->checkentry(par);
524 if (ret < 0)
525 return ret;
526 else if (ret > 0)
527 /* Flag up potential errors. */
528 return -EIO;
529 }
530 return 0;
531}
532EXPORT_SYMBOL_GPL(xt_check_match);
533
534/** xt_check_entry_match - check that matches end before start of target
535 *
536 * @match: beginning of xt_entry_match
537 * @target: beginning of this rules target (alleged end of matches)
538 * @alignment: alignment requirement of match structures
539 *
540 * Validates that all matches add up to the beginning of the target,
541 * and that each match covers at least the base structure size.
542 *
543 * Return: 0 on success, negative errno on failure.
544 */
545static int xt_check_entry_match(const char *match, const char *target,
546 const size_t alignment)
547{
548 const struct xt_entry_match *pos;
549 int length = target - match;
550
551 if (length == 0) /* no matches */
552 return 0;
553
554 pos = (struct xt_entry_match *)match;
555 do {
556 if ((unsigned long)pos % alignment)
557 return -EINVAL;
558
559 if (length < (int)sizeof(struct xt_entry_match))
560 return -EINVAL;
561
562 if (pos->u.match_size < sizeof(struct xt_entry_match))
563 return -EINVAL;
564
565 if (pos->u.match_size > length)
566 return -EINVAL;
567
568 length -= pos->u.match_size;
569 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
570 } while (length > 0);
571
572 return 0;
573}
574
575/** xt_check_table_hooks - check hook entry points are sane
576 *
577 * @info xt_table_info to check
578 * @valid_hooks - hook entry points that we can enter from
579 *
580 * Validates that the hook entry and underflows points are set up.
581 *
582 * Return: 0 on success, negative errno on failure.
583 */
584int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks)
585{
586 const char *err = "unsorted underflow";
587 unsigned int i, max_uflow, max_entry;
588 bool check_hooks = false;
589
590 BUILD_BUG_ON(ARRAY_SIZE(info->hook_entry) != ARRAY_SIZE(info->underflow));
591
592 max_entry = 0;
593 max_uflow = 0;
594
595 for (i = 0; i < ARRAY_SIZE(info->hook_entry); i++) {
596 if (!(valid_hooks & (1 << i)))
597 continue;
598
599 if (info->hook_entry[i] == 0xFFFFFFFF)
600 return -EINVAL;
601 if (info->underflow[i] == 0xFFFFFFFF)
602 return -EINVAL;
603
604 if (check_hooks) {
605 if (max_uflow > info->underflow[i])
606 goto error;
607
608 if (max_uflow == info->underflow[i]) {
609 err = "duplicate underflow";
610 goto error;
611 }
612 if (max_entry > info->hook_entry[i]) {
613 err = "unsorted entry";
614 goto error;
615 }
616 if (max_entry == info->hook_entry[i]) {
617 err = "duplicate entry";
618 goto error;
619 }
620 }
621 max_entry = info->hook_entry[i];
622 max_uflow = info->underflow[i];
623 check_hooks = true;
624 }
625
626 return 0;
627error:
628 pr_err_ratelimited("%s at hook %d\n", err, i);
629 return -EINVAL;
630}
631EXPORT_SYMBOL(xt_check_table_hooks);
632
633static bool verdict_ok(int verdict)
634{
635 if (verdict > 0)
636 return true;
637
638 if (verdict < 0) {
639 int v = -verdict - 1;
640
641 if (verdict == XT_RETURN)
642 return true;
643
644 switch (v) {
645 case NF_ACCEPT: return true;
646 case NF_DROP: return true;
647 case NF_QUEUE: return true;
648 default:
649 break;
650 }
651
652 return false;
653 }
654
655 return false;
656}
657
658static bool error_tg_ok(unsigned int usersize, unsigned int kernsize,
659 const char *msg, unsigned int msglen)
660{
661 return usersize == kernsize && strnlen(msg, msglen) < msglen;
662}
663
664#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
665int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
666{
667 struct xt_af *xp = &xt[af];
668
669 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
670
671 if (WARN_ON(!xp->compat_tab))
672 return -ENOMEM;
673
674 if (xp->cur >= xp->number)
675 return -EINVAL;
676
677 if (xp->cur)
678 delta += xp->compat_tab[xp->cur - 1].delta;
679 xp->compat_tab[xp->cur].offset = offset;
680 xp->compat_tab[xp->cur].delta = delta;
681 xp->cur++;
682 return 0;
683}
684EXPORT_SYMBOL_GPL(xt_compat_add_offset);
685
686void xt_compat_flush_offsets(u_int8_t af)
687{
688 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
689
690 if (xt[af].compat_tab) {
691 vfree(xt[af].compat_tab);
692 xt[af].compat_tab = NULL;
693 xt[af].number = 0;
694 xt[af].cur = 0;
695 }
696}
697EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
698
699int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
700{
701 struct compat_delta *tmp = xt[af].compat_tab;
702 int mid, left = 0, right = xt[af].cur - 1;
703
704 while (left <= right) {
705 mid = (left + right) >> 1;
706 if (offset > tmp[mid].offset)
707 left = mid + 1;
708 else if (offset < tmp[mid].offset)
709 right = mid - 1;
710 else
711 return mid ? tmp[mid - 1].delta : 0;
712 }
713 return left ? tmp[left - 1].delta : 0;
714}
715EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
716
717int xt_compat_init_offsets(u8 af, unsigned int number)
718{
719 size_t mem;
720
721 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
722
723 if (!number || number > (INT_MAX / sizeof(struct compat_delta)))
724 return -EINVAL;
725
726 if (WARN_ON(xt[af].compat_tab))
727 return -EINVAL;
728
729 mem = sizeof(struct compat_delta) * number;
730 if (mem > XT_MAX_TABLE_SIZE)
731 return -ENOMEM;
732
733 xt[af].compat_tab = vmalloc(mem);
734 if (!xt[af].compat_tab)
735 return -ENOMEM;
736
737 xt[af].number = number;
738 xt[af].cur = 0;
739
740 return 0;
741}
742EXPORT_SYMBOL(xt_compat_init_offsets);
743
744int xt_compat_match_offset(const struct xt_match *match)
745{
746 u_int16_t csize = match->compatsize ? : match->matchsize;
747 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
748}
749EXPORT_SYMBOL_GPL(xt_compat_match_offset);
750
751void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
752 unsigned int *size)
753{
754 const struct xt_match *match = m->u.kernel.match;
755 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
756 int off = xt_compat_match_offset(match);
757 u_int16_t msize = cm->u.user.match_size;
758 char name[sizeof(m->u.user.name)];
759
760 m = *dstptr;
761 memcpy(m, cm, sizeof(*cm));
762 if (match->compat_from_user)
763 match->compat_from_user(m->data, cm->data);
764 else
765 memcpy(m->data, cm->data, msize - sizeof(*cm));
766
767 msize += off;
768 m->u.user.match_size = msize;
769 strscpy(name, match->name, sizeof(name));
770 module_put(match->me);
771 strscpy_pad(m->u.user.name, name, sizeof(m->u.user.name));
772
773 *size += off;
774 *dstptr += msize;
775}
776EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
777
778#define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE) \
779 xt_data_to_user(U->data, K->data, \
780 K->u.kernel.TYPE->usersize, \
781 C_SIZE, \
782 COMPAT_XT_ALIGN(C_SIZE))
783
784int xt_compat_match_to_user(const struct xt_entry_match *m,
785 void __user **dstptr, unsigned int *size)
786{
787 const struct xt_match *match = m->u.kernel.match;
788 struct compat_xt_entry_match __user *cm = *dstptr;
789 int off = xt_compat_match_offset(match);
790 u_int16_t msize = m->u.user.match_size - off;
791
792 if (XT_OBJ_TO_USER(cm, m, match, msize))
793 return -EFAULT;
794
795 if (match->compat_to_user) {
796 if (match->compat_to_user((void __user *)cm->data, m->data))
797 return -EFAULT;
798 } else {
799 if (COMPAT_XT_DATA_TO_USER(cm, m, match, msize - sizeof(*cm)))
800 return -EFAULT;
801 }
802
803 *size -= off;
804 *dstptr += msize;
805 return 0;
806}
807EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
808
809/* non-compat version may have padding after verdict */
810struct compat_xt_standard_target {
811 struct compat_xt_entry_target t;
812 compat_uint_t verdict;
813};
814
815struct compat_xt_error_target {
816 struct compat_xt_entry_target t;
817 char errorname[XT_FUNCTION_MAXNAMELEN];
818};
819
820int xt_compat_check_entry_offsets(const void *base, const char *elems,
821 unsigned int target_offset,
822 unsigned int next_offset)
823{
824 long size_of_base_struct = elems - (const char *)base;
825 const struct compat_xt_entry_target *t;
826 const char *e = base;
827
828 if (target_offset < size_of_base_struct)
829 return -EINVAL;
830
831 if (target_offset + sizeof(*t) > next_offset)
832 return -EINVAL;
833
834 t = (void *)(e + target_offset);
835 if (t->u.target_size < sizeof(*t))
836 return -EINVAL;
837
838 if (target_offset + t->u.target_size > next_offset)
839 return -EINVAL;
840
841 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
842 const struct compat_xt_standard_target *st = (const void *)t;
843
844 if (COMPAT_XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
845 return -EINVAL;
846
847 if (!verdict_ok(st->verdict))
848 return -EINVAL;
849 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
850 const struct compat_xt_error_target *et = (const void *)t;
851
852 if (!error_tg_ok(t->u.target_size, sizeof(*et),
853 et->errorname, sizeof(et->errorname)))
854 return -EINVAL;
855 }
856
857 /* compat_xt_entry match has less strict alignment requirements,
858 * otherwise they are identical. In case of padding differences
859 * we need to add compat version of xt_check_entry_match.
860 */
861 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
862
863 return xt_check_entry_match(elems, base + target_offset,
864 __alignof__(struct compat_xt_entry_match));
865}
866EXPORT_SYMBOL(xt_compat_check_entry_offsets);
867#endif /* CONFIG_NETFILTER_XTABLES_COMPAT */
868
869/**
870 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
871 *
872 * @base: pointer to arp/ip/ip6t_entry
873 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
874 * @target_offset: the arp/ip/ip6_t->target_offset
875 * @next_offset: the arp/ip/ip6_t->next_offset
876 *
877 * validates that target_offset and next_offset are sane and that all
878 * match sizes (if any) align with the target offset.
879 *
880 * This function does not validate the targets or matches themselves, it
881 * only tests that all the offsets and sizes are correct, that all
882 * match structures are aligned, and that the last structure ends where
883 * the target structure begins.
884 *
885 * Also see xt_compat_check_entry_offsets for CONFIG_NETFILTER_XTABLES_COMPAT version.
886 *
887 * The arp/ip/ip6t_entry structure @base must have passed following tests:
888 * - it must point to a valid memory location
889 * - base to base + next_offset must be accessible, i.e. not exceed allocated
890 * length.
891 *
892 * A well-formed entry looks like this:
893 *
894 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
895 * e->elems[]-----' | |
896 * matchsize | |
897 * matchsize | |
898 * | |
899 * target_offset---------------------------------' |
900 * next_offset---------------------------------------------------'
901 *
902 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
903 * This is where matches (if any) and the target reside.
904 * target_offset: beginning of target.
905 * next_offset: start of the next rule; also: size of this rule.
906 * Since targets have a minimum size, target_offset + minlen <= next_offset.
907 *
908 * Every match stores its size, sum of sizes must not exceed target_offset.
909 *
910 * Return: 0 on success, negative errno on failure.
911 */
912int xt_check_entry_offsets(const void *base,
913 const char *elems,
914 unsigned int target_offset,
915 unsigned int next_offset)
916{
917 long size_of_base_struct = elems - (const char *)base;
918 const struct xt_entry_target *t;
919 const char *e = base;
920
921 /* target start is within the ip/ip6/arpt_entry struct */
922 if (target_offset < size_of_base_struct)
923 return -EINVAL;
924
925 if (target_offset + sizeof(*t) > next_offset)
926 return -EINVAL;
927
928 t = (void *)(e + target_offset);
929 if (t->u.target_size < sizeof(*t))
930 return -EINVAL;
931
932 if (target_offset + t->u.target_size > next_offset)
933 return -EINVAL;
934
935 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
936 const struct xt_standard_target *st = (const void *)t;
937
938 if (XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
939 return -EINVAL;
940
941 if (!verdict_ok(verdict: st->verdict))
942 return -EINVAL;
943 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
944 const struct xt_error_target *et = (const void *)t;
945
946 if (!error_tg_ok(usersize: t->u.target_size, kernsize: sizeof(*et),
947 msg: et->errorname, msglen: sizeof(et->errorname)))
948 return -EINVAL;
949 }
950
951 return xt_check_entry_match(match: elems, target: base + target_offset,
952 alignment: __alignof__(struct xt_entry_match));
953}
954EXPORT_SYMBOL(xt_check_entry_offsets);
955
956/**
957 * xt_alloc_entry_offsets - allocate array to store rule head offsets
958 *
959 * @size: number of entries
960 *
961 * Return: NULL or zeroed kmalloc'd or vmalloc'd array
962 */
963unsigned int *xt_alloc_entry_offsets(unsigned int size)
964{
965 if (size > XT_MAX_TABLE_SIZE / sizeof(unsigned int))
966 return NULL;
967
968 return kvcalloc(size, sizeof(unsigned int), GFP_KERNEL);
969
970}
971EXPORT_SYMBOL(xt_alloc_entry_offsets);
972
973/**
974 * xt_find_jump_offset - check if target is a valid jump offset
975 *
976 * @offsets: array containing all valid rule start offsets of a rule blob
977 * @target: the jump target to search for
978 * @size: entries in @offset
979 */
980bool xt_find_jump_offset(const unsigned int *offsets,
981 unsigned int target, unsigned int size)
982{
983 int m, low = 0, hi = size;
984
985 while (hi > low) {
986 m = (low + hi) / 2u;
987
988 if (offsets[m] > target)
989 hi = m;
990 else if (offsets[m] < target)
991 low = m + 1;
992 else
993 return true;
994 }
995
996 return false;
997}
998EXPORT_SYMBOL(xt_find_jump_offset);
999
1000int xt_check_target(struct xt_tgchk_param *par,
1001 unsigned int size, u16 proto, bool inv_proto)
1002{
1003 int ret;
1004
1005 if (XT_ALIGN(par->target->targetsize) != size) {
1006 pr_err_ratelimited("%s_tables: %s.%u target: invalid size %u (kernel) != (user) %u\n",
1007 xt_prefix[par->family], par->target->name,
1008 par->target->revision,
1009 XT_ALIGN(par->target->targetsize), size);
1010 return -EINVAL;
1011 }
1012 if (par->target->table != NULL &&
1013 strcmp(par->target->table, par->table) != 0) {
1014 pr_info_ratelimited("%s_tables: %s target: only valid in %s table, not %s\n",
1015 xt_prefix[par->family], par->target->name,
1016 par->target->table, par->table);
1017 return -EINVAL;
1018 }
1019 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
1020 char used[64], allow[64];
1021
1022 pr_info_ratelimited("%s_tables: %s target: used from hooks %s, but only usable from %s\n",
1023 xt_prefix[par->family], par->target->name,
1024 textify_hooks(used, sizeof(used),
1025 par->hook_mask, par->family),
1026 textify_hooks(allow, sizeof(allow),
1027 par->target->hooks,
1028 par->family));
1029 return -EINVAL;
1030 }
1031 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
1032 pr_info_ratelimited("%s_tables: %s target: only valid for protocol %u\n",
1033 xt_prefix[par->family], par->target->name,
1034 par->target->proto);
1035 return -EINVAL;
1036 }
1037 if (par->target->checkentry != NULL) {
1038 ret = par->target->checkentry(par);
1039 if (ret < 0)
1040 return ret;
1041 else if (ret > 0)
1042 /* Flag up potential errors. */
1043 return -EIO;
1044 }
1045 return 0;
1046}
1047EXPORT_SYMBOL_GPL(xt_check_target);
1048
1049/**
1050 * xt_copy_counters - copy counters and metadata from a sockptr_t
1051 *
1052 * @arg: src sockptr
1053 * @len: alleged size of userspace memory
1054 * @info: where to store the xt_counters_info metadata
1055 *
1056 * Copies counter meta data from @user and stores it in @info.
1057 *
1058 * vmallocs memory to hold the counters, then copies the counter data
1059 * from @user to the new memory and returns a pointer to it.
1060 *
1061 * If called from a compat syscall, @info gets converted automatically to the
1062 * 64bit representation.
1063 *
1064 * The metadata associated with the counters is stored in @info.
1065 *
1066 * Return: returns pointer that caller has to test via IS_ERR().
1067 * If IS_ERR is false, caller has to vfree the pointer.
1068 */
1069void *xt_copy_counters(sockptr_t arg, unsigned int len,
1070 struct xt_counters_info *info)
1071{
1072 size_t offset;
1073 void *mem;
1074 u64 size;
1075
1076#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1077 if (in_compat_syscall()) {
1078 /* structures only differ in size due to alignment */
1079 struct compat_xt_counters_info compat_tmp;
1080
1081 if (len <= sizeof(compat_tmp))
1082 return ERR_PTR(-EINVAL);
1083
1084 len -= sizeof(compat_tmp);
1085 if (copy_from_sockptr(&compat_tmp, arg, sizeof(compat_tmp)) != 0)
1086 return ERR_PTR(-EFAULT);
1087
1088 memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
1089 info->num_counters = compat_tmp.num_counters;
1090 offset = sizeof(compat_tmp);
1091 } else
1092#endif
1093 {
1094 if (len <= sizeof(*info))
1095 return ERR_PTR(error: -EINVAL);
1096
1097 len -= sizeof(*info);
1098 if (copy_from_sockptr(dst: info, src: arg, size: sizeof(*info)) != 0)
1099 return ERR_PTR(error: -EFAULT);
1100
1101 offset = sizeof(*info);
1102 }
1103 info->name[sizeof(info->name) - 1] = '\0';
1104
1105 size = sizeof(struct xt_counters);
1106 size *= info->num_counters;
1107
1108 if (size != (u64)len)
1109 return ERR_PTR(error: -EINVAL);
1110
1111 mem = vmalloc(len);
1112 if (!mem)
1113 return ERR_PTR(error: -ENOMEM);
1114
1115 if (copy_from_sockptr_offset(dst: mem, src: arg, offset, size: len) == 0)
1116 return mem;
1117
1118 vfree(addr: mem);
1119 return ERR_PTR(error: -EFAULT);
1120}
1121EXPORT_SYMBOL_GPL(xt_copy_counters);
1122
1123#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1124int xt_compat_target_offset(const struct xt_target *target)
1125{
1126 u_int16_t csize = target->compatsize ? : target->targetsize;
1127 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
1128}
1129EXPORT_SYMBOL_GPL(xt_compat_target_offset);
1130
1131void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
1132 unsigned int *size)
1133{
1134 const struct xt_target *target = t->u.kernel.target;
1135 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
1136 int off = xt_compat_target_offset(target);
1137 u_int16_t tsize = ct->u.user.target_size;
1138 char name[sizeof(t->u.user.name)];
1139
1140 t = *dstptr;
1141 memcpy(t, ct, sizeof(*ct));
1142 if (target->compat_from_user)
1143 target->compat_from_user(t->data, ct->data);
1144 else
1145 unsafe_memcpy(t->data, ct->data, tsize - sizeof(*ct),
1146 /* UAPI 0-sized destination */);
1147
1148 tsize += off;
1149 t->u.user.target_size = tsize;
1150 strscpy(name, target->name, sizeof(name));
1151 module_put(target->me);
1152 strscpy_pad(t->u.user.name, name, sizeof(t->u.user.name));
1153
1154 *size += off;
1155 *dstptr += tsize;
1156}
1157EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
1158
1159int xt_compat_target_to_user(const struct xt_entry_target *t,
1160 void __user **dstptr, unsigned int *size)
1161{
1162 const struct xt_target *target = t->u.kernel.target;
1163 struct compat_xt_entry_target __user *ct = *dstptr;
1164 int off = xt_compat_target_offset(target);
1165 u_int16_t tsize = t->u.user.target_size - off;
1166
1167 if (XT_OBJ_TO_USER(ct, t, target, tsize))
1168 return -EFAULT;
1169
1170 if (target->compat_to_user) {
1171 if (target->compat_to_user((void __user *)ct->data, t->data))
1172 return -EFAULT;
1173 } else {
1174 if (COMPAT_XT_DATA_TO_USER(ct, t, target, tsize - sizeof(*ct)))
1175 return -EFAULT;
1176 }
1177
1178 *size -= off;
1179 *dstptr += tsize;
1180 return 0;
1181}
1182EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
1183#endif
1184
1185struct xt_table_info *xt_alloc_table_info(unsigned int size)
1186{
1187 struct xt_table_info *info = NULL;
1188 size_t sz = sizeof(*info) + size;
1189
1190 if (sz < sizeof(*info) || sz >= XT_MAX_TABLE_SIZE)
1191 return NULL;
1192
1193 info = kvmalloc(sz, GFP_KERNEL_ACCOUNT);
1194 if (!info)
1195 return NULL;
1196
1197 memset(s: info, c: 0, n: sizeof(*info));
1198 info->size = size;
1199 return info;
1200}
1201EXPORT_SYMBOL(xt_alloc_table_info);
1202
1203void xt_free_table_info(struct xt_table_info *info)
1204{
1205 int cpu;
1206
1207 if (info->jumpstack != NULL) {
1208 for_each_possible_cpu(cpu)
1209 kvfree(addr: info->jumpstack[cpu]);
1210 kvfree(addr: info->jumpstack);
1211 }
1212
1213 kvfree(addr: info);
1214}
1215EXPORT_SYMBOL(xt_free_table_info);
1216
1217struct xt_table *xt_find_table(struct net *net, u8 af, const char *name)
1218{
1219 struct xt_pernet *xt_net = net_generic(net, id: xt_pernet_id);
1220 struct xt_table *t;
1221
1222 mutex_lock(lock: &xt[af].mutex);
1223 list_for_each_entry(t, &xt_net->tables[af], list) {
1224 if (strcmp(t->name, name) == 0) {
1225 mutex_unlock(lock: &xt[af].mutex);
1226 return t;
1227 }
1228 }
1229 mutex_unlock(lock: &xt[af].mutex);
1230 return NULL;
1231}
1232EXPORT_SYMBOL(xt_find_table);
1233
1234/* Find table by name, grabs mutex & ref. Returns ERR_PTR on error. */
1235struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1236 const char *name)
1237{
1238 struct xt_pernet *xt_net = net_generic(net, id: xt_pernet_id);
1239 struct module *owner = NULL;
1240 struct xt_template *tmpl;
1241 struct xt_table *t;
1242
1243 mutex_lock(lock: &xt[af].mutex);
1244 list_for_each_entry(t, &xt_net->tables[af], list)
1245 if (strcmp(t->name, name) == 0 && try_module_get(module: t->me))
1246 return t;
1247
1248 /* Table doesn't exist in this netns, check larval list */
1249 list_for_each_entry(tmpl, &xt_templates[af], list) {
1250 int err;
1251
1252 if (strcmp(tmpl->name, name))
1253 continue;
1254 if (!try_module_get(module: tmpl->me))
1255 goto out;
1256
1257 owner = tmpl->me;
1258
1259 mutex_unlock(lock: &xt[af].mutex);
1260 err = tmpl->table_init(net);
1261 if (err < 0) {
1262 module_put(module: owner);
1263 return ERR_PTR(error: err);
1264 }
1265
1266 mutex_lock(lock: &xt[af].mutex);
1267 break;
1268 }
1269
1270 /* and once again: */
1271 list_for_each_entry(t, &xt_net->tables[af], list)
1272 if (strcmp(t->name, name) == 0 && owner == t->me)
1273 return t;
1274
1275 module_put(module: owner);
1276 out:
1277 mutex_unlock(lock: &xt[af].mutex);
1278 return ERR_PTR(error: -ENOENT);
1279}
1280EXPORT_SYMBOL_GPL(xt_find_table_lock);
1281
1282struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
1283 const char *name)
1284{
1285 struct xt_table *t = xt_find_table_lock(net, af, name);
1286
1287#ifdef CONFIG_MODULES
1288 if (IS_ERR(ptr: t)) {
1289 int err = request_module("%stable_%s", xt_prefix[af], name);
1290 if (err < 0)
1291 return ERR_PTR(error: err);
1292 t = xt_find_table_lock(net, af, name);
1293 }
1294#endif
1295
1296 return t;
1297}
1298EXPORT_SYMBOL_GPL(xt_request_find_table_lock);
1299
1300void xt_table_unlock(struct xt_table *table)
1301{
1302 mutex_unlock(lock: &xt[table->af].mutex);
1303}
1304EXPORT_SYMBOL_GPL(xt_table_unlock);
1305
1306#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1307void xt_compat_lock(u_int8_t af)
1308{
1309 mutex_lock(&xt[af].compat_mutex);
1310}
1311EXPORT_SYMBOL_GPL(xt_compat_lock);
1312
1313void xt_compat_unlock(u_int8_t af)
1314{
1315 mutex_unlock(&xt[af].compat_mutex);
1316}
1317EXPORT_SYMBOL_GPL(xt_compat_unlock);
1318#endif
1319
1320struct static_key xt_tee_enabled __read_mostly;
1321EXPORT_SYMBOL_GPL(xt_tee_enabled);
1322
1323#ifdef CONFIG_NETFILTER_XTABLES_LEGACY
1324DEFINE_PER_CPU(seqcount_t, xt_recseq);
1325EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
1326
1327static int xt_jumpstack_alloc(struct xt_table_info *i)
1328{
1329 unsigned int size;
1330 int cpu;
1331
1332 size = sizeof(void **) * nr_cpu_ids;
1333 if (size > PAGE_SIZE)
1334 i->jumpstack = kvzalloc(size, GFP_KERNEL);
1335 else
1336 i->jumpstack = kzalloc(size, GFP_KERNEL);
1337 if (i->jumpstack == NULL)
1338 return -ENOMEM;
1339
1340 /* ruleset without jumps -- no stack needed */
1341 if (i->stacksize == 0)
1342 return 0;
1343
1344 /* Jumpstack needs to be able to record two full callchains, one
1345 * from the first rule set traversal, plus one table reentrancy
1346 * via -j TEE without clobbering the callchain that brought us to
1347 * TEE target.
1348 *
1349 * This is done by allocating two jumpstacks per cpu, on reentry
1350 * the upper half of the stack is used.
1351 *
1352 * see the jumpstack setup in ipt_do_table() for more details.
1353 */
1354 size = sizeof(void *) * i->stacksize * 2u;
1355 for_each_possible_cpu(cpu) {
1356 i->jumpstack[cpu] = kvmalloc_node(size, GFP_KERNEL,
1357 cpu_to_node(cpu));
1358 if (i->jumpstack[cpu] == NULL)
1359 /*
1360 * Freeing will be done later on by the callers. The
1361 * chain is: xt_replace_table -> __do_replace ->
1362 * do_replace -> xt_free_table_info.
1363 */
1364 return -ENOMEM;
1365 }
1366
1367 return 0;
1368}
1369
1370struct xt_counters *xt_counters_alloc(unsigned int counters)
1371{
1372 struct xt_counters *mem;
1373
1374 if (counters == 0 || counters > INT_MAX / sizeof(*mem))
1375 return NULL;
1376
1377 counters *= sizeof(*mem);
1378 if (counters > XT_MAX_TABLE_SIZE)
1379 return NULL;
1380
1381 return vzalloc(counters);
1382}
1383EXPORT_SYMBOL(xt_counters_alloc);
1384
1385struct xt_table_info *
1386xt_replace_table(struct xt_table *table,
1387 unsigned int num_counters,
1388 struct xt_table_info *newinfo,
1389 int *error)
1390{
1391 struct xt_table_info *private;
1392 unsigned int cpu;
1393 int ret;
1394
1395 ret = xt_jumpstack_alloc(newinfo);
1396 if (ret < 0) {
1397 *error = ret;
1398 return NULL;
1399 }
1400
1401 /* Do the substitution. */
1402 local_bh_disable();
1403 private = table->private;
1404
1405 /* Check inside lock: is the old number correct? */
1406 if (num_counters != private->number) {
1407 pr_debug("num_counters != table->private->number (%u/%u)\n",
1408 num_counters, private->number);
1409 local_bh_enable();
1410 *error = -EAGAIN;
1411 return NULL;
1412 }
1413
1414 newinfo->initial_entries = private->initial_entries;
1415 /*
1416 * Ensure contents of newinfo are visible before assigning to
1417 * private.
1418 */
1419 smp_wmb();
1420 table->private = newinfo;
1421
1422 /* make sure all cpus see new ->private value */
1423 smp_mb();
1424
1425 /*
1426 * Even though table entries have now been swapped, other CPU's
1427 * may still be using the old entries...
1428 */
1429 local_bh_enable();
1430
1431 /* ... so wait for even xt_recseq on all cpus */
1432 for_each_possible_cpu(cpu) {
1433 seqcount_t *s = &per_cpu(xt_recseq, cpu);
1434 u32 seq = raw_read_seqcount(s);
1435
1436 if (seq & 1) {
1437 do {
1438 cond_resched();
1439 cpu_relax();
1440 } while (seq == raw_read_seqcount(s));
1441 }
1442 }
1443
1444 audit_log_nfcfg(table->name, table->af, private->number,
1445 !private->number ? AUDIT_XT_OP_REGISTER :
1446 AUDIT_XT_OP_REPLACE,
1447 GFP_KERNEL);
1448 return private;
1449}
1450EXPORT_SYMBOL_GPL(xt_replace_table);
1451
1452struct xt_table *xt_register_table(struct net *net,
1453 const struct xt_table *input_table,
1454 struct xt_table_info *bootstrap,
1455 struct xt_table_info *newinfo)
1456{
1457 struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
1458 struct xt_table_info *private;
1459 struct xt_table *t, *table;
1460 int ret;
1461
1462 /* Don't add one object to multiple lists. */
1463 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1464 if (!table) {
1465 ret = -ENOMEM;
1466 goto out;
1467 }
1468
1469 mutex_lock(&xt[table->af].mutex);
1470 /* Don't autoload: we'd eat our tail... */
1471 list_for_each_entry(t, &xt_net->tables[table->af], list) {
1472 if (strcmp(t->name, table->name) == 0) {
1473 ret = -EEXIST;
1474 goto unlock;
1475 }
1476 }
1477
1478 /* Simplifies replace_table code. */
1479 table->private = bootstrap;
1480
1481 if (!xt_replace_table(table, 0, newinfo, &ret))
1482 goto unlock;
1483
1484 private = table->private;
1485 pr_debug("table->private->number = %u\n", private->number);
1486
1487 /* save number of initial entries */
1488 private->initial_entries = private->number;
1489
1490 list_add(&table->list, &xt_net->tables[table->af]);
1491 mutex_unlock(&xt[table->af].mutex);
1492 return table;
1493
1494unlock:
1495 mutex_unlock(&xt[table->af].mutex);
1496 kfree(table);
1497out:
1498 return ERR_PTR(ret);
1499}
1500EXPORT_SYMBOL_GPL(xt_register_table);
1501
1502void *xt_unregister_table(struct xt_table *table)
1503{
1504 struct xt_table_info *private;
1505
1506 mutex_lock(&xt[table->af].mutex);
1507 private = table->private;
1508 list_del(&table->list);
1509 mutex_unlock(&xt[table->af].mutex);
1510 audit_log_nfcfg(table->name, table->af, private->number,
1511 AUDIT_XT_OP_UNREGISTER, GFP_KERNEL);
1512 kfree(table->ops);
1513 kfree(table);
1514
1515 return private;
1516}
1517EXPORT_SYMBOL_GPL(xt_unregister_table);
1518#endif
1519
1520#ifdef CONFIG_PROC_FS
1521static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
1522{
1523 u8 af = (unsigned long)pde_data(inode: file_inode(f: seq->file));
1524 struct net *net = seq_file_net(seq);
1525 struct xt_pernet *xt_net;
1526
1527 xt_net = net_generic(net, id: xt_pernet_id);
1528
1529 mutex_lock(lock: &xt[af].mutex);
1530 return seq_list_start(head: &xt_net->tables[af], pos: *pos);
1531}
1532
1533static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1534{
1535 u8 af = (unsigned long)pde_data(inode: file_inode(f: seq->file));
1536 struct net *net = seq_file_net(seq);
1537 struct xt_pernet *xt_net;
1538
1539 xt_net = net_generic(net, id: xt_pernet_id);
1540
1541 return seq_list_next(v, head: &xt_net->tables[af], ppos: pos);
1542}
1543
1544static void xt_table_seq_stop(struct seq_file *seq, void *v)
1545{
1546 u_int8_t af = (unsigned long)pde_data(inode: file_inode(f: seq->file));
1547
1548 mutex_unlock(lock: &xt[af].mutex);
1549}
1550
1551static int xt_table_seq_show(struct seq_file *seq, void *v)
1552{
1553 struct xt_table *table = list_entry(v, struct xt_table, list);
1554
1555 if (*table->name)
1556 seq_printf(m: seq, fmt: "%s\n", table->name);
1557 return 0;
1558}
1559
1560static const struct seq_operations xt_table_seq_ops = {
1561 .start = xt_table_seq_start,
1562 .next = xt_table_seq_next,
1563 .stop = xt_table_seq_stop,
1564 .show = xt_table_seq_show,
1565};
1566
1567/*
1568 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1569 * the multi-AF mutexes.
1570 */
1571struct nf_mttg_trav {
1572 struct list_head *head, *curr;
1573 uint8_t class;
1574};
1575
1576enum {
1577 MTTG_TRAV_INIT,
1578 MTTG_TRAV_NFP_UNSPEC,
1579 MTTG_TRAV_NFP_SPEC,
1580 MTTG_TRAV_DONE,
1581};
1582
1583static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1584 bool is_target)
1585{
1586 static const uint8_t next_class[] = {
1587 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1588 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1589 };
1590 uint8_t nfproto = (unsigned long)pde_data(inode: file_inode(f: seq->file));
1591 struct nf_mttg_trav *trav = seq->private;
1592
1593 if (ppos != NULL)
1594 ++(*ppos);
1595
1596 switch (trav->class) {
1597 case MTTG_TRAV_INIT:
1598 trav->class = MTTG_TRAV_NFP_UNSPEC;
1599 mutex_lock(lock: &xt[NFPROTO_UNSPEC].mutex);
1600 trav->head = trav->curr = is_target ?
1601 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1602 break;
1603 case MTTG_TRAV_NFP_UNSPEC:
1604 trav->curr = trav->curr->next;
1605 if (trav->curr != trav->head)
1606 break;
1607 mutex_unlock(lock: &xt[NFPROTO_UNSPEC].mutex);
1608 mutex_lock(lock: &xt[nfproto].mutex);
1609 trav->head = trav->curr = is_target ?
1610 &xt[nfproto].target : &xt[nfproto].match;
1611 trav->class = next_class[trav->class];
1612 break;
1613 case MTTG_TRAV_NFP_SPEC:
1614 trav->curr = trav->curr->next;
1615 if (trav->curr != trav->head)
1616 break;
1617 fallthrough;
1618 default:
1619 return NULL;
1620 }
1621 return trav;
1622}
1623
1624static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1625 bool is_target)
1626{
1627 struct nf_mttg_trav *trav = seq->private;
1628 unsigned int j;
1629
1630 trav->class = MTTG_TRAV_INIT;
1631 for (j = 0; j < *pos; ++j)
1632 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1633 return NULL;
1634 return trav;
1635}
1636
1637static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1638{
1639 uint8_t nfproto = (unsigned long)pde_data(inode: file_inode(f: seq->file));
1640 struct nf_mttg_trav *trav = seq->private;
1641
1642 switch (trav->class) {
1643 case MTTG_TRAV_NFP_UNSPEC:
1644 mutex_unlock(lock: &xt[NFPROTO_UNSPEC].mutex);
1645 break;
1646 case MTTG_TRAV_NFP_SPEC:
1647 mutex_unlock(lock: &xt[nfproto].mutex);
1648 break;
1649 }
1650}
1651
1652static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1653{
1654 return xt_mttg_seq_start(seq, pos, is_target: false);
1655}
1656
1657static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1658{
1659 return xt_mttg_seq_next(seq, v, ppos, is_target: false);
1660}
1661
1662static int xt_match_seq_show(struct seq_file *seq, void *v)
1663{
1664 const struct nf_mttg_trav *trav = seq->private;
1665 const struct xt_match *match;
1666
1667 switch (trav->class) {
1668 case MTTG_TRAV_NFP_UNSPEC:
1669 case MTTG_TRAV_NFP_SPEC:
1670 if (trav->curr == trav->head)
1671 return 0;
1672 match = list_entry(trav->curr, struct xt_match, list);
1673 if (*match->name)
1674 seq_printf(m: seq, fmt: "%s\n", match->name);
1675 }
1676 return 0;
1677}
1678
1679static const struct seq_operations xt_match_seq_ops = {
1680 .start = xt_match_seq_start,
1681 .next = xt_match_seq_next,
1682 .stop = xt_mttg_seq_stop,
1683 .show = xt_match_seq_show,
1684};
1685
1686static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1687{
1688 return xt_mttg_seq_start(seq, pos, is_target: true);
1689}
1690
1691static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1692{
1693 return xt_mttg_seq_next(seq, v, ppos, is_target: true);
1694}
1695
1696static int xt_target_seq_show(struct seq_file *seq, void *v)
1697{
1698 const struct nf_mttg_trav *trav = seq->private;
1699 const struct xt_target *target;
1700
1701 switch (trav->class) {
1702 case MTTG_TRAV_NFP_UNSPEC:
1703 case MTTG_TRAV_NFP_SPEC:
1704 if (trav->curr == trav->head)
1705 return 0;
1706 target = list_entry(trav->curr, struct xt_target, list);
1707 if (*target->name)
1708 seq_printf(m: seq, fmt: "%s\n", target->name);
1709 }
1710 return 0;
1711}
1712
1713static const struct seq_operations xt_target_seq_ops = {
1714 .start = xt_target_seq_start,
1715 .next = xt_target_seq_next,
1716 .stop = xt_mttg_seq_stop,
1717 .show = xt_target_seq_show,
1718};
1719
1720#define FORMAT_TABLES "_tables_names"
1721#define FORMAT_MATCHES "_tables_matches"
1722#define FORMAT_TARGETS "_tables_targets"
1723
1724#endif /* CONFIG_PROC_FS */
1725
1726/**
1727 * xt_hook_ops_alloc - set up hooks for a new table
1728 * @table: table with metadata needed to set up hooks
1729 * @fn: Hook function
1730 *
1731 * This function will create the nf_hook_ops that the x_table needs
1732 * to hand to xt_hook_link_net().
1733 */
1734struct nf_hook_ops *
1735xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
1736{
1737 unsigned int hook_mask = table->valid_hooks;
1738 uint8_t i, num_hooks = hweight32(hook_mask);
1739 uint8_t hooknum;
1740 struct nf_hook_ops *ops;
1741
1742 if (!num_hooks)
1743 return ERR_PTR(error: -EINVAL);
1744
1745 ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
1746 if (ops == NULL)
1747 return ERR_PTR(error: -ENOMEM);
1748
1749 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1750 hook_mask >>= 1, ++hooknum) {
1751 if (!(hook_mask & 1))
1752 continue;
1753 ops[i].hook = fn;
1754 ops[i].pf = table->af;
1755 ops[i].hooknum = hooknum;
1756 ops[i].priority = table->priority;
1757 ++i;
1758 }
1759
1760 return ops;
1761}
1762EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
1763
1764int xt_register_template(const struct xt_table *table,
1765 int (*table_init)(struct net *net))
1766{
1767 int ret = -EEXIST, af = table->af;
1768 struct xt_template *t;
1769
1770 mutex_lock(lock: &xt[af].mutex);
1771
1772 list_for_each_entry(t, &xt_templates[af], list) {
1773 if (WARN_ON_ONCE(strcmp(table->name, t->name) == 0))
1774 goto out_unlock;
1775 }
1776
1777 ret = -ENOMEM;
1778 t = kzalloc(sizeof(*t), GFP_KERNEL);
1779 if (!t)
1780 goto out_unlock;
1781
1782 BUILD_BUG_ON(sizeof(t->name) != sizeof(table->name));
1783
1784 strscpy(t->name, table->name, sizeof(t->name));
1785 t->table_init = table_init;
1786 t->me = table->me;
1787 list_add(new: &t->list, head: &xt_templates[af]);
1788 ret = 0;
1789out_unlock:
1790 mutex_unlock(lock: &xt[af].mutex);
1791 return ret;
1792}
1793EXPORT_SYMBOL_GPL(xt_register_template);
1794
1795void xt_unregister_template(const struct xt_table *table)
1796{
1797 struct xt_template *t;
1798 int af = table->af;
1799
1800 mutex_lock(lock: &xt[af].mutex);
1801 list_for_each_entry(t, &xt_templates[af], list) {
1802 if (strcmp(table->name, t->name))
1803 continue;
1804
1805 list_del(entry: &t->list);
1806 mutex_unlock(lock: &xt[af].mutex);
1807 kfree(objp: t);
1808 return;
1809 }
1810
1811 mutex_unlock(lock: &xt[af].mutex);
1812 WARN_ON_ONCE(1);
1813}
1814EXPORT_SYMBOL_GPL(xt_unregister_template);
1815
1816int xt_proto_init(struct net *net, u_int8_t af)
1817{
1818#ifdef CONFIG_PROC_FS
1819 char buf[XT_FUNCTION_MAXNAMELEN];
1820 struct proc_dir_entry *proc;
1821 kuid_t root_uid;
1822 kgid_t root_gid;
1823#endif
1824
1825 if (af >= ARRAY_SIZE(xt_prefix))
1826 return -EINVAL;
1827
1828
1829#ifdef CONFIG_PROC_FS
1830 root_uid = make_kuid(from: net->user_ns, uid: 0);
1831 root_gid = make_kgid(from: net->user_ns, gid: 0);
1832
1833 strscpy(buf, xt_prefix[af], sizeof(buf));
1834 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1835 proc = proc_create_net_data(name: buf, mode: 0440, parent: net->proc_net, ops: &xt_table_seq_ops,
1836 state_size: sizeof(struct seq_net_private),
1837 data: (void *)(unsigned long)af);
1838 if (!proc)
1839 goto out;
1840 if (uid_valid(uid: root_uid) && gid_valid(gid: root_gid))
1841 proc_set_user(proc, root_uid, root_gid);
1842
1843 strscpy(buf, xt_prefix[af], sizeof(buf));
1844 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1845 proc = proc_create_seq_private(name: buf, mode: 0440, parent: net->proc_net,
1846 ops: &xt_match_seq_ops, state_size: sizeof(struct nf_mttg_trav),
1847 data: (void *)(unsigned long)af);
1848 if (!proc)
1849 goto out_remove_tables;
1850 if (uid_valid(uid: root_uid) && gid_valid(gid: root_gid))
1851 proc_set_user(proc, root_uid, root_gid);
1852
1853 strscpy(buf, xt_prefix[af], sizeof(buf));
1854 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1855 proc = proc_create_seq_private(name: buf, mode: 0440, parent: net->proc_net,
1856 ops: &xt_target_seq_ops, state_size: sizeof(struct nf_mttg_trav),
1857 data: (void *)(unsigned long)af);
1858 if (!proc)
1859 goto out_remove_matches;
1860 if (uid_valid(uid: root_uid) && gid_valid(gid: root_gid))
1861 proc_set_user(proc, root_uid, root_gid);
1862#endif
1863
1864 return 0;
1865
1866#ifdef CONFIG_PROC_FS
1867out_remove_matches:
1868 strscpy(buf, xt_prefix[af], sizeof(buf));
1869 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1870 remove_proc_entry(buf, net->proc_net);
1871
1872out_remove_tables:
1873 strscpy(buf, xt_prefix[af], sizeof(buf));
1874 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1875 remove_proc_entry(buf, net->proc_net);
1876out:
1877 return -1;
1878#endif
1879}
1880EXPORT_SYMBOL_GPL(xt_proto_init);
1881
1882void xt_proto_fini(struct net *net, u_int8_t af)
1883{
1884#ifdef CONFIG_PROC_FS
1885 char buf[XT_FUNCTION_MAXNAMELEN];
1886
1887 strscpy(buf, xt_prefix[af], sizeof(buf));
1888 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1889 remove_proc_entry(buf, net->proc_net);
1890
1891 strscpy(buf, xt_prefix[af], sizeof(buf));
1892 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1893 remove_proc_entry(buf, net->proc_net);
1894
1895 strscpy(buf, xt_prefix[af], sizeof(buf));
1896 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1897 remove_proc_entry(buf, net->proc_net);
1898#endif /*CONFIG_PROC_FS*/
1899}
1900EXPORT_SYMBOL_GPL(xt_proto_fini);
1901
1902#ifdef CONFIG_NETFILTER_XTABLES_LEGACY
1903/**
1904 * xt_percpu_counter_alloc - allocate x_tables rule counter
1905 *
1906 * @state: pointer to xt_percpu allocation state
1907 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1908 *
1909 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1910 * contain the address of the real (percpu) counter.
1911 *
1912 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1913 * to fetch the real percpu counter.
1914 *
1915 * To speed up allocation and improve data locality, a 4kb block is
1916 * allocated. Freeing any counter may free an entire block, so all
1917 * counters allocated using the same state must be freed at the same
1918 * time.
1919 *
1920 * xt_percpu_counter_alloc_state contains the base address of the
1921 * allocated page and the current sub-offset.
1922 *
1923 * returns false on error.
1924 */
1925bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1926 struct xt_counters *counter)
1927{
1928 BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
1929
1930 if (nr_cpu_ids <= 1)
1931 return true;
1932
1933 if (!state->mem) {
1934 state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1935 XT_PCPU_BLOCK_SIZE);
1936 if (!state->mem)
1937 return false;
1938 }
1939 counter->pcnt = (__force unsigned long)(state->mem + state->off);
1940 state->off += sizeof(*counter);
1941 if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1942 state->mem = NULL;
1943 state->off = 0;
1944 }
1945 return true;
1946}
1947EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1948
1949void xt_percpu_counter_free(struct xt_counters *counters)
1950{
1951 unsigned long pcnt = counters->pcnt;
1952
1953 if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
1954 free_percpu((void __percpu *)pcnt);
1955}
1956EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1957#endif
1958
1959static int __net_init xt_net_init(struct net *net)
1960{
1961 struct xt_pernet *xt_net = net_generic(net, id: xt_pernet_id);
1962 int i;
1963
1964 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1965 INIT_LIST_HEAD(list: &xt_net->tables[i]);
1966 return 0;
1967}
1968
1969static void __net_exit xt_net_exit(struct net *net)
1970{
1971 struct xt_pernet *xt_net = net_generic(net, id: xt_pernet_id);
1972 int i;
1973
1974 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1975 WARN_ON_ONCE(!list_empty(&xt_net->tables[i]));
1976}
1977
1978static struct pernet_operations xt_net_ops = {
1979 .init = xt_net_init,
1980 .exit = xt_net_exit,
1981 .id = &xt_pernet_id,
1982 .size = sizeof(struct xt_pernet),
1983};
1984
1985static int __init xt_init(void)
1986{
1987 unsigned int i;
1988 int rv;
1989
1990 if (IS_ENABLED(CONFIG_NETFILTER_XTABLES_LEGACY)) {
1991 for_each_possible_cpu(i) {
1992 seqcount_init(&per_cpu(xt_recseq, i));
1993 }
1994 }
1995
1996 xt = kcalloc(NFPROTO_NUMPROTO, sizeof(struct xt_af), GFP_KERNEL);
1997 if (!xt)
1998 return -ENOMEM;
1999
2000 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
2001 mutex_init(&xt[i].mutex);
2002#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
2003 mutex_init(&xt[i].compat_mutex);
2004 xt[i].compat_tab = NULL;
2005#endif
2006 INIT_LIST_HEAD(list: &xt[i].target);
2007 INIT_LIST_HEAD(list: &xt[i].match);
2008 INIT_LIST_HEAD(list: &xt_templates[i]);
2009 }
2010 rv = register_pernet_subsys(&xt_net_ops);
2011 if (rv < 0)
2012 kfree(objp: xt);
2013 return rv;
2014}
2015
2016static void __exit xt_fini(void)
2017{
2018 unregister_pernet_subsys(&xt_net_ops);
2019 kfree(objp: xt);
2020}
2021
2022module_init(xt_init);
2023module_exit(xt_fini);
2024