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