1*6352Ssam /* tcp_output.c 4.37 82/03/29 */ 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" 10*6352Ssam #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" 235110Swnj #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; 476149Ssam int off, flags, win; 485075Swnj register struct mbuf *m; 495075Swnj register struct tcpiphdr *ti; 505441Swnj u_char *opt; 515441Swnj unsigned optlen = 0; 524678Swnj 535075Swnj COUNT(TCP_OUTPUT); 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 */ 615075Swnj off = tp->snd_nxt - tp->snd_una; 625163Swnj len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off; 635285Sroot if (len < 0) 646149Ssam return (0); /* past FIN */ 655088Swnj if (len > tp->t_maxseg) 665088Swnj len = tp->t_maxseg; 676279Swnj 685088Swnj flags = tcp_outflags[tp->t_state]; 695299Sroot if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc) 705163Swnj flags &= ~TH_FIN; 716279Swnj if (flags & (TH_SYN|TH_RST|TH_FIN)) 725075Swnj goto send; 736279Swnj if (SEQ_GT(tp->snd_up, tp->snd_una)) 746279Swnj goto send; 754678Swnj 765075Swnj /* 776279Swnj * Sender silly window avoidance. If can send all data, 786279Swnj * a maximum segment, at least 1/4 of window do it, 796279Swnj * or are forced, do it; otherwise don't bother. 806279Swnj */ 816279Swnj if (len) { 826279Swnj if (len == tp->t_maxseg || off+len >= so->so_snd.sb_cc) 836279Swnj goto send; 846279Swnj if (len * 4 >= tp->snd_wnd) /* a lot */ 856279Swnj goto send; 866279Swnj if (tp->t_force) 876279Swnj goto send; 886279Swnj } 896279Swnj 906279Swnj /* 915285Sroot * Send if we owe peer an ACK. 925075Swnj */ 935441Swnj if (tp->t_flags&TF_ACKNOW) 945075Swnj goto send; 954678Swnj 965441Swnj #ifdef TCPTRUEOOB 975075Swnj /* 985441Swnj * Send if an out of band data or ack should be transmitted. 995441Swnj */ 1005441Swnj if (tp->t_oobflags&(TCPOOB_OWEACK|TCPOOB_NEEDACK))) 1015441Swnj goto send; 1025441Swnj #endif 1035441Swnj 1045441Swnj /* 1055075Swnj * Calculate available window in i, and also amount 1065075Swnj * of window known to peer (as advertised window less 1075075Swnj * next expected input.) If this is 35% or more of the 1085075Swnj * maximum possible window, then want to send a segment to peer. 1095075Swnj */ 1105088Swnj win = sbspace(&so->so_rcv); 1115088Swnj if (win > 0 && 1125088Swnj ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35)) 1135075Swnj goto send; 1144678Swnj 1155075Swnj /* 1165075Swnj * No reason to send a segment, just return. 1175075Swnj */ 1185110Swnj return (0); 1194678Swnj 1205075Swnj send: 1215075Swnj /* 1225075Swnj * Grab a header mbuf, attaching a copy of data to 1235075Swnj * be transmitted, and initialize the header from 1245075Swnj * the template for sends on this connection. 1255075Swnj */ 1264677Swnj MGET(m, 0); 1274677Swnj if (m == 0) 1284677Swnj return (0); 1295245Sroot m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 1304885Swnj m->m_len = sizeof (struct tcpiphdr); 1315075Swnj if (len) { 1325075Swnj m->m_next = m_copy(so->so_snd.sb_mb, off, len); 1335075Swnj if (m->m_next == 0) 1345075Swnj len = 0; 1355075Swnj } 1365075Swnj ti = mtod(m, struct tcpiphdr *); 1375075Swnj if (tp->t_template == 0) 1385075Swnj panic("tcp_output"); 1395110Swnj bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 1405075Swnj 1415075Swnj /* 1425075Swnj * Fill in fields, remembering maximum advertised 1435075Swnj * window for use in delaying messages about window sizes. 1445075Swnj */ 1455245Sroot ti->ti_seq = tp->snd_nxt; 1465245Sroot ti->ti_ack = tp->rcv_nxt; 1475245Sroot #if vax 1485245Sroot ti->ti_seq = htonl(ti->ti_seq); 1495245Sroot ti->ti_ack = htonl(ti->ti_ack); 1505245Sroot #endif 1515441Swnj /* 1525441Swnj * Before ESTABLISHED, force sending of initial options 1535441Swnj * unless TCP set to not do any options. 1545441Swnj */ 1555441Swnj if (tp->t_state < TCPS_ESTABLISHED) { 1565441Swnj if (tp->t_flags&TF_NOOPT) 1575441Swnj goto noopt; 1585441Swnj opt = tcp_initopt; 1595441Swnj optlen = sizeof (tcp_initopt); 1605441Swnj *(u_short *)(opt + 2) = so->so_rcv.sb_hiwat / 2; 1615441Swnj #if vax 1625441Swnj *(u_short *)(opt + 2) = htons(*(u_short *)(opt + 2)); 1635441Swnj #endif 1645441Swnj } else { 1655441Swnj if (tp->t_tcpopt == 0) 1665441Swnj goto noopt; 1675441Swnj opt = mtod(tp->t_tcpopt, u_char *); 1685441Swnj optlen = tp->t_tcpopt->m_len; 1695441Swnj } 1705441Swnj #ifndef TCPTRUEOOB 1715441Swnj if (opt) 1725441Swnj #else 1735441Swnj if (opt || (tp->t_oobflags&(TCPOOB_OWEACK|TCPOOB_NEEDACK))) 1745441Swnj #endif 1755441Swnj { 1765110Swnj m0 = m->m_next; 1775584Sroot m->m_next = m_get(M_DONTWAIT); 1785088Swnj if (m->m_next == 0) { 1795088Swnj (void) m_free(m); 1805441Swnj m_freem(m0); 1815088Swnj return (0); 1825088Swnj } 1835088Swnj m->m_next->m_next = m0; 1845441Swnj m0 = m->m_next; 1855441Swnj m0->m_off = MMINOFF; 1865441Swnj m0->m_len = optlen; 1876162Ssam bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); 1885441Swnj opt = (u_char *)(mtod(m0, caddr_t) + optlen); 1895441Swnj #ifdef TCPTRUEOOB 1905441Swnj if (tp->t_oobflags&TCPOOB_OWEACK) { 1915441Swnj printf("tp %x send OOBACK for %x\n", tp->t_iobseq); 1925441Swnj *opt++ = TCPOPT_OOBACK; 1935441Swnj *opt++ = 3; 1945441Swnj *opt++ = tp->t_iobseq; 1955441Swnj m0->m_len += 3; 1965441Swnj tp->t_oobflags &= ~TCPOOB_OWEACK; 1975441Swnj /* sender should rexmt oob to force ack repeat */ 1985441Swnj } 1995441Swnj if (tp->t_oobflags&TCPOOB_NEEDACK) { 2005441Swnj printf("tp %x send OOBDATA seq %x data %x\n", tp->t_oobseq, tp->t_oobc); 2015441Swnj *opt++ = TCPOPT_OOBDATA; 2025548Swnj *opt++ = 8; 2035441Swnj *opt++ = tp->t_oobseq; 2045441Swnj *opt++ = tp->t_oobc; 2055548Swnj *(tcp_seq *)opt = tp->t_oobmark - tp->snd_nxt; 2065548Swnj #ifdef vax 2075548Swnj *(tcp_seq *)opt = htonl((unsigned)*(tcp_seq *)opt); 2085548Swnj #endif 2095548Swnj m0->m_len += 8; 2105441Swnj TCPT_RANGESET(tp->t_timer[TCPT_OOBREXMT], 2115441Swnj tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 2125441Swnj } 2135441Swnj #endif 2145441Swnj while (m0->m_len & 0x3) { 2155441Swnj *opt++ = TCPOPT_EOL; 2165441Swnj m0->m_len++; 2175441Swnj } 2185441Swnj optlen = m0->m_len; 2195441Swnj ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 2205088Swnj } 2215441Swnj noopt: 2225088Swnj ti->ti_flags = flags; 2235075Swnj win = sbspace(&so->so_rcv); 2246279Swnj if (win < so->so_rcv.sb_hiwat / 4) /* avoid silly window */ 2256279Swnj win = 0; 2265075Swnj if (win > 0) 2276279Swnj #if vax 2285110Swnj ti->ti_win = htons((u_short)win); 2296279Swnj #else 2306279Swnj ti->ti_win = win; 2316279Swnj #endif 2325088Swnj if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 2335420Swnj ti->ti_urp = tp->snd_up - tp->snd_nxt; 2345420Swnj #if vax 2355420Swnj ti->ti_urp = htons(ti->ti_urp); 2365420Swnj #endif 2375075Swnj ti->ti_flags |= TH_URG; 2385075Swnj } else 2395075Swnj /* 2405075Swnj * If no urgent pointer to send, then we pull 2415075Swnj * the urgent pointer to the left edge of the send window 2425075Swnj * so that it doesn't drift into the send window on sequence 2435075Swnj * number wraparound. 2445075Swnj */ 2455088Swnj tp->snd_up = tp->snd_una; /* drag it along */ 2465088Swnj /* PUSH */ 2475075Swnj 2485075Swnj /* 2495075Swnj * Put TCP length in extended header, and then 2505075Swnj * checksum extended header and data. 2515075Swnj */ 2525441Swnj if (len + optlen) { 2535441Swnj ti->ti_len = sizeof (struct tcphdr) + optlen + len; 2545441Swnj #if vax 2555441Swnj ti->ti_len = htons((u_short)ti->ti_len); 2565441Swnj #endif 2575441Swnj } 2586162Ssam ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len); 2595075Swnj 2605075Swnj /* 2615088Swnj * Advance snd_nxt over sequence space of this segment 2625088Swnj */ 2635088Swnj if (flags & (TH_SYN|TH_FIN)) 2645245Sroot tp->snd_nxt++; 2655088Swnj tp->snd_nxt += len; 2665088Swnj 2675088Swnj /* 2685163Swnj * If this transmission closes the window, 2695245Sroot * start persistance timer at 2 round trip times 2705245Sroot * but at least TCPTV_PERSMIN ticks. 2715088Swnj */ 2725276Swnj if (TCPS_HAVERCVDSYN(tp->t_state) && 2735299Sroot SEQ_GEQ(tp->snd_nxt, tp->snd_una+tp->snd_wnd) && 2745245Sroot tp->t_timer[TCPT_PERSIST] == 0) 2755245Sroot TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 2765245Sroot 2 * tp->t_srtt, TCPTV_PERSMIN, TCPTV_MAX); 2775088Swnj 2785088Swnj /* 2795163Swnj * Time this transmission if not a retransmission and 2805163Swnj * not currently timing anything. 2815163Swnj */ 2825163Swnj if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) { 2835163Swnj tp->t_rtt = 1; 2845163Swnj tp->t_rtseq = tp->snd_nxt - len; 2855163Swnj } 2865163Swnj 2875163Swnj /* 2885163Swnj * Set retransmit timer if not currently set. 2895245Sroot * Initial value for retransmit timer to tcp_beta*tp->t_srtt. 2905163Swnj * Initialize shift counter which is used for exponential 2915163Swnj * backoff of retransmit time. 2925163Swnj */ 2935245Sroot if (tp->t_timer[TCPT_REXMT] == 0 && tp->snd_nxt != tp->snd_una) { 2945245Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 2955245Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 2965299Sroot tp->t_rtt = 0; 2975163Swnj tp->t_rxtshift = 0; 2985163Swnj } 2995163Swnj 3005163Swnj /* 3015268Sroot * Trace. 3025268Sroot */ 3035268Sroot if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 3045268Sroot tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 3055268Sroot 3065268Sroot /* 3075075Swnj * Fill in IP length and desired time to live and 3085075Swnj * send to IP level. 3095075Swnj */ 3105441Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; 3115075Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 312*6352Ssam if (ip_output(m, tp->t_ipopt, &tp->t_inpcb->inp_route, 0) == 0) 3135088Swnj return (0); 3145075Swnj 3155075Swnj /* 3165075Swnj * Data sent (as far as we can tell). 3175075Swnj * If this advertises a larger window than any other segment, 3185245Sroot * then remember the size of the advertised window. 3195088Swnj * Drop send for purpose of ACK requirements. 3205075Swnj */ 3215252Sroot if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 3225075Swnj tp->rcv_adv = tp->rcv_nxt + win; 3235088Swnj tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 3245245Sroot if (SEQ_GT(tp->snd_nxt, tp->snd_max)) 3255245Sroot tp->snd_max = tp->snd_nxt; 3265088Swnj return (1); 3274677Swnj } 328