2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
58 #include "opt_ipfw.h" /* for ipfw_fwd */
59 #include "opt_tcpdebug.h"
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/sysctl.h>
66 #include <sys/malloc.h>
68 #include <sys/proc.h> /* for proc0 declaration */
69 #include <sys/protosw.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/syslog.h>
74 #include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */
77 #include <net/route.h>
79 #include <netinet/in.h>
80 #include <netinet/in_systm.h>
81 #include <netinet/ip.h>
82 #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */
83 #include <netinet/in_var.h>
84 #include <netinet/ip_var.h>
85 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
86 #include <netinet/in_pcb.h>
88 #include <netinet/ip6.h>
89 #include <netinet/icmp6.h>
90 #include <netinet6/nd6.h>
91 #include <netinet6/ip6_var.h>
92 #include <netinet6/in6_pcb.h>
94 #include <netinet/tcp.h>
95 #include <netinet/tcp_fsm.h>
96 #include <netinet/tcp_seq.h>
97 #include <netinet/tcp_timer.h>
98 #include <netinet/tcp_var.h>
99 #include <netinet/tcpip.h>
101 #include <netinet/tcp_debug.h>
105 struct ip6_hdr _tcp_si6
;
108 struct ip tcp_saveip
;
110 struct tcphdr tcp_savetcp
;
111 #endif /* TCPDEBUG */
114 #include <netinet6/ipsec.h>
115 #include <netkey/key.h>
118 #include <sys/kdebug.h>
120 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 0)
121 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 2)
122 #define DBG_FNC_TCP_INPUT NETDBG_CODE(DBG_NETTCP, (3 << 8))
123 #define DBG_FNC_TCP_NEWCONN NETDBG_CODE(DBG_NETTCP, (7 << 8))
125 static int tcprexmtthresh
= 3;
129 struct tcpstat tcpstat
;
130 SYSCTL_STRUCT(_net_inet_tcp
, TCPCTL_STATS
, stats
,
131 CTLFLAG_RD
, &tcpstat
, tcpstat
, "");
134 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, log_in_vain
, CTLFLAG_RW
,
135 &log_in_vain
, 0, "");
137 int tcp_delack_enabled
= 1;
138 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, delayed_ack
, CTLFLAG_RW
,
139 &tcp_delack_enabled
, 0, "");
142 struct inpcbhead tcb
;
143 #define tcb6 tcb /* for KAME src sync over BSD*'s */
144 struct inpcbinfo tcbinfo
;
146 static void tcp_dooptions
__P((struct tcpcb
*,
147 u_char
*, int, struct tcphdr
*, struct tcpopt
*));
148 static void tcp_pulloutofband
__P((struct socket
*,
149 struct tcphdr
*, struct mbuf
*));
150 static void tcp_xmit_timer
__P((struct tcpcb
*, int));
153 * Neighbor Discovery, Neighbor Unreachability Detection
156 #define ND6_HINT(tp) { \
157 if ((tp) && (tp)->t_inpcb && (tp)->t_inpcb->in6p_route.ro_rt) \
158 nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL); \
162 extern u_long current_active_connections
;
163 extern u_long last_active_conn_count
;
165 extern u_long
*delack_bitmask
;
171 * Insert segment ti into reassembly queue of tcp with
172 * control block tp. Return TH_FIN if reassembly now includes
173 * a segment with FIN. The macro form does the common case inline
174 * (segment is the next to be received on an established connection,
175 * and the queue is empty), avoiding linkage into and removal
176 * from the queue and repetition of various conversions.
177 * Set DELACK for segments received in order, but ack immediately
178 * when segments are out of order (so fast retransmit can work).
181 #define _ONLY_IF_INET6_(x) x
183 #define _ONLY_IF_INET6_(x)
185 #define TCP_REASS(tp, th, tilen, m, so, flags, isipv6, needwakeup) { \
186 if ((th)->th_seq == (tp)->rcv_nxt && \
187 (tp)->segq.lh_first == NULL && \
188 (tp)->t_state == TCPS_ESTABLISHED) { \
189 if (tcp_delack_enabled) {\
190 if (last_active_conn_count > DELACK_BITMASK_THRESH) \
191 TCP_DELACK_BITSET(tp->t_inpcb->hash_element); \
192 tp->t_flags |= TF_DELACK; \
195 tp->t_flags |= TF_ACKNOW; \
196 (tp)->rcv_nxt += (tilen); \
197 flags = (th)->th_flags & TH_FIN; \
198 tcpstat.tcps_rcvpack++;\
199 tcpstat.tcps_rcvbyte += (tilen);\
200 _ONLY_IF_INET6_(ND6_HINT(tp);) \
201 sbappend(&(so)->so_rcv, (m)); \
204 (flags) = tcp_reass((tp), (th), (tilen), (m), (isipv6)); \
205 tp->t_flags |= TF_ACKNOW; \
211 * in the ip header part of the ipqe_tcp structure only the length is used.
214 tcp_reass(tp
, th
, tilen
, m
, isipv6
)
215 register struct tcpcb
*tp
;
216 register struct tcphdr
*th
;
223 register struct ipqent
*p
, *q
, *nq
, *tiqe
;
224 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
228 * Call with th==0 after become established to
229 * force pre-ESTABLISHED data up to user socket.
234 #if 0 /* Not using GETTCP(m) macro */
235 m
->m_pkthdr
.header
= ti
;
239 * Allocate a new queue entry, before we throw away any data.
240 * If we can't, just drop the packet. XXX
242 MALLOC(tiqe
, struct ipqent
*, sizeof (struct ipqent
), M_SONAME
, M_NOWAIT
);
244 tcpstat
.tcps_rcvmemdrop
++;
250 * Find a segment which begins after this one does.
252 for (p
= NULL
, q
= tp
->segq
.lh_first
; q
!= NULL
;
253 p
= q
, q
= q
->ipqe_q
.le_next
)
254 if (SEQ_GT(q
->ipqe_tcp
->ti_seq
, th
->th_seq
))
258 * If there is a preceding segment, it may provide some of
259 * our data already. If so, drop the data from the incoming
260 * segment. If it provides all of our data, drop us.
263 register struct tcpiphdr
*phdr
= p
->ipqe_tcp
;
266 /* conversion to int (in i) handles seq wraparound */
267 i
= phdr
->ti_seq
+ phdr
->ti_len
- th
->th_seq
;
270 tcpstat
.tcps_rcvduppack
++;
271 tcpstat
.tcps_rcvdupbyte
+= tilen
;
273 FREE(tiqe
, M_SONAME
);
275 #if 1 /* XXX: NetBSD just return 0 here */
277 * Try to present any queued data
278 * at the left window edge to the user.
279 * This is needed after the 3-WHS
282 goto present
; /* ??? */
290 tcpstat
.tcps_rcvoopack
++;
291 tcpstat
.tcps_rcvoobyte
+= tilen
;
294 * While we overlap succeeding segments trim them or,
295 * if they are completely covered, dequeue them.
298 register struct tcpiphdr
*qhdr
= q
->ipqe_tcp
;
299 register int i
= (th
->th_seq
+ tilen
) - qhdr
->ti_seq
;
303 if (i
< qhdr
->ti_len
) {
309 nq
= q
->ipqe_q
.le_next
;
311 LIST_REMOVE(q
, ipqe_q
);
316 /* Insert the new fragment queue entry into place. */
319 * There is a IP or IPv6 header in the mbuf before th
320 * so there is space for an ip header (for the length field)
323 ((struct tcpiphdr *)(((char *)(x)) - (sizeof (struct ip))))
325 tiqe
->ipqe_tcp
= thtoti(th
);
326 tiqe
->ipqe_tcp
->ti_len
= tilen
;
328 LIST_INSERT_HEAD(&tp
->segq
, tiqe
, ipqe_q
);
330 LIST_INSERT_AFTER(p
, tiqe
, ipqe_q
);
335 * Present data to user, advancing rcv_nxt through
336 * completed sequence space.
338 if (!TCPS_HAVEESTABLISHED(tp
->t_state
))
340 q
= tp
->segq
.lh_first
;
341 if (!q
|| q
->ipqe_tcp
->ti_seq
!= tp
->rcv_nxt
)
345 * XXX from INRIA for NetBSD, but should not happen because
346 * TCPS_HAVEESTABLISHED(tp->t_state) should be true here.
348 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& q
->ipqe_tcp
->ti_len
)
352 tp
->rcv_nxt
+= q
->ipqe_tcp
->ti_len
;
353 flags
= q
->ipqe_tcp
->ti_flags
& TH_FIN
;
354 nq
= q
->ipqe_q
.le_next
;
355 LIST_REMOVE(q
, ipqe_q
);
356 if (so
->so_state
& SS_CANTRCVMORE
)
359 sbappend(&so
->so_rcv
, q
->ipqe_m
);
362 } while (q
&& q
->ipqe_tcp
->ti_seq
== tp
->rcv_nxt
);
368 KERNEL_DEBUG(DBG_LAYER_END
, ((ti
->ti_dport
<< 16) | ti
->ti_sport
),
369 (((ti
->ti_src
.s_addr
& 0xffff) << 16) | (ti
->ti_dst
.s_addr
& 0xffff)),
370 ti
->ti_seq
, ti
->ti_ack
, ti
->ti_win
);
377 * TCP input routine, follows pages 65-76 of the
378 * protocol specification dated September, 1981 very closely.
382 tcp6_input(mp
, offp
, proto
)
386 tcp_input(*mp
, *offp
);
396 register struct tcphdr
*th
;
397 register struct ip
*ip
= NULL
;
398 register struct ipovly
*ipov
;
399 register struct inpcb
*inp
;
405 register struct tcpcb
*tp
= 0;
406 register int thflags
;
407 struct socket
*so
= 0;
408 int todrop
, acked
, ourfinisacked
, needoutput
= 0;
409 struct in_addr laddr
;
411 struct in6_addr laddr6
;
416 struct tcpopt to
; /* options in this segment */
417 struct rmxp_tao
*taop
; /* pointer to our TAO cache entry */
418 struct rmxp_tao tao_noncached
; /* in case there's no cached entry */
419 int need_sowwakeup
= 0;
420 int need_sorwakeup
= 0;
425 struct ip6_hdr
*ip6
= NULL
;
428 #define lgminh (sizeof (struct tcpiphdr))
430 int isipv6
= (mtod(m
, struct ip
*)->ip_v
== 6) ? 1 : 0;
432 struct proc
*proc0
=current_proc();
434 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_START
,0,0,0,0,0);
436 bzero((char *)&to
, sizeof(to
));
438 tcpstat
.tcps_rcvtotal
++;
440 * Get IP and TCP header together in first mbuf.
441 * Note: IP leaves IP header in first mbuf.
443 th
= mtod(m
, struct tcpiphdr
*);
445 KERNEL_DEBUG(DBG_LAYER_BEG
, ((ti
->ti_dport
<< 16) | ti
->ti_sport
),
446 (((ti
->ti_src
.s_addr
& 0xffff) << 16) | (ti
->ti_dst
.s_addr
& 0xffff)),
447 ti
->ti_seq
, ti
->ti_ack
, ti
->ti_win
);
451 ip6
= mtod(m
, struct ip6_hdr
*);
452 lgminh
= sizeof(struct tcpip6hdr
);
454 lgminh
= sizeof(struct tcpiphdr
);
456 ip
= mtod(m
, struct ip
*);
457 ipov
= (struct ipovly
*)ip
;
463 /* XXX not a good place to put this into... */
465 m
&& (m
->m_flags
& M_ANYCAST6
)) {
466 icmp6_error(m
, ICMP6_DST_UNREACH
, ICMP6_DST_UNREACH_ADDR
,
467 (caddr_t
)&ip6
->ip6_dst
- (caddr_t
)ip6
);
474 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct tcphdr
), );
475 ip6
= mtod(m
, struct ip6_hdr
*);
476 tilen
= ntohs(ip6
->ip6_plen
) - off
+ sizeof(*ip6
);
478 if (in6_cksum(m
, IPPROTO_TCP
, off
, tilen
)) {
479 tcpstat
.tcps_rcvbadsum
++;
482 th
= (struct tcphdr
*)((caddr_t
)ip6
+ off
);
487 * Get IP and TCP header together in first mbuf.
488 * Note: IP leaves IP header in first mbuf.
490 /* XXX: should we still require this for IPv4? */
491 if (off
> sizeof (struct ip
)) {
492 ip_stripoptions(m
, (struct mbuf
*)0);
493 off
= sizeof(struct ip
);
495 if (m
->m_len
< lgminh
) {
496 if ((m
= m_pullup(m
, lgminh
)) == 0) {
497 tcpstat
.tcps_rcvshort
++;
501 ip
= mtod(m
, struct ip
*);
502 ipov
= (struct ipovly
*)ip
;
505 * Checksum extended TCP header and data.
508 len
= sizeof (struct ip
) + tilen
;
509 bzero(ipov
->ih_x1
, sizeof(ipov
->ih_x1
));
510 ipov
->ih_len
= (u_short
)tilen
;
512 th
= (struct tcphdr
*)((caddr_t
)ip
+ off
);
513 th
->th_sum
= in_cksum(m
, len
);
515 tcpstat
.tcps_rcvbadsum
++;
521 * Check that TCP offset makes sense,
522 * pull out TCP options and adjust length. XXX
524 toff
= th
->th_off
<< 2;
525 if (toff
< sizeof (struct tcphdr
) || toff
> tilen
) {
526 tcpstat
.tcps_rcvbadoff
++;
530 if (toff
> sizeof (struct tcphdr
)) {
533 IP6_EXTHDR_CHECK(m
, off
, toff
, );
534 ip6
= mtod(m
, struct ip6_hdr
*);
535 th
= (struct tcphdr
*)((caddr_t
)ip6
+ off
);
539 if (m
->m_len
< sizeof(struct ip
) + toff
) {
540 if ((m
= m_pullup(m
, sizeof (struct ip
) + toff
)) == 0) {
541 tcpstat
.tcps_rcvshort
++;
544 ip
= mtod(m
, struct ip
*);
545 ipov
= (struct ipovly
*)ip
;
546 th
= (struct tcphdr
*)((caddr_t
)ip
+ off
);
549 optlen
= toff
- sizeof (struct tcphdr
);
550 optp
= (u_char
*)(th
+ 1);
552 * Do quick retrieval of timestamp options ("options
553 * prediction?"). If timestamp is the only option and it's
554 * formatted as recommended in RFC 1323 appendix A, we
555 * quickly get the values now and not bother calling
556 * tcp_dooptions(), etc.
558 if ((optlen
== TCPOLEN_TSTAMP_APPA
||
559 (optlen
> TCPOLEN_TSTAMP_APPA
&&
560 optp
[TCPOLEN_TSTAMP_APPA
] == TCPOPT_EOL
)) &&
561 *(u_int32_t
*)optp
== htonl(TCPOPT_TSTAMP_HDR
) &&
562 (th
->th_flags
& TH_SYN
) == 0) {
563 to
.to_flag
|= TOF_TS
;
564 to
.to_tsval
= ntohl(*(u_int32_t
*)(optp
+ 4));
565 to
.to_tsecr
= ntohl(*(u_int32_t
*)(optp
+ 8));
566 optp
= NULL
; /* we've parsed the options */
569 thflags
= th
->th_flags
;
572 * Convert TCP protocol specific fields to host format.
580 * Drop TCP, IP headers and TCP options.
582 hdroptlen
= off
+toff
;
583 m
->m_data
+= hdroptlen
;
584 m
->m_len
-= hdroptlen
;
587 * Locate pcb for segment.
590 #if IPFIREWALL_FORWARD
591 if (ip_fw_fwd_addr
!= NULL
597 * Diverted. Pretend to be the destination.
598 * already got one like this?
600 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
601 ip
->ip_dst
, th
->th_dport
, 0, m
->m_pkthdr
.rcvif
);
604 * No, then it's new. Try find the ambushing socket
606 if (!ip_fw_fwd_addr
->sin_port
) {
607 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
,
608 th
->th_sport
, ip_fw_fwd_addr
->sin_addr
,
609 th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
611 inp
= in_pcblookup_hash(&tcbinfo
,
612 ip
->ip_src
, th
->th_sport
,
613 ip_fw_fwd_addr
->sin_addr
,
614 ntohs(ip_fw_fwd_addr
->sin_port
), 1,
618 ip_fw_fwd_addr
= NULL
;
620 #endif /* IPFIREWALL_FORWARD */
624 inp
= in6_pcblookup_hash(&tcbinfo
, &ip6
->ip6_src
, th
->th_sport
,
625 &ip6
->ip6_dst
, th
->th_dport
, 1,
629 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
630 ip
->ip_dst
, th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
633 /* due to difference from other BSD stacks */
634 m
->m_data
-= hdroptlen
;
635 m
->m_len
+= hdroptlen
;
638 if (inp
!= NULL
&& ipsec6_in_reject_so(m
, inp
->inp_socket
)) {
639 ipsec6stat
.in_polvio
++;
644 if (inp
!= NULL
&& ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
645 ipsecstat
.in_polvio
++;
648 m
->m_data
+= hdroptlen
;
649 m
->m_len
-= hdroptlen
;
653 * If the state is CLOSED (i.e., TCB does not exist) then
654 * all data in the incoming segment is discarded.
655 * If the TCB exists but is in CLOSED state, it is embryonic,
656 * but should either do a listen or a connect soon.
659 if (log_in_vain
&& thflags
& TH_SYN
) {
661 char buf
[INET6_ADDRSTRLEN
];
663 char buf
[4*sizeof "123"];
668 strcpy(buf
, ip6_sprintf(&ip6
->ip6_dst
));
670 "Connection attempt to TCP %s:%d from %s:%d\n",
671 buf
, ntohs(th
->th_dport
),
672 ip6_sprintf(&ip6
->ip6_src
),
673 ntohs(th
->th_sport
));
676 strcpy(buf
, inet_ntoa(ip
->ip_dst
));
678 "Connection attempt to TCP %s:%d from %s:%d\n",
679 buf
, ntohs(th
->th_dport
), inet_ntoa(ip
->ip_src
),
680 ntohs(th
->th_sport
));
686 if (badport_bandlim(1) < 0)
694 if (tp
->t_state
== TCPS_CLOSED
)
697 /* Unscale the window into a 32-bit value. */
698 if ((thflags
& TH_SYN
) == 0)
699 tiwin
= th
->th_win
<< tp
->snd_scale
;
703 so
= inp
->inp_socket
;
704 if (so
->so_options
& (SO_DEBUG
|SO_ACCEPTCONN
)) {
706 if (so
->so_options
& SO_DEBUG
) {
707 ostate
= tp
->t_state
;
710 tcp_saveip
._tcp_si6
= *ip6
;
712 tcp_saveip
._tcp_si4
= *ip
;
720 if (so
->so_options
& SO_ACCEPTCONN
) {
721 register struct tcpcb
*tp0
= tp
;
727 struct inpcb
*oinp
= sotoinpcb(so
);
731 if ((thflags
& (TH_RST
|TH_ACK
|TH_SYN
)) != TH_SYN
) {
733 * Note: dropwithreset makes sure we don't
734 * send a RST in response to a RST.
736 if (thflags
& TH_ACK
) {
737 tcpstat
.tcps_badsyn
++;
743 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_START
,0,0,0,0,0);
744 so2
= sonewconn(so
, 0);
748 tcpstat
.tcps_listendrop
++;
749 so2
= sodropablereq(so
);
751 tcp_drop(sototcpcb(so2
), ETIMEDOUT
);
752 so2
= sonewconn(so
, 0);
762 * This is ugly, but ....
764 * Mark socket as temporary until we're
765 * committed to keeping it. The code at
766 * ``drop'' and ``dropwithreset'' check the
767 * flag dropsocket to see if the temporary
768 * socket created here should be discarded.
769 * We mark the socket as discardable until
770 * we're committed to it below in TCPS_LISTEN.
773 inp
= (struct inpcb
*)so
->so_pcb
;
776 inp
->in6p_laddr
= ip6
->ip6_dst
;
778 if (ip6_mapped_addr_on
) {
779 inp
->inp_vflag
&= ~INP_IPV6
;
780 inp
->inp_vflag
|= INP_IPV4
;
783 inp
->inp_laddr
= ip
->ip_dst
;
788 inp
->inp_lport
= th
->th_dport
;
789 if (in_pcbinshash(inp
) != 0) {
791 * Undo the assignments above if we failed to put
792 * the PCB on the hash lists.
796 inp
->in6p_laddr
= in6addr_any
;
799 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
805 * from IPsec perspective, it is important to do it
806 * after making actual listening socket.
807 * otherwise, cached security association will bark.
809 * Subject: (KAME-snap 748)
810 * From: Wayne Knowles <w.knowles@niwa.cri.nz>
812 if ((thflags
& (TH_RST
|TH_ACK
|TH_SYN
)) != TH_SYN
) {
814 * Note: dropwithreset makes sure we don't
815 * send a RST in response to a RST.
817 if (thflags
& TH_ACK
) {
818 tcpstat
.tcps_badsyn
++;
826 struct ip6_recvpktopts newopts
;
829 * Inherit socket options from the listening
831 * Note that in6p_inputopts are not (even
832 * should not be) copied, since it stores
833 * previously received options and is used to
834 * detect if each new option is different than
835 * the previous one and hence should be passed
837 * If we copied in6p_inputopts, a user would
838 * not be able to receive options just after
839 * calling the accept system call.
842 oinp
->inp_flags
& INP_CONTROLOPTS
;
843 if (oinp
->in6p_outputopts
)
844 inp
->in6p_outputopts
=
845 ip6_copypktopts(oinp
->in6p_outputopts
,
849 inp
->inp_options
= ip_srcroute();
851 /* copy old policy into new socket's */
852 if (ipsec_copy_policy(sotoinpcb(oso
)->inp_sp
,
854 printf("tcp_input: could not copy policy\n");
858 tp
->t_state
= TCPS_LISTEN
;
859 tp
->t_flags
|= tp0
->t_flags
& (TF_NOPUSH
|TF_NOOPT
);
861 /* Compute proper scaling value from buffer space */
862 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
863 TCP_MAXWIN
<< tp
->request_r_scale
< so
->so_rcv
.sb_hiwat
)
864 tp
->request_r_scale
++;
866 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_END
,0,0,0,0,0);
871 /* save packet options if user wanted */
872 if (isipv6
&& (inp
->in6p_flags
& INP_CONTROLOPTS
) != 0) {
873 struct ip6_recvpktopts opts6
;
876 * Temporarily re-adjusting the mbuf before ip6_savecontrol(),
877 * which is necessary for FreeBSD only due to difference from
879 * XXX: we'll soon make a more natural fix after getting a
883 m
->m_data
-= hdroptlen
;
884 m
->m_len
+= hdroptlen
;
886 ip6_savecontrol(inp
, ip6
, m
, &opts6
, &inp
->in6p_inputopts
);
887 if (inp
->in6p_inputopts
)
888 ip6_update_recvpcbopt(inp
->in6p_inputopts
, &opts6
);
890 if (sbappendcontrol(&inp
->in6p_socket
->so_rcv
,
896 m
->m_data
+= hdroptlen
; /* XXX */
897 m
->m_len
-= hdroptlen
; /* XXX */
903 * Segment received on connection.
904 * Reset idle time and keep-alive timer.
907 if (TCPS_HAVEESTABLISHED(tp
->t_state
))
908 tp
->t_timer
[TCPT_KEEP
] = tcp_keepidle
;
911 * Process options if not in LISTEN state,
912 * else do it below (after getting remote address).
914 if (tp
->t_state
!= TCPS_LISTEN
&& optp
)
915 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
916 if (th
->th_flags
& TH_SYN
)
917 tcp_mss(tp
, to
.to_maxseg
, isipv6
); /* sets t_maxseg */
920 * Header prediction: check for the two common cases
921 * of a uni-directional data xfer. If the packet has
922 * no control flags, is in-sequence, the window didn't
923 * change and we're not retransmitting, it's a
924 * candidate. If the length is zero and the ack moved
925 * forward, we're the sender side of the xfer. Just
926 * free the data acked & wake any higher level process
927 * that was blocked waiting for space. If the length
928 * is non-zero and the ack didn't move, we're the
929 * receiver side. If we're getting packets in-order
930 * (the reassembly queue is empty), add the data to
931 * the socket buffer and note that we need a delayed ack.
932 * Make sure that the hidden state-flags are also off.
933 * Since we check for TCPS_ESTABLISHED above, it can only
936 if (tp
->t_state
== TCPS_ESTABLISHED
&&
937 (thflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
)) == TH_ACK
&&
938 ((tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
)) == 0) &&
939 ((to
.to_flag
& TOF_TS
) == 0 ||
940 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
942 * Using the CC option is compulsory if once started:
943 * the segment is OK if no T/TCP was negotiated or
944 * if the segment has a CC option equal to CCrecv
946 ((tp
->t_flags
& (TF_REQ_CC
|TF_RCVD_CC
)) != (TF_REQ_CC
|TF_RCVD_CC
) ||
947 ((to
.to_flag
& TOF_CC
) != 0 && to
.to_cc
== tp
->cc_recv
)) &&
948 th
->th_seq
== tp
->rcv_nxt
&&
949 tiwin
&& tiwin
== tp
->snd_wnd
&&
950 tp
->snd_nxt
== tp
->snd_max
) {
953 * If last ACK falls within this segment's sequence numbers,
954 * record the timestamp.
955 * NOTE that the test is modified according to the latest
956 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
958 if ((to
.to_flag
& TOF_TS
) != 0 &&
959 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
960 tp
->ts_recent_age
= tcp_now
;
961 tp
->ts_recent
= to
.to_tsval
;
965 if (SEQ_GT(th
->th_ack
, tp
->snd_una
) &&
966 SEQ_LEQ(th
->th_ack
, tp
->snd_max
) &&
967 tp
->snd_cwnd
>= tp
->snd_wnd
&&
968 tp
->t_dupacks
< tcprexmtthresh
) {
970 * this is a pure ack for outstanding data.
972 ++tcpstat
.tcps_predack
;
973 if ((to
.to_flag
& TOF_TS
) != 0)
975 tcp_now
- to
.to_tsecr
+ 1);
976 else if (tp
->t_rtt
&&
977 SEQ_GT(th
->th_ack
, tp
->t_rtseq
))
978 tcp_xmit_timer(tp
, tp
->t_rtt
);
979 acked
= th
->th_ack
- tp
->snd_una
;
980 tcpstat
.tcps_rcvackpack
++;
981 tcpstat
.tcps_rcvackbyte
+= acked
;
982 sbdrop(&so
->so_snd
, acked
);
983 tp
->snd_una
= th
->th_ack
;
986 /* some progress has been done */
992 * If all outstanding data are acked, stop
993 * retransmit timer, otherwise restart timer
994 * using current (possibly backed-off) value.
995 * If process is waiting for space,
996 * wakeup/selwakeup/signal. If data
997 * are ready to send, let tcp_output
998 * decide between more output or persist.
1000 if (tp
->snd_una
== tp
->snd_max
)
1001 tp
->t_timer
[TCPT_REXMT
] = 0;
1002 else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
1003 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
1005 if (so
->so_snd
.sb_cc
)
1006 (void) tcp_output(tp
);
1008 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
1011 } else if (th
->th_ack
== tp
->snd_una
&&
1012 tp
->segq
.lh_first
== NULL
&&
1013 tilen
<= sbspace(&so
->so_rcv
)) {
1015 * this is a pure, in-sequence data packet
1016 * with nothing on the reassembly queue and
1017 * we have enough buffer space to take it.
1019 ++tcpstat
.tcps_preddat
;
1020 tp
->rcv_nxt
+= tilen
;
1021 tcpstat
.tcps_rcvpack
++;
1022 tcpstat
.tcps_rcvbyte
+= tilen
;
1024 /* some progress has been done */
1028 sbappend(&so
->so_rcv
, m
);
1029 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
1030 (((th
->th_src
.s_addr
& 0xffff) << 16) | (th
->th_dst
.s_addr
& 0xffff)),
1031 th
->th_seq
, th
->th_ack
, th
->th_win
);
1032 if (tcp_delack_enabled
) {
1033 if (last_active_conn_count
> DELACK_BITMASK_THRESH
)
1034 TCP_DELACK_BITSET(tp
->t_inpcb
->hash_element
);
1035 tp
->t_flags
|= TF_DELACK
;
1037 tp
->t_flags
|= TF_ACKNOW
;
1041 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
1047 * Calculate amount of space in receive window,
1048 * and then do TCP input processing.
1049 * Receive window is amount of space in rcv queue,
1050 * but not less than advertised window.
1054 win
= sbspace(&so
->so_rcv
);
1057 tp
->rcv_wnd
= imax(win
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
1060 switch (tp
->t_state
) {
1063 * If the state is LISTEN then ignore segment if it contains an RST.
1064 * If the segment contains an ACK then it is bad and send a RST.
1065 * If it does not contain a SYN then it is not interesting; drop it.
1066 * If it is from this socket, drop it, it must be forged.
1067 * Don't bother responding if the destination was a broadcast.
1068 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
1069 * tp->iss, and send a segment:
1070 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1071 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1072 * Fill in remote peer address fields if not previously specified.
1073 * Enter SYN_RECEIVED state, and process any other fields of this
1074 * segment in this state.
1077 register struct sockaddr_in
*sin
;
1079 register struct sockaddr_in6
*sin6
;
1082 if (thflags
& TH_RST
)
1084 if (thflags
& TH_ACK
)
1086 if ((thflags
& TH_SYN
) == 0)
1088 if (th
->th_dport
== th
->th_sport
) {
1091 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
1096 if (ip
->ip_dst
.s_addr
== ip
->ip_src
.s_addr
)
1102 if (m
->m_flags
& (M_BCAST
|M_MCAST
) ||
1103 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
))
1107 * Perhaps this should be a call/macro
1108 * to a function like in6_pcbconnect(), but almost
1109 * all of the checks have been done: we know
1110 * that the association is unique, and the
1111 * local address is always set here.
1113 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1114 inp
->in6p_laddr
= ip6
->ip6_dst
;
1115 inp
->in6p_faddr
= ip6
->ip6_src
;
1116 inp
->inp_fport
= th
->th_sport
;
1118 /* TODO: flowinfo initialization */
1122 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
,
1123 M_SONAME
, M_NOWAIT
);
1126 bzero(sin6
, sizeof(*sin6
));
1127 sin6
->sin6_family
= AF_INET6
;
1128 sin6
->sin6_len
= sizeof(*sin6
);
1129 sin6
->sin6_addr
= ip6
->ip6_src
;
1130 sin6
->sin6_port
= th
->th_sport
;
1131 laddr6
= inp
->in6p_laddr
;
1132 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1133 inp
->in6p_laddr
= ip6
->ip6_dst
;
1134 if (in6_pcbconnect(inp
, (struct sockaddr
*)sin6
,
1136 inp
->in6p_laddr
= laddr6
;
1137 FREE(sin6
, M_SONAME
);
1140 FREE(sin6
, M_SONAME
);
1146 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
1147 * in_broadcast() should never return true on a received
1148 * packet with M_BCAST not set.
1150 if (m
->m_flags
& (M_BCAST
|M_MCAST
) ||
1151 IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)))
1153 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
,
1157 sin
->sin_family
= AF_INET
;
1158 sin
->sin_len
= sizeof(*sin
);
1159 sin
->sin_addr
= ip
->ip_src
;
1160 sin
->sin_port
= th
->th_sport
;
1161 bzero((caddr_t
)sin
->sin_zero
, sizeof(sin
->sin_zero
));
1162 laddr
= inp
->inp_laddr
;
1163 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
1164 inp
->inp_laddr
= ip
->ip_dst
;
1165 if (in_pcbconnect(inp
, (struct sockaddr
*)sin
, &proc0
)) {
1166 inp
->inp_laddr
= laddr
;
1167 FREE(sin
, M_SONAME
);
1170 FREE(sin
, M_SONAME
);
1175 tp
->t_template
= tcp_template(tp
);
1176 if (tp
->t_template
== 0) {
1177 tp
= tcp_drop(tp
, ENOBUFS
);
1178 dropsocket
= 0; /* socket is already gone */
1181 if ((taop
= tcp_gettaocache(inp
)) == NULL
) {
1182 taop
= &tao_noncached
;
1183 bzero(taop
, sizeof(*taop
));
1185 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
1186 if (th
->th_flags
& TH_SYN
)
1187 tcp_mss(tp
, to
.to_maxseg
, isipv6
); /* sets t_maxseg */
1192 tcp_iss
+= TCP_ISSINCR
/4;
1193 tp
->irs
= th
->th_seq
;
1194 tcp_sendseqinit(tp
);
1197 * Initialization of the tcpcb for transaction;
1198 * set SND.WND = SEG.WND,
1199 * initialize CCsend and CCrecv.
1201 tp
->snd_wnd
= tiwin
; /* initial send-window */
1202 tp
->cc_send
= CC_INC(tcp_ccgen
);
1203 tp
->cc_recv
= to
.to_cc
;
1205 * Perform TAO test on incoming CC (SEG.CC) option, if any.
1206 * - compare SEG.CC against cached CC from the same host,
1208 * - if SEG.CC > chached value, SYN must be new and is accepted
1209 * immediately: save new CC in the cache, mark the socket
1210 * connected, enter ESTABLISHED state, turn on flag to
1211 * send a SYN in the next segment.
1212 * A virtual advertised window is set in rcv_adv to
1213 * initialize SWS prevention. Then enter normal segment
1214 * processing: drop SYN, process data and FIN.
1215 * - otherwise do a normal 3-way handshake.
1217 if ((to
.to_flag
& TOF_CC
) != 0) {
1218 if (((tp
->t_flags
& TF_NOPUSH
) != 0) &&
1219 taop
->tao_cc
!= 0 && CC_GT(to
.to_cc
, taop
->tao_cc
)) {
1221 taop
->tao_cc
= to
.to_cc
;
1222 if (tp
->t_state
!= TCPS_ESTABLISHED
)
1223 current_active_connections
++;
1225 tp
->t_state
= TCPS_ESTABLISHED
;
1228 * If there is a FIN, or if there is data and the
1229 * connection is local, then delay SYN,ACK(SYN) in
1230 * the hope of piggy-backing it on a response
1231 * segment. Otherwise must send ACK now in case
1232 * the other side is slow starting.
1234 if (tcp_delack_enabled
&&
1235 ((thflags
& TH_FIN
) ||
1238 (isipv6
&& in6_localaddr(&inp
->in6p_faddr
))
1242 in_localaddr(inp
->inp_faddr
)
1247 if (last_active_conn_count
> DELACK_BITMASK_THRESH
)
1248 TCP_DELACK_BITSET(tp
->t_inpcb
->hash_element
);
1250 tp
->t_flags
|= (TF_DELACK
| TF_NEEDSYN
);
1253 tp
->t_flags
|= (TF_ACKNOW
| TF_NEEDSYN
);
1256 * Limit the `virtual advertised window' to TCP_MAXWIN
1257 * here. Even if we requested window scaling, it will
1258 * become effective only later when our SYN is acked.
1260 tp
->rcv_adv
+= min(tp
->rcv_wnd
, TCP_MAXWIN
);
1261 tcpstat
.tcps_connects
++;
1263 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
1264 dropsocket
= 0; /* committed to socket */
1265 tcpstat
.tcps_accepts
++;
1268 /* else do standard 3-way handshake */
1271 * No CC option, but maybe CC.NEW:
1272 * invalidate cached value.
1277 * TAO test failed or there was no CC option,
1278 * do a standard 3-way handshake.
1280 tp
->t_flags
|= TF_ACKNOW
;
1281 tp
->t_state
= TCPS_SYN_RECEIVED
;
1282 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
1283 dropsocket
= 0; /* committed to socket */
1284 tcpstat
.tcps_accepts
++;
1289 * If the state is SYN_RECEIVED:
1290 * if seg contains an ACK, but not for our SYN/ACK, send a RST.
1292 case TCPS_SYN_RECEIVED
:
1293 if ((thflags
& TH_ACK
) &&
1294 (SEQ_LEQ(th
->th_ack
, tp
->snd_una
) ||
1295 SEQ_GT(th
->th_ack
, tp
->snd_max
)))
1300 * If the state is SYN_SENT:
1301 * if seg contains an ACK, but not for our SYN, drop the input.
1302 * if seg contains a RST, then drop the connection.
1303 * if seg does not contain SYN, then drop it.
1304 * Otherwise this is an acceptable SYN segment
1305 * initialize tp->rcv_nxt and tp->irs
1306 * if seg contains ack then advance tp->snd_una
1307 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1308 * arrange for segment to be acked (eventually)
1309 * continue processing rest of data/controls, beginning with URG
1312 if ((taop
= tcp_gettaocache(inp
)) == NULL
) {
1313 taop
= &tao_noncached
;
1314 bzero(taop
, sizeof(*taop
));
1317 if ((thflags
& TH_ACK
) &&
1318 (SEQ_LEQ(th
->th_ack
, tp
->iss
) ||
1319 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
1321 * If we have a cached CCsent for the remote host,
1322 * hence we haven't just crashed and restarted,
1323 * do not send a RST. This may be a retransmission
1324 * from the other side after our earlier ACK was lost.
1325 * Our new SYN, when it arrives, will serve as the
1328 if (taop
->tao_ccsent
!= 0)
1333 if (thflags
& TH_RST
) {
1334 if (thflags
& TH_ACK
) {
1335 tp
= tcp_drop(tp
, ECONNREFUSED
);
1336 postevent(so
, 0, EV_RESET
);
1340 if ((thflags
& TH_SYN
) == 0)
1342 tp
->snd_wnd
= th
->th_win
; /* initial send window */
1343 tp
->cc_recv
= to
.to_cc
; /* foreign CC */
1345 tp
->irs
= th
->th_seq
;
1347 if (thflags
& TH_ACK
) {
1349 * Our SYN was acked. If segment contains CC.ECHO
1350 * option, check it to make sure this segment really
1351 * matches our SYN. If not, just drop it as old
1352 * duplicate, but send an RST if we're still playing
1353 * by the old rules. If no CC.ECHO option, make sure
1354 * we don't get fooled into using T/TCP.
1356 if (to
.to_flag
& TOF_CCECHO
) {
1357 if (tp
->cc_send
!= to
.to_ccecho
)
1358 if (taop
->tao_ccsent
!= 0)
1363 tp
->t_flags
&= ~TF_RCVD_CC
;
1364 tcpstat
.tcps_connects
++;
1366 /* Do window scaling on this connection? */
1367 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
1368 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
1369 tp
->snd_scale
= tp
->requested_s_scale
;
1370 tp
->rcv_scale
= tp
->request_r_scale
;
1372 /* Segment is acceptable, update cache if undefined. */
1373 if (taop
->tao_ccsent
== 0)
1374 taop
->tao_ccsent
= to
.to_ccecho
;
1376 tp
->rcv_adv
+= tp
->rcv_wnd
;
1377 tp
->snd_una
++; /* SYN is acked */
1379 * If there's data, delay ACK; if there's also a FIN
1380 * ACKNOW will be turned on later.
1382 if (tcp_delack_enabled
&& tilen
!= 0) {
1383 if (last_active_conn_count
> DELACK_BITMASK_THRESH
)
1384 TCP_DELACK_BITSET(tp
->t_inpcb
->hash_element
);
1385 tp
->t_flags
|= TF_DELACK
;
1388 tp
->t_flags
|= TF_ACKNOW
;
1390 * Received <SYN,ACK> in SYN_SENT[*] state.
1392 * SYN_SENT --> ESTABLISHED
1393 * SYN_SENT* --> FIN_WAIT_1
1395 if (tp
->t_flags
& TF_NEEDFIN
) {
1396 tp
->t_state
= TCPS_FIN_WAIT_1
;
1397 tp
->t_flags
&= ~TF_NEEDFIN
;
1400 if (tp
->t_state
!= TCPS_ESTABLISHED
)
1401 current_active_connections
++;
1402 tp
->t_state
= TCPS_ESTABLISHED
;
1403 tp
->t_timer
[TCPT_KEEP
] = tcp_keepidle
;
1407 * Received initial SYN in SYN-SENT[*] state => simul-
1408 * taneous open. If segment contains CC option and there is
1409 * a cached CC, apply TAO test; if it succeeds, connection is
1410 * half-synchronized. Otherwise, do 3-way handshake:
1411 * SYN-SENT -> SYN-RECEIVED
1412 * SYN-SENT* -> SYN-RECEIVED*
1413 * If there was no CC option, clear cached CC value.
1415 tp
->t_flags
|= TF_ACKNOW
;
1416 tp
->t_timer
[TCPT_REXMT
] = 0;
1417 if (to
.to_flag
& TOF_CC
) {
1418 if (taop
->tao_cc
!= 0 &&
1419 CC_GT(to
.to_cc
, taop
->tao_cc
)) {
1421 * update cache and make transition:
1422 * SYN-SENT -> ESTABLISHED*
1423 * SYN-SENT* -> FIN-WAIT-1*
1425 taop
->tao_cc
= to
.to_cc
;
1426 if (tp
->t_flags
& TF_NEEDFIN
) {
1427 tp
->t_state
= TCPS_FIN_WAIT_1
;
1428 tp
->t_flags
&= ~TF_NEEDFIN
;
1430 if (tp
->t_state
!= TCPS_ESTABLISHED
)
1431 current_active_connections
++;
1432 tp
->t_state
= TCPS_ESTABLISHED
;
1433 tp
->t_timer
[TCPT_KEEP
] = tcp_keepidle
;
1435 tp
->t_flags
|= TF_NEEDSYN
;
1437 tp
->t_state
= TCPS_SYN_RECEIVED
;
1439 /* CC.NEW or no option => invalidate cache */
1441 tp
->t_state
= TCPS_SYN_RECEIVED
;
1447 * Advance th->th_seq to correspond to first data byte.
1448 * If data, trim to stay within window,
1449 * dropping FIN if necessary.
1452 if (tilen
> tp
->rcv_wnd
) {
1453 todrop
= tilen
- tp
->rcv_wnd
;
1455 tilen
= tp
->rcv_wnd
;
1457 tcpstat
.tcps_rcvpackafterwin
++;
1458 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
1460 tp
->snd_wl1
= th
->th_seq
- 1;
1461 tp
->rcv_up
= th
->th_seq
;
1463 * Client side of transaction: already sent SYN and data.
1464 * If the remote host used T/TCP to validate the SYN,
1465 * our data will be ACK'd; if so, enter normal data segment
1466 * processing in the middle of step 5, ack processing.
1467 * Otherwise, goto step 6.
1469 if (thflags
& TH_ACK
)
1473 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1474 * if segment contains a SYN and CC [not CC.NEW] option:
1475 * if state == TIME_WAIT and connection duration > MSL,
1476 * drop packet and send RST;
1478 * if SEG.CC > CCrecv then is new SYN, and can implicitly
1479 * ack the FIN (and data) in retransmission queue.
1480 * Complete close and delete TCPCB. Then reprocess
1481 * segment, hoping to find new TCPCB in LISTEN state;
1483 * else must be old SYN; drop it.
1484 * else do normal processing.
1488 case TCPS_TIME_WAIT
:
1489 if ((thflags
& TH_SYN
) &&
1490 (to
.to_flag
& TOF_CC
) && tp
->cc_recv
!= 0) {
1491 if (tp
->t_state
== TCPS_TIME_WAIT
&&
1492 tp
->t_duration
> TCPTV_MSL
)
1494 if (CC_GT(to
.to_cc
, tp
->cc_recv
)) {
1501 break; /* continue normal processing */
1505 * States other than LISTEN or SYN_SENT.
1506 * First check the RST flag and sequence number since reset segments
1507 * are exempt from the timestamp and connection count tests. This
1508 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
1509 * below which allowed reset segments in half the sequence space
1510 * to fall though and be processed (which gives forged reset
1511 * segments with a random sequence number a 50 percent chance of
1512 * killing a connection).
1513 * Then check timestamp, if present.
1514 * Then check the connection count, if present.
1515 * Then check that at least some bytes of segment are within
1516 * receive window. If segment begins before rcv_nxt,
1517 * drop leading data (and SYN); if nothing left, just ack.
1520 * If the RST bit is set, check the sequence number to see
1521 * if this is a valid reset segment.
1523 * In all states except SYN-SENT, all reset (RST) segments
1524 * are validated by checking their SEQ-fields. A reset is
1525 * valid if its sequence number is in the window.
1526 * Note: this does not take into account delayed ACKs, so
1527 * we should test against last_ack_sent instead of rcv_nxt.
1528 * Also, it does not make sense to allow reset segments with
1529 * sequence numbers greater than last_ack_sent to be processed
1530 * since these sequence numbers are just the acknowledgement
1531 * numbers in our outgoing packets being echoed back at us,
1532 * and these acknowledgement numbers are monotonically
1534 * If we have multiple segments in flight, the intial reset
1535 * segment sequence numbers will be to the left of last_ack_sent,
1536 * but they will eventually catch up.
1537 * In any case, it never made sense to trim reset segments to
1538 * fit the receive window since RFC 1122 says:
1539 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
1541 * A TCP SHOULD allow a received RST segment to include data.
1544 * It has been suggested that a RST segment could contain
1545 * ASCII text that encoded and explained the cause of the
1546 * RST. No standard has yet been established for such
1549 * If the reset segment passes the sequence number test examine
1551 * SYN_RECEIVED STATE:
1552 * If passive open, return to LISTEN state.
1553 * If active open, inform user that connection was refused.
1554 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1555 * Inform user that connection was reset, and close tcb.
1556 * CLOSING, LAST_ACK, TIME_WAIT STATES
1559 * Drop the segment - see Stevens, vol. 2, p. 964 and
1562 if (thflags
& TH_RST
) {
1563 if (tp
->last_ack_sent
== th
->th_seq
) {
1564 switch (tp
->t_state
) {
1566 case TCPS_SYN_RECEIVED
:
1567 so
->so_error
= ECONNREFUSED
;
1570 case TCPS_ESTABLISHED
:
1571 case TCPS_FIN_WAIT_1
:
1572 case TCPS_CLOSE_WAIT
:
1573 current_active_connections
--;
1577 case TCPS_FIN_WAIT_2
:
1578 so
->so_error
= ECONNRESET
;
1580 postevent(so
, 0, EV_RESET
);
1581 tp
->t_state
= TCPS_CLOSED
;
1582 tcpstat
.tcps_drops
++;
1588 current_active_connections
--;
1592 case TCPS_TIME_WAIT
:
1600 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1601 * and it's less than ts_recent, drop it.
1603 if ((to
.to_flag
& TOF_TS
) != 0 && tp
->ts_recent
&&
1604 TSTMP_LT(to
.to_tsval
, tp
->ts_recent
)) {
1606 /* Check to see if ts_recent is over 24 days old. */
1607 if ((int)(tcp_now
- tp
->ts_recent_age
) > TCP_PAWS_IDLE
) {
1609 * Invalidate ts_recent. If this segment updates
1610 * ts_recent, the age will be reset later and ts_recent
1611 * will get a valid value. If it does not, setting
1612 * ts_recent to zero will at least satisfy the
1613 * requirement that zero be placed in the timestamp
1614 * echo reply when ts_recent isn't valid. The
1615 * age isn't reset until we get a valid ts_recent
1616 * because we don't want out-of-order segments to be
1617 * dropped when ts_recent is old.
1621 tcpstat
.tcps_rcvduppack
++;
1622 tcpstat
.tcps_rcvdupbyte
+= tilen
;
1623 tcpstat
.tcps_pawsdrop
++;
1630 * If T/TCP was negotiated and the segment doesn't have CC,
1631 * or if its CC is wrong then drop the segment.
1632 * RST segments do not have to comply with this.
1634 if ((tp
->t_flags
& (TF_REQ_CC
|TF_RCVD_CC
)) == (TF_REQ_CC
|TF_RCVD_CC
) &&
1635 ((to
.to_flag
& TOF_CC
) == 0 || tp
->cc_recv
!= to
.to_cc
))
1639 * In the SYN-RECEIVED state, validate that the packet belongs to
1640 * this connection before trimming the data to fit the receive
1641 * window. Check the sequence number versus IRS since we know
1642 * the sequence numbers haven't wrapped. This is a partial fix
1643 * for the "LAND" DoS attack.
1645 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& SEQ_LT(th
->th_seq
, tp
->irs
))
1648 todrop
= tp
->rcv_nxt
- th
->th_seq
;
1650 if (thflags
& TH_SYN
) {
1660 * Following if statement from Stevens, vol. 2, p. 960.
1663 || (todrop
== tilen
&& (thflags
& TH_FIN
) == 0)) {
1665 * Any valid FIN must be to the left of the window.
1666 * At this point the FIN must be a duplicate or out
1667 * of sequence; drop it.
1672 * Send an ACK to resynchronize and drop any data.
1673 * But keep on processing for RST or ACK.
1675 tp
->t_flags
|= TF_ACKNOW
;
1677 tcpstat
.tcps_rcvduppack
++;
1678 tcpstat
.tcps_rcvdupbyte
+= todrop
;
1680 tcpstat
.tcps_rcvpartduppack
++;
1681 tcpstat
.tcps_rcvpartdupbyte
+= todrop
;
1684 th
->th_seq
+= todrop
;
1686 if (th
->th_urp
> todrop
)
1687 th
->th_urp
-= todrop
;
1695 * If new data are received on a connection after the
1696 * user processes are gone, then RST the other end.
1698 if ((so
->so_state
& SS_NOFDREF
) &&
1699 tp
->t_state
> TCPS_CLOSE_WAIT
&& tilen
) {
1701 tcpstat
.tcps_rcvafterclose
++;
1706 * If segment ends after window, drop trailing data
1707 * (and PUSH and FIN); if nothing left, just ACK.
1709 todrop
= (th
->th_seq
+tilen
) - (tp
->rcv_nxt
+tp
->rcv_wnd
);
1711 tcpstat
.tcps_rcvpackafterwin
++;
1712 if (todrop
>= tilen
) {
1713 tcpstat
.tcps_rcvbyteafterwin
+= tilen
;
1715 * If a new connection request is received
1716 * while in TIME_WAIT, drop the old connection
1717 * and start over if the sequence numbers
1718 * are above the previous ones.
1720 if (thflags
& TH_SYN
&&
1721 tp
->t_state
== TCPS_TIME_WAIT
&&
1722 SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
1723 iss
= tp
->rcv_nxt
+ TCP_ISSINCR
;
1728 * If window is closed can only take segments at
1729 * window edge, and have to drop data and PUSH from
1730 * incoming segments. Continue processing, but
1731 * remember to ack. Otherwise, drop segment
1734 if (tp
->rcv_wnd
== 0 && th
->th_seq
== tp
->rcv_nxt
) {
1735 tp
->t_flags
|= TF_ACKNOW
;
1736 tcpstat
.tcps_rcvwinprobe
++;
1740 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
1743 thflags
&= ~(TH_PUSH
|TH_FIN
);
1747 * If last ACK falls within this segment's sequence numbers,
1748 * record its timestamp.
1749 * NOTE that the test is modified according to the latest
1750 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1752 if ((to
.to_flag
& TOF_TS
) != 0 &&
1753 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
1754 tp
->ts_recent_age
= tcp_now
;
1755 tp
->ts_recent
= to
.to_tsval
;
1759 * If a SYN is in the window, then this is an
1760 * error and we send an RST and drop the connection.
1762 if (thflags
& TH_SYN
) {
1763 tp
= tcp_drop(tp
, ECONNRESET
);
1764 postevent(so
, 0, EV_RESET
);
1769 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
1770 * flag is on (half-synchronized state), then queue data for
1771 * later processing; else drop segment and return.
1773 if ((thflags
& TH_ACK
) == 0) {
1774 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
1775 (tp
->t_flags
& TF_NEEDSYN
))
1784 switch (tp
->t_state
) {
1787 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1788 * ESTABLISHED state and continue processing.
1789 * The ACK was checked above.
1791 case TCPS_SYN_RECEIVED
:
1793 tcpstat
.tcps_connects
++;
1795 current_active_connections
++;
1797 /* Do window scaling? */
1798 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
1799 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
1800 tp
->snd_scale
= tp
->requested_s_scale
;
1801 tp
->rcv_scale
= tp
->request_r_scale
;
1804 * Upon successful completion of 3-way handshake,
1805 * update cache.CC if it was undefined, pass any queued
1806 * data to the user, and advance state appropriately.
1808 if ((taop
= tcp_gettaocache(inp
)) != NULL
&&
1810 taop
->tao_cc
= tp
->cc_recv
;
1814 * SYN-RECEIVED -> ESTABLISHED
1815 * SYN-RECEIVED* -> FIN-WAIT-1
1817 if (tp
->t_flags
& TF_NEEDFIN
) {
1818 tp
->t_state
= TCPS_FIN_WAIT_1
;
1819 tp
->t_flags
&= ~TF_NEEDFIN
;
1821 tp
->t_state
= TCPS_ESTABLISHED
;
1822 tp
->t_timer
[TCPT_KEEP
] = tcp_keepidle
;
1825 * If segment contains data or ACK, will call tcp_reass()
1826 * later; if not, do so now to pass queued data to user.
1828 if (tilen
== 0 && (thflags
& TH_FIN
) == 0)
1829 (void) tcp_reass(tp
, (struct tcphdr
*)0, 0,
1830 (struct mbuf
*)0, isipv6
);
1831 tp
->snd_wl1
= th
->th_seq
- 1;
1835 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1836 * ACKs. If the ack is in the range
1837 * tp->snd_una < th->th_ack <= tp->snd_max
1838 * then advance tp->snd_una to th->th_ack and drop
1839 * data from the retransmission queue. If this ACK reflects
1840 * more up to date window information we update our window information.
1842 case TCPS_ESTABLISHED
:
1843 case TCPS_FIN_WAIT_1
:
1844 case TCPS_FIN_WAIT_2
:
1845 case TCPS_CLOSE_WAIT
:
1848 case TCPS_TIME_WAIT
:
1850 if (SEQ_LEQ(th
->th_ack
, tp
->snd_una
)) {
1851 if (tilen
== 0 && tiwin
== tp
->snd_wnd
) {
1852 tcpstat
.tcps_rcvdupack
++;
1854 * If we have outstanding data (other than
1855 * a window probe), this is a completely
1856 * duplicate ack (ie, window info didn't
1857 * change), the ack is the biggest we've
1858 * seen and we've seen exactly our rexmt
1859 * threshhold of them, assume a packet
1860 * has been dropped and retransmit it.
1861 * Kludge snd_nxt & the congestion
1862 * window so we send only this one
1865 * We know we're losing at the current
1866 * window size so do congestion avoidance
1867 * (set ssthresh to half the current window
1868 * and pull our congestion window back to
1869 * the new ssthresh).
1871 * Dup acks mean that packets have left the
1872 * network (they're now cached at the receiver)
1873 * so bump cwnd by the amount in the receiver
1874 * to keep a constant cwnd packets in the
1877 if (tp
->t_timer
[TCPT_REXMT
] == 0 ||
1878 th
->th_ack
!= tp
->snd_una
)
1880 else if (++tp
->t_dupacks
== tcprexmtthresh
) {
1881 tcp_seq onxt
= tp
->snd_nxt
;
1883 min(tp
->snd_wnd
, tp
->snd_cwnd
) / 2 /
1888 tp
->snd_ssthresh
= win
* tp
->t_maxseg
;
1889 tp
->t_timer
[TCPT_REXMT
] = 0;
1891 tp
->snd_nxt
= th
->th_ack
;
1892 tp
->snd_cwnd
= tp
->t_maxseg
;
1893 (void) tcp_output(tp
);
1894 tp
->snd_cwnd
= tp
->snd_ssthresh
+
1895 tp
->t_maxseg
* tp
->t_dupacks
;
1896 if (SEQ_GT(onxt
, tp
->snd_nxt
))
1899 } else if (tp
->t_dupacks
> tcprexmtthresh
) {
1900 tp
->snd_cwnd
+= tp
->t_maxseg
;
1901 (void) tcp_output(tp
);
1909 * If the congestion window was inflated to account
1910 * for the other side's cached packets, retract it.
1912 if (tp
->t_dupacks
>= tcprexmtthresh
&&
1913 tp
->snd_cwnd
> tp
->snd_ssthresh
)
1914 tp
->snd_cwnd
= tp
->snd_ssthresh
;
1916 if (SEQ_GT(th
->th_ack
, tp
->snd_max
)) {
1917 tcpstat
.tcps_rcvacktoomuch
++;
1921 * If we reach this point, ACK is not a duplicate,
1922 * i.e., it ACKs something we sent.
1924 if (tp
->t_flags
& TF_NEEDSYN
) {
1926 * T/TCP: Connection was half-synchronized, and our
1927 * SYN has been ACK'd (so connection is now fully
1928 * synchronized). Go to non-starred state,
1929 * increment snd_una for ACK of SYN, and check if
1930 * we can do window scaling.
1932 tp
->t_flags
&= ~TF_NEEDSYN
;
1934 /* Do window scaling? */
1935 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
1936 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
1937 tp
->snd_scale
= tp
->requested_s_scale
;
1938 tp
->rcv_scale
= tp
->request_r_scale
;
1943 acked
= th
->th_ack
- tp
->snd_una
;
1944 tcpstat
.tcps_rcvackpack
++;
1945 tcpstat
.tcps_rcvackbyte
+= acked
;
1948 * If we have a timestamp reply, update smoothed
1949 * round trip time. If no timestamp is present but
1950 * transmit timer is running and timed sequence
1951 * number was acked, update smoothed round trip time.
1952 * Since we now have an rtt measurement, cancel the
1953 * timer backoff (cf., Phil Karn's retransmit alg.).
1954 * Recompute the initial retransmit timer.
1956 if (to
.to_flag
& TOF_TS
)
1957 tcp_xmit_timer(tp
, tcp_now
- to
.to_tsecr
+ 1);
1958 else if (tp
->t_rtt
&& SEQ_GT(th
->th_ack
, tp
->t_rtseq
))
1959 tcp_xmit_timer(tp
,tp
->t_rtt
);
1962 * If all outstanding data is acked, stop retransmit
1963 * timer and remember to restart (more output or persist).
1964 * If there is more data to be acked, restart retransmit
1965 * timer, using current (possibly backed-off) value.
1967 if (th
->th_ack
== tp
->snd_max
) {
1968 tp
->t_timer
[TCPT_REXMT
] = 0;
1970 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
1971 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
1974 * If no data (only SYN) was ACK'd,
1975 * skip rest of ACK processing.
1981 * When new data is acked, open the congestion window.
1982 * If the window gives us less than ssthresh packets
1983 * in flight, open exponentially (maxseg per packet).
1984 * Otherwise open linearly: maxseg per window
1985 * (maxseg^2 / cwnd per packet).
1988 register u_int cw
= tp
->snd_cwnd
;
1989 register u_int incr
= tp
->t_maxseg
;
1991 if (cw
> tp
->snd_ssthresh
)
1992 incr
= incr
* incr
/ cw
;
1993 tp
->snd_cwnd
= min(cw
+ incr
, TCP_MAXWIN
<<tp
->snd_scale
);
1995 if (acked
> so
->so_snd
.sb_cc
) {
1996 tp
->snd_wnd
-= so
->so_snd
.sb_cc
;
1997 sbdrop(&so
->so_snd
, (int)so
->so_snd
.sb_cc
);
2000 sbdrop(&so
->so_snd
, acked
);
2001 tp
->snd_wnd
-= acked
;
2005 tp
->snd_una
= th
->th_ack
;
2006 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
2007 tp
->snd_nxt
= tp
->snd_una
;
2009 switch (tp
->t_state
) {
2012 * In FIN_WAIT_1 STATE in addition to the processing
2013 * for the ESTABLISHED state if our FIN is now acknowledged
2014 * then enter FIN_WAIT_2.
2016 case TCPS_FIN_WAIT_1
:
2017 if (ourfinisacked
) {
2019 * If we can't receive any more
2020 * data, then closing user can proceed.
2021 * Starting the timer is contrary to the
2022 * specification, but if we don't get a FIN
2023 * we'll hang forever.
2025 if (so
->so_state
& SS_CANTRCVMORE
) {
2026 soisdisconnected(so
);
2027 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;
2029 add_to_time_wait(tp
);
2030 current_active_connections
--;
2031 tp
->t_state
= TCPS_FIN_WAIT_2
;
2036 * In CLOSING STATE in addition to the processing for
2037 * the ESTABLISHED state if the ACK acknowledges our FIN
2038 * then enter the TIME-WAIT state, otherwise ignore
2042 if (ourfinisacked
) {
2043 tp
->t_state
= TCPS_TIME_WAIT
;
2044 tcp_canceltimers(tp
);
2045 /* Shorten TIME_WAIT [RFC-1644, p.28] */
2046 if (tp
->cc_recv
!= 0 &&
2047 tp
->t_duration
< TCPTV_MSL
)
2048 tp
->t_timer
[TCPT_2MSL
] =
2049 tp
->t_rxtcur
* TCPTV_TWTRUNC
;
2051 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
2052 add_to_time_wait(tp
);
2053 current_active_connections
--;
2054 soisdisconnected(so
);
2059 * In LAST_ACK, we may still be waiting for data to drain
2060 * and/or to be acked, as well as for the ack of our FIN.
2061 * If our FIN is now acknowledged, delete the TCB,
2062 * enter the closed state and return.
2065 if (ourfinisacked
) {
2072 * In TIME_WAIT state the only thing that should arrive
2073 * is a retransmission of the remote FIN. Acknowledge
2074 * it and restart the finack timer.
2076 case TCPS_TIME_WAIT
:
2077 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
2078 add_to_time_wait(tp
);
2085 * Update window information.
2086 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2088 if ((thflags
& TH_ACK
) &&
2089 (SEQ_LT(tp
->snd_wl1
, th
->th_seq
) ||
2090 (tp
->snd_wl1
== th
->th_seq
&& (SEQ_LT(tp
->snd_wl2
, th
->th_ack
) ||
2091 (tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
))))) {
2092 /* keep track of pure window updates */
2094 tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
)
2095 tcpstat
.tcps_rcvwinupd
++;
2096 tp
->snd_wnd
= tiwin
;
2097 tp
->snd_wl1
= th
->th_seq
;
2098 tp
->snd_wl2
= th
->th_ack
;
2099 if (tp
->snd_wnd
> tp
->max_sndwnd
)
2100 tp
->max_sndwnd
= tp
->snd_wnd
;
2105 * Process segments with URG.
2107 if ((thflags
& TH_URG
) && th
->th_urp
&&
2108 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
2110 * This is a kludge, but if we receive and accept
2111 * random urgent pointers, we'll crash in
2112 * soreceive. It's hard to imagine someone
2113 * actually wanting to send this much urgent data.
2115 if (th
->th_urp
+ so
->so_rcv
.sb_cc
> sb_max
) {
2116 th
->th_urp
= 0; /* XXX */
2117 thflags
&= ~TH_URG
; /* XXX */
2118 goto dodata
; /* XXX */
2121 * If this segment advances the known urgent pointer,
2122 * then mark the data stream. This should not happen
2123 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2124 * a FIN has been received from the remote side.
2125 * In these states we ignore the URG.
2127 * According to RFC961 (Assigned Protocols),
2128 * the urgent pointer points to the last octet
2129 * of urgent data. We continue, however,
2130 * to consider it to indicate the first octet
2131 * of data past the urgent section as the original
2132 * spec states (in one of two places).
2134 if (SEQ_GT(th
->th_seq
+th
->th_urp
, tp
->rcv_up
)) {
2135 tp
->rcv_up
= th
->th_seq
+ th
->th_urp
;
2136 so
->so_oobmark
= so
->so_rcv
.sb_cc
+
2137 (tp
->rcv_up
- tp
->rcv_nxt
) - 1;
2138 if (so
->so_oobmark
== 0) {
2139 so
->so_state
|= SS_RCVATMARK
;
2140 postevent(so
, 0, EV_OOB
);
2143 tp
->t_oobflags
&= ~(TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
2146 * Remove out of band data so doesn't get presented to user.
2147 * This can happen independent of advancing the URG pointer,
2148 * but if two URG's are pending at once, some out-of-band
2149 * data may creep in... ick.
2151 if (th
->th_urp
<= (u_long
)tilen
2153 && (so
->so_options
& SO_OOBINLINE
) == 0
2156 tcp_pulloutofband(so
, th
, m
);
2159 * If no out of band data is expected,
2160 * pull receive urgent pointer along
2161 * with the receive window.
2163 if (SEQ_GT(tp
->rcv_nxt
, tp
->rcv_up
))
2164 tp
->rcv_up
= tp
->rcv_nxt
;
2168 * Process the segment text, merging it into the TCP sequencing queue,
2169 * and arranging for acknowledgment of receipt if necessary.
2170 * This process logically involves adjusting tp->rcv_wnd as data
2171 * is presented to the user (this happens in tcp_usrreq.c,
2172 * case PRU_RCVD). If a FIN has already been received on this
2173 * connection then we just ignore the text.
2175 if ((tilen
|| (thflags
&TH_FIN
)) &&
2176 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
2177 TCP_REASS(tp
, th
, tilen
, m
, so
, thflags
, isipv6
, need_sorwakeup
);
2179 if (tp
->t_flags
& TF_DELACK
)
2181 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
2182 (((th
->th_src
.s_addr
& 0xffff) << 16) | (th
->th_dst
.s_addr
& 0xffff)),
2183 th
->th_seq
, th
->th_ack
, th
->th_win
);
2186 * Note the amount of data that peer has sent into
2187 * our window, in order to estimate the sender's
2190 len
= so
->so_rcv
.sb_hiwat
- (tp
->rcv_adv
- tp
->rcv_nxt
);
2197 * If FIN is received ACK the FIN and let the user know
2198 * that the connection is closing.
2200 if (thflags
& TH_FIN
) {
2201 if (TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
2203 postevent(so
, 0, EV_FIN
);
2205 * If connection is half-synchronized
2206 * (ie NEEDSYN flag on) then delay ACK,
2207 * so it may be piggybacked when SYN is sent.
2208 * Otherwise, since we received a FIN then no
2209 * more input can be expected, send ACK now.
2211 if (tcp_delack_enabled
&& (tp
->t_flags
& TF_NEEDSYN
)) {
2212 if (last_active_conn_count
> DELACK_BITMASK_THRESH
)
2213 TCP_DELACK_BITSET(tp
->t_inpcb
->hash_element
);
2215 tp
->t_flags
|= TF_DELACK
;
2218 tp
->t_flags
|= TF_ACKNOW
;
2221 switch (tp
->t_state
) {
2224 * In SYN_RECEIVED and ESTABLISHED STATES
2225 * enter the CLOSE_WAIT state.
2227 case TCPS_SYN_RECEIVED
:
2228 case TCPS_ESTABLISHED
:
2229 tp
->t_state
= TCPS_CLOSE_WAIT
;
2233 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2234 * enter the CLOSING state.
2236 case TCPS_FIN_WAIT_1
:
2237 tp
->t_state
= TCPS_CLOSING
;
2241 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2242 * starting the time-wait timer, turning off the other
2245 case TCPS_FIN_WAIT_2
:
2246 tp
->t_state
= TCPS_TIME_WAIT
;
2247 tcp_canceltimers(tp
);
2248 /* Shorten TIME_WAIT [RFC-1644, p.28] */
2249 if (tp
->cc_recv
!= 0 &&
2250 tp
->t_duration
< TCPTV_MSL
) {
2251 tp
->t_timer
[TCPT_2MSL
] =
2252 tp
->t_rxtcur
* TCPTV_TWTRUNC
;
2253 /* For transaction client, force ACK now. */
2254 tp
->t_flags
|= TF_ACKNOW
;
2257 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
2259 add_to_time_wait(tp
);
2260 soisdisconnected(so
);
2264 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2266 case TCPS_TIME_WAIT
:
2267 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
2268 add_to_time_wait(tp
);
2273 if (so
->so_options
& SO_DEBUG
) {
2276 tcp_saveip
._tcp_si6
.ip6_plen
= tilen
;
2278 tcp_saveip
._tcp_si4
.ip_len
= tilen
;
2280 tcp_saveip
.ip_len
= tilen
;
2283 tcp_trace(TA_INPUT
, ostate
, tp
, (void *)&tcp_saveip
,
2289 * Return any desired output.
2291 if (needoutput
|| (tp
->t_flags
& TF_ACKNOW
))
2292 (void) tcp_output(tp
);
2297 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2302 * Generate an ACK dropping incoming segment if it occupies
2303 * sequence space, where the ACK reflects our state.
2305 * We can now skip the test for the RST flag since all
2306 * paths to this code happen after packets containing
2307 * RST have been dropped.
2309 * In the SYN-RECEIVED state, don't send an ACK unless the
2310 * segment we received passes the SYN-RECEIVED ACK test.
2311 * If it fails send a RST. This breaks the loop in the
2312 * "LAND" DoS attack, and also prevents an ACK storm
2313 * between two listening ports that have been sent forged
2314 * SYN segments, each with the source address of the other.
2316 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& (thflags
& TH_ACK
) &&
2317 (SEQ_GT(tp
->snd_una
, th
->th_ack
) ||
2318 SEQ_GT(th
->th_ack
, tp
->snd_max
)) )
2321 if (so
->so_options
& SO_DEBUG
) {
2324 tcp_saveip
._tcp_si6
.ip6_plen
= tilen
;
2326 tcp_saveip
._tcp_si4
.ip_len
= tilen
;
2328 tcp_saveip
.ip_len
= tilen
;
2330 tcp_trace(TA_DROP
, ostate
, tp
, (void *)&tcp_saveip
,
2335 tp
->t_flags
|= TF_ACKNOW
;
2336 (void) tcp_output(tp
);
2341 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2346 * Generate a RST, dropping incoming segment.
2347 * Make ACK acceptable to originator of segment.
2348 * Don't bother to respond if destination was broadcast/multicast.
2350 if ((thflags
& TH_RST
) || m
->m_flags
& (M_BCAST
|M_MCAST
))
2354 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
))
2355 goto drop
; /* anycast check is done at the top */
2358 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)))
2361 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
)) {
2365 tcp_saveip
._tcp_si6
= *ip6
;
2367 tcp_saveip
._tcp_si4
= *ip
;
2374 tcp_saveip
._tcp_si6
.ip6_plen
= tilen
;
2376 tcp_saveip
._tcp_si4
.ip_len
= tilen
;
2378 tcp_saveip
.ip_len
= tilen
;
2380 tcp_trace(TA_DROP
, ostate
, tp
, (void *)&tcp_saveip
,
2384 if (thflags
& TH_ACK
)
2386 tcp_respond(tp
, isipv6
? (void *)ip6
: (void *)ip
, th
, m
,
2387 (tcp_seq
)0, th
->th_ack
, TH_RST
, isipv6
);
2389 tcp_respond(tp
, (void *)ip
, th
, m
,
2390 (tcp_seq
)0, th
->th_ack
, TH_RST
, isipv6
);
2393 if (thflags
& TH_SYN
)
2396 tcp_respond(tp
, isipv6
? (void *)ip6
: (void *)ip
, th
, m
,
2397 th
->th_seq
+tilen
, (tcp_seq
)0, TH_RST
|TH_ACK
,
2400 tcp_respond(tp
, (void *)ip
, th
, m
,
2401 th
->th_seq
+tilen
, (tcp_seq
)0, TH_RST
|TH_ACK
,
2405 /* destroy temporarily created socket */
2412 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2417 * Drop space held by incoming segment and return.
2420 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
)) {
2424 tcp_saveip
._tcp_si6
= *ip6
;
2426 tcp_saveip
._tcp_si4
= *ip
;
2433 tcp_saveip
._tcp_si6
.ip6_plen
= tilen
;
2435 tcp_saveip
._tcp_si4
.ip_len
= tilen
;
2437 tcp_saveip
.ip_len
= tilen
;
2439 tcp_trace(TA_DROP
, ostate
, tp
, (void *)&tcp_saveip
,
2448 /* destroy temporarily created socket */
2451 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2456 tcp_dooptions(tp
, cp
, cnt
, th
, to
)
2466 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
2468 if (opt
== TCPOPT_EOL
)
2470 if (opt
== TCPOPT_NOP
)
2483 if (optlen
!= TCPOLEN_MAXSEG
)
2485 if (!(th
->th_flags
& TH_SYN
))
2487 bcopy((char *) cp
+ 2, (char *) &mss
, sizeof(mss
));
2488 to
->to_maxseg
= ntohs(mss
);
2492 if (optlen
!= TCPOLEN_WINDOW
)
2494 if (!(th
->th_flags
& TH_SYN
))
2496 tp
->t_flags
|= TF_RCVD_SCALE
;
2497 tp
->requested_s_scale
= min(cp
[2], TCP_MAX_WINSHIFT
);
2500 case TCPOPT_TIMESTAMP
:
2501 if (optlen
!= TCPOLEN_TIMESTAMP
)
2503 to
->to_flag
|= TOF_TS
;
2504 bcopy((char *)cp
+ 2,
2505 (char *)&to
->to_tsval
, sizeof(to
->to_tsval
));
2506 NTOHL(to
->to_tsval
);
2507 bcopy((char *)cp
+ 6,
2508 (char *)&to
->to_tsecr
, sizeof(to
->to_tsecr
));
2509 NTOHL(to
->to_tsecr
);
2512 * A timestamp received in a SYN makes
2513 * it ok to send timestamp requests and replies.
2515 if (th
->th_flags
& TH_SYN
) {
2516 tp
->t_flags
|= TF_RCVD_TSTMP
;
2517 tp
->ts_recent
= to
->to_tsval
;
2518 tp
->ts_recent_age
= tcp_now
;
2522 if (optlen
!= TCPOLEN_CC
)
2524 to
->to_flag
|= TOF_CC
;
2525 bcopy((char *)cp
+ 2,
2526 (char *)&to
->to_cc
, sizeof(to
->to_cc
));
2529 * A CC or CC.new option received in a SYN makes
2530 * it ok to send CC in subsequent segments.
2532 if (th
->th_flags
& TH_SYN
)
2533 tp
->t_flags
|= TF_RCVD_CC
;
2536 if (optlen
!= TCPOLEN_CC
)
2538 if (!(th
->th_flags
& TH_SYN
))
2540 to
->to_flag
|= TOF_CCNEW
;
2541 bcopy((char *)cp
+ 2,
2542 (char *)&to
->to_cc
, sizeof(to
->to_cc
));
2545 * A CC or CC.new option received in a SYN makes
2546 * it ok to send CC in subsequent segments.
2548 tp
->t_flags
|= TF_RCVD_CC
;
2551 if (optlen
!= TCPOLEN_CC
)
2553 if (!(th
->th_flags
& TH_SYN
))
2555 to
->to_flag
|= TOF_CCECHO
;
2556 bcopy((char *)cp
+ 2,
2557 (char *)&to
->to_ccecho
, sizeof(to
->to_ccecho
));
2558 NTOHL(to
->to_ccecho
);
2565 * Pull out of band byte out of a segment so
2566 * it doesn't appear in the user's data queue.
2567 * It is still reflected in the segment length for
2568 * sequencing purposes.
2571 tcp_pulloutofband(so
, th
, m
)
2574 register struct mbuf
*m
;
2576 int cnt
= th
->th_urp
- 1;
2579 if (m
->m_len
> cnt
) {
2580 char *cp
= mtod(m
, caddr_t
) + cnt
;
2581 struct tcpcb
*tp
= sototcpcb(so
);
2584 tp
->t_oobflags
|= TCPOOB_HAVEDATA
;
2585 bcopy(cp
+1, cp
, (unsigned)(m
->m_len
- cnt
- 1));
2594 panic("tcp_pulloutofband");
2598 * Collect new round-trip time estimate
2599 * and update averages and current timeout.
2602 tcp_xmit_timer(tp
, rtt
)
2603 register struct tcpcb
*tp
;
2608 tcpstat
.tcps_rttupdated
++;
2610 if (tp
->t_srtt
!= 0) {
2612 * srtt is stored as fixed point with 5 bits after the
2613 * binary point (i.e., scaled by 8). The following magic
2614 * is equivalent to the smoothing algorithm in rfc793 with
2615 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2616 * point). Adjust rtt to origin 0.
2618 delta
= ((rtt
- 1) << TCP_DELTA_SHIFT
)
2619 - (tp
->t_srtt
>> (TCP_RTT_SHIFT
- TCP_DELTA_SHIFT
));
2621 if ((tp
->t_srtt
+= delta
) <= 0)
2625 * We accumulate a smoothed rtt variance (actually, a
2626 * smoothed mean difference), then set the retransmit
2627 * timer to smoothed rtt + 4 times the smoothed variance.
2628 * rttvar is stored as fixed point with 4 bits after the
2629 * binary point (scaled by 16). The following is
2630 * equivalent to rfc793 smoothing with an alpha of .75
2631 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
2632 * rfc793's wired-in beta.
2636 delta
-= tp
->t_rttvar
>> (TCP_RTTVAR_SHIFT
- TCP_DELTA_SHIFT
);
2637 if ((tp
->t_rttvar
+= delta
) <= 0)
2641 * No rtt measurement yet - use the unsmoothed rtt.
2642 * Set the variance to half the rtt (so our first
2643 * retransmit happens at 3*rtt).
2645 tp
->t_srtt
= rtt
<< TCP_RTT_SHIFT
;
2646 tp
->t_rttvar
= rtt
<< (TCP_RTTVAR_SHIFT
- 1);
2652 * the retransmit should happen at rtt + 4 * rttvar.
2653 * Because of the way we do the smoothing, srtt and rttvar
2654 * will each average +1/2 tick of bias. When we compute
2655 * the retransmit timer, we want 1/2 tick of rounding and
2656 * 1 extra tick because of +-1/2 tick uncertainty in the
2657 * firing of the timer. The bias will give us exactly the
2658 * 1.5 tick we need. But, because the bias is
2659 * statistical, we have to test that we don't drop below
2660 * the minimum feasible timer (which is 2 ticks).
2662 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
2663 max(tp
->t_rttmin
, rtt
+ 2), TCPTV_REXMTMAX
);
2666 * We received an ack for a packet that wasn't retransmitted;
2667 * it is probably safe to discard any error indications we've
2668 * received recently. This isn't quite right, but close enough
2669 * for now (a route might have failed after we sent a segment,
2670 * and the return path might not be symmetrical).
2672 tp
->t_softerror
= 0;
2676 * Determine a reasonable value for maxseg size.
2677 * If the route is known, check route for mtu.
2678 * If none, use an mss that can be handled on the outgoing
2679 * interface without forcing IP to fragment; if bigger than
2680 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2681 * to utilize large mbufs. If no route is found, route has no mtu,
2682 * or the destination isn't local, use a default, hopefully conservative
2683 * size (usually 512 or the default IP max size, but no more than the mtu
2684 * of the interface), as we can't discover anything about intervening
2685 * gateways or networks. We also initialize the congestion/slow start
2686 * window to be a single segment if the destination isn't local.
2687 * While looking at the routing entry, we also initialize other path-dependent
2688 * parameters from pre-set or cached values in the routing entry.
2690 * Also take into account the space needed for options that we
2691 * send regularly. Make maxseg shorter by that amount to assure
2692 * that we can send maxseg amount of data even when the options
2693 * are present. Store the upper limit of the length of options plus
2696 * NOTE that this routine is only called when we process an incoming
2697 * segment, for outgoing segments only tcp_mssopt is called.
2699 * In case of T/TCP, we call this routine during implicit connection
2700 * setup as well (offer = -1), to initialize maxseg from the cached
2704 tcp_mss(tp
, offer
, isipv6
)
2711 register struct rtentry
*rt
;
2713 register int rtt
, mss
;
2717 struct rmxp_tao
*taop
;
2718 int origoffer
= offer
;
2720 int lgminh
= isipv6
? sizeof (struct tcpip6hdr
) :
2721 sizeof (struct tcpiphdr
);
2723 #define lgminh (sizeof (struct tcpiphdr))
2729 rt
= tcp_rtlookup6(inp
);
2732 rt
= tcp_rtlookup(inp
);
2734 tp
->t_maxopd
= tp
->t_maxseg
=
2736 isipv6
? tcp_v6mssdflt
:
2742 so
= inp
->inp_socket
;
2744 taop
= rmx_taop(rt
->rt_rmx
);
2746 * Offer == -1 means that we didn't receive SYN yet,
2747 * use cached value in that case;
2750 offer
= taop
->tao_mssopt
;
2752 * Offer == 0 means that there was no MSS on the SYN segment,
2753 * in this case we use tcp_mssdflt.
2758 isipv6
? tcp_v6mssdflt
:
2763 * Sanity check: make sure that maxopd will be large
2764 * enough to allow some data on segments even is the
2765 * all the option space is used (40bytes). Otherwise
2766 * funny things may happen in tcp_output.
2768 offer
= max(offer
, 64);
2769 taop
->tao_mssopt
= offer
;
2772 * While we're here, check if there's an initial rtt
2773 * or rttvar. Convert from the route-table units
2774 * to scaled multiples of the slow timeout timer.
2776 if (tp
->t_srtt
== 0 && (rtt
= rt
->rt_rmx
.rmx_rtt
)) {
2778 * XXX the lock bit for RTT indicates that the value
2779 * is also a minimum value; this is subject to time.
2781 if (rt
->rt_rmx
.rmx_locks
& RTV_RTT
)
2782 tp
->t_rttmin
= rtt
/ (RTM_RTTUNIT
/ PR_SLOWHZ
);
2783 tp
->t_srtt
= rtt
/ (RTM_RTTUNIT
/ (PR_SLOWHZ
* TCP_RTT_SCALE
));
2784 tcpstat
.tcps_usedrtt
++;
2785 if (rt
->rt_rmx
.rmx_rttvar
) {
2786 tp
->t_rttvar
= rt
->rt_rmx
.rmx_rttvar
/
2787 (RTM_RTTUNIT
/ (PR_SLOWHZ
* TCP_RTTVAR_SCALE
));
2788 tcpstat
.tcps_usedrttvar
++;
2790 /* default variation is +- 1 rtt */
2792 tp
->t_srtt
* TCP_RTTVAR_SCALE
/ TCP_RTT_SCALE
;
2794 TCPT_RANGESET(tp
->t_rxtcur
,
2795 ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1,
2796 tp
->t_rttmin
, TCPTV_REXMTMAX
);
2799 * if there's an mtu associated with the route, use it
2800 * else, use the link mtu.
2802 if (rt
->rt_rmx
.rmx_mtu
)
2803 mss
= rt
->rt_rmx
.rmx_mtu
- lgminh
;
2807 isipv6
? nd_ifinfo
[rt
->rt_ifp
->if_index
].linkmtu
:
2809 ifp
->if_mtu
- lgminh
;
2811 if (rt
->rt_rmx
.rmx_mtu
== 0) {
2814 if (!in6_localaddr(&inp
->in6p_faddr
))
2815 mss
= min(mss
, tcp_v6mssdflt
);
2818 if (!in_localaddr(inp
->inp_faddr
))
2819 mss
= min(mss
, tcp_mssdflt
);
2821 mss
= min(mss
, offer
);
2823 * maxopd stores the maximum length of data AND options
2824 * in a segment; maxseg is the amount of data in a normal
2825 * segment. We need to store this value (maxopd) apart
2826 * from maxseg, because now every segment carries options
2827 * and thus we normally have somewhat less data in segments.
2832 * In case of T/TCP, origoffer==-1 indicates, that no segments
2833 * were received yet. In this case we just guess, otherwise
2834 * we do the same as before T/TCP.
2836 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
2838 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
))
2839 mss
-= TCPOLEN_TSTAMP_APPA
;
2840 if ((tp
->t_flags
& (TF_REQ_CC
|TF_NOOPT
)) == TF_REQ_CC
&&
2842 (tp
->t_flags
& TF_RCVD_CC
) == TF_RCVD_CC
))
2843 mss
-= TCPOLEN_CC_APPA
;
2845 #if (MCLBYTES & (MCLBYTES - 1)) == 0
2847 mss
&= ~(MCLBYTES
-1);
2850 mss
= mss
/ MCLBYTES
* MCLBYTES
;
2853 * If there's a pipesize, change the socket buffer
2854 * to that size. Make the socket buffers an integral
2855 * number of mss units; if the mss is larger than
2856 * the socket buffer, decrease the mss.
2859 if ((bufsize
= rt
->rt_rmx
.rmx_sendpipe
) == 0)
2861 bufsize
= so
->so_snd
.sb_hiwat
;
2865 bufsize
= roundup(bufsize
, mss
);
2866 if (bufsize
> sb_max
)
2868 (void)sbreserve(&so
->so_snd
, bufsize
);
2873 if ((bufsize
= rt
->rt_rmx
.rmx_recvpipe
) == 0)
2875 bufsize
= so
->so_rcv
.sb_hiwat
;
2876 if (bufsize
> mss
) {
2877 bufsize
= roundup(bufsize
, mss
);
2878 if (bufsize
> sb_max
)
2880 (void)sbreserve(&so
->so_rcv
, bufsize
);
2883 * Don't force slow-start on local network.
2887 if (!in6_localaddr(&inp
->in6p_faddr
))
2891 if (!in_localaddr(inp
->inp_faddr
))
2894 if (rt
->rt_rmx
.rmx_ssthresh
) {
2896 * There's some sort of gateway or interface
2897 * buffer limit on the path. Use this to set
2898 * the slow start threshhold, but set the
2899 * threshold to no less than 2*mss.
2901 tp
->snd_ssthresh
= max(2 * mss
, rt
->rt_rmx
.rmx_ssthresh
);
2902 tcpstat
.tcps_usedssthresh
++;
2907 * Determine the MSS option to send on an outgoing SYN.
2910 tcp_mssopt(tp
, isipv6
)
2919 int lgminh
= isipv6
? sizeof (struct tcpip6hdr
) :
2920 sizeof (struct tcpiphdr
);
2922 #define lgminh (sizeof (struct tcpiphdr))
2927 rt
= tcp_rtlookup6(tp
->t_inpcb
);
2930 rt
= tcp_rtlookup(tp
->t_inpcb
);
2934 isipv6
? tcp_v6mssdflt
:
2938 mss
= rt
->rt_ifp
->if_mtu
- lgminh
;