xref: /openbmc/linux/net/ipv4/tcp_minisocks.c (revision 714e85be3557222bc25f69c252326207c900a7db)
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_minisocks.c,v 1.15 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 #include <linux/mm.h>
241da177e4SLinus Torvalds #include <linux/module.h>
251da177e4SLinus Torvalds #include <linux/sysctl.h>
261da177e4SLinus Torvalds #include <linux/workqueue.h>
271da177e4SLinus Torvalds #include <net/tcp.h>
281da177e4SLinus Torvalds #include <net/inet_common.h>
291da177e4SLinus Torvalds #include <net/xfrm.h>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
321da177e4SLinus Torvalds #define SYNC_INIT 0 /* let the user enable it */
331da177e4SLinus Torvalds #else
341da177e4SLinus Torvalds #define SYNC_INIT 1
351da177e4SLinus Torvalds #endif
361da177e4SLinus Torvalds 
37ab32ea5dSBrian Haley int sysctl_tcp_syncookies __read_mostly = SYNC_INIT;
38ab32ea5dSBrian Haley int sysctl_tcp_abort_on_overflow __read_mostly;
391da177e4SLinus Torvalds 
40295ff7edSArnaldo Carvalho de Melo struct inet_timewait_death_row tcp_death_row = {
41295ff7edSArnaldo Carvalho de Melo 	.sysctl_max_tw_buckets = NR_FILE * 2,
42295ff7edSArnaldo Carvalho de Melo 	.period		= TCP_TIMEWAIT_LEN / INET_TWDR_TWKILL_SLOTS,
43e4d91918SIngo Molnar 	.death_lock	= __SPIN_LOCK_UNLOCKED(tcp_death_row.death_lock),
44295ff7edSArnaldo Carvalho de Melo 	.hashinfo	= &tcp_hashinfo,
45295ff7edSArnaldo Carvalho de Melo 	.tw_timer	= TIMER_INITIALIZER(inet_twdr_hangman, 0,
46295ff7edSArnaldo Carvalho de Melo 					    (unsigned long)&tcp_death_row),
47295ff7edSArnaldo Carvalho de Melo 	.twkill_work	= __WORK_INITIALIZER(tcp_death_row.twkill_work,
48295ff7edSArnaldo Carvalho de Melo 					     inet_twdr_twkill_work,
49295ff7edSArnaldo Carvalho de Melo 					     &tcp_death_row),
50295ff7edSArnaldo Carvalho de Melo /* Short-time timewait calendar */
51295ff7edSArnaldo Carvalho de Melo 
52295ff7edSArnaldo Carvalho de Melo 	.twcal_hand	= -1,
53295ff7edSArnaldo Carvalho de Melo 	.twcal_timer	= TIMER_INITIALIZER(inet_twdr_twcal_tick, 0,
54295ff7edSArnaldo Carvalho de Melo 					    (unsigned long)&tcp_death_row),
55295ff7edSArnaldo Carvalho de Melo };
56295ff7edSArnaldo Carvalho de Melo 
57295ff7edSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(tcp_death_row);
58295ff7edSArnaldo Carvalho de Melo 
591da177e4SLinus Torvalds static __inline__ int tcp_in_window(u32 seq, u32 end_seq, u32 s_win, u32 e_win)
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds 	if (seq == s_win)
621da177e4SLinus Torvalds 		return 1;
631da177e4SLinus Torvalds 	if (after(end_seq, s_win) && before(seq, e_win))
641da177e4SLinus Torvalds 		return 1;
651da177e4SLinus Torvalds 	return (seq == e_win && seq == end_seq);
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /*
691da177e4SLinus Torvalds  * * Main purpose of TIME-WAIT state is to close connection gracefully,
701da177e4SLinus Torvalds  *   when one of ends sits in LAST-ACK or CLOSING retransmitting FIN
711da177e4SLinus Torvalds  *   (and, probably, tail of data) and one or more our ACKs are lost.
721da177e4SLinus Torvalds  * * What is TIME-WAIT timeout? It is associated with maximal packet
731da177e4SLinus Torvalds  *   lifetime in the internet, which results in wrong conclusion, that
741da177e4SLinus Torvalds  *   it is set to catch "old duplicate segments" wandering out of their path.
751da177e4SLinus Torvalds  *   It is not quite correct. This timeout is calculated so that it exceeds
761da177e4SLinus Torvalds  *   maximal retransmission timeout enough to allow to lose one (or more)
771da177e4SLinus Torvalds  *   segments sent by peer and our ACKs. This time may be calculated from RTO.
781da177e4SLinus Torvalds  * * When TIME-WAIT socket receives RST, it means that another end
791da177e4SLinus Torvalds  *   finally closed and we are allowed to kill TIME-WAIT too.
801da177e4SLinus Torvalds  * * Second purpose of TIME-WAIT is catching old duplicate segments.
811da177e4SLinus Torvalds  *   Well, certainly it is pure paranoia, but if we load TIME-WAIT
821da177e4SLinus Torvalds  *   with this semantics, we MUST NOT kill TIME-WAIT state with RSTs.
831da177e4SLinus Torvalds  * * If we invented some more clever way to catch duplicates
841da177e4SLinus Torvalds  *   (f.e. based on PAWS), we could truncate TIME-WAIT to several RTOs.
851da177e4SLinus Torvalds  *
861da177e4SLinus Torvalds  * The algorithm below is based on FORMAL INTERPRETATION of RFCs.
871da177e4SLinus Torvalds  * When you compare it to RFCs, please, read section SEGMENT ARRIVES
881da177e4SLinus Torvalds  * from the very beginning.
891da177e4SLinus Torvalds  *
901da177e4SLinus Torvalds  * NOTE. With recycling (and later with fin-wait-2) TW bucket
911da177e4SLinus Torvalds  * is _not_ stateless. It means, that strictly speaking we must
921da177e4SLinus Torvalds  * spinlock it. I do not want! Well, probability of misbehaviour
931da177e4SLinus Torvalds  * is ridiculously low and, seems, we could use some mb() tricks
941da177e4SLinus Torvalds  * to avoid misread sequence numbers, states etc.  --ANK
951da177e4SLinus Torvalds  */
961da177e4SLinus Torvalds enum tcp_tw_status
978feaf0c0SArnaldo Carvalho de Melo tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
988feaf0c0SArnaldo Carvalho de Melo 			   const struct tcphdr *th)
991da177e4SLinus Torvalds {
1008feaf0c0SArnaldo Carvalho de Melo 	struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
1011da177e4SLinus Torvalds 	struct tcp_options_received tmp_opt;
1021da177e4SLinus Torvalds 	int paws_reject = 0;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	tmp_opt.saw_tstamp = 0;
1058feaf0c0SArnaldo Carvalho de Melo 	if (th->doff > (sizeof(*th) >> 2) && tcptw->tw_ts_recent_stamp) {
1061da177e4SLinus Torvalds 		tcp_parse_options(skb, &tmp_opt, 0);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 		if (tmp_opt.saw_tstamp) {
1098feaf0c0SArnaldo Carvalho de Melo 			tmp_opt.ts_recent	= tcptw->tw_ts_recent;
1108feaf0c0SArnaldo Carvalho de Melo 			tmp_opt.ts_recent_stamp	= tcptw->tw_ts_recent_stamp;
1111da177e4SLinus Torvalds 			paws_reject = tcp_paws_check(&tmp_opt, th->rst);
1121da177e4SLinus Torvalds 		}
1131da177e4SLinus Torvalds 	}
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	if (tw->tw_substate == TCP_FIN_WAIT2) {
1161da177e4SLinus Torvalds 		/* Just repeat all the checks of tcp_rcv_state_process() */
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 		/* Out of window, send ACK */
1191da177e4SLinus Torvalds 		if (paws_reject ||
1201da177e4SLinus Torvalds 		    !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
1218feaf0c0SArnaldo Carvalho de Melo 				   tcptw->tw_rcv_nxt,
1228feaf0c0SArnaldo Carvalho de Melo 				   tcptw->tw_rcv_nxt + tcptw->tw_rcv_wnd))
1231da177e4SLinus Torvalds 			return TCP_TW_ACK;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 		if (th->rst)
1261da177e4SLinus Torvalds 			goto kill;
1271da177e4SLinus Torvalds 
1288feaf0c0SArnaldo Carvalho de Melo 		if (th->syn && !before(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt))
1291da177e4SLinus Torvalds 			goto kill_with_rst;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 		/* Dup ACK? */
1328feaf0c0SArnaldo Carvalho de Melo 		if (!after(TCP_SKB_CB(skb)->end_seq, tcptw->tw_rcv_nxt) ||
1331da177e4SLinus Torvalds 		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) {
1348feaf0c0SArnaldo Carvalho de Melo 			inet_twsk_put(tw);
1351da177e4SLinus Torvalds 			return TCP_TW_SUCCESS;
1361da177e4SLinus Torvalds 		}
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 		/* New data or FIN. If new data arrive after half-duplex close,
1391da177e4SLinus Torvalds 		 * reset.
1401da177e4SLinus Torvalds 		 */
1411da177e4SLinus Torvalds 		if (!th->fin ||
1428feaf0c0SArnaldo Carvalho de Melo 		    TCP_SKB_CB(skb)->end_seq != tcptw->tw_rcv_nxt + 1) {
1431da177e4SLinus Torvalds kill_with_rst:
144295ff7edSArnaldo Carvalho de Melo 			inet_twsk_deschedule(tw, &tcp_death_row);
1458feaf0c0SArnaldo Carvalho de Melo 			inet_twsk_put(tw);
1461da177e4SLinus Torvalds 			return TCP_TW_RST;
1471da177e4SLinus Torvalds 		}
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 		/* FIN arrived, enter true time-wait state. */
1501da177e4SLinus Torvalds 		tw->tw_substate	  = TCP_TIME_WAIT;
1518feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_rcv_nxt = TCP_SKB_CB(skb)->end_seq;
1521da177e4SLinus Torvalds 		if (tmp_opt.saw_tstamp) {
1538feaf0c0SArnaldo Carvalho de Melo 			tcptw->tw_ts_recent_stamp = xtime.tv_sec;
1548feaf0c0SArnaldo Carvalho de Melo 			tcptw->tw_ts_recent	  = tmp_opt.rcv_tsval;
1551da177e4SLinus Torvalds 		}
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 		/* I am shamed, but failed to make it more elegant.
1581da177e4SLinus Torvalds 		 * Yes, it is direct reference to IP, which is impossible
1591da177e4SLinus Torvalds 		 * to generalize to IPv6. Taking into account that IPv6
160caa20d9aSStephen Hemminger 		 * do not understand recycling in any case, it not
1611da177e4SLinus Torvalds 		 * a big problem in practice. --ANK */
1621da177e4SLinus Torvalds 		if (tw->tw_family == AF_INET &&
163295ff7edSArnaldo Carvalho de Melo 		    tcp_death_row.sysctl_tw_recycle && tcptw->tw_ts_recent_stamp &&
1641da177e4SLinus Torvalds 		    tcp_v4_tw_remember_stamp(tw))
165696ab2d3SArnaldo Carvalho de Melo 			inet_twsk_schedule(tw, &tcp_death_row, tw->tw_timeout,
166696ab2d3SArnaldo Carvalho de Melo 					   TCP_TIMEWAIT_LEN);
1671da177e4SLinus Torvalds 		else
168696ab2d3SArnaldo Carvalho de Melo 			inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
169696ab2d3SArnaldo Carvalho de Melo 					   TCP_TIMEWAIT_LEN);
1701da177e4SLinus Torvalds 		return TCP_TW_ACK;
1711da177e4SLinus Torvalds 	}
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	/*
1741da177e4SLinus Torvalds 	 *	Now real TIME-WAIT state.
1751da177e4SLinus Torvalds 	 *
1761da177e4SLinus Torvalds 	 *	RFC 1122:
1771da177e4SLinus Torvalds 	 *	"When a connection is [...] on TIME-WAIT state [...]
1781da177e4SLinus Torvalds 	 *	[a TCP] MAY accept a new SYN from the remote TCP to
1791da177e4SLinus Torvalds 	 *	reopen the connection directly, if it:
1801da177e4SLinus Torvalds 	 *
1811da177e4SLinus Torvalds 	 *	(1)  assigns its initial sequence number for the new
1821da177e4SLinus Torvalds 	 *	connection to be larger than the largest sequence
1831da177e4SLinus Torvalds 	 *	number it used on the previous connection incarnation,
1841da177e4SLinus Torvalds 	 *	and
1851da177e4SLinus Torvalds 	 *
1861da177e4SLinus Torvalds 	 *	(2)  returns to TIME-WAIT state if the SYN turns out
1871da177e4SLinus Torvalds 	 *	to be an old duplicate".
1881da177e4SLinus Torvalds 	 */
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	if (!paws_reject &&
1918feaf0c0SArnaldo Carvalho de Melo 	    (TCP_SKB_CB(skb)->seq == tcptw->tw_rcv_nxt &&
1921da177e4SLinus Torvalds 	     (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq || th->rst))) {
1931da177e4SLinus Torvalds 		/* In window segment, it may be only reset or bare ack. */
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 		if (th->rst) {
196caa20d9aSStephen Hemminger 			/* This is TIME_WAIT assassination, in two flavors.
1971da177e4SLinus Torvalds 			 * Oh well... nobody has a sufficient solution to this
1981da177e4SLinus Torvalds 			 * protocol bug yet.
1991da177e4SLinus Torvalds 			 */
2001da177e4SLinus Torvalds 			if (sysctl_tcp_rfc1337 == 0) {
2011da177e4SLinus Torvalds kill:
202295ff7edSArnaldo Carvalho de Melo 				inet_twsk_deschedule(tw, &tcp_death_row);
2038feaf0c0SArnaldo Carvalho de Melo 				inet_twsk_put(tw);
2041da177e4SLinus Torvalds 				return TCP_TW_SUCCESS;
2051da177e4SLinus Torvalds 			}
2061da177e4SLinus Torvalds 		}
207696ab2d3SArnaldo Carvalho de Melo 		inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
208696ab2d3SArnaldo Carvalho de Melo 				   TCP_TIMEWAIT_LEN);
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 		if (tmp_opt.saw_tstamp) {
2118feaf0c0SArnaldo Carvalho de Melo 			tcptw->tw_ts_recent	  = tmp_opt.rcv_tsval;
2128feaf0c0SArnaldo Carvalho de Melo 			tcptw->tw_ts_recent_stamp = xtime.tv_sec;
2131da177e4SLinus Torvalds 		}
2141da177e4SLinus Torvalds 
2158feaf0c0SArnaldo Carvalho de Melo 		inet_twsk_put(tw);
2161da177e4SLinus Torvalds 		return TCP_TW_SUCCESS;
2171da177e4SLinus Torvalds 	}
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	/* Out of window segment.
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	   All the segments are ACKed immediately.
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	   The only exception is new SYN. We accept it, if it is
2241da177e4SLinus Torvalds 	   not old duplicate and we are not in danger to be killed
2251da177e4SLinus Torvalds 	   by delayed old duplicates. RFC check is that it has
2261da177e4SLinus Torvalds 	   newer sequence number works at rates <40Mbit/sec.
2271da177e4SLinus Torvalds 	   However, if paws works, it is reliable AND even more,
2281da177e4SLinus Torvalds 	   we even may relax silly seq space cutoff.
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	   RED-PEN: we violate main RFC requirement, if this SYN will appear
2311da177e4SLinus Torvalds 	   old duplicate (i.e. we receive RST in reply to SYN-ACK),
2321da177e4SLinus Torvalds 	   we must return socket to time-wait state. It is not good,
2331da177e4SLinus Torvalds 	   but not fatal yet.
2341da177e4SLinus Torvalds 	 */
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	if (th->syn && !th->rst && !th->ack && !paws_reject &&
2378feaf0c0SArnaldo Carvalho de Melo 	    (after(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt) ||
2388feaf0c0SArnaldo Carvalho de Melo 	     (tmp_opt.saw_tstamp &&
2398feaf0c0SArnaldo Carvalho de Melo 	      (s32)(tcptw->tw_ts_recent - tmp_opt.rcv_tsval) < 0))) {
2408feaf0c0SArnaldo Carvalho de Melo 		u32 isn = tcptw->tw_snd_nxt + 65535 + 2;
2411da177e4SLinus Torvalds 		if (isn == 0)
2421da177e4SLinus Torvalds 			isn++;
2431da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->when = isn;
2441da177e4SLinus Torvalds 		return TCP_TW_SYN;
2451da177e4SLinus Torvalds 	}
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	if (paws_reject)
2481da177e4SLinus Torvalds 		NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED);
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	if(!th->rst) {
2511da177e4SLinus Torvalds 		/* In this case we must reset the TIMEWAIT timer.
2521da177e4SLinus Torvalds 		 *
2531da177e4SLinus Torvalds 		 * If it is ACKless SYN it may be both old duplicate
2541da177e4SLinus Torvalds 		 * and new good SYN with random sequence number <rcv_nxt.
2551da177e4SLinus Torvalds 		 * Do not reschedule in the last case.
2561da177e4SLinus Torvalds 		 */
2571da177e4SLinus Torvalds 		if (paws_reject || th->ack)
258696ab2d3SArnaldo Carvalho de Melo 			inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
259696ab2d3SArnaldo Carvalho de Melo 					   TCP_TIMEWAIT_LEN);
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 		/* Send ACK. Note, we do not put the bucket,
2621da177e4SLinus Torvalds 		 * it will be released by caller.
2631da177e4SLinus Torvalds 		 */
2641da177e4SLinus Torvalds 		return TCP_TW_ACK;
2651da177e4SLinus Torvalds 	}
2668feaf0c0SArnaldo Carvalho de Melo 	inet_twsk_put(tw);
2671da177e4SLinus Torvalds 	return TCP_TW_SUCCESS;
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds /*
2711da177e4SLinus Torvalds  * Move a socket to time-wait or dead fin-wait-2 state.
2721da177e4SLinus Torvalds  */
2731da177e4SLinus Torvalds void tcp_time_wait(struct sock *sk, int state, int timeo)
2741da177e4SLinus Torvalds {
2758feaf0c0SArnaldo Carvalho de Melo 	struct inet_timewait_sock *tw = NULL;
2768292a17aSArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
2778feaf0c0SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
2781da177e4SLinus Torvalds 	int recycle_ok = 0;
2791da177e4SLinus Torvalds 
280295ff7edSArnaldo Carvalho de Melo 	if (tcp_death_row.sysctl_tw_recycle && tp->rx_opt.ts_recent_stamp)
2818292a17aSArnaldo Carvalho de Melo 		recycle_ok = icsk->icsk_af_ops->remember_stamp(sk);
2821da177e4SLinus Torvalds 
283295ff7edSArnaldo Carvalho de Melo 	if (tcp_death_row.tw_count < tcp_death_row.sysctl_max_tw_buckets)
284c676270bSArnaldo Carvalho de Melo 		tw = inet_twsk_alloc(sk, state);
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	if (tw != NULL) {
2878feaf0c0SArnaldo Carvalho de Melo 		struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
288463c84b9SArnaldo Carvalho de Melo 		const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
2898feaf0c0SArnaldo Carvalho de Melo 
2901da177e4SLinus Torvalds 		tw->tw_rcv_wscale	= tp->rx_opt.rcv_wscale;
2918feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_rcv_nxt	= tp->rcv_nxt;
2928feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_snd_nxt	= tp->snd_nxt;
2938feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_rcv_wnd	= tcp_receive_window(tp);
2948feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_ts_recent	= tp->rx_opt.ts_recent;
2958feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2981da177e4SLinus Torvalds 		if (tw->tw_family == PF_INET6) {
2991da177e4SLinus Torvalds 			struct ipv6_pinfo *np = inet6_sk(sk);
3000fa1a53eSArnaldo Carvalho de Melo 			struct inet6_timewait_sock *tw6;
3011da177e4SLinus Torvalds 
3020fa1a53eSArnaldo Carvalho de Melo 			tw->tw_ipv6_offset = inet6_tw_offset(sk->sk_prot);
3030fa1a53eSArnaldo Carvalho de Melo 			tw6 = inet6_twsk((struct sock *)tw);
3040fa1a53eSArnaldo Carvalho de Melo 			ipv6_addr_copy(&tw6->tw_v6_daddr, &np->daddr);
3050fa1a53eSArnaldo Carvalho de Melo 			ipv6_addr_copy(&tw6->tw_v6_rcv_saddr, &np->rcv_saddr);
3068feaf0c0SArnaldo Carvalho de Melo 			tw->tw_ipv6only = np->ipv6only;
307c676270bSArnaldo Carvalho de Melo 		}
3081da177e4SLinus Torvalds #endif
309cfb6eeb4SYOSHIFUJI Hideaki 
310cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
311cfb6eeb4SYOSHIFUJI Hideaki 		/*
312cfb6eeb4SYOSHIFUJI Hideaki 		 * The timewait bucket does not have the key DB from the
313cfb6eeb4SYOSHIFUJI Hideaki 		 * sock structure. We just make a quick copy of the
314cfb6eeb4SYOSHIFUJI Hideaki 		 * md5 key being used (if indeed we are using one)
315cfb6eeb4SYOSHIFUJI Hideaki 		 * so the timewait ack generating code has the key.
316cfb6eeb4SYOSHIFUJI Hideaki 		 */
317cfb6eeb4SYOSHIFUJI Hideaki 		do {
318cfb6eeb4SYOSHIFUJI Hideaki 			struct tcp_md5sig_key *key;
319cfb6eeb4SYOSHIFUJI Hideaki 			memset(tcptw->tw_md5_key, 0, sizeof(tcptw->tw_md5_key));
320cfb6eeb4SYOSHIFUJI Hideaki 			tcptw->tw_md5_keylen = 0;
321cfb6eeb4SYOSHIFUJI Hideaki 			key = tp->af_specific->md5_lookup(sk, sk);
322cfb6eeb4SYOSHIFUJI Hideaki 			if (key != NULL) {
323cfb6eeb4SYOSHIFUJI Hideaki 				memcpy(&tcptw->tw_md5_key, key->key, key->keylen);
324cfb6eeb4SYOSHIFUJI Hideaki 				tcptw->tw_md5_keylen = key->keylen;
325cfb6eeb4SYOSHIFUJI Hideaki 				if (tcp_alloc_md5sig_pool() == NULL)
326cfb6eeb4SYOSHIFUJI Hideaki 					BUG();
327cfb6eeb4SYOSHIFUJI Hideaki 			}
328cfb6eeb4SYOSHIFUJI Hideaki 		} while(0);
329cfb6eeb4SYOSHIFUJI Hideaki #endif
330cfb6eeb4SYOSHIFUJI Hideaki 
3311da177e4SLinus Torvalds 		/* Linkage updates. */
332e48c414eSArnaldo Carvalho de Melo 		__inet_twsk_hashdance(tw, sk, &tcp_hashinfo);
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 		/* Get the TIME_WAIT timeout firing. */
3351da177e4SLinus Torvalds 		if (timeo < rto)
3361da177e4SLinus Torvalds 			timeo = rto;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 		if (recycle_ok) {
3391da177e4SLinus Torvalds 			tw->tw_timeout = rto;
3401da177e4SLinus Torvalds 		} else {
3411da177e4SLinus Torvalds 			tw->tw_timeout = TCP_TIMEWAIT_LEN;
3421da177e4SLinus Torvalds 			if (state == TCP_TIME_WAIT)
3431da177e4SLinus Torvalds 				timeo = TCP_TIMEWAIT_LEN;
3441da177e4SLinus Torvalds 		}
3451da177e4SLinus Torvalds 
346696ab2d3SArnaldo Carvalho de Melo 		inet_twsk_schedule(tw, &tcp_death_row, timeo,
347696ab2d3SArnaldo Carvalho de Melo 				   TCP_TIMEWAIT_LEN);
3488feaf0c0SArnaldo Carvalho de Melo 		inet_twsk_put(tw);
3491da177e4SLinus Torvalds 	} else {
3501da177e4SLinus Torvalds 		/* Sorry, if we're out of memory, just CLOSE this
3511da177e4SLinus Torvalds 		 * socket up.  We've got bigger problems than
3521da177e4SLinus Torvalds 		 * non-graceful socket closings.
3531da177e4SLinus Torvalds 		 */
3541da177e4SLinus Torvalds 		if (net_ratelimit())
3551da177e4SLinus Torvalds 			printk(KERN_INFO "TCP: time wait bucket table overflow\n");
3561da177e4SLinus Torvalds 	}
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	tcp_update_metrics(sk);
3591da177e4SLinus Torvalds 	tcp_done(sk);
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
362cfb6eeb4SYOSHIFUJI Hideaki void tcp_twsk_destructor(struct sock *sk)
363cfb6eeb4SYOSHIFUJI Hideaki {
364cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
365a928630aSDavid S. Miller 	struct tcp_timewait_sock *twsk = tcp_twsk(sk);
366cfb6eeb4SYOSHIFUJI Hideaki 	if (twsk->tw_md5_keylen)
367cfb6eeb4SYOSHIFUJI Hideaki 		tcp_put_md5sig_pool();
368cfb6eeb4SYOSHIFUJI Hideaki #endif
369cfb6eeb4SYOSHIFUJI Hideaki }
370cfb6eeb4SYOSHIFUJI Hideaki 
371cfb6eeb4SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(tcp_twsk_destructor);
372cfb6eeb4SYOSHIFUJI Hideaki 
3731da177e4SLinus Torvalds /* This is not only more efficient than what we used to do, it eliminates
3741da177e4SLinus Torvalds  * a lot of code duplication between IPv4/IPv6 SYN recv processing. -DaveM
3751da177e4SLinus Torvalds  *
3761da177e4SLinus Torvalds  * Actually, we could lots of memory writes here. tp of listening
3771da177e4SLinus Torvalds  * socket contains all necessary default parameters.
3781da177e4SLinus Torvalds  */
37960236fddSArnaldo Carvalho de Melo struct sock *tcp_create_openreq_child(struct sock *sk, struct request_sock *req, struct sk_buff *skb)
3801da177e4SLinus Torvalds {
3819f1d2604SArnaldo Carvalho de Melo 	struct sock *newsk = inet_csk_clone(sk, req, GFP_ATOMIC);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	if (newsk != NULL) {
3849f1d2604SArnaldo Carvalho de Melo 		const struct inet_request_sock *ireq = inet_rsk(req);
3852e6599cbSArnaldo Carvalho de Melo 		struct tcp_request_sock *treq = tcp_rsk(req);
3869f1d2604SArnaldo Carvalho de Melo 		struct inet_connection_sock *newicsk = inet_csk(sk);
3871da177e4SLinus Torvalds 		struct tcp_sock *newtp;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 		/* Now setup tcp_sock */
3901da177e4SLinus Torvalds 		newtp = tcp_sk(newsk);
3911da177e4SLinus Torvalds 		newtp->pred_flags = 0;
3922e6599cbSArnaldo Carvalho de Melo 		newtp->rcv_nxt = treq->rcv_isn + 1;
39387d11cebSArnaldo Carvalho de Melo 		newtp->snd_nxt = newtp->snd_una = newtp->snd_sml = treq->snt_isn + 1;
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 		tcp_prequeue_init(newtp);
3961da177e4SLinus Torvalds 
3972e6599cbSArnaldo Carvalho de Melo 		tcp_init_wl(newtp, treq->snt_isn, treq->rcv_isn);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 		newtp->srtt = 0;
4001da177e4SLinus Torvalds 		newtp->mdev = TCP_TIMEOUT_INIT;
401463c84b9SArnaldo Carvalho de Melo 		newicsk->icsk_rto = TCP_TIMEOUT_INIT;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 		newtp->packets_out = 0;
4041da177e4SLinus Torvalds 		newtp->left_out = 0;
4051da177e4SLinus Torvalds 		newtp->retrans_out = 0;
4061da177e4SLinus Torvalds 		newtp->sacked_out = 0;
4071da177e4SLinus Torvalds 		newtp->fackets_out = 0;
4081da177e4SLinus Torvalds 		newtp->snd_ssthresh = 0x7fffffff;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 		/* So many TCP implementations out there (incorrectly) count the
4111da177e4SLinus Torvalds 		 * initial SYN frame in their delayed-ACK and congestion control
4121da177e4SLinus Torvalds 		 * algorithms that we must have the following bandaid to talk
4131da177e4SLinus Torvalds 		 * efficiently to them.  -DaveM
4141da177e4SLinus Torvalds 		 */
4151da177e4SLinus Torvalds 		newtp->snd_cwnd = 2;
4161da177e4SLinus Torvalds 		newtp->snd_cwnd_cnt = 0;
4179772efb9SStephen Hemminger 		newtp->bytes_acked = 0;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 		newtp->frto_counter = 0;
4201da177e4SLinus Torvalds 		newtp->frto_highmark = 0;
4211da177e4SLinus Torvalds 
4227957aed7SStephen Hemminger 		newicsk->icsk_ca_ops = &tcp_init_congestion_ops;
423317a76f9SStephen Hemminger 
4246687e988SArnaldo Carvalho de Melo 		tcp_set_ca_state(newsk, TCP_CA_Open);
4251da177e4SLinus Torvalds 		tcp_init_xmit_timers(newsk);
4261da177e4SLinus Torvalds 		skb_queue_head_init(&newtp->out_of_order_queue);
4272e6599cbSArnaldo Carvalho de Melo 		newtp->rcv_wup = treq->rcv_isn + 1;
4282e6599cbSArnaldo Carvalho de Melo 		newtp->write_seq = treq->snt_isn + 1;
4291da177e4SLinus Torvalds 		newtp->pushed_seq = newtp->write_seq;
4302e6599cbSArnaldo Carvalho de Melo 		newtp->copied_seq = treq->rcv_isn + 1;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 		newtp->rx_opt.saw_tstamp = 0;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 		newtp->rx_opt.dsack = 0;
4351da177e4SLinus Torvalds 		newtp->rx_opt.eff_sacks = 0;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 		newtp->rx_opt.num_sacks = 0;
4381da177e4SLinus Torvalds 		newtp->urg_data = 0;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 		if (sock_flag(newsk, SOCK_KEEPOPEN))
441463c84b9SArnaldo Carvalho de Melo 			inet_csk_reset_keepalive_timer(newsk,
4421da177e4SLinus Torvalds 						       keepalive_time_when(newtp));
4431da177e4SLinus Torvalds 
4442e6599cbSArnaldo Carvalho de Melo 		newtp->rx_opt.tstamp_ok = ireq->tstamp_ok;
4452e6599cbSArnaldo Carvalho de Melo 		if((newtp->rx_opt.sack_ok = ireq->sack_ok) != 0) {
4461da177e4SLinus Torvalds 			if (sysctl_tcp_fack)
4471da177e4SLinus Torvalds 				newtp->rx_opt.sack_ok |= 2;
4481da177e4SLinus Torvalds 		}
4491da177e4SLinus Torvalds 		newtp->window_clamp = req->window_clamp;
4501da177e4SLinus Torvalds 		newtp->rcv_ssthresh = req->rcv_wnd;
4511da177e4SLinus Torvalds 		newtp->rcv_wnd = req->rcv_wnd;
4522e6599cbSArnaldo Carvalho de Melo 		newtp->rx_opt.wscale_ok = ireq->wscale_ok;
4531da177e4SLinus Torvalds 		if (newtp->rx_opt.wscale_ok) {
4542e6599cbSArnaldo Carvalho de Melo 			newtp->rx_opt.snd_wscale = ireq->snd_wscale;
4552e6599cbSArnaldo Carvalho de Melo 			newtp->rx_opt.rcv_wscale = ireq->rcv_wscale;
4561da177e4SLinus Torvalds 		} else {
4571da177e4SLinus Torvalds 			newtp->rx_opt.snd_wscale = newtp->rx_opt.rcv_wscale = 0;
4581da177e4SLinus Torvalds 			newtp->window_clamp = min(newtp->window_clamp, 65535U);
4591da177e4SLinus Torvalds 		}
4601da177e4SLinus Torvalds 		newtp->snd_wnd = ntohs(skb->h.th->window) << newtp->rx_opt.snd_wscale;
4611da177e4SLinus Torvalds 		newtp->max_window = newtp->snd_wnd;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 		if (newtp->rx_opt.tstamp_ok) {
4641da177e4SLinus Torvalds 			newtp->rx_opt.ts_recent = req->ts_recent;
4651da177e4SLinus Torvalds 			newtp->rx_opt.ts_recent_stamp = xtime.tv_sec;
4661da177e4SLinus Torvalds 			newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
4671da177e4SLinus Torvalds 		} else {
4681da177e4SLinus Torvalds 			newtp->rx_opt.ts_recent_stamp = 0;
4691da177e4SLinus Torvalds 			newtp->tcp_header_len = sizeof(struct tcphdr);
4701da177e4SLinus Torvalds 		}
471cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
472cfb6eeb4SYOSHIFUJI Hideaki 		newtp->md5sig_info = NULL;	/*XXX*/
473cfb6eeb4SYOSHIFUJI Hideaki 		if (newtp->af_specific->md5_lookup(sk, newsk))
474cfb6eeb4SYOSHIFUJI Hideaki 			newtp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED;
475cfb6eeb4SYOSHIFUJI Hideaki #endif
4761da177e4SLinus Torvalds 		if (skb->len >= TCP_MIN_RCVMSS+newtp->tcp_header_len)
477463c84b9SArnaldo Carvalho de Melo 			newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len;
4781da177e4SLinus Torvalds 		newtp->rx_opt.mss_clamp = req->mss;
4791da177e4SLinus Torvalds 		TCP_ECN_openreq_child(newtp, req);
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 		TCP_INC_STATS_BH(TCP_MIB_PASSIVEOPENS);
4821da177e4SLinus Torvalds 	}
4831da177e4SLinus Torvalds 	return newsk;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds /*
4871da177e4SLinus Torvalds  *	Process an incoming packet for SYN_RECV sockets represented
48860236fddSArnaldo Carvalho de Melo  *	as a request_sock.
4891da177e4SLinus Torvalds  */
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds struct sock *tcp_check_req(struct sock *sk,struct sk_buff *skb,
49260236fddSArnaldo Carvalho de Melo 			   struct request_sock *req,
49360236fddSArnaldo Carvalho de Melo 			   struct request_sock **prev)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	struct tcphdr *th = skb->h.th;
496*714e85beSAl Viro 	__be32 flg = tcp_flag_word(th) & (TCP_FLAG_RST|TCP_FLAG_SYN|TCP_FLAG_ACK);
4971da177e4SLinus Torvalds 	int paws_reject = 0;
4981da177e4SLinus Torvalds 	struct tcp_options_received tmp_opt;
4991da177e4SLinus Torvalds 	struct sock *child;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	tmp_opt.saw_tstamp = 0;
5021da177e4SLinus Torvalds 	if (th->doff > (sizeof(struct tcphdr)>>2)) {
5031da177e4SLinus Torvalds 		tcp_parse_options(skb, &tmp_opt, 0);
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 		if (tmp_opt.saw_tstamp) {
5061da177e4SLinus Torvalds 			tmp_opt.ts_recent = req->ts_recent;
5071da177e4SLinus Torvalds 			/* We do not store true stamp, but it is not required,
5081da177e4SLinus Torvalds 			 * it can be estimated (approximately)
5091da177e4SLinus Torvalds 			 * from another data.
5101da177e4SLinus Torvalds 			 */
5111da177e4SLinus Torvalds 			tmp_opt.ts_recent_stamp = xtime.tv_sec - ((TCP_TIMEOUT_INIT/HZ)<<req->retrans);
5121da177e4SLinus Torvalds 			paws_reject = tcp_paws_check(&tmp_opt, th->rst);
5131da177e4SLinus Torvalds 		}
5141da177e4SLinus Torvalds 	}
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	/* Check for pure retransmitted SYN. */
5172e6599cbSArnaldo Carvalho de Melo 	if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn &&
5181da177e4SLinus Torvalds 	    flg == TCP_FLAG_SYN &&
5191da177e4SLinus Torvalds 	    !paws_reject) {
5201da177e4SLinus Torvalds 		/*
5211da177e4SLinus Torvalds 		 * RFC793 draws (Incorrectly! It was fixed in RFC1122)
5221da177e4SLinus Torvalds 		 * this case on figure 6 and figure 8, but formal
5231da177e4SLinus Torvalds 		 * protocol description says NOTHING.
5241da177e4SLinus Torvalds 		 * To be more exact, it says that we should send ACK,
5251da177e4SLinus Torvalds 		 * because this segment (at least, if it has no data)
5261da177e4SLinus Torvalds 		 * is out of window.
5271da177e4SLinus Torvalds 		 *
5281da177e4SLinus Torvalds 		 *  CONCLUSION: RFC793 (even with RFC1122) DOES NOT
5291da177e4SLinus Torvalds 		 *  describe SYN-RECV state. All the description
5301da177e4SLinus Torvalds 		 *  is wrong, we cannot believe to it and should
5311da177e4SLinus Torvalds 		 *  rely only on common sense and implementation
5321da177e4SLinus Torvalds 		 *  experience.
5331da177e4SLinus Torvalds 		 *
5341da177e4SLinus Torvalds 		 * Enforce "SYN-ACK" according to figure 8, figure 6
5351da177e4SLinus Torvalds 		 * of RFC793, fixed by RFC1122.
5361da177e4SLinus Torvalds 		 */
53760236fddSArnaldo Carvalho de Melo 		req->rsk_ops->rtx_syn_ack(sk, req, NULL);
5381da177e4SLinus Torvalds 		return NULL;
5391da177e4SLinus Torvalds 	}
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	/* Further reproduces section "SEGMENT ARRIVES"
5421da177e4SLinus Torvalds 	   for state SYN-RECEIVED of RFC793.
5431da177e4SLinus Torvalds 	   It is broken, however, it does not work only
5441da177e4SLinus Torvalds 	   when SYNs are crossed.
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	   You would think that SYN crossing is impossible here, since
5471da177e4SLinus Torvalds 	   we should have a SYN_SENT socket (from connect()) on our end,
5481da177e4SLinus Torvalds 	   but this is not true if the crossed SYNs were sent to both
5491da177e4SLinus Torvalds 	   ends by a malicious third party.  We must defend against this,
5501da177e4SLinus Torvalds 	   and to do that we first verify the ACK (as per RFC793, page
5511da177e4SLinus Torvalds 	   36) and reset if it is invalid.  Is this a true full defense?
5521da177e4SLinus Torvalds 	   To convince ourselves, let us consider a way in which the ACK
5531da177e4SLinus Torvalds 	   test can still pass in this 'malicious crossed SYNs' case.
5541da177e4SLinus Torvalds 	   Malicious sender sends identical SYNs (and thus identical sequence
5551da177e4SLinus Torvalds 	   numbers) to both A and B:
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 		A: gets SYN, seq=7
5581da177e4SLinus Torvalds 		B: gets SYN, seq=7
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	   By our good fortune, both A and B select the same initial
5611da177e4SLinus Torvalds 	   send sequence number of seven :-)
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 		A: sends SYN|ACK, seq=7, ack_seq=8
5641da177e4SLinus Torvalds 		B: sends SYN|ACK, seq=7, ack_seq=8
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	   So we are now A eating this SYN|ACK, ACK test passes.  So
5671da177e4SLinus Torvalds 	   does sequence test, SYN is truncated, and thus we consider
5681da177e4SLinus Torvalds 	   it a bare ACK.
5691da177e4SLinus Torvalds 
570295f7324SArnaldo Carvalho de Melo 	   If icsk->icsk_accept_queue.rskq_defer_accept, we silently drop this
571295f7324SArnaldo Carvalho de Melo 	   bare ACK.  Otherwise, we create an established connection.  Both
572295f7324SArnaldo Carvalho de Melo 	   ends (listening sockets) accept the new incoming connection and try
573295f7324SArnaldo Carvalho de Melo 	   to talk to each other. 8-)
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	   Note: This case is both harmless, and rare.  Possibility is about the
5761da177e4SLinus Torvalds 	   same as us discovering intelligent life on another plant tomorrow.
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	   But generally, we should (RFC lies!) to accept ACK
5791da177e4SLinus Torvalds 	   from SYNACK both here and in tcp_rcv_state_process().
5801da177e4SLinus Torvalds 	   tcp_rcv_state_process() does not, hence, we do not too.
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	   Note that the case is absolutely generic:
5831da177e4SLinus Torvalds 	   we cannot optimize anything here without
5841da177e4SLinus Torvalds 	   violating protocol. All the checks must be made
5851da177e4SLinus Torvalds 	   before attempt to create socket.
5861da177e4SLinus Torvalds 	 */
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	/* RFC793 page 36: "If the connection is in any non-synchronized state ...
5891da177e4SLinus Torvalds 	 *                  and the incoming segment acknowledges something not yet
590caa20d9aSStephen Hemminger 	 *                  sent (the segment carries an unacceptable ACK) ...
5911da177e4SLinus Torvalds 	 *                  a reset is sent."
5921da177e4SLinus Torvalds 	 *
5931da177e4SLinus Torvalds 	 * Invalid ACK: reset will be sent by listening socket
5941da177e4SLinus Torvalds 	 */
5951da177e4SLinus Torvalds 	if ((flg & TCP_FLAG_ACK) &&
5962e6599cbSArnaldo Carvalho de Melo 	    (TCP_SKB_CB(skb)->ack_seq != tcp_rsk(req)->snt_isn + 1))
5971da177e4SLinus Torvalds 		return sk;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	/* Also, it would be not so bad idea to check rcv_tsecr, which
6001da177e4SLinus Torvalds 	 * is essentially ACK extension and too early or too late values
6011da177e4SLinus Torvalds 	 * should cause reset in unsynchronized states.
6021da177e4SLinus Torvalds 	 */
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	/* RFC793: "first check sequence number". */
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	if (paws_reject || !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
6072e6599cbSArnaldo Carvalho de Melo 					  tcp_rsk(req)->rcv_isn + 1, tcp_rsk(req)->rcv_isn + 1 + req->rcv_wnd)) {
6081da177e4SLinus Torvalds 		/* Out of window: send ACK and drop. */
6091da177e4SLinus Torvalds 		if (!(flg & TCP_FLAG_RST))
61060236fddSArnaldo Carvalho de Melo 			req->rsk_ops->send_ack(skb, req);
6111da177e4SLinus Torvalds 		if (paws_reject)
6121da177e4SLinus Torvalds 			NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED);
6131da177e4SLinus Torvalds 		return NULL;
6141da177e4SLinus Torvalds 	}
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	/* In sequence, PAWS is OK. */
6171da177e4SLinus Torvalds 
6182e6599cbSArnaldo Carvalho de Melo 	if (tmp_opt.saw_tstamp && !after(TCP_SKB_CB(skb)->seq, tcp_rsk(req)->rcv_isn + 1))
6191da177e4SLinus Torvalds 			req->ts_recent = tmp_opt.rcv_tsval;
6201da177e4SLinus Torvalds 
6212e6599cbSArnaldo Carvalho de Melo 		if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn) {
6221da177e4SLinus Torvalds 			/* Truncate SYN, it is out of window starting
6232e6599cbSArnaldo Carvalho de Melo 			   at tcp_rsk(req)->rcv_isn + 1. */
6241da177e4SLinus Torvalds 			flg &= ~TCP_FLAG_SYN;
6251da177e4SLinus Torvalds 		}
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 		/* RFC793: "second check the RST bit" and
6281da177e4SLinus Torvalds 		 *	   "fourth, check the SYN bit"
6291da177e4SLinus Torvalds 		 */
6303687b1dcSWei Yongjun 		if (flg & (TCP_FLAG_RST|TCP_FLAG_SYN)) {
6313687b1dcSWei Yongjun 			TCP_INC_STATS_BH(TCP_MIB_ATTEMPTFAILS);
6321da177e4SLinus Torvalds 			goto embryonic_reset;
6333687b1dcSWei Yongjun 		}
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 		/* ACK sequence verified above, just make sure ACK is
6361da177e4SLinus Torvalds 		 * set.  If ACK not set, just silently drop the packet.
6371da177e4SLinus Torvalds 		 */
6381da177e4SLinus Torvalds 		if (!(flg & TCP_FLAG_ACK))
6391da177e4SLinus Torvalds 			return NULL;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 		/* If TCP_DEFER_ACCEPT is set, drop bare ACK. */
642295f7324SArnaldo Carvalho de Melo 		if (inet_csk(sk)->icsk_accept_queue.rskq_defer_accept &&
643295f7324SArnaldo Carvalho de Melo 		    TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) {
6442e6599cbSArnaldo Carvalho de Melo 			inet_rsk(req)->acked = 1;
6451da177e4SLinus Torvalds 			return NULL;
6461da177e4SLinus Torvalds 		}
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 		/* OK, ACK is valid, create big socket and
6491da177e4SLinus Torvalds 		 * feed this segment to it. It will repeat all
6501da177e4SLinus Torvalds 		 * the tests. THIS SEGMENT MUST MOVE SOCKET TO
6511da177e4SLinus Torvalds 		 * ESTABLISHED STATE. If it will be dropped after
6521da177e4SLinus Torvalds 		 * socket is created, wait for troubles.
6531da177e4SLinus Torvalds 		 */
6548292a17aSArnaldo Carvalho de Melo 		child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb,
6558292a17aSArnaldo Carvalho de Melo 								 req, NULL);
6561da177e4SLinus Torvalds 		if (child == NULL)
6571da177e4SLinus Torvalds 			goto listen_overflow;
658cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
659cfb6eeb4SYOSHIFUJI Hideaki 		else {
660cfb6eeb4SYOSHIFUJI Hideaki 			/* Copy over the MD5 key from the original socket */
661cfb6eeb4SYOSHIFUJI Hideaki 			struct tcp_md5sig_key *key;
662cfb6eeb4SYOSHIFUJI Hideaki 			struct tcp_sock *tp = tcp_sk(sk);
663cfb6eeb4SYOSHIFUJI Hideaki 			key = tp->af_specific->md5_lookup(sk, child);
664cfb6eeb4SYOSHIFUJI Hideaki 			if (key != NULL) {
665cfb6eeb4SYOSHIFUJI Hideaki 				/*
666cfb6eeb4SYOSHIFUJI Hideaki 				 * We're using one, so create a matching key on the
667cfb6eeb4SYOSHIFUJI Hideaki 				 * newsk structure. If we fail to get memory then we
668cfb6eeb4SYOSHIFUJI Hideaki 				 * end up not copying the key across. Shucks.
669cfb6eeb4SYOSHIFUJI Hideaki 				 */
670cfb6eeb4SYOSHIFUJI Hideaki 				char *newkey = kmalloc(key->keylen, GFP_ATOMIC);
671cfb6eeb4SYOSHIFUJI Hideaki 				if (newkey) {
672cfb6eeb4SYOSHIFUJI Hideaki 					if (!tcp_alloc_md5sig_pool())
673cfb6eeb4SYOSHIFUJI Hideaki 						BUG();
674cfb6eeb4SYOSHIFUJI Hideaki 					memcpy(newkey, key->key, key->keylen);
675cfb6eeb4SYOSHIFUJI Hideaki 					tp->af_specific->md5_add(child, child,
676cfb6eeb4SYOSHIFUJI Hideaki 								 newkey,
677cfb6eeb4SYOSHIFUJI Hideaki 								 key->keylen);
678cfb6eeb4SYOSHIFUJI Hideaki 				}
679cfb6eeb4SYOSHIFUJI Hideaki 			}
680cfb6eeb4SYOSHIFUJI Hideaki 		}
681cfb6eeb4SYOSHIFUJI Hideaki #endif
6821da177e4SLinus Torvalds 
683463c84b9SArnaldo Carvalho de Melo 		inet_csk_reqsk_queue_unlink(sk, req, prev);
684463c84b9SArnaldo Carvalho de Melo 		inet_csk_reqsk_queue_removed(sk, req);
6851da177e4SLinus Torvalds 
686463c84b9SArnaldo Carvalho de Melo 		inet_csk_reqsk_queue_add(sk, req, child);
6871da177e4SLinus Torvalds 		return child;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	listen_overflow:
6901da177e4SLinus Torvalds 		if (!sysctl_tcp_abort_on_overflow) {
6912e6599cbSArnaldo Carvalho de Melo 			inet_rsk(req)->acked = 1;
6921da177e4SLinus Torvalds 			return NULL;
6931da177e4SLinus Torvalds 		}
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	embryonic_reset:
6961da177e4SLinus Torvalds 		NET_INC_STATS_BH(LINUX_MIB_EMBRYONICRSTS);
6971da177e4SLinus Torvalds 		if (!(flg & TCP_FLAG_RST))
698cfb6eeb4SYOSHIFUJI Hideaki 			req->rsk_ops->send_reset(sk, skb);
6991da177e4SLinus Torvalds 
700463c84b9SArnaldo Carvalho de Melo 		inet_csk_reqsk_queue_drop(sk, req, prev);
7011da177e4SLinus Torvalds 		return NULL;
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds /*
7051da177e4SLinus Torvalds  * Queue segment on the new socket if the new socket is active,
7061da177e4SLinus Torvalds  * otherwise we just shortcircuit this and continue with
7071da177e4SLinus Torvalds  * the new socket.
7081da177e4SLinus Torvalds  */
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds int tcp_child_process(struct sock *parent, struct sock *child,
7111da177e4SLinus Torvalds 		      struct sk_buff *skb)
7121da177e4SLinus Torvalds {
7131da177e4SLinus Torvalds 	int ret = 0;
7141da177e4SLinus Torvalds 	int state = child->sk_state;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	if (!sock_owned_by_user(child)) {
7171da177e4SLinus Torvalds 		ret = tcp_rcv_state_process(child, skb, skb->h.th, skb->len);
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 		/* Wakeup parent, send SIGIO */
7201da177e4SLinus Torvalds 		if (state == TCP_SYN_RECV && child->sk_state != state)
7211da177e4SLinus Torvalds 			parent->sk_data_ready(parent, 0);
7221da177e4SLinus Torvalds 	} else {
7231da177e4SLinus Torvalds 		/* Alas, it is possible again, because we do lookup
7241da177e4SLinus Torvalds 		 * in main socket hash table and lock on listening
7251da177e4SLinus Torvalds 		 * socket does not protect us more.
7261da177e4SLinus Torvalds 		 */
7271da177e4SLinus Torvalds 		sk_add_backlog(child, skb);
7281da177e4SLinus Torvalds 	}
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	bh_unlock_sock(child);
7311da177e4SLinus Torvalds 	sock_put(child);
7321da177e4SLinus Torvalds 	return ret;
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_check_req);
7361da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_child_process);
7371da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_create_openreq_child);
7381da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_timewait_state_process);
739