VirtualBox

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

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

NAT: adds performance counters to sbuf and tcp_input

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