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