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