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/mm.h> 10 #include <linux/sysctl.h> 11 #include <linux/module.h> 12 #include <linux/socket.h> 13 #include <linux/netdevice.h> 14 #include <linux/ratelimit.h> 15 #include <linux/vmalloc.h> 16 #include <linux/init.h> 17 #include <linux/slab.h> 18 #include <linux/kmemleak.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 static int zero = 0; 27 static int one = 1; 28 static int two __maybe_unused = 2; 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 33 static int net_msg_warn; /* Unused, but still a sysctl */ 34 35 #ifdef CONFIG_RPS 36 static int rps_sock_flow_sysctl(struct ctl_table *table, int write, 37 void __user *buffer, size_t *lenp, loff_t *ppos) 38 { 39 unsigned int orig_size, size; 40 int ret, i; 41 struct ctl_table tmp = { 42 .data = &size, 43 .maxlen = sizeof(size), 44 .mode = table->mode 45 }; 46 struct rps_sock_flow_table *orig_sock_table, *sock_table; 47 static DEFINE_MUTEX(sock_flow_mutex); 48 49 mutex_lock(&sock_flow_mutex); 50 51 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table, 52 lockdep_is_held(&sock_flow_mutex)); 53 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0; 54 55 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos); 56 57 if (write) { 58 if (size) { 59 if (size > 1<<29) { 60 /* Enforce limit to prevent overflow */ 61 mutex_unlock(&sock_flow_mutex); 62 return -EINVAL; 63 } 64 size = roundup_pow_of_two(size); 65 if (size != orig_size) { 66 sock_table = 67 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size)); 68 if (!sock_table) { 69 mutex_unlock(&sock_flow_mutex); 70 return -ENOMEM; 71 } 72 rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1; 73 sock_table->mask = size - 1; 74 } else 75 sock_table = orig_sock_table; 76 77 for (i = 0; i < size; i++) 78 sock_table->ents[i] = RPS_NO_CPU; 79 } else 80 sock_table = NULL; 81 82 if (sock_table != orig_sock_table) { 83 rcu_assign_pointer(rps_sock_flow_table, sock_table); 84 if (sock_table) { 85 static_key_slow_inc(&rps_needed); 86 static_key_slow_inc(&rfs_needed); 87 } 88 if (orig_sock_table) { 89 static_key_slow_dec(&rps_needed); 90 static_key_slow_dec(&rfs_needed); 91 synchronize_rcu(); 92 vfree(orig_sock_table); 93 } 94 } 95 } 96 97 mutex_unlock(&sock_flow_mutex); 98 99 return ret; 100 } 101 #endif /* CONFIG_RPS */ 102 103 #ifdef CONFIG_NET_FLOW_LIMIT 104 static DEFINE_MUTEX(flow_limit_update_mutex); 105 106 static int flow_limit_cpu_sysctl(struct ctl_table *table, int write, 107 void __user *buffer, size_t *lenp, 108 loff_t *ppos) 109 { 110 struct sd_flow_limit *cur; 111 struct softnet_data *sd; 112 cpumask_var_t mask; 113 int i, len, ret = 0; 114 115 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) 116 return -ENOMEM; 117 118 if (write) { 119 ret = cpumask_parse_user(buffer, *lenp, mask); 120 if (ret) 121 goto done; 122 123 mutex_lock(&flow_limit_update_mutex); 124 len = sizeof(*cur) + netdev_flow_limit_table_len; 125 for_each_possible_cpu(i) { 126 sd = &per_cpu(softnet_data, i); 127 cur = rcu_dereference_protected(sd->flow_limit, 128 lockdep_is_held(&flow_limit_update_mutex)); 129 if (cur && !cpumask_test_cpu(i, mask)) { 130 RCU_INIT_POINTER(sd->flow_limit, NULL); 131 synchronize_rcu(); 132 kfree(cur); 133 } else if (!cur && cpumask_test_cpu(i, mask)) { 134 cur = kzalloc_node(len, GFP_KERNEL, 135 cpu_to_node(i)); 136 if (!cur) { 137 /* not unwinding previous changes */ 138 ret = -ENOMEM; 139 goto write_unlock; 140 } 141 cur->num_buckets = netdev_flow_limit_table_len; 142 rcu_assign_pointer(sd->flow_limit, cur); 143 } 144 } 145 write_unlock: 146 mutex_unlock(&flow_limit_update_mutex); 147 } else { 148 char kbuf[128]; 149 150 if (*ppos || !*lenp) { 151 *lenp = 0; 152 goto done; 153 } 154 155 cpumask_clear(mask); 156 rcu_read_lock(); 157 for_each_possible_cpu(i) { 158 sd = &per_cpu(softnet_data, i); 159 if (rcu_dereference(sd->flow_limit)) 160 cpumask_set_cpu(i, mask); 161 } 162 rcu_read_unlock(); 163 164 len = min(sizeof(kbuf) - 1, *lenp); 165 len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask)); 166 if (!len) { 167 *lenp = 0; 168 goto done; 169 } 170 if (len < *lenp) 171 kbuf[len++] = '\n'; 172 if (copy_to_user(buffer, kbuf, len)) { 173 ret = -EFAULT; 174 goto done; 175 } 176 *lenp = len; 177 *ppos += len; 178 } 179 180 done: 181 free_cpumask_var(mask); 182 return ret; 183 } 184 185 static int flow_limit_table_len_sysctl(struct ctl_table *table, int write, 186 void __user *buffer, size_t *lenp, 187 loff_t *ppos) 188 { 189 unsigned int old, *ptr; 190 int ret; 191 192 mutex_lock(&flow_limit_update_mutex); 193 194 ptr = table->data; 195 old = *ptr; 196 ret = proc_dointvec(table, write, buffer, lenp, ppos); 197 if (!ret && write && !is_power_of_2(*ptr)) { 198 *ptr = old; 199 ret = -EINVAL; 200 } 201 202 mutex_unlock(&flow_limit_update_mutex); 203 return ret; 204 } 205 #endif /* CONFIG_NET_FLOW_LIMIT */ 206 207 #ifdef CONFIG_NET_SCHED 208 static int set_default_qdisc(struct ctl_table *table, int write, 209 void __user *buffer, size_t *lenp, loff_t *ppos) 210 { 211 char id[IFNAMSIZ]; 212 struct ctl_table tbl = { 213 .data = id, 214 .maxlen = IFNAMSIZ, 215 }; 216 int ret; 217 218 qdisc_get_default(id, IFNAMSIZ); 219 220 ret = proc_dostring(&tbl, write, buffer, lenp, ppos); 221 if (write && ret == 0) 222 ret = qdisc_set_default(id); 223 return ret; 224 } 225 #endif 226 227 static int proc_do_dev_weight(struct ctl_table *table, int write, 228 void __user *buffer, size_t *lenp, loff_t *ppos) 229 { 230 int ret; 231 232 ret = proc_dointvec(table, write, buffer, lenp, ppos); 233 if (ret != 0) 234 return ret; 235 236 dev_rx_weight = weight_p * dev_weight_rx_bias; 237 dev_tx_weight = weight_p * dev_weight_tx_bias; 238 239 return ret; 240 } 241 242 static int proc_do_rss_key(struct ctl_table *table, int write, 243 void __user *buffer, size_t *lenp, loff_t *ppos) 244 { 245 struct ctl_table fake_table; 246 char buf[NETDEV_RSS_KEY_LEN * 3]; 247 248 snprintf(buf, sizeof(buf), "%*phC", NETDEV_RSS_KEY_LEN, netdev_rss_key); 249 fake_table.data = buf; 250 fake_table.maxlen = sizeof(buf); 251 return proc_dostring(&fake_table, write, buffer, lenp, ppos); 252 } 253 254 #ifdef CONFIG_BPF_JIT 255 static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write, 256 void __user *buffer, size_t *lenp, 257 loff_t *ppos) 258 { 259 int ret, jit_enable = *(int *)table->data; 260 struct ctl_table tmp = *table; 261 262 if (write && !capable(CAP_SYS_ADMIN)) 263 return -EPERM; 264 265 tmp.data = &jit_enable; 266 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 267 if (write && !ret) { 268 if (jit_enable < 2 || 269 (jit_enable == 2 && bpf_dump_raw_ok())) { 270 *(int *)table->data = jit_enable; 271 if (jit_enable == 2) 272 pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n"); 273 } else { 274 ret = -EPERM; 275 } 276 } 277 return ret; 278 } 279 280 # ifdef CONFIG_HAVE_EBPF_JIT 281 static int 282 proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write, 283 void __user *buffer, size_t *lenp, 284 loff_t *ppos) 285 { 286 if (!capable(CAP_SYS_ADMIN)) 287 return -EPERM; 288 289 return proc_dointvec_minmax(table, write, buffer, lenp, ppos); 290 } 291 # endif 292 #endif 293 294 static struct ctl_table net_core_table[] = { 295 #ifdef CONFIG_NET 296 { 297 .procname = "wmem_max", 298 .data = &sysctl_wmem_max, 299 .maxlen = sizeof(int), 300 .mode = 0644, 301 .proc_handler = proc_dointvec_minmax, 302 .extra1 = &min_sndbuf, 303 }, 304 { 305 .procname = "rmem_max", 306 .data = &sysctl_rmem_max, 307 .maxlen = sizeof(int), 308 .mode = 0644, 309 .proc_handler = proc_dointvec_minmax, 310 .extra1 = &min_rcvbuf, 311 }, 312 { 313 .procname = "wmem_default", 314 .data = &sysctl_wmem_default, 315 .maxlen = sizeof(int), 316 .mode = 0644, 317 .proc_handler = proc_dointvec_minmax, 318 .extra1 = &min_sndbuf, 319 }, 320 { 321 .procname = "rmem_default", 322 .data = &sysctl_rmem_default, 323 .maxlen = sizeof(int), 324 .mode = 0644, 325 .proc_handler = proc_dointvec_minmax, 326 .extra1 = &min_rcvbuf, 327 }, 328 { 329 .procname = "dev_weight", 330 .data = &weight_p, 331 .maxlen = sizeof(int), 332 .mode = 0644, 333 .proc_handler = proc_do_dev_weight, 334 }, 335 { 336 .procname = "dev_weight_rx_bias", 337 .data = &dev_weight_rx_bias, 338 .maxlen = sizeof(int), 339 .mode = 0644, 340 .proc_handler = proc_do_dev_weight, 341 }, 342 { 343 .procname = "dev_weight_tx_bias", 344 .data = &dev_weight_tx_bias, 345 .maxlen = sizeof(int), 346 .mode = 0644, 347 .proc_handler = proc_do_dev_weight, 348 }, 349 { 350 .procname = "netdev_max_backlog", 351 .data = &netdev_max_backlog, 352 .maxlen = sizeof(int), 353 .mode = 0644, 354 .proc_handler = proc_dointvec 355 }, 356 { 357 .procname = "netdev_rss_key", 358 .data = &netdev_rss_key, 359 .maxlen = sizeof(int), 360 .mode = 0444, 361 .proc_handler = proc_do_rss_key, 362 }, 363 #ifdef CONFIG_BPF_JIT 364 { 365 .procname = "bpf_jit_enable", 366 .data = &bpf_jit_enable, 367 .maxlen = sizeof(int), 368 .mode = 0644, 369 .proc_handler = proc_dointvec_minmax_bpf_enable, 370 # ifdef CONFIG_BPF_JIT_ALWAYS_ON 371 .extra1 = &one, 372 .extra2 = &one, 373 # else 374 .extra1 = &zero, 375 .extra2 = &two, 376 # endif 377 }, 378 # ifdef CONFIG_HAVE_EBPF_JIT 379 { 380 .procname = "bpf_jit_harden", 381 .data = &bpf_jit_harden, 382 .maxlen = sizeof(int), 383 .mode = 0600, 384 .proc_handler = proc_dointvec_minmax_bpf_restricted, 385 .extra1 = &zero, 386 .extra2 = &two, 387 }, 388 { 389 .procname = "bpf_jit_kallsyms", 390 .data = &bpf_jit_kallsyms, 391 .maxlen = sizeof(int), 392 .mode = 0600, 393 .proc_handler = proc_dointvec_minmax_bpf_restricted, 394 .extra1 = &zero, 395 .extra2 = &one, 396 }, 397 # endif 398 #endif 399 { 400 .procname = "netdev_tstamp_prequeue", 401 .data = &netdev_tstamp_prequeue, 402 .maxlen = sizeof(int), 403 .mode = 0644, 404 .proc_handler = proc_dointvec 405 }, 406 { 407 .procname = "message_cost", 408 .data = &net_ratelimit_state.interval, 409 .maxlen = sizeof(int), 410 .mode = 0644, 411 .proc_handler = proc_dointvec_jiffies, 412 }, 413 { 414 .procname = "message_burst", 415 .data = &net_ratelimit_state.burst, 416 .maxlen = sizeof(int), 417 .mode = 0644, 418 .proc_handler = proc_dointvec, 419 }, 420 { 421 .procname = "optmem_max", 422 .data = &sysctl_optmem_max, 423 .maxlen = sizeof(int), 424 .mode = 0644, 425 .proc_handler = proc_dointvec 426 }, 427 { 428 .procname = "tstamp_allow_data", 429 .data = &sysctl_tstamp_allow_data, 430 .maxlen = sizeof(int), 431 .mode = 0644, 432 .proc_handler = proc_dointvec_minmax, 433 .extra1 = &zero, 434 .extra2 = &one 435 }, 436 #ifdef CONFIG_RPS 437 { 438 .procname = "rps_sock_flow_entries", 439 .maxlen = sizeof(int), 440 .mode = 0644, 441 .proc_handler = rps_sock_flow_sysctl 442 }, 443 #endif 444 #ifdef CONFIG_NET_FLOW_LIMIT 445 { 446 .procname = "flow_limit_cpu_bitmap", 447 .mode = 0644, 448 .proc_handler = flow_limit_cpu_sysctl 449 }, 450 { 451 .procname = "flow_limit_table_len", 452 .data = &netdev_flow_limit_table_len, 453 .maxlen = sizeof(int), 454 .mode = 0644, 455 .proc_handler = flow_limit_table_len_sysctl 456 }, 457 #endif /* CONFIG_NET_FLOW_LIMIT */ 458 #ifdef CONFIG_NET_RX_BUSY_POLL 459 { 460 .procname = "busy_poll", 461 .data = &sysctl_net_busy_poll, 462 .maxlen = sizeof(unsigned int), 463 .mode = 0644, 464 .proc_handler = proc_dointvec_minmax, 465 .extra1 = &zero, 466 }, 467 { 468 .procname = "busy_read", 469 .data = &sysctl_net_busy_read, 470 .maxlen = sizeof(unsigned int), 471 .mode = 0644, 472 .proc_handler = proc_dointvec_minmax, 473 .extra1 = &zero, 474 }, 475 #endif 476 #ifdef CONFIG_NET_SCHED 477 { 478 .procname = "default_qdisc", 479 .mode = 0644, 480 .maxlen = IFNAMSIZ, 481 .proc_handler = set_default_qdisc 482 }, 483 #endif 484 #endif /* CONFIG_NET */ 485 { 486 .procname = "netdev_budget", 487 .data = &netdev_budget, 488 .maxlen = sizeof(int), 489 .mode = 0644, 490 .proc_handler = proc_dointvec 491 }, 492 { 493 .procname = "warnings", 494 .data = &net_msg_warn, 495 .maxlen = sizeof(int), 496 .mode = 0644, 497 .proc_handler = proc_dointvec 498 }, 499 { 500 .procname = "max_skb_frags", 501 .data = &sysctl_max_skb_frags, 502 .maxlen = sizeof(int), 503 .mode = 0644, 504 .proc_handler = proc_dointvec_minmax, 505 .extra1 = &one, 506 .extra2 = &max_skb_frags, 507 }, 508 { 509 .procname = "netdev_budget_usecs", 510 .data = &netdev_budget_usecs, 511 .maxlen = sizeof(unsigned int), 512 .mode = 0644, 513 .proc_handler = proc_dointvec_minmax, 514 .extra1 = &zero, 515 }, 516 { } 517 }; 518 519 static struct ctl_table netns_core_table[] = { 520 { 521 .procname = "somaxconn", 522 .data = &init_net.core.sysctl_somaxconn, 523 .maxlen = sizeof(int), 524 .mode = 0644, 525 .extra1 = &zero, 526 .proc_handler = proc_dointvec_minmax 527 }, 528 { } 529 }; 530 531 static __net_init int sysctl_core_net_init(struct net *net) 532 { 533 struct ctl_table *tbl; 534 535 tbl = netns_core_table; 536 if (!net_eq(net, &init_net)) { 537 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL); 538 if (tbl == NULL) 539 goto err_dup; 540 541 tbl[0].data = &net->core.sysctl_somaxconn; 542 543 /* Don't export any sysctls to unprivileged users */ 544 if (net->user_ns != &init_user_ns) { 545 tbl[0].procname = NULL; 546 } 547 } 548 549 net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl); 550 if (net->core.sysctl_hdr == NULL) 551 goto err_reg; 552 553 return 0; 554 555 err_reg: 556 if (tbl != netns_core_table) 557 kfree(tbl); 558 err_dup: 559 return -ENOMEM; 560 } 561 562 static __net_exit void sysctl_core_net_exit(struct net *net) 563 { 564 struct ctl_table *tbl; 565 566 tbl = net->core.sysctl_hdr->ctl_table_arg; 567 unregister_net_sysctl_table(net->core.sysctl_hdr); 568 BUG_ON(tbl == netns_core_table); 569 kfree(tbl); 570 } 571 572 static __net_initdata struct pernet_operations sysctl_core_ops = { 573 .init = sysctl_core_net_init, 574 .exit = sysctl_core_net_exit, 575 }; 576 577 static __init int sysctl_core_init(void) 578 { 579 register_net_sysctl(&init_net, "net/core", net_core_table); 580 return register_pernet_subsys(&sysctl_core_ops); 581 } 582 583 fs_initcall(sysctl_core_init); 584