]> Pileus Git - ~andy/linux/blob - net/core/sysctl_net_core.c
arp: flush arp cache on IFF_NOARP change
[~andy/linux] / net / core / sysctl_net_core.c
1 /* -*- linux-c -*-
2  * sysctl_net_core.c: sysctl interface to net core subsystem.
3  *
4  * Begun April 1, 1996, Mike Shaver.
5  * Added /proc/sys/net/core directory entry (empty =) ). [MS]
6  */
7
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>
18
19 #include <net/ip.h>
20 #include <net/sock.h>
21 #include <net/net_ratelimit.h>
22
23 static int one = 1;
24
25 #ifdef CONFIG_RPS
26 static int rps_sock_flow_sysctl(ctl_table *table, int write,
27                                 void __user *buffer, size_t *lenp, loff_t *ppos)
28 {
29         unsigned int orig_size, size;
30         int ret, i;
31         ctl_table tmp = {
32                 .data = &size,
33                 .maxlen = sizeof(size),
34                 .mode = table->mode
35         };
36         struct rps_sock_flow_table *orig_sock_table, *sock_table;
37         static DEFINE_MUTEX(sock_flow_mutex);
38
39         mutex_lock(&sock_flow_mutex);
40
41         orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
42                                         lockdep_is_held(&sock_flow_mutex));
43         size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
44
45         ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
46
47         if (write) {
48                 if (size) {
49                         if (size > 1<<30) {
50                                 /* Enforce limit to prevent overflow */
51                                 mutex_unlock(&sock_flow_mutex);
52                                 return -EINVAL;
53                         }
54                         size = roundup_pow_of_two(size);
55                         if (size != orig_size) {
56                                 sock_table =
57                                     vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
58                                 if (!sock_table) {
59                                         mutex_unlock(&sock_flow_mutex);
60                                         return -ENOMEM;
61                                 }
62
63                                 sock_table->mask = size - 1;
64                         } else
65                                 sock_table = orig_sock_table;
66
67                         for (i = 0; i < size; i++)
68                                 sock_table->ents[i] = RPS_NO_CPU;
69                 } else
70                         sock_table = NULL;
71
72                 if (sock_table != orig_sock_table) {
73                         rcu_assign_pointer(rps_sock_flow_table, sock_table);
74                         if (sock_table)
75                                 static_key_slow_inc(&rps_needed);
76                         if (orig_sock_table) {
77                                 static_key_slow_dec(&rps_needed);
78                                 synchronize_rcu();
79                                 vfree(orig_sock_table);
80                         }
81                 }
82         }
83
84         mutex_unlock(&sock_flow_mutex);
85
86         return ret;
87 }
88 #endif /* CONFIG_RPS */
89
90 #ifdef CONFIG_NET_FLOW_LIMIT
91 static DEFINE_MUTEX(flow_limit_update_mutex);
92
93 static int flow_limit_cpu_sysctl(ctl_table *table, int write,
94                                  void __user *buffer, size_t *lenp,
95                                  loff_t *ppos)
96 {
97         struct sd_flow_limit *cur;
98         struct softnet_data *sd;
99         cpumask_var_t mask;
100         int i, len, ret = 0;
101
102         if (!alloc_cpumask_var(&mask, GFP_KERNEL))
103                 return -ENOMEM;
104
105         if (write) {
106                 ret = cpumask_parse_user(buffer, *lenp, mask);
107                 if (ret)
108                         goto done;
109
110                 mutex_lock(&flow_limit_update_mutex);
111                 len = sizeof(*cur) + netdev_flow_limit_table_len;
112                 for_each_possible_cpu(i) {
113                         sd = &per_cpu(softnet_data, i);
114                         cur = rcu_dereference_protected(sd->flow_limit,
115                                      lockdep_is_held(&flow_limit_update_mutex));
116                         if (cur && !cpumask_test_cpu(i, mask)) {
117                                 RCU_INIT_POINTER(sd->flow_limit, NULL);
118                                 synchronize_rcu();
119                                 kfree(cur);
120                         } else if (!cur && cpumask_test_cpu(i, mask)) {
121                                 cur = kzalloc(len, GFP_KERNEL);
122                                 if (!cur) {
123                                         /* not unwinding previous changes */
124                                         ret = -ENOMEM;
125                                         goto write_unlock;
126                                 }
127                                 cur->num_buckets = netdev_flow_limit_table_len;
128                                 rcu_assign_pointer(sd->flow_limit, cur);
129                         }
130                 }
131 write_unlock:
132                 mutex_unlock(&flow_limit_update_mutex);
133         } else {
134                 if (*ppos || !*lenp) {
135                         *lenp = 0;
136                         goto done;
137                 }
138
139                 cpumask_clear(mask);
140                 rcu_read_lock();
141                 for_each_possible_cpu(i) {
142                         sd = &per_cpu(softnet_data, i);
143                         if (rcu_dereference(sd->flow_limit))
144                                 cpumask_set_cpu(i, mask);
145                 }
146                 rcu_read_unlock();
147
148                 len = cpumask_scnprintf(buffer, *lenp, mask);
149                 *lenp = len + 1;
150                 *ppos += len + 1;
151         }
152
153 done:
154         free_cpumask_var(mask);
155         return ret;
156 }
157
158 static int flow_limit_table_len_sysctl(ctl_table *table, int write,
159                                        void __user *buffer, size_t *lenp,
160                                        loff_t *ppos)
161 {
162         unsigned int old, *ptr;
163         int ret;
164
165         mutex_lock(&flow_limit_update_mutex);
166
167         ptr = table->data;
168         old = *ptr;
169         ret = proc_dointvec(table, write, buffer, lenp, ppos);
170         if (!ret && write && !is_power_of_2(*ptr)) {
171                 *ptr = old;
172                 ret = -EINVAL;
173         }
174
175         mutex_unlock(&flow_limit_update_mutex);
176         return ret;
177 }
178 #endif /* CONFIG_NET_FLOW_LIMIT */
179
180 static struct ctl_table net_core_table[] = {
181 #ifdef CONFIG_NET
182         {
183                 .procname       = "wmem_max",
184                 .data           = &sysctl_wmem_max,
185                 .maxlen         = sizeof(int),
186                 .mode           = 0644,
187                 .proc_handler   = proc_dointvec_minmax,
188                 .extra1         = &one,
189         },
190         {
191                 .procname       = "rmem_max",
192                 .data           = &sysctl_rmem_max,
193                 .maxlen         = sizeof(int),
194                 .mode           = 0644,
195                 .proc_handler   = proc_dointvec_minmax,
196                 .extra1         = &one,
197         },
198         {
199                 .procname       = "wmem_default",
200                 .data           = &sysctl_wmem_default,
201                 .maxlen         = sizeof(int),
202                 .mode           = 0644,
203                 .proc_handler   = proc_dointvec_minmax,
204                 .extra1         = &one,
205         },
206         {
207                 .procname       = "rmem_default",
208                 .data           = &sysctl_rmem_default,
209                 .maxlen         = sizeof(int),
210                 .mode           = 0644,
211                 .proc_handler   = proc_dointvec_minmax,
212                 .extra1         = &one,
213         },
214         {
215                 .procname       = "dev_weight",
216                 .data           = &weight_p,
217                 .maxlen         = sizeof(int),
218                 .mode           = 0644,
219                 .proc_handler   = proc_dointvec
220         },
221         {
222                 .procname       = "netdev_max_backlog",
223                 .data           = &netdev_max_backlog,
224                 .maxlen         = sizeof(int),
225                 .mode           = 0644,
226                 .proc_handler   = proc_dointvec
227         },
228 #ifdef CONFIG_BPF_JIT
229         {
230                 .procname       = "bpf_jit_enable",
231                 .data           = &bpf_jit_enable,
232                 .maxlen         = sizeof(int),
233                 .mode           = 0644,
234                 .proc_handler   = proc_dointvec
235         },
236 #endif
237         {
238                 .procname       = "netdev_tstamp_prequeue",
239                 .data           = &netdev_tstamp_prequeue,
240                 .maxlen         = sizeof(int),
241                 .mode           = 0644,
242                 .proc_handler   = proc_dointvec
243         },
244         {
245                 .procname       = "message_cost",
246                 .data           = &net_ratelimit_state.interval,
247                 .maxlen         = sizeof(int),
248                 .mode           = 0644,
249                 .proc_handler   = proc_dointvec_jiffies,
250         },
251         {
252                 .procname       = "message_burst",
253                 .data           = &net_ratelimit_state.burst,
254                 .maxlen         = sizeof(int),
255                 .mode           = 0644,
256                 .proc_handler   = proc_dointvec,
257         },
258         {
259                 .procname       = "optmem_max",
260                 .data           = &sysctl_optmem_max,
261                 .maxlen         = sizeof(int),
262                 .mode           = 0644,
263                 .proc_handler   = proc_dointvec
264         },
265 #ifdef CONFIG_RPS
266         {
267                 .procname       = "rps_sock_flow_entries",
268                 .maxlen         = sizeof(int),
269                 .mode           = 0644,
270                 .proc_handler   = rps_sock_flow_sysctl
271         },
272 #endif
273 #ifdef CONFIG_NET_FLOW_LIMIT
274         {
275                 .procname       = "flow_limit_cpu_bitmap",
276                 .mode           = 0644,
277                 .proc_handler   = flow_limit_cpu_sysctl
278         },
279         {
280                 .procname       = "flow_limit_table_len",
281                 .data           = &netdev_flow_limit_table_len,
282                 .maxlen         = sizeof(int),
283                 .mode           = 0644,
284                 .proc_handler   = flow_limit_table_len_sysctl
285         },
286 #endif /* CONFIG_NET_FLOW_LIMIT */
287 #endif /* CONFIG_NET */
288         {
289                 .procname       = "netdev_budget",
290                 .data           = &netdev_budget,
291                 .maxlen         = sizeof(int),
292                 .mode           = 0644,
293                 .proc_handler   = proc_dointvec
294         },
295         {
296                 .procname       = "warnings",
297                 .data           = &net_msg_warn,
298                 .maxlen         = sizeof(int),
299                 .mode           = 0644,
300                 .proc_handler   = proc_dointvec
301         },
302         { }
303 };
304
305 static struct ctl_table netns_core_table[] = {
306         {
307                 .procname       = "somaxconn",
308                 .data           = &init_net.core.sysctl_somaxconn,
309                 .maxlen         = sizeof(int),
310                 .mode           = 0644,
311                 .proc_handler   = proc_dointvec
312         },
313         { }
314 };
315
316 static __net_init int sysctl_core_net_init(struct net *net)
317 {
318         struct ctl_table *tbl;
319
320         net->core.sysctl_somaxconn = SOMAXCONN;
321
322         tbl = netns_core_table;
323         if (!net_eq(net, &init_net)) {
324                 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
325                 if (tbl == NULL)
326                         goto err_dup;
327
328                 tbl[0].data = &net->core.sysctl_somaxconn;
329
330                 /* Don't export any sysctls to unprivileged users */
331                 if (net->user_ns != &init_user_ns) {
332                         tbl[0].procname = NULL;
333                 }
334         }
335
336         net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
337         if (net->core.sysctl_hdr == NULL)
338                 goto err_reg;
339
340         return 0;
341
342 err_reg:
343         if (tbl != netns_core_table)
344                 kfree(tbl);
345 err_dup:
346         return -ENOMEM;
347 }
348
349 static __net_exit void sysctl_core_net_exit(struct net *net)
350 {
351         struct ctl_table *tbl;
352
353         tbl = net->core.sysctl_hdr->ctl_table_arg;
354         unregister_net_sysctl_table(net->core.sysctl_hdr);
355         BUG_ON(tbl == netns_core_table);
356         kfree(tbl);
357 }
358
359 static __net_initdata struct pernet_operations sysctl_core_ops = {
360         .init = sysctl_core_net_init,
361         .exit = sysctl_core_net_exit,
362 };
363
364 static __init int sysctl_core_init(void)
365 {
366         register_net_sysctl(&init_net, "net/core", net_core_table);
367         return register_pernet_subsys(&sysctl_core_ops);
368 }
369
370 fs_initcall(sysctl_core_init);