VirtualBox

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

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

per-socket and per-mbuf mutexes are removed
only global locks are stayin on their places

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

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