1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2017 - 2019, Intel Corporation.
7 #define pr_fmt(fmt) "MPTCP: " fmt
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>
15 #include <net/inet_common.h>
16 #include <net/inet_hashtables.h>
17 #include <net/protocol.h>
19 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
20 #include <net/ip6_route.h>
21 #include <net/transp_v6.h>
23 #include <net/mptcp.h>
24 #include <uapi/linux/mptcp.h>
28 #include <trace/events/mptcp.h>
30 static void mptcp_subflow_ops_undo_override(struct sock *ssk);
32 static void SUBFLOW_REQ_INC_STATS(struct request_sock *req,
33 enum linux_mptcp_mib_field field)
35 MPTCP_INC_STATS(sock_net(req_to_sk(req)), field);
38 static void subflow_req_destructor(struct request_sock *req)
40 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
42 pr_debug("subflow_req=%p", subflow_req);
45 sock_put((struct sock *)subflow_req->msk);
47 mptcp_token_destroy_request(req);
48 tcp_request_sock_ops.destructor(req);
51 static void subflow_generate_hmac(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
56 put_unaligned_be32(nonce1, &msg[0]);
57 put_unaligned_be32(nonce2, &msg[4]);
59 mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
62 static bool mptcp_can_accept_new_subflow(const struct mptcp_sock *msk)
64 return mptcp_is_fully_established((void *)msk) &&
65 READ_ONCE(msk->pm.accept_subflow);
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)
71 struct mptcp_sock *msk = subflow_req->msk;
72 u8 hmac[SHA256_DIGEST_SIZE];
74 get_random_bytes(&subflow_req->local_nonce, sizeof(u32));
76 subflow_generate_hmac(msk->local_key, msk->remote_key,
77 subflow_req->local_nonce,
78 subflow_req->remote_nonce, hmac);
80 subflow_req->thmac = get_unaligned_be64(hmac);
83 static struct mptcp_sock *subflow_token_join_request(struct request_sock *req)
85 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
86 struct mptcp_sock *msk;
89 msk = mptcp_token_get_sock(subflow_req->token);
91 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN);
95 local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req);
97 sock_put((struct sock *)msk);
100 subflow_req->local_id = local_id;
105 static void subflow_init_req(struct request_sock *req, const struct sock *sk_listener)
107 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
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);
117 static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk)
119 return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport;
122 static void subflow_add_reset_reason(struct sk_buff *skb, u8 reason)
124 struct mptcp_ext *mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
127 memset(mpext, 0, sizeof(*mpext));
128 mpext->reset_reason = reason;
132 /* Init mptcp request socket.
134 * Returns an error code if a JOIN has failed and a TCP reset
137 static int subflow_check_req(struct request_sock *req,
138 const struct sock *sk_listener,
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;
145 pr_debug("subflow_req=%p, listener=%p", subflow_req, listener);
147 #ifdef CONFIG_TCP_MD5SIG
148 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
151 if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info))
155 mptcp_get_options(sk_listener, skb, &mp_opt);
157 if (mp_opt.mp_capable) {
158 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE);
162 } else if (mp_opt.mp_join) {
163 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX);
166 if (mp_opt.mp_capable && listener->request_mptcp) {
167 int err, retries = MPTCP_TOKEN_MAX_RETRIES;
169 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
172 get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key));
173 } while (subflow_req->local_key == 0);
175 if (unlikely(req->syncookie)) {
176 mptcp_crypto_key_sha(subflow_req->local_key,
179 if (mptcp_token_exists(subflow_req->token)) {
182 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
184 subflow_req->mp_capable = 1;
189 err = mptcp_token_new_request(req);
191 subflow_req->mp_capable = 1;
192 else if (retries-- > 0)
195 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
197 } else if (mp_opt.mp_join && listener->request_mptcp) {
198 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
199 subflow_req->mp_join = 1;
200 subflow_req->backup = mp_opt.backup;
201 subflow_req->remote_id = mp_opt.join_id;
202 subflow_req->token = mp_opt.token;
203 subflow_req->remote_nonce = mp_opt.nonce;
204 subflow_req->msk = subflow_token_join_request(req);
206 /* Can't fall back to TCP in this case. */
207 if (!subflow_req->msk) {
208 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
212 if (subflow_use_different_sport(subflow_req->msk, sk_listener)) {
213 pr_debug("syn inet_sport=%d %d",
214 ntohs(inet_sk(sk_listener)->inet_sport),
215 ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport));
216 if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener)) {
217 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX);
220 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX);
223 subflow_req_create_thmac(subflow_req);
225 if (unlikely(req->syncookie)) {
226 if (mptcp_can_accept_new_subflow(subflow_req->msk))
227 subflow_init_req_cookie_join_save(subflow_req, skb);
232 pr_debug("token=%u, remote_nonce=%u msk=%p", subflow_req->token,
233 subflow_req->remote_nonce, subflow_req->msk);
239 int mptcp_subflow_init_cookie_req(struct request_sock *req,
240 const struct sock *sk_listener,
243 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
244 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
245 struct mptcp_options_received mp_opt;
248 subflow_init_req(req, sk_listener);
249 mptcp_get_options(sk_listener, skb, &mp_opt);
251 if (mp_opt.mp_capable && mp_opt.mp_join)
254 if (mp_opt.mp_capable && listener->request_mptcp) {
255 if (mp_opt.sndr_key == 0)
258 subflow_req->local_key = mp_opt.rcvr_key;
259 err = mptcp_token_new_request(req);
263 subflow_req->mp_capable = 1;
264 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
265 } else if (mp_opt.mp_join && listener->request_mptcp) {
266 if (!mptcp_token_join_cookie_init_state(subflow_req, skb))
269 subflow_req->mp_join = 1;
270 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
275 EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req);
277 static struct dst_entry *subflow_v4_route_req(const struct sock *sk,
280 struct request_sock *req)
282 struct dst_entry *dst;
285 tcp_rsk(req)->is_mptcp = 1;
286 subflow_init_req(req, sk);
288 dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req);
292 err = subflow_check_req(req, sk, skb);
298 tcp_request_sock_ops.send_reset(sk, skb);
302 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
303 static struct dst_entry *subflow_v6_route_req(const struct sock *sk,
306 struct request_sock *req)
308 struct dst_entry *dst;
311 tcp_rsk(req)->is_mptcp = 1;
312 subflow_init_req(req, sk);
314 dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req);
318 err = subflow_check_req(req, sk, skb);
324 tcp6_request_sock_ops.send_reset(sk, skb);
329 /* validate received truncated hmac and create hmac for third ACK */
330 static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
332 u8 hmac[SHA256_DIGEST_SIZE];
335 subflow_generate_hmac(subflow->remote_key, subflow->local_key,
336 subflow->remote_nonce, subflow->local_nonce,
339 thmac = get_unaligned_be64(hmac);
340 pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n",
341 subflow, subflow->token,
342 (unsigned long long)thmac,
343 (unsigned long long)subflow->thmac);
345 return thmac == subflow->thmac;
348 void mptcp_subflow_reset(struct sock *ssk)
350 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
351 struct sock *sk = subflow->conn;
353 /* must hold: tcp_done() could drop last reference on parent */
356 tcp_set_state(ssk, TCP_CLOSE);
357 tcp_send_active_reset(ssk, GFP_ATOMIC);
359 if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags) &&
360 schedule_work(&mptcp_sk(sk)->work))
361 return; /* worker will put sk for us */
366 static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk)
368 return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport;
371 void __mptcp_set_connected(struct sock *sk)
373 if (sk->sk_state == TCP_SYN_SENT) {
374 inet_sk_state_store(sk, TCP_ESTABLISHED);
375 sk->sk_state_change(sk);
379 static void mptcp_set_connected(struct sock *sk)
382 if (!sock_owned_by_user(sk))
383 __mptcp_set_connected(sk);
385 set_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->flags);
386 mptcp_data_unlock(sk);
389 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
391 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
392 struct mptcp_options_received mp_opt;
393 struct sock *parent = subflow->conn;
395 subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);
398 /* be sure no special action on any packet other than syn-ack */
399 if (subflow->conn_finished)
402 mptcp_propagate_sndbuf(parent, sk);
403 subflow->rel_write_seq = 1;
404 subflow->conn_finished = 1;
405 subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
406 pr_debug("subflow=%p synack seq=%x", subflow, subflow->ssn_offset);
408 mptcp_get_options(sk, skb, &mp_opt);
409 if (subflow->request_mptcp) {
410 if (!mp_opt.mp_capable) {
411 MPTCP_INC_STATS(sock_net(sk),
412 MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
413 mptcp_do_fallback(sk);
414 pr_fallback(mptcp_sk(subflow->conn));
418 if (mp_opt.csum_reqd)
419 WRITE_ONCE(mptcp_sk(parent)->csum_enabled, true);
420 if (mp_opt.deny_join_id0)
421 WRITE_ONCE(mptcp_sk(parent)->pm.remote_deny_join_id0, true);
422 subflow->mp_capable = 1;
423 subflow->can_ack = 1;
424 subflow->remote_key = mp_opt.sndr_key;
425 pr_debug("subflow=%p, remote_key=%llu", subflow,
426 subflow->remote_key);
427 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK);
428 mptcp_finish_connect(sk);
429 mptcp_set_connected(parent);
430 } else if (subflow->request_join) {
431 u8 hmac[SHA256_DIGEST_SIZE];
433 if (!mp_opt.mp_join) {
434 subflow->reset_reason = MPTCP_RST_EMPTCP;
438 subflow->thmac = mp_opt.thmac;
439 subflow->remote_nonce = mp_opt.nonce;
440 pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u", subflow,
441 subflow->thmac, subflow->remote_nonce);
443 if (!subflow_thmac_valid(subflow)) {
444 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC);
445 subflow->reset_reason = MPTCP_RST_EMPTCP;
449 if (!mptcp_finish_join(sk))
452 subflow_generate_hmac(subflow->local_key, subflow->remote_key,
453 subflow->local_nonce,
454 subflow->remote_nonce,
456 memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
458 subflow->mp_join = 1;
459 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
461 if (subflow_use_different_dport(mptcp_sk(parent), sk)) {
462 pr_debug("synack inet_dport=%d %d",
463 ntohs(inet_sk(sk)->inet_dport),
464 ntohs(inet_sk(parent)->inet_dport));
465 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX);
467 } else if (mptcp_check_fallback(sk)) {
469 mptcp_rcv_space_init(mptcp_sk(parent), sk);
470 mptcp_set_connected(parent);
475 subflow->reset_transient = 0;
476 mptcp_subflow_reset(sk);
479 struct request_sock_ops mptcp_subflow_request_sock_ops;
480 EXPORT_SYMBOL_GPL(mptcp_subflow_request_sock_ops);
481 static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops;
483 static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
485 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
487 pr_debug("subflow=%p", subflow);
489 /* Never answer to SYNs sent to broadcast or multicast */
490 if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
493 return tcp_conn_request(&mptcp_subflow_request_sock_ops,
494 &subflow_request_sock_ipv4_ops,
501 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
502 static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops;
503 static struct inet_connection_sock_af_ops subflow_v6_specific;
504 static struct inet_connection_sock_af_ops subflow_v6m_specific;
505 static struct proto tcpv6_prot_override;
507 static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
509 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
511 pr_debug("subflow=%p", subflow);
513 if (skb->protocol == htons(ETH_P_IP))
514 return subflow_v4_conn_request(sk, skb);
516 if (!ipv6_unicast_destination(skb))
519 if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) {
520 __IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS);
524 return tcp_conn_request(&mptcp_subflow_request_sock_ops,
525 &subflow_request_sock_ipv6_ops, sk, skb);
529 return 0; /* don't send reset */
533 /* validate hmac received in third ACK */
534 static bool subflow_hmac_valid(const struct request_sock *req,
535 const struct mptcp_options_received *mp_opt)
537 const struct mptcp_subflow_request_sock *subflow_req;
538 u8 hmac[SHA256_DIGEST_SIZE];
539 struct mptcp_sock *msk;
541 subflow_req = mptcp_subflow_rsk(req);
542 msk = subflow_req->msk;
546 subflow_generate_hmac(msk->remote_key, msk->local_key,
547 subflow_req->remote_nonce,
548 subflow_req->local_nonce, hmac);
550 return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN);
553 static void mptcp_sock_destruct(struct sock *sk)
555 /* if new mptcp socket isn't accepted, it is free'd
556 * from the tcp listener sockets request queue, linked
557 * from req->sk. The tcp socket is released.
558 * This calls the ULP release function which will
559 * also remove the mptcp socket, via
560 * sock_put(ctx->conn).
562 * Problem is that the mptcp socket will be in
563 * ESTABLISHED state and will not have the SOCK_DEAD flag.
564 * Both result in warnings from inet_sock_destruct.
566 if ((1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
567 sk->sk_state = TCP_CLOSE;
568 WARN_ON_ONCE(sk->sk_socket);
572 mptcp_destroy_common(mptcp_sk(sk));
573 inet_sock_destruct(sk);
576 static void mptcp_force_close(struct sock *sk)
578 /* the msk is not yet exposed to user-space */
579 inet_sk_state_store(sk, TCP_CLOSE);
580 sk_common_release(sk);
583 static void subflow_ulp_fallback(struct sock *sk,
584 struct mptcp_subflow_context *old_ctx)
586 struct inet_connection_sock *icsk = inet_csk(sk);
588 mptcp_subflow_tcp_fallback(sk, old_ctx);
589 icsk->icsk_ulp_ops = NULL;
590 rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
591 tcp_sk(sk)->is_mptcp = 0;
593 mptcp_subflow_ops_undo_override(sk);
596 static void subflow_drop_ctx(struct sock *ssk)
598 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
603 subflow_ulp_fallback(ssk, ctx);
610 void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
611 struct mptcp_options_received *mp_opt)
613 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
615 subflow->remote_key = mp_opt->sndr_key;
616 subflow->fully_established = 1;
617 subflow->can_ack = 1;
618 WRITE_ONCE(msk->fully_established, true);
621 static struct sock *subflow_syn_recv_sock(const struct sock *sk,
623 struct request_sock *req,
624 struct dst_entry *dst,
625 struct request_sock *req_unhash,
628 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk);
629 struct mptcp_subflow_request_sock *subflow_req;
630 struct mptcp_options_received mp_opt;
631 bool fallback, fallback_is_fatal;
632 struct sock *new_msk = NULL;
635 pr_debug("listener=%p, req=%p, conn=%p", listener, req, listener->conn);
637 /* After child creation we must look for 'mp_capable' even when options
640 mp_opt.mp_capable = 0;
642 /* hopefully temporary handling for MP_JOIN+syncookie */
643 subflow_req = mptcp_subflow_rsk(req);
644 fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join;
645 fallback = !tcp_rsk(req)->is_mptcp;
649 /* if the sk is MP_CAPABLE, we try to fetch the client key */
650 if (subflow_req->mp_capable) {
651 /* we can receive and accept an in-window, out-of-order pkt,
652 * which may not carry the MP_CAPABLE opt even on mptcp enabled
653 * paths: always try to extract the peer key, and fallback
654 * for packets missing it.
655 * Even OoO DSS packets coming legitly after dropped or
656 * reordered MPC will cause fallback, but we don't have other
659 mptcp_get_options(sk, skb, &mp_opt);
660 if (!mp_opt.mp_capable) {
665 new_msk = mptcp_sk_clone(listener->conn, &mp_opt, req);
668 } else if (subflow_req->mp_join) {
669 mptcp_get_options(sk, skb, &mp_opt);
670 if (!mp_opt.mp_join || !subflow_hmac_valid(req, &mp_opt) ||
671 !mptcp_can_accept_new_subflow(subflow_req->msk)) {
672 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
678 child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
679 req_unhash, own_req);
681 if (child && *own_req) {
682 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
684 tcp_rsk(req)->drop_req = false;
686 /* we need to fallback on ctx allocation failure and on pre-reqs
687 * checking above. In the latter scenario we additionally need
688 * to reset the context to non MPTCP status.
690 if (!ctx || fallback) {
691 if (fallback_is_fatal) {
692 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
696 subflow_drop_ctx(child);
700 /* ssk inherits options of listener sk */
701 ctx->setsockopt_seq = listener->setsockopt_seq;
703 if (ctx->mp_capable) {
704 /* this can't race with mptcp_close(), as the msk is
705 * not yet exposted to user-space
707 inet_sk_state_store((void *)new_msk, TCP_ESTABLISHED);
709 /* record the newly created socket as the first msk
710 * subflow, but don't link it yet into conn_list
712 WRITE_ONCE(mptcp_sk(new_msk)->first, child);
714 /* new mpc subflow takes ownership of the newly
715 * created mptcp socket
717 new_msk->sk_destruct = mptcp_sock_destruct;
718 mptcp_sk(new_msk)->setsockopt_seq = ctx->setsockopt_seq;
719 mptcp_pm_new_connection(mptcp_sk(new_msk), child, 1);
720 mptcp_token_accept(subflow_req, mptcp_sk(new_msk));
724 /* with OoO packets we can reach here without ingress
727 if (mp_opt.mp_capable)
728 mptcp_subflow_fully_established(ctx, &mp_opt);
729 } else if (ctx->mp_join) {
730 struct mptcp_sock *owner;
732 owner = subflow_req->msk;
734 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
738 /* move the msk reference ownership to the subflow */
739 subflow_req->msk = NULL;
740 ctx->conn = (struct sock *)owner;
742 if (subflow_use_different_sport(owner, sk)) {
743 pr_debug("ack inet_sport=%d %d",
744 ntohs(inet_sk(sk)->inet_sport),
745 ntohs(inet_sk((struct sock *)owner)->inet_sport));
746 if (!mptcp_pm_sport_in_anno_list(owner, sk)) {
747 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX);
750 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX);
753 if (!mptcp_finish_join(child))
756 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
757 tcp_rsk(req)->drop_req = true;
762 /* dispose of the left over mptcp master, if any */
763 if (unlikely(new_msk))
764 mptcp_force_close(new_msk);
766 /* check for expected invariant - should never trigger, just help
767 * catching eariler subtle bugs
769 WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
770 (!mptcp_subflow_ctx(child) ||
771 !mptcp_subflow_ctx(child)->conn));
775 subflow_drop_ctx(child);
776 tcp_rsk(req)->drop_req = true;
777 inet_csk_prepare_for_destroy_sock(child);
779 req->rsk_ops->send_reset(sk, skb);
781 /* The last child reference will be released by the caller */
785 static struct inet_connection_sock_af_ops subflow_specific;
786 static struct proto tcp_prot_override;
788 enum mapping_status {
796 static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
798 pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d",
799 ssn, subflow->map_subflow_seq, subflow->map_data_len);
802 static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
804 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
805 unsigned int skb_consumed;
807 skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
808 if (WARN_ON_ONCE(skb_consumed >= skb->len))
811 return skb->len - skb_consumed <= subflow->map_data_len -
812 mptcp_subflow_get_map_offset(subflow);
815 static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
817 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
818 u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
820 if (unlikely(before(ssn, subflow->map_subflow_seq))) {
821 /* Mapping covers data later in the subflow stream,
822 * currently unsupported.
824 dbg_bad_map(subflow, ssn);
827 if (unlikely(!before(ssn, subflow->map_subflow_seq +
828 subflow->map_data_len))) {
829 /* Mapping does covers past subflow data, invalid */
830 dbg_bad_map(subflow, ssn);
836 static enum mapping_status validate_data_csum(struct sock *ssk, struct sk_buff *skb,
839 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
840 struct csum_pseudo_header header;
841 u32 offset, seq, delta;
848 /* mapping already validated on previous traversal */
849 if (subflow->map_csum_len == subflow->map_data_len)
852 /* traverse the receive queue, ensuring it contains a full
853 * DSS mapping and accumulating the related csum.
854 * Preserve the accoumlate csum across multiple calls, to compute
857 delta = subflow->map_data_len - subflow->map_csum_len;
859 seq = tcp_sk(ssk)->copied_seq + subflow->map_csum_len;
860 offset = seq - TCP_SKB_CB(skb)->seq;
862 /* if the current skb has not been accounted yet, csum its contents
863 * up to the amount covered by the current DSS
865 if (offset < skb->len) {
868 len = min(skb->len - offset, delta);
869 csum = skb_checksum(skb, offset, len, 0);
870 subflow->map_data_csum = csum_block_add(subflow->map_data_csum, csum,
871 subflow->map_csum_len);
874 subflow->map_csum_len += len;
879 if (skb_queue_is_last(&ssk->sk_receive_queue, skb)) {
880 /* if this subflow is closed, the partial mapping
881 * will be never completed; flush the pending skbs, so
882 * that subflow_sched_work_if_closed() can kick in
884 if (unlikely(ssk->sk_state == TCP_CLOSE))
885 while ((skb = skb_peek(&ssk->sk_receive_queue)))
886 sk_eat_skb(ssk, skb);
888 /* not enough data to validate the csum */
889 return MAPPING_EMPTY;
892 /* the DSS mapping for next skbs will be validated later,
893 * when a get_mapping_status call will process such skb
898 /* note that 'map_data_len' accounts only for the carried data, does
899 * not include the eventual seq increment due to the data fin,
900 * while the pseudo header requires the original DSS data len,
903 header.data_seq = cpu_to_be64(subflow->map_seq);
904 header.subflow_seq = htonl(subflow->map_subflow_seq);
905 header.data_len = htons(subflow->map_data_len + subflow->map_data_fin);
908 csum = csum_partial(&header, sizeof(header), subflow->map_data_csum);
909 if (unlikely(csum_fold(csum))) {
910 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DATACSUMERR);
911 return subflow->mp_join ? MAPPING_INVALID : MAPPING_DUMMY;
917 static enum mapping_status get_mapping_status(struct sock *ssk,
918 struct mptcp_sock *msk)
920 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
921 bool csum_reqd = READ_ONCE(msk->csum_enabled);
922 struct mptcp_ext *mpext;
927 skb = skb_peek(&ssk->sk_receive_queue);
929 return MAPPING_EMPTY;
931 if (mptcp_check_fallback(ssk))
932 return MAPPING_DUMMY;
934 mpext = mptcp_get_ext(skb);
935 if (!mpext || !mpext->use_map) {
936 if (!subflow->map_valid && !skb->len) {
937 /* the TCP stack deliver 0 len FIN pkt to the receive
938 * queue, that is the only 0len pkts ever expected here,
939 * and we can admit no mapping only for 0 len pkts
941 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
942 WARN_ONCE(1, "0len seq %d:%d flags %x",
943 TCP_SKB_CB(skb)->seq,
944 TCP_SKB_CB(skb)->end_seq,
945 TCP_SKB_CB(skb)->tcp_flags);
946 sk_eat_skb(ssk, skb);
947 return MAPPING_EMPTY;
950 if (!subflow->map_valid)
951 return MAPPING_INVALID;
956 trace_get_mapping_status(mpext);
958 data_len = mpext->data_len;
960 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
961 return MAPPING_INVALID;
964 if (mpext->data_fin == 1) {
966 bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
968 pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
969 if (subflow->map_valid) {
970 /* A DATA_FIN might arrive in a DSS
971 * option before the previous mapping
972 * has been fully consumed. Continue
973 * handling the existing mapping.
975 skb_ext_del(skb, SKB_EXT_MPTCP);
978 if (updated && schedule_work(&msk->work))
979 sock_hold((struct sock *)msk);
981 return MAPPING_DATA_FIN;
984 u64 data_fin_seq = mpext->data_seq + data_len - 1;
986 /* If mpext->data_seq is a 32-bit value, data_fin_seq
987 * must also be limited to 32 bits.
990 data_fin_seq &= GENMASK_ULL(31, 0);
992 mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
993 pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d",
994 data_fin_seq, mpext->dsn64);
997 /* Adjust for DATA_FIN using 1 byte of sequence space */
1001 map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64);
1002 WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
1004 if (subflow->map_valid) {
1005 /* Allow replacing only with an identical map */
1006 if (subflow->map_seq == map_seq &&
1007 subflow->map_subflow_seq == mpext->subflow_seq &&
1008 subflow->map_data_len == data_len &&
1009 subflow->map_csum_reqd == mpext->csum_reqd) {
1010 skb_ext_del(skb, SKB_EXT_MPTCP);
1014 /* If this skb data are fully covered by the current mapping,
1015 * the new map would need caching, which is not supported
1017 if (skb_is_fully_mapped(ssk, skb)) {
1018 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
1019 return MAPPING_INVALID;
1022 /* will validate the next map after consuming the current one */
1026 subflow->map_seq = map_seq;
1027 subflow->map_subflow_seq = mpext->subflow_seq;
1028 subflow->map_data_len = data_len;
1029 subflow->map_valid = 1;
1030 subflow->map_data_fin = mpext->data_fin;
1031 subflow->mpc_map = mpext->mpc_map;
1032 subflow->map_csum_reqd = mpext->csum_reqd;
1033 subflow->map_csum_len = 0;
1034 subflow->map_data_csum = csum_unfold(mpext->csum);
1036 /* Cfr RFC 8684 Section 3.3.0 */
1037 if (unlikely(subflow->map_csum_reqd != csum_reqd))
1038 return MAPPING_INVALID;
1040 pr_debug("new map seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
1041 subflow->map_seq, subflow->map_subflow_seq,
1042 subflow->map_data_len, subflow->map_csum_reqd,
1043 subflow->map_data_csum);
1046 /* we revalidate valid mapping on new skb, because we must ensure
1047 * the current skb is completely covered by the available mapping
1049 if (!validate_mapping(ssk, skb)) {
1050 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSTCPMISMATCH);
1051 return MAPPING_INVALID;
1054 skb_ext_del(skb, SKB_EXT_MPTCP);
1057 return validate_data_csum(ssk, skb, csum_reqd);
1060 static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
1063 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1064 bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
1067 incr = limit >= skb->len ? skb->len + fin : limit;
1069 pr_debug("discarding=%d len=%d seq=%d", incr, skb->len,
1070 subflow->map_subflow_seq);
1071 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
1072 tcp_sk(ssk)->copied_seq += incr;
1073 if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
1074 sk_eat_skb(ssk, skb);
1075 if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
1076 subflow->map_valid = 0;
1079 /* sched mptcp worker to remove the subflow if no more data is pending */
1080 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
1082 struct sock *sk = (struct sock *)msk;
1084 if (likely(ssk->sk_state != TCP_CLOSE))
1087 if (skb_queue_empty(&ssk->sk_receive_queue) &&
1088 !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) {
1090 if (!schedule_work(&msk->work))
1095 static bool subflow_check_data_avail(struct sock *ssk)
1097 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1098 enum mapping_status status;
1099 struct mptcp_sock *msk;
1100 struct sk_buff *skb;
1102 if (!skb_peek(&ssk->sk_receive_queue))
1103 WRITE_ONCE(subflow->data_avail, 0);
1104 if (subflow->data_avail)
1107 msk = mptcp_sk(subflow->conn);
1112 status = get_mapping_status(ssk, msk);
1113 trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue));
1114 if (unlikely(status == MAPPING_INVALID))
1117 if (unlikely(status == MAPPING_DUMMY))
1120 if (status != MAPPING_OK)
1123 skb = skb_peek(&ssk->sk_receive_queue);
1124 if (WARN_ON_ONCE(!skb))
1127 /* if msk lacks the remote key, this subflow must provide an
1128 * MP_CAPABLE-based mapping
1130 if (unlikely(!READ_ONCE(msk->can_ack))) {
1131 if (!subflow->mpc_map)
1133 WRITE_ONCE(msk->remote_key, subflow->remote_key);
1134 WRITE_ONCE(msk->ack_seq, subflow->map_seq);
1135 WRITE_ONCE(msk->can_ack, true);
1138 old_ack = READ_ONCE(msk->ack_seq);
1139 ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
1140 pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack,
1142 if (unlikely(before64(ack_seq, old_ack))) {
1143 mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
1147 WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1153 subflow_sched_work_if_closed(msk, ssk);
1157 /* RFC 8684 section 3.7. */
1158 if (subflow->mp_join || subflow->fully_established) {
1159 /* fatal protocol error, close the socket.
1160 * subflow_error_report() will introduce the appropriate barriers
1162 ssk->sk_err = EBADMSG;
1163 tcp_set_state(ssk, TCP_CLOSE);
1164 subflow->reset_transient = 0;
1165 subflow->reset_reason = MPTCP_RST_EMPTCP;
1166 tcp_send_active_reset(ssk, GFP_ATOMIC);
1167 WRITE_ONCE(subflow->data_avail, 0);
1171 __mptcp_do_fallback(msk);
1172 skb = skb_peek(&ssk->sk_receive_queue);
1173 subflow->map_valid = 1;
1174 subflow->map_seq = READ_ONCE(msk->ack_seq);
1175 subflow->map_data_len = skb->len;
1176 subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
1177 WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1181 bool mptcp_subflow_data_available(struct sock *sk)
1183 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1185 /* check if current mapping is still valid */
1186 if (subflow->map_valid &&
1187 mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
1188 subflow->map_valid = 0;
1189 WRITE_ONCE(subflow->data_avail, 0);
1191 pr_debug("Done with mapping: seq=%u data_len=%u",
1192 subflow->map_subflow_seq,
1193 subflow->map_data_len);
1196 return subflow_check_data_avail(sk);
1199 /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
1202 * In mptcp, rwin is about the mptcp-level connection data.
1204 * Data that is still on the ssk rx queue can thus be ignored,
1205 * as far as mptcp peer is concerned that data is still inflight.
1206 * DSS ACK is updated when skb is moved to the mptcp rx queue.
1208 void mptcp_space(const struct sock *ssk, int *space, int *full_space)
1210 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1211 const struct sock *sk = subflow->conn;
1213 *space = __mptcp_space(sk);
1214 *full_space = tcp_full_space(sk);
1217 void __mptcp_error_report(struct sock *sk)
1219 struct mptcp_subflow_context *subflow;
1220 struct mptcp_sock *msk = mptcp_sk(sk);
1222 mptcp_for_each_subflow(msk, subflow) {
1223 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1224 int err = sock_error(ssk);
1229 /* only propagate errors on fallen-back sockets or
1232 if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk))
1235 inet_sk_state_store(sk, inet_sk_state_load(ssk));
1238 /* This barrier is coupled with smp_rmb() in mptcp_poll() */
1240 sk_error_report(sk);
1245 static void subflow_error_report(struct sock *ssk)
1247 struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1249 mptcp_data_lock(sk);
1250 if (!sock_owned_by_user(sk))
1251 __mptcp_error_report(sk);
1253 set_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags);
1254 mptcp_data_unlock(sk);
1257 static void subflow_data_ready(struct sock *sk)
1259 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1260 u16 state = 1 << inet_sk_state_load(sk);
1261 struct sock *parent = subflow->conn;
1262 struct mptcp_sock *msk;
1264 msk = mptcp_sk(parent);
1265 if (state & TCPF_LISTEN) {
1266 /* MPJ subflow are removed from accept queue before reaching here,
1267 * avoid stray wakeups
1269 if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
1272 set_bit(MPTCP_DATA_READY, &msk->flags);
1273 parent->sk_data_ready(parent);
1277 WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
1278 !subflow->mp_join && !(state & TCPF_CLOSE));
1280 if (mptcp_subflow_data_available(sk))
1281 mptcp_data_ready(parent, sk);
1282 else if (unlikely(sk->sk_err))
1283 subflow_error_report(sk);
1286 static void subflow_write_space(struct sock *ssk)
1288 struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1290 mptcp_propagate_sndbuf(sk, ssk);
1291 mptcp_write_space(sk);
1294 static struct inet_connection_sock_af_ops *
1295 subflow_default_af_ops(struct sock *sk)
1297 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1298 if (sk->sk_family == AF_INET6)
1299 return &subflow_v6_specific;
1301 return &subflow_specific;
1304 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1305 void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
1307 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1308 struct inet_connection_sock *icsk = inet_csk(sk);
1309 struct inet_connection_sock_af_ops *target;
1311 target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
1313 pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d",
1314 subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
1316 if (likely(icsk->icsk_af_ops == target))
1319 subflow->icsk_af_ops = icsk->icsk_af_ops;
1320 icsk->icsk_af_ops = target;
1324 void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
1325 struct sockaddr_storage *addr,
1326 unsigned short family)
1328 memset(addr, 0, sizeof(*addr));
1329 addr->ss_family = family;
1330 if (addr->ss_family == AF_INET) {
1331 struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
1333 if (info->family == AF_INET)
1334 in_addr->sin_addr = info->addr;
1335 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1336 else if (ipv6_addr_v4mapped(&info->addr6))
1337 in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3];
1339 in_addr->sin_port = info->port;
1341 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1342 else if (addr->ss_family == AF_INET6) {
1343 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;
1345 if (info->family == AF_INET)
1346 ipv6_addr_set_v4mapped(info->addr.s_addr,
1347 &in6_addr->sin6_addr);
1349 in6_addr->sin6_addr = info->addr6;
1350 in6_addr->sin6_port = info->port;
1355 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
1356 const struct mptcp_addr_info *remote,
1357 u8 flags, int ifindex)
1359 struct mptcp_sock *msk = mptcp_sk(sk);
1360 struct mptcp_subflow_context *subflow;
1361 struct sockaddr_storage addr;
1362 int remote_id = remote->id;
1363 int local_id = loc->id;
1370 if (!mptcp_is_fully_established(sk))
1373 err = mptcp_subflow_create_socket(sk, &sf);
1378 subflow = mptcp_subflow_ctx(ssk);
1380 get_random_bytes(&subflow->local_nonce, sizeof(u32));
1381 } while (!subflow->local_nonce);
1384 err = mptcp_pm_get_local_id(msk, (struct sock_common *)ssk);
1391 subflow->remote_key = msk->remote_key;
1392 subflow->local_key = msk->local_key;
1393 subflow->token = msk->token;
1394 mptcp_info2sockaddr(loc, &addr, ssk->sk_family);
1396 addrlen = sizeof(struct sockaddr_in);
1397 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1398 if (addr.ss_family == AF_INET6)
1399 addrlen = sizeof(struct sockaddr_in6);
1401 ssk->sk_bound_dev_if = ifindex;
1402 err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
1406 mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1407 pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk,
1408 remote_token, local_id, remote_id);
1409 subflow->remote_token = remote_token;
1410 subflow->local_id = local_id;
1411 subflow->remote_id = remote_id;
1412 subflow->request_join = 1;
1413 subflow->request_bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1414 mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
1416 mptcp_add_pending_subflow(msk, subflow);
1417 mptcp_sockopt_sync(msk, ssk);
1418 err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
1419 if (err && err != -EINPROGRESS)
1422 /* discard the subflow socket */
1423 mptcp_sock_graft(ssk, sk->sk_socket);
1424 iput(SOCK_INODE(sf));
1428 spin_lock_bh(&msk->join_list_lock);
1429 list_del(&subflow->node);
1430 spin_unlock_bh(&msk->join_list_lock);
1431 sock_put(mptcp_subflow_tcp_sock(subflow));
1434 subflow->disposable = 1;
1439 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
1441 #ifdef CONFIG_SOCK_CGROUP_DATA
1442 struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
1443 *child_skcd = &child->sk_cgrp_data;
1445 /* only the additional subflows created by kworkers have to be modified */
1446 if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
1447 cgroup_id(sock_cgroup_ptr(child_skcd))) {
1449 struct mem_cgroup *memcg = parent->sk_memcg;
1451 mem_cgroup_sk_free(child);
1452 if (memcg && css_tryget(&memcg->css))
1453 child->sk_memcg = memcg;
1454 #endif /* CONFIG_MEMCG */
1456 cgroup_sk_free(child_skcd);
1457 *child_skcd = *parent_skcd;
1458 cgroup_sk_clone(child_skcd);
1460 #endif /* CONFIG_SOCK_CGROUP_DATA */
1463 static void mptcp_subflow_ops_override(struct sock *ssk)
1465 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1466 if (ssk->sk_prot == &tcpv6_prot)
1467 ssk->sk_prot = &tcpv6_prot_override;
1470 ssk->sk_prot = &tcp_prot_override;
1473 static void mptcp_subflow_ops_undo_override(struct sock *ssk)
1475 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1476 if (ssk->sk_prot == &tcpv6_prot_override)
1477 ssk->sk_prot = &tcpv6_prot;
1480 ssk->sk_prot = &tcp_prot;
1482 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)
1484 struct mptcp_subflow_context *subflow;
1485 struct net *net = sock_net(sk);
1489 /* un-accepted server sockets can reach here - on bad configuration
1490 * bail early to avoid greater trouble later
1492 if (unlikely(!sk->sk_socket))
1495 err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP,
1502 /* the newly created socket has to be in the same cgroup as its parent */
1503 mptcp_attach_cgroup(sk, sf->sk);
1505 /* kernel sockets do not by default acquire net ref, but TCP timer
1508 sf->sk->sk_net_refcnt = 1;
1510 #ifdef CONFIG_PROC_FS
1511 this_cpu_add(*net->core.sock_inuse, 1);
1513 err = tcp_set_ulp(sf->sk, "mptcp");
1514 release_sock(sf->sk);
1521 /* the newly created socket really belongs to the owning MPTCP master
1522 * socket, even if for additional subflows the allocation is performed
1523 * by a kernel workqueue. Adjust inode references, so that the
1524 * procfs/diag interaces really show this one belonging to the correct
1527 SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
1528 SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
1529 SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
1531 subflow = mptcp_subflow_ctx(sf->sk);
1532 pr_debug("subflow=%p", subflow);
1537 mptcp_subflow_ops_override(sf->sk);
1542 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
1545 struct inet_connection_sock *icsk = inet_csk(sk);
1546 struct mptcp_subflow_context *ctx;
1548 ctx = kzalloc(sizeof(*ctx), priority);
1552 rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
1553 INIT_LIST_HEAD(&ctx->node);
1554 INIT_LIST_HEAD(&ctx->delegated_node);
1556 pr_debug("subflow=%p", ctx);
1563 static void __subflow_state_change(struct sock *sk)
1565 struct socket_wq *wq;
1568 wq = rcu_dereference(sk->sk_wq);
1569 if (skwq_has_sleeper(wq))
1570 wake_up_interruptible_all(&wq->wait);
1574 static bool subflow_is_done(const struct sock *sk)
1576 return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
1579 static void subflow_state_change(struct sock *sk)
1581 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1582 struct sock *parent = subflow->conn;
1584 __subflow_state_change(sk);
1586 if (subflow_simultaneous_connect(sk)) {
1587 mptcp_propagate_sndbuf(parent, sk);
1588 mptcp_do_fallback(sk);
1589 mptcp_rcv_space_init(mptcp_sk(parent), sk);
1590 pr_fallback(mptcp_sk(parent));
1591 subflow->conn_finished = 1;
1592 mptcp_set_connected(parent);
1595 /* as recvmsg() does not acquire the subflow socket for ssk selection
1596 * a fin packet carrying a DSS can be unnoticed if we don't trigger
1597 * the data available machinery here.
1599 if (mptcp_subflow_data_available(sk))
1600 mptcp_data_ready(parent, sk);
1601 else if (unlikely(sk->sk_err))
1602 subflow_error_report(sk);
1604 subflow_sched_work_if_closed(mptcp_sk(parent), sk);
1606 if (__mptcp_check_fallback(mptcp_sk(parent)) &&
1607 !subflow->rx_eof && subflow_is_done(sk)) {
1608 subflow->rx_eof = 1;
1609 mptcp_subflow_eof(parent);
1613 static int subflow_ulp_init(struct sock *sk)
1615 struct inet_connection_sock *icsk = inet_csk(sk);
1616 struct mptcp_subflow_context *ctx;
1617 struct tcp_sock *tp = tcp_sk(sk);
1620 /* disallow attaching ULP to a socket unless it has been
1621 * created with sock_create_kern()
1623 if (!sk->sk_kern_sock) {
1628 ctx = subflow_create_ctx(sk, GFP_KERNEL);
1634 pr_debug("subflow=%p, family=%d", ctx, sk->sk_family);
1637 ctx->icsk_af_ops = icsk->icsk_af_ops;
1638 icsk->icsk_af_ops = subflow_default_af_ops(sk);
1639 ctx->tcp_data_ready = sk->sk_data_ready;
1640 ctx->tcp_state_change = sk->sk_state_change;
1641 ctx->tcp_write_space = sk->sk_write_space;
1642 ctx->tcp_error_report = sk->sk_error_report;
1643 sk->sk_data_ready = subflow_data_ready;
1644 sk->sk_write_space = subflow_write_space;
1645 sk->sk_state_change = subflow_state_change;
1646 sk->sk_error_report = subflow_error_report;
1651 static void subflow_ulp_release(struct sock *ssk)
1653 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
1654 bool release = true;
1662 /* if the msk has been orphaned, keep the ctx
1663 * alive, will be freed by __mptcp_close_ssk(),
1664 * when the subflow is still unaccepted
1666 release = ctx->disposable || list_empty(&ctx->node);
1670 mptcp_subflow_ops_undo_override(ssk);
1672 kfree_rcu(ctx, rcu);
1675 static void subflow_ulp_clone(const struct request_sock *req,
1677 const gfp_t priority)
1679 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
1680 struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
1681 struct mptcp_subflow_context *new_ctx;
1683 if (!tcp_rsk(req)->is_mptcp ||
1684 (!subflow_req->mp_capable && !subflow_req->mp_join)) {
1685 subflow_ulp_fallback(newsk, old_ctx);
1689 new_ctx = subflow_create_ctx(newsk, priority);
1691 subflow_ulp_fallback(newsk, old_ctx);
1695 new_ctx->conn_finished = 1;
1696 new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
1697 new_ctx->tcp_data_ready = old_ctx->tcp_data_ready;
1698 new_ctx->tcp_state_change = old_ctx->tcp_state_change;
1699 new_ctx->tcp_write_space = old_ctx->tcp_write_space;
1700 new_ctx->tcp_error_report = old_ctx->tcp_error_report;
1701 new_ctx->rel_write_seq = 1;
1702 new_ctx->tcp_sock = newsk;
1704 if (subflow_req->mp_capable) {
1705 /* see comments in subflow_syn_recv_sock(), MPTCP connection
1706 * is fully established only after we receive the remote key
1708 new_ctx->mp_capable = 1;
1709 new_ctx->local_key = subflow_req->local_key;
1710 new_ctx->token = subflow_req->token;
1711 new_ctx->ssn_offset = subflow_req->ssn_offset;
1712 new_ctx->idsn = subflow_req->idsn;
1713 } else if (subflow_req->mp_join) {
1714 new_ctx->ssn_offset = subflow_req->ssn_offset;
1715 new_ctx->mp_join = 1;
1716 new_ctx->fully_established = 1;
1717 new_ctx->backup = subflow_req->backup;
1718 new_ctx->local_id = subflow_req->local_id;
1719 new_ctx->remote_id = subflow_req->remote_id;
1720 new_ctx->token = subflow_req->token;
1721 new_ctx->thmac = subflow_req->thmac;
1725 static void tcp_release_cb_override(struct sock *ssk)
1727 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1729 if (mptcp_subflow_has_delegated_action(subflow))
1730 mptcp_subflow_process_delegated(ssk);
1732 tcp_release_cb(ssk);
1735 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
1737 .owner = THIS_MODULE,
1738 .init = subflow_ulp_init,
1739 .release = subflow_ulp_release,
1740 .clone = subflow_ulp_clone,
1743 static int subflow_ops_init(struct request_sock_ops *subflow_ops)
1745 subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
1746 subflow_ops->slab_name = "request_sock_subflow";
1748 subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
1749 subflow_ops->obj_size, 0,
1751 SLAB_TYPESAFE_BY_RCU,
1753 if (!subflow_ops->slab)
1756 subflow_ops->destructor = subflow_req_destructor;
1761 void __init mptcp_subflow_init(void)
1763 mptcp_subflow_request_sock_ops = tcp_request_sock_ops;
1764 if (subflow_ops_init(&mptcp_subflow_request_sock_ops) != 0)
1765 panic("MPTCP: failed to init subflow request sock ops\n");
1767 subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
1768 subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
1770 subflow_specific = ipv4_specific;
1771 subflow_specific.conn_request = subflow_v4_conn_request;
1772 subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
1773 subflow_specific.sk_rx_dst_set = subflow_finish_connect;
1775 tcp_prot_override = tcp_prot;
1776 tcp_prot_override.release_cb = tcp_release_cb_override;
1778 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1779 subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
1780 subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
1782 subflow_v6_specific = ipv6_specific;
1783 subflow_v6_specific.conn_request = subflow_v6_conn_request;
1784 subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
1785 subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
1787 subflow_v6m_specific = subflow_v6_specific;
1788 subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
1789 subflow_v6m_specific.send_check = ipv4_specific.send_check;
1790 subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
1791 subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
1792 subflow_v6m_specific.net_frag_header_len = 0;
1794 tcpv6_prot_override = tcpv6_prot;
1795 tcpv6_prot_override.release_cb = tcp_release_cb_override;
1798 mptcp_diag_subflow_init(&subflow_ulp_ops);
1800 if (tcp_register_ulp(&subflow_ulp_ops) != 0)
1801 panic("MPTCP: failed to register subflows to ULP\n");