1*7644Sroot /* tcp_output.c 4.43 82/08/02 */ 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 544678Swnj 555075Swnj /* 566279Swnj * Determine length of data that should be transmitted, 575088Swnj * and flags that will be used. 585088Swnj * If there is some data or critical controls (SYN, RST) 595088Swnj * to send, then transmit; otherwise, investigate further. 605075Swnj */ 617125Swnj again: 627125Swnj sendalot = 0; 635075Swnj off = tp->snd_nxt - tp->snd_una; 645163Swnj len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off; 655285Sroot if (len < 0) 666505Ssam return (0); /* ??? */ /* past FIN */ 677125Swnj if (len > tp->t_maxseg) { 685088Swnj len = tp->t_maxseg; 697125Swnj sendalot = 1; 707125Swnj } 716279Swnj 725088Swnj flags = tcp_outflags[tp->t_state]; 735299Sroot if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc) 745163Swnj flags &= ~TH_FIN; 756279Swnj if (flags & (TH_SYN|TH_RST|TH_FIN)) 765075Swnj goto send; 776279Swnj if (SEQ_GT(tp->snd_up, tp->snd_una)) 786279Swnj goto send; 794678Swnj 805075Swnj /* 816279Swnj * Sender silly window avoidance. If can send all data, 826279Swnj * a maximum segment, at least 1/4 of window do it, 836279Swnj * or are forced, do it; otherwise don't bother. 846279Swnj */ 856279Swnj if (len) { 866279Swnj if (len == tp->t_maxseg || off+len >= so->so_snd.sb_cc) 876279Swnj goto send; 886279Swnj if (len * 4 >= tp->snd_wnd) /* a lot */ 896279Swnj goto send; 906279Swnj if (tp->t_force) 916279Swnj goto send; 926279Swnj } 936279Swnj 946279Swnj /* 955285Sroot * Send if we owe peer an ACK. 965075Swnj */ 975441Swnj if (tp->t_flags&TF_ACKNOW) 985075Swnj goto send; 994678Swnj 1005441Swnj #ifdef TCPTRUEOOB 1015075Swnj /* 1025441Swnj * Send if an out of band data or ack should be transmitted. 1035441Swnj */ 1045441Swnj if (tp->t_oobflags&(TCPOOB_OWEACK|TCPOOB_NEEDACK))) 1055441Swnj goto send; 1065441Swnj #endif 1075441Swnj 1085441Swnj /* 1095075Swnj * Calculate available window in i, and also amount 1105075Swnj * of window known to peer (as advertised window less 1115075Swnj * next expected input.) If this is 35% or more of the 1125075Swnj * maximum possible window, then want to send a segment to peer. 1135075Swnj */ 1145088Swnj win = sbspace(&so->so_rcv); 1155088Swnj if (win > 0 && 1165088Swnj ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35)) 1175075Swnj goto send; 1184678Swnj 1195075Swnj /* 1207125Swnj * TCP window updates are not reliable, rather a polling protocol 1217125Swnj * using ``persist'' packets is used to insure receipt of window 1227125Swnj * updates. The three ``states'' for the output side are: 1237125Swnj * idle not doing retransmits or persists 1247125Swnj * persisting to move a zero window 1257125Swnj * (re)transmitting and thereby not persisting 1267125Swnj * 1277125Swnj * tp->t_timer[TCPT_PERSIST] 1287125Swnj * is set when we are in persist state. 1297125Swnj * tp->t_force 1307125Swnj * is set when we are called to send a persist packet. 1317125Swnj * tp->t_timer[TCPT_REXMT] 1327125Swnj * is set when we are retransmitting 1337125Swnj * The output side is idle when both timers are zero. 1347125Swnj * 1357125Swnj * If send window is closed, there is data to transmit, and no 1367125Swnj * retransmit or persist is pending, then go to persist state, 1377125Swnj * arranging to force out a byte to get more current window information 1387125Swnj * if nothing happens soon. 1397125Swnj */ 1407125Swnj if (tp->snd_wnd == 0 && so->so_snd.sb_cc && 1417125Swnj tp->t_timer[TCPT_REXMT] == 0 && tp->t_timer[TCPT_PERSIST] == 0) { 1427125Swnj tp->t_rxtshift = 0; 1437125Swnj tcp_setpersist(tp); 1447125Swnj } 1457125Swnj 1467125Swnj /* 1475075Swnj * No reason to send a segment, just return. 1485075Swnj */ 1495110Swnj return (0); 1504678Swnj 1515075Swnj send: 1525075Swnj /* 1535075Swnj * Grab a header mbuf, attaching a copy of data to 1545075Swnj * be transmitted, and initialize the header from 1555075Swnj * the template for sends on this connection. 1565075Swnj */ 1574677Swnj MGET(m, 0); 1584677Swnj if (m == 0) 1596505Ssam return (ENOBUFS); 1605245Sroot m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 1614885Swnj m->m_len = sizeof (struct tcpiphdr); 1625075Swnj if (len) { 1635075Swnj m->m_next = m_copy(so->so_snd.sb_mb, off, len); 1645075Swnj if (m->m_next == 0) 1655075Swnj len = 0; 1665075Swnj } 1675075Swnj ti = mtod(m, struct tcpiphdr *); 1685075Swnj if (tp->t_template == 0) 1695075Swnj panic("tcp_output"); 1705110Swnj bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 1715075Swnj 1725075Swnj /* 1735075Swnj * Fill in fields, remembering maximum advertised 1745075Swnj * window for use in delaying messages about window sizes. 1755075Swnj */ 1765245Sroot ti->ti_seq = tp->snd_nxt; 1775245Sroot ti->ti_ack = tp->rcv_nxt; 1785245Sroot #if vax 1795245Sroot ti->ti_seq = htonl(ti->ti_seq); 1805245Sroot ti->ti_ack = htonl(ti->ti_ack); 1815245Sroot #endif 1825441Swnj /* 1835441Swnj * Before ESTABLISHED, force sending of initial options 1845441Swnj * unless TCP set to not do any options. 1855441Swnj */ 1865441Swnj if (tp->t_state < TCPS_ESTABLISHED) { 1875441Swnj if (tp->t_flags&TF_NOOPT) 1885441Swnj goto noopt; 1895441Swnj opt = tcp_initopt; 1905441Swnj optlen = sizeof (tcp_initopt); 1915441Swnj *(u_short *)(opt + 2) = so->so_rcv.sb_hiwat / 2; 1925441Swnj #if vax 1935441Swnj *(u_short *)(opt + 2) = htons(*(u_short *)(opt + 2)); 1945441Swnj #endif 1955441Swnj } else { 1965441Swnj if (tp->t_tcpopt == 0) 1975441Swnj goto noopt; 1985441Swnj opt = mtod(tp->t_tcpopt, u_char *); 1995441Swnj optlen = tp->t_tcpopt->m_len; 2005441Swnj } 2015441Swnj #ifndef TCPTRUEOOB 2025441Swnj if (opt) 2035441Swnj #else 2045441Swnj if (opt || (tp->t_oobflags&(TCPOOB_OWEACK|TCPOOB_NEEDACK))) 2055441Swnj #endif 2065441Swnj { 2075110Swnj m0 = m->m_next; 2085584Sroot m->m_next = m_get(M_DONTWAIT); 2095088Swnj if (m->m_next == 0) { 2105088Swnj (void) m_free(m); 2115441Swnj m_freem(m0); 2126505Ssam return (ENOBUFS); 2135088Swnj } 2145088Swnj m->m_next->m_next = m0; 2155441Swnj m0 = m->m_next; 2165441Swnj m0->m_off = MMINOFF; 2175441Swnj m0->m_len = optlen; 2186162Ssam bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); 2195441Swnj opt = (u_char *)(mtod(m0, caddr_t) + optlen); 2205441Swnj #ifdef TCPTRUEOOB 2215441Swnj if (tp->t_oobflags&TCPOOB_OWEACK) { 2225441Swnj printf("tp %x send OOBACK for %x\n", tp->t_iobseq); 2235441Swnj *opt++ = TCPOPT_OOBACK; 2245441Swnj *opt++ = 3; 2255441Swnj *opt++ = tp->t_iobseq; 2265441Swnj m0->m_len += 3; 2275441Swnj tp->t_oobflags &= ~TCPOOB_OWEACK; 2285441Swnj /* sender should rexmt oob to force ack repeat */ 2295441Swnj } 2305441Swnj if (tp->t_oobflags&TCPOOB_NEEDACK) { 2315441Swnj printf("tp %x send OOBDATA seq %x data %x\n", tp->t_oobseq, tp->t_oobc); 2325441Swnj *opt++ = TCPOPT_OOBDATA; 2335548Swnj *opt++ = 8; 2345441Swnj *opt++ = tp->t_oobseq; 2355441Swnj *opt++ = tp->t_oobc; 2365548Swnj *(tcp_seq *)opt = tp->t_oobmark - tp->snd_nxt; 2375548Swnj #ifdef vax 2385548Swnj *(tcp_seq *)opt = htonl((unsigned)*(tcp_seq *)opt); 2395548Swnj #endif 2405548Swnj m0->m_len += 8; 2415441Swnj TCPT_RANGESET(tp->t_timer[TCPT_OOBREXMT], 2425441Swnj tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 2435441Swnj } 2445441Swnj #endif 2455441Swnj while (m0->m_len & 0x3) { 2465441Swnj *opt++ = TCPOPT_EOL; 2475441Swnj m0->m_len++; 2485441Swnj } 2495441Swnj optlen = m0->m_len; 2505441Swnj ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 2515088Swnj } 2525441Swnj noopt: 2535088Swnj ti->ti_flags = flags; 2545075Swnj win = sbspace(&so->so_rcv); 2556279Swnj if (win < so->so_rcv.sb_hiwat / 4) /* avoid silly window */ 2566279Swnj win = 0; 2575075Swnj if (win > 0) 2586279Swnj #if vax 2595110Swnj ti->ti_win = htons((u_short)win); 2606279Swnj #else 2616279Swnj ti->ti_win = win; 2626279Swnj #endif 2635088Swnj if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 2645420Swnj ti->ti_urp = tp->snd_up - tp->snd_nxt; 2655420Swnj #if vax 2665420Swnj ti->ti_urp = htons(ti->ti_urp); 2675420Swnj #endif 2685075Swnj ti->ti_flags |= TH_URG; 2695075Swnj } else 2705075Swnj /* 2715075Swnj * If no urgent pointer to send, then we pull 2725075Swnj * the urgent pointer to the left edge of the send window 2735075Swnj * so that it doesn't drift into the send window on sequence 2745075Swnj * number wraparound. 2755075Swnj */ 2765088Swnj tp->snd_up = tp->snd_una; /* drag it along */ 277*7644Sroot /* 278*7644Sroot * If anything to send and we can send it all, set PUSH. 279*7644Sroot * (This will keep happy those implementations which only 280*7644Sroot * give data to the user when a buffer fills or a PUSH comes in. 281*7644Sroot */ 282*7644Sroot /* if (len && (ti->ti_flags & (TH_FIN|TH_RST|TH_SYN)) == 0) */ 283*7644Sroot if (len && off+len == so->so_snd.sb_cc) 284*7644Sroot ti->ti_flags |= TH_PUSH; 2855075Swnj 2865075Swnj /* 2875075Swnj * Put TCP length in extended header, and then 2885075Swnj * checksum extended header and data. 2895075Swnj */ 2905441Swnj if (len + optlen) { 2915441Swnj ti->ti_len = sizeof (struct tcphdr) + optlen + len; 2925441Swnj #if vax 2935441Swnj ti->ti_len = htons((u_short)ti->ti_len); 2945441Swnj #endif 2955441Swnj } 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 3007125Swnj * the retransmit. In persist state, reset persist time for 3017125Swnj * next persist. 3025088Swnj */ 3037125Swnj if (tp->t_force == 0) { 3047125Swnj /* 3057146Swnj * Advance snd_nxt over sequence space of this segment. 3067125Swnj */ 3077125Swnj if (flags & (TH_SYN|TH_FIN)) 3087125Swnj tp->snd_nxt++; 3097125Swnj tp->snd_nxt += len; 3107149Swnj if (SEQ_GT(tp->snd_nxt, tp->snd_max)) 3117149Swnj tp->snd_max = tp->snd_nxt; 3125088Swnj 3137125Swnj /* 3147125Swnj * Time this transmission if not a retransmission and 3157125Swnj * not currently timing anything. 3167125Swnj */ 3177125Swnj if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) { 3187125Swnj tp->t_rtt = 1; 3197125Swnj tp->t_rtseq = tp->snd_nxt - len; 3207125Swnj } 3215088Swnj 3227125Swnj /* 3237125Swnj * Set retransmit timer if not currently set. 3247125Swnj * Initial value for retransmit timer to tcp_beta*tp->t_srtt. 3257125Swnj * Initialize shift counter which is used for exponential 3267125Swnj * backoff 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_rtt = 0; 3337125Swnj tp->t_rxtshift = 0; 3347125Swnj } 3357125Swnj tp->t_timer[TCPT_PERSIST] = 0; 3367149Swnj } else { 3377149Swnj if (SEQ_GT(tp->snd_una+1, tp->snd_max)) 3387149Swnj tp->snd_max = tp->snd_una+1; 3397146Swnj } 3405163Swnj 3415163Swnj /* 3425268Sroot * Trace. 3435268Sroot */ 3447146Swnj if (so->so_options & SO_DEBUG) 3455268Sroot tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 3465268Sroot 3475268Sroot /* 3485075Swnj * Fill in IP length and desired time to live and 3495075Swnj * send to IP level. 3505075Swnj */ 3515441Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; 3525075Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 3537146Swnj if (error = ip_output(m, tp->t_ipopt, (so->so_options & SO_DONTROUTE) ? 3547146Swnj &routetoif : &tp->t_inpcb->inp_route, 0)) 3556505Ssam return (error); 3565075Swnj 3575075Swnj /* 3585075Swnj * Data sent (as far as we can tell). 3595075Swnj * If this advertises a larger window than any other segment, 3605245Sroot * then remember the size of the advertised window. 3615088Swnj * Drop send for purpose of ACK requirements. 3625075Swnj */ 3635252Sroot if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 3645075Swnj tp->rcv_adv = tp->rcv_nxt + win; 3655088Swnj tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 3667125Swnj if (sendalot && tp->t_force == 0) 3677125Swnj goto again; 3686505Ssam return (0); 3694677Swnj } 3707125Swnj 3717125Swnj tcp_setpersist(tp) 3727125Swnj register struct tcpcb *tp; 3737125Swnj { 3747125Swnj 3757125Swnj if (tp->t_timer[TCPT_REXMT]) 3767125Swnj panic("tcp_output REXMT"); 3777125Swnj /* 3787125Swnj * Start/restart persistance timer. 3797125Swnj */ 3807125Swnj TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 3817125Swnj ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift, 3827125Swnj TCPTV_PERSMIN, TCPTV_MAX); 3837125Swnj tp->t_rxtshift++; 3847125Swnj if (tp->t_rxtshift >= TCP_MAXRXTSHIFT) 3857125Swnj tp->t_rxtshift = 0; 3867125Swnj } 387