xref: /linux/net/core/sysctl_net_core.c (revision 7bb377107c72a40ab7505341f8626c8eb79a0cb7)
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 __maybe_unused = 2;
26 static int min_sndbuf = SOCK_MIN_SNDBUF;
27 static int min_rcvbuf = SOCK_MIN_RCVBUF;
28 static int max_skb_frags = MAX_SKB_FRAGS;
29 static long long_one __maybe_unused = 1;
30 static long long_max __maybe_unused = LONG_MAX;
31 
32 static int net_msg_warn;	/* Unused, but still a sysctl */
33 
34 int sysctl_fb_tunnels_only_for_init_net __read_mostly = 0;
35 EXPORT_SYMBOL(sysctl_fb_tunnels_only_for_init_net);
36 
37 /* 0 - Keep current behavior:
38  *     IPv4: inherit all current settings from init_net
39  *     IPv6: reset all settings to default
40  * 1 - Both inherit all current settings from init_net
41  * 2 - Both reset all settings to default
42  */
43 int sysctl_devconf_inherit_init_net __read_mostly;
44 EXPORT_SYMBOL(sysctl_devconf_inherit_init_net);
45 
46 #ifdef CONFIG_RPS
47 static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
48 				void *buffer, size_t *lenp, loff_t *ppos)
49 {
50 	unsigned int orig_size, size;
51 	int ret, i;
52 	struct ctl_table tmp = {
53 		.data = &size,
54 		.maxlen = sizeof(size),
55 		.mode = table->mode
56 	};
57 	struct rps_sock_flow_table *orig_sock_table, *sock_table;
58 	static DEFINE_MUTEX(sock_flow_mutex);
59 
60 	mutex_lock(&sock_flow_mutex);
61 
62 	orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
63 					lockdep_is_held(&sock_flow_mutex));
64 	size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
65 
66 	ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
67 
68 	if (write) {
69 		if (size) {
70 			if (size > 1<<29) {
71 				/* Enforce limit to prevent overflow */
72 				mutex_unlock(&sock_flow_mutex);
73 				return -EINVAL;
74 			}
75 			size = roundup_pow_of_two(size);
76 			if (size != orig_size) {
77 				sock_table =
78 				    vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
79 				if (!sock_table) {
80 					mutex_unlock(&sock_flow_mutex);
81 					return -ENOMEM;
82 				}
83 				rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1;
84 				sock_table->mask = size - 1;
85 			} else
86 				sock_table = orig_sock_table;
87 
88 			for (i = 0; i < size; i++)
89 				sock_table->ents[i] = RPS_NO_CPU;
90 		} else
91 			sock_table = NULL;
92 
93 		if (sock_table != orig_sock_table) {
94 			rcu_assign_pointer(rps_sock_flow_table, sock_table);
95 			if (sock_table) {
96 				static_branch_inc(&rps_needed);
97 				static_branch_inc(&rfs_needed);
98 			}
99 			if (orig_sock_table) {
100 				static_branch_dec(&rps_needed);
101 				static_branch_dec(&rfs_needed);
102 				synchronize_rcu();
103 				vfree(orig_sock_table);
104 			}
105 		}
106 	}
107 
108 	mutex_unlock(&sock_flow_mutex);
109 
110 	return ret;
111 }
112 #endif /* CONFIG_RPS */
113 
114 #ifdef CONFIG_NET_FLOW_LIMIT
115 static DEFINE_MUTEX(flow_limit_update_mutex);
116 
117 static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
118 				 void *buffer, size_t *lenp, loff_t *ppos)
119 {
120 	struct sd_flow_limit *cur;
121 	struct softnet_data *sd;
122 	cpumask_var_t mask;
123 	int i, len, ret = 0;
124 
125 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
126 		return -ENOMEM;
127 
128 	if (write) {
129 		ret = cpumask_parse_user(buffer, *lenp, mask);
130 		if (ret)
131 			goto done;
132 
133 		mutex_lock(&flow_limit_update_mutex);
134 		len = sizeof(*cur) + netdev_flow_limit_table_len;
135 		for_each_possible_cpu(i) {
136 			sd = &per_cpu(softnet_data, i);
137 			cur = rcu_dereference_protected(sd->flow_limit,
138 				     lockdep_is_held(&flow_limit_update_mutex));
139 			if (cur && !cpumask_test_cpu(i, mask)) {
140 				RCU_INIT_POINTER(sd->flow_limit, NULL);
141 				synchronize_rcu();
142 				kfree(cur);
143 			} else if (!cur && cpumask_test_cpu(i, mask)) {
144 				cur = kzalloc_node(len, GFP_KERNEL,
145 						   cpu_to_node(i));
146 				if (!cur) {
147 					/* not unwinding previous changes */
148 					ret = -ENOMEM;
149 					goto write_unlock;
150 				}
151 				cur->num_buckets = netdev_flow_limit_table_len;
152 				rcu_assign_pointer(sd->flow_limit, cur);
153 			}
154 		}
155 write_unlock:
156 		mutex_unlock(&flow_limit_update_mutex);
157 	} else {
158 		char kbuf[128];
159 
160 		if (*ppos || !*lenp) {
161 			*lenp = 0;
162 			goto done;
163 		}
164 
165 		cpumask_clear(mask);
166 		rcu_read_lock();
167 		for_each_possible_cpu(i) {
168 			sd = &per_cpu(softnet_data, i);
169 			if (rcu_dereference(sd->flow_limit))
170 				cpumask_set_cpu(i, mask);
171 		}
172 		rcu_read_unlock();
173 
174 		len = min(sizeof(kbuf) - 1, *lenp);
175 		len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask));
176 		if (!len) {
177 			*lenp = 0;
178 			goto done;
179 		}
180 		if (len < *lenp)
181 			kbuf[len++] = '\n';
182 		memcpy(buffer, kbuf, len);
183 		*lenp = len;
184 		*ppos += len;
185 	}
186 
187 done:
188 	free_cpumask_var(mask);
189 	return ret;
190 }
191 
192 static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
193 				       void *buffer, size_t *lenp, loff_t *ppos)
194 {
195 	unsigned int old, *ptr;
196 	int ret;
197 
198 	mutex_lock(&flow_limit_update_mutex);
199 
200 	ptr = table->data;
201 	old = *ptr;
202 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
203 	if (!ret && write && !is_power_of_2(*ptr)) {
204 		*ptr = old;
205 		ret = -EINVAL;
206 	}
207 
208 	mutex_unlock(&flow_limit_update_mutex);
209 	return ret;
210 }
211 #endif /* CONFIG_NET_FLOW_LIMIT */
212 
213 #ifdef CONFIG_NET_SCHED
214 static int set_default_qdisc(struct ctl_table *table, int write,
215 			     void *buffer, size_t *lenp, loff_t *ppos)
216 {
217 	char id[IFNAMSIZ];
218 	struct ctl_table tbl = {
219 		.data = id,
220 		.maxlen = IFNAMSIZ,
221 	};
222 	int ret;
223 
224 	qdisc_get_default(id, IFNAMSIZ);
225 
226 	ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
227 	if (write && ret == 0)
228 		ret = qdisc_set_default(id);
229 	return ret;
230 }
231 #endif
232 
233 static int proc_do_dev_weight(struct ctl_table *table, int write,
234 			   void *buffer, size_t *lenp, loff_t *ppos)
235 {
236 	int ret;
237 
238 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
239 	if (ret != 0)
240 		return ret;
241 
242 	dev_rx_weight = weight_p * dev_weight_rx_bias;
243 	dev_tx_weight = weight_p * dev_weight_tx_bias;
244 
245 	return ret;
246 }
247 
248 static int proc_do_rss_key(struct ctl_table *table, int write,
249 			   void *buffer, size_t *lenp, loff_t *ppos)
250 {
251 	struct ctl_table fake_table;
252 	char buf[NETDEV_RSS_KEY_LEN * 3];
253 
254 	snprintf(buf, sizeof(buf), "%*phC", NETDEV_RSS_KEY_LEN, netdev_rss_key);
255 	fake_table.data = buf;
256 	fake_table.maxlen = sizeof(buf);
257 	return proc_dostring(&fake_table, write, buffer, lenp, ppos);
258 }
259 
260 #ifdef CONFIG_BPF_JIT
261 static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write,
262 					   void *buffer, size_t *lenp,
263 					   loff_t *ppos)
264 {
265 	int ret, jit_enable = *(int *)table->data;
266 	struct ctl_table tmp = *table;
267 
268 	if (write && !capable(CAP_SYS_ADMIN))
269 		return -EPERM;
270 
271 	tmp.data = &jit_enable;
272 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
273 	if (write && !ret) {
274 		if (jit_enable < 2 ||
275 		    (jit_enable == 2 && bpf_dump_raw_ok())) {
276 			*(int *)table->data = jit_enable;
277 			if (jit_enable == 2)
278 				pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n");
279 		} else {
280 			ret = -EPERM;
281 		}
282 	}
283 	return ret;
284 }
285 
286 # ifdef CONFIG_HAVE_EBPF_JIT
287 static int
288 proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write,
289 				    void *buffer, size_t *lenp, loff_t *ppos)
290 {
291 	if (!capable(CAP_SYS_ADMIN))
292 		return -EPERM;
293 
294 	return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
295 }
296 # endif /* CONFIG_HAVE_EBPF_JIT */
297 
298 static int
299 proc_dolongvec_minmax_bpf_restricted(struct ctl_table *table, int write,
300 				     void *buffer, size_t *lenp, loff_t *ppos)
301 {
302 	if (!capable(CAP_SYS_ADMIN))
303 		return -EPERM;
304 
305 	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
306 }
307 #endif
308 
309 static struct ctl_table net_core_table[] = {
310 #ifdef CONFIG_NET
311 	{
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,
318 	},
319 	{
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,
326 	},
327 	{
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,
334 	},
335 	{
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,
342 	},
343 	{
344 		.procname	= "dev_weight",
345 		.data		= &weight_p,
346 		.maxlen		= sizeof(int),
347 		.mode		= 0644,
348 		.proc_handler	= proc_do_dev_weight,
349 	},
350 	{
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,
356 	},
357 	{
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,
363 	},
364 	{
365 		.procname	= "netdev_max_backlog",
366 		.data		= &netdev_max_backlog,
367 		.maxlen		= sizeof(int),
368 		.mode		= 0644,
369 		.proc_handler	= proc_dointvec
370 	},
371 	{
372 		.procname	= "netdev_rss_key",
373 		.data		= &netdev_rss_key,
374 		.maxlen		= sizeof(int),
375 		.mode		= 0444,
376 		.proc_handler	= proc_do_rss_key,
377 	},
378 #ifdef CONFIG_BPF_JIT
379 	{
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		= SYSCTL_ONE,
387 		.extra2		= SYSCTL_ONE,
388 # else
389 		.extra1		= SYSCTL_ZERO,
390 		.extra2		= &two,
391 # endif
392 	},
393 # ifdef CONFIG_HAVE_EBPF_JIT
394 	{
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		= SYSCTL_ZERO,
401 		.extra2		= &two,
402 	},
403 	{
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		= SYSCTL_ZERO,
410 		.extra2		= SYSCTL_ONE,
411 	},
412 # endif
413 	{
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,
421 	},
422 #endif
423 	{
424 		.procname	= "netdev_tstamp_prequeue",
425 		.data		= &netdev_tstamp_prequeue,
426 		.maxlen		= sizeof(int),
427 		.mode		= 0644,
428 		.proc_handler	= proc_dointvec
429 	},
430 	{
431 		.procname	= "message_cost",
432 		.data		= &net_ratelimit_state.interval,
433 		.maxlen		= sizeof(int),
434 		.mode		= 0644,
435 		.proc_handler	= proc_dointvec_jiffies,
436 	},
437 	{
438 		.procname	= "message_burst",
439 		.data		= &net_ratelimit_state.burst,
440 		.maxlen		= sizeof(int),
441 		.mode		= 0644,
442 		.proc_handler	= proc_dointvec,
443 	},
444 	{
445 		.procname	= "optmem_max",
446 		.data		= &sysctl_optmem_max,
447 		.maxlen		= sizeof(int),
448 		.mode		= 0644,
449 		.proc_handler	= proc_dointvec
450 	},
451 	{
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		= SYSCTL_ZERO,
458 		.extra2		= SYSCTL_ONE
459 	},
460 #ifdef CONFIG_RPS
461 	{
462 		.procname	= "rps_sock_flow_entries",
463 		.maxlen		= sizeof(int),
464 		.mode		= 0644,
465 		.proc_handler	= rps_sock_flow_sysctl
466 	},
467 #endif
468 #ifdef CONFIG_NET_FLOW_LIMIT
469 	{
470 		.procname	= "flow_limit_cpu_bitmap",
471 		.mode		= 0644,
472 		.proc_handler	= flow_limit_cpu_sysctl
473 	},
474 	{
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
480 	},
481 #endif /* CONFIG_NET_FLOW_LIMIT */
482 #ifdef CONFIG_NET_RX_BUSY_POLL
483 	{
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		= SYSCTL_ZERO,
490 	},
491 	{
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		= SYSCTL_ZERO,
498 	},
499 #endif
500 #ifdef CONFIG_NET_SCHED
501 	{
502 		.procname	= "default_qdisc",
503 		.mode		= 0644,
504 		.maxlen		= IFNAMSIZ,
505 		.proc_handler	= set_default_qdisc
506 	},
507 #endif
508 #endif /* CONFIG_NET */
509 	{
510 		.procname	= "netdev_budget",
511 		.data		= &netdev_budget,
512 		.maxlen		= sizeof(int),
513 		.mode		= 0644,
514 		.proc_handler	= proc_dointvec
515 	},
516 	{
517 		.procname	= "warnings",
518 		.data		= &net_msg_warn,
519 		.maxlen		= sizeof(int),
520 		.mode		= 0644,
521 		.proc_handler	= proc_dointvec
522 	},
523 	{
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		= SYSCTL_ONE,
530 		.extra2		= &max_skb_frags,
531 	},
532 	{
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		= SYSCTL_ZERO,
539 	},
540 	{
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		= SYSCTL_ZERO,
547 		.extra2		= SYSCTL_ONE,
548 	},
549 	{
550 		.procname	= "devconf_inherit_init_net",
551 		.data		= &sysctl_devconf_inherit_init_net,
552 		.maxlen		= sizeof(int),
553 		.mode		= 0644,
554 		.proc_handler	= proc_dointvec_minmax,
555 		.extra1		= SYSCTL_ZERO,
556 		.extra2		= &two,
557 	},
558 	{
559 		.procname	= "high_order_alloc_disable",
560 		.data		= &net_high_order_alloc_disable_key.key,
561 		.maxlen         = sizeof(net_high_order_alloc_disable_key),
562 		.mode		= 0644,
563 		.proc_handler	= proc_do_static_key,
564 	},
565 	{
566 		.procname	= "gro_normal_batch",
567 		.data		= &gro_normal_batch,
568 		.maxlen		= sizeof(unsigned int),
569 		.mode		= 0644,
570 		.proc_handler	= proc_dointvec_minmax,
571 		.extra1		= SYSCTL_ONE,
572 	},
573 	{ }
574 };
575 
576 static struct ctl_table netns_core_table[] = {
577 	{
578 		.procname	= "somaxconn",
579 		.data		= &init_net.core.sysctl_somaxconn,
580 		.maxlen		= sizeof(int),
581 		.mode		= 0644,
582 		.extra1		= SYSCTL_ZERO,
583 		.proc_handler	= proc_dointvec_minmax
584 	},
585 	{ }
586 };
587 
588 static __net_init int sysctl_core_net_init(struct net *net)
589 {
590 	struct ctl_table *tbl;
591 
592 	tbl = netns_core_table;
593 	if (!net_eq(net, &init_net)) {
594 		tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
595 		if (tbl == NULL)
596 			goto err_dup;
597 
598 		tbl[0].data = &net->core.sysctl_somaxconn;
599 
600 		/* Don't export any sysctls to unprivileged users */
601 		if (net->user_ns != &init_user_ns) {
602 			tbl[0].procname = NULL;
603 		}
604 	}
605 
606 	net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
607 	if (net->core.sysctl_hdr == NULL)
608 		goto err_reg;
609 
610 	return 0;
611 
612 err_reg:
613 	if (tbl != netns_core_table)
614 		kfree(tbl);
615 err_dup:
616 	return -ENOMEM;
617 }
618 
619 static __net_exit void sysctl_core_net_exit(struct net *net)
620 {
621 	struct ctl_table *tbl;
622 
623 	tbl = net->core.sysctl_hdr->ctl_table_arg;
624 	unregister_net_sysctl_table(net->core.sysctl_hdr);
625 	BUG_ON(tbl == netns_core_table);
626 	kfree(tbl);
627 }
628 
629 static __net_initdata struct pernet_operations sysctl_core_ops = {
630 	.init = sysctl_core_net_init,
631 	.exit = sysctl_core_net_exit,
632 };
633 
634 static __init int sysctl_core_init(void)
635 {
636 	register_net_sysctl(&init_net, "net/core", net_core_table);
637 	return register_pernet_subsys(&sysctl_core_ops);
638 }
639 
640 fs_initcall(sysctl_core_init);
641