Home | History | Annotate | Line # | Download | only in netinet
tcp_output.c revision 1.79.4.3
      1 /*	$NetBSD: tcp_output.c,v 1.79.4.3 2002/11/30 14:31:59 he 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.3 2002/11/30 14:31:59 he 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 void
    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 (rt->rt_rmx.rmx_mtu != 0)
    280 		size = rt->rt_rmx.rmx_mtu - iphlen - sizeof(struct tcphdr);
    281 	else if (ifp->if_flags & IFF_LOOPBACK)
    282 		size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    283 #ifdef INET
    284 	else if (inp && ip_mtudisc)
    285 		size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    286 	else if (inp && in_localaddr(inp->inp_faddr))
    287 		size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    288 #endif
    289 #ifdef INET6
    290 	else if (in6p) {
    291 #ifdef INET
    292 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
    293 			/* mapped addr case */
    294 			struct in_addr d;
    295 			bcopy(&in6p->in6p_faddr.s6_addr32[3], &d, sizeof(d));
    296 			if (ip_mtudisc || in_localaddr(d))
    297 				size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    298 		} else
    299 #endif
    300 		{
    301 			/*
    302 			 * for IPv6, path MTU discovery is always turned on,
    303 			 * or the node must use packet size <= 1280.
    304 			 */
    305 			size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    306 		}
    307 	}
    308 #endif
    309  out:
    310 	/*
    311 	 * Now we must make room for whatever extra TCP/IP options are in
    312 	 * the packet.
    313 	 */
    314 	optlen = tcp_optlen(tp);
    315 
    316 	/*
    317 	 * XXX tp->t_ourmss should have the right size, but without this code
    318 	 * fragmentation will occur... need more investigation
    319 	 */
    320 #ifdef INET
    321 	if (inp) {
    322 #ifdef IPSEC
    323 		optlen += ipsec4_hdrsiz_tcp(tp);
    324 #endif
    325 		optlen += ip_optlen(inp);
    326 	}
    327 #endif
    328 #ifdef INET6
    329 #ifdef INET
    330 	if (in6p && tp->t_family == AF_INET) {
    331 #ifdef IPSEC
    332 		optlen += ipsec4_hdrsiz_tcp(tp);
    333 #endif
    334 		/* XXX size -= ip_optlen(in6p); */
    335 	} else
    336 #endif
    337 	if (in6p && tp->t_family == AF_INET6) {
    338 #ifdef IPSEC
    339 		optlen += ipsec6_hdrsiz_tcp(tp);
    340 #endif
    341 		optlen += ip6_optlen(in6p);
    342 	}
    343 #endif
    344 	size -= optlen;
    345 
    346 	/*
    347 	 * *rxsegsizep holds *estimated* inbound segment size (estimation
    348 	 * assumes that path MTU is the same for both ways).  this is only
    349 	 * for silly window avoidance, do not use the value for other purposes.
    350 	 *
    351 	 * ipseclen is subtracted from both sides, this may not be right.
    352 	 * I'm not quite sure about this (could someone comment).
    353 	 */
    354 	*txsegsizep = min(tp->t_peermss - optlen, size);
    355 	/*
    356 	 * Never send more than half a buffer full.  This insures that we can
    357 	 * always keep 2 packets on the wire, no matter what SO_SNDBUF is, and
    358 	 * therefore acks will never be delayed unless we run out of data to
    359 	 * transmit.
    360 	 */
    361 	if (so)
    362 		*txsegsizep = min(so->so_snd.sb_hiwat >> 1, *txsegsizep);
    363 	*rxsegsizep = min(tp->t_ourmss - optlen, size);
    364 
    365 	if (*txsegsizep != tp->t_segsz) {
    366 		/*
    367 		 * If the new segment size is larger, we don't want to
    368 		 * mess up the congestion window, but if it is smaller
    369 		 * we'll have to reduce the congestion window to ensure
    370 		 * that we don't get into trouble with initial windows
    371 		 * and the rest.  In any case, if the segment size
    372 		 * has changed, chances are the path has, too, and
    373 		 * our congestion window will be different.
    374 		 */
    375 		if (*txsegsizep < tp->t_segsz) {
    376 			tp->snd_cwnd = max((tp->snd_cwnd / tp->t_segsz)
    377 					   * *txsegsizep, *txsegsizep);
    378 			tp->snd_ssthresh = max((tp->snd_ssthresh / tp->t_segsz)
    379 						* *txsegsizep, *txsegsizep);
    380 		}
    381 		tp->t_segsz = *txsegsizep;
    382 	}
    383 }
    384 
    385 static
    386 #ifndef GPROF
    387 __inline
    388 #endif
    389 int
    390 tcp_build_datapkt(struct tcpcb *tp, struct socket *so, int off,
    391     long len, int hdrlen, struct mbuf **mp)
    392 {
    393 	struct mbuf *m;
    394 
    395 	if (tp->t_force && len == 1)
    396 		tcpstat.tcps_sndprobe++;
    397 	else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
    398 		tcpstat.tcps_sndrexmitpack++;
    399 		tcpstat.tcps_sndrexmitbyte += len;
    400 	} else {
    401 		tcpstat.tcps_sndpack++;
    402 		tcpstat.tcps_sndbyte += len;
    403 	}
    404 #ifdef notyet
    405 	if ((m = m_copypack(so->so_snd.sb_mb, off,
    406 	    (int)len, max_linkhdr + hdrlen)) == 0)
    407 		return (ENOBUFS);
    408 	/*
    409 	 * m_copypack left space for our hdr; use it.
    410 	 */
    411 	m->m_len += hdrlen;
    412 	m->m_data -= hdrlen;
    413 #else
    414 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
    415 	if (__predict_false(m == NULL))
    416 		return (ENOBUFS);
    417 
    418 	/*
    419 	 * XXX Because other code assumes headers will fit in
    420 	 * XXX one header mbuf.
    421 	 *
    422 	 * (This code should almost *never* be run.)
    423 	 */
    424 	if (__predict_false((max_linkhdr + hdrlen) > MHLEN)) {
    425 		TCP_OUTPUT_COUNTER_INCR(&tcp_output_bigheader);
    426 		MCLGET(m, M_DONTWAIT);
    427 		if ((m->m_flags & M_EXT) == 0) {
    428 			m_freem(m);
    429 			return (ENOBUFS);
    430 		}
    431 	}
    432 
    433 	m->m_data += max_linkhdr;
    434 	m->m_len = hdrlen;
    435 	if (len <= M_TRAILINGSPACE(m)) {
    436 		m_copydata(so->so_snd.sb_mb, off, (int) len,
    437 		    mtod(m, caddr_t) + hdrlen);
    438 		m->m_len += len;
    439 		TCP_OUTPUT_COUNTER_INCR(&tcp_output_copysmall);
    440 	} else {
    441 		m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
    442 		if (m->m_next == NULL) {
    443 			m_freem(m);
    444 			return (ENOBUFS);
    445 		}
    446 #ifdef TCP_OUTPUT_COUNTERS
    447 		if (m->m_next->m_flags & M_EXT)
    448 			TCP_OUTPUT_COUNTER_INCR(&tcp_output_refbig);
    449 		else
    450 			TCP_OUTPUT_COUNTER_INCR(&tcp_output_copybig);
    451 #endif /* TCP_OUTPUT_COUNTERS */
    452 	}
    453 #endif
    454 
    455 	*mp = m;
    456 	return (0);
    457 }
    458 
    459 /*
    460  * Tcp output routine: figure out what should be sent and send it.
    461  */
    462 int
    463 tcp_output(tp)
    464 	struct tcpcb *tp;
    465 {
    466 	struct socket *so;
    467 	struct route *ro;
    468 	long len, win;
    469 	int off, flags, error;
    470 	struct mbuf *m;
    471 	struct ip *ip;
    472 #ifdef INET6
    473 	struct ip6_hdr *ip6;
    474 #endif
    475 	struct tcphdr *th;
    476 	u_char opt[MAX_TCPOPTLEN];
    477 	unsigned optlen, hdrlen;
    478 	int idle, sendalot, txsegsize, rxsegsize;
    479 	int maxburst = TCP_MAXBURST;
    480 	int af;		/* address family on the wire */
    481 	int iphdrlen;
    482 
    483 #ifdef DIAGNOSTIC
    484 	if (tp->t_inpcb && tp->t_in6pcb)
    485 		panic("tcp_output: both t_inpcb and t_in6pcb are set");
    486 #endif
    487 	so = NULL;
    488 	ro = NULL;
    489 	if (tp->t_inpcb) {
    490 		so = tp->t_inpcb->inp_socket;
    491 		ro = &tp->t_inpcb->inp_route;
    492 	}
    493 #ifdef INET6
    494 	else if (tp->t_in6pcb) {
    495 		so = tp->t_in6pcb->in6p_socket;
    496 		ro = (struct route *)&tp->t_in6pcb->in6p_route;
    497 	}
    498 #endif
    499 
    500 	switch (af = tp->t_family) {
    501 #ifdef INET
    502 	case AF_INET:
    503 		if (tp->t_inpcb)
    504 			break;
    505 #ifdef INET6
    506 		/* mapped addr case */
    507 		if (tp->t_in6pcb)
    508 			break;
    509 #endif
    510 		return EINVAL;
    511 #endif
    512 #ifdef INET6
    513 	case AF_INET6:
    514 		if (tp->t_in6pcb)
    515 			break;
    516 		return EINVAL;
    517 #endif
    518 	default:
    519 		return EAFNOSUPPORT;
    520 	}
    521 
    522 	tcp_segsize(tp, &txsegsize, &rxsegsize);
    523 
    524 	idle = (tp->snd_max == tp->snd_una);
    525 
    526 	/*
    527 	 * Restart Window computation.  From draft-floyd-incr-init-win-03:
    528 	 *
    529 	 *	Optionally, a TCP MAY set the restart window to the
    530 	 *	minimum of the value used for the initial window and
    531 	 *	the current value of cwnd (in other words, using a
    532 	 *	larger value for the restart window should never increase
    533 	 *	the size of cwnd).
    534 	 */
    535 	if (tcp_cwm) {
    536 		/*
    537 		 * Hughes/Touch/Heidemann Congestion Window Monitoring.
    538 		 * Count the number of packets currently pending
    539 		 * acknowledgement, and limit our congestion window
    540 		 * to a pre-determined allowed burst size plus that count.
    541 		 * This prevents bursting once all pending packets have
    542 		 * been acknowledged (i.e. transmission is idle).
    543 		 *
    544 		 * XXX Link this to Initial Window?
    545 		 */
    546 		tp->snd_cwnd = min(tp->snd_cwnd,
    547 		    (tcp_cwm_burstsize * txsegsize) +
    548 		    (tp->snd_nxt - tp->snd_una));
    549 	} else {
    550 		if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur) {
    551 			/*
    552 			 * We have been idle for "a while" and no acks are
    553 			 * expected to clock out any data we send --
    554 			 * slow start to get ack "clock" running again.
    555 			 */
    556 			tp->snd_cwnd = min(tp->snd_cwnd,
    557 			    TCP_INITIAL_WINDOW(tcp_init_win, txsegsize));
    558 		}
    559 	}
    560 
    561 again:
    562 	/*
    563 	 * Determine length of data that should be transmitted, and
    564 	 * flags that should be used.  If there is some data or critical
    565 	 * controls (SYN, RST) to send, then transmit; otherwise,
    566 	 * investigate further.
    567 	 */
    568 	sendalot = 0;
    569 	off = tp->snd_nxt - tp->snd_una;
    570 	win = min(tp->snd_wnd, tp->snd_cwnd);
    571 
    572 	flags = tcp_outflags[tp->t_state];
    573 	/*
    574 	 * If in persist timeout with window of 0, send 1 byte.
    575 	 * Otherwise, if window is small but nonzero
    576 	 * and timer expired, we will send what we can
    577 	 * and go to transmit state.
    578 	 */
    579 	if (tp->t_force) {
    580 		if (win == 0) {
    581 			/*
    582 			 * If we still have some data to send, then
    583 			 * clear the FIN bit.  Usually this would
    584 			 * happen below when it realizes that we
    585 			 * aren't sending all the data.  However,
    586 			 * if we have exactly 1 byte of unset data,
    587 			 * then it won't clear the FIN bit below,
    588 			 * and if we are in persist state, we wind
    589 			 * up sending the packet without recording
    590 			 * that we sent the FIN bit.
    591 			 *
    592 			 * We can't just blindly clear the FIN bit,
    593 			 * because if we don't have any more data
    594 			 * to send then the probe will be the FIN
    595 			 * itself.
    596 			 */
    597 			if (off < so->so_snd.sb_cc)
    598 				flags &= ~TH_FIN;
    599 			win = 1;
    600 		} else {
    601 			TCP_TIMER_DISARM(tp, TCPT_PERSIST);
    602 			tp->t_rxtshift = 0;
    603 		}
    604 	}
    605 
    606 	if (win < so->so_snd.sb_cc) {
    607 		len = win - off;
    608 		flags &= ~TH_FIN;
    609 	} else
    610 		len = so->so_snd.sb_cc - off;
    611 
    612 	if (len < 0) {
    613 		/*
    614 		 * If FIN has been sent but not acked,
    615 		 * but we haven't been called to retransmit,
    616 		 * len will be -1.  Otherwise, window shrank
    617 		 * after we sent into it.  If window shrank to 0,
    618 		 * cancel pending retransmit, pull snd_nxt back
    619 		 * to (closed) window, and set the persist timer
    620 		 * if it isn't already going.  If the window didn't
    621 		 * close completely, just wait for an ACK.
    622 		 *
    623 		 * If we have a pending FIN, either it has already been
    624 		 * transmitted or it is outside the window, so drop it.
    625 		 * If the FIN has been transmitted, but this is not a
    626 		 * retransmission, then len must be -1.  Therefore we also
    627 		 * prevent here the sending of `gratuitous FINs'.  This
    628 		 * eliminates the need to check for that case below (e.g.
    629 		 * to back up snd_nxt before the FIN so that the sequence
    630 		 * number is correct).
    631 		 */
    632 		len = 0;
    633 		flags &= ~TH_FIN;
    634 		if (win == 0) {
    635 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
    636 			tp->t_rxtshift = 0;
    637 			tp->snd_nxt = tp->snd_una;
    638 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
    639 				tcp_setpersist(tp);
    640 		}
    641 	}
    642 	if (len > txsegsize) {
    643 		len = txsegsize;
    644 		flags &= ~TH_FIN;
    645 		sendalot = 1;
    646 	}
    647 
    648 	win = sbspace(&so->so_rcv);
    649 
    650 	/*
    651 	 * Sender silly window avoidance.  If connection is idle
    652 	 * and can send all data, a maximum segment,
    653 	 * at least a maximum default-size segment do it,
    654 	 * or are forced, do it; otherwise don't bother.
    655 	 * If peer's buffer is tiny, then send
    656 	 * when window is at least half open.
    657 	 * If retransmitting (possibly after persist timer forced us
    658 	 * to send into a small window), then must resend.
    659 	 */
    660 	if (len) {
    661 		if (len == txsegsize)
    662 			goto send;
    663 		if ((so->so_state & SS_MORETOCOME) == 0 &&
    664 		    ((idle || tp->t_flags & TF_NODELAY) &&
    665 		     len + off >= so->so_snd.sb_cc))
    666 			goto send;
    667 		if (tp->t_force)
    668 			goto send;
    669 		if (len >= tp->max_sndwnd / 2)
    670 			goto send;
    671 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
    672 			goto send;
    673 	}
    674 
    675 	/*
    676 	 * Compare available window to amount of window known to peer
    677 	 * (as advertised window less next expected input).  If the
    678 	 * difference is at least twice the size of the largest segment
    679 	 * we expect to receive (i.e. two segments) or at least 50% of
    680 	 * the maximum possible window, then want to send a window update
    681 	 * to peer.
    682 	 */
    683 	if (win > 0) {
    684 		/*
    685 		 * "adv" is the amount we can increase the window,
    686 		 * taking into account that we are limited by
    687 		 * TCP_MAXWIN << tp->rcv_scale.
    688 		 */
    689 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
    690 			(tp->rcv_adv - tp->rcv_nxt);
    691 
    692 		if (adv >= (long) (2 * rxsegsize))
    693 			goto send;
    694 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
    695 			goto send;
    696 	}
    697 
    698 	/*
    699 	 * Send if we owe peer an ACK.
    700 	 */
    701 	if (tp->t_flags & TF_ACKNOW)
    702 		goto send;
    703 	if (flags & (TH_SYN|TH_FIN|TH_RST))
    704 		goto send;
    705 	if (SEQ_GT(tp->snd_up, tp->snd_una))
    706 		goto send;
    707 
    708 	/*
    709 	 * TCP window updates are not reliable, rather a polling protocol
    710 	 * using ``persist'' packets is used to insure receipt of window
    711 	 * updates.  The three ``states'' for the output side are:
    712 	 *	idle			not doing retransmits or persists
    713 	 *	persisting		to move a small or zero window
    714 	 *	(re)transmitting	and thereby not persisting
    715 	 *
    716 	 * tp->t_timer[TCPT_PERSIST]
    717 	 *	is set when we are in persist state.
    718 	 * tp->t_force
    719 	 *	is set when we are called to send a persist packet.
    720 	 * tp->t_timer[TCPT_REXMT]
    721 	 *	is set when we are retransmitting
    722 	 * The output side is idle when both timers are zero.
    723 	 *
    724 	 * If send window is too small, there is data to transmit, and no
    725 	 * retransmit or persist is pending, then go to persist state.
    726 	 * If nothing happens soon, send when timer expires:
    727 	 * if window is nonzero, transmit what we can,
    728 	 * otherwise force out a byte.
    729 	 */
    730 	if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
    731 	    TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
    732 		tp->t_rxtshift = 0;
    733 		tcp_setpersist(tp);
    734 	}
    735 
    736 	/*
    737 	 * No reason to send a segment, just return.
    738 	 */
    739 	return (0);
    740 
    741 send:
    742 	/*
    743 	 * Before ESTABLISHED, force sending of initial options
    744 	 * unless TCP set not to do any options.
    745 	 * NOTE: we assume that the IP/TCP header plus TCP options
    746 	 * always fit in a single mbuf, leaving room for a maximum
    747 	 * link header, i.e.
    748 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
    749 	 */
    750 	optlen = 0;
    751 	switch (af) {
    752 #ifdef INET
    753 	case AF_INET:
    754 		iphdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
    755 		break;
    756 #endif
    757 #ifdef INET6
    758 	case AF_INET6:
    759 		iphdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
    760 		break;
    761 #endif
    762 	default:	/*pacify gcc*/
    763 		iphdrlen = 0;
    764 		break;
    765 	}
    766 	hdrlen = iphdrlen;
    767 	if (flags & TH_SYN) {
    768 		struct rtentry *rt;
    769 
    770 		rt = NULL;
    771 #ifdef INET
    772 		if (tp->t_inpcb)
    773 			rt = in_pcbrtentry(tp->t_inpcb);
    774 #endif
    775 #ifdef INET6
    776 		if (tp->t_in6pcb)
    777 			rt = in6_pcbrtentry(tp->t_in6pcb);
    778 #endif
    779 
    780 		tp->snd_nxt = tp->iss;
    781 		tp->t_ourmss = tcp_mss_to_advertise(rt != NULL ?
    782 						    rt->rt_ifp : NULL, af);
    783 		if ((tp->t_flags & TF_NOOPT) == 0) {
    784 			opt[0] = TCPOPT_MAXSEG;
    785 			opt[1] = 4;
    786 			opt[2] = (tp->t_ourmss >> 8) & 0xff;
    787 			opt[3] = tp->t_ourmss & 0xff;
    788 			optlen = 4;
    789 
    790 			if ((tp->t_flags & TF_REQ_SCALE) &&
    791 			    ((flags & TH_ACK) == 0 ||
    792 			    (tp->t_flags & TF_RCVD_SCALE))) {
    793 				*((u_int32_t *) (opt + optlen)) = htonl(
    794 					TCPOPT_NOP << 24 |
    795 					TCPOPT_WINDOW << 16 |
    796 					TCPOLEN_WINDOW << 8 |
    797 					tp->request_r_scale);
    798 				optlen += 4;
    799 			}
    800 		}
    801  	}
    802 
    803  	/*
    804 	 * Send a timestamp and echo-reply if this is a SYN and our side
    805 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
    806 	 * and our peer have sent timestamps in our SYN's.
    807  	 */
    808  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
    809  	     (flags & TH_RST) == 0 &&
    810  	    ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
    811 	     (tp->t_flags & TF_RCVD_TSTMP))) {
    812 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
    813 
    814  		/* Form timestamp option as shown in appendix A of RFC 1323. */
    815  		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
    816  		*lp++ = htonl(TCP_TIMESTAMP(tp));
    817  		*lp   = htonl(tp->ts_recent);
    818  		optlen += TCPOLEN_TSTAMP_APPA;
    819  	}
    820 
    821  	hdrlen += optlen;
    822 
    823 #ifdef DIAGNOSTIC
    824 	if (len > txsegsize)
    825 		panic("tcp data to be sent is larger than segment");
    826  	if (max_linkhdr + hdrlen > MCLBYTES)
    827 		panic("tcphdr too big");
    828 #endif
    829 
    830 	/*
    831 	 * Grab a header mbuf, attaching a copy of data to
    832 	 * be transmitted, and initialize the header from
    833 	 * the template for sends on this connection.
    834 	 */
    835 	if (len) {
    836 		error = tcp_build_datapkt(tp, so, off, len, hdrlen, &m);
    837 		if (error)
    838 			goto out;
    839 		/*
    840 		 * If we're sending everything we've got, set PUSH.
    841 		 * (This will keep happy those implementations which only
    842 		 * give data to the user when a buffer fills or
    843 		 * a PUSH comes in.)
    844 		 */
    845 		if (off + len == so->so_snd.sb_cc)
    846 			flags |= TH_PUSH;
    847 	} else {
    848 		if (tp->t_flags & TF_ACKNOW)
    849 			tcpstat.tcps_sndacks++;
    850 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
    851 			tcpstat.tcps_sndctrl++;
    852 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
    853 			tcpstat.tcps_sndurg++;
    854 		else
    855 			tcpstat.tcps_sndwinup++;
    856 
    857 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    858 		if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
    859 			MCLGET(m, M_DONTWAIT);
    860 			if ((m->m_flags & M_EXT) == 0) {
    861 				m_freem(m);
    862 				m = NULL;
    863 			}
    864 		}
    865 		if (m == NULL) {
    866 			error = ENOBUFS;
    867 			goto out;
    868 		}
    869 		m->m_data += max_linkhdr;
    870 		m->m_len = hdrlen;
    871 	}
    872 	m->m_pkthdr.rcvif = (struct ifnet *)0;
    873 	switch (af) {
    874 #ifdef INET
    875 	case AF_INET:
    876 		ip = mtod(m, struct ip *);
    877 #ifdef INET6
    878 		ip6 = NULL;
    879 #endif
    880 		th = (struct tcphdr *)(ip + 1);
    881 		break;
    882 #endif
    883 #ifdef INET6
    884 	case AF_INET6:
    885 		ip = NULL;
    886 		ip6 = mtod(m, struct ip6_hdr *);
    887 		th = (struct tcphdr *)(ip6 + 1);
    888 		break;
    889 #endif
    890 	default:	/*pacify gcc*/
    891 		ip = NULL;
    892 #ifdef INET6
    893 		ip6 = NULL;
    894 #endif
    895 		th = NULL;
    896 		break;
    897 	}
    898 	if (tp->t_template == 0)
    899 		panic("tcp_output");
    900 	if (tp->t_template->m_len < iphdrlen)
    901 		panic("tcp_output");
    902 	bcopy(mtod(tp->t_template, caddr_t), mtod(m, caddr_t), iphdrlen);
    903 
    904 	/*
    905 	 * If we are doing retransmissions, then snd_nxt will
    906 	 * not reflect the first unsent octet.  For ACK only
    907 	 * packets, we do not want the sequence number of the
    908 	 * retransmitted packet, we want the sequence number
    909 	 * of the next unsent octet.  So, if there is no data
    910 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
    911 	 * when filling in ti_seq.  But if we are in persist
    912 	 * state, snd_max might reflect one byte beyond the
    913 	 * right edge of the window, so use snd_nxt in that
    914 	 * case, since we know we aren't doing a retransmission.
    915 	 * (retransmit and persist are mutually exclusive...)
    916 	 */
    917 	if (len || (flags & (TH_SYN|TH_FIN)) ||
    918 	    TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
    919 		th->th_seq = htonl(tp->snd_nxt);
    920 	else
    921 		th->th_seq = htonl(tp->snd_max);
    922 	th->th_ack = htonl(tp->rcv_nxt);
    923 	if (optlen) {
    924 		bcopy((caddr_t)opt, (caddr_t)(th + 1), optlen);
    925 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
    926 	}
    927 	th->th_flags = flags;
    928 	/*
    929 	 * Calculate receive window.  Don't shrink window,
    930 	 * but avoid silly window syndrome.
    931 	 */
    932 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
    933 		win = 0;
    934 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
    935 		win = (long)TCP_MAXWIN << tp->rcv_scale;
    936 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
    937 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
    938 	th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
    939 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
    940 		u_int32_t urp = tp->snd_up - tp->snd_nxt;
    941 		if (urp > IP_MAXPACKET)
    942 			urp = IP_MAXPACKET;
    943 		th->th_urp = htons((u_int16_t)urp);
    944 		th->th_flags |= TH_URG;
    945 	} else
    946 		/*
    947 		 * If no urgent pointer to send, then we pull
    948 		 * the urgent pointer to the left edge of the send window
    949 		 * so that it doesn't drift into the send window on sequence
    950 		 * number wraparound.
    951 		 */
    952 		tp->snd_up = tp->snd_una;		/* drag it along */
    953 
    954 	/*
    955 	 * Set ourselves up to be checksummed just before the packet
    956 	 * hits the wire.
    957 	 */
    958 	switch (af) {
    959 #ifdef INET
    960 	case AF_INET:
    961 		m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
    962 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
    963 		if (len + optlen) {
    964 			/* Fixup the pseudo-header checksum. */
    965 			/* XXXJRT Not IP Jumbogram safe. */
    966 			th->th_sum = in_cksum_addword(th->th_sum,
    967 			    htons((u_int16_t) (len + optlen)));
    968 		}
    969 		break;
    970 #endif
    971 #ifdef INET6
    972 	case AF_INET6:
    973 		/*
    974 		 * XXX Actually delaying the checksum is Hard
    975 		 * XXX (well, maybe not for Itojun, but it is
    976 		 * XXX for me), but we can still take advantage
    977 		 * XXX of the cached pseudo-header checksum.
    978 		 */
    979 		/* equals to hdrlen + len */
    980 		m->m_pkthdr.len = sizeof(struct ip6_hdr)
    981 			+ sizeof(struct tcphdr) + optlen + len;
    982 #ifdef notyet
    983 		m->m_pkthdr.csum_flags = M_CSUM_TCPv6;
    984 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
    985 #endif
    986 		if (len + optlen) {
    987 			/* Fixup the pseudo-header checksum. */
    988 			/* XXXJRT: Not IPv6 Jumbogram safe. */
    989 			th->th_sum = in_cksum_addword(th->th_sum,
    990 			    htons((u_int16_t) (len + optlen)));
    991 		}
    992 #ifndef notyet
    993 		th->th_sum = in6_cksum(m, 0, sizeof(struct ip6_hdr),
    994 		    sizeof(struct tcphdr) + optlen + len);
    995 #endif
    996 		break;
    997 #endif
    998 	}
    999 
   1000 	/*
   1001 	 * In transmit state, time the transmission and arrange for
   1002 	 * the retransmit.  In persist state, just set snd_max.
   1003 	 */
   1004 	if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
   1005 		tcp_seq startseq = tp->snd_nxt;
   1006 
   1007 		/*
   1008 		 * Advance snd_nxt over sequence space of this segment.
   1009 		 * There are no states in which we send both a SYN and a FIN,
   1010 		 * so we collapse the tests for these flags.
   1011 		 */
   1012 		if (flags & (TH_SYN|TH_FIN))
   1013 			tp->snd_nxt++;
   1014 		tp->snd_nxt += len;
   1015 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
   1016 			tp->snd_max = tp->snd_nxt;
   1017 			/*
   1018 			 * Time this transmission if not a retransmission and
   1019 			 * not currently timing anything.
   1020 			 */
   1021 			if (tp->t_rtttime == 0) {
   1022 				tp->t_rtttime = tcp_now;
   1023 				tp->t_rtseq = startseq;
   1024 				tcpstat.tcps_segstimed++;
   1025 			}
   1026 		}
   1027 
   1028 		/*
   1029 		 * Set retransmit timer if not currently set,
   1030 		 * and not doing an ack or a keep-alive probe.
   1031 		 * Initial value for retransmit timer is smoothed
   1032 		 * round-trip time + 2 * round-trip time variance.
   1033 		 * Initialize shift counter which is used for backoff
   1034 		 * of retransmit time.
   1035 		 */
   1036 		if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
   1037 		    tp->snd_nxt != tp->snd_una) {
   1038 			TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
   1039 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
   1040 				TCP_TIMER_DISARM(tp, TCPT_PERSIST);
   1041 				tp->t_rxtshift = 0;
   1042 			}
   1043 		}
   1044 	} else
   1045 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
   1046 			tp->snd_max = tp->snd_nxt + len;
   1047 
   1048 #ifdef TCP_DEBUG
   1049 	/*
   1050 	 * Trace.
   1051 	 */
   1052 	if (so->so_options & SO_DEBUG) {
   1053 		/*
   1054 		 * need to recover version # field, which was overwritten
   1055 		 * on ip_cksum computation.
   1056 		 */
   1057 		struct ip *sip;
   1058 		sip = mtod(m, struct ip *);
   1059 		switch (af) {
   1060 #ifdef INET
   1061 		case AF_INET:
   1062 			sip->ip_v = 4;
   1063 			break;
   1064 #endif
   1065 #ifdef INET6
   1066 		case AF_INET6:
   1067 			sip->ip_v = 6;
   1068 			break;
   1069 #endif
   1070 		}
   1071 		tcp_trace(TA_OUTPUT, tp->t_state, tp, m, 0);
   1072 	}
   1073 #endif
   1074 
   1075 	/*
   1076 	 * Fill in IP length and desired time to live and
   1077 	 * send to IP level.  There should be a better way
   1078 	 * to handle ttl and tos; we could keep them in
   1079 	 * the template, but need a way to checksum without them.
   1080 	 */
   1081 	m->m_pkthdr.len = hdrlen + len;
   1082 
   1083 	switch (af) {
   1084 #ifdef INET
   1085 	case AF_INET:
   1086 		ip->ip_len = m->m_pkthdr.len;
   1087 		if (tp->t_inpcb) {
   1088 			ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
   1089 			ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos;
   1090 		}
   1091 #ifdef INET6
   1092 		else if (tp->t_in6pcb) {
   1093 			ip->ip_ttl = in6_selecthlim(tp->t_in6pcb, NULL); /*XXX*/
   1094 			ip->ip_tos = 0;	/*XXX*/
   1095 		}
   1096 #endif
   1097 		break;
   1098 #endif
   1099 #ifdef INET6
   1100 	case AF_INET6:
   1101 		ip6->ip6_nxt = IPPROTO_TCP;
   1102 		if (tp->t_in6pcb) {
   1103 			/*
   1104 			 * we separately set hoplimit for every segment, since
   1105 			 * the user might want to change the value via
   1106 			 * setsockopt. Also, desired default hop limit might
   1107 			 * be changed via Neighbor Discovery.
   1108 			 */
   1109 			ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb,
   1110 				ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
   1111 		}
   1112 		/* ip6->ip6_flow = ??? */
   1113 		/* ip6_plen will be filled in ip6_output(). */
   1114 		break;
   1115 #endif
   1116 	}
   1117 
   1118 #ifdef IPSEC
   1119 	if (ipsec_setsocket(m, so) != 0) {
   1120 		m_freem(m);
   1121 		error = ENOBUFS;
   1122 		goto out;
   1123 	}
   1124 #endif /*IPSEC*/
   1125 
   1126 	switch (af) {
   1127 #ifdef INET
   1128 	case AF_INET:
   1129 	    {
   1130 		struct mbuf *opts;
   1131 
   1132 		if (tp->t_inpcb)
   1133 			opts = tp->t_inpcb->inp_options;
   1134 		else
   1135 			opts = NULL;
   1136 		error = ip_output(m, opts, ro,
   1137 			(ip_mtudisc ? IP_MTUDISC : 0) |
   1138 			(so->so_options & SO_DONTROUTE),
   1139 			0);
   1140 		break;
   1141 	    }
   1142 #endif
   1143 #ifdef INET6
   1144 	case AF_INET6:
   1145 	    {
   1146 		struct ip6_pktopts *opts;
   1147 
   1148 		if (tp->t_in6pcb)
   1149 			opts = tp->t_in6pcb->in6p_outputopts;
   1150 		else
   1151 			opts = NULL;
   1152 		error = ip6_output(m, opts, (struct route_in6 *)ro,
   1153 			so->so_options & SO_DONTROUTE, 0, NULL);
   1154 		break;
   1155 	    }
   1156 #endif
   1157 	default:
   1158 		error = EAFNOSUPPORT;
   1159 		break;
   1160 	}
   1161 	if (error) {
   1162 out:
   1163 		if (error == ENOBUFS) {
   1164 			tcpstat.tcps_selfquench++;
   1165 #ifdef INET
   1166 			if (tp->t_inpcb)
   1167 				tcp_quench(tp->t_inpcb, 0);
   1168 #endif
   1169 #ifdef INET6
   1170 			if (tp->t_in6pcb)
   1171 				tcp6_quench(tp->t_in6pcb, 0);
   1172 #endif
   1173 			error = 0;
   1174 		} else if ((error == EHOSTUNREACH || error == ENETDOWN) &&
   1175 		    TCPS_HAVERCVDSYN(tp->t_state)) {
   1176 			tp->t_softerror = error;
   1177 			error = 0;
   1178 		}
   1179 
   1180 		/* Restart the delayed ACK timer, if necessary. */
   1181 		if (tp->t_flags & TF_DELACK)
   1182 			TCP_RESTART_DELACK(tp);
   1183 
   1184 		return (error);
   1185 	}
   1186 	tcpstat.tcps_sndtotal++;
   1187 	if (tp->t_flags & TF_DELACK)
   1188 		tcpstat.tcps_delack++;
   1189 
   1190 	/*
   1191 	 * Data sent (as far as we can tell).
   1192 	 * If this advertises a larger window than any other segment,
   1193 	 * then remember the size of the advertised window.
   1194 	 * Any pending ACK has now been sent.
   1195 	 */
   1196 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
   1197 		tp->rcv_adv = tp->rcv_nxt + win;
   1198 	tp->last_ack_sent = tp->rcv_nxt;
   1199 	tp->t_flags &= ~TF_ACKNOW;
   1200 	TCP_CLEAR_DELACK(tp);
   1201 #ifdef DIAGNOSTIC
   1202 	if (maxburst < 0)
   1203 		printf("tcp_output: maxburst exceeded by %d\n", -maxburst);
   1204 #endif
   1205 	if (sendalot && (!tcp_do_newreno || --maxburst))
   1206 		goto again;
   1207 	return (0);
   1208 }
   1209 
   1210 void
   1211 tcp_setpersist(tp)
   1212 	struct tcpcb *tp;
   1213 {
   1214 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
   1215 	int nticks;
   1216 
   1217 	if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
   1218 		panic("tcp_output REXMT");
   1219 	/*
   1220 	 * Start/restart persistance timer.
   1221 	 */
   1222 	if (t < tp->t_rttmin)
   1223 		t = tp->t_rttmin;
   1224 	TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
   1225 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
   1226 	TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
   1227 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
   1228 		tp->t_rxtshift++;
   1229 }
   1230