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