Merge remote-tracking branch 'lsk/v3.10/topic/of' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / 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 zero = 0;
24 static int one = 1;
25 static int ushort_max = USHRT_MAX;
26
27 #ifdef CONFIG_RPS
28 static int rps_sock_flow_sysctl(ctl_table *table, int write,
29                                 void __user *buffer, size_t *lenp, loff_t *ppos)
30 {
31         unsigned int orig_size, size;
32         int ret, i;
33         ctl_table tmp = {
34                 .data = &size,
35                 .maxlen = sizeof(size),
36                 .mode = table->mode
37         };
38         struct rps_sock_flow_table *orig_sock_table, *sock_table;
39         static DEFINE_MUTEX(sock_flow_mutex);
40
41         mutex_lock(&sock_flow_mutex);
42
43         orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
44                                         lockdep_is_held(&sock_flow_mutex));
45         size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
46
47         ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
48
49         if (write) {
50                 if (size) {
51                         if (size > 1<<30) {
52                                 /* Enforce limit to prevent overflow */
53                                 mutex_unlock(&sock_flow_mutex);
54                                 return -EINVAL;
55                         }
56                         size = roundup_pow_of_two(size);
57                         if (size != orig_size) {
58                                 sock_table =
59                                     vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
60                                 if (!sock_table) {
61                                         mutex_unlock(&sock_flow_mutex);
62                                         return -ENOMEM;
63                                 }
64
65                                 sock_table->mask = size - 1;
66                         } else
67                                 sock_table = orig_sock_table;
68
69                         for (i = 0; i < size; i++)
70                                 sock_table->ents[i] = RPS_NO_CPU;
71                 } else
72                         sock_table = NULL;
73
74                 if (sock_table != orig_sock_table) {
75                         rcu_assign_pointer(rps_sock_flow_table, sock_table);
76                         if (sock_table)
77                                 static_key_slow_inc(&rps_needed);
78                         if (orig_sock_table) {
79                                 static_key_slow_dec(&rps_needed);
80                                 synchronize_rcu();
81                                 vfree(orig_sock_table);
82                         }
83                 }
84         }
85
86         mutex_unlock(&sock_flow_mutex);
87
88         return ret;
89 }
90 #endif /* CONFIG_RPS */
91
92 static struct ctl_table net_core_table[] = {
93 #ifdef CONFIG_NET
94         {
95                 .procname       = "wmem_max",
96                 .data           = &sysctl_wmem_max,
97                 .maxlen         = sizeof(int),
98                 .mode           = 0644,
99                 .proc_handler   = proc_dointvec_minmax,
100                 .extra1         = &one,
101         },
102         {
103                 .procname       = "rmem_max",
104                 .data           = &sysctl_rmem_max,
105                 .maxlen         = sizeof(int),
106                 .mode           = 0644,
107                 .proc_handler   = proc_dointvec_minmax,
108                 .extra1         = &one,
109         },
110         {
111                 .procname       = "wmem_default",
112                 .data           = &sysctl_wmem_default,
113                 .maxlen         = sizeof(int),
114                 .mode           = 0644,
115                 .proc_handler   = proc_dointvec_minmax,
116                 .extra1         = &one,
117         },
118         {
119                 .procname       = "rmem_default",
120                 .data           = &sysctl_rmem_default,
121                 .maxlen         = sizeof(int),
122                 .mode           = 0644,
123                 .proc_handler   = proc_dointvec_minmax,
124                 .extra1         = &one,
125         },
126         {
127                 .procname       = "dev_weight",
128                 .data           = &weight_p,
129                 .maxlen         = sizeof(int),
130                 .mode           = 0644,
131                 .proc_handler   = proc_dointvec
132         },
133         {
134                 .procname       = "netdev_max_backlog",
135                 .data           = &netdev_max_backlog,
136                 .maxlen         = sizeof(int),
137                 .mode           = 0644,
138                 .proc_handler   = proc_dointvec
139         },
140 #ifdef CONFIG_BPF_JIT
141         {
142                 .procname       = "bpf_jit_enable",
143                 .data           = &bpf_jit_enable,
144                 .maxlen         = sizeof(int),
145                 .mode           = 0644,
146                 .proc_handler   = proc_dointvec
147         },
148 #endif
149         {
150                 .procname       = "netdev_tstamp_prequeue",
151                 .data           = &netdev_tstamp_prequeue,
152                 .maxlen         = sizeof(int),
153                 .mode           = 0644,
154                 .proc_handler   = proc_dointvec
155         },
156         {
157                 .procname       = "message_cost",
158                 .data           = &net_ratelimit_state.interval,
159                 .maxlen         = sizeof(int),
160                 .mode           = 0644,
161                 .proc_handler   = proc_dointvec_jiffies,
162         },
163         {
164                 .procname       = "message_burst",
165                 .data           = &net_ratelimit_state.burst,
166                 .maxlen         = sizeof(int),
167                 .mode           = 0644,
168                 .proc_handler   = proc_dointvec,
169         },
170         {
171                 .procname       = "optmem_max",
172                 .data           = &sysctl_optmem_max,
173                 .maxlen         = sizeof(int),
174                 .mode           = 0644,
175                 .proc_handler   = proc_dointvec
176         },
177 #ifdef CONFIG_RPS
178         {
179                 .procname       = "rps_sock_flow_entries",
180                 .maxlen         = sizeof(int),
181                 .mode           = 0644,
182                 .proc_handler   = rps_sock_flow_sysctl
183         },
184 #endif
185 #endif /* CONFIG_NET */
186         {
187                 .procname       = "netdev_budget",
188                 .data           = &netdev_budget,
189                 .maxlen         = sizeof(int),
190                 .mode           = 0644,
191                 .proc_handler   = proc_dointvec
192         },
193         {
194                 .procname       = "warnings",
195                 .data           = &net_msg_warn,
196                 .maxlen         = sizeof(int),
197                 .mode           = 0644,
198                 .proc_handler   = proc_dointvec
199         },
200         { }
201 };
202
203 static struct ctl_table netns_core_table[] = {
204         {
205                 .procname       = "somaxconn",
206                 .data           = &init_net.core.sysctl_somaxconn,
207                 .maxlen         = sizeof(int),
208                 .mode           = 0644,
209                 .extra1         = &zero,
210                 .extra2         = &ushort_max,
211                 .proc_handler   = proc_dointvec_minmax
212         },
213         { }
214 };
215
216 static __net_init int sysctl_core_net_init(struct net *net)
217 {
218         struct ctl_table *tbl;
219
220         net->core.sysctl_somaxconn = SOMAXCONN;
221
222         tbl = netns_core_table;
223         if (!net_eq(net, &init_net)) {
224                 tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
225                 if (tbl == NULL)
226                         goto err_dup;
227
228                 tbl[0].data = &net->core.sysctl_somaxconn;
229
230                 /* Don't export any sysctls to unprivileged users */
231                 if (net->user_ns != &init_user_ns) {
232                         tbl[0].procname = NULL;
233                 }
234         }
235
236         net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
237         if (net->core.sysctl_hdr == NULL)
238                 goto err_reg;
239
240         return 0;
241
242 err_reg:
243         if (tbl != netns_core_table)
244                 kfree(tbl);
245 err_dup:
246         return -ENOMEM;
247 }
248
249 static __net_exit void sysctl_core_net_exit(struct net *net)
250 {
251         struct ctl_table *tbl;
252
253         tbl = net->core.sysctl_hdr->ctl_table_arg;
254         unregister_net_sysctl_table(net->core.sysctl_hdr);
255         BUG_ON(tbl == netns_core_table);
256         kfree(tbl);
257 }
258
259 static __net_initdata struct pernet_operations sysctl_core_ops = {
260         .init = sysctl_core_net_init,
261         .exit = sysctl_core_net_exit,
262 };
263
264 static __init int sysctl_core_init(void)
265 {
266         register_net_sysctl(&init_net, "net/core", net_core_table);
267         return register_pernet_subsys(&sysctl_core_ops);
268 }
269
270 fs_initcall(sysctl_core_init);