Home | History | Annotate | Line # | Download | only in netinet
tcp_output.c revision 1.75
      1  1.75     lukem /*	$NetBSD: tcp_output.c,v 1.75 2001/11/13 00:32:41 lukem Exp $	*/
      2  1.48    itojun 
      3  1.48    itojun /*
      4  1.48    itojun %%% portions-copyright-nrl-95
      5  1.48    itojun Portions of this software are Copyright 1995-1998 by Randall Atkinson,
      6  1.48    itojun Ronald Lee, Daniel McDonald, Bao Phan, and Chris Winters. All Rights
      7  1.48    itojun Reserved. All rights under this copyright have been assigned to the US
      8  1.48    itojun Naval Research Laboratory (NRL). The NRL Copyright Notice and License
      9  1.48    itojun Agreement Version 1.1 (January 17, 1995) applies to these portions of the
     10  1.48    itojun software.
     11  1.48    itojun You should have received a copy of the license with this software. If you
     12  1.48    itojun didn't get a copy, you may request one from <license (at) ipv6.nrl.navy.mil>.
     13  1.48    itojun 
     14  1.48    itojun */
     15  1.48    itojun 
     16  1.48    itojun /*
     17  1.48    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
     18  1.48    itojun  * All rights reserved.
     19  1.48    itojun  *
     20  1.48    itojun  * Redistribution and use in source and binary forms, with or without
     21  1.48    itojun  * modification, are permitted provided that the following conditions
     22  1.48    itojun  * are met:
     23  1.48    itojun  * 1. Redistributions of source code must retain the above copyright
     24  1.48    itojun  *    notice, this list of conditions and the following disclaimer.
     25  1.48    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     26  1.48    itojun  *    notice, this list of conditions and the following disclaimer in the
     27  1.48    itojun  *    documentation and/or other materials provided with the distribution.
     28  1.48    itojun  * 3. Neither the name of the project nor the names of its contributors
     29  1.48    itojun  *    may be used to endorse or promote products derived from this software
     30  1.48    itojun  *    without specific prior written permission.
     31  1.48    itojun  *
     32  1.48    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     33  1.48    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  1.48    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  1.48    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     36  1.48    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  1.48    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  1.48    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  1.48    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  1.48    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  1.48    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  1.48    itojun  * SUCH DAMAGE.
     43  1.48    itojun  */
     44  1.28   thorpej 
     45  1.28   thorpej /*-
     46  1.64   thorpej  * Copyright (c) 1997, 1998, 2001 The NetBSD Foundation, Inc.
     47  1.28   thorpej  * All rights reserved.
     48  1.28   thorpej  *
     49  1.28   thorpej  * This code is derived from software contributed to The NetBSD Foundation
     50  1.28   thorpej  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
     51  1.28   thorpej  * Facility, NASA Ames Research Center.
     52  1.28   thorpej  *
     53  1.28   thorpej  * Redistribution and use in source and binary forms, with or without
     54  1.28   thorpej  * modification, are permitted provided that the following conditions
     55  1.28   thorpej  * are met:
     56  1.28   thorpej  * 1. Redistributions of source code must retain the above copyright
     57  1.28   thorpej  *    notice, this list of conditions and the following disclaimer.
     58  1.28   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     59  1.28   thorpej  *    notice, this list of conditions and the following disclaimer in the
     60  1.28   thorpej  *    documentation and/or other materials provided with the distribution.
     61  1.28   thorpej  * 3. All advertising materials mentioning features or use of this software
     62  1.28   thorpej  *    must display the following acknowledgement:
     63  1.28   thorpej  *	This product includes software developed by the NetBSD
     64  1.28   thorpej  *	Foundation, Inc. and its contributors.
     65  1.28   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     66  1.28   thorpej  *    contributors may be used to endorse or promote products derived
     67  1.28   thorpej  *    from this software without specific prior written permission.
     68  1.28   thorpej  *
     69  1.28   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     70  1.28   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     71  1.28   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     72  1.28   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     73  1.28   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     74  1.28   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     75  1.28   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     76  1.28   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     77  1.28   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     78  1.28   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     79  1.28   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     80  1.28   thorpej  */
     81  1.10       cgd 
     82   1.1       cgd /*
     83  1.27   thorpej  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
     84   1.9   mycroft  *	The Regents of the University of California.  All rights reserved.
     85   1.1       cgd  *
     86   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     87   1.1       cgd  * modification, are permitted provided that the following conditions
     88   1.1       cgd  * are met:
     89   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     90   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     91   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     92   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     93   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     94   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     95   1.1       cgd  *    must display the following acknowledgement:
     96   1.1       cgd  *	This product includes software developed by the University of
     97   1.1       cgd  *	California, Berkeley and its contributors.
     98   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     99   1.1       cgd  *    may be used to endorse or promote products derived from this software
    100   1.1       cgd  *    without specific prior written permission.
    101   1.1       cgd  *
    102   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    103   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    104   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    105   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    106   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    107   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    108   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    109   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    110   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    111   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    112   1.1       cgd  * SUCH DAMAGE.
    113   1.1       cgd  *
    114  1.27   thorpej  *	@(#)tcp_output.c	8.4 (Berkeley) 5/24/95
    115   1.1       cgd  */
    116  1.75     lukem 
    117  1.75     lukem #include <sys/cdefs.h>
    118  1.75     lukem __KERNEL_RCSID(0, "$NetBSD: tcp_output.c,v 1.75 2001/11/13 00:32:41 lukem Exp $");
    119   1.1       cgd 
    120  1.48    itojun #include "opt_inet.h"
    121  1.51   thorpej #include "opt_ipsec.h"
    122  1.67       abs #include "opt_tcp_debug.h"
    123  1.48    itojun 
    124   1.4   mycroft #include <sys/param.h>
    125   1.4   mycroft #include <sys/systm.h>
    126   1.4   mycroft #include <sys/malloc.h>
    127   1.4   mycroft #include <sys/mbuf.h>
    128   1.4   mycroft #include <sys/protosw.h>
    129   1.4   mycroft #include <sys/socket.h>
    130   1.4   mycroft #include <sys/socketvar.h>
    131   1.4   mycroft #include <sys/errno.h>
    132  1.48    itojun #include <sys/domain.h>
    133  1.74   thorpej #include <sys/kernel.h>
    134   1.1       cgd 
    135  1.17   thorpej #include <net/if.h>
    136   1.4   mycroft #include <net/route.h>
    137   1.1       cgd 
    138   1.4   mycroft #include <netinet/in.h>
    139   1.4   mycroft #include <netinet/in_systm.h>
    140   1.4   mycroft #include <netinet/ip.h>
    141   1.4   mycroft #include <netinet/in_pcb.h>
    142   1.4   mycroft #include <netinet/ip_var.h>
    143  1.48    itojun 
    144  1.48    itojun #ifdef INET6
    145  1.48    itojun #ifndef INET
    146  1.48    itojun #include <netinet/in.h>
    147  1.48    itojun #endif
    148  1.48    itojun #include <netinet/ip6.h>
    149  1.48    itojun #include <netinet6/in6_pcb.h>
    150  1.48    itojun #include <netinet6/ip6_var.h>
    151  1.48    itojun #endif
    152  1.48    itojun 
    153   1.4   mycroft #include <netinet/tcp.h>
    154   1.1       cgd #define	TCPOUTFLAGS
    155   1.4   mycroft #include <netinet/tcp_fsm.h>
    156   1.4   mycroft #include <netinet/tcp_seq.h>
    157   1.4   mycroft #include <netinet/tcp_timer.h>
    158   1.4   mycroft #include <netinet/tcp_var.h>
    159   1.4   mycroft #include <netinet/tcpip.h>
    160   1.4   mycroft #include <netinet/tcp_debug.h>
    161  1.14  christos 
    162   1.1       cgd #ifdef notyet
    163   1.1       cgd extern struct mbuf *m_copypack();
    164   1.1       cgd #endif
    165   1.1       cgd 
    166   1.9   mycroft #define MAX_TCPOPTLEN	32	/* max # bytes that go in options */
    167   1.1       cgd 
    168  1.33   thorpej /*
    169  1.36   thorpej  * Knob to enable Congestion Window Monitoring, and control the
    170  1.36   thorpej  * the burst size it allows.  Default burst is 4 packets, per
    171  1.36   thorpej  * the Internet draft.
    172  1.33   thorpej  */
    173  1.72   thorpej int	tcp_cwm = 1;
    174  1.36   thorpej int	tcp_cwm_burstsize = 4;
    175  1.33   thorpej 
    176  1.69   thorpej static
    177  1.69   thorpej #ifndef GPROF
    178  1.69   thorpej __inline
    179  1.69   thorpej #endif
    180  1.69   thorpej void
    181  1.68   thorpej tcp_segsize(struct tcpcb *tp, int *txsegsizep, int *rxsegsizep)
    182  1.17   thorpej {
    183  1.60    itojun #ifdef INET
    184  1.17   thorpej 	struct inpcb *inp = tp->t_inpcb;
    185  1.60    itojun #endif
    186  1.48    itojun #ifdef INET6
    187  1.48    itojun 	struct in6pcb *in6p = tp->t_in6pcb;
    188  1.48    itojun #endif
    189  1.17   thorpej 	struct rtentry *rt;
    190  1.17   thorpej 	struct ifnet *ifp;
    191  1.17   thorpej 	int size;
    192  1.48    itojun 	int iphlen;
    193  1.48    itojun 
    194  1.60    itojun #ifdef DIAGNOSTIC
    195  1.60    itojun 	if (tp->t_inpcb && tp->t_in6pcb)
    196  1.60    itojun 		panic("tcp_segsize: both t_inpcb and t_in6pcb are set");
    197  1.60    itojun #endif
    198  1.48    itojun 	switch (tp->t_family) {
    199  1.60    itojun #ifdef INET
    200  1.48    itojun 	case AF_INET:
    201  1.48    itojun 		iphlen = sizeof(struct ip);
    202  1.48    itojun 		break;
    203  1.60    itojun #endif
    204  1.48    itojun #ifdef INET6
    205  1.48    itojun 	case AF_INET6:
    206  1.48    itojun 		iphlen = sizeof(struct ip6_hdr);
    207  1.48    itojun 		break;
    208  1.48    itojun #endif
    209  1.48    itojun 	default:
    210  1.48    itojun 		size = tcp_mssdflt;
    211  1.48    itojun 		goto out;
    212  1.48    itojun 	}
    213  1.17   thorpej 
    214  1.60    itojun 	rt = NULL;
    215  1.60    itojun #ifdef INET
    216  1.48    itojun 	if (inp)
    217  1.48    itojun 		rt = in_pcbrtentry(inp);
    218  1.60    itojun #endif
    219  1.61    itojun #ifdef INET6
    220  1.60    itojun 	if (in6p)
    221  1.48    itojun 		rt = in6_pcbrtentry(in6p);
    222  1.48    itojun #endif
    223  1.48    itojun 	if (rt == NULL) {
    224  1.17   thorpej 		size = tcp_mssdflt;
    225  1.17   thorpej 		goto out;
    226  1.17   thorpej 	}
    227  1.17   thorpej 
    228  1.17   thorpej 	ifp = rt->rt_ifp;
    229  1.17   thorpej 
    230  1.48    itojun 	size = tcp_mssdflt;
    231  1.19       kml 	if (rt->rt_rmx.rmx_mtu != 0)
    232  1.48    itojun 		size = rt->rt_rmx.rmx_mtu - iphlen - sizeof(struct tcphdr);
    233  1.60    itojun 	else if (ifp->if_flags & IFF_LOOPBACK)
    234  1.60    itojun 		size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    235  1.60    itojun #ifdef INET
    236  1.65    itojun 	else if (inp && ip_mtudisc)
    237  1.48    itojun 		size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    238  1.48    itojun 	else if (inp && in_localaddr(inp->inp_faddr))
    239  1.48    itojun 		size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    240  1.60    itojun #endif
    241  1.48    itojun #ifdef INET6
    242  1.48    itojun 	else if (in6p) {
    243  1.60    itojun #ifdef INET
    244  1.48    itojun 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
    245  1.48    itojun 			/* mapped addr case */
    246  1.48    itojun 			struct in_addr d;
    247  1.48    itojun 			bcopy(&in6p->in6p_faddr.s6_addr32[3], &d, sizeof(d));
    248  1.65    itojun 			if (ip_mtudisc || in_localaddr(d))
    249  1.48    itojun 				size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    250  1.60    itojun 		} else
    251  1.60    itojun #endif
    252  1.60    itojun 		{
    253  1.65    itojun 			/*
    254  1.65    itojun 			 * for IPv6, path MTU discovery is always turned on,
    255  1.65    itojun 			 * or the node must use packet size <= 1280.
    256  1.65    itojun 			 */
    257  1.65    itojun 			size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    258  1.48    itojun 		}
    259  1.48    itojun 	}
    260  1.48    itojun #endif
    261  1.48    itojun 	size -= tcp_optlen(tp);
    262  1.48    itojun 	/*
    263  1.48    itojun 	 * XXX tp->t_ourmss should have the right size, but without this code
    264  1.48    itojun 	 * fragmentation will occur... need more investigation
    265  1.48    itojun 	 */
    266  1.60    itojun #ifdef INET
    267  1.48    itojun 	if (inp) {
    268  1.48    itojun #ifdef IPSEC
    269  1.52    itojun 		size -= ipsec4_hdrsiz_tcp(tp);
    270  1.48    itojun #endif
    271  1.48    itojun 		size -= ip_optlen(inp);
    272  1.48    itojun 	}
    273  1.60    itojun #endif
    274  1.48    itojun #ifdef INET6
    275  1.60    itojun #ifdef INET
    276  1.60    itojun 	if (in6p && tp->t_family == AF_INET) {
    277  1.48    itojun #ifdef IPSEC
    278  1.52    itojun 		size -= ipsec4_hdrsiz_tcp(tp);
    279  1.48    itojun #endif
    280  1.48    itojun 		/* XXX size -= ip_optlen(in6p); */
    281  1.60    itojun 	} else
    282  1.60    itojun #endif
    283  1.60    itojun 	if (in6p && tp->t_family == AF_INET6) {
    284  1.61    itojun #ifdef IPSEC
    285  1.52    itojun 		size -= ipsec6_hdrsiz_tcp(tp);
    286  1.48    itojun #endif
    287  1.48    itojun 		size -= ip6_optlen(in6p);
    288  1.48    itojun 	}
    289  1.48    itojun #endif
    290  1.17   thorpej 
    291  1.17   thorpej  out:
    292  1.52    itojun 	/*
    293  1.52    itojun 	 * *rxsegsizep holds *estimated* inbound segment size (estimation
    294  1.52    itojun 	 * assumes that path MTU is the same for both ways).  this is only
    295  1.52    itojun 	 * for silly window avoidance, do not use the value for other purposes.
    296  1.52    itojun 	 *
    297  1.52    itojun 	 * ipseclen is subtracted from both sides, this may not be right.
    298  1.52    itojun 	 * I'm not quite sure about this (could someone comment).
    299  1.52    itojun 	 */
    300  1.21       kml 	*txsegsizep = min(tp->t_peermss, size);
    301  1.18   thorpej 	*rxsegsizep = min(tp->t_ourmss, size);
    302  1.21       kml 
    303  1.21       kml 	if (*txsegsizep != tp->t_segsz) {
    304  1.35       kml 		/*
    305  1.35       kml 		 * If the new segment size is larger, we don't want to
    306  1.35       kml 		 * mess up the congestion window, but if it is smaller
    307  1.35       kml 		 * we'll have to reduce the congestion window to ensure
    308  1.35       kml 		 * that we don't get into trouble with initial windows
    309  1.35       kml 		 * and the rest.  In any case, if the segment size
    310  1.35       kml 		 * has changed, chances are the path has, too, and
    311  1.35       kml 		 * our congestion window will be different.
    312  1.29       kml 		 */
    313  1.35       kml 		if (*txsegsizep < tp->t_segsz) {
    314  1.35       kml 			tp->snd_cwnd = max((tp->snd_cwnd / tp->t_segsz)
    315  1.35       kml 					   * *txsegsizep, *txsegsizep);
    316  1.35       kml 			tp->snd_ssthresh = max((tp->snd_ssthresh / tp->t_segsz)
    317  1.35       kml 						* *txsegsizep, *txsegsizep);
    318  1.35       kml 		}
    319  1.21       kml 		tp->t_segsz = *txsegsizep;
    320  1.21       kml 	}
    321  1.17   thorpej }
    322  1.17   thorpej 
    323  1.70   thorpej static
    324  1.70   thorpej #ifndef GPROF
    325  1.70   thorpej __inline
    326  1.70   thorpej #endif
    327  1.70   thorpej int
    328  1.70   thorpej tcp_build_datapkt(struct tcpcb *tp, struct socket *so, int off,
    329  1.70   thorpej     long len, int hdrlen, struct mbuf **mp)
    330  1.70   thorpej {
    331  1.70   thorpej 	struct mbuf *m;
    332  1.70   thorpej 
    333  1.70   thorpej 	if (tp->t_force && len == 1)
    334  1.70   thorpej 		tcpstat.tcps_sndprobe++;
    335  1.70   thorpej 	else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
    336  1.70   thorpej 		tcpstat.tcps_sndrexmitpack++;
    337  1.70   thorpej 		tcpstat.tcps_sndrexmitbyte += len;
    338  1.70   thorpej 	} else {
    339  1.70   thorpej 		tcpstat.tcps_sndpack++;
    340  1.70   thorpej 		tcpstat.tcps_sndbyte += len;
    341  1.70   thorpej 	}
    342  1.70   thorpej #ifdef notyet
    343  1.70   thorpej 	if ((m = m_copypack(so->so_snd.sb_mb, off,
    344  1.70   thorpej 	    (int)len, max_linkhdr + hdrlen)) == 0)
    345  1.70   thorpej 		return (ENOBUFS);
    346  1.70   thorpej 	/*
    347  1.70   thorpej 	 * m_copypack left space for our hdr; use it.
    348  1.70   thorpej 	 */
    349  1.70   thorpej 	m->m_len += hdrlen;
    350  1.70   thorpej 	m->m_data -= hdrlen;
    351  1.70   thorpej #else
    352  1.70   thorpej 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
    353  1.70   thorpej 	if (m != NULL &&
    354  1.70   thorpej 	    (max_linkhdr + hdrlen > MHLEN ||
    355  1.70   thorpej 	     max_linkhdr + hdrlen + len <= MCLBYTES)) {
    356  1.70   thorpej 		MCLGET(m, M_DONTWAIT);
    357  1.70   thorpej 		if ((m->m_flags & M_EXT) == 0) {
    358  1.70   thorpej 			m_freem(m);
    359  1.70   thorpej 			m = NULL;
    360  1.70   thorpej 		}
    361  1.70   thorpej 	}
    362  1.70   thorpej 	if (m == NULL)
    363  1.70   thorpej 		return (ENOBUFS);
    364  1.70   thorpej 	m->m_data += max_linkhdr;
    365  1.70   thorpej 	m->m_len = hdrlen;
    366  1.70   thorpej 	if (len <= M_TRAILINGSPACE(m)) {
    367  1.70   thorpej 		m_copydata(so->so_snd.sb_mb, off, (int) len,
    368  1.70   thorpej 		    mtod(m, caddr_t) + hdrlen);
    369  1.70   thorpej 		m->m_len += len;
    370  1.70   thorpej 	} else {
    371  1.70   thorpej 		m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
    372  1.70   thorpej 		if (m->m_next == NULL) {
    373  1.70   thorpej 			m_freem(m);
    374  1.70   thorpej 			return (ENOBUFS);
    375  1.70   thorpej 		}
    376  1.70   thorpej 	}
    377  1.70   thorpej #endif
    378  1.70   thorpej 
    379  1.70   thorpej 	*mp = m;
    380  1.70   thorpej 	return (0);
    381  1.70   thorpej }
    382  1.70   thorpej 
    383   1.1       cgd /*
    384   1.1       cgd  * Tcp output routine: figure out what should be sent and send it.
    385   1.1       cgd  */
    386   1.6   mycroft int
    387   1.1       cgd tcp_output(tp)
    388  1.56  augustss 	struct tcpcb *tp;
    389   1.1       cgd {
    390  1.48    itojun 	struct socket *so;
    391  1.48    itojun 	struct route *ro;
    392  1.47   thorpej 	long len, win;
    393   1.1       cgd 	int off, flags, error;
    394  1.56  augustss 	struct mbuf *m;
    395  1.48    itojun 	struct ip *ip;
    396  1.48    itojun #ifdef INET6
    397  1.48    itojun 	struct ip6_hdr *ip6;
    398  1.48    itojun #endif
    399  1.56  augustss 	struct tcphdr *th;
    400   1.9   mycroft 	u_char opt[MAX_TCPOPTLEN];
    401   1.1       cgd 	unsigned optlen, hdrlen;
    402  1.18   thorpej 	int idle, sendalot, txsegsize, rxsegsize;
    403  1.44      matt 	int maxburst = TCP_MAXBURST;
    404  1.48    itojun 	int af;		/* address family on the wire */
    405  1.48    itojun 	int iphdrlen;
    406  1.48    itojun 
    407  1.60    itojun #ifdef DIAGNOSTIC
    408  1.60    itojun 	if (tp->t_inpcb && tp->t_in6pcb)
    409  1.60    itojun 		panic("tcp_output: both t_inpcb and t_in6pcb are set");
    410  1.60    itojun #endif
    411  1.48    itojun 	so = NULL;
    412  1.48    itojun 	ro = NULL;
    413  1.48    itojun 	if (tp->t_inpcb) {
    414  1.48    itojun 		so = tp->t_inpcb->inp_socket;
    415  1.48    itojun 		ro = &tp->t_inpcb->inp_route;
    416  1.48    itojun 	}
    417  1.48    itojun #ifdef INET6
    418  1.48    itojun 	else if (tp->t_in6pcb) {
    419  1.48    itojun 		so = tp->t_in6pcb->in6p_socket;
    420  1.48    itojun 		ro = (struct route *)&tp->t_in6pcb->in6p_route;
    421  1.48    itojun 	}
    422  1.48    itojun #endif
    423  1.48    itojun 
    424  1.48    itojun 	switch (af = tp->t_family) {
    425  1.60    itojun #ifdef INET
    426  1.48    itojun 	case AF_INET:
    427  1.48    itojun 		if (tp->t_inpcb)
    428  1.48    itojun 			break;
    429  1.48    itojun #ifdef INET6
    430  1.48    itojun 		/* mapped addr case */
    431  1.48    itojun 		if (tp->t_in6pcb)
    432  1.48    itojun 			break;
    433  1.48    itojun #endif
    434  1.48    itojun 		return EINVAL;
    435  1.60    itojun #endif
    436  1.48    itojun #ifdef INET6
    437  1.48    itojun 	case AF_INET6:
    438  1.48    itojun 		if (tp->t_in6pcb)
    439  1.48    itojun 			break;
    440  1.48    itojun 		return EINVAL;
    441  1.48    itojun #endif
    442  1.48    itojun 	default:
    443  1.48    itojun 		return EAFNOSUPPORT;
    444  1.48    itojun 	}
    445  1.17   thorpej 
    446  1.18   thorpej 	tcp_segsize(tp, &txsegsize, &rxsegsize);
    447   1.1       cgd 
    448   1.1       cgd 	idle = (tp->snd_max == tp->snd_una);
    449  1.33   thorpej 
    450  1.41   thorpej 	/*
    451  1.41   thorpej 	 * Restart Window computation.  From draft-floyd-incr-init-win-03:
    452  1.41   thorpej 	 *
    453  1.41   thorpej 	 *	Optionally, a TCP MAY set the restart window to the
    454  1.41   thorpej 	 *	minimum of the value used for the initial window and
    455  1.41   thorpej 	 *	the current value of cwnd (in other words, using a
    456  1.41   thorpej 	 *	larger value for the restart window should never increase
    457  1.41   thorpej 	 *	the size of cwnd).
    458  1.41   thorpej 	 */
    459  1.33   thorpej 	if (tcp_cwm) {
    460   1.1       cgd 		/*
    461  1.33   thorpej 		 * Hughes/Touch/Heidemann Congestion Window Monitoring.
    462  1.33   thorpej 		 * Count the number of packets currently pending
    463  1.33   thorpej 		 * acknowledgement, and limit our congestion window
    464  1.37   thorpej 		 * to a pre-determined allowed burst size plus that count.
    465  1.33   thorpej 		 * This prevents bursting once all pending packets have
    466  1.33   thorpej 		 * been acknowledged (i.e. transmission is idle).
    467  1.42   thorpej 		 *
    468  1.42   thorpej 		 * XXX Link this to Initial Window?
    469   1.1       cgd 		 */
    470  1.33   thorpej 		tp->snd_cwnd = min(tp->snd_cwnd,
    471  1.36   thorpej 		    (tcp_cwm_burstsize * txsegsize) +
    472  1.33   thorpej 		    (tp->snd_nxt - tp->snd_una));
    473  1.33   thorpej 	} else {
    474  1.73   thorpej 		if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur) {
    475  1.33   thorpej 			/*
    476  1.33   thorpej 			 * We have been idle for "a while" and no acks are
    477  1.33   thorpej 			 * expected to clock out any data we send --
    478  1.33   thorpej 			 * slow start to get ack "clock" running again.
    479  1.33   thorpej 			 */
    480  1.41   thorpej 			tp->snd_cwnd = min(tp->snd_cwnd,
    481  1.41   thorpej 			    TCP_INITIAL_WINDOW(tcp_init_win, txsegsize));
    482  1.33   thorpej 		}
    483  1.33   thorpej 	}
    484  1.33   thorpej 
    485   1.1       cgd again:
    486  1.33   thorpej 	/*
    487  1.33   thorpej 	 * Determine length of data that should be transmitted, and
    488  1.33   thorpej 	 * flags that should be used.  If there is some data or critical
    489  1.33   thorpej 	 * controls (SYN, RST) to send, then transmit; otherwise,
    490  1.33   thorpej 	 * investigate further.
    491  1.33   thorpej 	 */
    492   1.1       cgd 	sendalot = 0;
    493   1.1       cgd 	off = tp->snd_nxt - tp->snd_una;
    494   1.1       cgd 	win = min(tp->snd_wnd, tp->snd_cwnd);
    495   1.1       cgd 
    496   1.9   mycroft 	flags = tcp_outflags[tp->t_state];
    497   1.1       cgd 	/*
    498   1.1       cgd 	 * If in persist timeout with window of 0, send 1 byte.
    499   1.1       cgd 	 * Otherwise, if window is small but nonzero
    500   1.1       cgd 	 * and timer expired, we will send what we can
    501   1.1       cgd 	 * and go to transmit state.
    502   1.1       cgd 	 */
    503   1.1       cgd 	if (tp->t_force) {
    504   1.9   mycroft 		if (win == 0) {
    505   1.9   mycroft 			/*
    506   1.9   mycroft 			 * If we still have some data to send, then
    507   1.9   mycroft 			 * clear the FIN bit.  Usually this would
    508   1.9   mycroft 			 * happen below when it realizes that we
    509   1.9   mycroft 			 * aren't sending all the data.  However,
    510   1.9   mycroft 			 * if we have exactly 1 byte of unset data,
    511   1.9   mycroft 			 * then it won't clear the FIN bit below,
    512   1.9   mycroft 			 * and if we are in persist state, we wind
    513   1.9   mycroft 			 * up sending the packet without recording
    514   1.9   mycroft 			 * that we sent the FIN bit.
    515   1.9   mycroft 			 *
    516   1.9   mycroft 			 * We can't just blindly clear the FIN bit,
    517   1.9   mycroft 			 * because if we don't have any more data
    518   1.9   mycroft 			 * to send then the probe will be the FIN
    519   1.9   mycroft 			 * itself.
    520   1.9   mycroft 			 */
    521   1.9   mycroft 			if (off < so->so_snd.sb_cc)
    522   1.9   mycroft 				flags &= ~TH_FIN;
    523   1.1       cgd 			win = 1;
    524   1.9   mycroft 		} else {
    525  1.38   thorpej 			TCP_TIMER_DISARM(tp, TCPT_PERSIST);
    526   1.1       cgd 			tp->t_rxtshift = 0;
    527   1.1       cgd 		}
    528   1.1       cgd 	}
    529   1.1       cgd 
    530  1.11   mycroft 	if (win < so->so_snd.sb_cc) {
    531  1.11   mycroft 		len = win - off;
    532  1.11   mycroft 		flags &= ~TH_FIN;
    533  1.11   mycroft 	} else
    534  1.11   mycroft 		len = so->so_snd.sb_cc - off;
    535   1.1       cgd 
    536   1.1       cgd 	if (len < 0) {
    537   1.1       cgd 		/*
    538   1.1       cgd 		 * If FIN has been sent but not acked,
    539   1.1       cgd 		 * but we haven't been called to retransmit,
    540   1.1       cgd 		 * len will be -1.  Otherwise, window shrank
    541   1.1       cgd 		 * after we sent into it.  If window shrank to 0,
    542  1.25   thorpej 		 * cancel pending retransmit, pull snd_nxt back
    543  1.25   thorpej 		 * to (closed) window, and set the persist timer
    544  1.25   thorpej 		 * if it isn't already going.  If the window didn't
    545  1.25   thorpej 		 * close completely, just wait for an ACK.
    546  1.43   mycroft 		 *
    547  1.43   mycroft 		 * If we have a pending FIN, either it has already been
    548  1.43   mycroft 		 * transmitted or it is outside the window, so drop it.
    549  1.43   mycroft 		 * If the FIN has been transmitted, but this is not a
    550  1.43   mycroft 		 * retransmission, then len must be -1.  Therefore we also
    551  1.43   mycroft 		 * prevent here the sending of `gratuitous FINs'.  This
    552  1.43   mycroft 		 * eliminates the need to check for that case below (e.g.
    553  1.43   mycroft 		 * to back up snd_nxt before the FIN so that the sequence
    554  1.43   mycroft 		 * number is correct).
    555   1.1       cgd 		 */
    556   1.1       cgd 		len = 0;
    557  1.43   mycroft 		flags &= ~TH_FIN;
    558   1.1       cgd 		if (win == 0) {
    559  1.38   thorpej 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
    560  1.25   thorpej 			tp->t_rxtshift = 0;
    561   1.1       cgd 			tp->snd_nxt = tp->snd_una;
    562  1.38   thorpej 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
    563  1.25   thorpej 				tcp_setpersist(tp);
    564   1.1       cgd 		}
    565   1.1       cgd 	}
    566  1.18   thorpej 	if (len > txsegsize) {
    567  1.18   thorpej 		len = txsegsize;
    568  1.11   mycroft 		flags &= ~TH_FIN;
    569   1.1       cgd 		sendalot = 1;
    570   1.1       cgd 	}
    571   1.1       cgd 
    572   1.1       cgd 	win = sbspace(&so->so_rcv);
    573   1.1       cgd 
    574   1.1       cgd 	/*
    575   1.1       cgd 	 * Sender silly window avoidance.  If connection is idle
    576   1.1       cgd 	 * and can send all data, a maximum segment,
    577   1.1       cgd 	 * at least a maximum default-size segment do it,
    578   1.1       cgd 	 * or are forced, do it; otherwise don't bother.
    579   1.1       cgd 	 * If peer's buffer is tiny, then send
    580   1.1       cgd 	 * when window is at least half open.
    581   1.1       cgd 	 * If retransmitting (possibly after persist timer forced us
    582   1.1       cgd 	 * to send into a small window), then must resend.
    583   1.1       cgd 	 */
    584   1.1       cgd 	if (len) {
    585  1.18   thorpej 		if (len == txsegsize)
    586   1.1       cgd 			goto send;
    587  1.46   thorpej 		if ((so->so_state & SS_MORETOCOME) == 0 &&
    588  1.46   thorpej 		    ((idle || tp->t_flags & TF_NODELAY) &&
    589  1.46   thorpej 		     len + off >= so->so_snd.sb_cc))
    590   1.1       cgd 			goto send;
    591   1.1       cgd 		if (tp->t_force)
    592   1.1       cgd 			goto send;
    593   1.1       cgd 		if (len >= tp->max_sndwnd / 2)
    594   1.1       cgd 			goto send;
    595   1.1       cgd 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
    596   1.1       cgd 			goto send;
    597   1.1       cgd 	}
    598   1.1       cgd 
    599   1.1       cgd 	/*
    600  1.18   thorpej 	 * Compare available window to amount of window known to peer
    601  1.18   thorpej 	 * (as advertised window less next expected input).  If the
    602  1.18   thorpej 	 * difference is at least twice the size of the largest segment
    603  1.18   thorpej 	 * we expect to receive (i.e. two segments) or at least 50% of
    604  1.18   thorpej 	 * the maximum possible window, then want to send a window update
    605  1.18   thorpej 	 * to peer.
    606   1.1       cgd 	 */
    607   1.1       cgd 	if (win > 0) {
    608   1.9   mycroft 		/*
    609   1.9   mycroft 		 * "adv" is the amount we can increase the window,
    610   1.9   mycroft 		 * taking into account that we are limited by
    611   1.9   mycroft 		 * TCP_MAXWIN << tp->rcv_scale.
    612   1.9   mycroft 		 */
    613   1.9   mycroft 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
    614   1.9   mycroft 			(tp->rcv_adv - tp->rcv_nxt);
    615   1.1       cgd 
    616  1.18   thorpej 		if (adv >= (long) (2 * rxsegsize))
    617   1.1       cgd 			goto send;
    618   1.1       cgd 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
    619   1.1       cgd 			goto send;
    620   1.1       cgd 	}
    621   1.1       cgd 
    622   1.1       cgd 	/*
    623   1.1       cgd 	 * Send if we owe peer an ACK.
    624   1.1       cgd 	 */
    625   1.1       cgd 	if (tp->t_flags & TF_ACKNOW)
    626   1.1       cgd 		goto send;
    627  1.43   mycroft 	if (flags & (TH_SYN|TH_FIN|TH_RST))
    628   1.1       cgd 		goto send;
    629   1.1       cgd 	if (SEQ_GT(tp->snd_up, tp->snd_una))
    630   1.1       cgd 		goto send;
    631   1.1       cgd 
    632   1.1       cgd 	/*
    633   1.1       cgd 	 * TCP window updates are not reliable, rather a polling protocol
    634   1.1       cgd 	 * using ``persist'' packets is used to insure receipt of window
    635   1.1       cgd 	 * updates.  The three ``states'' for the output side are:
    636   1.1       cgd 	 *	idle			not doing retransmits or persists
    637   1.1       cgd 	 *	persisting		to move a small or zero window
    638   1.1       cgd 	 *	(re)transmitting	and thereby not persisting
    639   1.1       cgd 	 *
    640   1.1       cgd 	 * tp->t_timer[TCPT_PERSIST]
    641   1.1       cgd 	 *	is set when we are in persist state.
    642   1.1       cgd 	 * tp->t_force
    643   1.1       cgd 	 *	is set when we are called to send a persist packet.
    644   1.1       cgd 	 * tp->t_timer[TCPT_REXMT]
    645   1.1       cgd 	 *	is set when we are retransmitting
    646   1.1       cgd 	 * The output side is idle when both timers are zero.
    647   1.1       cgd 	 *
    648   1.1       cgd 	 * If send window is too small, there is data to transmit, and no
    649   1.1       cgd 	 * retransmit or persist is pending, then go to persist state.
    650   1.1       cgd 	 * If nothing happens soon, send when timer expires:
    651   1.1       cgd 	 * if window is nonzero, transmit what we can,
    652   1.1       cgd 	 * otherwise force out a byte.
    653   1.1       cgd 	 */
    654  1.38   thorpej 	if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
    655  1.38   thorpej 	    TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
    656   1.1       cgd 		tp->t_rxtshift = 0;
    657   1.1       cgd 		tcp_setpersist(tp);
    658   1.1       cgd 	}
    659   1.1       cgd 
    660   1.1       cgd 	/*
    661   1.1       cgd 	 * No reason to send a segment, just return.
    662   1.1       cgd 	 */
    663   1.1       cgd 	return (0);
    664   1.1       cgd 
    665   1.1       cgd send:
    666   1.1       cgd 	/*
    667   1.1       cgd 	 * Before ESTABLISHED, force sending of initial options
    668   1.1       cgd 	 * unless TCP set not to do any options.
    669   1.1       cgd 	 * NOTE: we assume that the IP/TCP header plus TCP options
    670   1.1       cgd 	 * always fit in a single mbuf, leaving room for a maximum
    671   1.1       cgd 	 * link header, i.e.
    672  1.48    itojun 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
    673   1.1       cgd 	 */
    674   1.1       cgd 	optlen = 0;
    675  1.48    itojun 	switch (af) {
    676  1.60    itojun #ifdef INET
    677  1.48    itojun 	case AF_INET:
    678  1.48    itojun 		iphdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
    679  1.48    itojun 		break;
    680  1.60    itojun #endif
    681  1.48    itojun #ifdef INET6
    682  1.48    itojun 	case AF_INET6:
    683  1.48    itojun 		iphdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
    684  1.48    itojun 		break;
    685  1.48    itojun #endif
    686  1.49    itojun 	default:	/*pacify gcc*/
    687  1.49    itojun 		iphdrlen = 0;
    688  1.49    itojun 		break;
    689  1.48    itojun 	}
    690  1.48    itojun 	hdrlen = iphdrlen;
    691   1.9   mycroft 	if (flags & TH_SYN) {
    692  1.48    itojun 		struct rtentry *rt;
    693  1.48    itojun 
    694  1.60    itojun 		rt = NULL;
    695  1.60    itojun #ifdef INET
    696  1.48    itojun 		if (tp->t_inpcb)
    697  1.48    itojun 			rt = in_pcbrtentry(tp->t_inpcb);
    698  1.60    itojun #endif
    699  1.61    itojun #ifdef INET6
    700  1.60    itojun 		if (tp->t_in6pcb)
    701  1.48    itojun 			rt = in6_pcbrtentry(tp->t_in6pcb);
    702  1.48    itojun #endif
    703  1.34       kml 
    704   1.9   mycroft 		tp->snd_nxt = tp->iss;
    705  1.34       kml 		tp->t_ourmss = tcp_mss_to_advertise(rt != NULL ?
    706  1.52    itojun 						    rt->rt_ifp : NULL, af);
    707   1.9   mycroft 		if ((tp->t_flags & TF_NOOPT) == 0) {
    708   1.9   mycroft 			opt[0] = TCPOPT_MAXSEG;
    709   1.9   mycroft 			opt[1] = 4;
    710  1.17   thorpej 			opt[2] = (tp->t_ourmss >> 8) & 0xff;
    711  1.17   thorpej 			opt[3] = tp->t_ourmss & 0xff;
    712   1.9   mycroft 			optlen = 4;
    713   1.9   mycroft 
    714   1.9   mycroft 			if ((tp->t_flags & TF_REQ_SCALE) &&
    715   1.9   mycroft 			    ((flags & TH_ACK) == 0 ||
    716   1.9   mycroft 			    (tp->t_flags & TF_RCVD_SCALE))) {
    717  1.12       cgd 				*((u_int32_t *) (opt + optlen)) = htonl(
    718   1.9   mycroft 					TCPOPT_NOP << 24 |
    719   1.9   mycroft 					TCPOPT_WINDOW << 16 |
    720   1.9   mycroft 					TCPOLEN_WINDOW << 8 |
    721   1.9   mycroft 					tp->request_r_scale);
    722   1.9   mycroft 				optlen += 4;
    723   1.9   mycroft 			}
    724   1.9   mycroft 		}
    725   1.9   mycroft  	}
    726   1.9   mycroft 
    727   1.9   mycroft  	/*
    728   1.9   mycroft 	 * Send a timestamp and echo-reply if this is a SYN and our side
    729   1.9   mycroft 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
    730   1.9   mycroft 	 * and our peer have sent timestamps in our SYN's.
    731   1.9   mycroft  	 */
    732   1.9   mycroft  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
    733   1.9   mycroft  	     (flags & TH_RST) == 0 &&
    734   1.9   mycroft  	    ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
    735   1.9   mycroft 	     (tp->t_flags & TF_RCVD_TSTMP))) {
    736  1.13       cgd 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
    737   1.9   mycroft 
    738   1.9   mycroft  		/* Form timestamp option as shown in appendix A of RFC 1323. */
    739   1.9   mycroft  		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
    740  1.64   thorpej  		*lp++ = htonl(TCP_TIMESTAMP(tp));
    741   1.9   mycroft  		*lp   = htonl(tp->ts_recent);
    742   1.9   mycroft  		optlen += TCPOLEN_TSTAMP_APPA;
    743   1.9   mycroft  	}
    744   1.9   mycroft 
    745   1.9   mycroft  	hdrlen += optlen;
    746   1.9   mycroft 
    747   1.1       cgd #ifdef DIAGNOSTIC
    748  1.29       kml 	if (len > txsegsize)
    749  1.29       kml 		panic("tcp data to be sent is larger than segment");
    750  1.48    itojun  	if (max_linkhdr + hdrlen > MCLBYTES)
    751   1.9   mycroft 		panic("tcphdr too big");
    752   1.1       cgd #endif
    753   1.1       cgd 
    754   1.1       cgd 	/*
    755   1.1       cgd 	 * Grab a header mbuf, attaching a copy of data to
    756   1.1       cgd 	 * be transmitted, and initialize the header from
    757   1.1       cgd 	 * the template for sends on this connection.
    758   1.1       cgd 	 */
    759   1.1       cgd 	if (len) {
    760  1.70   thorpej 		error = tcp_build_datapkt(tp, so, off, len, hdrlen, &m);
    761  1.70   thorpej 		if (error)
    762   1.1       cgd 			goto out;
    763   1.1       cgd 		/*
    764   1.1       cgd 		 * If we're sending everything we've got, set PUSH.
    765   1.1       cgd 		 * (This will keep happy those implementations which only
    766   1.1       cgd 		 * give data to the user when a buffer fills or
    767   1.1       cgd 		 * a PUSH comes in.)
    768   1.1       cgd 		 */
    769   1.1       cgd 		if (off + len == so->so_snd.sb_cc)
    770   1.1       cgd 			flags |= TH_PUSH;
    771   1.1       cgd 	} else {
    772   1.1       cgd 		if (tp->t_flags & TF_ACKNOW)
    773   1.1       cgd 			tcpstat.tcps_sndacks++;
    774   1.1       cgd 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
    775   1.1       cgd 			tcpstat.tcps_sndctrl++;
    776   1.1       cgd 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
    777   1.1       cgd 			tcpstat.tcps_sndurg++;
    778   1.1       cgd 		else
    779   1.1       cgd 			tcpstat.tcps_sndwinup++;
    780   1.1       cgd 
    781   1.1       cgd 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    782  1.54    itojun 		if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
    783  1.48    itojun 			MCLGET(m, M_DONTWAIT);
    784  1.48    itojun 			if ((m->m_flags & M_EXT) == 0) {
    785  1.48    itojun 				m_freem(m);
    786  1.48    itojun 				m = NULL;
    787  1.48    itojun 			}
    788  1.48    itojun 		}
    789   1.1       cgd 		if (m == NULL) {
    790   1.1       cgd 			error = ENOBUFS;
    791   1.1       cgd 			goto out;
    792   1.1       cgd 		}
    793   1.1       cgd 		m->m_data += max_linkhdr;
    794   1.1       cgd 		m->m_len = hdrlen;
    795   1.1       cgd 	}
    796   1.1       cgd 	m->m_pkthdr.rcvif = (struct ifnet *)0;
    797  1.48    itojun 	switch (af) {
    798  1.60    itojun #ifdef INET
    799  1.48    itojun 	case AF_INET:
    800  1.48    itojun 		ip = mtod(m, struct ip *);
    801  1.48    itojun #ifdef INET6
    802  1.48    itojun 		ip6 = NULL;
    803  1.48    itojun #endif
    804  1.48    itojun 		th = (struct tcphdr *)(ip + 1);
    805  1.48    itojun 		break;
    806  1.60    itojun #endif
    807  1.48    itojun #ifdef INET6
    808  1.48    itojun 	case AF_INET6:
    809  1.48    itojun 		ip = NULL;
    810  1.48    itojun 		ip6 = mtod(m, struct ip6_hdr *);
    811  1.48    itojun 		th = (struct tcphdr *)(ip6 + 1);
    812  1.48    itojun 		break;
    813  1.48    itojun #endif
    814  1.49    itojun 	default:	/*pacify gcc*/
    815  1.49    itojun 		ip = NULL;
    816  1.50      fvdl #ifdef INET6
    817  1.49    itojun 		ip6 = NULL;
    818  1.50      fvdl #endif
    819  1.49    itojun 		th = NULL;
    820  1.49    itojun 		break;
    821  1.48    itojun 	}
    822   1.1       cgd 	if (tp->t_template == 0)
    823   1.1       cgd 		panic("tcp_output");
    824  1.48    itojun 	if (tp->t_template->m_len < iphdrlen)
    825  1.48    itojun 		panic("tcp_output");
    826  1.48    itojun 	bcopy(mtod(tp->t_template, caddr_t), mtod(m, caddr_t), iphdrlen);
    827   1.1       cgd 
    828   1.1       cgd 	/*
    829   1.9   mycroft 	 * If we are doing retransmissions, then snd_nxt will
    830   1.9   mycroft 	 * not reflect the first unsent octet.  For ACK only
    831   1.9   mycroft 	 * packets, we do not want the sequence number of the
    832   1.9   mycroft 	 * retransmitted packet, we want the sequence number
    833   1.9   mycroft 	 * of the next unsent octet.  So, if there is no data
    834   1.9   mycroft 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
    835   1.9   mycroft 	 * when filling in ti_seq.  But if we are in persist
    836   1.9   mycroft 	 * state, snd_max might reflect one byte beyond the
    837   1.9   mycroft 	 * right edge of the window, so use snd_nxt in that
    838   1.9   mycroft 	 * case, since we know we aren't doing a retransmission.
    839   1.9   mycroft 	 * (retransmit and persist are mutually exclusive...)
    840   1.9   mycroft 	 */
    841  1.38   thorpej 	if (len || (flags & (TH_SYN|TH_FIN)) ||
    842  1.38   thorpej 	    TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
    843  1.48    itojun 		th->th_seq = htonl(tp->snd_nxt);
    844   1.8   mycroft 	else
    845  1.48    itojun 		th->th_seq = htonl(tp->snd_max);
    846  1.48    itojun 	th->th_ack = htonl(tp->rcv_nxt);
    847   1.1       cgd 	if (optlen) {
    848  1.48    itojun 		bcopy((caddr_t)opt, (caddr_t)(th + 1), optlen);
    849  1.48    itojun 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
    850   1.1       cgd 	}
    851  1.48    itojun 	th->th_flags = flags;
    852   1.1       cgd 	/*
    853   1.1       cgd 	 * Calculate receive window.  Don't shrink window,
    854   1.1       cgd 	 * but avoid silly window syndrome.
    855   1.1       cgd 	 */
    856  1.18   thorpej 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
    857   1.1       cgd 		win = 0;
    858   1.9   mycroft 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
    859   1.9   mycroft 		win = (long)TCP_MAXWIN << tp->rcv_scale;
    860   1.1       cgd 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
    861   1.1       cgd 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
    862  1.48    itojun 	th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
    863   1.1       cgd 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
    864  1.16       kml 		u_int32_t urp = tp->snd_up - tp->snd_nxt;
    865  1.16       kml 		if (urp > IP_MAXPACKET)
    866  1.16       kml 			urp = IP_MAXPACKET;
    867  1.48    itojun 		th->th_urp = htons((u_int16_t)urp);
    868  1.48    itojun 		th->th_flags |= TH_URG;
    869   1.1       cgd 	} else
    870   1.1       cgd 		/*
    871   1.1       cgd 		 * If no urgent pointer to send, then we pull
    872   1.1       cgd 		 * the urgent pointer to the left edge of the send window
    873   1.1       cgd 		 * so that it doesn't drift into the send window on sequence
    874   1.1       cgd 		 * number wraparound.
    875   1.1       cgd 		 */
    876   1.1       cgd 		tp->snd_up = tp->snd_una;		/* drag it along */
    877   1.1       cgd 
    878   1.1       cgd 	/*
    879  1.66   thorpej 	 * Set ourselves up to be checksummed just before the packet
    880  1.66   thorpej 	 * hits the wire.
    881   1.1       cgd 	 */
    882  1.48    itojun 	switch (af) {
    883  1.60    itojun #ifdef INET
    884  1.48    itojun 	case AF_INET:
    885  1.66   thorpej 		m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
    886  1.66   thorpej 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
    887  1.66   thorpej 		if (len + optlen) {
    888  1.66   thorpej 			/* Fixup the pseudo-header checksum. */
    889  1.66   thorpej 			/* XXXJRT Not IP Jumbogram safe. */
    890  1.66   thorpej 			th->th_sum = in_cksum_addword(th->th_sum,
    891  1.66   thorpej 			    htons((u_int16_t) (len + optlen)));
    892  1.66   thorpej 		}
    893  1.48    itojun 		break;
    894  1.60    itojun #endif
    895  1.48    itojun #ifdef INET6
    896  1.48    itojun 	case AF_INET6:
    897  1.66   thorpej 		/*
    898  1.66   thorpej 		 * XXX Actually delaying the checksum is Hard
    899  1.66   thorpej 		 * XXX (well, maybe not for Itojun, but it is
    900  1.66   thorpej 		 * XXX for me), but we can still take advantage
    901  1.66   thorpej 		 * XXX of the cached pseudo-header checksum.
    902  1.66   thorpej 		 */
    903  1.48    itojun 		/* equals to hdrlen + len */
    904  1.48    itojun 		m->m_pkthdr.len = sizeof(struct ip6_hdr)
    905  1.48    itojun 			+ sizeof(struct tcphdr) + optlen + len;
    906  1.66   thorpej #ifdef notyet
    907  1.66   thorpej 		m->m_pkthdr.csum_flags = M_CSUM_TCPv6;
    908  1.66   thorpej 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
    909  1.66   thorpej #endif
    910  1.66   thorpej 		if (len + optlen) {
    911  1.66   thorpej 			/* Fixup the pseudo-header checksum. */
    912  1.66   thorpej 			/* XXXJRT: Not IPv6 Jumbogram safe. */
    913  1.66   thorpej 			th->th_sum = in_cksum_addword(th->th_sum,
    914  1.66   thorpej 			    htons((u_int16_t) (len + optlen)));
    915  1.66   thorpej 		}
    916  1.66   thorpej #ifndef notyet
    917  1.66   thorpej 		th->th_sum = in6_cksum(m, 0, sizeof(struct ip6_hdr),
    918  1.66   thorpej 		    sizeof(struct tcphdr) + optlen + len);
    919  1.66   thorpej #endif
    920  1.48    itojun 		break;
    921  1.48    itojun #endif
    922  1.48    itojun 	}
    923   1.1       cgd 
    924   1.1       cgd 	/*
    925   1.1       cgd 	 * In transmit state, time the transmission and arrange for
    926   1.1       cgd 	 * the retransmit.  In persist state, just set snd_max.
    927   1.1       cgd 	 */
    928  1.38   thorpej 	if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
    929   1.1       cgd 		tcp_seq startseq = tp->snd_nxt;
    930   1.1       cgd 
    931   1.1       cgd 		/*
    932   1.1       cgd 		 * Advance snd_nxt over sequence space of this segment.
    933  1.43   mycroft 		 * There are no states in which we send both a SYN and a FIN,
    934  1.43   mycroft 		 * so we collapse the tests for these flags.
    935   1.1       cgd 		 */
    936  1.43   mycroft 		if (flags & (TH_SYN|TH_FIN))
    937  1.43   mycroft 			tp->snd_nxt++;
    938   1.1       cgd 		tp->snd_nxt += len;
    939   1.1       cgd 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
    940   1.1       cgd 			tp->snd_max = tp->snd_nxt;
    941   1.1       cgd 			/*
    942   1.1       cgd 			 * Time this transmission if not a retransmission and
    943   1.1       cgd 			 * not currently timing anything.
    944   1.1       cgd 			 */
    945  1.73   thorpej 			if (tp->t_rtttime == 0) {
    946  1.73   thorpej 				tp->t_rtttime = tcp_now;
    947   1.1       cgd 				tp->t_rtseq = startseq;
    948   1.1       cgd 				tcpstat.tcps_segstimed++;
    949   1.1       cgd 			}
    950   1.1       cgd 		}
    951   1.1       cgd 
    952   1.1       cgd 		/*
    953   1.1       cgd 		 * Set retransmit timer if not currently set,
    954   1.1       cgd 		 * and not doing an ack or a keep-alive probe.
    955   1.1       cgd 		 * Initial value for retransmit timer is smoothed
    956   1.1       cgd 		 * round-trip time + 2 * round-trip time variance.
    957   1.1       cgd 		 * Initialize shift counter which is used for backoff
    958   1.1       cgd 		 * of retransmit time.
    959   1.1       cgd 		 */
    960  1.38   thorpej 		if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
    961   1.1       cgd 		    tp->snd_nxt != tp->snd_una) {
    962  1.38   thorpej 			TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
    963  1.38   thorpej 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
    964  1.38   thorpej 				TCP_TIMER_DISARM(tp, TCPT_PERSIST);
    965   1.1       cgd 				tp->t_rxtshift = 0;
    966   1.1       cgd 			}
    967   1.1       cgd 		}
    968   1.1       cgd 	} else
    969   1.1       cgd 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
    970   1.1       cgd 			tp->snd_max = tp->snd_nxt + len;
    971   1.1       cgd 
    972  1.67       abs #ifdef TCP_DEBUG
    973   1.1       cgd 	/*
    974   1.1       cgd 	 * Trace.
    975   1.1       cgd 	 */
    976  1.48    itojun 	if (so->so_options & SO_DEBUG) {
    977  1.48    itojun 		/*
    978  1.48    itojun 		 * need to recover version # field, which was overwritten
    979  1.48    itojun 		 * on ip_cksum computation.
    980  1.48    itojun 		 */
    981  1.48    itojun 		struct ip *sip;
    982  1.48    itojun 		sip = mtod(m, struct ip *);
    983  1.48    itojun 		switch (af) {
    984  1.60    itojun #ifdef INET
    985  1.48    itojun 		case AF_INET:
    986  1.48    itojun 			sip->ip_v = 4;
    987  1.48    itojun 			break;
    988  1.60    itojun #endif
    989  1.48    itojun #ifdef INET6
    990  1.48    itojun 		case AF_INET6:
    991  1.48    itojun 			sip->ip_v = 6;
    992  1.48    itojun 			break;
    993  1.48    itojun #endif
    994  1.48    itojun 		}
    995  1.48    itojun 		tcp_trace(TA_OUTPUT, tp->t_state, tp, m, 0);
    996  1.48    itojun 	}
    997  1.67       abs #endif
    998   1.1       cgd 
    999   1.1       cgd 	/*
   1000   1.1       cgd 	 * Fill in IP length and desired time to live and
   1001   1.1       cgd 	 * send to IP level.  There should be a better way
   1002   1.1       cgd 	 * to handle ttl and tos; we could keep them in
   1003   1.1       cgd 	 * the template, but need a way to checksum without them.
   1004   1.1       cgd 	 */
   1005   1.1       cgd 	m->m_pkthdr.len = hdrlen + len;
   1006  1.19       kml 
   1007  1.48    itojun 	switch (af) {
   1008  1.60    itojun #ifdef INET
   1009  1.48    itojun 	case AF_INET:
   1010  1.48    itojun 		ip->ip_len = m->m_pkthdr.len;
   1011  1.48    itojun 		if (tp->t_inpcb) {
   1012  1.48    itojun 			ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
   1013  1.48    itojun 			ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos;
   1014  1.48    itojun 		}
   1015  1.48    itojun #ifdef INET6
   1016  1.48    itojun 		else if (tp->t_in6pcb) {
   1017  1.62    itojun 			ip->ip_ttl = in6_selecthlim(tp->t_in6pcb, NULL); /*XXX*/
   1018  1.48    itojun 			ip->ip_tos = 0;	/*XXX*/
   1019  1.48    itojun 		}
   1020  1.48    itojun #endif
   1021  1.48    itojun 		break;
   1022  1.60    itojun #endif
   1023  1.48    itojun #ifdef INET6
   1024  1.48    itojun 	case AF_INET6:
   1025  1.48    itojun 		ip6->ip6_nxt = IPPROTO_TCP;
   1026  1.53    itojun 		if (tp->t_in6pcb) {
   1027  1.53    itojun 			/*
   1028  1.53    itojun 			 * we separately set hoplimit for every segment, since
   1029  1.53    itojun 			 * the user might want to change the value via
   1030  1.53    itojun 			 * setsockopt. Also, desired default hop limit might
   1031  1.53    itojun 			 * be changed via Neighbor Discovery.
   1032  1.53    itojun 			 */
   1033  1.53    itojun 			ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb,
   1034  1.53    itojun 				ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
   1035  1.53    itojun 		}
   1036  1.48    itojun 		/* ip6->ip6_flow = ??? */
   1037  1.48    itojun 		/* ip6_plen will be filled in ip6_output(). */
   1038  1.48    itojun 		break;
   1039  1.48    itojun #endif
   1040  1.48    itojun 	}
   1041  1.19       kml 
   1042  1.48    itojun #ifdef IPSEC
   1043  1.63    itojun 	if (ipsec_setsocket(m, so) != 0) {
   1044  1.63    itojun 		m_freem(m);
   1045  1.63    itojun 		error = ENOBUFS;
   1046  1.63    itojun 		goto out;
   1047  1.63    itojun 	}
   1048  1.48    itojun #endif /*IPSEC*/
   1049  1.48    itojun 
   1050  1.48    itojun 	switch (af) {
   1051  1.60    itojun #ifdef INET
   1052  1.48    itojun 	case AF_INET:
   1053  1.48    itojun 	    {
   1054  1.48    itojun 		struct mbuf *opts;
   1055  1.48    itojun 
   1056  1.48    itojun 		if (tp->t_inpcb)
   1057  1.48    itojun 			opts = tp->t_inpcb->inp_options;
   1058  1.48    itojun 		else
   1059  1.48    itojun 			opts = NULL;
   1060  1.48    itojun 		error = ip_output(m, opts, ro,
   1061  1.59   thorpej 			(ip_mtudisc ? IP_MTUDISC : 0) |
   1062  1.59   thorpej 			(so->so_options & SO_DONTROUTE),
   1063  1.59   thorpej 			0);
   1064  1.48    itojun 		break;
   1065  1.48    itojun 	    }
   1066  1.60    itojun #endif
   1067  1.48    itojun #ifdef INET6
   1068  1.48    itojun 	case AF_INET6:
   1069  1.48    itojun 	    {
   1070  1.48    itojun 		struct ip6_pktopts *opts;
   1071  1.48    itojun 
   1072  1.48    itojun 		if (tp->t_in6pcb)
   1073  1.48    itojun 			opts = tp->t_in6pcb->in6p_outputopts;
   1074  1.48    itojun 		else
   1075  1.48    itojun 			opts = NULL;
   1076  1.48    itojun 		error = ip6_output(m, opts, (struct route_in6 *)ro,
   1077  1.53    itojun 			so->so_options & SO_DONTROUTE, 0, NULL);
   1078  1.48    itojun 		break;
   1079  1.48    itojun 	    }
   1080  1.48    itojun #endif
   1081  1.49    itojun 	default:
   1082  1.49    itojun 		error = EAFNOSUPPORT;
   1083  1.49    itojun 		break;
   1084  1.48    itojun 	}
   1085   1.1       cgd 	if (error) {
   1086   1.1       cgd out:
   1087   1.1       cgd 		if (error == ENOBUFS) {
   1088  1.69   thorpej 			tcpstat.tcps_selfquench++;
   1089  1.60    itojun #ifdef INET
   1090  1.48    itojun 			if (tp->t_inpcb)
   1091  1.48    itojun 				tcp_quench(tp->t_inpcb, 0);
   1092  1.60    itojun #endif
   1093  1.58    itojun #ifdef INET6
   1094  1.60    itojun 			if (tp->t_in6pcb)
   1095  1.48    itojun 				tcp6_quench(tp->t_in6pcb, 0);
   1096  1.48    itojun #endif
   1097  1.71   thorpej 			error = 0;
   1098  1.71   thorpej 		} else if ((error == EHOSTUNREACH || error == ENETDOWN) &&
   1099  1.71   thorpej 		    TCPS_HAVERCVDSYN(tp->t_state)) {
   1100   1.1       cgd 			tp->t_softerror = error;
   1101  1.71   thorpej 			error = 0;
   1102   1.1       cgd 		}
   1103  1.71   thorpej 
   1104  1.71   thorpej 		/* Restart the delayed ACK timer, if necessary. */
   1105  1.71   thorpej 		if (tp->t_flags & TF_DELACK)
   1106  1.71   thorpej 			TCP_RESTART_DELACK(tp);
   1107  1.71   thorpej 
   1108   1.1       cgd 		return (error);
   1109   1.1       cgd 	}
   1110   1.1       cgd 	tcpstat.tcps_sndtotal++;
   1111  1.23   thorpej 	if (tp->t_flags & TF_DELACK)
   1112  1.23   thorpej 		tcpstat.tcps_delack++;
   1113   1.1       cgd 
   1114   1.1       cgd 	/*
   1115   1.1       cgd 	 * Data sent (as far as we can tell).
   1116   1.1       cgd 	 * If this advertises a larger window than any other segment,
   1117   1.1       cgd 	 * then remember the size of the advertised window.
   1118   1.1       cgd 	 * Any pending ACK has now been sent.
   1119   1.1       cgd 	 */
   1120   1.1       cgd 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
   1121   1.1       cgd 		tp->rcv_adv = tp->rcv_nxt + win;
   1122   1.9   mycroft 	tp->last_ack_sent = tp->rcv_nxt;
   1123  1.26   thorpej 	tp->t_flags &= ~TF_ACKNOW;
   1124  1.26   thorpej 	TCP_CLEAR_DELACK(tp);
   1125  1.44      matt #ifdef DIAGNOSTIC
   1126  1.44      matt 	if (maxburst < 0)
   1127  1.44      matt 		printf("tcp_output: maxburst exceeded by %d\n", -maxburst);
   1128  1.44      matt #endif
   1129  1.45      matt 	if (sendalot && (!tcp_do_newreno || --maxburst))
   1130   1.1       cgd 		goto again;
   1131   1.1       cgd 	return (0);
   1132   1.1       cgd }
   1133   1.1       cgd 
   1134   1.6   mycroft void
   1135   1.1       cgd tcp_setpersist(tp)
   1136  1.56  augustss 	struct tcpcb *tp;
   1137   1.1       cgd {
   1138  1.56  augustss 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
   1139  1.38   thorpej 	int nticks;
   1140   1.1       cgd 
   1141  1.38   thorpej 	if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
   1142   1.1       cgd 		panic("tcp_output REXMT");
   1143   1.1       cgd 	/*
   1144   1.1       cgd 	 * Start/restart persistance timer.
   1145   1.1       cgd 	 */
   1146  1.30       kml 	if (t < tp->t_rttmin)
   1147  1.30       kml 		t = tp->t_rttmin;
   1148  1.38   thorpej 	TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
   1149   1.1       cgd 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
   1150  1.38   thorpej 	TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
   1151   1.1       cgd 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
   1152   1.1       cgd 		tp->t_rxtshift++;
   1153   1.1       cgd }
   1154