2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
55 * $FreeBSD: src/sys/netinet/tcp_input.c,v 1.107.2.16 2001/08/22 00:59:12 silby Exp $
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/sysctl.h>
63 #include <sys/malloc.h>
65 #include <sys/proc.h> /* for proc0 declaration */
66 #include <sys/protosw.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/syslog.h>
71 #include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */
74 #include <net/if_types.h>
75 #include <net/route.h>
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
80 #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */
81 #include <netinet/in_var.h>
82 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
83 #include <netinet/in_pcb.h>
84 #include <netinet/ip_var.h>
86 #include <netinet/ip6.h>
87 #include <netinet/icmp6.h>
88 #include <netinet6/nd6.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet6/in6_pcb.h>
92 #include <netinet/tcp.h>
93 #include <netinet/tcp_fsm.h>
94 #include <netinet/tcp_seq.h>
95 #include <netinet/tcp_timer.h>
96 #include <netinet/tcp_var.h>
98 #include <netinet6/tcp6_var.h>
100 #include <netinet/tcpip.h>
102 #include <netinet/tcp_debug.h>
103 u_char tcp_saveipgen
[40]; /* the size must be of max ip header, now IPv6 */
104 struct tcphdr tcp_savetcp
;
105 #endif /* TCPDEBUG */
108 #include <netinet6/ipsec.h>
110 #include <netinet6/ipsec6.h>
112 #include <netkey/key.h>
115 #include <sys/kdebug.h>
118 MALLOC_DEFINE(M_TSEGQ
, "tseg_qent", "TCP segment queue entry");
121 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 0)
122 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 2)
123 #define DBG_FNC_TCP_INPUT NETDBG_CODE(DBG_NETTCP, (3 << 8))
124 #define DBG_FNC_TCP_NEWCONN NETDBG_CODE(DBG_NETTCP, (7 << 8))
126 static int tcprexmtthresh
= 3;
128 extern int apple_hwcksum_rx
;
131 extern int ipsec_bypass
;
132 extern lck_mtx_t
*sadb_mutex
;
135 struct tcpstat tcpstat
;
136 SYSCTL_STRUCT(_net_inet_tcp
, TCPCTL_STATS
, stats
, CTLFLAG_RD
,
137 &tcpstat
, tcpstat
, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
139 static int log_in_vain
= 0;
140 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, log_in_vain
, CTLFLAG_RW
,
141 &log_in_vain
, 0, "Log all incoming TCP connections");
143 static int blackhole
= 0;
144 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, blackhole
, CTLFLAG_RW
,
145 &blackhole
, 0, "Do not send RST when dropping refused connections");
147 int tcp_delack_enabled
= 3;
148 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, delayed_ack
, CTLFLAG_RW
,
149 &tcp_delack_enabled
, 0,
150 "Delay ACK to try and piggyback it onto a data packet");
152 int tcp_lq_overflow
= 1;
153 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tcp_lq_overflow
, CTLFLAG_RW
,
155 "Listen Queue Overflow");
158 static int drop_synfin
= 1;
159 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, drop_synfin
, CTLFLAG_RW
,
160 &drop_synfin
, 0, "Drop TCP packets with SYN+FIN set");
163 SYSCTL_NODE(_net_inet_tcp
, OID_AUTO
, reass
, CTLFLAG_RW
, 0,
164 "TCP Segment Reassembly Queue");
166 __private_extern__
int tcp_reass_maxseg
= 0;
167 SYSCTL_INT(_net_inet_tcp_reass
, OID_AUTO
, maxsegments
, CTLFLAG_RW
,
168 &tcp_reass_maxseg
, 0,
169 "Global maximum number of TCP Segments in Reassembly Queue");
171 __private_extern__
int tcp_reass_qsize
= 0;
172 SYSCTL_INT(_net_inet_tcp_reass
, OID_AUTO
, cursegments
, CTLFLAG_RD
,
174 "Global number of TCP Segments currently in Reassembly Queue");
176 static int tcp_reass_overflows
= 0;
177 SYSCTL_INT(_net_inet_tcp_reass
, OID_AUTO
, overflows
, CTLFLAG_RD
,
178 &tcp_reass_overflows
, 0,
179 "Global number of TCP Segment Reassembly Queue Overflows");
182 __private_extern__
int slowlink_wsize
= 8192;
183 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, slowlink_wsize
, CTLFLAG_RW
,
184 &slowlink_wsize
, 0, "Maximum advertised window size for slowlink");
188 struct inpcbhead tcb
;
189 #define tcb6 tcb /* for KAME src sync over BSD*'s */
190 struct inpcbinfo tcbinfo
;
192 static void tcp_dooptions(struct tcpcb
*,
193 u_char
*, int, struct tcphdr
*, struct tcpopt
*);
194 static void tcp_pulloutofband(struct socket
*,
195 struct tcphdr
*, struct mbuf
*, int);
196 static int tcp_reass(struct tcpcb
*, struct tcphdr
*, int *,
198 static void tcp_xmit_timer(struct tcpcb
*, int);
199 static int tcp_newreno
__P((struct tcpcb
*, struct tcphdr
*));
201 /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
203 #define ND6_HINT(tp) \
205 if ((tp) && (tp)->t_inpcb && \
206 ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \
207 (tp)->t_inpcb->in6p_route.ro_rt) \
208 nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \
214 extern u_long
*delack_bitmask
;
216 extern void ipfwsyslog( int level
, char *format
,...);
217 extern int ChkAddressOK( __uint32_t dstaddr
, __uint32_t srcaddr
);
218 extern int fw_verbose
;
220 #define log_in_vain_log( a ) { \
221 if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \
228 * Indicate whether this ack should be delayed.
229 * We can delay the ack if:
230 * - delayed acks are enabled (set to 1) and
231 * - our last ack wasn't a 0-sized window. We never want to delay
232 * the ack that opens up a 0-sized window.
233 * - delayed acks are enabled (set to 2, "more compatible") and
234 * - our last ack wasn't a 0-sized window.
235 * - if the peer hasn't sent us a TH_PUSH data packet (this solves 3649245)
236 * - the peer hasn't sent us a TH_PUSH data packet, if he did, take this as a clue that we
237 * need to ACK with no delay. This helps higher level protocols who won't send
238 * us more data even if the window is open because their last "segment" hasn't been ACKed
239 * - delayed acks are enabled (set to 3, "streaming detection") and
240 * - if we receive more than 4 full packets per second on this socket, we're streaming acts as "1".
241 * - if we don't meet that criteria, acts like "2". Allowing faster acking while browsing for example.
244 #define DELAY_ACK(tp) \
245 (((tcp_delack_enabled == 1) && ((tp->t_flags & TF_RXWIN0SENT) == 0)) || \
246 (((tcp_delack_enabled == 2) && (tp->t_flags & TF_RXWIN0SENT) == 0) && \
247 ((thflags & TH_PUSH) == 0) && ((tp->t_flags & TF_DELACK) == 0)) || \
248 (((tcp_delack_enabled == 3) && (tp->t_flags & TF_RXWIN0SENT) == 0) && \
249 (((tp->t_rcvtime == 0) && (tp->rcv_byps > (4* tp->t_maxseg))) || (((thflags & TH_PUSH) == 0) && ((tp->t_flags & TF_DELACK) == 0)))))
252 static int tcpdropdropablreq(struct socket
*head
);
256 tcp_reass(tp
, th
, tlenp
, m
)
257 register struct tcpcb
*tp
;
258 register struct tcphdr
*th
;
263 struct tseg_qent
*p
= NULL
;
264 struct tseg_qent
*nq
;
265 struct tseg_qent
*te
;
266 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
271 * Call with th==0 after become established to
272 * force pre-ESTABLISHED data up to user socket.
278 * Limit the number of segments in the reassembly queue to prevent
279 * holding on to too many segments (and thus running out of mbufs).
280 * Make sure to let the missing segment through which caused this
281 * queue. Always keep one global queue entry spare to be able to
282 * process the missing segment.
284 if (th
->th_seq
!= tp
->rcv_nxt
&&
285 tcp_reass_qsize
+ 1 >= tcp_reass_maxseg
) {
286 tcp_reass_overflows
++;
287 tcpstat
.tcps_rcvmemdrop
++;
292 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
293 MALLOC(te
, struct tseg_qent
*, sizeof (struct tseg_qent
), M_TSEGQ
,
296 tcpstat
.tcps_rcvmemdrop
++;
303 * Find a segment which begins after this one does.
305 LIST_FOREACH(q
, &tp
->t_segq
, tqe_q
) {
306 if (SEQ_GT(q
->tqe_th
->th_seq
, th
->th_seq
))
312 * If there is a preceding segment, it may provide some of
313 * our data already. If so, drop the data from the incoming
314 * segment. If it provides all of our data, drop us.
318 /* conversion to int (in i) handles seq wraparound */
319 i
= p
->tqe_th
->th_seq
+ p
->tqe_len
- th
->th_seq
;
322 tcpstat
.tcps_rcvduppack
++;
323 tcpstat
.tcps_rcvdupbyte
+= *tlenp
;
328 * Try to present any queued data
329 * at the left window edge to the user.
330 * This is needed after the 3-WHS
333 goto present
; /* ??? */
340 tcpstat
.tcps_rcvoopack
++;
341 tcpstat
.tcps_rcvoobyte
+= *tlenp
;
344 * While we overlap succeeding segments trim them or,
345 * if they are completely covered, dequeue them.
348 register int i
= (th
->th_seq
+ *tlenp
) - q
->tqe_th
->th_seq
;
351 if (i
< q
->tqe_len
) {
352 q
->tqe_th
->th_seq
+= i
;
358 nq
= LIST_NEXT(q
, tqe_q
);
359 LIST_REMOVE(q
, tqe_q
);
366 /* Insert the new segment queue entry into place. */
369 te
->tqe_len
= *tlenp
;
372 LIST_INSERT_HEAD(&tp
->t_segq
, te
, tqe_q
);
374 LIST_INSERT_AFTER(p
, te
, tqe_q
);
379 * Present data to user, advancing rcv_nxt through
380 * completed sequence space.
382 if (!TCPS_HAVEESTABLISHED(tp
->t_state
))
384 q
= LIST_FIRST(&tp
->t_segq
);
385 if (!q
|| q
->tqe_th
->th_seq
!= tp
->rcv_nxt
)
388 tp
->rcv_nxt
+= q
->tqe_len
;
389 flags
= q
->tqe_th
->th_flags
& TH_FIN
;
390 nq
= LIST_NEXT(q
, tqe_q
);
391 LIST_REMOVE(q
, tqe_q
);
392 if (so
->so_state
& SS_CANTRCVMORE
)
395 if (sbappend(&so
->so_rcv
, q
->tqe_m
))
401 } while (q
&& q
->tqe_th
->th_seq
== tp
->rcv_nxt
);
405 if ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0) {
407 KERNEL_DEBUG(DBG_LAYER_BEG
,
408 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
409 (((tp
->t_inpcb
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
410 (tp
->t_inpcb
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
416 KERNEL_DEBUG(DBG_LAYER_BEG
,
417 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
418 (((tp
->t_inpcb
->inp_laddr
.s_addr
& 0xffff) << 16) |
419 (tp
->t_inpcb
->inp_faddr
.s_addr
& 0xffff)),
423 sorwakeup(so
); /* done with socket lock held */
430 * TCP input routine, follows pages 65-76 of the
431 * protocol specification dated September, 1981 very closely.
439 register struct mbuf
*m
= *mp
;
440 struct in6_ifaddr
*ia6
;
442 IP6_EXTHDR_CHECK(m
, *offp
, sizeof(struct tcphdr
), return IPPROTO_DONE
);
445 * draft-itojun-ipv6-tcp-to-anycast
446 * better place to put this in?
448 ia6
= ip6_getdstifaddr(m
);
449 if (ia6
&& (ia6
->ia6_flags
& IN6_IFF_ANYCAST
)) {
452 ip6
= mtod(m
, struct ip6_hdr
*);
453 icmp6_error(m
, ICMP6_DST_UNREACH
, ICMP6_DST_UNREACH_ADDR
,
454 (caddr_t
)&ip6
->ip6_dst
- (caddr_t
)ip6
);
468 register struct tcphdr
*th
;
469 register struct ip
*ip
= NULL
;
470 register struct ipovly
*ipov
;
471 register struct inpcb
*inp
;
476 register struct tcpcb
*tp
= 0;
477 register int thflags
;
478 struct socket
*so
= 0;
479 int todrop
, acked
, ourfinisacked
, needoutput
= 0;
480 struct in_addr laddr
;
482 struct in6_addr laddr6
;
488 struct tcpopt to
; /* options in this segment */
489 struct rmxp_tao
*taop
; /* pointer to our TAO cache entry */
490 struct rmxp_tao tao_noncached
; /* in case there's no cached entry */
491 struct sockaddr_in
*next_hop
= NULL
;
495 struct m_tag
*fwd_tag
;
497 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
498 fwd_tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
, NULL
);
499 if (fwd_tag
!= NULL
) {
500 struct ip_fwd_tag
*ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
502 next_hop
= ipfwd_tag
->next_hop
;
503 m_tag_delete(m
, fwd_tag
);
507 struct ip6_hdr
*ip6
= NULL
;
510 int rstreason
; /* For badport_bandlim accounting purposes */
511 struct proc
*proc0
=current_proc();
513 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_START
,0,0,0,0,0);
516 isipv6
= (mtod(m
, struct ip
*)->ip_v
== 6) ? 1 : 0;
518 bzero((char *)&to
, sizeof(to
));
520 tcpstat
.tcps_rcvtotal
++;
526 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
527 ip6
= mtod(m
, struct ip6_hdr
*);
528 tlen
= sizeof(*ip6
) + ntohs(ip6
->ip6_plen
) - off0
;
529 if (in6_cksum(m
, IPPROTO_TCP
, off0
, tlen
)) {
530 tcpstat
.tcps_rcvbadsum
++;
533 th
= (struct tcphdr
*)((caddr_t
)ip6
+ off0
);
535 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
536 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
537 th
->th_seq
, th
->th_ack
, th
->th_win
);
539 * Be proactive about unspecified IPv6 address in source.
540 * As we use all-zero to indicate unbounded/unconnected pcb,
541 * unspecified IPv6 address can be used to confuse us.
543 * Note that packets with unspecified IPv6 destination is
544 * already dropped in ip6_input.
546 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
554 * Get IP and TCP header together in first mbuf.
555 * Note: IP leaves IP header in first mbuf.
557 if (off0
> sizeof (struct ip
)) {
558 ip_stripoptions(m
, (struct mbuf
*)0);
559 off0
= sizeof(struct ip
);
560 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
561 m
->m_pkthdr
.csum_flags
= 0; /* invalidate hwcksuming */
564 if (m
->m_len
< sizeof (struct tcpiphdr
)) {
565 if ((m
= m_pullup(m
, sizeof (struct tcpiphdr
))) == 0) {
566 tcpstat
.tcps_rcvshort
++;
570 ip
= mtod(m
, struct ip
*);
571 ipov
= (struct ipovly
*)ip
;
572 th
= (struct tcphdr
*)((caddr_t
)ip
+ off0
);
575 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
576 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
577 th
->th_seq
, th
->th_ack
, th
->th_win
);
579 if (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) {
580 if (apple_hwcksum_rx
&& (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)) {
583 *(uint32_t*)&b
[0] = *(uint32_t*)&ipov
->ih_x1
[0];
584 *(uint32_t*)&b
[4] = *(uint32_t*)&ipov
->ih_x1
[4];
585 *(uint8_t*)&b
[8] = *(uint8_t*)&ipov
->ih_x1
[8];
587 bzero(ipov
->ih_x1
, sizeof(ipov
->ih_x1
));
588 ipov
->ih_len
= (u_short
)tlen
;
590 pseudo
= in_cksum(m
, sizeof (struct ip
));
592 *(uint32_t*)&ipov
->ih_x1
[0] = *(uint32_t*)&b
[0];
593 *(uint32_t*)&ipov
->ih_x1
[4] = *(uint32_t*)&b
[4];
594 *(uint8_t*)&ipov
->ih_x1
[8] = *(uint8_t*)&b
[8];
596 th
->th_sum
= in_addword(pseudo
, (m
->m_pkthdr
.csum_data
& 0xFFFF));
598 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
)
599 th
->th_sum
= m
->m_pkthdr
.csum_data
;
601 th
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
,
602 ip
->ip_dst
.s_addr
, htonl(m
->m_pkthdr
.csum_data
+
603 ip
->ip_len
+ IPPROTO_TCP
));
605 th
->th_sum
^= 0xffff;
609 * Checksum extended TCP header and data.
611 *(uint32_t*)&b
[0] = *(uint32_t*)&ipov
->ih_x1
[0];
612 *(uint32_t*)&b
[4] = *(uint32_t*)&ipov
->ih_x1
[4];
613 *(uint8_t*)&b
[8] = *(uint8_t*)&ipov
->ih_x1
[8];
615 len
= sizeof (struct ip
) + tlen
;
616 bzero(ipov
->ih_x1
, sizeof(ipov
->ih_x1
));
617 ipov
->ih_len
= (u_short
)tlen
;
619 th
->th_sum
= in_cksum(m
, len
);
621 *(uint32_t*)&ipov
->ih_x1
[0] = *(uint32_t*)&b
[0];
622 *(uint32_t*)&ipov
->ih_x1
[4] = *(uint32_t*)&b
[4];
623 *(uint8_t*)&ipov
->ih_x1
[8] = *(uint8_t*)&b
[8];
626 tcpstat
.tcps_rcvbadsum
++;
630 /* Re-initialization for later version check */
631 ip
->ip_v
= IPVERSION
;
636 * Check that TCP offset makes sense,
637 * pull out TCP options and adjust length. XXX
639 off
= th
->th_off
<< 2;
640 if (off
< sizeof (struct tcphdr
) || off
> tlen
) {
641 tcpstat
.tcps_rcvbadoff
++;
644 tlen
-= off
; /* tlen is used instead of ti->ti_len */
645 if (off
> sizeof (struct tcphdr
)) {
648 IP6_EXTHDR_CHECK(m
, off0
, off
, return);
649 ip6
= mtod(m
, struct ip6_hdr
*);
650 th
= (struct tcphdr
*)((caddr_t
)ip6
+ off0
);
654 if (m
->m_len
< sizeof(struct ip
) + off
) {
655 if ((m
= m_pullup(m
, sizeof (struct ip
) + off
)) == 0) {
656 tcpstat
.tcps_rcvshort
++;
659 ip
= mtod(m
, struct ip
*);
660 ipov
= (struct ipovly
*)ip
;
661 th
= (struct tcphdr
*)((caddr_t
)ip
+ off0
);
664 optlen
= off
- sizeof (struct tcphdr
);
665 optp
= (u_char
*)(th
+ 1);
667 * Do quick retrieval of timestamp options ("options
668 * prediction?"). If timestamp is the only option and it's
669 * formatted as recommended in RFC 1323 appendix A, we
670 * quickly get the values now and not bother calling
671 * tcp_dooptions(), etc.
673 if ((optlen
== TCPOLEN_TSTAMP_APPA
||
674 (optlen
> TCPOLEN_TSTAMP_APPA
&&
675 optp
[TCPOLEN_TSTAMP_APPA
] == TCPOPT_EOL
)) &&
676 *(u_int32_t
*)optp
== htonl(TCPOPT_TSTAMP_HDR
) &&
677 (th
->th_flags
& TH_SYN
) == 0) {
678 to
.to_flag
|= TOF_TS
;
679 to
.to_tsval
= ntohl(*(u_int32_t
*)(optp
+ 4));
680 to
.to_tsecr
= ntohl(*(u_int32_t
*)(optp
+ 8));
681 optp
= NULL
; /* we've parsed the options */
684 thflags
= th
->th_flags
;
688 * If the drop_synfin option is enabled, drop all packets with
689 * both the SYN and FIN bits set. This prevents e.g. nmap from
690 * identifying the TCP/IP stack.
692 * This is incompatible with RFC1644 extensions (T/TCP).
694 if (drop_synfin
&& (thflags
& (TH_SYN
|TH_FIN
)) == (TH_SYN
|TH_FIN
))
699 * Convert TCP protocol specific fields to host format.
707 * Delay droping TCP, IP headers, IPv6 ext headers, and TCP options,
708 * until after ip6_savecontrol() is called and before other functions
709 * which don't want those proto headers.
710 * Because ip6_savecontrol() is going to parse the mbuf to
711 * search for data to be passed up to user-land, it wants mbuf
712 * parameters to be unchanged.
714 drop_hdrlen
= off0
+ off
;
717 * Locate pcb for segment.
720 #if IPFIREWALL_FORWARD
723 && isipv6
== NULL
/* IPv6 support is not yet */
727 * Diverted. Pretend to be the destination.
728 * already got one like this?
730 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
731 ip
->ip_dst
, th
->th_dport
, 0, m
->m_pkthdr
.rcvif
);
734 * No, then it's new. Try find the ambushing socket
736 if (!next_hop
->sin_port
) {
737 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
,
738 th
->th_sport
, next_hop
->sin_addr
,
739 th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
741 inp
= in_pcblookup_hash(&tcbinfo
,
742 ip
->ip_src
, th
->th_sport
,
744 ntohs(next_hop
->sin_port
), 1,
749 #endif /* IPFIREWALL_FORWARD */
753 inp
= in6_pcblookup_hash(&tcbinfo
, &ip6
->ip6_src
, th
->th_sport
,
754 &ip6
->ip6_dst
, th
->th_dport
, 1,
758 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
759 ip
->ip_dst
, th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
763 if (ipsec_bypass
== 0) {
764 lck_mtx_lock(sadb_mutex
);
767 if (inp
!= NULL
&& ipsec6_in_reject_so(m
, inp
->inp_socket
)) {
768 ipsec6stat
.in_polvio
++;
769 lck_mtx_unlock(sadb_mutex
);
774 if (inp
!= NULL
&& ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
775 ipsecstat
.in_polvio
++;
776 lck_mtx_unlock(sadb_mutex
);
779 lck_mtx_unlock(sadb_mutex
);
784 * If the state is CLOSED (i.e., TCB does not exist) then
785 * all data in the incoming segment is discarded.
786 * If the TCB exists but is in CLOSED state, it is embryonic,
787 * but should either do a listen or a connect soon.
792 char dbuf
[MAX_IPv6_STR_LEN
], sbuf
[MAX_IPv6_STR_LEN
];
794 char dbuf
[MAX_IPv4_STR_LEN
], sbuf
[MAX_IPv4_STR_LEN
];
799 inet_ntop(AF_INET6
, &ip6
->ip6_dst
, dbuf
, sizeof(dbuf
));
800 inet_ntop(AF_INET6
, &ip6
->ip6_src
, sbuf
, sizeof(sbuf
));
804 inet_ntop(AF_INET
, &ip
->ip_dst
, dbuf
, sizeof(dbuf
));
805 inet_ntop(AF_INET
, &ip
->ip_src
, sbuf
, sizeof(sbuf
));
807 switch (log_in_vain
) {
811 "Connection attempt to TCP %s:%d from %s:%d\n",
812 dbuf
, ntohs(th
->th_dport
),
814 ntohs(th
->th_sport
));
818 "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
819 dbuf
, ntohs(th
->th_dport
), sbuf
,
820 ntohs(th
->th_sport
), thflags
);
823 if ((thflags
& TH_SYN
) &&
824 !(m
->m_flags
& (M_BCAST
| M_MCAST
)) &&
826 ((isipv6
&& !IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
, &ip6
->ip6_src
)) ||
827 (!isipv6
&& ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
))
829 ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
832 log_in_vain_log((LOG_INFO
,
833 "Stealth Mode connection attempt to TCP %s:%d from %s:%d\n",
834 dbuf
, ntohs(th
->th_dport
),
836 ntohs(th
->th_sport
)));
843 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
)
846 if (thflags
& TH_SYN
)
855 rstreason
= BANDLIM_RST_CLOSEDPORT
;
856 goto dropwithresetnosock
;
858 so
= inp
->inp_socket
;
860 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
)
861 inp
= NULL
; // pretend we didn't find it
863 printf("tcp_input: no more socket for inp=%x\n", inp
);
868 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
869 tcp_unlock(so
, 1, 2);
870 inp
= NULL
; // pretend we didn't find it
876 rstreason
= BANDLIM_RST_CLOSEDPORT
;
879 if (tp
->t_state
== TCPS_CLOSED
)
884 * Bogus state when listening port owned by SharedIP with loopback as the
885 * only configured interface: BlueBox does not filters loopback
887 if (tp
->t_state
== TCP_NSTATES
)
891 /* Unscale the window into a 32-bit value. */
892 if ((thflags
& TH_SYN
) == 0)
893 tiwin
= th
->th_win
<< tp
->snd_scale
;
897 if (so
->so_options
& (SO_DEBUG
|SO_ACCEPTCONN
)) {
899 if (so
->so_options
& SO_DEBUG
) {
900 ostate
= tp
->t_state
;
903 bcopy((char *)ip6
, (char *)tcp_saveipgen
,
907 bcopy((char *)ip
, (char *)tcp_saveipgen
, sizeof(*ip
));
911 if (so
->so_options
& SO_ACCEPTCONN
) {
912 register struct tcpcb
*tp0
= tp
;
915 struct sockaddr_storage from
;
917 struct inpcb
*oinp
= sotoinpcb(so
);
919 int ogencnt
= so
->so_gencnt
;
923 * Current IPsec implementation makes incorrect IPsec
924 * cache if this check is done here.
925 * So delay this until duplicated socket is created.
927 if ((thflags
& (TH_RST
|TH_ACK
|TH_SYN
)) != TH_SYN
) {
929 * Note: dropwithreset makes sure we don't
930 * send a RST in response to a RST.
932 if (thflags
& TH_ACK
) {
933 tcpstat
.tcps_badsyn
++;
934 rstreason
= BANDLIM_RST_OPENPORT
;
940 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_START
,0,0,0,0,0);
944 * If deprecated address is forbidden,
945 * we do not accept SYN to deprecated interface
946 * address to prevent any new inbound connection from
947 * getting established.
948 * When we do not accept SYN, we send a TCP RST,
949 * with deprecated source address (instead of dropping
950 * it). We compromise it as it is much better for peer
951 * to send a RST, and RST will be the final packet
954 * If we do not forbid deprecated addresses, we accept
955 * the SYN packet. RFC2462 does not suggest dropping
957 * If we decipher RFC2462 5.5.4, it says like this:
958 * 1. use of deprecated addr with existing
959 * communication is okay - "SHOULD continue to be
961 * 2. use of it with new communication:
962 * (2a) "SHOULD NOT be used if alternate address
963 * with sufficient scope is available"
964 * (2b) nothing mentioned otherwise.
965 * Here we fall into (2b) case as we have no choice in
966 * our source address selection - we must obey the peer.
968 * The wording in RFC2462 is confusing, and there are
969 * multiple description text for deprecated address
970 * handling - worse, they are not exactly the same.
971 * I believe 5.5.4 is the best one, so we follow 5.5.4.
973 if (isipv6
&& !ip6_use_deprecated
) {
974 struct in6_ifaddr
*ia6
;
976 if ((ia6
= ip6_getdstifaddr(m
)) &&
977 (ia6
->ia6_flags
& IN6_IFF_DEPRECATED
)) {
979 rstreason
= BANDLIM_RST_OPENPORT
;
986 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&from
;
988 sin6
->sin6_len
= sizeof(*sin6
);
989 sin6
->sin6_family
= AF_INET6
;
990 sin6
->sin6_port
= th
->th_sport
;
991 sin6
->sin6_flowinfo
= 0;
992 sin6
->sin6_addr
= ip6
->ip6_src
;
993 sin6
->sin6_scope_id
= 0;
995 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&from
;
997 sin
->sin_len
= sizeof(*sin
);
998 sin
->sin_family
= AF_INET
;
999 sin
->sin_port
= th
->th_sport
;
1000 sin
->sin_addr
= ip
->ip_src
;
1002 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
1004 so2
= sonewconn(so
, 0, NULL
);
1007 tcpstat
.tcps_listendrop
++;
1008 if (tcpdropdropablreq(so
)) {
1010 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
1012 so2
= sonewconn(so
, 0, NULL
);
1018 * Make sure listening socket did not get closed during socket allocation,
1019 * not only this is incorrect but it is know to cause panic
1021 if (so
->so_gencnt
!= ogencnt
)
1025 tcp_unlock(so
, 0, 0); /* Unlock but keep a reference on listener for now */
1030 * This is ugly, but ....
1032 * Mark socket as temporary until we're
1033 * committed to keeping it. The code at
1034 * ``drop'' and ``dropwithreset'' check the
1035 * flag dropsocket to see if the temporary
1036 * socket created here should be discarded.
1037 * We mark the socket as discardable until
1038 * we're committed to it below in TCPS_LISTEN.
1041 inp
= (struct inpcb
*)so
->so_pcb
;
1044 inp
->in6p_laddr
= ip6
->ip6_dst
;
1046 inp
->inp_vflag
&= ~INP_IPV6
;
1047 inp
->inp_vflag
|= INP_IPV4
;
1049 inp
->inp_laddr
= ip
->ip_dst
;
1053 inp
->inp_lport
= th
->th_dport
;
1054 if (in_pcbinshash(inp
, 0) != 0) {
1056 * Undo the assignments above if we failed to
1057 * put the PCB on the hash lists.
1061 inp
->in6p_laddr
= in6addr_any
;
1064 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
1066 tcp_lock(oso
, 0, 0); /* release ref on parent */
1067 tcp_unlock(oso
, 1, 0);
1072 * To avoid creating incorrectly cached IPsec
1073 * association, this is need to be done here.
1075 * Subject: (KAME-snap 748)
1076 * From: Wayne Knowles <w.knowles@niwa.cri.nz>
1077 * ftp://ftp.kame.net/pub/mail-list/snap-users/748
1079 if ((thflags
& (TH_RST
|TH_ACK
|TH_SYN
)) != TH_SYN
) {
1081 * Note: dropwithreset makes sure we don't
1082 * send a RST in response to a RST.
1084 tcp_lock(oso
, 0, 0); /* release ref on parent */
1085 tcp_unlock(oso
, 1, 0);
1086 if (thflags
& TH_ACK
) {
1087 tcpstat
.tcps_badsyn
++;
1088 rstreason
= BANDLIM_RST_OPENPORT
;
1097 * Inherit socket options from the listening
1099 * Note that in6p_inputopts are not (even
1100 * should not be) copied, since it stores
1101 * previously received options and is used to
1102 * detect if each new option is different than
1103 * the previous one and hence should be passed
1105 * If we copied in6p_inputopts, a user would
1106 * not be able to receive options just after
1107 * calling the accept system call.
1110 oinp
->inp_flags
& INP_CONTROLOPTS
;
1111 if (oinp
->in6p_outputopts
)
1112 inp
->in6p_outputopts
=
1113 ip6_copypktopts(oinp
->in6p_outputopts
,
1117 inp
->inp_options
= ip_srcroute();
1118 tcp_lock(oso
, 0, 0);
1120 /* copy old policy into new socket's */
1121 if (sotoinpcb(oso
)->inp_sp
)
1124 lck_mtx_lock(sadb_mutex
);
1125 /* Is it a security hole here to silently fail to copy the policy? */
1126 if (inp
->inp_sp
!= NULL
)
1127 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
1128 if (error
!= 0 || ipsec_copy_policy(sotoinpcb(oso
)->inp_sp
, inp
->inp_sp
))
1129 printf("tcp_input: could not copy policy\n");
1130 lck_mtx_unlock(sadb_mutex
);
1133 tcp_unlock(oso
, 1, 0); /* now drop the reference on the listener */
1134 tp
= intotcpcb(inp
);
1135 tp
->t_state
= TCPS_LISTEN
;
1136 tp
->t_flags
|= tp0
->t_flags
& (TF_NOPUSH
|TF_NOOPT
|TF_NODELAY
);
1137 tp
->t_inpcb
->inp_ip_ttl
= tp0
->t_inpcb
->inp_ip_ttl
;
1138 /* Compute proper scaling value from buffer space */
1139 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
1140 TCP_MAXWIN
<< tp
->request_r_scale
<
1141 so
->so_rcv
.sb_hiwat
)
1142 tp
->request_r_scale
++;
1144 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_END
,0,0,0,0,0);
1149 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1153 * This is the second part of the MSS DoS prevention code (after
1154 * minmss on the sending side) and it deals with too many too small
1155 * tcp packets in a too short timeframe (1 second).
1157 * For every full second we count the number of received packets
1158 * and bytes. If we get a lot of packets per second for this connection
1159 * (tcp_minmssoverload) we take a closer look at it and compute the
1160 * average packet size for the past second. If that is less than
1161 * tcp_minmss we get too many packets with very small payload which
1162 * is not good and burdens our system (and every packet generates
1163 * a wakeup to the process connected to our socket). We can reasonable
1164 * expect this to be small packet DoS attack to exhaust our CPU
1167 * Care has to be taken for the minimum packet overload value. This
1168 * value defines the minimum number of packets per second before we
1169 * start to worry. This must not be too low to avoid killing for
1170 * example interactive connections with many small packets like
1174 * Account for packet if payload packet, skip over ACK, etc.
1176 * The packet per second count is done all the time and is also used
1177 * by "DELAY_ACK" to detect streaming situations.
1180 if (tp
->t_state
== TCPS_ESTABLISHED
&& tlen
> 0) {
1181 if (tp
->rcv_reset
> tcp_now
) {
1183 tp
->rcv_byps
+= tlen
+ off
;
1185 * Setting either tcp_minmssoverload or tcp_minmss to "0" disables
1188 if (tcp_minmss
&& tcp_minmssoverload
&& tp
->rcv_pps
> tcp_minmssoverload
) {
1189 if ((tp
->rcv_byps
/ tp
->rcv_pps
) < tcp_minmss
) {
1190 char ipstrbuf
[MAX_IPv6_STR_LEN
];
1191 printf("too many small tcp packets from "
1192 "%s:%u, av. %lubyte/packet, "
1193 "dropping connection\n",
1196 inet_ntop(AF_INET6
, &inp
->in6p_faddr
, ipstrbuf
,
1199 inet_ntop(AF_INET
, &inp
->inp_faddr
, ipstrbuf
,
1202 tp
->rcv_byps
/ tp
->rcv_pps
);
1203 tp
= tcp_drop(tp
, ECONNRESET
);
1204 /* tcpstat.tcps_minmssdrops++; */
1209 tp
->rcv_reset
= tcp_now
+ PR_SLOWHZ
;
1211 tp
->rcv_byps
= tlen
+ off
;
1216 * Segment received on connection.
1217 * Reset idle time and keep-alive timer.
1220 if (TCPS_HAVEESTABLISHED(tp
->t_state
))
1221 tp
->t_timer
[TCPT_KEEP
] = TCP_KEEPIDLE(tp
);
1224 * Process options if not in LISTEN state,
1225 * else do it below (after getting remote address).
1227 if (tp
->t_state
!= TCPS_LISTEN
&& optp
)
1228 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
1231 * Header prediction: check for the two common cases
1232 * of a uni-directional data xfer. If the packet has
1233 * no control flags, is in-sequence, the window didn't
1234 * change and we're not retransmitting, it's a
1235 * candidate. If the length is zero and the ack moved
1236 * forward, we're the sender side of the xfer. Just
1237 * free the data acked & wake any higher level process
1238 * that was blocked waiting for space. If the length
1239 * is non-zero and the ack didn't move, we're the
1240 * receiver side. If we're getting packets in-order
1241 * (the reassembly queue is empty), add the data to
1242 * the socket buffer and note that we need a delayed ack.
1243 * Make sure that the hidden state-flags are also off.
1244 * Since we check for TCPS_ESTABLISHED above, it can only
1247 if (tp
->t_state
== TCPS_ESTABLISHED
&&
1248 (thflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
)) == TH_ACK
&&
1249 ((tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
)) == 0) &&
1250 ((to
.to_flag
& TOF_TS
) == 0 ||
1251 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
1253 * Using the CC option is compulsory if once started:
1254 * the segment is OK if no T/TCP was negotiated or
1255 * if the segment has a CC option equal to CCrecv
1257 ((tp
->t_flags
& (TF_REQ_CC
|TF_RCVD_CC
)) != (TF_REQ_CC
|TF_RCVD_CC
) ||
1258 ((to
.to_flag
& TOF_CC
) != 0 && to
.to_cc
== tp
->cc_recv
)) &&
1259 th
->th_seq
== tp
->rcv_nxt
&&
1260 tiwin
&& tiwin
== tp
->snd_wnd
&&
1261 tp
->snd_nxt
== tp
->snd_max
) {
1264 * If last ACK falls within this segment's sequence numbers,
1265 * record the timestamp.
1266 * NOTE that the test is modified according to the latest
1267 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1269 if ((to
.to_flag
& TOF_TS
) != 0 &&
1270 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
1271 tp
->ts_recent_age
= tcp_now
;
1272 tp
->ts_recent
= to
.to_tsval
;
1276 if (SEQ_GT(th
->th_ack
, tp
->snd_una
) &&
1277 SEQ_LEQ(th
->th_ack
, tp
->snd_max
) &&
1278 tp
->snd_cwnd
>= tp
->snd_wnd
&&
1279 tp
->t_dupacks
< tcprexmtthresh
) {
1281 * this is a pure ack for outstanding data.
1283 ++tcpstat
.tcps_predack
;
1285 * "bad retransmit" recovery
1287 if (tp
->t_rxtshift
== 1 &&
1288 tcp_now
< tp
->t_badrxtwin
) {
1289 tp
->snd_cwnd
= tp
->snd_cwnd_prev
;
1291 tp
->snd_ssthresh_prev
;
1292 tp
->snd_nxt
= tp
->snd_max
;
1293 tp
->t_badrxtwin
= 0;
1295 if (((to
.to_flag
& TOF_TS
) != 0) && (to
.to_tsecr
!= 0)) /* Makes sure we already have a TS */
1297 tcp_now
- to
.to_tsecr
+ 1);
1298 else if (tp
->t_rtttime
&&
1299 SEQ_GT(th
->th_ack
, tp
->t_rtseq
))
1300 tcp_xmit_timer(tp
, tp
->t_rtttime
);
1301 acked
= th
->th_ack
- tp
->snd_una
;
1302 tcpstat
.tcps_rcvackpack
++;
1303 tcpstat
.tcps_rcvackbyte
+= acked
;
1304 sbdrop(&so
->so_snd
, acked
);
1305 tp
->snd_una
= th
->th_ack
;
1307 ND6_HINT(tp
); /* some progress has been done */
1310 * If all outstanding data are acked, stop
1311 * retransmit timer, otherwise restart timer
1312 * using current (possibly backed-off) value.
1313 * If process is waiting for space,
1314 * wakeup/selwakeup/signal. If data
1315 * are ready to send, let tcp_output
1316 * decide between more output or persist.
1318 if (tp
->snd_una
== tp
->snd_max
)
1319 tp
->t_timer
[TCPT_REXMT
] = 0;
1320 else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
1321 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
1323 sowwakeup(so
); /* has to be done with socket lock held */
1324 if ((so
->so_snd
.sb_cc
) || (tp
->t_flags
& TF_ACKNOW
))
1325 (void) tcp_output(tp
);
1326 tcp_unlock(so
, 1, 0);
1327 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
1330 } else if (th
->th_ack
== tp
->snd_una
&&
1331 LIST_EMPTY(&tp
->t_segq
) &&
1332 tlen
<= sbspace(&so
->so_rcv
)) {
1334 * this is a pure, in-sequence data packet
1335 * with nothing on the reassembly queue and
1336 * we have enough buffer space to take it.
1338 ++tcpstat
.tcps_preddat
;
1339 tp
->rcv_nxt
+= tlen
;
1340 tcpstat
.tcps_rcvpack
++;
1341 tcpstat
.tcps_rcvbyte
+= tlen
;
1342 ND6_HINT(tp
); /* some progress has been done */
1344 * Add data to socket buffer.
1346 m_adj(m
, drop_hdrlen
); /* delayed header drop */
1347 if (sbappend(&so
->so_rcv
, m
))
1351 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
1352 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
1353 th
->th_seq
, th
->th_ack
, th
->th_win
);
1358 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
1359 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
1360 th
->th_seq
, th
->th_ack
, th
->th_win
);
1362 if (DELAY_ACK(tp
)) {
1363 tp
->t_flags
|= TF_DELACK
;
1365 tp
->t_flags
|= TF_ACKNOW
;
1368 tcp_unlock(so
, 1, 0);
1369 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
1375 * Calculate amount of space in receive window,
1376 * and then do TCP input processing.
1377 * Receive window is amount of space in rcv queue,
1378 * but not less than advertised window.
1381 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1385 win
= sbspace(&so
->so_rcv
);
1388 else { /* clip rcv window to 4K for modems */
1389 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0)
1390 win
= min(win
, slowlink_wsize
);
1392 tp
->rcv_wnd
= imax(win
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
1395 switch (tp
->t_state
) {
1398 * If the state is LISTEN then ignore segment if it contains an RST.
1399 * If the segment contains an ACK then it is bad and send a RST.
1400 * If it does not contain a SYN then it is not interesting; drop it.
1401 * If it is from this socket, drop it, it must be forged.
1402 * Don't bother responding if the destination was a broadcast.
1403 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
1404 * tp->iss, and send a segment:
1405 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1406 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1407 * Fill in remote peer address fields if not previously specified.
1408 * Enter SYN_RECEIVED state, and process any other fields of this
1409 * segment in this state.
1412 register struct sockaddr_in
*sin
;
1414 register struct sockaddr_in6
*sin6
;
1418 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1420 if (thflags
& TH_RST
)
1422 if (thflags
& TH_ACK
) {
1423 rstreason
= BANDLIM_RST_OPENPORT
;
1426 if ((thflags
& TH_SYN
) == 0)
1428 if (th
->th_dport
== th
->th_sport
) {
1431 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
1436 if (ip
->ip_dst
.s_addr
== ip
->ip_src
.s_addr
)
1440 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
1441 * in_broadcast() should never return true on a received
1442 * packet with M_BCAST not set.
1444 * Packets with a multicast source address should also
1447 if (m
->m_flags
& (M_BCAST
|M_MCAST
))
1451 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
1452 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
))
1456 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
1457 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
1458 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
1459 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
))
1463 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
,
1464 M_SONAME
, M_NOWAIT
);
1467 bzero(sin6
, sizeof(*sin6
));
1468 sin6
->sin6_family
= AF_INET6
;
1469 sin6
->sin6_len
= sizeof(*sin6
);
1470 sin6
->sin6_addr
= ip6
->ip6_src
;
1471 sin6
->sin6_port
= th
->th_sport
;
1472 laddr6
= inp
->in6p_laddr
;
1473 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1474 inp
->in6p_laddr
= ip6
->ip6_dst
;
1475 if (in6_pcbconnect(inp
, (struct sockaddr
*)sin6
,
1477 inp
->in6p_laddr
= laddr6
;
1478 FREE(sin6
, M_SONAME
);
1481 FREE(sin6
, M_SONAME
);
1486 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1488 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
,
1492 sin
->sin_family
= AF_INET
;
1493 sin
->sin_len
= sizeof(*sin
);
1494 sin
->sin_addr
= ip
->ip_src
;
1495 sin
->sin_port
= th
->th_sport
;
1496 bzero((caddr_t
)sin
->sin_zero
, sizeof(sin
->sin_zero
));
1497 laddr
= inp
->inp_laddr
;
1498 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
1499 inp
->inp_laddr
= ip
->ip_dst
;
1500 if (in_pcbconnect(inp
, (struct sockaddr
*)sin
, proc0
)) {
1501 inp
->inp_laddr
= laddr
;
1502 FREE(sin
, M_SONAME
);
1505 FREE(sin
, M_SONAME
);
1507 if ((taop
= tcp_gettaocache(inp
)) == NULL
) {
1508 taop
= &tao_noncached
;
1509 bzero(taop
, sizeof(*taop
));
1511 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
1515 tp
->iss
= tcp_new_isn(tp
);
1517 tp
->irs
= th
->th_seq
;
1518 tcp_sendseqinit(tp
);
1520 tp
->snd_recover
= tp
->snd_una
;
1522 * Initialization of the tcpcb for transaction;
1523 * set SND.WND = SEG.WND,
1524 * initialize CCsend and CCrecv.
1526 tp
->snd_wnd
= tiwin
; /* initial send-window */
1527 tp
->cc_send
= CC_INC(tcp_ccgen
);
1528 tp
->cc_recv
= to
.to_cc
;
1530 * Perform TAO test on incoming CC (SEG.CC) option, if any.
1531 * - compare SEG.CC against cached CC from the same host,
1533 * - if SEG.CC > chached value, SYN must be new and is accepted
1534 * immediately: save new CC in the cache, mark the socket
1535 * connected, enter ESTABLISHED state, turn on flag to
1536 * send a SYN in the next segment.
1537 * A virtual advertised window is set in rcv_adv to
1538 * initialize SWS prevention. Then enter normal segment
1539 * processing: drop SYN, process data and FIN.
1540 * - otherwise do a normal 3-way handshake.
1542 if ((to
.to_flag
& TOF_CC
) != 0) {
1543 if (((tp
->t_flags
& TF_NOPUSH
) != 0) &&
1544 taop
->tao_cc
!= 0 && CC_GT(to
.to_cc
, taop
->tao_cc
)) {
1546 taop
->tao_cc
= to
.to_cc
;
1548 tp
->t_state
= TCPS_ESTABLISHED
;
1551 * If there is a FIN, or if there is data and the
1552 * connection is local, then delay SYN,ACK(SYN) in
1553 * the hope of piggy-backing it on a response
1554 * segment. Otherwise must send ACK now in case
1555 * the other side is slow starting.
1557 if (DELAY_ACK(tp
) && ((thflags
& TH_FIN
) ||
1560 (isipv6
&& in6_localaddr(&inp
->in6p_faddr
))
1564 in_localaddr(inp
->inp_faddr
)
1569 tp
->t_flags
|= (TF_DELACK
| TF_NEEDSYN
);
1572 tp
->t_flags
|= (TF_ACKNOW
| TF_NEEDSYN
);
1576 * Limit the `virtual advertised window' to TCP_MAXWIN
1577 * here. Even if we requested window scaling, it will
1578 * become effective only later when our SYN is acked.
1580 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0) /* clip window size for for slow link */
1581 tp
->rcv_adv
+= min(tp
->rcv_wnd
, slowlink_wsize
);
1583 tp
->rcv_adv
+= min(tp
->rcv_wnd
, TCP_MAXWIN
);
1584 tcpstat
.tcps_connects
++;
1586 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
1587 dropsocket
= 0; /* committed to socket */
1588 tcpstat
.tcps_accepts
++;
1591 /* else do standard 3-way handshake */
1594 * No CC option, but maybe CC.NEW:
1595 * invalidate cached value.
1600 * TAO test failed or there was no CC option,
1601 * do a standard 3-way handshake.
1603 tp
->t_flags
|= TF_ACKNOW
;
1604 tp
->t_state
= TCPS_SYN_RECEIVED
;
1605 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
1606 dropsocket
= 0; /* committed to socket */
1607 tcpstat
.tcps_accepts
++;
1612 * If the state is SYN_RECEIVED:
1613 * if seg contains an ACK, but not for our SYN/ACK, send a RST.
1615 case TCPS_SYN_RECEIVED
:
1616 if ((thflags
& TH_ACK
) &&
1617 (SEQ_LEQ(th
->th_ack
, tp
->snd_una
) ||
1618 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
1619 rstreason
= BANDLIM_RST_OPENPORT
;
1625 * If the state is SYN_SENT:
1626 * if seg contains an ACK, but not for our SYN, drop the input.
1627 * if seg contains a RST, then drop the connection.
1628 * if seg does not contain SYN, then drop it.
1629 * Otherwise this is an acceptable SYN segment
1630 * initialize tp->rcv_nxt and tp->irs
1631 * if seg contains ack then advance tp->snd_una
1632 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1633 * arrange for segment to be acked (eventually)
1634 * continue processing rest of data/controls, beginning with URG
1637 if ((taop
= tcp_gettaocache(inp
)) == NULL
) {
1638 taop
= &tao_noncached
;
1639 bzero(taop
, sizeof(*taop
));
1642 if ((thflags
& TH_ACK
) &&
1643 (SEQ_LEQ(th
->th_ack
, tp
->iss
) ||
1644 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
1646 * If we have a cached CCsent for the remote host,
1647 * hence we haven't just crashed and restarted,
1648 * do not send a RST. This may be a retransmission
1649 * from the other side after our earlier ACK was lost.
1650 * Our new SYN, when it arrives, will serve as the
1653 if (taop
->tao_ccsent
!= 0)
1656 rstreason
= BANDLIM_UNLIMITED
;
1660 if (thflags
& TH_RST
) {
1661 if (thflags
& TH_ACK
) {
1662 tp
= tcp_drop(tp
, ECONNREFUSED
);
1663 postevent(so
, 0, EV_RESET
);
1667 if ((thflags
& TH_SYN
) == 0)
1669 tp
->snd_wnd
= th
->th_win
; /* initial send window */
1670 tp
->cc_recv
= to
.to_cc
; /* foreign CC */
1672 tp
->irs
= th
->th_seq
;
1674 if (thflags
& TH_ACK
) {
1676 * Our SYN was acked. If segment contains CC.ECHO
1677 * option, check it to make sure this segment really
1678 * matches our SYN. If not, just drop it as old
1679 * duplicate, but send an RST if we're still playing
1680 * by the old rules. If no CC.ECHO option, make sure
1681 * we don't get fooled into using T/TCP.
1683 if (to
.to_flag
& TOF_CCECHO
) {
1684 if (tp
->cc_send
!= to
.to_ccecho
) {
1685 if (taop
->tao_ccsent
!= 0)
1688 rstreason
= BANDLIM_UNLIMITED
;
1693 tp
->t_flags
&= ~TF_RCVD_CC
;
1694 tcpstat
.tcps_connects
++;
1696 /* Do window scaling on this connection? */
1697 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
1698 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
1699 tp
->snd_scale
= tp
->requested_s_scale
;
1700 tp
->rcv_scale
= tp
->request_r_scale
;
1702 /* Segment is acceptable, update cache if undefined. */
1703 if (taop
->tao_ccsent
== 0)
1704 taop
->tao_ccsent
= to
.to_ccecho
;
1706 tp
->rcv_adv
+= tp
->rcv_wnd
;
1707 tp
->snd_una
++; /* SYN is acked */
1709 * If there's data, delay ACK; if there's also a FIN
1710 * ACKNOW will be turned on later.
1712 if (DELAY_ACK(tp
) && tlen
!= 0) {
1713 tp
->t_flags
|= TF_DELACK
;
1716 tp
->t_flags
|= TF_ACKNOW
;
1719 * Received <SYN,ACK> in SYN_SENT[*] state.
1721 * SYN_SENT --> ESTABLISHED
1722 * SYN_SENT* --> FIN_WAIT_1
1724 if (tp
->t_flags
& TF_NEEDFIN
) {
1725 tp
->t_state
= TCPS_FIN_WAIT_1
;
1726 tp
->t_flags
&= ~TF_NEEDFIN
;
1729 tp
->t_state
= TCPS_ESTABLISHED
;
1730 tp
->t_timer
[TCPT_KEEP
] = TCP_KEEPIDLE(tp
);
1734 * Received initial SYN in SYN-SENT[*] state => simul-
1735 * taneous open. If segment contains CC option and there is
1736 * a cached CC, apply TAO test; if it succeeds, connection is
1737 * half-synchronized. Otherwise, do 3-way handshake:
1738 * SYN-SENT -> SYN-RECEIVED
1739 * SYN-SENT* -> SYN-RECEIVED*
1740 * If there was no CC option, clear cached CC value.
1742 tp
->t_flags
|= TF_ACKNOW
;
1743 tp
->t_timer
[TCPT_REXMT
] = 0;
1744 if (to
.to_flag
& TOF_CC
) {
1745 if (taop
->tao_cc
!= 0 &&
1746 CC_GT(to
.to_cc
, taop
->tao_cc
)) {
1748 * update cache and make transition:
1749 * SYN-SENT -> ESTABLISHED*
1750 * SYN-SENT* -> FIN-WAIT-1*
1752 taop
->tao_cc
= to
.to_cc
;
1753 if (tp
->t_flags
& TF_NEEDFIN
) {
1754 tp
->t_state
= TCPS_FIN_WAIT_1
;
1755 tp
->t_flags
&= ~TF_NEEDFIN
;
1757 tp
->t_state
= TCPS_ESTABLISHED
;
1758 tp
->t_timer
[TCPT_KEEP
] = TCP_KEEPIDLE(tp
);
1760 tp
->t_flags
|= TF_NEEDSYN
;
1762 tp
->t_state
= TCPS_SYN_RECEIVED
;
1764 /* CC.NEW or no option => invalidate cache */
1766 tp
->t_state
= TCPS_SYN_RECEIVED
;
1772 * Advance th->th_seq to correspond to first data byte.
1773 * If data, trim to stay within window,
1774 * dropping FIN if necessary.
1777 if (tlen
> tp
->rcv_wnd
) {
1778 todrop
= tlen
- tp
->rcv_wnd
;
1782 tcpstat
.tcps_rcvpackafterwin
++;
1783 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
1785 tp
->snd_wl1
= th
->th_seq
- 1;
1786 tp
->rcv_up
= th
->th_seq
;
1788 * Client side of transaction: already sent SYN and data.
1789 * If the remote host used T/TCP to validate the SYN,
1790 * our data will be ACK'd; if so, enter normal data segment
1791 * processing in the middle of step 5, ack processing.
1792 * Otherwise, goto step 6.
1794 if (thflags
& TH_ACK
)
1798 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1799 * if segment contains a SYN and CC [not CC.NEW] option:
1800 * if state == TIME_WAIT and connection duration > MSL,
1801 * drop packet and send RST;
1803 * if SEG.CC > CCrecv then is new SYN, and can implicitly
1804 * ack the FIN (and data) in retransmission queue.
1805 * Complete close and delete TCPCB. Then reprocess
1806 * segment, hoping to find new TCPCB in LISTEN state;
1808 * else must be old SYN; drop it.
1809 * else do normal processing.
1813 case TCPS_TIME_WAIT
:
1814 if ((thflags
& TH_SYN
) &&
1815 (to
.to_flag
& TOF_CC
) && tp
->cc_recv
!= 0) {
1816 if (tp
->t_state
== TCPS_TIME_WAIT
&&
1817 tp
->t_starttime
> tcp_msl
) {
1818 rstreason
= BANDLIM_UNLIMITED
;
1821 if (CC_GT(to
.to_cc
, tp
->cc_recv
)) {
1823 tcp_unlock(so
, 1, 50);
1829 break; /* continue normal processing */
1831 /* Received a SYN while connection is already established.
1832 * This is a "half open connection and other anomalies" described
1833 * in RFC793 page 34, send an ACK so the remote reset the connection
1834 * or recovers by adjusting its sequence numberering
1836 case TCPS_ESTABLISHED
:
1837 if (thflags
& TH_SYN
)
1843 * States other than LISTEN or SYN_SENT.
1844 * First check the RST flag and sequence number since reset segments
1845 * are exempt from the timestamp and connection count tests. This
1846 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
1847 * below which allowed reset segments in half the sequence space
1848 * to fall though and be processed (which gives forged reset
1849 * segments with a random sequence number a 50 percent chance of
1850 * killing a connection).
1851 * Then check timestamp, if present.
1852 * Then check the connection count, if present.
1853 * Then check that at least some bytes of segment are within
1854 * receive window. If segment begins before rcv_nxt,
1855 * drop leading data (and SYN); if nothing left, just ack.
1858 * If the RST bit is set, check the sequence number to see
1859 * if this is a valid reset segment.
1861 * In all states except SYN-SENT, all reset (RST) segments
1862 * are validated by checking their SEQ-fields. A reset is
1863 * valid if its sequence number is in the window.
1864 * Note: this does not take into account delayed ACKs, so
1865 * we should test against last_ack_sent instead of rcv_nxt.
1866 * The sequence number in the reset segment is normally an
1867 * echo of our outgoing acknowlegement numbers, but some hosts
1868 * send a reset with the sequence number at the rightmost edge
1869 * of our receive window, and we have to handle this case.
1870 * If we have multiple segments in flight, the intial reset
1871 * segment sequence numbers will be to the left of last_ack_sent,
1872 * but they will eventually catch up.
1873 * In any case, it never made sense to trim reset segments to
1874 * fit the receive window since RFC 1122 says:
1875 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
1877 * A TCP SHOULD allow a received RST segment to include data.
1880 * It has been suggested that a RST segment could contain
1881 * ASCII text that encoded and explained the cause of the
1882 * RST. No standard has yet been established for such
1885 * If the reset segment passes the sequence number test examine
1887 * SYN_RECEIVED STATE:
1888 * If passive open, return to LISTEN state.
1889 * If active open, inform user that connection was refused.
1890 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1891 * Inform user that connection was reset, and close tcb.
1892 * CLOSING, LAST_ACK STATES:
1895 * Drop the segment - see Stevens, vol. 2, p. 964 and
1898 if (thflags
& TH_RST
) {
1899 if (SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
1900 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) {
1901 switch (tp
->t_state
) {
1903 case TCPS_SYN_RECEIVED
:
1904 so
->so_error
= ECONNREFUSED
;
1907 case TCPS_ESTABLISHED
:
1908 case TCPS_FIN_WAIT_1
:
1909 case TCPS_CLOSE_WAIT
:
1913 case TCPS_FIN_WAIT_2
:
1914 so
->so_error
= ECONNRESET
;
1916 postevent(so
, 0, EV_RESET
);
1917 tp
->t_state
= TCPS_CLOSED
;
1918 tcpstat
.tcps_drops
++;
1927 case TCPS_TIME_WAIT
:
1935 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1938 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1939 * and it's less than ts_recent, drop it.
1941 if ((to
.to_flag
& TOF_TS
) != 0 && tp
->ts_recent
&&
1942 TSTMP_LT(to
.to_tsval
, tp
->ts_recent
)) {
1944 /* Check to see if ts_recent is over 24 days old. */
1945 if ((int)(tcp_now
- tp
->ts_recent_age
) > TCP_PAWS_IDLE
) {
1947 * Invalidate ts_recent. If this segment updates
1948 * ts_recent, the age will be reset later and ts_recent
1949 * will get a valid value. If it does not, setting
1950 * ts_recent to zero will at least satisfy the
1951 * requirement that zero be placed in the timestamp
1952 * echo reply when ts_recent isn't valid. The
1953 * age isn't reset until we get a valid ts_recent
1954 * because we don't want out-of-order segments to be
1955 * dropped when ts_recent is old.
1959 tcpstat
.tcps_rcvduppack
++;
1960 tcpstat
.tcps_rcvdupbyte
+= tlen
;
1961 tcpstat
.tcps_pawsdrop
++;
1968 * If T/TCP was negotiated and the segment doesn't have CC,
1969 * or if its CC is wrong then drop the segment.
1970 * RST segments do not have to comply with this.
1972 if ((tp
->t_flags
& (TF_REQ_CC
|TF_RCVD_CC
)) == (TF_REQ_CC
|TF_RCVD_CC
) &&
1973 ((to
.to_flag
& TOF_CC
) == 0 || tp
->cc_recv
!= to
.to_cc
))
1977 * In the SYN-RECEIVED state, validate that the packet belongs to
1978 * this connection before trimming the data to fit the receive
1979 * window. Check the sequence number versus IRS since we know
1980 * the sequence numbers haven't wrapped. This is a partial fix
1981 * for the "LAND" DoS attack.
1983 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& SEQ_LT(th
->th_seq
, tp
->irs
)) {
1984 rstreason
= BANDLIM_RST_OPENPORT
;
1988 todrop
= tp
->rcv_nxt
- th
->th_seq
;
1990 if (thflags
& TH_SYN
) {
2000 * Following if statement from Stevens, vol. 2, p. 960.
2003 || (todrop
== tlen
&& (thflags
& TH_FIN
) == 0)) {
2005 * Any valid FIN must be to the left of the window.
2006 * At this point the FIN must be a duplicate or out
2007 * of sequence; drop it.
2012 * Send an ACK to resynchronize and drop any data.
2013 * But keep on processing for RST or ACK.
2015 tp
->t_flags
|= TF_ACKNOW
;
2017 tcpstat
.tcps_rcvduppack
++;
2018 tcpstat
.tcps_rcvdupbyte
+= todrop
;
2020 tcpstat
.tcps_rcvpartduppack
++;
2021 tcpstat
.tcps_rcvpartdupbyte
+= todrop
;
2023 drop_hdrlen
+= todrop
; /* drop from the top afterwards */
2024 th
->th_seq
+= todrop
;
2026 if (th
->th_urp
> todrop
)
2027 th
->th_urp
-= todrop
;
2035 * If new data are received on a connection after the
2036 * user processes are gone, then RST the other end.
2038 if ((so
->so_state
& SS_NOFDREF
) &&
2039 tp
->t_state
> TCPS_CLOSE_WAIT
&& tlen
) {
2041 tcpstat
.tcps_rcvafterclose
++;
2042 rstreason
= BANDLIM_UNLIMITED
;
2047 * If segment ends after window, drop trailing data
2048 * (and PUSH and FIN); if nothing left, just ACK.
2050 todrop
= (th
->th_seq
+tlen
) - (tp
->rcv_nxt
+tp
->rcv_wnd
);
2052 tcpstat
.tcps_rcvpackafterwin
++;
2053 if (todrop
>= tlen
) {
2054 tcpstat
.tcps_rcvbyteafterwin
+= tlen
;
2056 * If a new connection request is received
2057 * while in TIME_WAIT, drop the old connection
2058 * and start over if the sequence numbers
2059 * are above the previous ones.
2061 if (thflags
& TH_SYN
&&
2062 tp
->t_state
== TCPS_TIME_WAIT
&&
2063 SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
2064 iss
= tcp_new_isn(tp
);
2066 tcp_unlock(so
, 1, 0);
2070 * If window is closed can only take segments at
2071 * window edge, and have to drop data and PUSH from
2072 * incoming segments. Continue processing, but
2073 * remember to ack. Otherwise, drop segment
2076 if (tp
->rcv_wnd
== 0 && th
->th_seq
== tp
->rcv_nxt
) {
2077 tp
->t_flags
|= TF_ACKNOW
;
2078 tcpstat
.tcps_rcvwinprobe
++;
2082 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
2085 thflags
&= ~(TH_PUSH
|TH_FIN
);
2089 * If last ACK falls within this segment's sequence numbers,
2090 * record its timestamp.
2091 * NOTE that the test is modified according to the latest
2092 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
2094 if ((to
.to_flag
& TOF_TS
) != 0 &&
2095 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
2096 tp
->ts_recent_age
= tcp_now
;
2097 tp
->ts_recent
= to
.to_tsval
;
2101 * If a SYN is in the window, then this is an
2102 * error and we send an RST and drop the connection.
2104 if (thflags
& TH_SYN
) {
2105 tp
= tcp_drop(tp
, ECONNRESET
);
2106 rstreason
= BANDLIM_UNLIMITED
;
2107 postevent(so
, 0, EV_RESET
);
2112 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
2113 * flag is on (half-synchronized state), then queue data for
2114 * later processing; else drop segment and return.
2116 if ((thflags
& TH_ACK
) == 0) {
2117 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
2118 (tp
->t_flags
& TF_NEEDSYN
))
2127 switch (tp
->t_state
) {
2130 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2131 * ESTABLISHED state and continue processing.
2132 * The ACK was checked above.
2134 case TCPS_SYN_RECEIVED
:
2136 tcpstat
.tcps_connects
++;
2139 /* Do window scaling? */
2140 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
2141 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
2142 tp
->snd_scale
= tp
->requested_s_scale
;
2143 tp
->rcv_scale
= tp
->request_r_scale
;
2146 * Upon successful completion of 3-way handshake,
2147 * update cache.CC if it was undefined, pass any queued
2148 * data to the user, and advance state appropriately.
2150 if ((taop
= tcp_gettaocache(inp
)) != NULL
&&
2152 taop
->tao_cc
= tp
->cc_recv
;
2156 * SYN-RECEIVED -> ESTABLISHED
2157 * SYN-RECEIVED* -> FIN-WAIT-1
2159 if (tp
->t_flags
& TF_NEEDFIN
) {
2160 tp
->t_state
= TCPS_FIN_WAIT_1
;
2161 tp
->t_flags
&= ~TF_NEEDFIN
;
2163 tp
->t_state
= TCPS_ESTABLISHED
;
2164 tp
->t_timer
[TCPT_KEEP
] = TCP_KEEPIDLE(tp
);
2167 * If segment contains data or ACK, will call tcp_reass()
2168 * later; if not, do so now to pass queued data to user.
2170 if (tlen
== 0 && (thflags
& TH_FIN
) == 0)
2171 (void) tcp_reass(tp
, (struct tcphdr
*)0, 0,
2173 tp
->snd_wl1
= th
->th_seq
- 1;
2177 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2178 * ACKs. If the ack is in the range
2179 * tp->snd_una < th->th_ack <= tp->snd_max
2180 * then advance tp->snd_una to th->th_ack and drop
2181 * data from the retransmission queue. If this ACK reflects
2182 * more up to date window information we update our window information.
2184 case TCPS_ESTABLISHED
:
2185 case TCPS_FIN_WAIT_1
:
2186 case TCPS_FIN_WAIT_2
:
2187 case TCPS_CLOSE_WAIT
:
2190 case TCPS_TIME_WAIT
:
2192 if (SEQ_LEQ(th
->th_ack
, tp
->snd_una
)) {
2193 if (tlen
== 0 && tiwin
== tp
->snd_wnd
) {
2194 tcpstat
.tcps_rcvdupack
++;
2196 * If we have outstanding data (other than
2197 * a window probe), this is a completely
2198 * duplicate ack (ie, window info didn't
2199 * change), the ack is the biggest we've
2200 * seen and we've seen exactly our rexmt
2201 * threshhold of them, assume a packet
2202 * has been dropped and retransmit it.
2203 * Kludge snd_nxt & the congestion
2204 * window so we send only this one
2207 * We know we're losing at the current
2208 * window size so do congestion avoidance
2209 * (set ssthresh to half the current window
2210 * and pull our congestion window back to
2211 * the new ssthresh).
2213 * Dup acks mean that packets have left the
2214 * network (they're now cached at the receiver)
2215 * so bump cwnd by the amount in the receiver
2216 * to keep a constant cwnd packets in the
2219 if (tp
->t_timer
[TCPT_REXMT
] == 0 ||
2220 th
->th_ack
!= tp
->snd_una
)
2222 else if (++tp
->t_dupacks
== tcprexmtthresh
) {
2223 tcp_seq onxt
= tp
->snd_nxt
;
2225 min(tp
->snd_wnd
, tp
->snd_cwnd
) / 2 /
2227 if (tcp_do_newreno
&& SEQ_LT(th
->th_ack
,
2229 /* False retransmit, should not
2232 tp
->snd_cwnd
+= tp
->t_maxseg
;
2234 (void) tcp_output(tp
);
2239 tp
->snd_ssthresh
= win
* tp
->t_maxseg
;
2240 tp
->snd_recover
= tp
->snd_max
;
2241 tp
->t_timer
[TCPT_REXMT
] = 0;
2243 tp
->snd_nxt
= th
->th_ack
;
2244 tp
->snd_cwnd
= tp
->t_maxseg
;
2245 (void) tcp_output(tp
);
2246 tp
->snd_cwnd
= tp
->snd_ssthresh
+
2247 tp
->t_maxseg
* tp
->t_dupacks
;
2248 if (SEQ_GT(onxt
, tp
->snd_nxt
))
2251 } else if (tp
->t_dupacks
> tcprexmtthresh
) {
2252 tp
->snd_cwnd
+= tp
->t_maxseg
;
2253 (void) tcp_output(tp
);
2261 * If the congestion window was inflated to account
2262 * for the other side's cached packets, retract it.
2264 if (tcp_do_newreno
== 0) {
2265 if (tp
->t_dupacks
>= tcprexmtthresh
&&
2266 tp
->snd_cwnd
> tp
->snd_ssthresh
)
2267 tp
->snd_cwnd
= tp
->snd_ssthresh
;
2269 } else if (tp
->t_dupacks
>= tcprexmtthresh
&&
2270 !tcp_newreno(tp
, th
)) {
2272 * Window inflation should have left us with approx.
2273 * snd_ssthresh outstanding data. But in case we
2274 * would be inclined to send a burst, better to do
2275 * it via the slow start mechanism.
2277 if (SEQ_GT(th
->th_ack
+ tp
->snd_ssthresh
, tp
->snd_max
))
2279 tp
->snd_max
- th
->th_ack
+ tp
->t_maxseg
;
2281 tp
->snd_cwnd
= tp
->snd_ssthresh
;
2285 if (tp
->t_dupacks
< tcprexmtthresh
)
2288 if (SEQ_GT(th
->th_ack
, tp
->snd_max
)) {
2289 tcpstat
.tcps_rcvacktoomuch
++;
2293 * If we reach this point, ACK is not a duplicate,
2294 * i.e., it ACKs something we sent.
2296 if (tp
->t_flags
& TF_NEEDSYN
) {
2298 * T/TCP: Connection was half-synchronized, and our
2299 * SYN has been ACK'd (so connection is now fully
2300 * synchronized). Go to non-starred state,
2301 * increment snd_una for ACK of SYN, and check if
2302 * we can do window scaling.
2304 tp
->t_flags
&= ~TF_NEEDSYN
;
2306 /* Do window scaling? */
2307 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
2308 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
2309 tp
->snd_scale
= tp
->requested_s_scale
;
2310 tp
->rcv_scale
= tp
->request_r_scale
;
2315 acked
= th
->th_ack
- tp
->snd_una
;
2316 tcpstat
.tcps_rcvackpack
++;
2317 tcpstat
.tcps_rcvackbyte
+= acked
;
2320 * If we just performed our first retransmit, and the ACK
2321 * arrives within our recovery window, then it was a mistake
2322 * to do the retransmit in the first place. Recover our
2323 * original cwnd and ssthresh, and proceed to transmit where
2326 if (tp
->t_rxtshift
== 1 && tcp_now
< tp
->t_badrxtwin
) {
2327 tp
->snd_cwnd
= tp
->snd_cwnd_prev
;
2328 tp
->snd_ssthresh
= tp
->snd_ssthresh_prev
;
2329 tp
->snd_nxt
= tp
->snd_max
;
2330 tp
->t_badrxtwin
= 0; /* XXX probably not required */
2334 * If we have a timestamp reply, update smoothed
2335 * round trip time. If no timestamp is present but
2336 * transmit timer is running and timed sequence
2337 * number was acked, update smoothed round trip time.
2338 * Since we now have an rtt measurement, cancel the
2339 * timer backoff (cf., Phil Karn's retransmit alg.).
2340 * Recompute the initial retransmit timer.
2341 * Also makes sure we have a valid time stamp in hand
2343 if (((to
.to_flag
& TOF_TS
) != 0) && (to
.to_tsecr
!= 0))
2344 tcp_xmit_timer(tp
, tcp_now
- to
.to_tsecr
+ 1);
2345 else if (tp
->t_rtttime
&& SEQ_GT(th
->th_ack
, tp
->t_rtseq
))
2346 tcp_xmit_timer(tp
, tp
->t_rtttime
);
2349 * If all outstanding data is acked, stop retransmit
2350 * timer and remember to restart (more output or persist).
2351 * If there is more data to be acked, restart retransmit
2352 * timer, using current (possibly backed-off) value.
2354 if (th
->th_ack
== tp
->snd_max
) {
2355 tp
->t_timer
[TCPT_REXMT
] = 0;
2357 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
2358 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
2361 * If no data (only SYN) was ACK'd,
2362 * skip rest of ACK processing.
2368 * When new data is acked, open the congestion window.
2369 * If the window gives us less than ssthresh packets
2370 * in flight, open exponentially (maxseg per packet).
2371 * Otherwise open linearly: maxseg per window
2372 * (maxseg^2 / cwnd per packet).
2375 register u_int cw
= tp
->snd_cwnd
;
2376 register u_int incr
= tp
->t_maxseg
;
2378 if (cw
> tp
->snd_ssthresh
)
2379 incr
= incr
* incr
/ cw
;
2381 * If t_dupacks != 0 here, it indicates that we are still
2382 * in NewReno fast recovery mode, so we leave the congestion
2385 if (tcp_do_newreno
== 0 || tp
->t_dupacks
== 0)
2386 tp
->snd_cwnd
= min(cw
+ incr
,TCP_MAXWIN
<<tp
->snd_scale
);
2388 if (acked
> so
->so_snd
.sb_cc
) {
2389 tp
->snd_wnd
-= so
->so_snd
.sb_cc
;
2390 sbdrop(&so
->so_snd
, (int)so
->so_snd
.sb_cc
);
2393 sbdrop(&so
->so_snd
, acked
);
2394 tp
->snd_wnd
-= acked
;
2398 /* detect una wraparound */
2399 if (SEQ_GEQ(tp
->snd_una
, tp
->snd_recover
) &&
2400 SEQ_LT(th
->th_ack
, tp
->snd_recover
))
2401 tp
->snd_recover
= th
->th_ack
;
2402 if (SEQ_GT(tp
->snd_una
, tp
->snd_high
) &&
2403 SEQ_LEQ(th
->th_ack
, tp
->snd_high
))
2404 tp
->snd_high
= th
->th_ack
- 1;
2405 tp
->snd_una
= th
->th_ack
;
2406 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
2407 tp
->snd_nxt
= tp
->snd_una
;
2409 switch (tp
->t_state
) {
2412 * In FIN_WAIT_1 STATE in addition to the processing
2413 * for the ESTABLISHED state if our FIN is now acknowledged
2414 * then enter FIN_WAIT_2.
2416 case TCPS_FIN_WAIT_1
:
2417 if (ourfinisacked
) {
2419 * If we can't receive any more
2420 * data, then closing user can proceed.
2421 * Starting the timer is contrary to the
2422 * specification, but if we don't get a FIN
2423 * we'll hang forever.
2425 if (so
->so_state
& SS_CANTRCVMORE
) {
2426 soisdisconnected(so
);
2427 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;
2429 add_to_time_wait(tp
);
2430 tp
->t_state
= TCPS_FIN_WAIT_2
;
2436 * In CLOSING STATE in addition to the processing for
2437 * the ESTABLISHED state if the ACK acknowledges our FIN
2438 * then enter the TIME-WAIT state, otherwise ignore
2442 if (ourfinisacked
) {
2443 tp
->t_state
= TCPS_TIME_WAIT
;
2444 tcp_canceltimers(tp
);
2445 /* Shorten TIME_WAIT [RFC-1644, p.28] */
2446 if (tp
->cc_recv
!= 0 &&
2447 tp
->t_starttime
< tcp_msl
)
2448 tp
->t_timer
[TCPT_2MSL
] =
2449 tp
->t_rxtcur
* TCPTV_TWTRUNC
;
2451 tp
->t_timer
[TCPT_2MSL
] = 2 * tcp_msl
;
2452 add_to_time_wait(tp
);
2453 soisdisconnected(so
);
2458 * In LAST_ACK, we may still be waiting for data to drain
2459 * and/or to be acked, as well as for the ack of our FIN.
2460 * If our FIN is now acknowledged, delete the TCB,
2461 * enter the closed state and return.
2464 if (ourfinisacked
) {
2471 * In TIME_WAIT state the only thing that should arrive
2472 * is a retransmission of the remote FIN. Acknowledge
2473 * it and restart the finack timer.
2475 case TCPS_TIME_WAIT
:
2476 tp
->t_timer
[TCPT_2MSL
] = 2 * tcp_msl
;
2477 add_to_time_wait(tp
);
2484 * Update window information.
2485 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2487 if ((thflags
& TH_ACK
) &&
2488 (SEQ_LT(tp
->snd_wl1
, th
->th_seq
) ||
2489 (tp
->snd_wl1
== th
->th_seq
&& (SEQ_LT(tp
->snd_wl2
, th
->th_ack
) ||
2490 (tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
))))) {
2491 /* keep track of pure window updates */
2493 tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
)
2494 tcpstat
.tcps_rcvwinupd
++;
2495 tp
->snd_wnd
= tiwin
;
2496 tp
->snd_wl1
= th
->th_seq
;
2497 tp
->snd_wl2
= th
->th_ack
;
2498 if (tp
->snd_wnd
> tp
->max_sndwnd
)
2499 tp
->max_sndwnd
= tp
->snd_wnd
;
2504 * Process segments with URG.
2506 if ((thflags
& TH_URG
) && th
->th_urp
&&
2507 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
2509 * This is a kludge, but if we receive and accept
2510 * random urgent pointers, we'll crash in
2511 * soreceive. It's hard to imagine someone
2512 * actually wanting to send this much urgent data.
2514 if (th
->th_urp
+ so
->so_rcv
.sb_cc
> sb_max
) {
2515 th
->th_urp
= 0; /* XXX */
2516 thflags
&= ~TH_URG
; /* XXX */
2517 goto dodata
; /* XXX */
2520 * If this segment advances the known urgent pointer,
2521 * then mark the data stream. This should not happen
2522 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2523 * a FIN has been received from the remote side.
2524 * In these states we ignore the URG.
2526 * According to RFC961 (Assigned Protocols),
2527 * the urgent pointer points to the last octet
2528 * of urgent data. We continue, however,
2529 * to consider it to indicate the first octet
2530 * of data past the urgent section as the original
2531 * spec states (in one of two places).
2533 if (SEQ_GT(th
->th_seq
+th
->th_urp
, tp
->rcv_up
)) {
2534 tp
->rcv_up
= th
->th_seq
+ th
->th_urp
;
2535 so
->so_oobmark
= so
->so_rcv
.sb_cc
+
2536 (tp
->rcv_up
- tp
->rcv_nxt
) - 1;
2537 if (so
->so_oobmark
== 0) {
2538 so
->so_state
|= SS_RCVATMARK
;
2539 postevent(so
, 0, EV_OOB
);
2542 tp
->t_oobflags
&= ~(TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
2545 * Remove out of band data so doesn't get presented to user.
2546 * This can happen independent of advancing the URG pointer,
2547 * but if two URG's are pending at once, some out-of-band
2548 * data may creep in... ick.
2550 if (th
->th_urp
<= (u_long
)tlen
2552 && (so
->so_options
& SO_OOBINLINE
) == 0
2555 tcp_pulloutofband(so
, th
, m
,
2556 drop_hdrlen
); /* hdr drop is delayed */
2559 * If no out of band data is expected,
2560 * pull receive urgent pointer along
2561 * with the receive window.
2563 if (SEQ_GT(tp
->rcv_nxt
, tp
->rcv_up
))
2564 tp
->rcv_up
= tp
->rcv_nxt
;
2568 * Process the segment text, merging it into the TCP sequencing queue,
2569 * and arranging for acknowledgment of receipt if necessary.
2570 * This process logically involves adjusting tp->rcv_wnd as data
2571 * is presented to the user (this happens in tcp_usrreq.c,
2572 * case PRU_RCVD). If a FIN has already been received on this
2573 * connection then we just ignore the text.
2575 if ((tlen
|| (thflags
&TH_FIN
)) &&
2576 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
2577 m_adj(m
, drop_hdrlen
); /* delayed header drop */
2579 * Insert segment which inludes th into reassembly queue of tcp with
2580 * control block tp. Return TH_FIN if reassembly now includes
2581 * a segment with FIN. This handle the common case inline (segment
2582 * is the next to be received on an established connection, and the
2583 * queue is empty), avoiding linkage into and removal from the queue
2584 * and repetition of various conversions.
2585 * Set DELACK for segments received in order, but ack immediately
2586 * when segments are out of order (so fast retransmit can work).
2588 if (th
->th_seq
== tp
->rcv_nxt
&&
2589 LIST_EMPTY(&tp
->t_segq
) &&
2590 TCPS_HAVEESTABLISHED(tp
->t_state
)) {
2591 if (DELAY_ACK(tp
) && ((tp
->t_flags
& TF_ACKNOW
) == 0)) {
2592 tp
->t_flags
|= TF_DELACK
;
2595 tp
->t_flags
|= TF_ACKNOW
;
2597 tp
->rcv_nxt
+= tlen
;
2598 thflags
= th
->th_flags
& TH_FIN
;
2599 tcpstat
.tcps_rcvpack
++;
2600 tcpstat
.tcps_rcvbyte
+= tlen
;
2602 if (sbappend(&so
->so_rcv
, m
))
2605 thflags
= tcp_reass(tp
, th
, &tlen
, m
);
2606 tp
->t_flags
|= TF_ACKNOW
;
2609 if (tp
->t_flags
& TF_DELACK
)
2613 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
2614 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
2615 th
->th_seq
, th
->th_ack
, th
->th_win
);
2620 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
2621 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
2622 th
->th_seq
, th
->th_ack
, th
->th_win
);
2627 * Note the amount of data that peer has sent into
2628 * our window, in order to estimate the sender's
2631 len
= so
->so_rcv
.sb_hiwat
- (tp
->rcv_adv
- tp
->rcv_nxt
);
2638 * If FIN is received ACK the FIN and let the user know
2639 * that the connection is closing.
2641 if (thflags
& TH_FIN
) {
2642 if (TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
2644 postevent(so
, 0, EV_FIN
);
2646 * If connection is half-synchronized
2647 * (ie NEEDSYN flag on) then delay ACK,
2648 * so it may be piggybacked when SYN is sent.
2649 * Otherwise, since we received a FIN then no
2650 * more input can be expected, send ACK now.
2652 if (DELAY_ACK(tp
) && (tp
->t_flags
& TF_NEEDSYN
)) {
2653 tp
->t_flags
|= TF_DELACK
;
2656 tp
->t_flags
|= TF_ACKNOW
;
2660 switch (tp
->t_state
) {
2663 * In SYN_RECEIVED and ESTABLISHED STATES
2664 * enter the CLOSE_WAIT state.
2666 case TCPS_SYN_RECEIVED
:
2668 case TCPS_ESTABLISHED
:
2669 tp
->t_state
= TCPS_CLOSE_WAIT
;
2673 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2674 * enter the CLOSING state.
2676 case TCPS_FIN_WAIT_1
:
2677 tp
->t_state
= TCPS_CLOSING
;
2681 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2682 * starting the time-wait timer, turning off the other
2685 case TCPS_FIN_WAIT_2
:
2686 tp
->t_state
= TCPS_TIME_WAIT
;
2687 tcp_canceltimers(tp
);
2688 /* Shorten TIME_WAIT [RFC-1644, p.28] */
2689 if (tp
->cc_recv
!= 0 &&
2690 tp
->t_starttime
< tcp_msl
) {
2691 tp
->t_timer
[TCPT_2MSL
] =
2692 tp
->t_rxtcur
* TCPTV_TWTRUNC
;
2693 /* For transaction client, force ACK now. */
2694 tp
->t_flags
|= TF_ACKNOW
;
2697 tp
->t_timer
[TCPT_2MSL
] = 2 * tcp_msl
;
2699 add_to_time_wait(tp
);
2700 soisdisconnected(so
);
2704 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2706 case TCPS_TIME_WAIT
:
2707 tp
->t_timer
[TCPT_2MSL
] = 2 * tcp_msl
;
2708 add_to_time_wait(tp
);
2713 if (so
->so_options
& SO_DEBUG
)
2714 tcp_trace(TA_INPUT
, ostate
, tp
, (void *)tcp_saveipgen
,
2719 * Return any desired output.
2721 if (needoutput
|| (tp
->t_flags
& TF_ACKNOW
))
2722 (void) tcp_output(tp
);
2723 tcp_unlock(so
, 1, 0);
2724 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2729 * Generate an ACK dropping incoming segment if it occupies
2730 * sequence space, where the ACK reflects our state.
2732 * We can now skip the test for the RST flag since all
2733 * paths to this code happen after packets containing
2734 * RST have been dropped.
2736 * In the SYN-RECEIVED state, don't send an ACK unless the
2737 * segment we received passes the SYN-RECEIVED ACK test.
2738 * If it fails send a RST. This breaks the loop in the
2739 * "LAND" DoS attack, and also prevents an ACK storm
2740 * between two listening ports that have been sent forged
2741 * SYN segments, each with the source address of the other.
2743 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& (thflags
& TH_ACK
) &&
2744 (SEQ_GT(tp
->snd_una
, th
->th_ack
) ||
2745 SEQ_GT(th
->th_ack
, tp
->snd_max
)) ) {
2746 rstreason
= BANDLIM_RST_OPENPORT
;
2750 if (so
->so_options
& SO_DEBUG
)
2751 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
2755 tp
->t_flags
|= TF_ACKNOW
;
2756 (void) tcp_output(tp
);
2757 tcp_unlock(so
, 1, 0);
2758 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2760 dropwithresetnosock
:
2764 * Generate a RST, dropping incoming segment.
2765 * Make ACK acceptable to originator of segment.
2766 * Don't bother to respond if destination was broadcast/multicast.
2768 if ((thflags
& TH_RST
) || m
->m_flags
& (M_BCAST
|M_MCAST
))
2772 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
2773 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
))
2777 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
2778 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
2779 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
2780 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
))
2782 /* IPv6 anycast check is done at tcp6_input() */
2785 * Perform bandwidth limiting.
2788 if (badport_bandlim(rstreason
) < 0)
2793 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
2794 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
2797 if (thflags
& TH_ACK
)
2798 /* mtod() below is safe as long as hdr dropping is delayed */
2799 tcp_respond(tp
, mtod(m
, void *), th
, m
, (tcp_seq
)0, th
->th_ack
,
2802 if (thflags
& TH_SYN
)
2804 /* mtod() below is safe as long as hdr dropping is delayed */
2805 tcp_respond(tp
, mtod(m
, void *), th
, m
, th
->th_seq
+tlen
,
2806 (tcp_seq
)0, TH_RST
|TH_ACK
);
2808 /* destroy temporarily created socket */
2811 tcp_unlock(so
, 1, 0);
2814 if ((inp
!= NULL
) && (nosock
== 0))
2815 tcp_unlock(so
, 1, 0);
2816 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2822 * Drop space held by incoming segment and return.
2825 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
2826 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
2830 /* destroy temporarily created socket */
2833 tcp_unlock(so
, 1, 0);
2837 tcp_unlock(so
, 1, 0);
2838 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2843 tcp_dooptions(tp
, cp
, cnt
, th
, to
)
2853 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
2855 if (opt
== TCPOPT_EOL
)
2857 if (opt
== TCPOPT_NOP
)
2863 if (optlen
< 2 || optlen
> cnt
)
2872 if (optlen
!= TCPOLEN_MAXSEG
)
2874 if (!(th
->th_flags
& TH_SYN
))
2876 bcopy((char *) cp
+ 2, (char *) &mss
, sizeof(mss
));
2881 if (optlen
!= TCPOLEN_WINDOW
)
2883 if (!(th
->th_flags
& TH_SYN
))
2885 tp
->t_flags
|= TF_RCVD_SCALE
;
2886 tp
->requested_s_scale
= min(cp
[2], TCP_MAX_WINSHIFT
);
2889 case TCPOPT_TIMESTAMP
:
2890 if (optlen
!= TCPOLEN_TIMESTAMP
)
2892 to
->to_flag
|= TOF_TS
;
2893 bcopy((char *)cp
+ 2,
2894 (char *)&to
->to_tsval
, sizeof(to
->to_tsval
));
2895 NTOHL(to
->to_tsval
);
2896 bcopy((char *)cp
+ 6,
2897 (char *)&to
->to_tsecr
, sizeof(to
->to_tsecr
));
2898 NTOHL(to
->to_tsecr
);
2901 * A timestamp received in a SYN makes
2902 * it ok to send timestamp requests and replies.
2904 if (th
->th_flags
& TH_SYN
) {
2905 tp
->t_flags
|= TF_RCVD_TSTMP
;
2906 tp
->ts_recent
= to
->to_tsval
;
2907 tp
->ts_recent_age
= tcp_now
;
2911 if (optlen
!= TCPOLEN_CC
)
2913 to
->to_flag
|= TOF_CC
;
2914 bcopy((char *)cp
+ 2,
2915 (char *)&to
->to_cc
, sizeof(to
->to_cc
));
2918 * A CC or CC.new option received in a SYN makes
2919 * it ok to send CC in subsequent segments.
2921 if (th
->th_flags
& TH_SYN
)
2922 tp
->t_flags
|= TF_RCVD_CC
;
2925 if (optlen
!= TCPOLEN_CC
)
2927 if (!(th
->th_flags
& TH_SYN
))
2929 to
->to_flag
|= TOF_CCNEW
;
2930 bcopy((char *)cp
+ 2,
2931 (char *)&to
->to_cc
, sizeof(to
->to_cc
));
2934 * A CC or CC.new option received in a SYN makes
2935 * it ok to send CC in subsequent segments.
2937 tp
->t_flags
|= TF_RCVD_CC
;
2940 if (optlen
!= TCPOLEN_CC
)
2942 if (!(th
->th_flags
& TH_SYN
))
2944 to
->to_flag
|= TOF_CCECHO
;
2945 bcopy((char *)cp
+ 2,
2946 (char *)&to
->to_ccecho
, sizeof(to
->to_ccecho
));
2947 NTOHL(to
->to_ccecho
);
2951 if (th
->th_flags
& TH_SYN
)
2952 tcp_mss(tp
, mss
); /* sets t_maxseg */
2956 * Pull out of band byte out of a segment so
2957 * it doesn't appear in the user's data queue.
2958 * It is still reflected in the segment length for
2959 * sequencing purposes.
2962 tcp_pulloutofband(so
, th
, m
, off
)
2965 register struct mbuf
*m
;
2966 int off
; /* delayed to be droped hdrlen */
2968 int cnt
= off
+ th
->th_urp
- 1;
2971 if (m
->m_len
> cnt
) {
2972 char *cp
= mtod(m
, caddr_t
) + cnt
;
2973 struct tcpcb
*tp
= sototcpcb(so
);
2976 tp
->t_oobflags
|= TCPOOB_HAVEDATA
;
2977 bcopy(cp
+1, cp
, (unsigned)(m
->m_len
- cnt
- 1));
2979 if (m
->m_flags
& M_PKTHDR
)
2988 panic("tcp_pulloutofband");
2992 * Collect new round-trip time estimate
2993 * and update averages and current timeout.
2996 tcp_xmit_timer(tp
, rtt
)
2997 register struct tcpcb
*tp
;
3002 tcpstat
.tcps_rttupdated
++;
3004 if (tp
->t_srtt
!= 0) {
3006 * srtt is stored as fixed point with 5 bits after the
3007 * binary point (i.e., scaled by 8). The following magic
3008 * is equivalent to the smoothing algorithm in rfc793 with
3009 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3010 * point). Adjust rtt to origin 0.
3012 delta
= ((rtt
- 1) << TCP_DELTA_SHIFT
)
3013 - (tp
->t_srtt
>> (TCP_RTT_SHIFT
- TCP_DELTA_SHIFT
));
3015 if ((tp
->t_srtt
+= delta
) <= 0)
3019 * We accumulate a smoothed rtt variance (actually, a
3020 * smoothed mean difference), then set the retransmit
3021 * timer to smoothed rtt + 4 times the smoothed variance.
3022 * rttvar is stored as fixed point with 4 bits after the
3023 * binary point (scaled by 16). The following is
3024 * equivalent to rfc793 smoothing with an alpha of .75
3025 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
3026 * rfc793's wired-in beta.
3030 delta
-= tp
->t_rttvar
>> (TCP_RTTVAR_SHIFT
- TCP_DELTA_SHIFT
);
3031 if ((tp
->t_rttvar
+= delta
) <= 0)
3035 * No rtt measurement yet - use the unsmoothed rtt.
3036 * Set the variance to half the rtt (so our first
3037 * retransmit happens at 3*rtt).
3039 tp
->t_srtt
= rtt
<< TCP_RTT_SHIFT
;
3040 tp
->t_rttvar
= rtt
<< (TCP_RTTVAR_SHIFT
- 1);
3046 * the retransmit should happen at rtt + 4 * rttvar.
3047 * Because of the way we do the smoothing, srtt and rttvar
3048 * will each average +1/2 tick of bias. When we compute
3049 * the retransmit timer, we want 1/2 tick of rounding and
3050 * 1 extra tick because of +-1/2 tick uncertainty in the
3051 * firing of the timer. The bias will give us exactly the
3052 * 1.5 tick we need. But, because the bias is
3053 * statistical, we have to test that we don't drop below
3054 * the minimum feasible timer (which is 2 ticks).
3056 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
3057 max(tp
->t_rttmin
, rtt
+ 2), TCPTV_REXMTMAX
);
3060 * We received an ack for a packet that wasn't retransmitted;
3061 * it is probably safe to discard any error indications we've
3062 * received recently. This isn't quite right, but close enough
3063 * for now (a route might have failed after we sent a segment,
3064 * and the return path might not be symmetrical).
3066 tp
->t_softerror
= 0;
3070 * Determine a reasonable value for maxseg size.
3071 * If the route is known, check route for mtu.
3072 * If none, use an mss that can be handled on the outgoing
3073 * interface without forcing IP to fragment; if bigger than
3074 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
3075 * to utilize large mbufs. If no route is found, route has no mtu,
3076 * or the destination isn't local, use a default, hopefully conservative
3077 * size (usually 512 or the default IP max size, but no more than the mtu
3078 * of the interface), as we can't discover anything about intervening
3079 * gateways or networks. We also initialize the congestion/slow start
3080 * window to be a single segment if the destination isn't local.
3081 * While looking at the routing entry, we also initialize other path-dependent
3082 * parameters from pre-set or cached values in the routing entry.
3084 * Also take into account the space needed for options that we
3085 * send regularly. Make maxseg shorter by that amount to assure
3086 * that we can send maxseg amount of data even when the options
3087 * are present. Store the upper limit of the length of options plus
3090 * NOTE that this routine is only called when we process an incoming
3091 * segment, for outgoing segments only tcp_mssopt is called.
3093 * In case of T/TCP, we call this routine during implicit connection
3094 * setup as well (offer = -1), to initialize maxseg from the cached
3102 register struct rtentry
*rt
;
3104 register int rtt
, mss
;
3108 struct rmxp_tao
*taop
;
3109 int origoffer
= offer
;
3117 isipv6
= ((inp
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
3118 min_protoh
= isipv6
? sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
)
3119 : sizeof (struct tcpiphdr
);
3121 #define min_protoh (sizeof (struct tcpiphdr))
3125 rt
= tcp_rtlookup6(inp
);
3128 rt
= tcp_rtlookup(inp
);
3130 tp
->t_maxopd
= tp
->t_maxseg
=
3132 isipv6
? tcp_v6mssdflt
:
3139 * Slower link window correction:
3140 * If a value is specificied for slowlink_wsize use it for PPP links
3141 * believed to be on a serial modem (speed <128Kbps). Excludes 9600bps as
3142 * it is the default value adversized by pseudo-devices over ppp.
3144 if (ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
3145 ifp
->if_baudrate
> 9600 && ifp
->if_baudrate
<= 128000) {
3146 tp
->t_flags
|= TF_SLOWLINK
;
3148 so
= inp
->inp_socket
;
3150 taop
= rmx_taop(rt
->rt_rmx
);
3152 * Offer == -1 means that we didn't receive SYN yet,
3153 * use cached value in that case;
3156 offer
= taop
->tao_mssopt
;
3158 * Offer == 0 means that there was no MSS on the SYN segment,
3159 * in this case we use tcp_mssdflt.
3164 isipv6
? tcp_v6mssdflt
:
3169 * Prevent DoS attack with too small MSS. Round up
3170 * to at least minmss.
3172 offer
= max(offer
, tcp_minmss
);
3174 * Sanity check: make sure that maxopd will be large
3175 * enough to allow some data on segments even is the
3176 * all the option space is used (40bytes). Otherwise
3177 * funny things may happen in tcp_output.
3179 offer
= max(offer
, 64);
3181 taop
->tao_mssopt
= offer
;
3184 * While we're here, check if there's an initial rtt
3185 * or rttvar. Convert from the route-table units
3186 * to scaled multiples of the slow timeout timer.
3188 if (tp
->t_srtt
== 0 && (rtt
= rt
->rt_rmx
.rmx_rtt
)) {
3190 * XXX the lock bit for RTT indicates that the value
3191 * is also a minimum value; this is subject to time.
3193 if (rt
->rt_rmx
.rmx_locks
& RTV_RTT
)
3194 tp
->t_rttmin
= rtt
/ (RTM_RTTUNIT
/ PR_SLOWHZ
);
3195 tp
->t_srtt
= rtt
/ (RTM_RTTUNIT
/ (PR_SLOWHZ
* TCP_RTT_SCALE
));
3196 tcpstat
.tcps_usedrtt
++;
3197 if (rt
->rt_rmx
.rmx_rttvar
) {
3198 tp
->t_rttvar
= rt
->rt_rmx
.rmx_rttvar
/
3199 (RTM_RTTUNIT
/ (PR_SLOWHZ
* TCP_RTTVAR_SCALE
));
3200 tcpstat
.tcps_usedrttvar
++;
3202 /* default variation is +- 1 rtt */
3204 tp
->t_srtt
* TCP_RTTVAR_SCALE
/ TCP_RTT_SCALE
;
3206 TCPT_RANGESET(tp
->t_rxtcur
,
3207 ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1,
3208 tp
->t_rttmin
, TCPTV_REXMTMAX
);
3211 * if there's an mtu associated with the route, use it
3212 * else, use the link mtu.
3214 if (rt
->rt_rmx
.rmx_mtu
)
3215 mss
= rt
->rt_rmx
.rmx_mtu
- min_protoh
;
3220 (isipv6
? nd_ifinfo
[rt
->rt_ifp
->if_index
].linkmtu
:
3229 if (!in6_localaddr(&inp
->in6p_faddr
))
3230 mss
= min(mss
, tcp_v6mssdflt
);
3233 if (!in_localaddr(inp
->inp_faddr
))
3234 mss
= min(mss
, tcp_mssdflt
);
3236 mss
= min(mss
, offer
);
3238 * maxopd stores the maximum length of data AND options
3239 * in a segment; maxseg is the amount of data in a normal
3240 * segment. We need to store this value (maxopd) apart
3241 * from maxseg, because now every segment carries options
3242 * and thus we normally have somewhat less data in segments.
3247 * In case of T/TCP, origoffer==-1 indicates, that no segments
3248 * were received yet. In this case we just guess, otherwise
3249 * we do the same as before T/TCP.
3251 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
3253 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
))
3254 mss
-= TCPOLEN_TSTAMP_APPA
;
3255 if ((tp
->t_flags
& (TF_REQ_CC
|TF_NOOPT
)) == TF_REQ_CC
&&
3257 (tp
->t_flags
& TF_RCVD_CC
) == TF_RCVD_CC
))
3258 mss
-= TCPOLEN_CC_APPA
;
3261 * If there's a pipesize (ie loopback), change the socket
3262 * buffer to that size only if it's bigger than the current
3263 * sockbuf size. Make the socket buffers an integral
3264 * number of mss units; if the mss is larger than
3265 * the socket buffer, decrease the mss.
3268 bufsize
= rt
->rt_rmx
.rmx_sendpipe
;
3269 if (bufsize
< so
->so_snd
.sb_hiwat
)
3271 bufsize
= so
->so_snd
.sb_hiwat
;
3275 bufsize
= roundup(bufsize
, mss
);
3276 if (bufsize
> sb_max
)
3278 (void)sbreserve(&so
->so_snd
, bufsize
);
3283 bufsize
= rt
->rt_rmx
.rmx_recvpipe
;
3284 if (bufsize
< so
->so_rcv
.sb_hiwat
)
3286 bufsize
= so
->so_rcv
.sb_hiwat
;
3287 if (bufsize
> mss
) {
3288 bufsize
= roundup(bufsize
, mss
);
3289 if (bufsize
> sb_max
)
3291 (void)sbreserve(&so
->so_rcv
, bufsize
);
3295 * Set the slow-start flight size depending on whether this
3296 * is a local network or not.
3300 (isipv6
&& in6_localaddr(&inp
->in6p_faddr
)) ||
3303 in_localaddr(inp
->inp_faddr
)
3308 tp
->snd_cwnd
= mss
* ss_fltsz_local
;
3310 tp
->snd_cwnd
= mss
* ss_fltsz
;
3312 if (rt
->rt_rmx
.rmx_ssthresh
) {
3314 * There's some sort of gateway or interface
3315 * buffer limit on the path. Use this to set
3316 * the slow start threshhold, but set the
3317 * threshold to no less than 2*mss.
3319 tp
->snd_ssthresh
= max(2 * mss
, rt
->rt_rmx
.rmx_ssthresh
);
3320 tcpstat
.tcps_usedssthresh
++;
3325 * Determine the MSS option to send on an outgoing SYN.
3338 isipv6
= ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
3339 min_protoh
= isipv6
? sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
)
3340 : sizeof (struct tcpiphdr
);
3342 #define min_protoh (sizeof (struct tcpiphdr))
3346 rt
= tcp_rtlookup6(tp
->t_inpcb
);
3349 rt
= tcp_rtlookup(tp
->t_inpcb
);
3353 isipv6
? tcp_v6mssdflt
:
3357 * Slower link window correction:
3358 * If a value is specificied for slowlink_wsize use it for PPP links
3359 * believed to be on a serial modem (speed <128Kbps). Excludes 9600bps as
3360 * it is the default value adversized by pseudo-devices over ppp.
3362 if (rt
->rt_ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
3363 rt
->rt_ifp
->if_baudrate
> 9600 && rt
->rt_ifp
->if_baudrate
<= 128000) {
3364 tp
->t_flags
|= TF_SLOWLINK
;
3367 return rt
->rt_ifp
->if_mtu
- min_protoh
;
3372 * Checks for partial ack. If partial ack arrives, force the retransmission
3373 * of the next unacknowledged segment, do not clear tp->t_dupacks, and return
3374 * 1. By setting snd_nxt to ti_ack, this forces retransmission timer to
3375 * be started again. If the ack advances at least to tp->snd_recover, return 0.
3382 if (SEQ_LT(th
->th_ack
, tp
->snd_recover
)) {
3383 tcp_seq onxt
= tp
->snd_nxt
;
3384 u_long ocwnd
= tp
->snd_cwnd
;
3386 tp
->t_timer
[TCPT_REXMT
] = 0;
3388 callout_stop(tp
->tt_rexmt
);
3391 tp
->snd_nxt
= th
->th_ack
;
3393 * Set snd_cwnd to one segment beyond acknowledged offset
3394 * (tp->snd_una has not yet been updated when this function
3397 tp
->snd_cwnd
= tp
->t_maxseg
+ (th
->th_ack
- tp
->snd_una
);
3398 tp
->t_flags
|= TF_ACKNOW
;
3399 (void) tcp_output(tp
);
3400 tp
->snd_cwnd
= ocwnd
;
3401 if (SEQ_GT(onxt
, tp
->snd_nxt
))
3404 * Partial window deflation. Relies on fact that tp->snd_una
3407 tp
->snd_cwnd
-= (th
->th_ack
- tp
->snd_una
- tp
->t_maxseg
);
3414 * Drop a random TCP connection that hasn't been serviced yet and
3415 * is eligible for discard. There is a one in qlen chance that
3416 * we will return a null, saying that there are no dropable
3417 * requests. In this case, the protocol specific code should drop
3418 * the new request. This insures fairness.
3420 * The listening TCP socket "head" must be locked
3423 tcpdropdropablreq(struct socket
*head
)
3426 unsigned int i
, j
, qlen
;
3428 static struct timeval old_runtime
;
3429 static unsigned int cur_cnt
, old_cnt
;
3431 struct inpcb
*inp
= NULL
;
3435 if ((i
= (tv
.tv_sec
- old_runtime
.tv_sec
)) != 0) {
3437 old_cnt
= cur_cnt
/ i
;
3441 so
= TAILQ_FIRST(&head
->so_incomp
);
3445 qlen
= head
->so_incqlen
;
3446 if (++cur_cnt
> qlen
|| old_cnt
> qlen
) {
3447 rnd
= (314159 * rnd
+ 66329) & 0xffff;
3448 j
= ((qlen
+ 1) * rnd
) >> 16;
3451 so
= TAILQ_NEXT(so
, so_list
);
3453 /* Find a connection that is not already closing */
3455 inp
= (struct inpcb
*)so
->so_pcb
;
3457 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
)
3460 so
= TAILQ_NEXT(so
, so_list
);
3467 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
3468 tcp_unlock(head
, 0, 0);
3470 /* Let's remove this connection from the incomplete list */
3473 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
3474 tcp_unlock(so
, 1, 0);
3479 so
->so_usecount
--; /* No more held by so_head */
3482 * We do not want to lose track of the PCB right away in case we receive
3483 * more segments from the peer
3486 tp
->t_flags
|= TF_LQ_OVERFLOW
;
3487 tp
->t_state
= TCPS_CLOSED
;
3488 (void) tcp_output(tp
);
3489 tcpstat
.tcps_drops
++;
3490 soisdisconnected(so
);
3491 tcp_canceltimers(tp
);
3492 add_to_time_wait(tp
);
3494 tcp_unlock(so
, 1, 0);
3495 tcp_lock(head
, 0, 0);