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>
18 #include <linux/kmemleak.h>
22 #include <net/net_ratelimit.h>
23 #include <net/busy_poll.h>
24 #include <net/pkt_sched.h>
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
;
33 static int net_msg_warn
; /* Unused, but still a sysctl */
36 static int rps_sock_flow_sysctl(struct ctl_table
*table
, int write
,
37 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
39 unsigned int orig_size
, size
;
41 struct ctl_table tmp
= {
43 .maxlen
= sizeof(size
),
46 struct rps_sock_flow_table
*orig_sock_table
, *sock_table
;
47 static DEFINE_MUTEX(sock_flow_mutex
);
49 mutex_lock(&sock_flow_mutex
);
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;
55 ret
= proc_dointvec(&tmp
, write
, buffer
, lenp
, ppos
);
60 /* Enforce limit to prevent overflow */
61 mutex_unlock(&sock_flow_mutex
);
64 size
= roundup_pow_of_two(size
);
65 if (size
!= orig_size
) {
67 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size
));
69 mutex_unlock(&sock_flow_mutex
);
72 rps_cpu_mask
= roundup_pow_of_two(nr_cpu_ids
) - 1;
73 sock_table
->mask
= size
- 1;
75 sock_table
= orig_sock_table
;
77 for (i
= 0; i
< size
; i
++)
78 sock_table
->ents
[i
] = RPS_NO_CPU
;
82 if (sock_table
!= orig_sock_table
) {
83 rcu_assign_pointer(rps_sock_flow_table
, sock_table
);
85 static_key_slow_inc(&rps_needed
);
86 static_key_slow_inc(&rfs_needed
);
88 if (orig_sock_table
) {
89 static_key_slow_dec(&rps_needed
);
90 static_key_slow_dec(&rfs_needed
);
92 vfree(orig_sock_table
);
97 mutex_unlock(&sock_flow_mutex
);
101 #endif /* CONFIG_RPS */
103 #ifdef CONFIG_NET_FLOW_LIMIT
104 static DEFINE_MUTEX(flow_limit_update_mutex
);
106 static int flow_limit_cpu_sysctl(struct ctl_table
*table
, int write
,
107 void __user
*buffer
, size_t *lenp
,
110 struct sd_flow_limit
*cur
;
111 struct softnet_data
*sd
;
115 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
119 ret
= cpumask_parse_user(buffer
, *lenp
, mask
);
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
);
133 } else if (!cur
&& cpumask_test_cpu(i
, mask
)) {
134 cur
= kzalloc_node(len
, GFP_KERNEL
,
137 /* not unwinding previous changes */
141 cur
->num_buckets
= netdev_flow_limit_table_len
;
142 rcu_assign_pointer(sd
->flow_limit
, cur
);
146 mutex_unlock(&flow_limit_update_mutex
);
150 if (*ppos
|| !*lenp
) {
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
);
164 len
= min(sizeof(kbuf
) - 1, *lenp
);
165 len
= scnprintf(kbuf
, len
, "%*pb", cpumask_pr_args(mask
));
172 if (copy_to_user(buffer
, kbuf
, len
)) {
181 free_cpumask_var(mask
);
185 static int flow_limit_table_len_sysctl(struct ctl_table
*table
, int write
,
186 void __user
*buffer
, size_t *lenp
,
189 unsigned int old
, *ptr
;
192 mutex_lock(&flow_limit_update_mutex
);
196 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
197 if (!ret
&& write
&& !is_power_of_2(*ptr
)) {
202 mutex_unlock(&flow_limit_update_mutex
);
205 #endif /* CONFIG_NET_FLOW_LIMIT */
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
)
212 struct ctl_table tbl
= {
218 qdisc_get_default(id
, IFNAMSIZ
);
220 ret
= proc_dostring(&tbl
, write
, buffer
, lenp
, ppos
);
221 if (write
&& ret
== 0)
222 ret
= qdisc_set_default(id
);
227 static int proc_do_dev_weight(struct ctl_table
*table
, int write
,
228 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
232 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
236 dev_rx_weight
= weight_p
* dev_weight_rx_bias
;
237 dev_tx_weight
= weight_p
* dev_weight_tx_bias
;
242 static int proc_do_rss_key(struct ctl_table
*table
, int write
,
243 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
245 struct ctl_table fake_table
;
246 char buf
[NETDEV_RSS_KEY_LEN
* 3];
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
);
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
,
259 int ret
, jit_enable
= *(int *)table
->data
;
260 struct ctl_table tmp
= *table
;
262 if (write
&& !capable(CAP_SYS_ADMIN
))
265 tmp
.data
= &jit_enable
;
266 ret
= proc_dointvec_minmax(&tmp
, write
, buffer
, lenp
, ppos
);
268 if (jit_enable
< 2 ||
269 (jit_enable
== 2 && bpf_dump_raw_ok())) {
270 *(int *)table
->data
= jit_enable
;
272 pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n");
280 # ifdef CONFIG_HAVE_EBPF_JIT
282 proc_dointvec_minmax_bpf_restricted(struct ctl_table
*table
, int write
,
283 void __user
*buffer
, size_t *lenp
,
286 if (!capable(CAP_SYS_ADMIN
))
289 return proc_dointvec_minmax(table
, write
, buffer
, lenp
, ppos
);
294 static struct ctl_table net_core_table
[] = {
297 .procname
= "wmem_max",
298 .data
= &sysctl_wmem_max
,
299 .maxlen
= sizeof(int),
301 .proc_handler
= proc_dointvec_minmax
,
302 .extra1
= &min_sndbuf
,
305 .procname
= "rmem_max",
306 .data
= &sysctl_rmem_max
,
307 .maxlen
= sizeof(int),
309 .proc_handler
= proc_dointvec_minmax
,
310 .extra1
= &min_rcvbuf
,
313 .procname
= "wmem_default",
314 .data
= &sysctl_wmem_default
,
315 .maxlen
= sizeof(int),
317 .proc_handler
= proc_dointvec_minmax
,
318 .extra1
= &min_sndbuf
,
321 .procname
= "rmem_default",
322 .data
= &sysctl_rmem_default
,
323 .maxlen
= sizeof(int),
325 .proc_handler
= proc_dointvec_minmax
,
326 .extra1
= &min_rcvbuf
,
329 .procname
= "dev_weight",
331 .maxlen
= sizeof(int),
333 .proc_handler
= proc_do_dev_weight
,
336 .procname
= "dev_weight_rx_bias",
337 .data
= &dev_weight_rx_bias
,
338 .maxlen
= sizeof(int),
340 .proc_handler
= proc_do_dev_weight
,
343 .procname
= "dev_weight_tx_bias",
344 .data
= &dev_weight_tx_bias
,
345 .maxlen
= sizeof(int),
347 .proc_handler
= proc_do_dev_weight
,
350 .procname
= "netdev_max_backlog",
351 .data
= &netdev_max_backlog
,
352 .maxlen
= sizeof(int),
354 .proc_handler
= proc_dointvec
357 .procname
= "netdev_rss_key",
358 .data
= &netdev_rss_key
,
359 .maxlen
= sizeof(int),
361 .proc_handler
= proc_do_rss_key
,
363 #ifdef CONFIG_BPF_JIT
365 .procname
= "bpf_jit_enable",
366 .data
= &bpf_jit_enable
,
367 .maxlen
= sizeof(int),
369 .proc_handler
= proc_dointvec_minmax_bpf_enable
,
370 # ifdef CONFIG_BPF_JIT_ALWAYS_ON
378 # ifdef CONFIG_HAVE_EBPF_JIT
380 .procname
= "bpf_jit_harden",
381 .data
= &bpf_jit_harden
,
382 .maxlen
= sizeof(int),
384 .proc_handler
= proc_dointvec_minmax_bpf_restricted
,
389 .procname
= "bpf_jit_kallsyms",
390 .data
= &bpf_jit_kallsyms
,
391 .maxlen
= sizeof(int),
393 .proc_handler
= proc_dointvec_minmax_bpf_restricted
,
400 .procname
= "netdev_tstamp_prequeue",
401 .data
= &netdev_tstamp_prequeue
,
402 .maxlen
= sizeof(int),
404 .proc_handler
= proc_dointvec
407 .procname
= "message_cost",
408 .data
= &net_ratelimit_state
.interval
,
409 .maxlen
= sizeof(int),
411 .proc_handler
= proc_dointvec_jiffies
,
414 .procname
= "message_burst",
415 .data
= &net_ratelimit_state
.burst
,
416 .maxlen
= sizeof(int),
418 .proc_handler
= proc_dointvec
,
421 .procname
= "optmem_max",
422 .data
= &sysctl_optmem_max
,
423 .maxlen
= sizeof(int),
425 .proc_handler
= proc_dointvec
428 .procname
= "tstamp_allow_data",
429 .data
= &sysctl_tstamp_allow_data
,
430 .maxlen
= sizeof(int),
432 .proc_handler
= proc_dointvec_minmax
,
438 .procname
= "rps_sock_flow_entries",
439 .maxlen
= sizeof(int),
441 .proc_handler
= rps_sock_flow_sysctl
444 #ifdef CONFIG_NET_FLOW_LIMIT
446 .procname
= "flow_limit_cpu_bitmap",
448 .proc_handler
= flow_limit_cpu_sysctl
451 .procname
= "flow_limit_table_len",
452 .data
= &netdev_flow_limit_table_len
,
453 .maxlen
= sizeof(int),
455 .proc_handler
= flow_limit_table_len_sysctl
457 #endif /* CONFIG_NET_FLOW_LIMIT */
458 #ifdef CONFIG_NET_RX_BUSY_POLL
460 .procname
= "busy_poll",
461 .data
= &sysctl_net_busy_poll
,
462 .maxlen
= sizeof(unsigned int),
464 .proc_handler
= proc_dointvec_minmax
,
468 .procname
= "busy_read",
469 .data
= &sysctl_net_busy_read
,
470 .maxlen
= sizeof(unsigned int),
472 .proc_handler
= proc_dointvec_minmax
,
476 #ifdef CONFIG_NET_SCHED
478 .procname
= "default_qdisc",
481 .proc_handler
= set_default_qdisc
484 #endif /* CONFIG_NET */
486 .procname
= "netdev_budget",
487 .data
= &netdev_budget
,
488 .maxlen
= sizeof(int),
490 .proc_handler
= proc_dointvec
493 .procname
= "warnings",
494 .data
= &net_msg_warn
,
495 .maxlen
= sizeof(int),
497 .proc_handler
= proc_dointvec
500 .procname
= "max_skb_frags",
501 .data
= &sysctl_max_skb_frags
,
502 .maxlen
= sizeof(int),
504 .proc_handler
= proc_dointvec_minmax
,
506 .extra2
= &max_skb_frags
,
509 .procname
= "netdev_budget_usecs",
510 .data
= &netdev_budget_usecs
,
511 .maxlen
= sizeof(unsigned int),
513 .proc_handler
= proc_dointvec_minmax
,
519 static struct ctl_table netns_core_table
[] = {
521 .procname
= "somaxconn",
522 .data
= &init_net
.core
.sysctl_somaxconn
,
523 .maxlen
= sizeof(int),
526 .proc_handler
= proc_dointvec_minmax
531 static __net_init
int sysctl_core_net_init(struct net
*net
)
533 struct ctl_table
*tbl
;
535 tbl
= netns_core_table
;
536 if (!net_eq(net
, &init_net
)) {
537 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
541 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
543 /* Don't export any sysctls to unprivileged users */
544 if (net
->user_ns
!= &init_user_ns
) {
545 tbl
[0].procname
= NULL
;
549 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
550 if (net
->core
.sysctl_hdr
== NULL
)
556 if (tbl
!= netns_core_table
)
562 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
564 struct ctl_table
*tbl
;
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
);
572 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
573 .init
= sysctl_core_net_init
,
574 .exit
= sysctl_core_net_exit
,
577 static __init
int sysctl_core_init(void)
579 register_net_sysctl(&init_net
, "net/core", net_core_table
);
580 return register_pernet_subsys(&sysctl_core_ops
);
583 fs_initcall(sysctl_core_init
);