Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /* Multipath TCP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright (c) 2017 - 2019, Intel Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #define pr_fmt(fmt) "MPTCP: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <net/inet_common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <net/inet_hashtables.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <net/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <net/tcp_states.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #if IS_ENABLED(CONFIG_MPTCP_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <net/transp_v6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <net/mptcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include "protocol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include "mib.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #if IS_ENABLED(CONFIG_MPTCP_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) struct mptcp6_sock {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 	struct mptcp_sock msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 	struct ipv6_pinfo np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) struct mptcp_skb_cb {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	u64 map_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	u64 end_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	u32 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #define MPTCP_SKB_CB(__skb)	((struct mptcp_skb_cb *)&((__skb)->cb[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) static struct percpu_counter mptcp_sockets_allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * completed yet or has failed, return the subflow socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  * Otherwise return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	if (!msk->subflow || READ_ONCE(msk->can_ack))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	return msk->subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) static bool mptcp_is_tcpsk(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	struct socket *sock = sk->sk_socket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	if (unlikely(sk->sk_prot == &tcp_prot)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 		/* we are being invoked after mptcp_accept() has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 		 * accepted a non-mp-capable flow: sk is a tcp_sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 		 * not an mptcp one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 		 * Hand the socket over to tcp so all further socket ops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 		 * bypass mptcp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 		sock->ops = &inet_stream_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) #if IS_ENABLED(CONFIG_MPTCP_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	} else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 		sock->ops = &inet6_stream_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) static struct sock *__mptcp_tcp_fallback(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	sock_owned_by_me((const struct sock *)msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	if (likely(!__mptcp_check_fallback(msk)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	return msk->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) static int __mptcp_socket_create(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	struct socket *ssock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	err = mptcp_subflow_create_socket(sk, &ssock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	msk->first = ssock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	msk->subflow = ssock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	subflow = mptcp_subflow_ctx(ssock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	list_add(&subflow->node, &msk->conn_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	subflow->request_mptcp = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	/* accept() will wait on first subflow sk_wq, and we always wakes up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	 * via msk->sk_socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	RCU_INIT_POINTER(msk->first->sk_wq, &sk->sk_socket->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	sk_drops_add(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	__kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 			       struct sk_buff *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	bool fragstolen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	int delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	if (MPTCP_SKB_CB(from)->offset ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	    !skb_try_coalesce(to, from, &fragstolen, &delta))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 		 to->len, MPTCP_SKB_CB(from)->end_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	kfree_skb_partial(from, fragstolen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	atomic_add(delta, &sk->sk_rmem_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	sk_mem_charge(sk, delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 				   struct sk_buff *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	return mptcp_try_coalesce((struct sock *)msk, to, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) /* "inspired" by tcp_data_queue_ofo(), main differences:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151)  * - use mptcp seqs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152)  * - don't cope with sacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	struct rb_node **p, *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	u64 seq, end_seq, max_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	struct sk_buff *skb1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	int space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	seq = MPTCP_SKB_CB(skb)->map_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	end_seq = MPTCP_SKB_CB(skb)->end_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	space = tcp_space(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	max_seq = space > 0 ? space + msk->ack_seq : msk->ack_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		 RB_EMPTY_ROOT(&msk->out_of_order_queue));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	if (after64(seq, max_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 		/* out of window */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		mptcp_drop(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	p = &msk->out_of_order_queue.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		rb_link_node(&skb->rbnode, NULL, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		msk->ooo_last_skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	/* with 2 subflows, adding at end of ooo queue is quite likely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	/* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		parent = &msk->ooo_last_skb->rbnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		p = &parent->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		goto insert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	/* Find place to insert this segment. Handle overlaps on the way. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		skb1 = rb_to_skb(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 			p = &parent->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 			if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 				/* All the bits are present. Drop. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 				mptcp_drop(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 			if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 				/* partial overlap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 				 *     |     skb      |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 				 *  |     skb1    |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 				 * continue traversing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 				/* skb's seq == skb1's seq and skb covers skb1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 				 * Replace skb1 with skb.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 				rb_replace_node(&skb1->rbnode, &skb->rbnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 						&msk->out_of_order_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 				mptcp_drop(sk, skb1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 				goto merge_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		} else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		p = &parent->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) insert:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	/* Insert segment into RB tree. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	rb_link_node(&skb->rbnode, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) merge_right:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	/* Remove other segments covered by skb. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	while ((skb1 = skb_rb_next(skb)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		mptcp_drop(sk, skb1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	/* If there is no skb after us, we are the last_skb ! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	if (!skb1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		msk->ooo_last_skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	skb_condense(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	skb_set_owner_r(skb, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 			     struct sk_buff *skb, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 			     size_t copy_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	struct sk_buff *tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	__skb_unlink(skb, &ssk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	skb_ext_reset(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	skb_orphan(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	/* try to fetch required memory from subflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		int amount = sk_mem_pages(skb->truesize) << SK_MEM_QUANTUM_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		if (ssk->sk_forward_alloc < amount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		ssk->sk_forward_alloc -= amount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		sk->sk_forward_alloc += amount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	/* the skb map_seq accounts for the skb offset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	 * value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	MPTCP_SKB_CB(skb)->offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		/* in sequence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		tail = skb_peek_tail(&sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		if (tail && mptcp_try_coalesce(sk, tail, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		skb_set_owner_r(skb, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		__skb_queue_tail(&sk->sk_receive_queue, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	} else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		mptcp_data_queue_ofo(msk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	/* old data, keep it simple and drop the whole pkt, sender
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	 * will retransmit as needed, if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	mptcp_drop(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) static void mptcp_stop_timer(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	mptcp_sk(sk)->timer_ival = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) static void mptcp_check_data_fin_ack(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	if (__mptcp_check_fallback(msk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	/* Look for an acknowledged DATA_FIN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	if (((1 << sk->sk_state) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	     (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	    msk->write_seq == atomic64_read(&msk->snd_una)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		mptcp_stop_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		WRITE_ONCE(msk->snd_data_fin_enable, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		switch (sk->sk_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		case TCP_FIN_WAIT1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 			inet_sk_state_store(sk, TCP_FIN_WAIT2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 			sk->sk_state_change(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		case TCP_CLOSING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		case TCP_LAST_ACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 			inet_sk_state_store(sk, TCP_CLOSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 			sk->sk_state_change(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		if (sk->sk_shutdown == SHUTDOWN_MASK ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		    sk->sk_state == TCP_CLOSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 			sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 			sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	if (READ_ONCE(msk->rcv_data_fin) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	    ((1 << sk->sk_state) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	     (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		if (msk->ack_seq == rcv_data_fin_seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 			if (seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 				*seq = rcv_data_fin_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	long tout = ssk && inet_csk(ssk)->icsk_pending ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 				      inet_csk(ssk)->icsk_timeout - jiffies : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	if (tout <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		tout = mptcp_sk(sk)->timer_ival;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) static void mptcp_check_data_fin(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	u64 rcv_data_fin_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	if (__mptcp_check_fallback(msk) || !msk->first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	/* Need to ack a DATA_FIN received from a peer while this side
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	 * msk->rcv_data_fin was set when parsing the incoming options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	 * at the subflow level and the msk lock was not held, so this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	 * is the first opportunity to act on the DATA_FIN and change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	 * the msk state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	 * If we are caught up to the sequence number of the incoming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	 * DATA_FIN, send the DATA_ACK now and do state transition.  If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	 * not caught up, do nothing and let the recv code send DATA_ACK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	 * when catching up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		WRITE_ONCE(msk->rcv_data_fin, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		sk->sk_shutdown |= RCV_SHUTDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		set_bit(MPTCP_DATA_READY, &msk->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		switch (sk->sk_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		case TCP_ESTABLISHED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 			inet_sk_state_store(sk, TCP_CLOSE_WAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		case TCP_FIN_WAIT1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 			inet_sk_state_store(sk, TCP_CLOSING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		case TCP_FIN_WAIT2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 			inet_sk_state_store(sk, TCP_CLOSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 			// @@ Close subflows now?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 			/* Other states not expected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 			WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		mptcp_set_timeout(sk, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		mptcp_for_each_subflow(msk, subflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 			struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 			lock_sock(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 			tcp_send_ack(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 			release_sock(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		sk->sk_state_change(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		if (sk->sk_shutdown == SHUTDOWN_MASK ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		    sk->sk_state == TCP_CLOSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 			sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 			sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 					   struct sock *ssk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 					   unsigned int *bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	unsigned int moved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	bool more_data_avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	struct tcp_sock *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	u32 old_copied_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	bool done = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	pr_debug("msk=%p ssk=%p", msk, ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	tp = tcp_sk(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	old_copied_seq = tp->copied_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		u32 map_remaining, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		u32 seq = tp->copied_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		bool fin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		/* try to move as much data as available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		map_remaining = subflow->map_data_len -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 				mptcp_subflow_get_map_offset(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		skb = skb_peek(&ssk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 			/* if no data is found, a racing workqueue/recvmsg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 			 * already processed the new data, stop here or we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 			 * can enter an infinite loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 			if (!moved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 				done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		if (__mptcp_check_fallback(msk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 			/* if we are running under the workqueue, TCP could have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 			 * collapsed skbs between dummy map creation and now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 			 * be sure to adjust the size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 			map_remaining = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 			subflow->map_data_len = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		offset = seq - TCP_SKB_CB(skb)->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		if (fin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 			done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 			seq++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		if (offset < skb->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 			size_t len = skb->len - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 			if (tp->urg_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 				done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 			if (__mptcp_move_skb(msk, ssk, skb, offset, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 				moved += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 			seq += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 			if (WARN_ON_ONCE(map_remaining < len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 			WARN_ON_ONCE(!fin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 			sk_eat_skb(ssk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 			done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		WRITE_ONCE(tp->copied_seq, seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		more_data_avail = mptcp_subflow_data_available(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 			done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	} while (more_data_avail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	*bytes += moved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	if (tp->copied_seq != old_copied_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		tcp_cleanup_rbuf(ssk, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	return done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) static bool mptcp_ofo_queue(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	struct sk_buff *skb, *tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	bool moved = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	struct rb_node *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	u64 end_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	p = rb_first(&msk->out_of_order_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	while (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		skb = rb_to_skb(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		p = rb_next(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		rb_erase(&skb->rbnode, &msk->out_of_order_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 				      msk->ack_seq))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 			mptcp_drop(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		end_seq = MPTCP_SKB_CB(skb)->end_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		tail = skb_peek_tail(&sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 			int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 			/* skip overlapping data, if any */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 			pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 				 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 				 delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 			MPTCP_SKB_CB(skb)->offset += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 			__skb_queue_tail(&sk->sk_receive_queue, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		msk->ack_seq = end_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		moved = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	return moved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) /* In most cases we will be able to lock the mptcp socket.  If its already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590)  * owned, we need to defer to the work queue to avoid ABBA deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	unsigned int moved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	if (READ_ONCE(sk->sk_lock.owned))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	if (unlikely(!spin_trylock_bh(&sk->sk_lock.slock)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	/* must re-check after taking the lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	if (!READ_ONCE(sk->sk_lock.owned)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		__mptcp_move_skbs_from_subflow(msk, ssk, &moved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		mptcp_ofo_queue(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		/* If the moves have caught up with the DATA_FIN sequence number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		 * it's time to ack the DATA_FIN and change socket state, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		 * this is not a good place to change state. Let the workqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		 * do it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		if (mptcp_pending_data_fin(sk, NULL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		    schedule_work(&msk->work))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 			sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	spin_unlock_bh(&sk->sk_lock.slock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	return moved > 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) void mptcp_data_ready(struct sock *sk, struct sock *ssk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	bool wake;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	/* move_skbs_to_msk below can legitly clear the data_avail flag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	 * but we will need later to properly woke the reader, cache its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	 * value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	wake = subflow->data_avail == MPTCP_SUBFLOW_DATA_AVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	if (wake)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		set_bit(MPTCP_DATA_READY, &msk->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	if (atomic_read(&sk->sk_rmem_alloc) < READ_ONCE(sk->sk_rcvbuf) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	    move_skbs_to_msk(msk, ssk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		goto wake;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	/* don't schedule if mptcp sk is (still) over limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		goto wake;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	/* mptcp socket is owned, release_cb should retry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 			      &sk->sk_tsq_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		/* need to try again, its possible release_cb() has already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		 * been called after the test_and_set_bit() above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		move_skbs_to_msk(msk, ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) wake:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	if (wake)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		sk->sk_data_ready(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) static void __mptcp_flush_join_list(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	if (likely(list_empty(&msk->join_list)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	spin_lock_bh(&msk->join_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	list_splice_tail_init(&msk->join_list, &msk->conn_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	spin_unlock_bh(&msk->join_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) static bool mptcp_timer_pending(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) static void mptcp_reset_timer(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	unsigned long tout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	/* should never be called with mptcp level timer cleared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	tout = READ_ONCE(mptcp_sk(sk)->timer_ival);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	if (WARN_ON_ONCE(!tout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		tout = TCP_RTO_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) void mptcp_data_acked(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	mptcp_reset_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	if ((!test_bit(MPTCP_SEND_SPACE, &mptcp_sk(sk)->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	     (inet_sk_state_load(sk) != TCP_ESTABLISHED)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	    schedule_work(&mptcp_sk(sk)->work))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) void mptcp_subflow_eof(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	if (!test_and_set_bit(MPTCP_WORK_EOF, &msk->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	    schedule_work(&msk->work))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) static void mptcp_check_for_eof(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	int receivers = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	mptcp_for_each_subflow(msk, subflow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		receivers += !subflow->rx_eof;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	if (!receivers && !(sk->sk_shutdown & RCV_SHUTDOWN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		/* hopefully temporary hack: propagate shutdown status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		 * to msk, when all subflows agree on it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		sk->sk_shutdown |= RCV_SHUTDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		set_bit(MPTCP_DATA_READY, &msk->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		sk->sk_data_ready(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) static bool mptcp_ext_cache_refill(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	const struct sock *sk = (const struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	if (!msk->cached_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		msk->cached_ext = __skb_ext_alloc(sk->sk_allocation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	return !!msk->cached_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	sock_owned_by_me(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	mptcp_for_each_subflow(msk, subflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		if (subflow->data_avail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 			return mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) static bool mptcp_skb_can_collapse_to(u64 write_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 				      const struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 				      const struct mptcp_ext *mpext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	if (!tcp_skb_can_collapse_to(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	/* can collapse only if MPTCP level sequence is in order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	return mpext && mpext->data_seq + mpext->data_len == write_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) /* we can append data to the given data frag if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764)  * - there is space available in the backing page_frag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765)  * - the data frag tail matches the current page_frag free offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766)  * - the data frag end sequence number matches the current write seq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 				       const struct page_frag *pfrag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 				       const struct mptcp_data_frag *df)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	return df && pfrag->page == df->page &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		pfrag->offset == (df->offset + df->data_len) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		df->data_seq + df->data_len == msk->write_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) static void dfrag_uncharge(struct sock *sk, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	sk_mem_uncharge(sk, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	sk_wmem_queued_add(sk, -len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	int len = dfrag->data_len + dfrag->overhead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	list_del(&dfrag->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	dfrag_uncharge(sk, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	put_page(dfrag->page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) static bool mptcp_is_writeable(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	if (!sk_stream_is_writeable((struct sock *)msk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	mptcp_for_each_subflow(msk, subflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		if (sk_stream_is_writeable(subflow->tcp_sock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) static void mptcp_clean_una(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	struct mptcp_data_frag *dtmp, *dfrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	bool cleaned = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	u64 snd_una;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	/* on fallback we just need to ignore snd_una, as this is really
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	 * plain TCP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	if (__mptcp_check_fallback(msk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		atomic64_set(&msk->snd_una, msk->write_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	snd_una = atomic64_read(&msk->snd_una);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		dfrag_clear(sk, dfrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		cleaned = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	dfrag = mptcp_rtx_head(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	if (dfrag && after64(snd_una, dfrag->data_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		u64 delta = snd_una - dfrag->data_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		if (WARN_ON_ONCE(delta > dfrag->data_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		dfrag->data_seq += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		dfrag->offset += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		dfrag->data_len -= delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		dfrag_uncharge(sk, delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		cleaned = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	if (cleaned) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		sk_mem_reclaim_partial(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		/* Only wake up writers if a subflow is ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		if (mptcp_is_writeable(msk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 			set_bit(MPTCP_SEND_SPACE, &mptcp_sk(sk)->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 			smp_mb__after_atomic();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 			/* set SEND_SPACE before sk_stream_write_space clears
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 			 * NOSPACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 			sk_stream_write_space(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861)  * data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 					pfrag, sk->sk_allocation)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	sk->sk_prot->enter_memory_pressure(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	sk_stream_moderate_sndbuf(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) static struct mptcp_data_frag *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		      int orig_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	int offset = ALIGN(orig_offset, sizeof(long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	struct mptcp_data_frag *dfrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	dfrag->data_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	dfrag->data_seq = msk->write_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	dfrag->offset = offset + sizeof(struct mptcp_data_frag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	dfrag->page = pfrag->page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	return dfrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 			      struct msghdr *msg, struct mptcp_data_frag *dfrag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			      long *timeo, int *pmss_now,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 			      int *ps_goal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	int mss_now, avail_size, size_goal, offset, ret, frag_truesize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	bool dfrag_collapsed, can_collapse = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	struct mptcp_ext *mpext = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	bool retransmission = !!dfrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	struct sk_buff *skb, *tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	struct page_frag *pfrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	u64 *write_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	size_t psize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	/* use the mptcp page cache so that we can easily move the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	 * from one substream to another, but do per subflow memory accounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	 * Note: pfrag is used only !retransmission, but the compiler if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	 * fooled into a warning if we don't init here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	pfrag = sk_page_frag(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	if (!retransmission) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		write_seq = &msk->write_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		page = pfrag->page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		write_seq = &dfrag->data_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		page = dfrag->page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	/* compute copy limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	mss_now = tcp_send_mss(ssk, &size_goal, msg->msg_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	*pmss_now = mss_now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	*ps_goal = size_goal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	avail_size = size_goal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	skb = tcp_write_queue_tail(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	if (skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		/* Limit the write to the size available in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		 * current skb, if any, so that we create at most a new skb.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		 * Explicitly tells TCP internals to avoid collapsing on later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		 * queue management operation, to avoid breaking the ext <->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		 * SSN association set here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		can_collapse = (size_goal - skb->len > 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 			      mptcp_skb_can_collapse_to(*write_seq, skb, mpext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		if (!can_collapse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 			TCP_SKB_CB(skb)->eor = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 			avail_size = size_goal - skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	if (!retransmission) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		/* reuse tail pfrag, if possible, or carve a new one from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		 * page allocator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		dfrag = mptcp_rtx_tail(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		offset = pfrag->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		if (!dfrag_collapsed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			dfrag = mptcp_carve_data_frag(msk, pfrag, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			offset = dfrag->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 			frag_truesize = dfrag->overhead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		psize = min_t(size_t, pfrag->size - offset, avail_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		/* Copy to page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		pr_debug("left=%zu", msg_data_left(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		psize = copy_page_from_iter(pfrag->page, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 					    min_t(size_t, msg_data_left(msg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 						  psize),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 					    &msg->msg_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		pr_debug("left=%zu", msg_data_left(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		if (!psize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		if (!sk_wmem_schedule(sk, psize + dfrag->overhead)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 			iov_iter_revert(&msg->msg_iter, psize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		offset = dfrag->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		psize = min_t(size_t, dfrag->data_len, avail_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	/* tell the TCP stack to delay the push so that we can safely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	 * access the skb after the sendpages call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	ret = do_tcp_sendpages(ssk, page, offset, psize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 			       msg->msg_flags | MSG_SENDPAGE_NOTLAST | MSG_DONTWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	if (ret <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		if (!retransmission)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			iov_iter_revert(&msg->msg_iter, psize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	frag_truesize += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	if (!retransmission) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		if (unlikely(ret < psize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 			iov_iter_revert(&msg->msg_iter, psize - ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		/* send successful, keep track of sent data for mptcp-level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		 * retransmission
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		dfrag->data_len += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		if (!dfrag_collapsed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 			get_page(dfrag->page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 			list_add_tail(&dfrag->list, &msk->rtx_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 			sk_wmem_queued_add(sk, frag_truesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			sk_wmem_queued_add(sk, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		/* charge data on mptcp rtx queue to the master socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		 * Note: we charge such data both to sk and ssk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		sk->sk_forward_alloc -= frag_truesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	/* if the tail skb extension is still the cached one, collapsing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	 * really happened. Note: we can't check for 'same skb' as the sk_buff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	 * hdr on tail can be transmitted, freed and re-allocated by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	 * do_tcp_sendpages() call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	tail = tcp_write_queue_tail(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	if (mpext && tail && mpext == skb_ext_find(tail, SKB_EXT_MPTCP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		WARN_ON_ONCE(!can_collapse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		mpext->data_len += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	skb = tcp_write_queue_tail(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	mpext = __skb_ext_set(skb, SKB_EXT_MPTCP, msk->cached_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	msk->cached_ext = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	memset(mpext, 0, sizeof(*mpext));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	mpext->data_seq = *write_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	mpext->data_len = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	mpext->use_map = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	mpext->dsn64 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		 mpext->dsn64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	if (!retransmission)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		pfrag->offset += frag_truesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	WRITE_ONCE(*write_seq, *write_seq + ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	mptcp_subflow_ctx(ssk)->rel_write_seq += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) static void mptcp_nospace(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	clear_bit(MPTCP_SEND_SPACE, &msk->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	mptcp_for_each_subflow(msk, subflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		struct socket *sock = READ_ONCE(ssk->sk_socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		/* enables ssk->write_space() callbacks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		if (sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 			set_bit(SOCK_NOSPACE, &sock->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) static bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	/* can't send if JOIN hasn't completed yet (i.e. is usable for mptcp) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	if (subflow->request_join && !subflow->fully_established)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	/* only send if our side has not closed yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	return ((1 << ssk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) #define MPTCP_SEND_BURST_SIZE		((1 << 16) - \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 					 sizeof(struct tcphdr) - \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 					 MAX_TCP_OPTION_SPACE - \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 					 sizeof(struct ipv6hdr) - \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 					 sizeof(struct frag_hdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) struct subflow_send_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	struct sock *ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	u64 ratio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 					   u32 *sndbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	struct subflow_send_info send_info[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	int i, nr_active = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	struct sock *ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	u64 ratio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	u32 pace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	sock_owned_by_me((struct sock *)msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	*sndbuf = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	if (!mptcp_ext_cache_refill(msk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	if (__mptcp_check_fallback(msk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		if (!msk->first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		*sndbuf = msk->first->sk_sndbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		return sk_stream_memory_free(msk->first) ? msk->first : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	/* re-use last subflow, if the burst allow that */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	if (msk->last_snd && msk->snd_burst > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	    sk_stream_memory_free(msk->last_snd) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	    mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		mptcp_for_each_subflow(msk, subflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 			ssk =  mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 			*sndbuf = max(tcp_sk(ssk)->snd_wnd, *sndbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		return msk->last_snd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	/* pick the subflow with the lower wmem/wspace ratio */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	for (i = 0; i < 2; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		send_info[i].ssk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		send_info[i].ratio = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	mptcp_for_each_subflow(msk, subflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		ssk =  mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		if (!mptcp_subflow_active(subflow))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		nr_active += !subflow->backup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		*sndbuf = max(tcp_sk(ssk)->snd_wnd, *sndbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		if (!sk_stream_memory_free(subflow->tcp_sock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		pace = READ_ONCE(ssk->sk_pacing_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		if (!pace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 				pace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		if (ratio < send_info[subflow->backup].ratio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 			send_info[subflow->backup].ssk = ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 			send_info[subflow->backup].ratio = ratio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	pr_debug("msk=%p nr_active=%d ssk=%p:%lld backup=%p:%lld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		 msk, nr_active, send_info[0].ssk, send_info[0].ratio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		 send_info[1].ssk, send_info[1].ratio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	/* pick the best backup if no other subflow is active */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	if (!nr_active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		send_info[0].ssk = send_info[1].ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	if (send_info[0].ssk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		msk->last_snd = send_info[0].ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 				       sk_stream_wspace(msk->last_snd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		return msk->last_snd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) static void ssk_check_wmem(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	if (unlikely(!mptcp_is_writeable(msk)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		mptcp_nospace(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	int mss_now = 0, size_goal = 0, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	struct page_frag *pfrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	size_t copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	struct sock *ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	u32 sndbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	bool tx_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	long timeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		ret = sk_stream_wait_connect(sk, &timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	pfrag = sk_page_frag(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	mptcp_clean_una(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		ret = -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	__mptcp_flush_join_list(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	ssk = mptcp_subflow_get_send(msk, &sndbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	while (!sk_stream_memory_free(sk) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	       !ssk ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	       !mptcp_page_frag_refill(ssk, pfrag)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		if (ssk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 			/* make sure retransmit timer is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 			 * running before we wait for memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 			 * The retransmit timer might be needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 			 * to make the peer send an up-to-date
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 			 * MPTCP Ack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 			mptcp_set_timeout(sk, ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 			if (!mptcp_timer_pending(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 				mptcp_reset_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		mptcp_nospace(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		ret = sk_stream_wait_memory(sk, &timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		mptcp_clean_una(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		ssk = mptcp_subflow_get_send(msk, &sndbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		if (list_empty(&msk->conn_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 			ret = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	/* do auto tuning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	    sndbuf > READ_ONCE(sk->sk_sndbuf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 		WRITE_ONCE(sk->sk_sndbuf, sndbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	pr_debug("conn_list->subflow=%p", ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	lock_sock(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	tx_ok = msg_data_left(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	while (tx_ok) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 		ret = mptcp_sendmsg_frag(sk, ssk, msg, NULL, &timeo, &mss_now,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 					 &size_goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 			if (ret == -EAGAIN && timeo > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 				mptcp_set_timeout(sk, ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 				release_sock(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 				goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		/* burst can be negative, we will try move to the next subflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		 * at selection time, if possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		msk->snd_burst -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		copied += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		tx_ok = msg_data_left(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		if (!tx_ok)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		if (!sk_stream_memory_free(ssk) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 		    !mptcp_page_frag_refill(ssk, pfrag) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		    !mptcp_ext_cache_refill(msk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 			tcp_push(ssk, msg->msg_flags, mss_now,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 				 tcp_sk(ssk)->nonagle, size_goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 			mptcp_set_timeout(sk, ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 			release_sock(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 			goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		/* memory is charged to mptcp level socket as well, i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		 * if msg is very large, mptcp socket may run out of buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 		 * space.  mptcp_clean_una() will release data that has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		 * been acked at mptcp level in the mean time, so there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		 * a good chance we can continue sending data right away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		 * Normally, when the tcp subflow can accept more data, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		 * so can the MPTCP socket.  However, we need to cope with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		 * peers that might lag behind in their MPTCP-level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		 * acknowledgements, i.e.  data might have been acked at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		 * tcp level only.  So, we must also check the MPTCP socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		 * limits before we send more data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 		if (unlikely(!sk_stream_memory_free(sk))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 			tcp_push(ssk, msg->msg_flags, mss_now,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 				 tcp_sk(ssk)->nonagle, size_goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 			mptcp_clean_una(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 			if (!sk_stream_memory_free(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 				/* can't send more for now, need to wait for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 				 * MPTCP-level ACKs from peer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 				 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 				 * Wakeup will happen via mptcp_clean_una().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 				mptcp_set_timeout(sk, ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 				release_sock(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 				goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	mptcp_set_timeout(sk, ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	if (copied) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		tcp_push(ssk, msg->msg_flags, mss_now, tcp_sk(ssk)->nonagle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 			 size_goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		/* start the timer, if it's not pending */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		if (!mptcp_timer_pending(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 			mptcp_reset_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	release_sock(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	ssk_check_wmem(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	return copied ? : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) static void mptcp_wait_data(struct sock *sk, long *timeo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	add_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	sk_wait_event(sk, timeo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 		      test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	remove_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 				struct msghdr *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 				size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	int copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		u32 offset = MPTCP_SKB_CB(skb)->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		u32 data_len = skb->len - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		u32 count = min_t(size_t, len - copied, data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		err = skb_copy_datagram_msg(skb, offset, msg, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		if (unlikely(err < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 			if (!copied)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		copied += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		if (count < data_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 			MPTCP_SKB_CB(skb)->offset += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		__skb_unlink(skb, &sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		__kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		if (copied >= len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	return copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) /* receive buffer autotuning.  See tcp_rcv_space_adjust for more information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379)  * Only difference: Use highest rtt estimate of the subflows in use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	u32 time, advmss = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	u64 rtt_us, mstamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	sock_owned_by_me(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	if (copied <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	msk->rcvq_space.copied += copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	rtt_us = msk->rcvq_space.rtt_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	if (rtt_us && time < (rtt_us >> 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	rtt_us = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	mptcp_for_each_subflow(msk, subflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 		const struct tcp_sock *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 		u64 sf_rtt_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 		u32 sf_advmss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 		sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		sf_advmss = READ_ONCE(tp->advmss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 		rtt_us = max(sf_rtt_us, rtt_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		advmss = max(sf_advmss, advmss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	msk->rcvq_space.rtt_us = rtt_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	if (time < (rtt_us >> 3) || rtt_us == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	if (msk->rcvq_space.copied <= msk->rcvq_space.space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		goto new_measure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		int rcvmem, rcvbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		u64 rcvwin, grow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		do_div(grow, msk->rcvq_space.space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 		rcvwin += (grow << 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 		rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		while (tcp_win_from_space(sk, rcvmem) < advmss)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 			rcvmem += 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 		do_div(rcvwin, advmss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		rcvbuf = min_t(u64, rcvwin * rcvmem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 			       sock_net(sk)->ipv4.sysctl_tcp_rmem[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 		if (rcvbuf > sk->sk_rcvbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 			u32 window_clamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 			window_clamp = tcp_win_from_space(sk, rcvbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 			WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 			/* Make subflows follow along.  If we do not do this, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 			 * get drops at subflow level if skbs can't be moved to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 			 * the mptcp rx queue fast enough (announced rcv_win can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 			 * exceed ssk->sk_rcvbuf).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 			mptcp_for_each_subflow(msk, subflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 				struct sock *ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 				bool slow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 				ssk = mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 				slow = lock_sock_fast(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 				WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 				tcp_sk(ssk)->window_clamp = window_clamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 				tcp_cleanup_rbuf(ssk, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 				unlock_sock_fast(ssk, slow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	msk->rcvq_space.space = msk->rcvq_space.copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) new_measure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	msk->rcvq_space.copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	msk->rcvq_space.time = mstamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) static bool __mptcp_move_skbs(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	unsigned int moved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	bool done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	/* avoid looping forever below on racing close */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	if (((struct sock *)msk)->sk_state == TCP_CLOSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	__mptcp_flush_join_list(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		struct sock *ssk = mptcp_subflow_recv_lookup(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		if (!ssk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 		lock_sock(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 		release_sock(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	} while (!done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	if (mptcp_ofo_queue(msk) || moved > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		mptcp_check_data_fin((struct sock *)msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 			 int nonblock, int flags, int *addr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	int copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	int target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	long timeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	timeo = sock_rcvtimeo(sk, nonblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	len = min_t(size_t, len, INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	__mptcp_flush_join_list(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	while (len > (size_t)copied) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		int bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 		bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 		if (unlikely(bytes_read < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 			if (!copied)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 				copied = bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		copied += bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		if (skb_queue_empty(&sk->sk_receive_queue) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 		    __mptcp_move_skbs(msk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		/* only the master socket status is relevant here. The exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		 * conditions mirror closely tcp_recvmsg()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		if (copied >= target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		if (copied) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 			if (sk->sk_err ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 			    sk->sk_state == TCP_CLOSE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 			    (sk->sk_shutdown & RCV_SHUTDOWN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 			    !timeo ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 			    signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 			if (sk->sk_err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 				copied = sock_error(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 			if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 				mptcp_check_for_eof(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 			if (sk->sk_shutdown & RCV_SHUTDOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 			if (sk->sk_state == TCP_CLOSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 				copied = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 			if (!timeo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 				copied = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 			if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 				copied = sock_intr_errno(timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 		pr_debug("block timeout %ld", timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		mptcp_wait_data(sk, &timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	if (skb_queue_empty(&sk->sk_receive_queue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 		/* entire backlog drained, clear DATA_READY. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		clear_bit(MPTCP_DATA_READY, &msk->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		/* .. race-breaker: ssk might have gotten new data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 		 * after last __mptcp_move_skbs() returned false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		if (unlikely(__mptcp_move_skbs(msk)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 			set_bit(MPTCP_DATA_READY, &msk->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	} else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		/* data to read but mptcp_wait_data() cleared DATA_READY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		set_bit(MPTCP_DATA_READY, &msk->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	pr_debug("msk=%p data_ready=%d rx queue empty=%d copied=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		 msk, test_bit(MPTCP_DATA_READY, &msk->flags),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		 skb_queue_empty(&sk->sk_receive_queue), copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	mptcp_rcv_space_adjust(msk, copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	return copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) static void mptcp_retransmit_handler(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	if (atomic64_read(&msk->snd_una) == READ_ONCE(msk->write_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 		mptcp_stop_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		set_bit(MPTCP_WORK_RTX, &msk->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 		if (schedule_work(&msk->work))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 			sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) static void mptcp_retransmit_timer(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	struct inet_connection_sock *icsk = from_timer(icsk, t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 						       icsk_retransmit_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	struct sock *sk = &icsk->icsk_inet.sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	bh_lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	if (!sock_owned_by_user(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		mptcp_retransmit_handler(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		/* delegate our work to tcp_release_cb() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 				      &sk->sk_tsq_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 			sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	bh_unlock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) /* Find an idle subflow.  Return NULL if there is unacked data at tcp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638)  * level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640)  * A backup subflow is returned only if that is the only kind available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	struct sock *backup = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	sock_owned_by_me((const struct sock *)msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	if (__mptcp_check_fallback(msk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		return msk->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	mptcp_for_each_subflow(msk, subflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 		if (!mptcp_subflow_active(subflow))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 		/* still data outstanding at TCP level?  Don't retransmit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		if (!tcp_write_queue_empty(ssk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 			if (inet_csk(ssk)->icsk_ca_state >= TCP_CA_Loss)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 		if (subflow->backup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 			if (!backup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 				backup = ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 		return ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	return backup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) /* subflow sockets can be either outgoing (connect) or incoming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)  * (accept).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680)  * Outgoing subflows use in-kernel sockets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681)  * Incoming subflows do not have their own 'struct socket' allocated,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682)  * so we need to use tcp_close() after detaching them from the mptcp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683)  * parent socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		       struct mptcp_subflow_context *subflow,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 		       long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	struct socket *sock = READ_ONCE(ssk->sk_socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	list_del(&subflow->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	if (sock && sock != sk->sk_socket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 		/* outgoing subflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 		sock_release(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		/* incoming subflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		tcp_close(ssk, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) static void pm_work(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	struct mptcp_pm_data *pm = &msk->pm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	spin_lock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	pr_debug("msk=%p status=%x", msk, pm->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 		pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 		mptcp_pm_nl_add_addr_received(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 		pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 		mptcp_pm_nl_rm_addr_received(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 	if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 		mptcp_pm_nl_fully_established(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 		mptcp_pm_nl_subflow_established(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	spin_unlock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) static void __mptcp_close_subflow(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	struct mptcp_subflow_context *subflow, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		if (inet_sk_state_load(ssk) != TCP_CLOSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		__mptcp_close_ssk((struct sock *)msk, ssk, subflow, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) static void mptcp_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	struct sock *ssk, *sk = &msk->sk.icsk_inet.sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	int orig_len, orig_offset, mss_now = 0, size_goal = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	struct mptcp_data_frag *dfrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	u64 orig_write_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	size_t copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	struct msghdr msg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 		.msg_flags = MSG_DONTWAIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	long timeo = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	mptcp_clean_una(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	mptcp_check_data_fin_ack(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	__mptcp_flush_join_list(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		__mptcp_close_subflow(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	__mptcp_move_skbs(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	if (msk->pm.status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		pm_work(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 		mptcp_check_for_eof(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	mptcp_check_data_fin(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	dfrag = mptcp_rtx_head(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	if (!dfrag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	if (!mptcp_ext_cache_refill(msk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		goto reset_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	ssk = mptcp_subflow_get_retrans(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	if (!ssk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 		goto reset_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	lock_sock(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	orig_len = dfrag->data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	orig_offset = dfrag->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	orig_write_seq = dfrag->data_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	while (dfrag->data_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 		int ret = mptcp_sendmsg_frag(sk, ssk, &msg, dfrag, &timeo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 					     &mss_now, &size_goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 		copied += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 		dfrag->data_len -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 		dfrag->offset += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		if (!mptcp_ext_cache_refill(msk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	if (copied)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 		tcp_push(ssk, msg.msg_flags, mss_now, tcp_sk(ssk)->nonagle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 			 size_goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	dfrag->data_seq = orig_write_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	dfrag->offset = orig_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	dfrag->data_len = orig_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	mptcp_set_timeout(sk, ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	release_sock(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) reset_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	if (!mptcp_timer_pending(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 		mptcp_reset_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) static int __mptcp_init_sock(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	spin_lock_init(&msk->join_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	INIT_LIST_HEAD(&msk->conn_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	INIT_LIST_HEAD(&msk->join_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	INIT_LIST_HEAD(&msk->rtx_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	__set_bit(MPTCP_SEND_SPACE, &msk->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	INIT_WORK(&msk->work, mptcp_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	msk->out_of_order_queue = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	msk->first = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	mptcp_pm_data_init(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	/* re-use the csk retrans timer for MPTCP-level retrans */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) static int mptcp_init_sock(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	ret = __mptcp_init_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	if (!mptcp_is_enabled(net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 		return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	ret = __mptcp_socket_create(mptcp_sk(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	sk_sockets_allocated_inc(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 	sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) static void __mptcp_clear_xmit(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	struct mptcp_data_frag *dtmp, *dfrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 		dfrag_clear(sk, dfrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) static void mptcp_cancel_work(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	if (cancel_work_sync(&msk->work))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 		sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	lock_sock(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	switch (ssk->sk_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	case TCP_LISTEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 		if (!(how & RCV_SHUTDOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	case TCP_SYN_SENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 		tcp_disconnect(ssk, O_NONBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 		if (__mptcp_check_fallback(mptcp_sk(sk))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 			pr_debug("Fallback");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 			ssk->sk_shutdown |= how;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 			tcp_shutdown(ssk, how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 			pr_debug("Sending DATA_FIN on subflow %p", ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 			mptcp_set_timeout(sk, ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 			tcp_send_ack(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	release_sock(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) static const unsigned char new_state[16] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	/* current state:     new state:      action:	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	[0 /* (Invalid) */] = TCP_CLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	[TCP_ESTABLISHED]   = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	[TCP_SYN_SENT]      = TCP_CLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	[TCP_SYN_RECV]      = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	[TCP_FIN_WAIT1]     = TCP_FIN_WAIT1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	[TCP_FIN_WAIT2]     = TCP_FIN_WAIT2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	[TCP_TIME_WAIT]     = TCP_CLOSE,	/* should not happen ! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	[TCP_CLOSE]         = TCP_CLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	[TCP_CLOSE_WAIT]    = TCP_LAST_ACK  | TCP_ACTION_FIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	[TCP_LAST_ACK]      = TCP_LAST_ACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	[TCP_LISTEN]        = TCP_CLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	[TCP_CLOSING]       = TCP_CLOSING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	[TCP_NEW_SYN_RECV]  = TCP_CLOSE,	/* should not happen ! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) static int mptcp_close_state(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	int next = (int)new_state[sk->sk_state];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 	int ns = next & TCP_STATE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	inet_sk_state_store(sk, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	return next & TCP_ACTION_FIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) static void mptcp_close(struct sock *sk, long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	struct mptcp_subflow_context *subflow, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	LIST_HEAD(conn_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	sk->sk_shutdown = SHUTDOWN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	if (sk->sk_state == TCP_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 		inet_sk_state_store(sk, TCP_CLOSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	} else if (sk->sk_state == TCP_CLOSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	if (__mptcp_check_fallback(msk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 		goto update_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	} else if (mptcp_close_state(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 		pr_debug("Sending DATA_FIN sk=%p", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 		WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 		WRITE_ONCE(msk->snd_data_fin_enable, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		mptcp_for_each_subflow(msk, subflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 			struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 			mptcp_subflow_shutdown(sk, tcp_sk, SHUTDOWN_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	sk_stream_wait_close(sk, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) update_state:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	inet_sk_state_store(sk, TCP_CLOSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	/* be sure to always acquire the join list lock, to sync vs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	 * mptcp_finish_join().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	spin_lock_bh(&msk->join_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	list_splice_tail_init(&msk->join_list, &msk->conn_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 	spin_unlock_bh(&msk->join_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	list_splice_init(&msk->conn_list, &conn_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	__mptcp_clear_xmit(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 	list_for_each_entry_safe(subflow, tmp, &conn_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		__mptcp_close_ssk(sk, ssk, subflow, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	mptcp_cancel_work(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 	__skb_queue_purge(&sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	sk_common_release(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) #if IS_ENABLED(CONFIG_MPTCP_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	struct ipv6_pinfo *msk6 = inet6_sk(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	msk->sk_v6_daddr = ssk->sk_v6_daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	if (msk6 && ssk6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 		msk6->saddr = ssk6->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 		msk6->flow_label = ssk6->flow_label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) static int mptcp_disconnect(struct sock *sk, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	/* Should never be called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	 * inet_stream_connect() calls ->disconnect, but that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	 * refers to the subflow socket, not the mptcp one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) #if IS_ENABLED(CONFIG_MPTCP_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 	unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) struct sock *mptcp_sk_clone(const struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 			    const struct mptcp_options_received *mp_opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 			    struct request_sock *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	struct mptcp_sock *msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 	u64 ack_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	if (!nsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) #if IS_ENABLED(CONFIG_MPTCP_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	if (nsk->sk_family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 		inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	__mptcp_init_sock(nsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 	msk = mptcp_sk(nsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 	msk->local_key = subflow_req->local_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 	msk->token = subflow_req->token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 	msk->subflow = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	WRITE_ONCE(msk->fully_established, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	msk->write_seq = subflow_req->idsn + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 	atomic64_set(&msk->snd_una, msk->write_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	if (mp_opt->mp_capable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 		msk->can_ack = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 		msk->remote_key = mp_opt->sndr_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 		mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 		ack_seq++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 		WRITE_ONCE(msk->ack_seq, ack_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 	sock_reset_flag(nsk, SOCK_RCU_FREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	/* will be fully established after successful MPC subflow creation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	inet_sk_state_store(nsk, TCP_SYN_RECV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	security_inet_csk_clone(nsk, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	bh_unlock_sock(nsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	/* keep a single reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	__sock_put(nsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	return nsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	const struct tcp_sock *tp = tcp_sk(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	msk->rcvq_space.copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	msk->rcvq_space.rtt_us = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	msk->rcvq_space.time = tp->tcp_mstamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	/* initial rcv_space offering made to peer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 				      TCP_INIT_CWND * tp->advmss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	if (msk->rcvq_space.space == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 		msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 				 bool kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	struct socket *listener;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	struct sock *newsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	listener = __mptcp_nmpc_socket(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	if (WARN_ON_ONCE(!listener)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 		*err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 	newsk = inet_csk_accept(listener->sk, flags, err, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	if (!newsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 	pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	if (sk_is_mptcp(newsk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 		struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 		struct sock *new_mptcp_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 		struct sock *ssk = newsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 		subflow = mptcp_subflow_ctx(newsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 		new_mptcp_sock = subflow->conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 		/* is_mptcp should be false if subflow->conn is missing, see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 		 * subflow_syn_recv_sock()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 		if (WARN_ON_ONCE(!new_mptcp_sock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 			tcp_sk(newsk)->is_mptcp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 		/* acquire the 2nd reference for the owning socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 		sock_hold(new_mptcp_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 		local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 		bh_lock_sock(new_mptcp_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 		msk = mptcp_sk(new_mptcp_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 		msk->first = newsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 		newsk = new_mptcp_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 		mptcp_copy_inaddrs(newsk, ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 		list_add(&subflow->node, &msk->conn_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 		mptcp_rcv_space_init(msk, ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 		bh_unlock_sock(new_mptcp_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 		__MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 		local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 		MPTCP_INC_STATS(sock_net(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 				MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	newsk->sk_kern_sock = kern;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	return newsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) void mptcp_destroy_common(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 	skb_rbtree_purge(&msk->out_of_order_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 	mptcp_token_destroy(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	mptcp_pm_free_anno_list(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) static void mptcp_destroy(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 	if (msk->cached_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 		__skb_ext_put(msk->cached_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 	mptcp_destroy_common(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	sk_sockets_allocated_dec(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) static int mptcp_setsockopt_sol_socket(struct mptcp_sock *msk, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 				       sockptr_t optval, unsigned int optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 	struct socket *ssock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	case SO_REUSEPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 	case SO_REUSEADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 		lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 		ssock = __mptcp_nmpc_socket(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 		if (!ssock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 			release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 		ret = sock_setsockopt(ssock, SOL_SOCKET, optname, optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 		if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 			if (optname == SO_REUSEPORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 				sk->sk_reuseport = ssock->sk->sk_reuseport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 			else if (optname == SO_REUSEADDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 				sk->sk_reuse = ssock->sk->sk_reuse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 		release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	return sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname, optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) static int mptcp_setsockopt_v6(struct mptcp_sock *msk, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 			       sockptr_t optval, unsigned int optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 	struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	int ret = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 	struct socket *ssock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 	switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 	case IPV6_V6ONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 		lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 		ssock = __mptcp_nmpc_socket(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 		if (!ssock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 			release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 		ret = tcp_setsockopt(ssock->sk, SOL_IPV6, optname, optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 		if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 			sk->sk_ipv6only = ssock->sk->sk_ipv6only;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 		release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) static bool mptcp_unsupported(int level, int optname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	if (level == SOL_IP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 		switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 		case IP_ADD_MEMBERSHIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 		case IP_ADD_SOURCE_MEMBERSHIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 		case IP_DROP_MEMBERSHIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 		case IP_DROP_SOURCE_MEMBERSHIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 		case IP_BLOCK_SOURCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 		case IP_UNBLOCK_SOURCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 		case MCAST_JOIN_GROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 		case MCAST_LEAVE_GROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 		case MCAST_JOIN_SOURCE_GROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 		case MCAST_LEAVE_SOURCE_GROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 		case MCAST_BLOCK_SOURCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 		case MCAST_UNBLOCK_SOURCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 		case MCAST_MSFILTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	if (level == SOL_IPV6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 		switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 		case IPV6_ADDRFORM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 		case IPV6_ADD_MEMBERSHIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 		case IPV6_DROP_MEMBERSHIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 		case IPV6_JOIN_ANYCAST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 		case IPV6_LEAVE_ANYCAST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 		case MCAST_JOIN_GROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 		case MCAST_LEAVE_GROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 		case MCAST_JOIN_SOURCE_GROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 		case MCAST_LEAVE_SOURCE_GROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 		case MCAST_BLOCK_SOURCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 		case MCAST_UNBLOCK_SOURCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 		case MCAST_MSFILTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) static int mptcp_setsockopt(struct sock *sk, int level, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 			    sockptr_t optval, unsigned int optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 	struct sock *ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	pr_debug("msk=%p", msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 	if (mptcp_unsupported(level, optname))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 		return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 	if (level == SOL_SOCKET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 		return mptcp_setsockopt_sol_socket(msk, optname, optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 	/* @@ the meaning of setsockopt() when the socket is connected and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 	 * there are multiple subflows is not yet defined. It is up to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 	 * MPTCP-level socket to configure the subflows until the subflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 	 * is in TCP fallback, when TCP socket options are passed through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 	 * to the one remaining subflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	ssk = __mptcp_tcp_fallback(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	if (ssk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 		return tcp_setsockopt(ssk, level, optname, optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 	if (level == SOL_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 		return mptcp_setsockopt_v6(msk, optname, optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 	return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) static int mptcp_getsockopt(struct sock *sk, int level, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 			    char __user *optval, int __user *option)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	struct sock *ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	pr_debug("msk=%p", msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 	/* @@ the meaning of setsockopt() when the socket is connected and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	 * there are multiple subflows is not yet defined. It is up to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 	 * MPTCP-level socket to configure the subflows until the subflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 	 * is in TCP fallback, when socket options are passed through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 	 * to the one remaining subflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 	ssk = __mptcp_tcp_fallback(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	if (ssk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 		return tcp_getsockopt(ssk, level, optname, optval, option);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 	return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) #define MPTCP_DEFERRED_ALL (TCPF_DELACK_TIMER_DEFERRED | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 			    TCPF_WRITE_TIMER_DEFERRED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) /* this is very alike tcp_release_cb() but we must handle differently a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359)  * different set of events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) static void mptcp_release_cb(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 	unsigned long flags, nflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 		flags = sk->sk_tsq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 		if (!(flags & MPTCP_DEFERRED_ALL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 		nflags = flags & ~MPTCP_DEFERRED_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 	} while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 	sock_release_ownership(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 	if (flags & TCPF_DELACK_TIMER_DEFERRED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 		struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 		struct sock *ssk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 		ssk = mptcp_subflow_recv_lookup(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 		if (!ssk || !schedule_work(&msk->work))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 			__sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 	if (flags & TCPF_WRITE_TIMER_DEFERRED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 		mptcp_retransmit_handler(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 		__sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) static int mptcp_hash(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 	/* should never be called,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 	 * we hash the TCP subflows not the master socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 	WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) static void mptcp_unhash(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 	/* called from sk_common_release(), but nothing to do here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) static int mptcp_get_port(struct sock *sk, unsigned short snum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 	struct socket *ssock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	ssock = __mptcp_nmpc_socket(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 	pr_debug("msk=%p, subflow=%p", msk, ssock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 	if (WARN_ON_ONCE(!ssock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 	return inet_csk_get_port(ssock->sk, snum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) void mptcp_finish_connect(struct sock *ssk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 	struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 	struct mptcp_sock *msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 	struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 	u64 ack_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 	subflow = mptcp_subflow_ctx(ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 	sk = subflow->conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 	pr_debug("msk=%p, token=%u", sk, subflow->token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 	mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	ack_seq++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 	subflow->map_seq = ack_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	subflow->map_subflow_seq = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 	/* the socket is not connected yet, no msk/subflow ops can access/race
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 	 * accessing the field below
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 	WRITE_ONCE(msk->remote_key, subflow->remote_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 	WRITE_ONCE(msk->local_key, subflow->local_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 	WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 	WRITE_ONCE(msk->ack_seq, ack_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 	WRITE_ONCE(msk->can_ack, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 	atomic64_set(&msk->snd_una, msk->write_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 	mptcp_pm_new_connection(msk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 	mptcp_rcv_space_init(msk, ssk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) static void mptcp_sock_graft(struct sock *sk, struct socket *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	write_lock_bh(&sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 	rcu_assign_pointer(sk->sk_wq, &parent->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 	sk_set_socket(sk, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 	sk->sk_uid = SOCK_INODE(parent)->i_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 	write_unlock_bh(&sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) bool mptcp_finish_join(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 	struct sock *parent = (void *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 	struct socket *parent_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 	pr_debug("msk=%p, subflow=%p", msk, subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 	/* mptcp socket already closing? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 	if (!mptcp_is_fully_established(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 	if (!msk->pm.server_side)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	if (!mptcp_pm_allow_new_subflow(msk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	/* active connections are already on conn_list, and we can't acquire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	 * msk lock here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	 * use the join list lock as synchronization point and double-check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 	 * msk status to avoid racing with mptcp_close()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	spin_lock_bh(&msk->join_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	ret = inet_sk_state_load(parent) == TCP_ESTABLISHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 		list_add_tail(&subflow->node, &msk->join_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 	spin_unlock_bh(&msk->join_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	/* attach to msk socket only after we are sure he will deal with us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	 * at close time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	parent_sock = READ_ONCE(parent->sk_socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	if (parent_sock && !sk->sk_socket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 		mptcp_sock_graft(sk, parent_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	subflow->map_seq = READ_ONCE(msk->ack_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) static bool mptcp_memory_free(const struct sock *sk, int wake)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 	return wake ? test_bit(MPTCP_SEND_SPACE, &msk->flags) : true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) static struct proto mptcp_prot = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	.name		= "MPTCP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 	.init		= mptcp_init_sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 	.disconnect	= mptcp_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 	.close		= mptcp_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 	.accept		= mptcp_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 	.setsockopt	= mptcp_setsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 	.getsockopt	= mptcp_getsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	.shutdown	= tcp_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 	.destroy	= mptcp_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 	.sendmsg	= mptcp_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 	.recvmsg	= mptcp_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 	.release_cb	= mptcp_release_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	.hash		= mptcp_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	.unhash		= mptcp_unhash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	.get_port	= mptcp_get_port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 	.sockets_allocated	= &mptcp_sockets_allocated,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 	.memory_allocated	= &tcp_memory_allocated,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 	.memory_pressure	= &tcp_memory_pressure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 	.stream_memory_free	= mptcp_memory_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 	.sysctl_wmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_wmem),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	.sysctl_rmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_rmem),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 	.sysctl_mem	= sysctl_tcp_mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	.obj_size	= sizeof(struct mptcp_sock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 	.slab_flags	= SLAB_TYPESAFE_BY_RCU,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 	.no_autobind	= true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 	struct socket *ssock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 	lock_sock(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 	ssock = __mptcp_nmpc_socket(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 	if (!ssock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	err = ssock->ops->bind(ssock, uaddr, addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 		mptcp_copy_inaddrs(sock->sk, ssock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 	release_sock(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 					 struct mptcp_subflow_context *subflow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 	subflow->request_mptcp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 	__mptcp_do_fallback(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 				int addr_len, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 	struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 	struct socket *ssock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 	lock_sock(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 	if (sock->state != SS_UNCONNECTED && msk->subflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 		/* pending connection or invalid state, let existing subflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 		 * cope with that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 		ssock = msk->subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 		goto do_connect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 	ssock = __mptcp_nmpc_socket(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 	if (!ssock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 	mptcp_token_destroy(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 	inet_sk_state_store(sock->sk, TCP_SYN_SENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 	subflow = mptcp_subflow_ctx(ssock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) #ifdef CONFIG_TCP_MD5SIG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 	/* no MPTCP if MD5SIG is enabled on this socket or we may run out of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 	 * TCP option space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 		mptcp_subflow_early_fallback(msk, subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 		mptcp_subflow_early_fallback(msk, subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) do_connect:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 	err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 	sock->state = ssock->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 	/* on successful connect, the msk state will be moved to established by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 	 * subflow_finish_connect()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 	if (!err || err == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 		mptcp_copy_inaddrs(sock->sk, ssock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 		inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 	release_sock(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) static int mptcp_listen(struct socket *sock, int backlog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 	struct socket *ssock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 	pr_debug("msk=%p", msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	lock_sock(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	ssock = __mptcp_nmpc_socket(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 	if (!ssock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 	mptcp_token_destroy(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 	inet_sk_state_store(sock->sk, TCP_LISTEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 	sock_set_flag(sock->sk, SOCK_RCU_FREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 	err = ssock->ops->listen(ssock, backlog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 	inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 		mptcp_copy_inaddrs(sock->sk, ssock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 	release_sock(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 			       int flags, bool kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 	struct socket *ssock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 	pr_debug("msk=%p", msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 	lock_sock(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 	if (sock->sk->sk_state != TCP_LISTEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 		goto unlock_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 	ssock = __mptcp_nmpc_socket(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 	if (!ssock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 		goto unlock_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 	clear_bit(MPTCP_DATA_READY, &msk->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 	sock_hold(ssock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 	release_sock(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 	err = ssock->ops->accept(sock, newsock, flags, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 	if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 		struct mptcp_sock *msk = mptcp_sk(newsock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 		struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 		/* set ssk->sk_socket of accept()ed flows to mptcp socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 		 * This is needed so NOSPACE flag can be set from tcp stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 		__mptcp_flush_join_list(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 		mptcp_for_each_subflow(msk, subflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 			struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 			if (!ssk->sk_socket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 				mptcp_sock_graft(ssk, newsock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 	if (inet_csk_listen_poll(ssock->sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 		set_bit(MPTCP_DATA_READY, &msk->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 	sock_put(ssock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) unlock_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 	release_sock(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 	return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	       0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) static __poll_t mptcp_poll(struct file *file, struct socket *sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 			   struct poll_table_struct *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 	struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 	struct mptcp_sock *msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 	__poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 	int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 	msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 	sock_poll_wait(file, sock, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 	state = inet_sk_state_load(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 	pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 	if (state == TCP_LISTEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 		return mptcp_check_readable(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 	if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 		mask |= mptcp_check_readable(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 		if (test_bit(MPTCP_SEND_SPACE, &msk->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 			mask |= EPOLLOUT | EPOLLWRNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 	if (sk->sk_shutdown & RCV_SHUTDOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 		mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 	return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) static int mptcp_shutdown(struct socket *sock, int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 	struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 	pr_debug("sk=%p, how=%d", msk, how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 	lock_sock(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 	how++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	if ((how & ~SHUTDOWN_MASK) || !how) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 	if (sock->state == SS_CONNECTING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 		if ((1 << sock->sk->sk_state) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 		    (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 			sock->state = SS_DISCONNECTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 			sock->state = SS_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 	/* If we've already sent a FIN, or it's a closed state, skip this. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	if (__mptcp_check_fallback(msk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 		if (how == SHUT_WR || how == SHUT_RDWR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 			inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 		mptcp_for_each_subflow(msk, subflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 			struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 			mptcp_subflow_shutdown(sock->sk, tcp_sk, how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 	} else if ((how & SEND_SHUTDOWN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 		   ((1 << sock->sk->sk_state) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 		    (TCPF_ESTABLISHED | TCPF_SYN_SENT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 		     TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 		   mptcp_close_state(sock->sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 		__mptcp_flush_join_list(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 		WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 		WRITE_ONCE(msk->snd_data_fin_enable, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 		mptcp_for_each_subflow(msk, subflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 			struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 			mptcp_subflow_shutdown(sock->sk, tcp_sk, how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 	/* Wake up anyone sleeping in poll. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 	sock->sk->sk_state_change(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 	release_sock(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) static const struct proto_ops mptcp_stream_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 	.family		   = PF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 	.owner		   = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 	.release	   = inet_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 	.bind		   = mptcp_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 	.connect	   = mptcp_stream_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 	.socketpair	   = sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 	.accept		   = mptcp_stream_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 	.getname	   = inet_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 	.poll		   = mptcp_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 	.ioctl		   = inet_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 	.gettstamp	   = sock_gettstamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 	.listen		   = mptcp_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 	.shutdown	   = mptcp_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 	.setsockopt	   = sock_common_setsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 	.getsockopt	   = sock_common_getsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 	.sendmsg	   = inet_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 	.recvmsg	   = inet_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 	.mmap		   = sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 	.sendpage	   = inet_sendpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) static struct inet_protosw mptcp_protosw = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	.type		= SOCK_STREAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 	.protocol	= IPPROTO_MPTCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 	.prot		= &mptcp_prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	.ops		= &mptcp_stream_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 	.flags		= INET_PROTOSW_ICSK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) void __init mptcp_proto_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 	mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 	if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 		panic("Failed to allocate MPTCP pcpu counter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 	mptcp_subflow_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 	mptcp_pm_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 	mptcp_token_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 	if (proto_register(&mptcp_prot, 1) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 		panic("Failed to register MPTCP proto.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 	inet_register_protosw(&mptcp_protosw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 	BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) #if IS_ENABLED(CONFIG_MPTCP_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) static const struct proto_ops mptcp_v6_stream_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 	.family		   = PF_INET6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 	.owner		   = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 	.release	   = inet6_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 	.bind		   = mptcp_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 	.connect	   = mptcp_stream_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 	.socketpair	   = sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 	.accept		   = mptcp_stream_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 	.getname	   = inet6_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 	.poll		   = mptcp_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 	.ioctl		   = inet6_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 	.gettstamp	   = sock_gettstamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 	.listen		   = mptcp_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 	.shutdown	   = mptcp_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 	.setsockopt	   = sock_common_setsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 	.getsockopt	   = sock_common_getsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 	.sendmsg	   = inet6_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 	.recvmsg	   = inet6_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 	.mmap		   = sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 	.sendpage	   = inet_sendpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 	.compat_ioctl	   = inet6_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) static struct proto mptcp_v6_prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) static void mptcp_v6_destroy(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 	mptcp_destroy(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 	inet6_destroy_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) static struct inet_protosw mptcp_v6_protosw = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 	.type		= SOCK_STREAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 	.protocol	= IPPROTO_MPTCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 	.prot		= &mptcp_v6_prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 	.ops		= &mptcp_v6_stream_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 	.flags		= INET_PROTOSW_ICSK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) int __init mptcp_proto_v6_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 	mptcp_v6_prot = mptcp_prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 	strcpy(mptcp_v6_prot.name, "MPTCPv6");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 	mptcp_v6_prot.slab = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 	mptcp_v6_prot.destroy = mptcp_v6_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 	mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 	err = proto_register(&mptcp_v6_prot, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 	err = inet6_register_protosw(&mptcp_v6_protosw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 		proto_unregister(&mptcp_v6_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) #endif