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