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*26443Skarels * @(#)tcp_output.c 6.15 (Berkeley) 03/03/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); 6521116Skarels /* 6621116Skarels * If in persist timeout with window of 0, send 1 byte. 6725940Skarels * Otherwise, if window is small but nonzero 6825940Skarels * and timer expired, we will send what we can 6925940Skarels * and go to transmit state. 7021116Skarels */ 7121116Skarels if (tp->t_force) { 7221116Skarels if (win == 0) 7321116Skarels win = 1; 7421116Skarels else { 7521116Skarels tp->t_timer[TCPT_PERSIST] = 0; 7621116Skarels tp->t_rxtshift = 0; 7721116Skarels } 7821116Skarels } 7925940Skarels 8017361Skarels len = MIN(so->so_snd.sb_cc, win) - off; 815285Sroot if (len < 0) 826505Ssam return (0); /* ??? */ /* past FIN */ 837125Swnj if (len > tp->t_maxseg) { 845088Swnj len = tp->t_maxseg; 8517318Skarels /* 8621116Skarels * Don't send more than one segment if retransmitting 8721116Skarels * (or persisting, but then we shouldn't be here). 8817318Skarels */ 8917361Skarels if (tp->t_rxtshift == 0) 9017318Skarels sendalot = 1; 917125Swnj } 926279Swnj 9321116Skarels win = sbspace(&so->so_rcv); 945088Swnj flags = tcp_outflags[tp->t_state]; 955299Sroot if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc) 965163Swnj flags &= ~TH_FIN; 976279Swnj if (flags & (TH_SYN|TH_RST|TH_FIN)) 985075Swnj goto send; 9925940Skarels 10025940Skarels /* 10125940Skarels * Send if we owe peer an ACK. 10225940Skarels */ 10325940Skarels if (tp->t_flags&TF_ACKNOW) 10425940Skarels goto send; 1056279Swnj if (SEQ_GT(tp->snd_up, tp->snd_una)) 1066279Swnj goto send; 1074678Swnj 1085075Swnj /* 10917318Skarels * Sender silly window avoidance. If connection is idle 11017318Skarels * and can send all data, a maximum segment, 11117318Skarels * at least a maximum default-size segment do it, 1126279Swnj * or are forced, do it; otherwise don't bother. 11325261Skarels * If peer's buffer is tiny, then send 11425261Skarels * when window is at least half open. 11521116Skarels * If retransmitting (possibly after persist timer forced us 11621116Skarels * to send into a small window), then must resend. 1176279Swnj */ 1186279Swnj if (len) { 11925940Skarels if (len == tp->t_maxseg || len >= TCP_MSS) /* a lot */ 1206279Swnj goto send; 12125940Skarels if ((idle || tp->t_flags & TF_NODELAY) && 12225940Skarels len + off >= so->so_snd.sb_cc) 1236279Swnj goto send; 1246279Swnj if (tp->t_force) 1256279Swnj goto send; 12625261Skarels if (len >= tp->max_sndwnd / 2) 12725261Skarels goto send; 12821116Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 12921116Skarels goto send; 13025940Skarels } else 13125940Skarels /* 13225940Skarels * If window shrank after we sent into it, 13325940Skarels * cancel pending retransmit. We will enter 13425940Skarels * persist state below. 13525940Skarels */ 13625940Skarels if (off == 0 && SEQ_LT(tp->snd_nxt, tp->snd_max)) 13725940Skarels tp->t_timer[TCPT_REXMT] = 0; 1386279Swnj 1394678Swnj 1405441Swnj /* 14125940Skarels * Compare available window to amount of window 14225940Skarels * known to peer (as advertised window less 14317361Skarels * next expected input.) If the difference is 35% or more of the 14417361Skarels * maximum possible window, then want to send a window update to peer. 1455075Swnj */ 1465088Swnj if (win > 0 && 1475088Swnj ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35)) 1485075Swnj goto send; 1494678Swnj 1505075Swnj /* 1517125Swnj * TCP window updates are not reliable, rather a polling protocol 1527125Swnj * using ``persist'' packets is used to insure receipt of window 1537125Swnj * updates. The three ``states'' for the output side are: 1547125Swnj * idle not doing retransmits or persists 15525940Skarels * persisting to move a small or zero window 1567125Swnj * (re)transmitting and thereby not persisting 1577125Swnj * 1587125Swnj * tp->t_timer[TCPT_PERSIST] 1597125Swnj * is set when we are in persist state. 1607125Swnj * tp->t_force 1617125Swnj * is set when we are called to send a persist packet. 1627125Swnj * tp->t_timer[TCPT_REXMT] 1637125Swnj * is set when we are retransmitting 1647125Swnj * The output side is idle when both timers are zero. 1657125Swnj * 16621116Skarels * If send window is too small, there is data to transmit, and no 16721116Skarels * retransmit or persist is pending, then go to persist state. 16821116Skarels * If nothing happens soon, send when timer expires: 16921116Skarels * if window is nonzero, transmit what we can, 17021116Skarels * otherwise force out a byte. 1717125Swnj */ 17221116Skarels if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 && 17321116Skarels tp->t_timer[TCPT_PERSIST] == 0) { 1747125Swnj tp->t_rxtshift = 0; 1757125Swnj tcp_setpersist(tp); 1767125Swnj } 1777125Swnj 1787125Swnj /* 1795075Swnj * No reason to send a segment, just return. 1805075Swnj */ 1815110Swnj return (0); 1824678Swnj 1835075Swnj send: 1845075Swnj /* 1855075Swnj * Grab a header mbuf, attaching a copy of data to 1865075Swnj * be transmitted, and initialize the header from 1875075Swnj * the template for sends on this connection. 1885075Swnj */ 18911720Ssam MGET(m, M_DONTWAIT, MT_HEADER); 19011720Ssam if (m == NULL) 1916505Ssam return (ENOBUFS); 1925245Sroot m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 1934885Swnj m->m_len = sizeof (struct tcpiphdr); 1945075Swnj if (len) { 1955075Swnj m->m_next = m_copy(so->so_snd.sb_mb, off, len); 1965075Swnj if (m->m_next == 0) 1975075Swnj len = 0; 1985075Swnj } 1995075Swnj ti = mtod(m, struct tcpiphdr *); 2005075Swnj if (tp->t_template == 0) 2015075Swnj panic("tcp_output"); 2025110Swnj bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 2035075Swnj 2045075Swnj /* 2055075Swnj * Fill in fields, remembering maximum advertised 2065075Swnj * window for use in delaying messages about window sizes. 2075075Swnj */ 20825940Skarels ti->ti_seq = htonl(tp->snd_nxt); 20925940Skarels ti->ti_ack = htonl(tp->rcv_nxt); 2105441Swnj /* 2115441Swnj * Before ESTABLISHED, force sending of initial options 2125441Swnj * unless TCP set to not do any options. 2135441Swnj */ 21425940Skarels opt = NULL; 21525940Skarels if (tp->t_state < TCPS_ESTABLISHED && (tp->t_flags & TF_NOOPT) == 0) { 21626387Skarels u_short mss; 21717273Skarels 21817273Skarels mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp)); 21925940Skarels if (mss > IP_MSS - sizeof(struct tcpiphdr)) { 22025940Skarels opt = tcp_initopt; 22125940Skarels optlen = sizeof (tcp_initopt); 22225940Skarels *(u_short *)(opt + 2) = htons(mss); 22325940Skarels } 22425940Skarels } else if (tp->t_tcpopt) { 2255441Swnj opt = mtod(tp->t_tcpopt, u_char *); 2265441Swnj optlen = tp->t_tcpopt->m_len; 2275441Swnj } 2288314Sroot if (opt) { 2295110Swnj m0 = m->m_next; 2309643Ssam m->m_next = m_get(M_DONTWAIT, MT_DATA); 2315088Swnj if (m->m_next == 0) { 2325088Swnj (void) m_free(m); 2335441Swnj m_freem(m0); 2346505Ssam return (ENOBUFS); 2355088Swnj } 2365088Swnj m->m_next->m_next = m0; 2375441Swnj m0 = m->m_next; 2385441Swnj m0->m_len = optlen; 2396162Ssam bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); 2405441Swnj opt = (u_char *)(mtod(m0, caddr_t) + optlen); 2415441Swnj while (m0->m_len & 0x3) { 2425441Swnj *opt++ = TCPOPT_EOL; 2435441Swnj m0->m_len++; 2445441Swnj } 2455441Swnj optlen = m0->m_len; 2465441Swnj ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 2475088Swnj } 2485088Swnj ti->ti_flags = flags; 24925940Skarels /* 25025940Skarels * Calculate receive window. Don't shrink window, 25125940Skarels * but avoid silly window syndrome. 25225940Skarels */ 25325940Skarels if (win < so->so_rcv.sb_hiwat / 4 && win < tp->t_maxseg) 25425940Skarels win = 0; 25525940Skarels if (win < (int)(tp->rcv_adv - tp->rcv_nxt)) 25625940Skarels win = (int)(tp->rcv_adv - tp->rcv_nxt); 25725940Skarels ti->ti_win = htons((u_short)win); 2585088Swnj if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 25926387Skarels ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 2605075Swnj ti->ti_flags |= TH_URG; 2615075Swnj } else 2625075Swnj /* 2635075Swnj * If no urgent pointer to send, then we pull 2645075Swnj * the urgent pointer to the left edge of the send window 2655075Swnj * so that it doesn't drift into the send window on sequence 2665075Swnj * number wraparound. 2675075Swnj */ 2685088Swnj tp->snd_up = tp->snd_una; /* drag it along */ 2697644Sroot /* 2707644Sroot * If anything to send and we can send it all, set PUSH. 2717644Sroot * (This will keep happy those implementations which only 27210143Ssam * give data to the user when a buffer fills or a PUSH comes in.) 2737644Sroot */ 2747644Sroot if (len && off+len == so->so_snd.sb_cc) 2757644Sroot ti->ti_flags |= TH_PUSH; 2765075Swnj 2775075Swnj /* 2785075Swnj * Put TCP length in extended header, and then 2795075Swnj * checksum extended header and data. 2805075Swnj */ 28125940Skarels if (len + optlen) 28225940Skarels ti->ti_len = htons((u_short)(sizeof(struct tcphdr) + 28325940Skarels optlen + len)); 2846162Ssam ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len); 2855075Swnj 2865075Swnj /* 2877125Swnj * In transmit state, time the transmission and arrange for 28821116Skarels * the retransmit. In persist state, just set snd_max. 2895088Swnj */ 29021116Skarels if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { 2917125Swnj /* 2927146Swnj * Advance snd_nxt over sequence space of this segment. 2937125Swnj */ 2947125Swnj if (flags & (TH_SYN|TH_FIN)) 2957125Swnj tp->snd_nxt++; 2967125Swnj tp->snd_nxt += len; 29715385Ssam if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 2987149Swnj tp->snd_max = tp->snd_nxt; 29915385Ssam /* 30015385Ssam * Time this transmission if not a retransmission and 30115385Ssam * not currently timing anything. 30215385Ssam */ 30315385Ssam if (tp->t_rtt == 0) { 30415385Ssam tp->t_rtt = 1; 30515385Ssam tp->t_rtseq = tp->snd_nxt - len; 30615385Ssam } 3077125Swnj } 3085088Swnj 3097125Swnj /* 31021116Skarels * Set retransmit timer if not currently set, 311*26443Skarels * and not doing an ack or a keep-alive probe. 31225940Skarels * Initial value for retransmit timer is tcp_beta*tp->t_srtt. 3137125Swnj * Initialize shift counter which is used for exponential 3147125Swnj * backoff of retransmit time. 3157125Swnj */ 3167125Swnj if (tp->t_timer[TCPT_REXMT] == 0 && 3177125Swnj tp->snd_nxt != tp->snd_una) { 3187125Swnj TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 3197125Swnj tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 3207125Swnj tp->t_rxtshift = 0; 321*26443Skarels tp->t_timer[TCPT_PERSIST] = 0; 3227125Swnj } 323*26443Skarels } else 32425940Skarels if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 32525940Skarels tp->snd_max = tp->snd_nxt + len; 3265163Swnj 3275163Swnj /* 3285268Sroot * Trace. 3295268Sroot */ 3307146Swnj if (so->so_options & SO_DEBUG) 3315268Sroot tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 3325268Sroot 3335268Sroot /* 3345075Swnj * Fill in IP length and desired time to live and 3355075Swnj * send to IP level. 3365075Swnj */ 3375441Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; 3385075Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 33926059Skarels error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 34026059Skarels so->so_options & SO_DONTROUTE); 34112418Ssam if (error) 3426505Ssam return (error); 3435075Swnj 3445075Swnj /* 3455075Swnj * Data sent (as far as we can tell). 3465075Swnj * If this advertises a larger window than any other segment, 3475245Sroot * then remember the size of the advertised window. 34825940Skarels * Any pending ACK has now been sent. 3495075Swnj */ 3505252Sroot if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 3515075Swnj tp->rcv_adv = tp->rcv_nxt + win; 3525088Swnj tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 35325940Skarels if (sendalot) 3547125Swnj goto again; 3556505Ssam return (0); 3564677Swnj } 3577125Swnj 3587125Swnj tcp_setpersist(tp) 3597125Swnj register struct tcpcb *tp; 3607125Swnj { 3617125Swnj 3627125Swnj if (tp->t_timer[TCPT_REXMT]) 3637125Swnj panic("tcp_output REXMT"); 3647125Swnj /* 3657125Swnj * Start/restart persistance timer. 3667125Swnj */ 3677125Swnj TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 3687125Swnj ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift, 3697125Swnj TCPTV_PERSMIN, TCPTV_MAX); 3707125Swnj tp->t_rxtshift++; 3717125Swnj if (tp->t_rxtshift >= TCP_MAXRXTSHIFT) 3727125Swnj tp->t_rxtshift = 0; 3737125Swnj } 374