xref: /openbmc/linux/net/core/sysctl_net_core.c (revision 2c6467d2)
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 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;
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 #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)
42 {
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
49 	};
50 	struct rps_sock_flow_table *orig_sock_table, *sock_table;
51 	static DEFINE_MUTEX(sock_flow_mutex);
52 
53 	mutex_lock(&sock_flow_mutex);
54 
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;
58 
59 	ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
60 
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;
67 			}
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;
75 				}
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;
80 
81 			for (i = 0; i < size; i++)
82 				sock_table->ents[i] = RPS_NO_CPU;
83 		} else
84 			sock_table = NULL;
85 
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);
91 			}
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);
97 			}
98 		}
99 	}
100 
101 	mutex_unlock(&sock_flow_mutex);
102 
103 	return ret;
104 }
105 #endif /* CONFIG_RPS */
106 
107 #ifdef CONFIG_NET_FLOW_LIMIT
108 static DEFINE_MUTEX(flow_limit_update_mutex);
109 
110 static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
111 				 void __user *buffer, size_t *lenp,
112 				 loff_t *ppos)
113 {
114 	struct sd_flow_limit *cur;
115 	struct softnet_data *sd;
116 	cpumask_var_t mask;
117 	int i, len, ret = 0;
118 
119 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
120 		return -ENOMEM;
121 
122 	if (write) {
123 		ret = cpumask_parse_user(buffer, *lenp, mask);
124 		if (ret)
125 			goto done;
126 
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;
144 				}
145 				cur->num_buckets = netdev_flow_limit_table_len;
146 				rcu_assign_pointer(sd->flow_limit, cur);
147 			}
148 		}
149 write_unlock:
150 		mutex_unlock(&flow_limit_update_mutex);
151 	} else {
152 		char kbuf[128];
153 
154 		if (*ppos || !*lenp) {
155 			*lenp = 0;
156 			goto done;
157 		}
158 
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);
165 		}
166 		rcu_read_unlock();
167 
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;
173 		}
174 		if (len < *lenp)
175 			kbuf[len++] = '\n';
176 		if (copy_to_user(buffer, kbuf, len)) {
177 			ret = -EFAULT;
178 			goto done;
179 		}
180 		*lenp = len;
181 		*ppos += len;
182 	}
183 
184 done:
185 	free_cpumask_var(mask);
186 	return ret;
187 }
188 
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)
192 {
193 	unsigned int old, *ptr;
194 	int ret;
195 
196 	mutex_lock(&flow_limit_update_mutex);
197 
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;
204 	}
205 
206 	mutex_unlock(&flow_limit_update_mutex);
207 	return ret;
208 }
209 #endif /* CONFIG_NET_FLOW_LIMIT */
210 
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)
214 {
215 	char id[IFNAMSIZ];
216 	struct ctl_table tbl = {
217 		.data = id,
218 		.maxlen = IFNAMSIZ,
219 	};
220 	int ret;
221 
222 	qdisc_get_default(id, IFNAMSIZ);
223 
224 	ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
225 	if (write && ret == 0)
226 		ret = qdisc_set_default(id);
227 	return ret;
228 }
229 #endif
230 
231 static int proc_do_dev_weight(struct ctl_table *table, int write,
232 			   void __user *buffer, size_t *lenp, loff_t *ppos)
233 {
234 	int ret;
235 
236 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
237 	if (ret != 0)
238 		return ret;
239 
240 	dev_rx_weight = weight_p * dev_weight_rx_bias;
241 	dev_tx_weight = weight_p * dev_weight_tx_bias;
242 
243 	return ret;
244 }
245 
246 static int proc_do_rss_key(struct ctl_table *table, int write,
247 			   void __user *buffer, size_t *lenp, loff_t *ppos)
248 {
249 	struct ctl_table fake_table;
250 	char buf[NETDEV_RSS_KEY_LEN * 3];
251 
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);
256 }
257 
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)
262 {
263 	int ret, jit_enable = *(int *)table->data;
264 	struct ctl_table tmp = *table;
265 
266 	if (write && !capable(CAP_SYS_ADMIN))
267 		return -EPERM;
268 
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())) {
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;
279 		}
280 	}
281 	return ret;
282 }
283 
284 static int
285 proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write,
286 				    void __user *buffer, size_t *lenp,
287 				    loff_t *ppos)
288 {
289 	if (!capable(CAP_SYS_ADMIN))
290 		return -EPERM;
291 
292 	return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
293 }
294 
295 static int
296 proc_dolongvec_minmax_bpf_restricted(struct ctl_table *table, int write,
297 				     void __user *buffer, size_t *lenp,
298 				     loff_t *ppos)
299 {
300 	if (!capable(CAP_SYS_ADMIN))
301 		return -EPERM;
302 
303 	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
304 }
305 #endif
306 
307 static struct ctl_table net_core_table[] = {
308 #ifdef CONFIG_NET
309 	{
310 		.procname	= "wmem_max",
311 		.data		= &sysctl_wmem_max,
312 		.maxlen		= sizeof(int),
313 		.mode		= 0644,
314 		.proc_handler	= proc_dointvec_minmax,
315 		.extra1		= &min_sndbuf,
316 	},
317 	{
318 		.procname	= "rmem_max",
319 		.data		= &sysctl_rmem_max,
320 		.maxlen		= sizeof(int),
321 		.mode		= 0644,
322 		.proc_handler	= proc_dointvec_minmax,
323 		.extra1		= &min_rcvbuf,
324 	},
325 	{
326 		.procname	= "wmem_default",
327 		.data		= &sysctl_wmem_default,
328 		.maxlen		= sizeof(int),
329 		.mode		= 0644,
330 		.proc_handler	= proc_dointvec_minmax,
331 		.extra1		= &min_sndbuf,
332 	},
333 	{
334 		.procname	= "rmem_default",
335 		.data		= &sysctl_rmem_default,
336 		.maxlen		= sizeof(int),
337 		.mode		= 0644,
338 		.proc_handler	= proc_dointvec_minmax,
339 		.extra1		= &min_rcvbuf,
340 	},
341 	{
342 		.procname	= "dev_weight",
343 		.data		= &weight_p,
344 		.maxlen		= sizeof(int),
345 		.mode		= 0644,
346 		.proc_handler	= proc_do_dev_weight,
347 	},
348 	{
349 		.procname	= "dev_weight_rx_bias",
350 		.data		= &dev_weight_rx_bias,
351 		.maxlen		= sizeof(int),
352 		.mode		= 0644,
353 		.proc_handler	= proc_do_dev_weight,
354 	},
355 	{
356 		.procname	= "dev_weight_tx_bias",
357 		.data		= &dev_weight_tx_bias,
358 		.maxlen		= sizeof(int),
359 		.mode		= 0644,
360 		.proc_handler	= proc_do_dev_weight,
361 	},
362 	{
363 		.procname	= "netdev_max_backlog",
364 		.data		= &netdev_max_backlog,
365 		.maxlen		= sizeof(int),
366 		.mode		= 0644,
367 		.proc_handler	= proc_dointvec
368 	},
369 	{
370 		.procname	= "netdev_rss_key",
371 		.data		= &netdev_rss_key,
372 		.maxlen		= sizeof(int),
373 		.mode		= 0444,
374 		.proc_handler	= proc_do_rss_key,
375 	},
376 #ifdef CONFIG_BPF_JIT
377 	{
378 		.procname	= "bpf_jit_enable",
379 		.data		= &bpf_jit_enable,
380 		.maxlen		= sizeof(int),
381 		.mode		= 0644,
382 		.proc_handler	= proc_dointvec_minmax_bpf_enable,
383 # ifdef CONFIG_BPF_JIT_ALWAYS_ON
384 		.extra1		= &one,
385 		.extra2		= &one,
386 # else
387 		.extra1		= &zero,
388 		.extra2		= &two,
389 # endif
390 	},
391 # ifdef CONFIG_HAVE_EBPF_JIT
392 	{
393 		.procname	= "bpf_jit_harden",
394 		.data		= &bpf_jit_harden,
395 		.maxlen		= sizeof(int),
396 		.mode		= 0600,
397 		.proc_handler	= proc_dointvec_minmax_bpf_restricted,
398 		.extra1		= &zero,
399 		.extra2		= &two,
400 	},
401 	{
402 		.procname	= "bpf_jit_kallsyms",
403 		.data		= &bpf_jit_kallsyms,
404 		.maxlen		= sizeof(int),
405 		.mode		= 0600,
406 		.proc_handler	= proc_dointvec_minmax_bpf_restricted,
407 		.extra1		= &zero,
408 		.extra2		= &one,
409 	},
410 # endif
411 	{
412 		.procname	= "bpf_jit_limit",
413 		.data		= &bpf_jit_limit,
414 		.maxlen		= sizeof(long),
415 		.mode		= 0600,
416 		.proc_handler	= proc_dolongvec_minmax_bpf_restricted,
417 		.extra1		= &long_one,
418 		.extra2		= &long_max,
419 	},
420 #endif
421 	{
422 		.procname	= "netdev_tstamp_prequeue",
423 		.data		= &netdev_tstamp_prequeue,
424 		.maxlen		= sizeof(int),
425 		.mode		= 0644,
426 		.proc_handler	= proc_dointvec
427 	},
428 	{
429 		.procname	= "message_cost",
430 		.data		= &net_ratelimit_state.interval,
431 		.maxlen		= sizeof(int),
432 		.mode		= 0644,
433 		.proc_handler	= proc_dointvec_jiffies,
434 	},
435 	{
436 		.procname	= "message_burst",
437 		.data		= &net_ratelimit_state.burst,
438 		.maxlen		= sizeof(int),
439 		.mode		= 0644,
440 		.proc_handler	= proc_dointvec,
441 	},
442 	{
443 		.procname	= "optmem_max",
444 		.data		= &sysctl_optmem_max,
445 		.maxlen		= sizeof(int),
446 		.mode		= 0644,
447 		.proc_handler	= proc_dointvec
448 	},
449 	{
450 		.procname	= "tstamp_allow_data",
451 		.data		= &sysctl_tstamp_allow_data,
452 		.maxlen		= sizeof(int),
453 		.mode		= 0644,
454 		.proc_handler	= proc_dointvec_minmax,
455 		.extra1		= &zero,
456 		.extra2		= &one
457 	},
458 #ifdef CONFIG_RPS
459 	{
460 		.procname	= "rps_sock_flow_entries",
461 		.maxlen		= sizeof(int),
462 		.mode		= 0644,
463 		.proc_handler	= rps_sock_flow_sysctl
464 	},
465 #endif
466 #ifdef CONFIG_NET_FLOW_LIMIT
467 	{
468 		.procname	= "flow_limit_cpu_bitmap",
469 		.mode		= 0644,
470 		.proc_handler	= flow_limit_cpu_sysctl
471 	},
472 	{
473 		.procname	= "flow_limit_table_len",
474 		.data		= &netdev_flow_limit_table_len,
475 		.maxlen		= sizeof(int),
476 		.mode		= 0644,
477 		.proc_handler	= flow_limit_table_len_sysctl
478 	},
479 #endif /* CONFIG_NET_FLOW_LIMIT */
480 #ifdef CONFIG_NET_RX_BUSY_POLL
481 	{
482 		.procname	= "busy_poll",
483 		.data		= &sysctl_net_busy_poll,
484 		.maxlen		= sizeof(unsigned int),
485 		.mode		= 0644,
486 		.proc_handler	= proc_dointvec_minmax,
487 		.extra1		= &zero,
488 	},
489 	{
490 		.procname	= "busy_read",
491 		.data		= &sysctl_net_busy_read,
492 		.maxlen		= sizeof(unsigned int),
493 		.mode		= 0644,
494 		.proc_handler	= proc_dointvec_minmax,
495 		.extra1		= &zero,
496 	},
497 #endif
498 #ifdef CONFIG_NET_SCHED
499 	{
500 		.procname	= "default_qdisc",
501 		.mode		= 0644,
502 		.maxlen		= IFNAMSIZ,
503 		.proc_handler	= set_default_qdisc
504 	},
505 #endif
506 #endif /* CONFIG_NET */
507 	{
508 		.procname	= "netdev_budget",
509 		.data		= &netdev_budget,
510 		.maxlen		= sizeof(int),
511 		.mode		= 0644,
512 		.proc_handler	= proc_dointvec
513 	},
514 	{
515 		.procname	= "warnings",
516 		.data		= &net_msg_warn,
517 		.maxlen		= sizeof(int),
518 		.mode		= 0644,
519 		.proc_handler	= proc_dointvec
520 	},
521 	{
522 		.procname	= "max_skb_frags",
523 		.data		= &sysctl_max_skb_frags,
524 		.maxlen		= sizeof(int),
525 		.mode		= 0644,
526 		.proc_handler	= proc_dointvec_minmax,
527 		.extra1		= &one,
528 		.extra2		= &max_skb_frags,
529 	},
530 	{
531 		.procname	= "netdev_budget_usecs",
532 		.data		= &netdev_budget_usecs,
533 		.maxlen		= sizeof(unsigned int),
534 		.mode		= 0644,
535 		.proc_handler	= proc_dointvec_minmax,
536 		.extra1		= &zero,
537 	},
538 	{
539 		.procname	= "fb_tunnels_only_for_init_net",
540 		.data		= &sysctl_fb_tunnels_only_for_init_net,
541 		.maxlen		= sizeof(int),
542 		.mode		= 0644,
543 		.proc_handler	= proc_dointvec_minmax,
544 		.extra1		= &zero,
545 		.extra2		= &one,
546 	},
547 	{ }
548 };
549 
550 static struct ctl_table netns_core_table[] = {
551 	{
552 		.procname	= "somaxconn",
553 		.data		= &init_net.core.sysctl_somaxconn,
554 		.maxlen		= sizeof(int),
555 		.mode		= 0644,
556 		.extra1		= &zero,
557 		.proc_handler	= proc_dointvec_minmax
558 	},
559 	{ }
560 };
561 
562 static __net_init int sysctl_core_net_init(struct net *net)
563 {
564 	struct ctl_table *tbl;
565 
566 	tbl = netns_core_table;
567 	if (!net_eq(net, &init_net)) {
568 		tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
569 		if (tbl == NULL)
570 			goto err_dup;
571 
572 		tbl[0].data = &net->core.sysctl_somaxconn;
573 
574 		/* Don't export any sysctls to unprivileged users */
575 		if (net->user_ns != &init_user_ns) {
576 			tbl[0].procname = NULL;
577 		}
578 	}
579 
580 	net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
581 	if (net->core.sysctl_hdr == NULL)
582 		goto err_reg;
583 
584 	return 0;
585 
586 err_reg:
587 	if (tbl != netns_core_table)
588 		kfree(tbl);
589 err_dup:
590 	return -ENOMEM;
591 }
592 
593 static __net_exit void sysctl_core_net_exit(struct net *net)
594 {
595 	struct ctl_table *tbl;
596 
597 	tbl = net->core.sysctl_hdr->ctl_table_arg;
598 	unregister_net_sysctl_table(net->core.sysctl_hdr);
599 	BUG_ON(tbl == netns_core_table);
600 	kfree(tbl);
601 }
602 
603 static __net_initdata struct pernet_operations sysctl_core_ops = {
604 	.init = sysctl_core_net_init,
605 	.exit = sysctl_core_net_exit,
606 };
607 
608 static __init int sysctl_core_init(void)
609 {
610 	register_net_sysctl(&init_net, "net/core", net_core_table);
611 	return register_pernet_subsys(&sysctl_core_ops);
612 }
613 
614 fs_initcall(sysctl_core_init);
615