xref: /openbmc/linux/net/ipv4/syncookies.c (revision 646697b9e3fef913bb6393ebfb6115c442a96be7)
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>
161da177e4SLinus Torvalds #include <linux/cryptohash.h>
171da177e4SLinus Torvalds #include <linux/kernel.h>
18bc3b2d7fSPaul Gortmaker #include <linux/export.h>
191da177e4SLinus Torvalds #include <net/tcp.h>
2086b08d86SKOVACS Krisztian #include <net/route.h>
211da177e4SLinus Torvalds 
22734f614bSFlorian Westphal /* Timestamps: lowest bits store TCP options */
23172d69e6SFlorian Westphal #define TSBITS 6
244dfc2817SFlorian Westphal #define TSMASK (((__u32)1 << TSBITS) - 1)
254dfc2817SFlorian Westphal 
261da177e4SLinus Torvalds extern int sysctl_tcp_syncookies;
271da177e4SLinus Torvalds 
28253ff516SFlorian Westphal static u32 syncookie_secret[2][16-4+SHA_DIGEST_WORDS] __read_mostly;
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #define COOKIEBITS 24	/* Upper bits store count */
311da177e4SLinus Torvalds #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
321da177e4SLinus Torvalds 
33245b2e70STejun Heo static DEFINE_PER_CPU(__u32 [16 + 5 + SHA_WORKSPACE_WORDS],
34245b2e70STejun Heo 		      ipv4_cookie_scratch);
3511baab7aSEric Dumazet 
36714e85beSAl Viro static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport,
371da177e4SLinus Torvalds 		       u32 count, int c)
381da177e4SLinus Torvalds {
39b23a002fSHannes Frederic Sowa 	__u32 *tmp;
401da177e4SLinus Torvalds 
41b23a002fSHannes Frederic Sowa 	net_get_random_once(syncookie_secret, sizeof(syncookie_secret));
42b23a002fSHannes Frederic Sowa 
43903ceff7SChristoph Lameter 	tmp  = this_cpu_ptr(ipv4_cookie_scratch);
442051f11fSFlorian Westphal 	memcpy(tmp + 4, syncookie_secret[c], sizeof(syncookie_secret[c]));
45714e85beSAl Viro 	tmp[0] = (__force u32)saddr;
46714e85beSAl Viro 	tmp[1] = (__force u32)daddr;
47714e85beSAl Viro 	tmp[2] = ((__force u32)sport << 16) + (__force u32)dport;
481da177e4SLinus Torvalds 	tmp[3] = count;
491da177e4SLinus Torvalds 	sha_transform(tmp + 16, (__u8 *)tmp, tmp + 16 + 5);
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	return tmp[17];
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds 
544dfc2817SFlorian Westphal 
554dfc2817SFlorian Westphal /*
564dfc2817SFlorian Westphal  * when syncookies are in effect and tcp timestamps are enabled we encode
57734f614bSFlorian Westphal  * tcp options in the lower bits of the timestamp value that will be
584dfc2817SFlorian Westphal  * sent in the syn-ack.
594dfc2817SFlorian Westphal  * Since subsequent timestamps use the normal tcp_time_stamp value, we
604dfc2817SFlorian Westphal  * must make sure that the resulting initial timestamp is <= tcp_time_stamp.
614dfc2817SFlorian Westphal  */
624dfc2817SFlorian Westphal __u32 cookie_init_timestamp(struct request_sock *req)
634dfc2817SFlorian Westphal {
644dfc2817SFlorian Westphal 	struct inet_request_sock *ireq;
654dfc2817SFlorian Westphal 	u32 ts, ts_now = tcp_time_stamp;
664dfc2817SFlorian Westphal 	u32 options = 0;
674dfc2817SFlorian Westphal 
684dfc2817SFlorian Westphal 	ireq = inet_rsk(req);
69734f614bSFlorian Westphal 
70734f614bSFlorian Westphal 	options = ireq->wscale_ok ? ireq->snd_wscale : 0xf;
71734f614bSFlorian Westphal 	options |= ireq->sack_ok << 4;
72172d69e6SFlorian Westphal 	options |= ireq->ecn_ok << 5;
734dfc2817SFlorian Westphal 
744dfc2817SFlorian Westphal 	ts = ts_now & ~TSMASK;
754dfc2817SFlorian Westphal 	ts |= options;
764dfc2817SFlorian Westphal 	if (ts > ts_now) {
774dfc2817SFlorian Westphal 		ts >>= TSBITS;
784dfc2817SFlorian Westphal 		ts--;
794dfc2817SFlorian Westphal 		ts <<= TSBITS;
804dfc2817SFlorian Westphal 		ts |= options;
814dfc2817SFlorian Westphal 	}
824dfc2817SFlorian Westphal 	return ts;
834dfc2817SFlorian Westphal }
844dfc2817SFlorian Westphal 
854dfc2817SFlorian Westphal 
86714e85beSAl Viro static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
878c27bd75SFlorian Westphal 				   __be16 dport, __u32 sseq, __u32 data)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	/*
901da177e4SLinus Torvalds 	 * Compute the secure sequence number.
911da177e4SLinus Torvalds 	 * The output should be:
921da177e4SLinus Torvalds 	 *   HASH(sec1,saddr,sport,daddr,dport,sec1) + sseq + (count * 2^24)
931da177e4SLinus Torvalds 	 *      + (HASH(sec2,saddr,sport,daddr,dport,count,sec2) % 2^24).
941da177e4SLinus Torvalds 	 * Where sseq is their sequence number and count increases every
951da177e4SLinus Torvalds 	 * minute by 1.
961da177e4SLinus Torvalds 	 * As an extra hack, we add a small "data" value that encodes the
971da177e4SLinus Torvalds 	 * MSS into the second hash value.
981da177e4SLinus Torvalds 	 */
998c27bd75SFlorian Westphal 	u32 count = tcp_cookie_time();
1001da177e4SLinus Torvalds 	return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
1011da177e4SLinus Torvalds 		sseq + (count << COOKIEBITS) +
1021da177e4SLinus Torvalds 		((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
1031da177e4SLinus Torvalds 		 & COOKIEMASK));
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /*
1071da177e4SLinus Torvalds  * This retrieves the small "data" value from the syncookie.
1081da177e4SLinus Torvalds  * If the syncookie is bad, the data returned will be out of
1091da177e4SLinus Torvalds  * range.  This must be checked by the caller.
1101da177e4SLinus Torvalds  *
1118c27bd75SFlorian Westphal  * The count value used to generate the cookie must be less than
1128c27bd75SFlorian Westphal  * MAX_SYNCOOKIE_AGE minutes in the past.
1138c27bd75SFlorian Westphal  * The return value (__u32)-1 if this test fails.
1141da177e4SLinus Torvalds  */
115714e85beSAl Viro static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr,
1168c27bd75SFlorian Westphal 				  __be16 sport, __be16 dport, __u32 sseq)
1171da177e4SLinus Torvalds {
1188c27bd75SFlorian Westphal 	u32 diff, count = tcp_cookie_time();
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	/* Strip away the layers from the cookie */
1211da177e4SLinus Torvalds 	cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	/* Cookie is now reduced to (count * 2^24) ^ (hash % 2^24) */
1241da177e4SLinus Torvalds 	diff = (count - (cookie >> COOKIEBITS)) & ((__u32) -1 >> COOKIEBITS);
1258c27bd75SFlorian Westphal 	if (diff >= MAX_SYNCOOKIE_AGE)
1261da177e4SLinus Torvalds 		return (__u32)-1;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	return (cookie -
1291da177e4SLinus Torvalds 		cookie_hash(saddr, daddr, sport, dport, count - diff, 1))
1301da177e4SLinus Torvalds 		& COOKIEMASK;	/* Leaving the data behind */
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds /*
13408629354SFlorian Westphal  * MSS Values are chosen based on the 2011 paper
13508629354SFlorian Westphal  * 'An Analysis of TCP Maximum Segement Sizes' by S. Alcock and R. Nelson.
13608629354SFlorian Westphal  * Values ..
13708629354SFlorian Westphal  *  .. lower than 536 are rare (< 0.2%)
13808629354SFlorian Westphal  *  .. between 537 and 1299 account for less than < 1.5% of observed values
13908629354SFlorian Westphal  *  .. in the 1300-1349 range account for about 15 to 20% of observed mss values
14008629354SFlorian Westphal  *  .. exceeding 1460 are very rare (< 0.04%)
1415918e2fbSFlorian Westphal  *
14208629354SFlorian Westphal  *  1460 is the single most frequently announced mss value (30 to 46% depending
14308629354SFlorian Westphal  *  on monitor location).  Table must be sorted.
1441da177e4SLinus Torvalds  */
1451da177e4SLinus Torvalds static __u16 const msstab[] = {
1465918e2fbSFlorian Westphal 	536,
14708629354SFlorian Westphal 	1300,
14808629354SFlorian Westphal 	1440,	/* 1440, 1452: PPPoE */
1495918e2fbSFlorian Westphal 	1460,
1501da177e4SLinus Torvalds };
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds /*
1531da177e4SLinus Torvalds  * Generate a syncookie.  mssp points to the mss, which is returned
1541da177e4SLinus Torvalds  * rounded down to the value encoded in the cookie.
1551da177e4SLinus Torvalds  */
1560198230bSPatrick McHardy u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
1570198230bSPatrick McHardy 			      u16 *mssp)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	int mssind;
1601da177e4SLinus Torvalds 	const __u16 mss = *mssp;
1611da177e4SLinus Torvalds 
1625918e2fbSFlorian Westphal 	for (mssind = ARRAY_SIZE(msstab) - 1; mssind ; mssind--)
1635918e2fbSFlorian Westphal 		if (mss >= msstab[mssind])
1645918e2fbSFlorian Westphal 			break;
1655918e2fbSFlorian Westphal 	*mssp = msstab[mssind];
1661da177e4SLinus Torvalds 
167aa8223c7SArnaldo Carvalho de Melo 	return secure_tcp_syn_cookie(iph->saddr, iph->daddr,
168aa8223c7SArnaldo Carvalho de Melo 				     th->source, th->dest, ntohl(th->seq),
1698c27bd75SFlorian Westphal 				     mssind);
1701da177e4SLinus Torvalds }
1710198230bSPatrick McHardy EXPORT_SYMBOL_GPL(__cookie_v4_init_sequence);
1720198230bSPatrick McHardy 
17357b47553SOctavian Purdila __u32 cookie_v4_init_sequence(struct sock *sk, const struct sk_buff *skb,
17457b47553SOctavian Purdila 			      __u16 *mssp)
1750198230bSPatrick McHardy {
1760198230bSPatrick McHardy 	const struct iphdr *iph = ip_hdr(skb);
1770198230bSPatrick McHardy 	const struct tcphdr *th = tcp_hdr(skb);
1780198230bSPatrick McHardy 
1790198230bSPatrick McHardy 	tcp_synq_overflow(sk);
1800198230bSPatrick McHardy 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT);
1810198230bSPatrick McHardy 
1820198230bSPatrick McHardy 	return __cookie_v4_init_sequence(iph, th, mssp);
1830198230bSPatrick McHardy }
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds /*
1861da177e4SLinus Torvalds  * Check if a ack sequence number is a valid syncookie.
1871da177e4SLinus Torvalds  * Return the decoded mss if it is, or 0 if not.
1881da177e4SLinus Torvalds  */
1890198230bSPatrick McHardy int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
1900198230bSPatrick McHardy 		      u32 cookie)
1911da177e4SLinus Torvalds {
192aa8223c7SArnaldo Carvalho de Melo 	__u32 seq = ntohl(th->seq) - 1;
193aa8223c7SArnaldo Carvalho de Melo 	__u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
1948c27bd75SFlorian Westphal 					    th->source, th->dest, seq);
1951da177e4SLinus Torvalds 
1965918e2fbSFlorian Westphal 	return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
1971da177e4SLinus Torvalds }
1980198230bSPatrick McHardy EXPORT_SYMBOL_GPL(__cookie_v4_check);
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
20160236fddSArnaldo Carvalho de Melo 					   struct request_sock *req,
2021da177e4SLinus Torvalds 					   struct dst_entry *dst)
2031da177e4SLinus Torvalds {
2048292a17aSArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
2051da177e4SLinus Torvalds 	struct sock *child;
2061da177e4SLinus Torvalds 
2078292a17aSArnaldo Carvalho de Melo 	child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst);
2081da177e4SLinus Torvalds 	if (child)
209463c84b9SArnaldo Carvalho de Melo 		inet_csk_reqsk_queue_add(sk, req, child);
2101da177e4SLinus Torvalds 	else
21160236fddSArnaldo Carvalho de Melo 		reqsk_free(req);
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	return child;
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2164dfc2817SFlorian Westphal 
2174dfc2817SFlorian Westphal /*
2184dfc2817SFlorian Westphal  * when syncookies are in effect and tcp timestamps are enabled we stored
2194dfc2817SFlorian Westphal  * additional tcp options in the timestamp.
2204dfc2817SFlorian Westphal  * This extracts these options from the timestamp echo.
2214dfc2817SFlorian Westphal  *
222734f614bSFlorian Westphal  * The lowest 4 bits store snd_wscale.
223172d69e6SFlorian Westphal  * next 2 bits indicate SACK and ECN support.
2248c763681SFlorian Westphal  *
2258c763681SFlorian Westphal  * return false if we decode an option that should not be.
2264dfc2817SFlorian Westphal  */
2275d134f1cSHannes Frederic Sowa bool cookie_check_timestamp(struct tcp_options_received *tcp_opt,
2285d134f1cSHannes Frederic Sowa 			struct net *net, bool *ecn_ok)
2294dfc2817SFlorian Westphal {
230734f614bSFlorian Westphal 	/* echoed timestamp, lowest bits contain options */
2314dfc2817SFlorian Westphal 	u32 options = tcp_opt->rcv_tsecr & TSMASK;
2324dfc2817SFlorian Westphal 
2338c763681SFlorian Westphal 	if (!tcp_opt->saw_tstamp)  {
2348c763681SFlorian Westphal 		tcp_clear_options(tcp_opt);
2358c763681SFlorian Westphal 		return true;
2368c763681SFlorian Westphal 	}
2378c763681SFlorian Westphal 
2388c763681SFlorian Westphal 	if (!sysctl_tcp_timestamps)
2398c763681SFlorian Westphal 		return false;
2408c763681SFlorian Westphal 
241ab56222aSVijay Subramanian 	tcp_opt->sack_ok = (options & (1 << 4)) ? TCP_SACK_SEEN : 0;
242172d69e6SFlorian Westphal 	*ecn_ok = (options >> 5) & 1;
2435d134f1cSHannes Frederic Sowa 	if (*ecn_ok && !net->ipv4.sysctl_tcp_ecn)
244172d69e6SFlorian Westphal 		return false;
2454dfc2817SFlorian Westphal 
2468c763681SFlorian Westphal 	if (tcp_opt->sack_ok && !sysctl_tcp_sack)
2478c763681SFlorian Westphal 		return false;
2484dfc2817SFlorian Westphal 
249734f614bSFlorian Westphal 	if ((options & 0xf) == 0xf)
250734f614bSFlorian Westphal 		return true; /* no window scaling */
251734f614bSFlorian Westphal 
2524dfc2817SFlorian Westphal 	tcp_opt->wscale_ok = 1;
253734f614bSFlorian Westphal 	tcp_opt->snd_wscale = options & 0xf;
2548c763681SFlorian Westphal 	return sysctl_tcp_window_scaling != 0;
2558c763681SFlorian Westphal }
2564dfc2817SFlorian Westphal EXPORT_SYMBOL(cookie_check_timestamp);
2574dfc2817SFlorian Westphal 
258461b74c3SCong Wang struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
2591da177e4SLinus Torvalds {
260461b74c3SCong Wang 	struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
2614957faadSWilliam Allen Simpson 	struct tcp_options_received tcp_opt;
2622e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq;
2632e6599cbSArnaldo Carvalho de Melo 	struct tcp_request_sock *treq;
2641da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
265aa8223c7SArnaldo Carvalho de Melo 	const struct tcphdr *th = tcp_hdr(skb);
266aa8223c7SArnaldo Carvalho de Melo 	__u32 cookie = ntohl(th->ack_seq) - 1;
2671da177e4SLinus Torvalds 	struct sock *ret = sk;
26860236fddSArnaldo Carvalho de Melo 	struct request_sock *req;
2691da177e4SLinus Torvalds 	int mss;
2701da177e4SLinus Torvalds 	struct rtable *rt;
2711da177e4SLinus Torvalds 	__u8 rcv_wscale;
272f0e3d068SMike Waychison 	bool ecn_ok = false;
273dfd25fffSEric Dumazet 	struct flowi4 fl4;
2741da177e4SLinus Torvalds 
275af9b4738SFlorian Westphal 	if (!sysctl_tcp_syncookies || !th->ack || th->rst)
2761da177e4SLinus Torvalds 		goto out;
2771da177e4SLinus Torvalds 
278*646697b9SFlorian Westphal 	if (tcp_synq_no_recent_overflow(sk))
279*646697b9SFlorian Westphal 		goto out;
280*646697b9SFlorian Westphal 
281*646697b9SFlorian Westphal 	mss = __cookie_v4_check(ip_hdr(skb), th, cookie);
282*646697b9SFlorian Westphal 	if (mss == 0) {
283de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED);
2841da177e4SLinus Torvalds 		goto out;
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 
287de0744afSPavel Emelyanov 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESRECV);
2881da177e4SLinus Torvalds 
289bb5b7c11SDavid S. Miller 	/* check for timestamp cookie support */
290bb5b7c11SDavid S. Miller 	memset(&tcp_opt, 0, sizeof(tcp_opt));
2911a2c6181SChristoph Paasch 	tcp_parse_options(skb, &tcp_opt, 0, NULL);
292bb5b7c11SDavid S. Miller 
2935d134f1cSHannes Frederic Sowa 	if (!cookie_check_timestamp(&tcp_opt, sock_net(sk), &ecn_ok))
2948c763681SFlorian Westphal 		goto out;
295bb5b7c11SDavid S. Miller 
2961da177e4SLinus Torvalds 	ret = NULL;
297ce4a7d0dSArnaldo Carvalho de Melo 	req = inet_reqsk_alloc(&tcp_request_sock_ops); /* for safety */
2981da177e4SLinus Torvalds 	if (!req)
2991da177e4SLinus Torvalds 		goto out;
3001da177e4SLinus Torvalds 
3012e6599cbSArnaldo Carvalho de Melo 	ireq = inet_rsk(req);
3022e6599cbSArnaldo Carvalho de Melo 	treq = tcp_rsk(req);
303aa8223c7SArnaldo Carvalho de Melo 	treq->rcv_isn		= ntohl(th->seq) - 1;
3042e6599cbSArnaldo Carvalho de Melo 	treq->snt_isn		= cookie;
3051da177e4SLinus Torvalds 	req->mss		= mss;
306b44084c2SEric Dumazet 	ireq->ir_num		= ntohs(th->dest);
307634fb979SEric Dumazet 	ireq->ir_rmt_port	= th->source;
308634fb979SEric Dumazet 	ireq->ir_loc_addr	= ip_hdr(skb)->daddr;
309634fb979SEric Dumazet 	ireq->ir_rmt_addr	= ip_hdr(skb)->saddr;
31084f39b08SLorenzo Colitti 	ireq->ir_mark		= inet_request_mark(sk, skb);
311172d69e6SFlorian Westphal 	ireq->ecn_ok		= ecn_ok;
312bb5b7c11SDavid S. Miller 	ireq->snd_wscale	= tcp_opt.snd_wscale;
313bb5b7c11SDavid S. Miller 	ireq->sack_ok		= tcp_opt.sack_ok;
314bb5b7c11SDavid S. Miller 	ireq->wscale_ok		= tcp_opt.wscale_ok;
315bb5b7c11SDavid S. Miller 	ireq->tstamp_ok		= tcp_opt.saw_tstamp;
316bb5b7c11SDavid S. Miller 	req->ts_recent		= tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval : 0;
3179ad7c049SJerry Chu 	treq->snt_synack	= tcp_opt.saw_tstamp ? tcp_opt.rcv_tsecr : 0;
3188336886fSJerry Chu 	treq->listener		= NULL;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	/* We throwed the options of the initial SYN away, so we hope
3211da177e4SLinus Torvalds 	 * the ACK carries the same options again (see RFC1122 4.2.3.8)
3221da177e4SLinus Torvalds 	 */
323e25f866fSCong Wang 	ireq->opt = tcp_v4_save_options(skb);
3241da177e4SLinus Torvalds 
325284904aaSPaul Moore 	if (security_inet_conn_request(sk, skb, req)) {
326284904aaSPaul Moore 		reqsk_free(req);
327284904aaSPaul Moore 		goto out;
328284904aaSPaul Moore 	}
329284904aaSPaul Moore 
3301da177e4SLinus Torvalds 	req->expires	= 0UL;
331e6c022a4SEric Dumazet 	req->num_retrans = 0;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	/*
3341da177e4SLinus Torvalds 	 * We need to lookup the route here to get at the correct
3351da177e4SLinus Torvalds 	 * window size. We should better make sure that the window size
3361da177e4SLinus Torvalds 	 * hasn't changed since we received the original syn, but I see
3371da177e4SLinus Torvalds 	 * no easy way to do this.
3381da177e4SLinus Torvalds 	 */
33984f39b08SLorenzo Colitti 	flowi4_init_output(&fl4, sk->sk_bound_dev_if, ireq->ir_mark,
340d66954a0SDmitry Popov 			   RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, IPPROTO_TCP,
3411bba6ffeSDavid S. Miller 			   inet_sk_flowi_flags(sk),
342461b74c3SCong Wang 			   opt->srr ? opt->faddr : ireq->ir_rmt_addr,
343634fb979SEric Dumazet 			   ireq->ir_loc_addr, th->source, th->dest);
3449d6ec938SDavid S. Miller 	security_req_classify_flow(req, flowi4_to_flowi(&fl4));
3459d6ec938SDavid S. Miller 	rt = ip_route_output_key(sock_net(sk), &fl4);
346b23dd4feSDavid S. Miller 	if (IS_ERR(rt)) {
34760236fddSArnaldo Carvalho de Melo 		reqsk_free(req);
3481da177e4SLinus Torvalds 		goto out;
3491da177e4SLinus Torvalds 	}
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	/* Try to redo what tcp_v4_send_synack did. */
352d8d1f30bSChangli Gao 	req->window_clamp = tp->window_clamp ? :dst_metric(&rt->dst, RTAX_WINDOW);
3534dfc2817SFlorian Westphal 
3541da177e4SLinus Torvalds 	tcp_select_initial_window(tcp_full_space(sk), req->mss,
3551da177e4SLinus Torvalds 				  &req->rcv_wnd, &req->window_clamp,
35631d12926Slaurent chavey 				  ireq->wscale_ok, &rcv_wscale,
357d8d1f30bSChangli Gao 				  dst_metric(&rt->dst, RTAX_INITRWND));
3584dfc2817SFlorian Westphal 
3592e6599cbSArnaldo Carvalho de Melo 	ireq->rcv_wscale  = rcv_wscale;
3601da177e4SLinus Torvalds 
361d8d1f30bSChangli Gao 	ret = get_cookie_sock(sk, skb, req, &rt->dst);
362dfd25fffSEric Dumazet 	/* ip_queue_xmit() depends on our flow being setup
363dfd25fffSEric Dumazet 	 * Normal sockets get it right from inet_csk_route_child_sock()
364dfd25fffSEric Dumazet 	 */
365dfd25fffSEric Dumazet 	if (ret)
366dfd25fffSEric Dumazet 		inet_sk(ret)->cork.fl.u.ip4 = fl4;
3671da177e4SLinus Torvalds out:	return ret;
3681da177e4SLinus Torvalds }
369