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