xref: /linux/net/mptcp/pm_userspace.c (revision cfaaa7d010d1fc58f9717fcc8591201e741d2d49)
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 	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 *
mptcp_userspace_pm_lookup_addr_by_id(struct mptcp_sock * msk,unsigned int id)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 
mptcp_userspace_pm_get_local_id(struct mptcp_sock * msk,struct mptcp_addr_info * skc)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 
mptcp_userspace_pm_is_backup(struct mptcp_sock * msk,struct mptcp_addr_info * skc)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 
mptcp_pm_nl_announce_doit(struct sk_buff * skb,struct genl_info * info)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 
mptcp_userspace_pm_remove_id_zero_address(struct mptcp_sock * msk,struct genl_info * info)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 
mptcp_pm_nl_remove_doit(struct sk_buff * skb,struct genl_info * info)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 	spin_lock_bh(&msk->pm.lock);
312 	match = mptcp_userspace_pm_lookup_addr_by_id(msk, id_val);
313 	if (!match) {
314 		GENL_SET_ERR_MSG(info, "address with specified id not found");
315 		spin_unlock_bh(&msk->pm.lock);
316 		release_sock(sk);
317 		goto out;
318 	}
319 
320 	list_move(&match->list, &free_list);
321 	spin_unlock_bh(&msk->pm.lock);
322 
323 	mptcp_pm_remove_addrs(msk, &free_list);
324 
325 	release_sock(sk);
326 
327 	list_for_each_entry_safe(match, entry, &free_list, list) {
328 		sock_kfree_s(sk, match, sizeof(*match));
329 	}
330 
331 	err = 0;
332 out:
333 	sock_put(sk);
334 	return err;
335 }
336 
mptcp_pm_nl_subflow_create_doit(struct sk_buff * skb,struct genl_info * info)337 int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info)
338 {
339 	struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
340 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
341 	struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
342 	struct mptcp_pm_addr_entry entry = { 0 };
343 	struct mptcp_addr_info addr_r;
344 	struct mptcp_pm_local local;
345 	struct mptcp_sock *msk;
346 	int err = -EINVAL;
347 	struct sock *sk;
348 	u32 token_val;
349 
350 	if (!laddr || !raddr || !token) {
351 		GENL_SET_ERR_MSG(info, "missing required inputs");
352 		return err;
353 	}
354 
355 	token_val = nla_get_u32(token);
356 
357 	msk = mptcp_token_get_sock(genl_info_net(info), token_val);
358 	if (!msk) {
359 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
360 		return err;
361 	}
362 
363 	sk = (struct sock *)msk;
364 
365 	if (!mptcp_pm_is_userspace(msk)) {
366 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
367 		goto create_err;
368 	}
369 
370 	err = mptcp_pm_parse_entry(laddr, info, true, &entry);
371 	if (err < 0) {
372 		NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
373 		goto create_err;
374 	}
375 
376 	if (entry.flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
377 		GENL_SET_ERR_MSG(info, "invalid addr flags");
378 		err = -EINVAL;
379 		goto create_err;
380 	}
381 	entry.flags |= MPTCP_PM_ADDR_FLAG_SUBFLOW;
382 
383 	err = mptcp_pm_parse_addr(raddr, info, &addr_r);
384 	if (err < 0) {
385 		NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
386 		goto create_err;
387 	}
388 
389 	if (!mptcp_pm_addr_families_match(sk, &entry.addr, &addr_r)) {
390 		GENL_SET_ERR_MSG(info, "families mismatch");
391 		err = -EINVAL;
392 		goto create_err;
393 	}
394 
395 	err = mptcp_userspace_pm_append_new_local_addr(msk, &entry, false);
396 	if (err < 0) {
397 		GENL_SET_ERR_MSG(info, "did not match address and id");
398 		goto create_err;
399 	}
400 
401 	local.addr = entry.addr;
402 	local.flags = entry.flags;
403 	local.ifindex = entry.ifindex;
404 
405 	lock_sock(sk);
406 	err = __mptcp_subflow_connect(sk, &local, &addr_r);
407 	release_sock(sk);
408 
409 	spin_lock_bh(&msk->pm.lock);
410 	if (err)
411 		mptcp_userspace_pm_delete_local_addr(msk, &entry);
412 	else
413 		msk->pm.subflows++;
414 	spin_unlock_bh(&msk->pm.lock);
415 
416  create_err:
417 	sock_put(sk);
418 	return err;
419 }
420 
mptcp_nl_find_ssk(struct mptcp_sock * msk,const struct mptcp_addr_info * local,const struct mptcp_addr_info * remote)421 static struct sock *mptcp_nl_find_ssk(struct mptcp_sock *msk,
422 				      const struct mptcp_addr_info *local,
423 				      const struct mptcp_addr_info *remote)
424 {
425 	struct mptcp_subflow_context *subflow;
426 
427 	if (local->family != remote->family)
428 		return NULL;
429 
430 	mptcp_for_each_subflow(msk, subflow) {
431 		const struct inet_sock *issk;
432 		struct sock *ssk;
433 
434 		ssk = mptcp_subflow_tcp_sock(subflow);
435 
436 		if (local->family != ssk->sk_family)
437 			continue;
438 
439 		issk = inet_sk(ssk);
440 
441 		switch (ssk->sk_family) {
442 		case AF_INET:
443 			if (issk->inet_saddr != local->addr.s_addr ||
444 			    issk->inet_daddr != remote->addr.s_addr)
445 				continue;
446 			break;
447 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
448 		case AF_INET6: {
449 			const struct ipv6_pinfo *pinfo = inet6_sk(ssk);
450 
451 			if (!ipv6_addr_equal(&local->addr6, &pinfo->saddr) ||
452 			    !ipv6_addr_equal(&remote->addr6, &ssk->sk_v6_daddr))
453 				continue;
454 			break;
455 		}
456 #endif
457 		default:
458 			continue;
459 		}
460 
461 		if (issk->inet_sport == local->port &&
462 		    issk->inet_dport == remote->port)
463 			return ssk;
464 	}
465 
466 	return NULL;
467 }
468 
mptcp_pm_nl_subflow_destroy_doit(struct sk_buff * skb,struct genl_info * info)469 int mptcp_pm_nl_subflow_destroy_doit(struct sk_buff *skb, struct genl_info *info)
470 {
471 	struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
472 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
473 	struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
474 	struct mptcp_addr_info addr_l;
475 	struct mptcp_addr_info addr_r;
476 	struct mptcp_sock *msk;
477 	struct sock *sk, *ssk;
478 	int err = -EINVAL;
479 	u32 token_val;
480 
481 	if (!laddr || !raddr || !token) {
482 		GENL_SET_ERR_MSG(info, "missing required inputs");
483 		return err;
484 	}
485 
486 	token_val = nla_get_u32(token);
487 
488 	msk = mptcp_token_get_sock(genl_info_net(info), token_val);
489 	if (!msk) {
490 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
491 		return err;
492 	}
493 
494 	sk = (struct sock *)msk;
495 
496 	if (!mptcp_pm_is_userspace(msk)) {
497 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
498 		goto destroy_err;
499 	}
500 
501 	err = mptcp_pm_parse_addr(laddr, info, &addr_l);
502 	if (err < 0) {
503 		NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
504 		goto destroy_err;
505 	}
506 
507 	err = mptcp_pm_parse_addr(raddr, info, &addr_r);
508 	if (err < 0) {
509 		NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
510 		goto destroy_err;
511 	}
512 
513 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
514 	if (addr_l.family == AF_INET && ipv6_addr_v4mapped(&addr_r.addr6)) {
515 		ipv6_addr_set_v4mapped(addr_l.addr.s_addr, &addr_l.addr6);
516 		addr_l.family = AF_INET6;
517 	}
518 	if (addr_r.family == AF_INET && ipv6_addr_v4mapped(&addr_l.addr6)) {
519 		ipv6_addr_set_v4mapped(addr_r.addr.s_addr, &addr_r.addr6);
520 		addr_r.family = AF_INET6;
521 	}
522 #endif
523 	if (addr_l.family != addr_r.family) {
524 		GENL_SET_ERR_MSG(info, "address families do not match");
525 		err = -EINVAL;
526 		goto destroy_err;
527 	}
528 
529 	if (!addr_l.port || !addr_r.port) {
530 		GENL_SET_ERR_MSG(info, "missing local or remote port");
531 		err = -EINVAL;
532 		goto destroy_err;
533 	}
534 
535 	lock_sock(sk);
536 	ssk = mptcp_nl_find_ssk(msk, &addr_l, &addr_r);
537 	if (ssk) {
538 		struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
539 		struct mptcp_pm_addr_entry entry = { .addr = addr_l };
540 
541 		spin_lock_bh(&msk->pm.lock);
542 		mptcp_userspace_pm_delete_local_addr(msk, &entry);
543 		spin_unlock_bh(&msk->pm.lock);
544 		mptcp_subflow_shutdown(sk, ssk, RCV_SHUTDOWN | SEND_SHUTDOWN);
545 		mptcp_close_ssk(sk, ssk, subflow);
546 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
547 		err = 0;
548 	} else {
549 		err = -ESRCH;
550 	}
551 	release_sock(sk);
552 
553 destroy_err:
554 	sock_put(sk);
555 	return err;
556 }
557 
mptcp_userspace_pm_set_flags(struct sk_buff * skb,struct genl_info * info)558 int mptcp_userspace_pm_set_flags(struct sk_buff *skb, struct genl_info *info)
559 {
560 	struct mptcp_pm_addr_entry loc = { .addr = { .family = AF_UNSPEC }, };
561 	struct mptcp_pm_addr_entry rem = { .addr = { .family = AF_UNSPEC }, };
562 	struct nlattr *attr_rem = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
563 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
564 	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
565 	struct net *net = sock_net(skb->sk);
566 	struct mptcp_pm_addr_entry *entry;
567 	struct mptcp_sock *msk;
568 	int ret = -EINVAL;
569 	struct sock *sk;
570 	u32 token_val;
571 	u8 bkup = 0;
572 
573 	token_val = nla_get_u32(token);
574 
575 	msk = mptcp_token_get_sock(net, token_val);
576 	if (!msk) {
577 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
578 		return ret;
579 	}
580 
581 	sk = (struct sock *)msk;
582 
583 	if (!mptcp_pm_is_userspace(msk)) {
584 		GENL_SET_ERR_MSG(info, "userspace PM not selected");
585 		goto set_flags_err;
586 	}
587 
588 	ret = mptcp_pm_parse_entry(attr, info, false, &loc);
589 	if (ret < 0)
590 		goto set_flags_err;
591 
592 	if (attr_rem) {
593 		ret = mptcp_pm_parse_entry(attr_rem, info, false, &rem);
594 		if (ret < 0)
595 			goto set_flags_err;
596 	}
597 
598 	if (loc.addr.family == AF_UNSPEC ||
599 	    rem.addr.family == AF_UNSPEC) {
600 		GENL_SET_ERR_MSG(info, "invalid address families");
601 		ret = -EINVAL;
602 		goto set_flags_err;
603 	}
604 
605 	if (loc.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
606 		bkup = 1;
607 
608 	spin_lock_bh(&msk->pm.lock);
609 	list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
610 		if (mptcp_addresses_equal(&entry->addr, &loc.addr, false)) {
611 			if (bkup)
612 				entry->flags |= MPTCP_PM_ADDR_FLAG_BACKUP;
613 			else
614 				entry->flags &= ~MPTCP_PM_ADDR_FLAG_BACKUP;
615 		}
616 	}
617 	spin_unlock_bh(&msk->pm.lock);
618 
619 	lock_sock(sk);
620 	ret = mptcp_pm_nl_mp_prio_send_ack(msk, &loc.addr, &rem.addr, bkup);
621 	release_sock(sk);
622 
623 set_flags_err:
624 	sock_put(sk);
625 	return ret;
626 }
627 
mptcp_userspace_pm_dump_addr(struct sk_buff * msg,struct netlink_callback * cb)628 int mptcp_userspace_pm_dump_addr(struct sk_buff *msg,
629 				 struct netlink_callback *cb)
630 {
631 	struct id_bitmap {
632 		DECLARE_BITMAP(map, MPTCP_PM_MAX_ADDR_ID + 1);
633 	} *bitmap;
634 	const struct genl_info *info = genl_info_dump(cb);
635 	struct net *net = sock_net(msg->sk);
636 	struct mptcp_pm_addr_entry *entry;
637 	struct mptcp_sock *msk;
638 	struct nlattr *token;
639 	int ret = -EINVAL;
640 	struct sock *sk;
641 	void *hdr;
642 
643 	bitmap = (struct id_bitmap *)cb->ctx;
644 	token = info->attrs[MPTCP_PM_ATTR_TOKEN];
645 
646 	msk = mptcp_token_get_sock(net, nla_get_u32(token));
647 	if (!msk) {
648 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
649 		return ret;
650 	}
651 
652 	sk = (struct sock *)msk;
653 
654 	if (!mptcp_pm_is_userspace(msk)) {
655 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
656 		goto out;
657 	}
658 
659 	lock_sock(sk);
660 	spin_lock_bh(&msk->pm.lock);
661 	list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
662 		if (test_bit(entry->addr.id, bitmap->map))
663 			continue;
664 
665 		hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
666 				  cb->nlh->nlmsg_seq, &mptcp_genl_family,
667 				  NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
668 		if (!hdr)
669 			break;
670 
671 		if (mptcp_nl_fill_addr(msg, entry) < 0) {
672 			genlmsg_cancel(msg, hdr);
673 			break;
674 		}
675 
676 		__set_bit(entry->addr.id, bitmap->map);
677 		genlmsg_end(msg, hdr);
678 	}
679 	spin_unlock_bh(&msk->pm.lock);
680 	release_sock(sk);
681 	ret = msg->len;
682 
683 out:
684 	sock_put(sk);
685 	return ret;
686 }
687 
mptcp_userspace_pm_get_addr(struct sk_buff * skb,struct genl_info * info)688 int mptcp_userspace_pm_get_addr(struct sk_buff *skb,
689 				struct genl_info *info)
690 {
691 	struct nlattr *attr = info->attrs[MPTCP_PM_ENDPOINT_ADDR];
692 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
693 	struct mptcp_pm_addr_entry addr, *entry;
694 	struct net *net = sock_net(skb->sk);
695 	struct mptcp_sock *msk;
696 	struct sk_buff *msg;
697 	int ret = -EINVAL;
698 	struct sock *sk;
699 	void *reply;
700 
701 	msk = mptcp_token_get_sock(net, nla_get_u32(token));
702 	if (!msk) {
703 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
704 		return ret;
705 	}
706 
707 	sk = (struct sock *)msk;
708 
709 	if (!mptcp_pm_is_userspace(msk)) {
710 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
711 		goto out;
712 	}
713 
714 	ret = mptcp_pm_parse_entry(attr, info, false, &addr);
715 	if (ret < 0)
716 		goto out;
717 
718 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
719 	if (!msg) {
720 		ret = -ENOMEM;
721 		goto out;
722 	}
723 
724 	reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
725 				  info->genlhdr->cmd);
726 	if (!reply) {
727 		GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
728 		ret = -EMSGSIZE;
729 		goto fail;
730 	}
731 
732 	lock_sock(sk);
733 	spin_lock_bh(&msk->pm.lock);
734 	entry = mptcp_userspace_pm_lookup_addr_by_id(msk, addr.addr.id);
735 	if (!entry) {
736 		GENL_SET_ERR_MSG(info, "address not found");
737 		ret = -EINVAL;
738 		goto unlock_fail;
739 	}
740 
741 	ret = mptcp_nl_fill_addr(msg, entry);
742 	if (ret)
743 		goto unlock_fail;
744 
745 	genlmsg_end(msg, reply);
746 	ret = genlmsg_reply(msg, info);
747 	spin_unlock_bh(&msk->pm.lock);
748 	release_sock(sk);
749 	sock_put(sk);
750 	return ret;
751 
752 unlock_fail:
753 	spin_unlock_bh(&msk->pm.lock);
754 	release_sock(sk);
755 fail:
756 	nlmsg_free(msg);
757 out:
758 	sock_put(sk);
759 	return ret;
760 }
761