xref: /netbsd-src/sys/netinet/tcp_output.c (revision 4d7e773266e3c3f48566c86c0ad52d51c6454fd1)
1 /*	$NetBSD: tcp_output.c,v 1.20 1997/10/18 21:18:32 kml Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1988, 1990, 1993
5  *	The Regents of the University of California.  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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)tcp_output.c	8.3 (Berkeley) 12/30/93
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/errno.h>
46 
47 #include <net/if.h>
48 #include <net/route.h>
49 
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/in_pcb.h>
54 #include <netinet/ip_var.h>
55 #include <netinet/tcp.h>
56 #define	TCPOUTFLAGS
57 #include <netinet/tcp_fsm.h>
58 #include <netinet/tcp_seq.h>
59 #include <netinet/tcp_timer.h>
60 #include <netinet/tcp_var.h>
61 #include <netinet/tcpip.h>
62 #include <netinet/tcp_debug.h>
63 
64 #ifdef TUBA
65 #include <netiso/iso.h>
66 #include <netiso/tuba_table.h>
67 #endif
68 
69 #ifdef notyet
70 extern struct mbuf *m_copypack();
71 #endif
72 
73 #define MAX_TCPOPTLEN	32	/* max # bytes that go in options */
74 
75 static __inline void tcp_segsize __P((struct tcpcb *, int *, int *));
76 static __inline void
77 tcp_segsize(tp, txsegsizep, rxsegsizep)
78 	struct tcpcb *tp;
79 	int *txsegsizep, *rxsegsizep;
80 {
81 	struct inpcb *inp = tp->t_inpcb;
82 	struct rtentry *rt;
83 	struct ifnet *ifp;
84 	int size;
85 
86 	if ((rt = in_pcbrtentry(inp)) == NULL) {
87 		size = tcp_mssdflt;
88 		goto out;
89 	}
90 
91 	ifp = rt->rt_ifp;
92 
93 	if (rt->rt_rmx.rmx_mtu != 0)
94 		size = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
95 	else if (ip_mtudisc || in_localaddr(inp->inp_faddr) ||
96 		 ifp->if_flags & IFF_LOOPBACK)
97 		size = ifp->if_mtu - sizeof(struct tcpiphdr);
98 	else
99 		size = tcp_mssdflt;
100 
101  out:
102 	*txsegsizep = min(tp->t_maxseg, size);
103 	*rxsegsizep = min(tp->t_ourmss, size);
104 }
105 
106 /*
107  * Tcp output routine: figure out what should be sent and send it.
108  */
109 int
110 tcp_output(tp)
111 	register struct tcpcb *tp;
112 {
113 	register struct socket *so = tp->t_inpcb->inp_socket;
114 	register long len, win;
115 	int off, flags, error;
116 	register struct mbuf *m;
117 	register struct tcpiphdr *ti;
118 	u_char opt[MAX_TCPOPTLEN];
119 	unsigned optlen, hdrlen;
120 	int idle, sendalot, txsegsize, rxsegsize;
121 
122 	tcp_segsize(tp, &txsegsize, &rxsegsize);
123 
124 	/*
125 	 * Determine length of data that should be transmitted,
126 	 * and flags that will be used.
127 	 * If there is some data or critical controls (SYN, RST)
128 	 * to send, then transmit; otherwise, investigate further.
129 	 */
130 	idle = (tp->snd_max == tp->snd_una);
131 	if (idle && tp->t_idle >= tp->t_rxtcur)
132 		/*
133 		 * We have been idle for "a while" and no acks are
134 		 * expected to clock out any data we send --
135 		 * slow start to get ack "clock" running again.
136 		 */
137 		tp->snd_cwnd = tp->t_maxseg;
138 again:
139 	sendalot = 0;
140 	off = tp->snd_nxt - tp->snd_una;
141 	win = min(tp->snd_wnd, tp->snd_cwnd);
142 
143 	flags = tcp_outflags[tp->t_state];
144 	/*
145 	 * If in persist timeout with window of 0, send 1 byte.
146 	 * Otherwise, if window is small but nonzero
147 	 * and timer expired, we will send what we can
148 	 * and go to transmit state.
149 	 */
150 	if (tp->t_force) {
151 		if (win == 0) {
152 			/*
153 			 * If we still have some data to send, then
154 			 * clear the FIN bit.  Usually this would
155 			 * happen below when it realizes that we
156 			 * aren't sending all the data.  However,
157 			 * if we have exactly 1 byte of unset data,
158 			 * then it won't clear the FIN bit below,
159 			 * and if we are in persist state, we wind
160 			 * up sending the packet without recording
161 			 * that we sent the FIN bit.
162 			 *
163 			 * We can't just blindly clear the FIN bit,
164 			 * because if we don't have any more data
165 			 * to send then the probe will be the FIN
166 			 * itself.
167 			 */
168 			if (off < so->so_snd.sb_cc)
169 				flags &= ~TH_FIN;
170 			win = 1;
171 		} else {
172 			tp->t_timer[TCPT_PERSIST] = 0;
173 			tp->t_rxtshift = 0;
174 		}
175 	}
176 
177 	if (win < so->so_snd.sb_cc) {
178 		len = win - off;
179 		flags &= ~TH_FIN;
180 	} else
181 		len = so->so_snd.sb_cc - off;
182 
183 	if (len < 0) {
184 		/*
185 		 * If FIN has been sent but not acked,
186 		 * but we haven't been called to retransmit,
187 		 * len will be -1.  Otherwise, window shrank
188 		 * after we sent into it.  If window shrank to 0,
189 		 * cancel pending retransmit and pull snd_nxt
190 		 * back to (closed) window.  We will enter persist
191 		 * state below.  If the window didn't close completely,
192 		 * just wait for an ACK.
193 		 */
194 		len = 0;
195 		if (win == 0) {
196 			tp->t_timer[TCPT_REXMT] = 0;
197 			tp->snd_nxt = tp->snd_una;
198 		}
199 	}
200 	if (len > txsegsize) {
201 		len = txsegsize;
202 		flags &= ~TH_FIN;
203 		sendalot = 1;
204 	}
205 
206 	win = sbspace(&so->so_rcv);
207 
208 	/*
209 	 * Sender silly window avoidance.  If connection is idle
210 	 * and can send all data, a maximum segment,
211 	 * at least a maximum default-size segment do it,
212 	 * or are forced, do it; otherwise don't bother.
213 	 * If peer's buffer is tiny, then send
214 	 * when window is at least half open.
215 	 * If retransmitting (possibly after persist timer forced us
216 	 * to send into a small window), then must resend.
217 	 */
218 	if (len) {
219 		if (len == txsegsize)
220 			goto send;
221 		if ((idle || tp->t_flags & TF_NODELAY) &&
222 		    len + off >= so->so_snd.sb_cc)
223 			goto send;
224 		if (tp->t_force)
225 			goto send;
226 		if (len >= tp->max_sndwnd / 2)
227 			goto send;
228 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
229 			goto send;
230 	}
231 
232 	/*
233 	 * Compare available window to amount of window known to peer
234 	 * (as advertised window less next expected input).  If the
235 	 * difference is at least twice the size of the largest segment
236 	 * we expect to receive (i.e. two segments) or at least 50% of
237 	 * the maximum possible window, then want to send a window update
238 	 * to peer.
239 	 */
240 	if (win > 0) {
241 		/*
242 		 * "adv" is the amount we can increase the window,
243 		 * taking into account that we are limited by
244 		 * TCP_MAXWIN << tp->rcv_scale.
245 		 */
246 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
247 			(tp->rcv_adv - tp->rcv_nxt);
248 
249 		if (adv >= (long) (2 * rxsegsize))
250 			goto send;
251 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
252 			goto send;
253 	}
254 
255 	/*
256 	 * Send if we owe peer an ACK.
257 	 */
258 	if (tp->t_flags & TF_ACKNOW)
259 		goto send;
260 	if (flags & (TH_SYN|TH_RST))
261 		goto send;
262 	if (SEQ_GT(tp->snd_up, tp->snd_una))
263 		goto send;
264 	/*
265 	 * If our state indicates that FIN should be sent
266 	 * and we have not yet done so, or we're retransmitting the FIN,
267 	 * then we need to send.
268 	 */
269 	if (flags & TH_FIN &&
270 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
271 		goto send;
272 
273 	/*
274 	 * TCP window updates are not reliable, rather a polling protocol
275 	 * using ``persist'' packets is used to insure receipt of window
276 	 * updates.  The three ``states'' for the output side are:
277 	 *	idle			not doing retransmits or persists
278 	 *	persisting		to move a small or zero window
279 	 *	(re)transmitting	and thereby not persisting
280 	 *
281 	 * tp->t_timer[TCPT_PERSIST]
282 	 *	is set when we are in persist state.
283 	 * tp->t_force
284 	 *	is set when we are called to send a persist packet.
285 	 * tp->t_timer[TCPT_REXMT]
286 	 *	is set when we are retransmitting
287 	 * The output side is idle when both timers are zero.
288 	 *
289 	 * If send window is too small, there is data to transmit, and no
290 	 * retransmit or persist is pending, then go to persist state.
291 	 * If nothing happens soon, send when timer expires:
292 	 * if window is nonzero, transmit what we can,
293 	 * otherwise force out a byte.
294 	 */
295 	if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
296 	    tp->t_timer[TCPT_PERSIST] == 0) {
297 		tp->t_rxtshift = 0;
298 		tcp_setpersist(tp);
299 	}
300 
301 	/*
302 	 * No reason to send a segment, just return.
303 	 */
304 	return (0);
305 
306 send:
307 	/*
308 	 * Before ESTABLISHED, force sending of initial options
309 	 * unless TCP set not to do any options.
310 	 * NOTE: we assume that the IP/TCP header plus TCP options
311 	 * always fit in a single mbuf, leaving room for a maximum
312 	 * link header, i.e.
313 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
314 	 */
315 	optlen = 0;
316 	hdrlen = sizeof (struct tcpiphdr);
317 	if (flags & TH_SYN) {
318 		tp->snd_nxt = tp->iss;
319 		tp->t_ourmss = tcp_mss_to_advertise(tp);
320 		if ((tp->t_flags & TF_NOOPT) == 0) {
321 			opt[0] = TCPOPT_MAXSEG;
322 			opt[1] = 4;
323 			opt[2] = (tp->t_ourmss >> 8) & 0xff;
324 			opt[3] = tp->t_ourmss & 0xff;
325 			optlen = 4;
326 
327 			if ((tp->t_flags & TF_REQ_SCALE) &&
328 			    ((flags & TH_ACK) == 0 ||
329 			    (tp->t_flags & TF_RCVD_SCALE))) {
330 				*((u_int32_t *) (opt + optlen)) = htonl(
331 					TCPOPT_NOP << 24 |
332 					TCPOPT_WINDOW << 16 |
333 					TCPOLEN_WINDOW << 8 |
334 					tp->request_r_scale);
335 				optlen += 4;
336 			}
337 		}
338  	}
339 
340  	/*
341 	 * Send a timestamp and echo-reply if this is a SYN and our side
342 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
343 	 * and our peer have sent timestamps in our SYN's.
344  	 */
345  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
346  	     (flags & TH_RST) == 0 &&
347  	    ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
348 	     (tp->t_flags & TF_RCVD_TSTMP))) {
349 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
350 
351  		/* Form timestamp option as shown in appendix A of RFC 1323. */
352  		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
353  		*lp++ = htonl(tcp_now);
354  		*lp   = htonl(tp->ts_recent);
355  		optlen += TCPOLEN_TSTAMP_APPA;
356  	}
357 
358  	hdrlen += optlen;
359 
360 	/*
361 	 * Adjust data length if insertion of options will
362 	 * bump the packet length beyond the txsegsize length.
363 	 */
364 	 if (len > txsegsize - optlen) {
365 		len = txsegsize - optlen;
366 		flags &= ~TH_FIN;
367 		sendalot = 1;
368 	 }
369 
370 #ifdef DIAGNOSTIC
371  	if (max_linkhdr + hdrlen > MHLEN)
372 		panic("tcphdr too big");
373 #endif
374 
375 	/*
376 	 * Grab a header mbuf, attaching a copy of data to
377 	 * be transmitted, and initialize the header from
378 	 * the template for sends on this connection.
379 	 */
380 	if (len) {
381 		if (tp->t_force && len == 1)
382 			tcpstat.tcps_sndprobe++;
383 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
384 			tcpstat.tcps_sndrexmitpack++;
385 			tcpstat.tcps_sndrexmitbyte += len;
386 		} else {
387 			tcpstat.tcps_sndpack++;
388 			tcpstat.tcps_sndbyte += len;
389 		}
390 #ifdef notyet
391 		if ((m = m_copypack(so->so_snd.sb_mb, off,
392 		    (int)len, max_linkhdr + hdrlen)) == 0) {
393 			error = ENOBUFS;
394 			goto out;
395 		}
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 (m == NULL) {
404 			error = ENOBUFS;
405 			goto out;
406 		}
407 		m->m_data += max_linkhdr;
408 		m->m_len = hdrlen;
409 		if (len <= MHLEN - hdrlen - max_linkhdr) {
410 			m_copydata(so->so_snd.sb_mb, off, (int) len,
411 			    mtod(m, caddr_t) + hdrlen);
412 			m->m_len += len;
413 		} else {
414 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
415 			if (m->m_next == 0)
416 				len = 0;
417 		}
418 #endif
419 		/*
420 		 * If we're sending everything we've got, set PUSH.
421 		 * (This will keep happy those implementations which only
422 		 * give data to the user when a buffer fills or
423 		 * a PUSH comes in.)
424 		 */
425 		if (off + len == so->so_snd.sb_cc)
426 			flags |= TH_PUSH;
427 	} else {
428 		if (tp->t_flags & TF_ACKNOW)
429 			tcpstat.tcps_sndacks++;
430 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
431 			tcpstat.tcps_sndctrl++;
432 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
433 			tcpstat.tcps_sndurg++;
434 		else
435 			tcpstat.tcps_sndwinup++;
436 
437 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
438 		if (m == NULL) {
439 			error = ENOBUFS;
440 			goto out;
441 		}
442 		m->m_data += max_linkhdr;
443 		m->m_len = hdrlen;
444 	}
445 	m->m_pkthdr.rcvif = (struct ifnet *)0;
446 	ti = mtod(m, struct tcpiphdr *);
447 	if (tp->t_template == 0)
448 		panic("tcp_output");
449 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
450 
451 	/*
452 	 * Fill in fields, remembering maximum advertised
453 	 * window for use in delaying messages about window sizes.
454 	 * If resending a FIN, be sure not to use a new sequence number.
455 	 */
456 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
457 	    tp->snd_nxt == tp->snd_max)
458 		tp->snd_nxt--;
459 	/*
460 	 * If we are doing retransmissions, then snd_nxt will
461 	 * not reflect the first unsent octet.  For ACK only
462 	 * packets, we do not want the sequence number of the
463 	 * retransmitted packet, we want the sequence number
464 	 * of the next unsent octet.  So, if there is no data
465 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
466 	 * when filling in ti_seq.  But if we are in persist
467 	 * state, snd_max might reflect one byte beyond the
468 	 * right edge of the window, so use snd_nxt in that
469 	 * case, since we know we aren't doing a retransmission.
470 	 * (retransmit and persist are mutually exclusive...)
471 	 */
472 	if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
473 		ti->ti_seq = htonl(tp->snd_nxt);
474 	else
475 		ti->ti_seq = htonl(tp->snd_max);
476 	ti->ti_ack = htonl(tp->rcv_nxt);
477 	if (optlen) {
478 		bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
479 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
480 	}
481 	ti->ti_flags = flags;
482 	/*
483 	 * Calculate receive window.  Don't shrink window,
484 	 * but avoid silly window syndrome.
485 	 */
486 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
487 		win = 0;
488 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
489 		win = (long)TCP_MAXWIN << tp->rcv_scale;
490 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
491 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
492 	ti->ti_win = htons((u_int16_t) (win>>tp->rcv_scale));
493 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
494 		u_int32_t urp = tp->snd_up - tp->snd_nxt;
495 		if (urp > IP_MAXPACKET)
496 			urp = IP_MAXPACKET;
497 		ti->ti_urp = htons((u_int16_t)urp);
498 		ti->ti_flags |= TH_URG;
499 	} else
500 		/*
501 		 * If no urgent pointer to send, then we pull
502 		 * the urgent pointer to the left edge of the send window
503 		 * so that it doesn't drift into the send window on sequence
504 		 * number wraparound.
505 		 */
506 		tp->snd_up = tp->snd_una;		/* drag it along */
507 
508 	/*
509 	 * Put TCP length in extended header, and then
510 	 * checksum extended header and data.
511 	 */
512 	if (len + optlen)
513 		ti->ti_len = htons((u_int16_t)(sizeof (struct tcphdr) +
514 		    optlen + len));
515 	ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
516 
517 	/*
518 	 * In transmit state, time the transmission and arrange for
519 	 * the retransmit.  In persist state, just set snd_max.
520 	 */
521 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
522 		tcp_seq startseq = tp->snd_nxt;
523 
524 		/*
525 		 * Advance snd_nxt over sequence space of this segment.
526 		 */
527 		if (flags & (TH_SYN|TH_FIN)) {
528 			if (flags & TH_SYN)
529 				tp->snd_nxt++;
530 			if (flags & TH_FIN) {
531 				tp->snd_nxt++;
532 				tp->t_flags |= TF_SENTFIN;
533 			}
534 		}
535 		tp->snd_nxt += len;
536 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
537 			tp->snd_max = tp->snd_nxt;
538 			/*
539 			 * Time this transmission if not a retransmission and
540 			 * not currently timing anything.
541 			 */
542 			if (tp->t_rtt == 0) {
543 				tp->t_rtt = 1;
544 				tp->t_rtseq = startseq;
545 				tcpstat.tcps_segstimed++;
546 			}
547 		}
548 
549 		/*
550 		 * Set retransmit timer if not currently set,
551 		 * and not doing an ack or a keep-alive probe.
552 		 * Initial value for retransmit timer is smoothed
553 		 * round-trip time + 2 * round-trip time variance.
554 		 * Initialize shift counter which is used for backoff
555 		 * of retransmit time.
556 		 */
557 		if (tp->t_timer[TCPT_REXMT] == 0 &&
558 		    tp->snd_nxt != tp->snd_una) {
559 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
560 			if (tp->t_timer[TCPT_PERSIST]) {
561 				tp->t_timer[TCPT_PERSIST] = 0;
562 				tp->t_rxtshift = 0;
563 			}
564 		}
565 	} else
566 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
567 			tp->snd_max = tp->snd_nxt + len;
568 
569 	/*
570 	 * Trace.
571 	 */
572 	if (so->so_options & SO_DEBUG)
573 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
574 
575 	/*
576 	 * Fill in IP length and desired time to live and
577 	 * send to IP level.  There should be a better way
578 	 * to handle ttl and tos; we could keep them in
579 	 * the template, but need a way to checksum without them.
580 	 */
581 	m->m_pkthdr.len = hdrlen + len;
582 #ifdef TUBA
583 	if (tp->t_tuba_pcb)
584 		error = tuba_output(m, tp);
585 	else
586 #endif
587     {
588 	struct rtentry *rt;
589 
590 	((struct ip *)ti)->ip_len = m->m_pkthdr.len;
591 	((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;	/* XXX */
592 	((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos;	/* XXX */
593 
594 	if (ip_mtudisc && (rt = in_pcbrtentry(tp->t_inpcb)) != 0 &&
595 	    (rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
596 		((struct ip *)ti)->ip_off |= IP_DF;
597 
598 #if BSD >= 43
599 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
600 	    so->so_options & SO_DONTROUTE, 0);
601 #else
602 	error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
603 	    so->so_options & SO_DONTROUTE);
604 #endif
605     }
606 	if (error) {
607 out:
608 		if (error == ENOBUFS) {
609 			tcp_quench(tp->t_inpcb, 0);
610 			return (0);
611 		}
612 		if ((error == EHOSTUNREACH || error == ENETDOWN)
613 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
614 			tp->t_softerror = error;
615 			return (0);
616 		}
617 		return (error);
618 	}
619 	tcpstat.tcps_sndtotal++;
620 
621 	/*
622 	 * Data sent (as far as we can tell).
623 	 * If this advertises a larger window than any other segment,
624 	 * then remember the size of the advertised window.
625 	 * Any pending ACK has now been sent.
626 	 */
627 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
628 		tp->rcv_adv = tp->rcv_nxt + win;
629 	tp->last_ack_sent = tp->rcv_nxt;
630 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
631 	if (sendalot)
632 		goto again;
633 	return (0);
634 }
635 
636 void
637 tcp_setpersist(tp)
638 	register struct tcpcb *tp;
639 {
640 	register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
641 
642 	if (tp->t_timer[TCPT_REXMT])
643 		panic("tcp_output REXMT");
644 	/*
645 	 * Start/restart persistance timer.
646 	 */
647 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
648 	    t * tcp_backoff[tp->t_rxtshift],
649 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
650 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
651 		tp->t_rxtshift++;
652 }
653