blob: f9c88e2abb8e6d5a8f5e51b59488e26529baaba5 [file] [log] [blame]
Peter Krystad1b1c7a02020-03-27 14:48:38 -07001// SPDX-License-Identifier: GPL-2.0
2/* Multipath TCP
3 *
4 * Copyright (c) 2019, Intel Corporation.
5 */
Geliang Tangc85adce2020-04-03 17:14:08 +08006#define pr_fmt(fmt) "MPTCP: " fmt
7
Peter Krystad1b1c7a02020-03-27 14:48:38 -07008#include <linux/kernel.h>
9#include <net/tcp.h>
10#include <net/mptcp.h>
11#include "protocol.h"
12
Peter Krystad1b1c7a02020-03-27 14:48:38 -070013/* path manager command handlers */
14
15int mptcp_pm_announce_addr(struct mptcp_sock *msk,
Geliang Tang6a6c05a2020-09-24 08:29:50 +080016 const struct mptcp_addr_info *addr,
17 bool echo)
Peter Krystad1b1c7a02020-03-27 14:48:38 -070018{
Peter Krystad926bdea2020-03-27 14:48:41 -070019 pr_debug("msk=%p, local_id=%d", msk, addr->id);
20
21 msk->pm.local = *addr;
Geliang Tang6a6c05a2020-09-24 08:29:50 +080022 WRITE_ONCE(msk->pm.add_addr_echo, echo);
Geliang Tangf643b802020-09-24 08:29:47 +080023 WRITE_ONCE(msk->pm.add_addr_signal, true);
Peter Krystad926bdea2020-03-27 14:48:41 -070024 return 0;
Peter Krystad1b1c7a02020-03-27 14:48:38 -070025}
26
27int mptcp_pm_remove_addr(struct mptcp_sock *msk, u8 local_id)
28{
Geliang Tangb6c08382020-09-24 08:29:54 +080029 pr_debug("msk=%p, local_id=%d", msk, local_id);
30
31 msk->pm.rm_id = local_id;
32 WRITE_ONCE(msk->pm.rm_addr_signal, true);
33 return 0;
Peter Krystad1b1c7a02020-03-27 14:48:38 -070034}
35
Geliang Tang0ee42612020-09-24 08:29:55 +080036int mptcp_pm_remove_subflow(struct mptcp_sock *msk, u8 local_id)
Peter Krystad1b1c7a02020-03-27 14:48:38 -070037{
Geliang Tang0ee42612020-09-24 08:29:55 +080038 pr_debug("msk=%p, local_id=%d", msk, local_id);
39
40 spin_lock_bh(&msk->pm.lock);
41 mptcp_pm_nl_rm_subflow_received(msk, local_id);
42 spin_unlock_bh(&msk->pm.lock);
43 return 0;
Peter Krystad1b1c7a02020-03-27 14:48:38 -070044}
45
46/* path manager event handlers */
47
48void mptcp_pm_new_connection(struct mptcp_sock *msk, int server_side)
49{
50 struct mptcp_pm_data *pm = &msk->pm;
51
52 pr_debug("msk=%p, token=%u side=%d", msk, msk->token, server_side);
53
54 WRITE_ONCE(pm->server_side, server_side);
55}
56
57bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk)
58{
Peter Krystad926bdea2020-03-27 14:48:41 -070059 struct mptcp_pm_data *pm = &msk->pm;
Geliang Tangf58f0652020-09-24 08:29:53 +080060 int ret = 0;
Peter Krystad926bdea2020-03-27 14:48:41 -070061
62 pr_debug("msk=%p subflows=%d max=%d allow=%d", msk, pm->subflows,
63 pm->subflows_max, READ_ONCE(pm->accept_subflow));
64
65 /* try to avoid acquiring the lock below */
66 if (!READ_ONCE(pm->accept_subflow))
67 return false;
68
69 spin_lock_bh(&pm->lock);
Geliang Tangf58f0652020-09-24 08:29:53 +080070 if (READ_ONCE(pm->accept_subflow)) {
71 ret = pm->subflows < pm->subflows_max;
72 if (ret && ++pm->subflows == pm->subflows_max)
73 WRITE_ONCE(pm->accept_subflow, false);
74 }
Peter Krystad926bdea2020-03-27 14:48:41 -070075 spin_unlock_bh(&pm->lock);
76
77 return ret;
78}
79
80/* return true if the new status bit is currently cleared, that is, this event
81 * can be server, eventually by an already scheduled work
82 */
83static bool mptcp_pm_schedule_work(struct mptcp_sock *msk,
84 enum mptcp_pm_status new_status)
85{
86 pr_debug("msk=%p status=%x new=%lx", msk, msk->pm.status,
87 BIT(new_status));
88 if (msk->pm.status & BIT(new_status))
89 return false;
90
91 msk->pm.status |= BIT(new_status);
Paolo Abeniba8f48f2020-11-16 10:48:05 +010092 mptcp_schedule_work((struct sock *)msk);
Peter Krystad926bdea2020-03-27 14:48:41 -070093 return true;
Peter Krystad1b1c7a02020-03-27 14:48:38 -070094}
95
96void mptcp_pm_fully_established(struct mptcp_sock *msk)
97{
Peter Krystad926bdea2020-03-27 14:48:41 -070098 struct mptcp_pm_data *pm = &msk->pm;
99
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700100 pr_debug("msk=%p", msk);
Peter Krystad926bdea2020-03-27 14:48:41 -0700101
102 /* try to avoid acquiring the lock below */
103 if (!READ_ONCE(pm->work_pending))
104 return;
105
106 spin_lock_bh(&pm->lock);
107
108 if (READ_ONCE(pm->work_pending))
109 mptcp_pm_schedule_work(msk, MPTCP_PM_ESTABLISHED);
110
111 spin_unlock_bh(&pm->lock);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700112}
113
114void mptcp_pm_connection_closed(struct mptcp_sock *msk)
115{
116 pr_debug("msk=%p", msk);
117}
118
119void mptcp_pm_subflow_established(struct mptcp_sock *msk,
120 struct mptcp_subflow_context *subflow)
121{
Peter Krystad926bdea2020-03-27 14:48:41 -0700122 struct mptcp_pm_data *pm = &msk->pm;
123
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700124 pr_debug("msk=%p", msk);
Peter Krystad926bdea2020-03-27 14:48:41 -0700125
126 if (!READ_ONCE(pm->work_pending))
127 return;
128
129 spin_lock_bh(&pm->lock);
130
131 if (READ_ONCE(pm->work_pending))
132 mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
133
134 spin_unlock_bh(&pm->lock);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700135}
136
137void mptcp_pm_subflow_closed(struct mptcp_sock *msk, u8 id)
138{
139 pr_debug("msk=%p", msk);
140}
141
142void mptcp_pm_add_addr_received(struct mptcp_sock *msk,
143 const struct mptcp_addr_info *addr)
144{
Peter Krystad926bdea2020-03-27 14:48:41 -0700145 struct mptcp_pm_data *pm = &msk->pm;
146
147 pr_debug("msk=%p remote_id=%d accept=%d", msk, addr->id,
148 READ_ONCE(pm->accept_addr));
149
Peter Krystad926bdea2020-03-27 14:48:41 -0700150 spin_lock_bh(&pm->lock);
151
Geliang Tang6a6c05a2020-09-24 08:29:50 +0800152 if (!READ_ONCE(pm->accept_addr))
153 mptcp_pm_announce_addr(msk, addr, true);
154 else if (mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_RECEIVED))
Peter Krystad926bdea2020-03-27 14:48:41 -0700155 pm->remote = *addr;
156
157 spin_unlock_bh(&pm->lock);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700158}
159
Geliang Tangd0876b22020-09-24 08:29:49 +0800160void mptcp_pm_rm_addr_received(struct mptcp_sock *msk, u8 rm_id)
161{
162 struct mptcp_pm_data *pm = &msk->pm;
163
164 pr_debug("msk=%p remote_id=%d", msk, rm_id);
165
166 spin_lock_bh(&pm->lock);
167 mptcp_pm_schedule_work(msk, MPTCP_PM_RM_ADDR_RECEIVED);
168 pm->rm_id = rm_id;
169 spin_unlock_bh(&pm->lock);
170}
171
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700172/* path manager helpers */
173
Geliang Tangf643b802020-09-24 08:29:47 +0800174bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
Geliang Tang6a6c05a2020-09-24 08:29:50 +0800175 struct mptcp_addr_info *saddr, bool *echo)
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700176{
Peter Krystad926bdea2020-03-27 14:48:41 -0700177 int ret = false;
178
179 spin_lock_bh(&msk->pm.lock);
180
181 /* double check after the lock is acquired */
Geliang Tangf643b802020-09-24 08:29:47 +0800182 if (!mptcp_pm_should_add_signal(msk))
Peter Krystad926bdea2020-03-27 14:48:41 -0700183 goto out_unlock;
184
Matthieu Baerts456afe02020-10-03 17:36:56 +0200185 *echo = READ_ONCE(msk->pm.add_addr_echo);
186
187 if (remaining < mptcp_add_addr_len(msk->pm.local.family, *echo))
Peter Krystad926bdea2020-03-27 14:48:41 -0700188 goto out_unlock;
189
190 *saddr = msk->pm.local;
Geliang Tangf643b802020-09-24 08:29:47 +0800191 WRITE_ONCE(msk->pm.add_addr_signal, false);
Peter Krystad926bdea2020-03-27 14:48:41 -0700192 ret = true;
193
194out_unlock:
195 spin_unlock_bh(&msk->pm.lock);
196 return ret;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700197}
198
Geliang Tang5cb104a2020-09-24 08:29:48 +0800199bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
200 u8 *rm_id)
201{
202 int ret = false;
203
204 spin_lock_bh(&msk->pm.lock);
205
206 /* double check after the lock is acquired */
207 if (!mptcp_pm_should_rm_signal(msk))
208 goto out_unlock;
209
210 if (remaining < TCPOLEN_MPTCP_RM_ADDR_BASE)
211 goto out_unlock;
212
213 *rm_id = msk->pm.rm_id;
214 WRITE_ONCE(msk->pm.rm_addr_signal, false);
215 ret = true;
216
217out_unlock:
218 spin_unlock_bh(&msk->pm.lock);
219 return ret;
220}
221
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700222int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
223{
Paolo Abeni01cacb02020-03-27 14:48:51 -0700224 return mptcp_pm_nl_get_local_id(msk, skc);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700225}
226
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700227void mptcp_pm_data_init(struct mptcp_sock *msk)
228{
229 msk->pm.add_addr_signaled = 0;
230 msk->pm.add_addr_accepted = 0;
231 msk->pm.local_addr_used = 0;
232 msk->pm.subflows = 0;
Geliang Tang5cb104a2020-09-24 08:29:48 +0800233 msk->pm.rm_id = 0;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700234 WRITE_ONCE(msk->pm.work_pending, false);
Geliang Tangf643b802020-09-24 08:29:47 +0800235 WRITE_ONCE(msk->pm.add_addr_signal, false);
Geliang Tang5cb104a2020-09-24 08:29:48 +0800236 WRITE_ONCE(msk->pm.rm_addr_signal, false);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700237 WRITE_ONCE(msk->pm.accept_addr, false);
238 WRITE_ONCE(msk->pm.accept_subflow, false);
Geliang Tang6a6c05a2020-09-24 08:29:50 +0800239 WRITE_ONCE(msk->pm.add_addr_echo, false);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700240 msk->pm.status = 0;
241
242 spin_lock_init(&msk->pm.lock);
Geliang Tangb6c08382020-09-24 08:29:54 +0800243 INIT_LIST_HEAD(&msk->pm.anno_list);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700244
245 mptcp_pm_nl_data_init(msk);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700246}
247
Paolo Abenid39dcec2020-06-26 19:29:59 +0200248void __init mptcp_pm_init(void)
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700249{
Paolo Abeni01cacb02020-03-27 14:48:51 -0700250 mptcp_pm_nl_init();
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700251}