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