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