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