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
);
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
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 __user
*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 __user
*buffer
, size_t *lenp
,
123 struct sd_flow_limit
*cur
;
124 struct softnet_data
*sd
;
128 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
132 ret
= cpumask_parse_user(buffer
, *lenp
, mask
);
136 mutex_lock(&flow_limit_update_mutex
);
137 len
= sizeof(*cur
) + netdev_flow_limit_table_len
;
138 for_each_possible_cpu(i
) {
139 sd
= &per_cpu(softnet_data
, i
);
140 cur
= rcu_dereference_protected(sd
->flow_limit
,
141 lockdep_is_held(&flow_limit_update_mutex
));
142 if (cur
&& !cpumask_test_cpu(i
, mask
)) {
143 RCU_INIT_POINTER(sd
->flow_limit
, NULL
);
146 } else if (!cur
&& cpumask_test_cpu(i
, mask
)) {
147 cur
= kzalloc_node(len
, GFP_KERNEL
,
150 /* not unwinding previous changes */
154 cur
->num_buckets
= netdev_flow_limit_table_len
;
155 rcu_assign_pointer(sd
->flow_limit
, cur
);
159 mutex_unlock(&flow_limit_update_mutex
);
163 if (*ppos
|| !*lenp
) {
170 for_each_possible_cpu(i
) {
171 sd
= &per_cpu(softnet_data
, i
);
172 if (rcu_dereference(sd
->flow_limit
))
173 cpumask_set_cpu(i
, mask
);
177 len
= min(sizeof(kbuf
) - 1, *lenp
);
178 len
= scnprintf(kbuf
, len
, "%*pb", cpumask_pr_args(mask
));
185 if (copy_to_user(buffer
, kbuf
, len
)) {
194 free_cpumask_var(mask
);
198 static int flow_limit_table_len_sysctl(struct ctl_table
*table
, int write
,
199 void __user
*buffer
, size_t *lenp
,
202 unsigned int old
, *ptr
;
205 mutex_lock(&flow_limit_update_mutex
);
209 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
210 if (!ret
&& write
&& !is_power_of_2(*ptr
)) {
215 mutex_unlock(&flow_limit_update_mutex
);
218 #endif /* CONFIG_NET_FLOW_LIMIT */
220 #ifdef CONFIG_NET_SCHED
221 static int set_default_qdisc(struct ctl_table
*table
, int write
,
222 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
225 struct ctl_table tbl
= {
231 qdisc_get_default(id
, IFNAMSIZ
);
233 ret
= proc_dostring(&tbl
, write
, buffer
, lenp
, ppos
);
234 if (write
&& ret
== 0)
235 ret
= qdisc_set_default(id
);
240 static int proc_do_dev_weight(struct ctl_table
*table
, int write
,
241 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
245 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
249 dev_rx_weight
= weight_p
* dev_weight_rx_bias
;
250 dev_tx_weight
= weight_p
* dev_weight_tx_bias
;
255 static int proc_do_rss_key(struct ctl_table
*table
, int write
,
256 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
258 struct ctl_table fake_table
;
259 char buf
[NETDEV_RSS_KEY_LEN
* 3];
261 snprintf(buf
, sizeof(buf
), "%*phC", NETDEV_RSS_KEY_LEN
, netdev_rss_key
);
262 fake_table
.data
= buf
;
263 fake_table
.maxlen
= sizeof(buf
);
264 return proc_dostring(&fake_table
, write
, buffer
, lenp
, ppos
);
267 #ifdef CONFIG_BPF_JIT
268 static int proc_dointvec_minmax_bpf_enable(struct ctl_table
*table
, int write
,
269 void __user
*buffer
, size_t *lenp
,
272 int ret
, jit_enable
= *(int *)table
->data
;
273 struct ctl_table tmp
= *table
;
275 if (write
&& !capable(CAP_SYS_ADMIN
))
278 tmp
.data
= &jit_enable
;
279 ret
= proc_dointvec_minmax(&tmp
, write
, buffer
, lenp
, ppos
);
281 if (jit_enable
< 2 ||
282 (jit_enable
== 2 && bpf_dump_raw_ok())) {
283 *(int *)table
->data
= jit_enable
;
285 pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n");
294 proc_dointvec_minmax_bpf_restricted(struct ctl_table
*table
, int write
,
295 void __user
*buffer
, size_t *lenp
,
298 if (!capable(CAP_SYS_ADMIN
))
301 return proc_dointvec_minmax(table
, write
, buffer
, lenp
, ppos
);
305 proc_dolongvec_minmax_bpf_restricted(struct ctl_table
*table
, int write
,
306 void __user
*buffer
, size_t *lenp
,
309 if (!capable(CAP_SYS_ADMIN
))
312 return proc_doulongvec_minmax(table
, write
, buffer
, lenp
, ppos
);
316 static struct ctl_table net_core_table
[] = {
319 .procname
= "wmem_max",
320 .data
= &sysctl_wmem_max
,
321 .maxlen
= sizeof(int),
323 .proc_handler
= proc_dointvec_minmax
,
324 .extra1
= &min_sndbuf
,
327 .procname
= "rmem_max",
328 .data
= &sysctl_rmem_max
,
329 .maxlen
= sizeof(int),
331 .proc_handler
= proc_dointvec_minmax
,
332 .extra1
= &min_rcvbuf
,
335 .procname
= "wmem_default",
336 .data
= &sysctl_wmem_default
,
337 .maxlen
= sizeof(int),
339 .proc_handler
= proc_dointvec_minmax
,
340 .extra1
= &min_sndbuf
,
343 .procname
= "rmem_default",
344 .data
= &sysctl_rmem_default
,
345 .maxlen
= sizeof(int),
347 .proc_handler
= proc_dointvec_minmax
,
348 .extra1
= &min_rcvbuf
,
351 .procname
= "dev_weight",
353 .maxlen
= sizeof(int),
355 .proc_handler
= proc_do_dev_weight
,
358 .procname
= "dev_weight_rx_bias",
359 .data
= &dev_weight_rx_bias
,
360 .maxlen
= sizeof(int),
362 .proc_handler
= proc_do_dev_weight
,
365 .procname
= "dev_weight_tx_bias",
366 .data
= &dev_weight_tx_bias
,
367 .maxlen
= sizeof(int),
369 .proc_handler
= proc_do_dev_weight
,
372 .procname
= "netdev_max_backlog",
373 .data
= &netdev_max_backlog
,
374 .maxlen
= sizeof(int),
376 .proc_handler
= proc_dointvec
379 .procname
= "netdev_rss_key",
380 .data
= &netdev_rss_key
,
381 .maxlen
= sizeof(int),
383 .proc_handler
= proc_do_rss_key
,
385 #ifdef CONFIG_BPF_JIT
387 .procname
= "bpf_jit_enable",
388 .data
= &bpf_jit_enable
,
389 .maxlen
= sizeof(int),
391 .proc_handler
= proc_dointvec_minmax_bpf_enable
,
392 # ifdef CONFIG_BPF_JIT_ALWAYS_ON
400 # ifdef CONFIG_HAVE_EBPF_JIT
402 .procname
= "bpf_jit_harden",
403 .data
= &bpf_jit_harden
,
404 .maxlen
= sizeof(int),
406 .proc_handler
= proc_dointvec_minmax_bpf_restricted
,
411 .procname
= "bpf_jit_kallsyms",
412 .data
= &bpf_jit_kallsyms
,
413 .maxlen
= sizeof(int),
415 .proc_handler
= proc_dointvec_minmax_bpf_restricted
,
421 .procname
= "bpf_jit_limit",
422 .data
= &bpf_jit_limit
,
423 .maxlen
= sizeof(long),
425 .proc_handler
= proc_dolongvec_minmax_bpf_restricted
,
431 .procname
= "netdev_tstamp_prequeue",
432 .data
= &netdev_tstamp_prequeue
,
433 .maxlen
= sizeof(int),
435 .proc_handler
= proc_dointvec
438 .procname
= "message_cost",
439 .data
= &net_ratelimit_state
.interval
,
440 .maxlen
= sizeof(int),
442 .proc_handler
= proc_dointvec_jiffies
,
445 .procname
= "message_burst",
446 .data
= &net_ratelimit_state
.burst
,
447 .maxlen
= sizeof(int),
449 .proc_handler
= proc_dointvec
,
452 .procname
= "optmem_max",
453 .data
= &sysctl_optmem_max
,
454 .maxlen
= sizeof(int),
456 .proc_handler
= proc_dointvec
459 .procname
= "tstamp_allow_data",
460 .data
= &sysctl_tstamp_allow_data
,
461 .maxlen
= sizeof(int),
463 .proc_handler
= proc_dointvec_minmax
,
469 .procname
= "rps_sock_flow_entries",
470 .maxlen
= sizeof(int),
472 .proc_handler
= rps_sock_flow_sysctl
475 #ifdef CONFIG_NET_FLOW_LIMIT
477 .procname
= "flow_limit_cpu_bitmap",
479 .proc_handler
= flow_limit_cpu_sysctl
482 .procname
= "flow_limit_table_len",
483 .data
= &netdev_flow_limit_table_len
,
484 .maxlen
= sizeof(int),
486 .proc_handler
= flow_limit_table_len_sysctl
488 #endif /* CONFIG_NET_FLOW_LIMIT */
489 #ifdef CONFIG_NET_RX_BUSY_POLL
491 .procname
= "busy_poll",
492 .data
= &sysctl_net_busy_poll
,
493 .maxlen
= sizeof(unsigned int),
495 .proc_handler
= proc_dointvec_minmax
,
499 .procname
= "busy_read",
500 .data
= &sysctl_net_busy_read
,
501 .maxlen
= sizeof(unsigned int),
503 .proc_handler
= proc_dointvec_minmax
,
507 #ifdef CONFIG_NET_SCHED
509 .procname
= "default_qdisc",
512 .proc_handler
= set_default_qdisc
515 #endif /* CONFIG_NET */
517 .procname
= "netdev_budget",
518 .data
= &netdev_budget
,
519 .maxlen
= sizeof(int),
521 .proc_handler
= proc_dointvec
524 .procname
= "warnings",
525 .data
= &net_msg_warn
,
526 .maxlen
= sizeof(int),
528 .proc_handler
= proc_dointvec
531 .procname
= "max_skb_frags",
532 .data
= &sysctl_max_skb_frags
,
533 .maxlen
= sizeof(int),
535 .proc_handler
= proc_dointvec_minmax
,
537 .extra2
= &max_skb_frags
,
540 .procname
= "netdev_budget_usecs",
541 .data
= &netdev_budget_usecs
,
542 .maxlen
= sizeof(unsigned int),
544 .proc_handler
= proc_dointvec_minmax
,
548 .procname
= "fb_tunnels_only_for_init_net",
549 .data
= &sysctl_fb_tunnels_only_for_init_net
,
550 .maxlen
= sizeof(int),
552 .proc_handler
= proc_dointvec_minmax
,
557 .procname
= "devconf_inherit_init_net",
558 .data
= &sysctl_devconf_inherit_init_net
,
559 .maxlen
= sizeof(int),
561 .proc_handler
= proc_dointvec_minmax
,
566 .procname
= "high_order_alloc_disable",
567 .data
= &net_high_order_alloc_disable_key
.key
,
568 .maxlen
= sizeof(net_high_order_alloc_disable_key
),
570 .proc_handler
= proc_do_static_key
,
575 static struct ctl_table netns_core_table
[] = {
577 .procname
= "somaxconn",
578 .data
= &init_net
.core
.sysctl_somaxconn
,
579 .maxlen
= sizeof(int),
582 .proc_handler
= proc_dointvec_minmax
587 static __net_init
int sysctl_core_net_init(struct net
*net
)
589 struct ctl_table
*tbl
;
591 tbl
= netns_core_table
;
592 if (!net_eq(net
, &init_net
)) {
593 tbl
= kmemdup(tbl
, sizeof(netns_core_table
), GFP_KERNEL
);
597 tbl
[0].data
= &net
->core
.sysctl_somaxconn
;
599 /* Don't export any sysctls to unprivileged users */
600 if (net
->user_ns
!= &init_user_ns
) {
601 tbl
[0].procname
= NULL
;
605 net
->core
.sysctl_hdr
= register_net_sysctl(net
, "net/core", tbl
);
606 if (net
->core
.sysctl_hdr
== NULL
)
612 if (tbl
!= netns_core_table
)
618 static __net_exit
void sysctl_core_net_exit(struct net
*net
)
620 struct ctl_table
*tbl
;
622 tbl
= net
->core
.sysctl_hdr
->ctl_table_arg
;
623 unregister_net_sysctl_table(net
->core
.sysctl_hdr
);
624 BUG_ON(tbl
== netns_core_table
);
628 static __net_initdata
struct pernet_operations sysctl_core_ops
= {
629 .init
= sysctl_core_net_init
,
630 .exit
= sysctl_core_net_exit
,
633 static __init
int sysctl_core_init(void)
635 register_net_sysctl(&init_net
, "net/core", net_core_table
);
636 return register_pernet_subsys(&sysctl_core_ops
);
639 fs_initcall(sysctl_core_init
);