merage qemu master
[qemu/qemu-JZ.git] / slirp / tcp_input.c
blobf324adb1d962ac9e00c39531d647c4c675287f35
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 tcpfrag_list_empty(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 tcpfrag_list_empty(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 = tcpfrag_list_first(tp); !tcpfrag_list_end(q, tp);
138 q = tcpiphdr_next(q))
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 (!tcpfrag_list_end(tcpiphdr_prev(q), tp)) {
148 register int i;
149 q = tcpiphdr_prev(q);
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 = tcpiphdr_next(q);
171 STAT(tcpstat.tcps_rcvoopack++);
172 STAT(tcpstat.tcps_rcvoobyte += ti->ti_len);
173 ti->ti_mbuf = m;
176 * While we overlap succeeding segments trim them or,
177 * if they are completely covered, dequeue them.
179 while (!tcpfrag_list_end(q, 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(q->ti_mbuf, i);
187 break;
189 q = tcpiphdr_next(q);
190 m = tcpiphdr_prev(q)->ti_mbuf;
191 remque(tcpiphdr2qlink(tcpiphdr_prev(q)));
192 m_freem(m);
196 * Stick new segment in its place.
198 insque(tcpiphdr2qlink(ti), tcpiphdr2qlink(tcpiphdr_prev(q)));
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 = tcpfrag_list_first(tp);
208 if (tcpfrag_list_end(ti, 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(tcpiphdr2qlink(ti));
216 m = ti->ti_mbuf;
217 ti = tcpiphdr_next(ti);
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 tcpiphdr2qlink(ti)->next = tcpiphdr2qlink(ti)->prev = 0;
306 memset(&ti->ti_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
307 ti->ti_x1 = 0;
308 ti->ti_len = htons((u_int16_t)tlen);
309 len = sizeof(struct ip ) + tlen;
310 /* keep checksum for ICMP reply
311 * ti->ti_sum = cksum(m, len);
312 * if (ti->ti_sum) { */
313 if(cksum(m, len)) {
314 STAT(tcpstat.tcps_rcvbadsum++);
315 goto drop;
319 * Check that TCP offset makes sense,
320 * pull out TCP options and adjust length. XXX
322 off = ti->ti_off << 2;
323 if (off < sizeof (struct tcphdr) || off > tlen) {
324 STAT(tcpstat.tcps_rcvbadoff++);
325 goto drop;
327 tlen -= off;
328 ti->ti_len = tlen;
329 if (off > sizeof (struct tcphdr)) {
330 optlen = off - sizeof (struct tcphdr);
331 optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
334 * Do quick retrieval of timestamp options ("options
335 * prediction?"). If timestamp is the only option and it's
336 * formatted as recommended in RFC 1323 appendix A, we
337 * quickly get the values now and not bother calling
338 * tcp_dooptions(), etc.
340 /* if ((optlen == TCPOLEN_TSTAMP_APPA ||
341 * (optlen > TCPOLEN_TSTAMP_APPA &&
342 * optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
343 * *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
344 * (ti->ti_flags & TH_SYN) == 0) {
345 * ts_present = 1;
346 * ts_val = ntohl(*(u_int32_t *)(optp + 4));
347 * ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
348 * optp = NULL; / * we've parsed the options * /
352 tiflags = ti->ti_flags;
355 * Convert TCP protocol specific fields to host format.
357 NTOHL(ti->ti_seq);
358 NTOHL(ti->ti_ack);
359 NTOHS(ti->ti_win);
360 NTOHS(ti->ti_urp);
363 * Drop TCP, IP headers and TCP options.
365 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
366 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
368 if (slirp_restrict) {
369 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
370 if (ex_ptr->ex_fport == ti->ti_dport &&
371 (ntohl(ti->ti_dst.s_addr) & 0xff) == ex_ptr->ex_addr)
372 break;
374 if (!ex_ptr)
375 goto drop;
378 * Locate pcb for segment.
380 findso:
381 so = tcp_last_so;
382 if (so->so_fport != ti->ti_dport ||
383 so->so_lport != ti->ti_sport ||
384 so->so_laddr.s_addr != ti->ti_src.s_addr ||
385 so->so_faddr.s_addr != ti->ti_dst.s_addr) {
386 so = solookup(&tcb, ti->ti_src, ti->ti_sport,
387 ti->ti_dst, ti->ti_dport);
388 if (so)
389 tcp_last_so = so;
390 STAT(tcpstat.tcps_socachemiss++);
394 * If the state is CLOSED (i.e., TCB does not exist) then
395 * all data in the incoming segment is discarded.
396 * If the TCB exists but is in CLOSED state, it is embryonic,
397 * but should either do a listen or a connect soon.
399 * state == CLOSED means we've done socreate() but haven't
400 * attached it to a protocol yet...
402 * XXX If a TCB does not exist, and the TH_SYN flag is
403 * the only flag set, then create a session, mark it
404 * as if it was LISTENING, and continue...
406 if (so == 0) {
407 if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) != TH_SYN)
408 goto dropwithreset;
410 if ((so = socreate()) == NULL)
411 goto dropwithreset;
412 if (tcp_attach(so) < 0) {
413 free(so); /* Not sofree (if it failed, it's not insqued) */
414 goto dropwithreset;
417 sbreserve(&so->so_snd, TCP_SNDSPACE);
418 sbreserve(&so->so_rcv, TCP_RCVSPACE);
420 /* tcp_last_so = so; */ /* XXX ? */
421 /* tp = sototcpcb(so); */
423 so->so_laddr = ti->ti_src;
424 so->so_lport = ti->ti_sport;
425 so->so_faddr = ti->ti_dst;
426 so->so_fport = ti->ti_dport;
428 if ((so->so_iptos = tcp_tos(so)) == 0)
429 so->so_iptos = ((struct ip *)ti)->ip_tos;
431 tp = sototcpcb(so);
432 tp->t_state = TCPS_LISTEN;
436 * If this is a still-connecting socket, this probably
437 * a retransmit of the SYN. Whether it's a retransmit SYN
438 * or something else, we nuke it.
440 if (so->so_state & SS_ISFCONNECTING)
441 goto drop;
443 tp = sototcpcb(so);
445 /* XXX Should never fail */
446 if (tp == 0)
447 goto dropwithreset;
448 if (tp->t_state == TCPS_CLOSED)
449 goto drop;
451 /* Unscale the window into a 32-bit value. */
452 /* if ((tiflags & TH_SYN) == 0)
453 * tiwin = ti->ti_win << tp->snd_scale;
454 * else
456 tiwin = ti->ti_win;
459 * Segment received on connection.
460 * Reset idle time and keep-alive timer.
462 tp->t_idle = 0;
463 if (SO_OPTIONS)
464 tp->t_timer[TCPT_KEEP] = TCPTV_KEEPINTVL;
465 else
466 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_IDLE;
469 * Process options if not in LISTEN state,
470 * else do it below (after getting remote address).
472 if (optp && tp->t_state != TCPS_LISTEN)
473 tcp_dooptions(tp, (u_char *)optp, optlen, ti);
474 /* , */
475 /* &ts_present, &ts_val, &ts_ecr); */
478 * Header prediction: check for the two common cases
479 * of a uni-directional data xfer. If the packet has
480 * no control flags, is in-sequence, the window didn't
481 * change and we're not retransmitting, it's a
482 * candidate. If the length is zero and the ack moved
483 * forward, we're the sender side of the xfer. Just
484 * free the data acked & wake any higher level process
485 * that was blocked waiting for space. If the length
486 * is non-zero and the ack didn't move, we're the
487 * receiver side. If we're getting packets in-order
488 * (the reassembly queue is empty), add the data to
489 * the socket buffer and note that we need a delayed ack.
491 * XXX Some of these tests are not needed
492 * eg: the tiwin == tp->snd_wnd prevents many more
493 * predictions.. with no *real* advantage..
495 if (tp->t_state == TCPS_ESTABLISHED &&
496 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
497 /* (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) && */
498 ti->ti_seq == tp->rcv_nxt &&
499 tiwin && tiwin == tp->snd_wnd &&
500 tp->snd_nxt == tp->snd_max) {
502 * If last ACK falls within this segment's sequence numbers,
503 * record the timestamp.
505 /* if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
506 * SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
507 * tp->ts_recent_age = tcp_now;
508 * tp->ts_recent = ts_val;
511 if (ti->ti_len == 0) {
512 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
513 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
514 tp->snd_cwnd >= tp->snd_wnd) {
516 * this is a pure ack for outstanding data.
518 STAT(tcpstat.tcps_predack++);
519 /* if (ts_present)
520 * tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
521 * else
522 */ if (tp->t_rtt &&
523 SEQ_GT(ti->ti_ack, tp->t_rtseq))
524 tcp_xmit_timer(tp, tp->t_rtt);
525 acked = ti->ti_ack - tp->snd_una;
526 STAT(tcpstat.tcps_rcvackpack++);
527 STAT(tcpstat.tcps_rcvackbyte += acked);
528 sbdrop(&so->so_snd, acked);
529 tp->snd_una = ti->ti_ack;
530 m_freem(m);
533 * If all outstanding data are acked, stop
534 * retransmit timer, otherwise restart timer
535 * using current (possibly backed-off) value.
536 * If process is waiting for space,
537 * wakeup/selwakeup/signal. If data
538 * are ready to send, let tcp_output
539 * decide between more output or persist.
541 if (tp->snd_una == tp->snd_max)
542 tp->t_timer[TCPT_REXMT] = 0;
543 else if (tp->t_timer[TCPT_PERSIST] == 0)
544 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
547 * There's room in so_snd, sowwakup will read()
548 * from the socket if we can
550 /* if (so->so_snd.sb_flags & SB_NOTIFY)
551 * sowwakeup(so);
554 * This is called because sowwakeup might have
555 * put data into so_snd. Since we don't so sowwakeup,
556 * we don't need this.. XXX???
558 if (so->so_snd.sb_cc)
559 (void) tcp_output(tp);
561 return;
563 } else if (ti->ti_ack == tp->snd_una &&
564 tcpfrag_list_empty(tp) &&
565 ti->ti_len <= sbspace(&so->so_rcv)) {
567 * this is a pure, in-sequence data packet
568 * with nothing on the reassembly queue and
569 * we have enough buffer space to take it.
571 STAT(tcpstat.tcps_preddat++);
572 tp->rcv_nxt += ti->ti_len;
573 STAT(tcpstat.tcps_rcvpack++);
574 STAT(tcpstat.tcps_rcvbyte += ti->ti_len);
576 * Add data to socket buffer.
578 if (so->so_emu) {
579 if (tcp_emu(so,m)) sbappend(so, m);
580 } else
581 sbappend(so, m);
584 * XXX This is called when data arrives. Later, check
585 * if we can actually write() to the socket
586 * XXX Need to check? It's be NON_BLOCKING
588 /* sorwakeup(so); */
591 * If this is a short packet, then ACK now - with Nagel
592 * congestion avoidance sender won't send more until
593 * he gets an ACK.
595 * It is better to not delay acks at all to maximize
596 * TCP throughput. See RFC 2581.
598 tp->t_flags |= TF_ACKNOW;
599 tcp_output(tp);
600 return;
602 } /* header prediction */
604 * Calculate amount of space in receive window,
605 * and then do TCP input processing.
606 * Receive window is amount of space in rcv queue,
607 * but not less than advertised window.
609 { int win;
610 win = sbspace(&so->so_rcv);
611 if (win < 0)
612 win = 0;
613 tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
616 switch (tp->t_state) {
619 * If the state is LISTEN then ignore segment if it contains an RST.
620 * If the segment contains an ACK then it is bad and send a RST.
621 * If it does not contain a SYN then it is not interesting; drop it.
622 * Don't bother responding if the destination was a broadcast.
623 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
624 * tp->iss, and send a segment:
625 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
626 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
627 * Fill in remote peer address fields if not previously specified.
628 * Enter SYN_RECEIVED state, and process any other fields of this
629 * segment in this state.
631 case TCPS_LISTEN: {
633 if (tiflags & TH_RST)
634 goto drop;
635 if (tiflags & TH_ACK)
636 goto dropwithreset;
637 if ((tiflags & TH_SYN) == 0)
638 goto drop;
641 * This has way too many gotos...
642 * But a bit of spaghetti code never hurt anybody :)
646 * If this is destined for the control address, then flag to
647 * tcp_ctl once connected, otherwise connect
649 if ((so->so_faddr.s_addr&htonl(0xffffff00)) == special_addr.s_addr) {
650 int lastbyte=ntohl(so->so_faddr.s_addr) & 0xff;
651 if (lastbyte!=CTL_ALIAS && lastbyte!=CTL_DNS) {
652 #if 0
653 if(lastbyte==CTL_CMD || lastbyte==CTL_EXEC) {
654 /* Command or exec adress */
655 so->so_state |= SS_CTL;
656 } else
657 #endif
659 /* May be an add exec */
660 for(ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
661 if(ex_ptr->ex_fport == so->so_fport &&
662 lastbyte == ex_ptr->ex_addr) {
663 so->so_state |= SS_CTL;
664 break;
668 if(so->so_state & SS_CTL) goto cont_input;
670 /* CTL_ALIAS: Do nothing, tcp_fconnect will be called on it */
673 if (so->so_emu & EMU_NOCONNECT) {
674 so->so_emu &= ~EMU_NOCONNECT;
675 goto cont_input;
678 if((tcp_fconnect(so) == -1) && (errno != EINPROGRESS) && (errno != EWOULDBLOCK)) {
679 u_char code=ICMP_UNREACH_NET;
680 DEBUG_MISC((dfd," tcp fconnect errno = %d-%s\n",
681 errno,strerror(errno)));
682 if(errno == ECONNREFUSED) {
683 /* ACK the SYN, send RST to refuse the connection */
684 tcp_respond(tp, ti, m, ti->ti_seq+1, (tcp_seq)0,
685 TH_RST|TH_ACK);
686 } else {
687 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
688 HTONL(ti->ti_seq); /* restore tcp header */
689 HTONL(ti->ti_ack);
690 HTONS(ti->ti_win);
691 HTONS(ti->ti_urp);
692 m->m_data -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
693 m->m_len += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
694 *ip=save_ip;
695 icmp_error(m, ICMP_UNREACH,code, 0,strerror(errno));
697 tp = tcp_close(tp);
698 m_free(m);
699 } else {
701 * Haven't connected yet, save the current mbuf
702 * and ti, and return
703 * XXX Some OS's don't tell us whether the connect()
704 * succeeded or not. So we must time it out.
706 so->so_m = m;
707 so->so_ti = ti;
708 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
709 tp->t_state = TCPS_SYN_RECEIVED;
711 return;
713 cont_conn:
714 /* m==NULL
715 * Check if the connect succeeded
717 if (so->so_state & SS_NOFDREF) {
718 tp = tcp_close(tp);
719 goto dropwithreset;
721 cont_input:
722 tcp_template(tp);
724 if (optp)
725 tcp_dooptions(tp, (u_char *)optp, optlen, ti);
726 /* , */
727 /* &ts_present, &ts_val, &ts_ecr); */
729 if (iss)
730 tp->iss = iss;
731 else
732 tp->iss = tcp_iss;
733 tcp_iss += TCP_ISSINCR/2;
734 tp->irs = ti->ti_seq;
735 tcp_sendseqinit(tp);
736 tcp_rcvseqinit(tp);
737 tp->t_flags |= TF_ACKNOW;
738 tp->t_state = TCPS_SYN_RECEIVED;
739 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
740 STAT(tcpstat.tcps_accepts++);
741 goto trimthenstep6;
742 } /* case TCPS_LISTEN */
745 * If the state is SYN_SENT:
746 * if seg contains an ACK, but not for our SYN, drop the input.
747 * if seg contains a RST, then drop the connection.
748 * if seg does not contain SYN, then drop it.
749 * Otherwise this is an acceptable SYN segment
750 * initialize tp->rcv_nxt and tp->irs
751 * if seg contains ack then advance tp->snd_una
752 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
753 * arrange for segment to be acked (eventually)
754 * continue processing rest of data/controls, beginning with URG
756 case TCPS_SYN_SENT:
757 if ((tiflags & TH_ACK) &&
758 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
759 SEQ_GT(ti->ti_ack, tp->snd_max)))
760 goto dropwithreset;
762 if (tiflags & TH_RST) {
763 if (tiflags & TH_ACK)
764 tp = tcp_drop(tp,0); /* XXX Check t_softerror! */
765 goto drop;
768 if ((tiflags & TH_SYN) == 0)
769 goto drop;
770 if (tiflags & TH_ACK) {
771 tp->snd_una = ti->ti_ack;
772 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
773 tp->snd_nxt = tp->snd_una;
776 tp->t_timer[TCPT_REXMT] = 0;
777 tp->irs = ti->ti_seq;
778 tcp_rcvseqinit(tp);
779 tp->t_flags |= TF_ACKNOW;
780 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
781 STAT(tcpstat.tcps_connects++);
782 soisfconnected(so);
783 tp->t_state = TCPS_ESTABLISHED;
785 /* Do window scaling on this connection? */
786 /* if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
787 * (TF_RCVD_SCALE|TF_REQ_SCALE)) {
788 * tp->snd_scale = tp->requested_s_scale;
789 * tp->rcv_scale = tp->request_r_scale;
792 (void) tcp_reass(tp, (struct tcpiphdr *)0,
793 (struct mbuf *)0);
795 * if we didn't have to retransmit the SYN,
796 * use its rtt as our initial srtt & rtt var.
798 if (tp->t_rtt)
799 tcp_xmit_timer(tp, tp->t_rtt);
800 } else
801 tp->t_state = TCPS_SYN_RECEIVED;
803 trimthenstep6:
805 * Advance ti->ti_seq to correspond to first data byte.
806 * If data, trim to stay within window,
807 * dropping FIN if necessary.
809 ti->ti_seq++;
810 if (ti->ti_len > tp->rcv_wnd) {
811 todrop = ti->ti_len - tp->rcv_wnd;
812 m_adj(m, -todrop);
813 ti->ti_len = tp->rcv_wnd;
814 tiflags &= ~TH_FIN;
815 STAT(tcpstat.tcps_rcvpackafterwin++);
816 STAT(tcpstat.tcps_rcvbyteafterwin += todrop);
818 tp->snd_wl1 = ti->ti_seq - 1;
819 tp->rcv_up = ti->ti_seq;
820 goto step6;
821 } /* switch tp->t_state */
823 * States other than LISTEN or SYN_SENT.
824 * First check timestamp, if present.
825 * Then check that at least some bytes of segment are within
826 * receive window. If segment begins before rcv_nxt,
827 * drop leading data (and SYN); if nothing left, just ack.
829 * RFC 1323 PAWS: If we have a timestamp reply on this segment
830 * and it's less than ts_recent, drop it.
832 /* if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
833 * TSTMP_LT(ts_val, tp->ts_recent)) {
835 */ /* Check to see if ts_recent is over 24 days old. */
836 /* if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
837 */ /*
838 * * Invalidate ts_recent. If this segment updates
839 * * ts_recent, the age will be reset later and ts_recent
840 * * will get a valid value. If it does not, setting
841 * * ts_recent to zero will at least satisfy the
842 * * requirement that zero be placed in the timestamp
843 * * echo reply when ts_recent isn't valid. The
844 * * age isn't reset until we get a valid ts_recent
845 * * because we don't want out-of-order segments to be
846 * * dropped when ts_recent is old.
847 * */
848 /* tp->ts_recent = 0;
849 * } else {
850 * tcpstat.tcps_rcvduppack++;
851 * tcpstat.tcps_rcvdupbyte += ti->ti_len;
852 * tcpstat.tcps_pawsdrop++;
853 * goto dropafterack;
858 todrop = tp->rcv_nxt - ti->ti_seq;
859 if (todrop > 0) {
860 if (tiflags & TH_SYN) {
861 tiflags &= ~TH_SYN;
862 ti->ti_seq++;
863 if (ti->ti_urp > 1)
864 ti->ti_urp--;
865 else
866 tiflags &= ~TH_URG;
867 todrop--;
870 * Following if statement from Stevens, vol. 2, p. 960.
872 if (todrop > ti->ti_len
873 || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
875 * Any valid FIN must be to the left of the window.
876 * At this point the FIN must be a duplicate or out
877 * of sequence; drop it.
879 tiflags &= ~TH_FIN;
882 * Send an ACK to resynchronize and drop any data.
883 * But keep on processing for RST or ACK.
885 tp->t_flags |= TF_ACKNOW;
886 todrop = ti->ti_len;
887 STAT(tcpstat.tcps_rcvduppack++);
888 STAT(tcpstat.tcps_rcvdupbyte += todrop);
889 } else {
890 STAT(tcpstat.tcps_rcvpartduppack++);
891 STAT(tcpstat.tcps_rcvpartdupbyte += todrop);
893 m_adj(m, todrop);
894 ti->ti_seq += todrop;
895 ti->ti_len -= todrop;
896 if (ti->ti_urp > todrop)
897 ti->ti_urp -= todrop;
898 else {
899 tiflags &= ~TH_URG;
900 ti->ti_urp = 0;
904 * If new data are received on a connection after the
905 * user processes are gone, then RST the other end.
907 if ((so->so_state & SS_NOFDREF) &&
908 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
909 tp = tcp_close(tp);
910 STAT(tcpstat.tcps_rcvafterclose++);
911 goto dropwithreset;
915 * If segment ends after window, drop trailing data
916 * (and PUSH and FIN); if nothing left, just ACK.
918 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
919 if (todrop > 0) {
920 STAT(tcpstat.tcps_rcvpackafterwin++);
921 if (todrop >= ti->ti_len) {
922 STAT(tcpstat.tcps_rcvbyteafterwin += ti->ti_len);
924 * If a new connection request is received
925 * while in TIME_WAIT, drop the old connection
926 * and start over if the sequence numbers
927 * are above the previous ones.
929 if (tiflags & TH_SYN &&
930 tp->t_state == TCPS_TIME_WAIT &&
931 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
932 iss = tp->rcv_nxt + TCP_ISSINCR;
933 tp = tcp_close(tp);
934 goto findso;
937 * If window is closed can only take segments at
938 * window edge, and have to drop data and PUSH from
939 * incoming segments. Continue processing, but
940 * remember to ack. Otherwise, drop segment
941 * and ack.
943 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
944 tp->t_flags |= TF_ACKNOW;
945 STAT(tcpstat.tcps_rcvwinprobe++);
946 } else
947 goto dropafterack;
948 } else
949 STAT(tcpstat.tcps_rcvbyteafterwin += todrop);
950 m_adj(m, -todrop);
951 ti->ti_len -= todrop;
952 tiflags &= ~(TH_PUSH|TH_FIN);
956 * If last ACK falls within this segment's sequence numbers,
957 * record its timestamp.
959 /* if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
960 * SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
961 * ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
962 * tp->ts_recent_age = tcp_now;
963 * tp->ts_recent = ts_val;
968 * If the RST bit is set examine the state:
969 * SYN_RECEIVED STATE:
970 * If passive open, return to LISTEN state.
971 * If active open, inform user that connection was refused.
972 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
973 * Inform user that connection was reset, and close tcb.
974 * CLOSING, LAST_ACK, TIME_WAIT STATES
975 * Close the tcb.
977 if (tiflags&TH_RST) switch (tp->t_state) {
979 case TCPS_SYN_RECEIVED:
980 /* so->so_error = ECONNREFUSED; */
981 goto close;
983 case TCPS_ESTABLISHED:
984 case TCPS_FIN_WAIT_1:
985 case TCPS_FIN_WAIT_2:
986 case TCPS_CLOSE_WAIT:
987 /* so->so_error = ECONNRESET; */
988 close:
989 tp->t_state = TCPS_CLOSED;
990 STAT(tcpstat.tcps_drops++);
991 tp = tcp_close(tp);
992 goto drop;
994 case TCPS_CLOSING:
995 case TCPS_LAST_ACK:
996 case TCPS_TIME_WAIT:
997 tp = tcp_close(tp);
998 goto drop;
1002 * If a SYN is in the window, then this is an
1003 * error and we send an RST and drop the connection.
1005 if (tiflags & TH_SYN) {
1006 tp = tcp_drop(tp,0);
1007 goto dropwithreset;
1011 * If the ACK bit is off we drop the segment and return.
1013 if ((tiflags & TH_ACK) == 0) goto drop;
1016 * Ack processing.
1018 switch (tp->t_state) {
1020 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
1021 * ESTABLISHED state and continue processing, otherwise
1022 * send an RST. una<=ack<=max
1024 case TCPS_SYN_RECEIVED:
1026 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
1027 SEQ_GT(ti->ti_ack, tp->snd_max))
1028 goto dropwithreset;
1029 STAT(tcpstat.tcps_connects++);
1030 tp->t_state = TCPS_ESTABLISHED;
1032 * The sent SYN is ack'ed with our sequence number +1
1033 * The first data byte already in the buffer will get
1034 * lost if no correction is made. This is only needed for
1035 * SS_CTL since the buffer is empty otherwise.
1036 * tp->snd_una++; or:
1038 tp->snd_una=ti->ti_ack;
1039 if (so->so_state & SS_CTL) {
1040 /* So tcp_ctl reports the right state */
1041 ret = tcp_ctl(so);
1042 if (ret == 1) {
1043 soisfconnected(so);
1044 so->so_state &= ~SS_CTL; /* success XXX */
1045 } else if (ret == 2) {
1046 so->so_state = SS_NOFDREF; /* CTL_CMD */
1047 } else {
1048 needoutput = 1;
1049 tp->t_state = TCPS_FIN_WAIT_1;
1051 } else {
1052 soisfconnected(so);
1055 /* Do window scaling? */
1056 /* if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1057 * (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1058 * tp->snd_scale = tp->requested_s_scale;
1059 * tp->rcv_scale = tp->request_r_scale;
1062 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
1063 tp->snd_wl1 = ti->ti_seq - 1;
1064 /* Avoid ack processing; snd_una==ti_ack => dup ack */
1065 goto synrx_to_est;
1066 /* fall into ... */
1069 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1070 * ACKs. If the ack is in the range
1071 * tp->snd_una < ti->ti_ack <= tp->snd_max
1072 * then advance tp->snd_una to ti->ti_ack and drop
1073 * data from the retransmission queue. If this ACK reflects
1074 * more up to date window information we update our window information.
1076 case TCPS_ESTABLISHED:
1077 case TCPS_FIN_WAIT_1:
1078 case TCPS_FIN_WAIT_2:
1079 case TCPS_CLOSE_WAIT:
1080 case TCPS_CLOSING:
1081 case TCPS_LAST_ACK:
1082 case TCPS_TIME_WAIT:
1084 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
1085 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
1086 STAT(tcpstat.tcps_rcvdupack++);
1087 DEBUG_MISC((dfd," dup ack m = %lx so = %lx \n",
1088 (long )m, (long )so));
1090 * If we have outstanding data (other than
1091 * a window probe), this is a completely
1092 * duplicate ack (ie, window info didn't
1093 * change), the ack is the biggest we've
1094 * seen and we've seen exactly our rexmt
1095 * threshold of them, assume a packet
1096 * has been dropped and retransmit it.
1097 * Kludge snd_nxt & the congestion
1098 * window so we send only this one
1099 * packet.
1101 * We know we're losing at the current
1102 * window size so do congestion avoidance
1103 * (set ssthresh to half the current window
1104 * and pull our congestion window back to
1105 * the new ssthresh).
1107 * Dup acks mean that packets have left the
1108 * network (they're now cached at the receiver)
1109 * so bump cwnd by the amount in the receiver
1110 * to keep a constant cwnd packets in the
1111 * network.
1113 if (tp->t_timer[TCPT_REXMT] == 0 ||
1114 ti->ti_ack != tp->snd_una)
1115 tp->t_dupacks = 0;
1116 else if (++tp->t_dupacks == TCPREXMTTHRESH) {
1117 tcp_seq onxt = tp->snd_nxt;
1118 u_int win =
1119 min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1120 tp->t_maxseg;
1122 if (win < 2)
1123 win = 2;
1124 tp->snd_ssthresh = win * tp->t_maxseg;
1125 tp->t_timer[TCPT_REXMT] = 0;
1126 tp->t_rtt = 0;
1127 tp->snd_nxt = ti->ti_ack;
1128 tp->snd_cwnd = tp->t_maxseg;
1129 (void) tcp_output(tp);
1130 tp->snd_cwnd = tp->snd_ssthresh +
1131 tp->t_maxseg * tp->t_dupacks;
1132 if (SEQ_GT(onxt, tp->snd_nxt))
1133 tp->snd_nxt = onxt;
1134 goto drop;
1135 } else if (tp->t_dupacks > TCPREXMTTHRESH) {
1136 tp->snd_cwnd += tp->t_maxseg;
1137 (void) tcp_output(tp);
1138 goto drop;
1140 } else
1141 tp->t_dupacks = 0;
1142 break;
1144 synrx_to_est:
1146 * If the congestion window was inflated to account
1147 * for the other side's cached packets, retract it.
1149 if (tp->t_dupacks > TCPREXMTTHRESH &&
1150 tp->snd_cwnd > tp->snd_ssthresh)
1151 tp->snd_cwnd = tp->snd_ssthresh;
1152 tp->t_dupacks = 0;
1153 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
1154 STAT(tcpstat.tcps_rcvacktoomuch++);
1155 goto dropafterack;
1157 acked = ti->ti_ack - tp->snd_una;
1158 STAT(tcpstat.tcps_rcvackpack++);
1159 STAT(tcpstat.tcps_rcvackbyte += acked);
1162 * If we have a timestamp reply, update smoothed
1163 * round trip time. If no timestamp is present but
1164 * transmit timer is running and timed sequence
1165 * number was acked, update smoothed round trip time.
1166 * Since we now have an rtt measurement, cancel the
1167 * timer backoff (cf., Phil Karn's retransmit alg.).
1168 * Recompute the initial retransmit timer.
1170 /* if (ts_present)
1171 * tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
1172 * else
1174 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1175 tcp_xmit_timer(tp,tp->t_rtt);
1178 * If all outstanding data is acked, stop retransmit
1179 * timer and remember to restart (more output or persist).
1180 * If there is more data to be acked, restart retransmit
1181 * timer, using current (possibly backed-off) value.
1183 if (ti->ti_ack == tp->snd_max) {
1184 tp->t_timer[TCPT_REXMT] = 0;
1185 needoutput = 1;
1186 } else if (tp->t_timer[TCPT_PERSIST] == 0)
1187 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1189 * When new data is acked, open the congestion window.
1190 * If the window gives us less than ssthresh packets
1191 * in flight, open exponentially (maxseg per packet).
1192 * Otherwise open linearly: maxseg per window
1193 * (maxseg^2 / cwnd per packet).
1196 register u_int cw = tp->snd_cwnd;
1197 register u_int incr = tp->t_maxseg;
1199 if (cw > tp->snd_ssthresh)
1200 incr = incr * incr / cw;
1201 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1203 if (acked > so->so_snd.sb_cc) {
1204 tp->snd_wnd -= so->so_snd.sb_cc;
1205 sbdrop(&so->so_snd, (int )so->so_snd.sb_cc);
1206 ourfinisacked = 1;
1207 } else {
1208 sbdrop(&so->so_snd, acked);
1209 tp->snd_wnd -= acked;
1210 ourfinisacked = 0;
1213 * XXX sowwakup is called when data is acked and there's room for
1214 * for more data... it should read() the socket
1216 /* if (so->so_snd.sb_flags & SB_NOTIFY)
1217 * sowwakeup(so);
1219 tp->snd_una = ti->ti_ack;
1220 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1221 tp->snd_nxt = tp->snd_una;
1223 switch (tp->t_state) {
1226 * In FIN_WAIT_1 STATE in addition to the processing
1227 * for the ESTABLISHED state if our FIN is now acknowledged
1228 * then enter FIN_WAIT_2.
1230 case TCPS_FIN_WAIT_1:
1231 if (ourfinisacked) {
1233 * If we can't receive any more
1234 * data, then closing user can proceed.
1235 * Starting the timer is contrary to the
1236 * specification, but if we don't get a FIN
1237 * we'll hang forever.
1239 if (so->so_state & SS_FCANTRCVMORE) {
1240 soisfdisconnected(so);
1241 tp->t_timer[TCPT_2MSL] = TCP_MAXIDLE;
1243 tp->t_state = TCPS_FIN_WAIT_2;
1245 break;
1248 * In CLOSING STATE in addition to the processing for
1249 * the ESTABLISHED state if the ACK acknowledges our FIN
1250 * then enter the TIME-WAIT state, otherwise ignore
1251 * the segment.
1253 case TCPS_CLOSING:
1254 if (ourfinisacked) {
1255 tp->t_state = TCPS_TIME_WAIT;
1256 tcp_canceltimers(tp);
1257 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1258 soisfdisconnected(so);
1260 break;
1263 * In LAST_ACK, we may still be waiting for data to drain
1264 * and/or to be acked, as well as for the ack of our FIN.
1265 * If our FIN is now acknowledged, delete the TCB,
1266 * enter the closed state and return.
1268 case TCPS_LAST_ACK:
1269 if (ourfinisacked) {
1270 tp = tcp_close(tp);
1271 goto drop;
1273 break;
1276 * In TIME_WAIT state the only thing that should arrive
1277 * is a retransmission of the remote FIN. Acknowledge
1278 * it and restart the finack timer.
1280 case TCPS_TIME_WAIT:
1281 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1282 goto dropafterack;
1284 } /* switch(tp->t_state) */
1286 step6:
1288 * Update window information.
1289 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1291 if ((tiflags & TH_ACK) &&
1292 (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
1293 (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1294 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) {
1295 /* keep track of pure window updates */
1296 if (ti->ti_len == 0 &&
1297 tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
1298 STAT(tcpstat.tcps_rcvwinupd++);
1299 tp->snd_wnd = tiwin;
1300 tp->snd_wl1 = ti->ti_seq;
1301 tp->snd_wl2 = ti->ti_ack;
1302 if (tp->snd_wnd > tp->max_sndwnd)
1303 tp->max_sndwnd = tp->snd_wnd;
1304 needoutput = 1;
1308 * Process segments with URG.
1310 if ((tiflags & TH_URG) && ti->ti_urp &&
1311 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1313 * This is a kludge, but if we receive and accept
1314 * random urgent pointers, we'll crash in
1315 * soreceive. It's hard to imagine someone
1316 * actually wanting to send this much urgent data.
1318 if (ti->ti_urp + so->so_rcv.sb_cc > so->so_rcv.sb_datalen) {
1319 ti->ti_urp = 0;
1320 tiflags &= ~TH_URG;
1321 goto dodata;
1324 * If this segment advances the known urgent pointer,
1325 * then mark the data stream. This should not happen
1326 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1327 * a FIN has been received from the remote side.
1328 * In these states we ignore the URG.
1330 * According to RFC961 (Assigned Protocols),
1331 * the urgent pointer points to the last octet
1332 * of urgent data. We continue, however,
1333 * to consider it to indicate the first octet
1334 * of data past the urgent section as the original
1335 * spec states (in one of two places).
1337 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1338 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1339 so->so_urgc = so->so_rcv.sb_cc +
1340 (tp->rcv_up - tp->rcv_nxt); /* -1; */
1341 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1344 } else
1346 * If no out of band data is expected,
1347 * pull receive urgent pointer along
1348 * with the receive window.
1350 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1351 tp->rcv_up = tp->rcv_nxt;
1352 dodata:
1355 * Process the segment text, merging it into the TCP sequencing queue,
1356 * and arranging for acknowledgment of receipt if necessary.
1357 * This process logically involves adjusting tp->rcv_wnd as data
1358 * is presented to the user (this happens in tcp_usrreq.c,
1359 * case PRU_RCVD). If a FIN has already been received on this
1360 * connection then we just ignore the text.
1362 if ((ti->ti_len || (tiflags&TH_FIN)) &&
1363 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1364 TCP_REASS(tp, ti, m, so, tiflags);
1366 * Note the amount of data that peer has sent into
1367 * our window, in order to estimate the sender's
1368 * buffer size.
1370 len = so->so_rcv.sb_datalen - (tp->rcv_adv - tp->rcv_nxt);
1371 } else {
1372 m_free(m);
1373 tiflags &= ~TH_FIN;
1377 * If FIN is received ACK the FIN and let the user know
1378 * that the connection is closing.
1380 if (tiflags & TH_FIN) {
1381 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1383 * If we receive a FIN we can't send more data,
1384 * set it SS_FDRAIN
1385 * Shutdown the socket if there is no rx data in the
1386 * buffer.
1387 * soread() is called on completion of shutdown() and
1388 * will got to TCPS_LAST_ACK, and use tcp_output()
1389 * to send the FIN.
1391 /* sofcantrcvmore(so); */
1392 sofwdrain(so);
1394 tp->t_flags |= TF_ACKNOW;
1395 tp->rcv_nxt++;
1397 switch (tp->t_state) {
1400 * In SYN_RECEIVED and ESTABLISHED STATES
1401 * enter the CLOSE_WAIT state.
1403 case TCPS_SYN_RECEIVED:
1404 case TCPS_ESTABLISHED:
1405 if(so->so_emu == EMU_CTL) /* no shutdown on socket */
1406 tp->t_state = TCPS_LAST_ACK;
1407 else
1408 tp->t_state = TCPS_CLOSE_WAIT;
1409 break;
1412 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1413 * enter the CLOSING state.
1415 case TCPS_FIN_WAIT_1:
1416 tp->t_state = TCPS_CLOSING;
1417 break;
1420 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1421 * starting the time-wait timer, turning off the other
1422 * standard timers.
1424 case TCPS_FIN_WAIT_2:
1425 tp->t_state = TCPS_TIME_WAIT;
1426 tcp_canceltimers(tp);
1427 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1428 soisfdisconnected(so);
1429 break;
1432 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1434 case TCPS_TIME_WAIT:
1435 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1436 break;
1441 * If this is a small packet, then ACK now - with Nagel
1442 * congestion avoidance sender won't send more until
1443 * he gets an ACK.
1445 * See above.
1447 /* if (ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg) {
1449 /* if ((ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg &&
1450 * (so->so_iptos & IPTOS_LOWDELAY) == 0) ||
1451 * ((so->so_iptos & IPTOS_LOWDELAY) &&
1452 * ((struct tcpiphdr_2 *)ti)->first_char == (char)27)) {
1454 if (ti->ti_len && (unsigned)ti->ti_len <= 5 &&
1455 ((struct tcpiphdr_2 *)ti)->first_char == (char)27) {
1456 tp->t_flags |= TF_ACKNOW;
1460 * Return any desired output.
1462 if (needoutput || (tp->t_flags & TF_ACKNOW)) {
1463 (void) tcp_output(tp);
1465 return;
1467 dropafterack:
1469 * Generate an ACK dropping incoming segment if it occupies
1470 * sequence space, where the ACK reflects our state.
1472 if (tiflags & TH_RST)
1473 goto drop;
1474 m_freem(m);
1475 tp->t_flags |= TF_ACKNOW;
1476 (void) tcp_output(tp);
1477 return;
1479 dropwithreset:
1480 /* reuses m if m!=NULL, m_free() unnecessary */
1481 if (tiflags & TH_ACK)
1482 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1483 else {
1484 if (tiflags & TH_SYN) ti->ti_len++;
1485 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1486 TH_RST|TH_ACK);
1489 return;
1491 drop:
1493 * Drop space held by incoming segment and return.
1495 m_free(m);
1497 return;
1500 /* , ts_present, ts_val, ts_ecr) */
1501 /* int *ts_present;
1502 * u_int32_t *ts_val, *ts_ecr;
1504 static void
1505 tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt, struct tcpiphdr *ti)
1507 u_int16_t mss;
1508 int opt, optlen;
1510 DEBUG_CALL("tcp_dooptions");
1511 DEBUG_ARGS((dfd," tp = %lx cnt=%i \n", (long )tp, cnt));
1513 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1514 opt = cp[0];
1515 if (opt == TCPOPT_EOL)
1516 break;
1517 if (opt == TCPOPT_NOP)
1518 optlen = 1;
1519 else {
1520 optlen = cp[1];
1521 if (optlen <= 0)
1522 break;
1524 switch (opt) {
1526 default:
1527 continue;
1529 case TCPOPT_MAXSEG:
1530 if (optlen != TCPOLEN_MAXSEG)
1531 continue;
1532 if (!(ti->ti_flags & TH_SYN))
1533 continue;
1534 memcpy((char *) &mss, (char *) cp + 2, sizeof(mss));
1535 NTOHS(mss);
1536 (void) tcp_mss(tp, mss); /* sets t_maxseg */
1537 break;
1539 /* case TCPOPT_WINDOW:
1540 * if (optlen != TCPOLEN_WINDOW)
1541 * continue;
1542 * if (!(ti->ti_flags & TH_SYN))
1543 * continue;
1544 * tp->t_flags |= TF_RCVD_SCALE;
1545 * tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
1546 * break;
1548 /* case TCPOPT_TIMESTAMP:
1549 * if (optlen != TCPOLEN_TIMESTAMP)
1550 * continue;
1551 * *ts_present = 1;
1552 * memcpy((char *) ts_val, (char *)cp + 2, sizeof(*ts_val));
1553 * NTOHL(*ts_val);
1554 * memcpy((char *) ts_ecr, (char *)cp + 6, sizeof(*ts_ecr));
1555 * NTOHL(*ts_ecr);
1557 */ /*
1558 * * A timestamp received in a SYN makes
1559 * * it ok to send timestamp requests and replies.
1560 * */
1561 /* if (ti->ti_flags & TH_SYN) {
1562 * tp->t_flags |= TF_RCVD_TSTMP;
1563 * tp->ts_recent = *ts_val;
1564 * tp->ts_recent_age = tcp_now;
1566 */ break;
1573 * Pull out of band byte out of a segment so
1574 * it doesn't appear in the user's data queue.
1575 * It is still reflected in the segment length for
1576 * sequencing purposes.
1579 #ifdef notdef
1581 void
1582 tcp_pulloutofband(so, ti, m)
1583 struct socket *so;
1584 struct tcpiphdr *ti;
1585 register struct mbuf *m;
1587 int cnt = ti->ti_urp - 1;
1589 while (cnt >= 0) {
1590 if (m->m_len > cnt) {
1591 char *cp = mtod(m, caddr_t) + cnt;
1592 struct tcpcb *tp = sototcpcb(so);
1594 tp->t_iobc = *cp;
1595 tp->t_oobflags |= TCPOOB_HAVEDATA;
1596 memcpy(sp, cp+1, (unsigned)(m->m_len - cnt - 1));
1597 m->m_len--;
1598 return;
1600 cnt -= m->m_len;
1601 m = m->m_next; /* XXX WRONG! Fix it! */
1602 if (m == 0)
1603 break;
1605 panic("tcp_pulloutofband");
1608 #endif /* notdef */
1611 * Collect new round-trip time estimate
1612 * and update averages and current timeout.
1615 static void
1616 tcp_xmit_timer(register struct tcpcb *tp, int rtt)
1618 register short delta;
1620 DEBUG_CALL("tcp_xmit_timer");
1621 DEBUG_ARG("tp = %lx", (long)tp);
1622 DEBUG_ARG("rtt = %d", rtt);
1624 STAT(tcpstat.tcps_rttupdated++);
1625 if (tp->t_srtt != 0) {
1627 * srtt is stored as fixed point with 3 bits after the
1628 * binary point (i.e., scaled by 8). The following magic
1629 * is equivalent to the smoothing algorithm in rfc793 with
1630 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1631 * point). Adjust rtt to origin 0.
1633 delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1634 if ((tp->t_srtt += delta) <= 0)
1635 tp->t_srtt = 1;
1637 * We accumulate a smoothed rtt variance (actually, a
1638 * smoothed mean difference), then set the retransmit
1639 * timer to smoothed rtt + 4 times the smoothed variance.
1640 * rttvar is stored as fixed point with 2 bits after the
1641 * binary point (scaled by 4). The following is
1642 * equivalent to rfc793 smoothing with an alpha of .75
1643 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1644 * rfc793's wired-in beta.
1646 if (delta < 0)
1647 delta = -delta;
1648 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1649 if ((tp->t_rttvar += delta) <= 0)
1650 tp->t_rttvar = 1;
1651 } else {
1653 * No rtt measurement yet - use the unsmoothed rtt.
1654 * Set the variance to half the rtt (so our first
1655 * retransmit happens at 3*rtt).
1657 tp->t_srtt = rtt << TCP_RTT_SHIFT;
1658 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1660 tp->t_rtt = 0;
1661 tp->t_rxtshift = 0;
1664 * the retransmit should happen at rtt + 4 * rttvar.
1665 * Because of the way we do the smoothing, srtt and rttvar
1666 * will each average +1/2 tick of bias. When we compute
1667 * the retransmit timer, we want 1/2 tick of rounding and
1668 * 1 extra tick because of +-1/2 tick uncertainty in the
1669 * firing of the timer. The bias will give us exactly the
1670 * 1.5 tick we need. But, because the bias is
1671 * statistical, we have to test that we don't drop below
1672 * the minimum feasible timer (which is 2 ticks).
1674 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1675 (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */
1678 * We received an ack for a packet that wasn't retransmitted;
1679 * it is probably safe to discard any error indications we've
1680 * received recently. This isn't quite right, but close enough
1681 * for now (a route might have failed after we sent a segment,
1682 * and the return path might not be symmetrical).
1684 tp->t_softerror = 0;
1688 * Determine a reasonable value for maxseg size.
1689 * If the route is known, check route for mtu.
1690 * If none, use an mss that can be handled on the outgoing
1691 * interface without forcing IP to fragment; if bigger than
1692 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1693 * to utilize large mbufs. If no route is found, route has no mtu,
1694 * or the destination isn't local, use a default, hopefully conservative
1695 * size (usually 512 or the default IP max size, but no more than the mtu
1696 * of the interface), as we can't discover anything about intervening
1697 * gateways or networks. We also initialize the congestion/slow start
1698 * window to be a single segment if the destination isn't local.
1699 * While looking at the routing entry, we also initialize other path-dependent
1700 * parameters from pre-set or cached values in the routing entry.
1704 tcp_mss(tp, offer)
1705 register struct tcpcb *tp;
1706 u_int offer;
1708 struct socket *so = tp->t_socket;
1709 int mss;
1711 DEBUG_CALL("tcp_mss");
1712 DEBUG_ARG("tp = %lx", (long)tp);
1713 DEBUG_ARG("offer = %d", offer);
1715 mss = min(IF_MTU, IF_MRU) - sizeof(struct tcpiphdr);
1716 if (offer)
1717 mss = min(mss, offer);
1718 mss = max(mss, 32);
1719 if (mss < tp->t_maxseg || offer != 0)
1720 tp->t_maxseg = mss;
1722 tp->snd_cwnd = mss;
1724 sbreserve(&so->so_snd, TCP_SNDSPACE + ((TCP_SNDSPACE % mss) ?
1725 (mss - (TCP_SNDSPACE % mss)) :
1726 0));
1727 sbreserve(&so->so_rcv, TCP_RCVSPACE + ((TCP_RCVSPACE % mss) ?
1728 (mss - (TCP_RCVSPACE % mss)) :
1729 0));
1731 DEBUG_MISC((dfd, " returning mss = %d\n", mss));
1733 return mss;