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