1*17361Skarels /* tcp_output.c 6.7 84/11/14 */ 24677Swnj 317063Sbloom #include "param.h" 417063Sbloom #include "systm.h" 517063Sbloom #include "mbuf.h" 617063Sbloom #include "protosw.h" 717063Sbloom #include "socket.h" 817063Sbloom #include "socketvar.h" 917063Sbloom #include "errno.h" 1010895Ssam 1110895Ssam #include "../net/route.h" 1210895Ssam 1317063Sbloom #include "in.h" 1417063Sbloom #include "in_pcb.h" 1517063Sbloom #include "in_systm.h" 1617063Sbloom #include "ip.h" 1717063Sbloom #include "ip_var.h" 1817063Sbloom #include "tcp.h" 195088Swnj #define TCPOUTFLAGS 2017063Sbloom #include "tcp_fsm.h" 2117063Sbloom #include "tcp_seq.h" 2217063Sbloom #include "tcp_timer.h" 2317063Sbloom #include "tcp_var.h" 2417063Sbloom #include "tcpip.h" 2517063Sbloom #include "tcp_debug.h" 264677Swnj 274678Swnj /* 288314Sroot * Initial options. 295441Swnj */ 305441Swnj u_char tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, }; 315441Swnj 325441Swnj /* 335245Sroot * Tcp output routine: figure out what should be sent and send it. 344678Swnj */ 355075Swnj tcp_output(tp) 365075Swnj register struct tcpcb *tp; 374678Swnj { 385075Swnj register struct socket *so = tp->t_inpcb->inp_socket; 395075Swnj register int len; 405075Swnj struct mbuf *m0; 416505Ssam int off, flags, win, error; 425075Swnj register struct mbuf *m; 435075Swnj register struct tcpiphdr *ti; 445441Swnj u_char *opt; 455441Swnj unsigned optlen = 0; 467125Swnj int sendalot; 474678Swnj 485075Swnj /* 496279Swnj * Determine length of data that should be transmitted, 505088Swnj * and flags that will be used. 515088Swnj * If there is some data or critical controls (SYN, RST) 525088Swnj * to send, then transmit; otherwise, investigate further. 535075Swnj */ 547125Swnj again: 557125Swnj sendalot = 0; 565075Swnj off = tp->snd_nxt - tp->snd_una; 57*17361Skarels win = MIN(tp->snd_wnd, tp->snd_cwnd) + tp->t_force; 58*17361Skarels len = MIN(so->so_snd.sb_cc, win) - off; 595285Sroot if (len < 0) 606505Ssam return (0); /* ??? */ /* past FIN */ 617125Swnj if (len > tp->t_maxseg) { 625088Swnj len = tp->t_maxseg; 6317318Skarels /* 6417318Skarels * Don't send more than one segment if retransmitting. 6517318Skarels */ 66*17361Skarels if (tp->t_rxtshift == 0) 6717318Skarels sendalot = 1; 687125Swnj } 696279Swnj 705088Swnj flags = tcp_outflags[tp->t_state]; 715299Sroot if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc) 725163Swnj flags &= ~TH_FIN; 736279Swnj if (flags & (TH_SYN|TH_RST|TH_FIN)) 745075Swnj goto send; 756279Swnj if (SEQ_GT(tp->snd_up, tp->snd_una)) 766279Swnj goto send; 774678Swnj 785075Swnj /* 7917318Skarels * Sender silly window avoidance. If connection is idle 8017318Skarels * and can send all data, a maximum segment, 8117318Skarels * at least a maximum default-size segment do it, 826279Swnj * or are forced, do it; otherwise don't bother. 836279Swnj */ 846279Swnj if (len) { 8517318Skarels if (len == tp->t_maxseg || len >= so->so_snd.sb_cc) /* off = 0*/ 866279Swnj goto send; 8717318Skarels if (len >= TCP_MSS) /* a lot */ 886279Swnj goto send; 896279Swnj if (tp->t_force) 906279Swnj goto send; 916279Swnj } 926279Swnj 936279Swnj /* 945285Sroot * Send if we owe peer an ACK. 955075Swnj */ 965441Swnj if (tp->t_flags&TF_ACKNOW) 975075Swnj goto send; 984678Swnj 995441Swnj 1005441Swnj /* 101*17361Skarels * Calculate available window, and also amount 1025075Swnj * of window known to peer (as advertised window less 103*17361Skarels * next expected input.) If the difference is 35% or more of the 104*17361Skarels * maximum possible window, then want to send a window update to peer. 1055075Swnj */ 1065088Swnj win = sbspace(&so->so_rcv); 1075088Swnj if (win > 0 && 1085088Swnj ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35)) 1095075Swnj goto send; 1104678Swnj 1115075Swnj /* 1127125Swnj * TCP window updates are not reliable, rather a polling protocol 1137125Swnj * using ``persist'' packets is used to insure receipt of window 1147125Swnj * updates. The three ``states'' for the output side are: 1157125Swnj * idle not doing retransmits or persists 1167125Swnj * persisting to move a zero window 1177125Swnj * (re)transmitting and thereby not persisting 1187125Swnj * 1197125Swnj * tp->t_timer[TCPT_PERSIST] 1207125Swnj * is set when we are in persist state. 1217125Swnj * tp->t_force 1227125Swnj * is set when we are called to send a persist packet. 1237125Swnj * tp->t_timer[TCPT_REXMT] 1247125Swnj * is set when we are retransmitting 1257125Swnj * The output side is idle when both timers are zero. 1267125Swnj * 1277125Swnj * If send window is closed, there is data to transmit, and no 1287125Swnj * retransmit or persist is pending, then go to persist state, 1297125Swnj * arranging to force out a byte to get more current window information 1307125Swnj * if nothing happens soon. 1317125Swnj */ 1327125Swnj if (tp->snd_wnd == 0 && so->so_snd.sb_cc && 1337125Swnj tp->t_timer[TCPT_REXMT] == 0 && tp->t_timer[TCPT_PERSIST] == 0) { 1347125Swnj tp->t_rxtshift = 0; 1357125Swnj tcp_setpersist(tp); 1367125Swnj } 1377125Swnj 1387125Swnj /* 1395075Swnj * No reason to send a segment, just return. 1405075Swnj */ 1415110Swnj return (0); 1424678Swnj 1435075Swnj send: 1445075Swnj /* 1455075Swnj * Grab a header mbuf, attaching a copy of data to 1465075Swnj * be transmitted, and initialize the header from 1475075Swnj * the template for sends on this connection. 1485075Swnj */ 14911720Ssam MGET(m, M_DONTWAIT, MT_HEADER); 15011720Ssam if (m == NULL) 1516505Ssam return (ENOBUFS); 1525245Sroot m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 1534885Swnj m->m_len = sizeof (struct tcpiphdr); 1545075Swnj if (len) { 1555075Swnj m->m_next = m_copy(so->so_snd.sb_mb, off, len); 1565075Swnj if (m->m_next == 0) 1575075Swnj len = 0; 1585075Swnj } 1595075Swnj ti = mtod(m, struct tcpiphdr *); 1605075Swnj if (tp->t_template == 0) 1615075Swnj panic("tcp_output"); 1625110Swnj bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 1635075Swnj 1645075Swnj /* 1655075Swnj * Fill in fields, remembering maximum advertised 1665075Swnj * window for use in delaying messages about window sizes. 1675075Swnj */ 1685245Sroot ti->ti_seq = tp->snd_nxt; 1695245Sroot ti->ti_ack = tp->rcv_nxt; 1705245Sroot ti->ti_seq = htonl(ti->ti_seq); 1715245Sroot ti->ti_ack = htonl(ti->ti_ack); 1725441Swnj /* 1735441Swnj * Before ESTABLISHED, force sending of initial options 1745441Swnj * unless TCP set to not do any options. 1755441Swnj */ 1765441Swnj if (tp->t_state < TCPS_ESTABLISHED) { 17717273Skarels int mss; 17817273Skarels 1795441Swnj if (tp->t_flags&TF_NOOPT) 1805441Swnj goto noopt; 18117273Skarels mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp)); 18217273Skarels if (mss <= IP_MSS - sizeof(struct tcpiphdr)) 18317273Skarels goto noopt; 1845441Swnj opt = tcp_initopt; 1855441Swnj optlen = sizeof (tcp_initopt); 18617273Skarels *(u_short *)(opt + 2) = htons(mss); 1875441Swnj } else { 1885441Swnj if (tp->t_tcpopt == 0) 1895441Swnj goto noopt; 1905441Swnj opt = mtod(tp->t_tcpopt, u_char *); 1915441Swnj optlen = tp->t_tcpopt->m_len; 1925441Swnj } 1938314Sroot if (opt) { 1945110Swnj m0 = m->m_next; 1959643Ssam m->m_next = m_get(M_DONTWAIT, MT_DATA); 1965088Swnj if (m->m_next == 0) { 1975088Swnj (void) m_free(m); 1985441Swnj m_freem(m0); 1996505Ssam return (ENOBUFS); 2005088Swnj } 2015088Swnj m->m_next->m_next = m0; 2025441Swnj m0 = m->m_next; 2035441Swnj m0->m_len = optlen; 2046162Ssam bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); 2055441Swnj opt = (u_char *)(mtod(m0, caddr_t) + optlen); 2065441Swnj while (m0->m_len & 0x3) { 2075441Swnj *opt++ = TCPOPT_EOL; 2085441Swnj m0->m_len++; 2095441Swnj } 2105441Swnj optlen = m0->m_len; 2115441Swnj ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 2125088Swnj } 2135441Swnj noopt: 2145088Swnj ti->ti_flags = flags; 2155075Swnj win = sbspace(&so->so_rcv); 2166279Swnj if (win < so->so_rcv.sb_hiwat / 4) /* avoid silly window */ 2176279Swnj win = 0; 2185075Swnj if (win > 0) 2195110Swnj ti->ti_win = htons((u_short)win); 2205088Swnj if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 2215420Swnj ti->ti_urp = tp->snd_up - tp->snd_nxt; 2225420Swnj ti->ti_urp = htons(ti->ti_urp); 2235075Swnj ti->ti_flags |= TH_URG; 2245075Swnj } else 2255075Swnj /* 2265075Swnj * If no urgent pointer to send, then we pull 2275075Swnj * the urgent pointer to the left edge of the send window 2285075Swnj * so that it doesn't drift into the send window on sequence 2295075Swnj * number wraparound. 2305075Swnj */ 2315088Swnj tp->snd_up = tp->snd_una; /* drag it along */ 2327644Sroot /* 2337644Sroot * If anything to send and we can send it all, set PUSH. 2347644Sroot * (This will keep happy those implementations which only 23510143Ssam * give data to the user when a buffer fills or a PUSH comes in.) 2367644Sroot */ 2377644Sroot if (len && off+len == so->so_snd.sb_cc) 2387644Sroot ti->ti_flags |= TH_PUSH; 2395075Swnj 2405075Swnj /* 2415075Swnj * Put TCP length in extended header, and then 2425075Swnj * checksum extended header and data. 2435075Swnj */ 2445441Swnj if (len + optlen) { 2455441Swnj ti->ti_len = sizeof (struct tcphdr) + optlen + len; 2465441Swnj ti->ti_len = htons((u_short)ti->ti_len); 2475441Swnj } 2486162Ssam ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len); 2495075Swnj 2505075Swnj /* 2517125Swnj * In transmit state, time the transmission and arrange for 2527125Swnj * the retransmit. In persist state, reset persist time for 2537125Swnj * next persist. 2545088Swnj */ 2557125Swnj if (tp->t_force == 0) { 2567125Swnj /* 2577146Swnj * Advance snd_nxt over sequence space of this segment. 2587125Swnj */ 2597125Swnj if (flags & (TH_SYN|TH_FIN)) 2607125Swnj tp->snd_nxt++; 2617125Swnj tp->snd_nxt += len; 26215385Ssam if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 2637149Swnj tp->snd_max = tp->snd_nxt; 26415385Ssam /* 26515385Ssam * Time this transmission if not a retransmission and 26615385Ssam * not currently timing anything. 26715385Ssam */ 26815385Ssam if (tp->t_rtt == 0) { 26915385Ssam tp->t_rtt = 1; 27015385Ssam tp->t_rtseq = tp->snd_nxt - len; 27115385Ssam } 2727125Swnj } 2735088Swnj 2747125Swnj /* 2757125Swnj * Set retransmit timer if not currently set. 2767125Swnj * Initial value for retransmit timer to tcp_beta*tp->t_srtt. 2777125Swnj * Initialize shift counter which is used for exponential 2787125Swnj * backoff of retransmit time. 2797125Swnj */ 2807125Swnj if (tp->t_timer[TCPT_REXMT] == 0 && 2817125Swnj tp->snd_nxt != tp->snd_una) { 2827125Swnj TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 2837125Swnj tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 2847125Swnj tp->t_rxtshift = 0; 2857125Swnj } 2867125Swnj tp->t_timer[TCPT_PERSIST] = 0; 2877149Swnj } else { 2887149Swnj if (SEQ_GT(tp->snd_una+1, tp->snd_max)) 2897149Swnj tp->snd_max = tp->snd_una+1; 2907146Swnj } 2915163Swnj 2925163Swnj /* 2935268Sroot * Trace. 2945268Sroot */ 2957146Swnj if (so->so_options & SO_DEBUG) 2965268Sroot tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 2975268Sroot 2985268Sroot /* 2995075Swnj * Fill in IP length and desired time to live and 3005075Swnj * send to IP level. 3015075Swnj */ 3025441Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; 3035075Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 30412418Ssam if (so->so_options & SO_DONTROUTE) 30512765Ssam error = 30612765Ssam ip_output(m, tp->t_ipopt, (struct route *)0, IP_ROUTETOIF); 30712418Ssam else 30812418Ssam error = ip_output(m, tp->t_ipopt, &tp->t_inpcb->inp_route, 0); 30912418Ssam if (error) 3106505Ssam return (error); 3115075Swnj 3125075Swnj /* 3135075Swnj * Data sent (as far as we can tell). 3145075Swnj * If this advertises a larger window than any other segment, 3155245Sroot * then remember the size of the advertised window. 3165088Swnj * Drop send for purpose of ACK requirements. 3175075Swnj */ 3185252Sroot if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 3195075Swnj tp->rcv_adv = tp->rcv_nxt + win; 3205088Swnj tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 3217125Swnj if (sendalot && tp->t_force == 0) 3227125Swnj goto again; 3236505Ssam return (0); 3244677Swnj } 3257125Swnj 3267125Swnj tcp_setpersist(tp) 3277125Swnj register struct tcpcb *tp; 3287125Swnj { 3297125Swnj 3307125Swnj if (tp->t_timer[TCPT_REXMT]) 3317125Swnj panic("tcp_output REXMT"); 3327125Swnj /* 3337125Swnj * Start/restart persistance timer. 3347125Swnj */ 3357125Swnj TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 3367125Swnj ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift, 3377125Swnj TCPTV_PERSMIN, TCPTV_MAX); 3387125Swnj tp->t_rxtshift++; 3397125Swnj if (tp->t_rxtshift >= TCP_MAXRXTSHIFT) 3407125Swnj tp->t_rxtshift = 0; 3417125Swnj } 342