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 subflows_max = mptcp_pm_get_subflows_max(msk);
92 pr_debug("msk=%p subflows=%d max=%d allow=%d", msk, pm->subflows,
93 subflows_max, READ_ONCE(pm->accept_subflow));
95 /* try to avoid acquiring the lock below */
96 if (!READ_ONCE(pm->accept_subflow))
99 spin_lock_bh(&pm->lock);
100 if (READ_ONCE(pm->accept_subflow)) {
101 ret = pm->subflows < subflows_max;
102 if (ret && ++pm->subflows == subflows_max)
103 WRITE_ONCE(pm->accept_subflow, false);
105 spin_unlock_bh(&pm->lock);
110 /* return true if the new status bit is currently cleared, that is, this event
111 * can be server, eventually by an already scheduled work
113 static bool mptcp_pm_schedule_work(struct mptcp_sock *msk,
114 enum mptcp_pm_status new_status)
116 pr_debug("msk=%p status=%x new=%lx", msk, msk->pm.status,
118 if (msk->pm.status & BIT(new_status))
121 msk->pm.status |= BIT(new_status);
122 mptcp_schedule_work((struct sock *)msk);
126 void mptcp_pm_fully_established(struct mptcp_sock *msk, const struct sock *ssk, gfp_t gfp)
128 struct mptcp_pm_data *pm = &msk->pm;
129 bool announce = false;
131 pr_debug("msk=%p", msk);
133 spin_lock_bh(&pm->lock);
135 /* mptcp_pm_fully_established() can be invoked by multiple
136 * racing paths - accept() and check_fully_established()
137 * be sure to serve this event only once.
139 if (READ_ONCE(pm->work_pending) &&
140 !(msk->pm.status & BIT(MPTCP_PM_ALREADY_ESTABLISHED)))
141 mptcp_pm_schedule_work(msk, MPTCP_PM_ESTABLISHED);
143 if ((msk->pm.status & BIT(MPTCP_PM_ALREADY_ESTABLISHED)) == 0)
146 msk->pm.status |= BIT(MPTCP_PM_ALREADY_ESTABLISHED);
147 spin_unlock_bh(&pm->lock);
150 mptcp_event(MPTCP_EVENT_ESTABLISHED, msk, ssk, gfp);
153 void mptcp_pm_connection_closed(struct mptcp_sock *msk)
155 pr_debug("msk=%p", msk);
158 void mptcp_pm_subflow_established(struct mptcp_sock *msk)
160 struct mptcp_pm_data *pm = &msk->pm;
162 pr_debug("msk=%p", msk);
164 if (!READ_ONCE(pm->work_pending))
167 spin_lock_bh(&pm->lock);
169 if (READ_ONCE(pm->work_pending))
170 mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
172 spin_unlock_bh(&pm->lock);
175 void mptcp_pm_subflow_check_next(struct mptcp_sock *msk, const struct sock *ssk,
176 const struct mptcp_subflow_context *subflow)
178 struct mptcp_pm_data *pm = &msk->pm;
179 bool update_subflows;
181 update_subflows = (ssk->sk_state == TCP_CLOSE) &&
182 (subflow->request_join || subflow->mp_join);
183 if (!READ_ONCE(pm->work_pending) && !update_subflows)
186 spin_lock_bh(&pm->lock);
190 /* Even if this subflow is not really established, tell the PM to try
191 * to pick the next ones, if possible.
193 if (mptcp_pm_nl_check_work_pending(msk))
194 mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
196 spin_unlock_bh(&pm->lock);
199 void mptcp_pm_add_addr_received(struct mptcp_sock *msk,
200 const struct mptcp_addr_info *addr)
202 struct mptcp_pm_data *pm = &msk->pm;
204 pr_debug("msk=%p remote_id=%d accept=%d", msk, addr->id,
205 READ_ONCE(pm->accept_addr));
207 mptcp_event_addr_announced(msk, addr);
209 spin_lock_bh(&pm->lock);
211 if (!READ_ONCE(pm->accept_addr)) {
212 mptcp_pm_announce_addr(msk, addr, true);
213 mptcp_pm_add_addr_send_ack(msk);
214 } else if (mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_RECEIVED)) {
217 __MPTCP_INC_STATS(sock_net((struct sock *)msk), MPTCP_MIB_ADDADDRDROP);
220 spin_unlock_bh(&pm->lock);
223 void mptcp_pm_add_addr_echoed(struct mptcp_sock *msk,
224 const struct mptcp_addr_info *addr)
226 struct mptcp_pm_data *pm = &msk->pm;
228 pr_debug("msk=%p", msk);
230 spin_lock_bh(&pm->lock);
232 if (mptcp_lookup_anno_list_by_saddr(msk, addr) && READ_ONCE(pm->work_pending))
233 mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
235 spin_unlock_bh(&pm->lock);
238 void mptcp_pm_add_addr_send_ack(struct mptcp_sock *msk)
240 if (!mptcp_pm_should_add_signal(msk))
243 mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_SEND_ACK);
246 void mptcp_pm_rm_addr_received(struct mptcp_sock *msk,
247 const struct mptcp_rm_list *rm_list)
249 struct mptcp_pm_data *pm = &msk->pm;
252 pr_debug("msk=%p remote_ids_nr=%d", msk, rm_list->nr);
254 for (i = 0; i < rm_list->nr; i++)
255 mptcp_event_addr_removed(msk, rm_list->ids[i]);
257 spin_lock_bh(&pm->lock);
258 if (mptcp_pm_schedule_work(msk, MPTCP_PM_RM_ADDR_RECEIVED))
259 pm->rm_list_rx = *rm_list;
261 __MPTCP_INC_STATS(sock_net((struct sock *)msk), MPTCP_MIB_RMADDRDROP);
262 spin_unlock_bh(&pm->lock);
265 void mptcp_pm_mp_prio_received(struct sock *ssk, u8 bkup)
267 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
268 struct sock *sk = subflow->conn;
269 struct mptcp_sock *msk;
271 pr_debug("subflow->backup=%d, bkup=%d\n", subflow->backup, bkup);
273 if (subflow->backup != bkup) {
274 subflow->backup = bkup;
276 if (!sock_owned_by_user(sk))
277 msk->last_snd = NULL;
279 __set_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags);
280 mptcp_data_unlock(sk);
283 mptcp_event(MPTCP_EVENT_SUB_PRIORITY, msk, ssk, GFP_ATOMIC);
286 void mptcp_pm_mp_fail_received(struct sock *sk, u64 fail_seq)
288 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
289 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
291 pr_debug("fail_seq=%llu", fail_seq);
293 if (!mptcp_has_another_subflow(sk) && READ_ONCE(msk->allow_infinite_fallback))
294 subflow->send_infinite_map = 1;
297 /* path manager helpers */
299 bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, const struct sk_buff *skb,
300 unsigned int opt_size, unsigned int remaining,
301 struct mptcp_addr_info *addr, bool *echo,
302 bool *drop_other_suboptions)
309 spin_lock_bh(&msk->pm.lock);
311 /* double check after the lock is acquired */
312 if (!mptcp_pm_should_add_signal(msk))
315 /* always drop every other options for pure ack ADD_ADDR; this is a
316 * plain dup-ack from TCP perspective. The other MPTCP-relevant info,
317 * if any, will be carried by the 'original' TCP ack
319 if (skb && skb_is_tcp_pure_ack(skb)) {
320 remaining += opt_size;
321 *drop_other_suboptions = true;
324 *echo = mptcp_pm_should_add_signal_echo(msk);
325 port = !!(*echo ? msk->pm.remote.port : msk->pm.local.port);
327 family = *echo ? msk->pm.remote.family : msk->pm.local.family;
328 if (remaining < mptcp_add_addr_len(family, *echo, port))
332 *addr = msk->pm.remote;
333 add_addr = msk->pm.addr_signal & ~BIT(MPTCP_ADD_ADDR_ECHO);
335 *addr = msk->pm.local;
336 add_addr = msk->pm.addr_signal & ~BIT(MPTCP_ADD_ADDR_SIGNAL);
338 WRITE_ONCE(msk->pm.addr_signal, add_addr);
342 spin_unlock_bh(&msk->pm.lock);
346 bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
347 struct mptcp_rm_list *rm_list)
349 int ret = false, len;
352 spin_lock_bh(&msk->pm.lock);
354 /* double check after the lock is acquired */
355 if (!mptcp_pm_should_rm_signal(msk))
358 rm_addr = msk->pm.addr_signal & ~BIT(MPTCP_RM_ADDR_SIGNAL);
359 len = mptcp_rm_addr_len(&msk->pm.rm_list_tx);
361 WRITE_ONCE(msk->pm.addr_signal, rm_addr);
367 *rm_list = msk->pm.rm_list_tx;
368 WRITE_ONCE(msk->pm.addr_signal, rm_addr);
372 spin_unlock_bh(&msk->pm.lock);
376 int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
378 return mptcp_pm_nl_get_local_id(msk, skc);
381 void mptcp_pm_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk)
383 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
384 u32 rcv_tstamp = READ_ONCE(tcp_sk(ssk)->rcv_tstamp);
386 /* keep track of rtx periods with no progress */
387 if (!subflow->stale_count) {
388 subflow->stale_rcv_tstamp = rcv_tstamp;
389 subflow->stale_count++;
390 } else if (subflow->stale_rcv_tstamp == rcv_tstamp) {
391 if (subflow->stale_count < U8_MAX)
392 subflow->stale_count++;
393 mptcp_pm_nl_subflow_chk_stale(msk, ssk);
395 subflow->stale_count = 0;
396 mptcp_subflow_set_active(subflow);
400 void mptcp_pm_data_reset(struct mptcp_sock *msk)
402 msk->pm.add_addr_signaled = 0;
403 msk->pm.add_addr_accepted = 0;
404 msk->pm.local_addr_used = 0;
405 msk->pm.subflows = 0;
406 msk->pm.rm_list_tx.nr = 0;
407 msk->pm.rm_list_rx.nr = 0;
408 WRITE_ONCE(msk->pm.work_pending, false);
409 WRITE_ONCE(msk->pm.addr_signal, 0);
410 WRITE_ONCE(msk->pm.accept_addr, false);
411 WRITE_ONCE(msk->pm.accept_subflow, false);
412 WRITE_ONCE(msk->pm.remote_deny_join_id0, false);
414 bitmap_fill(msk->pm.id_avail_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
416 mptcp_pm_nl_data_init(msk);
419 void mptcp_pm_data_init(struct mptcp_sock *msk)
421 spin_lock_init(&msk->pm.lock);
422 INIT_LIST_HEAD(&msk->pm.anno_list);
423 mptcp_pm_data_reset(msk);
426 void __init mptcp_pm_init(void)