xref: /linux/net/ipv4/syncookies.c (revision 5918e2fb9035b35164002c3a268feaf70b7c04d5)
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>
181da177e4SLinus Torvalds #include <net/tcp.h>
1986b08d86SKOVACS Krisztian #include <net/route.h>
201da177e4SLinus Torvalds 
214dfc2817SFlorian Westphal /* Timestamps: lowest 9 bits store TCP options */
224dfc2817SFlorian Westphal #define TSBITS 9
234dfc2817SFlorian Westphal #define TSMASK (((__u32)1 << TSBITS) - 1)
244dfc2817SFlorian Westphal 
251da177e4SLinus Torvalds extern int sysctl_tcp_syncookies;
261da177e4SLinus Torvalds 
272051f11fSFlorian Westphal __u32 syncookie_secret[2][16-4+SHA_DIGEST_WORDS];
28c6aefafbSGlenn Griffin EXPORT_SYMBOL(syncookie_secret);
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds static __init int init_syncookies(void)
311da177e4SLinus Torvalds {
321da177e4SLinus Torvalds 	get_random_bytes(syncookie_secret, sizeof(syncookie_secret));
331da177e4SLinus Torvalds 	return 0;
341da177e4SLinus Torvalds }
35c6aefafbSGlenn Griffin __initcall(init_syncookies);
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #define COOKIEBITS 24	/* Upper bits store count */
381da177e4SLinus Torvalds #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
391da177e4SLinus Torvalds 
40245b2e70STejun Heo static DEFINE_PER_CPU(__u32 [16 + 5 + SHA_WORKSPACE_WORDS],
41245b2e70STejun Heo 		      ipv4_cookie_scratch);
4211baab7aSEric Dumazet 
43714e85beSAl Viro static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport,
441da177e4SLinus Torvalds 		       u32 count, int c)
451da177e4SLinus Torvalds {
46245b2e70STejun Heo 	__u32 *tmp = __get_cpu_var(ipv4_cookie_scratch);
471da177e4SLinus Torvalds 
482051f11fSFlorian Westphal 	memcpy(tmp + 4, syncookie_secret[c], sizeof(syncookie_secret[c]));
49714e85beSAl Viro 	tmp[0] = (__force u32)saddr;
50714e85beSAl Viro 	tmp[1] = (__force u32)daddr;
51714e85beSAl Viro 	tmp[2] = ((__force u32)sport << 16) + (__force u32)dport;
521da177e4SLinus Torvalds 	tmp[3] = count;
531da177e4SLinus Torvalds 	sha_transform(tmp + 16, (__u8 *)tmp, tmp + 16 + 5);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	return tmp[17];
561da177e4SLinus Torvalds }
571da177e4SLinus Torvalds 
584dfc2817SFlorian Westphal 
594dfc2817SFlorian Westphal /*
604dfc2817SFlorian Westphal  * when syncookies are in effect and tcp timestamps are enabled we encode
614dfc2817SFlorian Westphal  * tcp options in the lowest 9 bits of the timestamp value that will be
624dfc2817SFlorian Westphal  * sent in the syn-ack.
634dfc2817SFlorian Westphal  * Since subsequent timestamps use the normal tcp_time_stamp value, we
644dfc2817SFlorian Westphal  * must make sure that the resulting initial timestamp is <= tcp_time_stamp.
654dfc2817SFlorian Westphal  */
664dfc2817SFlorian Westphal __u32 cookie_init_timestamp(struct request_sock *req)
674dfc2817SFlorian Westphal {
684dfc2817SFlorian Westphal 	struct inet_request_sock *ireq;
694dfc2817SFlorian Westphal 	u32 ts, ts_now = tcp_time_stamp;
704dfc2817SFlorian Westphal 	u32 options = 0;
714dfc2817SFlorian Westphal 
724dfc2817SFlorian Westphal 	ireq = inet_rsk(req);
734dfc2817SFlorian Westphal 	if (ireq->wscale_ok) {
744dfc2817SFlorian Westphal 		options = ireq->snd_wscale;
754dfc2817SFlorian Westphal 		options |= ireq->rcv_wscale << 4;
764dfc2817SFlorian Westphal 	}
774dfc2817SFlorian Westphal 	options |= ireq->sack_ok << 8;
784dfc2817SFlorian Westphal 
794dfc2817SFlorian Westphal 	ts = ts_now & ~TSMASK;
804dfc2817SFlorian Westphal 	ts |= options;
814dfc2817SFlorian Westphal 	if (ts > ts_now) {
824dfc2817SFlorian Westphal 		ts >>= TSBITS;
834dfc2817SFlorian Westphal 		ts--;
844dfc2817SFlorian Westphal 		ts <<= TSBITS;
854dfc2817SFlorian Westphal 		ts |= options;
864dfc2817SFlorian Westphal 	}
874dfc2817SFlorian Westphal 	return ts;
884dfc2817SFlorian Westphal }
894dfc2817SFlorian Westphal 
904dfc2817SFlorian Westphal 
91714e85beSAl Viro static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
92714e85beSAl Viro 				   __be16 dport, __u32 sseq, __u32 count,
931da177e4SLinus Torvalds 				   __u32 data)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	/*
961da177e4SLinus Torvalds 	 * Compute the secure sequence number.
971da177e4SLinus Torvalds 	 * The output should be:
981da177e4SLinus Torvalds 	 *   HASH(sec1,saddr,sport,daddr,dport,sec1) + sseq + (count * 2^24)
991da177e4SLinus Torvalds 	 *      + (HASH(sec2,saddr,sport,daddr,dport,count,sec2) % 2^24).
1001da177e4SLinus Torvalds 	 * Where sseq is their sequence number and count increases every
1011da177e4SLinus Torvalds 	 * minute by 1.
1021da177e4SLinus Torvalds 	 * As an extra hack, we add a small "data" value that encodes the
1031da177e4SLinus Torvalds 	 * MSS into the second hash value.
1041da177e4SLinus Torvalds 	 */
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
1071da177e4SLinus Torvalds 		sseq + (count << COOKIEBITS) +
1081da177e4SLinus Torvalds 		((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
1091da177e4SLinus Torvalds 		 & COOKIEMASK));
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds /*
1131da177e4SLinus Torvalds  * This retrieves the small "data" value from the syncookie.
1141da177e4SLinus Torvalds  * If the syncookie is bad, the data returned will be out of
1151da177e4SLinus Torvalds  * range.  This must be checked by the caller.
1161da177e4SLinus Torvalds  *
1171da177e4SLinus Torvalds  * The count value used to generate the cookie must be within
1181da177e4SLinus Torvalds  * "maxdiff" if the current (passed-in) "count".  The return value
1191da177e4SLinus Torvalds  * is (__u32)-1 if this test fails.
1201da177e4SLinus Torvalds  */
121714e85beSAl Viro static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr,
122714e85beSAl Viro 				  __be16 sport, __be16 dport, __u32 sseq,
1231da177e4SLinus Torvalds 				  __u32 count, __u32 maxdiff)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	__u32 diff;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	/* Strip away the layers from the cookie */
1281da177e4SLinus Torvalds 	cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	/* Cookie is now reduced to (count * 2^24) ^ (hash % 2^24) */
1311da177e4SLinus Torvalds 	diff = (count - (cookie >> COOKIEBITS)) & ((__u32) - 1 >> COOKIEBITS);
1321da177e4SLinus Torvalds 	if (diff >= maxdiff)
1331da177e4SLinus Torvalds 		return (__u32)-1;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	return (cookie -
1361da177e4SLinus Torvalds 		cookie_hash(saddr, daddr, sport, dport, count - diff, 1))
1371da177e4SLinus Torvalds 		& COOKIEMASK;	/* Leaving the data behind */
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds /*
141*5918e2fbSFlorian Westphal  * MSS Values are taken from the 2009 paper
142*5918e2fbSFlorian Westphal  * 'Measuring TCP Maximum Segment Size' by S. Alcock and R. Nelson:
143*5918e2fbSFlorian Westphal  *  - values 1440 to 1460 accounted for 80% of observed mss values
144*5918e2fbSFlorian Westphal  *  - values outside the 536-1460 range are rare (<0.2%).
145*5918e2fbSFlorian Westphal  *
146*5918e2fbSFlorian Westphal  * Table must be sorted.
1471da177e4SLinus Torvalds  */
1481da177e4SLinus Torvalds static __u16 const msstab[] = {
149*5918e2fbSFlorian Westphal 	64,
150*5918e2fbSFlorian Westphal 	512,
151*5918e2fbSFlorian Westphal 	536,
152*5918e2fbSFlorian Westphal 	1024,
153*5918e2fbSFlorian Westphal 	1440,
154*5918e2fbSFlorian Westphal 	1460,
155*5918e2fbSFlorian Westphal 	4312,
156*5918e2fbSFlorian Westphal 	8960,
1571da177e4SLinus Torvalds };
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds /*
1601da177e4SLinus Torvalds  * Generate a syncookie.  mssp points to the mss, which is returned
1611da177e4SLinus Torvalds  * rounded down to the value encoded in the cookie.
1621da177e4SLinus Torvalds  */
1631da177e4SLinus Torvalds __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb, __u16 *mssp)
1641da177e4SLinus Torvalds {
165aa8223c7SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
166aa8223c7SArnaldo Carvalho de Melo 	const struct tcphdr *th = tcp_hdr(skb);
1671da177e4SLinus Torvalds 	int mssind;
1681da177e4SLinus Torvalds 	const __u16 mss = *mssp;
1691da177e4SLinus Torvalds 
170a0f82f64SFlorian Westphal 	tcp_synq_overflow(sk);
1711da177e4SLinus Torvalds 
172*5918e2fbSFlorian Westphal 	for (mssind = ARRAY_SIZE(msstab) - 1; mssind ; mssind--)
173*5918e2fbSFlorian Westphal 		if (mss >= msstab[mssind])
174*5918e2fbSFlorian Westphal 			break;
175*5918e2fbSFlorian Westphal 	*mssp = msstab[mssind];
1761da177e4SLinus Torvalds 
177de0744afSPavel Emelyanov 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT);
1781da177e4SLinus Torvalds 
179aa8223c7SArnaldo Carvalho de Melo 	return secure_tcp_syn_cookie(iph->saddr, iph->daddr,
180aa8223c7SArnaldo Carvalho de Melo 				     th->source, th->dest, ntohl(th->seq),
1811da177e4SLinus Torvalds 				     jiffies / (HZ * 60), mssind);
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds /*
1851da177e4SLinus Torvalds  * This (misnamed) value is the age of syncookie which is permitted.
1861da177e4SLinus Torvalds  * Its ideal value should be dependent on TCP_TIMEOUT_INIT and
1871da177e4SLinus Torvalds  * sysctl_tcp_retries1. It's a rather complicated formula (exponential
1881da177e4SLinus Torvalds  * backoff) to compute at runtime so it's currently hardcoded here.
1891da177e4SLinus Torvalds  */
1901da177e4SLinus Torvalds #define COUNTER_TRIES 4
1911da177e4SLinus Torvalds /*
1921da177e4SLinus Torvalds  * Check if a ack sequence number is a valid syncookie.
1931da177e4SLinus Torvalds  * Return the decoded mss if it is, or 0 if not.
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds static inline int cookie_check(struct sk_buff *skb, __u32 cookie)
1961da177e4SLinus Torvalds {
197aa8223c7SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
198aa8223c7SArnaldo Carvalho de Melo 	const struct tcphdr *th = tcp_hdr(skb);
199aa8223c7SArnaldo Carvalho de Melo 	__u32 seq = ntohl(th->seq) - 1;
200aa8223c7SArnaldo Carvalho de Melo 	__u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
201aa8223c7SArnaldo Carvalho de Melo 					    th->source, th->dest, seq,
202aa8223c7SArnaldo Carvalho de Melo 					    jiffies / (HZ * 60),
203aa8223c7SArnaldo Carvalho de Melo 					    COUNTER_TRIES);
2041da177e4SLinus Torvalds 
205*5918e2fbSFlorian Westphal 	return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
20960236fddSArnaldo Carvalho de Melo 					   struct request_sock *req,
2101da177e4SLinus Torvalds 					   struct dst_entry *dst)
2111da177e4SLinus Torvalds {
2128292a17aSArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
2131da177e4SLinus Torvalds 	struct sock *child;
2141da177e4SLinus Torvalds 
2158292a17aSArnaldo Carvalho de Melo 	child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst);
2161da177e4SLinus Torvalds 	if (child)
217463c84b9SArnaldo Carvalho de Melo 		inet_csk_reqsk_queue_add(sk, req, child);
2181da177e4SLinus Torvalds 	else
21960236fddSArnaldo Carvalho de Melo 		reqsk_free(req);
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	return child;
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2244dfc2817SFlorian Westphal 
2254dfc2817SFlorian Westphal /*
2264dfc2817SFlorian Westphal  * when syncookies are in effect and tcp timestamps are enabled we stored
2274dfc2817SFlorian Westphal  * additional tcp options in the timestamp.
2284dfc2817SFlorian Westphal  * This extracts these options from the timestamp echo.
2294dfc2817SFlorian Westphal  *
2304dfc2817SFlorian Westphal  * The lowest 4 bits are for snd_wscale
2314dfc2817SFlorian Westphal  * The next 4 lsb are for rcv_wscale
2324dfc2817SFlorian Westphal  * The next lsb is for sack_ok
2334dfc2817SFlorian Westphal  */
2344dfc2817SFlorian Westphal void cookie_check_timestamp(struct tcp_options_received *tcp_opt)
2354dfc2817SFlorian Westphal {
2364dfc2817SFlorian Westphal 	/* echoed timestamp, 9 lowest bits contain options */
2374dfc2817SFlorian Westphal 	u32 options = tcp_opt->rcv_tsecr & TSMASK;
2384dfc2817SFlorian Westphal 
2394dfc2817SFlorian Westphal 	tcp_opt->snd_wscale = options & 0xf;
2404dfc2817SFlorian Westphal 	options >>= 4;
2414dfc2817SFlorian Westphal 	tcp_opt->rcv_wscale = options & 0xf;
2424dfc2817SFlorian Westphal 
2434dfc2817SFlorian Westphal 	tcp_opt->sack_ok = (options >> 4) & 0x1;
2444dfc2817SFlorian Westphal 
2454dfc2817SFlorian Westphal 	if (tcp_opt->sack_ok)
2464dfc2817SFlorian Westphal 		tcp_sack_reset(tcp_opt);
2474dfc2817SFlorian Westphal 
2484dfc2817SFlorian Westphal 	if (tcp_opt->snd_wscale || tcp_opt->rcv_wscale)
2494dfc2817SFlorian Westphal 		tcp_opt->wscale_ok = 1;
2504dfc2817SFlorian Westphal }
2514dfc2817SFlorian Westphal EXPORT_SYMBOL(cookie_check_timestamp);
2524dfc2817SFlorian Westphal 
2531da177e4SLinus Torvalds struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
2541da177e4SLinus Torvalds 			     struct ip_options *opt)
2551da177e4SLinus Torvalds {
2564957faadSWilliam Allen Simpson 	struct tcp_options_received tcp_opt;
2574957faadSWilliam Allen Simpson 	u8 *hash_location;
2582e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq;
2592e6599cbSArnaldo Carvalho de Melo 	struct tcp_request_sock *treq;
2601da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
261aa8223c7SArnaldo Carvalho de Melo 	const struct tcphdr *th = tcp_hdr(skb);
262aa8223c7SArnaldo Carvalho de Melo 	__u32 cookie = ntohl(th->ack_seq) - 1;
2631da177e4SLinus Torvalds 	struct sock *ret = sk;
26460236fddSArnaldo Carvalho de Melo 	struct request_sock *req;
2651da177e4SLinus Torvalds 	int mss;
2661da177e4SLinus Torvalds 	struct rtable *rt;
2671da177e4SLinus Torvalds 	__u8 rcv_wscale;
2681da177e4SLinus Torvalds 
269af9b4738SFlorian Westphal 	if (!sysctl_tcp_syncookies || !th->ack || th->rst)
2701da177e4SLinus Torvalds 		goto out;
2711da177e4SLinus Torvalds 
272a0f82f64SFlorian Westphal 	if (tcp_synq_no_recent_overflow(sk) ||
2731da177e4SLinus Torvalds 	    (mss = cookie_check(skb, cookie)) == 0) {
274de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED);
2751da177e4SLinus Torvalds 		goto out;
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 
278de0744afSPavel Emelyanov 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESRECV);
2791da177e4SLinus Torvalds 
280bb5b7c11SDavid S. Miller 	/* check for timestamp cookie support */
281bb5b7c11SDavid S. Miller 	memset(&tcp_opt, 0, sizeof(tcp_opt));
282bb5b7c11SDavid S. Miller 	tcp_parse_options(skb, &tcp_opt, &hash_location, 0);
283bb5b7c11SDavid S. Miller 
284bb5b7c11SDavid S. Miller 	if (tcp_opt.saw_tstamp)
285bb5b7c11SDavid S. Miller 		cookie_check_timestamp(&tcp_opt);
286bb5b7c11SDavid S. Miller 
2871da177e4SLinus Torvalds 	ret = NULL;
288ce4a7d0dSArnaldo Carvalho de Melo 	req = inet_reqsk_alloc(&tcp_request_sock_ops); /* for safety */
2891da177e4SLinus Torvalds 	if (!req)
2901da177e4SLinus Torvalds 		goto out;
2911da177e4SLinus Torvalds 
2922e6599cbSArnaldo Carvalho de Melo 	ireq = inet_rsk(req);
2932e6599cbSArnaldo Carvalho de Melo 	treq = tcp_rsk(req);
294aa8223c7SArnaldo Carvalho de Melo 	treq->rcv_isn		= ntohl(th->seq) - 1;
2952e6599cbSArnaldo Carvalho de Melo 	treq->snt_isn		= cookie;
2961da177e4SLinus Torvalds 	req->mss		= mss;
297a3116ac5SKOVACS Krisztian 	ireq->loc_port		= th->dest;
298aa8223c7SArnaldo Carvalho de Melo 	ireq->rmt_port		= th->source;
299eddc9ec5SArnaldo Carvalho de Melo 	ireq->loc_addr		= ip_hdr(skb)->daddr;
300eddc9ec5SArnaldo Carvalho de Melo 	ireq->rmt_addr		= ip_hdr(skb)->saddr;
30116df845fSFlorian Westphal 	ireq->ecn_ok		= 0;
302bb5b7c11SDavid S. Miller 	ireq->snd_wscale	= tcp_opt.snd_wscale;
303bb5b7c11SDavid S. Miller 	ireq->rcv_wscale	= tcp_opt.rcv_wscale;
304bb5b7c11SDavid S. Miller 	ireq->sack_ok		= tcp_opt.sack_ok;
305bb5b7c11SDavid S. Miller 	ireq->wscale_ok		= tcp_opt.wscale_ok;
306bb5b7c11SDavid S. Miller 	ireq->tstamp_ok		= tcp_opt.saw_tstamp;
307bb5b7c11SDavid S. Miller 	req->ts_recent		= tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval : 0;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	/* We throwed the options of the initial SYN away, so we hope
3101da177e4SLinus Torvalds 	 * the ACK carries the same options again (see RFC1122 4.2.3.8)
3111da177e4SLinus Torvalds 	 */
3121da177e4SLinus Torvalds 	if (opt && opt->optlen) {
3131da177e4SLinus Torvalds 		int opt_size = sizeof(struct ip_options) + opt->optlen;
3141da177e4SLinus Torvalds 
3152e6599cbSArnaldo Carvalho de Melo 		ireq->opt = kmalloc(opt_size, GFP_ATOMIC);
3162e6599cbSArnaldo Carvalho de Melo 		if (ireq->opt != NULL && ip_options_echo(ireq->opt, skb)) {
3172e6599cbSArnaldo Carvalho de Melo 			kfree(ireq->opt);
3182e6599cbSArnaldo Carvalho de Melo 			ireq->opt = NULL;
3191da177e4SLinus Torvalds 		}
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds 
322284904aaSPaul Moore 	if (security_inet_conn_request(sk, skb, req)) {
323284904aaSPaul Moore 		reqsk_free(req);
324284904aaSPaul Moore 		goto out;
325284904aaSPaul Moore 	}
326284904aaSPaul Moore 
3271da177e4SLinus Torvalds 	req->expires	= 0UL;
3281da177e4SLinus Torvalds 	req->retrans	= 0;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	/*
3311da177e4SLinus Torvalds 	 * We need to lookup the route here to get at the correct
3321da177e4SLinus Torvalds 	 * window size. We should better make sure that the window size
3331da177e4SLinus Torvalds 	 * hasn't changed since we received the original syn, but I see
3341da177e4SLinus Torvalds 	 * no easy way to do this.
3351da177e4SLinus Torvalds 	 */
3361da177e4SLinus Torvalds 	{
337ffce9082SAtis Elsts 		struct flowi fl = { .mark = sk->sk_mark,
338ffce9082SAtis Elsts 				    .nl_u = { .ip4_u =
3391da177e4SLinus Torvalds 					      { .daddr = ((opt && opt->srr) ?
3401da177e4SLinus Torvalds 							  opt->faddr :
3412e6599cbSArnaldo Carvalho de Melo 							  ireq->rmt_addr),
3422e6599cbSArnaldo Carvalho de Melo 						.saddr = ireq->loc_addr,
3431da177e4SLinus Torvalds 						.tos = RT_CONN_FLAGS(sk) } },
3441da177e4SLinus Torvalds 				    .proto = IPPROTO_TCP,
34586b08d86SKOVACS Krisztian 				    .flags = inet_sk_flowi_flags(sk),
3461da177e4SLinus Torvalds 				    .uli_u = { .ports =
347aa8223c7SArnaldo Carvalho de Melo 					       { .sport = th->dest,
348aa8223c7SArnaldo Carvalho de Melo 						 .dport = th->source } } };
3494237c75cSVenkat Yekkirala 		security_req_classify_flow(req, &fl);
350f206351aSDenis V. Lunev 		if (ip_route_output_key(&init_net, &rt, &fl)) {
35160236fddSArnaldo Carvalho de Melo 			reqsk_free(req);
3521da177e4SLinus Torvalds 			goto out;
3531da177e4SLinus Torvalds 		}
3541da177e4SLinus Torvalds 	}
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	/* Try to redo what tcp_v4_send_synack did. */
3574dfc2817SFlorian Westphal 	req->window_clamp = tp->window_clamp ? :dst_metric(&rt->u.dst, RTAX_WINDOW);
3584dfc2817SFlorian Westphal 
3591da177e4SLinus Torvalds 	tcp_select_initial_window(tcp_full_space(sk), req->mss,
3601da177e4SLinus Torvalds 				  &req->rcv_wnd, &req->window_clamp,
36131d12926Slaurent chavey 				  ireq->wscale_ok, &rcv_wscale,
36231d12926Slaurent chavey 				  dst_metric(&rt->u.dst, RTAX_INITRWND));
3634dfc2817SFlorian Westphal 
3642e6599cbSArnaldo Carvalho de Melo 	ireq->rcv_wscale  = rcv_wscale;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	ret = get_cookie_sock(sk, skb, req, &rt->u.dst);
3671da177e4SLinus Torvalds out:	return ret;
3681da177e4SLinus Torvalds }
369