VirtualBox

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

Last change on this file since 28520 was 28482, checked in by vboxsync, 15 years ago

NAT: nuke dead code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 61.7 KB
Line 
1/* $Id: tcp_input.c 28482 2010-04-19 16:44:22Z vboxsync $ */
2/** @file
3 * NAT - TCP input.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*
23 * This code is based on:
24 *
25 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 *
56 * @(#)tcp_input.c 8.5 (Berkeley) 4/10/94
57 * tcp_input.c,v 1.10 1994/10/13 18:36:32 wollman Exp
58 */
59
60/*
61 * Changes and additions relating to SLiRP
62 * Copyright (c) 1995 Danny Gasparovski.
63 *
64 * Please read the file COPYRIGHT for the
65 * terms and conditions of the copyright.
66 */
67
68#include <slirp.h>
69#include "ip_icmp.h"
70
71
72#define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ)
73
74/* for modulo comparisons of timestamps */
75#define TSTMP_LT(a, b) ((int)((a)-(b)) < 0)
76#define TSTMP_GEQ(a, b) ((int)((a)-(b)) >= 0)
77
78#ifndef TCP_ACK_HACK
79#define DELAY_ACK(tp, ti) \
80 if (ti->ti_flags & TH_PUSH) \
81 tp->t_flags |= TF_ACKNOW; \
82 else \
83 tp->t_flags |= TF_DELACK;
84#else /* !TCP_ACK_HACK */
85#define DELAY_ACK(tp, ign) \
86 tp->t_flags |= TF_DELACK;
87#endif /* TCP_ACK_HACK */
88
89
90/*
91 * deps: netinet/tcp_reass.c
92 * tcp_reass_maxqlen = 48 (deafault)
93 * tcp_reass_maxseg = nmbclusters/16 (nmbclusters = 1024 + maxusers * 64 from kern/kern_mbuf.c let's say 256)
94 */
95int
96tcp_reass(PNATState pData, struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
97{
98 struct tseg_qent *q;
99 struct tseg_qent *p = NULL;
100 struct tseg_qent *nq;
101 struct tseg_qent *te = NULL;
102 struct socket *so = tp->t_socket;
103 int flags;
104 STAM_PROFILE_START(&pData->StatTCP_reassamble, tcp_reassamble);
105
106 /*
107 * XXX: tcp_reass() is rather inefficient with its data structures
108 * and should be rewritten (see NetBSD for optimizations). While
109 * doing that it should move to its own file tcp_reass.c.
110 */
111
112 /*
113 * Call with th==NULL after become established to
114 * force pre-ESTABLISHED data up to user socket.
115 */
116 if (th == NULL)
117 goto present;
118
119 /*
120 * Limit the number of segments in the reassembly queue to prevent
121 * holding on to too many segments (and thus running out of mbufs).
122 * Make sure to let the missing segment through which caused this
123 * queue. Always keep one global queue entry spare to be able to
124 * process the missing segment.
125 */
126 if ( th->th_seq != tp->rcv_nxt
127 && ( tcp_reass_qsize + 1 >= tcp_reass_maxseg
128 || tp->t_segqlen >= tcp_reass_maxqlen))
129 {
130 tcp_reass_overflows++;
131 tcpstat.tcps_rcvmemdrop++;
132 m_freem(pData, m);
133 *tlenp = 0;
134 STAM_PROFILE_STOP(&pData->StatTCP_reassamble, tcp_reassamble);
135 return (0);
136 }
137
138 /*
139 * Allocate a new queue entry. If we can't, or hit the zone limit
140 * just drop the pkt.
141 */
142 te = RTMemAlloc(sizeof(struct tseg_qent));
143 if (te == NULL)
144 {
145 tcpstat.tcps_rcvmemdrop++;
146 m_freem(pData, m);
147 *tlenp = 0;
148 STAM_PROFILE_STOP(&pData->StatTCP_reassamble, tcp_reassamble);
149 return (0);
150 }
151 tp->t_segqlen++;
152 tcp_reass_qsize++;
153
154 /*
155 * Find a segment which begins after this one does.
156 */
157 LIST_FOREACH(q, &tp->t_segq, tqe_q)
158 {
159 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
160 break;
161 p = q;
162 }
163
164 /*
165 * If there is a preceding segment, it may provide some of
166 * our data already. If so, drop the data from the incoming
167 * segment. If it provides all of our data, drop us.
168 */
169 if (p != NULL)
170 {
171 int i;
172 /* conversion to int (in i) handles seq wraparound */
173 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
174 if (i > 0)
175 {
176 if (i >= *tlenp)
177 {
178 tcpstat.tcps_rcvduppack++;
179 tcpstat.tcps_rcvdupbyte += *tlenp;
180 m_freem(pData, m);
181 RTMemFree(te);
182 tp->t_segqlen--;
183 tcp_reass_qsize--;
184 /*
185 * Try to present any queued data
186 * at the left window edge to the user.
187 * This is needed after the 3-WHS
188 * completes.
189 */
190 goto present; /* ??? */
191 }
192 m_adj(m, i);
193 *tlenp -= i;
194 th->th_seq += i;
195 }
196 }
197 tcpstat.tcps_rcvoopack++;
198 tcpstat.tcps_rcvoobyte += *tlenp;
199
200 /*
201 * While we overlap succeeding segments trim them or,
202 * if they are completely covered, dequeue them.
203 */
204 while (q)
205 {
206 int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
207 if (i <= 0)
208 break;
209 if (i < q->tqe_len)
210 {
211 q->tqe_th->th_seq += i;
212 q->tqe_len -= i;
213 m_adj(q->tqe_m, i);
214 break;
215 }
216
217 nq = LIST_NEXT(q, tqe_q);
218 LIST_REMOVE(q, tqe_q);
219 m_freem(pData, q->tqe_m);
220 RTMemFree(q);
221 tp->t_segqlen--;
222 tcp_reass_qsize--;
223 q = nq;
224 }
225
226 /* Insert the new segment queue entry into place. */
227 te->tqe_m = m;
228 te->tqe_th = th;
229 te->tqe_len = *tlenp;
230
231 if (p == NULL)
232 {
233 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
234 }
235 else
236 {
237 LIST_INSERT_AFTER(p, te, tqe_q);
238 }
239
240present:
241 /*
242 * Present data to user, advancing rcv_nxt through
243 * completed sequence space.
244 */
245 if (!TCPS_HAVEESTABLISHED(tp->t_state))
246 {
247 STAM_PROFILE_STOP(&pData->StatTCP_reassamble, tcp_reassamble);
248 return (0);
249 }
250 q = LIST_FIRST(&tp->t_segq);
251 if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
252 {
253 STAM_PROFILE_STOP(&pData->StatTCP_reassamble, tcp_reassamble);
254 return (0);
255 }
256 do
257 {
258 tp->rcv_nxt += q->tqe_len;
259 flags = q->tqe_th->th_flags & TH_FIN;
260 nq = LIST_NEXT(q, tqe_q);
261 LIST_REMOVE(q, tqe_q);
262 /* XXX: This place should be checked for the same code in
263 * original BSD code for Slirp and current BSD used SS_FCANTRCVMORE
264 */
265 if (so->so_state & SS_FCANTSENDMORE)
266 m_freem(pData, q->tqe_m);
267 else
268 sbappend(pData, so, q->tqe_m);
269 RTMemFree(q);
270 tp->t_segqlen--;
271 tcp_reass_qsize--;
272 q = nq;
273 }
274 while (q && q->tqe_th->th_seq == tp->rcv_nxt);
275
276 STAM_PROFILE_STOP(&pData->StatTCP_reassamble, tcp_reassamble);
277 return flags;
278}
279
280/*
281 * TCP input routine, follows pages 65-76 of the
282 * protocol specification dated September, 1981 very closely.
283 */
284void
285tcp_input(PNATState pData, register struct mbuf *m, int iphlen, struct socket *inso)
286{
287 struct ip save_ip, *ip;
288 register struct tcpiphdr *ti;
289 caddr_t optp = NULL;
290 int optlen = 0;
291 int len, tlen, off;
292 register struct tcpcb *tp = 0;
293 register int tiflags;
294 struct socket *so = 0;
295 int todrop, acked, ourfinisacked, needoutput = 0;
296/* int dropsocket = 0; */
297 int iss = 0;
298 u_long tiwin;
299/* int ts_present = 0; */
300 STAM_PROFILE_START(&pData->StatTCP_input, counter_input);
301
302 DEBUG_CALL("tcp_input");
303 DEBUG_ARGS((dfd," m = %8lx iphlen = %2d inso = %lx\n",
304 (long )m, iphlen, (long )inso ));
305
306 if (inso != NULL)
307 {
308 QSOCKET_LOCK(tcb);
309 SOCKET_LOCK(inso);
310 QSOCKET_UNLOCK(tcb);
311 }
312 /*
313 * If called with m == 0, then we're continuing the connect
314 */
315 if (m == NULL)
316 {
317 so = inso;
318 Log4(("NAT: tcp_input: %R[natsock]\n", so));
319 /* Re-set a few variables */
320 tp = sototcpcb(so);
321 m = so->so_m;
322
323 so->so_m = 0;
324 ti = so->so_ti;
325
326 /** @todo (vvl) clarify why it might happens */
327 if (ti == NULL)
328 {
329 LogRel(("NAT: ti is null. can't do any reseting connection actions\n"));
330 /* mbuf should be cleared in sofree called from tcp_close */
331 tcp_close(pData, tp);
332 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
333 return;
334 }
335
336 tiwin = ti->ti_win;
337 tiflags = ti->ti_flags;
338
339 goto cont_conn;
340 }
341
342 tcpstat.tcps_rcvtotal++;
343 /*
344 * Get IP and TCP header together in first mbuf.
345 * Note: IP leaves IP header in first mbuf.
346 */
347 ti = mtod(m, struct tcpiphdr *);
348 if (iphlen > sizeof(struct ip ))
349 {
350 ip_stripoptions(m, (struct mbuf *)0);
351 iphlen = sizeof(struct ip );
352 }
353 /* XXX Check if too short */
354
355
356 /*
357 * Save a copy of the IP header in case we want restore it
358 * for sending an ICMP error message in response.
359 */
360 ip = mtod(m, struct ip *);
361 /*
362 * (vvl) ip_input substracts IP header length from ip->ip_len value.
363 * here we do the test the same as input method of UDP protocol.
364 */
365#ifdef VBOX_WITH_SLIRP_BSD_MBUF
366 Assert((ip->ip_len + iphlen == m_length(m, NULL)));
367#else
368 Assert((ip->ip_len + iphlen == m->m_len));
369#endif
370 save_ip = *ip;
371 save_ip.ip_len+= iphlen;
372
373 /*
374 * Checksum extended TCP header and data.
375 */
376 tlen = ((struct ip *)ti)->ip_len;
377 memset(ti->ti_x1, 0, 9);
378 ti->ti_len = RT_H2N_U16((u_int16_t)tlen);
379 len = sizeof(struct ip ) + tlen;
380 /* keep checksum for ICMP reply
381 * ti->ti_sum = cksum(m, len);
382 * if (ti->ti_sum) { */
383 if (cksum(m, len))
384 {
385 tcpstat.tcps_rcvbadsum++;
386 goto drop;
387 }
388
389 /*
390 * Check that TCP offset makes sense,
391 * pull out TCP options and adjust length. XXX
392 */
393 off = ti->ti_off << 2;
394 if ( off < sizeof (struct tcphdr)
395 || off > tlen)
396 {
397 tcpstat.tcps_rcvbadoff++;
398 goto drop;
399 }
400 tlen -= off;
401 ti->ti_len = tlen;
402 if (off > sizeof (struct tcphdr))
403 {
404 optlen = off - sizeof (struct tcphdr);
405 optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
406
407 /*
408 * Do quick retrieval of timestamp options ("options
409 * prediction?"). If timestamp is the only option and it's
410 * formatted as recommended in RFC 1323 appendix A, we
411 * quickly get the values now and not bother calling
412 * tcp_dooptions(), etc.
413 */
414#if 0
415 if (( optlen == TCPOLEN_TSTAMP_APPA
416 || ( optlen > TCPOLEN_TSTAMP_APPA
417 && optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
418 *(u_int32_t *)optp == RT_H2N_U32_C(TCPOPT_TSTAMP_HDR) &&
419 (ti->ti_flags & TH_SYN) == 0)
420 {
421 ts_present = 1;
422 ts_val = RT_N2H_U32(*(u_int32_t *)(optp + 4));
423 ts_ecr = RT_N2H_U32(*(u_int32_t *)(optp + 8));
424 optp = NULL; / * we have parsed the options * /
425 }
426#endif
427 }
428 tiflags = ti->ti_flags;
429
430 /*
431 * Convert TCP protocol specific fields to host format.
432 */
433 NTOHL(ti->ti_seq);
434 NTOHL(ti->ti_ack);
435 NTOHS(ti->ti_win);
436 NTOHS(ti->ti_urp);
437
438 /*
439 * Drop TCP, IP headers and TCP options.
440 */
441 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
442 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
443
444 /*
445 * Locate pcb for segment.
446 */
447findso:
448 if (so != NULL && so != &tcb)
449 SOCKET_UNLOCK(so);
450 QSOCKET_LOCK(tcb);
451 so = tcp_last_so;
452 if ( so->so_fport != ti->ti_dport
453 || so->so_lport != ti->ti_sport
454 || so->so_laddr.s_addr != ti->ti_src.s_addr
455 || so->so_faddr.s_addr != ti->ti_dst.s_addr)
456 {
457#ifdef VBOX_WITH_SLIRP_MT
458 struct socket *sonxt;
459#endif
460 QSOCKET_UNLOCK(tcb);
461 /* @todo fix SOLOOKUP macrodefinition to be usable here */
462#ifndef VBOX_WITH_SLIRP_MT
463 so = solookup(&tcb, ti->ti_src, ti->ti_sport,
464 ti->ti_dst, ti->ti_dport);
465#else
466 so = NULL;
467 QSOCKET_FOREACH(so, sonxt, tcp)
468 /* { */
469 if ( so->so_lport == ti->ti_sport
470 && so->so_laddr.s_addr == ti->ti_src.s_addr
471 && so->so_faddr.s_addr == ti->ti_dst.s_addr
472 && so->so_fport == ti->ti_dport
473 && so->so_deleted != 1)
474 {
475 break; /* so is locked here */
476 }
477 LOOP_LABEL(tcp, so, sonxt);
478 }
479 if (so == &tcb) {
480 so = NULL;
481 }
482#endif
483 if (so)
484 {
485 tcp_last_so = so;
486 }
487 ++tcpstat.tcps_socachemiss;
488 }
489 else
490 {
491 SOCKET_LOCK(so);
492 QSOCKET_UNLOCK(tcb);
493 }
494
495 /*
496 * If the state is CLOSED (i.e., TCB does not exist) then
497 * all data in the incoming segment is discarded.
498 * If the TCB exists but is in CLOSED state, it is embryonic,
499 * but should either do a listen or a connect soon.
500 *
501 * state == CLOSED means we've done socreate() but haven't
502 * attached it to a protocol yet...
503 *
504 * XXX If a TCB does not exist, and the TH_SYN flag is
505 * the only flag set, then create a session, mark it
506 * as if it was LISTENING, and continue...
507 */
508 if (so == 0)
509 {
510 if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) != TH_SYN)
511 goto dropwithreset;
512
513 if ((so = socreate()) == NULL)
514 goto dropwithreset;
515 if (tcp_attach(pData, so) < 0)
516 {
517 RTMemFree(so); /* Not sofree (if it failed, it's not insqued) */
518 goto dropwithreset;
519 }
520 SOCKET_LOCK(so);
521 sbreserve(pData, &so->so_snd, tcp_sndspace);
522 sbreserve(pData, &so->so_rcv, tcp_rcvspace);
523
524/* tcp_last_so = so; */ /* XXX ? */
525/* tp = sototcpcb(so); */
526
527 so->so_laddr = ti->ti_src;
528 so->so_lport = ti->ti_sport;
529 so->so_faddr = ti->ti_dst;
530 so->so_fport = ti->ti_dport;
531
532 so->so_iptos = ((struct ip *)ti)->ip_tos;
533
534 tp = sototcpcb(so);
535 tp->t_state = TCPS_LISTEN;
536 }
537
538 /*
539 * If this is a still-connecting socket, this probably
540 * a retransmit of the SYN. Whether it's a retransmit SYN
541 * or something else, we nuke it.
542 */
543 if (so->so_state & SS_ISFCONNECTING)
544 {
545 goto drop;
546 }
547
548 tp = sototcpcb(so);
549
550 /* XXX Should never fail */
551 if (tp == 0)
552 goto dropwithreset;
553 if (tp->t_state == TCPS_CLOSED)
554 {
555 goto drop;
556 }
557
558 /* Unscale the window into a 32-bit value. */
559/* if ((tiflags & TH_SYN) == 0)
560 * tiwin = ti->ti_win << tp->snd_scale;
561 * else
562 */
563 tiwin = ti->ti_win;
564
565 /*
566 * Segment received on connection.
567 * Reset idle time and keep-alive timer.
568 */
569 tp->t_idle = 0;
570 if (so_options)
571 tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
572 else
573 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
574
575 /*
576 * Process options if not in LISTEN state,
577 * else do it below (after getting remote address).
578 */
579 if (optp && tp->t_state != TCPS_LISTEN)
580 tcp_dooptions(pData, tp, (u_char *)optp, optlen, ti);
581/* , */
582/* &ts_present, &ts_val, &ts_ecr); */
583
584 /*
585 * Header prediction: check for the two common cases
586 * of a uni-directional data xfer. If the packet has
587 * no control flags, is in-sequence, the window didn't
588 * change and we're not retransmitting, it's a
589 * candidate. If the length is zero and the ack moved
590 * forward, we're the sender side of the xfer. Just
591 * free the data acked & wake any higher level process
592 * that was blocked waiting for space. If the length
593 * is non-zero and the ack didn't move, we're the
594 * receiver side. If we're getting packets in-order
595 * (the reassembly queue is empty), add the data to
596 * the socket buffer and note that we need a delayed ack.
597 *
598 * XXX Some of these tests are not needed
599 * eg: the tiwin == tp->snd_wnd prevents many more
600 * predictions.. with no *real* advantage..
601 */
602 if ( tp->t_state == TCPS_ESTABLISHED
603 && (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK
604/* && (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) */
605 && ti->ti_seq == tp->rcv_nxt
606 && tiwin && tiwin == tp->snd_wnd
607 && tp->snd_nxt == tp->snd_max)
608 {
609 /*
610 * If last ACK falls within this segment's sequence numbers,
611 * record the timestamp.
612 */
613#if 0
614 if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
615 SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len))
616 {
617 tp->ts_recent_age = tcp_now;
618 tp->ts_recent = ts_val;
619 }
620#endif
621
622 if (ti->ti_len == 0)
623 {
624 if ( SEQ_GT(ti->ti_ack, tp->snd_una)
625 && SEQ_LEQ(ti->ti_ack, tp->snd_max)
626 && tp->snd_cwnd >= tp->snd_wnd)
627 {
628 /*
629 * this is a pure ack for outstanding data.
630 */
631 ++tcpstat.tcps_predack;
632#if 0
633 if (ts_present)
634 tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
635 else
636#endif
637 if ( tp->t_rtt
638 && SEQ_GT(ti->ti_ack, tp->t_rtseq))
639 tcp_xmit_timer(pData, tp, tp->t_rtt);
640 acked = ti->ti_ack - tp->snd_una;
641 tcpstat.tcps_rcvackpack++;
642 tcpstat.tcps_rcvackbyte += acked;
643 sbdrop(&so->so_snd, acked);
644 tp->snd_una = ti->ti_ack;
645 m_freem(pData, m);
646
647 /*
648 * If all outstanding data are acked, stop
649 * retransmit timer, otherwise restart timer
650 * using current (possibly backed-off) value.
651 * If process is waiting for space,
652 * wakeup/selwakeup/signal. If data
653 * are ready to send, let tcp_output
654 * decide between more output or persist.
655 */
656 if (tp->snd_una == tp->snd_max)
657 tp->t_timer[TCPT_REXMT] = 0;
658 else if (tp->t_timer[TCPT_PERSIST] == 0)
659 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
660
661 /*
662 * There's room in so_snd, sowwakup will read()
663 * from the socket if we can
664 */
665#if 0
666 if (so->so_snd.sb_flags & SB_NOTIFY)
667 sowwakeup(so);
668#endif
669 /*
670 * This is called because sowwakeup might have
671 * put data into so_snd. Since we don't so sowwakeup,
672 * we don't need this.. XXX???
673 */
674 if (so->so_snd.sb_cc)
675 (void) tcp_output(pData, tp);
676
677 SOCKET_UNLOCK(so);
678 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
679 return;
680 }
681 }
682 else if ( ti->ti_ack == tp->snd_una
683 && LIST_FIRST(&tp->t_segq)
684 && ti->ti_len <= sbspace(&so->so_rcv))
685 {
686 /*
687 * this is a pure, in-sequence data packet
688 * with nothing on the reassembly queue and
689 * we have enough buffer space to take it.
690 */
691 ++tcpstat.tcps_preddat;
692 tp->rcv_nxt += ti->ti_len;
693 tcpstat.tcps_rcvpack++;
694 tcpstat.tcps_rcvbyte += ti->ti_len;
695 /*
696 * Add data to socket buffer.
697 */
698 sbappend(pData, so, m);
699
700 /*
701 * XXX This is called when data arrives. Later, check
702 * if we can actually write() to the socket
703 * XXX Need to check? It's be NON_BLOCKING
704 */
705/* sorwakeup(so); */
706
707 /*
708 * If this is a short packet, then ACK now - with Nagel
709 * congestion avoidance sender won't send more until
710 * he gets an ACK.
711 *
712 * It is better to not delay acks at all to maximize
713 * TCP throughput. See RFC 2581.
714 */
715 tp->t_flags |= TF_ACKNOW;
716 tcp_output(pData, tp);
717 SOCKET_UNLOCK(so);
718 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
719 return;
720 }
721 } /* header prediction */
722 /*
723 * Calculate amount of space in receive window,
724 * and then do TCP input processing.
725 * Receive window is amount of space in rcv queue,
726 * but not less than advertised window.
727 */
728 {
729 int win;
730 win = sbspace(&so->so_rcv);
731 if (win < 0)
732 win = 0;
733 tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
734 }
735
736 switch (tp->t_state)
737 {
738 /*
739 * If the state is LISTEN then ignore segment if it contains an RST.
740 * If the segment contains an ACK then it is bad and send a RST.
741 * If it does not contain a SYN then it is not interesting; drop it.
742 * Don't bother responding if the destination was a broadcast.
743 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
744 * tp->iss, and send a segment:
745 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
746 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
747 * Fill in remote peer address fields if not previously specified.
748 * Enter SYN_RECEIVED state, and process any other fields of this
749 * segment in this state.
750 */
751 case TCPS_LISTEN:
752 {
753 if (tiflags & TH_RST) {
754 goto drop;
755 }
756 if (tiflags & TH_ACK)
757 goto dropwithreset;
758 if ((tiflags & TH_SYN) == 0)
759 {
760 goto drop;
761 }
762
763 /*
764 * This has way too many gotos...
765 * But a bit of spaghetti code never hurt anybody :)
766 */
767 if ( (tcp_fconnect(pData, so) == -1)
768 && errno != EINPROGRESS
769 && errno != EWOULDBLOCK)
770 {
771 u_char code = ICMP_UNREACH_NET;
772 DEBUG_MISC((dfd," tcp fconnect errno = %d-%s\n",
773 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 tp->t_socket->so_m = NULL;
793 }
794 tp = tcp_close(pData, tp);
795 m_freem(pData, m);
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"));
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 DEBUG_MISC((dfd," dup ack m = %lx so = %lx \n",
1214 (long )m, (long )so));
1215 /*
1216 * If we have outstanding data (other than
1217 * a window probe), this is a completely
1218 * duplicate ack (ie, window info didn't
1219 * change), the ack is the biggest we've
1220 * seen and we've seen exactly our rexmt
1221 * threshold of them, assume a packet
1222 * has been dropped and retransmit it.
1223 * Kludge snd_nxt & the congestion
1224 * window so we send only this one
1225 * packet.
1226 *
1227 * We know we're losing at the current
1228 * window size so do congestion avoidance
1229 * (set ssthresh to half the current window
1230 * and pull our congestion window back to
1231 * the new ssthresh).
1232 *
1233 * Dup acks mean that packets have left the
1234 * network (they're now cached at the receiver)
1235 * so bump cwnd by the amount in the receiver
1236 * to keep a constant cwnd packets in the
1237 * network.
1238 */
1239 if ( tp->t_timer[TCPT_REXMT] == 0
1240 || ti->ti_ack != tp->snd_una)
1241 tp->t_dupacks = 0;
1242 else if (++tp->t_dupacks == tcprexmtthresh)
1243 {
1244 tcp_seq onxt = tp->snd_nxt;
1245 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
1246 if (win < 2)
1247 win = 2;
1248 tp->snd_ssthresh = win * tp->t_maxseg;
1249 tp->t_timer[TCPT_REXMT] = 0;
1250 tp->t_rtt = 0;
1251 tp->snd_nxt = ti->ti_ack;
1252 tp->snd_cwnd = tp->t_maxseg;
1253 (void) tcp_output(pData, tp);
1254 tp->snd_cwnd = tp->snd_ssthresh +
1255 tp->t_maxseg * tp->t_dupacks;
1256 if (SEQ_GT(onxt, tp->snd_nxt))
1257 tp->snd_nxt = onxt;
1258 goto drop;
1259 }
1260 else if (tp->t_dupacks > tcprexmtthresh)
1261 {
1262 tp->snd_cwnd += tp->t_maxseg;
1263 (void) tcp_output(pData, tp);
1264 goto drop;
1265 }
1266 }
1267 else
1268 tp->t_dupacks = 0;
1269 break;
1270 }
1271synrx_to_est:
1272 /*
1273 * If the congestion window was inflated to account
1274 * for the other side's cached packets, retract it.
1275 */
1276 if ( tp->t_dupacks > tcprexmtthresh
1277 && tp->snd_cwnd > tp->snd_ssthresh)
1278 tp->snd_cwnd = tp->snd_ssthresh;
1279 tp->t_dupacks = 0;
1280 if (SEQ_GT(ti->ti_ack, tp->snd_max))
1281 {
1282 tcpstat.tcps_rcvacktoomuch++;
1283 goto dropafterack;
1284 }
1285 acked = ti->ti_ack - tp->snd_una;
1286 tcpstat.tcps_rcvackpack++;
1287 tcpstat.tcps_rcvackbyte += acked;
1288
1289 /*
1290 * If we have a timestamp reply, update smoothed
1291 * round trip time. If no timestamp is present but
1292 * transmit timer is running and timed sequence
1293 * number was acked, update smoothed round trip time.
1294 * Since we now have an rtt measurement, cancel the
1295 * timer backoff (cf., Phil Karn's retransmit alg.).
1296 * Recompute the initial retransmit timer.
1297 */
1298#if 0
1299 if (ts_present)
1300 tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
1301 else
1302#endif
1303 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1304 tcp_xmit_timer(pData, tp, tp->t_rtt);
1305
1306 /*
1307 * If all outstanding data is acked, stop retransmit
1308 * timer and remember to restart (more output or persist).
1309 * If there is more data to be acked, restart retransmit
1310 * timer, using current (possibly backed-off) value.
1311 */
1312 if (ti->ti_ack == tp->snd_max)
1313 {
1314 tp->t_timer[TCPT_REXMT] = 0;
1315 needoutput = 1;
1316 }
1317 else if (tp->t_timer[TCPT_PERSIST] == 0)
1318 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1319 /*
1320 * When new data is acked, open the congestion window.
1321 * If the window gives us less than ssthresh packets
1322 * in flight, open exponentially (maxseg per packet).
1323 * Otherwise open linearly: maxseg per window
1324 * (maxseg^2 / cwnd per packet).
1325 */
1326 {
1327 register u_int cw = tp->snd_cwnd;
1328 register u_int incr = tp->t_maxseg;
1329
1330 if (cw > tp->snd_ssthresh)
1331 incr = incr * incr / cw;
1332 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1333 }
1334 if (acked > so->so_snd.sb_cc)
1335 {
1336 tp->snd_wnd -= so->so_snd.sb_cc;
1337 sbdrop(&so->so_snd, (int )so->so_snd.sb_cc);
1338 ourfinisacked = 1;
1339 }
1340 else
1341 {
1342 sbdrop(&so->so_snd, acked);
1343 tp->snd_wnd -= acked;
1344 ourfinisacked = 0;
1345 }
1346 /*
1347 * XXX sowwakup is called when data is acked and there's room for
1348 * for more data... it should read() the socket
1349 */
1350#if 0
1351 if (so->so_snd.sb_flags & SB_NOTIFY)
1352 sowwakeup(so);
1353#endif
1354 tp->snd_una = ti->ti_ack;
1355 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1356 tp->snd_nxt = tp->snd_una;
1357
1358 switch (tp->t_state)
1359 {
1360 /*
1361 * In FIN_WAIT_1 STATE in addition to the processing
1362 * for the ESTABLISHED state if our FIN is now acknowledged
1363 * then enter FIN_WAIT_2.
1364 */
1365 case TCPS_FIN_WAIT_1:
1366 if (ourfinisacked)
1367 {
1368 /*
1369 * If we can't receive any more
1370 * data, then closing user can proceed.
1371 * Starting the timer is contrary to the
1372 * specification, but if we don't get a FIN
1373 * we'll hang forever.
1374 */
1375 if (so->so_state & SS_FCANTRCVMORE)
1376 {
1377 soisfdisconnected(so);
1378 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1379 }
1380 tp->t_state = TCPS_FIN_WAIT_2;
1381 }
1382 break;
1383
1384 /*
1385 * In CLOSING STATE in addition to the processing for
1386 * the ESTABLISHED state if the ACK acknowledges our FIN
1387 * then enter the TIME-WAIT state, otherwise ignore
1388 * the segment.
1389 */
1390 case TCPS_CLOSING:
1391 if (ourfinisacked)
1392 {
1393 tp->t_state = TCPS_TIME_WAIT;
1394 tcp_canceltimers(tp);
1395 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1396 soisfdisconnected(so);
1397 }
1398 break;
1399
1400 /*
1401 * In LAST_ACK, we may still be waiting for data to drain
1402 * and/or to be acked, as well as for the ack of our FIN.
1403 * If our FIN is now acknowledged, delete the TCB,
1404 * enter the closed state and return.
1405 */
1406 case TCPS_LAST_ACK:
1407 if (ourfinisacked)
1408 {
1409 tp = tcp_close(pData, tp);
1410 goto drop;
1411 }
1412 break;
1413
1414 /*
1415 * In TIME_WAIT state the only thing that should arrive
1416 * is a retransmission of the remote FIN. Acknowledge
1417 * it and restart the finack timer.
1418 */
1419 case TCPS_TIME_WAIT:
1420 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1421 goto dropafterack;
1422 }
1423 } /* switch(tp->t_state) */
1424
1425step6:
1426 /*
1427 * Update window information.
1428 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1429 */
1430 if ( (tiflags & TH_ACK)
1431 && ( SEQ_LT(tp->snd_wl1, ti->ti_seq)
1432 || ( tp->snd_wl1 == ti->ti_seq
1433 && ( SEQ_LT(tp->snd_wl2, ti->ti_ack)
1434 || ( tp->snd_wl2 == ti->ti_ack
1435 && tiwin > tp->snd_wnd)))))
1436 {
1437 /* keep track of pure window updates */
1438 if ( ti->ti_len == 0
1439 && tp->snd_wl2 == ti->ti_ack
1440 && tiwin > tp->snd_wnd)
1441 tcpstat.tcps_rcvwinupd++;
1442 tp->snd_wnd = tiwin;
1443 tp->snd_wl1 = ti->ti_seq;
1444 tp->snd_wl2 = ti->ti_ack;
1445 if (tp->snd_wnd > tp->max_sndwnd)
1446 tp->max_sndwnd = tp->snd_wnd;
1447 needoutput = 1;
1448 }
1449
1450 /*
1451 * Process segments with URG.
1452 */
1453 if ((tiflags & TH_URG) && ti->ti_urp &&
1454 TCPS_HAVERCVDFIN(tp->t_state) == 0)
1455 {
1456 /*
1457 * This is a kludge, but if we receive and accept
1458 * random urgent pointers, we'll crash in
1459 * soreceive. It's hard to imagine someone
1460 * actually wanting to send this much urgent data.
1461 */
1462 if (ti->ti_urp + so->so_rcv.sb_cc > so->so_rcv.sb_datalen)
1463 {
1464 ti->ti_urp = 0;
1465 tiflags &= ~TH_URG;
1466 goto dodata;
1467 }
1468 /*
1469 * If this segment advances the known urgent pointer,
1470 * then mark the data stream. This should not happen
1471 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1472 * a FIN has been received from the remote side.
1473 * In these states we ignore the URG.
1474 *
1475 * According to RFC961 (Assigned Protocols),
1476 * the urgent pointer points to the last octet
1477 * of urgent data. We continue, however,
1478 * to consider it to indicate the first octet
1479 * of data past the urgent section as the original
1480 * spec states (in one of two places).
1481 */
1482 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up))
1483 {
1484 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1485 so->so_urgc = so->so_rcv.sb_cc +
1486 (tp->rcv_up - tp->rcv_nxt); /* -1; */
1487 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1488 }
1489 }
1490 else
1491 /*
1492 * If no out of band data is expected,
1493 * pull receive urgent pointer along
1494 * with the receive window.
1495 */
1496 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1497 tp->rcv_up = tp->rcv_nxt;
1498dodata:
1499
1500 /*
1501 * If this is a small packet, then ACK now - with Nagel
1502 * congestion avoidance sender won't send more until
1503 * he gets an ACK.
1504 *
1505 * See above.
1506 */
1507 if ( ti->ti_len
1508 && (unsigned)ti->ti_len <= 5
1509 && ((struct tcpiphdr_2 *)ti)->first_char == (char)27)
1510 {
1511 tp->t_flags |= TF_ACKNOW;
1512 }
1513
1514 /*
1515 * Process the segment text, merging it into the TCP sequencing queue,
1516 * and arranging for acknowledgment of receipt if necessary.
1517 * This process logically involves adjusting tp->rcv_wnd as data
1518 * is presented to the user (this happens in tcp_usrreq.c,
1519 * case PRU_RCVD). If a FIN has already been received on this
1520 * connection then we just ignore the text.
1521 */
1522 if ( (ti->ti_len || (tiflags&TH_FIN))
1523 && TCPS_HAVERCVDFIN(tp->t_state) == 0)
1524 {
1525 if ( ti->ti_seq == tp->rcv_nxt
1526 && LIST_EMPTY(&tp->t_segq)
1527 && tp->t_state == TCPS_ESTABLISHED)
1528 {
1529 DELAY_ACK(tp, ti); /* little bit different from BSD declaration see netinet/tcp_input.c */
1530 tp->rcv_nxt += tlen;
1531 tiflags = ti->ti_t.th_flags & TH_FIN;
1532 tcpstat.tcps_rcvpack++;
1533 tcpstat.tcps_rcvbyte += tlen;
1534 if (so->so_state & SS_FCANTRCVMORE)
1535 m_freem(pData, m);
1536 else
1537 sbappend(pData, so, m);
1538 }
1539 else
1540 {
1541 tiflags = tcp_reass(pData, tp, &ti->ti_t, &tlen, m);
1542 tiflags |= TF_ACKNOW;
1543 }
1544 /*
1545 * Note the amount of data that peer has sent into
1546 * our window, in order to estimate the sender's
1547 * buffer size.
1548 */
1549 len = so->so_rcv.sb_datalen - (tp->rcv_adv - tp->rcv_nxt);
1550 }
1551 else
1552 {
1553 m_freem(pData, m);
1554 tiflags &= ~TH_FIN;
1555 }
1556
1557 /*
1558 * If FIN is received ACK the FIN and let the user know
1559 * that the connection is closing.
1560 */
1561 if (tiflags & TH_FIN)
1562 {
1563 if (TCPS_HAVERCVDFIN(tp->t_state) == 0)
1564 {
1565 /*
1566 * If we receive a FIN we can't send more data,
1567 * set it SS_FDRAIN
1568 * Shutdown the socket if there is no rx data in the
1569 * buffer.
1570 * soread() is called on completion of shutdown() and
1571 * will got to TCPS_LAST_ACK, and use tcp_output()
1572 * to send the FIN.
1573 */
1574/* sofcantrcvmore(so); */
1575 sofwdrain(so);
1576
1577 tp->t_flags |= TF_ACKNOW;
1578 tp->rcv_nxt++;
1579 }
1580 switch (tp->t_state)
1581 {
1582 /*
1583 * In SYN_RECEIVED and ESTABLISHED STATES
1584 * enter the CLOSE_WAIT state.
1585 */
1586 case TCPS_SYN_RECEIVED:
1587 case TCPS_ESTABLISHED:
1588 tp->t_state = TCPS_CLOSE_WAIT;
1589 break;
1590
1591 /*
1592 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1593 * enter the CLOSING state.
1594 */
1595 case TCPS_FIN_WAIT_1:
1596 tp->t_state = TCPS_CLOSING;
1597 break;
1598
1599 /*
1600 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1601 * starting the time-wait timer, turning off the other
1602 * standard timers.
1603 */
1604 case TCPS_FIN_WAIT_2:
1605 tp->t_state = TCPS_TIME_WAIT;
1606 tcp_canceltimers(tp);
1607 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1608 soisfdisconnected(so);
1609 break;
1610
1611 /*
1612 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1613 */
1614 case TCPS_TIME_WAIT:
1615 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1616 break;
1617 }
1618 }
1619
1620 /*
1621 * Return any desired output.
1622 */
1623 if (needoutput || (tp->t_flags & TF_ACKNOW))
1624 tcp_output(pData, tp);
1625
1626 SOCKET_UNLOCK(so);
1627 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
1628 return;
1629
1630dropafterack:
1631 Log2(("drop after ack\n"));
1632 /*
1633 * Generate an ACK dropping incoming segment if it occupies
1634 * sequence space, where the ACK reflects our state.
1635 */
1636 if (tiflags & TH_RST)
1637 goto drop;
1638 m_freem(pData, m);
1639 tp->t_flags |= TF_ACKNOW;
1640 (void) tcp_output(pData, tp);
1641 SOCKET_UNLOCK(so);
1642 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
1643 return;
1644
1645dropwithreset:
1646 /* reuses m if m!=NULL, m_free() unnecessary */
1647 if (tiflags & TH_ACK)
1648 tcp_respond(pData, tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1649 else
1650 {
1651 if (tiflags & TH_SYN) ti->ti_len++;
1652 tcp_respond(pData, tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1653 TH_RST|TH_ACK);
1654 }
1655
1656 if (so != &tcb)
1657 SOCKET_UNLOCK(so);
1658 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
1659 return;
1660
1661drop:
1662 /*
1663 * Drop space held by incoming segment and return.
1664 */
1665 m_freem(pData, m);
1666
1667#ifdef VBOX_WITH_SLIRP_MT
1668 if (RTCritSectIsOwned(&so->so_mutex))
1669 {
1670 SOCKET_UNLOCK(so);
1671 }
1672#endif
1673
1674 STAM_PROFILE_STOP(&pData->StatTCP_input, counter_input);
1675 return;
1676}
1677
1678void
1679tcp_dooptions(PNATState pData, struct tcpcb *tp, u_char *cp, int cnt, struct tcpiphdr *ti)
1680{
1681 u_int16_t mss;
1682 int opt, optlen;
1683
1684 DEBUG_CALL("tcp_dooptions");
1685 DEBUG_ARGS((dfd," tp = %lx cnt=%i \n", (long )tp, cnt));
1686
1687 for (; cnt > 0; cnt -= optlen, cp += optlen)
1688 {
1689 opt = cp[0];
1690 if (opt == TCPOPT_EOL)
1691 break;
1692 if (opt == TCPOPT_NOP)
1693 optlen = 1;
1694 else
1695 {
1696 optlen = cp[1];
1697 if (optlen <= 0)
1698 break;
1699 }
1700 switch (opt)
1701 {
1702 default:
1703 continue;
1704
1705 case TCPOPT_MAXSEG:
1706 if (optlen != TCPOLEN_MAXSEG)
1707 continue;
1708 if (!(ti->ti_flags & TH_SYN))
1709 continue;
1710 memcpy((char *) &mss, (char *) cp + 2, sizeof(mss));
1711 NTOHS(mss);
1712 (void) tcp_mss(pData, tp, mss); /* sets t_maxseg */
1713 break;
1714
1715#if 0
1716 case TCPOPT_WINDOW:
1717 if (optlen != TCPOLEN_WINDOW)
1718 continue;
1719 if (!(ti->ti_flags & TH_SYN))
1720 continue;
1721 tp->t_flags |= TF_RCVD_SCALE;
1722 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
1723 break;
1724
1725 case TCPOPT_TIMESTAMP:
1726 if (optlen != TCPOLEN_TIMESTAMP)
1727 continue;
1728 *ts_present = 1;
1729 memcpy((char *) ts_val, (char *)cp + 2, sizeof(*ts_val));
1730 NTOHL(*ts_val);
1731 memcpy((char *) ts_ecr, (char *)cp + 6, sizeof(*ts_ecr));
1732 NTOHL(*ts_ecr);
1733
1734 /*
1735 * A timestamp received in a SYN makes
1736 * it ok to send timestamp requests and replies.
1737 */
1738 if (ti->ti_flags & TH_SYN)
1739 {
1740 tp->t_flags |= TF_RCVD_TSTMP;
1741 tp->ts_recent = *ts_val;
1742 tp->ts_recent_age = tcp_now;
1743 }
1744 break;
1745#endif
1746 }
1747 }
1748}
1749
1750
1751/*
1752 * Pull out of band byte out of a segment so
1753 * it doesn't appear in the user's data queue.
1754 * It is still reflected in the segment length for
1755 * sequencing purposes.
1756 */
1757
1758#if 0
1759void
1760tcp_pulloutofband(struct socket *so, struct tcpiphdr *ti, struct mbuf *m)
1761{
1762 int cnt = ti->ti_urp - 1;
1763
1764 while (cnt >= 0)
1765 {
1766 if (m->m_len > cnt)
1767 {
1768 char *cp = mtod(m, caddr_t) + cnt;
1769 struct tcpcb *tp = sototcpcb(so);
1770
1771 tp->t_iobc = *cp;
1772 tp->t_oobflags |= TCPOOB_HAVEDATA;
1773 memcpy(sp, cp+1, (unsigned)(m->m_len - cnt - 1));
1774 m->m_len--;
1775 return;
1776 }
1777 cnt -= m->m_len;
1778 m = m->m_next; /* XXX WRONG! Fix it! */
1779 if (m == 0)
1780 break;
1781 }
1782 panic("tcp_pulloutofband");
1783}
1784#endif
1785
1786/*
1787 * Collect new round-trip time estimate
1788 * and update averages and current timeout.
1789 */
1790
1791void
1792tcp_xmit_timer(PNATState pData, register struct tcpcb *tp, int rtt)
1793{
1794 register short delta;
1795
1796 DEBUG_CALL("tcp_xmit_timer");
1797 DEBUG_ARG("tp = %lx", (long)tp);
1798 DEBUG_ARG("rtt = %d", rtt);
1799
1800 tcpstat.tcps_rttupdated++;
1801 if (tp->t_srtt != 0)
1802 {
1803 /*
1804 * srtt is stored as fixed point with 3 bits after the
1805 * binary point (i.e., scaled by 8). The following magic
1806 * is equivalent to the smoothing algorithm in rfc793 with
1807 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1808 * point). Adjust rtt to origin 0.
1809 */
1810 delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1811 if ((tp->t_srtt += delta) <= 0)
1812 tp->t_srtt = 1;
1813 /*
1814 * We accumulate a smoothed rtt variance (actually, a
1815 * smoothed mean difference), then set the retransmit
1816 * timer to smoothed rtt + 4 times the smoothed variance.
1817 * rttvar is stored as fixed point with 2 bits after the
1818 * binary point (scaled by 4). The following is
1819 * equivalent to rfc793 smoothing with an alpha of .75
1820 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1821 * rfc793's wired-in beta.
1822 */
1823 if (delta < 0)
1824 delta = -delta;
1825 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1826 if ((tp->t_rttvar += delta) <= 0)
1827 tp->t_rttvar = 1;
1828 }
1829 else
1830 {
1831 /*
1832 * No rtt measurement yet - use the unsmoothed rtt.
1833 * Set the variance to half the rtt (so our first
1834 * retransmit happens at 3*rtt).
1835 */
1836 tp->t_srtt = rtt << TCP_RTT_SHIFT;
1837 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1838 }
1839 tp->t_rtt = 0;
1840 tp->t_rxtshift = 0;
1841
1842 /*
1843 * the retransmit should happen at rtt + 4 * rttvar.
1844 * Because of the way we do the smoothing, srtt and rttvar
1845 * will each average +1/2 tick of bias. When we compute
1846 * the retransmit timer, we want 1/2 tick of rounding and
1847 * 1 extra tick because of +-1/2 tick uncertainty in the
1848 * firing of the timer. The bias will give us exactly the
1849 * 1.5 tick we need. But, because the bias is
1850 * statistical, we have to test that we don't drop below
1851 * the minimum feasible timer (which is 2 ticks).
1852 */
1853 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1854 (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */
1855
1856 /*
1857 * We received an ack for a packet that wasn't retransmitted;
1858 * it is probably safe to discard any error indications we've
1859 * received recently. This isn't quite right, but close enough
1860 * for now (a route might have failed after we sent a segment,
1861 * and the return path might not be symmetrical).
1862 */
1863 tp->t_softerror = 0;
1864}
1865
1866/*
1867 * Determine a reasonable value for maxseg size.
1868 * If the route is known, check route for mtu.
1869 * If none, use an mss that can be handled on the outgoing
1870 * interface without forcing IP to fragment; if bigger than
1871 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1872 * to utilize large mbufs. If no route is found, route has no mtu,
1873 * or the destination isn't local, use a default, hopefully conservative
1874 * size (usually 512 or the default IP max size, but no more than the mtu
1875 * of the interface), as we can't discover anything about intervening
1876 * gateways or networks. We also initialize the congestion/slow start
1877 * window to be a single segment if the destination isn't local.
1878 * While looking at the routing entry, we also initialize other path-dependent
1879 * parameters from pre-set or cached values in the routing entry.
1880 */
1881
1882int
1883tcp_mss(PNATState pData, register struct tcpcb *tp, u_int offer)
1884{
1885 struct socket *so = tp->t_socket;
1886 int mss;
1887
1888 DEBUG_CALL("tcp_mss");
1889 DEBUG_ARG("tp = %lx", (long)tp);
1890 DEBUG_ARG("offer = %d", offer);
1891
1892 mss = min(if_mtu, if_mru) - sizeof(struct tcpiphdr);
1893 if (offer)
1894 mss = min(mss, offer);
1895 mss = max(mss, 32);
1896 if (mss < tp->t_maxseg || offer != 0)
1897 tp->t_maxseg = mss;
1898
1899 tp->snd_cwnd = mss;
1900
1901 sbreserve(pData, &so->so_snd, tcp_sndspace+((tcp_sndspace%mss)?(mss-(tcp_sndspace%mss)):0));
1902 sbreserve(pData, &so->so_rcv, tcp_rcvspace+((tcp_rcvspace%mss)?(mss-(tcp_rcvspace%mss)):0));
1903
1904 DEBUG_MISC((dfd, " returning mss = %d\n", mss));
1905
1906 return mss;
1907}
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