xref: /linux/net/mptcp/pm.c (revision de73b5a97bba1538f065e1e90d8eeac399db7510)
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 /* path manager command handlers */
14 
15 int mptcp_pm_announce_addr(struct mptcp_sock *msk,
16 			   const struct mptcp_addr_info *addr,
17 			   bool echo, bool port)
18 {
19 	u8 add_addr = READ_ONCE(msk->pm.addr_signal);
20 
21 	pr_debug("msk=%p, local_id=%d", msk, addr->id);
22 
23 	lockdep_assert_held(&msk->pm.lock);
24 
25 	if (add_addr) {
26 		pr_warn("addr_signal error, add_addr=%d", add_addr);
27 		return -EINVAL;
28 	}
29 
30 	msk->pm.local = *addr;
31 	add_addr |= BIT(MPTCP_ADD_ADDR_SIGNAL);
32 	if (echo)
33 		add_addr |= BIT(MPTCP_ADD_ADDR_ECHO);
34 	if (addr->family == AF_INET6)
35 		add_addr |= BIT(MPTCP_ADD_ADDR_IPV6);
36 	if (port)
37 		add_addr |= BIT(MPTCP_ADD_ADDR_PORT);
38 	WRITE_ONCE(msk->pm.addr_signal, add_addr);
39 	return 0;
40 }
41 
42 int mptcp_pm_remove_addr(struct mptcp_sock *msk, u8 local_id)
43 {
44 	u8 rm_addr = READ_ONCE(msk->pm.addr_signal);
45 
46 	pr_debug("msk=%p, local_id=%d", msk, local_id);
47 
48 	if (rm_addr) {
49 		pr_warn("addr_signal error, rm_addr=%d", rm_addr);
50 		return -EINVAL;
51 	}
52 
53 	msk->pm.rm_id = local_id;
54 	rm_addr |= BIT(MPTCP_RM_ADDR_SIGNAL);
55 	WRITE_ONCE(msk->pm.addr_signal, rm_addr);
56 	return 0;
57 }
58 
59 int mptcp_pm_remove_subflow(struct mptcp_sock *msk, u8 local_id)
60 {
61 	pr_debug("msk=%p, local_id=%d", msk, local_id);
62 
63 	spin_lock_bh(&msk->pm.lock);
64 	mptcp_pm_nl_rm_subflow_received(msk, local_id);
65 	spin_unlock_bh(&msk->pm.lock);
66 	return 0;
67 }
68 
69 /* path manager event handlers */
70 
71 void mptcp_pm_new_connection(struct mptcp_sock *msk, const struct sock *ssk, int server_side)
72 {
73 	struct mptcp_pm_data *pm = &msk->pm;
74 
75 	pr_debug("msk=%p, token=%u side=%d", msk, msk->token, server_side);
76 
77 	WRITE_ONCE(pm->server_side, server_side);
78 	mptcp_event(MPTCP_EVENT_CREATED, msk, ssk, GFP_ATOMIC);
79 }
80 
81 bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk)
82 {
83 	struct mptcp_pm_data *pm = &msk->pm;
84 	unsigned int subflows_max;
85 	int ret = 0;
86 
87 	subflows_max = mptcp_pm_get_subflows_max(msk);
88 
89 	pr_debug("msk=%p subflows=%d max=%d allow=%d", msk, pm->subflows,
90 		 subflows_max, READ_ONCE(pm->accept_subflow));
91 
92 	/* try to avoid acquiring the lock below */
93 	if (!READ_ONCE(pm->accept_subflow))
94 		return false;
95 
96 	spin_lock_bh(&pm->lock);
97 	if (READ_ONCE(pm->accept_subflow)) {
98 		ret = pm->subflows < subflows_max;
99 		if (ret && ++pm->subflows == subflows_max)
100 			WRITE_ONCE(pm->accept_subflow, false);
101 	}
102 	spin_unlock_bh(&pm->lock);
103 
104 	return ret;
105 }
106 
107 /* return true if the new status bit is currently cleared, that is, this event
108  * can be server, eventually by an already scheduled work
109  */
110 static bool mptcp_pm_schedule_work(struct mptcp_sock *msk,
111 				   enum mptcp_pm_status new_status)
112 {
113 	pr_debug("msk=%p status=%x new=%lx", msk, msk->pm.status,
114 		 BIT(new_status));
115 	if (msk->pm.status & BIT(new_status))
116 		return false;
117 
118 	msk->pm.status |= BIT(new_status);
119 	mptcp_schedule_work((struct sock *)msk);
120 	return true;
121 }
122 
123 void mptcp_pm_fully_established(struct mptcp_sock *msk, const struct sock *ssk, gfp_t gfp)
124 {
125 	struct mptcp_pm_data *pm = &msk->pm;
126 	bool announce = false;
127 
128 	pr_debug("msk=%p", msk);
129 
130 	spin_lock_bh(&pm->lock);
131 
132 	/* mptcp_pm_fully_established() can be invoked by multiple
133 	 * racing paths - accept() and check_fully_established()
134 	 * be sure to serve this event only once.
135 	 */
136 	if (READ_ONCE(pm->work_pending) &&
137 	    !(msk->pm.status & BIT(MPTCP_PM_ALREADY_ESTABLISHED)))
138 		mptcp_pm_schedule_work(msk, MPTCP_PM_ESTABLISHED);
139 
140 	if ((msk->pm.status & BIT(MPTCP_PM_ALREADY_ESTABLISHED)) == 0)
141 		announce = true;
142 
143 	msk->pm.status |= BIT(MPTCP_PM_ALREADY_ESTABLISHED);
144 	spin_unlock_bh(&pm->lock);
145 
146 	if (announce)
147 		mptcp_event(MPTCP_EVENT_ESTABLISHED, msk, ssk, gfp);
148 }
149 
150 void mptcp_pm_connection_closed(struct mptcp_sock *msk)
151 {
152 	pr_debug("msk=%p", msk);
153 }
154 
155 void mptcp_pm_subflow_established(struct mptcp_sock *msk,
156 				  struct mptcp_subflow_context *subflow)
157 {
158 	struct mptcp_pm_data *pm = &msk->pm;
159 
160 	pr_debug("msk=%p", msk);
161 
162 	if (!READ_ONCE(pm->work_pending))
163 		return;
164 
165 	spin_lock_bh(&pm->lock);
166 
167 	if (READ_ONCE(pm->work_pending))
168 		mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
169 
170 	spin_unlock_bh(&pm->lock);
171 }
172 
173 void mptcp_pm_subflow_closed(struct mptcp_sock *msk, u8 id)
174 {
175 	pr_debug("msk=%p", msk);
176 }
177 
178 void mptcp_pm_add_addr_received(struct mptcp_sock *msk,
179 				const struct mptcp_addr_info *addr)
180 {
181 	struct mptcp_pm_data *pm = &msk->pm;
182 
183 	pr_debug("msk=%p remote_id=%d accept=%d", msk, addr->id,
184 		 READ_ONCE(pm->accept_addr));
185 
186 	mptcp_event_addr_announced(msk, addr);
187 
188 	spin_lock_bh(&pm->lock);
189 
190 	if (!READ_ONCE(pm->accept_addr)) {
191 		mptcp_pm_announce_addr(msk, addr, true, addr->port);
192 		mptcp_pm_add_addr_send_ack(msk);
193 	} else if (mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_RECEIVED)) {
194 		pm->remote = *addr;
195 	}
196 
197 	spin_unlock_bh(&pm->lock);
198 }
199 
200 void mptcp_pm_add_addr_send_ack(struct mptcp_sock *msk)
201 {
202 	if (!mptcp_pm_should_add_signal(msk))
203 		return;
204 
205 	mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_SEND_ACK);
206 }
207 
208 void mptcp_pm_rm_addr_received(struct mptcp_sock *msk, u8 rm_id)
209 {
210 	struct mptcp_pm_data *pm = &msk->pm;
211 
212 	pr_debug("msk=%p remote_id=%d", msk, rm_id);
213 
214 	mptcp_event_addr_removed(msk, rm_id);
215 
216 	spin_lock_bh(&pm->lock);
217 	mptcp_pm_schedule_work(msk, MPTCP_PM_RM_ADDR_RECEIVED);
218 	pm->rm_id = rm_id;
219 	spin_unlock_bh(&pm->lock);
220 }
221 
222 void mptcp_pm_mp_prio_received(struct sock *sk, u8 bkup)
223 {
224 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
225 
226 	pr_debug("subflow->backup=%d, bkup=%d\n", subflow->backup, bkup);
227 	subflow->backup = bkup;
228 
229 	mptcp_event(MPTCP_EVENT_SUB_PRIORITY, mptcp_sk(subflow->conn), sk, GFP_ATOMIC);
230 }
231 
232 /* path manager helpers */
233 
234 bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
235 			      struct mptcp_addr_info *saddr, bool *echo, bool *port)
236 {
237 	int ret = false;
238 
239 	spin_lock_bh(&msk->pm.lock);
240 
241 	/* double check after the lock is acquired */
242 	if (!mptcp_pm_should_add_signal(msk))
243 		goto out_unlock;
244 
245 	*echo = mptcp_pm_should_add_signal_echo(msk);
246 	*port = mptcp_pm_should_add_signal_port(msk);
247 
248 	if (remaining < mptcp_add_addr_len(msk->pm.local.family, *echo, *port))
249 		goto out_unlock;
250 
251 	*saddr = msk->pm.local;
252 	WRITE_ONCE(msk->pm.addr_signal, 0);
253 	ret = true;
254 
255 out_unlock:
256 	spin_unlock_bh(&msk->pm.lock);
257 	return ret;
258 }
259 
260 bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
261 			     u8 *rm_id)
262 {
263 	int ret = false;
264 
265 	spin_lock_bh(&msk->pm.lock);
266 
267 	/* double check after the lock is acquired */
268 	if (!mptcp_pm_should_rm_signal(msk))
269 		goto out_unlock;
270 
271 	if (remaining < TCPOLEN_MPTCP_RM_ADDR_BASE)
272 		goto out_unlock;
273 
274 	*rm_id = msk->pm.rm_id;
275 	WRITE_ONCE(msk->pm.addr_signal, 0);
276 	ret = true;
277 
278 out_unlock:
279 	spin_unlock_bh(&msk->pm.lock);
280 	return ret;
281 }
282 
283 int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
284 {
285 	return mptcp_pm_nl_get_local_id(msk, skc);
286 }
287 
288 void mptcp_pm_data_init(struct mptcp_sock *msk)
289 {
290 	msk->pm.add_addr_signaled = 0;
291 	msk->pm.add_addr_accepted = 0;
292 	msk->pm.local_addr_used = 0;
293 	msk->pm.subflows = 0;
294 	msk->pm.rm_id = 0;
295 	WRITE_ONCE(msk->pm.work_pending, false);
296 	WRITE_ONCE(msk->pm.addr_signal, 0);
297 	WRITE_ONCE(msk->pm.accept_addr, false);
298 	WRITE_ONCE(msk->pm.accept_subflow, false);
299 	msk->pm.status = 0;
300 
301 	spin_lock_init(&msk->pm.lock);
302 	INIT_LIST_HEAD(&msk->pm.anno_list);
303 
304 	mptcp_pm_nl_data_init(msk);
305 }
306 
307 void __init mptcp_pm_init(void)
308 {
309 	mptcp_pm_nl_init();
310 }
311