1*23191Smckusick /* 2*23191Smckusick * Copyright (c) 1982 Regents of the University of California. 3*23191Smckusick * All rights reserved. The Berkeley software License Agreement 4*23191Smckusick * specifies the terms and conditions for redistribution. 5*23191Smckusick * 6*23191Smckusick * @(#)tcp_output.c 6.9 (Berkeley) 06/08/85 7*23191Smckusick */ 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; 455075Swnj register int len; 465075Swnj struct mbuf *m0; 476505Ssam int off, flags, win, error; 485075Swnj register struct mbuf *m; 495075Swnj register struct tcpiphdr *ti; 505441Swnj u_char *opt; 515441Swnj unsigned optlen = 0; 527125Swnj int 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 */ 607125Swnj again: 617125Swnj sendalot = 0; 625075Swnj off = tp->snd_nxt - tp->snd_una; 6321116Skarels win = MIN(tp->snd_wnd, tp->snd_cwnd); 6421116Skarels /* 6521116Skarels * If in persist timeout with window of 0, send 1 byte. 6621116Skarels * Otherwise, window is small but nonzero 6721116Skarels * and timer expired, go to transmit state. 6821116Skarels */ 6921116Skarels if (tp->t_force) { 7021116Skarels if (win == 0) 7121116Skarels win = 1; 7221116Skarels else { 7321116Skarels tp->t_timer[TCPT_PERSIST] = 0; 7421116Skarels tp->t_rxtshift = 0; 7521116Skarels } 7621116Skarels } 7717361Skarels len = MIN(so->so_snd.sb_cc, win) - off; 785285Sroot if (len < 0) 796505Ssam return (0); /* ??? */ /* past FIN */ 807125Swnj if (len > tp->t_maxseg) { 815088Swnj len = tp->t_maxseg; 8217318Skarels /* 8321116Skarels * Don't send more than one segment if retransmitting 8421116Skarels * (or persisting, but then we shouldn't be here). 8517318Skarels */ 8617361Skarels if (tp->t_rxtshift == 0) 8717318Skarels sendalot = 1; 887125Swnj } 896279Swnj 9021116Skarels win = sbspace(&so->so_rcv); 915088Swnj flags = tcp_outflags[tp->t_state]; 925299Sroot if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc) 935163Swnj flags &= ~TH_FIN; 946279Swnj if (flags & (TH_SYN|TH_RST|TH_FIN)) 955075Swnj goto send; 966279Swnj if (SEQ_GT(tp->snd_up, tp->snd_una)) 976279Swnj goto send; 984678Swnj 995075Swnj /* 10017318Skarels * Sender silly window avoidance. If connection is idle 10117318Skarels * and can send all data, a maximum segment, 10217318Skarels * at least a maximum default-size segment do it, 1036279Swnj * or are forced, do it; otherwise don't bother. 10421116Skarels * If retransmitting (possibly after persist timer forced us 10521116Skarels * to send into a small window), then must resend. 1066279Swnj */ 1076279Swnj if (len) { 10817318Skarels if (len == tp->t_maxseg || len >= so->so_snd.sb_cc) /* off = 0*/ 1096279Swnj goto send; 11017318Skarels if (len >= TCP_MSS) /* a lot */ 1116279Swnj goto send; 1126279Swnj if (tp->t_force) 1136279Swnj goto send; 11421116Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 11521116Skarels goto send; 1166279Swnj } 1176279Swnj 1186279Swnj /* 1195285Sroot * Send if we owe peer an ACK. 1205075Swnj */ 1215441Swnj if (tp->t_flags&TF_ACKNOW) 1225075Swnj goto send; 1234678Swnj 1245441Swnj 1255441Swnj /* 12617361Skarels * Calculate available window, and also amount 1275075Swnj * of window known to peer (as advertised window less 12817361Skarels * next expected input.) If the difference is 35% or more of the 12917361Skarels * maximum possible window, then want to send a window update to peer. 1305075Swnj */ 1315088Swnj win = sbspace(&so->so_rcv); 1325088Swnj if (win > 0 && 1335088Swnj ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35)) 1345075Swnj goto send; 1354678Swnj 1365075Swnj /* 1377125Swnj * TCP window updates are not reliable, rather a polling protocol 1387125Swnj * using ``persist'' packets is used to insure receipt of window 1397125Swnj * updates. The three ``states'' for the output side are: 1407125Swnj * idle not doing retransmits or persists 1417125Swnj * persisting to move a zero window 1427125Swnj * (re)transmitting and thereby not persisting 1437125Swnj * 1447125Swnj * tp->t_timer[TCPT_PERSIST] 1457125Swnj * is set when we are in persist state. 1467125Swnj * tp->t_force 1477125Swnj * is set when we are called to send a persist packet. 1487125Swnj * tp->t_timer[TCPT_REXMT] 1497125Swnj * is set when we are retransmitting 1507125Swnj * The output side is idle when both timers are zero. 1517125Swnj * 15221116Skarels * If send window is too small, there is data to transmit, and no 15321116Skarels * retransmit or persist is pending, then go to persist state. 15421116Skarels * If nothing happens soon, send when timer expires: 15521116Skarels * if window is nonzero, transmit what we can, 15621116Skarels * otherwise force out a byte. 1577125Swnj */ 15821116Skarels if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 && 15921116Skarels tp->t_timer[TCPT_PERSIST] == 0) { 1607125Swnj tp->t_rxtshift = 0; 1617125Swnj tcp_setpersist(tp); 1627125Swnj } 1637125Swnj 1647125Swnj /* 1655075Swnj * No reason to send a segment, just return. 1665075Swnj */ 1675110Swnj return (0); 1684678Swnj 1695075Swnj send: 1705075Swnj /* 1715075Swnj * Grab a header mbuf, attaching a copy of data to 1725075Swnj * be transmitted, and initialize the header from 1735075Swnj * the template for sends on this connection. 1745075Swnj */ 17511720Ssam MGET(m, M_DONTWAIT, MT_HEADER); 17611720Ssam if (m == NULL) 1776505Ssam return (ENOBUFS); 1785245Sroot m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 1794885Swnj m->m_len = sizeof (struct tcpiphdr); 1805075Swnj if (len) { 1815075Swnj m->m_next = m_copy(so->so_snd.sb_mb, off, len); 1825075Swnj if (m->m_next == 0) 1835075Swnj len = 0; 1845075Swnj } 1855075Swnj ti = mtod(m, struct tcpiphdr *); 1865075Swnj if (tp->t_template == 0) 1875075Swnj panic("tcp_output"); 1885110Swnj bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 1895075Swnj 1905075Swnj /* 1915075Swnj * Fill in fields, remembering maximum advertised 1925075Swnj * window for use in delaying messages about window sizes. 1935075Swnj */ 1945245Sroot ti->ti_seq = tp->snd_nxt; 1955245Sroot ti->ti_ack = tp->rcv_nxt; 1965245Sroot ti->ti_seq = htonl(ti->ti_seq); 1975245Sroot ti->ti_ack = htonl(ti->ti_ack); 1985441Swnj /* 1995441Swnj * Before ESTABLISHED, force sending of initial options 2005441Swnj * unless TCP set to not do any options. 2015441Swnj */ 2025441Swnj if (tp->t_state < TCPS_ESTABLISHED) { 20317273Skarels int mss; 20417273Skarels 2055441Swnj if (tp->t_flags&TF_NOOPT) 2065441Swnj goto noopt; 20717273Skarels mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp)); 20817273Skarels if (mss <= IP_MSS - sizeof(struct tcpiphdr)) 20917273Skarels goto noopt; 2105441Swnj opt = tcp_initopt; 2115441Swnj optlen = sizeof (tcp_initopt); 21217273Skarels *(u_short *)(opt + 2) = htons(mss); 2135441Swnj } else { 2145441Swnj if (tp->t_tcpopt == 0) 2155441Swnj goto noopt; 2165441Swnj opt = mtod(tp->t_tcpopt, u_char *); 2175441Swnj optlen = tp->t_tcpopt->m_len; 2185441Swnj } 2198314Sroot if (opt) { 2205110Swnj m0 = m->m_next; 2219643Ssam m->m_next = m_get(M_DONTWAIT, MT_DATA); 2225088Swnj if (m->m_next == 0) { 2235088Swnj (void) m_free(m); 2245441Swnj m_freem(m0); 2256505Ssam return (ENOBUFS); 2265088Swnj } 2275088Swnj m->m_next->m_next = m0; 2285441Swnj m0 = m->m_next; 2295441Swnj m0->m_len = optlen; 2306162Ssam bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); 2315441Swnj opt = (u_char *)(mtod(m0, caddr_t) + optlen); 2325441Swnj while (m0->m_len & 0x3) { 2335441Swnj *opt++ = TCPOPT_EOL; 2345441Swnj m0->m_len++; 2355441Swnj } 2365441Swnj optlen = m0->m_len; 2375441Swnj ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 2385088Swnj } 2395441Swnj noopt: 2405088Swnj ti->ti_flags = flags; 24121116Skarels if (win >= so->so_rcv.sb_hiwat / 4) /* avoid silly window */ 2425110Swnj ti->ti_win = htons((u_short)win); 2435088Swnj if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 2445420Swnj ti->ti_urp = tp->snd_up - tp->snd_nxt; 2455420Swnj ti->ti_urp = htons(ti->ti_urp); 2465075Swnj ti->ti_flags |= TH_URG; 2475075Swnj } else 2485075Swnj /* 2495075Swnj * If no urgent pointer to send, then we pull 2505075Swnj * the urgent pointer to the left edge of the send window 2515075Swnj * so that it doesn't drift into the send window on sequence 2525075Swnj * number wraparound. 2535075Swnj */ 2545088Swnj tp->snd_up = tp->snd_una; /* drag it along */ 2557644Sroot /* 2567644Sroot * If anything to send and we can send it all, set PUSH. 2577644Sroot * (This will keep happy those implementations which only 25810143Ssam * give data to the user when a buffer fills or a PUSH comes in.) 2597644Sroot */ 2607644Sroot if (len && off+len == so->so_snd.sb_cc) 2617644Sroot ti->ti_flags |= TH_PUSH; 2625075Swnj 2635075Swnj /* 2645075Swnj * Put TCP length in extended header, and then 2655075Swnj * checksum extended header and data. 2665075Swnj */ 2675441Swnj if (len + optlen) { 2685441Swnj ti->ti_len = sizeof (struct tcphdr) + optlen + len; 2695441Swnj ti->ti_len = htons((u_short)ti->ti_len); 2705441Swnj } 2716162Ssam ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len); 2725075Swnj 2735075Swnj /* 2747125Swnj * In transmit state, time the transmission and arrange for 27521116Skarels * the retransmit. In persist state, just set snd_max. 2765088Swnj */ 27721116Skarels if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { 2787125Swnj /* 2797146Swnj * Advance snd_nxt over sequence space of this segment. 2807125Swnj */ 2817125Swnj if (flags & (TH_SYN|TH_FIN)) 2827125Swnj tp->snd_nxt++; 2837125Swnj tp->snd_nxt += len; 28415385Ssam if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 2857149Swnj tp->snd_max = tp->snd_nxt; 28615385Ssam /* 28715385Ssam * Time this transmission if not a retransmission and 28815385Ssam * not currently timing anything. 28915385Ssam */ 29015385Ssam if (tp->t_rtt == 0) { 29115385Ssam tp->t_rtt = 1; 29215385Ssam tp->t_rtseq = tp->snd_nxt - len; 29315385Ssam } 2947125Swnj } 2955088Swnj 2967125Swnj /* 29721116Skarels * Set retransmit timer if not currently set, 29821116Skarels * and not doing a keep-alive probe. 2997125Swnj * Initial value for retransmit timer to tcp_beta*tp->t_srtt. 3007125Swnj * Initialize shift counter which is used for exponential 3017125Swnj * backoff of retransmit time. 3027125Swnj */ 3037125Swnj if (tp->t_timer[TCPT_REXMT] == 0 && 3047125Swnj tp->snd_nxt != tp->snd_una) { 3057125Swnj TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 3067125Swnj tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 3077125Swnj tp->t_rxtshift = 0; 3087125Swnj } 3097125Swnj tp->t_timer[TCPT_PERSIST] = 0; 3107149Swnj } else { 3117149Swnj if (SEQ_GT(tp->snd_una+1, tp->snd_max)) 3127149Swnj tp->snd_max = tp->snd_una+1; 3137146Swnj } 3145163Swnj 3155163Swnj /* 3165268Sroot * Trace. 3175268Sroot */ 3187146Swnj if (so->so_options & SO_DEBUG) 3195268Sroot tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 3205268Sroot 3215268Sroot /* 3225075Swnj * Fill in IP length and desired time to live and 3235075Swnj * send to IP level. 3245075Swnj */ 3255441Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; 3265075Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 32712418Ssam if (so->so_options & SO_DONTROUTE) 32812765Ssam error = 32912765Ssam ip_output(m, tp->t_ipopt, (struct route *)0, IP_ROUTETOIF); 33012418Ssam else 33112418Ssam error = ip_output(m, tp->t_ipopt, &tp->t_inpcb->inp_route, 0); 33212418Ssam if (error) 3336505Ssam return (error); 3345075Swnj 3355075Swnj /* 3365075Swnj * Data sent (as far as we can tell). 3375075Swnj * If this advertises a larger window than any other segment, 3385245Sroot * then remember the size of the advertised window. 3395088Swnj * Drop send for purpose of ACK requirements. 3405075Swnj */ 3415252Sroot if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 3425075Swnj tp->rcv_adv = tp->rcv_nxt + win; 3435088Swnj tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 3447125Swnj if (sendalot && tp->t_force == 0) 3457125Swnj goto again; 3466505Ssam return (0); 3474677Swnj } 3487125Swnj 3497125Swnj tcp_setpersist(tp) 3507125Swnj register struct tcpcb *tp; 3517125Swnj { 3527125Swnj 3537125Swnj if (tp->t_timer[TCPT_REXMT]) 3547125Swnj panic("tcp_output REXMT"); 3557125Swnj /* 3567125Swnj * Start/restart persistance timer. 3577125Swnj */ 3587125Swnj TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 3597125Swnj ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift, 3607125Swnj TCPTV_PERSMIN, TCPTV_MAX); 3617125Swnj tp->t_rxtshift++; 3627125Swnj if (tp->t_rxtshift >= TCP_MAXRXTSHIFT) 3637125Swnj tp->t_rxtshift = 0; 3647125Swnj } 365