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