• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
mptcp_free_local_addr_list(struct mptcp_sock * msk)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 
mptcp_userspace_pm_append_new_local_addr(struct mptcp_sock * msk,struct mptcp_pm_addr_entry * entry,bool needs_id)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 			spin_unlock_bh(&msk->pm.lock);
64 			return -ENOMEM;
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 	spin_unlock_bh(&msk->pm.lock);
80 	return ret;
81 }
82 
83 /* If the subflow is closed from the other peer (not via a
84  * subflow destroy command then), we want to keep the entry
85  * not to assign the same ID to another address and to be
86  * able to send RM_ADDR after the removal of the subflow.
87  */
mptcp_userspace_pm_delete_local_addr(struct mptcp_sock * msk,struct mptcp_pm_addr_entry * addr)88 static int mptcp_userspace_pm_delete_local_addr(struct mptcp_sock *msk,
89 						struct mptcp_pm_addr_entry *addr)
90 {
91 	struct mptcp_pm_addr_entry *entry, *tmp;
92 
93 	list_for_each_entry_safe(entry, tmp, &msk->pm.userspace_pm_local_addr_list, list) {
94 		if (mptcp_addresses_equal(&entry->addr, &addr->addr, false)) {
95 			/* TODO: a refcount is needed because the entry can
96 			 * be used multiple times (e.g. fullmesh mode).
97 			 */
98 			list_del_rcu(&entry->list);
99 			kfree(entry);
100 			msk->pm.local_addr_used--;
101 			return 0;
102 		}
103 	}
104 
105 	return -EINVAL;
106 }
107 
mptcp_userspace_pm_get_flags_and_ifindex_by_id(struct mptcp_sock * msk,unsigned int id,u8 * flags,int * ifindex)108 int mptcp_userspace_pm_get_flags_and_ifindex_by_id(struct mptcp_sock *msk,
109 						   unsigned int id,
110 						   u8 *flags, int *ifindex)
111 {
112 	struct mptcp_pm_addr_entry *entry, *match = NULL;
113 
114 	*flags = 0;
115 	*ifindex = 0;
116 
117 	spin_lock_bh(&msk->pm.lock);
118 	list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
119 		if (id == entry->addr.id) {
120 			match = entry;
121 			break;
122 		}
123 	}
124 	spin_unlock_bh(&msk->pm.lock);
125 	if (match) {
126 		*flags = match->flags;
127 		*ifindex = match->ifindex;
128 	}
129 
130 	return 0;
131 }
132 
mptcp_userspace_pm_get_local_id(struct mptcp_sock * msk,struct mptcp_addr_info * skc)133 int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk,
134 				    struct mptcp_addr_info *skc)
135 {
136 	struct mptcp_pm_addr_entry *entry = NULL, *e, new_entry;
137 	__be16 msk_sport =  ((struct inet_sock *)
138 			     inet_sk((struct sock *)msk))->inet_sport;
139 
140 	spin_lock_bh(&msk->pm.lock);
141 	list_for_each_entry(e, &msk->pm.userspace_pm_local_addr_list, list) {
142 		if (mptcp_addresses_equal(&e->addr, skc, false)) {
143 			entry = e;
144 			break;
145 		}
146 	}
147 	spin_unlock_bh(&msk->pm.lock);
148 	if (entry)
149 		return entry->addr.id;
150 
151 	memset(&new_entry, 0, sizeof(struct mptcp_pm_addr_entry));
152 	new_entry.addr = *skc;
153 	new_entry.addr.id = 0;
154 	new_entry.flags = MPTCP_PM_ADDR_FLAG_IMPLICIT;
155 
156 	if (new_entry.addr.port == msk_sport)
157 		new_entry.addr.port = 0;
158 
159 	return mptcp_userspace_pm_append_new_local_addr(msk, &new_entry, true);
160 }
161 
mptcp_nl_cmd_announce(struct sk_buff * skb,struct genl_info * info)162 int mptcp_nl_cmd_announce(struct sk_buff *skb, struct genl_info *info)
163 {
164 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
165 	struct nlattr *addr = info->attrs[MPTCP_PM_ATTR_ADDR];
166 	struct mptcp_pm_addr_entry addr_val;
167 	struct mptcp_sock *msk;
168 	int err = -EINVAL;
169 	u32 token_val;
170 
171 	if (!addr || !token) {
172 		GENL_SET_ERR_MSG(info, "missing required inputs");
173 		return err;
174 	}
175 
176 	token_val = nla_get_u32(token);
177 
178 	msk = mptcp_token_get_sock(sock_net(skb->sk), token_val);
179 	if (!msk) {
180 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
181 		return err;
182 	}
183 
184 	if (!mptcp_pm_is_userspace(msk)) {
185 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
186 		goto announce_err;
187 	}
188 
189 	err = mptcp_pm_parse_entry(addr, info, true, &addr_val);
190 	if (err < 0) {
191 		GENL_SET_ERR_MSG(info, "error parsing local address");
192 		goto announce_err;
193 	}
194 
195 	if (addr_val.addr.id == 0 || !(addr_val.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
196 		GENL_SET_ERR_MSG(info, "invalid addr id or flags");
197 		err = -EINVAL;
198 		goto announce_err;
199 	}
200 
201 	err = mptcp_userspace_pm_append_new_local_addr(msk, &addr_val, false);
202 	if (err < 0) {
203 		GENL_SET_ERR_MSG(info, "did not match address and id");
204 		goto announce_err;
205 	}
206 
207 	lock_sock((struct sock *)msk);
208 	spin_lock_bh(&msk->pm.lock);
209 
210 	if (mptcp_pm_alloc_anno_list(msk, &addr_val)) {
211 		msk->pm.add_addr_signaled++;
212 		mptcp_pm_announce_addr(msk, &addr_val.addr, false);
213 		mptcp_pm_nl_addr_send_ack(msk);
214 	}
215 
216 	spin_unlock_bh(&msk->pm.lock);
217 	release_sock((struct sock *)msk);
218 
219 	err = 0;
220  announce_err:
221 	sock_put((struct sock *)msk);
222 	return err;
223 }
224 
mptcp_userspace_pm_remove_id_zero_address(struct mptcp_sock * msk,struct genl_info * info)225 static int mptcp_userspace_pm_remove_id_zero_address(struct mptcp_sock *msk,
226 						     struct genl_info *info)
227 {
228 	struct mptcp_rm_list list = { .nr = 0 };
229 	struct mptcp_subflow_context *subflow;
230 	struct sock *sk = (struct sock *)msk;
231 	bool has_id_0 = false;
232 	int err = -EINVAL;
233 
234 	lock_sock(sk);
235 	mptcp_for_each_subflow(msk, subflow) {
236 		if (subflow->local_id == 0) {
237 			has_id_0 = true;
238 			break;
239 		}
240 	}
241 	if (!has_id_0) {
242 		GENL_SET_ERR_MSG(info, "address with id 0 not found");
243 		goto remove_err;
244 	}
245 
246 	list.ids[list.nr++] = 0;
247 
248 	spin_lock_bh(&msk->pm.lock);
249 	mptcp_pm_remove_addr(msk, &list);
250 	spin_unlock_bh(&msk->pm.lock);
251 
252 	err = 0;
253 
254 remove_err:
255 	release_sock(sk);
256 	return err;
257 }
258 
mptcp_nl_cmd_remove(struct sk_buff * skb,struct genl_info * info)259 int mptcp_nl_cmd_remove(struct sk_buff *skb, struct genl_info *info)
260 {
261 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
262 	struct nlattr *id = info->attrs[MPTCP_PM_ATTR_LOC_ID];
263 	struct mptcp_pm_addr_entry *match = NULL;
264 	struct mptcp_pm_addr_entry *entry;
265 	struct mptcp_sock *msk;
266 	LIST_HEAD(free_list);
267 	int err = -EINVAL;
268 	u32 token_val;
269 	u8 id_val;
270 
271 	if (!id || !token) {
272 		GENL_SET_ERR_MSG(info, "missing required inputs");
273 		return err;
274 	}
275 
276 	id_val = nla_get_u8(id);
277 	token_val = nla_get_u32(token);
278 
279 	msk = mptcp_token_get_sock(sock_net(skb->sk), token_val);
280 	if (!msk) {
281 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
282 		return err;
283 	}
284 
285 	if (!mptcp_pm_is_userspace(msk)) {
286 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
287 		goto remove_err;
288 	}
289 
290 	if (id_val == 0) {
291 		err = mptcp_userspace_pm_remove_id_zero_address(msk, info);
292 		goto remove_err;
293 	}
294 
295 	lock_sock((struct sock *)msk);
296 
297 	list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
298 		if (entry->addr.id == id_val) {
299 			match = entry;
300 			break;
301 		}
302 	}
303 
304 	if (!match) {
305 		GENL_SET_ERR_MSG(info, "address with specified id not found");
306 		release_sock((struct sock *)msk);
307 		goto remove_err;
308 	}
309 
310 	list_move(&match->list, &free_list);
311 
312 	mptcp_pm_remove_addrs(msk, &free_list);
313 
314 	release_sock((struct sock *)msk);
315 
316 	list_for_each_entry_safe(match, entry, &free_list, list) {
317 		sock_kfree_s((struct sock *)msk, match, sizeof(*match));
318 	}
319 
320 	err = 0;
321  remove_err:
322 	sock_put((struct sock *)msk);
323 	return err;
324 }
325 
mptcp_nl_cmd_sf_create(struct sk_buff * skb,struct genl_info * info)326 int mptcp_nl_cmd_sf_create(struct sk_buff *skb, struct genl_info *info)
327 {
328 	struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
329 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
330 	struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
331 	struct mptcp_pm_addr_entry local = { 0 };
332 	struct mptcp_addr_info addr_r;
333 	struct mptcp_addr_info addr_l;
334 	struct mptcp_sock *msk;
335 	int err = -EINVAL;
336 	struct sock *sk;
337 	u32 token_val;
338 
339 	if (!laddr || !raddr || !token) {
340 		GENL_SET_ERR_MSG(info, "missing required inputs");
341 		return err;
342 	}
343 
344 	token_val = nla_get_u32(token);
345 
346 	msk = mptcp_token_get_sock(genl_info_net(info), token_val);
347 	if (!msk) {
348 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
349 		return err;
350 	}
351 
352 	if (!mptcp_pm_is_userspace(msk)) {
353 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
354 		goto create_err;
355 	}
356 
357 	err = mptcp_pm_parse_addr(laddr, info, &addr_l);
358 	if (err < 0) {
359 		NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
360 		goto create_err;
361 	}
362 
363 	err = mptcp_pm_parse_addr(raddr, info, &addr_r);
364 	if (err < 0) {
365 		NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
366 		goto create_err;
367 	}
368 
369 	sk = &msk->sk.icsk_inet.sk;
370 
371 	if (!mptcp_pm_addr_families_match(sk, &addr_l, &addr_r)) {
372 		GENL_SET_ERR_MSG(info, "families mismatch");
373 		err = -EINVAL;
374 		goto create_err;
375 	}
376 
377 	local.addr = addr_l;
378 	err = mptcp_userspace_pm_append_new_local_addr(msk, &local, false);
379 	if (err < 0) {
380 		GENL_SET_ERR_MSG(info, "did not match address and id");
381 		goto create_err;
382 	}
383 
384 	lock_sock(sk);
385 
386 	err = __mptcp_subflow_connect(sk, &addr_l, &addr_r);
387 
388 	release_sock(sk);
389 
390 	spin_lock_bh(&msk->pm.lock);
391 	if (err)
392 		mptcp_userspace_pm_delete_local_addr(msk, &local);
393 	else
394 		msk->pm.subflows++;
395 	spin_unlock_bh(&msk->pm.lock);
396 
397  create_err:
398 	sock_put((struct sock *)msk);
399 	return err;
400 }
401 
mptcp_nl_find_ssk(struct mptcp_sock * msk,const struct mptcp_addr_info * local,const struct mptcp_addr_info * remote)402 static struct sock *mptcp_nl_find_ssk(struct mptcp_sock *msk,
403 				      const struct mptcp_addr_info *local,
404 				      const struct mptcp_addr_info *remote)
405 {
406 	struct mptcp_subflow_context *subflow;
407 
408 	if (local->family != remote->family)
409 		return NULL;
410 
411 	mptcp_for_each_subflow(msk, subflow) {
412 		const struct inet_sock *issk;
413 		struct sock *ssk;
414 
415 		ssk = mptcp_subflow_tcp_sock(subflow);
416 
417 		if (local->family != ssk->sk_family)
418 			continue;
419 
420 		issk = inet_sk(ssk);
421 
422 		switch (ssk->sk_family) {
423 		case AF_INET:
424 			if (issk->inet_saddr != local->addr.s_addr ||
425 			    issk->inet_daddr != remote->addr.s_addr)
426 				continue;
427 			break;
428 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
429 		case AF_INET6: {
430 			const struct ipv6_pinfo *pinfo = inet6_sk(ssk);
431 
432 			if (!ipv6_addr_equal(&local->addr6, &pinfo->saddr) ||
433 			    !ipv6_addr_equal(&remote->addr6, &ssk->sk_v6_daddr))
434 				continue;
435 			break;
436 		}
437 #endif
438 		default:
439 			continue;
440 		}
441 
442 		if (issk->inet_sport == local->port &&
443 		    issk->inet_dport == remote->port)
444 			return ssk;
445 	}
446 
447 	return NULL;
448 }
449 
mptcp_nl_cmd_sf_destroy(struct sk_buff * skb,struct genl_info * info)450 int mptcp_nl_cmd_sf_destroy(struct sk_buff *skb, struct genl_info *info)
451 {
452 	struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
453 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
454 	struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
455 	struct mptcp_addr_info addr_l;
456 	struct mptcp_addr_info addr_r;
457 	struct mptcp_sock *msk;
458 	struct sock *sk, *ssk;
459 	int err = -EINVAL;
460 	u32 token_val;
461 
462 	if (!laddr || !raddr || !token) {
463 		GENL_SET_ERR_MSG(info, "missing required inputs");
464 		return err;
465 	}
466 
467 	token_val = nla_get_u32(token);
468 
469 	msk = mptcp_token_get_sock(genl_info_net(info), token_val);
470 	if (!msk) {
471 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
472 		return err;
473 	}
474 
475 	if (!mptcp_pm_is_userspace(msk)) {
476 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
477 		goto destroy_err;
478 	}
479 
480 	err = mptcp_pm_parse_addr(laddr, info, &addr_l);
481 	if (err < 0) {
482 		NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
483 		goto destroy_err;
484 	}
485 
486 	err = mptcp_pm_parse_addr(raddr, info, &addr_r);
487 	if (err < 0) {
488 		NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
489 		goto destroy_err;
490 	}
491 
492 	if (addr_l.family != addr_r.family) {
493 		GENL_SET_ERR_MSG(info, "address families do not match");
494 		err = -EINVAL;
495 		goto destroy_err;
496 	}
497 
498 	if (!addr_l.port || !addr_r.port) {
499 		GENL_SET_ERR_MSG(info, "missing local or remote port");
500 		err = -EINVAL;
501 		goto destroy_err;
502 	}
503 
504 	sk = &msk->sk.icsk_inet.sk;
505 	lock_sock(sk);
506 	ssk = mptcp_nl_find_ssk(msk, &addr_l, &addr_r);
507 	if (ssk) {
508 		struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
509 		struct mptcp_pm_addr_entry entry = { .addr = addr_l };
510 
511 		spin_lock_bh(&msk->pm.lock);
512 		mptcp_userspace_pm_delete_local_addr(msk, &entry);
513 		spin_unlock_bh(&msk->pm.lock);
514 		mptcp_subflow_shutdown(sk, ssk, RCV_SHUTDOWN | SEND_SHUTDOWN);
515 		mptcp_close_ssk(sk, ssk, subflow);
516 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
517 		err = 0;
518 	} else {
519 		err = -ESRCH;
520 	}
521 	release_sock(sk);
522 
523 destroy_err:
524 	sock_put((struct sock *)msk);
525 	return err;
526 }
527 
mptcp_userspace_pm_set_flags(struct net * net,struct nlattr * token,struct mptcp_pm_addr_entry * loc,struct mptcp_pm_addr_entry * rem,u8 bkup)528 int mptcp_userspace_pm_set_flags(struct net *net, struct nlattr *token,
529 				 struct mptcp_pm_addr_entry *loc,
530 				 struct mptcp_pm_addr_entry *rem, u8 bkup)
531 {
532 	struct mptcp_sock *msk;
533 	int ret = -EINVAL;
534 	u32 token_val;
535 
536 	token_val = nla_get_u32(token);
537 
538 	msk = mptcp_token_get_sock(net, token_val);
539 	if (!msk)
540 		return ret;
541 
542 	if (!mptcp_pm_is_userspace(msk))
543 		goto set_flags_err;
544 
545 	if (loc->addr.family == AF_UNSPEC ||
546 	    rem->addr.family == AF_UNSPEC)
547 		goto set_flags_err;
548 
549 	lock_sock((struct sock *)msk);
550 	ret = mptcp_pm_nl_mp_prio_send_ack(msk, &loc->addr, &rem->addr, bkup);
551 	release_sock((struct sock *)msk);
552 
553 set_flags_err:
554 	sock_put((struct sock *)msk);
555 	return ret;
556 }
557