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