Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-2.0
2 : : /* Multipath TCP
3 : : *
4 : : * Copyright (c) 2017 - 2019, Intel Corporation.
5 : : */
6 : :
7 : : #define pr_fmt(fmt) "MPTCP: " fmt
8 : :
9 : : #include <linux/kernel.h>
10 : : #include <linux/module.h>
11 : : #include <linux/netdevice.h>
12 : : #include <crypto/sha2.h>
13 : : #include <crypto/utils.h>
14 : : #include <net/sock.h>
15 : : #include <net/inet_common.h>
16 : : #include <net/inet_hashtables.h>
17 : : #include <net/protocol.h>
18 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
19 : : #include <net/ip6_route.h>
20 : : #include <net/transp_v6.h>
21 : : #endif
22 : : #include <net/mptcp.h>
23 : :
24 : : #include "protocol.h"
25 : : #include "mib.h"
26 : :
27 : : #include <trace/events/mptcp.h>
28 : : #include <trace/events/sock.h>
29 : :
30 : : static void mptcp_subflow_ops_undo_override(struct sock *ssk);
31 : :
32 : 0 : static void SUBFLOW_REQ_INC_STATS(struct request_sock *req,
33 : : enum linux_mptcp_mib_field field)
34 : : {
35 [ - - - - : 93 : MPTCP_INC_STATS(sock_net(req_to_sk(req)), field);
- - - - +
- - - + -
+ - - - -
- - - - -
- - - - -
- - - ]
36 : : }
37 : :
38 : 1847 : static void subflow_req_destructor(struct request_sock *req)
39 : : {
40 : 1847 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
41 : :
42 [ - + ]: 1847 : pr_debug("subflow_req=%p\n", subflow_req);
43 : :
44 [ + + ]: 1847 : if (subflow_req->msk)
45 : 30 : sock_put((struct sock *)subflow_req->msk);
46 : :
47 : 1847 : mptcp_token_destroy_request(req);
48 : 1847 : }
49 : :
50 : 2030 : static void subflow_generate_hmac(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
51 : : void *hmac)
52 : : {
53 : 2030 : u8 msg[8];
54 : :
55 : 2030 : put_unaligned_be32(nonce1, &msg[0]);
56 : 2030 : put_unaligned_be32(nonce2, &msg[4]);
57 : :
58 : 2030 : mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
59 : 2030 : }
60 : :
61 : 542 : static bool mptcp_can_accept_new_subflow(const struct mptcp_sock *msk)
62 : : {
63 [ + - + + ]: 542 : return mptcp_is_fully_established((void *)msk) &&
64 [ + + - - ]: 86 : ((mptcp_pm_is_userspace(msk) &&
65 : 26 : mptcp_userspace_pm_active(msk)) ||
66 [ + + + + ]: 520 : READ_ONCE(msk->pm.accept_subflow));
67 : : }
68 : :
69 : : /* validate received token and create truncated hmac and nonce for SYN-ACK */
70 : 522 : static void subflow_req_create_thmac(struct mptcp_subflow_request_sock *subflow_req)
71 : : {
72 : 522 : struct mptcp_sock *msk = subflow_req->msk;
73 : 522 : u8 hmac[SHA256_DIGEST_SIZE];
74 : :
75 : 522 : get_random_bytes(&subflow_req->local_nonce, sizeof(u32));
76 : :
77 : 522 : subflow_generate_hmac(READ_ONCE(msk->local_key),
78 : 522 : READ_ONCE(msk->remote_key),
79 : : subflow_req->local_nonce,
80 : : subflow_req->remote_nonce, hmac);
81 : :
82 : 522 : subflow_req->thmac = get_unaligned_be64(hmac);
83 : 522 : }
84 : :
85 : 528 : static struct mptcp_sock *subflow_token_join_request(struct request_sock *req)
86 : : {
87 : 528 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
88 : 528 : struct mptcp_sock *msk;
89 : 528 : int local_id;
90 : :
91 : 528 : msk = mptcp_token_get_sock(sock_net(req_to_sk(req)), subflow_req->token);
92 [ + + ]: 528 : if (!msk) {
93 [ + - ]: 6 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN);
94 : 6 : return NULL;
95 : : }
96 : :
97 : 522 : local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req);
98 [ - + ]: 522 : if (local_id < 0) {
99 : 0 : sock_put((struct sock *)msk);
100 : 0 : return NULL;
101 : : }
102 : 522 : subflow_req->local_id = local_id;
103 : 522 : subflow_req->request_bkup = mptcp_pm_is_backup(msk, (struct sock_common *)req);
104 : :
105 : 522 : return msk;
106 : : }
107 : :
108 : 1847 : static void subflow_init_req(struct request_sock *req, const struct sock *sk_listener)
109 : : {
110 : 1847 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
111 : :
112 : 1847 : subflow_req->mp_capable = 0;
113 : 1847 : subflow_req->mp_join = 0;
114 : 1847 : subflow_req->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk_listener));
115 : 1847 : subflow_req->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk_listener));
116 : 1847 : subflow_req->msk = NULL;
117 : 1847 : mptcp_token_init_request(req);
118 : 1847 : }
119 : :
120 : 120 : static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk)
121 : : {
122 : 1034 : return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport;
123 : : }
124 : :
125 : 18 : static void subflow_add_reset_reason(struct sk_buff *skb, u8 reason)
126 : : {
127 : 18 : struct mptcp_ext *mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
128 : :
129 [ + - ]: 18 : if (mpext) {
130 : 18 : memset(mpext, 0, sizeof(*mpext));
131 : 18 : mpext->reset_reason = reason;
132 : : }
133 : 18 : }
134 : :
135 : 2 : static int subflow_reset_req_endp(struct request_sock *req, struct sk_buff *skb)
136 : : {
137 [ + - ]: 2 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEENDPATTEMPT);
138 : 2 : subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
139 : 2 : return -EPERM;
140 : : }
141 : :
142 : : /* Init mptcp request socket.
143 : : *
144 : : * Returns an error code if a JOIN has failed and a TCP reset
145 : : * should be sent.
146 : : */
147 : 1798 : static int subflow_check_req(struct request_sock *req,
148 : : const struct sock *sk_listener,
149 : : struct sk_buff *skb)
150 : : {
151 [ - + ]: 1798 : struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
152 : 1798 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
153 : 1798 : struct mptcp_options_received mp_opt;
154 : 1798 : bool opt_mp_capable, opt_mp_join;
155 : :
156 [ - + ]: 1798 : pr_debug("subflow_req=%p, listener=%p\n", subflow_req, listener);
157 : :
158 : : #ifdef CONFIG_TCP_MD5SIG
159 : : /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
160 : : * TCP option space.
161 : : */
162 : : if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info)) {
163 : : subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
164 : : return -EINVAL;
165 : : }
166 : : #endif
167 : :
168 : 1798 : mptcp_get_options(skb, &mp_opt);
169 : :
170 : 1798 : opt_mp_capable = !!(mp_opt.suboptions & OPTION_MPTCP_MPC_SYN);
171 : 1798 : opt_mp_join = !!(mp_opt.suboptions & OPTION_MPTCP_MPJ_SYN);
172 [ + + ]: 1798 : if (opt_mp_capable) {
173 [ + - ]: 1194 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE);
174 : :
175 [ + + + + ]: 1194 : if (unlikely(listener->pm_listener))
176 : 2 : return subflow_reset_req_endp(req, skb);
177 [ - + ]: 1192 : if (opt_mp_join)
178 : : return 0;
179 [ + + ]: 604 : } else if (opt_mp_join) {
180 [ + - ]: 528 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX);
181 : :
182 [ + + ]: 528 : if (mp_opt.backup)
183 [ + - ]: 20 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNBACKUPRX);
184 [ - + - + ]: 76 : } else if (unlikely(listener->pm_listener)) {
185 : 0 : return subflow_reset_req_endp(req, skb);
186 : : }
187 : :
188 [ + + + - ]: 1796 : if (opt_mp_capable && listener->request_mptcp) {
189 : 1192 : int err, retries = MPTCP_TOKEN_MAX_RETRIES;
190 : :
191 : 1192 : subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
192 : : again:
193 : 1192 : do {
194 : 1192 : get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key));
195 [ - + ]: 1192 : } while (subflow_req->local_key == 0);
196 : :
197 [ + + ]: 1192 : if (unlikely(req->syncookie)) {
198 : 28 : mptcp_crypto_key_sha(subflow_req->local_key,
199 : : &subflow_req->token,
200 : : &subflow_req->idsn);
201 [ - + ]: 28 : if (mptcp_token_exists(subflow_req->token)) {
202 [ # # ]: 0 : if (retries-- > 0)
203 : 0 : goto again;
204 [ # # ]: 0 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
205 : : } else {
206 : 28 : subflow_req->mp_capable = 1;
207 : : }
208 : 28 : return 0;
209 : : }
210 : :
211 : 1164 : err = mptcp_token_new_request(req);
212 [ + - ]: 1164 : if (err == 0)
213 : 1164 : subflow_req->mp_capable = 1;
214 [ # # ]: 0 : else if (retries-- > 0)
215 : 0 : goto again;
216 : : else
217 [ # # ]: 0 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
218 : :
219 [ + + - + ]: 604 : } else if (opt_mp_join && listener->request_mptcp) {
220 : 528 : subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
221 : 528 : subflow_req->mp_join = 1;
222 : 528 : subflow_req->backup = mp_opt.backup;
223 : 528 : subflow_req->remote_id = mp_opt.join_id;
224 : 528 : subflow_req->token = mp_opt.token;
225 : 528 : subflow_req->remote_nonce = mp_opt.nonce;
226 : 528 : subflow_req->msk = subflow_token_join_request(req);
227 : :
228 : : /* Can't fall back to TCP in this case. */
229 [ + + ]: 528 : if (!subflow_req->msk) {
230 : 6 : subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
231 : 6 : return -EPERM;
232 : : }
233 : :
234 [ + + ]: 522 : if (subflow_use_different_sport(subflow_req->msk, sk_listener)) {
235 [ - + ]: 24 : pr_debug("syn inet_sport=%d %d\n",
236 : : ntohs(inet_sk(sk_listener)->inet_sport),
237 : : ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport));
238 [ - + ]: 24 : if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener)) {
239 [ # # ]: 0 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX);
240 : 0 : subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
241 : 0 : return -EPERM;
242 : : }
243 [ + - ]: 24 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX);
244 : : }
245 : :
246 : 522 : subflow_req_create_thmac(subflow_req);
247 : :
248 [ + + ]: 522 : if (unlikely(req->syncookie)) {
249 [ + + ]: 22 : if (!mptcp_can_accept_new_subflow(subflow_req->msk)) {
250 [ + - ]: 2 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINREJECTED);
251 : 2 : subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
252 : 2 : return -EPERM;
253 : : }
254 : :
255 : 20 : subflow_init_req_cookie_join_save(subflow_req, skb);
256 : : }
257 : :
258 [ + - ]: 520 : pr_debug("token=%u, remote_nonce=%u msk=%p\n", subflow_req->token,
259 : : subflow_req->remote_nonce, subflow_req->msk);
260 : : }
261 : :
262 : : return 0;
263 : : }
264 : :
265 : 49 : int mptcp_subflow_init_cookie_req(struct request_sock *req,
266 : : const struct sock *sk_listener,
267 : : struct sk_buff *skb)
268 : : {
269 : 49 : struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
270 : 49 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
271 : 49 : struct mptcp_options_received mp_opt;
272 : 49 : bool opt_mp_capable, opt_mp_join;
273 : 49 : int err;
274 : :
275 : 49 : subflow_init_req(req, sk_listener);
276 : 49 : mptcp_get_options(skb, &mp_opt);
277 : :
278 : 49 : opt_mp_capable = !!(mp_opt.suboptions & OPTION_MPTCP_MPC_ACK);
279 : 49 : opt_mp_join = !!(mp_opt.suboptions & OPTION_MPTCP_MPJ_ACK);
280 [ + - ]: 49 : if (opt_mp_capable && opt_mp_join)
281 : : return -EINVAL;
282 : :
283 [ + + + - ]: 49 : if (opt_mp_capable && listener->request_mptcp) {
284 [ + - ]: 28 : if (mp_opt.sndr_key == 0)
285 : : return -EINVAL;
286 : :
287 : 28 : subflow_req->local_key = mp_opt.rcvr_key;
288 : 28 : err = mptcp_token_new_request(req);
289 [ + - ]: 28 : if (err)
290 : : return err;
291 : :
292 : 28 : subflow_req->mp_capable = 1;
293 : 28 : subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
294 [ + + - + ]: 21 : } else if (opt_mp_join && listener->request_mptcp) {
295 [ + - ]: 20 : if (!mptcp_token_join_cookie_init_state(subflow_req, skb))
296 : : return -EINVAL;
297 : :
298 : 20 : subflow_req->mp_join = 1;
299 : 20 : subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
300 : : }
301 : :
302 : : return 0;
303 : : }
304 : : EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req);
305 : :
306 : 16 : static enum sk_rst_reason mptcp_get_rst_reason(const struct sk_buff *skb)
307 : : {
308 [ + - ]: 16 : const struct mptcp_ext *mpext = mptcp_get_ext(skb);
309 : :
310 [ + - ]: 16 : if (!mpext)
311 : : return SK_RST_REASON_NOT_SPECIFIED;
312 : :
313 [ + - ]: 16 : return sk_rst_convert_mptcp_reason(mpext->reset_reason);
314 : : }
315 : :
316 : 1492 : static struct dst_entry *subflow_v4_route_req(const struct sock *sk,
317 : : struct sk_buff *skb,
318 : : struct flowi *fl,
319 : : struct request_sock *req,
320 : : u32 tw_isn)
321 : : {
322 : 1492 : struct dst_entry *dst;
323 : 1492 : int err;
324 : :
325 : 1492 : tcp_rsk(req)->is_mptcp = 1;
326 : 1492 : subflow_init_req(req, sk);
327 : :
328 : 1492 : dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req, tw_isn);
329 [ - + ]: 1492 : if (!dst)
330 : : return NULL;
331 : :
332 : 1492 : err = subflow_check_req(req, sk, skb);
333 [ + + ]: 1492 : if (err == 0)
334 : : return dst;
335 : :
336 : 8 : dst_release(dst);
337 [ + + ]: 8 : if (!req->syncookie)
338 : 6 : tcp_request_sock_ops.send_reset(sk, skb,
339 : : mptcp_get_rst_reason(skb));
340 : : return NULL;
341 : : }
342 : :
343 : 1792 : static void subflow_prep_synack(const struct sock *sk, struct request_sock *req,
344 : : struct tcp_fastopen_cookie *foc,
345 : : enum tcp_synack_type synack_type)
346 : : {
347 [ + + ]: 1792 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
348 : 1792 : struct inet_request_sock *ireq = inet_rsk(req);
349 : :
350 : : /* clear tstamp_ok, as needed depending on cookie */
351 [ + + + + ]: 1792 : if (foc && foc->len > -1)
352 : 20 : ireq->tstamp_ok = 0;
353 : :
354 [ + + ]: 1792 : if (synack_type == TCP_SYNACK_FASTOPEN)
355 : 38 : mptcp_fastopen_subflow_synack_set_params(subflow, req);
356 : 1792 : }
357 : :
358 : 1484 : static int subflow_v4_send_synack(const struct sock *sk, struct dst_entry *dst,
359 : : struct flowi *fl,
360 : : struct request_sock *req,
361 : : struct tcp_fastopen_cookie *foc,
362 : : enum tcp_synack_type synack_type,
363 : : struct sk_buff *syn_skb)
364 : : {
365 : 1484 : subflow_prep_synack(sk, req, foc, synack_type);
366 : :
367 : 1484 : return tcp_request_sock_ipv4_ops.send_synack(sk, dst, fl, req, foc,
368 : : synack_type, syn_skb);
369 : : }
370 : :
371 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
372 : 308 : static int subflow_v6_send_synack(const struct sock *sk, struct dst_entry *dst,
373 : : struct flowi *fl,
374 : : struct request_sock *req,
375 : : struct tcp_fastopen_cookie *foc,
376 : : enum tcp_synack_type synack_type,
377 : : struct sk_buff *syn_skb)
378 : : {
379 : 308 : subflow_prep_synack(sk, req, foc, synack_type);
380 : :
381 : 308 : return tcp_request_sock_ipv6_ops.send_synack(sk, dst, fl, req, foc,
382 : : synack_type, syn_skb);
383 : : }
384 : :
385 : 306 : static struct dst_entry *subflow_v6_route_req(const struct sock *sk,
386 : : struct sk_buff *skb,
387 : : struct flowi *fl,
388 : : struct request_sock *req,
389 : : u32 tw_isn)
390 : : {
391 : 306 : struct dst_entry *dst;
392 : 306 : int err;
393 : :
394 : 306 : tcp_rsk(req)->is_mptcp = 1;
395 : 306 : subflow_init_req(req, sk);
396 : :
397 : 306 : dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req, tw_isn);
398 [ - + ]: 306 : if (!dst)
399 : : return NULL;
400 : :
401 : 306 : err = subflow_check_req(req, sk, skb);
402 [ + + ]: 306 : if (err == 0)
403 : : return dst;
404 : :
405 : 2 : dst_release(dst);
406 [ - + ]: 2 : if (!req->syncookie)
407 : 2 : tcp6_request_sock_ops.send_reset(sk, skb,
408 : : mptcp_get_rst_reason(skb));
409 : : return NULL;
410 : : }
411 : : #endif
412 : :
413 : : /* validate received truncated hmac and create hmac for third ACK */
414 : 494 : static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
415 : : {
416 : 494 : u8 hmac[SHA256_DIGEST_SIZE];
417 : 494 : u64 thmac;
418 : :
419 : 494 : subflow_generate_hmac(subflow->remote_key, subflow->local_key,
420 : : subflow->remote_nonce, subflow->local_nonce,
421 : : hmac);
422 : :
423 [ - + ]: 494 : thmac = get_unaligned_be64(hmac);
424 [ - + ]: 494 : pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n",
425 : : subflow, subflow->token, thmac, subflow->thmac);
426 : :
427 : 494 : return thmac == subflow->thmac;
428 : : }
429 : :
430 : 8 : void mptcp_subflow_reset(struct sock *ssk)
431 : : {
432 [ + - ]: 8 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
433 : 8 : struct sock *sk = subflow->conn;
434 : :
435 : : /* mptcp_mp_fail_no_response() can reach here on an already closed
436 : : * socket
437 : : */
438 [ + - ]: 8 : if (ssk->sk_state == TCP_CLOSE)
439 : : return;
440 : :
441 : : /* must hold: tcp_done() could drop last reference on parent */
442 : 8 : sock_hold(sk);
443 : :
444 : 8 : mptcp_send_active_reset_reason(ssk);
445 : 8 : tcp_done(ssk);
446 [ - + - + ]: 12 : if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags))
447 : 0 : mptcp_schedule_work(sk);
448 : :
449 : 8 : sock_put(sk);
450 : : }
451 : :
452 : 60 : static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk)
453 : : {
454 : 494 : return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport;
455 : : }
456 : :
457 : 1220 : void __mptcp_sync_state(struct sock *sk, int state)
458 : : {
459 : 1220 : struct mptcp_subflow_context *subflow;
460 [ - + ]: 1220 : struct mptcp_sock *msk = mptcp_sk(sk);
461 : 1220 : struct sock *ssk = msk->first;
462 : :
463 [ + + ]: 1220 : subflow = mptcp_subflow_ctx(ssk);
464 [ + + ]: 1220 : __mptcp_propagate_sndbuf(sk, ssk);
465 [ + - ]: 1220 : if (!msk->rcvspace_init)
466 : 1220 : mptcp_rcv_space_init(msk, ssk);
467 : :
468 [ + - ]: 1220 : if (sk->sk_state == TCP_SYN_SENT) {
469 : : /* subflow->idsn is always available is TCP_SYN_SENT state,
470 : : * even for the FASTOPEN scenarios
471 : : */
472 : 1220 : WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
473 : 1220 : WRITE_ONCE(msk->snd_nxt, msk->write_seq);
474 : 1220 : mptcp_set_state(sk, state);
475 : 1220 : sk->sk_state_change(sk);
476 : : }
477 : 1220 : }
478 : :
479 : 3848 : static void subflow_set_remote_key(struct mptcp_sock *msk,
480 : : struct mptcp_subflow_context *subflow,
481 : : const struct mptcp_options_received *mp_opt)
482 : : {
483 : : /* active MPC subflow will reach here multiple times:
484 : : * at subflow_finish_connect() time and at 4th ack time
485 : : */
486 [ + + ]: 3848 : if (subflow->remote_key_valid)
487 : : return;
488 : :
489 : 2286 : subflow->remote_key_valid = 1;
490 : 2286 : subflow->remote_key = mp_opt->sndr_key;
491 : 2286 : mptcp_crypto_key_sha(subflow->remote_key, NULL, &subflow->iasn);
492 : 2286 : subflow->iasn++;
493 : :
494 : 2286 : WRITE_ONCE(msk->remote_key, subflow->remote_key);
495 : 2286 : WRITE_ONCE(msk->ack_seq, subflow->iasn);
496 : 2286 : WRITE_ONCE(msk->can_ack, true);
497 : 2286 : atomic64_set(&msk->rcv_wnd_sent, subflow->iasn);
498 : : }
499 : :
500 : 1220 : static void mptcp_propagate_state(struct sock *sk, struct sock *ssk,
501 : : struct mptcp_subflow_context *subflow,
502 : : const struct mptcp_options_received *mp_opt)
503 : : {
504 [ - + ]: 1220 : struct mptcp_sock *msk = mptcp_sk(sk);
505 : :
506 : 1220 : mptcp_data_lock(sk);
507 [ + + ]: 1220 : if (mp_opt) {
508 : : /* Options are available only in the non fallback cases
509 : : * avoid updating rx path fields otherwise
510 : : */
511 : 1130 : WRITE_ONCE(msk->snd_una, subflow->idsn + 1);
512 [ - + ]: 1130 : WRITE_ONCE(msk->wnd_end, subflow->idsn + 1 + tcp_sk(ssk)->snd_wnd);
513 : 1130 : subflow_set_remote_key(msk, subflow, mp_opt);
514 : : }
515 : :
516 [ + + ]: 1220 : if (!sock_owned_by_user(sk)) {
517 : 339 : __mptcp_sync_state(sk, ssk->sk_state);
518 : : } else {
519 : 881 : msk->pending_state = ssk->sk_state;
520 [ - + - - : 881 : __set_bit(MPTCP_SYNC_STATE, &msk->cb_flags);
- - ]
521 : : }
522 : 1220 : mptcp_data_unlock(sk);
523 : 1220 : }
524 : :
525 : 5077 : static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
526 : : {
527 : 5077 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
528 : 5077 : struct mptcp_options_received mp_opt;
529 : 5077 : struct sock *parent = subflow->conn;
530 : 5077 : struct mptcp_sock *msk;
531 : :
532 : 5077 : subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);
533 : :
534 : : /* be sure no special action on any packet other than syn-ack */
535 [ + + ]: 5077 : if (subflow->conn_finished)
536 : 5077 : return;
537 : :
538 [ - + ]: 1714 : msk = mptcp_sk(parent);
539 : 1714 : subflow->rel_write_seq = 1;
540 : 1714 : subflow->conn_finished = 1;
541 : 1714 : subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
542 [ - + ]: 1714 : pr_debug("subflow=%p synack seq=%x\n", subflow, subflow->ssn_offset);
543 : :
544 : 1714 : mptcp_get_options(skb, &mp_opt);
545 [ + + ]: 1714 : if (subflow->request_mptcp) {
546 [ + + ]: 1202 : if (!(mp_opt.suboptions & OPTION_MPTCP_MPC_SYNACK)) {
547 [ + - ]: 72 : MPTCP_INC_STATS(sock_net(sk),
548 : : MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
549 : 72 : mptcp_do_fallback(sk);
550 [ - + ]: 72 : pr_fallback(msk);
551 : 72 : goto fallback;
552 : : }
553 : :
554 [ + + ]: 1130 : if (mp_opt.suboptions & OPTION_MPTCP_CSUMREQD)
555 : 8 : WRITE_ONCE(msk->csum_enabled, true);
556 [ + + ]: 1130 : if (mp_opt.deny_join_id0)
557 : 6 : WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
558 : 1130 : subflow->mp_capable = 1;
559 [ + - ]: 1130 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK);
560 : 1130 : mptcp_finish_connect(sk);
561 : 1130 : mptcp_active_enable(parent);
562 : 1130 : mptcp_propagate_state(parent, sk, subflow, &mp_opt);
563 [ + + ]: 512 : } else if (subflow->request_join) {
564 : 494 : u8 hmac[SHA256_DIGEST_SIZE];
565 : :
566 [ - + ]: 494 : if (!(mp_opt.suboptions & OPTION_MPTCP_MPJ_SYNACK)) {
567 : 0 : subflow->reset_reason = MPTCP_RST_EMPTCP;
568 : 0 : goto do_reset;
569 : : }
570 : :
571 : 494 : subflow->backup = mp_opt.backup;
572 : 494 : subflow->thmac = mp_opt.thmac;
573 : 494 : subflow->remote_nonce = mp_opt.nonce;
574 : 494 : WRITE_ONCE(subflow->remote_id, mp_opt.join_id);
575 [ - + ]: 494 : pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u backup=%d\n",
576 : : subflow, subflow->thmac, subflow->remote_nonce,
577 : : subflow->backup);
578 : :
579 [ - + ]: 494 : if (!subflow_thmac_valid(subflow)) {
580 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC);
581 : 0 : subflow->reset_reason = MPTCP_RST_EMPTCP;
582 : 0 : goto do_reset;
583 : : }
584 : :
585 [ - + ]: 494 : if (!mptcp_finish_join(sk))
586 : 0 : goto do_reset;
587 : :
588 : 494 : subflow_generate_hmac(subflow->local_key, subflow->remote_key,
589 : : subflow->local_nonce,
590 : : subflow->remote_nonce,
591 : : hmac);
592 : 494 : memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
593 : :
594 : 494 : subflow->mp_join = 1;
595 [ + - ]: 494 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
596 : :
597 [ + + ]: 494 : if (subflow->backup)
598 [ + - ]: 6 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKBACKUPRX);
599 : :
600 [ + + ]: 494 : if (subflow_use_different_dport(msk, sk)) {
601 [ - + ]: 24 : pr_debug("synack inet_dport=%d %d\n",
602 : : ntohs(inet_sk(sk)->inet_dport),
603 : : ntohs(inet_sk(parent)->inet_dport));
604 [ + - ]: 84 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX);
605 : : }
606 [ + - ]: 18 : } else if (mptcp_check_fallback(sk)) {
607 : : /* It looks like MPTCP is blocked, while TCP is not */
608 [ + + ]: 18 : if (subflow->mpc_drop)
609 : 6 : mptcp_active_disable(parent);
610 : 12 : fallback:
611 : 90 : mptcp_propagate_state(parent, sk, subflow, NULL);
612 : : }
613 : : return;
614 : :
615 : 0 : do_reset:
616 : 0 : subflow->reset_transient = 0;
617 : 0 : mptcp_subflow_reset(sk);
618 : : }
619 : :
620 : 1162 : static void subflow_set_local_id(struct mptcp_subflow_context *subflow, int local_id)
621 : : {
622 [ - + ]: 1096 : WARN_ON_ONCE(local_id < 0 || local_id > 255);
623 : 2282 : WRITE_ONCE(subflow->local_id, local_id);
624 : 2282 : }
625 : :
626 : 6932 : static int subflow_chk_local_id(struct sock *sk)
627 : : {
628 [ - + ]: 6932 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
629 [ - + ]: 6932 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
630 : 6932 : int err;
631 : :
632 [ + + ]: 6932 : if (likely(subflow->local_id >= 0))
633 : : return 0;
634 : :
635 : 258 : err = mptcp_pm_get_local_id(msk, (struct sock_common *)sk);
636 [ + - ]: 258 : if (err < 0)
637 : : return err;
638 : :
639 : 258 : subflow_set_local_id(subflow, err);
640 : 258 : subflow->request_bkup = mptcp_pm_is_backup(msk, (struct sock_common *)sk);
641 : :
642 : 258 : return 0;
643 : : }
644 : :
645 : 5341 : static int subflow_rebuild_header(struct sock *sk)
646 : : {
647 : 5341 : int err = subflow_chk_local_id(sk);
648 : :
649 [ + - ]: 5341 : if (unlikely(err < 0))
650 : : return err;
651 : :
652 : 5341 : return inet_sk_rebuild_header(sk);
653 : : }
654 : :
655 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
656 : 1591 : static int subflow_v6_rebuild_header(struct sock *sk)
657 : : {
658 : 1591 : int err = subflow_chk_local_id(sk);
659 : :
660 [ + - ]: 1591 : if (unlikely(err < 0))
661 : : return err;
662 : :
663 : 1591 : return inet6_sk_rebuild_header(sk);
664 : : }
665 : : #endif
666 : :
667 : : static struct request_sock_ops mptcp_subflow_v4_request_sock_ops __ro_after_init;
668 : : static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops __ro_after_init;
669 : :
670 : 1492 : static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
671 : : {
672 [ - + ]: 1492 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
673 : :
674 [ - + ]: 1492 : pr_debug("subflow=%p\n", subflow);
675 : :
676 : : /* Never answer to SYNs sent to broadcast or multicast */
677 [ - + ]: 1492 : if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
678 : 0 : goto drop;
679 : :
680 : 1492 : return tcp_conn_request(&mptcp_subflow_v4_request_sock_ops,
681 : : &subflow_request_sock_ipv4_ops,
682 : : sk, skb);
683 : 0 : drop:
684 : 0 : tcp_listendrop(sk);
685 : 0 : return 0;
686 : : }
687 : :
688 : 1533 : static void subflow_v4_req_destructor(struct request_sock *req)
689 : : {
690 : 1533 : subflow_req_destructor(req);
691 : 1533 : tcp_request_sock_ops.destructor(req);
692 : 1533 : }
693 : :
694 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
695 : : static struct request_sock_ops mptcp_subflow_v6_request_sock_ops __ro_after_init;
696 : : static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops __ro_after_init;
697 : : static struct inet_connection_sock_af_ops subflow_v6_specific __ro_after_init;
698 : : static struct inet_connection_sock_af_ops subflow_v6m_specific __ro_after_init;
699 : : static struct proto tcpv6_prot_override __ro_after_init;
700 : :
701 : 948 : static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
702 : : {
703 [ - + ]: 948 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
704 : :
705 [ - + ]: 948 : pr_debug("subflow=%p\n", subflow);
706 : :
707 [ + + ]: 948 : if (skb->protocol == htons(ETH_P_IP))
708 : 642 : return subflow_v4_conn_request(sk, skb);
709 : :
710 [ - + ]: 306 : if (!ipv6_unicast_destination(skb))
711 : 0 : goto drop;
712 : :
713 [ - + ]: 306 : if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) {
714 : 0 : __IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS);
715 : 0 : return 0;
716 : : }
717 : :
718 : 306 : return tcp_conn_request(&mptcp_subflow_v6_request_sock_ops,
719 : : &subflow_request_sock_ipv6_ops, sk, skb);
720 : :
721 : 0 : drop:
722 : 0 : tcp_listendrop(sk);
723 : 0 : return 0; /* don't send reset */
724 : : }
725 : :
726 : 314 : static void subflow_v6_req_destructor(struct request_sock *req)
727 : : {
728 : 314 : subflow_req_destructor(req);
729 : 314 : tcp6_request_sock_ops.destructor(req);
730 : 314 : }
731 : : #endif
732 : :
733 : 49 : struct request_sock *mptcp_subflow_reqsk_alloc(const struct request_sock_ops *ops,
734 : : struct sock *sk_listener,
735 : : bool attach_listener)
736 : : {
737 [ + + ]: 49 : if (ops->family == AF_INET)
738 : : ops = &mptcp_subflow_v4_request_sock_ops;
739 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
740 [ + - ]: 8 : else if (ops->family == AF_INET6)
741 : 8 : ops = &mptcp_subflow_v6_request_sock_ops;
742 : : #endif
743 : :
744 : 49 : return inet_reqsk_alloc(ops, sk_listener, attach_listener);
745 : : }
746 : : EXPORT_SYMBOL(mptcp_subflow_reqsk_alloc);
747 : :
748 : : /* validate hmac received in third ACK */
749 : 520 : static bool subflow_hmac_valid(const struct mptcp_subflow_request_sock *subflow_req,
750 : : const struct mptcp_options_received *mp_opt)
751 : : {
752 : 520 : struct mptcp_sock *msk = subflow_req->msk;
753 : 520 : u8 hmac[SHA256_DIGEST_SIZE];
754 : :
755 : 520 : subflow_generate_hmac(READ_ONCE(msk->remote_key),
756 : 520 : READ_ONCE(msk->local_key),
757 : 520 : subflow_req->remote_nonce,
758 : 520 : subflow_req->local_nonce, hmac);
759 : :
760 : 520 : return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN);
761 : : }
762 : :
763 : 145 : static void subflow_ulp_fallback(struct sock *sk,
764 : : struct mptcp_subflow_context *old_ctx)
765 : : {
766 : 145 : struct inet_connection_sock *icsk = inet_csk(sk);
767 : :
768 [ - + ]: 145 : mptcp_subflow_tcp_fallback(sk, old_ctx);
769 : 145 : icsk->icsk_ulp_ops = NULL;
770 : 145 : rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
771 [ - + ]: 145 : tcp_sk(sk)->is_mptcp = 0;
772 : :
773 : 169 : mptcp_subflow_ops_undo_override(sk);
774 : 145 : }
775 : :
776 : 145 : void mptcp_subflow_drop_ctx(struct sock *ssk)
777 : : {
778 [ + + ]: 145 : struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
779 : :
780 [ + + ]: 145 : if (!ctx)
781 : : return;
782 : :
783 [ + - ]: 68 : list_del(&mptcp_subflow_ctx(ssk)->node);
784 [ + - ]: 68 : if (inet_csk(ssk)->icsk_ulp_ops) {
785 : 68 : subflow_ulp_fallback(ssk, ctx);
786 [ + + ]: 68 : if (ctx->conn)
787 : 42 : sock_put(ctx->conn);
788 : : }
789 : :
790 : 68 : kfree_rcu(ctx, rcu);
791 : : }
792 : :
793 : 2718 : void __mptcp_subflow_fully_established(struct mptcp_sock *msk,
794 : : struct mptcp_subflow_context *subflow,
795 : : const struct mptcp_options_received *mp_opt)
796 : : {
797 : 2718 : subflow_set_remote_key(msk, subflow, mp_opt);
798 : 2718 : WRITE_ONCE(subflow->fully_established, true);
799 : 2718 : WRITE_ONCE(msk->fully_established, true);
800 : 2718 : }
801 : :
802 : 1783 : static struct sock *subflow_syn_recv_sock(const struct sock *sk,
803 : : struct sk_buff *skb,
804 : : struct request_sock *req,
805 : : struct dst_entry *dst,
806 : : struct request_sock *req_unhash,
807 : : bool *own_req)
808 : : {
809 [ - + ]: 1783 : struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk);
810 : 1783 : struct mptcp_subflow_request_sock *subflow_req;
811 : 1783 : struct mptcp_options_received mp_opt;
812 : 1783 : bool fallback, fallback_is_fatal;
813 : 1783 : enum sk_rst_reason reason;
814 : 1783 : struct mptcp_sock *owner;
815 : 1783 : struct sock *child;
816 : :
817 [ - + ]: 1783 : pr_debug("listener=%p, req=%p, conn=%p\n", listener, req, listener->conn);
818 : :
819 : : /* After child creation we must look for MPC even when options
820 : : * are not parsed
821 : : */
822 : 1783 : mp_opt.suboptions = 0;
823 : :
824 : : /* hopefully temporary handling for MP_JOIN+syncookie */
825 : 1783 : subflow_req = mptcp_subflow_rsk(req);
826 [ + + + + : 1783 : fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join;
+ + ]
827 [ - + ]: 1783 : fallback = !tcp_rsk(req)->is_mptcp;
828 [ - + ]: 1783 : if (fallback)
829 : 0 : goto create_child;
830 : :
831 : : /* if the sk is MP_CAPABLE, we try to fetch the client key */
832 [ + + ]: 1783 : if (subflow_req->mp_capable) {
833 : : /* we can receive and accept an in-window, out-of-order pkt,
834 : : * which may not carry the MP_CAPABLE opt even on mptcp enabled
835 : : * paths: always try to extract the peer key, and fallback
836 : : * for packets missing it.
837 : : * Even OoO DSS packets coming legitly after dropped or
838 : : * reordered MPC will cause fallback, but we don't have other
839 : : * options.
840 : : */
841 : 1186 : mptcp_get_options(skb, &mp_opt);
842 [ + + ]: 1186 : if (!(mp_opt.suboptions &
843 : : (OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_ACK)))
844 : 0 : fallback = true;
845 : :
846 [ + + ]: 597 : } else if (subflow_req->mp_join) {
847 : 520 : mptcp_get_options(skb, &mp_opt);
848 [ - + ]: 520 : if (!(mp_opt.suboptions & OPTION_MPTCP_MPJ_ACK))
849 : 0 : fallback = true;
850 : : }
851 : :
852 : 537 : create_child:
853 : 1783 : child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
854 : : req_unhash, own_req);
855 : :
856 [ + - + + : 1783 : if (child && *own_req) {
+ - ]
857 [ + + ]: 1783 : struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
858 : :
859 : 1783 : tcp_rsk(req)->drop_req = false;
860 : :
861 : : /* we need to fallback on ctx allocation failure and on pre-reqs
862 : : * checking above. In the latter scenario we additionally need
863 : : * to reset the context to non MPTCP status.
864 : : */
865 [ + + ]: 1783 : if (!ctx || fallback) {
866 [ - + ]: 95 : if (fallback_is_fatal) {
867 : 0 : subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
868 : 0 : goto dispose_child;
869 : : }
870 : 95 : goto fallback;
871 : : }
872 : :
873 : : /* ssk inherits options of listener sk */
874 : 1688 : ctx->setsockopt_seq = listener->setsockopt_seq;
875 : :
876 [ + + ]: 1688 : if (ctx->mp_capable) {
877 : 1168 : ctx->conn = mptcp_sk_clone_init(listener->conn, &mp_opt, child, req);
878 [ - + ]: 1168 : if (!ctx->conn)
879 : 0 : goto fallback;
880 : :
881 : 1168 : ctx->subflow_id = 1;
882 [ - + ]: 1168 : owner = mptcp_sk(ctx->conn);
883 : 1168 : mptcp_pm_new_connection(owner, child, 1);
884 : :
885 : : /* with OoO packets we can reach here without ingress
886 : : * mpc option
887 : : */
888 [ + + ]: 1168 : if (mp_opt.suboptions & OPTION_MPTCP_MPC_ACK) {
889 : 1130 : mptcp_pm_fully_established(owner, child);
890 : 1130 : ctx->pm_notified = 1;
891 : : }
892 [ + - ]: 520 : } else if (ctx->mp_join) {
893 : 520 : owner = subflow_req->msk;
894 [ - + ]: 520 : if (!owner) {
895 : 0 : subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
896 : 0 : goto dispose_child;
897 : : }
898 : :
899 [ - + ]: 520 : if (!subflow_hmac_valid(subflow_req, &mp_opt)) {
900 [ # # ]: 0 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
901 : 0 : subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
902 : 0 : goto dispose_child;
903 : : }
904 : :
905 [ + + ]: 520 : if (!mptcp_can_accept_new_subflow(owner)) {
906 [ + - ]: 8 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINREJECTED);
907 : 8 : subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
908 : 8 : goto dispose_child;
909 : : }
910 : :
911 : : /* move the msk reference ownership to the subflow */
912 : 512 : subflow_req->msk = NULL;
913 : 512 : ctx->conn = (struct sock *)owner;
914 : :
915 [ + + ]: 512 : if (subflow_use_different_sport(owner, sk)) {
916 [ - + ]: 24 : pr_debug("ack inet_sport=%d %d\n",
917 : : ntohs(inet_sk(sk)->inet_sport),
918 : : ntohs(inet_sk((struct sock *)owner)->inet_sport));
919 [ - + ]: 24 : if (!mptcp_pm_sport_in_anno_list(owner, sk)) {
920 [ # # ]: 0 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX);
921 : 0 : subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
922 : 0 : goto dispose_child;
923 : : }
924 [ + - ]: 24 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX);
925 : : }
926 : :
927 [ - + ]: 512 : if (!mptcp_finish_join(child)) {
928 : 0 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(child);
929 : :
930 : 0 : subflow_add_reset_reason(skb, subflow->reset_reason);
931 : 0 : goto dispose_child;
932 : : }
933 : :
934 [ + - ]: 512 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
935 : 512 : tcp_rsk(req)->drop_req = true;
936 : : }
937 : : }
938 : :
939 : : /* check for expected invariant - should never trigger, just help
940 : : * catching earlier subtle bugs
941 : : */
942 [ + + + + : 1680 : WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
+ + + + +
+ + - -
+ ]
943 : : (!mptcp_subflow_ctx(child) ||
944 : : !mptcp_subflow_ctx(child)->conn));
945 : 1680 : return child;
946 : :
947 : 8 : dispose_child:
948 : 8 : mptcp_subflow_drop_ctx(child);
949 : 8 : tcp_rsk(req)->drop_req = true;
950 : 8 : inet_csk_prepare_for_destroy_sock(child);
951 : 8 : tcp_done(child);
952 : 8 : reason = mptcp_get_rst_reason(skb);
953 : 8 : req->rsk_ops->send_reset(sk, skb, reason);
954 : :
955 : : /* The last child reference will be released by the caller */
956 : 8 : return child;
957 : :
958 : 95 : fallback:
959 [ + + ]: 95 : if (fallback)
960 [ + - ]: 18 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
961 : 95 : mptcp_subflow_drop_ctx(child);
962 : 95 : return child;
963 : : }
964 : :
965 : : static struct inet_connection_sock_af_ops subflow_specific __ro_after_init;
966 : : static struct proto tcp_prot_override __ro_after_init;
967 : :
968 : : enum mapping_status {
969 : : MAPPING_OK,
970 : : MAPPING_INVALID,
971 : : MAPPING_EMPTY,
972 : : MAPPING_DATA_FIN,
973 : : MAPPING_DUMMY,
974 : : MAPPING_BAD_CSUM,
975 : : MAPPING_NODSS
976 : : };
977 : :
978 : 0 : static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
979 : : {
980 [ # # ]: 0 : pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d\n",
981 : : ssn, subflow->map_subflow_seq, subflow->map_data_len);
982 : 0 : }
983 : :
984 : 0 : static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
985 : : {
986 [ # # ]: 0 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
987 : 0 : unsigned int skb_consumed;
988 : :
989 [ # # ]: 0 : skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
990 [ # # ]: 0 : if (unlikely(skb_consumed >= skb->len)) {
991 : 0 : DEBUG_NET_WARN_ON_ONCE(1);
992 : 0 : return true;
993 : : }
994 : :
995 : 0 : return skb->len - skb_consumed <= subflow->map_data_len -
996 : 0 : mptcp_subflow_get_map_offset(subflow);
997 : : }
998 : :
999 : 199635 : static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
1000 : : {
1001 [ - + ]: 199635 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1002 [ - + ]: 199635 : u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
1003 : :
1004 [ - + ]: 199635 : if (unlikely(before(ssn, subflow->map_subflow_seq))) {
1005 : : /* Mapping covers data later in the subflow stream,
1006 : : * currently unsupported.
1007 : : */
1008 : 0 : dbg_bad_map(subflow, ssn);
1009 : 0 : return false;
1010 : : }
1011 [ - + ]: 199635 : if (unlikely(!before(ssn, subflow->map_subflow_seq +
1012 : : subflow->map_data_len))) {
1013 : : /* Mapping does covers past subflow data, invalid */
1014 : 0 : dbg_bad_map(subflow, ssn);
1015 : 0 : return false;
1016 : : }
1017 : : return true;
1018 : : }
1019 : :
1020 : 564088 : static enum mapping_status validate_data_csum(struct sock *ssk, struct sk_buff *skb,
1021 : : bool csum_reqd)
1022 : : {
1023 [ + + ]: 564088 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1024 : 564088 : u32 offset, seq, delta;
1025 : 564088 : __sum16 csum;
1026 : 564088 : int len;
1027 : :
1028 [ + + ]: 564088 : if (!csum_reqd)
1029 : : return MAPPING_OK;
1030 : :
1031 : : /* mapping already validated on previous traversal */
1032 [ + + ]: 1610 : if (subflow->map_csum_len == subflow->map_data_len)
1033 : : return MAPPING_OK;
1034 : :
1035 : : /* traverse the receive queue, ensuring it contains a full
1036 : : * DSS mapping and accumulating the related csum.
1037 : : * Preserve the accoumlate csum across multiple calls, to compute
1038 : : * the csum only once
1039 : : */
1040 : 1600 : delta = subflow->map_data_len - subflow->map_csum_len;
1041 : 10329 : for (;;) {
1042 [ - + ]: 10329 : seq = tcp_sk(ssk)->copied_seq + subflow->map_csum_len;
1043 : 10329 : offset = seq - TCP_SKB_CB(skb)->seq;
1044 : :
1045 : : /* if the current skb has not been accounted yet, csum its contents
1046 : : * up to the amount covered by the current DSS
1047 : : */
1048 [ + + ]: 10329 : if (offset < skb->len) {
1049 : 1604 : __wsum csum;
1050 : :
1051 : 1604 : len = min(skb->len - offset, delta);
1052 : 1604 : csum = skb_checksum(skb, offset, len, 0);
1053 : 3208 : subflow->map_data_csum = csum_block_add(subflow->map_data_csum, csum,
1054 [ - + ]: 1604 : subflow->map_csum_len);
1055 : :
1056 : 1604 : delta -= len;
1057 : 1604 : subflow->map_csum_len += len;
1058 : : }
1059 [ + + ]: 10329 : if (delta == 0)
1060 : : break;
1061 : :
1062 [ + + ]: 10199 : if (skb_queue_is_last(&ssk->sk_receive_queue, skb)) {
1063 : : /* if this subflow is closed, the partial mapping
1064 : : * will be never completed; flush the pending skbs, so
1065 : : * that subflow_sched_work_if_closed() can kick in
1066 : : */
1067 [ - + ]: 1470 : if (unlikely(ssk->sk_state == TCP_CLOSE))
1068 [ # # # # ]: 0 : while ((skb = skb_peek(&ssk->sk_receive_queue)))
1069 : 0 : sk_eat_skb(ssk, skb);
1070 : :
1071 : : /* not enough data to validate the csum */
1072 : 1470 : return MAPPING_EMPTY;
1073 : : }
1074 : :
1075 : : /* the DSS mapping for next skbs will be validated later,
1076 : : * when a get_mapping_status call will process such skb
1077 : : */
1078 : : skb = skb->next;
1079 : : }
1080 : :
1081 : : /* note that 'map_data_len' accounts only for the carried data, does
1082 : : * not include the eventual seq increment due to the data fin,
1083 : : * while the pseudo header requires the original DSS data len,
1084 : : * including that
1085 : : */
1086 : 130 : csum = __mptcp_make_csum(subflow->map_seq,
1087 : : subflow->map_subflow_seq,
1088 : 130 : subflow->map_data_len + subflow->map_data_fin,
1089 : : subflow->map_data_csum);
1090 [ + + ]: 130 : if (unlikely(csum)) {
1091 [ + - ]: 4 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DATACSUMERR);
1092 : 4 : return MAPPING_BAD_CSUM;
1093 : : }
1094 : :
1095 : 126 : subflow->valid_csum_seen = 1;
1096 : 126 : return MAPPING_OK;
1097 : : }
1098 : :
1099 : 1118674 : static enum mapping_status get_mapping_status(struct sock *ssk,
1100 : : struct mptcp_sock *msk)
1101 : : {
1102 [ + + ]: 1118674 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1103 [ - + ]: 1118674 : bool csum_reqd = READ_ONCE(msk->csum_enabled);
1104 : 1118674 : struct mptcp_ext *mpext;
1105 : 1118674 : struct sk_buff *skb;
1106 : 1118674 : u16 data_len;
1107 : 1118674 : u64 map_seq;
1108 : :
1109 [ + + ]: 1118746 : skb = skb_peek(&ssk->sk_receive_queue);
1110 [ - + ]: 568211 : if (!skb)
1111 : : return MAPPING_EMPTY;
1112 : :
1113 [ + + ]: 568211 : if (mptcp_check_fallback(ssk))
1114 : : return MAPPING_DUMMY;
1115 : :
1116 [ + + ]: 565044 : mpext = mptcp_get_ext(skb);
1117 [ + + + + ]: 564207 : if (!mpext || !mpext->use_map) {
1118 [ + + + + ]: 2596 : if (!subflow->map_valid && !skb->len) {
1119 : : /* the TCP stack deliver 0 len FIN pkt to the receive
1120 : : * queue, that is the only 0len pkts ever expected here,
1121 : : * and we can admit no mapping only for 0 len pkts
1122 : : */
1123 [ - + ]: 924 : if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
1124 [ # # # # ]: 0 : WARN_ONCE(1, "0len seq %d:%d flags %x",
1125 : : TCP_SKB_CB(skb)->seq,
1126 : : TCP_SKB_CB(skb)->end_seq,
1127 : : TCP_SKB_CB(skb)->tcp_flags);
1128 : 924 : sk_eat_skb(ssk, skb);
1129 : 924 : return MAPPING_EMPTY;
1130 : : }
1131 : :
1132 : : /* If the required DSS has likely been dropped by a middlebox */
1133 [ + + ]: 1672 : if (!subflow->map_valid)
1134 : : return MAPPING_NODSS;
1135 : :
1136 : 1654 : goto validate_seq;
1137 : : }
1138 : :
1139 : 562448 : trace_get_mapping_status(mpext);
1140 : :
1141 : 562448 : data_len = mpext->data_len;
1142 [ + + ]: 562448 : if (data_len == 0) {
1143 [ - + ]: 2 : pr_debug("infinite mapping received\n");
1144 [ + - ]: 2 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
1145 : 2 : return MAPPING_INVALID;
1146 : : }
1147 : :
1148 [ + + ]: 562446 : if (mpext->data_fin == 1) {
1149 : 804 : u64 data_fin_seq;
1150 : :
1151 [ + + ]: 804 : if (data_len == 1) {
1152 : 12 : bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
1153 : 12 : mpext->dsn64);
1154 [ - + ]: 12 : pr_debug("DATA_FIN with no payload seq=%llu\n", mpext->data_seq);
1155 [ - + ]: 12 : if (subflow->map_valid) {
1156 : : /* A DATA_FIN might arrive in a DSS
1157 : : * option before the previous mapping
1158 : : * has been fully consumed. Continue
1159 : : * handling the existing mapping.
1160 : : */
1161 : 0 : skb_ext_del(skb, SKB_EXT_MPTCP);
1162 : 0 : return MAPPING_OK;
1163 : : }
1164 : :
1165 [ + + ]: 12 : if (updated)
1166 : 6 : mptcp_schedule_work((struct sock *)msk);
1167 : :
1168 : 12 : return MAPPING_DATA_FIN;
1169 : : }
1170 : :
1171 : 792 : data_fin_seq = mpext->data_seq + data_len - 1;
1172 : :
1173 : : /* If mpext->data_seq is a 32-bit value, data_fin_seq must also
1174 : : * be limited to 32 bits.
1175 : : */
1176 [ - + ]: 792 : if (!mpext->dsn64)
1177 : 0 : data_fin_seq &= GENMASK_ULL(31, 0);
1178 : :
1179 : 792 : mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
1180 [ - + ]: 792 : pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d\n",
1181 : : data_fin_seq, mpext->dsn64);
1182 : :
1183 : : /* Adjust for DATA_FIN using 1 byte of sequence space */
1184 : 792 : data_len--;
1185 : : }
1186 : :
1187 [ + + ]: 562434 : map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64);
1188 [ - + ]: 562434 : WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
1189 : :
1190 [ + + ]: 562434 : if (subflow->map_valid) {
1191 : : /* Allow replacing only with an identical map */
1192 [ + - ]: 364453 : if (subflow->map_seq == map_seq &&
1193 [ + - ]: 364453 : subflow->map_subflow_seq == mpext->subflow_seq &&
1194 [ + - ]: 364453 : subflow->map_data_len == data_len &&
1195 [ + - ]: 364453 : subflow->map_csum_reqd == mpext->csum_reqd) {
1196 : 364453 : skb_ext_del(skb, SKB_EXT_MPTCP);
1197 : 364453 : goto validate_csum;
1198 : : }
1199 : :
1200 : : /* If this skb data are fully covered by the current mapping,
1201 : : * the new map would need caching, which is not supported
1202 : : */
1203 [ # # ]: 0 : if (skb_is_fully_mapped(ssk, skb)) {
1204 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
1205 : 0 : return MAPPING_INVALID;
1206 : : }
1207 : :
1208 : : /* will validate the next map after consuming the current one */
1209 : 0 : goto validate_csum;
1210 : : }
1211 : :
1212 : 197981 : subflow->map_seq = map_seq;
1213 : 197981 : subflow->map_subflow_seq = mpext->subflow_seq;
1214 : 197981 : subflow->map_data_len = data_len;
1215 : 197981 : subflow->map_valid = 1;
1216 : 197981 : subflow->map_data_fin = mpext->data_fin;
1217 : 197981 : subflow->mpc_map = mpext->mpc_map;
1218 : 197981 : subflow->map_csum_reqd = mpext->csum_reqd;
1219 : 197981 : subflow->map_csum_len = 0;
1220 [ - + ]: 197981 : subflow->map_data_csum = csum_unfold(mpext->csum);
1221 : :
1222 : : /* Cfr RFC 8684 Section 3.3.0 */
1223 [ - + ]: 197981 : if (unlikely(subflow->map_csum_reqd != csum_reqd))
1224 : : return MAPPING_INVALID;
1225 : :
1226 [ - + ]: 197981 : pr_debug("new map seq=%llu subflow_seq=%u data_len=%u csum=%d:%u\n",
1227 : : subflow->map_seq, subflow->map_subflow_seq,
1228 : : subflow->map_data_len, subflow->map_csum_reqd,
1229 : : subflow->map_data_csum);
1230 : :
1231 : 199635 : validate_seq:
1232 : : /* we revalidate valid mapping on new skb, because we must ensure
1233 : : * the current skb is completely covered by the available mapping
1234 : : */
1235 [ - + ]: 199635 : if (!validate_mapping(ssk, skb)) {
1236 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSTCPMISMATCH);
1237 : 0 : return MAPPING_INVALID;
1238 : : }
1239 : :
1240 : 199635 : skb_ext_del(skb, SKB_EXT_MPTCP);
1241 : :
1242 : 564088 : validate_csum:
1243 : 564088 : return validate_data_csum(ssk, skb, csum_reqd);
1244 : : }
1245 : :
1246 : 4748 : static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
1247 : : u64 limit)
1248 : : {
1249 [ - + ]: 4748 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1250 : 4748 : bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
1251 [ - + ]: 4748 : struct tcp_sock *tp = tcp_sk(ssk);
1252 : 4748 : u32 offset, incr, avail_len;
1253 : :
1254 : 4748 : offset = tp->copied_seq - TCP_SKB_CB(skb)->seq;
1255 [ - + - + ]: 4748 : if (WARN_ON_ONCE(offset > skb->len))
1256 : 0 : goto out;
1257 : :
1258 : 4748 : avail_len = skb->len - offset;
1259 [ + + ]: 4748 : incr = limit >= avail_len ? avail_len + fin : limit;
1260 : :
1261 [ - + ]: 4748 : pr_debug("discarding=%d len=%d offset=%d seq=%d\n", incr, skb->len,
1262 : : offset, subflow->map_subflow_seq);
1263 [ + - ]: 4748 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
1264 [ - + ]: 4748 : tcp_sk(ssk)->copied_seq += incr;
1265 : :
1266 : 4748 : out:
1267 [ - + + + ]: 4748 : if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
1268 : 4564 : sk_eat_skb(ssk, skb);
1269 [ + + ]: 4748 : if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
1270 : 3628 : subflow->map_valid = 0;
1271 : 4748 : }
1272 : :
1273 : 0 : static bool subflow_is_done(const struct sock *sk)
1274 : : {
1275 [ - + - - ]: 207 : return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
1276 : : }
1277 : :
1278 : : /* sched mptcp worker for subflow cleanup if no more data is pending */
1279 : 557899 : static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
1280 : : {
1281 : 557899 : struct sock *sk = (struct sock *)msk;
1282 : :
1283 [ + + + + : 560699 : if (likely(ssk->sk_state != TCP_CLOSE &&
+ + ]
1284 : : (ssk->sk_state != TCP_CLOSE_WAIT ||
1285 : : inet_sk_state_load(sk) != TCP_ESTABLISHED)))
1286 : 556166 : return;
1287 : :
1288 [ + + ]: 1733 : if (!skb_queue_empty(&ssk->sk_receive_queue))
1289 : : return;
1290 : :
1291 [ + + ]: 2481 : if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
1292 : 659 : mptcp_schedule_work(sk);
1293 : :
1294 : : /* when the fallback subflow closes the rx side, trigger a 'dummy'
1295 : : * ingress data fin, so that the msk state will follow along
1296 : : */
1297 [ + + ]: 1649 : if (__mptcp_check_fallback(msk) && subflow_is_done(ssk) &&
1298 [ + - + + ]: 414 : msk->first == ssk &&
1299 : 207 : mptcp_update_rcv_data_fin(msk, READ_ONCE(msk->ack_seq), true))
1300 : 84 : mptcp_schedule_work(sk);
1301 : : }
1302 : :
1303 : 2 : static void mptcp_subflow_fail(struct mptcp_sock *msk, struct sock *ssk)
1304 : : {
1305 [ - + ]: 2 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1306 : 2 : unsigned long fail_tout;
1307 : :
1308 : : /* graceful failure can happen only on the MPC subflow */
1309 [ - + + - ]: 2 : if (WARN_ON_ONCE(ssk != READ_ONCE(msk->first)))
1310 : : return;
1311 : :
1312 : : /* since the close timeout take precedence on the fail one,
1313 : : * no need to start the latter when the first is already set
1314 : : */
1315 [ + - ]: 2 : if (sock_flag((struct sock *)msk, SOCK_DEAD))
1316 : : return;
1317 : :
1318 : : /* we don't need extreme accuracy here, use a zero fail_tout as special
1319 : : * value meaning no fail timeout at all;
1320 : : */
1321 : 2 : fail_tout = jiffies + TCP_RTO_MAX;
1322 : 2 : if (!fail_tout)
1323 : : fail_tout = 1;
1324 : 2 : WRITE_ONCE(subflow->fail_tout, fail_tout);
1325 : 2 : tcp_send_ack(ssk);
1326 : :
1327 : 2 : mptcp_reset_tout_timer(msk, subflow->fail_tout);
1328 : : }
1329 : :
1330 : 1169313 : static bool subflow_check_data_avail(struct sock *ssk)
1331 : : {
1332 [ + + ]: 1169313 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1333 : 1169313 : enum mapping_status status;
1334 : 1169313 : struct mptcp_sock *msk;
1335 : 1169313 : struct sk_buff *skb;
1336 : :
1337 [ + + - + ]: 1169313 : if (!skb_peek(&ssk->sk_receive_queue))
1338 : 545971 : WRITE_ONCE(subflow->data_avail, false);
1339 [ + + + + ]: 1169313 : if (subflow->data_avail)
1340 : : return true;
1341 : :
1342 [ - + ]: 1113926 : msk = mptcp_sk(subflow->conn);
1343 : 1123422 : for (;;) {
1344 : 1118674 : u64 ack_seq;
1345 : 1118674 : u64 old_ack;
1346 : :
1347 : 1118674 : status = get_mapping_status(ssk, msk);
1348 [ + + ]: 1670061 : trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue));
1349 [ + + + + ]: 1118674 : if (unlikely(status == MAPPING_INVALID || status == MAPPING_DUMMY ||
1350 : : status == MAPPING_BAD_CSUM || status == MAPPING_NODSS))
1351 : 3191 : goto fallback;
1352 : :
1353 [ + + ]: 1115483 : if (status != MAPPING_OK)
1354 : 552869 : goto no_data;
1355 : :
1356 [ + - ]: 562614 : skb = skb_peek(&ssk->sk_receive_queue);
1357 [ - + ]: 562614 : if (WARN_ON_ONCE(!skb))
1358 : 0 : goto no_data;
1359 : :
1360 [ - + - + ]: 562614 : if (unlikely(!READ_ONCE(msk->can_ack)))
1361 : 0 : goto fallback;
1362 : :
1363 : 562614 : old_ack = READ_ONCE(msk->ack_seq);
1364 : 562614 : ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
1365 [ - + ]: 562614 : pr_debug("msk ack_seq=%llx subflow ack_seq=%llx\n", old_ack,
1366 : : ack_seq);
1367 [ + + ]: 562614 : if (unlikely(before64(ack_seq, old_ack))) {
1368 : 4748 : mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
1369 : 4748 : continue;
1370 : : }
1371 : :
1372 : 557866 : WRITE_ONCE(subflow->data_avail, true);
1373 : 557866 : break;
1374 : : }
1375 : 557866 : return true;
1376 : :
1377 : 552869 : no_data:
1378 : 552869 : subflow_sched_work_if_closed(msk, ssk);
1379 : 552869 : return false;
1380 : :
1381 : 3191 : fallback:
1382 [ + + ]: 3191 : if (!__mptcp_check_fallback(msk)) {
1383 : : /* RFC 8684 section 3.7. */
1384 [ + + ]: 24 : if (status == MAPPING_BAD_CSUM &&
1385 [ + - ]: 4 : (subflow->mp_join || subflow->valid_csum_seen)) {
1386 : 4 : subflow->send_mp_fail = 1;
1387 : :
1388 [ + + + + ]: 4 : if (!READ_ONCE(msk->allow_infinite_fallback)) {
1389 : 2 : subflow->reset_transient = 0;
1390 : 2 : subflow->reset_reason = MPTCP_RST_EMIDDLEBOX;
1391 : 2 : goto reset;
1392 : : }
1393 : 2 : mptcp_subflow_fail(msk, ssk);
1394 : 2 : WRITE_ONCE(subflow->data_avail, true);
1395 : 2 : return true;
1396 : : }
1397 : :
1398 [ + + + + ]: 20 : if (!READ_ONCE(msk->allow_infinite_fallback)) {
1399 : : /* fatal protocol error, close the socket.
1400 : : * subflow_error_report() will introduce the appropriate barriers
1401 : : */
1402 : 12 : subflow->reset_transient = 0;
1403 : 12 : subflow->reset_reason = status == MAPPING_NODSS ?
1404 [ - + ]: 12 : MPTCP_RST_EMIDDLEBOX :
1405 : : MPTCP_RST_EMPTCP;
1406 : :
1407 : 14 : reset:
1408 : 14 : WRITE_ONCE(ssk->sk_err, EBADMSG);
1409 : 14 : tcp_set_state(ssk, TCP_CLOSE);
1410 [ + + + - ]: 29 : while ((skb = skb_peek(&ssk->sk_receive_queue)))
1411 : 15 : sk_eat_skb(ssk, skb);
1412 : 14 : mptcp_send_active_reset_reason(ssk);
1413 : 14 : WRITE_ONCE(subflow->data_avail, false);
1414 : 14 : return false;
1415 : : }
1416 : :
1417 : 8 : mptcp_do_fallback(ssk);
1418 : : }
1419 : :
1420 [ - + ]: 3175 : skb = skb_peek(&ssk->sk_receive_queue);
1421 : 3175 : subflow->map_valid = 1;
1422 : 3175 : subflow->map_seq = READ_ONCE(msk->ack_seq);
1423 : 3175 : subflow->map_data_len = skb->len;
1424 [ - + ]: 3175 : subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
1425 : 3175 : WRITE_ONCE(subflow->data_avail, true);
1426 : 3175 : return true;
1427 : : }
1428 : :
1429 : 1169313 : bool mptcp_subflow_data_available(struct sock *sk)
1430 : : {
1431 [ + + ]: 1169313 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1432 : :
1433 : : /* check if current mapping is still valid */
1434 [ + + ]: 1169313 : if (subflow->map_valid &&
1435 [ + + ]: 982346 : mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
1436 : 197522 : subflow->map_valid = 0;
1437 : 197522 : WRITE_ONCE(subflow->data_avail, false);
1438 : :
1439 [ - + ]: 197522 : pr_debug("Done with mapping: seq=%u data_len=%u\n",
1440 : : subflow->map_subflow_seq,
1441 : : subflow->map_data_len);
1442 : : }
1443 : :
1444 : 1169313 : return subflow_check_data_avail(sk);
1445 : : }
1446 : :
1447 : : /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
1448 : : * not the ssk one.
1449 : : *
1450 : : * In mptcp, rwin is about the mptcp-level connection data.
1451 : : *
1452 : : * Data that is still on the ssk rx queue can thus be ignored,
1453 : : * as far as mptcp peer is concerned that data is still inflight.
1454 : : * DSS ACK is updated when skb is moved to the mptcp rx queue.
1455 : : */
1456 : 1220773 : void mptcp_space(const struct sock *ssk, int *space, int *full_space)
1457 : : {
1458 : 1220773 : const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1459 : 1220773 : const struct sock *sk = subflow->conn;
1460 : :
1461 : 1220773 : *space = __mptcp_space(sk);
1462 : 1220773 : *full_space = mptcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf));
1463 : 1220773 : }
1464 : :
1465 : 1943 : static void subflow_error_report(struct sock *ssk)
1466 : : {
1467 [ + + ]: 1943 : struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1468 : :
1469 : : /* bail early if this is a no-op, so that we avoid introducing a
1470 : : * problematic lockdep dependency between TCP accept queue lock
1471 : : * and msk socket spinlock
1472 : : */
1473 [ + + ]: 1943 : if (!sk->sk_socket)
1474 : : return;
1475 : :
1476 : 1025 : mptcp_data_lock(sk);
1477 [ + + ]: 1025 : if (!sock_owned_by_user(sk))
1478 : 428 : __mptcp_error_report(sk);
1479 : : else
1480 [ - + ]: 597 : __set_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->cb_flags);
1481 : 1025 : mptcp_data_unlock(sk);
1482 : : }
1483 : :
1484 : 597856 : static void subflow_data_ready(struct sock *sk)
1485 : : {
1486 : 597856 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1487 [ - + ]: 597856 : u16 state = 1 << inet_sk_state_load(sk);
1488 : 597856 : struct sock *parent = subflow->conn;
1489 : 597856 : struct mptcp_sock *msk;
1490 : :
1491 : 597856 : trace_sk_data_ready(sk);
1492 : :
1493 [ - + ]: 597856 : msk = mptcp_sk(parent);
1494 [ + + ]: 597856 : if (state & TCPF_LISTEN) {
1495 : : /* MPJ subflow are removed from accept queue before reaching here,
1496 : : * avoid stray wakeups
1497 : : */
1498 [ + + ]: 1775 : if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
1499 : : return;
1500 : :
1501 : 1296 : parent->sk_data_ready(parent);
1502 : 1296 : return;
1503 : : }
1504 : :
1505 [ + + - + : 596081 : WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
- - ]
1506 : : !subflow->mp_join && !(state & TCPF_CLOSE));
1507 : :
1508 [ + + ]: 596081 : if (mptcp_subflow_data_available(sk)) {
1509 : 589269 : mptcp_data_ready(parent, sk);
1510 : :
1511 : : /* subflow-level lowat test are not relevant.
1512 : : * respect the msk-level threshold eventually mandating an immediate ack
1513 : : */
1514 [ + + ]: 589269 : if (mptcp_data_avail(msk) < parent->sk_rcvlowat &&
1515 [ - + - + : 107938 : (tcp_sk(sk)->rcv_nxt - tcp_sk(sk)->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss)
+ + ]
1516 : 64500 : inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_NOW;
1517 [ + + ]: 6812 : } else if (unlikely(sk->sk_err)) {
1518 : 14 : subflow_error_report(sk);
1519 : : }
1520 : : }
1521 : :
1522 : 319943 : static void subflow_write_space(struct sock *ssk)
1523 : : {
1524 : 319943 : struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1525 : :
1526 : 319943 : mptcp_propagate_sndbuf(sk, ssk);
1527 : 319943 : mptcp_write_space(sk);
1528 : 319943 : }
1529 : :
1530 : : static const struct inet_connection_sock_af_ops *
1531 : 0 : subflow_default_af_ops(struct sock *sk)
1532 : : {
1533 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1534 [ # # ]: 0 : if (sk->sk_family == AF_INET6)
1535 : 0 : return &subflow_v6_specific;
1536 : : #endif
1537 : : return &subflow_specific;
1538 : : }
1539 : :
1540 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1541 : 734 : void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
1542 : : {
1543 [ - + ]: 734 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1544 : 734 : struct inet_connection_sock *icsk = inet_csk(sk);
1545 : 734 : const struct inet_connection_sock_af_ops *target;
1546 : :
1547 [ - + ]: 734 : target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
1548 : :
1549 [ - + ]: 734 : pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d\n",
1550 : : subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
1551 : :
1552 [ + - ]: 734 : if (likely(icsk->icsk_af_ops == target))
1553 : : return;
1554 : :
1555 : 734 : subflow->icsk_af_ops = icsk->icsk_af_ops;
1556 : 734 : icsk->icsk_af_ops = target;
1557 : : }
1558 : : #endif
1559 : :
1560 : 1160 : void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
1561 : : struct sockaddr_storage *addr,
1562 : : unsigned short family)
1563 : : {
1564 : 1160 : memset(addr, 0, sizeof(*addr));
1565 : 1160 : addr->ss_family = family;
1566 [ + + ]: 1160 : if (addr->ss_family == AF_INET) {
1567 : 1036 : struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
1568 : :
1569 [ + + ]: 1036 : if (info->family == AF_INET)
1570 : 1017 : in_addr->sin_addr = info->addr;
1571 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1572 [ + - ]: 19 : else if (ipv6_addr_v4mapped(&info->addr6))
1573 : 19 : in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3];
1574 : : #endif
1575 : 1036 : in_addr->sin_port = info->port;
1576 : : }
1577 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1578 [ + - ]: 124 : else if (addr->ss_family == AF_INET6) {
1579 : 124 : struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;
1580 : :
1581 [ + + ]: 124 : if (info->family == AF_INET)
1582 [ - + ]: 2 : ipv6_addr_set_v4mapped(info->addr.s_addr,
1583 : : &in6_addr->sin6_addr);
1584 : : else
1585 : 122 : in6_addr->sin6_addr = info->addr6;
1586 : 124 : in6_addr->sin6_port = info->port;
1587 : : }
1588 : : #endif
1589 : 1160 : }
1590 : :
1591 : 634 : int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_pm_local *local,
1592 : : const struct mptcp_addr_info *remote)
1593 : : {
1594 [ - + ]: 634 : struct mptcp_sock *msk = mptcp_sk(sk);
1595 : 634 : struct mptcp_subflow_context *subflow;
1596 : 634 : int local_id = local->addr.id;
1597 : 634 : struct sockaddr_storage addr;
1598 : 634 : int remote_id = remote->id;
1599 : 634 : int err = -ENOTCONN;
1600 : 634 : struct socket *sf;
1601 : 634 : struct sock *ssk;
1602 : 634 : u32 remote_token;
1603 : 634 : int addrlen;
1604 : :
1605 : : /* The userspace PM sent the request too early? */
1606 [ - + ]: 634 : if (!mptcp_is_fully_established(sk))
1607 : 0 : goto err_out;
1608 : :
1609 : 634 : err = mptcp_subflow_create_socket(sk, local->addr.family, &sf);
1610 [ + + ]: 634 : if (err) {
1611 [ + - ]: 64 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXCREATSKERR);
1612 [ - + ]: 64 : pr_debug("msk=%p local=%d remote=%d create sock error: %d\n",
1613 : : msk, local_id, remote_id, err);
1614 : 64 : goto err_out;
1615 : : }
1616 : :
1617 : 570 : ssk = sf->sk;
1618 : 570 : subflow = mptcp_subflow_ctx(ssk);
1619 : 570 : do {
1620 : 570 : get_random_bytes(&subflow->local_nonce, sizeof(u32));
1621 [ - + ]: 570 : } while (!subflow->local_nonce);
1622 : :
1623 : : /* if 'IPADDRANY', the ID will be set later, after the routing */
1624 [ + + ]: 570 : if (local->addr.family == AF_INET) {
1625 [ + + ]: 508 : if (!local->addr.addr.s_addr)
1626 : : local_id = -1;
1627 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1628 [ + + ]: 62 : } else if (sk->sk_family == AF_INET6) {
1629 [ + + ]: 60 : if (ipv6_addr_any(&local->addr.addr6))
1630 : : local_id = -1;
1631 : : #endif
1632 : : }
1633 : :
1634 : 318 : if (local_id >= 0)
1635 : 318 : subflow_set_local_id(subflow, local_id);
1636 : :
1637 : 570 : subflow->remote_key_valid = 1;
1638 : 570 : subflow->remote_key = READ_ONCE(msk->remote_key);
1639 : 570 : subflow->local_key = READ_ONCE(msk->local_key);
1640 : 570 : subflow->token = msk->token;
1641 : 570 : mptcp_info2sockaddr(&local->addr, &addr, ssk->sk_family);
1642 : :
1643 : 570 : addrlen = sizeof(struct sockaddr_in);
1644 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1645 [ + + ]: 570 : if (addr.ss_family == AF_INET6)
1646 : 62 : addrlen = sizeof(struct sockaddr_in6);
1647 : : #endif
1648 : 570 : ssk->sk_bound_dev_if = local->ifindex;
1649 : 570 : err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
1650 [ + + ]: 570 : if (err) {
1651 [ + - ]: 2 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXBINDERR);
1652 [ - + ]: 2 : pr_debug("msk=%p local=%d remote=%d bind error: %d\n",
1653 : : msk, local_id, remote_id, err);
1654 : 2 : goto failed;
1655 : : }
1656 : :
1657 : 568 : mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1658 [ - + ]: 568 : pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d\n", msk,
1659 : : remote_token, local_id, remote_id);
1660 : 568 : subflow->remote_token = remote_token;
1661 : 568 : WRITE_ONCE(subflow->remote_id, remote_id);
1662 : 568 : subflow->request_join = 1;
1663 : 568 : subflow->request_bkup = !!(local->flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1664 : 568 : subflow->subflow_id = msk->subflow_id++;
1665 : 568 : mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
1666 : :
1667 : 568 : sock_hold(ssk);
1668 : 568 : list_add_tail(&subflow->node, &msk->conn_list);
1669 : 568 : err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
1670 [ + + ]: 568 : if (err && err != -EINPROGRESS) {
1671 [ + - ]: 26 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXCONNECTERR);
1672 [ - + ]: 26 : pr_debug("msk=%p local=%d remote=%d connect error: %d\n",
1673 : : msk, local_id, remote_id, err);
1674 : 26 : goto failed_unlink;
1675 : : }
1676 : :
1677 [ + - ]: 542 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTX);
1678 : :
1679 : : /* discard the subflow socket */
1680 : 542 : mptcp_sock_graft(ssk, sk->sk_socket);
1681 : 542 : iput(SOCK_INODE(sf));
1682 : 542 : WRITE_ONCE(msk->allow_infinite_fallback, false);
1683 : 542 : mptcp_stop_tout_timer(sk);
1684 : 542 : return 0;
1685 : :
1686 : 0 : failed_unlink:
1687 : 26 : list_del(&subflow->node);
1688 : 26 : sock_put(mptcp_subflow_tcp_sock(subflow));
1689 : :
1690 : 28 : failed:
1691 : 28 : subflow->disposable = 1;
1692 : 28 : sock_release(sf);
1693 : :
1694 : 92 : err_out:
1695 : : /* we account subflows before the creation, and this failures will not
1696 : : * be caught by sk_state_change()
1697 : : */
1698 : 92 : mptcp_pm_close_subflow(msk);
1699 : 92 : return err;
1700 : : }
1701 : :
1702 : 3100 : static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
1703 : : {
1704 : : #ifdef CONFIG_SOCK_CGROUP_DATA
1705 : 3100 : struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
1706 : 3100 : *child_skcd = &child->sk_cgrp_data;
1707 : :
1708 : : /* only the additional subflows created by kworkers have to be modified */
1709 [ + + ]: 3100 : if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
1710 [ + + ]: 187 : cgroup_id(sock_cgroup_ptr(child_skcd))) {
1711 : : #ifdef CONFIG_MEMCG
1712 : : struct mem_cgroup *memcg = parent->sk_memcg;
1713 : :
1714 : : mem_cgroup_sk_free(child);
1715 : : if (memcg && css_tryget(&memcg->css))
1716 : : child->sk_memcg = memcg;
1717 : : #endif /* CONFIG_MEMCG */
1718 : :
1719 : 61 : cgroup_sk_free(child_skcd);
1720 : 61 : *child_skcd = *parent_skcd;
1721 : 61 : cgroup_sk_clone(child_skcd);
1722 : : }
1723 : : #endif /* CONFIG_SOCK_CGROUP_DATA */
1724 : 3100 : }
1725 : :
1726 : 187 : static void mptcp_subflow_ops_override(struct sock *ssk)
1727 : : {
1728 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1729 : 3100 : if (ssk->sk_prot == &tcpv6_prot)
1730 : 948 : ssk->sk_prot = &tcpv6_prot_override;
1731 : : else
1732 : : #endif
1733 : 2152 : ssk->sk_prot = &tcp_prot_override;
1734 : : }
1735 : :
1736 : 313 : static void mptcp_subflow_ops_undo_override(struct sock *ssk)
1737 : : {
1738 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1739 [ + + ]: 145 : if (ssk->sk_prot == &tcpv6_prot_override)
1740 : 1838 : ssk->sk_prot = &tcpv6_prot;
1741 : : else
1742 : : #endif
1743 : 2946 : ssk->sk_prot = &tcp_prot;
1744 : : }
1745 : :
1746 : 3164 : int mptcp_subflow_create_socket(struct sock *sk, unsigned short family,
1747 : : struct socket **new_sock)
1748 : : {
1749 : 3164 : struct mptcp_subflow_context *subflow;
1750 [ + + ]: 3164 : struct net *net = sock_net(sk);
1751 : 3164 : struct socket *sf;
1752 : 3164 : int err;
1753 : :
1754 : : /* un-accepted server sockets can reach here - on bad configuration
1755 : : * bail early to avoid greater trouble later
1756 : : */
1757 [ + + ]: 3164 : if (unlikely(!sk->sk_socket))
1758 : : return -EINVAL;
1759 : :
1760 : 3100 : err = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP, &sf);
1761 [ + - ]: 3100 : if (err)
1762 : : return err;
1763 : :
1764 : 3100 : lock_sock_nested(sf->sk, SINGLE_DEPTH_NESTING);
1765 : :
1766 : 3100 : err = security_mptcp_add_subflow(sk, sf->sk);
1767 [ - + ]: 3100 : if (err)
1768 : 0 : goto err_free;
1769 : :
1770 : : /* the newly created socket has to be in the same cgroup as its parent */
1771 : 3100 : mptcp_attach_cgroup(sk, sf->sk);
1772 : :
1773 : : /* kernel sockets do not by default acquire net ref, but TCP timer
1774 : : * needs it.
1775 : : * Update ns_tracker to current stack trace and refcounted tracker.
1776 : : */
1777 : 3100 : sk_net_refcnt_upgrade(sf->sk);
1778 : 3100 : err = tcp_set_ulp(sf->sk, "mptcp");
1779 [ - + ]: 3100 : if (err)
1780 : 0 : goto err_free;
1781 : :
1782 [ - + ]: 3100 : mptcp_sockopt_sync_locked(mptcp_sk(sk), sf->sk);
1783 : 3100 : release_sock(sf->sk);
1784 : :
1785 : : /* the newly created socket really belongs to the owning MPTCP
1786 : : * socket, even if for additional subflows the allocation is performed
1787 : : * by a kernel workqueue. Adjust inode references, so that the
1788 : : * procfs/diag interfaces really show this one belonging to the correct
1789 : : * user.
1790 : : */
1791 [ - + ]: 3100 : SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
1792 : 3100 : SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
1793 : 3100 : SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
1794 : :
1795 [ - + ]: 3100 : subflow = mptcp_subflow_ctx(sf->sk);
1796 [ - + ]: 3100 : pr_debug("subflow=%p\n", subflow);
1797 : :
1798 : 3100 : *new_sock = sf;
1799 : 3100 : sock_hold(sk);
1800 : 3100 : subflow->conn = sk;
1801 [ + + ]: 3100 : mptcp_subflow_ops_override(sf->sk);
1802 : :
1803 : : return 0;
1804 : :
1805 : 0 : err_free:
1806 : 0 : release_sock(sf->sk);
1807 : 0 : sock_release(sf);
1808 : 0 : return err;
1809 : : }
1810 : :
1811 : 4806 : static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
1812 : : gfp_t priority)
1813 : : {
1814 : 4806 : struct inet_connection_sock *icsk = inet_csk(sk);
1815 : 4806 : struct mptcp_subflow_context *ctx;
1816 : :
1817 : 4806 : ctx = kzalloc(sizeof(*ctx), priority);
1818 [ + - ]: 4806 : if (!ctx)
1819 : : return NULL;
1820 : :
1821 : 4806 : rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
1822 [ - + ]: 4806 : INIT_LIST_HEAD(&ctx->node);
1823 : 4806 : INIT_LIST_HEAD(&ctx->delegated_node);
1824 : :
1825 [ - + ]: 4806 : pr_debug("subflow=%p\n", ctx);
1826 : :
1827 : 4806 : ctx->tcp_sock = sk;
1828 : 4806 : WRITE_ONCE(ctx->local_id, -1);
1829 : :
1830 : 4806 : return ctx;
1831 : : }
1832 : :
1833 : 5030 : static void __subflow_state_change(struct sock *sk)
1834 : : {
1835 : 5030 : struct socket_wq *wq;
1836 : :
1837 : 5030 : rcu_read_lock();
1838 [ + - - + : 5030 : wq = rcu_dereference(sk->sk_wq);
- - - - -
- ]
1839 [ + + ]: 5030 : if (skwq_has_sleeper(wq))
1840 : 356 : wake_up_interruptible_all(&wq->wait);
1841 : 5030 : rcu_read_unlock();
1842 : 5030 : }
1843 : :
1844 : 5030 : static void subflow_state_change(struct sock *sk)
1845 : : {
1846 : 5030 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1847 : 5030 : struct sock *parent = subflow->conn;
1848 : 5030 : struct mptcp_sock *msk;
1849 : :
1850 : 5030 : __subflow_state_change(sk);
1851 : :
1852 [ - + ]: 5030 : msk = mptcp_sk(parent);
1853 [ - + ]: 5030 : if (subflow_simultaneous_connect(sk)) {
1854 : 0 : mptcp_do_fallback(sk);
1855 [ # # ]: 0 : pr_fallback(msk);
1856 : 0 : subflow->conn_finished = 1;
1857 : 0 : mptcp_propagate_state(parent, sk, subflow, NULL);
1858 : : }
1859 : :
1860 : : /* as recvmsg() does not acquire the subflow socket for ssk selection
1861 : : * a fin packet carrying a DSS can be unnoticed if we don't trigger
1862 : : * the data available machinery here.
1863 : : */
1864 [ + + ]: 5030 : if (mptcp_subflow_data_available(sk))
1865 : 97 : mptcp_data_ready(parent, sk);
1866 [ + + ]: 4933 : else if (unlikely(sk->sk_err))
1867 : 498 : subflow_error_report(sk);
1868 : :
1869 [ - + ]: 5030 : subflow_sched_work_if_closed(mptcp_sk(parent), sk);
1870 : 5030 : }
1871 : :
1872 : 1253 : void mptcp_subflow_queue_clean(struct sock *listener_sk, struct sock *listener_ssk)
1873 : : {
1874 : 1253 : struct request_sock_queue *queue = &inet_csk(listener_ssk)->icsk_accept_queue;
1875 : 1253 : struct request_sock *req, *head, *tail;
1876 : 1253 : struct mptcp_subflow_context *subflow;
1877 : 1253 : struct sock *sk, *ssk;
1878 : :
1879 : : /* Due to lock dependencies no relevant lock can be acquired under rskq_lock.
1880 : : * Splice the req list, so that accept() can not reach the pending ssk after
1881 : : * the listener socket is released below.
1882 : : */
1883 : 1253 : spin_lock_bh(&queue->rskq_lock);
1884 : 1253 : head = queue->rskq_accept_head;
1885 : 1253 : tail = queue->rskq_accept_tail;
1886 : 1253 : queue->rskq_accept_head = NULL;
1887 : 1253 : queue->rskq_accept_tail = NULL;
1888 : 1253 : spin_unlock_bh(&queue->rskq_lock);
1889 : :
1890 [ + + ]: 1253 : if (!head)
1891 : : return;
1892 : :
1893 : : /* can't acquire the msk socket lock under the subflow one,
1894 : : * or will cause ABBA deadlock
1895 : : */
1896 : 43 : release_sock(listener_ssk);
1897 : :
1898 [ + + ]: 129 : for (req = head; req; req = req->dl_next) {
1899 : 43 : ssk = req->sk;
1900 [ + + - + ]: 43 : if (!sk_is_mptcp(ssk))
1901 : 1 : continue;
1902 : :
1903 [ + - ]: 42 : subflow = mptcp_subflow_ctx(ssk);
1904 [ + - - + ]: 42 : if (!subflow || !subflow->conn)
1905 : 0 : continue;
1906 : :
1907 : 42 : sk = subflow->conn;
1908 : 42 : sock_hold(sk);
1909 : :
1910 : 42 : lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1911 : 42 : __mptcp_unaccepted_force_close(sk);
1912 : 42 : release_sock(sk);
1913 : :
1914 : : /* lockdep will report a false positive ABBA deadlock
1915 : : * between cancel_work_sync and the listener socket.
1916 : : * The involved locks belong to different sockets WRT
1917 : : * the existing AB chain.
1918 : : * Using a per socket key is problematic as key
1919 : : * deregistration requires process context and must be
1920 : : * performed at socket disposal time, in atomic
1921 : : * context.
1922 : : * Just tell lockdep to consider the listener socket
1923 : : * released here.
1924 : : */
1925 : 42 : mutex_release(&listener_sk->sk_lock.dep_map, _RET_IP_);
1926 : 42 : mptcp_cancel_work(sk);
1927 : 42 : mutex_acquire(&listener_sk->sk_lock.dep_map, 0, 0, _RET_IP_);
1928 : :
1929 : 42 : sock_put(sk);
1930 : : }
1931 : :
1932 : : /* we are still under the listener msk socket lock */
1933 : 43 : lock_sock_nested(listener_ssk, SINGLE_DEPTH_NESTING);
1934 : :
1935 : : /* restore the listener queue, to let the TCP code clean it up */
1936 : 43 : spin_lock_bh(&queue->rskq_lock);
1937 [ - + ]: 43 : WARN_ON_ONCE(queue->rskq_accept_head);
1938 : 43 : queue->rskq_accept_head = head;
1939 : 43 : queue->rskq_accept_tail = tail;
1940 : 43 : spin_unlock_bh(&queue->rskq_lock);
1941 : : }
1942 : :
1943 : 3412 : static int subflow_ulp_init(struct sock *sk)
1944 : : {
1945 : 3412 : struct inet_connection_sock *icsk = inet_csk(sk);
1946 : 3412 : struct mptcp_subflow_context *ctx;
1947 [ - + ]: 3412 : struct tcp_sock *tp = tcp_sk(sk);
1948 : 3412 : int err = 0;
1949 : :
1950 : : /* disallow attaching ULP to a socket unless it has been
1951 : : * created with sock_create_kern()
1952 : : */
1953 [ + + ]: 3412 : if (!sk->sk_kern_sock) {
1954 : 312 : err = -EOPNOTSUPP;
1955 : 312 : goto out;
1956 : : }
1957 : :
1958 : 3100 : ctx = subflow_create_ctx(sk, GFP_KERNEL);
1959 [ - + ]: 3100 : if (!ctx) {
1960 : 0 : err = -ENOMEM;
1961 : 0 : goto out;
1962 : : }
1963 : :
1964 [ - + ]: 3100 : pr_debug("subflow=%p, family=%d\n", ctx, sk->sk_family);
1965 : :
1966 : 3100 : tp->is_mptcp = 1;
1967 : 3100 : ctx->icsk_af_ops = icsk->icsk_af_ops;
1968 [ + + ]: 3100 : icsk->icsk_af_ops = subflow_default_af_ops(sk);
1969 : 3100 : ctx->tcp_state_change = sk->sk_state_change;
1970 : 3100 : ctx->tcp_error_report = sk->sk_error_report;
1971 : :
1972 [ - + ]: 3100 : WARN_ON_ONCE(sk->sk_data_ready != sock_def_readable);
1973 [ - + ]: 3100 : WARN_ON_ONCE(sk->sk_write_space != sk_stream_write_space);
1974 : :
1975 : 3100 : sk->sk_data_ready = subflow_data_ready;
1976 : 3100 : sk->sk_write_space = subflow_write_space;
1977 : 3100 : sk->sk_state_change = subflow_state_change;
1978 : 3100 : sk->sk_error_report = subflow_error_report;
1979 : 3412 : out:
1980 : 3412 : return err;
1981 : : }
1982 : :
1983 : 4647 : static void subflow_ulp_release(struct sock *ssk)
1984 : : {
1985 [ + - ]: 4647 : struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
1986 : 4647 : bool release = true;
1987 : 4647 : struct sock *sk;
1988 : :
1989 [ + - ]: 4647 : if (!ctx)
1990 : : return;
1991 : :
1992 : 4647 : sk = ctx->conn;
1993 [ + - ]: 4647 : if (sk) {
1994 : : /* if the msk has been orphaned, keep the ctx
1995 : : * alive, will be freed by __mptcp_close_ssk(),
1996 : : * when the subflow is still unaccepted
1997 : : */
1998 [ - + - - ]: 4647 : release = ctx->disposable || list_empty(&ctx->node);
1999 : :
2000 : : /* inet_child_forget() does not call sk_state_change(),
2001 : : * explicitly trigger the socket close machinery
2002 : : */
2003 [ # # ]: 0 : if (!release && !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW,
2004 [ # # ]: 0 : &mptcp_sk(sk)->flags))
2005 : 0 : mptcp_schedule_work(sk);
2006 : 4647 : sock_put(sk);
2007 : : }
2008 : :
2009 [ + + ]: 4647 : mptcp_subflow_ops_undo_override(ssk);
2010 [ + - ]: 4647 : if (release)
2011 : 4647 : kfree_rcu(ctx, rcu);
2012 : : }
2013 : :
2014 : 1783 : static void subflow_ulp_clone(const struct request_sock *req,
2015 : : struct sock *newsk,
2016 : : const gfp_t priority)
2017 : : {
2018 : 1783 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
2019 [ + + ]: 1783 : struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
2020 : 1783 : struct mptcp_subflow_context *new_ctx;
2021 : :
2022 [ + + + - ]: 1783 : if (!tcp_rsk(req)->is_mptcp ||
2023 [ + + ]: 1783 : (!subflow_req->mp_capable && !subflow_req->mp_join)) {
2024 : 77 : subflow_ulp_fallback(newsk, old_ctx);
2025 : 77 : return;
2026 : : }
2027 : :
2028 : 1706 : new_ctx = subflow_create_ctx(newsk, priority);
2029 [ - + ]: 1706 : if (!new_ctx) {
2030 : 0 : subflow_ulp_fallback(newsk, old_ctx);
2031 : 0 : return;
2032 : : }
2033 : :
2034 : 1706 : new_ctx->conn_finished = 1;
2035 : 1706 : new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
2036 : 1706 : new_ctx->tcp_state_change = old_ctx->tcp_state_change;
2037 : 1706 : new_ctx->tcp_error_report = old_ctx->tcp_error_report;
2038 : 1706 : new_ctx->rel_write_seq = 1;
2039 : :
2040 [ + + ]: 1706 : if (subflow_req->mp_capable) {
2041 : : /* see comments in subflow_syn_recv_sock(), MPTCP connection
2042 : : * is fully established only after we receive the remote key
2043 : : */
2044 : 1186 : new_ctx->mp_capable = 1;
2045 : 1186 : new_ctx->local_key = subflow_req->local_key;
2046 : 1186 : new_ctx->token = subflow_req->token;
2047 : 1186 : new_ctx->ssn_offset = subflow_req->ssn_offset;
2048 : 1186 : new_ctx->idsn = subflow_req->idsn;
2049 : :
2050 : : /* this is the first subflow, id is always 0 */
2051 : 1186 : subflow_set_local_id(new_ctx, 0);
2052 [ + - ]: 520 : } else if (subflow_req->mp_join) {
2053 : 520 : new_ctx->ssn_offset = subflow_req->ssn_offset;
2054 : 520 : new_ctx->mp_join = 1;
2055 : 520 : WRITE_ONCE(new_ctx->fully_established, true);
2056 : 520 : new_ctx->remote_key_valid = 1;
2057 : 520 : new_ctx->backup = subflow_req->backup;
2058 : 520 : new_ctx->request_bkup = subflow_req->request_bkup;
2059 : 520 : WRITE_ONCE(new_ctx->remote_id, subflow_req->remote_id);
2060 : 520 : new_ctx->token = subflow_req->token;
2061 : 520 : new_ctx->thmac = subflow_req->thmac;
2062 : :
2063 : : /* the subflow req id is valid, fetched via subflow_check_req()
2064 : : * and subflow_token_join_request()
2065 : : */
2066 : 520 : subflow_set_local_id(new_ctx, subflow_req->local_id);
2067 : : }
2068 : : }
2069 : :
2070 : 531686 : static void tcp_release_cb_override(struct sock *ssk)
2071 : : {
2072 : 531686 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
2073 : 531686 : long status;
2074 : :
2075 : : /* process and clear all the pending actions, but leave the subflow into
2076 : : * the napi queue. To respect locking, only the same CPU that originated
2077 : : * the action can touch the list. mptcp_napi_poll will take care of it.
2078 : : */
2079 [ - + - + ]: 531686 : status = set_mask_bits(&subflow->delegated_status, MPTCP_DELEGATE_ACTIONS_MASK, 0);
2080 [ + + ]: 531686 : if (status)
2081 : 2655 : mptcp_subflow_process_delegated(ssk, status);
2082 : :
2083 : 531686 : tcp_release_cb(ssk);
2084 : 531686 : }
2085 : :
2086 : 0 : static int tcp_abort_override(struct sock *ssk, int err)
2087 : : {
2088 : : /* closing a listener subflow requires a great deal of care.
2089 : : * keep it simple and just prevent such operation
2090 : : */
2091 [ # # ]: 0 : if (inet_sk_state_load(ssk) == TCP_LISTEN)
2092 : : return -EINVAL;
2093 : :
2094 : 0 : return tcp_abort(ssk, err);
2095 : : }
2096 : :
2097 : : static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
2098 : : .name = "mptcp",
2099 : : .owner = THIS_MODULE,
2100 : : .init = subflow_ulp_init,
2101 : : .release = subflow_ulp_release,
2102 : : .clone = subflow_ulp_clone,
2103 : : };
2104 : :
2105 : 8 : static int subflow_ops_init(struct request_sock_ops *subflow_ops)
2106 : : {
2107 : 8 : subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
2108 : :
2109 : 8 : subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
2110 : : subflow_ops->obj_size, 0,
2111 : : SLAB_ACCOUNT |
2112 : : SLAB_TYPESAFE_BY_RCU,
2113 : : NULL);
2114 [ - + ]: 8 : if (!subflow_ops->slab)
2115 : 0 : return -ENOMEM;
2116 : :
2117 : : return 0;
2118 : : }
2119 : :
2120 : 4 : void __init mptcp_subflow_init(void)
2121 : : {
2122 : 4 : mptcp_subflow_v4_request_sock_ops = tcp_request_sock_ops;
2123 : 4 : mptcp_subflow_v4_request_sock_ops.slab_name = "request_sock_subflow_v4";
2124 : 4 : mptcp_subflow_v4_request_sock_ops.destructor = subflow_v4_req_destructor;
2125 : :
2126 [ - + ]: 4 : if (subflow_ops_init(&mptcp_subflow_v4_request_sock_ops) != 0)
2127 : 0 : panic("MPTCP: failed to init subflow v4 request sock ops\n");
2128 : :
2129 : 4 : subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
2130 : 4 : subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
2131 : 4 : subflow_request_sock_ipv4_ops.send_synack = subflow_v4_send_synack;
2132 : :
2133 : 4 : subflow_specific = ipv4_specific;
2134 : 4 : subflow_specific.conn_request = subflow_v4_conn_request;
2135 : 4 : subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
2136 : 4 : subflow_specific.sk_rx_dst_set = subflow_finish_connect;
2137 : 4 : subflow_specific.rebuild_header = subflow_rebuild_header;
2138 : :
2139 : 4 : tcp_prot_override = tcp_prot;
2140 : 4 : tcp_prot_override.release_cb = tcp_release_cb_override;
2141 : 4 : tcp_prot_override.diag_destroy = tcp_abort_override;
2142 : :
2143 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2144 : : /* In struct mptcp_subflow_request_sock, we assume the TCP request sock
2145 : : * structures for v4 and v6 have the same size. It should not changed in
2146 : : * the future but better to make sure to be warned if it is no longer
2147 : : * the case.
2148 : : */
2149 : 4 : BUILD_BUG_ON(sizeof(struct tcp_request_sock) != sizeof(struct tcp6_request_sock));
2150 : :
2151 : 4 : mptcp_subflow_v6_request_sock_ops = tcp6_request_sock_ops;
2152 : 4 : mptcp_subflow_v6_request_sock_ops.slab_name = "request_sock_subflow_v6";
2153 : 4 : mptcp_subflow_v6_request_sock_ops.destructor = subflow_v6_req_destructor;
2154 : :
2155 [ - + ]: 4 : if (subflow_ops_init(&mptcp_subflow_v6_request_sock_ops) != 0)
2156 : 0 : panic("MPTCP: failed to init subflow v6 request sock ops\n");
2157 : :
2158 : 4 : subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
2159 : 4 : subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
2160 : 4 : subflow_request_sock_ipv6_ops.send_synack = subflow_v6_send_synack;
2161 : :
2162 : 4 : subflow_v6_specific = ipv6_specific;
2163 : 4 : subflow_v6_specific.conn_request = subflow_v6_conn_request;
2164 : 4 : subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
2165 : 4 : subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
2166 : 4 : subflow_v6_specific.rebuild_header = subflow_v6_rebuild_header;
2167 : :
2168 : 4 : subflow_v6m_specific = subflow_v6_specific;
2169 : 4 : subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
2170 : 4 : subflow_v6m_specific.send_check = ipv4_specific.send_check;
2171 : 4 : subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
2172 : 4 : subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
2173 : 4 : subflow_v6m_specific.rebuild_header = subflow_rebuild_header;
2174 : :
2175 : 4 : tcpv6_prot_override = tcpv6_prot;
2176 : 4 : tcpv6_prot_override.release_cb = tcp_release_cb_override;
2177 : 4 : tcpv6_prot_override.diag_destroy = tcp_abort_override;
2178 : : #endif
2179 : :
2180 : 4 : mptcp_diag_subflow_init(&subflow_ulp_ops);
2181 : :
2182 [ - + ]: 4 : if (tcp_register_ulp(&subflow_ulp_ops) != 0)
2183 : 0 : panic("MPTCP: failed to register subflows to ULP\n");
2184 : 4 : }
|