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
mptcp_pm_announce_addr(struct mptcp_sock * msk,const struct mptcp_addr_info * addr,bool echo)15 int mptcp_pm_announce_addr(struct mptcp_sock *msk,
16 const struct mptcp_addr_info *addr,
17 bool echo)
18 {
19 pr_debug("msk=%p, local_id=%d", msk, addr->id);
20
21 msk->pm.local = *addr;
22 WRITE_ONCE(msk->pm.add_addr_echo, echo);
23 WRITE_ONCE(msk->pm.add_addr_signal, true);
24 return 0;
25 }
26
mptcp_pm_remove_addr(struct mptcp_sock * msk,u8 local_id)27 int mptcp_pm_remove_addr(struct mptcp_sock *msk, u8 local_id)
28 {
29 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;
34 }
35
mptcp_pm_remove_subflow(struct mptcp_sock * msk,u8 local_id)36 int mptcp_pm_remove_subflow(struct mptcp_sock *msk, u8 local_id)
37 {
38 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;
44 }
45
46 /* path manager event handlers */
47
mptcp_pm_new_connection(struct mptcp_sock * msk,int server_side)48 void 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
mptcp_pm_allow_new_subflow(struct mptcp_sock * msk)57 bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk)
58 {
59 struct mptcp_pm_data *pm = &msk->pm;
60 int ret = 0;
61
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);
70 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 }
75 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 */
mptcp_pm_schedule_work(struct mptcp_sock * msk,enum mptcp_pm_status new_status)83 static 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);
92 if (schedule_work(&msk->work))
93 sock_hold((struct sock *)msk);
94 return true;
95 }
96
mptcp_pm_fully_established(struct mptcp_sock * msk)97 void mptcp_pm_fully_established(struct mptcp_sock *msk)
98 {
99 struct mptcp_pm_data *pm = &msk->pm;
100
101 pr_debug("msk=%p", msk);
102
103 /* try to avoid acquiring the lock below */
104 if (!READ_ONCE(pm->work_pending))
105 return;
106
107 spin_lock_bh(&pm->lock);
108
109 if (READ_ONCE(pm->work_pending))
110 mptcp_pm_schedule_work(msk, MPTCP_PM_ESTABLISHED);
111
112 spin_unlock_bh(&pm->lock);
113 }
114
mptcp_pm_connection_closed(struct mptcp_sock * msk)115 void mptcp_pm_connection_closed(struct mptcp_sock *msk)
116 {
117 pr_debug("msk=%p", msk);
118 }
119
mptcp_pm_subflow_established(struct mptcp_sock * msk,struct mptcp_subflow_context * subflow)120 void mptcp_pm_subflow_established(struct mptcp_sock *msk,
121 struct mptcp_subflow_context *subflow)
122 {
123 struct mptcp_pm_data *pm = &msk->pm;
124
125 pr_debug("msk=%p", msk);
126
127 if (!READ_ONCE(pm->work_pending))
128 return;
129
130 spin_lock_bh(&pm->lock);
131
132 if (READ_ONCE(pm->work_pending))
133 mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
134
135 spin_unlock_bh(&pm->lock);
136 }
137
mptcp_pm_subflow_closed(struct mptcp_sock * msk,u8 id)138 void mptcp_pm_subflow_closed(struct mptcp_sock *msk, u8 id)
139 {
140 pr_debug("msk=%p", msk);
141 }
142
mptcp_pm_add_addr_received(struct mptcp_sock * msk,const struct mptcp_addr_info * addr)143 void mptcp_pm_add_addr_received(struct mptcp_sock *msk,
144 const struct mptcp_addr_info *addr)
145 {
146 struct mptcp_pm_data *pm = &msk->pm;
147
148 pr_debug("msk=%p remote_id=%d accept=%d", msk, addr->id,
149 READ_ONCE(pm->accept_addr));
150
151 spin_lock_bh(&pm->lock);
152
153 if (!READ_ONCE(pm->accept_addr))
154 mptcp_pm_announce_addr(msk, addr, true);
155 else if (mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_RECEIVED))
156 pm->remote = *addr;
157
158 spin_unlock_bh(&pm->lock);
159 }
160
mptcp_pm_rm_addr_received(struct mptcp_sock * msk,u8 rm_id)161 void mptcp_pm_rm_addr_received(struct mptcp_sock *msk, u8 rm_id)
162 {
163 struct mptcp_pm_data *pm = &msk->pm;
164
165 pr_debug("msk=%p remote_id=%d", msk, rm_id);
166
167 spin_lock_bh(&pm->lock);
168 mptcp_pm_schedule_work(msk, MPTCP_PM_RM_ADDR_RECEIVED);
169 pm->rm_id = rm_id;
170 spin_unlock_bh(&pm->lock);
171 }
172
173 /* path manager helpers */
174
mptcp_pm_add_addr_signal(struct mptcp_sock * msk,unsigned int remaining,struct mptcp_addr_info * saddr,bool * echo)175 bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
176 struct mptcp_addr_info *saddr, bool *echo)
177 {
178 int ret = false;
179
180 spin_lock_bh(&msk->pm.lock);
181
182 /* double check after the lock is acquired */
183 if (!mptcp_pm_should_add_signal(msk))
184 goto out_unlock;
185
186 *echo = READ_ONCE(msk->pm.add_addr_echo);
187
188 if (remaining < mptcp_add_addr_len(msk->pm.local.family, *echo))
189 goto out_unlock;
190
191 *saddr = msk->pm.local;
192 WRITE_ONCE(msk->pm.add_addr_signal, false);
193 ret = true;
194
195 out_unlock:
196 spin_unlock_bh(&msk->pm.lock);
197 return ret;
198 }
199
mptcp_pm_rm_addr_signal(struct mptcp_sock * msk,unsigned int remaining,u8 * rm_id)200 bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
201 u8 *rm_id)
202 {
203 int ret = false;
204
205 spin_lock_bh(&msk->pm.lock);
206
207 /* double check after the lock is acquired */
208 if (!mptcp_pm_should_rm_signal(msk))
209 goto out_unlock;
210
211 if (remaining < TCPOLEN_MPTCP_RM_ADDR_BASE)
212 goto out_unlock;
213
214 *rm_id = msk->pm.rm_id;
215 WRITE_ONCE(msk->pm.rm_addr_signal, false);
216 ret = true;
217
218 out_unlock:
219 spin_unlock_bh(&msk->pm.lock);
220 return ret;
221 }
222
mptcp_pm_get_local_id(struct mptcp_sock * msk,struct sock_common * skc)223 int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
224 {
225 return mptcp_pm_nl_get_local_id(msk, skc);
226 }
227
mptcp_pm_data_init(struct mptcp_sock * msk)228 void mptcp_pm_data_init(struct mptcp_sock *msk)
229 {
230 msk->pm.add_addr_signaled = 0;
231 msk->pm.add_addr_accepted = 0;
232 msk->pm.local_addr_used = 0;
233 msk->pm.subflows = 0;
234 msk->pm.rm_id = 0;
235 WRITE_ONCE(msk->pm.work_pending, false);
236 WRITE_ONCE(msk->pm.add_addr_signal, false);
237 WRITE_ONCE(msk->pm.rm_addr_signal, false);
238 WRITE_ONCE(msk->pm.accept_addr, false);
239 WRITE_ONCE(msk->pm.accept_subflow, false);
240 WRITE_ONCE(msk->pm.add_addr_echo, false);
241 msk->pm.status = 0;
242
243 spin_lock_init(&msk->pm.lock);
244 INIT_LIST_HEAD(&msk->pm.anno_list);
245
246 mptcp_pm_nl_data_init(msk);
247 }
248
mptcp_pm_init(void)249 void __init mptcp_pm_init(void)
250 {
251 mptcp_pm_nl_init();
252 }
253