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> 10fe62d05bSJason A. Donenfeld #include <linux/siphash.h> 111da177e4SLinus Torvalds #include <linux/kernel.h> 12bc3b2d7fSPaul Gortmaker #include <linux/export.h> 1384b114b9SEric Dumazet #include <net/secure_seq.h> 141da177e4SLinus Torvalds #include <net/tcp.h> 1586b08d86SKOVACS Krisztian #include <net/route.h> 161da177e4SLinus Torvalds 1749ecc2e9SEric Dumazet static siphash_aligned_key_t syncookie_secret[2]; 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #define COOKIEBITS 24 /* Upper bits store count */ 201da177e4SLinus Torvalds #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1) 211da177e4SLinus Torvalds 22274e2da0SFlorian Westphal /* TCP Timestamp: 6 lowest bits of timestamp sent in the cookie SYN-ACK 23274e2da0SFlorian Westphal * stores TCP options: 24274e2da0SFlorian Westphal * 25274e2da0SFlorian Westphal * MSB LSB 26274e2da0SFlorian Westphal * | 31 ... 6 | 5 | 4 | 3 2 1 0 | 27274e2da0SFlorian Westphal * | Timestamp | ECN | SACK | WScale | 28274e2da0SFlorian Westphal * 29274e2da0SFlorian Westphal * When we receive a valid cookie-ACK, we look at the echoed tsval (if 30274e2da0SFlorian Westphal * any) to figure out which TCP options we should use for the rebuilt 31274e2da0SFlorian Westphal * connection. 32274e2da0SFlorian Westphal * 33274e2da0SFlorian Westphal * A WScale setting of '0xf' (which is an invalid scaling value) 34274e2da0SFlorian Westphal * means that original syn did not include the TCP window scaling option. 35274e2da0SFlorian Westphal */ 36274e2da0SFlorian Westphal #define TS_OPT_WSCALE_MASK 0xf 37274e2da0SFlorian Westphal #define TS_OPT_SACK BIT(4) 38274e2da0SFlorian Westphal #define TS_OPT_ECN BIT(5) 39274e2da0SFlorian Westphal /* There is no TS_OPT_TIMESTAMP: 40274e2da0SFlorian Westphal * if ACK contains timestamp option, we already know it was 41274e2da0SFlorian Westphal * requested/supported by the syn/synack exchange. 42274e2da0SFlorian Westphal */ 43274e2da0SFlorian Westphal #define TSBITS 6 44274e2da0SFlorian Westphal 45714e85beSAl Viro static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport, 461da177e4SLinus Torvalds u32 count, int c) 471da177e4SLinus Torvalds { 48b23a002fSHannes Frederic Sowa net_get_random_once(syncookie_secret, sizeof(syncookie_secret)); 49fe62d05bSJason A. Donenfeld return siphash_4u32((__force u32)saddr, (__force u32)daddr, 50fe62d05bSJason A. Donenfeld (__force u32)sport << 16 | (__force u32)dport, 51fe62d05bSJason A. Donenfeld count, &syncookie_secret[c]); 521da177e4SLinus Torvalds } 531da177e4SLinus Torvalds 54003e07a1SEric Dumazet /* Convert one nsec 64bit timestamp to ts (ms or usec resolution) */ 55003e07a1SEric Dumazet static u64 tcp_ns_to_ts(bool usec_ts, u64 val) 56003e07a1SEric Dumazet { 57003e07a1SEric Dumazet if (usec_ts) 58003e07a1SEric Dumazet return div_u64(val, NSEC_PER_USEC); 59003e07a1SEric Dumazet 60003e07a1SEric Dumazet return div_u64(val, NSEC_PER_MSEC); 61003e07a1SEric Dumazet } 624dfc2817SFlorian Westphal 634dfc2817SFlorian Westphal /* 644dfc2817SFlorian Westphal * when syncookies are in effect and tcp timestamps are enabled we encode 65734f614bSFlorian Westphal * tcp options in the lower bits of the timestamp value that will be 664dfc2817SFlorian Westphal * sent in the syn-ack. 674dfc2817SFlorian Westphal * Since subsequent timestamps use the normal tcp_time_stamp value, we 684dfc2817SFlorian Westphal * must make sure that the resulting initial timestamp is <= tcp_time_stamp. 694dfc2817SFlorian Westphal */ 70200ecef6SEric Dumazet u64 cookie_init_timestamp(struct request_sock *req, u64 now) 714dfc2817SFlorian Westphal { 7273ed8e03SEric Dumazet const struct inet_request_sock *ireq = inet_rsk(req); 73003e07a1SEric Dumazet u64 ts, ts_now = tcp_ns_to_ts(false, now); 744dfc2817SFlorian Westphal u32 options = 0; 754dfc2817SFlorian Westphal 76274e2da0SFlorian Westphal options = ireq->wscale_ok ? ireq->snd_wscale : TS_OPT_WSCALE_MASK; 77274e2da0SFlorian Westphal if (ireq->sack_ok) 78274e2da0SFlorian Westphal options |= TS_OPT_SACK; 79274e2da0SFlorian Westphal if (ireq->ecn_ok) 80274e2da0SFlorian Westphal options |= TS_OPT_ECN; 814dfc2817SFlorian Westphal 8273ed8e03SEric Dumazet ts = (ts_now >> TSBITS) << TSBITS; 834dfc2817SFlorian Westphal ts |= options; 8473ed8e03SEric Dumazet if (ts > ts_now) 8573ed8e03SEric Dumazet ts -= (1UL << TSBITS); 8673ed8e03SEric Dumazet 87614e8316SEric Dumazet if (tcp_rsk(req)->req_usec_ts) 88614e8316SEric Dumazet return ts * NSEC_PER_USEC; 89614e8316SEric Dumazet return ts * NSEC_PER_MSEC; 904dfc2817SFlorian Westphal } 914dfc2817SFlorian Westphal 924dfc2817SFlorian Westphal 93714e85beSAl Viro static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport, 948c27bd75SFlorian Westphal __be16 dport, __u32 sseq, __u32 data) 951da177e4SLinus Torvalds { 961da177e4SLinus Torvalds /* 971da177e4SLinus Torvalds * Compute the secure sequence number. 981da177e4SLinus Torvalds * The output should be: 991da177e4SLinus Torvalds * HASH(sec1,saddr,sport,daddr,dport,sec1) + sseq + (count * 2^24) 1001da177e4SLinus Torvalds * + (HASH(sec2,saddr,sport,daddr,dport,count,sec2) % 2^24). 1011da177e4SLinus Torvalds * Where sseq is their sequence number and count increases every 1021da177e4SLinus Torvalds * minute by 1. 1031da177e4SLinus Torvalds * As an extra hack, we add a small "data" value that encodes the 1041da177e4SLinus Torvalds * MSS into the second hash value. 1051da177e4SLinus Torvalds */ 1068c27bd75SFlorian Westphal u32 count = tcp_cookie_time(); 1071da177e4SLinus Torvalds return (cookie_hash(saddr, daddr, sport, dport, 0, 0) + 1081da177e4SLinus Torvalds sseq + (count << COOKIEBITS) + 1091da177e4SLinus Torvalds ((cookie_hash(saddr, daddr, sport, dport, count, 1) + data) 1101da177e4SLinus Torvalds & COOKIEMASK)); 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /* 1141da177e4SLinus Torvalds * This retrieves the small "data" value from the syncookie. 1151da177e4SLinus Torvalds * If the syncookie is bad, the data returned will be out of 1161da177e4SLinus Torvalds * range. This must be checked by the caller. 1171da177e4SLinus Torvalds * 1188c27bd75SFlorian Westphal * The count value used to generate the cookie must be less than 1198c27bd75SFlorian Westphal * MAX_SYNCOOKIE_AGE minutes in the past. 1208c27bd75SFlorian Westphal * The return value (__u32)-1 if this test fails. 1211da177e4SLinus Torvalds */ 122714e85beSAl Viro static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr, 1238c27bd75SFlorian Westphal __be16 sport, __be16 dport, __u32 sseq) 1241da177e4SLinus Torvalds { 1258c27bd75SFlorian Westphal u32 diff, count = tcp_cookie_time(); 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); 1328c27bd75SFlorian Westphal if (diff >= MAX_SYNCOOKIE_AGE) 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 /* 14108629354SFlorian Westphal * MSS Values are chosen based on the 2011 paper 14208629354SFlorian Westphal * 'An Analysis of TCP Maximum Segement Sizes' by S. Alcock and R. Nelson. 14308629354SFlorian Westphal * Values .. 14408629354SFlorian Westphal * .. lower than 536 are rare (< 0.2%) 14508629354SFlorian Westphal * .. between 537 and 1299 account for less than < 1.5% of observed values 14608629354SFlorian Westphal * .. in the 1300-1349 range account for about 15 to 20% of observed mss values 14708629354SFlorian Westphal * .. exceeding 1460 are very rare (< 0.04%) 1485918e2fbSFlorian Westphal * 14908629354SFlorian Westphal * 1460 is the single most frequently announced mss value (30 to 46% depending 15008629354SFlorian Westphal * on monitor location). Table must be sorted. 1511da177e4SLinus Torvalds */ 1521da177e4SLinus Torvalds static __u16 const msstab[] = { 1535918e2fbSFlorian Westphal 536, 15408629354SFlorian Westphal 1300, 15508629354SFlorian Westphal 1440, /* 1440, 1452: PPPoE */ 1565918e2fbSFlorian Westphal 1460, 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 */ 1630198230bSPatrick McHardy u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th, 1640198230bSPatrick McHardy u16 *mssp) 1651da177e4SLinus Torvalds { 1661da177e4SLinus Torvalds int mssind; 1671da177e4SLinus Torvalds const __u16 mss = *mssp; 1681da177e4SLinus Torvalds 1695918e2fbSFlorian Westphal for (mssind = ARRAY_SIZE(msstab) - 1; mssind ; mssind--) 1705918e2fbSFlorian Westphal if (mss >= msstab[mssind]) 1715918e2fbSFlorian Westphal break; 1725918e2fbSFlorian Westphal *mssp = msstab[mssind]; 1731da177e4SLinus Torvalds 174aa8223c7SArnaldo Carvalho de Melo return secure_tcp_syn_cookie(iph->saddr, iph->daddr, 175aa8223c7SArnaldo Carvalho de Melo th->source, th->dest, ntohl(th->seq), 1768c27bd75SFlorian Westphal mssind); 1771da177e4SLinus Torvalds } 1780198230bSPatrick McHardy EXPORT_SYMBOL_GPL(__cookie_v4_init_sequence); 1790198230bSPatrick McHardy 1803f684b4bSEric Dumazet __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mssp) 1810198230bSPatrick McHardy { 1820198230bSPatrick McHardy const struct iphdr *iph = ip_hdr(skb); 1830198230bSPatrick McHardy const struct tcphdr *th = tcp_hdr(skb); 1840198230bSPatrick McHardy 1850198230bSPatrick McHardy return __cookie_v4_init_sequence(iph, th, mssp); 1860198230bSPatrick McHardy } 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds /* 1891da177e4SLinus Torvalds * Check if a ack sequence number is a valid syncookie. 1901da177e4SLinus Torvalds * Return the decoded mss if it is, or 0 if not. 1911da177e4SLinus Torvalds */ 1927577bc82SKuniyuki Iwashima int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th) 1931da177e4SLinus Torvalds { 1947577bc82SKuniyuki Iwashima __u32 cookie = ntohl(th->ack_seq) - 1; 195aa8223c7SArnaldo Carvalho de Melo __u32 seq = ntohl(th->seq) - 1; 1967577bc82SKuniyuki Iwashima __u32 mssind; 1977577bc82SKuniyuki Iwashima 1987577bc82SKuniyuki Iwashima 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, 207efce3d1fSKuniyuki Iwashima struct dst_entry *dst) 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); 2176bcfd7f8SEric Dumazet sock_rps_save_rxhash(child, skb); 2189466a1ccSFlorian Westphal 2190e8642cfSEric Dumazet if (rsk_drop_req(req)) { 2209d8c05adSPaolo Abeni reqsk_put(req); 2219466a1ccSFlorian Westphal return child; 2229466a1ccSFlorian Westphal } 2239466a1ccSFlorian Westphal 2249403cf23SGuillaume Nault if (inet_csk_reqsk_queue_add(sk, req, child)) 2259403cf23SGuillaume Nault return child; 2269403cf23SGuillaume Nault 2279d3e1368SGuillaume Nault bh_unlock_sock(child); 2289d3e1368SGuillaume Nault sock_put(child); 2299d3e1368SGuillaume Nault } 2309403cf23SGuillaume Nault __reqsk_free(req); 2319403cf23SGuillaume Nault 2329403cf23SGuillaume Nault return NULL; 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 */ 244f9301034SEric Dumazet bool cookie_timestamp_decode(const struct net *net, 245f9301034SEric Dumazet struct tcp_options_received *tcp_opt) 2464dfc2817SFlorian Westphal { 247734f614bSFlorian Westphal /* echoed timestamp, lowest bits contain options */ 248274e2da0SFlorian Westphal u32 options = tcp_opt->rcv_tsecr; 2494dfc2817SFlorian Westphal 2508c763681SFlorian Westphal if (!tcp_opt->saw_tstamp) { 2518c763681SFlorian Westphal tcp_clear_options(tcp_opt); 2528c763681SFlorian Westphal return true; 2538c763681SFlorian Westphal } 2548c763681SFlorian Westphal 2553666f666SKuniyuki Iwashima if (!READ_ONCE(net->ipv4.sysctl_tcp_timestamps)) 2568c763681SFlorian Westphal return false; 2578c763681SFlorian Westphal 258274e2da0SFlorian Westphal tcp_opt->sack_ok = (options & TS_OPT_SACK) ? TCP_SACK_SEEN : 0; 2594dfc2817SFlorian Westphal 2603666f666SKuniyuki Iwashima if (tcp_opt->sack_ok && !READ_ONCE(net->ipv4.sysctl_tcp_sack)) 2618c763681SFlorian Westphal return false; 2624dfc2817SFlorian Westphal 263274e2da0SFlorian Westphal if ((options & TS_OPT_WSCALE_MASK) == TS_OPT_WSCALE_MASK) 264734f614bSFlorian Westphal return true; /* no window scaling */ 265734f614bSFlorian Westphal 2664dfc2817SFlorian Westphal tcp_opt->wscale_ok = 1; 267274e2da0SFlorian Westphal tcp_opt->snd_wscale = options & TS_OPT_WSCALE_MASK; 268274e2da0SFlorian Westphal 2693666f666SKuniyuki Iwashima return READ_ONCE(net->ipv4.sysctl_tcp_window_scaling) != 0; 2708c763681SFlorian Westphal } 271f1673381SFlorian Westphal EXPORT_SYMBOL(cookie_timestamp_decode); 272f1673381SFlorian Westphal 273de5626b9SKuniyuki Iwashima static int cookie_tcp_reqsk_init(struct sock *sk, struct sk_buff *skb, 274de5626b9SKuniyuki Iwashima struct request_sock *req) 275de5626b9SKuniyuki Iwashima { 276de5626b9SKuniyuki Iwashima struct inet_request_sock *ireq = inet_rsk(req); 277de5626b9SKuniyuki Iwashima struct tcp_request_sock *treq = tcp_rsk(req); 278de5626b9SKuniyuki Iwashima const struct tcphdr *th = tcp_hdr(skb); 279de5626b9SKuniyuki Iwashima 280de5626b9SKuniyuki Iwashima req->num_retrans = 0; 281de5626b9SKuniyuki Iwashima 282de5626b9SKuniyuki Iwashima ireq->ir_num = ntohs(th->dest); 283de5626b9SKuniyuki Iwashima ireq->ir_rmt_port = th->source; 284de5626b9SKuniyuki Iwashima ireq->ir_iif = inet_request_bound_dev_if(sk, skb); 285de5626b9SKuniyuki Iwashima ireq->ir_mark = inet_request_mark(sk, skb); 286de5626b9SKuniyuki Iwashima 287de5626b9SKuniyuki Iwashima if (IS_ENABLED(CONFIG_SMC)) 288de5626b9SKuniyuki Iwashima ireq->smc_ok = 0; 289de5626b9SKuniyuki Iwashima 290de5626b9SKuniyuki Iwashima treq->snt_synack = 0; 291de5626b9SKuniyuki Iwashima treq->tfo_listener = false; 292de5626b9SKuniyuki Iwashima treq->txhash = net_tx_rndhash(); 293de5626b9SKuniyuki Iwashima treq->rcv_isn = ntohl(th->seq) - 1; 294de5626b9SKuniyuki Iwashima treq->snt_isn = ntohl(th->ack_seq) - 1; 295de5626b9SKuniyuki Iwashima treq->syn_tos = TCP_SKB_CB(skb)->ip_dsfield; 296de5626b9SKuniyuki Iwashima treq->req_usec_ts = false; 297de5626b9SKuniyuki Iwashima 298de5626b9SKuniyuki Iwashima #if IS_ENABLED(CONFIG_MPTCP) 299de5626b9SKuniyuki Iwashima treq->is_mptcp = sk_is_mptcp(sk); 300de5626b9SKuniyuki Iwashima if (treq->is_mptcp) 301de5626b9SKuniyuki Iwashima return mptcp_subflow_init_cookie_req(req, sk, skb); 302de5626b9SKuniyuki Iwashima #endif 303de5626b9SKuniyuki Iwashima 304de5626b9SKuniyuki Iwashima return 0; 305de5626b9SKuniyuki Iwashima } 306de5626b9SKuniyuki Iwashima 3076fc8c827SFlorian Westphal struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops, 308*8e7bab6bSKuniyuki Iwashima struct sock *sk, struct sk_buff *skb, 309*8e7bab6bSKuniyuki Iwashima struct tcp_options_received *tcp_opt, 310*8e7bab6bSKuniyuki Iwashima int mss, u32 tsoff) 3116fc8c827SFlorian Westphal { 312*8e7bab6bSKuniyuki Iwashima struct inet_request_sock *ireq; 313*8e7bab6bSKuniyuki Iwashima struct tcp_request_sock *treq; 3146fc8c827SFlorian Westphal struct request_sock *req; 3156fc8c827SFlorian Westphal 3166fc8c827SFlorian Westphal if (sk_is_mptcp(sk)) 3173fff8818SMatthieu Baerts req = mptcp_subflow_reqsk_alloc(ops, sk, false); 3183fff8818SMatthieu Baerts else 3196fc8c827SFlorian Westphal req = inet_reqsk_alloc(ops, sk, false); 3203fff8818SMatthieu Baerts 3216fc8c827SFlorian Westphal if (!req) 3226fc8c827SFlorian Westphal return NULL; 3236fc8c827SFlorian Westphal 324de5626b9SKuniyuki Iwashima if (cookie_tcp_reqsk_init(sk, skb, req)) { 3256fc8c827SFlorian Westphal reqsk_free(req); 3266fc8c827SFlorian Westphal return NULL; 3276fc8c827SFlorian Westphal } 3286fc8c827SFlorian Westphal 329*8e7bab6bSKuniyuki Iwashima ireq = inet_rsk(req); 330*8e7bab6bSKuniyuki Iwashima treq = tcp_rsk(req); 331*8e7bab6bSKuniyuki Iwashima 332*8e7bab6bSKuniyuki Iwashima req->mss = mss; 333*8e7bab6bSKuniyuki Iwashima req->ts_recent = tcp_opt->saw_tstamp ? tcp_opt->rcv_tsval : 0; 334*8e7bab6bSKuniyuki Iwashima 335*8e7bab6bSKuniyuki Iwashima ireq->snd_wscale = tcp_opt->snd_wscale; 336*8e7bab6bSKuniyuki Iwashima ireq->tstamp_ok = tcp_opt->saw_tstamp; 337*8e7bab6bSKuniyuki Iwashima ireq->sack_ok = tcp_opt->sack_ok; 338*8e7bab6bSKuniyuki Iwashima ireq->wscale_ok = tcp_opt->wscale_ok; 339*8e7bab6bSKuniyuki Iwashima ireq->ecn_ok = !!(tcp_opt->rcv_tsecr & TS_OPT_ECN); 340*8e7bab6bSKuniyuki Iwashima 341*8e7bab6bSKuniyuki Iwashima treq->ts_off = tsoff; 342*8e7bab6bSKuniyuki Iwashima 3436fc8c827SFlorian Westphal return req; 3446fc8c827SFlorian Westphal } 3456fc8c827SFlorian Westphal EXPORT_SYMBOL_GPL(cookie_tcp_reqsk_alloc); 3466fc8c827SFlorian Westphal 347*8e7bab6bSKuniyuki Iwashima static struct request_sock *cookie_tcp_check(struct net *net, struct sock *sk, 348*8e7bab6bSKuniyuki Iwashima struct sk_buff *skb) 3491da177e4SLinus Torvalds { 35034efc9cfSKuniyuki Iwashima struct tcp_options_received tcp_opt; 35184b114b9SEric Dumazet u32 tsoff = 0; 352*8e7bab6bSKuniyuki Iwashima int mss; 3531da177e4SLinus Torvalds 354646697b9SFlorian Westphal if (tcp_synq_no_recent_overflow(sk)) 355646697b9SFlorian Westphal goto out; 356646697b9SFlorian Westphal 357*8e7bab6bSKuniyuki Iwashima mss = __cookie_v4_check(ip_hdr(skb), tcp_hdr(skb)); 358*8e7bab6bSKuniyuki Iwashima if (!mss) { 35945c28509SKuniyuki Iwashima __NET_INC_STATS(net, LINUX_MIB_SYNCOOKIESFAILED); 3601da177e4SLinus Torvalds goto out; 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 36345c28509SKuniyuki Iwashima __NET_INC_STATS(net, LINUX_MIB_SYNCOOKIESRECV); 3641da177e4SLinus Torvalds 365bb5b7c11SDavid S. Miller /* check for timestamp cookie support */ 366bb5b7c11SDavid S. Miller memset(&tcp_opt, 0, sizeof(tcp_opt)); 36745c28509SKuniyuki Iwashima tcp_parse_options(net, skb, &tcp_opt, 0, NULL); 368bb5b7c11SDavid S. Miller 36984b114b9SEric Dumazet if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) { 37045c28509SKuniyuki Iwashima tsoff = secure_tcp_ts_off(net, 3715d2ed052SEric Dumazet ip_hdr(skb)->daddr, 3725d2ed052SEric Dumazet ip_hdr(skb)->saddr); 37384b114b9SEric Dumazet tcp_opt.rcv_tsecr -= tsoff; 37484b114b9SEric Dumazet } 37584b114b9SEric Dumazet 37645c28509SKuniyuki Iwashima if (!cookie_timestamp_decode(net, &tcp_opt)) 3778c763681SFlorian Westphal goto out; 378bb5b7c11SDavid S. Miller 379*8e7bab6bSKuniyuki Iwashima return cookie_tcp_reqsk_alloc(&tcp_request_sock_ops, sk, skb, 380*8e7bab6bSKuniyuki Iwashima &tcp_opt, mss, tsoff); 381*8e7bab6bSKuniyuki Iwashima out: 382*8e7bab6bSKuniyuki Iwashima return ERR_PTR(-EINVAL); 383*8e7bab6bSKuniyuki Iwashima } 384*8e7bab6bSKuniyuki Iwashima 385*8e7bab6bSKuniyuki Iwashima /* On input, sk is a listener. 386*8e7bab6bSKuniyuki Iwashima * Output is listener if incoming packet would not create a child 387*8e7bab6bSKuniyuki Iwashima * NULL if memory could not be allocated. 388*8e7bab6bSKuniyuki Iwashima */ 389*8e7bab6bSKuniyuki Iwashima struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) 390*8e7bab6bSKuniyuki Iwashima { 391*8e7bab6bSKuniyuki Iwashima struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt; 392*8e7bab6bSKuniyuki Iwashima const struct tcphdr *th = tcp_hdr(skb); 393*8e7bab6bSKuniyuki Iwashima struct tcp_sock *tp = tcp_sk(sk); 394*8e7bab6bSKuniyuki Iwashima struct inet_request_sock *ireq; 395*8e7bab6bSKuniyuki Iwashima struct net *net = sock_net(sk); 396*8e7bab6bSKuniyuki Iwashima struct request_sock *req; 397*8e7bab6bSKuniyuki Iwashima struct sock *ret = sk; 398*8e7bab6bSKuniyuki Iwashima struct flowi4 fl4; 399*8e7bab6bSKuniyuki Iwashima struct rtable *rt; 400*8e7bab6bSKuniyuki Iwashima __u8 rcv_wscale; 401*8e7bab6bSKuniyuki Iwashima int full_space; 402*8e7bab6bSKuniyuki Iwashima 403*8e7bab6bSKuniyuki Iwashima if (!READ_ONCE(net->ipv4.sysctl_tcp_syncookies) || 404*8e7bab6bSKuniyuki Iwashima !th->ack || th->rst) 405*8e7bab6bSKuniyuki Iwashima goto out; 406*8e7bab6bSKuniyuki Iwashima 407*8e7bab6bSKuniyuki Iwashima req = cookie_tcp_check(net, sk, skb); 408*8e7bab6bSKuniyuki Iwashima if (IS_ERR(req)) 409*8e7bab6bSKuniyuki Iwashima goto out; 4101da177e4SLinus Torvalds if (!req) 41150468cddSKuniyuki Iwashima goto out_drop; 4121da177e4SLinus Torvalds 4132e6599cbSArnaldo Carvalho de Melo ireq = inet_rsk(req); 414*8e7bab6bSKuniyuki Iwashima 41508d2cc3bSEric Dumazet sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr); 41608d2cc3bSEric Dumazet sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr); 41716f86165SEric Dumazet 4181da177e4SLinus Torvalds /* We throwed the options of the initial SYN away, so we hope 4191da177e4SLinus Torvalds * the ACK carries the same options again (see RFC1122 4.2.3.8) 4201da177e4SLinus Torvalds */ 42145c28509SKuniyuki Iwashima RCU_INIT_POINTER(ireq->ireq_opt, tcp_v4_save_options(net, skb)); 4221da177e4SLinus Torvalds 42350468cddSKuniyuki Iwashima if (security_inet_conn_request(sk, skb, req)) 42450468cddSKuniyuki Iwashima goto out_free; 425284904aaSPaul Moore 4267b0f570fSKuniyuki Iwashima tcp_ao_syncookie(sk, skb, req, AF_INET); 4277b0f570fSKuniyuki Iwashima 4281da177e4SLinus Torvalds /* 4291da177e4SLinus Torvalds * We need to lookup the route here to get at the correct 4301da177e4SLinus Torvalds * window size. We should better make sure that the window size 4311da177e4SLinus Torvalds * hasn't changed since we received the original syn, but I see 4321da177e4SLinus Torvalds * no easy way to do this. 4331da177e4SLinus Torvalds */ 4346dd9a14eSDavid Ahern flowi4_init_output(&fl4, ireq->ir_iif, ireq->ir_mark, 4356f8a76f8SGuillaume Nault ip_sock_rt_tos(sk), ip_sock_rt_scope(sk), 4366f8a76f8SGuillaume Nault IPPROTO_TCP, inet_sk_flowi_flags(sk), 437461b74c3SCong Wang opt->srr ? opt->faddr : ireq->ir_rmt_addr, 438e2d118a1SLorenzo Colitti ireq->ir_loc_addr, th->source, th->dest, sk->sk_uid); 4393df98d79SPaul Moore security_req_classify_flow(req, flowi4_to_flowi_common(&fl4)); 44045c28509SKuniyuki Iwashima rt = ip_route_output_key(net, &fl4); 44150468cddSKuniyuki Iwashima if (IS_ERR(rt)) 44250468cddSKuniyuki Iwashima goto out_free; 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds /* Try to redo what tcp_v4_send_synack did. */ 445ed53d0abSEric Dumazet req->rsk_window_clamp = tp->window_clamp ? :dst_metric(&rt->dst, RTAX_WINDOW); 446909172a1SMao Wenan /* limit the window selection if the user enforce a smaller rx buffer */ 447909172a1SMao Wenan full_space = tcp_full_space(sk); 448909172a1SMao Wenan if (sk->sk_userlocks & SOCK_RCVBUF_LOCK && 449909172a1SMao Wenan (req->rsk_window_clamp > full_space || req->rsk_window_clamp == 0)) 450909172a1SMao Wenan req->rsk_window_clamp = full_space; 4514dfc2817SFlorian Westphal 452909172a1SMao Wenan tcp_select_initial_window(sk, full_space, req->mss, 453ed53d0abSEric Dumazet &req->rsk_rcv_wnd, &req->rsk_window_clamp, 45431d12926Slaurent chavey ireq->wscale_ok, &rcv_wscale, 455d8d1f30bSChangli Gao dst_metric(&rt->dst, RTAX_INITRWND)); 4564dfc2817SFlorian Westphal 4572e6599cbSArnaldo Carvalho de Melo ireq->rcv_wscale = rcv_wscale; 458*8e7bab6bSKuniyuki Iwashima ireq->ecn_ok &= cookie_ecn_ok(net, &rt->dst); 4591da177e4SLinus Torvalds 460efce3d1fSKuniyuki Iwashima ret = tcp_get_cookie_sock(sk, skb, req, &rt->dst); 461dfd25fffSEric Dumazet /* ip_queue_xmit() depends on our flow being setup 462dfd25fffSEric Dumazet * Normal sockets get it right from inet_csk_route_child_sock() 463dfd25fffSEric Dumazet */ 464dfd25fffSEric Dumazet if (ret) 465dfd25fffSEric Dumazet inet_sk(ret)->cork.fl.u.ip4 = fl4; 46650468cddSKuniyuki Iwashima out: 46750468cddSKuniyuki Iwashima return ret; 46850468cddSKuniyuki Iwashima out_free: 46950468cddSKuniyuki Iwashima reqsk_free(req); 47050468cddSKuniyuki Iwashima out_drop: 47150468cddSKuniyuki Iwashima return NULL; 4721da177e4SLinus Torvalds } 473