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