1*5245Sroot /* tcp_output.c 4.22 81/12/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" 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" 215110Swnj #include "../errno.h" 224677Swnj 23*5245Sroot char *tcpstates[]; /* XXX */ 244678Swnj /* 25*5245Sroot * Tcp output routine: figure out what should be sent and send it. 264678Swnj */ 275075Swnj tcp_output(tp) 285075Swnj register struct tcpcb *tp; 294678Swnj { 305075Swnj register struct socket *so = tp->t_inpcb->inp_socket; 315075Swnj register int len; 325075Swnj struct mbuf *m0; 335075Swnj int off, flags; 345075Swnj register struct mbuf *m; 355075Swnj register struct tcpiphdr *ti; 365075Swnj int win; 374678Swnj 385075Swnj COUNT(TCP_OUTPUT); 394678Swnj 405075Swnj /* 415088Swnj * Determine length of data that can be transmitted, 425088Swnj * and flags that will be used. 435088Swnj * If there is some data or critical controls (SYN, RST) 445088Swnj * to send, then transmit; otherwise, investigate further. 455075Swnj */ 465075Swnj off = tp->snd_nxt - tp->snd_una; 475163Swnj len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off; 485088Swnj if (len > tp->t_maxseg) 495088Swnj len = tp->t_maxseg; 50*5245Sroot if (len < 0) 51*5245Sroot len = 0; /* FIN can cause -1 */ 525088Swnj flags = tcp_outflags[tp->t_state]; 535163Swnj if (len < so->so_snd.sb_cc) 545163Swnj flags &= ~TH_FIN; 555088Swnj if (len || (flags & (TH_SYN|TH_RST))) 565075Swnj goto send; 574678Swnj 585075Swnj /* 595088Swnj * See if we owe peer an ACK or have a unacked FIN to send. 605075Swnj */ 615088Swnj if (tp->t_flags & TF_ACKNOW) 625075Swnj goto send; 635088Swnj if ((so->so_state & SS_CANTSENDMORE) && 645088Swnj TCPS_OURFINNOTACKED(tp->t_state)) 655075Swnj goto send; 664678Swnj 675075Swnj /* 685075Swnj * Calculate available window in i, and also amount 695075Swnj * of window known to peer (as advertised window less 705075Swnj * next expected input.) If this is 35% or more of the 715075Swnj * maximum possible window, then want to send a segment to peer. 725075Swnj */ 735088Swnj win = sbspace(&so->so_rcv); 745088Swnj if (win > 0 && 755088Swnj ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35)) 765075Swnj goto send; 774678Swnj 785075Swnj /* 795075Swnj * No reason to send a segment, just return. 805075Swnj */ 81*5245Sroot printf("tcp_output: nothing to send\n"); 825110Swnj return (0); 834678Swnj 845075Swnj send: 855075Swnj /* 865075Swnj * Grab a header mbuf, attaching a copy of data to 875075Swnj * be transmitted, and initialize the header from 885075Swnj * the template for sends on this connection. 895075Swnj */ 904677Swnj MGET(m, 0); 914677Swnj if (m == 0) 924677Swnj return (0); 93*5245Sroot m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 944885Swnj m->m_len = sizeof (struct tcpiphdr); 955075Swnj if (len) { 965075Swnj m->m_next = m_copy(so->so_snd.sb_mb, off, len); 975075Swnj if (m->m_next == 0) 985075Swnj len = 0; 99*5245Sroot if (m->m_next) printf("copy *mtod()=%x\n", *mtod(m->m_next, char *)); 1005075Swnj } 1015075Swnj ti = mtod(m, struct tcpiphdr *); 1025075Swnj if (tp->t_template == 0) 1035075Swnj panic("tcp_output"); 1045110Swnj bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 1055075Swnj 1065075Swnj /* 1075075Swnj * Fill in fields, remembering maximum advertised 1085075Swnj * window for use in delaying messages about window sizes. 1095075Swnj */ 110*5245Sroot ti->ti_seq = tp->snd_nxt; 111*5245Sroot ti->ti_ack = tp->rcv_nxt; 112*5245Sroot #if vax 113*5245Sroot ti->ti_seq = htonl(ti->ti_seq); 114*5245Sroot ti->ti_ack = htonl(ti->ti_ack); 115*5245Sroot #endif 1165088Swnj if (tp->t_tcpopt) { 1175110Swnj m0 = m->m_next; 1185088Swnj m->m_next = m_get(0); 1195088Swnj if (m->m_next == 0) { 1205088Swnj (void) m_free(m); 1215110Swnj m_freem(m); 1225088Swnj return (0); 1235088Swnj } 1245088Swnj m->m_next->m_next = m0; 1255088Swnj m->m_off = MMINOFF; 1265088Swnj m->m_len = tp->t_tcpopt->m_len; 1275088Swnj bcopy(mtod(tp->t_tcpopt, caddr_t), mtod(m, caddr_t), 1285110Swnj (unsigned)tp->t_tcpopt->m_len); 1295088Swnj ti->ti_off = (sizeof (struct tcphdr)+tp->t_tcpopt->m_len) >> 2; 1305088Swnj } 1315088Swnj ti->ti_flags = flags; 1325075Swnj win = sbspace(&so->so_rcv); 1335075Swnj if (win > 0) 1345110Swnj ti->ti_win = htons((u_short)win); 1355088Swnj if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 1365088Swnj ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 1375075Swnj ti->ti_flags |= TH_URG; 1385075Swnj } else 1395075Swnj /* 1405075Swnj * If no urgent pointer to send, then we pull 1415075Swnj * the urgent pointer to the left edge of the send window 1425075Swnj * so that it doesn't drift into the send window on sequence 1435075Swnj * number wraparound. 1445075Swnj */ 1455088Swnj tp->snd_up = tp->snd_una; /* drag it along */ 1465088Swnj /* PUSH */ 1475075Swnj 1485075Swnj /* 1495075Swnj * Put TCP length in extended header, and then 1505075Swnj * checksum extended header and data. 1515075Swnj */ 1525075Swnj if (len) 1535075Swnj ti->ti_len = htons((u_short)(len + sizeof (struct tcphdr))); 1545088Swnj ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + len); 1555075Swnj 156*5245Sroot printf("tcp_output: ti %x flags %x seq %x ack %x win %d len %d sum %x\n", 157*5245Sroot ti, ti->ti_flags, htonl(ti->ti_seq), htonl(ti->ti_ack), htons(ti->ti_win), ti->ti_len, ti->ti_sum); 158*5245Sroot 1595075Swnj /* 1605088Swnj * Advance snd_nxt over sequence space of this segment 1615088Swnj */ 1625088Swnj if (flags & (TH_SYN|TH_FIN)) 163*5245Sroot tp->snd_nxt++; 1645088Swnj tp->snd_nxt += len; 1655088Swnj 1665088Swnj /* 1675163Swnj * If this transmission closes the window, 168*5245Sroot * start persistance timer at 2 round trip times 169*5245Sroot * but at least TCPTV_PERSMIN ticks. 1705088Swnj */ 171*5245Sroot if (SEQ_GT(tp->snd_nxt, tp->snd_una+tp->snd_wnd) && 172*5245Sroot tp->t_timer[TCPT_PERSIST] == 0) 173*5245Sroot TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 174*5245Sroot 2 * tp->t_srtt, TCPTV_PERSMIN, TCPTV_MAX); 1755088Swnj 1765088Swnj /* 1775163Swnj * Time this transmission if not a retransmission and 1785163Swnj * not currently timing anything. 1795163Swnj */ 1805163Swnj if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) { 1815163Swnj tp->t_rtt = 1; 1825163Swnj tp->t_rtseq = tp->snd_nxt - len; 1835163Swnj } 1845163Swnj 1855163Swnj /* 1865163Swnj * Set retransmit timer if not currently set. 187*5245Sroot * Initial value for retransmit timer to tcp_beta*tp->t_srtt. 1885163Swnj * Initialize shift counter which is used for exponential 1895163Swnj * backoff of retransmit time. 1905163Swnj */ 191*5245Sroot if (tp->t_timer[TCPT_REXMT] == 0 && tp->snd_nxt != tp->snd_una) { 192*5245Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 193*5245Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 194*5245Sroot printf("rexmt timer set to %d\n", tp->t_timer[TCPT_REXMT]); 1955163Swnj tp->t_rxtshift = 0; 1965163Swnj } 197*5245Sroot else printf("REXMT timer is already %d, snd_nxt %x snd_una %x\n", tp->t_timer[TCPT_REXMT], tp->snd_nxt, tp->snd_una); 1985163Swnj 1995163Swnj /* 2005075Swnj * Fill in IP length and desired time to live and 2015075Swnj * send to IP level. 2025075Swnj */ 2035075Swnj ((struct ip *)ti)->ip_len = len + sizeof (struct tcpiphdr); 2045075Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 205*5245Sroot if (ip_output(m, tp->t_ipopt) == 0) { 206*5245Sroot printf("ip_output failed\n"); 2075088Swnj return (0); 208*5245Sroot } 2095075Swnj 2105075Swnj /* 2115075Swnj * Data sent (as far as we can tell). 2125075Swnj * If this advertises a larger window than any other segment, 213*5245Sroot * then remember the size of the advertised window. 2145088Swnj * Drop send for purpose of ACK requirements. 2155075Swnj */ 216*5245Sroot if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) { 2175075Swnj tp->rcv_adv = tp->rcv_nxt + win; 218*5245Sroot } 2195088Swnj tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 220*5245Sroot if (SEQ_GT(tp->snd_nxt, tp->snd_max)) 221*5245Sroot tp->snd_max = tp->snd_nxt; 2225088Swnj return (1); 2234677Swnj } 224