VirtualBox

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

Last change on this file since 29603 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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