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