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