VirtualBox

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

Last change on this file since 16445 was 16445, checked in by vboxsync, 16 years ago

NAT:MT

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