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