123191Smckusick /* 229150Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 332788Sbostic * All rights reserved. 423191Smckusick * 532788Sbostic * Redistribution and use in source and binary forms are permitted 632788Sbostic * provided that this notice is preserved and that due credit is given 732788Sbostic * to the University of California at Berkeley. The name of the University 832788Sbostic * may not be used to endorse or promote products derived from this 932788Sbostic * software without specific prior written permission. This software 1032788Sbostic * is provided ``as is'' without express or implied warranty. 1132788Sbostic * 12*33783Skarels * @(#)tcp_output.c 7.15 (Berkeley) 03/24/88 1323191Smckusick */ 144677Swnj 1517063Sbloom #include "param.h" 1617063Sbloom #include "systm.h" 1717063Sbloom #include "mbuf.h" 1817063Sbloom #include "protosw.h" 1917063Sbloom #include "socket.h" 2017063Sbloom #include "socketvar.h" 2117063Sbloom #include "errno.h" 2210895Ssam 2310895Ssam #include "../net/route.h" 2410895Ssam 2517063Sbloom #include "in.h" 2617063Sbloom #include "in_pcb.h" 2717063Sbloom #include "in_systm.h" 2817063Sbloom #include "ip.h" 2917063Sbloom #include "ip_var.h" 3017063Sbloom #include "tcp.h" 315088Swnj #define TCPOUTFLAGS 3217063Sbloom #include "tcp_fsm.h" 3317063Sbloom #include "tcp_seq.h" 3417063Sbloom #include "tcp_timer.h" 3517063Sbloom #include "tcp_var.h" 3617063Sbloom #include "tcpip.h" 3717063Sbloom #include "tcp_debug.h" 384677Swnj 394678Swnj /* 408314Sroot * Initial options. 415441Swnj */ 425441Swnj u_char tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, }; 435441Swnj 445441Swnj /* 455245Sroot * Tcp output routine: figure out what should be sent and send it. 464678Swnj */ 475075Swnj tcp_output(tp) 485075Swnj register struct tcpcb *tp; 494678Swnj { 505075Swnj register struct socket *so = tp->t_inpcb->inp_socket; 5133620Skarels register long len, win; 525075Swnj struct mbuf *m0; 5325940Skarels int off, flags, error; 545075Swnj register struct mbuf *m; 555075Swnj register struct tcpiphdr *ti; 565441Swnj u_char *opt; 575441Swnj unsigned optlen = 0; 5825940Skarels int idle, sendalot; 594678Swnj 605075Swnj /* 616279Swnj * Determine length of data that should be transmitted, 625088Swnj * and flags that will be used. 635088Swnj * If there is some data or critical controls (SYN, RST) 645088Swnj * to send, then transmit; otherwise, investigate further. 655075Swnj */ 6625940Skarels idle = (tp->snd_max == tp->snd_una); 677125Swnj again: 687125Swnj sendalot = 0; 695075Swnj off = tp->snd_nxt - tp->snd_una; 7021116Skarels win = MIN(tp->snd_wnd, tp->snd_cwnd); 7126834Skarels 7221116Skarels /* 7321116Skarels * If in persist timeout with window of 0, send 1 byte. 7425940Skarels * Otherwise, if window is small but nonzero 7525940Skarels * and timer expired, we will send what we can 7625940Skarels * and go to transmit state. 7721116Skarels */ 7821116Skarels if (tp->t_force) { 7926834Skarels if (win == 0) 8021116Skarels win = 1; 8121116Skarels else { 8221116Skarels tp->t_timer[TCPT_PERSIST] = 0; 8321116Skarels tp->t_rxtshift = 0; 8421116Skarels } 8521116Skarels } 8625940Skarels 8717361Skarels len = MIN(so->so_snd.sb_cc, win) - off; 885088Swnj flags = tcp_outflags[tp->t_state]; 8925940Skarels 9026834Skarels if (len < 0) { 9126834Skarels /* 9227048Skarels * If FIN has been sent but not acked, 9327048Skarels * but we haven't been called to retransmit, 9432785Skarels * len will be -1. Otherwise, window shrank 9532785Skarels * after we sent into it. If window shrank to 0, 9632785Skarels * cancel pending retransmit and pull snd_nxt 9732785Skarels * back to (closed) window. We will enter persist 9832785Skarels * state below. If the window didn't close completely, 9927048Skarels * just wait for an ACK. 10026834Skarels */ 10132785Skarels len = 0; 10232785Skarels if (win == 0) { 10327067Skarels tp->t_timer[TCPT_REXMT] = 0; 10427067Skarels tp->snd_nxt = tp->snd_una; 10532785Skarels } 10626834Skarels } 10727067Skarels if (len > tp->t_maxseg) { 10827067Skarels len = tp->t_maxseg; 10931442Skarels sendalot = 1; 11027067Skarels } 11129795Skarels if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 11229795Skarels flags &= ~TH_FIN; 11326834Skarels win = sbspace(&so->so_rcv); 11426834Skarels 11527067Skarels 11625940Skarels /* 11727067Skarels * If our state indicates that FIN should be sent 11827067Skarels * and we have not yet done so, or we're retransmitting the FIN, 11927067Skarels * then we need to send. 12027067Skarels */ 12127067Skarels if (flags & TH_FIN && 12227067Skarels ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 12327067Skarels goto send; 12427067Skarels /* 12525940Skarels * Send if we owe peer an ACK. 12625940Skarels */ 12726834Skarels if (tp->t_flags & TF_ACKNOW) 12825940Skarels goto send; 12927067Skarels if (flags & (TH_SYN|TH_RST)) 13026834Skarels goto send; 1316279Swnj if (SEQ_GT(tp->snd_up, tp->snd_una)) 1326279Swnj goto send; 1334678Swnj 1345075Swnj /* 13517318Skarels * Sender silly window avoidance. If connection is idle 13617318Skarels * and can send all data, a maximum segment, 13717318Skarels * at least a maximum default-size segment do it, 1386279Swnj * or are forced, do it; otherwise don't bother. 13925261Skarels * If peer's buffer is tiny, then send 14025261Skarels * when window is at least half open. 14121116Skarels * If retransmitting (possibly after persist timer forced us 14221116Skarels * to send into a small window), then must resend. 1436279Swnj */ 1446279Swnj if (len) { 14531725Skarels if (len == tp->t_maxseg) 1466279Swnj goto send; 14725940Skarels if ((idle || tp->t_flags & TF_NODELAY) && 14825940Skarels len + off >= so->so_snd.sb_cc) 1496279Swnj goto send; 1506279Swnj if (tp->t_force) 1516279Swnj goto send; 15225261Skarels if (len >= tp->max_sndwnd / 2) 15325261Skarels goto send; 15421116Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 15521116Skarels goto send; 15626834Skarels } 1576279Swnj 1585441Swnj /* 15925940Skarels * Compare available window to amount of window 16025940Skarels * known to peer (as advertised window less 16132785Skarels * next expected input). If the difference is at least two 16232785Skarels * max size segments or at least 35% of the maximum possible 16332785Skarels * window, then want to send a window update to peer. 1645075Swnj */ 16532034Skarels if (win > 0) { 16632034Skarels int adv = win - (tp->rcv_adv - tp->rcv_nxt); 1674678Swnj 16832785Skarels if (so->so_rcv.sb_cc == 0 && adv >= 2 * tp->t_maxseg) 16932785Skarels goto send; 17032034Skarels if (100 * adv / so->so_rcv.sb_hiwat >= 35) 17132034Skarels goto send; 17232034Skarels } 17332034Skarels 1745075Swnj /* 1757125Swnj * TCP window updates are not reliable, rather a polling protocol 1767125Swnj * using ``persist'' packets is used to insure receipt of window 1777125Swnj * updates. The three ``states'' for the output side are: 1787125Swnj * idle not doing retransmits or persists 17925940Skarels * persisting to move a small or zero window 1807125Swnj * (re)transmitting and thereby not persisting 1817125Swnj * 1827125Swnj * tp->t_timer[TCPT_PERSIST] 1837125Swnj * is set when we are in persist state. 1847125Swnj * tp->t_force 1857125Swnj * is set when we are called to send a persist packet. 1867125Swnj * tp->t_timer[TCPT_REXMT] 1877125Swnj * is set when we are retransmitting 1887125Swnj * The output side is idle when both timers are zero. 1897125Swnj * 19021116Skarels * If send window is too small, there is data to transmit, and no 19121116Skarels * retransmit or persist is pending, then go to persist state. 19221116Skarels * If nothing happens soon, send when timer expires: 19321116Skarels * if window is nonzero, transmit what we can, 19421116Skarels * otherwise force out a byte. 1957125Swnj */ 19621116Skarels if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 && 19721116Skarels tp->t_timer[TCPT_PERSIST] == 0) { 1987125Swnj tp->t_rxtshift = 0; 1997125Swnj tcp_setpersist(tp); 2007125Swnj } 2017125Swnj 2027125Swnj /* 2035075Swnj * No reason to send a segment, just return. 2045075Swnj */ 2055110Swnj return (0); 2064678Swnj 2075075Swnj send: 2085075Swnj /* 2095075Swnj * Grab a header mbuf, attaching a copy of data to 2105075Swnj * be transmitted, and initialize the header from 2115075Swnj * the template for sends on this connection. 2125075Swnj */ 21311720Ssam MGET(m, M_DONTWAIT, MT_HEADER); 21411720Ssam if (m == NULL) 2156505Ssam return (ENOBUFS); 21633620Skarels #define MAXLINKHDR 32 /* belongs elsewhere */ 21733620Skarels #define DATASPACE (MMAXOFF - (MMINOFF + MAXLINKHDR + sizeof (struct tcpiphdr))) 21833620Skarels m->m_off = MMINOFF + MAXLINKHDR; 2194885Swnj m->m_len = sizeof (struct tcpiphdr); 22033620Skarels ti = mtod(m, struct tcpiphdr *); 2215075Swnj if (len) { 22231442Skarels if (tp->t_force && len == 1) 22331442Skarels tcpstat.tcps_sndprobe++; 22431442Skarels else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 22531442Skarels tcpstat.tcps_sndrexmitpack++; 22631442Skarels tcpstat.tcps_sndrexmitbyte += len; 22731442Skarels } else { 22831442Skarels tcpstat.tcps_sndpack++; 22931442Skarels tcpstat.tcps_sndbyte += len; 23031442Skarels } 23133620Skarels if (len <= DATASPACE) { 23233620Skarels m_copydata(so->so_snd.sb_mb, off, len, 23333620Skarels mtod(m, caddr_t) + sizeof(struct tcpiphdr)); 23433620Skarels m->m_len += len; 23533620Skarels } else { 23633620Skarels m->m_next = m_copy(so->so_snd.sb_mb, off, len); 23733620Skarels if (m->m_next == 0) 23833620Skarels len = 0; 23933620Skarels } 24031442Skarels } else if (tp->t_flags & TF_ACKNOW) 24131442Skarels tcpstat.tcps_sndacks++; 24231442Skarels else if (flags & (TH_SYN|TH_FIN|TH_RST)) 24331442Skarels tcpstat.tcps_sndctrl++; 24431442Skarels else if (SEQ_GT(tp->snd_up, tp->snd_una)) 24531442Skarels tcpstat.tcps_sndurg++; 24631442Skarels else 24731442Skarels tcpstat.tcps_sndwinup++; 24831442Skarels 2495075Swnj if (tp->t_template == 0) 2505075Swnj panic("tcp_output"); 2515110Swnj bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 2525075Swnj 2535075Swnj /* 2545075Swnj * Fill in fields, remembering maximum advertised 2555075Swnj * window for use in delaying messages about window sizes. 25627067Skarels * If resending a FIN, be sure not to use a new sequence number. 2575075Swnj */ 25831725Skarels if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 25931725Skarels tp->snd_nxt == tp->snd_max) 26027067Skarels tp->snd_nxt--; 26125940Skarels ti->ti_seq = htonl(tp->snd_nxt); 26225940Skarels ti->ti_ack = htonl(tp->rcv_nxt); 2635441Swnj /* 2645441Swnj * Before ESTABLISHED, force sending of initial options 2655441Swnj * unless TCP set to not do any options. 2665441Swnj */ 26725940Skarels opt = NULL; 26832373Skarels if (flags & TH_SYN && (tp->t_flags & TF_NOOPT) == 0) { 26926387Skarels u_short mss; 27017273Skarels 27117273Skarels mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp)); 27225940Skarels if (mss > IP_MSS - sizeof(struct tcpiphdr)) { 27325940Skarels opt = tcp_initopt; 27425940Skarels optlen = sizeof (tcp_initopt); 27525940Skarels *(u_short *)(opt + 2) = htons(mss); 27625940Skarels } 2775441Swnj } 2788314Sroot if (opt) { 2795110Swnj m0 = m->m_next; 2809643Ssam m->m_next = m_get(M_DONTWAIT, MT_DATA); 2815088Swnj if (m->m_next == 0) { 2825088Swnj (void) m_free(m); 2835441Swnj m_freem(m0); 2846505Ssam return (ENOBUFS); 2855088Swnj } 2865088Swnj m->m_next->m_next = m0; 2875441Swnj m0 = m->m_next; 2885441Swnj m0->m_len = optlen; 2896162Ssam bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); 2905441Swnj opt = (u_char *)(mtod(m0, caddr_t) + optlen); 2915441Swnj while (m0->m_len & 0x3) { 2925441Swnj *opt++ = TCPOPT_EOL; 2935441Swnj m0->m_len++; 2945441Swnj } 2955441Swnj optlen = m0->m_len; 2965441Swnj ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 2975088Swnj } 2985088Swnj ti->ti_flags = flags; 29925940Skarels /* 30025940Skarels * Calculate receive window. Don't shrink window, 30125940Skarels * but avoid silly window syndrome. 30225940Skarels */ 303*33783Skarels if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) 30425940Skarels win = 0; 30533620Skarels if (win > IP_MAXPACKET) 30633620Skarels win = IP_MAXPACKET; 307*33783Skarels if (win < (long)(tp->rcv_adv - tp->rcv_nxt)) 308*33783Skarels win = (long)(tp->rcv_adv - tp->rcv_nxt); 30925940Skarels ti->ti_win = htons((u_short)win); 3105088Swnj if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 31126387Skarels ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 3125075Swnj ti->ti_flags |= TH_URG; 3135075Swnj } else 3145075Swnj /* 3155075Swnj * If no urgent pointer to send, then we pull 3165075Swnj * the urgent pointer to the left edge of the send window 3175075Swnj * so that it doesn't drift into the send window on sequence 3185075Swnj * number wraparound. 3195075Swnj */ 3205088Swnj tp->snd_up = tp->snd_una; /* drag it along */ 3217644Sroot /* 3227644Sroot * If anything to send and we can send it all, set PUSH. 3237644Sroot * (This will keep happy those implementations which only 32410143Ssam * give data to the user when a buffer fills or a PUSH comes in.) 3257644Sroot */ 3267644Sroot if (len && off+len == so->so_snd.sb_cc) 3277644Sroot ti->ti_flags |= TH_PUSH; 3285075Swnj 3295075Swnj /* 3305075Swnj * Put TCP length in extended header, and then 3315075Swnj * checksum extended header and data. 3325075Swnj */ 33325940Skarels if (len + optlen) 33425940Skarels ti->ti_len = htons((u_short)(sizeof(struct tcphdr) + 33525940Skarels optlen + len)); 3366162Ssam ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len); 3375075Swnj 3385075Swnj /* 3397125Swnj * In transmit state, time the transmission and arrange for 34021116Skarels * the retransmit. In persist state, just set snd_max. 3415088Swnj */ 34221116Skarels if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { 34331758Skarels tcp_seq startseq = tp->snd_nxt; 34431758Skarels 3457125Swnj /* 3467146Swnj * Advance snd_nxt over sequence space of this segment. 3477125Swnj */ 34827067Skarels if (flags & TH_SYN) 3497125Swnj tp->snd_nxt++; 35027067Skarels if (flags & TH_FIN) { 35127067Skarels tp->snd_nxt++; 35227067Skarels tp->t_flags |= TF_SENTFIN; 35327067Skarels } 3547125Swnj tp->snd_nxt += len; 35531758Skarels if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 3567149Swnj tp->snd_max = tp->snd_nxt; 35731758Skarels /* 35831758Skarels * Time this transmission if not a retransmission and 35931758Skarels * not currently timing anything. 36031758Skarels */ 36131758Skarels if (tp->t_rtt == 0) { 36231758Skarels tp->t_rtt = 1; 36331758Skarels tp->t_rtseq = startseq; 36431758Skarels tcpstat.tcps_segstimed++; 36531758Skarels } 36631758Skarels } 3675088Swnj 3687125Swnj /* 36921116Skarels * Set retransmit timer if not currently set, 37026443Skarels * and not doing an ack or a keep-alive probe. 37131726Skarels * Initial value for retransmit timer is smoothed 37231726Skarels * round-trip time + 2 * round-trip time variance. 37326834Skarels * Initialize shift counter which is used for backoff 37426834Skarels * of retransmit time. 3757125Swnj */ 3767125Swnj if (tp->t_timer[TCPT_REXMT] == 0 && 3777125Swnj tp->snd_nxt != tp->snd_una) { 37832034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 37932034Skarels if (tp->t_timer[TCPT_PERSIST]) { 38032034Skarels tp->t_timer[TCPT_PERSIST] = 0; 38132034Skarels tp->t_rxtshift = 0; 38232034Skarels } 3837125Swnj } 38426443Skarels } else 38525940Skarels if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 38625940Skarels tp->snd_max = tp->snd_nxt + len; 3875163Swnj 3885163Swnj /* 3895268Sroot * Trace. 3905268Sroot */ 3917146Swnj if (so->so_options & SO_DEBUG) 3925268Sroot tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 3935268Sroot 3945268Sroot /* 3955075Swnj * Fill in IP length and desired time to live and 3965075Swnj * send to IP level. 3975075Swnj */ 3985441Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; 3995075Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 40026059Skarels error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 40126059Skarels so->so_options & SO_DONTROUTE); 40233446Skarels if (error) { 40333446Skarels if (error == ENOBUFS) { 40433446Skarels tcp_quench(tp->t_inpcb); 40533446Skarels return (0); 40633446Skarels } 4076505Ssam return (error); 40833446Skarels } 40931442Skarels tcpstat.tcps_sndtotal++; 4105075Swnj 4115075Swnj /* 4125075Swnj * Data sent (as far as we can tell). 4135075Swnj * If this advertises a larger window than any other segment, 4145245Sroot * then remember the size of the advertised window. 41525940Skarels * Any pending ACK has now been sent. 4165075Swnj */ 4175252Sroot if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 4185075Swnj tp->rcv_adv = tp->rcv_nxt + win; 4195088Swnj tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 42025940Skarels if (sendalot) 4217125Swnj goto again; 4226505Ssam return (0); 4234677Swnj } 4247125Swnj 4257125Swnj tcp_setpersist(tp) 4267125Swnj register struct tcpcb *tp; 4277125Swnj { 42831726Skarels register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 4297125Swnj 4307125Swnj if (tp->t_timer[TCPT_REXMT]) 4317125Swnj panic("tcp_output REXMT"); 4327125Swnj /* 4337125Swnj * Start/restart persistance timer. 4347125Swnj */ 4357125Swnj TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 43631726Skarels t * tcp_backoff[tp->t_rxtshift], 43731725Skarels TCPTV_PERSMIN, TCPTV_PERSMAX); 43831725Skarels if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 43931725Skarels tp->t_rxtshift++; 4407125Swnj } 441