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