xref: /openbmc/linux/net/mptcp/pm.c (revision c9933d494c54f72290831191c09bb8488bfd5905)
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", 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 		pr_warn("addr_signal error, add_addr=%d, echo=%d", add_addr, echo);
30 		return -EINVAL;
31 	}
32 
33 	if (echo) {
34 		msk->pm.remote = *addr;
35 		add_addr |= BIT(MPTCP_ADD_ADDR_ECHO);
36 	} else {
37 		msk->pm.local = *addr;
38 		add_addr |= BIT(MPTCP_ADD_ADDR_SIGNAL);
39 	}
40 	WRITE_ONCE(msk->pm.addr_signal, add_addr);
41 	return 0;
42 }
43 
44 int mptcp_pm_remove_addr(struct mptcp_sock *msk, const struct mptcp_rm_list *rm_list)
45 {
46 	u8 rm_addr = READ_ONCE(msk->pm.addr_signal);
47 
48 	pr_debug("msk=%p, rm_list_nr=%d", msk, rm_list->nr);
49 
50 	if (rm_addr) {
51 		pr_warn("addr_signal error, rm_addr=%d", rm_addr);
52 		return -EINVAL;
53 	}
54 
55 	msk->pm.rm_list_tx = *rm_list;
56 	rm_addr |= BIT(MPTCP_RM_ADDR_SIGNAL);
57 	WRITE_ONCE(msk->pm.addr_signal, rm_addr);
58 	mptcp_pm_nl_addr_send_ack(msk);
59 	return 0;
60 }
61 
62 int mptcp_pm_remove_subflow(struct mptcp_sock *msk, const struct mptcp_rm_list *rm_list)
63 {
64 	pr_debug("msk=%p, rm_list_nr=%d", msk, rm_list->nr);
65 
66 	spin_lock_bh(&msk->pm.lock);
67 	mptcp_pm_nl_rm_subflow_received(msk, rm_list);
68 	spin_unlock_bh(&msk->pm.lock);
69 	return 0;
70 }
71 
72 /* path manager event handlers */
73 
74 void mptcp_pm_new_connection(struct mptcp_sock *msk, const struct sock *ssk, int server_side)
75 {
76 	struct mptcp_pm_data *pm = &msk->pm;
77 
78 	pr_debug("msk=%p, token=%u side=%d", msk, msk->token, server_side);
79 
80 	WRITE_ONCE(pm->server_side, server_side);
81 	mptcp_event(MPTCP_EVENT_CREATED, msk, ssk, GFP_ATOMIC);
82 }
83 
84 bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk)
85 {
86 	struct mptcp_pm_data *pm = &msk->pm;
87 	unsigned int subflows_max;
88 	int ret = 0;
89 
90 	if (mptcp_pm_is_userspace(msk))
91 		return mptcp_userspace_pm_active(msk);
92 
93 	subflows_max = mptcp_pm_get_subflows_max(msk);
94 
95 	pr_debug("msk=%p subflows=%d max=%d allow=%d", msk, pm->subflows,
96 		 subflows_max, READ_ONCE(pm->accept_subflow));
97 
98 	/* try to avoid acquiring the lock below */
99 	if (!READ_ONCE(pm->accept_subflow))
100 		return false;
101 
102 	spin_lock_bh(&pm->lock);
103 	if (READ_ONCE(pm->accept_subflow)) {
104 		ret = pm->subflows < subflows_max;
105 		if (ret && ++pm->subflows == subflows_max)
106 			WRITE_ONCE(pm->accept_subflow, false);
107 	}
108 	spin_unlock_bh(&pm->lock);
109 
110 	return ret;
111 }
112 
113 /* return true if the new status bit is currently cleared, that is, this event
114  * can be server, eventually by an already scheduled work
115  */
116 static bool mptcp_pm_schedule_work(struct mptcp_sock *msk,
117 				   enum mptcp_pm_status new_status)
118 {
119 	pr_debug("msk=%p status=%x new=%lx", msk, msk->pm.status,
120 		 BIT(new_status));
121 	if (msk->pm.status & BIT(new_status))
122 		return false;
123 
124 	msk->pm.status |= BIT(new_status);
125 	mptcp_schedule_work((struct sock *)msk);
126 	return true;
127 }
128 
129 void mptcp_pm_fully_established(struct mptcp_sock *msk, const struct sock *ssk, gfp_t gfp)
130 {
131 	struct mptcp_pm_data *pm = &msk->pm;
132 	bool announce = false;
133 
134 	pr_debug("msk=%p", msk);
135 
136 	spin_lock_bh(&pm->lock);
137 
138 	/* mptcp_pm_fully_established() can be invoked by multiple
139 	 * racing paths - accept() and check_fully_established()
140 	 * be sure to serve this event only once.
141 	 */
142 	if (READ_ONCE(pm->work_pending) &&
143 	    !(msk->pm.status & BIT(MPTCP_PM_ALREADY_ESTABLISHED)))
144 		mptcp_pm_schedule_work(msk, MPTCP_PM_ESTABLISHED);
145 
146 	if ((msk->pm.status & BIT(MPTCP_PM_ALREADY_ESTABLISHED)) == 0)
147 		announce = true;
148 
149 	msk->pm.status |= BIT(MPTCP_PM_ALREADY_ESTABLISHED);
150 	spin_unlock_bh(&pm->lock);
151 
152 	if (announce)
153 		mptcp_event(MPTCP_EVENT_ESTABLISHED, msk, ssk, gfp);
154 }
155 
156 void mptcp_pm_connection_closed(struct mptcp_sock *msk)
157 {
158 	pr_debug("msk=%p", msk);
159 }
160 
161 void mptcp_pm_subflow_established(struct mptcp_sock *msk)
162 {
163 	struct mptcp_pm_data *pm = &msk->pm;
164 
165 	pr_debug("msk=%p", msk);
166 
167 	if (!READ_ONCE(pm->work_pending))
168 		return;
169 
170 	spin_lock_bh(&pm->lock);
171 
172 	if (READ_ONCE(pm->work_pending))
173 		mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
174 
175 	spin_unlock_bh(&pm->lock);
176 }
177 
178 void mptcp_pm_subflow_check_next(struct mptcp_sock *msk, const struct sock *ssk,
179 				 const struct mptcp_subflow_context *subflow)
180 {
181 	struct mptcp_pm_data *pm = &msk->pm;
182 	bool update_subflows;
183 
184 	update_subflows = (ssk->sk_state == TCP_CLOSE) &&
185 			  (subflow->request_join || subflow->mp_join) &&
186 			  mptcp_pm_is_kernel(msk);
187 	if (!READ_ONCE(pm->work_pending) && !update_subflows)
188 		return;
189 
190 	spin_lock_bh(&pm->lock);
191 	if (update_subflows)
192 		pm->subflows--;
193 
194 	/* Even if this subflow is not really established, tell the PM to try
195 	 * to pick the next ones, if possible.
196 	 */
197 	if (mptcp_pm_nl_check_work_pending(msk))
198 		mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
199 
200 	spin_unlock_bh(&pm->lock);
201 }
202 
203 void mptcp_pm_add_addr_received(const struct sock *ssk,
204 				const struct mptcp_addr_info *addr)
205 {
206 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
207 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
208 	struct mptcp_pm_data *pm = &msk->pm;
209 
210 	pr_debug("msk=%p remote_id=%d accept=%d", msk, addr->id,
211 		 READ_ONCE(pm->accept_addr));
212 
213 	mptcp_event_addr_announced(ssk, addr);
214 
215 	spin_lock_bh(&pm->lock);
216 
217 	if (mptcp_pm_is_userspace(msk)) {
218 		if (mptcp_userspace_pm_active(msk)) {
219 			mptcp_pm_announce_addr(msk, addr, true);
220 			mptcp_pm_add_addr_send_ack(msk);
221 		} else {
222 			__MPTCP_INC_STATS(sock_net((struct sock *)msk), MPTCP_MIB_ADDADDRDROP);
223 		}
224 	} else if (!READ_ONCE(pm->accept_addr)) {
225 		mptcp_pm_announce_addr(msk, addr, true);
226 		mptcp_pm_add_addr_send_ack(msk);
227 	} else if (mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_RECEIVED)) {
228 		pm->remote = *addr;
229 	} else {
230 		__MPTCP_INC_STATS(sock_net((struct sock *)msk), MPTCP_MIB_ADDADDRDROP);
231 	}
232 
233 	spin_unlock_bh(&pm->lock);
234 }
235 
236 void mptcp_pm_add_addr_echoed(struct mptcp_sock *msk,
237 			      const struct mptcp_addr_info *addr)
238 {
239 	struct mptcp_pm_data *pm = &msk->pm;
240 
241 	pr_debug("msk=%p", msk);
242 
243 	spin_lock_bh(&pm->lock);
244 
245 	if (mptcp_lookup_anno_list_by_saddr(msk, addr) && READ_ONCE(pm->work_pending))
246 		mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
247 
248 	spin_unlock_bh(&pm->lock);
249 }
250 
251 void mptcp_pm_add_addr_send_ack(struct mptcp_sock *msk)
252 {
253 	if (!mptcp_pm_should_add_signal(msk))
254 		return;
255 
256 	mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_SEND_ACK);
257 }
258 
259 void mptcp_pm_rm_addr_received(struct mptcp_sock *msk,
260 			       const struct mptcp_rm_list *rm_list)
261 {
262 	struct mptcp_pm_data *pm = &msk->pm;
263 	u8 i;
264 
265 	pr_debug("msk=%p remote_ids_nr=%d", msk, rm_list->nr);
266 
267 	for (i = 0; i < rm_list->nr; i++)
268 		mptcp_event_addr_removed(msk, rm_list->ids[i]);
269 
270 	spin_lock_bh(&pm->lock);
271 	if (mptcp_pm_schedule_work(msk, MPTCP_PM_RM_ADDR_RECEIVED))
272 		pm->rm_list_rx = *rm_list;
273 	else
274 		__MPTCP_INC_STATS(sock_net((struct sock *)msk), MPTCP_MIB_RMADDRDROP);
275 	spin_unlock_bh(&pm->lock);
276 }
277 
278 void mptcp_pm_mp_prio_received(struct sock *ssk, u8 bkup)
279 {
280 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
281 	struct sock *sk = subflow->conn;
282 	struct mptcp_sock *msk;
283 
284 	pr_debug("subflow->backup=%d, bkup=%d\n", subflow->backup, bkup);
285 	msk = mptcp_sk(sk);
286 	if (subflow->backup != bkup) {
287 		subflow->backup = bkup;
288 		mptcp_data_lock(sk);
289 		if (!sock_owned_by_user(sk))
290 			msk->last_snd = NULL;
291 		else
292 			__set_bit(MPTCP_RESET_SCHEDULER,  &msk->cb_flags);
293 		mptcp_data_unlock(sk);
294 	}
295 
296 	mptcp_event(MPTCP_EVENT_SUB_PRIORITY, msk, ssk, GFP_ATOMIC);
297 }
298 
299 void mptcp_pm_mp_fail_received(struct sock *sk, u64 fail_seq)
300 {
301 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
302 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
303 	struct sock *s = (struct sock *)msk;
304 
305 	pr_debug("fail_seq=%llu", fail_seq);
306 
307 	if (mptcp_has_another_subflow(sk) || !READ_ONCE(msk->allow_infinite_fallback))
308 		return;
309 
310 	if (!READ_ONCE(subflow->mp_fail_response_expect)) {
311 		pr_debug("send MP_FAIL response and infinite map");
312 
313 		subflow->send_mp_fail = 1;
314 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILTX);
315 		subflow->send_infinite_map = 1;
316 	} else if (s && inet_sk_state_load(s) != TCP_CLOSE) {
317 		pr_debug("MP_FAIL response received");
318 
319 		mptcp_data_lock(s);
320 		if (inet_sk_state_load(s) != TCP_CLOSE)
321 			sk_stop_timer(s, &s->sk_timer);
322 		mptcp_data_unlock(s);
323 	}
324 }
325 
326 /* path manager helpers */
327 
328 bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, const struct sk_buff *skb,
329 			      unsigned int opt_size, unsigned int remaining,
330 			      struct mptcp_addr_info *addr, bool *echo,
331 			      bool *drop_other_suboptions)
332 {
333 	int ret = false;
334 	u8 add_addr;
335 	u8 family;
336 	bool port;
337 
338 	spin_lock_bh(&msk->pm.lock);
339 
340 	/* double check after the lock is acquired */
341 	if (!mptcp_pm_should_add_signal(msk))
342 		goto out_unlock;
343 
344 	/* always drop every other options for pure ack ADD_ADDR; this is a
345 	 * plain dup-ack from TCP perspective. The other MPTCP-relevant info,
346 	 * if any, will be carried by the 'original' TCP ack
347 	 */
348 	if (skb && skb_is_tcp_pure_ack(skb)) {
349 		remaining += opt_size;
350 		*drop_other_suboptions = true;
351 	}
352 
353 	*echo = mptcp_pm_should_add_signal_echo(msk);
354 	port = !!(*echo ? msk->pm.remote.port : msk->pm.local.port);
355 
356 	family = *echo ? msk->pm.remote.family : msk->pm.local.family;
357 	if (remaining < mptcp_add_addr_len(family, *echo, port))
358 		goto out_unlock;
359 
360 	if (*echo) {
361 		*addr = msk->pm.remote;
362 		add_addr = msk->pm.addr_signal & ~BIT(MPTCP_ADD_ADDR_ECHO);
363 	} else {
364 		*addr = msk->pm.local;
365 		add_addr = msk->pm.addr_signal & ~BIT(MPTCP_ADD_ADDR_SIGNAL);
366 	}
367 	WRITE_ONCE(msk->pm.addr_signal, add_addr);
368 	ret = true;
369 
370 out_unlock:
371 	spin_unlock_bh(&msk->pm.lock);
372 	return ret;
373 }
374 
375 bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
376 			     struct mptcp_rm_list *rm_list)
377 {
378 	int ret = false, len;
379 	u8 rm_addr;
380 
381 	spin_lock_bh(&msk->pm.lock);
382 
383 	/* double check after the lock is acquired */
384 	if (!mptcp_pm_should_rm_signal(msk))
385 		goto out_unlock;
386 
387 	rm_addr = msk->pm.addr_signal & ~BIT(MPTCP_RM_ADDR_SIGNAL);
388 	len = mptcp_rm_addr_len(&msk->pm.rm_list_tx);
389 	if (len < 0) {
390 		WRITE_ONCE(msk->pm.addr_signal, rm_addr);
391 		goto out_unlock;
392 	}
393 	if (remaining < len)
394 		goto out_unlock;
395 
396 	*rm_list = msk->pm.rm_list_tx;
397 	WRITE_ONCE(msk->pm.addr_signal, rm_addr);
398 	ret = true;
399 
400 out_unlock:
401 	spin_unlock_bh(&msk->pm.lock);
402 	return ret;
403 }
404 
405 int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
406 {
407 	return mptcp_pm_nl_get_local_id(msk, skc);
408 }
409 
410 void mptcp_pm_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk)
411 {
412 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
413 	u32 rcv_tstamp = READ_ONCE(tcp_sk(ssk)->rcv_tstamp);
414 
415 	/* keep track of rtx periods with no progress */
416 	if (!subflow->stale_count) {
417 		subflow->stale_rcv_tstamp = rcv_tstamp;
418 		subflow->stale_count++;
419 	} else if (subflow->stale_rcv_tstamp == rcv_tstamp) {
420 		if (subflow->stale_count < U8_MAX)
421 			subflow->stale_count++;
422 		mptcp_pm_nl_subflow_chk_stale(msk, ssk);
423 	} else {
424 		subflow->stale_count = 0;
425 		mptcp_subflow_set_active(subflow);
426 	}
427 }
428 
429 void mptcp_pm_data_reset(struct mptcp_sock *msk)
430 {
431 	u8 pm_type = mptcp_get_pm_type(sock_net((struct sock *)msk));
432 	struct mptcp_pm_data *pm = &msk->pm;
433 
434 	pm->add_addr_signaled = 0;
435 	pm->add_addr_accepted = 0;
436 	pm->local_addr_used = 0;
437 	pm->subflows = 0;
438 	pm->rm_list_tx.nr = 0;
439 	pm->rm_list_rx.nr = 0;
440 	WRITE_ONCE(pm->pm_type, pm_type);
441 
442 	if (pm_type == MPTCP_PM_TYPE_KERNEL) {
443 		bool subflows_allowed = !!mptcp_pm_get_subflows_max(msk);
444 
445 		/* pm->work_pending must be only be set to 'true' when
446 		 * pm->pm_type is set to MPTCP_PM_TYPE_KERNEL
447 		 */
448 		WRITE_ONCE(pm->work_pending,
449 			   (!!mptcp_pm_get_local_addr_max(msk) &&
450 			    subflows_allowed) ||
451 			   !!mptcp_pm_get_add_addr_signal_max(msk));
452 		WRITE_ONCE(pm->accept_addr,
453 			   !!mptcp_pm_get_add_addr_accept_max(msk) &&
454 			   subflows_allowed);
455 		WRITE_ONCE(pm->accept_subflow, subflows_allowed);
456 	} else {
457 		WRITE_ONCE(pm->work_pending, 0);
458 		WRITE_ONCE(pm->accept_addr, 0);
459 		WRITE_ONCE(pm->accept_subflow, 0);
460 	}
461 
462 	WRITE_ONCE(pm->addr_signal, 0);
463 	WRITE_ONCE(pm->remote_deny_join_id0, false);
464 	pm->status = 0;
465 	bitmap_fill(msk->pm.id_avail_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
466 }
467 
468 void mptcp_pm_data_init(struct mptcp_sock *msk)
469 {
470 	spin_lock_init(&msk->pm.lock);
471 	INIT_LIST_HEAD(&msk->pm.anno_list);
472 	INIT_LIST_HEAD(&msk->pm.userspace_pm_local_addr_list);
473 	mptcp_pm_data_reset(msk);
474 }
475 
476 void __init mptcp_pm_init(void)
477 {
478 	mptcp_pm_nl_init();
479 }
480