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->msk = NULL;
112 mptcp_token_init_request(req);
115 static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk)
117 return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport;
120 static void subflow_add_reset_reason(struct sk_buff *skb, u8 reason)
122 struct mptcp_ext *mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
125 memset(mpext, 0, sizeof(*mpext));
126 mpext->reset_reason = reason;
130 /* Init mptcp request socket.
132 * Returns an error code if a JOIN has failed and a TCP reset
135 static int subflow_check_req(struct request_sock *req,
136 const struct sock *sk_listener,
139 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
140 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
141 struct mptcp_options_received mp_opt;
143 pr_debug("subflow_req=%p, listener=%p", subflow_req, listener);
145 #ifdef CONFIG_TCP_MD5SIG
146 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
149 if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info))
153 mptcp_get_options(sk_listener, skb, &mp_opt);
155 if (mp_opt.mp_capable) {
156 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE);
160 } else if (mp_opt.mp_join) {
161 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX);
164 if (mp_opt.mp_capable && listener->request_mptcp) {
165 int err, retries = 4;
167 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
170 get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key));
171 } while (subflow_req->local_key == 0);
173 if (unlikely(req->syncookie)) {
174 mptcp_crypto_key_sha(subflow_req->local_key,
177 if (mptcp_token_exists(subflow_req->token)) {
180 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
182 subflow_req->mp_capable = 1;
187 err = mptcp_token_new_request(req);
189 subflow_req->mp_capable = 1;
190 else if (retries-- > 0)
193 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
195 } else if (mp_opt.mp_join && listener->request_mptcp) {
196 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
197 subflow_req->mp_join = 1;
198 subflow_req->backup = mp_opt.backup;
199 subflow_req->remote_id = mp_opt.join_id;
200 subflow_req->token = mp_opt.token;
201 subflow_req->remote_nonce = mp_opt.nonce;
202 subflow_req->msk = subflow_token_join_request(req);
204 /* Can't fall back to TCP in this case. */
205 if (!subflow_req->msk) {
206 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
210 if (subflow_use_different_sport(subflow_req->msk, sk_listener)) {
211 pr_debug("syn inet_sport=%d %d",
212 ntohs(inet_sk(sk_listener)->inet_sport),
213 ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport));
214 if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener)) {
215 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX);
218 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX);
221 subflow_req_create_thmac(subflow_req);
223 if (unlikely(req->syncookie)) {
224 if (mptcp_can_accept_new_subflow(subflow_req->msk))
225 subflow_init_req_cookie_join_save(subflow_req, skb);
230 pr_debug("token=%u, remote_nonce=%u msk=%p", subflow_req->token,
231 subflow_req->remote_nonce, subflow_req->msk);
237 int mptcp_subflow_init_cookie_req(struct request_sock *req,
238 const struct sock *sk_listener,
241 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
242 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
243 struct mptcp_options_received mp_opt;
246 subflow_init_req(req, sk_listener);
247 mptcp_get_options(sk_listener, skb, &mp_opt);
249 if (mp_opt.mp_capable && mp_opt.mp_join)
252 if (mp_opt.mp_capable && listener->request_mptcp) {
253 if (mp_opt.sndr_key == 0)
256 subflow_req->local_key = mp_opt.rcvr_key;
257 err = mptcp_token_new_request(req);
261 subflow_req->mp_capable = 1;
262 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
263 } else if (mp_opt.mp_join && listener->request_mptcp) {
264 if (!mptcp_token_join_cookie_init_state(subflow_req, skb))
267 subflow_req->mp_join = 1;
268 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
273 EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req);
275 static struct dst_entry *subflow_v4_route_req(const struct sock *sk,
278 struct request_sock *req)
280 struct dst_entry *dst;
283 tcp_rsk(req)->is_mptcp = 1;
284 subflow_init_req(req, sk);
286 dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req);
290 err = subflow_check_req(req, sk, skb);
296 tcp_request_sock_ops.send_reset(sk, skb);
300 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
301 static struct dst_entry *subflow_v6_route_req(const struct sock *sk,
304 struct request_sock *req)
306 struct dst_entry *dst;
309 tcp_rsk(req)->is_mptcp = 1;
310 subflow_init_req(req, sk);
312 dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req);
316 err = subflow_check_req(req, sk, skb);
322 tcp6_request_sock_ops.send_reset(sk, skb);
327 /* validate received truncated hmac and create hmac for third ACK */
328 static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
330 u8 hmac[SHA256_DIGEST_SIZE];
333 subflow_generate_hmac(subflow->remote_key, subflow->local_key,
334 subflow->remote_nonce, subflow->local_nonce,
337 thmac = get_unaligned_be64(hmac);
338 pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n",
339 subflow, subflow->token,
340 (unsigned long long)thmac,
341 (unsigned long long)subflow->thmac);
343 return thmac == subflow->thmac;
346 void mptcp_subflow_reset(struct sock *ssk)
348 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
349 struct sock *sk = subflow->conn;
351 /* must hold: tcp_done() could drop last reference on parent */
354 tcp_set_state(ssk, TCP_CLOSE);
355 tcp_send_active_reset(ssk, GFP_ATOMIC);
357 if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags) &&
358 schedule_work(&mptcp_sk(sk)->work))
359 return; /* worker will put sk for us */
364 static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk)
366 return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport;
369 void __mptcp_set_connected(struct sock *sk)
371 if (sk->sk_state == TCP_SYN_SENT) {
372 inet_sk_state_store(sk, TCP_ESTABLISHED);
373 sk->sk_state_change(sk);
377 static void mptcp_set_connected(struct sock *sk)
380 if (!sock_owned_by_user(sk))
381 __mptcp_set_connected(sk);
383 set_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->flags);
384 mptcp_data_unlock(sk);
387 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
389 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
390 struct mptcp_options_received mp_opt;
391 struct sock *parent = subflow->conn;
393 subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);
396 /* be sure no special action on any packet other than syn-ack */
397 if (subflow->conn_finished)
400 mptcp_propagate_sndbuf(parent, sk);
401 subflow->rel_write_seq = 1;
402 subflow->conn_finished = 1;
403 subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
404 pr_debug("subflow=%p synack seq=%x", subflow, subflow->ssn_offset);
406 mptcp_get_options(sk, skb, &mp_opt);
407 if (subflow->request_mptcp) {
408 if (!mp_opt.mp_capable) {
409 MPTCP_INC_STATS(sock_net(sk),
410 MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
411 mptcp_do_fallback(sk);
412 pr_fallback(mptcp_sk(subflow->conn));
416 subflow->mp_capable = 1;
417 subflow->can_ack = 1;
418 subflow->remote_key = mp_opt.sndr_key;
419 pr_debug("subflow=%p, remote_key=%llu", subflow,
420 subflow->remote_key);
421 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK);
422 mptcp_finish_connect(sk);
423 mptcp_set_connected(parent);
424 } else if (subflow->request_join) {
425 u8 hmac[SHA256_DIGEST_SIZE];
427 if (!mp_opt.mp_join) {
428 subflow->reset_reason = MPTCP_RST_EMPTCP;
432 subflow->thmac = mp_opt.thmac;
433 subflow->remote_nonce = mp_opt.nonce;
434 pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u", subflow,
435 subflow->thmac, subflow->remote_nonce);
437 if (!subflow_thmac_valid(subflow)) {
438 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC);
439 subflow->reset_reason = MPTCP_RST_EMPTCP;
443 if (!mptcp_finish_join(sk))
446 subflow_generate_hmac(subflow->local_key, subflow->remote_key,
447 subflow->local_nonce,
448 subflow->remote_nonce,
450 memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
452 subflow->mp_join = 1;
453 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
455 if (subflow_use_different_dport(mptcp_sk(parent), sk)) {
456 pr_debug("synack inet_dport=%d %d",
457 ntohs(inet_sk(sk)->inet_dport),
458 ntohs(inet_sk(parent)->inet_dport));
459 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX);
461 } else if (mptcp_check_fallback(sk)) {
463 mptcp_rcv_space_init(mptcp_sk(parent), sk);
464 mptcp_set_connected(parent);
469 subflow->reset_transient = 0;
470 mptcp_subflow_reset(sk);
473 struct request_sock_ops mptcp_subflow_request_sock_ops;
474 EXPORT_SYMBOL_GPL(mptcp_subflow_request_sock_ops);
475 static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops;
477 static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
479 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
481 pr_debug("subflow=%p", subflow);
483 /* Never answer to SYNs sent to broadcast or multicast */
484 if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
487 return tcp_conn_request(&mptcp_subflow_request_sock_ops,
488 &subflow_request_sock_ipv4_ops,
495 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
496 static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops;
497 static struct inet_connection_sock_af_ops subflow_v6_specific;
498 static struct inet_connection_sock_af_ops subflow_v6m_specific;
499 static struct proto tcpv6_prot_override;
501 static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
503 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
505 pr_debug("subflow=%p", subflow);
507 if (skb->protocol == htons(ETH_P_IP))
508 return subflow_v4_conn_request(sk, skb);
510 if (!ipv6_unicast_destination(skb))
513 if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) {
514 __IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS);
518 return tcp_conn_request(&mptcp_subflow_request_sock_ops,
519 &subflow_request_sock_ipv6_ops, sk, skb);
523 return 0; /* don't send reset */
527 /* validate hmac received in third ACK */
528 static bool subflow_hmac_valid(const struct request_sock *req,
529 const struct mptcp_options_received *mp_opt)
531 const struct mptcp_subflow_request_sock *subflow_req;
532 u8 hmac[SHA256_DIGEST_SIZE];
533 struct mptcp_sock *msk;
535 subflow_req = mptcp_subflow_rsk(req);
536 msk = subflow_req->msk;
540 subflow_generate_hmac(msk->remote_key, msk->local_key,
541 subflow_req->remote_nonce,
542 subflow_req->local_nonce, hmac);
544 return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN);
547 static void mptcp_sock_destruct(struct sock *sk)
549 /* if new mptcp socket isn't accepted, it is free'd
550 * from the tcp listener sockets request queue, linked
551 * from req->sk. The tcp socket is released.
552 * This calls the ULP release function which will
553 * also remove the mptcp socket, via
554 * sock_put(ctx->conn).
556 * Problem is that the mptcp socket will be in
557 * ESTABLISHED state and will not have the SOCK_DEAD flag.
558 * Both result in warnings from inet_sock_destruct.
560 if ((1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
561 sk->sk_state = TCP_CLOSE;
562 WARN_ON_ONCE(sk->sk_socket);
566 mptcp_destroy_common(mptcp_sk(sk));
567 inet_sock_destruct(sk);
570 static void mptcp_force_close(struct sock *sk)
572 /* the msk is not yet exposed to user-space */
573 inet_sk_state_store(sk, TCP_CLOSE);
574 sk_common_release(sk);
577 static void subflow_ulp_fallback(struct sock *sk,
578 struct mptcp_subflow_context *old_ctx)
580 struct inet_connection_sock *icsk = inet_csk(sk);
582 mptcp_subflow_tcp_fallback(sk, old_ctx);
583 icsk->icsk_ulp_ops = NULL;
584 rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
585 tcp_sk(sk)->is_mptcp = 0;
587 mptcp_subflow_ops_undo_override(sk);
590 static void subflow_drop_ctx(struct sock *ssk)
592 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
597 subflow_ulp_fallback(ssk, ctx);
604 void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
605 struct mptcp_options_received *mp_opt)
607 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
609 subflow->remote_key = mp_opt->sndr_key;
610 subflow->fully_established = 1;
611 subflow->can_ack = 1;
612 WRITE_ONCE(msk->fully_established, true);
615 static struct sock *subflow_syn_recv_sock(const struct sock *sk,
617 struct request_sock *req,
618 struct dst_entry *dst,
619 struct request_sock *req_unhash,
622 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk);
623 struct mptcp_subflow_request_sock *subflow_req;
624 struct mptcp_options_received mp_opt;
625 bool fallback, fallback_is_fatal;
626 struct sock *new_msk = NULL;
629 pr_debug("listener=%p, req=%p, conn=%p", listener, req, listener->conn);
631 /* After child creation we must look for 'mp_capable' even when options
634 mp_opt.mp_capable = 0;
636 /* hopefully temporary handling for MP_JOIN+syncookie */
637 subflow_req = mptcp_subflow_rsk(req);
638 fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join;
639 fallback = !tcp_rsk(req)->is_mptcp;
643 /* if the sk is MP_CAPABLE, we try to fetch the client key */
644 if (subflow_req->mp_capable) {
645 /* we can receive and accept an in-window, out-of-order pkt,
646 * which may not carry the MP_CAPABLE opt even on mptcp enabled
647 * paths: always try to extract the peer key, and fallback
648 * for packets missing it.
649 * Even OoO DSS packets coming legitly after dropped or
650 * reordered MPC will cause fallback, but we don't have other
653 mptcp_get_options(sk, skb, &mp_opt);
654 if (!mp_opt.mp_capable) {
659 new_msk = mptcp_sk_clone(listener->conn, &mp_opt, req);
662 } else if (subflow_req->mp_join) {
663 mptcp_get_options(sk, skb, &mp_opt);
664 if (!mp_opt.mp_join || !subflow_hmac_valid(req, &mp_opt) ||
665 !mptcp_can_accept_new_subflow(subflow_req->msk)) {
666 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
672 child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
673 req_unhash, own_req);
675 if (child && *own_req) {
676 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
678 tcp_rsk(req)->drop_req = false;
680 /* we need to fallback on ctx allocation failure and on pre-reqs
681 * checking above. In the latter scenario we additionally need
682 * to reset the context to non MPTCP status.
684 if (!ctx || fallback) {
685 if (fallback_is_fatal) {
686 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
690 subflow_drop_ctx(child);
694 /* ssk inherits options of listener sk */
695 ctx->setsockopt_seq = listener->setsockopt_seq;
697 if (ctx->mp_capable) {
698 /* this can't race with mptcp_close(), as the msk is
699 * not yet exposted to user-space
701 inet_sk_state_store((void *)new_msk, TCP_ESTABLISHED);
703 /* record the newly created socket as the first msk
704 * subflow, but don't link it yet into conn_list
706 WRITE_ONCE(mptcp_sk(new_msk)->first, child);
708 /* new mpc subflow takes ownership of the newly
709 * created mptcp socket
711 new_msk->sk_destruct = mptcp_sock_destruct;
712 mptcp_sk(new_msk)->setsockopt_seq = ctx->setsockopt_seq;
713 mptcp_pm_new_connection(mptcp_sk(new_msk), child, 1);
714 mptcp_token_accept(subflow_req, mptcp_sk(new_msk));
718 /* with OoO packets we can reach here without ingress
721 if (mp_opt.mp_capable)
722 mptcp_subflow_fully_established(ctx, &mp_opt);
723 } else if (ctx->mp_join) {
724 struct mptcp_sock *owner;
726 owner = subflow_req->msk;
728 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
732 /* move the msk reference ownership to the subflow */
733 subflow_req->msk = NULL;
734 ctx->conn = (struct sock *)owner;
736 if (subflow_use_different_sport(owner, sk)) {
737 pr_debug("ack inet_sport=%d %d",
738 ntohs(inet_sk(sk)->inet_sport),
739 ntohs(inet_sk((struct sock *)owner)->inet_sport));
740 if (!mptcp_pm_sport_in_anno_list(owner, sk)) {
741 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX);
744 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX);
747 if (!mptcp_finish_join(child))
750 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
751 tcp_rsk(req)->drop_req = true;
756 /* dispose of the left over mptcp master, if any */
757 if (unlikely(new_msk))
758 mptcp_force_close(new_msk);
760 /* check for expected invariant - should never trigger, just help
761 * catching eariler subtle bugs
763 WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
764 (!mptcp_subflow_ctx(child) ||
765 !mptcp_subflow_ctx(child)->conn));
769 subflow_drop_ctx(child);
770 tcp_rsk(req)->drop_req = true;
771 inet_csk_prepare_for_destroy_sock(child);
773 req->rsk_ops->send_reset(sk, skb);
775 /* The last child reference will be released by the caller */
779 static struct inet_connection_sock_af_ops subflow_specific;
780 static struct proto tcp_prot_override;
782 enum mapping_status {
790 static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
792 pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d",
793 ssn, subflow->map_subflow_seq, subflow->map_data_len);
796 static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
798 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
799 unsigned int skb_consumed;
801 skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
802 if (WARN_ON_ONCE(skb_consumed >= skb->len))
805 return skb->len - skb_consumed <= subflow->map_data_len -
806 mptcp_subflow_get_map_offset(subflow);
809 static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
811 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
812 u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
814 if (unlikely(before(ssn, subflow->map_subflow_seq))) {
815 /* Mapping covers data later in the subflow stream,
816 * currently unsupported.
818 dbg_bad_map(subflow, ssn);
821 if (unlikely(!before(ssn, subflow->map_subflow_seq +
822 subflow->map_data_len))) {
823 /* Mapping does covers past subflow data, invalid */
824 dbg_bad_map(subflow, ssn);
830 static enum mapping_status get_mapping_status(struct sock *ssk,
831 struct mptcp_sock *msk)
833 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
834 struct mptcp_ext *mpext;
839 skb = skb_peek(&ssk->sk_receive_queue);
841 return MAPPING_EMPTY;
843 if (mptcp_check_fallback(ssk))
844 return MAPPING_DUMMY;
846 mpext = mptcp_get_ext(skb);
847 if (!mpext || !mpext->use_map) {
848 if (!subflow->map_valid && !skb->len) {
849 /* the TCP stack deliver 0 len FIN pkt to the receive
850 * queue, that is the only 0len pkts ever expected here,
851 * and we can admit no mapping only for 0 len pkts
853 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
854 WARN_ONCE(1, "0len seq %d:%d flags %x",
855 TCP_SKB_CB(skb)->seq,
856 TCP_SKB_CB(skb)->end_seq,
857 TCP_SKB_CB(skb)->tcp_flags);
858 sk_eat_skb(ssk, skb);
859 return MAPPING_EMPTY;
862 if (!subflow->map_valid)
863 return MAPPING_INVALID;
868 trace_get_mapping_status(mpext);
870 data_len = mpext->data_len;
872 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
873 return MAPPING_INVALID;
876 if (mpext->data_fin == 1) {
878 bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
880 pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
881 if (subflow->map_valid) {
882 /* A DATA_FIN might arrive in a DSS
883 * option before the previous mapping
884 * has been fully consumed. Continue
885 * handling the existing mapping.
887 skb_ext_del(skb, SKB_EXT_MPTCP);
890 if (updated && schedule_work(&msk->work))
891 sock_hold((struct sock *)msk);
893 return MAPPING_DATA_FIN;
896 u64 data_fin_seq = mpext->data_seq + data_len - 1;
898 /* If mpext->data_seq is a 32-bit value, data_fin_seq
899 * must also be limited to 32 bits.
902 data_fin_seq &= GENMASK_ULL(31, 0);
904 mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
905 pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d",
906 data_fin_seq, mpext->dsn64);
909 /* Adjust for DATA_FIN using 1 byte of sequence space */
913 map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64);
914 WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
916 if (subflow->map_valid) {
917 /* Allow replacing only with an identical map */
918 if (subflow->map_seq == map_seq &&
919 subflow->map_subflow_seq == mpext->subflow_seq &&
920 subflow->map_data_len == data_len) {
921 skb_ext_del(skb, SKB_EXT_MPTCP);
925 /* If this skb data are fully covered by the current mapping,
926 * the new map would need caching, which is not supported
928 if (skb_is_fully_mapped(ssk, skb)) {
929 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
930 return MAPPING_INVALID;
933 /* will validate the next map after consuming the current one */
937 subflow->map_seq = map_seq;
938 subflow->map_subflow_seq = mpext->subflow_seq;
939 subflow->map_data_len = data_len;
940 subflow->map_valid = 1;
941 subflow->mpc_map = mpext->mpc_map;
942 pr_debug("new map seq=%llu subflow_seq=%u data_len=%u",
943 subflow->map_seq, subflow->map_subflow_seq,
944 subflow->map_data_len);
947 /* we revalidate valid mapping on new skb, because we must ensure
948 * the current skb is completely covered by the available mapping
950 if (!validate_mapping(ssk, skb))
951 return MAPPING_INVALID;
953 skb_ext_del(skb, SKB_EXT_MPTCP);
957 static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
960 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
961 bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
964 incr = limit >= skb->len ? skb->len + fin : limit;
966 pr_debug("discarding=%d len=%d seq=%d", incr, skb->len,
967 subflow->map_subflow_seq);
968 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
969 tcp_sk(ssk)->copied_seq += incr;
970 if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
971 sk_eat_skb(ssk, skb);
972 if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
973 subflow->map_valid = 0;
976 /* sched mptcp worker to remove the subflow if no more data is pending */
977 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
979 struct sock *sk = (struct sock *)msk;
981 if (likely(ssk->sk_state != TCP_CLOSE))
984 if (skb_queue_empty(&ssk->sk_receive_queue) &&
985 !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) {
987 if (!schedule_work(&msk->work))
992 static bool subflow_check_data_avail(struct sock *ssk)
994 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
995 enum mapping_status status;
996 struct mptcp_sock *msk;
999 if (!skb_peek(&ssk->sk_receive_queue))
1000 WRITE_ONCE(subflow->data_avail, 0);
1001 if (subflow->data_avail)
1004 msk = mptcp_sk(subflow->conn);
1009 status = get_mapping_status(ssk, msk);
1010 trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue));
1011 if (unlikely(status == MAPPING_INVALID))
1014 if (unlikely(status == MAPPING_DUMMY))
1017 if (status != MAPPING_OK)
1020 skb = skb_peek(&ssk->sk_receive_queue);
1021 if (WARN_ON_ONCE(!skb))
1024 /* if msk lacks the remote key, this subflow must provide an
1025 * MP_CAPABLE-based mapping
1027 if (unlikely(!READ_ONCE(msk->can_ack))) {
1028 if (!subflow->mpc_map)
1030 WRITE_ONCE(msk->remote_key, subflow->remote_key);
1031 WRITE_ONCE(msk->ack_seq, subflow->map_seq);
1032 WRITE_ONCE(msk->can_ack, true);
1035 old_ack = READ_ONCE(msk->ack_seq);
1036 ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
1037 pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack,
1039 if (unlikely(before64(ack_seq, old_ack))) {
1040 mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
1044 WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1050 subflow_sched_work_if_closed(msk, ssk);
1054 /* RFC 8684 section 3.7. */
1055 if (subflow->mp_join || subflow->fully_established) {
1056 /* fatal protocol error, close the socket.
1057 * subflow_error_report() will introduce the appropriate barriers
1059 ssk->sk_err = EBADMSG;
1060 tcp_set_state(ssk, TCP_CLOSE);
1061 subflow->reset_transient = 0;
1062 subflow->reset_reason = MPTCP_RST_EMPTCP;
1063 tcp_send_active_reset(ssk, GFP_ATOMIC);
1064 WRITE_ONCE(subflow->data_avail, 0);
1068 __mptcp_do_fallback(msk);
1069 skb = skb_peek(&ssk->sk_receive_queue);
1070 subflow->map_valid = 1;
1071 subflow->map_seq = READ_ONCE(msk->ack_seq);
1072 subflow->map_data_len = skb->len;
1073 subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
1074 WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1078 bool mptcp_subflow_data_available(struct sock *sk)
1080 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1082 /* check if current mapping is still valid */
1083 if (subflow->map_valid &&
1084 mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
1085 subflow->map_valid = 0;
1086 WRITE_ONCE(subflow->data_avail, 0);
1088 pr_debug("Done with mapping: seq=%u data_len=%u",
1089 subflow->map_subflow_seq,
1090 subflow->map_data_len);
1093 return subflow_check_data_avail(sk);
1096 /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
1099 * In mptcp, rwin is about the mptcp-level connection data.
1101 * Data that is still on the ssk rx queue can thus be ignored,
1102 * as far as mptcp peer is concerned that data is still inflight.
1103 * DSS ACK is updated when skb is moved to the mptcp rx queue.
1105 void mptcp_space(const struct sock *ssk, int *space, int *full_space)
1107 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1108 const struct sock *sk = subflow->conn;
1110 *space = __mptcp_space(sk);
1111 *full_space = tcp_full_space(sk);
1114 void __mptcp_error_report(struct sock *sk)
1116 struct mptcp_subflow_context *subflow;
1117 struct mptcp_sock *msk = mptcp_sk(sk);
1119 mptcp_for_each_subflow(msk, subflow) {
1120 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1121 int err = sock_error(ssk);
1126 /* only propagate errors on fallen-back sockets or
1129 if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk))
1132 inet_sk_state_store(sk, inet_sk_state_load(ssk));
1135 /* This barrier is coupled with smp_rmb() in mptcp_poll() */
1137 sk->sk_error_report(sk);
1142 static void subflow_error_report(struct sock *ssk)
1144 struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1146 mptcp_data_lock(sk);
1147 if (!sock_owned_by_user(sk))
1148 __mptcp_error_report(sk);
1150 set_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags);
1151 mptcp_data_unlock(sk);
1154 static void subflow_data_ready(struct sock *sk)
1156 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1157 u16 state = 1 << inet_sk_state_load(sk);
1158 struct sock *parent = subflow->conn;
1159 struct mptcp_sock *msk;
1161 msk = mptcp_sk(parent);
1162 if (state & TCPF_LISTEN) {
1163 /* MPJ subflow are removed from accept queue before reaching here,
1164 * avoid stray wakeups
1166 if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
1169 set_bit(MPTCP_DATA_READY, &msk->flags);
1170 parent->sk_data_ready(parent);
1174 WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
1175 !subflow->mp_join && !(state & TCPF_CLOSE));
1177 if (mptcp_subflow_data_available(sk))
1178 mptcp_data_ready(parent, sk);
1179 else if (unlikely(sk->sk_err))
1180 subflow_error_report(sk);
1183 static void subflow_write_space(struct sock *ssk)
1185 struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1187 mptcp_propagate_sndbuf(sk, ssk);
1188 mptcp_write_space(sk);
1191 static struct inet_connection_sock_af_ops *
1192 subflow_default_af_ops(struct sock *sk)
1194 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1195 if (sk->sk_family == AF_INET6)
1196 return &subflow_v6_specific;
1198 return &subflow_specific;
1201 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1202 void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
1204 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1205 struct inet_connection_sock *icsk = inet_csk(sk);
1206 struct inet_connection_sock_af_ops *target;
1208 target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
1210 pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d",
1211 subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
1213 if (likely(icsk->icsk_af_ops == target))
1216 subflow->icsk_af_ops = icsk->icsk_af_ops;
1217 icsk->icsk_af_ops = target;
1221 void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
1222 struct sockaddr_storage *addr,
1223 unsigned short family)
1225 memset(addr, 0, sizeof(*addr));
1226 addr->ss_family = family;
1227 if (addr->ss_family == AF_INET) {
1228 struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
1230 if (info->family == AF_INET)
1231 in_addr->sin_addr = info->addr;
1232 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1233 else if (ipv6_addr_v4mapped(&info->addr6))
1234 in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3];
1236 in_addr->sin_port = info->port;
1238 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1239 else if (addr->ss_family == AF_INET6) {
1240 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;
1242 if (info->family == AF_INET)
1243 ipv6_addr_set_v4mapped(info->addr.s_addr,
1244 &in6_addr->sin6_addr);
1246 in6_addr->sin6_addr = info->addr6;
1247 in6_addr->sin6_port = info->port;
1252 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
1253 const struct mptcp_addr_info *remote,
1254 u8 flags, int ifindex)
1256 struct mptcp_sock *msk = mptcp_sk(sk);
1257 struct mptcp_subflow_context *subflow;
1258 struct sockaddr_storage addr;
1259 int remote_id = remote->id;
1260 int local_id = loc->id;
1267 if (!mptcp_is_fully_established(sk))
1270 err = mptcp_subflow_create_socket(sk, &sf);
1275 subflow = mptcp_subflow_ctx(ssk);
1277 get_random_bytes(&subflow->local_nonce, sizeof(u32));
1278 } while (!subflow->local_nonce);
1281 err = mptcp_pm_get_local_id(msk, (struct sock_common *)ssk);
1288 subflow->remote_key = msk->remote_key;
1289 subflow->local_key = msk->local_key;
1290 subflow->token = msk->token;
1291 mptcp_info2sockaddr(loc, &addr, ssk->sk_family);
1293 addrlen = sizeof(struct sockaddr_in);
1294 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1295 if (addr.ss_family == AF_INET6)
1296 addrlen = sizeof(struct sockaddr_in6);
1298 ssk->sk_bound_dev_if = ifindex;
1299 err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
1303 mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1304 pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk,
1305 remote_token, local_id, remote_id);
1306 subflow->remote_token = remote_token;
1307 subflow->local_id = local_id;
1308 subflow->remote_id = remote_id;
1309 subflow->request_join = 1;
1310 subflow->request_bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1311 mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
1313 mptcp_add_pending_subflow(msk, subflow);
1314 mptcp_sockopt_sync(msk, ssk);
1315 err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
1316 if (err && err != -EINPROGRESS)
1319 /* discard the subflow socket */
1320 mptcp_sock_graft(ssk, sk->sk_socket);
1321 iput(SOCK_INODE(sf));
1325 spin_lock_bh(&msk->join_list_lock);
1326 list_del(&subflow->node);
1327 spin_unlock_bh(&msk->join_list_lock);
1328 sock_put(mptcp_subflow_tcp_sock(subflow));
1331 subflow->disposable = 1;
1336 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
1338 #ifdef CONFIG_SOCK_CGROUP_DATA
1339 struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
1340 *child_skcd = &child->sk_cgrp_data;
1342 /* only the additional subflows created by kworkers have to be modified */
1343 if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
1344 cgroup_id(sock_cgroup_ptr(child_skcd))) {
1346 struct mem_cgroup *memcg = parent->sk_memcg;
1348 mem_cgroup_sk_free(child);
1349 if (memcg && css_tryget(&memcg->css))
1350 child->sk_memcg = memcg;
1351 #endif /* CONFIG_MEMCG */
1353 cgroup_sk_free(child_skcd);
1354 *child_skcd = *parent_skcd;
1355 cgroup_sk_clone(child_skcd);
1357 #endif /* CONFIG_SOCK_CGROUP_DATA */
1360 static void mptcp_subflow_ops_override(struct sock *ssk)
1362 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1363 if (ssk->sk_prot == &tcpv6_prot)
1364 ssk->sk_prot = &tcpv6_prot_override;
1367 ssk->sk_prot = &tcp_prot_override;
1370 static void mptcp_subflow_ops_undo_override(struct sock *ssk)
1372 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1373 if (ssk->sk_prot == &tcpv6_prot_override)
1374 ssk->sk_prot = &tcpv6_prot;
1377 ssk->sk_prot = &tcp_prot;
1379 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)
1381 struct mptcp_subflow_context *subflow;
1382 struct net *net = sock_net(sk);
1386 /* un-accepted server sockets can reach here - on bad configuration
1387 * bail early to avoid greater trouble later
1389 if (unlikely(!sk->sk_socket))
1392 err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP,
1399 /* the newly created socket has to be in the same cgroup as its parent */
1400 mptcp_attach_cgroup(sk, sf->sk);
1402 /* kernel sockets do not by default acquire net ref, but TCP timer
1405 sf->sk->sk_net_refcnt = 1;
1407 #ifdef CONFIG_PROC_FS
1408 this_cpu_add(*net->core.sock_inuse, 1);
1410 err = tcp_set_ulp(sf->sk, "mptcp");
1411 release_sock(sf->sk);
1418 /* the newly created socket really belongs to the owning MPTCP master
1419 * socket, even if for additional subflows the allocation is performed
1420 * by a kernel workqueue. Adjust inode references, so that the
1421 * procfs/diag interaces really show this one belonging to the correct
1424 SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
1425 SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
1426 SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
1428 subflow = mptcp_subflow_ctx(sf->sk);
1429 pr_debug("subflow=%p", subflow);
1434 mptcp_subflow_ops_override(sf->sk);
1439 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
1442 struct inet_connection_sock *icsk = inet_csk(sk);
1443 struct mptcp_subflow_context *ctx;
1445 ctx = kzalloc(sizeof(*ctx), priority);
1449 rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
1450 INIT_LIST_HEAD(&ctx->node);
1451 INIT_LIST_HEAD(&ctx->delegated_node);
1453 pr_debug("subflow=%p", ctx);
1460 static void __subflow_state_change(struct sock *sk)
1462 struct socket_wq *wq;
1465 wq = rcu_dereference(sk->sk_wq);
1466 if (skwq_has_sleeper(wq))
1467 wake_up_interruptible_all(&wq->wait);
1471 static bool subflow_is_done(const struct sock *sk)
1473 return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
1476 static void subflow_state_change(struct sock *sk)
1478 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1479 struct sock *parent = subflow->conn;
1481 __subflow_state_change(sk);
1483 if (subflow_simultaneous_connect(sk)) {
1484 mptcp_propagate_sndbuf(parent, sk);
1485 mptcp_do_fallback(sk);
1486 mptcp_rcv_space_init(mptcp_sk(parent), sk);
1487 pr_fallback(mptcp_sk(parent));
1488 subflow->conn_finished = 1;
1489 mptcp_set_connected(parent);
1492 /* as recvmsg() does not acquire the subflow socket for ssk selection
1493 * a fin packet carrying a DSS can be unnoticed if we don't trigger
1494 * the data available machinery here.
1496 if (mptcp_subflow_data_available(sk))
1497 mptcp_data_ready(parent, sk);
1498 else if (unlikely(sk->sk_err))
1499 subflow_error_report(sk);
1501 subflow_sched_work_if_closed(mptcp_sk(parent), sk);
1503 if (__mptcp_check_fallback(mptcp_sk(parent)) &&
1504 !subflow->rx_eof && subflow_is_done(sk)) {
1505 subflow->rx_eof = 1;
1506 mptcp_subflow_eof(parent);
1510 static int subflow_ulp_init(struct sock *sk)
1512 struct inet_connection_sock *icsk = inet_csk(sk);
1513 struct mptcp_subflow_context *ctx;
1514 struct tcp_sock *tp = tcp_sk(sk);
1517 /* disallow attaching ULP to a socket unless it has been
1518 * created with sock_create_kern()
1520 if (!sk->sk_kern_sock) {
1525 ctx = subflow_create_ctx(sk, GFP_KERNEL);
1531 pr_debug("subflow=%p, family=%d", ctx, sk->sk_family);
1534 ctx->icsk_af_ops = icsk->icsk_af_ops;
1535 icsk->icsk_af_ops = subflow_default_af_ops(sk);
1536 ctx->tcp_data_ready = sk->sk_data_ready;
1537 ctx->tcp_state_change = sk->sk_state_change;
1538 ctx->tcp_write_space = sk->sk_write_space;
1539 ctx->tcp_error_report = sk->sk_error_report;
1540 sk->sk_data_ready = subflow_data_ready;
1541 sk->sk_write_space = subflow_write_space;
1542 sk->sk_state_change = subflow_state_change;
1543 sk->sk_error_report = subflow_error_report;
1548 static void subflow_ulp_release(struct sock *ssk)
1550 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
1551 bool release = true;
1559 /* if the msk has been orphaned, keep the ctx
1560 * alive, will be freed by __mptcp_close_ssk(),
1561 * when the subflow is still unaccepted
1563 release = ctx->disposable || list_empty(&ctx->node);
1567 mptcp_subflow_ops_undo_override(ssk);
1569 kfree_rcu(ctx, rcu);
1572 static void subflow_ulp_clone(const struct request_sock *req,
1574 const gfp_t priority)
1576 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
1577 struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
1578 struct mptcp_subflow_context *new_ctx;
1580 if (!tcp_rsk(req)->is_mptcp ||
1581 (!subflow_req->mp_capable && !subflow_req->mp_join)) {
1582 subflow_ulp_fallback(newsk, old_ctx);
1586 new_ctx = subflow_create_ctx(newsk, priority);
1588 subflow_ulp_fallback(newsk, old_ctx);
1592 new_ctx->conn_finished = 1;
1593 new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
1594 new_ctx->tcp_data_ready = old_ctx->tcp_data_ready;
1595 new_ctx->tcp_state_change = old_ctx->tcp_state_change;
1596 new_ctx->tcp_write_space = old_ctx->tcp_write_space;
1597 new_ctx->tcp_error_report = old_ctx->tcp_error_report;
1598 new_ctx->rel_write_seq = 1;
1599 new_ctx->tcp_sock = newsk;
1601 if (subflow_req->mp_capable) {
1602 /* see comments in subflow_syn_recv_sock(), MPTCP connection
1603 * is fully established only after we receive the remote key
1605 new_ctx->mp_capable = 1;
1606 new_ctx->local_key = subflow_req->local_key;
1607 new_ctx->token = subflow_req->token;
1608 new_ctx->ssn_offset = subflow_req->ssn_offset;
1609 new_ctx->idsn = subflow_req->idsn;
1610 } else if (subflow_req->mp_join) {
1611 new_ctx->ssn_offset = subflow_req->ssn_offset;
1612 new_ctx->mp_join = 1;
1613 new_ctx->fully_established = 1;
1614 new_ctx->backup = subflow_req->backup;
1615 new_ctx->local_id = subflow_req->local_id;
1616 new_ctx->remote_id = subflow_req->remote_id;
1617 new_ctx->token = subflow_req->token;
1618 new_ctx->thmac = subflow_req->thmac;
1622 static void tcp_release_cb_override(struct sock *ssk)
1624 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1626 if (mptcp_subflow_has_delegated_action(subflow))
1627 mptcp_subflow_process_delegated(ssk);
1629 tcp_release_cb(ssk);
1632 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
1634 .owner = THIS_MODULE,
1635 .init = subflow_ulp_init,
1636 .release = subflow_ulp_release,
1637 .clone = subflow_ulp_clone,
1640 static int subflow_ops_init(struct request_sock_ops *subflow_ops)
1642 subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
1643 subflow_ops->slab_name = "request_sock_subflow";
1645 subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
1646 subflow_ops->obj_size, 0,
1648 SLAB_TYPESAFE_BY_RCU,
1650 if (!subflow_ops->slab)
1653 subflow_ops->destructor = subflow_req_destructor;
1658 void __init mptcp_subflow_init(void)
1660 mptcp_subflow_request_sock_ops = tcp_request_sock_ops;
1661 if (subflow_ops_init(&mptcp_subflow_request_sock_ops) != 0)
1662 panic("MPTCP: failed to init subflow request sock ops\n");
1664 subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
1665 subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
1667 subflow_specific = ipv4_specific;
1668 subflow_specific.conn_request = subflow_v4_conn_request;
1669 subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
1670 subflow_specific.sk_rx_dst_set = subflow_finish_connect;
1672 tcp_prot_override = tcp_prot;
1673 tcp_prot_override.release_cb = tcp_release_cb_override;
1675 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1676 subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
1677 subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
1679 subflow_v6_specific = ipv6_specific;
1680 subflow_v6_specific.conn_request = subflow_v6_conn_request;
1681 subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
1682 subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
1684 subflow_v6m_specific = subflow_v6_specific;
1685 subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
1686 subflow_v6m_specific.send_check = ipv4_specific.send_check;
1687 subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
1688 subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
1689 subflow_v6m_specific.net_frag_header_len = 0;
1691 tcpv6_prot_override = tcpv6_prot;
1692 tcpv6_prot_override.release_cb = tcp_release_cb_override;
1695 mptcp_diag_subflow_init(&subflow_ulp_ops);
1697 if (tcp_register_ulp(&subflow_ulp_ops) != 0)
1698 panic("MPTCP: failed to register subflows to ULP\n");