11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Implementation of the Transmission Control Protocol(TCP). 71da177e4SLinus Torvalds * 802c30a84SJesper Juhl * Authors: Ross Biro 91da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 101da177e4SLinus Torvalds * Mark Evans, <evansmp@uhura.aston.ac.uk> 111da177e4SLinus Torvalds * Corey Minyard <wf-rch!minyard@relay.EU.net> 121da177e4SLinus Torvalds * Florian La Roche, <flla@stud.uni-sb.de> 131da177e4SLinus Torvalds * Charles Hedrick, <hedrick@klinzhai.rutgers.edu> 141da177e4SLinus Torvalds * Linus Torvalds, <torvalds@cs.helsinki.fi> 151da177e4SLinus Torvalds * Alan Cox, <gw4pts@gw4pts.ampr.org> 161da177e4SLinus Torvalds * Matthew Dillon, <dillon@apollo.west.oic.com> 171da177e4SLinus Torvalds * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 181da177e4SLinus Torvalds * Jorge Cwik, <jorge@laser.satlink.net> 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #include <linux/mm.h> 221da177e4SLinus Torvalds #include <linux/module.h> 235a0e3ad6STejun Heo #include <linux/slab.h> 241da177e4SLinus Torvalds #include <linux/sysctl.h> 251da177e4SLinus Torvalds #include <linux/workqueue.h> 261da177e4SLinus Torvalds #include <net/tcp.h> 271da177e4SLinus Torvalds #include <net/inet_common.h> 281da177e4SLinus Torvalds #include <net/xfrm.h> 291da177e4SLinus Torvalds 30e994b7c9SDavid S. Miller int sysctl_tcp_syncookies __read_mostly = 1; 31c6aefafbSGlenn Griffin EXPORT_SYMBOL(sysctl_tcp_syncookies); 32c6aefafbSGlenn Griffin 33ab32ea5dSBrian Haley int sysctl_tcp_abort_on_overflow __read_mostly; 341da177e4SLinus Torvalds 35295ff7edSArnaldo Carvalho de Melo struct inet_timewait_death_row tcp_death_row = { 36295ff7edSArnaldo Carvalho de Melo .sysctl_max_tw_buckets = NR_FILE * 2, 37295ff7edSArnaldo Carvalho de Melo .period = TCP_TIMEWAIT_LEN / INET_TWDR_TWKILL_SLOTS, 38e4d91918SIngo Molnar .death_lock = __SPIN_LOCK_UNLOCKED(tcp_death_row.death_lock), 39295ff7edSArnaldo Carvalho de Melo .hashinfo = &tcp_hashinfo, 40295ff7edSArnaldo Carvalho de Melo .tw_timer = TIMER_INITIALIZER(inet_twdr_hangman, 0, 41295ff7edSArnaldo Carvalho de Melo (unsigned long)&tcp_death_row), 42295ff7edSArnaldo Carvalho de Melo .twkill_work = __WORK_INITIALIZER(tcp_death_row.twkill_work, 4365f27f38SDavid Howells inet_twdr_twkill_work), 44295ff7edSArnaldo Carvalho de Melo /* Short-time timewait calendar */ 45295ff7edSArnaldo Carvalho de Melo 46295ff7edSArnaldo Carvalho de Melo .twcal_hand = -1, 47295ff7edSArnaldo Carvalho de Melo .twcal_timer = TIMER_INITIALIZER(inet_twdr_twcal_tick, 0, 48295ff7edSArnaldo Carvalho de Melo (unsigned long)&tcp_death_row), 49295ff7edSArnaldo Carvalho de Melo }; 50295ff7edSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(tcp_death_row); 51295ff7edSArnaldo Carvalho de Melo 52a2a385d6SEric Dumazet static bool tcp_in_window(u32 seq, u32 end_seq, u32 s_win, u32 e_win) 531da177e4SLinus Torvalds { 541da177e4SLinus Torvalds if (seq == s_win) 55a2a385d6SEric Dumazet return true; 561da177e4SLinus Torvalds if (after(end_seq, s_win) && before(seq, e_win)) 57a2a385d6SEric Dumazet return true; 58a02cec21SEric Dumazet return seq == e_win && seq == end_seq; 591da177e4SLinus Torvalds } 601da177e4SLinus Torvalds 614fb17a60SNeal Cardwell static enum tcp_tw_status 624fb17a60SNeal Cardwell tcp_timewait_check_oow_rate_limit(struct inet_timewait_sock *tw, 634fb17a60SNeal Cardwell const struct sk_buff *skb, int mib_idx) 644fb17a60SNeal Cardwell { 654fb17a60SNeal Cardwell struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw); 664fb17a60SNeal Cardwell 674fb17a60SNeal Cardwell if (!tcp_oow_rate_limited(twsk_net(tw), skb, mib_idx, 684fb17a60SNeal Cardwell &tcptw->tw_last_oow_ack_time)) { 694fb17a60SNeal Cardwell /* Send ACK. Note, we do not put the bucket, 704fb17a60SNeal Cardwell * it will be released by caller. 714fb17a60SNeal Cardwell */ 724fb17a60SNeal Cardwell return TCP_TW_ACK; 734fb17a60SNeal Cardwell } 744fb17a60SNeal Cardwell 754fb17a60SNeal Cardwell /* We are rate-limiting, so just release the tw sock and drop skb. */ 764fb17a60SNeal Cardwell inet_twsk_put(tw); 774fb17a60SNeal Cardwell return TCP_TW_SUCCESS; 784fb17a60SNeal Cardwell } 794fb17a60SNeal Cardwell 801da177e4SLinus Torvalds /* 811da177e4SLinus Torvalds * * Main purpose of TIME-WAIT state is to close connection gracefully, 821da177e4SLinus Torvalds * when one of ends sits in LAST-ACK or CLOSING retransmitting FIN 831da177e4SLinus Torvalds * (and, probably, tail of data) and one or more our ACKs are lost. 841da177e4SLinus Torvalds * * What is TIME-WAIT timeout? It is associated with maximal packet 851da177e4SLinus Torvalds * lifetime in the internet, which results in wrong conclusion, that 861da177e4SLinus Torvalds * it is set to catch "old duplicate segments" wandering out of their path. 871da177e4SLinus Torvalds * It is not quite correct. This timeout is calculated so that it exceeds 881da177e4SLinus Torvalds * maximal retransmission timeout enough to allow to lose one (or more) 891da177e4SLinus Torvalds * segments sent by peer and our ACKs. This time may be calculated from RTO. 901da177e4SLinus Torvalds * * When TIME-WAIT socket receives RST, it means that another end 911da177e4SLinus Torvalds * finally closed and we are allowed to kill TIME-WAIT too. 921da177e4SLinus Torvalds * * Second purpose of TIME-WAIT is catching old duplicate segments. 931da177e4SLinus Torvalds * Well, certainly it is pure paranoia, but if we load TIME-WAIT 941da177e4SLinus Torvalds * with this semantics, we MUST NOT kill TIME-WAIT state with RSTs. 951da177e4SLinus Torvalds * * If we invented some more clever way to catch duplicates 961da177e4SLinus Torvalds * (f.e. based on PAWS), we could truncate TIME-WAIT to several RTOs. 971da177e4SLinus Torvalds * 981da177e4SLinus Torvalds * The algorithm below is based on FORMAL INTERPRETATION of RFCs. 991da177e4SLinus Torvalds * When you compare it to RFCs, please, read section SEGMENT ARRIVES 1001da177e4SLinus Torvalds * from the very beginning. 1011da177e4SLinus Torvalds * 1021da177e4SLinus Torvalds * NOTE. With recycling (and later with fin-wait-2) TW bucket 1031da177e4SLinus Torvalds * is _not_ stateless. It means, that strictly speaking we must 1041da177e4SLinus Torvalds * spinlock it. I do not want! Well, probability of misbehaviour 1051da177e4SLinus Torvalds * is ridiculously low and, seems, we could use some mb() tricks 1061da177e4SLinus Torvalds * to avoid misread sequence numbers, states etc. --ANK 1074308fc58SAlan Cox * 1084308fc58SAlan Cox * We don't need to initialize tmp_out.sack_ok as we don't use the results 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds enum tcp_tw_status 1118feaf0c0SArnaldo Carvalho de Melo tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb, 1128feaf0c0SArnaldo Carvalho de Melo const struct tcphdr *th) 1131da177e4SLinus Torvalds { 1141da177e4SLinus Torvalds struct tcp_options_received tmp_opt; 1154957faadSWilliam Allen Simpson struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw); 116a2a385d6SEric Dumazet bool paws_reject = false; 1171da177e4SLinus Torvalds 118bb5b7c11SDavid S. Miller tmp_opt.saw_tstamp = 0; 1198feaf0c0SArnaldo Carvalho de Melo if (th->doff > (sizeof(*th) >> 2) && tcptw->tw_ts_recent_stamp) { 1201a2c6181SChristoph Paasch tcp_parse_options(skb, &tmp_opt, 0, NULL); 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds if (tmp_opt.saw_tstamp) { 123ee684b6fSAndrey Vagin tmp_opt.rcv_tsecr -= tcptw->tw_ts_offset; 1248feaf0c0SArnaldo Carvalho de Melo tmp_opt.ts_recent = tcptw->tw_ts_recent; 1258feaf0c0SArnaldo Carvalho de Melo tmp_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp; 126c887e6d2SIlpo Järvinen paws_reject = tcp_paws_reject(&tmp_opt, th->rst); 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds if (tw->tw_substate == TCP_FIN_WAIT2) { 1311da177e4SLinus Torvalds /* Just repeat all the checks of tcp_rcv_state_process() */ 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds /* Out of window, send ACK */ 1341da177e4SLinus Torvalds if (paws_reject || 1351da177e4SLinus Torvalds !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq, 1368feaf0c0SArnaldo Carvalho de Melo tcptw->tw_rcv_nxt, 1378feaf0c0SArnaldo Carvalho de Melo tcptw->tw_rcv_nxt + tcptw->tw_rcv_wnd)) 1384fb17a60SNeal Cardwell return tcp_timewait_check_oow_rate_limit( 1394fb17a60SNeal Cardwell tw, skb, LINUX_MIB_TCPACKSKIPPEDFINWAIT2); 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds if (th->rst) 1421da177e4SLinus Torvalds goto kill; 1431da177e4SLinus Torvalds 1448feaf0c0SArnaldo Carvalho de Melo if (th->syn && !before(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt)) 1451da177e4SLinus Torvalds goto kill_with_rst; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* Dup ACK? */ 1481ac530b3SWei Yongjun if (!th->ack || 1491ac530b3SWei Yongjun !after(TCP_SKB_CB(skb)->end_seq, tcptw->tw_rcv_nxt) || 1501da177e4SLinus Torvalds TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) { 1518feaf0c0SArnaldo Carvalho de Melo inet_twsk_put(tw); 1521da177e4SLinus Torvalds return TCP_TW_SUCCESS; 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds /* New data or FIN. If new data arrive after half-duplex close, 1561da177e4SLinus Torvalds * reset. 1571da177e4SLinus Torvalds */ 1581da177e4SLinus Torvalds if (!th->fin || 1598feaf0c0SArnaldo Carvalho de Melo TCP_SKB_CB(skb)->end_seq != tcptw->tw_rcv_nxt + 1) { 1601da177e4SLinus Torvalds kill_with_rst: 161295ff7edSArnaldo Carvalho de Melo inet_twsk_deschedule(tw, &tcp_death_row); 1628feaf0c0SArnaldo Carvalho de Melo inet_twsk_put(tw); 1631da177e4SLinus Torvalds return TCP_TW_RST; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds /* FIN arrived, enter true time-wait state. */ 1671da177e4SLinus Torvalds tw->tw_substate = TCP_TIME_WAIT; 1688feaf0c0SArnaldo Carvalho de Melo tcptw->tw_rcv_nxt = TCP_SKB_CB(skb)->end_seq; 1691da177e4SLinus Torvalds if (tmp_opt.saw_tstamp) { 1709d729f72SJames Morris tcptw->tw_ts_recent_stamp = get_seconds(); 1718feaf0c0SArnaldo Carvalho de Melo tcptw->tw_ts_recent = tmp_opt.rcv_tsval; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 174ccb7c410SDavid S. Miller if (tcp_death_row.sysctl_tw_recycle && 175ccb7c410SDavid S. Miller tcptw->tw_ts_recent_stamp && 176ccb7c410SDavid S. Miller tcp_tw_remember_stamp(tw)) 177696ab2d3SArnaldo Carvalho de Melo inet_twsk_schedule(tw, &tcp_death_row, tw->tw_timeout, 178696ab2d3SArnaldo Carvalho de Melo TCP_TIMEWAIT_LEN); 1791da177e4SLinus Torvalds else 180696ab2d3SArnaldo Carvalho de Melo inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN, 181696ab2d3SArnaldo Carvalho de Melo TCP_TIMEWAIT_LEN); 1821da177e4SLinus Torvalds return TCP_TW_ACK; 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds /* 1861da177e4SLinus Torvalds * Now real TIME-WAIT state. 1871da177e4SLinus Torvalds * 1881da177e4SLinus Torvalds * RFC 1122: 1891da177e4SLinus Torvalds * "When a connection is [...] on TIME-WAIT state [...] 1901da177e4SLinus Torvalds * [a TCP] MAY accept a new SYN from the remote TCP to 1911da177e4SLinus Torvalds * reopen the connection directly, if it: 1921da177e4SLinus Torvalds * 1931da177e4SLinus Torvalds * (1) assigns its initial sequence number for the new 1941da177e4SLinus Torvalds * connection to be larger than the largest sequence 1951da177e4SLinus Torvalds * number it used on the previous connection incarnation, 1961da177e4SLinus Torvalds * and 1971da177e4SLinus Torvalds * 1981da177e4SLinus Torvalds * (2) returns to TIME-WAIT state if the SYN turns out 1991da177e4SLinus Torvalds * to be an old duplicate". 2001da177e4SLinus Torvalds */ 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds if (!paws_reject && 2038feaf0c0SArnaldo Carvalho de Melo (TCP_SKB_CB(skb)->seq == tcptw->tw_rcv_nxt && 2041da177e4SLinus Torvalds (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq || th->rst))) { 2051da177e4SLinus Torvalds /* In window segment, it may be only reset or bare ack. */ 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds if (th->rst) { 208caa20d9aSStephen Hemminger /* This is TIME_WAIT assassination, in two flavors. 2091da177e4SLinus Torvalds * Oh well... nobody has a sufficient solution to this 2101da177e4SLinus Torvalds * protocol bug yet. 2111da177e4SLinus Torvalds */ 2121da177e4SLinus Torvalds if (sysctl_tcp_rfc1337 == 0) { 2131da177e4SLinus Torvalds kill: 214295ff7edSArnaldo Carvalho de Melo inet_twsk_deschedule(tw, &tcp_death_row); 2158feaf0c0SArnaldo Carvalho de Melo inet_twsk_put(tw); 2161da177e4SLinus Torvalds return TCP_TW_SUCCESS; 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds } 219696ab2d3SArnaldo Carvalho de Melo inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN, 220696ab2d3SArnaldo Carvalho de Melo TCP_TIMEWAIT_LEN); 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds if (tmp_opt.saw_tstamp) { 2238feaf0c0SArnaldo Carvalho de Melo tcptw->tw_ts_recent = tmp_opt.rcv_tsval; 2249d729f72SJames Morris tcptw->tw_ts_recent_stamp = get_seconds(); 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 2278feaf0c0SArnaldo Carvalho de Melo inet_twsk_put(tw); 2281da177e4SLinus Torvalds return TCP_TW_SUCCESS; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds /* Out of window segment. 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds All the segments are ACKed immediately. 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds The only exception is new SYN. We accept it, if it is 2361da177e4SLinus Torvalds not old duplicate and we are not in danger to be killed 2371da177e4SLinus Torvalds by delayed old duplicates. RFC check is that it has 2381da177e4SLinus Torvalds newer sequence number works at rates <40Mbit/sec. 2391da177e4SLinus Torvalds However, if paws works, it is reliable AND even more, 2401da177e4SLinus Torvalds we even may relax silly seq space cutoff. 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds RED-PEN: we violate main RFC requirement, if this SYN will appear 2431da177e4SLinus Torvalds old duplicate (i.e. we receive RST in reply to SYN-ACK), 2441da177e4SLinus Torvalds we must return socket to time-wait state. It is not good, 2451da177e4SLinus Torvalds but not fatal yet. 2461da177e4SLinus Torvalds */ 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds if (th->syn && !th->rst && !th->ack && !paws_reject && 2498feaf0c0SArnaldo Carvalho de Melo (after(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt) || 2508feaf0c0SArnaldo Carvalho de Melo (tmp_opt.saw_tstamp && 2518feaf0c0SArnaldo Carvalho de Melo (s32)(tcptw->tw_ts_recent - tmp_opt.rcv_tsval) < 0))) { 2528feaf0c0SArnaldo Carvalho de Melo u32 isn = tcptw->tw_snd_nxt + 65535 + 2; 2531da177e4SLinus Torvalds if (isn == 0) 2541da177e4SLinus Torvalds isn++; 25504317dafSEric Dumazet TCP_SKB_CB(skb)->tcp_tw_isn = isn; 2561da177e4SLinus Torvalds return TCP_TW_SYN; 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds if (paws_reject) 260de0744afSPavel Emelyanov NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_PAWSESTABREJECTED); 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds if (!th->rst) { 2631da177e4SLinus Torvalds /* In this case we must reset the TIMEWAIT timer. 2641da177e4SLinus Torvalds * 2651da177e4SLinus Torvalds * If it is ACKless SYN it may be both old duplicate 2661da177e4SLinus Torvalds * and new good SYN with random sequence number <rcv_nxt. 2671da177e4SLinus Torvalds * Do not reschedule in the last case. 2681da177e4SLinus Torvalds */ 2691da177e4SLinus Torvalds if (paws_reject || th->ack) 270696ab2d3SArnaldo Carvalho de Melo inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN, 271696ab2d3SArnaldo Carvalho de Melo TCP_TIMEWAIT_LEN); 2721da177e4SLinus Torvalds 2734fb17a60SNeal Cardwell return tcp_timewait_check_oow_rate_limit( 2744fb17a60SNeal Cardwell tw, skb, LINUX_MIB_TCPACKSKIPPEDTIMEWAIT); 2751da177e4SLinus Torvalds } 2768feaf0c0SArnaldo Carvalho de Melo inet_twsk_put(tw); 2771da177e4SLinus Torvalds return TCP_TW_SUCCESS; 2781da177e4SLinus Torvalds } 2794bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_timewait_state_process); 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds /* 2821da177e4SLinus Torvalds * Move a socket to time-wait or dead fin-wait-2 state. 2831da177e4SLinus Torvalds */ 2841da177e4SLinus Torvalds void tcp_time_wait(struct sock *sk, int state, int timeo) 2851da177e4SLinus Torvalds { 2868feaf0c0SArnaldo Carvalho de Melo struct inet_timewait_sock *tw = NULL; 2878292a17aSArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 2888feaf0c0SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk); 289a2a385d6SEric Dumazet bool recycle_ok = false; 2901da177e4SLinus Torvalds 291b6242b9bSDavid S. Miller if (tcp_death_row.sysctl_tw_recycle && tp->rx_opt.ts_recent_stamp) 2923f419d2dSDavid S. Miller recycle_ok = tcp_remember_stamp(sk); 2931da177e4SLinus Torvalds 294295ff7edSArnaldo Carvalho de Melo if (tcp_death_row.tw_count < tcp_death_row.sysctl_max_tw_buckets) 295c676270bSArnaldo Carvalho de Melo tw = inet_twsk_alloc(sk, state); 2961da177e4SLinus Torvalds 297*00db4124SIan Morris if (tw) { 2988feaf0c0SArnaldo Carvalho de Melo struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw); 299463c84b9SArnaldo Carvalho de Melo const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1); 3002397849bSDavid S. Miller struct inet_sock *inet = inet_sk(sk); 3018feaf0c0SArnaldo Carvalho de Melo 3022397849bSDavid S. Miller tw->tw_transparent = inet->transparent; 3031da177e4SLinus Torvalds tw->tw_rcv_wscale = tp->rx_opt.rcv_wscale; 3048feaf0c0SArnaldo Carvalho de Melo tcptw->tw_rcv_nxt = tp->rcv_nxt; 3058feaf0c0SArnaldo Carvalho de Melo tcptw->tw_snd_nxt = tp->snd_nxt; 3068feaf0c0SArnaldo Carvalho de Melo tcptw->tw_rcv_wnd = tcp_receive_window(tp); 3078feaf0c0SArnaldo Carvalho de Melo tcptw->tw_ts_recent = tp->rx_opt.ts_recent; 3088feaf0c0SArnaldo Carvalho de Melo tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp; 309ceaa1fefSAndrey Vagin tcptw->tw_ts_offset = tp->tsoffset; 3104fb17a60SNeal Cardwell tcptw->tw_last_oow_ack_time = 0; 3111da177e4SLinus Torvalds 312dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 3131da177e4SLinus Torvalds if (tw->tw_family == PF_INET6) { 3141da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 3151da177e4SLinus Torvalds 316efe4208fSEric Dumazet tw->tw_v6_daddr = sk->sk_v6_daddr; 317efe4208fSEric Dumazet tw->tw_v6_rcv_saddr = sk->sk_v6_rcv_saddr; 318b903d324SEric Dumazet tw->tw_tclass = np->tclass; 3191d13a96cSFlorent Fourcot tw->tw_flowlabel = np->flow_label >> 12; 3209fe516baSEric Dumazet tw->tw_ipv6only = sk->sk_ipv6only; 321c676270bSArnaldo Carvalho de Melo } 3221da177e4SLinus Torvalds #endif 323cfb6eeb4SYOSHIFUJI Hideaki 324cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG 325cfb6eeb4SYOSHIFUJI Hideaki /* 326cfb6eeb4SYOSHIFUJI Hideaki * The timewait bucket does not have the key DB from the 327cfb6eeb4SYOSHIFUJI Hideaki * sock structure. We just make a quick copy of the 328cfb6eeb4SYOSHIFUJI Hideaki * md5 key being used (if indeed we are using one) 329cfb6eeb4SYOSHIFUJI Hideaki * so the timewait ack generating code has the key. 330cfb6eeb4SYOSHIFUJI Hideaki */ 331cfb6eeb4SYOSHIFUJI Hideaki do { 332cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key *key; 333a915da9bSEric Dumazet tcptw->tw_md5_key = NULL; 334cfb6eeb4SYOSHIFUJI Hideaki key = tp->af_specific->md5_lookup(sk, sk); 335*00db4124SIan Morris if (key) { 336a915da9bSEric Dumazet tcptw->tw_md5_key = kmemdup(key, sizeof(*key), GFP_ATOMIC); 33771cea17eSEric Dumazet if (tcptw->tw_md5_key && !tcp_alloc_md5sig_pool()) 338cfb6eeb4SYOSHIFUJI Hideaki BUG(); 339cfb6eeb4SYOSHIFUJI Hideaki } 340cfb6eeb4SYOSHIFUJI Hideaki } while (0); 341cfb6eeb4SYOSHIFUJI Hideaki #endif 342cfb6eeb4SYOSHIFUJI Hideaki 3431da177e4SLinus Torvalds /* Linkage updates. */ 344e48c414eSArnaldo Carvalho de Melo __inet_twsk_hashdance(tw, sk, &tcp_hashinfo); 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds /* Get the TIME_WAIT timeout firing. */ 3471da177e4SLinus Torvalds if (timeo < rto) 3481da177e4SLinus Torvalds timeo = rto; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds if (recycle_ok) { 3511da177e4SLinus Torvalds tw->tw_timeout = rto; 3521da177e4SLinus Torvalds } else { 3531da177e4SLinus Torvalds tw->tw_timeout = TCP_TIMEWAIT_LEN; 3541da177e4SLinus Torvalds if (state == TCP_TIME_WAIT) 3551da177e4SLinus Torvalds timeo = TCP_TIMEWAIT_LEN; 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds 358696ab2d3SArnaldo Carvalho de Melo inet_twsk_schedule(tw, &tcp_death_row, timeo, 359696ab2d3SArnaldo Carvalho de Melo TCP_TIMEWAIT_LEN); 3608feaf0c0SArnaldo Carvalho de Melo inet_twsk_put(tw); 3611da177e4SLinus Torvalds } else { 3621da177e4SLinus Torvalds /* Sorry, if we're out of memory, just CLOSE this 3631da177e4SLinus Torvalds * socket up. We've got bigger problems than 3641da177e4SLinus Torvalds * non-graceful socket closings. 3651da177e4SLinus Torvalds */ 36667631510STom Herbert NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPTIMEWAITOVERFLOW); 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds tcp_update_metrics(sk); 3701da177e4SLinus Torvalds tcp_done(sk); 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 373cfb6eeb4SYOSHIFUJI Hideaki void tcp_twsk_destructor(struct sock *sk) 374cfb6eeb4SYOSHIFUJI Hideaki { 375b6242b9bSDavid S. Miller #ifdef CONFIG_TCP_MD5SIG 376a928630aSDavid S. Miller struct tcp_timewait_sock *twsk = tcp_twsk(sk); 3772397849bSDavid S. Miller 37871cea17eSEric Dumazet if (twsk->tw_md5_key) 379a915da9bSEric Dumazet kfree_rcu(twsk->tw_md5_key, rcu); 380cfb6eeb4SYOSHIFUJI Hideaki #endif 381cfb6eeb4SYOSHIFUJI Hideaki } 382cfb6eeb4SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(tcp_twsk_destructor); 383cfb6eeb4SYOSHIFUJI Hideaki 384843f4a55SYuchung Cheng void tcp_openreq_init_rwin(struct request_sock *req, 385843f4a55SYuchung Cheng struct sock *sk, struct dst_entry *dst) 386843f4a55SYuchung Cheng { 387843f4a55SYuchung Cheng struct inet_request_sock *ireq = inet_rsk(req); 388843f4a55SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 389843f4a55SYuchung Cheng __u8 rcv_wscale; 390843f4a55SYuchung Cheng int mss = dst_metric_advmss(dst); 391843f4a55SYuchung Cheng 392843f4a55SYuchung Cheng if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < mss) 393843f4a55SYuchung Cheng mss = tp->rx_opt.user_mss; 394843f4a55SYuchung Cheng 395843f4a55SYuchung Cheng /* Set this up on the first call only */ 396843f4a55SYuchung Cheng req->window_clamp = tp->window_clamp ? : dst_metric(dst, RTAX_WINDOW); 397843f4a55SYuchung Cheng 398843f4a55SYuchung Cheng /* limit the window selection if the user enforce a smaller rx buffer */ 399843f4a55SYuchung Cheng if (sk->sk_userlocks & SOCK_RCVBUF_LOCK && 400843f4a55SYuchung Cheng (req->window_clamp > tcp_full_space(sk) || req->window_clamp == 0)) 401843f4a55SYuchung Cheng req->window_clamp = tcp_full_space(sk); 402843f4a55SYuchung Cheng 403843f4a55SYuchung Cheng /* tcp_full_space because it is guaranteed to be the first packet */ 404843f4a55SYuchung Cheng tcp_select_initial_window(tcp_full_space(sk), 405843f4a55SYuchung Cheng mss - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0), 406843f4a55SYuchung Cheng &req->rcv_wnd, 407843f4a55SYuchung Cheng &req->window_clamp, 408843f4a55SYuchung Cheng ireq->wscale_ok, 409843f4a55SYuchung Cheng &rcv_wscale, 410843f4a55SYuchung Cheng dst_metric(dst, RTAX_INITRWND)); 411843f4a55SYuchung Cheng ireq->rcv_wscale = rcv_wscale; 412843f4a55SYuchung Cheng } 413843f4a55SYuchung Cheng EXPORT_SYMBOL(tcp_openreq_init_rwin); 414843f4a55SYuchung Cheng 415735d3831SFlorian Westphal static void tcp_ecn_openreq_child(struct tcp_sock *tp, 416735d3831SFlorian Westphal const struct request_sock *req) 417bdf1ee5dSIlpo Järvinen { 418bdf1ee5dSIlpo Järvinen tp->ecn_flags = inet_rsk(req)->ecn_ok ? TCP_ECN_OK : 0; 419bdf1ee5dSIlpo Järvinen } 420bdf1ee5dSIlpo Järvinen 42181164413SDaniel Borkmann void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst) 42281164413SDaniel Borkmann { 42381164413SDaniel Borkmann struct inet_connection_sock *icsk = inet_csk(sk); 42481164413SDaniel Borkmann u32 ca_key = dst_metric(dst, RTAX_CC_ALGO); 42581164413SDaniel Borkmann bool ca_got_dst = false; 42681164413SDaniel Borkmann 42781164413SDaniel Borkmann if (ca_key != TCP_CA_UNSPEC) { 42881164413SDaniel Borkmann const struct tcp_congestion_ops *ca; 42981164413SDaniel Borkmann 43081164413SDaniel Borkmann rcu_read_lock(); 43181164413SDaniel Borkmann ca = tcp_ca_find_key(ca_key); 43281164413SDaniel Borkmann if (likely(ca && try_module_get(ca->owner))) { 43381164413SDaniel Borkmann icsk->icsk_ca_dst_locked = tcp_ca_dst_locked(dst); 43481164413SDaniel Borkmann icsk->icsk_ca_ops = ca; 43581164413SDaniel Borkmann ca_got_dst = true; 43681164413SDaniel Borkmann } 43781164413SDaniel Borkmann rcu_read_unlock(); 43881164413SDaniel Borkmann } 43981164413SDaniel Borkmann 44081164413SDaniel Borkmann if (!ca_got_dst && !try_module_get(icsk->icsk_ca_ops->owner)) 44181164413SDaniel Borkmann tcp_assign_congestion_control(sk); 44281164413SDaniel Borkmann 44381164413SDaniel Borkmann tcp_set_ca_state(sk, TCP_CA_Open); 44481164413SDaniel Borkmann } 44581164413SDaniel Borkmann EXPORT_SYMBOL_GPL(tcp_ca_openreq_child); 44681164413SDaniel Borkmann 4471da177e4SLinus Torvalds /* This is not only more efficient than what we used to do, it eliminates 4481da177e4SLinus Torvalds * a lot of code duplication between IPv4/IPv6 SYN recv processing. -DaveM 4491da177e4SLinus Torvalds * 4501da177e4SLinus Torvalds * Actually, we could lots of memory writes here. tp of listening 4511da177e4SLinus Torvalds * socket contains all necessary default parameters. 4521da177e4SLinus Torvalds */ 45360236fddSArnaldo Carvalho de Melo struct sock *tcp_create_openreq_child(struct sock *sk, struct request_sock *req, struct sk_buff *skb) 4541da177e4SLinus Torvalds { 455e56c57d0SEric Dumazet struct sock *newsk = inet_csk_clone_lock(sk, req, GFP_ATOMIC); 4561da177e4SLinus Torvalds 457*00db4124SIan Morris if (newsk) { 4589f1d2604SArnaldo Carvalho de Melo const struct inet_request_sock *ireq = inet_rsk(req); 4592e6599cbSArnaldo Carvalho de Melo struct tcp_request_sock *treq = tcp_rsk(req); 460a9948a7eSArnaldo Carvalho de Melo struct inet_connection_sock *newicsk = inet_csk(newsk); 461435cf559SWilliam Allen Simpson struct tcp_sock *newtp = tcp_sk(newsk); 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds /* Now setup tcp_sock */ 4641da177e4SLinus Torvalds newtp->pred_flags = 0; 465435cf559SWilliam Allen Simpson 466435cf559SWilliam Allen Simpson newtp->rcv_wup = newtp->copied_seq = 467435cf559SWilliam Allen Simpson newtp->rcv_nxt = treq->rcv_isn + 1; 468435cf559SWilliam Allen Simpson 469435cf559SWilliam Allen Simpson newtp->snd_sml = newtp->snd_una = 4701a2c6181SChristoph Paasch newtp->snd_nxt = newtp->snd_up = treq->snt_isn + 1; 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds tcp_prequeue_init(newtp); 47346d3ceabSEric Dumazet INIT_LIST_HEAD(&newtp->tsq_node); 4741da177e4SLinus Torvalds 475ee7537b6SHantzis Fotis tcp_init_wl(newtp, treq->rcv_isn); 4761da177e4SLinus Torvalds 477740b0f18SEric Dumazet newtp->srtt_us = 0; 478740b0f18SEric Dumazet newtp->mdev_us = jiffies_to_usecs(TCP_TIMEOUT_INIT); 479463c84b9SArnaldo Carvalho de Melo newicsk->icsk_rto = TCP_TIMEOUT_INIT; 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds newtp->packets_out = 0; 4821da177e4SLinus Torvalds newtp->retrans_out = 0; 4831da177e4SLinus Torvalds newtp->sacked_out = 0; 4841da177e4SLinus Torvalds newtp->fackets_out = 0; 4850b6a05c1SIlpo Järvinen newtp->snd_ssthresh = TCP_INFINITE_SSTHRESH; 486eed530b6SYuchung Cheng tcp_enable_early_retrans(newtp); 4879b717a8dSNandita Dukkipati newtp->tlp_high_seq = 0; 488375fe02cSYuchung Cheng newtp->lsndtime = treq->snt_synack; 489f2b2c582SNeal Cardwell newtp->last_oow_ack_time = 0; 490375fe02cSYuchung Cheng newtp->total_retrans = req->num_retrans; 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds /* So many TCP implementations out there (incorrectly) count the 4931da177e4SLinus Torvalds * initial SYN frame in their delayed-ACK and congestion control 4941da177e4SLinus Torvalds * algorithms that we must have the following bandaid to talk 4951da177e4SLinus Torvalds * efficiently to them. -DaveM 4961da177e4SLinus Torvalds */ 4979ad7c049SJerry Chu newtp->snd_cwnd = TCP_INIT_CWND; 4981da177e4SLinus Torvalds newtp->snd_cwnd_cnt = 0; 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds tcp_init_xmit_timers(newsk); 501996b175eSEric Dumazet __skb_queue_head_init(&newtp->out_of_order_queue); 5021a2c6181SChristoph Paasch newtp->write_seq = newtp->pushed_seq = treq->snt_isn + 1; 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds newtp->rx_opt.saw_tstamp = 0; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds newtp->rx_opt.dsack = 0; 5071da177e4SLinus Torvalds newtp->rx_opt.num_sacks = 0; 508cabeccbdSIlpo Järvinen 5091da177e4SLinus Torvalds newtp->urg_data = 0; 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds if (sock_flag(newsk, SOCK_KEEPOPEN)) 512463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer(newsk, 5131da177e4SLinus Torvalds keepalive_time_when(newtp)); 5141da177e4SLinus Torvalds 5152e6599cbSArnaldo Carvalho de Melo newtp->rx_opt.tstamp_ok = ireq->tstamp_ok; 5162e6599cbSArnaldo Carvalho de Melo if ((newtp->rx_opt.sack_ok = ireq->sack_ok) != 0) { 5171da177e4SLinus Torvalds if (sysctl_tcp_fack) 518e60402d0SIlpo Järvinen tcp_enable_fack(newtp); 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds newtp->window_clamp = req->window_clamp; 5211da177e4SLinus Torvalds newtp->rcv_ssthresh = req->rcv_wnd; 5221da177e4SLinus Torvalds newtp->rcv_wnd = req->rcv_wnd; 5232e6599cbSArnaldo Carvalho de Melo newtp->rx_opt.wscale_ok = ireq->wscale_ok; 5241da177e4SLinus Torvalds if (newtp->rx_opt.wscale_ok) { 5252e6599cbSArnaldo Carvalho de Melo newtp->rx_opt.snd_wscale = ireq->snd_wscale; 5262e6599cbSArnaldo Carvalho de Melo newtp->rx_opt.rcv_wscale = ireq->rcv_wscale; 5271da177e4SLinus Torvalds } else { 5281da177e4SLinus Torvalds newtp->rx_opt.snd_wscale = newtp->rx_opt.rcv_wscale = 0; 5291da177e4SLinus Torvalds newtp->window_clamp = min(newtp->window_clamp, 65535U); 5301da177e4SLinus Torvalds } 531aa8223c7SArnaldo Carvalho de Melo newtp->snd_wnd = (ntohs(tcp_hdr(skb)->window) << 532aa8223c7SArnaldo Carvalho de Melo newtp->rx_opt.snd_wscale); 5331da177e4SLinus Torvalds newtp->max_window = newtp->snd_wnd; 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds if (newtp->rx_opt.tstamp_ok) { 5361da177e4SLinus Torvalds newtp->rx_opt.ts_recent = req->ts_recent; 5379d729f72SJames Morris newtp->rx_opt.ts_recent_stamp = get_seconds(); 5381da177e4SLinus Torvalds newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED; 5391da177e4SLinus Torvalds } else { 5401da177e4SLinus Torvalds newtp->rx_opt.ts_recent_stamp = 0; 5411da177e4SLinus Torvalds newtp->tcp_header_len = sizeof(struct tcphdr); 5421da177e4SLinus Torvalds } 543ceaa1fefSAndrey Vagin newtp->tsoffset = 0; 544cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG 545cfb6eeb4SYOSHIFUJI Hideaki newtp->md5sig_info = NULL; /*XXX*/ 546cfb6eeb4SYOSHIFUJI Hideaki if (newtp->af_specific->md5_lookup(sk, newsk)) 547cfb6eeb4SYOSHIFUJI Hideaki newtp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED; 548cfb6eeb4SYOSHIFUJI Hideaki #endif 549bee7ca9eSWilliam Allen Simpson if (skb->len >= TCP_MSS_DEFAULT + newtp->tcp_header_len) 550463c84b9SArnaldo Carvalho de Melo newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len; 5511da177e4SLinus Torvalds newtp->rx_opt.mss_clamp = req->mss; 552735d3831SFlorian Westphal tcp_ecn_openreq_child(newtp, req); 5538336886fSJerry Chu newtp->fastopen_rsk = NULL; 5546f73601eSYuchung Cheng newtp->syn_data_acked = 0; 5551da177e4SLinus Torvalds 55663231bddSPavel Emelyanov TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_PASSIVEOPENS); 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds return newsk; 5591da177e4SLinus Torvalds } 5604bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_create_openreq_child); 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds /* 5638336886fSJerry Chu * Process an incoming packet for SYN_RECV sockets represented as a 5648336886fSJerry Chu * request_sock. Normally sk is the listener socket but for TFO it 5658336886fSJerry Chu * points to the child socket. 5668336886fSJerry Chu * 5678336886fSJerry Chu * XXX (TFO) - The current impl contains a special check for ack 5688336886fSJerry Chu * validation and inside tcp_v4_reqsk_send_ack(). Can we do better? 5694308fc58SAlan Cox * 5704308fc58SAlan Cox * We don't need to initialize tmp_opt.sack_ok as we don't use the results 5711da177e4SLinus Torvalds */ 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb, 57460236fddSArnaldo Carvalho de Melo struct request_sock *req, 5758336886fSJerry Chu bool fastopen) 5761da177e4SLinus Torvalds { 5774957faadSWilliam Allen Simpson struct tcp_options_received tmp_opt; 5784957faadSWilliam Allen Simpson struct sock *child; 579aa8223c7SArnaldo Carvalho de Melo const struct tcphdr *th = tcp_hdr(skb); 580714e85beSAl Viro __be32 flg = tcp_flag_word(th) & (TCP_FLAG_RST|TCP_FLAG_SYN|TCP_FLAG_ACK); 581a2a385d6SEric Dumazet bool paws_reject = false; 5821da177e4SLinus Torvalds 5838336886fSJerry Chu BUG_ON(fastopen == (sk->sk_state == TCP_LISTEN)); 5848336886fSJerry Chu 585bb5b7c11SDavid S. Miller tmp_opt.saw_tstamp = 0; 586bb5b7c11SDavid S. Miller if (th->doff > (sizeof(struct tcphdr)>>2)) { 5871a2c6181SChristoph Paasch tcp_parse_options(skb, &tmp_opt, 0, NULL); 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds if (tmp_opt.saw_tstamp) { 5901da177e4SLinus Torvalds tmp_opt.ts_recent = req->ts_recent; 5911da177e4SLinus Torvalds /* We do not store true stamp, but it is not required, 5921da177e4SLinus Torvalds * it can be estimated (approximately) 5931da177e4SLinus Torvalds * from another data. 5941da177e4SLinus Torvalds */ 595e6c022a4SEric Dumazet tmp_opt.ts_recent_stamp = get_seconds() - ((TCP_TIMEOUT_INIT/HZ)<<req->num_timeout); 596c887e6d2SIlpo Järvinen paws_reject = tcp_paws_reject(&tmp_opt, th->rst); 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds /* Check for pure retransmitted SYN. */ 6012e6599cbSArnaldo Carvalho de Melo if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn && 6021da177e4SLinus Torvalds flg == TCP_FLAG_SYN && 6031da177e4SLinus Torvalds !paws_reject) { 6041da177e4SLinus Torvalds /* 6051da177e4SLinus Torvalds * RFC793 draws (Incorrectly! It was fixed in RFC1122) 6061da177e4SLinus Torvalds * this case on figure 6 and figure 8, but formal 6071da177e4SLinus Torvalds * protocol description says NOTHING. 6081da177e4SLinus Torvalds * To be more exact, it says that we should send ACK, 6091da177e4SLinus Torvalds * because this segment (at least, if it has no data) 6101da177e4SLinus Torvalds * is out of window. 6111da177e4SLinus Torvalds * 6121da177e4SLinus Torvalds * CONCLUSION: RFC793 (even with RFC1122) DOES NOT 6131da177e4SLinus Torvalds * describe SYN-RECV state. All the description 6141da177e4SLinus Torvalds * is wrong, we cannot believe to it and should 6151da177e4SLinus Torvalds * rely only on common sense and implementation 6161da177e4SLinus Torvalds * experience. 6171da177e4SLinus Torvalds * 6181da177e4SLinus Torvalds * Enforce "SYN-ACK" according to figure 8, figure 6 6191da177e4SLinus Torvalds * of RFC793, fixed by RFC1122. 6208336886fSJerry Chu * 6218336886fSJerry Chu * Note that even if there is new data in the SYN packet 6228336886fSJerry Chu * they will be thrown away too. 623cd75eff6SYuchung Cheng * 624cd75eff6SYuchung Cheng * Reset timer after retransmitting SYNACK, similar to 625cd75eff6SYuchung Cheng * the idea of fast retransmit in recovery. 6261da177e4SLinus Torvalds */ 627a9b2c06dSNeal Cardwell if (!tcp_oow_rate_limited(sock_net(sk), skb, 628a9b2c06dSNeal Cardwell LINUX_MIB_TCPACKSKIPPEDSYNRECV, 629a9b2c06dSNeal Cardwell &tcp_rsk(req)->last_oow_ack_time) && 630a9b2c06dSNeal Cardwell 631a9b2c06dSNeal Cardwell !inet_rtx_syn_ack(sk, req)) 632fa76ce73SEric Dumazet mod_timer_pending(&req->rsk_timer, jiffies + 633fa76ce73SEric Dumazet min(TCP_TIMEOUT_INIT << req->num_timeout, 634fa76ce73SEric Dumazet TCP_RTO_MAX)); 6351da177e4SLinus Torvalds return NULL; 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds /* Further reproduces section "SEGMENT ARRIVES" 6391da177e4SLinus Torvalds for state SYN-RECEIVED of RFC793. 6401da177e4SLinus Torvalds It is broken, however, it does not work only 6411da177e4SLinus Torvalds when SYNs are crossed. 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds You would think that SYN crossing is impossible here, since 6441da177e4SLinus Torvalds we should have a SYN_SENT socket (from connect()) on our end, 6451da177e4SLinus Torvalds but this is not true if the crossed SYNs were sent to both 6461da177e4SLinus Torvalds ends by a malicious third party. We must defend against this, 6471da177e4SLinus Torvalds and to do that we first verify the ACK (as per RFC793, page 6481da177e4SLinus Torvalds 36) and reset if it is invalid. Is this a true full defense? 6491da177e4SLinus Torvalds To convince ourselves, let us consider a way in which the ACK 6501da177e4SLinus Torvalds test can still pass in this 'malicious crossed SYNs' case. 6511da177e4SLinus Torvalds Malicious sender sends identical SYNs (and thus identical sequence 6521da177e4SLinus Torvalds numbers) to both A and B: 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds A: gets SYN, seq=7 6551da177e4SLinus Torvalds B: gets SYN, seq=7 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds By our good fortune, both A and B select the same initial 6581da177e4SLinus Torvalds send sequence number of seven :-) 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds A: sends SYN|ACK, seq=7, ack_seq=8 6611da177e4SLinus Torvalds B: sends SYN|ACK, seq=7, ack_seq=8 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds So we are now A eating this SYN|ACK, ACK test passes. So 6641da177e4SLinus Torvalds does sequence test, SYN is truncated, and thus we consider 6651da177e4SLinus Torvalds it a bare ACK. 6661da177e4SLinus Torvalds 667ec0a1966SDavid S. Miller If icsk->icsk_accept_queue.rskq_defer_accept, we silently drop this 668ec0a1966SDavid S. Miller bare ACK. Otherwise, we create an established connection. Both 669ec0a1966SDavid S. Miller ends (listening sockets) accept the new incoming connection and try 670ec0a1966SDavid S. Miller to talk to each other. 8-) 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds Note: This case is both harmless, and rare. Possibility is about the 6731da177e4SLinus Torvalds same as us discovering intelligent life on another plant tomorrow. 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds But generally, we should (RFC lies!) to accept ACK 6761da177e4SLinus Torvalds from SYNACK both here and in tcp_rcv_state_process(). 6771da177e4SLinus Torvalds tcp_rcv_state_process() does not, hence, we do not too. 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds Note that the case is absolutely generic: 6801da177e4SLinus Torvalds we cannot optimize anything here without 6811da177e4SLinus Torvalds violating protocol. All the checks must be made 6821da177e4SLinus Torvalds before attempt to create socket. 6831da177e4SLinus Torvalds */ 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds /* RFC793 page 36: "If the connection is in any non-synchronized state ... 6861da177e4SLinus Torvalds * and the incoming segment acknowledges something not yet 687caa20d9aSStephen Hemminger * sent (the segment carries an unacceptable ACK) ... 6881da177e4SLinus Torvalds * a reset is sent." 6891da177e4SLinus Torvalds * 6908336886fSJerry Chu * Invalid ACK: reset will be sent by listening socket. 6918336886fSJerry Chu * Note that the ACK validity check for a Fast Open socket is done 6928336886fSJerry Chu * elsewhere and is checked directly against the child socket rather 6938336886fSJerry Chu * than req because user data may have been sent out. 6941da177e4SLinus Torvalds */ 6958336886fSJerry Chu if ((flg & TCP_FLAG_ACK) && !fastopen && 696435cf559SWilliam Allen Simpson (TCP_SKB_CB(skb)->ack_seq != 6971a2c6181SChristoph Paasch tcp_rsk(req)->snt_isn + 1)) 6981da177e4SLinus Torvalds return sk; 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds /* Also, it would be not so bad idea to check rcv_tsecr, which 7011da177e4SLinus Torvalds * is essentially ACK extension and too early or too late values 7021da177e4SLinus Torvalds * should cause reset in unsynchronized states. 7031da177e4SLinus Torvalds */ 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds /* RFC793: "first check sequence number". */ 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds if (paws_reject || !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq, 7088336886fSJerry Chu tcp_rsk(req)->rcv_nxt, tcp_rsk(req)->rcv_nxt + req->rcv_wnd)) { 7091da177e4SLinus Torvalds /* Out of window: send ACK and drop. */ 7101da177e4SLinus Torvalds if (!(flg & TCP_FLAG_RST)) 7116edafaafSGui Jianfeng req->rsk_ops->send_ack(sk, skb, req); 7121da177e4SLinus Torvalds if (paws_reject) 713de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED); 7141da177e4SLinus Torvalds return NULL; 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds /* In sequence, PAWS is OK. */ 7181da177e4SLinus Torvalds 7198336886fSJerry Chu if (tmp_opt.saw_tstamp && !after(TCP_SKB_CB(skb)->seq, tcp_rsk(req)->rcv_nxt)) 7201da177e4SLinus Torvalds req->ts_recent = tmp_opt.rcv_tsval; 7211da177e4SLinus Torvalds 7222e6599cbSArnaldo Carvalho de Melo if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn) { 7231da177e4SLinus Torvalds /* Truncate SYN, it is out of window starting 7242e6599cbSArnaldo Carvalho de Melo at tcp_rsk(req)->rcv_isn + 1. */ 7251da177e4SLinus Torvalds flg &= ~TCP_FLAG_SYN; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds /* RFC793: "second check the RST bit" and 7291da177e4SLinus Torvalds * "fourth, check the SYN bit" 7301da177e4SLinus Torvalds */ 7313687b1dcSWei Yongjun if (flg & (TCP_FLAG_RST|TCP_FLAG_SYN)) { 73263231bddSPavel Emelyanov TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_ATTEMPTFAILS); 7331da177e4SLinus Torvalds goto embryonic_reset; 7343687b1dcSWei Yongjun } 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds /* ACK sequence verified above, just make sure ACK is 7371da177e4SLinus Torvalds * set. If ACK not set, just silently drop the packet. 7388336886fSJerry Chu * 7398336886fSJerry Chu * XXX (TFO) - if we ever allow "data after SYN", the 7408336886fSJerry Chu * following check needs to be removed. 7411da177e4SLinus Torvalds */ 7421da177e4SLinus Torvalds if (!(flg & TCP_FLAG_ACK)) 7431da177e4SLinus Torvalds return NULL; 7441da177e4SLinus Torvalds 7458336886fSJerry Chu /* For Fast Open no more processing is needed (sk is the 7468336886fSJerry Chu * child socket). 7478336886fSJerry Chu */ 7488336886fSJerry Chu if (fastopen) 7498336886fSJerry Chu return sk; 7508336886fSJerry Chu 751d1b99ba4SJulian Anastasov /* While TCP_DEFER_ACCEPT is active, drop bare ACK. */ 752e6c022a4SEric Dumazet if (req->num_timeout < inet_csk(sk)->icsk_accept_queue.rskq_defer_accept && 753ec0a1966SDavid S. Miller TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) { 754ec0a1966SDavid S. Miller inet_rsk(req)->acked = 1; 755907cdda5SEric Dumazet NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDEFERACCEPTDROP); 756ec0a1966SDavid S. Miller return NULL; 757ec0a1966SDavid S. Miller } 758ec0a1966SDavid S. Miller 7591da177e4SLinus Torvalds /* OK, ACK is valid, create big socket and 7601da177e4SLinus Torvalds * feed this segment to it. It will repeat all 7611da177e4SLinus Torvalds * the tests. THIS SEGMENT MUST MOVE SOCKET TO 7621da177e4SLinus Torvalds * ESTABLISHED STATE. If it will be dropped after 7631da177e4SLinus Torvalds * socket is created, wait for troubles. 7641da177e4SLinus Torvalds */ 7652aaab9a0SAdam Langley child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL); 76651456b29SIan Morris if (!child) 7671da177e4SLinus Torvalds goto listen_overflow; 7681da177e4SLinus Torvalds 76952452c54SEric Dumazet inet_csk_reqsk_queue_unlink(sk, req); 770463c84b9SArnaldo Carvalho de Melo inet_csk_reqsk_queue_removed(sk, req); 7711da177e4SLinus Torvalds 772463c84b9SArnaldo Carvalho de Melo inet_csk_reqsk_queue_add(sk, req, child); 7731da177e4SLinus Torvalds return child; 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds listen_overflow: 7761da177e4SLinus Torvalds if (!sysctl_tcp_abort_on_overflow) { 7772e6599cbSArnaldo Carvalho de Melo inet_rsk(req)->acked = 1; 7781da177e4SLinus Torvalds return NULL; 7791da177e4SLinus Torvalds } 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds embryonic_reset: 7828336886fSJerry Chu if (!(flg & TCP_FLAG_RST)) { 7838336886fSJerry Chu /* Received a bad SYN pkt - for TFO We try not to reset 7848336886fSJerry Chu * the local connection unless it's really necessary to 7858336886fSJerry Chu * avoid becoming vulnerable to outside attack aiming at 7868336886fSJerry Chu * resetting legit local connections. 7878336886fSJerry Chu */ 788cfb6eeb4SYOSHIFUJI Hideaki req->rsk_ops->send_reset(sk, skb); 7898336886fSJerry Chu } else if (fastopen) { /* received a valid RST pkt */ 7908336886fSJerry Chu reqsk_fastopen_remove(sk, req, true); 7918336886fSJerry Chu tcp_reset(sk); 7928336886fSJerry Chu } 7938336886fSJerry Chu if (!fastopen) { 79452452c54SEric Dumazet inet_csk_reqsk_queue_drop(sk, req); 7958336886fSJerry Chu NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_EMBRYONICRSTS); 7968336886fSJerry Chu } 7971da177e4SLinus Torvalds return NULL; 7981da177e4SLinus Torvalds } 7994bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_check_req); 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds /* 8021da177e4SLinus Torvalds * Queue segment on the new socket if the new socket is active, 8031da177e4SLinus Torvalds * otherwise we just shortcircuit this and continue with 8041da177e4SLinus Torvalds * the new socket. 8058336886fSJerry Chu * 8068336886fSJerry Chu * For the vast majority of cases child->sk_state will be TCP_SYN_RECV 8078336886fSJerry Chu * when entering. But other states are possible due to a race condition 8088336886fSJerry Chu * where after __inet_lookup_established() fails but before the listener 8098336886fSJerry Chu * locked is obtained, other packets cause the same connection to 8108336886fSJerry Chu * be created. 8111da177e4SLinus Torvalds */ 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds int tcp_child_process(struct sock *parent, struct sock *child, 8141da177e4SLinus Torvalds struct sk_buff *skb) 8151da177e4SLinus Torvalds { 8161da177e4SLinus Torvalds int ret = 0; 8171da177e4SLinus Torvalds int state = child->sk_state; 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds if (!sock_owned_by_user(child)) { 820aa8223c7SArnaldo Carvalho de Melo ret = tcp_rcv_state_process(child, skb, tcp_hdr(skb), 821aa8223c7SArnaldo Carvalho de Melo skb->len); 8221da177e4SLinus Torvalds /* Wakeup parent, send SIGIO */ 8231da177e4SLinus Torvalds if (state == TCP_SYN_RECV && child->sk_state != state) 824676d2369SDavid S. Miller parent->sk_data_ready(parent); 8251da177e4SLinus Torvalds } else { 8261da177e4SLinus Torvalds /* Alas, it is possible again, because we do lookup 8271da177e4SLinus Torvalds * in main socket hash table and lock on listening 8281da177e4SLinus Torvalds * socket does not protect us more. 8291da177e4SLinus Torvalds */ 830a3a858ffSZhu Yi __sk_add_backlog(child, skb); 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds bh_unlock_sock(child); 8341da177e4SLinus Torvalds sock_put(child); 8351da177e4SLinus Torvalds return ret; 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_child_process); 838