1 // SPDX-License-Identifier: GPL-2.0
3 * sysctl_net_core.c: sysctl interface to net core subsystem.
5 * Begun April 1, 1996, Mike Shaver.
6 * Added /proc/sys/net/core directory entry (empty =) ). [MS]
9 #include <linux/filter.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>
22 #include <net/net_ratelimit.h>
23 #include <net/busy_poll.h>
24 #include <net/pkt_sched.h>
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;
34 static int net_msg_warn; /* Unused, but still a sysctl */
36 int sysctl_fb_tunnels_only_for_init_net __read_mostly = 0;
37 EXPORT_SYMBOL(sysctl_fb_tunnels_only_for_init_net);
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
46 int sysctl_devconf_inherit_init_net __read_mostly;
47 EXPORT_SYMBOL(sysctl_devconf_inherit_init_net);
50 static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
51 void *buffer, size_t *lenp, loff_t *ppos)
53 unsigned int orig_size, size;
55 struct ctl_table tmp = {
57 .maxlen = sizeof(size),
60 struct rps_sock_flow_table *orig_sock_table, *sock_table;
61 static DEFINE_MUTEX(sock_flow_mutex);
63 mutex_lock(&sock_flow_mutex);
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;
69 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
74 /* Enforce limit to prevent overflow */
75 mutex_unlock(&sock_flow_mutex);
78 size = roundup_pow_of_two(size);
79 if (size != orig_size) {
81 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
83 mutex_unlock(&sock_flow_mutex);
86 rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1;
87 sock_table->mask = size - 1;
89 sock_table = orig_sock_table;
91 for (i = 0; i < size; i++)
92 sock_table->ents[i] = RPS_NO_CPU;
96 if (sock_table != orig_sock_table) {
97 rcu_assign_pointer(rps_sock_flow_table, sock_table);
99 static_branch_inc(&rps_needed);
100 static_branch_inc(&rfs_needed);
102 if (orig_sock_table) {
103 static_branch_dec(&rps_needed);
104 static_branch_dec(&rfs_needed);
105 kvfree_rcu(orig_sock_table);
110 mutex_unlock(&sock_flow_mutex);
114 #endif /* CONFIG_RPS */
116 #ifdef CONFIG_NET_FLOW_LIMIT
117 static DEFINE_MUTEX(flow_limit_update_mutex);
119 static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
120 void *buffer, size_t *lenp, loff_t *ppos)
122 struct sd_flow_limit *cur;
123 struct softnet_data *sd;
127 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
131 ret = cpumask_parse(buffer, mask);
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);
144 } else if (!cur && cpumask_test_cpu(i, mask)) {
145 cur = kzalloc_node(len, GFP_KERNEL,
148 /* not unwinding previous changes */
152 cur->num_buckets = netdev_flow_limit_table_len;
153 rcu_assign_pointer(sd->flow_limit, cur);
157 mutex_unlock(&flow_limit_update_mutex);
161 if (*ppos || !*lenp) {
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);
175 len = min(sizeof(kbuf) - 1, *lenp);
176 len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask));
183 memcpy(buffer, kbuf, len);
189 free_cpumask_var(mask);
193 static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
194 void *buffer, size_t *lenp, loff_t *ppos)
196 unsigned int old, *ptr;
199 mutex_lock(&flow_limit_update_mutex);
203 ret = proc_dointvec(table, write, buffer, lenp, ppos);
204 if (!ret && write && !is_power_of_2(*ptr)) {
209 mutex_unlock(&flow_limit_update_mutex);
212 #endif /* CONFIG_NET_FLOW_LIMIT */
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)
219 struct ctl_table tbl = {
225 qdisc_get_default(id, IFNAMSIZ);
227 ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
228 if (write && ret == 0)
229 ret = qdisc_set_default(id);
234 static int proc_do_dev_weight(struct ctl_table *table, int write,
235 void *buffer, size_t *lenp, loff_t *ppos)
239 ret = proc_dointvec(table, write, buffer, lenp, ppos);
243 dev_rx_weight = weight_p * dev_weight_rx_bias;
244 dev_tx_weight = weight_p * dev_weight_tx_bias;
249 static int proc_do_rss_key(struct ctl_table *table, int write,
250 void *buffer, size_t *lenp, loff_t *ppos)
252 struct ctl_table fake_table;
253 char buf[NETDEV_RSS_KEY_LEN * 3];
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);
261 #ifdef CONFIG_BPF_JIT
262 static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write,
263 void *buffer, size_t *lenp,
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;
271 if (write && !capable(CAP_SYS_ADMIN))
274 tmp.data = &jit_enable;
275 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
277 if (jit_enable < 2 ||
278 (jit_enable == 2 && bpf_dump_raw_ok(current_cred()))) {
279 *(int *)table->data = jit_enable;
281 pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n");
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");
293 # ifdef CONFIG_HAVE_EBPF_JIT
295 proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write,
296 void *buffer, size_t *lenp, loff_t *ppos)
298 if (!capable(CAP_SYS_ADMIN))
301 return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
303 # endif /* CONFIG_HAVE_EBPF_JIT */
306 proc_dolongvec_minmax_bpf_restricted(struct ctl_table *table, int write,
307 void *buffer, size_t *lenp, loff_t *ppos)
309 if (!capable(CAP_SYS_ADMIN))
312 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
316 static struct ctl_table net_core_table[] = {
318 .procname = "wmem_max",
319 .data = &sysctl_wmem_max,
320 .maxlen = sizeof(int),
322 .proc_handler = proc_dointvec_minmax,
323 .extra1 = &min_sndbuf,
326 .procname = "rmem_max",
327 .data = &sysctl_rmem_max,
328 .maxlen = sizeof(int),
330 .proc_handler = proc_dointvec_minmax,
331 .extra1 = &min_rcvbuf,
334 .procname = "wmem_default",
335 .data = &sysctl_wmem_default,
336 .maxlen = sizeof(int),
338 .proc_handler = proc_dointvec_minmax,
339 .extra1 = &min_sndbuf,
342 .procname = "rmem_default",
343 .data = &sysctl_rmem_default,
344 .maxlen = sizeof(int),
346 .proc_handler = proc_dointvec_minmax,
347 .extra1 = &min_rcvbuf,
350 .procname = "dev_weight",
352 .maxlen = sizeof(int),
354 .proc_handler = proc_do_dev_weight,
357 .procname = "dev_weight_rx_bias",
358 .data = &dev_weight_rx_bias,
359 .maxlen = sizeof(int),
361 .proc_handler = proc_do_dev_weight,
364 .procname = "dev_weight_tx_bias",
365 .data = &dev_weight_tx_bias,
366 .maxlen = sizeof(int),
368 .proc_handler = proc_do_dev_weight,
371 .procname = "netdev_max_backlog",
372 .data = &netdev_max_backlog,
373 .maxlen = sizeof(int),
375 .proc_handler = proc_dointvec
378 .procname = "netdev_rss_key",
379 .data = &netdev_rss_key,
380 .maxlen = sizeof(int),
382 .proc_handler = proc_do_rss_key,
384 #ifdef CONFIG_BPF_JIT
386 .procname = "bpf_jit_enable",
387 .data = &bpf_jit_enable,
388 .maxlen = sizeof(int),
390 .proc_handler = proc_dointvec_minmax_bpf_enable,
391 # ifdef CONFIG_BPF_JIT_ALWAYS_ON
392 .extra1 = SYSCTL_ONE,
393 .extra2 = SYSCTL_ONE,
395 .extra1 = SYSCTL_ZERO,
396 .extra2 = SYSCTL_TWO,
399 # ifdef CONFIG_HAVE_EBPF_JIT
401 .procname = "bpf_jit_harden",
402 .data = &bpf_jit_harden,
403 .maxlen = sizeof(int),
405 .proc_handler = proc_dointvec_minmax_bpf_restricted,
406 .extra1 = SYSCTL_ZERO,
407 .extra2 = SYSCTL_TWO,
410 .procname = "bpf_jit_kallsyms",
411 .data = &bpf_jit_kallsyms,
412 .maxlen = sizeof(int),
414 .proc_handler = proc_dointvec_minmax_bpf_restricted,
415 .extra1 = SYSCTL_ZERO,
416 .extra2 = SYSCTL_ONE,
420 .procname = "bpf_jit_limit",
421 .data = &bpf_jit_limit,
422 .maxlen = sizeof(long),
424 .proc_handler = proc_dolongvec_minmax_bpf_restricted,
425 .extra1 = SYSCTL_LONG_ONE,
426 .extra2 = &bpf_jit_limit_max,
430 .procname = "netdev_tstamp_prequeue",
431 .data = &netdev_tstamp_prequeue,
432 .maxlen = sizeof(int),
434 .proc_handler = proc_dointvec
437 .procname = "message_cost",
438 .data = &net_ratelimit_state.interval,
439 .maxlen = sizeof(int),
441 .proc_handler = proc_dointvec_jiffies,
444 .procname = "message_burst",
445 .data = &net_ratelimit_state.burst,
446 .maxlen = sizeof(int),
448 .proc_handler = proc_dointvec,
451 .procname = "optmem_max",
452 .data = &sysctl_optmem_max,
453 .maxlen = sizeof(int),
455 .proc_handler = proc_dointvec
458 .procname = "tstamp_allow_data",
459 .data = &sysctl_tstamp_allow_data,
460 .maxlen = sizeof(int),
462 .proc_handler = proc_dointvec_minmax,
463 .extra1 = SYSCTL_ZERO,
468 .procname = "rps_sock_flow_entries",
469 .maxlen = sizeof(int),
471 .proc_handler = rps_sock_flow_sysctl
474 #ifdef CONFIG_NET_FLOW_LIMIT
476 .procname = "flow_limit_cpu_bitmap",
478 .proc_handler = flow_limit_cpu_sysctl
481 .procname = "flow_limit_table_len",
482 .data = &netdev_flow_limit_table_len,
483 .maxlen = sizeof(int),
485 .proc_handler = flow_limit_table_len_sysctl
487 #endif /* CONFIG_NET_FLOW_LIMIT */
488 #ifdef CONFIG_NET_RX_BUSY_POLL
490 .procname = "busy_poll",
491 .data = &sysctl_net_busy_poll,
492 .maxlen = sizeof(unsigned int),
494 .proc_handler = proc_dointvec_minmax,
495 .extra1 = SYSCTL_ZERO,
498 .procname = "busy_read",
499 .data = &sysctl_net_busy_read,
500 .maxlen = sizeof(unsigned int),
502 .proc_handler = proc_dointvec_minmax,
503 .extra1 = SYSCTL_ZERO,
506 #ifdef CONFIG_NET_SCHED
508 .procname = "default_qdisc",
511 .proc_handler = set_default_qdisc
515 .procname = "netdev_budget",
516 .data = &netdev_budget,
517 .maxlen = sizeof(int),
519 .proc_handler = proc_dointvec
522 .procname = "warnings",
523 .data = &net_msg_warn,
524 .maxlen = sizeof(int),
526 .proc_handler = proc_dointvec
529 .procname = "max_skb_frags",
530 .data = &sysctl_max_skb_frags,
531 .maxlen = sizeof(int),
533 .proc_handler = proc_dointvec_minmax,
534 .extra1 = SYSCTL_ONE,
535 .extra2 = &max_skb_frags,
538 .procname = "netdev_budget_usecs",
539 .data = &netdev_budget_usecs,
540 .maxlen = sizeof(unsigned int),
542 .proc_handler = proc_dointvec_minmax,
543 .extra1 = SYSCTL_ZERO,
546 .procname = "fb_tunnels_only_for_init_net",
547 .data = &sysctl_fb_tunnels_only_for_init_net,
548 .maxlen = sizeof(int),
550 .proc_handler = proc_dointvec_minmax,
551 .extra1 = SYSCTL_ZERO,
552 .extra2 = SYSCTL_TWO,
555 .procname = "devconf_inherit_init_net",
556 .data = &sysctl_devconf_inherit_init_net,
557 .maxlen = sizeof(int),
559 .proc_handler = proc_dointvec_minmax,
560 .extra1 = SYSCTL_ZERO,
561 .extra2 = SYSCTL_THREE,
564 .procname = "high_order_alloc_disable",
565 .data = &net_high_order_alloc_disable_key.key,
566 .maxlen = sizeof(net_high_order_alloc_disable_key),
568 .proc_handler = proc_do_static_key,
571 .procname = "gro_normal_batch",
572 .data = &gro_normal_batch,
573 .maxlen = sizeof(unsigned int),
575 .proc_handler = proc_dointvec_minmax,
576 .extra1 = SYSCTL_ONE,
579 .procname = "netdev_unregister_timeout_secs",
580 .data = &netdev_unregister_timeout_secs,
581 .maxlen = sizeof(unsigned int),
583 .proc_handler = proc_dointvec_minmax,
584 .extra1 = SYSCTL_ONE,
588 .procname = "skb_defer_max",
589 .data = &sysctl_skb_defer_max,
590 .maxlen = sizeof(unsigned int),
592 .proc_handler = proc_dointvec_minmax,
593 .extra1 = SYSCTL_ZERO,
598 static struct ctl_table netns_core_table[] = {
600 .procname = "somaxconn",
601 .data = &init_net.core.sysctl_somaxconn,
602 .maxlen = sizeof(int),
604 .extra1 = SYSCTL_ZERO,
605 .proc_handler = proc_dointvec_minmax
608 .procname = "txrehash",
609 .data = &init_net.core.sysctl_txrehash,
610 .maxlen = sizeof(u8),
612 .extra1 = SYSCTL_ZERO,
613 .extra2 = SYSCTL_ONE,
614 .proc_handler = proc_dou8vec_minmax,
619 static int __init fb_tunnels_only_for_init_net_sysctl_setup(char *str)
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;
630 __setup("fb_tunnels=", fb_tunnels_only_for_init_net_sysctl_setup);
632 static __net_init int sysctl_core_net_init(struct net *net)
634 struct ctl_table *tbl, *tmp;
636 tbl = netns_core_table;
637 if (!net_eq(net, &init_net)) {
638 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
642 for (tmp = tbl; tmp->procname; tmp++)
643 tmp->data += (char *)net - (char *)&init_net;
645 /* Don't export any sysctls to unprivileged users */
646 if (net->user_ns != &init_user_ns) {
647 tbl[0].procname = NULL;
651 net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
652 if (net->core.sysctl_hdr == NULL)
658 if (tbl != netns_core_table)
664 static __net_exit void sysctl_core_net_exit(struct net *net)
666 struct ctl_table *tbl;
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);
674 static __net_initdata struct pernet_operations sysctl_core_ops = {
675 .init = sysctl_core_net_init,
676 .exit = sysctl_core_net_exit,
679 static __init int sysctl_core_init(void)
681 register_net_sysctl(&init_net, "net/core", net_core_table);
682 return register_pernet_subsys(&sysctl_core_ops);
685 fs_initcall(sysctl_core_init);