KEYS: add missing permission check for request_key() destination
[linux/fpc-iii.git] / net / core / sysctl_net_core.c
blobcd386d2fd0399149d715ac6d1115e22106b3f651
1 /* -*- linux-c -*-
2 * sysctl_net_core.c: sysctl interface to net core subsystem.
4 * Begun April 1, 1996, Mike Shaver.
5 * Added /proc/sys/net/core directory entry (empty =) ). [MS]
6 */
8 #include <linux/mm.h>
9 #include <linux/sysctl.h>
10 #include <linux/module.h>
11 #include <linux/socket.h>
12 #include <linux/netdevice.h>
13 #include <linux/ratelimit.h>
14 #include <linux/vmalloc.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/kmemleak.h>
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>
25 static int zero = 0;
26 static int one = 1;
27 static int ushort_max = USHRT_MAX;
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;
32 #ifdef CONFIG_RPS
33 static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
34 void __user *buffer, size_t *lenp, loff_t *ppos)
36 unsigned int orig_size, size;
37 int ret, i;
38 struct ctl_table tmp = {
39 .data = &size,
40 .maxlen = sizeof(size),
41 .mode = table->mode
43 struct rps_sock_flow_table *orig_sock_table, *sock_table;
44 static DEFINE_MUTEX(sock_flow_mutex);
46 mutex_lock(&sock_flow_mutex);
48 orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
49 lockdep_is_held(&sock_flow_mutex));
50 size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
52 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
54 if (write) {
55 if (size) {
56 if (size > 1<<30) {
57 /* Enforce limit to prevent overflow */
58 mutex_unlock(&sock_flow_mutex);
59 return -EINVAL;
61 size = roundup_pow_of_two(size);
62 if (size != orig_size) {
63 sock_table =
64 vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
65 if (!sock_table) {
66 mutex_unlock(&sock_flow_mutex);
67 return -ENOMEM;
70 sock_table->mask = size - 1;
71 } else
72 sock_table = orig_sock_table;
74 for (i = 0; i < size; i++)
75 sock_table->ents[i] = RPS_NO_CPU;
76 } else
77 sock_table = NULL;
79 if (sock_table != orig_sock_table) {
80 rcu_assign_pointer(rps_sock_flow_table, sock_table);
81 if (sock_table)
82 static_key_slow_inc(&rps_needed);
83 if (orig_sock_table) {
84 static_key_slow_dec(&rps_needed);
85 synchronize_rcu();
86 vfree(orig_sock_table);
91 mutex_unlock(&sock_flow_mutex);
93 return ret;
95 #endif /* CONFIG_RPS */
97 #ifdef CONFIG_NET_FLOW_LIMIT
98 static DEFINE_MUTEX(flow_limit_update_mutex);
100 static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
101 void __user *buffer, size_t *lenp,
102 loff_t *ppos)
104 struct sd_flow_limit *cur;
105 struct softnet_data *sd;
106 cpumask_var_t mask;
107 int i, len, ret = 0;
109 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
110 return -ENOMEM;
112 if (write) {
113 ret = cpumask_parse_user(buffer, *lenp, mask);
114 if (ret)
115 goto done;
117 mutex_lock(&flow_limit_update_mutex);
118 len = sizeof(*cur) + netdev_flow_limit_table_len;
119 for_each_possible_cpu(i) {
120 sd = &per_cpu(softnet_data, i);
121 cur = rcu_dereference_protected(sd->flow_limit,
122 lockdep_is_held(&flow_limit_update_mutex));
123 if (cur && !cpumask_test_cpu(i, mask)) {
124 RCU_INIT_POINTER(sd->flow_limit, NULL);
125 synchronize_rcu();
126 kfree(cur);
127 } else if (!cur && cpumask_test_cpu(i, mask)) {
128 cur = kzalloc_node(len, GFP_KERNEL,
129 cpu_to_node(i));
130 if (!cur) {
131 /* not unwinding previous changes */
132 ret = -ENOMEM;
133 goto write_unlock;
135 cur->num_buckets = netdev_flow_limit_table_len;
136 rcu_assign_pointer(sd->flow_limit, cur);
139 write_unlock:
140 mutex_unlock(&flow_limit_update_mutex);
141 } else {
142 char kbuf[128];
144 if (*ppos || !*lenp) {
145 *lenp = 0;
146 goto done;
149 cpumask_clear(mask);
150 rcu_read_lock();
151 for_each_possible_cpu(i) {
152 sd = &per_cpu(softnet_data, i);
153 if (rcu_dereference(sd->flow_limit))
154 cpumask_set_cpu(i, mask);
156 rcu_read_unlock();
158 len = min(sizeof(kbuf) - 1, *lenp);
159 len = cpumask_scnprintf(kbuf, len, mask);
160 if (!len) {
161 *lenp = 0;
162 goto done;
164 if (len < *lenp)
165 kbuf[len++] = '\n';
166 if (copy_to_user(buffer, kbuf, len)) {
167 ret = -EFAULT;
168 goto done;
170 *lenp = len;
171 *ppos += len;
174 done:
175 free_cpumask_var(mask);
176 return ret;
179 static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
180 void __user *buffer, size_t *lenp,
181 loff_t *ppos)
183 unsigned int old, *ptr;
184 int ret;
186 mutex_lock(&flow_limit_update_mutex);
188 ptr = table->data;
189 old = *ptr;
190 ret = proc_dointvec(table, write, buffer, lenp, ppos);
191 if (!ret && write && !is_power_of_2(*ptr)) {
192 *ptr = old;
193 ret = -EINVAL;
196 mutex_unlock(&flow_limit_update_mutex);
197 return ret;
199 #endif /* CONFIG_NET_FLOW_LIMIT */
201 #ifdef CONFIG_NET_SCHED
202 static int set_default_qdisc(struct ctl_table *table, int write,
203 void __user *buffer, size_t *lenp, loff_t *ppos)
205 char id[IFNAMSIZ];
206 struct ctl_table tbl = {
207 .data = id,
208 .maxlen = IFNAMSIZ,
210 int ret;
212 qdisc_get_default(id, IFNAMSIZ);
214 ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
215 if (write && ret == 0)
216 ret = qdisc_set_default(id);
217 return ret;
219 #endif
221 static struct ctl_table net_core_table[] = {
222 #ifdef CONFIG_NET
224 .procname = "wmem_max",
225 .data = &sysctl_wmem_max,
226 .maxlen = sizeof(int),
227 .mode = 0644,
228 .proc_handler = proc_dointvec_minmax,
229 .extra1 = &min_sndbuf,
232 .procname = "rmem_max",
233 .data = &sysctl_rmem_max,
234 .maxlen = sizeof(int),
235 .mode = 0644,
236 .proc_handler = proc_dointvec_minmax,
237 .extra1 = &min_rcvbuf,
240 .procname = "wmem_default",
241 .data = &sysctl_wmem_default,
242 .maxlen = sizeof(int),
243 .mode = 0644,
244 .proc_handler = proc_dointvec_minmax,
245 .extra1 = &min_sndbuf,
248 .procname = "rmem_default",
249 .data = &sysctl_rmem_default,
250 .maxlen = sizeof(int),
251 .mode = 0644,
252 .proc_handler = proc_dointvec_minmax,
253 .extra1 = &min_rcvbuf,
256 .procname = "dev_weight",
257 .data = &weight_p,
258 .maxlen = sizeof(int),
259 .mode = 0644,
260 .proc_handler = proc_dointvec
263 .procname = "netdev_max_backlog",
264 .data = &netdev_max_backlog,
265 .maxlen = sizeof(int),
266 .mode = 0644,
267 .proc_handler = proc_dointvec
269 #ifdef CONFIG_BPF_JIT
271 .procname = "bpf_jit_enable",
272 .data = &bpf_jit_enable,
273 .maxlen = sizeof(int),
274 .mode = 0644,
275 .proc_handler = proc_dointvec
277 #endif
279 .procname = "netdev_tstamp_prequeue",
280 .data = &netdev_tstamp_prequeue,
281 .maxlen = sizeof(int),
282 .mode = 0644,
283 .proc_handler = proc_dointvec
286 .procname = "message_cost",
287 .data = &net_ratelimit_state.interval,
288 .maxlen = sizeof(int),
289 .mode = 0644,
290 .proc_handler = proc_dointvec_jiffies,
293 .procname = "message_burst",
294 .data = &net_ratelimit_state.burst,
295 .maxlen = sizeof(int),
296 .mode = 0644,
297 .proc_handler = proc_dointvec,
300 .procname = "optmem_max",
301 .data = &sysctl_optmem_max,
302 .maxlen = sizeof(int),
303 .mode = 0644,
304 .proc_handler = proc_dointvec
306 #ifdef CONFIG_RPS
308 .procname = "rps_sock_flow_entries",
309 .maxlen = sizeof(int),
310 .mode = 0644,
311 .proc_handler = rps_sock_flow_sysctl
313 #endif
314 #ifdef CONFIG_NET_FLOW_LIMIT
316 .procname = "flow_limit_cpu_bitmap",
317 .mode = 0644,
318 .proc_handler = flow_limit_cpu_sysctl
321 .procname = "flow_limit_table_len",
322 .data = &netdev_flow_limit_table_len,
323 .maxlen = sizeof(int),
324 .mode = 0644,
325 .proc_handler = flow_limit_table_len_sysctl
327 #endif /* CONFIG_NET_FLOW_LIMIT */
328 #ifdef CONFIG_NET_RX_BUSY_POLL
330 .procname = "busy_poll",
331 .data = &sysctl_net_busy_poll,
332 .maxlen = sizeof(unsigned int),
333 .mode = 0644,
334 .proc_handler = proc_dointvec
337 .procname = "busy_read",
338 .data = &sysctl_net_busy_read,
339 .maxlen = sizeof(unsigned int),
340 .mode = 0644,
341 .proc_handler = proc_dointvec
343 #endif
344 #ifdef CONFIG_NET_SCHED
346 .procname = "default_qdisc",
347 .mode = 0644,
348 .maxlen = IFNAMSIZ,
349 .proc_handler = set_default_qdisc
351 #endif
352 #endif /* CONFIG_NET */
354 .procname = "netdev_budget",
355 .data = &netdev_budget,
356 .maxlen = sizeof(int),
357 .mode = 0644,
358 .proc_handler = proc_dointvec
361 .procname = "warnings",
362 .data = &net_msg_warn,
363 .maxlen = sizeof(int),
364 .mode = 0644,
365 .proc_handler = proc_dointvec
368 .procname = "max_skb_frags",
369 .data = &sysctl_max_skb_frags,
370 .maxlen = sizeof(int),
371 .mode = 0644,
372 .proc_handler = proc_dointvec_minmax,
373 .extra1 = &one,
374 .extra2 = &max_skb_frags,
379 static struct ctl_table netns_core_table[] = {
381 .procname = "somaxconn",
382 .data = &init_net.core.sysctl_somaxconn,
383 .maxlen = sizeof(int),
384 .mode = 0644,
385 .extra1 = &zero,
386 .extra2 = &ushort_max,
387 .proc_handler = proc_dointvec_minmax
392 static __net_init int sysctl_core_net_init(struct net *net)
394 struct ctl_table *tbl;
396 net->core.sysctl_somaxconn = SOMAXCONN;
398 tbl = netns_core_table;
399 if (!net_eq(net, &init_net)) {
400 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
401 if (tbl == NULL)
402 goto err_dup;
404 tbl[0].data = &net->core.sysctl_somaxconn;
406 /* Don't export any sysctls to unprivileged users */
407 if (net->user_ns != &init_user_ns) {
408 tbl[0].procname = NULL;
412 net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
413 if (net->core.sysctl_hdr == NULL)
414 goto err_reg;
416 return 0;
418 err_reg:
419 if (tbl != netns_core_table)
420 kfree(tbl);
421 err_dup:
422 return -ENOMEM;
425 static __net_exit void sysctl_core_net_exit(struct net *net)
427 struct ctl_table *tbl;
429 tbl = net->core.sysctl_hdr->ctl_table_arg;
430 unregister_net_sysctl_table(net->core.sysctl_hdr);
431 BUG_ON(tbl == netns_core_table);
432 kfree(tbl);
435 static __net_initdata struct pernet_operations sysctl_core_ops = {
436 .init = sysctl_core_net_init,
437 .exit = sysctl_core_net_exit,
440 static __init int sysctl_core_init(void)
442 register_net_sysctl(&init_net, "net/core", net_core_table);
443 return register_pernet_subsys(&sysctl_core_ops);
446 fs_initcall(sysctl_core_init);