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