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 37fb59c426SYoshinobu Inoue #include "opt_inet6.h" 383a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 39c488362eSRobert Watson #include "opt_mac.h" 400cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 410cc12cc5SJoerg Wunsch 42df8bae1dSRodney W. Grimes #include <sys/param.h> 43df8bae1dSRodney W. Grimes #include <sys/systm.h> 44686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h> 45fb919e4dSMark Murray #include <sys/kernel.h> 46fb919e4dSMark Murray #include <sys/lock.h> 47c488362eSRobert Watson #include <sys/mac.h> 48fb919e4dSMark Murray #include <sys/mbuf.h> 49fb919e4dSMark Murray #include <sys/mutex.h> 50df8bae1dSRodney W. Grimes #include <sys/protosw.h> 51df8bae1dSRodney W. Grimes #include <sys/socket.h> 52df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 53fb919e4dSMark Murray #include <sys/sysctl.h> 54df8bae1dSRodney W. Grimes 55df8bae1dSRodney W. Grimes #include <net/route.h> 56df8bae1dSRodney W. Grimes 57df8bae1dSRodney W. Grimes #include <netinet/in.h> 58df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 59df8bae1dSRodney W. Grimes #include <netinet/ip.h> 60df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 61df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 62fb59c426SYoshinobu Inoue #ifdef INET6 63686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 64686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 65fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 66fb59c426SYoshinobu Inoue #endif 67df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 68df8bae1dSRodney W. Grimes #define TCPOUTFLAGS 69df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 70df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 71df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 72df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 73df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 74610ee2f9SDavid Greenman #ifdef TCPDEBUG 75df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 76610ee2f9SDavid Greenman #endif 77df8bae1dSRodney W. Grimes 783a2a9f79SYoshinobu Inoue #ifdef IPSEC 793a2a9f79SYoshinobu Inoue #include <netinet6/ipsec.h> 803a2a9f79SYoshinobu Inoue #endif /*IPSEC*/ 813a2a9f79SYoshinobu Inoue 82db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 83db4f9cc7SJonathan Lemon 84df8bae1dSRodney W. Grimes #ifdef notyet 85df8bae1dSRodney W. Grimes extern struct mbuf *m_copypack(); 86df8bae1dSRodney W. Grimes #endif 87df8bae1dSRodney W. Grimes 88eb5afebaSMike Silbersack int path_mtu_discovery = 1; 899a039a5fSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW, 909a039a5fSDavid Greenman &path_mtu_discovery, 1, "Enable Path MTU Discovery"); 919a039a5fSDavid Greenman 929b8b58e0SJonathan Lemon int ss_fltsz = 1; 939b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW, 949b8b58e0SJonathan Lemon &ss_fltsz, 1, "Slow start flight size"); 959b8b58e0SJonathan Lemon 963260eb18SMike Silbersack int ss_fltsz_local = 4; 979b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW, 989b8b58e0SJonathan Lemon &ss_fltsz_local, 1, "Slow start flight size for local networks"); 99df8bae1dSRodney W. Grimes 1007d200109SJayanth Vijayaraghavan int tcp_do_newreno = 1; 10146f58482SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno, 10246f58482SJonathan Lemon 0, "Enable NewReno Algorithms"); 103df8bae1dSRodney W. Grimes /* 104df8bae1dSRodney W. Grimes * Tcp output routine: figure out what should be sent and send it. 105df8bae1dSRodney W. Grimes */ 106df8bae1dSRodney W. Grimes int 107f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp) 108df8bae1dSRodney W. Grimes { 109f10e85d7SLuigi Rizzo struct socket *so = tp->t_inpcb->inp_socket; 110f10e85d7SLuigi Rizzo long len, win; 111df8bae1dSRodney W. Grimes int off, flags, error; 112f10e85d7SLuigi Rizzo struct mbuf *m; 113fb59c426SYoshinobu Inoue struct ip *ip = NULL; 114f10e85d7SLuigi Rizzo struct ipovly *ipov = NULL; 115f10e85d7SLuigi Rizzo struct tcphdr *th; 116a0292f23SGarrett Wollman u_char opt[TCP_MAXOLEN]; 117a04884fcSBill Fenner unsigned ipoptlen, optlen, hdrlen; 118df8bae1dSRodney W. Grimes int idle, sendalot; 119262c1c1aSMatthew Dillon #if 0 12046f58482SJonathan Lemon int maxburst = TCP_MAXBURST; 121262c1c1aSMatthew Dillon #endif 122a0292f23SGarrett Wollman struct rmxp_tao *taop; 123a0292f23SGarrett Wollman struct rmxp_tao tao_noncached; 124fb59c426SYoshinobu Inoue #ifdef INET6 125f10e85d7SLuigi Rizzo struct ip6_hdr *ip6 = NULL; 126fb59c426SYoshinobu Inoue int isipv6; 127fb59c426SYoshinobu Inoue 128fb59c426SYoshinobu Inoue isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 129fb59c426SYoshinobu Inoue #endif 130df8bae1dSRodney W. Grimes 1313d6ade3aSJennifer Yang mtx_assert(&tp->t_inpcb->inp_mtx, MA_OWNED); 1323d6ade3aSJennifer Yang 133df8bae1dSRodney W. Grimes /* 134df8bae1dSRodney W. Grimes * Determine length of data that should be transmitted, 135df8bae1dSRodney W. Grimes * and flags that will be used. 136df8bae1dSRodney W. Grimes * If there is some data or critical controls (SYN, RST) 137df8bae1dSRodney W. Grimes * to send, then transmit; otherwise, investigate further. 138df8bae1dSRodney W. Grimes */ 139c24d5daeSJayanth Vijayaraghavan idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 1409b8b58e0SJonathan Lemon if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) { 141df8bae1dSRodney W. Grimes /* 142df8bae1dSRodney W. Grimes * We have been idle for "a while" and no acks are 143df8bae1dSRodney W. Grimes * expected to clock out any data we send -- 144df8bae1dSRodney W. Grimes * slow start to get ack "clock" running again. 1459b8b58e0SJonathan Lemon * 1469b8b58e0SJonathan Lemon * Set the slow-start flight size depending on whether 1479b8b58e0SJonathan Lemon * this is a local network or not. 148df8bae1dSRodney W. Grimes */ 149f10e85d7SLuigi Rizzo int ss = ss_fltsz; 150fb59c426SYoshinobu Inoue #ifdef INET6 151f10e85d7SLuigi Rizzo if (isipv6) { 152f10e85d7SLuigi Rizzo if (in6_localaddr(&tp->t_inpcb->in6p_faddr)) 153f10e85d7SLuigi Rizzo ss = ss_fltsz_local; 154f10e85d7SLuigi Rizzo } else 155f10e85d7SLuigi Rizzo #endif /* INET6 */ 156f10e85d7SLuigi Rizzo if (in_localaddr(tp->t_inpcb->inp_faddr)) 157f10e85d7SLuigi Rizzo ss = ss_fltsz_local; 158f10e85d7SLuigi Rizzo tp->snd_cwnd = tp->t_maxseg * ss; 1599b8b58e0SJonathan Lemon } 160c24d5daeSJayanth Vijayaraghavan tp->t_flags &= ~TF_LASTIDLE; 161c24d5daeSJayanth Vijayaraghavan if (idle) { 162c24d5daeSJayanth Vijayaraghavan if (tp->t_flags & TF_MORETOCOME) { 163c24d5daeSJayanth Vijayaraghavan tp->t_flags |= TF_LASTIDLE; 164c24d5daeSJayanth Vijayaraghavan idle = 0; 165c24d5daeSJayanth Vijayaraghavan } 166c24d5daeSJayanth Vijayaraghavan } 167df8bae1dSRodney W. Grimes again: 168df8bae1dSRodney W. Grimes sendalot = 0; 169df8bae1dSRodney W. Grimes off = tp->snd_nxt - tp->snd_una; 170df8bae1dSRodney W. Grimes win = min(tp->snd_wnd, tp->snd_cwnd); 171df8bae1dSRodney W. Grimes 172df8bae1dSRodney W. Grimes flags = tcp_outflags[tp->t_state]; 173a0292f23SGarrett Wollman /* 174a0292f23SGarrett Wollman * Get standard flags, and add SYN or FIN if requested by 'hidden' 175a0292f23SGarrett Wollman * state flags. 176a0292f23SGarrett Wollman */ 177a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) 178a0292f23SGarrett Wollman flags |= TH_FIN; 179a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) 180a0292f23SGarrett Wollman flags |= TH_SYN; 181a0292f23SGarrett Wollman 182df8bae1dSRodney W. Grimes /* 183df8bae1dSRodney W. Grimes * If in persist timeout with window of 0, send 1 byte. 184df8bae1dSRodney W. Grimes * Otherwise, if window is small but nonzero 185df8bae1dSRodney W. Grimes * and timer expired, we will send what we can 186df8bae1dSRodney W. Grimes * and go to transmit state. 187df8bae1dSRodney W. Grimes */ 188df8bae1dSRodney W. Grimes if (tp->t_force) { 189df8bae1dSRodney W. Grimes if (win == 0) { 190df8bae1dSRodney W. Grimes /* 191df8bae1dSRodney W. Grimes * If we still have some data to send, then 192df8bae1dSRodney W. Grimes * clear the FIN bit. Usually this would 193df8bae1dSRodney W. Grimes * happen below when it realizes that we 194df8bae1dSRodney W. Grimes * aren't sending all the data. However, 19529089b51SJulian Elischer * if we have exactly 1 byte of unsent data, 196df8bae1dSRodney W. Grimes * then it won't clear the FIN bit below, 197df8bae1dSRodney W. Grimes * and if we are in persist state, we wind 198df8bae1dSRodney W. Grimes * up sending the packet without recording 199df8bae1dSRodney W. Grimes * that we sent the FIN bit. 200df8bae1dSRodney W. Grimes * 201df8bae1dSRodney W. Grimes * We can't just blindly clear the FIN bit, 202df8bae1dSRodney W. Grimes * because if we don't have any more data 203df8bae1dSRodney W. Grimes * to send then the probe will be the FIN 204df8bae1dSRodney W. Grimes * itself. 205df8bae1dSRodney W. Grimes */ 206df8bae1dSRodney W. Grimes if (off < so->so_snd.sb_cc) 207df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 208df8bae1dSRodney W. Grimes win = 1; 209df8bae1dSRodney W. Grimes } else { 2109b8b58e0SJonathan Lemon callout_stop(tp->tt_persist); 211df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 212df8bae1dSRodney W. Grimes } 213df8bae1dSRodney W. Grimes } 214df8bae1dSRodney W. Grimes 2159105bb46SBruce Evans len = (long)ulmin(so->so_snd.sb_cc, win) - off; 216df8bae1dSRodney W. Grimes 217be2ac88cSJonathan Lemon if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) { 218a0292f23SGarrett Wollman taop = &tao_noncached; 219a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 220a0292f23SGarrett Wollman } 221a0292f23SGarrett Wollman 222a0292f23SGarrett Wollman /* 223a0292f23SGarrett Wollman * Lop off SYN bit if it has already been sent. However, if this 224a0292f23SGarrett Wollman * is SYN-SENT state and if segment contains data and if we don't 225a0292f23SGarrett Wollman * know that foreign host supports TAO, suppress sending segment. 226a0292f23SGarrett Wollman */ 227a0292f23SGarrett Wollman if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 228a0292f23SGarrett Wollman flags &= ~TH_SYN; 229a0292f23SGarrett Wollman off--, len++; 230a0292f23SGarrett Wollman if (len > 0 && tp->t_state == TCPS_SYN_SENT && 231a0292f23SGarrett Wollman taop->tao_ccsent == 0) 232a0292f23SGarrett Wollman return 0; 233a0292f23SGarrett Wollman } 234a0292f23SGarrett Wollman 23581165e48SAndras Olah /* 23681165e48SAndras Olah * Be careful not to send data and/or FIN on SYN segments 23781165e48SAndras Olah * in cases when no CC option will be sent. 23881165e48SAndras Olah * This measure is needed to prevent interoperability problems 23981165e48SAndras Olah * with not fully conformant TCP implementations. 24081165e48SAndras Olah */ 24181165e48SAndras Olah if ((flags & TH_SYN) && 24281165e48SAndras Olah ((tp->t_flags & TF_NOOPT) || !(tp->t_flags & TF_REQ_CC) || 24381165e48SAndras Olah ((flags & TH_ACK) && !(tp->t_flags & TF_RCVD_CC)))) { 24481165e48SAndras Olah len = 0; 24581165e48SAndras Olah flags &= ~TH_FIN; 24681165e48SAndras Olah } 24781165e48SAndras Olah 248df8bae1dSRodney W. Grimes if (len < 0) { 249df8bae1dSRodney W. Grimes /* 250df8bae1dSRodney W. Grimes * If FIN has been sent but not acked, 251df8bae1dSRodney W. Grimes * but we haven't been called to retransmit, 252df8bae1dSRodney W. Grimes * len will be -1. Otherwise, window shrank 253df8bae1dSRodney W. Grimes * after we sent into it. If window shrank to 0, 2542d8266afSDavid Greenman * cancel pending retransmit, pull snd_nxt back 2552d8266afSDavid Greenman * to (closed) window, and set the persist timer 2562d8266afSDavid Greenman * if it isn't already going. If the window didn't 2572d8266afSDavid Greenman * close completely, just wait for an ACK. 258df8bae1dSRodney W. Grimes */ 259df8bae1dSRodney W. Grimes len = 0; 260df8bae1dSRodney W. Grimes if (win == 0) { 2619b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 2622d8266afSDavid Greenman tp->t_rxtshift = 0; 263df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2649b8b58e0SJonathan Lemon if (!callout_active(tp->tt_persist)) 2652d8266afSDavid Greenman tcp_setpersist(tp); 266df8bae1dSRodney W. Grimes } 267df8bae1dSRodney W. Grimes } 268df8bae1dSRodney W. Grimes if (len > tp->t_maxseg) { 269df8bae1dSRodney W. Grimes len = tp->t_maxseg; 270df8bae1dSRodney W. Grimes sendalot = 1; 271df8bae1dSRodney W. Grimes } 272df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 273df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 274df8bae1dSRodney W. Grimes 275df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 276df8bae1dSRodney W. Grimes 277df8bae1dSRodney W. Grimes /* 278262c1c1aSMatthew Dillon * Sender silly window avoidance. We transmit under the following 279262c1c1aSMatthew Dillon * conditions when len is non-zero: 280262c1c1aSMatthew Dillon * 281262c1c1aSMatthew Dillon * - We have a full segment 282262c1c1aSMatthew Dillon * - This is the last buffer in a write()/send() and we are 283262c1c1aSMatthew Dillon * either idle or running NODELAY 284262c1c1aSMatthew Dillon * - we've timed out (e.g. persist timer) 285262c1c1aSMatthew Dillon * - we have more then 1/2 the maximum send window's worth of 286262c1c1aSMatthew Dillon * data (receiver may be limited the window size) 287262c1c1aSMatthew Dillon * - we need to retransmit 288df8bae1dSRodney W. Grimes */ 289df8bae1dSRodney W. Grimes if (len) { 290df8bae1dSRodney W. Grimes if (len == tp->t_maxseg) 291df8bae1dSRodney W. Grimes goto send; 292262c1c1aSMatthew Dillon /* 293262c1c1aSMatthew Dillon * NOTE! on localhost connections an 'ack' from the remote 294262c1c1aSMatthew Dillon * end may occur synchronously with the output and cause 295262c1c1aSMatthew Dillon * us to flush a buffer queued with moretocome. XXX 296262c1c1aSMatthew Dillon * 297262c1c1aSMatthew Dillon * note: the len + off check is almost certainly unnecessary. 298262c1c1aSMatthew Dillon */ 299262c1c1aSMatthew Dillon if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 300262c1c1aSMatthew Dillon (idle || (tp->t_flags & TF_NODELAY)) && 301262c1c1aSMatthew Dillon len + off >= so->so_snd.sb_cc && 302262c1c1aSMatthew Dillon (tp->t_flags & TF_NOPUSH) == 0) { 303df8bae1dSRodney W. Grimes goto send; 304262c1c1aSMatthew Dillon } 305262c1c1aSMatthew Dillon if (tp->t_force) /* typ. timeout case */ 306df8bae1dSRodney W. Grimes goto send; 307a0292f23SGarrett Wollman if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 308df8bae1dSRodney W. Grimes goto send; 309262c1c1aSMatthew Dillon if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */ 310df8bae1dSRodney W. Grimes goto send; 311df8bae1dSRodney W. Grimes } 312df8bae1dSRodney W. Grimes 313df8bae1dSRodney W. Grimes /* 314df8bae1dSRodney W. Grimes * Compare available window to amount of window 315df8bae1dSRodney W. Grimes * known to peer (as advertised window less 316df8bae1dSRodney W. Grimes * next expected input). If the difference is at least two 317df8bae1dSRodney W. Grimes * max size segments, or at least 50% of the maximum possible 318df8bae1dSRodney W. Grimes * window, then want to send a window update to peer. 319df8bae1dSRodney W. Grimes */ 320df8bae1dSRodney W. Grimes if (win > 0) { 321df8bae1dSRodney W. Grimes /* 322df8bae1dSRodney W. Grimes * "adv" is the amount we can increase the window, 323df8bae1dSRodney W. Grimes * taking into account that we are limited by 324df8bae1dSRodney W. Grimes * TCP_MAXWIN << tp->rcv_scale. 325df8bae1dSRodney W. Grimes */ 326df8bae1dSRodney W. Grimes long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) - 327df8bae1dSRodney W. Grimes (tp->rcv_adv - tp->rcv_nxt); 328df8bae1dSRodney W. Grimes 329df8bae1dSRodney W. Grimes if (adv >= (long) (2 * tp->t_maxseg)) 330df8bae1dSRodney W. Grimes goto send; 331df8bae1dSRodney W. Grimes if (2 * adv >= (long) so->so_rcv.sb_hiwat) 332df8bae1dSRodney W. Grimes goto send; 333df8bae1dSRodney W. Grimes } 334df8bae1dSRodney W. Grimes 335df8bae1dSRodney W. Grimes /* 336df8bae1dSRodney W. Grimes * Send if we owe peer an ACK. 337df8bae1dSRodney W. Grimes */ 338df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 339df8bae1dSRodney W. Grimes goto send; 340a0292f23SGarrett Wollman if ((flags & TH_RST) || 341a0292f23SGarrett Wollman ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 342df8bae1dSRodney W. Grimes goto send; 343df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_una)) 344df8bae1dSRodney W. Grimes goto send; 345df8bae1dSRodney W. Grimes /* 346df8bae1dSRodney W. Grimes * If our state indicates that FIN should be sent 347df8bae1dSRodney W. Grimes * and we have not yet done so, or we're retransmitting the FIN, 348df8bae1dSRodney W. Grimes * then we need to send. 349df8bae1dSRodney W. Grimes */ 350df8bae1dSRodney W. Grimes if (flags & TH_FIN && 351df8bae1dSRodney W. Grimes ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 352df8bae1dSRodney W. Grimes goto send; 353df8bae1dSRodney W. Grimes 354df8bae1dSRodney W. Grimes /* 355df8bae1dSRodney W. Grimes * TCP window updates are not reliable, rather a polling protocol 356df8bae1dSRodney W. Grimes * using ``persist'' packets is used to insure receipt of window 357df8bae1dSRodney W. Grimes * updates. The three ``states'' for the output side are: 358df8bae1dSRodney W. Grimes * idle not doing retransmits or persists 359df8bae1dSRodney W. Grimes * persisting to move a small or zero window 360df8bae1dSRodney W. Grimes * (re)transmitting and thereby not persisting 361df8bae1dSRodney W. Grimes * 3629b8b58e0SJonathan Lemon * callout_active(tp->tt_persist) 3639b8b58e0SJonathan Lemon * is true when we are in persist state. 364df8bae1dSRodney W. Grimes * tp->t_force 365df8bae1dSRodney W. Grimes * is set when we are called to send a persist packet. 3669b8b58e0SJonathan Lemon * callout_active(tp->tt_rexmt) 367df8bae1dSRodney W. Grimes * is set when we are retransmitting 368df8bae1dSRodney W. Grimes * The output side is idle when both timers are zero. 369df8bae1dSRodney W. Grimes * 370df8bae1dSRodney W. Grimes * If send window is too small, there is data to transmit, and no 371df8bae1dSRodney W. Grimes * retransmit or persist is pending, then go to persist state. 372df8bae1dSRodney W. Grimes * If nothing happens soon, send when timer expires: 373df8bae1dSRodney W. Grimes * if window is nonzero, transmit what we can, 374df8bae1dSRodney W. Grimes * otherwise force out a byte. 375df8bae1dSRodney W. Grimes */ 3769b8b58e0SJonathan Lemon if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) && 3779b8b58e0SJonathan Lemon !callout_active(tp->tt_persist)) { 378df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 379df8bae1dSRodney W. Grimes tcp_setpersist(tp); 380df8bae1dSRodney W. Grimes } 381df8bae1dSRodney W. Grimes 382df8bae1dSRodney W. Grimes /* 383df8bae1dSRodney W. Grimes * No reason to send a segment, just return. 384df8bae1dSRodney W. Grimes */ 385df8bae1dSRodney W. Grimes return (0); 386df8bae1dSRodney W. Grimes 387df8bae1dSRodney W. Grimes send: 388df8bae1dSRodney W. Grimes /* 389df8bae1dSRodney W. Grimes * Before ESTABLISHED, force sending of initial options 390df8bae1dSRodney W. Grimes * unless TCP set not to do any options. 391df8bae1dSRodney W. Grimes * NOTE: we assume that the IP/TCP header plus TCP options 392df8bae1dSRodney W. Grimes * always fit in a single mbuf, leaving room for a maximum 393df8bae1dSRodney W. Grimes * link header, i.e. 39433841545SHajimu UMEMOTO * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 395df8bae1dSRodney W. Grimes */ 396df8bae1dSRodney W. Grimes optlen = 0; 397fb59c426SYoshinobu Inoue #ifdef INET6 398fb59c426SYoshinobu Inoue if (isipv6) 399fb59c426SYoshinobu Inoue hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 400fb59c426SYoshinobu Inoue else 401fb59c426SYoshinobu Inoue #endif 402df8bae1dSRodney W. Grimes hdrlen = sizeof (struct tcpiphdr); 403df8bae1dSRodney W. Grimes if (flags & TH_SYN) { 404df8bae1dSRodney W. Grimes tp->snd_nxt = tp->iss; 405df8bae1dSRodney W. Grimes if ((tp->t_flags & TF_NOOPT) == 0) { 406df8bae1dSRodney W. Grimes u_short mss; 407df8bae1dSRodney W. Grimes 408df8bae1dSRodney W. Grimes opt[0] = TCPOPT_MAXSEG; 409a0292f23SGarrett Wollman opt[1] = TCPOLEN_MAXSEG; 410a0292f23SGarrett Wollman mss = htons((u_short) tcp_mssopt(tp)); 41194a5d9b6SDavid Greenman (void)memcpy(opt + 2, &mss, sizeof(mss)); 412a0292f23SGarrett Wollman optlen = TCPOLEN_MAXSEG; 413df8bae1dSRodney W. Grimes 414df8bae1dSRodney W. Grimes if ((tp->t_flags & TF_REQ_SCALE) && 415df8bae1dSRodney W. Grimes ((flags & TH_ACK) == 0 || 416df8bae1dSRodney W. Grimes (tp->t_flags & TF_RCVD_SCALE))) { 4179105bb46SBruce Evans *((u_int32_t *)(opt + optlen)) = htonl( 418df8bae1dSRodney W. Grimes TCPOPT_NOP << 24 | 419df8bae1dSRodney W. Grimes TCPOPT_WINDOW << 16 | 420df8bae1dSRodney W. Grimes TCPOLEN_WINDOW << 8 | 421df8bae1dSRodney W. Grimes tp->request_r_scale); 422df8bae1dSRodney W. Grimes optlen += 4; 423df8bae1dSRodney W. Grimes } 424df8bae1dSRodney W. Grimes } 425df8bae1dSRodney W. Grimes } 426df8bae1dSRodney W. Grimes 427df8bae1dSRodney W. Grimes /* 428df8bae1dSRodney W. Grimes * Send a timestamp and echo-reply if this is a SYN and our side 429df8bae1dSRodney W. Grimes * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 430df8bae1dSRodney W. Grimes * and our peer have sent timestamps in our SYN's. 431df8bae1dSRodney W. Grimes */ 432df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 433df8bae1dSRodney W. Grimes (flags & TH_RST) == 0 && 434a0292f23SGarrett Wollman ((flags & TH_ACK) == 0 || 435df8bae1dSRodney W. Grimes (tp->t_flags & TF_RCVD_TSTMP))) { 4369105bb46SBruce Evans u_int32_t *lp = (u_int32_t *)(opt + optlen); 437df8bae1dSRodney W. Grimes 438df8bae1dSRodney W. Grimes /* Form timestamp option as shown in appendix A of RFC 1323. */ 439df8bae1dSRodney W. Grimes *lp++ = htonl(TCPOPT_TSTAMP_HDR); 4409b8b58e0SJonathan Lemon *lp++ = htonl(ticks); 441df8bae1dSRodney W. Grimes *lp = htonl(tp->ts_recent); 442df8bae1dSRodney W. Grimes optlen += TCPOLEN_TSTAMP_APPA; 443df8bae1dSRodney W. Grimes } 444df8bae1dSRodney W. Grimes 445a0292f23SGarrett Wollman /* 446a0292f23SGarrett Wollman * Send `CC-family' options if our side wants to use them (TF_REQ_CC), 447a0292f23SGarrett Wollman * options are allowed (!TF_NOOPT) and it's not a RST. 448a0292f23SGarrett Wollman */ 449a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 450a0292f23SGarrett Wollman (flags & TH_RST) == 0) { 451a0292f23SGarrett Wollman switch (flags & (TH_SYN|TH_ACK)) { 452a0292f23SGarrett Wollman /* 453a0292f23SGarrett Wollman * This is a normal ACK, send CC if we received CC before 454a0292f23SGarrett Wollman * from our peer. 455a0292f23SGarrett Wollman */ 456a0292f23SGarrett Wollman case TH_ACK: 457a0292f23SGarrett Wollman if (!(tp->t_flags & TF_RCVD_CC)) 458a0292f23SGarrett Wollman break; 459a0292f23SGarrett Wollman /*FALLTHROUGH*/ 460a0292f23SGarrett Wollman 461a0292f23SGarrett Wollman /* 462a0292f23SGarrett Wollman * We can only get here in T/TCP's SYN_SENT* state, when 463a0292f23SGarrett Wollman * we're a sending a non-SYN segment without waiting for 464a0292f23SGarrett Wollman * the ACK of our SYN. A check above assures that we only 465a0292f23SGarrett Wollman * do this if our peer understands T/TCP. 466a0292f23SGarrett Wollman */ 467a0292f23SGarrett Wollman case 0: 468a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 469a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 470a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_CC; 471a0292f23SGarrett Wollman opt[optlen++] = TCPOLEN_CC; 472a0292f23SGarrett Wollman *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send); 473a0292f23SGarrett Wollman 474a0292f23SGarrett Wollman optlen += 4; 475a0292f23SGarrett Wollman break; 476a0292f23SGarrett Wollman 477a0292f23SGarrett Wollman /* 478a0292f23SGarrett Wollman * This is our initial SYN, check whether we have to use 479a0292f23SGarrett Wollman * CC or CC.new. 480a0292f23SGarrett Wollman */ 481a0292f23SGarrett Wollman case TH_SYN: 482a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 483a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 484a45d2726SAndras Olah opt[optlen++] = tp->t_flags & TF_SENDCCNEW ? 485a45d2726SAndras Olah TCPOPT_CCNEW : TCPOPT_CC; 486a0292f23SGarrett Wollman opt[optlen++] = TCPOLEN_CC; 487a0292f23SGarrett Wollman *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send); 488a0292f23SGarrett Wollman optlen += 4; 489a0292f23SGarrett Wollman break; 490a0292f23SGarrett Wollman 491a0292f23SGarrett Wollman /* 492a0292f23SGarrett Wollman * This is a SYN,ACK; send CC and CC.echo if we received 493a0292f23SGarrett Wollman * CC from our peer. 494a0292f23SGarrett Wollman */ 495a0292f23SGarrett Wollman case (TH_SYN|TH_ACK): 496a0292f23SGarrett Wollman if (tp->t_flags & TF_RCVD_CC) { 497a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 498a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 499a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_CC; 500a0292f23SGarrett Wollman opt[optlen++] = TCPOLEN_CC; 501a0292f23SGarrett Wollman *(u_int32_t *)&opt[optlen] = 502a0292f23SGarrett Wollman htonl(tp->cc_send); 503a0292f23SGarrett Wollman optlen += 4; 504a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 505a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 506a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_CCECHO; 507a0292f23SGarrett Wollman opt[optlen++] = TCPOLEN_CC; 508a0292f23SGarrett Wollman *(u_int32_t *)&opt[optlen] = 509a0292f23SGarrett Wollman htonl(tp->cc_recv); 510a0292f23SGarrett Wollman optlen += 4; 511a0292f23SGarrett Wollman } 512a0292f23SGarrett Wollman break; 513a0292f23SGarrett Wollman } 514a0292f23SGarrett Wollman } 515a0292f23SGarrett Wollman 516df8bae1dSRodney W. Grimes hdrlen += optlen; 517df8bae1dSRodney W. Grimes 518fb59c426SYoshinobu Inoue #ifdef INET6 519fb59c426SYoshinobu Inoue if (isipv6) 520fb59c426SYoshinobu Inoue ipoptlen = ip6_optlen(tp->t_inpcb); 521fb59c426SYoshinobu Inoue else 522fb59c426SYoshinobu Inoue #endif 523f10e85d7SLuigi Rizzo if (tp->t_inpcb->inp_options) 524a04884fcSBill Fenner ipoptlen = tp->t_inpcb->inp_options->m_len - 525a04884fcSBill Fenner offsetof(struct ipoption, ipopt_list); 526f10e85d7SLuigi Rizzo else 527a04884fcSBill Fenner ipoptlen = 0; 528fb59c426SYoshinobu Inoue #ifdef IPSEC 529fb59c426SYoshinobu Inoue ipoptlen += ipsec_hdrsiz_tcp(tp); 530fb59c426SYoshinobu Inoue #endif 531a04884fcSBill Fenner 532df8bae1dSRodney W. Grimes /* 533df8bae1dSRodney W. Grimes * Adjust data length if insertion of options will 534a0292f23SGarrett Wollman * bump the packet length beyond the t_maxopd length. 535a0292f23SGarrett Wollman * Clear the FIN bit because we cut off the tail of 536a0292f23SGarrett Wollman * the segment. 537df8bae1dSRodney W. Grimes */ 538a04884fcSBill Fenner if (len + optlen + ipoptlen > tp->t_maxopd) { 539297a37f3SDavid Greenman /* 540297a37f3SDavid Greenman * If there is still more to send, don't close the connection. 541297a37f3SDavid Greenman */ 542297a37f3SDavid Greenman flags &= ~TH_FIN; 543a04884fcSBill Fenner len = tp->t_maxopd - optlen - ipoptlen; 544df8bae1dSRodney W. Grimes sendalot = 1; 545df8bae1dSRodney W. Grimes } 546df8bae1dSRodney W. Grimes 547a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/ 548a683a7ddSYoshinobu Inoue #ifdef INET6 549a683a7ddSYoshinobu Inoue if (max_linkhdr + hdrlen > MCLBYTES) 550a683a7ddSYoshinobu Inoue #else 551df8bae1dSRodney W. Grimes if (max_linkhdr + hdrlen > MHLEN) 552a683a7ddSYoshinobu Inoue #endif 553f10e85d7SLuigi Rizzo panic("tcphdr too big"); 554a0292f23SGarrett Wollman /*#endif*/ 555df8bae1dSRodney W. Grimes 556df8bae1dSRodney W. Grimes /* 557df8bae1dSRodney W. Grimes * Grab a header mbuf, attaching a copy of data to 558df8bae1dSRodney W. Grimes * be transmitted, and initialize the header from 559df8bae1dSRodney W. Grimes * the template for sends on this connection. 560df8bae1dSRodney W. Grimes */ 561df8bae1dSRodney W. Grimes if (len) { 562df8bae1dSRodney W. Grimes if (tp->t_force && len == 1) 563df8bae1dSRodney W. Grimes tcpstat.tcps_sndprobe++; 564df8bae1dSRodney W. Grimes else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 565df8bae1dSRodney W. Grimes tcpstat.tcps_sndrexmitpack++; 566df8bae1dSRodney W. Grimes tcpstat.tcps_sndrexmitbyte += len; 567df8bae1dSRodney W. Grimes } else { 568df8bae1dSRodney W. Grimes tcpstat.tcps_sndpack++; 569df8bae1dSRodney W. Grimes tcpstat.tcps_sndbyte += len; 570df8bae1dSRodney W. Grimes } 571df8bae1dSRodney W. Grimes #ifdef notyet 572df8bae1dSRodney W. Grimes if ((m = m_copypack(so->so_snd.sb_mb, off, 573df8bae1dSRodney W. Grimes (int)len, max_linkhdr + hdrlen)) == 0) { 574df8bae1dSRodney W. Grimes error = ENOBUFS; 575df8bae1dSRodney W. Grimes goto out; 576df8bae1dSRodney W. Grimes } 577df8bae1dSRodney W. Grimes /* 578df8bae1dSRodney W. Grimes * m_copypack left space for our hdr; use it. 579df8bae1dSRodney W. Grimes */ 580df8bae1dSRodney W. Grimes m->m_len += hdrlen; 581df8bae1dSRodney W. Grimes m->m_data -= hdrlen; 582df8bae1dSRodney W. Grimes #else 583df8bae1dSRodney W. Grimes MGETHDR(m, M_DONTWAIT, MT_HEADER); 584df8bae1dSRodney W. Grimes if (m == NULL) { 585df8bae1dSRodney W. Grimes error = ENOBUFS; 586df8bae1dSRodney W. Grimes goto out; 587df8bae1dSRodney W. Grimes } 588fb59c426SYoshinobu Inoue #ifdef INET6 589a683a7ddSYoshinobu Inoue if (MHLEN < hdrlen + max_linkhdr) { 590a683a7ddSYoshinobu Inoue MCLGET(m, M_DONTWAIT); 591a683a7ddSYoshinobu Inoue if ((m->m_flags & M_EXT) == 0) { 592a683a7ddSYoshinobu Inoue m_freem(m); 593a683a7ddSYoshinobu Inoue error = ENOBUFS; 594a683a7ddSYoshinobu Inoue goto out; 595a683a7ddSYoshinobu Inoue } 596a683a7ddSYoshinobu Inoue } 597fb59c426SYoshinobu Inoue #endif 598df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 599df8bae1dSRodney W. Grimes m->m_len = hdrlen; 600df8bae1dSRodney W. Grimes if (len <= MHLEN - hdrlen - max_linkhdr) { 601df8bae1dSRodney W. Grimes m_copydata(so->so_snd.sb_mb, off, (int) len, 602df8bae1dSRodney W. Grimes mtod(m, caddr_t) + hdrlen); 603df8bae1dSRodney W. Grimes m->m_len += len; 604df8bae1dSRodney W. Grimes } else { 605df8bae1dSRodney W. Grimes m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 60651823c3aSGarrett Wollman if (m->m_next == 0) { 607d7f570e6SGarrett Wollman (void) m_free(m); 60851823c3aSGarrett Wollman error = ENOBUFS; 60951823c3aSGarrett Wollman goto out; 61051823c3aSGarrett Wollman } 611df8bae1dSRodney W. Grimes } 612df8bae1dSRodney W. Grimes #endif 613df8bae1dSRodney W. Grimes /* 614df8bae1dSRodney W. Grimes * If we're sending everything we've got, set PUSH. 615df8bae1dSRodney W. Grimes * (This will keep happy those implementations which only 616df8bae1dSRodney W. Grimes * give data to the user when a buffer fills or 617df8bae1dSRodney W. Grimes * a PUSH comes in.) 618df8bae1dSRodney W. Grimes */ 619df8bae1dSRodney W. Grimes if (off + len == so->so_snd.sb_cc) 620df8bae1dSRodney W. Grimes flags |= TH_PUSH; 621df8bae1dSRodney W. Grimes } else { 622df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 623df8bae1dSRodney W. Grimes tcpstat.tcps_sndacks++; 624df8bae1dSRodney W. Grimes else if (flags & (TH_SYN|TH_FIN|TH_RST)) 625df8bae1dSRodney W. Grimes tcpstat.tcps_sndctrl++; 626df8bae1dSRodney W. Grimes else if (SEQ_GT(tp->snd_up, tp->snd_una)) 627df8bae1dSRodney W. Grimes tcpstat.tcps_sndurg++; 628df8bae1dSRodney W. Grimes else 629df8bae1dSRodney W. Grimes tcpstat.tcps_sndwinup++; 630df8bae1dSRodney W. Grimes 631df8bae1dSRodney W. Grimes MGETHDR(m, M_DONTWAIT, MT_HEADER); 632df8bae1dSRodney W. Grimes if (m == NULL) { 633df8bae1dSRodney W. Grimes error = ENOBUFS; 634df8bae1dSRodney W. Grimes goto out; 635df8bae1dSRodney W. Grimes } 636fb59c426SYoshinobu Inoue #ifdef INET6 637fb59c426SYoshinobu Inoue if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 638fb59c426SYoshinobu Inoue MHLEN >= hdrlen) { 639fb59c426SYoshinobu Inoue MH_ALIGN(m, hdrlen); 640fb59c426SYoshinobu Inoue } else 641fb59c426SYoshinobu Inoue #endif 642df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 643df8bae1dSRodney W. Grimes m->m_len = hdrlen; 644df8bae1dSRodney W. Grimes } 645df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 646c488362eSRobert Watson #ifdef MAC 647c488362eSRobert Watson mac_create_mbuf_from_socket(so, m); 648c488362eSRobert Watson #endif 649fb59c426SYoshinobu Inoue #ifdef INET6 650fb59c426SYoshinobu Inoue if (isipv6) { 651fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 652fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip6 + 1); 65308517d53SMike Silbersack tcp_fillheaders(tp, ip6, th); 654fb59c426SYoshinobu Inoue } else 655fb59c426SYoshinobu Inoue #endif /* INET6 */ 656fb59c426SYoshinobu Inoue { 657fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 658fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 659fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip + 1); 660db4f9cc7SJonathan Lemon /* this picks up the pseudo header (w/o the length) */ 66108517d53SMike Silbersack tcp_fillheaders(tp, ip, th); 662fb59c426SYoshinobu Inoue } 663df8bae1dSRodney W. Grimes 664df8bae1dSRodney W. Grimes /* 665df8bae1dSRodney W. Grimes * Fill in fields, remembering maximum advertised 666df8bae1dSRodney W. Grimes * window for use in delaying messages about window sizes. 667df8bae1dSRodney W. Grimes * If resending a FIN, be sure not to use a new sequence number. 668df8bae1dSRodney W. Grimes */ 669df8bae1dSRodney W. Grimes if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 670df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) 671df8bae1dSRodney W. Grimes tp->snd_nxt--; 672df8bae1dSRodney W. Grimes /* 673df8bae1dSRodney W. Grimes * If we are doing retransmissions, then snd_nxt will 674df8bae1dSRodney W. Grimes * not reflect the first unsent octet. For ACK only 675df8bae1dSRodney W. Grimes * packets, we do not want the sequence number of the 676df8bae1dSRodney W. Grimes * retransmitted packet, we want the sequence number 677df8bae1dSRodney W. Grimes * of the next unsent octet. So, if there is no data 678df8bae1dSRodney W. Grimes * (and no SYN or FIN), use snd_max instead of snd_nxt 679df8bae1dSRodney W. Grimes * when filling in ti_seq. But if we are in persist 680df8bae1dSRodney W. Grimes * state, snd_max might reflect one byte beyond the 681df8bae1dSRodney W. Grimes * right edge of the window, so use snd_nxt in that 682df8bae1dSRodney W. Grimes * case, since we know we aren't doing a retransmission. 683df8bae1dSRodney W. Grimes * (retransmit and persist are mutually exclusive...) 684df8bae1dSRodney W. Grimes */ 6859b8b58e0SJonathan Lemon if (len || (flags & (TH_SYN|TH_FIN)) 6869b8b58e0SJonathan Lemon || callout_active(tp->tt_persist)) 687fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_nxt); 688df8bae1dSRodney W. Grimes else 689fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_max); 690fb59c426SYoshinobu Inoue th->th_ack = htonl(tp->rcv_nxt); 691df8bae1dSRodney W. Grimes if (optlen) { 692fb59c426SYoshinobu Inoue bcopy(opt, th + 1, optlen); 693fb59c426SYoshinobu Inoue th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 694df8bae1dSRodney W. Grimes } 695fb59c426SYoshinobu Inoue th->th_flags = flags; 696df8bae1dSRodney W. Grimes /* 697df8bae1dSRodney W. Grimes * Calculate receive window. Don't shrink window, 698df8bae1dSRodney W. Grimes * but avoid silly window syndrome. 699df8bae1dSRodney W. Grimes */ 700df8bae1dSRodney W. Grimes if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) 701df8bae1dSRodney W. Grimes win = 0; 702df8bae1dSRodney W. Grimes if (win < (long)(tp->rcv_adv - tp->rcv_nxt)) 703df8bae1dSRodney W. Grimes win = (long)(tp->rcv_adv - tp->rcv_nxt); 704610a2e9cSBill Fenner if (win > (long)TCP_MAXWIN << tp->rcv_scale) 705610a2e9cSBill Fenner win = (long)TCP_MAXWIN << tp->rcv_scale; 706fb59c426SYoshinobu Inoue th->th_win = htons((u_short) (win>>tp->rcv_scale)); 707262c1c1aSMatthew Dillon 708262c1c1aSMatthew Dillon 709262c1c1aSMatthew Dillon /* 710262c1c1aSMatthew Dillon * Adjust the RXWIN0SENT flag - indicate that we have advertised 711262c1c1aSMatthew Dillon * a 0 window. This may cause the remote transmitter to stall. This 712262c1c1aSMatthew Dillon * flag tells soreceive() to disable delayed acknowledgements when 713262c1c1aSMatthew Dillon * draining the buffer. This can occur if the receiver is attempting 714262c1c1aSMatthew Dillon * to read more data then can be buffered prior to transmitting on 715262c1c1aSMatthew Dillon * the connection. 716262c1c1aSMatthew Dillon */ 717262c1c1aSMatthew Dillon if (win == 0) 718262c1c1aSMatthew Dillon tp->t_flags |= TF_RXWIN0SENT; 719262c1c1aSMatthew Dillon else 720262c1c1aSMatthew Dillon tp->t_flags &= ~TF_RXWIN0SENT; 721df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 722fb59c426SYoshinobu Inoue th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 723fb59c426SYoshinobu Inoue th->th_flags |= TH_URG; 724df8bae1dSRodney W. Grimes } else 725df8bae1dSRodney W. Grimes /* 726df8bae1dSRodney W. Grimes * If no urgent pointer to send, then we pull 727df8bae1dSRodney W. Grimes * the urgent pointer to the left edge of the send window 728df8bae1dSRodney W. Grimes * so that it doesn't drift into the send window on sequence 729df8bae1dSRodney W. Grimes * number wraparound. 730df8bae1dSRodney W. Grimes */ 731df8bae1dSRodney W. Grimes tp->snd_up = tp->snd_una; /* drag it along */ 732df8bae1dSRodney W. Grimes 733df8bae1dSRodney W. Grimes /* 734df8bae1dSRodney W. Grimes * Put TCP length in extended header, and then 735df8bae1dSRodney W. Grimes * checksum extended header and data. 736df8bae1dSRodney W. Grimes */ 737fb59c426SYoshinobu Inoue m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 738fb59c426SYoshinobu Inoue #ifdef INET6 739fb59c426SYoshinobu Inoue if (isipv6) 740fb59c426SYoshinobu Inoue /* 741fb59c426SYoshinobu Inoue * ip6_plen is not need to be filled now, and will be filled 742fb59c426SYoshinobu Inoue * in ip6_output. 743fb59c426SYoshinobu Inoue */ 744fb59c426SYoshinobu Inoue th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 745fb59c426SYoshinobu Inoue sizeof(struct tcphdr) + optlen + len); 746fb59c426SYoshinobu Inoue else 747fb59c426SYoshinobu Inoue #endif /* INET6 */ 748fb59c426SYoshinobu Inoue { 749db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_TCP; 750db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 751df8bae1dSRodney W. Grimes if (len + optlen) 752db4f9cc7SJonathan Lemon th->th_sum = in_addword(th->th_sum, 753db4f9cc7SJonathan Lemon htons((u_short)(optlen + len))); 754fb59c426SYoshinobu Inoue 755db4f9cc7SJonathan Lemon /* IP version must be set here for ipv4/ipv6 checking later */ 756db4f9cc7SJonathan Lemon KASSERT(ip->ip_v == IPVERSION, 7576e551fb6SDavid E. O'Brien ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 758fb59c426SYoshinobu Inoue } 759df8bae1dSRodney W. Grimes 760df8bae1dSRodney W. Grimes /* 761df8bae1dSRodney W. Grimes * In transmit state, time the transmission and arrange for 762df8bae1dSRodney W. Grimes * the retransmit. In persist state, just set snd_max. 763df8bae1dSRodney W. Grimes */ 7649b8b58e0SJonathan Lemon if (tp->t_force == 0 || !callout_active(tp->tt_persist)) { 765df8bae1dSRodney W. Grimes tcp_seq startseq = tp->snd_nxt; 766df8bae1dSRodney W. Grimes 767df8bae1dSRodney W. Grimes /* 768df8bae1dSRodney W. Grimes * Advance snd_nxt over sequence space of this segment. 769df8bae1dSRodney W. Grimes */ 770df8bae1dSRodney W. Grimes if (flags & (TH_SYN|TH_FIN)) { 771df8bae1dSRodney W. Grimes if (flags & TH_SYN) 772df8bae1dSRodney W. Grimes tp->snd_nxt++; 773df8bae1dSRodney W. Grimes if (flags & TH_FIN) { 774df8bae1dSRodney W. Grimes tp->snd_nxt++; 775df8bae1dSRodney W. Grimes tp->t_flags |= TF_SENTFIN; 776df8bae1dSRodney W. Grimes } 777df8bae1dSRodney W. Grimes } 778df8bae1dSRodney W. Grimes tp->snd_nxt += len; 779df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 780df8bae1dSRodney W. Grimes tp->snd_max = tp->snd_nxt; 781df8bae1dSRodney W. Grimes /* 782df8bae1dSRodney W. Grimes * Time this transmission if not a retransmission and 783df8bae1dSRodney W. Grimes * not currently timing anything. 784df8bae1dSRodney W. Grimes */ 7859b8b58e0SJonathan Lemon if (tp->t_rtttime == 0) { 7869b8b58e0SJonathan Lemon tp->t_rtttime = ticks; 787df8bae1dSRodney W. Grimes tp->t_rtseq = startseq; 788df8bae1dSRodney W. Grimes tcpstat.tcps_segstimed++; 789df8bae1dSRodney W. Grimes } 790df8bae1dSRodney W. Grimes } 791df8bae1dSRodney W. Grimes 792df8bae1dSRodney W. Grimes /* 793df8bae1dSRodney W. Grimes * Set retransmit timer if not currently set, 794df8bae1dSRodney W. Grimes * and not doing an ack or a keep-alive probe. 795df8bae1dSRodney W. Grimes * Initial value for retransmit timer is smoothed 796df8bae1dSRodney W. Grimes * round-trip time + 2 * round-trip time variance. 797df8bae1dSRodney W. Grimes * Initialize shift counter which is used for backoff 798df8bae1dSRodney W. Grimes * of retransmit time. 799df8bae1dSRodney W. Grimes */ 8009b8b58e0SJonathan Lemon if (!callout_active(tp->tt_rexmt) && 801df8bae1dSRodney W. Grimes tp->snd_nxt != tp->snd_una) { 8029b8b58e0SJonathan Lemon if (callout_active(tp->tt_persist)) { 8039b8b58e0SJonathan Lemon callout_stop(tp->tt_persist); 804df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 805df8bae1dSRodney W. Grimes } 80646f58482SJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 80746f58482SJonathan Lemon tcp_timer_rexmt, tp); 808df8bae1dSRodney W. Grimes } 809df8bae1dSRodney W. Grimes } else 810df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 811df8bae1dSRodney W. Grimes tp->snd_max = tp->snd_nxt + len; 812df8bae1dSRodney W. Grimes 813610ee2f9SDavid Greenman #ifdef TCPDEBUG 814df8bae1dSRodney W. Grimes /* 815df8bae1dSRodney W. Grimes * Trace. 816df8bae1dSRodney W. Grimes */ 8174cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 818fb59c426SYoshinobu Inoue tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 819610ee2f9SDavid Greenman #endif 820df8bae1dSRodney W. Grimes 821df8bae1dSRodney W. Grimes /* 822df8bae1dSRodney W. Grimes * Fill in IP length and desired time to live and 823df8bae1dSRodney W. Grimes * send to IP level. There should be a better way 824df8bae1dSRodney W. Grimes * to handle ttl and tos; we could keep them in 825df8bae1dSRodney W. Grimes * the template, but need a way to checksum without them. 826df8bae1dSRodney W. Grimes */ 827fb59c426SYoshinobu Inoue /* 828fb59c426SYoshinobu Inoue * m->m_pkthdr.len should have been set before cksum calcuration, 829fb59c426SYoshinobu Inoue * because in6_cksum() need it. 830fb59c426SYoshinobu Inoue */ 831fb59c426SYoshinobu Inoue #ifdef INET6 832fb59c426SYoshinobu Inoue if (isipv6) { 833fb59c426SYoshinobu Inoue /* 834fb59c426SYoshinobu Inoue * we separately set hoplimit for every segment, since the 835fb59c426SYoshinobu Inoue * user might want to change the value via setsockopt. 836fb59c426SYoshinobu Inoue * Also, desired default hop limit might be changed via 837fb59c426SYoshinobu Inoue * Neighbor Discovery. 838fb59c426SYoshinobu Inoue */ 839fb59c426SYoshinobu Inoue ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, 840fb59c426SYoshinobu Inoue tp->t_inpcb->in6p_route.ro_rt ? 841fb59c426SYoshinobu Inoue tp->t_inpcb->in6p_route.ro_rt->rt_ifp 842fb59c426SYoshinobu Inoue : NULL); 843fb59c426SYoshinobu Inoue 844fb59c426SYoshinobu Inoue /* TODO: IPv6 IP6TOS_ECT bit on */ 845fb59c426SYoshinobu Inoue #ifdef IPSEC 84633841545SHajimu UMEMOTO if (ipsec_setsocket(m, so) != 0) { 84733841545SHajimu UMEMOTO m_freem(m); 84833841545SHajimu UMEMOTO error = ENOBUFS; 84933841545SHajimu UMEMOTO goto out; 85033841545SHajimu UMEMOTO } 851fb59c426SYoshinobu Inoue #endif /*IPSEC*/ 852fb59c426SYoshinobu Inoue error = ip6_output(m, 853fb59c426SYoshinobu Inoue tp->t_inpcb->in6p_outputopts, 854fb59c426SYoshinobu Inoue &tp->t_inpcb->in6p_route, 8554cc20ab1SSeigo Tanimura (so->so_options & SO_DONTROUTE), NULL, NULL); 856fb59c426SYoshinobu Inoue } else 857fb59c426SYoshinobu Inoue #endif /* INET6 */ 858df8bae1dSRodney W. Grimes { 859f138387aSGarrett Wollman struct rtentry *rt; 860fb59c426SYoshinobu Inoue ip->ip_len = m->m_pkthdr.len; 861686cdd19SJun-ichiro itojun Hagino #ifdef INET6 862686cdd19SJun-ichiro itojun Hagino if (INP_CHECK_SOCKAF(so, AF_INET6)) 863686cdd19SJun-ichiro itojun Hagino ip->ip_ttl = in6_selecthlim(tp->t_inpcb, 864686cdd19SJun-ichiro itojun Hagino tp->t_inpcb->in6p_route.ro_rt ? 865686cdd19SJun-ichiro itojun Hagino tp->t_inpcb->in6p_route.ro_rt->rt_ifp 866686cdd19SJun-ichiro itojun Hagino : NULL); 867686cdd19SJun-ichiro itojun Hagino else 868686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */ 869fb59c426SYoshinobu Inoue ip->ip_ttl = tp->t_inpcb->inp_ip_ttl; /* XXX */ 870fb59c426SYoshinobu Inoue ip->ip_tos = tp->t_inpcb->inp_ip_tos; /* XXX */ 871f138387aSGarrett Wollman /* 872f138387aSGarrett Wollman * See if we should do MTU discovery. We do it only if the following 873f138387aSGarrett Wollman * are true: 874f138387aSGarrett Wollman * 1) we have a valid route to the destination 875f138387aSGarrett Wollman * 2) the MTU is not locked (if it is, then discovery has been 876f138387aSGarrett Wollman * disabled) 877f138387aSGarrett Wollman */ 8789a039a5fSDavid Greenman if (path_mtu_discovery 8799a039a5fSDavid Greenman && (rt = tp->t_inpcb->inp_route.ro_rt) 880f138387aSGarrett Wollman && rt->rt_flags & RTF_UP 881f138387aSGarrett Wollman && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { 882fb59c426SYoshinobu Inoue ip->ip_off |= IP_DF; 883f138387aSGarrett Wollman } 884686cdd19SJun-ichiro itojun Hagino #ifdef IPSEC 885686cdd19SJun-ichiro itojun Hagino ipsec_setsocket(m, so); 886686cdd19SJun-ichiro itojun Hagino #endif /*IPSEC*/ 887df8bae1dSRodney W. Grimes error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 8884cc20ab1SSeigo Tanimura (so->so_options & SO_DONTROUTE), 0); 889df8bae1dSRodney W. Grimes } 890df8bae1dSRodney W. Grimes if (error) { 8917734ea06SArchie Cobbs 8927734ea06SArchie Cobbs /* 8937734ea06SArchie Cobbs * We know that the packet was lost, so back out the 8947734ea06SArchie Cobbs * sequence number advance, if any. 8957734ea06SArchie Cobbs */ 8967734ea06SArchie Cobbs if (tp->t_force == 0 || !callout_active(tp->tt_persist)) { 8977734ea06SArchie Cobbs /* 8987734ea06SArchie Cobbs * No need to check for TH_FIN here because 8997734ea06SArchie Cobbs * the TF_SENTFIN flag handles that case. 9007734ea06SArchie Cobbs */ 9016612c70eSArchie Cobbs if ((flags & TH_SYN) == 0) 9027734ea06SArchie Cobbs tp->snd_nxt -= len; 9037734ea06SArchie Cobbs } 9047734ea06SArchie Cobbs 905df8bae1dSRodney W. Grimes out: 906df8bae1dSRodney W. Grimes if (error == ENOBUFS) { 90759f577adSJonathan Lemon if (!callout_active(tp->tt_rexmt) && 90859f577adSJonathan Lemon !callout_active(tp->tt_persist)) 90959f577adSJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 91059f577adSJonathan Lemon tcp_timer_rexmt, tp); 911df8bae1dSRodney W. Grimes tcp_quench(tp->t_inpcb, 0); 912df8bae1dSRodney W. Grimes return (0); 913df8bae1dSRodney W. Grimes } 9143d1f141bSGarrett Wollman if (error == EMSGSIZE) { 9153d1f141bSGarrett Wollman /* 9163d1f141bSGarrett Wollman * ip_output() will have already fixed the route 9173d1f141bSGarrett Wollman * for us. tcp_mtudisc() will, as its last action, 9183d1f141bSGarrett Wollman * initiate retransmission, so it is important to 9193d1f141bSGarrett Wollman * not do so here. 9203d1f141bSGarrett Wollman */ 9213d1f141bSGarrett Wollman tcp_mtudisc(tp->t_inpcb, 0); 9223d1f141bSGarrett Wollman return 0; 9233d1f141bSGarrett Wollman } 924df8bae1dSRodney W. Grimes if ((error == EHOSTUNREACH || error == ENETDOWN) 925df8bae1dSRodney W. Grimes && TCPS_HAVERCVDSYN(tp->t_state)) { 926df8bae1dSRodney W. Grimes tp->t_softerror = error; 927df8bae1dSRodney W. Grimes return (0); 928df8bae1dSRodney W. Grimes } 929df8bae1dSRodney W. Grimes return (error); 930df8bae1dSRodney W. Grimes } 931df8bae1dSRodney W. Grimes tcpstat.tcps_sndtotal++; 932df8bae1dSRodney W. Grimes 933df8bae1dSRodney W. Grimes /* 934df8bae1dSRodney W. Grimes * Data sent (as far as we can tell). 935df8bae1dSRodney W. Grimes * If this advertises a larger window than any other segment, 936df8bae1dSRodney W. Grimes * then remember the size of the advertised window. 937df8bae1dSRodney W. Grimes * Any pending ACK has now been sent. 938df8bae1dSRodney W. Grimes */ 939df8bae1dSRodney W. Grimes if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 940df8bae1dSRodney W. Grimes tp->rcv_adv = tp->rcv_nxt + win; 941df8bae1dSRodney W. Grimes tp->last_ack_sent = tp->rcv_nxt; 9429b8b58e0SJonathan Lemon tp->t_flags &= ~TF_ACKNOW; 9439b8b58e0SJonathan Lemon if (tcp_delack_enabled) 9449b8b58e0SJonathan Lemon callout_stop(tp->tt_delack); 945d912c694SMatthew Dillon #if 0 946d912c694SMatthew Dillon /* 947d912c694SMatthew Dillon * This completely breaks TCP if newreno is turned on. What happens 948d912c694SMatthew Dillon * is that if delayed-acks are turned on on the receiver, this code 949d912c694SMatthew Dillon * on the transmitter effectively destroys the TCP window, forcing 950d912c694SMatthew Dillon * it to four packets (1.5Kx4 = 6K window). 951d912c694SMatthew Dillon */ 95246f58482SJonathan Lemon if (sendalot && (!tcp_do_newreno || --maxburst)) 953df8bae1dSRodney W. Grimes goto again; 954d912c694SMatthew Dillon #endif 955d912c694SMatthew Dillon if (sendalot) 956d912c694SMatthew Dillon goto again; 957df8bae1dSRodney W. Grimes return (0); 958df8bae1dSRodney W. Grimes } 959df8bae1dSRodney W. Grimes 960df8bae1dSRodney W. Grimes void 961df8bae1dSRodney W. Grimes tcp_setpersist(tp) 962df8bae1dSRodney W. Grimes register struct tcpcb *tp; 963df8bae1dSRodney W. Grimes { 9649b8b58e0SJonathan Lemon int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 9659b8b58e0SJonathan Lemon int tt; 966df8bae1dSRodney W. Grimes 9679b8b58e0SJonathan Lemon if (callout_active(tp->tt_rexmt)) 9689b8b58e0SJonathan Lemon panic("tcp_setpersist: retransmit pending"); 969df8bae1dSRodney W. Grimes /* 970df8bae1dSRodney W. Grimes * Start/restart persistance timer. 971df8bae1dSRodney W. Grimes */ 9729b8b58e0SJonathan Lemon TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 973df8bae1dSRodney W. Grimes TCPTV_PERSMIN, TCPTV_PERSMAX); 9749b8b58e0SJonathan Lemon callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp); 975df8bae1dSRodney W. Grimes if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 976df8bae1dSRodney W. Grimes tp->t_rxtshift++; 977df8bae1dSRodney W. Grimes } 978