123191Smckusick /* 2*29150Smckusick * 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*29150Smckusick * @(#)tcp_output.c 7.1 (Berkeley) 06/05/86 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]; 8326834Skarels if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 845163Swnj flags &= ~TH_FIN; 8525940Skarels 8626834Skarels if (len < 0) { 8726834Skarels /* 8827048Skarels * If FIN has been sent but not acked, 8927048Skarels * but we haven't been called to retransmit, 9027067Skarels * len will be -1; transmit if acking, otherwise no need. 9126834Skarels * Otherwise, window shrank after we sent into it. 9226834Skarels * If window shrank to 0, cancel pending retransmit 9326834Skarels * and pull snd_nxt back to (closed) window. 9426834Skarels * We will enter persist state below. 9527048Skarels * If the window didn't close completely, 9627048Skarels * just wait for an ACK. 9726834Skarels */ 9827067Skarels if (flags & TH_FIN) { 9927067Skarels if (tp->t_flags & TF_ACKNOW) 10027067Skarels len = 0; 10127067Skarels else 10227067Skarels return (0); 10327067Skarels } else if (win == 0) { 10427067Skarels tp->t_timer[TCPT_REXMT] = 0; 10527067Skarels tp->snd_nxt = tp->snd_una; 10627067Skarels len = 0; 10727067Skarels } else 10826834Skarels return (0); 10926834Skarels } 11027067Skarels if (len > tp->t_maxseg) { 11127067Skarels len = tp->t_maxseg; 11227067Skarels /* 11327067Skarels * Don't send more than one segment if retransmitting 11427067Skarels * (or persisting, but then we shouldn't be here). 11527067Skarels */ 11627067Skarels if (tp->t_rxtshift == 0) 11727067Skarels sendalot = 1; 11827067Skarels } 11926834Skarels win = sbspace(&so->so_rcv); 12026834Skarels 12127067Skarels 12225940Skarels /* 12327067Skarels * If our state indicates that FIN should be sent 12427067Skarels * and we have not yet done so, or we're retransmitting the FIN, 12527067Skarels * then we need to send. 12627067Skarels */ 12727067Skarels if (flags & TH_FIN && 12827067Skarels ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 12927067Skarels goto send; 13027067Skarels /* 13125940Skarels * Send if we owe peer an ACK. 13225940Skarels */ 13326834Skarels if (tp->t_flags & TF_ACKNOW) 13425940Skarels goto send; 13527067Skarels if (flags & (TH_SYN|TH_RST)) 13626834Skarels goto send; 1376279Swnj if (SEQ_GT(tp->snd_up, tp->snd_una)) 1386279Swnj goto send; 1394678Swnj 1405075Swnj /* 14117318Skarels * Sender silly window avoidance. If connection is idle 14217318Skarels * and can send all data, a maximum segment, 14317318Skarels * at least a maximum default-size segment do it, 1446279Swnj * or are forced, do it; otherwise don't bother. 14525261Skarels * If peer's buffer is tiny, then send 14625261Skarels * when window is at least half open. 14721116Skarels * If retransmitting (possibly after persist timer forced us 14821116Skarels * to send into a small window), then must resend. 1496279Swnj */ 1506279Swnj if (len) { 15125940Skarels if (len == tp->t_maxseg || len >= TCP_MSS) /* a lot */ 1526279Swnj goto send; 15325940Skarels if ((idle || tp->t_flags & TF_NODELAY) && 15425940Skarels len + off >= so->so_snd.sb_cc) 1556279Swnj goto send; 1566279Swnj if (tp->t_force) 1576279Swnj goto send; 15825261Skarels if (len >= tp->max_sndwnd / 2) 15925261Skarels goto send; 16021116Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 16121116Skarels goto send; 16226834Skarels } 1636279Swnj 1645441Swnj /* 16525940Skarels * Compare available window to amount of window 16625940Skarels * known to peer (as advertised window less 16717361Skarels * next expected input.) If the difference is 35% or more of the 16817361Skarels * maximum possible window, then want to send a window update to peer. 1695075Swnj */ 1705088Swnj if (win > 0 && 1715088Swnj ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35)) 1725075Swnj goto send; 1734678Swnj 1745075Swnj /* 1757125Swnj * TCP window updates are not reliable, rather a polling protocol 1767125Swnj * using ``persist'' packets is used to insure receipt of window 1777125Swnj * updates. The three ``states'' for the output side are: 1787125Swnj * idle not doing retransmits or persists 17925940Skarels * persisting to move a small or zero window 1807125Swnj * (re)transmitting and thereby not persisting 1817125Swnj * 1827125Swnj * tp->t_timer[TCPT_PERSIST] 1837125Swnj * is set when we are in persist state. 1847125Swnj * tp->t_force 1857125Swnj * is set when we are called to send a persist packet. 1867125Swnj * tp->t_timer[TCPT_REXMT] 1877125Swnj * is set when we are retransmitting 1887125Swnj * The output side is idle when both timers are zero. 1897125Swnj * 19021116Skarels * If send window is too small, there is data to transmit, and no 19121116Skarels * retransmit or persist is pending, then go to persist state. 19221116Skarels * If nothing happens soon, send when timer expires: 19321116Skarels * if window is nonzero, transmit what we can, 19421116Skarels * otherwise force out a byte. 1957125Swnj */ 19621116Skarels if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 && 19721116Skarels tp->t_timer[TCPT_PERSIST] == 0) { 1987125Swnj tp->t_rxtshift = 0; 1997125Swnj tcp_setpersist(tp); 2007125Swnj } 2017125Swnj 2027125Swnj /* 2035075Swnj * No reason to send a segment, just return. 2045075Swnj */ 2055110Swnj return (0); 2064678Swnj 2075075Swnj send: 2085075Swnj /* 2095075Swnj * Grab a header mbuf, attaching a copy of data to 2105075Swnj * be transmitted, and initialize the header from 2115075Swnj * the template for sends on this connection. 2125075Swnj */ 21311720Ssam MGET(m, M_DONTWAIT, MT_HEADER); 21411720Ssam if (m == NULL) 2156505Ssam return (ENOBUFS); 2165245Sroot m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 2174885Swnj m->m_len = sizeof (struct tcpiphdr); 2185075Swnj if (len) { 2195075Swnj m->m_next = m_copy(so->so_snd.sb_mb, off, len); 2205075Swnj if (m->m_next == 0) 2215075Swnj len = 0; 2225075Swnj } 2235075Swnj ti = mtod(m, struct tcpiphdr *); 2245075Swnj if (tp->t_template == 0) 2255075Swnj panic("tcp_output"); 2265110Swnj bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 2275075Swnj 2285075Swnj /* 2295075Swnj * Fill in fields, remembering maximum advertised 2305075Swnj * window for use in delaying messages about window sizes. 23127067Skarels * If resending a FIN, be sure not to use a new sequence number. 2325075Swnj */ 23327067Skarels if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 23427067Skarels tp->snd_nxt != tp->snd_una) 23527067Skarels tp->snd_nxt--; 23625940Skarels ti->ti_seq = htonl(tp->snd_nxt); 23725940Skarels ti->ti_ack = htonl(tp->rcv_nxt); 2385441Swnj /* 2395441Swnj * Before ESTABLISHED, force sending of initial options 2405441Swnj * unless TCP set to not do any options. 2415441Swnj */ 24225940Skarels opt = NULL; 24325940Skarels if (tp->t_state < TCPS_ESTABLISHED && (tp->t_flags & TF_NOOPT) == 0) { 24426387Skarels u_short mss; 24517273Skarels 24617273Skarels mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp)); 24725940Skarels if (mss > IP_MSS - sizeof(struct tcpiphdr)) { 24825940Skarels opt = tcp_initopt; 24925940Skarels optlen = sizeof (tcp_initopt); 25025940Skarels *(u_short *)(opt + 2) = htons(mss); 25125940Skarels } 25225940Skarels } else if (tp->t_tcpopt) { 2535441Swnj opt = mtod(tp->t_tcpopt, u_char *); 2545441Swnj optlen = tp->t_tcpopt->m_len; 2555441Swnj } 2568314Sroot if (opt) { 2575110Swnj m0 = m->m_next; 2589643Ssam m->m_next = m_get(M_DONTWAIT, MT_DATA); 2595088Swnj if (m->m_next == 0) { 2605088Swnj (void) m_free(m); 2615441Swnj m_freem(m0); 2626505Ssam return (ENOBUFS); 2635088Swnj } 2645088Swnj m->m_next->m_next = m0; 2655441Swnj m0 = m->m_next; 2665441Swnj m0->m_len = optlen; 2676162Ssam bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); 2685441Swnj opt = (u_char *)(mtod(m0, caddr_t) + optlen); 2695441Swnj while (m0->m_len & 0x3) { 2705441Swnj *opt++ = TCPOPT_EOL; 2715441Swnj m0->m_len++; 2725441Swnj } 2735441Swnj optlen = m0->m_len; 2745441Swnj ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 2755088Swnj } 2765088Swnj ti->ti_flags = flags; 27725940Skarels /* 27825940Skarels * Calculate receive window. Don't shrink window, 27925940Skarels * but avoid silly window syndrome. 28025940Skarels */ 28125940Skarels if (win < so->so_rcv.sb_hiwat / 4 && win < tp->t_maxseg) 28225940Skarels win = 0; 28325940Skarels if (win < (int)(tp->rcv_adv - tp->rcv_nxt)) 28425940Skarels win = (int)(tp->rcv_adv - tp->rcv_nxt); 28525940Skarels ti->ti_win = htons((u_short)win); 2865088Swnj if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 28726387Skarels ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 2885075Swnj ti->ti_flags |= TH_URG; 2895075Swnj } else 2905075Swnj /* 2915075Swnj * If no urgent pointer to send, then we pull 2925075Swnj * the urgent pointer to the left edge of the send window 2935075Swnj * so that it doesn't drift into the send window on sequence 2945075Swnj * number wraparound. 2955075Swnj */ 2965088Swnj tp->snd_up = tp->snd_una; /* drag it along */ 2977644Sroot /* 2987644Sroot * If anything to send and we can send it all, set PUSH. 2997644Sroot * (This will keep happy those implementations which only 30010143Ssam * give data to the user when a buffer fills or a PUSH comes in.) 3017644Sroot */ 3027644Sroot if (len && off+len == so->so_snd.sb_cc) 3037644Sroot ti->ti_flags |= TH_PUSH; 3045075Swnj 3055075Swnj /* 3065075Swnj * Put TCP length in extended header, and then 3075075Swnj * checksum extended header and data. 3085075Swnj */ 30925940Skarels if (len + optlen) 31025940Skarels ti->ti_len = htons((u_short)(sizeof(struct tcphdr) + 31125940Skarels optlen + len)); 3126162Ssam ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len); 3135075Swnj 3145075Swnj /* 3157125Swnj * In transmit state, time the transmission and arrange for 31621116Skarels * the retransmit. In persist state, just set snd_max. 3175088Swnj */ 31821116Skarels if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { 3197125Swnj /* 3207146Swnj * Advance snd_nxt over sequence space of this segment. 3217125Swnj */ 32227067Skarels if (flags & TH_SYN) 3237125Swnj tp->snd_nxt++; 32427067Skarels if (flags & TH_FIN) { 32527067Skarels tp->snd_nxt++; 32627067Skarels tp->t_flags |= TF_SENTFIN; 32727067Skarels } 3287125Swnj tp->snd_nxt += len; 32915385Ssam if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 3307149Swnj tp->snd_max = tp->snd_nxt; 33115385Ssam /* 33215385Ssam * Time this transmission if not a retransmission and 33315385Ssam * not currently timing anything. 33415385Ssam */ 33515385Ssam if (tp->t_rtt == 0) { 33615385Ssam tp->t_rtt = 1; 33715385Ssam tp->t_rtseq = tp->snd_nxt - len; 33815385Ssam } 3397125Swnj } 3405088Swnj 3417125Swnj /* 34221116Skarels * Set retransmit timer if not currently set, 34326443Skarels * and not doing an ack or a keep-alive probe. 34425940Skarels * Initial value for retransmit timer is tcp_beta*tp->t_srtt. 34526834Skarels * Initialize shift counter which is used for backoff 34626834Skarels * of retransmit time. 3477125Swnj */ 3487125Swnj if (tp->t_timer[TCPT_REXMT] == 0 && 3497125Swnj tp->snd_nxt != tp->snd_una) { 3507125Swnj TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 35127048Skarels tcp_beta * (tp->t_srtt ? tp->t_srtt : TCPTV_SRTTDFLT), 35227048Skarels TCPTV_MIN, TCPTV_MAX); 3537125Swnj tp->t_rxtshift = 0; 35426443Skarels tp->t_timer[TCPT_PERSIST] = 0; 3557125Swnj } 35626443Skarels } else 35725940Skarels if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 35825940Skarels tp->snd_max = tp->snd_nxt + len; 3595163Swnj 3605163Swnj /* 3615268Sroot * Trace. 3625268Sroot */ 3637146Swnj if (so->so_options & SO_DEBUG) 3645268Sroot tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 3655268Sroot 3665268Sroot /* 3675075Swnj * Fill in IP length and desired time to live and 3685075Swnj * send to IP level. 3695075Swnj */ 3705441Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; 3715075Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 37226059Skarels error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 37326059Skarels so->so_options & SO_DONTROUTE); 37412418Ssam if (error) 3756505Ssam return (error); 3765075Swnj 3775075Swnj /* 3785075Swnj * Data sent (as far as we can tell). 3795075Swnj * If this advertises a larger window than any other segment, 3805245Sroot * then remember the size of the advertised window. 38125940Skarels * Any pending ACK has now been sent. 3825075Swnj */ 3835252Sroot if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 3845075Swnj tp->rcv_adv = tp->rcv_nxt + win; 3855088Swnj tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 38625940Skarels if (sendalot) 3877125Swnj goto again; 3886505Ssam return (0); 3894677Swnj } 3907125Swnj 3917125Swnj tcp_setpersist(tp) 3927125Swnj register struct tcpcb *tp; 3937125Swnj { 3947125Swnj 3957125Swnj if (tp->t_timer[TCPT_REXMT]) 3967125Swnj panic("tcp_output REXMT"); 3977125Swnj /* 3987125Swnj * Start/restart persistance timer. 3997125Swnj */ 4007125Swnj TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 4017125Swnj ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift, 4027125Swnj TCPTV_PERSMIN, TCPTV_MAX); 4037125Swnj tp->t_rxtshift++; 4047125Swnj if (tp->t_rxtshift >= TCP_MAXRXTSHIFT) 4057125Swnj tp->t_rxtshift = 0; 4067125Swnj } 407