Lines Matching +full:wait +full:- +full:state

1 // SPDX-License-Identifier: GPL-2.0-only
12 * Corey Minyard <wf-rch!minyard@relay.EU.net>
13 * Florian La Roche, <flla@stud.uni-sb.de>
45 &tcptw->tw_last_oow_ack_time)) {
52 /* We are rate-limiting, so just release the tw sock and drop skb. */
63 ao = rcu_dereference(tcptw->ao_info);
65 WRITE_ONCE(ao->rcv_sne, ao->rcv_sne + 1);
67 WRITE_ONCE(tcptw->tw_rcv_nxt, seq);
71 * * Main purpose of TIME-WAIT state is to close connection gracefully,
72 * when one of ends sits in LAST-ACK or CLOSING retransmitting FIN
74 * * What is TIME-WAIT timeout? It is associated with maximal packet
80 * * When TIME-WAIT socket receives RST, it means that another end
81 * finally closed and we are allowed to kill TIME-WAIT too.
82 * * Second purpose of TIME-WAIT is catching old duplicate segments.
83 * Well, certainly it is pure paranoia, but if we load TIME-WAIT
84 * with this semantics, we MUST NOT kill TIME-WAIT state with RSTs.
86 * (f.e. based on PAWS), we could truncate TIME-WAIT to several RTOs.
92 * NOTE. With recycling (and later with fin-wait-2) TW bucket
96 * to avoid misread sequence numbers, states etc. --ANK
106 u32 rcv_nxt = READ_ONCE(tcptw->tw_rcv_nxt);
112 /* Instead of dropping immediately, wait to see what value is
113 * returned. We will accept a non psp-encapsulated syn in the
119 ts_recent_stamp = READ_ONCE(tcptw->tw_ts_recent_stamp);
120 if (th->doff > (sizeof(*th) >> 2) && ts_recent_stamp) {
125 tmp_opt.rcv_tsecr -= tcptw->tw_ts_offset;
126 tmp_opt.ts_recent = READ_ONCE(tcptw->tw_ts_recent);
128 paws_reject = tcp_paws_reject(&tmp_opt, th->rst);
132 if (READ_ONCE(tw->tw_substate) == TCP_FIN_WAIT2) {
140 !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
142 rcv_nxt + tcptw->tw_rcv_wnd))
146 if (th->rst)
149 if (th->syn && !before(TCP_SKB_CB(skb)->seq, rcv_nxt))
153 if (!th->ack ||
154 !after(TCP_SKB_CB(skb)->end_seq, rcv_nxt) ||
155 TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) {
160 /* New data or FIN. If new data arrive after half-duplex close,
163 if (!th->fin ||
164 TCP_SKB_CB(skb)->end_seq != rcv_nxt + 1)
167 /* FIN arrived, enter true time-wait state. */
168 WRITE_ONCE(tw->tw_substate, TCP_TIME_WAIT);
169 twsk_rcv_nxt_update(tcptw, TCP_SKB_CB(skb)->end_seq,
175 WRITE_ONCE(tw->tw_entry_stamp, ts);
176 WRITE_ONCE(tcptw->tw_ts_recent_stamp,
178 WRITE_ONCE(tcptw->tw_ts_recent,
187 * Now real TIME-WAIT state.
190 * "When a connection is [...] on TIME-WAIT state [...]
199 * (2) returns to TIME-WAIT state if the SYN turns out
204 (TCP_SKB_CB(skb)->seq == rcv_nxt &&
205 (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq || th->rst))) {
211 if (th->rst) {
216 if (!READ_ONCE(twsk_net(tw)->ipv4.sysctl_tcp_rfc1337)) {
226 WRITE_ONCE(tcptw->tw_ts_recent,
228 WRITE_ONCE(tcptw->tw_ts_recent_stamp,
247 RED-PEN: we violate main RFC requirement, if this SYN will appear
248 old duplicate (i.e. we receive RST in reply to SYN-ACK),
249 we must return socket to time-wait state. It is not good,
253 if (th->syn && !th->rst && !th->ack && !paws_reject &&
254 (after(TCP_SKB_CB(skb)->seq, rcv_nxt) ||
256 (s32)(READ_ONCE(tcptw->tw_ts_recent) - tmp_opt.rcv_tsval) < 0))) {
257 u32 isn = tcptw->tw_snd_nxt + 65535 + 2;
272 if (!th->rst) {
279 if (paws_reject || th->ack)
304 tcptw->tw_md5_key = NULL;
308 key = tp->af_specific->md5_lookup(sk, sk);
310 tcptw->tw_md5_key = kmemdup(key, sizeof(*key), GFP_ATOMIC);
311 if (!tcptw->tw_md5_key)
320 kfree(tcptw->tw_md5_key);
321 tcptw->tw_md5_key = NULL;
326 * Move a socket to time-wait or dead fin-wait-2 state.
328 void tcp_time_wait(struct sock *sk, int state, int timeo)
335 tw = inet_twsk_alloc(sk, &net->ipv4.tcp_death_row, state);
339 const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
341 tw->tw_transparent = inet_test_bit(TRANSPARENT, sk);
342 tw->tw_mark = sk->sk_mark;
343 tw->tw_priority = READ_ONCE(sk->sk_priority);
344 tw->tw_rcv_wscale = tp->rx_opt.rcv_wscale;
345 /* refreshed when we enter true TIME-WAIT state */
346 tw->tw_entry_stamp = tcp_time_stamp_ms(tp);
347 tcptw->tw_rcv_nxt = tp->rcv_nxt;
348 tcptw->tw_snd_nxt = tp->snd_nxt;
349 tcptw->tw_rcv_wnd = tcp_receive_window(tp);
350 tcptw->tw_ts_recent = tp->rx_opt.ts_recent;
351 tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp;
352 tcptw->tw_ts_offset = tp->tsoffset;
353 tw->tw_usec_ts = tp->tcp_usec_ts;
354 tcptw->tw_last_oow_ack_time = 0;
355 tcptw->tw_tx_delay = tp->tcp_tx_delay;
356 tw->tw_txhash = sk->sk_txhash;
357 tw->tw_tx_queue_mapping = sk->sk_tx_queue_mapping;
359 tw->tw_rx_queue_mapping = sk->sk_rx_queue_mapping;
362 if (tw->tw_family == PF_INET6) {
365 tw->tw_v6_daddr = sk->sk_v6_daddr;
366 tw->tw_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
367 tw->tw_tclass = np->tclass;
368 tw->tw_flowlabel = be32_to_cpu(np->flow_label & IPV6_FLOWLABEL_MASK);
369 tw->tw_ipv6only = sk->sk_ipv6only;
380 if (state == TCP_TIME_WAIT)
386 inet_twsk_hashdance_schedule(tw, sk, net->ipv4.tcp_death_row.hashinfo, timeo);
390 * non-graceful socket closings.
406 if (twsk->tw_md5_key) {
407 kfree(twsk->tw_md5_key);
423 if (net->ipv4.tcp_death_row.hashinfo->pernet) {
425 inet_twsk_purge(net->ipv4.tcp_death_row.hashinfo);
449 window_clamp = READ_ONCE(tp->window_clamp);
451 req->rsk_window_clamp = window_clamp ? : dst_metric(dst, RTAX_WINDOW);
454 if (sk_listener->sk_userlocks & SOCK_RCVBUF_LOCK &&
455 (req->rsk_window_clamp > full_space || req->rsk_window_clamp == 0))
456 req->rsk_window_clamp = full_space;
466 mss - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),
467 &req->rsk_rcv_wnd,
468 &req->rsk_window_clamp,
469 ireq->wscale_ok,
472 ireq->rcv_wscale = rcv_wscale;
482 if (treq->accecn_ok) {
484 tp->syn_ect_snt = treq->syn_ect_snt;
485 tcp_accecn_third_ack(sk, skb, treq->syn_ect_snt);
486 tp->saw_accecn_opt = treq->saw_accecn_opt;
487 tp->prev_ecnfield = treq->syn_ect_rcv;
488 tp->accecn_opt_demand = 1;
491 tcp_ecn_mode_set(tp, inet_rsk(req)->ecn_ok ?
508 if (likely(ca && bpf_try_module_get(ca, ca->owner))) {
509 icsk->icsk_ca_dst_locked = tcp_ca_dst_locked(dst);
510 icsk->icsk_ca_ops = ca;
518 (!icsk->icsk_ca_setsockopt ||
519 !bpf_try_module_get(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner)))
535 if (oldtp->syn_smc && !ireq->smc_ok)
536 newtp->syn_smc = 0;
542 * a lot of code duplication between IPv4/IPv6 SYN recv processing. -DaveM
569 newtp->pred_flags = 0;
571 seq = treq->rcv_isn + 1;
572 newtp->rcv_wup = seq;
573 WRITE_ONCE(newtp->copied_seq, seq);
574 WRITE_ONCE(newtp->rcv_nxt, seq);
575 newtp->segs_in = 1;
577 seq = treq->snt_isn + 1;
578 newtp->snd_sml = newtp->snd_una = seq;
579 WRITE_ONCE(newtp->snd_nxt, seq);
580 newtp->snd_up = seq;
582 INIT_LIST_HEAD(&newtp->tsq_node);
583 INIT_LIST_HEAD(&newtp->tsorted_sent_queue);
585 tcp_init_wl(newtp, treq->rcv_isn);
587 minmax_reset(&newtp->rtt_min, tcp_jiffies32, ~0U);
588 newicsk->icsk_ack.lrcvtime = tcp_jiffies32;
590 newtp->lsndtime = tcp_jiffies32;
591 newsk->sk_txhash = READ_ONCE(treq->txhash);
592 newtp->total_retrans = req->num_retrans;
595 WRITE_ONCE(newtp->write_seq, newtp->pushed_seq = treq->snt_isn + 1);
600 newtp->rx_opt.tstamp_ok = ireq->tstamp_ok;
601 newtp->rx_opt.sack_ok = ireq->sack_ok;
602 newtp->window_clamp = req->rsk_window_clamp;
603 newtp->rcv_ssthresh = req->rsk_rcv_wnd;
604 newtp->rcv_wnd = req->rsk_rcv_wnd;
605 newtp->rx_opt.wscale_ok = ireq->wscale_ok;
606 if (newtp->rx_opt.wscale_ok) {
607 newtp->rx_opt.snd_wscale = ireq->snd_wscale;
608 newtp->rx_opt.rcv_wscale = ireq->rcv_wscale;
610 newtp->rx_opt.snd_wscale = newtp->rx_opt.rcv_wscale = 0;
611 newtp->window_clamp = min(newtp->window_clamp, 65535U);
613 newtp->snd_wnd = ntohs(tcp_hdr(skb)->window) << newtp->rx_opt.snd_wscale;
614 newtp->max_window = newtp->snd_wnd;
616 if (newtp->rx_opt.tstamp_ok) {
617 newtp->tcp_usec_ts = treq->req_usec_ts;
618 newtp->rx_opt.ts_recent = req->ts_recent;
619 newtp->rx_opt.ts_recent_stamp = ktime_get_seconds();
620 newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
622 newtp->tcp_usec_ts = 0;
623 newtp->rx_opt.ts_recent_stamp = 0;
624 newtp->tcp_header_len = sizeof(struct tcphdr);
626 if (req->num_timeout) {
627 newtp->total_rto = req->num_timeout;
628 newtp->undo_marker = treq->snt_isn;
629 if (newtp->tcp_usec_ts) {
630 newtp->retrans_stamp = treq->snt_synack;
631 newtp->total_rto_time = (u32)(tcp_clock_us() -
632 newtp->retrans_stamp) / USEC_PER_MSEC;
634 newtp->retrans_stamp = div_u64(treq->snt_synack,
636 newtp->total_rto_time = tcp_clock_ms() -
637 newtp->retrans_stamp;
639 newtp->total_rto_recoveries = 1;
641 newtp->tsoffset = treq->ts_off;
643 newtp->md5sig_info = NULL; /*XXX*/
646 newtp->ao_info = NULL;
651 ao_key = treq->af_specific->ao_lookup(sk, req, tcp_rsk(req)->ao_keyid, -1);
653 newtp->tcp_header_len += tcp_ao_len_aligned(ao_key);
656 if (skb->len >= TCP_MSS_DEFAULT + newtp->tcp_header_len)
657 newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len;
658 newtp->rx_opt.mss_clamp = req->mss;
660 newtp->fastopen_req = NULL;
661 RCU_INIT_POINTER(newtp->fastopen_rsk, NULL);
663 newtp->bpf_chg_cc_inprogress = 0;
668 xa_init_flags(&newsk->sk_user_frags, XA_FLAGS_ALLOC1);
679 * XXX (TFO) - The current impl contains a special check for ack
703 if (th->doff > (sizeof(struct tcphdr)>>2)) {
707 tmp_opt.ts_recent = req->ts_recent;
709 if (inet_rsk(req)->tstamp_ok && !fastopen)
711 tcp_rsk(req)->snt_tsval_first,
712 READ_ONCE(tcp_rsk(req)->snt_tsval_last));
713 tmp_opt.rcv_tsecr -= tcp_rsk(req)->ts_off;
719 tmp_opt.ts_recent_stamp = ktime_get_seconds() - reqsk_timeout(req, TCP_RTO_MAX) / HZ;
720 paws_reject = tcp_paws_reject(&tmp_opt, th->rst);
725 if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn &&
737 * describe SYN-RECV state. All the description
742 * Enforce "SYN-ACK" according to figure 8, figure 6
753 &tcp_rsk(req)->last_oow_ack_time) &&
760 mod_timer_pending(&req->rsk_timer, expires);
762 req->rsk_timer.expires = expires;
768 for state SYN-RECEIVED of RFC793.
787 send sequence number of seven :-)
796 If icsk->icsk_accept_queue.rskq_defer_accept, we silently drop this
799 to talk to each other. 8-)
814 /* RFC793 page 36: "If the connection is in any non-synchronized state ...
825 (TCP_SKB_CB(skb)->ack_seq !=
826 tcp_rsk(req)->snt_isn + 1))
832 !tcp_in_window(TCP_SKB_CB(skb)->seq,
833 TCP_SKB_CB(skb)->end_seq,
834 tcp_rsk(req)->rcv_nxt,
835 tcp_rsk(req)->rcv_nxt +
841 &tcp_rsk(req)->last_oow_ack_time))
842 req->rsk_ops->send_ack(sk, skb, req);
857 if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn) {
859 at tcp_rsk(req)->rcv_isn + 1. */
874 * XXX (TFO) - if we ever allow "data after SYN", the
880 if (tcp_rsk(req)->accecn_ok && tmp_opt.accecn &&
881 tcp_rsk(req)->saw_accecn_opt < TCP_ACCECN_OPT_COUNTER_SEEN) {
884 tcp_rsk(req)->saw_accecn_opt = saw_opt;
885 if (tcp_rsk(req)->saw_accecn_opt == TCP_ACCECN_OPT_FAIL_SEEN) {
888 tcp_rsk(req)->accecn_fail_mode |= fail_mode;
899 if (req->num_timeout < READ_ONCE(inet_csk(sk)->icsk_accept_queue.rskq_defer_accept) &&
900 TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) {
901 inet_rsk(req)->acked = 1;
909 * ESTABLISHED STATE. If it will be dropped after
910 * socket is created, wait for troubles.
912 child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL,
918 !after(TCP_SKB_CB(skb)->seq, tcp_rsk(req)->rcv_nxt))
919 tcp_sk(child)->rx_opt.ts_recent = tmp_opt.rcv_tsval;
922 reqsk_queue_removed(&inet_csk(req->rsk_listener)->icsk_accept_queue, req);
923 inet_csk_reqsk_queue_drop_and_put(req->rsk_listener, req);
934 if (sk != req->rsk_listener)
937 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_abort_on_overflow)) {
938 inet_rsk(req)->acked = 1;
944 /* Received a bad SYN pkt - for TFO We try not to reset
949 req->rsk_ops->send_reset(sk, skb, SK_RST_REASON_INVALID_SYN);
970 * For the vast majority of cases child->sk_state will be TCP_SYN_RECV
979 __releases(&((child)->sk_lock.slock))
982 int state = child->sk_state;
991 if (state == TCP_SYN_RECV && child->sk_state != state)
992 parent->sk_data_ready(parent);