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