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 two __maybe_unused
= 2;
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 static long long_one __maybe_unused
= 1;
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
);
40 static int rps_sock_flow_sysctl(struct ctl_table
*table
, int write
,
41 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
43 unsigned int orig_size
, size
;
45 struct ctl_table tmp
= {
47 .maxlen
= sizeof(size
),
50 struct rps_sock_flow_table
*orig_sock_table
, *sock_table
;
51 static DEFINE_MUTEX(sock_flow_mutex
);
53 mutex_lock(&sock_flow_mutex
);
55 orig_sock_table
= rcu_dereference_protected(rps_sock_flow_table
,
56 lockdep_is_held(&sock_flow_mutex
));
57 size
= orig_size
= orig_sock_table
? orig_sock_table
->mask
+ 1 : 0;
59 ret
= proc_dointvec(&tmp
, write
, buffer
, lenp
, ppos
);
64 /* Enforce limit to prevent overflow */
65 mutex_unlock(&sock_flow_mutex
);
68 size
= roundup_pow_of_two(size
);
69 if (size
!= orig_size
) {
71 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size
));
73 mutex_unlock(&sock_flow_mutex
);
76 rps_cpu_mask
= roundup_pow_of_two(nr_cpu_ids
) - 1;
77 sock_table
->mask
= size
- 1;
79 sock_table
= orig_sock_table
;
81 for (i
= 0; i
< size
; i
++)
82 sock_table
->ents
[i
] = RPS_NO_CPU
;
86 if (sock_table
!= orig_sock_table
) {
87 rcu_assign_pointer(rps_sock_flow_table
, sock_table
);
89 static_key_slow_inc(&rps_needed
);
90 static_key_slow_inc(&rfs_needed
);
92 if (orig_sock_table
) {
93 static_key_slow_dec(&rps_needed
);
94 static_key_slow_dec(&rfs_needed
);
96 vfree(orig_sock_table
);
101 mutex_unlock(&sock_flow_mutex
);
105 #endif /* CONFIG_RPS */
107 #ifdef CONFIG_NET_FLOW_LIMIT
108 static DEFINE_MUTEX(flow_limit_update_mutex
);
110 static int flow_limit_cpu_sysctl(struct ctl_table
*table
, int write
,
111 void __user
*buffer
, size_t *lenp
,
114 struct sd_flow_limit
*cur
;
115 struct softnet_data
*sd
;
119 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
123 ret
= cpumask_parse_user(buffer
, *lenp
, mask
);
127 mutex_lock(&flow_limit_update_mutex
);
128 len
= sizeof(*cur
) + netdev_flow_limit_table_len
;
129 for_each_possible_cpu(i
) {
130 sd
= &per_cpu(softnet_data
, i
);
131 cur
= rcu_dereference_protected(sd
->flow_limit
,
132 lockdep_is_held(&flow_limit_update_mutex
));
133 if (cur
&& !cpumask_test_cpu(i
, mask
)) {
134 RCU_INIT_POINTER(sd
->flow_limit
, NULL
);
137 } else if (!cur
&& cpumask_test_cpu(i
, mask
)) {
138 cur
= kzalloc_node(len
, GFP_KERNEL
,
141 /* not unwinding previous changes */
145 cur
->num_buckets
= netdev_flow_limit_table_len
;
146 rcu_assign_pointer(sd
->flow_limit
, cur
);
150 mutex_unlock(&flow_limit_update_mutex
);
154 if (*ppos
|| !*lenp
) {
161 for_each_possible_cpu(i
) {
162 sd
= &per_cpu(softnet_data
, i
);
163 if (rcu_dereference(sd
->flow_limit
))
164 cpumask_set_cpu(i
, mask
);
168 len
= min(sizeof(kbuf
) - 1, *lenp
);
169 len
= scnprintf(kbuf
, len
, "%*pb", cpumask_pr_args(mask
));
176 if (copy_to_user(buffer
, kbuf
, len
)) {
185 free_cpumask_var(mask
);
189 static int flow_limit_table_len_sysctl(struct ctl_table
*table
, int write
,
190 void __user
*buffer
, size_t *lenp
,
193 unsigned int old
, *ptr
;
196 mutex_lock(&flow_limit_update_mutex
);
200 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
201 if (!ret
&& write
&& !is_power_of_2(*ptr
)) {
206 mutex_unlock(&flow_limit_update_mutex
);
209 #endif /* CONFIG_NET_FLOW_LIMIT */
211 #ifdef CONFIG_NET_SCHED
212 static int set_default_qdisc(struct ctl_table
*table
, int write
,
213 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
216 struct ctl_table tbl
= {
222 qdisc_get_default(id
, IFNAMSIZ
);
224 ret
= proc_dostring(&tbl
, write
, buffer
, lenp
, ppos
);
225 if (write
&& ret
== 0)
226 ret
= qdisc_set_default(id
);
231 static int proc_do_dev_weight(struct ctl_table
*table
, int write
,
232 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
236 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
240 dev_rx_weight
= weight_p
* dev_weight_rx_bias
;
241 dev_tx_weight
= weight_p
* dev_weight_tx_bias
;
246 static int proc_do_rss_key(struct ctl_table
*table
, int write
,
247 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
249 struct ctl_table fake_table
;
250 char buf
[NETDEV_RSS_KEY_LEN
* 3];
252 snprintf(buf
, sizeof(buf
), "%*phC", NETDEV_RSS_KEY_LEN
, netdev_rss_key
);
253 fake_table
.data
= buf
;
254 fake_table
.maxlen
= sizeof(buf
);
255 return proc_dostring(&fake_table
, write
, buffer
, lenp
, ppos
);
258 #ifdef CONFIG_BPF_JIT
259 static int proc_dointvec_minmax_bpf_enable(struct ctl_table
*table
, int write
,
260 void __user
*buffer
, size_t *lenp
,
263 int ret
, jit_enable
= *(int *)table
->data
;
264 struct ctl_table tmp
= *table
;
266 if (write
&& !capable(CAP_SYS_ADMIN
))
269 tmp
.data
= &jit_enable
;
270 ret
= proc_dointvec_minmax(&tmp
, write
, buffer
, lenp
, ppos
);
272 if (jit_enable
< 2 ||
273 (jit_enable
== 2 && bpf_dump_raw_ok(current_cred()))) {
274 *(int *)table
->data
= jit_enable
;
276 pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n");
284 # ifdef CONFIG_HAVE_EBPF_JIT
286 proc_dointvec_minmax_bpf_restricted(struct ctl_table
*table
, int write
,
287 void __user
*buffer
, size_t *lenp
,
290 if (!capable(CAP_SYS_ADMIN
))
293 return proc_dointvec_minmax(table
, write
, buffer
, lenp
, ppos
);
295 # endif /* CONFIG_HAVE_EBPF_JIT */
298 proc_dolongvec_minmax_bpf_restricted(struct ctl_table
*table
, int write
,
299 void __user
*buffer
, size_t *lenp
,
302 if (!capable(CAP_SYS_ADMIN
))
305 return proc_doulongvec_minmax(table
, write
, buffer
, lenp
, ppos
);
309 static struct ctl_table net_core_table
[] = {
312 .procname
= "wmem_max",
313 .data
= &sysctl_wmem_max
,
314 .maxlen
= sizeof(int),
316 .proc_handler
= proc_dointvec_minmax
,
317 .extra1
= &min_sndbuf
,
320 .procname
= "rmem_max",
321 .data
= &sysctl_rmem_max
,
322 .maxlen
= sizeof(int),
324 .proc_handler
= proc_dointvec_minmax
,
325 .extra1
= &min_rcvbuf
,
328 .procname
= "wmem_default",
329 .data
= &sysctl_wmem_default
,
330 .maxlen
= sizeof(int),
332 .proc_handler
= proc_dointvec_minmax
,
333 .extra1
= &min_sndbuf
,
336 .procname
= "rmem_default",
337 .data
= &sysctl_rmem_default
,
338 .maxlen
= sizeof(int),
340 .proc_handler
= proc_dointvec_minmax
,
341 .extra1
= &min_rcvbuf
,
344 .procname
= "dev_weight",
346 .maxlen
= sizeof(int),
348 .proc_handler
= proc_do_dev_weight
,
351 .procname
= "dev_weight_rx_bias",
352 .data
= &dev_weight_rx_bias
,
353 .maxlen
= sizeof(int),
355 .proc_handler
= proc_do_dev_weight
,
358 .procname
= "dev_weight_tx_bias",
359 .data
= &dev_weight_tx_bias
,
360 .maxlen
= sizeof(int),
362 .proc_handler
= proc_do_dev_weight
,
365 .procname
= "netdev_max_backlog",
366 .data
= &netdev_max_backlog
,
367 .maxlen
= sizeof(int),
369 .proc_handler
= proc_dointvec
372 .procname
= "netdev_rss_key",
373 .data
= &netdev_rss_key
,
374 .maxlen
= sizeof(int),
376 .proc_handler
= proc_do_rss_key
,
378 #ifdef CONFIG_BPF_JIT
380 .procname
= "bpf_jit_enable",
381 .data
= &bpf_jit_enable
,
382 .maxlen
= sizeof(int),
384 .proc_handler
= proc_dointvec_minmax_bpf_enable
,
385 # ifdef CONFIG_BPF_JIT_ALWAYS_ON
393 # ifdef CONFIG_HAVE_EBPF_JIT
395 .procname
= "bpf_jit_harden",
396 .data
= &bpf_jit_harden
,
397 .maxlen
= sizeof(int),
399 .proc_handler
= proc_dointvec_minmax_bpf_restricted
,
404 .procname
= "bpf_jit_kallsyms",
405 .data
= &bpf_jit_kallsyms
,
406 .maxlen
= sizeof(int),
408 .proc_handler
= proc_dointvec_minmax_bpf_restricted
,
414 .procname
= "bpf_jit_limit",
415 .data
= &bpf_jit_limit
,
416 .maxlen
= sizeof(long),
418 .proc_handler
= proc_dolongvec_minmax_bpf_restricted
,
424 .procname
= "netdev_tstamp_prequeue",
425 .data
= &netdev_tstamp_prequeue
,
426 .maxlen
= sizeof(int),
428 .proc_handler
= proc_dointvec
431 .procname
= "message_cost",
432 .data
= &net_ratelimit_state
.interval
,
433 .maxlen
= sizeof(int),
435 .proc_handler
= proc_dointvec_jiffies
,
438 .procname
= "message_burst",
439 .data
= &net_ratelimit_state
.burst
,
440 .maxlen
= sizeof(int),
442 .proc_handler
= proc_dointvec
,
445 .procname
= "optmem_max",
446 .data
= &sysctl_optmem_max
,
447 .maxlen
= sizeof(int),
449 .proc_handler
= proc_dointvec
452 .procname
= "tstamp_allow_data",
453 .data
= &sysctl_tstamp_allow_data
,
454 .maxlen
= sizeof(int),
456 .proc_handler
= proc_dointvec_minmax
,
462 .procname
= "rps_sock_flow_entries",
463 .maxlen
= sizeof(int),
465 .proc_handler
= rps_sock_flow_sysctl
468 #ifdef CONFIG_NET_FLOW_LIMIT
470 .procname
= "flow_limit_cpu_bitmap",
472 .proc_handler
= flow_limit_cpu_sysctl
475 .procname
= "flow_limit_table_len",
476 .data
= &netdev_flow_limit_table_len
,
477 .maxlen
= sizeof(int),
479 .proc_handler
= flow_limit_table_len_sysctl
481 #endif /* CONFIG_NET_FLOW_LIMIT */
482 #ifdef CONFIG_NET_RX_BUSY_POLL
484 .procname
= "busy_poll",
485 .data
= &sysctl_net_busy_poll
,
486 .maxlen
= sizeof(unsigned int),
488 .proc_handler
= proc_dointvec_minmax
,
492 .procname
= "busy_read",
493 .data
= &sysctl_net_busy_read
,
494 .maxlen
= sizeof(unsigned int),
496 .proc_handler
= proc_dointvec_minmax
,
500 #ifdef CONFIG_NET_SCHED
502 .procname
= "default_qdisc",
505 .proc_handler
= set_default_qdisc
508 #endif /* CONFIG_NET */
510 .procname
= "netdev_budget",
511 .data
= &netdev_budget
,
512 .maxlen
= sizeof(int),
514 .proc_handler
= proc_dointvec
517 .procname
= "warnings",
518 .data
= &net_msg_warn
,
519 .maxlen
= sizeof(int),
521 .proc_handler
= proc_dointvec
524 .procname
= "max_skb_frags",
525 .data
= &sysctl_max_skb_frags
,
526 .maxlen
= sizeof(int),
528 .proc_handler
= proc_dointvec_minmax
,
530 .extra2
= &max_skb_frags
,
533 .procname
= "netdev_budget_usecs",
534 .data
= &netdev_budget_usecs
,
535 .maxlen
= sizeof(unsigned int),
537 .proc_handler
= proc_dointvec_minmax
,
541 .procname
= "fb_tunnels_only_for_init_net",
542 .data
= &sysctl_fb_tunnels_only_for_init_net
,
543 .maxlen
= sizeof(int),
545 .proc_handler
= proc_dointvec_minmax
,
552 static struct ctl_table netns_core_table
[] = {
554 .procname
= "somaxconn",
555 .data
= &init_net
.core
.sysctl_somaxconn
,
556 .maxlen
= sizeof(int),
559 .proc_handler
= proc_dointvec_minmax
564 static __net_init
int sysctl_core_net_init(struct net
*net
)
566 struct ctl_table
*tbl
;
568 tbl
= netns_core_table
;
569 if (!net_eq(net
, &init_net
)) {
570 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
574 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
576 /* Don't export any sysctls to unprivileged users */
577 if (net
->user_ns
!= &init_user_ns
) {
578 tbl
[0].procname
= NULL
;
582 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
583 if (net
->core
.sysctl_hdr
== NULL
)
589 if (tbl
!= netns_core_table
)
595 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
597 struct ctl_table
*tbl
;
599 tbl
= net
->core
.sysctl_hdr
->ctl_table_arg
;
600 unregister_net_sysctl_table(net
->core
.sysctl_hdr
);
601 BUG_ON(tbl
== netns_core_table
);
605 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
606 .init
= sysctl_core_net_init
,
607 .exit
= sysctl_core_net_exit
,
610 static __init
int sysctl_core_init(void)
612 register_net_sysctl(&init_net
, "net/core", net_core_table
);
613 return register_pernet_subsys(&sysctl_core_ops
);
616 fs_initcall(sysctl_core_init
);