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