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