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