xref: /openbmc/linux/net/mptcp/pm_userspace.c (revision 6aeadf78)
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 {
31 	DECLARE_BITMAP(id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
32 	struct mptcp_pm_addr_entry *match = NULL;
33 	struct sock *sk = (struct sock *)msk;
34 	struct mptcp_pm_addr_entry *e;
35 	bool addr_match = false;
36 	bool id_match = false;
37 	int ret = -EINVAL;
38 
39 	bitmap_zero(id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
40 
41 	spin_lock_bh(&msk->pm.lock);
42 	list_for_each_entry(e, &msk->pm.userspace_pm_local_addr_list, list) {
43 		addr_match = mptcp_addresses_equal(&e->addr, &entry->addr, true);
44 		if (addr_match && entry->addr.id == 0)
45 			entry->addr.id = e->addr.id;
46 		id_match = (e->addr.id == entry->addr.id);
47 		if (addr_match && id_match) {
48 			match = e;
49 			break;
50 		} else if (addr_match || id_match) {
51 			break;
52 		}
53 		__set_bit(e->addr.id, id_bitmap);
54 	}
55 
56 	if (!match && !addr_match && !id_match) {
57 		/* Memory for the entry is allocated from the
58 		 * sock option buffer.
59 		 */
60 		e = sock_kmalloc(sk, sizeof(*e), GFP_ATOMIC);
61 		if (!e) {
62 			ret = -ENOMEM;
63 			goto append_err;
64 		}
65 
66 		*e = *entry;
67 		if (!e->addr.id)
68 			e->addr.id = find_next_zero_bit(id_bitmap,
69 							MPTCP_PM_MAX_ADDR_ID + 1,
70 							1);
71 		list_add_tail_rcu(&e->list, &msk->pm.userspace_pm_local_addr_list);
72 		msk->pm.local_addr_used++;
73 		ret = e->addr.id;
74 	} else if (match) {
75 		ret = entry->addr.id;
76 	}
77 
78 append_err:
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  */
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 
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 
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 new_entry;
137 	__be16 msk_sport =  ((struct inet_sock *)
138 			     inet_sk((struct sock *)msk))->inet_sport;
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);
149 }
150 
151 int mptcp_nl_cmd_announce(struct sk_buff *skb, struct genl_info *info)
152 {
153 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
154 	struct nlattr *addr = info->attrs[MPTCP_PM_ATTR_ADDR];
155 	struct mptcp_pm_addr_entry addr_val;
156 	struct mptcp_sock *msk;
157 	int err = -EINVAL;
158 	u32 token_val;
159 
160 	if (!addr || !token) {
161 		GENL_SET_ERR_MSG(info, "missing required inputs");
162 		return err;
163 	}
164 
165 	token_val = nla_get_u32(token);
166 
167 	msk = mptcp_token_get_sock(sock_net(skb->sk), token_val);
168 	if (!msk) {
169 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
170 		return err;
171 	}
172 
173 	if (!mptcp_pm_is_userspace(msk)) {
174 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
175 		goto announce_err;
176 	}
177 
178 	err = mptcp_pm_parse_entry(addr, info, true, &addr_val);
179 	if (err < 0) {
180 		GENL_SET_ERR_MSG(info, "error parsing local address");
181 		goto announce_err;
182 	}
183 
184 	if (addr_val.addr.id == 0 || !(addr_val.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
185 		GENL_SET_ERR_MSG(info, "invalid addr id or flags");
186 		err = -EINVAL;
187 		goto announce_err;
188 	}
189 
190 	err = mptcp_userspace_pm_append_new_local_addr(msk, &addr_val);
191 	if (err < 0) {
192 		GENL_SET_ERR_MSG(info, "did not match address and id");
193 		goto announce_err;
194 	}
195 
196 	lock_sock((struct sock *)msk);
197 	spin_lock_bh(&msk->pm.lock);
198 
199 	if (mptcp_pm_alloc_anno_list(msk, &addr_val)) {
200 		msk->pm.add_addr_signaled++;
201 		mptcp_pm_announce_addr(msk, &addr_val.addr, false);
202 		mptcp_pm_nl_addr_send_ack(msk);
203 	}
204 
205 	spin_unlock_bh(&msk->pm.lock);
206 	release_sock((struct sock *)msk);
207 
208 	err = 0;
209  announce_err:
210 	sock_put((struct sock *)msk);
211 	return err;
212 }
213 
214 int mptcp_nl_cmd_remove(struct sk_buff *skb, struct genl_info *info)
215 {
216 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
217 	struct nlattr *id = info->attrs[MPTCP_PM_ATTR_LOC_ID];
218 	struct mptcp_pm_addr_entry *match = NULL;
219 	struct mptcp_pm_addr_entry *entry;
220 	struct mptcp_sock *msk;
221 	LIST_HEAD(free_list);
222 	int err = -EINVAL;
223 	u32 token_val;
224 	u8 id_val;
225 
226 	if (!id || !token) {
227 		GENL_SET_ERR_MSG(info, "missing required inputs");
228 		return err;
229 	}
230 
231 	id_val = nla_get_u8(id);
232 	token_val = nla_get_u32(token);
233 
234 	msk = mptcp_token_get_sock(sock_net(skb->sk), token_val);
235 	if (!msk) {
236 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
237 		return err;
238 	}
239 
240 	if (!mptcp_pm_is_userspace(msk)) {
241 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
242 		goto remove_err;
243 	}
244 
245 	lock_sock((struct sock *)msk);
246 
247 	list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
248 		if (entry->addr.id == id_val) {
249 			match = entry;
250 			break;
251 		}
252 	}
253 
254 	if (!match) {
255 		GENL_SET_ERR_MSG(info, "address with specified id not found");
256 		release_sock((struct sock *)msk);
257 		goto remove_err;
258 	}
259 
260 	list_move(&match->list, &free_list);
261 
262 	mptcp_pm_remove_addrs(msk, &free_list);
263 
264 	release_sock((struct sock *)msk);
265 
266 	list_for_each_entry_safe(match, entry, &free_list, list) {
267 		sock_kfree_s((struct sock *)msk, match, sizeof(*match));
268 	}
269 
270 	err = 0;
271  remove_err:
272 	sock_put((struct sock *)msk);
273 	return err;
274 }
275 
276 int mptcp_nl_cmd_sf_create(struct sk_buff *skb, struct genl_info *info)
277 {
278 	struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
279 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
280 	struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
281 	struct mptcp_pm_addr_entry local = { 0 };
282 	struct mptcp_addr_info addr_r;
283 	struct mptcp_addr_info addr_l;
284 	struct mptcp_sock *msk;
285 	int err = -EINVAL;
286 	struct sock *sk;
287 	u32 token_val;
288 
289 	if (!laddr || !raddr || !token) {
290 		GENL_SET_ERR_MSG(info, "missing required inputs");
291 		return err;
292 	}
293 
294 	token_val = nla_get_u32(token);
295 
296 	msk = mptcp_token_get_sock(genl_info_net(info), token_val);
297 	if (!msk) {
298 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
299 		return err;
300 	}
301 
302 	if (!mptcp_pm_is_userspace(msk)) {
303 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
304 		goto create_err;
305 	}
306 
307 	err = mptcp_pm_parse_addr(laddr, info, &addr_l);
308 	if (err < 0) {
309 		NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
310 		goto create_err;
311 	}
312 
313 	if (addr_l.id == 0) {
314 		NL_SET_ERR_MSG_ATTR(info->extack, laddr, "missing local addr id");
315 		err = -EINVAL;
316 		goto create_err;
317 	}
318 
319 	err = mptcp_pm_parse_addr(raddr, info, &addr_r);
320 	if (err < 0) {
321 		NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
322 		goto create_err;
323 	}
324 
325 	sk = (struct sock *)msk;
326 
327 	if (!mptcp_pm_addr_families_match(sk, &addr_l, &addr_r)) {
328 		GENL_SET_ERR_MSG(info, "families mismatch");
329 		err = -EINVAL;
330 		goto create_err;
331 	}
332 
333 	local.addr = addr_l;
334 	err = mptcp_userspace_pm_append_new_local_addr(msk, &local);
335 	if (err < 0) {
336 		GENL_SET_ERR_MSG(info, "did not match address and id");
337 		goto create_err;
338 	}
339 
340 	lock_sock(sk);
341 
342 	err = __mptcp_subflow_connect(sk, &addr_l, &addr_r);
343 
344 	release_sock(sk);
345 
346 	spin_lock_bh(&msk->pm.lock);
347 	if (err)
348 		mptcp_userspace_pm_delete_local_addr(msk, &local);
349 	else
350 		msk->pm.subflows++;
351 	spin_unlock_bh(&msk->pm.lock);
352 
353  create_err:
354 	sock_put((struct sock *)msk);
355 	return err;
356 }
357 
358 static struct sock *mptcp_nl_find_ssk(struct mptcp_sock *msk,
359 				      const struct mptcp_addr_info *local,
360 				      const struct mptcp_addr_info *remote)
361 {
362 	struct mptcp_subflow_context *subflow;
363 
364 	if (local->family != remote->family)
365 		return NULL;
366 
367 	mptcp_for_each_subflow(msk, subflow) {
368 		const struct inet_sock *issk;
369 		struct sock *ssk;
370 
371 		ssk = mptcp_subflow_tcp_sock(subflow);
372 
373 		if (local->family != ssk->sk_family)
374 			continue;
375 
376 		issk = inet_sk(ssk);
377 
378 		switch (ssk->sk_family) {
379 		case AF_INET:
380 			if (issk->inet_saddr != local->addr.s_addr ||
381 			    issk->inet_daddr != remote->addr.s_addr)
382 				continue;
383 			break;
384 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
385 		case AF_INET6: {
386 			const struct ipv6_pinfo *pinfo = inet6_sk(ssk);
387 
388 			if (!ipv6_addr_equal(&local->addr6, &pinfo->saddr) ||
389 			    !ipv6_addr_equal(&remote->addr6, &ssk->sk_v6_daddr))
390 				continue;
391 			break;
392 		}
393 #endif
394 		default:
395 			continue;
396 		}
397 
398 		if (issk->inet_sport == local->port &&
399 		    issk->inet_dport == remote->port)
400 			return ssk;
401 	}
402 
403 	return NULL;
404 }
405 
406 int mptcp_nl_cmd_sf_destroy(struct sk_buff *skb, struct genl_info *info)
407 {
408 	struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
409 	struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
410 	struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
411 	struct mptcp_addr_info addr_l;
412 	struct mptcp_addr_info addr_r;
413 	struct mptcp_sock *msk;
414 	struct sock *sk, *ssk;
415 	int err = -EINVAL;
416 	u32 token_val;
417 
418 	if (!laddr || !raddr || !token) {
419 		GENL_SET_ERR_MSG(info, "missing required inputs");
420 		return err;
421 	}
422 
423 	token_val = nla_get_u32(token);
424 
425 	msk = mptcp_token_get_sock(genl_info_net(info), token_val);
426 	if (!msk) {
427 		NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
428 		return err;
429 	}
430 
431 	if (!mptcp_pm_is_userspace(msk)) {
432 		GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
433 		goto destroy_err;
434 	}
435 
436 	err = mptcp_pm_parse_addr(laddr, info, &addr_l);
437 	if (err < 0) {
438 		NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
439 		goto destroy_err;
440 	}
441 
442 	err = mptcp_pm_parse_addr(raddr, info, &addr_r);
443 	if (err < 0) {
444 		NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
445 		goto destroy_err;
446 	}
447 
448 	if (addr_l.family != addr_r.family) {
449 		GENL_SET_ERR_MSG(info, "address families do not match");
450 		err = -EINVAL;
451 		goto destroy_err;
452 	}
453 
454 	if (!addr_l.port || !addr_r.port) {
455 		GENL_SET_ERR_MSG(info, "missing local or remote port");
456 		err = -EINVAL;
457 		goto destroy_err;
458 	}
459 
460 	sk = (struct sock *)msk;
461 	lock_sock(sk);
462 	ssk = mptcp_nl_find_ssk(msk, &addr_l, &addr_r);
463 	if (ssk) {
464 		struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
465 		struct mptcp_pm_addr_entry entry = { .addr = addr_l };
466 
467 		spin_lock_bh(&msk->pm.lock);
468 		mptcp_userspace_pm_delete_local_addr(msk, &entry);
469 		spin_unlock_bh(&msk->pm.lock);
470 		mptcp_subflow_shutdown(sk, ssk, RCV_SHUTDOWN | SEND_SHUTDOWN);
471 		mptcp_close_ssk(sk, ssk, subflow);
472 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
473 		err = 0;
474 	} else {
475 		err = -ESRCH;
476 	}
477 	release_sock(sk);
478 
479 destroy_err:
480 	sock_put((struct sock *)msk);
481 	return err;
482 }
483 
484 int mptcp_userspace_pm_set_flags(struct net *net, struct nlattr *token,
485 				 struct mptcp_pm_addr_entry *loc,
486 				 struct mptcp_pm_addr_entry *rem, u8 bkup)
487 {
488 	struct mptcp_sock *msk;
489 	int ret = -EINVAL;
490 	u32 token_val;
491 
492 	token_val = nla_get_u32(token);
493 
494 	msk = mptcp_token_get_sock(net, token_val);
495 	if (!msk)
496 		return ret;
497 
498 	if (!mptcp_pm_is_userspace(msk))
499 		goto set_flags_err;
500 
501 	if (loc->addr.family == AF_UNSPEC ||
502 	    rem->addr.family == AF_UNSPEC)
503 		goto set_flags_err;
504 
505 	lock_sock((struct sock *)msk);
506 	ret = mptcp_pm_nl_mp_prio_send_ack(msk, &loc->addr, &rem->addr, bkup);
507 	release_sock((struct sock *)msk);
508 
509 set_flags_err:
510 	sock_put((struct sock *)msk);
511 	return ret;
512 }
513