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