36b15726f85112cc1c34f6fe58bb91ce50b573a2
[linux-2.6-microblaze.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 static void mptcp_subflow_ops_undo_override(struct sock *ssk);
29
30 static void SUBFLOW_REQ_INC_STATS(struct request_sock *req,
31                                   enum linux_mptcp_mib_field field)
32 {
33         MPTCP_INC_STATS(sock_net(req_to_sk(req)), field);
34 }
35
36 static void subflow_req_destructor(struct request_sock *req)
37 {
38         struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
39
40         pr_debug("subflow_req=%p", subflow_req);
41
42         if (subflow_req->msk)
43                 sock_put((struct sock *)subflow_req->msk);
44
45         mptcp_token_destroy_request(req);
46         tcp_request_sock_ops.destructor(req);
47 }
48
49 static void subflow_generate_hmac(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
50                                   void *hmac)
51 {
52         u8 msg[8];
53
54         put_unaligned_be32(nonce1, &msg[0]);
55         put_unaligned_be32(nonce2, &msg[4]);
56
57         mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
58 }
59
60 static bool mptcp_can_accept_new_subflow(const struct mptcp_sock *msk)
61 {
62         return mptcp_is_fully_established((void *)msk) &&
63                READ_ONCE(msk->pm.accept_subflow);
64 }
65
66 /* validate received token and create truncated hmac and nonce for SYN-ACK */
67 static void subflow_req_create_thmac(struct mptcp_subflow_request_sock *subflow_req)
68 {
69         struct mptcp_sock *msk = subflow_req->msk;
70         u8 hmac[SHA256_DIGEST_SIZE];
71
72         get_random_bytes(&subflow_req->local_nonce, sizeof(u32));
73
74         subflow_generate_hmac(msk->local_key, msk->remote_key,
75                               subflow_req->local_nonce,
76                               subflow_req->remote_nonce, hmac);
77
78         subflow_req->thmac = get_unaligned_be64(hmac);
79 }
80
81 static struct mptcp_sock *subflow_token_join_request(struct request_sock *req)
82 {
83         struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
84         struct mptcp_sock *msk;
85         int local_id;
86
87         msk = mptcp_token_get_sock(subflow_req->token);
88         if (!msk) {
89                 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN);
90                 return NULL;
91         }
92
93         local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req);
94         if (local_id < 0) {
95                 sock_put((struct sock *)msk);
96                 return NULL;
97         }
98         subflow_req->local_id = local_id;
99
100         return msk;
101 }
102
103 static int __subflow_init_req(struct request_sock *req, const struct sock *sk_listener)
104 {
105         struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
106
107         subflow_req->mp_capable = 0;
108         subflow_req->mp_join = 0;
109         subflow_req->msk = NULL;
110         mptcp_token_init_request(req);
111
112 #ifdef CONFIG_TCP_MD5SIG
113         /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
114          * TCP option space.
115          */
116         if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info))
117                 return -EINVAL;
118 #endif
119
120         return 0;
121 }
122
123 static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk)
124 {
125         return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport;
126 }
127
128 /* Init mptcp request socket.
129  *
130  * Returns an error code if a JOIN has failed and a TCP reset
131  * should be sent.
132  */
133 static int subflow_init_req(struct request_sock *req,
134                             const struct sock *sk_listener,
135                             struct sk_buff *skb)
136 {
137         struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
138         struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
139         struct mptcp_options_received mp_opt;
140         int ret;
141
142         pr_debug("subflow_req=%p, listener=%p", subflow_req, listener);
143
144         ret = __subflow_init_req(req, sk_listener);
145         if (ret)
146                 return 0;
147
148         mptcp_get_options(skb, &mp_opt);
149
150         if (mp_opt.mp_capable) {
151                 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE);
152
153                 if (mp_opt.mp_join)
154                         return 0;
155         } else if (mp_opt.mp_join) {
156                 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX);
157         }
158
159         if (mp_opt.mp_capable && listener->request_mptcp) {
160                 int err, retries = 4;
161
162                 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
163 again:
164                 do {
165                         get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key));
166                 } while (subflow_req->local_key == 0);
167
168                 if (unlikely(req->syncookie)) {
169                         mptcp_crypto_key_sha(subflow_req->local_key,
170                                              &subflow_req->token,
171                                              &subflow_req->idsn);
172                         if (mptcp_token_exists(subflow_req->token)) {
173                                 if (retries-- > 0)
174                                         goto again;
175                         } else {
176                                 subflow_req->mp_capable = 1;
177                         }
178                         return 0;
179                 }
180
181                 err = mptcp_token_new_request(req);
182                 if (err == 0)
183                         subflow_req->mp_capable = 1;
184                 else if (retries-- > 0)
185                         goto again;
186
187         } else if (mp_opt.mp_join && listener->request_mptcp) {
188                 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
189                 subflow_req->mp_join = 1;
190                 subflow_req->backup = mp_opt.backup;
191                 subflow_req->remote_id = mp_opt.join_id;
192                 subflow_req->token = mp_opt.token;
193                 subflow_req->remote_nonce = mp_opt.nonce;
194                 subflow_req->msk = subflow_token_join_request(req);
195
196                 /* Can't fall back to TCP in this case. */
197                 if (!subflow_req->msk)
198                         return -EPERM;
199
200                 if (subflow_use_different_sport(subflow_req->msk, sk_listener)) {
201                         pr_debug("syn inet_sport=%d %d",
202                                  ntohs(inet_sk(sk_listener)->inet_sport),
203                                  ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport));
204                         if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener)) {
205                                 sock_put((struct sock *)subflow_req->msk);
206                                 mptcp_token_destroy_request(req);
207                                 tcp_request_sock_ops.destructor(req);
208                                 subflow_req->msk = NULL;
209                                 subflow_req->mp_join = 0;
210                                 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX);
211                                 return -EPERM;
212                         }
213                         SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX);
214                 }
215
216                 subflow_req_create_thmac(subflow_req);
217
218                 if (unlikely(req->syncookie)) {
219                         if (mptcp_can_accept_new_subflow(subflow_req->msk))
220                                 subflow_init_req_cookie_join_save(subflow_req, skb);
221                 }
222
223                 pr_debug("token=%u, remote_nonce=%u msk=%p", subflow_req->token,
224                          subflow_req->remote_nonce, subflow_req->msk);
225         }
226
227         return 0;
228 }
229
230 int mptcp_subflow_init_cookie_req(struct request_sock *req,
231                                   const struct sock *sk_listener,
232                                   struct sk_buff *skb)
233 {
234         struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
235         struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
236         struct mptcp_options_received mp_opt;
237         int err;
238
239         err = __subflow_init_req(req, sk_listener);
240         if (err)
241                 return err;
242
243         mptcp_get_options(skb, &mp_opt);
244
245         if (mp_opt.mp_capable && mp_opt.mp_join)
246                 return -EINVAL;
247
248         if (mp_opt.mp_capable && listener->request_mptcp) {
249                 if (mp_opt.sndr_key == 0)
250                         return -EINVAL;
251
252                 subflow_req->local_key = mp_opt.rcvr_key;
253                 err = mptcp_token_new_request(req);
254                 if (err)
255                         return err;
256
257                 subflow_req->mp_capable = 1;
258                 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
259         } else if (mp_opt.mp_join && listener->request_mptcp) {
260                 if (!mptcp_token_join_cookie_init_state(subflow_req, skb))
261                         return -EINVAL;
262
263                 if (mptcp_can_accept_new_subflow(subflow_req->msk))
264                         subflow_req->mp_join = 1;
265
266                 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
267         }
268
269         return 0;
270 }
271 EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req);
272
273 static struct dst_entry *subflow_v4_route_req(const struct sock *sk,
274                                               struct sk_buff *skb,
275                                               struct flowi *fl,
276                                               struct request_sock *req)
277 {
278         struct dst_entry *dst;
279         int err;
280
281         tcp_rsk(req)->is_mptcp = 1;
282
283         dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req);
284         if (!dst)
285                 return NULL;
286
287         err = subflow_init_req(req, sk, skb);
288         if (err == 0)
289                 return dst;
290
291         dst_release(dst);
292         if (!req->syncookie)
293                 tcp_request_sock_ops.send_reset(sk, skb);
294         return NULL;
295 }
296
297 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
298 static struct dst_entry *subflow_v6_route_req(const struct sock *sk,
299                                               struct sk_buff *skb,
300                                               struct flowi *fl,
301                                               struct request_sock *req)
302 {
303         struct dst_entry *dst;
304         int err;
305
306         tcp_rsk(req)->is_mptcp = 1;
307
308         dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req);
309         if (!dst)
310                 return NULL;
311
312         err = subflow_init_req(req, sk, skb);
313         if (err == 0)
314                 return dst;
315
316         dst_release(dst);
317         if (!req->syncookie)
318                 tcp6_request_sock_ops.send_reset(sk, skb);
319         return NULL;
320 }
321 #endif
322
323 /* validate received truncated hmac and create hmac for third ACK */
324 static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
325 {
326         u8 hmac[SHA256_DIGEST_SIZE];
327         u64 thmac;
328
329         subflow_generate_hmac(subflow->remote_key, subflow->local_key,
330                               subflow->remote_nonce, subflow->local_nonce,
331                               hmac);
332
333         thmac = get_unaligned_be64(hmac);
334         pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n",
335                  subflow, subflow->token,
336                  (unsigned long long)thmac,
337                  (unsigned long long)subflow->thmac);
338
339         return thmac == subflow->thmac;
340 }
341
342 void mptcp_subflow_reset(struct sock *ssk)
343 {
344         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
345         struct sock *sk = subflow->conn;
346
347         /* must hold: tcp_done() could drop last reference on parent */
348         sock_hold(sk);
349
350         tcp_set_state(ssk, TCP_CLOSE);
351         tcp_send_active_reset(ssk, GFP_ATOMIC);
352         tcp_done(ssk);
353         if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags) &&
354             schedule_work(&mptcp_sk(sk)->work))
355                 return; /* worker will put sk for us */
356
357         sock_put(sk);
358 }
359
360 static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk)
361 {
362         return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport;
363 }
364
365 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
366 {
367         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
368         struct mptcp_options_received mp_opt;
369         struct sock *parent = subflow->conn;
370
371         subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);
372
373         if (inet_sk_state_load(parent) == TCP_SYN_SENT) {
374                 inet_sk_state_store(parent, TCP_ESTABLISHED);
375                 parent->sk_state_change(parent);
376         }
377
378         /* be sure no special action on any packet other than syn-ack */
379         if (subflow->conn_finished)
380                 return;
381
382         mptcp_propagate_sndbuf(parent, sk);
383         subflow->rel_write_seq = 1;
384         subflow->conn_finished = 1;
385         subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
386         pr_debug("subflow=%p synack seq=%x", subflow, subflow->ssn_offset);
387
388         mptcp_get_options(skb, &mp_opt);
389         if (subflow->request_mptcp) {
390                 if (!mp_opt.mp_capable) {
391                         MPTCP_INC_STATS(sock_net(sk),
392                                         MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
393                         mptcp_do_fallback(sk);
394                         pr_fallback(mptcp_sk(subflow->conn));
395                         goto fallback;
396                 }
397
398                 subflow->mp_capable = 1;
399                 subflow->can_ack = 1;
400                 subflow->remote_key = mp_opt.sndr_key;
401                 pr_debug("subflow=%p, remote_key=%llu", subflow,
402                          subflow->remote_key);
403                 mptcp_finish_connect(sk);
404         } else if (subflow->request_join) {
405                 u8 hmac[SHA256_DIGEST_SIZE];
406
407                 if (!mp_opt.mp_join)
408                         goto do_reset;
409
410                 subflow->thmac = mp_opt.thmac;
411                 subflow->remote_nonce = mp_opt.nonce;
412                 pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u", subflow,
413                          subflow->thmac, subflow->remote_nonce);
414
415                 if (!subflow_thmac_valid(subflow)) {
416                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC);
417                         goto do_reset;
418                 }
419
420                 subflow_generate_hmac(subflow->local_key, subflow->remote_key,
421                                       subflow->local_nonce,
422                                       subflow->remote_nonce,
423                                       hmac);
424                 memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
425
426                 if (!mptcp_finish_join(sk))
427                         goto do_reset;
428
429                 subflow->mp_join = 1;
430                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
431
432                 if (subflow_use_different_dport(mptcp_sk(parent), sk)) {
433                         pr_debug("synack inet_dport=%d %d",
434                                  ntohs(inet_sk(sk)->inet_dport),
435                                  ntohs(inet_sk(parent)->inet_dport));
436                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX);
437                 }
438         } else if (mptcp_check_fallback(sk)) {
439 fallback:
440                 mptcp_rcv_space_init(mptcp_sk(parent), sk);
441         }
442         return;
443
444 do_reset:
445         mptcp_subflow_reset(sk);
446 }
447
448 struct request_sock_ops mptcp_subflow_request_sock_ops;
449 EXPORT_SYMBOL_GPL(mptcp_subflow_request_sock_ops);
450 static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops;
451
452 static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
453 {
454         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
455
456         pr_debug("subflow=%p", subflow);
457
458         /* Never answer to SYNs sent to broadcast or multicast */
459         if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
460                 goto drop;
461
462         return tcp_conn_request(&mptcp_subflow_request_sock_ops,
463                                 &subflow_request_sock_ipv4_ops,
464                                 sk, skb);
465 drop:
466         tcp_listendrop(sk);
467         return 0;
468 }
469
470 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
471 static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops;
472 static struct inet_connection_sock_af_ops subflow_v6_specific;
473 static struct inet_connection_sock_af_ops subflow_v6m_specific;
474 static struct proto tcpv6_prot_override;
475
476 static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
477 {
478         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
479
480         pr_debug("subflow=%p", subflow);
481
482         if (skb->protocol == htons(ETH_P_IP))
483                 return subflow_v4_conn_request(sk, skb);
484
485         if (!ipv6_unicast_destination(skb))
486                 goto drop;
487
488         return tcp_conn_request(&mptcp_subflow_request_sock_ops,
489                                 &subflow_request_sock_ipv6_ops, sk, skb);
490
491 drop:
492         tcp_listendrop(sk);
493         return 0; /* don't send reset */
494 }
495 #endif
496
497 /* validate hmac received in third ACK */
498 static bool subflow_hmac_valid(const struct request_sock *req,
499                                const struct mptcp_options_received *mp_opt)
500 {
501         const struct mptcp_subflow_request_sock *subflow_req;
502         u8 hmac[SHA256_DIGEST_SIZE];
503         struct mptcp_sock *msk;
504
505         subflow_req = mptcp_subflow_rsk(req);
506         msk = subflow_req->msk;
507         if (!msk)
508                 return false;
509
510         subflow_generate_hmac(msk->remote_key, msk->local_key,
511                               subflow_req->remote_nonce,
512                               subflow_req->local_nonce, hmac);
513
514         return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN);
515 }
516
517 static void mptcp_sock_destruct(struct sock *sk)
518 {
519         /* if new mptcp socket isn't accepted, it is free'd
520          * from the tcp listener sockets request queue, linked
521          * from req->sk.  The tcp socket is released.
522          * This calls the ULP release function which will
523          * also remove the mptcp socket, via
524          * sock_put(ctx->conn).
525          *
526          * Problem is that the mptcp socket will be in
527          * ESTABLISHED state and will not have the SOCK_DEAD flag.
528          * Both result in warnings from inet_sock_destruct.
529          */
530
531         if (sk->sk_state == TCP_ESTABLISHED) {
532                 sk->sk_state = TCP_CLOSE;
533                 WARN_ON_ONCE(sk->sk_socket);
534                 sock_orphan(sk);
535         }
536
537         mptcp_destroy_common(mptcp_sk(sk));
538         inet_sock_destruct(sk);
539 }
540
541 static void mptcp_force_close(struct sock *sk)
542 {
543         inet_sk_state_store(sk, TCP_CLOSE);
544         sk_common_release(sk);
545 }
546
547 static void subflow_ulp_fallback(struct sock *sk,
548                                  struct mptcp_subflow_context *old_ctx)
549 {
550         struct inet_connection_sock *icsk = inet_csk(sk);
551
552         mptcp_subflow_tcp_fallback(sk, old_ctx);
553         icsk->icsk_ulp_ops = NULL;
554         rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
555         tcp_sk(sk)->is_mptcp = 0;
556
557         mptcp_subflow_ops_undo_override(sk);
558 }
559
560 static void subflow_drop_ctx(struct sock *ssk)
561 {
562         struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
563
564         if (!ctx)
565                 return;
566
567         subflow_ulp_fallback(ssk, ctx);
568         if (ctx->conn)
569                 sock_put(ctx->conn);
570
571         kfree_rcu(ctx, rcu);
572 }
573
574 void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
575                                      struct mptcp_options_received *mp_opt)
576 {
577         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
578
579         subflow->remote_key = mp_opt->sndr_key;
580         subflow->fully_established = 1;
581         subflow->can_ack = 1;
582         WRITE_ONCE(msk->fully_established, true);
583 }
584
585 static struct sock *subflow_syn_recv_sock(const struct sock *sk,
586                                           struct sk_buff *skb,
587                                           struct request_sock *req,
588                                           struct dst_entry *dst,
589                                           struct request_sock *req_unhash,
590                                           bool *own_req)
591 {
592         struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk);
593         struct mptcp_subflow_request_sock *subflow_req;
594         struct mptcp_options_received mp_opt;
595         bool fallback, fallback_is_fatal;
596         struct sock *new_msk = NULL;
597         struct sock *child;
598
599         pr_debug("listener=%p, req=%p, conn=%p", listener, req, listener->conn);
600
601         /* After child creation we must look for 'mp_capable' even when options
602          * are not parsed
603          */
604         mp_opt.mp_capable = 0;
605
606         /* hopefully temporary handling for MP_JOIN+syncookie */
607         subflow_req = mptcp_subflow_rsk(req);
608         fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join;
609         fallback = !tcp_rsk(req)->is_mptcp;
610         if (fallback)
611                 goto create_child;
612
613         /* if the sk is MP_CAPABLE, we try to fetch the client key */
614         if (subflow_req->mp_capable) {
615                 if (TCP_SKB_CB(skb)->seq != subflow_req->ssn_offset + 1) {
616                         /* here we can receive and accept an in-window,
617                          * out-of-order pkt, which will not carry the MP_CAPABLE
618                          * opt even on mptcp enabled paths
619                          */
620                         goto create_msk;
621                 }
622
623                 mptcp_get_options(skb, &mp_opt);
624                 if (!mp_opt.mp_capable) {
625                         fallback = true;
626                         goto create_child;
627                 }
628
629 create_msk:
630                 new_msk = mptcp_sk_clone(listener->conn, &mp_opt, req);
631                 if (!new_msk)
632                         fallback = true;
633         } else if (subflow_req->mp_join) {
634                 mptcp_get_options(skb, &mp_opt);
635                 if (!mp_opt.mp_join || !subflow_hmac_valid(req, &mp_opt) ||
636                     !mptcp_can_accept_new_subflow(subflow_req->msk)) {
637                         SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
638                         fallback = true;
639                 }
640         }
641
642 create_child:
643         child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
644                                                      req_unhash, own_req);
645
646         if (child && *own_req) {
647                 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
648
649                 tcp_rsk(req)->drop_req = false;
650
651                 /* we need to fallback on ctx allocation failure and on pre-reqs
652                  * checking above. In the latter scenario we additionally need
653                  * to reset the context to non MPTCP status.
654                  */
655                 if (!ctx || fallback) {
656                         if (fallback_is_fatal)
657                                 goto dispose_child;
658
659                         subflow_drop_ctx(child);
660                         goto out;
661                 }
662
663                 if (ctx->mp_capable) {
664                         /* this can't race with mptcp_close(), as the msk is
665                          * not yet exposted to user-space
666                          */
667                         inet_sk_state_store((void *)new_msk, TCP_ESTABLISHED);
668
669                         /* record the newly created socket as the first msk
670                          * subflow, but don't link it yet into conn_list
671                          */
672                         WRITE_ONCE(mptcp_sk(new_msk)->first, child);
673
674                         /* new mpc subflow takes ownership of the newly
675                          * created mptcp socket
676                          */
677                         new_msk->sk_destruct = mptcp_sock_destruct;
678                         mptcp_pm_new_connection(mptcp_sk(new_msk), 1);
679                         mptcp_token_accept(subflow_req, mptcp_sk(new_msk));
680                         ctx->conn = new_msk;
681                         new_msk = NULL;
682
683                         /* with OoO packets we can reach here without ingress
684                          * mpc option
685                          */
686                         if (mp_opt.mp_capable)
687                                 mptcp_subflow_fully_established(ctx, &mp_opt);
688                 } else if (ctx->mp_join) {
689                         struct mptcp_sock *owner;
690
691                         owner = subflow_req->msk;
692                         if (!owner)
693                                 goto dispose_child;
694
695                         /* move the msk reference ownership to the subflow */
696                         subflow_req->msk = NULL;
697                         ctx->conn = (struct sock *)owner;
698                         if (!mptcp_finish_join(child))
699                                 goto dispose_child;
700
701                         SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
702                         tcp_rsk(req)->drop_req = true;
703
704                         if (subflow_use_different_sport(owner, sk)) {
705                                 pr_debug("ack inet_sport=%d %d",
706                                          ntohs(inet_sk(sk)->inet_sport),
707                                          ntohs(inet_sk((struct sock *)owner)->inet_sport));
708                                 if (!mptcp_pm_sport_in_anno_list(owner, sk)) {
709                                         SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX);
710                                         goto out;
711                                 }
712                                 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX);
713                         }
714                 }
715         }
716
717 out:
718         /* dispose of the left over mptcp master, if any */
719         if (unlikely(new_msk))
720                 mptcp_force_close(new_msk);
721
722         /* check for expected invariant - should never trigger, just help
723          * catching eariler subtle bugs
724          */
725         WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
726                      (!mptcp_subflow_ctx(child) ||
727                       !mptcp_subflow_ctx(child)->conn));
728         return child;
729
730 dispose_child:
731         subflow_drop_ctx(child);
732         tcp_rsk(req)->drop_req = true;
733         inet_csk_prepare_for_destroy_sock(child);
734         tcp_done(child);
735         req->rsk_ops->send_reset(sk, skb);
736
737         /* The last child reference will be released by the caller */
738         return child;
739 }
740
741 static struct inet_connection_sock_af_ops subflow_specific;
742 static struct proto tcp_prot_override;
743
744 enum mapping_status {
745         MAPPING_OK,
746         MAPPING_INVALID,
747         MAPPING_EMPTY,
748         MAPPING_DATA_FIN,
749         MAPPING_DUMMY
750 };
751
752 static u64 expand_seq(u64 old_seq, u16 old_data_len, u64 seq)
753 {
754         if ((u32)seq == (u32)old_seq)
755                 return old_seq;
756
757         /* Assume map covers data not mapped yet. */
758         return seq | ((old_seq + old_data_len + 1) & GENMASK_ULL(63, 32));
759 }
760
761 static void warn_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
762 {
763         WARN_ONCE(1, "Bad mapping: ssn=%d map_seq=%d map_data_len=%d",
764                   ssn, subflow->map_subflow_seq, subflow->map_data_len);
765 }
766
767 static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
768 {
769         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
770         unsigned int skb_consumed;
771
772         skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
773         if (WARN_ON_ONCE(skb_consumed >= skb->len))
774                 return true;
775
776         return skb->len - skb_consumed <= subflow->map_data_len -
777                                           mptcp_subflow_get_map_offset(subflow);
778 }
779
780 static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
781 {
782         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
783         u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
784
785         if (unlikely(before(ssn, subflow->map_subflow_seq))) {
786                 /* Mapping covers data later in the subflow stream,
787                  * currently unsupported.
788                  */
789                 warn_bad_map(subflow, ssn);
790                 return false;
791         }
792         if (unlikely(!before(ssn, subflow->map_subflow_seq +
793                                   subflow->map_data_len))) {
794                 /* Mapping does covers past subflow data, invalid */
795                 warn_bad_map(subflow, ssn + skb->len);
796                 return false;
797         }
798         return true;
799 }
800
801 static enum mapping_status get_mapping_status(struct sock *ssk,
802                                               struct mptcp_sock *msk)
803 {
804         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
805         struct mptcp_ext *mpext;
806         struct sk_buff *skb;
807         u16 data_len;
808         u64 map_seq;
809
810         skb = skb_peek(&ssk->sk_receive_queue);
811         if (!skb)
812                 return MAPPING_EMPTY;
813
814         if (mptcp_check_fallback(ssk))
815                 return MAPPING_DUMMY;
816
817         mpext = mptcp_get_ext(skb);
818         if (!mpext || !mpext->use_map) {
819                 if (!subflow->map_valid && !skb->len) {
820                         /* the TCP stack deliver 0 len FIN pkt to the receive
821                          * queue, that is the only 0len pkts ever expected here,
822                          * and we can admit no mapping only for 0 len pkts
823                          */
824                         if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
825                                 WARN_ONCE(1, "0len seq %d:%d flags %x",
826                                           TCP_SKB_CB(skb)->seq,
827                                           TCP_SKB_CB(skb)->end_seq,
828                                           TCP_SKB_CB(skb)->tcp_flags);
829                         sk_eat_skb(ssk, skb);
830                         return MAPPING_EMPTY;
831                 }
832
833                 if (!subflow->map_valid)
834                         return MAPPING_INVALID;
835
836                 goto validate_seq;
837         }
838
839         pr_debug("seq=%llu is64=%d ssn=%u data_len=%u data_fin=%d",
840                  mpext->data_seq, mpext->dsn64, mpext->subflow_seq,
841                  mpext->data_len, mpext->data_fin);
842
843         data_len = mpext->data_len;
844         if (data_len == 0) {
845                 pr_err("Infinite mapping not handled");
846                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
847                 return MAPPING_INVALID;
848         }
849
850         if (mpext->data_fin == 1) {
851                 if (data_len == 1) {
852                         bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
853                                                                  mpext->dsn64);
854                         pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
855                         if (subflow->map_valid) {
856                                 /* A DATA_FIN might arrive in a DSS
857                                  * option before the previous mapping
858                                  * has been fully consumed. Continue
859                                  * handling the existing mapping.
860                                  */
861                                 skb_ext_del(skb, SKB_EXT_MPTCP);
862                                 return MAPPING_OK;
863                         } else {
864                                 if (updated && schedule_work(&msk->work))
865                                         sock_hold((struct sock *)msk);
866
867                                 return MAPPING_DATA_FIN;
868                         }
869                 } else {
870                         u64 data_fin_seq = mpext->data_seq + data_len - 1;
871
872                         /* If mpext->data_seq is a 32-bit value, data_fin_seq
873                          * must also be limited to 32 bits.
874                          */
875                         if (!mpext->dsn64)
876                                 data_fin_seq &= GENMASK_ULL(31, 0);
877
878                         mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
879                         pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d",
880                                  data_fin_seq, mpext->dsn64);
881                 }
882
883                 /* Adjust for DATA_FIN using 1 byte of sequence space */
884                 data_len--;
885         }
886
887         if (!mpext->dsn64) {
888                 map_seq = expand_seq(subflow->map_seq, subflow->map_data_len,
889                                      mpext->data_seq);
890                 pr_debug("expanded seq=%llu", subflow->map_seq);
891         } else {
892                 map_seq = mpext->data_seq;
893         }
894         WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
895
896         if (subflow->map_valid) {
897                 /* Allow replacing only with an identical map */
898                 if (subflow->map_seq == map_seq &&
899                     subflow->map_subflow_seq == mpext->subflow_seq &&
900                     subflow->map_data_len == data_len) {
901                         skb_ext_del(skb, SKB_EXT_MPTCP);
902                         return MAPPING_OK;
903                 }
904
905                 /* If this skb data are fully covered by the current mapping,
906                  * the new map would need caching, which is not supported
907                  */
908                 if (skb_is_fully_mapped(ssk, skb)) {
909                         MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
910                         return MAPPING_INVALID;
911                 }
912
913                 /* will validate the next map after consuming the current one */
914                 return MAPPING_OK;
915         }
916
917         subflow->map_seq = map_seq;
918         subflow->map_subflow_seq = mpext->subflow_seq;
919         subflow->map_data_len = data_len;
920         subflow->map_valid = 1;
921         subflow->mpc_map = mpext->mpc_map;
922         pr_debug("new map seq=%llu subflow_seq=%u data_len=%u",
923                  subflow->map_seq, subflow->map_subflow_seq,
924                  subflow->map_data_len);
925
926 validate_seq:
927         /* we revalidate valid mapping on new skb, because we must ensure
928          * the current skb is completely covered by the available mapping
929          */
930         if (!validate_mapping(ssk, skb))
931                 return MAPPING_INVALID;
932
933         skb_ext_del(skb, SKB_EXT_MPTCP);
934         return MAPPING_OK;
935 }
936
937 static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
938                                        u64 limit)
939 {
940         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
941         bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
942         u32 incr;
943
944         incr = limit >= skb->len ? skb->len + fin : limit;
945
946         pr_debug("discarding=%d len=%d seq=%d", incr, skb->len,
947                  subflow->map_subflow_seq);
948         MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
949         tcp_sk(ssk)->copied_seq += incr;
950         if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
951                 sk_eat_skb(ssk, skb);
952         if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
953                 subflow->map_valid = 0;
954 }
955
956 /* sched mptcp worker to remove the subflow if no more data is pending */
957 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
958 {
959         struct sock *sk = (struct sock *)msk;
960
961         if (likely(ssk->sk_state != TCP_CLOSE))
962                 return;
963
964         if (skb_queue_empty(&ssk->sk_receive_queue) &&
965             !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) {
966                 sock_hold(sk);
967                 if (!schedule_work(&msk->work))
968                         sock_put(sk);
969         }
970 }
971
972 static bool subflow_check_data_avail(struct sock *ssk)
973 {
974         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
975         enum mapping_status status;
976         struct mptcp_sock *msk;
977         struct sk_buff *skb;
978
979         pr_debug("msk=%p ssk=%p data_avail=%d skb=%p", subflow->conn, ssk,
980                  subflow->data_avail, skb_peek(&ssk->sk_receive_queue));
981         if (!skb_peek(&ssk->sk_receive_queue))
982                 subflow->data_avail = 0;
983         if (subflow->data_avail)
984                 return true;
985
986         msk = mptcp_sk(subflow->conn);
987         for (;;) {
988                 u64 ack_seq;
989                 u64 old_ack;
990
991                 status = get_mapping_status(ssk, msk);
992                 pr_debug("msk=%p ssk=%p status=%d", msk, ssk, status);
993                 if (status == MAPPING_INVALID) {
994                         ssk->sk_err = EBADMSG;
995                         goto fatal;
996                 }
997                 if (status == MAPPING_DUMMY) {
998                         __mptcp_do_fallback(msk);
999                         skb = skb_peek(&ssk->sk_receive_queue);
1000                         subflow->map_valid = 1;
1001                         subflow->map_seq = READ_ONCE(msk->ack_seq);
1002                         subflow->map_data_len = skb->len;
1003                         subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq -
1004                                                    subflow->ssn_offset;
1005                         subflow->data_avail = MPTCP_SUBFLOW_DATA_AVAIL;
1006                         return true;
1007                 }
1008
1009                 if (status != MAPPING_OK)
1010                         goto no_data;
1011
1012                 skb = skb_peek(&ssk->sk_receive_queue);
1013                 if (WARN_ON_ONCE(!skb))
1014                         goto no_data;
1015
1016                 /* if msk lacks the remote key, this subflow must provide an
1017                  * MP_CAPABLE-based mapping
1018                  */
1019                 if (unlikely(!READ_ONCE(msk->can_ack))) {
1020                         if (!subflow->mpc_map) {
1021                                 ssk->sk_err = EBADMSG;
1022                                 goto fatal;
1023                         }
1024                         WRITE_ONCE(msk->remote_key, subflow->remote_key);
1025                         WRITE_ONCE(msk->ack_seq, subflow->map_seq);
1026                         WRITE_ONCE(msk->can_ack, true);
1027                 }
1028
1029                 old_ack = READ_ONCE(msk->ack_seq);
1030                 ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
1031                 pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack,
1032                          ack_seq);
1033                 if (ack_seq == old_ack) {
1034                         subflow->data_avail = MPTCP_SUBFLOW_DATA_AVAIL;
1035                         break;
1036                 } else if (after64(ack_seq, old_ack)) {
1037                         subflow->data_avail = MPTCP_SUBFLOW_OOO_DATA;
1038                         break;
1039                 }
1040
1041                 /* only accept in-sequence mapping. Old values are spurious
1042                  * retransmission
1043                  */
1044                 mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
1045         }
1046         return true;
1047
1048 no_data:
1049         subflow_sched_work_if_closed(msk, ssk);
1050         return false;
1051 fatal:
1052         /* fatal protocol error, close the socket */
1053         /* This barrier is coupled with smp_rmb() in tcp_poll() */
1054         smp_wmb();
1055         ssk->sk_error_report(ssk);
1056         tcp_set_state(ssk, TCP_CLOSE);
1057         tcp_send_active_reset(ssk, GFP_ATOMIC);
1058         subflow->data_avail = 0;
1059         return false;
1060 }
1061
1062 bool mptcp_subflow_data_available(struct sock *sk)
1063 {
1064         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1065
1066         /* check if current mapping is still valid */
1067         if (subflow->map_valid &&
1068             mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
1069                 subflow->map_valid = 0;
1070                 subflow->data_avail = 0;
1071
1072                 pr_debug("Done with mapping: seq=%u data_len=%u",
1073                          subflow->map_subflow_seq,
1074                          subflow->map_data_len);
1075         }
1076
1077         return subflow_check_data_avail(sk);
1078 }
1079
1080 /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
1081  * not the ssk one.
1082  *
1083  * In mptcp, rwin is about the mptcp-level connection data.
1084  *
1085  * Data that is still on the ssk rx queue can thus be ignored,
1086  * as far as mptcp peer is concerened that data is still inflight.
1087  * DSS ACK is updated when skb is moved to the mptcp rx queue.
1088  */
1089 void mptcp_space(const struct sock *ssk, int *space, int *full_space)
1090 {
1091         const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1092         const struct sock *sk = subflow->conn;
1093
1094         *space = __mptcp_space(sk);
1095         *full_space = tcp_full_space(sk);
1096 }
1097
1098 static void subflow_data_ready(struct sock *sk)
1099 {
1100         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1101         u16 state = 1 << inet_sk_state_load(sk);
1102         struct sock *parent = subflow->conn;
1103         struct mptcp_sock *msk;
1104
1105         msk = mptcp_sk(parent);
1106         if (state & TCPF_LISTEN) {
1107                 set_bit(MPTCP_DATA_READY, &msk->flags);
1108                 parent->sk_data_ready(parent);
1109                 return;
1110         }
1111
1112         WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
1113                      !subflow->mp_join && !(state & TCPF_CLOSE));
1114
1115         if (mptcp_subflow_data_available(sk))
1116                 mptcp_data_ready(parent, sk);
1117 }
1118
1119 static void subflow_write_space(struct sock *ssk)
1120 {
1121         struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1122
1123         mptcp_propagate_sndbuf(sk, ssk);
1124         mptcp_write_space(sk);
1125 }
1126
1127 static struct inet_connection_sock_af_ops *
1128 subflow_default_af_ops(struct sock *sk)
1129 {
1130 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1131         if (sk->sk_family == AF_INET6)
1132                 return &subflow_v6_specific;
1133 #endif
1134         return &subflow_specific;
1135 }
1136
1137 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1138 void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
1139 {
1140         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1141         struct inet_connection_sock *icsk = inet_csk(sk);
1142         struct inet_connection_sock_af_ops *target;
1143
1144         target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
1145
1146         pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d",
1147                  subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
1148
1149         if (likely(icsk->icsk_af_ops == target))
1150                 return;
1151
1152         subflow->icsk_af_ops = icsk->icsk_af_ops;
1153         icsk->icsk_af_ops = target;
1154 }
1155 #endif
1156
1157 void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
1158                          struct sockaddr_storage *addr,
1159                          unsigned short family)
1160 {
1161         memset(addr, 0, sizeof(*addr));
1162         addr->ss_family = family;
1163         if (addr->ss_family == AF_INET) {
1164                 struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
1165
1166                 if (info->family == AF_INET)
1167                         in_addr->sin_addr = info->addr;
1168 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1169                 else if (ipv6_addr_v4mapped(&info->addr6))
1170                         in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3];
1171 #endif
1172                 in_addr->sin_port = info->port;
1173         }
1174 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1175         else if (addr->ss_family == AF_INET6) {
1176                 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;
1177
1178                 if (info->family == AF_INET)
1179                         ipv6_addr_set_v4mapped(info->addr.s_addr,
1180                                                &in6_addr->sin6_addr);
1181                 else
1182                         in6_addr->sin6_addr = info->addr6;
1183                 in6_addr->sin6_port = info->port;
1184         }
1185 #endif
1186 }
1187
1188 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
1189                             const struct mptcp_addr_info *remote)
1190 {
1191         struct mptcp_sock *msk = mptcp_sk(sk);
1192         struct mptcp_subflow_context *subflow;
1193         struct sockaddr_storage addr;
1194         int remote_id = remote->id;
1195         int local_id = loc->id;
1196         struct socket *sf;
1197         struct sock *ssk;
1198         u32 remote_token;
1199         int addrlen;
1200         int err;
1201
1202         if (!mptcp_is_fully_established(sk))
1203                 return -ENOTCONN;
1204
1205         err = mptcp_subflow_create_socket(sk, &sf);
1206         if (err)
1207                 return err;
1208
1209         ssk = sf->sk;
1210         subflow = mptcp_subflow_ctx(ssk);
1211         do {
1212                 get_random_bytes(&subflow->local_nonce, sizeof(u32));
1213         } while (!subflow->local_nonce);
1214
1215         if (!local_id) {
1216                 err = mptcp_pm_get_local_id(msk, (struct sock_common *)ssk);
1217                 if (err < 0)
1218                         goto failed;
1219
1220                 local_id = err;
1221         }
1222
1223         subflow->remote_key = msk->remote_key;
1224         subflow->local_key = msk->local_key;
1225         subflow->token = msk->token;
1226         mptcp_info2sockaddr(loc, &addr, ssk->sk_family);
1227
1228         addrlen = sizeof(struct sockaddr_in);
1229 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1230         if (addr.ss_family == AF_INET6)
1231                 addrlen = sizeof(struct sockaddr_in6);
1232 #endif
1233         ssk->sk_bound_dev_if = loc->ifindex;
1234         err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
1235         if (err)
1236                 goto failed;
1237
1238         mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1239         pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk,
1240                  remote_token, local_id, remote_id);
1241         subflow->remote_token = remote_token;
1242         subflow->local_id = local_id;
1243         subflow->remote_id = remote_id;
1244         subflow->request_join = 1;
1245         subflow->request_bkup = !!(loc->flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1246         mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
1247
1248         mptcp_add_pending_subflow(msk, subflow);
1249         err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
1250         if (err && err != -EINPROGRESS)
1251                 goto failed_unlink;
1252
1253         /* discard the subflow socket */
1254         mptcp_sock_graft(ssk, sk->sk_socket);
1255         iput(SOCK_INODE(sf));
1256         return err;
1257
1258 failed_unlink:
1259         spin_lock_bh(&msk->join_list_lock);
1260         list_del(&subflow->node);
1261         spin_unlock_bh(&msk->join_list_lock);
1262
1263 failed:
1264         subflow->disposable = 1;
1265         sock_release(sf);
1266         return err;
1267 }
1268
1269 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
1270 {
1271 #ifdef CONFIG_SOCK_CGROUP_DATA
1272         struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
1273                                 *child_skcd = &child->sk_cgrp_data;
1274
1275         /* only the additional subflows created by kworkers have to be modified */
1276         if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
1277             cgroup_id(sock_cgroup_ptr(child_skcd))) {
1278 #ifdef CONFIG_MEMCG
1279                 struct mem_cgroup *memcg = parent->sk_memcg;
1280
1281                 mem_cgroup_sk_free(child);
1282                 if (memcg && css_tryget(&memcg->css))
1283                         child->sk_memcg = memcg;
1284 #endif /* CONFIG_MEMCG */
1285
1286                 cgroup_sk_free(child_skcd);
1287                 *child_skcd = *parent_skcd;
1288                 cgroup_sk_clone(child_skcd);
1289         }
1290 #endif /* CONFIG_SOCK_CGROUP_DATA */
1291 }
1292
1293 static void mptcp_subflow_ops_override(struct sock *ssk)
1294 {
1295 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1296         if (ssk->sk_prot == &tcpv6_prot)
1297                 ssk->sk_prot = &tcpv6_prot_override;
1298         else
1299 #endif
1300                 ssk->sk_prot = &tcp_prot_override;
1301 }
1302
1303 static void mptcp_subflow_ops_undo_override(struct sock *ssk)
1304 {
1305 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1306         if (ssk->sk_prot == &tcpv6_prot_override)
1307                 ssk->sk_prot = &tcpv6_prot;
1308         else
1309 #endif
1310                 ssk->sk_prot = &tcp_prot;
1311 }
1312 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)
1313 {
1314         struct mptcp_subflow_context *subflow;
1315         struct net *net = sock_net(sk);
1316         struct socket *sf;
1317         int err;
1318
1319         /* un-accepted server sockets can reach here - on bad configuration
1320          * bail early to avoid greater trouble later
1321          */
1322         if (unlikely(!sk->sk_socket))
1323                 return -EINVAL;
1324
1325         err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP,
1326                                &sf);
1327         if (err)
1328                 return err;
1329
1330         lock_sock(sf->sk);
1331
1332         /* the newly created socket has to be in the same cgroup as its parent */
1333         mptcp_attach_cgroup(sk, sf->sk);
1334
1335         /* kernel sockets do not by default acquire net ref, but TCP timer
1336          * needs it.
1337          */
1338         sf->sk->sk_net_refcnt = 1;
1339         get_net(net);
1340 #ifdef CONFIG_PROC_FS
1341         this_cpu_add(*net->core.sock_inuse, 1);
1342 #endif
1343         err = tcp_set_ulp(sf->sk, "mptcp");
1344         release_sock(sf->sk);
1345
1346         if (err) {
1347                 sock_release(sf);
1348                 return err;
1349         }
1350
1351         /* the newly created socket really belongs to the owning MPTCP master
1352          * socket, even if for additional subflows the allocation is performed
1353          * by a kernel workqueue. Adjust inode references, so that the
1354          * procfs/diag interaces really show this one belonging to the correct
1355          * user.
1356          */
1357         SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
1358         SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
1359         SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
1360
1361         subflow = mptcp_subflow_ctx(sf->sk);
1362         pr_debug("subflow=%p", subflow);
1363
1364         *new_sock = sf;
1365         sock_hold(sk);
1366         subflow->conn = sk;
1367         mptcp_subflow_ops_override(sf->sk);
1368
1369         return 0;
1370 }
1371
1372 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
1373                                                         gfp_t priority)
1374 {
1375         struct inet_connection_sock *icsk = inet_csk(sk);
1376         struct mptcp_subflow_context *ctx;
1377
1378         ctx = kzalloc(sizeof(*ctx), priority);
1379         if (!ctx)
1380                 return NULL;
1381
1382         rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
1383         INIT_LIST_HEAD(&ctx->node);
1384         INIT_LIST_HEAD(&ctx->delegated_node);
1385
1386         pr_debug("subflow=%p", ctx);
1387
1388         ctx->tcp_sock = sk;
1389
1390         return ctx;
1391 }
1392
1393 static void __subflow_state_change(struct sock *sk)
1394 {
1395         struct socket_wq *wq;
1396
1397         rcu_read_lock();
1398         wq = rcu_dereference(sk->sk_wq);
1399         if (skwq_has_sleeper(wq))
1400                 wake_up_interruptible_all(&wq->wait);
1401         rcu_read_unlock();
1402 }
1403
1404 static bool subflow_is_done(const struct sock *sk)
1405 {
1406         return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
1407 }
1408
1409 static void subflow_state_change(struct sock *sk)
1410 {
1411         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1412         struct sock *parent = subflow->conn;
1413
1414         __subflow_state_change(sk);
1415
1416         if (subflow_simultaneous_connect(sk)) {
1417                 mptcp_propagate_sndbuf(parent, sk);
1418                 mptcp_do_fallback(sk);
1419                 mptcp_rcv_space_init(mptcp_sk(parent), sk);
1420                 pr_fallback(mptcp_sk(parent));
1421                 subflow->conn_finished = 1;
1422                 if (inet_sk_state_load(parent) == TCP_SYN_SENT) {
1423                         inet_sk_state_store(parent, TCP_ESTABLISHED);
1424                         parent->sk_state_change(parent);
1425                 }
1426         }
1427
1428         /* as recvmsg() does not acquire the subflow socket for ssk selection
1429          * a fin packet carrying a DSS can be unnoticed if we don't trigger
1430          * the data available machinery here.
1431          */
1432         if (mptcp_subflow_data_available(sk))
1433                 mptcp_data_ready(parent, sk);
1434
1435         subflow_sched_work_if_closed(mptcp_sk(parent), sk);
1436
1437         if (__mptcp_check_fallback(mptcp_sk(parent)) &&
1438             !subflow->rx_eof && subflow_is_done(sk)) {
1439                 subflow->rx_eof = 1;
1440                 mptcp_subflow_eof(parent);
1441         }
1442 }
1443
1444 static int subflow_ulp_init(struct sock *sk)
1445 {
1446         struct inet_connection_sock *icsk = inet_csk(sk);
1447         struct mptcp_subflow_context *ctx;
1448         struct tcp_sock *tp = tcp_sk(sk);
1449         int err = 0;
1450
1451         /* disallow attaching ULP to a socket unless it has been
1452          * created with sock_create_kern()
1453          */
1454         if (!sk->sk_kern_sock) {
1455                 err = -EOPNOTSUPP;
1456                 goto out;
1457         }
1458
1459         ctx = subflow_create_ctx(sk, GFP_KERNEL);
1460         if (!ctx) {
1461                 err = -ENOMEM;
1462                 goto out;
1463         }
1464
1465         pr_debug("subflow=%p, family=%d", ctx, sk->sk_family);
1466
1467         tp->is_mptcp = 1;
1468         ctx->icsk_af_ops = icsk->icsk_af_ops;
1469         icsk->icsk_af_ops = subflow_default_af_ops(sk);
1470         ctx->tcp_data_ready = sk->sk_data_ready;
1471         ctx->tcp_state_change = sk->sk_state_change;
1472         ctx->tcp_write_space = sk->sk_write_space;
1473         sk->sk_data_ready = subflow_data_ready;
1474         sk->sk_write_space = subflow_write_space;
1475         sk->sk_state_change = subflow_state_change;
1476 out:
1477         return err;
1478 }
1479
1480 static void subflow_ulp_release(struct sock *ssk)
1481 {
1482         struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
1483         bool release = true;
1484         struct sock *sk;
1485
1486         if (!ctx)
1487                 return;
1488
1489         sk = ctx->conn;
1490         if (sk) {
1491                 /* if the msk has been orphaned, keep the ctx
1492                  * alive, will be freed by __mptcp_close_ssk(),
1493                  * when the subflow is still unaccepted
1494                  */
1495                 release = ctx->disposable || list_empty(&ctx->node);
1496                 sock_put(sk);
1497         }
1498
1499         mptcp_subflow_ops_undo_override(ssk);
1500         if (release)
1501                 kfree_rcu(ctx, rcu);
1502 }
1503
1504 static void subflow_ulp_clone(const struct request_sock *req,
1505                               struct sock *newsk,
1506                               const gfp_t priority)
1507 {
1508         struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
1509         struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
1510         struct mptcp_subflow_context *new_ctx;
1511
1512         if (!tcp_rsk(req)->is_mptcp ||
1513             (!subflow_req->mp_capable && !subflow_req->mp_join)) {
1514                 subflow_ulp_fallback(newsk, old_ctx);
1515                 return;
1516         }
1517
1518         new_ctx = subflow_create_ctx(newsk, priority);
1519         if (!new_ctx) {
1520                 subflow_ulp_fallback(newsk, old_ctx);
1521                 return;
1522         }
1523
1524         new_ctx->conn_finished = 1;
1525         new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
1526         new_ctx->tcp_data_ready = old_ctx->tcp_data_ready;
1527         new_ctx->tcp_state_change = old_ctx->tcp_state_change;
1528         new_ctx->tcp_write_space = old_ctx->tcp_write_space;
1529         new_ctx->rel_write_seq = 1;
1530         new_ctx->tcp_sock = newsk;
1531
1532         if (subflow_req->mp_capable) {
1533                 /* see comments in subflow_syn_recv_sock(), MPTCP connection
1534                  * is fully established only after we receive the remote key
1535                  */
1536                 new_ctx->mp_capable = 1;
1537                 new_ctx->local_key = subflow_req->local_key;
1538                 new_ctx->token = subflow_req->token;
1539                 new_ctx->ssn_offset = subflow_req->ssn_offset;
1540                 new_ctx->idsn = subflow_req->idsn;
1541         } else if (subflow_req->mp_join) {
1542                 new_ctx->ssn_offset = subflow_req->ssn_offset;
1543                 new_ctx->mp_join = 1;
1544                 new_ctx->fully_established = 1;
1545                 new_ctx->backup = subflow_req->backup;
1546                 new_ctx->local_id = subflow_req->local_id;
1547                 new_ctx->remote_id = subflow_req->remote_id;
1548                 new_ctx->token = subflow_req->token;
1549                 new_ctx->thmac = subflow_req->thmac;
1550         }
1551 }
1552
1553 static void tcp_release_cb_override(struct sock *ssk)
1554 {
1555         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1556
1557         if (mptcp_subflow_has_delegated_action(subflow))
1558                 mptcp_subflow_process_delegated(ssk);
1559
1560         tcp_release_cb(ssk);
1561 }
1562
1563 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
1564         .name           = "mptcp",
1565         .owner          = THIS_MODULE,
1566         .init           = subflow_ulp_init,
1567         .release        = subflow_ulp_release,
1568         .clone          = subflow_ulp_clone,
1569 };
1570
1571 static int subflow_ops_init(struct request_sock_ops *subflow_ops)
1572 {
1573         subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
1574         subflow_ops->slab_name = "request_sock_subflow";
1575
1576         subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
1577                                               subflow_ops->obj_size, 0,
1578                                               SLAB_ACCOUNT |
1579                                               SLAB_TYPESAFE_BY_RCU,
1580                                               NULL);
1581         if (!subflow_ops->slab)
1582                 return -ENOMEM;
1583
1584         subflow_ops->destructor = subflow_req_destructor;
1585
1586         return 0;
1587 }
1588
1589 void __init mptcp_subflow_init(void)
1590 {
1591         mptcp_subflow_request_sock_ops = tcp_request_sock_ops;
1592         if (subflow_ops_init(&mptcp_subflow_request_sock_ops) != 0)
1593                 panic("MPTCP: failed to init subflow request sock ops\n");
1594
1595         subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
1596         subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
1597
1598         subflow_specific = ipv4_specific;
1599         subflow_specific.conn_request = subflow_v4_conn_request;
1600         subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
1601         subflow_specific.sk_rx_dst_set = subflow_finish_connect;
1602
1603         tcp_prot_override = tcp_prot;
1604         tcp_prot_override.release_cb = tcp_release_cb_override;
1605
1606 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1607         subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
1608         subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
1609
1610         subflow_v6_specific = ipv6_specific;
1611         subflow_v6_specific.conn_request = subflow_v6_conn_request;
1612         subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
1613         subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
1614
1615         subflow_v6m_specific = subflow_v6_specific;
1616         subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
1617         subflow_v6m_specific.send_check = ipv4_specific.send_check;
1618         subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
1619         subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
1620         subflow_v6m_specific.net_frag_header_len = 0;
1621
1622         tcpv6_prot_override = tcpv6_prot;
1623         tcpv6_prot_override.release_cb = tcp_release_cb_override;
1624 #endif
1625
1626         mptcp_diag_subflow_init(&subflow_ulp_ops);
1627
1628         if (tcp_register_ulp(&subflow_ulp_ops) != 0)
1629                 panic("MPTCP: failed to register subflows to ULP\n");
1630 }