Linux Audio

Check our new training course

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