1*7149Swnj /* tcp_output.c 4.41 82/06/12 */ 24677Swnj 34677Swnj #include "../h/param.h" 44677Swnj #include "../h/systm.h" 54677Swnj #include "../h/mbuf.h" 65163Swnj #include "../h/protosw.h" 74677Swnj #include "../h/socket.h" 84804Swnj #include "../h/socketvar.h" 95088Swnj #include "../net/in.h" 106352Ssam #include "../net/route.h" 115088Swnj #include "../net/in_pcb.h" 125088Swnj #include "../net/in_systm.h" 134804Swnj #include "../net/ip.h" 144900Swnj #include "../net/ip_var.h" 154804Swnj #include "../net/tcp.h" 165088Swnj #define TCPOUTFLAGS 175088Swnj #include "../net/tcp_fsm.h" 185088Swnj #include "../net/tcp_seq.h" 195088Swnj #include "../net/tcp_timer.h" 204804Swnj #include "../net/tcp_var.h" 215088Swnj #include "../net/tcpip.h" 225268Sroot #include "../net/tcp_debug.h" 236505Ssam #include <errno.h> 244677Swnj 255245Sroot char *tcpstates[]; /* XXX */ 265441Swnj 274678Swnj /* 285441Swnj * Initial options: indicate max segment length 1/2 of space 295441Swnj * allocated for receive; if TCPTRUEOOB is defined, indicate 305441Swnj * willingness to do true out-of-band. 315441Swnj */ 325441Swnj #ifndef TCPTRUEOOB 335441Swnj u_char tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, }; 345441Swnj #else 355441Swnj u_char tcp_initopt[6] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, TCPOPT_WILLOOB, 2 }; 365441Swnj #endif 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 COUNT(TCP_OUTPUT); 554678Swnj 565075Swnj /* 576279Swnj * Determine length of data that should be transmitted, 585088Swnj * and flags that will be used. 595088Swnj * If there is some data or critical controls (SYN, RST) 605088Swnj * to send, then transmit; otherwise, investigate further. 615075Swnj */ 627125Swnj again: 637125Swnj sendalot = 0; 645075Swnj off = tp->snd_nxt - tp->snd_una; 655163Swnj len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off; 665285Sroot if (len < 0) 676505Ssam return (0); /* ??? */ /* past FIN */ 687125Swnj if (len > tp->t_maxseg) { 695088Swnj len = tp->t_maxseg; 707125Swnj sendalot = 1; 717125Swnj } 726279Swnj 735088Swnj flags = tcp_outflags[tp->t_state]; 745299Sroot if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc) 755163Swnj flags &= ~TH_FIN; 766279Swnj if (flags & (TH_SYN|TH_RST|TH_FIN)) 775075Swnj goto send; 786279Swnj if (SEQ_GT(tp->snd_up, tp->snd_una)) 796279Swnj goto send; 804678Swnj 815075Swnj /* 826279Swnj * Sender silly window avoidance. If can send all data, 836279Swnj * a maximum segment, at least 1/4 of window do it, 846279Swnj * or are forced, do it; otherwise don't bother. 856279Swnj */ 866279Swnj if (len) { 876279Swnj if (len == tp->t_maxseg || off+len >= so->so_snd.sb_cc) 886279Swnj goto send; 896279Swnj if (len * 4 >= tp->snd_wnd) /* a lot */ 906279Swnj goto send; 916279Swnj if (tp->t_force) 926279Swnj goto send; 936279Swnj } 946279Swnj 956279Swnj /* 965285Sroot * Send if we owe peer an ACK. 975075Swnj */ 985441Swnj if (tp->t_flags&TF_ACKNOW) 995075Swnj goto send; 1004678Swnj 1015441Swnj #ifdef TCPTRUEOOB 1025075Swnj /* 1035441Swnj * Send if an out of band data or ack should be transmitted. 1045441Swnj */ 1055441Swnj if (tp->t_oobflags&(TCPOOB_OWEACK|TCPOOB_NEEDACK))) 1065441Swnj goto send; 1075441Swnj #endif 1085441Swnj 1095441Swnj /* 1105075Swnj * Calculate available window in i, and also amount 1115075Swnj * of window known to peer (as advertised window less 1125075Swnj * next expected input.) If this is 35% or more of the 1135075Swnj * maximum possible window, then want to send a segment to peer. 1145075Swnj */ 1155088Swnj win = sbspace(&so->so_rcv); 1165088Swnj if (win > 0 && 1175088Swnj ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35)) 1185075Swnj goto send; 1194678Swnj 1205075Swnj /* 1217125Swnj * TCP window updates are not reliable, rather a polling protocol 1227125Swnj * using ``persist'' packets is used to insure receipt of window 1237125Swnj * updates. The three ``states'' for the output side are: 1247125Swnj * idle not doing retransmits or persists 1257125Swnj * persisting to move a zero window 1267125Swnj * (re)transmitting and thereby not persisting 1277125Swnj * 1287125Swnj * tp->t_timer[TCPT_PERSIST] 1297125Swnj * is set when we are in persist state. 1307125Swnj * tp->t_force 1317125Swnj * is set when we are called to send a persist packet. 1327125Swnj * tp->t_timer[TCPT_REXMT] 1337125Swnj * is set when we are retransmitting 1347125Swnj * The output side is idle when both timers are zero. 1357125Swnj * 1367125Swnj * If send window is closed, there is data to transmit, and no 1377125Swnj * retransmit or persist is pending, then go to persist state, 1387125Swnj * arranging to force out a byte to get more current window information 1397125Swnj * if nothing happens soon. 1407125Swnj */ 1417125Swnj if (tp->snd_wnd == 0 && so->so_snd.sb_cc && 1427125Swnj tp->t_timer[TCPT_REXMT] == 0 && tp->t_timer[TCPT_PERSIST] == 0) { 1437125Swnj tp->t_rxtshift = 0; 1447125Swnj tcp_setpersist(tp); 1457125Swnj } 1467125Swnj 1477125Swnj /* 1485075Swnj * No reason to send a segment, just return. 1495075Swnj */ 1505110Swnj return (0); 1514678Swnj 1525075Swnj send: 1535075Swnj /* 1545075Swnj * Grab a header mbuf, attaching a copy of data to 1555075Swnj * be transmitted, and initialize the header from 1565075Swnj * the template for sends on this connection. 1575075Swnj */ 1584677Swnj MGET(m, 0); 1594677Swnj if (m == 0) 1606505Ssam return (ENOBUFS); 1615245Sroot m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 1624885Swnj m->m_len = sizeof (struct tcpiphdr); 1635075Swnj if (len) { 1645075Swnj m->m_next = m_copy(so->so_snd.sb_mb, off, len); 1655075Swnj if (m->m_next == 0) 1665075Swnj len = 0; 1675075Swnj } 1685075Swnj ti = mtod(m, struct tcpiphdr *); 1695075Swnj if (tp->t_template == 0) 1705075Swnj panic("tcp_output"); 1715110Swnj bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 1725075Swnj 1735075Swnj /* 1745075Swnj * Fill in fields, remembering maximum advertised 1755075Swnj * window for use in delaying messages about window sizes. 1765075Swnj */ 1775245Sroot ti->ti_seq = tp->snd_nxt; 1785245Sroot ti->ti_ack = tp->rcv_nxt; 1795245Sroot #if vax 1805245Sroot ti->ti_seq = htonl(ti->ti_seq); 1815245Sroot ti->ti_ack = htonl(ti->ti_ack); 1825245Sroot #endif 1835441Swnj /* 1845441Swnj * Before ESTABLISHED, force sending of initial options 1855441Swnj * unless TCP set to not do any options. 1865441Swnj */ 1875441Swnj if (tp->t_state < TCPS_ESTABLISHED) { 1885441Swnj if (tp->t_flags&TF_NOOPT) 1895441Swnj goto noopt; 1905441Swnj opt = tcp_initopt; 1915441Swnj optlen = sizeof (tcp_initopt); 1925441Swnj *(u_short *)(opt + 2) = so->so_rcv.sb_hiwat / 2; 1935441Swnj #if vax 1945441Swnj *(u_short *)(opt + 2) = htons(*(u_short *)(opt + 2)); 1955441Swnj #endif 1965441Swnj } else { 1975441Swnj if (tp->t_tcpopt == 0) 1985441Swnj goto noopt; 1995441Swnj opt = mtod(tp->t_tcpopt, u_char *); 2005441Swnj optlen = tp->t_tcpopt->m_len; 2015441Swnj } 2025441Swnj #ifndef TCPTRUEOOB 2035441Swnj if (opt) 2045441Swnj #else 2055441Swnj if (opt || (tp->t_oobflags&(TCPOOB_OWEACK|TCPOOB_NEEDACK))) 2065441Swnj #endif 2075441Swnj { 2085110Swnj m0 = m->m_next; 2095584Sroot m->m_next = m_get(M_DONTWAIT); 2105088Swnj if (m->m_next == 0) { 2115088Swnj (void) m_free(m); 2125441Swnj m_freem(m0); 2136505Ssam return (ENOBUFS); 2145088Swnj } 2155088Swnj m->m_next->m_next = m0; 2165441Swnj m0 = m->m_next; 2175441Swnj m0->m_off = MMINOFF; 2185441Swnj m0->m_len = optlen; 2196162Ssam bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); 2205441Swnj opt = (u_char *)(mtod(m0, caddr_t) + optlen); 2215441Swnj #ifdef TCPTRUEOOB 2225441Swnj if (tp->t_oobflags&TCPOOB_OWEACK) { 2235441Swnj printf("tp %x send OOBACK for %x\n", tp->t_iobseq); 2245441Swnj *opt++ = TCPOPT_OOBACK; 2255441Swnj *opt++ = 3; 2265441Swnj *opt++ = tp->t_iobseq; 2275441Swnj m0->m_len += 3; 2285441Swnj tp->t_oobflags &= ~TCPOOB_OWEACK; 2295441Swnj /* sender should rexmt oob to force ack repeat */ 2305441Swnj } 2315441Swnj if (tp->t_oobflags&TCPOOB_NEEDACK) { 2325441Swnj printf("tp %x send OOBDATA seq %x data %x\n", tp->t_oobseq, tp->t_oobc); 2335441Swnj *opt++ = TCPOPT_OOBDATA; 2345548Swnj *opt++ = 8; 2355441Swnj *opt++ = tp->t_oobseq; 2365441Swnj *opt++ = tp->t_oobc; 2375548Swnj *(tcp_seq *)opt = tp->t_oobmark - tp->snd_nxt; 2385548Swnj #ifdef vax 2395548Swnj *(tcp_seq *)opt = htonl((unsigned)*(tcp_seq *)opt); 2405548Swnj #endif 2415548Swnj m0->m_len += 8; 2425441Swnj TCPT_RANGESET(tp->t_timer[TCPT_OOBREXMT], 2435441Swnj tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 2445441Swnj } 2455441Swnj #endif 2465441Swnj while (m0->m_len & 0x3) { 2475441Swnj *opt++ = TCPOPT_EOL; 2485441Swnj m0->m_len++; 2495441Swnj } 2505441Swnj optlen = m0->m_len; 2515441Swnj ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 2525088Swnj } 2535441Swnj noopt: 2545088Swnj ti->ti_flags = flags; 2555075Swnj win = sbspace(&so->so_rcv); 2566279Swnj if (win < so->so_rcv.sb_hiwat / 4) /* avoid silly window */ 2576279Swnj win = 0; 2585075Swnj if (win > 0) 2596279Swnj #if vax 2605110Swnj ti->ti_win = htons((u_short)win); 2616279Swnj #else 2626279Swnj ti->ti_win = win; 2636279Swnj #endif 2645088Swnj if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 2655420Swnj ti->ti_urp = tp->snd_up - tp->snd_nxt; 2665420Swnj #if vax 2675420Swnj ti->ti_urp = htons(ti->ti_urp); 2685420Swnj #endif 2695075Swnj ti->ti_flags |= TH_URG; 2705075Swnj } else 2715075Swnj /* 2725075Swnj * If no urgent pointer to send, then we pull 2735075Swnj * the urgent pointer to the left edge of the send window 2745075Swnj * so that it doesn't drift into the send window on sequence 2755075Swnj * number wraparound. 2765075Swnj */ 2775088Swnj tp->snd_up = tp->snd_una; /* drag it along */ 2785088Swnj /* PUSH */ 2795075Swnj 2805075Swnj /* 2815075Swnj * Put TCP length in extended header, and then 2825075Swnj * checksum extended header and data. 2835075Swnj */ 2845441Swnj if (len + optlen) { 2855441Swnj ti->ti_len = sizeof (struct tcphdr) + optlen + len; 2865441Swnj #if vax 2875441Swnj ti->ti_len = htons((u_short)ti->ti_len); 2885441Swnj #endif 2895441Swnj } 2906162Ssam ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len); 2915075Swnj 2925075Swnj /* 2937125Swnj * In transmit state, time the transmission and arrange for 2947125Swnj * the retransmit. In persist state, reset persist time for 2957125Swnj * next persist. 2965088Swnj */ 2977125Swnj if (tp->t_force == 0) { 2987125Swnj /* 2997146Swnj * Advance snd_nxt over sequence space of this segment. 3007125Swnj */ 3017125Swnj if (flags & (TH_SYN|TH_FIN)) 3027125Swnj tp->snd_nxt++; 3037125Swnj tp->snd_nxt += len; 304*7149Swnj if (SEQ_GT(tp->snd_nxt, tp->snd_max)) 305*7149Swnj tp->snd_max = tp->snd_nxt; 3065088Swnj 3077125Swnj /* 3087125Swnj * Time this transmission if not a retransmission and 3097125Swnj * not currently timing anything. 3107125Swnj */ 3117125Swnj if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) { 3127125Swnj tp->t_rtt = 1; 3137125Swnj tp->t_rtseq = tp->snd_nxt - len; 3147125Swnj } 3155088Swnj 3167125Swnj /* 3177125Swnj * Set retransmit timer if not currently set. 3187125Swnj * Initial value for retransmit timer to tcp_beta*tp->t_srtt. 3197125Swnj * Initialize shift counter which is used for exponential 3207125Swnj * backoff of retransmit time. 3217125Swnj */ 3227125Swnj if (tp->t_timer[TCPT_REXMT] == 0 && 3237125Swnj tp->snd_nxt != tp->snd_una) { 3247125Swnj TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 3257125Swnj tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 3267125Swnj tp->t_rtt = 0; 3277125Swnj tp->t_rxtshift = 0; 3287125Swnj } 3297125Swnj tp->t_timer[TCPT_PERSIST] = 0; 330*7149Swnj } else { 331*7149Swnj if (SEQ_GT(tp->snd_una+1, tp->snd_max)) 332*7149Swnj tp->snd_max = tp->snd_una+1; 3337146Swnj } 3345163Swnj 3355163Swnj /* 3365268Sroot * Trace. 3375268Sroot */ 3387146Swnj if (so->so_options & SO_DEBUG) 3395268Sroot tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 3405268Sroot 3415268Sroot /* 3425075Swnj * Fill in IP length and desired time to live and 3435075Swnj * send to IP level. 3445075Swnj */ 3455441Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; 3465075Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 3477146Swnj if (error = ip_output(m, tp->t_ipopt, (so->so_options & SO_DONTROUTE) ? 3487146Swnj &routetoif : &tp->t_inpcb->inp_route, 0)) 3496505Ssam return (error); 3505075Swnj 3515075Swnj /* 3525075Swnj * Data sent (as far as we can tell). 3535075Swnj * If this advertises a larger window than any other segment, 3545245Sroot * then remember the size of the advertised window. 3555088Swnj * Drop send for purpose of ACK requirements. 3565075Swnj */ 3575252Sroot if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 3585075Swnj tp->rcv_adv = tp->rcv_nxt + win; 3595088Swnj tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 3607125Swnj if (sendalot && tp->t_force == 0) 3617125Swnj goto again; 3626505Ssam return (0); 3634677Swnj } 3647125Swnj 3657125Swnj tcp_setpersist(tp) 3667125Swnj register struct tcpcb *tp; 3677125Swnj { 3687125Swnj 3697125Swnj if (tp->t_timer[TCPT_REXMT]) 3707125Swnj panic("tcp_output REXMT"); 3717125Swnj /* 3727125Swnj * Start/restart persistance timer. 3737125Swnj */ 3747125Swnj TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 3757125Swnj ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift, 3767125Swnj TCPTV_PERSMIN, TCPTV_MAX); 3777125Swnj tp->t_rxtshift++; 3787125Swnj if (tp->t_rxtshift >= TCP_MAXRXTSHIFT) 3797125Swnj tp->t_rxtshift = 0; 3807125Swnj } 381