VirtualBox

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

最後變更 在這個檔案從23154是 22584,由 vboxsync 提交於 15 年 前

Devices: tab

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette