1 // SPDX-License-Identifier: GPL-2.0 2 /* -*- linux-c -*- 3 * sysctl_net_core.c: sysctl interface to net core subsystem. 4 * 5 * Begun April 1, 1996, Mike Shaver. 6 * Added /proc/sys/net/core directory entry (empty =) ). [MS] 7 */ 8 9 #include <linux/filter.h> 10 #include <linux/mm.h> 11 #include <linux/sysctl.h> 12 #include <linux/module.h> 13 #include <linux/socket.h> 14 #include <linux/netdevice.h> 15 #include <linux/ratelimit.h> 16 #include <linux/vmalloc.h> 17 #include <linux/init.h> 18 #include <linux/slab.h> 19 #include <linux/sched/isolation.h> 20 21 #include <net/ip.h> 22 #include <net/sock.h> 23 #include <net/net_ratelimit.h> 24 #include <net/busy_poll.h> 25 #include <net/pkt_sched.h> 26 27 #include "dev.h" 28 29 static int int_3600 = 3600; 30 static int min_sndbuf = SOCK_MIN_SNDBUF; 31 static int min_rcvbuf = SOCK_MIN_RCVBUF; 32 static int max_skb_frags = MAX_SKB_FRAGS; 33 static int min_mem_pcpu_rsv = SK_MEMORY_PCPU_RESERVE; 34 static int netdev_budget_usecs_min = 2 * USEC_PER_SEC / HZ; 35 36 static int net_msg_warn; /* Unused, but still a sysctl */ 37 38 int sysctl_fb_tunnels_only_for_init_net __read_mostly = 0; 39 EXPORT_SYMBOL(sysctl_fb_tunnels_only_for_init_net); 40 41 /* 0 - Keep current behavior: 42 * IPv4: inherit all current settings from init_net 43 * IPv6: reset all settings to default 44 * 1 - Both inherit all current settings from init_net 45 * 2 - Both reset all settings to default 46 * 3 - Both inherit all settings from current netns 47 */ 48 int sysctl_devconf_inherit_init_net __read_mostly; 49 EXPORT_SYMBOL(sysctl_devconf_inherit_init_net); 50 51 #if IS_ENABLED(CONFIG_NET_FLOW_LIMIT) || IS_ENABLED(CONFIG_RPS) 52 static void dump_cpumask(void *buffer, size_t *lenp, loff_t *ppos, 53 struct cpumask *mask) 54 { 55 char kbuf[128]; 56 int len; 57 58 if (*ppos || !*lenp) { 59 *lenp = 0; 60 return; 61 } 62 63 len = min(sizeof(kbuf) - 1, *lenp); 64 len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask)); 65 if (!len) { 66 *lenp = 0; 67 return; 68 } 69 70 if (len < *lenp) 71 kbuf[len++] = '\n'; 72 memcpy(buffer, kbuf, len); 73 *lenp = len; 74 *ppos += len; 75 } 76 #endif 77 78 #ifdef CONFIG_RPS 79 80 static struct cpumask *rps_default_mask_cow_alloc(struct net *net) 81 { 82 struct cpumask *rps_default_mask; 83 84 if (net->core.rps_default_mask) 85 return net->core.rps_default_mask; 86 87 rps_default_mask = kzalloc(cpumask_size(), GFP_KERNEL); 88 if (!rps_default_mask) 89 return NULL; 90 91 /* pairs with READ_ONCE in rx_queue_default_mask() */ 92 WRITE_ONCE(net->core.rps_default_mask, rps_default_mask); 93 return rps_default_mask; 94 } 95 96 static int rps_default_mask_sysctl(struct ctl_table *table, int write, 97 void *buffer, size_t *lenp, loff_t *ppos) 98 { 99 struct net *net = (struct net *)table->data; 100 int err = 0; 101 102 rtnl_lock(); 103 if (write) { 104 struct cpumask *rps_default_mask = rps_default_mask_cow_alloc(net); 105 106 err = -ENOMEM; 107 if (!rps_default_mask) 108 goto done; 109 110 err = cpumask_parse(buffer, rps_default_mask); 111 if (err) 112 goto done; 113 114 err = rps_cpumask_housekeeping(rps_default_mask); 115 if (err) 116 goto done; 117 } else { 118 dump_cpumask(buffer, lenp, ppos, 119 net->core.rps_default_mask ? : cpu_none_mask); 120 } 121 122 done: 123 rtnl_unlock(); 124 return err; 125 } 126 127 static int rps_sock_flow_sysctl(struct ctl_table *table, int write, 128 void *buffer, size_t *lenp, loff_t *ppos) 129 { 130 unsigned int orig_size, size; 131 int ret, i; 132 struct ctl_table tmp = { 133 .data = &size, 134 .maxlen = sizeof(size), 135 .mode = table->mode 136 }; 137 struct rps_sock_flow_table *orig_sock_table, *sock_table; 138 static DEFINE_MUTEX(sock_flow_mutex); 139 140 mutex_lock(&sock_flow_mutex); 141 142 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table, 143 lockdep_is_held(&sock_flow_mutex)); 144 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0; 145 146 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos); 147 148 if (write) { 149 if (size) { 150 if (size > 1<<29) { 151 /* Enforce limit to prevent overflow */ 152 mutex_unlock(&sock_flow_mutex); 153 return -EINVAL; 154 } 155 size = roundup_pow_of_two(size); 156 if (size != orig_size) { 157 sock_table = 158 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size)); 159 if (!sock_table) { 160 mutex_unlock(&sock_flow_mutex); 161 return -ENOMEM; 162 } 163 rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1; 164 sock_table->mask = size - 1; 165 } else 166 sock_table = orig_sock_table; 167 168 for (i = 0; i < size; i++) 169 sock_table->ents[i] = RPS_NO_CPU; 170 } else 171 sock_table = NULL; 172 173 if (sock_table != orig_sock_table) { 174 rcu_assign_pointer(rps_sock_flow_table, sock_table); 175 if (sock_table) { 176 static_branch_inc(&rps_needed); 177 static_branch_inc(&rfs_needed); 178 } 179 if (orig_sock_table) { 180 static_branch_dec(&rps_needed); 181 static_branch_dec(&rfs_needed); 182 kvfree_rcu_mightsleep(orig_sock_table); 183 } 184 } 185 } 186 187 mutex_unlock(&sock_flow_mutex); 188 189 return ret; 190 } 191 #endif /* CONFIG_RPS */ 192 193 #ifdef CONFIG_NET_FLOW_LIMIT 194 static DEFINE_MUTEX(flow_limit_update_mutex); 195 196 static int flow_limit_cpu_sysctl(struct ctl_table *table, int write, 197 void *buffer, size_t *lenp, loff_t *ppos) 198 { 199 struct sd_flow_limit *cur; 200 struct softnet_data *sd; 201 cpumask_var_t mask; 202 int i, len, ret = 0; 203 204 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) 205 return -ENOMEM; 206 207 if (write) { 208 ret = cpumask_parse(buffer, mask); 209 if (ret) 210 goto done; 211 212 mutex_lock(&flow_limit_update_mutex); 213 len = sizeof(*cur) + netdev_flow_limit_table_len; 214 for_each_possible_cpu(i) { 215 sd = &per_cpu(softnet_data, i); 216 cur = rcu_dereference_protected(sd->flow_limit, 217 lockdep_is_held(&flow_limit_update_mutex)); 218 if (cur && !cpumask_test_cpu(i, mask)) { 219 RCU_INIT_POINTER(sd->flow_limit, NULL); 220 kfree_rcu_mightsleep(cur); 221 } else if (!cur && cpumask_test_cpu(i, mask)) { 222 cur = kzalloc_node(len, GFP_KERNEL, 223 cpu_to_node(i)); 224 if (!cur) { 225 /* not unwinding previous changes */ 226 ret = -ENOMEM; 227 goto write_unlock; 228 } 229 cur->num_buckets = netdev_flow_limit_table_len; 230 rcu_assign_pointer(sd->flow_limit, cur); 231 } 232 } 233 write_unlock: 234 mutex_unlock(&flow_limit_update_mutex); 235 } else { 236 cpumask_clear(mask); 237 rcu_read_lock(); 238 for_each_possible_cpu(i) { 239 sd = &per_cpu(softnet_data, i); 240 if (rcu_dereference(sd->flow_limit)) 241 cpumask_set_cpu(i, mask); 242 } 243 rcu_read_unlock(); 244 245 dump_cpumask(buffer, lenp, ppos, mask); 246 } 247 248 done: 249 free_cpumask_var(mask); 250 return ret; 251 } 252 253 static int flow_limit_table_len_sysctl(struct ctl_table *table, int write, 254 void *buffer, size_t *lenp, loff_t *ppos) 255 { 256 unsigned int old, *ptr; 257 int ret; 258 259 mutex_lock(&flow_limit_update_mutex); 260 261 ptr = table->data; 262 old = *ptr; 263 ret = proc_dointvec(table, write, buffer, lenp, ppos); 264 if (!ret && write && !is_power_of_2(*ptr)) { 265 *ptr = old; 266 ret = -EINVAL; 267 } 268 269 mutex_unlock(&flow_limit_update_mutex); 270 return ret; 271 } 272 #endif /* CONFIG_NET_FLOW_LIMIT */ 273 274 #ifdef CONFIG_NET_SCHED 275 static int set_default_qdisc(struct ctl_table *table, int write, 276 void *buffer, size_t *lenp, loff_t *ppos) 277 { 278 char id[IFNAMSIZ]; 279 struct ctl_table tbl = { 280 .data = id, 281 .maxlen = IFNAMSIZ, 282 }; 283 int ret; 284 285 qdisc_get_default(id, IFNAMSIZ); 286 287 ret = proc_dostring(&tbl, write, buffer, lenp, ppos); 288 if (write && ret == 0) 289 ret = qdisc_set_default(id); 290 return ret; 291 } 292 #endif 293 294 static int proc_do_dev_weight(struct ctl_table *table, int write, 295 void *buffer, size_t *lenp, loff_t *ppos) 296 { 297 static DEFINE_MUTEX(dev_weight_mutex); 298 int ret, weight; 299 300 mutex_lock(&dev_weight_mutex); 301 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 302 if (!ret && write) { 303 weight = READ_ONCE(weight_p); 304 WRITE_ONCE(dev_rx_weight, weight * dev_weight_rx_bias); 305 WRITE_ONCE(dev_tx_weight, weight * dev_weight_tx_bias); 306 } 307 mutex_unlock(&dev_weight_mutex); 308 309 return ret; 310 } 311 312 static int proc_do_rss_key(struct ctl_table *table, int write, 313 void *buffer, size_t *lenp, loff_t *ppos) 314 { 315 struct ctl_table fake_table; 316 char buf[NETDEV_RSS_KEY_LEN * 3]; 317 318 snprintf(buf, sizeof(buf), "%*phC", NETDEV_RSS_KEY_LEN, netdev_rss_key); 319 fake_table.data = buf; 320 fake_table.maxlen = sizeof(buf); 321 return proc_dostring(&fake_table, write, buffer, lenp, ppos); 322 } 323 324 #ifdef CONFIG_BPF_JIT 325 static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write, 326 void *buffer, size_t *lenp, 327 loff_t *ppos) 328 { 329 int ret, jit_enable = *(int *)table->data; 330 int min = *(int *)table->extra1; 331 int max = *(int *)table->extra2; 332 struct ctl_table tmp = *table; 333 334 if (write && !capable(CAP_SYS_ADMIN)) 335 return -EPERM; 336 337 tmp.data = &jit_enable; 338 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 339 if (write && !ret) { 340 if (jit_enable < 2 || 341 (jit_enable == 2 && bpf_dump_raw_ok(current_cred()))) { 342 *(int *)table->data = jit_enable; 343 if (jit_enable == 2) 344 pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n"); 345 } else { 346 ret = -EPERM; 347 } 348 } 349 350 if (write && ret && min == max) 351 pr_info_once("CONFIG_BPF_JIT_ALWAYS_ON is enabled, bpf_jit_enable is permanently set to 1.\n"); 352 353 return ret; 354 } 355 356 # ifdef CONFIG_HAVE_EBPF_JIT 357 static int 358 proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write, 359 void *buffer, size_t *lenp, loff_t *ppos) 360 { 361 if (!capable(CAP_SYS_ADMIN)) 362 return -EPERM; 363 364 return proc_dointvec_minmax(table, write, buffer, lenp, ppos); 365 } 366 # endif /* CONFIG_HAVE_EBPF_JIT */ 367 368 static int 369 proc_dolongvec_minmax_bpf_restricted(struct ctl_table *table, int write, 370 void *buffer, size_t *lenp, loff_t *ppos) 371 { 372 if (!capable(CAP_SYS_ADMIN)) 373 return -EPERM; 374 375 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos); 376 } 377 #endif 378 379 static struct ctl_table net_core_table[] = { 380 { 381 .procname = "wmem_max", 382 .data = &sysctl_wmem_max, 383 .maxlen = sizeof(int), 384 .mode = 0644, 385 .proc_handler = proc_dointvec_minmax, 386 .extra1 = &min_sndbuf, 387 }, 388 { 389 .procname = "rmem_max", 390 .data = &sysctl_rmem_max, 391 .maxlen = sizeof(int), 392 .mode = 0644, 393 .proc_handler = proc_dointvec_minmax, 394 .extra1 = &min_rcvbuf, 395 }, 396 { 397 .procname = "wmem_default", 398 .data = &sysctl_wmem_default, 399 .maxlen = sizeof(int), 400 .mode = 0644, 401 .proc_handler = proc_dointvec_minmax, 402 .extra1 = &min_sndbuf, 403 }, 404 { 405 .procname = "rmem_default", 406 .data = &sysctl_rmem_default, 407 .maxlen = sizeof(int), 408 .mode = 0644, 409 .proc_handler = proc_dointvec_minmax, 410 .extra1 = &min_rcvbuf, 411 }, 412 { 413 .procname = "mem_pcpu_rsv", 414 .data = &sysctl_mem_pcpu_rsv, 415 .maxlen = sizeof(int), 416 .mode = 0644, 417 .proc_handler = proc_dointvec_minmax, 418 .extra1 = &min_mem_pcpu_rsv, 419 }, 420 { 421 .procname = "dev_weight", 422 .data = &weight_p, 423 .maxlen = sizeof(int), 424 .mode = 0644, 425 .proc_handler = proc_do_dev_weight, 426 .extra1 = SYSCTL_ONE, 427 }, 428 { 429 .procname = "dev_weight_rx_bias", 430 .data = &dev_weight_rx_bias, 431 .maxlen = sizeof(int), 432 .mode = 0644, 433 .proc_handler = proc_do_dev_weight, 434 .extra1 = SYSCTL_ONE, 435 }, 436 { 437 .procname = "dev_weight_tx_bias", 438 .data = &dev_weight_tx_bias, 439 .maxlen = sizeof(int), 440 .mode = 0644, 441 .proc_handler = proc_do_dev_weight, 442 .extra1 = SYSCTL_ONE, 443 }, 444 { 445 .procname = "netdev_max_backlog", 446 .data = &netdev_max_backlog, 447 .maxlen = sizeof(int), 448 .mode = 0644, 449 .proc_handler = proc_dointvec 450 }, 451 { 452 .procname = "netdev_rss_key", 453 .data = &netdev_rss_key, 454 .maxlen = sizeof(int), 455 .mode = 0444, 456 .proc_handler = proc_do_rss_key, 457 }, 458 #ifdef CONFIG_BPF_JIT 459 { 460 .procname = "bpf_jit_enable", 461 .data = &bpf_jit_enable, 462 .maxlen = sizeof(int), 463 .mode = 0644, 464 .proc_handler = proc_dointvec_minmax_bpf_enable, 465 # ifdef CONFIG_BPF_JIT_ALWAYS_ON 466 .extra1 = SYSCTL_ONE, 467 .extra2 = SYSCTL_ONE, 468 # else 469 .extra1 = SYSCTL_ZERO, 470 .extra2 = SYSCTL_TWO, 471 # endif 472 }, 473 # ifdef CONFIG_HAVE_EBPF_JIT 474 { 475 .procname = "bpf_jit_harden", 476 .data = &bpf_jit_harden, 477 .maxlen = sizeof(int), 478 .mode = 0600, 479 .proc_handler = proc_dointvec_minmax_bpf_restricted, 480 .extra1 = SYSCTL_ZERO, 481 .extra2 = SYSCTL_TWO, 482 }, 483 { 484 .procname = "bpf_jit_kallsyms", 485 .data = &bpf_jit_kallsyms, 486 .maxlen = sizeof(int), 487 .mode = 0600, 488 .proc_handler = proc_dointvec_minmax_bpf_restricted, 489 .extra1 = SYSCTL_ZERO, 490 .extra2 = SYSCTL_ONE, 491 }, 492 # endif 493 { 494 .procname = "bpf_jit_limit", 495 .data = &bpf_jit_limit, 496 .maxlen = sizeof(long), 497 .mode = 0600, 498 .proc_handler = proc_dolongvec_minmax_bpf_restricted, 499 .extra1 = SYSCTL_LONG_ONE, 500 .extra2 = &bpf_jit_limit_max, 501 }, 502 #endif 503 { 504 .procname = "netdev_tstamp_prequeue", 505 .data = &netdev_tstamp_prequeue, 506 .maxlen = sizeof(int), 507 .mode = 0644, 508 .proc_handler = proc_dointvec 509 }, 510 { 511 .procname = "message_cost", 512 .data = &net_ratelimit_state.interval, 513 .maxlen = sizeof(int), 514 .mode = 0644, 515 .proc_handler = proc_dointvec_jiffies, 516 }, 517 { 518 .procname = "message_burst", 519 .data = &net_ratelimit_state.burst, 520 .maxlen = sizeof(int), 521 .mode = 0644, 522 .proc_handler = proc_dointvec, 523 }, 524 { 525 .procname = "optmem_max", 526 .data = &sysctl_optmem_max, 527 .maxlen = sizeof(int), 528 .mode = 0644, 529 .proc_handler = proc_dointvec 530 }, 531 { 532 .procname = "tstamp_allow_data", 533 .data = &sysctl_tstamp_allow_data, 534 .maxlen = sizeof(int), 535 .mode = 0644, 536 .proc_handler = proc_dointvec_minmax, 537 .extra1 = SYSCTL_ZERO, 538 .extra2 = SYSCTL_ONE 539 }, 540 #ifdef CONFIG_RPS 541 { 542 .procname = "rps_sock_flow_entries", 543 .maxlen = sizeof(int), 544 .mode = 0644, 545 .proc_handler = rps_sock_flow_sysctl 546 }, 547 #endif 548 #ifdef CONFIG_NET_FLOW_LIMIT 549 { 550 .procname = "flow_limit_cpu_bitmap", 551 .mode = 0644, 552 .proc_handler = flow_limit_cpu_sysctl 553 }, 554 { 555 .procname = "flow_limit_table_len", 556 .data = &netdev_flow_limit_table_len, 557 .maxlen = sizeof(int), 558 .mode = 0644, 559 .proc_handler = flow_limit_table_len_sysctl 560 }, 561 #endif /* CONFIG_NET_FLOW_LIMIT */ 562 #ifdef CONFIG_NET_RX_BUSY_POLL 563 { 564 .procname = "busy_poll", 565 .data = &sysctl_net_busy_poll, 566 .maxlen = sizeof(unsigned int), 567 .mode = 0644, 568 .proc_handler = proc_dointvec_minmax, 569 .extra1 = SYSCTL_ZERO, 570 }, 571 { 572 .procname = "busy_read", 573 .data = &sysctl_net_busy_read, 574 .maxlen = sizeof(unsigned int), 575 .mode = 0644, 576 .proc_handler = proc_dointvec_minmax, 577 .extra1 = SYSCTL_ZERO, 578 }, 579 #endif 580 #ifdef CONFIG_NET_SCHED 581 { 582 .procname = "default_qdisc", 583 .mode = 0644, 584 .maxlen = IFNAMSIZ, 585 .proc_handler = set_default_qdisc 586 }, 587 #endif 588 { 589 .procname = "netdev_budget", 590 .data = &netdev_budget, 591 .maxlen = sizeof(int), 592 .mode = 0644, 593 .proc_handler = proc_dointvec 594 }, 595 { 596 .procname = "warnings", 597 .data = &net_msg_warn, 598 .maxlen = sizeof(int), 599 .mode = 0644, 600 .proc_handler = proc_dointvec 601 }, 602 { 603 .procname = "max_skb_frags", 604 .data = &sysctl_max_skb_frags, 605 .maxlen = sizeof(int), 606 .mode = 0644, 607 .proc_handler = proc_dointvec_minmax, 608 .extra1 = SYSCTL_ONE, 609 .extra2 = &max_skb_frags, 610 }, 611 { 612 .procname = "netdev_budget_usecs", 613 .data = &netdev_budget_usecs, 614 .maxlen = sizeof(unsigned int), 615 .mode = 0644, 616 .proc_handler = proc_dointvec_minmax, 617 .extra1 = &netdev_budget_usecs_min, 618 }, 619 { 620 .procname = "fb_tunnels_only_for_init_net", 621 .data = &sysctl_fb_tunnels_only_for_init_net, 622 .maxlen = sizeof(int), 623 .mode = 0644, 624 .proc_handler = proc_dointvec_minmax, 625 .extra1 = SYSCTL_ZERO, 626 .extra2 = SYSCTL_TWO, 627 }, 628 { 629 .procname = "devconf_inherit_init_net", 630 .data = &sysctl_devconf_inherit_init_net, 631 .maxlen = sizeof(int), 632 .mode = 0644, 633 .proc_handler = proc_dointvec_minmax, 634 .extra1 = SYSCTL_ZERO, 635 .extra2 = SYSCTL_THREE, 636 }, 637 { 638 .procname = "high_order_alloc_disable", 639 .data = &net_high_order_alloc_disable_key.key, 640 .maxlen = sizeof(net_high_order_alloc_disable_key), 641 .mode = 0644, 642 .proc_handler = proc_do_static_key, 643 }, 644 { 645 .procname = "gro_normal_batch", 646 .data = &gro_normal_batch, 647 .maxlen = sizeof(unsigned int), 648 .mode = 0644, 649 .proc_handler = proc_dointvec_minmax, 650 .extra1 = SYSCTL_ONE, 651 }, 652 { 653 .procname = "netdev_unregister_timeout_secs", 654 .data = &netdev_unregister_timeout_secs, 655 .maxlen = sizeof(unsigned int), 656 .mode = 0644, 657 .proc_handler = proc_dointvec_minmax, 658 .extra1 = SYSCTL_ONE, 659 .extra2 = &int_3600, 660 }, 661 { 662 .procname = "skb_defer_max", 663 .data = &sysctl_skb_defer_max, 664 .maxlen = sizeof(unsigned int), 665 .mode = 0644, 666 .proc_handler = proc_dointvec_minmax, 667 .extra1 = SYSCTL_ZERO, 668 }, 669 { } 670 }; 671 672 static struct ctl_table netns_core_table[] = { 673 #if IS_ENABLED(CONFIG_RPS) 674 { 675 .procname = "rps_default_mask", 676 .data = &init_net, 677 .mode = 0644, 678 .proc_handler = rps_default_mask_sysctl 679 }, 680 #endif 681 { 682 .procname = "somaxconn", 683 .data = &init_net.core.sysctl_somaxconn, 684 .maxlen = sizeof(int), 685 .mode = 0644, 686 .extra1 = SYSCTL_ZERO, 687 .proc_handler = proc_dointvec_minmax 688 }, 689 { 690 .procname = "txrehash", 691 .data = &init_net.core.sysctl_txrehash, 692 .maxlen = sizeof(u8), 693 .mode = 0644, 694 .extra1 = SYSCTL_ZERO, 695 .extra2 = SYSCTL_ONE, 696 .proc_handler = proc_dou8vec_minmax, 697 }, 698 { } 699 }; 700 701 static int __init fb_tunnels_only_for_init_net_sysctl_setup(char *str) 702 { 703 /* fallback tunnels for initns only */ 704 if (!strncmp(str, "initns", 6)) 705 sysctl_fb_tunnels_only_for_init_net = 1; 706 /* no fallback tunnels anywhere */ 707 else if (!strncmp(str, "none", 4)) 708 sysctl_fb_tunnels_only_for_init_net = 2; 709 710 return 1; 711 } 712 __setup("fb_tunnels=", fb_tunnels_only_for_init_net_sysctl_setup); 713 714 static __net_init int sysctl_core_net_init(struct net *net) 715 { 716 struct ctl_table *tbl, *tmp; 717 718 tbl = netns_core_table; 719 if (!net_eq(net, &init_net)) { 720 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL); 721 if (tbl == NULL) 722 goto err_dup; 723 724 for (tmp = tbl; tmp->procname; tmp++) 725 tmp->data += (char *)net - (char *)&init_net; 726 } 727 728 net->core.sysctl_hdr = register_net_sysctl_sz(net, "net/core", tbl, 729 ARRAY_SIZE(netns_core_table)); 730 if (net->core.sysctl_hdr == NULL) 731 goto err_reg; 732 733 return 0; 734 735 err_reg: 736 if (tbl != netns_core_table) 737 kfree(tbl); 738 err_dup: 739 return -ENOMEM; 740 } 741 742 static __net_exit void sysctl_core_net_exit(struct net *net) 743 { 744 struct ctl_table *tbl; 745 746 tbl = net->core.sysctl_hdr->ctl_table_arg; 747 unregister_net_sysctl_table(net->core.sysctl_hdr); 748 BUG_ON(tbl == netns_core_table); 749 #if IS_ENABLED(CONFIG_RPS) 750 kfree(net->core.rps_default_mask); 751 #endif 752 kfree(tbl); 753 } 754 755 static __net_initdata struct pernet_operations sysctl_core_ops = { 756 .init = sysctl_core_net_init, 757 .exit = sysctl_core_net_exit, 758 }; 759 760 static __init int sysctl_core_init(void) 761 { 762 register_net_sysctl(&init_net, "net/core", net_core_table); 763 return register_pernet_subsys(&sysctl_core_ops); 764 } 765 766 fs_initcall(sysctl_core_init); 767