xref: /openbmc/linux/net/ipv4/tcp_minisocks.c (revision ec94c2696f0bcd5ae92a553244e4ac30d2171a2d)
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>
2660e2a778SUrsula Braun #include <linux/static_key.h>
271da177e4SLinus Torvalds #include <net/tcp.h>
281da177e4SLinus Torvalds #include <net/inet_common.h>
291da177e4SLinus Torvalds #include <net/xfrm.h>
30e5907459SAlexander Duyck #include <net/busy_poll.h>
311da177e4SLinus Torvalds 
32a2a385d6SEric Dumazet static bool tcp_in_window(u32 seq, u32 end_seq, u32 s_win, u32 e_win)
331da177e4SLinus Torvalds {
341da177e4SLinus Torvalds 	if (seq == s_win)
35a2a385d6SEric Dumazet 		return true;
361da177e4SLinus Torvalds 	if (after(end_seq, s_win) && before(seq, e_win))
37a2a385d6SEric Dumazet 		return true;
38a02cec21SEric Dumazet 	return seq == e_win && seq == end_seq;
391da177e4SLinus Torvalds }
401da177e4SLinus Torvalds 
414fb17a60SNeal Cardwell static enum tcp_tw_status
424fb17a60SNeal Cardwell tcp_timewait_check_oow_rate_limit(struct inet_timewait_sock *tw,
434fb17a60SNeal Cardwell 				  const struct sk_buff *skb, int mib_idx)
444fb17a60SNeal Cardwell {
454fb17a60SNeal Cardwell 	struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
464fb17a60SNeal Cardwell 
474fb17a60SNeal Cardwell 	if (!tcp_oow_rate_limited(twsk_net(tw), skb, mib_idx,
484fb17a60SNeal Cardwell 				  &tcptw->tw_last_oow_ack_time)) {
494fb17a60SNeal Cardwell 		/* Send ACK. Note, we do not put the bucket,
504fb17a60SNeal Cardwell 		 * it will be released by caller.
514fb17a60SNeal Cardwell 		 */
524fb17a60SNeal Cardwell 		return TCP_TW_ACK;
534fb17a60SNeal Cardwell 	}
544fb17a60SNeal Cardwell 
554fb17a60SNeal Cardwell 	/* We are rate-limiting, so just release the tw sock and drop skb. */
564fb17a60SNeal Cardwell 	inet_twsk_put(tw);
574fb17a60SNeal Cardwell 	return TCP_TW_SUCCESS;
584fb17a60SNeal Cardwell }
594fb17a60SNeal Cardwell 
601da177e4SLinus Torvalds /*
611da177e4SLinus Torvalds  * * Main purpose of TIME-WAIT state is to close connection gracefully,
621da177e4SLinus Torvalds  *   when one of ends sits in LAST-ACK or CLOSING retransmitting FIN
631da177e4SLinus Torvalds  *   (and, probably, tail of data) and one or more our ACKs are lost.
641da177e4SLinus Torvalds  * * What is TIME-WAIT timeout? It is associated with maximal packet
651da177e4SLinus Torvalds  *   lifetime in the internet, which results in wrong conclusion, that
661da177e4SLinus Torvalds  *   it is set to catch "old duplicate segments" wandering out of their path.
671da177e4SLinus Torvalds  *   It is not quite correct. This timeout is calculated so that it exceeds
681da177e4SLinus Torvalds  *   maximal retransmission timeout enough to allow to lose one (or more)
691da177e4SLinus Torvalds  *   segments sent by peer and our ACKs. This time may be calculated from RTO.
701da177e4SLinus Torvalds  * * When TIME-WAIT socket receives RST, it means that another end
711da177e4SLinus Torvalds  *   finally closed and we are allowed to kill TIME-WAIT too.
721da177e4SLinus Torvalds  * * Second purpose of TIME-WAIT is catching old duplicate segments.
731da177e4SLinus Torvalds  *   Well, certainly it is pure paranoia, but if we load TIME-WAIT
741da177e4SLinus Torvalds  *   with this semantics, we MUST NOT kill TIME-WAIT state with RSTs.
751da177e4SLinus Torvalds  * * If we invented some more clever way to catch duplicates
761da177e4SLinus Torvalds  *   (f.e. based on PAWS), we could truncate TIME-WAIT to several RTOs.
771da177e4SLinus Torvalds  *
781da177e4SLinus Torvalds  * The algorithm below is based on FORMAL INTERPRETATION of RFCs.
791da177e4SLinus Torvalds  * When you compare it to RFCs, please, read section SEGMENT ARRIVES
801da177e4SLinus Torvalds  * from the very beginning.
811da177e4SLinus Torvalds  *
821da177e4SLinus Torvalds  * NOTE. With recycling (and later with fin-wait-2) TW bucket
831da177e4SLinus Torvalds  * is _not_ stateless. It means, that strictly speaking we must
841da177e4SLinus Torvalds  * spinlock it. I do not want! Well, probability of misbehaviour
851da177e4SLinus Torvalds  * is ridiculously low and, seems, we could use some mb() tricks
861da177e4SLinus Torvalds  * to avoid misread sequence numbers, states etc.  --ANK
874308fc58SAlan Cox  *
884308fc58SAlan Cox  * We don't need to initialize tmp_out.sack_ok as we don't use the results
891da177e4SLinus Torvalds  */
901da177e4SLinus Torvalds enum tcp_tw_status
918feaf0c0SArnaldo Carvalho de Melo tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
928feaf0c0SArnaldo Carvalho de Melo 			   const struct tcphdr *th)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds 	struct tcp_options_received tmp_opt;
954957faadSWilliam Allen Simpson 	struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
96a2a385d6SEric Dumazet 	bool paws_reject = false;
971da177e4SLinus Torvalds 
98bb5b7c11SDavid S. Miller 	tmp_opt.saw_tstamp = 0;
998feaf0c0SArnaldo Carvalho de Melo 	if (th->doff > (sizeof(*th) >> 2) && tcptw->tw_ts_recent_stamp) {
100eed29f17SEric Dumazet 		tcp_parse_options(twsk_net(tw), skb, &tmp_opt, 0, NULL);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 		if (tmp_opt.saw_tstamp) {
103eee2faabSAlexey Kodanev 			if (tmp_opt.rcv_tsecr)
104ee684b6fSAndrey Vagin 				tmp_opt.rcv_tsecr -= tcptw->tw_ts_offset;
1058feaf0c0SArnaldo Carvalho de Melo 			tmp_opt.ts_recent	= tcptw->tw_ts_recent;
1068feaf0c0SArnaldo Carvalho de Melo 			tmp_opt.ts_recent_stamp	= tcptw->tw_ts_recent_stamp;
107c887e6d2SIlpo Järvinen 			paws_reject = tcp_paws_reject(&tmp_opt, th->rst);
1081da177e4SLinus Torvalds 		}
1091da177e4SLinus Torvalds 	}
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	if (tw->tw_substate == TCP_FIN_WAIT2) {
1121da177e4SLinus Torvalds 		/* Just repeat all the checks of tcp_rcv_state_process() */
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 		/* Out of window, send ACK */
1151da177e4SLinus Torvalds 		if (paws_reject ||
1161da177e4SLinus Torvalds 		    !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
1178feaf0c0SArnaldo Carvalho de Melo 				   tcptw->tw_rcv_nxt,
1188feaf0c0SArnaldo Carvalho de Melo 				   tcptw->tw_rcv_nxt + tcptw->tw_rcv_wnd))
1194fb17a60SNeal Cardwell 			return tcp_timewait_check_oow_rate_limit(
1204fb17a60SNeal Cardwell 				tw, skb, LINUX_MIB_TCPACKSKIPPEDFINWAIT2);
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 		if (th->rst)
1231da177e4SLinus Torvalds 			goto kill;
1241da177e4SLinus Torvalds 
1258feaf0c0SArnaldo Carvalho de Melo 		if (th->syn && !before(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt))
126271c3b9bSFlorian Westphal 			return TCP_TW_RST;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 		/* Dup ACK? */
1291ac530b3SWei Yongjun 		if (!th->ack ||
1301ac530b3SWei Yongjun 		    !after(TCP_SKB_CB(skb)->end_seq, tcptw->tw_rcv_nxt) ||
1311da177e4SLinus Torvalds 		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) {
1328feaf0c0SArnaldo Carvalho de Melo 			inet_twsk_put(tw);
1331da177e4SLinus Torvalds 			return TCP_TW_SUCCESS;
1341da177e4SLinus Torvalds 		}
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 		/* New data or FIN. If new data arrive after half-duplex close,
1371da177e4SLinus Torvalds 		 * reset.
1381da177e4SLinus Torvalds 		 */
1391da177e4SLinus Torvalds 		if (!th->fin ||
140271c3b9bSFlorian Westphal 		    TCP_SKB_CB(skb)->end_seq != tcptw->tw_rcv_nxt + 1)
1411da177e4SLinus Torvalds 			return TCP_TW_RST;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 		/* FIN arrived, enter true time-wait state. */
1441da177e4SLinus Torvalds 		tw->tw_substate	  = TCP_TIME_WAIT;
1458feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_rcv_nxt = TCP_SKB_CB(skb)->end_seq;
1461da177e4SLinus Torvalds 		if (tmp_opt.saw_tstamp) {
1479d729f72SJames Morris 			tcptw->tw_ts_recent_stamp = get_seconds();
1488feaf0c0SArnaldo Carvalho de Melo 			tcptw->tw_ts_recent	  = tmp_opt.rcv_tsval;
1491da177e4SLinus Torvalds 		}
1501da177e4SLinus Torvalds 
151ed2e9239SEric Dumazet 		inet_twsk_reschedule(tw, TCP_TIMEWAIT_LEN);
1521da177e4SLinus Torvalds 		return TCP_TW_ACK;
1531da177e4SLinus Torvalds 	}
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	/*
1561da177e4SLinus Torvalds 	 *	Now real TIME-WAIT state.
1571da177e4SLinus Torvalds 	 *
1581da177e4SLinus Torvalds 	 *	RFC 1122:
1591da177e4SLinus Torvalds 	 *	"When a connection is [...] on TIME-WAIT state [...]
1601da177e4SLinus Torvalds 	 *	[a TCP] MAY accept a new SYN from the remote TCP to
1611da177e4SLinus Torvalds 	 *	reopen the connection directly, if it:
1621da177e4SLinus Torvalds 	 *
1631da177e4SLinus Torvalds 	 *	(1)  assigns its initial sequence number for the new
1641da177e4SLinus Torvalds 	 *	connection to be larger than the largest sequence
1651da177e4SLinus Torvalds 	 *	number it used on the previous connection incarnation,
1661da177e4SLinus Torvalds 	 *	and
1671da177e4SLinus Torvalds 	 *
1681da177e4SLinus Torvalds 	 *	(2)  returns to TIME-WAIT state if the SYN turns out
1691da177e4SLinus Torvalds 	 *	to be an old duplicate".
1701da177e4SLinus Torvalds 	 */
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	if (!paws_reject &&
1738feaf0c0SArnaldo Carvalho de Melo 	    (TCP_SKB_CB(skb)->seq == tcptw->tw_rcv_nxt &&
1741da177e4SLinus Torvalds 	     (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq || th->rst))) {
1751da177e4SLinus Torvalds 		/* In window segment, it may be only reset or bare ack. */
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 		if (th->rst) {
178caa20d9aSStephen Hemminger 			/* This is TIME_WAIT assassination, in two flavors.
1791da177e4SLinus Torvalds 			 * Oh well... nobody has a sufficient solution to this
1801da177e4SLinus Torvalds 			 * protocol bug yet.
1811da177e4SLinus Torvalds 			 */
182625357aaSEric Dumazet 			if (twsk_net(tw)->ipv4.sysctl_tcp_rfc1337 == 0) {
1831da177e4SLinus Torvalds kill:
184dbe7faa4SEric Dumazet 				inet_twsk_deschedule_put(tw);
1851da177e4SLinus Torvalds 				return TCP_TW_SUCCESS;
1861da177e4SLinus Torvalds 			}
1871da177e4SLinus Torvalds 		}
188ed2e9239SEric Dumazet 		inet_twsk_reschedule(tw, TCP_TIMEWAIT_LEN);
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 		if (tmp_opt.saw_tstamp) {
1918feaf0c0SArnaldo Carvalho de Melo 			tcptw->tw_ts_recent	  = tmp_opt.rcv_tsval;
1929d729f72SJames Morris 			tcptw->tw_ts_recent_stamp = get_seconds();
1931da177e4SLinus Torvalds 		}
1941da177e4SLinus Torvalds 
1958feaf0c0SArnaldo Carvalho de Melo 		inet_twsk_put(tw);
1961da177e4SLinus Torvalds 		return TCP_TW_SUCCESS;
1971da177e4SLinus Torvalds 	}
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	/* Out of window segment.
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	   All the segments are ACKed immediately.
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	   The only exception is new SYN. We accept it, if it is
2041da177e4SLinus Torvalds 	   not old duplicate and we are not in danger to be killed
2051da177e4SLinus Torvalds 	   by delayed old duplicates. RFC check is that it has
2061da177e4SLinus Torvalds 	   newer sequence number works at rates <40Mbit/sec.
2071da177e4SLinus Torvalds 	   However, if paws works, it is reliable AND even more,
2081da177e4SLinus Torvalds 	   we even may relax silly seq space cutoff.
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	   RED-PEN: we violate main RFC requirement, if this SYN will appear
2111da177e4SLinus Torvalds 	   old duplicate (i.e. we receive RST in reply to SYN-ACK),
2121da177e4SLinus Torvalds 	   we must return socket to time-wait state. It is not good,
2131da177e4SLinus Torvalds 	   but not fatal yet.
2141da177e4SLinus Torvalds 	 */
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	if (th->syn && !th->rst && !th->ack && !paws_reject &&
2178feaf0c0SArnaldo Carvalho de Melo 	    (after(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt) ||
2188feaf0c0SArnaldo Carvalho de Melo 	     (tmp_opt.saw_tstamp &&
2198feaf0c0SArnaldo Carvalho de Melo 	      (s32)(tcptw->tw_ts_recent - tmp_opt.rcv_tsval) < 0))) {
2208feaf0c0SArnaldo Carvalho de Melo 		u32 isn = tcptw->tw_snd_nxt + 65535 + 2;
2211da177e4SLinus Torvalds 		if (isn == 0)
2221da177e4SLinus Torvalds 			isn++;
22304317dafSEric Dumazet 		TCP_SKB_CB(skb)->tcp_tw_isn = isn;
2241da177e4SLinus Torvalds 		return TCP_TW_SYN;
2251da177e4SLinus Torvalds 	}
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	if (paws_reject)
22802a1d6e7SEric Dumazet 		__NET_INC_STATS(twsk_net(tw), LINUX_MIB_PAWSESTABREJECTED);
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	if (!th->rst) {
2311da177e4SLinus Torvalds 		/* In this case we must reset the TIMEWAIT timer.
2321da177e4SLinus Torvalds 		 *
2331da177e4SLinus Torvalds 		 * If it is ACKless SYN it may be both old duplicate
2341da177e4SLinus Torvalds 		 * and new good SYN with random sequence number <rcv_nxt.
2351da177e4SLinus Torvalds 		 * Do not reschedule in the last case.
2361da177e4SLinus Torvalds 		 */
2371da177e4SLinus Torvalds 		if (paws_reject || th->ack)
238ed2e9239SEric Dumazet 			inet_twsk_reschedule(tw, TCP_TIMEWAIT_LEN);
2391da177e4SLinus Torvalds 
2404fb17a60SNeal Cardwell 		return tcp_timewait_check_oow_rate_limit(
2414fb17a60SNeal Cardwell 			tw, skb, LINUX_MIB_TCPACKSKIPPEDTIMEWAIT);
2421da177e4SLinus Torvalds 	}
2438feaf0c0SArnaldo Carvalho de Melo 	inet_twsk_put(tw);
2441da177e4SLinus Torvalds 	return TCP_TW_SUCCESS;
2451da177e4SLinus Torvalds }
2464bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_timewait_state_process);
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds /*
2491da177e4SLinus Torvalds  * Move a socket to time-wait or dead fin-wait-2 state.
2501da177e4SLinus Torvalds  */
2511da177e4SLinus Torvalds void tcp_time_wait(struct sock *sk, int state, int timeo)
2521da177e4SLinus Torvalds {
2538292a17aSArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
2548feaf0c0SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
255789f558cSEric Dumazet 	struct inet_timewait_sock *tw;
2561946e672SHaishuang Yan 	struct inet_timewait_death_row *tcp_death_row = &sock_net(sk)->ipv4.tcp_death_row;
2571da177e4SLinus Torvalds 
2581946e672SHaishuang Yan 	tw = inet_twsk_alloc(sk, tcp_death_row, state);
2591da177e4SLinus Torvalds 
26000db4124SIan Morris 	if (tw) {
2618feaf0c0SArnaldo Carvalho de Melo 		struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
262463c84b9SArnaldo Carvalho de Melo 		const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
2632397849bSDavid S. Miller 		struct inet_sock *inet = inet_sk(sk);
2648feaf0c0SArnaldo Carvalho de Melo 
2652397849bSDavid S. Miller 		tw->tw_transparent	= inet->transparent;
2661da177e4SLinus Torvalds 		tw->tw_rcv_wscale	= tp->rx_opt.rcv_wscale;
2678feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_rcv_nxt	= tp->rcv_nxt;
2688feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_snd_nxt	= tp->snd_nxt;
2698feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_rcv_wnd	= tcp_receive_window(tp);
2708feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_ts_recent	= tp->rx_opt.ts_recent;
2718feaf0c0SArnaldo Carvalho de Melo 		tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp;
272ceaa1fefSAndrey Vagin 		tcptw->tw_ts_offset	= tp->tsoffset;
2734fb17a60SNeal Cardwell 		tcptw->tw_last_oow_ack_time = 0;
2741da177e4SLinus Torvalds 
275dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
2761da177e4SLinus Torvalds 		if (tw->tw_family == PF_INET6) {
2771da177e4SLinus Torvalds 			struct ipv6_pinfo *np = inet6_sk(sk);
2781da177e4SLinus Torvalds 
279efe4208fSEric Dumazet 			tw->tw_v6_daddr = sk->sk_v6_daddr;
280efe4208fSEric Dumazet 			tw->tw_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
281b903d324SEric Dumazet 			tw->tw_tclass = np->tclass;
28221858cd0SFlorent Fourcot 			tw->tw_flowlabel = be32_to_cpu(np->flow_label & IPV6_FLOWLABEL_MASK);
2839fe516baSEric Dumazet 			tw->tw_ipv6only = sk->sk_ipv6only;
284c676270bSArnaldo Carvalho de Melo 		}
2851da177e4SLinus Torvalds #endif
286cfb6eeb4SYOSHIFUJI Hideaki 
287cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
288cfb6eeb4SYOSHIFUJI Hideaki 		/*
289cfb6eeb4SYOSHIFUJI Hideaki 		 * The timewait bucket does not have the key DB from the
290cfb6eeb4SYOSHIFUJI Hideaki 		 * sock structure. We just make a quick copy of the
291cfb6eeb4SYOSHIFUJI Hideaki 		 * md5 key being used (if indeed we are using one)
292cfb6eeb4SYOSHIFUJI Hideaki 		 * so the timewait ack generating code has the key.
293cfb6eeb4SYOSHIFUJI Hideaki 		 */
294cfb6eeb4SYOSHIFUJI Hideaki 		do {
295cfb6eeb4SYOSHIFUJI Hideaki 			struct tcp_md5sig_key *key;
296a915da9bSEric Dumazet 			tcptw->tw_md5_key = NULL;
297cfb6eeb4SYOSHIFUJI Hideaki 			key = tp->af_specific->md5_lookup(sk, sk);
29800db4124SIan Morris 			if (key) {
299a915da9bSEric Dumazet 				tcptw->tw_md5_key = kmemdup(key, sizeof(*key), GFP_ATOMIC);
30049ca1943SGustavo A. R. Silva 				BUG_ON(tcptw->tw_md5_key && !tcp_alloc_md5sig_pool());
301cfb6eeb4SYOSHIFUJI Hideaki 			}
302cfb6eeb4SYOSHIFUJI Hideaki 		} while (0);
303cfb6eeb4SYOSHIFUJI Hideaki #endif
304cfb6eeb4SYOSHIFUJI Hideaki 
3051da177e4SLinus Torvalds 		/* Get the TIME_WAIT timeout firing. */
3061da177e4SLinus Torvalds 		if (timeo < rto)
3071da177e4SLinus Torvalds 			timeo = rto;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 		tw->tw_timeout = TCP_TIMEWAIT_LEN;
3101da177e4SLinus Torvalds 		if (state == TCP_TIME_WAIT)
3111da177e4SLinus Torvalds 			timeo = TCP_TIMEWAIT_LEN;
3121da177e4SLinus Torvalds 
313cfac7f83SEric Dumazet 		/* tw_timer is pinned, so we need to make sure BH are disabled
314cfac7f83SEric Dumazet 		 * in following section, otherwise timer handler could run before
315cfac7f83SEric Dumazet 		 * we complete the initialization.
316cfac7f83SEric Dumazet 		 */
317cfac7f83SEric Dumazet 		local_bh_disable();
318789f558cSEric Dumazet 		inet_twsk_schedule(tw, timeo);
319*ec94c269SEric Dumazet 		/* Linkage updates.
320*ec94c269SEric Dumazet 		 * Note that access to tw after this point is illegal.
321*ec94c269SEric Dumazet 		 */
322*ec94c269SEric Dumazet 		inet_twsk_hashdance(tw, sk, &tcp_hashinfo);
323cfac7f83SEric Dumazet 		local_bh_enable();
3241da177e4SLinus Torvalds 	} else {
3251da177e4SLinus Torvalds 		/* Sorry, if we're out of memory, just CLOSE this
3261da177e4SLinus Torvalds 		 * socket up.  We've got bigger problems than
3271da177e4SLinus Torvalds 		 * non-graceful socket closings.
3281da177e4SLinus Torvalds 		 */
329c10d9310SEric Dumazet 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPTIMEWAITOVERFLOW);
3301da177e4SLinus Torvalds 	}
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	tcp_update_metrics(sk);
3331da177e4SLinus Torvalds 	tcp_done(sk);
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
336cfb6eeb4SYOSHIFUJI Hideaki void tcp_twsk_destructor(struct sock *sk)
337cfb6eeb4SYOSHIFUJI Hideaki {
338b6242b9bSDavid S. Miller #ifdef CONFIG_TCP_MD5SIG
339a928630aSDavid S. Miller 	struct tcp_timewait_sock *twsk = tcp_twsk(sk);
3402397849bSDavid S. Miller 
34171cea17eSEric Dumazet 	if (twsk->tw_md5_key)
342a915da9bSEric Dumazet 		kfree_rcu(twsk->tw_md5_key, rcu);
343cfb6eeb4SYOSHIFUJI Hideaki #endif
344cfb6eeb4SYOSHIFUJI Hideaki }
345cfb6eeb4SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(tcp_twsk_destructor);
346cfb6eeb4SYOSHIFUJI Hideaki 
347b1964b5fSEric Dumazet /* Warning : This function is called without sk_listener being locked.
348b1964b5fSEric Dumazet  * Be sure to read socket fields once, as their value could change under us.
349b1964b5fSEric Dumazet  */
350843f4a55SYuchung Cheng void tcp_openreq_init_rwin(struct request_sock *req,
351b1964b5fSEric Dumazet 			   const struct sock *sk_listener,
352b1964b5fSEric Dumazet 			   const struct dst_entry *dst)
353843f4a55SYuchung Cheng {
354843f4a55SYuchung Cheng 	struct inet_request_sock *ireq = inet_rsk(req);
355b1964b5fSEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk_listener);
356b1964b5fSEric Dumazet 	int full_space = tcp_full_space(sk_listener);
357b1964b5fSEric Dumazet 	u32 window_clamp;
358b1964b5fSEric Dumazet 	__u8 rcv_wscale;
35913d3b1ebSLawrence Brakmo 	u32 rcv_wnd;
3603541f9e8SEric Dumazet 	int mss;
361843f4a55SYuchung Cheng 
3623541f9e8SEric Dumazet 	mss = tcp_mss_clamp(tp, dst_metric_advmss(dst));
363b1964b5fSEric Dumazet 	window_clamp = READ_ONCE(tp->window_clamp);
364843f4a55SYuchung Cheng 	/* Set this up on the first call only */
365ed53d0abSEric Dumazet 	req->rsk_window_clamp = window_clamp ? : dst_metric(dst, RTAX_WINDOW);
366843f4a55SYuchung Cheng 
367843f4a55SYuchung Cheng 	/* limit the window selection if the user enforce a smaller rx buffer */
368b1964b5fSEric Dumazet 	if (sk_listener->sk_userlocks & SOCK_RCVBUF_LOCK &&
369ed53d0abSEric Dumazet 	    (req->rsk_window_clamp > full_space || req->rsk_window_clamp == 0))
370ed53d0abSEric Dumazet 		req->rsk_window_clamp = full_space;
371843f4a55SYuchung Cheng 
37213d3b1ebSLawrence Brakmo 	rcv_wnd = tcp_rwnd_init_bpf((struct sock *)req);
37313d3b1ebSLawrence Brakmo 	if (rcv_wnd == 0)
37413d3b1ebSLawrence Brakmo 		rcv_wnd = dst_metric(dst, RTAX_INITRWND);
37513d3b1ebSLawrence Brakmo 	else if (full_space < rcv_wnd * mss)
37613d3b1ebSLawrence Brakmo 		full_space = rcv_wnd * mss;
37713d3b1ebSLawrence Brakmo 
378843f4a55SYuchung Cheng 	/* tcp_full_space because it is guaranteed to be the first packet */
379ceef9ab6SEric Dumazet 	tcp_select_initial_window(sk_listener, full_space,
380843f4a55SYuchung Cheng 		mss - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),
381ed53d0abSEric Dumazet 		&req->rsk_rcv_wnd,
382ed53d0abSEric Dumazet 		&req->rsk_window_clamp,
383843f4a55SYuchung Cheng 		ireq->wscale_ok,
384843f4a55SYuchung Cheng 		&rcv_wscale,
38513d3b1ebSLawrence Brakmo 		rcv_wnd);
386843f4a55SYuchung Cheng 	ireq->rcv_wscale = rcv_wscale;
387843f4a55SYuchung Cheng }
388843f4a55SYuchung Cheng EXPORT_SYMBOL(tcp_openreq_init_rwin);
389843f4a55SYuchung Cheng 
390735d3831SFlorian Westphal static void tcp_ecn_openreq_child(struct tcp_sock *tp,
391735d3831SFlorian Westphal 				  const struct request_sock *req)
392bdf1ee5dSIlpo Järvinen {
393bdf1ee5dSIlpo Järvinen 	tp->ecn_flags = inet_rsk(req)->ecn_ok ? TCP_ECN_OK : 0;
394bdf1ee5dSIlpo Järvinen }
395bdf1ee5dSIlpo Järvinen 
39681164413SDaniel Borkmann void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst)
39781164413SDaniel Borkmann {
39881164413SDaniel Borkmann 	struct inet_connection_sock *icsk = inet_csk(sk);
39981164413SDaniel Borkmann 	u32 ca_key = dst_metric(dst, RTAX_CC_ALGO);
40081164413SDaniel Borkmann 	bool ca_got_dst = false;
40181164413SDaniel Borkmann 
40281164413SDaniel Borkmann 	if (ca_key != TCP_CA_UNSPEC) {
40381164413SDaniel Borkmann 		const struct tcp_congestion_ops *ca;
40481164413SDaniel Borkmann 
40581164413SDaniel Borkmann 		rcu_read_lock();
40681164413SDaniel Borkmann 		ca = tcp_ca_find_key(ca_key);
40781164413SDaniel Borkmann 		if (likely(ca && try_module_get(ca->owner))) {
40881164413SDaniel Borkmann 			icsk->icsk_ca_dst_locked = tcp_ca_dst_locked(dst);
40981164413SDaniel Borkmann 			icsk->icsk_ca_ops = ca;
41081164413SDaniel Borkmann 			ca_got_dst = true;
41181164413SDaniel Borkmann 		}
41281164413SDaniel Borkmann 		rcu_read_unlock();
41381164413SDaniel Borkmann 	}
41481164413SDaniel Borkmann 
4159f950415SNeal Cardwell 	/* If no valid choice made yet, assign current system default ca. */
4169f950415SNeal Cardwell 	if (!ca_got_dst &&
4179f950415SNeal Cardwell 	    (!icsk->icsk_ca_setsockopt ||
4189f950415SNeal Cardwell 	     !try_module_get(icsk->icsk_ca_ops->owner)))
41981164413SDaniel Borkmann 		tcp_assign_congestion_control(sk);
42081164413SDaniel Borkmann 
42181164413SDaniel Borkmann 	tcp_set_ca_state(sk, TCP_CA_Open);
42281164413SDaniel Borkmann }
42381164413SDaniel Borkmann EXPORT_SYMBOL_GPL(tcp_ca_openreq_child);
42481164413SDaniel Borkmann 
42560e2a778SUrsula Braun static void smc_check_reset_syn_req(struct tcp_sock *oldtp,
42660e2a778SUrsula Braun 				    struct request_sock *req,
42760e2a778SUrsula Braun 				    struct tcp_sock *newtp)
42860e2a778SUrsula Braun {
42960e2a778SUrsula Braun #if IS_ENABLED(CONFIG_SMC)
43060e2a778SUrsula Braun 	struct inet_request_sock *ireq;
43160e2a778SUrsula Braun 
43260e2a778SUrsula Braun 	if (static_branch_unlikely(&tcp_have_smc)) {
43360e2a778SUrsula Braun 		ireq = inet_rsk(req);
43460e2a778SUrsula Braun 		if (oldtp->syn_smc && !ireq->smc_ok)
43560e2a778SUrsula Braun 			newtp->syn_smc = 0;
43660e2a778SUrsula Braun 	}
43760e2a778SUrsula Braun #endif
43860e2a778SUrsula Braun }
43960e2a778SUrsula Braun 
4401da177e4SLinus Torvalds /* This is not only more efficient than what we used to do, it eliminates
4411da177e4SLinus Torvalds  * a lot of code duplication between IPv4/IPv6 SYN recv processing. -DaveM
4421da177e4SLinus Torvalds  *
4431da177e4SLinus Torvalds  * Actually, we could lots of memory writes here. tp of listening
4441da177e4SLinus Torvalds  * socket contains all necessary default parameters.
4451da177e4SLinus Torvalds  */
446c28c6f04SEric Dumazet struct sock *tcp_create_openreq_child(const struct sock *sk,
447c28c6f04SEric Dumazet 				      struct request_sock *req,
448c28c6f04SEric Dumazet 				      struct sk_buff *skb)
4491da177e4SLinus Torvalds {
450e56c57d0SEric Dumazet 	struct sock *newsk = inet_csk_clone_lock(sk, req, GFP_ATOMIC);
4511da177e4SLinus Torvalds 
45200db4124SIan Morris 	if (newsk) {
4539f1d2604SArnaldo Carvalho de Melo 		const struct inet_request_sock *ireq = inet_rsk(req);
4542e6599cbSArnaldo Carvalho de Melo 		struct tcp_request_sock *treq = tcp_rsk(req);
455a9948a7eSArnaldo Carvalho de Melo 		struct inet_connection_sock *newicsk = inet_csk(newsk);
456435cf559SWilliam Allen Simpson 		struct tcp_sock *newtp = tcp_sk(newsk);
45760e2a778SUrsula Braun 		struct tcp_sock *oldtp = tcp_sk(sk);
45860e2a778SUrsula Braun 
45960e2a778SUrsula Braun 		smc_check_reset_syn_req(oldtp, req, newtp);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 		/* Now setup tcp_sock */
46231770e34SFlorian Westphal 		newtp->pred_flags = 0;
46331770e34SFlorian Westphal 
464435cf559SWilliam Allen Simpson 		newtp->rcv_wup = newtp->copied_seq =
465435cf559SWilliam Allen Simpson 		newtp->rcv_nxt = treq->rcv_isn + 1;
466a9d99ce2SEric Dumazet 		newtp->segs_in = 1;
467435cf559SWilliam Allen Simpson 
468435cf559SWilliam Allen Simpson 		newtp->snd_sml = newtp->snd_una =
4691a2c6181SChristoph Paasch 		newtp->snd_nxt = newtp->snd_up = treq->snt_isn + 1;
4701da177e4SLinus Torvalds 
47146d3ceabSEric Dumazet 		INIT_LIST_HEAD(&newtp->tsq_node);
472e2080072SEric Dumazet 		INIT_LIST_HEAD(&newtp->tsorted_sent_queue);
4731da177e4SLinus Torvalds 
474ee7537b6SHantzis Fotis 		tcp_init_wl(newtp, treq->rcv_isn);
4751da177e4SLinus Torvalds 
476740b0f18SEric Dumazet 		newtp->srtt_us = 0;
477740b0f18SEric Dumazet 		newtp->mdev_us = jiffies_to_usecs(TCP_TIMEOUT_INIT);
478ac9517fcSEric Dumazet 		minmax_reset(&newtp->rtt_min, tcp_jiffies32, ~0U);
479463c84b9SArnaldo Carvalho de Melo 		newicsk->icsk_rto = TCP_TIMEOUT_INIT;
48070eabf0eSEric Dumazet 		newicsk->icsk_ack.lrcvtime = tcp_jiffies32;
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 		newtp->packets_out = 0;
4831da177e4SLinus Torvalds 		newtp->retrans_out = 0;
4841da177e4SLinus Torvalds 		newtp->sacked_out = 0;
4850b6a05c1SIlpo Järvinen 		newtp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
4869b717a8dSNandita Dukkipati 		newtp->tlp_high_seq = 0;
4879a568de4SEric Dumazet 		newtp->lsndtime = tcp_jiffies32;
488d8ed6250SEric Dumazet 		newsk->sk_txhash = treq->txhash;
489f2b2c582SNeal Cardwell 		newtp->last_oow_ack_time = 0;
490375fe02cSYuchung Cheng 		newtp->total_retrans = req->num_retrans;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 		/* So many TCP implementations out there (incorrectly) count the
4931da177e4SLinus Torvalds 		 * initial SYN frame in their delayed-ACK and congestion control
4941da177e4SLinus Torvalds 		 * algorithms that we must have the following bandaid to talk
4951da177e4SLinus Torvalds 		 * efficiently to them.  -DaveM
4961da177e4SLinus Torvalds 		 */
4979ad7c049SJerry Chu 		newtp->snd_cwnd = TCP_INIT_CWND;
4981da177e4SLinus Torvalds 		newtp->snd_cwnd_cnt = 0;
4991da177e4SLinus Torvalds 
500d7722e85SSoheil Hassas Yeganeh 		/* There's a bubble in the pipe until at least the first ACK. */
501d7722e85SSoheil Hassas Yeganeh 		newtp->app_limited = ~0U;
502d7722e85SSoheil Hassas Yeganeh 
5031da177e4SLinus Torvalds 		tcp_init_xmit_timers(newsk);
5041a2c6181SChristoph Paasch 		newtp->write_seq = newtp->pushed_seq = treq->snt_isn + 1;
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 		newtp->rx_opt.saw_tstamp = 0;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 		newtp->rx_opt.dsack = 0;
5091da177e4SLinus Torvalds 		newtp->rx_opt.num_sacks = 0;
510cabeccbdSIlpo Järvinen 
5111da177e4SLinus Torvalds 		newtp->urg_data = 0;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 		if (sock_flag(newsk, SOCK_KEEPOPEN))
514463c84b9SArnaldo Carvalho de Melo 			inet_csk_reset_keepalive_timer(newsk,
5151da177e4SLinus Torvalds 						       keepalive_time_when(newtp));
5161da177e4SLinus Torvalds 
5172e6599cbSArnaldo Carvalho de Melo 		newtp->rx_opt.tstamp_ok = ireq->tstamp_ok;
518713bafeaSYuchung Cheng 		newtp->rx_opt.sack_ok = ireq->sack_ok;
519ed53d0abSEric Dumazet 		newtp->window_clamp = req->rsk_window_clamp;
520ed53d0abSEric Dumazet 		newtp->rcv_ssthresh = req->rsk_rcv_wnd;
521ed53d0abSEric Dumazet 		newtp->rcv_wnd = req->rsk_rcv_wnd;
5222e6599cbSArnaldo Carvalho de Melo 		newtp->rx_opt.wscale_ok = ireq->wscale_ok;
5231da177e4SLinus Torvalds 		if (newtp->rx_opt.wscale_ok) {
5242e6599cbSArnaldo Carvalho de Melo 			newtp->rx_opt.snd_wscale = ireq->snd_wscale;
5252e6599cbSArnaldo Carvalho de Melo 			newtp->rx_opt.rcv_wscale = ireq->rcv_wscale;
5261da177e4SLinus Torvalds 		} else {
5271da177e4SLinus Torvalds 			newtp->rx_opt.snd_wscale = newtp->rx_opt.rcv_wscale = 0;
5281da177e4SLinus Torvalds 			newtp->window_clamp = min(newtp->window_clamp, 65535U);
5291da177e4SLinus Torvalds 		}
530aa8223c7SArnaldo Carvalho de Melo 		newtp->snd_wnd = (ntohs(tcp_hdr(skb)->window) <<
531aa8223c7SArnaldo Carvalho de Melo 				  newtp->rx_opt.snd_wscale);
5321da177e4SLinus Torvalds 		newtp->max_window = newtp->snd_wnd;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 		if (newtp->rx_opt.tstamp_ok) {
5351da177e4SLinus Torvalds 			newtp->rx_opt.ts_recent = req->ts_recent;
5369d729f72SJames Morris 			newtp->rx_opt.ts_recent_stamp = get_seconds();
5371da177e4SLinus Torvalds 			newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
5381da177e4SLinus Torvalds 		} else {
5391da177e4SLinus Torvalds 			newtp->rx_opt.ts_recent_stamp = 0;
5401da177e4SLinus Torvalds 			newtp->tcp_header_len = sizeof(struct tcphdr);
5411da177e4SLinus Torvalds 		}
54295a22caeSFlorian Westphal 		newtp->tsoffset = treq->ts_off;
543cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
544cfb6eeb4SYOSHIFUJI Hideaki 		newtp->md5sig_info = NULL;	/*XXX*/
545cfb6eeb4SYOSHIFUJI Hideaki 		if (newtp->af_specific->md5_lookup(sk, newsk))
546cfb6eeb4SYOSHIFUJI Hideaki 			newtp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED;
547cfb6eeb4SYOSHIFUJI Hideaki #endif
548bee7ca9eSWilliam Allen Simpson 		if (skb->len >= TCP_MSS_DEFAULT + newtp->tcp_header_len)
549463c84b9SArnaldo Carvalho de Melo 			newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len;
5501da177e4SLinus Torvalds 		newtp->rx_opt.mss_clamp = req->mss;
551735d3831SFlorian Westphal 		tcp_ecn_openreq_child(newtp, req);
5528b485ce6SEric Dumazet 		newtp->fastopen_req = NULL;
5538336886fSJerry Chu 		newtp->fastopen_rsk = NULL;
5546f73601eSYuchung Cheng 		newtp->syn_data_acked = 0;
5559a568de4SEric Dumazet 		newtp->rack.mstamp = 0;
556659a8ad5SYuchung Cheng 		newtp->rack.advanced = 0;
5571f255691SPriyaranjan Jha 		newtp->rack.reo_wnd_steps = 1;
5581f255691SPriyaranjan Jha 		newtp->rack.last_delivered = 0;
5591f255691SPriyaranjan Jha 		newtp->rack.reo_wnd_persist = 0;
5601f255691SPriyaranjan Jha 		newtp->rack.dsack_seen = 0;
5611da177e4SLinus Torvalds 
56290bbcc60SEric Dumazet 		__TCP_INC_STATS(sock_net(sk), TCP_MIB_PASSIVEOPENS);
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds 	return newsk;
5651da177e4SLinus Torvalds }
5664bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_create_openreq_child);
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds /*
5698336886fSJerry Chu  * Process an incoming packet for SYN_RECV sockets represented as a
5708336886fSJerry Chu  * request_sock. Normally sk is the listener socket but for TFO it
5718336886fSJerry Chu  * points to the child socket.
5728336886fSJerry Chu  *
5738336886fSJerry Chu  * XXX (TFO) - The current impl contains a special check for ack
5748336886fSJerry Chu  * validation and inside tcp_v4_reqsk_send_ack(). Can we do better?
5754308fc58SAlan Cox  *
5764308fc58SAlan Cox  * We don't need to initialize tmp_opt.sack_ok as we don't use the results
5771da177e4SLinus Torvalds  */
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
58060236fddSArnaldo Carvalho de Melo 			   struct request_sock *req,
5818336886fSJerry Chu 			   bool fastopen)
5821da177e4SLinus Torvalds {
5834957faadSWilliam Allen Simpson 	struct tcp_options_received tmp_opt;
5844957faadSWilliam Allen Simpson 	struct sock *child;
585aa8223c7SArnaldo Carvalho de Melo 	const struct tcphdr *th = tcp_hdr(skb);
586714e85beSAl Viro 	__be32 flg = tcp_flag_word(th) & (TCP_FLAG_RST|TCP_FLAG_SYN|TCP_FLAG_ACK);
587a2a385d6SEric Dumazet 	bool paws_reject = false;
5885e0724d0SEric Dumazet 	bool own_req;
5891da177e4SLinus Torvalds 
590bb5b7c11SDavid S. Miller 	tmp_opt.saw_tstamp = 0;
591bb5b7c11SDavid S. Miller 	if (th->doff > (sizeof(struct tcphdr)>>2)) {
592eed29f17SEric Dumazet 		tcp_parse_options(sock_net(sk), skb, &tmp_opt, 0, NULL);
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 		if (tmp_opt.saw_tstamp) {
5951da177e4SLinus Torvalds 			tmp_opt.ts_recent = req->ts_recent;
59695a22caeSFlorian Westphal 			if (tmp_opt.rcv_tsecr)
59795a22caeSFlorian Westphal 				tmp_opt.rcv_tsecr -= tcp_rsk(req)->ts_off;
5981da177e4SLinus Torvalds 			/* We do not store true stamp, but it is not required,
5991da177e4SLinus Torvalds 			 * it can be estimated (approximately)
6001da177e4SLinus Torvalds 			 * from another data.
6011da177e4SLinus Torvalds 			 */
602e6c022a4SEric Dumazet 			tmp_opt.ts_recent_stamp = get_seconds() - ((TCP_TIMEOUT_INIT/HZ)<<req->num_timeout);
603c887e6d2SIlpo Järvinen 			paws_reject = tcp_paws_reject(&tmp_opt, th->rst);
6041da177e4SLinus Torvalds 		}
6051da177e4SLinus Torvalds 	}
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	/* Check for pure retransmitted SYN. */
6082e6599cbSArnaldo Carvalho de Melo 	if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn &&
6091da177e4SLinus Torvalds 	    flg == TCP_FLAG_SYN &&
6101da177e4SLinus Torvalds 	    !paws_reject) {
6111da177e4SLinus Torvalds 		/*
6121da177e4SLinus Torvalds 		 * RFC793 draws (Incorrectly! It was fixed in RFC1122)
6131da177e4SLinus Torvalds 		 * this case on figure 6 and figure 8, but formal
6141da177e4SLinus Torvalds 		 * protocol description says NOTHING.
6151da177e4SLinus Torvalds 		 * To be more exact, it says that we should send ACK,
6161da177e4SLinus Torvalds 		 * because this segment (at least, if it has no data)
6171da177e4SLinus Torvalds 		 * is out of window.
6181da177e4SLinus Torvalds 		 *
6191da177e4SLinus Torvalds 		 *  CONCLUSION: RFC793 (even with RFC1122) DOES NOT
6201da177e4SLinus Torvalds 		 *  describe SYN-RECV state. All the description
6211da177e4SLinus Torvalds 		 *  is wrong, we cannot believe to it and should
6221da177e4SLinus Torvalds 		 *  rely only on common sense and implementation
6231da177e4SLinus Torvalds 		 *  experience.
6241da177e4SLinus Torvalds 		 *
6251da177e4SLinus Torvalds 		 * Enforce "SYN-ACK" according to figure 8, figure 6
6261da177e4SLinus Torvalds 		 * of RFC793, fixed by RFC1122.
6278336886fSJerry Chu 		 *
6288336886fSJerry Chu 		 * Note that even if there is new data in the SYN packet
6298336886fSJerry Chu 		 * they will be thrown away too.
630cd75eff6SYuchung Cheng 		 *
631cd75eff6SYuchung Cheng 		 * Reset timer after retransmitting SYNACK, similar to
632cd75eff6SYuchung Cheng 		 * the idea of fast retransmit in recovery.
6331da177e4SLinus Torvalds 		 */
634a9b2c06dSNeal Cardwell 		if (!tcp_oow_rate_limited(sock_net(sk), skb,
635a9b2c06dSNeal Cardwell 					  LINUX_MIB_TCPACKSKIPPEDSYNRECV,
636a9b2c06dSNeal Cardwell 					  &tcp_rsk(req)->last_oow_ack_time) &&
637a9b2c06dSNeal Cardwell 
638dd929c1bSEric Dumazet 		    !inet_rtx_syn_ack(sk, req)) {
639dd929c1bSEric Dumazet 			unsigned long expires = jiffies;
640dd929c1bSEric Dumazet 
641dd929c1bSEric Dumazet 			expires += min(TCP_TIMEOUT_INIT << req->num_timeout,
642dd929c1bSEric Dumazet 				       TCP_RTO_MAX);
643dd929c1bSEric Dumazet 			if (!fastopen)
644dd929c1bSEric Dumazet 				mod_timer_pending(&req->rsk_timer, expires);
645dd929c1bSEric Dumazet 			else
646dd929c1bSEric Dumazet 				req->rsk_timer.expires = expires;
647dd929c1bSEric Dumazet 		}
6481da177e4SLinus Torvalds 		return NULL;
6491da177e4SLinus Torvalds 	}
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	/* Further reproduces section "SEGMENT ARRIVES"
6521da177e4SLinus Torvalds 	   for state SYN-RECEIVED of RFC793.
6531da177e4SLinus Torvalds 	   It is broken, however, it does not work only
6541da177e4SLinus Torvalds 	   when SYNs are crossed.
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	   You would think that SYN crossing is impossible here, since
6571da177e4SLinus Torvalds 	   we should have a SYN_SENT socket (from connect()) on our end,
6581da177e4SLinus Torvalds 	   but this is not true if the crossed SYNs were sent to both
6591da177e4SLinus Torvalds 	   ends by a malicious third party.  We must defend against this,
6601da177e4SLinus Torvalds 	   and to do that we first verify the ACK (as per RFC793, page
6611da177e4SLinus Torvalds 	   36) and reset if it is invalid.  Is this a true full defense?
6621da177e4SLinus Torvalds 	   To convince ourselves, let us consider a way in which the ACK
6631da177e4SLinus Torvalds 	   test can still pass in this 'malicious crossed SYNs' case.
6641da177e4SLinus Torvalds 	   Malicious sender sends identical SYNs (and thus identical sequence
6651da177e4SLinus Torvalds 	   numbers) to both A and B:
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 		A: gets SYN, seq=7
6681da177e4SLinus Torvalds 		B: gets SYN, seq=7
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	   By our good fortune, both A and B select the same initial
6711da177e4SLinus Torvalds 	   send sequence number of seven :-)
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 		A: sends SYN|ACK, seq=7, ack_seq=8
6741da177e4SLinus Torvalds 		B: sends SYN|ACK, seq=7, ack_seq=8
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	   So we are now A eating this SYN|ACK, ACK test passes.  So
6771da177e4SLinus Torvalds 	   does sequence test, SYN is truncated, and thus we consider
6781da177e4SLinus Torvalds 	   it a bare ACK.
6791da177e4SLinus Torvalds 
680ec0a1966SDavid S. Miller 	   If icsk->icsk_accept_queue.rskq_defer_accept, we silently drop this
681ec0a1966SDavid S. Miller 	   bare ACK.  Otherwise, we create an established connection.  Both
682ec0a1966SDavid S. Miller 	   ends (listening sockets) accept the new incoming connection and try
683ec0a1966SDavid S. Miller 	   to talk to each other. 8-)
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	   Note: This case is both harmless, and rare.  Possibility is about the
6861da177e4SLinus Torvalds 	   same as us discovering intelligent life on another plant tomorrow.
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	   But generally, we should (RFC lies!) to accept ACK
6891da177e4SLinus Torvalds 	   from SYNACK both here and in tcp_rcv_state_process().
6901da177e4SLinus Torvalds 	   tcp_rcv_state_process() does not, hence, we do not too.
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	   Note that the case is absolutely generic:
6931da177e4SLinus Torvalds 	   we cannot optimize anything here without
6941da177e4SLinus Torvalds 	   violating protocol. All the checks must be made
6951da177e4SLinus Torvalds 	   before attempt to create socket.
6961da177e4SLinus Torvalds 	 */
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	/* RFC793 page 36: "If the connection is in any non-synchronized state ...
6991da177e4SLinus Torvalds 	 *                  and the incoming segment acknowledges something not yet
700caa20d9aSStephen Hemminger 	 *                  sent (the segment carries an unacceptable ACK) ...
7011da177e4SLinus Torvalds 	 *                  a reset is sent."
7021da177e4SLinus Torvalds 	 *
7038336886fSJerry Chu 	 * Invalid ACK: reset will be sent by listening socket.
7048336886fSJerry Chu 	 * Note that the ACK validity check for a Fast Open socket is done
7058336886fSJerry Chu 	 * elsewhere and is checked directly against the child socket rather
7068336886fSJerry Chu 	 * than req because user data may have been sent out.
7071da177e4SLinus Torvalds 	 */
7088336886fSJerry Chu 	if ((flg & TCP_FLAG_ACK) && !fastopen &&
709435cf559SWilliam Allen Simpson 	    (TCP_SKB_CB(skb)->ack_seq !=
7101a2c6181SChristoph Paasch 	     tcp_rsk(req)->snt_isn + 1))
7111da177e4SLinus Torvalds 		return sk;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	/* Also, it would be not so bad idea to check rcv_tsecr, which
7141da177e4SLinus Torvalds 	 * is essentially ACK extension and too early or too late values
7151da177e4SLinus Torvalds 	 * should cause reset in unsynchronized states.
7161da177e4SLinus Torvalds 	 */
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	/* RFC793: "first check sequence number". */
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	if (paws_reject || !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
721ed53d0abSEric Dumazet 					  tcp_rsk(req)->rcv_nxt, tcp_rsk(req)->rcv_nxt + req->rsk_rcv_wnd)) {
7221da177e4SLinus Torvalds 		/* Out of window: send ACK and drop. */
7234ce7e93cSEric Dumazet 		if (!(flg & TCP_FLAG_RST) &&
7244ce7e93cSEric Dumazet 		    !tcp_oow_rate_limited(sock_net(sk), skb,
7254ce7e93cSEric Dumazet 					  LINUX_MIB_TCPACKSKIPPEDSYNRECV,
7264ce7e93cSEric Dumazet 					  &tcp_rsk(req)->last_oow_ack_time))
7276edafaafSGui Jianfeng 			req->rsk_ops->send_ack(sk, skb, req);
7281da177e4SLinus Torvalds 		if (paws_reject)
72902a1d6e7SEric Dumazet 			__NET_INC_STATS(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);
7301da177e4SLinus Torvalds 		return NULL;
7311da177e4SLinus Torvalds 	}
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	/* In sequence, PAWS is OK. */
7341da177e4SLinus Torvalds 
7358336886fSJerry Chu 	if (tmp_opt.saw_tstamp && !after(TCP_SKB_CB(skb)->seq, tcp_rsk(req)->rcv_nxt))
7361da177e4SLinus Torvalds 		req->ts_recent = tmp_opt.rcv_tsval;
7371da177e4SLinus Torvalds 
7382e6599cbSArnaldo Carvalho de Melo 	if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn) {
7391da177e4SLinus Torvalds 		/* Truncate SYN, it is out of window starting
7402e6599cbSArnaldo Carvalho de Melo 		   at tcp_rsk(req)->rcv_isn + 1. */
7411da177e4SLinus Torvalds 		flg &= ~TCP_FLAG_SYN;
7421da177e4SLinus Torvalds 	}
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	/* RFC793: "second check the RST bit" and
7451da177e4SLinus Torvalds 	 *	   "fourth, check the SYN bit"
7461da177e4SLinus Torvalds 	 */
7473687b1dcSWei Yongjun 	if (flg & (TCP_FLAG_RST|TCP_FLAG_SYN)) {
74890bbcc60SEric Dumazet 		__TCP_INC_STATS(sock_net(sk), TCP_MIB_ATTEMPTFAILS);
7491da177e4SLinus Torvalds 		goto embryonic_reset;
7503687b1dcSWei Yongjun 	}
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	/* ACK sequence verified above, just make sure ACK is
7531da177e4SLinus Torvalds 	 * set.  If ACK not set, just silently drop the packet.
7548336886fSJerry Chu 	 *
7558336886fSJerry Chu 	 * XXX (TFO) - if we ever allow "data after SYN", the
7568336886fSJerry Chu 	 * following check needs to be removed.
7571da177e4SLinus Torvalds 	 */
7581da177e4SLinus Torvalds 	if (!(flg & TCP_FLAG_ACK))
7591da177e4SLinus Torvalds 		return NULL;
7601da177e4SLinus Torvalds 
7618336886fSJerry Chu 	/* For Fast Open no more processing is needed (sk is the
7628336886fSJerry Chu 	 * child socket).
7638336886fSJerry Chu 	 */
7648336886fSJerry Chu 	if (fastopen)
7658336886fSJerry Chu 		return sk;
7668336886fSJerry Chu 
767d1b99ba4SJulian Anastasov 	/* While TCP_DEFER_ACCEPT is active, drop bare ACK. */
768e6c022a4SEric Dumazet 	if (req->num_timeout < inet_csk(sk)->icsk_accept_queue.rskq_defer_accept &&
769ec0a1966SDavid S. Miller 	    TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) {
770ec0a1966SDavid S. Miller 		inet_rsk(req)->acked = 1;
77102a1d6e7SEric Dumazet 		__NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDEFERACCEPTDROP);
772ec0a1966SDavid S. Miller 		return NULL;
773ec0a1966SDavid S. Miller 	}
774ec0a1966SDavid S. Miller 
7751da177e4SLinus Torvalds 	/* OK, ACK is valid, create big socket and
7761da177e4SLinus Torvalds 	 * feed this segment to it. It will repeat all
7771da177e4SLinus Torvalds 	 * the tests. THIS SEGMENT MUST MOVE SOCKET TO
7781da177e4SLinus Torvalds 	 * ESTABLISHED STATE. If it will be dropped after
7791da177e4SLinus Torvalds 	 * socket is created, wait for troubles.
7801da177e4SLinus Torvalds 	 */
7815e0724d0SEric Dumazet 	child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL,
7825e0724d0SEric Dumazet 							 req, &own_req);
78351456b29SIan Morris 	if (!child)
7841da177e4SLinus Torvalds 		goto listen_overflow;
7851da177e4SLinus Torvalds 
7866bcfd7f8SEric Dumazet 	sock_rps_save_rxhash(child, skb);
7870f1c28aeSYuchung Cheng 	tcp_synack_rtt_meas(child, req);
7885e0724d0SEric Dumazet 	return inet_csk_complete_hashdance(sk, child, req, own_req);
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds listen_overflow:
79165c9410cSEric Dumazet 	if (!sock_net(sk)->ipv4.sysctl_tcp_abort_on_overflow) {
7922e6599cbSArnaldo Carvalho de Melo 		inet_rsk(req)->acked = 1;
7931da177e4SLinus Torvalds 		return NULL;
7941da177e4SLinus Torvalds 	}
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds embryonic_reset:
7978336886fSJerry Chu 	if (!(flg & TCP_FLAG_RST)) {
7988336886fSJerry Chu 		/* Received a bad SYN pkt - for TFO We try not to reset
7998336886fSJerry Chu 		 * the local connection unless it's really necessary to
8008336886fSJerry Chu 		 * avoid becoming vulnerable to outside attack aiming at
8018336886fSJerry Chu 		 * resetting legit local connections.
8028336886fSJerry Chu 		 */
803cfb6eeb4SYOSHIFUJI Hideaki 		req->rsk_ops->send_reset(sk, skb);
8048336886fSJerry Chu 	} else if (fastopen) { /* received a valid RST pkt */
8058336886fSJerry Chu 		reqsk_fastopen_remove(sk, req, true);
8068336886fSJerry Chu 		tcp_reset(sk);
8078336886fSJerry Chu 	}
8088336886fSJerry Chu 	if (!fastopen) {
80952452c54SEric Dumazet 		inet_csk_reqsk_queue_drop(sk, req);
81002a1d6e7SEric Dumazet 		__NET_INC_STATS(sock_net(sk), LINUX_MIB_EMBRYONICRSTS);
8118336886fSJerry Chu 	}
8121da177e4SLinus Torvalds 	return NULL;
8131da177e4SLinus Torvalds }
8144bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_check_req);
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds /*
8171da177e4SLinus Torvalds  * Queue segment on the new socket if the new socket is active,
8181da177e4SLinus Torvalds  * otherwise we just shortcircuit this and continue with
8191da177e4SLinus Torvalds  * the new socket.
8208336886fSJerry Chu  *
8218336886fSJerry Chu  * For the vast majority of cases child->sk_state will be TCP_SYN_RECV
8228336886fSJerry Chu  * when entering. But other states are possible due to a race condition
8238336886fSJerry Chu  * where after __inet_lookup_established() fails but before the listener
8248336886fSJerry Chu  * locked is obtained, other packets cause the same connection to
8258336886fSJerry Chu  * be created.
8261da177e4SLinus Torvalds  */
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds int tcp_child_process(struct sock *parent, struct sock *child,
8291da177e4SLinus Torvalds 		      struct sk_buff *skb)
8301da177e4SLinus Torvalds {
8311da177e4SLinus Torvalds 	int ret = 0;
8321da177e4SLinus Torvalds 	int state = child->sk_state;
8331da177e4SLinus Torvalds 
834e5907459SAlexander Duyck 	/* record NAPI ID of child */
835e5907459SAlexander Duyck 	sk_mark_napi_id(child, skb);
836e5907459SAlexander Duyck 
837a44d6eacSMartin KaFai Lau 	tcp_segs_in(tcp_sk(child), skb);
8381da177e4SLinus Torvalds 	if (!sock_owned_by_user(child)) {
83972ab4a86SEric Dumazet 		ret = tcp_rcv_state_process(child, skb);
8401da177e4SLinus Torvalds 		/* Wakeup parent, send SIGIO */
8411da177e4SLinus Torvalds 		if (state == TCP_SYN_RECV && child->sk_state != state)
842676d2369SDavid S. Miller 			parent->sk_data_ready(parent);
8431da177e4SLinus Torvalds 	} else {
8441da177e4SLinus Torvalds 		/* Alas, it is possible again, because we do lookup
8451da177e4SLinus Torvalds 		 * in main socket hash table and lock on listening
8461da177e4SLinus Torvalds 		 * socket does not protect us more.
8471da177e4SLinus Torvalds 		 */
848a3a858ffSZhu Yi 		__sk_add_backlog(child, skb);
8491da177e4SLinus Torvalds 	}
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	bh_unlock_sock(child);
8521da177e4SLinus Torvalds 	sock_put(child);
8531da177e4SLinus Torvalds 	return ret;
8541da177e4SLinus Torvalds }
8551da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_child_process);
856