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