Home | History | Annotate | Line # | Download | only in netinet
tcp_output.c revision 1.154
      1 /*	$NetBSD: tcp_output.c,v 1.154 2007/02/10 09:43:05 degroote 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, 2005, 2006 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  * This code is derived from software contributed to The NetBSD Foundation
     80  * by Charles M. Hannum.
     81  * This code is derived from software contributed to The NetBSD Foundation
     82  * by Rui Paulo.
     83  *
     84  * Redistribution and use in source and binary forms, with or without
     85  * modification, are permitted provided that the following conditions
     86  * are met:
     87  * 1. Redistributions of source code must retain the above copyright
     88  *    notice, this list of conditions and the following disclaimer.
     89  * 2. Redistributions in binary form must reproduce the above copyright
     90  *    notice, this list of conditions and the following disclaimer in the
     91  *    documentation and/or other materials provided with the distribution.
     92  * 3. All advertising materials mentioning features or use of this software
     93  *    must display the following acknowledgement:
     94  *	This product includes software developed by the NetBSD
     95  *	Foundation, Inc. and its contributors.
     96  * 4. Neither the name of The NetBSD Foundation nor the names of its
     97  *    contributors may be used to endorse or promote products derived
     98  *    from this software without specific prior written permission.
     99  *
    100  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
    101  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    102  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    103  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
    104  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    105  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    106  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    107  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    108  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    109  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    110  * POSSIBILITY OF SUCH DAMAGE.
    111  */
    112 
    113 /*
    114  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
    115  *	The Regents of the University of California.  All rights reserved.
    116  *
    117  * Redistribution and use in source and binary forms, with or without
    118  * modification, are permitted provided that the following conditions
    119  * are met:
    120  * 1. Redistributions of source code must retain the above copyright
    121  *    notice, this list of conditions and the following disclaimer.
    122  * 2. Redistributions in binary form must reproduce the above copyright
    123  *    notice, this list of conditions and the following disclaimer in the
    124  *    documentation and/or other materials provided with the distribution.
    125  * 3. 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.154 2007/02/10 09:43:05 degroote 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 #ifdef TCP_SIGNATURE
    162 #include <sys/md5.h>
    163 #endif
    164 
    165 #include <net/if.h>
    166 #include <net/route.h>
    167 
    168 #include <netinet/in.h>
    169 #include <netinet/in_systm.h>
    170 #include <netinet/ip.h>
    171 #include <netinet/in_pcb.h>
    172 #include <netinet/ip_var.h>
    173 
    174 #ifdef INET6
    175 #ifndef INET
    176 #include <netinet/in.h>
    177 #endif
    178 #include <netinet/ip6.h>
    179 #include <netinet6/in6_var.h>
    180 #include <netinet6/ip6_var.h>
    181 #include <netinet6/in6_pcb.h>
    182 #include <netinet6/nd6.h>
    183 #endif
    184 
    185 #ifdef FAST_IPSEC
    186 #include <netipsec/ipsec.h>
    187 #include <netipsec/key.h>
    188 #ifdef INET6
    189 #include <netipsec/ipsec6.h>
    190 #endif
    191 #endif	/* FAST_IPSEC*/
    192 #ifdef IPSEC
    193 #include <netinet6/ipsec.h>
    194 #endif
    195 
    196 #include <netinet/tcp.h>
    197 #define	TCPOUTFLAGS
    198 #include <netinet/tcp_fsm.h>
    199 #include <netinet/tcp_seq.h>
    200 #include <netinet/tcp_timer.h>
    201 #include <netinet/tcp_var.h>
    202 #include <netinet/tcp_congctl.h>
    203 #include <netinet/tcpip.h>
    204 #include <netinet/tcp_debug.h>
    205 #include <netinet/in_offload.h>
    206 #include <netinet6/in6_offload.h>
    207 
    208 #ifdef IPSEC
    209 #include <netkey/key.h>
    210 #endif
    211 
    212 #ifdef notyet
    213 extern struct mbuf *m_copypack();
    214 #endif
    215 
    216 /*
    217  * Knob to enable Congestion Window Monitoring, and control
    218  * the burst size it allows.  Default burst is 4 packets, per
    219  * the Internet draft.
    220  */
    221 int	tcp_cwm = 0;
    222 int	tcp_cwm_burstsize = 4;
    223 
    224 #ifdef TCP_OUTPUT_COUNTERS
    225 #include <sys/device.h>
    226 
    227 extern struct evcnt tcp_output_bigheader;
    228 extern struct evcnt tcp_output_predict_hit;
    229 extern struct evcnt tcp_output_predict_miss;
    230 extern struct evcnt tcp_output_copysmall;
    231 extern struct evcnt tcp_output_copybig;
    232 extern struct evcnt tcp_output_refbig;
    233 
    234 #define	TCP_OUTPUT_COUNTER_INCR(ev)	(ev)->ev_count++
    235 #else
    236 
    237 #define	TCP_OUTPUT_COUNTER_INCR(ev)	/* nothing */
    238 
    239 #endif /* TCP_OUTPUT_COUNTERS */
    240 
    241 static
    242 #ifndef GPROF
    243 inline
    244 #endif
    245 int
    246 tcp_segsize(struct tcpcb *tp, int *txsegsizep, int *rxsegsizep,
    247     boolean_t *alwaysfragp)
    248 {
    249 #ifdef INET
    250 	struct inpcb *inp = tp->t_inpcb;
    251 #endif
    252 #ifdef INET6
    253 	struct in6pcb *in6p = tp->t_in6pcb;
    254 #endif
    255 	struct socket *so = NULL;
    256 	struct rtentry *rt;
    257 	struct ifnet *ifp;
    258 	int size;
    259 	int hdrlen;
    260 	int optlen;
    261 
    262 	*alwaysfragp = FALSE;
    263 
    264 #ifdef DIAGNOSTIC
    265 	if (tp->t_inpcb && tp->t_in6pcb)
    266 		panic("tcp_segsize: both t_inpcb and t_in6pcb are set");
    267 #endif
    268 	switch (tp->t_family) {
    269 #ifdef INET
    270 	case AF_INET:
    271 		hdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
    272 		break;
    273 #endif
    274 #ifdef INET6
    275 	case AF_INET6:
    276 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
    277 		break;
    278 #endif
    279 	default:
    280 		size = tcp_mssdflt;
    281 		goto out;
    282 	}
    283 
    284 	rt = NULL;
    285 #ifdef INET
    286 	if (inp) {
    287 		rt = in_pcbrtentry(inp);
    288 		so = inp->inp_socket;
    289 	}
    290 #endif
    291 #ifdef INET6
    292 	if (in6p) {
    293 		rt = in6_pcbrtentry(in6p);
    294 		so = in6p->in6p_socket;
    295 	}
    296 #endif
    297 	if (rt == NULL) {
    298 		size = tcp_mssdflt;
    299 		goto out;
    300 	}
    301 
    302 	ifp = rt->rt_ifp;
    303 
    304 	size = tcp_mssdflt;
    305 	if (tp->t_mtudisc && rt->rt_rmx.rmx_mtu != 0) {
    306 #ifdef INET6
    307 		if (in6p && rt->rt_rmx.rmx_mtu < IPV6_MMTU) {
    308 			/*
    309 			 * RFC2460 section 5, last paragraph: if path MTU is
    310 			 * smaller than 1280, use 1280 as packet size and
    311 			 * attach fragment header.
    312 			 */
    313 			size = IPV6_MMTU - hdrlen - sizeof(struct ip6_frag);
    314 			*alwaysfragp = TRUE;
    315 		} else
    316 			size = rt->rt_rmx.rmx_mtu - hdrlen;
    317 #else
    318 		size = rt->rt_rmx.rmx_mtu - hdrlen;
    319 #endif
    320 	} else if (ifp->if_flags & IFF_LOOPBACK)
    321 		size = ifp->if_mtu - hdrlen;
    322 #ifdef INET
    323 	else if (inp && tp->t_mtudisc)
    324 		size = ifp->if_mtu - hdrlen;
    325 	else if (inp && in_localaddr(inp->inp_faddr))
    326 		size = ifp->if_mtu - hdrlen;
    327 #endif
    328 #ifdef INET6
    329 	else if (in6p) {
    330 #ifdef INET
    331 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
    332 			/* mapped addr case */
    333 			struct in_addr d;
    334 			bcopy(&in6p->in6p_faddr.s6_addr32[3], &d, sizeof(d));
    335 			if (tp->t_mtudisc || in_localaddr(d))
    336 				size = ifp->if_mtu - hdrlen;
    337 		} else
    338 #endif
    339 		{
    340 			/*
    341 			 * for IPv6, path MTU discovery is always turned on,
    342 			 * or the node must use packet size <= 1280.
    343 			 */
    344 			size = tp->t_mtudisc ? IN6_LINKMTU(ifp) : IPV6_MMTU;
    345 			size -= hdrlen;
    346 		}
    347 	}
    348 #endif
    349  out:
    350 	/*
    351 	 * Now we must make room for whatever extra TCP/IP options are in
    352 	 * the packet.
    353 	 */
    354 	optlen = tcp_optlen(tp);
    355 
    356 	/*
    357 	 * XXX tp->t_ourmss should have the right size, but without this code
    358 	 * fragmentation will occur... need more investigation
    359 	 */
    360 #ifdef INET
    361 	if (inp) {
    362 #if defined(IPSEC) || defined(FAST_IPSEC)
    363 		if (! IPSEC_PCB_SKIP_IPSEC(inp->inp_sp, IPSEC_DIR_OUTBOUND))
    364 			optlen += ipsec4_hdrsiz_tcp(tp);
    365 #endif
    366 		optlen += ip_optlen(inp);
    367 	}
    368 #endif
    369 #ifdef INET6
    370 #ifdef INET
    371 	if (in6p && tp->t_family == AF_INET) {
    372 #if defined(IPSEC) || defined(FAST_IPSEC)
    373 		if (! IPSEC_PCB_SKIP_IPSEC(in6p->in6p_sp, IPSEC_DIR_OUTBOUND))
    374 			optlen += ipsec4_hdrsiz_tcp(tp);
    375 #endif
    376 		/* XXX size -= ip_optlen(in6p); */
    377 	} else
    378 #endif
    379 	if (in6p && tp->t_family == AF_INET6) {
    380 #if defined(IPSEC) || defined(FAST_IPSEC)
    381 		if (! IPSEC_PCB_SKIP_IPSEC(in6p->in6p_sp, IPSEC_DIR_OUTBOUND))
    382 			optlen += ipsec6_hdrsiz_tcp(tp);
    383 #endif
    384 		optlen += ip6_optlen(in6p);
    385 	}
    386 #endif
    387 	size -= optlen;
    388 
    389 	/* there may not be any room for data if mtu is too small */
    390 	if (size < 0)
    391 		return (EMSGSIZE);
    392 
    393 	/*
    394 	 * *rxsegsizep holds *estimated* inbound segment size (estimation
    395 	 * assumes that path MTU is the same for both ways).  this is only
    396 	 * for silly window avoidance, do not use the value for other purposes.
    397 	 *
    398 	 * ipseclen is subtracted from both sides, this may not be right.
    399 	 * I'm not quite sure about this (could someone comment).
    400 	 */
    401 	*txsegsizep = min(tp->t_peermss - optlen, size);
    402 	/*
    403 	 * Never send more than half a buffer full.  This insures that we can
    404 	 * always keep 2 packets on the wire, no matter what SO_SNDBUF is, and
    405 	 * therefore acks will never be delayed unless we run out of data to
    406 	 * transmit.
    407 	 */
    408 	if (so)
    409 		*txsegsizep = min(so->so_snd.sb_hiwat >> 1, *txsegsizep);
    410 	*rxsegsizep = min(tp->t_ourmss - optlen, size);
    411 
    412 	if (*txsegsizep != tp->t_segsz) {
    413 		/*
    414 		 * If the new segment size is larger, we don't want to
    415 		 * mess up the congestion window, but if it is smaller
    416 		 * we'll have to reduce the congestion window to ensure
    417 		 * that we don't get into trouble with initial windows
    418 		 * and the rest.  In any case, if the segment size
    419 		 * has changed, chances are the path has, too, and
    420 		 * our congestion window will be different.
    421 		 */
    422 		if (*txsegsizep < tp->t_segsz) {
    423 			tp->snd_cwnd = max((tp->snd_cwnd / tp->t_segsz)
    424 					   * *txsegsizep, *txsegsizep);
    425 			tp->snd_ssthresh = max((tp->snd_ssthresh / tp->t_segsz)
    426 						* *txsegsizep, *txsegsizep);
    427 		}
    428 		tp->t_segsz = *txsegsizep;
    429 	}
    430 
    431 	return (0);
    432 }
    433 
    434 static
    435 #ifndef GPROF
    436 inline
    437 #endif
    438 int
    439 tcp_build_datapkt(struct tcpcb *tp, struct socket *so, int off,
    440     long len, int hdrlen, struct mbuf **mp)
    441 {
    442 	struct mbuf *m, *m0;
    443 
    444 	if (tp->t_force && len == 1)
    445 		tcpstat.tcps_sndprobe++;
    446 	else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
    447 		tcpstat.tcps_sndrexmitpack++;
    448 		tcpstat.tcps_sndrexmitbyte += len;
    449 	} else {
    450 		tcpstat.tcps_sndpack++;
    451 		tcpstat.tcps_sndbyte += len;
    452 	}
    453 #ifdef notyet
    454 	if ((m = m_copypack(so->so_snd.sb_mb, off,
    455 	    (int)len, max_linkhdr + hdrlen)) == 0)
    456 		return (ENOBUFS);
    457 	/*
    458 	 * m_copypack left space for our hdr; use it.
    459 	 */
    460 	m->m_len += hdrlen;
    461 	m->m_data -= hdrlen;
    462 #else
    463 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
    464 	if (__predict_false(m == NULL))
    465 		return (ENOBUFS);
    466 	MCLAIM(m, &tcp_tx_mowner);
    467 
    468 	/*
    469 	 * XXX Because other code assumes headers will fit in
    470 	 * XXX one header mbuf.
    471 	 *
    472 	 * (This code should almost *never* be run.)
    473 	 */
    474 	if (__predict_false((max_linkhdr + hdrlen) > MHLEN)) {
    475 		TCP_OUTPUT_COUNTER_INCR(&tcp_output_bigheader);
    476 		MCLGET(m, M_DONTWAIT);
    477 		if ((m->m_flags & M_EXT) == 0) {
    478 			m_freem(m);
    479 			return (ENOBUFS);
    480 		}
    481 	}
    482 
    483 	m->m_data += max_linkhdr;
    484 	m->m_len = hdrlen;
    485 
    486 	/*
    487 	 * To avoid traversing the whole sb_mb chain for correct
    488 	 * data to send, remember last sent mbuf, its offset and
    489 	 * the sent size.  When called the next time, see if the
    490 	 * data to send is directly following the previous transfer.
    491 	 * This is important for large TCP windows.
    492 	 */
    493 	if (off == 0 || tp->t_lastm == NULL ||
    494 	    (tp->t_lastoff + tp->t_lastlen) != off) {
    495 		TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_miss);
    496 		/*
    497 		 * Either a new packet or a retransmit.
    498 		 * Start from the beginning.
    499 		 */
    500 		tp->t_lastm = so->so_snd.sb_mb;
    501 		tp->t_inoff = off;
    502 	} else {
    503 		TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_hit);
    504 		tp->t_inoff += tp->t_lastlen;
    505 	}
    506 
    507 	/* Traverse forward to next packet */
    508 	while (tp->t_inoff > 0) {
    509 		if (tp->t_lastm == NULL)
    510 			panic("tp->t_lastm == NULL");
    511 		if (tp->t_inoff < tp->t_lastm->m_len)
    512 			break;
    513 		tp->t_inoff -= tp->t_lastm->m_len;
    514 		tp->t_lastm = tp->t_lastm->m_next;
    515 	}
    516 
    517 	tp->t_lastoff = off;
    518 	tp->t_lastlen = len;
    519 	m0 = tp->t_lastm;
    520 	off = tp->t_inoff;
    521 
    522 	if (len <= M_TRAILINGSPACE(m)) {
    523 		m_copydata(m0, off, (int) len, mtod(m, caddr_t) + hdrlen);
    524 		m->m_len += len;
    525 		TCP_OUTPUT_COUNTER_INCR(&tcp_output_copysmall);
    526 	} else {
    527 		m->m_next = m_copy(m0, off, (int) len);
    528 		if (m->m_next == NULL) {
    529 			m_freem(m);
    530 			return (ENOBUFS);
    531 		}
    532 #ifdef TCP_OUTPUT_COUNTERS
    533 		if (m->m_next->m_flags & M_EXT)
    534 			TCP_OUTPUT_COUNTER_INCR(&tcp_output_refbig);
    535 		else
    536 			TCP_OUTPUT_COUNTER_INCR(&tcp_output_copybig);
    537 #endif /* TCP_OUTPUT_COUNTERS */
    538 	}
    539 #endif
    540 
    541 	*mp = m;
    542 	return (0);
    543 }
    544 
    545 /*
    546  * Tcp output routine: figure out what should be sent and send it.
    547  */
    548 int
    549 tcp_output(struct tcpcb *tp)
    550 {
    551 	struct socket *so;
    552 	struct route *ro;
    553 	long len, win;
    554 	int off, flags, error;
    555 	struct mbuf *m;
    556 	struct ip *ip;
    557 #ifdef INET6
    558 	struct ip6_hdr *ip6;
    559 #endif
    560 	struct tcphdr *th;
    561 	u_char opt[MAX_TCPOPTLEN];
    562 	unsigned optlen, hdrlen, packetlen;
    563 	unsigned int sack_numblks;
    564 	int idle, sendalot, txsegsize, rxsegsize;
    565 	int txsegsize_nosack;
    566 	int maxburst = TCP_MAXBURST;
    567 	int af;		/* address family on the wire */
    568 	int iphdrlen;
    569 	int has_tso4, has_tso6;
    570 	int has_tso, use_tso;
    571 	boolean_t alwaysfrag;
    572 	int sack_rxmit;
    573 	int sack_bytes_rxmt;
    574 	struct sackhole *p;
    575 #ifdef TCP_SIGNATURE
    576 	int sigoff = 0;
    577 #endif
    578 
    579 #ifdef DIAGNOSTIC
    580 	if (tp->t_inpcb && tp->t_in6pcb)
    581 		panic("tcp_output: both t_inpcb and t_in6pcb are set");
    582 #endif
    583 	so = NULL;
    584 	ro = NULL;
    585 	if (tp->t_inpcb) {
    586 		so = tp->t_inpcb->inp_socket;
    587 		ro = &tp->t_inpcb->inp_route;
    588 	}
    589 #ifdef INET6
    590 	else if (tp->t_in6pcb) {
    591 		so = tp->t_in6pcb->in6p_socket;
    592 		ro = (struct route *)&tp->t_in6pcb->in6p_route;
    593 	}
    594 #endif
    595 
    596 	switch (af = tp->t_family) {
    597 #ifdef INET
    598 	case AF_INET:
    599 		if (tp->t_inpcb)
    600 			break;
    601 #ifdef INET6
    602 		/* mapped addr case */
    603 		if (tp->t_in6pcb)
    604 			break;
    605 #endif
    606 		return (EINVAL);
    607 #endif
    608 #ifdef INET6
    609 	case AF_INET6:
    610 		if (tp->t_in6pcb)
    611 			break;
    612 		return (EINVAL);
    613 #endif
    614 	default:
    615 		return (EAFNOSUPPORT);
    616 	}
    617 
    618 	if (tcp_segsize(tp, &txsegsize, &rxsegsize, &alwaysfrag))
    619 		return (EMSGSIZE);
    620 
    621 	idle = (tp->snd_max == tp->snd_una);
    622 
    623 	/*
    624 	 * Determine if we can use TCP segmentation offload:
    625 	 * - If we're using IPv4
    626 	 * - If there is not an IPsec policy that prevents it
    627 	 * - If the interface can do it
    628 	 */
    629 	has_tso4 = has_tso6 = FALSE;
    630 #if defined(INET)
    631 	has_tso4 = tp->t_inpcb != NULL &&
    632 #if defined(IPSEC) || defined(FAST_IPSEC)
    633 		  IPSEC_PCB_SKIP_IPSEC(tp->t_inpcb->inp_sp,
    634 		  		       IPSEC_DIR_OUTBOUND) &&
    635 #endif
    636 		  tp->t_inpcb->inp_route.ro_rt != NULL &&
    637 		  (tp->t_inpcb->inp_route.ro_rt->rt_ifp->if_capenable &
    638 		   IFCAP_TSOv4) != 0;
    639 #endif /* defined(INET) */
    640 #if defined(INET6)
    641 	has_tso6 = tp->t_in6pcb != NULL &&
    642 #if defined(IPSEC) || defined(FAST_IPSEC)
    643 		  IPSEC_PCB_SKIP_IPSEC(tp->t_in6pcb->in6p_sp,
    644 		  		       IPSEC_DIR_OUTBOUND) &&
    645 #endif
    646 		  tp->t_in6pcb->in6p_route.ro_rt != NULL &&
    647 		  (tp->t_in6pcb->in6p_route.ro_rt->rt_ifp->if_capenable &
    648 		   IFCAP_TSOv6) != 0;
    649 #endif /* defined(INET6) */
    650 	has_tso = (has_tso4 || has_tso6) && !alwaysfrag;
    651 
    652 	/*
    653 	 * Restart Window computation.  From draft-floyd-incr-init-win-03:
    654 	 *
    655 	 *	Optionally, a TCP MAY set the restart window to the
    656 	 *	minimum of the value used for the initial window and
    657 	 *	the current value of cwnd (in other words, using a
    658 	 *	larger value for the restart window should never increase
    659 	 *	the size of cwnd).
    660 	 */
    661 	if (tcp_cwm) {
    662 		/*
    663 		 * Hughes/Touch/Heidemann Congestion Window Monitoring.
    664 		 * Count the number of packets currently pending
    665 		 * acknowledgement, and limit our congestion window
    666 		 * to a pre-determined allowed burst size plus that count.
    667 		 * This prevents bursting once all pending packets have
    668 		 * been acknowledged (i.e. transmission is idle).
    669 		 *
    670 		 * XXX Link this to Initial Window?
    671 		 */
    672 		tp->snd_cwnd = min(tp->snd_cwnd,
    673 		    (tcp_cwm_burstsize * txsegsize) +
    674 		    (tp->snd_nxt - tp->snd_una));
    675 	} else {
    676 		if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur) {
    677 			/*
    678 			 * We have been idle for "a while" and no acks are
    679 			 * expected to clock out any data we send --
    680 			 * slow start to get ack "clock" running again.
    681 			 */
    682 			int ss = tcp_init_win;
    683 #ifdef INET
    684 			if (tp->t_inpcb &&
    685 			    in_localaddr(tp->t_inpcb->inp_faddr))
    686 				ss = tcp_init_win_local;
    687 #endif
    688 #ifdef INET6
    689 			if (tp->t_in6pcb &&
    690 			    in6_localaddr(&tp->t_in6pcb->in6p_faddr))
    691 				ss = tcp_init_win_local;
    692 #endif
    693 			tp->snd_cwnd = min(tp->snd_cwnd,
    694 			    TCP_INITIAL_WINDOW(ss, txsegsize));
    695 		}
    696 	}
    697 
    698 	txsegsize_nosack = txsegsize;
    699 again:
    700 	use_tso = has_tso;
    701 	if ((tp->t_flags & (TF_ECN_SND_CWR|TF_ECN_SND_ECE)) != 0) {
    702 		/* don't duplicate CWR/ECE. */
    703 		use_tso = 0;
    704 	}
    705 	TCP_REASS_LOCK(tp);
    706 	sack_numblks = tcp_sack_numblks(tp);
    707 	if (sack_numblks) {
    708 		int sackoptlen;
    709 
    710 		sackoptlen = TCP_SACK_OPTLEN(sack_numblks);
    711 		if (sackoptlen > txsegsize_nosack) {
    712 			sack_numblks = 0; /* give up SACK */
    713 			txsegsize = txsegsize_nosack;
    714 		} else {
    715 			if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) {
    716 				/* don't duplicate D-SACK. */
    717 				use_tso = 0;
    718 			}
    719 			txsegsize = txsegsize_nosack - sackoptlen;
    720 		}
    721 	} else {
    722 		txsegsize = txsegsize_nosack;
    723 	}
    724 
    725 	/*
    726 	 * Determine length of data that should be transmitted, and
    727 	 * flags that should be used.  If there is some data or critical
    728 	 * controls (SYN, RST) to send, then transmit; otherwise,
    729 	 * investigate further.
    730 	 *
    731 	 * Readjust SACK information to avoid resending duplicate data.
    732 	 */
    733 	if (TCP_SACK_ENABLED(tp) && SEQ_LT(tp->snd_nxt, tp->snd_max))
    734 		tcp_sack_adjust(tp);
    735 	sendalot = 0;
    736 	off = tp->snd_nxt - tp->snd_una;
    737 	win = min(tp->snd_wnd, tp->snd_cwnd);
    738 
    739 	flags = tcp_outflags[tp->t_state];
    740 
    741 	/*
    742 	 * Send any SACK-generated retransmissions.  If we're explicitly trying
    743 	 * to send out new data (when sendalot is 1), bypass this function.
    744 	 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
    745 	 * we're replacing a (future) new transmission with a retransmission
    746 	 * now, and we previously incremented snd_cwnd in tcp_input().
    747 	 */
    748 	/*
    749 	 * Still in sack recovery , reset rxmit flag to zero.
    750 	 */
    751 	sack_rxmit = 0;
    752 	sack_bytes_rxmt = 0;
    753 	len = 0;
    754 	p = NULL;
    755 	do {
    756 		long cwin;
    757 		if (!TCP_SACK_ENABLED(tp))
    758 			break;
    759 		if (tp->t_partialacks < 0)
    760 			break;
    761 		p = tcp_sack_output(tp, &sack_bytes_rxmt);
    762 		if (p == NULL)
    763 			break;
    764 
    765 		cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
    766 		if (cwin < 0)
    767 			cwin = 0;
    768 		/* Do not retransmit SACK segments beyond snd_recover */
    769 		if (SEQ_GT(p->end, tp->snd_recover)) {
    770 			/*
    771 			 * (At least) part of sack hole extends beyond
    772 			 * snd_recover. Check to see if we can rexmit data
    773 			 * for this hole.
    774 			 */
    775 			if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
    776 				/*
    777 				 * Can't rexmit any more data for this hole.
    778 				 * That data will be rexmitted in the next
    779 				 * sack recovery episode, when snd_recover
    780 				 * moves past p->rxmit.
    781 				 */
    782 				p = NULL;
    783 				break;
    784 			}
    785 			/* Can rexmit part of the current hole */
    786 			len = ((long)ulmin(cwin, tp->snd_recover - p->rxmit));
    787 		} else
    788 			len = ((long)ulmin(cwin, p->end - p->rxmit));
    789 		off = p->rxmit - tp->snd_una;
    790 		if (off + len > so->so_snd.sb_cc) {
    791 			/* 1 for TH_FIN */
    792 			KASSERT(off + len == so->so_snd.sb_cc + 1);
    793 			KASSERT(p->rxmit + len == tp->snd_max);
    794 			len = so->so_snd.sb_cc - off;
    795 		}
    796 		if (len > 0) {
    797 			sack_rxmit = 1;
    798 			sendalot = 1;
    799 		}
    800 	} while (/*CONSTCOND*/0);
    801 
    802 	/*
    803 	 * If in persist timeout with window of 0, send 1 byte.
    804 	 * Otherwise, if window is small but nonzero
    805 	 * and timer expired, we will send what we can
    806 	 * and go to transmit state.
    807 	 */
    808 	if (tp->t_force) {
    809 		if (win == 0) {
    810 			/*
    811 			 * If we still have some data to send, then
    812 			 * clear the FIN bit.  Usually this would
    813 			 * happen below when it realizes that we
    814 			 * aren't sending all the data.  However,
    815 			 * if we have exactly 1 byte of unset data,
    816 			 * then it won't clear the FIN bit below,
    817 			 * and if we are in persist state, we wind
    818 			 * up sending the packet without recording
    819 			 * that we sent the FIN bit.
    820 			 *
    821 			 * We can't just blindly clear the FIN bit,
    822 			 * because if we don't have any more data
    823 			 * to send then the probe will be the FIN
    824 			 * itself.
    825 			 */
    826 			if (off < so->so_snd.sb_cc)
    827 				flags &= ~TH_FIN;
    828 			win = 1;
    829 		} else {
    830 			TCP_TIMER_DISARM(tp, TCPT_PERSIST);
    831 			tp->t_rxtshift = 0;
    832 		}
    833 	}
    834 
    835 	if (sack_rxmit == 0) {
    836 		if (TCP_SACK_ENABLED(tp) && tp->t_partialacks >= 0) {
    837 			long cwin;
    838 
    839 			/*
    840 			 * We are inside of a SACK recovery episode and are
    841 			 * sending new data, having retransmitted all the
    842 			 * data possible in the scoreboard.
    843 			 */
    844 			if (tp->snd_wnd < so->so_snd.sb_cc) {
    845 				len = tp->snd_wnd - off;
    846 				flags &= ~TH_FIN;
    847 			} else {
    848 				len = so->so_snd.sb_cc - off;
    849 			}
    850 
    851 			/*
    852 			 * From FreeBSD:
    853 			 *  Don't remove this (len > 0) check !
    854 			 *  We explicitly check for len > 0 here (although it
    855 			 *  isn't really necessary), to work around a gcc
    856 			 *  optimization issue - to force gcc to compute
    857 			 *  len above. Without this check, the computation
    858 			 *  of len is bungled by the optimizer.
    859 			 */
    860 			if (len > 0) {
    861 				cwin = tp->snd_cwnd -
    862 				    (tp->snd_nxt - tp->sack_newdata) -
    863 				    sack_bytes_rxmt;
    864 				if (cwin < 0)
    865 					cwin = 0;
    866 				if (cwin < len) {
    867 					len = cwin;
    868 					flags &= ~TH_FIN;
    869 				}
    870 			}
    871 		} else if (win < so->so_snd.sb_cc) {
    872 			len = win - off;
    873 			flags &= ~TH_FIN;
    874 		} else {
    875 			len = so->so_snd.sb_cc - off;
    876 		}
    877 	}
    878 
    879 	if (len < 0) {
    880 		/*
    881 		 * If FIN has been sent but not acked,
    882 		 * but we haven't been called to retransmit,
    883 		 * len will be -1.  Otherwise, window shrank
    884 		 * after we sent into it.  If window shrank to 0,
    885 		 * cancel pending retransmit, pull snd_nxt back
    886 		 * to (closed) window, and set the persist timer
    887 		 * if it isn't already going.  If the window didn't
    888 		 * close completely, just wait for an ACK.
    889 		 *
    890 		 * If we have a pending FIN, either it has already been
    891 		 * transmitted or it is outside the window, so drop it.
    892 		 * If the FIN has been transmitted, but this is not a
    893 		 * retransmission, then len must be -1.  Therefore we also
    894 		 * prevent here the sending of `gratuitous FINs'.  This
    895 		 * eliminates the need to check for that case below (e.g.
    896 		 * to back up snd_nxt before the FIN so that the sequence
    897 		 * number is correct).
    898 		 */
    899 		len = 0;
    900 		flags &= ~TH_FIN;
    901 		if (win == 0) {
    902 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
    903 			tp->t_rxtshift = 0;
    904 			tp->snd_nxt = tp->snd_una;
    905 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
    906 				tcp_setpersist(tp);
    907 		}
    908 	}
    909 	if (len > txsegsize) {
    910 		if (use_tso) {
    911 			/*
    912 			 * Truncate TSO transfers to IP_MAXPACKET, and make
    913 			 * sure that we send equal size transfers down the
    914 			 * stack (rather than big-small-big-small-...).
    915 			 */
    916 #ifdef INET6
    917 #if IPV6_MAXPACKET != IP_MAXPACKET
    918 #error IPV6_MAXPACKET != IP_MAXPACKET
    919 #endif
    920 #endif
    921 			len = (min(len, IP_MAXPACKET) / txsegsize) * txsegsize;
    922 			if (len <= txsegsize) {
    923 				use_tso = 0;
    924 			}
    925 		} else
    926 			len = txsegsize;
    927 		flags &= ~TH_FIN;
    928 		sendalot = 1;
    929 	} else
    930 		use_tso = 0;
    931 	if (sack_rxmit) {
    932 		if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
    933 			flags &= ~TH_FIN;
    934 	}
    935 
    936 	win = sbspace(&so->so_rcv);
    937 
    938 	/*
    939 	 * Sender silly window avoidance.  If connection is idle
    940 	 * and can send all data, a maximum segment,
    941 	 * at least a maximum default-size segment do it,
    942 	 * or are forced, do it; otherwise don't bother.
    943 	 * If peer's buffer is tiny, then send
    944 	 * when window is at least half open.
    945 	 * If retransmitting (possibly after persist timer forced us
    946 	 * to send into a small window), then must resend.
    947 	 */
    948 	if (len) {
    949 		if (len >= txsegsize)
    950 			goto send;
    951 		if ((so->so_state & SS_MORETOCOME) == 0 &&
    952 		    ((idle || tp->t_flags & TF_NODELAY) &&
    953 		     len + off >= so->so_snd.sb_cc))
    954 			goto send;
    955 		if (tp->t_force)
    956 			goto send;
    957 		if (len >= tp->max_sndwnd / 2)
    958 			goto send;
    959 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
    960 			goto send;
    961 		if (sack_rxmit)
    962 			goto send;
    963 	}
    964 
    965 	/*
    966 	 * Compare available window to amount of window known to peer
    967 	 * (as advertised window less next expected input).  If the
    968 	 * difference is at least twice the size of the largest segment
    969 	 * we expect to receive (i.e. two segments) or at least 50% of
    970 	 * the maximum possible window, then want to send a window update
    971 	 * to peer.
    972 	 */
    973 	if (win > 0) {
    974 		/*
    975 		 * "adv" is the amount we can increase the window,
    976 		 * taking into account that we are limited by
    977 		 * TCP_MAXWIN << tp->rcv_scale.
    978 		 */
    979 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
    980 			(tp->rcv_adv - tp->rcv_nxt);
    981 
    982 		if (adv >= (long) (2 * rxsegsize))
    983 			goto send;
    984 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
    985 			goto send;
    986 	}
    987 
    988 	/*
    989 	 * Send if we owe peer an ACK.
    990 	 */
    991 	if (tp->t_flags & TF_ACKNOW)
    992 		goto send;
    993 	if (flags & (TH_SYN|TH_FIN|TH_RST))
    994 		goto send;
    995 	if (SEQ_GT(tp->snd_up, tp->snd_una))
    996 		goto send;
    997 	/*
    998 	 * In SACK, it is possible for tcp_output to fail to send a segment
    999 	 * after the retransmission timer has been turned off.  Make sure
   1000 	 * that the retransmission timer is set.
   1001 	 */
   1002 	if (TCP_SACK_ENABLED(tp) && SEQ_GT(tp->snd_max, tp->snd_una) &&
   1003 	    !TCP_TIMER_ISARMED(tp, TCPT_REXMT) &&
   1004 	    !TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
   1005 		TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
   1006 		goto just_return;
   1007 	}
   1008 
   1009 	/*
   1010 	 * TCP window updates are not reliable, rather a polling protocol
   1011 	 * using ``persist'' packets is used to insure receipt of window
   1012 	 * updates.  The three ``states'' for the output side are:
   1013 	 *	idle			not doing retransmits or persists
   1014 	 *	persisting		to move a small or zero window
   1015 	 *	(re)transmitting	and thereby not persisting
   1016 	 *
   1017 	 * tp->t_timer[TCPT_PERSIST]
   1018 	 *	is set when we are in persist state.
   1019 	 * tp->t_force
   1020 	 *	is set when we are called to send a persist packet.
   1021 	 * tp->t_timer[TCPT_REXMT]
   1022 	 *	is set when we are retransmitting
   1023 	 * The output side is idle when both timers are zero.
   1024 	 *
   1025 	 * If send window is too small, there is data to transmit, and no
   1026 	 * retransmit or persist is pending, then go to persist state.
   1027 	 * If nothing happens soon, send when timer expires:
   1028 	 * if window is nonzero, transmit what we can,
   1029 	 * otherwise force out a byte.
   1030 	 */
   1031 	if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
   1032 	    TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
   1033 		tp->t_rxtshift = 0;
   1034 		tcp_setpersist(tp);
   1035 	}
   1036 
   1037 	/*
   1038 	 * No reason to send a segment, just return.
   1039 	 */
   1040 just_return:
   1041 	TCP_REASS_UNLOCK(tp);
   1042 	return (0);
   1043 
   1044 send:
   1045 	/*
   1046 	 * Before ESTABLISHED, force sending of initial options
   1047 	 * unless TCP set not to do any options.
   1048 	 * NOTE: we assume that the IP/TCP header plus TCP options
   1049 	 * always fit in a single mbuf, leaving room for a maximum
   1050 	 * link header, i.e.
   1051 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
   1052 	 */
   1053 	optlen = 0;
   1054 	switch (af) {
   1055 #ifdef INET
   1056 	case AF_INET:
   1057 		iphdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
   1058 		break;
   1059 #endif
   1060 #ifdef INET6
   1061 	case AF_INET6:
   1062 		iphdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
   1063 		break;
   1064 #endif
   1065 	default:	/*pacify gcc*/
   1066 		iphdrlen = 0;
   1067 		break;
   1068 	}
   1069 	hdrlen = iphdrlen;
   1070 	if (flags & TH_SYN) {
   1071 		struct rtentry *rt;
   1072 
   1073 		rt = NULL;
   1074 #ifdef INET
   1075 		if (tp->t_inpcb)
   1076 			rt = in_pcbrtentry(tp->t_inpcb);
   1077 #endif
   1078 #ifdef INET6
   1079 		if (tp->t_in6pcb)
   1080 			rt = in6_pcbrtentry(tp->t_in6pcb);
   1081 #endif
   1082 
   1083 		tp->snd_nxt = tp->iss;
   1084 		tp->t_ourmss = tcp_mss_to_advertise(rt != NULL ?
   1085 						    rt->rt_ifp : NULL, af);
   1086 		if ((tp->t_flags & TF_NOOPT) == 0) {
   1087 			opt[0] = TCPOPT_MAXSEG;
   1088 			opt[1] = 4;
   1089 			opt[2] = (tp->t_ourmss >> 8) & 0xff;
   1090 			opt[3] = tp->t_ourmss & 0xff;
   1091 			optlen = 4;
   1092 
   1093 			if ((tp->t_flags & TF_REQ_SCALE) &&
   1094 			    ((flags & TH_ACK) == 0 ||
   1095 			    (tp->t_flags & TF_RCVD_SCALE))) {
   1096 				*((u_int32_t *) (opt + optlen)) = htonl(
   1097 					TCPOPT_NOP << 24 |
   1098 					TCPOPT_WINDOW << 16 |
   1099 					TCPOLEN_WINDOW << 8 |
   1100 					tp->request_r_scale);
   1101 				optlen += 4;
   1102 			}
   1103 			if (tcp_do_sack) {
   1104 				u_int8_t *cp = (u_int8_t *)(opt + optlen);
   1105 
   1106 				cp[0] = TCPOPT_SACK_PERMITTED;
   1107 				cp[1] = 2;
   1108 				cp[2] = TCPOPT_NOP;
   1109 				cp[3] = TCPOPT_NOP;
   1110 				optlen += 4;
   1111 			}
   1112 		}
   1113 	}
   1114 
   1115 	/*
   1116 	 * Send a timestamp and echo-reply if this is a SYN and our side
   1117 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
   1118 	 * and our peer have sent timestamps in our SYN's.
   1119 	 */
   1120 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
   1121 	     (flags & TH_RST) == 0 &&
   1122 	    ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
   1123 	     (tp->t_flags & TF_RCVD_TSTMP))) {
   1124 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
   1125 
   1126 		/* Form timestamp option as shown in appendix A of RFC 1323. */
   1127 		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
   1128 		*lp++ = htonl(TCP_TIMESTAMP(tp));
   1129 		*lp   = htonl(tp->ts_recent);
   1130 		optlen += TCPOLEN_TSTAMP_APPA;
   1131 	}
   1132 
   1133 	/*
   1134 	 * Tack on the SACK block if it is necessary.
   1135 	 */
   1136 	if (sack_numblks) {
   1137 		int sack_len;
   1138 		u_char *bp = (u_char *)(opt + optlen);
   1139 		u_int32_t *lp = (u_int32_t *)(bp + 4);
   1140 		struct ipqent *tiqe;
   1141 
   1142 		sack_len = sack_numblks * 8 + 2;
   1143 		bp[0] = TCPOPT_NOP;
   1144 		bp[1] = TCPOPT_NOP;
   1145 		bp[2] = TCPOPT_SACK;
   1146 		bp[3] = sack_len;
   1147 		if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) {
   1148 			sack_numblks--;
   1149 			*lp++ = htonl(tp->rcv_dsack_block.left);
   1150 			*lp++ = htonl(tp->rcv_dsack_block.right);
   1151 			tp->rcv_sack_flags &= ~TCPSACK_HAVED;
   1152 		}
   1153 		for (tiqe = TAILQ_FIRST(&tp->timeq);
   1154 		    sack_numblks > 0; tiqe = TAILQ_NEXT(tiqe, ipqe_timeq)) {
   1155 			KASSERT(tiqe != NULL);
   1156 			sack_numblks--;
   1157 			*lp++ = htonl(tiqe->ipqe_seq);
   1158 			*lp++ = htonl(tiqe->ipqe_seq + tiqe->ipqe_len +
   1159 			    ((tiqe->ipqe_flags & TH_FIN) != 0 ? 1 : 0));
   1160 		}
   1161 		optlen += sack_len + 2;
   1162 	}
   1163 	TCP_REASS_UNLOCK(tp);
   1164 
   1165 #ifdef TCP_SIGNATURE
   1166 	if (tp->t_flags & TF_SIGNATURE) {
   1167 		u_char *bp;
   1168 		/*
   1169 		 * Initialize TCP-MD5 option (RFC2385)
   1170 		 */
   1171 		bp = (u_char *)opt + optlen;
   1172 		*bp++ = TCPOPT_SIGNATURE;
   1173 		*bp++ = TCPOLEN_SIGNATURE;
   1174 		sigoff = optlen + 2;
   1175 		bzero(bp, TCP_SIGLEN);
   1176 		bp += TCP_SIGLEN;
   1177 		optlen += TCPOLEN_SIGNATURE;
   1178 		/*
   1179 		 * Terminate options list and maintain 32-bit alignment.
   1180  		 */
   1181 		*bp++ = TCPOPT_NOP;
   1182 		*bp++ = TCPOPT_EOL;
   1183  		optlen += 2;
   1184  	}
   1185 #endif /* TCP_SIGNATURE */
   1186 
   1187 	hdrlen += optlen;
   1188 
   1189 #ifdef DIAGNOSTIC
   1190 	if (!use_tso && len > txsegsize)
   1191 		panic("tcp data to be sent is larger than segment");
   1192 	else if (use_tso && len > IP_MAXPACKET)
   1193 		panic("tcp data to be sent is larger than max TSO size");
   1194 	if (max_linkhdr + hdrlen > MCLBYTES)
   1195 		panic("tcphdr too big");
   1196 #endif
   1197 
   1198 	/*
   1199 	 * Grab a header mbuf, attaching a copy of data to
   1200 	 * be transmitted, and initialize the header from
   1201 	 * the template for sends on this connection.
   1202 	 */
   1203 	if (len) {
   1204 		error = tcp_build_datapkt(tp, so, off, len, hdrlen, &m);
   1205 		if (error)
   1206 			goto out;
   1207 		/*
   1208 		 * If we're sending everything we've got, set PUSH.
   1209 		 * (This will keep happy those implementations which only
   1210 		 * give data to the user when a buffer fills or
   1211 		 * a PUSH comes in.)
   1212 		 */
   1213 		if (off + len == so->so_snd.sb_cc)
   1214 			flags |= TH_PUSH;
   1215 	} else {
   1216 		if (tp->t_flags & TF_ACKNOW)
   1217 			tcpstat.tcps_sndacks++;
   1218 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
   1219 			tcpstat.tcps_sndctrl++;
   1220 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
   1221 			tcpstat.tcps_sndurg++;
   1222 		else
   1223 			tcpstat.tcps_sndwinup++;
   1224 
   1225 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
   1226 		if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
   1227 			MCLGET(m, M_DONTWAIT);
   1228 			if ((m->m_flags & M_EXT) == 0) {
   1229 				m_freem(m);
   1230 				m = NULL;
   1231 			}
   1232 		}
   1233 		if (m == NULL) {
   1234 			error = ENOBUFS;
   1235 			goto out;
   1236 		}
   1237 		MCLAIM(m, &tcp_tx_mowner);
   1238 		m->m_data += max_linkhdr;
   1239 		m->m_len = hdrlen;
   1240 	}
   1241 	m->m_pkthdr.rcvif = (struct ifnet *)0;
   1242 	switch (af) {
   1243 #ifdef INET
   1244 	case AF_INET:
   1245 		ip = mtod(m, struct ip *);
   1246 #ifdef INET6
   1247 		ip6 = NULL;
   1248 #endif
   1249 		th = (struct tcphdr *)(ip + 1);
   1250 		break;
   1251 #endif
   1252 #ifdef INET6
   1253 	case AF_INET6:
   1254 		ip = NULL;
   1255 		ip6 = mtod(m, struct ip6_hdr *);
   1256 		th = (struct tcphdr *)(ip6 + 1);
   1257 		break;
   1258 #endif
   1259 	default:	/*pacify gcc*/
   1260 		ip = NULL;
   1261 #ifdef INET6
   1262 		ip6 = NULL;
   1263 #endif
   1264 		th = NULL;
   1265 		break;
   1266 	}
   1267 	if (tp->t_template == 0)
   1268 		panic("tcp_output");
   1269 	if (tp->t_template->m_len < iphdrlen)
   1270 		panic("tcp_output");
   1271 	bcopy(mtod(tp->t_template, caddr_t), mtod(m, caddr_t), iphdrlen);
   1272 
   1273 	/*
   1274 	 * If we are starting a connection, send ECN setup
   1275 	 * SYN packet. If we are on a retransmit, we may
   1276 	 * resend those bits a number of times as per
   1277 	 * RFC 3168.
   1278 	 */
   1279 	if (tp->t_state == TCPS_SYN_SENT && tcp_do_ecn) {
   1280 		if (tp->t_flags & TF_SYN_REXMT) {
   1281 			if (tp->t_ecn_retries--)
   1282 				flags |= TH_ECE|TH_CWR;
   1283 		} else {
   1284 			flags |= TH_ECE|TH_CWR;
   1285 			tp->t_ecn_retries = tcp_ecn_maxretries;
   1286 		}
   1287 	}
   1288 
   1289 	if (TCP_ECN_ALLOWED(tp)) {
   1290 		/*
   1291 		 * If the peer has ECN, mark data packets
   1292 		 * ECN capable. Ignore pure ack packets, retransmissions
   1293 		 * and window probes.
   1294 		 */
   1295 		if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
   1296 		    !(tp->t_force && len == 1)) {
   1297 			switch (af) {
   1298 #ifdef INET
   1299 			case AF_INET:
   1300 				tp->t_inpcb->inp_ip.ip_tos |= IPTOS_ECN_ECT0;
   1301 				break;
   1302 #endif
   1303 #ifdef INET6
   1304 			case AF_INET6:
   1305 				ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
   1306 				break;
   1307 #endif
   1308 			}
   1309 			tcpstat.tcps_ecn_ect++;
   1310 		}
   1311 
   1312 		/*
   1313 		 * Reply with proper ECN notifications.
   1314 		 */
   1315 		if (tp->t_flags & TF_ECN_SND_CWR) {
   1316 			flags |= TH_CWR;
   1317 			tp->t_flags &= ~TF_ECN_SND_CWR;
   1318 		}
   1319 		if (tp->t_flags & TF_ECN_SND_ECE) {
   1320 			flags |= TH_ECE;
   1321 		}
   1322 	}
   1323 
   1324 
   1325 	/*
   1326 	 * If we are doing retransmissions, then snd_nxt will
   1327 	 * not reflect the first unsent octet.  For ACK only
   1328 	 * packets, we do not want the sequence number of the
   1329 	 * retransmitted packet, we want the sequence number
   1330 	 * of the next unsent octet.  So, if there is no data
   1331 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
   1332 	 * when filling in ti_seq.  But if we are in persist
   1333 	 * state, snd_max might reflect one byte beyond the
   1334 	 * right edge of the window, so use snd_nxt in that
   1335 	 * case, since we know we aren't doing a retransmission.
   1336 	 * (retransmit and persist are mutually exclusive...)
   1337 	 */
   1338 	if (TCP_SACK_ENABLED(tp) && sack_rxmit) {
   1339 		th->th_seq = htonl(p->rxmit);
   1340 		p->rxmit += len;
   1341 	} else {
   1342 		if (len || (flags & (TH_SYN|TH_FIN)) ||
   1343 		    TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
   1344 			th->th_seq = htonl(tp->snd_nxt);
   1345 		else
   1346 			th->th_seq = htonl(tp->snd_max);
   1347 	}
   1348 	th->th_ack = htonl(tp->rcv_nxt);
   1349 	if (optlen) {
   1350 		bcopy((caddr_t)opt, (caddr_t)(th + 1), optlen);
   1351 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
   1352 	}
   1353 	th->th_flags = flags;
   1354 	/*
   1355 	 * Calculate receive window.  Don't shrink window,
   1356 	 * but avoid silly window syndrome.
   1357 	 */
   1358 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
   1359 		win = 0;
   1360 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
   1361 		win = (long)TCP_MAXWIN << tp->rcv_scale;
   1362 	if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt))
   1363 		win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt);
   1364 	th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
   1365 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
   1366 		u_int32_t urp = tp->snd_up - tp->snd_nxt;
   1367 		if (urp > IP_MAXPACKET)
   1368 			urp = IP_MAXPACKET;
   1369 		th->th_urp = htons((u_int16_t)urp);
   1370 		th->th_flags |= TH_URG;
   1371 	} else
   1372 		/*
   1373 		 * If no urgent pointer to send, then we pull
   1374 		 * the urgent pointer to the left edge of the send window
   1375 		 * so that it doesn't drift into the send window on sequence
   1376 		 * number wraparound.
   1377 		 */
   1378 		tp->snd_up = tp->snd_una;		/* drag it along */
   1379 
   1380 #ifdef TCP_SIGNATURE
   1381 	if (sigoff && (tp->t_flags & TF_SIGNATURE)) {
   1382 		struct secasvar *sav;
   1383 		u_int8_t *sigp;
   1384 
   1385 		sav = tcp_signature_getsav(m, th);
   1386 
   1387 		if (sav == NULL) {
   1388 			if (m)
   1389 				m_freem(m);
   1390 			return (EPERM);
   1391 		}
   1392 
   1393 		m->m_pkthdr.len = hdrlen + len;
   1394 		sigp = (caddr_t)th + sizeof(*th) + sigoff;
   1395 		tcp_signature(m, th, (caddr_t)th - mtod(m, caddr_t), sav, sigp);
   1396 
   1397 		key_sa_recordxfer(sav, m);
   1398 #ifdef FAST_IPSEC
   1399 		KEY_FREESAV(&sav);
   1400 #else
   1401 		key_freesav(sav);
   1402 #endif
   1403 	}
   1404 #endif
   1405 
   1406 	/*
   1407 	 * Set ourselves up to be checksummed just before the packet
   1408 	 * hits the wire.
   1409 	 */
   1410 	switch (af) {
   1411 #ifdef INET
   1412 	case AF_INET:
   1413 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
   1414 		if (use_tso) {
   1415 			m->m_pkthdr.segsz = txsegsize;
   1416 			m->m_pkthdr.csum_flags = M_CSUM_TSOv4;
   1417 		} else {
   1418 			m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
   1419 			if (len + optlen) {
   1420 				/* Fixup the pseudo-header checksum. */
   1421 				/* XXXJRT Not IP Jumbogram safe. */
   1422 				th->th_sum = in_cksum_addword(th->th_sum,
   1423 				    htons((u_int16_t) (len + optlen)));
   1424 			}
   1425 		}
   1426 		break;
   1427 #endif
   1428 #ifdef INET6
   1429 	case AF_INET6:
   1430 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
   1431 		if (use_tso) {
   1432 			m->m_pkthdr.segsz = txsegsize;
   1433 			m->m_pkthdr.csum_flags = M_CSUM_TSOv6;
   1434 		} else {
   1435 			m->m_pkthdr.csum_flags = M_CSUM_TCPv6;
   1436 			if (len + optlen) {
   1437 				/* Fixup the pseudo-header checksum. */
   1438 				/* XXXJRT: Not IPv6 Jumbogram safe. */
   1439 				th->th_sum = in_cksum_addword(th->th_sum,
   1440 				    htons((u_int16_t) (len + optlen)));
   1441 			}
   1442 		}
   1443 		break;
   1444 #endif
   1445 	}
   1446 
   1447 	/*
   1448 	 * In transmit state, time the transmission and arrange for
   1449 	 * the retransmit.  In persist state, just set snd_max.
   1450 	 */
   1451 	if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
   1452 		tcp_seq startseq = tp->snd_nxt;
   1453 
   1454 		/*
   1455 		 * Advance snd_nxt over sequence space of this segment.
   1456 		 * There are no states in which we send both a SYN and a FIN,
   1457 		 * so we collapse the tests for these flags.
   1458 		 */
   1459 		if (flags & (TH_SYN|TH_FIN))
   1460 			tp->snd_nxt++;
   1461 		if (sack_rxmit)
   1462 			goto timer;
   1463 		tp->snd_nxt += len;
   1464 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
   1465 			tp->snd_max = tp->snd_nxt;
   1466 			/*
   1467 			 * Time this transmission if not a retransmission and
   1468 			 * not currently timing anything.
   1469 			 */
   1470 			if (tp->t_rtttime == 0) {
   1471 				tp->t_rtttime = tcp_now;
   1472 				tp->t_rtseq = startseq;
   1473 				tcpstat.tcps_segstimed++;
   1474 			}
   1475 		}
   1476 
   1477 		/*
   1478 		 * Set retransmit timer if not currently set,
   1479 		 * and not doing an ack or a keep-alive probe.
   1480 		 * Initial value for retransmit timer is smoothed
   1481 		 * round-trip time + 2 * round-trip time variance.
   1482 		 * Initialize shift counter which is used for backoff
   1483 		 * of retransmit time.
   1484 		 */
   1485 timer:
   1486 		if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
   1487 			((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
   1488 		    tp->snd_nxt != tp->snd_una)) {
   1489 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
   1490 				TCP_TIMER_DISARM(tp, TCPT_PERSIST);
   1491 				tp->t_rxtshift = 0;
   1492 			}
   1493 			TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
   1494 		}
   1495 	} else
   1496 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
   1497 			tp->snd_max = tp->snd_nxt + len;
   1498 
   1499 #ifdef TCP_DEBUG
   1500 	/*
   1501 	 * Trace.
   1502 	 */
   1503 	if (so->so_options & SO_DEBUG)
   1504 		tcp_trace(TA_OUTPUT, tp->t_state, tp, m, 0);
   1505 #endif
   1506 
   1507 	/*
   1508 	 * Fill in IP length and desired time to live and
   1509 	 * send to IP level.  There should be a better way
   1510 	 * to handle ttl and tos; we could keep them in
   1511 	 * the template, but need a way to checksum without them.
   1512 	 */
   1513 	m->m_pkthdr.len = hdrlen + len;
   1514 
   1515 	switch (af) {
   1516 #ifdef INET
   1517 	case AF_INET:
   1518 		ip->ip_len = htons(m->m_pkthdr.len);
   1519 		packetlen = m->m_pkthdr.len;
   1520 		if (tp->t_inpcb) {
   1521 			ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
   1522 			ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos;
   1523 		}
   1524 #ifdef INET6
   1525 		else if (tp->t_in6pcb) {
   1526 			ip->ip_ttl = in6_selecthlim(tp->t_in6pcb, NULL); /*XXX*/
   1527 			ip->ip_tos = 0;	/*XXX*/
   1528 		}
   1529 #endif
   1530 		break;
   1531 #endif
   1532 #ifdef INET6
   1533 	case AF_INET6:
   1534 		packetlen = m->m_pkthdr.len;
   1535 		ip6->ip6_nxt = IPPROTO_TCP;
   1536 		if (tp->t_in6pcb) {
   1537 			/*
   1538 			 * we separately set hoplimit for every segment, since
   1539 			 * the user might want to change the value via
   1540 			 * setsockopt. Also, desired default hop limit might
   1541 			 * be changed via Neighbor Discovery.
   1542 			 */
   1543 			ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb,
   1544 				ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
   1545 		}
   1546 		/* ip6->ip6_flow = ??? */
   1547 		/* ip6_plen will be filled in ip6_output(). */
   1548 		break;
   1549 #endif
   1550 	default:	/*pacify gcc*/
   1551 		packetlen = 0;
   1552 		break;
   1553 	}
   1554 
   1555 	switch (af) {
   1556 #ifdef INET
   1557 	case AF_INET:
   1558 	    {
   1559 		struct mbuf *opts;
   1560 
   1561 		if (tp->t_inpcb)
   1562 			opts = tp->t_inpcb->inp_options;
   1563 		else
   1564 			opts = NULL;
   1565 		error = ip_output(m, opts, ro,
   1566 			(tp->t_mtudisc ? IP_MTUDISC : 0) |
   1567 			(so->so_options & SO_DONTROUTE),
   1568 			(struct ip_moptions *)0, so);
   1569 		break;
   1570 	    }
   1571 #endif
   1572 #ifdef INET6
   1573 	case AF_INET6:
   1574 	    {
   1575 		struct ip6_pktopts *opts;
   1576 
   1577 		if (tp->t_in6pcb)
   1578 			opts = tp->t_in6pcb->in6p_outputopts;
   1579 		else
   1580 			opts = NULL;
   1581 		error = ip6_output(m, opts, (struct route_in6 *)ro,
   1582 			so->so_options & SO_DONTROUTE,
   1583 			(struct ip6_moptions *)0, so, NULL);
   1584 		break;
   1585 	    }
   1586 #endif
   1587 	default:
   1588 		error = EAFNOSUPPORT;
   1589 		break;
   1590 	}
   1591 	if (error) {
   1592 out:
   1593 		if (error == ENOBUFS) {
   1594 			tcpstat.tcps_selfquench++;
   1595 #ifdef INET
   1596 			if (tp->t_inpcb)
   1597 				tcp_quench(tp->t_inpcb, 0);
   1598 #endif
   1599 #ifdef INET6
   1600 			if (tp->t_in6pcb)
   1601 				tcp6_quench(tp->t_in6pcb, 0);
   1602 #endif
   1603 			error = 0;
   1604 		} else if ((error == EHOSTUNREACH || error == ENETDOWN) &&
   1605 		    TCPS_HAVERCVDSYN(tp->t_state)) {
   1606 			tp->t_softerror = error;
   1607 			error = 0;
   1608 		}
   1609 
   1610 		/* Back out the seqence number advance. */
   1611 		if (sack_rxmit)
   1612 			p->rxmit -= len;
   1613 
   1614 		/* Restart the delayed ACK timer, if necessary. */
   1615 		if (tp->t_flags & TF_DELACK)
   1616 			TCP_RESTART_DELACK(tp);
   1617 
   1618 		return (error);
   1619 	}
   1620 
   1621 	if (packetlen > tp->t_pmtud_mtu_sent)
   1622 		tp->t_pmtud_mtu_sent = packetlen;
   1623 
   1624 	tcpstat.tcps_sndtotal++;
   1625 	if (tp->t_flags & TF_DELACK)
   1626 		tcpstat.tcps_delack++;
   1627 
   1628 	/*
   1629 	 * Data sent (as far as we can tell).
   1630 	 * If this advertises a larger window than any other segment,
   1631 	 * then remember the size of the advertised window.
   1632 	 * Any pending ACK has now been sent.
   1633 	 */
   1634 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
   1635 		tp->rcv_adv = tp->rcv_nxt + win;
   1636 	tp->last_ack_sent = tp->rcv_nxt;
   1637 	tp->t_flags &= ~TF_ACKNOW;
   1638 	TCP_CLEAR_DELACK(tp);
   1639 #ifdef DIAGNOSTIC
   1640 	if (maxburst < 0)
   1641 		printf("tcp_output: maxburst exceeded by %d\n", -maxburst);
   1642 #endif
   1643 	if (sendalot && (tp->t_congctl == &tcp_reno_ctl || --maxburst))
   1644 		goto again;
   1645 	return (0);
   1646 }
   1647 
   1648 void
   1649 tcp_setpersist(struct tcpcb *tp)
   1650 {
   1651 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
   1652 	int nticks;
   1653 
   1654 	if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
   1655 		panic("tcp_output REXMT");
   1656 	/*
   1657 	 * Start/restart persistance timer.
   1658 	 */
   1659 	if (t < tp->t_rttmin)
   1660 		t = tp->t_rttmin;
   1661 	TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
   1662 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
   1663 	TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
   1664 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
   1665 		tp->t_rxtshift++;
   1666 }
   1667