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 6*34855Sbostic * provided that the above copyright notice and this paragraph are 7*34855Sbostic * duplicated in all such forms and that any documentation, 8*34855Sbostic * advertising materials, and other materials related to such 9*34855Sbostic * distribution and use acknowledge that the software was developed 10*34855Sbostic * by the University of California, Berkeley. The name of the 11*34855Sbostic * University may not be used to endorse or promote products derived 12*34855Sbostic * from this software without specific prior written permission. 13*34855Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34855Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34855Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1632788Sbostic * 17*34855Sbostic * @(#)tcp_output.c 7.17 (Berkeley) 06/29/88 1823191Smckusick */ 194677Swnj 2017063Sbloom #include "param.h" 2117063Sbloom #include "systm.h" 2217063Sbloom #include "mbuf.h" 2317063Sbloom #include "protosw.h" 2417063Sbloom #include "socket.h" 2517063Sbloom #include "socketvar.h" 2617063Sbloom #include "errno.h" 2710895Ssam 2810895Ssam #include "../net/route.h" 2910895Ssam 3017063Sbloom #include "in.h" 3117063Sbloom #include "in_pcb.h" 3217063Sbloom #include "in_systm.h" 3317063Sbloom #include "ip.h" 3417063Sbloom #include "ip_var.h" 3517063Sbloom #include "tcp.h" 365088Swnj #define TCPOUTFLAGS 3717063Sbloom #include "tcp_fsm.h" 3817063Sbloom #include "tcp_seq.h" 3917063Sbloom #include "tcp_timer.h" 4017063Sbloom #include "tcp_var.h" 4117063Sbloom #include "tcpip.h" 4217063Sbloom #include "tcp_debug.h" 434677Swnj 444678Swnj /* 458314Sroot * Initial options. 465441Swnj */ 475441Swnj u_char tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, }; 485441Swnj 495441Swnj /* 505245Sroot * Tcp output routine: figure out what should be sent and send it. 514678Swnj */ 525075Swnj tcp_output(tp) 535075Swnj register struct tcpcb *tp; 544678Swnj { 555075Swnj register struct socket *so = tp->t_inpcb->inp_socket; 5634500Skarels register long len, win; 575075Swnj struct mbuf *m0; 5825940Skarels int off, flags, error; 595075Swnj register struct mbuf *m; 605075Swnj register struct tcpiphdr *ti; 615441Swnj u_char *opt; 625441Swnj unsigned optlen = 0; 6325940Skarels int idle, sendalot; 644678Swnj 655075Swnj /* 666279Swnj * Determine length of data that should be transmitted, 675088Swnj * and flags that will be used. 685088Swnj * If there is some data or critical controls (SYN, RST) 695088Swnj * to send, then transmit; otherwise, investigate further. 705075Swnj */ 7125940Skarels idle = (tp->snd_max == tp->snd_una); 727125Swnj again: 737125Swnj sendalot = 0; 745075Swnj off = tp->snd_nxt - tp->snd_una; 7521116Skarels win = MIN(tp->snd_wnd, tp->snd_cwnd); 7626834Skarels 7721116Skarels /* 7821116Skarels * If in persist timeout with window of 0, send 1 byte. 7925940Skarels * Otherwise, if window is small but nonzero 8025940Skarels * and timer expired, we will send what we can 8125940Skarels * and go to transmit state. 8221116Skarels */ 8321116Skarels if (tp->t_force) { 8426834Skarels if (win == 0) 8521116Skarels win = 1; 8621116Skarels else { 8721116Skarels tp->t_timer[TCPT_PERSIST] = 0; 8821116Skarels tp->t_rxtshift = 0; 8921116Skarels } 9021116Skarels } 9125940Skarels 9217361Skarels len = MIN(so->so_snd.sb_cc, win) - off; 935088Swnj flags = tcp_outflags[tp->t_state]; 9425940Skarels 9526834Skarels if (len < 0) { 9626834Skarels /* 9727048Skarels * If FIN has been sent but not acked, 9827048Skarels * but we haven't been called to retransmit, 9932785Skarels * len will be -1. Otherwise, window shrank 10032785Skarels * after we sent into it. If window shrank to 0, 10132785Skarels * cancel pending retransmit and pull snd_nxt 10232785Skarels * back to (closed) window. We will enter persist 10332785Skarels * state below. If the window didn't close completely, 10427048Skarels * just wait for an ACK. 10526834Skarels */ 10632785Skarels len = 0; 10732785Skarels if (win == 0) { 10827067Skarels tp->t_timer[TCPT_REXMT] = 0; 10927067Skarels tp->snd_nxt = tp->snd_una; 11032785Skarels } 11126834Skarels } 11227067Skarels if (len > tp->t_maxseg) { 11327067Skarels len = tp->t_maxseg; 11431442Skarels sendalot = 1; 11527067Skarels } 11629795Skarels if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 11729795Skarels flags &= ~TH_FIN; 11826834Skarels win = sbspace(&so->so_rcv); 11926834Skarels 12027067Skarels 12125940Skarels /* 12227067Skarels * If our state indicates that FIN should be sent 12327067Skarels * and we have not yet done so, or we're retransmitting the FIN, 12427067Skarels * then we need to send. 12527067Skarels */ 12627067Skarels if (flags & TH_FIN && 12727067Skarels ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 12827067Skarels goto send; 12927067Skarels /* 13025940Skarels * Send if we owe peer an ACK. 13125940Skarels */ 13226834Skarels if (tp->t_flags & TF_ACKNOW) 13325940Skarels goto send; 13427067Skarels if (flags & (TH_SYN|TH_RST)) 13526834Skarels goto send; 1366279Swnj if (SEQ_GT(tp->snd_up, tp->snd_una)) 1376279Swnj goto send; 1384678Swnj 1395075Swnj /* 14017318Skarels * Sender silly window avoidance. If connection is idle 14117318Skarels * and can send all data, a maximum segment, 14217318Skarels * at least a maximum default-size segment do it, 1436279Swnj * or are forced, do it; otherwise don't bother. 14425261Skarels * If peer's buffer is tiny, then send 14525261Skarels * when window is at least half open. 14621116Skarels * If retransmitting (possibly after persist timer forced us 14721116Skarels * to send into a small window), then must resend. 1486279Swnj */ 1496279Swnj if (len) { 15031725Skarels if (len == tp->t_maxseg) 1516279Swnj goto send; 15225940Skarels if ((idle || tp->t_flags & TF_NODELAY) && 15325940Skarels len + off >= so->so_snd.sb_cc) 1546279Swnj goto send; 1556279Swnj if (tp->t_force) 1566279Swnj goto send; 15725261Skarels if (len >= tp->max_sndwnd / 2) 15825261Skarels goto send; 15921116Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 16021116Skarels goto send; 16126834Skarels } 1626279Swnj 1635441Swnj /* 16425940Skarels * Compare available window to amount of window 16525940Skarels * known to peer (as advertised window less 16632785Skarels * next expected input). If the difference is at least two 16732785Skarels * max size segments or at least 35% of the maximum possible 16832785Skarels * window, then want to send a window update to peer. 1695075Swnj */ 17032034Skarels if (win > 0) { 17132034Skarels int adv = win - (tp->rcv_adv - tp->rcv_nxt); 1724678Swnj 17332785Skarels if (so->so_rcv.sb_cc == 0 && adv >= 2 * tp->t_maxseg) 17432785Skarels goto send; 17532034Skarels if (100 * adv / so->so_rcv.sb_hiwat >= 35) 17632034Skarels goto send; 17732034Skarels } 17832034Skarels 1795075Swnj /* 1807125Swnj * TCP window updates are not reliable, rather a polling protocol 1817125Swnj * using ``persist'' packets is used to insure receipt of window 1827125Swnj * updates. The three ``states'' for the output side are: 1837125Swnj * idle not doing retransmits or persists 18425940Skarels * persisting to move a small or zero window 1857125Swnj * (re)transmitting and thereby not persisting 1867125Swnj * 1877125Swnj * tp->t_timer[TCPT_PERSIST] 1887125Swnj * is set when we are in persist state. 1897125Swnj * tp->t_force 1907125Swnj * is set when we are called to send a persist packet. 1917125Swnj * tp->t_timer[TCPT_REXMT] 1927125Swnj * is set when we are retransmitting 1937125Swnj * The output side is idle when both timers are zero. 1947125Swnj * 19521116Skarels * If send window is too small, there is data to transmit, and no 19621116Skarels * retransmit or persist is pending, then go to persist state. 19721116Skarels * If nothing happens soon, send when timer expires: 19821116Skarels * if window is nonzero, transmit what we can, 19921116Skarels * otherwise force out a byte. 2007125Swnj */ 20121116Skarels if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 && 20221116Skarels tp->t_timer[TCPT_PERSIST] == 0) { 2037125Swnj tp->t_rxtshift = 0; 2047125Swnj tcp_setpersist(tp); 2057125Swnj } 2067125Swnj 2077125Swnj /* 2085075Swnj * No reason to send a segment, just return. 2095075Swnj */ 2105110Swnj return (0); 2114678Swnj 2125075Swnj send: 2135075Swnj /* 2145075Swnj * Grab a header mbuf, attaching a copy of data to 2155075Swnj * be transmitted, and initialize the header from 2165075Swnj * the template for sends on this connection. 2175075Swnj */ 21811720Ssam MGET(m, M_DONTWAIT, MT_HEADER); 21911720Ssam if (m == NULL) 2206505Ssam return (ENOBUFS); 22134500Skarels #define MAXLINKHDR 32 /* belongs elsewhere */ 22234500Skarels #define DATASPACE (MMAXOFF - (MMINOFF + MAXLINKHDR + sizeof (struct tcpiphdr))) 22334500Skarels m->m_off = MMINOFF + MAXLINKHDR; 2244885Swnj m->m_len = sizeof (struct tcpiphdr); 22534500Skarels ti = mtod(m, struct tcpiphdr *); 2265075Swnj if (len) { 22731442Skarels if (tp->t_force && len == 1) 22831442Skarels tcpstat.tcps_sndprobe++; 22931442Skarels else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 23031442Skarels tcpstat.tcps_sndrexmitpack++; 23131442Skarels tcpstat.tcps_sndrexmitbyte += len; 23231442Skarels } else { 23331442Skarels tcpstat.tcps_sndpack++; 23431442Skarels tcpstat.tcps_sndbyte += len; 23531442Skarels } 23634500Skarels if (len <= DATASPACE) { 23734500Skarels m_copydata(so->so_snd.sb_mb, off, (int) len, 23834500Skarels mtod(m, caddr_t) + sizeof(struct tcpiphdr)); 23934500Skarels m->m_len += len; 24034500Skarels } else { 24134500Skarels m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 24234500Skarels if (m->m_next == 0) 24334500Skarels len = 0; 24434500Skarels } 24531442Skarels } else if (tp->t_flags & TF_ACKNOW) 24631442Skarels tcpstat.tcps_sndacks++; 24731442Skarels else if (flags & (TH_SYN|TH_FIN|TH_RST)) 24831442Skarels tcpstat.tcps_sndctrl++; 24931442Skarels else if (SEQ_GT(tp->snd_up, tp->snd_una)) 25031442Skarels tcpstat.tcps_sndurg++; 25131442Skarels else 25231442Skarels tcpstat.tcps_sndwinup++; 25331442Skarels 2545075Swnj if (tp->t_template == 0) 2555075Swnj panic("tcp_output"); 2565110Swnj bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 2575075Swnj 2585075Swnj /* 2595075Swnj * Fill in fields, remembering maximum advertised 2605075Swnj * window for use in delaying messages about window sizes. 26127067Skarels * If resending a FIN, be sure not to use a new sequence number. 2625075Swnj */ 26331725Skarels if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 26431725Skarels tp->snd_nxt == tp->snd_max) 26527067Skarels tp->snd_nxt--; 26625940Skarels ti->ti_seq = htonl(tp->snd_nxt); 26725940Skarels ti->ti_ack = htonl(tp->rcv_nxt); 2685441Swnj /* 2695441Swnj * Before ESTABLISHED, force sending of initial options 2705441Swnj * unless TCP set to not do any options. 2715441Swnj */ 27225940Skarels opt = NULL; 27332373Skarels if (flags & TH_SYN && (tp->t_flags & TF_NOOPT) == 0) { 27426387Skarels u_short mss; 27517273Skarels 27617273Skarels mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp)); 27725940Skarels if (mss > IP_MSS - sizeof(struct tcpiphdr)) { 27825940Skarels opt = tcp_initopt; 27925940Skarels optlen = sizeof (tcp_initopt); 28025940Skarels *(u_short *)(opt + 2) = htons(mss); 28125940Skarels } 2825441Swnj } 2838314Sroot if (opt) { 2845110Swnj m0 = m->m_next; 2859643Ssam m->m_next = m_get(M_DONTWAIT, MT_DATA); 2865088Swnj if (m->m_next == 0) { 2875088Swnj (void) m_free(m); 2885441Swnj m_freem(m0); 2896505Ssam return (ENOBUFS); 2905088Swnj } 2915088Swnj m->m_next->m_next = m0; 2925441Swnj m0 = m->m_next; 2935441Swnj m0->m_len = optlen; 2946162Ssam bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); 2955441Swnj opt = (u_char *)(mtod(m0, caddr_t) + optlen); 2965441Swnj while (m0->m_len & 0x3) { 2975441Swnj *opt++ = TCPOPT_EOL; 2985441Swnj m0->m_len++; 2995441Swnj } 3005441Swnj optlen = m0->m_len; 3015441Swnj ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 3025088Swnj } 3035088Swnj ti->ti_flags = flags; 30425940Skarels /* 30525940Skarels * Calculate receive window. Don't shrink window, 30625940Skarels * but avoid silly window syndrome. 30725940Skarels */ 30833783Skarels if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) 30925940Skarels win = 0; 31034500Skarels if (win > IP_MAXPACKET) 31134500Skarels win = IP_MAXPACKET; 31233785Skarels if (win < (long)(tp->rcv_adv - tp->rcv_nxt)) 31333785Skarels win = (long)(tp->rcv_adv - tp->rcv_nxt); 31425940Skarels ti->ti_win = htons((u_short)win); 3155088Swnj if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 31626387Skarels ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 3175075Swnj ti->ti_flags |= TH_URG; 3185075Swnj } else 3195075Swnj /* 3205075Swnj * If no urgent pointer to send, then we pull 3215075Swnj * the urgent pointer to the left edge of the send window 3225075Swnj * so that it doesn't drift into the send window on sequence 3235075Swnj * number wraparound. 3245075Swnj */ 3255088Swnj tp->snd_up = tp->snd_una; /* drag it along */ 3267644Sroot /* 3277644Sroot * If anything to send and we can send it all, set PUSH. 3287644Sroot * (This will keep happy those implementations which only 32910143Ssam * give data to the user when a buffer fills or a PUSH comes in.) 3307644Sroot */ 3317644Sroot if (len && off+len == so->so_snd.sb_cc) 3327644Sroot ti->ti_flags |= TH_PUSH; 3335075Swnj 3345075Swnj /* 3355075Swnj * Put TCP length in extended header, and then 3365075Swnj * checksum extended header and data. 3375075Swnj */ 33825940Skarels if (len + optlen) 33925940Skarels ti->ti_len = htons((u_short)(sizeof(struct tcphdr) + 34025940Skarels optlen + len)); 34134500Skarels ti->ti_sum = in_cksum(m, 34234500Skarels (int)(sizeof (struct tcpiphdr) + (int)optlen + len)); 3435075Swnj 3445075Swnj /* 3457125Swnj * In transmit state, time the transmission and arrange for 34621116Skarels * the retransmit. In persist state, just set snd_max. 3475088Swnj */ 34821116Skarels if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { 34931758Skarels tcp_seq startseq = tp->snd_nxt; 35031758Skarels 3517125Swnj /* 3527146Swnj * Advance snd_nxt over sequence space of this segment. 3537125Swnj */ 35427067Skarels if (flags & TH_SYN) 3557125Swnj tp->snd_nxt++; 35627067Skarels if (flags & TH_FIN) { 35727067Skarels tp->snd_nxt++; 35827067Skarels tp->t_flags |= TF_SENTFIN; 35927067Skarels } 3607125Swnj tp->snd_nxt += len; 36131758Skarels if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 3627149Swnj tp->snd_max = tp->snd_nxt; 36331758Skarels /* 36431758Skarels * Time this transmission if not a retransmission and 36531758Skarels * not currently timing anything. 36631758Skarels */ 36731758Skarels if (tp->t_rtt == 0) { 36831758Skarels tp->t_rtt = 1; 36931758Skarels tp->t_rtseq = startseq; 37031758Skarels tcpstat.tcps_segstimed++; 37131758Skarels } 37231758Skarels } 3735088Swnj 3747125Swnj /* 37521116Skarels * Set retransmit timer if not currently set, 37626443Skarels * and not doing an ack or a keep-alive probe. 37731726Skarels * Initial value for retransmit timer is smoothed 37831726Skarels * round-trip time + 2 * round-trip time variance. 37926834Skarels * Initialize shift counter which is used for backoff 38026834Skarels * of retransmit time. 3817125Swnj */ 3827125Swnj if (tp->t_timer[TCPT_REXMT] == 0 && 3837125Swnj tp->snd_nxt != tp->snd_una) { 38432034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 38532034Skarels if (tp->t_timer[TCPT_PERSIST]) { 38632034Skarels tp->t_timer[TCPT_PERSIST] = 0; 38732034Skarels tp->t_rxtshift = 0; 38832034Skarels } 3897125Swnj } 39026443Skarels } else 39125940Skarels if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 39225940Skarels tp->snd_max = tp->snd_nxt + len; 3935163Swnj 3945163Swnj /* 3955268Sroot * Trace. 3965268Sroot */ 3977146Swnj if (so->so_options & SO_DEBUG) 3985268Sroot tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 3995268Sroot 4005268Sroot /* 4015075Swnj * Fill in IP length and desired time to live and 4025075Swnj * send to IP level. 4035075Swnj */ 4045441Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; 4055075Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 40626059Skarels error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 40726059Skarels so->so_options & SO_DONTROUTE); 40833446Skarels if (error) { 40933446Skarels if (error == ENOBUFS) { 41033446Skarels tcp_quench(tp->t_inpcb); 41133446Skarels return (0); 41233446Skarels } 4136505Ssam return (error); 41433446Skarels } 41531442Skarels tcpstat.tcps_sndtotal++; 4165075Swnj 4175075Swnj /* 4185075Swnj * Data sent (as far as we can tell). 4195075Swnj * If this advertises a larger window than any other segment, 4205245Sroot * then remember the size of the advertised window. 42125940Skarels * Any pending ACK has now been sent. 4225075Swnj */ 4235252Sroot if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 4245075Swnj tp->rcv_adv = tp->rcv_nxt + win; 4255088Swnj tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 42625940Skarels if (sendalot) 4277125Swnj goto again; 4286505Ssam return (0); 4294677Swnj } 4307125Swnj 4317125Swnj tcp_setpersist(tp) 4327125Swnj register struct tcpcb *tp; 4337125Swnj { 43431726Skarels register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 4357125Swnj 4367125Swnj if (tp->t_timer[TCPT_REXMT]) 4377125Swnj panic("tcp_output REXMT"); 4387125Swnj /* 4397125Swnj * Start/restart persistance timer. 4407125Swnj */ 4417125Swnj TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 44231726Skarels t * tcp_backoff[tp->t_rxtshift], 44331725Skarels TCPTV_PERSMIN, TCPTV_PERSMAX); 44431725Skarels if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 44531725Skarels tp->t_rxtshift++; 4467125Swnj } 447