1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2019, Intel Corporation.
6 #define pr_fmt(fmt) "MPTCP: " fmt
8 #include <linux/kernel.h>
10 #include <net/mptcp.h>
15 /* path manager command handlers */
17 int mptcp_pm_announce_addr(struct mptcp_sock *msk,
18 const struct mptcp_addr_info *addr,
21 u8 add_addr = READ_ONCE(msk->pm.addr_signal);
23 pr_debug("msk=%p, local_id=%d, echo=%d", msk, addr->id, echo);
25 lockdep_assert_held(&msk->pm.lock);
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);
34 msk->pm.remote = *addr;
35 add_addr |= BIT(MPTCP_ADD_ADDR_ECHO);
37 msk->pm.local = *addr;
38 add_addr |= BIT(MPTCP_ADD_ADDR_SIGNAL);
40 WRITE_ONCE(msk->pm.addr_signal, add_addr);
44 int mptcp_pm_remove_addr(struct mptcp_sock *msk, const struct mptcp_rm_list *rm_list)
46 u8 rm_addr = READ_ONCE(msk->pm.addr_signal);
48 pr_debug("msk=%p, rm_list_nr=%d", msk, rm_list->nr);
51 pr_warn("addr_signal error, rm_addr=%d", rm_addr);
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);
62 int mptcp_pm_remove_subflow(struct mptcp_sock *msk, const struct mptcp_rm_list *rm_list)
64 pr_debug("msk=%p, rm_list_nr=%d", msk, rm_list->nr);
66 spin_lock_bh(&msk->pm.lock);
67 mptcp_pm_nl_rm_subflow_received(msk, rm_list);
68 spin_unlock_bh(&msk->pm.lock);
72 /* path manager event handlers */
74 void mptcp_pm_new_connection(struct mptcp_sock *msk, const struct sock *ssk, int server_side)
76 struct mptcp_pm_data *pm = &msk->pm;
78 pr_debug("msk=%p, token=%u side=%d", msk, msk->token, server_side);
80 WRITE_ONCE(pm->server_side, server_side);
81 mptcp_event(MPTCP_EVENT_CREATED, msk, ssk, GFP_ATOMIC);
84 bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk)
86 struct mptcp_pm_data *pm = &msk->pm;
87 unsigned int subflows_max;
90 if (mptcp_pm_is_userspace(msk))
91 return mptcp_userspace_pm_active(msk);
93 subflows_max = mptcp_pm_get_subflows_max(msk);
95 pr_debug("msk=%p subflows=%d max=%d allow=%d", msk, pm->subflows,
96 subflows_max, READ_ONCE(pm->accept_subflow));
98 /* try to avoid acquiring the lock below */
99 if (!READ_ONCE(pm->accept_subflow))
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);
108 spin_unlock_bh(&pm->lock);
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
116 static bool mptcp_pm_schedule_work(struct mptcp_sock *msk,
117 enum mptcp_pm_status new_status)
119 pr_debug("msk=%p status=%x new=%lx", msk, msk->pm.status,
121 if (msk->pm.status & BIT(new_status))
124 msk->pm.status |= BIT(new_status);
125 mptcp_schedule_work((struct sock *)msk);
129 void mptcp_pm_fully_established(struct mptcp_sock *msk, const struct sock *ssk, gfp_t gfp)
131 struct mptcp_pm_data *pm = &msk->pm;
132 bool announce = false;
134 pr_debug("msk=%p", msk);
136 spin_lock_bh(&pm->lock);
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.
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);
146 if ((msk->pm.status & BIT(MPTCP_PM_ALREADY_ESTABLISHED)) == 0)
149 msk->pm.status |= BIT(MPTCP_PM_ALREADY_ESTABLISHED);
150 spin_unlock_bh(&pm->lock);
153 mptcp_event(MPTCP_EVENT_ESTABLISHED, msk, ssk, gfp);
156 void mptcp_pm_connection_closed(struct mptcp_sock *msk)
158 pr_debug("msk=%p", msk);
161 void mptcp_pm_subflow_established(struct mptcp_sock *msk)
163 struct mptcp_pm_data *pm = &msk->pm;
165 pr_debug("msk=%p", msk);
167 if (!READ_ONCE(pm->work_pending))
170 spin_lock_bh(&pm->lock);
172 if (READ_ONCE(pm->work_pending))
173 mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
175 spin_unlock_bh(&pm->lock);
178 void mptcp_pm_subflow_check_next(struct mptcp_sock *msk, const struct sock *ssk,
179 const struct mptcp_subflow_context *subflow)
181 struct mptcp_pm_data *pm = &msk->pm;
182 bool update_subflows;
184 update_subflows = (subflow->request_join || subflow->mp_join) &&
185 mptcp_pm_is_kernel(msk);
186 if (!READ_ONCE(pm->work_pending) && !update_subflows)
189 spin_lock_bh(&pm->lock);
191 __mptcp_pm_close_subflow(msk);
193 /* Even if this subflow is not really established, tell the PM to try
194 * to pick the next ones, if possible.
196 if (mptcp_pm_nl_check_work_pending(msk))
197 mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
199 spin_unlock_bh(&pm->lock);
202 void mptcp_pm_add_addr_received(const struct sock *ssk,
203 const struct mptcp_addr_info *addr)
205 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
206 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
207 struct mptcp_pm_data *pm = &msk->pm;
209 pr_debug("msk=%p remote_id=%d accept=%d", msk, addr->id,
210 READ_ONCE(pm->accept_addr));
212 mptcp_event_addr_announced(ssk, addr);
214 spin_lock_bh(&pm->lock);
216 if (mptcp_pm_is_userspace(msk)) {
217 if (mptcp_userspace_pm_active(msk)) {
218 mptcp_pm_announce_addr(msk, addr, true);
219 mptcp_pm_add_addr_send_ack(msk);
221 __MPTCP_INC_STATS(sock_net((struct sock *)msk), MPTCP_MIB_ADDADDRDROP);
223 } else if (!READ_ONCE(pm->accept_addr)) {
224 mptcp_pm_announce_addr(msk, addr, true);
225 mptcp_pm_add_addr_send_ack(msk);
226 } else if (mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_RECEIVED)) {
229 __MPTCP_INC_STATS(sock_net((struct sock *)msk), MPTCP_MIB_ADDADDRDROP);
232 spin_unlock_bh(&pm->lock);
235 void mptcp_pm_add_addr_echoed(struct mptcp_sock *msk,
236 const struct mptcp_addr_info *addr)
238 struct mptcp_pm_data *pm = &msk->pm;
240 pr_debug("msk=%p", msk);
242 spin_lock_bh(&pm->lock);
244 if (mptcp_lookup_anno_list_by_saddr(msk, addr) && READ_ONCE(pm->work_pending))
245 mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
247 spin_unlock_bh(&pm->lock);
250 void mptcp_pm_add_addr_send_ack(struct mptcp_sock *msk)
252 if (!mptcp_pm_should_add_signal(msk))
255 mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_SEND_ACK);
258 void mptcp_pm_rm_addr_received(struct mptcp_sock *msk,
259 const struct mptcp_rm_list *rm_list)
261 struct mptcp_pm_data *pm = &msk->pm;
264 pr_debug("msk=%p remote_ids_nr=%d", msk, rm_list->nr);
266 for (i = 0; i < rm_list->nr; i++)
267 mptcp_event_addr_removed(msk, rm_list->ids[i]);
269 spin_lock_bh(&pm->lock);
270 if (mptcp_pm_schedule_work(msk, MPTCP_PM_RM_ADDR_RECEIVED))
271 pm->rm_list_rx = *rm_list;
273 __MPTCP_INC_STATS(sock_net((struct sock *)msk), MPTCP_MIB_RMADDRDROP);
274 spin_unlock_bh(&pm->lock);
277 void mptcp_pm_mp_prio_received(struct sock *ssk, u8 bkup)
279 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
280 struct sock *sk = subflow->conn;
281 struct mptcp_sock *msk;
283 pr_debug("subflow->backup=%d, bkup=%d\n", subflow->backup, bkup);
285 if (subflow->backup != bkup) {
286 subflow->backup = bkup;
288 if (!sock_owned_by_user(sk))
289 msk->last_snd = NULL;
291 __set_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags);
292 mptcp_data_unlock(sk);
295 mptcp_event(MPTCP_EVENT_SUB_PRIORITY, msk, ssk, GFP_ATOMIC);
298 void mptcp_pm_mp_fail_received(struct sock *sk, u64 fail_seq)
300 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
301 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
303 pr_debug("fail_seq=%llu", fail_seq);
305 if (!READ_ONCE(msk->allow_infinite_fallback))
308 if (!subflow->fail_tout) {
309 pr_debug("send MP_FAIL response and infinite map");
311 subflow->send_mp_fail = 1;
312 subflow->send_infinite_map = 1;
315 pr_debug("MP_FAIL response received");
316 WRITE_ONCE(subflow->fail_tout, 0);
320 /* path manager helpers */
322 bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, const struct sk_buff *skb,
323 unsigned int opt_size, unsigned int remaining,
324 struct mptcp_addr_info *addr, bool *echo,
325 bool *drop_other_suboptions)
332 spin_lock_bh(&msk->pm.lock);
334 /* double check after the lock is acquired */
335 if (!mptcp_pm_should_add_signal(msk))
338 /* always drop every other options for pure ack ADD_ADDR; this is a
339 * plain dup-ack from TCP perspective. The other MPTCP-relevant info,
340 * if any, will be carried by the 'original' TCP ack
342 if (skb && skb_is_tcp_pure_ack(skb)) {
343 remaining += opt_size;
344 *drop_other_suboptions = true;
347 *echo = mptcp_pm_should_add_signal_echo(msk);
348 port = !!(*echo ? msk->pm.remote.port : msk->pm.local.port);
350 family = *echo ? msk->pm.remote.family : msk->pm.local.family;
351 if (remaining < mptcp_add_addr_len(family, *echo, port))
355 *addr = msk->pm.remote;
356 add_addr = msk->pm.addr_signal & ~BIT(MPTCP_ADD_ADDR_ECHO);
358 *addr = msk->pm.local;
359 add_addr = msk->pm.addr_signal & ~BIT(MPTCP_ADD_ADDR_SIGNAL);
361 WRITE_ONCE(msk->pm.addr_signal, add_addr);
365 spin_unlock_bh(&msk->pm.lock);
369 bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
370 struct mptcp_rm_list *rm_list)
372 int ret = false, len;
375 spin_lock_bh(&msk->pm.lock);
377 /* double check after the lock is acquired */
378 if (!mptcp_pm_should_rm_signal(msk))
381 rm_addr = msk->pm.addr_signal & ~BIT(MPTCP_RM_ADDR_SIGNAL);
382 len = mptcp_rm_addr_len(&msk->pm.rm_list_tx);
384 WRITE_ONCE(msk->pm.addr_signal, rm_addr);
390 *rm_list = msk->pm.rm_list_tx;
391 WRITE_ONCE(msk->pm.addr_signal, rm_addr);
395 spin_unlock_bh(&msk->pm.lock);
399 int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
401 return mptcp_pm_nl_get_local_id(msk, skc);
404 void mptcp_pm_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk)
406 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
407 u32 rcv_tstamp = READ_ONCE(tcp_sk(ssk)->rcv_tstamp);
409 /* keep track of rtx periods with no progress */
410 if (!subflow->stale_count) {
411 subflow->stale_rcv_tstamp = rcv_tstamp;
412 subflow->stale_count++;
413 } else if (subflow->stale_rcv_tstamp == rcv_tstamp) {
414 if (subflow->stale_count < U8_MAX)
415 subflow->stale_count++;
416 mptcp_pm_nl_subflow_chk_stale(msk, ssk);
418 subflow->stale_count = 0;
419 mptcp_subflow_set_active(subflow);
423 void mptcp_pm_data_reset(struct mptcp_sock *msk)
425 u8 pm_type = mptcp_get_pm_type(sock_net((struct sock *)msk));
426 struct mptcp_pm_data *pm = &msk->pm;
428 pm->add_addr_signaled = 0;
429 pm->add_addr_accepted = 0;
430 pm->local_addr_used = 0;
432 pm->rm_list_tx.nr = 0;
433 pm->rm_list_rx.nr = 0;
434 WRITE_ONCE(pm->pm_type, pm_type);
436 if (pm_type == MPTCP_PM_TYPE_KERNEL) {
437 bool subflows_allowed = !!mptcp_pm_get_subflows_max(msk);
439 /* pm->work_pending must be only be set to 'true' when
440 * pm->pm_type is set to MPTCP_PM_TYPE_KERNEL
442 WRITE_ONCE(pm->work_pending,
443 (!!mptcp_pm_get_local_addr_max(msk) &&
445 !!mptcp_pm_get_add_addr_signal_max(msk));
446 WRITE_ONCE(pm->accept_addr,
447 !!mptcp_pm_get_add_addr_accept_max(msk) &&
449 WRITE_ONCE(pm->accept_subflow, subflows_allowed);
451 WRITE_ONCE(pm->work_pending, 0);
452 WRITE_ONCE(pm->accept_addr, 0);
453 WRITE_ONCE(pm->accept_subflow, 0);
456 WRITE_ONCE(pm->addr_signal, 0);
457 WRITE_ONCE(pm->remote_deny_join_id0, false);
459 bitmap_fill(msk->pm.id_avail_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
462 void mptcp_pm_data_init(struct mptcp_sock *msk)
464 spin_lock_init(&msk->pm.lock);
465 INIT_LIST_HEAD(&msk->pm.anno_list);
466 INIT_LIST_HEAD(&msk->pm.userspace_pm_local_addr_list);
467 mptcp_pm_data_reset(msk);
470 void __init mptcp_pm_init(void)