VirtualBox

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

Last change on this file since 69498 was 69498, checked in by vboxsync, 7 years ago

backed out r118835 as it incorrectly updated the 'This file is based on' file headers.

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