VirtualBox

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

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