1df8bae1dSRodney W. Grimes /* 2d7f570e6SGarrett Wollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33d7f570e6SGarrett Wollman * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 370cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 380cc12cc5SJoerg Wunsch 39a04884fcSBill Fenner #include <stddef.h> 40a04884fcSBill Fenner 41df8bae1dSRodney W. Grimes #include <sys/param.h> 42df8bae1dSRodney W. Grimes #include <sys/systm.h> 439a039a5fSDavid Greenman #include <sys/kernel.h> 449a039a5fSDavid Greenman #include <sys/sysctl.h> 45df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 46df8bae1dSRodney W. Grimes #include <sys/protosw.h> 47df8bae1dSRodney W. Grimes #include <sys/socket.h> 48df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 49df8bae1dSRodney W. Grimes 50df8bae1dSRodney W. Grimes #include <net/route.h> 51df8bae1dSRodney W. Grimes 52df8bae1dSRodney W. Grimes #include <netinet/in.h> 53df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 54df8bae1dSRodney W. Grimes #include <netinet/ip.h> 55df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 56df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 57df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 58df8bae1dSRodney W. Grimes #define TCPOUTFLAGS 59df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 60df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 61df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 62df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 63df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 64610ee2f9SDavid Greenman #ifdef TCPDEBUG 65df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 66610ee2f9SDavid Greenman #endif 67df8bae1dSRodney W. Grimes 68df8bae1dSRodney W. Grimes #ifdef notyet 69df8bae1dSRodney W. Grimes extern struct mbuf *m_copypack(); 70df8bae1dSRodney W. Grimes #endif 71df8bae1dSRodney W. Grimes 729a039a5fSDavid Greenman static int path_mtu_discovery = 1; 739a039a5fSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW, 749a039a5fSDavid Greenman &path_mtu_discovery, 1, "Enable Path MTU Discovery"); 759a039a5fSDavid Greenman 769b8b58e0SJonathan Lemon int ss_fltsz = 1; 779b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW, 789b8b58e0SJonathan Lemon &ss_fltsz, 1, "Slow start flight size"); 799b8b58e0SJonathan Lemon 809b8b58e0SJonathan Lemon int ss_fltsz_local = TCP_MAXWIN; /* something large */ 819b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW, 829b8b58e0SJonathan Lemon &ss_fltsz_local, 1, "Slow start flight size for local networks"); 83df8bae1dSRodney W. Grimes 84df8bae1dSRodney W. Grimes /* 85df8bae1dSRodney W. Grimes * Tcp output routine: figure out what should be sent and send it. 86df8bae1dSRodney W. Grimes */ 87df8bae1dSRodney W. Grimes int 88df8bae1dSRodney W. Grimes tcp_output(tp) 89df8bae1dSRodney W. Grimes register struct tcpcb *tp; 90df8bae1dSRodney W. Grimes { 91df8bae1dSRodney W. Grimes register struct socket *so = tp->t_inpcb->inp_socket; 92df8bae1dSRodney W. Grimes register long len, win; 93df8bae1dSRodney W. Grimes int off, flags, error; 94df8bae1dSRodney W. Grimes register struct mbuf *m; 95df8bae1dSRodney W. Grimes register struct tcpiphdr *ti; 96a0292f23SGarrett Wollman u_char opt[TCP_MAXOLEN]; 97a04884fcSBill Fenner unsigned ipoptlen, optlen, hdrlen; 98df8bae1dSRodney W. Grimes int idle, sendalot; 99a0292f23SGarrett Wollman struct rmxp_tao *taop; 100a0292f23SGarrett Wollman struct rmxp_tao tao_noncached; 101df8bae1dSRodney W. Grimes 102df8bae1dSRodney W. Grimes /* 103df8bae1dSRodney W. Grimes * Determine length of data that should be transmitted, 104df8bae1dSRodney W. Grimes * and flags that will be used. 105df8bae1dSRodney W. Grimes * If there is some data or critical controls (SYN, RST) 106df8bae1dSRodney W. Grimes * to send, then transmit; otherwise, investigate further. 107df8bae1dSRodney W. Grimes */ 108df8bae1dSRodney W. Grimes idle = (tp->snd_max == tp->snd_una); 1099b8b58e0SJonathan Lemon if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) { 110df8bae1dSRodney W. Grimes /* 111df8bae1dSRodney W. Grimes * We have been idle for "a while" and no acks are 112df8bae1dSRodney W. Grimes * expected to clock out any data we send -- 113df8bae1dSRodney W. Grimes * slow start to get ack "clock" running again. 1149b8b58e0SJonathan Lemon * 1159b8b58e0SJonathan Lemon * Set the slow-start flight size depending on whether 1169b8b58e0SJonathan Lemon * this is a local network or not. 117df8bae1dSRodney W. Grimes */ 1189b8b58e0SJonathan Lemon if (in_localaddr(tp->t_inpcb->inp_faddr)) 1199b8b58e0SJonathan Lemon tp->snd_cwnd = tp->t_maxseg * ss_fltsz_local; 1209b8b58e0SJonathan Lemon else 1219b8b58e0SJonathan Lemon tp->snd_cwnd = tp->t_maxseg * ss_fltsz; 1229b8b58e0SJonathan Lemon } 123df8bae1dSRodney W. Grimes again: 124df8bae1dSRodney W. Grimes sendalot = 0; 125df8bae1dSRodney W. Grimes off = tp->snd_nxt - tp->snd_una; 126df8bae1dSRodney W. Grimes win = min(tp->snd_wnd, tp->snd_cwnd); 127df8bae1dSRodney W. Grimes 128df8bae1dSRodney W. Grimes flags = tcp_outflags[tp->t_state]; 129a0292f23SGarrett Wollman /* 130a0292f23SGarrett Wollman * Get standard flags, and add SYN or FIN if requested by 'hidden' 131a0292f23SGarrett Wollman * state flags. 132a0292f23SGarrett Wollman */ 133a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) 134a0292f23SGarrett Wollman flags |= TH_FIN; 135a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) 136a0292f23SGarrett Wollman flags |= TH_SYN; 137a0292f23SGarrett Wollman 138df8bae1dSRodney W. Grimes /* 139df8bae1dSRodney W. Grimes * If in persist timeout with window of 0, send 1 byte. 140df8bae1dSRodney W. Grimes * Otherwise, if window is small but nonzero 141df8bae1dSRodney W. Grimes * and timer expired, we will send what we can 142df8bae1dSRodney W. Grimes * and go to transmit state. 143df8bae1dSRodney W. Grimes */ 144df8bae1dSRodney W. Grimes if (tp->t_force) { 145df8bae1dSRodney W. Grimes if (win == 0) { 146df8bae1dSRodney W. Grimes /* 147df8bae1dSRodney W. Grimes * If we still have some data to send, then 148df8bae1dSRodney W. Grimes * clear the FIN bit. Usually this would 149df8bae1dSRodney W. Grimes * happen below when it realizes that we 150df8bae1dSRodney W. Grimes * aren't sending all the data. However, 15129089b51SJulian Elischer * if we have exactly 1 byte of unsent data, 152df8bae1dSRodney W. Grimes * then it won't clear the FIN bit below, 153df8bae1dSRodney W. Grimes * and if we are in persist state, we wind 154df8bae1dSRodney W. Grimes * up sending the packet without recording 155df8bae1dSRodney W. Grimes * that we sent the FIN bit. 156df8bae1dSRodney W. Grimes * 157df8bae1dSRodney W. Grimes * We can't just blindly clear the FIN bit, 158df8bae1dSRodney W. Grimes * because if we don't have any more data 159df8bae1dSRodney W. Grimes * to send then the probe will be the FIN 160df8bae1dSRodney W. Grimes * itself. 161df8bae1dSRodney W. Grimes */ 162df8bae1dSRodney W. Grimes if (off < so->so_snd.sb_cc) 163df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 164df8bae1dSRodney W. Grimes win = 1; 165df8bae1dSRodney W. Grimes } else { 1669b8b58e0SJonathan Lemon callout_stop(tp->tt_persist); 167df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 168df8bae1dSRodney W. Grimes } 169df8bae1dSRodney W. Grimes } 170df8bae1dSRodney W. Grimes 1719105bb46SBruce Evans len = (long)ulmin(so->so_snd.sb_cc, win) - off; 172df8bae1dSRodney W. Grimes 173a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) { 174a0292f23SGarrett Wollman taop = &tao_noncached; 175a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 176a0292f23SGarrett Wollman } 177a0292f23SGarrett Wollman 178a0292f23SGarrett Wollman /* 179a0292f23SGarrett Wollman * Lop off SYN bit if it has already been sent. However, if this 180a0292f23SGarrett Wollman * is SYN-SENT state and if segment contains data and if we don't 181a0292f23SGarrett Wollman * know that foreign host supports TAO, suppress sending segment. 182a0292f23SGarrett Wollman */ 183a0292f23SGarrett Wollman if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 184a0292f23SGarrett Wollman flags &= ~TH_SYN; 185a0292f23SGarrett Wollman off--, len++; 186a0292f23SGarrett Wollman if (len > 0 && tp->t_state == TCPS_SYN_SENT && 187a0292f23SGarrett Wollman taop->tao_ccsent == 0) 188a0292f23SGarrett Wollman return 0; 189a0292f23SGarrett Wollman } 190a0292f23SGarrett Wollman 19181165e48SAndras Olah /* 19281165e48SAndras Olah * Be careful not to send data and/or FIN on SYN segments 19381165e48SAndras Olah * in cases when no CC option will be sent. 19481165e48SAndras Olah * This measure is needed to prevent interoperability problems 19581165e48SAndras Olah * with not fully conformant TCP implementations. 19681165e48SAndras Olah */ 19781165e48SAndras Olah if ((flags & TH_SYN) && 19881165e48SAndras Olah ((tp->t_flags & TF_NOOPT) || !(tp->t_flags & TF_REQ_CC) || 19981165e48SAndras Olah ((flags & TH_ACK) && !(tp->t_flags & TF_RCVD_CC)))) { 20081165e48SAndras Olah len = 0; 20181165e48SAndras Olah flags &= ~TH_FIN; 20281165e48SAndras Olah } 20381165e48SAndras Olah 204df8bae1dSRodney W. Grimes if (len < 0) { 205df8bae1dSRodney W. Grimes /* 206df8bae1dSRodney W. Grimes * If FIN has been sent but not acked, 207df8bae1dSRodney W. Grimes * but we haven't been called to retransmit, 208df8bae1dSRodney W. Grimes * len will be -1. Otherwise, window shrank 209df8bae1dSRodney W. Grimes * after we sent into it. If window shrank to 0, 2102d8266afSDavid Greenman * cancel pending retransmit, pull snd_nxt back 2112d8266afSDavid Greenman * to (closed) window, and set the persist timer 2122d8266afSDavid Greenman * if it isn't already going. If the window didn't 2132d8266afSDavid Greenman * close completely, just wait for an ACK. 214df8bae1dSRodney W. Grimes */ 215df8bae1dSRodney W. Grimes len = 0; 216df8bae1dSRodney W. Grimes if (win == 0) { 2179b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 2182d8266afSDavid Greenman tp->t_rxtshift = 0; 219df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2209b8b58e0SJonathan Lemon if (!callout_active(tp->tt_persist)) 2212d8266afSDavid Greenman tcp_setpersist(tp); 222df8bae1dSRodney W. Grimes } 223df8bae1dSRodney W. Grimes } 224df8bae1dSRodney W. Grimes if (len > tp->t_maxseg) { 225df8bae1dSRodney W. Grimes len = tp->t_maxseg; 226df8bae1dSRodney W. Grimes sendalot = 1; 227df8bae1dSRodney W. Grimes } 228df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 229df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 230df8bae1dSRodney W. Grimes 231df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 232df8bae1dSRodney W. Grimes 233df8bae1dSRodney W. Grimes /* 234df8bae1dSRodney W. Grimes * Sender silly window avoidance. If connection is idle 235df8bae1dSRodney W. Grimes * and can send all data, a maximum segment, 236df8bae1dSRodney W. Grimes * at least a maximum default-size segment do it, 237df8bae1dSRodney W. Grimes * or are forced, do it; otherwise don't bother. 238df8bae1dSRodney W. Grimes * If peer's buffer is tiny, then send 239df8bae1dSRodney W. Grimes * when window is at least half open. 240df8bae1dSRodney W. Grimes * If retransmitting (possibly after persist timer forced us 241df8bae1dSRodney W. Grimes * to send into a small window), then must resend. 242df8bae1dSRodney W. Grimes */ 243df8bae1dSRodney W. Grimes if (len) { 244df8bae1dSRodney W. Grimes if (len == tp->t_maxseg) 245df8bae1dSRodney W. Grimes goto send; 246b0acefa8SBill Fenner if (!(tp->t_flags & TF_MORETOCOME) && 247b0acefa8SBill Fenner (idle || tp->t_flags & TF_NODELAY) && 248a0292f23SGarrett Wollman (tp->t_flags & TF_NOPUSH) == 0 && 249df8bae1dSRodney W. Grimes len + off >= so->so_snd.sb_cc) 250df8bae1dSRodney W. Grimes goto send; 251df8bae1dSRodney W. Grimes if (tp->t_force) 252df8bae1dSRodney W. Grimes goto send; 253a0292f23SGarrett Wollman if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 254df8bae1dSRodney W. Grimes goto send; 255df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 256df8bae1dSRodney W. Grimes goto send; 257df8bae1dSRodney W. Grimes } 258df8bae1dSRodney W. Grimes 259df8bae1dSRodney W. Grimes /* 260df8bae1dSRodney W. Grimes * Compare available window to amount of window 261df8bae1dSRodney W. Grimes * known to peer (as advertised window less 262df8bae1dSRodney W. Grimes * next expected input). If the difference is at least two 263df8bae1dSRodney W. Grimes * max size segments, or at least 50% of the maximum possible 264df8bae1dSRodney W. Grimes * window, then want to send a window update to peer. 265df8bae1dSRodney W. Grimes */ 266df8bae1dSRodney W. Grimes if (win > 0) { 267df8bae1dSRodney W. Grimes /* 268df8bae1dSRodney W. Grimes * "adv" is the amount we can increase the window, 269df8bae1dSRodney W. Grimes * taking into account that we are limited by 270df8bae1dSRodney W. Grimes * TCP_MAXWIN << tp->rcv_scale. 271df8bae1dSRodney W. Grimes */ 272df8bae1dSRodney W. Grimes long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) - 273df8bae1dSRodney W. Grimes (tp->rcv_adv - tp->rcv_nxt); 274df8bae1dSRodney W. Grimes 275df8bae1dSRodney W. Grimes if (adv >= (long) (2 * tp->t_maxseg)) 276df8bae1dSRodney W. Grimes goto send; 277df8bae1dSRodney W. Grimes if (2 * adv >= (long) so->so_rcv.sb_hiwat) 278df8bae1dSRodney W. Grimes goto send; 279df8bae1dSRodney W. Grimes } 280df8bae1dSRodney W. Grimes 281df8bae1dSRodney W. Grimes /* 282df8bae1dSRodney W. Grimes * Send if we owe peer an ACK. 283df8bae1dSRodney W. Grimes */ 284df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 285df8bae1dSRodney W. Grimes goto send; 286a0292f23SGarrett Wollman if ((flags & TH_RST) || 287a0292f23SGarrett Wollman ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 288df8bae1dSRodney W. Grimes goto send; 289df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_una)) 290df8bae1dSRodney W. Grimes goto send; 291df8bae1dSRodney W. Grimes /* 292df8bae1dSRodney W. Grimes * If our state indicates that FIN should be sent 293df8bae1dSRodney W. Grimes * and we have not yet done so, or we're retransmitting the FIN, 294df8bae1dSRodney W. Grimes * then we need to send. 295df8bae1dSRodney W. Grimes */ 296df8bae1dSRodney W. Grimes if (flags & TH_FIN && 297df8bae1dSRodney W. Grimes ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 298df8bae1dSRodney W. Grimes goto send; 299df8bae1dSRodney W. Grimes 300df8bae1dSRodney W. Grimes /* 301df8bae1dSRodney W. Grimes * TCP window updates are not reliable, rather a polling protocol 302df8bae1dSRodney W. Grimes * using ``persist'' packets is used to insure receipt of window 303df8bae1dSRodney W. Grimes * updates. The three ``states'' for the output side are: 304df8bae1dSRodney W. Grimes * idle not doing retransmits or persists 305df8bae1dSRodney W. Grimes * persisting to move a small or zero window 306df8bae1dSRodney W. Grimes * (re)transmitting and thereby not persisting 307df8bae1dSRodney W. Grimes * 3089b8b58e0SJonathan Lemon * callout_active(tp->tt_persist) 3099b8b58e0SJonathan Lemon * is true when we are in persist state. 310df8bae1dSRodney W. Grimes * tp->t_force 311df8bae1dSRodney W. Grimes * is set when we are called to send a persist packet. 3129b8b58e0SJonathan Lemon * callout_active(tp->tt_rexmt) 313df8bae1dSRodney W. Grimes * is set when we are retransmitting 314df8bae1dSRodney W. Grimes * The output side is idle when both timers are zero. 315df8bae1dSRodney W. Grimes * 316df8bae1dSRodney W. Grimes * If send window is too small, there is data to transmit, and no 317df8bae1dSRodney W. Grimes * retransmit or persist is pending, then go to persist state. 318df8bae1dSRodney W. Grimes * If nothing happens soon, send when timer expires: 319df8bae1dSRodney W. Grimes * if window is nonzero, transmit what we can, 320df8bae1dSRodney W. Grimes * otherwise force out a byte. 321df8bae1dSRodney W. Grimes */ 3229b8b58e0SJonathan Lemon if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) && 3239b8b58e0SJonathan Lemon !callout_active(tp->tt_persist)) { 324df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 325df8bae1dSRodney W. Grimes tcp_setpersist(tp); 326df8bae1dSRodney W. Grimes } 327df8bae1dSRodney W. Grimes 328df8bae1dSRodney W. Grimes /* 329df8bae1dSRodney W. Grimes * No reason to send a segment, just return. 330df8bae1dSRodney W. Grimes */ 331df8bae1dSRodney W. Grimes return (0); 332df8bae1dSRodney W. Grimes 333df8bae1dSRodney W. Grimes send: 334df8bae1dSRodney W. Grimes /* 335df8bae1dSRodney W. Grimes * Before ESTABLISHED, force sending of initial options 336df8bae1dSRodney W. Grimes * unless TCP set not to do any options. 337df8bae1dSRodney W. Grimes * NOTE: we assume that the IP/TCP header plus TCP options 338df8bae1dSRodney W. Grimes * always fit in a single mbuf, leaving room for a maximum 339df8bae1dSRodney W. Grimes * link header, i.e. 340df8bae1dSRodney W. Grimes * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN 341df8bae1dSRodney W. Grimes */ 342df8bae1dSRodney W. Grimes optlen = 0; 343df8bae1dSRodney W. Grimes hdrlen = sizeof (struct tcpiphdr); 344df8bae1dSRodney W. Grimes if (flags & TH_SYN) { 345df8bae1dSRodney W. Grimes tp->snd_nxt = tp->iss; 346df8bae1dSRodney W. Grimes if ((tp->t_flags & TF_NOOPT) == 0) { 347df8bae1dSRodney W. Grimes u_short mss; 348df8bae1dSRodney W. Grimes 349df8bae1dSRodney W. Grimes opt[0] = TCPOPT_MAXSEG; 350a0292f23SGarrett Wollman opt[1] = TCPOLEN_MAXSEG; 351a0292f23SGarrett Wollman mss = htons((u_short) tcp_mssopt(tp)); 35294a5d9b6SDavid Greenman (void)memcpy(opt + 2, &mss, sizeof(mss)); 353a0292f23SGarrett Wollman optlen = TCPOLEN_MAXSEG; 354df8bae1dSRodney W. Grimes 355df8bae1dSRodney W. Grimes if ((tp->t_flags & TF_REQ_SCALE) && 356df8bae1dSRodney W. Grimes ((flags & TH_ACK) == 0 || 357df8bae1dSRodney W. Grimes (tp->t_flags & TF_RCVD_SCALE))) { 3589105bb46SBruce Evans *((u_int32_t *)(opt + optlen)) = htonl( 359df8bae1dSRodney W. Grimes TCPOPT_NOP << 24 | 360df8bae1dSRodney W. Grimes TCPOPT_WINDOW << 16 | 361df8bae1dSRodney W. Grimes TCPOLEN_WINDOW << 8 | 362df8bae1dSRodney W. Grimes tp->request_r_scale); 363df8bae1dSRodney W. Grimes optlen += 4; 364df8bae1dSRodney W. Grimes } 365df8bae1dSRodney W. Grimes } 366df8bae1dSRodney W. Grimes } 367df8bae1dSRodney W. Grimes 368df8bae1dSRodney W. Grimes /* 369df8bae1dSRodney W. Grimes * Send a timestamp and echo-reply if this is a SYN and our side 370df8bae1dSRodney W. Grimes * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 371df8bae1dSRodney W. Grimes * and our peer have sent timestamps in our SYN's. 372df8bae1dSRodney W. Grimes */ 373df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 374df8bae1dSRodney W. Grimes (flags & TH_RST) == 0 && 375a0292f23SGarrett Wollman ((flags & TH_ACK) == 0 || 376df8bae1dSRodney W. Grimes (tp->t_flags & TF_RCVD_TSTMP))) { 3779105bb46SBruce Evans u_int32_t *lp = (u_int32_t *)(opt + optlen); 378df8bae1dSRodney W. Grimes 379df8bae1dSRodney W. Grimes /* Form timestamp option as shown in appendix A of RFC 1323. */ 380df8bae1dSRodney W. Grimes *lp++ = htonl(TCPOPT_TSTAMP_HDR); 3819b8b58e0SJonathan Lemon *lp++ = htonl(ticks); 382df8bae1dSRodney W. Grimes *lp = htonl(tp->ts_recent); 383df8bae1dSRodney W. Grimes optlen += TCPOLEN_TSTAMP_APPA; 384df8bae1dSRodney W. Grimes } 385df8bae1dSRodney W. Grimes 386a0292f23SGarrett Wollman /* 387a0292f23SGarrett Wollman * Send `CC-family' options if our side wants to use them (TF_REQ_CC), 388a0292f23SGarrett Wollman * options are allowed (!TF_NOOPT) and it's not a RST. 389a0292f23SGarrett Wollman */ 390a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 391a0292f23SGarrett Wollman (flags & TH_RST) == 0) { 392a0292f23SGarrett Wollman switch (flags & (TH_SYN|TH_ACK)) { 393a0292f23SGarrett Wollman /* 394a0292f23SGarrett Wollman * This is a normal ACK, send CC if we received CC before 395a0292f23SGarrett Wollman * from our peer. 396a0292f23SGarrett Wollman */ 397a0292f23SGarrett Wollman case TH_ACK: 398a0292f23SGarrett Wollman if (!(tp->t_flags & TF_RCVD_CC)) 399a0292f23SGarrett Wollman break; 400a0292f23SGarrett Wollman /*FALLTHROUGH*/ 401a0292f23SGarrett Wollman 402a0292f23SGarrett Wollman /* 403a0292f23SGarrett Wollman * We can only get here in T/TCP's SYN_SENT* state, when 404a0292f23SGarrett Wollman * we're a sending a non-SYN segment without waiting for 405a0292f23SGarrett Wollman * the ACK of our SYN. A check above assures that we only 406a0292f23SGarrett Wollman * do this if our peer understands T/TCP. 407a0292f23SGarrett Wollman */ 408a0292f23SGarrett Wollman case 0: 409a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 410a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 411a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_CC; 412a0292f23SGarrett Wollman opt[optlen++] = TCPOLEN_CC; 413a0292f23SGarrett Wollman *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send); 414a0292f23SGarrett Wollman 415a0292f23SGarrett Wollman optlen += 4; 416a0292f23SGarrett Wollman break; 417a0292f23SGarrett Wollman 418a0292f23SGarrett Wollman /* 419a0292f23SGarrett Wollman * This is our initial SYN, check whether we have to use 420a0292f23SGarrett Wollman * CC or CC.new. 421a0292f23SGarrett Wollman */ 422a0292f23SGarrett Wollman case TH_SYN: 423a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 424a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 425a45d2726SAndras Olah opt[optlen++] = tp->t_flags & TF_SENDCCNEW ? 426a45d2726SAndras Olah TCPOPT_CCNEW : TCPOPT_CC; 427a0292f23SGarrett Wollman opt[optlen++] = TCPOLEN_CC; 428a0292f23SGarrett Wollman *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send); 429a0292f23SGarrett Wollman optlen += 4; 430a0292f23SGarrett Wollman break; 431a0292f23SGarrett Wollman 432a0292f23SGarrett Wollman /* 433a0292f23SGarrett Wollman * This is a SYN,ACK; send CC and CC.echo if we received 434a0292f23SGarrett Wollman * CC from our peer. 435a0292f23SGarrett Wollman */ 436a0292f23SGarrett Wollman case (TH_SYN|TH_ACK): 437a0292f23SGarrett Wollman if (tp->t_flags & TF_RCVD_CC) { 438a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 439a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 440a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_CC; 441a0292f23SGarrett Wollman opt[optlen++] = TCPOLEN_CC; 442a0292f23SGarrett Wollman *(u_int32_t *)&opt[optlen] = 443a0292f23SGarrett Wollman htonl(tp->cc_send); 444a0292f23SGarrett Wollman optlen += 4; 445a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 446a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 447a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_CCECHO; 448a0292f23SGarrett Wollman opt[optlen++] = TCPOLEN_CC; 449a0292f23SGarrett Wollman *(u_int32_t *)&opt[optlen] = 450a0292f23SGarrett Wollman htonl(tp->cc_recv); 451a0292f23SGarrett Wollman optlen += 4; 452a0292f23SGarrett Wollman } 453a0292f23SGarrett Wollman break; 454a0292f23SGarrett Wollman } 455a0292f23SGarrett Wollman } 456a0292f23SGarrett Wollman 457df8bae1dSRodney W. Grimes hdrlen += optlen; 458df8bae1dSRodney W. Grimes 459a04884fcSBill Fenner if (tp->t_inpcb->inp_options) { 460a04884fcSBill Fenner ipoptlen = tp->t_inpcb->inp_options->m_len - 461a04884fcSBill Fenner offsetof(struct ipoption, ipopt_list); 462a04884fcSBill Fenner } else { 463a04884fcSBill Fenner ipoptlen = 0; 464a04884fcSBill Fenner } 465a04884fcSBill Fenner 466df8bae1dSRodney W. Grimes /* 467df8bae1dSRodney W. Grimes * Adjust data length if insertion of options will 468a0292f23SGarrett Wollman * bump the packet length beyond the t_maxopd length. 469a0292f23SGarrett Wollman * Clear the FIN bit because we cut off the tail of 470a0292f23SGarrett Wollman * the segment. 471df8bae1dSRodney W. Grimes */ 472a04884fcSBill Fenner if (len + optlen + ipoptlen > tp->t_maxopd) { 473297a37f3SDavid Greenman /* 474297a37f3SDavid Greenman * If there is still more to send, don't close the connection. 475297a37f3SDavid Greenman */ 476297a37f3SDavid Greenman flags &= ~TH_FIN; 477a04884fcSBill Fenner len = tp->t_maxopd - optlen - ipoptlen; 478df8bae1dSRodney W. Grimes sendalot = 1; 479df8bae1dSRodney W. Grimes } 480df8bae1dSRodney W. Grimes 481a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/ 482df8bae1dSRodney W. Grimes if (max_linkhdr + hdrlen > MHLEN) 483df8bae1dSRodney W. Grimes panic("tcphdr too big"); 484a0292f23SGarrett Wollman /*#endif*/ 485df8bae1dSRodney W. Grimes 486df8bae1dSRodney W. Grimes /* 487df8bae1dSRodney W. Grimes * Grab a header mbuf, attaching a copy of data to 488df8bae1dSRodney W. Grimes * be transmitted, and initialize the header from 489df8bae1dSRodney W. Grimes * the template for sends on this connection. 490df8bae1dSRodney W. Grimes */ 491df8bae1dSRodney W. Grimes if (len) { 492df8bae1dSRodney W. Grimes if (tp->t_force && len == 1) 493df8bae1dSRodney W. Grimes tcpstat.tcps_sndprobe++; 494df8bae1dSRodney W. Grimes else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 495df8bae1dSRodney W. Grimes tcpstat.tcps_sndrexmitpack++; 496df8bae1dSRodney W. Grimes tcpstat.tcps_sndrexmitbyte += len; 497df8bae1dSRodney W. Grimes } else { 498df8bae1dSRodney W. Grimes tcpstat.tcps_sndpack++; 499df8bae1dSRodney W. Grimes tcpstat.tcps_sndbyte += len; 500df8bae1dSRodney W. Grimes } 501df8bae1dSRodney W. Grimes #ifdef notyet 502df8bae1dSRodney W. Grimes if ((m = m_copypack(so->so_snd.sb_mb, off, 503df8bae1dSRodney W. Grimes (int)len, max_linkhdr + hdrlen)) == 0) { 504df8bae1dSRodney W. Grimes error = ENOBUFS; 505df8bae1dSRodney W. Grimes goto out; 506df8bae1dSRodney W. Grimes } 507df8bae1dSRodney W. Grimes /* 508df8bae1dSRodney W. Grimes * m_copypack left space for our hdr; use it. 509df8bae1dSRodney W. Grimes */ 510df8bae1dSRodney W. Grimes m->m_len += hdrlen; 511df8bae1dSRodney W. Grimes m->m_data -= hdrlen; 512df8bae1dSRodney W. Grimes #else 513df8bae1dSRodney W. Grimes MGETHDR(m, M_DONTWAIT, MT_HEADER); 514df8bae1dSRodney W. Grimes if (m == NULL) { 515df8bae1dSRodney W. Grimes error = ENOBUFS; 516df8bae1dSRodney W. Grimes goto out; 517df8bae1dSRodney W. Grimes } 518df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 519df8bae1dSRodney W. Grimes m->m_len = hdrlen; 520df8bae1dSRodney W. Grimes if (len <= MHLEN - hdrlen - max_linkhdr) { 521df8bae1dSRodney W. Grimes m_copydata(so->so_snd.sb_mb, off, (int) len, 522df8bae1dSRodney W. Grimes mtod(m, caddr_t) + hdrlen); 523df8bae1dSRodney W. Grimes m->m_len += len; 524df8bae1dSRodney W. Grimes } else { 525df8bae1dSRodney W. Grimes m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 52651823c3aSGarrett Wollman if (m->m_next == 0) { 527d7f570e6SGarrett Wollman (void) m_free(m); 52851823c3aSGarrett Wollman error = ENOBUFS; 52951823c3aSGarrett Wollman goto out; 53051823c3aSGarrett Wollman } 531df8bae1dSRodney W. Grimes } 532df8bae1dSRodney W. Grimes #endif 533df8bae1dSRodney W. Grimes /* 534df8bae1dSRodney W. Grimes * If we're sending everything we've got, set PUSH. 535df8bae1dSRodney W. Grimes * (This will keep happy those implementations which only 536df8bae1dSRodney W. Grimes * give data to the user when a buffer fills or 537df8bae1dSRodney W. Grimes * a PUSH comes in.) 538df8bae1dSRodney W. Grimes */ 539df8bae1dSRodney W. Grimes if (off + len == so->so_snd.sb_cc) 540df8bae1dSRodney W. Grimes flags |= TH_PUSH; 541df8bae1dSRodney W. Grimes } else { 542df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 543df8bae1dSRodney W. Grimes tcpstat.tcps_sndacks++; 544df8bae1dSRodney W. Grimes else if (flags & (TH_SYN|TH_FIN|TH_RST)) 545df8bae1dSRodney W. Grimes tcpstat.tcps_sndctrl++; 546df8bae1dSRodney W. Grimes else if (SEQ_GT(tp->snd_up, tp->snd_una)) 547df8bae1dSRodney W. Grimes tcpstat.tcps_sndurg++; 548df8bae1dSRodney W. Grimes else 549df8bae1dSRodney W. Grimes tcpstat.tcps_sndwinup++; 550df8bae1dSRodney W. Grimes 551df8bae1dSRodney W. Grimes MGETHDR(m, M_DONTWAIT, MT_HEADER); 552df8bae1dSRodney W. Grimes if (m == NULL) { 553df8bae1dSRodney W. Grimes error = ENOBUFS; 554df8bae1dSRodney W. Grimes goto out; 555df8bae1dSRodney W. Grimes } 556df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 557df8bae1dSRodney W. Grimes m->m_len = hdrlen; 558df8bae1dSRodney W. Grimes } 559df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 560df8bae1dSRodney W. Grimes ti = mtod(m, struct tcpiphdr *); 561df8bae1dSRodney W. Grimes if (tp->t_template == 0) 562df8bae1dSRodney W. Grimes panic("tcp_output"); 56394a5d9b6SDavid Greenman (void)memcpy(ti, tp->t_template, sizeof (struct tcpiphdr)); 564df8bae1dSRodney W. Grimes 565df8bae1dSRodney W. Grimes /* 566df8bae1dSRodney W. Grimes * Fill in fields, remembering maximum advertised 567df8bae1dSRodney W. Grimes * window for use in delaying messages about window sizes. 568df8bae1dSRodney W. Grimes * If resending a FIN, be sure not to use a new sequence number. 569df8bae1dSRodney W. Grimes */ 570df8bae1dSRodney W. Grimes if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 571df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) 572df8bae1dSRodney W. Grimes tp->snd_nxt--; 573df8bae1dSRodney W. Grimes /* 574df8bae1dSRodney W. Grimes * If we are doing retransmissions, then snd_nxt will 575df8bae1dSRodney W. Grimes * not reflect the first unsent octet. For ACK only 576df8bae1dSRodney W. Grimes * packets, we do not want the sequence number of the 577df8bae1dSRodney W. Grimes * retransmitted packet, we want the sequence number 578df8bae1dSRodney W. Grimes * of the next unsent octet. So, if there is no data 579df8bae1dSRodney W. Grimes * (and no SYN or FIN), use snd_max instead of snd_nxt 580df8bae1dSRodney W. Grimes * when filling in ti_seq. But if we are in persist 581df8bae1dSRodney W. Grimes * state, snd_max might reflect one byte beyond the 582df8bae1dSRodney W. Grimes * right edge of the window, so use snd_nxt in that 583df8bae1dSRodney W. Grimes * case, since we know we aren't doing a retransmission. 584df8bae1dSRodney W. Grimes * (retransmit and persist are mutually exclusive...) 585df8bae1dSRodney W. Grimes */ 5869b8b58e0SJonathan Lemon if (len || (flags & (TH_SYN|TH_FIN)) 5879b8b58e0SJonathan Lemon || callout_active(tp->tt_persist)) 588df8bae1dSRodney W. Grimes ti->ti_seq = htonl(tp->snd_nxt); 589df8bae1dSRodney W. Grimes else 590df8bae1dSRodney W. Grimes ti->ti_seq = htonl(tp->snd_max); 591df8bae1dSRodney W. Grimes ti->ti_ack = htonl(tp->rcv_nxt); 592df8bae1dSRodney W. Grimes if (optlen) { 5930453d3cbSBruce Evans bcopy(opt, ti + 1, optlen); 594df8bae1dSRodney W. Grimes ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 595df8bae1dSRodney W. Grimes } 596df8bae1dSRodney W. Grimes ti->ti_flags = flags; 597df8bae1dSRodney W. Grimes /* 598df8bae1dSRodney W. Grimes * Calculate receive window. Don't shrink window, 599df8bae1dSRodney W. Grimes * but avoid silly window syndrome. 600df8bae1dSRodney W. Grimes */ 601df8bae1dSRodney W. Grimes if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) 602df8bae1dSRodney W. Grimes win = 0; 603df8bae1dSRodney W. Grimes if (win < (long)(tp->rcv_adv - tp->rcv_nxt)) 604df8bae1dSRodney W. Grimes win = (long)(tp->rcv_adv - tp->rcv_nxt); 605610a2e9cSBill Fenner if (win > (long)TCP_MAXWIN << tp->rcv_scale) 606610a2e9cSBill Fenner win = (long)TCP_MAXWIN << tp->rcv_scale; 607df8bae1dSRodney W. Grimes ti->ti_win = htons((u_short) (win>>tp->rcv_scale)); 608df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 609df8bae1dSRodney W. Grimes ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 610df8bae1dSRodney W. Grimes ti->ti_flags |= TH_URG; 611df8bae1dSRodney W. Grimes } else 612df8bae1dSRodney W. Grimes /* 613df8bae1dSRodney W. Grimes * If no urgent pointer to send, then we pull 614df8bae1dSRodney W. Grimes * the urgent pointer to the left edge of the send window 615df8bae1dSRodney W. Grimes * so that it doesn't drift into the send window on sequence 616df8bae1dSRodney W. Grimes * number wraparound. 617df8bae1dSRodney W. Grimes */ 618df8bae1dSRodney W. Grimes tp->snd_up = tp->snd_una; /* drag it along */ 619df8bae1dSRodney W. Grimes 620df8bae1dSRodney W. Grimes /* 621df8bae1dSRodney W. Grimes * Put TCP length in extended header, and then 622df8bae1dSRodney W. Grimes * checksum extended header and data. 623df8bae1dSRodney W. Grimes */ 624df8bae1dSRodney W. Grimes if (len + optlen) 625df8bae1dSRodney W. Grimes ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + 626df8bae1dSRodney W. Grimes optlen + len)); 627df8bae1dSRodney W. Grimes ti->ti_sum = in_cksum(m, (int)(hdrlen + len)); 628df8bae1dSRodney W. Grimes 629df8bae1dSRodney W. Grimes /* 630df8bae1dSRodney W. Grimes * In transmit state, time the transmission and arrange for 631df8bae1dSRodney W. Grimes * the retransmit. In persist state, just set snd_max. 632df8bae1dSRodney W. Grimes */ 6339b8b58e0SJonathan Lemon if (tp->t_force == 0 || !callout_active(tp->tt_persist)) { 634df8bae1dSRodney W. Grimes tcp_seq startseq = tp->snd_nxt; 635df8bae1dSRodney W. Grimes 636df8bae1dSRodney W. Grimes /* 637df8bae1dSRodney W. Grimes * Advance snd_nxt over sequence space of this segment. 638df8bae1dSRodney W. Grimes */ 639df8bae1dSRodney W. Grimes if (flags & (TH_SYN|TH_FIN)) { 640df8bae1dSRodney W. Grimes if (flags & TH_SYN) 641df8bae1dSRodney W. Grimes tp->snd_nxt++; 642df8bae1dSRodney W. Grimes if (flags & TH_FIN) { 643df8bae1dSRodney W. Grimes tp->snd_nxt++; 644df8bae1dSRodney W. Grimes tp->t_flags |= TF_SENTFIN; 645df8bae1dSRodney W. Grimes } 646df8bae1dSRodney W. Grimes } 647df8bae1dSRodney W. Grimes tp->snd_nxt += len; 648df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 649df8bae1dSRodney W. Grimes tp->snd_max = tp->snd_nxt; 650df8bae1dSRodney W. Grimes /* 651df8bae1dSRodney W. Grimes * Time this transmission if not a retransmission and 652df8bae1dSRodney W. Grimes * not currently timing anything. 653df8bae1dSRodney W. Grimes */ 6549b8b58e0SJonathan Lemon if (tp->t_rtttime == 0) { 6559b8b58e0SJonathan Lemon tp->t_rtttime = ticks; 656df8bae1dSRodney W. Grimes tp->t_rtseq = startseq; 657df8bae1dSRodney W. Grimes tcpstat.tcps_segstimed++; 658df8bae1dSRodney W. Grimes } 659df8bae1dSRodney W. Grimes } 660df8bae1dSRodney W. Grimes 661df8bae1dSRodney W. Grimes /* 662df8bae1dSRodney W. Grimes * Set retransmit timer if not currently set, 663df8bae1dSRodney W. Grimes * and not doing an ack or a keep-alive probe. 664df8bae1dSRodney W. Grimes * Initial value for retransmit timer is smoothed 665df8bae1dSRodney W. Grimes * round-trip time + 2 * round-trip time variance. 666df8bae1dSRodney W. Grimes * Initialize shift counter which is used for backoff 667df8bae1dSRodney W. Grimes * of retransmit time. 668df8bae1dSRodney W. Grimes */ 6699b8b58e0SJonathan Lemon if (!callout_active(tp->tt_rexmt) && 670df8bae1dSRodney W. Grimes tp->snd_nxt != tp->snd_una) { 6719b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 6729b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 6739b8b58e0SJonathan Lemon if (callout_active(tp->tt_persist)) { 6749b8b58e0SJonathan Lemon callout_stop(tp->tt_persist); 675df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 676df8bae1dSRodney W. Grimes } 677df8bae1dSRodney W. Grimes } 678df8bae1dSRodney W. Grimes } else 679df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 680df8bae1dSRodney W. Grimes tp->snd_max = tp->snd_nxt + len; 681df8bae1dSRodney W. Grimes 682610ee2f9SDavid Greenman #ifdef TCPDEBUG 683df8bae1dSRodney W. Grimes /* 684df8bae1dSRodney W. Grimes * Trace. 685df8bae1dSRodney W. Grimes */ 686df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) 687df8bae1dSRodney W. Grimes tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 688610ee2f9SDavid Greenman #endif 689df8bae1dSRodney W. Grimes 690df8bae1dSRodney W. Grimes /* 691df8bae1dSRodney W. Grimes * Fill in IP length and desired time to live and 692df8bae1dSRodney W. Grimes * send to IP level. There should be a better way 693df8bae1dSRodney W. Grimes * to handle ttl and tos; we could keep them in 694df8bae1dSRodney W. Grimes * the template, but need a way to checksum without them. 695df8bae1dSRodney W. Grimes */ 696df8bae1dSRodney W. Grimes m->m_pkthdr.len = hdrlen + len; 697df8bae1dSRodney W. Grimes { 698f138387aSGarrett Wollman struct rtentry *rt; 699df8bae1dSRodney W. Grimes ((struct ip *)ti)->ip_len = m->m_pkthdr.len; 700ca98b82cSDavid Greenman ((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip_ttl; /* XXX */ 701ca98b82cSDavid Greenman ((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip_tos; /* XXX */ 702f138387aSGarrett Wollman /* 703f138387aSGarrett Wollman * See if we should do MTU discovery. We do it only if the following 704f138387aSGarrett Wollman * are true: 705f138387aSGarrett Wollman * 1) we have a valid route to the destination 706f138387aSGarrett Wollman * 2) the MTU is not locked (if it is, then discovery has been 707f138387aSGarrett Wollman * disabled) 708f138387aSGarrett Wollman */ 7099a039a5fSDavid Greenman if (path_mtu_discovery 7109a039a5fSDavid Greenman && (rt = tp->t_inpcb->inp_route.ro_rt) 711f138387aSGarrett Wollman && rt->rt_flags & RTF_UP 712f138387aSGarrett Wollman && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { 713f138387aSGarrett Wollman ((struct ip *)ti)->ip_off |= IP_DF; 714f138387aSGarrett Wollman } 715df8bae1dSRodney W. Grimes error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 716df8bae1dSRodney W. Grimes so->so_options & SO_DONTROUTE, 0); 717df8bae1dSRodney W. Grimes } 718df8bae1dSRodney W. Grimes if (error) { 719df8bae1dSRodney W. Grimes out: 720df8bae1dSRodney W. Grimes if (error == ENOBUFS) { 721df8bae1dSRodney W. Grimes tcp_quench(tp->t_inpcb, 0); 722df8bae1dSRodney W. Grimes return (0); 723df8bae1dSRodney W. Grimes } 7243d1f141bSGarrett Wollman if (error == EMSGSIZE) { 7253d1f141bSGarrett Wollman /* 7263d1f141bSGarrett Wollman * ip_output() will have already fixed the route 7273d1f141bSGarrett Wollman * for us. tcp_mtudisc() will, as its last action, 7283d1f141bSGarrett Wollman * initiate retransmission, so it is important to 7293d1f141bSGarrett Wollman * not do so here. 7303d1f141bSGarrett Wollman */ 7313d1f141bSGarrett Wollman tcp_mtudisc(tp->t_inpcb, 0); 7323d1f141bSGarrett Wollman return 0; 7333d1f141bSGarrett Wollman } 734df8bae1dSRodney W. Grimes if ((error == EHOSTUNREACH || error == ENETDOWN) 735df8bae1dSRodney W. Grimes && TCPS_HAVERCVDSYN(tp->t_state)) { 736df8bae1dSRodney W. Grimes tp->t_softerror = error; 737df8bae1dSRodney W. Grimes return (0); 738df8bae1dSRodney W. Grimes } 739df8bae1dSRodney W. Grimes return (error); 740df8bae1dSRodney W. Grimes } 741df8bae1dSRodney W. Grimes tcpstat.tcps_sndtotal++; 742df8bae1dSRodney W. Grimes 743df8bae1dSRodney W. Grimes /* 744df8bae1dSRodney W. Grimes * Data sent (as far as we can tell). 745df8bae1dSRodney W. Grimes * If this advertises a larger window than any other segment, 746df8bae1dSRodney W. Grimes * then remember the size of the advertised window. 747df8bae1dSRodney W. Grimes * Any pending ACK has now been sent. 748df8bae1dSRodney W. Grimes */ 749df8bae1dSRodney W. Grimes if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 750df8bae1dSRodney W. Grimes tp->rcv_adv = tp->rcv_nxt + win; 751df8bae1dSRodney W. Grimes tp->last_ack_sent = tp->rcv_nxt; 7529b8b58e0SJonathan Lemon tp->t_flags &= ~TF_ACKNOW; 7539b8b58e0SJonathan Lemon if (tcp_delack_enabled) 7549b8b58e0SJonathan Lemon callout_stop(tp->tt_delack); 755df8bae1dSRodney W. Grimes if (sendalot) 756df8bae1dSRodney W. Grimes goto again; 757df8bae1dSRodney W. Grimes return (0); 758df8bae1dSRodney W. Grimes } 759df8bae1dSRodney W. Grimes 760df8bae1dSRodney W. Grimes void 761df8bae1dSRodney W. Grimes tcp_setpersist(tp) 762df8bae1dSRodney W. Grimes register struct tcpcb *tp; 763df8bae1dSRodney W. Grimes { 7649b8b58e0SJonathan Lemon int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 7659b8b58e0SJonathan Lemon int tt; 766df8bae1dSRodney W. Grimes 7679b8b58e0SJonathan Lemon if (callout_active(tp->tt_rexmt)) 7689b8b58e0SJonathan Lemon panic("tcp_setpersist: retransmit pending"); 769df8bae1dSRodney W. Grimes /* 770df8bae1dSRodney W. Grimes * Start/restart persistance timer. 771df8bae1dSRodney W. Grimes */ 7729b8b58e0SJonathan Lemon TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 773df8bae1dSRodney W. Grimes TCPTV_PERSMIN, TCPTV_PERSMAX); 7749b8b58e0SJonathan Lemon callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp); 775df8bae1dSRodney W. Grimes if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 776df8bae1dSRodney W. Grimes tp->t_rxtshift++; 777df8bae1dSRodney W. Grimes } 778