1*5299Sroot /* tcp_output.c 4.27 81/12/22 */ 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" 105088Swnj #include "../net/in_pcb.h" 115088Swnj #include "../net/in_systm.h" 124804Swnj #include "../net/ip.h" 134900Swnj #include "../net/ip_var.h" 144804Swnj #include "../net/tcp.h" 155088Swnj #define TCPOUTFLAGS 165088Swnj #include "../net/tcp_fsm.h" 175088Swnj #include "../net/tcp_seq.h" 185088Swnj #include "../net/tcp_timer.h" 194804Swnj #include "../net/tcp_var.h" 205088Swnj #include "../net/tcpip.h" 215268Sroot #include "../net/tcp_debug.h" 225110Swnj #include "../errno.h" 234677Swnj 245245Sroot char *tcpstates[]; /* XXX */ 254678Swnj /* 265245Sroot * Tcp output routine: figure out what should be sent and send it. 274678Swnj */ 285075Swnj tcp_output(tp) 295075Swnj register struct tcpcb *tp; 304678Swnj { 315075Swnj register struct socket *so = tp->t_inpcb->inp_socket; 325075Swnj register int len; 335075Swnj struct mbuf *m0; 345075Swnj int off, flags; 355075Swnj register struct mbuf *m; 365075Swnj register struct tcpiphdr *ti; 375075Swnj int win; 384678Swnj 395075Swnj COUNT(TCP_OUTPUT); 404678Swnj 415075Swnj /* 425088Swnj * Determine length of data that can be transmitted, 435088Swnj * and flags that will be used. 445088Swnj * If there is some data or critical controls (SYN, RST) 455088Swnj * to send, then transmit; otherwise, investigate further. 465075Swnj */ 475075Swnj off = tp->snd_nxt - tp->snd_una; 485163Swnj len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off; 495285Sroot if (len < 0) 505285Sroot return; /* past FIN */ 515088Swnj if (len > tp->t_maxseg) 525088Swnj len = tp->t_maxseg; 535088Swnj flags = tcp_outflags[tp->t_state]; 54*5299Sroot if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc) 555163Swnj flags &= ~TH_FIN; 565285Sroot if (len || (flags & (TH_SYN|TH_RST|TH_FIN))) 575075Swnj goto send; 584678Swnj 595075Swnj /* 605285Sroot * Send if we owe peer an ACK. 615075Swnj */ 625088Swnj if (tp->t_flags & TF_ACKNOW) 635075Swnj goto send; 644678Swnj 655075Swnj /* 665075Swnj * Calculate available window in i, and also amount 675075Swnj * of window known to peer (as advertised window less 685075Swnj * next expected input.) If this is 35% or more of the 695075Swnj * maximum possible window, then want to send a segment to peer. 705075Swnj */ 715088Swnj win = sbspace(&so->so_rcv); 725088Swnj if (win > 0 && 735088Swnj ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35)) 745075Swnj goto send; 754678Swnj 765075Swnj /* 775075Swnj * No reason to send a segment, just return. 785075Swnj */ 795110Swnj return (0); 804678Swnj 815075Swnj send: 825075Swnj /* 835075Swnj * Grab a header mbuf, attaching a copy of data to 845075Swnj * be transmitted, and initialize the header from 855075Swnj * the template for sends on this connection. 865075Swnj */ 874677Swnj MGET(m, 0); 884677Swnj if (m == 0) 894677Swnj return (0); 905245Sroot m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 914885Swnj m->m_len = sizeof (struct tcpiphdr); 925075Swnj if (len) { 935075Swnj m->m_next = m_copy(so->so_snd.sb_mb, off, len); 945075Swnj if (m->m_next == 0) 955075Swnj len = 0; 965075Swnj } 975075Swnj ti = mtod(m, struct tcpiphdr *); 985075Swnj if (tp->t_template == 0) 995075Swnj panic("tcp_output"); 1005110Swnj bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 1015075Swnj 1025075Swnj /* 1035075Swnj * Fill in fields, remembering maximum advertised 1045075Swnj * window for use in delaying messages about window sizes. 1055075Swnj */ 1065245Sroot ti->ti_seq = tp->snd_nxt; 1075245Sroot ti->ti_ack = tp->rcv_nxt; 1085245Sroot #if vax 1095245Sroot ti->ti_seq = htonl(ti->ti_seq); 1105245Sroot ti->ti_ack = htonl(ti->ti_ack); 1115245Sroot #endif 1125088Swnj if (tp->t_tcpopt) { 1135110Swnj m0 = m->m_next; 1145088Swnj m->m_next = m_get(0); 1155088Swnj if (m->m_next == 0) { 1165088Swnj (void) m_free(m); 1175110Swnj m_freem(m); 1185088Swnj return (0); 1195088Swnj } 1205088Swnj m->m_next->m_next = m0; 1215088Swnj m->m_off = MMINOFF; 1225088Swnj m->m_len = tp->t_tcpopt->m_len; 1235088Swnj bcopy(mtod(tp->t_tcpopt, caddr_t), mtod(m, caddr_t), 1245110Swnj (unsigned)tp->t_tcpopt->m_len); 1255088Swnj ti->ti_off = (sizeof (struct tcphdr)+tp->t_tcpopt->m_len) >> 2; 1265088Swnj } 1275088Swnj ti->ti_flags = flags; 1285075Swnj win = sbspace(&so->so_rcv); 1295075Swnj if (win > 0) 1305110Swnj ti->ti_win = htons((u_short)win); 1315088Swnj if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 1325088Swnj ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 1335075Swnj ti->ti_flags |= TH_URG; 1345075Swnj } else 1355075Swnj /* 1365075Swnj * If no urgent pointer to send, then we pull 1375075Swnj * the urgent pointer to the left edge of the send window 1385075Swnj * so that it doesn't drift into the send window on sequence 1395075Swnj * number wraparound. 1405075Swnj */ 1415088Swnj tp->snd_up = tp->snd_una; /* drag it along */ 1425088Swnj /* PUSH */ 1435075Swnj 1445075Swnj /* 1455075Swnj * Put TCP length in extended header, and then 1465075Swnj * checksum extended header and data. 1475075Swnj */ 1485075Swnj if (len) 1495075Swnj ti->ti_len = htons((u_short)(len + sizeof (struct tcphdr))); 1505088Swnj ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + len); 1515075Swnj 1525075Swnj /* 1535088Swnj * Advance snd_nxt over sequence space of this segment 1545088Swnj */ 1555088Swnj if (flags & (TH_SYN|TH_FIN)) 1565245Sroot tp->snd_nxt++; 1575088Swnj tp->snd_nxt += len; 1585088Swnj 1595088Swnj /* 1605163Swnj * If this transmission closes the window, 1615245Sroot * start persistance timer at 2 round trip times 1625245Sroot * but at least TCPTV_PERSMIN ticks. 1635088Swnj */ 1645276Swnj if (TCPS_HAVERCVDSYN(tp->t_state) && 165*5299Sroot SEQ_GEQ(tp->snd_nxt, tp->snd_una+tp->snd_wnd) && 1665245Sroot tp->t_timer[TCPT_PERSIST] == 0) 1675245Sroot TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 1685245Sroot 2 * tp->t_srtt, TCPTV_PERSMIN, TCPTV_MAX); 1695088Swnj 1705088Swnj /* 1715163Swnj * Time this transmission if not a retransmission and 1725163Swnj * not currently timing anything. 1735163Swnj */ 1745163Swnj if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) { 1755163Swnj tp->t_rtt = 1; 1765163Swnj tp->t_rtseq = tp->snd_nxt - len; 1775163Swnj } 1785163Swnj 1795163Swnj /* 1805163Swnj * Set retransmit timer if not currently set. 1815245Sroot * Initial value for retransmit timer to tcp_beta*tp->t_srtt. 1825163Swnj * Initialize shift counter which is used for exponential 1835163Swnj * backoff of retransmit time. 1845163Swnj */ 1855245Sroot if (tp->t_timer[TCPT_REXMT] == 0 && tp->snd_nxt != tp->snd_una) { 1865245Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 1875245Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 188*5299Sroot tp->t_rtt = 0; 1895163Swnj tp->t_rxtshift = 0; 1905163Swnj } 1915163Swnj 1925163Swnj /* 1935268Sroot * Trace. 1945268Sroot */ 1955268Sroot if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 1965268Sroot tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 1975268Sroot 1985268Sroot /* 1995075Swnj * Fill in IP length and desired time to live and 2005075Swnj * send to IP level. 2015075Swnj */ 2025075Swnj ((struct ip *)ti)->ip_len = len + sizeof (struct tcpiphdr); 2035075Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 2045285Sroot if (ip_output(m, tp->t_ipopt) == 0) { 2055285Sroot printf("ip_output failed\n"); 2065088Swnj return (0); 2075285Sroot } 2085075Swnj 2095075Swnj /* 2105075Swnj * Data sent (as far as we can tell). 2115075Swnj * If this advertises a larger window than any other segment, 2125245Sroot * then remember the size of the advertised window. 2135088Swnj * Drop send for purpose of ACK requirements. 2145075Swnj */ 2155252Sroot if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 2165075Swnj tp->rcv_adv = tp->rcv_nxt + win; 2175088Swnj tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 2185245Sroot if (SEQ_GT(tp->snd_nxt, tp->snd_max)) 2195245Sroot tp->snd_max = tp->snd_nxt; 2205088Swnj return (1); 2214677Swnj } 222