VirtualBox

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

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

csum validation hack, for testing IP integration of fragmentation routines borrowed from BSD

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