1df8bae1dSRodney W. Grimes /* 2e79adb8eSGarrett Wollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33e79adb8eSGarrett Wollman * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37fb59c426SYoshinobu Inoue #include "opt_inet6.h" 380cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 390cc12cc5SJoerg Wunsch 40df8bae1dSRodney W. Grimes #include <sys/param.h> 4198163b98SPoul-Henning Kamp #include <sys/kernel.h> 42c74af4faSBruce Evans #include <sys/lock.h> 4308517d53SMike Silbersack #include <sys/mbuf.h> 44c74af4faSBruce Evans #include <sys/mutex.h> 45c74af4faSBruce Evans #include <sys/protosw.h> 46df8bae1dSRodney W. Grimes #include <sys/socket.h> 47df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 48c74af4faSBruce Evans #include <sys/sysctl.h> 49c74af4faSBruce Evans #include <sys/systm.h> 50e79adb8eSGarrett Wollman 51df8bae1dSRodney W. Grimes #include <net/route.h> 52df8bae1dSRodney W. Grimes 53df8bae1dSRodney W. Grimes #include <netinet/in.h> 54df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 55c74af4faSBruce Evans #include <netinet/in_systm.h> 56fb59c426SYoshinobu Inoue #ifdef INET6 57fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h> 58fb59c426SYoshinobu Inoue #endif 59df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 60df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 61df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 62df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 63df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 64df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 65af7a2999SDavid Greenman #ifdef TCPDEBUG 66af7a2999SDavid Greenman #include <netinet/tcp_debug.h> 67af7a2999SDavid Greenman #endif 68df8bae1dSRodney W. Grimes 69ccb4d0c6SJonathan Lemon static int 7082d9ae4eSPoul-Henning Kamp sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS) 71ccb4d0c6SJonathan Lemon { 72ccb4d0c6SJonathan Lemon int error, s, tt; 73ccb4d0c6SJonathan Lemon 74ccb4d0c6SJonathan Lemon tt = *(int *)oidp->oid_arg1; 758ea8a680SJohn Polstra s = (int)((int64_t)tt * 1000 / hz); 76ccb4d0c6SJonathan Lemon 779fc2bcf6SJonathan Lemon error = sysctl_handle_int(oidp, &s, 0, req); 78ccb4d0c6SJonathan Lemon if (error || !req->newptr) 79ccb4d0c6SJonathan Lemon return (error); 80ccb4d0c6SJonathan Lemon 818ea8a680SJohn Polstra tt = (int)((int64_t)s * hz / 1000); 829fc2bcf6SJonathan Lemon if (tt < 1) 839fc2bcf6SJonathan Lemon return (EINVAL); 84ccb4d0c6SJonathan Lemon 85ccb4d0c6SJonathan Lemon *(int *)oidp->oid_arg1 = tt; 86ccb4d0c6SJonathan Lemon return (0); 87ccb4d0c6SJonathan Lemon } 88ccb4d0c6SJonathan Lemon 899b8b58e0SJonathan Lemon int tcp_keepinit; 90ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW, 91ccb4d0c6SJonathan Lemon &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", ""); 927b40aa32SPaul Traina 939b8b58e0SJonathan Lemon int tcp_keepidle; 94ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW, 95ccb4d0c6SJonathan Lemon &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", ""); 9698163b98SPoul-Henning Kamp 979b8b58e0SJonathan Lemon int tcp_keepintvl; 98ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW, 99ccb4d0c6SJonathan Lemon &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", ""); 10098163b98SPoul-Henning Kamp 1019b8b58e0SJonathan Lemon int tcp_delacktime; 102ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, 103ccb4d0c6SJonathan Lemon CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I", 104ccb4d0c6SJonathan Lemon "Time before a delayed ACK is sent"); 1059b8b58e0SJonathan Lemon 1069b8b58e0SJonathan Lemon int tcp_msl; 107ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW, 108ccb4d0c6SJonathan Lemon &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime"); 1099b8b58e0SJonathan Lemon 110701bec5aSMatthew Dillon int tcp_rexmit_min; 111701bec5aSMatthew Dillon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW, 112701bec5aSMatthew Dillon &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I", "Minimum Retransmission Timeout"); 113701bec5aSMatthew Dillon 114701bec5aSMatthew Dillon int tcp_rexmit_slop; 115701bec5aSMatthew Dillon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW, 116701bec5aSMatthew Dillon &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I", "Retransmission Timer Slop"); 117701bec5aSMatthew Dillon 118c39a614eSRobert Watson static int always_keepalive = 1; 1193d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW, 1203d177f46SBill Fumerola &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections"); 12134be9bf3SPoul-Henning Kamp 1220312fbe9SPoul-Henning Kamp static int tcp_keepcnt = TCPTV_KEEPCNT; 1230312fbe9SPoul-Henning Kamp /* max idle probes */ 1249b8b58e0SJonathan Lemon int tcp_maxpersistidle; 1250312fbe9SPoul-Henning Kamp /* max idle time in persist */ 126df8bae1dSRodney W. Grimes int tcp_maxidle; 127e79adb8eSGarrett Wollman 128df8bae1dSRodney W. Grimes /* 129df8bae1dSRodney W. Grimes * Tcp protocol timeout routine called every 500 ms. 1309b8b58e0SJonathan Lemon * Updates timestamps used for TCP 131df8bae1dSRodney W. Grimes * causes finite state machine actions if timers expire. 132df8bae1dSRodney W. Grimes */ 133df8bae1dSRodney W. Grimes void 134df8bae1dSRodney W. Grimes tcp_slowtimo() 135df8bae1dSRodney W. Grimes { 13615bd2b43SDavid Greenman int s; 13715bd2b43SDavid Greenman 13815bd2b43SDavid Greenman s = splnet(); 139df8bae1dSRodney W. Grimes 140e79adb8eSGarrett Wollman tcp_maxidle = tcp_keepcnt * tcp_keepintvl; 14115bd2b43SDavid Greenman 142df8bae1dSRodney W. Grimes splx(s); 143df8bae1dSRodney W. Grimes } 144df8bae1dSRodney W. Grimes 145df8bae1dSRodney W. Grimes /* 146df8bae1dSRodney W. Grimes * Cancel all timers for TCP tp. 147df8bae1dSRodney W. Grimes */ 148df8bae1dSRodney W. Grimes void 149df8bae1dSRodney W. Grimes tcp_canceltimers(tp) 150df8bae1dSRodney W. Grimes struct tcpcb *tp; 151df8bae1dSRodney W. Grimes { 1529b8b58e0SJonathan Lemon callout_stop(tp->tt_2msl); 1539b8b58e0SJonathan Lemon callout_stop(tp->tt_persist); 1549b8b58e0SJonathan Lemon callout_stop(tp->tt_keep); 1559b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 156df8bae1dSRodney W. Grimes } 157df8bae1dSRodney W. Grimes 1587d42e30cSJonathan Lemon int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] = 1597d42e30cSJonathan Lemon { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 }; 1607d42e30cSJonathan Lemon 161df8bae1dSRodney W. Grimes int tcp_backoff[TCP_MAXRXTSHIFT + 1] = 162df8bae1dSRodney W. Grimes { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; 163df8bae1dSRodney W. Grimes 1640312fbe9SPoul-Henning Kamp static int tcp_totbackoff = 511; /* sum of tcp_backoff[] */ 165e79adb8eSGarrett Wollman 166df8bae1dSRodney W. Grimes /* 167df8bae1dSRodney W. Grimes * TCP timer processing. 168df8bae1dSRodney W. Grimes */ 169f76fcf6dSJeffrey Hsu 1709b8b58e0SJonathan Lemon void 1719b8b58e0SJonathan Lemon tcp_timer_delack(xtp) 1729b8b58e0SJonathan Lemon void *xtp; 173df8bae1dSRodney W. Grimes { 1749b8b58e0SJonathan Lemon struct tcpcb *tp = xtp; 1759b8b58e0SJonathan Lemon int s; 176f76fcf6dSJeffrey Hsu struct inpcb *inp; 177df8bae1dSRodney W. Grimes 1789b8b58e0SJonathan Lemon s = splnet(); 179f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&tcbinfo); 180f76fcf6dSJeffrey Hsu inp = tp->t_inpcb; 181abe239cfSJeffrey Hsu if (!inp) { 182abe239cfSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); 183abe239cfSJeffrey Hsu splx(s); 184abe239cfSJeffrey Hsu return; 185abe239cfSJeffrey Hsu } 186f76fcf6dSJeffrey Hsu INP_LOCK(inp); 187f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); 188af1270f8SJonathan Lemon if (callout_pending(tp->tt_delack) || !callout_active(tp->tt_delack)) { 189f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1909b8b58e0SJonathan Lemon splx(s); 1919b8b58e0SJonathan Lemon return; 1929b8b58e0SJonathan Lemon } 1939b8b58e0SJonathan Lemon callout_deactivate(tp->tt_delack); 194df8bae1dSRodney W. Grimes 1959b8b58e0SJonathan Lemon tp->t_flags |= TF_ACKNOW; 1969b8b58e0SJonathan Lemon tcpstat.tcps_delack++; 1979b8b58e0SJonathan Lemon (void) tcp_output(tp); 198f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1999b8b58e0SJonathan Lemon splx(s); 2009b8b58e0SJonathan Lemon } 2019b8b58e0SJonathan Lemon 2029b8b58e0SJonathan Lemon void 2039b8b58e0SJonathan Lemon tcp_timer_2msl(xtp) 2049b8b58e0SJonathan Lemon void *xtp; 2059b8b58e0SJonathan Lemon { 2069b8b58e0SJonathan Lemon struct tcpcb *tp = xtp; 2079b8b58e0SJonathan Lemon int s; 208f76fcf6dSJeffrey Hsu struct inpcb *inp; 2099b8b58e0SJonathan Lemon #ifdef TCPDEBUG 2109b8b58e0SJonathan Lemon int ostate; 2119b8b58e0SJonathan Lemon 2129b8b58e0SJonathan Lemon ostate = tp->t_state; 2139b8b58e0SJonathan Lemon #endif 2149b8b58e0SJonathan Lemon s = splnet(); 215f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 216f76fcf6dSJeffrey Hsu inp = tp->t_inpcb; 217abe239cfSJeffrey Hsu if (!inp) { 218abe239cfSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 219abe239cfSJeffrey Hsu splx(s); 220abe239cfSJeffrey Hsu return; 221abe239cfSJeffrey Hsu } 222f76fcf6dSJeffrey Hsu INP_LOCK(inp); 223af1270f8SJonathan Lemon if (callout_pending(tp->tt_2msl) || !callout_active(tp->tt_2msl)) { 224f76fcf6dSJeffrey Hsu INP_UNLOCK(tp->t_inpcb); 225f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2269b8b58e0SJonathan Lemon splx(s); 2279b8b58e0SJonathan Lemon return; 2289b8b58e0SJonathan Lemon } 2299b8b58e0SJonathan Lemon callout_deactivate(tp->tt_2msl); 230df8bae1dSRodney W. Grimes /* 231df8bae1dSRodney W. Grimes * 2 MSL timeout in shutdown went off. If we're closed but 232df8bae1dSRodney W. Grimes * still waiting for peer to close and connection has been idle 233df8bae1dSRodney W. Grimes * too long, or if 2MSL time is up from TIME_WAIT, delete connection 234df8bae1dSRodney W. Grimes * control block. Otherwise, check again in a bit. 235df8bae1dSRodney W. Grimes */ 236df8bae1dSRodney W. Grimes if (tp->t_state != TCPS_TIME_WAIT && 2379b8b58e0SJonathan Lemon (ticks - tp->t_rcvtime) <= tcp_maxidle) 2389b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_keepintvl, 2399b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 240df8bae1dSRodney W. Grimes else 241df8bae1dSRodney W. Grimes tp = tcp_close(tp); 242df8bae1dSRodney W. Grimes 2439b8b58e0SJonathan Lemon #ifdef TCPDEBUG 2444cc20ab1SSeigo Tanimura if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 245fb59c426SYoshinobu Inoue tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 2469b8b58e0SJonathan Lemon PRU_SLOWTIMO); 2479b8b58e0SJonathan Lemon #endif 248f76fcf6dSJeffrey Hsu if (tp) 249f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 250f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2519b8b58e0SJonathan Lemon splx(s); 2529b8b58e0SJonathan Lemon } 2539b8b58e0SJonathan Lemon 2549b8b58e0SJonathan Lemon void 2559b8b58e0SJonathan Lemon tcp_timer_keep(xtp) 2569b8b58e0SJonathan Lemon void *xtp; 2579b8b58e0SJonathan Lemon { 2589b8b58e0SJonathan Lemon struct tcpcb *tp = xtp; 25908517d53SMike Silbersack struct tcptemp *t_template; 2609b8b58e0SJonathan Lemon int s; 261f76fcf6dSJeffrey Hsu struct inpcb *inp; 2629b8b58e0SJonathan Lemon #ifdef TCPDEBUG 2639b8b58e0SJonathan Lemon int ostate; 2649b8b58e0SJonathan Lemon 2659b8b58e0SJonathan Lemon ostate = tp->t_state; 2669b8b58e0SJonathan Lemon #endif 2679b8b58e0SJonathan Lemon s = splnet(); 268f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 269f76fcf6dSJeffrey Hsu inp = tp->t_inpcb; 270abe239cfSJeffrey Hsu if (!inp) { 271abe239cfSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 272abe239cfSJeffrey Hsu splx(s); 273abe239cfSJeffrey Hsu return; 274abe239cfSJeffrey Hsu } 275f76fcf6dSJeffrey Hsu INP_LOCK(inp); 276af1270f8SJonathan Lemon if (callout_pending(tp->tt_keep) || !callout_active(tp->tt_keep)) { 277f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 278f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2799b8b58e0SJonathan Lemon splx(s); 2809b8b58e0SJonathan Lemon return; 2819b8b58e0SJonathan Lemon } 2829b8b58e0SJonathan Lemon callout_deactivate(tp->tt_keep); 2839b8b58e0SJonathan Lemon /* 2849b8b58e0SJonathan Lemon * Keep-alive timer went off; send something 2859b8b58e0SJonathan Lemon * or drop connection if idle for too long. 2869b8b58e0SJonathan Lemon */ 2879b8b58e0SJonathan Lemon tcpstat.tcps_keeptimeo++; 2889b8b58e0SJonathan Lemon if (tp->t_state < TCPS_ESTABLISHED) 2899b8b58e0SJonathan Lemon goto dropit; 2909b8b58e0SJonathan Lemon if ((always_keepalive || 2919b8b58e0SJonathan Lemon tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) && 2929b8b58e0SJonathan Lemon tp->t_state <= TCPS_CLOSING) { 2939b8b58e0SJonathan Lemon if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle) 2949b8b58e0SJonathan Lemon goto dropit; 2959b8b58e0SJonathan Lemon /* 2969b8b58e0SJonathan Lemon * Send a packet designed to force a response 2979b8b58e0SJonathan Lemon * if the peer is up and reachable: 2989b8b58e0SJonathan Lemon * either an ACK if the connection is still alive, 2999b8b58e0SJonathan Lemon * or an RST if the peer has closed the connection 3009b8b58e0SJonathan Lemon * due to timeout or reboot. 3019b8b58e0SJonathan Lemon * Using sequence number tp->snd_una-1 3029b8b58e0SJonathan Lemon * causes the transmitted zero-length segment 3039b8b58e0SJonathan Lemon * to lie outside the receive window; 3049b8b58e0SJonathan Lemon * by the protocol spec, this requires the 3059b8b58e0SJonathan Lemon * correspondent TCP to respond. 3069b8b58e0SJonathan Lemon */ 3079b8b58e0SJonathan Lemon tcpstat.tcps_keepprobe++; 30879909384SJonathan Lemon t_template = tcpip_maketemplate(inp); 30908517d53SMike Silbersack if (t_template) { 31008517d53SMike Silbersack tcp_respond(tp, t_template->tt_ipgen, 31108517d53SMike Silbersack &t_template->tt_t, (struct mbuf *)NULL, 3129b8b58e0SJonathan Lemon tp->rcv_nxt, tp->snd_una - 1, 0); 31308517d53SMike Silbersack (void) m_free(dtom(t_template)); 31408517d53SMike Silbersack } 3159b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp); 3164cc20ab1SSeigo Tanimura } else 3179b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 3189b8b58e0SJonathan Lemon 3199b8b58e0SJonathan Lemon #ifdef TCPDEBUG 3204cc20ab1SSeigo Tanimura if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 321fb59c426SYoshinobu Inoue tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 3229b8b58e0SJonathan Lemon PRU_SLOWTIMO); 3239b8b58e0SJonathan Lemon #endif 324f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 325f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 3269b8b58e0SJonathan Lemon splx(s); 3279b8b58e0SJonathan Lemon return; 3289b8b58e0SJonathan Lemon 3299b8b58e0SJonathan Lemon dropit: 3309b8b58e0SJonathan Lemon tcpstat.tcps_keepdrops++; 3319b8b58e0SJonathan Lemon tp = tcp_drop(tp, ETIMEDOUT); 3329b8b58e0SJonathan Lemon 3339b8b58e0SJonathan Lemon #ifdef TCPDEBUG 3344cc20ab1SSeigo Tanimura if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 335fb59c426SYoshinobu Inoue tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 3369b8b58e0SJonathan Lemon PRU_SLOWTIMO); 3379b8b58e0SJonathan Lemon #endif 338f76fcf6dSJeffrey Hsu if (tp) 339f76fcf6dSJeffrey Hsu INP_UNLOCK(tp->t_inpcb); 340f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 3419b8b58e0SJonathan Lemon splx(s); 3429b8b58e0SJonathan Lemon } 3439b8b58e0SJonathan Lemon 3449b8b58e0SJonathan Lemon void 3459b8b58e0SJonathan Lemon tcp_timer_persist(xtp) 3469b8b58e0SJonathan Lemon void *xtp; 3479b8b58e0SJonathan Lemon { 3489b8b58e0SJonathan Lemon struct tcpcb *tp = xtp; 3499b8b58e0SJonathan Lemon int s; 350f76fcf6dSJeffrey Hsu struct inpcb *inp; 3519b8b58e0SJonathan Lemon #ifdef TCPDEBUG 3529b8b58e0SJonathan Lemon int ostate; 3539b8b58e0SJonathan Lemon 3549b8b58e0SJonathan Lemon ostate = tp->t_state; 3559b8b58e0SJonathan Lemon #endif 3569b8b58e0SJonathan Lemon s = splnet(); 357f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 358f76fcf6dSJeffrey Hsu inp = tp->t_inpcb; 359abe239cfSJeffrey Hsu if (!inp) { 360abe239cfSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 361abe239cfSJeffrey Hsu splx(s); 362abe239cfSJeffrey Hsu return; 363abe239cfSJeffrey Hsu } 364f76fcf6dSJeffrey Hsu INP_LOCK(inp); 365af1270f8SJonathan Lemon if (callout_pending(tp->tt_persist) || !callout_active(tp->tt_persist)){ 366f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 367f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 3689b8b58e0SJonathan Lemon splx(s); 3699b8b58e0SJonathan Lemon return; 3709b8b58e0SJonathan Lemon } 3719b8b58e0SJonathan Lemon callout_deactivate(tp->tt_persist); 3729b8b58e0SJonathan Lemon /* 3739b8b58e0SJonathan Lemon * Persistance timer into zero window. 3749b8b58e0SJonathan Lemon * Force a byte to be output, if possible. 3759b8b58e0SJonathan Lemon */ 3769b8b58e0SJonathan Lemon tcpstat.tcps_persisttimeo++; 3779b8b58e0SJonathan Lemon /* 3789b8b58e0SJonathan Lemon * Hack: if the peer is dead/unreachable, we do not 3799b8b58e0SJonathan Lemon * time out if the window is closed. After a full 3809b8b58e0SJonathan Lemon * backoff, drop the connection if the idle time 3819b8b58e0SJonathan Lemon * (no responses to probes) reaches the maximum 3829b8b58e0SJonathan Lemon * backoff that we would use if retransmitting. 3839b8b58e0SJonathan Lemon */ 3849b8b58e0SJonathan Lemon if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 3859b8b58e0SJonathan Lemon ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle || 3869b8b58e0SJonathan Lemon (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) { 3879b8b58e0SJonathan Lemon tcpstat.tcps_persistdrop++; 3889b8b58e0SJonathan Lemon tp = tcp_drop(tp, ETIMEDOUT); 3899b8b58e0SJonathan Lemon goto out; 3909b8b58e0SJonathan Lemon } 3919b8b58e0SJonathan Lemon tcp_setpersist(tp); 3929b8b58e0SJonathan Lemon tp->t_force = 1; 3939b8b58e0SJonathan Lemon (void) tcp_output(tp); 3949b8b58e0SJonathan Lemon tp->t_force = 0; 3959b8b58e0SJonathan Lemon 3969b8b58e0SJonathan Lemon out: 3979b8b58e0SJonathan Lemon #ifdef TCPDEBUG 3984cc20ab1SSeigo Tanimura if (tp && tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 399fb59c426SYoshinobu Inoue tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 4009b8b58e0SJonathan Lemon PRU_SLOWTIMO); 4019b8b58e0SJonathan Lemon #endif 402f76fcf6dSJeffrey Hsu if (tp) 403f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 404f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 4059b8b58e0SJonathan Lemon splx(s); 4069b8b58e0SJonathan Lemon } 4079b8b58e0SJonathan Lemon 4089b8b58e0SJonathan Lemon void 4099b8b58e0SJonathan Lemon tcp_timer_rexmt(xtp) 4109b8b58e0SJonathan Lemon void *xtp; 4119b8b58e0SJonathan Lemon { 4129b8b58e0SJonathan Lemon struct tcpcb *tp = xtp; 4139b8b58e0SJonathan Lemon int s; 4149b8b58e0SJonathan Lemon int rexmt; 415f76fcf6dSJeffrey Hsu int headlocked; 416f76fcf6dSJeffrey Hsu struct inpcb *inp; 4179b8b58e0SJonathan Lemon #ifdef TCPDEBUG 4189b8b58e0SJonathan Lemon int ostate; 4199b8b58e0SJonathan Lemon 4209b8b58e0SJonathan Lemon ostate = tp->t_state; 4219b8b58e0SJonathan Lemon #endif 4229b8b58e0SJonathan Lemon s = splnet(); 423f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 424f76fcf6dSJeffrey Hsu headlocked = 1; 425f76fcf6dSJeffrey Hsu inp = tp->t_inpcb; 426abe239cfSJeffrey Hsu if (!inp) { 427abe239cfSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 428abe239cfSJeffrey Hsu splx(s); 429abe239cfSJeffrey Hsu return; 430abe239cfSJeffrey Hsu } 431f76fcf6dSJeffrey Hsu INP_LOCK(inp); 432af1270f8SJonathan Lemon if (callout_pending(tp->tt_rexmt) || !callout_active(tp->tt_rexmt)) { 433f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 434f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 4359b8b58e0SJonathan Lemon splx(s); 4369b8b58e0SJonathan Lemon return; 4379b8b58e0SJonathan Lemon } 4389b8b58e0SJonathan Lemon callout_deactivate(tp->tt_rexmt); 439df8bae1dSRodney W. Grimes /* 440df8bae1dSRodney W. Grimes * Retransmission timer went off. Message has not 441df8bae1dSRodney W. Grimes * been acked within retransmit interval. Back off 442df8bae1dSRodney W. Grimes * to a longer retransmit interval and retransmit one segment. 443df8bae1dSRodney W. Grimes */ 444df8bae1dSRodney W. Grimes if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { 445df8bae1dSRodney W. Grimes tp->t_rxtshift = TCP_MAXRXTSHIFT; 446df8bae1dSRodney W. Grimes tcpstat.tcps_timeoutdrop++; 447df8bae1dSRodney W. Grimes tp = tcp_drop(tp, tp->t_softerror ? 448df8bae1dSRodney W. Grimes tp->t_softerror : ETIMEDOUT); 4499b8b58e0SJonathan Lemon goto out; 4509b8b58e0SJonathan Lemon } 451f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 452f76fcf6dSJeffrey Hsu headlocked = 0; 4539b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1) { 4549b8b58e0SJonathan Lemon /* 4559b8b58e0SJonathan Lemon * first retransmit; record ssthresh and cwnd so they can 4569b8b58e0SJonathan Lemon * be recovered if this turns out to be a "bad" retransmit. 4579b8b58e0SJonathan Lemon * A retransmit is considered "bad" if an ACK for this 4589b8b58e0SJonathan Lemon * segment is received within RTT/2 interval; the assumption 4599b8b58e0SJonathan Lemon * here is that the ACK was already in flight. See 4609b8b58e0SJonathan Lemon * "On Estimating End-to-End Network Path Properties" by 4619b8b58e0SJonathan Lemon * Allman and Paxson for more details. 4629b8b58e0SJonathan Lemon */ 4639b8b58e0SJonathan Lemon tp->snd_cwnd_prev = tp->snd_cwnd; 4649b8b58e0SJonathan Lemon tp->snd_ssthresh_prev = tp->snd_ssthresh; 465cb942153SJeffrey Hsu tp->snd_high_prev = tp->snd_high; 4669b8b58e0SJonathan Lemon tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1)); 467df8bae1dSRodney W. Grimes } 468df8bae1dSRodney W. Grimes tcpstat.tcps_rexmttimeo++; 4697d42e30cSJonathan Lemon if (tp->t_state == TCPS_SYN_SENT) 4707d42e30cSJonathan Lemon rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift]; 4717d42e30cSJonathan Lemon else 472df8bae1dSRodney W. Grimes rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 473df8bae1dSRodney W. Grimes TCPT_RANGESET(tp->t_rxtcur, rexmt, 474df8bae1dSRodney W. Grimes tp->t_rttmin, TCPTV_REXMTMAX); 475df8bae1dSRodney W. Grimes /* 4767ceb7783SJesper Skriver * Disable rfc1323 and rfc1644 if we havn't got any response to 4777ceb7783SJesper Skriver * our third SYN to work-around some broken terminal servers 4787ceb7783SJesper Skriver * (most of which have hopefully been retired) that have bad VJ 4797ceb7783SJesper Skriver * header compression code which trashes TCP segments containing 4807ceb7783SJesper Skriver * unknown-to-them TCP options. 4817ceb7783SJesper Skriver */ 4827ceb7783SJesper Skriver if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3)) 4837ceb7783SJesper Skriver tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_REQ_CC); 4847ceb7783SJesper Skriver /* 485df8bae1dSRodney W. Grimes * If losing, let the lower level know and try for 486df8bae1dSRodney W. Grimes * a better route. Also, if we backed off this far, 487df8bae1dSRodney W. Grimes * our srtt estimate is probably bogus. Clobber it 488df8bae1dSRodney W. Grimes * so we'll take the next rtt measurement as our srtt; 489df8bae1dSRodney W. Grimes * move the current srtt into rttvar to keep the current 490df8bae1dSRodney W. Grimes * retransmit times until then. 491df8bae1dSRodney W. Grimes */ 492df8bae1dSRodney W. Grimes if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 493fb59c426SYoshinobu Inoue #ifdef INET6 494fb59c426SYoshinobu Inoue if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 495fb59c426SYoshinobu Inoue in6_losing(tp->t_inpcb); 496fb59c426SYoshinobu Inoue else 497fb59c426SYoshinobu Inoue #endif 498df8bae1dSRodney W. Grimes in_losing(tp->t_inpcb); 499df8bae1dSRodney W. Grimes tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 500df8bae1dSRodney W. Grimes tp->t_srtt = 0; 501df8bae1dSRodney W. Grimes } 502df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 503cb942153SJeffrey Hsu tp->snd_high = tp->snd_max; 50446f58482SJonathan Lemon /* 50574b48c1dSAndras Olah * Force a segment to be sent. 50674b48c1dSAndras Olah */ 50774b48c1dSAndras Olah tp->t_flags |= TF_ACKNOW; 50874b48c1dSAndras Olah /* 509df8bae1dSRodney W. Grimes * If timing a segment in this window, stop the timer. 510df8bae1dSRodney W. Grimes */ 5119b8b58e0SJonathan Lemon tp->t_rtttime = 0; 512df8bae1dSRodney W. Grimes /* 513df8bae1dSRodney W. Grimes * Close the congestion window down to one segment 514df8bae1dSRodney W. Grimes * (we'll open it by one segment for each ack we get). 515df8bae1dSRodney W. Grimes * Since we probably have a window's worth of unacked 516df8bae1dSRodney W. Grimes * data accumulated, this "slow start" keeps us from 517df8bae1dSRodney W. Grimes * dumping all that data as back-to-back packets (which 518df8bae1dSRodney W. Grimes * might overwhelm an intermediate gateway). 519df8bae1dSRodney W. Grimes * 520df8bae1dSRodney W. Grimes * There are two phases to the opening: Initially we 521df8bae1dSRodney W. Grimes * open by one mss on each ack. This makes the window 522df8bae1dSRodney W. Grimes * size increase exponentially with time. If the 523df8bae1dSRodney W. Grimes * window is larger than the path can handle, this 524df8bae1dSRodney W. Grimes * exponential growth results in dropped packet(s) 525df8bae1dSRodney W. Grimes * almost immediately. To get more time between 526df8bae1dSRodney W. Grimes * drops but still "push" the network to take advantage 527df8bae1dSRodney W. Grimes * of improving conditions, we switch from exponential 528df8bae1dSRodney W. Grimes * to linear window opening at some threshhold size. 529df8bae1dSRodney W. Grimes * For a threshhold, we use half the current window 530df8bae1dSRodney W. Grimes * size, truncated to a multiple of the mss. 531df8bae1dSRodney W. Grimes * 532df8bae1dSRodney W. Grimes * (the minimum cwnd that will give us exponential 533df8bae1dSRodney W. Grimes * growth is 2 mss. We don't allow the threshhold 534df8bae1dSRodney W. Grimes * to go below this.) 535df8bae1dSRodney W. Grimes */ 536df8bae1dSRodney W. Grimes { 537df8bae1dSRodney W. Grimes u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg; 538df8bae1dSRodney W. Grimes if (win < 2) 539df8bae1dSRodney W. Grimes win = 2; 540df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 541df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 542df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 543df8bae1dSRodney W. Grimes } 544df8bae1dSRodney W. Grimes (void) tcp_output(tp); 545df8bae1dSRodney W. Grimes 5469b8b58e0SJonathan Lemon out: 5479b8b58e0SJonathan Lemon #ifdef TCPDEBUG 5484cc20ab1SSeigo Tanimura if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 549fb59c426SYoshinobu Inoue tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 5509b8b58e0SJonathan Lemon PRU_SLOWTIMO); 551df8bae1dSRodney W. Grimes #endif 552f76fcf6dSJeffrey Hsu if (tp) 553f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 554f76fcf6dSJeffrey Hsu if (headlocked) 555f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 5569b8b58e0SJonathan Lemon splx(s); 557df8bae1dSRodney W. Grimes } 558