xref: /linux/net/ipv4/syncookies.c (revision 9d8c05ad5627b3a650087c14c67dd1d22a6368ab)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Syncookies implementation for the Linux kernel
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1997 Andi Kleen
61da177e4SLinus Torvalds  *  Based on ideas by D.J.Bernstein and Eric Schenk.
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/tcp.h>
101da177e4SLinus Torvalds #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/random.h>
12fe62d05bSJason A. Donenfeld #include <linux/siphash.h>
131da177e4SLinus Torvalds #include <linux/kernel.h>
14bc3b2d7fSPaul Gortmaker #include <linux/export.h>
1584b114b9SEric Dumazet #include <net/secure_seq.h>
161da177e4SLinus Torvalds #include <net/tcp.h>
1786b08d86SKOVACS Krisztian #include <net/route.h>
181da177e4SLinus Torvalds 
19fe62d05bSJason A. Donenfeld static siphash_key_t syncookie_secret[2] __read_mostly;
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #define COOKIEBITS 24	/* Upper bits store count */
221da177e4SLinus Torvalds #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
231da177e4SLinus Torvalds 
24274e2da0SFlorian Westphal /* TCP Timestamp: 6 lowest bits of timestamp sent in the cookie SYN-ACK
25274e2da0SFlorian Westphal  * stores TCP options:
26274e2da0SFlorian Westphal  *
27274e2da0SFlorian Westphal  * MSB                               LSB
28274e2da0SFlorian Westphal  * | 31 ...   6 |  5  |  4   | 3 2 1 0 |
29274e2da0SFlorian Westphal  * |  Timestamp | ECN | SACK | WScale  |
30274e2da0SFlorian Westphal  *
31274e2da0SFlorian Westphal  * When we receive a valid cookie-ACK, we look at the echoed tsval (if
32274e2da0SFlorian Westphal  * any) to figure out which TCP options we should use for the rebuilt
33274e2da0SFlorian Westphal  * connection.
34274e2da0SFlorian Westphal  *
35274e2da0SFlorian Westphal  * A WScale setting of '0xf' (which is an invalid scaling value)
36274e2da0SFlorian Westphal  * means that original syn did not include the TCP window scaling option.
37274e2da0SFlorian Westphal  */
38274e2da0SFlorian Westphal #define TS_OPT_WSCALE_MASK	0xf
39274e2da0SFlorian Westphal #define TS_OPT_SACK		BIT(4)
40274e2da0SFlorian Westphal #define TS_OPT_ECN		BIT(5)
41274e2da0SFlorian Westphal /* There is no TS_OPT_TIMESTAMP:
42274e2da0SFlorian Westphal  * if ACK contains timestamp option, we already know it was
43274e2da0SFlorian Westphal  * requested/supported by the syn/synack exchange.
44274e2da0SFlorian Westphal  */
45274e2da0SFlorian Westphal #define TSBITS	6
46274e2da0SFlorian Westphal #define TSMASK	(((__u32)1 << TSBITS) - 1)
47274e2da0SFlorian Westphal 
48714e85beSAl Viro static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport,
491da177e4SLinus Torvalds 		       u32 count, int c)
501da177e4SLinus Torvalds {
51b23a002fSHannes Frederic Sowa 	net_get_random_once(syncookie_secret, sizeof(syncookie_secret));
52fe62d05bSJason A. Donenfeld 	return siphash_4u32((__force u32)saddr, (__force u32)daddr,
53fe62d05bSJason A. Donenfeld 			    (__force u32)sport << 16 | (__force u32)dport,
54fe62d05bSJason A. Donenfeld 			    count, &syncookie_secret[c]);
551da177e4SLinus Torvalds }
561da177e4SLinus Torvalds 
574dfc2817SFlorian Westphal 
584dfc2817SFlorian Westphal /*
594dfc2817SFlorian Westphal  * when syncookies are in effect and tcp timestamps are enabled we encode
60734f614bSFlorian Westphal  * tcp options in the lower bits of the timestamp value that will be
614dfc2817SFlorian Westphal  * sent in the syn-ack.
624dfc2817SFlorian Westphal  * Since subsequent timestamps use the normal tcp_time_stamp value, we
634dfc2817SFlorian Westphal  * must make sure that the resulting initial timestamp is <= tcp_time_stamp.
644dfc2817SFlorian Westphal  */
65200ecef6SEric Dumazet u64 cookie_init_timestamp(struct request_sock *req, u64 now)
664dfc2817SFlorian Westphal {
674dfc2817SFlorian Westphal 	struct inet_request_sock *ireq;
68200ecef6SEric Dumazet 	u32 ts, ts_now = tcp_ns_to_ts(now);
694dfc2817SFlorian Westphal 	u32 options = 0;
704dfc2817SFlorian Westphal 
714dfc2817SFlorian Westphal 	ireq = inet_rsk(req);
72734f614bSFlorian Westphal 
73274e2da0SFlorian Westphal 	options = ireq->wscale_ok ? ireq->snd_wscale : TS_OPT_WSCALE_MASK;
74274e2da0SFlorian Westphal 	if (ireq->sack_ok)
75274e2da0SFlorian Westphal 		options |= TS_OPT_SACK;
76274e2da0SFlorian Westphal 	if (ireq->ecn_ok)
77274e2da0SFlorian Westphal 		options |= TS_OPT_ECN;
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 	}
87d3edd06eSEric Dumazet 	return (u64)ts * (NSEC_PER_SEC / TCP_TS_HZ);
884dfc2817SFlorian Westphal }
894dfc2817SFlorian Westphal 
904dfc2817SFlorian Westphal 
91714e85beSAl Viro static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
928c27bd75SFlorian Westphal 				   __be16 dport, __u32 sseq, __u32 data)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds 	/*
951da177e4SLinus Torvalds 	 * Compute the secure sequence number.
961da177e4SLinus Torvalds 	 * The output should be:
971da177e4SLinus Torvalds 	 *   HASH(sec1,saddr,sport,daddr,dport,sec1) + sseq + (count * 2^24)
981da177e4SLinus Torvalds 	 *      + (HASH(sec2,saddr,sport,daddr,dport,count,sec2) % 2^24).
991da177e4SLinus Torvalds 	 * Where sseq is their sequence number and count increases every
1001da177e4SLinus Torvalds 	 * minute by 1.
1011da177e4SLinus Torvalds 	 * As an extra hack, we add a small "data" value that encodes the
1021da177e4SLinus Torvalds 	 * MSS into the second hash value.
1031da177e4SLinus Torvalds 	 */
1048c27bd75SFlorian Westphal 	u32 count = tcp_cookie_time();
1051da177e4SLinus Torvalds 	return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
1061da177e4SLinus Torvalds 		sseq + (count << COOKIEBITS) +
1071da177e4SLinus Torvalds 		((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
1081da177e4SLinus Torvalds 		 & COOKIEMASK));
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /*
1121da177e4SLinus Torvalds  * This retrieves the small "data" value from the syncookie.
1131da177e4SLinus Torvalds  * If the syncookie is bad, the data returned will be out of
1141da177e4SLinus Torvalds  * range.  This must be checked by the caller.
1151da177e4SLinus Torvalds  *
1168c27bd75SFlorian Westphal  * The count value used to generate the cookie must be less than
1178c27bd75SFlorian Westphal  * MAX_SYNCOOKIE_AGE minutes in the past.
1188c27bd75SFlorian Westphal  * The return value (__u32)-1 if this test fails.
1191da177e4SLinus Torvalds  */
120714e85beSAl Viro static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr,
1218c27bd75SFlorian Westphal 				  __be16 sport, __be16 dport, __u32 sseq)
1221da177e4SLinus Torvalds {
1238c27bd75SFlorian Westphal 	u32 diff, count = tcp_cookie_time();
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	/* Strip away the layers from the cookie */
1261da177e4SLinus Torvalds 	cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	/* Cookie is now reduced to (count * 2^24) ^ (hash % 2^24) */
1291da177e4SLinus Torvalds 	diff = (count - (cookie >> COOKIEBITS)) & ((__u32) -1 >> COOKIEBITS);
1308c27bd75SFlorian Westphal 	if (diff >= MAX_SYNCOOKIE_AGE)
1311da177e4SLinus Torvalds 		return (__u32)-1;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	return (cookie -
1341da177e4SLinus Torvalds 		cookie_hash(saddr, daddr, sport, dport, count - diff, 1))
1351da177e4SLinus Torvalds 		& COOKIEMASK;	/* Leaving the data behind */
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /*
13908629354SFlorian Westphal  * MSS Values are chosen based on the 2011 paper
14008629354SFlorian Westphal  * 'An Analysis of TCP Maximum Segement Sizes' by S. Alcock and R. Nelson.
14108629354SFlorian Westphal  * Values ..
14208629354SFlorian Westphal  *  .. lower than 536 are rare (< 0.2%)
14308629354SFlorian Westphal  *  .. between 537 and 1299 account for less than < 1.5% of observed values
14408629354SFlorian Westphal  *  .. in the 1300-1349 range account for about 15 to 20% of observed mss values
14508629354SFlorian Westphal  *  .. exceeding 1460 are very rare (< 0.04%)
1465918e2fbSFlorian Westphal  *
14708629354SFlorian Westphal  *  1460 is the single most frequently announced mss value (30 to 46% depending
14808629354SFlorian Westphal  *  on monitor location).  Table must be sorted.
1491da177e4SLinus Torvalds  */
1501da177e4SLinus Torvalds static __u16 const msstab[] = {
1515918e2fbSFlorian Westphal 	536,
15208629354SFlorian Westphal 	1300,
15308629354SFlorian Westphal 	1440,	/* 1440, 1452: PPPoE */
1545918e2fbSFlorian Westphal 	1460,
1551da177e4SLinus Torvalds };
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /*
1581da177e4SLinus Torvalds  * Generate a syncookie.  mssp points to the mss, which is returned
1591da177e4SLinus Torvalds  * rounded down to the value encoded in the cookie.
1601da177e4SLinus Torvalds  */
1610198230bSPatrick McHardy u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
1620198230bSPatrick McHardy 			      u16 *mssp)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	int mssind;
1651da177e4SLinus Torvalds 	const __u16 mss = *mssp;
1661da177e4SLinus Torvalds 
1675918e2fbSFlorian Westphal 	for (mssind = ARRAY_SIZE(msstab) - 1; mssind ; mssind--)
1685918e2fbSFlorian Westphal 		if (mss >= msstab[mssind])
1695918e2fbSFlorian Westphal 			break;
1705918e2fbSFlorian Westphal 	*mssp = msstab[mssind];
1711da177e4SLinus Torvalds 
172aa8223c7SArnaldo Carvalho de Melo 	return secure_tcp_syn_cookie(iph->saddr, iph->daddr,
173aa8223c7SArnaldo Carvalho de Melo 				     th->source, th->dest, ntohl(th->seq),
1748c27bd75SFlorian Westphal 				     mssind);
1751da177e4SLinus Torvalds }
1760198230bSPatrick McHardy EXPORT_SYMBOL_GPL(__cookie_v4_init_sequence);
1770198230bSPatrick McHardy 
1783f684b4bSEric Dumazet __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mssp)
1790198230bSPatrick McHardy {
1800198230bSPatrick McHardy 	const struct iphdr *iph = ip_hdr(skb);
1810198230bSPatrick McHardy 	const struct tcphdr *th = tcp_hdr(skb);
1820198230bSPatrick McHardy 
1830198230bSPatrick McHardy 	return __cookie_v4_init_sequence(iph, th, mssp);
1840198230bSPatrick McHardy }
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds /*
1871da177e4SLinus Torvalds  * Check if a ack sequence number is a valid syncookie.
1881da177e4SLinus Torvalds  * Return the decoded mss if it is, or 0 if not.
1891da177e4SLinus Torvalds  */
1900198230bSPatrick McHardy int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
1910198230bSPatrick McHardy 		      u32 cookie)
1921da177e4SLinus Torvalds {
193aa8223c7SArnaldo Carvalho de Melo 	__u32 seq = ntohl(th->seq) - 1;
194aa8223c7SArnaldo Carvalho de Melo 	__u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
1958c27bd75SFlorian Westphal 					    th->source, th->dest, seq);
1961da177e4SLinus Torvalds 
1975918e2fbSFlorian Westphal 	return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
1981da177e4SLinus Torvalds }
1990198230bSPatrick McHardy EXPORT_SYMBOL_GPL(__cookie_v4_check);
2001da177e4SLinus Torvalds 
201b80c0e78SEric Dumazet struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
20260236fddSArnaldo Carvalho de Melo 				 struct request_sock *req,
20384b114b9SEric Dumazet 				 struct dst_entry *dst, u32 tsoff)
2041da177e4SLinus Torvalds {
2058292a17aSArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
2061da177e4SLinus Torvalds 	struct sock *child;
2075e0724d0SEric Dumazet 	bool own_req;
2081da177e4SLinus Torvalds 
2095e0724d0SEric Dumazet 	child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
2105e0724d0SEric Dumazet 						 NULL, &own_req);
2110470c8caSEric Dumazet 	if (child) {
21241c6d650SReshetova, Elena 		refcount_set(&req->rsk_refcnt, 1);
21384b114b9SEric Dumazet 		tcp_sk(child)->tsoffset = tsoff;
2146bcfd7f8SEric Dumazet 		sock_rps_save_rxhash(child, skb);
2159466a1ccSFlorian Westphal 
2160e8642cfSEric Dumazet 		if (rsk_drop_req(req)) {
217*9d8c05adSPaolo Abeni 			reqsk_put(req);
2189466a1ccSFlorian Westphal 			return child;
2199466a1ccSFlorian Westphal 		}
2209466a1ccSFlorian Westphal 
2219403cf23SGuillaume Nault 		if (inet_csk_reqsk_queue_add(sk, req, child))
2229403cf23SGuillaume Nault 			return child;
2239403cf23SGuillaume Nault 
2249d3e1368SGuillaume Nault 		bh_unlock_sock(child);
2259d3e1368SGuillaume Nault 		sock_put(child);
2269d3e1368SGuillaume Nault 	}
2279403cf23SGuillaume Nault 	__reqsk_free(req);
2289403cf23SGuillaume Nault 
2299403cf23SGuillaume Nault 	return NULL;
2301da177e4SLinus Torvalds }
231b80c0e78SEric Dumazet EXPORT_SYMBOL(tcp_get_cookie_sock);
2324dfc2817SFlorian Westphal 
2334dfc2817SFlorian Westphal /*
2344dfc2817SFlorian Westphal  * when syncookies are in effect and tcp timestamps are enabled we stored
2354dfc2817SFlorian Westphal  * additional tcp options in the timestamp.
2364dfc2817SFlorian Westphal  * This extracts these options from the timestamp echo.
2374dfc2817SFlorian Westphal  *
238f1673381SFlorian Westphal  * return false if we decode a tcp option that is disabled
239f1673381SFlorian Westphal  * on the host.
2404dfc2817SFlorian Westphal  */
241f9301034SEric Dumazet bool cookie_timestamp_decode(const struct net *net,
242f9301034SEric Dumazet 			     struct tcp_options_received *tcp_opt)
2434dfc2817SFlorian Westphal {
244734f614bSFlorian Westphal 	/* echoed timestamp, lowest bits contain options */
245274e2da0SFlorian Westphal 	u32 options = tcp_opt->rcv_tsecr;
2464dfc2817SFlorian Westphal 
2478c763681SFlorian Westphal 	if (!tcp_opt->saw_tstamp)  {
2488c763681SFlorian Westphal 		tcp_clear_options(tcp_opt);
2498c763681SFlorian Westphal 		return true;
2508c763681SFlorian Westphal 	}
2518c763681SFlorian Westphal 
2525d2ed052SEric Dumazet 	if (!net->ipv4.sysctl_tcp_timestamps)
2538c763681SFlorian Westphal 		return false;
2548c763681SFlorian Westphal 
255274e2da0SFlorian Westphal 	tcp_opt->sack_ok = (options & TS_OPT_SACK) ? TCP_SACK_SEEN : 0;
2564dfc2817SFlorian Westphal 
257f9301034SEric Dumazet 	if (tcp_opt->sack_ok && !net->ipv4.sysctl_tcp_sack)
2588c763681SFlorian Westphal 		return false;
2594dfc2817SFlorian Westphal 
260274e2da0SFlorian Westphal 	if ((options & TS_OPT_WSCALE_MASK) == TS_OPT_WSCALE_MASK)
261734f614bSFlorian Westphal 		return true; /* no window scaling */
262734f614bSFlorian Westphal 
2634dfc2817SFlorian Westphal 	tcp_opt->wscale_ok = 1;
264274e2da0SFlorian Westphal 	tcp_opt->snd_wscale = options & TS_OPT_WSCALE_MASK;
265274e2da0SFlorian Westphal 
2669bb37ef0SEric Dumazet 	return net->ipv4.sysctl_tcp_window_scaling != 0;
2678c763681SFlorian Westphal }
268f1673381SFlorian Westphal EXPORT_SYMBOL(cookie_timestamp_decode);
269f1673381SFlorian Westphal 
270f1673381SFlorian Westphal bool cookie_ecn_ok(const struct tcp_options_received *tcp_opt,
271f7b3bec6SFlorian Westphal 		   const struct net *net, const struct dst_entry *dst)
272f1673381SFlorian Westphal {
273f1673381SFlorian Westphal 	bool ecn_ok = tcp_opt->rcv_tsecr & TS_OPT_ECN;
274f1673381SFlorian Westphal 
275f1673381SFlorian Westphal 	if (!ecn_ok)
276f1673381SFlorian Westphal 		return false;
277f1673381SFlorian Westphal 
278f1673381SFlorian Westphal 	if (net->ipv4.sysctl_tcp_ecn)
279f1673381SFlorian Westphal 		return true;
280f1673381SFlorian Westphal 
281f7b3bec6SFlorian Westphal 	return dst_feature(dst, RTAX_FEATURE_ECN);
282f1673381SFlorian Westphal }
283f1673381SFlorian Westphal EXPORT_SYMBOL(cookie_ecn_ok);
2844dfc2817SFlorian Westphal 
2856fc8c827SFlorian Westphal struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops,
2866fc8c827SFlorian Westphal 					    struct sock *sk,
2876fc8c827SFlorian Westphal 					    struct sk_buff *skb)
2886fc8c827SFlorian Westphal {
2896fc8c827SFlorian Westphal 	struct request_sock *req;
2906fc8c827SFlorian Westphal 
2916fc8c827SFlorian Westphal #ifdef CONFIG_MPTCP
2920e8642cfSEric Dumazet 	struct tcp_request_sock *treq;
2930e8642cfSEric Dumazet 
2946fc8c827SFlorian Westphal 	if (sk_is_mptcp(sk))
2956fc8c827SFlorian Westphal 		ops = &mptcp_subflow_request_sock_ops;
2966fc8c827SFlorian Westphal #endif
2976fc8c827SFlorian Westphal 
2986fc8c827SFlorian Westphal 	req = inet_reqsk_alloc(ops, sk, false);
2996fc8c827SFlorian Westphal 	if (!req)
3006fc8c827SFlorian Westphal 		return NULL;
3016fc8c827SFlorian Westphal 
3026fc8c827SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP)
3036fc8c827SFlorian Westphal 	treq = tcp_rsk(req);
3046fc8c827SFlorian Westphal 	treq->is_mptcp = sk_is_mptcp(sk);
3056fc8c827SFlorian Westphal 	if (treq->is_mptcp) {
3066fc8c827SFlorian Westphal 		int err = mptcp_subflow_init_cookie_req(req, sk, skb);
3076fc8c827SFlorian Westphal 
3086fc8c827SFlorian Westphal 		if (err) {
3096fc8c827SFlorian Westphal 			reqsk_free(req);
3106fc8c827SFlorian Westphal 			return NULL;
3116fc8c827SFlorian Westphal 		}
3126fc8c827SFlorian Westphal 	}
3136fc8c827SFlorian Westphal #endif
3146fc8c827SFlorian Westphal 
3156fc8c827SFlorian Westphal 	return req;
3166fc8c827SFlorian Westphal }
3176fc8c827SFlorian Westphal EXPORT_SYMBOL_GPL(cookie_tcp_reqsk_alloc);
3186fc8c827SFlorian Westphal 
319079096f1SEric Dumazet /* On input, sk is a listener.
320079096f1SEric Dumazet  * Output is listener if incoming packet would not create a child
321079096f1SEric Dumazet  *           NULL if memory could not be allocated.
322079096f1SEric Dumazet  */
323461b74c3SCong Wang struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
3241da177e4SLinus Torvalds {
325461b74c3SCong Wang 	struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
3264957faadSWilliam Allen Simpson 	struct tcp_options_received tcp_opt;
3272e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq;
3282e6599cbSArnaldo Carvalho de Melo 	struct tcp_request_sock *treq;
3291da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
330aa8223c7SArnaldo Carvalho de Melo 	const struct tcphdr *th = tcp_hdr(skb);
331aa8223c7SArnaldo Carvalho de Melo 	__u32 cookie = ntohl(th->ack_seq) - 1;
3321da177e4SLinus Torvalds 	struct sock *ret = sk;
33360236fddSArnaldo Carvalho de Melo 	struct request_sock *req;
3341da177e4SLinus Torvalds 	int mss;
3351da177e4SLinus Torvalds 	struct rtable *rt;
3361da177e4SLinus Torvalds 	__u8 rcv_wscale;
337dfd25fffSEric Dumazet 	struct flowi4 fl4;
33884b114b9SEric Dumazet 	u32 tsoff = 0;
3391da177e4SLinus Torvalds 
34012ed8244SNikolay Borisov 	if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies || !th->ack || th->rst)
3411da177e4SLinus Torvalds 		goto out;
3421da177e4SLinus Torvalds 
343646697b9SFlorian Westphal 	if (tcp_synq_no_recent_overflow(sk))
344646697b9SFlorian Westphal 		goto out;
345646697b9SFlorian Westphal 
346646697b9SFlorian Westphal 	mss = __cookie_v4_check(ip_hdr(skb), th, cookie);
347646697b9SFlorian Westphal 	if (mss == 0) {
34802a1d6e7SEric Dumazet 		__NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED);
3491da177e4SLinus Torvalds 		goto out;
3501da177e4SLinus Torvalds 	}
3511da177e4SLinus Torvalds 
35202a1d6e7SEric Dumazet 	__NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESRECV);
3531da177e4SLinus Torvalds 
354bb5b7c11SDavid S. Miller 	/* check for timestamp cookie support */
355bb5b7c11SDavid S. Miller 	memset(&tcp_opt, 0, sizeof(tcp_opt));
356eed29f17SEric Dumazet 	tcp_parse_options(sock_net(sk), skb, &tcp_opt, 0, NULL);
357bb5b7c11SDavid S. Miller 
35884b114b9SEric Dumazet 	if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) {
3595d2ed052SEric Dumazet 		tsoff = secure_tcp_ts_off(sock_net(sk),
3605d2ed052SEric Dumazet 					  ip_hdr(skb)->daddr,
3615d2ed052SEric Dumazet 					  ip_hdr(skb)->saddr);
36284b114b9SEric Dumazet 		tcp_opt.rcv_tsecr -= tsoff;
36384b114b9SEric Dumazet 	}
36484b114b9SEric Dumazet 
365f9301034SEric Dumazet 	if (!cookie_timestamp_decode(sock_net(sk), &tcp_opt))
3668c763681SFlorian Westphal 		goto out;
367bb5b7c11SDavid S. Miller 
3681da177e4SLinus Torvalds 	ret = NULL;
3696fc8c827SFlorian Westphal 	req = cookie_tcp_reqsk_alloc(&tcp_request_sock_ops, sk, skb);
3701da177e4SLinus Torvalds 	if (!req)
3711da177e4SLinus Torvalds 		goto out;
3721da177e4SLinus Torvalds 
3732e6599cbSArnaldo Carvalho de Melo 	ireq = inet_rsk(req);
3742e6599cbSArnaldo Carvalho de Melo 	treq = tcp_rsk(req);
375aa8223c7SArnaldo Carvalho de Melo 	treq->rcv_isn		= ntohl(th->seq) - 1;
3762e6599cbSArnaldo Carvalho de Melo 	treq->snt_isn		= cookie;
37795a22caeSFlorian Westphal 	treq->ts_off		= 0;
37818bcf290SAlexander Potapenko 	treq->txhash		= net_tx_rndhash();
3791da177e4SLinus Torvalds 	req->mss		= mss;
380b44084c2SEric Dumazet 	ireq->ir_num		= ntohs(th->dest);
381634fb979SEric Dumazet 	ireq->ir_rmt_port	= th->source;
38208d2cc3bSEric Dumazet 	sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
38308d2cc3bSEric Dumazet 	sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
38484f39b08SLorenzo Colitti 	ireq->ir_mark		= inet_request_mark(sk, skb);
385bb5b7c11SDavid S. Miller 	ireq->snd_wscale	= tcp_opt.snd_wscale;
386bb5b7c11SDavid S. Miller 	ireq->sack_ok		= tcp_opt.sack_ok;
387bb5b7c11SDavid S. Miller 	ireq->wscale_ok		= tcp_opt.wscale_ok;
388bb5b7c11SDavid S. Miller 	ireq->tstamp_ok		= tcp_opt.saw_tstamp;
389bb5b7c11SDavid S. Miller 	req->ts_recent		= tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval : 0;
3909a568de4SEric Dumazet 	treq->snt_synack	= 0;
3919439ce00SEric Dumazet 	treq->tfo_listener	= false;
392ae2dd716SFlorian Westphal 
393bc58a1baSHans Wippel 	if (IS_ENABLED(CONFIG_SMC))
394bc58a1baSHans Wippel 		ireq->smc_ok = 0;
3951da177e4SLinus Torvalds 
3966dd9a14eSDavid Ahern 	ireq->ir_iif = inet_request_bound_dev_if(sk, skb);
39716f86165SEric Dumazet 
3981da177e4SLinus Torvalds 	/* We throwed the options of the initial SYN away, so we hope
3991da177e4SLinus Torvalds 	 * the ACK carries the same options again (see RFC1122 4.2.3.8)
4001da177e4SLinus Torvalds 	 */
401c92e8c02SEric Dumazet 	RCU_INIT_POINTER(ireq->ireq_opt, tcp_v4_save_options(sock_net(sk), skb));
4021da177e4SLinus Torvalds 
403284904aaSPaul Moore 	if (security_inet_conn_request(sk, skb, req)) {
4040470c8caSEric Dumazet 		reqsk_free(req);
405284904aaSPaul Moore 		goto out;
406284904aaSPaul Moore 	}
407284904aaSPaul Moore 
408e6c022a4SEric Dumazet 	req->num_retrans = 0;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	/*
4111da177e4SLinus Torvalds 	 * We need to lookup the route here to get at the correct
4121da177e4SLinus Torvalds 	 * window size. We should better make sure that the window size
4131da177e4SLinus Torvalds 	 * hasn't changed since we received the original syn, but I see
4141da177e4SLinus Torvalds 	 * no easy way to do this.
4151da177e4SLinus Torvalds 	 */
4166dd9a14eSDavid Ahern 	flowi4_init_output(&fl4, ireq->ir_iif, ireq->ir_mark,
417d66954a0SDmitry Popov 			   RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, IPPROTO_TCP,
4181bba6ffeSDavid S. Miller 			   inet_sk_flowi_flags(sk),
419461b74c3SCong Wang 			   opt->srr ? opt->faddr : ireq->ir_rmt_addr,
420e2d118a1SLorenzo Colitti 			   ireq->ir_loc_addr, th->source, th->dest, sk->sk_uid);
4219d6ec938SDavid S. Miller 	security_req_classify_flow(req, flowi4_to_flowi(&fl4));
4229d6ec938SDavid S. Miller 	rt = ip_route_output_key(sock_net(sk), &fl4);
423b23dd4feSDavid S. Miller 	if (IS_ERR(rt)) {
4240470c8caSEric Dumazet 		reqsk_free(req);
4251da177e4SLinus Torvalds 		goto out;
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	/* Try to redo what tcp_v4_send_synack did. */
429ed53d0abSEric Dumazet 	req->rsk_window_clamp = tp->window_clamp ? :dst_metric(&rt->dst, RTAX_WINDOW);
4304dfc2817SFlorian Westphal 
431ceef9ab6SEric Dumazet 	tcp_select_initial_window(sk, tcp_full_space(sk), req->mss,
432ed53d0abSEric Dumazet 				  &req->rsk_rcv_wnd, &req->rsk_window_clamp,
43331d12926Slaurent chavey 				  ireq->wscale_ok, &rcv_wscale,
434d8d1f30bSChangli Gao 				  dst_metric(&rt->dst, RTAX_INITRWND));
4354dfc2817SFlorian Westphal 
4362e6599cbSArnaldo Carvalho de Melo 	ireq->rcv_wscale  = rcv_wscale;
437f7b3bec6SFlorian Westphal 	ireq->ecn_ok = cookie_ecn_ok(&tcp_opt, sock_net(sk), &rt->dst);
4381da177e4SLinus Torvalds 
43984b114b9SEric Dumazet 	ret = tcp_get_cookie_sock(sk, skb, req, &rt->dst, tsoff);
440dfd25fffSEric Dumazet 	/* ip_queue_xmit() depends on our flow being setup
441dfd25fffSEric Dumazet 	 * Normal sockets get it right from inet_csk_route_child_sock()
442dfd25fffSEric Dumazet 	 */
443dfd25fffSEric Dumazet 	if (ret)
444dfd25fffSEric Dumazet 		inet_sk(ret)->cork.fl.u.ip4 = fl4;
4451da177e4SLinus Torvalds out:	return ret;
4461da177e4SLinus Torvalds }
447