1df8bae1dSRodney W. Grimes /* 2d7f570e6SGarrett Wollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33d7f570e6SGarrett Wollman * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37fb59c426SYoshinobu Inoue #include "opt_inet6.h" 383a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 390cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 400cc12cc5SJoerg Wunsch 41df8bae1dSRodney W. Grimes #include <sys/param.h> 42df8bae1dSRodney W. Grimes #include <sys/systm.h> 43686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h> 44fb919e4dSMark Murray #include <sys/kernel.h> 45fb919e4dSMark Murray #include <sys/lock.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> 60fb59c426SYoshinobu Inoue #ifdef INET6 61686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 62686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 63fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 64fb59c426SYoshinobu Inoue #endif 65df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 66df8bae1dSRodney W. Grimes #define TCPOUTFLAGS 67df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 68df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 69df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 70df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 71df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 72610ee2f9SDavid Greenman #ifdef TCPDEBUG 73df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 74610ee2f9SDavid Greenman #endif 75df8bae1dSRodney W. Grimes 763a2a9f79SYoshinobu Inoue #ifdef IPSEC 773a2a9f79SYoshinobu Inoue #include <netinet6/ipsec.h> 783a2a9f79SYoshinobu Inoue #endif /*IPSEC*/ 793a2a9f79SYoshinobu Inoue 80db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 81db4f9cc7SJonathan Lemon 82df8bae1dSRodney W. Grimes #ifdef notyet 83df8bae1dSRodney W. Grimes extern struct mbuf *m_copypack(); 84df8bae1dSRodney W. Grimes #endif 85df8bae1dSRodney W. Grimes 869a039a5fSDavid Greenman static int path_mtu_discovery = 1; 879a039a5fSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW, 889a039a5fSDavid Greenman &path_mtu_discovery, 1, "Enable Path MTU Discovery"); 899a039a5fSDavid Greenman 909b8b58e0SJonathan Lemon int ss_fltsz = 1; 919b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW, 929b8b58e0SJonathan Lemon &ss_fltsz, 1, "Slow start flight size"); 939b8b58e0SJonathan Lemon 949b8b58e0SJonathan Lemon int ss_fltsz_local = TCP_MAXWIN; /* something large */ 959b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW, 969b8b58e0SJonathan Lemon &ss_fltsz_local, 1, "Slow start flight size for local networks"); 97df8bae1dSRodney W. Grimes 987d200109SJayanth Vijayaraghavan int tcp_do_newreno = 1; 9946f58482SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno, 10046f58482SJonathan Lemon 0, "Enable NewReno Algorithms"); 101df8bae1dSRodney W. Grimes /* 102df8bae1dSRodney W. Grimes * Tcp output routine: figure out what should be sent and send it. 103df8bae1dSRodney W. Grimes */ 104df8bae1dSRodney W. Grimes int 105df8bae1dSRodney W. Grimes tcp_output(tp) 106df8bae1dSRodney W. Grimes register struct tcpcb *tp; 107df8bae1dSRodney W. Grimes { 108df8bae1dSRodney W. Grimes register struct socket *so = tp->t_inpcb->inp_socket; 109df8bae1dSRodney W. Grimes register long len, win; 110df8bae1dSRodney W. Grimes int off, flags, error; 111df8bae1dSRodney W. Grimes register struct mbuf *m; 112fb59c426SYoshinobu Inoue struct ip *ip = NULL; 113fb59c426SYoshinobu Inoue register struct ipovly *ipov = NULL; 114fb59c426SYoshinobu Inoue #ifdef INET6 115fb59c426SYoshinobu Inoue struct ip6_hdr *ip6 = NULL; 116fb59c426SYoshinobu Inoue #endif /* INET6 */ 117fb59c426SYoshinobu Inoue register struct tcphdr *th; 118a0292f23SGarrett Wollman u_char opt[TCP_MAXOLEN]; 119a04884fcSBill Fenner unsigned ipoptlen, optlen, hdrlen; 120df8bae1dSRodney W. Grimes int idle, sendalot; 12146f58482SJonathan Lemon int maxburst = TCP_MAXBURST; 122a0292f23SGarrett Wollman struct rmxp_tao *taop; 123a0292f23SGarrett Wollman struct rmxp_tao tao_noncached; 124fb59c426SYoshinobu Inoue #ifdef INET6 125fb59c426SYoshinobu Inoue int isipv6; 126fb59c426SYoshinobu Inoue #endif 127fb59c426SYoshinobu Inoue 128fb59c426SYoshinobu Inoue #ifdef INET6 129fb59c426SYoshinobu Inoue isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 130fb59c426SYoshinobu Inoue #endif 131df8bae1dSRodney W. Grimes 132df8bae1dSRodney W. Grimes /* 133df8bae1dSRodney W. Grimes * Determine length of data that should be transmitted, 134df8bae1dSRodney W. Grimes * and flags that will be used. 135df8bae1dSRodney W. Grimes * If there is some data or critical controls (SYN, RST) 136df8bae1dSRodney W. Grimes * to send, then transmit; otherwise, investigate further. 137df8bae1dSRodney W. Grimes */ 138df8bae1dSRodney W. Grimes idle = (tp->snd_max == tp->snd_una); 1399b8b58e0SJonathan Lemon if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) { 140df8bae1dSRodney W. Grimes /* 141df8bae1dSRodney W. Grimes * We have been idle for "a while" and no acks are 142df8bae1dSRodney W. Grimes * expected to clock out any data we send -- 143df8bae1dSRodney W. Grimes * slow start to get ack "clock" running again. 1449b8b58e0SJonathan Lemon * 1459b8b58e0SJonathan Lemon * Set the slow-start flight size depending on whether 1469b8b58e0SJonathan Lemon * this is a local network or not. 147df8bae1dSRodney W. Grimes */ 148fb59c426SYoshinobu Inoue if ( 149fb59c426SYoshinobu Inoue #ifdef INET6 150fb59c426SYoshinobu Inoue (isipv6 && in6_localaddr(&tp->t_inpcb->in6p_faddr)) || 151fb59c426SYoshinobu Inoue (!isipv6 && 152fb59c426SYoshinobu Inoue #endif 153fb59c426SYoshinobu Inoue in_localaddr(tp->t_inpcb->inp_faddr) 154fb59c426SYoshinobu Inoue #ifdef INET6 155fb59c426SYoshinobu Inoue ) 156fb59c426SYoshinobu Inoue #endif 157fb59c426SYoshinobu Inoue ) 1589b8b58e0SJonathan Lemon tp->snd_cwnd = tp->t_maxseg * ss_fltsz_local; 1599b8b58e0SJonathan Lemon else 1609b8b58e0SJonathan Lemon tp->snd_cwnd = tp->t_maxseg * ss_fltsz; 1619b8b58e0SJonathan Lemon } 162df8bae1dSRodney W. Grimes again: 163df8bae1dSRodney W. Grimes sendalot = 0; 164df8bae1dSRodney W. Grimes off = tp->snd_nxt - tp->snd_una; 165df8bae1dSRodney W. Grimes win = min(tp->snd_wnd, tp->snd_cwnd); 166df8bae1dSRodney W. Grimes 167df8bae1dSRodney W. Grimes flags = tcp_outflags[tp->t_state]; 168a0292f23SGarrett Wollman /* 169a0292f23SGarrett Wollman * Get standard flags, and add SYN or FIN if requested by 'hidden' 170a0292f23SGarrett Wollman * state flags. 171a0292f23SGarrett Wollman */ 172a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) 173a0292f23SGarrett Wollman flags |= TH_FIN; 174a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) 175a0292f23SGarrett Wollman flags |= TH_SYN; 176a0292f23SGarrett Wollman 177df8bae1dSRodney W. Grimes /* 178df8bae1dSRodney W. Grimes * If in persist timeout with window of 0, send 1 byte. 179df8bae1dSRodney W. Grimes * Otherwise, if window is small but nonzero 180df8bae1dSRodney W. Grimes * and timer expired, we will send what we can 181df8bae1dSRodney W. Grimes * and go to transmit state. 182df8bae1dSRodney W. Grimes */ 183df8bae1dSRodney W. Grimes if (tp->t_force) { 184df8bae1dSRodney W. Grimes if (win == 0) { 185df8bae1dSRodney W. Grimes /* 186df8bae1dSRodney W. Grimes * If we still have some data to send, then 187df8bae1dSRodney W. Grimes * clear the FIN bit. Usually this would 188df8bae1dSRodney W. Grimes * happen below when it realizes that we 189df8bae1dSRodney W. Grimes * aren't sending all the data. However, 19029089b51SJulian Elischer * if we have exactly 1 byte of unsent data, 191df8bae1dSRodney W. Grimes * then it won't clear the FIN bit below, 192df8bae1dSRodney W. Grimes * and if we are in persist state, we wind 193df8bae1dSRodney W. Grimes * up sending the packet without recording 194df8bae1dSRodney W. Grimes * that we sent the FIN bit. 195df8bae1dSRodney W. Grimes * 196df8bae1dSRodney W. Grimes * We can't just blindly clear the FIN bit, 197df8bae1dSRodney W. Grimes * because if we don't have any more data 198df8bae1dSRodney W. Grimes * to send then the probe will be the FIN 199df8bae1dSRodney W. Grimes * itself. 200df8bae1dSRodney W. Grimes */ 201df8bae1dSRodney W. Grimes if (off < so->so_snd.sb_cc) 202df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 203df8bae1dSRodney W. Grimes win = 1; 204df8bae1dSRodney W. Grimes } else { 2059b8b58e0SJonathan Lemon callout_stop(tp->tt_persist); 206df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 207df8bae1dSRodney W. Grimes } 208df8bae1dSRodney W. Grimes } 209df8bae1dSRodney W. Grimes 2109105bb46SBruce Evans len = (long)ulmin(so->so_snd.sb_cc, win) - off; 211df8bae1dSRodney W. Grimes 212a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) { 213a0292f23SGarrett Wollman taop = &tao_noncached; 214a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 215a0292f23SGarrett Wollman } 216a0292f23SGarrett Wollman 217a0292f23SGarrett Wollman /* 218a0292f23SGarrett Wollman * Lop off SYN bit if it has already been sent. However, if this 219a0292f23SGarrett Wollman * is SYN-SENT state and if segment contains data and if we don't 220a0292f23SGarrett Wollman * know that foreign host supports TAO, suppress sending segment. 221a0292f23SGarrett Wollman */ 222a0292f23SGarrett Wollman if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 223a0292f23SGarrett Wollman flags &= ~TH_SYN; 224a0292f23SGarrett Wollman off--, len++; 225a0292f23SGarrett Wollman if (len > 0 && tp->t_state == TCPS_SYN_SENT && 226a0292f23SGarrett Wollman taop->tao_ccsent == 0) 227a0292f23SGarrett Wollman return 0; 228a0292f23SGarrett Wollman } 229a0292f23SGarrett Wollman 23081165e48SAndras Olah /* 23181165e48SAndras Olah * Be careful not to send data and/or FIN on SYN segments 23281165e48SAndras Olah * in cases when no CC option will be sent. 23381165e48SAndras Olah * This measure is needed to prevent interoperability problems 23481165e48SAndras Olah * with not fully conformant TCP implementations. 23581165e48SAndras Olah */ 23681165e48SAndras Olah if ((flags & TH_SYN) && 23781165e48SAndras Olah ((tp->t_flags & TF_NOOPT) || !(tp->t_flags & TF_REQ_CC) || 23881165e48SAndras Olah ((flags & TH_ACK) && !(tp->t_flags & TF_RCVD_CC)))) { 23981165e48SAndras Olah len = 0; 24081165e48SAndras Olah flags &= ~TH_FIN; 24181165e48SAndras Olah } 24281165e48SAndras Olah 243df8bae1dSRodney W. Grimes if (len < 0) { 244df8bae1dSRodney W. Grimes /* 245df8bae1dSRodney W. Grimes * If FIN has been sent but not acked, 246df8bae1dSRodney W. Grimes * but we haven't been called to retransmit, 247df8bae1dSRodney W. Grimes * len will be -1. Otherwise, window shrank 248df8bae1dSRodney W. Grimes * after we sent into it. If window shrank to 0, 2492d8266afSDavid Greenman * cancel pending retransmit, pull snd_nxt back 2502d8266afSDavid Greenman * to (closed) window, and set the persist timer 2512d8266afSDavid Greenman * if it isn't already going. If the window didn't 2522d8266afSDavid Greenman * close completely, just wait for an ACK. 253df8bae1dSRodney W. Grimes */ 254df8bae1dSRodney W. Grimes len = 0; 255df8bae1dSRodney W. Grimes if (win == 0) { 2569b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 2572d8266afSDavid Greenman tp->t_rxtshift = 0; 258df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2599b8b58e0SJonathan Lemon if (!callout_active(tp->tt_persist)) 2602d8266afSDavid Greenman tcp_setpersist(tp); 261df8bae1dSRodney W. Grimes } 262df8bae1dSRodney W. Grimes } 263df8bae1dSRodney W. Grimes if (len > tp->t_maxseg) { 264df8bae1dSRodney W. Grimes len = tp->t_maxseg; 265df8bae1dSRodney W. Grimes sendalot = 1; 266df8bae1dSRodney W. Grimes } 267df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 268df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 269df8bae1dSRodney W. Grimes 270df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 271df8bae1dSRodney W. Grimes 272df8bae1dSRodney W. Grimes /* 273df8bae1dSRodney W. Grimes * Sender silly window avoidance. If connection is idle 274df8bae1dSRodney W. Grimes * and can send all data, a maximum segment, 275df8bae1dSRodney W. Grimes * at least a maximum default-size segment do it, 276df8bae1dSRodney W. Grimes * or are forced, do it; otherwise don't bother. 277df8bae1dSRodney W. Grimes * If peer's buffer is tiny, then send 278df8bae1dSRodney W. Grimes * when window is at least half open. 279df8bae1dSRodney W. Grimes * If retransmitting (possibly after persist timer forced us 280df8bae1dSRodney W. Grimes * to send into a small window), then must resend. 281df8bae1dSRodney W. Grimes */ 282df8bae1dSRodney W. Grimes if (len) { 283df8bae1dSRodney W. Grimes if (len == tp->t_maxseg) 284df8bae1dSRodney W. Grimes goto send; 285b0acefa8SBill Fenner if (!(tp->t_flags & TF_MORETOCOME) && 286b0acefa8SBill Fenner (idle || tp->t_flags & TF_NODELAY) && 287a0292f23SGarrett Wollman (tp->t_flags & TF_NOPUSH) == 0 && 288df8bae1dSRodney W. Grimes len + off >= so->so_snd.sb_cc) 289df8bae1dSRodney W. Grimes goto send; 290df8bae1dSRodney W. Grimes if (tp->t_force) 291df8bae1dSRodney W. Grimes goto send; 292a0292f23SGarrett Wollman if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 293df8bae1dSRodney W. Grimes goto send; 294df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 295df8bae1dSRodney W. Grimes goto send; 296df8bae1dSRodney W. Grimes } 297df8bae1dSRodney W. Grimes 298df8bae1dSRodney W. Grimes /* 299df8bae1dSRodney W. Grimes * Compare available window to amount of window 300df8bae1dSRodney W. Grimes * known to peer (as advertised window less 301df8bae1dSRodney W. Grimes * next expected input). If the difference is at least two 302df8bae1dSRodney W. Grimes * max size segments, or at least 50% of the maximum possible 303df8bae1dSRodney W. Grimes * window, then want to send a window update to peer. 304df8bae1dSRodney W. Grimes */ 305df8bae1dSRodney W. Grimes if (win > 0) { 306df8bae1dSRodney W. Grimes /* 307df8bae1dSRodney W. Grimes * "adv" is the amount we can increase the window, 308df8bae1dSRodney W. Grimes * taking into account that we are limited by 309df8bae1dSRodney W. Grimes * TCP_MAXWIN << tp->rcv_scale. 310df8bae1dSRodney W. Grimes */ 311df8bae1dSRodney W. Grimes long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) - 312df8bae1dSRodney W. Grimes (tp->rcv_adv - tp->rcv_nxt); 313df8bae1dSRodney W. Grimes 314df8bae1dSRodney W. Grimes if (adv >= (long) (2 * tp->t_maxseg)) 315df8bae1dSRodney W. Grimes goto send; 316df8bae1dSRodney W. Grimes if (2 * adv >= (long) so->so_rcv.sb_hiwat) 317df8bae1dSRodney W. Grimes goto send; 318df8bae1dSRodney W. Grimes } 319df8bae1dSRodney W. Grimes 320df8bae1dSRodney W. Grimes /* 321df8bae1dSRodney W. Grimes * Send if we owe peer an ACK. 322df8bae1dSRodney W. Grimes */ 323df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 324df8bae1dSRodney W. Grimes goto send; 325a0292f23SGarrett Wollman if ((flags & TH_RST) || 326a0292f23SGarrett Wollman ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 327df8bae1dSRodney W. Grimes goto send; 328df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_una)) 329df8bae1dSRodney W. Grimes goto send; 330df8bae1dSRodney W. Grimes /* 331df8bae1dSRodney W. Grimes * If our state indicates that FIN should be sent 332df8bae1dSRodney W. Grimes * and we have not yet done so, or we're retransmitting the FIN, 333df8bae1dSRodney W. Grimes * then we need to send. 334df8bae1dSRodney W. Grimes */ 335df8bae1dSRodney W. Grimes if (flags & TH_FIN && 336df8bae1dSRodney W. Grimes ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 337df8bae1dSRodney W. Grimes goto send; 338df8bae1dSRodney W. Grimes 339df8bae1dSRodney W. Grimes /* 340df8bae1dSRodney W. Grimes * TCP window updates are not reliable, rather a polling protocol 341df8bae1dSRodney W. Grimes * using ``persist'' packets is used to insure receipt of window 342df8bae1dSRodney W. Grimes * updates. The three ``states'' for the output side are: 343df8bae1dSRodney W. Grimes * idle not doing retransmits or persists 344df8bae1dSRodney W. Grimes * persisting to move a small or zero window 345df8bae1dSRodney W. Grimes * (re)transmitting and thereby not persisting 346df8bae1dSRodney W. Grimes * 3479b8b58e0SJonathan Lemon * callout_active(tp->tt_persist) 3489b8b58e0SJonathan Lemon * is true when we are in persist state. 349df8bae1dSRodney W. Grimes * tp->t_force 350df8bae1dSRodney W. Grimes * is set when we are called to send a persist packet. 3519b8b58e0SJonathan Lemon * callout_active(tp->tt_rexmt) 352df8bae1dSRodney W. Grimes * is set when we are retransmitting 353df8bae1dSRodney W. Grimes * The output side is idle when both timers are zero. 354df8bae1dSRodney W. Grimes * 355df8bae1dSRodney W. Grimes * If send window is too small, there is data to transmit, and no 356df8bae1dSRodney W. Grimes * retransmit or persist is pending, then go to persist state. 357df8bae1dSRodney W. Grimes * If nothing happens soon, send when timer expires: 358df8bae1dSRodney W. Grimes * if window is nonzero, transmit what we can, 359df8bae1dSRodney W. Grimes * otherwise force out a byte. 360df8bae1dSRodney W. Grimes */ 3619b8b58e0SJonathan Lemon if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) && 3629b8b58e0SJonathan Lemon !callout_active(tp->tt_persist)) { 363df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 364df8bae1dSRodney W. Grimes tcp_setpersist(tp); 365df8bae1dSRodney W. Grimes } 366df8bae1dSRodney W. Grimes 367df8bae1dSRodney W. Grimes /* 368df8bae1dSRodney W. Grimes * No reason to send a segment, just return. 369df8bae1dSRodney W. Grimes */ 370df8bae1dSRodney W. Grimes return (0); 371df8bae1dSRodney W. Grimes 372df8bae1dSRodney W. Grimes send: 373df8bae1dSRodney W. Grimes /* 374df8bae1dSRodney W. Grimes * Before ESTABLISHED, force sending of initial options 375df8bae1dSRodney W. Grimes * unless TCP set not to do any options. 376df8bae1dSRodney W. Grimes * NOTE: we assume that the IP/TCP header plus TCP options 377df8bae1dSRodney W. Grimes * always fit in a single mbuf, leaving room for a maximum 378df8bae1dSRodney W. Grimes * link header, i.e. 37933841545SHajimu UMEMOTO * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 380df8bae1dSRodney W. Grimes */ 381df8bae1dSRodney W. Grimes optlen = 0; 382fb59c426SYoshinobu Inoue #ifdef INET6 383fb59c426SYoshinobu Inoue if (isipv6) 384fb59c426SYoshinobu Inoue hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 385fb59c426SYoshinobu Inoue else 386fb59c426SYoshinobu Inoue #endif 387df8bae1dSRodney W. Grimes hdrlen = sizeof (struct tcpiphdr); 388df8bae1dSRodney W. Grimes if (flags & TH_SYN) { 389df8bae1dSRodney W. Grimes tp->snd_nxt = tp->iss; 390df8bae1dSRodney W. Grimes if ((tp->t_flags & TF_NOOPT) == 0) { 391df8bae1dSRodney W. Grimes u_short mss; 392df8bae1dSRodney W. Grimes 393df8bae1dSRodney W. Grimes opt[0] = TCPOPT_MAXSEG; 394a0292f23SGarrett Wollman opt[1] = TCPOLEN_MAXSEG; 395a0292f23SGarrett Wollman mss = htons((u_short) tcp_mssopt(tp)); 39694a5d9b6SDavid Greenman (void)memcpy(opt + 2, &mss, sizeof(mss)); 397a0292f23SGarrett Wollman optlen = TCPOLEN_MAXSEG; 398df8bae1dSRodney W. Grimes 399df8bae1dSRodney W. Grimes if ((tp->t_flags & TF_REQ_SCALE) && 400df8bae1dSRodney W. Grimes ((flags & TH_ACK) == 0 || 401df8bae1dSRodney W. Grimes (tp->t_flags & TF_RCVD_SCALE))) { 4029105bb46SBruce Evans *((u_int32_t *)(opt + optlen)) = htonl( 403df8bae1dSRodney W. Grimes TCPOPT_NOP << 24 | 404df8bae1dSRodney W. Grimes TCPOPT_WINDOW << 16 | 405df8bae1dSRodney W. Grimes TCPOLEN_WINDOW << 8 | 406df8bae1dSRodney W. Grimes tp->request_r_scale); 407df8bae1dSRodney W. Grimes optlen += 4; 408df8bae1dSRodney W. Grimes } 409df8bae1dSRodney W. Grimes } 410df8bae1dSRodney W. Grimes } 411df8bae1dSRodney W. Grimes 412df8bae1dSRodney W. Grimes /* 413df8bae1dSRodney W. Grimes * Send a timestamp and echo-reply if this is a SYN and our side 414df8bae1dSRodney W. Grimes * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 415df8bae1dSRodney W. Grimes * and our peer have sent timestamps in our SYN's. 416df8bae1dSRodney W. Grimes */ 417df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 418df8bae1dSRodney W. Grimes (flags & TH_RST) == 0 && 419a0292f23SGarrett Wollman ((flags & TH_ACK) == 0 || 420df8bae1dSRodney W. Grimes (tp->t_flags & TF_RCVD_TSTMP))) { 4219105bb46SBruce Evans u_int32_t *lp = (u_int32_t *)(opt + optlen); 422df8bae1dSRodney W. Grimes 423df8bae1dSRodney W. Grimes /* Form timestamp option as shown in appendix A of RFC 1323. */ 424df8bae1dSRodney W. Grimes *lp++ = htonl(TCPOPT_TSTAMP_HDR); 4259b8b58e0SJonathan Lemon *lp++ = htonl(ticks); 426df8bae1dSRodney W. Grimes *lp = htonl(tp->ts_recent); 427df8bae1dSRodney W. Grimes optlen += TCPOLEN_TSTAMP_APPA; 428df8bae1dSRodney W. Grimes } 429df8bae1dSRodney W. Grimes 430a0292f23SGarrett Wollman /* 431a0292f23SGarrett Wollman * Send `CC-family' options if our side wants to use them (TF_REQ_CC), 432a0292f23SGarrett Wollman * options are allowed (!TF_NOOPT) and it's not a RST. 433a0292f23SGarrett Wollman */ 434a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 435a0292f23SGarrett Wollman (flags & TH_RST) == 0) { 436a0292f23SGarrett Wollman switch (flags & (TH_SYN|TH_ACK)) { 437a0292f23SGarrett Wollman /* 438a0292f23SGarrett Wollman * This is a normal ACK, send CC if we received CC before 439a0292f23SGarrett Wollman * from our peer. 440a0292f23SGarrett Wollman */ 441a0292f23SGarrett Wollman case TH_ACK: 442a0292f23SGarrett Wollman if (!(tp->t_flags & TF_RCVD_CC)) 443a0292f23SGarrett Wollman break; 444a0292f23SGarrett Wollman /*FALLTHROUGH*/ 445a0292f23SGarrett Wollman 446a0292f23SGarrett Wollman /* 447a0292f23SGarrett Wollman * We can only get here in T/TCP's SYN_SENT* state, when 448a0292f23SGarrett Wollman * we're a sending a non-SYN segment without waiting for 449a0292f23SGarrett Wollman * the ACK of our SYN. A check above assures that we only 450a0292f23SGarrett Wollman * do this if our peer understands T/TCP. 451a0292f23SGarrett Wollman */ 452a0292f23SGarrett Wollman case 0: 453a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 454a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 455a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_CC; 456a0292f23SGarrett Wollman opt[optlen++] = TCPOLEN_CC; 457a0292f23SGarrett Wollman *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send); 458a0292f23SGarrett Wollman 459a0292f23SGarrett Wollman optlen += 4; 460a0292f23SGarrett Wollman break; 461a0292f23SGarrett Wollman 462a0292f23SGarrett Wollman /* 463a0292f23SGarrett Wollman * This is our initial SYN, check whether we have to use 464a0292f23SGarrett Wollman * CC or CC.new. 465a0292f23SGarrett Wollman */ 466a0292f23SGarrett Wollman case TH_SYN: 467a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 468a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 469a45d2726SAndras Olah opt[optlen++] = tp->t_flags & TF_SENDCCNEW ? 470a45d2726SAndras Olah TCPOPT_CCNEW : TCPOPT_CC; 471a0292f23SGarrett Wollman opt[optlen++] = TCPOLEN_CC; 472a0292f23SGarrett Wollman *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send); 473a0292f23SGarrett Wollman optlen += 4; 474a0292f23SGarrett Wollman break; 475a0292f23SGarrett Wollman 476a0292f23SGarrett Wollman /* 477a0292f23SGarrett Wollman * This is a SYN,ACK; send CC and CC.echo if we received 478a0292f23SGarrett Wollman * CC from our peer. 479a0292f23SGarrett Wollman */ 480a0292f23SGarrett Wollman case (TH_SYN|TH_ACK): 481a0292f23SGarrett Wollman if (tp->t_flags & TF_RCVD_CC) { 482a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 483a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 484a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_CC; 485a0292f23SGarrett Wollman opt[optlen++] = TCPOLEN_CC; 486a0292f23SGarrett Wollman *(u_int32_t *)&opt[optlen] = 487a0292f23SGarrett Wollman htonl(tp->cc_send); 488a0292f23SGarrett Wollman optlen += 4; 489a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 490a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_NOP; 491a0292f23SGarrett Wollman opt[optlen++] = TCPOPT_CCECHO; 492a0292f23SGarrett Wollman opt[optlen++] = TCPOLEN_CC; 493a0292f23SGarrett Wollman *(u_int32_t *)&opt[optlen] = 494a0292f23SGarrett Wollman htonl(tp->cc_recv); 495a0292f23SGarrett Wollman optlen += 4; 496a0292f23SGarrett Wollman } 497a0292f23SGarrett Wollman break; 498a0292f23SGarrett Wollman } 499a0292f23SGarrett Wollman } 500a0292f23SGarrett Wollman 501df8bae1dSRodney W. Grimes hdrlen += optlen; 502df8bae1dSRodney W. Grimes 503fb59c426SYoshinobu Inoue #ifdef INET6 504fb59c426SYoshinobu Inoue if (isipv6) 505fb59c426SYoshinobu Inoue ipoptlen = ip6_optlen(tp->t_inpcb); 506fb59c426SYoshinobu Inoue else 507fb59c426SYoshinobu Inoue #endif 508fb59c426SYoshinobu Inoue { 509a04884fcSBill Fenner if (tp->t_inpcb->inp_options) { 510a04884fcSBill Fenner ipoptlen = tp->t_inpcb->inp_options->m_len - 511a04884fcSBill Fenner offsetof(struct ipoption, ipopt_list); 512a04884fcSBill Fenner } else { 513a04884fcSBill Fenner ipoptlen = 0; 514a04884fcSBill Fenner } 515fb59c426SYoshinobu Inoue } 516fb59c426SYoshinobu Inoue #ifdef IPSEC 517fb59c426SYoshinobu Inoue ipoptlen += ipsec_hdrsiz_tcp(tp); 518fb59c426SYoshinobu Inoue #endif 519a04884fcSBill Fenner 520df8bae1dSRodney W. Grimes /* 521df8bae1dSRodney W. Grimes * Adjust data length if insertion of options will 522a0292f23SGarrett Wollman * bump the packet length beyond the t_maxopd length. 523a0292f23SGarrett Wollman * Clear the FIN bit because we cut off the tail of 524a0292f23SGarrett Wollman * the segment. 525df8bae1dSRodney W. Grimes */ 526a04884fcSBill Fenner if (len + optlen + ipoptlen > tp->t_maxopd) { 527297a37f3SDavid Greenman /* 528297a37f3SDavid Greenman * If there is still more to send, don't close the connection. 529297a37f3SDavid Greenman */ 530297a37f3SDavid Greenman flags &= ~TH_FIN; 531a04884fcSBill Fenner len = tp->t_maxopd - optlen - ipoptlen; 532df8bae1dSRodney W. Grimes sendalot = 1; 533df8bae1dSRodney W. Grimes } 534df8bae1dSRodney W. Grimes 535a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/ 536a683a7ddSYoshinobu Inoue #ifdef INET6 537a683a7ddSYoshinobu Inoue if (max_linkhdr + hdrlen > MCLBYTES) 538a683a7ddSYoshinobu Inoue panic("tcphdr too big"); 539a683a7ddSYoshinobu Inoue #else 540df8bae1dSRodney W. Grimes if (max_linkhdr + hdrlen > MHLEN) 541df8bae1dSRodney W. Grimes panic("tcphdr too big"); 542a683a7ddSYoshinobu Inoue #endif 543a0292f23SGarrett Wollman /*#endif*/ 544df8bae1dSRodney W. Grimes 545df8bae1dSRodney W. Grimes /* 546df8bae1dSRodney W. Grimes * Grab a header mbuf, attaching a copy of data to 547df8bae1dSRodney W. Grimes * be transmitted, and initialize the header from 548df8bae1dSRodney W. Grimes * the template for sends on this connection. 549df8bae1dSRodney W. Grimes */ 550df8bae1dSRodney W. Grimes if (len) { 551df8bae1dSRodney W. Grimes if (tp->t_force && len == 1) 552df8bae1dSRodney W. Grimes tcpstat.tcps_sndprobe++; 553df8bae1dSRodney W. Grimes else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 554df8bae1dSRodney W. Grimes tcpstat.tcps_sndrexmitpack++; 555df8bae1dSRodney W. Grimes tcpstat.tcps_sndrexmitbyte += len; 556df8bae1dSRodney W. Grimes } else { 557df8bae1dSRodney W. Grimes tcpstat.tcps_sndpack++; 558df8bae1dSRodney W. Grimes tcpstat.tcps_sndbyte += len; 559df8bae1dSRodney W. Grimes } 560df8bae1dSRodney W. Grimes #ifdef notyet 561df8bae1dSRodney W. Grimes if ((m = m_copypack(so->so_snd.sb_mb, off, 562df8bae1dSRodney W. Grimes (int)len, max_linkhdr + hdrlen)) == 0) { 563df8bae1dSRodney W. Grimes error = ENOBUFS; 564df8bae1dSRodney W. Grimes goto out; 565df8bae1dSRodney W. Grimes } 566df8bae1dSRodney W. Grimes /* 567df8bae1dSRodney W. Grimes * m_copypack left space for our hdr; use it. 568df8bae1dSRodney W. Grimes */ 569df8bae1dSRodney W. Grimes m->m_len += hdrlen; 570df8bae1dSRodney W. Grimes m->m_data -= hdrlen; 571df8bae1dSRodney W. Grimes #else 572df8bae1dSRodney W. Grimes MGETHDR(m, M_DONTWAIT, MT_HEADER); 573df8bae1dSRodney W. Grimes if (m == NULL) { 574df8bae1dSRodney W. Grimes error = ENOBUFS; 575df8bae1dSRodney W. Grimes goto out; 576df8bae1dSRodney W. Grimes } 577fb59c426SYoshinobu Inoue #ifdef INET6 578a683a7ddSYoshinobu Inoue if (MHLEN < hdrlen + max_linkhdr) { 579a683a7ddSYoshinobu Inoue MCLGET(m, M_DONTWAIT); 580a683a7ddSYoshinobu Inoue if ((m->m_flags & M_EXT) == 0) { 581a683a7ddSYoshinobu Inoue m_freem(m); 582a683a7ddSYoshinobu Inoue error = ENOBUFS; 583a683a7ddSYoshinobu Inoue goto out; 584a683a7ddSYoshinobu Inoue } 585a683a7ddSYoshinobu Inoue } 586fb59c426SYoshinobu Inoue #endif 587df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 588df8bae1dSRodney W. Grimes m->m_len = hdrlen; 589df8bae1dSRodney W. Grimes if (len <= MHLEN - hdrlen - max_linkhdr) { 590df8bae1dSRodney W. Grimes m_copydata(so->so_snd.sb_mb, off, (int) len, 591df8bae1dSRodney W. Grimes mtod(m, caddr_t) + hdrlen); 592df8bae1dSRodney W. Grimes m->m_len += len; 593df8bae1dSRodney W. Grimes } else { 594df8bae1dSRodney W. Grimes m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 59551823c3aSGarrett Wollman if (m->m_next == 0) { 596d7f570e6SGarrett Wollman (void) m_free(m); 59751823c3aSGarrett Wollman error = ENOBUFS; 59851823c3aSGarrett Wollman goto out; 59951823c3aSGarrett Wollman } 600df8bae1dSRodney W. Grimes } 601df8bae1dSRodney W. Grimes #endif 602df8bae1dSRodney W. Grimes /* 603df8bae1dSRodney W. Grimes * If we're sending everything we've got, set PUSH. 604df8bae1dSRodney W. Grimes * (This will keep happy those implementations which only 605df8bae1dSRodney W. Grimes * give data to the user when a buffer fills or 606df8bae1dSRodney W. Grimes * a PUSH comes in.) 607df8bae1dSRodney W. Grimes */ 608df8bae1dSRodney W. Grimes if (off + len == so->so_snd.sb_cc) 609df8bae1dSRodney W. Grimes flags |= TH_PUSH; 610df8bae1dSRodney W. Grimes } else { 611df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 612df8bae1dSRodney W. Grimes tcpstat.tcps_sndacks++; 613df8bae1dSRodney W. Grimes else if (flags & (TH_SYN|TH_FIN|TH_RST)) 614df8bae1dSRodney W. Grimes tcpstat.tcps_sndctrl++; 615df8bae1dSRodney W. Grimes else if (SEQ_GT(tp->snd_up, tp->snd_una)) 616df8bae1dSRodney W. Grimes tcpstat.tcps_sndurg++; 617df8bae1dSRodney W. Grimes else 618df8bae1dSRodney W. Grimes tcpstat.tcps_sndwinup++; 619df8bae1dSRodney W. Grimes 620df8bae1dSRodney W. Grimes MGETHDR(m, M_DONTWAIT, MT_HEADER); 621df8bae1dSRodney W. Grimes if (m == NULL) { 622df8bae1dSRodney W. Grimes error = ENOBUFS; 623df8bae1dSRodney W. Grimes goto out; 624df8bae1dSRodney W. Grimes } 625fb59c426SYoshinobu Inoue #ifdef INET6 626fb59c426SYoshinobu Inoue if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 627fb59c426SYoshinobu Inoue MHLEN >= hdrlen) { 628fb59c426SYoshinobu Inoue MH_ALIGN(m, hdrlen); 629fb59c426SYoshinobu Inoue } else 630fb59c426SYoshinobu Inoue #endif 631df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 632df8bae1dSRodney W. Grimes m->m_len = hdrlen; 633df8bae1dSRodney W. Grimes } 634df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 635df8bae1dSRodney W. Grimes if (tp->t_template == 0) 636df8bae1dSRodney W. Grimes panic("tcp_output"); 637fb59c426SYoshinobu Inoue #ifdef INET6 638fb59c426SYoshinobu Inoue if (isipv6) { 639fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 640fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip6 + 1); 641fb59c426SYoshinobu Inoue bcopy((caddr_t)tp->t_template->tt_ipgen, (caddr_t)ip6, 642fb59c426SYoshinobu Inoue sizeof(struct ip6_hdr)); 643fb59c426SYoshinobu Inoue bcopy((caddr_t)&tp->t_template->tt_t, (caddr_t)th, 644fb59c426SYoshinobu Inoue sizeof(struct tcphdr)); 645fb59c426SYoshinobu Inoue } else 646fb59c426SYoshinobu Inoue #endif /* INET6 */ 647fb59c426SYoshinobu Inoue { 648fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 649fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 650fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip + 1); 651db4f9cc7SJonathan Lemon /* this picks up the pseudo header (w/o the length) */ 652fb59c426SYoshinobu Inoue bcopy((caddr_t)tp->t_template->tt_ipgen, (caddr_t)ip, 653fb59c426SYoshinobu Inoue sizeof(struct ip)); 654fb59c426SYoshinobu Inoue bcopy((caddr_t)&tp->t_template->tt_t, (caddr_t)th, 655fb59c426SYoshinobu Inoue sizeof(struct tcphdr)); 656fb59c426SYoshinobu Inoue } 657df8bae1dSRodney W. Grimes 658df8bae1dSRodney W. Grimes /* 659df8bae1dSRodney W. Grimes * Fill in fields, remembering maximum advertised 660df8bae1dSRodney W. Grimes * window for use in delaying messages about window sizes. 661df8bae1dSRodney W. Grimes * If resending a FIN, be sure not to use a new sequence number. 662df8bae1dSRodney W. Grimes */ 663df8bae1dSRodney W. Grimes if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 664df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) 665df8bae1dSRodney W. Grimes tp->snd_nxt--; 666df8bae1dSRodney W. Grimes /* 667df8bae1dSRodney W. Grimes * If we are doing retransmissions, then snd_nxt will 668df8bae1dSRodney W. Grimes * not reflect the first unsent octet. For ACK only 669df8bae1dSRodney W. Grimes * packets, we do not want the sequence number of the 670df8bae1dSRodney W. Grimes * retransmitted packet, we want the sequence number 671df8bae1dSRodney W. Grimes * of the next unsent octet. So, if there is no data 672df8bae1dSRodney W. Grimes * (and no SYN or FIN), use snd_max instead of snd_nxt 673df8bae1dSRodney W. Grimes * when filling in ti_seq. But if we are in persist 674df8bae1dSRodney W. Grimes * state, snd_max might reflect one byte beyond the 675df8bae1dSRodney W. Grimes * right edge of the window, so use snd_nxt in that 676df8bae1dSRodney W. Grimes * case, since we know we aren't doing a retransmission. 677df8bae1dSRodney W. Grimes * (retransmit and persist are mutually exclusive...) 678df8bae1dSRodney W. Grimes */ 6799b8b58e0SJonathan Lemon if (len || (flags & (TH_SYN|TH_FIN)) 6809b8b58e0SJonathan Lemon || callout_active(tp->tt_persist)) 681fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_nxt); 682df8bae1dSRodney W. Grimes else 683fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_max); 684fb59c426SYoshinobu Inoue th->th_ack = htonl(tp->rcv_nxt); 685df8bae1dSRodney W. Grimes if (optlen) { 686fb59c426SYoshinobu Inoue bcopy(opt, th + 1, optlen); 687fb59c426SYoshinobu Inoue th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 688df8bae1dSRodney W. Grimes } 689fb59c426SYoshinobu Inoue th->th_flags = flags; 690df8bae1dSRodney W. Grimes /* 691df8bae1dSRodney W. Grimes * Calculate receive window. Don't shrink window, 692df8bae1dSRodney W. Grimes * but avoid silly window syndrome. 693df8bae1dSRodney W. Grimes */ 694df8bae1dSRodney W. Grimes if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) 695df8bae1dSRodney W. Grimes win = 0; 696df8bae1dSRodney W. Grimes if (win < (long)(tp->rcv_adv - tp->rcv_nxt)) 697df8bae1dSRodney W. Grimes win = (long)(tp->rcv_adv - tp->rcv_nxt); 698610a2e9cSBill Fenner if (win > (long)TCP_MAXWIN << tp->rcv_scale) 699610a2e9cSBill Fenner win = (long)TCP_MAXWIN << tp->rcv_scale; 700fb59c426SYoshinobu Inoue th->th_win = htons((u_short) (win>>tp->rcv_scale)); 701df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 702fb59c426SYoshinobu Inoue th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 703fb59c426SYoshinobu Inoue th->th_flags |= TH_URG; 704df8bae1dSRodney W. Grimes } else 705df8bae1dSRodney W. Grimes /* 706df8bae1dSRodney W. Grimes * If no urgent pointer to send, then we pull 707df8bae1dSRodney W. Grimes * the urgent pointer to the left edge of the send window 708df8bae1dSRodney W. Grimes * so that it doesn't drift into the send window on sequence 709df8bae1dSRodney W. Grimes * number wraparound. 710df8bae1dSRodney W. Grimes */ 711df8bae1dSRodney W. Grimes tp->snd_up = tp->snd_una; /* drag it along */ 712df8bae1dSRodney W. Grimes 713df8bae1dSRodney W. Grimes /* 714df8bae1dSRodney W. Grimes * Put TCP length in extended header, and then 715df8bae1dSRodney W. Grimes * checksum extended header and data. 716df8bae1dSRodney W. Grimes */ 717fb59c426SYoshinobu Inoue m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 718fb59c426SYoshinobu Inoue #ifdef INET6 719fb59c426SYoshinobu Inoue if (isipv6) 720fb59c426SYoshinobu Inoue /* 721fb59c426SYoshinobu Inoue * ip6_plen is not need to be filled now, and will be filled 722fb59c426SYoshinobu Inoue * in ip6_output. 723fb59c426SYoshinobu Inoue */ 724fb59c426SYoshinobu Inoue th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 725fb59c426SYoshinobu Inoue sizeof(struct tcphdr) + optlen + len); 726fb59c426SYoshinobu Inoue else 727fb59c426SYoshinobu Inoue #endif /* INET6 */ 728fb59c426SYoshinobu Inoue { 729db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_TCP; 730db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 731df8bae1dSRodney W. Grimes if (len + optlen) 732db4f9cc7SJonathan Lemon th->th_sum = in_addword(th->th_sum, 733db4f9cc7SJonathan Lemon htons((u_short)(optlen + len))); 734fb59c426SYoshinobu Inoue 735db4f9cc7SJonathan Lemon /* IP version must be set here for ipv4/ipv6 checking later */ 736db4f9cc7SJonathan Lemon KASSERT(ip->ip_v == IPVERSION, 737db4f9cc7SJonathan Lemon ("%s: IP version incorrect: %d", __FUNCTION__, ip->ip_v)); 738fb59c426SYoshinobu Inoue } 739df8bae1dSRodney W. Grimes 740df8bae1dSRodney W. Grimes /* 741df8bae1dSRodney W. Grimes * In transmit state, time the transmission and arrange for 742df8bae1dSRodney W. Grimes * the retransmit. In persist state, just set snd_max. 743df8bae1dSRodney W. Grimes */ 7449b8b58e0SJonathan Lemon if (tp->t_force == 0 || !callout_active(tp->tt_persist)) { 745df8bae1dSRodney W. Grimes tcp_seq startseq = tp->snd_nxt; 746df8bae1dSRodney W. Grimes 747df8bae1dSRodney W. Grimes /* 748df8bae1dSRodney W. Grimes * Advance snd_nxt over sequence space of this segment. 749df8bae1dSRodney W. Grimes */ 750df8bae1dSRodney W. Grimes if (flags & (TH_SYN|TH_FIN)) { 751df8bae1dSRodney W. Grimes if (flags & TH_SYN) 752df8bae1dSRodney W. Grimes tp->snd_nxt++; 753df8bae1dSRodney W. Grimes if (flags & TH_FIN) { 754df8bae1dSRodney W. Grimes tp->snd_nxt++; 755df8bae1dSRodney W. Grimes tp->t_flags |= TF_SENTFIN; 756df8bae1dSRodney W. Grimes } 757df8bae1dSRodney W. Grimes } 758df8bae1dSRodney W. Grimes tp->snd_nxt += len; 759df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 760df8bae1dSRodney W. Grimes tp->snd_max = tp->snd_nxt; 761df8bae1dSRodney W. Grimes /* 762df8bae1dSRodney W. Grimes * Time this transmission if not a retransmission and 763df8bae1dSRodney W. Grimes * not currently timing anything. 764df8bae1dSRodney W. Grimes */ 7659b8b58e0SJonathan Lemon if (tp->t_rtttime == 0) { 7669b8b58e0SJonathan Lemon tp->t_rtttime = ticks; 767df8bae1dSRodney W. Grimes tp->t_rtseq = startseq; 768df8bae1dSRodney W. Grimes tcpstat.tcps_segstimed++; 769df8bae1dSRodney W. Grimes } 770df8bae1dSRodney W. Grimes } 771df8bae1dSRodney W. Grimes 772df8bae1dSRodney W. Grimes /* 773df8bae1dSRodney W. Grimes * Set retransmit timer if not currently set, 774df8bae1dSRodney W. Grimes * and not doing an ack or a keep-alive probe. 775df8bae1dSRodney W. Grimes * Initial value for retransmit timer is smoothed 776df8bae1dSRodney W. Grimes * round-trip time + 2 * round-trip time variance. 777df8bae1dSRodney W. Grimes * Initialize shift counter which is used for backoff 778df8bae1dSRodney W. Grimes * of retransmit time. 779df8bae1dSRodney W. Grimes */ 7809b8b58e0SJonathan Lemon if (!callout_active(tp->tt_rexmt) && 781df8bae1dSRodney W. Grimes tp->snd_nxt != tp->snd_una) { 7829b8b58e0SJonathan Lemon if (callout_active(tp->tt_persist)) { 7839b8b58e0SJonathan Lemon callout_stop(tp->tt_persist); 784df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 785df8bae1dSRodney W. Grimes } 78646f58482SJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 78746f58482SJonathan Lemon tcp_timer_rexmt, tp); 788df8bae1dSRodney W. Grimes } 789df8bae1dSRodney W. Grimes } else 790df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 791df8bae1dSRodney W. Grimes tp->snd_max = tp->snd_nxt + len; 792df8bae1dSRodney W. Grimes 793610ee2f9SDavid Greenman #ifdef TCPDEBUG 794df8bae1dSRodney W. Grimes /* 795df8bae1dSRodney W. Grimes * Trace. 796df8bae1dSRodney W. Grimes */ 797df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) 798fb59c426SYoshinobu Inoue tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 799610ee2f9SDavid Greenman #endif 800df8bae1dSRodney W. Grimes 801df8bae1dSRodney W. Grimes /* 802df8bae1dSRodney W. Grimes * Fill in IP length and desired time to live and 803df8bae1dSRodney W. Grimes * send to IP level. There should be a better way 804df8bae1dSRodney W. Grimes * to handle ttl and tos; we could keep them in 805df8bae1dSRodney W. Grimes * the template, but need a way to checksum without them. 806df8bae1dSRodney W. Grimes */ 807fb59c426SYoshinobu Inoue /* 808fb59c426SYoshinobu Inoue * m->m_pkthdr.len should have been set before cksum calcuration, 809fb59c426SYoshinobu Inoue * because in6_cksum() need it. 810fb59c426SYoshinobu Inoue */ 811fb59c426SYoshinobu Inoue #ifdef INET6 812fb59c426SYoshinobu Inoue if (isipv6) { 813fb59c426SYoshinobu Inoue /* 814fb59c426SYoshinobu Inoue * we separately set hoplimit for every segment, since the 815fb59c426SYoshinobu Inoue * user might want to change the value via setsockopt. 816fb59c426SYoshinobu Inoue * Also, desired default hop limit might be changed via 817fb59c426SYoshinobu Inoue * Neighbor Discovery. 818fb59c426SYoshinobu Inoue */ 819fb59c426SYoshinobu Inoue ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, 820fb59c426SYoshinobu Inoue tp->t_inpcb->in6p_route.ro_rt ? 821fb59c426SYoshinobu Inoue tp->t_inpcb->in6p_route.ro_rt->rt_ifp 822fb59c426SYoshinobu Inoue : NULL); 823fb59c426SYoshinobu Inoue 824fb59c426SYoshinobu Inoue /* TODO: IPv6 IP6TOS_ECT bit on */ 825fb59c426SYoshinobu Inoue #ifdef IPSEC 82633841545SHajimu UMEMOTO if (ipsec_setsocket(m, so) != 0) { 82733841545SHajimu UMEMOTO m_freem(m); 82833841545SHajimu UMEMOTO error = ENOBUFS; 82933841545SHajimu UMEMOTO goto out; 83033841545SHajimu UMEMOTO } 831fb59c426SYoshinobu Inoue #endif /*IPSEC*/ 832fb59c426SYoshinobu Inoue error = ip6_output(m, 833fb59c426SYoshinobu Inoue tp->t_inpcb->in6p_outputopts, 834fb59c426SYoshinobu Inoue &tp->t_inpcb->in6p_route, 835686cdd19SJun-ichiro itojun Hagino (so->so_options & SO_DONTROUTE), NULL, NULL); 836fb59c426SYoshinobu Inoue } else 837fb59c426SYoshinobu Inoue #endif /* INET6 */ 838df8bae1dSRodney W. Grimes { 839f138387aSGarrett Wollman struct rtentry *rt; 840fb59c426SYoshinobu Inoue ip->ip_len = m->m_pkthdr.len; 841686cdd19SJun-ichiro itojun Hagino #ifdef INET6 842686cdd19SJun-ichiro itojun Hagino if (INP_CHECK_SOCKAF(so, AF_INET6)) 843686cdd19SJun-ichiro itojun Hagino ip->ip_ttl = in6_selecthlim(tp->t_inpcb, 844686cdd19SJun-ichiro itojun Hagino tp->t_inpcb->in6p_route.ro_rt ? 845686cdd19SJun-ichiro itojun Hagino tp->t_inpcb->in6p_route.ro_rt->rt_ifp 846686cdd19SJun-ichiro itojun Hagino : NULL); 847686cdd19SJun-ichiro itojun Hagino else 848686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */ 849fb59c426SYoshinobu Inoue ip->ip_ttl = tp->t_inpcb->inp_ip_ttl; /* XXX */ 850fb59c426SYoshinobu Inoue ip->ip_tos = tp->t_inpcb->inp_ip_tos; /* XXX */ 851f138387aSGarrett Wollman /* 852f138387aSGarrett Wollman * See if we should do MTU discovery. We do it only if the following 853f138387aSGarrett Wollman * are true: 854f138387aSGarrett Wollman * 1) we have a valid route to the destination 855f138387aSGarrett Wollman * 2) the MTU is not locked (if it is, then discovery has been 856f138387aSGarrett Wollman * disabled) 857f138387aSGarrett Wollman */ 8589a039a5fSDavid Greenman if (path_mtu_discovery 8599a039a5fSDavid Greenman && (rt = tp->t_inpcb->inp_route.ro_rt) 860f138387aSGarrett Wollman && rt->rt_flags & RTF_UP 861f138387aSGarrett Wollman && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { 862fb59c426SYoshinobu Inoue ip->ip_off |= IP_DF; 863f138387aSGarrett Wollman } 864686cdd19SJun-ichiro itojun Hagino #ifdef IPSEC 865686cdd19SJun-ichiro itojun Hagino ipsec_setsocket(m, so); 866686cdd19SJun-ichiro itojun Hagino #endif /*IPSEC*/ 867df8bae1dSRodney W. Grimes error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 868686cdd19SJun-ichiro itojun Hagino (so->so_options & SO_DONTROUTE), 0); 869df8bae1dSRodney W. Grimes } 870df8bae1dSRodney W. Grimes if (error) { 8717734ea06SArchie Cobbs 8727734ea06SArchie Cobbs /* 8737734ea06SArchie Cobbs * We know that the packet was lost, so back out the 8747734ea06SArchie Cobbs * sequence number advance, if any. 8757734ea06SArchie Cobbs */ 8767734ea06SArchie Cobbs if (tp->t_force == 0 || !callout_active(tp->tt_persist)) { 8777734ea06SArchie Cobbs /* 8787734ea06SArchie Cobbs * No need to check for TH_FIN here because 8797734ea06SArchie Cobbs * the TF_SENTFIN flag handles that case. 8807734ea06SArchie Cobbs */ 8816612c70eSArchie Cobbs if ((flags & TH_SYN) == 0) 8827734ea06SArchie Cobbs tp->snd_nxt -= len; 8837734ea06SArchie Cobbs } 8847734ea06SArchie Cobbs 885df8bae1dSRodney W. Grimes out: 886df8bae1dSRodney W. Grimes if (error == ENOBUFS) { 88759f577adSJonathan Lemon if (!callout_active(tp->tt_rexmt) && 88859f577adSJonathan Lemon !callout_active(tp->tt_persist)) 88959f577adSJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 89059f577adSJonathan Lemon tcp_timer_rexmt, tp); 891df8bae1dSRodney W. Grimes tcp_quench(tp->t_inpcb, 0); 892df8bae1dSRodney W. Grimes return (0); 893df8bae1dSRodney W. Grimes } 8943d1f141bSGarrett Wollman if (error == EMSGSIZE) { 8953d1f141bSGarrett Wollman /* 8963d1f141bSGarrett Wollman * ip_output() will have already fixed the route 8973d1f141bSGarrett Wollman * for us. tcp_mtudisc() will, as its last action, 8983d1f141bSGarrett Wollman * initiate retransmission, so it is important to 8993d1f141bSGarrett Wollman * not do so here. 9003d1f141bSGarrett Wollman */ 9013d1f141bSGarrett Wollman tcp_mtudisc(tp->t_inpcb, 0); 9023d1f141bSGarrett Wollman return 0; 9033d1f141bSGarrett Wollman } 904df8bae1dSRodney W. Grimes if ((error == EHOSTUNREACH || error == ENETDOWN) 905df8bae1dSRodney W. Grimes && TCPS_HAVERCVDSYN(tp->t_state)) { 906df8bae1dSRodney W. Grimes tp->t_softerror = error; 907df8bae1dSRodney W. Grimes return (0); 908df8bae1dSRodney W. Grimes } 909df8bae1dSRodney W. Grimes return (error); 910df8bae1dSRodney W. Grimes } 911df8bae1dSRodney W. Grimes tcpstat.tcps_sndtotal++; 912df8bae1dSRodney W. Grimes 913df8bae1dSRodney W. Grimes /* 914df8bae1dSRodney W. Grimes * Data sent (as far as we can tell). 915df8bae1dSRodney W. Grimes * If this advertises a larger window than any other segment, 916df8bae1dSRodney W. Grimes * then remember the size of the advertised window. 917df8bae1dSRodney W. Grimes * Any pending ACK has now been sent. 918df8bae1dSRodney W. Grimes */ 919df8bae1dSRodney W. Grimes if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 920df8bae1dSRodney W. Grimes tp->rcv_adv = tp->rcv_nxt + win; 921df8bae1dSRodney W. Grimes tp->last_ack_sent = tp->rcv_nxt; 9229b8b58e0SJonathan Lemon tp->t_flags &= ~TF_ACKNOW; 9239b8b58e0SJonathan Lemon if (tcp_delack_enabled) 9249b8b58e0SJonathan Lemon callout_stop(tp->tt_delack); 92546f58482SJonathan Lemon if (sendalot && (!tcp_do_newreno || --maxburst)) 926df8bae1dSRodney W. Grimes goto again; 927df8bae1dSRodney W. Grimes return (0); 928df8bae1dSRodney W. Grimes } 929df8bae1dSRodney W. Grimes 930df8bae1dSRodney W. Grimes void 931df8bae1dSRodney W. Grimes tcp_setpersist(tp) 932df8bae1dSRodney W. Grimes register struct tcpcb *tp; 933df8bae1dSRodney W. Grimes { 9349b8b58e0SJonathan Lemon int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 9359b8b58e0SJonathan Lemon int tt; 936df8bae1dSRodney W. Grimes 9379b8b58e0SJonathan Lemon if (callout_active(tp->tt_rexmt)) 9389b8b58e0SJonathan Lemon panic("tcp_setpersist: retransmit pending"); 939df8bae1dSRodney W. Grimes /* 940df8bae1dSRodney W. Grimes * Start/restart persistance timer. 941df8bae1dSRodney W. Grimes */ 9429b8b58e0SJonathan Lemon TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 943df8bae1dSRodney W. Grimes TCPTV_PERSMIN, TCPTV_PERSMAX); 9449b8b58e0SJonathan Lemon callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp); 945df8bae1dSRodney W. Grimes if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 946df8bae1dSRodney W. Grimes tp->t_rxtshift++; 947df8bae1dSRodney W. Grimes } 948