2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2006 Pavel Fedin
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * From: @(#)tcp_input.c 8.5 (Berkeley) 4/10/94
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/protosw.h>
46 #include <sys/errno.h>
47 #include <sys/queue.h>
49 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/in_pcb.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/tcp.h>
58 #include <netinet/tcp_fsm.h>
59 #include <netinet/tcp_seq.h>
60 #include <netinet/tcp_timer.h>
61 #include <netinet/tcp_var.h>
62 #include <netinet/tcpip.h>
64 #include <netinet/tcp_debug.h>
65 struct tcpiphdr tcp_saveti
;
68 int tcprexmtthresh
= 3;
76 struct tcpstat tcpstat
;
79 struct inpcbinfo tcbinfo
;
81 #endif /* TUBA_INCLUDE */
83 #include <kern/uipc_socket2_protos.h>
84 //#include <netinet/tcp_subr_protos.h>
87 * Insert segment ti into reassembly queue of tcp with
88 * control block tp. Return TH_FIN if reassembly now includes
89 * a segment with FIN. The macro form does the common case inline
90 * (segment is the next to be received on an established connection,
91 * and the queue is empty), avoiding linkage into and removal
92 * from the queue and repetition of various conversions.
93 * Set DELACK for segments received in order, but ack immediately
94 * when segments are out of order (so fast retransmit can work).
96 #define TCP_REASS(tp, ti, m, so, flags) { \
97 if ((ti)->ti_seq == (tp)->rcv_nxt && \
98 (tp)->t_segq == NULL && \
99 (tp)->t_state == TCPS_ESTABLISHED) { \
100 if (ti->ti_flags & TH_PUSH) \
101 tp->t_flags |= TF_ACKNOW; \
103 tp->t_flags |= TF_DELACK; \
104 (tp)->rcv_nxt += (ti)->ti_len; \
105 flags = (ti)->ti_flags & TH_FIN; \
106 tcpstat.tcps_rcvpack++;\
107 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
108 sbappend(&(so)->so_rcv, (m)); \
111 (flags) = tcp_reass((tp), (ti), (m)); \
112 tp->t_flags |= TF_ACKNOW; \
125 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
128 #define GETTCP(m) ((struct tcpiphdr *)m->m_pkthdr.header)
131 * Call with ti==0 after become established to
132 * force pre-ESTABLISHED data up to user socket.
137 m
->m_pkthdr
.header
= (caddr_t
)ti
;
140 * Find a segment which begins after this one does.
142 for (q
= tp
->t_segq
, p
= NULL
; q
; p
= q
, q
= q
->m_nextpkt
)
143 if (SEQ_GT(GETTCP(q
)->ti_seq
, ti
->ti_seq
))
147 * If there is a preceding segment, it may provide some of
148 * our data already. If so, drop the data from the incoming
149 * segment. If it provides all of our data, drop us.
153 /* conversion to int (in i) handles seq wraparound */
154 i
= GETTCP(p
)->ti_seq
+ GETTCP(p
)->ti_len
- ti
->ti_seq
;
156 if (i
>= ti
->ti_len
) {
157 tcpstat
.tcps_rcvduppack
++;
158 tcpstat
.tcps_rcvdupbyte
+= ti
->ti_len
;
161 * Try to present any queued data
162 * at the left window edge to the user.
163 * This is needed after the 3-WHS
166 goto present
; /* ??? */
173 tcpstat
.tcps_rcvoopack
++;
174 tcpstat
.tcps_rcvoobyte
+= ti
->ti_len
;
177 * While we overlap succeeding segments trim them or,
178 * if they are completely covered, dequeue them.
181 register int i
= (ti
->ti_seq
+ ti
->ti_len
) - GETTCP(q
)->ti_seq
;
184 if (i
< GETTCP(q
)->ti_len
) {
185 GETTCP(q
)->ti_seq
+= i
;
186 GETTCP(q
)->ti_len
-= i
;
201 m
->m_nextpkt
= tp
->t_segq
;
204 m
->m_nextpkt
= p
->m_nextpkt
;
210 * Present data to user, advancing rcv_nxt through
211 * completed sequence space.
213 if (!TCPS_HAVEESTABLISHED(tp
->t_state
))
216 if (!q
|| GETTCP(q
)->ti_seq
!= tp
->rcv_nxt
)
219 tp
->rcv_nxt
+= GETTCP(q
)->ti_len
;
220 flags
= GETTCP(q
)->ti_flags
& TH_FIN
;
224 if (so
->so_state
& SS_CANTRCVMORE
)
227 sbappend(&so
->so_rcv
, q
);
229 } while (q
&& GETTCP(q
)->ti_seq
== tp
->rcv_nxt
);
237 * TCP input routine, follows pages 65-76 of the
238 * protocol specification dated September, 1981 very closely.
240 void tcp_input(void *arg
, ...)
242 register struct mbuf
*m
= arg
;
244 register struct tcpiphdr
*ti
;
245 register struct inpcb
*inp
;
249 register struct tcpcb
*tp
= 0;
250 register int tiflags
;
251 struct socket
*so
= 0;
252 int todrop
, acked
, ourfinisacked
, needoutput
= 0;
253 struct in_addr laddr
;
257 struct tcpopt to
; /* options in this segment */
258 struct rmxp_tao
*taop
; /* pointer to our TAO cache entry */
259 struct rmxp_tao tao_noncached
; /* in case there's no cached entry */
266 iphlen
= va_arg(va
, int);
270 bzero((char *)&to
, sizeof(to
));
272 tcpstat
.tcps_rcvtotal
++;
274 * Get IP and TCP header together in first mbuf.
275 * Note: IP leaves IP header in first mbuf.
277 ti
= mtod(m
, struct tcpiphdr
*);
278 if (iphlen
> sizeof (struct ip
))
279 ip_stripoptions(m
, (struct mbuf
*)0);
280 if (m
->m_len
< sizeof (struct tcpiphdr
)) {
281 if ((m
= m_pullup(m
, sizeof (struct tcpiphdr
))) == 0) {
282 tcpstat
.tcps_rcvshort
++;
285 ti
= mtod(m
, struct tcpiphdr
*);
289 * Checksum extended TCP header and data.
291 tlen
= ((struct ip
*)ti
)->ip_len
;
292 len
= sizeof (struct ip
) + tlen
;
293 bzero(ti
->ti_x1
, sizeof(ti
->ti_x1
));
294 ti
->ti_len
= (u_short
)tlen
;
296 ti
->ti_sum
= in_cksum(m
, len
);
298 tcpstat
.tcps_rcvbadsum
++;
303 * Check that TCP offset makes sense,
304 * pull out TCP options and adjust length. XXX
306 off
= ti
->ti_off
<< 2;
307 if (off
< sizeof (struct tcphdr
) || off
> tlen
) {
308 tcpstat
.tcps_rcvbadoff
++;
313 if (off
> sizeof (struct tcphdr
)) {
314 if (m
->m_len
< sizeof(struct ip
) + off
) {
315 if ((m
= m_pullup(m
, sizeof (struct ip
) + off
)) == 0) {
316 tcpstat
.tcps_rcvshort
++;
319 ti
= mtod(m
, struct tcpiphdr
*);
321 optlen
= off
- sizeof (struct tcphdr
);
322 optp
= mtod(m
, caddr_t
) + sizeof (struct tcpiphdr
);
324 * Do quick retrieval of timestamp options ("options
325 * prediction?"). If timestamp is the only option and it's
326 * formatted as recommended in RFC 1323 appendix A, we
327 * quickly get the values now and not bother calling
328 * tcp_dooptions(), etc.
330 if ((optlen
== TCPOLEN_TSTAMP_APPA
||
331 (optlen
> TCPOLEN_TSTAMP_APPA
&&
332 optp
[TCPOLEN_TSTAMP_APPA
] == TCPOPT_EOL
)) &&
333 *(u_long
*)optp
== htonl(TCPOPT_TSTAMP_HDR
) &&
334 (ti
->ti_flags
& TH_SYN
) == 0) {
335 to
.to_flag
|= TOF_TS
;
336 to
.to_tsval
= ntohl(*(u_long
*)(optp
+ 4));
337 to
.to_tsecr
= ntohl(*(u_long
*)(optp
+ 8));
338 optp
= NULL
; /* we've parsed the options */
341 tiflags
= ti
->ti_flags
;
344 * Convert TCP protocol specific fields to host format.
352 * Drop TCP, IP headers and TCP options.
354 m
->m_data
+= sizeof(struct tcpiphdr
)+off
-sizeof(struct tcphdr
);
355 m
->m_len
-= sizeof(struct tcpiphdr
)+off
-sizeof(struct tcphdr
);
358 * Locate pcb for segment.
362 * First look for an exact match.
364 inp
= in_pcblookuphash(&tcbinfo
, ti
->ti_src
, ti
->ti_sport
,
365 ti
->ti_dst
, ti
->ti_dport
);
367 * ...and if that fails, do a wildcard search.
370 inp
= in_pcblookup(&tcb
, ti
->ti_src
, ti
->ti_sport
,
371 ti
->ti_dst
, ti
->ti_dport
, INPLOOKUP_WILDCARD
);
375 * If the state is CLOSED (i.e., TCB does not exist) then
376 * all data in the incoming segment is discarded.
377 * If the TCB exists but is in CLOSED state, it is embryonic,
378 * but should either do a listen or a connect soon.
385 if (tp
->t_state
== TCPS_CLOSED
)
388 /* Unscale the window into a 32-bit value. */
389 if ((tiflags
& TH_SYN
) == 0)
390 tiwin
= ti
->ti_win
<< tp
->snd_scale
;
394 so
= inp
->inp_socket
;
395 if (so
->so_options
& (SO_DEBUG
|SO_ACCEPTCONN
)) {
397 if (so
->so_options
& SO_DEBUG
) {
398 ostate
= tp
->t_state
;
402 if (so
->so_options
& SO_ACCEPTCONN
) {
403 register struct tcpcb
*tp0
= tp
;
405 * call to access control.. (AmiTCP/IP extra)
407 if (controlaccess(ti
->ti_src
, ti
->ti_dport
) == 0)
409 so
= sonewconn(so
, 0);
413 * This is ugly, but ....
415 * Mark socket as temporary until we're
416 * committed to keeping it. The code at
417 * ``drop'' and ``dropwithreset'' check the
418 * flag dropsocket to see if the temporary
419 * socket created here should be discarded.
420 * We mark the socket as discardable until
421 * we're committed to it below in TCPS_LISTEN.
424 inp
= (struct inpcb
*)so
->so_pcb
;
425 inp
->inp_laddr
= ti
->ti_dst
;
426 inp
->inp_lport
= ti
->ti_dport
;
429 inp
->inp_options
= ip_srcroute();
432 tp
->t_state
= TCPS_LISTEN
;
433 tp
->t_flags
|= tp0
->t_flags
& (TF_NOPUSH
|TF_NOOPT
);
435 /* Compute proper scaling value from buffer space */
436 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
437 TCP_MAXWIN
<< tp
->request_r_scale
< so
->so_rcv
.sb_hiwat
)
438 tp
->request_r_scale
++;
443 * Segment received on connection.
444 * Reset idle time and keep-alive timer.
447 tp
->t_timer
[TCPT_KEEP
] = tcp_keepidle
;
450 * Process options if not in LISTEN state,
451 * else do it below (after getting remote address).
453 if (optp
&& tp
->t_state
!= TCPS_LISTEN
)
454 tcp_dooptions(tp
, optp
, optlen
, ti
,
458 * Header prediction: check for the two common cases
459 * of a uni-directional data xfer. If the packet has
460 * no control flags, is in-sequence, the window didn't
461 * change and we're not retransmitting, it's a
462 * candidate. If the length is zero and the ack moved
463 * forward, we're the sender side of the xfer. Just
464 * free the data acked & wake any higher level process
465 * that was blocked waiting for space. If the length
466 * is non-zero and the ack didn't move, we're the
467 * receiver side. If we're getting packets in-order
468 * (the reassembly queue is empty), add the data to
469 * the socket buffer and note that we need a delayed ack.
470 * Make sure that the hidden state-flags are also off.
471 * Since we check for TCPS_ESTABLISHED above, it can only
474 if (tp
->t_state
== TCPS_ESTABLISHED
&&
475 (tiflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
)) == TH_ACK
&&
476 ((tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
)) == 0) &&
477 ((to
.to_flag
& TOF_TS
) == 0 ||
478 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
480 * Using the CC option is compulsory if once started:
481 * the segment is OK if no T/TCP was negotiated or
482 * if the segment has a CC option equal to CCrecv
484 ((tp
->t_flags
& (TF_REQ_CC
|TF_RCVD_CC
)) != (TF_REQ_CC
|TF_RCVD_CC
) ||
485 (to
.to_flag
& TOF_CC
) != 0 && to
.to_cc
== tp
->cc_recv
) &&
486 ti
->ti_seq
== tp
->rcv_nxt
&&
487 tiwin
&& tiwin
== tp
->snd_wnd
&&
488 tp
->snd_nxt
== tp
->snd_max
) {
491 * If last ACK falls within this segment's sequence numbers,
492 * record the timestamp.
493 * NOTE that the test is modified according to the latest
494 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
496 if ((to
.to_flag
& TOF_TS
) != 0 &&
497 SEQ_LEQ(ti
->ti_seq
, tp
->last_ack_sent
)) {
498 tp
->ts_recent_age
= tcp_now
;
499 tp
->ts_recent
= to
.to_tsval
;
502 if (ti
->ti_len
== 0) {
503 if (SEQ_GT(ti
->ti_ack
, tp
->snd_una
) &&
504 SEQ_LEQ(ti
->ti_ack
, tp
->snd_max
) &&
505 tp
->snd_cwnd
>= tp
->snd_wnd
) {
507 * this is a pure ack for outstanding data.
509 ++tcpstat
.tcps_predack
;
510 if ((to
.to_flag
& TOF_TS
) != 0)
512 tcp_now
- to
.to_tsecr
+ 1);
513 else if (tp
->t_rtt
&&
514 SEQ_GT(ti
->ti_ack
, tp
->t_rtseq
))
515 tcp_xmit_timer(tp
, tp
->t_rtt
);
516 acked
= ti
->ti_ack
- tp
->snd_una
;
517 tcpstat
.tcps_rcvackpack
++;
518 tcpstat
.tcps_rcvackbyte
+= acked
;
519 sbdrop(&so
->so_snd
, acked
);
520 tp
->snd_una
= ti
->ti_ack
;
524 * If all outstanding data are acked, stop
525 * retransmit timer, otherwise restart timer
526 * using current (possibly backed-off) value.
527 * If process is waiting for space,
528 * wakeup/selwakeup/signal. If data
529 * are ready to send, let tcp_output
530 * decide between more output or persist.
532 if (tp
->snd_una
== tp
->snd_max
)
533 tp
->t_timer
[TCPT_REXMT
] = 0;
534 else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
535 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
537 if (so
->so_snd
.sb_flags
& SB_NOTIFY
)
539 if (so
->so_snd
.sb_cc
)
540 (void) tcp_output(tp
);
543 } else if (ti
->ti_ack
== tp
->snd_una
&&
544 tp
->t_segq
== NULL
&&
545 ti
->ti_len
<= sbspace(&so
->so_rcv
)) {
547 * this is a pure, in-sequence data packet
548 * with nothing on the reassembly queue and
549 * we have enough buffer space to take it.
551 ++tcpstat
.tcps_preddat
;
552 tp
->rcv_nxt
+= ti
->ti_len
;
553 tcpstat
.tcps_rcvpack
++;
554 tcpstat
.tcps_rcvbyte
+= ti
->ti_len
;
556 * Add data to socket buffer.
558 sbappend(&so
->so_rcv
, m
);
562 * If this is a short packet, then ACK now - with Nagel
563 * congestion avoidance sender won't send more until
566 if (tiflags
& TH_PUSH
) {
567 tp
->t_flags
|= TF_ACKNOW
;
570 tp
->t_flags
|= TF_DELACK
;
573 tp
->t_flags
|= TF_DELACK
;
580 * Calculate amount of space in receive window,
581 * and then do TCP input processing.
582 * Receive window is amount of space in rcv queue,
583 * but not less than advertised window.
587 win
= sbspace(&so
->so_rcv
);
590 tp
->rcv_wnd
= MAX(win
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
593 switch (tp
->t_state
) {
596 * If the state is LISTEN then ignore segment if it contains an RST.
597 * If the segment contains an ACK then it is bad and send a RST.
598 * If it does not contain a SYN then it is not interesting; drop it.
599 * Don't bother responding if the destination was a broadcast.
600 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
601 * tp->iss, and send a segment:
602 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
603 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
604 * Fill in remote peer address fields if not previously specified.
605 * Enter SYN_RECEIVED state, and process any other fields of this
606 * segment in this state.
610 register struct sockaddr_in
*sin
;
612 if (tiflags
& TH_RST
)
614 if (tiflags
& TH_ACK
)
616 if ((tiflags
& TH_SYN
) == 0)
619 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
620 * in_broadcast() should never return true on a received
621 * packet with M_BCAST not set.
623 if (m
->m_flags
& (M_BCAST
|M_MCAST
) ||
624 IN_MULTICAST(ntohl(ti
->ti_dst
.s_addr
)))
626 am
= m_get(M_DONTWAIT
, MT_SONAME
); /* XXX */
629 am
->m_len
= sizeof (struct sockaddr_in
);
630 sin
= mtod(am
, struct sockaddr_in
*);
631 sin
->sin_family
= AF_INET
;
632 sin
->sin_len
= sizeof(*sin
);
633 sin
->sin_addr
= ti
->ti_src
;
634 sin
->sin_port
= ti
->ti_sport
;
635 bzero((caddr_t
)sin
->sin_zero
, sizeof(sin
->sin_zero
));
636 laddr
= inp
->inp_laddr
;
637 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
638 inp
->inp_laddr
= ti
->ti_dst
;
639 if (in_pcbconnect(inp
, am
)) {
640 inp
->inp_laddr
= laddr
;
645 tp
->t_template
= tcp_template(tp
);
646 if (tp
->t_template
== 0) {
647 tp
= tcp_drop(tp
, ENOBUFS
);
648 dropsocket
= 0; /* socket is already gone */
651 if ((taop
= tcp_gettaocache(inp
)) == NULL
) {
652 taop
= &tao_noncached
;
653 bzero(taop
, sizeof(*taop
));
656 tcp_dooptions(tp
, optp
, optlen
, ti
,
662 tcp_iss
+= TCP_ISSINCR
/2;
663 tp
->irs
= ti
->ti_seq
;
667 * Initialization of the tcpcb for transaction;
668 * set SND.WND = SEG.WND,
669 * initialize CCsend and CCrecv.
671 tp
->snd_wnd
= tiwin
; /* initial send-window */
672 tp
->cc_send
= CC_INC(tcp_ccgen
);
673 tp
->cc_recv
= to
.to_cc
;
675 * Perform TAO test on incoming CC (SEG.CC) option, if any.
676 * - compare SEG.CC against cached CC from the same host,
678 * - if SEG.CC > chached value, SYN must be new and is accepted
679 * immediately: save new CC in the cache, mark the socket
680 * connected, enter ESTABLISHED state, turn on flag to
681 * send a SYN in the next segment.
682 * A virtual advertised window is set in rcv_adv to
683 * initialize SWS prevention. Then enter normal segment
684 * processing: drop SYN, process data and FIN.
685 * - otherwise do a normal 3-way handshake.
687 if ((to
.to_flag
& TOF_CC
) != 0) {
688 if (taop
->tao_cc
!= 0 && CC_GT(to
.to_cc
, taop
->tao_cc
)) {
689 taop
->tao_cc
= to
.to_cc
;
690 tp
->t_state
= TCPS_ESTABLISHED
;
693 * If there is a FIN, or if there is data and the
694 * connection is local, then delay SYN,ACK(SYN) in
695 * the hope of piggy-backing it on a response
696 * segment. Otherwise must send ACK now in case
697 * the other side is slow starting.
699 if ((tiflags
& TH_FIN
) || (ti
->ti_len
!= 0 &&
700 in_localaddr(inp
->inp_faddr
)))
701 tp
->t_flags
|= (TF_DELACK
| TF_NEEDSYN
);
703 tp
->t_flags
|= (TF_ACKNOW
| TF_NEEDSYN
);
704 tp
->rcv_adv
+= tp
->rcv_wnd
;
705 tcpstat
.tcps_connects
++;
707 tp
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
;
708 dropsocket
= 0; /* committed to socket */
709 tcpstat
.tcps_accepts
++;
712 /* else do standard 3-way handshake */
715 * No CC option, but maybe CC.NEW:
716 * invalidate cached value.
721 * TAO test failed or there was no CC option,
722 * do a standard 3-way handshake.
724 tp
->t_flags
|= TF_ACKNOW
;
725 tp
->t_state
= TCPS_SYN_RECEIVED
;
726 tp
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
;
727 dropsocket
= 0; /* committed to socket */
728 tcpstat
.tcps_accepts
++;
733 * If the state is SYN_SENT:
734 * if seg contains an ACK, but not for our SYN, drop the input.
735 * if seg contains a RST, then drop the connection.
736 * if seg does not contain SYN, then drop it.
737 * Otherwise this is an acceptable SYN segment
738 * initialize tp->rcv_nxt and tp->irs
739 * if seg contains ack then advance tp->snd_una
740 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
741 * arrange for segment to be acked (eventually)
742 * continue processing rest of data/controls, beginning with URG
745 if ((taop
= tcp_gettaocache(inp
)) == NULL
) {
746 taop
= &tao_noncached
;
747 bzero(taop
, sizeof(*taop
));
750 if ((tiflags
& TH_ACK
) &&
751 (SEQ_LEQ(ti
->ti_ack
, tp
->iss
) ||
752 SEQ_GT(ti
->ti_ack
, tp
->snd_max
))) {
754 * If we have a cached CCsent for the remote host,
755 * hence we haven't just crashed and restarted,
756 * do not send a RST. This may be a retransmission
757 * from the other side after our earlier ACK was lost.
758 * Our new SYN, when it arrives, will serve as the
761 if (taop
->tao_ccsent
!= 0)
766 if (tiflags
& TH_RST
) {
767 if (tiflags
& TH_ACK
)
768 tp
= tcp_drop(tp
, ECONNREFUSED
);
771 if ((tiflags
& TH_SYN
) == 0)
773 tp
->snd_wnd
= ti
->ti_win
; /* initial send window */
774 tp
->cc_recv
= to
.to_cc
; /* foreign CC */
776 tp
->irs
= ti
->ti_seq
;
778 if (tiflags
& TH_ACK
&& SEQ_GT(ti
->ti_ack
, tp
->iss
)) {
779 tcpstat
.tcps_connects
++;
781 /* Do window scaling on this connection? */
782 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
783 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
784 tp
->snd_scale
= tp
->requested_s_scale
;
785 tp
->rcv_scale
= tp
->request_r_scale
;
788 * Our SYN was acked. If segment contains CC.ECHO
789 * option, check it to make sure this segment really
790 * matches our SYN. If not, just drop it as old
791 * duplicate, but send an RST if we're still playing
794 if ((to
.to_flag
& TOF_CCECHO
) &&
795 tp
->cc_send
!= to
.to_ccecho
) {
796 if (taop
->tao_ccsent
!= 0)
801 /* Segment is acceptable, update cache if undefined. */
802 if (taop
->tao_ccsent
== 0)
803 taop
->tao_ccsent
= to
.to_ccecho
;
805 tp
->rcv_adv
+= tp
->rcv_wnd
;
806 tp
->snd_una
++; /* SYN is acked */
808 * If there's data, delay ACK; if there's also a FIN
809 * ACKNOW will be turned on later.
812 tp
->t_flags
|= TF_DELACK
;
814 tp
->t_flags
|= TF_ACKNOW
;
816 * Received <SYN,ACK> in SYN_SENT[*] state.
818 * SYN_SENT --> ESTABLISHED
819 * SYN_SENT* --> FIN_WAIT_1
821 if (tp
->t_flags
& TF_NEEDFIN
) {
822 tp
->t_state
= TCPS_FIN_WAIT_1
;
823 tp
->t_flags
&= ~TF_NEEDFIN
;
826 tp
->t_state
= TCPS_ESTABLISHED
;
830 * Received initial SYN in SYN-SENT[*] state => simul-
831 * taneous open. If segment contains CC option and there is
832 * a cached CC, apply TAO test; if it succeeds, connection is
833 * half-synchronized. Otherwise, do 3-way handshake:
834 * SYN-SENT -> SYN-RECEIVED
835 * SYN-SENT* -> SYN-RECEIVED*
836 * If there was no CC option, clear cached CC value.
838 tp
->t_flags
|= TF_ACKNOW
;
839 tp
->t_timer
[TCPT_REXMT
] = 0;
840 if (to
.to_flag
& TOF_CC
) {
841 if (taop
->tao_cc
!= 0 &&
842 CC_GT(to
.to_cc
, taop
->tao_cc
)) {
844 * update cache and make transition:
845 * SYN-SENT -> ESTABLISHED*
846 * SYN-SENT* -> FIN-WAIT-1*
848 taop
->tao_cc
= to
.to_cc
;
849 if (tp
->t_flags
& TF_NEEDFIN
) {
850 tp
->t_state
= TCPS_FIN_WAIT_1
;
851 tp
->t_flags
&= ~TF_NEEDFIN
;
853 tp
->t_state
= TCPS_ESTABLISHED
;
854 tp
->t_flags
|= TF_NEEDSYN
;
856 tp
->t_state
= TCPS_SYN_RECEIVED
;
858 /* CC.NEW or no option => invalidate cache */
860 tp
->t_state
= TCPS_SYN_RECEIVED
;
866 * Advance ti->ti_seq to correspond to first data byte.
867 * If data, trim to stay within window,
868 * dropping FIN if necessary.
871 if (ti
->ti_len
> tp
->rcv_wnd
) {
872 todrop
= ti
->ti_len
- tp
->rcv_wnd
;
874 ti
->ti_len
= tp
->rcv_wnd
;
876 tcpstat
.tcps_rcvpackafterwin
++;
877 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
879 tp
->snd_wl1
= ti
->ti_seq
- 1;
880 tp
->rcv_up
= ti
->ti_seq
;
882 * Client side of transaction: already sent SYN and data.
883 * If the remote host used T/TCP to validate the SYN,
884 * our data will be ACK'd; if so, enter normal data segment
885 * processing in the middle of step 5, ack processing.
886 * Otherwise, goto step 6.
888 if (tiflags
& TH_ACK
)
892 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
893 * if segment contains a SYN and CC [not CC.NEW] option:
894 * if state == TIME_WAIT and connection duration > MSL,
895 * drop packet and send RST;
897 * if SEG.CC > CCrecv then is new SYN, and can implicitly
898 * ack the FIN (and data) in retransmission queue.
899 * Complete close and delete TCPCB. Then reprocess
900 * segment, hoping to find new TCPCB in LISTEN state;
902 * else must be old SYN; drop it.
903 * else do normal processing.
908 if ((tiflags
& TH_SYN
) &&
909 (to
.to_flag
& TOF_CC
) && tp
->cc_recv
!= 0) {
910 if (tp
->t_state
== TCPS_TIME_WAIT
&&
911 tp
->t_duration
> TCPTV_MSL
)
913 if (CC_GT(to
.to_cc
, tp
->cc_recv
)) {
920 break; /* continue normal processing */
924 * States other than LISTEN or SYN_SENT.
925 * First check timestamp, if present.
926 * Then check the connection count, if present.
927 * Then check that at least some bytes of segment are within
928 * receive window. If segment begins before rcv_nxt,
929 * drop leading data (and SYN); if nothing left, just ack.
931 * RFC 1323 PAWS: If we have a timestamp reply on this segment
932 * and it's less than ts_recent, drop it.
934 if ((to
.to_flag
& TOF_TS
) != 0 && (tiflags
& TH_RST
) == 0 &&
935 tp
->ts_recent
&& TSTMP_LT(to
.to_tsval
, tp
->ts_recent
)) {
937 /* Check to see if ts_recent is over 24 days old. */
938 if ((int)(tcp_now
- tp
->ts_recent_age
) > TCP_PAWS_IDLE
) {
940 * Invalidate ts_recent. If this segment updates
941 * ts_recent, the age will be reset later and ts_recent
942 * will get a valid value. If it does not, setting
943 * ts_recent to zero will at least satisfy the
944 * requirement that zero be placed in the timestamp
945 * echo reply when ts_recent isn't valid. The
946 * age isn't reset until we get a valid ts_recent
947 * because we don't want out-of-order segments to be
948 * dropped when ts_recent is old.
952 tcpstat
.tcps_rcvduppack
++;
953 tcpstat
.tcps_rcvdupbyte
+= ti
->ti_len
;
954 tcpstat
.tcps_pawsdrop
++;
961 * If T/TCP was negotiated and the segment doesn't have CC,
962 * or if it's CC is wrong then drop the segment.
963 * RST segments do not have to comply with this.
965 if ((tp
->t_flags
& (TF_REQ_CC
|TF_RCVD_CC
)) == (TF_REQ_CC
|TF_RCVD_CC
) &&
966 ((to
.to_flag
& TOF_CC
) == 0 || tp
->cc_recv
!= to
.to_cc
) &&
967 (tiflags
& TH_RST
) == 0)
970 todrop
= tp
->rcv_nxt
- ti
->ti_seq
;
972 if (tiflags
& TH_SYN
) {
982 * Following if statement from Stevens, vol. 2, p. 960.
984 if (todrop
> ti
->ti_len
985 || (todrop
== ti
->ti_len
&& (tiflags
& TH_FIN
) == 0)) {
987 * Any valid FIN must be to the left of the window.
988 * At this point the FIN must be a duplicate or out
989 * of sequence; drop it.
994 * Send an ACK to resynchronize and drop any data.
995 * But keep on processing for RST or ACK.
997 tp
->t_flags
|= TF_ACKNOW
;
999 tcpstat
.tcps_rcvduppack
++;
1000 tcpstat
.tcps_rcvdupbyte
+= todrop
;
1002 tcpstat
.tcps_rcvpartduppack
++;
1003 tcpstat
.tcps_rcvpartdupbyte
+= todrop
;
1006 ti
->ti_seq
+= todrop
;
1007 ti
->ti_len
-= todrop
;
1008 if (ti
->ti_urp
> todrop
)
1009 ti
->ti_urp
-= todrop
;
1017 * If new data are received on a connection after the
1018 * user processes are gone, then RST the other end.
1020 if ((so
->so_state
& SS_NOFDREF
) &&
1021 tp
->t_state
> TCPS_CLOSE_WAIT
&& ti
->ti_len
) {
1023 tcpstat
.tcps_rcvafterclose
++;
1028 * If segment ends after window, drop trailing data
1029 * (and PUSH and FIN); if nothing left, just ACK.
1031 todrop
= (ti
->ti_seq
+ti
->ti_len
) - (tp
->rcv_nxt
+tp
->rcv_wnd
);
1033 tcpstat
.tcps_rcvpackafterwin
++;
1034 if (todrop
>= ti
->ti_len
) {
1035 tcpstat
.tcps_rcvbyteafterwin
+= ti
->ti_len
;
1037 * If a new connection request is received
1038 * while in TIME_WAIT, drop the old connection
1039 * and start over if the sequence numbers
1040 * are above the previous ones.
1042 if (tiflags
& TH_SYN
&&
1043 tp
->t_state
== TCPS_TIME_WAIT
&&
1044 SEQ_GT(ti
->ti_seq
, tp
->rcv_nxt
)) {
1045 iss
= tp
->rcv_nxt
+ TCP_ISSINCR
;
1050 * If window is closed can only take segments at
1051 * window edge, and have to drop data and PUSH from
1052 * incoming segments. Continue processing, but
1053 * remember to ack. Otherwise, drop segment
1056 if (tp
->rcv_wnd
== 0 && ti
->ti_seq
== tp
->rcv_nxt
) {
1057 tp
->t_flags
|= TF_ACKNOW
;
1058 tcpstat
.tcps_rcvwinprobe
++;
1062 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
1064 ti
->ti_len
-= todrop
;
1065 tiflags
&= ~(TH_PUSH
|TH_FIN
);
1069 * If last ACK falls within this segment's sequence numbers,
1070 * record its timestamp.
1071 * NOTE that the test is modified according to the latest
1072 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1074 if ((to
.to_flag
& TOF_TS
) != 0 &&
1075 SEQ_LEQ(ti
->ti_seq
, tp
->last_ack_sent
)) {
1076 tp
->ts_recent_age
= tcp_now
;
1077 tp
->ts_recent
= to
.to_tsval
;
1081 * If the RST bit is set examine the state:
1082 * SYN_RECEIVED STATE:
1083 * If passive open, return to LISTEN state.
1084 * If active open, inform user that connection was refused.
1085 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1086 * Inform user that connection was reset, and close tcb.
1087 * CLOSING, LAST_ACK, TIME_WAIT STATES
1090 if (tiflags
&TH_RST
) switch (tp
->t_state
) {
1092 case TCPS_SYN_RECEIVED
:
1093 so
->so_error
= ECONNREFUSED
;
1096 case TCPS_ESTABLISHED
:
1097 case TCPS_FIN_WAIT_1
:
1098 case TCPS_FIN_WAIT_2
:
1099 case TCPS_CLOSE_WAIT
:
1100 so
->so_error
= ECONNRESET
;
1102 tp
->t_state
= TCPS_CLOSED
;
1103 tcpstat
.tcps_drops
++;
1109 case TCPS_TIME_WAIT
:
1115 * If a SYN is in the window, then this is an
1116 * error and we send an RST and drop the connection.
1118 if (tiflags
& TH_SYN
) {
1119 tp
= tcp_drop(tp
, ECONNRESET
);
1124 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
1125 * flag is on (half-synchronized state), then queue data for
1126 * later processing; else drop segment and return.
1128 if ((tiflags
& TH_ACK
) == 0) {
1129 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
1130 (tp
->t_flags
& TF_NEEDSYN
))
1139 switch (tp
->t_state
) {
1142 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
1143 * ESTABLISHED state and continue processing, otherwise
1146 case TCPS_SYN_RECEIVED
:
1147 if (SEQ_GT(tp
->snd_una
, ti
->ti_ack
) ||
1148 SEQ_GT(ti
->ti_ack
, tp
->snd_max
))
1151 tcpstat
.tcps_connects
++;
1153 /* Do window scaling? */
1154 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
1155 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
1156 tp
->snd_scale
= tp
->requested_s_scale
;
1157 tp
->rcv_scale
= tp
->request_r_scale
;
1160 * Upon successful completion of 3-way handshake,
1161 * update cache.CC if it was undefined, pass any queued
1162 * data to the user, and advance state appropriately.
1164 if ((taop
= tcp_gettaocache(inp
)) != NULL
&&
1166 taop
->tao_cc
= tp
->cc_recv
;
1170 * SYN-RECEIVED -> ESTABLISHED
1171 * SYN-RECEIVED* -> FIN-WAIT-1
1173 if (tp
->t_flags
& TF_NEEDFIN
) {
1174 tp
->t_state
= TCPS_FIN_WAIT_1
;
1175 tp
->t_flags
&= ~TF_NEEDFIN
;
1177 tp
->t_state
= TCPS_ESTABLISHED
;
1179 * If segment contains data or ACK, will call tcp_reass()
1180 * later; if not, do so now to pass queued data to user.
1182 if (ti
->ti_len
== 0 && (tiflags
& TH_FIN
) == 0)
1183 (void) tcp_reass(tp
, (struct tcpiphdr
*)0,
1185 tp
->snd_wl1
= ti
->ti_seq
- 1;
1189 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1190 * ACKs. If the ack is in the range
1191 * tp->snd_una < ti->ti_ack <= tp->snd_max
1192 * then advance tp->snd_una to ti->ti_ack and drop
1193 * data from the retransmission queue. If this ACK reflects
1194 * more up to date window information we update our window information.
1196 case TCPS_ESTABLISHED
:
1197 case TCPS_FIN_WAIT_1
:
1198 case TCPS_FIN_WAIT_2
:
1199 case TCPS_CLOSE_WAIT
:
1202 case TCPS_TIME_WAIT
:
1204 if (SEQ_LEQ(ti
->ti_ack
, tp
->snd_una
)) {
1205 if (ti
->ti_len
== 0 && tiwin
== tp
->snd_wnd
) {
1206 tcpstat
.tcps_rcvdupack
++;
1208 * If we have outstanding data (other than
1209 * a window probe), this is a completely
1210 * duplicate ack (ie, window info didn't
1211 * change), the ack is the biggest we've
1212 * seen and we've seen exactly our rexmt
1213 * threshhold of them, assume a packet
1214 * has been dropped and retransmit it.
1215 * Kludge snd_nxt & the congestion
1216 * window so we send only this one
1219 * We know we're losing at the current
1220 * window size so do congestion avoidance
1221 * (set ssthresh to half the current window
1222 * and pull our congestion window back to
1223 * the new ssthresh).
1225 * Dup acks mean that packets have left the
1226 * network (they're now cached at the receiver)
1227 * so bump cwnd by the amount in the receiver
1228 * to keep a constant cwnd packets in the
1231 if (tp
->t_timer
[TCPT_REXMT
] == 0 ||
1232 ti
->ti_ack
!= tp
->snd_una
)
1234 else if (++tp
->t_dupacks
== tcprexmtthresh
) {
1235 tcp_seq onxt
= tp
->snd_nxt
;
1237 MIN(tp
->snd_wnd
, tp
->snd_cwnd
) / 2 /
1242 tp
->snd_ssthresh
= win
* tp
->t_maxseg
;
1243 tp
->t_timer
[TCPT_REXMT
] = 0;
1245 tp
->snd_nxt
= ti
->ti_ack
;
1246 tp
->snd_cwnd
= tp
->t_maxseg
;
1247 (void) tcp_output(tp
);
1248 tp
->snd_cwnd
= tp
->snd_ssthresh
+
1249 tp
->t_maxseg
* tp
->t_dupacks
;
1250 if (SEQ_GT(onxt
, tp
->snd_nxt
))
1253 } else if (tp
->t_dupacks
> tcprexmtthresh
) {
1254 tp
->snd_cwnd
+= tp
->t_maxseg
;
1255 (void) tcp_output(tp
);
1263 * If the congestion window was inflated to account
1264 * for the other side's cached packets, retract it.
1266 if (tp
->t_dupacks
> tcprexmtthresh
&&
1267 tp
->snd_cwnd
> tp
->snd_ssthresh
)
1268 tp
->snd_cwnd
= tp
->snd_ssthresh
;
1270 if (SEQ_GT(ti
->ti_ack
, tp
->snd_max
)) {
1271 tcpstat
.tcps_rcvacktoomuch
++;
1275 * If we reach this point, ACK is not a duplicate,
1276 * i.e., it ACKs something we sent.
1278 if (tp
->t_flags
& TF_NEEDSYN
) {
1280 * T/TCP: Connection was half-synchronized, and our
1281 * SYN has been ACK'd (so connection is now fully
1282 * synchronized). Go to non-starred state and
1283 * increment snd_una for ACK of SYN.
1285 tp
->t_flags
&= ~TF_NEEDSYN
;
1290 acked
= ti
->ti_ack
- tp
->snd_una
;
1291 tcpstat
.tcps_rcvackpack
++;
1292 tcpstat
.tcps_rcvackbyte
+= acked
;
1295 * If we have a timestamp reply, update smoothed
1296 * round trip time. If no timestamp is present but
1297 * transmit timer is running and timed sequence
1298 * number was acked, update smoothed round trip time.
1299 * Since we now have an rtt measurement, cancel the
1300 * timer backoff (cf., Phil Karn's retransmit alg.).
1301 * Recompute the initial retransmit timer.
1303 if (to
.to_flag
& TOF_TS
)
1304 tcp_xmit_timer(tp
, tcp_now
- to
.to_tsecr
+ 1);
1305 else if (tp
->t_rtt
&& SEQ_GT(ti
->ti_ack
, tp
->t_rtseq
))
1306 tcp_xmit_timer(tp
,tp
->t_rtt
);
1309 * If all outstanding data is acked, stop retransmit
1310 * timer and remember to restart (more output or persist).
1311 * If there is more data to be acked, restart retransmit
1312 * timer, using current (possibly backed-off) value.
1314 if (ti
->ti_ack
== tp
->snd_max
) {
1315 tp
->t_timer
[TCPT_REXMT
] = 0;
1317 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
1318 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
1321 * If no data (only SYN) was ACK'd,
1322 * skip rest of ACK processing.
1328 * When new data is acked, open the congestion window.
1329 * If the window gives us less than ssthresh packets
1330 * in flight, open exponentially (maxseg per packet).
1331 * Otherwise open linearly: maxseg per window
1332 * (maxseg^2 / cwnd per packet).
1335 register u_int cw
= tp
->snd_cwnd
;
1336 register u_int incr
= tp
->t_maxseg
;
1338 if (cw
> tp
->snd_ssthresh
)
1339 incr
= incr
* incr
/ cw
;
1340 tp
->snd_cwnd
= MIN(cw
+ incr
, TCP_MAXWIN
<<tp
->snd_scale
);
1342 if (acked
> so
->so_snd
.sb_cc
) {
1343 tp
->snd_wnd
-= so
->so_snd
.sb_cc
;
1344 sbdrop(&so
->so_snd
, (int)so
->so_snd
.sb_cc
);
1347 sbdrop(&so
->so_snd
, acked
);
1348 tp
->snd_wnd
-= acked
;
1351 if (so
->so_snd
.sb_flags
& SB_NOTIFY
)
1353 tp
->snd_una
= ti
->ti_ack
;
1354 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
1355 tp
->snd_nxt
= tp
->snd_una
;
1357 switch (tp
->t_state
) {
1360 * In FIN_WAIT_1 STATE in addition to the processing
1361 * for the ESTABLISHED state if our FIN is now acknowledged
1362 * then enter FIN_WAIT_2.
1364 case TCPS_FIN_WAIT_1
:
1365 if (ourfinisacked
) {
1367 * If we can't receive any more
1368 * data, then closing user can proceed.
1369 * Starting the timer is contrary to the
1370 * specification, but if we don't get a FIN
1371 * we'll hang forever.
1373 if (so
->so_state
& SS_CANTRCVMORE
) {
1374 soisdisconnected(so
);
1375 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;
1377 tp
->t_state
= TCPS_FIN_WAIT_2
;
1382 * In CLOSING STATE in addition to the processing for
1383 * the ESTABLISHED state if the ACK acknowledges our FIN
1384 * then enter the TIME-WAIT state, otherwise ignore
1388 if (ourfinisacked
) {
1389 tp
->t_state
= TCPS_TIME_WAIT
;
1390 tcp_canceltimers(tp
);
1391 /* Shorten TIME_WAIT [RFC-1644, p.28] */
1392 if (tp
->cc_recv
!= 0 &&
1393 tp
->t_duration
< TCPTV_MSL
)
1394 tp
->t_timer
[TCPT_2MSL
] =
1395 tp
->t_rxtcur
* TCPTV_TWTRUNC
;
1397 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
1398 soisdisconnected(so
);
1403 * In LAST_ACK, we may still be waiting for data to drain
1404 * and/or to be acked, as well as for the ack of our FIN.
1405 * If our FIN is now acknowledged, delete the TCB,
1406 * enter the closed state and return.
1409 if (ourfinisacked
) {
1416 * In TIME_WAIT state the only thing that should arrive
1417 * is a retransmission of the remote FIN. Acknowledge
1418 * it and restart the finack timer.
1420 case TCPS_TIME_WAIT
:
1421 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
1428 * Update window information.
1429 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1431 if ((tiflags
& TH_ACK
) &&
1432 (SEQ_LT(tp
->snd_wl1
, ti
->ti_seq
) ||
1433 (tp
->snd_wl1
== ti
->ti_seq
&& (SEQ_LT(tp
->snd_wl2
, ti
->ti_ack
) ||
1434 (tp
->snd_wl2
== ti
->ti_ack
&& tiwin
> tp
->snd_wnd
))))) {
1435 /* keep track of pure window updates */
1436 if (ti
->ti_len
== 0 &&
1437 tp
->snd_wl2
== ti
->ti_ack
&& tiwin
> tp
->snd_wnd
)
1438 tcpstat
.tcps_rcvwinupd
++;
1439 tp
->snd_wnd
= tiwin
;
1440 tp
->snd_wl1
= ti
->ti_seq
;
1441 tp
->snd_wl2
= ti
->ti_ack
;
1442 if (tp
->snd_wnd
> tp
->max_sndwnd
)
1443 tp
->max_sndwnd
= tp
->snd_wnd
;
1448 * Process segments with URG.
1450 if ((tiflags
& TH_URG
) && ti
->ti_urp
&&
1451 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
1453 * This is a kludge, but if we receive and accept
1454 * random urgent pointers, we'll crash in
1455 * soreceive. It's hard to imagine someone
1456 * actually wanting to send this much urgent data.
1458 if (ti
->ti_urp
+ so
->so_rcv
.sb_cc
> sb_max
) {
1459 ti
->ti_urp
= 0; /* XXX */
1460 tiflags
&= ~TH_URG
; /* XXX */
1461 goto dodata
; /* XXX */
1464 * If this segment advances the known urgent pointer,
1465 * then mark the data stream. This should not happen
1466 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1467 * a FIN has been received from the remote side.
1468 * In these states we ignore the URG.
1470 * According to RFC961 (Assigned Protocols),
1471 * the urgent pointer points to the last octet
1472 * of urgent data. We continue, however,
1473 * to consider it to indicate the first octet
1474 * of data past the urgent section as the original
1475 * spec states (in one of two places).
1477 if (SEQ_GT(ti
->ti_seq
+ti
->ti_urp
, tp
->rcv_up
)) {
1478 tp
->rcv_up
= ti
->ti_seq
+ ti
->ti_urp
;
1479 so
->so_oobmark
= so
->so_rcv
.sb_cc
+
1480 (tp
->rcv_up
- tp
->rcv_nxt
) - 1;
1481 if (so
->so_oobmark
== 0)
1482 so
->so_state
|= SS_RCVATMARK
;
1484 tp
->t_oobflags
&= ~(TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
1487 * Remove out of band data so doesn't get presented to user.
1488 * This can happen independent of advancing the URG pointer,
1489 * but if two URG's are pending at once, some out-of-band
1490 * data may creep in... ick.
1492 if (ti
->ti_urp
<= (u_long
)ti
->ti_len
1494 && (so
->so_options
& SO_OOBINLINE
) == 0
1497 tcp_pulloutofband(so
, ti
, m
);
1500 * If no out of band data is expected,
1501 * pull receive urgent pointer along
1502 * with the receive window.
1504 if (SEQ_GT(tp
->rcv_nxt
, tp
->rcv_up
))
1505 tp
->rcv_up
= tp
->rcv_nxt
;
1509 * Process the segment text, merging it into the TCP sequencing queue,
1510 * and arranging for acknowledgment of receipt if necessary.
1511 * This process logically involves adjusting tp->rcv_wnd as data
1512 * is presented to the user (this happens in tcp_usrreq.c,
1513 * case PRU_RCVD). If a FIN has already been received on this
1514 * connection then we just ignore the text.
1516 if ((ti
->ti_len
|| (tiflags
&TH_FIN
)) &&
1517 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
1518 TCP_REASS(tp
, ti
, m
, so
, tiflags
);
1520 * Note the amount of data that peer has sent into
1521 * our window, in order to estimate the sender's
1524 len
= so
->so_rcv
.sb_hiwat
- (tp
->rcv_adv
- tp
->rcv_nxt
);
1531 * If FIN is received ACK the FIN and let the user know
1532 * that the connection is closing.
1534 if (tiflags
& TH_FIN
) {
1535 if (TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
1538 * If connection is half-synchronized
1539 * (ie SEND_SYN flag on) then delay ACK,
1540 * so it may be piggybacked when SYN is sent.
1541 * Otherwise, since we received a FIN then no
1542 * more input can be expected, send ACK now.
1544 if (tp
->t_flags
& TF_NEEDSYN
)
1545 tp
->t_flags
|= TF_DELACK
;
1547 tp
->t_flags
|= TF_ACKNOW
;
1550 switch (tp
->t_state
) {
1553 * In SYN_RECEIVED and ESTABLISHED STATES
1554 * enter the CLOSE_WAIT state.
1556 case TCPS_SYN_RECEIVED
:
1557 case TCPS_ESTABLISHED
:
1558 tp
->t_state
= TCPS_CLOSE_WAIT
;
1562 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1563 * enter the CLOSING state.
1565 case TCPS_FIN_WAIT_1
:
1566 tp
->t_state
= TCPS_CLOSING
;
1570 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1571 * starting the time-wait timer, turning off the other
1574 case TCPS_FIN_WAIT_2
:
1575 tp
->t_state
= TCPS_TIME_WAIT
;
1576 tcp_canceltimers(tp
);
1577 /* Shorten TIME_WAIT [RFC-1644, p.28] */
1578 if (tp
->cc_recv
!= 0 &&
1579 tp
->t_duration
< TCPTV_MSL
) {
1580 tp
->t_timer
[TCPT_2MSL
] =
1581 tp
->t_rxtcur
* TCPTV_TWTRUNC
;
1582 /* For transaction client, force ACK now. */
1583 tp
->t_flags
|= TF_ACKNOW
;
1586 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
1587 soisdisconnected(so
);
1591 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1593 case TCPS_TIME_WAIT
:
1594 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
1599 if (so
->so_options
& SO_DEBUG
)
1600 tcp_trace(TA_INPUT
, ostate
, tp
, &tcp_saveti
, 0);
1604 * Return any desired output.
1606 if (needoutput
|| (tp
->t_flags
& TF_ACKNOW
))
1607 (void) tcp_output(tp
);
1612 * Generate an ACK dropping incoming segment if it occupies
1613 * sequence space, where the ACK reflects our state.
1615 if (tiflags
& TH_RST
)
1618 if (so
->so_options
& SO_DEBUG
)
1619 tcp_trace(TA_DROP
, ostate
, tp
, &tcp_saveti
, 0);
1622 tp
->t_flags
|= TF_ACKNOW
;
1623 (void) tcp_output(tp
);
1628 * Generate a RST, dropping incoming segment.
1629 * Make ACK acceptable to originator of segment.
1630 * Don't bother to respond if destination was broadcast/multicast.
1632 if ((tiflags
& TH_RST
) || m
->m_flags
& (M_BCAST
|M_MCAST
) ||
1633 IN_MULTICAST(ntohl(ti
->ti_dst
.s_addr
)))
1636 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
1637 tcp_trace(TA_DROP
, ostate
, tp
, &tcp_saveti
, 0);
1639 if (tiflags
& TH_ACK
)
1640 tcp_respond(tp
, ti
, m
, (tcp_seq
)0, ti
->ti_ack
, TH_RST
);
1642 if (tiflags
& TH_SYN
)
1644 tcp_respond(tp
, ti
, m
, ti
->ti_seq
+ti
->ti_len
, (tcp_seq
)0,
1647 /* destroy temporarily created socket */
1654 * Drop space held by incoming segment and return.
1657 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
1658 tcp_trace(TA_DROP
, ostate
, tp
, &tcp_saveti
, 0);
1661 /* destroy temporarily created socket */
1665 #ifndef TUBA_INCLUDE
1669 tcp_dooptions(tp
, cp
, cnt
, ti
, to
)
1673 struct tcpiphdr
*ti
;
1679 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1681 if (opt
== TCPOPT_EOL
)
1683 if (opt
== TCPOPT_NOP
)
1696 if (optlen
!= TCPOLEN_MAXSEG
)
1698 if (!(ti
->ti_flags
& TH_SYN
))
1700 bcopy((char *) cp
+ 2, (char *) &mss
, sizeof(mss
));
1705 if (optlen
!= TCPOLEN_WINDOW
)
1707 if (!(ti
->ti_flags
& TH_SYN
))
1709 tp
->t_flags
|= TF_RCVD_SCALE
;
1710 tp
->requested_s_scale
= MIN(cp
[2], TCP_MAX_WINSHIFT
);
1713 case TCPOPT_TIMESTAMP
:
1714 if (optlen
!= TCPOLEN_TIMESTAMP
)
1716 to
->to_flag
|= TOF_TS
;
1717 bcopy((char *)cp
+ 2,
1718 (char *)&to
->to_tsval
, sizeof(to
->to_tsval
));
1719 NTOHL(to
->to_tsval
);
1720 bcopy((char *)cp
+ 6,
1721 (char *)&to
->to_tsecr
, sizeof(to
->to_tsecr
));
1722 NTOHL(to
->to_tsecr
);
1725 * A timestamp received in a SYN makes
1726 * it ok to send timestamp requests and replies.
1728 if (ti
->ti_flags
& TH_SYN
) {
1729 tp
->t_flags
|= TF_RCVD_TSTMP
;
1730 tp
->ts_recent
= to
->to_tsval
;
1731 tp
->ts_recent_age
= tcp_now
;
1735 if (optlen
!= TCPOLEN_CC
)
1737 to
->to_flag
|= TOF_CC
;
1738 bcopy((char *)cp
+ 2,
1739 (char *)&to
->to_cc
, sizeof(to
->to_cc
));
1742 * A CC or CC.new option received in a SYN makes
1743 * it ok to send CC in subsequent segments.
1745 if (ti
->ti_flags
& TH_SYN
)
1746 tp
->t_flags
|= TF_RCVD_CC
;
1749 if (optlen
!= TCPOLEN_CC
)
1751 if (!(ti
->ti_flags
& TH_SYN
))
1753 to
->to_flag
|= TOF_CCNEW
;
1754 bcopy((char *)cp
+ 2,
1755 (char *)&to
->to_cc
, sizeof(to
->to_cc
));
1758 * A CC or CC.new option received in a SYN makes
1759 * it ok to send CC in subsequent segments.
1761 tp
->t_flags
|= TF_RCVD_CC
;
1764 if (optlen
!= TCPOLEN_CC
)
1766 if (!(ti
->ti_flags
& TH_SYN
))
1768 to
->to_flag
|= TOF_CCECHO
;
1769 bcopy((char *)cp
+ 2,
1770 (char *)&to
->to_ccecho
, sizeof(to
->to_ccecho
));
1771 NTOHL(to
->to_ccecho
);
1775 if (ti
->ti_flags
& TH_SYN
)
1776 tcp_mss(tp
, mss
); /* sets t_maxseg */
1780 * Pull out of band byte out of a segment so
1781 * it doesn't appear in the user's data queue.
1782 * It is still reflected in the segment length for
1783 * sequencing purposes.
1786 tcp_pulloutofband(so
, ti
, m
)
1788 struct tcpiphdr
*ti
;
1789 register struct mbuf
*m
;
1791 int cnt
= ti
->ti_urp
- 1;
1794 if (m
->m_len
> cnt
) {
1795 char *cp
= mtod(m
, caddr_t
) + cnt
;
1796 struct tcpcb
*tp
= sototcpcb(so
);
1799 tp
->t_oobflags
|= TCPOOB_HAVEDATA
;
1800 bcopy(cp
+1, cp
, (unsigned)(m
->m_len
- cnt
- 1));
1809 panic("tcp_pulloutofband");
1813 * Collect new round-trip time estimate
1814 * and update averages and current timeout.
1817 tcp_xmit_timer(tp
, rtt
)
1818 register struct tcpcb
*tp
;
1821 register short delta
;
1823 tcpstat
.tcps_rttupdated
++;
1824 if (tp
->t_srtt
!= 0) {
1826 * srtt is stored as fixed point with 3 bits after the
1827 * binary point (i.e., scaled by 8). The following magic
1828 * is equivalent to the smoothing algorithm in rfc793 with
1829 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1830 * point). Adjust rtt to origin 0.
1832 delta
= rtt
- 1 - (tp
->t_srtt
>> TCP_RTT_SHIFT
);
1833 if ((tp
->t_srtt
+= delta
) <= 0)
1836 * We accumulate a smoothed rtt variance (actually, a
1837 * smoothed mean difference), then set the retransmit
1838 * timer to smoothed rtt + 4 times the smoothed variance.
1839 * rttvar is stored as fixed point with 2 bits after the
1840 * binary point (scaled by 4). The following is
1841 * equivalent to rfc793 smoothing with an alpha of .75
1842 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1843 * rfc793's wired-in beta.
1847 delta
-= (tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
);
1848 if ((tp
->t_rttvar
+= delta
) <= 0)
1852 * No rtt measurement yet - use the unsmoothed rtt.
1853 * Set the variance to half the rtt (so our first
1854 * retransmit happens at 3*rtt).
1856 tp
->t_srtt
= rtt
<< TCP_RTT_SHIFT
;
1857 tp
->t_rttvar
= rtt
<< (TCP_RTTVAR_SHIFT
- 1);
1863 * the retransmit should happen at rtt + 4 * rttvar.
1864 * Because of the way we do the smoothing, srtt and rttvar
1865 * will each average +1/2 tick of bias. When we compute
1866 * the retransmit timer, we want 1/2 tick of rounding and
1867 * 1 extra tick because of +-1/2 tick uncertainty in the
1868 * firing of the timer. The bias will give us exactly the
1869 * 1.5 tick we need. But, because the bias is
1870 * statistical, we have to test that we don't drop below
1871 * the minimum feasible timer (which is 2 ticks).
1873 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
1874 tp
->t_rttmin
, TCPTV_REXMTMAX
);
1877 * We received an ack for a packet that wasn't retransmitted;
1878 * it is probably safe to discard any error indications we've
1879 * received recently. This isn't quite right, but close enough
1880 * for now (a route might have failed after we sent a segment,
1881 * and the return path might not be symmetrical).
1883 tp
->t_softerror
= 0;
1887 * Determine a reasonable value for maxseg size.
1888 * If the route is known, check route for mtu.
1889 * If none, use an mss that can be handled on the outgoing
1890 * interface without forcing IP to fragment; if bigger than
1891 * an mbuf cluster (mbconf.mclbytes), round down to nearest multiple of mbconf.mclbytes
1892 * to utilize large mbufs. If no route is found, route has no mtu,
1893 * or the destination isn't local, use a default, hopefully conservative
1894 * size (usually 512 or the default IP max size, but no more than the mtu
1895 * of the interface), as we can't discover anything about intervening
1896 * gateways or networks. We also initialize the congestion/slow start
1897 * window to be a single segment if the destination isn't local.
1898 * While looking at the routing entry, we also initialize other path-dependent
1899 * parameters from pre-set or cached values in the routing entry.
1901 * Also take into account the space needed for options that we
1902 * send regularly. Make maxseg shorter by that amount to assure
1903 * that we can send maxseg amount of data even when the options
1904 * are present. Store the upper limit of the length of options plus
1907 * NOTE that this routine is only called when we process an incoming
1908 * segment, for outgoing segments only tcp_mssopt is called.
1910 * In case of T/TCP, we call this routine during implicit connection
1911 * setup as well (offer = -1), to initialize maxseg from the cached
1919 register struct rtentry
*rt
;
1921 register int rtt
, mss
;
1925 struct rmxp_tao
*taop
;
1926 int origoffer
= offer
;
1929 if ((rt
= tcp_rtlookup(inp
)) == NULL
) {
1930 tp
->t_maxopd
= tp
->t_maxseg
= tcp_mssdflt
;
1934 so
= inp
->inp_socket
;
1936 taop
= rmx_taop(rt
->rt_rmx
);
1938 * Offer == -1 means that we didn't receive SYN yet,
1939 * use cached value in that case;
1942 offer
= taop
->tao_mssopt
;
1944 * Offer == 0 means that there was no MSS on the SYN segment,
1945 * in this case we use tcp_mssdflt.
1948 offer
= tcp_mssdflt
;
1951 * Sanity check: make sure that maxopd will be large
1952 * enough to allow some data on segments even is the
1953 * all the option space is used (40bytes). Otherwise
1954 * funny things may happen in tcp_output.
1956 offer
= MAX(offer
, 64);
1957 taop
->tao_mssopt
= offer
;
1959 #ifdef RTV_MTU /* if route characteristics exist ... */
1961 * While we're here, check if there's an initial rtt
1962 * or rttvar. Convert from the route-table units
1963 * to scaled multiples of the slow timeout timer.
1965 if (tp
->t_srtt
== 0 && (rtt
= rt
->rt_rmx
.rmx_rtt
)) {
1967 * XXX the lock bit for RTT indicates that the value
1968 * is also a minimum value; this is subject to time.
1970 if (rt
->rt_rmx
.rmx_locks
& RTV_RTT
)
1971 tp
->t_rttmin
= rtt
/ (RTM_RTTUNIT
/ PR_SLOWHZ
);
1972 tp
->t_srtt
= rtt
/ (RTM_RTTUNIT
/ (PR_SLOWHZ
* TCP_RTT_SCALE
));
1973 if (rt
->rt_rmx
.rmx_rttvar
)
1974 tp
->t_rttvar
= rt
->rt_rmx
.rmx_rttvar
/
1975 (RTM_RTTUNIT
/ (PR_SLOWHZ
* TCP_RTTVAR_SCALE
));
1977 /* default variation is +- 1 rtt */
1979 tp
->t_srtt
* TCP_RTTVAR_SCALE
/ TCP_RTT_SCALE
;
1980 TCPT_RANGESET(tp
->t_rxtcur
,
1981 ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1,
1982 tp
->t_rttmin
, TCPTV_REXMTMAX
);
1985 * if there's an mtu associated with the route, use it
1987 if (rt
->rt_rmx
.rmx_mtu
)
1988 mss
= rt
->rt_rmx
.rmx_mtu
- sizeof(struct tcpiphdr
);
1990 #endif /* RTV_MTU */
1992 mss
= ifp
->if_mtu
- sizeof(struct tcpiphdr
);
1993 if (!in_localaddr(inp
->inp_faddr
))
1994 mss
= MIN(mss
, tcp_mssdflt
);
1996 mss
= MIN(mss
, offer
);
1998 * maxopd stores the maximum length of data AND options
1999 * in a segment; maxseg is the amount of data in a normal
2000 * segment. We need to store this value (maxopd) apart
2001 * from maxseg, because now every segment carries options
2002 * and thus we normally have somewhat less data in segments.
2007 * In case of T/TCP, origoffer==-1 indicates, that no segments
2008 * were received yet. In this case we just guess, otherwise
2009 * we do the same as before T/TCP.
2011 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
2013 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
))
2014 mss
-= TCPOLEN_TSTAMP_APPA
;
2015 if ((tp
->t_flags
& (TF_REQ_CC
|TF_NOOPT
)) == TF_REQ_CC
&&
2017 (tp
->t_flags
& TF_RCVD_CC
) == TF_RCVD_CC
))
2018 mss
-= TCPOLEN_CC_APPA
;
2020 if ((mbconf
.mclbytes
& (mbconf
.mclbytes
- 1)) == 0) {
2021 if (mss
> mbconf
.mclbytes
)
2022 mss
&= ~(mbconf
.mclbytes
-1);
2024 if (mss
> mbconf
.mclbytes
)
2026 * DO NOT REMOVE THIS! A side effect is rounding.
2027 * Read the function description for more info
2029 mss
= mss
/ mbconf
.mclbytes
* mbconf
.mclbytes
;
2032 * If there's a pipesize, change the socket buffer
2033 * to that size. Make the socket buffers an integral
2034 * number of mss units; if the mss is larger than
2035 * the socket buffer, decrease the mss.
2038 if ((bufsize
= rt
->rt_rmx
.rmx_sendpipe
) == 0)
2040 bufsize
= so
->so_snd
.sb_hiwat
;
2044 bufsize
= roundup(bufsize
, mss
);
2045 if (bufsize
> sb_max
)
2047 (void)sbreserve(&so
->so_snd
, bufsize
);
2052 if ((bufsize
= rt
->rt_rmx
.rmx_recvpipe
) == 0)
2054 bufsize
= so
->so_rcv
.sb_hiwat
;
2055 if (bufsize
> mss
) {
2056 bufsize
= roundup(bufsize
, mss
);
2057 if (bufsize
> sb_max
)
2059 (void)sbreserve(&so
->so_rcv
, bufsize
);
2062 * Don't force slow-start on local network.
2064 if (!in_localaddr(inp
->inp_faddr
))
2068 if (rt
->rt_rmx
.rmx_ssthresh
) {
2070 * There's some sort of gateway or interface
2071 * buffer limit on the path. Use this to set
2072 * the slow start threshhold, but set the
2073 * threshold to no less than 2*mss.
2075 tp
->snd_ssthresh
= MAX(2 * mss
, rt
->rt_rmx
.rmx_ssthresh
);
2081 * Determine the MSS option to send on an outgoing SYN.
2089 rt
= tcp_rtlookup(tp
->t_inpcb
);
2094 * if there's an mtu associated with the route, use it
2096 if (rt
->rt_rmx
.rmx_mtu
)
2097 return rt
->rt_rmx
.rmx_mtu
- sizeof(struct tcpiphdr
);
2099 return rt
->rt_ifp
->if_mtu
- sizeof(struct tcpiphdr
);
2101 #endif /* TUBA_INCLUDE */