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