xref: /openbmc/linux/net/ipv4/tcp_minisocks.c (revision a915da9b69273815527ccb3789421cb7027b545b)
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  *
802c30a84SJesper Juhl  * Authors:	Ross Biro
91da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
101da177e4SLinus Torvalds  *		Mark Evans, <evansmp@uhura.aston.ac.uk>
111da177e4SLinus Torvalds  *		Corey Minyard <wf-rch!minyard@relay.EU.net>
121da177e4SLinus Torvalds  *		Florian La Roche, <flla@stud.uni-sb.de>
131da177e4SLinus Torvalds  *		Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
141da177e4SLinus Torvalds  *		Linus Torvalds, <torvalds@cs.helsinki.fi>
151da177e4SLinus Torvalds  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
161da177e4SLinus Torvalds  *		Matthew Dillon, <dillon@apollo.west.oic.com>
171da177e4SLinus Torvalds  *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
181da177e4SLinus Torvalds  *		Jorge Cwik, <jorge@laser.satlink.net>
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <linux/mm.h>
221da177e4SLinus Torvalds #include <linux/module.h>
235a0e3ad6STejun Heo #include <linux/slab.h>
241da177e4SLinus Torvalds #include <linux/sysctl.h>
251da177e4SLinus Torvalds #include <linux/workqueue.h>
261da177e4SLinus Torvalds #include <net/tcp.h>
271da177e4SLinus Torvalds #include <net/inet_common.h>
281da177e4SLinus Torvalds #include <net/xfrm.h>
291da177e4SLinus Torvalds 
30e994b7c9SDavid S. Miller int sysctl_tcp_syncookies __read_mostly = 1;
31c6aefafbSGlenn Griffin EXPORT_SYMBOL(sysctl_tcp_syncookies);
32c6aefafbSGlenn Griffin 
33ab32ea5dSBrian Haley int sysctl_tcp_abort_on_overflow __read_mostly;
341da177e4SLinus Torvalds 
35295ff7edSArnaldo Carvalho de Melo struct inet_timewait_death_row tcp_death_row = {
36295ff7edSArnaldo Carvalho de Melo 	.sysctl_max_tw_buckets = NR_FILE * 2,
37295ff7edSArnaldo Carvalho de Melo 	.period		= TCP_TIMEWAIT_LEN / INET_TWDR_TWKILL_SLOTS,
38e4d91918SIngo Molnar 	.death_lock	= __SPIN_LOCK_UNLOCKED(tcp_death_row.death_lock),
39295ff7edSArnaldo Carvalho de Melo 	.hashinfo	= &tcp_hashinfo,
40295ff7edSArnaldo Carvalho de Melo 	.tw_timer	= TIMER_INITIALIZER(inet_twdr_hangman, 0,
41295ff7edSArnaldo Carvalho de Melo 					    (unsigned long)&tcp_death_row),
42295ff7edSArnaldo Carvalho de Melo 	.twkill_work	= __WORK_INITIALIZER(tcp_death_row.twkill_work,
4365f27f38SDavid Howells 					     inet_twdr_twkill_work),
44295ff7edSArnaldo Carvalho de Melo /* Short-time timewait calendar */
45295ff7edSArnaldo Carvalho de Melo 
46295ff7edSArnaldo Carvalho de Melo 	.twcal_hand	= -1,
47295ff7edSArnaldo Carvalho de Melo 	.twcal_timer	= TIMER_INITIALIZER(inet_twdr_twcal_tick, 0,
48295ff7edSArnaldo Carvalho de Melo 					    (unsigned long)&tcp_death_row),
49295ff7edSArnaldo Carvalho de Melo };
50295ff7edSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(tcp_death_row);
51295ff7edSArnaldo Carvalho de Melo 
523f419d2dSDavid S. Miller /* VJ's idea. Save last timestamp seen from this destination
533f419d2dSDavid S. Miller  * and hold it at least for normal timewait interval to use for duplicate
543f419d2dSDavid S. Miller  * segment detection in subsequent connections, before they enter synchronized
553f419d2dSDavid S. Miller  * state.
563f419d2dSDavid S. Miller  */
573f419d2dSDavid S. Miller 
583f419d2dSDavid S. Miller static int tcp_remember_stamp(struct sock *sk)
593f419d2dSDavid S. Miller {
603f419d2dSDavid S. Miller 	const struct inet_connection_sock *icsk = inet_csk(sk);
613f419d2dSDavid S. Miller 	struct tcp_sock *tp = tcp_sk(sk);
623f419d2dSDavid S. Miller 	struct inet_peer *peer;
633f419d2dSDavid S. Miller 	bool release_it;
643f419d2dSDavid S. Miller 
653f419d2dSDavid S. Miller 	peer = icsk->icsk_af_ops->get_peer(sk, &release_it);
663f419d2dSDavid S. Miller 	if (peer) {
673f419d2dSDavid S. Miller 		if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 ||
683f419d2dSDavid S. Miller 		    ((u32)get_seconds() - peer->tcp_ts_stamp > TCP_PAWS_MSL &&
693f419d2dSDavid S. Miller 		     peer->tcp_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
703f419d2dSDavid S. Miller 			peer->tcp_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
713f419d2dSDavid S. Miller 			peer->tcp_ts = tp->rx_opt.ts_recent;
723f419d2dSDavid S. Miller 		}
733f419d2dSDavid S. Miller 		if (release_it)
743f419d2dSDavid S. Miller 			inet_putpeer(peer);
753f419d2dSDavid S. Miller 		return 1;
763f419d2dSDavid S. Miller 	}
773f419d2dSDavid S. Miller 
783f419d2dSDavid S. Miller 	return 0;
793f419d2dSDavid S. Miller }
803f419d2dSDavid S. Miller 
81ccb7c410SDavid S. Miller static int tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
82ccb7c410SDavid S. Miller {
83ccb7c410SDavid S. Miller 	struct sock *sk = (struct sock *) tw;
84ccb7c410SDavid S. Miller 	struct inet_peer *peer;
85ccb7c410SDavid S. Miller 
86ccb7c410SDavid S. Miller 	peer = twsk_getpeer(sk);
87ccb7c410SDavid S. Miller 	if (peer) {
88ccb7c410SDavid S. Miller 		const struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
89ccb7c410SDavid S. Miller 
90ccb7c410SDavid S. Miller 		if ((s32)(peer->tcp_ts - tcptw->tw_ts_recent) <= 0 ||
91ccb7c410SDavid S. Miller 		    ((u32)get_seconds() - peer->tcp_ts_stamp > TCP_PAWS_MSL &&
92ccb7c410SDavid S. Miller 		     peer->tcp_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
93ccb7c410SDavid S. Miller 			peer->tcp_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
94ccb7c410SDavid S. Miller 			peer->tcp_ts	   = tcptw->tw_ts_recent;
95ccb7c410SDavid S. Miller 		}
96ccb7c410SDavid S. Miller 		inet_putpeer(peer);
97ccb7c410SDavid S. Miller 		return 1;
98ccb7c410SDavid S. Miller 	}
99ccb7c410SDavid S. Miller 	return 0;
100ccb7c410SDavid S. Miller }
101ccb7c410SDavid S. Miller 
1021da177e4SLinus Torvalds static __inline__ int tcp_in_window(u32 seq, u32 end_seq, u32 s_win, u32 e_win)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	if (seq == s_win)
1051da177e4SLinus Torvalds 		return 1;
1061da177e4SLinus Torvalds 	if (after(end_seq, s_win) && before(seq, e_win))
1071da177e4SLinus Torvalds 		return 1;
108a02cec21SEric Dumazet 	return seq == e_win && seq == end_seq;
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /*
1121da177e4SLinus Torvalds  * * Main purpose of TIME-WAIT state is to close connection gracefully,
1131da177e4SLinus Torvalds  *   when one of ends sits in LAST-ACK or CLOSING retransmitting FIN
1141da177e4SLinus Torvalds  *   (and, probably, tail of data) and one or more our ACKs are lost.
1151da177e4SLinus Torvalds  * * What is TIME-WAIT timeout? It is associated with maximal packet
1161da177e4SLinus Torvalds  *   lifetime in the internet, which results in wrong conclusion, that
1171da177e4SLinus Torvalds  *   it is set to catch "old duplicate segments" wandering out of their path.
1181da177e4SLinus Torvalds  *   It is not quite correct. This timeout is calculated so that it exceeds
1191da177e4SLinus Torvalds  *   maximal retransmission timeout enough to allow to lose one (or more)
1201da177e4SLinus Torvalds  *   segments sent by peer and our ACKs. This time may be calculated from RTO.
1211da177e4SLinus Torvalds  * * When TIME-WAIT socket receives RST, it means that another end
1221da177e4SLinus Torvalds  *   finally closed and we are allowed to kill TIME-WAIT too.
1231da177e4SLinus Torvalds  * * Second purpose of TIME-WAIT is catching old duplicate segments.
1241da177e4SLinus Torvalds  *   Well, certainly it is pure paranoia, but if we load TIME-WAIT
1251da177e4SLinus Torvalds  *   with this semantics, we MUST NOT kill TIME-WAIT state with RSTs.
1261da177e4SLinus Torvalds  * * If we invented some more clever way to catch duplicates
1271da177e4SLinus Torvalds  *   (f.e. based on PAWS), we could truncate TIME-WAIT to several RTOs.
1281da177e4SLinus Torvalds  *
1291da177e4SLinus Torvalds  * The algorithm below is based on FORMAL INTERPRETATION of RFCs.
1301da177e4SLinus Torvalds  * When you compare it to RFCs, please, read section SEGMENT ARRIVES
1311da177e4SLinus Torvalds  * from the very beginning.
1321da177e4SLinus Torvalds  *
1331da177e4SLinus Torvalds  * NOTE. With recycling (and later with fin-wait-2) TW bucket
1341da177e4SLinus Torvalds  * is _not_ stateless. It means, that strictly speaking we must
1351da177e4SLinus Torvalds  * spinlock it. I do not want! Well, probability of misbehaviour
1361da177e4SLinus Torvalds  * is ridiculously low and, seems, we could use some mb() tricks
1371da177e4SLinus Torvalds  * to avoid misread sequence numbers, states etc.  --ANK
1381da177e4SLinus Torvalds  */
1391da177e4SLinus Torvalds enum tcp_tw_status
1408feaf0c0SArnaldo Carvalho de Melo tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
1418feaf0c0SArnaldo Carvalho de Melo 			   const struct tcphdr *th)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	struct tcp_options_received tmp_opt;
144cf533ea5SEric Dumazet 	const u8 *hash_location;
1454957faadSWilliam Allen Simpson 	struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
1461da177e4SLinus Torvalds 	int paws_reject = 0;
1471da177e4SLinus Torvalds 
148bb5b7c11SDavid S. Miller 	tmp_opt.saw_tstamp = 0;
1498feaf0c0SArnaldo Carvalho de Melo 	if (th->doff > (sizeof(*th) >> 2) && tcptw->tw_ts_recent_stamp) {
150bb5b7c11SDavid S. Miller 		tcp_parse_options(skb, &tmp_opt, &hash_location, 0);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 		if (tmp_opt.saw_tstamp) {
1538feaf0c0SArnaldo Carvalho de Melo 			tmp_opt.ts_recent	= tcptw->tw_ts_recent;
1548feaf0c0SArnaldo Carvalho de Melo 			tmp_opt.ts_recent_stamp	= tcptw->tw_ts_recent_stamp;
155c887e6d2SIlpo Järvinen 			paws_reject = tcp_paws_reject(&tmp_opt, th->rst);
1561da177e4SLinus Torvalds 		}
1571da177e4SLinus Torvalds 	}
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	if (tw->tw_substate == TCP_FIN_WAIT2) {
1601da177e4SLinus Torvalds 		/* Just repeat all the checks of tcp_rcv_state_process() */
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 		/* Out of window, send ACK */
1631da177e4SLinus Torvalds 		if (paws_reject ||
1641da177e4SLinus Torvalds 		    !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
1658feaf0c0SArnaldo Carvalho de Melo 				   tcptw->tw_rcv_nxt,
1668feaf0c0SArnaldo Carvalho de Melo 				   tcptw->tw_rcv_nxt + tcptw->tw_rcv_wnd))
1671da177e4SLinus Torvalds 			return TCP_TW_ACK;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 		if (th->rst)
1701da177e4SLinus Torvalds 			goto kill;
1711da177e4SLinus Torvalds 
1728feaf0c0SArnaldo Carvalho de Melo 		if (th->syn && !before(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt))
1731da177e4SLinus Torvalds 			goto kill_with_rst;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 		/* Dup ACK? */
1761ac530b3SWei Yongjun 		if (!th->ack ||
1771ac530b3SWei Yongjun 		    !after(TCP_SKB_CB(skb)->end_seq, tcptw->tw_rcv_nxt) ||
1781da177e4SLinus Torvalds 		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) {
1798feaf0c0SArnaldo Carvalho de Melo 			inet_twsk_put(tw);
1801da177e4SLinus Torvalds 			return TCP_TW_SUCCESS;
1811da177e4SLinus Torvalds 		}
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 		/* New data or FIN. If new data arrive after half-duplex close,
1841da177e4SLinus Torvalds 		 * reset.
1851da177e4SLinus Torvalds 		 */
1861da177e4SLinus Torvalds 		if (!th->fin ||
1878feaf0c0SArnaldo Carvalho de Melo 		    TCP_SKB_CB(skb)->end_seq != tcptw->tw_rcv_nxt + 1) {
1881da177e4SLinus Torvalds kill_with_rst:
189295ff7edSArnaldo Carvalho de Melo 			inet_twsk_deschedule(tw, &tcp_death_row);
1908feaf0c0SArnaldo Carvalho de Melo 			inet_twsk_put(tw);
1911da177e4SLinus Torvalds 			return TCP_TW_RST;
1921da177e4SLinus Torvalds 		}
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 		/* FIN arrived, enter true time-wait state. */
1951da177e4SLinus Torvalds 		tw->tw_substate	  = TCP_TIME_WAIT;
1968feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_rcv_nxt = TCP_SKB_CB(skb)->end_seq;
1971da177e4SLinus Torvalds 		if (tmp_opt.saw_tstamp) {
1989d729f72SJames Morris 			tcptw->tw_ts_recent_stamp = get_seconds();
1998feaf0c0SArnaldo Carvalho de Melo 			tcptw->tw_ts_recent	  = tmp_opt.rcv_tsval;
2001da177e4SLinus Torvalds 		}
2011da177e4SLinus Torvalds 
202ccb7c410SDavid S. Miller 		if (tcp_death_row.sysctl_tw_recycle &&
203ccb7c410SDavid S. Miller 		    tcptw->tw_ts_recent_stamp &&
204ccb7c410SDavid S. Miller 		    tcp_tw_remember_stamp(tw))
205696ab2d3SArnaldo Carvalho de Melo 			inet_twsk_schedule(tw, &tcp_death_row, tw->tw_timeout,
206696ab2d3SArnaldo Carvalho de Melo 					   TCP_TIMEWAIT_LEN);
2071da177e4SLinus Torvalds 		else
208696ab2d3SArnaldo Carvalho de Melo 			inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
209696ab2d3SArnaldo Carvalho de Melo 					   TCP_TIMEWAIT_LEN);
2101da177e4SLinus Torvalds 		return TCP_TW_ACK;
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	/*
2141da177e4SLinus Torvalds 	 *	Now real TIME-WAIT state.
2151da177e4SLinus Torvalds 	 *
2161da177e4SLinus Torvalds 	 *	RFC 1122:
2171da177e4SLinus Torvalds 	 *	"When a connection is [...] on TIME-WAIT state [...]
2181da177e4SLinus Torvalds 	 *	[a TCP] MAY accept a new SYN from the remote TCP to
2191da177e4SLinus Torvalds 	 *	reopen the connection directly, if it:
2201da177e4SLinus Torvalds 	 *
2211da177e4SLinus Torvalds 	 *	(1)  assigns its initial sequence number for the new
2221da177e4SLinus Torvalds 	 *	connection to be larger than the largest sequence
2231da177e4SLinus Torvalds 	 *	number it used on the previous connection incarnation,
2241da177e4SLinus Torvalds 	 *	and
2251da177e4SLinus Torvalds 	 *
2261da177e4SLinus Torvalds 	 *	(2)  returns to TIME-WAIT state if the SYN turns out
2271da177e4SLinus Torvalds 	 *	to be an old duplicate".
2281da177e4SLinus Torvalds 	 */
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	if (!paws_reject &&
2318feaf0c0SArnaldo Carvalho de Melo 	    (TCP_SKB_CB(skb)->seq == tcptw->tw_rcv_nxt &&
2321da177e4SLinus Torvalds 	     (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq || th->rst))) {
2331da177e4SLinus Torvalds 		/* In window segment, it may be only reset or bare ack. */
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 		if (th->rst) {
236caa20d9aSStephen Hemminger 			/* This is TIME_WAIT assassination, in two flavors.
2371da177e4SLinus Torvalds 			 * Oh well... nobody has a sufficient solution to this
2381da177e4SLinus Torvalds 			 * protocol bug yet.
2391da177e4SLinus Torvalds 			 */
2401da177e4SLinus Torvalds 			if (sysctl_tcp_rfc1337 == 0) {
2411da177e4SLinus Torvalds kill:
242295ff7edSArnaldo Carvalho de Melo 				inet_twsk_deschedule(tw, &tcp_death_row);
2438feaf0c0SArnaldo Carvalho de Melo 				inet_twsk_put(tw);
2441da177e4SLinus Torvalds 				return TCP_TW_SUCCESS;
2451da177e4SLinus Torvalds 			}
2461da177e4SLinus Torvalds 		}
247696ab2d3SArnaldo Carvalho de Melo 		inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
248696ab2d3SArnaldo Carvalho de Melo 				   TCP_TIMEWAIT_LEN);
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 		if (tmp_opt.saw_tstamp) {
2518feaf0c0SArnaldo Carvalho de Melo 			tcptw->tw_ts_recent	  = tmp_opt.rcv_tsval;
2529d729f72SJames Morris 			tcptw->tw_ts_recent_stamp = get_seconds();
2531da177e4SLinus Torvalds 		}
2541da177e4SLinus Torvalds 
2558feaf0c0SArnaldo Carvalho de Melo 		inet_twsk_put(tw);
2561da177e4SLinus Torvalds 		return TCP_TW_SUCCESS;
2571da177e4SLinus Torvalds 	}
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	/* Out of window segment.
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	   All the segments are ACKed immediately.
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	   The only exception is new SYN. We accept it, if it is
2641da177e4SLinus Torvalds 	   not old duplicate and we are not in danger to be killed
2651da177e4SLinus Torvalds 	   by delayed old duplicates. RFC check is that it has
2661da177e4SLinus Torvalds 	   newer sequence number works at rates <40Mbit/sec.
2671da177e4SLinus Torvalds 	   However, if paws works, it is reliable AND even more,
2681da177e4SLinus Torvalds 	   we even may relax silly seq space cutoff.
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	   RED-PEN: we violate main RFC requirement, if this SYN will appear
2711da177e4SLinus Torvalds 	   old duplicate (i.e. we receive RST in reply to SYN-ACK),
2721da177e4SLinus Torvalds 	   we must return socket to time-wait state. It is not good,
2731da177e4SLinus Torvalds 	   but not fatal yet.
2741da177e4SLinus Torvalds 	 */
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	if (th->syn && !th->rst && !th->ack && !paws_reject &&
2778feaf0c0SArnaldo Carvalho de Melo 	    (after(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt) ||
2788feaf0c0SArnaldo Carvalho de Melo 	     (tmp_opt.saw_tstamp &&
2798feaf0c0SArnaldo Carvalho de Melo 	      (s32)(tcptw->tw_ts_recent - tmp_opt.rcv_tsval) < 0))) {
2808feaf0c0SArnaldo Carvalho de Melo 		u32 isn = tcptw->tw_snd_nxt + 65535 + 2;
2811da177e4SLinus Torvalds 		if (isn == 0)
2821da177e4SLinus Torvalds 			isn++;
2831da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->when = isn;
2841da177e4SLinus Torvalds 		return TCP_TW_SYN;
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	if (paws_reject)
288de0744afSPavel Emelyanov 		NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_PAWSESTABREJECTED);
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	if (!th->rst) {
2911da177e4SLinus Torvalds 		/* In this case we must reset the TIMEWAIT timer.
2921da177e4SLinus Torvalds 		 *
2931da177e4SLinus Torvalds 		 * If it is ACKless SYN it may be both old duplicate
2941da177e4SLinus Torvalds 		 * and new good SYN with random sequence number <rcv_nxt.
2951da177e4SLinus Torvalds 		 * Do not reschedule in the last case.
2961da177e4SLinus Torvalds 		 */
2971da177e4SLinus Torvalds 		if (paws_reject || th->ack)
298696ab2d3SArnaldo Carvalho de Melo 			inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
299696ab2d3SArnaldo Carvalho de Melo 					   TCP_TIMEWAIT_LEN);
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 		/* Send ACK. Note, we do not put the bucket,
3021da177e4SLinus Torvalds 		 * it will be released by caller.
3031da177e4SLinus Torvalds 		 */
3041da177e4SLinus Torvalds 		return TCP_TW_ACK;
3051da177e4SLinus Torvalds 	}
3068feaf0c0SArnaldo Carvalho de Melo 	inet_twsk_put(tw);
3071da177e4SLinus Torvalds 	return TCP_TW_SUCCESS;
3081da177e4SLinus Torvalds }
3094bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_timewait_state_process);
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds /*
3121da177e4SLinus Torvalds  * Move a socket to time-wait or dead fin-wait-2 state.
3131da177e4SLinus Torvalds  */
3141da177e4SLinus Torvalds void tcp_time_wait(struct sock *sk, int state, int timeo)
3151da177e4SLinus Torvalds {
3168feaf0c0SArnaldo Carvalho de Melo 	struct inet_timewait_sock *tw = NULL;
3178292a17aSArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
3188feaf0c0SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
3191da177e4SLinus Torvalds 	int recycle_ok = 0;
3201da177e4SLinus Torvalds 
321295ff7edSArnaldo Carvalho de Melo 	if (tcp_death_row.sysctl_tw_recycle && tp->rx_opt.ts_recent_stamp)
3223f419d2dSDavid S. Miller 		recycle_ok = tcp_remember_stamp(sk);
3231da177e4SLinus Torvalds 
324295ff7edSArnaldo Carvalho de Melo 	if (tcp_death_row.tw_count < tcp_death_row.sysctl_max_tw_buckets)
325c676270bSArnaldo Carvalho de Melo 		tw = inet_twsk_alloc(sk, state);
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	if (tw != NULL) {
3288feaf0c0SArnaldo Carvalho de Melo 		struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
329463c84b9SArnaldo Carvalho de Melo 		const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
3308feaf0c0SArnaldo Carvalho de Melo 
33158af19e3SKOVACS Krisztian 		tw->tw_transparent	= inet_sk(sk)->transparent;
3321da177e4SLinus Torvalds 		tw->tw_rcv_wscale	= tp->rx_opt.rcv_wscale;
3338feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_rcv_nxt	= tp->rcv_nxt;
3348feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_snd_nxt	= tp->snd_nxt;
3358feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_rcv_wnd	= tcp_receive_window(tp);
3368feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_ts_recent	= tp->rx_opt.ts_recent;
3378feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp;
3381da177e4SLinus Torvalds 
339dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
3401da177e4SLinus Torvalds 		if (tw->tw_family == PF_INET6) {
3411da177e4SLinus Torvalds 			struct ipv6_pinfo *np = inet6_sk(sk);
3420fa1a53eSArnaldo Carvalho de Melo 			struct inet6_timewait_sock *tw6;
3431da177e4SLinus Torvalds 
3440fa1a53eSArnaldo Carvalho de Melo 			tw->tw_ipv6_offset = inet6_tw_offset(sk->sk_prot);
3450fa1a53eSArnaldo Carvalho de Melo 			tw6 = inet6_twsk((struct sock *)tw);
3464e3fd7a0SAlexey Dobriyan 			tw6->tw_v6_daddr = np->daddr;
3474e3fd7a0SAlexey Dobriyan 			tw6->tw_v6_rcv_saddr = np->rcv_saddr;
348b903d324SEric Dumazet 			tw->tw_tclass = np->tclass;
3498feaf0c0SArnaldo Carvalho de Melo 			tw->tw_ipv6only = np->ipv6only;
350c676270bSArnaldo Carvalho de Melo 		}
3511da177e4SLinus Torvalds #endif
352cfb6eeb4SYOSHIFUJI Hideaki 
353cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
354cfb6eeb4SYOSHIFUJI Hideaki 		/*
355cfb6eeb4SYOSHIFUJI Hideaki 		 * The timewait bucket does not have the key DB from the
356cfb6eeb4SYOSHIFUJI Hideaki 		 * sock structure. We just make a quick copy of the
357cfb6eeb4SYOSHIFUJI Hideaki 		 * md5 key being used (if indeed we are using one)
358cfb6eeb4SYOSHIFUJI Hideaki 		 * so the timewait ack generating code has the key.
359cfb6eeb4SYOSHIFUJI Hideaki 		 */
360cfb6eeb4SYOSHIFUJI Hideaki 		do {
361cfb6eeb4SYOSHIFUJI Hideaki 			struct tcp_md5sig_key *key;
362*a915da9bSEric Dumazet 			tcptw->tw_md5_key = NULL;
363cfb6eeb4SYOSHIFUJI Hideaki 			key = tp->af_specific->md5_lookup(sk, sk);
364cfb6eeb4SYOSHIFUJI Hideaki 			if (key != NULL) {
365*a915da9bSEric Dumazet 				tcptw->tw_md5_key = kmemdup(key, sizeof(*key), GFP_ATOMIC);
366*a915da9bSEric Dumazet 				if (tcptw->tw_md5_key && tcp_alloc_md5sig_pool(sk) == NULL)
367cfb6eeb4SYOSHIFUJI Hideaki 					BUG();
368cfb6eeb4SYOSHIFUJI Hideaki 			}
369cfb6eeb4SYOSHIFUJI Hideaki 		} while (0);
370cfb6eeb4SYOSHIFUJI Hideaki #endif
371cfb6eeb4SYOSHIFUJI Hideaki 
3721da177e4SLinus Torvalds 		/* Linkage updates. */
373e48c414eSArnaldo Carvalho de Melo 		__inet_twsk_hashdance(tw, sk, &tcp_hashinfo);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 		/* Get the TIME_WAIT timeout firing. */
3761da177e4SLinus Torvalds 		if (timeo < rto)
3771da177e4SLinus Torvalds 			timeo = rto;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 		if (recycle_ok) {
3801da177e4SLinus Torvalds 			tw->tw_timeout = rto;
3811da177e4SLinus Torvalds 		} else {
3821da177e4SLinus Torvalds 			tw->tw_timeout = TCP_TIMEWAIT_LEN;
3831da177e4SLinus Torvalds 			if (state == TCP_TIME_WAIT)
3841da177e4SLinus Torvalds 				timeo = TCP_TIMEWAIT_LEN;
3851da177e4SLinus Torvalds 		}
3861da177e4SLinus Torvalds 
387696ab2d3SArnaldo Carvalho de Melo 		inet_twsk_schedule(tw, &tcp_death_row, timeo,
388696ab2d3SArnaldo Carvalho de Melo 				   TCP_TIMEWAIT_LEN);
3898feaf0c0SArnaldo Carvalho de Melo 		inet_twsk_put(tw);
3901da177e4SLinus Torvalds 	} else {
3911da177e4SLinus Torvalds 		/* Sorry, if we're out of memory, just CLOSE this
3921da177e4SLinus Torvalds 		 * socket up.  We've got bigger problems than
3931da177e4SLinus Torvalds 		 * non-graceful socket closings.
3941da177e4SLinus Torvalds 		 */
39567631510STom Herbert 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPTIMEWAITOVERFLOW);
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	tcp_update_metrics(sk);
3991da177e4SLinus Torvalds 	tcp_done(sk);
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
402cfb6eeb4SYOSHIFUJI Hideaki void tcp_twsk_destructor(struct sock *sk)
403cfb6eeb4SYOSHIFUJI Hideaki {
404cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
405a928630aSDavid S. Miller 	struct tcp_timewait_sock *twsk = tcp_twsk(sk);
406*a915da9bSEric Dumazet 	if (twsk->tw_md5_key) {
407657e9649SRobert Varga 		tcp_free_md5sig_pool();
408*a915da9bSEric Dumazet 		kfree_rcu(twsk->tw_md5_key, rcu);
409*a915da9bSEric Dumazet 	}
410cfb6eeb4SYOSHIFUJI Hideaki #endif
411cfb6eeb4SYOSHIFUJI Hideaki }
412cfb6eeb4SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(tcp_twsk_destructor);
413cfb6eeb4SYOSHIFUJI Hideaki 
414bdf1ee5dSIlpo Järvinen static inline void TCP_ECN_openreq_child(struct tcp_sock *tp,
415bdf1ee5dSIlpo Järvinen 					 struct request_sock *req)
416bdf1ee5dSIlpo Järvinen {
417bdf1ee5dSIlpo Järvinen 	tp->ecn_flags = inet_rsk(req)->ecn_ok ? TCP_ECN_OK : 0;
418bdf1ee5dSIlpo Järvinen }
419bdf1ee5dSIlpo Järvinen 
4201da177e4SLinus Torvalds /* This is not only more efficient than what we used to do, it eliminates
4211da177e4SLinus Torvalds  * a lot of code duplication between IPv4/IPv6 SYN recv processing. -DaveM
4221da177e4SLinus Torvalds  *
4231da177e4SLinus Torvalds  * Actually, we could lots of memory writes here. tp of listening
4241da177e4SLinus Torvalds  * socket contains all necessary default parameters.
4251da177e4SLinus Torvalds  */
42660236fddSArnaldo Carvalho de Melo struct sock *tcp_create_openreq_child(struct sock *sk, struct request_sock *req, struct sk_buff *skb)
4271da177e4SLinus Torvalds {
428e56c57d0SEric Dumazet 	struct sock *newsk = inet_csk_clone_lock(sk, req, GFP_ATOMIC);
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	if (newsk != NULL) {
4319f1d2604SArnaldo Carvalho de Melo 		const struct inet_request_sock *ireq = inet_rsk(req);
4322e6599cbSArnaldo Carvalho de Melo 		struct tcp_request_sock *treq = tcp_rsk(req);
433a9948a7eSArnaldo Carvalho de Melo 		struct inet_connection_sock *newicsk = inet_csk(newsk);
434435cf559SWilliam Allen Simpson 		struct tcp_sock *newtp = tcp_sk(newsk);
435435cf559SWilliam Allen Simpson 		struct tcp_sock *oldtp = tcp_sk(sk);
436435cf559SWilliam Allen Simpson 		struct tcp_cookie_values *oldcvp = oldtp->cookie_values;
437435cf559SWilliam Allen Simpson 
438435cf559SWilliam Allen Simpson 		/* TCP Cookie Transactions require space for the cookie pair,
439435cf559SWilliam Allen Simpson 		 * as it differs for each connection.  There is no need to
440435cf559SWilliam Allen Simpson 		 * copy any s_data_payload stored at the original socket.
441435cf559SWilliam Allen Simpson 		 * Failure will prevent resuming the connection.
442435cf559SWilliam Allen Simpson 		 *
443435cf559SWilliam Allen Simpson 		 * Presumed copied, in order of appearance:
444435cf559SWilliam Allen Simpson 		 *	cookie_in_always, cookie_out_never
445435cf559SWilliam Allen Simpson 		 */
446435cf559SWilliam Allen Simpson 		if (oldcvp != NULL) {
447435cf559SWilliam Allen Simpson 			struct tcp_cookie_values *newcvp =
448435cf559SWilliam Allen Simpson 				kzalloc(sizeof(*newtp->cookie_values),
449435cf559SWilliam Allen Simpson 					GFP_ATOMIC);
450435cf559SWilliam Allen Simpson 
451435cf559SWilliam Allen Simpson 			if (newcvp != NULL) {
452435cf559SWilliam Allen Simpson 				kref_init(&newcvp->kref);
453435cf559SWilliam Allen Simpson 				newcvp->cookie_desired =
454435cf559SWilliam Allen Simpson 						oldcvp->cookie_desired;
455435cf559SWilliam Allen Simpson 				newtp->cookie_values = newcvp;
456435cf559SWilliam Allen Simpson 			} else {
457435cf559SWilliam Allen Simpson 				/* Not Yet Implemented */
458435cf559SWilliam Allen Simpson 				newtp->cookie_values = NULL;
459435cf559SWilliam Allen Simpson 			}
460435cf559SWilliam Allen Simpson 		}
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 		/* Now setup tcp_sock */
4631da177e4SLinus Torvalds 		newtp->pred_flags = 0;
464435cf559SWilliam Allen Simpson 
465435cf559SWilliam Allen Simpson 		newtp->rcv_wup = newtp->copied_seq =
466435cf559SWilliam Allen Simpson 		newtp->rcv_nxt = treq->rcv_isn + 1;
467435cf559SWilliam Allen Simpson 
468435cf559SWilliam Allen Simpson 		newtp->snd_sml = newtp->snd_una =
469435cf559SWilliam Allen Simpson 		newtp->snd_nxt = newtp->snd_up =
470435cf559SWilliam Allen Simpson 			treq->snt_isn + 1 + tcp_s_data_size(oldtp);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 		tcp_prequeue_init(newtp);
4731da177e4SLinus Torvalds 
474ee7537b6SHantzis Fotis 		tcp_init_wl(newtp, treq->rcv_isn);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 		newtp->srtt = 0;
4771da177e4SLinus Torvalds 		newtp->mdev = TCP_TIMEOUT_INIT;
478463c84b9SArnaldo Carvalho de Melo 		newicsk->icsk_rto = TCP_TIMEOUT_INIT;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 		newtp->packets_out = 0;
4811da177e4SLinus Torvalds 		newtp->retrans_out = 0;
4821da177e4SLinus Torvalds 		newtp->sacked_out = 0;
4831da177e4SLinus Torvalds 		newtp->fackets_out = 0;
4840b6a05c1SIlpo Järvinen 		newtp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 		/* So many TCP implementations out there (incorrectly) count the
4871da177e4SLinus Torvalds 		 * initial SYN frame in their delayed-ACK and congestion control
4881da177e4SLinus Torvalds 		 * algorithms that we must have the following bandaid to talk
4891da177e4SLinus Torvalds 		 * efficiently to them.  -DaveM
4901da177e4SLinus Torvalds 		 */
4919ad7c049SJerry Chu 		newtp->snd_cwnd = TCP_INIT_CWND;
4921da177e4SLinus Torvalds 		newtp->snd_cwnd_cnt = 0;
4939772efb9SStephen Hemminger 		newtp->bytes_acked = 0;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 		newtp->frto_counter = 0;
4961da177e4SLinus Torvalds 		newtp->frto_highmark = 0;
4971da177e4SLinus Torvalds 
498d8a6e65fSEric Dumazet 		if (newicsk->icsk_ca_ops != &tcp_init_congestion_ops &&
499d8a6e65fSEric Dumazet 		    !try_module_get(newicsk->icsk_ca_ops->owner))
5007957aed7SStephen Hemminger 			newicsk->icsk_ca_ops = &tcp_init_congestion_ops;
501317a76f9SStephen Hemminger 
5026687e988SArnaldo Carvalho de Melo 		tcp_set_ca_state(newsk, TCP_CA_Open);
5031da177e4SLinus Torvalds 		tcp_init_xmit_timers(newsk);
5041da177e4SLinus Torvalds 		skb_queue_head_init(&newtp->out_of_order_queue);
505435cf559SWilliam Allen Simpson 		newtp->write_seq = newtp->pushed_seq =
506435cf559SWilliam Allen Simpson 			treq->snt_isn + 1 + tcp_s_data_size(oldtp);
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 		newtp->rx_opt.saw_tstamp = 0;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 		newtp->rx_opt.dsack = 0;
5111da177e4SLinus Torvalds 		newtp->rx_opt.num_sacks = 0;
512cabeccbdSIlpo Järvinen 
5131da177e4SLinus Torvalds 		newtp->urg_data = 0;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 		if (sock_flag(newsk, SOCK_KEEPOPEN))
516463c84b9SArnaldo Carvalho de Melo 			inet_csk_reset_keepalive_timer(newsk,
5171da177e4SLinus Torvalds 						       keepalive_time_when(newtp));
5181da177e4SLinus Torvalds 
5192e6599cbSArnaldo Carvalho de Melo 		newtp->rx_opt.tstamp_ok = ireq->tstamp_ok;
5202e6599cbSArnaldo Carvalho de Melo 		if ((newtp->rx_opt.sack_ok = ireq->sack_ok) != 0) {
5211da177e4SLinus Torvalds 			if (sysctl_tcp_fack)
522e60402d0SIlpo Järvinen 				tcp_enable_fack(newtp);
5231da177e4SLinus Torvalds 		}
5241da177e4SLinus Torvalds 		newtp->window_clamp = req->window_clamp;
5251da177e4SLinus Torvalds 		newtp->rcv_ssthresh = req->rcv_wnd;
5261da177e4SLinus Torvalds 		newtp->rcv_wnd = req->rcv_wnd;
5272e6599cbSArnaldo Carvalho de Melo 		newtp->rx_opt.wscale_ok = ireq->wscale_ok;
5281da177e4SLinus Torvalds 		if (newtp->rx_opt.wscale_ok) {
5292e6599cbSArnaldo Carvalho de Melo 			newtp->rx_opt.snd_wscale = ireq->snd_wscale;
5302e6599cbSArnaldo Carvalho de Melo 			newtp->rx_opt.rcv_wscale = ireq->rcv_wscale;
5311da177e4SLinus Torvalds 		} else {
5321da177e4SLinus Torvalds 			newtp->rx_opt.snd_wscale = newtp->rx_opt.rcv_wscale = 0;
5331da177e4SLinus Torvalds 			newtp->window_clamp = min(newtp->window_clamp, 65535U);
5341da177e4SLinus Torvalds 		}
535aa8223c7SArnaldo Carvalho de Melo 		newtp->snd_wnd = (ntohs(tcp_hdr(skb)->window) <<
536aa8223c7SArnaldo Carvalho de Melo 				  newtp->rx_opt.snd_wscale);
5371da177e4SLinus Torvalds 		newtp->max_window = newtp->snd_wnd;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 		if (newtp->rx_opt.tstamp_ok) {
5401da177e4SLinus Torvalds 			newtp->rx_opt.ts_recent = req->ts_recent;
5419d729f72SJames Morris 			newtp->rx_opt.ts_recent_stamp = get_seconds();
5421da177e4SLinus Torvalds 			newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
5431da177e4SLinus Torvalds 		} else {
5441da177e4SLinus Torvalds 			newtp->rx_opt.ts_recent_stamp = 0;
5451da177e4SLinus Torvalds 			newtp->tcp_header_len = sizeof(struct tcphdr);
5461da177e4SLinus Torvalds 		}
547cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
548cfb6eeb4SYOSHIFUJI Hideaki 		newtp->md5sig_info = NULL;	/*XXX*/
549cfb6eeb4SYOSHIFUJI Hideaki 		if (newtp->af_specific->md5_lookup(sk, newsk))
550cfb6eeb4SYOSHIFUJI Hideaki 			newtp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED;
551cfb6eeb4SYOSHIFUJI Hideaki #endif
552bee7ca9eSWilliam Allen Simpson 		if (skb->len >= TCP_MSS_DEFAULT + newtp->tcp_header_len)
553463c84b9SArnaldo Carvalho de Melo 			newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len;
5541da177e4SLinus Torvalds 		newtp->rx_opt.mss_clamp = req->mss;
5551da177e4SLinus Torvalds 		TCP_ECN_openreq_child(newtp, req);
5561da177e4SLinus Torvalds 
55763231bddSPavel Emelyanov 		TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_PASSIVEOPENS);
5581da177e4SLinus Torvalds 	}
5591da177e4SLinus Torvalds 	return newsk;
5601da177e4SLinus Torvalds }
5614bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_create_openreq_child);
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds /*
5641da177e4SLinus Torvalds  *	Process an incoming packet for SYN_RECV sockets represented
56560236fddSArnaldo Carvalho de Melo  *	as a request_sock.
5661da177e4SLinus Torvalds  */
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
56960236fddSArnaldo Carvalho de Melo 			   struct request_sock *req,
57060236fddSArnaldo Carvalho de Melo 			   struct request_sock **prev)
5711da177e4SLinus Torvalds {
5724957faadSWilliam Allen Simpson 	struct tcp_options_received tmp_opt;
573cf533ea5SEric Dumazet 	const u8 *hash_location;
5744957faadSWilliam Allen Simpson 	struct sock *child;
575aa8223c7SArnaldo Carvalho de Melo 	const struct tcphdr *th = tcp_hdr(skb);
576714e85beSAl Viro 	__be32 flg = tcp_flag_word(th) & (TCP_FLAG_RST|TCP_FLAG_SYN|TCP_FLAG_ACK);
5771da177e4SLinus Torvalds 	int paws_reject = 0;
5781da177e4SLinus Torvalds 
579bb5b7c11SDavid S. Miller 	tmp_opt.saw_tstamp = 0;
580bb5b7c11SDavid S. Miller 	if (th->doff > (sizeof(struct tcphdr)>>2)) {
581bb5b7c11SDavid S. Miller 		tcp_parse_options(skb, &tmp_opt, &hash_location, 0);
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 		if (tmp_opt.saw_tstamp) {
5841da177e4SLinus Torvalds 			tmp_opt.ts_recent = req->ts_recent;
5851da177e4SLinus Torvalds 			/* We do not store true stamp, but it is not required,
5861da177e4SLinus Torvalds 			 * it can be estimated (approximately)
5871da177e4SLinus Torvalds 			 * from another data.
5881da177e4SLinus Torvalds 			 */
5899d729f72SJames Morris 			tmp_opt.ts_recent_stamp = get_seconds() - ((TCP_TIMEOUT_INIT/HZ)<<req->retrans);
590c887e6d2SIlpo Järvinen 			paws_reject = tcp_paws_reject(&tmp_opt, th->rst);
5911da177e4SLinus Torvalds 		}
5921da177e4SLinus Torvalds 	}
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	/* Check for pure retransmitted SYN. */
5952e6599cbSArnaldo Carvalho de Melo 	if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn &&
5961da177e4SLinus Torvalds 	    flg == TCP_FLAG_SYN &&
5971da177e4SLinus Torvalds 	    !paws_reject) {
5981da177e4SLinus Torvalds 		/*
5991da177e4SLinus Torvalds 		 * RFC793 draws (Incorrectly! It was fixed in RFC1122)
6001da177e4SLinus Torvalds 		 * this case on figure 6 and figure 8, but formal
6011da177e4SLinus Torvalds 		 * protocol description says NOTHING.
6021da177e4SLinus Torvalds 		 * To be more exact, it says that we should send ACK,
6031da177e4SLinus Torvalds 		 * because this segment (at least, if it has no data)
6041da177e4SLinus Torvalds 		 * is out of window.
6051da177e4SLinus Torvalds 		 *
6061da177e4SLinus Torvalds 		 *  CONCLUSION: RFC793 (even with RFC1122) DOES NOT
6071da177e4SLinus Torvalds 		 *  describe SYN-RECV state. All the description
6081da177e4SLinus Torvalds 		 *  is wrong, we cannot believe to it and should
6091da177e4SLinus Torvalds 		 *  rely only on common sense and implementation
6101da177e4SLinus Torvalds 		 *  experience.
6111da177e4SLinus Torvalds 		 *
6121da177e4SLinus Torvalds 		 * Enforce "SYN-ACK" according to figure 8, figure 6
6131da177e4SLinus Torvalds 		 * of RFC793, fixed by RFC1122.
6141da177e4SLinus Torvalds 		 */
615e6b4d113SWilliam Allen Simpson 		req->rsk_ops->rtx_syn_ack(sk, req, NULL);
6161da177e4SLinus Torvalds 		return NULL;
6171da177e4SLinus Torvalds 	}
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	/* Further reproduces section "SEGMENT ARRIVES"
6201da177e4SLinus Torvalds 	   for state SYN-RECEIVED of RFC793.
6211da177e4SLinus Torvalds 	   It is broken, however, it does not work only
6221da177e4SLinus Torvalds 	   when SYNs are crossed.
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	   You would think that SYN crossing is impossible here, since
6251da177e4SLinus Torvalds 	   we should have a SYN_SENT socket (from connect()) on our end,
6261da177e4SLinus Torvalds 	   but this is not true if the crossed SYNs were sent to both
6271da177e4SLinus Torvalds 	   ends by a malicious third party.  We must defend against this,
6281da177e4SLinus Torvalds 	   and to do that we first verify the ACK (as per RFC793, page
6291da177e4SLinus Torvalds 	   36) and reset if it is invalid.  Is this a true full defense?
6301da177e4SLinus Torvalds 	   To convince ourselves, let us consider a way in which the ACK
6311da177e4SLinus Torvalds 	   test can still pass in this 'malicious crossed SYNs' case.
6321da177e4SLinus Torvalds 	   Malicious sender sends identical SYNs (and thus identical sequence
6331da177e4SLinus Torvalds 	   numbers) to both A and B:
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 		A: gets SYN, seq=7
6361da177e4SLinus Torvalds 		B: gets SYN, seq=7
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	   By our good fortune, both A and B select the same initial
6391da177e4SLinus Torvalds 	   send sequence number of seven :-)
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 		A: sends SYN|ACK, seq=7, ack_seq=8
6421da177e4SLinus Torvalds 		B: sends SYN|ACK, seq=7, ack_seq=8
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	   So we are now A eating this SYN|ACK, ACK test passes.  So
6451da177e4SLinus Torvalds 	   does sequence test, SYN is truncated, and thus we consider
6461da177e4SLinus Torvalds 	   it a bare ACK.
6471da177e4SLinus Torvalds 
648ec0a1966SDavid S. Miller 	   If icsk->icsk_accept_queue.rskq_defer_accept, we silently drop this
649ec0a1966SDavid S. Miller 	   bare ACK.  Otherwise, we create an established connection.  Both
650ec0a1966SDavid S. Miller 	   ends (listening sockets) accept the new incoming connection and try
651ec0a1966SDavid S. Miller 	   to talk to each other. 8-)
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	   Note: This case is both harmless, and rare.  Possibility is about the
6541da177e4SLinus Torvalds 	   same as us discovering intelligent life on another plant tomorrow.
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	   But generally, we should (RFC lies!) to accept ACK
6571da177e4SLinus Torvalds 	   from SYNACK both here and in tcp_rcv_state_process().
6581da177e4SLinus Torvalds 	   tcp_rcv_state_process() does not, hence, we do not too.
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	   Note that the case is absolutely generic:
6611da177e4SLinus Torvalds 	   we cannot optimize anything here without
6621da177e4SLinus Torvalds 	   violating protocol. All the checks must be made
6631da177e4SLinus Torvalds 	   before attempt to create socket.
6641da177e4SLinus Torvalds 	 */
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	/* RFC793 page 36: "If the connection is in any non-synchronized state ...
6671da177e4SLinus Torvalds 	 *                  and the incoming segment acknowledges something not yet
668caa20d9aSStephen Hemminger 	 *                  sent (the segment carries an unacceptable ACK) ...
6691da177e4SLinus Torvalds 	 *                  a reset is sent."
6701da177e4SLinus Torvalds 	 *
6711da177e4SLinus Torvalds 	 * Invalid ACK: reset will be sent by listening socket
6721da177e4SLinus Torvalds 	 */
6731da177e4SLinus Torvalds 	if ((flg & TCP_FLAG_ACK) &&
674435cf559SWilliam Allen Simpson 	    (TCP_SKB_CB(skb)->ack_seq !=
675435cf559SWilliam Allen Simpson 	     tcp_rsk(req)->snt_isn + 1 + tcp_s_data_size(tcp_sk(sk))))
6761da177e4SLinus Torvalds 		return sk;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	/* Also, it would be not so bad idea to check rcv_tsecr, which
6791da177e4SLinus Torvalds 	 * is essentially ACK extension and too early or too late values
6801da177e4SLinus Torvalds 	 * should cause reset in unsynchronized states.
6811da177e4SLinus Torvalds 	 */
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	/* RFC793: "first check sequence number". */
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	if (paws_reject || !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
6862e6599cbSArnaldo Carvalho de Melo 					  tcp_rsk(req)->rcv_isn + 1, tcp_rsk(req)->rcv_isn + 1 + req->rcv_wnd)) {
6871da177e4SLinus Torvalds 		/* Out of window: send ACK and drop. */
6881da177e4SLinus Torvalds 		if (!(flg & TCP_FLAG_RST))
6896edafaafSGui Jianfeng 			req->rsk_ops->send_ack(sk, skb, req);
6901da177e4SLinus Torvalds 		if (paws_reject)
691de0744afSPavel Emelyanov 			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);
6921da177e4SLinus Torvalds 		return NULL;
6931da177e4SLinus Torvalds 	}
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	/* In sequence, PAWS is OK. */
6961da177e4SLinus Torvalds 
6972e6599cbSArnaldo Carvalho de Melo 	if (tmp_opt.saw_tstamp && !after(TCP_SKB_CB(skb)->seq, tcp_rsk(req)->rcv_isn + 1))
6981da177e4SLinus Torvalds 		req->ts_recent = tmp_opt.rcv_tsval;
6991da177e4SLinus Torvalds 
7002e6599cbSArnaldo Carvalho de Melo 	if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn) {
7011da177e4SLinus Torvalds 		/* Truncate SYN, it is out of window starting
7022e6599cbSArnaldo Carvalho de Melo 		   at tcp_rsk(req)->rcv_isn + 1. */
7031da177e4SLinus Torvalds 		flg &= ~TCP_FLAG_SYN;
7041da177e4SLinus Torvalds 	}
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	/* RFC793: "second check the RST bit" and
7071da177e4SLinus Torvalds 	 *	   "fourth, check the SYN bit"
7081da177e4SLinus Torvalds 	 */
7093687b1dcSWei Yongjun 	if (flg & (TCP_FLAG_RST|TCP_FLAG_SYN)) {
71063231bddSPavel Emelyanov 		TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_ATTEMPTFAILS);
7111da177e4SLinus Torvalds 		goto embryonic_reset;
7123687b1dcSWei Yongjun 	}
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	/* ACK sequence verified above, just make sure ACK is
7151da177e4SLinus Torvalds 	 * set.  If ACK not set, just silently drop the packet.
7161da177e4SLinus Torvalds 	 */
7171da177e4SLinus Torvalds 	if (!(flg & TCP_FLAG_ACK))
7181da177e4SLinus Torvalds 		return NULL;
7191da177e4SLinus Torvalds 
720d1b99ba4SJulian Anastasov 	/* While TCP_DEFER_ACCEPT is active, drop bare ACK. */
721d1b99ba4SJulian Anastasov 	if (req->retrans < inet_csk(sk)->icsk_accept_queue.rskq_defer_accept &&
722ec0a1966SDavid S. Miller 	    TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) {
723ec0a1966SDavid S. Miller 		inet_rsk(req)->acked = 1;
724907cdda5SEric Dumazet 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDEFERACCEPTDROP);
725ec0a1966SDavid S. Miller 		return NULL;
726ec0a1966SDavid S. Miller 	}
7279ad7c049SJerry Chu 	if (tmp_opt.saw_tstamp && tmp_opt.rcv_tsecr)
7289ad7c049SJerry Chu 		tcp_rsk(req)->snt_synack = tmp_opt.rcv_tsecr;
7299ad7c049SJerry Chu 	else if (req->retrans) /* don't take RTT sample if retrans && ~TS */
7309ad7c049SJerry Chu 		tcp_rsk(req)->snt_synack = 0;
731ec0a1966SDavid S. Miller 
7321da177e4SLinus Torvalds 	/* OK, ACK is valid, create big socket and
7331da177e4SLinus Torvalds 	 * feed this segment to it. It will repeat all
7341da177e4SLinus Torvalds 	 * the tests. THIS SEGMENT MUST MOVE SOCKET TO
7351da177e4SLinus Torvalds 	 * ESTABLISHED STATE. If it will be dropped after
7361da177e4SLinus Torvalds 	 * socket is created, wait for troubles.
7371da177e4SLinus Torvalds 	 */
7382aaab9a0SAdam Langley 	child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL);
7391da177e4SLinus Torvalds 	if (child == NULL)
7401da177e4SLinus Torvalds 		goto listen_overflow;
7411da177e4SLinus Torvalds 
742463c84b9SArnaldo Carvalho de Melo 	inet_csk_reqsk_queue_unlink(sk, req, prev);
743463c84b9SArnaldo Carvalho de Melo 	inet_csk_reqsk_queue_removed(sk, req);
7441da177e4SLinus Torvalds 
745463c84b9SArnaldo Carvalho de Melo 	inet_csk_reqsk_queue_add(sk, req, child);
7461da177e4SLinus Torvalds 	return child;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds listen_overflow:
7491da177e4SLinus Torvalds 	if (!sysctl_tcp_abort_on_overflow) {
7502e6599cbSArnaldo Carvalho de Melo 		inet_rsk(req)->acked = 1;
7511da177e4SLinus Torvalds 		return NULL;
7521da177e4SLinus Torvalds 	}
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds embryonic_reset:
755de0744afSPavel Emelyanov 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_EMBRYONICRSTS);
7561da177e4SLinus Torvalds 	if (!(flg & TCP_FLAG_RST))
757cfb6eeb4SYOSHIFUJI Hideaki 		req->rsk_ops->send_reset(sk, skb);
7581da177e4SLinus Torvalds 
759463c84b9SArnaldo Carvalho de Melo 	inet_csk_reqsk_queue_drop(sk, req, prev);
7601da177e4SLinus Torvalds 	return NULL;
7611da177e4SLinus Torvalds }
7624bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_check_req);
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds /*
7651da177e4SLinus Torvalds  * Queue segment on the new socket if the new socket is active,
7661da177e4SLinus Torvalds  * otherwise we just shortcircuit this and continue with
7671da177e4SLinus Torvalds  * the new socket.
7681da177e4SLinus Torvalds  */
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds int tcp_child_process(struct sock *parent, struct sock *child,
7711da177e4SLinus Torvalds 		      struct sk_buff *skb)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds 	int ret = 0;
7741da177e4SLinus Torvalds 	int state = child->sk_state;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	if (!sock_owned_by_user(child)) {
777aa8223c7SArnaldo Carvalho de Melo 		ret = tcp_rcv_state_process(child, skb, tcp_hdr(skb),
778aa8223c7SArnaldo Carvalho de Melo 					    skb->len);
7791da177e4SLinus Torvalds 		/* Wakeup parent, send SIGIO */
7801da177e4SLinus Torvalds 		if (state == TCP_SYN_RECV && child->sk_state != state)
7811da177e4SLinus Torvalds 			parent->sk_data_ready(parent, 0);
7821da177e4SLinus Torvalds 	} else {
7831da177e4SLinus Torvalds 		/* Alas, it is possible again, because we do lookup
7841da177e4SLinus Torvalds 		 * in main socket hash table and lock on listening
7851da177e4SLinus Torvalds 		 * socket does not protect us more.
7861da177e4SLinus Torvalds 		 */
787a3a858ffSZhu Yi 		__sk_add_backlog(child, skb);
7881da177e4SLinus Torvalds 	}
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	bh_unlock_sock(child);
7911da177e4SLinus Torvalds 	sock_put(child);
7921da177e4SLinus Torvalds 	return ret;
7931da177e4SLinus Torvalds }
7941da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_child_process);
795