xref: /openbmc/linux/net/mptcp/pm.c (revision e5242c5f)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Multipath TCP
3  *
4  * Copyright (c) 2019, Intel Corporation.
5  */
6 #define pr_fmt(fmt) "MPTCP: " fmt
7 
8 #include <linux/kernel.h>
9 #include <net/tcp.h>
10 #include <net/mptcp.h>
11 #include "protocol.h"
12 
13 #include "mib.h"
14 
15 /* path manager command handlers */
16 
17 int mptcp_pm_announce_addr(struct mptcp_sock *msk,
18 			   const struct mptcp_addr_info *addr,
19 			   bool echo)
20 {
21 	u8 add_addr = READ_ONCE(msk->pm.addr_signal);
22 
23 	pr_debug("msk=%p, local_id=%d, echo=%d\n", msk, addr->id, echo);
24 
25 	lockdep_assert_held(&msk->pm.lock);
26 
27 	if (add_addr &
28 	    (echo ? BIT(MPTCP_ADD_ADDR_ECHO) : BIT(MPTCP_ADD_ADDR_SIGNAL))) {
29 		MPTCP_INC_STATS(sock_net((struct sock *)msk),
30 				echo ? MPTCP_MIB_ECHOADDTXDROP : MPTCP_MIB_ADDADDRTXDROP);
31 		return -EINVAL;
32 	}
33 
34 	if (echo) {
35 		msk->pm.remote = *addr;
36 		add_addr |= BIT(MPTCP_ADD_ADDR_ECHO);
37 	} else {
38 		msk->pm.local = *addr;
39 		add_addr |= BIT(MPTCP_ADD_ADDR_SIGNAL);
40 	}
41 	WRITE_ONCE(msk->pm.addr_signal, add_addr);
42 	return 0;
43 }
44 
45 int mptcp_pm_remove_addr(struct mptcp_sock *msk, const struct mptcp_rm_list *rm_list)
46 {
47 	u8 rm_addr = READ_ONCE(msk->pm.addr_signal);
48 
49 	pr_debug("msk=%p, rm_list_nr=%d\n", msk, rm_list->nr);
50 
51 	if (rm_addr) {
52 		MPTCP_ADD_STATS(sock_net((struct sock *)msk),
53 				MPTCP_MIB_RMADDRTXDROP, rm_list->nr);
54 		return -EINVAL;
55 	}
56 
57 	msk->pm.rm_list_tx = *rm_list;
58 	rm_addr |= BIT(MPTCP_RM_ADDR_SIGNAL);
59 	WRITE_ONCE(msk->pm.addr_signal, rm_addr);
60 	mptcp_pm_nl_addr_send_ack(msk);
61 	return 0;
62 }
63 
64 /* path manager event handlers */
65 
66 void mptcp_pm_new_connection(struct mptcp_sock *msk, const struct sock *ssk, int server_side)
67 {
68 	struct mptcp_pm_data *pm = &msk->pm;
69 
70 	pr_debug("msk=%p, token=%u side=%d\n", msk, msk->token, server_side);
71 
72 	WRITE_ONCE(pm->server_side, server_side);
73 	mptcp_event(MPTCP_EVENT_CREATED, msk, ssk, GFP_ATOMIC);
74 }
75 
76 bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk)
77 {
78 	struct mptcp_pm_data *pm = &msk->pm;
79 	unsigned int subflows_max;
80 	int ret = 0;
81 
82 	if (mptcp_pm_is_userspace(msk)) {
83 		if (mptcp_userspace_pm_active(msk)) {
84 			spin_lock_bh(&pm->lock);
85 			pm->subflows++;
86 			spin_unlock_bh(&pm->lock);
87 			return true;
88 		}
89 		return false;
90 	}
91 
92 	subflows_max = mptcp_pm_get_subflows_max(msk);
93 
94 	pr_debug("msk=%p subflows=%d max=%d allow=%d\n", msk, pm->subflows,
95 		 subflows_max, READ_ONCE(pm->accept_subflow));
96 
97 	/* try to avoid acquiring the lock below */
98 	if (!READ_ONCE(pm->accept_subflow))
99 		return false;
100 
101 	spin_lock_bh(&pm->lock);
102 	if (READ_ONCE(pm->accept_subflow)) {
103 		ret = pm->subflows < subflows_max;
104 		if (ret && ++pm->subflows == subflows_max)
105 			WRITE_ONCE(pm->accept_subflow, false);
106 	}
107 	spin_unlock_bh(&pm->lock);
108 
109 	return ret;
110 }
111 
112 /* return true if the new status bit is currently cleared, that is, this event
113  * can be server, eventually by an already scheduled work
114  */
115 static bool mptcp_pm_schedule_work(struct mptcp_sock *msk,
116 				   enum mptcp_pm_status new_status)
117 {
118 	pr_debug("msk=%p status=%x new=%lx\n", msk, msk->pm.status,
119 		 BIT(new_status));
120 	if (msk->pm.status & BIT(new_status))
121 		return false;
122 
123 	msk->pm.status |= BIT(new_status);
124 	mptcp_schedule_work((struct sock *)msk);
125 	return true;
126 }
127 
128 void mptcp_pm_fully_established(struct mptcp_sock *msk, const struct sock *ssk)
129 {
130 	struct mptcp_pm_data *pm = &msk->pm;
131 	bool announce = false;
132 
133 	pr_debug("msk=%p\n", msk);
134 
135 	spin_lock_bh(&pm->lock);
136 
137 	/* mptcp_pm_fully_established() can be invoked by multiple
138 	 * racing paths - accept() and check_fully_established()
139 	 * be sure to serve this event only once.
140 	 */
141 	if (READ_ONCE(pm->work_pending) &&
142 	    !(msk->pm.status & BIT(MPTCP_PM_ALREADY_ESTABLISHED)))
143 		mptcp_pm_schedule_work(msk, MPTCP_PM_ESTABLISHED);
144 
145 	if ((msk->pm.status & BIT(MPTCP_PM_ALREADY_ESTABLISHED)) == 0)
146 		announce = true;
147 
148 	msk->pm.status |= BIT(MPTCP_PM_ALREADY_ESTABLISHED);
149 	spin_unlock_bh(&pm->lock);
150 
151 	if (announce)
152 		mptcp_event(MPTCP_EVENT_ESTABLISHED, msk, ssk, GFP_ATOMIC);
153 }
154 
155 void mptcp_pm_connection_closed(struct mptcp_sock *msk)
156 {
157 	pr_debug("msk=%p\n", msk);
158 }
159 
160 void mptcp_pm_subflow_established(struct mptcp_sock *msk)
161 {
162 	struct mptcp_pm_data *pm = &msk->pm;
163 
164 	pr_debug("msk=%p\n", msk);
165 
166 	if (!READ_ONCE(pm->work_pending))
167 		return;
168 
169 	spin_lock_bh(&pm->lock);
170 
171 	if (READ_ONCE(pm->work_pending))
172 		mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
173 
174 	spin_unlock_bh(&pm->lock);
175 }
176 
177 void mptcp_pm_subflow_check_next(struct mptcp_sock *msk, const struct sock *ssk,
178 				 const struct mptcp_subflow_context *subflow)
179 {
180 	struct mptcp_pm_data *pm = &msk->pm;
181 	bool update_subflows;
182 
183 	update_subflows = subflow->request_join || subflow->mp_join;
184 	if (mptcp_pm_is_userspace(msk)) {
185 		if (update_subflows) {
186 			spin_lock_bh(&pm->lock);
187 			pm->subflows--;
188 			spin_unlock_bh(&pm->lock);
189 		}
190 		return;
191 	}
192 
193 	if (!READ_ONCE(pm->work_pending) && !update_subflows)
194 		return;
195 
196 	spin_lock_bh(&pm->lock);
197 	if (update_subflows)
198 		__mptcp_pm_close_subflow(msk);
199 
200 	/* Even if this subflow is not really established, tell the PM to try
201 	 * to pick the next ones, if possible.
202 	 */
203 	if (mptcp_pm_nl_check_work_pending(msk))
204 		mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
205 
206 	spin_unlock_bh(&pm->lock);
207 }
208 
209 void mptcp_pm_add_addr_received(const struct sock *ssk,
210 				const struct mptcp_addr_info *addr)
211 {
212 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
213 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
214 	struct mptcp_pm_data *pm = &msk->pm;
215 
216 	pr_debug("msk=%p remote_id=%d accept=%d\n", msk, addr->id,
217 		 READ_ONCE(pm->accept_addr));
218 
219 	mptcp_event_addr_announced(ssk, addr);
220 
221 	spin_lock_bh(&pm->lock);
222 
223 	if (mptcp_pm_is_userspace(msk)) {
224 		if (mptcp_userspace_pm_active(msk)) {
225 			mptcp_pm_announce_addr(msk, addr, true);
226 			mptcp_pm_add_addr_send_ack(msk);
227 		} else {
228 			__MPTCP_INC_STATS(sock_net((struct sock *)msk), MPTCP_MIB_ADDADDRDROP);
229 		}
230 	/* id0 should not have a different address */
231 	} else if ((addr->id == 0 && !mptcp_pm_nl_is_init_remote_addr(msk, addr)) ||
232 		   (addr->id > 0 && !READ_ONCE(pm->accept_addr))) {
233 		mptcp_pm_announce_addr(msk, addr, true);
234 		mptcp_pm_add_addr_send_ack(msk);
235 	} else if (mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_RECEIVED)) {
236 		pm->remote = *addr;
237 	} else {
238 		__MPTCP_INC_STATS(sock_net((struct sock *)msk), MPTCP_MIB_ADDADDRDROP);
239 	}
240 
241 	spin_unlock_bh(&pm->lock);
242 }
243 
244 void mptcp_pm_add_addr_echoed(struct mptcp_sock *msk,
245 			      const struct mptcp_addr_info *addr)
246 {
247 	struct mptcp_pm_data *pm = &msk->pm;
248 
249 	pr_debug("msk=%p\n", msk);
250 
251 	spin_lock_bh(&pm->lock);
252 
253 	if (mptcp_lookup_anno_list_by_saddr(msk, addr) && READ_ONCE(pm->work_pending))
254 		mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
255 
256 	spin_unlock_bh(&pm->lock);
257 }
258 
259 void mptcp_pm_add_addr_send_ack(struct mptcp_sock *msk)
260 {
261 	if (!mptcp_pm_should_add_signal(msk))
262 		return;
263 
264 	mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_SEND_ACK);
265 }
266 
267 void mptcp_pm_rm_addr_received(struct mptcp_sock *msk,
268 			       const struct mptcp_rm_list *rm_list)
269 {
270 	struct mptcp_pm_data *pm = &msk->pm;
271 	u8 i;
272 
273 	pr_debug("msk=%p remote_ids_nr=%d\n", msk, rm_list->nr);
274 
275 	for (i = 0; i < rm_list->nr; i++)
276 		mptcp_event_addr_removed(msk, rm_list->ids[i]);
277 
278 	spin_lock_bh(&pm->lock);
279 	if (mptcp_pm_schedule_work(msk, MPTCP_PM_RM_ADDR_RECEIVED))
280 		pm->rm_list_rx = *rm_list;
281 	else
282 		__MPTCP_INC_STATS(sock_net((struct sock *)msk), MPTCP_MIB_RMADDRDROP);
283 	spin_unlock_bh(&pm->lock);
284 }
285 
286 void mptcp_pm_mp_prio_received(struct sock *ssk, u8 bkup)
287 {
288 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
289 	struct sock *sk = subflow->conn;
290 	struct mptcp_sock *msk;
291 
292 	pr_debug("subflow->backup=%d, bkup=%d\n", subflow->backup, bkup);
293 	msk = mptcp_sk(sk);
294 	if (subflow->backup != bkup)
295 		subflow->backup = bkup;
296 
297 	mptcp_event(MPTCP_EVENT_SUB_PRIORITY, msk, ssk, GFP_ATOMIC);
298 }
299 
300 void mptcp_pm_mp_fail_received(struct sock *sk, u64 fail_seq)
301 {
302 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
303 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
304 
305 	pr_debug("fail_seq=%llu\n", fail_seq);
306 
307 	if (!READ_ONCE(msk->allow_infinite_fallback))
308 		return;
309 
310 	if (!subflow->fail_tout) {
311 		pr_debug("send MP_FAIL response and infinite map\n");
312 
313 		subflow->send_mp_fail = 1;
314 		subflow->send_infinite_map = 1;
315 		tcp_send_ack(sk);
316 	} else {
317 		pr_debug("MP_FAIL response received\n");
318 		WRITE_ONCE(subflow->fail_tout, 0);
319 	}
320 }
321 
322 /* path manager helpers */
323 
324 bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, const struct sk_buff *skb,
325 			      unsigned int opt_size, unsigned int remaining,
326 			      struct mptcp_addr_info *addr, bool *echo,
327 			      bool *drop_other_suboptions)
328 {
329 	int ret = false;
330 	u8 add_addr;
331 	u8 family;
332 	bool port;
333 
334 	spin_lock_bh(&msk->pm.lock);
335 
336 	/* double check after the lock is acquired */
337 	if (!mptcp_pm_should_add_signal(msk))
338 		goto out_unlock;
339 
340 	/* always drop every other options for pure ack ADD_ADDR; this is a
341 	 * plain dup-ack from TCP perspective. The other MPTCP-relevant info,
342 	 * if any, will be carried by the 'original' TCP ack
343 	 */
344 	if (skb && skb_is_tcp_pure_ack(skb)) {
345 		remaining += opt_size;
346 		*drop_other_suboptions = true;
347 	}
348 
349 	*echo = mptcp_pm_should_add_signal_echo(msk);
350 	port = !!(*echo ? msk->pm.remote.port : msk->pm.local.port);
351 
352 	family = *echo ? msk->pm.remote.family : msk->pm.local.family;
353 	if (remaining < mptcp_add_addr_len(family, *echo, port))
354 		goto out_unlock;
355 
356 	if (*echo) {
357 		*addr = msk->pm.remote;
358 		add_addr = msk->pm.addr_signal & ~BIT(MPTCP_ADD_ADDR_ECHO);
359 	} else {
360 		*addr = msk->pm.local;
361 		add_addr = msk->pm.addr_signal & ~BIT(MPTCP_ADD_ADDR_SIGNAL);
362 	}
363 	WRITE_ONCE(msk->pm.addr_signal, add_addr);
364 	ret = true;
365 
366 out_unlock:
367 	spin_unlock_bh(&msk->pm.lock);
368 	return ret;
369 }
370 
371 bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
372 			     struct mptcp_rm_list *rm_list)
373 {
374 	int ret = false, len;
375 	u8 rm_addr;
376 
377 	spin_lock_bh(&msk->pm.lock);
378 
379 	/* double check after the lock is acquired */
380 	if (!mptcp_pm_should_rm_signal(msk))
381 		goto out_unlock;
382 
383 	rm_addr = msk->pm.addr_signal & ~BIT(MPTCP_RM_ADDR_SIGNAL);
384 	len = mptcp_rm_addr_len(&msk->pm.rm_list_tx);
385 	if (len < 0) {
386 		WRITE_ONCE(msk->pm.addr_signal, rm_addr);
387 		goto out_unlock;
388 	}
389 	if (remaining < len)
390 		goto out_unlock;
391 
392 	*rm_list = msk->pm.rm_list_tx;
393 	WRITE_ONCE(msk->pm.addr_signal, rm_addr);
394 	ret = true;
395 
396 out_unlock:
397 	spin_unlock_bh(&msk->pm.lock);
398 	return ret;
399 }
400 
401 int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
402 {
403 	struct mptcp_addr_info skc_local;
404 	struct mptcp_addr_info msk_local;
405 
406 	if (WARN_ON_ONCE(!msk))
407 		return -1;
408 
409 	/* The 0 ID mapping is defined by the first subflow, copied into the msk
410 	 * addr
411 	 */
412 	mptcp_local_address((struct sock_common *)msk, &msk_local);
413 	mptcp_local_address((struct sock_common *)skc, &skc_local);
414 	if (mptcp_addresses_equal(&msk_local, &skc_local, false))
415 		return 0;
416 
417 	if (mptcp_pm_is_userspace(msk))
418 		return mptcp_userspace_pm_get_local_id(msk, &skc_local);
419 	return mptcp_pm_nl_get_local_id(msk, &skc_local);
420 }
421 
422 bool mptcp_pm_is_backup(struct mptcp_sock *msk, struct sock_common *skc)
423 {
424 	struct mptcp_addr_info skc_local;
425 
426 	mptcp_local_address((struct sock_common *)skc, &skc_local);
427 
428 	if (mptcp_pm_is_userspace(msk))
429 		return mptcp_userspace_pm_is_backup(msk, &skc_local);
430 
431 	return mptcp_pm_nl_is_backup(msk, &skc_local);
432 }
433 
434 int mptcp_pm_get_flags_and_ifindex_by_id(struct mptcp_sock *msk, unsigned int id,
435 					 u8 *flags, int *ifindex)
436 {
437 	*flags = 0;
438 	*ifindex = 0;
439 
440 	if (mptcp_pm_is_userspace(msk))
441 		return mptcp_userspace_pm_get_flags_and_ifindex_by_id(msk, id, flags, ifindex);
442 	return mptcp_pm_nl_get_flags_and_ifindex_by_id(msk, id, flags, ifindex);
443 }
444 
445 int mptcp_pm_set_flags(struct net *net, struct nlattr *token,
446 		       struct mptcp_pm_addr_entry *loc,
447 		       struct mptcp_pm_addr_entry *rem, u8 bkup)
448 {
449 	if (token)
450 		return mptcp_userspace_pm_set_flags(net, token, loc, rem, bkup);
451 	return mptcp_pm_nl_set_flags(net, loc, bkup);
452 }
453 
454 void mptcp_pm_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk)
455 {
456 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
457 	u32 rcv_tstamp = READ_ONCE(tcp_sk(ssk)->rcv_tstamp);
458 
459 	/* keep track of rtx periods with no progress */
460 	if (!subflow->stale_count) {
461 		subflow->stale_rcv_tstamp = rcv_tstamp;
462 		subflow->stale_count++;
463 	} else if (subflow->stale_rcv_tstamp == rcv_tstamp) {
464 		if (subflow->stale_count < U8_MAX)
465 			subflow->stale_count++;
466 		mptcp_pm_nl_subflow_chk_stale(msk, ssk);
467 	} else {
468 		subflow->stale_count = 0;
469 		mptcp_subflow_set_active(subflow);
470 	}
471 }
472 
473 /* if sk is ipv4 or ipv6_only allows only same-family local and remote addresses,
474  * otherwise allow any matching local/remote pair
475  */
476 bool mptcp_pm_addr_families_match(const struct sock *sk,
477 				  const struct mptcp_addr_info *loc,
478 				  const struct mptcp_addr_info *rem)
479 {
480 	bool mptcp_is_v4 = sk->sk_family == AF_INET;
481 
482 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
483 	bool loc_is_v4 = loc->family == AF_INET || ipv6_addr_v4mapped(&loc->addr6);
484 	bool rem_is_v4 = rem->family == AF_INET || ipv6_addr_v4mapped(&rem->addr6);
485 
486 	if (mptcp_is_v4)
487 		return loc_is_v4 && rem_is_v4;
488 
489 	if (ipv6_only_sock(sk))
490 		return !loc_is_v4 && !rem_is_v4;
491 
492 	return loc_is_v4 == rem_is_v4;
493 #else
494 	return mptcp_is_v4 && loc->family == AF_INET && rem->family == AF_INET;
495 #endif
496 }
497 
498 void mptcp_pm_data_reset(struct mptcp_sock *msk)
499 {
500 	u8 pm_type = mptcp_get_pm_type(sock_net((struct sock *)msk));
501 	struct mptcp_pm_data *pm = &msk->pm;
502 
503 	pm->add_addr_signaled = 0;
504 	pm->add_addr_accepted = 0;
505 	pm->local_addr_used = 0;
506 	pm->subflows = 0;
507 	pm->rm_list_tx.nr = 0;
508 	pm->rm_list_rx.nr = 0;
509 	WRITE_ONCE(pm->pm_type, pm_type);
510 
511 	if (pm_type == MPTCP_PM_TYPE_KERNEL) {
512 		bool subflows_allowed = !!mptcp_pm_get_subflows_max(msk);
513 
514 		/* pm->work_pending must be only be set to 'true' when
515 		 * pm->pm_type is set to MPTCP_PM_TYPE_KERNEL
516 		 */
517 		WRITE_ONCE(pm->work_pending,
518 			   (!!mptcp_pm_get_local_addr_max(msk) &&
519 			    subflows_allowed) ||
520 			   !!mptcp_pm_get_add_addr_signal_max(msk));
521 		WRITE_ONCE(pm->accept_addr,
522 			   !!mptcp_pm_get_add_addr_accept_max(msk) &&
523 			   subflows_allowed);
524 		WRITE_ONCE(pm->accept_subflow, subflows_allowed);
525 	} else {
526 		WRITE_ONCE(pm->work_pending, 0);
527 		WRITE_ONCE(pm->accept_addr, 0);
528 		WRITE_ONCE(pm->accept_subflow, 0);
529 	}
530 
531 	WRITE_ONCE(pm->addr_signal, 0);
532 	WRITE_ONCE(pm->remote_deny_join_id0, false);
533 	pm->status = 0;
534 	bitmap_fill(msk->pm.id_avail_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
535 }
536 
537 void mptcp_pm_data_init(struct mptcp_sock *msk)
538 {
539 	spin_lock_init(&msk->pm.lock);
540 	INIT_LIST_HEAD(&msk->pm.anno_list);
541 	INIT_LIST_HEAD(&msk->pm.userspace_pm_local_addr_list);
542 	mptcp_pm_data_reset(msk);
543 }
544 
545 void __init mptcp_pm_init(void)
546 {
547 	mptcp_pm_nl_init();
548 }
549