7805 lines · 227.0 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * INET		An implementation of the TCP/IP protocol suite for the LINUX *		operating system.  INET is implemented using the  BSD Socket *		interface as the means of communication with the user level. * *		Implementation of the Transmission Control Protocol(TCP). * * Authors:	Ross Biro *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> *		Mark Evans, <evansmp@uhura.aston.ac.uk> *		Corey Minyard <wf-rch!minyard@relay.EU.net> *		Florian La Roche, <flla@stud.uni-sb.de> *		Charles Hedrick, <hedrick@klinzhai.rutgers.edu> *		Linus Torvalds, <torvalds@cs.helsinki.fi> *		Alan Cox, <gw4pts@gw4pts.ampr.org> *		Matthew Dillon, <dillon@apollo.west.oic.com> *		Arnt Gulbrandsen, <agulbra@nvg.unit.no> *		Jorge Cwik, <jorge@laser.satlink.net> */ /* * Changes: *		Pedro Roque	:	Fast Retransmit/Recovery. *					Two receive queues. *					Retransmit queue handled by TCP. *					Better retransmit timer handling. *					New congestion avoidance. *					Header prediction. *					Variable renaming. * *		Eric		:	Fast Retransmit. *		Randy Scott	:	MSS option defines. *		Eric Schenk	:	Fixes to slow start algorithm. *		Eric Schenk	:	Yet another double ACK bug. *		Eric Schenk	:	Delayed ACK bug fixes. *		Eric Schenk	:	Floyd style fast retrans war avoidance. *		David S. Miller	:	Don't allow zero congestion window. *		Eric Schenk	:	Fix retransmitter so that it sends *					next packet on ack of previous packet. *		Andi Kleen	:	Moved open_request checking here *					and process RSTs for open_requests. *		Andi Kleen	:	Better prune_queue, and other fixes. *		Andrey Savochkin:	Fix RTT measurements in the presence of *					timestamps. *		Andrey Savochkin:	Check sequence numbers correctly when *					removing SACKs due to in sequence incoming *					data segments. *		Andi Kleen:		Make sure we never ack data there is not *					enough room for. Also make this condition *					a fatal error if it might still happen. *		Andi Kleen:		Add tcp_measure_rcv_mss to make *					connections with MSS<min(MTU,ann. MSS) *					work without delayed acks. *		Andi Kleen:		Process packets with PSH set in the *					fast path. *		J Hadi Salim:		ECN support *	 	Andrei Gurtov, *		Pasi Sarolahti, *		Panu Kuhlberg:		Experimental audit of TCP (re)transmission *					engine. Lots of bugs are found. *		Pasi Sarolahti:		F-RTO for dealing with spurious RTOs */ #define pr_fmt(fmt) "TCP: " fmt #include <linux/mm.h>#include <linux/slab.h>#include <linux/module.h>#include <linux/sysctl.h>#include <linux/kernel.h>#include <linux/prefetch.h>#include <linux/bitops.h>#include <net/dst.h>#include <net/tcp.h>#include <net/tcp_ecn.h>#include <net/proto_memory.h>#include <net/inet_common.h>#include <linux/ipsec.h>#include <linux/unaligned.h>#include <linux/errqueue.h>#include <trace/events/tcp.h>#include <linux/jump_label_ratelimit.h>#include <net/busy_poll.h>#include <net/mptcp.h> int sysctl_tcp_max_orphans __read_mostly = NR_FILE; #define FLAG_DATA		0x01 /* Incoming frame contained data.		*/#define FLAG_WIN_UPDATE		0x02 /* Incoming ACK was a window update.	*/#define FLAG_DATA_ACKED		0x04 /* This ACK acknowledged new data.		*/#define FLAG_RETRANS_DATA_ACKED	0x08 /* "" "" some of which was retransmitted.	*/#define FLAG_SYN_ACKED		0x10 /* This ACK acknowledged SYN.		*/#define FLAG_DATA_SACKED	0x20 /* New SACK.				*/#define FLAG_ECE		0x40 /* ECE in this ACK				*/#define FLAG_LOST_RETRANS	0x80 /* This ACK marks some retransmission lost */#define FLAG_SLOWPATH		0x100 /* Do not skip RFC checks for window update.*/#define FLAG_ORIG_SACK_ACKED	0x200 /* Never retransmitted data are (s)acked	*/#define FLAG_SND_UNA_ADVANCED	0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */#define FLAG_DSACKING_ACK	0x800 /* SACK blocks contained D-SACK info */#define FLAG_SET_XMIT_TIMER	0x1000 /* Set TLP or RTO timer */#define FLAG_SACK_RENEGING	0x2000 /* snd_una advanced to a sacked seq */#define FLAG_UPDATE_TS_RECENT	0x4000 /* tcp_replace_ts_recent() */#define FLAG_NO_CHALLENGE_ACK	0x8000 /* do not call tcp_send_challenge_ack()	*/#define FLAG_ACK_MAYBE_DELAYED	0x10000 /* Likely a delayed ACK */#define FLAG_DSACK_TLP		0x20000 /* DSACK for tail loss probe */#define FLAG_TS_PROGRESS	0x40000 /* Positive timestamp delta */ #define FLAG_ACKED		(FLAG_DATA_ACKED|FLAG_SYN_ACKED)#define FLAG_NOT_DUP		(FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)#define FLAG_CA_ALERT		(FLAG_DATA_SACKED|FLAG_ECE|FLAG_DSACKING_ACK)#define FLAG_FORWARD_PROGRESS	(FLAG_ACKED|FLAG_DATA_SACKED) #define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)#define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH)) #define REXMIT_NONE	0 /* no loss recovery to do */#define REXMIT_LOST	1 /* retransmit packets marked lost */#define REXMIT_NEW	2 /* FRTO-style transmit of unsent/new packets */ #if IS_ENABLED(CONFIG_TLS_DEVICE)static DEFINE_STATIC_KEY_DEFERRED_FALSE(clean_acked_data_enabled, HZ); void clean_acked_data_enable(struct tcp_sock *tp,			     void (*cad)(struct sock *sk, u32 ack_seq)){	tp->tcp_clean_acked = cad;	static_branch_deferred_inc(&clean_acked_data_enabled);}EXPORT_SYMBOL_GPL(clean_acked_data_enable); void clean_acked_data_disable(struct tcp_sock *tp){	static_branch_slow_dec_deferred(&clean_acked_data_enabled);	tp->tcp_clean_acked = NULL;}EXPORT_SYMBOL_GPL(clean_acked_data_disable); void clean_acked_data_flush(void){	static_key_deferred_flush(&clean_acked_data_enabled);}EXPORT_SYMBOL_GPL(clean_acked_data_flush);#endif #ifdef CONFIG_CGROUP_BPFstatic void bpf_skops_parse_hdr(struct sock *sk, struct sk_buff *skb){	bool unknown_opt = tcp_sk(sk)->rx_opt.saw_unknown &&		BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk),				       BPF_SOCK_OPS_PARSE_UNKNOWN_HDR_OPT_CB_FLAG);	bool parse_all_opt = BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk),						    BPF_SOCK_OPS_PARSE_ALL_HDR_OPT_CB_FLAG);	struct bpf_sock_ops_kern sock_ops; 	if (likely(!unknown_opt && !parse_all_opt))		return; 	/* The skb will be handled in the	 * bpf_skops_established() or	 * bpf_skops_write_hdr_opt().	 */	switch (sk->sk_state) {	case TCP_SYN_RECV:	case TCP_SYN_SENT:	case TCP_LISTEN:		return;	} 	sock_owned_by_me(sk); 	memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));	sock_ops.op = BPF_SOCK_OPS_PARSE_HDR_OPT_CB;	sock_ops.is_fullsock = 1;	sock_ops.is_locked_tcp_sock = 1;	sock_ops.sk = sk;	bpf_skops_init_skb(&sock_ops, skb, tcp_hdrlen(skb)); 	BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);} static void bpf_skops_established(struct sock *sk, int bpf_op,				  struct sk_buff *skb){	struct bpf_sock_ops_kern sock_ops; 	sock_owned_by_me(sk); 	memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));	sock_ops.op = bpf_op;	sock_ops.is_fullsock = 1;	sock_ops.is_locked_tcp_sock = 1;	sock_ops.sk = sk;	/* sk with TCP_REPAIR_ON does not have skb in tcp_finish_connect */	if (skb)		bpf_skops_init_skb(&sock_ops, skb, tcp_hdrlen(skb)); 	BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);}#elsestatic void bpf_skops_parse_hdr(struct sock *sk, struct sk_buff *skb){} static void bpf_skops_established(struct sock *sk, int bpf_op,				  struct sk_buff *skb){}#endif static __cold void tcp_gro_dev_warn(const struct sock *sk, const struct sk_buff *skb,				    unsigned int len){	struct net_device *dev; 	rcu_read_lock();	dev = dev_get_by_index_rcu(sock_net(sk), skb->skb_iif);	if (!dev || len >= READ_ONCE(dev->mtu))		pr_warn("%s: Driver has suspect GRO implementation, TCP performance may be compromised.\n",			dev ? dev->name : "Unknown driver");	rcu_read_unlock();} /* Adapt the MSS value used to make delayed ack decision to the * real world. */static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb){	struct inet_connection_sock *icsk = inet_csk(sk);	const unsigned int lss = icsk->icsk_ack.last_seg_size;	unsigned int len; 	icsk->icsk_ack.last_seg_size = 0; 	/* skb->len may jitter because of SACKs, even if peer	 * sends good full-sized frames.	 */	len = skb_shinfo(skb)->gso_size ? : skb->len;	if (len >= icsk->icsk_ack.rcv_mss) {		/* Note: divides are still a bit expensive.		 * For the moment, only adjust scaling_ratio		 * when we update icsk_ack.rcv_mss.		 */		if (unlikely(len != icsk->icsk_ack.rcv_mss)) {			u64 val = (u64)skb->len << TCP_RMEM_TO_WIN_SCALE;			u8 old_ratio = tcp_sk(sk)->scaling_ratio; 			do_div(val, skb->truesize);			tcp_sk(sk)->scaling_ratio = val ? val : 1; 			if (old_ratio != tcp_sk(sk)->scaling_ratio) {				struct tcp_sock *tp = tcp_sk(sk); 				val = tcp_win_from_space(sk, sk->sk_rcvbuf);				WRITE_ONCE(tp->window_clamp, val); 				if (tp->window_clamp < tp->rcvq_space.space)					tp->rcvq_space.space = tp->window_clamp;			}		}		icsk->icsk_ack.rcv_mss = min_t(unsigned int, len,					       tcp_sk(sk)->advmss);		/* Account for possibly-removed options */		DO_ONCE_LITE_IF(len > icsk->icsk_ack.rcv_mss + MAX_TCP_OPTION_SPACE,				tcp_gro_dev_warn, sk, skb, len);		/* If the skb has a len of exactly 1*MSS and has the PSH bit		 * set then it is likely the end of an application write. So		 * more data may not be arriving soon, and yet the data sender		 * may be waiting for an ACK if cwnd-bound or using TX zero		 * copy. So we set ICSK_ACK_PUSHED here so that		 * tcp_cleanup_rbuf() will send an ACK immediately if the app		 * reads all of the data and is not ping-pong. If len > MSS		 * then this logic does not matter (and does not hurt) because		 * tcp_cleanup_rbuf() will always ACK immediately if the app		 * reads data and there is more than an MSS of unACKed data.		 */		if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_PSH)			icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;	} else {		/* Otherwise, we make more careful check taking into account,		 * that SACKs block is variable.		 *		 * "len" is invariant segment length, including TCP header.		 */		len += skb->data - skb_transport_header(skb);		if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) ||		    /* If PSH is not set, packet should be		     * full sized, provided peer TCP is not badly broken.		     * This observation (if it is correct 8)) allows		     * to handle super-low mtu links fairly.		     */		    (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&		     !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) {			/* Subtract also invariant (if peer is RFC compliant),			 * tcp header plus fixed timestamp option length.			 * Resulting "len" is MSS free of SACK jitter.			 */			len -= tcp_sk(sk)->tcp_header_len;			icsk->icsk_ack.last_seg_size = len;			if (len == lss) {				icsk->icsk_ack.rcv_mss = len;				return;			}		}		if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)			icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2;		icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;	}} static void tcp_incr_quickack(struct sock *sk, unsigned int max_quickacks){	struct inet_connection_sock *icsk = inet_csk(sk);	unsigned int quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss); 	if (quickacks == 0)		quickacks = 2;	quickacks = min(quickacks, max_quickacks);	if (quickacks > icsk->icsk_ack.quick)		icsk->icsk_ack.quick = quickacks;} static void tcp_enter_quickack_mode(struct sock *sk, unsigned int max_quickacks){	struct inet_connection_sock *icsk = inet_csk(sk); 	tcp_incr_quickack(sk, max_quickacks);	inet_csk_exit_pingpong_mode(sk);	icsk->icsk_ack.ato = TCP_ATO_MIN;} /* Send ACKs quickly, if "quick" count is not exhausted * and the session is not interactive. */ static bool tcp_in_quickack_mode(struct sock *sk){	const struct inet_connection_sock *icsk = inet_csk(sk); 	return icsk->icsk_ack.dst_quick_ack ||		(icsk->icsk_ack.quick && !inet_csk_in_pingpong_mode(sk));} static void tcp_data_ecn_check(struct sock *sk, const struct sk_buff *skb){	struct tcp_sock *tp = tcp_sk(sk); 	if (tcp_ecn_disabled(tp))		return; 	switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) {	case INET_ECN_NOT_ECT:		/* Funny extension: if ECT is not set on a segment,		 * and we already seen ECT on a previous segment,		 * it is probably a retransmit.		 */		if (tp->ecn_flags & TCP_ECN_SEEN)			tcp_enter_quickack_mode(sk, 2);		break;	case INET_ECN_CE:		if (tcp_ca_needs_ecn(sk))			tcp_ca_event(sk, CA_EVENT_ECN_IS_CE); 		if (!(tp->ecn_flags & TCP_ECN_DEMAND_CWR) &&		    tcp_ecn_mode_rfc3168(tp)) {			/* Better not delay acks, sender can have a very low cwnd */			tcp_enter_quickack_mode(sk, 2);			tp->ecn_flags |= TCP_ECN_DEMAND_CWR;		}		/* As for RFC3168 ECN, the TCP_ECN_SEEN flag is set by		 * tcp_data_ecn_check() when the ECN codepoint of		 * received TCP data contains ECT(0), ECT(1), or CE.		 */		if (!tcp_ecn_mode_rfc3168(tp))			break;		tp->ecn_flags |= TCP_ECN_SEEN;		break;	default:		if (tcp_ca_needs_ecn(sk))			tcp_ca_event(sk, CA_EVENT_ECN_NO_CE);		if (!tcp_ecn_mode_rfc3168(tp))			break;		tp->ecn_flags |= TCP_ECN_SEEN;		break;	}} /* Returns true if the byte counters can be used */static bool tcp_accecn_process_option(struct tcp_sock *tp,				      const struct sk_buff *skb,				      u32 delivered_bytes, int flag){	u8 estimate_ecnfield = tp->est_ecnfield;	bool ambiguous_ecn_bytes_incr = false;	bool first_changed = false;	unsigned int optlen;	bool order1, res;	unsigned int i;	u8 *ptr; 	if (tcp_accecn_opt_fail_recv(tp))		return false; 	if (!(flag & FLAG_SLOWPATH) || !tp->rx_opt.accecn) {		if (!tp->saw_accecn_opt) {			/* Too late to enable after this point due to			 * potential counter wraps			 */			if (tp->bytes_sent >= (1 << 23) - 1) {				u8 saw_opt = TCP_ACCECN_OPT_FAIL_SEEN; 				tcp_accecn_saw_opt_fail_recv(tp, saw_opt);			}			return false;		} 		if (estimate_ecnfield) {			u8 ecnfield = estimate_ecnfield - 1; 			tp->delivered_ecn_bytes[ecnfield] += delivered_bytes;			return true;		}		return false;	} 	ptr = skb_transport_header(skb) + tp->rx_opt.accecn;	optlen = ptr[1] - 2;	if (WARN_ON_ONCE(ptr[0] != TCPOPT_ACCECN0 && ptr[0] != TCPOPT_ACCECN1))		return false;	order1 = (ptr[0] == TCPOPT_ACCECN1);	ptr += 2; 	if (tp->saw_accecn_opt < TCP_ACCECN_OPT_COUNTER_SEEN) {		tp->saw_accecn_opt = tcp_accecn_option_init(skb,							    tp->rx_opt.accecn);		if (tp->saw_accecn_opt == TCP_ACCECN_OPT_FAIL_SEEN)			tcp_accecn_fail_mode_set(tp, TCP_ACCECN_OPT_FAIL_RECV);	} 	res = !!estimate_ecnfield;	for (i = 0; i < 3; i++) {		u32 init_offset;		u8 ecnfield;		s32 delta;		u32 *cnt; 		if (optlen < TCPOLEN_ACCECN_PERFIELD)			break; 		ecnfield = tcp_accecn_optfield_to_ecnfield(i, order1);		init_offset = tcp_accecn_field_init_offset(ecnfield);		cnt = &tp->delivered_ecn_bytes[ecnfield - 1];		delta = tcp_update_ecn_bytes(cnt, ptr, init_offset);		if (delta && delta < 0) {			res = false;			ambiguous_ecn_bytes_incr = true;		}		if (delta && ecnfield != estimate_ecnfield) {			if (!first_changed) {				tp->est_ecnfield = ecnfield;				first_changed = true;			} else {				res = false;				ambiguous_ecn_bytes_incr = true;			}		} 		optlen -= TCPOLEN_ACCECN_PERFIELD;		ptr += TCPOLEN_ACCECN_PERFIELD;	}	if (ambiguous_ecn_bytes_incr)		tp->est_ecnfield = 0; 	return res;} static void tcp_count_delivered_ce(struct tcp_sock *tp, u32 ecn_count){	WRITE_ONCE(tp->delivered_ce, tp->delivered_ce + ecn_count);} /* Updates the delivered and delivered_ce counts */static void tcp_count_delivered(struct tcp_sock *tp, u32 delivered,				bool ece_ack){	WRITE_ONCE(tp->delivered, tp->delivered + delivered);	if (tcp_ecn_mode_rfc3168(tp) && ece_ack)		tcp_count_delivered_ce(tp, delivered);} #define PKTS_ACKED_WEIGHT	6#define PKTS_ACKED_PREC		6#define ACK_COMP_THRESH		4 /* Returns the ECN CE delta */static u32 __tcp_accecn_process(struct sock *sk, const struct sk_buff *skb,				u32 delivered_pkts, u32 delivered_bytes,				int flag){	u32 old_ceb = tcp_sk(sk)->delivered_ecn_bytes[INET_ECN_CE - 1];

Lines 1–500 of 7805.