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