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(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 sock_put((struct sock *)subflow_req->msk);
216 mptcp_token_destroy_request(req);
217 tcp_request_sock_ops.destructor(req);
218 subflow_req->msk = NULL;
219 subflow_req->mp_join = 0;
220 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX);
223 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX);
226 subflow_req_create_thmac(subflow_req);
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);
233 pr_debug("token=%u, remote_nonce=%u msk=%p", subflow_req->token,
234 subflow_req->remote_nonce, subflow_req->msk);
240 int mptcp_subflow_init_cookie_req(struct request_sock *req,
241 const struct sock *sk_listener,
244 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
245 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
246 struct mptcp_options_received mp_opt;
249 subflow_init_req(req, sk_listener);
250 mptcp_get_options(skb, &mp_opt);
252 if (mp_opt.mp_capable && mp_opt.mp_join)
255 if (mp_opt.mp_capable && listener->request_mptcp) {
256 if (mp_opt.sndr_key == 0)
259 subflow_req->local_key = mp_opt.rcvr_key;
260 err = mptcp_token_new_request(req);
264 subflow_req->mp_capable = 1;
265 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
266 } else if (mp_opt.mp_join && listener->request_mptcp) {
267 if (!mptcp_token_join_cookie_init_state(subflow_req, skb))
270 if (mptcp_can_accept_new_subflow(subflow_req->msk))
271 subflow_req->mp_join = 1;
273 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
278 EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req);
280 static struct dst_entry *subflow_v4_route_req(const struct sock *sk,
283 struct request_sock *req)
285 struct dst_entry *dst;
288 tcp_rsk(req)->is_mptcp = 1;
289 subflow_init_req(req, sk);
291 dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req);
295 err = subflow_check_req(req, sk, skb);
301 tcp_request_sock_ops.send_reset(sk, skb);
305 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
306 static struct dst_entry *subflow_v6_route_req(const struct sock *sk,
309 struct request_sock *req)
311 struct dst_entry *dst;
314 tcp_rsk(req)->is_mptcp = 1;
315 subflow_init_req(req, sk);
317 dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req);
321 err = subflow_check_req(req, sk, skb);
327 tcp6_request_sock_ops.send_reset(sk, skb);
332 /* validate received truncated hmac and create hmac for third ACK */
333 static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
335 u8 hmac[SHA256_DIGEST_SIZE];
338 subflow_generate_hmac(subflow->remote_key, subflow->local_key,
339 subflow->remote_nonce, subflow->local_nonce,
342 thmac = get_unaligned_be64(hmac);
343 pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n",
344 subflow, subflow->token,
345 (unsigned long long)thmac,
346 (unsigned long long)subflow->thmac);
348 return thmac == subflow->thmac;
351 void mptcp_subflow_reset(struct sock *ssk)
353 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
354 struct sock *sk = subflow->conn;
356 /* must hold: tcp_done() could drop last reference on parent */
359 tcp_set_state(ssk, TCP_CLOSE);
360 tcp_send_active_reset(ssk, GFP_ATOMIC);
362 if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags) &&
363 schedule_work(&mptcp_sk(sk)->work))
364 return; /* worker will put sk for us */
369 static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk)
371 return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport;
374 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
376 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
377 struct mptcp_options_received mp_opt;
378 struct sock *parent = subflow->conn;
380 subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);
382 if (inet_sk_state_load(parent) == TCP_SYN_SENT) {
383 inet_sk_state_store(parent, TCP_ESTABLISHED);
384 parent->sk_state_change(parent);
387 /* be sure no special action on any packet other than syn-ack */
388 if (subflow->conn_finished)
391 mptcp_propagate_sndbuf(parent, sk);
392 subflow->rel_write_seq = 1;
393 subflow->conn_finished = 1;
394 subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
395 pr_debug("subflow=%p synack seq=%x", subflow, subflow->ssn_offset);
397 mptcp_get_options(skb, &mp_opt);
398 if (subflow->request_mptcp) {
399 if (!mp_opt.mp_capable) {
400 MPTCP_INC_STATS(sock_net(sk),
401 MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
402 mptcp_do_fallback(sk);
403 pr_fallback(mptcp_sk(subflow->conn));
407 subflow->mp_capable = 1;
408 subflow->can_ack = 1;
409 subflow->remote_key = mp_opt.sndr_key;
410 pr_debug("subflow=%p, remote_key=%llu", subflow,
411 subflow->remote_key);
412 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK);
413 mptcp_finish_connect(sk);
414 } else if (subflow->request_join) {
415 u8 hmac[SHA256_DIGEST_SIZE];
417 if (!mp_opt.mp_join) {
418 subflow->reset_reason = MPTCP_RST_EMPTCP;
422 subflow->thmac = mp_opt.thmac;
423 subflow->remote_nonce = mp_opt.nonce;
424 pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u", subflow,
425 subflow->thmac, subflow->remote_nonce);
427 if (!subflow_thmac_valid(subflow)) {
428 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC);
429 subflow->reset_reason = MPTCP_RST_EMPTCP;
433 subflow_generate_hmac(subflow->local_key, subflow->remote_key,
434 subflow->local_nonce,
435 subflow->remote_nonce,
437 memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
439 if (!mptcp_finish_join(sk))
442 subflow->mp_join = 1;
443 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
445 if (subflow_use_different_dport(mptcp_sk(parent), sk)) {
446 pr_debug("synack inet_dport=%d %d",
447 ntohs(inet_sk(sk)->inet_dport),
448 ntohs(inet_sk(parent)->inet_dport));
449 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX);
451 } else if (mptcp_check_fallback(sk)) {
453 mptcp_rcv_space_init(mptcp_sk(parent), sk);
458 subflow->reset_transient = 0;
459 mptcp_subflow_reset(sk);
462 struct request_sock_ops mptcp_subflow_request_sock_ops;
463 EXPORT_SYMBOL_GPL(mptcp_subflow_request_sock_ops);
464 static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops;
466 static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
468 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
470 pr_debug("subflow=%p", subflow);
472 /* Never answer to SYNs sent to broadcast or multicast */
473 if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
476 return tcp_conn_request(&mptcp_subflow_request_sock_ops,
477 &subflow_request_sock_ipv4_ops,
484 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
485 static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops;
486 static struct inet_connection_sock_af_ops subflow_v6_specific;
487 static struct inet_connection_sock_af_ops subflow_v6m_specific;
488 static struct proto tcpv6_prot_override;
490 static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
492 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
494 pr_debug("subflow=%p", subflow);
496 if (skb->protocol == htons(ETH_P_IP))
497 return subflow_v4_conn_request(sk, skb);
499 if (!ipv6_unicast_destination(skb))
502 if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) {
503 __IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS);
507 return tcp_conn_request(&mptcp_subflow_request_sock_ops,
508 &subflow_request_sock_ipv6_ops, sk, skb);
512 return 0; /* don't send reset */
516 /* validate hmac received in third ACK */
517 static bool subflow_hmac_valid(const struct request_sock *req,
518 const struct mptcp_options_received *mp_opt)
520 const struct mptcp_subflow_request_sock *subflow_req;
521 u8 hmac[SHA256_DIGEST_SIZE];
522 struct mptcp_sock *msk;
524 subflow_req = mptcp_subflow_rsk(req);
525 msk = subflow_req->msk;
529 subflow_generate_hmac(msk->remote_key, msk->local_key,
530 subflow_req->remote_nonce,
531 subflow_req->local_nonce, hmac);
533 return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN);
536 static void mptcp_sock_destruct(struct sock *sk)
538 /* if new mptcp socket isn't accepted, it is free'd
539 * from the tcp listener sockets request queue, linked
540 * from req->sk. The tcp socket is released.
541 * This calls the ULP release function which will
542 * also remove the mptcp socket, via
543 * sock_put(ctx->conn).
545 * Problem is that the mptcp socket will be in
546 * ESTABLISHED state and will not have the SOCK_DEAD flag.
547 * Both result in warnings from inet_sock_destruct.
549 if ((1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
550 sk->sk_state = TCP_CLOSE;
551 WARN_ON_ONCE(sk->sk_socket);
555 mptcp_destroy_common(mptcp_sk(sk));
556 inet_sock_destruct(sk);
559 static void mptcp_force_close(struct sock *sk)
561 inet_sk_state_store(sk, TCP_CLOSE);
562 sk_common_release(sk);
565 static void subflow_ulp_fallback(struct sock *sk,
566 struct mptcp_subflow_context *old_ctx)
568 struct inet_connection_sock *icsk = inet_csk(sk);
570 mptcp_subflow_tcp_fallback(sk, old_ctx);
571 icsk->icsk_ulp_ops = NULL;
572 rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
573 tcp_sk(sk)->is_mptcp = 0;
575 mptcp_subflow_ops_undo_override(sk);
578 static void subflow_drop_ctx(struct sock *ssk)
580 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
585 subflow_ulp_fallback(ssk, ctx);
592 void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
593 struct mptcp_options_received *mp_opt)
595 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
597 subflow->remote_key = mp_opt->sndr_key;
598 subflow->fully_established = 1;
599 subflow->can_ack = 1;
600 WRITE_ONCE(msk->fully_established, true);
603 static struct sock *subflow_syn_recv_sock(const struct sock *sk,
605 struct request_sock *req,
606 struct dst_entry *dst,
607 struct request_sock *req_unhash,
610 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk);
611 struct mptcp_subflow_request_sock *subflow_req;
612 struct mptcp_options_received mp_opt;
613 bool fallback, fallback_is_fatal;
614 struct sock *new_msk = NULL;
617 pr_debug("listener=%p, req=%p, conn=%p", listener, req, listener->conn);
619 /* After child creation we must look for 'mp_capable' even when options
622 mp_opt.mp_capable = 0;
624 /* hopefully temporary handling for MP_JOIN+syncookie */
625 subflow_req = mptcp_subflow_rsk(req);
626 fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join;
627 fallback = !tcp_rsk(req)->is_mptcp;
631 /* if the sk is MP_CAPABLE, we try to fetch the client key */
632 if (subflow_req->mp_capable) {
633 if (TCP_SKB_CB(skb)->seq != subflow_req->ssn_offset + 1) {
634 /* here we can receive and accept an in-window,
635 * out-of-order pkt, which will not carry the MP_CAPABLE
636 * opt even on mptcp enabled paths
641 mptcp_get_options(skb, &mp_opt);
642 if (!mp_opt.mp_capable) {
648 new_msk = mptcp_sk_clone(listener->conn, &mp_opt, req);
651 } else if (subflow_req->mp_join) {
652 mptcp_get_options(skb, &mp_opt);
653 if (!mp_opt.mp_join || !subflow_hmac_valid(req, &mp_opt) ||
654 !mptcp_can_accept_new_subflow(subflow_req->msk)) {
655 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
661 child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
662 req_unhash, own_req);
664 if (child && *own_req) {
665 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
667 tcp_rsk(req)->drop_req = false;
669 /* we need to fallback on ctx allocation failure and on pre-reqs
670 * checking above. In the latter scenario we additionally need
671 * to reset the context to non MPTCP status.
673 if (!ctx || fallback) {
674 if (fallback_is_fatal) {
675 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
679 subflow_drop_ctx(child);
683 /* ssk inherits options of listener sk */
684 ctx->setsockopt_seq = listener->setsockopt_seq;
686 if (ctx->mp_capable) {
687 /* this can't race with mptcp_close(), as the msk is
688 * not yet exposted to user-space
690 inet_sk_state_store((void *)new_msk, TCP_ESTABLISHED);
692 /* record the newly created socket as the first msk
693 * subflow, but don't link it yet into conn_list
695 WRITE_ONCE(mptcp_sk(new_msk)->first, child);
697 /* new mpc subflow takes ownership of the newly
698 * created mptcp socket
700 new_msk->sk_destruct = mptcp_sock_destruct;
701 mptcp_sk(new_msk)->setsockopt_seq = ctx->setsockopt_seq;
702 mptcp_pm_new_connection(mptcp_sk(new_msk), child, 1);
703 mptcp_token_accept(subflow_req, mptcp_sk(new_msk));
707 /* with OoO packets we can reach here without ingress
710 if (mp_opt.mp_capable)
711 mptcp_subflow_fully_established(ctx, &mp_opt);
712 } else if (ctx->mp_join) {
713 struct mptcp_sock *owner;
715 owner = subflow_req->msk;
717 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
721 /* move the msk reference ownership to the subflow */
722 subflow_req->msk = NULL;
723 ctx->conn = (struct sock *)owner;
725 if (subflow_use_different_sport(owner, sk)) {
726 pr_debug("ack inet_sport=%d %d",
727 ntohs(inet_sk(sk)->inet_sport),
728 ntohs(inet_sk((struct sock *)owner)->inet_sport));
729 if (!mptcp_pm_sport_in_anno_list(owner, sk)) {
730 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX);
733 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX);
736 if (!mptcp_finish_join(child))
739 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
740 tcp_rsk(req)->drop_req = true;
745 /* dispose of the left over mptcp master, if any */
746 if (unlikely(new_msk))
747 mptcp_force_close(new_msk);
749 /* check for expected invariant - should never trigger, just help
750 * catching eariler subtle bugs
752 WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
753 (!mptcp_subflow_ctx(child) ||
754 !mptcp_subflow_ctx(child)->conn));
758 subflow_drop_ctx(child);
759 tcp_rsk(req)->drop_req = true;
760 inet_csk_prepare_for_destroy_sock(child);
762 req->rsk_ops->send_reset(sk, skb);
764 /* The last child reference will be released by the caller */
768 static struct inet_connection_sock_af_ops subflow_specific;
769 static struct proto tcp_prot_override;
771 enum mapping_status {
779 static u64 expand_seq(u64 old_seq, u16 old_data_len, u64 seq)
781 if ((u32)seq == (u32)old_seq)
784 /* Assume map covers data not mapped yet. */
785 return seq | ((old_seq + old_data_len + 1) & GENMASK_ULL(63, 32));
788 static void warn_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
790 WARN_ONCE(1, "Bad mapping: ssn=%d map_seq=%d map_data_len=%d",
791 ssn, subflow->map_subflow_seq, subflow->map_data_len);
794 static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
796 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
797 unsigned int skb_consumed;
799 skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
800 if (WARN_ON_ONCE(skb_consumed >= skb->len))
803 return skb->len - skb_consumed <= subflow->map_data_len -
804 mptcp_subflow_get_map_offset(subflow);
807 static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
809 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
810 u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
812 if (unlikely(before(ssn, subflow->map_subflow_seq))) {
813 /* Mapping covers data later in the subflow stream,
814 * currently unsupported.
816 warn_bad_map(subflow, ssn);
819 if (unlikely(!before(ssn, subflow->map_subflow_seq +
820 subflow->map_data_len))) {
821 /* Mapping does covers past subflow data, invalid */
822 warn_bad_map(subflow, ssn + skb->len);
828 static enum mapping_status get_mapping_status(struct sock *ssk,
829 struct mptcp_sock *msk)
831 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
832 struct mptcp_ext *mpext;
837 skb = skb_peek(&ssk->sk_receive_queue);
839 return MAPPING_EMPTY;
841 if (mptcp_check_fallback(ssk))
842 return MAPPING_DUMMY;
844 mpext = mptcp_get_ext(skb);
845 if (!mpext || !mpext->use_map) {
846 if (!subflow->map_valid && !skb->len) {
847 /* the TCP stack deliver 0 len FIN pkt to the receive
848 * queue, that is the only 0len pkts ever expected here,
849 * and we can admit no mapping only for 0 len pkts
851 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
852 WARN_ONCE(1, "0len seq %d:%d flags %x",
853 TCP_SKB_CB(skb)->seq,
854 TCP_SKB_CB(skb)->end_seq,
855 TCP_SKB_CB(skb)->tcp_flags);
856 sk_eat_skb(ssk, skb);
857 return MAPPING_EMPTY;
860 if (!subflow->map_valid)
861 return MAPPING_INVALID;
866 trace_get_mapping_status(mpext);
868 data_len = mpext->data_len;
870 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
871 return MAPPING_INVALID;
874 if (mpext->data_fin == 1) {
876 bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
878 pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
879 if (subflow->map_valid) {
880 /* A DATA_FIN might arrive in a DSS
881 * option before the previous mapping
882 * has been fully consumed. Continue
883 * handling the existing mapping.
885 skb_ext_del(skb, SKB_EXT_MPTCP);
888 if (updated && schedule_work(&msk->work))
889 sock_hold((struct sock *)msk);
891 return MAPPING_DATA_FIN;
894 u64 data_fin_seq = mpext->data_seq + data_len - 1;
896 /* If mpext->data_seq is a 32-bit value, data_fin_seq
897 * must also be limited to 32 bits.
900 data_fin_seq &= GENMASK_ULL(31, 0);
902 mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
903 pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d",
904 data_fin_seq, mpext->dsn64);
907 /* Adjust for DATA_FIN using 1 byte of sequence space */
912 map_seq = expand_seq(subflow->map_seq, subflow->map_data_len,
914 pr_debug("expanded seq=%llu", subflow->map_seq);
916 map_seq = mpext->data_seq;
918 WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
920 if (subflow->map_valid) {
921 /* Allow replacing only with an identical map */
922 if (subflow->map_seq == map_seq &&
923 subflow->map_subflow_seq == mpext->subflow_seq &&
924 subflow->map_data_len == data_len) {
925 skb_ext_del(skb, SKB_EXT_MPTCP);
929 /* If this skb data are fully covered by the current mapping,
930 * the new map would need caching, which is not supported
932 if (skb_is_fully_mapped(ssk, skb)) {
933 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
934 return MAPPING_INVALID;
937 /* will validate the next map after consuming the current one */
941 subflow->map_seq = map_seq;
942 subflow->map_subflow_seq = mpext->subflow_seq;
943 subflow->map_data_len = data_len;
944 subflow->map_valid = 1;
945 subflow->mpc_map = mpext->mpc_map;
946 pr_debug("new map seq=%llu subflow_seq=%u data_len=%u",
947 subflow->map_seq, subflow->map_subflow_seq,
948 subflow->map_data_len);
951 /* we revalidate valid mapping on new skb, because we must ensure
952 * the current skb is completely covered by the available mapping
954 if (!validate_mapping(ssk, skb))
955 return MAPPING_INVALID;
957 skb_ext_del(skb, SKB_EXT_MPTCP);
961 static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
964 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
965 bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
968 incr = limit >= skb->len ? skb->len + fin : limit;
970 pr_debug("discarding=%d len=%d seq=%d", incr, skb->len,
971 subflow->map_subflow_seq);
972 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
973 tcp_sk(ssk)->copied_seq += incr;
974 if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
975 sk_eat_skb(ssk, skb);
976 if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
977 subflow->map_valid = 0;
980 /* sched mptcp worker to remove the subflow if no more data is pending */
981 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
983 struct sock *sk = (struct sock *)msk;
985 if (likely(ssk->sk_state != TCP_CLOSE))
988 if (skb_queue_empty(&ssk->sk_receive_queue) &&
989 !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) {
991 if (!schedule_work(&msk->work))
996 static bool subflow_check_data_avail(struct sock *ssk)
998 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
999 enum mapping_status status;
1000 struct mptcp_sock *msk;
1001 struct sk_buff *skb;
1003 if (!skb_peek(&ssk->sk_receive_queue))
1004 subflow->data_avail = 0;
1005 if (subflow->data_avail)
1008 msk = mptcp_sk(subflow->conn);
1013 status = get_mapping_status(ssk, msk);
1014 trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue));
1015 if (status == MAPPING_INVALID) {
1016 ssk->sk_err = EBADMSG;
1019 if (status == MAPPING_DUMMY) {
1020 __mptcp_do_fallback(msk);
1021 skb = skb_peek(&ssk->sk_receive_queue);
1022 subflow->map_valid = 1;
1023 subflow->map_seq = READ_ONCE(msk->ack_seq);
1024 subflow->map_data_len = skb->len;
1025 subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq -
1026 subflow->ssn_offset;
1027 subflow->data_avail = MPTCP_SUBFLOW_DATA_AVAIL;
1031 if (status != MAPPING_OK)
1034 skb = skb_peek(&ssk->sk_receive_queue);
1035 if (WARN_ON_ONCE(!skb))
1038 /* if msk lacks the remote key, this subflow must provide an
1039 * MP_CAPABLE-based mapping
1041 if (unlikely(!READ_ONCE(msk->can_ack))) {
1042 if (!subflow->mpc_map) {
1043 ssk->sk_err = EBADMSG;
1046 WRITE_ONCE(msk->remote_key, subflow->remote_key);
1047 WRITE_ONCE(msk->ack_seq, subflow->map_seq);
1048 WRITE_ONCE(msk->can_ack, true);
1051 old_ack = READ_ONCE(msk->ack_seq);
1052 ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
1053 pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack,
1055 if (ack_seq == old_ack) {
1056 subflow->data_avail = MPTCP_SUBFLOW_DATA_AVAIL;
1058 } else if (after64(ack_seq, old_ack)) {
1059 subflow->data_avail = MPTCP_SUBFLOW_OOO_DATA;
1063 /* only accept in-sequence mapping. Old values are spurious
1066 mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
1071 subflow_sched_work_if_closed(msk, ssk);
1074 /* fatal protocol error, close the socket */
1075 /* This barrier is coupled with smp_rmb() in tcp_poll() */
1077 ssk->sk_error_report(ssk);
1078 tcp_set_state(ssk, TCP_CLOSE);
1079 subflow->reset_transient = 0;
1080 subflow->reset_reason = MPTCP_RST_EMPTCP;
1081 tcp_send_active_reset(ssk, GFP_ATOMIC);
1082 subflow->data_avail = 0;
1086 bool mptcp_subflow_data_available(struct sock *sk)
1088 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1090 /* check if current mapping is still valid */
1091 if (subflow->map_valid &&
1092 mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
1093 subflow->map_valid = 0;
1094 subflow->data_avail = 0;
1096 pr_debug("Done with mapping: seq=%u data_len=%u",
1097 subflow->map_subflow_seq,
1098 subflow->map_data_len);
1101 return subflow_check_data_avail(sk);
1104 /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
1107 * In mptcp, rwin is about the mptcp-level connection data.
1109 * Data that is still on the ssk rx queue can thus be ignored,
1110 * as far as mptcp peer is concerned that data is still inflight.
1111 * DSS ACK is updated when skb is moved to the mptcp rx queue.
1113 void mptcp_space(const struct sock *ssk, int *space, int *full_space)
1115 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1116 const struct sock *sk = subflow->conn;
1118 *space = __mptcp_space(sk);
1119 *full_space = tcp_full_space(sk);
1122 static void subflow_data_ready(struct sock *sk)
1124 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1125 u16 state = 1 << inet_sk_state_load(sk);
1126 struct sock *parent = subflow->conn;
1127 struct mptcp_sock *msk;
1129 msk = mptcp_sk(parent);
1130 if (state & TCPF_LISTEN) {
1131 /* MPJ subflow are removed from accept queue before reaching here,
1132 * avoid stray wakeups
1134 if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
1137 set_bit(MPTCP_DATA_READY, &msk->flags);
1138 parent->sk_data_ready(parent);
1142 WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
1143 !subflow->mp_join && !(state & TCPF_CLOSE));
1145 if (mptcp_subflow_data_available(sk))
1146 mptcp_data_ready(parent, sk);
1149 static void subflow_write_space(struct sock *ssk)
1151 struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1153 mptcp_propagate_sndbuf(sk, ssk);
1154 mptcp_write_space(sk);
1157 void __mptcp_error_report(struct sock *sk)
1159 struct mptcp_subflow_context *subflow;
1160 struct mptcp_sock *msk = mptcp_sk(sk);
1162 mptcp_for_each_subflow(msk, subflow) {
1163 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1164 int err = sock_error(ssk);
1169 /* only propagate errors on fallen-back sockets or
1172 if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk))
1175 inet_sk_state_store(sk, inet_sk_state_load(ssk));
1178 /* This barrier is coupled with smp_rmb() in mptcp_poll() */
1180 sk->sk_error_report(sk);
1185 static void subflow_error_report(struct sock *ssk)
1187 struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1189 mptcp_data_lock(sk);
1190 if (!sock_owned_by_user(sk))
1191 __mptcp_error_report(sk);
1193 set_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags);
1194 mptcp_data_unlock(sk);
1197 static struct inet_connection_sock_af_ops *
1198 subflow_default_af_ops(struct sock *sk)
1200 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1201 if (sk->sk_family == AF_INET6)
1202 return &subflow_v6_specific;
1204 return &subflow_specific;
1207 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1208 void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
1210 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1211 struct inet_connection_sock *icsk = inet_csk(sk);
1212 struct inet_connection_sock_af_ops *target;
1214 target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
1216 pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d",
1217 subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
1219 if (likely(icsk->icsk_af_ops == target))
1222 subflow->icsk_af_ops = icsk->icsk_af_ops;
1223 icsk->icsk_af_ops = target;
1227 void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
1228 struct sockaddr_storage *addr,
1229 unsigned short family)
1231 memset(addr, 0, sizeof(*addr));
1232 addr->ss_family = family;
1233 if (addr->ss_family == AF_INET) {
1234 struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
1236 if (info->family == AF_INET)
1237 in_addr->sin_addr = info->addr;
1238 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1239 else if (ipv6_addr_v4mapped(&info->addr6))
1240 in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3];
1242 in_addr->sin_port = info->port;
1244 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1245 else if (addr->ss_family == AF_INET6) {
1246 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;
1248 if (info->family == AF_INET)
1249 ipv6_addr_set_v4mapped(info->addr.s_addr,
1250 &in6_addr->sin6_addr);
1252 in6_addr->sin6_addr = info->addr6;
1253 in6_addr->sin6_port = info->port;
1258 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
1259 const struct mptcp_addr_info *remote,
1260 u8 flags, int ifindex)
1262 struct mptcp_sock *msk = mptcp_sk(sk);
1263 struct mptcp_subflow_context *subflow;
1264 struct sockaddr_storage addr;
1265 int remote_id = remote->id;
1266 int local_id = loc->id;
1273 if (!mptcp_is_fully_established(sk))
1276 err = mptcp_subflow_create_socket(sk, &sf);
1281 subflow = mptcp_subflow_ctx(ssk);
1283 get_random_bytes(&subflow->local_nonce, sizeof(u32));
1284 } while (!subflow->local_nonce);
1287 err = mptcp_pm_get_local_id(msk, (struct sock_common *)ssk);
1294 subflow->remote_key = msk->remote_key;
1295 subflow->local_key = msk->local_key;
1296 subflow->token = msk->token;
1297 mptcp_info2sockaddr(loc, &addr, ssk->sk_family);
1299 addrlen = sizeof(struct sockaddr_in);
1300 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1301 if (addr.ss_family == AF_INET6)
1302 addrlen = sizeof(struct sockaddr_in6);
1304 ssk->sk_bound_dev_if = ifindex;
1305 err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
1309 mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1310 pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk,
1311 remote_token, local_id, remote_id);
1312 subflow->remote_token = remote_token;
1313 subflow->local_id = local_id;
1314 subflow->remote_id = remote_id;
1315 subflow->request_join = 1;
1316 subflow->request_bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1317 mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
1319 mptcp_add_pending_subflow(msk, subflow);
1320 mptcp_sockopt_sync(msk, ssk);
1321 err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
1322 if (err && err != -EINPROGRESS)
1325 /* discard the subflow socket */
1326 mptcp_sock_graft(ssk, sk->sk_socket);
1327 iput(SOCK_INODE(sf));
1331 spin_lock_bh(&msk->join_list_lock);
1332 list_del(&subflow->node);
1333 spin_unlock_bh(&msk->join_list_lock);
1334 sock_put(mptcp_subflow_tcp_sock(subflow));
1337 subflow->disposable = 1;
1342 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
1344 #ifdef CONFIG_SOCK_CGROUP_DATA
1345 struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
1346 *child_skcd = &child->sk_cgrp_data;
1348 /* only the additional subflows created by kworkers have to be modified */
1349 if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
1350 cgroup_id(sock_cgroup_ptr(child_skcd))) {
1352 struct mem_cgroup *memcg = parent->sk_memcg;
1354 mem_cgroup_sk_free(child);
1355 if (memcg && css_tryget(&memcg->css))
1356 child->sk_memcg = memcg;
1357 #endif /* CONFIG_MEMCG */
1359 cgroup_sk_free(child_skcd);
1360 *child_skcd = *parent_skcd;
1361 cgroup_sk_clone(child_skcd);
1363 #endif /* CONFIG_SOCK_CGROUP_DATA */
1366 static void mptcp_subflow_ops_override(struct sock *ssk)
1368 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1369 if (ssk->sk_prot == &tcpv6_prot)
1370 ssk->sk_prot = &tcpv6_prot_override;
1373 ssk->sk_prot = &tcp_prot_override;
1376 static void mptcp_subflow_ops_undo_override(struct sock *ssk)
1378 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1379 if (ssk->sk_prot == &tcpv6_prot_override)
1380 ssk->sk_prot = &tcpv6_prot;
1383 ssk->sk_prot = &tcp_prot;
1385 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)
1387 struct mptcp_subflow_context *subflow;
1388 struct net *net = sock_net(sk);
1392 /* un-accepted server sockets can reach here - on bad configuration
1393 * bail early to avoid greater trouble later
1395 if (unlikely(!sk->sk_socket))
1398 err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP,
1405 /* the newly created socket has to be in the same cgroup as its parent */
1406 mptcp_attach_cgroup(sk, sf->sk);
1408 /* kernel sockets do not by default acquire net ref, but TCP timer
1411 sf->sk->sk_net_refcnt = 1;
1413 #ifdef CONFIG_PROC_FS
1414 this_cpu_add(*net->core.sock_inuse, 1);
1416 err = tcp_set_ulp(sf->sk, "mptcp");
1417 release_sock(sf->sk);
1424 /* the newly created socket really belongs to the owning MPTCP master
1425 * socket, even if for additional subflows the allocation is performed
1426 * by a kernel workqueue. Adjust inode references, so that the
1427 * procfs/diag interaces really show this one belonging to the correct
1430 SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
1431 SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
1432 SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
1434 subflow = mptcp_subflow_ctx(sf->sk);
1435 pr_debug("subflow=%p", subflow);
1440 mptcp_subflow_ops_override(sf->sk);
1445 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
1448 struct inet_connection_sock *icsk = inet_csk(sk);
1449 struct mptcp_subflow_context *ctx;
1451 ctx = kzalloc(sizeof(*ctx), priority);
1455 rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
1456 INIT_LIST_HEAD(&ctx->node);
1457 INIT_LIST_HEAD(&ctx->delegated_node);
1459 pr_debug("subflow=%p", ctx);
1466 static void __subflow_state_change(struct sock *sk)
1468 struct socket_wq *wq;
1471 wq = rcu_dereference(sk->sk_wq);
1472 if (skwq_has_sleeper(wq))
1473 wake_up_interruptible_all(&wq->wait);
1477 static bool subflow_is_done(const struct sock *sk)
1479 return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
1482 static void subflow_state_change(struct sock *sk)
1484 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1485 struct sock *parent = subflow->conn;
1487 __subflow_state_change(sk);
1489 if (subflow_simultaneous_connect(sk)) {
1490 mptcp_propagate_sndbuf(parent, sk);
1491 mptcp_do_fallback(sk);
1492 mptcp_rcv_space_init(mptcp_sk(parent), sk);
1493 pr_fallback(mptcp_sk(parent));
1494 subflow->conn_finished = 1;
1495 if (inet_sk_state_load(parent) == TCP_SYN_SENT) {
1496 inet_sk_state_store(parent, TCP_ESTABLISHED);
1497 parent->sk_state_change(parent);
1501 /* as recvmsg() does not acquire the subflow socket for ssk selection
1502 * a fin packet carrying a DSS can be unnoticed if we don't trigger
1503 * the data available machinery here.
1505 if (mptcp_subflow_data_available(sk))
1506 mptcp_data_ready(parent, sk);
1508 subflow_sched_work_if_closed(mptcp_sk(parent), sk);
1510 if (__mptcp_check_fallback(mptcp_sk(parent)) &&
1511 !subflow->rx_eof && subflow_is_done(sk)) {
1512 subflow->rx_eof = 1;
1513 mptcp_subflow_eof(parent);
1517 static int subflow_ulp_init(struct sock *sk)
1519 struct inet_connection_sock *icsk = inet_csk(sk);
1520 struct mptcp_subflow_context *ctx;
1521 struct tcp_sock *tp = tcp_sk(sk);
1524 /* disallow attaching ULP to a socket unless it has been
1525 * created with sock_create_kern()
1527 if (!sk->sk_kern_sock) {
1532 ctx = subflow_create_ctx(sk, GFP_KERNEL);
1538 pr_debug("subflow=%p, family=%d", ctx, sk->sk_family);
1541 ctx->icsk_af_ops = icsk->icsk_af_ops;
1542 icsk->icsk_af_ops = subflow_default_af_ops(sk);
1543 ctx->tcp_data_ready = sk->sk_data_ready;
1544 ctx->tcp_state_change = sk->sk_state_change;
1545 ctx->tcp_write_space = sk->sk_write_space;
1546 ctx->tcp_error_report = sk->sk_error_report;
1547 sk->sk_data_ready = subflow_data_ready;
1548 sk->sk_write_space = subflow_write_space;
1549 sk->sk_state_change = subflow_state_change;
1550 sk->sk_error_report = subflow_error_report;
1555 static void subflow_ulp_release(struct sock *ssk)
1557 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
1558 bool release = true;
1566 /* if the msk has been orphaned, keep the ctx
1567 * alive, will be freed by __mptcp_close_ssk(),
1568 * when the subflow is still unaccepted
1570 release = ctx->disposable || list_empty(&ctx->node);
1574 mptcp_subflow_ops_undo_override(ssk);
1576 kfree_rcu(ctx, rcu);
1579 static void subflow_ulp_clone(const struct request_sock *req,
1581 const gfp_t priority)
1583 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
1584 struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
1585 struct mptcp_subflow_context *new_ctx;
1587 if (!tcp_rsk(req)->is_mptcp ||
1588 (!subflow_req->mp_capable && !subflow_req->mp_join)) {
1589 subflow_ulp_fallback(newsk, old_ctx);
1593 new_ctx = subflow_create_ctx(newsk, priority);
1595 subflow_ulp_fallback(newsk, old_ctx);
1599 new_ctx->conn_finished = 1;
1600 new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
1601 new_ctx->tcp_data_ready = old_ctx->tcp_data_ready;
1602 new_ctx->tcp_state_change = old_ctx->tcp_state_change;
1603 new_ctx->tcp_write_space = old_ctx->tcp_write_space;
1604 new_ctx->tcp_error_report = old_ctx->tcp_error_report;
1605 new_ctx->rel_write_seq = 1;
1606 new_ctx->tcp_sock = newsk;
1608 if (subflow_req->mp_capable) {
1609 /* see comments in subflow_syn_recv_sock(), MPTCP connection
1610 * is fully established only after we receive the remote key
1612 new_ctx->mp_capable = 1;
1613 new_ctx->local_key = subflow_req->local_key;
1614 new_ctx->token = subflow_req->token;
1615 new_ctx->ssn_offset = subflow_req->ssn_offset;
1616 new_ctx->idsn = subflow_req->idsn;
1617 } else if (subflow_req->mp_join) {
1618 new_ctx->ssn_offset = subflow_req->ssn_offset;
1619 new_ctx->mp_join = 1;
1620 new_ctx->fully_established = 1;
1621 new_ctx->backup = subflow_req->backup;
1622 new_ctx->local_id = subflow_req->local_id;
1623 new_ctx->remote_id = subflow_req->remote_id;
1624 new_ctx->token = subflow_req->token;
1625 new_ctx->thmac = subflow_req->thmac;
1629 static void tcp_release_cb_override(struct sock *ssk)
1631 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1633 if (mptcp_subflow_has_delegated_action(subflow))
1634 mptcp_subflow_process_delegated(ssk);
1636 tcp_release_cb(ssk);
1639 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
1641 .owner = THIS_MODULE,
1642 .init = subflow_ulp_init,
1643 .release = subflow_ulp_release,
1644 .clone = subflow_ulp_clone,
1647 static int subflow_ops_init(struct request_sock_ops *subflow_ops)
1649 subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
1650 subflow_ops->slab_name = "request_sock_subflow";
1652 subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
1653 subflow_ops->obj_size, 0,
1655 SLAB_TYPESAFE_BY_RCU,
1657 if (!subflow_ops->slab)
1660 subflow_ops->destructor = subflow_req_destructor;
1665 void __init mptcp_subflow_init(void)
1667 mptcp_subflow_request_sock_ops = tcp_request_sock_ops;
1668 if (subflow_ops_init(&mptcp_subflow_request_sock_ops) != 0)
1669 panic("MPTCP: failed to init subflow request sock ops\n");
1671 subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
1672 subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
1674 subflow_specific = ipv4_specific;
1675 subflow_specific.conn_request = subflow_v4_conn_request;
1676 subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
1677 subflow_specific.sk_rx_dst_set = subflow_finish_connect;
1679 tcp_prot_override = tcp_prot;
1680 tcp_prot_override.release_cb = tcp_release_cb_override;
1682 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1683 subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
1684 subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
1686 subflow_v6_specific = ipv6_specific;
1687 subflow_v6_specific.conn_request = subflow_v6_conn_request;
1688 subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
1689 subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
1691 subflow_v6m_specific = subflow_v6_specific;
1692 subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
1693 subflow_v6m_specific.send_check = ipv4_specific.send_check;
1694 subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
1695 subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
1696 subflow_v6m_specific.net_frag_header_len = 0;
1698 tcpv6_prot_override = tcpv6_prot;
1699 tcpv6_prot_override.release_cb = tcp_release_cb_override;
1702 mptcp_diag_subflow_init(&subflow_ulp_ops);
1704 if (tcp_register_ulp(&subflow_ulp_ops) != 0)
1705 panic("MPTCP: failed to register subflows to ULP\n");