xref: /openbmc/linux/net/mptcp/pm_userspace.c (revision d5a9588c)
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 int mptcp_userspace_pm_get_flags_and_ifindex_by_id(struct mptcp_sock *msk,
110 						   unsigned int id,
111 						   u8 *flags, int *ifindex)
112 {
113 	struct mptcp_pm_addr_entry *entry, *match = NULL;
114 
115 	spin_lock_bh(&msk->pm.lock);
116 	list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
117 		if (id == entry->addr.id) {
118 			match = entry;
119 			break;
120 		}
121 	}
122 	spin_unlock_bh(&msk->pm.lock);
123 	if (match) {
124 		*flags = match->flags;
125 		*ifindex = match->ifindex;
126 	}
127 
128 	return 0;
129 }
130 
131 int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk,
132 				    struct mptcp_addr_info *skc)
133 {
134 	struct mptcp_pm_addr_entry *entry = NULL, *e, new_entry;
135 	__be16 msk_sport =  ((struct inet_sock *)
136 			     inet_sk((struct sock *)msk))->inet_sport;
137 
138 	spin_lock_bh(&msk->pm.lock);
139 	list_for_each_entry(e, &msk->pm.userspace_pm_local_addr_list, list) {
140 		if (mptcp_addresses_equal(&e->addr, skc, false)) {
141 			entry = e;
142 			break;
143 		}
144 	}
145 	spin_unlock_bh(&msk->pm.lock);
146 	if (entry)
147 		return entry->addr.id;
148 
149 	memset(&new_entry, 0, sizeof(struct mptcp_pm_addr_entry));
150 	new_entry.addr = *skc;
151 	new_entry.addr.id = 0;
152 	new_entry.flags = MPTCP_PM_ADDR_FLAG_IMPLICIT;
153 
154 	if (new_entry.addr.port == msk_sport)
155 		new_entry.addr.port = 0;
156 
157 	return mptcp_userspace_pm_append_new_local_addr(msk, &new_entry, true);
158 }
159 
160 bool mptcp_userspace_pm_is_backup(struct mptcp_sock *msk,
161 				  struct mptcp_addr_info *skc)
162 {
163 	struct mptcp_pm_addr_entry *entry;
164 	bool backup = false;
165 
166 	spin_lock_bh(&msk->pm.lock);
167 	list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
168 		if (mptcp_addresses_equal(&entry->addr, skc, false)) {
169 			backup = !!(entry->flags & MPTCP_PM_ADDR_FLAG_BACKUP);
170 			break;
171 		}
172 	}
173 	spin_unlock_bh(&msk->pm.lock);
174 
175 	return backup;
176 }
177 
178 int mptcp_nl_cmd_announce(struct sk_buff *skb, struct genl_info *info)
179 {
180 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
181 	struct nlattr *addr = info->attrs[MPTCP_PM_ATTR_ADDR];
182 	struct mptcp_pm_addr_entry addr_val;
183 	struct mptcp_sock *msk;
184 	int err = -EINVAL;
185 	u32 token_val;
186 
187 	if (!addr || !token) {
188 		GENL_SET_ERR_MSG(info, "missing required inputs");
189 		return err;
190 	}
191 
192 	token_val = nla_get_u32(token);
193 
194 	msk = mptcp_token_get_sock(sock_net(skb->sk), token_val);
195 	if (!msk) {
196 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
197 		return err;
198 	}
199 
200 	if (!mptcp_pm_is_userspace(msk)) {
201 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
202 		goto announce_err;
203 	}
204 
205 	err = mptcp_pm_parse_entry(addr, info, true, &addr_val);
206 	if (err < 0) {
207 		GENL_SET_ERR_MSG(info, "error parsing local address");
208 		goto announce_err;
209 	}
210 
211 	if (addr_val.addr.id == 0 || !(addr_val.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
212 		GENL_SET_ERR_MSG(info, "invalid addr id or flags");
213 		err = -EINVAL;
214 		goto announce_err;
215 	}
216 
217 	err = mptcp_userspace_pm_append_new_local_addr(msk, &addr_val, false);
218 	if (err < 0) {
219 		GENL_SET_ERR_MSG(info, "did not match address and id");
220 		goto announce_err;
221 	}
222 
223 	lock_sock((struct sock *)msk);
224 	spin_lock_bh(&msk->pm.lock);
225 
226 	if (mptcp_pm_alloc_anno_list(msk, &addr_val.addr)) {
227 		msk->pm.add_addr_signaled++;
228 		mptcp_pm_announce_addr(msk, &addr_val.addr, false);
229 		mptcp_pm_nl_addr_send_ack(msk);
230 	}
231 
232 	spin_unlock_bh(&msk->pm.lock);
233 	release_sock((struct sock *)msk);
234 
235 	err = 0;
236  announce_err:
237 	sock_put((struct sock *)msk);
238 	return err;
239 }
240 
241 static int mptcp_userspace_pm_remove_id_zero_address(struct mptcp_sock *msk,
242 						     struct genl_info *info)
243 {
244 	struct mptcp_rm_list list = { .nr = 0 };
245 	struct mptcp_subflow_context *subflow;
246 	struct sock *sk = (struct sock *)msk;
247 	bool has_id_0 = false;
248 	int err = -EINVAL;
249 
250 	lock_sock(sk);
251 	mptcp_for_each_subflow(msk, subflow) {
252 		if (subflow->local_id == 0) {
253 			has_id_0 = true;
254 			break;
255 		}
256 	}
257 	if (!has_id_0) {
258 		GENL_SET_ERR_MSG(info, "address with id 0 not found");
259 		goto remove_err;
260 	}
261 
262 	list.ids[list.nr++] = 0;
263 
264 	spin_lock_bh(&msk->pm.lock);
265 	mptcp_pm_remove_addr(msk, &list);
266 	spin_unlock_bh(&msk->pm.lock);
267 
268 	err = 0;
269 
270 remove_err:
271 	release_sock(sk);
272 	return err;
273 }
274 
275 int mptcp_nl_cmd_remove(struct sk_buff *skb, struct genl_info *info)
276 {
277 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
278 	struct nlattr *id = info->attrs[MPTCP_PM_ATTR_LOC_ID];
279 	struct mptcp_pm_addr_entry *match = NULL;
280 	struct mptcp_pm_addr_entry *entry;
281 	struct mptcp_sock *msk;
282 	LIST_HEAD(free_list);
283 	int err = -EINVAL;
284 	u32 token_val;
285 	u8 id_val;
286 
287 	if (!id || !token) {
288 		GENL_SET_ERR_MSG(info, "missing required inputs");
289 		return err;
290 	}
291 
292 	id_val = nla_get_u8(id);
293 	token_val = nla_get_u32(token);
294 
295 	msk = mptcp_token_get_sock(sock_net(skb->sk), token_val);
296 	if (!msk) {
297 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
298 		return err;
299 	}
300 
301 	if (!mptcp_pm_is_userspace(msk)) {
302 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
303 		goto remove_err;
304 	}
305 
306 	if (id_val == 0) {
307 		err = mptcp_userspace_pm_remove_id_zero_address(msk, info);
308 		goto remove_err;
309 	}
310 
311 	lock_sock((struct sock *)msk);
312 
313 	list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
314 		if (entry->addr.id == id_val) {
315 			match = entry;
316 			break;
317 		}
318 	}
319 
320 	if (!match) {
321 		GENL_SET_ERR_MSG(info, "address with specified id not found");
322 		release_sock((struct sock *)msk);
323 		goto remove_err;
324 	}
325 
326 	list_move(&match->list, &free_list);
327 
328 	mptcp_pm_remove_addrs(msk, &free_list);
329 
330 	release_sock((struct sock *)msk);
331 
332 	list_for_each_entry_safe(match, entry, &free_list, list) {
333 		sock_kfree_s((struct sock *)msk, match, sizeof(*match));
334 	}
335 
336 	err = 0;
337  remove_err:
338 	sock_put((struct sock *)msk);
339 	return err;
340 }
341 
342 int mptcp_nl_cmd_sf_create(struct sk_buff *skb, struct genl_info *info)
343 {
344 	struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
345 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
346 	struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
347 	struct mptcp_pm_addr_entry local = { 0 };
348 	struct mptcp_addr_info addr_r;
349 	struct mptcp_addr_info addr_l;
350 	struct mptcp_sock *msk;
351 	int err = -EINVAL;
352 	struct sock *sk;
353 	u32 token_val;
354 
355 	if (!laddr || !raddr || !token) {
356 		GENL_SET_ERR_MSG(info, "missing required inputs");
357 		return err;
358 	}
359 
360 	token_val = nla_get_u32(token);
361 
362 	msk = mptcp_token_get_sock(genl_info_net(info), token_val);
363 	if (!msk) {
364 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
365 		return err;
366 	}
367 
368 	if (!mptcp_pm_is_userspace(msk)) {
369 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
370 		goto create_err;
371 	}
372 
373 	err = mptcp_pm_parse_addr(laddr, info, &addr_l);
374 	if (err < 0) {
375 		NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
376 		goto create_err;
377 	}
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 	sk = (struct sock *)msk;
386 
387 	if (!mptcp_pm_addr_families_match(sk, &addr_l, &addr_r)) {
388 		GENL_SET_ERR_MSG(info, "families mismatch");
389 		err = -EINVAL;
390 		goto create_err;
391 	}
392 
393 	local.addr = addr_l;
394 	err = mptcp_userspace_pm_append_new_local_addr(msk, &local, false);
395 	if (err < 0) {
396 		GENL_SET_ERR_MSG(info, "did not match address and id");
397 		goto create_err;
398 	}
399 
400 	lock_sock(sk);
401 
402 	err = __mptcp_subflow_connect(sk, &addr_l, &addr_r);
403 
404 	release_sock(sk);
405 
406 	spin_lock_bh(&msk->pm.lock);
407 	if (err)
408 		mptcp_userspace_pm_delete_local_addr(msk, &local);
409 	else
410 		msk->pm.subflows++;
411 	spin_unlock_bh(&msk->pm.lock);
412 
413  create_err:
414 	sock_put((struct sock *)msk);
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_nl_cmd_sf_destroy(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 	if (!mptcp_pm_is_userspace(msk)) {
492 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
493 		goto destroy_err;
494 	}
495 
496 	err = mptcp_pm_parse_addr(laddr, info, &addr_l);
497 	if (err < 0) {
498 		NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
499 		goto destroy_err;
500 	}
501 
502 	err = mptcp_pm_parse_addr(raddr, info, &addr_r);
503 	if (err < 0) {
504 		NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
505 		goto destroy_err;
506 	}
507 
508 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
509 	if (addr_l.family == AF_INET && ipv6_addr_v4mapped(&addr_r.addr6)) {
510 		ipv6_addr_set_v4mapped(addr_l.addr.s_addr, &addr_l.addr6);
511 		addr_l.family = AF_INET6;
512 	}
513 	if (addr_r.family == AF_INET && ipv6_addr_v4mapped(&addr_l.addr6)) {
514 		ipv6_addr_set_v4mapped(addr_r.addr.s_addr, &addr_r.addr6);
515 		addr_r.family = AF_INET6;
516 	}
517 #endif
518 	if (addr_l.family != addr_r.family) {
519 		GENL_SET_ERR_MSG(info, "address families do not match");
520 		err = -EINVAL;
521 		goto destroy_err;
522 	}
523 
524 	if (!addr_l.port || !addr_r.port) {
525 		GENL_SET_ERR_MSG(info, "missing local or remote port");
526 		err = -EINVAL;
527 		goto destroy_err;
528 	}
529 
530 	sk = (struct sock *)msk;
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((struct sock *)msk);
551 	return err;
552 }
553 
554 int mptcp_userspace_pm_set_flags(struct net *net, struct nlattr *token,
555 				 struct mptcp_pm_addr_entry *loc,
556 				 struct mptcp_pm_addr_entry *rem, u8 bkup)
557 {
558 	struct mptcp_sock *msk;
559 	int ret = -EINVAL;
560 	u32 token_val;
561 
562 	token_val = nla_get_u32(token);
563 
564 	msk = mptcp_token_get_sock(net, token_val);
565 	if (!msk)
566 		return ret;
567 
568 	if (!mptcp_pm_is_userspace(msk))
569 		goto set_flags_err;
570 
571 	if (loc->addr.family == AF_UNSPEC ||
572 	    rem->addr.family == AF_UNSPEC)
573 		goto set_flags_err;
574 
575 	lock_sock((struct sock *)msk);
576 	ret = mptcp_pm_nl_mp_prio_send_ack(msk, &loc->addr, &rem->addr, bkup);
577 	release_sock((struct sock *)msk);
578 
579 set_flags_err:
580 	sock_put((struct sock *)msk);
581 	return ret;
582 }
583