xref: /linux/net/ipv4/tcp_output.c (revision 84d3e7b9573291a1ea845bdd51b74bb484597661)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Implementation of the Transmission Control Protocol(TCP).
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Version:	$Id: tcp_output.c,v 1.146 2002/02/01 22:01:04 davem Exp $
91da177e4SLinus Torvalds  *
1002c30a84SJesper Juhl  * Authors:	Ross Biro
111da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
121da177e4SLinus Torvalds  *		Mark Evans, <evansmp@uhura.aston.ac.uk>
131da177e4SLinus Torvalds  *		Corey Minyard <wf-rch!minyard@relay.EU.net>
141da177e4SLinus Torvalds  *		Florian La Roche, <flla@stud.uni-sb.de>
151da177e4SLinus Torvalds  *		Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
161da177e4SLinus Torvalds  *		Linus Torvalds, <torvalds@cs.helsinki.fi>
171da177e4SLinus Torvalds  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
181da177e4SLinus Torvalds  *		Matthew Dillon, <dillon@apollo.west.oic.com>
191da177e4SLinus Torvalds  *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
201da177e4SLinus Torvalds  *		Jorge Cwik, <jorge@laser.satlink.net>
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds /*
241da177e4SLinus Torvalds  * Changes:	Pedro Roque	:	Retransmit queue handled by TCP.
251da177e4SLinus Torvalds  *				:	Fragmentation on mtu decrease
261da177e4SLinus Torvalds  *				:	Segment collapse on retransmit
271da177e4SLinus Torvalds  *				:	AF independence
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  *		Linus Torvalds	:	send_delayed_ack
301da177e4SLinus Torvalds  *		David S. Miller	:	Charge memory using the right skb
311da177e4SLinus Torvalds  *					during syn/ack processing.
321da177e4SLinus Torvalds  *		David S. Miller :	Output engine completely rewritten.
331da177e4SLinus Torvalds  *		Andrea Arcangeli:	SYNACK carry ts_recent in tsecr.
341da177e4SLinus Torvalds  *		Cacophonix Gaul :	draft-minshall-nagle-01
351da177e4SLinus Torvalds  *		J Hadi Salim	:	ECN support
361da177e4SLinus Torvalds  *
371da177e4SLinus Torvalds  */
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <net/tcp.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <linux/compiler.h>
421da177e4SLinus Torvalds #include <linux/module.h>
431da177e4SLinus Torvalds #include <linux/smp_lock.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /* People can turn this off for buggy TCP's found in printers etc. */
461da177e4SLinus Torvalds int sysctl_tcp_retrans_collapse = 1;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /* This limits the percentage of the congestion window which we
491da177e4SLinus Torvalds  * will allow a single TSO frame to consume.  Building TSO frames
501da177e4SLinus Torvalds  * which are too large can cause TCP streams to be bursty.
511da177e4SLinus Torvalds  */
521da177e4SLinus Torvalds int sysctl_tcp_tso_win_divisor = 8;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds static inline void update_send_head(struct sock *sk, struct tcp_sock *tp,
551da177e4SLinus Torvalds 				    struct sk_buff *skb)
561da177e4SLinus Torvalds {
571da177e4SLinus Torvalds 	sk->sk_send_head = skb->next;
581da177e4SLinus Torvalds 	if (sk->sk_send_head == (struct sk_buff *)&sk->sk_write_queue)
591da177e4SLinus Torvalds 		sk->sk_send_head = NULL;
601da177e4SLinus Torvalds 	tp->snd_nxt = TCP_SKB_CB(skb)->end_seq;
611da177e4SLinus Torvalds 	tcp_packets_out_inc(sk, tp, skb);
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /* SND.NXT, if window was not shrunk.
651da177e4SLinus Torvalds  * If window has been shrunk, what should we make? It is not clear at all.
661da177e4SLinus Torvalds  * Using SND.UNA we will fail to open window, SND.NXT is out of window. :-(
671da177e4SLinus Torvalds  * Anything in between SND.UNA...SND.UNA+SND.WND also can be already
681da177e4SLinus Torvalds  * invalid. OK, let's make this for now:
691da177e4SLinus Torvalds  */
701da177e4SLinus Torvalds static inline __u32 tcp_acceptable_seq(struct sock *sk, struct tcp_sock *tp)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	if (!before(tp->snd_una+tp->snd_wnd, tp->snd_nxt))
731da177e4SLinus Torvalds 		return tp->snd_nxt;
741da177e4SLinus Torvalds 	else
751da177e4SLinus Torvalds 		return tp->snd_una+tp->snd_wnd;
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds /* Calculate mss to advertise in SYN segment.
791da177e4SLinus Torvalds  * RFC1122, RFC1063, draft-ietf-tcpimpl-pmtud-01 state that:
801da177e4SLinus Torvalds  *
811da177e4SLinus Torvalds  * 1. It is independent of path mtu.
821da177e4SLinus Torvalds  * 2. Ideally, it is maximal possible segment size i.e. 65535-40.
831da177e4SLinus Torvalds  * 3. For IPv4 it is reasonable to calculate it from maximal MTU of
841da177e4SLinus Torvalds  *    attached devices, because some buggy hosts are confused by
851da177e4SLinus Torvalds  *    large MSS.
861da177e4SLinus Torvalds  * 4. We do not make 3, we advertise MSS, calculated from first
871da177e4SLinus Torvalds  *    hop device mtu, but allow to raise it to ip_rt_min_advmss.
881da177e4SLinus Torvalds  *    This may be overridden via information stored in routing table.
891da177e4SLinus Torvalds  * 5. Value 65535 for MSS is valid in IPv6 and means "as large as possible,
901da177e4SLinus Torvalds  *    probably even Jumbo".
911da177e4SLinus Torvalds  */
921da177e4SLinus Torvalds static __u16 tcp_advertise_mss(struct sock *sk)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
951da177e4SLinus Torvalds 	struct dst_entry *dst = __sk_dst_get(sk);
961da177e4SLinus Torvalds 	int mss = tp->advmss;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	if (dst && dst_metric(dst, RTAX_ADVMSS) < mss) {
991da177e4SLinus Torvalds 		mss = dst_metric(dst, RTAX_ADVMSS);
1001da177e4SLinus Torvalds 		tp->advmss = mss;
1011da177e4SLinus Torvalds 	}
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	return (__u16)mss;
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /* RFC2861. Reset CWND after idle period longer RTO to "restart window".
1071da177e4SLinus Torvalds  * This is the first part of cwnd validation mechanism. */
1081da177e4SLinus Torvalds static void tcp_cwnd_restart(struct tcp_sock *tp, struct dst_entry *dst)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	s32 delta = tcp_time_stamp - tp->lsndtime;
1111da177e4SLinus Torvalds 	u32 restart_cwnd = tcp_init_cwnd(tp, dst);
1121da177e4SLinus Torvalds 	u32 cwnd = tp->snd_cwnd;
1131da177e4SLinus Torvalds 
114317a76f9SStephen Hemminger 	tcp_ca_event(tp, CA_EVENT_CWND_RESTART);
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	tp->snd_ssthresh = tcp_current_ssthresh(tp);
1171da177e4SLinus Torvalds 	restart_cwnd = min(restart_cwnd, cwnd);
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	while ((delta -= tp->rto) > 0 && cwnd > restart_cwnd)
1201da177e4SLinus Torvalds 		cwnd >>= 1;
1211da177e4SLinus Torvalds 	tp->snd_cwnd = max(cwnd, restart_cwnd);
1221da177e4SLinus Torvalds 	tp->snd_cwnd_stamp = tcp_time_stamp;
1231da177e4SLinus Torvalds 	tp->snd_cwnd_used = 0;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds static inline void tcp_event_data_sent(struct tcp_sock *tp,
1271da177e4SLinus Torvalds 				       struct sk_buff *skb, struct sock *sk)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	u32 now = tcp_time_stamp;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	if (!tp->packets_out && (s32)(now - tp->lsndtime) > tp->rto)
1321da177e4SLinus Torvalds 		tcp_cwnd_restart(tp, __sk_dst_get(sk));
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	tp->lsndtime = now;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	/* If it is a reply for ato after last received
1371da177e4SLinus Torvalds 	 * packet, enter pingpong mode.
1381da177e4SLinus Torvalds 	 */
1391da177e4SLinus Torvalds 	if ((u32)(now - tp->ack.lrcvtime) < tp->ack.ato)
1401da177e4SLinus Torvalds 		tp->ack.pingpong = 1;
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
143fc6415bcSDavid S. Miller static __inline__ void tcp_event_ack_sent(struct sock *sk, unsigned int pkts)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
1461da177e4SLinus Torvalds 
147fc6415bcSDavid S. Miller 	tcp_dec_quickack_mode(tp, pkts);
1481da177e4SLinus Torvalds 	tcp_clear_xmit_timer(sk, TCP_TIME_DACK);
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer.
1521da177e4SLinus Torvalds  * Based on the assumption that the given amount of space
1531da177e4SLinus Torvalds  * will be offered. Store the results in the tp structure.
1541da177e4SLinus Torvalds  * NOTE: for smooth operation initial space offering should
1551da177e4SLinus Torvalds  * be a multiple of mss if possible. We assume here that mss >= 1.
1561da177e4SLinus Torvalds  * This MUST be enforced by all callers.
1571da177e4SLinus Torvalds  */
1581da177e4SLinus Torvalds void tcp_select_initial_window(int __space, __u32 mss,
1591da177e4SLinus Torvalds 			       __u32 *rcv_wnd, __u32 *window_clamp,
1601da177e4SLinus Torvalds 			       int wscale_ok, __u8 *rcv_wscale)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	unsigned int space = (__space < 0 ? 0 : __space);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	/* If no clamp set the clamp to the max possible scaled window */
1651da177e4SLinus Torvalds 	if (*window_clamp == 0)
1661da177e4SLinus Torvalds 		(*window_clamp) = (65535 << 14);
1671da177e4SLinus Torvalds 	space = min(*window_clamp, space);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	/* Quantize space offering to a multiple of mss if possible. */
1701da177e4SLinus Torvalds 	if (space > mss)
1711da177e4SLinus Torvalds 		space = (space / mss) * mss;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	/* NOTE: offering an initial window larger than 32767
1741da177e4SLinus Torvalds 	 * will break some buggy TCP stacks. We try to be nice.
1751da177e4SLinus Torvalds 	 * If we are not window scaling, then this truncates
1761da177e4SLinus Torvalds 	 * our initial window offering to 32k. There should also
1771da177e4SLinus Torvalds 	 * be a sysctl option to stop being nice.
1781da177e4SLinus Torvalds 	 */
1791da177e4SLinus Torvalds 	(*rcv_wnd) = min(space, MAX_TCP_WINDOW);
1801da177e4SLinus Torvalds 	(*rcv_wscale) = 0;
1811da177e4SLinus Torvalds 	if (wscale_ok) {
1821da177e4SLinus Torvalds 		/* Set window scaling on max possible window
1831da177e4SLinus Torvalds 		 * See RFC1323 for an explanation of the limit to 14
1841da177e4SLinus Torvalds 		 */
1851da177e4SLinus Torvalds 		space = max_t(u32, sysctl_tcp_rmem[2], sysctl_rmem_max);
1861da177e4SLinus Torvalds 		while (space > 65535 && (*rcv_wscale) < 14) {
1871da177e4SLinus Torvalds 			space >>= 1;
1881da177e4SLinus Torvalds 			(*rcv_wscale)++;
1891da177e4SLinus Torvalds 		}
1901da177e4SLinus Torvalds 	}
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	/* Set initial window to value enough for senders,
1931da177e4SLinus Torvalds 	 * following RFC1414. Senders, not following this RFC,
1941da177e4SLinus Torvalds 	 * will be satisfied with 2.
1951da177e4SLinus Torvalds 	 */
1961da177e4SLinus Torvalds 	if (mss > (1<<*rcv_wscale)) {
1971da177e4SLinus Torvalds 		int init_cwnd = 4;
1981da177e4SLinus Torvalds 		if (mss > 1460*3)
1991da177e4SLinus Torvalds 			init_cwnd = 2;
2001da177e4SLinus Torvalds 		else if (mss > 1460)
2011da177e4SLinus Torvalds 			init_cwnd = 3;
2021da177e4SLinus Torvalds 		if (*rcv_wnd > init_cwnd*mss)
2031da177e4SLinus Torvalds 			*rcv_wnd = init_cwnd*mss;
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	/* Set the clamp no higher than max representable value */
2071da177e4SLinus Torvalds 	(*window_clamp) = min(65535U << (*rcv_wscale), *window_clamp);
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds /* Chose a new window to advertise, update state in tcp_sock for the
2111da177e4SLinus Torvalds  * socket, and return result with RFC1323 scaling applied.  The return
2121da177e4SLinus Torvalds  * value can be stuffed directly into th->window for an outgoing
2131da177e4SLinus Torvalds  * frame.
2141da177e4SLinus Torvalds  */
2151da177e4SLinus Torvalds static __inline__ u16 tcp_select_window(struct sock *sk)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
2181da177e4SLinus Torvalds 	u32 cur_win = tcp_receive_window(tp);
2191da177e4SLinus Torvalds 	u32 new_win = __tcp_select_window(sk);
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	/* Never shrink the offered window */
2221da177e4SLinus Torvalds 	if(new_win < cur_win) {
2231da177e4SLinus Torvalds 		/* Danger Will Robinson!
2241da177e4SLinus Torvalds 		 * Don't update rcv_wup/rcv_wnd here or else
2251da177e4SLinus Torvalds 		 * we will not be able to advertise a zero
2261da177e4SLinus Torvalds 		 * window in time.  --DaveM
2271da177e4SLinus Torvalds 		 *
2281da177e4SLinus Torvalds 		 * Relax Will Robinson.
2291da177e4SLinus Torvalds 		 */
2301da177e4SLinus Torvalds 		new_win = cur_win;
2311da177e4SLinus Torvalds 	}
2321da177e4SLinus Torvalds 	tp->rcv_wnd = new_win;
2331da177e4SLinus Torvalds 	tp->rcv_wup = tp->rcv_nxt;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	/* Make sure we do not exceed the maximum possible
2361da177e4SLinus Torvalds 	 * scaled window.
2371da177e4SLinus Torvalds 	 */
2381da177e4SLinus Torvalds 	if (!tp->rx_opt.rcv_wscale)
2391da177e4SLinus Torvalds 		new_win = min(new_win, MAX_TCP_WINDOW);
2401da177e4SLinus Torvalds 	else
2411da177e4SLinus Torvalds 		new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	/* RFC1323 scaling applied */
2441da177e4SLinus Torvalds 	new_win >>= tp->rx_opt.rcv_wscale;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	/* If we advertise zero window, disable fast path. */
2471da177e4SLinus Torvalds 	if (new_win == 0)
2481da177e4SLinus Torvalds 		tp->pred_flags = 0;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	return new_win;
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds /* This routine actually transmits TCP packets queued in by
2551da177e4SLinus Torvalds  * tcp_do_sendmsg().  This is used by both the initial
2561da177e4SLinus Torvalds  * transmission and possible later retransmissions.
2571da177e4SLinus Torvalds  * All SKB's seen here are completely headerless.  It is our
2581da177e4SLinus Torvalds  * job to build the TCP header, and pass the packet down to
2591da177e4SLinus Torvalds  * IP so it can do the same plus pass the packet off to the
2601da177e4SLinus Torvalds  * device.
2611da177e4SLinus Torvalds  *
2621da177e4SLinus Torvalds  * We are working here with either a clone of the original
2631da177e4SLinus Torvalds  * SKB, or a fresh unique copy made by the retransmit engine.
2641da177e4SLinus Torvalds  */
2651da177e4SLinus Torvalds static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb)
2661da177e4SLinus Torvalds {
2671da177e4SLinus Torvalds 	if (skb != NULL) {
2681da177e4SLinus Torvalds 		struct inet_sock *inet = inet_sk(sk);
2691da177e4SLinus Torvalds 		struct tcp_sock *tp = tcp_sk(sk);
2701da177e4SLinus Torvalds 		struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2711da177e4SLinus Torvalds 		int tcp_header_size = tp->tcp_header_len;
2721da177e4SLinus Torvalds 		struct tcphdr *th;
2731da177e4SLinus Torvalds 		int sysctl_flags;
2741da177e4SLinus Torvalds 		int err;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 		BUG_ON(!tcp_skb_pcount(skb));
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds #define SYSCTL_FLAG_TSTAMPS	0x1
2791da177e4SLinus Torvalds #define SYSCTL_FLAG_WSCALE	0x2
2801da177e4SLinus Torvalds #define SYSCTL_FLAG_SACK	0x4
2811da177e4SLinus Torvalds 
282317a76f9SStephen Hemminger 		/* If congestion control is doing timestamping */
283317a76f9SStephen Hemminger 		if (tp->ca_ops->rtt_sample)
284317a76f9SStephen Hemminger 			do_gettimeofday(&skb->stamp);
285317a76f9SStephen Hemminger 
2861da177e4SLinus Torvalds 		sysctl_flags = 0;
2871da177e4SLinus Torvalds 		if (tcb->flags & TCPCB_FLAG_SYN) {
2881da177e4SLinus Torvalds 			tcp_header_size = sizeof(struct tcphdr) + TCPOLEN_MSS;
2891da177e4SLinus Torvalds 			if(sysctl_tcp_timestamps) {
2901da177e4SLinus Torvalds 				tcp_header_size += TCPOLEN_TSTAMP_ALIGNED;
2911da177e4SLinus Torvalds 				sysctl_flags |= SYSCTL_FLAG_TSTAMPS;
2921da177e4SLinus Torvalds 			}
2931da177e4SLinus Torvalds 			if(sysctl_tcp_window_scaling) {
2941da177e4SLinus Torvalds 				tcp_header_size += TCPOLEN_WSCALE_ALIGNED;
2951da177e4SLinus Torvalds 				sysctl_flags |= SYSCTL_FLAG_WSCALE;
2961da177e4SLinus Torvalds 			}
2971da177e4SLinus Torvalds 			if(sysctl_tcp_sack) {
2981da177e4SLinus Torvalds 				sysctl_flags |= SYSCTL_FLAG_SACK;
2991da177e4SLinus Torvalds 				if(!(sysctl_flags & SYSCTL_FLAG_TSTAMPS))
3001da177e4SLinus Torvalds 					tcp_header_size += TCPOLEN_SACKPERM_ALIGNED;
3011da177e4SLinus Torvalds 			}
3021da177e4SLinus Torvalds 		} else if (tp->rx_opt.eff_sacks) {
3031da177e4SLinus Torvalds 			/* A SACK is 2 pad bytes, a 2 byte header, plus
3041da177e4SLinus Torvalds 			 * 2 32-bit sequence numbers for each SACK block.
3051da177e4SLinus Torvalds 			 */
3061da177e4SLinus Torvalds 			tcp_header_size += (TCPOLEN_SACK_BASE_ALIGNED +
3071da177e4SLinus Torvalds 					    (tp->rx_opt.eff_sacks * TCPOLEN_SACK_PERBLOCK));
3081da177e4SLinus Torvalds 		}
3091da177e4SLinus Torvalds 
310317a76f9SStephen Hemminger 		if (tcp_packets_in_flight(tp) == 0)
311317a76f9SStephen Hemminger 			tcp_ca_event(tp, CA_EVENT_TX_START);
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 		th = (struct tcphdr *) skb_push(skb, tcp_header_size);
3141da177e4SLinus Torvalds 		skb->h.th = th;
3151da177e4SLinus Torvalds 		skb_set_owner_w(skb, sk);
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 		/* Build TCP header and checksum it. */
3181da177e4SLinus Torvalds 		th->source		= inet->sport;
3191da177e4SLinus Torvalds 		th->dest		= inet->dport;
3201da177e4SLinus Torvalds 		th->seq			= htonl(tcb->seq);
3211da177e4SLinus Torvalds 		th->ack_seq		= htonl(tp->rcv_nxt);
3221da177e4SLinus Torvalds 		*(((__u16 *)th) + 6)	= htons(((tcp_header_size >> 2) << 12) | tcb->flags);
3231da177e4SLinus Torvalds 		if (tcb->flags & TCPCB_FLAG_SYN) {
3241da177e4SLinus Torvalds 			/* RFC1323: The window in SYN & SYN/ACK segments
3251da177e4SLinus Torvalds 			 * is never scaled.
3261da177e4SLinus Torvalds 			 */
3271da177e4SLinus Torvalds 			th->window	= htons(tp->rcv_wnd);
3281da177e4SLinus Torvalds 		} else {
3291da177e4SLinus Torvalds 			th->window	= htons(tcp_select_window(sk));
3301da177e4SLinus Torvalds 		}
3311da177e4SLinus Torvalds 		th->check		= 0;
3321da177e4SLinus Torvalds 		th->urg_ptr		= 0;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 		if (tp->urg_mode &&
3351da177e4SLinus Torvalds 		    between(tp->snd_up, tcb->seq+1, tcb->seq+0xFFFF)) {
3361da177e4SLinus Torvalds 			th->urg_ptr		= htons(tp->snd_up-tcb->seq);
3371da177e4SLinus Torvalds 			th->urg			= 1;
3381da177e4SLinus Torvalds 		}
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 		if (tcb->flags & TCPCB_FLAG_SYN) {
3411da177e4SLinus Torvalds 			tcp_syn_build_options((__u32 *)(th + 1),
3421da177e4SLinus Torvalds 					      tcp_advertise_mss(sk),
3431da177e4SLinus Torvalds 					      (sysctl_flags & SYSCTL_FLAG_TSTAMPS),
3441da177e4SLinus Torvalds 					      (sysctl_flags & SYSCTL_FLAG_SACK),
3451da177e4SLinus Torvalds 					      (sysctl_flags & SYSCTL_FLAG_WSCALE),
3461da177e4SLinus Torvalds 					      tp->rx_opt.rcv_wscale,
3471da177e4SLinus Torvalds 					      tcb->when,
3481da177e4SLinus Torvalds 		      			      tp->rx_opt.ts_recent);
3491da177e4SLinus Torvalds 		} else {
3501da177e4SLinus Torvalds 			tcp_build_and_update_options((__u32 *)(th + 1),
3511da177e4SLinus Torvalds 						     tp, tcb->when);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 			TCP_ECN_send(sk, tp, skb, tcp_header_size);
3541da177e4SLinus Torvalds 		}
3551da177e4SLinus Torvalds 		tp->af_specific->send_check(sk, th, skb->len, skb);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 		if (tcb->flags & TCPCB_FLAG_ACK)
358fc6415bcSDavid S. Miller 			tcp_event_ack_sent(sk, tcp_skb_pcount(skb));
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 		if (skb->len != tcp_header_size)
3611da177e4SLinus Torvalds 			tcp_event_data_sent(tp, skb, sk);
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 		TCP_INC_STATS(TCP_MIB_OUTSEGS);
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 		err = tp->af_specific->queue_xmit(skb, 0);
3661da177e4SLinus Torvalds 		if (err <= 0)
3671da177e4SLinus Torvalds 			return err;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 		tcp_enter_cwr(tp);
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 		/* NET_XMIT_CN is special. It does not guarantee,
3721da177e4SLinus Torvalds 		 * that this packet is lost. It tells that device
3731da177e4SLinus Torvalds 		 * is about to start to drop packets or already
3741da177e4SLinus Torvalds 		 * drops some packets of the same priority and
3751da177e4SLinus Torvalds 		 * invokes us to send less aggressively.
3761da177e4SLinus Torvalds 		 */
3771da177e4SLinus Torvalds 		return err == NET_XMIT_CN ? 0 : err;
3781da177e4SLinus Torvalds 	}
3791da177e4SLinus Torvalds 	return -ENOBUFS;
3801da177e4SLinus Torvalds #undef SYSCTL_FLAG_TSTAMPS
3811da177e4SLinus Torvalds #undef SYSCTL_FLAG_WSCALE
3821da177e4SLinus Torvalds #undef SYSCTL_FLAG_SACK
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds /* This routine just queue's the buffer
3871da177e4SLinus Torvalds  *
3881da177e4SLinus Torvalds  * NOTE: probe0 timer is not checked, do not forget tcp_push_pending_frames,
3891da177e4SLinus Torvalds  * otherwise socket can stall.
3901da177e4SLinus Torvalds  */
3911da177e4SLinus Torvalds static void tcp_queue_skb(struct sock *sk, struct sk_buff *skb)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	/* Advance write_seq and place onto the write_queue. */
3961da177e4SLinus Torvalds 	tp->write_seq = TCP_SKB_CB(skb)->end_seq;
3971da177e4SLinus Torvalds 	skb_header_release(skb);
3981da177e4SLinus Torvalds 	__skb_queue_tail(&sk->sk_write_queue, skb);
3991da177e4SLinus Torvalds 	sk_charge_skb(sk, skb);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	/* Queue it, remembering where we must start sending. */
4021da177e4SLinus Torvalds 	if (sk->sk_send_head == NULL)
4031da177e4SLinus Torvalds 		sk->sk_send_head = skb;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds static inline void tcp_tso_set_push(struct sk_buff *skb)
4071da177e4SLinus Torvalds {
4081da177e4SLinus Torvalds 	/* Force push to be on for any TSO frames to workaround
4091da177e4SLinus Torvalds 	 * problems with busted implementations like Mac OS-X that
4101da177e4SLinus Torvalds 	 * hold off socket receive wakeups until push is seen.
4111da177e4SLinus Torvalds 	 */
4121da177e4SLinus Torvalds 	if (tcp_skb_pcount(skb) > 1)
4131da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
416f6302d1dSDavid S. Miller static void tcp_set_skb_tso_segs(struct sock *sk, struct sk_buff *skb)
417f6302d1dSDavid S. Miller {
418f6302d1dSDavid S. Miller 	struct tcp_sock *tp = tcp_sk(sk);
419f6302d1dSDavid S. Miller 
420f6302d1dSDavid S. Miller 	if (skb->len <= tp->mss_cache_std ||
421f6302d1dSDavid S. Miller 	    !(sk->sk_route_caps & NETIF_F_TSO)) {
422f6302d1dSDavid S. Miller 		/* Avoid the costly divide in the normal
423f6302d1dSDavid S. Miller 		 * non-TSO case.
424f6302d1dSDavid S. Miller 		 */
425f6302d1dSDavid S. Miller 		skb_shinfo(skb)->tso_segs = 1;
426f6302d1dSDavid S. Miller 		skb_shinfo(skb)->tso_size = 0;
427f6302d1dSDavid S. Miller 	} else {
428f6302d1dSDavid S. Miller 		unsigned int factor;
429f6302d1dSDavid S. Miller 
430f6302d1dSDavid S. Miller 		factor = skb->len + (tp->mss_cache_std - 1);
431f6302d1dSDavid S. Miller 		factor /= tp->mss_cache_std;
432f6302d1dSDavid S. Miller 		skb_shinfo(skb)->tso_segs = factor;
433f6302d1dSDavid S. Miller 		skb_shinfo(skb)->tso_size = tp->mss_cache_std;
434f6302d1dSDavid S. Miller 	}
435f6302d1dSDavid S. Miller }
436f6302d1dSDavid S. Miller 
437f6302d1dSDavid S. Miller static inline int tcp_minshall_check(const struct tcp_sock *tp)
438f6302d1dSDavid S. Miller {
439f6302d1dSDavid S. Miller 	return after(tp->snd_sml,tp->snd_una) &&
440f6302d1dSDavid S. Miller 		!after(tp->snd_sml, tp->snd_nxt);
441f6302d1dSDavid S. Miller }
442f6302d1dSDavid S. Miller 
443f6302d1dSDavid S. Miller /* Return 0, if packet can be sent now without violation Nagle's rules:
444f6302d1dSDavid S. Miller  * 1. It is full sized.
445f6302d1dSDavid S. Miller  * 2. Or it contains FIN.
446f6302d1dSDavid S. Miller  * 3. Or TCP_NODELAY was set.
447f6302d1dSDavid S. Miller  * 4. Or TCP_CORK is not set, and all sent packets are ACKed.
448f6302d1dSDavid S. Miller  *    With Minshall's modification: all sent small packets are ACKed.
449f6302d1dSDavid S. Miller  */
450f6302d1dSDavid S. Miller 
451f6302d1dSDavid S. Miller static inline int tcp_nagle_check(const struct tcp_sock *tp,
452f6302d1dSDavid S. Miller 				  const struct sk_buff *skb,
453f6302d1dSDavid S. Miller 				  unsigned mss_now, int nonagle)
454f6302d1dSDavid S. Miller {
455f6302d1dSDavid S. Miller 	return (skb->len < mss_now &&
456f6302d1dSDavid S. Miller 		!(TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN) &&
457f6302d1dSDavid S. Miller 		((nonagle&TCP_NAGLE_CORK) ||
458f6302d1dSDavid S. Miller 		 (!nonagle &&
459f6302d1dSDavid S. Miller 		  tp->packets_out &&
460f6302d1dSDavid S. Miller 		  tcp_minshall_check(tp))));
461f6302d1dSDavid S. Miller }
462f6302d1dSDavid S. Miller 
463f6302d1dSDavid S. Miller /* This checks if the data bearing packet SKB (usually sk->sk_send_head)
464f6302d1dSDavid S. Miller  * should be put on the wire right now.
465f6302d1dSDavid S. Miller  */
466f6302d1dSDavid S. Miller static int tcp_snd_test(struct sock *sk, struct sk_buff *skb,
467f6302d1dSDavid S. Miller 			unsigned cur_mss, int nonagle)
468f6302d1dSDavid S. Miller {
469f6302d1dSDavid S. Miller 	struct tcp_sock *tp = tcp_sk(sk);
470f6302d1dSDavid S. Miller 	int pkts = tcp_skb_pcount(skb);
471f6302d1dSDavid S. Miller 
472f6302d1dSDavid S. Miller 	if (!pkts) {
473f6302d1dSDavid S. Miller 		tcp_set_skb_tso_segs(sk, skb);
474f6302d1dSDavid S. Miller 		pkts = tcp_skb_pcount(skb);
475f6302d1dSDavid S. Miller 	}
476f6302d1dSDavid S. Miller 
477f6302d1dSDavid S. Miller 	/*	RFC 1122 - section 4.2.3.4
478f6302d1dSDavid S. Miller 	 *
479f6302d1dSDavid S. Miller 	 *	We must queue if
480f6302d1dSDavid S. Miller 	 *
481f6302d1dSDavid S. Miller 	 *	a) The right edge of this frame exceeds the window
482f6302d1dSDavid S. Miller 	 *	b) There are packets in flight and we have a small segment
483f6302d1dSDavid S. Miller 	 *	   [SWS avoidance and Nagle algorithm]
484f6302d1dSDavid S. Miller 	 *	   (part of SWS is done on packetization)
485f6302d1dSDavid S. Miller 	 *	   Minshall version sounds: there are no _small_
486f6302d1dSDavid S. Miller 	 *	   segments in flight. (tcp_nagle_check)
487f6302d1dSDavid S. Miller 	 *	c) We have too many packets 'in flight'
488f6302d1dSDavid S. Miller 	 *
489f6302d1dSDavid S. Miller 	 * 	Don't use the nagle rule for urgent data (or
490f6302d1dSDavid S. Miller 	 *	for the final FIN -DaveM).
491f6302d1dSDavid S. Miller 	 *
492f6302d1dSDavid S. Miller 	 *	Also, Nagle rule does not apply to frames, which
493f6302d1dSDavid S. Miller 	 *	sit in the middle of queue (they have no chances
494f6302d1dSDavid S. Miller 	 *	to get new data) and if room at tail of skb is
495f6302d1dSDavid S. Miller 	 *	not enough to save something seriously (<32 for now).
496f6302d1dSDavid S. Miller 	 */
497f6302d1dSDavid S. Miller 
498f6302d1dSDavid S. Miller 	/* Don't be strict about the congestion window for the
499f6302d1dSDavid S. Miller 	 * final FIN frame.  -DaveM
500f6302d1dSDavid S. Miller 	 */
501f6302d1dSDavid S. Miller 	return (((nonagle&TCP_NAGLE_PUSH) || tp->urg_mode
502f6302d1dSDavid S. Miller 		 || !tcp_nagle_check(tp, skb, cur_mss, nonagle)) &&
503f6302d1dSDavid S. Miller 		(((tcp_packets_in_flight(tp) + (pkts-1)) < tp->snd_cwnd) ||
504f6302d1dSDavid S. Miller 		 (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN)) &&
505f6302d1dSDavid S. Miller 		!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una + tp->snd_wnd));
506f6302d1dSDavid S. Miller }
507f6302d1dSDavid S. Miller 
508f6302d1dSDavid S. Miller static inline int tcp_skb_is_last(const struct sock *sk,
509f6302d1dSDavid S. Miller 				  const struct sk_buff *skb)
510f6302d1dSDavid S. Miller {
511f6302d1dSDavid S. Miller 	return skb->next == (struct sk_buff *)&sk->sk_write_queue;
512f6302d1dSDavid S. Miller }
513f6302d1dSDavid S. Miller 
514f6302d1dSDavid S. Miller /* Push out any pending frames which were held back due to
515f6302d1dSDavid S. Miller  * TCP_CORK or attempt at coalescing tiny packets.
516f6302d1dSDavid S. Miller  * The socket must be locked by the caller.
517f6302d1dSDavid S. Miller  */
518f6302d1dSDavid S. Miller void __tcp_push_pending_frames(struct sock *sk, struct tcp_sock *tp,
519f6302d1dSDavid S. Miller 			       unsigned cur_mss, int nonagle)
520f6302d1dSDavid S. Miller {
521f6302d1dSDavid S. Miller 	struct sk_buff *skb = sk->sk_send_head;
522f6302d1dSDavid S. Miller 
523f6302d1dSDavid S. Miller 	if (skb) {
524f6302d1dSDavid S. Miller 		if (!tcp_skb_is_last(sk, skb))
525f6302d1dSDavid S. Miller 			nonagle = TCP_NAGLE_PUSH;
526f6302d1dSDavid S. Miller 		if (!tcp_snd_test(sk, skb, cur_mss, nonagle) ||
527f6302d1dSDavid S. Miller 		    tcp_write_xmit(sk, nonagle))
528f6302d1dSDavid S. Miller 			tcp_check_probe_timer(sk, tp);
529f6302d1dSDavid S. Miller 	}
530f6302d1dSDavid S. Miller 	tcp_cwnd_validate(sk, tp);
531f6302d1dSDavid S. Miller }
532f6302d1dSDavid S. Miller 
533*84d3e7b9SDavid S. Miller void __tcp_data_snd_check(struct sock *sk, struct sk_buff *skb)
534*84d3e7b9SDavid S. Miller {
535*84d3e7b9SDavid S. Miller 	struct tcp_sock *tp = tcp_sk(sk);
536*84d3e7b9SDavid S. Miller 
537*84d3e7b9SDavid S. Miller 	if (after(TCP_SKB_CB(skb)->end_seq, tp->snd_una + tp->snd_wnd) ||
538*84d3e7b9SDavid S. Miller 	    tcp_packets_in_flight(tp) >= tp->snd_cwnd ||
539*84d3e7b9SDavid S. Miller 	    tcp_write_xmit(sk, tp->nonagle))
540*84d3e7b9SDavid S. Miller 		tcp_check_probe_timer(sk, tp);
541*84d3e7b9SDavid S. Miller }
542*84d3e7b9SDavid S. Miller 
543f6302d1dSDavid S. Miller int tcp_may_send_now(struct sock *sk, struct tcp_sock *tp)
544f6302d1dSDavid S. Miller {
545f6302d1dSDavid S. Miller 	struct sk_buff *skb = sk->sk_send_head;
546f6302d1dSDavid S. Miller 
547f6302d1dSDavid S. Miller 	return (skb &&
548f6302d1dSDavid S. Miller 		tcp_snd_test(sk, skb, tcp_current_mss(sk, 1),
549f6302d1dSDavid S. Miller 			     (tcp_skb_is_last(sk, skb) ?
550f6302d1dSDavid S. Miller 			      TCP_NAGLE_PUSH :
551f6302d1dSDavid S. Miller 			      tp->nonagle)));
552f6302d1dSDavid S. Miller }
553f6302d1dSDavid S. Miller 
554f6302d1dSDavid S. Miller 
5551da177e4SLinus Torvalds /* Send _single_ skb sitting at the send head. This function requires
5561da177e4SLinus Torvalds  * true push pending frames to setup probe timer etc.
5571da177e4SLinus Torvalds  */
5581da177e4SLinus Torvalds void tcp_push_one(struct sock *sk, unsigned cur_mss)
5591da177e4SLinus Torvalds {
5601da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
5611da177e4SLinus Torvalds 	struct sk_buff *skb = sk->sk_send_head;
5621da177e4SLinus Torvalds 
563d5ac99a6SDavid S. Miller 	if (tcp_snd_test(sk, skb, cur_mss, TCP_NAGLE_PUSH)) {
5641da177e4SLinus Torvalds 		/* Send it out now. */
5651da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->when = tcp_time_stamp;
5661da177e4SLinus Torvalds 		tcp_tso_set_push(skb);
5671da177e4SLinus Torvalds 		if (!tcp_transmit_skb(sk, skb_clone(skb, sk->sk_allocation))) {
5681da177e4SLinus Torvalds 			sk->sk_send_head = NULL;
5691da177e4SLinus Torvalds 			tp->snd_nxt = TCP_SKB_CB(skb)->end_seq;
5701da177e4SLinus Torvalds 			tcp_packets_out_inc(sk, tp, skb);
5711da177e4SLinus Torvalds 			return;
5721da177e4SLinus Torvalds 		}
5731da177e4SLinus Torvalds 	}
5741da177e4SLinus Torvalds }
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds /* Function to create two new TCP segments.  Shrinks the given segment
5771da177e4SLinus Torvalds  * to the specified size and appends a new segment with the rest of the
5781da177e4SLinus Torvalds  * packet to the list.  This won't be called frequently, I hope.
5791da177e4SLinus Torvalds  * Remember, these are still headerless SKBs at this point.
5801da177e4SLinus Torvalds  */
5811da177e4SLinus Torvalds static int tcp_fragment(struct sock *sk, struct sk_buff *skb, u32 len)
5821da177e4SLinus Torvalds {
5831da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
5841da177e4SLinus Torvalds 	struct sk_buff *buff;
5851da177e4SLinus Torvalds 	int nsize;
5861da177e4SLinus Torvalds 	u16 flags;
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	nsize = skb_headlen(skb) - len;
5891da177e4SLinus Torvalds 	if (nsize < 0)
5901da177e4SLinus Torvalds 		nsize = 0;
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	if (skb_cloned(skb) &&
5931da177e4SLinus Torvalds 	    skb_is_nonlinear(skb) &&
5941da177e4SLinus Torvalds 	    pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
5951da177e4SLinus Torvalds 		return -ENOMEM;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	/* Get a new skb... force flag on. */
5981da177e4SLinus Torvalds 	buff = sk_stream_alloc_skb(sk, nsize, GFP_ATOMIC);
5991da177e4SLinus Torvalds 	if (buff == NULL)
6001da177e4SLinus Torvalds 		return -ENOMEM; /* We'll just try again later. */
6011da177e4SLinus Torvalds 	sk_charge_skb(sk, buff);
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	/* Correct the sequence numbers. */
6041da177e4SLinus Torvalds 	TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
6051da177e4SLinus Torvalds 	TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
6061da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	/* PSH and FIN should only be set in the second packet. */
6091da177e4SLinus Torvalds 	flags = TCP_SKB_CB(skb)->flags;
6101da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->flags = flags & ~(TCPCB_FLAG_FIN|TCPCB_FLAG_PSH);
6111da177e4SLinus Torvalds 	TCP_SKB_CB(buff)->flags = flags;
6121da177e4SLinus Torvalds 	TCP_SKB_CB(buff)->sacked =
6131da177e4SLinus Torvalds 		(TCP_SKB_CB(skb)->sacked &
6141da177e4SLinus Torvalds 		 (TCPCB_LOST | TCPCB_EVER_RETRANS | TCPCB_AT_TAIL));
6151da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->sacked &= ~TCPCB_AT_TAIL;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	if (!skb_shinfo(skb)->nr_frags && skb->ip_summed != CHECKSUM_HW) {
6181da177e4SLinus Torvalds 		/* Copy and checksum data tail into the new buffer. */
6191da177e4SLinus Torvalds 		buff->csum = csum_partial_copy_nocheck(skb->data + len, skb_put(buff, nsize),
6201da177e4SLinus Torvalds 						       nsize, 0);
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 		skb_trim(skb, len);
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 		skb->csum = csum_block_sub(skb->csum, buff->csum, len);
6251da177e4SLinus Torvalds 	} else {
6261da177e4SLinus Torvalds 		skb->ip_summed = CHECKSUM_HW;
6271da177e4SLinus Torvalds 		skb_split(skb, buff, len);
6281da177e4SLinus Torvalds 	}
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	buff->ip_summed = skb->ip_summed;
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	/* Looks stupid, but our code really uses when of
6331da177e4SLinus Torvalds 	 * skbs, which it never sent before. --ANK
6341da177e4SLinus Torvalds 	 */
6351da177e4SLinus Torvalds 	TCP_SKB_CB(buff)->when = TCP_SKB_CB(skb)->when;
636317a76f9SStephen Hemminger 	buff->stamp = skb->stamp;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST) {
6391da177e4SLinus Torvalds 		tp->lost_out -= tcp_skb_pcount(skb);
6401da177e4SLinus Torvalds 		tp->left_out -= tcp_skb_pcount(skb);
6411da177e4SLinus Torvalds 	}
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	/* Fix up tso_factor for both original and new SKB.  */
644d5ac99a6SDavid S. Miller 	tcp_set_skb_tso_segs(sk, skb);
645d5ac99a6SDavid S. Miller 	tcp_set_skb_tso_segs(sk, buff);
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST) {
6481da177e4SLinus Torvalds 		tp->lost_out += tcp_skb_pcount(skb);
6491da177e4SLinus Torvalds 		tp->left_out += tcp_skb_pcount(skb);
6501da177e4SLinus Torvalds 	}
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	if (TCP_SKB_CB(buff)->sacked&TCPCB_LOST) {
6531da177e4SLinus Torvalds 		tp->lost_out += tcp_skb_pcount(buff);
6541da177e4SLinus Torvalds 		tp->left_out += tcp_skb_pcount(buff);
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	/* Link BUFF into the send queue. */
6581da177e4SLinus Torvalds 	__skb_append(skb, buff);
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	return 0;
6611da177e4SLinus Torvalds }
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds /* This is similar to __pskb_pull_head() (it will go to core/skbuff.c
6641da177e4SLinus Torvalds  * eventually). The difference is that pulled data not copied, but
6651da177e4SLinus Torvalds  * immediately discarded.
6661da177e4SLinus Torvalds  */
6671da177e4SLinus Torvalds static unsigned char *__pskb_trim_head(struct sk_buff *skb, int len)
6681da177e4SLinus Torvalds {
6691da177e4SLinus Torvalds 	int i, k, eat;
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	eat = len;
6721da177e4SLinus Torvalds 	k = 0;
6731da177e4SLinus Torvalds 	for (i=0; i<skb_shinfo(skb)->nr_frags; i++) {
6741da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frags[i].size <= eat) {
6751da177e4SLinus Torvalds 			put_page(skb_shinfo(skb)->frags[i].page);
6761da177e4SLinus Torvalds 			eat -= skb_shinfo(skb)->frags[i].size;
6771da177e4SLinus Torvalds 		} else {
6781da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
6791da177e4SLinus Torvalds 			if (eat) {
6801da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].page_offset += eat;
6811da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].size -= eat;
6821da177e4SLinus Torvalds 				eat = 0;
6831da177e4SLinus Torvalds 			}
6841da177e4SLinus Torvalds 			k++;
6851da177e4SLinus Torvalds 		}
6861da177e4SLinus Torvalds 	}
6871da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	skb->tail = skb->data;
6901da177e4SLinus Torvalds 	skb->data_len -= len;
6911da177e4SLinus Torvalds 	skb->len = skb->data_len;
6921da177e4SLinus Torvalds 	return skb->tail;
6931da177e4SLinus Torvalds }
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds int tcp_trim_head(struct sock *sk, struct sk_buff *skb, u32 len)
6961da177e4SLinus Torvalds {
6971da177e4SLinus Torvalds 	if (skb_cloned(skb) &&
6981da177e4SLinus Torvalds 	    pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6991da177e4SLinus Torvalds 		return -ENOMEM;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	if (len <= skb_headlen(skb)) {
7021da177e4SLinus Torvalds 		__skb_pull(skb, len);
7031da177e4SLinus Torvalds 	} else {
7041da177e4SLinus Torvalds 		if (__pskb_trim_head(skb, len-skb_headlen(skb)) == NULL)
7051da177e4SLinus Torvalds 			return -ENOMEM;
7061da177e4SLinus Torvalds 	}
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->seq += len;
7091da177e4SLinus Torvalds 	skb->ip_summed = CHECKSUM_HW;
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	skb->truesize	     -= len;
7121da177e4SLinus Torvalds 	sk->sk_wmem_queued   -= len;
7131da177e4SLinus Torvalds 	sk->sk_forward_alloc += len;
7141da177e4SLinus Torvalds 	sock_set_flag(sk, SOCK_QUEUE_SHRUNK);
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	/* Any change of skb->len requires recalculation of tso
7171da177e4SLinus Torvalds 	 * factor and mss.
7181da177e4SLinus Torvalds 	 */
7191da177e4SLinus Torvalds 	if (tcp_skb_pcount(skb) > 1)
720d5ac99a6SDavid S. Miller 		tcp_set_skb_tso_segs(sk, skb);
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	return 0;
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds /* This function synchronize snd mss to current pmtu/exthdr set.
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds    tp->rx_opt.user_mss is mss set by user by TCP_MAXSEG. It does NOT counts
7281da177e4SLinus Torvalds    for TCP options, but includes only bare TCP header.
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds    tp->rx_opt.mss_clamp is mss negotiated at connection setup.
7311da177e4SLinus Torvalds    It is minumum of user_mss and mss received with SYN.
7321da177e4SLinus Torvalds    It also does not include TCP options.
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds    tp->pmtu_cookie is last pmtu, seen by this function.
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds    tp->mss_cache is current effective sending mss, including
7371da177e4SLinus Torvalds    all tcp options except for SACKs. It is evaluated,
7381da177e4SLinus Torvalds    taking into account current pmtu, but never exceeds
7391da177e4SLinus Torvalds    tp->rx_opt.mss_clamp.
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds    NOTE1. rfc1122 clearly states that advertised MSS
7421da177e4SLinus Torvalds    DOES NOT include either tcp or ip options.
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds    NOTE2. tp->pmtu_cookie and tp->mss_cache are READ ONLY outside
7451da177e4SLinus Torvalds    this function.			--ANK (980731)
7461da177e4SLinus Torvalds  */
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu)
7491da177e4SLinus Torvalds {
7501da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
7511da177e4SLinus Torvalds 	int mss_now;
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	/* Calculate base mss without TCP options:
7541da177e4SLinus Torvalds 	   It is MMS_S - sizeof(tcphdr) of rfc1122
7551da177e4SLinus Torvalds 	 */
7561da177e4SLinus Torvalds 	mss_now = pmtu - tp->af_specific->net_header_len - sizeof(struct tcphdr);
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	/* Clamp it (mss_clamp does not include tcp options) */
7591da177e4SLinus Torvalds 	if (mss_now > tp->rx_opt.mss_clamp)
7601da177e4SLinus Torvalds 		mss_now = tp->rx_opt.mss_clamp;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	/* Now subtract optional transport overhead */
7631da177e4SLinus Torvalds 	mss_now -= tp->ext_header_len;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	/* Then reserve room for full set of TCP options and 8 bytes of data */
7661da177e4SLinus Torvalds 	if (mss_now < 48)
7671da177e4SLinus Torvalds 		mss_now = 48;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	/* Now subtract TCP options size, not including SACKs */
7701da177e4SLinus Torvalds 	mss_now -= tp->tcp_header_len - sizeof(struct tcphdr);
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	/* Bound mss with half of window */
7731da177e4SLinus Torvalds 	if (tp->max_window && mss_now > (tp->max_window>>1))
7741da177e4SLinus Torvalds 		mss_now = max((tp->max_window>>1), 68U - tp->tcp_header_len);
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	/* And store cached results */
7771da177e4SLinus Torvalds 	tp->pmtu_cookie = pmtu;
7781da177e4SLinus Torvalds 	tp->mss_cache = tp->mss_cache_std = mss_now;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	return mss_now;
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds /* Compute the current effective MSS, taking SACKs and IP options,
7841da177e4SLinus Torvalds  * and even PMTU discovery events into account.
7851da177e4SLinus Torvalds  *
7861da177e4SLinus Torvalds  * LARGESEND note: !urg_mode is overkill, only frames up to snd_up
7871da177e4SLinus Torvalds  * cannot be large. However, taking into account rare use of URG, this
7881da177e4SLinus Torvalds  * is not a big flaw.
7891da177e4SLinus Torvalds  */
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds unsigned int tcp_current_mss(struct sock *sk, int large)
7921da177e4SLinus Torvalds {
7931da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
7941da177e4SLinus Torvalds 	struct dst_entry *dst = __sk_dst_get(sk);
7951da177e4SLinus Torvalds 	unsigned int do_large, mss_now;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	mss_now = tp->mss_cache_std;
7981da177e4SLinus Torvalds 	if (dst) {
7991da177e4SLinus Torvalds 		u32 mtu = dst_mtu(dst);
8001da177e4SLinus Torvalds 		if (mtu != tp->pmtu_cookie)
8011da177e4SLinus Torvalds 			mss_now = tcp_sync_mss(sk, mtu);
8021da177e4SLinus Torvalds 	}
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	do_large = (large &&
8051da177e4SLinus Torvalds 		    (sk->sk_route_caps & NETIF_F_TSO) &&
8061da177e4SLinus Torvalds 		    !tp->urg_mode);
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	if (do_large) {
8091da177e4SLinus Torvalds 		unsigned int large_mss, factor, limit;
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 		large_mss = 65535 - tp->af_specific->net_header_len -
8121da177e4SLinus Torvalds 			tp->ext_header_len - tp->tcp_header_len;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 		if (tp->max_window && large_mss > (tp->max_window>>1))
8151da177e4SLinus Torvalds 			large_mss = max((tp->max_window>>1),
8161da177e4SLinus Torvalds 					68U - tp->tcp_header_len);
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 		factor = large_mss / mss_now;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 		/* Always keep large mss multiple of real mss, but
8211da177e4SLinus Torvalds 		 * do not exceed 1/tso_win_divisor of the congestion window
8221da177e4SLinus Torvalds 		 * so we can keep the ACK clock ticking and minimize
8231da177e4SLinus Torvalds 		 * bursting.
8241da177e4SLinus Torvalds 		 */
8251da177e4SLinus Torvalds 		limit = tp->snd_cwnd;
8261da177e4SLinus Torvalds 		if (sysctl_tcp_tso_win_divisor)
8271da177e4SLinus Torvalds 			limit /= sysctl_tcp_tso_win_divisor;
8281da177e4SLinus Torvalds 		limit = max(1U, limit);
8291da177e4SLinus Torvalds 		if (factor > limit)
8301da177e4SLinus Torvalds 			factor = limit;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 		tp->mss_cache = mss_now * factor;
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 		mss_now = tp->mss_cache;
8351da177e4SLinus Torvalds 	}
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	if (tp->rx_opt.eff_sacks)
8381da177e4SLinus Torvalds 		mss_now -= (TCPOLEN_SACK_BASE_ALIGNED +
8391da177e4SLinus Torvalds 			    (tp->rx_opt.eff_sacks * TCPOLEN_SACK_PERBLOCK));
8401da177e4SLinus Torvalds 	return mss_now;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds /* This routine writes packets to the network.  It advances the
8441da177e4SLinus Torvalds  * send_head.  This happens as incoming acks open up the remote
8451da177e4SLinus Torvalds  * window for us.
8461da177e4SLinus Torvalds  *
8471da177e4SLinus Torvalds  * Returns 1, if no segments are in flight and we have queued segments, but
8481da177e4SLinus Torvalds  * cannot send anything now because of SWS or another problem.
8491da177e4SLinus Torvalds  */
8501da177e4SLinus Torvalds int tcp_write_xmit(struct sock *sk, int nonagle)
8511da177e4SLinus Torvalds {
8521da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
8531da177e4SLinus Torvalds 	unsigned int mss_now;
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	/* If we are closed, the bytes will have to remain here.
8561da177e4SLinus Torvalds 	 * In time closedown will finish, we empty the write queue and all
8571da177e4SLinus Torvalds 	 * will be happy.
8581da177e4SLinus Torvalds 	 */
8591da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE) {
8601da177e4SLinus Torvalds 		struct sk_buff *skb;
8611da177e4SLinus Torvalds 		int sent_pkts = 0;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 		/* Account for SACKS, we may need to fragment due to this.
8641da177e4SLinus Torvalds 		 * It is just like the real MSS changing on us midstream.
8651da177e4SLinus Torvalds 		 * We also handle things correctly when the user adds some
8661da177e4SLinus Torvalds 		 * IP options mid-stream.  Silly to do, but cover it.
8671da177e4SLinus Torvalds 		 */
8681da177e4SLinus Torvalds 		mss_now = tcp_current_mss(sk, 1);
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 		while ((skb = sk->sk_send_head) &&
871d5ac99a6SDavid S. Miller 		       tcp_snd_test(sk, skb, mss_now,
8721da177e4SLinus Torvalds 			       	    tcp_skb_is_last(sk, skb) ? nonagle :
8731da177e4SLinus Torvalds 				    			       TCP_NAGLE_PUSH)) {
8741da177e4SLinus Torvalds 			if (skb->len > mss_now) {
8751da177e4SLinus Torvalds 				if (tcp_fragment(sk, skb, mss_now))
8761da177e4SLinus Torvalds 					break;
8771da177e4SLinus Torvalds 			}
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 			TCP_SKB_CB(skb)->when = tcp_time_stamp;
8801da177e4SLinus Torvalds 			tcp_tso_set_push(skb);
8811da177e4SLinus Torvalds 			if (tcp_transmit_skb(sk, skb_clone(skb, GFP_ATOMIC)))
8821da177e4SLinus Torvalds 				break;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 			/* Advance the send_head.  This one is sent out.
8851da177e4SLinus Torvalds 			 * This call will increment packets_out.
8861da177e4SLinus Torvalds 			 */
8871da177e4SLinus Torvalds 			update_send_head(sk, tp, skb);
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 			tcp_minshall_update(tp, mss_now, skb);
8901da177e4SLinus Torvalds 			sent_pkts = 1;
8911da177e4SLinus Torvalds 		}
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 		if (sent_pkts) {
8941da177e4SLinus Torvalds 			tcp_cwnd_validate(sk, tp);
8951da177e4SLinus Torvalds 			return 0;
8961da177e4SLinus Torvalds 		}
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 		return !tp->packets_out && sk->sk_send_head;
8991da177e4SLinus Torvalds 	}
9001da177e4SLinus Torvalds 	return 0;
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds /* This function returns the amount that we can raise the
9041da177e4SLinus Torvalds  * usable window based on the following constraints
9051da177e4SLinus Torvalds  *
9061da177e4SLinus Torvalds  * 1. The window can never be shrunk once it is offered (RFC 793)
9071da177e4SLinus Torvalds  * 2. We limit memory per socket
9081da177e4SLinus Torvalds  *
9091da177e4SLinus Torvalds  * RFC 1122:
9101da177e4SLinus Torvalds  * "the suggested [SWS] avoidance algorithm for the receiver is to keep
9111da177e4SLinus Torvalds  *  RECV.NEXT + RCV.WIN fixed until:
9121da177e4SLinus Torvalds  *  RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)"
9131da177e4SLinus Torvalds  *
9141da177e4SLinus Torvalds  * i.e. don't raise the right edge of the window until you can raise
9151da177e4SLinus Torvalds  * it at least MSS bytes.
9161da177e4SLinus Torvalds  *
9171da177e4SLinus Torvalds  * Unfortunately, the recommended algorithm breaks header prediction,
9181da177e4SLinus Torvalds  * since header prediction assumes th->window stays fixed.
9191da177e4SLinus Torvalds  *
9201da177e4SLinus Torvalds  * Strictly speaking, keeping th->window fixed violates the receiver
9211da177e4SLinus Torvalds  * side SWS prevention criteria. The problem is that under this rule
9221da177e4SLinus Torvalds  * a stream of single byte packets will cause the right side of the
9231da177e4SLinus Torvalds  * window to always advance by a single byte.
9241da177e4SLinus Torvalds  *
9251da177e4SLinus Torvalds  * Of course, if the sender implements sender side SWS prevention
9261da177e4SLinus Torvalds  * then this will not be a problem.
9271da177e4SLinus Torvalds  *
9281da177e4SLinus Torvalds  * BSD seems to make the following compromise:
9291da177e4SLinus Torvalds  *
9301da177e4SLinus Torvalds  *	If the free space is less than the 1/4 of the maximum
9311da177e4SLinus Torvalds  *	space available and the free space is less than 1/2 mss,
9321da177e4SLinus Torvalds  *	then set the window to 0.
9331da177e4SLinus Torvalds  *	[ Actually, bsd uses MSS and 1/4 of maximal _window_ ]
9341da177e4SLinus Torvalds  *	Otherwise, just prevent the window from shrinking
9351da177e4SLinus Torvalds  *	and from being larger than the largest representable value.
9361da177e4SLinus Torvalds  *
9371da177e4SLinus Torvalds  * This prevents incremental opening of the window in the regime
9381da177e4SLinus Torvalds  * where TCP is limited by the speed of the reader side taking
9391da177e4SLinus Torvalds  * data out of the TCP receive queue. It does nothing about
9401da177e4SLinus Torvalds  * those cases where the window is constrained on the sender side
9411da177e4SLinus Torvalds  * because the pipeline is full.
9421da177e4SLinus Torvalds  *
9431da177e4SLinus Torvalds  * BSD also seems to "accidentally" limit itself to windows that are a
9441da177e4SLinus Torvalds  * multiple of MSS, at least until the free space gets quite small.
9451da177e4SLinus Torvalds  * This would appear to be a side effect of the mbuf implementation.
9461da177e4SLinus Torvalds  * Combining these two algorithms results in the observed behavior
9471da177e4SLinus Torvalds  * of having a fixed window size at almost all times.
9481da177e4SLinus Torvalds  *
9491da177e4SLinus Torvalds  * Below we obtain similar behavior by forcing the offered window to
9501da177e4SLinus Torvalds  * a multiple of the mss when it is feasible to do so.
9511da177e4SLinus Torvalds  *
9521da177e4SLinus Torvalds  * Note, we don't "adjust" for TIMESTAMP or SACK option bytes.
9531da177e4SLinus Torvalds  * Regular options like TIMESTAMP are taken into account.
9541da177e4SLinus Torvalds  */
9551da177e4SLinus Torvalds u32 __tcp_select_window(struct sock *sk)
9561da177e4SLinus Torvalds {
9571da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
9581da177e4SLinus Torvalds 	/* MSS for the peer's data.  Previous verions used mss_clamp
9591da177e4SLinus Torvalds 	 * here.  I don't know if the value based on our guesses
9601da177e4SLinus Torvalds 	 * of peer's MSS is better for the performance.  It's more correct
9611da177e4SLinus Torvalds 	 * but may be worse for the performance because of rcv_mss
9621da177e4SLinus Torvalds 	 * fluctuations.  --SAW  1998/11/1
9631da177e4SLinus Torvalds 	 */
9641da177e4SLinus Torvalds 	int mss = tp->ack.rcv_mss;
9651da177e4SLinus Torvalds 	int free_space = tcp_space(sk);
9661da177e4SLinus Torvalds 	int full_space = min_t(int, tp->window_clamp, tcp_full_space(sk));
9671da177e4SLinus Torvalds 	int window;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	if (mss > full_space)
9701da177e4SLinus Torvalds 		mss = full_space;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	if (free_space < full_space/2) {
9731da177e4SLinus Torvalds 		tp->ack.quick = 0;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 		if (tcp_memory_pressure)
9761da177e4SLinus Torvalds 			tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U*tp->advmss);
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 		if (free_space < mss)
9791da177e4SLinus Torvalds 			return 0;
9801da177e4SLinus Torvalds 	}
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	if (free_space > tp->rcv_ssthresh)
9831da177e4SLinus Torvalds 		free_space = tp->rcv_ssthresh;
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	/* Don't do rounding if we are using window scaling, since the
9861da177e4SLinus Torvalds 	 * scaled window will not line up with the MSS boundary anyway.
9871da177e4SLinus Torvalds 	 */
9881da177e4SLinus Torvalds 	window = tp->rcv_wnd;
9891da177e4SLinus Torvalds 	if (tp->rx_opt.rcv_wscale) {
9901da177e4SLinus Torvalds 		window = free_space;
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 		/* Advertise enough space so that it won't get scaled away.
9931da177e4SLinus Torvalds 		 * Import case: prevent zero window announcement if
9941da177e4SLinus Torvalds 		 * 1<<rcv_wscale > mss.
9951da177e4SLinus Torvalds 		 */
9961da177e4SLinus Torvalds 		if (((window >> tp->rx_opt.rcv_wscale) << tp->rx_opt.rcv_wscale) != window)
9971da177e4SLinus Torvalds 			window = (((window >> tp->rx_opt.rcv_wscale) + 1)
9981da177e4SLinus Torvalds 				  << tp->rx_opt.rcv_wscale);
9991da177e4SLinus Torvalds 	} else {
10001da177e4SLinus Torvalds 		/* Get the largest window that is a nice multiple of mss.
10011da177e4SLinus Torvalds 		 * Window clamp already applied above.
10021da177e4SLinus Torvalds 		 * If our current window offering is within 1 mss of the
10031da177e4SLinus Torvalds 		 * free space we just keep it. This prevents the divide
10041da177e4SLinus Torvalds 		 * and multiply from happening most of the time.
10051da177e4SLinus Torvalds 		 * We also don't do any window rounding when the free space
10061da177e4SLinus Torvalds 		 * is too small.
10071da177e4SLinus Torvalds 		 */
10081da177e4SLinus Torvalds 		if (window <= free_space - mss || window > free_space)
10091da177e4SLinus Torvalds 			window = (free_space/mss)*mss;
10101da177e4SLinus Torvalds 	}
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	return window;
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds /* Attempt to collapse two adjacent SKB's during retransmission. */
10161da177e4SLinus Torvalds static void tcp_retrans_try_collapse(struct sock *sk, struct sk_buff *skb, int mss_now)
10171da177e4SLinus Torvalds {
10181da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
10191da177e4SLinus Torvalds 	struct sk_buff *next_skb = skb->next;
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	/* The first test we must make is that neither of these two
10221da177e4SLinus Torvalds 	 * SKB's are still referenced by someone else.
10231da177e4SLinus Torvalds 	 */
10241da177e4SLinus Torvalds 	if (!skb_cloned(skb) && !skb_cloned(next_skb)) {
10251da177e4SLinus Torvalds 		int skb_size = skb->len, next_skb_size = next_skb->len;
10261da177e4SLinus Torvalds 		u16 flags = TCP_SKB_CB(skb)->flags;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 		/* Also punt if next skb has been SACK'd. */
10291da177e4SLinus Torvalds 		if(TCP_SKB_CB(next_skb)->sacked & TCPCB_SACKED_ACKED)
10301da177e4SLinus Torvalds 			return;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 		/* Next skb is out of window. */
10331da177e4SLinus Torvalds 		if (after(TCP_SKB_CB(next_skb)->end_seq, tp->snd_una+tp->snd_wnd))
10341da177e4SLinus Torvalds 			return;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 		/* Punt if not enough space exists in the first SKB for
10371da177e4SLinus Torvalds 		 * the data in the second, or the total combined payload
10381da177e4SLinus Torvalds 		 * would exceed the MSS.
10391da177e4SLinus Torvalds 		 */
10401da177e4SLinus Torvalds 		if ((next_skb_size > skb_tailroom(skb)) ||
10411da177e4SLinus Torvalds 		    ((skb_size + next_skb_size) > mss_now))
10421da177e4SLinus Torvalds 			return;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 		BUG_ON(tcp_skb_pcount(skb) != 1 ||
10451da177e4SLinus Torvalds 		       tcp_skb_pcount(next_skb) != 1);
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 		/* Ok.  We will be able to collapse the packet. */
10481da177e4SLinus Torvalds 		__skb_unlink(next_skb, next_skb->list);
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 		memcpy(skb_put(skb, next_skb_size), next_skb->data, next_skb_size);
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 		if (next_skb->ip_summed == CHECKSUM_HW)
10531da177e4SLinus Torvalds 			skb->ip_summed = CHECKSUM_HW;
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 		if (skb->ip_summed != CHECKSUM_HW)
10561da177e4SLinus Torvalds 			skb->csum = csum_block_add(skb->csum, next_skb->csum, skb_size);
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 		/* Update sequence range on original skb. */
10591da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(next_skb)->end_seq;
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 		/* Merge over control information. */
10621da177e4SLinus Torvalds 		flags |= TCP_SKB_CB(next_skb)->flags; /* This moves PSH/FIN etc. over */
10631da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->flags = flags;
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 		/* All done, get rid of second SKB and account for it so
10661da177e4SLinus Torvalds 		 * packet counting does not break.
10671da177e4SLinus Torvalds 		 */
10681da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->sacked |= TCP_SKB_CB(next_skb)->sacked&(TCPCB_EVER_RETRANS|TCPCB_AT_TAIL);
10691da177e4SLinus Torvalds 		if (TCP_SKB_CB(next_skb)->sacked&TCPCB_SACKED_RETRANS)
10701da177e4SLinus Torvalds 			tp->retrans_out -= tcp_skb_pcount(next_skb);
10711da177e4SLinus Torvalds 		if (TCP_SKB_CB(next_skb)->sacked&TCPCB_LOST) {
10721da177e4SLinus Torvalds 			tp->lost_out -= tcp_skb_pcount(next_skb);
10731da177e4SLinus Torvalds 			tp->left_out -= tcp_skb_pcount(next_skb);
10741da177e4SLinus Torvalds 		}
10751da177e4SLinus Torvalds 		/* Reno case is special. Sigh... */
10761da177e4SLinus Torvalds 		if (!tp->rx_opt.sack_ok && tp->sacked_out) {
10771da177e4SLinus Torvalds 			tcp_dec_pcount_approx(&tp->sacked_out, next_skb);
10781da177e4SLinus Torvalds 			tp->left_out -= tcp_skb_pcount(next_skb);
10791da177e4SLinus Torvalds 		}
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 		/* Not quite right: it can be > snd.fack, but
10821da177e4SLinus Torvalds 		 * it is better to underestimate fackets.
10831da177e4SLinus Torvalds 		 */
10841da177e4SLinus Torvalds 		tcp_dec_pcount_approx(&tp->fackets_out, next_skb);
10851da177e4SLinus Torvalds 		tcp_packets_out_dec(tp, next_skb);
10861da177e4SLinus Torvalds 		sk_stream_free_skb(sk, next_skb);
10871da177e4SLinus Torvalds 	}
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds /* Do a simple retransmit without using the backoff mechanisms in
10911da177e4SLinus Torvalds  * tcp_timer. This is used for path mtu discovery.
10921da177e4SLinus Torvalds  * The socket is already locked here.
10931da177e4SLinus Torvalds  */
10941da177e4SLinus Torvalds void tcp_simple_retransmit(struct sock *sk)
10951da177e4SLinus Torvalds {
10961da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
10971da177e4SLinus Torvalds 	struct sk_buff *skb;
10981da177e4SLinus Torvalds 	unsigned int mss = tcp_current_mss(sk, 0);
10991da177e4SLinus Torvalds 	int lost = 0;
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 	sk_stream_for_retrans_queue(skb, sk) {
11021da177e4SLinus Torvalds 		if (skb->len > mss &&
11031da177e4SLinus Torvalds 		    !(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED)) {
11041da177e4SLinus Torvalds 			if (TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS) {
11051da177e4SLinus Torvalds 				TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
11061da177e4SLinus Torvalds 				tp->retrans_out -= tcp_skb_pcount(skb);
11071da177e4SLinus Torvalds 			}
11081da177e4SLinus Torvalds 			if (!(TCP_SKB_CB(skb)->sacked&TCPCB_LOST)) {
11091da177e4SLinus Torvalds 				TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
11101da177e4SLinus Torvalds 				tp->lost_out += tcp_skb_pcount(skb);
11111da177e4SLinus Torvalds 				lost = 1;
11121da177e4SLinus Torvalds 			}
11131da177e4SLinus Torvalds 		}
11141da177e4SLinus Torvalds 	}
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	if (!lost)
11171da177e4SLinus Torvalds 		return;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	tcp_sync_left_out(tp);
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds  	/* Don't muck with the congestion window here.
11221da177e4SLinus Torvalds 	 * Reason is that we do not increase amount of _data_
11231da177e4SLinus Torvalds 	 * in network, but units changed and effective
11241da177e4SLinus Torvalds 	 * cwnd/ssthresh really reduced now.
11251da177e4SLinus Torvalds 	 */
11261da177e4SLinus Torvalds 	if (tp->ca_state != TCP_CA_Loss) {
11271da177e4SLinus Torvalds 		tp->high_seq = tp->snd_nxt;
11281da177e4SLinus Torvalds 		tp->snd_ssthresh = tcp_current_ssthresh(tp);
11291da177e4SLinus Torvalds 		tp->prior_ssthresh = 0;
11301da177e4SLinus Torvalds 		tp->undo_marker = 0;
11311da177e4SLinus Torvalds 		tcp_set_ca_state(tp, TCP_CA_Loss);
11321da177e4SLinus Torvalds 	}
11331da177e4SLinus Torvalds 	tcp_xmit_retransmit_queue(sk);
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds /* This retransmits one SKB.  Policy decisions and retransmit queue
11371da177e4SLinus Torvalds  * state updates are done by the caller.  Returns non-zero if an
11381da177e4SLinus Torvalds  * error occurred which prevented the send.
11391da177e4SLinus Torvalds  */
11401da177e4SLinus Torvalds int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
11411da177e4SLinus Torvalds {
11421da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
11431da177e4SLinus Torvalds  	unsigned int cur_mss = tcp_current_mss(sk, 0);
11441da177e4SLinus Torvalds 	int err;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	/* Do not sent more than we queued. 1/4 is reserved for possible
11471da177e4SLinus Torvalds 	 * copying overhead: frgagmentation, tunneling, mangling etc.
11481da177e4SLinus Torvalds 	 */
11491da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_wmem_alloc) >
11501da177e4SLinus Torvalds 	    min(sk->sk_wmem_queued + (sk->sk_wmem_queued >> 2), sk->sk_sndbuf))
11511da177e4SLinus Torvalds 		return -EAGAIN;
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 	if (before(TCP_SKB_CB(skb)->seq, tp->snd_una)) {
11541da177e4SLinus Torvalds 		if (before(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
11551da177e4SLinus Torvalds 			BUG();
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 		if (sk->sk_route_caps & NETIF_F_TSO) {
11581da177e4SLinus Torvalds 			sk->sk_route_caps &= ~NETIF_F_TSO;
11591da177e4SLinus Torvalds 			sock_set_flag(sk, SOCK_NO_LARGESEND);
11601da177e4SLinus Torvalds 			tp->mss_cache = tp->mss_cache_std;
11611da177e4SLinus Torvalds 		}
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 		if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
11641da177e4SLinus Torvalds 			return -ENOMEM;
11651da177e4SLinus Torvalds 	}
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	/* If receiver has shrunk his window, and skb is out of
11681da177e4SLinus Torvalds 	 * new window, do not retransmit it. The exception is the
11691da177e4SLinus Torvalds 	 * case, when window is shrunk to zero. In this case
11701da177e4SLinus Torvalds 	 * our retransmit serves as a zero window probe.
11711da177e4SLinus Torvalds 	 */
11721da177e4SLinus Torvalds 	if (!before(TCP_SKB_CB(skb)->seq, tp->snd_una+tp->snd_wnd)
11731da177e4SLinus Torvalds 	    && TCP_SKB_CB(skb)->seq != tp->snd_una)
11741da177e4SLinus Torvalds 		return -EAGAIN;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	if (skb->len > cur_mss) {
11771da177e4SLinus Torvalds 		int old_factor = tcp_skb_pcount(skb);
11781da177e4SLinus Torvalds 		int new_factor;
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 		if (tcp_fragment(sk, skb, cur_mss))
11811da177e4SLinus Torvalds 			return -ENOMEM; /* We'll try again later. */
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 		/* New SKB created, account for it. */
11841da177e4SLinus Torvalds 		new_factor = tcp_skb_pcount(skb);
11851da177e4SLinus Torvalds 		tp->packets_out -= old_factor - new_factor;
11861da177e4SLinus Torvalds 		tp->packets_out += tcp_skb_pcount(skb->next);
11871da177e4SLinus Torvalds 	}
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	/* Collapse two adjacent packets if worthwhile and we can. */
11901da177e4SLinus Torvalds 	if(!(TCP_SKB_CB(skb)->flags & TCPCB_FLAG_SYN) &&
11911da177e4SLinus Torvalds 	   (skb->len < (cur_mss >> 1)) &&
11921da177e4SLinus Torvalds 	   (skb->next != sk->sk_send_head) &&
11931da177e4SLinus Torvalds 	   (skb->next != (struct sk_buff *)&sk->sk_write_queue) &&
11941da177e4SLinus Torvalds 	   (skb_shinfo(skb)->nr_frags == 0 && skb_shinfo(skb->next)->nr_frags == 0) &&
11951da177e4SLinus Torvalds 	   (tcp_skb_pcount(skb) == 1 && tcp_skb_pcount(skb->next) == 1) &&
11961da177e4SLinus Torvalds 	   (sysctl_tcp_retrans_collapse != 0))
11971da177e4SLinus Torvalds 		tcp_retrans_try_collapse(sk, skb, cur_mss);
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 	if(tp->af_specific->rebuild_header(sk))
12001da177e4SLinus Torvalds 		return -EHOSTUNREACH; /* Routing failure or similar. */
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	/* Some Solaris stacks overoptimize and ignore the FIN on a
12031da177e4SLinus Torvalds 	 * retransmit when old data is attached.  So strip it off
12041da177e4SLinus Torvalds 	 * since it is cheap to do so and saves bytes on the network.
12051da177e4SLinus Torvalds 	 */
12061da177e4SLinus Torvalds 	if(skb->len > 0 &&
12071da177e4SLinus Torvalds 	   (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN) &&
12081da177e4SLinus Torvalds 	   tp->snd_una == (TCP_SKB_CB(skb)->end_seq - 1)) {
12091da177e4SLinus Torvalds 		if (!pskb_trim(skb, 0)) {
12101da177e4SLinus Torvalds 			TCP_SKB_CB(skb)->seq = TCP_SKB_CB(skb)->end_seq - 1;
12111da177e4SLinus Torvalds 			skb_shinfo(skb)->tso_segs = 1;
12121da177e4SLinus Torvalds 			skb_shinfo(skb)->tso_size = 0;
12131da177e4SLinus Torvalds 			skb->ip_summed = CHECKSUM_NONE;
12141da177e4SLinus Torvalds 			skb->csum = 0;
12151da177e4SLinus Torvalds 		}
12161da177e4SLinus Torvalds 	}
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	/* Make a copy, if the first transmission SKB clone we made
12191da177e4SLinus Torvalds 	 * is still in somebody's hands, else make a clone.
12201da177e4SLinus Torvalds 	 */
12211da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->when = tcp_time_stamp;
12221da177e4SLinus Torvalds 	tcp_tso_set_push(skb);
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	err = tcp_transmit_skb(sk, (skb_cloned(skb) ?
12251da177e4SLinus Torvalds 				    pskb_copy(skb, GFP_ATOMIC):
12261da177e4SLinus Torvalds 				    skb_clone(skb, GFP_ATOMIC)));
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	if (err == 0) {
12291da177e4SLinus Torvalds 		/* Update global TCP statistics. */
12301da177e4SLinus Torvalds 		TCP_INC_STATS(TCP_MIB_RETRANSSEGS);
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 		tp->total_retrans++;
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 0
12351da177e4SLinus Torvalds 		if (TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS) {
12361da177e4SLinus Torvalds 			if (net_ratelimit())
12371da177e4SLinus Torvalds 				printk(KERN_DEBUG "retrans_out leaked.\n");
12381da177e4SLinus Torvalds 		}
12391da177e4SLinus Torvalds #endif
12401da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->sacked |= TCPCB_RETRANS;
12411da177e4SLinus Torvalds 		tp->retrans_out += tcp_skb_pcount(skb);
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 		/* Save stamp of the first retransmit. */
12441da177e4SLinus Torvalds 		if (!tp->retrans_stamp)
12451da177e4SLinus Torvalds 			tp->retrans_stamp = TCP_SKB_CB(skb)->when;
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 		tp->undo_retrans++;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 		/* snd_nxt is stored to detect loss of retransmitted segment,
12501da177e4SLinus Torvalds 		 * see tcp_input.c tcp_sacktag_write_queue().
12511da177e4SLinus Torvalds 		 */
12521da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->ack_seq = tp->snd_nxt;
12531da177e4SLinus Torvalds 	}
12541da177e4SLinus Torvalds 	return err;
12551da177e4SLinus Torvalds }
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds /* This gets called after a retransmit timeout, and the initially
12581da177e4SLinus Torvalds  * retransmitted data is acknowledged.  It tries to continue
12591da177e4SLinus Torvalds  * resending the rest of the retransmit queue, until either
12601da177e4SLinus Torvalds  * we've sent it all or the congestion window limit is reached.
12611da177e4SLinus Torvalds  * If doing SACK, the first ACK which comes back for a timeout
12621da177e4SLinus Torvalds  * based retransmit packet might feed us FACK information again.
12631da177e4SLinus Torvalds  * If so, we use it to avoid unnecessarily retransmissions.
12641da177e4SLinus Torvalds  */
12651da177e4SLinus Torvalds void tcp_xmit_retransmit_queue(struct sock *sk)
12661da177e4SLinus Torvalds {
12671da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
12681da177e4SLinus Torvalds 	struct sk_buff *skb;
12691da177e4SLinus Torvalds 	int packet_cnt = tp->lost_out;
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	/* First pass: retransmit lost packets. */
12721da177e4SLinus Torvalds 	if (packet_cnt) {
12731da177e4SLinus Torvalds 		sk_stream_for_retrans_queue(skb, sk) {
12741da177e4SLinus Torvalds 			__u8 sacked = TCP_SKB_CB(skb)->sacked;
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 			/* Assume this retransmit will generate
12771da177e4SLinus Torvalds 			 * only one packet for congestion window
12781da177e4SLinus Torvalds 			 * calculation purposes.  This works because
12791da177e4SLinus Torvalds 			 * tcp_retransmit_skb() will chop up the
12801da177e4SLinus Torvalds 			 * packet to be MSS sized and all the
12811da177e4SLinus Torvalds 			 * packet counting works out.
12821da177e4SLinus Torvalds 			 */
12831da177e4SLinus Torvalds 			if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
12841da177e4SLinus Torvalds 				return;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 			if (sacked&TCPCB_LOST) {
12871da177e4SLinus Torvalds 				if (!(sacked&(TCPCB_SACKED_ACKED|TCPCB_SACKED_RETRANS))) {
12881da177e4SLinus Torvalds 					if (tcp_retransmit_skb(sk, skb))
12891da177e4SLinus Torvalds 						return;
12901da177e4SLinus Torvalds 					if (tp->ca_state != TCP_CA_Loss)
12911da177e4SLinus Torvalds 						NET_INC_STATS_BH(LINUX_MIB_TCPFASTRETRANS);
12921da177e4SLinus Torvalds 					else
12931da177e4SLinus Torvalds 						NET_INC_STATS_BH(LINUX_MIB_TCPSLOWSTARTRETRANS);
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 					if (skb ==
12961da177e4SLinus Torvalds 					    skb_peek(&sk->sk_write_queue))
12971da177e4SLinus Torvalds 						tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
12981da177e4SLinus Torvalds 				}
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 				packet_cnt -= tcp_skb_pcount(skb);
13011da177e4SLinus Torvalds 				if (packet_cnt <= 0)
13021da177e4SLinus Torvalds 					break;
13031da177e4SLinus Torvalds 			}
13041da177e4SLinus Torvalds 		}
13051da177e4SLinus Torvalds 	}
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	/* OK, demanded retransmission is finished. */
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	/* Forward retransmissions are possible only during Recovery. */
13101da177e4SLinus Torvalds 	if (tp->ca_state != TCP_CA_Recovery)
13111da177e4SLinus Torvalds 		return;
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 	/* No forward retransmissions in Reno are possible. */
13141da177e4SLinus Torvalds 	if (!tp->rx_opt.sack_ok)
13151da177e4SLinus Torvalds 		return;
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	/* Yeah, we have to make difficult choice between forward transmission
13181da177e4SLinus Torvalds 	 * and retransmission... Both ways have their merits...
13191da177e4SLinus Torvalds 	 *
13201da177e4SLinus Torvalds 	 * For now we do not retransmit anything, while we have some new
13211da177e4SLinus Torvalds 	 * segments to send.
13221da177e4SLinus Torvalds 	 */
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	if (tcp_may_send_now(sk, tp))
13251da177e4SLinus Torvalds 		return;
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	packet_cnt = 0;
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 	sk_stream_for_retrans_queue(skb, sk) {
13301da177e4SLinus Torvalds 		/* Similar to the retransmit loop above we
13311da177e4SLinus Torvalds 		 * can pretend that the retransmitted SKB
13321da177e4SLinus Torvalds 		 * we send out here will be composed of one
13331da177e4SLinus Torvalds 		 * real MSS sized packet because tcp_retransmit_skb()
13341da177e4SLinus Torvalds 		 * will fragment it if necessary.
13351da177e4SLinus Torvalds 		 */
13361da177e4SLinus Torvalds 		if (++packet_cnt > tp->fackets_out)
13371da177e4SLinus Torvalds 			break;
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds 		if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
13401da177e4SLinus Torvalds 			break;
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 		if (TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS)
13431da177e4SLinus Torvalds 			continue;
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 		/* Ok, retransmit it. */
13461da177e4SLinus Torvalds 		if (tcp_retransmit_skb(sk, skb))
13471da177e4SLinus Torvalds 			break;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 		if (skb == skb_peek(&sk->sk_write_queue))
13501da177e4SLinus Torvalds 			tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 		NET_INC_STATS_BH(LINUX_MIB_TCPFORWARDRETRANS);
13531da177e4SLinus Torvalds 	}
13541da177e4SLinus Torvalds }
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds /* Send a fin.  The caller locks the socket for us.  This cannot be
13581da177e4SLinus Torvalds  * allowed to fail queueing a FIN frame under any circumstances.
13591da177e4SLinus Torvalds  */
13601da177e4SLinus Torvalds void tcp_send_fin(struct sock *sk)
13611da177e4SLinus Torvalds {
13621da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
13631da177e4SLinus Torvalds 	struct sk_buff *skb = skb_peek_tail(&sk->sk_write_queue);
13641da177e4SLinus Torvalds 	int mss_now;
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	/* Optimization, tack on the FIN if we have a queue of
13671da177e4SLinus Torvalds 	 * unsent frames.  But be careful about outgoing SACKS
13681da177e4SLinus Torvalds 	 * and IP options.
13691da177e4SLinus Torvalds 	 */
13701da177e4SLinus Torvalds 	mss_now = tcp_current_mss(sk, 1);
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 	if (sk->sk_send_head != NULL) {
13731da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_FIN;
13741da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->end_seq++;
13751da177e4SLinus Torvalds 		tp->write_seq++;
13761da177e4SLinus Torvalds 	} else {
13771da177e4SLinus Torvalds 		/* Socket is locked, keep trying until memory is available. */
13781da177e4SLinus Torvalds 		for (;;) {
13791da177e4SLinus Torvalds 			skb = alloc_skb(MAX_TCP_HEADER, GFP_KERNEL);
13801da177e4SLinus Torvalds 			if (skb)
13811da177e4SLinus Torvalds 				break;
13821da177e4SLinus Torvalds 			yield();
13831da177e4SLinus Torvalds 		}
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 		/* Reserve space for headers and prepare control bits. */
13861da177e4SLinus Torvalds 		skb_reserve(skb, MAX_TCP_HEADER);
13871da177e4SLinus Torvalds 		skb->csum = 0;
13881da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->flags = (TCPCB_FLAG_ACK | TCPCB_FLAG_FIN);
13891da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->sacked = 0;
13901da177e4SLinus Torvalds 		skb_shinfo(skb)->tso_segs = 1;
13911da177e4SLinus Torvalds 		skb_shinfo(skb)->tso_size = 0;
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 		/* FIN eats a sequence byte, write_seq advanced by tcp_queue_skb(). */
13941da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->seq = tp->write_seq;
13951da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + 1;
13961da177e4SLinus Torvalds 		tcp_queue_skb(sk, skb);
13971da177e4SLinus Torvalds 	}
13981da177e4SLinus Torvalds 	__tcp_push_pending_frames(sk, tp, mss_now, TCP_NAGLE_OFF);
13991da177e4SLinus Torvalds }
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds /* We get here when a process closes a file descriptor (either due to
14021da177e4SLinus Torvalds  * an explicit close() or as a byproduct of exit()'ing) and there
14031da177e4SLinus Torvalds  * was unread data in the receive queue.  This behavior is recommended
14041da177e4SLinus Torvalds  * by draft-ietf-tcpimpl-prob-03.txt section 3.10.  -DaveM
14051da177e4SLinus Torvalds  */
14061da177e4SLinus Torvalds void tcp_send_active_reset(struct sock *sk, int priority)
14071da177e4SLinus Torvalds {
14081da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
14091da177e4SLinus Torvalds 	struct sk_buff *skb;
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds 	/* NOTE: No TCP options attached and we never retransmit this. */
14121da177e4SLinus Torvalds 	skb = alloc_skb(MAX_TCP_HEADER, priority);
14131da177e4SLinus Torvalds 	if (!skb) {
14141da177e4SLinus Torvalds 		NET_INC_STATS(LINUX_MIB_TCPABORTFAILED);
14151da177e4SLinus Torvalds 		return;
14161da177e4SLinus Torvalds 	}
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 	/* Reserve space for headers and prepare control bits. */
14191da177e4SLinus Torvalds 	skb_reserve(skb, MAX_TCP_HEADER);
14201da177e4SLinus Torvalds 	skb->csum = 0;
14211da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->flags = (TCPCB_FLAG_ACK | TCPCB_FLAG_RST);
14221da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->sacked = 0;
14231da177e4SLinus Torvalds 	skb_shinfo(skb)->tso_segs = 1;
14241da177e4SLinus Torvalds 	skb_shinfo(skb)->tso_size = 0;
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds 	/* Send it off. */
14271da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->seq = tcp_acceptable_seq(sk, tp);
14281da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq;
14291da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->when = tcp_time_stamp;
14301da177e4SLinus Torvalds 	if (tcp_transmit_skb(sk, skb))
14311da177e4SLinus Torvalds 		NET_INC_STATS(LINUX_MIB_TCPABORTFAILED);
14321da177e4SLinus Torvalds }
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds /* WARNING: This routine must only be called when we have already sent
14351da177e4SLinus Torvalds  * a SYN packet that crossed the incoming SYN that caused this routine
14361da177e4SLinus Torvalds  * to get called. If this assumption fails then the initial rcv_wnd
14371da177e4SLinus Torvalds  * and rcv_wscale values will not be correct.
14381da177e4SLinus Torvalds  */
14391da177e4SLinus Torvalds int tcp_send_synack(struct sock *sk)
14401da177e4SLinus Torvalds {
14411da177e4SLinus Torvalds 	struct sk_buff* skb;
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 	skb = skb_peek(&sk->sk_write_queue);
14441da177e4SLinus Torvalds 	if (skb == NULL || !(TCP_SKB_CB(skb)->flags&TCPCB_FLAG_SYN)) {
14451da177e4SLinus Torvalds 		printk(KERN_DEBUG "tcp_send_synack: wrong queue state\n");
14461da177e4SLinus Torvalds 		return -EFAULT;
14471da177e4SLinus Torvalds 	}
14481da177e4SLinus Torvalds 	if (!(TCP_SKB_CB(skb)->flags&TCPCB_FLAG_ACK)) {
14491da177e4SLinus Torvalds 		if (skb_cloned(skb)) {
14501da177e4SLinus Torvalds 			struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);
14511da177e4SLinus Torvalds 			if (nskb == NULL)
14521da177e4SLinus Torvalds 				return -ENOMEM;
14531da177e4SLinus Torvalds 			__skb_unlink(skb, &sk->sk_write_queue);
14541da177e4SLinus Torvalds 			skb_header_release(nskb);
14551da177e4SLinus Torvalds 			__skb_queue_head(&sk->sk_write_queue, nskb);
14561da177e4SLinus Torvalds 			sk_stream_free_skb(sk, skb);
14571da177e4SLinus Torvalds 			sk_charge_skb(sk, nskb);
14581da177e4SLinus Torvalds 			skb = nskb;
14591da177e4SLinus Torvalds 		}
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_ACK;
14621da177e4SLinus Torvalds 		TCP_ECN_send_synack(tcp_sk(sk), skb);
14631da177e4SLinus Torvalds 	}
14641da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->when = tcp_time_stamp;
14651da177e4SLinus Torvalds 	return tcp_transmit_skb(sk, skb_clone(skb, GFP_ATOMIC));
14661da177e4SLinus Torvalds }
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds /*
14691da177e4SLinus Torvalds  * Prepare a SYN-ACK.
14701da177e4SLinus Torvalds  */
14711da177e4SLinus Torvalds struct sk_buff * tcp_make_synack(struct sock *sk, struct dst_entry *dst,
147260236fddSArnaldo Carvalho de Melo 				 struct request_sock *req)
14731da177e4SLinus Torvalds {
14742e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq = inet_rsk(req);
14751da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
14761da177e4SLinus Torvalds 	struct tcphdr *th;
14771da177e4SLinus Torvalds 	int tcp_header_size;
14781da177e4SLinus Torvalds 	struct sk_buff *skb;
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 	skb = sock_wmalloc(sk, MAX_TCP_HEADER + 15, 1, GFP_ATOMIC);
14811da177e4SLinus Torvalds 	if (skb == NULL)
14821da177e4SLinus Torvalds 		return NULL;
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 	/* Reserve space for headers. */
14851da177e4SLinus Torvalds 	skb_reserve(skb, MAX_TCP_HEADER);
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds 	skb->dst = dst_clone(dst);
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 	tcp_header_size = (sizeof(struct tcphdr) + TCPOLEN_MSS +
14902e6599cbSArnaldo Carvalho de Melo 			   (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0) +
14912e6599cbSArnaldo Carvalho de Melo 			   (ireq->wscale_ok ? TCPOLEN_WSCALE_ALIGNED : 0) +
14921da177e4SLinus Torvalds 			   /* SACK_PERM is in the place of NOP NOP of TS */
14932e6599cbSArnaldo Carvalho de Melo 			   ((ireq->sack_ok && !ireq->tstamp_ok) ? TCPOLEN_SACKPERM_ALIGNED : 0));
14941da177e4SLinus Torvalds 	skb->h.th = th = (struct tcphdr *) skb_push(skb, tcp_header_size);
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds 	memset(th, 0, sizeof(struct tcphdr));
14971da177e4SLinus Torvalds 	th->syn = 1;
14981da177e4SLinus Torvalds 	th->ack = 1;
14991da177e4SLinus Torvalds 	if (dst->dev->features&NETIF_F_TSO)
15002e6599cbSArnaldo Carvalho de Melo 		ireq->ecn_ok = 0;
15011da177e4SLinus Torvalds 	TCP_ECN_make_synack(req, th);
15021da177e4SLinus Torvalds 	th->source = inet_sk(sk)->sport;
15032e6599cbSArnaldo Carvalho de Melo 	th->dest = ireq->rmt_port;
15042e6599cbSArnaldo Carvalho de Melo 	TCP_SKB_CB(skb)->seq = tcp_rsk(req)->snt_isn;
15051da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + 1;
15061da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->sacked = 0;
15071da177e4SLinus Torvalds 	skb_shinfo(skb)->tso_segs = 1;
15081da177e4SLinus Torvalds 	skb_shinfo(skb)->tso_size = 0;
15091da177e4SLinus Torvalds 	th->seq = htonl(TCP_SKB_CB(skb)->seq);
15102e6599cbSArnaldo Carvalho de Melo 	th->ack_seq = htonl(tcp_rsk(req)->rcv_isn + 1);
15111da177e4SLinus Torvalds 	if (req->rcv_wnd == 0) { /* ignored for retransmitted syns */
15121da177e4SLinus Torvalds 		__u8 rcv_wscale;
15131da177e4SLinus Torvalds 		/* Set this up on the first call only */
15141da177e4SLinus Torvalds 		req->window_clamp = tp->window_clamp ? : dst_metric(dst, RTAX_WINDOW);
15151da177e4SLinus Torvalds 		/* tcp_full_space because it is guaranteed to be the first packet */
15161da177e4SLinus Torvalds 		tcp_select_initial_window(tcp_full_space(sk),
15172e6599cbSArnaldo Carvalho de Melo 			dst_metric(dst, RTAX_ADVMSS) - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),
15181da177e4SLinus Torvalds 			&req->rcv_wnd,
15191da177e4SLinus Torvalds 			&req->window_clamp,
15202e6599cbSArnaldo Carvalho de Melo 			ireq->wscale_ok,
15211da177e4SLinus Torvalds 			&rcv_wscale);
15222e6599cbSArnaldo Carvalho de Melo 		ireq->rcv_wscale = rcv_wscale;
15231da177e4SLinus Torvalds 	}
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 	/* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */
15261da177e4SLinus Torvalds 	th->window = htons(req->rcv_wnd);
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->when = tcp_time_stamp;
15292e6599cbSArnaldo Carvalho de Melo 	tcp_syn_build_options((__u32 *)(th + 1), dst_metric(dst, RTAX_ADVMSS), ireq->tstamp_ok,
15302e6599cbSArnaldo Carvalho de Melo 			      ireq->sack_ok, ireq->wscale_ok, ireq->rcv_wscale,
15311da177e4SLinus Torvalds 			      TCP_SKB_CB(skb)->when,
15321da177e4SLinus Torvalds 			      req->ts_recent);
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 	skb->csum = 0;
15351da177e4SLinus Torvalds 	th->doff = (tcp_header_size >> 2);
15361da177e4SLinus Torvalds 	TCP_INC_STATS(TCP_MIB_OUTSEGS);
15371da177e4SLinus Torvalds 	return skb;
15381da177e4SLinus Torvalds }
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds /*
15411da177e4SLinus Torvalds  * Do all connect socket setups that can be done AF independent.
15421da177e4SLinus Torvalds  */
15431da177e4SLinus Torvalds static inline void tcp_connect_init(struct sock *sk)
15441da177e4SLinus Torvalds {
15451da177e4SLinus Torvalds 	struct dst_entry *dst = __sk_dst_get(sk);
15461da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
15471da177e4SLinus Torvalds 	__u8 rcv_wscale;
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	/* We'll fix this up when we get a response from the other end.
15501da177e4SLinus Torvalds 	 * See tcp_input.c:tcp_rcv_state_process case TCP_SYN_SENT.
15511da177e4SLinus Torvalds 	 */
15521da177e4SLinus Torvalds 	tp->tcp_header_len = sizeof(struct tcphdr) +
15531da177e4SLinus Torvalds 		(sysctl_tcp_timestamps ? TCPOLEN_TSTAMP_ALIGNED : 0);
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	/* If user gave his TCP_MAXSEG, record it to clamp */
15561da177e4SLinus Torvalds 	if (tp->rx_opt.user_mss)
15571da177e4SLinus Torvalds 		tp->rx_opt.mss_clamp = tp->rx_opt.user_mss;
15581da177e4SLinus Torvalds 	tp->max_window = 0;
15591da177e4SLinus Torvalds 	tcp_sync_mss(sk, dst_mtu(dst));
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	if (!tp->window_clamp)
15621da177e4SLinus Torvalds 		tp->window_clamp = dst_metric(dst, RTAX_WINDOW);
15631da177e4SLinus Torvalds 	tp->advmss = dst_metric(dst, RTAX_ADVMSS);
15641da177e4SLinus Torvalds 	tcp_initialize_rcv_mss(sk);
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 	tcp_select_initial_window(tcp_full_space(sk),
15671da177e4SLinus Torvalds 				  tp->advmss - (tp->rx_opt.ts_recent_stamp ? tp->tcp_header_len - sizeof(struct tcphdr) : 0),
15681da177e4SLinus Torvalds 				  &tp->rcv_wnd,
15691da177e4SLinus Torvalds 				  &tp->window_clamp,
15701da177e4SLinus Torvalds 				  sysctl_tcp_window_scaling,
15711da177e4SLinus Torvalds 				  &rcv_wscale);
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	tp->rx_opt.rcv_wscale = rcv_wscale;
15741da177e4SLinus Torvalds 	tp->rcv_ssthresh = tp->rcv_wnd;
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	sk->sk_err = 0;
15771da177e4SLinus Torvalds 	sock_reset_flag(sk, SOCK_DONE);
15781da177e4SLinus Torvalds 	tp->snd_wnd = 0;
15791da177e4SLinus Torvalds 	tcp_init_wl(tp, tp->write_seq, 0);
15801da177e4SLinus Torvalds 	tp->snd_una = tp->write_seq;
15811da177e4SLinus Torvalds 	tp->snd_sml = tp->write_seq;
15821da177e4SLinus Torvalds 	tp->rcv_nxt = 0;
15831da177e4SLinus Torvalds 	tp->rcv_wup = 0;
15841da177e4SLinus Torvalds 	tp->copied_seq = 0;
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	tp->rto = TCP_TIMEOUT_INIT;
15871da177e4SLinus Torvalds 	tp->retransmits = 0;
15881da177e4SLinus Torvalds 	tcp_clear_retrans(tp);
15891da177e4SLinus Torvalds }
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds /*
15921da177e4SLinus Torvalds  * Build a SYN and send it off.
15931da177e4SLinus Torvalds  */
15941da177e4SLinus Torvalds int tcp_connect(struct sock *sk)
15951da177e4SLinus Torvalds {
15961da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
15971da177e4SLinus Torvalds 	struct sk_buff *buff;
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	tcp_connect_init(sk);
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	buff = alloc_skb(MAX_TCP_HEADER + 15, sk->sk_allocation);
16021da177e4SLinus Torvalds 	if (unlikely(buff == NULL))
16031da177e4SLinus Torvalds 		return -ENOBUFS;
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 	/* Reserve space for headers. */
16061da177e4SLinus Torvalds 	skb_reserve(buff, MAX_TCP_HEADER);
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds 	TCP_SKB_CB(buff)->flags = TCPCB_FLAG_SYN;
16091da177e4SLinus Torvalds 	TCP_ECN_send_syn(sk, tp, buff);
16101da177e4SLinus Torvalds 	TCP_SKB_CB(buff)->sacked = 0;
16111da177e4SLinus Torvalds 	skb_shinfo(buff)->tso_segs = 1;
16121da177e4SLinus Torvalds 	skb_shinfo(buff)->tso_size = 0;
16131da177e4SLinus Torvalds 	buff->csum = 0;
16141da177e4SLinus Torvalds 	TCP_SKB_CB(buff)->seq = tp->write_seq++;
16151da177e4SLinus Torvalds 	TCP_SKB_CB(buff)->end_seq = tp->write_seq;
16161da177e4SLinus Torvalds 	tp->snd_nxt = tp->write_seq;
16171da177e4SLinus Torvalds 	tp->pushed_seq = tp->write_seq;
16181da177e4SLinus Torvalds 
16191da177e4SLinus Torvalds 	/* Send it off. */
16201da177e4SLinus Torvalds 	TCP_SKB_CB(buff)->when = tcp_time_stamp;
16211da177e4SLinus Torvalds 	tp->retrans_stamp = TCP_SKB_CB(buff)->when;
16221da177e4SLinus Torvalds 	skb_header_release(buff);
16231da177e4SLinus Torvalds 	__skb_queue_tail(&sk->sk_write_queue, buff);
16241da177e4SLinus Torvalds 	sk_charge_skb(sk, buff);
16251da177e4SLinus Torvalds 	tp->packets_out += tcp_skb_pcount(buff);
16261da177e4SLinus Torvalds 	tcp_transmit_skb(sk, skb_clone(buff, GFP_KERNEL));
16271da177e4SLinus Torvalds 	TCP_INC_STATS(TCP_MIB_ACTIVEOPENS);
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	/* Timer for repeating the SYN until an answer. */
16301da177e4SLinus Torvalds 	tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
16311da177e4SLinus Torvalds 	return 0;
16321da177e4SLinus Torvalds }
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds /* Send out a delayed ack, the caller does the policy checking
16351da177e4SLinus Torvalds  * to see if we should even be here.  See tcp_input.c:tcp_ack_snd_check()
16361da177e4SLinus Torvalds  * for details.
16371da177e4SLinus Torvalds  */
16381da177e4SLinus Torvalds void tcp_send_delayed_ack(struct sock *sk)
16391da177e4SLinus Torvalds {
16401da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
16411da177e4SLinus Torvalds 	int ato = tp->ack.ato;
16421da177e4SLinus Torvalds 	unsigned long timeout;
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 	if (ato > TCP_DELACK_MIN) {
16451da177e4SLinus Torvalds 		int max_ato = HZ/2;
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 		if (tp->ack.pingpong || (tp->ack.pending&TCP_ACK_PUSHED))
16481da177e4SLinus Torvalds 			max_ato = TCP_DELACK_MAX;
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 		/* Slow path, intersegment interval is "high". */
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 		/* If some rtt estimate is known, use it to bound delayed ack.
16531da177e4SLinus Torvalds 		 * Do not use tp->rto here, use results of rtt measurements
16541da177e4SLinus Torvalds 		 * directly.
16551da177e4SLinus Torvalds 		 */
16561da177e4SLinus Torvalds 		if (tp->srtt) {
16571da177e4SLinus Torvalds 			int rtt = max(tp->srtt>>3, TCP_DELACK_MIN);
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds 			if (rtt < max_ato)
16601da177e4SLinus Torvalds 				max_ato = rtt;
16611da177e4SLinus Torvalds 		}
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 		ato = min(ato, max_ato);
16641da177e4SLinus Torvalds 	}
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 	/* Stay within the limit we were given */
16671da177e4SLinus Torvalds 	timeout = jiffies + ato;
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds 	/* Use new timeout only if there wasn't a older one earlier. */
16701da177e4SLinus Torvalds 	if (tp->ack.pending&TCP_ACK_TIMER) {
16711da177e4SLinus Torvalds 		/* If delack timer was blocked or is about to expire,
16721da177e4SLinus Torvalds 		 * send ACK now.
16731da177e4SLinus Torvalds 		 */
16741da177e4SLinus Torvalds 		if (tp->ack.blocked || time_before_eq(tp->ack.timeout, jiffies+(ato>>2))) {
16751da177e4SLinus Torvalds 			tcp_send_ack(sk);
16761da177e4SLinus Torvalds 			return;
16771da177e4SLinus Torvalds 		}
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 		if (!time_before(timeout, tp->ack.timeout))
16801da177e4SLinus Torvalds 			timeout = tp->ack.timeout;
16811da177e4SLinus Torvalds 	}
16821da177e4SLinus Torvalds 	tp->ack.pending |= TCP_ACK_SCHED|TCP_ACK_TIMER;
16831da177e4SLinus Torvalds 	tp->ack.timeout = timeout;
16841da177e4SLinus Torvalds 	sk_reset_timer(sk, &tp->delack_timer, timeout);
16851da177e4SLinus Torvalds }
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds /* This routine sends an ack and also updates the window. */
16881da177e4SLinus Torvalds void tcp_send_ack(struct sock *sk)
16891da177e4SLinus Torvalds {
16901da177e4SLinus Torvalds 	/* If we have been reset, we may not send again. */
16911da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE) {
16921da177e4SLinus Torvalds 		struct tcp_sock *tp = tcp_sk(sk);
16931da177e4SLinus Torvalds 		struct sk_buff *buff;
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 		/* We are not putting this on the write queue, so
16961da177e4SLinus Torvalds 		 * tcp_transmit_skb() will set the ownership to this
16971da177e4SLinus Torvalds 		 * sock.
16981da177e4SLinus Torvalds 		 */
16991da177e4SLinus Torvalds 		buff = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
17001da177e4SLinus Torvalds 		if (buff == NULL) {
17011da177e4SLinus Torvalds 			tcp_schedule_ack(tp);
17021da177e4SLinus Torvalds 			tp->ack.ato = TCP_ATO_MIN;
17031da177e4SLinus Torvalds 			tcp_reset_xmit_timer(sk, TCP_TIME_DACK, TCP_DELACK_MAX);
17041da177e4SLinus Torvalds 			return;
17051da177e4SLinus Torvalds 		}
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 		/* Reserve space for headers and prepare control bits. */
17081da177e4SLinus Torvalds 		skb_reserve(buff, MAX_TCP_HEADER);
17091da177e4SLinus Torvalds 		buff->csum = 0;
17101da177e4SLinus Torvalds 		TCP_SKB_CB(buff)->flags = TCPCB_FLAG_ACK;
17111da177e4SLinus Torvalds 		TCP_SKB_CB(buff)->sacked = 0;
17121da177e4SLinus Torvalds 		skb_shinfo(buff)->tso_segs = 1;
17131da177e4SLinus Torvalds 		skb_shinfo(buff)->tso_size = 0;
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds 		/* Send it off, this clears delayed acks for us. */
17161da177e4SLinus Torvalds 		TCP_SKB_CB(buff)->seq = TCP_SKB_CB(buff)->end_seq = tcp_acceptable_seq(sk, tp);
17171da177e4SLinus Torvalds 		TCP_SKB_CB(buff)->when = tcp_time_stamp;
17181da177e4SLinus Torvalds 		tcp_transmit_skb(sk, buff);
17191da177e4SLinus Torvalds 	}
17201da177e4SLinus Torvalds }
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds /* This routine sends a packet with an out of date sequence
17231da177e4SLinus Torvalds  * number. It assumes the other end will try to ack it.
17241da177e4SLinus Torvalds  *
17251da177e4SLinus Torvalds  * Question: what should we make while urgent mode?
17261da177e4SLinus Torvalds  * 4.4BSD forces sending single byte of data. We cannot send
17271da177e4SLinus Torvalds  * out of window data, because we have SND.NXT==SND.MAX...
17281da177e4SLinus Torvalds  *
17291da177e4SLinus Torvalds  * Current solution: to send TWO zero-length segments in urgent mode:
17301da177e4SLinus Torvalds  * one is with SEG.SEQ=SND.UNA to deliver urgent pointer, another is
17311da177e4SLinus Torvalds  * out-of-date with SND.UNA-1 to probe window.
17321da177e4SLinus Torvalds  */
17331da177e4SLinus Torvalds static int tcp_xmit_probe_skb(struct sock *sk, int urgent)
17341da177e4SLinus Torvalds {
17351da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
17361da177e4SLinus Torvalds 	struct sk_buff *skb;
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	/* We don't queue it, tcp_transmit_skb() sets ownership. */
17391da177e4SLinus Torvalds 	skb = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
17401da177e4SLinus Torvalds 	if (skb == NULL)
17411da177e4SLinus Torvalds 		return -1;
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds 	/* Reserve space for headers and set control bits. */
17441da177e4SLinus Torvalds 	skb_reserve(skb, MAX_TCP_HEADER);
17451da177e4SLinus Torvalds 	skb->csum = 0;
17461da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->flags = TCPCB_FLAG_ACK;
17471da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->sacked = urgent;
17481da177e4SLinus Torvalds 	skb_shinfo(skb)->tso_segs = 1;
17491da177e4SLinus Torvalds 	skb_shinfo(skb)->tso_size = 0;
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 	/* Use a previous sequence.  This should cause the other
17521da177e4SLinus Torvalds 	 * end to send an ack.  Don't queue or clone SKB, just
17531da177e4SLinus Torvalds 	 * send it.
17541da177e4SLinus Torvalds 	 */
17551da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->seq = urgent ? tp->snd_una : tp->snd_una - 1;
17561da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq;
17571da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->when = tcp_time_stamp;
17581da177e4SLinus Torvalds 	return tcp_transmit_skb(sk, skb);
17591da177e4SLinus Torvalds }
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds int tcp_write_wakeup(struct sock *sk)
17621da177e4SLinus Torvalds {
17631da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE) {
17641da177e4SLinus Torvalds 		struct tcp_sock *tp = tcp_sk(sk);
17651da177e4SLinus Torvalds 		struct sk_buff *skb;
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds 		if ((skb = sk->sk_send_head) != NULL &&
17681da177e4SLinus Torvalds 		    before(TCP_SKB_CB(skb)->seq, tp->snd_una+tp->snd_wnd)) {
17691da177e4SLinus Torvalds 			int err;
17701da177e4SLinus Torvalds 			unsigned int mss = tcp_current_mss(sk, 0);
17711da177e4SLinus Torvalds 			unsigned int seg_size = tp->snd_una+tp->snd_wnd-TCP_SKB_CB(skb)->seq;
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 			if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq))
17741da177e4SLinus Torvalds 				tp->pushed_seq = TCP_SKB_CB(skb)->end_seq;
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 			/* We are probing the opening of a window
17771da177e4SLinus Torvalds 			 * but the window size is != 0
17781da177e4SLinus Torvalds 			 * must have been a result SWS avoidance ( sender )
17791da177e4SLinus Torvalds 			 */
17801da177e4SLinus Torvalds 			if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq ||
17811da177e4SLinus Torvalds 			    skb->len > mss) {
17821da177e4SLinus Torvalds 				seg_size = min(seg_size, mss);
17831da177e4SLinus Torvalds 				TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
17841da177e4SLinus Torvalds 				if (tcp_fragment(sk, skb, seg_size))
17851da177e4SLinus Torvalds 					return -1;
17861da177e4SLinus Torvalds 				/* SWS override triggered forced fragmentation.
17871da177e4SLinus Torvalds 				 * Disable TSO, the connection is too sick. */
17881da177e4SLinus Torvalds 				if (sk->sk_route_caps & NETIF_F_TSO) {
17891da177e4SLinus Torvalds 					sock_set_flag(sk, SOCK_NO_LARGESEND);
17901da177e4SLinus Torvalds 					sk->sk_route_caps &= ~NETIF_F_TSO;
17911da177e4SLinus Torvalds 					tp->mss_cache = tp->mss_cache_std;
17921da177e4SLinus Torvalds 				}
17931da177e4SLinus Torvalds 			} else if (!tcp_skb_pcount(skb))
1794d5ac99a6SDavid S. Miller 				tcp_set_skb_tso_segs(sk, skb);
17951da177e4SLinus Torvalds 
17961da177e4SLinus Torvalds 			TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
17971da177e4SLinus Torvalds 			TCP_SKB_CB(skb)->when = tcp_time_stamp;
17981da177e4SLinus Torvalds 			tcp_tso_set_push(skb);
17991da177e4SLinus Torvalds 			err = tcp_transmit_skb(sk, skb_clone(skb, GFP_ATOMIC));
18001da177e4SLinus Torvalds 			if (!err) {
18011da177e4SLinus Torvalds 				update_send_head(sk, tp, skb);
18021da177e4SLinus Torvalds 			}
18031da177e4SLinus Torvalds 			return err;
18041da177e4SLinus Torvalds 		} else {
18051da177e4SLinus Torvalds 			if (tp->urg_mode &&
18061da177e4SLinus Torvalds 			    between(tp->snd_up, tp->snd_una+1, tp->snd_una+0xFFFF))
18071da177e4SLinus Torvalds 				tcp_xmit_probe_skb(sk, TCPCB_URG);
18081da177e4SLinus Torvalds 			return tcp_xmit_probe_skb(sk, 0);
18091da177e4SLinus Torvalds 		}
18101da177e4SLinus Torvalds 	}
18111da177e4SLinus Torvalds 	return -1;
18121da177e4SLinus Torvalds }
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds /* A window probe timeout has occurred.  If window is not closed send
18151da177e4SLinus Torvalds  * a partial packet else a zero probe.
18161da177e4SLinus Torvalds  */
18171da177e4SLinus Torvalds void tcp_send_probe0(struct sock *sk)
18181da177e4SLinus Torvalds {
18191da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
18201da177e4SLinus Torvalds 	int err;
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 	err = tcp_write_wakeup(sk);
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds 	if (tp->packets_out || !sk->sk_send_head) {
18251da177e4SLinus Torvalds 		/* Cancel probe timer, if it is not required. */
18261da177e4SLinus Torvalds 		tp->probes_out = 0;
18271da177e4SLinus Torvalds 		tp->backoff = 0;
18281da177e4SLinus Torvalds 		return;
18291da177e4SLinus Torvalds 	}
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds 	if (err <= 0) {
18321da177e4SLinus Torvalds 		if (tp->backoff < sysctl_tcp_retries2)
18331da177e4SLinus Torvalds 			tp->backoff++;
18341da177e4SLinus Torvalds 		tp->probes_out++;
18351da177e4SLinus Torvalds 		tcp_reset_xmit_timer (sk, TCP_TIME_PROBE0,
18361da177e4SLinus Torvalds 				      min(tp->rto << tp->backoff, TCP_RTO_MAX));
18371da177e4SLinus Torvalds 	} else {
18381da177e4SLinus Torvalds 		/* If packet was not sent due to local congestion,
18391da177e4SLinus Torvalds 		 * do not backoff and do not remember probes_out.
18401da177e4SLinus Torvalds 		 * Let local senders to fight for local resources.
18411da177e4SLinus Torvalds 		 *
18421da177e4SLinus Torvalds 		 * Use accumulated backoff yet.
18431da177e4SLinus Torvalds 		 */
18441da177e4SLinus Torvalds 		if (!tp->probes_out)
18451da177e4SLinus Torvalds 			tp->probes_out=1;
18461da177e4SLinus Torvalds 		tcp_reset_xmit_timer (sk, TCP_TIME_PROBE0,
18471da177e4SLinus Torvalds 				      min(tp->rto << tp->backoff, TCP_RESOURCE_PROBE_INTERVAL));
18481da177e4SLinus Torvalds 	}
18491da177e4SLinus Torvalds }
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_connect);
18521da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_make_synack);
18531da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_simple_retransmit);
18541da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_sync_mss);
1855