VirtualBox

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

Last change on this file since 35860 was 35860, checked in by vboxsync, 14 years ago

NAT/tcp: similar to r69862 case on tcp.

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