Skip to content

Commit c83a47e

Browse files
Florian Westphaldavem330
authored andcommitted
mptcp: subflow: add mptcp_subflow_init_cookie_req helper
Will be used to initialize the mptcp request socket when a MP_CAPABLE request was handled in syncookie mode, i.e. when a TCP ACK containing a MP_CAPABLE option is a valid syncookie value. Normally (non-cookie case), MPTCP will generate a unique 32 bit connection ID and stores it in the MPTCP token storage to be able to retrieve the mptcp socket for subflow joining. In syncookie case, we do not want to store any state, so just generate the unique ID and use it in the reply. This means there is a small window where another connection could generate the same token. When Cookie ACK comes back, we check that the token has not been registered in the mean time. If it was, the connection needs to fall back to TCP. Changes in v2: - use req->syncookie instead of passing 'want_cookie' arg to ->init_req() (Eric Dumazet) Signed-off-by: Florian Westphal <[email protected]> Reviewed-by: Mat Martineau <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 08b8d08 commit c83a47e

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

include/net/mptcp.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
131131
}
132132

133133
void mptcp_seq_show(struct seq_file *seq);
134+
int mptcp_subflow_init_cookie_req(struct request_sock *req,
135+
const struct sock *sk_listener,
136+
struct sk_buff *skb);
134137
#else
135138

136139
static inline void mptcp_init(void)
@@ -200,6 +203,13 @@ static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
200203

201204
static inline void mptcp_space(const struct sock *ssk, int *s, int *fs) { }
202205
static inline void mptcp_seq_show(struct seq_file *seq) { }
206+
207+
static inline int mptcp_subflow_init_cookie_req(struct request_sock *req,
208+
const struct sock *sk_listener,
209+
struct sk_buff *skb)
210+
{
211+
return 0; /* TCP fallback */
212+
}
203213
#endif /* CONFIG_MPTCP */
204214

205215
#if IS_ENABLED(CONFIG_MPTCP_IPV6)

net/mptcp/protocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ void mptcp_token_destroy_request(struct request_sock *req);
400400
int mptcp_token_new_connect(struct sock *sk);
401401
void mptcp_token_accept(struct mptcp_subflow_request_sock *r,
402402
struct mptcp_sock *msk);
403+
bool mptcp_token_exists(u32 token);
403404
struct mptcp_sock *mptcp_token_get_sock(u32 token);
404405
struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
405406
long *s_num);

net/mptcp/subflow.c

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,31 @@ static void subflow_init_req(struct request_sock *req,
140140
if (mp_opt.mp_capable && listener->request_mptcp) {
141141
int err, retries = 4;
142142

143+
subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
143144
again:
144145
do {
145146
get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key));
146147
} while (subflow_req->local_key == 0);
147148

149+
if (unlikely(req->syncookie)) {
150+
mptcp_crypto_key_sha(subflow_req->local_key,
151+
&subflow_req->token,
152+
&subflow_req->idsn);
153+
if (mptcp_token_exists(subflow_req->token)) {
154+
if (retries-- > 0)
155+
goto again;
156+
} else {
157+
subflow_req->mp_capable = 1;
158+
}
159+
return;
160+
}
161+
148162
err = mptcp_token_new_request(req);
149163
if (err == 0)
150164
subflow_req->mp_capable = 1;
151165
else if (retries-- > 0)
152166
goto again;
153167

154-
subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
155168
} else if (mp_opt.mp_join && listener->request_mptcp) {
156169
subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
157170
subflow_req->mp_join = 1;
@@ -165,6 +178,41 @@ static void subflow_init_req(struct request_sock *req,
165178
}
166179
}
167180

181+
int mptcp_subflow_init_cookie_req(struct request_sock *req,
182+
const struct sock *sk_listener,
183+
struct sk_buff *skb)
184+
{
185+
struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
186+
struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
187+
struct mptcp_options_received mp_opt;
188+
int err;
189+
190+
err = __subflow_init_req(req, sk_listener);
191+
if (err)
192+
return err;
193+
194+
mptcp_get_options(skb, &mp_opt);
195+
196+
if (mp_opt.mp_capable && mp_opt.mp_join)
197+
return -EINVAL;
198+
199+
if (mp_opt.mp_capable && listener->request_mptcp) {
200+
if (mp_opt.sndr_key == 0)
201+
return -EINVAL;
202+
203+
subflow_req->local_key = mp_opt.rcvr_key;
204+
err = mptcp_token_new_request(req);
205+
if (err)
206+
return err;
207+
208+
subflow_req->mp_capable = 1;
209+
subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
210+
}
211+
212+
return 0;
213+
}
214+
EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req);
215+
168216
static void subflow_v4_init_req(struct request_sock *req,
169217
const struct sock *sk_listener,
170218
struct sk_buff *skb)

net/mptcp/token.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,32 @@ void mptcp_token_accept(struct mptcp_subflow_request_sock *req,
204204
spin_unlock_bh(&bucket->lock);
205205
}
206206

207+
bool mptcp_token_exists(u32 token)
208+
{
209+
struct hlist_nulls_node *pos;
210+
struct token_bucket *bucket;
211+
struct mptcp_sock *msk;
212+
struct sock *sk;
213+
214+
rcu_read_lock();
215+
bucket = token_bucket(token);
216+
217+
again:
218+
sk_nulls_for_each_rcu(sk, pos, &bucket->msk_chain) {
219+
msk = mptcp_sk(sk);
220+
if (READ_ONCE(msk->token) == token)
221+
goto found;
222+
}
223+
if (get_nulls_value(pos) != (token & token_mask))
224+
goto again;
225+
226+
rcu_read_unlock();
227+
return false;
228+
found:
229+
rcu_read_unlock();
230+
return true;
231+
}
232+
207233
/**
208234
* mptcp_token_get_sock - retrieve mptcp connection sock using its token
209235
* @token: token of the mptcp connection to retrieve

0 commit comments

Comments
 (0)