xref: /openbmc/linux/net/mptcp/pm_userspace.c (revision 657c45b3)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Multipath TCP
3  *
4  * Copyright (c) 2022, Intel Corporation.
5  */
6 
7 #include "protocol.h"
8 #include "mib.h"
9 
10 void mptcp_free_local_addr_list(struct mptcp_sock *msk)
11 {
12 	struct mptcp_pm_addr_entry *entry, *tmp;
13 	struct sock *sk = (struct sock *)msk;
14 	LIST_HEAD(free_list);
15 
16 	if (!mptcp_pm_is_userspace(msk))
17 		return;
18 
19 	spin_lock_bh(&msk->pm.lock);
20 	list_splice_init(&msk->pm.userspace_pm_local_addr_list, &free_list);
21 	spin_unlock_bh(&msk->pm.lock);
22 
23 	list_for_each_entry_safe(entry, tmp, &free_list, list) {
24 		sock_kfree_s(sk, entry, sizeof(*entry));
25 	}
26 }
27 
28 static int mptcp_userspace_pm_append_new_local_addr(struct mptcp_sock *msk,
29 						    struct mptcp_pm_addr_entry *entry)
30 {
31 	DECLARE_BITMAP(id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
32 	struct mptcp_pm_addr_entry *match = NULL;
33 	struct sock *sk = (struct sock *)msk;
34 	struct mptcp_pm_addr_entry *e;
35 	bool addr_match = false;
36 	bool id_match = false;
37 	int ret = -EINVAL;
38 
39 	bitmap_zero(id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
40 
41 	spin_lock_bh(&msk->pm.lock);
42 	list_for_each_entry(e, &msk->pm.userspace_pm_local_addr_list, list) {
43 		addr_match = mptcp_addresses_equal(&e->addr, &entry->addr, true);
44 		if (addr_match && entry->addr.id == 0)
45 			entry->addr.id = e->addr.id;
46 		id_match = (e->addr.id == entry->addr.id);
47 		if (addr_match && id_match) {
48 			match = e;
49 			break;
50 		} else if (addr_match || id_match) {
51 			break;
52 		}
53 		__set_bit(e->addr.id, id_bitmap);
54 	}
55 
56 	if (!match && !addr_match && !id_match) {
57 		/* Memory for the entry is allocated from the
58 		 * sock option buffer.
59 		 */
60 		e = sock_kmalloc(sk, sizeof(*e), GFP_ATOMIC);
61 		if (!e) {
62 			ret = -ENOMEM;
63 			goto append_err;
64 		}
65 
66 		*e = *entry;
67 		if (!e->addr.id)
68 			e->addr.id = find_next_zero_bit(id_bitmap,
69 							MPTCP_PM_MAX_ADDR_ID + 1,
70 							1);
71 		list_add_tail_rcu(&e->list, &msk->pm.userspace_pm_local_addr_list);
72 		msk->pm.local_addr_used++;
73 		ret = e->addr.id;
74 	} else if (match) {
75 		ret = entry->addr.id;
76 	}
77 
78 append_err:
79 	spin_unlock_bh(&msk->pm.lock);
80 	return ret;
81 }
82 
83 /* If the subflow is closed from the other peer (not via a
84  * subflow destroy command then), we want to keep the entry
85  * not to assign the same ID to another address and to be
86  * able to send RM_ADDR after the removal of the subflow.
87  */
88 static int mptcp_userspace_pm_delete_local_addr(struct mptcp_sock *msk,
89 						struct mptcp_pm_addr_entry *addr)
90 {
91 	struct mptcp_pm_addr_entry *entry, *tmp;
92 
93 	list_for_each_entry_safe(entry, tmp, &msk->pm.userspace_pm_local_addr_list, list) {
94 		if (mptcp_addresses_equal(&entry->addr, &addr->addr, false)) {
95 			/* TODO: a refcount is needed because the entry can
96 			 * be used multiple times (e.g. fullmesh mode).
97 			 */
98 			list_del_rcu(&entry->list);
99 			kfree(entry);
100 			msk->pm.local_addr_used--;
101 			return 0;
102 		}
103 	}
104 
105 	return -EINVAL;
106 }
107 
108 int mptcp_userspace_pm_get_flags_and_ifindex_by_id(struct mptcp_sock *msk,
109 						   unsigned int id,
110 						   u8 *flags, int *ifindex)
111 {
112 	struct mptcp_pm_addr_entry *entry, *match = NULL;
113 
114 	spin_lock_bh(&msk->pm.lock);
115 	list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
116 		if (id == entry->addr.id) {
117 			match = entry;
118 			break;
119 		}
120 	}
121 	spin_unlock_bh(&msk->pm.lock);
122 	if (match) {
123 		*flags = match->flags;
124 		*ifindex = match->ifindex;
125 	}
126 
127 	return 0;
128 }
129 
130 int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk,
131 				    struct mptcp_addr_info *skc)
132 {
133 	struct mptcp_pm_addr_entry new_entry;
134 	__be16 msk_sport =  ((struct inet_sock *)
135 			     inet_sk((struct sock *)msk))->inet_sport;
136 
137 	memset(&new_entry, 0, sizeof(struct mptcp_pm_addr_entry));
138 	new_entry.addr = *skc;
139 	new_entry.addr.id = 0;
140 	new_entry.flags = MPTCP_PM_ADDR_FLAG_IMPLICIT;
141 
142 	if (new_entry.addr.port == msk_sport)
143 		new_entry.addr.port = 0;
144 
145 	return mptcp_userspace_pm_append_new_local_addr(msk, &new_entry);
146 }
147 
148 int mptcp_nl_cmd_announce(struct sk_buff *skb, struct genl_info *info)
149 {
150 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
151 	struct nlattr *addr = info->attrs[MPTCP_PM_ATTR_ADDR];
152 	struct mptcp_pm_addr_entry addr_val;
153 	struct mptcp_sock *msk;
154 	int err = -EINVAL;
155 	u32 token_val;
156 
157 	if (!addr || !token) {
158 		GENL_SET_ERR_MSG(info, "missing required inputs");
159 		return err;
160 	}
161 
162 	token_val = nla_get_u32(token);
163 
164 	msk = mptcp_token_get_sock(sock_net(skb->sk), token_val);
165 	if (!msk) {
166 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
167 		return err;
168 	}
169 
170 	if (!mptcp_pm_is_userspace(msk)) {
171 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
172 		goto announce_err;
173 	}
174 
175 	err = mptcp_pm_parse_entry(addr, info, true, &addr_val);
176 	if (err < 0) {
177 		GENL_SET_ERR_MSG(info, "error parsing local address");
178 		goto announce_err;
179 	}
180 
181 	if (addr_val.addr.id == 0 || !(addr_val.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
182 		GENL_SET_ERR_MSG(info, "invalid addr id or flags");
183 		err = -EINVAL;
184 		goto announce_err;
185 	}
186 
187 	err = mptcp_userspace_pm_append_new_local_addr(msk, &addr_val);
188 	if (err < 0) {
189 		GENL_SET_ERR_MSG(info, "did not match address and id");
190 		goto announce_err;
191 	}
192 
193 	lock_sock((struct sock *)msk);
194 	spin_lock_bh(&msk->pm.lock);
195 
196 	if (mptcp_pm_alloc_anno_list(msk, &addr_val.addr)) {
197 		msk->pm.add_addr_signaled++;
198 		mptcp_pm_announce_addr(msk, &addr_val.addr, false);
199 		mptcp_pm_nl_addr_send_ack(msk);
200 	}
201 
202 	spin_unlock_bh(&msk->pm.lock);
203 	release_sock((struct sock *)msk);
204 
205 	err = 0;
206  announce_err:
207 	sock_put((struct sock *)msk);
208 	return err;
209 }
210 
211 int mptcp_nl_cmd_remove(struct sk_buff *skb, struct genl_info *info)
212 {
213 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
214 	struct nlattr *id = info->attrs[MPTCP_PM_ATTR_LOC_ID];
215 	struct mptcp_pm_addr_entry *match = NULL;
216 	struct mptcp_pm_addr_entry *entry;
217 	struct mptcp_sock *msk;
218 	LIST_HEAD(free_list);
219 	int err = -EINVAL;
220 	u32 token_val;
221 	u8 id_val;
222 
223 	if (!id || !token) {
224 		GENL_SET_ERR_MSG(info, "missing required inputs");
225 		return err;
226 	}
227 
228 	id_val = nla_get_u8(id);
229 	token_val = nla_get_u32(token);
230 
231 	msk = mptcp_token_get_sock(sock_net(skb->sk), token_val);
232 	if (!msk) {
233 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
234 		return err;
235 	}
236 
237 	if (!mptcp_pm_is_userspace(msk)) {
238 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
239 		goto remove_err;
240 	}
241 
242 	lock_sock((struct sock *)msk);
243 
244 	list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
245 		if (entry->addr.id == id_val) {
246 			match = entry;
247 			break;
248 		}
249 	}
250 
251 	if (!match) {
252 		GENL_SET_ERR_MSG(info, "address with specified id not found");
253 		release_sock((struct sock *)msk);
254 		goto remove_err;
255 	}
256 
257 	list_move(&match->list, &free_list);
258 
259 	mptcp_pm_remove_addrs(msk, &free_list);
260 
261 	release_sock((struct sock *)msk);
262 
263 	list_for_each_entry_safe(match, entry, &free_list, list) {
264 		sock_kfree_s((struct sock *)msk, match, sizeof(*match));
265 	}
266 
267 	err = 0;
268  remove_err:
269 	sock_put((struct sock *)msk);
270 	return err;
271 }
272 
273 int mptcp_nl_cmd_sf_create(struct sk_buff *skb, struct genl_info *info)
274 {
275 	struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
276 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
277 	struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
278 	struct mptcp_pm_addr_entry local = { 0 };
279 	struct mptcp_addr_info addr_r;
280 	struct mptcp_addr_info addr_l;
281 	struct mptcp_sock *msk;
282 	int err = -EINVAL;
283 	struct sock *sk;
284 	u32 token_val;
285 
286 	if (!laddr || !raddr || !token) {
287 		GENL_SET_ERR_MSG(info, "missing required inputs");
288 		return err;
289 	}
290 
291 	token_val = nla_get_u32(token);
292 
293 	msk = mptcp_token_get_sock(genl_info_net(info), token_val);
294 	if (!msk) {
295 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
296 		return err;
297 	}
298 
299 	if (!mptcp_pm_is_userspace(msk)) {
300 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
301 		goto create_err;
302 	}
303 
304 	err = mptcp_pm_parse_addr(laddr, info, &addr_l);
305 	if (err < 0) {
306 		NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
307 		goto create_err;
308 	}
309 
310 	if (addr_l.id == 0) {
311 		NL_SET_ERR_MSG_ATTR(info->extack, laddr, "missing local addr id");
312 		err = -EINVAL;
313 		goto create_err;
314 	}
315 
316 	err = mptcp_pm_parse_addr(raddr, info, &addr_r);
317 	if (err < 0) {
318 		NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
319 		goto create_err;
320 	}
321 
322 	sk = (struct sock *)msk;
323 
324 	if (!mptcp_pm_addr_families_match(sk, &addr_l, &addr_r)) {
325 		GENL_SET_ERR_MSG(info, "families mismatch");
326 		err = -EINVAL;
327 		goto create_err;
328 	}
329 
330 	local.addr = addr_l;
331 	err = mptcp_userspace_pm_append_new_local_addr(msk, &local);
332 	if (err < 0) {
333 		GENL_SET_ERR_MSG(info, "did not match address and id");
334 		goto create_err;
335 	}
336 
337 	lock_sock(sk);
338 
339 	err = __mptcp_subflow_connect(sk, &addr_l, &addr_r);
340 
341 	release_sock(sk);
342 
343 	spin_lock_bh(&msk->pm.lock);
344 	if (err)
345 		mptcp_userspace_pm_delete_local_addr(msk, &local);
346 	else
347 		msk->pm.subflows++;
348 	spin_unlock_bh(&msk->pm.lock);
349 
350  create_err:
351 	sock_put((struct sock *)msk);
352 	return err;
353 }
354 
355 static struct sock *mptcp_nl_find_ssk(struct mptcp_sock *msk,
356 				      const struct mptcp_addr_info *local,
357 				      const struct mptcp_addr_info *remote)
358 {
359 	struct mptcp_subflow_context *subflow;
360 
361 	if (local->family != remote->family)
362 		return NULL;
363 
364 	mptcp_for_each_subflow(msk, subflow) {
365 		const struct inet_sock *issk;
366 		struct sock *ssk;
367 
368 		ssk = mptcp_subflow_tcp_sock(subflow);
369 
370 		if (local->family != ssk->sk_family)
371 			continue;
372 
373 		issk = inet_sk(ssk);
374 
375 		switch (ssk->sk_family) {
376 		case AF_INET:
377 			if (issk->inet_saddr != local->addr.s_addr ||
378 			    issk->inet_daddr != remote->addr.s_addr)
379 				continue;
380 			break;
381 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
382 		case AF_INET6: {
383 			const struct ipv6_pinfo *pinfo = inet6_sk(ssk);
384 
385 			if (!ipv6_addr_equal(&local->addr6, &pinfo->saddr) ||
386 			    !ipv6_addr_equal(&remote->addr6, &ssk->sk_v6_daddr))
387 				continue;
388 			break;
389 		}
390 #endif
391 		default:
392 			continue;
393 		}
394 
395 		if (issk->inet_sport == local->port &&
396 		    issk->inet_dport == remote->port)
397 			return ssk;
398 	}
399 
400 	return NULL;
401 }
402 
403 int mptcp_nl_cmd_sf_destroy(struct sk_buff *skb, struct genl_info *info)
404 {
405 	struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
406 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
407 	struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
408 	struct mptcp_addr_info addr_l;
409 	struct mptcp_addr_info addr_r;
410 	struct mptcp_sock *msk;
411 	struct sock *sk, *ssk;
412 	int err = -EINVAL;
413 	u32 token_val;
414 
415 	if (!laddr || !raddr || !token) {
416 		GENL_SET_ERR_MSG(info, "missing required inputs");
417 		return err;
418 	}
419 
420 	token_val = nla_get_u32(token);
421 
422 	msk = mptcp_token_get_sock(genl_info_net(info), token_val);
423 	if (!msk) {
424 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
425 		return err;
426 	}
427 
428 	if (!mptcp_pm_is_userspace(msk)) {
429 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
430 		goto destroy_err;
431 	}
432 
433 	err = mptcp_pm_parse_addr(laddr, info, &addr_l);
434 	if (err < 0) {
435 		NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
436 		goto destroy_err;
437 	}
438 
439 	err = mptcp_pm_parse_addr(raddr, info, &addr_r);
440 	if (err < 0) {
441 		NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
442 		goto destroy_err;
443 	}
444 
445 	if (addr_l.family != addr_r.family) {
446 		GENL_SET_ERR_MSG(info, "address families do not match");
447 		err = -EINVAL;
448 		goto destroy_err;
449 	}
450 
451 	if (!addr_l.port || !addr_r.port) {
452 		GENL_SET_ERR_MSG(info, "missing local or remote port");
453 		err = -EINVAL;
454 		goto destroy_err;
455 	}
456 
457 	sk = (struct sock *)msk;
458 	lock_sock(sk);
459 	ssk = mptcp_nl_find_ssk(msk, &addr_l, &addr_r);
460 	if (ssk) {
461 		struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
462 		struct mptcp_pm_addr_entry entry = { .addr = addr_l };
463 
464 		spin_lock_bh(&msk->pm.lock);
465 		mptcp_userspace_pm_delete_local_addr(msk, &entry);
466 		spin_unlock_bh(&msk->pm.lock);
467 		mptcp_subflow_shutdown(sk, ssk, RCV_SHUTDOWN | SEND_SHUTDOWN);
468 		mptcp_close_ssk(sk, ssk, subflow);
469 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
470 		err = 0;
471 	} else {
472 		err = -ESRCH;
473 	}
474 	release_sock(sk);
475 
476 destroy_err:
477 	sock_put((struct sock *)msk);
478 	return err;
479 }
480 
481 int mptcp_userspace_pm_set_flags(struct net *net, struct nlattr *token,
482 				 struct mptcp_pm_addr_entry *loc,
483 				 struct mptcp_pm_addr_entry *rem, u8 bkup)
484 {
485 	struct mptcp_sock *msk;
486 	int ret = -EINVAL;
487 	u32 token_val;
488 
489 	token_val = nla_get_u32(token);
490 
491 	msk = mptcp_token_get_sock(net, token_val);
492 	if (!msk)
493 		return ret;
494 
495 	if (!mptcp_pm_is_userspace(msk))
496 		goto set_flags_err;
497 
498 	if (loc->addr.family == AF_UNSPEC ||
499 	    rem->addr.family == AF_UNSPEC)
500 		goto set_flags_err;
501 
502 	lock_sock((struct sock *)msk);
503 	ret = mptcp_pm_nl_mp_prio_send_ack(msk, &loc->addr, &rem->addr, bkup);
504 	release_sock((struct sock *)msk);
505 
506 set_flags_err:
507 	sock_put((struct sock *)msk);
508 	return ret;
509 }
510