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