Linux 4.19.133
[linux/fpc-iii.git] / net / core / sysctl_net_core.c
blob2597449ae9b48586830c710890e1fdcb22e4f410
1 // SPDX-License-Identifier: GPL-2.0
2 /* -*- linux-c -*-
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]
7 */
9 #include <linux/mm.h>
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>
19 #include <net/ip.h>
20 #include <net/sock.h>
21 #include <net/net_ratelimit.h>
22 #include <net/busy_poll.h>
23 #include <net/pkt_sched.h>
25 static int zero = 0;
26 static int one = 1;
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 #ifdef CONFIG_RPS
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;
44 int ret, i;
45 struct ctl_table tmp = {
46 .data = &size,
47 .maxlen = sizeof(size),
48 .mode = table->mode
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);
61 if (write) {
62 if (size) {
63 if (size > 1<<29) {
64 /* Enforce limit to prevent overflow */
65 mutex_unlock(&sock_flow_mutex);
66 return -EINVAL;
68 size = roundup_pow_of_two(size);
69 if (size != orig_size) {
70 sock_table =
71 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
72 if (!sock_table) {
73 mutex_unlock(&sock_flow_mutex);
74 return -ENOMEM;
76 rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1;
77 sock_table->mask = size - 1;
78 } else
79 sock_table = orig_sock_table;
81 for (i = 0; i < size; i++)
82 sock_table->ents[i] = RPS_NO_CPU;
83 } else
84 sock_table = NULL;
86 if (sock_table != orig_sock_table) {
87 rcu_assign_pointer(rps_sock_flow_table, sock_table);
88 if (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);
95 synchronize_rcu();
96 vfree(orig_sock_table);
101 mutex_unlock(&sock_flow_mutex);
103 return ret;
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,
112 loff_t *ppos)
114 struct sd_flow_limit *cur;
115 struct softnet_data *sd;
116 cpumask_var_t mask;
117 int i, len, ret = 0;
119 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
120 return -ENOMEM;
122 if (write) {
123 ret = cpumask_parse_user(buffer, *lenp, mask);
124 if (ret)
125 goto done;
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);
135 synchronize_rcu();
136 kfree(cur);
137 } else if (!cur && cpumask_test_cpu(i, mask)) {
138 cur = kzalloc_node(len, GFP_KERNEL,
139 cpu_to_node(i));
140 if (!cur) {
141 /* not unwinding previous changes */
142 ret = -ENOMEM;
143 goto write_unlock;
145 cur->num_buckets = netdev_flow_limit_table_len;
146 rcu_assign_pointer(sd->flow_limit, cur);
149 write_unlock:
150 mutex_unlock(&flow_limit_update_mutex);
151 } else {
152 char kbuf[128];
154 if (*ppos || !*lenp) {
155 *lenp = 0;
156 goto done;
159 cpumask_clear(mask);
160 rcu_read_lock();
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);
166 rcu_read_unlock();
168 len = min(sizeof(kbuf) - 1, *lenp);
169 len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask));
170 if (!len) {
171 *lenp = 0;
172 goto done;
174 if (len < *lenp)
175 kbuf[len++] = '\n';
176 if (copy_to_user(buffer, kbuf, len)) {
177 ret = -EFAULT;
178 goto done;
180 *lenp = len;
181 *ppos += len;
184 done:
185 free_cpumask_var(mask);
186 return ret;
189 static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
190 void __user *buffer, size_t *lenp,
191 loff_t *ppos)
193 unsigned int old, *ptr;
194 int ret;
196 mutex_lock(&flow_limit_update_mutex);
198 ptr = table->data;
199 old = *ptr;
200 ret = proc_dointvec(table, write, buffer, lenp, ppos);
201 if (!ret && write && !is_power_of_2(*ptr)) {
202 *ptr = old;
203 ret = -EINVAL;
206 mutex_unlock(&flow_limit_update_mutex);
207 return ret;
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)
215 char id[IFNAMSIZ];
216 struct ctl_table tbl = {
217 .data = id,
218 .maxlen = IFNAMSIZ,
220 int ret;
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);
227 return ret;
229 #endif
231 static int proc_do_dev_weight(struct ctl_table *table, int write,
232 void __user *buffer, size_t *lenp, loff_t *ppos)
234 int ret;
236 ret = proc_dointvec(table, write, buffer, lenp, ppos);
237 if (ret != 0)
238 return ret;
240 dev_rx_weight = weight_p * dev_weight_rx_bias;
241 dev_tx_weight = weight_p * dev_weight_tx_bias;
243 return ret;
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,
261 loff_t *ppos)
263 int ret, jit_enable = *(int *)table->data;
264 struct ctl_table tmp = *table;
266 if (write && !capable(CAP_SYS_ADMIN))
267 return -EPERM;
269 tmp.data = &jit_enable;
270 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
271 if (write && !ret) {
272 if (jit_enable < 2 ||
273 (jit_enable == 2 && bpf_dump_raw_ok(current_cred()))) {
274 *(int *)table->data = jit_enable;
275 if (jit_enable == 2)
276 pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n");
277 } else {
278 ret = -EPERM;
281 return ret;
284 # ifdef CONFIG_HAVE_EBPF_JIT
285 static int
286 proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write,
287 void __user *buffer, size_t *lenp,
288 loff_t *ppos)
290 if (!capable(CAP_SYS_ADMIN))
291 return -EPERM;
293 return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
295 # endif /* CONFIG_HAVE_EBPF_JIT */
297 static int
298 proc_dolongvec_minmax_bpf_restricted(struct ctl_table *table, int write,
299 void __user *buffer, size_t *lenp,
300 loff_t *ppos)
302 if (!capable(CAP_SYS_ADMIN))
303 return -EPERM;
305 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
307 #endif
309 static struct ctl_table net_core_table[] = {
310 #ifdef CONFIG_NET
312 .procname = "wmem_max",
313 .data = &sysctl_wmem_max,
314 .maxlen = sizeof(int),
315 .mode = 0644,
316 .proc_handler = proc_dointvec_minmax,
317 .extra1 = &min_sndbuf,
320 .procname = "rmem_max",
321 .data = &sysctl_rmem_max,
322 .maxlen = sizeof(int),
323 .mode = 0644,
324 .proc_handler = proc_dointvec_minmax,
325 .extra1 = &min_rcvbuf,
328 .procname = "wmem_default",
329 .data = &sysctl_wmem_default,
330 .maxlen = sizeof(int),
331 .mode = 0644,
332 .proc_handler = proc_dointvec_minmax,
333 .extra1 = &min_sndbuf,
336 .procname = "rmem_default",
337 .data = &sysctl_rmem_default,
338 .maxlen = sizeof(int),
339 .mode = 0644,
340 .proc_handler = proc_dointvec_minmax,
341 .extra1 = &min_rcvbuf,
344 .procname = "dev_weight",
345 .data = &weight_p,
346 .maxlen = sizeof(int),
347 .mode = 0644,
348 .proc_handler = proc_do_dev_weight,
351 .procname = "dev_weight_rx_bias",
352 .data = &dev_weight_rx_bias,
353 .maxlen = sizeof(int),
354 .mode = 0644,
355 .proc_handler = proc_do_dev_weight,
358 .procname = "dev_weight_tx_bias",
359 .data = &dev_weight_tx_bias,
360 .maxlen = sizeof(int),
361 .mode = 0644,
362 .proc_handler = proc_do_dev_weight,
365 .procname = "netdev_max_backlog",
366 .data = &netdev_max_backlog,
367 .maxlen = sizeof(int),
368 .mode = 0644,
369 .proc_handler = proc_dointvec
372 .procname = "netdev_rss_key",
373 .data = &netdev_rss_key,
374 .maxlen = sizeof(int),
375 .mode = 0444,
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),
383 .mode = 0644,
384 .proc_handler = proc_dointvec_minmax_bpf_enable,
385 # ifdef CONFIG_BPF_JIT_ALWAYS_ON
386 .extra1 = &one,
387 .extra2 = &one,
388 # else
389 .extra1 = &zero,
390 .extra2 = &two,
391 # endif
393 # ifdef CONFIG_HAVE_EBPF_JIT
395 .procname = "bpf_jit_harden",
396 .data = &bpf_jit_harden,
397 .maxlen = sizeof(int),
398 .mode = 0600,
399 .proc_handler = proc_dointvec_minmax_bpf_restricted,
400 .extra1 = &zero,
401 .extra2 = &two,
404 .procname = "bpf_jit_kallsyms",
405 .data = &bpf_jit_kallsyms,
406 .maxlen = sizeof(int),
407 .mode = 0600,
408 .proc_handler = proc_dointvec_minmax_bpf_restricted,
409 .extra1 = &zero,
410 .extra2 = &one,
412 # endif
414 .procname = "bpf_jit_limit",
415 .data = &bpf_jit_limit,
416 .maxlen = sizeof(long),
417 .mode = 0600,
418 .proc_handler = proc_dolongvec_minmax_bpf_restricted,
419 .extra1 = &long_one,
420 .extra2 = &long_max,
422 #endif
424 .procname = "netdev_tstamp_prequeue",
425 .data = &netdev_tstamp_prequeue,
426 .maxlen = sizeof(int),
427 .mode = 0644,
428 .proc_handler = proc_dointvec
431 .procname = "message_cost",
432 .data = &net_ratelimit_state.interval,
433 .maxlen = sizeof(int),
434 .mode = 0644,
435 .proc_handler = proc_dointvec_jiffies,
438 .procname = "message_burst",
439 .data = &net_ratelimit_state.burst,
440 .maxlen = sizeof(int),
441 .mode = 0644,
442 .proc_handler = proc_dointvec,
445 .procname = "optmem_max",
446 .data = &sysctl_optmem_max,
447 .maxlen = sizeof(int),
448 .mode = 0644,
449 .proc_handler = proc_dointvec
452 .procname = "tstamp_allow_data",
453 .data = &sysctl_tstamp_allow_data,
454 .maxlen = sizeof(int),
455 .mode = 0644,
456 .proc_handler = proc_dointvec_minmax,
457 .extra1 = &zero,
458 .extra2 = &one
460 #ifdef CONFIG_RPS
462 .procname = "rps_sock_flow_entries",
463 .maxlen = sizeof(int),
464 .mode = 0644,
465 .proc_handler = rps_sock_flow_sysctl
467 #endif
468 #ifdef CONFIG_NET_FLOW_LIMIT
470 .procname = "flow_limit_cpu_bitmap",
471 .mode = 0644,
472 .proc_handler = flow_limit_cpu_sysctl
475 .procname = "flow_limit_table_len",
476 .data = &netdev_flow_limit_table_len,
477 .maxlen = sizeof(int),
478 .mode = 0644,
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),
487 .mode = 0644,
488 .proc_handler = proc_dointvec_minmax,
489 .extra1 = &zero,
492 .procname = "busy_read",
493 .data = &sysctl_net_busy_read,
494 .maxlen = sizeof(unsigned int),
495 .mode = 0644,
496 .proc_handler = proc_dointvec_minmax,
497 .extra1 = &zero,
499 #endif
500 #ifdef CONFIG_NET_SCHED
502 .procname = "default_qdisc",
503 .mode = 0644,
504 .maxlen = IFNAMSIZ,
505 .proc_handler = set_default_qdisc
507 #endif
508 #endif /* CONFIG_NET */
510 .procname = "netdev_budget",
511 .data = &netdev_budget,
512 .maxlen = sizeof(int),
513 .mode = 0644,
514 .proc_handler = proc_dointvec
517 .procname = "warnings",
518 .data = &net_msg_warn,
519 .maxlen = sizeof(int),
520 .mode = 0644,
521 .proc_handler = proc_dointvec
524 .procname = "max_skb_frags",
525 .data = &sysctl_max_skb_frags,
526 .maxlen = sizeof(int),
527 .mode = 0644,
528 .proc_handler = proc_dointvec_minmax,
529 .extra1 = &one,
530 .extra2 = &max_skb_frags,
533 .procname = "netdev_budget_usecs",
534 .data = &netdev_budget_usecs,
535 .maxlen = sizeof(unsigned int),
536 .mode = 0644,
537 .proc_handler = proc_dointvec_minmax,
538 .extra1 = &zero,
541 .procname = "fb_tunnels_only_for_init_net",
542 .data = &sysctl_fb_tunnels_only_for_init_net,
543 .maxlen = sizeof(int),
544 .mode = 0644,
545 .proc_handler = proc_dointvec_minmax,
546 .extra1 = &zero,
547 .extra2 = &one,
552 static struct ctl_table netns_core_table[] = {
554 .procname = "somaxconn",
555 .data = &init_net.core.sysctl_somaxconn,
556 .maxlen = sizeof(int),
557 .mode = 0644,
558 .extra1 = &zero,
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);
571 if (tbl == NULL)
572 goto err_dup;
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)
584 goto err_reg;
586 return 0;
588 err_reg:
589 if (tbl != netns_core_table)
590 kfree(tbl);
591 err_dup:
592 return -ENOMEM;
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);
602 kfree(tbl);
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);