xref: /linux/net/ipv4/syncookies.c (revision ceef9ab6be7234f9e49f79769e0da88d1dccfcc7)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Syncookies implementation for the Linux kernel
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1997 Andi Kleen
51da177e4SLinus Torvalds  *  Based on ideas by D.J.Bernstein and Eric Schenk.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
81da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
91da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
101da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/tcp.h>
141da177e4SLinus Torvalds #include <linux/slab.h>
151da177e4SLinus Torvalds #include <linux/random.h>
16fe62d05bSJason A. Donenfeld #include <linux/siphash.h>
171da177e4SLinus Torvalds #include <linux/kernel.h>
18bc3b2d7fSPaul Gortmaker #include <linux/export.h>
1984b114b9SEric Dumazet #include <net/secure_seq.h>
201da177e4SLinus Torvalds #include <net/tcp.h>
2186b08d86SKOVACS Krisztian #include <net/route.h>
221da177e4SLinus Torvalds 
23fe62d05bSJason A. Donenfeld static siphash_key_t syncookie_secret[2] __read_mostly;
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #define COOKIEBITS 24	/* Upper bits store count */
261da177e4SLinus Torvalds #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
271da177e4SLinus Torvalds 
28274e2da0SFlorian Westphal /* TCP Timestamp: 6 lowest bits of timestamp sent in the cookie SYN-ACK
29274e2da0SFlorian Westphal  * stores TCP options:
30274e2da0SFlorian Westphal  *
31274e2da0SFlorian Westphal  * MSB                               LSB
32274e2da0SFlorian Westphal  * | 31 ...   6 |  5  |  4   | 3 2 1 0 |
33274e2da0SFlorian Westphal  * |  Timestamp | ECN | SACK | WScale  |
34274e2da0SFlorian Westphal  *
35274e2da0SFlorian Westphal  * When we receive a valid cookie-ACK, we look at the echoed tsval (if
36274e2da0SFlorian Westphal  * any) to figure out which TCP options we should use for the rebuilt
37274e2da0SFlorian Westphal  * connection.
38274e2da0SFlorian Westphal  *
39274e2da0SFlorian Westphal  * A WScale setting of '0xf' (which is an invalid scaling value)
40274e2da0SFlorian Westphal  * means that original syn did not include the TCP window scaling option.
41274e2da0SFlorian Westphal  */
42274e2da0SFlorian Westphal #define TS_OPT_WSCALE_MASK	0xf
43274e2da0SFlorian Westphal #define TS_OPT_SACK		BIT(4)
44274e2da0SFlorian Westphal #define TS_OPT_ECN		BIT(5)
45274e2da0SFlorian Westphal /* There is no TS_OPT_TIMESTAMP:
46274e2da0SFlorian Westphal  * if ACK contains timestamp option, we already know it was
47274e2da0SFlorian Westphal  * requested/supported by the syn/synack exchange.
48274e2da0SFlorian Westphal  */
49274e2da0SFlorian Westphal #define TSBITS	6
50274e2da0SFlorian Westphal #define TSMASK	(((__u32)1 << TSBITS) - 1)
51274e2da0SFlorian Westphal 
52714e85beSAl Viro static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport,
531da177e4SLinus Torvalds 		       u32 count, int c)
541da177e4SLinus Torvalds {
55b23a002fSHannes Frederic Sowa 	net_get_random_once(syncookie_secret, sizeof(syncookie_secret));
56fe62d05bSJason A. Donenfeld 	return siphash_4u32((__force u32)saddr, (__force u32)daddr,
57fe62d05bSJason A. Donenfeld 			    (__force u32)sport << 16 | (__force u32)dport,
58fe62d05bSJason A. Donenfeld 			    count, &syncookie_secret[c]);
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
614dfc2817SFlorian Westphal 
624dfc2817SFlorian Westphal /*
634dfc2817SFlorian Westphal  * when syncookies are in effect and tcp timestamps are enabled we encode
64734f614bSFlorian Westphal  * tcp options in the lower bits of the timestamp value that will be
654dfc2817SFlorian Westphal  * sent in the syn-ack.
664dfc2817SFlorian Westphal  * Since subsequent timestamps use the normal tcp_time_stamp value, we
674dfc2817SFlorian Westphal  * must make sure that the resulting initial timestamp is <= tcp_time_stamp.
684dfc2817SFlorian Westphal  */
699a568de4SEric Dumazet u64 cookie_init_timestamp(struct request_sock *req)
704dfc2817SFlorian Westphal {
714dfc2817SFlorian Westphal 	struct inet_request_sock *ireq;
729a568de4SEric Dumazet 	u32 ts, ts_now = tcp_time_stamp_raw();
734dfc2817SFlorian Westphal 	u32 options = 0;
744dfc2817SFlorian Westphal 
754dfc2817SFlorian Westphal 	ireq = inet_rsk(req);
76734f614bSFlorian Westphal 
77274e2da0SFlorian Westphal 	options = ireq->wscale_ok ? ireq->snd_wscale : TS_OPT_WSCALE_MASK;
78274e2da0SFlorian Westphal 	if (ireq->sack_ok)
79274e2da0SFlorian Westphal 		options |= TS_OPT_SACK;
80274e2da0SFlorian Westphal 	if (ireq->ecn_ok)
81274e2da0SFlorian Westphal 		options |= TS_OPT_ECN;
824dfc2817SFlorian Westphal 
834dfc2817SFlorian Westphal 	ts = ts_now & ~TSMASK;
844dfc2817SFlorian Westphal 	ts |= options;
854dfc2817SFlorian Westphal 	if (ts > ts_now) {
864dfc2817SFlorian Westphal 		ts >>= TSBITS;
874dfc2817SFlorian Westphal 		ts--;
884dfc2817SFlorian Westphal 		ts <<= TSBITS;
894dfc2817SFlorian Westphal 		ts |= options;
904dfc2817SFlorian Westphal 	}
919a568de4SEric Dumazet 	return (u64)ts * (USEC_PER_SEC / TCP_TS_HZ);
924dfc2817SFlorian Westphal }
934dfc2817SFlorian Westphal 
944dfc2817SFlorian Westphal 
95714e85beSAl Viro static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
968c27bd75SFlorian Westphal 				   __be16 dport, __u32 sseq, __u32 data)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds 	/*
991da177e4SLinus Torvalds 	 * Compute the secure sequence number.
1001da177e4SLinus Torvalds 	 * The output should be:
1011da177e4SLinus Torvalds 	 *   HASH(sec1,saddr,sport,daddr,dport,sec1) + sseq + (count * 2^24)
1021da177e4SLinus Torvalds 	 *      + (HASH(sec2,saddr,sport,daddr,dport,count,sec2) % 2^24).
1031da177e4SLinus Torvalds 	 * Where sseq is their sequence number and count increases every
1041da177e4SLinus Torvalds 	 * minute by 1.
1051da177e4SLinus Torvalds 	 * As an extra hack, we add a small "data" value that encodes the
1061da177e4SLinus Torvalds 	 * MSS into the second hash value.
1071da177e4SLinus Torvalds 	 */
1088c27bd75SFlorian Westphal 	u32 count = tcp_cookie_time();
1091da177e4SLinus Torvalds 	return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
1101da177e4SLinus Torvalds 		sseq + (count << COOKIEBITS) +
1111da177e4SLinus Torvalds 		((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
1121da177e4SLinus Torvalds 		 & COOKIEMASK));
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /*
1161da177e4SLinus Torvalds  * This retrieves the small "data" value from the syncookie.
1171da177e4SLinus Torvalds  * If the syncookie is bad, the data returned will be out of
1181da177e4SLinus Torvalds  * range.  This must be checked by the caller.
1191da177e4SLinus Torvalds  *
1208c27bd75SFlorian Westphal  * The count value used to generate the cookie must be less than
1218c27bd75SFlorian Westphal  * MAX_SYNCOOKIE_AGE minutes in the past.
1228c27bd75SFlorian Westphal  * The return value (__u32)-1 if this test fails.
1231da177e4SLinus Torvalds  */
124714e85beSAl Viro static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr,
1258c27bd75SFlorian Westphal 				  __be16 sport, __be16 dport, __u32 sseq)
1261da177e4SLinus Torvalds {
1278c27bd75SFlorian Westphal 	u32 diff, count = tcp_cookie_time();
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	/* Strip away the layers from the cookie */
1301da177e4SLinus Torvalds 	cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	/* Cookie is now reduced to (count * 2^24) ^ (hash % 2^24) */
1331da177e4SLinus Torvalds 	diff = (count - (cookie >> COOKIEBITS)) & ((__u32) -1 >> COOKIEBITS);
1348c27bd75SFlorian Westphal 	if (diff >= MAX_SYNCOOKIE_AGE)
1351da177e4SLinus Torvalds 		return (__u32)-1;
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	return (cookie -
1381da177e4SLinus Torvalds 		cookie_hash(saddr, daddr, sport, dport, count - diff, 1))
1391da177e4SLinus Torvalds 		& COOKIEMASK;	/* Leaving the data behind */
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds /*
14308629354SFlorian Westphal  * MSS Values are chosen based on the 2011 paper
14408629354SFlorian Westphal  * 'An Analysis of TCP Maximum Segement Sizes' by S. Alcock and R. Nelson.
14508629354SFlorian Westphal  * Values ..
14608629354SFlorian Westphal  *  .. lower than 536 are rare (< 0.2%)
14708629354SFlorian Westphal  *  .. between 537 and 1299 account for less than < 1.5% of observed values
14808629354SFlorian Westphal  *  .. in the 1300-1349 range account for about 15 to 20% of observed mss values
14908629354SFlorian Westphal  *  .. exceeding 1460 are very rare (< 0.04%)
1505918e2fbSFlorian Westphal  *
15108629354SFlorian Westphal  *  1460 is the single most frequently announced mss value (30 to 46% depending
15208629354SFlorian Westphal  *  on monitor location).  Table must be sorted.
1531da177e4SLinus Torvalds  */
1541da177e4SLinus Torvalds static __u16 const msstab[] = {
1555918e2fbSFlorian Westphal 	536,
15608629354SFlorian Westphal 	1300,
15708629354SFlorian Westphal 	1440,	/* 1440, 1452: PPPoE */
1585918e2fbSFlorian Westphal 	1460,
1591da177e4SLinus Torvalds };
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds /*
1621da177e4SLinus Torvalds  * Generate a syncookie.  mssp points to the mss, which is returned
1631da177e4SLinus Torvalds  * rounded down to the value encoded in the cookie.
1641da177e4SLinus Torvalds  */
1650198230bSPatrick McHardy u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
1660198230bSPatrick McHardy 			      u16 *mssp)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	int mssind;
1691da177e4SLinus Torvalds 	const __u16 mss = *mssp;
1701da177e4SLinus Torvalds 
1715918e2fbSFlorian Westphal 	for (mssind = ARRAY_SIZE(msstab) - 1; mssind ; mssind--)
1725918e2fbSFlorian Westphal 		if (mss >= msstab[mssind])
1735918e2fbSFlorian Westphal 			break;
1745918e2fbSFlorian Westphal 	*mssp = msstab[mssind];
1751da177e4SLinus Torvalds 
176aa8223c7SArnaldo Carvalho de Melo 	return secure_tcp_syn_cookie(iph->saddr, iph->daddr,
177aa8223c7SArnaldo Carvalho de Melo 				     th->source, th->dest, ntohl(th->seq),
1788c27bd75SFlorian Westphal 				     mssind);
1791da177e4SLinus Torvalds }
1800198230bSPatrick McHardy EXPORT_SYMBOL_GPL(__cookie_v4_init_sequence);
1810198230bSPatrick McHardy 
1823f684b4bSEric Dumazet __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mssp)
1830198230bSPatrick McHardy {
1840198230bSPatrick McHardy 	const struct iphdr *iph = ip_hdr(skb);
1850198230bSPatrick McHardy 	const struct tcphdr *th = tcp_hdr(skb);
1860198230bSPatrick McHardy 
1870198230bSPatrick McHardy 	return __cookie_v4_init_sequence(iph, th, mssp);
1880198230bSPatrick McHardy }
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds /*
1911da177e4SLinus Torvalds  * Check if a ack sequence number is a valid syncookie.
1921da177e4SLinus Torvalds  * Return the decoded mss if it is, or 0 if not.
1931da177e4SLinus Torvalds  */
1940198230bSPatrick McHardy int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
1950198230bSPatrick McHardy 		      u32 cookie)
1961da177e4SLinus Torvalds {
197aa8223c7SArnaldo Carvalho de Melo 	__u32 seq = ntohl(th->seq) - 1;
198aa8223c7SArnaldo Carvalho de Melo 	__u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
1998c27bd75SFlorian Westphal 					    th->source, th->dest, seq);
2001da177e4SLinus Torvalds 
2015918e2fbSFlorian Westphal 	return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
2021da177e4SLinus Torvalds }
2030198230bSPatrick McHardy EXPORT_SYMBOL_GPL(__cookie_v4_check);
2041da177e4SLinus Torvalds 
205b80c0e78SEric Dumazet struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
20660236fddSArnaldo Carvalho de Melo 				 struct request_sock *req,
20784b114b9SEric Dumazet 				 struct dst_entry *dst, u32 tsoff)
2081da177e4SLinus Torvalds {
2098292a17aSArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
2101da177e4SLinus Torvalds 	struct sock *child;
2115e0724d0SEric Dumazet 	bool own_req;
2121da177e4SLinus Torvalds 
2135e0724d0SEric Dumazet 	child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
2145e0724d0SEric Dumazet 						 NULL, &own_req);
2150470c8caSEric Dumazet 	if (child) {
21641c6d650SReshetova, Elena 		refcount_set(&req->rsk_refcnt, 1);
21784b114b9SEric Dumazet 		tcp_sk(child)->tsoffset = tsoff;
2186bcfd7f8SEric Dumazet 		sock_rps_save_rxhash(child, skb);
219463c84b9SArnaldo Carvalho de Melo 		inet_csk_reqsk_queue_add(sk, req, child);
2200470c8caSEric Dumazet 	} else {
22160236fddSArnaldo Carvalho de Melo 		reqsk_free(req);
2220470c8caSEric Dumazet 	}
2231da177e4SLinus Torvalds 	return child;
2241da177e4SLinus Torvalds }
225b80c0e78SEric Dumazet EXPORT_SYMBOL(tcp_get_cookie_sock);
2264dfc2817SFlorian Westphal 
2274dfc2817SFlorian Westphal /*
2284dfc2817SFlorian Westphal  * when syncookies are in effect and tcp timestamps are enabled we stored
2294dfc2817SFlorian Westphal  * additional tcp options in the timestamp.
2304dfc2817SFlorian Westphal  * This extracts these options from the timestamp echo.
2314dfc2817SFlorian Westphal  *
232f1673381SFlorian Westphal  * return false if we decode a tcp option that is disabled
233f1673381SFlorian Westphal  * on the host.
2344dfc2817SFlorian Westphal  */
235f9301034SEric Dumazet bool cookie_timestamp_decode(const struct net *net,
236f9301034SEric Dumazet 			     struct tcp_options_received *tcp_opt)
2374dfc2817SFlorian Westphal {
238734f614bSFlorian Westphal 	/* echoed timestamp, lowest bits contain options */
239274e2da0SFlorian Westphal 	u32 options = tcp_opt->rcv_tsecr;
2404dfc2817SFlorian Westphal 
2418c763681SFlorian Westphal 	if (!tcp_opt->saw_tstamp)  {
2428c763681SFlorian Westphal 		tcp_clear_options(tcp_opt);
2438c763681SFlorian Westphal 		return true;
2448c763681SFlorian Westphal 	}
2458c763681SFlorian Westphal 
2465d2ed052SEric Dumazet 	if (!net->ipv4.sysctl_tcp_timestamps)
2478c763681SFlorian Westphal 		return false;
2488c763681SFlorian Westphal 
249274e2da0SFlorian Westphal 	tcp_opt->sack_ok = (options & TS_OPT_SACK) ? TCP_SACK_SEEN : 0;
2504dfc2817SFlorian Westphal 
251f9301034SEric Dumazet 	if (tcp_opt->sack_ok && !net->ipv4.sysctl_tcp_sack)
2528c763681SFlorian Westphal 		return false;
2534dfc2817SFlorian Westphal 
254274e2da0SFlorian Westphal 	if ((options & TS_OPT_WSCALE_MASK) == TS_OPT_WSCALE_MASK)
255734f614bSFlorian Westphal 		return true; /* no window scaling */
256734f614bSFlorian Westphal 
2574dfc2817SFlorian Westphal 	tcp_opt->wscale_ok = 1;
258274e2da0SFlorian Westphal 	tcp_opt->snd_wscale = options & TS_OPT_WSCALE_MASK;
259274e2da0SFlorian Westphal 
2609bb37ef0SEric Dumazet 	return net->ipv4.sysctl_tcp_window_scaling != 0;
2618c763681SFlorian Westphal }
262f1673381SFlorian Westphal EXPORT_SYMBOL(cookie_timestamp_decode);
263f1673381SFlorian Westphal 
264f1673381SFlorian Westphal bool cookie_ecn_ok(const struct tcp_options_received *tcp_opt,
265f7b3bec6SFlorian Westphal 		   const struct net *net, const struct dst_entry *dst)
266f1673381SFlorian Westphal {
267f1673381SFlorian Westphal 	bool ecn_ok = tcp_opt->rcv_tsecr & TS_OPT_ECN;
268f1673381SFlorian Westphal 
269f1673381SFlorian Westphal 	if (!ecn_ok)
270f1673381SFlorian Westphal 		return false;
271f1673381SFlorian Westphal 
272f1673381SFlorian Westphal 	if (net->ipv4.sysctl_tcp_ecn)
273f1673381SFlorian Westphal 		return true;
274f1673381SFlorian Westphal 
275f7b3bec6SFlorian Westphal 	return dst_feature(dst, RTAX_FEATURE_ECN);
276f1673381SFlorian Westphal }
277f1673381SFlorian Westphal EXPORT_SYMBOL(cookie_ecn_ok);
2784dfc2817SFlorian Westphal 
279079096f1SEric Dumazet /* On input, sk is a listener.
280079096f1SEric Dumazet  * Output is listener if incoming packet would not create a child
281079096f1SEric Dumazet  *           NULL if memory could not be allocated.
282079096f1SEric Dumazet  */
283461b74c3SCong Wang struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
2841da177e4SLinus Torvalds {
285461b74c3SCong Wang 	struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
2864957faadSWilliam Allen Simpson 	struct tcp_options_received tcp_opt;
2872e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq;
2882e6599cbSArnaldo Carvalho de Melo 	struct tcp_request_sock *treq;
2891da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
290aa8223c7SArnaldo Carvalho de Melo 	const struct tcphdr *th = tcp_hdr(skb);
291aa8223c7SArnaldo Carvalho de Melo 	__u32 cookie = ntohl(th->ack_seq) - 1;
2921da177e4SLinus Torvalds 	struct sock *ret = sk;
29360236fddSArnaldo Carvalho de Melo 	struct request_sock *req;
2941da177e4SLinus Torvalds 	int mss;
2951da177e4SLinus Torvalds 	struct rtable *rt;
2961da177e4SLinus Torvalds 	__u8 rcv_wscale;
297dfd25fffSEric Dumazet 	struct flowi4 fl4;
29884b114b9SEric Dumazet 	u32 tsoff = 0;
2991da177e4SLinus Torvalds 
30012ed8244SNikolay Borisov 	if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies || !th->ack || th->rst)
3011da177e4SLinus Torvalds 		goto out;
3021da177e4SLinus Torvalds 
303646697b9SFlorian Westphal 	if (tcp_synq_no_recent_overflow(sk))
304646697b9SFlorian Westphal 		goto out;
305646697b9SFlorian Westphal 
306646697b9SFlorian Westphal 	mss = __cookie_v4_check(ip_hdr(skb), th, cookie);
307646697b9SFlorian Westphal 	if (mss == 0) {
30802a1d6e7SEric Dumazet 		__NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED);
3091da177e4SLinus Torvalds 		goto out;
3101da177e4SLinus Torvalds 	}
3111da177e4SLinus Torvalds 
31202a1d6e7SEric Dumazet 	__NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESRECV);
3131da177e4SLinus Torvalds 
314bb5b7c11SDavid S. Miller 	/* check for timestamp cookie support */
315bb5b7c11SDavid S. Miller 	memset(&tcp_opt, 0, sizeof(tcp_opt));
316eed29f17SEric Dumazet 	tcp_parse_options(sock_net(sk), skb, &tcp_opt, 0, NULL);
317bb5b7c11SDavid S. Miller 
31884b114b9SEric Dumazet 	if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) {
3195d2ed052SEric Dumazet 		tsoff = secure_tcp_ts_off(sock_net(sk),
3205d2ed052SEric Dumazet 					  ip_hdr(skb)->daddr,
3215d2ed052SEric Dumazet 					  ip_hdr(skb)->saddr);
32284b114b9SEric Dumazet 		tcp_opt.rcv_tsecr -= tsoff;
32384b114b9SEric Dumazet 	}
32484b114b9SEric Dumazet 
325f9301034SEric Dumazet 	if (!cookie_timestamp_decode(sock_net(sk), &tcp_opt))
3268c763681SFlorian Westphal 		goto out;
327bb5b7c11SDavid S. Miller 
3281da177e4SLinus Torvalds 	ret = NULL;
329a1a5344dSEric Dumazet 	req = inet_reqsk_alloc(&tcp_request_sock_ops, sk, false); /* for safety */
3301da177e4SLinus Torvalds 	if (!req)
3311da177e4SLinus Torvalds 		goto out;
3321da177e4SLinus Torvalds 
3332e6599cbSArnaldo Carvalho de Melo 	ireq = inet_rsk(req);
3342e6599cbSArnaldo Carvalho de Melo 	treq = tcp_rsk(req);
335aa8223c7SArnaldo Carvalho de Melo 	treq->rcv_isn		= ntohl(th->seq) - 1;
3362e6599cbSArnaldo Carvalho de Melo 	treq->snt_isn		= cookie;
33795a22caeSFlorian Westphal 	treq->ts_off		= 0;
33818bcf290SAlexander Potapenko 	treq->txhash		= net_tx_rndhash();
3391da177e4SLinus Torvalds 	req->mss		= mss;
340b44084c2SEric Dumazet 	ireq->ir_num		= ntohs(th->dest);
341634fb979SEric Dumazet 	ireq->ir_rmt_port	= th->source;
34208d2cc3bSEric Dumazet 	sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
34308d2cc3bSEric Dumazet 	sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
34484f39b08SLorenzo Colitti 	ireq->ir_mark		= inet_request_mark(sk, skb);
345bb5b7c11SDavid S. Miller 	ireq->snd_wscale	= tcp_opt.snd_wscale;
346bb5b7c11SDavid S. Miller 	ireq->sack_ok		= tcp_opt.sack_ok;
347bb5b7c11SDavid S. Miller 	ireq->wscale_ok		= tcp_opt.wscale_ok;
348bb5b7c11SDavid S. Miller 	ireq->tstamp_ok		= tcp_opt.saw_tstamp;
349bb5b7c11SDavid S. Miller 	req->ts_recent		= tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval : 0;
3509a568de4SEric Dumazet 	treq->snt_synack	= 0;
3519439ce00SEric Dumazet 	treq->tfo_listener	= false;
3521da177e4SLinus Torvalds 
3536dd9a14eSDavid Ahern 	ireq->ir_iif = inet_request_bound_dev_if(sk, skb);
35416f86165SEric Dumazet 
3551da177e4SLinus Torvalds 	/* We throwed the options of the initial SYN away, so we hope
3561da177e4SLinus Torvalds 	 * the ACK carries the same options again (see RFC1122 4.2.3.8)
3571da177e4SLinus Torvalds 	 */
358c92e8c02SEric Dumazet 	RCU_INIT_POINTER(ireq->ireq_opt, tcp_v4_save_options(sock_net(sk), skb));
3591da177e4SLinus Torvalds 
360284904aaSPaul Moore 	if (security_inet_conn_request(sk, skb, req)) {
3610470c8caSEric Dumazet 		reqsk_free(req);
362284904aaSPaul Moore 		goto out;
363284904aaSPaul Moore 	}
364284904aaSPaul Moore 
365e6c022a4SEric Dumazet 	req->num_retrans = 0;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	/*
3681da177e4SLinus Torvalds 	 * We need to lookup the route here to get at the correct
3691da177e4SLinus Torvalds 	 * window size. We should better make sure that the window size
3701da177e4SLinus Torvalds 	 * hasn't changed since we received the original syn, but I see
3711da177e4SLinus Torvalds 	 * no easy way to do this.
3721da177e4SLinus Torvalds 	 */
3736dd9a14eSDavid Ahern 	flowi4_init_output(&fl4, ireq->ir_iif, ireq->ir_mark,
374d66954a0SDmitry Popov 			   RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, IPPROTO_TCP,
3751bba6ffeSDavid S. Miller 			   inet_sk_flowi_flags(sk),
376461b74c3SCong Wang 			   opt->srr ? opt->faddr : ireq->ir_rmt_addr,
377e2d118a1SLorenzo Colitti 			   ireq->ir_loc_addr, th->source, th->dest, sk->sk_uid);
3789d6ec938SDavid S. Miller 	security_req_classify_flow(req, flowi4_to_flowi(&fl4));
3799d6ec938SDavid S. Miller 	rt = ip_route_output_key(sock_net(sk), &fl4);
380b23dd4feSDavid S. Miller 	if (IS_ERR(rt)) {
3810470c8caSEric Dumazet 		reqsk_free(req);
3821da177e4SLinus Torvalds 		goto out;
3831da177e4SLinus Torvalds 	}
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	/* Try to redo what tcp_v4_send_synack did. */
386ed53d0abSEric Dumazet 	req->rsk_window_clamp = tp->window_clamp ? :dst_metric(&rt->dst, RTAX_WINDOW);
3874dfc2817SFlorian Westphal 
388*ceef9ab6SEric Dumazet 	tcp_select_initial_window(sk, tcp_full_space(sk), req->mss,
389ed53d0abSEric Dumazet 				  &req->rsk_rcv_wnd, &req->rsk_window_clamp,
39031d12926Slaurent chavey 				  ireq->wscale_ok, &rcv_wscale,
391d8d1f30bSChangli Gao 				  dst_metric(&rt->dst, RTAX_INITRWND));
3924dfc2817SFlorian Westphal 
3932e6599cbSArnaldo Carvalho de Melo 	ireq->rcv_wscale  = rcv_wscale;
394f7b3bec6SFlorian Westphal 	ireq->ecn_ok = cookie_ecn_ok(&tcp_opt, sock_net(sk), &rt->dst);
3951da177e4SLinus Torvalds 
39684b114b9SEric Dumazet 	ret = tcp_get_cookie_sock(sk, skb, req, &rt->dst, tsoff);
397dfd25fffSEric Dumazet 	/* ip_queue_xmit() depends on our flow being setup
398dfd25fffSEric Dumazet 	 * Normal sockets get it right from inet_csk_route_child_sock()
399dfd25fffSEric Dumazet 	 */
400dfd25fffSEric Dumazet 	if (ret)
401dfd25fffSEric Dumazet 		inet_sk(ret)->cork.fl.u.ip4 = fl4;
4021da177e4SLinus Torvalds out:	return ret;
4031da177e4SLinus Torvalds }
404