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