xref: /openbmc/linux/net/core/sysctl_net_core.c (revision 79e790ff)
1 // SPDX-License-Identifier: GPL-2.0
2 /* -*- linux-c -*-
3  * sysctl_net_core.c: sysctl interface to net core subsystem.
4  *
5  * Begun April 1, 1996, Mike Shaver.
6  * Added /proc/sys/net/core directory entry (empty =) ). [MS]
7  */
8 
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>
18 
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>
24 
25 static int two = 2;
26 static int three = 3;
27 static int int_3600 = 3600;
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;
33 
34 static int net_msg_warn;	/* Unused, but still a sysctl */
35 
36 int sysctl_fb_tunnels_only_for_init_net __read_mostly = 0;
37 EXPORT_SYMBOL(sysctl_fb_tunnels_only_for_init_net);
38 
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
44  * 3 - Both inherit all settings from current netns
45  */
46 int sysctl_devconf_inherit_init_net __read_mostly;
47 EXPORT_SYMBOL(sysctl_devconf_inherit_init_net);
48 
49 #ifdef CONFIG_RPS
50 static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
51 				void *buffer, size_t *lenp, loff_t *ppos)
52 {
53 	unsigned int orig_size, size;
54 	int ret, i;
55 	struct ctl_table tmp = {
56 		.data = &size,
57 		.maxlen = sizeof(size),
58 		.mode = table->mode
59 	};
60 	struct rps_sock_flow_table *orig_sock_table, *sock_table;
61 	static DEFINE_MUTEX(sock_flow_mutex);
62 
63 	mutex_lock(&sock_flow_mutex);
64 
65 	orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
66 					lockdep_is_held(&sock_flow_mutex));
67 	size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
68 
69 	ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
70 
71 	if (write) {
72 		if (size) {
73 			if (size > 1<<29) {
74 				/* Enforce limit to prevent overflow */
75 				mutex_unlock(&sock_flow_mutex);
76 				return -EINVAL;
77 			}
78 			size = roundup_pow_of_two(size);
79 			if (size != orig_size) {
80 				sock_table =
81 				    vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
82 				if (!sock_table) {
83 					mutex_unlock(&sock_flow_mutex);
84 					return -ENOMEM;
85 				}
86 				rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1;
87 				sock_table->mask = size - 1;
88 			} else
89 				sock_table = orig_sock_table;
90 
91 			for (i = 0; i < size; i++)
92 				sock_table->ents[i] = RPS_NO_CPU;
93 		} else
94 			sock_table = NULL;
95 
96 		if (sock_table != orig_sock_table) {
97 			rcu_assign_pointer(rps_sock_flow_table, sock_table);
98 			if (sock_table) {
99 				static_branch_inc(&rps_needed);
100 				static_branch_inc(&rfs_needed);
101 			}
102 			if (orig_sock_table) {
103 				static_branch_dec(&rps_needed);
104 				static_branch_dec(&rfs_needed);
105 				synchronize_rcu();
106 				vfree(orig_sock_table);
107 			}
108 		}
109 	}
110 
111 	mutex_unlock(&sock_flow_mutex);
112 
113 	return ret;
114 }
115 #endif /* CONFIG_RPS */
116 
117 #ifdef CONFIG_NET_FLOW_LIMIT
118 static DEFINE_MUTEX(flow_limit_update_mutex);
119 
120 static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
121 				 void *buffer, size_t *lenp, loff_t *ppos)
122 {
123 	struct sd_flow_limit *cur;
124 	struct softnet_data *sd;
125 	cpumask_var_t mask;
126 	int i, len, ret = 0;
127 
128 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
129 		return -ENOMEM;
130 
131 	if (write) {
132 		ret = cpumask_parse(buffer, mask);
133 		if (ret)
134 			goto done;
135 
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);
144 				synchronize_rcu();
145 				kfree(cur);
146 			} else if (!cur && cpumask_test_cpu(i, mask)) {
147 				cur = kzalloc_node(len, GFP_KERNEL,
148 						   cpu_to_node(i));
149 				if (!cur) {
150 					/* not unwinding previous changes */
151 					ret = -ENOMEM;
152 					goto write_unlock;
153 				}
154 				cur->num_buckets = netdev_flow_limit_table_len;
155 				rcu_assign_pointer(sd->flow_limit, cur);
156 			}
157 		}
158 write_unlock:
159 		mutex_unlock(&flow_limit_update_mutex);
160 	} else {
161 		char kbuf[128];
162 
163 		if (*ppos || !*lenp) {
164 			*lenp = 0;
165 			goto done;
166 		}
167 
168 		cpumask_clear(mask);
169 		rcu_read_lock();
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);
174 		}
175 		rcu_read_unlock();
176 
177 		len = min(sizeof(kbuf) - 1, *lenp);
178 		len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask));
179 		if (!len) {
180 			*lenp = 0;
181 			goto done;
182 		}
183 		if (len < *lenp)
184 			kbuf[len++] = '\n';
185 		memcpy(buffer, kbuf, len);
186 		*lenp = len;
187 		*ppos += len;
188 	}
189 
190 done:
191 	free_cpumask_var(mask);
192 	return ret;
193 }
194 
195 static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
196 				       void *buffer, size_t *lenp, loff_t *ppos)
197 {
198 	unsigned int old, *ptr;
199 	int ret;
200 
201 	mutex_lock(&flow_limit_update_mutex);
202 
203 	ptr = table->data;
204 	old = *ptr;
205 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
206 	if (!ret && write && !is_power_of_2(*ptr)) {
207 		*ptr = old;
208 		ret = -EINVAL;
209 	}
210 
211 	mutex_unlock(&flow_limit_update_mutex);
212 	return ret;
213 }
214 #endif /* CONFIG_NET_FLOW_LIMIT */
215 
216 #ifdef CONFIG_NET_SCHED
217 static int set_default_qdisc(struct ctl_table *table, int write,
218 			     void *buffer, size_t *lenp, loff_t *ppos)
219 {
220 	char id[IFNAMSIZ];
221 	struct ctl_table tbl = {
222 		.data = id,
223 		.maxlen = IFNAMSIZ,
224 	};
225 	int ret;
226 
227 	qdisc_get_default(id, IFNAMSIZ);
228 
229 	ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
230 	if (write && ret == 0)
231 		ret = qdisc_set_default(id);
232 	return ret;
233 }
234 #endif
235 
236 static int proc_do_dev_weight(struct ctl_table *table, int write,
237 			   void *buffer, size_t *lenp, loff_t *ppos)
238 {
239 	int ret;
240 
241 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
242 	if (ret != 0)
243 		return ret;
244 
245 	dev_rx_weight = weight_p * dev_weight_rx_bias;
246 	dev_tx_weight = weight_p * dev_weight_tx_bias;
247 
248 	return ret;
249 }
250 
251 static int proc_do_rss_key(struct ctl_table *table, int write,
252 			   void *buffer, size_t *lenp, loff_t *ppos)
253 {
254 	struct ctl_table fake_table;
255 	char buf[NETDEV_RSS_KEY_LEN * 3];
256 
257 	snprintf(buf, sizeof(buf), "%*phC", NETDEV_RSS_KEY_LEN, netdev_rss_key);
258 	fake_table.data = buf;
259 	fake_table.maxlen = sizeof(buf);
260 	return proc_dostring(&fake_table, write, buffer, lenp, ppos);
261 }
262 
263 #ifdef CONFIG_BPF_JIT
264 static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write,
265 					   void *buffer, size_t *lenp,
266 					   loff_t *ppos)
267 {
268 	int ret, jit_enable = *(int *)table->data;
269 	struct ctl_table tmp = *table;
270 
271 	if (write && !capable(CAP_SYS_ADMIN))
272 		return -EPERM;
273 
274 	tmp.data = &jit_enable;
275 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
276 	if (write && !ret) {
277 		if (jit_enable < 2 ||
278 		    (jit_enable == 2 && bpf_dump_raw_ok(current_cred()))) {
279 			*(int *)table->data = jit_enable;
280 			if (jit_enable == 2)
281 				pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n");
282 		} else {
283 			ret = -EPERM;
284 		}
285 	}
286 	return ret;
287 }
288 
289 # ifdef CONFIG_HAVE_EBPF_JIT
290 static int
291 proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write,
292 				    void *buffer, size_t *lenp, loff_t *ppos)
293 {
294 	if (!capable(CAP_SYS_ADMIN))
295 		return -EPERM;
296 
297 	return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
298 }
299 # endif /* CONFIG_HAVE_EBPF_JIT */
300 
301 static int
302 proc_dolongvec_minmax_bpf_restricted(struct ctl_table *table, int write,
303 				     void *buffer, size_t *lenp, loff_t *ppos)
304 {
305 	if (!capable(CAP_SYS_ADMIN))
306 		return -EPERM;
307 
308 	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
309 }
310 #endif
311 
312 static struct ctl_table net_core_table[] = {
313 	{
314 		.procname	= "wmem_max",
315 		.data		= &sysctl_wmem_max,
316 		.maxlen		= sizeof(int),
317 		.mode		= 0644,
318 		.proc_handler	= proc_dointvec_minmax,
319 		.extra1		= &min_sndbuf,
320 	},
321 	{
322 		.procname	= "rmem_max",
323 		.data		= &sysctl_rmem_max,
324 		.maxlen		= sizeof(int),
325 		.mode		= 0644,
326 		.proc_handler	= proc_dointvec_minmax,
327 		.extra1		= &min_rcvbuf,
328 	},
329 	{
330 		.procname	= "wmem_default",
331 		.data		= &sysctl_wmem_default,
332 		.maxlen		= sizeof(int),
333 		.mode		= 0644,
334 		.proc_handler	= proc_dointvec_minmax,
335 		.extra1		= &min_sndbuf,
336 	},
337 	{
338 		.procname	= "rmem_default",
339 		.data		= &sysctl_rmem_default,
340 		.maxlen		= sizeof(int),
341 		.mode		= 0644,
342 		.proc_handler	= proc_dointvec_minmax,
343 		.extra1		= &min_rcvbuf,
344 	},
345 	{
346 		.procname	= "dev_weight",
347 		.data		= &weight_p,
348 		.maxlen		= sizeof(int),
349 		.mode		= 0644,
350 		.proc_handler	= proc_do_dev_weight,
351 	},
352 	{
353 		.procname	= "dev_weight_rx_bias",
354 		.data		= &dev_weight_rx_bias,
355 		.maxlen		= sizeof(int),
356 		.mode		= 0644,
357 		.proc_handler	= proc_do_dev_weight,
358 	},
359 	{
360 		.procname	= "dev_weight_tx_bias",
361 		.data		= &dev_weight_tx_bias,
362 		.maxlen		= sizeof(int),
363 		.mode		= 0644,
364 		.proc_handler	= proc_do_dev_weight,
365 	},
366 	{
367 		.procname	= "netdev_max_backlog",
368 		.data		= &netdev_max_backlog,
369 		.maxlen		= sizeof(int),
370 		.mode		= 0644,
371 		.proc_handler	= proc_dointvec
372 	},
373 	{
374 		.procname	= "netdev_rss_key",
375 		.data		= &netdev_rss_key,
376 		.maxlen		= sizeof(int),
377 		.mode		= 0444,
378 		.proc_handler	= proc_do_rss_key,
379 	},
380 #ifdef CONFIG_BPF_JIT
381 	{
382 		.procname	= "bpf_jit_enable",
383 		.data		= &bpf_jit_enable,
384 		.maxlen		= sizeof(int),
385 		.mode		= 0644,
386 		.proc_handler	= proc_dointvec_minmax_bpf_enable,
387 # ifdef CONFIG_BPF_JIT_ALWAYS_ON
388 		.extra1		= SYSCTL_ONE,
389 		.extra2		= SYSCTL_ONE,
390 # else
391 		.extra1		= SYSCTL_ZERO,
392 		.extra2		= &two,
393 # endif
394 	},
395 # ifdef CONFIG_HAVE_EBPF_JIT
396 	{
397 		.procname	= "bpf_jit_harden",
398 		.data		= &bpf_jit_harden,
399 		.maxlen		= sizeof(int),
400 		.mode		= 0600,
401 		.proc_handler	= proc_dointvec_minmax_bpf_restricted,
402 		.extra1		= SYSCTL_ZERO,
403 		.extra2		= &two,
404 	},
405 	{
406 		.procname	= "bpf_jit_kallsyms",
407 		.data		= &bpf_jit_kallsyms,
408 		.maxlen		= sizeof(int),
409 		.mode		= 0600,
410 		.proc_handler	= proc_dointvec_minmax_bpf_restricted,
411 		.extra1		= SYSCTL_ZERO,
412 		.extra2		= SYSCTL_ONE,
413 	},
414 # endif
415 	{
416 		.procname	= "bpf_jit_limit",
417 		.data		= &bpf_jit_limit,
418 		.maxlen		= sizeof(long),
419 		.mode		= 0600,
420 		.proc_handler	= proc_dolongvec_minmax_bpf_restricted,
421 		.extra1		= &long_one,
422 		.extra2		= &long_max,
423 	},
424 #endif
425 	{
426 		.procname	= "netdev_tstamp_prequeue",
427 		.data		= &netdev_tstamp_prequeue,
428 		.maxlen		= sizeof(int),
429 		.mode		= 0644,
430 		.proc_handler	= proc_dointvec
431 	},
432 	{
433 		.procname	= "message_cost",
434 		.data		= &net_ratelimit_state.interval,
435 		.maxlen		= sizeof(int),
436 		.mode		= 0644,
437 		.proc_handler	= proc_dointvec_jiffies,
438 	},
439 	{
440 		.procname	= "message_burst",
441 		.data		= &net_ratelimit_state.burst,
442 		.maxlen		= sizeof(int),
443 		.mode		= 0644,
444 		.proc_handler	= proc_dointvec,
445 	},
446 	{
447 		.procname	= "optmem_max",
448 		.data		= &sysctl_optmem_max,
449 		.maxlen		= sizeof(int),
450 		.mode		= 0644,
451 		.proc_handler	= proc_dointvec
452 	},
453 	{
454 		.procname	= "tstamp_allow_data",
455 		.data		= &sysctl_tstamp_allow_data,
456 		.maxlen		= sizeof(int),
457 		.mode		= 0644,
458 		.proc_handler	= proc_dointvec_minmax,
459 		.extra1		= SYSCTL_ZERO,
460 		.extra2		= SYSCTL_ONE
461 	},
462 #ifdef CONFIG_RPS
463 	{
464 		.procname	= "rps_sock_flow_entries",
465 		.maxlen		= sizeof(int),
466 		.mode		= 0644,
467 		.proc_handler	= rps_sock_flow_sysctl
468 	},
469 #endif
470 #ifdef CONFIG_NET_FLOW_LIMIT
471 	{
472 		.procname	= "flow_limit_cpu_bitmap",
473 		.mode		= 0644,
474 		.proc_handler	= flow_limit_cpu_sysctl
475 	},
476 	{
477 		.procname	= "flow_limit_table_len",
478 		.data		= &netdev_flow_limit_table_len,
479 		.maxlen		= sizeof(int),
480 		.mode		= 0644,
481 		.proc_handler	= flow_limit_table_len_sysctl
482 	},
483 #endif /* CONFIG_NET_FLOW_LIMIT */
484 #ifdef CONFIG_NET_RX_BUSY_POLL
485 	{
486 		.procname	= "busy_poll",
487 		.data		= &sysctl_net_busy_poll,
488 		.maxlen		= sizeof(unsigned int),
489 		.mode		= 0644,
490 		.proc_handler	= proc_dointvec_minmax,
491 		.extra1		= SYSCTL_ZERO,
492 	},
493 	{
494 		.procname	= "busy_read",
495 		.data		= &sysctl_net_busy_read,
496 		.maxlen		= sizeof(unsigned int),
497 		.mode		= 0644,
498 		.proc_handler	= proc_dointvec_minmax,
499 		.extra1		= SYSCTL_ZERO,
500 	},
501 #endif
502 #ifdef CONFIG_NET_SCHED
503 	{
504 		.procname	= "default_qdisc",
505 		.mode		= 0644,
506 		.maxlen		= IFNAMSIZ,
507 		.proc_handler	= set_default_qdisc
508 	},
509 #endif
510 	{
511 		.procname	= "netdev_budget",
512 		.data		= &netdev_budget,
513 		.maxlen		= sizeof(int),
514 		.mode		= 0644,
515 		.proc_handler	= proc_dointvec
516 	},
517 	{
518 		.procname	= "warnings",
519 		.data		= &net_msg_warn,
520 		.maxlen		= sizeof(int),
521 		.mode		= 0644,
522 		.proc_handler	= proc_dointvec
523 	},
524 	{
525 		.procname	= "max_skb_frags",
526 		.data		= &sysctl_max_skb_frags,
527 		.maxlen		= sizeof(int),
528 		.mode		= 0644,
529 		.proc_handler	= proc_dointvec_minmax,
530 		.extra1		= SYSCTL_ONE,
531 		.extra2		= &max_skb_frags,
532 	},
533 	{
534 		.procname	= "netdev_budget_usecs",
535 		.data		= &netdev_budget_usecs,
536 		.maxlen		= sizeof(unsigned int),
537 		.mode		= 0644,
538 		.proc_handler	= proc_dointvec_minmax,
539 		.extra1		= SYSCTL_ZERO,
540 	},
541 	{
542 		.procname	= "fb_tunnels_only_for_init_net",
543 		.data		= &sysctl_fb_tunnels_only_for_init_net,
544 		.maxlen		= sizeof(int),
545 		.mode		= 0644,
546 		.proc_handler	= proc_dointvec_minmax,
547 		.extra1		= SYSCTL_ZERO,
548 		.extra2		= &two,
549 	},
550 	{
551 		.procname	= "devconf_inherit_init_net",
552 		.data		= &sysctl_devconf_inherit_init_net,
553 		.maxlen		= sizeof(int),
554 		.mode		= 0644,
555 		.proc_handler	= proc_dointvec_minmax,
556 		.extra1		= SYSCTL_ZERO,
557 		.extra2		= &three,
558 	},
559 	{
560 		.procname	= "high_order_alloc_disable",
561 		.data		= &net_high_order_alloc_disable_key.key,
562 		.maxlen         = sizeof(net_high_order_alloc_disable_key),
563 		.mode		= 0644,
564 		.proc_handler	= proc_do_static_key,
565 	},
566 	{
567 		.procname	= "gro_normal_batch",
568 		.data		= &gro_normal_batch,
569 		.maxlen		= sizeof(unsigned int),
570 		.mode		= 0644,
571 		.proc_handler	= proc_dointvec_minmax,
572 		.extra1		= SYSCTL_ONE,
573 	},
574 	{
575 		.procname	= "netdev_unregister_timeout_secs",
576 		.data		= &netdev_unregister_timeout_secs,
577 		.maxlen		= sizeof(unsigned int),
578 		.mode		= 0644,
579 		.proc_handler	= proc_dointvec_minmax,
580 		.extra1		= SYSCTL_ONE,
581 		.extra2		= &int_3600,
582 	},
583 	{ }
584 };
585 
586 static struct ctl_table netns_core_table[] = {
587 	{
588 		.procname	= "somaxconn",
589 		.data		= &init_net.core.sysctl_somaxconn,
590 		.maxlen		= sizeof(int),
591 		.mode		= 0644,
592 		.extra1		= SYSCTL_ZERO,
593 		.proc_handler	= proc_dointvec_minmax
594 	},
595 	{ }
596 };
597 
598 static int __init fb_tunnels_only_for_init_net_sysctl_setup(char *str)
599 {
600 	/* fallback tunnels for initns only */
601 	if (!strncmp(str, "initns", 6))
602 		sysctl_fb_tunnels_only_for_init_net = 1;
603 	/* no fallback tunnels anywhere */
604 	else if (!strncmp(str, "none", 4))
605 		sysctl_fb_tunnels_only_for_init_net = 2;
606 
607 	return 1;
608 }
609 __setup("fb_tunnels=", fb_tunnels_only_for_init_net_sysctl_setup);
610 
611 static __net_init int sysctl_core_net_init(struct net *net)
612 {
613 	struct ctl_table *tbl;
614 
615 	tbl = netns_core_table;
616 	if (!net_eq(net, &init_net)) {
617 		tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
618 		if (tbl == NULL)
619 			goto err_dup;
620 
621 		tbl[0].data = &net->core.sysctl_somaxconn;
622 
623 		/* Don't export any sysctls to unprivileged users */
624 		if (net->user_ns != &init_user_ns) {
625 			tbl[0].procname = NULL;
626 		}
627 	}
628 
629 	net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
630 	if (net->core.sysctl_hdr == NULL)
631 		goto err_reg;
632 
633 	return 0;
634 
635 err_reg:
636 	if (tbl != netns_core_table)
637 		kfree(tbl);
638 err_dup:
639 	return -ENOMEM;
640 }
641 
642 static __net_exit void sysctl_core_net_exit(struct net *net)
643 {
644 	struct ctl_table *tbl;
645 
646 	tbl = net->core.sysctl_hdr->ctl_table_arg;
647 	unregister_net_sysctl_table(net->core.sysctl_hdr);
648 	BUG_ON(tbl == netns_core_table);
649 	kfree(tbl);
650 }
651 
652 static __net_initdata struct pernet_operations sysctl_core_ops = {
653 	.init = sysctl_core_net_init,
654 	.exit = sysctl_core_net_exit,
655 };
656 
657 static __init int sysctl_core_init(void)
658 {
659 	register_net_sysctl(&init_net, "net/core", net_core_table);
660 	return register_pernet_subsys(&sysctl_core_ops);
661 }
662 
663 fs_initcall(sysctl_core_init);
664