123191Smckusick /* 244380Skarels * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California. 332788Sbostic * All rights reserved. 423191Smckusick * 544486Sbostic * %sccs.include.redist.c% 632788Sbostic * 7*57433Sandrew * @(#)tcp_output.c 7.25 (Berkeley) 01/08/93 823191Smckusick */ 94677Swnj 1056531Sbostic #include <sys/param.h> 1156531Sbostic #include <sys/systm.h> 1256531Sbostic #include <sys/malloc.h> 1356531Sbostic #include <sys/mbuf.h> 1456531Sbostic #include <sys/protosw.h> 1556531Sbostic #include <sys/socket.h> 1656531Sbostic #include <sys/socketvar.h> 1756531Sbostic #include <sys/errno.h> 1810895Ssam 1956531Sbostic #include <net/route.h> 2010895Ssam 2156531Sbostic #include <netinet/in.h> 2256531Sbostic #include <netinet/in_systm.h> 2356531Sbostic #include <netinet/ip.h> 2456531Sbostic #include <netinet/in_pcb.h> 2556531Sbostic #include <netinet/ip_var.h> 2656531Sbostic #include <netinet/tcp.h> 275088Swnj #define TCPOUTFLAGS 2856531Sbostic #include <netinet/tcp_fsm.h> 2956531Sbostic #include <netinet/tcp_seq.h> 3056531Sbostic #include <netinet/tcp_timer.h> 3156531Sbostic #include <netinet/tcp_var.h> 3256531Sbostic #include <netinet/tcpip.h> 3356531Sbostic #include <netinet/tcp_debug.h> 344677Swnj 3544380Skarels #ifdef notyet 3644380Skarels extern struct mbuf *m_copypack(); 3744380Skarels #endif 3844380Skarels 395441Swnj 40*57433Sandrew #define MAX_TCPOPTLEN 32 /* max # bytes that go in options */ 41*57433Sandrew 425441Swnj /* 435245Sroot * Tcp output routine: figure out what should be sent and send it. 444678Swnj */ 455075Swnj tcp_output(tp) 465075Swnj register struct tcpcb *tp; 474678Swnj { 485075Swnj register struct socket *so = tp->t_inpcb->inp_socket; 4937317Skarels register long len, win; 5025940Skarels int off, flags, error; 515075Swnj register struct mbuf *m; 525075Swnj register struct tcpiphdr *ti; 53*57433Sandrew u_char opt[MAX_TCPOPTLEN]; 5444380Skarels unsigned optlen, hdrlen; 5525940Skarels int idle, sendalot; 564678Swnj 575075Swnj /* 586279Swnj * Determine length of data that should be transmitted, 595088Swnj * and flags that will be used. 605088Swnj * If there is some data or critical controls (SYN, RST) 615088Swnj * to send, then transmit; otherwise, investigate further. 625075Swnj */ 6325940Skarels idle = (tp->snd_max == tp->snd_una); 6444380Skarels if (idle && tp->t_idle >= tp->t_rxtcur) 6544380Skarels /* 6644380Skarels * We have been idle for "a while" and no acks are 6744380Skarels * expected to clock out any data we send -- 6844380Skarels * slow start to get ack "clock" running again. 6944380Skarels */ 7044380Skarels tp->snd_cwnd = tp->t_maxseg; 717125Swnj again: 727125Swnj sendalot = 0; 735075Swnj off = tp->snd_nxt - tp->snd_una; 7437317Skarels win = min(tp->snd_wnd, tp->snd_cwnd); 7526834Skarels 7621116Skarels /* 7721116Skarels * If in persist timeout with window of 0, send 1 byte. 7825940Skarels * Otherwise, if window is small but nonzero 7925940Skarels * and timer expired, we will send what we can 8025940Skarels * and go to transmit state. 8121116Skarels */ 8221116Skarels if (tp->t_force) { 8326834Skarels if (win == 0) 8421116Skarels win = 1; 8521116Skarels else { 8621116Skarels tp->t_timer[TCPT_PERSIST] = 0; 8721116Skarels tp->t_rxtshift = 0; 8821116Skarels } 8921116Skarels } 9025940Skarels 9144380Skarels flags = tcp_outflags[tp->t_state]; 9237317Skarels len = min(so->so_snd.sb_cc, win) - off; 9325940Skarels 9426834Skarels if (len < 0) { 9526834Skarels /* 9627048Skarels * If FIN has been sent but not acked, 9727048Skarels * but we haven't been called to retransmit, 9832785Skarels * len will be -1. Otherwise, window shrank 9932785Skarels * after we sent into it. If window shrank to 0, 10032785Skarels * cancel pending retransmit and pull snd_nxt 10132785Skarels * back to (closed) window. We will enter persist 10232785Skarels * state below. If the window didn't close completely, 10327048Skarels * just wait for an ACK. 10426834Skarels */ 10532785Skarels len = 0; 10632785Skarels if (win == 0) { 10727067Skarels tp->t_timer[TCPT_REXMT] = 0; 10827067Skarels tp->snd_nxt = tp->snd_una; 10932785Skarels } 11026834Skarels } 11127067Skarels if (len > tp->t_maxseg) { 11227067Skarels len = tp->t_maxseg; 11331442Skarels sendalot = 1; 11427067Skarels } 11529795Skarels if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 11629795Skarels flags &= ~TH_FIN; 11744380Skarels 11826834Skarels win = sbspace(&so->so_rcv); 11926834Skarels 12025940Skarels /* 12117318Skarels * Sender silly window avoidance. If connection is idle 12217318Skarels * and can send all data, a maximum segment, 12317318Skarels * at least a maximum default-size segment do it, 1246279Swnj * or are forced, do it; otherwise don't bother. 12525261Skarels * If peer's buffer is tiny, then send 12625261Skarels * when window is at least half open. 12721116Skarels * If retransmitting (possibly after persist timer forced us 12821116Skarels * to send into a small window), then must resend. 1296279Swnj */ 1306279Swnj if (len) { 13131725Skarels if (len == tp->t_maxseg) 1326279Swnj goto send; 13325940Skarels if ((idle || tp->t_flags & TF_NODELAY) && 13425940Skarels len + off >= so->so_snd.sb_cc) 1356279Swnj goto send; 1366279Swnj if (tp->t_force) 1376279Swnj goto send; 13825261Skarels if (len >= tp->max_sndwnd / 2) 13925261Skarels goto send; 14021116Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 14121116Skarels goto send; 14226834Skarels } 1436279Swnj 1445441Swnj /* 14525940Skarels * Compare available window to amount of window 14625940Skarels * known to peer (as advertised window less 14732785Skarels * next expected input). If the difference is at least two 14840685Skarels * max size segments, or at least 50% of the maximum possible 14932785Skarels * window, then want to send a window update to peer. 1505075Swnj */ 15132034Skarels if (win > 0) { 152*57433Sandrew /* 153*57433Sandrew * "adv" is the amount we can increase the window, 154*57433Sandrew * taking into account that we are limited by 155*57433Sandrew * TCP_MAXWIN << tp->rcv_scale. 156*57433Sandrew */ 157*57433Sandrew long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) - 158*57433Sandrew (tp->rcv_adv - tp->rcv_nxt); 1594678Swnj 16045169Skarels if (adv >= (long) (2 * tp->t_maxseg)) 16132785Skarels goto send; 16245169Skarels if (2 * adv >= (long) so->so_rcv.sb_hiwat) 16332034Skarels goto send; 16432034Skarels } 16532034Skarels 1665075Swnj /* 16744380Skarels * Send if we owe peer an ACK. 16844380Skarels */ 16944380Skarels if (tp->t_flags & TF_ACKNOW) 17044380Skarels goto send; 17144380Skarels if (flags & (TH_SYN|TH_RST)) 17244380Skarels goto send; 17344380Skarels if (SEQ_GT(tp->snd_up, tp->snd_una)) 17444380Skarels goto send; 17544380Skarels /* 17644380Skarels * If our state indicates that FIN should be sent 17744380Skarels * and we have not yet done so, or we're retransmitting the FIN, 17844380Skarels * then we need to send. 17944380Skarels */ 18044380Skarels if (flags & TH_FIN && 18144380Skarels ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 18244380Skarels goto send; 18344380Skarels 18444380Skarels /* 1857125Swnj * TCP window updates are not reliable, rather a polling protocol 1867125Swnj * using ``persist'' packets is used to insure receipt of window 1877125Swnj * updates. The three ``states'' for the output side are: 1887125Swnj * idle not doing retransmits or persists 18925940Skarels * persisting to move a small or zero window 1907125Swnj * (re)transmitting and thereby not persisting 1917125Swnj * 1927125Swnj * tp->t_timer[TCPT_PERSIST] 1937125Swnj * is set when we are in persist state. 1947125Swnj * tp->t_force 1957125Swnj * is set when we are called to send a persist packet. 1967125Swnj * tp->t_timer[TCPT_REXMT] 1977125Swnj * is set when we are retransmitting 1987125Swnj * The output side is idle when both timers are zero. 1997125Swnj * 20021116Skarels * If send window is too small, there is data to transmit, and no 20121116Skarels * retransmit or persist is pending, then go to persist state. 20221116Skarels * If nothing happens soon, send when timer expires: 20321116Skarels * if window is nonzero, transmit what we can, 20421116Skarels * otherwise force out a byte. 2057125Swnj */ 20621116Skarels if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 && 20721116Skarels tp->t_timer[TCPT_PERSIST] == 0) { 2087125Swnj tp->t_rxtshift = 0; 2097125Swnj tcp_setpersist(tp); 2107125Swnj } 2117125Swnj 2127125Swnj /* 2135075Swnj * No reason to send a segment, just return. 2145075Swnj */ 2155110Swnj return (0); 2164678Swnj 2175075Swnj send: 2185075Swnj /* 21944380Skarels * Before ESTABLISHED, force sending of initial options 22044380Skarels * unless TCP set not to do any options. 22144380Skarels * NOTE: we assume that the IP/TCP header plus TCP options 22244380Skarels * always fit in a single mbuf, leaving room for a maximum 22344380Skarels * link header, i.e. 22444380Skarels * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN 22544380Skarels */ 22644380Skarels optlen = 0; 22744380Skarels hdrlen = sizeof (struct tcpiphdr); 22844380Skarels if (flags & TH_SYN && (tp->t_flags & TF_NOOPT) == 0) { 22952515Storek u_short mss; 23052515Storek 231*57433Sandrew opt[0] = TCPOPT_MAXSEG; 232*57433Sandrew opt[1] = 4; 23352515Storek mss = htons((u_short) tcp_mss(tp, 0)); 23452515Storek bcopy((caddr_t)&mss, (caddr_t)(opt + 2), sizeof(mss)); 235*57433Sandrew optlen = 4; 236*57433Sandrew 237*57433Sandrew if ((tp->t_flags & TF_REQ_SCALE) && 238*57433Sandrew ((flags & TH_ACK) == 0 || (tp->t_flags & TF_RCVD_SCALE))) { 239*57433Sandrew *((u_long *) (opt + optlen)) = htonl( 240*57433Sandrew TCPOPT_NOP<<24 | 241*57433Sandrew TCPOPT_WINDOW<<16 | 242*57433Sandrew TCPOLEN_WINDOW<<8 | 243*57433Sandrew tp->request_r_scale); 244*57433Sandrew optlen += 4; 245*57433Sandrew } 246*57433Sandrew 247*57433Sandrew #ifdef DO_SACK 248*57433Sandrew /* Send a SACK_PERMITTED option in the SYN segment. */ 249*57433Sandrew *((u_long *) (opt + optlen)) = htonl( 250*57433Sandrew TCPOPT_NOP<<24 | 251*57433Sandrew TCPOPT_NOP<<16 | 252*57433Sandrew TCPOPT_SACK_PERMITTED<<8 | 253*57433Sandrew TCPOLEN_SACK_PERMITTED); 254*57433Sandrew optlen += 4; 255*57433Sandrew #endif 256*57433Sandrew } 257*57433Sandrew 258*57433Sandrew /* 259*57433Sandrew * Send a timestamp and echo-reply if this is a SYN and our side 260*57433Sandrew * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 261*57433Sandrew * and our peer have sent timestamps in our SYN's. 262*57433Sandrew */ 263*57433Sandrew if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 264*57433Sandrew (flags & TH_RST) == 0 && 265*57433Sandrew ((flags & (TH_SYN|TH_ACK)) == TH_SYN || 266*57433Sandrew (tp->t_flags & TF_RCVD_TSTMP))) { 267*57433Sandrew u_long *lp = (u_long *)(opt + optlen); 268*57433Sandrew 269*57433Sandrew /* Form timestamp option as shown in appendix A of RFC 1323. */ 270*57433Sandrew *lp++ = htonl(TCPOPT_TSTAMP_HDR); 271*57433Sandrew *lp++ = htonl(tcp_now); 272*57433Sandrew *lp = htonl(tp->ts_recent); 273*57433Sandrew optlen += TCPOLEN_TSTAMP_APPA; 274*57433Sandrew } 275*57433Sandrew 276*57433Sandrew #ifdef DO_SACK 277*57433Sandrew /* Send SACK if needed. Don't tack a SACK onto a non-empty segment. */ 278*57433Sandrew if (tp->seg_next != (struct tcpiphdr *)tp && len == 0 && 279*57433Sandrew (tp->t_flags & (TF_SACK_PERMIT|TF_NOOPT)) == TF_SACK_PERMIT) { 280*57433Sandrew 281*57433Sandrew register struct tcpiphdr *q = tp->seg_next; 282*57433Sandrew register u_long *optl = (u_long *)(opt + optlen) + 1; 283*57433Sandrew tcp_seq block_start; 284*57433Sandrew u_long block_size; 285*57433Sandrew int sack_len = 2; 286*57433Sandrew 287*57433Sandrew /* 288*57433Sandrew * Use these to gather runs of received segments. 289*57433Sandrew * The first segment in the queue becomes the initial run. 290*57433Sandrew */ 291*57433Sandrew block_start = q->ti_seq; 292*57433Sandrew block_size = q->ti_len; 293*57433Sandrew 294*57433Sandrew do { 295*57433Sandrew q = (struct tcpiphdr *) q->ti_next; 296*57433Sandrew if (q == (struct tcpiphdr *) tp || 297*57433Sandrew block_start + block_size != q->ti_seq) { 298*57433Sandrew 299*57433Sandrew /* 300*57433Sandrew * Stick the relative origin and block size 301*57433Sandrew * in the SACK option. 302*57433Sandrew */ 303*57433Sandrew *optl++ = htonl(block_start-tp->rcv_nxt); 304*57433Sandrew *optl++ = htonl(block_size); 305*57433Sandrew sack_len += 8; 306*57433Sandrew 307*57433Sandrew /* If no more will fit into options, quit. */ 308*57433Sandrew if (sack_len + optlen > MAX_TCPOPTLEN - 8) 309*57433Sandrew break; 310*57433Sandrew 311*57433Sandrew if (q != (struct tcpiphdr *) tp) { 312*57433Sandrew block_start = q->ti_seq; 313*57433Sandrew block_size = q->ti_len; 314*57433Sandrew } 315*57433Sandrew } else { 316*57433Sandrew /* 317*57433Sandrew * This segment just accumulates into previous 318*57433Sandrew * run. 319*57433Sandrew */ 320*57433Sandrew block_size += q->ti_len; 321*57433Sandrew } 322*57433Sandrew } while (q != (struct tcpiphdr *) tp); 323*57433Sandrew 324*57433Sandrew *((u_long *) (opt + optlen)) = htonl(TCPOPT_NOP << 24 | 325*57433Sandrew TCPOPT_NOP << 16 | TCPOPT_SACK << 8 | sack_len); 326*57433Sandrew optlen += sack_len + 2; 327*57433Sandrew } 328*57433Sandrew #endif 329*57433Sandrew hdrlen += optlen; 330*57433Sandrew /* 331*57433Sandrew * Adjust data length if insertion of options will 332*57433Sandrew * bump the packet length beyond the t_maxseg length. 333*57433Sandrew */ 334*57433Sandrew if (len > tp->t_maxseg - optlen) { 335*57433Sandrew len = tp->t_maxseg - optlen; 336*57433Sandrew sendalot = 1; 337*57433Sandrew } 338*57433Sandrew 339*57433Sandrew 34044380Skarels #ifdef DIAGNOSTIC 341*57433Sandrew if (max_linkhdr + hdrlen > MHLEN) 342*57433Sandrew panic("tcphdr too big"); 34344380Skarels #endif 34444380Skarels 34544380Skarels /* 3465075Swnj * Grab a header mbuf, attaching a copy of data to 3475075Swnj * be transmitted, and initialize the header from 3485075Swnj * the template for sends on this connection. 3495075Swnj */ 3505075Swnj if (len) { 35131442Skarels if (tp->t_force && len == 1) 35231442Skarels tcpstat.tcps_sndprobe++; 35331442Skarels else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 35431442Skarels tcpstat.tcps_sndrexmitpack++; 35531442Skarels tcpstat.tcps_sndrexmitbyte += len; 35631442Skarels } else { 35731442Skarels tcpstat.tcps_sndpack++; 35831442Skarels tcpstat.tcps_sndbyte += len; 35931442Skarels } 36044380Skarels #ifdef notyet 36144380Skarels if ((m = m_copypack(so->so_snd.sb_mb, off, 36244380Skarels (int)len, max_linkhdr + hdrlen)) == 0) { 36344380Skarels error = ENOBUFS; 36444380Skarels goto out; 36544380Skarels } 36644380Skarels /* 36744380Skarels * m_copypack left space for our hdr; use it. 36844380Skarels */ 36944380Skarels m->m_len += hdrlen; 37044380Skarels m->m_data -= hdrlen; 37144380Skarels #else 37244380Skarels MGETHDR(m, M_DONTWAIT, MT_HEADER); 37344380Skarels if (m == NULL) { 37444380Skarels error = ENOBUFS; 37544380Skarels goto out; 37644380Skarels } 37744380Skarels m->m_data += max_linkhdr; 37844380Skarels m->m_len = hdrlen; 37944380Skarels if (len <= MHLEN - hdrlen - max_linkhdr) { 38037317Skarels m_copydata(so->so_snd.sb_mb, off, (int) len, 38144380Skarels mtod(m, caddr_t) + hdrlen); 38237317Skarels m->m_len += len; 38337317Skarels } else { 38437317Skarels m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 38537317Skarels if (m->m_next == 0) 38637317Skarels len = 0; 38737317Skarels } 38844380Skarels #endif 38944380Skarels /* 39044380Skarels * If we're sending everything we've got, set PUSH. 39144380Skarels * (This will keep happy those implementations which only 39244380Skarels * give data to the user when a buffer fills or 39344380Skarels * a PUSH comes in.) 39444380Skarels */ 39544380Skarels if (off + len == so->so_snd.sb_cc) 39644380Skarels flags |= TH_PUSH; 39744380Skarels } else { 39844380Skarels if (tp->t_flags & TF_ACKNOW) 39944380Skarels tcpstat.tcps_sndacks++; 40044380Skarels else if (flags & (TH_SYN|TH_FIN|TH_RST)) 40144380Skarels tcpstat.tcps_sndctrl++; 40244380Skarels else if (SEQ_GT(tp->snd_up, tp->snd_una)) 40344380Skarels tcpstat.tcps_sndurg++; 40444380Skarels else 40544380Skarels tcpstat.tcps_sndwinup++; 40631442Skarels 40744380Skarels MGETHDR(m, M_DONTWAIT, MT_HEADER); 40844380Skarels if (m == NULL) { 40944380Skarels error = ENOBUFS; 41044380Skarels goto out; 41144380Skarels } 41244380Skarels m->m_data += max_linkhdr; 41344380Skarels m->m_len = hdrlen; 41444380Skarels } 41544380Skarels m->m_pkthdr.rcvif = (struct ifnet *)0; 41644380Skarels ti = mtod(m, struct tcpiphdr *); 4175075Swnj if (tp->t_template == 0) 4185075Swnj panic("tcp_output"); 4195110Swnj bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 4205075Swnj 4215075Swnj /* 4225075Swnj * Fill in fields, remembering maximum advertised 4235075Swnj * window for use in delaying messages about window sizes. 42427067Skarels * If resending a FIN, be sure not to use a new sequence number. 4255075Swnj */ 42631725Skarels if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 42731725Skarels tp->snd_nxt == tp->snd_max) 42827067Skarels tp->snd_nxt--; 42925940Skarels ti->ti_seq = htonl(tp->snd_nxt); 43025940Skarels ti->ti_ack = htonl(tp->rcv_nxt); 43144380Skarels if (optlen) { 43244380Skarels bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen); 4335441Swnj ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 4345088Swnj } 4355088Swnj ti->ti_flags = flags; 43625940Skarels /* 43725940Skarels * Calculate receive window. Don't shrink window, 43825940Skarels * but avoid silly window syndrome. 43925940Skarels */ 44033783Skarels if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) 44125940Skarels win = 0; 442*57433Sandrew if (win > (long)TCP_MAXWIN << tp->rcv_scale) 443*57433Sandrew win = (long)TCP_MAXWIN << tp->rcv_scale; 44436777Skarels if (win < (long)(tp->rcv_adv - tp->rcv_nxt)) 44536777Skarels win = (long)(tp->rcv_adv - tp->rcv_nxt); 446*57433Sandrew ti->ti_win = htons((u_short) (win>>tp->rcv_scale)); 4475088Swnj if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 44826387Skarels ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 4495075Swnj ti->ti_flags |= TH_URG; 4505075Swnj } else 4515075Swnj /* 4525075Swnj * If no urgent pointer to send, then we pull 4535075Swnj * the urgent pointer to the left edge of the send window 4545075Swnj * so that it doesn't drift into the send window on sequence 4555075Swnj * number wraparound. 4565075Swnj */ 4575088Swnj tp->snd_up = tp->snd_una; /* drag it along */ 4585075Swnj 4595075Swnj /* 4605075Swnj * Put TCP length in extended header, and then 4615075Swnj * checksum extended header and data. 4625075Swnj */ 46325940Skarels if (len + optlen) 46444380Skarels ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + 46525940Skarels optlen + len)); 46644380Skarels ti->ti_sum = in_cksum(m, (int)(hdrlen + len)); 4675075Swnj 4685075Swnj /* 4697125Swnj * In transmit state, time the transmission and arrange for 47021116Skarels * the retransmit. In persist state, just set snd_max. 4715088Swnj */ 47221116Skarels if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { 47331758Skarels tcp_seq startseq = tp->snd_nxt; 47431758Skarels 4757125Swnj /* 4767146Swnj * Advance snd_nxt over sequence space of this segment. 4777125Swnj */ 47844380Skarels if (flags & (TH_SYN|TH_FIN)) { 47944380Skarels if (flags & TH_SYN) 48044380Skarels tp->snd_nxt++; 48144380Skarels if (flags & TH_FIN) { 48244380Skarels tp->snd_nxt++; 48344380Skarels tp->t_flags |= TF_SENTFIN; 48444380Skarels } 48527067Skarels } 4867125Swnj tp->snd_nxt += len; 48731758Skarels if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 4887149Swnj tp->snd_max = tp->snd_nxt; 48931758Skarels /* 49031758Skarels * Time this transmission if not a retransmission and 49131758Skarels * not currently timing anything. 49231758Skarels */ 49331758Skarels if (tp->t_rtt == 0) { 49431758Skarels tp->t_rtt = 1; 49531758Skarels tp->t_rtseq = startseq; 49631758Skarels tcpstat.tcps_segstimed++; 49731758Skarels } 49831758Skarels } 4995088Swnj 5007125Swnj /* 50121116Skarels * Set retransmit timer if not currently set, 50226443Skarels * and not doing an ack or a keep-alive probe. 50331726Skarels * Initial value for retransmit timer is smoothed 50431726Skarels * round-trip time + 2 * round-trip time variance. 50526834Skarels * Initialize shift counter which is used for backoff 50626834Skarels * of retransmit time. 5077125Swnj */ 5087125Swnj if (tp->t_timer[TCPT_REXMT] == 0 && 5097125Swnj tp->snd_nxt != tp->snd_una) { 51032034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 51132034Skarels if (tp->t_timer[TCPT_PERSIST]) { 51232034Skarels tp->t_timer[TCPT_PERSIST] = 0; 51332034Skarels tp->t_rxtshift = 0; 51432034Skarels } 5157125Swnj } 51626443Skarels } else 51725940Skarels if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 51825940Skarels tp->snd_max = tp->snd_nxt + len; 5195163Swnj 5205163Swnj /* 5215268Sroot * Trace. 5225268Sroot */ 5237146Swnj if (so->so_options & SO_DEBUG) 5245268Sroot tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 5255268Sroot 5265268Sroot /* 5275075Swnj * Fill in IP length and desired time to live and 52844380Skarels * send to IP level. There should be a better way 52944380Skarels * to handle ttl and tos; we could keep them in 53044380Skarels * the template, but need a way to checksum without them. 5315075Swnj */ 53244380Skarels m->m_pkthdr.len = hdrlen + len; 53344380Skarels ((struct ip *)ti)->ip_len = m->m_pkthdr.len; 53444380Skarels ((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; /* XXX */ 53544380Skarels ((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos; /* XXX */ 53644380Skarels #if BSD >= 43 53726059Skarels error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 538*57433Sandrew so->so_options & SO_DONTROUTE, 0); 53944380Skarels #else 54044380Skarels error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route, 54144380Skarels so->so_options & SO_DONTROUTE); 54244380Skarels #endif 54333446Skarels if (error) { 54444380Skarels out: 54533446Skarels if (error == ENOBUFS) { 54633446Skarels tcp_quench(tp->t_inpcb); 54733446Skarels return (0); 54833446Skarels } 54944380Skarels if ((error == EHOSTUNREACH || error == ENETDOWN) 55044380Skarels && TCPS_HAVERCVDSYN(tp->t_state)) { 55144380Skarels tp->t_softerror = error; 55244380Skarels return (0); 55344380Skarels } 5546505Ssam return (error); 55533446Skarels } 55631442Skarels tcpstat.tcps_sndtotal++; 5575075Swnj 5585075Swnj /* 5595075Swnj * Data sent (as far as we can tell). 5605075Swnj * If this advertises a larger window than any other segment, 5615245Sroot * then remember the size of the advertised window. 56225940Skarels * Any pending ACK has now been sent. 5635075Swnj */ 5645252Sroot if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 5655075Swnj tp->rcv_adv = tp->rcv_nxt + win; 566*57433Sandrew tp->last_ack_sent = tp->rcv_nxt; 5675088Swnj tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 56825940Skarels if (sendalot) 5697125Swnj goto again; 5706505Ssam return (0); 5714677Swnj } 5727125Swnj 5737125Swnj tcp_setpersist(tp) 5747125Swnj register struct tcpcb *tp; 5757125Swnj { 57631726Skarels register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 5777125Swnj 5787125Swnj if (tp->t_timer[TCPT_REXMT]) 5797125Swnj panic("tcp_output REXMT"); 5807125Swnj /* 5817125Swnj * Start/restart persistance timer. 5827125Swnj */ 5837125Swnj TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 58431726Skarels t * tcp_backoff[tp->t_rxtshift], 58531725Skarels TCPTV_PERSMIN, TCPTV_PERSMAX); 58631725Skarels if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 58731725Skarels tp->t_rxtshift++; 5887125Swnj } 589