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