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