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