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