mptcp: use proper req destructor for IPv6
[platform/kernel/linux-starfive.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 & OPTIONS_MPTCP_MPC);
160         opt_mp_join = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ);
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 & OPTIONS_MPTCP_MPC);
257         opt_mp_join = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ);
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         /* must hold: tcp_done() could drop last reference on parent */
359         sock_hold(sk);
360
361         tcp_set_state(ssk, TCP_CLOSE);
362         tcp_send_active_reset(ssk, GFP_ATOMIC);
363         tcp_done(ssk);
364         if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags) &&
365             schedule_work(&mptcp_sk(sk)->work))
366                 return; /* worker will put sk for us */
367
368         sock_put(sk);
369 }
370
371 static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk)
372 {
373         return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport;
374 }
375
376 void __mptcp_set_connected(struct sock *sk)
377 {
378         if (sk->sk_state == TCP_SYN_SENT) {
379                 inet_sk_state_store(sk, TCP_ESTABLISHED);
380                 sk->sk_state_change(sk);
381         }
382 }
383
384 static void mptcp_set_connected(struct sock *sk)
385 {
386         mptcp_data_lock(sk);
387         if (!sock_owned_by_user(sk))
388                 __mptcp_set_connected(sk);
389         else
390                 __set_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->cb_flags);
391         mptcp_data_unlock(sk);
392 }
393
394 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
395 {
396         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
397         struct mptcp_options_received mp_opt;
398         struct sock *parent = subflow->conn;
399
400         subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);
401
402         /* be sure no special action on any packet other than syn-ack */
403         if (subflow->conn_finished)
404                 return;
405
406         mptcp_propagate_sndbuf(parent, sk);
407         subflow->rel_write_seq = 1;
408         subflow->conn_finished = 1;
409         subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
410         pr_debug("subflow=%p synack seq=%x", subflow, subflow->ssn_offset);
411
412         mptcp_get_options(skb, &mp_opt);
413         if (subflow->request_mptcp) {
414                 if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPC)) {
415                         MPTCP_INC_STATS(sock_net(sk),
416                                         MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
417                         mptcp_do_fallback(sk);
418                         pr_fallback(mptcp_sk(subflow->conn));
419                         goto fallback;
420                 }
421
422                 if (mp_opt.suboptions & OPTION_MPTCP_CSUMREQD)
423                         WRITE_ONCE(mptcp_sk(parent)->csum_enabled, true);
424                 if (mp_opt.deny_join_id0)
425                         WRITE_ONCE(mptcp_sk(parent)->pm.remote_deny_join_id0, true);
426                 subflow->mp_capable = 1;
427                 subflow->can_ack = 1;
428                 subflow->remote_key = mp_opt.sndr_key;
429                 pr_debug("subflow=%p, remote_key=%llu", subflow,
430                          subflow->remote_key);
431                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK);
432                 mptcp_finish_connect(sk);
433                 mptcp_set_connected(parent);
434         } else if (subflow->request_join) {
435                 u8 hmac[SHA256_DIGEST_SIZE];
436
437                 if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ)) {
438                         subflow->reset_reason = MPTCP_RST_EMPTCP;
439                         goto do_reset;
440                 }
441
442                 subflow->backup = mp_opt.backup;
443                 subflow->thmac = mp_opt.thmac;
444                 subflow->remote_nonce = mp_opt.nonce;
445                 subflow->remote_id = mp_opt.join_id;
446                 pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u backup=%d",
447                          subflow, subflow->thmac, subflow->remote_nonce,
448                          subflow->backup);
449
450                 if (!subflow_thmac_valid(subflow)) {
451                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC);
452                         subflow->reset_reason = MPTCP_RST_EMPTCP;
453                         goto do_reset;
454                 }
455
456                 if (!mptcp_finish_join(sk))
457                         goto do_reset;
458
459                 subflow_generate_hmac(subflow->local_key, subflow->remote_key,
460                                       subflow->local_nonce,
461                                       subflow->remote_nonce,
462                                       hmac);
463                 memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
464
465                 subflow->mp_join = 1;
466                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
467
468                 if (subflow_use_different_dport(mptcp_sk(parent), sk)) {
469                         pr_debug("synack inet_dport=%d %d",
470                                  ntohs(inet_sk(sk)->inet_dport),
471                                  ntohs(inet_sk(parent)->inet_dport));
472                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX);
473                 }
474         } else if (mptcp_check_fallback(sk)) {
475 fallback:
476                 mptcp_rcv_space_init(mptcp_sk(parent), sk);
477                 mptcp_set_connected(parent);
478         }
479         return;
480
481 do_reset:
482         subflow->reset_transient = 0;
483         mptcp_subflow_reset(sk);
484 }
485
486 static void subflow_set_local_id(struct mptcp_subflow_context *subflow, int local_id)
487 {
488         subflow->local_id = local_id;
489         subflow->local_id_valid = 1;
490 }
491
492 static int subflow_chk_local_id(struct sock *sk)
493 {
494         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
495         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
496         int err;
497
498         if (likely(subflow->local_id_valid))
499                 return 0;
500
501         err = mptcp_pm_get_local_id(msk, (struct sock_common *)sk);
502         if (err < 0)
503                 return err;
504
505         subflow_set_local_id(subflow, err);
506         return 0;
507 }
508
509 static int subflow_rebuild_header(struct sock *sk)
510 {
511         int err = subflow_chk_local_id(sk);
512
513         if (unlikely(err < 0))
514                 return err;
515
516         return inet_sk_rebuild_header(sk);
517 }
518
519 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
520 static int subflow_v6_rebuild_header(struct sock *sk)
521 {
522         int err = subflow_chk_local_id(sk);
523
524         if (unlikely(err < 0))
525                 return err;
526
527         return inet6_sk_rebuild_header(sk);
528 }
529 #endif
530
531 static struct request_sock_ops mptcp_subflow_v4_request_sock_ops __ro_after_init;
532 static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops __ro_after_init;
533
534 static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
535 {
536         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
537
538         pr_debug("subflow=%p", subflow);
539
540         /* Never answer to SYNs sent to broadcast or multicast */
541         if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
542                 goto drop;
543
544         return tcp_conn_request(&mptcp_subflow_v4_request_sock_ops,
545                                 &subflow_request_sock_ipv4_ops,
546                                 sk, skb);
547 drop:
548         tcp_listendrop(sk);
549         return 0;
550 }
551
552 static void subflow_v4_req_destructor(struct request_sock *req)
553 {
554         subflow_req_destructor(req);
555         tcp_request_sock_ops.destructor(req);
556 }
557
558 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
559 static struct request_sock_ops mptcp_subflow_v6_request_sock_ops __ro_after_init;
560 static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops __ro_after_init;
561 static struct inet_connection_sock_af_ops subflow_v6_specific __ro_after_init;
562 static struct inet_connection_sock_af_ops subflow_v6m_specific __ro_after_init;
563 static struct proto tcpv6_prot_override;
564
565 static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
566 {
567         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
568
569         pr_debug("subflow=%p", subflow);
570
571         if (skb->protocol == htons(ETH_P_IP))
572                 return subflow_v4_conn_request(sk, skb);
573
574         if (!ipv6_unicast_destination(skb))
575                 goto drop;
576
577         if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) {
578                 __IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS);
579                 return 0;
580         }
581
582         return tcp_conn_request(&mptcp_subflow_v6_request_sock_ops,
583                                 &subflow_request_sock_ipv6_ops, sk, skb);
584
585 drop:
586         tcp_listendrop(sk);
587         return 0; /* don't send reset */
588 }
589
590 static void subflow_v6_req_destructor(struct request_sock *req)
591 {
592         subflow_req_destructor(req);
593         tcp6_request_sock_ops.destructor(req);
594 }
595 #endif
596
597 struct request_sock *mptcp_subflow_reqsk_alloc(const struct request_sock_ops *ops,
598                                                struct sock *sk_listener,
599                                                bool attach_listener)
600 {
601         if (ops->family == AF_INET)
602                 ops = &mptcp_subflow_v4_request_sock_ops;
603 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
604         else if (ops->family == AF_INET6)
605                 ops = &mptcp_subflow_v6_request_sock_ops;
606 #endif
607
608         return inet_reqsk_alloc(ops, sk_listener, attach_listener);
609 }
610 EXPORT_SYMBOL(mptcp_subflow_reqsk_alloc);
611
612 /* validate hmac received in third ACK */
613 static bool subflow_hmac_valid(const struct request_sock *req,
614                                const struct mptcp_options_received *mp_opt)
615 {
616         const struct mptcp_subflow_request_sock *subflow_req;
617         u8 hmac[SHA256_DIGEST_SIZE];
618         struct mptcp_sock *msk;
619
620         subflow_req = mptcp_subflow_rsk(req);
621         msk = subflow_req->msk;
622         if (!msk)
623                 return false;
624
625         subflow_generate_hmac(msk->remote_key, msk->local_key,
626                               subflow_req->remote_nonce,
627                               subflow_req->local_nonce, hmac);
628
629         return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN);
630 }
631
632 static void mptcp_force_close(struct sock *sk)
633 {
634         /* the msk is not yet exposed to user-space */
635         inet_sk_state_store(sk, TCP_CLOSE);
636         sk_common_release(sk);
637 }
638
639 static void subflow_ulp_fallback(struct sock *sk,
640                                  struct mptcp_subflow_context *old_ctx)
641 {
642         struct inet_connection_sock *icsk = inet_csk(sk);
643
644         mptcp_subflow_tcp_fallback(sk, old_ctx);
645         icsk->icsk_ulp_ops = NULL;
646         rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
647         tcp_sk(sk)->is_mptcp = 0;
648
649         mptcp_subflow_ops_undo_override(sk);
650 }
651
652 static void subflow_drop_ctx(struct sock *ssk)
653 {
654         struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
655
656         if (!ctx)
657                 return;
658
659         subflow_ulp_fallback(ssk, ctx);
660         if (ctx->conn)
661                 sock_put(ctx->conn);
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 sock *new_msk = NULL;
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 & OPTIONS_MPTCP_MPC)) {
717                         fallback = true;
718                         goto create_child;
719                 }
720
721                 new_msk = mptcp_sk_clone(listener->conn, &mp_opt, req);
722                 if (!new_msk)
723                         fallback = true;
724         } else if (subflow_req->mp_join) {
725                 mptcp_get_options(skb, &mp_opt);
726                 if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ) ||
727                     !subflow_hmac_valid(req, &mp_opt) ||
728                     !mptcp_can_accept_new_subflow(subflow_req->msk)) {
729                         SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
730                         fallback = true;
731                 }
732         }
733
734 create_child:
735         child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
736                                                      req_unhash, own_req);
737
738         if (child && *own_req) {
739                 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
740
741                 tcp_rsk(req)->drop_req = false;
742
743                 /* we need to fallback on ctx allocation failure and on pre-reqs
744                  * checking above. In the latter scenario we additionally need
745                  * to reset the context to non MPTCP status.
746                  */
747                 if (!ctx || fallback) {
748                         if (fallback_is_fatal) {
749                                 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
750                                 goto dispose_child;
751                         }
752
753                         if (new_msk)
754                                 mptcp_copy_inaddrs(new_msk, child);
755                         subflow_drop_ctx(child);
756                         goto out;
757                 }
758
759                 /* ssk inherits options of listener sk */
760                 ctx->setsockopt_seq = listener->setsockopt_seq;
761
762                 if (ctx->mp_capable) {
763                         /* this can't race with mptcp_close(), as the msk is
764                          * not yet exposted to user-space
765                          */
766                         inet_sk_state_store((void *)new_msk, TCP_ESTABLISHED);
767
768                         /* record the newly created socket as the first msk
769                          * subflow, but don't link it yet into conn_list
770                          */
771                         WRITE_ONCE(mptcp_sk(new_msk)->first, child);
772
773                         /* new mpc subflow takes ownership of the newly
774                          * created mptcp socket
775                          */
776                         mptcp_sk(new_msk)->setsockopt_seq = ctx->setsockopt_seq;
777                         mptcp_pm_new_connection(mptcp_sk(new_msk), child, 1);
778                         mptcp_token_accept(subflow_req, mptcp_sk(new_msk));
779                         ctx->conn = new_msk;
780                         new_msk = NULL;
781
782                         /* set msk addresses early to ensure mptcp_pm_get_local_id()
783                          * uses the correct data
784                          */
785                         mptcp_copy_inaddrs(ctx->conn, child);
786
787                         /* with OoO packets we can reach here without ingress
788                          * mpc option
789                          */
790                         if (mp_opt.suboptions & OPTIONS_MPTCP_MPC)
791                                 mptcp_subflow_fully_established(ctx, &mp_opt);
792                 } else if (ctx->mp_join) {
793                         struct mptcp_sock *owner;
794
795                         owner = subflow_req->msk;
796                         if (!owner) {
797                                 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
798                                 goto dispose_child;
799                         }
800
801                         /* move the msk reference ownership to the subflow */
802                         subflow_req->msk = NULL;
803                         ctx->conn = (struct sock *)owner;
804
805                         if (subflow_use_different_sport(owner, sk)) {
806                                 pr_debug("ack inet_sport=%d %d",
807                                          ntohs(inet_sk(sk)->inet_sport),
808                                          ntohs(inet_sk((struct sock *)owner)->inet_sport));
809                                 if (!mptcp_pm_sport_in_anno_list(owner, sk)) {
810                                         SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX);
811                                         goto dispose_child;
812                                 }
813                                 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX);
814                         }
815
816                         if (!mptcp_finish_join(child))
817                                 goto dispose_child;
818
819                         SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
820                         tcp_rsk(req)->drop_req = true;
821                 }
822         }
823
824 out:
825         /* dispose of the left over mptcp master, if any */
826         if (unlikely(new_msk))
827                 mptcp_force_close(new_msk);
828
829         /* check for expected invariant - should never trigger, just help
830          * catching eariler subtle bugs
831          */
832         WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
833                      (!mptcp_subflow_ctx(child) ||
834                       !mptcp_subflow_ctx(child)->conn));
835         return child;
836
837 dispose_child:
838         subflow_drop_ctx(child);
839         tcp_rsk(req)->drop_req = true;
840         inet_csk_prepare_for_destroy_sock(child);
841         tcp_done(child);
842         req->rsk_ops->send_reset(sk, skb);
843
844         /* The last child reference will be released by the caller */
845         return child;
846 }
847
848 static struct inet_connection_sock_af_ops subflow_specific __ro_after_init;
849 static struct proto tcp_prot_override;
850
851 enum mapping_status {
852         MAPPING_OK,
853         MAPPING_INVALID,
854         MAPPING_EMPTY,
855         MAPPING_DATA_FIN,
856         MAPPING_DUMMY,
857         MAPPING_BAD_CSUM
858 };
859
860 static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
861 {
862         pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d",
863                  ssn, subflow->map_subflow_seq, subflow->map_data_len);
864 }
865
866 static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
867 {
868         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
869         unsigned int skb_consumed;
870
871         skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
872         if (WARN_ON_ONCE(skb_consumed >= skb->len))
873                 return true;
874
875         return skb->len - skb_consumed <= subflow->map_data_len -
876                                           mptcp_subflow_get_map_offset(subflow);
877 }
878
879 static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
880 {
881         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
882         u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
883
884         if (unlikely(before(ssn, subflow->map_subflow_seq))) {
885                 /* Mapping covers data later in the subflow stream,
886                  * currently unsupported.
887                  */
888                 dbg_bad_map(subflow, ssn);
889                 return false;
890         }
891         if (unlikely(!before(ssn, subflow->map_subflow_seq +
892                                   subflow->map_data_len))) {
893                 /* Mapping does covers past subflow data, invalid */
894                 dbg_bad_map(subflow, ssn);
895                 return false;
896         }
897         return true;
898 }
899
900 static enum mapping_status validate_data_csum(struct sock *ssk, struct sk_buff *skb,
901                                               bool csum_reqd)
902 {
903         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
904         u32 offset, seq, delta;
905         __sum16 csum;
906         int len;
907
908         if (!csum_reqd)
909                 return MAPPING_OK;
910
911         /* mapping already validated on previous traversal */
912         if (subflow->map_csum_len == subflow->map_data_len)
913                 return MAPPING_OK;
914
915         /* traverse the receive queue, ensuring it contains a full
916          * DSS mapping and accumulating the related csum.
917          * Preserve the accoumlate csum across multiple calls, to compute
918          * the csum only once
919          */
920         delta = subflow->map_data_len - subflow->map_csum_len;
921         for (;;) {
922                 seq = tcp_sk(ssk)->copied_seq + subflow->map_csum_len;
923                 offset = seq - TCP_SKB_CB(skb)->seq;
924
925                 /* if the current skb has not been accounted yet, csum its contents
926                  * up to the amount covered by the current DSS
927                  */
928                 if (offset < skb->len) {
929                         __wsum csum;
930
931                         len = min(skb->len - offset, delta);
932                         csum = skb_checksum(skb, offset, len, 0);
933                         subflow->map_data_csum = csum_block_add(subflow->map_data_csum, csum,
934                                                                 subflow->map_csum_len);
935
936                         delta -= len;
937                         subflow->map_csum_len += len;
938                 }
939                 if (delta == 0)
940                         break;
941
942                 if (skb_queue_is_last(&ssk->sk_receive_queue, skb)) {
943                         /* if this subflow is closed, the partial mapping
944                          * will be never completed; flush the pending skbs, so
945                          * that subflow_sched_work_if_closed() can kick in
946                          */
947                         if (unlikely(ssk->sk_state == TCP_CLOSE))
948                                 while ((skb = skb_peek(&ssk->sk_receive_queue)))
949                                         sk_eat_skb(ssk, skb);
950
951                         /* not enough data to validate the csum */
952                         return MAPPING_EMPTY;
953                 }
954
955                 /* the DSS mapping for next skbs will be validated later,
956                  * when a get_mapping_status call will process such skb
957                  */
958                 skb = skb->next;
959         }
960
961         /* note that 'map_data_len' accounts only for the carried data, does
962          * not include the eventual seq increment due to the data fin,
963          * while the pseudo header requires the original DSS data len,
964          * including that
965          */
966         csum = __mptcp_make_csum(subflow->map_seq,
967                                  subflow->map_subflow_seq,
968                                  subflow->map_data_len + subflow->map_data_fin,
969                                  subflow->map_data_csum);
970         if (unlikely(csum)) {
971                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DATACSUMERR);
972                 return MAPPING_BAD_CSUM;
973         }
974
975         subflow->valid_csum_seen = 1;
976         return MAPPING_OK;
977 }
978
979 static enum mapping_status get_mapping_status(struct sock *ssk,
980                                               struct mptcp_sock *msk)
981 {
982         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
983         bool csum_reqd = READ_ONCE(msk->csum_enabled);
984         struct mptcp_ext *mpext;
985         struct sk_buff *skb;
986         u16 data_len;
987         u64 map_seq;
988
989         skb = skb_peek(&ssk->sk_receive_queue);
990         if (!skb)
991                 return MAPPING_EMPTY;
992
993         if (mptcp_check_fallback(ssk))
994                 return MAPPING_DUMMY;
995
996         mpext = mptcp_get_ext(skb);
997         if (!mpext || !mpext->use_map) {
998                 if (!subflow->map_valid && !skb->len) {
999                         /* the TCP stack deliver 0 len FIN pkt to the receive
1000                          * queue, that is the only 0len pkts ever expected here,
1001                          * and we can admit no mapping only for 0 len pkts
1002                          */
1003                         if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
1004                                 WARN_ONCE(1, "0len seq %d:%d flags %x",
1005                                           TCP_SKB_CB(skb)->seq,
1006                                           TCP_SKB_CB(skb)->end_seq,
1007                                           TCP_SKB_CB(skb)->tcp_flags);
1008                         sk_eat_skb(ssk, skb);
1009                         return MAPPING_EMPTY;
1010                 }
1011
1012                 if (!subflow->map_valid)
1013                         return MAPPING_INVALID;
1014
1015                 goto validate_seq;
1016         }
1017
1018         trace_get_mapping_status(mpext);
1019
1020         data_len = mpext->data_len;
1021         if (data_len == 0) {
1022                 pr_debug("infinite mapping received");
1023                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
1024                 subflow->map_data_len = 0;
1025                 return MAPPING_INVALID;
1026         }
1027
1028         if (mpext->data_fin == 1) {
1029                 if (data_len == 1) {
1030                         bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
1031                                                                  mpext->dsn64);
1032                         pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
1033                         if (subflow->map_valid) {
1034                                 /* A DATA_FIN might arrive in a DSS
1035                                  * option before the previous mapping
1036                                  * has been fully consumed. Continue
1037                                  * handling the existing mapping.
1038                                  */
1039                                 skb_ext_del(skb, SKB_EXT_MPTCP);
1040                                 return MAPPING_OK;
1041                         } else {
1042                                 if (updated && schedule_work(&msk->work))
1043                                         sock_hold((struct sock *)msk);
1044
1045                                 return MAPPING_DATA_FIN;
1046                         }
1047                 } else {
1048                         u64 data_fin_seq = mpext->data_seq + data_len - 1;
1049
1050                         /* If mpext->data_seq is a 32-bit value, data_fin_seq
1051                          * must also be limited to 32 bits.
1052                          */
1053                         if (!mpext->dsn64)
1054                                 data_fin_seq &= GENMASK_ULL(31, 0);
1055
1056                         mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
1057                         pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d",
1058                                  data_fin_seq, mpext->dsn64);
1059                 }
1060
1061                 /* Adjust for DATA_FIN using 1 byte of sequence space */
1062                 data_len--;
1063         }
1064
1065         map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64);
1066         WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
1067
1068         if (subflow->map_valid) {
1069                 /* Allow replacing only with an identical map */
1070                 if (subflow->map_seq == map_seq &&
1071                     subflow->map_subflow_seq == mpext->subflow_seq &&
1072                     subflow->map_data_len == data_len &&
1073                     subflow->map_csum_reqd == mpext->csum_reqd) {
1074                         skb_ext_del(skb, SKB_EXT_MPTCP);
1075                         goto validate_csum;
1076                 }
1077
1078                 /* If this skb data are fully covered by the current mapping,
1079                  * the new map would need caching, which is not supported
1080                  */
1081                 if (skb_is_fully_mapped(ssk, skb)) {
1082                         MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
1083                         return MAPPING_INVALID;
1084                 }
1085
1086                 /* will validate the next map after consuming the current one */
1087                 goto validate_csum;
1088         }
1089
1090         subflow->map_seq = map_seq;
1091         subflow->map_subflow_seq = mpext->subflow_seq;
1092         subflow->map_data_len = data_len;
1093         subflow->map_valid = 1;
1094         subflow->map_data_fin = mpext->data_fin;
1095         subflow->mpc_map = mpext->mpc_map;
1096         subflow->map_csum_reqd = mpext->csum_reqd;
1097         subflow->map_csum_len = 0;
1098         subflow->map_data_csum = csum_unfold(mpext->csum);
1099
1100         /* Cfr RFC 8684 Section 3.3.0 */
1101         if (unlikely(subflow->map_csum_reqd != csum_reqd))
1102                 return MAPPING_INVALID;
1103
1104         pr_debug("new map seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
1105                  subflow->map_seq, subflow->map_subflow_seq,
1106                  subflow->map_data_len, subflow->map_csum_reqd,
1107                  subflow->map_data_csum);
1108
1109 validate_seq:
1110         /* we revalidate valid mapping on new skb, because we must ensure
1111          * the current skb is completely covered by the available mapping
1112          */
1113         if (!validate_mapping(ssk, skb)) {
1114                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSTCPMISMATCH);
1115                 return MAPPING_INVALID;
1116         }
1117
1118         skb_ext_del(skb, SKB_EXT_MPTCP);
1119
1120 validate_csum:
1121         return validate_data_csum(ssk, skb, csum_reqd);
1122 }
1123
1124 static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
1125                                        u64 limit)
1126 {
1127         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1128         bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
1129         u32 incr;
1130
1131         incr = limit >= skb->len ? skb->len + fin : limit;
1132
1133         pr_debug("discarding=%d len=%d seq=%d", incr, skb->len,
1134                  subflow->map_subflow_seq);
1135         MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
1136         tcp_sk(ssk)->copied_seq += incr;
1137         if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
1138                 sk_eat_skb(ssk, skb);
1139         if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
1140                 subflow->map_valid = 0;
1141 }
1142
1143 /* sched mptcp worker to remove the subflow if no more data is pending */
1144 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
1145 {
1146         struct sock *sk = (struct sock *)msk;
1147
1148         if (likely(ssk->sk_state != TCP_CLOSE))
1149                 return;
1150
1151         if (skb_queue_empty(&ssk->sk_receive_queue) &&
1152             !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) {
1153                 sock_hold(sk);
1154                 if (!schedule_work(&msk->work))
1155                         sock_put(sk);
1156         }
1157 }
1158
1159 static bool subflow_can_fallback(struct mptcp_subflow_context *subflow)
1160 {
1161         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1162
1163         if (subflow->mp_join)
1164                 return false;
1165         else if (READ_ONCE(msk->csum_enabled))
1166                 return !subflow->valid_csum_seen;
1167         else
1168                 return !subflow->fully_established;
1169 }
1170
1171 static void mptcp_subflow_fail(struct mptcp_sock *msk, struct sock *ssk)
1172 {
1173         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1174         unsigned long fail_tout;
1175
1176         /* greceful failure can happen only on the MPC subflow */
1177         if (WARN_ON_ONCE(ssk != READ_ONCE(msk->first)))
1178                 return;
1179
1180         /* since the close timeout take precedence on the fail one,
1181          * no need to start the latter when the first is already set
1182          */
1183         if (sock_flag((struct sock *)msk, SOCK_DEAD))
1184                 return;
1185
1186         /* we don't need extreme accuracy here, use a zero fail_tout as special
1187          * value meaning no fail timeout at all;
1188          */
1189         fail_tout = jiffies + TCP_RTO_MAX;
1190         if (!fail_tout)
1191                 fail_tout = 1;
1192         WRITE_ONCE(subflow->fail_tout, fail_tout);
1193         tcp_send_ack(ssk);
1194
1195         mptcp_reset_timeout(msk, subflow->fail_tout);
1196 }
1197
1198 static bool subflow_check_data_avail(struct sock *ssk)
1199 {
1200         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1201         enum mapping_status status;
1202         struct mptcp_sock *msk;
1203         struct sk_buff *skb;
1204
1205         if (!skb_peek(&ssk->sk_receive_queue))
1206                 WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1207         if (subflow->data_avail)
1208                 return true;
1209
1210         msk = mptcp_sk(subflow->conn);
1211         for (;;) {
1212                 u64 ack_seq;
1213                 u64 old_ack;
1214
1215                 status = get_mapping_status(ssk, msk);
1216                 trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue));
1217                 if (unlikely(status == MAPPING_INVALID || status == MAPPING_DUMMY ||
1218                              status == MAPPING_BAD_CSUM))
1219                         goto fallback;
1220
1221                 if (status != MAPPING_OK)
1222                         goto no_data;
1223
1224                 skb = skb_peek(&ssk->sk_receive_queue);
1225                 if (WARN_ON_ONCE(!skb))
1226                         goto no_data;
1227
1228                 /* if msk lacks the remote key, this subflow must provide an
1229                  * MP_CAPABLE-based mapping
1230                  */
1231                 if (unlikely(!READ_ONCE(msk->can_ack))) {
1232                         if (!subflow->mpc_map)
1233                                 goto fallback;
1234                         WRITE_ONCE(msk->remote_key, subflow->remote_key);
1235                         WRITE_ONCE(msk->ack_seq, subflow->map_seq);
1236                         WRITE_ONCE(msk->can_ack, true);
1237                 }
1238
1239                 old_ack = READ_ONCE(msk->ack_seq);
1240                 ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
1241                 pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack,
1242                          ack_seq);
1243                 if (unlikely(before64(ack_seq, old_ack))) {
1244                         mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
1245                         continue;
1246                 }
1247
1248                 WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1249                 break;
1250         }
1251         return true;
1252
1253 no_data:
1254         subflow_sched_work_if_closed(msk, ssk);
1255         return false;
1256
1257 fallback:
1258         if (!__mptcp_check_fallback(msk)) {
1259                 /* RFC 8684 section 3.7. */
1260                 if (status == MAPPING_BAD_CSUM &&
1261                     (subflow->mp_join || subflow->valid_csum_seen)) {
1262                         subflow->send_mp_fail = 1;
1263
1264                         if (!READ_ONCE(msk->allow_infinite_fallback)) {
1265                                 subflow->reset_transient = 0;
1266                                 subflow->reset_reason = MPTCP_RST_EMIDDLEBOX;
1267                                 goto reset;
1268                         }
1269                         mptcp_subflow_fail(msk, ssk);
1270                         WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1271                         return true;
1272                 }
1273
1274                 if (!subflow_can_fallback(subflow) && subflow->map_data_len) {
1275                         /* fatal protocol error, close the socket.
1276                          * subflow_error_report() will introduce the appropriate barriers
1277                          */
1278                         subflow->reset_transient = 0;
1279                         subflow->reset_reason = MPTCP_RST_EMPTCP;
1280
1281 reset:
1282                         ssk->sk_err = EBADMSG;
1283                         tcp_set_state(ssk, TCP_CLOSE);
1284                         while ((skb = skb_peek(&ssk->sk_receive_queue)))
1285                                 sk_eat_skb(ssk, skb);
1286                         tcp_send_active_reset(ssk, GFP_ATOMIC);
1287                         WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1288                         return false;
1289                 }
1290
1291                 mptcp_do_fallback(ssk);
1292         }
1293
1294         skb = skb_peek(&ssk->sk_receive_queue);
1295         subflow->map_valid = 1;
1296         subflow->map_seq = READ_ONCE(msk->ack_seq);
1297         subflow->map_data_len = skb->len;
1298         subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
1299         WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1300         return true;
1301 }
1302
1303 bool mptcp_subflow_data_available(struct sock *sk)
1304 {
1305         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1306
1307         /* check if current mapping is still valid */
1308         if (subflow->map_valid &&
1309             mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
1310                 subflow->map_valid = 0;
1311                 WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1312
1313                 pr_debug("Done with mapping: seq=%u data_len=%u",
1314                          subflow->map_subflow_seq,
1315                          subflow->map_data_len);
1316         }
1317
1318         return subflow_check_data_avail(sk);
1319 }
1320
1321 /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
1322  * not the ssk one.
1323  *
1324  * In mptcp, rwin is about the mptcp-level connection data.
1325  *
1326  * Data that is still on the ssk rx queue can thus be ignored,
1327  * as far as mptcp peer is concerned that data is still inflight.
1328  * DSS ACK is updated when skb is moved to the mptcp rx queue.
1329  */
1330 void mptcp_space(const struct sock *ssk, int *space, int *full_space)
1331 {
1332         const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1333         const struct sock *sk = subflow->conn;
1334
1335         *space = __mptcp_space(sk);
1336         *full_space = tcp_full_space(sk);
1337 }
1338
1339 void __mptcp_error_report(struct sock *sk)
1340 {
1341         struct mptcp_subflow_context *subflow;
1342         struct mptcp_sock *msk = mptcp_sk(sk);
1343
1344         mptcp_for_each_subflow(msk, subflow) {
1345                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1346                 int err = sock_error(ssk);
1347
1348                 if (!err)
1349                         continue;
1350
1351                 /* only propagate errors on fallen-back sockets or
1352                  * on MPC connect
1353                  */
1354                 if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk))
1355                         continue;
1356
1357                 inet_sk_state_store(sk, inet_sk_state_load(ssk));
1358                 sk->sk_err = -err;
1359
1360                 /* This barrier is coupled with smp_rmb() in mptcp_poll() */
1361                 smp_wmb();
1362                 sk_error_report(sk);
1363                 break;
1364         }
1365 }
1366
1367 static void subflow_error_report(struct sock *ssk)
1368 {
1369         struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1370
1371         mptcp_data_lock(sk);
1372         if (!sock_owned_by_user(sk))
1373                 __mptcp_error_report(sk);
1374         else
1375                 __set_bit(MPTCP_ERROR_REPORT,  &mptcp_sk(sk)->cb_flags);
1376         mptcp_data_unlock(sk);
1377 }
1378
1379 static void subflow_data_ready(struct sock *sk)
1380 {
1381         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1382         u16 state = 1 << inet_sk_state_load(sk);
1383         struct sock *parent = subflow->conn;
1384         struct mptcp_sock *msk;
1385
1386         msk = mptcp_sk(parent);
1387         if (state & TCPF_LISTEN) {
1388                 /* MPJ subflow are removed from accept queue before reaching here,
1389                  * avoid stray wakeups
1390                  */
1391                 if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
1392                         return;
1393
1394                 parent->sk_data_ready(parent);
1395                 return;
1396         }
1397
1398         WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
1399                      !subflow->mp_join && !(state & TCPF_CLOSE));
1400
1401         if (mptcp_subflow_data_available(sk))
1402                 mptcp_data_ready(parent, sk);
1403         else if (unlikely(sk->sk_err))
1404                 subflow_error_report(sk);
1405 }
1406
1407 static void subflow_write_space(struct sock *ssk)
1408 {
1409         struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1410
1411         mptcp_propagate_sndbuf(sk, ssk);
1412         mptcp_write_space(sk);
1413 }
1414
1415 static const struct inet_connection_sock_af_ops *
1416 subflow_default_af_ops(struct sock *sk)
1417 {
1418 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1419         if (sk->sk_family == AF_INET6)
1420                 return &subflow_v6_specific;
1421 #endif
1422         return &subflow_specific;
1423 }
1424
1425 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1426 void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
1427 {
1428         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1429         struct inet_connection_sock *icsk = inet_csk(sk);
1430         const struct inet_connection_sock_af_ops *target;
1431
1432         target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
1433
1434         pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d",
1435                  subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
1436
1437         if (likely(icsk->icsk_af_ops == target))
1438                 return;
1439
1440         subflow->icsk_af_ops = icsk->icsk_af_ops;
1441         icsk->icsk_af_ops = target;
1442 }
1443 #endif
1444
1445 void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
1446                          struct sockaddr_storage *addr,
1447                          unsigned short family)
1448 {
1449         memset(addr, 0, sizeof(*addr));
1450         addr->ss_family = family;
1451         if (addr->ss_family == AF_INET) {
1452                 struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
1453
1454                 if (info->family == AF_INET)
1455                         in_addr->sin_addr = info->addr;
1456 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1457                 else if (ipv6_addr_v4mapped(&info->addr6))
1458                         in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3];
1459 #endif
1460                 in_addr->sin_port = info->port;
1461         }
1462 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1463         else if (addr->ss_family == AF_INET6) {
1464                 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;
1465
1466                 if (info->family == AF_INET)
1467                         ipv6_addr_set_v4mapped(info->addr.s_addr,
1468                                                &in6_addr->sin6_addr);
1469                 else
1470                         in6_addr->sin6_addr = info->addr6;
1471                 in6_addr->sin6_port = info->port;
1472         }
1473 #endif
1474 }
1475
1476 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
1477                             const struct mptcp_addr_info *remote)
1478 {
1479         struct mptcp_sock *msk = mptcp_sk(sk);
1480         struct mptcp_subflow_context *subflow;
1481         struct sockaddr_storage addr;
1482         int remote_id = remote->id;
1483         int local_id = loc->id;
1484         int err = -ENOTCONN;
1485         struct socket *sf;
1486         struct sock *ssk;
1487         u32 remote_token;
1488         int addrlen;
1489         int ifindex;
1490         u8 flags;
1491
1492         if (!mptcp_is_fully_established(sk))
1493                 goto err_out;
1494
1495         err = mptcp_subflow_create_socket(sk, &sf);
1496         if (err)
1497                 goto err_out;
1498
1499         ssk = sf->sk;
1500         subflow = mptcp_subflow_ctx(ssk);
1501         do {
1502                 get_random_bytes(&subflow->local_nonce, sizeof(u32));
1503         } while (!subflow->local_nonce);
1504
1505         if (local_id)
1506                 subflow_set_local_id(subflow, local_id);
1507
1508         mptcp_pm_get_flags_and_ifindex_by_id(msk, local_id,
1509                                              &flags, &ifindex);
1510         subflow->remote_key = msk->remote_key;
1511         subflow->local_key = msk->local_key;
1512         subflow->token = msk->token;
1513         mptcp_info2sockaddr(loc, &addr, ssk->sk_family);
1514
1515         addrlen = sizeof(struct sockaddr_in);
1516 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1517         if (addr.ss_family == AF_INET6)
1518                 addrlen = sizeof(struct sockaddr_in6);
1519 #endif
1520         mptcp_sockopt_sync(msk, ssk);
1521
1522         ssk->sk_bound_dev_if = ifindex;
1523         err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
1524         if (err)
1525                 goto failed;
1526
1527         mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1528         pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk,
1529                  remote_token, local_id, remote_id);
1530         subflow->remote_token = remote_token;
1531         subflow->remote_id = remote_id;
1532         subflow->request_join = 1;
1533         subflow->request_bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1534         mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
1535
1536         sock_hold(ssk);
1537         list_add_tail(&subflow->node, &msk->conn_list);
1538         err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
1539         if (err && err != -EINPROGRESS)
1540                 goto failed_unlink;
1541
1542         /* discard the subflow socket */
1543         mptcp_sock_graft(ssk, sk->sk_socket);
1544         iput(SOCK_INODE(sf));
1545         WRITE_ONCE(msk->allow_infinite_fallback, false);
1546         return 0;
1547
1548 failed_unlink:
1549         list_del(&subflow->node);
1550         sock_put(mptcp_subflow_tcp_sock(subflow));
1551
1552 failed:
1553         subflow->disposable = 1;
1554         sock_release(sf);
1555
1556 err_out:
1557         /* we account subflows before the creation, and this failures will not
1558          * be caught by sk_state_change()
1559          */
1560         mptcp_pm_close_subflow(msk);
1561         return err;
1562 }
1563
1564 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
1565 {
1566 #ifdef CONFIG_SOCK_CGROUP_DATA
1567         struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
1568                                 *child_skcd = &child->sk_cgrp_data;
1569
1570         /* only the additional subflows created by kworkers have to be modified */
1571         if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
1572             cgroup_id(sock_cgroup_ptr(child_skcd))) {
1573 #ifdef CONFIG_MEMCG
1574                 struct mem_cgroup *memcg = parent->sk_memcg;
1575
1576                 mem_cgroup_sk_free(child);
1577                 if (memcg && css_tryget(&memcg->css))
1578                         child->sk_memcg = memcg;
1579 #endif /* CONFIG_MEMCG */
1580
1581                 cgroup_sk_free(child_skcd);
1582                 *child_skcd = *parent_skcd;
1583                 cgroup_sk_clone(child_skcd);
1584         }
1585 #endif /* CONFIG_SOCK_CGROUP_DATA */
1586 }
1587
1588 static void mptcp_subflow_ops_override(struct sock *ssk)
1589 {
1590 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1591         if (ssk->sk_prot == &tcpv6_prot)
1592                 ssk->sk_prot = &tcpv6_prot_override;
1593         else
1594 #endif
1595                 ssk->sk_prot = &tcp_prot_override;
1596 }
1597
1598 static void mptcp_subflow_ops_undo_override(struct sock *ssk)
1599 {
1600 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1601         if (ssk->sk_prot == &tcpv6_prot_override)
1602                 ssk->sk_prot = &tcpv6_prot;
1603         else
1604 #endif
1605                 ssk->sk_prot = &tcp_prot;
1606 }
1607 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)
1608 {
1609         struct mptcp_subflow_context *subflow;
1610         struct net *net = sock_net(sk);
1611         struct socket *sf;
1612         int err;
1613
1614         /* un-accepted server sockets can reach here - on bad configuration
1615          * bail early to avoid greater trouble later
1616          */
1617         if (unlikely(!sk->sk_socket))
1618                 return -EINVAL;
1619
1620         err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP,
1621                                &sf);
1622         if (err)
1623                 return err;
1624
1625         lock_sock(sf->sk);
1626
1627         /* the newly created socket has to be in the same cgroup as its parent */
1628         mptcp_attach_cgroup(sk, sf->sk);
1629
1630         /* kernel sockets do not by default acquire net ref, but TCP timer
1631          * needs it.
1632          */
1633         sf->sk->sk_net_refcnt = 1;
1634         get_net_track(net, &sf->sk->ns_tracker, GFP_KERNEL);
1635         sock_inuse_add(net, 1);
1636         err = tcp_set_ulp(sf->sk, "mptcp");
1637         release_sock(sf->sk);
1638
1639         if (err) {
1640                 sock_release(sf);
1641                 return err;
1642         }
1643
1644         /* the newly created socket really belongs to the owning MPTCP master
1645          * socket, even if for additional subflows the allocation is performed
1646          * by a kernel workqueue. Adjust inode references, so that the
1647          * procfs/diag interfaces really show this one belonging to the correct
1648          * user.
1649          */
1650         SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
1651         SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
1652         SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
1653
1654         subflow = mptcp_subflow_ctx(sf->sk);
1655         pr_debug("subflow=%p", subflow);
1656
1657         *new_sock = sf;
1658         sock_hold(sk);
1659         subflow->conn = sk;
1660         mptcp_subflow_ops_override(sf->sk);
1661
1662         return 0;
1663 }
1664
1665 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
1666                                                         gfp_t priority)
1667 {
1668         struct inet_connection_sock *icsk = inet_csk(sk);
1669         struct mptcp_subflow_context *ctx;
1670
1671         ctx = kzalloc(sizeof(*ctx), priority);
1672         if (!ctx)
1673                 return NULL;
1674
1675         rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
1676         INIT_LIST_HEAD(&ctx->node);
1677         INIT_LIST_HEAD(&ctx->delegated_node);
1678
1679         pr_debug("subflow=%p", ctx);
1680
1681         ctx->tcp_sock = sk;
1682
1683         return ctx;
1684 }
1685
1686 static void __subflow_state_change(struct sock *sk)
1687 {
1688         struct socket_wq *wq;
1689
1690         rcu_read_lock();
1691         wq = rcu_dereference(sk->sk_wq);
1692         if (skwq_has_sleeper(wq))
1693                 wake_up_interruptible_all(&wq->wait);
1694         rcu_read_unlock();
1695 }
1696
1697 static bool subflow_is_done(const struct sock *sk)
1698 {
1699         return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
1700 }
1701
1702 static void subflow_state_change(struct sock *sk)
1703 {
1704         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1705         struct sock *parent = subflow->conn;
1706
1707         __subflow_state_change(sk);
1708
1709         if (subflow_simultaneous_connect(sk)) {
1710                 mptcp_propagate_sndbuf(parent, sk);
1711                 mptcp_do_fallback(sk);
1712                 mptcp_rcv_space_init(mptcp_sk(parent), sk);
1713                 pr_fallback(mptcp_sk(parent));
1714                 subflow->conn_finished = 1;
1715                 mptcp_set_connected(parent);
1716         }
1717
1718         /* as recvmsg() does not acquire the subflow socket for ssk selection
1719          * a fin packet carrying a DSS can be unnoticed if we don't trigger
1720          * the data available machinery here.
1721          */
1722         if (mptcp_subflow_data_available(sk))
1723                 mptcp_data_ready(parent, sk);
1724         else if (unlikely(sk->sk_err))
1725                 subflow_error_report(sk);
1726
1727         subflow_sched_work_if_closed(mptcp_sk(parent), sk);
1728
1729         if (__mptcp_check_fallback(mptcp_sk(parent)) &&
1730             !subflow->rx_eof && subflow_is_done(sk)) {
1731                 subflow->rx_eof = 1;
1732                 mptcp_subflow_eof(parent);
1733         }
1734 }
1735
1736 void mptcp_subflow_queue_clean(struct sock *listener_ssk)
1737 {
1738         struct request_sock_queue *queue = &inet_csk(listener_ssk)->icsk_accept_queue;
1739         struct mptcp_sock *msk, *next, *head = NULL;
1740         struct request_sock *req;
1741
1742         /* build a list of all unaccepted mptcp sockets */
1743         spin_lock_bh(&queue->rskq_lock);
1744         for (req = queue->rskq_accept_head; req; req = req->dl_next) {
1745                 struct mptcp_subflow_context *subflow;
1746                 struct sock *ssk = req->sk;
1747                 struct mptcp_sock *msk;
1748
1749                 if (!sk_is_mptcp(ssk))
1750                         continue;
1751
1752                 subflow = mptcp_subflow_ctx(ssk);
1753                 if (!subflow || !subflow->conn)
1754                         continue;
1755
1756                 /* skip if already in list */
1757                 msk = mptcp_sk(subflow->conn);
1758                 if (msk->dl_next || msk == head)
1759                         continue;
1760
1761                 msk->dl_next = head;
1762                 head = msk;
1763         }
1764         spin_unlock_bh(&queue->rskq_lock);
1765         if (!head)
1766                 return;
1767
1768         /* can't acquire the msk socket lock under the subflow one,
1769          * or will cause ABBA deadlock
1770          */
1771         release_sock(listener_ssk);
1772
1773         for (msk = head; msk; msk = next) {
1774                 struct sock *sk = (struct sock *)msk;
1775                 bool do_cancel_work;
1776
1777                 sock_hold(sk);
1778                 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1779                 next = msk->dl_next;
1780                 msk->first = NULL;
1781                 msk->dl_next = NULL;
1782
1783                 do_cancel_work = __mptcp_close(sk, 0);
1784                 release_sock(sk);
1785                 if (do_cancel_work)
1786                         mptcp_cancel_work(sk);
1787                 sock_put(sk);
1788         }
1789
1790         /* we are still under the listener msk socket lock */
1791         lock_sock_nested(listener_ssk, SINGLE_DEPTH_NESTING);
1792 }
1793
1794 static int subflow_ulp_init(struct sock *sk)
1795 {
1796         struct inet_connection_sock *icsk = inet_csk(sk);
1797         struct mptcp_subflow_context *ctx;
1798         struct tcp_sock *tp = tcp_sk(sk);
1799         int err = 0;
1800
1801         /* disallow attaching ULP to a socket unless it has been
1802          * created with sock_create_kern()
1803          */
1804         if (!sk->sk_kern_sock) {
1805                 err = -EOPNOTSUPP;
1806                 goto out;
1807         }
1808
1809         ctx = subflow_create_ctx(sk, GFP_KERNEL);
1810         if (!ctx) {
1811                 err = -ENOMEM;
1812                 goto out;
1813         }
1814
1815         pr_debug("subflow=%p, family=%d", ctx, sk->sk_family);
1816
1817         tp->is_mptcp = 1;
1818         ctx->icsk_af_ops = icsk->icsk_af_ops;
1819         icsk->icsk_af_ops = subflow_default_af_ops(sk);
1820         ctx->tcp_state_change = sk->sk_state_change;
1821         ctx->tcp_error_report = sk->sk_error_report;
1822
1823         WARN_ON_ONCE(sk->sk_data_ready != sock_def_readable);
1824         WARN_ON_ONCE(sk->sk_write_space != sk_stream_write_space);
1825
1826         sk->sk_data_ready = subflow_data_ready;
1827         sk->sk_write_space = subflow_write_space;
1828         sk->sk_state_change = subflow_state_change;
1829         sk->sk_error_report = subflow_error_report;
1830 out:
1831         return err;
1832 }
1833
1834 static void subflow_ulp_release(struct sock *ssk)
1835 {
1836         struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
1837         bool release = true;
1838         struct sock *sk;
1839
1840         if (!ctx)
1841                 return;
1842
1843         sk = ctx->conn;
1844         if (sk) {
1845                 /* if the msk has been orphaned, keep the ctx
1846                  * alive, will be freed by __mptcp_close_ssk(),
1847                  * when the subflow is still unaccepted
1848                  */
1849                 release = ctx->disposable || list_empty(&ctx->node);
1850                 sock_put(sk);
1851         }
1852
1853         mptcp_subflow_ops_undo_override(ssk);
1854         if (release)
1855                 kfree_rcu(ctx, rcu);
1856 }
1857
1858 static void subflow_ulp_clone(const struct request_sock *req,
1859                               struct sock *newsk,
1860                               const gfp_t priority)
1861 {
1862         struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
1863         struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
1864         struct mptcp_subflow_context *new_ctx;
1865
1866         if (!tcp_rsk(req)->is_mptcp ||
1867             (!subflow_req->mp_capable && !subflow_req->mp_join)) {
1868                 subflow_ulp_fallback(newsk, old_ctx);
1869                 return;
1870         }
1871
1872         new_ctx = subflow_create_ctx(newsk, priority);
1873         if (!new_ctx) {
1874                 subflow_ulp_fallback(newsk, old_ctx);
1875                 return;
1876         }
1877
1878         new_ctx->conn_finished = 1;
1879         new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
1880         new_ctx->tcp_state_change = old_ctx->tcp_state_change;
1881         new_ctx->tcp_error_report = old_ctx->tcp_error_report;
1882         new_ctx->rel_write_seq = 1;
1883         new_ctx->tcp_sock = newsk;
1884
1885         if (subflow_req->mp_capable) {
1886                 /* see comments in subflow_syn_recv_sock(), MPTCP connection
1887                  * is fully established only after we receive the remote key
1888                  */
1889                 new_ctx->mp_capable = 1;
1890                 new_ctx->local_key = subflow_req->local_key;
1891                 new_ctx->token = subflow_req->token;
1892                 new_ctx->ssn_offset = subflow_req->ssn_offset;
1893                 new_ctx->idsn = subflow_req->idsn;
1894
1895                 /* this is the first subflow, id is always 0 */
1896                 new_ctx->local_id_valid = 1;
1897         } else if (subflow_req->mp_join) {
1898                 new_ctx->ssn_offset = subflow_req->ssn_offset;
1899                 new_ctx->mp_join = 1;
1900                 new_ctx->fully_established = 1;
1901                 new_ctx->backup = subflow_req->backup;
1902                 new_ctx->remote_id = subflow_req->remote_id;
1903                 new_ctx->token = subflow_req->token;
1904                 new_ctx->thmac = subflow_req->thmac;
1905
1906                 /* the subflow req id is valid, fetched via subflow_check_req()
1907                  * and subflow_token_join_request()
1908                  */
1909                 subflow_set_local_id(new_ctx, subflow_req->local_id);
1910         }
1911 }
1912
1913 static void tcp_release_cb_override(struct sock *ssk)
1914 {
1915         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1916
1917         if (mptcp_subflow_has_delegated_action(subflow))
1918                 mptcp_subflow_process_delegated(ssk);
1919
1920         tcp_release_cb(ssk);
1921 }
1922
1923 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
1924         .name           = "mptcp",
1925         .owner          = THIS_MODULE,
1926         .init           = subflow_ulp_init,
1927         .release        = subflow_ulp_release,
1928         .clone          = subflow_ulp_clone,
1929 };
1930
1931 static int subflow_ops_init(struct request_sock_ops *subflow_ops)
1932 {
1933         subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
1934
1935         subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
1936                                               subflow_ops->obj_size, 0,
1937                                               SLAB_ACCOUNT |
1938                                               SLAB_TYPESAFE_BY_RCU,
1939                                               NULL);
1940         if (!subflow_ops->slab)
1941                 return -ENOMEM;
1942
1943         return 0;
1944 }
1945
1946 void __init mptcp_subflow_init(void)
1947 {
1948         mptcp_subflow_v4_request_sock_ops = tcp_request_sock_ops;
1949         mptcp_subflow_v4_request_sock_ops.slab_name = "request_sock_subflow_v4";
1950         mptcp_subflow_v4_request_sock_ops.destructor = subflow_v4_req_destructor;
1951
1952         if (subflow_ops_init(&mptcp_subflow_v4_request_sock_ops) != 0)
1953                 panic("MPTCP: failed to init subflow v4 request sock ops\n");
1954
1955         subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
1956         subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
1957
1958         subflow_specific = ipv4_specific;
1959         subflow_specific.conn_request = subflow_v4_conn_request;
1960         subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
1961         subflow_specific.sk_rx_dst_set = subflow_finish_connect;
1962         subflow_specific.rebuild_header = subflow_rebuild_header;
1963
1964         tcp_prot_override = tcp_prot;
1965         tcp_prot_override.release_cb = tcp_release_cb_override;
1966
1967 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1968         /* In struct mptcp_subflow_request_sock, we assume the TCP request sock
1969          * structures for v4 and v6 have the same size. It should not changed in
1970          * the future but better to make sure to be warned if it is no longer
1971          * the case.
1972          */
1973         BUILD_BUG_ON(sizeof(struct tcp_request_sock) != sizeof(struct tcp6_request_sock));
1974
1975         mptcp_subflow_v6_request_sock_ops = tcp6_request_sock_ops;
1976         mptcp_subflow_v6_request_sock_ops.slab_name = "request_sock_subflow_v6";
1977         mptcp_subflow_v6_request_sock_ops.destructor = subflow_v6_req_destructor;
1978
1979         if (subflow_ops_init(&mptcp_subflow_v6_request_sock_ops) != 0)
1980                 panic("MPTCP: failed to init subflow v6 request sock ops\n");
1981
1982         subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
1983         subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
1984
1985         subflow_v6_specific = ipv6_specific;
1986         subflow_v6_specific.conn_request = subflow_v6_conn_request;
1987         subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
1988         subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
1989         subflow_v6_specific.rebuild_header = subflow_v6_rebuild_header;
1990
1991         subflow_v6m_specific = subflow_v6_specific;
1992         subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
1993         subflow_v6m_specific.send_check = ipv4_specific.send_check;
1994         subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
1995         subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
1996         subflow_v6m_specific.net_frag_header_len = 0;
1997         subflow_v6m_specific.rebuild_header = subflow_rebuild_header;
1998
1999         tcpv6_prot_override = tcpv6_prot;
2000         tcpv6_prot_override.release_cb = tcp_release_cb_override;
2001 #endif
2002
2003         mptcp_diag_subflow_init(&subflow_ulp_ops);
2004
2005         if (tcp_register_ulp(&subflow_ulp_ops) != 0)
2006                 panic("MPTCP: failed to register subflows to ULP\n");
2007 }