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 221da177e4SLinus Torvalds extern int sysctl_tcp_syncookies; 231da177e4SLinus Torvalds 24253ff516SFlorian Westphal static u32 syncookie_secret[2][16-4+SHA_DIGEST_WORDS] __read_mostly; 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #define COOKIEBITS 24 /* Upper bits store count */ 271da177e4SLinus Torvalds #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1) 281da177e4SLinus Torvalds 29274e2da0SFlorian Westphal /* TCP Timestamp: 6 lowest bits of timestamp sent in the cookie SYN-ACK 30274e2da0SFlorian Westphal * stores TCP options: 31274e2da0SFlorian Westphal * 32274e2da0SFlorian Westphal * MSB LSB 33274e2da0SFlorian Westphal * | 31 ... 6 | 5 | 4 | 3 2 1 0 | 34274e2da0SFlorian Westphal * | Timestamp | ECN | SACK | WScale | 35274e2da0SFlorian Westphal * 36274e2da0SFlorian Westphal * When we receive a valid cookie-ACK, we look at the echoed tsval (if 37274e2da0SFlorian Westphal * any) to figure out which TCP options we should use for the rebuilt 38274e2da0SFlorian Westphal * connection. 39274e2da0SFlorian Westphal * 40274e2da0SFlorian Westphal * A WScale setting of '0xf' (which is an invalid scaling value) 41274e2da0SFlorian Westphal * means that original syn did not include the TCP window scaling option. 42274e2da0SFlorian Westphal */ 43274e2da0SFlorian Westphal #define TS_OPT_WSCALE_MASK 0xf 44274e2da0SFlorian Westphal #define TS_OPT_SACK BIT(4) 45274e2da0SFlorian Westphal #define TS_OPT_ECN BIT(5) 46274e2da0SFlorian Westphal /* There is no TS_OPT_TIMESTAMP: 47274e2da0SFlorian Westphal * if ACK contains timestamp option, we already know it was 48274e2da0SFlorian Westphal * requested/supported by the syn/synack exchange. 49274e2da0SFlorian Westphal */ 50274e2da0SFlorian Westphal #define TSBITS 6 51274e2da0SFlorian Westphal #define TSMASK (((__u32)1 << TSBITS) - 1) 52274e2da0SFlorian Westphal 53245b2e70STejun Heo static DEFINE_PER_CPU(__u32 [16 + 5 + SHA_WORKSPACE_WORDS], 54245b2e70STejun Heo ipv4_cookie_scratch); 5511baab7aSEric Dumazet 56714e85beSAl Viro static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport, 571da177e4SLinus Torvalds u32 count, int c) 581da177e4SLinus Torvalds { 59b23a002fSHannes Frederic Sowa __u32 *tmp; 601da177e4SLinus Torvalds 61b23a002fSHannes Frederic Sowa net_get_random_once(syncookie_secret, sizeof(syncookie_secret)); 62b23a002fSHannes Frederic Sowa 63903ceff7SChristoph Lameter tmp = this_cpu_ptr(ipv4_cookie_scratch); 642051f11fSFlorian Westphal memcpy(tmp + 4, syncookie_secret[c], sizeof(syncookie_secret[c])); 65714e85beSAl Viro tmp[0] = (__force u32)saddr; 66714e85beSAl Viro tmp[1] = (__force u32)daddr; 67714e85beSAl Viro tmp[2] = ((__force u32)sport << 16) + (__force u32)dport; 681da177e4SLinus Torvalds tmp[3] = count; 691da177e4SLinus Torvalds sha_transform(tmp + 16, (__u8 *)tmp, tmp + 16 + 5); 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds return tmp[17]; 721da177e4SLinus Torvalds } 731da177e4SLinus Torvalds 744dfc2817SFlorian Westphal 754dfc2817SFlorian Westphal /* 764dfc2817SFlorian Westphal * when syncookies are in effect and tcp timestamps are enabled we encode 77734f614bSFlorian Westphal * tcp options in the lower bits of the timestamp value that will be 784dfc2817SFlorian Westphal * sent in the syn-ack. 794dfc2817SFlorian Westphal * Since subsequent timestamps use the normal tcp_time_stamp value, we 804dfc2817SFlorian Westphal * must make sure that the resulting initial timestamp is <= tcp_time_stamp. 814dfc2817SFlorian Westphal */ 824dfc2817SFlorian Westphal __u32 cookie_init_timestamp(struct request_sock *req) 834dfc2817SFlorian Westphal { 844dfc2817SFlorian Westphal struct inet_request_sock *ireq; 854dfc2817SFlorian Westphal u32 ts, ts_now = tcp_time_stamp; 864dfc2817SFlorian Westphal u32 options = 0; 874dfc2817SFlorian Westphal 884dfc2817SFlorian Westphal ireq = inet_rsk(req); 89734f614bSFlorian Westphal 90274e2da0SFlorian Westphal options = ireq->wscale_ok ? ireq->snd_wscale : TS_OPT_WSCALE_MASK; 91274e2da0SFlorian Westphal if (ireq->sack_ok) 92274e2da0SFlorian Westphal options |= TS_OPT_SACK; 93274e2da0SFlorian Westphal if (ireq->ecn_ok) 94274e2da0SFlorian Westphal options |= TS_OPT_ECN; 954dfc2817SFlorian Westphal 964dfc2817SFlorian Westphal ts = ts_now & ~TSMASK; 974dfc2817SFlorian Westphal ts |= options; 984dfc2817SFlorian Westphal if (ts > ts_now) { 994dfc2817SFlorian Westphal ts >>= TSBITS; 1004dfc2817SFlorian Westphal ts--; 1014dfc2817SFlorian Westphal ts <<= TSBITS; 1024dfc2817SFlorian Westphal ts |= options; 1034dfc2817SFlorian Westphal } 1044dfc2817SFlorian Westphal return ts; 1054dfc2817SFlorian Westphal } 1064dfc2817SFlorian Westphal 1074dfc2817SFlorian Westphal 108714e85beSAl Viro static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport, 1098c27bd75SFlorian Westphal __be16 dport, __u32 sseq, __u32 data) 1101da177e4SLinus Torvalds { 1111da177e4SLinus Torvalds /* 1121da177e4SLinus Torvalds * Compute the secure sequence number. 1131da177e4SLinus Torvalds * The output should be: 1141da177e4SLinus Torvalds * HASH(sec1,saddr,sport,daddr,dport,sec1) + sseq + (count * 2^24) 1151da177e4SLinus Torvalds * + (HASH(sec2,saddr,sport,daddr,dport,count,sec2) % 2^24). 1161da177e4SLinus Torvalds * Where sseq is their sequence number and count increases every 1171da177e4SLinus Torvalds * minute by 1. 1181da177e4SLinus Torvalds * As an extra hack, we add a small "data" value that encodes the 1191da177e4SLinus Torvalds * MSS into the second hash value. 1201da177e4SLinus Torvalds */ 1218c27bd75SFlorian Westphal u32 count = tcp_cookie_time(); 1221da177e4SLinus Torvalds return (cookie_hash(saddr, daddr, sport, dport, 0, 0) + 1231da177e4SLinus Torvalds sseq + (count << COOKIEBITS) + 1241da177e4SLinus Torvalds ((cookie_hash(saddr, daddr, sport, dport, count, 1) + data) 1251da177e4SLinus Torvalds & COOKIEMASK)); 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds /* 1291da177e4SLinus Torvalds * This retrieves the small "data" value from the syncookie. 1301da177e4SLinus Torvalds * If the syncookie is bad, the data returned will be out of 1311da177e4SLinus Torvalds * range. This must be checked by the caller. 1321da177e4SLinus Torvalds * 1338c27bd75SFlorian Westphal * The count value used to generate the cookie must be less than 1348c27bd75SFlorian Westphal * MAX_SYNCOOKIE_AGE minutes in the past. 1358c27bd75SFlorian Westphal * The return value (__u32)-1 if this test fails. 1361da177e4SLinus Torvalds */ 137714e85beSAl Viro static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr, 1388c27bd75SFlorian Westphal __be16 sport, __be16 dport, __u32 sseq) 1391da177e4SLinus Torvalds { 1408c27bd75SFlorian Westphal u32 diff, count = tcp_cookie_time(); 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds /* Strip away the layers from the cookie */ 1431da177e4SLinus Torvalds cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds /* Cookie is now reduced to (count * 2^24) ^ (hash % 2^24) */ 1461da177e4SLinus Torvalds diff = (count - (cookie >> COOKIEBITS)) & ((__u32) -1 >> COOKIEBITS); 1478c27bd75SFlorian Westphal if (diff >= MAX_SYNCOOKIE_AGE) 1481da177e4SLinus Torvalds return (__u32)-1; 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds return (cookie - 1511da177e4SLinus Torvalds cookie_hash(saddr, daddr, sport, dport, count - diff, 1)) 1521da177e4SLinus Torvalds & COOKIEMASK; /* Leaving the data behind */ 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds /* 15608629354SFlorian Westphal * MSS Values are chosen based on the 2011 paper 15708629354SFlorian Westphal * 'An Analysis of TCP Maximum Segement Sizes' by S. Alcock and R. Nelson. 15808629354SFlorian Westphal * Values .. 15908629354SFlorian Westphal * .. lower than 536 are rare (< 0.2%) 16008629354SFlorian Westphal * .. between 537 and 1299 account for less than < 1.5% of observed values 16108629354SFlorian Westphal * .. in the 1300-1349 range account for about 15 to 20% of observed mss values 16208629354SFlorian Westphal * .. exceeding 1460 are very rare (< 0.04%) 1635918e2fbSFlorian Westphal * 16408629354SFlorian Westphal * 1460 is the single most frequently announced mss value (30 to 46% depending 16508629354SFlorian Westphal * on monitor location). Table must be sorted. 1661da177e4SLinus Torvalds */ 1671da177e4SLinus Torvalds static __u16 const msstab[] = { 1685918e2fbSFlorian Westphal 536, 16908629354SFlorian Westphal 1300, 17008629354SFlorian Westphal 1440, /* 1440, 1452: PPPoE */ 1715918e2fbSFlorian Westphal 1460, 1721da177e4SLinus Torvalds }; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds /* 1751da177e4SLinus Torvalds * Generate a syncookie. mssp points to the mss, which is returned 1761da177e4SLinus Torvalds * rounded down to the value encoded in the cookie. 1771da177e4SLinus Torvalds */ 1780198230bSPatrick McHardy u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th, 1790198230bSPatrick McHardy u16 *mssp) 1801da177e4SLinus Torvalds { 1811da177e4SLinus Torvalds int mssind; 1821da177e4SLinus Torvalds const __u16 mss = *mssp; 1831da177e4SLinus Torvalds 1845918e2fbSFlorian Westphal for (mssind = ARRAY_SIZE(msstab) - 1; mssind ; mssind--) 1855918e2fbSFlorian Westphal if (mss >= msstab[mssind]) 1865918e2fbSFlorian Westphal break; 1875918e2fbSFlorian Westphal *mssp = msstab[mssind]; 1881da177e4SLinus Torvalds 189aa8223c7SArnaldo Carvalho de Melo return secure_tcp_syn_cookie(iph->saddr, iph->daddr, 190aa8223c7SArnaldo Carvalho de Melo th->source, th->dest, ntohl(th->seq), 1918c27bd75SFlorian Westphal mssind); 1921da177e4SLinus Torvalds } 1930198230bSPatrick McHardy EXPORT_SYMBOL_GPL(__cookie_v4_init_sequence); 1940198230bSPatrick McHardy 195*3f684b4bSEric Dumazet __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mssp) 1960198230bSPatrick McHardy { 1970198230bSPatrick McHardy const struct iphdr *iph = ip_hdr(skb); 1980198230bSPatrick McHardy const struct tcphdr *th = tcp_hdr(skb); 1990198230bSPatrick McHardy 2000198230bSPatrick McHardy return __cookie_v4_init_sequence(iph, th, mssp); 2010198230bSPatrick McHardy } 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /* 2041da177e4SLinus Torvalds * Check if a ack sequence number is a valid syncookie. 2051da177e4SLinus Torvalds * Return the decoded mss if it is, or 0 if not. 2061da177e4SLinus Torvalds */ 2070198230bSPatrick McHardy int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th, 2080198230bSPatrick McHardy u32 cookie) 2091da177e4SLinus Torvalds { 210aa8223c7SArnaldo Carvalho de Melo __u32 seq = ntohl(th->seq) - 1; 211aa8223c7SArnaldo Carvalho de Melo __u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr, 2128c27bd75SFlorian Westphal th->source, th->dest, seq); 2131da177e4SLinus Torvalds 2145918e2fbSFlorian Westphal return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0; 2151da177e4SLinus Torvalds } 2160198230bSPatrick McHardy EXPORT_SYMBOL_GPL(__cookie_v4_check); 2171da177e4SLinus Torvalds 218b80c0e78SEric Dumazet struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb, 21960236fddSArnaldo Carvalho de Melo struct request_sock *req, 2201da177e4SLinus Torvalds struct dst_entry *dst) 2211da177e4SLinus Torvalds { 2228292a17aSArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2231da177e4SLinus Torvalds struct sock *child; 2241da177e4SLinus Torvalds 2258292a17aSArnaldo Carvalho de Melo child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst); 2260470c8caSEric Dumazet if (child) { 2270470c8caSEric Dumazet atomic_set(&req->rsk_refcnt, 1); 228463c84b9SArnaldo Carvalho de Melo inet_csk_reqsk_queue_add(sk, req, child); 2290470c8caSEric Dumazet } else { 23060236fddSArnaldo Carvalho de Melo reqsk_free(req); 2310470c8caSEric Dumazet } 2321da177e4SLinus Torvalds return child; 2331da177e4SLinus Torvalds } 234b80c0e78SEric Dumazet EXPORT_SYMBOL(tcp_get_cookie_sock); 2354dfc2817SFlorian Westphal 2364dfc2817SFlorian Westphal /* 2374dfc2817SFlorian Westphal * when syncookies are in effect and tcp timestamps are enabled we stored 2384dfc2817SFlorian Westphal * additional tcp options in the timestamp. 2394dfc2817SFlorian Westphal * This extracts these options from the timestamp echo. 2404dfc2817SFlorian Westphal * 241f1673381SFlorian Westphal * return false if we decode a tcp option that is disabled 242f1673381SFlorian Westphal * on the host. 2434dfc2817SFlorian Westphal */ 244f1673381SFlorian Westphal bool cookie_timestamp_decode(struct tcp_options_received *tcp_opt) 2454dfc2817SFlorian Westphal { 246734f614bSFlorian Westphal /* echoed timestamp, lowest bits contain options */ 247274e2da0SFlorian Westphal u32 options = tcp_opt->rcv_tsecr; 2484dfc2817SFlorian Westphal 2498c763681SFlorian Westphal if (!tcp_opt->saw_tstamp) { 2508c763681SFlorian Westphal tcp_clear_options(tcp_opt); 2518c763681SFlorian Westphal return true; 2528c763681SFlorian Westphal } 2538c763681SFlorian Westphal 2548c763681SFlorian Westphal if (!sysctl_tcp_timestamps) 2558c763681SFlorian Westphal return false; 2568c763681SFlorian Westphal 257274e2da0SFlorian Westphal tcp_opt->sack_ok = (options & TS_OPT_SACK) ? TCP_SACK_SEEN : 0; 2584dfc2817SFlorian Westphal 2598c763681SFlorian Westphal if (tcp_opt->sack_ok && !sysctl_tcp_sack) 2608c763681SFlorian Westphal return false; 2614dfc2817SFlorian Westphal 262274e2da0SFlorian Westphal if ((options & TS_OPT_WSCALE_MASK) == TS_OPT_WSCALE_MASK) 263734f614bSFlorian Westphal return true; /* no window scaling */ 264734f614bSFlorian Westphal 2654dfc2817SFlorian Westphal tcp_opt->wscale_ok = 1; 266274e2da0SFlorian Westphal tcp_opt->snd_wscale = options & TS_OPT_WSCALE_MASK; 267274e2da0SFlorian Westphal 2688c763681SFlorian Westphal return sysctl_tcp_window_scaling != 0; 2698c763681SFlorian Westphal } 270f1673381SFlorian Westphal EXPORT_SYMBOL(cookie_timestamp_decode); 271f1673381SFlorian Westphal 272f1673381SFlorian Westphal bool cookie_ecn_ok(const struct tcp_options_received *tcp_opt, 273f7b3bec6SFlorian Westphal const struct net *net, const struct dst_entry *dst) 274f1673381SFlorian Westphal { 275f1673381SFlorian Westphal bool ecn_ok = tcp_opt->rcv_tsecr & TS_OPT_ECN; 276f1673381SFlorian Westphal 277f1673381SFlorian Westphal if (!ecn_ok) 278f1673381SFlorian Westphal return false; 279f1673381SFlorian Westphal 280f1673381SFlorian Westphal if (net->ipv4.sysctl_tcp_ecn) 281f1673381SFlorian Westphal return true; 282f1673381SFlorian Westphal 283f7b3bec6SFlorian Westphal return dst_feature(dst, RTAX_FEATURE_ECN); 284f1673381SFlorian Westphal } 285f1673381SFlorian Westphal EXPORT_SYMBOL(cookie_ecn_ok); 2864dfc2817SFlorian Westphal 287461b74c3SCong Wang struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) 2881da177e4SLinus Torvalds { 289461b74c3SCong Wang struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt; 2904957faadSWilliam Allen Simpson struct tcp_options_received tcp_opt; 2912e6599cbSArnaldo Carvalho de Melo struct inet_request_sock *ireq; 2922e6599cbSArnaldo Carvalho de Melo struct tcp_request_sock *treq; 2931da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 294aa8223c7SArnaldo Carvalho de Melo const struct tcphdr *th = tcp_hdr(skb); 295aa8223c7SArnaldo Carvalho de Melo __u32 cookie = ntohl(th->ack_seq) - 1; 2961da177e4SLinus Torvalds struct sock *ret = sk; 29760236fddSArnaldo Carvalho de Melo struct request_sock *req; 2981da177e4SLinus Torvalds int mss; 2991da177e4SLinus Torvalds struct rtable *rt; 3001da177e4SLinus Torvalds __u8 rcv_wscale; 301dfd25fffSEric Dumazet struct flowi4 fl4; 3021da177e4SLinus Torvalds 303af9b4738SFlorian Westphal if (!sysctl_tcp_syncookies || !th->ack || th->rst) 3041da177e4SLinus Torvalds goto out; 3051da177e4SLinus Torvalds 306646697b9SFlorian Westphal if (tcp_synq_no_recent_overflow(sk)) 307646697b9SFlorian Westphal goto out; 308646697b9SFlorian Westphal 309646697b9SFlorian Westphal mss = __cookie_v4_check(ip_hdr(skb), th, cookie); 310646697b9SFlorian Westphal if (mss == 0) { 311de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED); 3121da177e4SLinus Torvalds goto out; 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 315de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESRECV); 3161da177e4SLinus Torvalds 317bb5b7c11SDavid S. Miller /* check for timestamp cookie support */ 318bb5b7c11SDavid S. Miller memset(&tcp_opt, 0, sizeof(tcp_opt)); 3191a2c6181SChristoph Paasch tcp_parse_options(skb, &tcp_opt, 0, NULL); 320bb5b7c11SDavid S. Miller 321f1673381SFlorian Westphal if (!cookie_timestamp_decode(&tcp_opt)) 3228c763681SFlorian Westphal goto out; 323bb5b7c11SDavid S. Miller 3241da177e4SLinus Torvalds ret = NULL; 325407640deSEric Dumazet req = inet_reqsk_alloc(&tcp_request_sock_ops, sk); /* for safety */ 3261da177e4SLinus Torvalds if (!req) 3271da177e4SLinus Torvalds goto out; 3281da177e4SLinus Torvalds 3292e6599cbSArnaldo Carvalho de Melo ireq = inet_rsk(req); 3302e6599cbSArnaldo Carvalho de Melo treq = tcp_rsk(req); 331aa8223c7SArnaldo Carvalho de Melo treq->rcv_isn = ntohl(th->seq) - 1; 3322e6599cbSArnaldo Carvalho de Melo treq->snt_isn = cookie; 3331da177e4SLinus Torvalds req->mss = mss; 334b44084c2SEric Dumazet ireq->ir_num = ntohs(th->dest); 335634fb979SEric Dumazet ireq->ir_rmt_port = th->source; 33608d2cc3bSEric Dumazet sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr); 33708d2cc3bSEric Dumazet sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr); 33884f39b08SLorenzo Colitti ireq->ir_mark = inet_request_mark(sk, skb); 339bb5b7c11SDavid S. Miller ireq->snd_wscale = tcp_opt.snd_wscale; 340bb5b7c11SDavid S. Miller ireq->sack_ok = tcp_opt.sack_ok; 341bb5b7c11SDavid S. Miller ireq->wscale_ok = tcp_opt.wscale_ok; 342bb5b7c11SDavid S. Miller ireq->tstamp_ok = tcp_opt.saw_tstamp; 343bb5b7c11SDavid S. Miller req->ts_recent = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval : 0; 3440f1c28aeSYuchung Cheng treq->snt_synack.v64 = 0; 3459439ce00SEric Dumazet treq->tfo_listener = false; 3461da177e4SLinus Torvalds 34716f86165SEric Dumazet ireq->ir_iif = sk->sk_bound_dev_if; 34816f86165SEric Dumazet 3491da177e4SLinus Torvalds /* We throwed the options of the initial SYN away, so we hope 3501da177e4SLinus Torvalds * the ACK carries the same options again (see RFC1122 4.2.3.8) 3511da177e4SLinus Torvalds */ 352e25f866fSCong Wang ireq->opt = tcp_v4_save_options(skb); 3531da177e4SLinus Torvalds 354284904aaSPaul Moore if (security_inet_conn_request(sk, skb, req)) { 3550470c8caSEric Dumazet reqsk_free(req); 356284904aaSPaul Moore goto out; 357284904aaSPaul Moore } 358284904aaSPaul Moore 359e6c022a4SEric Dumazet req->num_retrans = 0; 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds /* 3621da177e4SLinus Torvalds * We need to lookup the route here to get at the correct 3631da177e4SLinus Torvalds * window size. We should better make sure that the window size 3641da177e4SLinus Torvalds * hasn't changed since we received the original syn, but I see 3651da177e4SLinus Torvalds * no easy way to do this. 3661da177e4SLinus Torvalds */ 36784f39b08SLorenzo Colitti flowi4_init_output(&fl4, sk->sk_bound_dev_if, ireq->ir_mark, 368d66954a0SDmitry Popov RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, IPPROTO_TCP, 3691bba6ffeSDavid S. Miller inet_sk_flowi_flags(sk), 370461b74c3SCong Wang opt->srr ? opt->faddr : ireq->ir_rmt_addr, 371634fb979SEric Dumazet ireq->ir_loc_addr, th->source, th->dest); 3729d6ec938SDavid S. Miller security_req_classify_flow(req, flowi4_to_flowi(&fl4)); 3739d6ec938SDavid S. Miller rt = ip_route_output_key(sock_net(sk), &fl4); 374b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 3750470c8caSEric Dumazet reqsk_free(req); 3761da177e4SLinus Torvalds goto out; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds /* Try to redo what tcp_v4_send_synack did. */ 380d8d1f30bSChangli Gao req->window_clamp = tp->window_clamp ? :dst_metric(&rt->dst, RTAX_WINDOW); 3814dfc2817SFlorian Westphal 3821da177e4SLinus Torvalds tcp_select_initial_window(tcp_full_space(sk), req->mss, 3831da177e4SLinus Torvalds &req->rcv_wnd, &req->window_clamp, 38431d12926Slaurent chavey ireq->wscale_ok, &rcv_wscale, 385d8d1f30bSChangli Gao dst_metric(&rt->dst, RTAX_INITRWND)); 3864dfc2817SFlorian Westphal 3872e6599cbSArnaldo Carvalho de Melo ireq->rcv_wscale = rcv_wscale; 388f7b3bec6SFlorian Westphal ireq->ecn_ok = cookie_ecn_ok(&tcp_opt, sock_net(sk), &rt->dst); 3891da177e4SLinus Torvalds 390b80c0e78SEric Dumazet ret = tcp_get_cookie_sock(sk, skb, req, &rt->dst); 391dfd25fffSEric Dumazet /* ip_queue_xmit() depends on our flow being setup 392dfd25fffSEric Dumazet * Normal sockets get it right from inet_csk_route_child_sock() 393dfd25fffSEric Dumazet */ 394dfd25fffSEric Dumazet if (ret) 395dfd25fffSEric Dumazet inet_sk(ret)->cork.fl.u.ip4 = fl4; 3961da177e4SLinus Torvalds out: return ret; 3971da177e4SLinus Torvalds } 398