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" 380cc12cc5SJoerg Wunsch 39df8bae1dSRodney W. Grimes #include <sys/param.h> 40df8bae1dSRodney W. Grimes #include <sys/systm.h> 41686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h> 42fb919e4dSMark Murray #include <sys/kernel.h> 43fb919e4dSMark Murray #include <sys/lock.h> 44fb919e4dSMark Murray #include <sys/mbuf.h> 45fb919e4dSMark Murray #include <sys/mutex.h> 46df8bae1dSRodney W. Grimes #include <sys/protosw.h> 47df8bae1dSRodney W. Grimes #include <sys/socket.h> 48df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 49fb919e4dSMark Murray #include <sys/sysctl.h> 50df8bae1dSRodney W. Grimes 51df8bae1dSRodney W. Grimes #include <net/route.h> 52df8bae1dSRodney W. Grimes 53df8bae1dSRodney W. Grimes #include <netinet/in.h> 54df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 55df8bae1dSRodney W. Grimes #include <netinet/ip.h> 56df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 57df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 58ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 59fb59c426SYoshinobu Inoue #ifdef INET6 60686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 61686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 62fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 63fb59c426SYoshinobu Inoue #endif 64df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 65df8bae1dSRodney W. Grimes #define TCPOUTFLAGS 66df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 67df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 68df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 69df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 70df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 71610ee2f9SDavid Greenman #ifdef TCPDEBUG 72df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 73610ee2f9SDavid Greenman #endif 74df8bae1dSRodney W. Grimes 753a2a9f79SYoshinobu Inoue #ifdef IPSEC 763a2a9f79SYoshinobu Inoue #include <netinet6/ipsec.h> 773a2a9f79SYoshinobu Inoue #endif /*IPSEC*/ 783a2a9f79SYoshinobu Inoue 79b9234fafSSam Leffler #ifdef FAST_IPSEC 80b9234fafSSam Leffler #include <netipsec/ipsec.h> 81b9234fafSSam Leffler #define IPSEC 82b9234fafSSam Leffler #endif /*FAST_IPSEC*/ 83b9234fafSSam Leffler 84db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 85db4f9cc7SJonathan Lemon 86aed55708SRobert Watson #include <security/mac/mac_framework.h> 87aed55708SRobert Watson 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; 1058b8ed7a7SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, 1068b8ed7a7SAndre Oppermann &tcp_do_newreno, 0, "Enable NewReno Algorithms"); 107314e5a3dSJeffrey Hsu 108b3c0f300SAndre Oppermann int tcp_do_tso = 1; 109b3c0f300SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_RW, 110b3c0f300SAndre Oppermann &tcp_do_tso, 0, "Enable TCP Segmentation Offload"); 111b3c0f300SAndre Oppermann 1126741ecf5SAndre Oppermann int tcp_do_autosndbuf = 1; 1136741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto, CTLFLAG_RW, 1146741ecf5SAndre Oppermann &tcp_do_autosndbuf, 0, "Enable automatic send buffer sizing"); 1156741ecf5SAndre Oppermann 1166741ecf5SAndre Oppermann int tcp_autosndbuf_inc = 8*1024; 1176741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_inc, CTLFLAG_RW, 1186741ecf5SAndre Oppermann &tcp_autosndbuf_inc, 0, "Incrementor step size of automatic send buffer"); 1196741ecf5SAndre Oppermann 1206741ecf5SAndre Oppermann int tcp_autosndbuf_max = 256*1024; 1216741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_max, CTLFLAG_RW, 1226741ecf5SAndre Oppermann &tcp_autosndbuf_max, 0, "Max size of automatic send buffer"); 1236741ecf5SAndre Oppermann 1246741ecf5SAndre Oppermann 125df8bae1dSRodney W. Grimes /* 126df8bae1dSRodney W. Grimes * Tcp output routine: figure out what should be sent and send it. 127df8bae1dSRodney W. Grimes */ 128df8bae1dSRodney W. Grimes int 129f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp) 130df8bae1dSRodney W. Grimes { 131f10e85d7SLuigi Rizzo struct socket *so = tp->t_inpcb->inp_socket; 132201d185bSAndre Oppermann long len, recwin, sendwin; 133df8bae1dSRodney W. Grimes int off, flags, error; 13445d370eeSBruce M Simpson #ifdef TCP_SIGNATURE 1351cfd4b53SBruce M Simpson int sigoff = 0; 136265ed012SBruce M Simpson #endif 137f10e85d7SLuigi Rizzo struct mbuf *m; 138fb59c426SYoshinobu Inoue struct ip *ip = NULL; 139f10e85d7SLuigi Rizzo struct ipovly *ipov = NULL; 140f10e85d7SLuigi Rizzo struct tcphdr *th; 141a0292f23SGarrett Wollman u_char opt[TCP_MAXOLEN]; 142a04884fcSBill Fenner unsigned ipoptlen, optlen, hdrlen; 143df8bae1dSRodney W. Grimes int idle, sendalot; 14402a1a643SAndre Oppermann int sack_rxmit, sack_bytes_rxmt; 1456d90faf3SPaul Saab struct sackhole *p; 146b3c0f300SAndre Oppermann int tso = 0; 14702a1a643SAndre Oppermann struct tcpopt to; 148262c1c1aSMatthew Dillon #if 0 14946f58482SJonathan Lemon int maxburst = TCP_MAXBURST; 150262c1c1aSMatthew Dillon #endif 151fb59c426SYoshinobu Inoue #ifdef INET6 152f10e85d7SLuigi Rizzo struct ip6_hdr *ip6 = NULL; 153fb59c426SYoshinobu Inoue int isipv6; 154fb59c426SYoshinobu Inoue 155fb59c426SYoshinobu Inoue isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 156fb59c426SYoshinobu Inoue #endif 157df8bae1dSRodney W. Grimes 158fa286d7dSSam Leffler INP_LOCK_ASSERT(tp->t_inpcb); 1593d6ade3aSJennifer Yang 160df8bae1dSRodney W. Grimes /* 161df8bae1dSRodney W. Grimes * Determine length of data that should be transmitted, 162df8bae1dSRodney W. Grimes * and flags that will be used. 163df8bae1dSRodney W. Grimes * If there is some data or critical controls (SYN, RST) 164df8bae1dSRodney W. Grimes * to send, then transmit; otherwise, investigate further. 165df8bae1dSRodney W. Grimes */ 166c24d5daeSJayanth Vijayaraghavan idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 1679b8b58e0SJonathan Lemon if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) { 168df8bae1dSRodney W. Grimes /* 169df8bae1dSRodney W. Grimes * We have been idle for "a while" and no acks are 170df8bae1dSRodney W. Grimes * expected to clock out any data we send -- 171df8bae1dSRodney W. Grimes * slow start to get ack "clock" running again. 1729b8b58e0SJonathan Lemon * 1739b8b58e0SJonathan Lemon * Set the slow-start flight size depending on whether 1749b8b58e0SJonathan Lemon * this is a local network or not. 175df8bae1dSRodney W. Grimes */ 176f10e85d7SLuigi Rizzo int ss = ss_fltsz; 177fb59c426SYoshinobu Inoue #ifdef INET6 178f10e85d7SLuigi Rizzo if (isipv6) { 179f10e85d7SLuigi Rizzo if (in6_localaddr(&tp->t_inpcb->in6p_faddr)) 180f10e85d7SLuigi Rizzo ss = ss_fltsz_local; 181f10e85d7SLuigi Rizzo } else 182f10e85d7SLuigi Rizzo #endif /* INET6 */ 183f10e85d7SLuigi Rizzo if (in_localaddr(tp->t_inpcb->inp_faddr)) 184f10e85d7SLuigi Rizzo ss = ss_fltsz_local; 185f10e85d7SLuigi Rizzo tp->snd_cwnd = tp->t_maxseg * ss; 1869b8b58e0SJonathan Lemon } 187c24d5daeSJayanth Vijayaraghavan tp->t_flags &= ~TF_LASTIDLE; 188c24d5daeSJayanth Vijayaraghavan if (idle) { 189c24d5daeSJayanth Vijayaraghavan if (tp->t_flags & TF_MORETOCOME) { 190c24d5daeSJayanth Vijayaraghavan tp->t_flags |= TF_LASTIDLE; 191c24d5daeSJayanth Vijayaraghavan idle = 0; 192c24d5daeSJayanth Vijayaraghavan } 193c24d5daeSJayanth Vijayaraghavan } 194df8bae1dSRodney W. Grimes again: 1956d90faf3SPaul Saab /* 1966d90faf3SPaul Saab * If we've recently taken a timeout, snd_max will be greater than 1976d90faf3SPaul Saab * snd_nxt. There may be SACK information that allows us to avoid 1986d90faf3SPaul Saab * resending already delivered data. Adjust snd_nxt accordingly. 1996d90faf3SPaul Saab */ 2006d90faf3SPaul Saab if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max)) 2016d90faf3SPaul Saab tcp_sack_adjust(tp); 202df8bae1dSRodney W. Grimes sendalot = 0; 203df8bae1dSRodney W. Grimes off = tp->snd_nxt - tp->snd_una; 204201d185bSAndre Oppermann sendwin = min(tp->snd_wnd, tp->snd_cwnd); 205201d185bSAndre Oppermann sendwin = min(sendwin, tp->snd_bwnd); 206df8bae1dSRodney W. Grimes 207df8bae1dSRodney W. Grimes flags = tcp_outflags[tp->t_state]; 208a0292f23SGarrett Wollman /* 2096d90faf3SPaul Saab * Send any SACK-generated retransmissions. If we're explicitly trying 2106d90faf3SPaul Saab * to send out new data (when sendalot is 1), bypass this function. 2116d90faf3SPaul Saab * If we retransmit in fast recovery mode, decrement snd_cwnd, since 2126d90faf3SPaul Saab * we're replacing a (future) new transmission with a retransmission 2136d90faf3SPaul Saab * now, and we previously incremented snd_cwnd in tcp_input(). 2146d90faf3SPaul Saab */ 2156d90faf3SPaul Saab /* 2166d90faf3SPaul Saab * Still in sack recovery , reset rxmit flag to zero. 2176d90faf3SPaul Saab */ 2186d90faf3SPaul Saab sack_rxmit = 0; 219a55db2b6SPaul Saab sack_bytes_rxmt = 0; 2206d90faf3SPaul Saab len = 0; 2216d90faf3SPaul Saab p = NULL; 22204f0d9a0SJayanth Vijayaraghavan if (tp->sack_enable && IN_FASTRECOVERY(tp) && 223a55db2b6SPaul Saab (p = tcp_sack_output(tp, &sack_bytes_rxmt))) { 224a55db2b6SPaul Saab long cwin; 225a55db2b6SPaul Saab 226a55db2b6SPaul Saab cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt; 227a55db2b6SPaul Saab if (cwin < 0) 228a55db2b6SPaul Saab cwin = 0; 229f787edd8SJayanth Vijayaraghavan /* Do not retransmit SACK segments beyond snd_recover */ 230f787edd8SJayanth Vijayaraghavan if (SEQ_GT(p->end, tp->snd_recover)) { 231f787edd8SJayanth Vijayaraghavan /* 232f787edd8SJayanth Vijayaraghavan * (At least) part of sack hole extends beyond 233f787edd8SJayanth Vijayaraghavan * snd_recover. Check to see if we can rexmit data 234f787edd8SJayanth Vijayaraghavan * for this hole. 235f787edd8SJayanth Vijayaraghavan */ 236f787edd8SJayanth Vijayaraghavan if (SEQ_GEQ(p->rxmit, tp->snd_recover)) { 237f787edd8SJayanth Vijayaraghavan /* 238f787edd8SJayanth Vijayaraghavan * Can't rexmit any more data for this hole. 239f787edd8SJayanth Vijayaraghavan * That data will be rexmitted in the next 240f787edd8SJayanth Vijayaraghavan * sack recovery episode, when snd_recover 241f787edd8SJayanth Vijayaraghavan * moves past p->rxmit. 242f787edd8SJayanth Vijayaraghavan */ 243f787edd8SJayanth Vijayaraghavan p = NULL; 244f787edd8SJayanth Vijayaraghavan goto after_sack_rexmit; 245f787edd8SJayanth Vijayaraghavan } else 246f787edd8SJayanth Vijayaraghavan /* Can rexmit part of the current hole */ 247a55db2b6SPaul Saab len = ((long)ulmin(cwin, 248f787edd8SJayanth Vijayaraghavan tp->snd_recover - p->rxmit)); 249f787edd8SJayanth Vijayaraghavan } else 250a55db2b6SPaul Saab len = ((long)ulmin(cwin, p->end - p->rxmit)); 2516d90faf3SPaul Saab off = p->rxmit - tp->snd_una; 252f787edd8SJayanth Vijayaraghavan KASSERT(off >= 0,("%s: sack block to the left of una : %d", 253f787edd8SJayanth Vijayaraghavan __func__, off)); 2546d90faf3SPaul Saab if (len > 0) { 255ab5c14d8SRobert Watson sack_rxmit = 1; 256ab5c14d8SRobert Watson sendalot = 1; 2576d90faf3SPaul Saab tcpstat.tcps_sack_rexmits++; 2586d90faf3SPaul Saab tcpstat.tcps_sack_rexmit_bytes += 2596d90faf3SPaul Saab min(len, tp->t_maxseg); 2606d90faf3SPaul Saab } 2616d90faf3SPaul Saab } 262f787edd8SJayanth Vijayaraghavan after_sack_rexmit: 2636d90faf3SPaul Saab /* 264a0292f23SGarrett Wollman * Get standard flags, and add SYN or FIN if requested by 'hidden' 265a0292f23SGarrett Wollman * state flags. 266a0292f23SGarrett Wollman */ 267a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) 268a0292f23SGarrett Wollman flags |= TH_FIN; 269a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) 270a0292f23SGarrett Wollman flags |= TH_SYN; 271a0292f23SGarrett Wollman 272cf2942b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 273df8bae1dSRodney W. Grimes /* 274df8bae1dSRodney W. Grimes * If in persist timeout with window of 0, send 1 byte. 275df8bae1dSRodney W. Grimes * Otherwise, if window is small but nonzero 276df8bae1dSRodney W. Grimes * and timer expired, we will send what we can 277df8bae1dSRodney W. Grimes * and go to transmit state. 278df8bae1dSRodney W. Grimes */ 2792cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) { 280201d185bSAndre Oppermann if (sendwin == 0) { 281df8bae1dSRodney W. Grimes /* 282df8bae1dSRodney W. Grimes * If we still have some data to send, then 283df8bae1dSRodney W. Grimes * clear the FIN bit. Usually this would 284df8bae1dSRodney W. Grimes * happen below when it realizes that we 285df8bae1dSRodney W. Grimes * aren't sending all the data. However, 28629089b51SJulian Elischer * if we have exactly 1 byte of unsent data, 287df8bae1dSRodney W. Grimes * then it won't clear the FIN bit below, 288df8bae1dSRodney W. Grimes * and if we are in persist state, we wind 289df8bae1dSRodney W. Grimes * up sending the packet without recording 290df8bae1dSRodney W. Grimes * that we sent the FIN bit. 291df8bae1dSRodney W. Grimes * 292df8bae1dSRodney W. Grimes * We can't just blindly clear the FIN bit, 293df8bae1dSRodney W. Grimes * because if we don't have any more data 294df8bae1dSRodney W. Grimes * to send then the probe will be the FIN 295df8bae1dSRodney W. Grimes * itself. 296df8bae1dSRodney W. Grimes */ 297df8bae1dSRodney W. Grimes if (off < so->so_snd.sb_cc) 298df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 299201d185bSAndre Oppermann sendwin = 1; 300df8bae1dSRodney W. Grimes } else { 301b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, 0); 302df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 303df8bae1dSRodney W. Grimes } 304df8bae1dSRodney W. Grimes } 305df8bae1dSRodney W. Grimes 30628257b5cSMatthew Dillon /* 30728257b5cSMatthew Dillon * If snd_nxt == snd_max and we have transmitted a FIN, the 30828257b5cSMatthew Dillon * offset will be > 0 even if so_snd.sb_cc is 0, resulting in 309201d185bSAndre Oppermann * a negative length. This can also occur when TCP opens up 31028257b5cSMatthew Dillon * its congestion window while receiving additional duplicate 31128257b5cSMatthew Dillon * acks after fast-retransmit because TCP will reset snd_nxt 31228257b5cSMatthew Dillon * to snd_max after the fast-retransmit. 31328257b5cSMatthew Dillon * 31428257b5cSMatthew Dillon * In the normal retransmit-FIN-only case, however, snd_nxt will 31528257b5cSMatthew Dillon * be set to snd_una, the offset will be 0, and the length may 31628257b5cSMatthew Dillon * wind up 0. 3176d90faf3SPaul Saab * 3186d90faf3SPaul Saab * If sack_rxmit is true we are retransmitting from the scoreboard 3196d90faf3SPaul Saab * in which case len is already set. 32028257b5cSMatthew Dillon */ 321a55db2b6SPaul Saab if (sack_rxmit == 0) { 322a55db2b6SPaul Saab if (sack_bytes_rxmt == 0) 3236d90faf3SPaul Saab len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off); 324a55db2b6SPaul Saab else { 325a55db2b6SPaul Saab long cwin; 326a55db2b6SPaul Saab 327a55db2b6SPaul Saab /* 328a55db2b6SPaul Saab * We are inside of a SACK recovery episode and are 329a55db2b6SPaul Saab * sending new data, having retransmitted all the 330a55db2b6SPaul Saab * data possible in the scoreboard. 331a55db2b6SPaul Saab */ 3327d5ed1ceSPaul Saab len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd) 3337d5ed1ceSPaul Saab - off); 3348d03f2b5SPaul Saab /* 3358d03f2b5SPaul Saab * Don't remove this (len > 0) check ! 3368d03f2b5SPaul Saab * We explicitly check for len > 0 here (although it 3378d03f2b5SPaul Saab * isn't really necessary), to work around a gcc 3388d03f2b5SPaul Saab * optimization issue - to force gcc to compute 3398d03f2b5SPaul Saab * len above. Without this check, the computation 3408d03f2b5SPaul Saab * of len is bungled by the optimizer. 3418d03f2b5SPaul Saab */ 3428d03f2b5SPaul Saab if (len > 0) { 3437d5ed1ceSPaul Saab cwin = tp->snd_cwnd - 3447d5ed1ceSPaul Saab (tp->snd_nxt - tp->sack_newdata) - 345a55db2b6SPaul Saab sack_bytes_rxmt; 346a55db2b6SPaul Saab if (cwin < 0) 347a55db2b6SPaul Saab cwin = 0; 348a55db2b6SPaul Saab len = lmin(len, cwin); 349a55db2b6SPaul Saab } 350a55db2b6SPaul Saab } 3518d03f2b5SPaul Saab } 352a0292f23SGarrett Wollman 353a0292f23SGarrett Wollman /* 354a0292f23SGarrett Wollman * Lop off SYN bit if it has already been sent. However, if this 355a0292f23SGarrett Wollman * is SYN-SENT state and if segment contains data and if we don't 356a0292f23SGarrett Wollman * know that foreign host supports TAO, suppress sending segment. 357a0292f23SGarrett Wollman */ 358a0292f23SGarrett Wollman if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 3594b8e98d6SQing Li if (tp->t_state != TCPS_SYN_RECEIVED) 360a0292f23SGarrett Wollman flags &= ~TH_SYN; 361a0292f23SGarrett Wollman off--, len++; 362a0292f23SGarrett Wollman } 363a0292f23SGarrett Wollman 36481165e48SAndras Olah /* 365c94c54e4SAndre Oppermann * Be careful not to send data and/or FIN on SYN segments. 36681165e48SAndras Olah * This measure is needed to prevent interoperability problems 36781165e48SAndras Olah * with not fully conformant TCP implementations. 36881165e48SAndras Olah */ 369c94c54e4SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 37081165e48SAndras Olah len = 0; 37181165e48SAndras Olah flags &= ~TH_FIN; 37281165e48SAndras Olah } 37381165e48SAndras Olah 374df8bae1dSRodney W. Grimes if (len < 0) { 375df8bae1dSRodney W. Grimes /* 376df8bae1dSRodney W. Grimes * If FIN has been sent but not acked, 377df8bae1dSRodney W. Grimes * but we haven't been called to retransmit, 37828257b5cSMatthew Dillon * len will be < 0. Otherwise, window shrank 379df8bae1dSRodney W. Grimes * after we sent into it. If window shrank to 0, 3802d8266afSDavid Greenman * cancel pending retransmit, pull snd_nxt back 3812d8266afSDavid Greenman * to (closed) window, and set the persist timer 3822d8266afSDavid Greenman * if it isn't already going. If the window didn't 3832d8266afSDavid Greenman * close completely, just wait for an ACK. 384df8bae1dSRodney W. Grimes */ 385df8bae1dSRodney W. Grimes len = 0; 386201d185bSAndre Oppermann if (sendwin == 0) { 387b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 3882d8266afSDavid Greenman tp->t_rxtshift = 0; 389df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 390b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_PERSIST)) 3912d8266afSDavid Greenman tcp_setpersist(tp); 392df8bae1dSRodney W. Grimes } 393df8bae1dSRodney W. Grimes } 39428257b5cSMatthew Dillon 3956741ecf5SAndre Oppermann /* len will be >= 0 after this point. */ 3966741ecf5SAndre Oppermann KASSERT(len >= 0, ("%s: len < 0", __func__)); 3976741ecf5SAndre Oppermann 39828257b5cSMatthew Dillon /* 3996741ecf5SAndre Oppermann * Automatic sizing of send socket buffer. Often the send buffer 4006741ecf5SAndre Oppermann * size is not optimally adjusted to the actual network conditions 4016741ecf5SAndre Oppermann * at hand (delay bandwidth product). Setting the buffer size too 4026741ecf5SAndre Oppermann * small limits throughput on links with high bandwidth and high 4036741ecf5SAndre Oppermann * delay (eg. trans-continental/oceanic links). Setting the 4046741ecf5SAndre Oppermann * buffer size too big consumes too much real kernel memory, 4056741ecf5SAndre Oppermann * especially with many connections on busy servers. 4066741ecf5SAndre Oppermann * 4076741ecf5SAndre Oppermann * The criteria to step up the send buffer one notch are: 4086741ecf5SAndre Oppermann * 1. receive window of remote host is larger than send buffer 4096741ecf5SAndre Oppermann * (with a fudge factor of 5/4th); 4106741ecf5SAndre Oppermann * 2. send buffer is filled to 7/8th with data (so we actually 4116741ecf5SAndre Oppermann * have data to make use of it); 4126741ecf5SAndre Oppermann * 3. send buffer fill has not hit maximal automatic size; 4136741ecf5SAndre Oppermann * 4. our send window (slow start and cogestion controlled) is 4146741ecf5SAndre Oppermann * larger than sent but unacknowledged data in send buffer. 4156741ecf5SAndre Oppermann * 4166741ecf5SAndre Oppermann * The remote host receive window scaling factor may limit the 4176741ecf5SAndre Oppermann * growing of the send buffer before it reaches its allowed 4186741ecf5SAndre Oppermann * maximum. 4196741ecf5SAndre Oppermann * 4206741ecf5SAndre Oppermann * It scales directly with slow start or congestion window 4216741ecf5SAndre Oppermann * and does at most one step per received ACK. This fast 4226741ecf5SAndre Oppermann * scaling has the drawback of growing the send buffer beyond 4236741ecf5SAndre Oppermann * what is strictly necessary to make full use of a given 4246741ecf5SAndre Oppermann * delay*bandwith product. However testing has shown this not 4256741ecf5SAndre Oppermann * to be much of an problem. At worst we are trading wasting 4266741ecf5SAndre Oppermann * of available bandwith (the non-use of it) for wasting some 4276741ecf5SAndre Oppermann * socket buffer memory. 4286741ecf5SAndre Oppermann * 4296741ecf5SAndre Oppermann * TODO: Shrink send buffer during idle periods together 4306741ecf5SAndre Oppermann * with congestion window. Requires another timer. Has to 4316741ecf5SAndre Oppermann * wait for upcoming tcp timer rewrite. 4326741ecf5SAndre Oppermann */ 4336741ecf5SAndre Oppermann if (tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) { 4346741ecf5SAndre Oppermann if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat && 4356741ecf5SAndre Oppermann so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) && 4366741ecf5SAndre Oppermann so->so_snd.sb_cc < tcp_autosndbuf_max && 4376741ecf5SAndre Oppermann sendwin >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) { 4386741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_snd, 4396741ecf5SAndre Oppermann min(so->so_snd.sb_hiwat + tcp_autosndbuf_inc, 4406741ecf5SAndre Oppermann tcp_autosndbuf_max), so, curthread)) 4416741ecf5SAndre Oppermann so->so_snd.sb_flags &= ~SB_AUTOSIZE; 4426741ecf5SAndre Oppermann } 4436741ecf5SAndre Oppermann } 4446741ecf5SAndre Oppermann 4456741ecf5SAndre Oppermann /* 4466741ecf5SAndre Oppermann * Truncate to the maximum segment length or enable TCP Segmentation 4476741ecf5SAndre Oppermann * Offloading (if supported by hardware) and ensure that FIN is removed 4486741ecf5SAndre Oppermann * if the length no longer contains the last data byte. 449b3c0f300SAndre Oppermann * 450b3c0f300SAndre Oppermann * TSO may only be used if we are in a pure bulk sending state. The 451b3c0f300SAndre Oppermann * presence of TCP-MD5, SACK retransmits, SACK advertizements and 452b3c0f300SAndre Oppermann * IP options prevent using TSO. With TSO the TCP header is the same 453b3c0f300SAndre Oppermann * (except for the sequence number) for all generated packets. This 454b3c0f300SAndre Oppermann * makes it impossible to transmit any options which vary per generated 455b3c0f300SAndre Oppermann * segment or packet. 456b3c0f300SAndre Oppermann * 457b3c0f300SAndre Oppermann * The length of TSO bursts is limited to TCP_MAXWIN. That limit and 458b3c0f300SAndre Oppermann * removal of FIN (if not already catched here) are handled later after 459b3c0f300SAndre Oppermann * the exact length of the TCP options are known. 46028257b5cSMatthew Dillon */ 461df8bae1dSRodney W. Grimes if (len > tp->t_maxseg) { 462b3c0f300SAndre Oppermann if ((tp->t_flags & TF_TSO) && tcp_do_tso && 463b3c0f300SAndre Oppermann ((tp->t_flags & TF_SIGNATURE) == 0) && 464b3c0f300SAndre Oppermann tp->rcv_numsacks == 0 && sack_rxmit == 0 && 465b3c0f300SAndre Oppermann tp->t_inpcb->inp_options == NULL && 466b3c0f300SAndre Oppermann tp->t_inpcb->in6p_options == NULL && 467b3c0f300SAndre Oppermann tp->t_inpcb->inp_sp == NULL) { 468b3c0f300SAndre Oppermann tso = 1; 469b3c0f300SAndre Oppermann } else { 470df8bae1dSRodney W. Grimes len = tp->t_maxseg; 471df8bae1dSRodney W. Grimes sendalot = 1; 472b3c0f300SAndre Oppermann tso = 0; 473b3c0f300SAndre Oppermann } 474df8bae1dSRodney W. Grimes } 4755d3b1b75SJayanth Vijayaraghavan if (sack_rxmit) { 4765d3b1b75SJayanth Vijayaraghavan if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc)) 477df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 4785d3b1b75SJayanth Vijayaraghavan } else { 4795d3b1b75SJayanth Vijayaraghavan if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 4805d3b1b75SJayanth Vijayaraghavan flags &= ~TH_FIN; 4815d3b1b75SJayanth Vijayaraghavan } 482df8bae1dSRodney W. Grimes 483201d185bSAndre Oppermann recwin = sbspace(&so->so_rcv); 484df8bae1dSRodney W. Grimes 485df8bae1dSRodney W. Grimes /* 486262c1c1aSMatthew Dillon * Sender silly window avoidance. We transmit under the following 487262c1c1aSMatthew Dillon * conditions when len is non-zero: 488262c1c1aSMatthew Dillon * 489b3c0f300SAndre Oppermann * - We have a full segment (or more with TSO) 490262c1c1aSMatthew Dillon * - This is the last buffer in a write()/send() and we are 491262c1c1aSMatthew Dillon * either idle or running NODELAY 492262c1c1aSMatthew Dillon * - we've timed out (e.g. persist timer) 493262c1c1aSMatthew Dillon * - we have more then 1/2 the maximum send window's worth of 494262c1c1aSMatthew Dillon * data (receiver may be limited the window size) 495262c1c1aSMatthew Dillon * - we need to retransmit 496df8bae1dSRodney W. Grimes */ 497df8bae1dSRodney W. Grimes if (len) { 498b3c0f300SAndre Oppermann if (len >= tp->t_maxseg) 499df8bae1dSRodney W. Grimes goto send; 500262c1c1aSMatthew Dillon /* 501262c1c1aSMatthew Dillon * NOTE! on localhost connections an 'ack' from the remote 502262c1c1aSMatthew Dillon * end may occur synchronously with the output and cause 503262c1c1aSMatthew Dillon * us to flush a buffer queued with moretocome. XXX 504262c1c1aSMatthew Dillon * 505262c1c1aSMatthew Dillon * note: the len + off check is almost certainly unnecessary. 506262c1c1aSMatthew Dillon */ 507262c1c1aSMatthew Dillon if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 508262c1c1aSMatthew Dillon (idle || (tp->t_flags & TF_NODELAY)) && 509262c1c1aSMatthew Dillon len + off >= so->so_snd.sb_cc && 510262c1c1aSMatthew Dillon (tp->t_flags & TF_NOPUSH) == 0) { 511df8bae1dSRodney W. Grimes goto send; 512262c1c1aSMatthew Dillon } 5132cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) /* typ. timeout case */ 514df8bae1dSRodney W. Grimes goto send; 515a0292f23SGarrett Wollman if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 516df8bae1dSRodney W. Grimes goto send; 517262c1c1aSMatthew Dillon if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */ 518df8bae1dSRodney W. Grimes goto send; 5196d90faf3SPaul Saab if (sack_rxmit) 5206d90faf3SPaul Saab goto send; 521df8bae1dSRodney W. Grimes } 522df8bae1dSRodney W. Grimes 523df8bae1dSRodney W. Grimes /* 524df8bae1dSRodney W. Grimes * Compare available window to amount of window 525df8bae1dSRodney W. Grimes * known to peer (as advertised window less 526df8bae1dSRodney W. Grimes * next expected input). If the difference is at least two 527df8bae1dSRodney W. Grimes * max size segments, or at least 50% of the maximum possible 528df8bae1dSRodney W. Grimes * window, then want to send a window update to peer. 5293bfd6421SJonathan Lemon * Skip this if the connection is in T/TCP half-open state. 530df8bae1dSRodney W. Grimes */ 531201d185bSAndre Oppermann if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN)) { 532df8bae1dSRodney W. Grimes /* 533df8bae1dSRodney W. Grimes * "adv" is the amount we can increase the window, 534df8bae1dSRodney W. Grimes * taking into account that we are limited by 535df8bae1dSRodney W. Grimes * TCP_MAXWIN << tp->rcv_scale. 536df8bae1dSRodney W. Grimes */ 537201d185bSAndre Oppermann long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) - 538df8bae1dSRodney W. Grimes (tp->rcv_adv - tp->rcv_nxt); 539df8bae1dSRodney W. Grimes 540df8bae1dSRodney W. Grimes if (adv >= (long) (2 * tp->t_maxseg)) 541df8bae1dSRodney W. Grimes goto send; 542df8bae1dSRodney W. Grimes if (2 * adv >= (long) so->so_rcv.sb_hiwat) 543df8bae1dSRodney W. Grimes goto send; 544df8bae1dSRodney W. Grimes } 545df8bae1dSRodney W. Grimes 546df8bae1dSRodney W. Grimes /* 54728257b5cSMatthew Dillon * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 54828257b5cSMatthew Dillon * is also a catch-all for the retransmit timer timeout case. 549df8bae1dSRodney W. Grimes */ 550df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 551df8bae1dSRodney W. Grimes goto send; 552a0292f23SGarrett Wollman if ((flags & TH_RST) || 553a0292f23SGarrett Wollman ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 554df8bae1dSRodney W. Grimes goto send; 555df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_una)) 556df8bae1dSRodney W. Grimes goto send; 557df8bae1dSRodney W. Grimes /* 558df8bae1dSRodney W. Grimes * If our state indicates that FIN should be sent 55928257b5cSMatthew Dillon * and we have not yet done so, then we need to send. 560df8bae1dSRodney W. Grimes */ 561df8bae1dSRodney W. Grimes if (flags & TH_FIN && 562df8bae1dSRodney W. Grimes ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 563df8bae1dSRodney W. Grimes goto send; 5646d90faf3SPaul Saab /* 5656d90faf3SPaul Saab * In SACK, it is possible for tcp_output to fail to send a segment 5666d90faf3SPaul Saab * after the retransmission timer has been turned off. Make sure 5676d90faf3SPaul Saab * that the retransmission timer is set. 5686d90faf3SPaul Saab */ 5696d90faf3SPaul Saab if (tp->sack_enable && SEQ_GT(tp->snd_max, tp->snd_una) && 570b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_REXMT) && 571b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 572b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 573cf2942b6SRobert Watson goto just_return; 5746d90faf3SPaul Saab } 575df8bae1dSRodney W. Grimes /* 576df8bae1dSRodney W. Grimes * TCP window updates are not reliable, rather a polling protocol 577df8bae1dSRodney W. Grimes * using ``persist'' packets is used to insure receipt of window 578df8bae1dSRodney W. Grimes * updates. The three ``states'' for the output side are: 579df8bae1dSRodney W. Grimes * idle not doing retransmits or persists 580df8bae1dSRodney W. Grimes * persisting to move a small or zero window 581df8bae1dSRodney W. Grimes * (re)transmitting and thereby not persisting 582df8bae1dSRodney W. Grimes * 583b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_PERSIST) 5849b8b58e0SJonathan Lemon * is true when we are in persist state. 5852cdbfa66SPaul Saab * (tp->t_flags & TF_FORCEDATA) 586df8bae1dSRodney W. Grimes * is set when we are called to send a persist packet. 587b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_REXMT) 588df8bae1dSRodney W. Grimes * is set when we are retransmitting 589df8bae1dSRodney W. Grimes * The output side is idle when both timers are zero. 590df8bae1dSRodney W. Grimes * 591df8bae1dSRodney W. Grimes * If send window is too small, there is data to transmit, and no 592df8bae1dSRodney W. Grimes * retransmit or persist is pending, then go to persist state. 593df8bae1dSRodney W. Grimes * If nothing happens soon, send when timer expires: 594df8bae1dSRodney W. Grimes * if window is nonzero, transmit what we can, 595df8bae1dSRodney W. Grimes * otherwise force out a byte. 596df8bae1dSRodney W. Grimes */ 597b8152ba7SAndre Oppermann if (so->so_snd.sb_cc && !tcp_timer_active(tp, TT_REXMT) && 598b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 599df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 600df8bae1dSRodney W. Grimes tcp_setpersist(tp); 601df8bae1dSRodney W. Grimes } 602df8bae1dSRodney W. Grimes 603df8bae1dSRodney W. Grimes /* 604df8bae1dSRodney W. Grimes * No reason to send a segment, just return. 605df8bae1dSRodney W. Grimes */ 606cf2942b6SRobert Watson just_return: 607cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 608df8bae1dSRodney W. Grimes return (0); 609df8bae1dSRodney W. Grimes 610df8bae1dSRodney W. Grimes send: 611cf2942b6SRobert Watson SOCKBUF_LOCK_ASSERT(&so->so_snd); 612df8bae1dSRodney W. Grimes /* 613df8bae1dSRodney W. Grimes * Before ESTABLISHED, force sending of initial options 614df8bae1dSRodney W. Grimes * unless TCP set not to do any options. 615df8bae1dSRodney W. Grimes * NOTE: we assume that the IP/TCP header plus TCP options 616df8bae1dSRodney W. Grimes * always fit in a single mbuf, leaving room for a maximum 617df8bae1dSRodney W. Grimes * link header, i.e. 61833841545SHajimu UMEMOTO * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 619df8bae1dSRodney W. Grimes */ 620df8bae1dSRodney W. Grimes optlen = 0; 621fb59c426SYoshinobu Inoue #ifdef INET6 622fb59c426SYoshinobu Inoue if (isipv6) 623fb59c426SYoshinobu Inoue hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 624fb59c426SYoshinobu Inoue else 625fb59c426SYoshinobu Inoue #endif 626df8bae1dSRodney W. Grimes hdrlen = sizeof (struct tcpiphdr); 62702a1a643SAndre Oppermann 62802a1a643SAndre Oppermann /* 62902a1a643SAndre Oppermann * Compute options for segment. 63002a1a643SAndre Oppermann * We only have to care about SYN and established connection 63102a1a643SAndre Oppermann * segments. Options for SYN-ACK segments are handled in TCP 63202a1a643SAndre Oppermann * syncache. 63302a1a643SAndre Oppermann */ 63402a1a643SAndre Oppermann if ((tp->t_flags & TF_NOOPT) == 0) { 63502a1a643SAndre Oppermann to.to_flags = 0; 63602a1a643SAndre Oppermann /* Maximum segment size. */ 637df8bae1dSRodney W. Grimes if (flags & TH_SYN) { 638df8bae1dSRodney W. Grimes tp->snd_nxt = tp->iss; 63902a1a643SAndre Oppermann to.to_mss = tcp_mssopt(&tp->t_inpcb->inp_inc); 64002a1a643SAndre Oppermann to.to_flags |= TOF_MSS; 641df8bae1dSRodney W. Grimes } 64202a1a643SAndre Oppermann /* Window scaling. */ 64302a1a643SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 64402a1a643SAndre Oppermann to.to_wscale = tp->request_r_scale; 64502a1a643SAndre Oppermann to.to_flags |= TOF_SCALE; 646df8bae1dSRodney W. Grimes } 64702a1a643SAndre Oppermann /* Timestamps. */ 64802a1a643SAndre Oppermann if ((tp->t_flags & TF_RCVD_TSTMP) || 64902a1a643SAndre Oppermann ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 65002a1a643SAndre Oppermann to.to_tsval = ticks + tp->ts_offset; 65102a1a643SAndre Oppermann to.to_tsecr = tp->ts_recent; 65202a1a643SAndre Oppermann to.to_flags |= TOF_TS; 6536741ecf5SAndre Oppermann /* Set receive buffer autosizing timestamp. */ 65402a1a643SAndre Oppermann if (tp->rfbuf_ts == 0 && 65502a1a643SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) 6566741ecf5SAndre Oppermann tp->rfbuf_ts = ticks; 6571cfd4b53SBruce M Simpson } 65802a1a643SAndre Oppermann /* Selective ACK's. */ 65902a1a643SAndre Oppermann if (tp->sack_enable) { 66002a1a643SAndre Oppermann if (flags & TH_SYN) 66102a1a643SAndre Oppermann to.to_flags |= TOF_SACKPERM; 66202a1a643SAndre Oppermann else if (TCPS_HAVEESTABLISHED(tp->t_state) && 66302a1a643SAndre Oppermann (tp->t_flags & TF_SACK_PERMIT) && 66402a1a643SAndre Oppermann tp->rcv_numsacks > 0) { 66502a1a643SAndre Oppermann to.to_flags |= TOF_SACK; 66602a1a643SAndre Oppermann to.to_nsacks = tp->rcv_numsacks; 66702a1a643SAndre Oppermann to.to_sacks = (u_char *)tp->sackblks; 66802a1a643SAndre Oppermann } 66902a1a643SAndre Oppermann } 67002a1a643SAndre Oppermann #ifdef TCP_SIGNATURE 67102a1a643SAndre Oppermann /* TCP-MD5 (RFC2385). */ 67202a1a643SAndre Oppermann #ifdef INET6 67302a1a643SAndre Oppermann if (!isipv6 && (tp->t_flags & TF_SIGNATURE)) 67402a1a643SAndre Oppermann #else 67502a1a643SAndre Oppermann if (tp->t_flags & TF_SIGNATURE) 67602a1a643SAndre Oppermann #endif /* INET6 */ 67702a1a643SAndre Oppermann to.to_flags |= TOF_SIGNATURE; 6781cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 6791cfd4b53SBruce M Simpson 68002a1a643SAndre Oppermann /* Processing the options. */ 68102a1a643SAndre Oppermann hdrlen += optlen = tcp_addoptions(&to, (u_char *)&opt); 682be3f3b5eSPaul Saab 68302a1a643SAndre Oppermann #ifdef TCP_SIGNATURE 68402a1a643SAndre Oppermann sigoff = to.to_signature - (u_char *)&to; 68502a1a643SAndre Oppermann #endif /* TCP_SIGNATURE */ 686be3f3b5eSPaul Saab } 687be3f3b5eSPaul Saab 688fb59c426SYoshinobu Inoue #ifdef INET6 689fb59c426SYoshinobu Inoue if (isipv6) 690fb59c426SYoshinobu Inoue ipoptlen = ip6_optlen(tp->t_inpcb); 691fb59c426SYoshinobu Inoue else 692fb59c426SYoshinobu Inoue #endif 693f10e85d7SLuigi Rizzo if (tp->t_inpcb->inp_options) 694a04884fcSBill Fenner ipoptlen = tp->t_inpcb->inp_options->m_len - 695a04884fcSBill Fenner offsetof(struct ipoption, ipopt_list); 696f10e85d7SLuigi Rizzo else 697a04884fcSBill Fenner ipoptlen = 0; 698fb59c426SYoshinobu Inoue #ifdef IPSEC 699fb59c426SYoshinobu Inoue ipoptlen += ipsec_hdrsiz_tcp(tp); 700fb59c426SYoshinobu Inoue #endif 701a04884fcSBill Fenner 702df8bae1dSRodney W. Grimes /* 703df8bae1dSRodney W. Grimes * Adjust data length if insertion of options will 704a0292f23SGarrett Wollman * bump the packet length beyond the t_maxopd length. 705a0292f23SGarrett Wollman * Clear the FIN bit because we cut off the tail of 706a0292f23SGarrett Wollman * the segment. 707b3c0f300SAndre Oppermann * 70831ecb34aSAndre Oppermann * When doing TSO limit a burst to TCP_MAXWIN minus the 70931ecb34aSAndre Oppermann * IP, TCP and Options length to keep ip->ip_len from 71031ecb34aSAndre Oppermann * overflowing. Prevent the last segment from being 71131ecb34aSAndre Oppermann * fractional thus making them all equal sized and set 7126aa5b623SAndre Oppermann * the flag to continue sending. TSO is disabled when 7136aa5b623SAndre Oppermann * IP options or IPSEC are present. 714df8bae1dSRodney W. Grimes */ 715a04884fcSBill Fenner if (len + optlen + ipoptlen > tp->t_maxopd) { 716297a37f3SDavid Greenman flags &= ~TH_FIN; 717b3c0f300SAndre Oppermann if (tso) { 718eec9d82dSAndre Oppermann if (len > TCP_MAXWIN - hdrlen - optlen) { 7196aa5b623SAndre Oppermann len = TCP_MAXWIN - hdrlen - optlen; 72031ecb34aSAndre Oppermann len = len - (len % (tp->t_maxopd - optlen)); 721b3c0f300SAndre Oppermann sendalot = 1; 722b3c0f300SAndre Oppermann } else if (tp->t_flags & TF_NEEDFIN) 723b3c0f300SAndre Oppermann sendalot = 1; 724b3c0f300SAndre Oppermann } else { 725a04884fcSBill Fenner len = tp->t_maxopd - optlen - ipoptlen; 726df8bae1dSRodney W. Grimes sendalot = 1; 727df8bae1dSRodney W. Grimes } 728b3c0f300SAndre Oppermann } 729df8bae1dSRodney W. Grimes 730a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/ 731a683a7ddSYoshinobu Inoue #ifdef INET6 732a683a7ddSYoshinobu Inoue if (max_linkhdr + hdrlen > MCLBYTES) 733a683a7ddSYoshinobu Inoue #else 734df8bae1dSRodney W. Grimes if (max_linkhdr + hdrlen > MHLEN) 735a683a7ddSYoshinobu Inoue #endif 736f10e85d7SLuigi Rizzo panic("tcphdr too big"); 737a0292f23SGarrett Wollman /*#endif*/ 738df8bae1dSRodney W. Grimes 739df8bae1dSRodney W. Grimes /* 740df8bae1dSRodney W. Grimes * Grab a header mbuf, attaching a copy of data to 741df8bae1dSRodney W. Grimes * be transmitted, and initialize the header from 742df8bae1dSRodney W. Grimes * the template for sends on this connection. 743df8bae1dSRodney W. Grimes */ 744df8bae1dSRodney W. Grimes if (len) { 7454e023759SAndre Oppermann struct mbuf *mb; 7464e023759SAndre Oppermann u_int moff; 7474e023759SAndre Oppermann 7482cdbfa66SPaul Saab if ((tp->t_flags & TF_FORCEDATA) && len == 1) 749df8bae1dSRodney W. Grimes tcpstat.tcps_sndprobe++; 750df8bae1dSRodney W. Grimes else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 751df8bae1dSRodney W. Grimes tcpstat.tcps_sndrexmitpack++; 752df8bae1dSRodney W. Grimes tcpstat.tcps_sndrexmitbyte += len; 753df8bae1dSRodney W. Grimes } else { 754df8bae1dSRodney W. Grimes tcpstat.tcps_sndpack++; 755df8bae1dSRodney W. Grimes tcpstat.tcps_sndbyte += len; 756df8bae1dSRodney W. Grimes } 757df8bae1dSRodney W. Grimes #ifdef notyet 758df8bae1dSRodney W. Grimes if ((m = m_copypack(so->so_snd.sb_mb, off, 759df8bae1dSRodney W. Grimes (int)len, max_linkhdr + hdrlen)) == 0) { 760cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 761df8bae1dSRodney W. Grimes error = ENOBUFS; 762df8bae1dSRodney W. Grimes goto out; 763df8bae1dSRodney W. Grimes } 764df8bae1dSRodney W. Grimes /* 765df8bae1dSRodney W. Grimes * m_copypack left space for our hdr; use it. 766df8bae1dSRodney W. Grimes */ 767df8bae1dSRodney W. Grimes m->m_len += hdrlen; 768df8bae1dSRodney W. Grimes m->m_data -= hdrlen; 769df8bae1dSRodney W. Grimes #else 77034333b16SAndre Oppermann MGETHDR(m, M_DONTWAIT, MT_DATA); 771df8bae1dSRodney W. Grimes if (m == NULL) { 772cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 773df8bae1dSRodney W. Grimes error = ENOBUFS; 774df8bae1dSRodney W. Grimes goto out; 775df8bae1dSRodney W. Grimes } 776fb59c426SYoshinobu Inoue #ifdef INET6 777a683a7ddSYoshinobu Inoue if (MHLEN < hdrlen + max_linkhdr) { 778a163d034SWarner Losh MCLGET(m, M_DONTWAIT); 779a683a7ddSYoshinobu Inoue if ((m->m_flags & M_EXT) == 0) { 780cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 781a683a7ddSYoshinobu Inoue m_freem(m); 782a683a7ddSYoshinobu Inoue error = ENOBUFS; 783a683a7ddSYoshinobu Inoue goto out; 784a683a7ddSYoshinobu Inoue } 785a683a7ddSYoshinobu Inoue } 786fb59c426SYoshinobu Inoue #endif 787df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 788df8bae1dSRodney W. Grimes m->m_len = hdrlen; 7894e023759SAndre Oppermann 7904e023759SAndre Oppermann /* 7914e023759SAndre Oppermann * Start the m_copy functions from the closest mbuf 7924e023759SAndre Oppermann * to the offset in the socket buffer chain. 7934e023759SAndre Oppermann */ 7944e023759SAndre Oppermann mb = sbsndptr(&so->so_snd, off, len, &moff); 7954e023759SAndre Oppermann 796df8bae1dSRodney W. Grimes if (len <= MHLEN - hdrlen - max_linkhdr) { 7974e023759SAndre Oppermann m_copydata(mb, moff, (int)len, 798df8bae1dSRodney W. Grimes mtod(m, caddr_t) + hdrlen); 799df8bae1dSRodney W. Grimes m->m_len += len; 800df8bae1dSRodney W. Grimes } else { 8014e023759SAndre Oppermann m->m_next = m_copy(mb, moff, (int)len); 8024e023759SAndre Oppermann if (m->m_next == NULL) { 803cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 804d7f570e6SGarrett Wollman (void) m_free(m); 80551823c3aSGarrett Wollman error = ENOBUFS; 80651823c3aSGarrett Wollman goto out; 80751823c3aSGarrett Wollman } 808df8bae1dSRodney W. Grimes } 809df8bae1dSRodney W. Grimes #endif 810df8bae1dSRodney W. Grimes /* 811df8bae1dSRodney W. Grimes * If we're sending everything we've got, set PUSH. 812df8bae1dSRodney W. Grimes * (This will keep happy those implementations which only 813df8bae1dSRodney W. Grimes * give data to the user when a buffer fills or 814df8bae1dSRodney W. Grimes * a PUSH comes in.) 815df8bae1dSRodney W. Grimes */ 816df8bae1dSRodney W. Grimes if (off + len == so->so_snd.sb_cc) 817df8bae1dSRodney W. Grimes flags |= TH_PUSH; 818cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 819df8bae1dSRodney W. Grimes } else { 820cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 821df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 822df8bae1dSRodney W. Grimes tcpstat.tcps_sndacks++; 823df8bae1dSRodney W. Grimes else if (flags & (TH_SYN|TH_FIN|TH_RST)) 824df8bae1dSRodney W. Grimes tcpstat.tcps_sndctrl++; 825df8bae1dSRodney W. Grimes else if (SEQ_GT(tp->snd_up, tp->snd_una)) 826df8bae1dSRodney W. Grimes tcpstat.tcps_sndurg++; 827df8bae1dSRodney W. Grimes else 828df8bae1dSRodney W. Grimes tcpstat.tcps_sndwinup++; 829df8bae1dSRodney W. Grimes 83034333b16SAndre Oppermann MGETHDR(m, M_DONTWAIT, MT_DATA); 831df8bae1dSRodney W. Grimes if (m == NULL) { 832df8bae1dSRodney W. Grimes error = ENOBUFS; 833df8bae1dSRodney W. Grimes goto out; 834df8bae1dSRodney W. Grimes } 835fb59c426SYoshinobu Inoue #ifdef INET6 836fb59c426SYoshinobu Inoue if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 837fb59c426SYoshinobu Inoue MHLEN >= hdrlen) { 838fb59c426SYoshinobu Inoue MH_ALIGN(m, hdrlen); 839fb59c426SYoshinobu Inoue } else 840fb59c426SYoshinobu Inoue #endif 841df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 842df8bae1dSRodney W. Grimes m->m_len = hdrlen; 843df8bae1dSRodney W. Grimes } 844cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); 845df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 846c488362eSRobert Watson #ifdef MAC 847c18b97c6SRobert Watson mac_create_mbuf_from_inpcb(tp->t_inpcb, m); 848c488362eSRobert Watson #endif 849fb59c426SYoshinobu Inoue #ifdef INET6 850fb59c426SYoshinobu Inoue if (isipv6) { 851fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 852fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip6 + 1); 85379909384SJonathan Lemon tcpip_fillheaders(tp->t_inpcb, ip6, th); 854fb59c426SYoshinobu Inoue } else 855fb59c426SYoshinobu Inoue #endif /* INET6 */ 856fb59c426SYoshinobu Inoue { 857fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 858fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 859fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip + 1); 86079909384SJonathan Lemon tcpip_fillheaders(tp->t_inpcb, ip, th); 861fb59c426SYoshinobu Inoue } 862df8bae1dSRodney W. Grimes 863df8bae1dSRodney W. Grimes /* 864df8bae1dSRodney W. Grimes * Fill in fields, remembering maximum advertised 865df8bae1dSRodney W. Grimes * window for use in delaying messages about window sizes. 866df8bae1dSRodney W. Grimes * If resending a FIN, be sure not to use a new sequence number. 867df8bae1dSRodney W. Grimes */ 868df8bae1dSRodney W. Grimes if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 869df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) 870df8bae1dSRodney W. Grimes tp->snd_nxt--; 871df8bae1dSRodney W. Grimes /* 872df8bae1dSRodney W. Grimes * If we are doing retransmissions, then snd_nxt will 873df8bae1dSRodney W. Grimes * not reflect the first unsent octet. For ACK only 874df8bae1dSRodney W. Grimes * packets, we do not want the sequence number of the 875df8bae1dSRodney W. Grimes * retransmitted packet, we want the sequence number 876df8bae1dSRodney W. Grimes * of the next unsent octet. So, if there is no data 877df8bae1dSRodney W. Grimes * (and no SYN or FIN), use snd_max instead of snd_nxt 878df8bae1dSRodney W. Grimes * when filling in ti_seq. But if we are in persist 879df8bae1dSRodney W. Grimes * state, snd_max might reflect one byte beyond the 880df8bae1dSRodney W. Grimes * right edge of the window, so use snd_nxt in that 881df8bae1dSRodney W. Grimes * case, since we know we aren't doing a retransmission. 882df8bae1dSRodney W. Grimes * (retransmit and persist are mutually exclusive...) 883df8bae1dSRodney W. Grimes */ 884a55db2b6SPaul Saab if (sack_rxmit == 0) { 885b8152ba7SAndre Oppermann if (len || (flags & (TH_SYN|TH_FIN)) || 886b8152ba7SAndre Oppermann tcp_timer_active(tp, TT_PERSIST)) 887fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_nxt); 888df8bae1dSRodney W. Grimes else 889fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_max); 890a55db2b6SPaul Saab } else { 8916d90faf3SPaul Saab th->th_seq = htonl(p->rxmit); 8926d90faf3SPaul Saab p->rxmit += len; 8930077b016SPaul Saab tp->sackhint.sack_bytes_rexmit += len; 8946d90faf3SPaul Saab } 895fb59c426SYoshinobu Inoue th->th_ack = htonl(tp->rcv_nxt); 896df8bae1dSRodney W. Grimes if (optlen) { 897fb59c426SYoshinobu Inoue bcopy(opt, th + 1, optlen); 898fb59c426SYoshinobu Inoue th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 899df8bae1dSRodney W. Grimes } 900fb59c426SYoshinobu Inoue th->th_flags = flags; 901df8bae1dSRodney W. Grimes /* 902df8bae1dSRodney W. Grimes * Calculate receive window. Don't shrink window, 903df8bae1dSRodney W. Grimes * but avoid silly window syndrome. 90402a1a643SAndre Oppermann * 90502a1a643SAndre Oppermann * XXX: RFC1323: The Window field in a SYN (i.e., a <SYN> or 90602a1a643SAndre Oppermann * <SYN,ACK>) segment itself is never scaled. 907df8bae1dSRodney W. Grimes */ 908201d185bSAndre Oppermann if (recwin < (long)(so->so_rcv.sb_hiwat / 4) && 909201d185bSAndre Oppermann recwin < (long)tp->t_maxseg) 910201d185bSAndre Oppermann recwin = 0; 911201d185bSAndre Oppermann if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt)) 912201d185bSAndre Oppermann recwin = (long)(tp->rcv_adv - tp->rcv_nxt); 913201d185bSAndre Oppermann if (recwin > (long)TCP_MAXWIN << tp->rcv_scale) 914201d185bSAndre Oppermann recwin = (long)TCP_MAXWIN << tp->rcv_scale; 915201d185bSAndre Oppermann th->th_win = htons((u_short) (recwin >> tp->rcv_scale)); 916262c1c1aSMatthew Dillon 917262c1c1aSMatthew Dillon 918262c1c1aSMatthew Dillon /* 919262c1c1aSMatthew Dillon * Adjust the RXWIN0SENT flag - indicate that we have advertised 920262c1c1aSMatthew Dillon * a 0 window. This may cause the remote transmitter to stall. This 921262c1c1aSMatthew Dillon * flag tells soreceive() to disable delayed acknowledgements when 922262c1c1aSMatthew Dillon * draining the buffer. This can occur if the receiver is attempting 923262c1c1aSMatthew Dillon * to read more data then can be buffered prior to transmitting on 924262c1c1aSMatthew Dillon * the connection. 925262c1c1aSMatthew Dillon */ 926201d185bSAndre Oppermann if (recwin == 0) 927262c1c1aSMatthew Dillon tp->t_flags |= TF_RXWIN0SENT; 928262c1c1aSMatthew Dillon else 929262c1c1aSMatthew Dillon tp->t_flags &= ~TF_RXWIN0SENT; 930df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 931fb59c426SYoshinobu Inoue th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 932fb59c426SYoshinobu Inoue th->th_flags |= TH_URG; 933df8bae1dSRodney W. Grimes } else 934df8bae1dSRodney W. Grimes /* 935df8bae1dSRodney W. Grimes * If no urgent pointer to send, then we pull 936df8bae1dSRodney W. Grimes * the urgent pointer to the left edge of the send window 937df8bae1dSRodney W. Grimes * so that it doesn't drift into the send window on sequence 938df8bae1dSRodney W. Grimes * number wraparound. 939df8bae1dSRodney W. Grimes */ 940df8bae1dSRodney W. Grimes tp->snd_up = tp->snd_una; /* drag it along */ 941df8bae1dSRodney W. Grimes 9421cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 9431cfd4b53SBruce M Simpson #ifdef INET6 9441cfd4b53SBruce M Simpson if (!isipv6) 9451cfd4b53SBruce M Simpson #endif 9461cfd4b53SBruce M Simpson if (tp->t_flags & TF_SIGNATURE) 947265ed012SBruce M Simpson tcp_signature_compute(m, sizeof(struct ip), len, optlen, 9481cfd4b53SBruce M Simpson (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND); 949265ed012SBruce M Simpson #endif 9501cfd4b53SBruce M Simpson 951df8bae1dSRodney W. Grimes /* 952df8bae1dSRodney W. Grimes * Put TCP length in extended header, and then 953df8bae1dSRodney W. Grimes * checksum extended header and data. 954df8bae1dSRodney W. Grimes */ 955fb59c426SYoshinobu Inoue m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 956fb59c426SYoshinobu Inoue #ifdef INET6 957fb59c426SYoshinobu Inoue if (isipv6) 958fb59c426SYoshinobu Inoue /* 959fb59c426SYoshinobu Inoue * ip6_plen is not need to be filled now, and will be filled 960fb59c426SYoshinobu Inoue * in ip6_output. 961fb59c426SYoshinobu Inoue */ 962fb59c426SYoshinobu Inoue th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 963fb59c426SYoshinobu Inoue sizeof(struct tcphdr) + optlen + len); 964fb59c426SYoshinobu Inoue else 965fb59c426SYoshinobu Inoue #endif /* INET6 */ 966fb59c426SYoshinobu Inoue { 967db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_TCP; 968db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 96979909384SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 97079909384SJonathan Lemon htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen)); 971fb59c426SYoshinobu Inoue 972db4f9cc7SJonathan Lemon /* IP version must be set here for ipv4/ipv6 checking later */ 973db4f9cc7SJonathan Lemon KASSERT(ip->ip_v == IPVERSION, 9746e551fb6SDavid E. O'Brien ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 975fb59c426SYoshinobu Inoue } 976df8bae1dSRodney W. Grimes 977df8bae1dSRodney W. Grimes /* 978b3c0f300SAndre Oppermann * Enable TSO and specify the size of the segments. 979b3c0f300SAndre Oppermann * The TCP pseudo header checksum is always provided. 980b3c0f300SAndre Oppermann * XXX: Fixme: This is currently not the case for IPv6. 981b3c0f300SAndre Oppermann */ 982b3c0f300SAndre Oppermann if (tso) { 983b3c0f300SAndre Oppermann m->m_pkthdr.csum_flags = CSUM_TSO; 984b3c0f300SAndre Oppermann m->m_pkthdr.tso_segsz = tp->t_maxopd - optlen; 985b3c0f300SAndre Oppermann } 986b3c0f300SAndre Oppermann 987b3c0f300SAndre Oppermann /* 988df8bae1dSRodney W. Grimes * In transmit state, time the transmission and arrange for 989df8bae1dSRodney W. Grimes * the retransmit. In persist state, just set snd_max. 990df8bae1dSRodney W. Grimes */ 9912cdbfa66SPaul Saab if ((tp->t_flags & TF_FORCEDATA) == 0 || 992b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 993df8bae1dSRodney W. Grimes tcp_seq startseq = tp->snd_nxt; 994df8bae1dSRodney W. Grimes 995df8bae1dSRodney W. Grimes /* 996df8bae1dSRodney W. Grimes * Advance snd_nxt over sequence space of this segment. 997df8bae1dSRodney W. Grimes */ 998df8bae1dSRodney W. Grimes if (flags & (TH_SYN|TH_FIN)) { 999df8bae1dSRodney W. Grimes if (flags & TH_SYN) 1000df8bae1dSRodney W. Grimes tp->snd_nxt++; 1001df8bae1dSRodney W. Grimes if (flags & TH_FIN) { 1002df8bae1dSRodney W. Grimes tp->snd_nxt++; 1003df8bae1dSRodney W. Grimes tp->t_flags |= TF_SENTFIN; 1004df8bae1dSRodney W. Grimes } 1005df8bae1dSRodney W. Grimes } 1006a55db2b6SPaul Saab if (sack_rxmit) 10076d90faf3SPaul Saab goto timer; 1008df8bae1dSRodney W. Grimes tp->snd_nxt += len; 1009df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 1010df8bae1dSRodney W. Grimes tp->snd_max = tp->snd_nxt; 1011df8bae1dSRodney W. Grimes /* 1012df8bae1dSRodney W. Grimes * Time this transmission if not a retransmission and 1013df8bae1dSRodney W. Grimes * not currently timing anything. 1014df8bae1dSRodney W. Grimes */ 10159b8b58e0SJonathan Lemon if (tp->t_rtttime == 0) { 10169b8b58e0SJonathan Lemon tp->t_rtttime = ticks; 1017df8bae1dSRodney W. Grimes tp->t_rtseq = startseq; 1018df8bae1dSRodney W. Grimes tcpstat.tcps_segstimed++; 1019df8bae1dSRodney W. Grimes } 1020df8bae1dSRodney W. Grimes } 1021df8bae1dSRodney W. Grimes 1022df8bae1dSRodney W. Grimes /* 1023df8bae1dSRodney W. Grimes * Set retransmit timer if not currently set, 102428257b5cSMatthew Dillon * and not doing a pure ack or a keep-alive probe. 1025df8bae1dSRodney W. Grimes * Initial value for retransmit timer is smoothed 1026df8bae1dSRodney W. Grimes * round-trip time + 2 * round-trip time variance. 1027df8bae1dSRodney W. Grimes * Initialize shift counter which is used for backoff 1028df8bae1dSRodney W. Grimes * of retransmit time. 1029df8bae1dSRodney W. Grimes */ 10306d90faf3SPaul Saab timer: 1031b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_PERSIST) && 1032a55db2b6SPaul Saab ((sack_rxmit && tp->snd_nxt != tp->snd_max) || 1033a55db2b6SPaul Saab (tp->snd_nxt != tp->snd_una))) { 1034b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_PERSIST)) { 1035b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, 0); 1036df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 1037df8bae1dSRodney W. Grimes } 1038b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 1039df8bae1dSRodney W. Grimes } 104028257b5cSMatthew Dillon } else { 104128257b5cSMatthew Dillon /* 104228257b5cSMatthew Dillon * Persist case, update snd_max but since we are in 104328257b5cSMatthew Dillon * persist mode (no window) we do not update snd_nxt. 104428257b5cSMatthew Dillon */ 104528257b5cSMatthew Dillon int xlen = len; 104628257b5cSMatthew Dillon if (flags & TH_SYN) 104728257b5cSMatthew Dillon ++xlen; 104828257b5cSMatthew Dillon if (flags & TH_FIN) { 104928257b5cSMatthew Dillon ++xlen; 105028257b5cSMatthew Dillon tp->t_flags |= TF_SENTFIN; 105128257b5cSMatthew Dillon } 1052abac41a6SMatthew Dillon if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) 1053df8bae1dSRodney W. Grimes tp->snd_max = tp->snd_nxt + len; 105428257b5cSMatthew Dillon } 1055df8bae1dSRodney W. Grimes 1056610ee2f9SDavid Greenman #ifdef TCPDEBUG 1057df8bae1dSRodney W. Grimes /* 1058df8bae1dSRodney W. Grimes * Trace. 1059df8bae1dSRodney W. Grimes */ 106091f467d5SHartmut Brandt if (so->so_options & SO_DEBUG) { 1061f3e0b7efSBruce M Simpson u_short save = 0; 10625214cb3fSBruce M Simpson #ifdef INET6 10635214cb3fSBruce M Simpson if (!isipv6) 10645214cb3fSBruce M Simpson #endif 10655214cb3fSBruce M Simpson { 10665214cb3fSBruce M Simpson save = ipov->ih_len; 106791f467d5SHartmut Brandt ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */); 10685214cb3fSBruce M Simpson } 1069fb59c426SYoshinobu Inoue tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 10705214cb3fSBruce M Simpson #ifdef INET6 10715214cb3fSBruce M Simpson if (!isipv6) 10725214cb3fSBruce M Simpson #endif 107391f467d5SHartmut Brandt ipov->ih_len = save; 107491f467d5SHartmut Brandt } 1075610ee2f9SDavid Greenman #endif 1076df8bae1dSRodney W. Grimes 1077df8bae1dSRodney W. Grimes /* 1078df8bae1dSRodney W. Grimes * Fill in IP length and desired time to live and 1079df8bae1dSRodney W. Grimes * send to IP level. There should be a better way 1080df8bae1dSRodney W. Grimes * to handle ttl and tos; we could keep them in 1081df8bae1dSRodney W. Grimes * the template, but need a way to checksum without them. 1082df8bae1dSRodney W. Grimes */ 1083fb59c426SYoshinobu Inoue /* 1084fb59c426SYoshinobu Inoue * m->m_pkthdr.len should have been set before cksum calcuration, 1085fb59c426SYoshinobu Inoue * because in6_cksum() need it. 1086fb59c426SYoshinobu Inoue */ 1087fb59c426SYoshinobu Inoue #ifdef INET6 1088fb59c426SYoshinobu Inoue if (isipv6) { 1089fb59c426SYoshinobu Inoue /* 1090fb59c426SYoshinobu Inoue * we separately set hoplimit for every segment, since the 1091fb59c426SYoshinobu Inoue * user might want to change the value via setsockopt. 1092fb59c426SYoshinobu Inoue * Also, desired default hop limit might be changed via 1093fb59c426SYoshinobu Inoue * Neighbor Discovery. 1094fb59c426SYoshinobu Inoue */ 109597d8d152SAndre Oppermann ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL); 1096fb59c426SYoshinobu Inoue 1097fb59c426SYoshinobu Inoue /* TODO: IPv6 IP6TOS_ECT bit on */ 1098fb59c426SYoshinobu Inoue error = ip6_output(m, 109997d8d152SAndre Oppermann tp->t_inpcb->in6p_outputopts, NULL, 1100b5d47ff5SJohn-Mark Gurney ((so->so_options & SO_DONTROUTE) ? 1101b5d47ff5SJohn-Mark Gurney IP_ROUTETOIF : 0), NULL, NULL, tp->t_inpcb); 1102fb59c426SYoshinobu Inoue } else 1103fb59c426SYoshinobu Inoue #endif /* INET6 */ 1104df8bae1dSRodney W. Grimes { 1105fb59c426SYoshinobu Inoue ip->ip_len = m->m_pkthdr.len; 1106686cdd19SJun-ichiro itojun Hagino #ifdef INET6 1107686cdd19SJun-ichiro itojun Hagino if (INP_CHECK_SOCKAF(so, AF_INET6)) 110897d8d152SAndre Oppermann ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL); 1109686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */ 1110f138387aSGarrett Wollman /* 111197d8d152SAndre Oppermann * If we do path MTU discovery, then we set DF on every packet. 111297d8d152SAndre Oppermann * This might not be the best thing to do according to RFC3390 111397d8d152SAndre Oppermann * Section 2. However the tcp hostcache migitates the problem 111497d8d152SAndre Oppermann * so it affects only the first tcp connection with a host. 1115f138387aSGarrett Wollman */ 111697d8d152SAndre Oppermann if (path_mtu_discovery) 1117fb59c426SYoshinobu Inoue ip->ip_off |= IP_DF; 111897d8d152SAndre Oppermann 111997d8d152SAndre Oppermann error = ip_output(m, tp->t_inpcb->inp_options, NULL, 1120b5d47ff5SJohn-Mark Gurney ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0, 1121b5d47ff5SJohn-Mark Gurney tp->t_inpcb); 1122df8bae1dSRodney W. Grimes } 1123df8bae1dSRodney W. Grimes if (error) { 11247734ea06SArchie Cobbs 11257734ea06SArchie Cobbs /* 11267734ea06SArchie Cobbs * We know that the packet was lost, so back out the 11277734ea06SArchie Cobbs * sequence number advance, if any. 11282c30ec0aSAndre Oppermann * 11292c30ec0aSAndre Oppermann * If the error is EPERM the packet got blocked by the 11302c30ec0aSAndre Oppermann * local firewall. Normally we should terminate the 11312c30ec0aSAndre Oppermann * connection but the blocking may have been spurious 11322c30ec0aSAndre Oppermann * due to a firewall reconfiguration cycle. So we treat 11332c30ec0aSAndre Oppermann * it like a packet loss and let the retransmit timer and 11342c30ec0aSAndre Oppermann * timeouts do their work over time. 11352c30ec0aSAndre Oppermann * XXX: It is a POLA question whether calling tcp_drop right 11362c30ec0aSAndre Oppermann * away would be the really correct behavior instead. 11377734ea06SArchie Cobbs */ 113872757d9aSGleb Smirnoff if (((tp->t_flags & TF_FORCEDATA) == 0 || 1139b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) && 114072757d9aSGleb Smirnoff ((flags & TH_SYN) == 0) && 114172757d9aSGleb Smirnoff (error != EPERM)) { 11420077b016SPaul Saab if (sack_rxmit) { 11435d3b1b75SJayanth Vijayaraghavan p->rxmit -= len; 11440077b016SPaul Saab tp->sackhint.sack_bytes_rexmit -= len; 114572757d9aSGleb Smirnoff KASSERT(tp->sackhint.sack_bytes_rexmit >= 0, 11460077b016SPaul Saab ("sackhint bytes rtx >= 0")); 11470077b016SPaul Saab } else 11487734ea06SArchie Cobbs tp->snd_nxt -= len; 11497734ea06SArchie Cobbs } 1150df8bae1dSRodney W. Grimes out: 1151cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); /* Check gotos. */ 115272757d9aSGleb Smirnoff switch (error) { 115372757d9aSGleb Smirnoff case EPERM: 115472757d9aSGleb Smirnoff tp->t_softerror = error; 115572757d9aSGleb Smirnoff return (error); 115672757d9aSGleb Smirnoff case ENOBUFS: 1157b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) && 1158b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) 1159b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 11601600372bSAndre Oppermann tp->snd_cwnd = tp->t_maxseg; 1161df8bae1dSRodney W. Grimes return (0); 116272757d9aSGleb Smirnoff case EMSGSIZE: 11633d1f141bSGarrett Wollman /* 1164b3c0f300SAndre Oppermann * For some reason the interface we used initially 1165b3c0f300SAndre Oppermann * to send segments changed to another or lowered 1166b3c0f300SAndre Oppermann * its MTU. 1167b3c0f300SAndre Oppermann * 1168b3c0f300SAndre Oppermann * tcp_mtudisc() will find out the new MTU and as 1169b3c0f300SAndre Oppermann * its last action, initiate retransmission, so it 1170b3c0f300SAndre Oppermann * is important to not do so here. 1171b3c0f300SAndre Oppermann * 1172b3c0f300SAndre Oppermann * If TSO was active we either got an interface 1173b3c0f300SAndre Oppermann * without TSO capabilits or TSO was turned off. 1174b3c0f300SAndre Oppermann * Disable it for this connection as too and 1175b3c0f300SAndre Oppermann * immediatly retry with MSS sized segments generated 1176b3c0f300SAndre Oppermann * by this function. 11773d1f141bSGarrett Wollman */ 1178b3c0f300SAndre Oppermann if (tso) 1179b3c0f300SAndre Oppermann tp->t_flags &= ~TF_TSO; 11803d1f141bSGarrett Wollman tcp_mtudisc(tp->t_inpcb, 0); 118172757d9aSGleb Smirnoff return (0); 11828bec3467SGleb Smirnoff case EHOSTDOWN: 118372757d9aSGleb Smirnoff case EHOSTUNREACH: 118472757d9aSGleb Smirnoff case ENETDOWN: 11858bec3467SGleb Smirnoff case ENETUNREACH: 118672757d9aSGleb Smirnoff if (TCPS_HAVERCVDSYN(tp->t_state)) { 1187df8bae1dSRodney W. Grimes tp->t_softerror = error; 1188df8bae1dSRodney W. Grimes return (0); 1189df8bae1dSRodney W. Grimes } 119072757d9aSGleb Smirnoff /* FALLTHROUGH */ 119172757d9aSGleb Smirnoff default: 1192df8bae1dSRodney W. Grimes return (error); 1193df8bae1dSRodney W. Grimes } 119472757d9aSGleb Smirnoff } 1195df8bae1dSRodney W. Grimes tcpstat.tcps_sndtotal++; 1196df8bae1dSRodney W. Grimes 1197df8bae1dSRodney W. Grimes /* 1198df8bae1dSRodney W. Grimes * Data sent (as far as we can tell). 1199df8bae1dSRodney W. Grimes * If this advertises a larger window than any other segment, 1200df8bae1dSRodney W. Grimes * then remember the size of the advertised window. 1201df8bae1dSRodney W. Grimes * Any pending ACK has now been sent. 1202df8bae1dSRodney W. Grimes */ 1203201d185bSAndre Oppermann if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 1204201d185bSAndre Oppermann tp->rcv_adv = tp->rcv_nxt + recwin; 1205df8bae1dSRodney W. Grimes tp->last_ack_sent = tp->rcv_nxt; 12063bfd6421SJonathan Lemon tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 1207b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_DELACK)) 1208b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 0); 1209d912c694SMatthew Dillon #if 0 1210d912c694SMatthew Dillon /* 1211d912c694SMatthew Dillon * This completely breaks TCP if newreno is turned on. What happens 1212d912c694SMatthew Dillon * is that if delayed-acks are turned on on the receiver, this code 1213d912c694SMatthew Dillon * on the transmitter effectively destroys the TCP window, forcing 1214d912c694SMatthew Dillon * it to four packets (1.5Kx4 = 6K window). 1215d912c694SMatthew Dillon */ 121646f58482SJonathan Lemon if (sendalot && (!tcp_do_newreno || --maxburst)) 1217df8bae1dSRodney W. Grimes goto again; 1218d912c694SMatthew Dillon #endif 1219d912c694SMatthew Dillon if (sendalot) 1220d912c694SMatthew Dillon goto again; 1221df8bae1dSRodney W. Grimes return (0); 1222df8bae1dSRodney W. Grimes } 1223df8bae1dSRodney W. Grimes 1224df8bae1dSRodney W. Grimes void 1225ad3f9ab3SAndre Oppermann tcp_setpersist(struct tcpcb *tp) 1226df8bae1dSRodney W. Grimes { 12279b8b58e0SJonathan Lemon int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 12289b8b58e0SJonathan Lemon int tt; 1229df8bae1dSRodney W. Grimes 1230b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_REXMT)) 12319b8b58e0SJonathan Lemon panic("tcp_setpersist: retransmit pending"); 1232df8bae1dSRodney W. Grimes /* 1233df8bae1dSRodney W. Grimes * Start/restart persistance timer. 1234df8bae1dSRodney W. Grimes */ 12359b8b58e0SJonathan Lemon TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 1236df8bae1dSRodney W. Grimes TCPTV_PERSMIN, TCPTV_PERSMAX); 1237b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, tt); 1238df8bae1dSRodney W. Grimes if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 1239df8bae1dSRodney W. Grimes tp->t_rxtshift++; 1240df8bae1dSRodney W. Grimes } 124102a1a643SAndre Oppermann 124202a1a643SAndre Oppermann /* 124302a1a643SAndre Oppermann * Insert TCP options according to the supplied parameters to the place 124402a1a643SAndre Oppermann * optp in a consistent way. Can handle unaligned destinations. 124502a1a643SAndre Oppermann * 124602a1a643SAndre Oppermann * The order of the option processing is crucial for optimal packing and 124702a1a643SAndre Oppermann * alignment for the scarce option space. 124802a1a643SAndre Oppermann * 124902a1a643SAndre Oppermann * The optimal order for a SYN/SYN-ACK segment is: 125002a1a643SAndre Oppermann * MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) + 125102a1a643SAndre Oppermann * Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40. 125202a1a643SAndre Oppermann * 125302a1a643SAndre Oppermann * The SACK options should be last. SACK blocks consume 8*n+2 bytes. 125402a1a643SAndre Oppermann * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks). 125502a1a643SAndre Oppermann * At minimum we need 10 bytes (to generate 1 SACK block). If both 125602a1a643SAndre Oppermann * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present, 125702a1a643SAndre Oppermann * we only have 10 bytes for SACK options (40 - (12 + 18)). 125802a1a643SAndre Oppermann */ 125902a1a643SAndre Oppermann int 126002a1a643SAndre Oppermann tcp_addoptions(struct tcpopt *to, u_char *optp) 126102a1a643SAndre Oppermann { 126202a1a643SAndre Oppermann u_int mask, optlen = 0; 126302a1a643SAndre Oppermann 126402a1a643SAndre Oppermann for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) { 126502a1a643SAndre Oppermann if ((to->to_flags & mask) != mask) 126602a1a643SAndre Oppermann continue; 126702a1a643SAndre Oppermann switch (to->to_flags & mask) { 126802a1a643SAndre Oppermann case TOF_MSS: 126902a1a643SAndre Oppermann while (optlen % 4) { 127002a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 127102a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 127202a1a643SAndre Oppermann } 127302a1a643SAndre Oppermann optlen += TCPOLEN_MAXSEG; 127402a1a643SAndre Oppermann *optp++ = TCPOPT_MAXSEG; 127502a1a643SAndre Oppermann *optp++ = TCPOLEN_MAXSEG; 127602a1a643SAndre Oppermann to->to_mss = htons(to->to_mss); 127702a1a643SAndre Oppermann bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss)); 127802a1a643SAndre Oppermann optp += sizeof(to->to_mss); 127902a1a643SAndre Oppermann break; 128002a1a643SAndre Oppermann case TOF_SCALE: 128102a1a643SAndre Oppermann while (!optlen || optlen % 2 != 1) { 128202a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 128302a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 128402a1a643SAndre Oppermann } 128502a1a643SAndre Oppermann optlen += TCPOLEN_WINDOW; 128602a1a643SAndre Oppermann *optp++ = TCPOPT_WINDOW; 128702a1a643SAndre Oppermann *optp++ = TCPOLEN_WINDOW; 128802a1a643SAndre Oppermann *optp++ = to->to_wscale; 128902a1a643SAndre Oppermann break; 129002a1a643SAndre Oppermann case TOF_SACKPERM: 129102a1a643SAndre Oppermann while (optlen % 2) { 129202a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 129302a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 129402a1a643SAndre Oppermann } 129502a1a643SAndre Oppermann optlen += TCPOLEN_SACK_PERMITTED; 129602a1a643SAndre Oppermann *optp++ = TCPOPT_SACK_PERMITTED; 129702a1a643SAndre Oppermann *optp++ = TCPOLEN_SACK_PERMITTED; 129802a1a643SAndre Oppermann break; 129902a1a643SAndre Oppermann case TOF_TS: 130002a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 130102a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 130202a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 130302a1a643SAndre Oppermann } 130402a1a643SAndre Oppermann optlen += TCPOLEN_TIMESTAMP; 130502a1a643SAndre Oppermann *optp++ = TCPOPT_TIMESTAMP; 130602a1a643SAndre Oppermann *optp++ = TCPOLEN_TIMESTAMP; 130702a1a643SAndre Oppermann to->to_tsval = htonl(to->to_tsval); 130802a1a643SAndre Oppermann to->to_tsecr = htonl(to->to_tsecr); 130902a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval)); 131002a1a643SAndre Oppermann optp += sizeof(to->to_tsval); 131102a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr)); 131202a1a643SAndre Oppermann optp += sizeof(to->to_tsecr); 131302a1a643SAndre Oppermann break; 131402a1a643SAndre Oppermann case TOF_SIGNATURE: 131502a1a643SAndre Oppermann { 131602a1a643SAndre Oppermann int siglen = TCPOLEN_SIGNATURE - 2; 131702a1a643SAndre Oppermann 131802a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 131902a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 132002a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 132102a1a643SAndre Oppermann } 132202a1a643SAndre Oppermann if (MAX_TCPOPTLEN - optlen < TCPOLEN_SIGNATURE) 132302a1a643SAndre Oppermann continue; 132402a1a643SAndre Oppermann optlen += TCPOLEN_SIGNATURE; 132502a1a643SAndre Oppermann *optp++ = TCPOPT_SIGNATURE; 132602a1a643SAndre Oppermann *optp++ = TCPOLEN_SIGNATURE; 132702a1a643SAndre Oppermann to->to_signature = optp; 132802a1a643SAndre Oppermann while (siglen--) 132902a1a643SAndre Oppermann *optp++ = 0; 133002a1a643SAndre Oppermann break; 133102a1a643SAndre Oppermann } 133202a1a643SAndre Oppermann case TOF_SACK: 133302a1a643SAndre Oppermann { 133402a1a643SAndre Oppermann int sackblks = 0; 133502a1a643SAndre Oppermann struct sackblk *sack = (struct sackblk *)to->to_sacks; 133602a1a643SAndre Oppermann tcp_seq sack_seq; 133702a1a643SAndre Oppermann 133802a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 133902a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 134002a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 134102a1a643SAndre Oppermann } 134202a1a643SAndre Oppermann if (MAX_TCPOPTLEN - optlen < 2 + TCPOLEN_SACK) 134302a1a643SAndre Oppermann continue; 134402a1a643SAndre Oppermann optlen += TCPOLEN_SACKHDR; 134502a1a643SAndre Oppermann *optp++ = TCPOPT_SACK; 134602a1a643SAndre Oppermann sackblks = min(to->to_nsacks, 134702a1a643SAndre Oppermann (MAX_TCPOPTLEN - optlen) / TCPOLEN_SACK); 134802a1a643SAndre Oppermann *optp++ = TCPOLEN_SACKHDR + sackblks * TCPOLEN_SACK; 134902a1a643SAndre Oppermann while (sackblks--) { 135002a1a643SAndre Oppermann sack_seq = htonl(sack->start); 135102a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 135202a1a643SAndre Oppermann optp += sizeof(sack_seq); 135302a1a643SAndre Oppermann sack_seq = htonl(sack->end); 135402a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 135502a1a643SAndre Oppermann optp += sizeof(sack_seq); 135602a1a643SAndre Oppermann optlen += TCPOLEN_SACK; 135702a1a643SAndre Oppermann sack++; 135802a1a643SAndre Oppermann } 135902a1a643SAndre Oppermann tcpstat.tcps_sack_send_blocks++; 136002a1a643SAndre Oppermann break; 136102a1a643SAndre Oppermann } 136202a1a643SAndre Oppermann default: 136302a1a643SAndre Oppermann panic("%s: unknown TCP option type", __func__); 136402a1a643SAndre Oppermann break; 136502a1a643SAndre Oppermann } 136602a1a643SAndre Oppermann } 136702a1a643SAndre Oppermann 136802a1a643SAndre Oppermann /* Terminate and pad TCP options to a 4 byte boundary. */ 136902a1a643SAndre Oppermann if (optlen % 4) { 137002a1a643SAndre Oppermann optlen += TCPOLEN_EOL; 137102a1a643SAndre Oppermann *optp++ = TCPOPT_EOL; 137202a1a643SAndre Oppermann } 137302a1a643SAndre Oppermann while (optlen % 4) { 137402a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 137502a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 137602a1a643SAndre Oppermann } 137702a1a643SAndre Oppermann 137802a1a643SAndre Oppermann KASSERT(optlen <= MAX_TCPOPTLEN, ("%s: TCP options too long", __func__)); 137902a1a643SAndre Oppermann return (optlen); 138002a1a643SAndre Oppermann } 1381