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