2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
61 * $FreeBSD: src/sys/netinet/tcp_input.c,v 1.107.2.16 2001/08/22 00:59:12 silby Exp $
64 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/sysctl.h>
74 #include <sys/malloc.h>
76 #include <sys/proc.h> /* for proc0 declaration */
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/syslog.h>
82 #include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */
84 #include <machine/endian.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */
94 #include <netinet/in_var.h>
95 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
96 #include <netinet/in_pcb.h>
97 #include <netinet/ip_var.h>
99 #include <netinet/ip6.h>
100 #include <netinet/icmp6.h>
101 #include <netinet6/nd6.h>
102 #include <netinet6/ip6_var.h>
103 #include <netinet6/in6_pcb.h>
105 #include <netinet/tcp.h>
106 #include <netinet/tcp_fsm.h>
107 #include <netinet/tcp_seq.h>
108 #include <netinet/tcp_timer.h>
109 #include <netinet/tcp_var.h>
111 #include <netinet6/tcp6_var.h>
113 #include <netinet/tcpip.h>
115 #include <netinet/tcp_debug.h>
116 u_char tcp_saveipgen
[40]; /* the size must be of max ip header, now IPv6 */
117 struct tcphdr tcp_savetcp
;
118 #endif /* TCPDEBUG */
121 #include <netinet6/ipsec.h>
123 #include <netinet6/ipsec6.h>
125 #include <netkey/key.h>
128 #if CONFIG_MACF_NET || CONFIG_MACF_SOCKET
129 #include <security/mac_framework.h>
130 #endif /* CONFIG_MACF_NET || CONFIG_MACF_SOCKET */
132 #include <sys/kdebug.h>
135 MALLOC_DEFINE(M_TSEGQ
, "tseg_qent", "TCP segment queue entry");
138 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 0)
139 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 2)
140 #define DBG_FNC_TCP_INPUT NETDBG_CODE(DBG_NETTCP, (3 << 8))
141 #define DBG_FNC_TCP_NEWCONN NETDBG_CODE(DBG_NETTCP, (7 << 8))
143 static int tcprexmtthresh
= 2;
147 extern int ipsec_bypass
;
150 struct tcpstat tcpstat
;
152 static int log_in_vain
= 0;
153 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, log_in_vain
, CTLFLAG_RW
,
154 &log_in_vain
, 0, "Log all incoming TCP connections");
156 static int blackhole
= 0;
157 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, blackhole
, CTLFLAG_RW
,
158 &blackhole
, 0, "Do not send RST when dropping refused connections");
160 int tcp_delack_enabled
= 3;
161 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, delayed_ack
, CTLFLAG_RW
,
162 &tcp_delack_enabled
, 0,
163 "Delay ACK to try and piggyback it onto a data packet");
165 int tcp_lq_overflow
= 1;
166 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tcp_lq_overflow
, CTLFLAG_RW
,
168 "Listen Queue Overflow");
171 static int drop_synfin
= 1;
172 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, drop_synfin
, CTLFLAG_RW
,
173 &drop_synfin
, 0, "Drop TCP packets with SYN+FIN set");
176 SYSCTL_NODE(_net_inet_tcp
, OID_AUTO
, reass
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0,
177 "TCP Segment Reassembly Queue");
179 __private_extern__
int tcp_reass_maxseg
= 0;
180 SYSCTL_INT(_net_inet_tcp_reass
, OID_AUTO
, maxsegments
, CTLFLAG_RW
,
181 &tcp_reass_maxseg
, 0,
182 "Global maximum number of TCP Segments in Reassembly Queue");
184 __private_extern__
int tcp_reass_qsize
= 0;
185 SYSCTL_INT(_net_inet_tcp_reass
, OID_AUTO
, cursegments
, CTLFLAG_RD
,
187 "Global number of TCP Segments currently in Reassembly Queue");
189 static int tcp_reass_overflows
= 0;
190 SYSCTL_INT(_net_inet_tcp_reass
, OID_AUTO
, overflows
, CTLFLAG_RD
,
191 &tcp_reass_overflows
, 0,
192 "Global number of TCP Segment Reassembly Queue Overflows");
195 __private_extern__
int slowlink_wsize
= 8192;
196 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, slowlink_wsize
, CTLFLAG_RW
,
197 &slowlink_wsize
, 0, "Maximum advertised window size for slowlink");
199 static int maxseg_unacked
= 8;
200 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, maxseg_unacked
, CTLFLAG_RW
,
201 &maxseg_unacked
, 0, "Maximum number of outstanding segments left unacked");
203 static int tcp_do_rfc3465
= 1;
204 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rfc3465
, CTLFLAG_RW
,
205 &tcp_do_rfc3465
, 0, "");
207 static int tcp_do_rfc3465_lim2
= 1;
208 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rfc3465_lim2
, CTLFLAG_RW
,
209 &tcp_do_rfc3465_lim2
, 0, "Appropriate bytes counting w/ L=2*SMSS");
211 #if CONFIG_IFEF_NOWINDOWSCALE
212 int tcp_obey_ifef_nowindowscale
= 0;
213 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, obey_ifef_nowindowscale
, CTLFLAG_RW
,
214 &tcp_obey_ifef_nowindowscale
, 0, "");
217 extern int tcp_TCPTV_MIN
;
221 struct inpcbhead tcb
;
222 #define tcb6 tcb /* for KAME src sync over BSD*'s */
223 struct inpcbinfo tcbinfo
;
225 static void tcp_dooptions(struct tcpcb
*,
226 u_char
*, int, struct tcphdr
*, struct tcpopt
*, unsigned int);
227 static void tcp_pulloutofband(struct socket
*,
228 struct tcphdr
*, struct mbuf
*, int);
229 static int tcp_reass(struct tcpcb
*, struct tcphdr
*, int *,
231 static void tcp_xmit_timer(struct tcpcb
*, int);
232 static inline unsigned int tcp_maxmtu(struct rtentry
*);
234 static inline unsigned int tcp_maxmtu6(struct rtentry
*);
237 /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
239 #define ND6_HINT(tp) \
241 if ((tp) && (tp)->t_inpcb && \
242 ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \
243 (tp)->t_inpcb->in6p_route.ro_rt) \
244 nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \
250 extern u_int32_t
*delack_bitmask
;
252 extern void add_to_time_wait(struct tcpcb
*);
253 extern void postevent(struct socket
*, struct sockbuf
*, int);
255 extern void ipfwsyslog( int level
, const char *format
,...);
256 extern int ChkAddressOK( __uint32_t dstaddr
, __uint32_t srcaddr
);
257 extern int fw_verbose
;
258 __private_extern__
int tcp_sockthreshold
;
259 __private_extern__
int tcp_win_scale
;
262 #define log_in_vain_log( a ) { \
263 if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \
269 #define log_in_vain_log( a ) { log a; }
274 * Indicate whether this ack should be delayed.
275 * We can delay the ack if:
276 * - delayed acks are enabled (set to 1) and
277 * - our last ack wasn't a 0-sized window. We never want to delay
278 * the ack that opens up a 0-sized window.
279 * - delayed acks are enabled (set to 2, "more compatible") and
280 * - our last ack wasn't a 0-sized window.
281 * - if the peer hasn't sent us a TH_PUSH data packet (this solves 3649245)
282 * - the peer hasn't sent us a TH_PUSH data packet, if he did, take this as a clue that we
283 * need to ACK with no delay. This helps higher level protocols who won't send
284 * us more data even if the window is open because their last "segment" hasn't been ACKed
285 * - delayed acks are enabled (set to 3, "streaming detection") and
286 * - if we receive more than "maxseg_unacked" full packets per second on this socket
287 * - if we don't have more than "maxseg_unacked" delayed so far
288 * - if those criteria aren't met, acts like "2". Allowing faster acking while browsing for example.
291 #define DELAY_ACK(tp) \
292 (((tcp_delack_enabled == 1) && ((tp->t_flags & TF_RXWIN0SENT) == 0)) || \
293 (((tcp_delack_enabled == 2) && (tp->t_flags & TF_RXWIN0SENT) == 0) && \
294 ((thflags & TH_PUSH) == 0) && ((tp->t_flags & TF_DELACK) == 0)) || \
295 (((tcp_delack_enabled == 3) && (tp->t_flags & TF_RXWIN0SENT) == 0) && \
296 (tp->t_rcvtime == 0) && ((thflags & TH_PUSH) == 0) && \
297 (((tp->t_unacksegs == 0)) || \
298 ((tp->rcv_byps > (maxseg_unacked * tp->t_maxseg)) && (tp->t_unacksegs < maxseg_unacked)))))
300 static int tcp_dropdropablreq(struct socket
*head
);
301 static void tcp_newreno_partial_ack(struct tcpcb
*tp
, struct tcphdr
*th
);
305 tcp_reass(tp
, th
, tlenp
, m
)
306 register struct tcpcb
*tp
;
307 register struct tcphdr
*th
;
312 struct tseg_qent
*p
= NULL
;
313 struct tseg_qent
*nq
;
314 struct tseg_qent
*te
= NULL
;
315 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
320 * Call with th==0 after become established to
321 * force pre-ESTABLISHED data up to user socket.
327 * Limit the number of segments in the reassembly queue to prevent
328 * holding on to too many segments (and thus running out of mbufs).
329 * Make sure to let the missing segment through which caused this
330 * queue. Always keep one global queue entry spare to be able to
331 * process the missing segment.
333 if (th
->th_seq
!= tp
->rcv_nxt
&&
334 tcp_reass_qsize
+ 1 >= tcp_reass_maxseg
) {
335 tcp_reass_overflows
++;
336 tcpstat
.tcps_rcvmemdrop
++;
342 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
343 MALLOC(te
, struct tseg_qent
*, sizeof (struct tseg_qent
), M_TSEGQ
,
346 tcpstat
.tcps_rcvmemdrop
++;
353 * Find a segment which begins after this one does.
355 LIST_FOREACH(q
, &tp
->t_segq
, tqe_q
) {
356 if (SEQ_GT(q
->tqe_th
->th_seq
, th
->th_seq
))
362 * If there is a preceding segment, it may provide some of
363 * our data already. If so, drop the data from the incoming
364 * segment. If it provides all of our data, drop us.
368 /* conversion to int (in i) handles seq wraparound */
369 i
= p
->tqe_th
->th_seq
+ p
->tqe_len
- th
->th_seq
;
372 tcpstat
.tcps_rcvduppack
++;
373 tcpstat
.tcps_rcvdupbyte
+= *tlenp
;
378 * Try to present any queued data
379 * at the left window edge to the user.
380 * This is needed after the 3-WHS
383 goto present
; /* ??? */
390 tcpstat
.tcps_rcvoopack
++;
391 tcpstat
.tcps_rcvoobyte
+= *tlenp
;
394 * While we overlap succeeding segments trim them or,
395 * if they are completely covered, dequeue them.
398 register int i
= (th
->th_seq
+ *tlenp
) - q
->tqe_th
->th_seq
;
401 if (i
< q
->tqe_len
) {
402 q
->tqe_th
->th_seq
+= i
;
408 nq
= LIST_NEXT(q
, tqe_q
);
409 LIST_REMOVE(q
, tqe_q
);
416 /* Insert the new segment queue entry into place. */
419 te
->tqe_len
= *tlenp
;
422 LIST_INSERT_HEAD(&tp
->t_segq
, te
, tqe_q
);
424 LIST_INSERT_AFTER(p
, te
, tqe_q
);
429 * Present data to user, advancing rcv_nxt through
430 * completed sequence space.
432 if (!TCPS_HAVEESTABLISHED(tp
->t_state
))
434 q
= LIST_FIRST(&tp
->t_segq
);
435 if (!q
|| q
->tqe_th
->th_seq
!= tp
->rcv_nxt
)
438 tp
->rcv_nxt
+= q
->tqe_len
;
439 flags
= q
->tqe_th
->th_flags
& TH_FIN
;
440 nq
= LIST_NEXT(q
, tqe_q
);
441 LIST_REMOVE(q
, tqe_q
);
442 if (so
->so_state
& SS_CANTRCVMORE
)
445 if (sbappendstream(&so
->so_rcv
, q
->tqe_m
))
451 } while (q
&& q
->tqe_th
->th_seq
== tp
->rcv_nxt
);
455 if ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0) {
457 KERNEL_DEBUG(DBG_LAYER_BEG
,
458 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
459 (((tp
->t_inpcb
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
460 (tp
->t_inpcb
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
466 KERNEL_DEBUG(DBG_LAYER_BEG
,
467 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
468 (((tp
->t_inpcb
->inp_laddr
.s_addr
& 0xffff) << 16) |
469 (tp
->t_inpcb
->inp_faddr
.s_addr
& 0xffff)),
473 sorwakeup(so
); /* done with socket lock held */
479 * Reduce congestion window.
482 tcp_reduce_congestion_window(
487 win
= min(tp
->snd_wnd
, tp
->snd_cwnd
) /
491 tp
->snd_ssthresh
= win
* tp
->t_maxseg
;
492 ENTER_FASTRECOVERY(tp
);
493 tp
->snd_recover
= tp
->snd_max
;
494 tp
->t_timer
[TCPT_REXMT
] = 0;
496 tp
->ecn_flags
|= TE_SENDCWR
;
497 tp
->snd_cwnd
= tp
->snd_ssthresh
+
498 tp
->t_maxseg
* tcprexmtthresh
;
503 * TCP input routine, follows pages 65-76 of the
504 * protocol specification dated September, 1981 very closely.
512 register struct mbuf
*m
= *mp
;
513 struct in6_ifaddr
*ia6
;
515 IP6_EXTHDR_CHECK(m
, *offp
, sizeof(struct tcphdr
), return IPPROTO_DONE
);
518 * draft-itojun-ipv6-tcp-to-anycast
519 * better place to put this in?
521 ia6
= ip6_getdstifaddr(m
);
522 if (ia6
&& (ia6
->ia6_flags
& IN6_IFF_ANYCAST
)) {
525 ip6
= mtod(m
, struct ip6_hdr
*);
526 icmp6_error(m
, ICMP6_DST_UNREACH
, ICMP6_DST_UNREACH_ADDR
,
527 (caddr_t
)&ip6
->ip6_dst
- (caddr_t
)ip6
);
541 register struct tcphdr
*th
;
542 register struct ip
*ip
= NULL
;
543 register struct ipovly
*ipov
;
544 register struct inpcb
*inp
;
549 register struct tcpcb
*tp
= 0;
550 register int thflags
;
551 struct socket
*so
= 0;
552 int todrop
, acked
, ourfinisacked
, needoutput
= 0;
553 struct in_addr laddr
;
555 struct in6_addr laddr6
;
561 struct tcpopt to
; /* options in this segment */
562 struct sockaddr_in
*next_hop
= NULL
;
566 struct m_tag
*fwd_tag
;
567 u_char ip_ecn
= IPTOS_ECN_NOTECT
;
568 unsigned int ifscope
;
571 * Record the interface where this segment arrived on; this does not
572 * affect normal data output (for non-detached TCP) as it provides a
573 * hint about which route and interface to use for sending in the
574 * absence of a PCB, when scoped routing (and thus source interface
575 * selection) are enabled.
577 if ((m
->m_flags
& M_PKTHDR
) && m
->m_pkthdr
.rcvif
!= NULL
)
578 ifscope
= m
->m_pkthdr
.rcvif
->if_index
;
580 ifscope
= IFSCOPE_NONE
;
582 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
583 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
)) {
584 fwd_tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
585 KERNEL_TAG_TYPE_IPFORWARD
, NULL
);
589 if (fwd_tag
!= NULL
) {
590 struct ip_fwd_tag
*ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
592 next_hop
= ipfwd_tag
->next_hop
;
593 m_tag_delete(m
, fwd_tag
);
597 struct ip6_hdr
*ip6
= NULL
;
600 int rstreason
; /* For badport_bandlim accounting purposes */
601 struct proc
*proc0
=current_proc();
603 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_START
,0,0,0,0,0);
606 isipv6
= (mtod(m
, struct ip
*)->ip_v
== 6) ? 1 : 0;
608 bzero((char *)&to
, sizeof(to
));
610 tcpstat
.tcps_rcvtotal
++;
616 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
617 ip6
= mtod(m
, struct ip6_hdr
*);
618 tlen
= sizeof(*ip6
) + ntohs(ip6
->ip6_plen
) - off0
;
619 if (in6_cksum(m
, IPPROTO_TCP
, off0
, tlen
)) {
620 tcpstat
.tcps_rcvbadsum
++;
623 th
= (struct tcphdr
*)((caddr_t
)ip6
+ off0
);
625 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
626 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
627 th
->th_seq
, th
->th_ack
, th
->th_win
);
629 * Be proactive about unspecified IPv6 address in source.
630 * As we use all-zero to indicate unbounded/unconnected pcb,
631 * unspecified IPv6 address can be used to confuse us.
633 * Note that packets with unspecified IPv6 destination is
634 * already dropped in ip6_input.
636 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
644 * Get IP and TCP header together in first mbuf.
645 * Note: IP leaves IP header in first mbuf.
647 if (off0
> sizeof (struct ip
)) {
648 ip_stripoptions(m
, (struct mbuf
*)0);
649 off0
= sizeof(struct ip
);
650 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
651 m
->m_pkthdr
.csum_flags
= 0; /* invalidate hwcksuming */
654 if (m
->m_len
< sizeof (struct tcpiphdr
)) {
655 if ((m
= m_pullup(m
, sizeof (struct tcpiphdr
))) == 0) {
656 tcpstat
.tcps_rcvshort
++;
660 ip
= mtod(m
, struct ip
*);
661 ipov
= (struct ipovly
*)ip
;
662 th
= (struct tcphdr
*)((caddr_t
)ip
+ off0
);
665 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
666 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
667 th
->th_seq
, th
->th_ack
, th
->th_win
);
669 if (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) {
670 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
) {
673 *(uint32_t*)&b
[0] = *(uint32_t*)&ipov
->ih_x1
[0];
674 *(uint32_t*)&b
[4] = *(uint32_t*)&ipov
->ih_x1
[4];
675 *(uint8_t*)&b
[8] = *(uint8_t*)&ipov
->ih_x1
[8];
677 bzero(ipov
->ih_x1
, sizeof(ipov
->ih_x1
));
678 ipov
->ih_len
= (u_short
)tlen
;
680 #if BYTE_ORDER != BIG_ENDIAN
684 pseudo
= in_cksum(m
, sizeof (struct ip
));
686 *(uint32_t*)&ipov
->ih_x1
[0] = *(uint32_t*)&b
[0];
687 *(uint32_t*)&ipov
->ih_x1
[4] = *(uint32_t*)&b
[4];
688 *(uint8_t*)&ipov
->ih_x1
[8] = *(uint8_t*)&b
[8];
690 th
->th_sum
= in_addword(pseudo
, (m
->m_pkthdr
.csum_data
& 0xFFFF));
692 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
)
693 th
->th_sum
= m
->m_pkthdr
.csum_data
;
695 th
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
,
696 ip
->ip_dst
.s_addr
, htonl(m
->m_pkthdr
.csum_data
+
697 ip
->ip_len
+ IPPROTO_TCP
));
699 th
->th_sum
^= 0xffff;
703 * Checksum extended TCP header and data.
705 *(uint32_t*)&b
[0] = *(uint32_t*)&ipov
->ih_x1
[0];
706 *(uint32_t*)&b
[4] = *(uint32_t*)&ipov
->ih_x1
[4];
707 *(uint8_t*)&b
[8] = *(uint8_t*)&ipov
->ih_x1
[8];
709 len
= sizeof (struct ip
) + tlen
;
710 bzero(ipov
->ih_x1
, sizeof(ipov
->ih_x1
));
711 ipov
->ih_len
= (u_short
)tlen
;
713 #if BYTE_ORDER != BIG_ENDIAN
717 th
->th_sum
= in_cksum(m
, len
);
719 *(uint32_t*)&ipov
->ih_x1
[0] = *(uint32_t*)&b
[0];
720 *(uint32_t*)&ipov
->ih_x1
[4] = *(uint32_t*)&b
[4];
721 *(uint8_t*)&ipov
->ih_x1
[8] = *(uint8_t*)&b
[8];
723 tcp_in_cksum_stats(len
);
726 tcpstat
.tcps_rcvbadsum
++;
730 /* Re-initialization for later version check */
731 ip
->ip_v
= IPVERSION
;
733 ip_ecn
= (ip
->ip_tos
& IPTOS_ECN_MASK
);
737 * Check that TCP offset makes sense,
738 * pull out TCP options and adjust length. XXX
740 off
= th
->th_off
<< 2;
741 if (off
< sizeof (struct tcphdr
) || off
> tlen
) {
742 tcpstat
.tcps_rcvbadoff
++;
745 tlen
-= off
; /* tlen is used instead of ti->ti_len */
746 if (off
> sizeof (struct tcphdr
)) {
749 IP6_EXTHDR_CHECK(m
, off0
, off
, return);
750 ip6
= mtod(m
, struct ip6_hdr
*);
751 th
= (struct tcphdr
*)((caddr_t
)ip6
+ off0
);
755 if (m
->m_len
< sizeof(struct ip
) + off
) {
756 if ((m
= m_pullup(m
, sizeof (struct ip
) + off
)) == 0) {
757 tcpstat
.tcps_rcvshort
++;
760 ip
= mtod(m
, struct ip
*);
761 ipov
= (struct ipovly
*)ip
;
762 th
= (struct tcphdr
*)((caddr_t
)ip
+ off0
);
765 optlen
= off
- sizeof (struct tcphdr
);
766 optp
= (u_char
*)(th
+ 1);
768 * Do quick retrieval of timestamp options ("options
769 * prediction?"). If timestamp is the only option and it's
770 * formatted as recommended in RFC 1323 appendix A, we
771 * quickly get the values now and not bother calling
772 * tcp_dooptions(), etc.
774 if ((optlen
== TCPOLEN_TSTAMP_APPA
||
775 (optlen
> TCPOLEN_TSTAMP_APPA
&&
776 optp
[TCPOLEN_TSTAMP_APPA
] == TCPOPT_EOL
)) &&
777 *(u_int32_t
*)optp
== htonl(TCPOPT_TSTAMP_HDR
) &&
778 (th
->th_flags
& TH_SYN
) == 0) {
779 to
.to_flags
|= TOF_TS
;
780 to
.to_tsval
= ntohl(*(u_int32_t
*)(optp
+ 4));
781 to
.to_tsecr
= ntohl(*(u_int32_t
*)(optp
+ 8));
782 optp
= NULL
; /* we've parsed the options */
785 thflags
= th
->th_flags
;
789 * If the drop_synfin option is enabled, drop all packets with
790 * both the SYN and FIN bits set. This prevents e.g. nmap from
791 * identifying the TCP/IP stack.
793 * This is a violation of the TCP specification.
795 if (drop_synfin
&& (thflags
& (TH_SYN
|TH_FIN
)) == (TH_SYN
|TH_FIN
))
800 * Convert TCP protocol specific fields to host format.
803 #if BYTE_ORDER != BIG_ENDIAN
811 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
812 * until after ip6_savecontrol() is called and before other functions
813 * which don't want those proto headers.
814 * Because ip6_savecontrol() is going to parse the mbuf to
815 * search for data to be passed up to user-land, it wants mbuf
816 * parameters to be unchanged.
818 drop_hdrlen
= off0
+ off
;
821 * Locate pcb for segment.
824 #if IPFIREWALL_FORWARD
827 && isipv6
== 0 /* IPv6 support is not yet */
831 * Diverted. Pretend to be the destination.
832 * already got one like this?
834 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
835 ip
->ip_dst
, th
->th_dport
, 0, m
->m_pkthdr
.rcvif
);
838 * No, then it's new. Try find the ambushing socket
840 if (!next_hop
->sin_port
) {
841 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
,
842 th
->th_sport
, next_hop
->sin_addr
,
843 th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
845 inp
= in_pcblookup_hash(&tcbinfo
,
846 ip
->ip_src
, th
->th_sport
,
848 ntohs(next_hop
->sin_port
), 1,
853 #endif /* IPFIREWALL_FORWARD */
857 inp
= in6_pcblookup_hash(&tcbinfo
, &ip6
->ip6_src
, th
->th_sport
,
858 &ip6
->ip6_dst
, th
->th_dport
, 1,
862 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
863 ip
->ip_dst
, th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
867 * Use the interface scope information from the PCB for outbound
868 * segments. If the PCB isn't present and if scoped routing is
869 * enabled, tcp_respond will use the scope of the interface where
870 * the segment arrived on.
872 if (inp
!= NULL
&& (inp
->inp_flags
& INP_BOUND_IF
))
873 ifscope
= inp
->inp_boundif
;
875 if (ipsec_bypass
== 0) {
878 if (inp
!= NULL
&& ipsec6_in_reject_so(m
, inp
->inp_socket
)) {
879 IPSEC_STAT_INCREMENT(ipsec6stat
.in_polvio
);
880 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 0) == WNT_STOPUSING
)
881 inp
= NULL
; // pretend we didn't find it
886 if (inp
!= NULL
&& ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
887 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
888 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 0) == WNT_STOPUSING
)
889 inp
= NULL
; // pretend we didn't find it
896 * If the state is CLOSED (i.e., TCB does not exist) then
897 * all data in the incoming segment is discarded.
898 * If the TCB exists but is in CLOSED state, it is embryonic,
899 * but should either do a listen or a connect soon.
904 char dbuf
[MAX_IPv6_STR_LEN
], sbuf
[MAX_IPv6_STR_LEN
];
906 char dbuf
[MAX_IPv4_STR_LEN
], sbuf
[MAX_IPv4_STR_LEN
];
911 inet_ntop(AF_INET6
, &ip6
->ip6_dst
, dbuf
, sizeof(dbuf
));
912 inet_ntop(AF_INET6
, &ip6
->ip6_src
, sbuf
, sizeof(sbuf
));
916 inet_ntop(AF_INET
, &ip
->ip_dst
, dbuf
, sizeof(dbuf
));
917 inet_ntop(AF_INET
, &ip
->ip_src
, sbuf
, sizeof(sbuf
));
919 switch (log_in_vain
) {
923 "Connection attempt to TCP %s:%d from %s:%d\n",
924 dbuf
, ntohs(th
->th_dport
),
926 ntohs(th
->th_sport
));
930 "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
931 dbuf
, ntohs(th
->th_dport
), sbuf
,
932 ntohs(th
->th_sport
), thflags
);
935 if ((thflags
& TH_SYN
) &&
936 !(m
->m_flags
& (M_BCAST
| M_MCAST
)) &&
938 ((isipv6
&& !IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
, &ip6
->ip6_src
)) ||
939 (!isipv6
&& ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
))
941 ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
944 log_in_vain_log((LOG_INFO
,
945 "Stealth Mode connection attempt to TCP %s:%d from %s:%d\n",
946 dbuf
, ntohs(th
->th_dport
),
948 ntohs(th
->th_sport
)));
955 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
)
958 if (thflags
& TH_SYN
)
967 rstreason
= BANDLIM_RST_CLOSEDPORT
;
968 goto dropwithresetnosock
;
970 so
= inp
->inp_socket
;
972 /* This case shouldn't happen as the socket shouldn't be null
973 * if inp_state isn't set to INPCB_STATE_DEAD
974 * But just in case, we pretend we didn't find the socket if we hit this case
975 * as this isn't cause for a panic (the socket might be leaked however)...
979 printf("tcp_input: no more socket for inp=%x. This shouldn't happen\n", inp
);
984 tcp_lock(so
, 1, (void *)2);
985 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
986 tcp_unlock(so
, 1, (void *)2);
987 inp
= NULL
; // pretend we didn't find it
993 rstreason
= BANDLIM_RST_CLOSEDPORT
;
996 if (tp
->t_state
== TCPS_CLOSED
)
999 /* Unscale the window into a 32-bit value. */
1000 if ((thflags
& TH_SYN
) == 0)
1001 tiwin
= th
->th_win
<< tp
->snd_scale
;
1006 if (mac_inpcb_check_deliver(inp
, m
, AF_INET
, SOCK_STREAM
))
1010 if (so
->so_options
& (SO_DEBUG
|SO_ACCEPTCONN
)) {
1012 if (so
->so_options
& SO_DEBUG
) {
1013 ostate
= tp
->t_state
;
1016 bcopy((char *)ip6
, (char *)tcp_saveipgen
,
1020 bcopy((char *)ip
, (char *)tcp_saveipgen
, sizeof(*ip
));
1024 if (so
->so_options
& SO_ACCEPTCONN
) {
1025 register struct tcpcb
*tp0
= tp
;
1028 struct sockaddr_storage from
;
1030 struct inpcb
*oinp
= sotoinpcb(so
);
1032 unsigned int head_ifscope
;
1034 /* Get listener's bound-to-interface, if any */
1035 head_ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
1036 inp
->inp_boundif
: IFSCOPE_NONE
;
1039 * If the state is LISTEN then ignore segment if it contains an RST.
1040 * If the segment contains an ACK then it is bad and send a RST.
1041 * If it does not contain a SYN then it is not interesting; drop it.
1042 * If it is from this socket, drop it, it must be forged.
1044 if ((thflags
& (TH_RST
|TH_ACK
|TH_SYN
)) != TH_SYN
) {
1045 if (thflags
& TH_RST
) {
1048 if (thflags
& TH_ACK
) {
1050 tcpstat
.tcps_badsyn
++;
1051 rstreason
= BANDLIM_RST_OPENPORT
;
1055 /* We come here if there is no SYN set */
1056 tcpstat
.tcps_badsyn
++;
1059 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_START
,0,0,0,0,0);
1060 if (th
->th_dport
== th
->th_sport
) {
1063 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
1068 if (ip
->ip_dst
.s_addr
== ip
->ip_src
.s_addr
)
1072 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
1073 * in_broadcast() should never return true on a received
1074 * packet with M_BCAST not set.
1076 * Packets with a multicast source address should also
1079 if (m
->m_flags
& (M_BCAST
|M_MCAST
))
1083 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
1084 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
))
1088 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
1089 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
1090 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
1091 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
))
1097 * If deprecated address is forbidden,
1098 * we do not accept SYN to deprecated interface
1099 * address to prevent any new inbound connection from
1100 * getting established.
1101 * When we do not accept SYN, we send a TCP RST,
1102 * with deprecated source address (instead of dropping
1103 * it). We compromise it as it is much better for peer
1104 * to send a RST, and RST will be the final packet
1107 * If we do not forbid deprecated addresses, we accept
1108 * the SYN packet. RFC2462 does not suggest dropping
1110 * If we decipher RFC2462 5.5.4, it says like this:
1111 * 1. use of deprecated addr with existing
1112 * communication is okay - "SHOULD continue to be
1114 * 2. use of it with new communication:
1115 * (2a) "SHOULD NOT be used if alternate address
1116 * with sufficient scope is available"
1117 * (2b) nothing mentioned otherwise.
1118 * Here we fall into (2b) case as we have no choice in
1119 * our source address selection - we must obey the peer.
1121 * The wording in RFC2462 is confusing, and there are
1122 * multiple description text for deprecated address
1123 * handling - worse, they are not exactly the same.
1124 * I believe 5.5.4 is the best one, so we follow 5.5.4.
1126 if (isipv6
&& !ip6_use_deprecated
) {
1127 struct in6_ifaddr
*ia6
;
1129 if ((ia6
= ip6_getdstifaddr(m
)) &&
1130 (ia6
->ia6_flags
& IN6_IFF_DEPRECATED
)) {
1132 rstreason
= BANDLIM_RST_OPENPORT
;
1140 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&from
;
1142 sin6
->sin6_len
= sizeof(*sin6
);
1143 sin6
->sin6_family
= AF_INET6
;
1144 sin6
->sin6_port
= th
->th_sport
;
1145 sin6
->sin6_flowinfo
= 0;
1146 sin6
->sin6_addr
= ip6
->ip6_src
;
1147 sin6
->sin6_scope_id
= 0;
1152 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&from
;
1154 sin
->sin_len
= sizeof(*sin
);
1155 sin
->sin_family
= AF_INET
;
1156 sin
->sin_port
= th
->th_sport
;
1157 sin
->sin_addr
= ip
->ip_src
;
1159 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
1161 so2
= sonewconn(so
, 0, NULL
);
1164 tcpstat
.tcps_listendrop
++;
1165 if (tcp_dropdropablreq(so
)) {
1167 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
1169 so2
= sonewconn(so
, 0, NULL
);
1175 /* Point "inp" and "tp" in tandem to new socket */
1176 inp
= (struct inpcb
*)so2
->so_pcb
;
1177 tp
= intotcpcb(inp
);
1180 tcp_unlock(so
, 0, 0); /* Unlock but keep a reference on listener for now */
1185 * Mark socket as temporary until we're
1186 * committed to keeping it. The code at
1187 * ``drop'' and ``dropwithreset'' check the
1188 * flag dropsocket to see if the temporary
1189 * socket created here should be discarded.
1190 * We mark the socket as discardable until
1191 * we're committed to it below in TCPS_LISTEN.
1192 * There are some error conditions in which we
1193 * have to drop the temporary socket.
1197 * Inherit INP_BOUND_IF from listener; testing if
1198 * head_ifscope is non-zero is sufficient, since it
1199 * can only be set to a non-zero value earlier if
1200 * the listener has such a flag set.
1203 if (head_ifscope
!= IFSCOPE_NONE
&& !isipv6
) {
1205 if (head_ifscope
!= IFSCOPE_NONE
) {
1207 inp
->inp_flags
|= INP_BOUND_IF
;
1208 inp
->inp_boundif
= head_ifscope
;
1212 inp
->in6p_laddr
= ip6
->ip6_dst
;
1214 inp
->inp_vflag
&= ~INP_IPV6
;
1215 inp
->inp_vflag
|= INP_IPV4
;
1217 inp
->inp_laddr
= ip
->ip_dst
;
1221 inp
->inp_lport
= th
->th_dport
;
1222 if (in_pcbinshash(inp
, 0) != 0) {
1224 * Undo the assignments above if we failed to
1225 * put the PCB on the hash lists.
1229 inp
->in6p_laddr
= in6addr_any
;
1232 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
1234 tcp_lock(oso
, 0, 0); /* release ref on parent */
1235 tcp_unlock(oso
, 1, 0);
1241 * Inherit socket options from the listening
1243 * Note that in6p_inputopts are not (even
1244 * should not be) copied, since it stores
1245 * previously received options and is used to
1246 * detect if each new option is different than
1247 * the previous one and hence should be passed
1249 * If we copied in6p_inputopts, a user would
1250 * not be able to receive options just after
1251 * calling the accept system call.
1254 oinp
->inp_flags
& INP_CONTROLOPTS
;
1255 if (oinp
->in6p_outputopts
)
1256 inp
->in6p_outputopts
=
1257 ip6_copypktopts(oinp
->in6p_outputopts
,
1261 inp
->inp_options
= ip_srcroute();
1262 tcp_lock(oso
, 0, 0);
1264 /* copy old policy into new socket's */
1265 if (sotoinpcb(oso
)->inp_sp
)
1268 /* Is it a security hole here to silently fail to copy the policy? */
1269 if (inp
->inp_sp
!= NULL
)
1270 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
1271 if (error
!= 0 || ipsec_copy_policy(sotoinpcb(oso
)->inp_sp
, inp
->inp_sp
))
1272 printf("tcp_input: could not copy policy\n");
1275 /* inherit states from the listener */
1276 tp
->t_state
= TCPS_LISTEN
;
1277 tp
->t_flags
|= tp0
->t_flags
& (TF_NOPUSH
|TF_NOOPT
|TF_NODELAY
);
1278 tp
->t_keepinit
= tp0
->t_keepinit
;
1279 tp
->t_inpcb
->inp_ip_ttl
= tp0
->t_inpcb
->inp_ip_ttl
;
1281 /* now drop the reference on the listener */
1282 tcp_unlock(oso
, 1, 0);
1284 /* Compute proper scaling value from buffer space */
1285 if (inp
->inp_pcbinfo
->ipi_count
< tcp_sockthreshold
) {
1286 tp
->request_r_scale
= max(tcp_win_scale
, tp
->request_r_scale
);
1287 so
->so_rcv
.sb_hiwat
= imin(TCP_MAXWIN
<< tp
->request_r_scale
, (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
);
1290 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
1291 TCP_MAXWIN
<< tp
->request_r_scale
<
1292 so
->so_rcv
.sb_hiwat
)
1293 tp
->request_r_scale
++;
1296 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_END
,0,0,0,0,0);
1301 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1305 * This is the second part of the MSS DoS prevention code (after
1306 * minmss on the sending side) and it deals with too many too small
1307 * tcp packets in a too short timeframe (1 second).
1309 * For every full second we count the number of received packets
1310 * and bytes. If we get a lot of packets per second for this connection
1311 * (tcp_minmssoverload) we take a closer look at it and compute the
1312 * average packet size for the past second. If that is less than
1313 * tcp_minmss we get too many packets with very small payload which
1314 * is not good and burdens our system (and every packet generates
1315 * a wakeup to the process connected to our socket). We can reasonable
1316 * expect this to be small packet DoS attack to exhaust our CPU
1319 * Care has to be taken for the minimum packet overload value. This
1320 * value defines the minimum number of packets per second before we
1321 * start to worry. This must not be too low to avoid killing for
1322 * example interactive connections with many small packets like
1325 * Setting either tcp_minmssoverload or tcp_minmss to "0" disables
1328 * Account for packet if payload packet, skip over ACK, etc.
1330 * The packet per second count is done all the time and is also used
1331 * by "DELAY_ACK" to detect streaming situations.
1334 if (tp
->t_state
== TCPS_ESTABLISHED
&& tlen
> 0) {
1335 if (tp
->rcv_reset
> tcp_now
) {
1337 tp
->rcv_byps
+= tlen
+ off
;
1338 if (tp
->rcv_byps
> tp
->rcv_maxbyps
)
1339 tp
->rcv_maxbyps
= tp
->rcv_byps
;
1341 * Setting either tcp_minmssoverload or tcp_minmss to "0" disables
1344 if (tcp_minmss
&& tcp_minmssoverload
&& tp
->rcv_pps
> tcp_minmssoverload
) {
1345 if ((tp
->rcv_byps
/ tp
->rcv_pps
) < tcp_minmss
) {
1346 char ipstrbuf
[MAX_IPv6_STR_LEN
];
1347 printf("too many small tcp packets from "
1348 "%s:%u, av. %ubyte/packet, "
1349 "dropping connection\n",
1352 inet_ntop(AF_INET6
, &inp
->in6p_faddr
, ipstrbuf
,
1355 inet_ntop(AF_INET
, &inp
->inp_faddr
, ipstrbuf
,
1358 tp
->rcv_byps
/ tp
->rcv_pps
);
1359 tp
= tcp_drop(tp
, ECONNRESET
);
1360 /* tcpstat.tcps_minmssdrops++; */
1365 tp
->rcv_reset
= tcp_now
+ TCP_RETRANSHZ
;
1367 tp
->rcv_byps
= tlen
+ off
;
1372 if (so
->so_traffic_mgt_flags
& TRAFFIC_MGT_SO_BACKGROUND
) {
1373 tcpstat
.tcps_bg_rcvtotal
++;
1375 /* Take snapshots of pkts recv;
1376 * tcpcb should have been initialized to 0 when allocated,
1377 * so if 0 then this is the first time we're doing this
1379 if (!tp
->tot_recv_snapshot
) {
1380 tp
->tot_recv_snapshot
= tcpstat
.tcps_rcvtotal
;
1382 if (!tp
->bg_recv_snapshot
) {
1383 tp
->bg_recv_snapshot
= tcpstat
.tcps_bg_rcvtotal
;
1386 #endif /* TRAFFIC_MGT */
1389 Explicit Congestion Notification - Flag that we need to send ECT if
1390 + The IP Congestion experienced flag was set.
1391 + Socket is in established state
1392 + We negotiated ECN in the TCP setup
1393 + This isn't a pure ack (tlen > 0)
1394 + The data is in the valid window
1396 TE_SENDECE will be cleared when we receive a packet with TH_CWR set.
1398 if (ip_ecn
== IPTOS_ECN_CE
&& tp
->t_state
== TCPS_ESTABLISHED
&&
1399 (tp
->ecn_flags
& (TE_SETUPSENT
| TE_SETUPRECEIVED
)) ==
1400 (TE_SETUPSENT
| TE_SETUPRECEIVED
) && tlen
> 0 &&
1401 SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
1402 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) {
1403 tp
->ecn_flags
|= TE_SENDECE
;
1407 Clear TE_SENDECE if TH_CWR is set. This is harmless, so we don't
1408 bother doing extensive checks for state and whatnot.
1410 if ((thflags
& TH_CWR
) == TH_CWR
) {
1411 tp
->ecn_flags
&= ~TE_SENDECE
;
1415 * Segment received on connection.
1416 * Reset idle time and keep-alive timer.
1419 if (TCPS_HAVEESTABLISHED(tp
->t_state
))
1420 tp
->t_timer
[TCPT_KEEP
] = TCP_KEEPIDLE(tp
);
1423 * Process options if not in LISTEN state,
1424 * else do it below (after getting remote address).
1426 if (tp
->t_state
!= TCPS_LISTEN
&& optp
)
1427 tcp_dooptions(tp
, optp
, optlen
, th
, &to
, ifscope
);
1429 if (tp
->t_state
== TCPS_SYN_SENT
&& (thflags
& TH_SYN
)) {
1430 if (to
.to_flags
& TOF_SCALE
) {
1431 tp
->t_flags
|= TF_RCVD_SCALE
;
1432 tp
->requested_s_scale
= to
.to_requested_s_scale
;
1433 tp
->snd_wnd
= th
->th_win
<< tp
->snd_scale
;
1434 tiwin
= tp
->snd_wnd
;
1436 if (to
.to_flags
& TOF_TS
) {
1437 tp
->t_flags
|= TF_RCVD_TSTMP
;
1438 tp
->ts_recent
= to
.to_tsval
;
1439 tp
->ts_recent_age
= tcp_now
;
1441 if (to
.to_flags
& TOF_MSS
)
1442 tcp_mss(tp
, to
.to_mss
, ifscope
);
1443 if (tp
->sack_enable
) {
1444 if (!(to
.to_flags
& TOF_SACK
))
1445 tp
->sack_enable
= 0;
1447 tp
->t_flags
|= TF_SACK_PERMIT
;
1452 * Header prediction: check for the two common cases
1453 * of a uni-directional data xfer. If the packet has
1454 * no control flags, is in-sequence, the window didn't
1455 * change and we're not retransmitting, it's a
1456 * candidate. If the length is zero and the ack moved
1457 * forward, we're the sender side of the xfer. Just
1458 * free the data acked & wake any higher level process
1459 * that was blocked waiting for space. If the length
1460 * is non-zero and the ack didn't move, we're the
1461 * receiver side. If we're getting packets in-order
1462 * (the reassembly queue is empty), add the data to
1463 * the socket buffer and note that we need a delayed ack.
1464 * Make sure that the hidden state-flags are also off.
1465 * Since we check for TCPS_ESTABLISHED above, it can only
1468 if (tp
->t_state
== TCPS_ESTABLISHED
&&
1469 (thflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
|TH_ECE
)) == TH_ACK
&&
1470 ((tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
)) == 0) &&
1471 ((to
.to_flags
& TOF_TS
) == 0 ||
1472 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
1473 th
->th_seq
== tp
->rcv_nxt
&&
1474 tiwin
&& tiwin
== tp
->snd_wnd
&&
1475 tp
->snd_nxt
== tp
->snd_max
) {
1478 * If last ACK falls within this segment's sequence numbers,
1479 * record the timestamp.
1480 * NOTE that the test is modified according to the latest
1481 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1483 if ((to
.to_flags
& TOF_TS
) != 0 &&
1484 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
1485 tp
->ts_recent_age
= tcp_now
;
1486 tp
->ts_recent
= to
.to_tsval
;
1489 /* Force acknowledgment if we received a FIN */
1491 if (thflags
& TH_FIN
)
1492 tp
->t_flags
|= TF_ACKNOW
;
1495 if (SEQ_GT(th
->th_ack
, tp
->snd_una
) &&
1496 SEQ_LEQ(th
->th_ack
, tp
->snd_max
) &&
1497 tp
->snd_cwnd
>= tp
->snd_ssthresh
&&
1498 ((!tcp_do_newreno
&& !tp
->sack_enable
&&
1499 tp
->t_dupacks
< tcprexmtthresh
) ||
1500 ((tcp_do_newreno
|| tp
->sack_enable
) &&
1501 !IN_FASTRECOVERY(tp
) && to
.to_nsacks
== 0 &&
1502 TAILQ_EMPTY(&tp
->snd_holes
)))) {
1504 * this is a pure ack for outstanding data.
1506 ++tcpstat
.tcps_predack
;
1508 * "bad retransmit" recovery
1510 if (tp
->t_rxtshift
== 1 &&
1511 tcp_now
< tp
->t_badrxtwin
) {
1512 ++tcpstat
.tcps_sndrexmitbad
;
1513 tp
->snd_cwnd
= tp
->snd_cwnd_prev
;
1515 tp
->snd_ssthresh_prev
;
1516 tp
->snd_recover
= tp
->snd_recover_prev
;
1517 if (tp
->t_flags
& TF_WASFRECOVERY
)
1518 ENTER_FASTRECOVERY(tp
);
1519 tp
->snd_nxt
= tp
->snd_max
;
1520 tp
->t_badrxtwin
= 0;
1523 * Recalculate the transmit timer / rtt.
1525 * Some boxes send broken timestamp replies
1526 * during the SYN+ACK phase, ignore
1527 * timestamps of 0 or we could calculate a
1528 * huge RTT and blow up the retransmit timer.
1530 if (((to
.to_flags
& TOF_TS
) != 0) && (to
.to_tsecr
!= 0)) { /* Makes sure we already have a TS */
1531 if (!tp
->t_rttlow
||
1532 tp
->t_rttlow
> tcp_now
- to
.to_tsecr
)
1533 tp
->t_rttlow
= tcp_now
- to
.to_tsecr
;
1535 tcp_now
- to
.to_tsecr
);
1536 } else if (tp
->t_rtttime
&&
1537 SEQ_GT(th
->th_ack
, tp
->t_rtseq
)) {
1538 if (!tp
->t_rttlow
||
1539 tp
->t_rttlow
> tcp_now
- tp
->t_rtttime
)
1540 tp
->t_rttlow
= tcp_now
- tp
->t_rtttime
;
1541 tcp_xmit_timer(tp
, tp
->t_rtttime
);
1543 acked
= th
->th_ack
- tp
->snd_una
;
1544 tcpstat
.tcps_rcvackpack
++;
1545 tcpstat
.tcps_rcvackbyte
+= acked
;
1547 * Grow the congestion window, if the
1548 * connection is cwnd bound.
1550 if (tp
->snd_cwnd
< tp
->snd_wnd
) {
1551 tp
->t_bytes_acked
+= acked
;
1552 if (tp
->t_bytes_acked
> tp
->snd_cwnd
) {
1553 tp
->t_bytes_acked
-= tp
->snd_cwnd
;
1554 tp
->snd_cwnd
+= tp
->t_maxseg
;
1557 sbdrop(&so
->so_snd
, acked
);
1558 if (SEQ_GT(tp
->snd_una
, tp
->snd_recover
) &&
1559 SEQ_LEQ(th
->th_ack
, tp
->snd_recover
))
1560 tp
->snd_recover
= th
->th_ack
- 1;
1561 tp
->snd_una
= th
->th_ack
;
1563 * pull snd_wl2 up to prevent seq wrap relative
1566 tp
->snd_wl2
= th
->th_ack
;
1569 ND6_HINT(tp
); /* some progress has been done */
1572 * If all outstanding data are acked, stop
1573 * retransmit timer, otherwise restart timer
1574 * using current (possibly backed-off) value.
1575 * If process is waiting for space,
1576 * wakeup/selwakeup/signal. If data
1577 * are ready to send, let tcp_output
1578 * decide between more output or persist.
1580 if (tp
->snd_una
== tp
->snd_max
)
1581 tp
->t_timer
[TCPT_REXMT
] = 0;
1582 else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
1583 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
1585 sowwakeup(so
); /* has to be done with socket lock held */
1586 if ((so
->so_snd
.sb_cc
) || (tp
->t_flags
& TF_ACKNOW
)) {
1587 tp
->t_unacksegs
= 0;
1588 (void) tcp_output(tp
);
1590 tcp_unlock(so
, 1, 0);
1591 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
1594 } else if (th
->th_ack
== tp
->snd_una
&&
1595 LIST_EMPTY(&tp
->t_segq
) &&
1596 tlen
<= tcp_sbspace(tp
)) {
1598 * this is a pure, in-sequence data packet
1599 * with nothing on the reassembly queue and
1600 * we have enough buffer space to take it.
1602 /* Clean receiver SACK report if present */
1603 if (tp
->sack_enable
&& tp
->rcv_numsacks
)
1604 tcp_clean_sackreport(tp
);
1605 ++tcpstat
.tcps_preddat
;
1606 tp
->rcv_nxt
+= tlen
;
1608 * Pull snd_wl1 up to prevent seq wrap relative to
1611 tp
->snd_wl1
= th
->th_seq
;
1613 * Pull rcv_up up to prevent seq wrap relative to
1616 tp
->rcv_up
= tp
->rcv_nxt
;
1617 tcpstat
.tcps_rcvpack
++;
1618 tcpstat
.tcps_rcvbyte
+= tlen
;
1619 ND6_HINT(tp
); /* some progress has been done */
1621 * Add data to socket buffer.
1623 m_adj(m
, drop_hdrlen
); /* delayed header drop */
1624 if (sbappendstream(&so
->so_rcv
, m
))
1628 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
1629 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
1630 th
->th_seq
, th
->th_ack
, th
->th_win
);
1635 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
1636 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
1637 th
->th_seq
, th
->th_ack
, th
->th_win
);
1639 if (DELAY_ACK(tp
)) {
1640 tp
->t_flags
|= TF_DELACK
;
1643 tp
->t_unacksegs
= 0;
1644 tp
->t_flags
|= TF_ACKNOW
;
1647 tcp_unlock(so
, 1, 0);
1648 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
1654 * Calculate amount of space in receive window,
1655 * and then do TCP input processing.
1656 * Receive window is amount of space in rcv queue,
1657 * but not less than advertised window.
1660 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1664 win
= tcp_sbspace(tp
);
1668 else { /* clip rcv window to 4K for modems */
1669 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0)
1670 win
= min(win
, slowlink_wsize
);
1672 tp
->rcv_wnd
= imax(win
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
1675 switch (tp
->t_state
) {
1678 * Initialize tp->rcv_nxt, and tp->irs, select an initial
1679 * tp->iss, and send a segment:
1680 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1681 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1682 * Fill in remote peer address fields if not previously specified.
1683 * Enter SYN_RECEIVED state, and process any other fields of this
1684 * segment in this state.
1687 register struct sockaddr_in
*sin
;
1689 register struct sockaddr_in6
*sin6
;
1693 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1697 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
,
1698 M_SONAME
, M_NOWAIT
);
1701 bzero(sin6
, sizeof(*sin6
));
1702 sin6
->sin6_family
= AF_INET6
;
1703 sin6
->sin6_len
= sizeof(*sin6
);
1704 sin6
->sin6_addr
= ip6
->ip6_src
;
1705 sin6
->sin6_port
= th
->th_sport
;
1706 laddr6
= inp
->in6p_laddr
;
1707 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1708 inp
->in6p_laddr
= ip6
->ip6_dst
;
1709 if (in6_pcbconnect(inp
, (struct sockaddr
*)sin6
,
1711 inp
->in6p_laddr
= laddr6
;
1712 FREE(sin6
, M_SONAME
);
1715 FREE(sin6
, M_SONAME
);
1720 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1722 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
,
1726 sin
->sin_family
= AF_INET
;
1727 sin
->sin_len
= sizeof(*sin
);
1728 sin
->sin_addr
= ip
->ip_src
;
1729 sin
->sin_port
= th
->th_sport
;
1730 bzero((caddr_t
)sin
->sin_zero
, sizeof(sin
->sin_zero
));
1731 laddr
= inp
->inp_laddr
;
1732 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
1733 inp
->inp_laddr
= ip
->ip_dst
;
1734 if (in_pcbconnect(inp
, (struct sockaddr
*)sin
, proc0
)) {
1735 inp
->inp_laddr
= laddr
;
1736 FREE(sin
, M_SONAME
);
1739 FREE(sin
, M_SONAME
);
1742 tcp_dooptions(tp
, optp
, optlen
, th
, &to
, ifscope
);
1744 if (tp
->sack_enable
) {
1745 if (!(to
.to_flags
& TOF_SACK
))
1746 tp
->sack_enable
= 0;
1748 tp
->t_flags
|= TF_SACK_PERMIT
;
1754 tp
->iss
= tcp_new_isn(tp
);
1756 tp
->irs
= th
->th_seq
;
1757 tcp_sendseqinit(tp
);
1759 tp
->snd_recover
= tp
->snd_una
;
1761 * Initialization of the tcpcb for transaction;
1762 * set SND.WND = SEG.WND,
1763 * initialize CCsend and CCrecv.
1765 tp
->snd_wnd
= tiwin
; /* initial send-window */
1766 tp
->t_flags
|= TF_ACKNOW
;
1767 tp
->t_unacksegs
= 0;
1768 tp
->t_state
= TCPS_SYN_RECEIVED
;
1769 tp
->t_timer
[TCPT_KEEP
] = tp
->t_keepinit
? tp
->t_keepinit
: tcp_keepinit
;
1770 dropsocket
= 0; /* committed to socket */
1771 tcpstat
.tcps_accepts
++;
1772 if ((thflags
& (TH_ECE
| TH_CWR
)) == (TH_ECE
| TH_CWR
)) {
1774 tp
->ecn_flags
|= (TE_SETUPRECEIVED
| TE_SENDIPECT
);
1776 #if CONFIG_IFEF_NOWINDOWSCALE
1777 if (tcp_obey_ifef_nowindowscale
&& m
->m_pkthdr
.rcvif
!= NULL
&&
1778 (m
->m_pkthdr
.rcvif
->if_eflags
& IFEF_NOWINDOWSCALE
)) {
1779 /* Window scaling is not enabled on this interface */
1780 tp
->t_flags
&= ~TF_REQ_SCALE
;
1787 * If the state is SYN_RECEIVED:
1788 * if seg contains an ACK, but not for our SYN/ACK, send a RST.
1790 case TCPS_SYN_RECEIVED
:
1791 if ((thflags
& TH_ACK
) &&
1792 (SEQ_LEQ(th
->th_ack
, tp
->snd_una
) ||
1793 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
1794 rstreason
= BANDLIM_RST_OPENPORT
;
1800 * If the state is SYN_SENT:
1801 * if seg contains an ACK, but not for our SYN, drop the input.
1802 * if seg contains a RST, then drop the connection.
1803 * if seg does not contain SYN, then drop it.
1804 * Otherwise this is an acceptable SYN segment
1805 * initialize tp->rcv_nxt and tp->irs
1806 * if seg contains ack then advance tp->snd_una
1807 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1808 * arrange for segment to be acked (eventually)
1809 * continue processing rest of data/controls, beginning with URG
1812 if ((thflags
& TH_ACK
) &&
1813 (SEQ_LEQ(th
->th_ack
, tp
->iss
) ||
1814 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
1815 rstreason
= BANDLIM_UNLIMITED
;
1818 if (thflags
& TH_RST
) {
1819 if ((thflags
& TH_ACK
) != 0) {
1820 tp
= tcp_drop(tp
, ECONNREFUSED
);
1821 postevent(so
, 0, EV_RESET
);
1825 if ((thflags
& TH_SYN
) == 0)
1827 tp
->snd_wnd
= th
->th_win
; /* initial send window */
1829 tp
->irs
= th
->th_seq
;
1831 if (thflags
& TH_ACK
) {
1832 tcpstat
.tcps_connects
++;
1834 if ((thflags
& (TH_ECE
| TH_CWR
)) == (TH_ECE
)) {
1835 /* ECN-setup SYN-ACK */
1836 tp
->ecn_flags
|= TE_SETUPRECEIVED
;
1839 /* non-ECN-setup SYN-ACK */
1840 tp
->ecn_flags
&= ~TE_SENDIPECT
;
1843 #if CONFIG_MACF_NET && CONFIG_MACF_SOCKET
1844 /* XXXMAC: recursive lock: SOCK_LOCK(so); */
1845 mac_socketpeer_label_associate_mbuf(m
, so
);
1846 /* XXXMAC: SOCK_UNLOCK(so); */
1848 /* Do window scaling on this connection? */
1849 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
1850 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
1851 tp
->snd_scale
= tp
->requested_s_scale
;
1852 tp
->rcv_scale
= tp
->request_r_scale
;
1854 tp
->rcv_adv
+= tp
->rcv_wnd
;
1855 tp
->snd_una
++; /* SYN is acked */
1857 * If there's data, delay ACK; if there's also a FIN
1858 * ACKNOW will be turned on later.
1860 if (DELAY_ACK(tp
) && tlen
!= 0) {
1861 tp
->t_flags
|= TF_DELACK
;
1865 tp
->t_flags
|= TF_ACKNOW
;
1866 tp
->t_unacksegs
= 0;
1869 * Received <SYN,ACK> in SYN_SENT[*] state.
1871 * SYN_SENT --> ESTABLISHED
1872 * SYN_SENT* --> FIN_WAIT_1
1874 tp
->t_starttime
= 0;
1875 if (tp
->t_flags
& TF_NEEDFIN
) {
1876 tp
->t_state
= TCPS_FIN_WAIT_1
;
1877 tp
->t_flags
&= ~TF_NEEDFIN
;
1880 tp
->t_state
= TCPS_ESTABLISHED
;
1881 tp
->t_timer
[TCPT_KEEP
] = TCP_KEEPIDLE(tp
);
1883 /* soisconnected may lead to socket_unlock in case of upcalls,
1884 * make sure this is done when everything is setup.
1889 * Received initial SYN in SYN-SENT[*] state => simul-
1890 * taneous open. If segment contains CC option and there is
1891 * a cached CC, apply TAO test; if it succeeds, connection is
1892 * half-synchronized. Otherwise, do 3-way handshake:
1893 * SYN-SENT -> SYN-RECEIVED
1894 * SYN-SENT* -> SYN-RECEIVED*
1896 tp
->t_flags
|= TF_ACKNOW
;
1897 tp
->t_timer
[TCPT_REXMT
] = 0;
1898 tp
->t_state
= TCPS_SYN_RECEIVED
;
1904 * Advance th->th_seq to correspond to first data byte.
1905 * If data, trim to stay within window,
1906 * dropping FIN if necessary.
1909 if (tlen
> tp
->rcv_wnd
) {
1910 todrop
= tlen
- tp
->rcv_wnd
;
1914 tcpstat
.tcps_rcvpackafterwin
++;
1915 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
1917 tp
->snd_wl1
= th
->th_seq
- 1;
1918 tp
->rcv_up
= th
->th_seq
;
1920 * Client side of transaction: already sent SYN and data.
1921 * If the remote host used T/TCP to validate the SYN,
1922 * our data will be ACK'd; if so, enter normal data segment
1923 * processing in the middle of step 5, ack processing.
1924 * Otherwise, goto step 6.
1926 if (thflags
& TH_ACK
)
1930 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1931 * do normal processing.
1933 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later.
1937 case TCPS_TIME_WAIT
:
1938 break; /* continue normal processing */
1940 /* Received a SYN while connection is already established.
1941 * This is a "half open connection and other anomalies" described
1942 * in RFC793 page 34, send an ACK so the remote reset the connection
1943 * or recovers by adjusting its sequence numberering
1945 case TCPS_ESTABLISHED
:
1946 if (thflags
& TH_SYN
)
1952 * States other than LISTEN or SYN_SENT.
1953 * First check the RST flag and sequence number since reset segments
1954 * are exempt from the timestamp and connection count tests. This
1955 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
1956 * below which allowed reset segments in half the sequence space
1957 * to fall though and be processed (which gives forged reset
1958 * segments with a random sequence number a 50 percent chance of
1959 * killing a connection).
1960 * Then check timestamp, if present.
1961 * Then check the connection count, if present.
1962 * Then check that at least some bytes of segment are within
1963 * receive window. If segment begins before rcv_nxt,
1964 * drop leading data (and SYN); if nothing left, just ack.
1967 * If the RST bit is set, check the sequence number to see
1968 * if this is a valid reset segment.
1970 * In all states except SYN-SENT, all reset (RST) segments
1971 * are validated by checking their SEQ-fields. A reset is
1972 * valid if its sequence number is in the window.
1973 * Note: this does not take into account delayed ACKs, so
1974 * we should test against last_ack_sent instead of rcv_nxt.
1975 * The sequence number in the reset segment is normally an
1976 * echo of our outgoing acknowlegement numbers, but some hosts
1977 * send a reset with the sequence number at the rightmost edge
1978 * of our receive window, and we have to handle this case.
1979 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
1980 * that brute force RST attacks are possible. To combat this,
1981 * we use a much stricter check while in the ESTABLISHED state,
1982 * only accepting RSTs where the sequence number is equal to
1983 * last_ack_sent. In all other states (the states in which a
1984 * RST is more likely), the more permissive check is used.
1985 * If we have multiple segments in flight, the intial reset
1986 * segment sequence numbers will be to the left of last_ack_sent,
1987 * but they will eventually catch up.
1988 * In any case, it never made sense to trim reset segments to
1989 * fit the receive window since RFC 1122 says:
1990 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
1992 * A TCP SHOULD allow a received RST segment to include data.
1995 * It has been suggested that a RST segment could contain
1996 * ASCII text that encoded and explained the cause of the
1997 * RST. No standard has yet been established for such
2000 * If the reset segment passes the sequence number test examine
2002 * SYN_RECEIVED STATE:
2003 * If passive open, return to LISTEN state.
2004 * If active open, inform user that connection was refused.
2005 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
2006 * Inform user that connection was reset, and close tcb.
2007 * CLOSING, LAST_ACK STATES:
2010 * Drop the segment - see Stevens, vol. 2, p. 964 and
2013 * Radar 4803931: Allows for the case where we ACKed the FIN but
2014 * there is already a RST in flight from the peer.
2015 * In that case, accept the RST for non-established
2016 * state if it's one off from last_ack_sent.
2019 if (thflags
& TH_RST
) {
2020 if ((SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
2021 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) ||
2022 (tp
->rcv_wnd
== 0 &&
2023 ((tp
->last_ack_sent
== th
->th_seq
) || ((tp
->last_ack_sent
-1) == th
->th_seq
)))) {
2024 switch (tp
->t_state
) {
2026 case TCPS_SYN_RECEIVED
:
2027 so
->so_error
= ECONNREFUSED
;
2030 case TCPS_ESTABLISHED
:
2031 if (tp
->last_ack_sent
!= th
->th_seq
) {
2032 tcpstat
.tcps_badrst
++;
2035 case TCPS_FIN_WAIT_1
:
2036 case TCPS_CLOSE_WAIT
:
2040 case TCPS_FIN_WAIT_2
:
2041 so
->so_error
= ECONNRESET
;
2043 postevent(so
, 0, EV_RESET
);
2044 tp
->t_state
= TCPS_CLOSED
;
2045 tcpstat
.tcps_drops
++;
2054 case TCPS_TIME_WAIT
:
2062 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
2066 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2067 * and it's less than ts_recent, drop it.
2069 if ((to
.to_flags
& TOF_TS
) != 0 && tp
->ts_recent
&&
2070 TSTMP_LT(to
.to_tsval
, tp
->ts_recent
)) {
2072 /* Check to see if ts_recent is over 24 days old. */
2073 if ((int)(tcp_now
- tp
->ts_recent_age
) > TCP_PAWS_IDLE
) {
2075 * Invalidate ts_recent. If this segment updates
2076 * ts_recent, the age will be reset later and ts_recent
2077 * will get a valid value. If it does not, setting
2078 * ts_recent to zero will at least satisfy the
2079 * requirement that zero be placed in the timestamp
2080 * echo reply when ts_recent isn't valid. The
2081 * age isn't reset until we get a valid ts_recent
2082 * because we don't want out-of-order segments to be
2083 * dropped when ts_recent is old.
2087 tcpstat
.tcps_rcvduppack
++;
2088 tcpstat
.tcps_rcvdupbyte
+= tlen
;
2089 tcpstat
.tcps_pawsdrop
++;
2097 * In the SYN-RECEIVED state, validate that the packet belongs to
2098 * this connection before trimming the data to fit the receive
2099 * window. Check the sequence number versus IRS since we know
2100 * the sequence numbers haven't wrapped. This is a partial fix
2101 * for the "LAND" DoS attack.
2103 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& SEQ_LT(th
->th_seq
, tp
->irs
)) {
2104 rstreason
= BANDLIM_RST_OPENPORT
;
2108 todrop
= tp
->rcv_nxt
- th
->th_seq
;
2110 if (thflags
& TH_SYN
) {
2120 * Following if statement from Stevens, vol. 2, p. 960.
2123 || (todrop
== tlen
&& (thflags
& TH_FIN
) == 0)) {
2125 * Any valid FIN must be to the left of the window.
2126 * At this point the FIN must be a duplicate or out
2127 * of sequence; drop it.
2132 * Send an ACK to resynchronize and drop any data.
2133 * But keep on processing for RST or ACK.
2135 tp
->t_flags
|= TF_ACKNOW
;
2136 tp
->t_unacksegs
= 0;
2138 tcpstat
.tcps_rcvduppack
++;
2139 tcpstat
.tcps_rcvdupbyte
+= todrop
;
2141 tcpstat
.tcps_rcvpartduppack
++;
2142 tcpstat
.tcps_rcvpartdupbyte
+= todrop
;
2144 drop_hdrlen
+= todrop
; /* drop from the top afterwards */
2145 th
->th_seq
+= todrop
;
2147 if (th
->th_urp
> todrop
)
2148 th
->th_urp
-= todrop
;
2156 * If new data are received on a connection after the
2157 * user processes are gone, then RST the other end.
2159 if ((so
->so_state
& SS_NOFDREF
) &&
2160 tp
->t_state
> TCPS_CLOSE_WAIT
&& tlen
) {
2162 tcpstat
.tcps_rcvafterclose
++;
2163 rstreason
= BANDLIM_UNLIMITED
;
2168 * If segment ends after window, drop trailing data
2169 * (and PUSH and FIN); if nothing left, just ACK.
2171 todrop
= (th
->th_seq
+tlen
) - (tp
->rcv_nxt
+tp
->rcv_wnd
);
2173 tcpstat
.tcps_rcvpackafterwin
++;
2174 if (todrop
>= tlen
) {
2175 tcpstat
.tcps_rcvbyteafterwin
+= tlen
;
2177 * If a new connection request is received
2178 * while in TIME_WAIT, drop the old connection
2179 * and start over if the sequence numbers
2180 * are above the previous ones.
2182 if (thflags
& TH_SYN
&&
2183 tp
->t_state
== TCPS_TIME_WAIT
&&
2184 SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
2185 iss
= tcp_new_isn(tp
);
2187 tcp_unlock(so
, 1, 0);
2191 * If window is closed can only take segments at
2192 * window edge, and have to drop data and PUSH from
2193 * incoming segments. Continue processing, but
2194 * remember to ack. Otherwise, drop segment
2197 if (tp
->rcv_wnd
== 0 && th
->th_seq
== tp
->rcv_nxt
) {
2198 tp
->t_flags
|= TF_ACKNOW
;
2199 tp
->t_unacksegs
= 0;
2200 tcpstat
.tcps_rcvwinprobe
++;
2204 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
2207 thflags
&= ~(TH_PUSH
|TH_FIN
);
2211 * If last ACK falls within this segment's sequence numbers,
2212 * record its timestamp.
2214 * 1) That the test incorporates suggestions from the latest
2215 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
2216 * 2) That updating only on newer timestamps interferes with
2217 * our earlier PAWS tests, so this check should be solely
2218 * predicated on the sequence space of this segment.
2219 * 3) That we modify the segment boundary check to be
2220 * Last.ACK.Sent <= SEG.SEQ + SEG.Len
2221 * instead of RFC1323's
2222 * Last.ACK.Sent < SEG.SEQ + SEG.Len,
2223 * This modified check allows us to overcome RFC1323's
2224 * limitations as described in Stevens TCP/IP Illustrated
2225 * Vol. 2 p.869. In such cases, we can still calculate the
2226 * RTT correctly when RCV.NXT == Last.ACK.Sent.
2228 if ((to
.to_flags
& TOF_TS
) != 0 &&
2229 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
) &&
2230 SEQ_LEQ(tp
->last_ack_sent
, th
->th_seq
+ tlen
+
2231 ((thflags
& (TH_SYN
|TH_FIN
)) != 0))) {
2232 tp
->ts_recent_age
= tcp_now
;
2233 tp
->ts_recent
= to
.to_tsval
;
2237 * If a SYN is in the window, then this is an
2238 * error and we send an RST and drop the connection.
2240 if (thflags
& TH_SYN
) {
2241 tp
= tcp_drop(tp
, ECONNRESET
);
2242 rstreason
= BANDLIM_UNLIMITED
;
2243 postevent(so
, 0, EV_RESET
);
2248 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
2249 * flag is on (half-synchronized state), then queue data for
2250 * later processing; else drop segment and return.
2252 if ((thflags
& TH_ACK
) == 0) {
2253 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
2254 (tp
->t_flags
& TF_NEEDSYN
))
2256 else if (tp
->t_flags
& TF_ACKNOW
)
2265 switch (tp
->t_state
) {
2268 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2269 * ESTABLISHED state and continue processing.
2270 * The ACK was checked above.
2272 case TCPS_SYN_RECEIVED
:
2274 tcpstat
.tcps_connects
++;
2276 /* Do window scaling? */
2277 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
2278 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
2279 tp
->snd_scale
= tp
->requested_s_scale
;
2280 tp
->rcv_scale
= tp
->request_r_scale
;
2284 * SYN-RECEIVED -> ESTABLISHED
2285 * SYN-RECEIVED* -> FIN-WAIT-1
2287 tp
->t_starttime
= 0;
2288 if (tp
->t_flags
& TF_NEEDFIN
) {
2289 tp
->t_state
= TCPS_FIN_WAIT_1
;
2290 tp
->t_flags
&= ~TF_NEEDFIN
;
2292 tp
->t_state
= TCPS_ESTABLISHED
;
2293 tp
->t_timer
[TCPT_KEEP
] = TCP_KEEPIDLE(tp
);
2296 * If segment contains data or ACK, will call tcp_reass()
2297 * later; if not, do so now to pass queued data to user.
2299 if (tlen
== 0 && (thflags
& TH_FIN
) == 0)
2300 (void) tcp_reass(tp
, (struct tcphdr
*)0, &tlen
,
2302 tp
->snd_wl1
= th
->th_seq
- 1;
2306 /* soisconnected may lead to socket_unlock in case of upcalls,
2307 * make sure this is done when everything is setup.
2312 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2313 * ACKs. If the ack is in the range
2314 * tp->snd_una < th->th_ack <= tp->snd_max
2315 * then advance tp->snd_una to th->th_ack and drop
2316 * data from the retransmission queue. If this ACK reflects
2317 * more up to date window information we update our window information.
2319 case TCPS_ESTABLISHED
:
2320 case TCPS_FIN_WAIT_1
:
2321 case TCPS_FIN_WAIT_2
:
2322 case TCPS_CLOSE_WAIT
:
2325 case TCPS_TIME_WAIT
:
2326 if (SEQ_GT(th
->th_ack
, tp
->snd_max
)) {
2327 tcpstat
.tcps_rcvacktoomuch
++;
2330 if (tp
->sack_enable
&&
2331 (to
.to_nsacks
> 0 || !TAILQ_EMPTY(&tp
->snd_holes
)))
2332 tcp_sack_doack(tp
, &to
, th
->th_ack
);
2333 if (SEQ_LEQ(th
->th_ack
, tp
->snd_una
)) {
2334 if (tlen
== 0 && tiwin
== tp
->snd_wnd
) {
2335 tcpstat
.tcps_rcvdupack
++;
2337 * If we have outstanding data (other than
2338 * a window probe), this is a completely
2339 * duplicate ack (ie, window info didn't
2340 * change), the ack is the biggest we've
2341 * seen and we've seen exactly our rexmt
2342 * threshhold of them, assume a packet
2343 * has been dropped and retransmit it.
2344 * Kludge snd_nxt & the congestion
2345 * window so we send only this one
2348 * We know we're losing at the current
2349 * window size so do congestion avoidance
2350 * (set ssthresh to half the current window
2351 * and pull our congestion window back to
2352 * the new ssthresh).
2354 * Dup acks mean that packets have left the
2355 * network (they're now cached at the receiver)
2356 * so bump cwnd by the amount in the receiver
2357 * to keep a constant cwnd packets in the
2360 if (tp
->t_timer
[TCPT_REXMT
] == 0 ||
2361 th
->th_ack
!= tp
->snd_una
)
2363 else if (++tp
->t_dupacks
> tcprexmtthresh
||
2364 ((tcp_do_newreno
|| tp
->sack_enable
) &&
2365 IN_FASTRECOVERY(tp
))) {
2366 if (tp
->sack_enable
&& IN_FASTRECOVERY(tp
)) {
2370 * Compute the amount of data in flight first.
2371 * We can inject new data into the pipe iff
2372 * we have less than 1/2 the original window's
2373 * worth of data in flight.
2375 awnd
= (tp
->snd_nxt
- tp
->snd_fack
) +
2376 tp
->sackhint
.sack_bytes_rexmit
;
2377 if (awnd
< tp
->snd_ssthresh
) {
2378 tp
->snd_cwnd
+= tp
->t_maxseg
;
2379 if (tp
->snd_cwnd
> tp
->snd_ssthresh
)
2380 tp
->snd_cwnd
= tp
->snd_ssthresh
;
2383 tp
->snd_cwnd
+= tp
->t_maxseg
;
2384 tp
->t_unacksegs
= 0;
2385 (void) tcp_output(tp
);
2387 } else if (tp
->t_dupacks
== tcprexmtthresh
) {
2388 tcp_seq onxt
= tp
->snd_nxt
;
2392 * If we're doing sack, check to
2393 * see if we're already in sack
2394 * recovery. If we're not doing sack,
2395 * check to see if we're in newreno
2398 if (tp
->sack_enable
) {
2399 if (IN_FASTRECOVERY(tp
)) {
2403 } else if (tcp_do_newreno
) {
2404 if (SEQ_LEQ(th
->th_ack
,
2410 win
= min(tp
->snd_wnd
, tp
->snd_cwnd
) /
2414 tp
->snd_ssthresh
= win
* tp
->t_maxseg
;
2415 ENTER_FASTRECOVERY(tp
);
2416 tp
->snd_recover
= tp
->snd_max
;
2417 tp
->t_timer
[TCPT_REXMT
] = 0;
2419 tp
->ecn_flags
|= TE_SENDCWR
;
2420 if (tp
->sack_enable
) {
2421 tcpstat
.tcps_sack_recovery_episode
++;
2422 tp
->sack_newdata
= tp
->snd_nxt
;
2423 tp
->snd_cwnd
= tp
->t_maxseg
;
2424 tp
->t_unacksegs
= 0;
2425 (void) tcp_output(tp
);
2428 tp
->snd_nxt
= th
->th_ack
;
2429 tp
->snd_cwnd
= tp
->t_maxseg
;
2430 tp
->t_unacksegs
= 0;
2431 (void) tcp_output(tp
);
2432 tp
->snd_cwnd
= tp
->snd_ssthresh
+
2433 tp
->t_maxseg
* tp
->t_dupacks
;
2434 if (SEQ_GT(onxt
, tp
->snd_nxt
))
2443 * If the congestion window was inflated to account
2444 * for the other side's cached packets, retract it.
2446 if (!IN_FASTRECOVERY(tp
)) {
2448 * We were not in fast recovery. Reset the duplicate ack
2454 * If the congestion window was inflated to account
2455 * for the other side's cached packets, retract it.
2458 if (tcp_do_newreno
|| tp
->sack_enable
) {
2459 if (SEQ_LT(th
->th_ack
, tp
->snd_recover
)) {
2460 if (tp
->sack_enable
)
2461 tcp_sack_partialack(tp
, th
);
2463 tcp_newreno_partial_ack(tp
, th
);
2466 if (tcp_do_newreno
) {
2467 int32_t ss
= tp
->snd_max
- th
->th_ack
;
2470 * Complete ack. Inflate the congestion window to
2471 * ssthresh and exit fast recovery.
2473 * Window inflation should have left us with approx.
2474 * snd_ssthresh outstanding data. But in case we
2475 * would be inclined to send a burst, better to do
2476 * it via the slow start mechanism.
2478 if (ss
< tp
->snd_ssthresh
)
2479 tp
->snd_cwnd
= ss
+ tp
->t_maxseg
;
2481 tp
->snd_cwnd
= tp
->snd_ssthresh
;
2485 * Clamp the congestion window to the crossover point
2486 * and exit fast recovery.
2488 if (tp
->snd_cwnd
> tp
->snd_ssthresh
)
2489 tp
->snd_cwnd
= tp
->snd_ssthresh
;
2492 EXIT_FASTRECOVERY(tp
);
2494 tp
->t_bytes_acked
= 0;
2499 * Clamp the congestion window to the crossover point
2500 * and exit fast recovery in non-newreno and non-SACK case.
2502 if (tp
->snd_cwnd
> tp
->snd_ssthresh
)
2503 tp
->snd_cwnd
= tp
->snd_ssthresh
;
2504 EXIT_FASTRECOVERY(tp
);
2506 tp
->t_bytes_acked
= 0;
2512 * If we reach this point, ACK is not a duplicate,
2513 * i.e., it ACKs something we sent.
2515 if (tp
->t_flags
& TF_NEEDSYN
) {
2517 * T/TCP: Connection was half-synchronized, and our
2518 * SYN has been ACK'd (so connection is now fully
2519 * synchronized). Go to non-starred state,
2520 * increment snd_una for ACK of SYN, and check if
2521 * we can do window scaling.
2523 tp
->t_flags
&= ~TF_NEEDSYN
;
2525 /* Do window scaling? */
2526 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
2527 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
2528 tp
->snd_scale
= tp
->requested_s_scale
;
2529 tp
->rcv_scale
= tp
->request_r_scale
;
2534 acked
= th
->th_ack
- tp
->snd_una
;
2535 tcpstat
.tcps_rcvackpack
++;
2536 tcpstat
.tcps_rcvackbyte
+= acked
;
2539 * If we just performed our first retransmit, and the ACK
2540 * arrives within our recovery window, then it was a mistake
2541 * to do the retransmit in the first place. Recover our
2542 * original cwnd and ssthresh, and proceed to transmit where
2545 if (tp
->t_rxtshift
== 1 && tcp_now
< tp
->t_badrxtwin
) {
2546 ++tcpstat
.tcps_sndrexmitbad
;
2547 tp
->snd_cwnd
= tp
->snd_cwnd_prev
;
2548 tp
->snd_ssthresh
= tp
->snd_ssthresh_prev
;
2549 tp
->snd_recover
= tp
->snd_recover_prev
;
2550 if (tp
->t_flags
& TF_WASFRECOVERY
)
2551 ENTER_FASTRECOVERY(tp
);
2552 tp
->snd_nxt
= tp
->snd_max
;
2553 tp
->t_badrxtwin
= 0; /* XXX probably not required */
2557 * If we have a timestamp reply, update smoothed
2558 * round trip time. If no timestamp is present but
2559 * transmit timer is running and timed sequence
2560 * number was acked, update smoothed round trip time.
2561 * Since we now have an rtt measurement, cancel the
2562 * timer backoff (cf., Phil Karn's retransmit alg.).
2563 * Recompute the initial retransmit timer.
2564 * Also makes sure we have a valid time stamp in hand
2566 * Some boxes send broken timestamp replies
2567 * during the SYN+ACK phase, ignore
2568 * timestamps of 0 or we could calculate a
2569 * huge RTT and blow up the retransmit timer.
2571 if (((to
.to_flags
& TOF_TS
) != 0) && (to
.to_tsecr
!= 0)) {
2572 if (!tp
->t_rttlow
|| tp
->t_rttlow
> tcp_now
- to
.to_tsecr
)
2573 tp
->t_rttlow
= tcp_now
- to
.to_tsecr
;
2574 tcp_xmit_timer(tp
, tcp_now
- to
.to_tsecr
);
2575 } else if (tp
->t_rtttime
&& SEQ_GT(th
->th_ack
, tp
->t_rtseq
)) {
2576 if (!tp
->t_rttlow
|| tp
->t_rttlow
> tcp_now
- tp
->t_rtttime
)
2577 tp
->t_rttlow
= tcp_now
- tp
->t_rtttime
;
2578 tcp_xmit_timer(tp
, tp
->t_rtttime
);
2582 * If all outstanding data is acked, stop retransmit
2583 * timer and remember to restart (more output or persist).
2584 * If there is more data to be acked, restart retransmit
2585 * timer, using current (possibly backed-off) value.
2587 if (th
->th_ack
== tp
->snd_max
) {
2588 tp
->t_timer
[TCPT_REXMT
] = 0;
2590 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
2591 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
2594 * If no data (only SYN) was ACK'd,
2595 * skip rest of ACK processing.
2601 * When new data is acked, open the congestion window.
2603 if ((thflags
& TH_ECE
) != 0 &&
2604 (tp
->ecn_flags
& TE_SETUPSENT
) != 0) {
2606 * Reduce the congestion window if we haven't done so.
2608 if (!(tp
->sack_enable
&& IN_FASTRECOVERY(tp
)) &&
2609 !(tcp_do_newreno
&& SEQ_LEQ(th
->th_ack
, tp
->snd_recover
))) {
2610 tcp_reduce_congestion_window(tp
);
2612 } else if ((!tcp_do_newreno
&& !tp
->sack_enable
) ||
2613 !IN_FASTRECOVERY(tp
)) {
2615 * RFC 3465 - Appropriate Byte Counting.
2617 * If the window is currently less than ssthresh,
2618 * open the window by the number of bytes ACKed by
2619 * the last ACK, however clamp the window increase
2620 * to an upper limit "L".
2622 * In congestion avoidance phase, open the window by
2623 * one segment each time "bytes_acked" grows to be
2624 * greater than or equal to the congestion window.
2627 register u_int cw
= tp
->snd_cwnd
;
2628 register u_int incr
= tp
->t_maxseg
;
2630 if (tcp_do_rfc3465
) {
2632 if (cw
>= tp
->snd_ssthresh
) {
2633 tp
->t_bytes_acked
+= acked
;
2634 if (tp
->t_bytes_acked
>= cw
) {
2635 /* Time to increase the window. */
2636 tp
->t_bytes_acked
-= cw
;
2638 /* No need to increase yet. */
2643 * If the user explicitly enables RFC3465
2644 * use 2*SMSS for the "L" param. Otherwise
2645 * use the more conservative 1*SMSS.
2647 * (See RFC 3465 2.3 Choosing the Limit)
2651 abc_lim
= (tcp_do_rfc3465_lim2
&&
2652 tp
->snd_nxt
== tp
->snd_max
) ? incr
* 2 : incr
;
2654 incr
= lmin(acked
, abc_lim
);
2659 * If the window gives us less than ssthresh packets
2660 * in flight, open exponentially (segsz per packet).
2661 * Otherwise open linearly: segsz per window
2662 * (segsz^2 / cwnd per packet).
2665 if (cw
>= tp
->snd_ssthresh
) {
2666 incr
= max((incr
* incr
/ cw
), 1);
2671 tp
->snd_cwnd
= min(cw
+incr
, TCP_MAXWIN
<<tp
->snd_scale
);
2673 if (acked
> so
->so_snd
.sb_cc
) {
2674 tp
->snd_wnd
-= so
->so_snd
.sb_cc
;
2675 sbdrop(&so
->so_snd
, (int)so
->so_snd
.sb_cc
);
2678 sbdrop(&so
->so_snd
, acked
);
2679 tp
->snd_wnd
-= acked
;
2682 /* detect una wraparound */
2683 if ((tcp_do_newreno
|| tp
->sack_enable
) &&
2684 !IN_FASTRECOVERY(tp
) &&
2685 SEQ_GT(tp
->snd_una
, tp
->snd_recover
) &&
2686 SEQ_LEQ(th
->th_ack
, tp
->snd_recover
))
2687 tp
->snd_recover
= th
->th_ack
- 1;
2688 if ((tcp_do_newreno
|| tp
->sack_enable
) &&
2689 IN_FASTRECOVERY(tp
) &&
2690 SEQ_GEQ(th
->th_ack
, tp
->snd_recover
))
2691 EXIT_FASTRECOVERY(tp
);
2692 tp
->snd_una
= th
->th_ack
;
2693 if (tp
->sack_enable
) {
2694 if (SEQ_GT(tp
->snd_una
, tp
->snd_recover
))
2695 tp
->snd_recover
= tp
->snd_una
;
2697 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
2698 tp
->snd_nxt
= tp
->snd_una
;
2701 * sowwakeup must happen after snd_una, et al. are updated so that
2702 * the sequence numbers are in sync with so_snd
2706 switch (tp
->t_state
) {
2709 * In FIN_WAIT_1 STATE in addition to the processing
2710 * for the ESTABLISHED state if our FIN is now acknowledged
2711 * then enter FIN_WAIT_2.
2713 case TCPS_FIN_WAIT_1
:
2714 if (ourfinisacked
) {
2716 * If we can't receive any more
2717 * data, then closing user can proceed.
2718 * Starting the timer is contrary to the
2719 * specification, but if we don't get a FIN
2720 * we'll hang forever.
2722 if (so
->so_state
& SS_CANTRCVMORE
) {
2723 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;
2724 add_to_time_wait(tp
);
2725 soisdisconnected(so
);
2727 tp
->t_state
= TCPS_FIN_WAIT_2
;
2728 /* fall through and make sure we also recognize data ACKed with the FIN */
2730 tp
->t_flags
|= TF_ACKNOW
;
2734 * In CLOSING STATE in addition to the processing for
2735 * the ESTABLISHED state if the ACK acknowledges our FIN
2736 * then enter the TIME-WAIT state, otherwise ignore
2740 if (ourfinisacked
) {
2741 tp
->t_state
= TCPS_TIME_WAIT
;
2742 tcp_canceltimers(tp
);
2743 /* Shorten TIME_WAIT [RFC-1644, p.28] */
2744 if (tp
->cc_recv
!= 0 &&
2745 tp
->t_starttime
< (u_int32_t
)tcp_msl
)
2746 tp
->t_timer
[TCPT_2MSL
] =
2747 tp
->t_rxtcur
* TCPTV_TWTRUNC
;
2749 tp
->t_timer
[TCPT_2MSL
] = 2 * tcp_msl
;
2750 add_to_time_wait(tp
);
2751 soisdisconnected(so
);
2753 tp
->t_flags
|= TF_ACKNOW
;
2757 * In LAST_ACK, we may still be waiting for data to drain
2758 * and/or to be acked, as well as for the ack of our FIN.
2759 * If our FIN is now acknowledged, delete the TCB,
2760 * enter the closed state and return.
2763 if (ourfinisacked
) {
2770 * In TIME_WAIT state the only thing that should arrive
2771 * is a retransmission of the remote FIN. Acknowledge
2772 * it and restart the finack timer.
2774 case TCPS_TIME_WAIT
:
2775 tp
->t_timer
[TCPT_2MSL
] = 2 * tcp_msl
;
2776 add_to_time_wait(tp
);
2783 * Update window information.
2784 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2786 if ((thflags
& TH_ACK
) &&
2787 (SEQ_LT(tp
->snd_wl1
, th
->th_seq
) ||
2788 (tp
->snd_wl1
== th
->th_seq
&& (SEQ_LT(tp
->snd_wl2
, th
->th_ack
) ||
2789 (tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
))))) {
2790 /* keep track of pure window updates */
2792 tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
)
2793 tcpstat
.tcps_rcvwinupd
++;
2794 tp
->snd_wnd
= tiwin
;
2795 tp
->snd_wl1
= th
->th_seq
;
2796 tp
->snd_wl2
= th
->th_ack
;
2797 if (tp
->snd_wnd
> tp
->max_sndwnd
)
2798 tp
->max_sndwnd
= tp
->snd_wnd
;
2803 * Process segments with URG.
2805 if ((thflags
& TH_URG
) && th
->th_urp
&&
2806 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
2808 * This is a kludge, but if we receive and accept
2809 * random urgent pointers, we'll crash in
2810 * soreceive. It's hard to imagine someone
2811 * actually wanting to send this much urgent data.
2813 if (th
->th_urp
+ so
->so_rcv
.sb_cc
> sb_max
) {
2814 th
->th_urp
= 0; /* XXX */
2815 thflags
&= ~TH_URG
; /* XXX */
2816 goto dodata
; /* XXX */
2819 * If this segment advances the known urgent pointer,
2820 * then mark the data stream. This should not happen
2821 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2822 * a FIN has been received from the remote side.
2823 * In these states we ignore the URG.
2825 * According to RFC961 (Assigned Protocols),
2826 * the urgent pointer points to the last octet
2827 * of urgent data. We continue, however,
2828 * to consider it to indicate the first octet
2829 * of data past the urgent section as the original
2830 * spec states (in one of two places).
2832 if (SEQ_GT(th
->th_seq
+th
->th_urp
, tp
->rcv_up
)) {
2833 tp
->rcv_up
= th
->th_seq
+ th
->th_urp
;
2834 so
->so_oobmark
= so
->so_rcv
.sb_cc
+
2835 (tp
->rcv_up
- tp
->rcv_nxt
) - 1;
2836 if (so
->so_oobmark
== 0) {
2837 so
->so_state
|= SS_RCVATMARK
;
2838 postevent(so
, 0, EV_OOB
);
2841 tp
->t_oobflags
&= ~(TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
2844 * Remove out of band data so doesn't get presented to user.
2845 * This can happen independent of advancing the URG pointer,
2846 * but if two URG's are pending at once, some out-of-band
2847 * data may creep in... ick.
2849 if (th
->th_urp
<= (u_int32_t
)tlen
2851 && (so
->so_options
& SO_OOBINLINE
) == 0
2854 tcp_pulloutofband(so
, th
, m
,
2855 drop_hdrlen
); /* hdr drop is delayed */
2858 * If no out of band data is expected,
2859 * pull receive urgent pointer along
2860 * with the receive window.
2862 if (SEQ_GT(tp
->rcv_nxt
, tp
->rcv_up
))
2863 tp
->rcv_up
= tp
->rcv_nxt
;
2867 * Process the segment text, merging it into the TCP sequencing queue,
2868 * and arranging for acknowledgment of receipt if necessary.
2869 * This process logically involves adjusting tp->rcv_wnd as data
2870 * is presented to the user (this happens in tcp_usrreq.c,
2871 * case PRU_RCVD). If a FIN has already been received on this
2872 * connection then we just ignore the text.
2874 if ((tlen
|| (thflags
& TH_FIN
)) &&
2875 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
2876 tcp_seq save_start
= th
->th_seq
;
2877 tcp_seq save_end
= th
->th_seq
+ tlen
;
2878 m_adj(m
, drop_hdrlen
); /* delayed header drop */
2880 * Insert segment which includes th into TCP reassembly queue
2881 * with control block tp. Set thflags to whether reassembly now
2882 * includes a segment with FIN. This handles the common case
2883 * inline (segment is the next to be received on an established
2884 * connection, and the queue is empty), avoiding linkage into
2885 * and removal from the queue and repetition of various
2887 * Set DELACK for segments received in order, but ack
2888 * immediately when segments are out of order (so
2889 * fast retransmit can work).
2891 if (th
->th_seq
== tp
->rcv_nxt
&&
2892 LIST_EMPTY(&tp
->t_segq
) &&
2893 TCPS_HAVEESTABLISHED(tp
->t_state
)) {
2894 if (DELAY_ACK(tp
) && ((tp
->t_flags
& TF_ACKNOW
) == 0)) {
2895 tp
->t_flags
|= TF_DELACK
;
2899 tp
->t_unacksegs
= 0;
2900 tp
->t_flags
|= TF_ACKNOW
;
2902 tp
->rcv_nxt
+= tlen
;
2903 thflags
= th
->th_flags
& TH_FIN
;
2904 tcpstat
.tcps_rcvpack
++;
2905 tcpstat
.tcps_rcvbyte
+= tlen
;
2907 if (sbappendstream(&so
->so_rcv
, m
))
2910 thflags
= tcp_reass(tp
, th
, &tlen
, m
);
2911 tp
->t_flags
|= TF_ACKNOW
;
2912 tp
->t_unacksegs
= 0;
2915 if (tlen
> 0 && tp
->sack_enable
)
2916 tcp_update_sack_list(tp
, save_start
, save_end
);
2918 if (tp
->t_flags
& TF_DELACK
)
2922 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
2923 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
2924 th
->th_seq
, th
->th_ack
, th
->th_win
);
2929 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
2930 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
2931 th
->th_seq
, th
->th_ack
, th
->th_win
);
2936 * Note the amount of data that peer has sent into
2937 * our window, in order to estimate the sender's
2940 len
= (u_int
)(so
->so_rcv
.sb_hiwat
- (tp
->rcv_adv
- tp
->rcv_nxt
));
2941 if (len
> so
->so_rcv
.sb_maxused
)
2942 so
->so_rcv
.sb_maxused
= len
;
2949 * If FIN is received ACK the FIN and let the user know
2950 * that the connection is closing.
2952 if (thflags
& TH_FIN
) {
2953 if (TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
2955 postevent(so
, 0, EV_FIN
);
2957 * If connection is half-synchronized
2958 * (ie NEEDSYN flag on) then delay ACK,
2959 * If connection is half-synchronized
2960 * (ie NEEDSYN flag on) then delay ACK,
2961 * so it may be piggybacked when SYN is sent.
2962 * Otherwise, since we received a FIN then no
2963 * more input can be expected, send ACK now.
2965 if (DELAY_ACK(tp
) && (tp
->t_flags
& TF_NEEDSYN
)) {
2966 tp
->t_flags
|= TF_DELACK
;
2970 tp
->t_flags
|= TF_ACKNOW
;
2971 tp
->t_unacksegs
= 0;
2975 switch (tp
->t_state
) {
2978 * In SYN_RECEIVED and ESTABLISHED STATES
2979 * enter the CLOSE_WAIT state.
2981 case TCPS_SYN_RECEIVED
:
2982 tp
->t_starttime
= 0;
2983 case TCPS_ESTABLISHED
:
2984 tp
->t_state
= TCPS_CLOSE_WAIT
;
2988 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2989 * enter the CLOSING state.
2991 case TCPS_FIN_WAIT_1
:
2992 tp
->t_state
= TCPS_CLOSING
;
2996 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2997 * starting the time-wait timer, turning off the other
3000 case TCPS_FIN_WAIT_2
:
3001 tp
->t_state
= TCPS_TIME_WAIT
;
3002 tcp_canceltimers(tp
);
3003 /* Shorten TIME_WAIT [RFC-1644, p.28] */
3004 if (tp
->cc_recv
!= 0 &&
3005 tp
->t_starttime
< (u_int32_t
)tcp_msl
) {
3006 tp
->t_timer
[TCPT_2MSL
] =
3007 tp
->t_rxtcur
* TCPTV_TWTRUNC
;
3008 /* For transaction client, force ACK now. */
3009 tp
->t_flags
|= TF_ACKNOW
;
3010 tp
->t_unacksegs
= 0;
3013 tp
->t_timer
[TCPT_2MSL
] = 2 * tcp_msl
;
3015 add_to_time_wait(tp
);
3016 soisdisconnected(so
);
3020 * In TIME_WAIT state restart the 2 MSL time_wait timer.
3022 case TCPS_TIME_WAIT
:
3023 tp
->t_timer
[TCPT_2MSL
] = 2 * tcp_msl
;
3024 add_to_time_wait(tp
);
3029 if (so
->so_options
& SO_DEBUG
)
3030 tcp_trace(TA_INPUT
, ostate
, tp
, (void *)tcp_saveipgen
,
3035 * Return any desired output.
3037 if (needoutput
|| (tp
->t_flags
& TF_ACKNOW
)) {
3038 tp
->t_unacksegs
= 0;
3039 (void) tcp_output(tp
);
3041 tcp_unlock(so
, 1, 0);
3042 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
3047 * Generate an ACK dropping incoming segment if it occupies
3048 * sequence space, where the ACK reflects our state.
3050 * We can now skip the test for the RST flag since all
3051 * paths to this code happen after packets containing
3052 * RST have been dropped.
3054 * In the SYN-RECEIVED state, don't send an ACK unless the
3055 * segment we received passes the SYN-RECEIVED ACK test.
3056 * If it fails send a RST. This breaks the loop in the
3057 * "LAND" DoS attack, and also prevents an ACK storm
3058 * between two listening ports that have been sent forged
3059 * SYN segments, each with the source address of the other.
3061 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& (thflags
& TH_ACK
) &&
3062 (SEQ_GT(tp
->snd_una
, th
->th_ack
) ||
3063 SEQ_GT(th
->th_ack
, tp
->snd_max
)) ) {
3064 rstreason
= BANDLIM_RST_OPENPORT
;
3068 if (so
->so_options
& SO_DEBUG
)
3069 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
3073 tp
->t_flags
|= TF_ACKNOW
;
3074 tp
->t_unacksegs
= 0;
3075 (void) tcp_output(tp
);
3076 tcp_unlock(so
, 1, 0);
3077 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
3079 dropwithresetnosock
:
3083 * Generate a RST, dropping incoming segment.
3084 * Make ACK acceptable to originator of segment.
3085 * Don't bother to respond if destination was broadcast/multicast.
3087 if ((thflags
& TH_RST
) || m
->m_flags
& (M_BCAST
|M_MCAST
))
3091 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
3092 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
))
3096 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
3097 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
3098 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
3099 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
))
3101 /* IPv6 anycast check is done at tcp6_input() */
3104 * Perform bandwidth limiting.
3107 if (badport_bandlim(rstreason
) < 0)
3112 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
3113 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
3116 if (thflags
& TH_ACK
)
3117 /* mtod() below is safe as long as hdr dropping is delayed */
3118 tcp_respond(tp
, mtod(m
, void *), th
, m
, (tcp_seq
)0, th
->th_ack
,
3121 if (thflags
& TH_SYN
)
3123 /* mtod() below is safe as long as hdr dropping is delayed */
3124 tcp_respond(tp
, mtod(m
, void *), th
, m
, th
->th_seq
+tlen
,
3125 (tcp_seq
)0, TH_RST
|TH_ACK
, ifscope
);
3127 /* destroy temporarily created socket */
3130 tcp_unlock(so
, 1, 0);
3133 if ((inp
!= NULL
) && (nosock
== 0))
3134 tcp_unlock(so
, 1, 0);
3135 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
3141 * Drop space held by incoming segment and return.
3144 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
3145 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
3149 /* destroy temporarily created socket */
3152 tcp_unlock(so
, 1, 0);
3156 tcp_unlock(so
, 1, 0);
3157 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
3162 tcp_dooptions(tp
, cp
, cnt
, th
, to
, input_ifscope
)
3164 * Parse TCP options and place in tcpopt.
3171 unsigned int input_ifscope
;
3176 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
3178 if (opt
== TCPOPT_EOL
)
3180 if (opt
== TCPOPT_NOP
)
3186 if (optlen
< 2 || optlen
> cnt
)
3195 if (optlen
!= TCPOLEN_MAXSEG
)
3197 if (!(th
->th_flags
& TH_SYN
))
3199 bcopy((char *) cp
+ 2, (char *) &mss
, sizeof(mss
));
3201 #if BYTE_ORDER != BIG_ENDIAN
3208 if (optlen
!= TCPOLEN_WINDOW
)
3210 if (!(th
->th_flags
& TH_SYN
))
3212 tp
->t_flags
|= TF_RCVD_SCALE
;
3213 tp
->requested_s_scale
= min(cp
[2], TCP_MAX_WINSHIFT
);
3216 case TCPOPT_TIMESTAMP
:
3217 if (optlen
!= TCPOLEN_TIMESTAMP
)
3219 to
->to_flags
|= TOF_TS
;
3220 bcopy((char *)cp
+ 2,
3221 (char *)&to
->to_tsval
, sizeof(to
->to_tsval
));
3223 #if BYTE_ORDER != BIG_ENDIAN
3224 NTOHL(to
->to_tsval
);
3227 bcopy((char *)cp
+ 6,
3228 (char *)&to
->to_tsecr
, sizeof(to
->to_tsecr
));
3230 #if BYTE_ORDER != BIG_ENDIAN
3231 NTOHL(to
->to_tsecr
);
3235 * A timestamp received in a SYN makes
3236 * it ok to send timestamp requests and replies.
3238 if (th
->th_flags
& TH_SYN
) {
3239 tp
->t_flags
|= TF_RCVD_TSTMP
;
3240 tp
->ts_recent
= to
->to_tsval
;
3241 tp
->ts_recent_age
= tcp_now
;
3244 case TCPOPT_SACK_PERMITTED
:
3246 optlen
!= TCPOLEN_SACK_PERMITTED
)
3248 if (th
->th_flags
& TH_SYN
)
3249 to
->to_flags
|= TOF_SACK
;
3252 if (optlen
<= 2 || (optlen
- 2) % TCPOLEN_SACK
!= 0)
3254 to
->to_nsacks
= (optlen
- 2) / TCPOLEN_SACK
;
3255 to
->to_sacks
= cp
+ 2;
3256 tcpstat
.tcps_sack_rcv_blocks
++;
3261 if (th
->th_flags
& TH_SYN
)
3262 tcp_mss(tp
, mss
, input_ifscope
); /* sets t_maxseg */
3266 * Pull out of band byte out of a segment so
3267 * it doesn't appear in the user's data queue.
3268 * It is still reflected in the segment length for
3269 * sequencing purposes.
3272 tcp_pulloutofband(so
, th
, m
, off
)
3275 register struct mbuf
*m
;
3276 int off
; /* delayed to be droped hdrlen */
3278 int cnt
= off
+ th
->th_urp
- 1;
3281 if (m
->m_len
> cnt
) {
3282 char *cp
= mtod(m
, caddr_t
) + cnt
;
3283 struct tcpcb
*tp
= sototcpcb(so
);
3286 tp
->t_oobflags
|= TCPOOB_HAVEDATA
;
3287 bcopy(cp
+1, cp
, (unsigned)(m
->m_len
- cnt
- 1));
3289 if (m
->m_flags
& M_PKTHDR
)
3298 panic("tcp_pulloutofband");
3302 * Collect new round-trip time estimate
3303 * and update averages and current timeout.
3306 tcp_xmit_timer(tp
, rtt
)
3307 register struct tcpcb
*tp
;
3312 tcpstat
.tcps_rttupdated
++;
3314 if (tp
->t_srtt
!= 0) {
3316 * srtt is stored as fixed point with 5 bits after the
3317 * binary point (i.e., scaled by 8). The following magic
3318 * is equivalent to the smoothing algorithm in rfc793 with
3319 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3320 * point). Adjust rtt to origin 0.
3322 delta
= ((rtt
- 1) << TCP_DELTA_SHIFT
)
3323 - (tp
->t_srtt
>> (TCP_RTT_SHIFT
- TCP_DELTA_SHIFT
));
3325 if ((tp
->t_srtt
+= delta
) <= 0)
3329 * We accumulate a smoothed rtt variance (actually, a
3330 * smoothed mean difference), then set the retransmit
3331 * timer to smoothed rtt + 4 times the smoothed variance.
3332 * rttvar is stored as fixed point with 4 bits after the
3333 * binary point (scaled by 16). The following is
3334 * equivalent to rfc793 smoothing with an alpha of .75
3335 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
3336 * rfc793's wired-in beta.
3340 delta
-= tp
->t_rttvar
>> (TCP_RTTVAR_SHIFT
- TCP_DELTA_SHIFT
);
3341 if ((tp
->t_rttvar
+= delta
) <= 0)
3343 if (tp
->t_rttbest
> tp
->t_srtt
+ tp
->t_rttvar
)
3344 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
3347 * No rtt measurement yet - use the unsmoothed rtt.
3348 * Set the variance to half the rtt (so our first
3349 * retransmit happens at 3*rtt).
3351 tp
->t_srtt
= rtt
<< TCP_RTT_SHIFT
;
3352 tp
->t_rttvar
= rtt
<< (TCP_RTTVAR_SHIFT
- 1);
3353 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
3359 * the retransmit should happen at rtt + 4 * rttvar.
3360 * Because of the way we do the smoothing, srtt and rttvar
3361 * will each average +1/2 tick of bias. When we compute
3362 * the retransmit timer, we want 1/2 tick of rounding and
3363 * 1 extra tick because of +-1/2 tick uncertainty in the
3364 * firing of the timer. The bias will give us exactly the
3365 * 1.5 tick we need. But, because the bias is
3366 * statistical, we have to test that we don't drop below
3367 * the minimum feasible timer (which is 2 ticks).
3369 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
3370 max(tp
->t_rttmin
, rtt
+ 2), TCPTV_REXMTMAX
);
3373 * We received an ack for a packet that wasn't retransmitted;
3374 * it is probably safe to discard any error indications we've
3375 * received recently. This isn't quite right, but close enough
3376 * for now (a route might have failed after we sent a segment,
3377 * and the return path might not be symmetrical).
3379 tp
->t_softerror
= 0;
3382 static inline unsigned int
3383 tcp_maxmtu(struct rtentry
*rt
)
3385 unsigned int maxmtu
;
3387 RT_LOCK_ASSERT_HELD(rt
);
3388 if (rt
->rt_rmx
.rmx_mtu
== 0)
3389 maxmtu
= rt
->rt_ifp
->if_mtu
;
3391 maxmtu
= MIN(rt
->rt_rmx
.rmx_mtu
, rt
->rt_ifp
->if_mtu
);
3397 static inline unsigned int
3398 tcp_maxmtu6(struct rtentry
*rt
)
3400 unsigned int maxmtu
;
3402 RT_LOCK_ASSERT_HELD(rt
);
3403 lck_rw_lock_shared(nd_if_rwlock
);
3404 if (rt
->rt_rmx
.rmx_mtu
== 0)
3405 maxmtu
= IN6_LINKMTU(rt
->rt_ifp
);
3407 maxmtu
= MIN(rt
->rt_rmx
.rmx_mtu
, IN6_LINKMTU(rt
->rt_ifp
));
3408 lck_rw_done(nd_if_rwlock
);
3415 * Determine a reasonable value for maxseg size.
3416 * If the route is known, check route for mtu.
3417 * If none, use an mss that can be handled on the outgoing
3418 * interface without forcing IP to fragment; if bigger than
3419 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
3420 * to utilize large mbufs. If no route is found, route has no mtu,
3421 * or the destination isn't local, use a default, hopefully conservative
3422 * size (usually 512 or the default IP max size, but no more than the mtu
3423 * of the interface), as we can't discover anything about intervening
3424 * gateways or networks. We also initialize the congestion/slow start
3425 * window to be a single segment if the destination isn't local.
3426 * While looking at the routing entry, we also initialize other path-dependent
3427 * parameters from pre-set or cached values in the routing entry.
3429 * Also take into account the space needed for options that we
3430 * send regularly. Make maxseg shorter by that amount to assure
3431 * that we can send maxseg amount of data even when the options
3432 * are present. Store the upper limit of the length of options plus
3435 * NOTE that this routine is only called when we process an incoming
3436 * segment, for outgoing segments only tcp_mssopt is called.
3440 tcp_mss(tp
, offer
, input_ifscope
)
3443 unsigned int input_ifscope
;
3445 register struct rtentry
*rt
;
3447 register int rtt
, mss
;
3451 struct rmxp_tao
*taop
;
3452 int origoffer
= offer
;
3453 u_int32_t sb_max_corrected
;
3462 isipv6
= ((inp
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
3463 min_protoh
= isipv6
? sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
)
3464 : sizeof (struct tcpiphdr
);
3466 #define min_protoh (sizeof (struct tcpiphdr))
3471 rt
= tcp_rtlookup6(inp
);
3473 (IN6_IS_ADDR_LOOPBACK(&inp
->in6p_faddr
) ||
3474 IN6_IS_ADDR_LINKLOCAL(&inp
->in6p_faddr
) ||
3475 rt
->rt_gateway
->sa_family
== AF_LINK
))
3481 rt
= tcp_rtlookup(inp
, input_ifscope
);
3483 (rt
->rt_gateway
->sa_family
== AF_LINK
||
3484 rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
))
3488 tp
->t_maxopd
= tp
->t_maxseg
=
3490 isipv6
? tcp_v6mssdflt
:
3497 * Slower link window correction:
3498 * If a value is specificied for slowlink_wsize use it for PPP links
3499 * believed to be on a serial modem (speed <128Kbps). Excludes 9600bps as
3500 * it is the default value adversized by pseudo-devices over ppp.
3502 if (ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
3503 ifp
->if_baudrate
> 9600 && ifp
->if_baudrate
<= 128000) {
3504 tp
->t_flags
|= TF_SLOWLINK
;
3506 so
= inp
->inp_socket
;
3508 taop
= rmx_taop(rt
->rt_rmx
);
3510 * Offer == -1 means that we didn't receive SYN yet,
3511 * use cached value in that case;
3514 offer
= taop
->tao_mssopt
;
3516 * Offer == 0 means that there was no MSS on the SYN segment,
3517 * in this case we use tcp_mssdflt.
3522 isipv6
? tcp_v6mssdflt
:
3527 * Prevent DoS attack with too small MSS. Round up
3528 * to at least minmss.
3530 offer
= max(offer
, tcp_minmss
);
3532 * Sanity check: make sure that maxopd will be large
3533 * enough to allow some data on segments even is the
3534 * all the option space is used (40bytes). Otherwise
3535 * funny things may happen in tcp_output.
3537 offer
= max(offer
, 64);
3539 taop
->tao_mssopt
= offer
;
3542 * While we're here, check if there's an initial rtt
3543 * or rttvar. Convert from the route-table units
3544 * to scaled multiples of the slow timeout timer.
3546 if (tp
->t_srtt
== 0 && (rtt
= rt
->rt_rmx
.rmx_rtt
)) {
3548 * XXX the lock bit for RTT indicates that the value
3549 * is also a minimum value; this is subject to time.
3551 if (rt
->rt_rmx
.rmx_locks
& RTV_RTT
)
3552 tp
->t_rttmin
= rtt
/ (RTM_RTTUNIT
/ TCP_RETRANSHZ
);
3554 tp
->t_rttmin
= isnetlocal
? tcp_TCPTV_MIN
: TCP_RETRANSHZ
;
3555 tp
->t_srtt
= rtt
/ (RTM_RTTUNIT
/ (TCP_RETRANSHZ
* TCP_RTT_SCALE
));
3556 tcpstat
.tcps_usedrtt
++;
3557 if (rt
->rt_rmx
.rmx_rttvar
) {
3558 tp
->t_rttvar
= rt
->rt_rmx
.rmx_rttvar
/
3559 (RTM_RTTUNIT
/ (TCP_RETRANSHZ
* TCP_RTTVAR_SCALE
));
3560 tcpstat
.tcps_usedrttvar
++;
3562 /* default variation is +- 1 rtt */
3564 tp
->t_srtt
* TCP_RTTVAR_SCALE
/ TCP_RTT_SCALE
;
3566 TCPT_RANGESET(tp
->t_rxtcur
,
3567 ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1,
3568 tp
->t_rttmin
, TCPTV_REXMTMAX
);
3571 tp
->t_rttmin
= isnetlocal
? tcp_TCPTV_MIN
: TCP_RETRANSHZ
;
3574 mss
= (isipv6
? tcp_maxmtu6(rt
) : tcp_maxmtu(rt
));
3576 mss
= tcp_maxmtu(rt
);
3580 if (rt
->rt_rmx
.rmx_mtu
== 0) {
3584 mss
= min(mss
, tcp_v6mssdflt
);
3588 mss
= min(mss
, tcp_mssdflt
);
3591 mss
= min(mss
, offer
);
3593 * maxopd stores the maximum length of data AND options
3594 * in a segment; maxseg is the amount of data in a normal
3595 * segment. We need to store this value (maxopd) apart
3596 * from maxseg, because now every segment carries options
3597 * and thus we normally have somewhat less data in segments.
3602 * origoffer==-1 indicates, that no segments were received yet.
3603 * In this case we just guess.
3605 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
3607 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
))
3608 mss
-= TCPOLEN_TSTAMP_APPA
;
3612 * Calculate corrected value for sb_max; ensure to upgrade the
3613 * numerator for large sb_max values else it will overflow.
3615 sb_max_corrected
= (sb_max
* (u_int64_t
)MCLBYTES
) / (MSIZE
+ MCLBYTES
);
3618 * If there's a pipesize (ie loopback), change the socket
3619 * buffer to that size only if it's bigger than the current
3620 * sockbuf size. Make the socket buffers an integral
3621 * number of mss units; if the mss is larger than
3622 * the socket buffer, decrease the mss.
3625 bufsize
= rt
->rt_rmx
.rmx_sendpipe
;
3626 if (bufsize
< so
->so_snd
.sb_hiwat
)
3628 bufsize
= so
->so_snd
.sb_hiwat
;
3632 bufsize
= (((bufsize
+ (u_int64_t
)mss
- 1) / (u_int64_t
)mss
) * (u_int64_t
)mss
);
3633 if (bufsize
> sb_max_corrected
)
3634 bufsize
= sb_max_corrected
;
3635 (void)sbreserve(&so
->so_snd
, bufsize
);
3640 bufsize
= rt
->rt_rmx
.rmx_recvpipe
;
3641 if (bufsize
< so
->so_rcv
.sb_hiwat
)
3643 bufsize
= so
->so_rcv
.sb_hiwat
;
3644 if (bufsize
> mss
) {
3645 bufsize
= (((bufsize
+ (u_int64_t
)mss
- 1) / (u_int64_t
)mss
) * (u_int64_t
)mss
);
3646 if (bufsize
> sb_max_corrected
)
3647 bufsize
= sb_max_corrected
;
3648 (void)sbreserve(&so
->so_rcv
, bufsize
);
3652 * Set the slow-start flight size depending on whether this
3653 * is a local network or not.
3656 tp
->snd_cwnd
= mss
* ss_fltsz_local
;
3658 tp
->snd_cwnd
= mss
* ss_fltsz
;
3660 if (rt
->rt_rmx
.rmx_ssthresh
) {
3662 * There's some sort of gateway or interface
3663 * buffer limit on the path. Use this to set
3664 * the slow start threshhold, but set the
3665 * threshold to no less than 2*mss.
3667 tp
->snd_ssthresh
= max(2 * mss
, rt
->rt_rmx
.rmx_ssthresh
);
3668 tcpstat
.tcps_usedssthresh
++;
3670 tp
->snd_ssthresh
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
3673 /* Route locked during lookup above */
3678 * Determine the MSS option to send on an outgoing SYN.
3692 isipv6
= ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
3693 min_protoh
= isipv6
? sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
)
3694 : sizeof (struct tcpiphdr
);
3696 #define min_protoh (sizeof (struct tcpiphdr))
3701 rt
= tcp_rtlookup6(tp
->t_inpcb
);
3704 rt
= tcp_rtlookup(tp
->t_inpcb
, IFSCOPE_NONE
);
3708 isipv6
? tcp_v6mssdflt
:
3713 * Slower link window correction:
3714 * If a value is specificied for slowlink_wsize use it for PPP links
3715 * believed to be on a serial modem (speed <128Kbps). Excludes 9600bps as
3716 * it is the default value adversized by pseudo-devices over ppp.
3718 if (rt
->rt_ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
3719 rt
->rt_ifp
->if_baudrate
> 9600 && rt
->rt_ifp
->if_baudrate
<= 128000) {
3720 tp
->t_flags
|= TF_SLOWLINK
;
3724 mss
= (isipv6
? tcp_maxmtu6(rt
) : tcp_maxmtu(rt
));
3726 mss
= tcp_maxmtu(rt
);
3728 /* Route locked during lookup above */
3730 return (mss
- min_protoh
);
3734 * On a partial ack arrives, force the retransmission of the
3735 * next unacknowledged segment. Do not clear tp->t_dupacks.
3736 * By setting snd_nxt to ti_ack, this forces retransmission timer to
3740 tcp_newreno_partial_ack(tp
, th
)
3744 tcp_seq onxt
= tp
->snd_nxt
;
3745 u_int32_t ocwnd
= tp
->snd_cwnd
;
3746 tp
->t_timer
[TCPT_REXMT
] = 0;
3748 tp
->snd_nxt
= th
->th_ack
;
3750 * Set snd_cwnd to one segment beyond acknowledged offset
3751 * (tp->snd_una has not yet been updated when this function
3754 tp
->snd_cwnd
= tp
->t_maxseg
+ (th
->th_ack
- tp
->snd_una
);
3755 tp
->t_flags
|= TF_ACKNOW
;
3756 tp
->t_unacksegs
= 0;
3757 (void) tcp_output(tp
);
3758 tp
->snd_cwnd
= ocwnd
;
3759 if (SEQ_GT(onxt
, tp
->snd_nxt
))
3762 * Partial window deflation. Relies on fact that tp->snd_una
3765 if (tp
->snd_cwnd
> th
->th_ack
- tp
->snd_una
)
3766 tp
->snd_cwnd
-= th
->th_ack
- tp
->snd_una
;
3769 tp
->snd_cwnd
+= tp
->t_maxseg
;
3774 * Drop a random TCP connection that hasn't been serviced yet and
3775 * is eligible for discard. There is a one in qlen chance that
3776 * we will return a null, saying that there are no dropable
3777 * requests. In this case, the protocol specific code should drop
3778 * the new request. This insures fairness.
3780 * The listening TCP socket "head" must be locked
3783 tcp_dropdropablreq(struct socket
*head
)
3785 struct socket
*so
, *sonext
;
3786 unsigned int i
, j
, qlen
;
3788 static struct timeval old_runtime
;
3789 static unsigned int cur_cnt
, old_cnt
;
3791 struct inpcb
*inp
= NULL
;
3794 if ((head
->so_options
& SO_ACCEPTCONN
) == 0)
3797 so
= TAILQ_FIRST(&head
->so_incomp
);
3802 if ((i
= (tv
.tv_sec
- old_runtime
.tv_sec
)) != 0) {
3804 old_cnt
= cur_cnt
/ i
;
3809 qlen
= head
->so_incqlen
;
3810 if (++cur_cnt
> qlen
|| old_cnt
> qlen
) {
3811 rnd
= (314159 * rnd
+ 66329) & 0xffff;
3812 j
= ((qlen
+ 1) * rnd
) >> 16;
3815 so
= TAILQ_NEXT(so
, so_list
);
3817 /* Find a connection that is not already closing (or being served) */
3819 inp
= (struct inpcb
*)so
->so_pcb
;
3821 sonext
= TAILQ_NEXT(so
, so_list
);
3823 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
3824 /* Avoid the issue of a socket being accepted by one input thread
3825 * and being dropped by another input thread.
3826 * If we can't get a hold on this mutex, then grab the next socket in line.
3828 if (lck_mtx_try_lock(inp
->inpcb_mtx
)) {
3830 if ((so
->so_usecount
== 2) && so
->so_state
& SS_INCOMP
)
3832 else {/* don't use if beeing accepted or used in any other way */
3833 in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
3834 tcp_unlock(so
, 1, 0);
3838 /* do not try to lock the inp in in_pcb_checkstate
3839 * because the lock is already held in some other thread.
3840 * Only drop the inp_wntcnt reference.
3842 in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
3851 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
3852 tcp_unlock(head
, 0, 0);
3854 /* Makes sure socket is still in the right state to be discarded */
3856 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
3857 tcp_unlock(so
, 1, 0);
3858 tcp_lock(head
, 0, 0);
3862 if (so
->so_usecount
!= 2 || !(so
->so_state
& SS_INCOMP
)) {
3863 /* do not discard: that socket is beeing accepted */
3864 tcp_unlock(so
, 1, 0);
3865 tcp_lock(head
, 0, 0);
3872 * We do not want to lose track of the PCB right away in case we receive
3873 * more segments from the peer
3876 so
->so_flags
|= SOF_OVERFLOW
;
3877 tp
->t_state
= TCPS_TIME_WAIT
;
3878 (void) tcp_close(tp
);
3879 tp
->t_unacksegs
= 0;
3880 tcpstat
.tcps_drops
++;
3881 tcp_canceltimers(tp
);
3882 add_to_time_wait(tp
);
3884 tcp_unlock(so
, 1, 0);
3885 tcp_lock(head
, 0, 0);
3892 tcp_getstat SYSCTL_HANDLER_ARGS
3894 #pragma unused(oidp, arg1, arg2)
3898 if (req
->oldptr
== 0) {
3899 req
->oldlen
= (size_t)sizeof(struct tcpstat
);
3902 error
= SYSCTL_OUT(req
, &tcpstat
, MIN(sizeof (tcpstat
), req
->oldlen
));
3908 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_STATS
, stats
, CTLFLAG_RD
, 0, 0,
3909 tcp_getstat
, "S,tcpstat", "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
3912 sysctl_rexmtthresh SYSCTL_HANDLER_ARGS
3914 #pragma unused(arg1, arg2)
3916 int error
, val
= tcprexmtthresh
;
3918 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
3919 if (error
|| !req
->newptr
)
3923 * Constrain the number of duplicate ACKs
3924 * to consider for TCP fast retransmit
3928 if (val
< 2 || val
> 3)
3931 tcprexmtthresh
= val
;
3936 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, rexmt_thresh
, CTLTYPE_INT
|CTLFLAG_RW
,
3937 &tcprexmtthresh
, 0, &sysctl_rexmtthresh
, "I", "Duplicate ACK Threshold for Fast Retransmit");