123191Smckusick /* 229150Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 323191Smckusick * All rights reserved. The Berkeley software License Agreement 423191Smckusick * specifies the terms and conditions for redistribution. 523191Smckusick * 6*32785Skarels * @(#)tcp_output.c 7.11 (Berkeley) 12/07/87 723191Smckusick */ 84677Swnj 917063Sbloom #include "param.h" 1017063Sbloom #include "systm.h" 1117063Sbloom #include "mbuf.h" 1217063Sbloom #include "protosw.h" 1317063Sbloom #include "socket.h" 1417063Sbloom #include "socketvar.h" 1517063Sbloom #include "errno.h" 1610895Ssam 1710895Ssam #include "../net/route.h" 1810895Ssam 1917063Sbloom #include "in.h" 2017063Sbloom #include "in_pcb.h" 2117063Sbloom #include "in_systm.h" 2217063Sbloom #include "ip.h" 2317063Sbloom #include "ip_var.h" 2417063Sbloom #include "tcp.h" 255088Swnj #define TCPOUTFLAGS 2617063Sbloom #include "tcp_fsm.h" 2717063Sbloom #include "tcp_seq.h" 2817063Sbloom #include "tcp_timer.h" 2917063Sbloom #include "tcp_var.h" 3017063Sbloom #include "tcpip.h" 3117063Sbloom #include "tcp_debug.h" 324677Swnj 334678Swnj /* 348314Sroot * Initial options. 355441Swnj */ 365441Swnj u_char tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, }; 375441Swnj 385441Swnj /* 395245Sroot * Tcp output routine: figure out what should be sent and send it. 404678Swnj */ 415075Swnj tcp_output(tp) 425075Swnj register struct tcpcb *tp; 434678Swnj { 445075Swnj register struct socket *so = tp->t_inpcb->inp_socket; 4525940Skarels register int len, win; 465075Swnj struct mbuf *m0; 4725940Skarels int off, flags, error; 485075Swnj register struct mbuf *m; 495075Swnj register struct tcpiphdr *ti; 505441Swnj u_char *opt; 515441Swnj unsigned optlen = 0; 5225940Skarels int idle, sendalot; 534678Swnj 545075Swnj /* 556279Swnj * Determine length of data that should be transmitted, 565088Swnj * and flags that will be used. 575088Swnj * If there is some data or critical controls (SYN, RST) 585088Swnj * to send, then transmit; otherwise, investigate further. 595075Swnj */ 6025940Skarels idle = (tp->snd_max == tp->snd_una); 617125Swnj again: 627125Swnj sendalot = 0; 635075Swnj off = tp->snd_nxt - tp->snd_una; 6421116Skarels win = MIN(tp->snd_wnd, tp->snd_cwnd); 6526834Skarels 6621116Skarels /* 6721116Skarels * If in persist timeout with window of 0, send 1 byte. 6825940Skarels * Otherwise, if window is small but nonzero 6925940Skarels * and timer expired, we will send what we can 7025940Skarels * and go to transmit state. 7121116Skarels */ 7221116Skarels if (tp->t_force) { 7326834Skarels if (win == 0) 7421116Skarels win = 1; 7521116Skarels else { 7621116Skarels tp->t_timer[TCPT_PERSIST] = 0; 7721116Skarels tp->t_rxtshift = 0; 7821116Skarels } 7921116Skarels } 8025940Skarels 8117361Skarels len = MIN(so->so_snd.sb_cc, win) - off; 825088Swnj flags = tcp_outflags[tp->t_state]; 8325940Skarels 8426834Skarels if (len < 0) { 8526834Skarels /* 8627048Skarels * If FIN has been sent but not acked, 8727048Skarels * but we haven't been called to retransmit, 88*32785Skarels * len will be -1. Otherwise, window shrank 89*32785Skarels * after we sent into it. If window shrank to 0, 90*32785Skarels * cancel pending retransmit and pull snd_nxt 91*32785Skarels * back to (closed) window. We will enter persist 92*32785Skarels * state below. If the window didn't close completely, 9327048Skarels * just wait for an ACK. 9426834Skarels */ 95*32785Skarels len = 0; 96*32785Skarels if (win == 0) { 9727067Skarels tp->t_timer[TCPT_REXMT] = 0; 9827067Skarels tp->snd_nxt = tp->snd_una; 99*32785Skarels } 10026834Skarels } 10127067Skarels if (len > tp->t_maxseg) { 10227067Skarels len = tp->t_maxseg; 10331442Skarels sendalot = 1; 10427067Skarels } 10529795Skarels if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 10629795Skarels flags &= ~TH_FIN; 10726834Skarels win = sbspace(&so->so_rcv); 10826834Skarels 10927067Skarels 11025940Skarels /* 11127067Skarels * If our state indicates that FIN should be sent 11227067Skarels * and we have not yet done so, or we're retransmitting the FIN, 11327067Skarels * then we need to send. 11427067Skarels */ 11527067Skarels if (flags & TH_FIN && 11627067Skarels ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 11727067Skarels goto send; 11827067Skarels /* 11925940Skarels * Send if we owe peer an ACK. 12025940Skarels */ 12126834Skarels if (tp->t_flags & TF_ACKNOW) 12225940Skarels goto send; 12327067Skarels if (flags & (TH_SYN|TH_RST)) 12426834Skarels goto send; 1256279Swnj if (SEQ_GT(tp->snd_up, tp->snd_una)) 1266279Swnj goto send; 1274678Swnj 1285075Swnj /* 12917318Skarels * Sender silly window avoidance. If connection is idle 13017318Skarels * and can send all data, a maximum segment, 13117318Skarels * at least a maximum default-size segment do it, 1326279Swnj * or are forced, do it; otherwise don't bother. 13325261Skarels * If peer's buffer is tiny, then send 13425261Skarels * when window is at least half open. 13521116Skarels * If retransmitting (possibly after persist timer forced us 13621116Skarels * to send into a small window), then must resend. 1376279Swnj */ 1386279Swnj if (len) { 13931725Skarels if (len == tp->t_maxseg) 1406279Swnj goto send; 14125940Skarels if ((idle || tp->t_flags & TF_NODELAY) && 14225940Skarels len + off >= so->so_snd.sb_cc) 1436279Swnj goto send; 1446279Swnj if (tp->t_force) 1456279Swnj goto send; 14625261Skarels if (len >= tp->max_sndwnd / 2) 14725261Skarels goto send; 14821116Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 14921116Skarels goto send; 15026834Skarels } 1516279Swnj 1525441Swnj /* 15325940Skarels * Compare available window to amount of window 15425940Skarels * known to peer (as advertised window less 155*32785Skarels * next expected input). If the difference is at least two 156*32785Skarels * max size segments or at least 35% of the maximum possible 157*32785Skarels * window, then want to send a window update to peer. 1585075Swnj */ 15932034Skarels if (win > 0) { 16032034Skarels int adv = win - (tp->rcv_adv - tp->rcv_nxt); 1614678Swnj 162*32785Skarels if (so->so_rcv.sb_cc == 0 && adv >= 2 * tp->t_maxseg) 163*32785Skarels goto send; 16432034Skarels if (100 * adv / so->so_rcv.sb_hiwat >= 35) 16532034Skarels goto send; 16632034Skarels } 16732034Skarels 1685075Swnj /* 1697125Swnj * TCP window updates are not reliable, rather a polling protocol 1707125Swnj * using ``persist'' packets is used to insure receipt of window 1717125Swnj * updates. The three ``states'' for the output side are: 1727125Swnj * idle not doing retransmits or persists 17325940Skarels * persisting to move a small or zero window 1747125Swnj * (re)transmitting and thereby not persisting 1757125Swnj * 1767125Swnj * tp->t_timer[TCPT_PERSIST] 1777125Swnj * is set when we are in persist state. 1787125Swnj * tp->t_force 1797125Swnj * is set when we are called to send a persist packet. 1807125Swnj * tp->t_timer[TCPT_REXMT] 1817125Swnj * is set when we are retransmitting 1827125Swnj * The output side is idle when both timers are zero. 1837125Swnj * 18421116Skarels * If send window is too small, there is data to transmit, and no 18521116Skarels * retransmit or persist is pending, then go to persist state. 18621116Skarels * If nothing happens soon, send when timer expires: 18721116Skarels * if window is nonzero, transmit what we can, 18821116Skarels * otherwise force out a byte. 1897125Swnj */ 19021116Skarels if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 && 19121116Skarels tp->t_timer[TCPT_PERSIST] == 0) { 1927125Swnj tp->t_rxtshift = 0; 1937125Swnj tcp_setpersist(tp); 1947125Swnj } 1957125Swnj 1967125Swnj /* 1975075Swnj * No reason to send a segment, just return. 1985075Swnj */ 1995110Swnj return (0); 2004678Swnj 2015075Swnj send: 2025075Swnj /* 2035075Swnj * Grab a header mbuf, attaching a copy of data to 2045075Swnj * be transmitted, and initialize the header from 2055075Swnj * the template for sends on this connection. 2065075Swnj */ 20711720Ssam MGET(m, M_DONTWAIT, MT_HEADER); 20811720Ssam if (m == NULL) 2096505Ssam return (ENOBUFS); 2105245Sroot m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 2114885Swnj m->m_len = sizeof (struct tcpiphdr); 2125075Swnj if (len) { 21331442Skarels if (tp->t_force && len == 1) 21431442Skarels tcpstat.tcps_sndprobe++; 21531442Skarels else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 21631442Skarels tcpstat.tcps_sndrexmitpack++; 21731442Skarels tcpstat.tcps_sndrexmitbyte += len; 21831442Skarels } else { 21931442Skarels tcpstat.tcps_sndpack++; 22031442Skarels tcpstat.tcps_sndbyte += len; 22131442Skarels } 2225075Swnj m->m_next = m_copy(so->so_snd.sb_mb, off, len); 2235075Swnj if (m->m_next == 0) 2245075Swnj len = 0; 22531442Skarels } else if (tp->t_flags & TF_ACKNOW) 22631442Skarels tcpstat.tcps_sndacks++; 22731442Skarels else if (flags & (TH_SYN|TH_FIN|TH_RST)) 22831442Skarels tcpstat.tcps_sndctrl++; 22931442Skarels else if (SEQ_GT(tp->snd_up, tp->snd_una)) 23031442Skarels tcpstat.tcps_sndurg++; 23131442Skarels else 23231442Skarels tcpstat.tcps_sndwinup++; 23331442Skarels 2345075Swnj ti = mtod(m, struct tcpiphdr *); 2355075Swnj if (tp->t_template == 0) 2365075Swnj panic("tcp_output"); 2375110Swnj bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 2385075Swnj 2395075Swnj /* 2405075Swnj * Fill in fields, remembering maximum advertised 2415075Swnj * window for use in delaying messages about window sizes. 24227067Skarels * If resending a FIN, be sure not to use a new sequence number. 2435075Swnj */ 24431725Skarels if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 24531725Skarels tp->snd_nxt == tp->snd_max) 24627067Skarels tp->snd_nxt--; 24725940Skarels ti->ti_seq = htonl(tp->snd_nxt); 24825940Skarels ti->ti_ack = htonl(tp->rcv_nxt); 2495441Swnj /* 2505441Swnj * Before ESTABLISHED, force sending of initial options 2515441Swnj * unless TCP set to not do any options. 2525441Swnj */ 25325940Skarels opt = NULL; 25432373Skarels if (flags & TH_SYN && (tp->t_flags & TF_NOOPT) == 0) { 25526387Skarels u_short mss; 25617273Skarels 25717273Skarels mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp)); 25825940Skarels if (mss > IP_MSS - sizeof(struct tcpiphdr)) { 25925940Skarels opt = tcp_initopt; 26025940Skarels optlen = sizeof (tcp_initopt); 26125940Skarels *(u_short *)(opt + 2) = htons(mss); 26225940Skarels } 2635441Swnj } 2648314Sroot if (opt) { 2655110Swnj m0 = m->m_next; 2669643Ssam m->m_next = m_get(M_DONTWAIT, MT_DATA); 2675088Swnj if (m->m_next == 0) { 2685088Swnj (void) m_free(m); 2695441Swnj m_freem(m0); 2706505Ssam return (ENOBUFS); 2715088Swnj } 2725088Swnj m->m_next->m_next = m0; 2735441Swnj m0 = m->m_next; 2745441Swnj m0->m_len = optlen; 2756162Ssam bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); 2765441Swnj opt = (u_char *)(mtod(m0, caddr_t) + optlen); 2775441Swnj while (m0->m_len & 0x3) { 2785441Swnj *opt++ = TCPOPT_EOL; 2795441Swnj m0->m_len++; 2805441Swnj } 2815441Swnj optlen = m0->m_len; 2825441Swnj ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 2835088Swnj } 2845088Swnj ti->ti_flags = flags; 28525940Skarels /* 28625940Skarels * Calculate receive window. Don't shrink window, 28725940Skarels * but avoid silly window syndrome. 28825940Skarels */ 28925940Skarels if (win < so->so_rcv.sb_hiwat / 4 && win < tp->t_maxseg) 29025940Skarels win = 0; 29125940Skarels if (win < (int)(tp->rcv_adv - tp->rcv_nxt)) 29225940Skarels win = (int)(tp->rcv_adv - tp->rcv_nxt); 29325940Skarels ti->ti_win = htons((u_short)win); 2945088Swnj if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 29526387Skarels ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 2965075Swnj ti->ti_flags |= TH_URG; 2975075Swnj } else 2985075Swnj /* 2995075Swnj * If no urgent pointer to send, then we pull 3005075Swnj * the urgent pointer to the left edge of the send window 3015075Swnj * so that it doesn't drift into the send window on sequence 3025075Swnj * number wraparound. 3035075Swnj */ 3045088Swnj tp->snd_up = tp->snd_una; /* drag it along */ 3057644Sroot /* 3067644Sroot * If anything to send and we can send it all, set PUSH. 3077644Sroot * (This will keep happy those implementations which only 30810143Ssam * give data to the user when a buffer fills or a PUSH comes in.) 3097644Sroot */ 3107644Sroot if (len && off+len == so->so_snd.sb_cc) 3117644Sroot ti->ti_flags |= TH_PUSH; 3125075Swnj 3135075Swnj /* 3145075Swnj * Put TCP length in extended header, and then 3155075Swnj * checksum extended header and data. 3165075Swnj */ 31725940Skarels if (len + optlen) 31825940Skarels ti->ti_len = htons((u_short)(sizeof(struct tcphdr) + 31925940Skarels optlen + len)); 3206162Ssam ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len); 3215075Swnj 3225075Swnj /* 3237125Swnj * In transmit state, time the transmission and arrange for 32421116Skarels * the retransmit. In persist state, just set snd_max. 3255088Swnj */ 32621116Skarels if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { 32731758Skarels tcp_seq startseq = tp->snd_nxt; 32831758Skarels 3297125Swnj /* 3307146Swnj * Advance snd_nxt over sequence space of this segment. 3317125Swnj */ 33227067Skarels if (flags & TH_SYN) 3337125Swnj tp->snd_nxt++; 33427067Skarels if (flags & TH_FIN) { 33527067Skarels tp->snd_nxt++; 33627067Skarels tp->t_flags |= TF_SENTFIN; 33727067Skarels } 3387125Swnj tp->snd_nxt += len; 33931758Skarels if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 3407149Swnj tp->snd_max = tp->snd_nxt; 34131758Skarels /* 34231758Skarels * Time this transmission if not a retransmission and 34331758Skarels * not currently timing anything. 34431758Skarels */ 34531758Skarels if (tp->t_rtt == 0) { 34631758Skarels tp->t_rtt = 1; 34731758Skarels tp->t_rtseq = startseq; 34831758Skarels tcpstat.tcps_segstimed++; 34931758Skarels } 35031758Skarels } 3515088Swnj 3527125Swnj /* 35321116Skarels * Set retransmit timer if not currently set, 35426443Skarels * and not doing an ack or a keep-alive probe. 35531726Skarels * Initial value for retransmit timer is smoothed 35631726Skarels * round-trip time + 2 * round-trip time variance. 35726834Skarels * Initialize shift counter which is used for backoff 35826834Skarels * of retransmit time. 3597125Swnj */ 3607125Swnj if (tp->t_timer[TCPT_REXMT] == 0 && 3617125Swnj tp->snd_nxt != tp->snd_una) { 36232034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 36332034Skarels if (tp->t_timer[TCPT_PERSIST]) { 36432034Skarels tp->t_timer[TCPT_PERSIST] = 0; 36532034Skarels tp->t_rxtshift = 0; 36632034Skarels } 3677125Swnj } 36826443Skarels } else 36925940Skarels if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 37025940Skarels tp->snd_max = tp->snd_nxt + len; 3715163Swnj 3725163Swnj /* 3735268Sroot * Trace. 3745268Sroot */ 3757146Swnj if (so->so_options & SO_DEBUG) 3765268Sroot tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 3775268Sroot 3785268Sroot /* 3795075Swnj * Fill in IP length and desired time to live and 3805075Swnj * send to IP level. 3815075Swnj */ 3825441Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; 3835075Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 38426059Skarels error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 38526059Skarels so->so_options & SO_DONTROUTE); 38612418Ssam if (error) 3876505Ssam return (error); 38831442Skarels tcpstat.tcps_sndtotal++; 3895075Swnj 3905075Swnj /* 3915075Swnj * Data sent (as far as we can tell). 3925075Swnj * If this advertises a larger window than any other segment, 3935245Sroot * then remember the size of the advertised window. 39425940Skarels * Any pending ACK has now been sent. 3955075Swnj */ 3965252Sroot if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 3975075Swnj tp->rcv_adv = tp->rcv_nxt + win; 3985088Swnj tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 39925940Skarels if (sendalot) 4007125Swnj goto again; 4016505Ssam return (0); 4024677Swnj } 4037125Swnj 4047125Swnj tcp_setpersist(tp) 4057125Swnj register struct tcpcb *tp; 4067125Swnj { 40731726Skarels register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 4087125Swnj 4097125Swnj if (tp->t_timer[TCPT_REXMT]) 4107125Swnj panic("tcp_output REXMT"); 4117125Swnj /* 4127125Swnj * Start/restart persistance timer. 4137125Swnj */ 4147125Swnj TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 41531726Skarels t * tcp_backoff[tp->t_rxtshift], 41631725Skarels TCPTV_PERSMIN, TCPTV_PERSMAX); 41731725Skarels if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 41831725Skarels tp->t_rxtshift++; 4197125Swnj } 420