xref: /linux/net/mptcp/ctrl.c (revision 40ccd6aa3e2e05be93394e3cd560c718dedfcc77)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Multipath TCP
3  *
4  * Copyright (c) 2019, Tessares SA.
5  */
6 
7 #ifdef CONFIG_SYSCTL
8 #include <linux/sysctl.h>
9 #endif
10 
11 #include <net/net_namespace.h>
12 #include <net/netns/generic.h>
13 
14 #include "protocol.h"
15 
16 #define MPTCP_SYSCTL_PATH "net/mptcp"
17 
18 static int mptcp_pernet_id;
19 
20 #ifdef CONFIG_SYSCTL
21 static int mptcp_pm_type_max = __MPTCP_PM_TYPE_MAX;
22 #endif
23 
24 struct mptcp_pernet {
25 #ifdef CONFIG_SYSCTL
26 	struct ctl_table_header *ctl_table_hdr;
27 #endif
28 
29 	unsigned int add_addr_timeout;
30 	unsigned int close_timeout;
31 	unsigned int stale_loss_cnt;
32 	u8 mptcp_enabled;
33 	u8 checksum_enabled;
34 	u8 allow_join_initial_addr_port;
35 	u8 pm_type;
36 	char scheduler[MPTCP_SCHED_NAME_MAX];
37 };
38 
39 static struct mptcp_pernet *mptcp_get_pernet(const struct net *net)
40 {
41 	return net_generic(net, mptcp_pernet_id);
42 }
43 
44 int mptcp_is_enabled(const struct net *net)
45 {
46 	return mptcp_get_pernet(net)->mptcp_enabled;
47 }
48 
49 unsigned int mptcp_get_add_addr_timeout(const struct net *net)
50 {
51 	return mptcp_get_pernet(net)->add_addr_timeout;
52 }
53 
54 int mptcp_is_checksum_enabled(const struct net *net)
55 {
56 	return mptcp_get_pernet(net)->checksum_enabled;
57 }
58 
59 int mptcp_allow_join_id0(const struct net *net)
60 {
61 	return mptcp_get_pernet(net)->allow_join_initial_addr_port;
62 }
63 
64 unsigned int mptcp_stale_loss_cnt(const struct net *net)
65 {
66 	return mptcp_get_pernet(net)->stale_loss_cnt;
67 }
68 
69 unsigned int mptcp_close_timeout(const struct sock *sk)
70 {
71 	if (sock_flag(sk, SOCK_DEAD))
72 		return TCP_TIMEWAIT_LEN;
73 	return mptcp_get_pernet(sock_net(sk))->close_timeout;
74 }
75 
76 int mptcp_get_pm_type(const struct net *net)
77 {
78 	return mptcp_get_pernet(net)->pm_type;
79 }
80 
81 const char *mptcp_get_scheduler(const struct net *net)
82 {
83 	return mptcp_get_pernet(net)->scheduler;
84 }
85 
86 static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
87 {
88 	pernet->mptcp_enabled = 1;
89 	pernet->add_addr_timeout = TCP_RTO_MAX;
90 	pernet->close_timeout = TCP_TIMEWAIT_LEN;
91 	pernet->checksum_enabled = 0;
92 	pernet->allow_join_initial_addr_port = 1;
93 	pernet->stale_loss_cnt = 4;
94 	pernet->pm_type = MPTCP_PM_TYPE_KERNEL;
95 	strscpy(pernet->scheduler, "default", sizeof(pernet->scheduler));
96 }
97 
98 #ifdef CONFIG_SYSCTL
99 static int mptcp_set_scheduler(const struct net *net, const char *name)
100 {
101 	struct mptcp_pernet *pernet = mptcp_get_pernet(net);
102 	struct mptcp_sched_ops *sched;
103 	int ret = 0;
104 
105 	rcu_read_lock();
106 	sched = mptcp_sched_find(name);
107 	if (sched)
108 		strscpy(pernet->scheduler, name, MPTCP_SCHED_NAME_MAX);
109 	else
110 		ret = -ENOENT;
111 	rcu_read_unlock();
112 
113 	return ret;
114 }
115 
116 static int proc_scheduler(struct ctl_table *ctl, int write,
117 			  void *buffer, size_t *lenp, loff_t *ppos)
118 {
119 	const struct net *net = current->nsproxy->net_ns;
120 	char val[MPTCP_SCHED_NAME_MAX];
121 	struct ctl_table tbl = {
122 		.data = val,
123 		.maxlen = MPTCP_SCHED_NAME_MAX,
124 	};
125 	int ret;
126 
127 	strscpy(val, mptcp_get_scheduler(net), MPTCP_SCHED_NAME_MAX);
128 
129 	ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
130 	if (write && ret == 0)
131 		ret = mptcp_set_scheduler(net, val);
132 
133 	return ret;
134 }
135 
136 static int proc_available_schedulers(struct ctl_table *ctl,
137 				     int write, void *buffer,
138 				     size_t *lenp, loff_t *ppos)
139 {
140 	struct ctl_table tbl = { .maxlen = MPTCP_SCHED_BUF_MAX, };
141 	int ret;
142 
143 	tbl.data = kmalloc(tbl.maxlen, GFP_USER);
144 	if (!tbl.data)
145 		return -ENOMEM;
146 
147 	mptcp_get_available_schedulers(tbl.data, MPTCP_SCHED_BUF_MAX);
148 	ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
149 	kfree(tbl.data);
150 
151 	return ret;
152 }
153 
154 static struct ctl_table mptcp_sysctl_table[] = {
155 	{
156 		.procname = "enabled",
157 		.maxlen = sizeof(u8),
158 		.mode = 0644,
159 		/* users with CAP_NET_ADMIN or root (not and) can change this
160 		 * value, same as other sysctl or the 'net' tree.
161 		 */
162 		.proc_handler = proc_dou8vec_minmax,
163 		.extra1       = SYSCTL_ZERO,
164 		.extra2       = SYSCTL_ONE
165 	},
166 	{
167 		.procname = "add_addr_timeout",
168 		.maxlen = sizeof(unsigned int),
169 		.mode = 0644,
170 		.proc_handler = proc_dointvec_jiffies,
171 	},
172 	{
173 		.procname = "checksum_enabled",
174 		.maxlen = sizeof(u8),
175 		.mode = 0644,
176 		.proc_handler = proc_dou8vec_minmax,
177 		.extra1       = SYSCTL_ZERO,
178 		.extra2       = SYSCTL_ONE
179 	},
180 	{
181 		.procname = "allow_join_initial_addr_port",
182 		.maxlen = sizeof(u8),
183 		.mode = 0644,
184 		.proc_handler = proc_dou8vec_minmax,
185 		.extra1       = SYSCTL_ZERO,
186 		.extra2       = SYSCTL_ONE
187 	},
188 	{
189 		.procname = "stale_loss_cnt",
190 		.maxlen = sizeof(unsigned int),
191 		.mode = 0644,
192 		.proc_handler = proc_douintvec_minmax,
193 	},
194 	{
195 		.procname = "pm_type",
196 		.maxlen = sizeof(u8),
197 		.mode = 0644,
198 		.proc_handler = proc_dou8vec_minmax,
199 		.extra1       = SYSCTL_ZERO,
200 		.extra2       = &mptcp_pm_type_max
201 	},
202 	{
203 		.procname = "scheduler",
204 		.maxlen	= MPTCP_SCHED_NAME_MAX,
205 		.mode = 0644,
206 		.proc_handler = proc_scheduler,
207 	},
208 	{
209 		.procname = "available_schedulers",
210 		.maxlen	= MPTCP_SCHED_BUF_MAX,
211 		.mode = 0644,
212 		.proc_handler = proc_available_schedulers,
213 	},
214 	{
215 		.procname = "close_timeout",
216 		.maxlen = sizeof(unsigned int),
217 		.mode = 0644,
218 		.proc_handler = proc_dointvec_jiffies,
219 	},
220 };
221 
222 static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
223 {
224 	struct ctl_table_header *hdr;
225 	struct ctl_table *table;
226 
227 	table = mptcp_sysctl_table;
228 	if (!net_eq(net, &init_net)) {
229 		table = kmemdup(table, sizeof(mptcp_sysctl_table), GFP_KERNEL);
230 		if (!table)
231 			goto err_alloc;
232 	}
233 
234 	table[0].data = &pernet->mptcp_enabled;
235 	table[1].data = &pernet->add_addr_timeout;
236 	table[2].data = &pernet->checksum_enabled;
237 	table[3].data = &pernet->allow_join_initial_addr_port;
238 	table[4].data = &pernet->stale_loss_cnt;
239 	table[5].data = &pernet->pm_type;
240 	table[6].data = &pernet->scheduler;
241 	/* table[7] is for available_schedulers which is read-only info */
242 	table[8].data = &pernet->close_timeout;
243 
244 	hdr = register_net_sysctl_sz(net, MPTCP_SYSCTL_PATH, table,
245 				     ARRAY_SIZE(mptcp_sysctl_table));
246 	if (!hdr)
247 		goto err_reg;
248 
249 	pernet->ctl_table_hdr = hdr;
250 
251 	return 0;
252 
253 err_reg:
254 	if (!net_eq(net, &init_net))
255 		kfree(table);
256 err_alloc:
257 	return -ENOMEM;
258 }
259 
260 static void mptcp_pernet_del_table(struct mptcp_pernet *pernet)
261 {
262 	const struct ctl_table *table = pernet->ctl_table_hdr->ctl_table_arg;
263 
264 	unregister_net_sysctl_table(pernet->ctl_table_hdr);
265 
266 	kfree(table);
267 }
268 
269 #else
270 
271 static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
272 {
273 	return 0;
274 }
275 
276 static void mptcp_pernet_del_table(struct mptcp_pernet *pernet) {}
277 
278 #endif /* CONFIG_SYSCTL */
279 
280 static int __net_init mptcp_net_init(struct net *net)
281 {
282 	struct mptcp_pernet *pernet = mptcp_get_pernet(net);
283 
284 	mptcp_pernet_set_defaults(pernet);
285 
286 	return mptcp_pernet_new_table(net, pernet);
287 }
288 
289 /* Note: the callback will only be called per extra netns */
290 static void __net_exit mptcp_net_exit(struct net *net)
291 {
292 	struct mptcp_pernet *pernet = mptcp_get_pernet(net);
293 
294 	mptcp_pernet_del_table(pernet);
295 }
296 
297 static struct pernet_operations mptcp_pernet_ops = {
298 	.init = mptcp_net_init,
299 	.exit = mptcp_net_exit,
300 	.id = &mptcp_pernet_id,
301 	.size = sizeof(struct mptcp_pernet),
302 };
303 
304 void __init mptcp_init(void)
305 {
306 	mptcp_join_cookie_init();
307 	mptcp_proto_init();
308 
309 	if (register_pernet_subsys(&mptcp_pernet_ops) < 0)
310 		panic("Failed to register MPTCP pernet subsystem.\n");
311 }
312 
313 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
314 int __init mptcpv6_init(void)
315 {
316 	int err;
317 
318 	err = mptcp_proto_v6_init();
319 
320 	return err;
321 }
322 #endif
323