VirtualBox

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

Last change on this file since 51727 was 51727, checked in by vboxsync, 10 years ago

NAT: Fix edito introduced in r39462 - TF_ACKNOW should be set in
tp->t_flags (pcb flags) not tiflags (TCP header flags). TF_ACKNOW
happens to have the same numeric value as TH_FIN, so the rest of the
function was tricked into thinking we did get a datagram with FIN set.

Due to fortunate numerology it would almost never affect anything, but
with inbound direction half-closed it does affect behaviour: it makes
pcb transition from FIN_WAIT_2 to TIME_WAIT, acking the non-existing
FIN and confusing the peer, that would get an ACK one greater than it
would expect.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 65.0 KB
Line 
1/* $Id: tcp_input.c 51727 2014-06-26 02:05:12Z vboxsync $ */
2/** @file
3 * NAT - TCP input.
4 */
5
6/*
7 * Copyright (C) 2006-2012 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 QSOCKET_UNLOCK(tcb);
463 /* @todo fix SOLOOKUP macrodefinition to be usable here */
464 so = solookup(&tcb, ti->ti_src, ti->ti_sport,
465 ti->ti_dst, ti->ti_dport);
466 if (so)
467 {
468 tcp_last_so = so;
469 }
470 ++tcpstat.tcps_socachemiss;
471 }
472 else
473 {
474 SOCKET_LOCK(so);
475 QSOCKET_UNLOCK(tcb);
476 }
477 LogFlowFunc(("(leave) findso: %R[natsock]\n", so));
478
479 /*
480 * If the state is CLOSED (i.e., TCB does not exist) then
481 * all data in the incoming segment is discarded.
482 * If the TCB exists but is in CLOSED state, it is embryonic,
483 * but should either do a listen or a connect soon.
484 *
485 * state == CLOSED means we've done socreate() but haven't
486 * attached it to a protocol yet...
487 *
488 * XXX If a TCB does not exist, and the TH_SYN flag is
489 * the only flag set, then create a session, mark it
490 * as if it was LISTENING, and continue...
491 */
492 if (so == 0)
493 {
494 if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) != TH_SYN)
495 {
496 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
497 goto dropwithreset;
498 }
499
500 if ((so = socreate()) == NULL)
501 {
502 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
503 goto dropwithreset;
504 }
505 if (tcp_attach(pData, so) < 0)
506 {
507 RTMemFree(so); /* Not sofree (if it failed, it's not insqued) */
508 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
509 goto dropwithreset;
510 }
511 SOCKET_LOCK(so);
512#ifndef VBOX_WITH_SLIRP_BSD_SBUF
513 sbreserve(pData, &so->so_snd, tcp_sndspace);
514 sbreserve(pData, &so->so_rcv, tcp_rcvspace);
515#else
516 sbuf_new(&so->so_snd, NULL, tcp_sndspace, SBUF_AUTOEXTEND);
517 sbuf_new(&so->so_rcv, NULL, tcp_rcvspace, SBUF_AUTOEXTEND);
518#endif
519
520/* tcp_last_so = so; */ /* XXX ? */
521/* tp = sototcpcb(so); */
522
523 so->so_laddr = ti->ti_src;
524 so->so_lport = ti->ti_sport;
525 so->so_faddr = ti->ti_dst;
526 so->so_fport = ti->ti_dport;
527
528 so->so_iptos = ((struct ip *)ti)->ip_tos;
529
530 tp = sototcpcb(so);
531 TCP_STATE_SWITCH_TO(tp, TCPS_LISTEN);
532 }
533
534 /*
535 * If this is a still-connecting socket, this probably
536 * a retransmit of the SYN. Whether it's a retransmit SYN
537 * or something else, we nuke it.
538 */
539 if (so->so_state & SS_ISFCONNECTING)
540 {
541 LogFlowFunc(("%d -> drop\n", __LINE__));
542 goto drop;
543 }
544
545 tp = sototcpcb(so);
546
547 /* XXX Should never fail */
548 if (tp == 0)
549 {
550 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
551 goto dropwithreset;
552 }
553 if (tp->t_state == TCPS_CLOSED)
554 {
555 LogFlowFunc(("%d -> drop\n", __LINE__));
556 goto drop;
557 }
558
559 /* Unscale the window into a 32-bit value. */
560/* if ((tiflags & TH_SYN) == 0)
561 * tiwin = ti->ti_win << tp->snd_scale;
562 * else
563 */
564 tiwin = ti->ti_win;
565
566 /*
567 * Segment received on connection.
568 * Reset idle time and keep-alive timer.
569 */
570 tp->t_idle = 0;
571 if (so_options)
572 tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
573 else
574 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
575
576 /*
577 * Process options if not in LISTEN state,
578 * else do it below (after getting remote address).
579 */
580 if (optp && tp->t_state != TCPS_LISTEN)
581 tcp_dooptions(pData, tp, (u_char *)optp, optlen, ti);
582/* , */
583/* &ts_present, &ts_val, &ts_ecr); */
584
585 /*
586 * Header prediction: check for the two common cases
587 * of a uni-directional data xfer. If the packet has
588 * no control flags, is in-sequence, the window didn't
589 * change and we're not retransmitting, it's a
590 * candidate. If the length is zero and the ack moved
591 * forward, we're the sender side of the xfer. Just
592 * free the data acked & wake any higher level process
593 * that was blocked waiting for space. If the length
594 * is non-zero and the ack didn't move, we're the
595 * receiver side. If we're getting packets in-order
596 * (the reassembly queue is empty), add the data to
597 * the socket buffer and note that we need a delayed ack.
598 *
599 * XXX Some of these tests are not needed
600 * eg: the tiwin == tp->snd_wnd prevents many more
601 * predictions.. with no *real* advantage..
602 */
603 if ( tp->t_state == TCPS_ESTABLISHED
604 && (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK
605/* && (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) */
606 && ti->ti_seq == tp->rcv_nxt
607 && tiwin && tiwin == tp->snd_wnd
608 && tp->snd_nxt == tp->snd_max)
609 {
610 /*
611 * If last ACK falls within this segment's sequence numbers,
612 * record the timestamp.
613 */
614#if 0
615 if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
616 SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len))
617 {
618 tp->ts_recent_age = tcp_now;
619 tp->ts_recent = ts_val;
620 }
621#endif
622
623 if (ti->ti_len == 0)
624 {
625 if ( SEQ_GT(ti->ti_ack, tp->snd_una)
626 && SEQ_LEQ(ti->ti_ack, tp->snd_max)
627 && tp->snd_cwnd >= tp->snd_wnd)
628 {
629 /*
630 * this is a pure ack for outstanding data.
631 */
632 ++tcpstat.tcps_predack;
633#if 0
634 if (ts_present)
635 tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
636 else
637#endif
638 if ( tp->t_rtt
639 && SEQ_GT(ti->ti_ack, tp->t_rtseq))
640 tcp_xmit_timer(pData, tp, tp->t_rtt);
641 acked = ti->ti_ack - tp->snd_una;
642 tcpstat.tcps_rcvackpack++;
643 tcpstat.tcps_rcvackbyte += acked;
644#ifndef VBOX_WITH_SLIRP_BSD_SBUF
645 sbdrop(&so->so_snd, acked);
646#else
647 if (sbuf_len(&so->so_snd) < acked)
648 /* drop all what sbuf have */
649 sbuf_setpos(&so->so_snd, 0);
650 else
651 sbuf_setpos(&so->so_snd, sbuf_len(&so->so_snd) - acked);
652#endif
653 tp->snd_una = ti->ti_ack;
654 m_freem(pData, m);
655
656 /*
657 * If all outstanding data are acked, stop
658 * retransmit timer, otherwise restart timer
659 * using current (possibly backed-off) value.
660 * If process is waiting for space,
661 * wakeup/selwakeup/signal. If data
662 * are ready to send, let tcp_output
663 * decide between more output or persist.
664 */
665 if (tp->snd_una == tp->snd_max)
666 tp->t_timer[TCPT_REXMT] = 0;
667 else if (tp->t_timer[TCPT_PERSIST] == 0)
668 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
669
670 /*
671 * There's room in so_snd, sowwakup will read()
672 * from the socket if we can
673 */
674#if 0
675 if (so->so_snd.sb_flags & SB_NOTIFY)
676 sowwakeup(so);
677#endif
678 /*
679 * This is called because sowwakeup might have
680 * put data into so_snd. Since we don't so sowwakeup,
681 * we don't need this.. XXX???
682 */
683 if (SBUF_LEN(&so->so_snd))
684 (void) tcp_output(pData, tp);
685
686 SOCKET_UNLOCK(so);
687 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
688 return;
689 }
690 }
691 else if ( ti->ti_ack == tp->snd_una
692 && LIST_FIRST(&tp->t_segq)
693 && ti->ti_len <= sbspace(&so->so_rcv))
694 {
695 /*
696 * this is a pure, in-sequence data packet
697 * with nothing on the reassembly queue and
698 * we have enough buffer space to take it.
699 */
700 ++tcpstat.tcps_preddat;
701 tp->rcv_nxt += ti->ti_len;
702 tcpstat.tcps_rcvpack++;
703 tcpstat.tcps_rcvbyte += ti->ti_len;
704 /*
705 * Add data to socket buffer.
706 */
707 sbappend(pData, so, m);
708
709 /*
710 * XXX This is called when data arrives. Later, check
711 * if we can actually write() to the socket
712 * XXX Need to check? It's be NON_BLOCKING
713 */
714/* sorwakeup(so); */
715
716 /*
717 * If this is a short packet, then ACK now - with Nagel
718 * congestion avoidance sender won't send more until
719 * he gets an ACK.
720 *
721 * It is better to not delay acks at all to maximize
722 * TCP throughput. See RFC 2581.
723 */
724 tp->t_flags |= TF_ACKNOW;
725 tcp_output(pData, tp);
726 SOCKET_UNLOCK(so);
727 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
728 return;
729 }
730 } /* header prediction */
731 /*
732 * Calculate amount of space in receive window,
733 * and then do TCP input processing.
734 * Receive window is amount of space in rcv queue,
735 * but not less than advertised window.
736 */
737 {
738 int win;
739 win = sbspace(&so->so_rcv);
740 if (win < 0)
741 win = 0;
742 tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
743 }
744
745 switch (tp->t_state)
746 {
747 /*
748 * If the state is LISTEN then ignore segment if it contains an RST.
749 * If the segment contains an ACK then it is bad and send a RST.
750 * If it does not contain a SYN then it is not interesting; drop it.
751 * Don't bother responding if the destination was a broadcast.
752 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
753 * tp->iss, and send a segment:
754 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
755 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
756 * Fill in remote peer address fields if not previously specified.
757 * Enter SYN_RECEIVED state, and process any other fields of this
758 * segment in this state.
759 */
760 case TCPS_LISTEN:
761 {
762 if (tiflags & TH_RST)
763 {
764 LogFlowFunc(("%d -> drop\n", __LINE__));
765 goto drop;
766 }
767 if (tiflags & TH_ACK)
768 {
769 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
770 goto dropwithreset;
771 }
772 if ((tiflags & TH_SYN) == 0)
773 {
774 LogFlowFunc(("%d -> drop\n", __LINE__));
775 goto drop;
776 }
777
778 /*
779 * This has way too many gotos...
780 * But a bit of spaghetti code never hurt anybody :)
781 */
782 if ( (tcp_fconnect(pData, so) == -1)
783 && errno != EINPROGRESS
784 && errno != EWOULDBLOCK)
785 {
786 u_char code = ICMP_UNREACH_NET;
787 Log2((" tcp fconnect errno = %d (%s)\n", errno, strerror(errno)));
788 if (errno == ECONNREFUSED)
789 {
790 /* ACK the SYN, send RST to refuse the connection */
791 tcp_respond(pData, tp, ti, m, ti->ti_seq+1, (tcp_seq)0,
792 TH_RST|TH_ACK);
793 }
794 else
795 {
796 if (errno == EHOSTUNREACH)
797 code = ICMP_UNREACH_HOST;
798 HTONL(ti->ti_seq); /* restore tcp header */
799 HTONL(ti->ti_ack);
800 HTONS(ti->ti_win);
801 HTONS(ti->ti_urp);
802 m->m_data -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
803 m->m_len += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
804 *ip = save_ip;
805 icmp_error(pData, m, ICMP_UNREACH, code, 0, strerror(errno));
806 tp->t_socket->so_m = NULL;
807 }
808 tp = tcp_close(pData, tp);
809 }
810 else
811 {
812 /*
813 * Haven't connected yet, save the current mbuf
814 * and ti, and return
815 * XXX Some OS's don't tell us whether the connect()
816 * succeeded or not. So we must time it out.
817 */
818 so->so_m = m;
819 so->so_ti = ti;
820 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
821 TCP_STATE_SWITCH_TO(tp, TCPS_SYN_RECEIVED);
822 }
823 SOCKET_UNLOCK(so);
824 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
825 LogFlowFuncLeave();
826 return;
827
828cont_conn:
829 /* m==NULL
830 * Check if the connect succeeded
831 */
832 LogFlowFunc(("cont_conn:\n"));
833 if (so->so_state & SS_NOFDREF)
834 {
835 tp = tcp_close(pData, tp);
836 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
837 goto dropwithreset;
838 }
839
840 tcp_template(tp);
841
842 if (optp)
843 tcp_dooptions(pData, tp, (u_char *)optp, optlen, ti);
844
845 if (iss)
846 tp->iss = iss;
847 else
848 tp->iss = tcp_iss;
849 tcp_iss += TCP_ISSINCR/2;
850 tp->irs = ti->ti_seq;
851 tcp_sendseqinit(tp);
852 tcp_rcvseqinit(tp);
853 tp->t_flags |= TF_ACKNOW;
854 TCP_STATE_SWITCH_TO(tp, TCPS_SYN_RECEIVED);
855 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
856 tcpstat.tcps_accepts++;
857 LogFlowFunc(("%d -> trimthenstep6\n", __LINE__));
858 goto trimthenstep6;
859 } /* case TCPS_LISTEN */
860
861 /*
862 * If the state is SYN_SENT:
863 * if seg contains an ACK, but not for our SYN, drop the input.
864 * if seg contains a RST, then drop the connection.
865 * if seg does not contain SYN, then drop it.
866 * Otherwise this is an acceptable SYN segment
867 * initialize tp->rcv_nxt and tp->irs
868 * if seg contains ack then advance tp->snd_una
869 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
870 * arrange for segment to be acked (eventually)
871 * continue processing rest of data/controls, beginning with URG
872 */
873 case TCPS_SYN_SENT:
874 if ( (tiflags & TH_ACK)
875 && ( SEQ_LEQ(ti->ti_ack, tp->iss)
876 || SEQ_GT(ti->ti_ack, tp->snd_max)))
877 {
878 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
879 goto dropwithreset;
880 }
881
882 if (tiflags & TH_RST)
883 {
884 if (tiflags & TH_ACK)
885 tp = tcp_drop(pData, tp, 0); /* XXX Check t_softerror! */
886 LogFlowFunc(("%d -> drop\n", __LINE__));
887 goto drop;
888 }
889
890 if ((tiflags & TH_SYN) == 0)
891 {
892 LogFlowFunc(("%d -> drop\n", __LINE__));
893 goto drop;
894 }
895 if (tiflags & TH_ACK)
896 {
897 tp->snd_una = ti->ti_ack;
898 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
899 tp->snd_nxt = tp->snd_una;
900 }
901
902 tp->t_timer[TCPT_REXMT] = 0;
903 tp->irs = ti->ti_seq;
904 tcp_rcvseqinit(tp);
905 tp->t_flags |= TF_ACKNOW;
906 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss))
907 {
908 tcpstat.tcps_connects++;
909 soisfconnected(so);
910 TCP_STATE_SWITCH_TO(tp, TCPS_ESTABLISHED);
911
912 /* Do window scaling on this connection? */
913#if 0
914 if (( tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE))
915 == (TF_RCVD_SCALE|TF_REQ_SCALE))
916 {
917 tp->snd_scale = tp->requested_s_scale;
918 tp->rcv_scale = tp->request_r_scale;
919 }
920#endif
921 (void) tcp_reass(pData, tp, (struct tcphdr *)0, NULL, (struct mbuf *)0);
922 /*
923 * if we didn't have to retransmit the SYN,
924 * use its rtt as our initial srtt & rtt var.
925 */
926 if (tp->t_rtt)
927 tcp_xmit_timer(pData, tp, tp->t_rtt);
928 }
929 else
930 TCP_STATE_SWITCH_TO(tp, TCPS_SYN_RECEIVED);
931
932trimthenstep6:
933 LogFlowFunc(("trimthenstep6:\n"));
934 /*
935 * Advance ti->ti_seq to correspond to first data byte.
936 * If data, trim to stay within window,
937 * dropping FIN if necessary.
938 */
939 ti->ti_seq++;
940 if (ti->ti_len > tp->rcv_wnd)
941 {
942 todrop = ti->ti_len - tp->rcv_wnd;
943 m_adj(m, -todrop);
944 ti->ti_len = tp->rcv_wnd;
945 tiflags &= ~TH_FIN;
946 tcpstat.tcps_rcvpackafterwin++;
947 tcpstat.tcps_rcvbyteafterwin += todrop;
948 }
949 tp->snd_wl1 = ti->ti_seq - 1;
950 tp->rcv_up = ti->ti_seq;
951 LogFlowFunc(("%d -> step6\n", __LINE__));
952 goto step6;
953 } /* switch tp->t_state */
954 /*
955 * States other than LISTEN or SYN_SENT.
956 * First check timestamp, if present.
957 * Then check that at least some bytes of segment are within
958 * receive window. If segment begins before rcv_nxt,
959 * drop leading data (and SYN); if nothing left, just ack.
960 *
961 * RFC 1323 PAWS: If we have a timestamp reply on this segment
962 * and it's less than ts_recent, drop it.
963 */
964#if 0
965 if ( ts_present
966 && (tiflags & TH_RST) == 0
967 && tp->ts_recent
968 && TSTMP_LT(ts_val, tp->ts_recent))
969 {
970 /* Check to see if ts_recent is over 24 days old. */
971 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE)
972 {
973 /*
974 * Invalidate ts_recent. If this segment updates
975 * ts_recent, the age will be reset later and ts_recent
976 * will get a valid value. If it does not, setting
977 * ts_recent to zero will at least satisfy the
978 * requirement that zero be placed in the timestamp
979 * echo reply when ts_recent isn't valid. The
980 * age isn't reset until we get a valid ts_recent
981 * because we don't want out-of-order segments to be
982 * dropped when ts_recent is old.
983 */
984 tp->ts_recent = 0;
985 }
986 else
987 {
988 tcpstat.tcps_rcvduppack++;
989 tcpstat.tcps_rcvdupbyte += ti->ti_len;
990 tcpstat.tcps_pawsdrop++;
991 goto dropafterack;
992 }
993 }
994#endif
995
996 todrop = tp->rcv_nxt - ti->ti_seq;
997 if (todrop > 0)
998 {
999 if (tiflags & TH_SYN)
1000 {
1001 tiflags &= ~TH_SYN;
1002 ti->ti_seq++;
1003 if (ti->ti_urp > 1)
1004 ti->ti_urp--;
1005 else
1006 tiflags &= ~TH_URG;
1007 todrop--;
1008 }
1009 /*
1010 * Following if statement from Stevens, vol. 2, p. 960.
1011 */
1012 if ( todrop > ti->ti_len
1013 || ( todrop == ti->ti_len
1014 && (tiflags & TH_FIN) == 0))
1015 {
1016 /*
1017 * Any valid FIN must be to the left of the window.
1018 * At this point the FIN must be a duplicate or out
1019 * of sequence; drop it.
1020 */
1021 tiflags &= ~TH_FIN;
1022
1023 /*
1024 * Send an ACK to resynchronize and drop any data.
1025 * But keep on processing for RST or ACK.
1026 */
1027 tp->t_flags |= TF_ACKNOW;
1028 todrop = ti->ti_len;
1029 tcpstat.tcps_rcvduppack++;
1030 tcpstat.tcps_rcvdupbyte += todrop;
1031 }
1032 else
1033 {
1034 tcpstat.tcps_rcvpartduppack++;
1035 tcpstat.tcps_rcvpartdupbyte += todrop;
1036 }
1037 m_adj(m, todrop);
1038 ti->ti_seq += todrop;
1039 ti->ti_len -= todrop;
1040 if (ti->ti_urp > todrop)
1041 ti->ti_urp -= todrop;
1042 else
1043 {
1044 tiflags &= ~TH_URG;
1045 ti->ti_urp = 0;
1046 }
1047 }
1048 /*
1049 * If new data are received on a connection after the
1050 * user processes are gone, then RST the other end.
1051 */
1052 if ( (so->so_state & SS_NOFDREF)
1053 && tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len)
1054 {
1055 tp = tcp_close(pData, tp);
1056 tcpstat.tcps_rcvafterclose++;
1057 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
1058 goto dropwithreset;
1059 }
1060
1061 /*
1062 * If segment ends after window, drop trailing data
1063 * (and PUSH and FIN); if nothing left, just ACK.
1064 */
1065 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
1066 if (todrop > 0)
1067 {
1068 tcpstat.tcps_rcvpackafterwin++;
1069 if (todrop >= ti->ti_len)
1070 {
1071 tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
1072 /*
1073 * If a new connection request is received
1074 * while in TIME_WAIT, drop the old connection
1075 * and start over if the sequence numbers
1076 * are above the previous ones.
1077 */
1078 if ( tiflags & TH_SYN
1079 && tp->t_state == TCPS_TIME_WAIT
1080 && SEQ_GT(ti->ti_seq, tp->rcv_nxt))
1081 {
1082 iss = tp->rcv_nxt + TCP_ISSINCR;
1083 tp = tcp_close(pData, tp);
1084 SOCKET_UNLOCK(tp->t_socket);
1085 LogFlowFunc(("%d -> findso\n", __LINE__));
1086 goto findso;
1087 }
1088 /*
1089 * If window is closed can only take segments at
1090 * window edge, and have to drop data and PUSH from
1091 * incoming segments. Continue processing, but
1092 * remember to ack. Otherwise, drop segment
1093 * and ack.
1094 */
1095 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt)
1096 {
1097 tp->t_flags |= TF_ACKNOW;
1098 tcpstat.tcps_rcvwinprobe++;
1099 }
1100 else
1101 {
1102 LogFlowFunc(("%d -> dropafterack\n", __LINE__));
1103 goto dropafterack;
1104 }
1105 }
1106 else
1107 tcpstat.tcps_rcvbyteafterwin += todrop;
1108 m_adj(m, -todrop);
1109 ti->ti_len -= todrop;
1110 tiflags &= ~(TH_PUSH|TH_FIN);
1111 }
1112
1113 /*
1114 * If last ACK falls within this segment's sequence numbers,
1115 * record its timestamp.
1116 */
1117#if 0
1118 if ( ts_present
1119 && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)
1120 && SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len + ((tiflags & (TH_SYN|TH_FIN)) != 0)))
1121 {
1122 tp->ts_recent_age = tcp_now;
1123 tp->ts_recent = ts_val;
1124 }
1125#endif
1126
1127 /*
1128 * If the RST bit is set examine the state:
1129 * SYN_RECEIVED STATE:
1130 * If passive open, return to LISTEN state.
1131 * If active open, inform user that connection was refused.
1132 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1133 * Inform user that connection was reset, and close tcb.
1134 * CLOSING, LAST_ACK, TIME_WAIT STATES
1135 * Close the tcb.
1136 */
1137 if (tiflags&TH_RST)
1138 switch (tp->t_state)
1139 {
1140 case TCPS_SYN_RECEIVED:
1141/* so->so_error = ECONNREFUSED; */
1142 LogFlowFunc(("%d -> close\n", __LINE__));
1143 goto close;
1144
1145 case TCPS_ESTABLISHED:
1146 case TCPS_FIN_WAIT_1:
1147 case TCPS_FIN_WAIT_2:
1148 case TCPS_CLOSE_WAIT:
1149/* so->so_error = ECONNRESET; */
1150close:
1151 LogFlowFunc(("close:\n"));
1152 TCP_STATE_SWITCH_TO(tp, TCPS_CLOSED);
1153 tcpstat.tcps_drops++;
1154 tp = tcp_close(pData, tp);
1155 LogFlowFunc(("%d -> drop\n", __LINE__));
1156 goto drop;
1157
1158 case TCPS_CLOSING:
1159 case TCPS_LAST_ACK:
1160 case TCPS_TIME_WAIT:
1161 tp = tcp_close(pData, tp);
1162 LogFlowFunc(("%d -> drop\n", __LINE__));
1163 goto drop;
1164 }
1165
1166 /*
1167 * If a SYN is in the window, then this is an
1168 * error and we send an RST and drop the connection.
1169 */
1170 if (tiflags & TH_SYN)
1171 {
1172 tp = tcp_drop(pData, tp, 0);
1173 LogFlowFunc(("%d -> dropwithreset\n", __LINE__));
1174 goto dropwithreset;
1175 }
1176
1177 /*
1178 * If the ACK bit is off we drop the segment and return.
1179 */
1180 if ((tiflags & TH_ACK) == 0)
1181 {
1182 LogFlowFunc(("%d -> drop\n", __LINE__));
1183 goto drop;
1184 }
1185
1186 /*
1187 * Ack processing.
1188 */
1189 switch (tp->t_state)
1190 {
1191 /*
1192 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
1193 * ESTABLISHED state and continue processing, otherwise
1194 * send an RST. una<=ack<=max
1195 */
1196 case TCPS_SYN_RECEIVED:
1197 LogFlowFunc(("%d -> TCPS_SYN_RECEIVED\n", __LINE__));
1198 if ( SEQ_GT(tp->snd_una, ti->ti_ack)
1199 || SEQ_GT(ti->ti_ack, tp->snd_max))
1200 goto dropwithreset;
1201 tcpstat.tcps_connects++;
1202 TCP_STATE_SWITCH_TO(tp, TCPS_ESTABLISHED);
1203 /*
1204 * The sent SYN is ack'ed with our sequence number +1
1205 * The first data byte already in the buffer will get
1206 * lost if no correction is made. This is only needed for
1207 * SS_CTL since the buffer is empty otherwise.
1208 * tp->snd_una++; or:
1209 */
1210 tp->snd_una = ti->ti_ack;
1211 soisfconnected(so);
1212
1213 /* Do window scaling? */
1214#if 0
1215 if ( (tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE))
1216 == (TF_RCVD_SCALE|TF_REQ_SCALE))
1217 {
1218 tp->snd_scale = tp->requested_s_scale;
1219 tp->rcv_scale = tp->request_r_scale;
1220 }
1221#endif
1222 (void) tcp_reass(pData, tp, (struct tcphdr *)0, (int *)0, (struct mbuf *)0);
1223 tp->snd_wl1 = ti->ti_seq - 1;
1224 /* Avoid ack processing; snd_una==ti_ack => dup ack */
1225 LogFlowFunc(("%d -> synrx_to_est\n", __LINE__));
1226 goto synrx_to_est;
1227 /* fall into ... */
1228
1229 /*
1230 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1231 * ACKs. If the ack is in the range
1232 * tp->snd_una < ti->ti_ack <= tp->snd_max
1233 * then advance tp->snd_una to ti->ti_ack and drop
1234 * data from the retransmission queue. If this ACK reflects
1235 * more up to date window information we update our window information.
1236 */
1237 case TCPS_ESTABLISHED:
1238 case TCPS_FIN_WAIT_1:
1239 case TCPS_FIN_WAIT_2:
1240 case TCPS_CLOSE_WAIT:
1241 case TCPS_CLOSING:
1242 case TCPS_LAST_ACK:
1243 case TCPS_TIME_WAIT:
1244 LogFlowFunc(("%d -> TCPS_ESTABLISHED|TCPS_FIN_WAIT_1|TCPS_FIN_WAIT_2|TCPS_CLOSE_WAIT|"
1245 "TCPS_CLOSING|TCPS_LAST_ACK|TCPS_TIME_WAIT\n", __LINE__));
1246 if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
1247 {
1248 if (ti->ti_len == 0 && tiwin == tp->snd_wnd)
1249 {
1250 tcpstat.tcps_rcvdupack++;
1251 Log2((" dup ack m = %lx, so = %lx\n", (long)m, (long)so));
1252 /*
1253 * If we have outstanding data (other than
1254 * a window probe), this is a completely
1255 * duplicate ack (ie, window info didn't
1256 * change), the ack is the biggest we've
1257 * seen and we've seen exactly our rexmt
1258 * threshold of them, assume a packet
1259 * has been dropped and retransmit it.
1260 * Kludge snd_nxt & the congestion
1261 * window so we send only this one
1262 * packet.
1263 *
1264 * We know we're losing at the current
1265 * window size so do congestion avoidance
1266 * (set ssthresh to half the current window
1267 * and pull our congestion window back to
1268 * the new ssthresh).
1269 *
1270 * Dup acks mean that packets have left the
1271 * network (they're now cached at the receiver)
1272 * so bump cwnd by the amount in the receiver
1273 * to keep a constant cwnd packets in the
1274 * network.
1275 */
1276 if ( tp->t_timer[TCPT_REXMT] == 0
1277 || ti->ti_ack != tp->snd_una)
1278 tp->t_dupacks = 0;
1279 else if (++tp->t_dupacks == tcprexmtthresh)
1280 {
1281 tcp_seq onxt = tp->snd_nxt;
1282 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
1283 if (win < 2)
1284 win = 2;
1285 tp->snd_ssthresh = win * tp->t_maxseg;
1286 tp->t_timer[TCPT_REXMT] = 0;
1287 tp->t_rtt = 0;
1288 tp->snd_nxt = ti->ti_ack;
1289 tp->snd_cwnd = tp->t_maxseg;
1290 (void) tcp_output(pData, tp);
1291 tp->snd_cwnd = tp->snd_ssthresh +
1292 tp->t_maxseg * tp->t_dupacks;
1293 if (SEQ_GT(onxt, tp->snd_nxt))
1294 tp->snd_nxt = onxt;
1295 LogFlowFunc(("%d -> drop\n", __LINE__));
1296 goto drop;
1297 }
1298 else if (tp->t_dupacks > tcprexmtthresh)
1299 {
1300 tp->snd_cwnd += tp->t_maxseg;
1301 (void) tcp_output(pData, tp);
1302 LogFlowFunc(("%d -> drop\n", __LINE__));
1303 goto drop;
1304 }
1305 }
1306 else
1307 tp->t_dupacks = 0;
1308 break;
1309 }
1310synrx_to_est:
1311 LogFlowFunc(("synrx_to_est:\n"));
1312 /*
1313 * If the congestion window was inflated to account
1314 * for the other side's cached packets, retract it.
1315 */
1316 if ( tp->t_dupacks > tcprexmtthresh
1317 && tp->snd_cwnd > tp->snd_ssthresh)
1318 tp->snd_cwnd = tp->snd_ssthresh;
1319 tp->t_dupacks = 0;
1320 if (SEQ_GT(ti->ti_ack, tp->snd_max))
1321 {
1322 tcpstat.tcps_rcvacktoomuch++;
1323 LogFlowFunc(("%d -> dropafterack\n", __LINE__));
1324 goto dropafterack;
1325 }
1326 acked = ti->ti_ack - tp->snd_una;
1327 tcpstat.tcps_rcvackpack++;
1328 tcpstat.tcps_rcvackbyte += acked;
1329
1330 /*
1331 * If we have a timestamp reply, update smoothed
1332 * round trip time. If no timestamp is present but
1333 * transmit timer is running and timed sequence
1334 * number was acked, update smoothed round trip time.
1335 * Since we now have an rtt measurement, cancel the
1336 * timer backoff (cf., Phil Karn's retransmit alg.).
1337 * Recompute the initial retransmit timer.
1338 */
1339#if 0
1340 if (ts_present)
1341 tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
1342 else
1343#endif
1344 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1345 tcp_xmit_timer(pData, tp, tp->t_rtt);
1346
1347 /*
1348 * If all outstanding data is acked, stop retransmit
1349 * timer and remember to restart (more output or persist).
1350 * If there is more data to be acked, restart retransmit
1351 * timer, using current (possibly backed-off) value.
1352 */
1353 if (ti->ti_ack == tp->snd_max)
1354 {
1355 tp->t_timer[TCPT_REXMT] = 0;
1356 needoutput = 1;
1357 }
1358 else if (tp->t_timer[TCPT_PERSIST] == 0)
1359 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1360 /*
1361 * When new data is acked, open the congestion window.
1362 * If the window gives us less than ssthresh packets
1363 * in flight, open exponentially (maxseg per packet).
1364 * Otherwise open linearly: maxseg per window
1365 * (maxseg^2 / cwnd per packet).
1366 */
1367 {
1368 register u_int cw = tp->snd_cwnd;
1369 register u_int incr = tp->t_maxseg;
1370
1371 if (cw > tp->snd_ssthresh)
1372 incr = incr * incr / cw;
1373 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1374 }
1375 if (acked > SBUF_LEN(&so->so_snd))
1376 {
1377 tp->snd_wnd -= SBUF_LEN(&so->so_snd);
1378#ifndef VBOX_WITH_SLIRP_BSD_SBUF
1379 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1380#else
1381 sbuf_clear(&so->so_snd);
1382#endif
1383 ourfinisacked = 1;
1384 }
1385 else
1386 {
1387#ifndef VBOX_WITH_SLIRP_BSD_SBUF
1388 sbdrop(&so->so_snd, acked);
1389#else
1390 sbuf_setpos(&so->so_snd, sbuf_len(&so->so_snd) - acked);
1391#endif
1392 tp->snd_wnd -= acked;
1393 ourfinisacked = 0;
1394 }
1395 /*
1396 * XXX sowwakup is called when data is acked and there's room for
1397 * for more data... it should read() the socket
1398 */
1399#if 0
1400 if (so->so_snd.sb_flags & SB_NOTIFY)
1401 sowwakeup(so);
1402#endif
1403 tp->snd_una = ti->ti_ack;
1404 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1405 tp->snd_nxt = tp->snd_una;
1406
1407 switch (tp->t_state)
1408 {
1409 /*
1410 * In FIN_WAIT_1 STATE in addition to the processing
1411 * for the ESTABLISHED state if our FIN is now acknowledged
1412 * then enter FIN_WAIT_2.
1413 */
1414 case TCPS_FIN_WAIT_1:
1415 if (ourfinisacked)
1416 {
1417 /*
1418 * If we can't receive any more
1419 * data, then closing user can proceed.
1420 * Starting the timer is contrary to the
1421 * specification, but if we don't get a FIN
1422 * we'll hang forever.
1423 */
1424 if (so->so_state & SS_FCANTRCVMORE)
1425 {
1426 soisfdisconnected(so);
1427 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1428 }
1429 TCP_STATE_SWITCH_TO(tp, TCPS_FIN_WAIT_2);
1430 }
1431 break;
1432
1433 /*
1434 * In CLOSING STATE in addition to the processing for
1435 * the ESTABLISHED state if the ACK acknowledges our FIN
1436 * then enter the TIME-WAIT state, otherwise ignore
1437 * the segment.
1438 */
1439 case TCPS_CLOSING:
1440 if (ourfinisacked)
1441 {
1442 TCP_STATE_SWITCH_TO(tp, TCPS_TIME_WAIT);
1443 tcp_canceltimers(tp);
1444 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1445 soisfdisconnected(so);
1446 }
1447 break;
1448
1449 /*
1450 * In LAST_ACK, we may still be waiting for data to drain
1451 * and/or to be acked, as well as for the ack of our FIN.
1452 * If our FIN is now acknowledged, delete the TCB,
1453 * enter the closed state and return.
1454 */
1455 case TCPS_LAST_ACK:
1456 if (ourfinisacked)
1457 {
1458 tp = tcp_close(pData, tp);
1459 LogFlowFunc(("%d -> drop\n", __LINE__));
1460 goto drop;
1461 }
1462 break;
1463
1464 /*
1465 * In TIME_WAIT state the only thing that should arrive
1466 * is a retransmission of the remote FIN. Acknowledge
1467 * it and restart the finack timer.
1468 */
1469 case TCPS_TIME_WAIT:
1470 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1471 LogFlowFunc(("%d -> dropafterack\n", __LINE__));
1472 goto dropafterack;
1473 }
1474 } /* switch(tp->t_state) */
1475
1476step6:
1477 LogFlowFunc(("step6:\n"));
1478 /*
1479 * Update window information.
1480 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1481 */
1482 if ( (tiflags & TH_ACK)
1483 && ( SEQ_LT(tp->snd_wl1, ti->ti_seq)
1484 || ( tp->snd_wl1 == ti->ti_seq
1485 && ( SEQ_LT(tp->snd_wl2, ti->ti_ack)
1486 || ( tp->snd_wl2 == ti->ti_ack
1487 && tiwin > tp->snd_wnd)))))
1488 {
1489 /* keep track of pure window updates */
1490 if ( ti->ti_len == 0
1491 && tp->snd_wl2 == ti->ti_ack
1492 && tiwin > tp->snd_wnd)
1493 tcpstat.tcps_rcvwinupd++;
1494 tp->snd_wnd = tiwin;
1495 tp->snd_wl1 = ti->ti_seq;
1496 tp->snd_wl2 = ti->ti_ack;
1497 if (tp->snd_wnd > tp->max_sndwnd)
1498 tp->max_sndwnd = tp->snd_wnd;
1499 needoutput = 1;
1500 }
1501
1502 /*
1503 * Process segments with URG.
1504 */
1505 if ((tiflags & TH_URG) && ti->ti_urp &&
1506 TCPS_HAVERCVDFIN(tp->t_state) == 0)
1507 {
1508 /* BSD's sbufs are auto extent so we shouldn't worry here */
1509#ifndef VBOX_WITH_SLIRP_BSD_SBUF
1510 /*
1511 * This is a kludge, but if we receive and accept
1512 * random urgent pointers, we'll crash in
1513 * soreceive. It's hard to imagine someone
1514 * actually wanting to send this much urgent data.
1515 */
1516 if (ti->ti_urp + so->so_rcv.sb_cc > so->so_rcv.sb_datalen)
1517 {
1518 ti->ti_urp = 0;
1519 tiflags &= ~TH_URG;
1520 LogFlowFunc(("%d -> dodata\n", __LINE__));
1521 goto dodata;
1522 }
1523#endif
1524 /*
1525 * If this segment advances the known urgent pointer,
1526 * then mark the data stream. This should not happen
1527 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1528 * a FIN has been received from the remote side.
1529 * In these states we ignore the URG.
1530 *
1531 * According to RFC961 (Assigned Protocols),
1532 * the urgent pointer points to the last octet
1533 * of urgent data. We continue, however,
1534 * to consider it to indicate the first octet
1535 * of data past the urgent section as the original
1536 * spec states (in one of two places).
1537 */
1538 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up))
1539 {
1540 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1541 so->so_urgc = SBUF_LEN(&so->so_rcv) +
1542 (tp->rcv_up - tp->rcv_nxt); /* -1; */
1543 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1544 }
1545 }
1546 else
1547 /*
1548 * If no out of band data is expected,
1549 * pull receive urgent pointer along
1550 * with the receive window.
1551 */
1552 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1553 tp->rcv_up = tp->rcv_nxt;
1554dodata:
1555 LogFlowFunc(("dodata:\n"));
1556
1557 /*
1558 * If this is a small packet, then ACK now - with Nagel
1559 * congestion avoidance sender won't send more until
1560 * he gets an ACK.
1561 *
1562 * See above.
1563 */
1564 if ( ti->ti_len
1565 && (unsigned)ti->ti_len <= 5
1566 && ((struct tcpiphdr_2 *)ti)->first_char == (char)27)
1567 {
1568 tp->t_flags |= TF_ACKNOW;
1569 }
1570
1571 /*
1572 * Process the segment text, merging it into the TCP sequencing queue,
1573 * and arranging for acknowledgment of receipt if necessary.
1574 * This process logically involves adjusting tp->rcv_wnd as data
1575 * is presented to the user (this happens in tcp_usrreq.c,
1576 * case PRU_RCVD). If a FIN has already been received on this
1577 * connection then we just ignore the text.
1578 */
1579 if ( (ti->ti_len || (tiflags&TH_FIN))
1580 && TCPS_HAVERCVDFIN(tp->t_state) == 0)
1581 {
1582 if ( ti->ti_seq == tp->rcv_nxt
1583 && LIST_EMPTY(&tp->t_segq)
1584 && tp->t_state == TCPS_ESTABLISHED)
1585 {
1586 DELAY_ACK(tp, ti); /* little bit different from BSD declaration see netinet/tcp_input.c */
1587 tp->rcv_nxt += tlen;
1588 tiflags = ti->ti_t.th_flags & TH_FIN;
1589 tcpstat.tcps_rcvpack++;
1590 tcpstat.tcps_rcvbyte += tlen;
1591 if (so->so_state & SS_FCANTRCVMORE)
1592 m_freem(pData, m);
1593 else
1594 sbappend(pData, so, m);
1595 }
1596 else
1597 {
1598 tiflags = tcp_reass(pData, tp, &ti->ti_t, &tlen, m);
1599 tp->t_flags |= TF_ACKNOW;
1600 }
1601 /*
1602 * Note the amount of data that peer has sent into
1603 * our window, in order to estimate the sender's
1604 * buffer size.
1605 */
1606 len = SBUF_SIZE(&so->so_rcv) - (tp->rcv_adv - tp->rcv_nxt);
1607 }
1608 else
1609 {
1610 m_freem(pData, m);
1611 tiflags &= ~TH_FIN;
1612 }
1613
1614 /*
1615 * If FIN is received ACK the FIN and let the user know
1616 * that the connection is closing.
1617 */
1618 if (tiflags & TH_FIN)
1619 {
1620 if (TCPS_HAVERCVDFIN(tp->t_state) == 0)
1621 {
1622 /*
1623 * If we receive a FIN we can't send more data,
1624 * set it SS_FDRAIN
1625 * Shutdown the socket if there is no rx data in the
1626 * buffer.
1627 * soread() is called on completion of shutdown() and
1628 * will got to TCPS_LAST_ACK, and use tcp_output()
1629 * to send the FIN.
1630 */
1631/* sofcantrcvmore(so); */
1632 sofwdrain(so);
1633
1634 tp->t_flags |= TF_ACKNOW;
1635 tp->rcv_nxt++;
1636 }
1637 switch (tp->t_state)
1638 {
1639 /*
1640 * In SYN_RECEIVED and ESTABLISHED STATES
1641 * enter the CLOSE_WAIT state.
1642 */
1643 case TCPS_SYN_RECEIVED:
1644 case TCPS_ESTABLISHED:
1645 TCP_STATE_SWITCH_TO(tp, TCPS_CLOSE_WAIT);
1646 break;
1647
1648 /*
1649 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1650 * enter the CLOSING state.
1651 */
1652 case TCPS_FIN_WAIT_1:
1653 TCP_STATE_SWITCH_TO(tp, TCPS_CLOSING);
1654 break;
1655
1656 /*
1657 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1658 * starting the time-wait timer, turning off the other
1659 * standard timers.
1660 */
1661 case TCPS_FIN_WAIT_2:
1662 TCP_STATE_SWITCH_TO(tp, TCPS_TIME_WAIT);
1663 tcp_canceltimers(tp);
1664 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1665 soisfdisconnected(so);
1666 break;
1667
1668 /*
1669 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1670 */
1671 case TCPS_TIME_WAIT:
1672 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1673 break;
1674 }
1675 }
1676
1677 /*
1678 * Return any desired output.
1679 */
1680 if (needoutput || (tp->t_flags & TF_ACKNOW))
1681 tcp_output(pData, tp);
1682
1683 SOCKET_UNLOCK(so);
1684 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
1685 LogFlowFuncLeave();
1686 return;
1687
1688dropafterack:
1689 LogFlowFunc(("dropafterack:\n"));
1690 /*
1691 * Generate an ACK dropping incoming segment if it occupies
1692 * sequence space, where the ACK reflects our state.
1693 */
1694 if (tiflags & TH_RST)
1695 {
1696 LogFlowFunc(("%d -> drop\n", __LINE__));
1697 goto drop;
1698 }
1699 m_freem(pData, m);
1700 tp->t_flags |= TF_ACKNOW;
1701 (void) tcp_output(pData, tp);
1702 SOCKET_UNLOCK(so);
1703 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
1704 LogFlowFuncLeave();
1705 return;
1706
1707dropwithreset:
1708 LogFlowFunc(("dropwithreset:\n"));
1709 /* reuses m if m!=NULL, m_free() unnecessary */
1710 if (tiflags & TH_ACK)
1711 tcp_respond(pData, tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1712 else
1713 {
1714 if (tiflags & TH_SYN)
1715 ti->ti_len++;
1716 tcp_respond(pData, tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1717 TH_RST|TH_ACK);
1718 }
1719
1720 if (so != &tcb)
1721 SOCKET_UNLOCK(so);
1722 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
1723 LogFlowFuncLeave();
1724 return;
1725
1726drop:
1727 LogFlowFunc(("drop:\n"));
1728 /*
1729 * Drop space held by incoming segment and return.
1730 */
1731 m_freem(pData, m);
1732
1733#ifdef VBOX_WITH_SLIRP_MT
1734 if (RTCritSectIsOwned(&so->so_mutex))
1735 {
1736 SOCKET_UNLOCK(so);
1737 }
1738#endif
1739
1740 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
1741 LogFlowFuncLeave();
1742 return;
1743}
1744
1745void
1746tcp_dooptions(PNATState pData, struct tcpcb *tp, u_char *cp, int cnt, struct tcpiphdr *ti)
1747{
1748 u_int16_t mss;
1749 int opt, optlen;
1750
1751 LogFlowFunc(("tcp_dooptions: tp = %R[tcpcb793], cnt=%i\n", tp, cnt));
1752
1753 for (; cnt > 0; cnt -= optlen, cp += optlen)
1754 {
1755 opt = cp[0];
1756 if (opt == TCPOPT_EOL)
1757 break;
1758 if (opt == TCPOPT_NOP)
1759 optlen = 1;
1760 else
1761 {
1762 optlen = cp[1];
1763 if (optlen <= 0)
1764 break;
1765 }
1766 switch (opt)
1767 {
1768 default:
1769 continue;
1770
1771 case TCPOPT_MAXSEG:
1772 if (optlen != TCPOLEN_MAXSEG)
1773 continue;
1774 if (!(ti->ti_flags & TH_SYN))
1775 continue;
1776 memcpy((char *) &mss, (char *) cp + 2, sizeof(mss));
1777 NTOHS(mss);
1778 (void) tcp_mss(pData, tp, mss); /* sets t_maxseg */
1779 break;
1780
1781#if 0
1782 case TCPOPT_WINDOW:
1783 if (optlen != TCPOLEN_WINDOW)
1784 continue;
1785 if (!(ti->ti_flags & TH_SYN))
1786 continue;
1787 tp->t_flags |= TF_RCVD_SCALE;
1788 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
1789 break;
1790
1791 case TCPOPT_TIMESTAMP:
1792 if (optlen != TCPOLEN_TIMESTAMP)
1793 continue;
1794 *ts_present = 1;
1795 memcpy((char *) ts_val, (char *)cp + 2, sizeof(*ts_val));
1796 NTOHL(*ts_val);
1797 memcpy((char *) ts_ecr, (char *)cp + 6, sizeof(*ts_ecr));
1798 NTOHL(*ts_ecr);
1799
1800 /*
1801 * A timestamp received in a SYN makes
1802 * it ok to send timestamp requests and replies.
1803 */
1804 if (ti->ti_flags & TH_SYN)
1805 {
1806 tp->t_flags |= TF_RCVD_TSTMP;
1807 tp->ts_recent = *ts_val;
1808 tp->ts_recent_age = tcp_now;
1809 }
1810 break;
1811#endif
1812 }
1813 }
1814}
1815
1816
1817/*
1818 * Pull out of band byte out of a segment so
1819 * it doesn't appear in the user's data queue.
1820 * It is still reflected in the segment length for
1821 * sequencing purposes.
1822 */
1823
1824#if 0
1825void
1826tcp_pulloutofband(struct socket *so, struct tcpiphdr *ti, struct mbuf *m)
1827{
1828 int cnt = ti->ti_urp - 1;
1829
1830 while (cnt >= 0)
1831 {
1832 if (m->m_len > cnt)
1833 {
1834 char *cp = mtod(m, caddr_t) + cnt;
1835 struct tcpcb *tp = sototcpcb(so);
1836
1837 tp->t_iobc = *cp;
1838 tp->t_oobflags |= TCPOOB_HAVEDATA;
1839 memcpy(sp, cp+1, (unsigned)(m->m_len - cnt - 1));
1840 m->m_len--;
1841 return;
1842 }
1843 cnt -= m->m_len;
1844 m = m->m_next; /* XXX WRONG! Fix it! */
1845 if (m == 0)
1846 break;
1847 }
1848 panic("tcp_pulloutofband");
1849}
1850#endif
1851
1852/*
1853 * Collect new round-trip time estimate
1854 * and update averages and current timeout.
1855 */
1856
1857void
1858tcp_xmit_timer(PNATState pData, register struct tcpcb *tp, int rtt)
1859{
1860 register short delta;
1861
1862 LogFlowFunc(("ENTER: tcp_xmit_timer: tp = %R[tcpcb793] rtt = %d\n", tp, rtt));
1863
1864 tcpstat.tcps_rttupdated++;
1865 if (tp->t_srtt != 0)
1866 {
1867 /*
1868 * srtt is stored as fixed point with 3 bits after the
1869 * binary point (i.e., scaled by 8). The following magic
1870 * is equivalent to the smoothing algorithm in rfc793 with
1871 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1872 * point). Adjust rtt to origin 0.
1873 */
1874 delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1875 if ((tp->t_srtt += delta) <= 0)
1876 tp->t_srtt = 1;
1877 /*
1878 * We accumulate a smoothed rtt variance (actually, a
1879 * smoothed mean difference), then set the retransmit
1880 * timer to smoothed rtt + 4 times the smoothed variance.
1881 * rttvar is stored as fixed point with 2 bits after the
1882 * binary point (scaled by 4). The following is
1883 * equivalent to rfc793 smoothing with an alpha of .75
1884 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1885 * rfc793's wired-in beta.
1886 */
1887 if (delta < 0)
1888 delta = -delta;
1889 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1890 if ((tp->t_rttvar += delta) <= 0)
1891 tp->t_rttvar = 1;
1892 }
1893 else
1894 {
1895 /*
1896 * No rtt measurement yet - use the unsmoothed rtt.
1897 * Set the variance to half the rtt (so our first
1898 * retransmit happens at 3*rtt).
1899 */
1900 tp->t_srtt = rtt << TCP_RTT_SHIFT;
1901 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1902 }
1903 tp->t_rtt = 0;
1904 tp->t_rxtshift = 0;
1905
1906 /*
1907 * the retransmit should happen at rtt + 4 * rttvar.
1908 * Because of the way we do the smoothing, srtt and rttvar
1909 * will each average +1/2 tick of bias. When we compute
1910 * the retransmit timer, we want 1/2 tick of rounding and
1911 * 1 extra tick because of +-1/2 tick uncertainty in the
1912 * firing of the timer. The bias will give us exactly the
1913 * 1.5 tick we need. But, because the bias is
1914 * statistical, we have to test that we don't drop below
1915 * the minimum feasible timer (which is 2 ticks).
1916 */
1917 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1918 (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */
1919
1920 /*
1921 * We received an ack for a packet that wasn't retransmitted;
1922 * it is probably safe to discard any error indications we've
1923 * received recently. This isn't quite right, but close enough
1924 * for now (a route might have failed after we sent a segment,
1925 * and the return path might not be symmetrical).
1926 */
1927 tp->t_softerror = 0;
1928}
1929
1930/*
1931 * Determine a reasonable value for maxseg size.
1932 * If the route is known, check route for mtu.
1933 * If none, use an mss that can be handled on the outgoing
1934 * interface without forcing IP to fragment; if bigger than
1935 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1936 * to utilize large mbufs. If no route is found, route has no mtu,
1937 * or the destination isn't local, use a default, hopefully conservative
1938 * size (usually 512 or the default IP max size, but no more than the mtu
1939 * of the interface), as we can't discover anything about intervening
1940 * gateways or networks. We also initialize the congestion/slow start
1941 * window to be a single segment if the destination isn't local.
1942 * While looking at the routing entry, we also initialize other path-dependent
1943 * parameters from pre-set or cached values in the routing entry.
1944 */
1945
1946int
1947tcp_mss(PNATState pData, register struct tcpcb *tp, u_int offer)
1948{
1949 struct socket *so = tp->t_socket;
1950 int mss;
1951
1952 LogFlowFunc(("ENTER: tcp_mss: tp = %R[tcpcb793], offer = %d\n", tp, offer));
1953
1954 mss = min(if_mtu, if_mru) - sizeof(struct tcpiphdr);
1955 if (offer)
1956 mss = min(mss, offer);
1957 mss = max(mss, 32);
1958 if (mss < tp->t_maxseg || offer != 0)
1959 tp->t_maxseg = mss;
1960
1961 tp->snd_cwnd = mss;
1962
1963#ifndef VBOX_WITH_SLIRP_BSD_SBUF
1964 sbreserve(pData, &so->so_snd, tcp_sndspace+((tcp_sndspace%mss)?(mss-(tcp_sndspace%mss)):0));
1965 sbreserve(pData, &so->so_rcv, tcp_rcvspace+((tcp_rcvspace%mss)?(mss-(tcp_rcvspace%mss)):0));
1966#else
1967 sbuf_new(&so->so_snd, NULL, tcp_sndspace+((tcp_sndspace%mss)?(mss-(tcp_sndspace%mss)):0), SBUF_AUTOEXTEND);
1968 sbuf_new(&so->so_rcv, NULL, tcp_rcvspace+((tcp_rcvspace%mss)?(mss-(tcp_rcvspace%mss)):0), SBUF_AUTOEXTEND);
1969#endif
1970
1971 Log2((" returning mss = %d\n", mss));
1972
1973 return mss;
1974}
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