xref: /linux/net/mptcp/ctrl.c (revision 8f5b5f78113e881cb8570c961b0dc42b218a1b9e)
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 	strcpy(pernet->scheduler, "default");
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 struct ctl_table mptcp_sysctl_table[] = {
137 	{
138 		.procname = "enabled",
139 		.maxlen = sizeof(u8),
140 		.mode = 0644,
141 		/* users with CAP_NET_ADMIN or root (not and) can change this
142 		 * value, same as other sysctl or the 'net' tree.
143 		 */
144 		.proc_handler = proc_dou8vec_minmax,
145 		.extra1       = SYSCTL_ZERO,
146 		.extra2       = SYSCTL_ONE
147 	},
148 	{
149 		.procname = "add_addr_timeout",
150 		.maxlen = sizeof(unsigned int),
151 		.mode = 0644,
152 		.proc_handler = proc_dointvec_jiffies,
153 	},
154 	{
155 		.procname = "checksum_enabled",
156 		.maxlen = sizeof(u8),
157 		.mode = 0644,
158 		.proc_handler = proc_dou8vec_minmax,
159 		.extra1       = SYSCTL_ZERO,
160 		.extra2       = SYSCTL_ONE
161 	},
162 	{
163 		.procname = "allow_join_initial_addr_port",
164 		.maxlen = sizeof(u8),
165 		.mode = 0644,
166 		.proc_handler = proc_dou8vec_minmax,
167 		.extra1       = SYSCTL_ZERO,
168 		.extra2       = SYSCTL_ONE
169 	},
170 	{
171 		.procname = "stale_loss_cnt",
172 		.maxlen = sizeof(unsigned int),
173 		.mode = 0644,
174 		.proc_handler = proc_douintvec_minmax,
175 	},
176 	{
177 		.procname = "pm_type",
178 		.maxlen = sizeof(u8),
179 		.mode = 0644,
180 		.proc_handler = proc_dou8vec_minmax,
181 		.extra1       = SYSCTL_ZERO,
182 		.extra2       = &mptcp_pm_type_max
183 	},
184 	{
185 		.procname = "scheduler",
186 		.maxlen	= MPTCP_SCHED_NAME_MAX,
187 		.mode = 0644,
188 		.proc_handler = proc_scheduler,
189 	},
190 	{
191 		.procname = "close_timeout",
192 		.maxlen = sizeof(unsigned int),
193 		.mode = 0644,
194 		.proc_handler = proc_dointvec_jiffies,
195 	},
196 	{}
197 };
198 
199 static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
200 {
201 	struct ctl_table_header *hdr;
202 	struct ctl_table *table;
203 
204 	table = mptcp_sysctl_table;
205 	if (!net_eq(net, &init_net)) {
206 		table = kmemdup(table, sizeof(mptcp_sysctl_table), GFP_KERNEL);
207 		if (!table)
208 			goto err_alloc;
209 	}
210 
211 	table[0].data = &pernet->mptcp_enabled;
212 	table[1].data = &pernet->add_addr_timeout;
213 	table[2].data = &pernet->checksum_enabled;
214 	table[3].data = &pernet->allow_join_initial_addr_port;
215 	table[4].data = &pernet->stale_loss_cnt;
216 	table[5].data = &pernet->pm_type;
217 	table[6].data = &pernet->scheduler;
218 	table[7].data = &pernet->close_timeout;
219 
220 	hdr = register_net_sysctl_sz(net, MPTCP_SYSCTL_PATH, table,
221 				     ARRAY_SIZE(mptcp_sysctl_table));
222 	if (!hdr)
223 		goto err_reg;
224 
225 	pernet->ctl_table_hdr = hdr;
226 
227 	return 0;
228 
229 err_reg:
230 	if (!net_eq(net, &init_net))
231 		kfree(table);
232 err_alloc:
233 	return -ENOMEM;
234 }
235 
236 static void mptcp_pernet_del_table(struct mptcp_pernet *pernet)
237 {
238 	struct ctl_table *table = pernet->ctl_table_hdr->ctl_table_arg;
239 
240 	unregister_net_sysctl_table(pernet->ctl_table_hdr);
241 
242 	kfree(table);
243 }
244 
245 #else
246 
247 static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
248 {
249 	return 0;
250 }
251 
252 static void mptcp_pernet_del_table(struct mptcp_pernet *pernet) {}
253 
254 #endif /* CONFIG_SYSCTL */
255 
256 static int __net_init mptcp_net_init(struct net *net)
257 {
258 	struct mptcp_pernet *pernet = mptcp_get_pernet(net);
259 
260 	mptcp_pernet_set_defaults(pernet);
261 
262 	return mptcp_pernet_new_table(net, pernet);
263 }
264 
265 /* Note: the callback will only be called per extra netns */
266 static void __net_exit mptcp_net_exit(struct net *net)
267 {
268 	struct mptcp_pernet *pernet = mptcp_get_pernet(net);
269 
270 	mptcp_pernet_del_table(pernet);
271 }
272 
273 static struct pernet_operations mptcp_pernet_ops = {
274 	.init = mptcp_net_init,
275 	.exit = mptcp_net_exit,
276 	.id = &mptcp_pernet_id,
277 	.size = sizeof(struct mptcp_pernet),
278 };
279 
280 void __init mptcp_init(void)
281 {
282 	mptcp_join_cookie_init();
283 	mptcp_proto_init();
284 
285 	if (register_pernet_subsys(&mptcp_pernet_ops) < 0)
286 		panic("Failed to register MPTCP pernet subsystem.\n");
287 }
288 
289 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
290 int __init mptcpv6_init(void)
291 {
292 	int err;
293 
294 	err = mptcp_proto_v6_init();
295 
296 	return err;
297 }
298 #endif
299