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]
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>
21 #include <net/net_ratelimit.h>
22 #include <net/busy_poll.h>
23 #include <net/pkt_sched.h>
27 static int min_sndbuf
= SOCK_MIN_SNDBUF
;
28 static int min_rcvbuf
= SOCK_MIN_RCVBUF
;
29 static int max_skb_frags
= MAX_SKB_FRAGS
;
30 static long long_one __maybe_unused
= 1;
31 static long long_max __maybe_unused
= LONG_MAX
;
33 static int net_msg_warn
; /* Unused, but still a sysctl */
35 int sysctl_fb_tunnels_only_for_init_net __read_mostly
= 0;
36 EXPORT_SYMBOL(sysctl_fb_tunnels_only_for_init_net
);
38 /* 0 - Keep current behavior:
39 * IPv4: inherit all current settings from init_net
40 * IPv6: reset all settings to default
41 * 1 - Both inherit all current settings from init_net
42 * 2 - Both reset all settings to default
43 * 3 - Both inherit all settings from current netns
45 int sysctl_devconf_inherit_init_net __read_mostly
;
46 EXPORT_SYMBOL(sysctl_devconf_inherit_init_net
);
49 static int rps_sock_flow_sysctl(struct ctl_table
*table
, int write
,
50 void *buffer
, size_t *lenp
, loff_t
*ppos
)
52 unsigned int orig_size
, size
;
54 struct ctl_table tmp
= {
56 .maxlen
= sizeof(size
),
59 struct rps_sock_flow_table
*orig_sock_table
, *sock_table
;
60 static DEFINE_MUTEX(sock_flow_mutex
);
62 mutex_lock(&sock_flow_mutex
);
64 orig_sock_table
= rcu_dereference_protected(rps_sock_flow_table
,
65 lockdep_is_held(&sock_flow_mutex
));
66 size
= orig_size
= orig_sock_table
? orig_sock_table
->mask
+ 1 : 0;
68 ret
= proc_dointvec(&tmp
, write
, buffer
, lenp
, ppos
);
73 /* Enforce limit to prevent overflow */
74 mutex_unlock(&sock_flow_mutex
);
77 size
= roundup_pow_of_two(size
);
78 if (size
!= orig_size
) {
80 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size
));
82 mutex_unlock(&sock_flow_mutex
);
85 rps_cpu_mask
= roundup_pow_of_two(nr_cpu_ids
) - 1;
86 sock_table
->mask
= size
- 1;
88 sock_table
= orig_sock_table
;
90 for (i
= 0; i
< size
; i
++)
91 sock_table
->ents
[i
] = RPS_NO_CPU
;
95 if (sock_table
!= orig_sock_table
) {
96 rcu_assign_pointer(rps_sock_flow_table
, sock_table
);
98 static_branch_inc(&rps_needed
);
99 static_branch_inc(&rfs_needed
);
101 if (orig_sock_table
) {
102 static_branch_dec(&rps_needed
);
103 static_branch_dec(&rfs_needed
);
105 vfree(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
);
145 } else if (!cur
&& cpumask_test_cpu(i
, mask
)) {
146 cur
= kzalloc_node(len
, GFP_KERNEL
,
149 /* not unwinding previous changes */
153 cur
->num_buckets
= netdev_flow_limit_table_len
;
154 rcu_assign_pointer(sd
->flow_limit
, cur
);
158 mutex_unlock(&flow_limit_update_mutex
);
162 if (*ppos
|| !*lenp
) {
169 for_each_possible_cpu(i
) {
170 sd
= &per_cpu(softnet_data
, i
);
171 if (rcu_dereference(sd
->flow_limit
))
172 cpumask_set_cpu(i
, mask
);
176 len
= min(sizeof(kbuf
) - 1, *lenp
);
177 len
= scnprintf(kbuf
, len
, "%*pb", cpumask_pr_args(mask
));
184 memcpy(buffer
, kbuf
, len
);
190 free_cpumask_var(mask
);
194 static int flow_limit_table_len_sysctl(struct ctl_table
*table
, int write
,
195 void *buffer
, size_t *lenp
, loff_t
*ppos
)
197 unsigned int old
, *ptr
;
200 mutex_lock(&flow_limit_update_mutex
);
204 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
205 if (!ret
&& write
&& !is_power_of_2(*ptr
)) {
210 mutex_unlock(&flow_limit_update_mutex
);
213 #endif /* CONFIG_NET_FLOW_LIMIT */
215 #ifdef CONFIG_NET_SCHED
216 static int set_default_qdisc(struct ctl_table
*table
, int write
,
217 void *buffer
, size_t *lenp
, loff_t
*ppos
)
220 struct ctl_table tbl
= {
226 qdisc_get_default(id
, IFNAMSIZ
);
228 ret
= proc_dostring(&tbl
, write
, buffer
, lenp
, ppos
);
229 if (write
&& ret
== 0)
230 ret
= qdisc_set_default(id
);
235 static int proc_do_dev_weight(struct ctl_table
*table
, int write
,
236 void *buffer
, size_t *lenp
, loff_t
*ppos
)
240 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
244 dev_rx_weight
= weight_p
* dev_weight_rx_bias
;
245 dev_tx_weight
= weight_p
* dev_weight_tx_bias
;
250 static int proc_do_rss_key(struct ctl_table
*table
, int write
,
251 void *buffer
, size_t *lenp
, loff_t
*ppos
)
253 struct ctl_table fake_table
;
254 char buf
[NETDEV_RSS_KEY_LEN
* 3];
256 snprintf(buf
, sizeof(buf
), "%*phC", NETDEV_RSS_KEY_LEN
, netdev_rss_key
);
257 fake_table
.data
= buf
;
258 fake_table
.maxlen
= sizeof(buf
);
259 return proc_dostring(&fake_table
, write
, buffer
, lenp
, ppos
);
262 #ifdef CONFIG_BPF_JIT
263 static int proc_dointvec_minmax_bpf_enable(struct ctl_table
*table
, int write
,
264 void *buffer
, size_t *lenp
,
267 int ret
, jit_enable
= *(int *)table
->data
;
268 struct ctl_table tmp
= *table
;
270 if (write
&& !capable(CAP_SYS_ADMIN
))
273 tmp
.data
= &jit_enable
;
274 ret
= proc_dointvec_minmax(&tmp
, write
, buffer
, lenp
, ppos
);
276 if (jit_enable
< 2 ||
277 (jit_enable
== 2 && bpf_dump_raw_ok(current_cred()))) {
278 *(int *)table
->data
= jit_enable
;
280 pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n");
288 # ifdef CONFIG_HAVE_EBPF_JIT
290 proc_dointvec_minmax_bpf_restricted(struct ctl_table
*table
, int write
,
291 void *buffer
, size_t *lenp
, loff_t
*ppos
)
293 if (!capable(CAP_SYS_ADMIN
))
296 return proc_dointvec_minmax(table
, write
, buffer
, lenp
, ppos
);
298 # endif /* CONFIG_HAVE_EBPF_JIT */
301 proc_dolongvec_minmax_bpf_restricted(struct ctl_table
*table
, int write
,
302 void *buffer
, size_t *lenp
, loff_t
*ppos
)
304 if (!capable(CAP_SYS_ADMIN
))
307 return proc_doulongvec_minmax(table
, write
, buffer
, lenp
, ppos
);
311 static struct ctl_table net_core_table
[] = {
314 .procname
= "wmem_max",
315 .data
= &sysctl_wmem_max
,
316 .maxlen
= sizeof(int),
318 .proc_handler
= proc_dointvec_minmax
,
319 .extra1
= &min_sndbuf
,
322 .procname
= "rmem_max",
323 .data
= &sysctl_rmem_max
,
324 .maxlen
= sizeof(int),
326 .proc_handler
= proc_dointvec_minmax
,
327 .extra1
= &min_rcvbuf
,
330 .procname
= "wmem_default",
331 .data
= &sysctl_wmem_default
,
332 .maxlen
= sizeof(int),
334 .proc_handler
= proc_dointvec_minmax
,
335 .extra1
= &min_sndbuf
,
338 .procname
= "rmem_default",
339 .data
= &sysctl_rmem_default
,
340 .maxlen
= sizeof(int),
342 .proc_handler
= proc_dointvec_minmax
,
343 .extra1
= &min_rcvbuf
,
346 .procname
= "dev_weight",
348 .maxlen
= sizeof(int),
350 .proc_handler
= proc_do_dev_weight
,
353 .procname
= "dev_weight_rx_bias",
354 .data
= &dev_weight_rx_bias
,
355 .maxlen
= sizeof(int),
357 .proc_handler
= proc_do_dev_weight
,
360 .procname
= "dev_weight_tx_bias",
361 .data
= &dev_weight_tx_bias
,
362 .maxlen
= sizeof(int),
364 .proc_handler
= proc_do_dev_weight
,
367 .procname
= "netdev_max_backlog",
368 .data
= &netdev_max_backlog
,
369 .maxlen
= sizeof(int),
371 .proc_handler
= proc_dointvec
374 .procname
= "netdev_rss_key",
375 .data
= &netdev_rss_key
,
376 .maxlen
= sizeof(int),
378 .proc_handler
= proc_do_rss_key
,
380 #ifdef CONFIG_BPF_JIT
382 .procname
= "bpf_jit_enable",
383 .data
= &bpf_jit_enable
,
384 .maxlen
= sizeof(int),
386 .proc_handler
= proc_dointvec_minmax_bpf_enable
,
387 # ifdef CONFIG_BPF_JIT_ALWAYS_ON
388 .extra1
= SYSCTL_ONE
,
389 .extra2
= SYSCTL_ONE
,
391 .extra1
= SYSCTL_ZERO
,
395 # ifdef CONFIG_HAVE_EBPF_JIT
397 .procname
= "bpf_jit_harden",
398 .data
= &bpf_jit_harden
,
399 .maxlen
= sizeof(int),
401 .proc_handler
= proc_dointvec_minmax_bpf_restricted
,
402 .extra1
= SYSCTL_ZERO
,
406 .procname
= "bpf_jit_kallsyms",
407 .data
= &bpf_jit_kallsyms
,
408 .maxlen
= sizeof(int),
410 .proc_handler
= proc_dointvec_minmax_bpf_restricted
,
411 .extra1
= SYSCTL_ZERO
,
412 .extra2
= SYSCTL_ONE
,
416 .procname
= "bpf_jit_limit",
417 .data
= &bpf_jit_limit
,
418 .maxlen
= sizeof(long),
420 .proc_handler
= proc_dolongvec_minmax_bpf_restricted
,
426 .procname
= "netdev_tstamp_prequeue",
427 .data
= &netdev_tstamp_prequeue
,
428 .maxlen
= sizeof(int),
430 .proc_handler
= proc_dointvec
433 .procname
= "message_cost",
434 .data
= &net_ratelimit_state
.interval
,
435 .maxlen
= sizeof(int),
437 .proc_handler
= proc_dointvec_jiffies
,
440 .procname
= "message_burst",
441 .data
= &net_ratelimit_state
.burst
,
442 .maxlen
= sizeof(int),
444 .proc_handler
= proc_dointvec
,
447 .procname
= "optmem_max",
448 .data
= &sysctl_optmem_max
,
449 .maxlen
= sizeof(int),
451 .proc_handler
= proc_dointvec
454 .procname
= "tstamp_allow_data",
455 .data
= &sysctl_tstamp_allow_data
,
456 .maxlen
= sizeof(int),
458 .proc_handler
= proc_dointvec_minmax
,
459 .extra1
= SYSCTL_ZERO
,
464 .procname
= "rps_sock_flow_entries",
465 .maxlen
= sizeof(int),
467 .proc_handler
= rps_sock_flow_sysctl
470 #ifdef CONFIG_NET_FLOW_LIMIT
472 .procname
= "flow_limit_cpu_bitmap",
474 .proc_handler
= flow_limit_cpu_sysctl
477 .procname
= "flow_limit_table_len",
478 .data
= &netdev_flow_limit_table_len
,
479 .maxlen
= sizeof(int),
481 .proc_handler
= flow_limit_table_len_sysctl
483 #endif /* CONFIG_NET_FLOW_LIMIT */
484 #ifdef CONFIG_NET_RX_BUSY_POLL
486 .procname
= "busy_poll",
487 .data
= &sysctl_net_busy_poll
,
488 .maxlen
= sizeof(unsigned int),
490 .proc_handler
= proc_dointvec_minmax
,
491 .extra1
= SYSCTL_ZERO
,
494 .procname
= "busy_read",
495 .data
= &sysctl_net_busy_read
,
496 .maxlen
= sizeof(unsigned int),
498 .proc_handler
= proc_dointvec_minmax
,
499 .extra1
= SYSCTL_ZERO
,
502 #ifdef CONFIG_NET_SCHED
504 .procname
= "default_qdisc",
507 .proc_handler
= set_default_qdisc
510 #endif /* CONFIG_NET */
512 .procname
= "netdev_budget",
513 .data
= &netdev_budget
,
514 .maxlen
= sizeof(int),
516 .proc_handler
= proc_dointvec
519 .procname
= "warnings",
520 .data
= &net_msg_warn
,
521 .maxlen
= sizeof(int),
523 .proc_handler
= proc_dointvec
526 .procname
= "max_skb_frags",
527 .data
= &sysctl_max_skb_frags
,
528 .maxlen
= sizeof(int),
530 .proc_handler
= proc_dointvec_minmax
,
531 .extra1
= SYSCTL_ONE
,
532 .extra2
= &max_skb_frags
,
535 .procname
= "netdev_budget_usecs",
536 .data
= &netdev_budget_usecs
,
537 .maxlen
= sizeof(unsigned int),
539 .proc_handler
= proc_dointvec_minmax
,
540 .extra1
= SYSCTL_ZERO
,
543 .procname
= "fb_tunnels_only_for_init_net",
544 .data
= &sysctl_fb_tunnels_only_for_init_net
,
545 .maxlen
= sizeof(int),
547 .proc_handler
= proc_dointvec_minmax
,
548 .extra1
= SYSCTL_ZERO
,
552 .procname
= "devconf_inherit_init_net",
553 .data
= &sysctl_devconf_inherit_init_net
,
554 .maxlen
= sizeof(int),
556 .proc_handler
= proc_dointvec_minmax
,
557 .extra1
= SYSCTL_ZERO
,
561 .procname
= "high_order_alloc_disable",
562 .data
= &net_high_order_alloc_disable_key
.key
,
563 .maxlen
= sizeof(net_high_order_alloc_disable_key
),
565 .proc_handler
= proc_do_static_key
,
568 .procname
= "gro_normal_batch",
569 .data
= &gro_normal_batch
,
570 .maxlen
= sizeof(unsigned int),
572 .proc_handler
= proc_dointvec_minmax
,
573 .extra1
= SYSCTL_ONE
,
578 static struct ctl_table netns_core_table
[] = {
580 .procname
= "somaxconn",
581 .data
= &init_net
.core
.sysctl_somaxconn
,
582 .maxlen
= sizeof(int),
584 .extra1
= SYSCTL_ZERO
,
585 .proc_handler
= proc_dointvec_minmax
590 static int __init
fb_tunnels_only_for_init_net_sysctl_setup(char *str
)
592 /* fallback tunnels for initns only */
593 if (!strncmp(str
, "initns", 6))
594 sysctl_fb_tunnels_only_for_init_net
= 1;
595 /* no fallback tunnels anywhere */
596 else if (!strncmp(str
, "none", 4))
597 sysctl_fb_tunnels_only_for_init_net
= 2;
601 __setup("fb_tunnels=", fb_tunnels_only_for_init_net_sysctl_setup
);
603 static __net_init
int sysctl_core_net_init(struct net
*net
)
605 struct ctl_table
*tbl
;
607 tbl
= netns_core_table
;
608 if (!net_eq(net
, &init_net
)) {
609 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
613 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
615 /* Don't export any sysctls to unprivileged users */
616 if (net
->user_ns
!= &init_user_ns
) {
617 tbl
[0].procname
= NULL
;
621 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
622 if (net
->core
.sysctl_hdr
== NULL
)
628 if (tbl
!= netns_core_table
)
634 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
636 struct ctl_table
*tbl
;
638 tbl
= net
->core
.sysctl_hdr
->ctl_table_arg
;
639 unregister_net_sysctl_table(net
->core
.sysctl_hdr
);
640 BUG_ON(tbl
== netns_core_table
);
644 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
645 .init
= sysctl_core_net_init
,
646 .exit
= sysctl_core_net_exit
,
649 static __init
int sysctl_core_init(void)
651 register_net_sysctl(&init_net
, "net/core", net_core_table
);
652 return register_pernet_subsys(&sysctl_core_ops
);
655 fs_initcall(sysctl_core_init
);