123191Smckusick /* 223191Smckusick * Copyright (c) 1982 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*26834Skarels * @(#)tcp_output.c 6.16 (Berkeley) 03/12/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); 65*26834Skarels 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) { 73*26834Skarels if (win == 0 && off) 74*26834Skarels log(7, "persist offset %d\n", off); 75*26834Skarels if (win == 0) 7621116Skarels win = 1; 7721116Skarels else { 7821116Skarels tp->t_timer[TCPT_PERSIST] = 0; 7921116Skarels tp->t_rxtshift = 0; 8021116Skarels } 8121116Skarels } 8225940Skarels 8317361Skarels len = MIN(so->so_snd.sb_cc, win) - off; 847125Swnj if (len > tp->t_maxseg) { 855088Swnj len = tp->t_maxseg; 8617318Skarels /* 8721116Skarels * Don't send more than one segment if retransmitting 8821116Skarels * (or persisting, but then we shouldn't be here). 8917318Skarels */ 9017361Skarels if (tp->t_rxtshift == 0) 9117318Skarels sendalot = 1; 927125Swnj } 936279Swnj 945088Swnj flags = tcp_outflags[tp->t_state]; 95*26834Skarels if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 965163Swnj flags &= ~TH_FIN; 9725940Skarels 98*26834Skarels if (len < 0) { 99*26834Skarels /* 100*26834Skarels * If closing and all data acked, len will be -1; 101*26834Skarels * retransmit FIN if necessary. 102*26834Skarels * Otherwise, window shrank after we sent into it. 103*26834Skarels * If window shrank to 0, cancel pending retransmit 104*26834Skarels * and pull snd_nxt back to (closed) window. 105*26834Skarels * We will enter persist state below. 106*26834Skarels */ 107*26834Skarels if (flags & TH_FIN) 108*26834Skarels len = 0; 109*26834Skarels else if (win == 0) { 110*26834Skarels tp->t_timer[TCPT_REXMT] = 0; 111*26834Skarels tp->snd_nxt = tp->snd_una; 112*26834Skarels len = 0; 113*26834Skarels } else 114*26834Skarels return (0); 115*26834Skarels } 116*26834Skarels win = sbspace(&so->so_rcv); 117*26834Skarels 11825940Skarels /* 11925940Skarels * Send if we owe peer an ACK. 12025940Skarels */ 121*26834Skarels if (tp->t_flags & TF_ACKNOW) 12225940Skarels goto send; 123*26834Skarels if (flags & (TH_SYN|TH_RST|TH_FIN)) 124*26834Skarels 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) { 13925940Skarels if (len == tp->t_maxseg || len >= TCP_MSS) /* a lot */ 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; 150*26834Skarels } 1516279Swnj 1525441Swnj /* 15325940Skarels * Compare available window to amount of window 15425940Skarels * known to peer (as advertised window less 15517361Skarels * next expected input.) If the difference is 35% or more of the 15617361Skarels * maximum possible window, then want to send a window update to peer. 1575075Swnj */ 1585088Swnj if (win > 0 && 1595088Swnj ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35)) 1605075Swnj goto send; 1614678Swnj 1625075Swnj /* 1637125Swnj * TCP window updates are not reliable, rather a polling protocol 1647125Swnj * using ``persist'' packets is used to insure receipt of window 1657125Swnj * updates. The three ``states'' for the output side are: 1667125Swnj * idle not doing retransmits or persists 16725940Skarels * persisting to move a small or zero window 1687125Swnj * (re)transmitting and thereby not persisting 1697125Swnj * 1707125Swnj * tp->t_timer[TCPT_PERSIST] 1717125Swnj * is set when we are in persist state. 1727125Swnj * tp->t_force 1737125Swnj * is set when we are called to send a persist packet. 1747125Swnj * tp->t_timer[TCPT_REXMT] 1757125Swnj * is set when we are retransmitting 1767125Swnj * The output side is idle when both timers are zero. 1777125Swnj * 17821116Skarels * If send window is too small, there is data to transmit, and no 17921116Skarels * retransmit or persist is pending, then go to persist state. 18021116Skarels * If nothing happens soon, send when timer expires: 18121116Skarels * if window is nonzero, transmit what we can, 18221116Skarels * otherwise force out a byte. 1837125Swnj */ 18421116Skarels if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 && 18521116Skarels tp->t_timer[TCPT_PERSIST] == 0) { 1867125Swnj tp->t_rxtshift = 0; 1877125Swnj tcp_setpersist(tp); 1887125Swnj } 1897125Swnj 1907125Swnj /* 1915075Swnj * No reason to send a segment, just return. 1925075Swnj */ 1935110Swnj return (0); 1944678Swnj 1955075Swnj send: 1965075Swnj /* 1975075Swnj * Grab a header mbuf, attaching a copy of data to 1985075Swnj * be transmitted, and initialize the header from 1995075Swnj * the template for sends on this connection. 2005075Swnj */ 20111720Ssam MGET(m, M_DONTWAIT, MT_HEADER); 20211720Ssam if (m == NULL) 2036505Ssam return (ENOBUFS); 2045245Sroot m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 2054885Swnj m->m_len = sizeof (struct tcpiphdr); 2065075Swnj if (len) { 2075075Swnj m->m_next = m_copy(so->so_snd.sb_mb, off, len); 2085075Swnj if (m->m_next == 0) 2095075Swnj len = 0; 2105075Swnj } 2115075Swnj ti = mtod(m, struct tcpiphdr *); 2125075Swnj if (tp->t_template == 0) 2135075Swnj panic("tcp_output"); 2145110Swnj bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 2155075Swnj 2165075Swnj /* 2175075Swnj * Fill in fields, remembering maximum advertised 2185075Swnj * window for use in delaying messages about window sizes. 2195075Swnj */ 22025940Skarels ti->ti_seq = htonl(tp->snd_nxt); 22125940Skarels ti->ti_ack = htonl(tp->rcv_nxt); 2225441Swnj /* 2235441Swnj * Before ESTABLISHED, force sending of initial options 2245441Swnj * unless TCP set to not do any options. 2255441Swnj */ 22625940Skarels opt = NULL; 22725940Skarels if (tp->t_state < TCPS_ESTABLISHED && (tp->t_flags & TF_NOOPT) == 0) { 22826387Skarels u_short mss; 22917273Skarels 23017273Skarels mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp)); 23125940Skarels if (mss > IP_MSS - sizeof(struct tcpiphdr)) { 23225940Skarels opt = tcp_initopt; 23325940Skarels optlen = sizeof (tcp_initopt); 23425940Skarels *(u_short *)(opt + 2) = htons(mss); 23525940Skarels } 23625940Skarels } else if (tp->t_tcpopt) { 2375441Swnj opt = mtod(tp->t_tcpopt, u_char *); 2385441Swnj optlen = tp->t_tcpopt->m_len; 2395441Swnj } 2408314Sroot if (opt) { 2415110Swnj m0 = m->m_next; 2429643Ssam m->m_next = m_get(M_DONTWAIT, MT_DATA); 2435088Swnj if (m->m_next == 0) { 2445088Swnj (void) m_free(m); 2455441Swnj m_freem(m0); 2466505Ssam return (ENOBUFS); 2475088Swnj } 2485088Swnj m->m_next->m_next = m0; 2495441Swnj m0 = m->m_next; 2505441Swnj m0->m_len = optlen; 2516162Ssam bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); 2525441Swnj opt = (u_char *)(mtod(m0, caddr_t) + optlen); 2535441Swnj while (m0->m_len & 0x3) { 2545441Swnj *opt++ = TCPOPT_EOL; 2555441Swnj m0->m_len++; 2565441Swnj } 2575441Swnj optlen = m0->m_len; 2585441Swnj ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 2595088Swnj } 2605088Swnj ti->ti_flags = flags; 26125940Skarels /* 26225940Skarels * Calculate receive window. Don't shrink window, 26325940Skarels * but avoid silly window syndrome. 26425940Skarels */ 26525940Skarels if (win < so->so_rcv.sb_hiwat / 4 && win < tp->t_maxseg) 26625940Skarels win = 0; 26725940Skarels if (win < (int)(tp->rcv_adv - tp->rcv_nxt)) 26825940Skarels win = (int)(tp->rcv_adv - tp->rcv_nxt); 26925940Skarels ti->ti_win = htons((u_short)win); 2705088Swnj if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 27126387Skarels ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 2725075Swnj ti->ti_flags |= TH_URG; 2735075Swnj } else 2745075Swnj /* 2755075Swnj * If no urgent pointer to send, then we pull 2765075Swnj * the urgent pointer to the left edge of the send window 2775075Swnj * so that it doesn't drift into the send window on sequence 2785075Swnj * number wraparound. 2795075Swnj */ 2805088Swnj tp->snd_up = tp->snd_una; /* drag it along */ 2817644Sroot /* 2827644Sroot * If anything to send and we can send it all, set PUSH. 2837644Sroot * (This will keep happy those implementations which only 28410143Ssam * give data to the user when a buffer fills or a PUSH comes in.) 2857644Sroot */ 2867644Sroot if (len && off+len == so->so_snd.sb_cc) 2877644Sroot ti->ti_flags |= TH_PUSH; 2885075Swnj 2895075Swnj /* 2905075Swnj * Put TCP length in extended header, and then 2915075Swnj * checksum extended header and data. 2925075Swnj */ 29325940Skarels if (len + optlen) 29425940Skarels ti->ti_len = htons((u_short)(sizeof(struct tcphdr) + 29525940Skarels optlen + len)); 2966162Ssam ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len); 2975075Swnj 2985075Swnj /* 2997125Swnj * In transmit state, time the transmission and arrange for 30021116Skarels * the retransmit. In persist state, just set snd_max. 3015088Swnj */ 30221116Skarels if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { 3037125Swnj /* 3047146Swnj * Advance snd_nxt over sequence space of this segment. 3057125Swnj */ 3067125Swnj if (flags & (TH_SYN|TH_FIN)) 3077125Swnj tp->snd_nxt++; 3087125Swnj tp->snd_nxt += len; 30915385Ssam if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 3107149Swnj tp->snd_max = tp->snd_nxt; 31115385Ssam /* 31215385Ssam * Time this transmission if not a retransmission and 31315385Ssam * not currently timing anything. 31415385Ssam */ 31515385Ssam if (tp->t_rtt == 0) { 31615385Ssam tp->t_rtt = 1; 31715385Ssam tp->t_rtseq = tp->snd_nxt - len; 31815385Ssam } 3197125Swnj } 3205088Swnj 3217125Swnj /* 32221116Skarels * Set retransmit timer if not currently set, 32326443Skarels * and not doing an ack or a keep-alive probe. 32425940Skarels * Initial value for retransmit timer is tcp_beta*tp->t_srtt. 325*26834Skarels * Initialize shift counter which is used for backoff 326*26834Skarels * of retransmit time. 3277125Swnj */ 3287125Swnj if (tp->t_timer[TCPT_REXMT] == 0 && 3297125Swnj tp->snd_nxt != tp->snd_una) { 3307125Swnj TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 3317125Swnj tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 3327125Swnj tp->t_rxtshift = 0; 33326443Skarels tp->t_timer[TCPT_PERSIST] = 0; 3347125Swnj } 33526443Skarels } else 33625940Skarels if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 33725940Skarels tp->snd_max = tp->snd_nxt + len; 3385163Swnj 3395163Swnj /* 3405268Sroot * Trace. 3415268Sroot */ 3427146Swnj if (so->so_options & SO_DEBUG) 3435268Sroot tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 3445268Sroot 3455268Sroot /* 3465075Swnj * Fill in IP length and desired time to live and 3475075Swnj * send to IP level. 3485075Swnj */ 3495441Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; 3505075Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 35126059Skarels error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 35226059Skarels so->so_options & SO_DONTROUTE); 35312418Ssam if (error) 3546505Ssam return (error); 3555075Swnj 3565075Swnj /* 3575075Swnj * Data sent (as far as we can tell). 3585075Swnj * If this advertises a larger window than any other segment, 3595245Sroot * then remember the size of the advertised window. 36025940Skarels * Any pending ACK has now been sent. 3615075Swnj */ 3625252Sroot if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 3635075Swnj tp->rcv_adv = tp->rcv_nxt + win; 3645088Swnj tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 36525940Skarels if (sendalot) 3667125Swnj goto again; 3676505Ssam return (0); 3684677Swnj } 3697125Swnj 3707125Swnj tcp_setpersist(tp) 3717125Swnj register struct tcpcb *tp; 3727125Swnj { 3737125Swnj 3747125Swnj if (tp->t_timer[TCPT_REXMT]) 3757125Swnj panic("tcp_output REXMT"); 3767125Swnj /* 3777125Swnj * Start/restart persistance timer. 3787125Swnj */ 3797125Swnj TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 3807125Swnj ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift, 3817125Swnj TCPTV_PERSMIN, TCPTV_MAX); 3827125Swnj tp->t_rxtshift++; 3837125Swnj if (tp->t_rxtshift >= TCP_MAXRXTSHIFT) 3847125Swnj tp->t_rxtshift = 0; 3857125Swnj } 386