tcp_output.c revision 1.36 1 /* $NetBSD: tcp_output.c,v 1.36 1998/04/30 18:27:20 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 /*
113 * Knob to enable Congestion Window Monitoring, and control the
114 * the burst size it allows. Default burst is 4 packets, per
115 * the Internet draft.
116 */
117 int tcp_cwm = 0;
118 int tcp_cwm_burstsize = 4;
119
120 static __inline void tcp_segsize __P((struct tcpcb *, int *, int *));
121 static __inline void
122 tcp_segsize(tp, txsegsizep, rxsegsizep)
123 struct tcpcb *tp;
124 int *txsegsizep, *rxsegsizep;
125 {
126 struct inpcb *inp = tp->t_inpcb;
127 struct rtentry *rt;
128 struct ifnet *ifp;
129 int size;
130
131 if ((rt = in_pcbrtentry(inp)) == NULL) {
132 size = tcp_mssdflt;
133 goto out;
134 }
135
136 ifp = rt->rt_ifp;
137
138 if (rt->rt_rmx.rmx_mtu != 0)
139 size = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
140 else if (ip_mtudisc || in_localaddr(inp->inp_faddr) ||
141 ifp->if_flags & IFF_LOOPBACK)
142 size = ifp->if_mtu - sizeof(struct tcpiphdr);
143 else
144 size = tcp_mssdflt;
145 size -= (tcp_optlen(tp) + ip_optlen(tp->t_inpcb));
146
147 out:
148 *txsegsizep = min(tp->t_peermss, size);
149 *rxsegsizep = min(tp->t_ourmss, size);
150
151 if (*txsegsizep != tp->t_segsz) {
152 /*
153 * If the new segment size is larger, we don't want to
154 * mess up the congestion window, but if it is smaller
155 * we'll have to reduce the congestion window to ensure
156 * that we don't get into trouble with initial windows
157 * and the rest. In any case, if the segment size
158 * has changed, chances are the path has, too, and
159 * our congestion window will be different.
160 */
161 if (*txsegsizep < tp->t_segsz) {
162 tp->snd_cwnd = max((tp->snd_cwnd / tp->t_segsz)
163 * *txsegsizep, *txsegsizep);
164 tp->snd_ssthresh = max((tp->snd_ssthresh / tp->t_segsz)
165 * *txsegsizep, *txsegsizep);
166 }
167 tp->t_segsz = *txsegsizep;
168 }
169 }
170
171 /*
172 * Tcp output routine: figure out what should be sent and send it.
173 */
174 int
175 tcp_output(tp)
176 register struct tcpcb *tp;
177 {
178 register struct socket *so = tp->t_inpcb->inp_socket;
179 register long len, win;
180 int off, flags, error;
181 register struct mbuf *m;
182 register struct tcpiphdr *ti;
183 u_char opt[MAX_TCPOPTLEN];
184 unsigned optlen, hdrlen;
185 int idle, sendalot, txsegsize, rxsegsize;
186
187 tcp_segsize(tp, &txsegsize, &rxsegsize);
188
189 idle = (tp->snd_max == tp->snd_una);
190
191 if (tcp_cwm) {
192 /*
193 * Hughes/Touch/Heidemann Congestion Window Monitoring.
194 * Count the number of packets currently pending
195 * acknowledgement, and limit our congestion window
196 * to the initial congestion window plus that count.
197 * This prevents bursting once all pending packets have
198 * been acknowledged (i.e. transmission is idle).
199 */
200 tp->snd_cwnd = min(tp->snd_cwnd,
201 (tcp_cwm_burstsize * txsegsize) +
202 (tp->snd_nxt - tp->snd_una));
203 } else {
204 if (idle && tp->t_idle >= tp->t_rxtcur) {
205 /*
206 * We have been idle for "a while" and no acks are
207 * expected to clock out any data we send --
208 * slow start to get ack "clock" running again.
209 */
210 tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win,
211 txsegsize);
212 }
213 }
214
215 again:
216 /*
217 * Determine length of data that should be transmitted, and
218 * flags that should be used. If there is some data or critical
219 * controls (SYN, RST) to send, then transmit; otherwise,
220 * investigate further.
221 */
222 sendalot = 0;
223 off = tp->snd_nxt - tp->snd_una;
224 win = min(tp->snd_wnd, tp->snd_cwnd);
225
226 flags = tcp_outflags[tp->t_state];
227 /*
228 * If in persist timeout with window of 0, send 1 byte.
229 * Otherwise, if window is small but nonzero
230 * and timer expired, we will send what we can
231 * and go to transmit state.
232 */
233 if (tp->t_force) {
234 if (win == 0) {
235 /*
236 * If we still have some data to send, then
237 * clear the FIN bit. Usually this would
238 * happen below when it realizes that we
239 * aren't sending all the data. However,
240 * if we have exactly 1 byte of unset data,
241 * then it won't clear the FIN bit below,
242 * and if we are in persist state, we wind
243 * up sending the packet without recording
244 * that we sent the FIN bit.
245 *
246 * We can't just blindly clear the FIN bit,
247 * because if we don't have any more data
248 * to send then the probe will be the FIN
249 * itself.
250 */
251 if (off < so->so_snd.sb_cc)
252 flags &= ~TH_FIN;
253 win = 1;
254 } else {
255 tp->t_timer[TCPT_PERSIST] = 0;
256 tp->t_rxtshift = 0;
257 }
258 }
259
260 if (win < so->so_snd.sb_cc) {
261 len = win - off;
262 flags &= ~TH_FIN;
263 } else
264 len = so->so_snd.sb_cc - off;
265
266 if (len < 0) {
267 /*
268 * If FIN has been sent but not acked,
269 * but we haven't been called to retransmit,
270 * len will be -1. Otherwise, window shrank
271 * after we sent into it. If window shrank to 0,
272 * cancel pending retransmit, pull snd_nxt back
273 * to (closed) window, and set the persist timer
274 * if it isn't already going. If the window didn't
275 * close completely, just wait for an ACK.
276 */
277 len = 0;
278 if (win == 0) {
279 tp->t_timer[TCPT_REXMT] = 0;
280 tp->t_rxtshift = 0;
281 tp->snd_nxt = tp->snd_una;
282 if (tp->t_timer[TCPT_PERSIST] == 0)
283 tcp_setpersist(tp);
284 }
285 }
286 if (len > txsegsize) {
287 len = txsegsize;
288 flags &= ~TH_FIN;
289 sendalot = 1;
290 }
291
292 win = sbspace(&so->so_rcv);
293
294 /*
295 * Sender silly window avoidance. If connection is idle
296 * and can send all data, a maximum segment,
297 * at least a maximum default-size segment do it,
298 * or are forced, do it; otherwise don't bother.
299 * If peer's buffer is tiny, then send
300 * when window is at least half open.
301 * If retransmitting (possibly after persist timer forced us
302 * to send into a small window), then must resend.
303 */
304 if (len) {
305 if (len == txsegsize)
306 goto send;
307 if ((idle || tp->t_flags & TF_NODELAY) &&
308 len + off >= so->so_snd.sb_cc)
309 goto send;
310 if (tp->t_force)
311 goto send;
312 if (len >= tp->max_sndwnd / 2)
313 goto send;
314 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
315 goto send;
316 }
317
318 /*
319 * Compare available window to amount of window known to peer
320 * (as advertised window less next expected input). If the
321 * difference is at least twice the size of the largest segment
322 * we expect to receive (i.e. two segments) or at least 50% of
323 * the maximum possible window, then want to send a window update
324 * to peer.
325 */
326 if (win > 0) {
327 /*
328 * "adv" is the amount we can increase the window,
329 * taking into account that we are limited by
330 * TCP_MAXWIN << tp->rcv_scale.
331 */
332 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
333 (tp->rcv_adv - tp->rcv_nxt);
334
335 if (adv >= (long) (2 * rxsegsize))
336 goto send;
337 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
338 goto send;
339 }
340
341 /*
342 * Send if we owe peer an ACK.
343 */
344 if (tp->t_flags & TF_ACKNOW)
345 goto send;
346 if (flags & (TH_SYN|TH_RST))
347 goto send;
348 if (SEQ_GT(tp->snd_up, tp->snd_una))
349 goto send;
350 /*
351 * If our state indicates that FIN should be sent
352 * and we have not yet done so, or we're retransmitting the FIN,
353 * then we need to send.
354 */
355 if (flags & TH_FIN &&
356 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
357 goto send;
358
359 /*
360 * TCP window updates are not reliable, rather a polling protocol
361 * using ``persist'' packets is used to insure receipt of window
362 * updates. The three ``states'' for the output side are:
363 * idle not doing retransmits or persists
364 * persisting to move a small or zero window
365 * (re)transmitting and thereby not persisting
366 *
367 * tp->t_timer[TCPT_PERSIST]
368 * is set when we are in persist state.
369 * tp->t_force
370 * is set when we are called to send a persist packet.
371 * tp->t_timer[TCPT_REXMT]
372 * is set when we are retransmitting
373 * The output side is idle when both timers are zero.
374 *
375 * If send window is too small, there is data to transmit, and no
376 * retransmit or persist is pending, then go to persist state.
377 * If nothing happens soon, send when timer expires:
378 * if window is nonzero, transmit what we can,
379 * otherwise force out a byte.
380 */
381 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
382 tp->t_timer[TCPT_PERSIST] == 0) {
383 tp->t_rxtshift = 0;
384 tcp_setpersist(tp);
385 }
386
387 /*
388 * No reason to send a segment, just return.
389 */
390 return (0);
391
392 send:
393 /*
394 * Before ESTABLISHED, force sending of initial options
395 * unless TCP set not to do any options.
396 * NOTE: we assume that the IP/TCP header plus TCP options
397 * always fit in a single mbuf, leaving room for a maximum
398 * link header, i.e.
399 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
400 */
401 optlen = 0;
402 hdrlen = sizeof (struct tcpiphdr);
403 if (flags & TH_SYN) {
404 struct rtentry *rt = in_pcbrtentry(tp->t_inpcb);
405
406 tp->snd_nxt = tp->iss;
407 tp->t_ourmss = tcp_mss_to_advertise(rt != NULL ?
408 rt->rt_ifp : NULL);
409 if ((tp->t_flags & TF_NOOPT) == 0) {
410 opt[0] = TCPOPT_MAXSEG;
411 opt[1] = 4;
412 opt[2] = (tp->t_ourmss >> 8) & 0xff;
413 opt[3] = tp->t_ourmss & 0xff;
414 optlen = 4;
415
416 if ((tp->t_flags & TF_REQ_SCALE) &&
417 ((flags & TH_ACK) == 0 ||
418 (tp->t_flags & TF_RCVD_SCALE))) {
419 *((u_int32_t *) (opt + optlen)) = htonl(
420 TCPOPT_NOP << 24 |
421 TCPOPT_WINDOW << 16 |
422 TCPOLEN_WINDOW << 8 |
423 tp->request_r_scale);
424 optlen += 4;
425 }
426 }
427 }
428
429 /*
430 * Send a timestamp and echo-reply if this is a SYN and our side
431 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
432 * and our peer have sent timestamps in our SYN's.
433 */
434 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
435 (flags & TH_RST) == 0 &&
436 ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
437 (tp->t_flags & TF_RCVD_TSTMP))) {
438 u_int32_t *lp = (u_int32_t *)(opt + optlen);
439
440 /* Form timestamp option as shown in appendix A of RFC 1323. */
441 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
442 *lp++ = htonl(tcp_now);
443 *lp = htonl(tp->ts_recent);
444 optlen += TCPOLEN_TSTAMP_APPA;
445 }
446
447 hdrlen += optlen;
448
449 #ifdef DIAGNOSTIC
450 if (len > txsegsize)
451 panic("tcp data to be sent is larger than segment");
452 if (max_linkhdr + hdrlen > MHLEN)
453 panic("tcphdr too big");
454 #endif
455
456 /*
457 * Grab a header mbuf, attaching a copy of data to
458 * be transmitted, and initialize the header from
459 * the template for sends on this connection.
460 */
461 if (len) {
462 if (tp->t_force && len == 1)
463 tcpstat.tcps_sndprobe++;
464 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
465 tcpstat.tcps_sndrexmitpack++;
466 tcpstat.tcps_sndrexmitbyte += len;
467 } else {
468 tcpstat.tcps_sndpack++;
469 tcpstat.tcps_sndbyte += len;
470 }
471 #ifdef notyet
472 if ((m = m_copypack(so->so_snd.sb_mb, off,
473 (int)len, max_linkhdr + hdrlen)) == 0) {
474 error = ENOBUFS;
475 goto out;
476 }
477 /*
478 * m_copypack left space for our hdr; use it.
479 */
480 m->m_len += hdrlen;
481 m->m_data -= hdrlen;
482 #else
483 MGETHDR(m, M_DONTWAIT, MT_HEADER);
484 if (m == NULL) {
485 error = ENOBUFS;
486 goto out;
487 }
488 m->m_data += max_linkhdr;
489 m->m_len = hdrlen;
490 if (len <= MHLEN - hdrlen - max_linkhdr) {
491 m_copydata(so->so_snd.sb_mb, off, (int) len,
492 mtod(m, caddr_t) + hdrlen);
493 m->m_len += len;
494 } else {
495 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
496 if (m->m_next == 0) {
497 (void) m_freem(m);
498 error = ENOBUFS;
499 goto out;
500 }
501 }
502 #endif
503 /*
504 * If we're sending everything we've got, set PUSH.
505 * (This will keep happy those implementations which only
506 * give data to the user when a buffer fills or
507 * a PUSH comes in.)
508 */
509 if (off + len == so->so_snd.sb_cc)
510 flags |= TH_PUSH;
511 } else {
512 if (tp->t_flags & TF_ACKNOW)
513 tcpstat.tcps_sndacks++;
514 else if (flags & (TH_SYN|TH_FIN|TH_RST))
515 tcpstat.tcps_sndctrl++;
516 else if (SEQ_GT(tp->snd_up, tp->snd_una))
517 tcpstat.tcps_sndurg++;
518 else
519 tcpstat.tcps_sndwinup++;
520
521 MGETHDR(m, M_DONTWAIT, MT_HEADER);
522 if (m == NULL) {
523 error = ENOBUFS;
524 goto out;
525 }
526 m->m_data += max_linkhdr;
527 m->m_len = hdrlen;
528 }
529 m->m_pkthdr.rcvif = (struct ifnet *)0;
530 ti = mtod(m, struct tcpiphdr *);
531 if (tp->t_template == 0)
532 panic("tcp_output");
533 bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
534
535 /*
536 * Fill in fields, remembering maximum advertised
537 * window for use in delaying messages about window sizes.
538 * If resending a FIN, be sure not to use a new sequence number.
539 */
540 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
541 tp->snd_nxt == tp->snd_max)
542 tp->snd_nxt--;
543 /*
544 * If we are doing retransmissions, then snd_nxt will
545 * not reflect the first unsent octet. For ACK only
546 * packets, we do not want the sequence number of the
547 * retransmitted packet, we want the sequence number
548 * of the next unsent octet. So, if there is no data
549 * (and no SYN or FIN), use snd_max instead of snd_nxt
550 * when filling in ti_seq. But if we are in persist
551 * state, snd_max might reflect one byte beyond the
552 * right edge of the window, so use snd_nxt in that
553 * case, since we know we aren't doing a retransmission.
554 * (retransmit and persist are mutually exclusive...)
555 */
556 if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
557 ti->ti_seq = htonl(tp->snd_nxt);
558 else
559 ti->ti_seq = htonl(tp->snd_max);
560 ti->ti_ack = htonl(tp->rcv_nxt);
561 if (optlen) {
562 bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
563 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
564 }
565 ti->ti_flags = flags;
566 /*
567 * Calculate receive window. Don't shrink window,
568 * but avoid silly window syndrome.
569 */
570 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
571 win = 0;
572 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
573 win = (long)TCP_MAXWIN << tp->rcv_scale;
574 if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
575 win = (long)(tp->rcv_adv - tp->rcv_nxt);
576 ti->ti_win = htons((u_int16_t) (win>>tp->rcv_scale));
577 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
578 u_int32_t urp = tp->snd_up - tp->snd_nxt;
579 if (urp > IP_MAXPACKET)
580 urp = IP_MAXPACKET;
581 ti->ti_urp = htons((u_int16_t)urp);
582 ti->ti_flags |= TH_URG;
583 } else
584 /*
585 * If no urgent pointer to send, then we pull
586 * the urgent pointer to the left edge of the send window
587 * so that it doesn't drift into the send window on sequence
588 * number wraparound.
589 */
590 tp->snd_up = tp->snd_una; /* drag it along */
591
592 /*
593 * Put TCP length in extended header, and then
594 * checksum extended header and data.
595 */
596 if (len + optlen)
597 ti->ti_len = htons((u_int16_t)(sizeof (struct tcphdr) +
598 optlen + len));
599 ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
600
601 /*
602 * In transmit state, time the transmission and arrange for
603 * the retransmit. In persist state, just set snd_max.
604 */
605 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
606 tcp_seq startseq = tp->snd_nxt;
607
608 /*
609 * Advance snd_nxt over sequence space of this segment.
610 */
611 if (flags & (TH_SYN|TH_FIN)) {
612 if (flags & TH_SYN)
613 tp->snd_nxt++;
614 if (flags & TH_FIN) {
615 tp->snd_nxt++;
616 tp->t_flags |= TF_SENTFIN;
617 }
618 }
619 tp->snd_nxt += len;
620 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
621 tp->snd_max = tp->snd_nxt;
622 /*
623 * Time this transmission if not a retransmission and
624 * not currently timing anything.
625 */
626 if (tp->t_rtt == 0) {
627 tp->t_rtt = 1;
628 tp->t_rtseq = startseq;
629 tcpstat.tcps_segstimed++;
630 }
631 }
632
633 /*
634 * Set retransmit timer if not currently set,
635 * and not doing an ack or a keep-alive probe.
636 * Initial value for retransmit timer is smoothed
637 * round-trip time + 2 * round-trip time variance.
638 * Initialize shift counter which is used for backoff
639 * of retransmit time.
640 */
641 if (tp->t_timer[TCPT_REXMT] == 0 &&
642 tp->snd_nxt != tp->snd_una) {
643 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
644 if (tp->t_timer[TCPT_PERSIST]) {
645 tp->t_timer[TCPT_PERSIST] = 0;
646 tp->t_rxtshift = 0;
647 }
648 }
649 } else
650 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
651 tp->snd_max = tp->snd_nxt + len;
652
653 /*
654 * Trace.
655 */
656 if (so->so_options & SO_DEBUG)
657 tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
658
659 /*
660 * Fill in IP length and desired time to live and
661 * send to IP level. There should be a better way
662 * to handle ttl and tos; we could keep them in
663 * the template, but need a way to checksum without them.
664 */
665 m->m_pkthdr.len = hdrlen + len;
666 #ifdef TUBA
667 if (tp->t_tuba_pcb)
668 error = tuba_output(m, tp);
669 else
670 #endif
671 {
672 struct rtentry *rt;
673
674 ((struct ip *)ti)->ip_len = m->m_pkthdr.len;
675 ((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; /* XXX */
676 ((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos; /* XXX */
677
678 if (ip_mtudisc && (rt = in_pcbrtentry(tp->t_inpcb)) != 0 &&
679 (rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
680 ((struct ip *)ti)->ip_off |= IP_DF;
681
682 #if BSD >= 43
683 error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
684 so->so_options & SO_DONTROUTE, 0);
685 #else
686 error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
687 so->so_options & SO_DONTROUTE);
688 #endif
689 }
690 if (error) {
691 out:
692 if (error == ENOBUFS) {
693 tcp_quench(tp->t_inpcb, 0);
694 return (0);
695 }
696 if ((error == EHOSTUNREACH || error == ENETDOWN)
697 && TCPS_HAVERCVDSYN(tp->t_state)) {
698 tp->t_softerror = error;
699 return (0);
700 }
701 return (error);
702 }
703 tcpstat.tcps_sndtotal++;
704 if (tp->t_flags & TF_DELACK)
705 tcpstat.tcps_delack++;
706
707 /*
708 * Data sent (as far as we can tell).
709 * If this advertises a larger window than any other segment,
710 * then remember the size of the advertised window.
711 * Any pending ACK has now been sent.
712 */
713 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
714 tp->rcv_adv = tp->rcv_nxt + win;
715 tp->last_ack_sent = tp->rcv_nxt;
716 tp->t_flags &= ~TF_ACKNOW;
717 TCP_CLEAR_DELACK(tp);
718 if (sendalot)
719 goto again;
720 return (0);
721 }
722
723 void
724 tcp_setpersist(tp)
725 register struct tcpcb *tp;
726 {
727 register int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
728
729 if (tp->t_timer[TCPT_REXMT])
730 panic("tcp_output REXMT");
731 /*
732 * Start/restart persistance timer.
733 */
734 if (t < tp->t_rttmin)
735 t = tp->t_rttmin;
736 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
737 t * tcp_backoff[tp->t_rxtshift],
738 TCPTV_PERSMIN, TCPTV_PERSMAX);
739 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
740 tp->t_rxtshift++;
741 }
742