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