VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/tcp_input.c@ 40623

Last change on this file since 40623 was 40423, checked in by vboxsync, 13 years ago

NAT: warnings [-Wunused-macros]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 65.6 KB
Line 
1/* $Id: tcp_input.c 40423 2012-03-11 03:22:22Z vboxsync $ */
2/** @file
3 * NAT - TCP input.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*
19 * This code is based on:
20 *
21 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
22 * The Regents of the University of California. All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * @(#)tcp_input.c 8.5 (Berkeley) 4/10/94
53 * tcp_input.c,v 1.10 1994/10/13 18:36:32 wollman Exp
54 */
55
56/*
57 * Changes and additions relating to SLiRP
58 * Copyright (c) 1995 Danny Gasparovski.
59 *
60 * Please read the file COPYRIGHT for the
61 * terms and conditions of the copyright.
62 */
63
64#include <slirp.h>
65#include "ip_icmp.h"
66
67
68#if 0 /* code using this macroses is commented out */
69# define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ)
70
71/* for modulo comparisons of timestamps */
72# define TSTMP_LT(a, b) ((int)((a)-(b)) < 0)
73# define TSTMP_GEQ(a, b) ((int)((a)-(b)) >= 0)
74#endif
75
76#ifndef TCP_ACK_HACK
77#define DELAY_ACK(tp, ti) \
78 if (ti->ti_flags & TH_PUSH) \
79 tp->t_flags |= TF_ACKNOW; \
80 else \
81 tp->t_flags |= TF_DELACK;
82#else /* !TCP_ACK_HACK */
83#define DELAY_ACK(tp, ign) \
84 tp->t_flags |= TF_DELACK;
85#endif /* TCP_ACK_HACK */
86
87
88/*
89 * deps: netinet/tcp_reass.c
90 * tcp_reass_maxqlen = 48 (deafault)
91 * tcp_reass_maxseg = nmbclusters/16 (nmbclusters = 1024 + maxusers * 64 from kern/kern_mbuf.c let's say 256)
92 */
93int
94tcp_reass(PNATState pData, struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
95{
96 struct tseg_qent *q;
97 struct tseg_qent *p = NULL;
98 struct tseg_qent *nq;
99 struct tseg_qent *te = NULL;
100 struct socket *so = tp->t_socket;
101 int flags;
102 STAM_PROFILE_START(&pData->StatTCP_reassamble, tcp_reassamble);
103 LogFlowFunc(("ENTER: pData:%p, tp:%R[tcpcb793], th:%p, tlenp:%p, m:%p\n", pData, tp, th, tlenp, m));
104
105 /*
106 * XXX: tcp_reass() is rather inefficient with its data structures
107 * and should be rewritten (see NetBSD for optimizations). While
108 * doing that it should move to its own file tcp_reass.c.
109 */
110
111 /*
112 * Call with th==NULL after become established to
113 * force pre-ESTABLISHED data up to user socket.
114 */
115 if (th == NULL)
116 {
117 LogFlowFunc(("%d -> present\n", __LINE__));
118 goto present;
119 }
120
121 /*
122 * Limit the number of segments in the reassembly queue to prevent
123 * holding on to too many segments (and thus running out of mbufs).
124 * Make sure to let the missing segment through which caused this
125 * queue. Always keep one global queue entry spare to be able to
126 * process the missing segment.
127 */
128 if ( th->th_seq != tp->rcv_nxt
129 && ( tcp_reass_qsize + 1 >= tcp_reass_maxseg
130 || tp->t_segqlen >= tcp_reass_maxqlen))
131 {
132 tcp_reass_overflows++;
133 tcpstat.tcps_rcvmemdrop++;
134 m_freem(pData, m);
135 *tlenp = 0;
136 STAM_PROFILE_STOP(&pData->StatTCP_reassamble, tcp_reassamble);
137 LogFlowFuncLeave();
138 return (0);
139 }
140
141 /*
142 * Allocate a new queue entry. If we can't, or hit the zone limit
143 * just drop the pkt.
144 */
145 te = RTMemAlloc(sizeof(struct tseg_qent));
146 if (te == NULL)
147 {
148 tcpstat.tcps_rcvmemdrop++;
149 m_freem(pData, m);
150 *tlenp = 0;
151 STAM_PROFILE_STOP(&pData->StatTCP_reassamble, tcp_reassamble);
152 LogFlowFuncLeave();
153 return (0);
154 }
155 tp->t_segqlen++;
156 tcp_reass_qsize++;
157
158 /*
159 * Find a segment which begins after this one does.
160 */
161 LIST_FOREACH(q, &tp->t_segq, tqe_q)
162 {
163 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
164 break;
165 p = q;
166 }
167
168 /*
169 * If there is a preceding segment, it may provide some of
170 * our data already. If so, drop the data from the incoming
171 * segment. If it provides all of our data, drop us.
172 */
173 if (p != NULL)
174 {
175 int i;
176 /* conversion to int (in i) handles seq wraparound */
177 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
178 if (i > 0)
179 {
180 if (i >= *tlenp)
181 {
182 tcpstat.tcps_rcvduppack++;
183 tcpstat.tcps_rcvdupbyte += *tlenp;
184 m_freem(pData, m);
185 RTMemFree(te);
186 tp->t_segqlen--;
187 tcp_reass_qsize--;
188 /*
189 * Try to present any queued data
190 * at the left window edge to the user.
191 * This is needed after the 3-WHS
192 * completes.
193 */
194 LogFlowFunc(("%d -> present\n", __LINE__));
195 goto present; /* ??? */
196 }
197 m_adj(m, i);
198 *tlenp -= i;
199 th->th_seq += i;
200 }
201 }
202 tcpstat.tcps_rcvoopack++;
203 tcpstat.tcps_rcvoobyte += *tlenp;
204
205 /*
206 * While we overlap succeeding segments trim them or,
207 * if they are completely covered, dequeue them.
208 */
209 while (q)
210 {
211 int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
212 if (i <= 0)
213 break;
214 if (i < q->tqe_len)
215 {
216 q->tqe_th->th_seq += i;
217 q->tqe_len -= i;
218 m_adj(q->tqe_m, i);
219 break;
220 }
221
222 nq = LIST_NEXT(q, tqe_q);
223 LIST_REMOVE(q, tqe_q);
224 m_freem(pData, q->tqe_m);
225 RTMemFree(q);
226 tp->t_segqlen--;
227 tcp_reass_qsize--;
228 q = nq;
229 }
230
231 /* Insert the new segment queue entry into place. */
232 te->tqe_m = m;
233 te->tqe_th = th;
234 te->tqe_len = *tlenp;
235
236 if (p == NULL)
237 {
238 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
239 }
240 else
241 {
242 LIST_INSERT_AFTER(p, te, tqe_q);
243 }
244
245present:
246 /*
247 * Present data to user, advancing rcv_nxt through
248 * completed sequence space.
249 */
250 if (!TCPS_HAVEESTABLISHED(tp->t_state))
251 {
252 STAM_PROFILE_STOP(&pData->StatTCP_reassamble, tcp_reassamble);
253 return (0);
254 }
255 q = LIST_FIRST(&tp->t_segq);
256 if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
257 {
258 STAM_PROFILE_STOP(&pData->StatTCP_reassamble, tcp_reassamble);
259 return (0);
260 }
261 do
262 {
263 tp->rcv_nxt += q->tqe_len;
264 flags = q->tqe_th->th_flags & TH_FIN;
265 nq = LIST_NEXT(q, tqe_q);
266 LIST_REMOVE(q, tqe_q);
267 /* XXX: This place should be checked for the same code in
268 * original BSD code for Slirp and current BSD used SS_FCANTRCVMORE
269 */
270 if (so->so_state & SS_FCANTSENDMORE)
271 m_freem(pData, q->tqe_m);
272 else
273 sbappend(pData, so, q->tqe_m);
274 RTMemFree(q);
275 tp->t_segqlen--;
276 tcp_reass_qsize--;
277 q = nq;
278 }
279 while (q && q->tqe_th->th_seq == tp->rcv_nxt);
280
281 STAM_PROFILE_STOP(&pData->StatTCP_reassamble, tcp_reassamble);
282 return flags;
283}
284
285/*
286 * TCP input routine, follows pages 65-76 of the
287 * protocol specification dated September, 1981 very closely.
288 */
289void
290tcp_input(PNATState pData, register struct mbuf *m, int iphlen, struct socket *inso)
291{
292 struct ip save_ip, *ip;
293 register struct tcpiphdr *ti;
294 caddr_t optp = NULL;
295 int optlen = 0;
296 int len, tlen, off;
297 register struct tcpcb *tp = 0;
298 register int tiflags;
299 struct socket *so = 0;
300 int todrop, acked, ourfinisacked, needoutput = 0;
301/* int dropsocket = 0; */
302 int iss = 0;
303 u_long tiwin;
304/* int ts_present = 0; */
305 STAM_PROFILE_START(&pData->StatTCP_input, counter_input);
306
307 LogFlow(("tcp_input: m = %8lx, iphlen = %2d, inso = %R[natsock]\n",
308 (long)m, iphlen, inso));
309
310 if (inso != NULL)
311 {
312 QSOCKET_LOCK(tcb);
313 SOCKET_LOCK(inso);
314 QSOCKET_UNLOCK(tcb);
315 }
316 /*
317 * If called with m == 0, then we're continuing the connect
318 */
319 if (m == NULL)
320 {
321 so = inso;
322 Log4(("NAT: tcp_input: %R[natsock]\n", so));
323 /* Re-set a few variables */
324 tp = sototcpcb(so);
325 m = so->so_m;
326
327 so->so_m = 0;
328 ti = so->so_ti;
329
330 /** @todo (vvl) clarify why it might happens */
331 if (ti == NULL)
332 {
333 LogRel(("NAT: ti is null. can't do any reseting connection actions\n"));
334 /* mbuf should be cleared in sofree called from tcp_close */
335 tcp_close(pData, tp);
336 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
337 LogFlowFuncLeave();
338 return;
339 }
340
341 tiwin = ti->ti_win;
342 tiflags = ti->ti_flags;
343
344 LogFlowFunc(("%d -> cont_conn\n", __LINE__));
345 goto cont_conn;
346 }
347
348 tcpstat.tcps_rcvtotal++;
349 /*
350 * Get IP and TCP header together in first mbuf.
351 * Note: IP leaves IP header in first mbuf.
352 */
353 ti = mtod(m, struct tcpiphdr *);
354 if (iphlen > sizeof(struct ip))
355 {
356 ip_stripoptions(m, (struct mbuf *)0);
357 iphlen = sizeof(struct ip);
358 }
359 /* XXX Check if too short */
360
361
362 /*
363 * Save a copy of the IP header in case we want restore it
364 * for sending an ICMP error message in response.
365 */
366 ip = mtod(m, struct ip *);
367 /*
368 * (vvl) ip_input substracts IP header length from ip->ip_len value.
369 * here we do the test the same as input method of UDP protocol.
370 */
371 Assert((ip->ip_len + iphlen == m_length(m, NULL)));
372 save_ip = *ip;
373 save_ip.ip_len+= iphlen;
374
375 /*
376 * Checksum extended TCP header and data.
377 */
378 tlen = ((struct ip *)ti)->ip_len;
379 memset(ti->ti_x1, 0, 9);
380 ti->ti_len = RT_H2N_U16((u_int16_t)tlen);
381 len = sizeof(struct ip) + tlen;
382 /* keep checksum for ICMP reply
383 * ti->ti_sum = cksum(m, len);
384 * if (ti->ti_sum) { */
385 if (cksum(m, len))
386 {
387 tcpstat.tcps_rcvbadsum++;
388 LogFlowFunc(("%d -> drop\n", __LINE__));
389 goto drop;
390 }
391
392 /*
393 * Check that TCP offset makes sense,
394 * pull out TCP options and adjust length. XXX
395 */
396 off = ti->ti_off << 2;
397 if ( off < sizeof (struct tcphdr)
398 || off > tlen)
399 {
400 tcpstat.tcps_rcvbadoff++;
401 LogFlowFunc(("%d -> drop\n", __LINE__));
402 goto drop;
403 }
404 tlen -= off;
405 ti->ti_len = tlen;
406 if (off > sizeof (struct tcphdr))
407 {
408 optlen = off - sizeof (struct tcphdr);
409 optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
410
411 /*
412 * Do quick retrieval of timestamp options ("options
413 * prediction?"). If timestamp is the only option and it's
414 * formatted as recommended in RFC 1323 appendix A, we
415 * quickly get the values now and not bother calling
416 * tcp_dooptions(), etc.
417 */
418#if 0
419 if (( optlen == TCPOLEN_TSTAMP_APPA
420 || ( optlen > TCPOLEN_TSTAMP_APPA
421 && optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
422 *(u_int32_t *)optp == RT_H2N_U32_C(TCPOPT_TSTAMP_HDR) &&
423 (ti->ti_flags & TH_SYN) == 0)
424 {
425 ts_present = 1;
426 ts_val = RT_N2H_U32(*(u_int32_t *)(optp + 4));
427 ts_ecr = RT_N2H_U32(*(u_int32_t *)(optp + 8));
428 optp = NULL; / * we have parsed the options * /
429 }
430#endif
431 }
432 tiflags = ti->ti_flags;
433
434 /*
435 * Convert TCP protocol specific fields to host format.
436 */
437 NTOHL(ti->ti_seq);
438 NTOHL(ti->ti_ack);
439 NTOHS(ti->ti_win);
440 NTOHS(ti->ti_urp);
441
442 /*
443 * Drop TCP, IP headers and TCP options.
444 */
445 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
446 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
447
448 /*
449 * Locate pcb for segment.
450 */
451findso:
452 LogFlowFunc(("(enter) findso: %R[natsock]\n", so));
453 if (so != NULL && so != &tcb)
454 SOCKET_UNLOCK(so);
455 QSOCKET_LOCK(tcb);
456 so = tcp_last_so;
457 if ( so->so_fport != ti->ti_dport
458 || so->so_lport != ti->ti_sport
459 || so->so_laddr.s_addr != ti->ti_src.s_addr
460 || so->so_faddr.s_addr != ti->ti_dst.s_addr)
461 {
462#ifdef VBOX_WITH_SLIRP_MT
463 struct socket *sonxt;
464#endif
465 QSOCKET_UNLOCK(tcb);
466 /* @todo fix SOLOOKUP macrodefinition to be usable here */
467#ifndef VBOX_WITH_SLIRP_MT
468 so = solookup(&tcb, ti->ti_src, ti->ti_sport,
469 ti->ti_dst, ti->ti_dport);
470#else
471 so = NULL;
472 QSOCKET_FOREACH(so, sonxt, tcp)
473 /* { */
474 if ( so->so_lport == ti->ti_sport
475 && so->so_laddr.s_addr == ti->ti_src.s_addr
476 && so->so_faddr.s_addr == ti->ti_dst.s_addr
477 && so->so_fport == ti->ti_dport
478 && so->so_deleted != 1)
479 {
480 break; /* so is locked here */
481 }
482 LOOP_LABEL(tcp, so, sonxt);
483 }
484 if (so == &tcb) {
485 so = NULL;
486 }
487#endif
488 if (so)
489 {
490 tcp_last_so = so;
491 }
492 ++tcpstat.tcps_socachemiss;
493 }
494 else
495 {
496 SOCKET_LOCK(so);
497 QSOCKET_UNLOCK(tcb);
498 }
499 LogFlowFunc(("(leave) findso: %R[natsock]\n", so));
500
501 /*
502 * If the state is CLOSED (i.e., TCB does not exist) then
503 * all data in the incoming segment is discarded.
504 * If the TCB exists but is in CLOSED state, it is embryonic,
505 * but should either do a listen or a connect soon.
506 *
507 * state == CLOSED means we've done socreate() but haven't
508 * attached it to a protocol yet...
509 *
510 * XXX If a TCB does not exist, and the TH_SYN flag is
511 * the only flag set, then create a session, mark it
512 * as if it was LISTENING, and continue...
513 */
514 if (so == 0)
515 {
516 if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) != TH_SYN)
517 {
518 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
519 goto dropwithreset;
520 }
521
522 if ((so = socreate()) == NULL)
523 {
524 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
525 goto dropwithreset;
526 }
527 if (tcp_attach(pData, so) < 0)
528 {
529 RTMemFree(so); /* Not sofree (if it failed, it's not insqued) */
530 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
531 goto dropwithreset;
532 }
533 SOCKET_LOCK(so);
534#ifndef VBOX_WITH_SLIRP_BSD_SBUF
535 sbreserve(pData, &so->so_snd, tcp_sndspace);
536 sbreserve(pData, &so->so_rcv, tcp_rcvspace);
537#else
538 sbuf_new(&so->so_snd, NULL, tcp_sndspace, SBUF_AUTOEXTEND);
539 sbuf_new(&so->so_rcv, NULL, tcp_rcvspace, SBUF_AUTOEXTEND);
540#endif
541
542/* tcp_last_so = so; */ /* XXX ? */
543/* tp = sototcpcb(so); */
544
545 so->so_laddr = ti->ti_src;
546 so->so_lport = ti->ti_sport;
547 so->so_faddr = ti->ti_dst;
548 so->so_fport = ti->ti_dport;
549
550 so->so_iptos = ((struct ip *)ti)->ip_tos;
551
552 tp = sototcpcb(so);
553 TCP_STATE_SWITCH_TO(tp, TCPS_LISTEN);
554 }
555
556 /*
557 * If this is a still-connecting socket, this probably
558 * a retransmit of the SYN. Whether it's a retransmit SYN
559 * or something else, we nuke it.
560 */
561 if (so->so_state & SS_ISFCONNECTING)
562 {
563 LogFlowFunc(("%d -> drop\n", __LINE__));
564 goto drop;
565 }
566
567 tp = sototcpcb(so);
568
569 /* XXX Should never fail */
570 if (tp == 0)
571 {
572 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
573 goto dropwithreset;
574 }
575 if (tp->t_state == TCPS_CLOSED)
576 {
577 LogFlowFunc(("%d -> drop\n", __LINE__));
578 goto drop;
579 }
580
581 /* Unscale the window into a 32-bit value. */
582/* if ((tiflags & TH_SYN) == 0)
583 * tiwin = ti->ti_win << tp->snd_scale;
584 * else
585 */
586 tiwin = ti->ti_win;
587
588 /*
589 * Segment received on connection.
590 * Reset idle time and keep-alive timer.
591 */
592 tp->t_idle = 0;
593 if (so_options)
594 tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
595 else
596 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
597
598 /*
599 * Process options if not in LISTEN state,
600 * else do it below (after getting remote address).
601 */
602 if (optp && tp->t_state != TCPS_LISTEN)
603 tcp_dooptions(pData, tp, (u_char *)optp, optlen, ti);
604/* , */
605/* &ts_present, &ts_val, &ts_ecr); */
606
607 /*
608 * Header prediction: check for the two common cases
609 * of a uni-directional data xfer. If the packet has
610 * no control flags, is in-sequence, the window didn't
611 * change and we're not retransmitting, it's a
612 * candidate. If the length is zero and the ack moved
613 * forward, we're the sender side of the xfer. Just
614 * free the data acked & wake any higher level process
615 * that was blocked waiting for space. If the length
616 * is non-zero and the ack didn't move, we're the
617 * receiver side. If we're getting packets in-order
618 * (the reassembly queue is empty), add the data to
619 * the socket buffer and note that we need a delayed ack.
620 *
621 * XXX Some of these tests are not needed
622 * eg: the tiwin == tp->snd_wnd prevents many more
623 * predictions.. with no *real* advantage..
624 */
625 if ( tp->t_state == TCPS_ESTABLISHED
626 && (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK
627/* && (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) */
628 && ti->ti_seq == tp->rcv_nxt
629 && tiwin && tiwin == tp->snd_wnd
630 && tp->snd_nxt == tp->snd_max)
631 {
632 /*
633 * If last ACK falls within this segment's sequence numbers,
634 * record the timestamp.
635 */
636#if 0
637 if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
638 SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len))
639 {
640 tp->ts_recent_age = tcp_now;
641 tp->ts_recent = ts_val;
642 }
643#endif
644
645 if (ti->ti_len == 0)
646 {
647 if ( SEQ_GT(ti->ti_ack, tp->snd_una)
648 && SEQ_LEQ(ti->ti_ack, tp->snd_max)
649 && tp->snd_cwnd >= tp->snd_wnd)
650 {
651 /*
652 * this is a pure ack for outstanding data.
653 */
654 ++tcpstat.tcps_predack;
655#if 0
656 if (ts_present)
657 tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
658 else
659#endif
660 if ( tp->t_rtt
661 && SEQ_GT(ti->ti_ack, tp->t_rtseq))
662 tcp_xmit_timer(pData, tp, tp->t_rtt);
663 acked = ti->ti_ack - tp->snd_una;
664 tcpstat.tcps_rcvackpack++;
665 tcpstat.tcps_rcvackbyte += acked;
666#ifndef VBOX_WITH_SLIRP_BSD_SBUF
667 sbdrop(&so->so_snd, acked);
668#else
669 if (sbuf_len(&so->so_snd) < acked)
670 /* drop all what sbuf have */
671 sbuf_setpos(&so->so_snd, 0);
672 else
673 sbuf_setpos(&so->so_snd, sbuf_len(&so->so_snd) - acked);
674#endif
675 tp->snd_una = ti->ti_ack;
676 m_freem(pData, m);
677
678 /*
679 * If all outstanding data are acked, stop
680 * retransmit timer, otherwise restart timer
681 * using current (possibly backed-off) value.
682 * If process is waiting for space,
683 * wakeup/selwakeup/signal. If data
684 * are ready to send, let tcp_output
685 * decide between more output or persist.
686 */
687 if (tp->snd_una == tp->snd_max)
688 tp->t_timer[TCPT_REXMT] = 0;
689 else if (tp->t_timer[TCPT_PERSIST] == 0)
690 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
691
692 /*
693 * There's room in so_snd, sowwakup will read()
694 * from the socket if we can
695 */
696#if 0
697 if (so->so_snd.sb_flags & SB_NOTIFY)
698 sowwakeup(so);
699#endif
700 /*
701 * This is called because sowwakeup might have
702 * put data into so_snd. Since we don't so sowwakeup,
703 * we don't need this.. XXX???
704 */
705 if (SBUF_LEN(&so->so_snd))
706 (void) tcp_output(pData, tp);
707
708 SOCKET_UNLOCK(so);
709 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
710 return;
711 }
712 }
713 else if ( ti->ti_ack == tp->snd_una
714 && LIST_FIRST(&tp->t_segq)
715 && ti->ti_len <= sbspace(&so->so_rcv))
716 {
717 /*
718 * this is a pure, in-sequence data packet
719 * with nothing on the reassembly queue and
720 * we have enough buffer space to take it.
721 */
722 ++tcpstat.tcps_preddat;
723 tp->rcv_nxt += ti->ti_len;
724 tcpstat.tcps_rcvpack++;
725 tcpstat.tcps_rcvbyte += ti->ti_len;
726 /*
727 * Add data to socket buffer.
728 */
729 sbappend(pData, so, m);
730
731 /*
732 * XXX This is called when data arrives. Later, check
733 * if we can actually write() to the socket
734 * XXX Need to check? It's be NON_BLOCKING
735 */
736/* sorwakeup(so); */
737
738 /*
739 * If this is a short packet, then ACK now - with Nagel
740 * congestion avoidance sender won't send more until
741 * he gets an ACK.
742 *
743 * It is better to not delay acks at all to maximize
744 * TCP throughput. See RFC 2581.
745 */
746 tp->t_flags |= TF_ACKNOW;
747 tcp_output(pData, tp);
748 SOCKET_UNLOCK(so);
749 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
750 return;
751 }
752 } /* header prediction */
753 /*
754 * Calculate amount of space in receive window,
755 * and then do TCP input processing.
756 * Receive window is amount of space in rcv queue,
757 * but not less than advertised window.
758 */
759 {
760 int win;
761 win = sbspace(&so->so_rcv);
762 if (win < 0)
763 win = 0;
764 tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
765 }
766
767 switch (tp->t_state)
768 {
769 /*
770 * If the state is LISTEN then ignore segment if it contains an RST.
771 * If the segment contains an ACK then it is bad and send a RST.
772 * If it does not contain a SYN then it is not interesting; drop it.
773 * Don't bother responding if the destination was a broadcast.
774 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
775 * tp->iss, and send a segment:
776 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
777 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
778 * Fill in remote peer address fields if not previously specified.
779 * Enter SYN_RECEIVED state, and process any other fields of this
780 * segment in this state.
781 */
782 case TCPS_LISTEN:
783 {
784 if (tiflags & TH_RST)
785 {
786 LogFlowFunc(("%d -> drop\n", __LINE__));
787 goto drop;
788 }
789 if (tiflags & TH_ACK)
790 {
791 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
792 goto dropwithreset;
793 }
794 if ((tiflags & TH_SYN) == 0)
795 {
796 LogFlowFunc(("%d -> drop\n", __LINE__));
797 goto drop;
798 }
799
800 /*
801 * This has way too many gotos...
802 * But a bit of spaghetti code never hurt anybody :)
803 */
804 if ( (tcp_fconnect(pData, so) == -1)
805 && errno != EINPROGRESS
806 && errno != EWOULDBLOCK)
807 {
808 u_char code = ICMP_UNREACH_NET;
809 Log2((" tcp fconnect errno = %d (%s)\n", errno, strerror(errno)));
810 if (errno == ECONNREFUSED)
811 {
812 /* ACK the SYN, send RST to refuse the connection */
813 tcp_respond(pData, tp, ti, m, ti->ti_seq+1, (tcp_seq)0,
814 TH_RST|TH_ACK);
815 }
816 else
817 {
818 if (errno == EHOSTUNREACH)
819 code = ICMP_UNREACH_HOST;
820 HTONL(ti->ti_seq); /* restore tcp header */
821 HTONL(ti->ti_ack);
822 HTONS(ti->ti_win);
823 HTONS(ti->ti_urp);
824 m->m_data -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
825 m->m_len += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
826 *ip = save_ip;
827 icmp_error(pData, m, ICMP_UNREACH, code, 0, strerror(errno));
828 tp->t_socket->so_m = NULL;
829 }
830 tp = tcp_close(pData, tp);
831 }
832 else
833 {
834 /*
835 * Haven't connected yet, save the current mbuf
836 * and ti, and return
837 * XXX Some OS's don't tell us whether the connect()
838 * succeeded or not. So we must time it out.
839 */
840 so->so_m = m;
841 so->so_ti = ti;
842 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
843 TCP_STATE_SWITCH_TO(tp, TCPS_SYN_RECEIVED);
844 }
845 SOCKET_UNLOCK(so);
846 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
847 LogFlowFuncLeave();
848 return;
849
850cont_conn:
851 /* m==NULL
852 * Check if the connect succeeded
853 */
854 LogFlowFunc(("cont_conn:\n"));
855 if (so->so_state & SS_NOFDREF)
856 {
857 tp = tcp_close(pData, tp);
858 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
859 goto dropwithreset;
860 }
861
862 tcp_template(tp);
863
864 if (optp)
865 tcp_dooptions(pData, tp, (u_char *)optp, optlen, ti);
866
867 if (iss)
868 tp->iss = iss;
869 else
870 tp->iss = tcp_iss;
871 tcp_iss += TCP_ISSINCR/2;
872 tp->irs = ti->ti_seq;
873 tcp_sendseqinit(tp);
874 tcp_rcvseqinit(tp);
875 tp->t_flags |= TF_ACKNOW;
876 TCP_STATE_SWITCH_TO(tp, TCPS_SYN_RECEIVED);
877 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
878 tcpstat.tcps_accepts++;
879 LogFlowFunc(("%d -> trimthenstep6\n", __LINE__));
880 goto trimthenstep6;
881 } /* case TCPS_LISTEN */
882
883 /*
884 * If the state is SYN_SENT:
885 * if seg contains an ACK, but not for our SYN, drop the input.
886 * if seg contains a RST, then drop the connection.
887 * if seg does not contain SYN, then drop it.
888 * Otherwise this is an acceptable SYN segment
889 * initialize tp->rcv_nxt and tp->irs
890 * if seg contains ack then advance tp->snd_una
891 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
892 * arrange for segment to be acked (eventually)
893 * continue processing rest of data/controls, beginning with URG
894 */
895 case TCPS_SYN_SENT:
896 if ( (tiflags & TH_ACK)
897 && ( SEQ_LEQ(ti->ti_ack, tp->iss)
898 || SEQ_GT(ti->ti_ack, tp->snd_max)))
899 {
900 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
901 goto dropwithreset;
902 }
903
904 if (tiflags & TH_RST)
905 {
906 if (tiflags & TH_ACK)
907 tp = tcp_drop(pData, tp, 0); /* XXX Check t_softerror! */
908 LogFlowFunc(("%d -> drop\n", __LINE__));
909 goto drop;
910 }
911
912 if ((tiflags & TH_SYN) == 0)
913 {
914 LogFlowFunc(("%d -> drop\n", __LINE__));
915 goto drop;
916 }
917 if (tiflags & TH_ACK)
918 {
919 tp->snd_una = ti->ti_ack;
920 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
921 tp->snd_nxt = tp->snd_una;
922 }
923
924 tp->t_timer[TCPT_REXMT] = 0;
925 tp->irs = ti->ti_seq;
926 tcp_rcvseqinit(tp);
927 tp->t_flags |= TF_ACKNOW;
928 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss))
929 {
930 tcpstat.tcps_connects++;
931 soisfconnected(so);
932 TCP_STATE_SWITCH_TO(tp, TCPS_ESTABLISHED);
933
934 /* Do window scaling on this connection? */
935#if 0
936 if (( tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE))
937 == (TF_RCVD_SCALE|TF_REQ_SCALE))
938 {
939 tp->snd_scale = tp->requested_s_scale;
940 tp->rcv_scale = tp->request_r_scale;
941 }
942#endif
943 (void) tcp_reass(pData, tp, (struct tcphdr *)0, NULL, (struct mbuf *)0);
944 /*
945 * if we didn't have to retransmit the SYN,
946 * use its rtt as our initial srtt & rtt var.
947 */
948 if (tp->t_rtt)
949 tcp_xmit_timer(pData, tp, tp->t_rtt);
950 }
951 else
952 TCP_STATE_SWITCH_TO(tp, TCPS_SYN_RECEIVED);
953
954trimthenstep6:
955 LogFlowFunc(("trimthenstep6:\n"));
956 /*
957 * Advance ti->ti_seq to correspond to first data byte.
958 * If data, trim to stay within window,
959 * dropping FIN if necessary.
960 */
961 ti->ti_seq++;
962 if (ti->ti_len > tp->rcv_wnd)
963 {
964 todrop = ti->ti_len - tp->rcv_wnd;
965 m_adj(m, -todrop);
966 ti->ti_len = tp->rcv_wnd;
967 tiflags &= ~TH_FIN;
968 tcpstat.tcps_rcvpackafterwin++;
969 tcpstat.tcps_rcvbyteafterwin += todrop;
970 }
971 tp->snd_wl1 = ti->ti_seq - 1;
972 tp->rcv_up = ti->ti_seq;
973 LogFlowFunc(("%d -> step6\n", __LINE__));
974 goto step6;
975 } /* switch tp->t_state */
976 /*
977 * States other than LISTEN or SYN_SENT.
978 * First check timestamp, if present.
979 * Then check that at least some bytes of segment are within
980 * receive window. If segment begins before rcv_nxt,
981 * drop leading data (and SYN); if nothing left, just ack.
982 *
983 * RFC 1323 PAWS: If we have a timestamp reply on this segment
984 * and it's less than ts_recent, drop it.
985 */
986#if 0
987 if ( ts_present
988 && (tiflags & TH_RST) == 0
989 && tp->ts_recent
990 && TSTMP_LT(ts_val, tp->ts_recent))
991 {
992 /* Check to see if ts_recent is over 24 days old. */
993 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE)
994 {
995 /*
996 * Invalidate ts_recent. If this segment updates
997 * ts_recent, the age will be reset later and ts_recent
998 * will get a valid value. If it does not, setting
999 * ts_recent to zero will at least satisfy the
1000 * requirement that zero be placed in the timestamp
1001 * echo reply when ts_recent isn't valid. The
1002 * age isn't reset until we get a valid ts_recent
1003 * because we don't want out-of-order segments to be
1004 * dropped when ts_recent is old.
1005 */
1006 tp->ts_recent = 0;
1007 }
1008 else
1009 {
1010 tcpstat.tcps_rcvduppack++;
1011 tcpstat.tcps_rcvdupbyte += ti->ti_len;
1012 tcpstat.tcps_pawsdrop++;
1013 goto dropafterack;
1014 }
1015 }
1016#endif
1017
1018 todrop = tp->rcv_nxt - ti->ti_seq;
1019 if (todrop > 0)
1020 {
1021 if (tiflags & TH_SYN)
1022 {
1023 tiflags &= ~TH_SYN;
1024 ti->ti_seq++;
1025 if (ti->ti_urp > 1)
1026 ti->ti_urp--;
1027 else
1028 tiflags &= ~TH_URG;
1029 todrop--;
1030 }
1031 /*
1032 * Following if statement from Stevens, vol. 2, p. 960.
1033 */
1034 if ( todrop > ti->ti_len
1035 || ( todrop == ti->ti_len
1036 && (tiflags & TH_FIN) == 0))
1037 {
1038 /*
1039 * Any valid FIN must be to the left of the window.
1040 * At this point the FIN must be a duplicate or out
1041 * of sequence; drop it.
1042 */
1043 tiflags &= ~TH_FIN;
1044
1045 /*
1046 * Send an ACK to resynchronize and drop any data.
1047 * But keep on processing for RST or ACK.
1048 */
1049 tp->t_flags |= TF_ACKNOW;
1050 todrop = ti->ti_len;
1051 tcpstat.tcps_rcvduppack++;
1052 tcpstat.tcps_rcvdupbyte += todrop;
1053 }
1054 else
1055 {
1056 tcpstat.tcps_rcvpartduppack++;
1057 tcpstat.tcps_rcvpartdupbyte += todrop;
1058 }
1059 m_adj(m, todrop);
1060 ti->ti_seq += todrop;
1061 ti->ti_len -= todrop;
1062 if (ti->ti_urp > todrop)
1063 ti->ti_urp -= todrop;
1064 else
1065 {
1066 tiflags &= ~TH_URG;
1067 ti->ti_urp = 0;
1068 }
1069 }
1070 /*
1071 * If new data are received on a connection after the
1072 * user processes are gone, then RST the other end.
1073 */
1074 if ( (so->so_state & SS_NOFDREF)
1075 && tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len)
1076 {
1077 tp = tcp_close(pData, tp);
1078 tcpstat.tcps_rcvafterclose++;
1079 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
1080 goto dropwithreset;
1081 }
1082
1083 /*
1084 * If segment ends after window, drop trailing data
1085 * (and PUSH and FIN); if nothing left, just ACK.
1086 */
1087 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
1088 if (todrop > 0)
1089 {
1090 tcpstat.tcps_rcvpackafterwin++;
1091 if (todrop >= ti->ti_len)
1092 {
1093 tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
1094 /*
1095 * If a new connection request is received
1096 * while in TIME_WAIT, drop the old connection
1097 * and start over if the sequence numbers
1098 * are above the previous ones.
1099 */
1100 if ( tiflags & TH_SYN
1101 && tp->t_state == TCPS_TIME_WAIT
1102 && SEQ_GT(ti->ti_seq, tp->rcv_nxt))
1103 {
1104 iss = tp->rcv_nxt + TCP_ISSINCR;
1105 tp = tcp_close(pData, tp);
1106 SOCKET_UNLOCK(tp->t_socket);
1107 LogFlowFunc(("%d -> findso\n", __LINE__));
1108 goto findso;
1109 }
1110 /*
1111 * If window is closed can only take segments at
1112 * window edge, and have to drop data and PUSH from
1113 * incoming segments. Continue processing, but
1114 * remember to ack. Otherwise, drop segment
1115 * and ack.
1116 */
1117 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt)
1118 {
1119 tp->t_flags |= TF_ACKNOW;
1120 tcpstat.tcps_rcvwinprobe++;
1121 }
1122 else
1123 {
1124 LogFlowFunc(("%d -> dropafterack\n", __LINE__));
1125 goto dropafterack;
1126 }
1127 }
1128 else
1129 tcpstat.tcps_rcvbyteafterwin += todrop;
1130 m_adj(m, -todrop);
1131 ti->ti_len -= todrop;
1132 tiflags &= ~(TH_PUSH|TH_FIN);
1133 }
1134
1135 /*
1136 * If last ACK falls within this segment's sequence numbers,
1137 * record its timestamp.
1138 */
1139#if 0
1140 if ( ts_present
1141 && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)
1142 && SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len + ((tiflags & (TH_SYN|TH_FIN)) != 0)))
1143 {
1144 tp->ts_recent_age = tcp_now;
1145 tp->ts_recent = ts_val;
1146 }
1147#endif
1148
1149 /*
1150 * If the RST bit is set examine the state:
1151 * SYN_RECEIVED STATE:
1152 * If passive open, return to LISTEN state.
1153 * If active open, inform user that connection was refused.
1154 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1155 * Inform user that connection was reset, and close tcb.
1156 * CLOSING, LAST_ACK, TIME_WAIT STATES
1157 * Close the tcb.
1158 */
1159 if (tiflags&TH_RST)
1160 switch (tp->t_state)
1161 {
1162 case TCPS_SYN_RECEIVED:
1163/* so->so_error = ECONNREFUSED; */
1164 LogFlowFunc(("%d -> close\n", __LINE__));
1165 goto close;
1166
1167 case TCPS_ESTABLISHED:
1168 case TCPS_FIN_WAIT_1:
1169 case TCPS_FIN_WAIT_2:
1170 case TCPS_CLOSE_WAIT:
1171/* so->so_error = ECONNRESET; */
1172close:
1173 LogFlowFunc(("close:\n"));
1174 TCP_STATE_SWITCH_TO(tp, TCPS_CLOSED);
1175 tcpstat.tcps_drops++;
1176 tp = tcp_close(pData, tp);
1177 LogFlowFunc(("%d -> drop\n", __LINE__));
1178 goto drop;
1179
1180 case TCPS_CLOSING:
1181 case TCPS_LAST_ACK:
1182 case TCPS_TIME_WAIT:
1183 tp = tcp_close(pData, tp);
1184 LogFlowFunc(("%d -> drop\n", __LINE__));
1185 goto drop;
1186 }
1187
1188 /*
1189 * If a SYN is in the window, then this is an
1190 * error and we send an RST and drop the connection.
1191 */
1192 if (tiflags & TH_SYN)
1193 {
1194 tp = tcp_drop(pData, tp, 0);
1195 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
1196 goto dropwithreset;
1197 }
1198
1199 /*
1200 * If the ACK bit is off we drop the segment and return.
1201 */
1202 if ((tiflags & TH_ACK) == 0)
1203 {
1204 LogFlowFunc(("%d -> drop\n", __LINE__));
1205 goto drop;
1206 }
1207
1208 /*
1209 * Ack processing.
1210 */
1211 switch (tp->t_state)
1212 {
1213 /*
1214 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
1215 * ESTABLISHED state and continue processing, otherwise
1216 * send an RST. una<=ack<=max
1217 */
1218 case TCPS_SYN_RECEIVED:
1219 LogFlowFunc(("%d -> TCPS_SYN_RECEIVED\n", __LINE__));
1220 if ( SEQ_GT(tp->snd_una, ti->ti_ack)
1221 || SEQ_GT(ti->ti_ack, tp->snd_max))
1222 goto dropwithreset;
1223 tcpstat.tcps_connects++;
1224 TCP_STATE_SWITCH_TO(tp, TCPS_ESTABLISHED);
1225 /*
1226 * The sent SYN is ack'ed with our sequence number +1
1227 * The first data byte already in the buffer will get
1228 * lost if no correction is made. This is only needed for
1229 * SS_CTL since the buffer is empty otherwise.
1230 * tp->snd_una++; or:
1231 */
1232 tp->snd_una = ti->ti_ack;
1233 soisfconnected(so);
1234
1235 /* Do window scaling? */
1236#if 0
1237 if ( (tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE))
1238 == (TF_RCVD_SCALE|TF_REQ_SCALE))
1239 {
1240 tp->snd_scale = tp->requested_s_scale;
1241 tp->rcv_scale = tp->request_r_scale;
1242 }
1243#endif
1244 (void) tcp_reass(pData, tp, (struct tcphdr *)0, (int *)0, (struct mbuf *)0);
1245 tp->snd_wl1 = ti->ti_seq - 1;
1246 /* Avoid ack processing; snd_una==ti_ack => dup ack */
1247 LogFlowFunc(("%d -> synrx_to_est\n", __LINE__));
1248 goto synrx_to_est;
1249 /* fall into ... */
1250
1251 /*
1252 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1253 * ACKs. If the ack is in the range
1254 * tp->snd_una < ti->ti_ack <= tp->snd_max
1255 * then advance tp->snd_una to ti->ti_ack and drop
1256 * data from the retransmission queue. If this ACK reflects
1257 * more up to date window information we update our window information.
1258 */
1259 case TCPS_ESTABLISHED:
1260 case TCPS_FIN_WAIT_1:
1261 case TCPS_FIN_WAIT_2:
1262 case TCPS_CLOSE_WAIT:
1263 case TCPS_CLOSING:
1264 case TCPS_LAST_ACK:
1265 case TCPS_TIME_WAIT:
1266 LogFlowFunc(("%d -> TCPS_ESTABLISHED|TCPS_FIN_WAIT_1|TCPS_FIN_WAIT_2|TCPS_CLOSE_WAIT|"
1267 "TCPS_CLOSING|TCPS_LAST_ACK|TCPS_TIME_WAIT\n", __LINE__));
1268 if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
1269 {
1270 if (ti->ti_len == 0 && tiwin == tp->snd_wnd)
1271 {
1272 tcpstat.tcps_rcvdupack++;
1273 Log2((" dup ack m = %lx, so = %lx\n", (long)m, (long)so));
1274 /*
1275 * If we have outstanding data (other than
1276 * a window probe), this is a completely
1277 * duplicate ack (ie, window info didn't
1278 * change), the ack is the biggest we've
1279 * seen and we've seen exactly our rexmt
1280 * threshold of them, assume a packet
1281 * has been dropped and retransmit it.
1282 * Kludge snd_nxt & the congestion
1283 * window so we send only this one
1284 * packet.
1285 *
1286 * We know we're losing at the current
1287 * window size so do congestion avoidance
1288 * (set ssthresh to half the current window
1289 * and pull our congestion window back to
1290 * the new ssthresh).
1291 *
1292 * Dup acks mean that packets have left the
1293 * network (they're now cached at the receiver)
1294 * so bump cwnd by the amount in the receiver
1295 * to keep a constant cwnd packets in the
1296 * network.
1297 */
1298 if ( tp->t_timer[TCPT_REXMT] == 0
1299 || ti->ti_ack != tp->snd_una)
1300 tp->t_dupacks = 0;
1301 else if (++tp->t_dupacks == tcprexmtthresh)
1302 {
1303 tcp_seq onxt = tp->snd_nxt;
1304 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
1305 if (win < 2)
1306 win = 2;
1307 tp->snd_ssthresh = win * tp->t_maxseg;
1308 tp->t_timer[TCPT_REXMT] = 0;
1309 tp->t_rtt = 0;
1310 tp->snd_nxt = ti->ti_ack;
1311 tp->snd_cwnd = tp->t_maxseg;
1312 (void) tcp_output(pData, tp);
1313 tp->snd_cwnd = tp->snd_ssthresh +
1314 tp->t_maxseg * tp->t_dupacks;
1315 if (SEQ_GT(onxt, tp->snd_nxt))
1316 tp->snd_nxt = onxt;
1317 LogFlowFunc(("%d -> drop\n", __LINE__));
1318 goto drop;
1319 }
1320 else if (tp->t_dupacks > tcprexmtthresh)
1321 {
1322 tp->snd_cwnd += tp->t_maxseg;
1323 (void) tcp_output(pData, tp);
1324 LogFlowFunc(("%d -> drop\n", __LINE__));
1325 goto drop;
1326 }
1327 }
1328 else
1329 tp->t_dupacks = 0;
1330 break;
1331 }
1332synrx_to_est:
1333 LogFlowFunc(("synrx_to_est:\n"));
1334 /*
1335 * If the congestion window was inflated to account
1336 * for the other side's cached packets, retract it.
1337 */
1338 if ( tp->t_dupacks > tcprexmtthresh
1339 && tp->snd_cwnd > tp->snd_ssthresh)
1340 tp->snd_cwnd = tp->snd_ssthresh;
1341 tp->t_dupacks = 0;
1342 if (SEQ_GT(ti->ti_ack, tp->snd_max))
1343 {
1344 tcpstat.tcps_rcvacktoomuch++;
1345 LogFlowFunc(("%d -> dropafterack\n", __LINE__));
1346 goto dropafterack;
1347 }
1348 acked = ti->ti_ack - tp->snd_una;
1349 tcpstat.tcps_rcvackpack++;
1350 tcpstat.tcps_rcvackbyte += acked;
1351
1352 /*
1353 * If we have a timestamp reply, update smoothed
1354 * round trip time. If no timestamp is present but
1355 * transmit timer is running and timed sequence
1356 * number was acked, update smoothed round trip time.
1357 * Since we now have an rtt measurement, cancel the
1358 * timer backoff (cf., Phil Karn's retransmit alg.).
1359 * Recompute the initial retransmit timer.
1360 */
1361#if 0
1362 if (ts_present)
1363 tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
1364 else
1365#endif
1366 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1367 tcp_xmit_timer(pData, tp, tp->t_rtt);
1368
1369 /*
1370 * If all outstanding data is acked, stop retransmit
1371 * timer and remember to restart (more output or persist).
1372 * If there is more data to be acked, restart retransmit
1373 * timer, using current (possibly backed-off) value.
1374 */
1375 if (ti->ti_ack == tp->snd_max)
1376 {
1377 tp->t_timer[TCPT_REXMT] = 0;
1378 needoutput = 1;
1379 }
1380 else if (tp->t_timer[TCPT_PERSIST] == 0)
1381 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1382 /*
1383 * When new data is acked, open the congestion window.
1384 * If the window gives us less than ssthresh packets
1385 * in flight, open exponentially (maxseg per packet).
1386 * Otherwise open linearly: maxseg per window
1387 * (maxseg^2 / cwnd per packet).
1388 */
1389 {
1390 register u_int cw = tp->snd_cwnd;
1391 register u_int incr = tp->t_maxseg;
1392
1393 if (cw > tp->snd_ssthresh)
1394 incr = incr * incr / cw;
1395 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1396 }
1397 if (acked > SBUF_LEN(&so->so_snd))
1398 {
1399 tp->snd_wnd -= SBUF_LEN(&so->so_snd);
1400#ifndef VBOX_WITH_SLIRP_BSD_SBUF
1401 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1402#else
1403 sbuf_clear(&so->so_snd);
1404#endif
1405 ourfinisacked = 1;
1406 }
1407 else
1408 {
1409#ifndef VBOX_WITH_SLIRP_BSD_SBUF
1410 sbdrop(&so->so_snd, acked);
1411#else
1412 sbuf_setpos(&so->so_snd, sbuf_len(&so->so_snd) - acked);
1413#endif
1414 tp->snd_wnd -= acked;
1415 ourfinisacked = 0;
1416 }
1417 /*
1418 * XXX sowwakup is called when data is acked and there's room for
1419 * for more data... it should read() the socket
1420 */
1421#if 0
1422 if (so->so_snd.sb_flags & SB_NOTIFY)
1423 sowwakeup(so);
1424#endif
1425 tp->snd_una = ti->ti_ack;
1426 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1427 tp->snd_nxt = tp->snd_una;
1428
1429 switch (tp->t_state)
1430 {
1431 /*
1432 * In FIN_WAIT_1 STATE in addition to the processing
1433 * for the ESTABLISHED state if our FIN is now acknowledged
1434 * then enter FIN_WAIT_2.
1435 */
1436 case TCPS_FIN_WAIT_1:
1437 if (ourfinisacked)
1438 {
1439 /*
1440 * If we can't receive any more
1441 * data, then closing user can proceed.
1442 * Starting the timer is contrary to the
1443 * specification, but if we don't get a FIN
1444 * we'll hang forever.
1445 */
1446 if (so->so_state & SS_FCANTRCVMORE)
1447 {
1448 soisfdisconnected(so);
1449 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1450 }
1451 TCP_STATE_SWITCH_TO(tp, TCPS_FIN_WAIT_2);
1452 }
1453 break;
1454
1455 /*
1456 * In CLOSING STATE in addition to the processing for
1457 * the ESTABLISHED state if the ACK acknowledges our FIN
1458 * then enter the TIME-WAIT state, otherwise ignore
1459 * the segment.
1460 */
1461 case TCPS_CLOSING:
1462 if (ourfinisacked)
1463 {
1464 TCP_STATE_SWITCH_TO(tp, TCPS_TIME_WAIT);
1465 tcp_canceltimers(tp);
1466 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1467 soisfdisconnected(so);
1468 }
1469 break;
1470
1471 /*
1472 * In LAST_ACK, we may still be waiting for data to drain
1473 * and/or to be acked, as well as for the ack of our FIN.
1474 * If our FIN is now acknowledged, delete the TCB,
1475 * enter the closed state and return.
1476 */
1477 case TCPS_LAST_ACK:
1478 if (ourfinisacked)
1479 {
1480 tp = tcp_close(pData, tp);
1481 LogFlowFunc(("%d -> drop\n", __LINE__));
1482 goto drop;
1483 }
1484 break;
1485
1486 /*
1487 * In TIME_WAIT state the only thing that should arrive
1488 * is a retransmission of the remote FIN. Acknowledge
1489 * it and restart the finack timer.
1490 */
1491 case TCPS_TIME_WAIT:
1492 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1493 LogFlowFunc(("%d -> dropafterack\n", __LINE__));
1494 goto dropafterack;
1495 }
1496 } /* switch(tp->t_state) */
1497
1498step6:
1499 LogFlowFunc(("step6:\n"));
1500 /*
1501 * Update window information.
1502 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1503 */
1504 if ( (tiflags & TH_ACK)
1505 && ( SEQ_LT(tp->snd_wl1, ti->ti_seq)
1506 || ( tp->snd_wl1 == ti->ti_seq
1507 && ( SEQ_LT(tp->snd_wl2, ti->ti_ack)
1508 || ( tp->snd_wl2 == ti->ti_ack
1509 && tiwin > tp->snd_wnd)))))
1510 {
1511 /* keep track of pure window updates */
1512 if ( ti->ti_len == 0
1513 && tp->snd_wl2 == ti->ti_ack
1514 && tiwin > tp->snd_wnd)
1515 tcpstat.tcps_rcvwinupd++;
1516 tp->snd_wnd = tiwin;
1517 tp->snd_wl1 = ti->ti_seq;
1518 tp->snd_wl2 = ti->ti_ack;
1519 if (tp->snd_wnd > tp->max_sndwnd)
1520 tp->max_sndwnd = tp->snd_wnd;
1521 needoutput = 1;
1522 }
1523
1524 /*
1525 * Process segments with URG.
1526 */
1527 if ((tiflags & TH_URG) && ti->ti_urp &&
1528 TCPS_HAVERCVDFIN(tp->t_state) == 0)
1529 {
1530 /* BSD's sbufs are auto extent so we shouldn't worry here */
1531#ifndef VBOX_WITH_SLIRP_BSD_SBUF
1532 /*
1533 * This is a kludge, but if we receive and accept
1534 * random urgent pointers, we'll crash in
1535 * soreceive. It's hard to imagine someone
1536 * actually wanting to send this much urgent data.
1537 */
1538 if (ti->ti_urp + so->so_rcv.sb_cc > so->so_rcv.sb_datalen)
1539 {
1540 ti->ti_urp = 0;
1541 tiflags &= ~TH_URG;
1542 LogFlowFunc(("%d -> dodata\n", __LINE__));
1543 goto dodata;
1544 }
1545#endif
1546 /*
1547 * If this segment advances the known urgent pointer,
1548 * then mark the data stream. This should not happen
1549 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1550 * a FIN has been received from the remote side.
1551 * In these states we ignore the URG.
1552 *
1553 * According to RFC961 (Assigned Protocols),
1554 * the urgent pointer points to the last octet
1555 * of urgent data. We continue, however,
1556 * to consider it to indicate the first octet
1557 * of data past the urgent section as the original
1558 * spec states (in one of two places).
1559 */
1560 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up))
1561 {
1562 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1563 so->so_urgc = SBUF_LEN(&so->so_rcv) +
1564 (tp->rcv_up - tp->rcv_nxt); /* -1; */
1565 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1566 }
1567 }
1568 else
1569 /*
1570 * If no out of band data is expected,
1571 * pull receive urgent pointer along
1572 * with the receive window.
1573 */
1574 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1575 tp->rcv_up = tp->rcv_nxt;
1576dodata:
1577 LogFlowFunc(("dodata:\n"));
1578
1579 /*
1580 * If this is a small packet, then ACK now - with Nagel
1581 * congestion avoidance sender won't send more until
1582 * he gets an ACK.
1583 *
1584 * See above.
1585 */
1586 if ( ti->ti_len
1587 && (unsigned)ti->ti_len <= 5
1588 && ((struct tcpiphdr_2 *)ti)->first_char == (char)27)
1589 {
1590 tp->t_flags |= TF_ACKNOW;
1591 }
1592
1593 /*
1594 * Process the segment text, merging it into the TCP sequencing queue,
1595 * and arranging for acknowledgment of receipt if necessary.
1596 * This process logically involves adjusting tp->rcv_wnd as data
1597 * is presented to the user (this happens in tcp_usrreq.c,
1598 * case PRU_RCVD). If a FIN has already been received on this
1599 * connection then we just ignore the text.
1600 */
1601 if ( (ti->ti_len || (tiflags&TH_FIN))
1602 && TCPS_HAVERCVDFIN(tp->t_state) == 0)
1603 {
1604 if ( ti->ti_seq == tp->rcv_nxt
1605 && LIST_EMPTY(&tp->t_segq)
1606 && tp->t_state == TCPS_ESTABLISHED)
1607 {
1608 DELAY_ACK(tp, ti); /* little bit different from BSD declaration see netinet/tcp_input.c */
1609 tp->rcv_nxt += tlen;
1610 tiflags = ti->ti_t.th_flags & TH_FIN;
1611 tcpstat.tcps_rcvpack++;
1612 tcpstat.tcps_rcvbyte += tlen;
1613 if (so->so_state & SS_FCANTRCVMORE)
1614 m_freem(pData, m);
1615 else
1616 sbappend(pData, so, m);
1617 }
1618 else
1619 {
1620 tiflags = tcp_reass(pData, tp, &ti->ti_t, &tlen, m);
1621 tiflags |= TF_ACKNOW;
1622 }
1623 /*
1624 * Note the amount of data that peer has sent into
1625 * our window, in order to estimate the sender's
1626 * buffer size.
1627 */
1628 len = SBUF_SIZE(&so->so_rcv) - (tp->rcv_adv - tp->rcv_nxt);
1629 }
1630 else
1631 {
1632 m_freem(pData, m);
1633 tiflags &= ~TH_FIN;
1634 }
1635
1636 /*
1637 * If FIN is received ACK the FIN and let the user know
1638 * that the connection is closing.
1639 */
1640 if (tiflags & TH_FIN)
1641 {
1642 if (TCPS_HAVERCVDFIN(tp->t_state) == 0)
1643 {
1644 /*
1645 * If we receive a FIN we can't send more data,
1646 * set it SS_FDRAIN
1647 * Shutdown the socket if there is no rx data in the
1648 * buffer.
1649 * soread() is called on completion of shutdown() and
1650 * will got to TCPS_LAST_ACK, and use tcp_output()
1651 * to send the FIN.
1652 */
1653/* sofcantrcvmore(so); */
1654 sofwdrain(so);
1655
1656 tp->t_flags |= TF_ACKNOW;
1657 tp->rcv_nxt++;
1658 }
1659 switch (tp->t_state)
1660 {
1661 /*
1662 * In SYN_RECEIVED and ESTABLISHED STATES
1663 * enter the CLOSE_WAIT state.
1664 */
1665 case TCPS_SYN_RECEIVED:
1666 case TCPS_ESTABLISHED:
1667 TCP_STATE_SWITCH_TO(tp, TCPS_CLOSE_WAIT);
1668 break;
1669
1670 /*
1671 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1672 * enter the CLOSING state.
1673 */
1674 case TCPS_FIN_WAIT_1:
1675 TCP_STATE_SWITCH_TO(tp, TCPS_CLOSING);
1676 break;
1677
1678 /*
1679 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1680 * starting the time-wait timer, turning off the other
1681 * standard timers.
1682 */
1683 case TCPS_FIN_WAIT_2:
1684 TCP_STATE_SWITCH_TO(tp, TCPS_TIME_WAIT);
1685 tcp_canceltimers(tp);
1686 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1687 soisfdisconnected(so);
1688 break;
1689
1690 /*
1691 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1692 */
1693 case TCPS_TIME_WAIT:
1694 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1695 break;
1696 }
1697 }
1698
1699 /*
1700 * Return any desired output.
1701 */
1702 if (needoutput || (tp->t_flags & TF_ACKNOW))
1703 tcp_output(pData, tp);
1704
1705 SOCKET_UNLOCK(so);
1706 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
1707 LogFlowFuncLeave();
1708 return;
1709
1710dropafterack:
1711 LogFlowFunc(("dropafterack:\n"));
1712 /*
1713 * Generate an ACK dropping incoming segment if it occupies
1714 * sequence space, where the ACK reflects our state.
1715 */
1716 if (tiflags & TH_RST)
1717 {
1718 LogFlowFunc(("%d -> drop\n", __LINE__));
1719 goto drop;
1720 }
1721 m_freem(pData, m);
1722 tp->t_flags |= TF_ACKNOW;
1723 (void) tcp_output(pData, tp);
1724 SOCKET_UNLOCK(so);
1725 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
1726 LogFlowFuncLeave();
1727 return;
1728
1729dropwithreset:
1730 LogFlowFunc(("dropwithreset:\n"));
1731 /* reuses m if m!=NULL, m_free() unnecessary */
1732 if (tiflags & TH_ACK)
1733 tcp_respond(pData, tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1734 else
1735 {
1736 if (tiflags & TH_SYN)
1737 ti->ti_len++;
1738 tcp_respond(pData, tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1739 TH_RST|TH_ACK);
1740 }
1741
1742 if (so != &tcb)
1743 SOCKET_UNLOCK(so);
1744 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
1745 LogFlowFuncLeave();
1746 return;
1747
1748drop:
1749 LogFlowFunc(("drop:\n"));
1750 /*
1751 * Drop space held by incoming segment and return.
1752 */
1753 m_freem(pData, m);
1754
1755#ifdef VBOX_WITH_SLIRP_MT
1756 if (RTCritSectIsOwned(&so->so_mutex))
1757 {
1758 SOCKET_UNLOCK(so);
1759 }
1760#endif
1761
1762 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
1763 LogFlowFuncLeave();
1764 return;
1765}
1766
1767void
1768tcp_dooptions(PNATState pData, struct tcpcb *tp, u_char *cp, int cnt, struct tcpiphdr *ti)
1769{
1770 u_int16_t mss;
1771 int opt, optlen;
1772
1773 LogFlowFunc(("tcp_dooptions: tp = %R[tcpcb793], cnt=%i\n", tp, cnt));
1774
1775 for (; cnt > 0; cnt -= optlen, cp += optlen)
1776 {
1777 opt = cp[0];
1778 if (opt == TCPOPT_EOL)
1779 break;
1780 if (opt == TCPOPT_NOP)
1781 optlen = 1;
1782 else
1783 {
1784 optlen = cp[1];
1785 if (optlen <= 0)
1786 break;
1787 }
1788 switch (opt)
1789 {
1790 default:
1791 continue;
1792
1793 case TCPOPT_MAXSEG:
1794 if (optlen != TCPOLEN_MAXSEG)
1795 continue;
1796 if (!(ti->ti_flags & TH_SYN))
1797 continue;
1798 memcpy((char *) &mss, (char *) cp + 2, sizeof(mss));
1799 NTOHS(mss);
1800 (void) tcp_mss(pData, tp, mss); /* sets t_maxseg */
1801 break;
1802
1803#if 0
1804 case TCPOPT_WINDOW:
1805 if (optlen != TCPOLEN_WINDOW)
1806 continue;
1807 if (!(ti->ti_flags & TH_SYN))
1808 continue;
1809 tp->t_flags |= TF_RCVD_SCALE;
1810 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
1811 break;
1812
1813 case TCPOPT_TIMESTAMP:
1814 if (optlen != TCPOLEN_TIMESTAMP)
1815 continue;
1816 *ts_present = 1;
1817 memcpy((char *) ts_val, (char *)cp + 2, sizeof(*ts_val));
1818 NTOHL(*ts_val);
1819 memcpy((char *) ts_ecr, (char *)cp + 6, sizeof(*ts_ecr));
1820 NTOHL(*ts_ecr);
1821
1822 /*
1823 * A timestamp received in a SYN makes
1824 * it ok to send timestamp requests and replies.
1825 */
1826 if (ti->ti_flags & TH_SYN)
1827 {
1828 tp->t_flags |= TF_RCVD_TSTMP;
1829 tp->ts_recent = *ts_val;
1830 tp->ts_recent_age = tcp_now;
1831 }
1832 break;
1833#endif
1834 }
1835 }
1836}
1837
1838
1839/*
1840 * Pull out of band byte out of a segment so
1841 * it doesn't appear in the user's data queue.
1842 * It is still reflected in the segment length for
1843 * sequencing purposes.
1844 */
1845
1846#if 0
1847void
1848tcp_pulloutofband(struct socket *so, struct tcpiphdr *ti, struct mbuf *m)
1849{
1850 int cnt = ti->ti_urp - 1;
1851
1852 while (cnt >= 0)
1853 {
1854 if (m->m_len > cnt)
1855 {
1856 char *cp = mtod(m, caddr_t) + cnt;
1857 struct tcpcb *tp = sototcpcb(so);
1858
1859 tp->t_iobc = *cp;
1860 tp->t_oobflags |= TCPOOB_HAVEDATA;
1861 memcpy(sp, cp+1, (unsigned)(m->m_len - cnt - 1));
1862 m->m_len--;
1863 return;
1864 }
1865 cnt -= m->m_len;
1866 m = m->m_next; /* XXX WRONG! Fix it! */
1867 if (m == 0)
1868 break;
1869 }
1870 panic("tcp_pulloutofband");
1871}
1872#endif
1873
1874/*
1875 * Collect new round-trip time estimate
1876 * and update averages and current timeout.
1877 */
1878
1879void
1880tcp_xmit_timer(PNATState pData, register struct tcpcb *tp, int rtt)
1881{
1882 register short delta;
1883
1884 LogFlowFunc(("ENTER: tcp_xmit_timer: tp = %R[tcpcb793] rtt = %d\n", tp, rtt));
1885
1886 tcpstat.tcps_rttupdated++;
1887 if (tp->t_srtt != 0)
1888 {
1889 /*
1890 * srtt is stored as fixed point with 3 bits after the
1891 * binary point (i.e., scaled by 8). The following magic
1892 * is equivalent to the smoothing algorithm in rfc793 with
1893 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1894 * point). Adjust rtt to origin 0.
1895 */
1896 delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1897 if ((tp->t_srtt += delta) <= 0)
1898 tp->t_srtt = 1;
1899 /*
1900 * We accumulate a smoothed rtt variance (actually, a
1901 * smoothed mean difference), then set the retransmit
1902 * timer to smoothed rtt + 4 times the smoothed variance.
1903 * rttvar is stored as fixed point with 2 bits after the
1904 * binary point (scaled by 4). The following is
1905 * equivalent to rfc793 smoothing with an alpha of .75
1906 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1907 * rfc793's wired-in beta.
1908 */
1909 if (delta < 0)
1910 delta = -delta;
1911 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1912 if ((tp->t_rttvar += delta) <= 0)
1913 tp->t_rttvar = 1;
1914 }
1915 else
1916 {
1917 /*
1918 * No rtt measurement yet - use the unsmoothed rtt.
1919 * Set the variance to half the rtt (so our first
1920 * retransmit happens at 3*rtt).
1921 */
1922 tp->t_srtt = rtt << TCP_RTT_SHIFT;
1923 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1924 }
1925 tp->t_rtt = 0;
1926 tp->t_rxtshift = 0;
1927
1928 /*
1929 * the retransmit should happen at rtt + 4 * rttvar.
1930 * Because of the way we do the smoothing, srtt and rttvar
1931 * will each average +1/2 tick of bias. When we compute
1932 * the retransmit timer, we want 1/2 tick of rounding and
1933 * 1 extra tick because of +-1/2 tick uncertainty in the
1934 * firing of the timer. The bias will give us exactly the
1935 * 1.5 tick we need. But, because the bias is
1936 * statistical, we have to test that we don't drop below
1937 * the minimum feasible timer (which is 2 ticks).
1938 */
1939 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1940 (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */
1941
1942 /*
1943 * We received an ack for a packet that wasn't retransmitted;
1944 * it is probably safe to discard any error indications we've
1945 * received recently. This isn't quite right, but close enough
1946 * for now (a route might have failed after we sent a segment,
1947 * and the return path might not be symmetrical).
1948 */
1949 tp->t_softerror = 0;
1950}
1951
1952/*
1953 * Determine a reasonable value for maxseg size.
1954 * If the route is known, check route for mtu.
1955 * If none, use an mss that can be handled on the outgoing
1956 * interface without forcing IP to fragment; if bigger than
1957 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1958 * to utilize large mbufs. If no route is found, route has no mtu,
1959 * or the destination isn't local, use a default, hopefully conservative
1960 * size (usually 512 or the default IP max size, but no more than the mtu
1961 * of the interface), as we can't discover anything about intervening
1962 * gateways or networks. We also initialize the congestion/slow start
1963 * window to be a single segment if the destination isn't local.
1964 * While looking at the routing entry, we also initialize other path-dependent
1965 * parameters from pre-set or cached values in the routing entry.
1966 */
1967
1968int
1969tcp_mss(PNATState pData, register struct tcpcb *tp, u_int offer)
1970{
1971 struct socket *so = tp->t_socket;
1972 int mss;
1973
1974 LogFlowFunc(("ENTER: tcp_mss: tp = %R[tcpcb793], offer = %d\n", tp, offer));
1975
1976 mss = min(if_mtu, if_mru) - sizeof(struct tcpiphdr);
1977 if (offer)
1978 mss = min(mss, offer);
1979 mss = max(mss, 32);
1980 if (mss < tp->t_maxseg || offer != 0)
1981 tp->t_maxseg = mss;
1982
1983 tp->snd_cwnd = mss;
1984
1985#ifndef VBOX_WITH_SLIRP_BSD_SBUF
1986 sbreserve(pData, &so->so_snd, tcp_sndspace+((tcp_sndspace%mss)?(mss-(tcp_sndspace%mss)):0));
1987 sbreserve(pData, &so->so_rcv, tcp_rcvspace+((tcp_rcvspace%mss)?(mss-(tcp_rcvspace%mss)):0));
1988#else
1989 sbuf_new(&so->so_snd, NULL, tcp_sndspace+((tcp_sndspace%mss)?(mss-(tcp_sndspace%mss)):0), SBUF_AUTOEXTEND);
1990 sbuf_new(&so->so_rcv, NULL, tcp_rcvspace+((tcp_rcvspace%mss)?(mss-(tcp_rcvspace%mss)):0), SBUF_AUTOEXTEND);
1991#endif
1992
1993 Log2((" returning mss = %d\n", mss));
1994
1995 return mss;
1996}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette