2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
58 #include "opt_ipfw.h" /* for ipfw_fwd */
59 #include "opt_tcpdebug.h"
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/sysctl.h>
66 #include <sys/malloc.h>
68 #include <sys/proc.h> /* for proc0 declaration */
69 #include <sys/protosw.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/syslog.h>
74 #include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */
77 #include <net/route.h>
79 #include <netinet/in.h>
80 #include <netinet/in_systm.h>
81 #include <netinet/ip.h>
82 #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */
83 #include <netinet/in_var.h>
84 #include <netinet/ip_var.h>
85 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
86 #include <netinet/in_pcb.h>
88 #include <netinet/ip6.h>
89 #include <netinet/icmp6.h>
90 #include <netinet6/nd6.h>
91 #include <netinet6/ip6_var.h>
92 #include <netinet6/in6_pcb.h>
94 #include <netinet/tcp.h>
95 #include <netinet/tcp_fsm.h>
96 #include <netinet/tcp_seq.h>
97 #include <netinet/tcp_timer.h>
98 #include <netinet/tcp_var.h>
99 #include <netinet/tcpip.h>
101 #include <netinet/tcp_debug.h>
105 struct ip6_hdr _tcp_si6
;
108 struct ip tcp_saveip
;
110 struct tcphdr tcp_savetcp
;
111 #endif /* TCPDEBUG */
114 #include <netinet6/ipsec.h>
115 #include <netkey/key.h>
118 #include <sys/kdebug.h>
120 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 0)
121 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 2)
122 #define DBG_FNC_TCP_INPUT NETDBG_CODE(DBG_NETTCP, (3 << 8))
123 #define DBG_FNC_TCP_NEWCONN NETDBG_CODE(DBG_NETTCP, (7 << 8))
125 static int tcprexmtthresh
= 3;
128 extern int apple_hwcksum_rx
;
130 struct tcpstat tcpstat
;
131 SYSCTL_STRUCT(_net_inet_tcp
, TCPCTL_STATS
, stats
,
132 CTLFLAG_RD
, &tcpstat
, tcpstat
, "");
135 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, log_in_vain
, CTLFLAG_RW
,
136 &log_in_vain
, 0, "");
138 int tcp_delack_enabled
= 1;
139 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, delayed_ack
, CTLFLAG_RW
,
140 &tcp_delack_enabled
, 0, "");
143 struct inpcbhead tcb
;
144 #define tcb6 tcb /* for KAME src sync over BSD*'s */
145 struct inpcbinfo tcbinfo
;
147 static void tcp_dooptions
__P((struct tcpcb
*,
148 u_char
*, int, struct tcphdr
*, struct tcpopt
*));
149 static void tcp_pulloutofband
__P((struct socket
*,
150 struct tcphdr
*, struct mbuf
*));
151 static void tcp_xmit_timer
__P((struct tcpcb
*, int));
154 * Neighbor Discovery, Neighbor Unreachability Detection
157 #define ND6_HINT(tp) { \
158 if ((tp) && (tp)->t_inpcb && (tp)->t_inpcb->in6p_route.ro_rt) \
159 nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL); \
163 extern u_long current_active_connections
;
164 extern u_long last_active_conn_count
;
166 extern u_long
*delack_bitmask
;
172 * Insert segment ti into reassembly queue of tcp with
173 * control block tp. Return TH_FIN if reassembly now includes
174 * a segment with FIN. The macro form does the common case inline
175 * (segment is the next to be received on an established connection,
176 * and the queue is empty), avoiding linkage into and removal
177 * from the queue and repetition of various conversions.
178 * Set DELACK for segments received in order, but ack immediately
179 * when segments are out of order (so fast retransmit can work).
182 #define _ONLY_IF_INET6_(x) x
184 #define _ONLY_IF_INET6_(x)
186 #define TCP_REASS(tp, th, tilen, m, so, flags, isipv6, needwakeup) { \
187 if ((th)->th_seq == (tp)->rcv_nxt && \
188 (tp)->segq.lh_first == NULL && \
189 (tp)->t_state == TCPS_ESTABLISHED) { \
190 if (tcp_delack_enabled) {\
191 if (last_active_conn_count > DELACK_BITMASK_THRESH) \
192 TCP_DELACK_BITSET(tp->t_inpcb->hash_element); \
193 tp->t_flags |= TF_DELACK; \
196 tp->t_flags |= TF_ACKNOW; \
197 (tp)->rcv_nxt += (tilen); \
198 flags = (th)->th_flags & TH_FIN; \
199 tcpstat.tcps_rcvpack++;\
200 tcpstat.tcps_rcvbyte += (tilen);\
201 _ONLY_IF_INET6_(ND6_HINT(tp);) \
202 sbappend(&(so)->so_rcv, (m)); \
205 (flags) = tcp_reass((tp), (th), (tilen), (m), (isipv6)); \
206 tp->t_flags |= TF_ACKNOW; \
212 * in the ip header part of the ipqe_tcp structure only the length is used.
215 tcp_reass(tp
, th
, tilen
, m
, isipv6
)
216 register struct tcpcb
*tp
;
217 register struct tcphdr
*th
;
224 register struct ipqent
*p
, *q
, *nq
, *tiqe
;
225 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
229 * Call with th==0 after become established to
230 * force pre-ESTABLISHED data up to user socket.
235 #if 0 /* Not using GETTCP(m) macro */
236 m
->m_pkthdr
.header
= ti
;
240 * Allocate a new queue entry, before we throw away any data.
241 * If we can't, just drop the packet. XXX
243 MALLOC(tiqe
, struct ipqent
*, sizeof (struct ipqent
), M_SONAME
, M_NOWAIT
);
245 tcpstat
.tcps_rcvmemdrop
++;
251 * Find a segment which begins after this one does.
253 for (p
= NULL
, q
= tp
->segq
.lh_first
; q
!= NULL
;
254 p
= q
, q
= q
->ipqe_q
.le_next
)
255 if (SEQ_GT(q
->ipqe_tcp
->ti_seq
, th
->th_seq
))
259 * If there is a preceding segment, it may provide some of
260 * our data already. If so, drop the data from the incoming
261 * segment. If it provides all of our data, drop us.
264 register struct tcpiphdr
*phdr
= p
->ipqe_tcp
;
267 /* conversion to int (in i) handles seq wraparound */
268 i
= phdr
->ti_seq
+ phdr
->ti_len
- th
->th_seq
;
271 tcpstat
.tcps_rcvduppack
++;
272 tcpstat
.tcps_rcvdupbyte
+= tilen
;
274 FREE(tiqe
, M_SONAME
);
276 #if 1 /* XXX: NetBSD just return 0 here */
278 * Try to present any queued data
279 * at the left window edge to the user.
280 * This is needed after the 3-WHS
283 goto present
; /* ??? */
291 tcpstat
.tcps_rcvoopack
++;
292 tcpstat
.tcps_rcvoobyte
+= tilen
;
295 * While we overlap succeeding segments trim them or,
296 * if they are completely covered, dequeue them.
299 register struct tcpiphdr
*qhdr
= q
->ipqe_tcp
;
300 register int i
= (th
->th_seq
+ tilen
) - qhdr
->ti_seq
;
304 if (i
< qhdr
->ti_len
) {
310 nq
= q
->ipqe_q
.le_next
;
312 LIST_REMOVE(q
, ipqe_q
);
317 /* Insert the new fragment queue entry into place. */
320 * There is a IP or IPv6 header in the mbuf before th
321 * so there is space for an ip header (for the length field)
324 ((struct tcpiphdr *)(((char *)(x)) - (sizeof (struct ip))))
326 tiqe
->ipqe_tcp
= thtoti(th
);
327 tiqe
->ipqe_tcp
->ti_len
= tilen
;
329 LIST_INSERT_HEAD(&tp
->segq
, tiqe
, ipqe_q
);
331 LIST_INSERT_AFTER(p
, tiqe
, ipqe_q
);
336 * Present data to user, advancing rcv_nxt through
337 * completed sequence space.
339 if (!TCPS_HAVEESTABLISHED(tp
->t_state
))
341 q
= tp
->segq
.lh_first
;
342 if (!q
|| q
->ipqe_tcp
->ti_seq
!= tp
->rcv_nxt
)
346 * XXX from INRIA for NetBSD, but should not happen because
347 * TCPS_HAVEESTABLISHED(tp->t_state) should be true here.
349 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& q
->ipqe_tcp
->ti_len
)
353 tp
->rcv_nxt
+= q
->ipqe_tcp
->ti_len
;
354 flags
= q
->ipqe_tcp
->ti_flags
& TH_FIN
;
355 nq
= q
->ipqe_q
.le_next
;
356 LIST_REMOVE(q
, ipqe_q
);
357 if (so
->so_state
& SS_CANTRCVMORE
)
360 sbappend(&so
->so_rcv
, q
->ipqe_m
);
363 } while (q
&& q
->ipqe_tcp
->ti_seq
== tp
->rcv_nxt
);
369 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
370 (((thtoti(th
)->ti_src
.s_addr
& 0xffff) << 16) | (thtoti(th
)->ti_dst
.s_addr
& 0xffff)),
371 th
->th_seq
, th
->th_ack
, th
->th_win
);
378 * TCP input routine, follows pages 65-76 of the
379 * protocol specification dated September, 1981 very closely.
383 tcp6_input(mp
, offp
, proto
)
387 tcp_input(*mp
, *offp
);
397 register struct tcphdr
*th
;
398 register struct ip
*ip
= NULL
;
399 register struct ipovly
*ipov
;
400 register struct inpcb
*inp
;
406 register struct tcpcb
*tp
= 0;
407 register int thflags
;
408 struct socket
*so
= 0;
409 int todrop
, acked
, ourfinisacked
, needoutput
= 0;
410 struct in_addr laddr
;
412 struct in6_addr laddr6
;
417 struct tcpopt to
; /* options in this segment */
418 struct rmxp_tao
*taop
; /* pointer to our TAO cache entry */
419 struct rmxp_tao tao_noncached
; /* in case there's no cached entry */
420 int need_sowwakeup
= 0;
421 int need_sorwakeup
= 0;
426 struct ip6_hdr
*ip6
= NULL
;
429 #define lgminh (sizeof (struct tcpiphdr))
431 int isipv6
= (mtod(m
, struct ip
*)->ip_v
== 6) ? 1 : 0;
433 struct proc
*proc0
=current_proc();
435 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_START
,0,0,0,0,0);
437 bzero((char *)&to
, sizeof(to
));
439 tcpstat
.tcps_rcvtotal
++;
441 * Get IP and TCP header together in first mbuf.
442 * Note: IP leaves IP header in first mbuf.
444 th
= mtod(m
, struct tcpiphdr
*);
446 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
447 (((thtoti(th
)->ti_src
.s_addr
& 0xffff) << 16) | (thtoti(th
)->ti_dst
.s_addr
& 0xffff)),
448 th
->th_seq
, th
->th_ack
, th
->th_win
);
452 ip6
= mtod(m
, struct ip6_hdr
*);
453 lgminh
= sizeof(struct tcpip6hdr
);
455 lgminh
= sizeof(struct tcpiphdr
);
457 ip
= mtod(m
, struct ip
*);
458 ipov
= (struct ipovly
*)ip
;
464 /* XXX not a good place to put this into... */
466 m
&& (m
->m_flags
& M_ANYCAST6
)) {
467 icmp6_error(m
, ICMP6_DST_UNREACH
, ICMP6_DST_UNREACH_ADDR
,
468 (caddr_t
)&ip6
->ip6_dst
- (caddr_t
)ip6
);
475 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct tcphdr
), );
476 ip6
= mtod(m
, struct ip6_hdr
*);
477 tilen
= ntohs(ip6
->ip6_plen
) - off
+ sizeof(*ip6
);
479 if (in6_cksum(m
, IPPROTO_TCP
, off
, tilen
)) {
480 tcpstat
.tcps_rcvbadsum
++;
483 th
= (struct tcphdr
*)((caddr_t
)ip6
+ off
);
488 * Get IP and TCP header together in first mbuf.
489 * Note: IP leaves IP header in first mbuf.
491 /* XXX: should we still require this for IPv4? */
492 if (off
> sizeof (struct ip
)) {
493 ip_stripoptions(m
, (struct mbuf
*)0);
494 off
= sizeof(struct ip
);
495 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
496 m
->m_pkthdr
.csum_flags
= 0; /* invalidate hwcksuming */
498 if (m
->m_len
< lgminh
) {
499 if ((m
= m_pullup(m
, lgminh
)) == 0) {
500 tcpstat
.tcps_rcvshort
++;
504 ip
= mtod(m
, struct ip
*);
505 ipov
= (struct ipovly
*)ip
;
506 th
= (struct tcphdr
*)((caddr_t
)ip
+ off
);
508 len
= sizeof (struct ip
) + tilen
;
510 if (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) {
512 if (apple_hwcksum_rx
&& (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)) {
514 bzero(ipov
->ih_x1
, sizeof(ipov
->ih_x1
));
515 ipov
->ih_len
= (u_short
)tilen
;
517 pseudo
= in_cksum(m
, sizeof (struct ip
));
518 th
->th_sum
= in_addword(pseudo
, (m
->m_pkthdr
.csum_data
& 0xFFFF));
521 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
)
522 th
->th_sum
= m
->m_pkthdr
.csum_data
;
523 else goto dotcpcksum
;
525 th
->th_sum
^= 0xffff;
529 * Checksum extended TCP header and data.
533 len
= sizeof (struct ip
) + tilen
;
534 bzero(ipov
->ih_x1
, sizeof(ipov
->ih_x1
));
535 ipov
->ih_len
= (u_short
)tilen
;
537 th
= (struct tcphdr
*)((caddr_t
)ip
+ off
);
538 th
->th_sum
= in_cksum(m
, len
);
543 tcpstat
.tcps_rcvbadsum
++;
549 * Check that TCP offset makes sense,
550 * pull out TCP options and adjust length. XXX
552 toff
= th
->th_off
<< 2;
553 if (toff
< sizeof (struct tcphdr
) || toff
> tilen
) {
554 tcpstat
.tcps_rcvbadoff
++;
558 if (toff
> sizeof (struct tcphdr
)) {
561 IP6_EXTHDR_CHECK(m
, off
, toff
, );
562 ip6
= mtod(m
, struct ip6_hdr
*);
563 th
= (struct tcphdr
*)((caddr_t
)ip6
+ off
);
567 if (m
->m_len
< sizeof(struct ip
) + toff
) {
568 if ((m
= m_pullup(m
, sizeof (struct ip
) + toff
)) == 0) {
569 tcpstat
.tcps_rcvshort
++;
572 ip
= mtod(m
, struct ip
*);
573 ipov
= (struct ipovly
*)ip
;
574 th
= (struct tcphdr
*)((caddr_t
)ip
+ off
);
577 optlen
= toff
- sizeof (struct tcphdr
);
578 optp
= (u_char
*)(th
+ 1);
580 * Do quick retrieval of timestamp options ("options
581 * prediction?"). If timestamp is the only option and it's
582 * formatted as recommended in RFC 1323 appendix A, we
583 * quickly get the values now and not bother calling
584 * tcp_dooptions(), etc.
586 if ((optlen
== TCPOLEN_TSTAMP_APPA
||
587 (optlen
> TCPOLEN_TSTAMP_APPA
&&
588 optp
[TCPOLEN_TSTAMP_APPA
] == TCPOPT_EOL
)) &&
589 *(u_int32_t
*)optp
== htonl(TCPOPT_TSTAMP_HDR
) &&
590 (th
->th_flags
& TH_SYN
) == 0) {
591 to
.to_flag
|= TOF_TS
;
592 to
.to_tsval
= ntohl(*(u_int32_t
*)(optp
+ 4));
593 to
.to_tsecr
= ntohl(*(u_int32_t
*)(optp
+ 8));
594 optp
= NULL
; /* we've parsed the options */
597 thflags
= th
->th_flags
;
600 * Convert TCP protocol specific fields to host format.
608 * Drop TCP, IP headers and TCP options.
610 hdroptlen
= off
+toff
;
611 m
->m_data
+= hdroptlen
;
612 m
->m_len
-= hdroptlen
;
615 * Locate pcb for segment.
618 #if IPFIREWALL_FORWARD
619 if (ip_fw_fwd_addr
!= NULL
625 * Diverted. Pretend to be the destination.
626 * already got one like this?
628 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
629 ip
->ip_dst
, th
->th_dport
, 0, m
->m_pkthdr
.rcvif
);
632 * No, then it's new. Try find the ambushing socket
634 if (!ip_fw_fwd_addr
->sin_port
) {
635 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
,
636 th
->th_sport
, ip_fw_fwd_addr
->sin_addr
,
637 th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
639 inp
= in_pcblookup_hash(&tcbinfo
,
640 ip
->ip_src
, th
->th_sport
,
641 ip_fw_fwd_addr
->sin_addr
,
642 ntohs(ip_fw_fwd_addr
->sin_port
), 1,
646 ip_fw_fwd_addr
= NULL
;
648 #endif /* IPFIREWALL_FORWARD */
652 inp
= in6_pcblookup_hash(&tcbinfo
, &ip6
->ip6_src
, th
->th_sport
,
653 &ip6
->ip6_dst
, th
->th_dport
, 1,
657 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
658 ip
->ip_dst
, th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
661 /* due to difference from other BSD stacks */
662 m
->m_data
-= hdroptlen
;
663 m
->m_len
+= hdroptlen
;
666 if (inp
!= NULL
&& ipsec6_in_reject_so(m
, inp
->inp_socket
)) {
667 ipsec6stat
.in_polvio
++;
672 if (inp
!= NULL
&& ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
673 ipsecstat
.in_polvio
++;
676 m
->m_data
+= hdroptlen
;
677 m
->m_len
-= hdroptlen
;
681 * If the state is CLOSED (i.e., TCB does not exist) then
682 * all data in the incoming segment is discarded.
683 * If the TCB exists but is in CLOSED state, it is embryonic,
684 * but should either do a listen or a connect soon.
687 if (log_in_vain
&& thflags
& TH_SYN
) {
689 char buf
[INET6_ADDRSTRLEN
];
691 char buf
[4*sizeof "123"];
696 strcpy(buf
, ip6_sprintf(&ip6
->ip6_dst
));
698 "Connection attempt to TCP %s:%d from %s:%d\n",
699 buf
, ntohs(th
->th_dport
),
700 ip6_sprintf(&ip6
->ip6_src
),
701 ntohs(th
->th_sport
));
704 strcpy(buf
, inet_ntoa(ip
->ip_dst
));
706 "Connection attempt to TCP %s:%d from %s:%d\n",
707 buf
, ntohs(th
->th_dport
), inet_ntoa(ip
->ip_src
),
708 ntohs(th
->th_sport
));
714 if (badport_bandlim(1) < 0)
722 if (tp
->t_state
== TCPS_CLOSED
)
725 * Bogus state when listening port owned by SharedIP with loopback as the
726 * only configured interface: BlueBox does not filters loopback
728 if (tp
->t_state
== TCP_NSTATES
)
731 /* Unscale the window into a 32-bit value. */
732 if ((thflags
& TH_SYN
) == 0)
733 tiwin
= th
->th_win
<< tp
->snd_scale
;
737 so
= inp
->inp_socket
;
738 if (so
->so_options
& (SO_DEBUG
|SO_ACCEPTCONN
)) {
740 if (so
->so_options
& SO_DEBUG
) {
741 ostate
= tp
->t_state
;
744 tcp_saveip
._tcp_si6
= *ip6
;
746 tcp_saveip
._tcp_si4
= *ip
;
754 if (so
->so_options
& SO_ACCEPTCONN
) {
755 register struct tcpcb
*tp0
= tp
;
761 struct inpcb
*oinp
= sotoinpcb(so
);
765 if ((thflags
& (TH_RST
|TH_ACK
|TH_SYN
)) != TH_SYN
) {
767 * Note: dropwithreset makes sure we don't
768 * send a RST in response to a RST.
770 if (thflags
& TH_ACK
) {
771 tcpstat
.tcps_badsyn
++;
777 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_START
,0,0,0,0,0);
778 so2
= sonewconn(so
, 0);
782 tcpstat
.tcps_listendrop
++;
783 so2
= sodropablereq(so
);
785 tcp_drop(sototcpcb(so2
), ETIMEDOUT
);
786 so2
= sonewconn(so
, 0);
796 * This is ugly, but ....
798 * Mark socket as temporary until we're
799 * committed to keeping it. The code at
800 * ``drop'' and ``dropwithreset'' check the
801 * flag dropsocket to see if the temporary
802 * socket created here should be discarded.
803 * We mark the socket as discardable until
804 * we're committed to it below in TCPS_LISTEN.
807 inp
= (struct inpcb
*)so
->so_pcb
;
810 inp
->in6p_laddr
= ip6
->ip6_dst
;
812 if (ip6_mapped_addr_on
) {
813 inp
->inp_vflag
&= ~INP_IPV6
;
814 inp
->inp_vflag
|= INP_IPV4
;
817 inp
->inp_laddr
= ip
->ip_dst
;
822 inp
->inp_lport
= th
->th_dport
;
823 if (in_pcbinshash(inp
) != 0) {
825 * Undo the assignments above if we failed to put
826 * the PCB on the hash lists.
830 inp
->in6p_laddr
= in6addr_any
;
833 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
839 * from IPsec perspective, it is important to do it
840 * after making actual listening socket.
841 * otherwise, cached security association will bark.
843 * Subject: (KAME-snap 748)
844 * From: Wayne Knowles <w.knowles@niwa.cri.nz>
846 if ((thflags
& (TH_RST
|TH_ACK
|TH_SYN
)) != TH_SYN
) {
848 * Note: dropwithreset makes sure we don't
849 * send a RST in response to a RST.
851 if (thflags
& TH_ACK
) {
852 tcpstat
.tcps_badsyn
++;
860 struct ip6_recvpktopts newopts
;
863 * Inherit socket options from the listening
865 * Note that in6p_inputopts are not (even
866 * should not be) copied, since it stores
867 * previously received options and is used to
868 * detect if each new option is different than
869 * the previous one and hence should be passed
871 * If we copied in6p_inputopts, a user would
872 * not be able to receive options just after
873 * calling the accept system call.
876 oinp
->inp_flags
& INP_CONTROLOPTS
;
877 if (oinp
->in6p_outputopts
)
878 inp
->in6p_outputopts
=
879 ip6_copypktopts(oinp
->in6p_outputopts
,
883 inp
->inp_options
= ip_srcroute();
885 /* copy old policy into new socket's */
886 if (ipsec_copy_policy(sotoinpcb(oso
)->inp_sp
,
888 printf("tcp_input: could not copy policy\n");
892 tp
->t_state
= TCPS_LISTEN
;
893 tp
->t_flags
|= tp0
->t_flags
& (TF_NOPUSH
|TF_NOOPT
);
895 /* Compute proper scaling value from buffer space */
896 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
897 TCP_MAXWIN
<< tp
->request_r_scale
< so
->so_rcv
.sb_hiwat
)
898 tp
->request_r_scale
++;
900 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_END
,0,0,0,0,0);
905 /* save packet options if user wanted */
906 if (isipv6
&& (inp
->in6p_flags
& INP_CONTROLOPTS
) != 0) {
907 struct ip6_recvpktopts opts6
;
910 * Temporarily re-adjusting the mbuf before ip6_savecontrol(),
911 * which is necessary for FreeBSD only due to difference from
913 * XXX: we'll soon make a more natural fix after getting a
917 m
->m_data
-= hdroptlen
;
918 m
->m_len
+= hdroptlen
;
920 ip6_savecontrol(inp
, ip6
, m
, &opts6
, &inp
->in6p_inputopts
);
921 if (inp
->in6p_inputopts
)
922 ip6_update_recvpcbopt(inp
->in6p_inputopts
, &opts6
);
924 if (sbappendcontrol(&inp
->in6p_socket
->so_rcv
,
930 m
->m_data
+= hdroptlen
; /* XXX */
931 m
->m_len
-= hdroptlen
; /* XXX */
937 * Segment received on connection.
938 * Reset idle time and keep-alive timer.
941 if (TCPS_HAVEESTABLISHED(tp
->t_state
))
942 tp
->t_timer
[TCPT_KEEP
] = tcp_keepidle
;
945 * Process options if not in LISTEN state,
946 * else do it below (after getting remote address).
948 if (tp
->t_state
!= TCPS_LISTEN
&& optp
)
949 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
950 if (th
->th_flags
& TH_SYN
)
951 tcp_mss(tp
, to
.to_maxseg
, isipv6
); /* sets t_maxseg */
954 * Header prediction: check for the two common cases
955 * of a uni-directional data xfer. If the packet has
956 * no control flags, is in-sequence, the window didn't
957 * change and we're not retransmitting, it's a
958 * candidate. If the length is zero and the ack moved
959 * forward, we're the sender side of the xfer. Just
960 * free the data acked & wake any higher level process
961 * that was blocked waiting for space. If the length
962 * is non-zero and the ack didn't move, we're the
963 * receiver side. If we're getting packets in-order
964 * (the reassembly queue is empty), add the data to
965 * the socket buffer and note that we need a delayed ack.
966 * Make sure that the hidden state-flags are also off.
967 * Since we check for TCPS_ESTABLISHED above, it can only
970 if (tp
->t_state
== TCPS_ESTABLISHED
&&
971 (thflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
)) == TH_ACK
&&
972 ((tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
)) == 0) &&
973 ((to
.to_flag
& TOF_TS
) == 0 ||
974 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
976 * Using the CC option is compulsory if once started:
977 * the segment is OK if no T/TCP was negotiated or
978 * if the segment has a CC option equal to CCrecv
980 ((tp
->t_flags
& (TF_REQ_CC
|TF_RCVD_CC
)) != (TF_REQ_CC
|TF_RCVD_CC
) ||
981 ((to
.to_flag
& TOF_CC
) != 0 && to
.to_cc
== tp
->cc_recv
)) &&
982 th
->th_seq
== tp
->rcv_nxt
&&
983 tiwin
&& tiwin
== tp
->snd_wnd
&&
984 tp
->snd_nxt
== tp
->snd_max
) {
987 * If last ACK falls within this segment's sequence numbers,
988 * record the timestamp.
989 * NOTE that the test is modified according to the latest
990 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
992 if ((to
.to_flag
& TOF_TS
) != 0 &&
993 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
994 tp
->ts_recent_age
= tcp_now
;
995 tp
->ts_recent
= to
.to_tsval
;
999 if (SEQ_GT(th
->th_ack
, tp
->snd_una
) &&
1000 SEQ_LEQ(th
->th_ack
, tp
->snd_max
) &&
1001 tp
->snd_cwnd
>= tp
->snd_wnd
&&
1002 tp
->t_dupacks
< tcprexmtthresh
) {
1004 * this is a pure ack for outstanding data.
1006 ++tcpstat
.tcps_predack
;
1007 if ((to
.to_flag
& TOF_TS
) != 0)
1009 tcp_now
- to
.to_tsecr
+ 1);
1010 else if (tp
->t_rtt
&&
1011 SEQ_GT(th
->th_ack
, tp
->t_rtseq
))
1012 tcp_xmit_timer(tp
, tp
->t_rtt
);
1013 acked
= th
->th_ack
- tp
->snd_una
;
1014 tcpstat
.tcps_rcvackpack
++;
1015 tcpstat
.tcps_rcvackbyte
+= acked
;
1016 sbdrop(&so
->so_snd
, acked
);
1017 tp
->snd_una
= th
->th_ack
;
1020 /* some progress has been done */
1026 * If all outstanding data are acked, stop
1027 * retransmit timer, otherwise restart timer
1028 * using current (possibly backed-off) value.
1029 * If process is waiting for space,
1030 * wakeup/selwakeup/signal. If data
1031 * are ready to send, let tcp_output
1032 * decide between more output or persist.
1034 if (tp
->snd_una
== tp
->snd_max
)
1035 tp
->t_timer
[TCPT_REXMT
] = 0;
1036 else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
1037 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
1039 if (so
->so_snd
.sb_cc
)
1040 (void) tcp_output(tp
);
1042 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
1045 } else if (th
->th_ack
== tp
->snd_una
&&
1046 tp
->segq
.lh_first
== NULL
&&
1047 tilen
<= sbspace(&so
->so_rcv
)) {
1049 * this is a pure, in-sequence data packet
1050 * with nothing on the reassembly queue and
1051 * we have enough buffer space to take it.
1053 ++tcpstat
.tcps_preddat
;
1054 tp
->rcv_nxt
+= tilen
;
1055 tcpstat
.tcps_rcvpack
++;
1056 tcpstat
.tcps_rcvbyte
+= tilen
;
1058 /* some progress has been done */
1062 sbappend(&so
->so_rcv
, m
);
1063 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
1064 (((thtoti(th
)->ti_src
.s_addr
& 0xffff) << 16) | (thtoti(th
)->ti_dst
.s_addr
& 0xffff)),
1065 th
->th_seq
, th
->th_ack
, th
->th_win
);
1066 if (tcp_delack_enabled
) {
1067 if (last_active_conn_count
> DELACK_BITMASK_THRESH
)
1068 TCP_DELACK_BITSET(tp
->t_inpcb
->hash_element
);
1069 tp
->t_flags
|= TF_DELACK
;
1071 tp
->t_flags
|= TF_ACKNOW
;
1075 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
1081 * Calculate amount of space in receive window,
1082 * and then do TCP input processing.
1083 * Receive window is amount of space in rcv queue,
1084 * but not less than advertised window.
1088 win
= sbspace(&so
->so_rcv
);
1091 tp
->rcv_wnd
= imax(win
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
1094 switch (tp
->t_state
) {
1097 * If the state is LISTEN then ignore segment if it contains an RST.
1098 * If the segment contains an ACK then it is bad and send a RST.
1099 * If it does not contain a SYN then it is not interesting; drop it.
1100 * If it is from this socket, drop it, it must be forged.
1101 * Don't bother responding if the destination was a broadcast.
1102 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
1103 * tp->iss, and send a segment:
1104 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1105 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1106 * Fill in remote peer address fields if not previously specified.
1107 * Enter SYN_RECEIVED state, and process any other fields of this
1108 * segment in this state.
1111 register struct sockaddr_in
*sin
;
1113 register struct sockaddr_in6
*sin6
;
1116 if (thflags
& TH_RST
)
1118 if (thflags
& TH_ACK
)
1120 if ((thflags
& TH_SYN
) == 0)
1122 if (th
->th_dport
== th
->th_sport
) {
1125 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
1130 if (ip
->ip_dst
.s_addr
== ip
->ip_src
.s_addr
)
1136 if (m
->m_flags
& (M_BCAST
|M_MCAST
) ||
1137 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
))
1141 * Perhaps this should be a call/macro
1142 * to a function like in6_pcbconnect(), but almost
1143 * all of the checks have been done: we know
1144 * that the association is unique, and the
1145 * local address is always set here.
1147 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1148 inp
->in6p_laddr
= ip6
->ip6_dst
;
1149 inp
->in6p_faddr
= ip6
->ip6_src
;
1150 inp
->inp_fport
= th
->th_sport
;
1152 /* TODO: flowinfo initialization */
1156 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
,
1157 M_SONAME
, M_NOWAIT
);
1160 bzero(sin6
, sizeof(*sin6
));
1161 sin6
->sin6_family
= AF_INET6
;
1162 sin6
->sin6_len
= sizeof(*sin6
);
1163 sin6
->sin6_addr
= ip6
->ip6_src
;
1164 sin6
->sin6_port
= th
->th_sport
;
1165 laddr6
= inp
->in6p_laddr
;
1166 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1167 inp
->in6p_laddr
= ip6
->ip6_dst
;
1168 if (in6_pcbconnect(inp
, (struct sockaddr
*)sin6
,
1170 inp
->in6p_laddr
= laddr6
;
1171 FREE(sin6
, M_SONAME
);
1174 FREE(sin6
, M_SONAME
);
1180 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
1181 * in_broadcast() should never return true on a received
1182 * packet with M_BCAST not set.
1184 if (m
->m_flags
& (M_BCAST
|M_MCAST
) ||
1185 IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)))
1187 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
,
1191 sin
->sin_family
= AF_INET
;
1192 sin
->sin_len
= sizeof(*sin
);
1193 sin
->sin_addr
= ip
->ip_src
;
1194 sin
->sin_port
= th
->th_sport
;
1195 bzero((caddr_t
)sin
->sin_zero
, sizeof(sin
->sin_zero
));
1196 laddr
= inp
->inp_laddr
;
1197 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
1198 inp
->inp_laddr
= ip
->ip_dst
;
1199 if (in_pcbconnect(inp
, (struct sockaddr
*)sin
, &proc0
)) {
1200 inp
->inp_laddr
= laddr
;
1201 FREE(sin
, M_SONAME
);
1204 FREE(sin
, M_SONAME
);
1209 tp
->t_template
= tcp_template(tp
);
1210 if (tp
->t_template
== 0) {
1211 tp
= tcp_drop(tp
, ENOBUFS
);
1212 dropsocket
= 0; /* socket is already gone */
1215 if ((taop
= tcp_gettaocache(inp
)) == NULL
) {
1216 taop
= &tao_noncached
;
1217 bzero(taop
, sizeof(*taop
));
1219 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
1220 if (th
->th_flags
& TH_SYN
)
1221 tcp_mss(tp
, to
.to_maxseg
, isipv6
); /* sets t_maxseg */
1225 #ifdef TCP_COMPAT_42
1226 tcp_iss
+= TCP_ISSINCR
/2;
1229 tp
->iss
= tcp_rndiss_next();
1230 #endif /* TCP_COMPAT_42 */
1232 tp
->irs
= th
->th_seq
;
1233 tcp_sendseqinit(tp
);
1236 * Initialization of the tcpcb for transaction;
1237 * set SND.WND = SEG.WND,
1238 * initialize CCsend and CCrecv.
1240 tp
->snd_wnd
= tiwin
; /* initial send-window */
1241 tp
->cc_send
= CC_INC(tcp_ccgen
);
1242 tp
->cc_recv
= to
.to_cc
;
1244 * Perform TAO test on incoming CC (SEG.CC) option, if any.
1245 * - compare SEG.CC against cached CC from the same host,
1247 * - if SEG.CC > chached value, SYN must be new and is accepted
1248 * immediately: save new CC in the cache, mark the socket
1249 * connected, enter ESTABLISHED state, turn on flag to
1250 * send a SYN in the next segment.
1251 * A virtual advertised window is set in rcv_adv to
1252 * initialize SWS prevention. Then enter normal segment
1253 * processing: drop SYN, process data and FIN.
1254 * - otherwise do a normal 3-way handshake.
1256 if ((to
.to_flag
& TOF_CC
) != 0) {
1257 if (((tp
->t_flags
& TF_NOPUSH
) != 0) &&
1258 taop
->tao_cc
!= 0 && CC_GT(to
.to_cc
, taop
->tao_cc
)) {
1260 taop
->tao_cc
= to
.to_cc
;
1261 if (tp
->t_state
!= TCPS_ESTABLISHED
)
1262 current_active_connections
++;
1264 tp
->t_state
= TCPS_ESTABLISHED
;
1267 * If there is a FIN, or if there is data and the
1268 * connection is local, then delay SYN,ACK(SYN) in
1269 * the hope of piggy-backing it on a response
1270 * segment. Otherwise must send ACK now in case
1271 * the other side is slow starting.
1273 if (tcp_delack_enabled
&&
1274 ((thflags
& TH_FIN
) ||
1277 (isipv6
&& in6_localaddr(&inp
->in6p_faddr
))
1281 in_localaddr(inp
->inp_faddr
)
1286 if (last_active_conn_count
> DELACK_BITMASK_THRESH
)
1287 TCP_DELACK_BITSET(tp
->t_inpcb
->hash_element
);
1289 tp
->t_flags
|= (TF_DELACK
| TF_NEEDSYN
);
1292 tp
->t_flags
|= (TF_ACKNOW
| TF_NEEDSYN
);
1295 * Limit the `virtual advertised window' to TCP_MAXWIN
1296 * here. Even if we requested window scaling, it will
1297 * become effective only later when our SYN is acked.
1299 tp
->rcv_adv
+= min(tp
->rcv_wnd
, TCP_MAXWIN
);
1300 tcpstat
.tcps_connects
++;
1302 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
1303 dropsocket
= 0; /* committed to socket */
1304 tcpstat
.tcps_accepts
++;
1307 /* else do standard 3-way handshake */
1310 * No CC option, but maybe CC.NEW:
1311 * invalidate cached value.
1316 * TAO test failed or there was no CC option,
1317 * do a standard 3-way handshake.
1319 tp
->t_flags
|= TF_ACKNOW
;
1320 tp
->t_state
= TCPS_SYN_RECEIVED
;
1321 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
1322 dropsocket
= 0; /* committed to socket */
1323 tcpstat
.tcps_accepts
++;
1328 * If the state is SYN_RECEIVED:
1329 * if seg contains an ACK, but not for our SYN/ACK, send a RST.
1331 case TCPS_SYN_RECEIVED
:
1332 if ((thflags
& TH_ACK
) &&
1333 (SEQ_LEQ(th
->th_ack
, tp
->snd_una
) ||
1334 SEQ_GT(th
->th_ack
, tp
->snd_max
)))
1339 * If the state is SYN_SENT:
1340 * if seg contains an ACK, but not for our SYN, drop the input.
1341 * if seg contains a RST, then drop the connection.
1342 * if seg does not contain SYN, then drop it.
1343 * Otherwise this is an acceptable SYN segment
1344 * initialize tp->rcv_nxt and tp->irs
1345 * if seg contains ack then advance tp->snd_una
1346 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1347 * arrange for segment to be acked (eventually)
1348 * continue processing rest of data/controls, beginning with URG
1351 if ((taop
= tcp_gettaocache(inp
)) == NULL
) {
1352 taop
= &tao_noncached
;
1353 bzero(taop
, sizeof(*taop
));
1356 if ((thflags
& TH_ACK
) &&
1357 (SEQ_LEQ(th
->th_ack
, tp
->iss
) ||
1358 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
1360 * If we have a cached CCsent for the remote host,
1361 * hence we haven't just crashed and restarted,
1362 * do not send a RST. This may be a retransmission
1363 * from the other side after our earlier ACK was lost.
1364 * Our new SYN, when it arrives, will serve as the
1367 if (taop
->tao_ccsent
!= 0)
1372 if (thflags
& TH_RST
) {
1373 if (thflags
& TH_ACK
) {
1374 tp
= tcp_drop(tp
, ECONNREFUSED
);
1375 postevent(so
, 0, EV_RESET
);
1379 if ((thflags
& TH_SYN
) == 0)
1381 tp
->snd_wnd
= th
->th_win
; /* initial send window */
1382 tp
->cc_recv
= to
.to_cc
; /* foreign CC */
1384 tp
->irs
= th
->th_seq
;
1386 if (thflags
& TH_ACK
) {
1388 * Our SYN was acked. If segment contains CC.ECHO
1389 * option, check it to make sure this segment really
1390 * matches our SYN. If not, just drop it as old
1391 * duplicate, but send an RST if we're still playing
1392 * by the old rules. If no CC.ECHO option, make sure
1393 * we don't get fooled into using T/TCP.
1395 if (to
.to_flag
& TOF_CCECHO
) {
1396 if (tp
->cc_send
!= to
.to_ccecho
)
1397 if (taop
->tao_ccsent
!= 0)
1402 tp
->t_flags
&= ~TF_RCVD_CC
;
1403 tcpstat
.tcps_connects
++;
1405 /* Do window scaling on this connection? */
1406 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
1407 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
1408 tp
->snd_scale
= tp
->requested_s_scale
;
1409 tp
->rcv_scale
= tp
->request_r_scale
;
1411 /* Segment is acceptable, update cache if undefined. */
1412 if (taop
->tao_ccsent
== 0)
1413 taop
->tao_ccsent
= to
.to_ccecho
;
1415 tp
->rcv_adv
+= tp
->rcv_wnd
;
1416 tp
->snd_una
++; /* SYN is acked */
1418 * If there's data, delay ACK; if there's also a FIN
1419 * ACKNOW will be turned on later.
1421 if (tcp_delack_enabled
&& tilen
!= 0) {
1422 if (last_active_conn_count
> DELACK_BITMASK_THRESH
)
1423 TCP_DELACK_BITSET(tp
->t_inpcb
->hash_element
);
1424 tp
->t_flags
|= TF_DELACK
;
1427 tp
->t_flags
|= TF_ACKNOW
;
1429 * Received <SYN,ACK> in SYN_SENT[*] state.
1431 * SYN_SENT --> ESTABLISHED
1432 * SYN_SENT* --> FIN_WAIT_1
1434 if (tp
->t_flags
& TF_NEEDFIN
) {
1435 tp
->t_state
= TCPS_FIN_WAIT_1
;
1436 tp
->t_flags
&= ~TF_NEEDFIN
;
1439 if (tp
->t_state
!= TCPS_ESTABLISHED
)
1440 current_active_connections
++;
1441 tp
->t_state
= TCPS_ESTABLISHED
;
1442 tp
->t_timer
[TCPT_KEEP
] = tcp_keepidle
;
1446 * Received initial SYN in SYN-SENT[*] state => simul-
1447 * taneous open. If segment contains CC option and there is
1448 * a cached CC, apply TAO test; if it succeeds, connection is
1449 * half-synchronized. Otherwise, do 3-way handshake:
1450 * SYN-SENT -> SYN-RECEIVED
1451 * SYN-SENT* -> SYN-RECEIVED*
1452 * If there was no CC option, clear cached CC value.
1454 tp
->t_flags
|= TF_ACKNOW
;
1455 tp
->t_timer
[TCPT_REXMT
] = 0;
1456 if (to
.to_flag
& TOF_CC
) {
1457 if (taop
->tao_cc
!= 0 &&
1458 CC_GT(to
.to_cc
, taop
->tao_cc
)) {
1460 * update cache and make transition:
1461 * SYN-SENT -> ESTABLISHED*
1462 * SYN-SENT* -> FIN-WAIT-1*
1464 taop
->tao_cc
= to
.to_cc
;
1465 if (tp
->t_flags
& TF_NEEDFIN
) {
1466 tp
->t_state
= TCPS_FIN_WAIT_1
;
1467 tp
->t_flags
&= ~TF_NEEDFIN
;
1469 if (tp
->t_state
!= TCPS_ESTABLISHED
)
1470 current_active_connections
++;
1471 tp
->t_state
= TCPS_ESTABLISHED
;
1472 tp
->t_timer
[TCPT_KEEP
] = tcp_keepidle
;
1474 tp
->t_flags
|= TF_NEEDSYN
;
1476 tp
->t_state
= TCPS_SYN_RECEIVED
;
1478 /* CC.NEW or no option => invalidate cache */
1480 tp
->t_state
= TCPS_SYN_RECEIVED
;
1486 * Advance th->th_seq to correspond to first data byte.
1487 * If data, trim to stay within window,
1488 * dropping FIN if necessary.
1491 if (tilen
> tp
->rcv_wnd
) {
1492 todrop
= tilen
- tp
->rcv_wnd
;
1494 tilen
= tp
->rcv_wnd
;
1496 tcpstat
.tcps_rcvpackafterwin
++;
1497 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
1499 tp
->snd_wl1
= th
->th_seq
- 1;
1500 tp
->rcv_up
= th
->th_seq
;
1502 * Client side of transaction: already sent SYN and data.
1503 * If the remote host used T/TCP to validate the SYN,
1504 * our data will be ACK'd; if so, enter normal data segment
1505 * processing in the middle of step 5, ack processing.
1506 * Otherwise, goto step 6.
1508 if (thflags
& TH_ACK
)
1512 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1513 * if segment contains a SYN and CC [not CC.NEW] option:
1514 * if state == TIME_WAIT and connection duration > MSL,
1515 * drop packet and send RST;
1517 * if SEG.CC > CCrecv then is new SYN, and can implicitly
1518 * ack the FIN (and data) in retransmission queue.
1519 * Complete close and delete TCPCB. Then reprocess
1520 * segment, hoping to find new TCPCB in LISTEN state;
1522 * else must be old SYN; drop it.
1523 * else do normal processing.
1527 case TCPS_TIME_WAIT
:
1528 if ((thflags
& TH_SYN
) &&
1529 (to
.to_flag
& TOF_CC
) && tp
->cc_recv
!= 0) {
1530 if (tp
->t_state
== TCPS_TIME_WAIT
&&
1531 tp
->t_duration
> TCPTV_MSL
)
1533 if (CC_GT(to
.to_cc
, tp
->cc_recv
)) {
1540 break; /* continue normal processing */
1544 * States other than LISTEN or SYN_SENT.
1545 * First check the RST flag and sequence number since reset segments
1546 * are exempt from the timestamp and connection count tests. This
1547 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
1548 * below which allowed reset segments in half the sequence space
1549 * to fall though and be processed (which gives forged reset
1550 * segments with a random sequence number a 50 percent chance of
1551 * killing a connection).
1552 * Then check timestamp, if present.
1553 * Then check the connection count, if present.
1554 * Then check that at least some bytes of segment are within
1555 * receive window. If segment begins before rcv_nxt,
1556 * drop leading data (and SYN); if nothing left, just ack.
1559 * If the RST bit is set, check the sequence number to see
1560 * if this is a valid reset segment.
1562 * In all states except SYN-SENT, all reset (RST) segments
1563 * are validated by checking their SEQ-fields. A reset is
1564 * valid if its sequence number is in the window.
1565 * Note: this does not take into account delayed ACKs, so
1566 * we should test against last_ack_sent instead of rcv_nxt.
1567 * Also, it does not make sense to allow reset segments with
1568 * sequence numbers greater than last_ack_sent to be processed
1569 * since these sequence numbers are just the acknowledgement
1570 * numbers in our outgoing packets being echoed back at us,
1571 * and these acknowledgement numbers are monotonically
1573 * If we have multiple segments in flight, the intial reset
1574 * segment sequence numbers will be to the left of last_ack_sent,
1575 * but they will eventually catch up.
1576 * In any case, it never made sense to trim reset segments to
1577 * fit the receive window since RFC 1122 says:
1578 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
1580 * A TCP SHOULD allow a received RST segment to include data.
1583 * It has been suggested that a RST segment could contain
1584 * ASCII text that encoded and explained the cause of the
1585 * RST. No standard has yet been established for such
1588 * If the reset segment passes the sequence number test examine
1590 * SYN_RECEIVED STATE:
1591 * If passive open, return to LISTEN state.
1592 * If active open, inform user that connection was refused.
1593 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1594 * Inform user that connection was reset, and close tcb.
1595 * CLOSING, LAST_ACK, TIME_WAIT STATES
1598 * Drop the segment - see Stevens, vol. 2, p. 964 and
1601 if (thflags
& TH_RST
) {
1602 if (tp
->last_ack_sent
== th
->th_seq
) {
1603 switch (tp
->t_state
) {
1605 case TCPS_SYN_RECEIVED
:
1606 so
->so_error
= ECONNREFUSED
;
1609 case TCPS_ESTABLISHED
:
1610 case TCPS_FIN_WAIT_1
:
1611 case TCPS_CLOSE_WAIT
:
1612 current_active_connections
--;
1616 case TCPS_FIN_WAIT_2
:
1617 so
->so_error
= ECONNRESET
;
1619 postevent(so
, 0, EV_RESET
);
1620 tp
->t_state
= TCPS_CLOSED
;
1621 tcpstat
.tcps_drops
++;
1627 current_active_connections
--;
1631 case TCPS_TIME_WAIT
:
1639 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1640 * and it's less than ts_recent, drop it.
1642 if ((to
.to_flag
& TOF_TS
) != 0 && tp
->ts_recent
&&
1643 TSTMP_LT(to
.to_tsval
, tp
->ts_recent
)) {
1645 /* Check to see if ts_recent is over 24 days old. */
1646 if ((int)(tcp_now
- tp
->ts_recent_age
) > TCP_PAWS_IDLE
) {
1648 * Invalidate ts_recent. If this segment updates
1649 * ts_recent, the age will be reset later and ts_recent
1650 * will get a valid value. If it does not, setting
1651 * ts_recent to zero will at least satisfy the
1652 * requirement that zero be placed in the timestamp
1653 * echo reply when ts_recent isn't valid. The
1654 * age isn't reset until we get a valid ts_recent
1655 * because we don't want out-of-order segments to be
1656 * dropped when ts_recent is old.
1660 tcpstat
.tcps_rcvduppack
++;
1661 tcpstat
.tcps_rcvdupbyte
+= tilen
;
1662 tcpstat
.tcps_pawsdrop
++;
1669 * If T/TCP was negotiated and the segment doesn't have CC,
1670 * or if its CC is wrong then drop the segment.
1671 * RST segments do not have to comply with this.
1673 if ((tp
->t_flags
& (TF_REQ_CC
|TF_RCVD_CC
)) == (TF_REQ_CC
|TF_RCVD_CC
) &&
1674 ((to
.to_flag
& TOF_CC
) == 0 || tp
->cc_recv
!= to
.to_cc
))
1678 * In the SYN-RECEIVED state, validate that the packet belongs to
1679 * this connection before trimming the data to fit the receive
1680 * window. Check the sequence number versus IRS since we know
1681 * the sequence numbers haven't wrapped. This is a partial fix
1682 * for the "LAND" DoS attack.
1684 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& SEQ_LT(th
->th_seq
, tp
->irs
))
1687 todrop
= tp
->rcv_nxt
- th
->th_seq
;
1689 if (thflags
& TH_SYN
) {
1699 * Following if statement from Stevens, vol. 2, p. 960.
1702 || (todrop
== tilen
&& (thflags
& TH_FIN
) == 0)) {
1704 * Any valid FIN must be to the left of the window.
1705 * At this point the FIN must be a duplicate or out
1706 * of sequence; drop it.
1711 * Send an ACK to resynchronize and drop any data.
1712 * But keep on processing for RST or ACK.
1714 tp
->t_flags
|= TF_ACKNOW
;
1716 tcpstat
.tcps_rcvduppack
++;
1717 tcpstat
.tcps_rcvdupbyte
+= todrop
;
1719 tcpstat
.tcps_rcvpartduppack
++;
1720 tcpstat
.tcps_rcvpartdupbyte
+= todrop
;
1723 th
->th_seq
+= todrop
;
1725 if (th
->th_urp
> todrop
)
1726 th
->th_urp
-= todrop
;
1734 * If new data are received on a connection after the
1735 * user processes are gone, then RST the other end.
1737 if ((so
->so_state
& SS_NOFDREF
) &&
1738 tp
->t_state
> TCPS_CLOSE_WAIT
&& tilen
) {
1740 tcpstat
.tcps_rcvafterclose
++;
1745 * If segment ends after window, drop trailing data
1746 * (and PUSH and FIN); if nothing left, just ACK.
1748 todrop
= (th
->th_seq
+tilen
) - (tp
->rcv_nxt
+tp
->rcv_wnd
);
1750 tcpstat
.tcps_rcvpackafterwin
++;
1751 if (todrop
>= tilen
) {
1752 tcpstat
.tcps_rcvbyteafterwin
+= tilen
;
1754 * If a new connection request is received
1755 * while in TIME_WAIT, drop the old connection
1756 * and start over if the sequence numbers
1757 * are above the previous ones.
1759 if (thflags
& TH_SYN
&&
1760 tp
->t_state
== TCPS_TIME_WAIT
&&
1761 SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
1762 #ifdef TCP_COMPAT_42
1763 iss
= tp
->rcv_nxt
+ TCP_ISSINCR
;
1765 iss
= tcp_rndiss_next();
1766 #endif /* TCP_COMPAT_42 */
1771 * If window is closed can only take segments at
1772 * window edge, and have to drop data and PUSH from
1773 * incoming segments. Continue processing, but
1774 * remember to ack. Otherwise, drop segment
1777 if (tp
->rcv_wnd
== 0 && th
->th_seq
== tp
->rcv_nxt
) {
1778 tp
->t_flags
|= TF_ACKNOW
;
1779 tcpstat
.tcps_rcvwinprobe
++;
1783 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
1786 thflags
&= ~(TH_PUSH
|TH_FIN
);
1790 * If last ACK falls within this segment's sequence numbers,
1791 * record its timestamp.
1792 * NOTE that the test is modified according to the latest
1793 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1795 if ((to
.to_flag
& TOF_TS
) != 0 &&
1796 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
1797 tp
->ts_recent_age
= tcp_now
;
1798 tp
->ts_recent
= to
.to_tsval
;
1802 * If a SYN is in the window, then this is an
1803 * error and we send an RST and drop the connection.
1805 if (thflags
& TH_SYN
) {
1806 tp
= tcp_drop(tp
, ECONNRESET
);
1807 postevent(so
, 0, EV_RESET
);
1812 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
1813 * flag is on (half-synchronized state), then queue data for
1814 * later processing; else drop segment and return.
1816 if ((thflags
& TH_ACK
) == 0) {
1817 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
1818 (tp
->t_flags
& TF_NEEDSYN
))
1827 switch (tp
->t_state
) {
1830 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1831 * ESTABLISHED state and continue processing.
1832 * The ACK was checked above.
1834 case TCPS_SYN_RECEIVED
:
1836 tcpstat
.tcps_connects
++;
1838 current_active_connections
++;
1840 /* Do window scaling? */
1841 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
1842 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
1843 tp
->snd_scale
= tp
->requested_s_scale
;
1844 tp
->rcv_scale
= tp
->request_r_scale
;
1847 * Upon successful completion of 3-way handshake,
1848 * update cache.CC if it was undefined, pass any queued
1849 * data to the user, and advance state appropriately.
1851 if ((taop
= tcp_gettaocache(inp
)) != NULL
&&
1853 taop
->tao_cc
= tp
->cc_recv
;
1857 * SYN-RECEIVED -> ESTABLISHED
1858 * SYN-RECEIVED* -> FIN-WAIT-1
1860 if (tp
->t_flags
& TF_NEEDFIN
) {
1861 tp
->t_state
= TCPS_FIN_WAIT_1
;
1862 tp
->t_flags
&= ~TF_NEEDFIN
;
1864 tp
->t_state
= TCPS_ESTABLISHED
;
1865 tp
->t_timer
[TCPT_KEEP
] = tcp_keepidle
;
1868 * If segment contains data or ACK, will call tcp_reass()
1869 * later; if not, do so now to pass queued data to user.
1871 if (tilen
== 0 && (thflags
& TH_FIN
) == 0)
1872 (void) tcp_reass(tp
, (struct tcphdr
*)0, 0,
1873 (struct mbuf
*)0, isipv6
);
1874 tp
->snd_wl1
= th
->th_seq
- 1;
1878 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1879 * ACKs. If the ack is in the range
1880 * tp->snd_una < th->th_ack <= tp->snd_max
1881 * then advance tp->snd_una to th->th_ack and drop
1882 * data from the retransmission queue. If this ACK reflects
1883 * more up to date window information we update our window information.
1885 case TCPS_ESTABLISHED
:
1886 case TCPS_FIN_WAIT_1
:
1887 case TCPS_FIN_WAIT_2
:
1888 case TCPS_CLOSE_WAIT
:
1891 case TCPS_TIME_WAIT
:
1893 if (SEQ_LEQ(th
->th_ack
, tp
->snd_una
)) {
1894 if (tilen
== 0 && tiwin
== tp
->snd_wnd
) {
1895 tcpstat
.tcps_rcvdupack
++;
1897 * If we have outstanding data (other than
1898 * a window probe), this is a completely
1899 * duplicate ack (ie, window info didn't
1900 * change), the ack is the biggest we've
1901 * seen and we've seen exactly our rexmt
1902 * threshhold of them, assume a packet
1903 * has been dropped and retransmit it.
1904 * Kludge snd_nxt & the congestion
1905 * window so we send only this one
1908 * We know we're losing at the current
1909 * window size so do congestion avoidance
1910 * (set ssthresh to half the current window
1911 * and pull our congestion window back to
1912 * the new ssthresh).
1914 * Dup acks mean that packets have left the
1915 * network (they're now cached at the receiver)
1916 * so bump cwnd by the amount in the receiver
1917 * to keep a constant cwnd packets in the
1920 if (tp
->t_timer
[TCPT_REXMT
] == 0 ||
1921 th
->th_ack
!= tp
->snd_una
)
1923 else if (++tp
->t_dupacks
== tcprexmtthresh
) {
1924 tcp_seq onxt
= tp
->snd_nxt
;
1926 min(tp
->snd_wnd
, tp
->snd_cwnd
) / 2 /
1931 tp
->snd_ssthresh
= win
* tp
->t_maxseg
;
1932 tp
->t_timer
[TCPT_REXMT
] = 0;
1934 tp
->snd_nxt
= th
->th_ack
;
1935 tp
->snd_cwnd
= tp
->t_maxseg
;
1936 (void) tcp_output(tp
);
1937 tp
->snd_cwnd
= tp
->snd_ssthresh
+
1938 tp
->t_maxseg
* tp
->t_dupacks
;
1939 if (SEQ_GT(onxt
, tp
->snd_nxt
))
1942 } else if (tp
->t_dupacks
> tcprexmtthresh
) {
1943 tp
->snd_cwnd
+= tp
->t_maxseg
;
1944 (void) tcp_output(tp
);
1952 * If the congestion window was inflated to account
1953 * for the other side's cached packets, retract it.
1955 if (tp
->t_dupacks
>= tcprexmtthresh
&&
1956 tp
->snd_cwnd
> tp
->snd_ssthresh
)
1957 tp
->snd_cwnd
= tp
->snd_ssthresh
;
1959 if (SEQ_GT(th
->th_ack
, tp
->snd_max
)) {
1960 tcpstat
.tcps_rcvacktoomuch
++;
1964 * If we reach this point, ACK is not a duplicate,
1965 * i.e., it ACKs something we sent.
1967 if (tp
->t_flags
& TF_NEEDSYN
) {
1969 * T/TCP: Connection was half-synchronized, and our
1970 * SYN has been ACK'd (so connection is now fully
1971 * synchronized). Go to non-starred state,
1972 * increment snd_una for ACK of SYN, and check if
1973 * we can do window scaling.
1975 tp
->t_flags
&= ~TF_NEEDSYN
;
1977 /* Do window scaling? */
1978 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
1979 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
1980 tp
->snd_scale
= tp
->requested_s_scale
;
1981 tp
->rcv_scale
= tp
->request_r_scale
;
1986 acked
= th
->th_ack
- tp
->snd_una
;
1987 tcpstat
.tcps_rcvackpack
++;
1988 tcpstat
.tcps_rcvackbyte
+= acked
;
1991 * If we have a timestamp reply, update smoothed
1992 * round trip time. If no timestamp is present but
1993 * transmit timer is running and timed sequence
1994 * number was acked, update smoothed round trip time.
1995 * Since we now have an rtt measurement, cancel the
1996 * timer backoff (cf., Phil Karn's retransmit alg.).
1997 * Recompute the initial retransmit timer.
1999 if (to
.to_flag
& TOF_TS
)
2000 tcp_xmit_timer(tp
, tcp_now
- to
.to_tsecr
+ 1);
2001 else if (tp
->t_rtt
&& SEQ_GT(th
->th_ack
, tp
->t_rtseq
))
2002 tcp_xmit_timer(tp
,tp
->t_rtt
);
2005 * If all outstanding data is acked, stop retransmit
2006 * timer and remember to restart (more output or persist).
2007 * If there is more data to be acked, restart retransmit
2008 * timer, using current (possibly backed-off) value.
2010 if (th
->th_ack
== tp
->snd_max
) {
2011 tp
->t_timer
[TCPT_REXMT
] = 0;
2013 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
2014 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
2017 * If no data (only SYN) was ACK'd,
2018 * skip rest of ACK processing.
2024 * When new data is acked, open the congestion window.
2025 * If the window gives us less than ssthresh packets
2026 * in flight, open exponentially (maxseg per packet).
2027 * Otherwise open linearly: maxseg per window
2028 * (maxseg^2 / cwnd per packet).
2031 register u_int cw
= tp
->snd_cwnd
;
2032 register u_int incr
= tp
->t_maxseg
;
2034 if (cw
> tp
->snd_ssthresh
)
2035 incr
= incr
* incr
/ cw
;
2036 tp
->snd_cwnd
= min(cw
+ incr
, TCP_MAXWIN
<<tp
->snd_scale
);
2038 if (acked
> so
->so_snd
.sb_cc
) {
2039 tp
->snd_wnd
-= so
->so_snd
.sb_cc
;
2040 sbdrop(&so
->so_snd
, (int)so
->so_snd
.sb_cc
);
2043 sbdrop(&so
->so_snd
, acked
);
2044 tp
->snd_wnd
-= acked
;
2048 tp
->snd_una
= th
->th_ack
;
2049 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
2050 tp
->snd_nxt
= tp
->snd_una
;
2052 switch (tp
->t_state
) {
2055 * In FIN_WAIT_1 STATE in addition to the processing
2056 * for the ESTABLISHED state if our FIN is now acknowledged
2057 * then enter FIN_WAIT_2.
2059 case TCPS_FIN_WAIT_1
:
2060 if (ourfinisacked
) {
2062 * If we can't receive any more
2063 * data, then closing user can proceed.
2064 * Starting the timer is contrary to the
2065 * specification, but if we don't get a FIN
2066 * we'll hang forever.
2068 if (so
->so_state
& SS_CANTRCVMORE
) {
2069 soisdisconnected(so
);
2070 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;
2072 add_to_time_wait(tp
);
2073 current_active_connections
--;
2074 tp
->t_state
= TCPS_FIN_WAIT_2
;
2079 * In CLOSING STATE in addition to the processing for
2080 * the ESTABLISHED state if the ACK acknowledges our FIN
2081 * then enter the TIME-WAIT state, otherwise ignore
2085 if (ourfinisacked
) {
2086 tp
->t_state
= TCPS_TIME_WAIT
;
2087 tcp_canceltimers(tp
);
2088 /* Shorten TIME_WAIT [RFC-1644, p.28] */
2089 if (tp
->cc_recv
!= 0 &&
2090 tp
->t_duration
< TCPTV_MSL
)
2091 tp
->t_timer
[TCPT_2MSL
] =
2092 tp
->t_rxtcur
* TCPTV_TWTRUNC
;
2094 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
2095 add_to_time_wait(tp
);
2096 current_active_connections
--;
2097 soisdisconnected(so
);
2102 * In LAST_ACK, we may still be waiting for data to drain
2103 * and/or to be acked, as well as for the ack of our FIN.
2104 * If our FIN is now acknowledged, delete the TCB,
2105 * enter the closed state and return.
2108 if (ourfinisacked
) {
2115 * In TIME_WAIT state the only thing that should arrive
2116 * is a retransmission of the remote FIN. Acknowledge
2117 * it and restart the finack timer.
2119 case TCPS_TIME_WAIT
:
2120 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
2121 add_to_time_wait(tp
);
2128 * Update window information.
2129 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2131 if ((thflags
& TH_ACK
) &&
2132 (SEQ_LT(tp
->snd_wl1
, th
->th_seq
) ||
2133 (tp
->snd_wl1
== th
->th_seq
&& (SEQ_LT(tp
->snd_wl2
, th
->th_ack
) ||
2134 (tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
))))) {
2135 /* keep track of pure window updates */
2137 tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
)
2138 tcpstat
.tcps_rcvwinupd
++;
2139 tp
->snd_wnd
= tiwin
;
2140 tp
->snd_wl1
= th
->th_seq
;
2141 tp
->snd_wl2
= th
->th_ack
;
2142 if (tp
->snd_wnd
> tp
->max_sndwnd
)
2143 tp
->max_sndwnd
= tp
->snd_wnd
;
2148 * Process segments with URG.
2150 if ((thflags
& TH_URG
) && th
->th_urp
&&
2151 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
2153 * This is a kludge, but if we receive and accept
2154 * random urgent pointers, we'll crash in
2155 * soreceive. It's hard to imagine someone
2156 * actually wanting to send this much urgent data.
2158 if (th
->th_urp
+ so
->so_rcv
.sb_cc
> sb_max
) {
2159 th
->th_urp
= 0; /* XXX */
2160 thflags
&= ~TH_URG
; /* XXX */
2161 goto dodata
; /* XXX */
2164 * If this segment advances the known urgent pointer,
2165 * then mark the data stream. This should not happen
2166 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2167 * a FIN has been received from the remote side.
2168 * In these states we ignore the URG.
2170 * According to RFC961 (Assigned Protocols),
2171 * the urgent pointer points to the last octet
2172 * of urgent data. We continue, however,
2173 * to consider it to indicate the first octet
2174 * of data past the urgent section as the original
2175 * spec states (in one of two places).
2177 if (SEQ_GT(th
->th_seq
+th
->th_urp
, tp
->rcv_up
)) {
2178 tp
->rcv_up
= th
->th_seq
+ th
->th_urp
;
2179 so
->so_oobmark
= so
->so_rcv
.sb_cc
+
2180 (tp
->rcv_up
- tp
->rcv_nxt
) - 1;
2181 if (so
->so_oobmark
== 0) {
2182 so
->so_state
|= SS_RCVATMARK
;
2183 postevent(so
, 0, EV_OOB
);
2186 tp
->t_oobflags
&= ~(TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
2189 * Remove out of band data so doesn't get presented to user.
2190 * This can happen independent of advancing the URG pointer,
2191 * but if two URG's are pending at once, some out-of-band
2192 * data may creep in... ick.
2194 if (th
->th_urp
<= (u_long
)tilen
2196 && (so
->so_options
& SO_OOBINLINE
) == 0
2199 tcp_pulloutofband(so
, th
, m
);
2202 * If no out of band data is expected,
2203 * pull receive urgent pointer along
2204 * with the receive window.
2206 if (SEQ_GT(tp
->rcv_nxt
, tp
->rcv_up
))
2207 tp
->rcv_up
= tp
->rcv_nxt
;
2211 * Process the segment text, merging it into the TCP sequencing queue,
2212 * and arranging for acknowledgment of receipt if necessary.
2213 * This process logically involves adjusting tp->rcv_wnd as data
2214 * is presented to the user (this happens in tcp_usrreq.c,
2215 * case PRU_RCVD). If a FIN has already been received on this
2216 * connection then we just ignore the text.
2218 if ((tilen
|| (thflags
&TH_FIN
)) &&
2219 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
2220 TCP_REASS(tp
, th
, tilen
, m
, so
, thflags
, isipv6
, need_sorwakeup
);
2222 if (tp
->t_flags
& TF_DELACK
)
2224 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
2225 (((thtoti(th
)->ti_src
.s_addr
& 0xffff) << 16) | (thtoti(th
)->ti_dst
.s_addr
& 0xffff)),
2226 th
->th_seq
, th
->th_ack
, th
->th_win
);
2229 * Note the amount of data that peer has sent into
2230 * our window, in order to estimate the sender's
2233 len
= so
->so_rcv
.sb_hiwat
- (tp
->rcv_adv
- tp
->rcv_nxt
);
2240 * If FIN is received ACK the FIN and let the user know
2241 * that the connection is closing.
2243 if (thflags
& TH_FIN
) {
2244 if (TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
2246 postevent(so
, 0, EV_FIN
);
2248 * If connection is half-synchronized
2249 * (ie NEEDSYN flag on) then delay ACK,
2250 * so it may be piggybacked when SYN is sent.
2251 * Otherwise, since we received a FIN then no
2252 * more input can be expected, send ACK now.
2254 if (tcp_delack_enabled
&& (tp
->t_flags
& TF_NEEDSYN
)) {
2255 if (last_active_conn_count
> DELACK_BITMASK_THRESH
)
2256 TCP_DELACK_BITSET(tp
->t_inpcb
->hash_element
);
2258 tp
->t_flags
|= TF_DELACK
;
2261 tp
->t_flags
|= TF_ACKNOW
;
2264 switch (tp
->t_state
) {
2267 * In SYN_RECEIVED and ESTABLISHED STATES
2268 * enter the CLOSE_WAIT state.
2270 case TCPS_SYN_RECEIVED
:
2271 case TCPS_ESTABLISHED
:
2272 tp
->t_state
= TCPS_CLOSE_WAIT
;
2276 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2277 * enter the CLOSING state.
2279 case TCPS_FIN_WAIT_1
:
2280 tp
->t_state
= TCPS_CLOSING
;
2284 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2285 * starting the time-wait timer, turning off the other
2288 case TCPS_FIN_WAIT_2
:
2289 tp
->t_state
= TCPS_TIME_WAIT
;
2290 tcp_canceltimers(tp
);
2291 /* Shorten TIME_WAIT [RFC-1644, p.28] */
2292 if (tp
->cc_recv
!= 0 &&
2293 tp
->t_duration
< TCPTV_MSL
) {
2294 tp
->t_timer
[TCPT_2MSL
] =
2295 tp
->t_rxtcur
* TCPTV_TWTRUNC
;
2296 /* For transaction client, force ACK now. */
2297 tp
->t_flags
|= TF_ACKNOW
;
2300 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
2302 add_to_time_wait(tp
);
2303 soisdisconnected(so
);
2307 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2309 case TCPS_TIME_WAIT
:
2310 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
2311 add_to_time_wait(tp
);
2316 if (so
->so_options
& SO_DEBUG
) {
2319 tcp_saveip
._tcp_si6
.ip6_plen
= tilen
;
2321 tcp_saveip
._tcp_si4
.ip_len
= tilen
;
2323 tcp_saveip
.ip_len
= tilen
;
2326 tcp_trace(TA_INPUT
, ostate
, tp
, (void *)&tcp_saveip
,
2332 * Return any desired output.
2334 if (needoutput
|| (tp
->t_flags
& TF_ACKNOW
))
2335 (void) tcp_output(tp
);
2340 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2345 * Generate an ACK dropping incoming segment if it occupies
2346 * sequence space, where the ACK reflects our state.
2348 * We can now skip the test for the RST flag since all
2349 * paths to this code happen after packets containing
2350 * RST have been dropped.
2352 * In the SYN-RECEIVED state, don't send an ACK unless the
2353 * segment we received passes the SYN-RECEIVED ACK test.
2354 * If it fails send a RST. This breaks the loop in the
2355 * "LAND" DoS attack, and also prevents an ACK storm
2356 * between two listening ports that have been sent forged
2357 * SYN segments, each with the source address of the other.
2359 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& (thflags
& TH_ACK
) &&
2360 (SEQ_GT(tp
->snd_una
, th
->th_ack
) ||
2361 SEQ_GT(th
->th_ack
, tp
->snd_max
)) )
2364 if (so
->so_options
& SO_DEBUG
) {
2367 tcp_saveip
._tcp_si6
.ip6_plen
= tilen
;
2369 tcp_saveip
._tcp_si4
.ip_len
= tilen
;
2371 tcp_saveip
.ip_len
= tilen
;
2373 tcp_trace(TA_DROP
, ostate
, tp
, (void *)&tcp_saveip
,
2378 tp
->t_flags
|= TF_ACKNOW
;
2379 (void) tcp_output(tp
);
2384 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2389 * Generate a RST, dropping incoming segment.
2390 * Make ACK acceptable to originator of segment.
2391 * Don't bother to respond if destination was broadcast/multicast.
2393 if ((thflags
& TH_RST
) || m
->m_flags
& (M_BCAST
|M_MCAST
))
2397 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
))
2398 goto drop
; /* anycast check is done at the top */
2401 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)))
2404 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
)) {
2408 tcp_saveip
._tcp_si6
= *ip6
;
2410 tcp_saveip
._tcp_si4
= *ip
;
2417 tcp_saveip
._tcp_si6
.ip6_plen
= tilen
;
2419 tcp_saveip
._tcp_si4
.ip_len
= tilen
;
2421 tcp_saveip
.ip_len
= tilen
;
2423 tcp_trace(TA_DROP
, ostate
, tp
, (void *)&tcp_saveip
,
2427 if (thflags
& TH_ACK
)
2429 tcp_respond(tp
, isipv6
? (void *)ip6
: (void *)ip
, th
, m
,
2430 (tcp_seq
)0, th
->th_ack
, TH_RST
, isipv6
);
2432 tcp_respond(tp
, (void *)ip
, th
, m
,
2433 (tcp_seq
)0, th
->th_ack
, TH_RST
, isipv6
);
2436 if (thflags
& TH_SYN
)
2439 tcp_respond(tp
, isipv6
? (void *)ip6
: (void *)ip
, th
, m
,
2440 th
->th_seq
+tilen
, (tcp_seq
)0, TH_RST
|TH_ACK
,
2443 tcp_respond(tp
, (void *)ip
, th
, m
,
2444 th
->th_seq
+tilen
, (tcp_seq
)0, TH_RST
|TH_ACK
,
2448 /* destroy temporarily created socket */
2455 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2460 * Drop space held by incoming segment and return.
2463 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
)) {
2467 tcp_saveip
._tcp_si6
= *ip6
;
2469 tcp_saveip
._tcp_si4
= *ip
;
2476 tcp_saveip
._tcp_si6
.ip6_plen
= tilen
;
2478 tcp_saveip
._tcp_si4
.ip_len
= tilen
;
2480 tcp_saveip
.ip_len
= tilen
;
2482 tcp_trace(TA_DROP
, ostate
, tp
, (void *)&tcp_saveip
,
2491 /* destroy temporarily created socket */
2494 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2499 tcp_dooptions(tp
, cp
, cnt
, th
, to
)
2509 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
2511 if (opt
== TCPOPT_EOL
)
2513 if (opt
== TCPOPT_NOP
)
2526 if (optlen
!= TCPOLEN_MAXSEG
)
2528 if (!(th
->th_flags
& TH_SYN
))
2530 bcopy((char *) cp
+ 2, (char *) &mss
, sizeof(mss
));
2531 to
->to_maxseg
= ntohs(mss
);
2535 if (optlen
!= TCPOLEN_WINDOW
)
2537 if (!(th
->th_flags
& TH_SYN
))
2539 tp
->t_flags
|= TF_RCVD_SCALE
;
2540 tp
->requested_s_scale
= min(cp
[2], TCP_MAX_WINSHIFT
);
2543 case TCPOPT_TIMESTAMP
:
2544 if (optlen
!= TCPOLEN_TIMESTAMP
)
2546 to
->to_flag
|= TOF_TS
;
2547 bcopy((char *)cp
+ 2,
2548 (char *)&to
->to_tsval
, sizeof(to
->to_tsval
));
2549 NTOHL(to
->to_tsval
);
2550 bcopy((char *)cp
+ 6,
2551 (char *)&to
->to_tsecr
, sizeof(to
->to_tsecr
));
2552 NTOHL(to
->to_tsecr
);
2555 * A timestamp received in a SYN makes
2556 * it ok to send timestamp requests and replies.
2558 if (th
->th_flags
& TH_SYN
) {
2559 tp
->t_flags
|= TF_RCVD_TSTMP
;
2560 tp
->ts_recent
= to
->to_tsval
;
2561 tp
->ts_recent_age
= tcp_now
;
2565 if (optlen
!= TCPOLEN_CC
)
2567 to
->to_flag
|= TOF_CC
;
2568 bcopy((char *)cp
+ 2,
2569 (char *)&to
->to_cc
, sizeof(to
->to_cc
));
2572 * A CC or CC.new option received in a SYN makes
2573 * it ok to send CC in subsequent segments.
2575 if (th
->th_flags
& TH_SYN
)
2576 tp
->t_flags
|= TF_RCVD_CC
;
2579 if (optlen
!= TCPOLEN_CC
)
2581 if (!(th
->th_flags
& TH_SYN
))
2583 to
->to_flag
|= TOF_CCNEW
;
2584 bcopy((char *)cp
+ 2,
2585 (char *)&to
->to_cc
, sizeof(to
->to_cc
));
2588 * A CC or CC.new option received in a SYN makes
2589 * it ok to send CC in subsequent segments.
2591 tp
->t_flags
|= TF_RCVD_CC
;
2594 if (optlen
!= TCPOLEN_CC
)
2596 if (!(th
->th_flags
& TH_SYN
))
2598 to
->to_flag
|= TOF_CCECHO
;
2599 bcopy((char *)cp
+ 2,
2600 (char *)&to
->to_ccecho
, sizeof(to
->to_ccecho
));
2601 NTOHL(to
->to_ccecho
);
2608 * Pull out of band byte out of a segment so
2609 * it doesn't appear in the user's data queue.
2610 * It is still reflected in the segment length for
2611 * sequencing purposes.
2614 tcp_pulloutofband(so
, th
, m
)
2617 register struct mbuf
*m
;
2619 int cnt
= th
->th_urp
- 1;
2622 if (m
->m_len
> cnt
) {
2623 char *cp
= mtod(m
, caddr_t
) + cnt
;
2624 struct tcpcb
*tp
= sototcpcb(so
);
2627 tp
->t_oobflags
|= TCPOOB_HAVEDATA
;
2628 bcopy(cp
+1, cp
, (unsigned)(m
->m_len
- cnt
- 1));
2637 panic("tcp_pulloutofband");
2641 * Collect new round-trip time estimate
2642 * and update averages and current timeout.
2645 tcp_xmit_timer(tp
, rtt
)
2646 register struct tcpcb
*tp
;
2651 tcpstat
.tcps_rttupdated
++;
2653 if (tp
->t_srtt
!= 0) {
2655 * srtt is stored as fixed point with 5 bits after the
2656 * binary point (i.e., scaled by 8). The following magic
2657 * is equivalent to the smoothing algorithm in rfc793 with
2658 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2659 * point). Adjust rtt to origin 0.
2661 delta
= ((rtt
- 1) << TCP_DELTA_SHIFT
)
2662 - (tp
->t_srtt
>> (TCP_RTT_SHIFT
- TCP_DELTA_SHIFT
));
2664 if ((tp
->t_srtt
+= delta
) <= 0)
2668 * We accumulate a smoothed rtt variance (actually, a
2669 * smoothed mean difference), then set the retransmit
2670 * timer to smoothed rtt + 4 times the smoothed variance.
2671 * rttvar is stored as fixed point with 4 bits after the
2672 * binary point (scaled by 16). The following is
2673 * equivalent to rfc793 smoothing with an alpha of .75
2674 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
2675 * rfc793's wired-in beta.
2679 delta
-= tp
->t_rttvar
>> (TCP_RTTVAR_SHIFT
- TCP_DELTA_SHIFT
);
2680 if ((tp
->t_rttvar
+= delta
) <= 0)
2684 * No rtt measurement yet - use the unsmoothed rtt.
2685 * Set the variance to half the rtt (so our first
2686 * retransmit happens at 3*rtt).
2688 tp
->t_srtt
= rtt
<< TCP_RTT_SHIFT
;
2689 tp
->t_rttvar
= rtt
<< (TCP_RTTVAR_SHIFT
- 1);
2695 * the retransmit should happen at rtt + 4 * rttvar.
2696 * Because of the way we do the smoothing, srtt and rttvar
2697 * will each average +1/2 tick of bias. When we compute
2698 * the retransmit timer, we want 1/2 tick of rounding and
2699 * 1 extra tick because of +-1/2 tick uncertainty in the
2700 * firing of the timer. The bias will give us exactly the
2701 * 1.5 tick we need. But, because the bias is
2702 * statistical, we have to test that we don't drop below
2703 * the minimum feasible timer (which is 2 ticks).
2705 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
2706 max(tp
->t_rttmin
, rtt
+ 2), TCPTV_REXMTMAX
);
2709 * We received an ack for a packet that wasn't retransmitted;
2710 * it is probably safe to discard any error indications we've
2711 * received recently. This isn't quite right, but close enough
2712 * for now (a route might have failed after we sent a segment,
2713 * and the return path might not be symmetrical).
2715 tp
->t_softerror
= 0;
2719 * Determine a reasonable value for maxseg size.
2720 * If the route is known, check route for mtu.
2721 * If none, use an mss that can be handled on the outgoing
2722 * interface without forcing IP to fragment; if bigger than
2723 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2724 * to utilize large mbufs. If no route is found, route has no mtu,
2725 * or the destination isn't local, use a default, hopefully conservative
2726 * size (usually 512 or the default IP max size, but no more than the mtu
2727 * of the interface), as we can't discover anything about intervening
2728 * gateways or networks. We also initialize the congestion/slow start
2729 * window to be a single segment if the destination isn't local.
2730 * While looking at the routing entry, we also initialize other path-dependent
2731 * parameters from pre-set or cached values in the routing entry.
2733 * Also take into account the space needed for options that we
2734 * send regularly. Make maxseg shorter by that amount to assure
2735 * that we can send maxseg amount of data even when the options
2736 * are present. Store the upper limit of the length of options plus
2739 * NOTE that this routine is only called when we process an incoming
2740 * segment, for outgoing segments only tcp_mssopt is called.
2742 * In case of T/TCP, we call this routine during implicit connection
2743 * setup as well (offer = -1), to initialize maxseg from the cached
2747 tcp_mss(tp
, offer
, isipv6
)
2754 register struct rtentry
*rt
;
2756 register int rtt
, mss
;
2760 struct rmxp_tao
*taop
;
2761 int origoffer
= offer
;
2763 int lgminh
= isipv6
? sizeof (struct tcpip6hdr
) :
2764 sizeof (struct tcpiphdr
);
2766 #define lgminh (sizeof (struct tcpiphdr))
2772 rt
= tcp_rtlookup6(inp
);
2775 rt
= tcp_rtlookup(inp
);
2777 tp
->t_maxopd
= tp
->t_maxseg
=
2779 isipv6
? tcp_v6mssdflt
:
2785 so
= inp
->inp_socket
;
2787 taop
= rmx_taop(rt
->rt_rmx
);
2789 * Offer == -1 means that we didn't receive SYN yet,
2790 * use cached value in that case;
2793 offer
= taop
->tao_mssopt
;
2795 * Offer == 0 means that there was no MSS on the SYN segment,
2796 * in this case we use tcp_mssdflt.
2801 isipv6
? tcp_v6mssdflt
:
2806 * Sanity check: make sure that maxopd will be large
2807 * enough to allow some data on segments even is the
2808 * all the option space is used (40bytes). Otherwise
2809 * funny things may happen in tcp_output.
2811 offer
= max(offer
, 64);
2812 taop
->tao_mssopt
= offer
;
2815 * While we're here, check if there's an initial rtt
2816 * or rttvar. Convert from the route-table units
2817 * to scaled multiples of the slow timeout timer.
2819 if (tp
->t_srtt
== 0 && (rtt
= rt
->rt_rmx
.rmx_rtt
)) {
2821 * XXX the lock bit for RTT indicates that the value
2822 * is also a minimum value; this is subject to time.
2824 if (rt
->rt_rmx
.rmx_locks
& RTV_RTT
)
2825 tp
->t_rttmin
= rtt
/ (RTM_RTTUNIT
/ PR_SLOWHZ
);
2826 tp
->t_srtt
= rtt
/ (RTM_RTTUNIT
/ (PR_SLOWHZ
* TCP_RTT_SCALE
));
2827 tcpstat
.tcps_usedrtt
++;
2828 if (rt
->rt_rmx
.rmx_rttvar
) {
2829 tp
->t_rttvar
= rt
->rt_rmx
.rmx_rttvar
/
2830 (RTM_RTTUNIT
/ (PR_SLOWHZ
* TCP_RTTVAR_SCALE
));
2831 tcpstat
.tcps_usedrttvar
++;
2833 /* default variation is +- 1 rtt */
2835 tp
->t_srtt
* TCP_RTTVAR_SCALE
/ TCP_RTT_SCALE
;
2837 TCPT_RANGESET(tp
->t_rxtcur
,
2838 ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1,
2839 tp
->t_rttmin
, TCPTV_REXMTMAX
);
2842 * if there's an mtu associated with the route, use it
2843 * else, use the link mtu.
2845 if (rt
->rt_rmx
.rmx_mtu
)
2846 mss
= rt
->rt_rmx
.rmx_mtu
- lgminh
;
2850 isipv6
? nd_ifinfo
[rt
->rt_ifp
->if_index
].linkmtu
:
2852 ifp
->if_mtu
- lgminh
;
2854 if (rt
->rt_rmx
.rmx_mtu
== 0) {
2857 if (!in6_localaddr(&inp
->in6p_faddr
))
2858 mss
= min(mss
, tcp_v6mssdflt
);
2861 if (!in_localaddr(inp
->inp_faddr
))
2862 mss
= min(mss
, tcp_mssdflt
);
2864 mss
= min(mss
, offer
);
2866 * maxopd stores the maximum length of data AND options
2867 * in a segment; maxseg is the amount of data in a normal
2868 * segment. We need to store this value (maxopd) apart
2869 * from maxseg, because now every segment carries options
2870 * and thus we normally have somewhat less data in segments.
2875 * In case of T/TCP, origoffer==-1 indicates, that no segments
2876 * were received yet. In this case we just guess, otherwise
2877 * we do the same as before T/TCP.
2879 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
2881 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
))
2882 mss
-= TCPOLEN_TSTAMP_APPA
;
2883 if ((tp
->t_flags
& (TF_REQ_CC
|TF_NOOPT
)) == TF_REQ_CC
&&
2885 (tp
->t_flags
& TF_RCVD_CC
) == TF_RCVD_CC
))
2886 mss
-= TCPOLEN_CC_APPA
;
2888 #if (MCLBYTES & (MCLBYTES - 1)) == 0
2890 mss
&= ~(MCLBYTES
-1);
2893 mss
= mss
/ MCLBYTES
* MCLBYTES
;
2896 * If there's a pipesize, change the socket buffer
2897 * to that size. Make the socket buffers an integral
2898 * number of mss units; if the mss is larger than
2899 * the socket buffer, decrease the mss.
2902 if ((bufsize
= rt
->rt_rmx
.rmx_sendpipe
) == 0)
2904 bufsize
= so
->so_snd
.sb_hiwat
;
2908 bufsize
= roundup(bufsize
, mss
);
2909 if (bufsize
> sb_max
)
2911 (void)sbreserve(&so
->so_snd
, bufsize
);
2916 if ((bufsize
= rt
->rt_rmx
.rmx_recvpipe
) == 0)
2918 bufsize
= so
->so_rcv
.sb_hiwat
;
2919 if (bufsize
> mss
) {
2920 bufsize
= roundup(bufsize
, mss
);
2921 if (bufsize
> sb_max
)
2923 (void)sbreserve(&so
->so_rcv
, bufsize
);
2926 * Don't force slow-start on local network.
2930 if (!in6_localaddr(&inp
->in6p_faddr
))
2934 if (!in_localaddr(inp
->inp_faddr
))
2937 if (rt
->rt_rmx
.rmx_ssthresh
) {
2939 * There's some sort of gateway or interface
2940 * buffer limit on the path. Use this to set
2941 * the slow start threshhold, but set the
2942 * threshold to no less than 2*mss.
2944 tp
->snd_ssthresh
= max(2 * mss
, rt
->rt_rmx
.rmx_ssthresh
);
2945 tcpstat
.tcps_usedssthresh
++;
2950 * Determine the MSS option to send on an outgoing SYN.
2953 tcp_mssopt(tp
, isipv6
)
2962 int lgminh
= isipv6
? sizeof (struct tcpip6hdr
) :
2963 sizeof (struct tcpiphdr
);
2965 #define lgminh (sizeof (struct tcpiphdr))
2970 rt
= tcp_rtlookup6(tp
->t_inpcb
);
2973 rt
= tcp_rtlookup(tp
->t_inpcb
);
2977 isipv6
? tcp_v6mssdflt
:
2981 mss
= rt
->rt_ifp
->if_mtu
- lgminh
;