sysctl_net_core.c (16332B)
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 28static int int_3600 = 3600; 29static int min_sndbuf = SOCK_MIN_SNDBUF; 30static int min_rcvbuf = SOCK_MIN_RCVBUF; 31static int max_skb_frags = MAX_SKB_FRAGS; 32static long long_max __maybe_unused = LONG_MAX; 33 34static int net_msg_warn; /* Unused, but still a sysctl */ 35 36int sysctl_fb_tunnels_only_for_init_net __read_mostly = 0; 37EXPORT_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 */ 46int sysctl_devconf_inherit_init_net __read_mostly; 47EXPORT_SYMBOL(sysctl_devconf_inherit_init_net); 48 49#ifdef CONFIG_RPS 50static 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 117static DEFINE_MUTEX(flow_limit_update_mutex); 118 119static 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 } 156write_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 188done: 189 free_cpumask_var(mask); 190 return ret; 191} 192 193static 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 215static 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 234static int proc_do_dev_weight(struct ctl_table *table, int write, 235 void *buffer, size_t *lenp, loff_t *ppos) 236{ 237 int ret; 238 239 ret = proc_dointvec(table, write, buffer, lenp, ppos); 240 if (ret != 0) 241 return ret; 242 243 dev_rx_weight = weight_p * dev_weight_rx_bias; 244 dev_tx_weight = weight_p * dev_weight_tx_bias; 245 246 return ret; 247} 248 249static int proc_do_rss_key(struct ctl_table *table, int write, 250 void *buffer, size_t *lenp, loff_t *ppos) 251{ 252 struct ctl_table fake_table; 253 char buf[NETDEV_RSS_KEY_LEN * 3]; 254 255 snprintf(buf, sizeof(buf), "%*phC", NETDEV_RSS_KEY_LEN, netdev_rss_key); 256 fake_table.data = buf; 257 fake_table.maxlen = sizeof(buf); 258 return proc_dostring(&fake_table, write, buffer, lenp, ppos); 259} 260 261#ifdef CONFIG_BPF_JIT 262static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write, 263 void *buffer, size_t *lenp, 264 loff_t *ppos) 265{ 266 int ret, jit_enable = *(int *)table->data; 267 int min = *(int *)table->extra1; 268 int max = *(int *)table->extra2; 269 struct ctl_table tmp = *table; 270 271 if (write && !capable(CAP_SYS_ADMIN)) 272 return -EPERM; 273 274 tmp.data = &jit_enable; 275 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 276 if (write && !ret) { 277 if (jit_enable < 2 || 278 (jit_enable == 2 && bpf_dump_raw_ok(current_cred()))) { 279 *(int *)table->data = jit_enable; 280 if (jit_enable == 2) 281 pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n"); 282 } else { 283 ret = -EPERM; 284 } 285 } 286 287 if (write && ret && min == max) 288 pr_info_once("CONFIG_BPF_JIT_ALWAYS_ON is enabled, bpf_jit_enable is permanently set to 1.\n"); 289 290 return ret; 291} 292 293# ifdef CONFIG_HAVE_EBPF_JIT 294static int 295proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write, 296 void *buffer, size_t *lenp, loff_t *ppos) 297{ 298 if (!capable(CAP_SYS_ADMIN)) 299 return -EPERM; 300 301 return proc_dointvec_minmax(table, write, buffer, lenp, ppos); 302} 303# endif /* CONFIG_HAVE_EBPF_JIT */ 304 305static int 306proc_dolongvec_minmax_bpf_restricted(struct ctl_table *table, int write, 307 void *buffer, size_t *lenp, loff_t *ppos) 308{ 309 if (!capable(CAP_SYS_ADMIN)) 310 return -EPERM; 311 312 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos); 313} 314#endif 315 316static struct ctl_table net_core_table[] = { 317 { 318 .procname = "wmem_max", 319 .data = &sysctl_wmem_max, 320 .maxlen = sizeof(int), 321 .mode = 0644, 322 .proc_handler = proc_dointvec_minmax, 323 .extra1 = &min_sndbuf, 324 }, 325 { 326 .procname = "rmem_max", 327 .data = &sysctl_rmem_max, 328 .maxlen = sizeof(int), 329 .mode = 0644, 330 .proc_handler = proc_dointvec_minmax, 331 .extra1 = &min_rcvbuf, 332 }, 333 { 334 .procname = "wmem_default", 335 .data = &sysctl_wmem_default, 336 .maxlen = sizeof(int), 337 .mode = 0644, 338 .proc_handler = proc_dointvec_minmax, 339 .extra1 = &min_sndbuf, 340 }, 341 { 342 .procname = "rmem_default", 343 .data = &sysctl_rmem_default, 344 .maxlen = sizeof(int), 345 .mode = 0644, 346 .proc_handler = proc_dointvec_minmax, 347 .extra1 = &min_rcvbuf, 348 }, 349 { 350 .procname = "dev_weight", 351 .data = &weight_p, 352 .maxlen = sizeof(int), 353 .mode = 0644, 354 .proc_handler = proc_do_dev_weight, 355 }, 356 { 357 .procname = "dev_weight_rx_bias", 358 .data = &dev_weight_rx_bias, 359 .maxlen = sizeof(int), 360 .mode = 0644, 361 .proc_handler = proc_do_dev_weight, 362 }, 363 { 364 .procname = "dev_weight_tx_bias", 365 .data = &dev_weight_tx_bias, 366 .maxlen = sizeof(int), 367 .mode = 0644, 368 .proc_handler = proc_do_dev_weight, 369 }, 370 { 371 .procname = "netdev_max_backlog", 372 .data = &netdev_max_backlog, 373 .maxlen = sizeof(int), 374 .mode = 0644, 375 .proc_handler = proc_dointvec 376 }, 377 { 378 .procname = "netdev_rss_key", 379 .data = &netdev_rss_key, 380 .maxlen = sizeof(int), 381 .mode = 0444, 382 .proc_handler = proc_do_rss_key, 383 }, 384#ifdef CONFIG_BPF_JIT 385 { 386 .procname = "bpf_jit_enable", 387 .data = &bpf_jit_enable, 388 .maxlen = sizeof(int), 389 .mode = 0644, 390 .proc_handler = proc_dointvec_minmax_bpf_enable, 391# ifdef CONFIG_BPF_JIT_ALWAYS_ON 392 .extra1 = SYSCTL_ONE, 393 .extra2 = SYSCTL_ONE, 394# else 395 .extra1 = SYSCTL_ZERO, 396 .extra2 = SYSCTL_TWO, 397# endif 398 }, 399# ifdef CONFIG_HAVE_EBPF_JIT 400 { 401 .procname = "bpf_jit_harden", 402 .data = &bpf_jit_harden, 403 .maxlen = sizeof(int), 404 .mode = 0600, 405 .proc_handler = proc_dointvec_minmax_bpf_restricted, 406 .extra1 = SYSCTL_ZERO, 407 .extra2 = SYSCTL_TWO, 408 }, 409 { 410 .procname = "bpf_jit_kallsyms", 411 .data = &bpf_jit_kallsyms, 412 .maxlen = sizeof(int), 413 .mode = 0600, 414 .proc_handler = proc_dointvec_minmax_bpf_restricted, 415 .extra1 = SYSCTL_ZERO, 416 .extra2 = SYSCTL_ONE, 417 }, 418# endif 419 { 420 .procname = "bpf_jit_limit", 421 .data = &bpf_jit_limit, 422 .maxlen = sizeof(long), 423 .mode = 0600, 424 .proc_handler = proc_dolongvec_minmax_bpf_restricted, 425 .extra1 = SYSCTL_LONG_ONE, 426 .extra2 = &bpf_jit_limit_max, 427 }, 428#endif 429 { 430 .procname = "netdev_tstamp_prequeue", 431 .data = &netdev_tstamp_prequeue, 432 .maxlen = sizeof(int), 433 .mode = 0644, 434 .proc_handler = proc_dointvec 435 }, 436 { 437 .procname = "message_cost", 438 .data = &net_ratelimit_state.interval, 439 .maxlen = sizeof(int), 440 .mode = 0644, 441 .proc_handler = proc_dointvec_jiffies, 442 }, 443 { 444 .procname = "message_burst", 445 .data = &net_ratelimit_state.burst, 446 .maxlen = sizeof(int), 447 .mode = 0644, 448 .proc_handler = proc_dointvec, 449 }, 450 { 451 .procname = "optmem_max", 452 .data = &sysctl_optmem_max, 453 .maxlen = sizeof(int), 454 .mode = 0644, 455 .proc_handler = proc_dointvec 456 }, 457 { 458 .procname = "tstamp_allow_data", 459 .data = &sysctl_tstamp_allow_data, 460 .maxlen = sizeof(int), 461 .mode = 0644, 462 .proc_handler = proc_dointvec_minmax, 463 .extra1 = SYSCTL_ZERO, 464 .extra2 = SYSCTL_ONE 465 }, 466#ifdef CONFIG_RPS 467 { 468 .procname = "rps_sock_flow_entries", 469 .maxlen = sizeof(int), 470 .mode = 0644, 471 .proc_handler = rps_sock_flow_sysctl 472 }, 473#endif 474#ifdef CONFIG_NET_FLOW_LIMIT 475 { 476 .procname = "flow_limit_cpu_bitmap", 477 .mode = 0644, 478 .proc_handler = flow_limit_cpu_sysctl 479 }, 480 { 481 .procname = "flow_limit_table_len", 482 .data = &netdev_flow_limit_table_len, 483 .maxlen = sizeof(int), 484 .mode = 0644, 485 .proc_handler = flow_limit_table_len_sysctl 486 }, 487#endif /* CONFIG_NET_FLOW_LIMIT */ 488#ifdef CONFIG_NET_RX_BUSY_POLL 489 { 490 .procname = "busy_poll", 491 .data = &sysctl_net_busy_poll, 492 .maxlen = sizeof(unsigned int), 493 .mode = 0644, 494 .proc_handler = proc_dointvec_minmax, 495 .extra1 = SYSCTL_ZERO, 496 }, 497 { 498 .procname = "busy_read", 499 .data = &sysctl_net_busy_read, 500 .maxlen = sizeof(unsigned int), 501 .mode = 0644, 502 .proc_handler = proc_dointvec_minmax, 503 .extra1 = SYSCTL_ZERO, 504 }, 505#endif 506#ifdef CONFIG_NET_SCHED 507 { 508 .procname = "default_qdisc", 509 .mode = 0644, 510 .maxlen = IFNAMSIZ, 511 .proc_handler = set_default_qdisc 512 }, 513#endif 514 { 515 .procname = "netdev_budget", 516 .data = &netdev_budget, 517 .maxlen = sizeof(int), 518 .mode = 0644, 519 .proc_handler = proc_dointvec 520 }, 521 { 522 .procname = "warnings", 523 .data = &net_msg_warn, 524 .maxlen = sizeof(int), 525 .mode = 0644, 526 .proc_handler = proc_dointvec 527 }, 528 { 529 .procname = "max_skb_frags", 530 .data = &sysctl_max_skb_frags, 531 .maxlen = sizeof(int), 532 .mode = 0644, 533 .proc_handler = proc_dointvec_minmax, 534 .extra1 = SYSCTL_ONE, 535 .extra2 = &max_skb_frags, 536 }, 537 { 538 .procname = "netdev_budget_usecs", 539 .data = &netdev_budget_usecs, 540 .maxlen = sizeof(unsigned int), 541 .mode = 0644, 542 .proc_handler = proc_dointvec_minmax, 543 .extra1 = SYSCTL_ZERO, 544 }, 545 { 546 .procname = "fb_tunnels_only_for_init_net", 547 .data = &sysctl_fb_tunnels_only_for_init_net, 548 .maxlen = sizeof(int), 549 .mode = 0644, 550 .proc_handler = proc_dointvec_minmax, 551 .extra1 = SYSCTL_ZERO, 552 .extra2 = SYSCTL_TWO, 553 }, 554 { 555 .procname = "devconf_inherit_init_net", 556 .data = &sysctl_devconf_inherit_init_net, 557 .maxlen = sizeof(int), 558 .mode = 0644, 559 .proc_handler = proc_dointvec_minmax, 560 .extra1 = SYSCTL_ZERO, 561 .extra2 = SYSCTL_THREE, 562 }, 563 { 564 .procname = "high_order_alloc_disable", 565 .data = &net_high_order_alloc_disable_key.key, 566 .maxlen = sizeof(net_high_order_alloc_disable_key), 567 .mode = 0644, 568 .proc_handler = proc_do_static_key, 569 }, 570 { 571 .procname = "gro_normal_batch", 572 .data = &gro_normal_batch, 573 .maxlen = sizeof(unsigned int), 574 .mode = 0644, 575 .proc_handler = proc_dointvec_minmax, 576 .extra1 = SYSCTL_ONE, 577 }, 578 { 579 .procname = "netdev_unregister_timeout_secs", 580 .data = &netdev_unregister_timeout_secs, 581 .maxlen = sizeof(unsigned int), 582 .mode = 0644, 583 .proc_handler = proc_dointvec_minmax, 584 .extra1 = SYSCTL_ONE, 585 .extra2 = &int_3600, 586 }, 587 { 588 .procname = "skb_defer_max", 589 .data = &sysctl_skb_defer_max, 590 .maxlen = sizeof(unsigned int), 591 .mode = 0644, 592 .proc_handler = proc_dointvec_minmax, 593 .extra1 = SYSCTL_ZERO, 594 }, 595 { } 596}; 597 598static struct ctl_table netns_core_table[] = { 599 { 600 .procname = "somaxconn", 601 .data = &init_net.core.sysctl_somaxconn, 602 .maxlen = sizeof(int), 603 .mode = 0644, 604 .extra1 = SYSCTL_ZERO, 605 .proc_handler = proc_dointvec_minmax 606 }, 607 { 608 .procname = "txrehash", 609 .data = &init_net.core.sysctl_txrehash, 610 .maxlen = sizeof(u8), 611 .mode = 0644, 612 .extra1 = SYSCTL_ZERO, 613 .extra2 = SYSCTL_ONE, 614 .proc_handler = proc_dou8vec_minmax, 615 }, 616 { } 617}; 618 619static int __init fb_tunnels_only_for_init_net_sysctl_setup(char *str) 620{ 621 /* fallback tunnels for initns only */ 622 if (!strncmp(str, "initns", 6)) 623 sysctl_fb_tunnels_only_for_init_net = 1; 624 /* no fallback tunnels anywhere */ 625 else if (!strncmp(str, "none", 4)) 626 sysctl_fb_tunnels_only_for_init_net = 2; 627 628 return 1; 629} 630__setup("fb_tunnels=", fb_tunnels_only_for_init_net_sysctl_setup); 631 632static __net_init int sysctl_core_net_init(struct net *net) 633{ 634 struct ctl_table *tbl, *tmp; 635 636 tbl = netns_core_table; 637 if (!net_eq(net, &init_net)) { 638 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL); 639 if (tbl == NULL) 640 goto err_dup; 641 642 for (tmp = tbl; tmp->procname; tmp++) 643 tmp->data += (char *)net - (char *)&init_net; 644 645 /* Don't export any sysctls to unprivileged users */ 646 if (net->user_ns != &init_user_ns) { 647 tbl[0].procname = NULL; 648 } 649 } 650 651 net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl); 652 if (net->core.sysctl_hdr == NULL) 653 goto err_reg; 654 655 return 0; 656 657err_reg: 658 if (tbl != netns_core_table) 659 kfree(tbl); 660err_dup: 661 return -ENOMEM; 662} 663 664static __net_exit void sysctl_core_net_exit(struct net *net) 665{ 666 struct ctl_table *tbl; 667 668 tbl = net->core.sysctl_hdr->ctl_table_arg; 669 unregister_net_sysctl_table(net->core.sysctl_hdr); 670 BUG_ON(tbl == netns_core_table); 671 kfree(tbl); 672} 673 674static __net_initdata struct pernet_operations sysctl_core_ops = { 675 .init = sysctl_core_net_init, 676 .exit = sysctl_core_net_exit, 677}; 678 679static __init int sysctl_core_init(void) 680{ 681 register_net_sysctl(&init_net, "net/core", net_core_table); 682 return register_pernet_subsys(&sysctl_core_ops); 683} 684 685fs_initcall(sysctl_core_init);