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
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
33 * From: @(#)tcp_input.c 8.5 (Berkeley) 4/10/94
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/errno.h>
45 #include <sys/queue.h>
46 #include <sys/kernel.h>
49 #include <net/route.h>
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/in_pcb.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/tcp.h>
57 #include <netinet/tcp_fsm.h>
58 #include <netinet/tcp_seq.h>
59 #include <netinet/tcp_timer.h>
60 #include <netinet/tcp_var.h>
61 #include <netinet/tcpip.h>
63 #include <netinet/tcp_debug.h>
64 struct tcpiphdr tcp_saveti
;
67 int tcprexmtthresh
= 3;
70 struct tcpstat tcpstat
;
73 struct inpcbinfo tcbinfo
;
75 #endif /* TUBA_INCLUDE */
78 * Insert segment ti into reassembly queue of tcp with
79 * control block tp. Return TH_FIN if reassembly now includes
80 * a segment with FIN. The macro form does the common case inline
81 * (segment is the next to be received on an established connection,
82 * and the queue is empty), avoiding linkage into and removal
83 * from the queue and repetition of various conversions.
84 * Set DELACK for segments received in order, but ack immediately
85 * when segments are out of order (so fast retransmit can work).
87 #define TCP_REASS(tp, ti, m, so, flags) { \
88 if ((ti)->ti_seq == (tp)->rcv_nxt && \
89 (tp)->seg_next == (struct tcpiphdr *)(tp) && \
90 (tp)->t_state == TCPS_ESTABLISHED) { \
91 if (ti->ti_flags & TH_PUSH) \
92 tp->t_flags |= TF_ACKNOW; \
94 tp->t_flags |= TF_DELACK; \
95 (tp)->rcv_nxt += (ti)->ti_len; \
96 flags = (ti)->ti_flags & TH_FIN; \
97 tcpstat.tcps_rcvpack++;\
98 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
99 sbappend(&(so)->so_rcv, (m)); \
102 (flags) = tcp_reass((tp), (ti), (m)); \
103 tp->t_flags |= TF_ACKNOW; \
110 register struct tcpcb
*tp
;
111 register struct tcpiphdr
*ti
;
114 register struct tcpiphdr
*q
;
115 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
119 * Call with ti==0 after become established to
120 * force pre-ESTABLISHED data up to user socket.
126 * Find a segment which begins after this one does.
128 for (q
= tp
->seg_next
; q
!= (struct tcpiphdr
*)tp
;
129 q
= (struct tcpiphdr
*)q
->ti_next
)
130 if (SEQ_GT(q
->ti_seq
, ti
->ti_seq
))
134 * If there is a preceding segment, it may provide some of
135 * our data already. If so, drop the data from the incoming
136 * segment. If it provides all of our data, drop us.
138 if ((struct tcpiphdr
*)q
->ti_prev
!= (struct tcpiphdr
*)tp
) {
140 q
= (struct tcpiphdr
*)q
->ti_prev
;
141 /* conversion to int (in i) handles seq wraparound */
142 i
= q
->ti_seq
+ q
->ti_len
- ti
->ti_seq
;
144 if (i
>= ti
->ti_len
) {
145 tcpstat
.tcps_rcvduppack
++;
146 tcpstat
.tcps_rcvdupbyte
+= ti
->ti_len
;
149 * Try to present any queued data
150 * at the left window edge to the user.
151 * This is needed after the 3-WHS
154 goto present
; /* ??? */
160 q
= (struct tcpiphdr
*)(q
->ti_next
);
162 tcpstat
.tcps_rcvoopack
++;
163 tcpstat
.tcps_rcvoobyte
+= ti
->ti_len
;
164 REASS_MBUF(ti
) = m
; /* XXX */
167 * While we overlap succeeding segments trim them or,
168 * if they are completely covered, dequeue them.
170 while (q
!= (struct tcpiphdr
*)tp
) {
171 register int i
= (ti
->ti_seq
+ ti
->ti_len
) - q
->ti_seq
;
177 m_adj(REASS_MBUF(q
), i
);
180 q
= (struct tcpiphdr
*)q
->ti_next
;
181 m
= REASS_MBUF((struct tcpiphdr
*)q
->ti_prev
);
187 * Stick new segment in its place.
189 insque(ti
, q
->ti_prev
);
193 * Present data to user, advancing rcv_nxt through
194 * completed sequence space.
196 if (!TCPS_HAVEESTABLISHED(tp
->t_state
))
199 if (ti
== (struct tcpiphdr
*)tp
|| ti
->ti_seq
!= tp
->rcv_nxt
)
202 tp
->rcv_nxt
+= ti
->ti_len
;
203 OS_DbgPrint(OSK_MID_TRACE
,("Added %d to rcv_nxt (result %d)\n",
204 ti
->ti_len
, tp
->rcv_nxt
));
205 flags
= ti
->ti_flags
& TH_FIN
;
208 ti
= (struct tcpiphdr
*)ti
->ti_next
;
209 if (so
->so_state
& SS_CANTRCVMORE
)
212 sbappend(&so
->so_rcv
, m
);
213 } while (ti
!= (struct tcpiphdr
*)tp
&& ti
->ti_seq
== tp
->rcv_nxt
);
219 * TCP input routine, follows pages 65-76 of the
220 * protocol specification dated September, 1981 very closely.
224 register struct mbuf
*m
;
227 register struct tcpiphdr
*ti
;
228 register struct inpcb
*inp
;
232 register struct tcpcb
*tp
= 0;
233 register int tiflags
;
234 struct socket
*so
= 0;
235 int todrop
, acked
, ourfinisacked
, needoutput
= 0;
236 struct in_addr laddr
;
240 struct tcpopt to
; /* options in this segment */
241 struct rmxp_tao
*taop
; /* pointer to our TAO cache entry */
242 struct rmxp_tao tao_noncached
; /* in case there's no cached entry */
246 bzero((char *)&to
, sizeof(to
));
248 tcpstat
.tcps_rcvtotal
++;
250 * Get IP and TCP header together in first mbuf.
251 * Note: IP leaves IP header in first mbuf.
253 ti
= mtod(m
, struct tcpiphdr
*);
254 if (iphlen
> sizeof (struct ip
))
255 ip_stripoptions(m
, (struct mbuf
*)0);
256 if (m
->m_len
< sizeof (struct tcpiphdr
)) {
257 if ((m
= m_pullup(m
, sizeof (struct tcpiphdr
))) == 0) {
258 tcpstat
.tcps_rcvshort
++;
261 ti
= mtod(m
, struct tcpiphdr
*);
265 * Checksum extended TCP header and data.
267 tlen
= ((struct ip
*)ti
)->ip_len
;
268 len
= sizeof (struct ip
) + tlen
;
269 ti
->ti_next
= ti
->ti_prev
= 0;
271 ti
->ti_len
= (u_short
)tlen
;
273 #ifndef __REACTOS__ /* Checksum already done in IPReceive */
274 ti
->ti_sum
= in_cksum(m
, len
);
276 printf("TCP: Bad Checksum\n");
277 tcpstat
.tcps_rcvbadsum
++;
281 #endif /* TUBA_INCLUDE */
284 * Check that TCP offset makes sense,
285 * pull out TCP options and adjust length. XXX
287 off
= ti
->ti_off
<< 2;
288 if (off
< sizeof (struct tcphdr
) || off
> tlen
) {
289 tcpstat
.tcps_rcvbadoff
++;
294 if (off
> sizeof (struct tcphdr
)) {
295 if (m
->m_len
< sizeof(struct ip
) + off
) {
296 if ((m
= m_pullup(m
, sizeof (struct ip
) + off
)) == 0) {
297 tcpstat
.tcps_rcvshort
++;
300 ti
= mtod(m
, struct tcpiphdr
*);
302 optlen
= off
- sizeof (struct tcphdr
);
303 optp
= mtod(m
, caddr_t
) + sizeof (struct tcpiphdr
);
305 tiflags
= ti
->ti_flags
;
308 * Convert TCP protocol specific fields to host format.
316 * Drop TCP, IP headers and TCP options.
318 m
->m_data
+= sizeof(struct tcpiphdr
)+off
-sizeof(struct tcphdr
);
319 m
->m_len
-= sizeof(struct tcpiphdr
)+off
-sizeof(struct tcphdr
);
322 * Locate pcb for segment.
326 * First look for an exact match.
328 inp
= in_pcblookuphash(&tcbinfo
, ti
->ti_src
, ti
->ti_sport
,
329 ti
->ti_dst
, ti
->ti_dport
);
331 * ...and if that fails, do a wildcard search.
334 inp
= in_pcblookup(&tcb
, ti
->ti_src
, ti
->ti_sport
,
335 ti
->ti_dst
, ti
->ti_dport
, INPLOOKUP_WILDCARD
);
339 * If the state is CLOSED (i.e., TCB does not exist) then
340 * all data in the incoming segment is discarded.
341 * If the TCB exists but is in CLOSED state, it is embryonic,
342 * but should either do a listen or a connect soon.
349 if (tp
->t_state
== TCPS_CLOSED
)
352 /* Unscale the window into a 32-bit value. */
353 if ((tiflags
& TH_SYN
) == 0)
354 tiwin
= ti
->ti_win
<< tp
->snd_scale
;
358 so
= inp
->inp_socket
;
359 if (so
->so_options
& (SO_DEBUG
|SO_ACCEPTCONN
)) {
361 if (so
->so_options
& SO_DEBUG
) {
362 ostate
= tp
->t_state
;
366 if (so
->so_options
& SO_ACCEPTCONN
) {
367 register struct tcpcb
*tp0
= tp
;
369 if ((tiflags
& (TH_RST
|TH_ACK
|TH_SYN
)) != TH_SYN
) {
371 * Note: dropwithreset makes sure we don't
372 * send a RST in response to a RST.
374 if (tiflags
& TH_ACK
) {
375 tcpstat
.tcps_badsyn
++;
380 so2
= sonewconn(so
, 0);
382 unsigned int i
, j
, qlen
;
385 static long old_mono_secs
;
386 static unsigned int cur_cnt
, old_cnt
;
388 tcpstat
.tcps_listendrop
++;
391 * Keep a decaying average of the number
392 * of overruns we've been getting.
394 if ((i
= (mono_time
.tv_sec
-
395 old_mono_secs
)) != 0) {
396 old_mono_secs
= mono_time
.tv_sec
;
397 old_cnt
= cur_cnt
/ i
;
406 * If we've been getting a lot of hits,
407 * random drop an incomplete connection
408 * from the queue, otherwise, fall through
409 * so we head-drop from the queue.
412 if (++cur_cnt
> qlen
|| old_cnt
> qlen
) {
413 rnd
= (314159 * rnd
+ 66329) & 0xffff;
414 j
= ((qlen
+ 1) * rnd
) >> 16;
420 tcp_drop(sototcpcb(so2
), ETIMEDOUT
);
421 so2
= sonewconn(so
, 0);
428 * This is ugly, but ....
430 * Mark socket as temporary until we're
431 * committed to keeping it. The code at
432 * ``drop'' and ``dropwithreset'' check the
433 * flag dropsocket to see if the temporary
434 * socket created here should be discarded.
435 * We mark the socket as discardable until
436 * we're committed to it below in TCPS_LISTEN.
439 inp
= (struct inpcb
*)so
->so_pcb
;
440 inp
->inp_laddr
= ti
->ti_dst
;
441 inp
->inp_lport
= ti
->ti_dport
;
444 inp
->inp_options
= ip_srcroute();
447 tp
->t_state
= TCPS_LISTEN
;
448 tp
->t_flags
|= tp0
->t_flags
& (TF_NOPUSH
|TF_NOOPT
);
450 /* Compute proper scaling value from buffer space */
451 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
452 TCP_MAXWIN
<< tp
->request_r_scale
< so
->so_rcv
.sb_hiwat
)
453 tp
->request_r_scale
++;
458 * Segment received on connection.
459 * Reset idle time and keep-alive timer.
462 if (TCPS_HAVEESTABLISHED(tp
->t_state
))
463 tp
->t_timer
[TCPT_KEEP
] = tcp_keepidle
;
466 * Process options if not in LISTEN state,
467 * else do it below (after getting remote address).
469 if (tp
->t_state
!= TCPS_LISTEN
)
470 tcp_dooptions(tp
, (u_char
*)optp
, optlen
, ti
, &to
);
473 * Header prediction: check for the two common cases
474 * of a uni-directional data xfer. If the packet has
475 * no control flags, is in-sequence, the window didn't
476 * change and we're not retransmitting, it's a
477 * candidate. If the length is zero and the ack moved
478 * forward, we're the sender side of the xfer. Just
479 * free the data acked & wake any higher level process
480 * that was blocked waiting for space. If the length
481 * is non-zero and the ack didn't move, we're the
482 * receiver side. If we're getting packets in-order
483 * (the reassembly queue is empty), add the data to
484 * the socket buffer and note that we need a delayed ack.
485 * Make sure that the hidden state-flags are also off.
486 * Since we check for TCPS_ESTABLISHED above, it can only
489 if (tp
->t_state
== TCPS_ESTABLISHED
&&
490 (tiflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
)) == TH_ACK
&&
491 ((tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
)) == 0) &&
492 ((to
.to_flag
& TOF_TS
) == 0 ||
493 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
495 * Using the CC option is compulsory if once started:
496 * the segment is OK if no T/TCP was negotiated or
497 * if the segment has a CC option equal to CCrecv
499 ((tp
->t_flags
& (TF_REQ_CC
|TF_RCVD_CC
)) != (TF_REQ_CC
|TF_RCVD_CC
) ||
500 ((to
.to_flag
& TOF_CC
) != 0 && to
.to_cc
== tp
->cc_recv
)) &&
501 ti
->ti_seq
== tp
->rcv_nxt
&&
502 tiwin
&& tiwin
== tp
->snd_wnd
&&
503 tp
->snd_nxt
== tp
->snd_max
) {
506 * If last ACK falls within this segment's sequence numbers,
507 * record the timestamp.
508 * NOTE that the test is modified according to the latest
509 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
511 if ((to
.to_flag
& TOF_TS
) != 0 &&
512 SEQ_LEQ(ti
->ti_seq
, tp
->last_ack_sent
)) {
513 tp
->ts_recent_age
= tcp_now
;
514 tp
->ts_recent
= to
.to_tsval
;
517 if (ti
->ti_len
== 0) {
518 if (SEQ_GT(ti
->ti_ack
, tp
->snd_una
) &&
519 SEQ_LEQ(ti
->ti_ack
, tp
->snd_max
) &&
520 tp
->snd_cwnd
>= tp
->snd_wnd
) {
522 * this is a pure ack for outstanding data.
524 ++tcpstat
.tcps_predack
;
525 if ((to
.to_flag
& TOF_TS
) != 0)
527 tcp_now
- to
.to_tsecr
+ 1);
528 else if (tp
->t_rtt
&&
529 SEQ_GT(ti
->ti_ack
, tp
->t_rtseq
))
530 tcp_xmit_timer(tp
, tp
->t_rtt
);
531 acked
= ti
->ti_ack
- tp
->snd_una
;
532 tcpstat
.tcps_rcvackpack
++;
533 tcpstat
.tcps_rcvackbyte
+= acked
;
534 sbdrop(&so
->so_snd
, acked
);
535 tp
->snd_una
= ti
->ti_ack
;
539 * If all outstanding data are acked, stop
540 * retransmit timer, otherwise restart timer
541 * using current (possibly backed-off) value.
542 * If process is waiting for space,
543 * wakeup/selwakeup/signal. If data
544 * are ready to send, let tcp_output
545 * decide between more output or persist.
547 if (tp
->snd_una
== tp
->snd_max
)
548 tp
->t_timer
[TCPT_REXMT
] = 0;
549 else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
550 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
552 if (so
->so_snd
.sb_flags
& SB_NOTIFY
)
554 if (so
->so_snd
.sb_cc
)
555 (void) tcp_output(tp
);
558 } else if (ti
->ti_ack
== tp
->snd_una
&&
559 tp
->seg_next
== (struct tcpiphdr
*)tp
&&
560 ti
->ti_len
<= sbspace(&so
->so_rcv
)) {
562 * this is a pure, in-sequence data packet
563 * with nothing on the reassembly queue and
564 * we have enough buffer space to take it.
566 ++tcpstat
.tcps_preddat
;
567 tp
->rcv_nxt
+= ti
->ti_len
;
568 OS_DbgPrint(OSK_MID_TRACE
,("Added %d to rcv_nxt\n", ti
->ti_len
- sizeof(struct ip
)));
569 tcpstat
.tcps_rcvpack
++;
570 tcpstat
.tcps_rcvbyte
+= ti
->ti_len
;
572 * Add data to socket buffer.
574 sbappend(&so
->so_rcv
, m
);
577 * If this is a short packet, then ACK now - with Nagel
578 * congestion avoidance sender won't send more until
581 if (tiflags
& TH_PUSH
) {
582 tp
->t_flags
|= TF_ACKNOW
;
585 tp
->t_flags
|= TF_DELACK
;
592 * Calculate amount of space in receive window,
593 * and then do TCP input processing.
594 * Receive window is amount of space in rcv queue,
595 * but not less than advertised window.
599 win
= sbspace(&so
->so_rcv
);
602 tp
->rcv_wnd
= max(win
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
605 switch (tp
->t_state
) {
608 * If the state is LISTEN then ignore segment if it contains an RST.
609 * If the segment contains an ACK then it is bad and send a RST.
610 * If it does not contain a SYN then it is not interesting; drop it.
611 * Don't bother responding if the destination was a broadcast.
612 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
613 * tp->iss, and send a segment:
614 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
615 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
616 * Fill in remote peer address fields if not previously specified.
617 * Enter SYN_RECEIVED state, and process any other fields of this
618 * segment in this state.
622 register struct sockaddr_in
*sin
;
624 if (tiflags
& TH_RST
)
626 if (tiflags
& TH_ACK
)
628 if ((tiflags
& TH_SYN
) == 0)
631 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
632 * in_broadcast() should never return true on a received
633 * packet with M_BCAST not set.
635 if (m
->m_flags
& (M_BCAST
|M_MCAST
) ||
636 IN_MULTICAST(ntohl(ti
->ti_dst
.s_addr
)))
638 am
= m_get(M_DONTWAIT
, MT_SONAME
); /* XXX */
641 am
->m_len
= sizeof (struct sockaddr_in
);
642 sin
= mtod(am
, struct sockaddr_in
*);
643 sin
->sin_family
= AF_INET
;
644 sin
->sin_len
= sizeof(*sin
);
645 sin
->sin_addr
= ti
->ti_src
;
646 sin
->sin_port
= ti
->ti_sport
;
647 bzero((caddr_t
)sin
->sin_zero
, sizeof(sin
->sin_zero
));
648 laddr
= inp
->inp_laddr
;
649 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
650 inp
->inp_laddr
= ti
->ti_dst
;
651 if (in_pcbconnect(inp
, am
)) {
652 inp
->inp_laddr
= laddr
;
657 tp
->t_template
= tcp_template(tp
);
658 if (tp
->t_template
== 0) {
659 tp
= tcp_drop(tp
, ENOBUFS
);
660 dropsocket
= 0; /* socket is already gone */
663 if ((taop
= tcp_gettaocache(inp
)) == NULL
) {
664 taop
= &tao_noncached
;
665 bzero(taop
, sizeof(*taop
));
667 tcp_dooptions(tp
, (u_char
*)optp
, optlen
, ti
, &to
);
672 tcp_iss
+= TCP_ISSINCR
/2;
673 tp
->irs
= ti
->ti_seq
;
677 * Initialization of the tcpcb for transaction;
678 * set SND.WND = SEG.WND,
679 * initialize CCsend and CCrecv.
681 tp
->snd_wnd
= tiwin
; /* initial send-window */
682 tp
->cc_send
= CC_INC(tcp_ccgen
);
683 tp
->cc_recv
= to
.to_cc
;
685 * Perform TAO test on incoming CC (SEG.CC) option, if any.
686 * - compare SEG.CC against cached CC from the same host,
688 * - if SEG.CC > chached value, SYN must be new and is accepted
689 * immediately: save new CC in the cache, mark the socket
690 * connected, enter ESTABLISHED state, turn on flag to
691 * send a SYN in the next segment.
692 * A virtual advertised window is set in rcv_adv to
693 * initialize SWS prevention. Then enter normal segment
694 * processing: drop SYN, process data and FIN.
695 * - otherwise do a normal 3-way handshake.
697 if ((to
.to_flag
& TOF_CC
) != 0) {
698 if (taop
->tao_cc
!= 0 && CC_GT(to
.to_cc
, taop
->tao_cc
)) {
699 taop
->tao_cc
= to
.to_cc
;
700 tp
->t_state
= TCPS_ESTABLISHED
;
703 * If there is a FIN, or if there is data and the
704 * connection is local, then delay SYN,ACK(SYN) in
705 * the hope of piggy-backing it on a response
706 * segment. Otherwise must send ACK now in case
707 * the other side is slow starting.
709 if ((tiflags
& TH_FIN
) || (ti
->ti_len
!= 0 &&
710 in_localaddr(inp
->inp_faddr
)))
711 tp
->t_flags
|= (TF_DELACK
| TF_NEEDSYN
);
713 tp
->t_flags
|= (TF_ACKNOW
| TF_NEEDSYN
);
716 * Limit the `virtual advertised window' to TCP_MAXWIN
717 * here. Even if we requested window scaling, it will
718 * become effective only later when our SYN is acked.
720 tp
->rcv_adv
+= min(tp
->rcv_wnd
, TCP_MAXWIN
);
721 tcpstat
.tcps_connects
++;
723 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
724 dropsocket
= 0; /* committed to socket */
725 tcpstat
.tcps_accepts
++;
728 /* else do standard 3-way handshake */
731 * No CC option, but maybe CC.NEW:
732 * invalidate cached value.
737 * TAO test failed or there was no CC option,
738 * do a standard 3-way handshake.
740 tp
->t_flags
|= TF_ACKNOW
;
741 tp
->t_state
= TCPS_SYN_RECEIVED
;
742 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
743 dropsocket
= 0; /* committed to socket */
744 tcpstat
.tcps_accepts
++;
749 * If the state is SYN_RECEIVED:
750 * do just the ack and RST checks from SYN_SENT state.
751 * If the state is SYN_SENT:
752 * if seg contains an ACK, but not for our SYN, drop the input.
753 * if seg contains a RST, then drop the connection.
754 * if seg does not contain SYN, then drop it.
755 * Otherwise this is an acceptable SYN segment
756 * initialize tp->rcv_nxt and tp->irs
757 * if seg contains ack then advance tp->snd_una
758 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
759 * arrange for segment to be acked (eventually)
760 * continue processing rest of data/controls, beginning with URG
762 case TCPS_SYN_RECEIVED
:
764 if ((taop
= tcp_gettaocache(inp
)) == NULL
) {
765 taop
= &tao_noncached
;
766 bzero(taop
, sizeof(*taop
));
769 if ((tiflags
& TH_ACK
) &&
770 (SEQ_LEQ(ti
->ti_ack
, tp
->iss
) ||
771 SEQ_GT(ti
->ti_ack
, tp
->snd_max
))) {
773 * If we have a cached CCsent for the remote host,
774 * hence we haven't just crashed and restarted,
775 * do not send a RST. This may be a retransmission
776 * from the other side after our earlier ACK was lost.
777 * Our new SYN, when it arrives, will serve as the
780 if (taop
->tao_ccsent
!= 0)
785 if (tiflags
& TH_RST
) {
786 if (tiflags
& TH_ACK
)
787 tp
= tcp_drop(tp
, ECONNREFUSED
);
790 if (tp
->t_state
== TCPS_SYN_RECEIVED
)
792 if ((tiflags
& TH_SYN
) == 0)
794 tp
->snd_wnd
= ti
->ti_win
; /* initial send window */
795 tp
->cc_recv
= to
.to_cc
; /* foreign CC */
797 tp
->irs
= ti
->ti_seq
;
799 if (tiflags
& TH_ACK
) {
801 * Our SYN was acked. If segment contains CC.ECHO
802 * option, check it to make sure this segment really
803 * matches our SYN. If not, just drop it as old
804 * duplicate, but send an RST if we're still playing
807 if ((to
.to_flag
& TOF_CCECHO
) &&
808 tp
->cc_send
!= to
.to_ccecho
) {
809 if (taop
->tao_ccsent
!= 0)
814 tcpstat
.tcps_connects
++;
816 /* Do window scaling on this connection? */
817 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
818 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
819 tp
->snd_scale
= tp
->requested_s_scale
;
820 tp
->rcv_scale
= tp
->request_r_scale
;
822 /* Segment is acceptable, update cache if undefined. */
823 if (taop
->tao_ccsent
== 0)
824 taop
->tao_ccsent
= to
.to_ccecho
;
826 tp
->rcv_adv
+= tp
->rcv_wnd
;
827 tp
->snd_una
++; /* SYN is acked */
829 * If there's data, delay ACK; if there's also a FIN
830 * ACKNOW will be turned on later.
833 tp
->t_flags
|= TF_DELACK
;
835 tp
->t_flags
|= TF_ACKNOW
;
837 * Received <SYN,ACK> in SYN_SENT[*] state.
839 * SYN_SENT --> ESTABLISHED
840 * SYN_SENT* --> FIN_WAIT_1
842 if (tp
->t_flags
& TF_NEEDFIN
) {
843 tp
->t_state
= TCPS_FIN_WAIT_1
;
844 tp
->t_flags
&= ~TF_NEEDFIN
;
847 tp
->t_state
= TCPS_ESTABLISHED
;
848 tp
->t_timer
[TCPT_KEEP
] = tcp_keepidle
;
852 * Received initial SYN in SYN-SENT[*] state => simul-
853 * taneous open. If segment contains CC option and there is
854 * a cached CC, apply TAO test; if it succeeds, connection is
855 * half-synchronized. Otherwise, do 3-way handshake:
856 * SYN-SENT -> SYN-RECEIVED
857 * SYN-SENT* -> SYN-RECEIVED*
858 * If there was no CC option, clear cached CC value.
860 tp
->t_flags
|= TF_ACKNOW
;
861 tp
->t_timer
[TCPT_REXMT
] = 0;
862 if (to
.to_flag
& TOF_CC
) {
863 if (taop
->tao_cc
!= 0 &&
864 CC_GT(to
.to_cc
, taop
->tao_cc
)) {
866 * update cache and make transition:
867 * SYN-SENT -> ESTABLISHED*
868 * SYN-SENT* -> FIN-WAIT-1*
870 taop
->tao_cc
= to
.to_cc
;
871 if (tp
->t_flags
& TF_NEEDFIN
) {
872 tp
->t_state
= TCPS_FIN_WAIT_1
;
873 tp
->t_flags
&= ~TF_NEEDFIN
;
875 tp
->t_state
= TCPS_ESTABLISHED
;
876 tp
->t_timer
[TCPT_KEEP
] = tcp_keepidle
;
878 tp
->t_flags
|= TF_NEEDSYN
;
880 tp
->t_state
= TCPS_SYN_RECEIVED
;
882 /* CC.NEW or no option => invalidate cache */
884 tp
->t_state
= TCPS_SYN_RECEIVED
;
890 * Advance ti->ti_seq to correspond to first data byte.
891 * If data, trim to stay within window,
892 * dropping FIN if necessary.
895 if (ti
->ti_len
> tp
->rcv_wnd
) {
896 todrop
= ti
->ti_len
- tp
->rcv_wnd
;
898 ti
->ti_len
= tp
->rcv_wnd
;
900 tcpstat
.tcps_rcvpackafterwin
++;
901 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
903 tp
->snd_wl1
= ti
->ti_seq
- 1;
904 tp
->rcv_up
= ti
->ti_seq
;
906 * Client side of transaction: already sent SYN and data.
907 * If the remote host used T/TCP to validate the SYN,
908 * our data will be ACK'd; if so, enter normal data segment
909 * processing in the middle of step 5, ack processing.
910 * Otherwise, goto step 6.
912 if (tiflags
& TH_ACK
)
916 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
917 * if segment contains a SYN and CC [not CC.NEW] option:
918 * if state == TIME_WAIT and connection duration > MSL,
919 * drop packet and send RST;
921 * if SEG.CC > CCrecv then is new SYN, and can implicitly
922 * ack the FIN (and data) in retransmission queue.
923 * Complete close and delete TCPCB. Then reprocess
924 * segment, hoping to find new TCPCB in LISTEN state;
926 * else must be old SYN; drop it.
927 * else do normal processing.
932 if ((tiflags
& TH_SYN
) &&
933 (to
.to_flag
& TOF_CC
) && tp
->cc_recv
!= 0) {
934 if (tp
->t_state
== TCPS_TIME_WAIT
&&
935 tp
->t_duration
> TCPTV_MSL
)
937 if (CC_GT(to
.to_cc
, tp
->cc_recv
)) {
944 break; /* continue normal processing */
948 * States other than LISTEN or SYN_SENT.
949 * First check timestamp, if present.
950 * Then check the connection count, if present.
951 * Then check that at least some bytes of segment are within
952 * receive window. If segment begins before rcv_nxt,
953 * drop leading data (and SYN); if nothing left, just ack.
955 * RFC 1323 PAWS: If we have a timestamp reply on this segment
956 * and it's less than ts_recent, drop it.
958 if ((to
.to_flag
& TOF_TS
) != 0 && (tiflags
& TH_RST
) == 0 &&
959 tp
->ts_recent
&& TSTMP_LT(to
.to_tsval
, tp
->ts_recent
)) {
961 /* Check to see if ts_recent is over 24 days old. */
962 if ((int)(tcp_now
- tp
->ts_recent_age
) > TCP_PAWS_IDLE
) {
964 * Invalidate ts_recent. If this segment updates
965 * ts_recent, the age will be reset later and ts_recent
966 * will get a valid value. If it does not, setting
967 * ts_recent to zero will at least satisfy the
968 * requirement that zero be placed in the timestamp
969 * echo reply when ts_recent isn't valid. The
970 * age isn't reset until we get a valid ts_recent
971 * because we don't want out-of-order segments to be
972 * dropped when ts_recent is old.
976 tcpstat
.tcps_rcvduppack
++;
977 tcpstat
.tcps_rcvdupbyte
+= ti
->ti_len
;
978 tcpstat
.tcps_pawsdrop
++;
985 * If T/TCP was negotiated and the segment doesn't have CC,
986 * or if it's CC is wrong then drop the segment.
987 * RST segments do not have to comply with this.
989 if ((tp
->t_flags
& (TF_REQ_CC
|TF_RCVD_CC
)) == (TF_REQ_CC
|TF_RCVD_CC
) &&
990 ((to
.to_flag
& TOF_CC
) == 0 || tp
->cc_recv
!= to
.to_cc
) &&
991 (tiflags
& TH_RST
) == 0)
994 todrop
= tp
->rcv_nxt
- ti
->ti_seq
;
996 if (tiflags
& TH_SYN
) {
1006 * Following if statement from Stevens, vol. 2, p. 960.
1008 if (todrop
> ti
->ti_len
1009 || (todrop
== ti
->ti_len
&& (tiflags
& TH_FIN
) == 0)) {
1011 * Any valid FIN must be to the left of the window.
1012 * At this point the FIN must be a duplicate or out
1013 * of sequence; drop it.
1018 * Send an ACK to resynchronize and drop any data.
1019 * But keep on processing for RST or ACK.
1021 tp
->t_flags
|= TF_ACKNOW
;
1022 todrop
= ti
->ti_len
;
1023 tcpstat
.tcps_rcvduppack
++;
1024 tcpstat
.tcps_rcvdupbyte
+= todrop
;
1026 tcpstat
.tcps_rcvpartduppack
++;
1027 tcpstat
.tcps_rcvpartdupbyte
+= todrop
;
1030 ti
->ti_seq
+= todrop
;
1031 ti
->ti_len
-= todrop
;
1032 if (ti
->ti_urp
> todrop
)
1033 ti
->ti_urp
-= todrop
;
1041 * If new data are received on a connection after the
1042 * user processes are gone, then RST the other end.
1044 if ((so
->so_state
& SS_NOFDREF
) &&
1045 tp
->t_state
> TCPS_CLOSE_WAIT
&& ti
->ti_len
) {
1047 tcpstat
.tcps_rcvafterclose
++;
1052 * If segment ends after window, drop trailing data
1053 * (and PUSH and FIN); if nothing left, just ACK.
1055 todrop
= (ti
->ti_seq
+ti
->ti_len
) - (tp
->rcv_nxt
+tp
->rcv_wnd
);
1057 tcpstat
.tcps_rcvpackafterwin
++;
1058 if (todrop
>= ti
->ti_len
) {
1059 tcpstat
.tcps_rcvbyteafterwin
+= ti
->ti_len
;
1061 * If a new connection request is received
1062 * while in TIME_WAIT, drop the old connection
1063 * and start over if the sequence numbers
1064 * are above the previous ones.
1066 if (tiflags
& TH_SYN
&&
1067 tp
->t_state
== TCPS_TIME_WAIT
&&
1068 SEQ_GT(ti
->ti_seq
, tp
->rcv_nxt
)) {
1069 iss
= tp
->rcv_nxt
+ TCP_ISSINCR
;
1074 * If window is closed can only take segments at
1075 * window edge, and have to drop data and PUSH from
1076 * incoming segments. Continue processing, but
1077 * remember to ack. Otherwise, drop segment
1080 if (tp
->rcv_wnd
== 0 && ti
->ti_seq
== tp
->rcv_nxt
) {
1081 tp
->t_flags
|= TF_ACKNOW
;
1082 tcpstat
.tcps_rcvwinprobe
++;
1086 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
1088 ti
->ti_len
-= todrop
;
1089 tiflags
&= ~(TH_PUSH
|TH_FIN
);
1093 * If last ACK falls within this segment's sequence numbers,
1094 * record its timestamp.
1095 * NOTE that the test is modified according to the latest
1096 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1098 if ((to
.to_flag
& TOF_TS
) != 0 &&
1099 SEQ_LEQ(ti
->ti_seq
, tp
->last_ack_sent
)) {
1100 tp
->ts_recent_age
= tcp_now
;
1101 tp
->ts_recent
= to
.to_tsval
;
1105 * If the RST bit is set examine the state:
1106 * SYN_RECEIVED STATE:
1107 * If passive open, return to LISTEN state.
1108 * If active open, inform user that connection was refused.
1109 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1110 * Inform user that connection was reset, and close tcb.
1111 * CLOSING, LAST_ACK, TIME_WAIT STATES
1114 if (tiflags
&TH_RST
) switch (tp
->t_state
) {
1116 case TCPS_SYN_RECEIVED
:
1117 so
->so_error
= ECONNREFUSED
;
1120 case TCPS_ESTABLISHED
:
1121 case TCPS_FIN_WAIT_1
:
1122 case TCPS_FIN_WAIT_2
:
1123 case TCPS_CLOSE_WAIT
:
1124 so
->so_error
= ECONNRESET
;
1126 tp
->t_state
= TCPS_CLOSED
;
1127 tcpstat
.tcps_drops
++;
1133 case TCPS_TIME_WAIT
:
1139 * If a SYN is in the window, then this is an
1140 * error and we send an RST and drop the connection.
1142 if (tiflags
& TH_SYN
) {
1143 tp
= tcp_drop(tp
, ECONNRESET
);
1148 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
1149 * flag is on (half-synchronized state), then queue data for
1150 * later processing; else drop segment and return.
1152 if ((tiflags
& TH_ACK
) == 0) {
1153 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
1154 (tp
->t_flags
& TF_NEEDSYN
))
1163 switch (tp
->t_state
) {
1166 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
1167 * ESTABLISHED state and continue processing, otherwise
1170 case TCPS_SYN_RECEIVED
:
1171 if (SEQ_GT(tp
->snd_una
, ti
->ti_ack
) ||
1172 SEQ_GT(ti
->ti_ack
, tp
->snd_max
))
1175 tcpstat
.tcps_connects
++;
1177 /* Do window scaling? */
1178 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
1179 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
1180 tp
->snd_scale
= tp
->requested_s_scale
;
1181 tp
->rcv_scale
= tp
->request_r_scale
;
1184 * Upon successful completion of 3-way handshake,
1185 * update cache.CC if it was undefined, pass any queued
1186 * data to the user, and advance state appropriately.
1188 if ((taop
= tcp_gettaocache(inp
)) != NULL
&&
1190 taop
->tao_cc
= tp
->cc_recv
;
1194 * SYN-RECEIVED -> ESTABLISHED
1195 * SYN-RECEIVED* -> FIN-WAIT-1
1197 if (tp
->t_flags
& TF_NEEDFIN
) {
1198 tp
->t_state
= TCPS_FIN_WAIT_1
;
1199 tp
->t_flags
&= ~TF_NEEDFIN
;
1201 tp
->t_state
= TCPS_ESTABLISHED
;
1202 tp
->t_timer
[TCPT_KEEP
] = tcp_keepidle
;
1205 * If segment contains data or ACK, will call tcp_reass()
1206 * later; if not, do so now to pass queued data to user.
1208 if (ti
->ti_len
== 0 && (tiflags
& TH_FIN
) == 0)
1209 (void) tcp_reass(tp
, (struct tcpiphdr
*)0,
1211 tp
->snd_wl1
= ti
->ti_seq
- 1;
1215 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1216 * ACKs. If the ack is in the range
1217 * tp->snd_una < ti->ti_ack <= tp->snd_max
1218 * then advance tp->snd_una to ti->ti_ack and drop
1219 * data from the retransmission queue. If this ACK reflects
1220 * more up to date window information we update our window information.
1222 case TCPS_ESTABLISHED
:
1223 case TCPS_FIN_WAIT_1
:
1224 case TCPS_FIN_WAIT_2
:
1225 case TCPS_CLOSE_WAIT
:
1228 case TCPS_TIME_WAIT
:
1230 if (SEQ_LEQ(ti
->ti_ack
, tp
->snd_una
)) {
1231 if (ti
->ti_len
== 0 && tiwin
== tp
->snd_wnd
) {
1232 tcpstat
.tcps_rcvdupack
++;
1234 * If we have outstanding data (other than
1235 * a window probe), this is a completely
1236 * duplicate ack (ie, window info didn't
1237 * change), the ack is the biggest we've
1238 * seen and we've seen exactly our rexmt
1239 * threshhold of them, assume a packet
1240 * has been dropped and retransmit it.
1241 * Kludge snd_nxt & the congestion
1242 * window so we send only this one
1245 * We know we're losing at the current
1246 * window size so do congestion avoidance
1247 * (set ssthresh to half the current window
1248 * and pull our congestion window back to
1249 * the new ssthresh).
1251 * Dup acks mean that packets have left the
1252 * network (they're now cached at the receiver)
1253 * so bump cwnd by the amount in the receiver
1254 * to keep a constant cwnd packets in the
1257 if (tp
->t_timer
[TCPT_REXMT
] == 0 ||
1258 ti
->ti_ack
!= tp
->snd_una
)
1260 else if (++tp
->t_dupacks
== tcprexmtthresh
) {
1261 tcp_seq onxt
= tp
->snd_nxt
;
1263 min(tp
->snd_wnd
, tp
->snd_cwnd
) / 2 /
1268 tp
->snd_ssthresh
= win
* tp
->t_maxseg
;
1269 tp
->t_timer
[TCPT_REXMT
] = 0;
1271 tp
->snd_nxt
= ti
->ti_ack
;
1272 tp
->snd_cwnd
= tp
->t_maxseg
;
1273 (void) tcp_output(tp
);
1274 tp
->snd_cwnd
= tp
->snd_ssthresh
+
1275 tp
->t_maxseg
* tp
->t_dupacks
;
1276 if (SEQ_GT(onxt
, tp
->snd_nxt
))
1279 } else if (tp
->t_dupacks
> tcprexmtthresh
) {
1280 tp
->snd_cwnd
+= tp
->t_maxseg
;
1281 (void) tcp_output(tp
);
1289 * If the congestion window was inflated to account
1290 * for the other side's cached packets, retract it.
1292 if (tp
->t_dupacks
> tcprexmtthresh
&&
1293 tp
->snd_cwnd
> tp
->snd_ssthresh
)
1294 tp
->snd_cwnd
= tp
->snd_ssthresh
;
1296 if (SEQ_GT(ti
->ti_ack
, tp
->snd_max
)) {
1297 tcpstat
.tcps_rcvacktoomuch
++;
1301 * If we reach this point, ACK is not a duplicate,
1302 * i.e., it ACKs something we sent.
1304 if (tp
->t_flags
& TF_NEEDSYN
) {
1306 * T/TCP: Connection was half-synchronized, and our
1307 * SYN has been ACK'd (so connection is now fully
1308 * synchronized). Go to non-starred state,
1309 * increment snd_una for ACK of SYN, and check if
1310 * we can do window scaling.
1312 tp
->t_flags
&= ~TF_NEEDSYN
;
1314 /* Do window scaling? */
1315 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
1316 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
1317 tp
->snd_scale
= tp
->requested_s_scale
;
1318 tp
->rcv_scale
= tp
->request_r_scale
;
1323 acked
= ti
->ti_ack
- tp
->snd_una
;
1324 tcpstat
.tcps_rcvackpack
++;
1325 tcpstat
.tcps_rcvackbyte
+= acked
;
1328 * If we have a timestamp reply, update smoothed
1329 * round trip time. If no timestamp is present but
1330 * transmit timer is running and timed sequence
1331 * number was acked, update smoothed round trip time.
1332 * Since we now have an rtt measurement, cancel the
1333 * timer backoff (cf., Phil Karn's retransmit alg.).
1334 * Recompute the initial retransmit timer.
1336 if (to
.to_flag
& TOF_TS
)
1337 tcp_xmit_timer(tp
, tcp_now
- to
.to_tsecr
+ 1);
1338 else if (tp
->t_rtt
&& SEQ_GT(ti
->ti_ack
, tp
->t_rtseq
))
1339 tcp_xmit_timer(tp
,tp
->t_rtt
);
1342 * If all outstanding data is acked, stop retransmit
1343 * timer and remember to restart (more output or persist).
1344 * If there is more data to be acked, restart retransmit
1345 * timer, using current (possibly backed-off) value.
1347 if (ti
->ti_ack
== tp
->snd_max
) {
1348 tp
->t_timer
[TCPT_REXMT
] = 0;
1350 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
1351 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
1354 * If no data (only SYN) was ACK'd,
1355 * skip rest of ACK processing.
1361 * When new data is acked, open the congestion window.
1362 * If the window gives us less than ssthresh packets
1363 * in flight, open exponentially (maxseg per packet).
1364 * Otherwise open linearly: maxseg per window
1365 * (maxseg^2 / cwnd per packet).
1368 register u_int cw
= tp
->snd_cwnd
;
1369 register u_int incr
= tp
->t_maxseg
;
1371 if (cw
> tp
->snd_ssthresh
)
1372 incr
= incr
* incr
/ cw
;
1373 tp
->snd_cwnd
= min(cw
+ incr
, TCP_MAXWIN
<<tp
->snd_scale
);
1375 if (acked
> so
->so_snd
.sb_cc
) {
1376 tp
->snd_wnd
-= so
->so_snd
.sb_cc
;
1377 sbdrop(&so
->so_snd
, (int)so
->so_snd
.sb_cc
);
1380 sbdrop(&so
->so_snd
, acked
);
1381 tp
->snd_wnd
-= acked
;
1384 if (so
->so_snd
.sb_flags
& SB_NOTIFY
)
1386 tp
->snd_una
= ti
->ti_ack
;
1387 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
1388 tp
->snd_nxt
= tp
->snd_una
;
1390 switch (tp
->t_state
) {
1393 * In FIN_WAIT_1 STATE in addition to the processing
1394 * for the ESTABLISHED state if our FIN is now acknowledged
1395 * then enter FIN_WAIT_2.
1397 case TCPS_FIN_WAIT_1
:
1398 if (ourfinisacked
) {
1400 * If we can't receive any more
1401 * data, then closing user can proceed.
1402 * Starting the timer is contrary to the
1403 * specification, but if we don't get a FIN
1404 * we'll hang forever.
1406 if (so
->so_state
& SS_CANTRCVMORE
) {
1407 soisdisconnected(so
);
1408 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;
1410 tp
->t_state
= TCPS_FIN_WAIT_2
;
1415 * In CLOSING STATE in addition to the processing for
1416 * the ESTABLISHED state if the ACK acknowledges our FIN
1417 * then enter the TIME-WAIT state, otherwise ignore
1421 if (ourfinisacked
) {
1422 tp
->t_state
= TCPS_TIME_WAIT
;
1423 tcp_canceltimers(tp
);
1424 /* Shorten TIME_WAIT [RFC-1644, p.28] */
1425 if (tp
->cc_recv
!= 0 &&
1426 tp
->t_duration
< TCPTV_MSL
)
1427 tp
->t_timer
[TCPT_2MSL
] =
1428 tp
->t_rxtcur
* TCPTV_TWTRUNC
;
1430 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
1431 soisdisconnected(so
);
1436 * In LAST_ACK, we may still be waiting for data to drain
1437 * and/or to be acked, as well as for the ack of our FIN.
1438 * If our FIN is now acknowledged, delete the TCB,
1439 * enter the closed state and return.
1442 if (ourfinisacked
) {
1449 * In TIME_WAIT state the only thing that should arrive
1450 * is a retransmission of the remote FIN. Acknowledge
1451 * it and restart the finack timer.
1453 case TCPS_TIME_WAIT
:
1454 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
1461 * Update window information.
1462 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1464 if ((tiflags
& TH_ACK
) &&
1465 (SEQ_LT(tp
->snd_wl1
, ti
->ti_seq
) ||
1466 (tp
->snd_wl1
== ti
->ti_seq
&& (SEQ_LT(tp
->snd_wl2
, ti
->ti_ack
) ||
1467 (tp
->snd_wl2
== ti
->ti_ack
&& tiwin
> tp
->snd_wnd
))))) {
1468 /* keep track of pure window updates */
1469 if (ti
->ti_len
== 0 &&
1470 tp
->snd_wl2
== ti
->ti_ack
&& tiwin
> tp
->snd_wnd
)
1471 tcpstat
.tcps_rcvwinupd
++;
1472 tp
->snd_wnd
= tiwin
;
1473 tp
->snd_wl1
= ti
->ti_seq
;
1474 tp
->snd_wl2
= ti
->ti_ack
;
1475 if (tp
->snd_wnd
> tp
->max_sndwnd
)
1476 tp
->max_sndwnd
= tp
->snd_wnd
;
1481 * Process segments with URG.
1483 if ((tiflags
& TH_URG
) && ti
->ti_urp
&&
1484 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
1486 * This is a kludge, but if we receive and accept
1487 * random urgent pointers, we'll crash in
1488 * soreceive. It's hard to imagine someone
1489 * actually wanting to send this much urgent data.
1491 if (ti
->ti_urp
+ so
->so_rcv
.sb_cc
> sb_max
) {
1492 ti
->ti_urp
= 0; /* XXX */
1493 tiflags
&= ~TH_URG
; /* XXX */
1494 goto dodata
; /* XXX */
1497 * If this segment advances the known urgent pointer,
1498 * then mark the data stream. This should not happen
1499 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1500 * a FIN has been received from the remote side.
1501 * In these states we ignore the URG.
1503 * According to RFC961 (Assigned Protocols),
1504 * the urgent pointer points to the last octet
1505 * of urgent data. We continue, however,
1506 * to consider it to indicate the first octet
1507 * of data past the urgent section as the original
1508 * spec states (in one of two places).
1510 if (SEQ_GT(ti
->ti_seq
+ti
->ti_urp
, tp
->rcv_up
)) {
1511 tp
->rcv_up
= ti
->ti_seq
+ ti
->ti_urp
;
1512 so
->so_oobmark
= so
->so_rcv
.sb_cc
+
1513 (tp
->rcv_up
- tp
->rcv_nxt
) - 1;
1514 if (so
->so_oobmark
== 0)
1515 so
->so_state
|= SS_RCVATMARK
;
1517 tp
->t_oobflags
&= ~(TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
1520 * Remove out of band data so doesn't get presented to user.
1521 * This can happen independent of advancing the URG pointer,
1522 * but if two URG's are pending at once, some out-of-band
1523 * data may creep in... ick.
1525 if (ti
->ti_urp
<= (u_long
)ti
->ti_len
1527 && (so
->so_options
& SO_OOBINLINE
) == 0
1530 tcp_pulloutofband(so
, ti
, m
);
1533 * If no out of band data is expected,
1534 * pull receive urgent pointer along
1535 * with the receive window.
1537 if (SEQ_GT(tp
->rcv_nxt
, tp
->rcv_up
))
1538 tp
->rcv_up
= tp
->rcv_nxt
;
1542 * Process the segment text, merging it into the TCP sequencing queue,
1543 * and arranging for acknowledgment of receipt if necessary.
1544 * This process logically involves adjusting tp->rcv_wnd as data
1545 * is presented to the user (this happens in tcp_usrreq.c,
1546 * case PRU_RCVD). If a FIN has already been received on this
1547 * connection then we just ignore the text.
1549 if ((ti
->ti_len
|| (tiflags
&TH_FIN
)) &&
1550 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
1551 TCP_REASS(tp
, ti
, m
, so
, tiflags
);
1553 * Note the amount of data that peer has sent into
1554 * our window, in order to estimate the sender's
1557 len
= so
->so_rcv
.sb_hiwat
- (tp
->rcv_adv
- tp
->rcv_nxt
);
1564 * If FIN is received ACK the FIN and let the user know
1565 * that the connection is closing.
1567 if (tiflags
& TH_FIN
) {
1568 if (TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
1571 * If connection is half-synchronized
1572 * (ie NEEDSYN flag on) then delay ACK,
1573 * so it may be piggybacked when SYN is sent.
1574 * Otherwise, since we received a FIN then no
1575 * more input can be expected, send ACK now.
1577 if (tp
->t_flags
& TF_NEEDSYN
)
1578 tp
->t_flags
|= TF_DELACK
;
1580 tp
->t_flags
|= TF_ACKNOW
;
1583 switch (tp
->t_state
) {
1586 * In SYN_RECEIVED and ESTABLISHED STATES
1587 * enter the CLOSE_WAIT state.
1589 case TCPS_SYN_RECEIVED
:
1590 case TCPS_ESTABLISHED
:
1591 tp
->t_state
= TCPS_CLOSE_WAIT
;
1595 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1596 * enter the CLOSING state.
1598 case TCPS_FIN_WAIT_1
:
1599 tp
->t_state
= TCPS_CLOSING
;
1603 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1604 * starting the time-wait timer, turning off the other
1607 case TCPS_FIN_WAIT_2
:
1608 tp
->t_state
= TCPS_TIME_WAIT
;
1609 tcp_canceltimers(tp
);
1610 /* Shorten TIME_WAIT [RFC-1644, p.28] */
1611 if (tp
->cc_recv
!= 0 &&
1612 tp
->t_duration
< TCPTV_MSL
) {
1613 tp
->t_timer
[TCPT_2MSL
] =
1614 tp
->t_rxtcur
* TCPTV_TWTRUNC
;
1615 /* For transaction client, force ACK now. */
1616 tp
->t_flags
|= TF_ACKNOW
;
1619 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
1620 soisdisconnected(so
);
1624 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1626 case TCPS_TIME_WAIT
:
1627 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
1632 if (so
->so_options
& SO_DEBUG
)
1633 tcp_trace(TA_INPUT
, ostate
, tp
, &tcp_saveti
, 0);
1637 * Return any desired output.
1639 if (needoutput
|| (tp
->t_flags
& TF_ACKNOW
))
1640 (void) tcp_output(tp
);
1645 * Generate an ACK dropping incoming segment if it occupies
1646 * sequence space, where the ACK reflects our state.
1648 if (tiflags
& TH_RST
)
1651 if (so
->so_options
& SO_DEBUG
)
1652 tcp_trace(TA_DROP
, ostate
, tp
, &tcp_saveti
, 0);
1655 tp
->t_flags
|= TF_ACKNOW
;
1656 (void) tcp_output(tp
);
1661 * Generate a RST, dropping incoming segment.
1662 * Make ACK acceptable to originator of segment.
1663 * Don't bother to respond if destination was broadcast/multicast.
1665 if ((tiflags
& TH_RST
) || m
->m_flags
& (M_BCAST
|M_MCAST
) ||
1666 IN_MULTICAST(ntohl(ti
->ti_dst
.s_addr
)))
1669 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
1670 tcp_trace(TA_DROP
, ostate
, tp
, &tcp_saveti
, 0);
1672 if (tiflags
& TH_ACK
)
1673 tcp_respond(tp
, ti
, m
, (tcp_seq
)0, ti
->ti_ack
, TH_RST
);
1675 if (tiflags
& TH_SYN
)
1677 tcp_respond(tp
, ti
, m
, ti
->ti_seq
+ti
->ti_len
, (tcp_seq
)0,
1680 /* destroy temporarily created socket */
1687 * Drop space held by incoming segment and return.
1690 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
1691 tcp_trace(TA_DROP
, ostate
, tp
, &tcp_saveti
, 0);
1694 /* destroy temporarily created socket */
1698 #ifndef TUBA_INCLUDE
1702 tcp_dooptions(tp
, cp
, cnt
, ti
, to
)
1706 struct tcpiphdr
*ti
;
1712 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1714 if (opt
== TCPOPT_EOL
)
1716 if (opt
== TCPOPT_NOP
)
1729 if (optlen
!= TCPOLEN_MAXSEG
)
1731 if (!(ti
->ti_flags
& TH_SYN
))
1733 bcopy((char *) cp
+ 2, (char *) &mss
, sizeof(mss
));
1738 if (optlen
!= TCPOLEN_WINDOW
)
1740 if (!(ti
->ti_flags
& TH_SYN
))
1742 tp
->t_flags
|= TF_RCVD_SCALE
;
1743 tp
->requested_s_scale
= min(cp
[2], TCP_MAX_WINSHIFT
);
1746 case TCPOPT_TIMESTAMP
:
1747 if (optlen
!= TCPOLEN_TIMESTAMP
)
1749 to
->to_flag
|= TOF_TS
;
1750 bcopy((char *)cp
+ 2,
1751 (char *)&to
->to_tsval
, sizeof(to
->to_tsval
));
1752 NTOHL(to
->to_tsval
);
1753 bcopy((char *)cp
+ 6,
1754 (char *)&to
->to_tsecr
, sizeof(to
->to_tsecr
));
1755 NTOHL(to
->to_tsecr
);
1758 * A timestamp received in a SYN makes
1759 * it ok to send timestamp requests and replies.
1761 if (ti
->ti_flags
& TH_SYN
) {
1762 tp
->t_flags
|= TF_RCVD_TSTMP
;
1763 tp
->ts_recent
= to
->to_tsval
;
1764 tp
->ts_recent_age
= tcp_now
;
1768 if (optlen
!= TCPOLEN_CC
)
1770 to
->to_flag
|= TOF_CC
;
1771 bcopy((char *)cp
+ 2,
1772 (char *)&to
->to_cc
, sizeof(to
->to_cc
));
1775 * A CC or CC.new option received in a SYN makes
1776 * it ok to send CC in subsequent segments.
1778 if (ti
->ti_flags
& TH_SYN
)
1779 tp
->t_flags
|= TF_RCVD_CC
;
1782 if (optlen
!= TCPOLEN_CC
)
1784 if (!(ti
->ti_flags
& TH_SYN
))
1786 to
->to_flag
|= TOF_CCNEW
;
1787 bcopy((char *)cp
+ 2,
1788 (char *)&to
->to_cc
, sizeof(to
->to_cc
));
1791 * A CC or CC.new option received in a SYN makes
1792 * it ok to send CC in subsequent segments.
1794 tp
->t_flags
|= TF_RCVD_CC
;
1797 if (optlen
!= TCPOLEN_CC
)
1799 if (!(ti
->ti_flags
& TH_SYN
))
1801 to
->to_flag
|= TOF_CCECHO
;
1802 bcopy((char *)cp
+ 2,
1803 (char *)&to
->to_ccecho
, sizeof(to
->to_ccecho
));
1804 NTOHL(to
->to_ccecho
);
1808 if (ti
->ti_flags
& TH_SYN
)
1809 tcp_mss(tp
, mss
); /* sets t_maxseg */
1813 * Pull out of band byte out of a segment so
1814 * it doesn't appear in the user's data queue.
1815 * It is still reflected in the segment length for
1816 * sequencing purposes.
1819 tcp_pulloutofband(so
, ti
, m
)
1821 struct tcpiphdr
*ti
;
1822 register struct mbuf
*m
;
1824 int cnt
= ti
->ti_urp
- 1;
1827 if (m
->m_len
> cnt
) {
1828 char *cp
= mtod(m
, caddr_t
) + cnt
;
1829 struct tcpcb
*tp
= sototcpcb(so
);
1832 tp
->t_oobflags
|= TCPOOB_HAVEDATA
;
1833 bcopy(cp
+1, cp
, (unsigned)(m
->m_len
- cnt
- 1));
1842 panic("tcp_pulloutofband");
1846 * Collect new round-trip time estimate
1847 * and update averages and current timeout.
1850 tcp_xmit_timer(tp
, rtt
)
1851 register struct tcpcb
*tp
;
1854 register short delta
;
1856 tcpstat
.tcps_rttupdated
++;
1858 if (tp
->t_srtt
!= 0) {
1860 * srtt is stored as fixed point with 3 bits after the
1861 * binary point (i.e., scaled by 8). The following magic
1862 * is equivalent to the smoothing algorithm in rfc793 with
1863 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1864 * point). Adjust rtt to origin 0.
1866 delta
= rtt
- 1 - (tp
->t_srtt
>> TCP_RTT_SHIFT
);
1867 if ((tp
->t_srtt
+= delta
) <= 0)
1870 * We accumulate a smoothed rtt variance (actually, a
1871 * smoothed mean difference), then set the retransmit
1872 * timer to smoothed rtt + 4 times the smoothed variance.
1873 * rttvar is stored as fixed point with 2 bits after the
1874 * binary point (scaled by 4). The following is
1875 * equivalent to rfc793 smoothing with an alpha of .75
1876 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1877 * rfc793's wired-in beta.
1881 delta
-= (tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
);
1882 if ((tp
->t_rttvar
+= delta
) <= 0)
1886 * No rtt measurement yet - use the unsmoothed rtt.
1887 * Set the variance to half the rtt (so our first
1888 * retransmit happens at 3*rtt).
1890 tp
->t_srtt
= rtt
<< TCP_RTT_SHIFT
;
1891 tp
->t_rttvar
= rtt
<< (TCP_RTTVAR_SHIFT
- 1);
1897 * the retransmit should happen at rtt + 4 * rttvar.
1898 * Because of the way we do the smoothing, srtt and rttvar
1899 * will each average +1/2 tick of bias. When we compute
1900 * the retransmit timer, we want 1/2 tick of rounding and
1901 * 1 extra tick because of +-1/2 tick uncertainty in the
1902 * firing of the timer. The bias will give us exactly the
1903 * 1.5 tick we need. But, because the bias is
1904 * statistical, we have to test that we don't drop below
1905 * the minimum feasible timer (which is 2 ticks).
1907 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
1908 tp
->t_rttmin
, TCPTV_REXMTMAX
);
1911 * We received an ack for a packet that wasn't retransmitted;
1912 * it is probably safe to discard any error indications we've
1913 * received recently. This isn't quite right, but close enough
1914 * for now (a route might have failed after we sent a segment,
1915 * and the return path might not be symmetrical).
1917 tp
->t_softerror
= 0;
1921 * Determine a reasonable value for maxseg size.
1922 * If the route is known, check route for mtu.
1923 * If none, use an mss that can be handled on the outgoing
1924 * interface without forcing IP to fragment; if bigger than
1925 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1926 * to utilize large mbufs. If no route is found, route has no mtu,
1927 * or the destination isn't local, use a default, hopefully conservative
1928 * size (usually 512 or the default IP max size, but no more than the mtu
1929 * of the interface), as we can't discover anything about intervening
1930 * gateways or networks. We also initialize the congestion/slow start
1931 * window to be a single segment if the destination isn't local.
1932 * While looking at the routing entry, we also initialize other path-dependent
1933 * parameters from pre-set or cached values in the routing entry.
1935 * Also take into account the space needed for options that we
1936 * send regularly. Make maxseg shorter by that amount to assure
1937 * that we can send maxseg amount of data even when the options
1938 * are present. Store the upper limit of the length of options plus
1941 * NOTE that this routine is only called when we process an incoming
1942 * segment, for outgoing segments only tcp_mssopt is called.
1944 * In case of T/TCP, we call this routine during implicit connection
1945 * setup as well (offer = -1), to initialize maxseg from the cached
1953 register struct rtentry
*rt
;
1954 struct ifnet
*ifp
= NULL
;
1955 register int rtt
, mss
;
1959 struct rmxp_tao
*taop
;
1960 int origoffer
= offer
;
1963 if ((rt
= tcp_rtlookup(inp
)) == NULL
) {
1965 tp
->t_maxopd
= tp
->t_maxseg
= tcp_mssdflt
;
1967 if (offer
< tcp_mssdflt
)
1968 tp
->t_maxopd
= tp
->t_maxseg
= tcp_mssdflt
;
1970 tp
->t_maxopd
= tp
->t_maxseg
= min(offer
, tcp_mssopt(tp
));
1977 so
= inp
->inp_socket
;
1979 taop
= rmx_taop(rt
->rt_rmx
);
1981 * Offer == -1 means that we didn't receive SYN yet,
1982 * use cached value in that case;
1985 offer
= taop
->tao_mssopt
;
1987 * Offer == 0 means that there was no MSS on the SYN segment,
1988 * in this case we use tcp_mssdflt.
1991 offer
= tcp_mssdflt
;
1994 * Sanity check: make sure that maxopd will be large
1995 * enough to allow some data on segments even is the
1996 * all the option space is used (40bytes). Otherwise
1997 * funny things may happen in tcp_output.
1999 offer
= max(offer
, 64);
2000 taop
->tao_mssopt
= offer
;
2003 * While we're here, check if there's an initial rtt
2004 * or rttvar. Convert from the route-table units
2005 * to scaled multiples of the slow timeout timer.
2007 if (tp
->t_srtt
== 0 && (rtt
= rt
->rt_rmx
.rmx_rtt
)) {
2009 * XXX the lock bit for RTT indicates that the value
2010 * is also a minimum value; this is subject to time.
2012 if (rt
->rt_rmx
.rmx_locks
& RTV_RTT
)
2013 tp
->t_rttmin
= rtt
/ (RTM_RTTUNIT
/ PR_SLOWHZ
);
2014 tp
->t_srtt
= rtt
/ (RTM_RTTUNIT
/ (PR_SLOWHZ
* TCP_RTT_SCALE
));
2015 tcpstat
.tcps_usedrtt
++;
2016 if (rt
->rt_rmx
.rmx_rttvar
) {
2017 tp
->t_rttvar
= rt
->rt_rmx
.rmx_rttvar
/
2018 (RTM_RTTUNIT
/ (PR_SLOWHZ
* TCP_RTTVAR_SCALE
));
2019 tcpstat
.tcps_usedrttvar
++;
2021 /* default variation is +- 1 rtt */
2023 tp
->t_srtt
* TCP_RTTVAR_SCALE
/ TCP_RTT_SCALE
;
2025 TCPT_RANGESET(tp
->t_rxtcur
,
2026 ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1,
2027 tp
->t_rttmin
, TCPTV_REXMTMAX
);
2030 * if there's an mtu associated with the route, use it
2032 if (rt
->rt_rmx
.rmx_mtu
)
2033 mss
= rt
->rt_rmx
.rmx_mtu
- sizeof(struct tcpiphdr
);
2036 mss
= ifp
->if_mtu
- sizeof(struct tcpiphdr
);
2037 if (!in_localaddr(inp
->inp_faddr
))
2038 mss
= min(mss
, tcp_mssdflt
);
2040 mss
= min(mss
, offer
);
2042 * maxopd stores the maximum length of data AND options
2043 * in a segment; maxseg is the amount of data in a normal
2044 * segment. We need to store this value (maxopd) apart
2045 * from maxseg, because now every segment carries options
2046 * and thus we normally have somewhat less data in segments.
2051 * In case of T/TCP, origoffer==-1 indicates, that no segments
2052 * were received yet. In this case we just guess, otherwise
2053 * we do the same as before T/TCP.
2055 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
2057 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
))
2058 mss
-= TCPOLEN_TSTAMP_APPA
;
2059 if ((tp
->t_flags
& (TF_REQ_CC
|TF_NOOPT
)) == TF_REQ_CC
&&
2061 (tp
->t_flags
& TF_RCVD_CC
) == TF_RCVD_CC
))
2062 mss
-= TCPOLEN_CC_APPA
;
2064 #if (MCLBYTES & (MCLBYTES - 1)) == 0
2066 mss
&= ~(MCLBYTES
-1);
2069 mss
= mss
/ MCLBYTES
* MCLBYTES
;
2072 * If there's a pipesize, change the socket buffer
2073 * to that size. Make the socket buffers an integral
2074 * number of mss units; if the mss is larger than
2075 * the socket buffer, decrease the mss.
2078 if ((bufsize
= rt
->rt_rmx
.rmx_sendpipe
) == 0)
2080 bufsize
= so
->so_snd
.sb_hiwat
;
2084 bufsize
= roundup(bufsize
, mss
);
2085 if (bufsize
> sb_max
)
2087 (void)sbreserve(&so
->so_snd
, bufsize
);
2092 if ((bufsize
= rt
->rt_rmx
.rmx_recvpipe
) == 0)
2094 bufsize
= so
->so_rcv
.sb_hiwat
;
2095 if (bufsize
> mss
) {
2096 bufsize
= roundup(bufsize
, mss
);
2097 if (bufsize
> sb_max
)
2099 (void)sbreserve(&so
->so_rcv
, bufsize
);
2102 * Don't force slow-start on local network.
2104 if (!in_localaddr(inp
->inp_faddr
))
2107 if (rt
->rt_rmx
.rmx_ssthresh
) {
2109 * There's some sort of gateway or interface
2110 * buffer limit on the path. Use this to set
2111 * the slow start threshhold, but set the
2112 * threshold to no less than 2*mss.
2114 tp
->snd_ssthresh
= max(2 * mss
, rt
->rt_rmx
.rmx_ssthresh
);
2115 tcpstat
.tcps_usedssthresh
++;
2120 * Determine the MSS option to send on an outgoing SYN.
2129 rt
= tcp_rtlookup(tp
->t_inpcb
);
2133 return rt
->rt_ifp
->if_mtu
- sizeof(struct tcpiphdr
);
2135 struct ifaddr
*ifa
= ifa_ifwithnet((struct sockaddr
*)&tp
->t_inpcb
->inp_faddr
);
2139 return ifa
->ifa_mtu
- sizeof(struct tcpiphdr
);
2142 #endif /* TUBA_INCLUDE */