1c398230bSWarner Losh /*- 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 * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29d7f570e6SGarrett Wollman * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95 30c3aac50fSPeter Wemm * $FreeBSD$ 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 331cfd4b53SBruce M Simpson #include "opt_inet.h" 34fb59c426SYoshinobu Inoue #include "opt_inet6.h" 353a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 36c488362eSRobert Watson #include "opt_mac.h" 370cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 386d90faf3SPaul Saab #include "opt_tcp_sack.h" 390cc12cc5SJoerg Wunsch 40df8bae1dSRodney W. Grimes #include <sys/param.h> 41df8bae1dSRodney W. Grimes #include <sys/systm.h> 42686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h> 43fb919e4dSMark Murray #include <sys/kernel.h> 44fb919e4dSMark Murray #include <sys/lock.h> 45c488362eSRobert Watson #include <sys/mac.h> 46fb919e4dSMark Murray #include <sys/mbuf.h> 47fb919e4dSMark Murray #include <sys/mutex.h> 48df8bae1dSRodney W. Grimes #include <sys/protosw.h> 49df8bae1dSRodney W. Grimes #include <sys/socket.h> 50df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 51fb919e4dSMark Murray #include <sys/sysctl.h> 52df8bae1dSRodney W. Grimes 53df8bae1dSRodney W. Grimes #include <net/route.h> 54df8bae1dSRodney W. Grimes 55df8bae1dSRodney W. Grimes #include <netinet/in.h> 56df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 57df8bae1dSRodney W. Grimes #include <netinet/ip.h> 58df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 59df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 60ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 61fb59c426SYoshinobu Inoue #ifdef INET6 62686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 63686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 64fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 65fb59c426SYoshinobu Inoue #endif 66df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 67df8bae1dSRodney W. Grimes #define TCPOUTFLAGS 68df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 69df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 70df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 71df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 72df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 73610ee2f9SDavid Greenman #ifdef TCPDEBUG 74df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 75610ee2f9SDavid Greenman #endif 76df8bae1dSRodney W. Grimes 773a2a9f79SYoshinobu Inoue #ifdef IPSEC 783a2a9f79SYoshinobu Inoue #include <netinet6/ipsec.h> 793a2a9f79SYoshinobu Inoue #endif /*IPSEC*/ 803a2a9f79SYoshinobu Inoue 81b9234fafSSam Leffler #ifdef FAST_IPSEC 82b9234fafSSam Leffler #include <netipsec/ipsec.h> 83b9234fafSSam Leffler #define IPSEC 84b9234fafSSam Leffler #endif /*FAST_IPSEC*/ 85b9234fafSSam Leffler 86db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 87db4f9cc7SJonathan Lemon 88df8bae1dSRodney W. Grimes #ifdef notyet 89df8bae1dSRodney W. Grimes extern struct mbuf *m_copypack(); 90df8bae1dSRodney W. Grimes #endif 91df8bae1dSRodney W. Grimes 92eb5afebaSMike Silbersack int path_mtu_discovery = 1; 939a039a5fSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW, 949a039a5fSDavid Greenman &path_mtu_discovery, 1, "Enable Path MTU Discovery"); 959a039a5fSDavid Greenman 969b8b58e0SJonathan Lemon int ss_fltsz = 1; 979b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW, 989b8b58e0SJonathan Lemon &ss_fltsz, 1, "Slow start flight size"); 999b8b58e0SJonathan Lemon 1003260eb18SMike Silbersack int ss_fltsz_local = 4; 1019b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW, 1029b8b58e0SJonathan Lemon &ss_fltsz_local, 1, "Slow start flight size for local networks"); 103df8bae1dSRodney W. Grimes 1047d200109SJayanth Vijayaraghavan int tcp_do_newreno = 1; 10546f58482SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno, 10646f58482SJonathan Lemon 0, "Enable NewReno Algorithms"); 107314e5a3dSJeffrey Hsu 108df8bae1dSRodney W. Grimes /* 109df8bae1dSRodney W. Grimes * Tcp output routine: figure out what should be sent and send it. 110df8bae1dSRodney W. Grimes */ 111df8bae1dSRodney W. Grimes int 112f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp) 113df8bae1dSRodney W. Grimes { 114f10e85d7SLuigi Rizzo struct socket *so = tp->t_inpcb->inp_socket; 115201d185bSAndre Oppermann long len, recwin, sendwin; 116df8bae1dSRodney W. Grimes int off, flags, error; 11745d370eeSBruce M Simpson #ifdef TCP_SIGNATURE 1181cfd4b53SBruce M Simpson int sigoff = 0; 119265ed012SBruce M Simpson #endif 120f10e85d7SLuigi Rizzo struct mbuf *m; 121fb59c426SYoshinobu Inoue struct ip *ip = NULL; 122f10e85d7SLuigi Rizzo struct ipovly *ipov = NULL; 123f10e85d7SLuigi Rizzo struct tcphdr *th; 124a0292f23SGarrett Wollman u_char opt[TCP_MAXOLEN]; 125a04884fcSBill Fenner unsigned ipoptlen, optlen, hdrlen; 126df8bae1dSRodney W. Grimes int idle, sendalot; 1276d90faf3SPaul Saab int i, sack_rxmit; 128a55db2b6SPaul Saab int sack_bytes_rxmt; 1296d90faf3SPaul Saab struct sackhole *p; 130262c1c1aSMatthew Dillon #if 0 13146f58482SJonathan Lemon int maxburst = TCP_MAXBURST; 132262c1c1aSMatthew Dillon #endif 133fb59c426SYoshinobu Inoue #ifdef INET6 134f10e85d7SLuigi Rizzo struct ip6_hdr *ip6 = NULL; 135fb59c426SYoshinobu Inoue int isipv6; 136fb59c426SYoshinobu Inoue 137fb59c426SYoshinobu Inoue isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 138fb59c426SYoshinobu Inoue #endif 139df8bae1dSRodney W. Grimes 140fa286d7dSSam Leffler INP_LOCK_ASSERT(tp->t_inpcb); 1413d6ade3aSJennifer Yang 142df8bae1dSRodney W. Grimes /* 143df8bae1dSRodney W. Grimes * Determine length of data that should be transmitted, 144df8bae1dSRodney W. Grimes * and flags that will be used. 145df8bae1dSRodney W. Grimes * If there is some data or critical controls (SYN, RST) 146df8bae1dSRodney W. Grimes * to send, then transmit; otherwise, investigate further. 147df8bae1dSRodney W. Grimes */ 148c24d5daeSJayanth Vijayaraghavan idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 1499b8b58e0SJonathan Lemon if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) { 150df8bae1dSRodney W. Grimes /* 151df8bae1dSRodney W. Grimes * We have been idle for "a while" and no acks are 152df8bae1dSRodney W. Grimes * expected to clock out any data we send -- 153df8bae1dSRodney W. Grimes * slow start to get ack "clock" running again. 1549b8b58e0SJonathan Lemon * 1559b8b58e0SJonathan Lemon * Set the slow-start flight size depending on whether 1569b8b58e0SJonathan Lemon * this is a local network or not. 157df8bae1dSRodney W. Grimes */ 158f10e85d7SLuigi Rizzo int ss = ss_fltsz; 159fb59c426SYoshinobu Inoue #ifdef INET6 160f10e85d7SLuigi Rizzo if (isipv6) { 161f10e85d7SLuigi Rizzo if (in6_localaddr(&tp->t_inpcb->in6p_faddr)) 162f10e85d7SLuigi Rizzo ss = ss_fltsz_local; 163f10e85d7SLuigi Rizzo } else 164f10e85d7SLuigi Rizzo #endif /* INET6 */ 165f10e85d7SLuigi Rizzo if (in_localaddr(tp->t_inpcb->inp_faddr)) 166f10e85d7SLuigi Rizzo ss = ss_fltsz_local; 167f10e85d7SLuigi Rizzo tp->snd_cwnd = tp->t_maxseg * ss; 1689b8b58e0SJonathan Lemon } 169c24d5daeSJayanth Vijayaraghavan tp->t_flags &= ~TF_LASTIDLE; 170c24d5daeSJayanth Vijayaraghavan if (idle) { 171c24d5daeSJayanth Vijayaraghavan if (tp->t_flags & TF_MORETOCOME) { 172c24d5daeSJayanth Vijayaraghavan tp->t_flags |= TF_LASTIDLE; 173c24d5daeSJayanth Vijayaraghavan idle = 0; 174c24d5daeSJayanth Vijayaraghavan } 175c24d5daeSJayanth Vijayaraghavan } 176df8bae1dSRodney W. Grimes again: 1776d90faf3SPaul Saab /* 1786d90faf3SPaul Saab * If we've recently taken a timeout, snd_max will be greater than 1796d90faf3SPaul Saab * snd_nxt. There may be SACK information that allows us to avoid 1806d90faf3SPaul Saab * resending already delivered data. Adjust snd_nxt accordingly. 1816d90faf3SPaul Saab */ 1826d90faf3SPaul Saab if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max)) 1836d90faf3SPaul Saab tcp_sack_adjust(tp); 184df8bae1dSRodney W. Grimes sendalot = 0; 185df8bae1dSRodney W. Grimes off = tp->snd_nxt - tp->snd_una; 186201d185bSAndre Oppermann sendwin = min(tp->snd_wnd, tp->snd_cwnd); 187201d185bSAndre Oppermann sendwin = min(sendwin, tp->snd_bwnd); 188df8bae1dSRodney W. Grimes 189df8bae1dSRodney W. Grimes flags = tcp_outflags[tp->t_state]; 190a0292f23SGarrett Wollman /* 1916d90faf3SPaul Saab * Send any SACK-generated retransmissions. If we're explicitly trying 1926d90faf3SPaul Saab * to send out new data (when sendalot is 1), bypass this function. 1936d90faf3SPaul Saab * If we retransmit in fast recovery mode, decrement snd_cwnd, since 1946d90faf3SPaul Saab * we're replacing a (future) new transmission with a retransmission 1956d90faf3SPaul Saab * now, and we previously incremented snd_cwnd in tcp_input(). 1966d90faf3SPaul Saab */ 1976d90faf3SPaul Saab /* 1986d90faf3SPaul Saab * Still in sack recovery , reset rxmit flag to zero. 1996d90faf3SPaul Saab */ 2006d90faf3SPaul Saab sack_rxmit = 0; 201a55db2b6SPaul Saab sack_bytes_rxmt = 0; 2026d90faf3SPaul Saab len = 0; 2036d90faf3SPaul Saab p = NULL; 20404f0d9a0SJayanth Vijayaraghavan if (tp->sack_enable && IN_FASTRECOVERY(tp) && 205a55db2b6SPaul Saab (p = tcp_sack_output(tp, &sack_bytes_rxmt))) { 206a55db2b6SPaul Saab long cwin; 207a55db2b6SPaul Saab 208a55db2b6SPaul Saab cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt; 209a55db2b6SPaul Saab if (cwin < 0) 210a55db2b6SPaul Saab cwin = 0; 211f787edd8SJayanth Vijayaraghavan /* Do not retransmit SACK segments beyond snd_recover */ 212f787edd8SJayanth Vijayaraghavan if (SEQ_GT(p->end, tp->snd_recover)) { 213f787edd8SJayanth Vijayaraghavan /* 214f787edd8SJayanth Vijayaraghavan * (At least) part of sack hole extends beyond 215f787edd8SJayanth Vijayaraghavan * snd_recover. Check to see if we can rexmit data 216f787edd8SJayanth Vijayaraghavan * for this hole. 217f787edd8SJayanth Vijayaraghavan */ 218f787edd8SJayanth Vijayaraghavan if (SEQ_GEQ(p->rxmit, tp->snd_recover)) { 219f787edd8SJayanth Vijayaraghavan /* 220f787edd8SJayanth Vijayaraghavan * Can't rexmit any more data for this hole. 221f787edd8SJayanth Vijayaraghavan * That data will be rexmitted in the next 222f787edd8SJayanth Vijayaraghavan * sack recovery episode, when snd_recover 223f787edd8SJayanth Vijayaraghavan * moves past p->rxmit. 224f787edd8SJayanth Vijayaraghavan */ 225f787edd8SJayanth Vijayaraghavan p = NULL; 226f787edd8SJayanth Vijayaraghavan goto after_sack_rexmit; 227f787edd8SJayanth Vijayaraghavan } else 228f787edd8SJayanth Vijayaraghavan /* Can rexmit part of the current hole */ 229a55db2b6SPaul Saab len = ((long)ulmin(cwin, 230f787edd8SJayanth Vijayaraghavan tp->snd_recover - p->rxmit)); 231f787edd8SJayanth Vijayaraghavan } else 232a55db2b6SPaul Saab len = ((long)ulmin(cwin, p->end - p->rxmit)); 2336d90faf3SPaul Saab off = p->rxmit - tp->snd_una; 234f787edd8SJayanth Vijayaraghavan KASSERT(off >= 0,("%s: sack block to the left of una : %d", 235f787edd8SJayanth Vijayaraghavan __func__, off)); 2366d90faf3SPaul Saab if (len > 0) { 237ab5c14d8SRobert Watson sack_rxmit = 1; 238ab5c14d8SRobert Watson sendalot = 1; 2396d90faf3SPaul Saab tcpstat.tcps_sack_rexmits++; 2406d90faf3SPaul Saab tcpstat.tcps_sack_rexmit_bytes += 2416d90faf3SPaul Saab min(len, tp->t_maxseg); 2426d90faf3SPaul Saab } 2436d90faf3SPaul Saab } 244f787edd8SJayanth Vijayaraghavan after_sack_rexmit: 2456d90faf3SPaul Saab /* 246a0292f23SGarrett Wollman * Get standard flags, and add SYN or FIN if requested by 'hidden' 247a0292f23SGarrett Wollman * state flags. 248a0292f23SGarrett Wollman */ 249a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) 250a0292f23SGarrett Wollman flags |= TH_FIN; 251a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) 252a0292f23SGarrett Wollman flags |= TH_SYN; 253a0292f23SGarrett Wollman 254cf2942b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 255df8bae1dSRodney W. Grimes /* 256df8bae1dSRodney W. Grimes * If in persist timeout with window of 0, send 1 byte. 257df8bae1dSRodney W. Grimes * Otherwise, if window is small but nonzero 258df8bae1dSRodney W. Grimes * and timer expired, we will send what we can 259df8bae1dSRodney W. Grimes * and go to transmit state. 260df8bae1dSRodney W. Grimes */ 2612cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) { 262201d185bSAndre Oppermann if (sendwin == 0) { 263df8bae1dSRodney W. Grimes /* 264df8bae1dSRodney W. Grimes * If we still have some data to send, then 265df8bae1dSRodney W. Grimes * clear the FIN bit. Usually this would 266df8bae1dSRodney W. Grimes * happen below when it realizes that we 267df8bae1dSRodney W. Grimes * aren't sending all the data. However, 26829089b51SJulian Elischer * if we have exactly 1 byte of unsent data, 269df8bae1dSRodney W. Grimes * then it won't clear the FIN bit below, 270df8bae1dSRodney W. Grimes * and if we are in persist state, we wind 271df8bae1dSRodney W. Grimes * up sending the packet without recording 272df8bae1dSRodney W. Grimes * that we sent the FIN bit. 273df8bae1dSRodney W. Grimes * 274df8bae1dSRodney W. Grimes * We can't just blindly clear the FIN bit, 275df8bae1dSRodney W. Grimes * because if we don't have any more data 276df8bae1dSRodney W. Grimes * to send then the probe will be the FIN 277df8bae1dSRodney W. Grimes * itself. 278df8bae1dSRodney W. Grimes */ 279df8bae1dSRodney W. Grimes if (off < so->so_snd.sb_cc) 280df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 281201d185bSAndre Oppermann sendwin = 1; 282df8bae1dSRodney W. Grimes } else { 2839b8b58e0SJonathan Lemon callout_stop(tp->tt_persist); 284df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 285df8bae1dSRodney W. Grimes } 286df8bae1dSRodney W. Grimes } 287df8bae1dSRodney W. Grimes 28828257b5cSMatthew Dillon /* 28928257b5cSMatthew Dillon * If snd_nxt == snd_max and we have transmitted a FIN, the 29028257b5cSMatthew Dillon * offset will be > 0 even if so_snd.sb_cc is 0, resulting in 291201d185bSAndre Oppermann * a negative length. This can also occur when TCP opens up 29228257b5cSMatthew Dillon * its congestion window while receiving additional duplicate 29328257b5cSMatthew Dillon * acks after fast-retransmit because TCP will reset snd_nxt 29428257b5cSMatthew Dillon * to snd_max after the fast-retransmit. 29528257b5cSMatthew Dillon * 29628257b5cSMatthew Dillon * In the normal retransmit-FIN-only case, however, snd_nxt will 29728257b5cSMatthew Dillon * be set to snd_una, the offset will be 0, and the length may 29828257b5cSMatthew Dillon * wind up 0. 2996d90faf3SPaul Saab * 3006d90faf3SPaul Saab * If sack_rxmit is true we are retransmitting from the scoreboard 3016d90faf3SPaul Saab * in which case len is already set. 30228257b5cSMatthew Dillon */ 303a55db2b6SPaul Saab if (sack_rxmit == 0) { 304a55db2b6SPaul Saab if (sack_bytes_rxmt == 0) 3056d90faf3SPaul Saab len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off); 306a55db2b6SPaul Saab else { 307a55db2b6SPaul Saab long cwin; 308a55db2b6SPaul Saab 309a55db2b6SPaul Saab /* 310a55db2b6SPaul Saab * We are inside of a SACK recovery episode and are 311a55db2b6SPaul Saab * sending new data, having retransmitted all the 312a55db2b6SPaul Saab * data possible in the scoreboard. 313a55db2b6SPaul Saab */ 3147d5ed1ceSPaul Saab len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd) 3157d5ed1ceSPaul Saab - off); 3168d03f2b5SPaul Saab /* 3178d03f2b5SPaul Saab * Don't remove this (len > 0) check ! 3188d03f2b5SPaul Saab * We explicitly check for len > 0 here (although it 3198d03f2b5SPaul Saab * isn't really necessary), to work around a gcc 3208d03f2b5SPaul Saab * optimization issue - to force gcc to compute 3218d03f2b5SPaul Saab * len above. Without this check, the computation 3228d03f2b5SPaul Saab * of len is bungled by the optimizer. 3238d03f2b5SPaul Saab */ 3248d03f2b5SPaul Saab if (len > 0) { 3257d5ed1ceSPaul Saab cwin = tp->snd_cwnd - 3267d5ed1ceSPaul Saab (tp->snd_nxt - tp->sack_newdata) - 327a55db2b6SPaul Saab sack_bytes_rxmt; 328a55db2b6SPaul Saab if (cwin < 0) 329a55db2b6SPaul Saab cwin = 0; 330a55db2b6SPaul Saab len = lmin(len, cwin); 331a55db2b6SPaul Saab } 332a55db2b6SPaul Saab } 3338d03f2b5SPaul Saab } 334a0292f23SGarrett Wollman 335a0292f23SGarrett Wollman /* 336a0292f23SGarrett Wollman * Lop off SYN bit if it has already been sent. However, if this 337a0292f23SGarrett Wollman * is SYN-SENT state and if segment contains data and if we don't 338a0292f23SGarrett Wollman * know that foreign host supports TAO, suppress sending segment. 339a0292f23SGarrett Wollman */ 340a0292f23SGarrett Wollman if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 341a0292f23SGarrett Wollman flags &= ~TH_SYN; 342a0292f23SGarrett Wollman off--, len++; 343a0292f23SGarrett Wollman } 344a0292f23SGarrett Wollman 34581165e48SAndras Olah /* 346c94c54e4SAndre Oppermann * Be careful not to send data and/or FIN on SYN segments. 34781165e48SAndras Olah * This measure is needed to prevent interoperability problems 34881165e48SAndras Olah * with not fully conformant TCP implementations. 34981165e48SAndras Olah */ 350c94c54e4SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 35181165e48SAndras Olah len = 0; 35281165e48SAndras Olah flags &= ~TH_FIN; 35381165e48SAndras Olah } 35481165e48SAndras Olah 355df8bae1dSRodney W. Grimes if (len < 0) { 356df8bae1dSRodney W. Grimes /* 357df8bae1dSRodney W. Grimes * If FIN has been sent but not acked, 358df8bae1dSRodney W. Grimes * but we haven't been called to retransmit, 35928257b5cSMatthew Dillon * len will be < 0. Otherwise, window shrank 360df8bae1dSRodney W. Grimes * after we sent into it. If window shrank to 0, 3612d8266afSDavid Greenman * cancel pending retransmit, pull snd_nxt back 3622d8266afSDavid Greenman * to (closed) window, and set the persist timer 3632d8266afSDavid Greenman * if it isn't already going. If the window didn't 3642d8266afSDavid Greenman * close completely, just wait for an ACK. 365df8bae1dSRodney W. Grimes */ 366df8bae1dSRodney W. Grimes len = 0; 367201d185bSAndre Oppermann if (sendwin == 0) { 3689b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 3692d8266afSDavid Greenman tp->t_rxtshift = 0; 370df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 3719b8b58e0SJonathan Lemon if (!callout_active(tp->tt_persist)) 3722d8266afSDavid Greenman tcp_setpersist(tp); 373df8bae1dSRodney W. Grimes } 374df8bae1dSRodney W. Grimes } 37528257b5cSMatthew Dillon 37628257b5cSMatthew Dillon /* 37728257b5cSMatthew Dillon * len will be >= 0 after this point. Truncate to the maximum 37828257b5cSMatthew Dillon * segment length and ensure that FIN is removed if the length 37928257b5cSMatthew Dillon * no longer contains the last data byte. 38028257b5cSMatthew Dillon */ 381df8bae1dSRodney W. Grimes if (len > tp->t_maxseg) { 382df8bae1dSRodney W. Grimes len = tp->t_maxseg; 383df8bae1dSRodney W. Grimes sendalot = 1; 384df8bae1dSRodney W. Grimes } 3855d3b1b75SJayanth Vijayaraghavan if (sack_rxmit) { 3865d3b1b75SJayanth Vijayaraghavan if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc)) 387df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 3885d3b1b75SJayanth Vijayaraghavan } else { 3895d3b1b75SJayanth Vijayaraghavan if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 3905d3b1b75SJayanth Vijayaraghavan flags &= ~TH_FIN; 3915d3b1b75SJayanth Vijayaraghavan } 392df8bae1dSRodney W. Grimes 393201d185bSAndre Oppermann recwin = sbspace(&so->so_rcv); 394df8bae1dSRodney W. Grimes 395df8bae1dSRodney W. Grimes /* 396262c1c1aSMatthew Dillon * Sender silly window avoidance. We transmit under the following 397262c1c1aSMatthew Dillon * conditions when len is non-zero: 398262c1c1aSMatthew Dillon * 399262c1c1aSMatthew Dillon * - We have a full segment 400262c1c1aSMatthew Dillon * - This is the last buffer in a write()/send() and we are 401262c1c1aSMatthew Dillon * either idle or running NODELAY 402262c1c1aSMatthew Dillon * - we've timed out (e.g. persist timer) 403262c1c1aSMatthew Dillon * - we have more then 1/2 the maximum send window's worth of 404262c1c1aSMatthew Dillon * data (receiver may be limited the window size) 405262c1c1aSMatthew Dillon * - we need to retransmit 406df8bae1dSRodney W. Grimes */ 407df8bae1dSRodney W. Grimes if (len) { 408df8bae1dSRodney W. Grimes if (len == tp->t_maxseg) 409df8bae1dSRodney W. Grimes goto send; 410262c1c1aSMatthew Dillon /* 411262c1c1aSMatthew Dillon * NOTE! on localhost connections an 'ack' from the remote 412262c1c1aSMatthew Dillon * end may occur synchronously with the output and cause 413262c1c1aSMatthew Dillon * us to flush a buffer queued with moretocome. XXX 414262c1c1aSMatthew Dillon * 415262c1c1aSMatthew Dillon * note: the len + off check is almost certainly unnecessary. 416262c1c1aSMatthew Dillon */ 417262c1c1aSMatthew Dillon if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 418262c1c1aSMatthew Dillon (idle || (tp->t_flags & TF_NODELAY)) && 419262c1c1aSMatthew Dillon len + off >= so->so_snd.sb_cc && 420262c1c1aSMatthew Dillon (tp->t_flags & TF_NOPUSH) == 0) { 421df8bae1dSRodney W. Grimes goto send; 422262c1c1aSMatthew Dillon } 4232cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) /* typ. timeout case */ 424df8bae1dSRodney W. Grimes goto send; 425a0292f23SGarrett Wollman if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 426df8bae1dSRodney W. Grimes goto send; 427262c1c1aSMatthew Dillon if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */ 428df8bae1dSRodney W. Grimes goto send; 4296d90faf3SPaul Saab if (sack_rxmit) 4306d90faf3SPaul Saab goto send; 431df8bae1dSRodney W. Grimes } 432df8bae1dSRodney W. Grimes 433df8bae1dSRodney W. Grimes /* 434df8bae1dSRodney W. Grimes * Compare available window to amount of window 435df8bae1dSRodney W. Grimes * known to peer (as advertised window less 436df8bae1dSRodney W. Grimes * next expected input). If the difference is at least two 437df8bae1dSRodney W. Grimes * max size segments, or at least 50% of the maximum possible 438df8bae1dSRodney W. Grimes * window, then want to send a window update to peer. 4393bfd6421SJonathan Lemon * Skip this if the connection is in T/TCP half-open state. 440df8bae1dSRodney W. Grimes */ 441201d185bSAndre Oppermann if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN)) { 442df8bae1dSRodney W. Grimes /* 443df8bae1dSRodney W. Grimes * "adv" is the amount we can increase the window, 444df8bae1dSRodney W. Grimes * taking into account that we are limited by 445df8bae1dSRodney W. Grimes * TCP_MAXWIN << tp->rcv_scale. 446df8bae1dSRodney W. Grimes */ 447201d185bSAndre Oppermann long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) - 448df8bae1dSRodney W. Grimes (tp->rcv_adv - tp->rcv_nxt); 449df8bae1dSRodney W. Grimes 450df8bae1dSRodney W. Grimes if (adv >= (long) (2 * tp->t_maxseg)) 451df8bae1dSRodney W. Grimes goto send; 452df8bae1dSRodney W. Grimes if (2 * adv >= (long) so->so_rcv.sb_hiwat) 453df8bae1dSRodney W. Grimes goto send; 454df8bae1dSRodney W. Grimes } 455df8bae1dSRodney W. Grimes 456df8bae1dSRodney W. Grimes /* 45728257b5cSMatthew Dillon * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 45828257b5cSMatthew Dillon * is also a catch-all for the retransmit timer timeout case. 459df8bae1dSRodney W. Grimes */ 460df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 461df8bae1dSRodney W. Grimes goto send; 462a0292f23SGarrett Wollman if ((flags & TH_RST) || 463a0292f23SGarrett Wollman ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 464df8bae1dSRodney W. Grimes goto send; 465df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_una)) 466df8bae1dSRodney W. Grimes goto send; 467df8bae1dSRodney W. Grimes /* 468df8bae1dSRodney W. Grimes * If our state indicates that FIN should be sent 46928257b5cSMatthew Dillon * and we have not yet done so, then we need to send. 470df8bae1dSRodney W. Grimes */ 471df8bae1dSRodney W. Grimes if (flags & TH_FIN && 472df8bae1dSRodney W. Grimes ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 473df8bae1dSRodney W. Grimes goto send; 4746d90faf3SPaul Saab /* 4756d90faf3SPaul Saab * In SACK, it is possible for tcp_output to fail to send a segment 4766d90faf3SPaul Saab * after the retransmission timer has been turned off. Make sure 4776d90faf3SPaul Saab * that the retransmission timer is set. 4786d90faf3SPaul Saab */ 4796d90faf3SPaul Saab if (tp->sack_enable && SEQ_GT(tp->snd_max, tp->snd_una) && 4806d90faf3SPaul Saab !callout_active(tp->tt_rexmt) && 4816d90faf3SPaul Saab !callout_active(tp->tt_persist)) { 4826d90faf3SPaul Saab callout_reset(tp->tt_rexmt, tp->t_rxtcur, 4836d90faf3SPaul Saab tcp_timer_rexmt, tp); 484cf2942b6SRobert Watson goto just_return; 4856d90faf3SPaul Saab } 486df8bae1dSRodney W. Grimes /* 487df8bae1dSRodney W. Grimes * TCP window updates are not reliable, rather a polling protocol 488df8bae1dSRodney W. Grimes * using ``persist'' packets is used to insure receipt of window 489df8bae1dSRodney W. Grimes * updates. The three ``states'' for the output side are: 490df8bae1dSRodney W. Grimes * idle not doing retransmits or persists 491df8bae1dSRodney W. Grimes * persisting to move a small or zero window 492df8bae1dSRodney W. Grimes * (re)transmitting and thereby not persisting 493df8bae1dSRodney W. Grimes * 4949b8b58e0SJonathan Lemon * callout_active(tp->tt_persist) 4959b8b58e0SJonathan Lemon * is true when we are in persist state. 4962cdbfa66SPaul Saab * (tp->t_flags & TF_FORCEDATA) 497df8bae1dSRodney W. Grimes * is set when we are called to send a persist packet. 4989b8b58e0SJonathan Lemon * callout_active(tp->tt_rexmt) 499df8bae1dSRodney W. Grimes * is set when we are retransmitting 500df8bae1dSRodney W. Grimes * The output side is idle when both timers are zero. 501df8bae1dSRodney W. Grimes * 502df8bae1dSRodney W. Grimes * If send window is too small, there is data to transmit, and no 503df8bae1dSRodney W. Grimes * retransmit or persist is pending, then go to persist state. 504df8bae1dSRodney W. Grimes * If nothing happens soon, send when timer expires: 505df8bae1dSRodney W. Grimes * if window is nonzero, transmit what we can, 506df8bae1dSRodney W. Grimes * otherwise force out a byte. 507df8bae1dSRodney W. Grimes */ 5089b8b58e0SJonathan Lemon if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) && 5099b8b58e0SJonathan Lemon !callout_active(tp->tt_persist)) { 510df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 511df8bae1dSRodney W. Grimes tcp_setpersist(tp); 512df8bae1dSRodney W. Grimes } 513df8bae1dSRodney W. Grimes 514df8bae1dSRodney W. Grimes /* 515df8bae1dSRodney W. Grimes * No reason to send a segment, just return. 516df8bae1dSRodney W. Grimes */ 517cf2942b6SRobert Watson just_return: 518cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 519df8bae1dSRodney W. Grimes return (0); 520df8bae1dSRodney W. Grimes 521df8bae1dSRodney W. Grimes send: 522cf2942b6SRobert Watson SOCKBUF_LOCK_ASSERT(&so->so_snd); 523df8bae1dSRodney W. Grimes /* 524df8bae1dSRodney W. Grimes * Before ESTABLISHED, force sending of initial options 525df8bae1dSRodney W. Grimes * unless TCP set not to do any options. 526df8bae1dSRodney W. Grimes * NOTE: we assume that the IP/TCP header plus TCP options 527df8bae1dSRodney W. Grimes * always fit in a single mbuf, leaving room for a maximum 528df8bae1dSRodney W. Grimes * link header, i.e. 52933841545SHajimu UMEMOTO * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 530df8bae1dSRodney W. Grimes */ 531df8bae1dSRodney W. Grimes optlen = 0; 532fb59c426SYoshinobu Inoue #ifdef INET6 533fb59c426SYoshinobu Inoue if (isipv6) 534fb59c426SYoshinobu Inoue hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 535fb59c426SYoshinobu Inoue else 536fb59c426SYoshinobu Inoue #endif 537df8bae1dSRodney W. Grimes hdrlen = sizeof (struct tcpiphdr); 538df8bae1dSRodney W. Grimes if (flags & TH_SYN) { 539df8bae1dSRodney W. Grimes tp->snd_nxt = tp->iss; 540df8bae1dSRodney W. Grimes if ((tp->t_flags & TF_NOOPT) == 0) { 541df8bae1dSRodney W. Grimes u_short mss; 542df8bae1dSRodney W. Grimes 543df8bae1dSRodney W. Grimes opt[0] = TCPOPT_MAXSEG; 544a0292f23SGarrett Wollman opt[1] = TCPOLEN_MAXSEG; 54597d8d152SAndre Oppermann mss = htons((u_short) tcp_mssopt(&tp->t_inpcb->inp_inc)); 54694a5d9b6SDavid Greenman (void)memcpy(opt + 2, &mss, sizeof(mss)); 547a0292f23SGarrett Wollman optlen = TCPOLEN_MAXSEG; 548df8bae1dSRodney W. Grimes 549df8bae1dSRodney W. Grimes if ((tp->t_flags & TF_REQ_SCALE) && 550df8bae1dSRodney W. Grimes ((flags & TH_ACK) == 0 || 551df8bae1dSRodney W. Grimes (tp->t_flags & TF_RCVD_SCALE))) { 5529105bb46SBruce Evans *((u_int32_t *)(opt + optlen)) = htonl( 553df8bae1dSRodney W. Grimes TCPOPT_NOP << 24 | 554df8bae1dSRodney W. Grimes TCPOPT_WINDOW << 16 | 555df8bae1dSRodney W. Grimes TCPOLEN_WINDOW << 8 | 556df8bae1dSRodney W. Grimes tp->request_r_scale); 557df8bae1dSRodney W. Grimes optlen += 4; 558df8bae1dSRodney W. Grimes } 559df8bae1dSRodney W. Grimes } 560df8bae1dSRodney W. Grimes } 561df8bae1dSRodney W. Grimes 562df8bae1dSRodney W. Grimes /* 563df8bae1dSRodney W. Grimes * Send a timestamp and echo-reply if this is a SYN and our side 564df8bae1dSRodney W. Grimes * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 565df8bae1dSRodney W. Grimes * and our peer have sent timestamps in our SYN's. 566df8bae1dSRodney W. Grimes */ 567df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 568df8bae1dSRodney W. Grimes (flags & TH_RST) == 0 && 569a0292f23SGarrett Wollman ((flags & TH_ACK) == 0 || 570df8bae1dSRodney W. Grimes (tp->t_flags & TF_RCVD_TSTMP))) { 5719105bb46SBruce Evans u_int32_t *lp = (u_int32_t *)(opt + optlen); 572df8bae1dSRodney W. Grimes 573df8bae1dSRodney W. Grimes /* Form timestamp option as shown in appendix A of RFC 1323. */ 574df8bae1dSRodney W. Grimes *lp++ = htonl(TCPOPT_TSTAMP_HDR); 5759b8b58e0SJonathan Lemon *lp++ = htonl(ticks); 576df8bae1dSRodney W. Grimes *lp = htonl(tp->ts_recent); 577df8bae1dSRodney W. Grimes optlen += TCPOLEN_TSTAMP_APPA; 578df8bae1dSRodney W. Grimes } 579df8bae1dSRodney W. Grimes 5801cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 5811cfd4b53SBruce M Simpson #ifdef INET6 5821cfd4b53SBruce M Simpson if (!isipv6) 5831cfd4b53SBruce M Simpson #endif 5841cfd4b53SBruce M Simpson if (tp->t_flags & TF_SIGNATURE) { 5851cfd4b53SBruce M Simpson int i; 5861cfd4b53SBruce M Simpson u_char *bp; 587bca0e5bfSBruce M Simpson 588bca0e5bfSBruce M Simpson /* Initialize TCP-MD5 option (RFC2385) */ 5891cfd4b53SBruce M Simpson bp = (u_char *)opt + optlen; 5901cfd4b53SBruce M Simpson *bp++ = TCPOPT_SIGNATURE; 5911cfd4b53SBruce M Simpson *bp++ = TCPOLEN_SIGNATURE; 5921cfd4b53SBruce M Simpson sigoff = optlen + 2; 5931cfd4b53SBruce M Simpson for (i = 0; i < TCP_SIGLEN; i++) 5941cfd4b53SBruce M Simpson *bp++ = 0; 5951cfd4b53SBruce M Simpson optlen += TCPOLEN_SIGNATURE; 5961cfd4b53SBruce M Simpson } 5971cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 5981cfd4b53SBruce M Simpson 599be3f3b5eSPaul Saab if (tp->sack_enable && ((tp->t_flags & TF_NOOPT) == 0)) { 600be3f3b5eSPaul Saab /* 601be3f3b5eSPaul Saab * Tack on the SACK permitted option *last*. 602be3f3b5eSPaul Saab * And do padding of options after tacking this on. 603be3f3b5eSPaul Saab * This is because of MSS, TS, WinScale and Signatures are 604be3f3b5eSPaul Saab * all present, we have just 2 bytes left for the SACK 605be3f3b5eSPaul Saab * permitted option, which is just enough. 606be3f3b5eSPaul Saab */ 607be3f3b5eSPaul Saab /* 608be3f3b5eSPaul Saab * If this is the first SYN of connection (not a SYN 609be3f3b5eSPaul Saab * ACK), include SACK permitted option. If this is a 610be3f3b5eSPaul Saab * SYN ACK, include SACK permitted option if peer has 611be3f3b5eSPaul Saab * already done so. This is only for active connect, 612be3f3b5eSPaul Saab * since the syncache takes care of the passive connect. 613be3f3b5eSPaul Saab */ 614be3f3b5eSPaul Saab if ((flags & TH_SYN) && 615be3f3b5eSPaul Saab (!(flags & TH_ACK) || (tp->t_flags & TF_SACK_PERMIT))) { 616be3f3b5eSPaul Saab u_char *bp; 617be3f3b5eSPaul Saab bp = (u_char *)opt + optlen; 618be3f3b5eSPaul Saab 619be3f3b5eSPaul Saab *bp++ = TCPOPT_SACK_PERMITTED; 620be3f3b5eSPaul Saab *bp++ = TCPOLEN_SACK_PERMITTED; 621be3f3b5eSPaul Saab optlen += TCPOLEN_SACK_PERMITTED; 622be3f3b5eSPaul Saab } 623be3f3b5eSPaul Saab 624be3f3b5eSPaul Saab /* 625be3f3b5eSPaul Saab * Send SACKs if necessary. This should be the last 626be3f3b5eSPaul Saab * option processed. Only as many SACKs are sent as 627be3f3b5eSPaul Saab * are permitted by the maximum options size. 628be3f3b5eSPaul Saab * 629be3f3b5eSPaul Saab * In general, SACK blocks consume 8*n+2 bytes. 630be3f3b5eSPaul Saab * So a full size SACK blocks option is 34 bytes 631be3f3b5eSPaul Saab * (to generate 4 SACK blocks). At a minimum, 632be3f3b5eSPaul Saab * we need 10 bytes (to generate 1 SACK block). 633be3f3b5eSPaul Saab * If TCP Timestamps (12 bytes) and TCP Signatures 634be3f3b5eSPaul Saab * (18 bytes) are both present, we'll just have 635be3f3b5eSPaul Saab * 10 bytes for SACK options 40 - (12 + 18). 636be3f3b5eSPaul Saab */ 637be3f3b5eSPaul Saab if (TCPS_HAVEESTABLISHED(tp->t_state) && 638be3f3b5eSPaul Saab (tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks > 0 && 639be3f3b5eSPaul Saab MAX_TCPOPTLEN - optlen - 2 >= TCPOLEN_SACK) { 640be3f3b5eSPaul Saab int nsack, sackoptlen, padlen; 641be3f3b5eSPaul Saab u_char *bp = (u_char *)opt + optlen; 642be3f3b5eSPaul Saab u_int32_t *lp; 643be3f3b5eSPaul Saab 644be3f3b5eSPaul Saab nsack = (MAX_TCPOPTLEN - optlen - 2) / TCPOLEN_SACK; 645be3f3b5eSPaul Saab nsack = min(nsack, tp->rcv_numsacks); 646be3f3b5eSPaul Saab sackoptlen = (2 + nsack * TCPOLEN_SACK); 647be3f3b5eSPaul Saab 648be3f3b5eSPaul Saab /* 649be3f3b5eSPaul Saab * First we need to pad options so that the 650be3f3b5eSPaul Saab * SACK blocks can start at a 4-byte boundary 651be3f3b5eSPaul Saab * (sack option and length are at a 2 byte offset). 652be3f3b5eSPaul Saab */ 653be3f3b5eSPaul Saab padlen = (MAX_TCPOPTLEN - optlen - sackoptlen) % 4; 654be3f3b5eSPaul Saab optlen += padlen; 655be3f3b5eSPaul Saab while (padlen-- > 0) 656be3f3b5eSPaul Saab *bp++ = TCPOPT_NOP; 657be3f3b5eSPaul Saab 658be3f3b5eSPaul Saab tcpstat.tcps_sack_send_blocks++; 659be3f3b5eSPaul Saab *bp++ = TCPOPT_SACK; 660be3f3b5eSPaul Saab *bp++ = sackoptlen; 661be3f3b5eSPaul Saab lp = (u_int32_t *)bp; 662be3f3b5eSPaul Saab for (i = 0; i < nsack; i++) { 663be3f3b5eSPaul Saab struct sackblk sack = tp->sackblks[i]; 664be3f3b5eSPaul Saab *lp++ = htonl(sack.start); 665be3f3b5eSPaul Saab *lp++ = htonl(sack.end); 666be3f3b5eSPaul Saab } 667be3f3b5eSPaul Saab optlen += sackoptlen; 668be3f3b5eSPaul Saab } 669be3f3b5eSPaul Saab } 670be3f3b5eSPaul Saab 671be3f3b5eSPaul Saab /* Pad TCP options to a 4 byte boundary */ 672be3f3b5eSPaul Saab if (optlen < MAX_TCPOPTLEN && (optlen % sizeof(u_int32_t))) { 673be3f3b5eSPaul Saab int pad = sizeof(u_int32_t) - (optlen % sizeof(u_int32_t)); 674be3f3b5eSPaul Saab u_char *bp = (u_char *)opt + optlen; 675be3f3b5eSPaul Saab 676be3f3b5eSPaul Saab optlen += pad; 677be3f3b5eSPaul Saab while (pad) { 678be3f3b5eSPaul Saab *bp++ = TCPOPT_EOL; 679be3f3b5eSPaul Saab pad--; 680be3f3b5eSPaul Saab } 681be3f3b5eSPaul Saab } 682be3f3b5eSPaul Saab 683df8bae1dSRodney W. Grimes hdrlen += optlen; 684df8bae1dSRodney W. Grimes 685fb59c426SYoshinobu Inoue #ifdef INET6 686fb59c426SYoshinobu Inoue if (isipv6) 687fb59c426SYoshinobu Inoue ipoptlen = ip6_optlen(tp->t_inpcb); 688fb59c426SYoshinobu Inoue else 689fb59c426SYoshinobu Inoue #endif 690f10e85d7SLuigi Rizzo if (tp->t_inpcb->inp_options) 691a04884fcSBill Fenner ipoptlen = tp->t_inpcb->inp_options->m_len - 692a04884fcSBill Fenner offsetof(struct ipoption, ipopt_list); 693f10e85d7SLuigi Rizzo else 694a04884fcSBill Fenner ipoptlen = 0; 695fb59c426SYoshinobu Inoue #ifdef IPSEC 696fb59c426SYoshinobu Inoue ipoptlen += ipsec_hdrsiz_tcp(tp); 697fb59c426SYoshinobu Inoue #endif 698a04884fcSBill Fenner 699df8bae1dSRodney W. Grimes /* 700df8bae1dSRodney W. Grimes * Adjust data length if insertion of options will 701a0292f23SGarrett Wollman * bump the packet length beyond the t_maxopd length. 702a0292f23SGarrett Wollman * Clear the FIN bit because we cut off the tail of 703a0292f23SGarrett Wollman * the segment. 704df8bae1dSRodney W. Grimes */ 705a04884fcSBill Fenner if (len + optlen + ipoptlen > tp->t_maxopd) { 706297a37f3SDavid Greenman /* 707297a37f3SDavid Greenman * If there is still more to send, don't close the connection. 708297a37f3SDavid Greenman */ 709297a37f3SDavid Greenman flags &= ~TH_FIN; 710a04884fcSBill Fenner len = tp->t_maxopd - optlen - ipoptlen; 711df8bae1dSRodney W. Grimes sendalot = 1; 712df8bae1dSRodney W. Grimes } 713df8bae1dSRodney W. Grimes 714a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/ 715a683a7ddSYoshinobu Inoue #ifdef INET6 716a683a7ddSYoshinobu Inoue if (max_linkhdr + hdrlen > MCLBYTES) 717a683a7ddSYoshinobu Inoue #else 718df8bae1dSRodney W. Grimes if (max_linkhdr + hdrlen > MHLEN) 719a683a7ddSYoshinobu Inoue #endif 720f10e85d7SLuigi Rizzo panic("tcphdr too big"); 721a0292f23SGarrett Wollman /*#endif*/ 722df8bae1dSRodney W. Grimes 723df8bae1dSRodney W. Grimes /* 724df8bae1dSRodney W. Grimes * Grab a header mbuf, attaching a copy of data to 725df8bae1dSRodney W. Grimes * be transmitted, and initialize the header from 726df8bae1dSRodney W. Grimes * the template for sends on this connection. 727df8bae1dSRodney W. Grimes */ 728df8bae1dSRodney W. Grimes if (len) { 7292cdbfa66SPaul Saab if ((tp->t_flags & TF_FORCEDATA) && len == 1) 730df8bae1dSRodney W. Grimes tcpstat.tcps_sndprobe++; 731df8bae1dSRodney W. Grimes else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 732df8bae1dSRodney W. Grimes tcpstat.tcps_sndrexmitpack++; 733df8bae1dSRodney W. Grimes tcpstat.tcps_sndrexmitbyte += len; 734df8bae1dSRodney W. Grimes } else { 735df8bae1dSRodney W. Grimes tcpstat.tcps_sndpack++; 736df8bae1dSRodney W. Grimes tcpstat.tcps_sndbyte += len; 737df8bae1dSRodney W. Grimes } 738df8bae1dSRodney W. Grimes #ifdef notyet 739df8bae1dSRodney W. Grimes if ((m = m_copypack(so->so_snd.sb_mb, off, 740df8bae1dSRodney W. Grimes (int)len, max_linkhdr + hdrlen)) == 0) { 741cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 742df8bae1dSRodney W. Grimes error = ENOBUFS; 743df8bae1dSRodney W. Grimes goto out; 744df8bae1dSRodney W. Grimes } 745df8bae1dSRodney W. Grimes /* 746df8bae1dSRodney W. Grimes * m_copypack left space for our hdr; use it. 747df8bae1dSRodney W. Grimes */ 748df8bae1dSRodney W. Grimes m->m_len += hdrlen; 749df8bae1dSRodney W. Grimes m->m_data -= hdrlen; 750df8bae1dSRodney W. Grimes #else 75134333b16SAndre Oppermann MGETHDR(m, M_DONTWAIT, MT_DATA); 752df8bae1dSRodney W. Grimes if (m == NULL) { 753cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 754df8bae1dSRodney W. Grimes error = ENOBUFS; 755df8bae1dSRodney W. Grimes goto out; 756df8bae1dSRodney W. Grimes } 757fb59c426SYoshinobu Inoue #ifdef INET6 758a683a7ddSYoshinobu Inoue if (MHLEN < hdrlen + max_linkhdr) { 759a163d034SWarner Losh MCLGET(m, M_DONTWAIT); 760a683a7ddSYoshinobu Inoue if ((m->m_flags & M_EXT) == 0) { 761cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 762a683a7ddSYoshinobu Inoue m_freem(m); 763a683a7ddSYoshinobu Inoue error = ENOBUFS; 764a683a7ddSYoshinobu Inoue goto out; 765a683a7ddSYoshinobu Inoue } 766a683a7ddSYoshinobu Inoue } 767fb59c426SYoshinobu Inoue #endif 768df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 769df8bae1dSRodney W. Grimes m->m_len = hdrlen; 770df8bae1dSRodney W. Grimes if (len <= MHLEN - hdrlen - max_linkhdr) { 771df8bae1dSRodney W. Grimes m_copydata(so->so_snd.sb_mb, off, (int) len, 772df8bae1dSRodney W. Grimes mtod(m, caddr_t) + hdrlen); 773df8bae1dSRodney W. Grimes m->m_len += len; 774df8bae1dSRodney W. Grimes } else { 775df8bae1dSRodney W. Grimes m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 77651823c3aSGarrett Wollman if (m->m_next == 0) { 777cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 778d7f570e6SGarrett Wollman (void) m_free(m); 77951823c3aSGarrett Wollman error = ENOBUFS; 78051823c3aSGarrett Wollman goto out; 78151823c3aSGarrett Wollman } 782df8bae1dSRodney W. Grimes } 783df8bae1dSRodney W. Grimes #endif 784df8bae1dSRodney W. Grimes /* 785df8bae1dSRodney W. Grimes * If we're sending everything we've got, set PUSH. 786df8bae1dSRodney W. Grimes * (This will keep happy those implementations which only 787df8bae1dSRodney W. Grimes * give data to the user when a buffer fills or 788df8bae1dSRodney W. Grimes * a PUSH comes in.) 789df8bae1dSRodney W. Grimes */ 790df8bae1dSRodney W. Grimes if (off + len == so->so_snd.sb_cc) 791df8bae1dSRodney W. Grimes flags |= TH_PUSH; 792cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 793df8bae1dSRodney W. Grimes } else { 794cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 795df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 796df8bae1dSRodney W. Grimes tcpstat.tcps_sndacks++; 797df8bae1dSRodney W. Grimes else if (flags & (TH_SYN|TH_FIN|TH_RST)) 798df8bae1dSRodney W. Grimes tcpstat.tcps_sndctrl++; 799df8bae1dSRodney W. Grimes else if (SEQ_GT(tp->snd_up, tp->snd_una)) 800df8bae1dSRodney W. Grimes tcpstat.tcps_sndurg++; 801df8bae1dSRodney W. Grimes else 802df8bae1dSRodney W. Grimes tcpstat.tcps_sndwinup++; 803df8bae1dSRodney W. Grimes 80434333b16SAndre Oppermann MGETHDR(m, M_DONTWAIT, MT_DATA); 805df8bae1dSRodney W. Grimes if (m == NULL) { 806df8bae1dSRodney W. Grimes error = ENOBUFS; 807df8bae1dSRodney W. Grimes goto out; 808df8bae1dSRodney W. Grimes } 809fb59c426SYoshinobu Inoue #ifdef INET6 810fb59c426SYoshinobu Inoue if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 811fb59c426SYoshinobu Inoue MHLEN >= hdrlen) { 812fb59c426SYoshinobu Inoue MH_ALIGN(m, hdrlen); 813fb59c426SYoshinobu Inoue } else 814fb59c426SYoshinobu Inoue #endif 815df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 816df8bae1dSRodney W. Grimes m->m_len = hdrlen; 817df8bae1dSRodney W. Grimes } 818cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); 819df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 820c488362eSRobert Watson #ifdef MAC 821c18b97c6SRobert Watson mac_create_mbuf_from_inpcb(tp->t_inpcb, m); 822c488362eSRobert Watson #endif 823fb59c426SYoshinobu Inoue #ifdef INET6 824fb59c426SYoshinobu Inoue if (isipv6) { 825fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 826fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip6 + 1); 82779909384SJonathan Lemon tcpip_fillheaders(tp->t_inpcb, ip6, th); 828fb59c426SYoshinobu Inoue } else 829fb59c426SYoshinobu Inoue #endif /* INET6 */ 830fb59c426SYoshinobu Inoue { 831fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 832fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 833fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip + 1); 83479909384SJonathan Lemon tcpip_fillheaders(tp->t_inpcb, ip, th); 835fb59c426SYoshinobu Inoue } 836df8bae1dSRodney W. Grimes 837df8bae1dSRodney W. Grimes /* 838df8bae1dSRodney W. Grimes * Fill in fields, remembering maximum advertised 839df8bae1dSRodney W. Grimes * window for use in delaying messages about window sizes. 840df8bae1dSRodney W. Grimes * If resending a FIN, be sure not to use a new sequence number. 841df8bae1dSRodney W. Grimes */ 842df8bae1dSRodney W. Grimes if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 843df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) 844df8bae1dSRodney W. Grimes tp->snd_nxt--; 845df8bae1dSRodney W. Grimes /* 846df8bae1dSRodney W. Grimes * If we are doing retransmissions, then snd_nxt will 847df8bae1dSRodney W. Grimes * not reflect the first unsent octet. For ACK only 848df8bae1dSRodney W. Grimes * packets, we do not want the sequence number of the 849df8bae1dSRodney W. Grimes * retransmitted packet, we want the sequence number 850df8bae1dSRodney W. Grimes * of the next unsent octet. So, if there is no data 851df8bae1dSRodney W. Grimes * (and no SYN or FIN), use snd_max instead of snd_nxt 852df8bae1dSRodney W. Grimes * when filling in ti_seq. But if we are in persist 853df8bae1dSRodney W. Grimes * state, snd_max might reflect one byte beyond the 854df8bae1dSRodney W. Grimes * right edge of the window, so use snd_nxt in that 855df8bae1dSRodney W. Grimes * case, since we know we aren't doing a retransmission. 856df8bae1dSRodney W. Grimes * (retransmit and persist are mutually exclusive...) 857df8bae1dSRodney W. Grimes */ 858a55db2b6SPaul Saab if (sack_rxmit == 0) { 8599b8b58e0SJonathan Lemon if (len || (flags & (TH_SYN|TH_FIN)) 8609b8b58e0SJonathan Lemon || callout_active(tp->tt_persist)) 861fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_nxt); 862df8bae1dSRodney W. Grimes else 863fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_max); 864a55db2b6SPaul Saab } else { 8656d90faf3SPaul Saab th->th_seq = htonl(p->rxmit); 8666d90faf3SPaul Saab p->rxmit += len; 8670077b016SPaul Saab tp->sackhint.sack_bytes_rexmit += len; 8686d90faf3SPaul Saab } 869fb59c426SYoshinobu Inoue th->th_ack = htonl(tp->rcv_nxt); 870df8bae1dSRodney W. Grimes if (optlen) { 871fb59c426SYoshinobu Inoue bcopy(opt, th + 1, optlen); 872fb59c426SYoshinobu Inoue th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 873df8bae1dSRodney W. Grimes } 874fb59c426SYoshinobu Inoue th->th_flags = flags; 875df8bae1dSRodney W. Grimes /* 876df8bae1dSRodney W. Grimes * Calculate receive window. Don't shrink window, 877df8bae1dSRodney W. Grimes * but avoid silly window syndrome. 878df8bae1dSRodney W. Grimes */ 879201d185bSAndre Oppermann if (recwin < (long)(so->so_rcv.sb_hiwat / 4) && 880201d185bSAndre Oppermann recwin < (long)tp->t_maxseg) 881201d185bSAndre Oppermann recwin = 0; 882201d185bSAndre Oppermann if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt)) 883201d185bSAndre Oppermann recwin = (long)(tp->rcv_adv - tp->rcv_nxt); 884201d185bSAndre Oppermann if (recwin > (long)TCP_MAXWIN << tp->rcv_scale) 885201d185bSAndre Oppermann recwin = (long)TCP_MAXWIN << tp->rcv_scale; 886201d185bSAndre Oppermann th->th_win = htons((u_short) (recwin >> tp->rcv_scale)); 887262c1c1aSMatthew Dillon 888262c1c1aSMatthew Dillon 889262c1c1aSMatthew Dillon /* 890262c1c1aSMatthew Dillon * Adjust the RXWIN0SENT flag - indicate that we have advertised 891262c1c1aSMatthew Dillon * a 0 window. This may cause the remote transmitter to stall. This 892262c1c1aSMatthew Dillon * flag tells soreceive() to disable delayed acknowledgements when 893262c1c1aSMatthew Dillon * draining the buffer. This can occur if the receiver is attempting 894262c1c1aSMatthew Dillon * to read more data then can be buffered prior to transmitting on 895262c1c1aSMatthew Dillon * the connection. 896262c1c1aSMatthew Dillon */ 897201d185bSAndre Oppermann if (recwin == 0) 898262c1c1aSMatthew Dillon tp->t_flags |= TF_RXWIN0SENT; 899262c1c1aSMatthew Dillon else 900262c1c1aSMatthew Dillon tp->t_flags &= ~TF_RXWIN0SENT; 901df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 902fb59c426SYoshinobu Inoue th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 903fb59c426SYoshinobu Inoue th->th_flags |= TH_URG; 904df8bae1dSRodney W. Grimes } else 905df8bae1dSRodney W. Grimes /* 906df8bae1dSRodney W. Grimes * If no urgent pointer to send, then we pull 907df8bae1dSRodney W. Grimes * the urgent pointer to the left edge of the send window 908df8bae1dSRodney W. Grimes * so that it doesn't drift into the send window on sequence 909df8bae1dSRodney W. Grimes * number wraparound. 910df8bae1dSRodney W. Grimes */ 911df8bae1dSRodney W. Grimes tp->snd_up = tp->snd_una; /* drag it along */ 912df8bae1dSRodney W. Grimes 9131cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 9141cfd4b53SBruce M Simpson #ifdef INET6 9151cfd4b53SBruce M Simpson if (!isipv6) 9161cfd4b53SBruce M Simpson #endif 9171cfd4b53SBruce M Simpson if (tp->t_flags & TF_SIGNATURE) 918265ed012SBruce M Simpson tcp_signature_compute(m, sizeof(struct ip), len, optlen, 9191cfd4b53SBruce M Simpson (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND); 920265ed012SBruce M Simpson #endif 9211cfd4b53SBruce M Simpson 922df8bae1dSRodney W. Grimes /* 923df8bae1dSRodney W. Grimes * Put TCP length in extended header, and then 924df8bae1dSRodney W. Grimes * checksum extended header and data. 925df8bae1dSRodney W. Grimes */ 926fb59c426SYoshinobu Inoue m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 927fb59c426SYoshinobu Inoue #ifdef INET6 928fb59c426SYoshinobu Inoue if (isipv6) 929fb59c426SYoshinobu Inoue /* 930fb59c426SYoshinobu Inoue * ip6_plen is not need to be filled now, and will be filled 931fb59c426SYoshinobu Inoue * in ip6_output. 932fb59c426SYoshinobu Inoue */ 933fb59c426SYoshinobu Inoue th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 934fb59c426SYoshinobu Inoue sizeof(struct tcphdr) + optlen + len); 935fb59c426SYoshinobu Inoue else 936fb59c426SYoshinobu Inoue #endif /* INET6 */ 937fb59c426SYoshinobu Inoue { 938db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_TCP; 939db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 94079909384SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 94179909384SJonathan Lemon htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen)); 942fb59c426SYoshinobu Inoue 943db4f9cc7SJonathan Lemon /* IP version must be set here for ipv4/ipv6 checking later */ 944db4f9cc7SJonathan Lemon KASSERT(ip->ip_v == IPVERSION, 9456e551fb6SDavid E. O'Brien ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 946fb59c426SYoshinobu Inoue } 947df8bae1dSRodney W. Grimes 948df8bae1dSRodney W. Grimes /* 949df8bae1dSRodney W. Grimes * In transmit state, time the transmission and arrange for 950df8bae1dSRodney W. Grimes * the retransmit. In persist state, just set snd_max. 951df8bae1dSRodney W. Grimes */ 9522cdbfa66SPaul Saab if ((tp->t_flags & TF_FORCEDATA) == 0 || 9532cdbfa66SPaul Saab !callout_active(tp->tt_persist)) { 954df8bae1dSRodney W. Grimes tcp_seq startseq = tp->snd_nxt; 955df8bae1dSRodney W. Grimes 956df8bae1dSRodney W. Grimes /* 957df8bae1dSRodney W. Grimes * Advance snd_nxt over sequence space of this segment. 958df8bae1dSRodney W. Grimes */ 959df8bae1dSRodney W. Grimes if (flags & (TH_SYN|TH_FIN)) { 960df8bae1dSRodney W. Grimes if (flags & TH_SYN) 961df8bae1dSRodney W. Grimes tp->snd_nxt++; 962df8bae1dSRodney W. Grimes if (flags & TH_FIN) { 963df8bae1dSRodney W. Grimes tp->snd_nxt++; 964df8bae1dSRodney W. Grimes tp->t_flags |= TF_SENTFIN; 965df8bae1dSRodney W. Grimes } 966df8bae1dSRodney W. Grimes } 967a55db2b6SPaul Saab if (sack_rxmit) 9686d90faf3SPaul Saab goto timer; 969df8bae1dSRodney W. Grimes tp->snd_nxt += len; 970df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 971df8bae1dSRodney W. Grimes tp->snd_max = tp->snd_nxt; 972df8bae1dSRodney W. Grimes /* 973df8bae1dSRodney W. Grimes * Time this transmission if not a retransmission and 974df8bae1dSRodney W. Grimes * not currently timing anything. 975df8bae1dSRodney W. Grimes */ 9769b8b58e0SJonathan Lemon if (tp->t_rtttime == 0) { 9779b8b58e0SJonathan Lemon tp->t_rtttime = ticks; 978df8bae1dSRodney W. Grimes tp->t_rtseq = startseq; 979df8bae1dSRodney W. Grimes tcpstat.tcps_segstimed++; 980df8bae1dSRodney W. Grimes } 981df8bae1dSRodney W. Grimes } 982df8bae1dSRodney W. Grimes 983df8bae1dSRodney W. Grimes /* 984df8bae1dSRodney W. Grimes * Set retransmit timer if not currently set, 98528257b5cSMatthew Dillon * and not doing a pure ack or a keep-alive probe. 986df8bae1dSRodney W. Grimes * Initial value for retransmit timer is smoothed 987df8bae1dSRodney W. Grimes * round-trip time + 2 * round-trip time variance. 988df8bae1dSRodney W. Grimes * Initialize shift counter which is used for backoff 989df8bae1dSRodney W. Grimes * of retransmit time. 990df8bae1dSRodney W. Grimes */ 9916d90faf3SPaul Saab timer: 9929b8b58e0SJonathan Lemon if (!callout_active(tp->tt_rexmt) && 993a55db2b6SPaul Saab ((sack_rxmit && tp->snd_nxt != tp->snd_max) || 994a55db2b6SPaul Saab (tp->snd_nxt != tp->snd_una))) { 9959b8b58e0SJonathan Lemon if (callout_active(tp->tt_persist)) { 9969b8b58e0SJonathan Lemon callout_stop(tp->tt_persist); 997df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 998df8bae1dSRodney W. Grimes } 99946f58482SJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 100046f58482SJonathan Lemon tcp_timer_rexmt, tp); 1001df8bae1dSRodney W. Grimes } 100228257b5cSMatthew Dillon } else { 100328257b5cSMatthew Dillon /* 100428257b5cSMatthew Dillon * Persist case, update snd_max but since we are in 100528257b5cSMatthew Dillon * persist mode (no window) we do not update snd_nxt. 100628257b5cSMatthew Dillon */ 100728257b5cSMatthew Dillon int xlen = len; 100828257b5cSMatthew Dillon if (flags & TH_SYN) 100928257b5cSMatthew Dillon ++xlen; 101028257b5cSMatthew Dillon if (flags & TH_FIN) { 101128257b5cSMatthew Dillon ++xlen; 101228257b5cSMatthew Dillon tp->t_flags |= TF_SENTFIN; 101328257b5cSMatthew Dillon } 1014abac41a6SMatthew Dillon if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) 1015df8bae1dSRodney W. Grimes tp->snd_max = tp->snd_nxt + len; 101628257b5cSMatthew Dillon } 1017df8bae1dSRodney W. Grimes 1018610ee2f9SDavid Greenman #ifdef TCPDEBUG 1019df8bae1dSRodney W. Grimes /* 1020df8bae1dSRodney W. Grimes * Trace. 1021df8bae1dSRodney W. Grimes */ 102291f467d5SHartmut Brandt if (so->so_options & SO_DEBUG) { 1023f3e0b7efSBruce M Simpson u_short save = 0; 10245214cb3fSBruce M Simpson #ifdef INET6 10255214cb3fSBruce M Simpson if (!isipv6) 10265214cb3fSBruce M Simpson #endif 10275214cb3fSBruce M Simpson { 10285214cb3fSBruce M Simpson save = ipov->ih_len; 102991f467d5SHartmut Brandt ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */); 10305214cb3fSBruce M Simpson } 1031fb59c426SYoshinobu Inoue tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 10325214cb3fSBruce M Simpson #ifdef INET6 10335214cb3fSBruce M Simpson if (!isipv6) 10345214cb3fSBruce M Simpson #endif 103591f467d5SHartmut Brandt ipov->ih_len = save; 103691f467d5SHartmut Brandt } 1037610ee2f9SDavid Greenman #endif 1038df8bae1dSRodney W. Grimes 1039df8bae1dSRodney W. Grimes /* 1040df8bae1dSRodney W. Grimes * Fill in IP length and desired time to live and 1041df8bae1dSRodney W. Grimes * send to IP level. There should be a better way 1042df8bae1dSRodney W. Grimes * to handle ttl and tos; we could keep them in 1043df8bae1dSRodney W. Grimes * the template, but need a way to checksum without them. 1044df8bae1dSRodney W. Grimes */ 1045fb59c426SYoshinobu Inoue /* 1046fb59c426SYoshinobu Inoue * m->m_pkthdr.len should have been set before cksum calcuration, 1047fb59c426SYoshinobu Inoue * because in6_cksum() need it. 1048fb59c426SYoshinobu Inoue */ 1049fb59c426SYoshinobu Inoue #ifdef INET6 1050fb59c426SYoshinobu Inoue if (isipv6) { 1051fb59c426SYoshinobu Inoue /* 1052fb59c426SYoshinobu Inoue * we separately set hoplimit for every segment, since the 1053fb59c426SYoshinobu Inoue * user might want to change the value via setsockopt. 1054fb59c426SYoshinobu Inoue * Also, desired default hop limit might be changed via 1055fb59c426SYoshinobu Inoue * Neighbor Discovery. 1056fb59c426SYoshinobu Inoue */ 105797d8d152SAndre Oppermann ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL); 1058fb59c426SYoshinobu Inoue 1059fb59c426SYoshinobu Inoue /* TODO: IPv6 IP6TOS_ECT bit on */ 1060fb59c426SYoshinobu Inoue error = ip6_output(m, 106197d8d152SAndre Oppermann tp->t_inpcb->in6p_outputopts, NULL, 1062b5d47ff5SJohn-Mark Gurney ((so->so_options & SO_DONTROUTE) ? 1063b5d47ff5SJohn-Mark Gurney IP_ROUTETOIF : 0), NULL, NULL, tp->t_inpcb); 1064fb59c426SYoshinobu Inoue } else 1065fb59c426SYoshinobu Inoue #endif /* INET6 */ 1066df8bae1dSRodney W. Grimes { 1067fb59c426SYoshinobu Inoue ip->ip_len = m->m_pkthdr.len; 1068686cdd19SJun-ichiro itojun Hagino #ifdef INET6 1069686cdd19SJun-ichiro itojun Hagino if (INP_CHECK_SOCKAF(so, AF_INET6)) 107097d8d152SAndre Oppermann ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL); 1071686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */ 1072f138387aSGarrett Wollman /* 107397d8d152SAndre Oppermann * If we do path MTU discovery, then we set DF on every packet. 107497d8d152SAndre Oppermann * This might not be the best thing to do according to RFC3390 107597d8d152SAndre Oppermann * Section 2. However the tcp hostcache migitates the problem 107697d8d152SAndre Oppermann * so it affects only the first tcp connection with a host. 1077f138387aSGarrett Wollman */ 107897d8d152SAndre Oppermann if (path_mtu_discovery) 1079fb59c426SYoshinobu Inoue ip->ip_off |= IP_DF; 108097d8d152SAndre Oppermann 108197d8d152SAndre Oppermann error = ip_output(m, tp->t_inpcb->inp_options, NULL, 1082b5d47ff5SJohn-Mark Gurney ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0, 1083b5d47ff5SJohn-Mark Gurney tp->t_inpcb); 1084df8bae1dSRodney W. Grimes } 1085df8bae1dSRodney W. Grimes if (error) { 10867734ea06SArchie Cobbs 10877734ea06SArchie Cobbs /* 10887734ea06SArchie Cobbs * We know that the packet was lost, so back out the 10897734ea06SArchie Cobbs * sequence number advance, if any. 10907734ea06SArchie Cobbs */ 10912cdbfa66SPaul Saab if ((tp->t_flags & TF_FORCEDATA) == 0 || 10922cdbfa66SPaul Saab !callout_active(tp->tt_persist)) { 10937734ea06SArchie Cobbs /* 10947734ea06SArchie Cobbs * No need to check for TH_FIN here because 10957734ea06SArchie Cobbs * the TF_SENTFIN flag handles that case. 10967734ea06SArchie Cobbs */ 10975d3b1b75SJayanth Vijayaraghavan if ((flags & TH_SYN) == 0) { 10980077b016SPaul Saab if (sack_rxmit) { 10995d3b1b75SJayanth Vijayaraghavan p->rxmit -= len; 11000077b016SPaul Saab tp->sackhint.sack_bytes_rexmit -= len; 11010077b016SPaul Saab KASSERT(tp->sackhint.sack_bytes_rexmit 11020077b016SPaul Saab >= 0, 11030077b016SPaul Saab ("sackhint bytes rtx >= 0")); 11040077b016SPaul Saab } else 11057734ea06SArchie Cobbs tp->snd_nxt -= len; 11067734ea06SArchie Cobbs } 11075d3b1b75SJayanth Vijayaraghavan } 11087734ea06SArchie Cobbs 1109df8bae1dSRodney W. Grimes out: 1110cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); /* Check gotos. */ 1111df8bae1dSRodney W. Grimes if (error == ENOBUFS) { 111259f577adSJonathan Lemon if (!callout_active(tp->tt_rexmt) && 111359f577adSJonathan Lemon !callout_active(tp->tt_persist)) 111459f577adSJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 111559f577adSJonathan Lemon tcp_timer_rexmt, tp); 11161600372bSAndre Oppermann tp->snd_cwnd = tp->t_maxseg; 1117df8bae1dSRodney W. Grimes return (0); 1118df8bae1dSRodney W. Grimes } 11193d1f141bSGarrett Wollman if (error == EMSGSIZE) { 11203d1f141bSGarrett Wollman /* 11213d1f141bSGarrett Wollman * ip_output() will have already fixed the route 11223d1f141bSGarrett Wollman * for us. tcp_mtudisc() will, as its last action, 11233d1f141bSGarrett Wollman * initiate retransmission, so it is important to 11243d1f141bSGarrett Wollman * not do so here. 11253d1f141bSGarrett Wollman */ 11263d1f141bSGarrett Wollman tcp_mtudisc(tp->t_inpcb, 0); 11273d1f141bSGarrett Wollman return 0; 11283d1f141bSGarrett Wollman } 1129df8bae1dSRodney W. Grimes if ((error == EHOSTUNREACH || error == ENETDOWN) 1130df8bae1dSRodney W. Grimes && TCPS_HAVERCVDSYN(tp->t_state)) { 1131df8bae1dSRodney W. Grimes tp->t_softerror = error; 1132df8bae1dSRodney W. Grimes return (0); 1133df8bae1dSRodney W. Grimes } 1134df8bae1dSRodney W. Grimes return (error); 1135df8bae1dSRodney W. Grimes } 1136df8bae1dSRodney W. Grimes tcpstat.tcps_sndtotal++; 1137df8bae1dSRodney W. Grimes 1138df8bae1dSRodney W. Grimes /* 1139df8bae1dSRodney W. Grimes * Data sent (as far as we can tell). 1140df8bae1dSRodney W. Grimes * If this advertises a larger window than any other segment, 1141df8bae1dSRodney W. Grimes * then remember the size of the advertised window. 1142df8bae1dSRodney W. Grimes * Any pending ACK has now been sent. 1143df8bae1dSRodney W. Grimes */ 1144201d185bSAndre Oppermann if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 1145201d185bSAndre Oppermann tp->rcv_adv = tp->rcv_nxt + recwin; 1146df8bae1dSRodney W. Grimes tp->last_ack_sent = tp->rcv_nxt; 11473bfd6421SJonathan Lemon tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 11483bfd6421SJonathan Lemon if (callout_active(tp->tt_delack)) 11499b8b58e0SJonathan Lemon callout_stop(tp->tt_delack); 1150d912c694SMatthew Dillon #if 0 1151d912c694SMatthew Dillon /* 1152d912c694SMatthew Dillon * This completely breaks TCP if newreno is turned on. What happens 1153d912c694SMatthew Dillon * is that if delayed-acks are turned on on the receiver, this code 1154d912c694SMatthew Dillon * on the transmitter effectively destroys the TCP window, forcing 1155d912c694SMatthew Dillon * it to four packets (1.5Kx4 = 6K window). 1156d912c694SMatthew Dillon */ 115746f58482SJonathan Lemon if (sendalot && (!tcp_do_newreno || --maxburst)) 1158df8bae1dSRodney W. Grimes goto again; 1159d912c694SMatthew Dillon #endif 1160d912c694SMatthew Dillon if (sendalot) 1161d912c694SMatthew Dillon goto again; 1162df8bae1dSRodney W. Grimes return (0); 1163df8bae1dSRodney W. Grimes } 1164df8bae1dSRodney W. Grimes 1165df8bae1dSRodney W. Grimes void 1166df8bae1dSRodney W. Grimes tcp_setpersist(tp) 1167df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1168df8bae1dSRodney W. Grimes { 11699b8b58e0SJonathan Lemon int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 11709b8b58e0SJonathan Lemon int tt; 1171df8bae1dSRodney W. Grimes 11729b8b58e0SJonathan Lemon if (callout_active(tp->tt_rexmt)) 11739b8b58e0SJonathan Lemon panic("tcp_setpersist: retransmit pending"); 1174df8bae1dSRodney W. Grimes /* 1175df8bae1dSRodney W. Grimes * Start/restart persistance timer. 1176df8bae1dSRodney W. Grimes */ 11779b8b58e0SJonathan Lemon TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 1178df8bae1dSRodney W. Grimes TCPTV_PERSMIN, TCPTV_PERSMAX); 11799b8b58e0SJonathan Lemon callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp); 1180df8bae1dSRodney W. Grimes if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 1181df8bae1dSRodney W. Grimes tp->t_rxtshift++; 1182df8bae1dSRodney W. Grimes } 1183