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
18 #include <net/ip.h>
19 #include <net/sock.h>
20 #include <net/net_ratelimit.h>
21
22 static int zero = 0;
23 static int ushort_max = USHRT_MAX;
24
25 #ifdef CONFIG_RPS
rps_sock_flow_sysctl(ctl_table * table,int write,void __user * buffer,size_t * lenp,loff_t * ppos)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 static struct ctl_table net_core_table[] = {
91 #ifdef CONFIG_NET
92 {
93 .procname = "wmem_max",
94 .data = &sysctl_wmem_max,
95 .maxlen = sizeof(int),
96 .mode = 0644,
97 .proc_handler = proc_dointvec
98 },
99 {
100 .procname = "rmem_max",
101 .data = &sysctl_rmem_max,
102 .maxlen = sizeof(int),
103 .mode = 0644,
104 .proc_handler = proc_dointvec
105 },
106 {
107 .procname = "wmem_default",
108 .data = &sysctl_wmem_default,
109 .maxlen = sizeof(int),
110 .mode = 0644,
111 .proc_handler = proc_dointvec
112 },
113 {
114 .procname = "rmem_default",
115 .data = &sysctl_rmem_default,
116 .maxlen = sizeof(int),
117 .mode = 0644,
118 .proc_handler = proc_dointvec
119 },
120 {
121 .procname = "dev_weight",
122 .data = &weight_p,
123 .maxlen = sizeof(int),
124 .mode = 0644,
125 .proc_handler = proc_dointvec
126 },
127 {
128 .procname = "netdev_max_backlog",
129 .data = &netdev_max_backlog,
130 .maxlen = sizeof(int),
131 .mode = 0644,
132 .proc_handler = proc_dointvec
133 },
134 #ifdef CONFIG_BPF_JIT
135 {
136 .procname = "bpf_jit_enable",
137 .data = &bpf_jit_enable,
138 .maxlen = sizeof(int),
139 .mode = 0644,
140 .proc_handler = proc_dointvec
141 },
142 #endif
143 {
144 .procname = "netdev_tstamp_prequeue",
145 .data = &netdev_tstamp_prequeue,
146 .maxlen = sizeof(int),
147 .mode = 0644,
148 .proc_handler = proc_dointvec
149 },
150 {
151 .procname = "message_cost",
152 .data = &net_ratelimit_state.interval,
153 .maxlen = sizeof(int),
154 .mode = 0644,
155 .proc_handler = proc_dointvec_jiffies,
156 },
157 {
158 .procname = "message_burst",
159 .data = &net_ratelimit_state.burst,
160 .maxlen = sizeof(int),
161 .mode = 0644,
162 .proc_handler = proc_dointvec,
163 },
164 {
165 .procname = "optmem_max",
166 .data = &sysctl_optmem_max,
167 .maxlen = sizeof(int),
168 .mode = 0644,
169 .proc_handler = proc_dointvec
170 },
171 #ifdef CONFIG_RPS
172 {
173 .procname = "rps_sock_flow_entries",
174 .maxlen = sizeof(int),
175 .mode = 0644,
176 .proc_handler = rps_sock_flow_sysctl
177 },
178 #endif
179 #endif /* CONFIG_NET */
180 {
181 .procname = "netdev_budget",
182 .data = &netdev_budget,
183 .maxlen = sizeof(int),
184 .mode = 0644,
185 .proc_handler = proc_dointvec
186 },
187 {
188 .procname = "warnings",
189 .data = &net_msg_warn,
190 .maxlen = sizeof(int),
191 .mode = 0644,
192 .proc_handler = proc_dointvec
193 },
194 { }
195 };
196
197 static struct ctl_table netns_core_table[] = {
198 {
199 .procname = "somaxconn",
200 .data = &init_net.core.sysctl_somaxconn,
201 .maxlen = sizeof(int),
202 .mode = 0644,
203 .extra1 = &zero,
204 .extra2 = &ushort_max,
205 .proc_handler = proc_dointvec_minmax
206 },
207 { }
208 };
209
210 __net_initdata struct ctl_path net_core_path[] = {
211 { .procname = "net", },
212 { .procname = "core", },
213 { },
214 };
215
sysctl_core_net_init(struct net * net)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
231 net->core.sysctl_hdr = register_net_sysctl_table(net,
232 net_core_path, tbl);
233 if (net->core.sysctl_hdr == NULL)
234 goto err_reg;
235
236 return 0;
237
238 err_reg:
239 if (tbl != netns_core_table)
240 kfree(tbl);
241 err_dup:
242 return -ENOMEM;
243 }
244
sysctl_core_net_exit(struct net * net)245 static __net_exit void sysctl_core_net_exit(struct net *net)
246 {
247 struct ctl_table *tbl;
248
249 tbl = net->core.sysctl_hdr->ctl_table_arg;
250 unregister_net_sysctl_table(net->core.sysctl_hdr);
251 BUG_ON(tbl == netns_core_table);
252 kfree(tbl);
253 }
254
255 static __net_initdata struct pernet_operations sysctl_core_ops = {
256 .init = sysctl_core_net_init,
257 .exit = sysctl_core_net_exit,
258 };
259
sysctl_core_init(void)260 static __init int sysctl_core_init(void)
261 {
262 static struct ctl_table empty[1];
263
264 register_sysctl_paths(net_core_path, empty);
265 register_net_sysctl_rotable(net_core_path, net_core_table);
266 return register_pernet_subsys(&sysctl_core_ops);
267 }
268
269 fs_initcall(sysctl_core_init);
270