xref: /linux/net/mptcp/pm_userspace.c (revision ae22a94997b8a03dcb3c922857c203246711f9d4)
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 
94 	list_for_each_entry_safe(entry, tmp, &msk->pm.userspace_pm_local_addr_list, list) {
95 		if (mptcp_addresses_equal(&entry->addr, &addr->addr, false)) {
96 			/* TODO: a refcount is needed because the entry can
97 			 * be used multiple times (e.g. fullmesh mode).
98 			 */
99 			list_del_rcu(&entry->list);
100 			kfree(entry);
101 			msk->pm.local_addr_used--;
102 			return 0;
103 		}
104 	}
105 
106 	return -EINVAL;
107 }
108 
109 static struct mptcp_pm_addr_entry *
110 mptcp_userspace_pm_lookup_addr_by_id(struct mptcp_sock *msk, unsigned int id)
111 {
112 	struct mptcp_pm_addr_entry *entry;
113 
114 	list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
115 		if (entry->addr.id == id)
116 			return entry;
117 	}
118 	return NULL;
119 }
120 
121 int mptcp_userspace_pm_get_flags_and_ifindex_by_id(struct mptcp_sock *msk,
122 						   unsigned int id,
123 						   u8 *flags, int *ifindex)
124 {
125 	struct mptcp_pm_addr_entry *match;
126 
127 	spin_lock_bh(&msk->pm.lock);
128 	match = mptcp_userspace_pm_lookup_addr_by_id(msk, id);
129 	spin_unlock_bh(&msk->pm.lock);
130 	if (match) {
131 		*flags = match->flags;
132 		*ifindex = match->ifindex;
133 	}
134 
135 	return 0;
136 }
137 
138 int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk,
139 				    struct mptcp_addr_info *skc)
140 {
141 	struct mptcp_pm_addr_entry *entry = NULL, *e, new_entry;
142 	__be16 msk_sport =  ((struct inet_sock *)
143 			     inet_sk((struct sock *)msk))->inet_sport;
144 
145 	spin_lock_bh(&msk->pm.lock);
146 	list_for_each_entry(e, &msk->pm.userspace_pm_local_addr_list, list) {
147 		if (mptcp_addresses_equal(&e->addr, skc, false)) {
148 			entry = e;
149 			break;
150 		}
151 	}
152 	spin_unlock_bh(&msk->pm.lock);
153 	if (entry)
154 		return entry->addr.id;
155 
156 	memset(&new_entry, 0, sizeof(struct mptcp_pm_addr_entry));
157 	new_entry.addr = *skc;
158 	new_entry.addr.id = 0;
159 	new_entry.flags = MPTCP_PM_ADDR_FLAG_IMPLICIT;
160 
161 	if (new_entry.addr.port == msk_sport)
162 		new_entry.addr.port = 0;
163 
164 	return mptcp_userspace_pm_append_new_local_addr(msk, &new_entry, true);
165 }
166 
167 int mptcp_pm_nl_announce_doit(struct sk_buff *skb, struct genl_info *info)
168 {
169 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
170 	struct nlattr *addr = info->attrs[MPTCP_PM_ATTR_ADDR];
171 	struct mptcp_pm_addr_entry addr_val;
172 	struct mptcp_sock *msk;
173 	int err = -EINVAL;
174 	struct sock *sk;
175 	u32 token_val;
176 
177 	if (!addr || !token) {
178 		GENL_SET_ERR_MSG(info, "missing required inputs");
179 		return err;
180 	}
181 
182 	token_val = nla_get_u32(token);
183 
184 	msk = mptcp_token_get_sock(sock_net(skb->sk), token_val);
185 	if (!msk) {
186 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
187 		return err;
188 	}
189 
190 	sk = (struct sock *)msk;
191 
192 	if (!mptcp_pm_is_userspace(msk)) {
193 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
194 		goto announce_err;
195 	}
196 
197 	err = mptcp_pm_parse_entry(addr, info, true, &addr_val);
198 	if (err < 0) {
199 		GENL_SET_ERR_MSG(info, "error parsing local address");
200 		goto announce_err;
201 	}
202 
203 	if (addr_val.addr.id == 0 || !(addr_val.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
204 		GENL_SET_ERR_MSG(info, "invalid addr id or flags");
205 		err = -EINVAL;
206 		goto announce_err;
207 	}
208 
209 	err = mptcp_userspace_pm_append_new_local_addr(msk, &addr_val, false);
210 	if (err < 0) {
211 		GENL_SET_ERR_MSG(info, "did not match address and id");
212 		goto announce_err;
213 	}
214 
215 	lock_sock(sk);
216 	spin_lock_bh(&msk->pm.lock);
217 
218 	if (mptcp_pm_alloc_anno_list(msk, &addr_val.addr)) {
219 		msk->pm.add_addr_signaled++;
220 		mptcp_pm_announce_addr(msk, &addr_val.addr, false);
221 		mptcp_pm_nl_addr_send_ack(msk);
222 	}
223 
224 	spin_unlock_bh(&msk->pm.lock);
225 	release_sock(sk);
226 
227 	err = 0;
228  announce_err:
229 	sock_put(sk);
230 	return err;
231 }
232 
233 static int mptcp_userspace_pm_remove_id_zero_address(struct mptcp_sock *msk,
234 						     struct genl_info *info)
235 {
236 	struct mptcp_rm_list list = { .nr = 0 };
237 	struct mptcp_subflow_context *subflow;
238 	struct sock *sk = (struct sock *)msk;
239 	bool has_id_0 = false;
240 	int err = -EINVAL;
241 
242 	lock_sock(sk);
243 	mptcp_for_each_subflow(msk, subflow) {
244 		if (READ_ONCE(subflow->local_id) == 0) {
245 			has_id_0 = true;
246 			break;
247 		}
248 	}
249 	if (!has_id_0) {
250 		GENL_SET_ERR_MSG(info, "address with id 0 not found");
251 		goto remove_err;
252 	}
253 
254 	list.ids[list.nr++] = 0;
255 
256 	spin_lock_bh(&msk->pm.lock);
257 	mptcp_pm_remove_addr(msk, &list);
258 	spin_unlock_bh(&msk->pm.lock);
259 
260 	err = 0;
261 
262 remove_err:
263 	release_sock(sk);
264 	return err;
265 }
266 
267 int mptcp_pm_nl_remove_doit(struct sk_buff *skb, struct genl_info *info)
268 {
269 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
270 	struct nlattr *id = info->attrs[MPTCP_PM_ATTR_LOC_ID];
271 	struct mptcp_pm_addr_entry *match;
272 	struct mptcp_pm_addr_entry *entry;
273 	struct mptcp_sock *msk;
274 	LIST_HEAD(free_list);
275 	int err = -EINVAL;
276 	struct sock *sk;
277 	u32 token_val;
278 	u8 id_val;
279 
280 	if (!id || !token) {
281 		GENL_SET_ERR_MSG(info, "missing required inputs");
282 		return err;
283 	}
284 
285 	id_val = nla_get_u8(id);
286 	token_val = nla_get_u32(token);
287 
288 	msk = mptcp_token_get_sock(sock_net(skb->sk), token_val);
289 	if (!msk) {
290 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
291 		return err;
292 	}
293 
294 	sk = (struct sock *)msk;
295 
296 	if (!mptcp_pm_is_userspace(msk)) {
297 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
298 		goto out;
299 	}
300 
301 	if (id_val == 0) {
302 		err = mptcp_userspace_pm_remove_id_zero_address(msk, info);
303 		goto out;
304 	}
305 
306 	lock_sock(sk);
307 
308 	match = mptcp_userspace_pm_lookup_addr_by_id(msk, id_val);
309 	if (!match) {
310 		GENL_SET_ERR_MSG(info, "address with specified id not found");
311 		release_sock(sk);
312 		goto out;
313 	}
314 
315 	list_move(&match->list, &free_list);
316 
317 	mptcp_pm_remove_addrs(msk, &free_list);
318 
319 	release_sock(sk);
320 
321 	list_for_each_entry_safe(match, entry, &free_list, list) {
322 		sock_kfree_s(sk, match, sizeof(*match));
323 	}
324 
325 	err = 0;
326 out:
327 	sock_put(sk);
328 	return err;
329 }
330 
331 int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info)
332 {
333 	struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
334 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
335 	struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
336 	struct mptcp_pm_addr_entry local = { 0 };
337 	struct mptcp_addr_info addr_r;
338 	struct mptcp_sock *msk;
339 	int err = -EINVAL;
340 	struct sock *sk;
341 	u32 token_val;
342 
343 	if (!laddr || !raddr || !token) {
344 		GENL_SET_ERR_MSG(info, "missing required inputs");
345 		return err;
346 	}
347 
348 	token_val = nla_get_u32(token);
349 
350 	msk = mptcp_token_get_sock(genl_info_net(info), token_val);
351 	if (!msk) {
352 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
353 		return err;
354 	}
355 
356 	sk = (struct sock *)msk;
357 
358 	if (!mptcp_pm_is_userspace(msk)) {
359 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
360 		goto create_err;
361 	}
362 
363 	err = mptcp_pm_parse_entry(laddr, info, true, &local);
364 	if (err < 0) {
365 		NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
366 		goto create_err;
367 	}
368 
369 	if (local.flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
370 		GENL_SET_ERR_MSG(info, "invalid addr flags");
371 		err = -EINVAL;
372 		goto create_err;
373 	}
374 	local.flags |= MPTCP_PM_ADDR_FLAG_SUBFLOW;
375 
376 	err = mptcp_pm_parse_addr(raddr, info, &addr_r);
377 	if (err < 0) {
378 		NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
379 		goto create_err;
380 	}
381 
382 	if (!mptcp_pm_addr_families_match(sk, &local.addr, &addr_r)) {
383 		GENL_SET_ERR_MSG(info, "families mismatch");
384 		err = -EINVAL;
385 		goto create_err;
386 	}
387 
388 	err = mptcp_userspace_pm_append_new_local_addr(msk, &local, false);
389 	if (err < 0) {
390 		GENL_SET_ERR_MSG(info, "did not match address and id");
391 		goto create_err;
392 	}
393 
394 	lock_sock(sk);
395 
396 	err = __mptcp_subflow_connect(sk, &local.addr, &addr_r);
397 
398 	release_sock(sk);
399 
400 	spin_lock_bh(&msk->pm.lock);
401 	if (err)
402 		mptcp_userspace_pm_delete_local_addr(msk, &local);
403 	else
404 		msk->pm.subflows++;
405 	spin_unlock_bh(&msk->pm.lock);
406 
407  create_err:
408 	sock_put(sk);
409 	return err;
410 }
411 
412 static struct sock *mptcp_nl_find_ssk(struct mptcp_sock *msk,
413 				      const struct mptcp_addr_info *local,
414 				      const struct mptcp_addr_info *remote)
415 {
416 	struct mptcp_subflow_context *subflow;
417 
418 	if (local->family != remote->family)
419 		return NULL;
420 
421 	mptcp_for_each_subflow(msk, subflow) {
422 		const struct inet_sock *issk;
423 		struct sock *ssk;
424 
425 		ssk = mptcp_subflow_tcp_sock(subflow);
426 
427 		if (local->family != ssk->sk_family)
428 			continue;
429 
430 		issk = inet_sk(ssk);
431 
432 		switch (ssk->sk_family) {
433 		case AF_INET:
434 			if (issk->inet_saddr != local->addr.s_addr ||
435 			    issk->inet_daddr != remote->addr.s_addr)
436 				continue;
437 			break;
438 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
439 		case AF_INET6: {
440 			const struct ipv6_pinfo *pinfo = inet6_sk(ssk);
441 
442 			if (!ipv6_addr_equal(&local->addr6, &pinfo->saddr) ||
443 			    !ipv6_addr_equal(&remote->addr6, &ssk->sk_v6_daddr))
444 				continue;
445 			break;
446 		}
447 #endif
448 		default:
449 			continue;
450 		}
451 
452 		if (issk->inet_sport == local->port &&
453 		    issk->inet_dport == remote->port)
454 			return ssk;
455 	}
456 
457 	return NULL;
458 }
459 
460 int mptcp_pm_nl_subflow_destroy_doit(struct sk_buff *skb, struct genl_info *info)
461 {
462 	struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
463 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
464 	struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
465 	struct mptcp_addr_info addr_l;
466 	struct mptcp_addr_info addr_r;
467 	struct mptcp_sock *msk;
468 	struct sock *sk, *ssk;
469 	int err = -EINVAL;
470 	u32 token_val;
471 
472 	if (!laddr || !raddr || !token) {
473 		GENL_SET_ERR_MSG(info, "missing required inputs");
474 		return err;
475 	}
476 
477 	token_val = nla_get_u32(token);
478 
479 	msk = mptcp_token_get_sock(genl_info_net(info), token_val);
480 	if (!msk) {
481 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
482 		return err;
483 	}
484 
485 	sk = (struct sock *)msk;
486 
487 	if (!mptcp_pm_is_userspace(msk)) {
488 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
489 		goto destroy_err;
490 	}
491 
492 	err = mptcp_pm_parse_addr(laddr, info, &addr_l);
493 	if (err < 0) {
494 		NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
495 		goto destroy_err;
496 	}
497 
498 	err = mptcp_pm_parse_addr(raddr, info, &addr_r);
499 	if (err < 0) {
500 		NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
501 		goto destroy_err;
502 	}
503 
504 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
505 	if (addr_l.family == AF_INET && ipv6_addr_v4mapped(&addr_r.addr6)) {
506 		ipv6_addr_set_v4mapped(addr_l.addr.s_addr, &addr_l.addr6);
507 		addr_l.family = AF_INET6;
508 	}
509 	if (addr_r.family == AF_INET && ipv6_addr_v4mapped(&addr_l.addr6)) {
510 		ipv6_addr_set_v4mapped(addr_r.addr.s_addr, &addr_r.addr6);
511 		addr_r.family = AF_INET6;
512 	}
513 #endif
514 	if (addr_l.family != addr_r.family) {
515 		GENL_SET_ERR_MSG(info, "address families do not match");
516 		err = -EINVAL;
517 		goto destroy_err;
518 	}
519 
520 	if (!addr_l.port || !addr_r.port) {
521 		GENL_SET_ERR_MSG(info, "missing local or remote port");
522 		err = -EINVAL;
523 		goto destroy_err;
524 	}
525 
526 	lock_sock(sk);
527 	ssk = mptcp_nl_find_ssk(msk, &addr_l, &addr_r);
528 	if (ssk) {
529 		struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
530 		struct mptcp_pm_addr_entry entry = { .addr = addr_l };
531 
532 		spin_lock_bh(&msk->pm.lock);
533 		mptcp_userspace_pm_delete_local_addr(msk, &entry);
534 		spin_unlock_bh(&msk->pm.lock);
535 		mptcp_subflow_shutdown(sk, ssk, RCV_SHUTDOWN | SEND_SHUTDOWN);
536 		mptcp_close_ssk(sk, ssk, subflow);
537 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
538 		err = 0;
539 	} else {
540 		err = -ESRCH;
541 	}
542 	release_sock(sk);
543 
544 destroy_err:
545 	sock_put(sk);
546 	return err;
547 }
548 
549 int mptcp_userspace_pm_set_flags(struct sk_buff *skb, struct genl_info *info)
550 {
551 	struct mptcp_pm_addr_entry loc = { .addr = { .family = AF_UNSPEC }, };
552 	struct mptcp_pm_addr_entry rem = { .addr = { .family = AF_UNSPEC }, };
553 	struct nlattr *attr_rem = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
554 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
555 	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
556 	struct net *net = sock_net(skb->sk);
557 	struct mptcp_sock *msk;
558 	int ret = -EINVAL;
559 	struct sock *sk;
560 	u32 token_val;
561 	u8 bkup = 0;
562 
563 	token_val = nla_get_u32(token);
564 
565 	msk = mptcp_token_get_sock(net, token_val);
566 	if (!msk) {
567 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
568 		return ret;
569 	}
570 
571 	sk = (struct sock *)msk;
572 
573 	if (!mptcp_pm_is_userspace(msk)) {
574 		GENL_SET_ERR_MSG(info, "userspace PM not selected");
575 		goto set_flags_err;
576 	}
577 
578 	ret = mptcp_pm_parse_entry(attr, info, false, &loc);
579 	if (ret < 0)
580 		goto set_flags_err;
581 
582 	if (attr_rem) {
583 		ret = mptcp_pm_parse_entry(attr_rem, info, false, &rem);
584 		if (ret < 0)
585 			goto set_flags_err;
586 	}
587 
588 	if (loc.addr.family == AF_UNSPEC ||
589 	    rem.addr.family == AF_UNSPEC) {
590 		GENL_SET_ERR_MSG(info, "invalid address families");
591 		ret = -EINVAL;
592 		goto set_flags_err;
593 	}
594 
595 	if (loc.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
596 		bkup = 1;
597 
598 	lock_sock(sk);
599 	ret = mptcp_pm_nl_mp_prio_send_ack(msk, &loc.addr, &rem.addr, bkup);
600 	release_sock(sk);
601 
602 set_flags_err:
603 	sock_put(sk);
604 	return ret;
605 }
606 
607 int mptcp_userspace_pm_dump_addr(struct sk_buff *msg,
608 				 struct netlink_callback *cb)
609 {
610 	struct id_bitmap {
611 		DECLARE_BITMAP(map, MPTCP_PM_MAX_ADDR_ID + 1);
612 	} *bitmap;
613 	const struct genl_info *info = genl_info_dump(cb);
614 	struct net *net = sock_net(msg->sk);
615 	struct mptcp_pm_addr_entry *entry;
616 	struct mptcp_sock *msk;
617 	struct nlattr *token;
618 	int ret = -EINVAL;
619 	struct sock *sk;
620 	void *hdr;
621 
622 	bitmap = (struct id_bitmap *)cb->ctx;
623 	token = info->attrs[MPTCP_PM_ATTR_TOKEN];
624 
625 	msk = mptcp_token_get_sock(net, nla_get_u32(token));
626 	if (!msk) {
627 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
628 		return ret;
629 	}
630 
631 	sk = (struct sock *)msk;
632 
633 	if (!mptcp_pm_is_userspace(msk)) {
634 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
635 		goto out;
636 	}
637 
638 	lock_sock(sk);
639 	spin_lock_bh(&msk->pm.lock);
640 	list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
641 		if (test_bit(entry->addr.id, bitmap->map))
642 			continue;
643 
644 		hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
645 				  cb->nlh->nlmsg_seq, &mptcp_genl_family,
646 				  NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
647 		if (!hdr)
648 			break;
649 
650 		if (mptcp_nl_fill_addr(msg, entry) < 0) {
651 			genlmsg_cancel(msg, hdr);
652 			break;
653 		}
654 
655 		__set_bit(entry->addr.id, bitmap->map);
656 		genlmsg_end(msg, hdr);
657 	}
658 	spin_unlock_bh(&msk->pm.lock);
659 	release_sock(sk);
660 	ret = msg->len;
661 
662 out:
663 	sock_put(sk);
664 	return ret;
665 }
666 
667 int mptcp_userspace_pm_get_addr(struct sk_buff *skb,
668 				struct genl_info *info)
669 {
670 	struct nlattr *attr = info->attrs[MPTCP_PM_ENDPOINT_ADDR];
671 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
672 	struct mptcp_pm_addr_entry addr, *entry;
673 	struct net *net = sock_net(skb->sk);
674 	struct mptcp_sock *msk;
675 	struct sk_buff *msg;
676 	int ret = -EINVAL;
677 	struct sock *sk;
678 	void *reply;
679 
680 	msk = mptcp_token_get_sock(net, nla_get_u32(token));
681 	if (!msk) {
682 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
683 		return ret;
684 	}
685 
686 	sk = (struct sock *)msk;
687 
688 	if (!mptcp_pm_is_userspace(msk)) {
689 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
690 		goto out;
691 	}
692 
693 	ret = mptcp_pm_parse_entry(attr, info, false, &addr);
694 	if (ret < 0)
695 		goto out;
696 
697 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
698 	if (!msg) {
699 		ret = -ENOMEM;
700 		goto out;
701 	}
702 
703 	reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
704 				  info->genlhdr->cmd);
705 	if (!reply) {
706 		GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
707 		ret = -EMSGSIZE;
708 		goto fail;
709 	}
710 
711 	lock_sock(sk);
712 	spin_lock_bh(&msk->pm.lock);
713 	entry = mptcp_userspace_pm_lookup_addr_by_id(msk, addr.addr.id);
714 	if (!entry) {
715 		GENL_SET_ERR_MSG(info, "address not found");
716 		ret = -EINVAL;
717 		goto unlock_fail;
718 	}
719 
720 	ret = mptcp_nl_fill_addr(msg, entry);
721 	if (ret)
722 		goto unlock_fail;
723 
724 	genlmsg_end(msg, reply);
725 	ret = genlmsg_reply(msg, info);
726 	spin_unlock_bh(&msk->pm.lock);
727 	release_sock(sk);
728 	sock_put(sk);
729 	return ret;
730 
731 unlock_fail:
732 	spin_unlock_bh(&msk->pm.lock);
733 	release_sock(sk);
734 fail:
735 	nlmsg_free(msg);
736 out:
737 	sock_put(sk);
738 	return ret;
739 }
740