xref: /titanic_51/usr/src/uts/common/inet/tcp/tcp_input.c (revision 1f183ba0b0be3e10202501aa3740753df6512804)
1721fffe3SKacheong Poon /*
2721fffe3SKacheong Poon  * CDDL HEADER START
3721fffe3SKacheong Poon  *
4721fffe3SKacheong Poon  * The contents of this file are subject to the terms of the
5721fffe3SKacheong Poon  * Common Development and Distribution License (the "License").
6721fffe3SKacheong Poon  * You may not use this file except in compliance with the License.
7721fffe3SKacheong Poon  *
8721fffe3SKacheong Poon  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9721fffe3SKacheong Poon  * or http://www.opensolaris.org/os/licensing.
10721fffe3SKacheong Poon  * See the License for the specific language governing permissions
11721fffe3SKacheong Poon  * and limitations under the License.
12721fffe3SKacheong Poon  *
13721fffe3SKacheong Poon  * When distributing Covered Code, include this CDDL HEADER in each
14721fffe3SKacheong Poon  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15721fffe3SKacheong Poon  * If applicable, add the following below this CDDL HEADER, with the
16721fffe3SKacheong Poon  * fields enclosed by brackets "[]" replaced with your own identifying
17721fffe3SKacheong Poon  * information: Portions Copyright [yyyy] [name of copyright owner]
18721fffe3SKacheong Poon  *
19721fffe3SKacheong Poon  * CDDL HEADER END
20721fffe3SKacheong Poon  */
21721fffe3SKacheong Poon 
22721fffe3SKacheong Poon /*
2366cd0f60SKacheong Poon  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24f2297e92SDan McDonald  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
258b21eaf3SRobert Mustacchi  * Copyright (c) 2011 Joyent, Inc. All rights reserved.
26633fc3a6SSebastien Roy  * Copyright (c) 2014 by Delphix. All rights reserved.
27721fffe3SKacheong Poon  */
28721fffe3SKacheong Poon 
29721fffe3SKacheong Poon /* This file contains all TCP input processing functions. */
30721fffe3SKacheong Poon 
31721fffe3SKacheong Poon #include <sys/types.h>
32721fffe3SKacheong Poon #include <sys/stream.h>
33721fffe3SKacheong Poon #include <sys/strsun.h>
34721fffe3SKacheong Poon #include <sys/strsubr.h>
35721fffe3SKacheong Poon #include <sys/stropts.h>
36721fffe3SKacheong Poon #include <sys/strlog.h>
37721fffe3SKacheong Poon #define	_SUN_TPI_VERSION 2
38721fffe3SKacheong Poon #include <sys/tihdr.h>
39721fffe3SKacheong Poon #include <sys/suntpi.h>
40721fffe3SKacheong Poon #include <sys/xti_inet.h>
41721fffe3SKacheong Poon #include <sys/squeue_impl.h>
42721fffe3SKacheong Poon #include <sys/squeue.h>
43721fffe3SKacheong Poon #include <sys/tsol/tnet.h>
44721fffe3SKacheong Poon 
45721fffe3SKacheong Poon #include <inet/common.h>
46721fffe3SKacheong Poon #include <inet/ip.h>
47721fffe3SKacheong Poon #include <inet/tcp.h>
48721fffe3SKacheong Poon #include <inet/tcp_impl.h>
49721fffe3SKacheong Poon #include <inet/tcp_cluster.h>
50721fffe3SKacheong Poon #include <inet/proto_set.h>
51721fffe3SKacheong Poon #include <inet/ipsec_impl.h>
52721fffe3SKacheong Poon 
53721fffe3SKacheong Poon /*
54*1f183ba0SLauri Tirkkonen  * RFC7323-recommended phrasing of TSTAMP option, for easier parsing
55721fffe3SKacheong Poon  */
56721fffe3SKacheong Poon 
57721fffe3SKacheong Poon #ifdef _BIG_ENDIAN
58721fffe3SKacheong Poon #define	TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
59721fffe3SKacheong Poon 	(TCPOPT_TSTAMP << 8) | 10)
60721fffe3SKacheong Poon #else
61721fffe3SKacheong Poon #define	TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \
62721fffe3SKacheong Poon 	(TCPOPT_NOP << 8) | TCPOPT_NOP)
63721fffe3SKacheong Poon #endif
64721fffe3SKacheong Poon 
65721fffe3SKacheong Poon /*
66721fffe3SKacheong Poon  *  PAWS needs a timer for 24 days.  This is the number of ticks in 24 days
67721fffe3SKacheong Poon  */
68721fffe3SKacheong Poon #define	PAWS_TIMEOUT	((clock_t)(24*24*60*60*hz))
69721fffe3SKacheong Poon 
70721fffe3SKacheong Poon /*
71721fffe3SKacheong Poon  * Since tcp_listener is not cleared atomically with tcp_detached
72721fffe3SKacheong Poon  * being cleared we need this extra bit to tell a detached connection
73721fffe3SKacheong Poon  * apart from one that is in the process of being accepted.
74721fffe3SKacheong Poon  */
75721fffe3SKacheong Poon #define	TCP_IS_DETACHED_NONEAGER(tcp)	\
76721fffe3SKacheong Poon 	(TCP_IS_DETACHED(tcp) &&	\
77721fffe3SKacheong Poon 	    (!(tcp)->tcp_hard_binding))
78721fffe3SKacheong Poon 
79721fffe3SKacheong Poon /*
80721fffe3SKacheong Poon  * Steps to do when a tcp_t moves to TIME-WAIT state.
81721fffe3SKacheong Poon  *
82721fffe3SKacheong Poon  * This connection is done, we don't need to account for it.  Decrement
83721fffe3SKacheong Poon  * the listener connection counter if needed.
84721fffe3SKacheong Poon  *
85721fffe3SKacheong Poon  * Decrement the connection counter of the stack.  Note that this counter
86721fffe3SKacheong Poon  * is per CPU.  So the total number of connections in a stack is the sum of all
87721fffe3SKacheong Poon  * of them.  Since there is no lock for handling all of them exclusively, the
88721fffe3SKacheong Poon  * resulting sum is only an approximation.
89721fffe3SKacheong Poon  *
90721fffe3SKacheong Poon  * Unconditionally clear the exclusive binding bit so this TIME-WAIT
91721fffe3SKacheong Poon  * connection won't interfere with new ones.
92721fffe3SKacheong Poon  *
93721fffe3SKacheong Poon  * Start the TIME-WAIT timer.  If upper layer has not closed the connection,
94721fffe3SKacheong Poon  * the timer is handled within the context of this tcp_t.  When the timer
95721fffe3SKacheong Poon  * fires, tcp_clean_death() is called.  If upper layer closes the connection
96721fffe3SKacheong Poon  * during this period, tcp_time_wait_append() will be called to add this
97721fffe3SKacheong Poon  * tcp_t to the global TIME-WAIT list.  Note that this means that the
98721fffe3SKacheong Poon  * actual wait time in TIME-WAIT state will be longer than the
99721fffe3SKacheong Poon  * tcps_time_wait_interval since the period before upper layer closes the
100721fffe3SKacheong Poon  * connection is not accounted for when tcp_time_wait_append() is called.
101721fffe3SKacheong Poon  *
102721fffe3SKacheong Poon  * If uppser layer has closed the connection, call tcp_time_wait_append()
103721fffe3SKacheong Poon  * directly.
104721fffe3SKacheong Poon  *
105721fffe3SKacheong Poon  */
106721fffe3SKacheong Poon #define	SET_TIME_WAIT(tcps, tcp, connp)				\
107721fffe3SKacheong Poon {								\
108721fffe3SKacheong Poon 	(tcp)->tcp_state = TCPS_TIME_WAIT;			\
109721fffe3SKacheong Poon 	if ((tcp)->tcp_listen_cnt != NULL)			\
110721fffe3SKacheong Poon 		TCP_DECR_LISTEN_CNT(tcp);			\
111721fffe3SKacheong Poon 	atomic_dec_64(						\
112721fffe3SKacheong Poon 	    (uint64_t *)&(tcps)->tcps_sc[CPU->cpu_seqid]->tcp_sc_conn_cnt); \
113721fffe3SKacheong Poon 	(connp)->conn_exclbind = 0;				\
114721fffe3SKacheong Poon 	if (!TCP_IS_DETACHED(tcp)) {				\
115721fffe3SKacheong Poon 		TCP_TIMER_RESTART(tcp, (tcps)->tcps_time_wait_interval); \
116721fffe3SKacheong Poon 	} else {						\
117721fffe3SKacheong Poon 		tcp_time_wait_append(tcp);			\
118721fffe3SKacheong Poon 		TCP_DBGSTAT(tcps, tcp_rput_time_wait);		\
119721fffe3SKacheong Poon 	}							\
120721fffe3SKacheong Poon }
121721fffe3SKacheong Poon 
122721fffe3SKacheong Poon /*
123721fffe3SKacheong Poon  * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more
124721fffe3SKacheong Poon  * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent
125721fffe3SKacheong Poon  * data, TCP will not respond with an ACK.  RFC 793 requires that
126721fffe3SKacheong Poon  * TCP responds with an ACK for such a bogus ACK.  By not following
127721fffe3SKacheong Poon  * the RFC, we prevent TCP from getting into an ACK storm if somehow
128721fffe3SKacheong Poon  * an attacker successfully spoofs an acceptable segment to our
129721fffe3SKacheong Poon  * peer; or when our peer is "confused."
130721fffe3SKacheong Poon  */
131721fffe3SKacheong Poon static uint32_t tcp_drop_ack_unsent_cnt = 10;
132721fffe3SKacheong Poon 
133721fffe3SKacheong Poon /*
13432c3e7eeSKacheong Poon  * To protect TCP against attacker using a small window and requesting
13532c3e7eeSKacheong Poon  * large amount of data (DoS attack by conuming memory), TCP checks the
13632c3e7eeSKacheong Poon  * window advertised in the last ACK of the 3-way handshake.  TCP uses
13732c3e7eeSKacheong Poon  * the tcp_mss (the size of one packet) value for comparion.  The window
13832c3e7eeSKacheong Poon  * should be larger than tcp_mss.  But while a sane TCP should advertise
13932c3e7eeSKacheong Poon  * a receive window larger than or equal to 4*MSS to avoid stop and go
14032c3e7eeSKacheong Poon  * tarrfic, not all TCP stacks do that.  This is especially true when
14132c3e7eeSKacheong Poon  * tcp_mss is a big value.
14232c3e7eeSKacheong Poon  *
14332c3e7eeSKacheong Poon  * To work around this issue, an additional fixed value for comparison
14432c3e7eeSKacheong Poon  * is also used.  If the advertised window is smaller than both tcp_mss
14532c3e7eeSKacheong Poon  * and tcp_init_wnd_chk, the ACK is considered as invalid.  So for large
14632c3e7eeSKacheong Poon  * tcp_mss value (say, 8K), a window larger than tcp_init_wnd_chk but
14732c3e7eeSKacheong Poon  * smaller than 8K is considered to be OK.
148721fffe3SKacheong Poon  */
14932c3e7eeSKacheong Poon static uint32_t tcp_init_wnd_chk = 4096;
150721fffe3SKacheong Poon 
151721fffe3SKacheong Poon /* Process ICMP source quench message or not. */
152721fffe3SKacheong Poon static boolean_t tcp_icmp_source_quench = B_FALSE;
153721fffe3SKacheong Poon 
154721fffe3SKacheong Poon static boolean_t tcp_outbound_squeue_switch = B_FALSE;
155721fffe3SKacheong Poon 
156721fffe3SKacheong Poon static mblk_t	*tcp_conn_create_v4(conn_t *, conn_t *, mblk_t *,
157721fffe3SKacheong Poon 		    ip_recv_attr_t *);
158721fffe3SKacheong Poon static mblk_t	*tcp_conn_create_v6(conn_t *, conn_t *, mblk_t *,
159721fffe3SKacheong Poon 		    ip_recv_attr_t *);
160721fffe3SKacheong Poon static boolean_t	tcp_drop_q0(tcp_t *);
161721fffe3SKacheong Poon static void	tcp_icmp_error_ipv6(tcp_t *, mblk_t *, ip_recv_attr_t *);
162721fffe3SKacheong Poon static mblk_t	*tcp_input_add_ancillary(tcp_t *, mblk_t *, ip_pkt_t *,
163721fffe3SKacheong Poon 		    ip_recv_attr_t *);
164721fffe3SKacheong Poon static void	tcp_input_listener(void *, mblk_t *, void *, ip_recv_attr_t *);
165721fffe3SKacheong Poon static void	tcp_process_options(tcp_t *, tcpha_t *);
166721fffe3SKacheong Poon static mblk_t	*tcp_reass(tcp_t *, mblk_t *, uint32_t);
167721fffe3SKacheong Poon static void	tcp_reass_elim_overlap(tcp_t *, mblk_t *);
168721fffe3SKacheong Poon static void	tcp_rsrv_input(void *, mblk_t *, void *, ip_recv_attr_t *);
169721fffe3SKacheong Poon static void	tcp_set_rto(tcp_t *, time_t);
170721fffe3SKacheong Poon static void	tcp_setcred_data(mblk_t *, ip_recv_attr_t *);
171721fffe3SKacheong Poon 
172721fffe3SKacheong Poon /*
173721fffe3SKacheong Poon  * Set the MSS associated with a particular tcp based on its current value,
174721fffe3SKacheong Poon  * and a new one passed in. Observe minimums and maximums, and reset other
175721fffe3SKacheong Poon  * state variables that we want to view as multiples of MSS.
176721fffe3SKacheong Poon  *
177721fffe3SKacheong Poon  * The value of MSS could be either increased or descreased.
178721fffe3SKacheong Poon  */
179721fffe3SKacheong Poon void
180721fffe3SKacheong Poon tcp_mss_set(tcp_t *tcp, uint32_t mss)
181721fffe3SKacheong Poon {
182721fffe3SKacheong Poon 	uint32_t	mss_max;
183721fffe3SKacheong Poon 	tcp_stack_t	*tcps = tcp->tcp_tcps;
184721fffe3SKacheong Poon 	conn_t		*connp = tcp->tcp_connp;
185721fffe3SKacheong Poon 
186721fffe3SKacheong Poon 	if (connp->conn_ipversion == IPV4_VERSION)
187721fffe3SKacheong Poon 		mss_max = tcps->tcps_mss_max_ipv4;
188721fffe3SKacheong Poon 	else
189721fffe3SKacheong Poon 		mss_max = tcps->tcps_mss_max_ipv6;
190721fffe3SKacheong Poon 
191721fffe3SKacheong Poon 	if (mss < tcps->tcps_mss_min)
192721fffe3SKacheong Poon 		mss = tcps->tcps_mss_min;
193721fffe3SKacheong Poon 	if (mss > mss_max)
194721fffe3SKacheong Poon 		mss = mss_max;
195721fffe3SKacheong Poon 	/*
196721fffe3SKacheong Poon 	 * Unless naglim has been set by our client to
197721fffe3SKacheong Poon 	 * a non-mss value, force naglim to track mss.
198721fffe3SKacheong Poon 	 * This can help to aggregate small writes.
199721fffe3SKacheong Poon 	 */
200721fffe3SKacheong Poon 	if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim)
201721fffe3SKacheong Poon 		tcp->tcp_naglim = mss;
202721fffe3SKacheong Poon 	/*
203721fffe3SKacheong Poon 	 * TCP should be able to buffer at least 4 MSS data for obvious
204721fffe3SKacheong Poon 	 * performance reason.
205721fffe3SKacheong Poon 	 */
206721fffe3SKacheong Poon 	if ((mss << 2) > connp->conn_sndbuf)
207721fffe3SKacheong Poon 		connp->conn_sndbuf = mss << 2;
208721fffe3SKacheong Poon 
209721fffe3SKacheong Poon 	/*
210721fffe3SKacheong Poon 	 * Set the send lowater to at least twice of MSS.
211721fffe3SKacheong Poon 	 */
212721fffe3SKacheong Poon 	if ((mss << 1) > connp->conn_sndlowat)
213721fffe3SKacheong Poon 		connp->conn_sndlowat = mss << 1;
214721fffe3SKacheong Poon 
215721fffe3SKacheong Poon 	/*
216721fffe3SKacheong Poon 	 * Update tcp_cwnd according to the new value of MSS. Keep the
217721fffe3SKacheong Poon 	 * previous ratio to preserve the transmit rate.
218721fffe3SKacheong Poon 	 */
219721fffe3SKacheong Poon 	tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss;
220721fffe3SKacheong Poon 	tcp->tcp_cwnd_cnt = 0;
221721fffe3SKacheong Poon 
222721fffe3SKacheong Poon 	tcp->tcp_mss = mss;
223721fffe3SKacheong Poon 	(void) tcp_maxpsz_set(tcp, B_TRUE);
224721fffe3SKacheong Poon }
225721fffe3SKacheong Poon 
226721fffe3SKacheong Poon /*
227721fffe3SKacheong Poon  * Extract option values from a tcp header.  We put any found values into the
228721fffe3SKacheong Poon  * tcpopt struct and return a bitmask saying which options were found.
229721fffe3SKacheong Poon  */
230*1f183ba0SLauri Tirkkonen int
231721fffe3SKacheong Poon tcp_parse_options(tcpha_t *tcpha, tcp_opt_t *tcpopt)
232721fffe3SKacheong Poon {
233721fffe3SKacheong Poon 	uchar_t		*endp;
234721fffe3SKacheong Poon 	int		len;
235721fffe3SKacheong Poon 	uint32_t	mss;
236721fffe3SKacheong Poon 	uchar_t		*up = (uchar_t *)tcpha;
237721fffe3SKacheong Poon 	int		found = 0;
238721fffe3SKacheong Poon 	int32_t		sack_len;
239721fffe3SKacheong Poon 	tcp_seq		sack_begin, sack_end;
240721fffe3SKacheong Poon 	tcp_t		*tcp;
241721fffe3SKacheong Poon 
242721fffe3SKacheong Poon 	endp = up + TCP_HDR_LENGTH(tcpha);
243721fffe3SKacheong Poon 	up += TCP_MIN_HEADER_LENGTH;
244*1f183ba0SLauri Tirkkonen 	/*
245*1f183ba0SLauri Tirkkonen 	 * If timestamp option is aligned as recommended in RFC 7323 Appendix
246*1f183ba0SLauri Tirkkonen 	 * A, and is the only option, return quickly.
247*1f183ba0SLauri Tirkkonen 	 */
248*1f183ba0SLauri Tirkkonen 	if (TCP_HDR_LENGTH(tcpha) == (uint32_t)TCP_MIN_HEADER_LENGTH +
249*1f183ba0SLauri Tirkkonen 	    TCPOPT_REAL_TS_LEN &&
250*1f183ba0SLauri Tirkkonen 	    OK_32PTR(up) &&
251*1f183ba0SLauri Tirkkonen 	    *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) {
252*1f183ba0SLauri Tirkkonen 		tcpopt->tcp_opt_ts_val = ABE32_TO_U32((up+4));
253*1f183ba0SLauri Tirkkonen 		tcpopt->tcp_opt_ts_ecr = ABE32_TO_U32((up+8));
254*1f183ba0SLauri Tirkkonen 
255*1f183ba0SLauri Tirkkonen 		return (TCP_OPT_TSTAMP_PRESENT);
256*1f183ba0SLauri Tirkkonen 	}
257721fffe3SKacheong Poon 	while (up < endp) {
258721fffe3SKacheong Poon 		len = endp - up;
259721fffe3SKacheong Poon 		switch (*up) {
260721fffe3SKacheong Poon 		case TCPOPT_EOL:
261721fffe3SKacheong Poon 			break;
262721fffe3SKacheong Poon 
263721fffe3SKacheong Poon 		case TCPOPT_NOP:
264721fffe3SKacheong Poon 			up++;
265721fffe3SKacheong Poon 			continue;
266721fffe3SKacheong Poon 
267721fffe3SKacheong Poon 		case TCPOPT_MAXSEG:
268721fffe3SKacheong Poon 			if (len < TCPOPT_MAXSEG_LEN ||
269721fffe3SKacheong Poon 			    up[1] != TCPOPT_MAXSEG_LEN)
270721fffe3SKacheong Poon 				break;
271721fffe3SKacheong Poon 
272721fffe3SKacheong Poon 			mss = BE16_TO_U16(up+2);
273721fffe3SKacheong Poon 			/* Caller must handle tcp_mss_min and tcp_mss_max_* */
274721fffe3SKacheong Poon 			tcpopt->tcp_opt_mss = mss;
275721fffe3SKacheong Poon 			found |= TCP_OPT_MSS_PRESENT;
276721fffe3SKacheong Poon 
277721fffe3SKacheong Poon 			up += TCPOPT_MAXSEG_LEN;
278721fffe3SKacheong Poon 			continue;
279721fffe3SKacheong Poon 
280721fffe3SKacheong Poon 		case TCPOPT_WSCALE:
281721fffe3SKacheong Poon 			if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN)
282721fffe3SKacheong Poon 				break;
283721fffe3SKacheong Poon 
284721fffe3SKacheong Poon 			if (up[2] > TCP_MAX_WINSHIFT)
285721fffe3SKacheong Poon 				tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT;
286721fffe3SKacheong Poon 			else
287721fffe3SKacheong Poon 				tcpopt->tcp_opt_wscale = up[2];
288721fffe3SKacheong Poon 			found |= TCP_OPT_WSCALE_PRESENT;
289721fffe3SKacheong Poon 
290721fffe3SKacheong Poon 			up += TCPOPT_WS_LEN;
291721fffe3SKacheong Poon 			continue;
292721fffe3SKacheong Poon 
293721fffe3SKacheong Poon 		case TCPOPT_SACK_PERMITTED:
294721fffe3SKacheong Poon 			if (len < TCPOPT_SACK_OK_LEN ||
295721fffe3SKacheong Poon 			    up[1] != TCPOPT_SACK_OK_LEN)
296721fffe3SKacheong Poon 				break;
297721fffe3SKacheong Poon 			found |= TCP_OPT_SACK_OK_PRESENT;
298721fffe3SKacheong Poon 			up += TCPOPT_SACK_OK_LEN;
299721fffe3SKacheong Poon 			continue;
300721fffe3SKacheong Poon 
301721fffe3SKacheong Poon 		case TCPOPT_SACK:
302721fffe3SKacheong Poon 			if (len <= 2 || up[1] <= 2 || len < up[1])
303721fffe3SKacheong Poon 				break;
304721fffe3SKacheong Poon 
305721fffe3SKacheong Poon 			/* If TCP is not interested in SACK blks... */
306721fffe3SKacheong Poon 			if ((tcp = tcpopt->tcp) == NULL) {
307721fffe3SKacheong Poon 				up += up[1];
308721fffe3SKacheong Poon 				continue;
309721fffe3SKacheong Poon 			}
310721fffe3SKacheong Poon 			sack_len = up[1] - TCPOPT_HEADER_LEN;
311721fffe3SKacheong Poon 			up += TCPOPT_HEADER_LEN;
312721fffe3SKacheong Poon 
313721fffe3SKacheong Poon 			/*
314721fffe3SKacheong Poon 			 * If the list is empty, allocate one and assume
315721fffe3SKacheong Poon 			 * nothing is sack'ed.
316721fffe3SKacheong Poon 			 */
317721fffe3SKacheong Poon 			if (tcp->tcp_notsack_list == NULL) {
318721fffe3SKacheong Poon 				tcp_notsack_update(&(tcp->tcp_notsack_list),
319721fffe3SKacheong Poon 				    tcp->tcp_suna, tcp->tcp_snxt,
320721fffe3SKacheong Poon 				    &(tcp->tcp_num_notsack_blk),
321721fffe3SKacheong Poon 				    &(tcp->tcp_cnt_notsack_list));
322721fffe3SKacheong Poon 
323721fffe3SKacheong Poon 				/*
324721fffe3SKacheong Poon 				 * Make sure tcp_notsack_list is not NULL.
325721fffe3SKacheong Poon 				 * This happens when kmem_alloc(KM_NOSLEEP)
326721fffe3SKacheong Poon 				 * returns NULL.
327721fffe3SKacheong Poon 				 */
328721fffe3SKacheong Poon 				if (tcp->tcp_notsack_list == NULL) {
329721fffe3SKacheong Poon 					up += sack_len;
330721fffe3SKacheong Poon 					continue;
331721fffe3SKacheong Poon 				}
332721fffe3SKacheong Poon 				tcp->tcp_fack = tcp->tcp_suna;
333721fffe3SKacheong Poon 			}
334721fffe3SKacheong Poon 
335721fffe3SKacheong Poon 			while (sack_len > 0) {
336721fffe3SKacheong Poon 				if (up + 8 > endp) {
337721fffe3SKacheong Poon 					up = endp;
338721fffe3SKacheong Poon 					break;
339721fffe3SKacheong Poon 				}
340721fffe3SKacheong Poon 				sack_begin = BE32_TO_U32(up);
341721fffe3SKacheong Poon 				up += 4;
342721fffe3SKacheong Poon 				sack_end = BE32_TO_U32(up);
343721fffe3SKacheong Poon 				up += 4;
344721fffe3SKacheong Poon 				sack_len -= 8;
345721fffe3SKacheong Poon 				/*
346721fffe3SKacheong Poon 				 * Bounds checking.  Make sure the SACK
347721fffe3SKacheong Poon 				 * info is within tcp_suna and tcp_snxt.
348721fffe3SKacheong Poon 				 * If this SACK blk is out of bound, ignore
349721fffe3SKacheong Poon 				 * it but continue to parse the following
350721fffe3SKacheong Poon 				 * blks.
351721fffe3SKacheong Poon 				 */
352721fffe3SKacheong Poon 				if (SEQ_LEQ(sack_end, sack_begin) ||
353721fffe3SKacheong Poon 				    SEQ_LT(sack_begin, tcp->tcp_suna) ||
354721fffe3SKacheong Poon 				    SEQ_GT(sack_end, tcp->tcp_snxt)) {
355721fffe3SKacheong Poon 					continue;
356721fffe3SKacheong Poon 				}
357721fffe3SKacheong Poon 				tcp_notsack_insert(&(tcp->tcp_notsack_list),
358721fffe3SKacheong Poon 				    sack_begin, sack_end,
359721fffe3SKacheong Poon 				    &(tcp->tcp_num_notsack_blk),
360721fffe3SKacheong Poon 				    &(tcp->tcp_cnt_notsack_list));
361721fffe3SKacheong Poon 				if (SEQ_GT(sack_end, tcp->tcp_fack)) {
362721fffe3SKacheong Poon 					tcp->tcp_fack = sack_end;
363721fffe3SKacheong Poon 				}
364721fffe3SKacheong Poon 			}
365721fffe3SKacheong Poon 			found |= TCP_OPT_SACK_PRESENT;
366721fffe3SKacheong Poon 			continue;
367721fffe3SKacheong Poon 
368721fffe3SKacheong Poon 		case TCPOPT_TSTAMP:
369721fffe3SKacheong Poon 			if (len < TCPOPT_TSTAMP_LEN ||
370721fffe3SKacheong Poon 			    up[1] != TCPOPT_TSTAMP_LEN)
371721fffe3SKacheong Poon 				break;
372721fffe3SKacheong Poon 
373721fffe3SKacheong Poon 			tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2);
374721fffe3SKacheong Poon 			tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6);
375721fffe3SKacheong Poon 
376721fffe3SKacheong Poon 			found |= TCP_OPT_TSTAMP_PRESENT;
377721fffe3SKacheong Poon 
378721fffe3SKacheong Poon 			up += TCPOPT_TSTAMP_LEN;
379721fffe3SKacheong Poon 			continue;
380721fffe3SKacheong Poon 
381721fffe3SKacheong Poon 		default:
382721fffe3SKacheong Poon 			if (len <= 1 || len < (int)up[1] || up[1] == 0)
383721fffe3SKacheong Poon 				break;
384721fffe3SKacheong Poon 			up += up[1];
385721fffe3SKacheong Poon 			continue;
386721fffe3SKacheong Poon 		}
387721fffe3SKacheong Poon 		break;
388721fffe3SKacheong Poon 	}
389721fffe3SKacheong Poon 	return (found);
390721fffe3SKacheong Poon }
391721fffe3SKacheong Poon 
392721fffe3SKacheong Poon /*
393721fffe3SKacheong Poon  * Process all TCP option in SYN segment.  Note that this function should
394721fffe3SKacheong Poon  * be called after tcp_set_destination() is called so that the necessary info
395721fffe3SKacheong Poon  * from IRE is already set in the tcp structure.
396721fffe3SKacheong Poon  *
397721fffe3SKacheong Poon  * This function sets up the correct tcp_mss value according to the
398721fffe3SKacheong Poon  * MSS option value and our header size.  It also sets up the window scale
399721fffe3SKacheong Poon  * and timestamp values, and initialize SACK info blocks.  But it does not
400721fffe3SKacheong Poon  * change receive window size after setting the tcp_mss value.  The caller
401721fffe3SKacheong Poon  * should do the appropriate change.
402721fffe3SKacheong Poon  */
403721fffe3SKacheong Poon static void
404721fffe3SKacheong Poon tcp_process_options(tcp_t *tcp, tcpha_t *tcpha)
405721fffe3SKacheong Poon {
406721fffe3SKacheong Poon 	int options;
407721fffe3SKacheong Poon 	tcp_opt_t tcpopt;
408721fffe3SKacheong Poon 	uint32_t mss_max;
409721fffe3SKacheong Poon 	char *tmp_tcph;
410721fffe3SKacheong Poon 	tcp_stack_t	*tcps = tcp->tcp_tcps;
411721fffe3SKacheong Poon 	conn_t		*connp = tcp->tcp_connp;
412721fffe3SKacheong Poon 
413721fffe3SKacheong Poon 	tcpopt.tcp = NULL;
414721fffe3SKacheong Poon 	options = tcp_parse_options(tcpha, &tcpopt);
415721fffe3SKacheong Poon 
416721fffe3SKacheong Poon 	/*
417721fffe3SKacheong Poon 	 * Process MSS option.  Note that MSS option value does not account
418721fffe3SKacheong Poon 	 * for IP or TCP options.  This means that it is equal to MTU - minimum
419721fffe3SKacheong Poon 	 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for
420721fffe3SKacheong Poon 	 * IPv6.
421721fffe3SKacheong Poon 	 */
422721fffe3SKacheong Poon 	if (!(options & TCP_OPT_MSS_PRESENT)) {
423721fffe3SKacheong Poon 		if (connp->conn_ipversion == IPV4_VERSION)
424721fffe3SKacheong Poon 			tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4;
425721fffe3SKacheong Poon 		else
426721fffe3SKacheong Poon 			tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6;
427721fffe3SKacheong Poon 	} else {
428721fffe3SKacheong Poon 		if (connp->conn_ipversion == IPV4_VERSION)
429721fffe3SKacheong Poon 			mss_max = tcps->tcps_mss_max_ipv4;
430721fffe3SKacheong Poon 		else
431721fffe3SKacheong Poon 			mss_max = tcps->tcps_mss_max_ipv6;
432721fffe3SKacheong Poon 		if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min)
433721fffe3SKacheong Poon 			tcpopt.tcp_opt_mss = tcps->tcps_mss_min;
434721fffe3SKacheong Poon 		else if (tcpopt.tcp_opt_mss > mss_max)
435721fffe3SKacheong Poon 			tcpopt.tcp_opt_mss = mss_max;
436721fffe3SKacheong Poon 	}
437721fffe3SKacheong Poon 
438721fffe3SKacheong Poon 	/* Process Window Scale option. */
439721fffe3SKacheong Poon 	if (options & TCP_OPT_WSCALE_PRESENT) {
440721fffe3SKacheong Poon 		tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale;
441721fffe3SKacheong Poon 		tcp->tcp_snd_ws_ok = B_TRUE;
442721fffe3SKacheong Poon 	} else {
443721fffe3SKacheong Poon 		tcp->tcp_snd_ws = B_FALSE;
444721fffe3SKacheong Poon 		tcp->tcp_snd_ws_ok = B_FALSE;
445721fffe3SKacheong Poon 		tcp->tcp_rcv_ws = B_FALSE;
446721fffe3SKacheong Poon 	}
447721fffe3SKacheong Poon 
448721fffe3SKacheong Poon 	/* Process Timestamp option. */
449721fffe3SKacheong Poon 	if ((options & TCP_OPT_TSTAMP_PRESENT) &&
450721fffe3SKacheong Poon 	    (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) {
451721fffe3SKacheong Poon 		tmp_tcph = (char *)tcp->tcp_tcpha;
452721fffe3SKacheong Poon 
453721fffe3SKacheong Poon 		tcp->tcp_snd_ts_ok = B_TRUE;
454721fffe3SKacheong Poon 		tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val;
455721fffe3SKacheong Poon 		tcp->tcp_last_rcv_lbolt = ddi_get_lbolt64();
456721fffe3SKacheong Poon 		ASSERT(OK_32PTR(tmp_tcph));
457721fffe3SKacheong Poon 		ASSERT(connp->conn_ht_ulp_len == TCP_MIN_HEADER_LENGTH);
458721fffe3SKacheong Poon 
459721fffe3SKacheong Poon 		/* Fill in our template header with basic timestamp option. */
460721fffe3SKacheong Poon 		tmp_tcph += connp->conn_ht_ulp_len;
461721fffe3SKacheong Poon 		tmp_tcph[0] = TCPOPT_NOP;
462721fffe3SKacheong Poon 		tmp_tcph[1] = TCPOPT_NOP;
463721fffe3SKacheong Poon 		tmp_tcph[2] = TCPOPT_TSTAMP;
464721fffe3SKacheong Poon 		tmp_tcph[3] = TCPOPT_TSTAMP_LEN;
465721fffe3SKacheong Poon 		connp->conn_ht_iphc_len += TCPOPT_REAL_TS_LEN;
466721fffe3SKacheong Poon 		connp->conn_ht_ulp_len += TCPOPT_REAL_TS_LEN;
467721fffe3SKacheong Poon 		tcp->tcp_tcpha->tha_offset_and_reserved += (3 << 4);
468721fffe3SKacheong Poon 	} else {
469721fffe3SKacheong Poon 		tcp->tcp_snd_ts_ok = B_FALSE;
470721fffe3SKacheong Poon 	}
471721fffe3SKacheong Poon 
472721fffe3SKacheong Poon 	/*
473721fffe3SKacheong Poon 	 * Process SACK options.  If SACK is enabled for this connection,
474721fffe3SKacheong Poon 	 * then allocate the SACK info structure.  Note the following ways
475721fffe3SKacheong Poon 	 * when tcp_snd_sack_ok is set to true.
476721fffe3SKacheong Poon 	 *
477721fffe3SKacheong Poon 	 * For active connection: in tcp_set_destination() called in
478721fffe3SKacheong Poon 	 * tcp_connect().
479721fffe3SKacheong Poon 	 *
480721fffe3SKacheong Poon 	 * For passive connection: in tcp_set_destination() called in
481721fffe3SKacheong Poon 	 * tcp_input_listener().
482721fffe3SKacheong Poon 	 *
483721fffe3SKacheong Poon 	 * That's the reason why the extra TCP_IS_DETACHED() check is there.
484721fffe3SKacheong Poon 	 * That check makes sure that if we did not send a SACK OK option,
485721fffe3SKacheong Poon 	 * we will not enable SACK for this connection even though the other
486721fffe3SKacheong Poon 	 * side sends us SACK OK option.  For active connection, the SACK
487721fffe3SKacheong Poon 	 * info structure has already been allocated.  So we need to free
488721fffe3SKacheong Poon 	 * it if SACK is disabled.
489721fffe3SKacheong Poon 	 */
490721fffe3SKacheong Poon 	if ((options & TCP_OPT_SACK_OK_PRESENT) &&
491721fffe3SKacheong Poon 	    (tcp->tcp_snd_sack_ok ||
492721fffe3SKacheong Poon 	    (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) {
49366cd0f60SKacheong Poon 		ASSERT(tcp->tcp_num_sack_blk == 0);
49466cd0f60SKacheong Poon 		ASSERT(tcp->tcp_notsack_list == NULL);
49566cd0f60SKacheong Poon 
496721fffe3SKacheong Poon 		tcp->tcp_snd_sack_ok = B_TRUE;
497721fffe3SKacheong Poon 		if (tcp->tcp_snd_ts_ok) {
498721fffe3SKacheong Poon 			tcp->tcp_max_sack_blk = 3;
499721fffe3SKacheong Poon 		} else {
500721fffe3SKacheong Poon 			tcp->tcp_max_sack_blk = 4;
501721fffe3SKacheong Poon 		}
50266cd0f60SKacheong Poon 	} else if (tcp->tcp_snd_sack_ok) {
503721fffe3SKacheong Poon 		/*
504721fffe3SKacheong Poon 		 * Resetting tcp_snd_sack_ok to B_FALSE so that
505721fffe3SKacheong Poon 		 * no SACK info will be used for this
506721fffe3SKacheong Poon 		 * connection.  This assumes that SACK usage
507721fffe3SKacheong Poon 		 * permission is negotiated.  This may need
508721fffe3SKacheong Poon 		 * to be changed once this is clarified.
509721fffe3SKacheong Poon 		 */
51066cd0f60SKacheong Poon 		ASSERT(tcp->tcp_num_sack_blk == 0);
511721fffe3SKacheong Poon 		ASSERT(tcp->tcp_notsack_list == NULL);
512721fffe3SKacheong Poon 		tcp->tcp_snd_sack_ok = B_FALSE;
513721fffe3SKacheong Poon 	}
514721fffe3SKacheong Poon 
515721fffe3SKacheong Poon 	/*
516721fffe3SKacheong Poon 	 * Now we know the exact TCP/IP header length, subtract
517721fffe3SKacheong Poon 	 * that from tcp_mss to get our side's MSS.
518721fffe3SKacheong Poon 	 */
519721fffe3SKacheong Poon 	tcp->tcp_mss -= connp->conn_ht_iphc_len;
520721fffe3SKacheong Poon 
521721fffe3SKacheong Poon 	/*
522721fffe3SKacheong Poon 	 * Here we assume that the other side's header size will be equal to
523721fffe3SKacheong Poon 	 * our header size.  We calculate the real MSS accordingly.  Need to
524721fffe3SKacheong Poon 	 * take into additional stuffs IPsec puts in.
525721fffe3SKacheong Poon 	 *
526721fffe3SKacheong Poon 	 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header)
527721fffe3SKacheong Poon 	 */
528721fffe3SKacheong Poon 	tcpopt.tcp_opt_mss -= connp->conn_ht_iphc_len +
529721fffe3SKacheong Poon 	    tcp->tcp_ipsec_overhead -
530721fffe3SKacheong Poon 	    ((connp->conn_ipversion == IPV4_VERSION ?
531721fffe3SKacheong Poon 	    IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH);
532721fffe3SKacheong Poon 
533721fffe3SKacheong Poon 	/*
534721fffe3SKacheong Poon 	 * Set MSS to the smaller one of both ends of the connection.
535721fffe3SKacheong Poon 	 * We should not have called tcp_mss_set() before, but our
536721fffe3SKacheong Poon 	 * side of the MSS should have been set to a proper value
537721fffe3SKacheong Poon 	 * by tcp_set_destination().  tcp_mss_set() will also set up the
538721fffe3SKacheong Poon 	 * STREAM head parameters properly.
539721fffe3SKacheong Poon 	 *
540721fffe3SKacheong Poon 	 * If we have a larger-than-16-bit window but the other side
541721fffe3SKacheong Poon 	 * didn't want to do window scale, tcp_rwnd_set() will take
542721fffe3SKacheong Poon 	 * care of that.
543721fffe3SKacheong Poon 	 */
544721fffe3SKacheong Poon 	tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss));
545721fffe3SKacheong Poon 
546721fffe3SKacheong Poon 	/*
547721fffe3SKacheong Poon 	 * Initialize tcp_cwnd value. After tcp_mss_set(), tcp_mss has been
548721fffe3SKacheong Poon 	 * updated properly.
549721fffe3SKacheong Poon 	 */
550721fffe3SKacheong Poon 	TCP_SET_INIT_CWND(tcp, tcp->tcp_mss, tcps->tcps_slow_start_initial);
551721fffe3SKacheong Poon }
552721fffe3SKacheong Poon 
553721fffe3SKacheong Poon /*
554721fffe3SKacheong Poon  * Add a new piece to the tcp reassembly queue.  If the gap at the beginning
555721fffe3SKacheong Poon  * is filled, return as much as we can.  The message passed in may be
556721fffe3SKacheong Poon  * multi-part, chained using b_cont.  "start" is the starting sequence
557721fffe3SKacheong Poon  * number for this piece.
558721fffe3SKacheong Poon  */
559721fffe3SKacheong Poon static mblk_t *
560721fffe3SKacheong Poon tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start)
561721fffe3SKacheong Poon {
562721fffe3SKacheong Poon 	uint32_t	end;
563721fffe3SKacheong Poon 	mblk_t		*mp1;
564721fffe3SKacheong Poon 	mblk_t		*mp2;
565721fffe3SKacheong Poon 	mblk_t		*next_mp;
566721fffe3SKacheong Poon 	uint32_t	u1;
567721fffe3SKacheong Poon 	tcp_stack_t	*tcps = tcp->tcp_tcps;
568721fffe3SKacheong Poon 
569721fffe3SKacheong Poon 
570721fffe3SKacheong Poon 	/* Walk through all the new pieces. */
571721fffe3SKacheong Poon 	do {
572721fffe3SKacheong Poon 		ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
573721fffe3SKacheong Poon 		    (uintptr_t)INT_MAX);
574721fffe3SKacheong Poon 		end = start + (int)(mp->b_wptr - mp->b_rptr);
575721fffe3SKacheong Poon 		next_mp = mp->b_cont;
576721fffe3SKacheong Poon 		if (start == end) {
577721fffe3SKacheong Poon 			/* Empty.  Blast it. */
578721fffe3SKacheong Poon 			freeb(mp);
579721fffe3SKacheong Poon 			continue;
580721fffe3SKacheong Poon 		}
581721fffe3SKacheong Poon 		mp->b_cont = NULL;
582721fffe3SKacheong Poon 		TCP_REASS_SET_SEQ(mp, start);
583721fffe3SKacheong Poon 		TCP_REASS_SET_END(mp, end);
584721fffe3SKacheong Poon 		mp1 = tcp->tcp_reass_tail;
585721fffe3SKacheong Poon 		if (!mp1) {
586721fffe3SKacheong Poon 			tcp->tcp_reass_tail = mp;
587721fffe3SKacheong Poon 			tcp->tcp_reass_head = mp;
588721fffe3SKacheong Poon 			TCPS_BUMP_MIB(tcps, tcpInDataUnorderSegs);
589721fffe3SKacheong Poon 			TCPS_UPDATE_MIB(tcps, tcpInDataUnorderBytes,
590721fffe3SKacheong Poon 			    end - start);
591721fffe3SKacheong Poon 			continue;
592721fffe3SKacheong Poon 		}
593721fffe3SKacheong Poon 		/* New stuff completely beyond tail? */
594721fffe3SKacheong Poon 		if (SEQ_GEQ(start, TCP_REASS_END(mp1))) {
595721fffe3SKacheong Poon 			/* Link it on end. */
596721fffe3SKacheong Poon 			mp1->b_cont = mp;
597721fffe3SKacheong Poon 			tcp->tcp_reass_tail = mp;
598721fffe3SKacheong Poon 			TCPS_BUMP_MIB(tcps, tcpInDataUnorderSegs);
599721fffe3SKacheong Poon 			TCPS_UPDATE_MIB(tcps, tcpInDataUnorderBytes,
600721fffe3SKacheong Poon 			    end - start);
601721fffe3SKacheong Poon 			continue;
602721fffe3SKacheong Poon 		}
603721fffe3SKacheong Poon 		mp1 = tcp->tcp_reass_head;
604721fffe3SKacheong Poon 		u1 = TCP_REASS_SEQ(mp1);
605721fffe3SKacheong Poon 		/* New stuff at the front? */
606721fffe3SKacheong Poon 		if (SEQ_LT(start, u1)) {
607721fffe3SKacheong Poon 			/* Yes... Check for overlap. */
608721fffe3SKacheong Poon 			mp->b_cont = mp1;
609721fffe3SKacheong Poon 			tcp->tcp_reass_head = mp;
610721fffe3SKacheong Poon 			tcp_reass_elim_overlap(tcp, mp);
611721fffe3SKacheong Poon 			continue;
612721fffe3SKacheong Poon 		}
613721fffe3SKacheong Poon 		/*
614721fffe3SKacheong Poon 		 * The new piece fits somewhere between the head and tail.
615721fffe3SKacheong Poon 		 * We find our slot, where mp1 precedes us and mp2 trails.
616721fffe3SKacheong Poon 		 */
617721fffe3SKacheong Poon 		for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) {
618721fffe3SKacheong Poon 			u1 = TCP_REASS_SEQ(mp2);
619721fffe3SKacheong Poon 			if (SEQ_LEQ(start, u1))
620721fffe3SKacheong Poon 				break;
621721fffe3SKacheong Poon 		}
622721fffe3SKacheong Poon 		/* Link ourselves in */
623721fffe3SKacheong Poon 		mp->b_cont = mp2;
624721fffe3SKacheong Poon 		mp1->b_cont = mp;
625721fffe3SKacheong Poon 
626721fffe3SKacheong Poon 		/* Trim overlap with following mblk(s) first */
627721fffe3SKacheong Poon 		tcp_reass_elim_overlap(tcp, mp);
628721fffe3SKacheong Poon 
629721fffe3SKacheong Poon 		/* Trim overlap with preceding mblk */
630721fffe3SKacheong Poon 		tcp_reass_elim_overlap(tcp, mp1);
631721fffe3SKacheong Poon 
632721fffe3SKacheong Poon 	} while (start = end, mp = next_mp);
633721fffe3SKacheong Poon 	mp1 = tcp->tcp_reass_head;
634721fffe3SKacheong Poon 	/* Anything ready to go? */
635721fffe3SKacheong Poon 	if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt)
636721fffe3SKacheong Poon 		return (NULL);
637721fffe3SKacheong Poon 	/* Eat what we can off the queue */
638721fffe3SKacheong Poon 	for (;;) {
639721fffe3SKacheong Poon 		mp = mp1->b_cont;
640721fffe3SKacheong Poon 		end = TCP_REASS_END(mp1);
641721fffe3SKacheong Poon 		TCP_REASS_SET_SEQ(mp1, 0);
642721fffe3SKacheong Poon 		TCP_REASS_SET_END(mp1, 0);
643721fffe3SKacheong Poon 		if (!mp) {
644721fffe3SKacheong Poon 			tcp->tcp_reass_tail = NULL;
645721fffe3SKacheong Poon 			break;
646721fffe3SKacheong Poon 		}
647721fffe3SKacheong Poon 		if (end != TCP_REASS_SEQ(mp)) {
648721fffe3SKacheong Poon 			mp1->b_cont = NULL;
649721fffe3SKacheong Poon 			break;
650721fffe3SKacheong Poon 		}
651721fffe3SKacheong Poon 		mp1 = mp;
652721fffe3SKacheong Poon 	}
653721fffe3SKacheong Poon 	mp1 = tcp->tcp_reass_head;
654721fffe3SKacheong Poon 	tcp->tcp_reass_head = mp;
655721fffe3SKacheong Poon 	return (mp1);
656721fffe3SKacheong Poon }
657721fffe3SKacheong Poon 
658721fffe3SKacheong Poon /* Eliminate any overlap that mp may have over later mblks */
659721fffe3SKacheong Poon static void
660721fffe3SKacheong Poon tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp)
661721fffe3SKacheong Poon {
662721fffe3SKacheong Poon 	uint32_t	end;
663721fffe3SKacheong Poon 	mblk_t		*mp1;
664721fffe3SKacheong Poon 	uint32_t	u1;
665721fffe3SKacheong Poon 	tcp_stack_t	*tcps = tcp->tcp_tcps;
666721fffe3SKacheong Poon 
667721fffe3SKacheong Poon 	end = TCP_REASS_END(mp);
668721fffe3SKacheong Poon 	while ((mp1 = mp->b_cont) != NULL) {
669721fffe3SKacheong Poon 		u1 = TCP_REASS_SEQ(mp1);
670721fffe3SKacheong Poon 		if (!SEQ_GT(end, u1))
671721fffe3SKacheong Poon 			break;
672721fffe3SKacheong Poon 		if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) {
673721fffe3SKacheong Poon 			mp->b_wptr -= end - u1;
674721fffe3SKacheong Poon 			TCP_REASS_SET_END(mp, u1);
675721fffe3SKacheong Poon 			TCPS_BUMP_MIB(tcps, tcpInDataPartDupSegs);
676721fffe3SKacheong Poon 			TCPS_UPDATE_MIB(tcps, tcpInDataPartDupBytes,
677721fffe3SKacheong Poon 			    end - u1);
678721fffe3SKacheong Poon 			break;
679721fffe3SKacheong Poon 		}
680721fffe3SKacheong Poon 		mp->b_cont = mp1->b_cont;
681721fffe3SKacheong Poon 		TCP_REASS_SET_SEQ(mp1, 0);
682721fffe3SKacheong Poon 		TCP_REASS_SET_END(mp1, 0);
683721fffe3SKacheong Poon 		freeb(mp1);
684721fffe3SKacheong Poon 		TCPS_BUMP_MIB(tcps, tcpInDataDupSegs);
685721fffe3SKacheong Poon 		TCPS_UPDATE_MIB(tcps, tcpInDataDupBytes, end - u1);
686721fffe3SKacheong Poon 	}
687721fffe3SKacheong Poon 	if (!mp1)
688721fffe3SKacheong Poon 		tcp->tcp_reass_tail = mp;
689721fffe3SKacheong Poon }
690721fffe3SKacheong Poon 
691721fffe3SKacheong Poon /*
692*1f183ba0SLauri Tirkkonen  * This function does PAWS protection check, per RFC 7323 section 5. Requires
693*1f183ba0SLauri Tirkkonen  * that timestamp options are already processed into tcpoptp. Returns B_TRUE if
694*1f183ba0SLauri Tirkkonen  * the segment passes the PAWS test, else returns B_FALSE.
695721fffe3SKacheong Poon  */
696721fffe3SKacheong Poon boolean_t
697*1f183ba0SLauri Tirkkonen tcp_paws_check(tcp_t *tcp, const tcp_opt_t *tcpoptp)
698721fffe3SKacheong Poon {
699*1f183ba0SLauri Tirkkonen 	if (TSTMP_LT(tcpoptp->tcp_opt_ts_val,
700721fffe3SKacheong Poon 	    tcp->tcp_ts_recent)) {
701fec71d83SGeorge Shepherd 		if (LBOLT_FASTPATH64 <
702fec71d83SGeorge Shepherd 		    (tcp->tcp_last_rcv_lbolt + PAWS_TIMEOUT)) {
703721fffe3SKacheong Poon 			/* This segment is not acceptable. */
704721fffe3SKacheong Poon 			return (B_FALSE);
705721fffe3SKacheong Poon 		} else {
706721fffe3SKacheong Poon 			/*
707721fffe3SKacheong Poon 			 * Connection has been idle for
708721fffe3SKacheong Poon 			 * too long.  Reset the timestamp
709721fffe3SKacheong Poon 			 */
710721fffe3SKacheong Poon 			tcp->tcp_ts_recent =
711721fffe3SKacheong Poon 			    tcpoptp->tcp_opt_ts_val;
712721fffe3SKacheong Poon 		}
713721fffe3SKacheong Poon 	}
714721fffe3SKacheong Poon 	return (B_TRUE);
715721fffe3SKacheong Poon }
716721fffe3SKacheong Poon 
717721fffe3SKacheong Poon /*
718721fffe3SKacheong Poon  * Defense for the SYN attack -
719721fffe3SKacheong Poon  * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest
720721fffe3SKacheong Poon  *    one from the list of droppable eagers. This list is a subset of q0.
721721fffe3SKacheong Poon  *    see comments before the definition of MAKE_DROPPABLE().
722721fffe3SKacheong Poon  * 2. Don't drop a SYN request before its first timeout. This gives every
723721fffe3SKacheong Poon  *    request at least til the first timeout to complete its 3-way handshake.
724721fffe3SKacheong Poon  * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many
725721fffe3SKacheong Poon  *    requests currently on the queue that has timed out. This will be used
726721fffe3SKacheong Poon  *    as an indicator of whether an attack is under way, so that appropriate
727721fffe3SKacheong Poon  *    actions can be taken. (It's incremented in tcp_timer() and decremented
728721fffe3SKacheong Poon  *    either when eager goes into ESTABLISHED, or gets freed up.)
729721fffe3SKacheong Poon  * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on
730721fffe3SKacheong Poon  *    # of timeout drops back to <= q0len/32 => SYN alert off
731721fffe3SKacheong Poon  */
732721fffe3SKacheong Poon static boolean_t
733721fffe3SKacheong Poon tcp_drop_q0(tcp_t *tcp)
734721fffe3SKacheong Poon {
735721fffe3SKacheong Poon 	tcp_t	*eager;
736721fffe3SKacheong Poon 	mblk_t	*mp;
737721fffe3SKacheong Poon 	tcp_stack_t	*tcps = tcp->tcp_tcps;
738721fffe3SKacheong Poon 
739721fffe3SKacheong Poon 	ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock));
740721fffe3SKacheong Poon 	ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0);
741721fffe3SKacheong Poon 
742721fffe3SKacheong Poon 	/* Pick oldest eager from the list of droppable eagers */
743721fffe3SKacheong Poon 	eager = tcp->tcp_eager_prev_drop_q0;
744721fffe3SKacheong Poon 
745721fffe3SKacheong Poon 	/* If list is empty. return B_FALSE */
746721fffe3SKacheong Poon 	if (eager == tcp) {
747721fffe3SKacheong Poon 		return (B_FALSE);
748721fffe3SKacheong Poon 	}
749721fffe3SKacheong Poon 
750721fffe3SKacheong Poon 	/* If allocated, the mp will be freed in tcp_clean_death_wrapper() */
751721fffe3SKacheong Poon 	if ((mp = allocb(0, BPRI_HI)) == NULL)
752721fffe3SKacheong Poon 		return (B_FALSE);
753721fffe3SKacheong Poon 
754721fffe3SKacheong Poon 	/*
755721fffe3SKacheong Poon 	 * Take this eager out from the list of droppable eagers since we are
756721fffe3SKacheong Poon 	 * going to drop it.
757721fffe3SKacheong Poon 	 */
758721fffe3SKacheong Poon 	MAKE_UNDROPPABLE(eager);
759721fffe3SKacheong Poon 
760721fffe3SKacheong Poon 	if (tcp->tcp_connp->conn_debug) {
761721fffe3SKacheong Poon 		(void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE,
762721fffe3SKacheong Poon 		    "tcp_drop_q0: listen half-open queue (max=%d) overflow"
763721fffe3SKacheong Poon 		    " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0,
764721fffe3SKacheong Poon 		    tcp->tcp_conn_req_cnt_q0,
765721fffe3SKacheong Poon 		    tcp_display(tcp, NULL, DISP_PORT_ONLY));
766721fffe3SKacheong Poon 	}
767721fffe3SKacheong Poon 
768721fffe3SKacheong Poon 	TCPS_BUMP_MIB(tcps, tcpHalfOpenDrop);
769721fffe3SKacheong Poon 
770721fffe3SKacheong Poon 	/* Put a reference on the conn as we are enqueueing it in the sqeue */
771721fffe3SKacheong Poon 	CONN_INC_REF(eager->tcp_connp);
772721fffe3SKacheong Poon 
773721fffe3SKacheong Poon 	SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp,
774721fffe3SKacheong Poon 	    tcp_clean_death_wrapper, eager->tcp_connp, NULL,
775721fffe3SKacheong Poon 	    SQ_FILL, SQTAG_TCP_DROP_Q0);
776721fffe3SKacheong Poon 
777721fffe3SKacheong Poon 	return (B_TRUE);
778721fffe3SKacheong Poon }
779721fffe3SKacheong Poon 
780721fffe3SKacheong Poon /*
781721fffe3SKacheong Poon  * Handle a SYN on an AF_INET6 socket; can be either IPv4 or IPv6
782721fffe3SKacheong Poon  */
783721fffe3SKacheong Poon static mblk_t *
784721fffe3SKacheong Poon tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp,
785721fffe3SKacheong Poon     ip_recv_attr_t *ira)
786721fffe3SKacheong Poon {
787721fffe3SKacheong Poon 	tcp_t 		*ltcp = lconnp->conn_tcp;
788721fffe3SKacheong Poon 	tcp_t		*tcp = connp->conn_tcp;
789721fffe3SKacheong Poon 	mblk_t		*tpi_mp;
790721fffe3SKacheong Poon 	ipha_t		*ipha;
791721fffe3SKacheong Poon 	ip6_t		*ip6h;
792721fffe3SKacheong Poon 	sin6_t 		sin6;
793721fffe3SKacheong Poon 	uint_t		ifindex = ira->ira_ruifindex;
794721fffe3SKacheong Poon 	tcp_stack_t	*tcps = tcp->tcp_tcps;
795721fffe3SKacheong Poon 
796721fffe3SKacheong Poon 	if (ira->ira_flags & IRAF_IS_IPV4) {
797721fffe3SKacheong Poon 		ipha = (ipha_t *)mp->b_rptr;
798721fffe3SKacheong Poon 
799721fffe3SKacheong Poon 		connp->conn_ipversion = IPV4_VERSION;
800721fffe3SKacheong Poon 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_laddr_v6);
801721fffe3SKacheong Poon 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_faddr_v6);
802721fffe3SKacheong Poon 		connp->conn_saddr_v6 = connp->conn_laddr_v6;
803721fffe3SKacheong Poon 
804721fffe3SKacheong Poon 		sin6 = sin6_null;
805721fffe3SKacheong Poon 		sin6.sin6_addr = connp->conn_faddr_v6;
806721fffe3SKacheong Poon 		sin6.sin6_port = connp->conn_fport;
807721fffe3SKacheong Poon 		sin6.sin6_family = AF_INET6;
808721fffe3SKacheong Poon 		sin6.__sin6_src_id = ip_srcid_find_addr(&connp->conn_laddr_v6,
809721fffe3SKacheong Poon 		    IPCL_ZONEID(lconnp), tcps->tcps_netstack);
810721fffe3SKacheong Poon 
811721fffe3SKacheong Poon 		if (connp->conn_recv_ancillary.crb_recvdstaddr) {
812721fffe3SKacheong Poon 			sin6_t	sin6d;
813721fffe3SKacheong Poon 
814721fffe3SKacheong Poon 			sin6d = sin6_null;
815721fffe3SKacheong Poon 			sin6d.sin6_addr = connp->conn_laddr_v6;
816721fffe3SKacheong Poon 			sin6d.sin6_port = connp->conn_lport;
817721fffe3SKacheong Poon 			sin6d.sin6_family = AF_INET;
818721fffe3SKacheong Poon 			tpi_mp = mi_tpi_extconn_ind(NULL,
819721fffe3SKacheong Poon 			    (char *)&sin6d, sizeof (sin6_t),
820721fffe3SKacheong Poon 			    (char *)&tcp,
821721fffe3SKacheong Poon 			    (t_scalar_t)sizeof (intptr_t),
822721fffe3SKacheong Poon 			    (char *)&sin6d, sizeof (sin6_t),
823721fffe3SKacheong Poon 			    (t_scalar_t)ltcp->tcp_conn_req_seqnum);
824721fffe3SKacheong Poon 		} else {
825721fffe3SKacheong Poon 			tpi_mp = mi_tpi_conn_ind(NULL,
826721fffe3SKacheong Poon 			    (char *)&sin6, sizeof (sin6_t),
827721fffe3SKacheong Poon 			    (char *)&tcp, (t_scalar_t)sizeof (intptr_t),
828721fffe3SKacheong Poon 			    (t_scalar_t)ltcp->tcp_conn_req_seqnum);
829721fffe3SKacheong Poon 		}
830721fffe3SKacheong Poon 	} else {
831721fffe3SKacheong Poon 		ip6h = (ip6_t *)mp->b_rptr;
832721fffe3SKacheong Poon 
833721fffe3SKacheong Poon 		connp->conn_ipversion = IPV6_VERSION;
834721fffe3SKacheong Poon 		connp->conn_laddr_v6 = ip6h->ip6_dst;
835721fffe3SKacheong Poon 		connp->conn_faddr_v6 = ip6h->ip6_src;
836721fffe3SKacheong Poon 		connp->conn_saddr_v6 = connp->conn_laddr_v6;
837721fffe3SKacheong Poon 
838721fffe3SKacheong Poon 		sin6 = sin6_null;
839721fffe3SKacheong Poon 		sin6.sin6_addr = connp->conn_faddr_v6;
840721fffe3SKacheong Poon 		sin6.sin6_port = connp->conn_fport;
841721fffe3SKacheong Poon 		sin6.sin6_family = AF_INET6;
842721fffe3SKacheong Poon 		sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK;
843721fffe3SKacheong Poon 		sin6.__sin6_src_id = ip_srcid_find_addr(&connp->conn_laddr_v6,
844721fffe3SKacheong Poon 		    IPCL_ZONEID(lconnp), tcps->tcps_netstack);
845721fffe3SKacheong Poon 
846721fffe3SKacheong Poon 		if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) {
847721fffe3SKacheong Poon 			/* Pass up the scope_id of remote addr */
848721fffe3SKacheong Poon 			sin6.sin6_scope_id = ifindex;
849721fffe3SKacheong Poon 		} else {
850721fffe3SKacheong Poon 			sin6.sin6_scope_id = 0;
851721fffe3SKacheong Poon 		}
852721fffe3SKacheong Poon 		if (connp->conn_recv_ancillary.crb_recvdstaddr) {
853721fffe3SKacheong Poon 			sin6_t	sin6d;
854721fffe3SKacheong Poon 
855721fffe3SKacheong Poon 			sin6d = sin6_null;
856721fffe3SKacheong Poon 			sin6.sin6_addr = connp->conn_laddr_v6;
857721fffe3SKacheong Poon 			sin6d.sin6_port = connp->conn_lport;
858721fffe3SKacheong Poon 			sin6d.sin6_family = AF_INET6;
859721fffe3SKacheong Poon 			if (IN6_IS_ADDR_LINKSCOPE(&connp->conn_laddr_v6))
860721fffe3SKacheong Poon 				sin6d.sin6_scope_id = ifindex;
861721fffe3SKacheong Poon 
862721fffe3SKacheong Poon 			tpi_mp = mi_tpi_extconn_ind(NULL,
863721fffe3SKacheong Poon 			    (char *)&sin6d, sizeof (sin6_t),
864721fffe3SKacheong Poon 			    (char *)&tcp, (t_scalar_t)sizeof (intptr_t),
865721fffe3SKacheong Poon 			    (char *)&sin6d, sizeof (sin6_t),
866721fffe3SKacheong Poon 			    (t_scalar_t)ltcp->tcp_conn_req_seqnum);
867721fffe3SKacheong Poon 		} else {
868721fffe3SKacheong Poon 			tpi_mp = mi_tpi_conn_ind(NULL,
869721fffe3SKacheong Poon 			    (char *)&sin6, sizeof (sin6_t),
870721fffe3SKacheong Poon 			    (char *)&tcp, (t_scalar_t)sizeof (intptr_t),
871721fffe3SKacheong Poon 			    (t_scalar_t)ltcp->tcp_conn_req_seqnum);
872721fffe3SKacheong Poon 		}
873721fffe3SKacheong Poon 	}
874721fffe3SKacheong Poon 
875721fffe3SKacheong Poon 	tcp->tcp_mss = tcps->tcps_mss_def_ipv6;
876721fffe3SKacheong Poon 	return (tpi_mp);
877721fffe3SKacheong Poon }
878721fffe3SKacheong Poon 
879721fffe3SKacheong Poon /* Handle a SYN on an AF_INET socket */
880721fffe3SKacheong Poon static mblk_t *
881721fffe3SKacheong Poon tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, mblk_t *mp,
882721fffe3SKacheong Poon     ip_recv_attr_t *ira)
883721fffe3SKacheong Poon {
884721fffe3SKacheong Poon 	tcp_t 		*ltcp = lconnp->conn_tcp;
885721fffe3SKacheong Poon 	tcp_t		*tcp = connp->conn_tcp;
886721fffe3SKacheong Poon 	sin_t		sin;
887721fffe3SKacheong Poon 	mblk_t		*tpi_mp = NULL;
888721fffe3SKacheong Poon 	tcp_stack_t	*tcps = tcp->tcp_tcps;
889721fffe3SKacheong Poon 	ipha_t		*ipha;
890721fffe3SKacheong Poon 
891721fffe3SKacheong Poon 	ASSERT(ira->ira_flags & IRAF_IS_IPV4);
892721fffe3SKacheong Poon 	ipha = (ipha_t *)mp->b_rptr;
893721fffe3SKacheong Poon 
894721fffe3SKacheong Poon 	connp->conn_ipversion = IPV4_VERSION;
895721fffe3SKacheong Poon 	IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_laddr_v6);
896721fffe3SKacheong Poon 	IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_faddr_v6);
897721fffe3SKacheong Poon 	connp->conn_saddr_v6 = connp->conn_laddr_v6;
898721fffe3SKacheong Poon 
899721fffe3SKacheong Poon 	sin = sin_null;
900721fffe3SKacheong Poon 	sin.sin_addr.s_addr = connp->conn_faddr_v4;
901721fffe3SKacheong Poon 	sin.sin_port = connp->conn_fport;
902721fffe3SKacheong Poon 	sin.sin_family = AF_INET;
903721fffe3SKacheong Poon 	if (lconnp->conn_recv_ancillary.crb_recvdstaddr) {
904721fffe3SKacheong Poon 		sin_t	sind;
905721fffe3SKacheong Poon 
906721fffe3SKacheong Poon 		sind = sin_null;
907721fffe3SKacheong Poon 		sind.sin_addr.s_addr = connp->conn_laddr_v4;
908721fffe3SKacheong Poon 		sind.sin_port = connp->conn_lport;
909721fffe3SKacheong Poon 		sind.sin_family = AF_INET;
910721fffe3SKacheong Poon 		tpi_mp = mi_tpi_extconn_ind(NULL,
911721fffe3SKacheong Poon 		    (char *)&sind, sizeof (sin_t), (char *)&tcp,
912721fffe3SKacheong Poon 		    (t_scalar_t)sizeof (intptr_t), (char *)&sind,
913721fffe3SKacheong Poon 		    sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum);
914721fffe3SKacheong Poon 	} else {
915721fffe3SKacheong Poon 		tpi_mp = mi_tpi_conn_ind(NULL,
916721fffe3SKacheong Poon 		    (char *)&sin, sizeof (sin_t),
917721fffe3SKacheong Poon 		    (char *)&tcp, (t_scalar_t)sizeof (intptr_t),
918721fffe3SKacheong Poon 		    (t_scalar_t)ltcp->tcp_conn_req_seqnum);
919721fffe3SKacheong Poon 	}
920721fffe3SKacheong Poon 
921721fffe3SKacheong Poon 	tcp->tcp_mss = tcps->tcps_mss_def_ipv4;
922721fffe3SKacheong Poon 	return (tpi_mp);
923721fffe3SKacheong Poon }
924721fffe3SKacheong Poon 
925721fffe3SKacheong Poon /*
926721fffe3SKacheong Poon  * Called via squeue to get on to eager's perimeter. It sends a
927721fffe3SKacheong Poon  * TH_RST if eager is in the fanout table. The listener wants the
928721fffe3SKacheong Poon  * eager to disappear either by means of tcp_eager_blowoff() or
929721fffe3SKacheong Poon  * tcp_eager_cleanup() being called. tcp_eager_kill() can also be
930721fffe3SKacheong Poon  * called (via squeue) if the eager cannot be inserted in the
931721fffe3SKacheong Poon  * fanout table in tcp_input_listener().
932721fffe3SKacheong Poon  */
933721fffe3SKacheong Poon /* ARGSUSED */
934721fffe3SKacheong Poon void
935721fffe3SKacheong Poon tcp_eager_kill(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
936721fffe3SKacheong Poon {
937721fffe3SKacheong Poon 	conn_t	*econnp = (conn_t *)arg;
938721fffe3SKacheong Poon 	tcp_t	*eager = econnp->conn_tcp;
939721fffe3SKacheong Poon 	tcp_t	*listener = eager->tcp_listener;
940721fffe3SKacheong Poon 
941721fffe3SKacheong Poon 	/*
942721fffe3SKacheong Poon 	 * We could be called because listener is closing. Since
943721fffe3SKacheong Poon 	 * the eager was using listener's queue's, we avoid
944721fffe3SKacheong Poon 	 * using the listeners queues from now on.
945721fffe3SKacheong Poon 	 */
946721fffe3SKacheong Poon 	ASSERT(eager->tcp_detached);
947721fffe3SKacheong Poon 	econnp->conn_rq = NULL;
948721fffe3SKacheong Poon 	econnp->conn_wq = NULL;
949721fffe3SKacheong Poon 
950721fffe3SKacheong Poon 	/*
951721fffe3SKacheong Poon 	 * An eager's conn_fanout will be NULL if it's a duplicate
952721fffe3SKacheong Poon 	 * for an existing 4-tuples in the conn fanout table.
953721fffe3SKacheong Poon 	 * We don't want to send an RST out in such case.
954721fffe3SKacheong Poon 	 */
955721fffe3SKacheong Poon 	if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) {
956721fffe3SKacheong Poon 		tcp_xmit_ctl("tcp_eager_kill, can't wait",
957721fffe3SKacheong Poon 		    eager, eager->tcp_snxt, 0, TH_RST);
958721fffe3SKacheong Poon 	}
959721fffe3SKacheong Poon 
960721fffe3SKacheong Poon 	/* We are here because listener wants this eager gone */
961721fffe3SKacheong Poon 	if (listener != NULL) {
962721fffe3SKacheong Poon 		mutex_enter(&listener->tcp_eager_lock);
963721fffe3SKacheong Poon 		tcp_eager_unlink(eager);
964721fffe3SKacheong Poon 		if (eager->tcp_tconnind_started) {
965721fffe3SKacheong Poon 			/*
966721fffe3SKacheong Poon 			 * The eager has sent a conn_ind up to the
967721fffe3SKacheong Poon 			 * listener but listener decides to close
968721fffe3SKacheong Poon 			 * instead. We need to drop the extra ref
969721fffe3SKacheong Poon 			 * placed on eager in tcp_input_data() before
970721fffe3SKacheong Poon 			 * sending the conn_ind to listener.
971721fffe3SKacheong Poon 			 */
972721fffe3SKacheong Poon 			CONN_DEC_REF(econnp);
973721fffe3SKacheong Poon 		}
974721fffe3SKacheong Poon 		mutex_exit(&listener->tcp_eager_lock);
975721fffe3SKacheong Poon 		CONN_DEC_REF(listener->tcp_connp);
976721fffe3SKacheong Poon 	}
977721fffe3SKacheong Poon 
978721fffe3SKacheong Poon 	if (eager->tcp_state != TCPS_CLOSED)
979721fffe3SKacheong Poon 		tcp_close_detached(eager);
980721fffe3SKacheong Poon }
981721fffe3SKacheong Poon 
982721fffe3SKacheong Poon /*
983721fffe3SKacheong Poon  * Reset any eager connection hanging off this listener marked
984721fffe3SKacheong Poon  * with 'seqnum' and then reclaim it's resources.
985721fffe3SKacheong Poon  */
986721fffe3SKacheong Poon boolean_t
987721fffe3SKacheong Poon tcp_eager_blowoff(tcp_t	*listener, t_scalar_t seqnum)
988721fffe3SKacheong Poon {
989721fffe3SKacheong Poon 	tcp_t	*eager;
990721fffe3SKacheong Poon 	mblk_t 	*mp;
991721fffe3SKacheong Poon 
992721fffe3SKacheong Poon 	eager = listener;
993721fffe3SKacheong Poon 	mutex_enter(&listener->tcp_eager_lock);
994721fffe3SKacheong Poon 	do {
995721fffe3SKacheong Poon 		eager = eager->tcp_eager_next_q;
996721fffe3SKacheong Poon 		if (eager == NULL) {
997721fffe3SKacheong Poon 			mutex_exit(&listener->tcp_eager_lock);
998721fffe3SKacheong Poon 			return (B_FALSE);
999721fffe3SKacheong Poon 		}
1000721fffe3SKacheong Poon 	} while (eager->tcp_conn_req_seqnum != seqnum);
1001721fffe3SKacheong Poon 
1002721fffe3SKacheong Poon 	if (eager->tcp_closemp_used) {
1003721fffe3SKacheong Poon 		mutex_exit(&listener->tcp_eager_lock);
1004721fffe3SKacheong Poon 		return (B_TRUE);
1005721fffe3SKacheong Poon 	}
1006721fffe3SKacheong Poon 	eager->tcp_closemp_used = B_TRUE;
1007721fffe3SKacheong Poon 	TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15);
1008721fffe3SKacheong Poon 	CONN_INC_REF(eager->tcp_connp);
1009721fffe3SKacheong Poon 	mutex_exit(&listener->tcp_eager_lock);
1010721fffe3SKacheong Poon 	mp = &eager->tcp_closemp;
1011721fffe3SKacheong Poon 	SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill,
1012721fffe3SKacheong Poon 	    eager->tcp_connp, NULL, SQ_FILL, SQTAG_TCP_EAGER_BLOWOFF);
1013721fffe3SKacheong Poon 	return (B_TRUE);
1014721fffe3SKacheong Poon }
1015721fffe3SKacheong Poon 
1016721fffe3SKacheong Poon /*
1017721fffe3SKacheong Poon  * Reset any eager connection hanging off this listener
1018721fffe3SKacheong Poon  * and then reclaim it's resources.
1019721fffe3SKacheong Poon  */
1020721fffe3SKacheong Poon void
1021721fffe3SKacheong Poon tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only)
1022721fffe3SKacheong Poon {
1023721fffe3SKacheong Poon 	tcp_t	*eager;
1024721fffe3SKacheong Poon 	mblk_t	*mp;
1025721fffe3SKacheong Poon 	tcp_stack_t	*tcps = listener->tcp_tcps;
1026721fffe3SKacheong Poon 
1027721fffe3SKacheong Poon 	ASSERT(MUTEX_HELD(&listener->tcp_eager_lock));
1028721fffe3SKacheong Poon 
1029721fffe3SKacheong Poon 	if (!q0_only) {
1030721fffe3SKacheong Poon 		/* First cleanup q */
1031721fffe3SKacheong Poon 		TCP_STAT(tcps, tcp_eager_blowoff_q);
1032721fffe3SKacheong Poon 		eager = listener->tcp_eager_next_q;
1033721fffe3SKacheong Poon 		while (eager != NULL) {
1034721fffe3SKacheong Poon 			if (!eager->tcp_closemp_used) {
1035721fffe3SKacheong Poon 				eager->tcp_closemp_used = B_TRUE;
1036721fffe3SKacheong Poon 				TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15);
1037721fffe3SKacheong Poon 				CONN_INC_REF(eager->tcp_connp);
1038721fffe3SKacheong Poon 				mp = &eager->tcp_closemp;
1039721fffe3SKacheong Poon 				SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp,
1040721fffe3SKacheong Poon 				    tcp_eager_kill, eager->tcp_connp, NULL,
1041721fffe3SKacheong Poon 				    SQ_FILL, SQTAG_TCP_EAGER_CLEANUP);
1042721fffe3SKacheong Poon 			}
1043721fffe3SKacheong Poon 			eager = eager->tcp_eager_next_q;
1044721fffe3SKacheong Poon 		}
1045721fffe3SKacheong Poon 	}
1046721fffe3SKacheong Poon 	/* Then cleanup q0 */
1047721fffe3SKacheong Poon 	TCP_STAT(tcps, tcp_eager_blowoff_q0);
1048721fffe3SKacheong Poon 	eager = listener->tcp_eager_next_q0;
1049721fffe3SKacheong Poon 	while (eager != listener) {
1050721fffe3SKacheong Poon 		if (!eager->tcp_closemp_used) {
1051721fffe3SKacheong Poon 			eager->tcp_closemp_used = B_TRUE;
1052721fffe3SKacheong Poon 			TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15);
1053721fffe3SKacheong Poon 			CONN_INC_REF(eager->tcp_connp);
1054721fffe3SKacheong Poon 			mp = &eager->tcp_closemp;
1055721fffe3SKacheong Poon 			SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp,
1056721fffe3SKacheong Poon 			    tcp_eager_kill, eager->tcp_connp, NULL, SQ_FILL,
1057721fffe3SKacheong Poon 			    SQTAG_TCP_EAGER_CLEANUP_Q0);
1058721fffe3SKacheong Poon 		}
1059721fffe3SKacheong Poon 		eager = eager->tcp_eager_next_q0;
1060721fffe3SKacheong Poon 	}
1061721fffe3SKacheong Poon }
1062721fffe3SKacheong Poon 
1063721fffe3SKacheong Poon /*
1064721fffe3SKacheong Poon  * If we are an eager connection hanging off a listener that hasn't
1065721fffe3SKacheong Poon  * formally accepted the connection yet, get off his list and blow off
1066721fffe3SKacheong Poon  * any data that we have accumulated.
1067721fffe3SKacheong Poon  */
1068721fffe3SKacheong Poon void
1069721fffe3SKacheong Poon tcp_eager_unlink(tcp_t *tcp)
1070721fffe3SKacheong Poon {
1071721fffe3SKacheong Poon 	tcp_t	*listener = tcp->tcp_listener;
1072721fffe3SKacheong Poon 
1073721fffe3SKacheong Poon 	ASSERT(listener != NULL);
1074721fffe3SKacheong Poon 	ASSERT(MUTEX_HELD(&listener->tcp_eager_lock));
1075721fffe3SKacheong Poon 	if (tcp->tcp_eager_next_q0 != NULL) {
1076721fffe3SKacheong Poon 		ASSERT(tcp->tcp_eager_prev_q0 != NULL);
1077721fffe3SKacheong Poon 
1078721fffe3SKacheong Poon 		/* Remove the eager tcp from q0 */
1079721fffe3SKacheong Poon 		tcp->tcp_eager_next_q0->tcp_eager_prev_q0 =
1080721fffe3SKacheong Poon 		    tcp->tcp_eager_prev_q0;
1081721fffe3SKacheong Poon 		tcp->tcp_eager_prev_q0->tcp_eager_next_q0 =
1082721fffe3SKacheong Poon 		    tcp->tcp_eager_next_q0;
1083721fffe3SKacheong Poon 		ASSERT(listener->tcp_conn_req_cnt_q0 > 0);
1084721fffe3SKacheong Poon 		listener->tcp_conn_req_cnt_q0--;
1085721fffe3SKacheong Poon 
1086721fffe3SKacheong Poon 		tcp->tcp_eager_next_q0 = NULL;
1087721fffe3SKacheong Poon 		tcp->tcp_eager_prev_q0 = NULL;
1088721fffe3SKacheong Poon 
1089721fffe3SKacheong Poon 		/*
1090721fffe3SKacheong Poon 		 * Take the eager out, if it is in the list of droppable
1091721fffe3SKacheong Poon 		 * eagers.
1092721fffe3SKacheong Poon 		 */
1093721fffe3SKacheong Poon 		MAKE_UNDROPPABLE(tcp);
1094721fffe3SKacheong Poon 
1095721fffe3SKacheong Poon 		if (tcp->tcp_syn_rcvd_timeout != 0) {
1096721fffe3SKacheong Poon 			/* we have timed out before */
1097721fffe3SKacheong Poon 			ASSERT(listener->tcp_syn_rcvd_timeout > 0);
1098721fffe3SKacheong Poon 			listener->tcp_syn_rcvd_timeout--;
1099721fffe3SKacheong Poon 		}
1100721fffe3SKacheong Poon 	} else {
1101721fffe3SKacheong Poon 		tcp_t   **tcpp = &listener->tcp_eager_next_q;
1102721fffe3SKacheong Poon 		tcp_t	*prev = NULL;
1103721fffe3SKacheong Poon 
1104721fffe3SKacheong Poon 		for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) {
1105721fffe3SKacheong Poon 			if (tcpp[0] == tcp) {
1106721fffe3SKacheong Poon 				if (listener->tcp_eager_last_q == tcp) {
1107721fffe3SKacheong Poon 					/*
1108721fffe3SKacheong Poon 					 * If we are unlinking the last
1109721fffe3SKacheong Poon 					 * element on the list, adjust
1110721fffe3SKacheong Poon 					 * tail pointer. Set tail pointer
1111721fffe3SKacheong Poon 					 * to nil when list is empty.
1112721fffe3SKacheong Poon 					 */
1113721fffe3SKacheong Poon 					ASSERT(tcp->tcp_eager_next_q == NULL);
1114721fffe3SKacheong Poon 					if (listener->tcp_eager_last_q ==
1115721fffe3SKacheong Poon 					    listener->tcp_eager_next_q) {
1116721fffe3SKacheong Poon 						listener->tcp_eager_last_q =
1117721fffe3SKacheong Poon 						    NULL;
1118721fffe3SKacheong Poon 					} else {
1119721fffe3SKacheong Poon 						/*
1120721fffe3SKacheong Poon 						 * We won't get here if there
1121721fffe3SKacheong Poon 						 * is only one eager in the
1122721fffe3SKacheong Poon 						 * list.
1123721fffe3SKacheong Poon 						 */
1124721fffe3SKacheong Poon 						ASSERT(prev != NULL);
1125721fffe3SKacheong Poon 						listener->tcp_eager_last_q =
1126721fffe3SKacheong Poon 						    prev;
1127721fffe3SKacheong Poon 					}
1128721fffe3SKacheong Poon 				}
1129721fffe3SKacheong Poon 				tcpp[0] = tcp->tcp_eager_next_q;
1130721fffe3SKacheong Poon 				tcp->tcp_eager_next_q = NULL;
1131721fffe3SKacheong Poon 				tcp->tcp_eager_last_q = NULL;
1132721fffe3SKacheong Poon 				ASSERT(listener->tcp_conn_req_cnt_q > 0);
1133721fffe3SKacheong Poon 				listener->tcp_conn_req_cnt_q--;
1134721fffe3SKacheong Poon 				break;
1135721fffe3SKacheong Poon 			}
1136721fffe3SKacheong Poon 			prev = tcpp[0];
1137721fffe3SKacheong Poon 		}
1138721fffe3SKacheong Poon 	}
1139721fffe3SKacheong Poon 	tcp->tcp_listener = NULL;
1140721fffe3SKacheong Poon }
1141721fffe3SKacheong Poon 
1142721fffe3SKacheong Poon /* BEGIN CSTYLED */
1143721fffe3SKacheong Poon /*
1144721fffe3SKacheong Poon  *
1145721fffe3SKacheong Poon  * The sockfs ACCEPT path:
1146721fffe3SKacheong Poon  * =======================
1147721fffe3SKacheong Poon  *
1148721fffe3SKacheong Poon  * The eager is now established in its own perimeter as soon as SYN is
1149721fffe3SKacheong Poon  * received in tcp_input_listener(). When sockfs receives conn_ind, it
1150721fffe3SKacheong Poon  * completes the accept processing on the acceptor STREAM. The sending
1151721fffe3SKacheong Poon  * of conn_ind part is common for both sockfs listener and a TLI/XTI
1152721fffe3SKacheong Poon  * listener but a TLI/XTI listener completes the accept processing
1153721fffe3SKacheong Poon  * on the listener perimeter.
1154721fffe3SKacheong Poon  *
1155721fffe3SKacheong Poon  * Common control flow for 3 way handshake:
1156721fffe3SKacheong Poon  * ----------------------------------------
1157721fffe3SKacheong Poon  *
1158721fffe3SKacheong Poon  * incoming SYN (listener perimeter)	-> tcp_input_listener()
1159721fffe3SKacheong Poon  *
1160721fffe3SKacheong Poon  * incoming SYN-ACK-ACK (eager perim) 	-> tcp_input_data()
1161721fffe3SKacheong Poon  * send T_CONN_IND (listener perim)	-> tcp_send_conn_ind()
1162721fffe3SKacheong Poon  *
1163721fffe3SKacheong Poon  * Sockfs ACCEPT Path:
1164721fffe3SKacheong Poon  * -------------------
1165721fffe3SKacheong Poon  *
1166721fffe3SKacheong Poon  * open acceptor stream (tcp_open allocates tcp_tli_accept()
1167721fffe3SKacheong Poon  * as STREAM entry point)
1168721fffe3SKacheong Poon  *
1169721fffe3SKacheong Poon  * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_tli_accept()
1170721fffe3SKacheong Poon  *
1171721fffe3SKacheong Poon  * tcp_tli_accept() extracts the eager and makes the q->q_ptr <-> eager
1172721fffe3SKacheong Poon  * association (we are not behind eager's squeue but sockfs is protecting us
1173721fffe3SKacheong Poon  * and no one knows about this stream yet. The STREAMS entry point q->q_info
1174721fffe3SKacheong Poon  * is changed to point at tcp_wput().
1175721fffe3SKacheong Poon  *
1176721fffe3SKacheong Poon  * tcp_accept_common() sends any deferred eagers via tcp_send_pending() to
1177721fffe3SKacheong Poon  * listener (done on listener's perimeter).
1178721fffe3SKacheong Poon  *
1179721fffe3SKacheong Poon  * tcp_tli_accept() calls tcp_accept_finish() on eagers perimeter to finish
1180721fffe3SKacheong Poon  * accept.
1181721fffe3SKacheong Poon  *
1182721fffe3SKacheong Poon  * TLI/XTI client ACCEPT path:
1183721fffe3SKacheong Poon  * ---------------------------
1184721fffe3SKacheong Poon  *
1185721fffe3SKacheong Poon  * soaccept() sends T_CONN_RES on the listener STREAM.
1186721fffe3SKacheong Poon  *
1187721fffe3SKacheong Poon  * tcp_tli_accept() -> tcp_accept_swap() complete the processing and send
1188721fffe3SKacheong Poon  * a M_SETOPS mblk to eager perimeter to finish accept (tcp_accept_finish()).
1189721fffe3SKacheong Poon  *
1190721fffe3SKacheong Poon  * Locks:
1191721fffe3SKacheong Poon  * ======
1192721fffe3SKacheong Poon  *
1193721fffe3SKacheong Poon  * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and
1194721fffe3SKacheong Poon  * and listeners->tcp_eager_next_q.
1195721fffe3SKacheong Poon  *
1196721fffe3SKacheong Poon  * Referencing:
1197721fffe3SKacheong Poon  * ============
1198721fffe3SKacheong Poon  *
1199721fffe3SKacheong Poon  * 1) We start out in tcp_input_listener by eager placing a ref on
1200721fffe3SKacheong Poon  * listener and listener adding eager to listeners->tcp_eager_next_q0.
1201721fffe3SKacheong Poon  *
1202721fffe3SKacheong Poon  * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before
1203721fffe3SKacheong Poon  * doing so we place a ref on the eager. This ref is finally dropped at the
1204721fffe3SKacheong Poon  * end of tcp_accept_finish() while unwinding from the squeue, i.e. the
1205721fffe3SKacheong Poon  * reference is dropped by the squeue framework.
1206721fffe3SKacheong Poon  *
1207721fffe3SKacheong Poon  * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish
1208721fffe3SKacheong Poon  *
1209721fffe3SKacheong Poon  * The reference must be released by the same entity that added the reference
1210721fffe3SKacheong Poon  * In the above scheme, the eager is the entity that adds and releases the
1211721fffe3SKacheong Poon  * references. Note that tcp_accept_finish executes in the squeue of the eager
1212721fffe3SKacheong Poon  * (albeit after it is attached to the acceptor stream). Though 1. executes
1213721fffe3SKacheong Poon  * in the listener's squeue, the eager is nascent at this point and the
1214721fffe3SKacheong Poon  * reference can be considered to have been added on behalf of the eager.
1215721fffe3SKacheong Poon  *
1216721fffe3SKacheong Poon  * Eager getting a Reset or listener closing:
1217721fffe3SKacheong Poon  * ==========================================
1218721fffe3SKacheong Poon  *
1219721fffe3SKacheong Poon  * Once the listener and eager are linked, the listener never does the unlink.
1220721fffe3SKacheong Poon  * If the listener needs to close, tcp_eager_cleanup() is called which queues
1221721fffe3SKacheong Poon  * a message on all eager perimeter. The eager then does the unlink, clears
1222721fffe3SKacheong Poon  * any pointers to the listener's queue and drops the reference to the
1223721fffe3SKacheong Poon  * listener. The listener waits in tcp_close outside the squeue until its
1224721fffe3SKacheong Poon  * refcount has dropped to 1. This ensures that the listener has waited for
1225721fffe3SKacheong Poon  * all eagers to clear their association with the listener.
1226721fffe3SKacheong Poon  *
1227721fffe3SKacheong Poon  * Similarly, if eager decides to go away, it can unlink itself and close.
1228721fffe3SKacheong Poon  * When the T_CONN_RES comes down, we check if eager has closed. Note that
1229721fffe3SKacheong Poon  * the reference to eager is still valid because of the extra ref we put
1230721fffe3SKacheong Poon  * in tcp_send_conn_ind.
1231721fffe3SKacheong Poon  *
1232721fffe3SKacheong Poon  * Listener can always locate the eager under the protection
1233721fffe3SKacheong Poon  * of the listener->tcp_eager_lock, and then do a refhold
1234721fffe3SKacheong Poon  * on the eager during the accept processing.
1235721fffe3SKacheong Poon  *
1236721fffe3SKacheong Poon  * The acceptor stream accesses the eager in the accept processing
1237721fffe3SKacheong Poon  * based on the ref placed on eager before sending T_conn_ind.
1238721fffe3SKacheong Poon  * The only entity that can negate this refhold is a listener close
1239721fffe3SKacheong Poon  * which is mutually exclusive with an active acceptor stream.
1240721fffe3SKacheong Poon  *
1241721fffe3SKacheong Poon  * Eager's reference on the listener
1242721fffe3SKacheong Poon  * ===================================
1243721fffe3SKacheong Poon  *
1244721fffe3SKacheong Poon  * If the accept happens (even on a closed eager) the eager drops its
1245721fffe3SKacheong Poon  * reference on the listener at the start of tcp_accept_finish. If the
1246721fffe3SKacheong Poon  * eager is killed due to an incoming RST before the T_conn_ind is sent up,
1247721fffe3SKacheong Poon  * the reference is dropped in tcp_closei_local. If the listener closes,
1248721fffe3SKacheong Poon  * the reference is dropped in tcp_eager_kill. In all cases the reference
1249721fffe3SKacheong Poon  * is dropped while executing in the eager's context (squeue).
1250721fffe3SKacheong Poon  */
1251721fffe3SKacheong Poon /* END CSTYLED */
1252721fffe3SKacheong Poon 
1253721fffe3SKacheong Poon /* Process the SYN packet, mp, directed at the listener 'tcp' */
1254721fffe3SKacheong Poon 
1255721fffe3SKacheong Poon /*
1256721fffe3SKacheong Poon  * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN.
1257721fffe3SKacheong Poon  * tcp_input_data will not see any packets for listeners since the listener
1258721fffe3SKacheong Poon  * has conn_recv set to tcp_input_listener.
1259721fffe3SKacheong Poon  */
1260721fffe3SKacheong Poon /* ARGSUSED */
1261721fffe3SKacheong Poon static void
1262721fffe3SKacheong Poon tcp_input_listener(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
1263721fffe3SKacheong Poon {
1264721fffe3SKacheong Poon 	tcpha_t		*tcpha;
1265721fffe3SKacheong Poon 	uint32_t	seg_seq;
1266721fffe3SKacheong Poon 	tcp_t		*eager;
1267721fffe3SKacheong Poon 	int		err;
1268721fffe3SKacheong Poon 	conn_t		*econnp = NULL;
1269721fffe3SKacheong Poon 	squeue_t	*new_sqp;
1270721fffe3SKacheong Poon 	mblk_t		*mp1;
1271721fffe3SKacheong Poon 	uint_t 		ip_hdr_len;
1272721fffe3SKacheong Poon 	conn_t		*lconnp = (conn_t *)arg;
1273721fffe3SKacheong Poon 	tcp_t		*listener = lconnp->conn_tcp;
1274721fffe3SKacheong Poon 	tcp_stack_t	*tcps = listener->tcp_tcps;
1275721fffe3SKacheong Poon 	ip_stack_t	*ipst = tcps->tcps_netstack->netstack_ip;
1276721fffe3SKacheong Poon 	uint_t		flags;
1277721fffe3SKacheong Poon 	mblk_t		*tpi_mp;
1278721fffe3SKacheong Poon 	uint_t		ifindex = ira->ira_ruifindex;
1279721fffe3SKacheong Poon 	boolean_t	tlc_set = B_FALSE;
1280721fffe3SKacheong Poon 
1281721fffe3SKacheong Poon 	ip_hdr_len = ira->ira_ip_hdr_length;
1282721fffe3SKacheong Poon 	tcpha = (tcpha_t *)&mp->b_rptr[ip_hdr_len];
1283721fffe3SKacheong Poon 	flags = (unsigned int)tcpha->tha_flags & 0xFF;
1284721fffe3SKacheong Poon 
12859cd928feSAlan Maguire 	DTRACE_TCP5(receive, mblk_t *, NULL, ip_xmit_attr_t *, lconnp->conn_ixa,
12869cd928feSAlan Maguire 	    __dtrace_tcp_void_ip_t *, mp->b_rptr, tcp_t *, listener,
12879cd928feSAlan Maguire 	    __dtrace_tcp_tcph_t *, tcpha);
12889cd928feSAlan Maguire 
1289721fffe3SKacheong Poon 	if (!(flags & TH_SYN)) {
1290721fffe3SKacheong Poon 		if ((flags & TH_RST) || (flags & TH_URG)) {
1291721fffe3SKacheong Poon 			freemsg(mp);
1292721fffe3SKacheong Poon 			return;
1293721fffe3SKacheong Poon 		}
1294721fffe3SKacheong Poon 		if (flags & TH_ACK) {
1295721fffe3SKacheong Poon 			/* Note this executes in listener's squeue */
1296721fffe3SKacheong Poon 			tcp_xmit_listeners_reset(mp, ira, ipst, lconnp);
1297721fffe3SKacheong Poon 			return;
1298721fffe3SKacheong Poon 		}
1299721fffe3SKacheong Poon 
1300721fffe3SKacheong Poon 		freemsg(mp);
1301721fffe3SKacheong Poon 		return;
1302721fffe3SKacheong Poon 	}
1303721fffe3SKacheong Poon 
1304721fffe3SKacheong Poon 	if (listener->tcp_state != TCPS_LISTEN)
1305721fffe3SKacheong Poon 		goto error2;
1306721fffe3SKacheong Poon 
1307721fffe3SKacheong Poon 	ASSERT(IPCL_IS_BOUND(lconnp));
1308721fffe3SKacheong Poon 
1309721fffe3SKacheong Poon 	mutex_enter(&listener->tcp_eager_lock);
1310721fffe3SKacheong Poon 
1311721fffe3SKacheong Poon 	/*
1312721fffe3SKacheong Poon 	 * The system is under memory pressure, so we need to do our part
1313721fffe3SKacheong Poon 	 * to relieve the pressure.  So we only accept new request if there
1314721fffe3SKacheong Poon 	 * is nothing waiting to be accepted or waiting to complete the 3-way
1315721fffe3SKacheong Poon 	 * handshake.  This means that busy listener will not get too many
1316721fffe3SKacheong Poon 	 * new requests which they cannot handle in time while non-busy
1317721fffe3SKacheong Poon 	 * listener is still functioning properly.
1318721fffe3SKacheong Poon 	 */
1319721fffe3SKacheong Poon 	if (tcps->tcps_reclaim && (listener->tcp_conn_req_cnt_q > 0 ||
1320721fffe3SKacheong Poon 	    listener->tcp_conn_req_cnt_q0 > 0)) {
1321721fffe3SKacheong Poon 		mutex_exit(&listener->tcp_eager_lock);
1322721fffe3SKacheong Poon 		TCP_STAT(tcps, tcp_listen_mem_drop);
1323721fffe3SKacheong Poon 		goto error2;
1324721fffe3SKacheong Poon 	}
1325721fffe3SKacheong Poon 
1326721fffe3SKacheong Poon 	if (listener->tcp_conn_req_cnt_q >= listener->tcp_conn_req_max) {
1327721fffe3SKacheong Poon 		mutex_exit(&listener->tcp_eager_lock);
1328721fffe3SKacheong Poon 		TCP_STAT(tcps, tcp_listendrop);
1329721fffe3SKacheong Poon 		TCPS_BUMP_MIB(tcps, tcpListenDrop);
1330721fffe3SKacheong Poon 		if (lconnp->conn_debug) {
1331721fffe3SKacheong Poon 			(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR,
1332721fffe3SKacheong Poon 			    "tcp_input_listener: listen backlog (max=%d) "
1333721fffe3SKacheong Poon 			    "overflow (%d pending) on %s",
1334721fffe3SKacheong Poon 			    listener->tcp_conn_req_max,
1335721fffe3SKacheong Poon 			    listener->tcp_conn_req_cnt_q,
1336721fffe3SKacheong Poon 			    tcp_display(listener, NULL, DISP_PORT_ONLY));
1337721fffe3SKacheong Poon 		}
1338721fffe3SKacheong Poon 		goto error2;
1339721fffe3SKacheong Poon 	}
1340721fffe3SKacheong Poon 
1341721fffe3SKacheong Poon 	if (listener->tcp_conn_req_cnt_q0 >=
1342721fffe3SKacheong Poon 	    listener->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) {
1343721fffe3SKacheong Poon 		/*
1344721fffe3SKacheong Poon 		 * Q0 is full. Drop a pending half-open req from the queue
1345721fffe3SKacheong Poon 		 * to make room for the new SYN req. Also mark the time we
1346721fffe3SKacheong Poon 		 * drop a SYN.
1347721fffe3SKacheong Poon 		 *
1348721fffe3SKacheong Poon 		 * A more aggressive defense against SYN attack will
1349721fffe3SKacheong Poon 		 * be to set the "tcp_syn_defense" flag now.
1350721fffe3SKacheong Poon 		 */
1351721fffe3SKacheong Poon 		TCP_STAT(tcps, tcp_listendropq0);
1352721fffe3SKacheong Poon 		listener->tcp_last_rcv_lbolt = ddi_get_lbolt64();
1353721fffe3SKacheong Poon 		if (!tcp_drop_q0(listener)) {
1354721fffe3SKacheong Poon 			mutex_exit(&listener->tcp_eager_lock);
1355721fffe3SKacheong Poon 			TCPS_BUMP_MIB(tcps, tcpListenDropQ0);
1356721fffe3SKacheong Poon 			if (lconnp->conn_debug) {
1357721fffe3SKacheong Poon 				(void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE,
1358721fffe3SKacheong Poon 				    "tcp_input_listener: listen half-open "
1359721fffe3SKacheong Poon 				    "queue (max=%d) full (%d pending) on %s",
1360721fffe3SKacheong Poon 				    tcps->tcps_conn_req_max_q0,
1361721fffe3SKacheong Poon 				    listener->tcp_conn_req_cnt_q0,
1362721fffe3SKacheong Poon 				    tcp_display(listener, NULL,
1363721fffe3SKacheong Poon 				    DISP_PORT_ONLY));
1364721fffe3SKacheong Poon 			}
1365721fffe3SKacheong Poon 			goto error2;
1366721fffe3SKacheong Poon 		}
1367721fffe3SKacheong Poon 	}
1368721fffe3SKacheong Poon 
1369721fffe3SKacheong Poon 	/*
1370721fffe3SKacheong Poon 	 * Enforce the limit set on the number of connections per listener.
1371721fffe3SKacheong Poon 	 * Note that tlc_cnt starts with 1.  So need to add 1 to tlc_max
1372721fffe3SKacheong Poon 	 * for comparison.
1373721fffe3SKacheong Poon 	 */
1374721fffe3SKacheong Poon 	if (listener->tcp_listen_cnt != NULL) {
1375721fffe3SKacheong Poon 		tcp_listen_cnt_t *tlc = listener->tcp_listen_cnt;
1376721fffe3SKacheong Poon 		int64_t now;
1377721fffe3SKacheong Poon 
13781a5e258fSJosef 'Jeff' Sipek 		if (atomic_inc_32_nv(&tlc->tlc_cnt) > tlc->tlc_max + 1) {
1379721fffe3SKacheong Poon 			mutex_exit(&listener->tcp_eager_lock);
1380721fffe3SKacheong Poon 			now = ddi_get_lbolt64();
13811a5e258fSJosef 'Jeff' Sipek 			atomic_dec_32(&tlc->tlc_cnt);
1382721fffe3SKacheong Poon 			TCP_STAT(tcps, tcp_listen_cnt_drop);
1383721fffe3SKacheong Poon 			tlc->tlc_drop++;
1384721fffe3SKacheong Poon 			if (now - tlc->tlc_report_time >
1385721fffe3SKacheong Poon 			    MSEC_TO_TICK(TCP_TLC_REPORT_INTERVAL)) {
1386721fffe3SKacheong Poon 				zcmn_err(lconnp->conn_zoneid, CE_WARN,
1387721fffe3SKacheong Poon 				    "Listener (port %d) connection max (%u) "
1388721fffe3SKacheong Poon 				    "reached: %u attempts dropped total\n",
1389721fffe3SKacheong Poon 				    ntohs(listener->tcp_connp->conn_lport),
1390721fffe3SKacheong Poon 				    tlc->tlc_max, tlc->tlc_drop);
1391721fffe3SKacheong Poon 				tlc->tlc_report_time = now;
1392721fffe3SKacheong Poon 			}
1393721fffe3SKacheong Poon 			goto error2;
1394721fffe3SKacheong Poon 		}
1395721fffe3SKacheong Poon 		tlc_set = B_TRUE;
1396721fffe3SKacheong Poon 	}
1397721fffe3SKacheong Poon 
1398721fffe3SKacheong Poon 	mutex_exit(&listener->tcp_eager_lock);
1399721fffe3SKacheong Poon 
1400721fffe3SKacheong Poon 	/*
1401721fffe3SKacheong Poon 	 * IP sets ira_sqp to either the senders conn_sqp (for loopback)
1402721fffe3SKacheong Poon 	 * or based on the ring (for packets from GLD). Otherwise it is
1403721fffe3SKacheong Poon 	 * set based on lbolt i.e., a somewhat random number.
1404721fffe3SKacheong Poon 	 */
1405721fffe3SKacheong Poon 	ASSERT(ira->ira_sqp != NULL);
1406721fffe3SKacheong Poon 	new_sqp = ira->ira_sqp;
1407721fffe3SKacheong Poon 
1408721fffe3SKacheong Poon 	econnp = (conn_t *)tcp_get_conn(arg2, tcps);
1409721fffe3SKacheong Poon 	if (econnp == NULL)
1410721fffe3SKacheong Poon 		goto error2;
1411721fffe3SKacheong Poon 
1412721fffe3SKacheong Poon 	ASSERT(econnp->conn_netstack == lconnp->conn_netstack);
1413721fffe3SKacheong Poon 	econnp->conn_sqp = new_sqp;
1414721fffe3SKacheong Poon 	econnp->conn_initial_sqp = new_sqp;
1415721fffe3SKacheong Poon 	econnp->conn_ixa->ixa_sqp = new_sqp;
1416721fffe3SKacheong Poon 
1417721fffe3SKacheong Poon 	econnp->conn_fport = tcpha->tha_lport;
1418721fffe3SKacheong Poon 	econnp->conn_lport = tcpha->tha_fport;
1419721fffe3SKacheong Poon 
1420721fffe3SKacheong Poon 	err = conn_inherit_parent(lconnp, econnp);
1421721fffe3SKacheong Poon 	if (err != 0)
1422721fffe3SKacheong Poon 		goto error3;
1423721fffe3SKacheong Poon 
1424721fffe3SKacheong Poon 	/* We already know the laddr of the new connection is ours */
1425721fffe3SKacheong Poon 	econnp->conn_ixa->ixa_src_generation = ipst->ips_src_generation;
1426721fffe3SKacheong Poon 
1427721fffe3SKacheong Poon 	ASSERT(OK_32PTR(mp->b_rptr));
1428721fffe3SKacheong Poon 	ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION ||
1429721fffe3SKacheong Poon 	    IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
1430721fffe3SKacheong Poon 
1431721fffe3SKacheong Poon 	if (lconnp->conn_family == AF_INET) {
1432721fffe3SKacheong Poon 		ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
1433721fffe3SKacheong Poon 		tpi_mp = tcp_conn_create_v4(lconnp, econnp, mp, ira);
1434721fffe3SKacheong Poon 	} else {
1435721fffe3SKacheong Poon 		tpi_mp = tcp_conn_create_v6(lconnp, econnp, mp, ira);
1436721fffe3SKacheong Poon 	}
1437721fffe3SKacheong Poon 
1438721fffe3SKacheong Poon 	if (tpi_mp == NULL)
1439721fffe3SKacheong Poon 		goto error3;
1440721fffe3SKacheong Poon 
1441721fffe3SKacheong Poon 	eager = econnp->conn_tcp;
1442721fffe3SKacheong Poon 	eager->tcp_detached = B_TRUE;
1443721fffe3SKacheong Poon 	SOCK_CONNID_INIT(eager->tcp_connid);
1444721fffe3SKacheong Poon 
1445707e74bcSKacheong Poon 	/*
1446707e74bcSKacheong Poon 	 * Initialize the eager's tcp_t and inherit some parameters from
1447707e74bcSKacheong Poon 	 * the listener.
1448707e74bcSKacheong Poon 	 */
1449707e74bcSKacheong Poon 	tcp_init_values(eager, listener);
1450721fffe3SKacheong Poon 
1451721fffe3SKacheong Poon 	ASSERT((econnp->conn_ixa->ixa_flags &
1452721fffe3SKacheong Poon 	    (IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
1453721fffe3SKacheong Poon 	    IXAF_VERIFY_PMTU | IXAF_VERIFY_LSO)) ==
1454721fffe3SKacheong Poon 	    (IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
1455721fffe3SKacheong Poon 	    IXAF_VERIFY_PMTU | IXAF_VERIFY_LSO));
1456721fffe3SKacheong Poon 
1457721fffe3SKacheong Poon 	if (!tcps->tcps_dev_flow_ctl)
1458721fffe3SKacheong Poon 		econnp->conn_ixa->ixa_flags |= IXAF_NO_DEV_FLOW_CTL;
1459721fffe3SKacheong Poon 
1460721fffe3SKacheong Poon 	/* Prepare for diffing against previous packets */
1461721fffe3SKacheong Poon 	eager->tcp_recvifindex = 0;
1462721fffe3SKacheong Poon 	eager->tcp_recvhops = 0xffffffffU;
1463721fffe3SKacheong Poon 
1464721fffe3SKacheong Poon 	if (!(ira->ira_flags & IRAF_IS_IPV4) && econnp->conn_bound_if == 0) {
1465721fffe3SKacheong Poon 		if (IN6_IS_ADDR_LINKSCOPE(&econnp->conn_faddr_v6) ||
1466721fffe3SKacheong Poon 		    IN6_IS_ADDR_LINKSCOPE(&econnp->conn_laddr_v6)) {
1467721fffe3SKacheong Poon 			econnp->conn_incoming_ifindex = ifindex;
1468721fffe3SKacheong Poon 			econnp->conn_ixa->ixa_flags |= IXAF_SCOPEID_SET;
1469721fffe3SKacheong Poon 			econnp->conn_ixa->ixa_scopeid = ifindex;
1470721fffe3SKacheong Poon 		}
1471721fffe3SKacheong Poon 	}
1472721fffe3SKacheong Poon 
1473721fffe3SKacheong Poon 	if ((ira->ira_flags & (IRAF_IS_IPV4|IRAF_IPV4_OPTIONS)) ==
1474721fffe3SKacheong Poon 	    (IRAF_IS_IPV4|IRAF_IPV4_OPTIONS) &&
1475721fffe3SKacheong Poon 	    tcps->tcps_rev_src_routes) {
1476721fffe3SKacheong Poon 		ipha_t *ipha = (ipha_t *)mp->b_rptr;
1477721fffe3SKacheong Poon 		ip_pkt_t *ipp = &econnp->conn_xmit_ipp;
1478721fffe3SKacheong Poon 
1479721fffe3SKacheong Poon 		/* Source routing option copyover (reverse it) */
1480721fffe3SKacheong Poon 		err = ip_find_hdr_v4(ipha, ipp, B_TRUE);
1481721fffe3SKacheong Poon 		if (err != 0) {
1482721fffe3SKacheong Poon 			freemsg(tpi_mp);
1483721fffe3SKacheong Poon 			goto error3;
1484721fffe3SKacheong Poon 		}
1485721fffe3SKacheong Poon 		ip_pkt_source_route_reverse_v4(ipp);
1486721fffe3SKacheong Poon 	}
1487721fffe3SKacheong Poon 
1488721fffe3SKacheong Poon 	ASSERT(eager->tcp_conn.tcp_eager_conn_ind == NULL);
1489721fffe3SKacheong Poon 	ASSERT(!eager->tcp_tconnind_started);
1490721fffe3SKacheong Poon 	/*
1491721fffe3SKacheong Poon 	 * If the SYN came with a credential, it's a loopback packet or a
1492721fffe3SKacheong Poon 	 * labeled packet; attach the credential to the TPI message.
1493721fffe3SKacheong Poon 	 */
1494721fffe3SKacheong Poon 	if (ira->ira_cred != NULL)
1495721fffe3SKacheong Poon 		mblk_setcred(tpi_mp, ira->ira_cred, ira->ira_cpid);
1496721fffe3SKacheong Poon 
1497721fffe3SKacheong Poon 	eager->tcp_conn.tcp_eager_conn_ind = tpi_mp;
14983e95bd4aSAnders Persson 	ASSERT(eager->tcp_ordrel_mp == NULL);
14993e95bd4aSAnders Persson 
1500721fffe3SKacheong Poon 	/* Inherit the listener's non-STREAMS flag */
1501721fffe3SKacheong Poon 	if (IPCL_IS_NONSTR(lconnp)) {
1502721fffe3SKacheong Poon 		econnp->conn_flags |= IPCL_NONSTR;
15033e95bd4aSAnders Persson 		/* All non-STREAMS tcp_ts are sockets */
15043e95bd4aSAnders Persson 		eager->tcp_issocket = B_TRUE;
15053e95bd4aSAnders Persson 	} else {
1506721fffe3SKacheong Poon 		/*
1507721fffe3SKacheong Poon 		 * Pre-allocate the T_ordrel_ind mblk for TPI socket so that
1508721fffe3SKacheong Poon 		 * at close time, we will always have that to send up.
1509721fffe3SKacheong Poon 		 * Otherwise, we need to do special handling in case the
1510721fffe3SKacheong Poon 		 * allocation fails at that time.
1511721fffe3SKacheong Poon 		 */
1512721fffe3SKacheong Poon 		if ((eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL)
1513721fffe3SKacheong Poon 			goto error3;
1514721fffe3SKacheong Poon 	}
1515721fffe3SKacheong Poon 	/*
1516721fffe3SKacheong Poon 	 * Now that the IP addresses and ports are setup in econnp we
1517721fffe3SKacheong Poon 	 * can do the IPsec policy work.
1518721fffe3SKacheong Poon 	 */
1519721fffe3SKacheong Poon 	if (ira->ira_flags & IRAF_IPSEC_SECURE) {
1520721fffe3SKacheong Poon 		if (lconnp->conn_policy != NULL) {
1521721fffe3SKacheong Poon 			/*
1522721fffe3SKacheong Poon 			 * Inherit the policy from the listener; use
1523721fffe3SKacheong Poon 			 * actions from ira
1524721fffe3SKacheong Poon 			 */
1525721fffe3SKacheong Poon 			if (!ip_ipsec_policy_inherit(econnp, lconnp, ira)) {
1526721fffe3SKacheong Poon 				CONN_DEC_REF(econnp);
1527721fffe3SKacheong Poon 				freemsg(mp);
1528721fffe3SKacheong Poon 				goto error3;
1529721fffe3SKacheong Poon 			}
1530721fffe3SKacheong Poon 		}
1531721fffe3SKacheong Poon 	}
1532721fffe3SKacheong Poon 
1533721fffe3SKacheong Poon 	/*
1534721fffe3SKacheong Poon 	 * tcp_set_destination() may set tcp_rwnd according to the route
1535721fffe3SKacheong Poon 	 * metrics. If it does not, the eager's receive window will be set
1536721fffe3SKacheong Poon 	 * to the listener's receive window later in this function.
1537721fffe3SKacheong Poon 	 */
1538721fffe3SKacheong Poon 	eager->tcp_rwnd = 0;
1539721fffe3SKacheong Poon 
1540721fffe3SKacheong Poon 	if (is_system_labeled()) {
1541721fffe3SKacheong Poon 		ip_xmit_attr_t *ixa = econnp->conn_ixa;
1542721fffe3SKacheong Poon 
1543721fffe3SKacheong Poon 		ASSERT(ira->ira_tsl != NULL);
1544721fffe3SKacheong Poon 		/* Discard any old label */
1545721fffe3SKacheong Poon 		if (ixa->ixa_free_flags & IXA_FREE_TSL) {
1546721fffe3SKacheong Poon 			ASSERT(ixa->ixa_tsl != NULL);
1547721fffe3SKacheong Poon 			label_rele(ixa->ixa_tsl);
1548721fffe3SKacheong Poon 			ixa->ixa_free_flags &= ~IXA_FREE_TSL;
1549721fffe3SKacheong Poon 			ixa->ixa_tsl = NULL;
1550721fffe3SKacheong Poon 		}
1551721fffe3SKacheong Poon 		if ((lconnp->conn_mlp_type != mlptSingle ||
1552721fffe3SKacheong Poon 		    lconnp->conn_mac_mode != CONN_MAC_DEFAULT) &&
1553721fffe3SKacheong Poon 		    ira->ira_tsl != NULL) {
1554721fffe3SKacheong Poon 			/*
1555721fffe3SKacheong Poon 			 * If this is an MLP connection or a MAC-Exempt
1556721fffe3SKacheong Poon 			 * connection with an unlabeled node, packets are to be
1557721fffe3SKacheong Poon 			 * exchanged using the security label of the received
1558721fffe3SKacheong Poon 			 * SYN packet instead of the server application's label.
1559721fffe3SKacheong Poon 			 * tsol_check_dest called from ip_set_destination
1560721fffe3SKacheong Poon 			 * might later update TSF_UNLABELED by replacing
1561721fffe3SKacheong Poon 			 * ixa_tsl with a new label.
1562721fffe3SKacheong Poon 			 */
1563721fffe3SKacheong Poon 			label_hold(ira->ira_tsl);
1564721fffe3SKacheong Poon 			ip_xmit_attr_replace_tsl(ixa, ira->ira_tsl);
1565721fffe3SKacheong Poon 			DTRACE_PROBE2(mlp_syn_accept, conn_t *,
1566721fffe3SKacheong Poon 			    econnp, ts_label_t *, ixa->ixa_tsl)
1567721fffe3SKacheong Poon 		} else {
1568721fffe3SKacheong Poon 			ixa->ixa_tsl = crgetlabel(econnp->conn_cred);
1569721fffe3SKacheong Poon 			DTRACE_PROBE2(syn_accept, conn_t *,
1570721fffe3SKacheong Poon 			    econnp, ts_label_t *, ixa->ixa_tsl)
1571721fffe3SKacheong Poon 		}
1572721fffe3SKacheong Poon 		/*
1573721fffe3SKacheong Poon 		 * conn_connect() called from tcp_set_destination will verify
1574721fffe3SKacheong Poon 		 * the destination is allowed to receive packets at the
1575721fffe3SKacheong Poon 		 * security label of the SYN-ACK we are generating. As part of
1576721fffe3SKacheong Poon 		 * that, tsol_check_dest() may create a new effective label for
1577721fffe3SKacheong Poon 		 * this connection.
1578721fffe3SKacheong Poon 		 * Finally conn_connect() will call conn_update_label.
1579721fffe3SKacheong Poon 		 * All that remains for TCP to do is to call
1580721fffe3SKacheong Poon 		 * conn_build_hdr_template which is done as part of
1581721fffe3SKacheong Poon 		 * tcp_set_destination.
1582721fffe3SKacheong Poon 		 */
1583721fffe3SKacheong Poon 	}
1584721fffe3SKacheong Poon 
1585721fffe3SKacheong Poon 	/*
1586721fffe3SKacheong Poon 	 * Since we will clear tcp_listener before we clear tcp_detached
1587721fffe3SKacheong Poon 	 * in the accept code we need tcp_hard_binding aka tcp_accept_inprogress
15883e95bd4aSAnders Persson 	 * so we can tell a TCP_IS_DETACHED_NONEAGER apart.
1589721fffe3SKacheong Poon 	 */
1590721fffe3SKacheong Poon 	eager->tcp_hard_binding = B_TRUE;
1591721fffe3SKacheong Poon 
1592721fffe3SKacheong Poon 	tcp_bind_hash_insert(&tcps->tcps_bind_fanout[
1593721fffe3SKacheong Poon 	    TCP_BIND_HASH(econnp->conn_lport)], eager, 0);
1594721fffe3SKacheong Poon 
1595721fffe3SKacheong Poon 	CL_INET_CONNECT(econnp, B_FALSE, err);
1596721fffe3SKacheong Poon 	if (err != 0) {
1597721fffe3SKacheong Poon 		tcp_bind_hash_remove(eager);
1598721fffe3SKacheong Poon 		goto error3;
1599721fffe3SKacheong Poon 	}
1600721fffe3SKacheong Poon 
1601721fffe3SKacheong Poon 	SOCK_CONNID_BUMP(eager->tcp_connid);
1602721fffe3SKacheong Poon 
1603721fffe3SKacheong Poon 	/*
1604721fffe3SKacheong Poon 	 * Adapt our mss, ttl, ... based on the remote address.
1605721fffe3SKacheong Poon 	 */
1606721fffe3SKacheong Poon 
1607721fffe3SKacheong Poon 	if (tcp_set_destination(eager) != 0) {
1608721fffe3SKacheong Poon 		TCPS_BUMP_MIB(tcps, tcpAttemptFails);
1609721fffe3SKacheong Poon 		/* Undo the bind_hash_insert */
1610721fffe3SKacheong Poon 		tcp_bind_hash_remove(eager);
1611721fffe3SKacheong Poon 		goto error3;
1612721fffe3SKacheong Poon 	}
1613721fffe3SKacheong Poon 
1614721fffe3SKacheong Poon 	/* Process all TCP options. */
1615721fffe3SKacheong Poon 	tcp_process_options(eager, tcpha);
1616721fffe3SKacheong Poon 
1617721fffe3SKacheong Poon 	/* Is the other end ECN capable? */
1618721fffe3SKacheong Poon 	if (tcps->tcps_ecn_permitted >= 1 &&
1619721fffe3SKacheong Poon 	    (tcpha->tha_flags & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) {
1620721fffe3SKacheong Poon 		eager->tcp_ecn_ok = B_TRUE;
1621721fffe3SKacheong Poon 	}
1622721fffe3SKacheong Poon 
1623721fffe3SKacheong Poon 	/*
1624721fffe3SKacheong Poon 	 * The listener's conn_rcvbuf should be the default window size or a
1625721fffe3SKacheong Poon 	 * window size changed via SO_RCVBUF option. First round up the
1626721fffe3SKacheong Poon 	 * eager's tcp_rwnd to the nearest MSS. Then find out the window
1627721fffe3SKacheong Poon 	 * scale option value if needed. Call tcp_rwnd_set() to finish the
1628721fffe3SKacheong Poon 	 * setting.
1629721fffe3SKacheong Poon 	 *
1630721fffe3SKacheong Poon 	 * Note if there is a rpipe metric associated with the remote host,
1631721fffe3SKacheong Poon 	 * we should not inherit receive window size from listener.
1632721fffe3SKacheong Poon 	 */
1633721fffe3SKacheong Poon 	eager->tcp_rwnd = MSS_ROUNDUP(
1634721fffe3SKacheong Poon 	    (eager->tcp_rwnd == 0 ? econnp->conn_rcvbuf :
1635721fffe3SKacheong Poon 	    eager->tcp_rwnd), eager->tcp_mss);
1636721fffe3SKacheong Poon 	if (eager->tcp_snd_ws_ok)
1637721fffe3SKacheong Poon 		tcp_set_ws_value(eager);
1638721fffe3SKacheong Poon 	/*
1639721fffe3SKacheong Poon 	 * Note that this is the only place tcp_rwnd_set() is called for
1640721fffe3SKacheong Poon 	 * accepting a connection.  We need to call it here instead of
1641721fffe3SKacheong Poon 	 * after the 3-way handshake because we need to tell the other
1642721fffe3SKacheong Poon 	 * side our rwnd in the SYN-ACK segment.
1643721fffe3SKacheong Poon 	 */
1644721fffe3SKacheong Poon 	(void) tcp_rwnd_set(eager, eager->tcp_rwnd);
1645721fffe3SKacheong Poon 
1646721fffe3SKacheong Poon 	ASSERT(eager->tcp_connp->conn_rcvbuf != 0 &&
1647721fffe3SKacheong Poon 	    eager->tcp_connp->conn_rcvbuf == eager->tcp_rwnd);
1648721fffe3SKacheong Poon 
1649721fffe3SKacheong Poon 	ASSERT(econnp->conn_rcvbuf != 0 &&
1650721fffe3SKacheong Poon 	    econnp->conn_rcvbuf == eager->tcp_rwnd);
1651721fffe3SKacheong Poon 
1652721fffe3SKacheong Poon 	/* Put a ref on the listener for the eager. */
1653721fffe3SKacheong Poon 	CONN_INC_REF(lconnp);
1654721fffe3SKacheong Poon 	mutex_enter(&listener->tcp_eager_lock);
1655721fffe3SKacheong Poon 	listener->tcp_eager_next_q0->tcp_eager_prev_q0 = eager;
1656721fffe3SKacheong Poon 	eager->tcp_eager_next_q0 = listener->tcp_eager_next_q0;
1657721fffe3SKacheong Poon 	listener->tcp_eager_next_q0 = eager;
1658721fffe3SKacheong Poon 	eager->tcp_eager_prev_q0 = listener;
1659721fffe3SKacheong Poon 
1660721fffe3SKacheong Poon 	/* Set tcp_listener before adding it to tcp_conn_fanout */
1661721fffe3SKacheong Poon 	eager->tcp_listener = listener;
1662721fffe3SKacheong Poon 	eager->tcp_saved_listener = listener;
1663721fffe3SKacheong Poon 
1664721fffe3SKacheong Poon 	/*
1665721fffe3SKacheong Poon 	 * Set tcp_listen_cnt so that when the connection is done, the counter
1666721fffe3SKacheong Poon 	 * is decremented.
1667721fffe3SKacheong Poon 	 */
1668721fffe3SKacheong Poon 	eager->tcp_listen_cnt = listener->tcp_listen_cnt;
1669721fffe3SKacheong Poon 
1670721fffe3SKacheong Poon 	/*
1671721fffe3SKacheong Poon 	 * Tag this detached tcp vector for later retrieval
1672721fffe3SKacheong Poon 	 * by our listener client in tcp_accept().
1673721fffe3SKacheong Poon 	 */
1674721fffe3SKacheong Poon 	eager->tcp_conn_req_seqnum = listener->tcp_conn_req_seqnum;
1675721fffe3SKacheong Poon 	listener->tcp_conn_req_cnt_q0++;
1676721fffe3SKacheong Poon 	if (++listener->tcp_conn_req_seqnum == -1) {
1677721fffe3SKacheong Poon 		/*
1678721fffe3SKacheong Poon 		 * -1 is "special" and defined in TPI as something
1679721fffe3SKacheong Poon 		 * that should never be used in T_CONN_IND
1680721fffe3SKacheong Poon 		 */
1681721fffe3SKacheong Poon 		++listener->tcp_conn_req_seqnum;
1682721fffe3SKacheong Poon 	}
1683721fffe3SKacheong Poon 	mutex_exit(&listener->tcp_eager_lock);
1684721fffe3SKacheong Poon 
1685721fffe3SKacheong Poon 	if (listener->tcp_syn_defense) {
1686721fffe3SKacheong Poon 		/* Don't drop the SYN that comes from a good IP source */
1687721fffe3SKacheong Poon 		ipaddr_t *addr_cache;
1688721fffe3SKacheong Poon 
1689721fffe3SKacheong Poon 		addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache);
1690721fffe3SKacheong Poon 		if (addr_cache != NULL && econnp->conn_faddr_v4 ==
1691721fffe3SKacheong Poon 		    addr_cache[IP_ADDR_CACHE_HASH(econnp->conn_faddr_v4)]) {
1692721fffe3SKacheong Poon 			eager->tcp_dontdrop = B_TRUE;
1693721fffe3SKacheong Poon 		}
1694721fffe3SKacheong Poon 	}
1695721fffe3SKacheong Poon 
1696721fffe3SKacheong Poon 	/*
1697721fffe3SKacheong Poon 	 * We need to insert the eager in its own perimeter but as soon
1698721fffe3SKacheong Poon 	 * as we do that, we expose the eager to the classifier and
1699721fffe3SKacheong Poon 	 * should not touch any field outside the eager's perimeter.
1700721fffe3SKacheong Poon 	 * So do all the work necessary before inserting the eager
1701721fffe3SKacheong Poon 	 * in its own perimeter. Be optimistic that conn_connect()
1702721fffe3SKacheong Poon 	 * will succeed but undo everything if it fails.
1703721fffe3SKacheong Poon 	 */
1704721fffe3SKacheong Poon 	seg_seq = ntohl(tcpha->tha_seq);
1705721fffe3SKacheong Poon 	eager->tcp_irs = seg_seq;
1706721fffe3SKacheong Poon 	eager->tcp_rack = seg_seq;
1707721fffe3SKacheong Poon 	eager->tcp_rnxt = seg_seq + 1;
1708721fffe3SKacheong Poon 	eager->tcp_tcpha->tha_ack = htonl(eager->tcp_rnxt);
1709721fffe3SKacheong Poon 	TCPS_BUMP_MIB(tcps, tcpPassiveOpens);
1710721fffe3SKacheong Poon 	eager->tcp_state = TCPS_SYN_RCVD;
17119cd928feSAlan Maguire 	DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
17129cd928feSAlan Maguire 	    econnp->conn_ixa, void, NULL, tcp_t *, eager, void, NULL,
17139cd928feSAlan Maguire 	    int32_t, TCPS_LISTEN);
17149cd928feSAlan Maguire 
1715721fffe3SKacheong Poon 	mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss,
1716721fffe3SKacheong Poon 	    NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE);
1717721fffe3SKacheong Poon 	if (mp1 == NULL) {
1718721fffe3SKacheong Poon 		/*
1719721fffe3SKacheong Poon 		 * Increment the ref count as we are going to
1720721fffe3SKacheong Poon 		 * enqueueing an mp in squeue
1721721fffe3SKacheong Poon 		 */
1722721fffe3SKacheong Poon 		CONN_INC_REF(econnp);
1723721fffe3SKacheong Poon 		goto error;
1724721fffe3SKacheong Poon 	}
1725721fffe3SKacheong Poon 
1726721fffe3SKacheong Poon 	/*
1727721fffe3SKacheong Poon 	 * We need to start the rto timer. In normal case, we start
1728721fffe3SKacheong Poon 	 * the timer after sending the packet on the wire (or at
1729721fffe3SKacheong Poon 	 * least believing that packet was sent by waiting for
1730721fffe3SKacheong Poon 	 * conn_ip_output() to return). Since this is the first packet
1731721fffe3SKacheong Poon 	 * being sent on the wire for the eager, our initial tcp_rto
1732721fffe3SKacheong Poon 	 * is at least tcp_rexmit_interval_min which is a fairly
1733721fffe3SKacheong Poon 	 * large value to allow the algorithm to adjust slowly to large
1734721fffe3SKacheong Poon 	 * fluctuations of RTT during first few transmissions.
1735721fffe3SKacheong Poon 	 *
1736721fffe3SKacheong Poon 	 * Starting the timer first and then sending the packet in this
1737721fffe3SKacheong Poon 	 * case shouldn't make much difference since tcp_rexmit_interval_min
1738721fffe3SKacheong Poon 	 * is of the order of several 100ms and starting the timer
1739721fffe3SKacheong Poon 	 * first and then sending the packet will result in difference
1740721fffe3SKacheong Poon 	 * of few micro seconds.
1741721fffe3SKacheong Poon 	 *
1742721fffe3SKacheong Poon 	 * Without this optimization, we are forced to hold the fanout
1743721fffe3SKacheong Poon 	 * lock across the ipcl_bind_insert() and sending the packet
1744721fffe3SKacheong Poon 	 * so that we don't race against an incoming packet (maybe RST)
1745721fffe3SKacheong Poon 	 * for this eager.
1746721fffe3SKacheong Poon 	 *
1747721fffe3SKacheong Poon 	 * It is necessary to acquire an extra reference on the eager
1748721fffe3SKacheong Poon 	 * at this point and hold it until after tcp_send_data() to
1749721fffe3SKacheong Poon 	 * ensure against an eager close race.
1750721fffe3SKacheong Poon 	 */
1751721fffe3SKacheong Poon 
1752721fffe3SKacheong Poon 	CONN_INC_REF(econnp);
1753721fffe3SKacheong Poon 
1754721fffe3SKacheong Poon 	TCP_TIMER_RESTART(eager, eager->tcp_rto);
1755721fffe3SKacheong Poon 
1756721fffe3SKacheong Poon 	/*
1757721fffe3SKacheong Poon 	 * Insert the eager in its own perimeter now. We are ready to deal
1758721fffe3SKacheong Poon 	 * with any packets on eager.
1759721fffe3SKacheong Poon 	 */
1760721fffe3SKacheong Poon 	if (ipcl_conn_insert(econnp) != 0)
1761721fffe3SKacheong Poon 		goto error;
1762721fffe3SKacheong Poon 
1763721fffe3SKacheong Poon 	ASSERT(econnp->conn_ixa->ixa_notify_cookie == econnp->conn_tcp);
1764721fffe3SKacheong Poon 	freemsg(mp);
1765721fffe3SKacheong Poon 	/*
1766721fffe3SKacheong Poon 	 * Send the SYN-ACK. Use the right squeue so that conn_ixa is
1767721fffe3SKacheong Poon 	 * only used by one thread at a time.
1768721fffe3SKacheong Poon 	 */
1769721fffe3SKacheong Poon 	if (econnp->conn_sqp == lconnp->conn_sqp) {
17709cd928feSAlan Maguire 		DTRACE_TCP5(send, mblk_t *, NULL, ip_xmit_attr_t *,
17719cd928feSAlan Maguire 		    econnp->conn_ixa, __dtrace_tcp_void_ip_t *, mp1->b_rptr,
17729cd928feSAlan Maguire 		    tcp_t *, eager, __dtrace_tcp_tcph_t *,
17739cd928feSAlan Maguire 		    &mp1->b_rptr[econnp->conn_ixa->ixa_ip_hdr_length]);
1774721fffe3SKacheong Poon 		(void) conn_ip_output(mp1, econnp->conn_ixa);
1775721fffe3SKacheong Poon 		CONN_DEC_REF(econnp);
1776721fffe3SKacheong Poon 	} else {
1777721fffe3SKacheong Poon 		SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_send_synack,
1778721fffe3SKacheong Poon 		    econnp, NULL, SQ_PROCESS, SQTAG_TCP_SEND_SYNACK);
1779721fffe3SKacheong Poon 	}
1780721fffe3SKacheong Poon 	return;
1781721fffe3SKacheong Poon error:
1782721fffe3SKacheong Poon 	freemsg(mp1);
1783721fffe3SKacheong Poon 	eager->tcp_closemp_used = B_TRUE;
1784721fffe3SKacheong Poon 	TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15);
1785721fffe3SKacheong Poon 	mp1 = &eager->tcp_closemp;
1786721fffe3SKacheong Poon 	SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_eager_kill,
1787721fffe3SKacheong Poon 	    econnp, NULL, SQ_FILL, SQTAG_TCP_CONN_REQ_2);
1788721fffe3SKacheong Poon 
1789721fffe3SKacheong Poon 	/*
1790721fffe3SKacheong Poon 	 * If a connection already exists, send the mp to that connections so
1791721fffe3SKacheong Poon 	 * that it can be appropriately dealt with.
1792721fffe3SKacheong Poon 	 */
1793721fffe3SKacheong Poon 	ipst = tcps->tcps_netstack->netstack_ip;
1794721fffe3SKacheong Poon 
1795721fffe3SKacheong Poon 	if ((econnp = ipcl_classify(mp, ira, ipst)) != NULL) {
1796721fffe3SKacheong Poon 		if (!IPCL_IS_CONNECTED(econnp)) {
1797721fffe3SKacheong Poon 			/*
1798721fffe3SKacheong Poon 			 * Something bad happened. ipcl_conn_insert()
1799721fffe3SKacheong Poon 			 * failed because a connection already existed
1800721fffe3SKacheong Poon 			 * in connected hash but we can't find it
1801721fffe3SKacheong Poon 			 * anymore (someone blew it away). Just
1802721fffe3SKacheong Poon 			 * free this message and hopefully remote
1803721fffe3SKacheong Poon 			 * will retransmit at which time the SYN can be
1804721fffe3SKacheong Poon 			 * treated as a new connection or dealth with
1805721fffe3SKacheong Poon 			 * a TH_RST if a connection already exists.
1806721fffe3SKacheong Poon 			 */
1807721fffe3SKacheong Poon 			CONN_DEC_REF(econnp);
1808721fffe3SKacheong Poon 			freemsg(mp);
1809721fffe3SKacheong Poon 		} else {
1810721fffe3SKacheong Poon 			SQUEUE_ENTER_ONE(econnp->conn_sqp, mp, tcp_input_data,
1811721fffe3SKacheong Poon 			    econnp, ira, SQ_FILL, SQTAG_TCP_CONN_REQ_1);
1812721fffe3SKacheong Poon 		}
1813721fffe3SKacheong Poon 	} else {
1814721fffe3SKacheong Poon 		/* Nobody wants this packet */
1815721fffe3SKacheong Poon 		freemsg(mp);
1816721fffe3SKacheong Poon 	}
1817721fffe3SKacheong Poon 	return;
1818721fffe3SKacheong Poon error3:
1819721fffe3SKacheong Poon 	CONN_DEC_REF(econnp);
1820721fffe3SKacheong Poon error2:
1821721fffe3SKacheong Poon 	freemsg(mp);
1822721fffe3SKacheong Poon 	if (tlc_set)
18231a5e258fSJosef 'Jeff' Sipek 		atomic_dec_32(&listener->tcp_listen_cnt->tlc_cnt);
1824721fffe3SKacheong Poon }
1825721fffe3SKacheong Poon 
1826721fffe3SKacheong Poon /*
1827721fffe3SKacheong Poon  * In an ideal case of vertical partition in NUMA architecture, its
1828721fffe3SKacheong Poon  * beneficial to have the listener and all the incoming connections
1829721fffe3SKacheong Poon  * tied to the same squeue. The other constraint is that incoming
1830721fffe3SKacheong Poon  * connections should be tied to the squeue attached to interrupted
1831721fffe3SKacheong Poon  * CPU for obvious locality reason so this leaves the listener to
1832721fffe3SKacheong Poon  * be tied to the same squeue. Our only problem is that when listener
1833721fffe3SKacheong Poon  * is binding, the CPU that will get interrupted by the NIC whose
1834721fffe3SKacheong Poon  * IP address the listener is binding to is not even known. So
1835721fffe3SKacheong Poon  * the code below allows us to change that binding at the time the
1836721fffe3SKacheong Poon  * CPU is interrupted by virtue of incoming connection's squeue.
1837721fffe3SKacheong Poon  *
1838721fffe3SKacheong Poon  * This is usefull only in case of a listener bound to a specific IP
1839721fffe3SKacheong Poon  * address. For other kind of listeners, they get bound the
1840721fffe3SKacheong Poon  * very first time and there is no attempt to rebind them.
1841721fffe3SKacheong Poon  */
1842721fffe3SKacheong Poon void
1843721fffe3SKacheong Poon tcp_input_listener_unbound(void *arg, mblk_t *mp, void *arg2,
1844721fffe3SKacheong Poon     ip_recv_attr_t *ira)
1845721fffe3SKacheong Poon {
1846721fffe3SKacheong Poon 	conn_t		*connp = (conn_t *)arg;
1847721fffe3SKacheong Poon 	squeue_t	*sqp = (squeue_t *)arg2;
1848721fffe3SKacheong Poon 	squeue_t	*new_sqp;
1849721fffe3SKacheong Poon 	uint32_t	conn_flags;
1850721fffe3SKacheong Poon 
1851721fffe3SKacheong Poon 	/*
1852721fffe3SKacheong Poon 	 * IP sets ira_sqp to either the senders conn_sqp (for loopback)
1853721fffe3SKacheong Poon 	 * or based on the ring (for packets from GLD). Otherwise it is
1854721fffe3SKacheong Poon 	 * set based on lbolt i.e., a somewhat random number.
1855721fffe3SKacheong Poon 	 */
1856721fffe3SKacheong Poon 	ASSERT(ira->ira_sqp != NULL);
1857721fffe3SKacheong Poon 	new_sqp = ira->ira_sqp;
1858721fffe3SKacheong Poon 
1859721fffe3SKacheong Poon 	if (connp->conn_fanout == NULL)
1860721fffe3SKacheong Poon 		goto done;
1861721fffe3SKacheong Poon 
1862721fffe3SKacheong Poon 	if (!(connp->conn_flags & IPCL_FULLY_BOUND)) {
1863721fffe3SKacheong Poon 		mutex_enter(&connp->conn_fanout->connf_lock);
1864721fffe3SKacheong Poon 		mutex_enter(&connp->conn_lock);
1865721fffe3SKacheong Poon 		/*
1866721fffe3SKacheong Poon 		 * No one from read or write side can access us now
1867721fffe3SKacheong Poon 		 * except for already queued packets on this squeue.
1868721fffe3SKacheong Poon 		 * But since we haven't changed the squeue yet, they
1869721fffe3SKacheong Poon 		 * can't execute. If they are processed after we have
1870721fffe3SKacheong Poon 		 * changed the squeue, they are sent back to the
1871721fffe3SKacheong Poon 		 * correct squeue down below.
1872721fffe3SKacheong Poon 		 * But a listner close can race with processing of
1873721fffe3SKacheong Poon 		 * incoming SYN. If incoming SYN processing changes
1874721fffe3SKacheong Poon 		 * the squeue then the listener close which is waiting
1875721fffe3SKacheong Poon 		 * to enter the squeue would operate on the wrong
1876721fffe3SKacheong Poon 		 * squeue. Hence we don't change the squeue here unless
1877721fffe3SKacheong Poon 		 * the refcount is exactly the minimum refcount. The
1878721fffe3SKacheong Poon 		 * minimum refcount of 4 is counted as - 1 each for
1879721fffe3SKacheong Poon 		 * TCP and IP, 1 for being in the classifier hash, and
1880721fffe3SKacheong Poon 		 * 1 for the mblk being processed.
1881721fffe3SKacheong Poon 		 */
1882721fffe3SKacheong Poon 
1883721fffe3SKacheong Poon 		if (connp->conn_ref != 4 ||
1884721fffe3SKacheong Poon 		    connp->conn_tcp->tcp_state != TCPS_LISTEN) {
1885721fffe3SKacheong Poon 			mutex_exit(&connp->conn_lock);
1886721fffe3SKacheong Poon 			mutex_exit(&connp->conn_fanout->connf_lock);
1887721fffe3SKacheong Poon 			goto done;
1888721fffe3SKacheong Poon 		}
1889721fffe3SKacheong Poon 		if (connp->conn_sqp != new_sqp) {
1890721fffe3SKacheong Poon 			while (connp->conn_sqp != new_sqp)
189175d94465SJosef 'Jeff' Sipek 				(void) atomic_cas_ptr(&connp->conn_sqp, sqp,
189275d94465SJosef 'Jeff' Sipek 				    new_sqp);
1893721fffe3SKacheong Poon 			/* No special MT issues for outbound ixa_sqp hint */
1894721fffe3SKacheong Poon 			connp->conn_ixa->ixa_sqp = new_sqp;
1895721fffe3SKacheong Poon 		}
1896721fffe3SKacheong Poon 
1897721fffe3SKacheong Poon 		do {
1898721fffe3SKacheong Poon 			conn_flags = connp->conn_flags;
1899721fffe3SKacheong Poon 			conn_flags |= IPCL_FULLY_BOUND;
190075d94465SJosef 'Jeff' Sipek 			(void) atomic_cas_32(&connp->conn_flags,
190175d94465SJosef 'Jeff' Sipek 			    connp->conn_flags, conn_flags);
1902721fffe3SKacheong Poon 		} while (!(connp->conn_flags & IPCL_FULLY_BOUND));
1903721fffe3SKacheong Poon 
1904721fffe3SKacheong Poon 		mutex_exit(&connp->conn_fanout->connf_lock);
1905721fffe3SKacheong Poon 		mutex_exit(&connp->conn_lock);
1906721fffe3SKacheong Poon 
1907721fffe3SKacheong Poon 		/*
1908721fffe3SKacheong Poon 		 * Assume we have picked a good squeue for the listener. Make
1909721fffe3SKacheong Poon 		 * subsequent SYNs not try to change the squeue.
1910721fffe3SKacheong Poon 		 */
1911721fffe3SKacheong Poon 		connp->conn_recv = tcp_input_listener;
1912721fffe3SKacheong Poon 	}
1913721fffe3SKacheong Poon 
1914721fffe3SKacheong Poon done:
1915721fffe3SKacheong Poon 	if (connp->conn_sqp != sqp) {
1916721fffe3SKacheong Poon 		CONN_INC_REF(connp);
1917721fffe3SKacheong Poon 		SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp,
1918721fffe3SKacheong Poon 		    ira, SQ_FILL, SQTAG_TCP_CONN_REQ_UNBOUND);
1919721fffe3SKacheong Poon 	} else {
1920721fffe3SKacheong Poon 		tcp_input_listener(connp, mp, sqp, ira);
1921721fffe3SKacheong Poon 	}
1922721fffe3SKacheong Poon }
1923721fffe3SKacheong Poon 
1924721fffe3SKacheong Poon /*
1925721fffe3SKacheong Poon  * Send up all messages queued on tcp_rcv_list.
1926721fffe3SKacheong Poon  */
1927721fffe3SKacheong Poon uint_t
1928721fffe3SKacheong Poon tcp_rcv_drain(tcp_t *tcp)
1929721fffe3SKacheong Poon {
1930721fffe3SKacheong Poon 	mblk_t *mp;
1931721fffe3SKacheong Poon 	uint_t ret = 0;
1932721fffe3SKacheong Poon #ifdef DEBUG
1933721fffe3SKacheong Poon 	uint_t cnt = 0;
1934721fffe3SKacheong Poon #endif
1935721fffe3SKacheong Poon 	queue_t	*q = tcp->tcp_connp->conn_rq;
1936721fffe3SKacheong Poon 
1937721fffe3SKacheong Poon 	/* Can't drain on an eager connection */
1938721fffe3SKacheong Poon 	if (tcp->tcp_listener != NULL)
1939721fffe3SKacheong Poon 		return (ret);
1940721fffe3SKacheong Poon 
1941721fffe3SKacheong Poon 	/* Can't be a non-STREAMS connection */
1942721fffe3SKacheong Poon 	ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1943721fffe3SKacheong Poon 
1944721fffe3SKacheong Poon 	/* No need for the push timer now. */
1945721fffe3SKacheong Poon 	if (tcp->tcp_push_tid != 0) {
1946721fffe3SKacheong Poon 		(void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid);
1947721fffe3SKacheong Poon 		tcp->tcp_push_tid = 0;
1948721fffe3SKacheong Poon 	}
1949721fffe3SKacheong Poon 
1950721fffe3SKacheong Poon 	/*
1951721fffe3SKacheong Poon 	 * Handle two cases here: we are currently fused or we were
1952721fffe3SKacheong Poon 	 * previously fused and have some urgent data to be delivered
1953721fffe3SKacheong Poon 	 * upstream.  The latter happens because we either ran out of
1954721fffe3SKacheong Poon 	 * memory or were detached and therefore sending the SIGURG was
1955721fffe3SKacheong Poon 	 * deferred until this point.  In either case we pass control
1956721fffe3SKacheong Poon 	 * over to tcp_fuse_rcv_drain() since it may need to complete
1957721fffe3SKacheong Poon 	 * some work.
1958721fffe3SKacheong Poon 	 */
1959721fffe3SKacheong Poon 	if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) {
1960721fffe3SKacheong Poon 		if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL :
1961721fffe3SKacheong Poon 		    &tcp->tcp_fused_sigurg_mp))
1962721fffe3SKacheong Poon 			return (ret);
1963721fffe3SKacheong Poon 	}
1964721fffe3SKacheong Poon 
1965721fffe3SKacheong Poon 	while ((mp = tcp->tcp_rcv_list) != NULL) {
1966721fffe3SKacheong Poon 		tcp->tcp_rcv_list = mp->b_next;
1967721fffe3SKacheong Poon 		mp->b_next = NULL;
1968721fffe3SKacheong Poon #ifdef DEBUG
1969721fffe3SKacheong Poon 		cnt += msgdsize(mp);
1970721fffe3SKacheong Poon #endif
1971721fffe3SKacheong Poon 		putnext(q, mp);
1972721fffe3SKacheong Poon 	}
1973721fffe3SKacheong Poon #ifdef DEBUG
1974721fffe3SKacheong Poon 	ASSERT(cnt == tcp->tcp_rcv_cnt);
1975721fffe3SKacheong Poon #endif
1976721fffe3SKacheong Poon 	tcp->tcp_rcv_last_head = NULL;
1977721fffe3SKacheong Poon 	tcp->tcp_rcv_last_tail = NULL;
1978721fffe3SKacheong Poon 	tcp->tcp_rcv_cnt = 0;
1979721fffe3SKacheong Poon 
1980721fffe3SKacheong Poon 	if (canputnext(q))
1981721fffe3SKacheong Poon 		return (tcp_rwnd_reopen(tcp));
1982721fffe3SKacheong Poon 
1983721fffe3SKacheong Poon 	return (ret);
1984721fffe3SKacheong Poon }
1985721fffe3SKacheong Poon 
1986721fffe3SKacheong Poon /*
1987721fffe3SKacheong Poon  * Queue data on tcp_rcv_list which is a b_next chain.
1988721fffe3SKacheong Poon  * tcp_rcv_last_head/tail is the last element of this chain.
1989721fffe3SKacheong Poon  * Each element of the chain is a b_cont chain.
1990721fffe3SKacheong Poon  *
1991721fffe3SKacheong Poon  * M_DATA messages are added to the current element.
1992721fffe3SKacheong Poon  * Other messages are added as new (b_next) elements.
1993721fffe3SKacheong Poon  */
1994721fffe3SKacheong Poon void
1995721fffe3SKacheong Poon tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len, cred_t *cr)
1996721fffe3SKacheong Poon {
1997721fffe3SKacheong Poon 	ASSERT(seg_len == msgdsize(mp));
1998721fffe3SKacheong Poon 	ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL);
1999721fffe3SKacheong Poon 
2000721fffe3SKacheong Poon 	if (is_system_labeled()) {
2001721fffe3SKacheong Poon 		ASSERT(cr != NULL || msg_getcred(mp, NULL) != NULL);
2002721fffe3SKacheong Poon 		/*
2003721fffe3SKacheong Poon 		 * Provide for protocols above TCP such as RPC. NOPID leaves
2004721fffe3SKacheong Poon 		 * db_cpid unchanged.
2005721fffe3SKacheong Poon 		 * The cred could have already been set.
2006721fffe3SKacheong Poon 		 */
2007721fffe3SKacheong Poon 		if (cr != NULL)
2008721fffe3SKacheong Poon 			mblk_setcred(mp, cr, NOPID);
2009721fffe3SKacheong Poon 	}
2010721fffe3SKacheong Poon 
2011721fffe3SKacheong Poon 	if (tcp->tcp_rcv_list == NULL) {
2012721fffe3SKacheong Poon 		ASSERT(tcp->tcp_rcv_last_head == NULL);
2013721fffe3SKacheong Poon 		tcp->tcp_rcv_list = mp;
2014721fffe3SKacheong Poon 		tcp->tcp_rcv_last_head = mp;
2015721fffe3SKacheong Poon 	} else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) {
2016721fffe3SKacheong Poon 		tcp->tcp_rcv_last_tail->b_cont = mp;
2017721fffe3SKacheong Poon 	} else {
2018721fffe3SKacheong Poon 		tcp->tcp_rcv_last_head->b_next = mp;
2019721fffe3SKacheong Poon 		tcp->tcp_rcv_last_head = mp;
2020721fffe3SKacheong Poon 	}
2021721fffe3SKacheong Poon 
2022721fffe3SKacheong Poon 	while (mp->b_cont)
2023721fffe3SKacheong Poon 		mp = mp->b_cont;
2024721fffe3SKacheong Poon 
2025721fffe3SKacheong Poon 	tcp->tcp_rcv_last_tail = mp;
2026721fffe3SKacheong Poon 	tcp->tcp_rcv_cnt += seg_len;
2027721fffe3SKacheong Poon 	tcp->tcp_rwnd -= seg_len;
2028721fffe3SKacheong Poon }
2029721fffe3SKacheong Poon 
2030721fffe3SKacheong Poon /* Generate an ACK-only (no data) segment for a TCP endpoint */
2031721fffe3SKacheong Poon mblk_t *
2032721fffe3SKacheong Poon tcp_ack_mp(tcp_t *tcp)
2033721fffe3SKacheong Poon {
2034721fffe3SKacheong Poon 	uint32_t	seq_no;
2035721fffe3SKacheong Poon 	tcp_stack_t	*tcps = tcp->tcp_tcps;
2036721fffe3SKacheong Poon 	conn_t		*connp = tcp->tcp_connp;
2037721fffe3SKacheong Poon 
2038721fffe3SKacheong Poon 	/*
2039721fffe3SKacheong Poon 	 * There are a few cases to be considered while setting the sequence no.
2040721fffe3SKacheong Poon 	 * Essentially, we can come here while processing an unacceptable pkt
2041721fffe3SKacheong Poon 	 * in the TCPS_SYN_RCVD state, in which case we set the sequence number
2042721fffe3SKacheong Poon 	 * to snxt (per RFC 793), note the swnd wouldn't have been set yet.
2043721fffe3SKacheong Poon 	 * If we are here for a zero window probe, stick with suna. In all
2044721fffe3SKacheong Poon 	 * other cases, we check if suna + swnd encompasses snxt and set
2045721fffe3SKacheong Poon 	 * the sequence number to snxt, if so. If snxt falls outside the
2046721fffe3SKacheong Poon 	 * window (the receiver probably shrunk its window), we will go with
2047721fffe3SKacheong Poon 	 * suna + swnd, otherwise the sequence no will be unacceptable to the
2048721fffe3SKacheong Poon 	 * receiver.
2049721fffe3SKacheong Poon 	 */
2050721fffe3SKacheong Poon 	if (tcp->tcp_zero_win_probe) {
2051721fffe3SKacheong Poon 		seq_no = tcp->tcp_suna;
2052721fffe3SKacheong Poon 	} else if (tcp->tcp_state == TCPS_SYN_RCVD) {
2053721fffe3SKacheong Poon 		ASSERT(tcp->tcp_swnd == 0);
2054721fffe3SKacheong Poon 		seq_no = tcp->tcp_snxt;
2055721fffe3SKacheong Poon 	} else {
2056721fffe3SKacheong Poon 		seq_no = SEQ_GT(tcp->tcp_snxt,
2057721fffe3SKacheong Poon 		    (tcp->tcp_suna + tcp->tcp_swnd)) ?
2058721fffe3SKacheong Poon 		    (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt;
2059721fffe3SKacheong Poon 	}
2060721fffe3SKacheong Poon 
2061721fffe3SKacheong Poon 	if (tcp->tcp_valid_bits) {
2062721fffe3SKacheong Poon 		/*
2063721fffe3SKacheong Poon 		 * For the complex case where we have to send some
2064721fffe3SKacheong Poon 		 * controls (FIN or SYN), let tcp_xmit_mp do it.
2065721fffe3SKacheong Poon 		 */
2066721fffe3SKacheong Poon 		return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE,
2067721fffe3SKacheong Poon 		    NULL, B_FALSE));
2068721fffe3SKacheong Poon 	} else {
2069721fffe3SKacheong Poon 		/* Generate a simple ACK */
2070721fffe3SKacheong Poon 		int	data_length;
2071721fffe3SKacheong Poon 		uchar_t	*rptr;
2072721fffe3SKacheong Poon 		tcpha_t	*tcpha;
2073721fffe3SKacheong Poon 		mblk_t	*mp1;
2074721fffe3SKacheong Poon 		int32_t	total_hdr_len;
2075721fffe3SKacheong Poon 		int32_t	tcp_hdr_len;
2076721fffe3SKacheong Poon 		int32_t	num_sack_blk = 0;
2077721fffe3SKacheong Poon 		int32_t sack_opt_len;
2078721fffe3SKacheong Poon 		ip_xmit_attr_t *ixa = connp->conn_ixa;
2079721fffe3SKacheong Poon 
2080721fffe3SKacheong Poon 		/*
2081721fffe3SKacheong Poon 		 * Allocate space for TCP + IP headers
2082721fffe3SKacheong Poon 		 * and link-level header
2083721fffe3SKacheong Poon 		 */
2084721fffe3SKacheong Poon 		if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
2085721fffe3SKacheong Poon 			num_sack_blk = MIN(tcp->tcp_max_sack_blk,
2086721fffe3SKacheong Poon 			    tcp->tcp_num_sack_blk);
2087721fffe3SKacheong Poon 			sack_opt_len = num_sack_blk * sizeof (sack_blk_t) +
2088721fffe3SKacheong Poon 			    TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN;
2089721fffe3SKacheong Poon 			total_hdr_len = connp->conn_ht_iphc_len + sack_opt_len;
2090721fffe3SKacheong Poon 			tcp_hdr_len = connp->conn_ht_ulp_len + sack_opt_len;
2091721fffe3SKacheong Poon 		} else {
2092721fffe3SKacheong Poon 			total_hdr_len = connp->conn_ht_iphc_len;
2093721fffe3SKacheong Poon 			tcp_hdr_len = connp->conn_ht_ulp_len;
2094721fffe3SKacheong Poon 		}
2095721fffe3SKacheong Poon 		mp1 = allocb(total_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED);
2096721fffe3SKacheong Poon 		if (!mp1)
2097721fffe3SKacheong Poon 			return (NULL);
2098721fffe3SKacheong Poon 
2099721fffe3SKacheong Poon 		/* Update the latest receive window size in TCP header. */
2100721fffe3SKacheong Poon 		tcp->tcp_tcpha->tha_win =
2101721fffe3SKacheong Poon 		    htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
2102721fffe3SKacheong Poon 		/* copy in prototype TCP + IP header */
2103721fffe3SKacheong Poon 		rptr = mp1->b_rptr + tcps->tcps_wroff_xtra;
2104721fffe3SKacheong Poon 		mp1->b_rptr = rptr;
2105721fffe3SKacheong Poon 		mp1->b_wptr = rptr + total_hdr_len;
2106721fffe3SKacheong Poon 		bcopy(connp->conn_ht_iphc, rptr, connp->conn_ht_iphc_len);
2107721fffe3SKacheong Poon 
2108721fffe3SKacheong Poon 		tcpha = (tcpha_t *)&rptr[ixa->ixa_ip_hdr_length];
2109721fffe3SKacheong Poon 
2110721fffe3SKacheong Poon 		/* Set the TCP sequence number. */
2111721fffe3SKacheong Poon 		tcpha->tha_seq = htonl(seq_no);
2112721fffe3SKacheong Poon 
2113721fffe3SKacheong Poon 		/* Set up the TCP flag field. */
2114721fffe3SKacheong Poon 		tcpha->tha_flags = (uchar_t)TH_ACK;
2115721fffe3SKacheong Poon 		if (tcp->tcp_ecn_echo_on)
2116721fffe3SKacheong Poon 			tcpha->tha_flags |= TH_ECE;
2117721fffe3SKacheong Poon 
2118721fffe3SKacheong Poon 		tcp->tcp_rack = tcp->tcp_rnxt;
2119721fffe3SKacheong Poon 		tcp->tcp_rack_cnt = 0;
2120721fffe3SKacheong Poon 
2121721fffe3SKacheong Poon 		/* fill in timestamp option if in use */
2122721fffe3SKacheong Poon 		if (tcp->tcp_snd_ts_ok) {
2123721fffe3SKacheong Poon 			uint32_t llbolt = (uint32_t)LBOLT_FASTPATH;
2124721fffe3SKacheong Poon 
2125721fffe3SKacheong Poon 			U32_TO_BE32(llbolt,
2126721fffe3SKacheong Poon 			    (char *)tcpha + TCP_MIN_HEADER_LENGTH+4);
2127721fffe3SKacheong Poon 			U32_TO_BE32(tcp->tcp_ts_recent,
2128721fffe3SKacheong Poon 			    (char *)tcpha + TCP_MIN_HEADER_LENGTH+8);
2129721fffe3SKacheong Poon 		}
2130721fffe3SKacheong Poon 
2131721fffe3SKacheong Poon 		/* Fill in SACK options */
2132721fffe3SKacheong Poon 		if (num_sack_blk > 0) {
2133721fffe3SKacheong Poon 			uchar_t *wptr = (uchar_t *)tcpha +
2134721fffe3SKacheong Poon 			    connp->conn_ht_ulp_len;
2135721fffe3SKacheong Poon 			sack_blk_t *tmp;
2136721fffe3SKacheong Poon 			int32_t	i;
2137721fffe3SKacheong Poon 
2138721fffe3SKacheong Poon 			wptr[0] = TCPOPT_NOP;
2139721fffe3SKacheong Poon 			wptr[1] = TCPOPT_NOP;
2140721fffe3SKacheong Poon 			wptr[2] = TCPOPT_SACK;
2141721fffe3SKacheong Poon 			wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
2142721fffe3SKacheong Poon 			    sizeof (sack_blk_t);
2143721fffe3SKacheong Poon 			wptr += TCPOPT_REAL_SACK_LEN;
2144721fffe3SKacheong Poon 
2145721fffe3SKacheong Poon 			tmp = tcp->tcp_sack_list;
2146721fffe3SKacheong Poon 			for (i = 0; i < num_sack_blk; i++) {
2147721fffe3SKacheong Poon 				U32_TO_BE32(tmp[i].begin, wptr);
2148721fffe3SKacheong Poon 				wptr += sizeof (tcp_seq);
2149721fffe3SKacheong Poon 				U32_TO_BE32(tmp[i].end, wptr);
2150721fffe3SKacheong Poon 				wptr += sizeof (tcp_seq);
2151721fffe3SKacheong Poon 			}
2152721fffe3SKacheong Poon 			tcpha->tha_offset_and_reserved +=
2153721fffe3SKacheong Poon 			    ((num_sack_blk * 2 + 1) << 4);
2154721fffe3SKacheong Poon 		}
2155721fffe3SKacheong Poon 
2156721fffe3SKacheong Poon 		ixa->ixa_pktlen = total_hdr_len;
2157721fffe3SKacheong Poon 
2158721fffe3SKacheong Poon 		if (ixa->ixa_flags & IXAF_IS_IPV4) {
2159721fffe3SKacheong Poon 			((ipha_t *)rptr)->ipha_length = htons(total_hdr_len);
2160721fffe3SKacheong Poon 		} else {
2161721fffe3SKacheong Poon 			ip6_t *ip6 = (ip6_t *)rptr;
2162721fffe3SKacheong Poon 
2163721fffe3SKacheong Poon 			ip6->ip6_plen = htons(total_hdr_len - IPV6_HDR_LEN);
2164721fffe3SKacheong Poon 		}
2165721fffe3SKacheong Poon 
2166721fffe3SKacheong Poon 		/*
2167721fffe3SKacheong Poon 		 * Prime pump for checksum calculation in IP.  Include the
2168721fffe3SKacheong Poon 		 * adjustment for a source route if any.
2169721fffe3SKacheong Poon 		 */
2170721fffe3SKacheong Poon 		data_length = tcp_hdr_len + connp->conn_sum;
2171721fffe3SKacheong Poon 		data_length = (data_length >> 16) + (data_length & 0xFFFF);
2172721fffe3SKacheong Poon 		tcpha->tha_sum = htons(data_length);
2173721fffe3SKacheong Poon 
2174721fffe3SKacheong Poon 		if (tcp->tcp_ip_forward_progress) {
2175721fffe3SKacheong Poon 			tcp->tcp_ip_forward_progress = B_FALSE;
2176721fffe3SKacheong Poon 			connp->conn_ixa->ixa_flags |= IXAF_REACH_CONF;
2177721fffe3SKacheong Poon 		} else {
2178721fffe3SKacheong Poon 			connp->conn_ixa->ixa_flags &= ~IXAF_REACH_CONF;
2179721fffe3SKacheong Poon 		}
2180721fffe3SKacheong Poon 		return (mp1);
2181721fffe3SKacheong Poon 	}
2182721fffe3SKacheong Poon }
2183721fffe3SKacheong Poon 
2184721fffe3SKacheong Poon /*
2185f2297e92SDan McDonald  * Dummy socket upcalls for if/when the conn_t gets detached from a
2186f2297e92SDan McDonald  * direct-callback sonode via a user-driven close().  Easy to catch with
2187f2297e92SDan McDonald  * DTrace FBT, and should be mostly harmless.
2188f2297e92SDan McDonald  */
2189f2297e92SDan McDonald 
2190f2297e92SDan McDonald /* ARGSUSED */
2191f2297e92SDan McDonald static sock_upper_handle_t
2192f2297e92SDan McDonald tcp_dummy_newconn(sock_upper_handle_t x, sock_lower_handle_t y,
2193f2297e92SDan McDonald     sock_downcalls_t *z, cred_t *cr, pid_t pid, sock_upcalls_t **ignored)
2194f2297e92SDan McDonald {
2195f2297e92SDan McDonald 	ASSERT(0);	/* Panic in debug, otherwise ignore. */
2196f2297e92SDan McDonald 	return (NULL);
2197f2297e92SDan McDonald }
2198f2297e92SDan McDonald 
2199f2297e92SDan McDonald /* ARGSUSED */
2200f2297e92SDan McDonald static void
2201f2297e92SDan McDonald tcp_dummy_connected(sock_upper_handle_t x, sock_connid_t y, cred_t *cr,
2202f2297e92SDan McDonald     pid_t pid)
2203f2297e92SDan McDonald {
2204f2297e92SDan McDonald 	ASSERT(x == NULL);
2205f2297e92SDan McDonald 	/* Normally we'd crhold(cr) and attach it to socket state. */
2206f2297e92SDan McDonald 	/* LINTED */
2207f2297e92SDan McDonald }
2208f2297e92SDan McDonald 
2209f2297e92SDan McDonald /* ARGSUSED */
2210f2297e92SDan McDonald static int
2211f2297e92SDan McDonald tcp_dummy_disconnected(sock_upper_handle_t x, sock_connid_t y, int blah)
2212f2297e92SDan McDonald {
2213f2297e92SDan McDonald 	ASSERT(0);	/* Panic in debug, otherwise ignore. */
2214f2297e92SDan McDonald 	return (-1);
2215f2297e92SDan McDonald }
2216f2297e92SDan McDonald 
2217f2297e92SDan McDonald /* ARGSUSED */
2218f2297e92SDan McDonald static void
2219f2297e92SDan McDonald tcp_dummy_opctl(sock_upper_handle_t x, sock_opctl_action_t y, uintptr_t blah)
2220f2297e92SDan McDonald {
2221f2297e92SDan McDonald 	ASSERT(x == NULL);
2222f2297e92SDan McDonald 	/* We really want this one to be a harmless NOP for now. */
2223f2297e92SDan McDonald 	/* LINTED */
2224f2297e92SDan McDonald }
2225f2297e92SDan McDonald 
2226f2297e92SDan McDonald /* ARGSUSED */
2227f2297e92SDan McDonald static ssize_t
2228f2297e92SDan McDonald tcp_dummy_recv(sock_upper_handle_t x, mblk_t *mp, size_t len, int flags,
2229f2297e92SDan McDonald     int *error, boolean_t *push)
2230f2297e92SDan McDonald {
2231f2297e92SDan McDonald 	ASSERT(x == NULL);
2232f2297e92SDan McDonald 
2233f2297e92SDan McDonald 	/*
2234f2297e92SDan McDonald 	 * Consume the message, set ESHUTDOWN, and return an error.
2235f2297e92SDan McDonald 	 * Nobody's home!
2236f2297e92SDan McDonald 	 */
2237f2297e92SDan McDonald 	freemsg(mp);
2238f2297e92SDan McDonald 	*error = ESHUTDOWN;
2239f2297e92SDan McDonald 	return (-1);
2240f2297e92SDan McDonald }
2241f2297e92SDan McDonald 
2242f2297e92SDan McDonald /* ARGSUSED */
2243f2297e92SDan McDonald static void
2244f2297e92SDan McDonald tcp_dummy_set_proto_props(sock_upper_handle_t x, struct sock_proto_props *y)
2245f2297e92SDan McDonald {
2246f2297e92SDan McDonald 	ASSERT(0);	/* Panic in debug, otherwise ignore. */
2247f2297e92SDan McDonald }
2248f2297e92SDan McDonald 
2249f2297e92SDan McDonald /* ARGSUSED */
2250f2297e92SDan McDonald static void
2251f2297e92SDan McDonald tcp_dummy_txq_full(sock_upper_handle_t x, boolean_t y)
2252f2297e92SDan McDonald {
2253f2297e92SDan McDonald 	ASSERT(0);	/* Panic in debug, otherwise ignore. */
2254f2297e92SDan McDonald }
2255f2297e92SDan McDonald 
2256f2297e92SDan McDonald /* ARGSUSED */
2257f2297e92SDan McDonald static void
2258f2297e92SDan McDonald tcp_dummy_signal_oob(sock_upper_handle_t x, ssize_t len)
2259f2297e92SDan McDonald {
2260f2297e92SDan McDonald 	ASSERT(x == NULL);
2261f2297e92SDan McDonald 	/* Otherwise, this would signal socket state about OOB data. */
2262f2297e92SDan McDonald }
2263f2297e92SDan McDonald 
2264f2297e92SDan McDonald /* ARGSUSED */
2265f2297e92SDan McDonald static void
2266f2297e92SDan McDonald tcp_dummy_set_error(sock_upper_handle_t x, int err)
2267f2297e92SDan McDonald {
2268f2297e92SDan McDonald 	ASSERT(0);	/* Panic in debug, otherwise ignore. */
2269f2297e92SDan McDonald }
2270f2297e92SDan McDonald 
2271f2297e92SDan McDonald /* ARGSUSED */
2272f2297e92SDan McDonald static void
2273f2297e92SDan McDonald tcp_dummy_onearg(sock_upper_handle_t x)
2274f2297e92SDan McDonald {
2275f2297e92SDan McDonald 	ASSERT(0);	/* Panic in debug, otherwise ignore. */
2276f2297e92SDan McDonald }
2277f2297e92SDan McDonald 
2278f2297e92SDan McDonald static sock_upcalls_t tcp_dummy_upcalls = {
2279f2297e92SDan McDonald 	tcp_dummy_newconn,
2280f2297e92SDan McDonald 	tcp_dummy_connected,
2281f2297e92SDan McDonald 	tcp_dummy_disconnected,
2282f2297e92SDan McDonald 	tcp_dummy_opctl,
2283f2297e92SDan McDonald 	tcp_dummy_recv,
2284f2297e92SDan McDonald 	tcp_dummy_set_proto_props,
2285f2297e92SDan McDonald 	tcp_dummy_txq_full,
2286f2297e92SDan McDonald 	tcp_dummy_signal_oob,
2287f2297e92SDan McDonald 	tcp_dummy_onearg,
2288f2297e92SDan McDonald 	tcp_dummy_set_error,
2289f2297e92SDan McDonald 	tcp_dummy_onearg
2290f2297e92SDan McDonald };
2291f2297e92SDan McDonald 
2292f2297e92SDan McDonald /*
2293721fffe3SKacheong Poon  * Handle M_DATA messages from IP. Its called directly from IP via
2294721fffe3SKacheong Poon  * squeue for received IP packets.
2295721fffe3SKacheong Poon  *
2296721fffe3SKacheong Poon  * The first argument is always the connp/tcp to which the mp belongs.
2297721fffe3SKacheong Poon  * There are no exceptions to this rule. The caller has already put
2298721fffe3SKacheong Poon  * a reference on this connp/tcp and once tcp_input_data() returns,
2299721fffe3SKacheong Poon  * the squeue will do the refrele.
2300721fffe3SKacheong Poon  *
2301721fffe3SKacheong Poon  * The TH_SYN for the listener directly go to tcp_input_listener via
2302721fffe3SKacheong Poon  * squeue. ICMP errors go directly to tcp_icmp_input().
2303721fffe3SKacheong Poon  *
2304721fffe3SKacheong Poon  * sqp: NULL = recursive, sqp != NULL means called from squeue
2305721fffe3SKacheong Poon  */
2306721fffe3SKacheong Poon void
2307721fffe3SKacheong Poon tcp_input_data(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
2308721fffe3SKacheong Poon {
2309721fffe3SKacheong Poon 	int32_t		bytes_acked;
2310721fffe3SKacheong Poon 	int32_t		gap;
2311721fffe3SKacheong Poon 	mblk_t		*mp1;
2312721fffe3SKacheong Poon 	uint_t		flags;
2313721fffe3SKacheong Poon 	uint32_t	new_swnd = 0;
2314721fffe3SKacheong Poon 	uchar_t		*iphdr;
2315721fffe3SKacheong Poon 	uchar_t		*rptr;
2316721fffe3SKacheong Poon 	int32_t		rgap;
2317721fffe3SKacheong Poon 	uint32_t	seg_ack;
2318721fffe3SKacheong Poon 	int		seg_len;
2319721fffe3SKacheong Poon 	uint_t		ip_hdr_len;
2320721fffe3SKacheong Poon 	uint32_t	seg_seq;
2321721fffe3SKacheong Poon 	tcpha_t		*tcpha;
2322721fffe3SKacheong Poon 	int		urp;
2323721fffe3SKacheong Poon 	tcp_opt_t	tcpopt;
2324721fffe3SKacheong Poon 	ip_pkt_t	ipp;
2325721fffe3SKacheong Poon 	boolean_t	ofo_seg = B_FALSE; /* Out of order segment */
2326721fffe3SKacheong Poon 	uint32_t	cwnd;
2327721fffe3SKacheong Poon 	uint32_t	add;
2328721fffe3SKacheong Poon 	int		npkt;
2329721fffe3SKacheong Poon 	int		mss;
2330721fffe3SKacheong Poon 	conn_t		*connp = (conn_t *)arg;
2331721fffe3SKacheong Poon 	squeue_t	*sqp = (squeue_t *)arg2;
2332721fffe3SKacheong Poon 	tcp_t		*tcp = connp->conn_tcp;
2333721fffe3SKacheong Poon 	tcp_stack_t	*tcps = tcp->tcp_tcps;
2334f2297e92SDan McDonald 	sock_upcalls_t	*sockupcalls;
2335721fffe3SKacheong Poon 
2336721fffe3SKacheong Poon 	/*
2337721fffe3SKacheong Poon 	 * RST from fused tcp loopback peer should trigger an unfuse.
2338721fffe3SKacheong Poon 	 */
2339721fffe3SKacheong Poon 	if (tcp->tcp_fused) {
2340721fffe3SKacheong Poon 		TCP_STAT(tcps, tcp_fusion_aborted);
2341721fffe3SKacheong Poon 		tcp_unfuse(tcp);
2342721fffe3SKacheong Poon 	}
2343721fffe3SKacheong Poon 
2344721fffe3SKacheong Poon 	iphdr = mp->b_rptr;
2345721fffe3SKacheong Poon 	rptr = mp->b_rptr;
2346721fffe3SKacheong Poon 	ASSERT(OK_32PTR(rptr));
2347721fffe3SKacheong Poon 
2348721fffe3SKacheong Poon 	ip_hdr_len = ira->ira_ip_hdr_length;
2349721fffe3SKacheong Poon 	if (connp->conn_recv_ancillary.crb_all != 0) {
2350721fffe3SKacheong Poon 		/*
2351721fffe3SKacheong Poon 		 * Record packet information in the ip_pkt_t
2352721fffe3SKacheong Poon 		 */
2353721fffe3SKacheong Poon 		ipp.ipp_fields = 0;
2354721fffe3SKacheong Poon 		if (ira->ira_flags & IRAF_IS_IPV4) {
2355721fffe3SKacheong Poon 			(void) ip_find_hdr_v4((ipha_t *)rptr, &ipp,
2356721fffe3SKacheong Poon 			    B_FALSE);
2357721fffe3SKacheong Poon 		} else {
2358721fffe3SKacheong Poon 			uint8_t nexthdrp;
2359721fffe3SKacheong Poon 
2360721fffe3SKacheong Poon 			/*
2361721fffe3SKacheong Poon 			 * IPv6 packets can only be received by applications
2362721fffe3SKacheong Poon 			 * that are prepared to receive IPv6 addresses.
2363721fffe3SKacheong Poon 			 * The IP fanout must ensure this.
2364721fffe3SKacheong Poon 			 */
2365721fffe3SKacheong Poon 			ASSERT(connp->conn_family == AF_INET6);
2366721fffe3SKacheong Poon 
2367721fffe3SKacheong Poon 			(void) ip_find_hdr_v6(mp, (ip6_t *)rptr, B_TRUE, &ipp,
2368721fffe3SKacheong Poon 			    &nexthdrp);
2369721fffe3SKacheong Poon 			ASSERT(nexthdrp == IPPROTO_TCP);
2370721fffe3SKacheong Poon 
2371721fffe3SKacheong Poon 			/* Could have caused a pullup? */
2372721fffe3SKacheong Poon 			iphdr = mp->b_rptr;
2373721fffe3SKacheong Poon 			rptr = mp->b_rptr;
2374721fffe3SKacheong Poon 		}
2375721fffe3SKacheong Poon 	}
2376721fffe3SKacheong Poon 	ASSERT(DB_TYPE(mp) == M_DATA);
2377721fffe3SKacheong Poon 	ASSERT(mp->b_next == NULL);
2378721fffe3SKacheong Poon 
2379721fffe3SKacheong Poon 	tcpha = (tcpha_t *)&rptr[ip_hdr_len];
2380721fffe3SKacheong Poon 	seg_seq = ntohl(tcpha->tha_seq);
2381721fffe3SKacheong Poon 	seg_ack = ntohl(tcpha->tha_ack);
2382721fffe3SKacheong Poon 	ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX);
2383721fffe3SKacheong Poon 	seg_len = (int)(mp->b_wptr - rptr) -
2384721fffe3SKacheong Poon 	    (ip_hdr_len + TCP_HDR_LENGTH(tcpha));
2385721fffe3SKacheong Poon 	if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) {
2386721fffe3SKacheong Poon 		do {
2387721fffe3SKacheong Poon 			ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <=
2388721fffe3SKacheong Poon 			    (uintptr_t)INT_MAX);
2389721fffe3SKacheong Poon 			seg_len += (int)(mp1->b_wptr - mp1->b_rptr);
2390721fffe3SKacheong Poon 		} while ((mp1 = mp1->b_cont) != NULL &&
2391721fffe3SKacheong Poon 		    mp1->b_datap->db_type == M_DATA);
2392721fffe3SKacheong Poon 	}
2393721fffe3SKacheong Poon 
23949cd928feSAlan Maguire 	DTRACE_TCP5(receive, mblk_t *, NULL, ip_xmit_attr_t *, connp->conn_ixa,
23959cd928feSAlan Maguire 	    __dtrace_tcp_void_ip_t *, iphdr, tcp_t *, tcp,
23969cd928feSAlan Maguire 	    __dtrace_tcp_tcph_t *, tcpha);
23979cd928feSAlan Maguire 
2398721fffe3SKacheong Poon 	if (tcp->tcp_state == TCPS_TIME_WAIT) {
2399721fffe3SKacheong Poon 		tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack,
2400721fffe3SKacheong Poon 		    seg_len, tcpha, ira);
2401721fffe3SKacheong Poon 		return;
2402721fffe3SKacheong Poon 	}
2403721fffe3SKacheong Poon 
2404721fffe3SKacheong Poon 	if (sqp != NULL) {
2405721fffe3SKacheong Poon 		/*
2406721fffe3SKacheong Poon 		 * This is the correct place to update tcp_last_recv_time. Note
2407721fffe3SKacheong Poon 		 * that it is also updated for tcp structure that belongs to
2408721fffe3SKacheong Poon 		 * global and listener queues which do not really need updating.
2409721fffe3SKacheong Poon 		 * But that should not cause any harm.  And it is updated for
2410721fffe3SKacheong Poon 		 * all kinds of incoming segments, not only for data segments.
2411721fffe3SKacheong Poon 		 */
2412721fffe3SKacheong Poon 		tcp->tcp_last_recv_time = LBOLT_FASTPATH;
2413721fffe3SKacheong Poon 	}
2414721fffe3SKacheong Poon 
2415721fffe3SKacheong Poon 	flags = (unsigned int)tcpha->tha_flags & 0xFF;
2416721fffe3SKacheong Poon 
2417721fffe3SKacheong Poon 	BUMP_LOCAL(tcp->tcp_ibsegs);
2418721fffe3SKacheong Poon 	DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp);
2419721fffe3SKacheong Poon 
2420721fffe3SKacheong Poon 	if ((flags & TH_URG) && sqp != NULL) {
2421721fffe3SKacheong Poon 		/*
2422721fffe3SKacheong Poon 		 * TCP can't handle urgent pointers that arrive before
2423721fffe3SKacheong Poon 		 * the connection has been accept()ed since it can't
2424721fffe3SKacheong Poon 		 * buffer OOB data.  Discard segment if this happens.
2425721fffe3SKacheong Poon 		 *
2426721fffe3SKacheong Poon 		 * We can't just rely on a non-null tcp_listener to indicate
2427721fffe3SKacheong Poon 		 * that the accept() has completed since unlinking of the
2428721fffe3SKacheong Poon 		 * eager and completion of the accept are not atomic.
2429721fffe3SKacheong Poon 		 * tcp_detached, when it is not set (B_FALSE) indicates
2430721fffe3SKacheong Poon 		 * that the accept() has completed.
2431721fffe3SKacheong Poon 		 *
2432721fffe3SKacheong Poon 		 * Nor can it reassemble urgent pointers, so discard
2433721fffe3SKacheong Poon 		 * if it's not the next segment expected.
2434721fffe3SKacheong Poon 		 *
2435721fffe3SKacheong Poon 		 * Otherwise, collapse chain into one mblk (discard if
2436721fffe3SKacheong Poon 		 * that fails).  This makes sure the headers, retransmitted
2437721fffe3SKacheong Poon 		 * data, and new data all are in the same mblk.
2438721fffe3SKacheong Poon 		 */
2439721fffe3SKacheong Poon 		ASSERT(mp != NULL);
2440721fffe3SKacheong Poon 		if (tcp->tcp_detached || !pullupmsg(mp, -1)) {
2441721fffe3SKacheong Poon 			freemsg(mp);
2442721fffe3SKacheong Poon 			return;
2443721fffe3SKacheong Poon 		}
2444721fffe3SKacheong Poon 		/* Update pointers into message */
2445721fffe3SKacheong Poon 		iphdr = rptr = mp->b_rptr;
2446721fffe3SKacheong Poon 		tcpha = (tcpha_t *)&rptr[ip_hdr_len];
2447721fffe3SKacheong Poon 		if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) {
2448721fffe3SKacheong Poon 			/*
2449721fffe3SKacheong Poon 			 * Since we can't handle any data with this urgent
2450721fffe3SKacheong Poon 			 * pointer that is out of sequence, we expunge
2451721fffe3SKacheong Poon 			 * the data.  This allows us to still register
2452721fffe3SKacheong Poon 			 * the urgent mark and generate the M_PCSIG,
2453721fffe3SKacheong Poon 			 * which we can do.
2454721fffe3SKacheong Poon 			 */
2455721fffe3SKacheong Poon 			mp->b_wptr = (uchar_t *)tcpha + TCP_HDR_LENGTH(tcpha);
2456721fffe3SKacheong Poon 			seg_len = 0;
2457721fffe3SKacheong Poon 		}
2458721fffe3SKacheong Poon 	}
2459721fffe3SKacheong Poon 
2460f2297e92SDan McDonald 	sockupcalls = connp->conn_upcalls;
2461f2297e92SDan McDonald 	/* A conn_t may have belonged to a now-closed socket.  Be careful. */
2462f2297e92SDan McDonald 	if (sockupcalls == NULL)
2463f2297e92SDan McDonald 		sockupcalls = &tcp_dummy_upcalls;
2464f2297e92SDan McDonald 
2465721fffe3SKacheong Poon 	switch (tcp->tcp_state) {
2466721fffe3SKacheong Poon 	case TCPS_SYN_SENT:
2467721fffe3SKacheong Poon 		if (connp->conn_final_sqp == NULL &&
2468721fffe3SKacheong Poon 		    tcp_outbound_squeue_switch && sqp != NULL) {
2469721fffe3SKacheong Poon 			ASSERT(connp->conn_initial_sqp == connp->conn_sqp);
2470721fffe3SKacheong Poon 			connp->conn_final_sqp = sqp;
2471721fffe3SKacheong Poon 			if (connp->conn_final_sqp != connp->conn_sqp) {
2472721fffe3SKacheong Poon 				DTRACE_PROBE1(conn__final__sqp__switch,
2473721fffe3SKacheong Poon 				    conn_t *, connp);
2474721fffe3SKacheong Poon 				CONN_INC_REF(connp);
2475721fffe3SKacheong Poon 				SQUEUE_SWITCH(connp, connp->conn_final_sqp);
2476721fffe3SKacheong Poon 				SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
2477721fffe3SKacheong Poon 				    tcp_input_data, connp, ira, ip_squeue_flag,
2478721fffe3SKacheong Poon 				    SQTAG_CONNECT_FINISH);
2479721fffe3SKacheong Poon 				return;
2480721fffe3SKacheong Poon 			}
2481721fffe3SKacheong Poon 			DTRACE_PROBE1(conn__final__sqp__same, conn_t *, connp);
2482721fffe3SKacheong Poon 		}
2483721fffe3SKacheong Poon 		if (flags & TH_ACK) {
2484721fffe3SKacheong Poon 			/*
2485721fffe3SKacheong Poon 			 * Note that our stack cannot send data before a
2486721fffe3SKacheong Poon 			 * connection is established, therefore the
2487721fffe3SKacheong Poon 			 * following check is valid.  Otherwise, it has
2488721fffe3SKacheong Poon 			 * to be changed.
2489721fffe3SKacheong Poon 			 */
2490721fffe3SKacheong Poon 			if (SEQ_LEQ(seg_ack, tcp->tcp_iss) ||
2491721fffe3SKacheong Poon 			    SEQ_GT(seg_ack, tcp->tcp_snxt)) {
2492721fffe3SKacheong Poon 				freemsg(mp);
2493721fffe3SKacheong Poon 				if (flags & TH_RST)
2494721fffe3SKacheong Poon 					return;
2495721fffe3SKacheong Poon 				tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq",
2496721fffe3SKacheong Poon 				    tcp, seg_ack, 0, TH_RST);
2497721fffe3SKacheong Poon 				return;
2498721fffe3SKacheong Poon 			}
2499721fffe3SKacheong Poon 			ASSERT(tcp->tcp_suna + 1 == seg_ack);
2500721fffe3SKacheong Poon 		}
2501721fffe3SKacheong Poon 		if (flags & TH_RST) {
25029cd928feSAlan Maguire 			if (flags & TH_ACK) {
25039cd928feSAlan Maguire 				DTRACE_TCP5(connect__refused, mblk_t *, NULL,
25049cd928feSAlan Maguire 				    ip_xmit_attr_t *, connp->conn_ixa,
25059cd928feSAlan Maguire 				    void_ip_t *, iphdr, tcp_t *, tcp,
25069cd928feSAlan Maguire 				    tcph_t *, tcpha);
2507721fffe3SKacheong Poon 				(void) tcp_clean_death(tcp, ECONNREFUSED);
25089cd928feSAlan Maguire 			}
25099cd928feSAlan Maguire 			freemsg(mp);
2510721fffe3SKacheong Poon 			return;
2511721fffe3SKacheong Poon 		}
2512721fffe3SKacheong Poon 		if (!(flags & TH_SYN)) {
2513721fffe3SKacheong Poon 			freemsg(mp);
2514721fffe3SKacheong Poon 			return;
2515721fffe3SKacheong Poon 		}
2516721fffe3SKacheong Poon 
2517721fffe3SKacheong Poon 		/* Process all TCP options. */
2518721fffe3SKacheong Poon 		tcp_process_options(tcp, tcpha);
2519721fffe3SKacheong Poon 		/*
2520721fffe3SKacheong Poon 		 * The following changes our rwnd to be a multiple of the
2521721fffe3SKacheong Poon 		 * MIN(peer MSS, our MSS) for performance reason.
2522721fffe3SKacheong Poon 		 */
2523721fffe3SKacheong Poon 		(void) tcp_rwnd_set(tcp, MSS_ROUNDUP(connp->conn_rcvbuf,
2524721fffe3SKacheong Poon 		    tcp->tcp_mss));
2525721fffe3SKacheong Poon 
2526721fffe3SKacheong Poon 		/* Is the other end ECN capable? */
2527721fffe3SKacheong Poon 		if (tcp->tcp_ecn_ok) {
2528721fffe3SKacheong Poon 			if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) {
2529721fffe3SKacheong Poon 				tcp->tcp_ecn_ok = B_FALSE;
2530721fffe3SKacheong Poon 			}
2531721fffe3SKacheong Poon 		}
2532721fffe3SKacheong Poon 		/*
2533721fffe3SKacheong Poon 		 * Clear ECN flags because it may interfere with later
2534721fffe3SKacheong Poon 		 * processing.
2535721fffe3SKacheong Poon 		 */
2536721fffe3SKacheong Poon 		flags &= ~(TH_ECE|TH_CWR);
2537721fffe3SKacheong Poon 
2538721fffe3SKacheong Poon 		tcp->tcp_irs = seg_seq;
2539721fffe3SKacheong Poon 		tcp->tcp_rack = seg_seq;
2540721fffe3SKacheong Poon 		tcp->tcp_rnxt = seg_seq + 1;
2541721fffe3SKacheong Poon 		tcp->tcp_tcpha->tha_ack = htonl(tcp->tcp_rnxt);
2542721fffe3SKacheong Poon 		if (!TCP_IS_DETACHED(tcp)) {
2543721fffe3SKacheong Poon 			/* Allocate room for SACK options if needed. */
2544721fffe3SKacheong Poon 			connp->conn_wroff = connp->conn_ht_iphc_len;
2545721fffe3SKacheong Poon 			if (tcp->tcp_snd_sack_ok)
2546721fffe3SKacheong Poon 				connp->conn_wroff += TCPOPT_MAX_SACK_LEN;
2547721fffe3SKacheong Poon 			if (!tcp->tcp_loopback)
2548721fffe3SKacheong Poon 				connp->conn_wroff += tcps->tcps_wroff_xtra;
2549721fffe3SKacheong Poon 
2550721fffe3SKacheong Poon 			(void) proto_set_tx_wroff(connp->conn_rq, connp,
2551721fffe3SKacheong Poon 			    connp->conn_wroff);
2552721fffe3SKacheong Poon 		}
2553721fffe3SKacheong Poon 		if (flags & TH_ACK) {
2554721fffe3SKacheong Poon 			/*
2555721fffe3SKacheong Poon 			 * If we can't get the confirmation upstream, pretend
2556721fffe3SKacheong Poon 			 * we didn't even see this one.
2557721fffe3SKacheong Poon 			 *
2558721fffe3SKacheong Poon 			 * XXX: how can we pretend we didn't see it if we
2559721fffe3SKacheong Poon 			 * have updated rnxt et. al.
2560721fffe3SKacheong Poon 			 *
2561721fffe3SKacheong Poon 			 * For loopback we defer sending up the T_CONN_CON
2562721fffe3SKacheong Poon 			 * until after some checks below.
2563721fffe3SKacheong Poon 			 */
2564721fffe3SKacheong Poon 			mp1 = NULL;
2565721fffe3SKacheong Poon 			/*
2566721fffe3SKacheong Poon 			 * tcp_sendmsg() checks tcp_state without entering
2567721fffe3SKacheong Poon 			 * the squeue so tcp_state should be updated before
25689cd928feSAlan Maguire 			 * sending up connection confirmation.  Probe the
25699cd928feSAlan Maguire 			 * state change below when we are sure the connection
25709cd928feSAlan Maguire 			 * confirmation has been sent.
2571721fffe3SKacheong Poon 			 */
2572721fffe3SKacheong Poon 			tcp->tcp_state = TCPS_ESTABLISHED;
2573721fffe3SKacheong Poon 			if (!tcp_conn_con(tcp, iphdr, mp,
2574721fffe3SKacheong Poon 			    tcp->tcp_loopback ? &mp1 : NULL, ira)) {
2575721fffe3SKacheong Poon 				tcp->tcp_state = TCPS_SYN_SENT;
2576721fffe3SKacheong Poon 				freemsg(mp);
2577721fffe3SKacheong Poon 				return;
2578721fffe3SKacheong Poon 			}
2579721fffe3SKacheong Poon 			TCPS_CONN_INC(tcps);
2580721fffe3SKacheong Poon 			/* SYN was acked - making progress */
2581721fffe3SKacheong Poon 			tcp->tcp_ip_forward_progress = B_TRUE;
2582721fffe3SKacheong Poon 
2583721fffe3SKacheong Poon 			/* One for the SYN */
2584721fffe3SKacheong Poon 			tcp->tcp_suna = tcp->tcp_iss + 1;
2585721fffe3SKacheong Poon 			tcp->tcp_valid_bits &= ~TCP_ISS_VALID;
2586721fffe3SKacheong Poon 
2587721fffe3SKacheong Poon 			/*
2588721fffe3SKacheong Poon 			 * If SYN was retransmitted, need to reset all
2589721fffe3SKacheong Poon 			 * retransmission info.  This is because this
2590721fffe3SKacheong Poon 			 * segment will be treated as a dup ACK.
2591721fffe3SKacheong Poon 			 */
2592721fffe3SKacheong Poon 			if (tcp->tcp_rexmit) {
2593721fffe3SKacheong Poon 				tcp->tcp_rexmit = B_FALSE;
2594721fffe3SKacheong Poon 				tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
2595721fffe3SKacheong Poon 				tcp->tcp_rexmit_max = tcp->tcp_snxt;
2596721fffe3SKacheong Poon 				tcp->tcp_ms_we_have_waited = 0;
2597721fffe3SKacheong Poon 
2598721fffe3SKacheong Poon 				/*
2599721fffe3SKacheong Poon 				 * Set tcp_cwnd back to 1 MSS, per
2600721fffe3SKacheong Poon 				 * recommendation from
2601721fffe3SKacheong Poon 				 * draft-floyd-incr-init-win-01.txt,
2602721fffe3SKacheong Poon 				 * Increasing TCP's Initial Window.
2603721fffe3SKacheong Poon 				 */
2604721fffe3SKacheong Poon 				tcp->tcp_cwnd = tcp->tcp_mss;
2605721fffe3SKacheong Poon 			}
2606721fffe3SKacheong Poon 
2607721fffe3SKacheong Poon 			tcp->tcp_swl1 = seg_seq;
2608721fffe3SKacheong Poon 			tcp->tcp_swl2 = seg_ack;
2609721fffe3SKacheong Poon 
2610721fffe3SKacheong Poon 			new_swnd = ntohs(tcpha->tha_win);
2611721fffe3SKacheong Poon 			tcp->tcp_swnd = new_swnd;
2612721fffe3SKacheong Poon 			if (new_swnd > tcp->tcp_max_swnd)
2613721fffe3SKacheong Poon 				tcp->tcp_max_swnd = new_swnd;
2614721fffe3SKacheong Poon 
2615721fffe3SKacheong Poon 			/*
2616721fffe3SKacheong Poon 			 * Always send the three-way handshake ack immediately
2617721fffe3SKacheong Poon 			 * in order to make the connection complete as soon as
2618721fffe3SKacheong Poon 			 * possible on the accepting host.
2619721fffe3SKacheong Poon 			 */
2620721fffe3SKacheong Poon 			flags |= TH_ACK_NEEDED;
2621721fffe3SKacheong Poon 
2622721fffe3SKacheong Poon 			/*
26239cd928feSAlan Maguire 			 * Trace connect-established here.
26249cd928feSAlan Maguire 			 */
26259cd928feSAlan Maguire 			DTRACE_TCP5(connect__established, mblk_t *, NULL,
26269cd928feSAlan Maguire 			    ip_xmit_attr_t *, tcp->tcp_connp->conn_ixa,
26279cd928feSAlan Maguire 			    void_ip_t *, iphdr, tcp_t *, tcp, tcph_t *, tcpha);
26289cd928feSAlan Maguire 
26299cd928feSAlan Maguire 			/* Trace change from SYN_SENT -> ESTABLISHED here */
26309cd928feSAlan Maguire 			DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
26319cd928feSAlan Maguire 			    connp->conn_ixa, void, NULL, tcp_t *, tcp,
26329cd928feSAlan Maguire 			    void, NULL, int32_t, TCPS_SYN_SENT);
26339cd928feSAlan Maguire 
26349cd928feSAlan Maguire 			/*
2635721fffe3SKacheong Poon 			 * Special case for loopback.  At this point we have
2636721fffe3SKacheong Poon 			 * received SYN-ACK from the remote endpoint.  In
2637721fffe3SKacheong Poon 			 * order to ensure that both endpoints reach the
2638721fffe3SKacheong Poon 			 * fused state prior to any data exchange, the final
2639721fffe3SKacheong Poon 			 * ACK needs to be sent before we indicate T_CONN_CON
2640721fffe3SKacheong Poon 			 * to the module upstream.
2641721fffe3SKacheong Poon 			 */
2642721fffe3SKacheong Poon 			if (tcp->tcp_loopback) {
2643721fffe3SKacheong Poon 				mblk_t *ack_mp;
2644721fffe3SKacheong Poon 
2645721fffe3SKacheong Poon 				ASSERT(!tcp->tcp_unfusable);
2646721fffe3SKacheong Poon 				ASSERT(mp1 != NULL);
2647721fffe3SKacheong Poon 				/*
2648721fffe3SKacheong Poon 				 * For loopback, we always get a pure SYN-ACK
2649721fffe3SKacheong Poon 				 * and only need to send back the final ACK
2650721fffe3SKacheong Poon 				 * with no data (this is because the other
2651721fffe3SKacheong Poon 				 * tcp is ours and we don't do T/TCP).  This
2652721fffe3SKacheong Poon 				 * final ACK triggers the passive side to
2653721fffe3SKacheong Poon 				 * perform fusion in ESTABLISHED state.
2654721fffe3SKacheong Poon 				 */
2655721fffe3SKacheong Poon 				if ((ack_mp = tcp_ack_mp(tcp)) != NULL) {
2656721fffe3SKacheong Poon 					if (tcp->tcp_ack_tid != 0) {
2657721fffe3SKacheong Poon 						(void) TCP_TIMER_CANCEL(tcp,
2658721fffe3SKacheong Poon 						    tcp->tcp_ack_tid);
2659721fffe3SKacheong Poon 						tcp->tcp_ack_tid = 0;
2660721fffe3SKacheong Poon 					}
2661721fffe3SKacheong Poon 					tcp_send_data(tcp, ack_mp);
2662721fffe3SKacheong Poon 					BUMP_LOCAL(tcp->tcp_obsegs);
2663721fffe3SKacheong Poon 					TCPS_BUMP_MIB(tcps, tcpOutAck);
2664721fffe3SKacheong Poon 
2665721fffe3SKacheong Poon 					if (!IPCL_IS_NONSTR(connp)) {
2666721fffe3SKacheong Poon 						/* Send up T_CONN_CON */
2667721fffe3SKacheong Poon 						if (ira->ira_cred != NULL) {
2668721fffe3SKacheong Poon 							mblk_setcred(mp1,
2669721fffe3SKacheong Poon 							    ira->ira_cred,
2670721fffe3SKacheong Poon 							    ira->ira_cpid);
2671721fffe3SKacheong Poon 						}
2672721fffe3SKacheong Poon 						putnext(connp->conn_rq, mp1);
2673721fffe3SKacheong Poon 					} else {
2674f2297e92SDan McDonald 						(*sockupcalls->su_connected)
2675721fffe3SKacheong Poon 						    (connp->conn_upper_handle,
2676721fffe3SKacheong Poon 						    tcp->tcp_connid,
2677721fffe3SKacheong Poon 						    ira->ira_cred,
2678721fffe3SKacheong Poon 						    ira->ira_cpid);
2679721fffe3SKacheong Poon 						freemsg(mp1);
2680721fffe3SKacheong Poon 					}
2681721fffe3SKacheong Poon 
2682721fffe3SKacheong Poon 					freemsg(mp);
2683721fffe3SKacheong Poon 					return;
2684721fffe3SKacheong Poon 				}
2685721fffe3SKacheong Poon 				/*
2686721fffe3SKacheong Poon 				 * Forget fusion; we need to handle more
2687721fffe3SKacheong Poon 				 * complex cases below.  Send the deferred
2688721fffe3SKacheong Poon 				 * T_CONN_CON message upstream and proceed
2689721fffe3SKacheong Poon 				 * as usual.  Mark this tcp as not capable
2690721fffe3SKacheong Poon 				 * of fusion.
2691721fffe3SKacheong Poon 				 */
2692721fffe3SKacheong Poon 				TCP_STAT(tcps, tcp_fusion_unfusable);
2693721fffe3SKacheong Poon 				tcp->tcp_unfusable = B_TRUE;
2694721fffe3SKacheong Poon 				if (!IPCL_IS_NONSTR(connp)) {
2695721fffe3SKacheong Poon 					if (ira->ira_cred != NULL) {
2696721fffe3SKacheong Poon 						mblk_setcred(mp1, ira->ira_cred,
2697721fffe3SKacheong Poon 						    ira->ira_cpid);
2698721fffe3SKacheong Poon 					}
2699721fffe3SKacheong Poon 					putnext(connp->conn_rq, mp1);
2700721fffe3SKacheong Poon 				} else {
2701f2297e92SDan McDonald 					(*sockupcalls->su_connected)
2702721fffe3SKacheong Poon 					    (connp->conn_upper_handle,
2703721fffe3SKacheong Poon 					    tcp->tcp_connid, ira->ira_cred,
2704721fffe3SKacheong Poon 					    ira->ira_cpid);
2705721fffe3SKacheong Poon 					freemsg(mp1);
2706721fffe3SKacheong Poon 				}
2707721fffe3SKacheong Poon 			}
2708721fffe3SKacheong Poon 
2709721fffe3SKacheong Poon 			/*
2710721fffe3SKacheong Poon 			 * Check to see if there is data to be sent.  If
2711721fffe3SKacheong Poon 			 * yes, set the transmit flag.  Then check to see
2712721fffe3SKacheong Poon 			 * if received data processing needs to be done.
2713721fffe3SKacheong Poon 			 * If not, go straight to xmit_check.  This short
2714721fffe3SKacheong Poon 			 * cut is OK as we don't support T/TCP.
2715721fffe3SKacheong Poon 			 */
2716721fffe3SKacheong Poon 			if (tcp->tcp_unsent)
2717721fffe3SKacheong Poon 				flags |= TH_XMIT_NEEDED;
2718721fffe3SKacheong Poon 
2719721fffe3SKacheong Poon 			if (seg_len == 0 && !(flags & TH_URG)) {
2720721fffe3SKacheong Poon 				freemsg(mp);
2721721fffe3SKacheong Poon 				goto xmit_check;
2722721fffe3SKacheong Poon 			}
2723721fffe3SKacheong Poon 
2724721fffe3SKacheong Poon 			flags &= ~TH_SYN;
2725721fffe3SKacheong Poon 			seg_seq++;
2726721fffe3SKacheong Poon 			break;
2727721fffe3SKacheong Poon 		}
2728721fffe3SKacheong Poon 		tcp->tcp_state = TCPS_SYN_RCVD;
27299cd928feSAlan Maguire 		DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
27309cd928feSAlan Maguire 		    connp->conn_ixa, void_ip_t *, NULL, tcp_t *, tcp,
27319cd928feSAlan Maguire 		    tcph_t *, NULL, int32_t, TCPS_SYN_SENT);
2732721fffe3SKacheong Poon 		mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss,
2733721fffe3SKacheong Poon 		    NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE);
2734721fffe3SKacheong Poon 		if (mp1 != NULL) {
2735721fffe3SKacheong Poon 			tcp_send_data(tcp, mp1);
2736721fffe3SKacheong Poon 			TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
2737721fffe3SKacheong Poon 		}
2738721fffe3SKacheong Poon 		freemsg(mp);
2739721fffe3SKacheong Poon 		return;
2740721fffe3SKacheong Poon 	case TCPS_SYN_RCVD:
2741721fffe3SKacheong Poon 		if (flags & TH_ACK) {
274232c3e7eeSKacheong Poon 			uint32_t pinit_wnd;
274332c3e7eeSKacheong Poon 
2744721fffe3SKacheong Poon 			/*
2745721fffe3SKacheong Poon 			 * In this state, a SYN|ACK packet is either bogus
2746721fffe3SKacheong Poon 			 * because the other side must be ACKing our SYN which
2747721fffe3SKacheong Poon 			 * indicates it has seen the ACK for their SYN and
2748721fffe3SKacheong Poon 			 * shouldn't retransmit it or we're crossing SYNs
2749721fffe3SKacheong Poon 			 * on active open.
2750721fffe3SKacheong Poon 			 */
2751721fffe3SKacheong Poon 			if ((flags & TH_SYN) && !tcp->tcp_active_open) {
2752721fffe3SKacheong Poon 				freemsg(mp);
2753721fffe3SKacheong Poon 				tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn",
2754721fffe3SKacheong Poon 				    tcp, seg_ack, 0, TH_RST);
2755721fffe3SKacheong Poon 				return;
2756721fffe3SKacheong Poon 			}
2757721fffe3SKacheong Poon 			/*
2758721fffe3SKacheong Poon 			 * NOTE: RFC 793 pg. 72 says this should be
2759721fffe3SKacheong Poon 			 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt
2760721fffe3SKacheong Poon 			 * but that would mean we have an ack that ignored
2761721fffe3SKacheong Poon 			 * our SYN.
2762721fffe3SKacheong Poon 			 */
2763721fffe3SKacheong Poon 			if (SEQ_LEQ(seg_ack, tcp->tcp_suna) ||
2764721fffe3SKacheong Poon 			    SEQ_GT(seg_ack, tcp->tcp_snxt)) {
2765721fffe3SKacheong Poon 				freemsg(mp);
2766721fffe3SKacheong Poon 				tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack",
2767721fffe3SKacheong Poon 				    tcp, seg_ack, 0, TH_RST);
2768721fffe3SKacheong Poon 				return;
2769721fffe3SKacheong Poon 			}
2770721fffe3SKacheong Poon 			/*
2771721fffe3SKacheong Poon 			 * No sane TCP stack will send such a small window
2772721fffe3SKacheong Poon 			 * without receiving any data.  Just drop this invalid
2773721fffe3SKacheong Poon 			 * ACK.  We also shorten the abort timeout in case
2774721fffe3SKacheong Poon 			 * this is an attack.
2775721fffe3SKacheong Poon 			 */
277632c3e7eeSKacheong Poon 			pinit_wnd = ntohs(tcpha->tha_win) << tcp->tcp_snd_ws;
277732c3e7eeSKacheong Poon 			if (pinit_wnd < tcp->tcp_mss &&
277832c3e7eeSKacheong Poon 			    pinit_wnd < tcp_init_wnd_chk) {
2779721fffe3SKacheong Poon 				freemsg(mp);
2780721fffe3SKacheong Poon 				TCP_STAT(tcps, tcp_zwin_ack_syn);
2781721fffe3SKacheong Poon 				tcp->tcp_second_ctimer_threshold =
2782721fffe3SKacheong Poon 				    tcp_early_abort * SECONDS;
2783721fffe3SKacheong Poon 				return;
2784721fffe3SKacheong Poon 			}
2785721fffe3SKacheong Poon 		}
2786721fffe3SKacheong Poon 		break;
2787721fffe3SKacheong Poon 	case TCPS_LISTEN:
2788721fffe3SKacheong Poon 		/*
2789721fffe3SKacheong Poon 		 * Only a TLI listener can come through this path when a
2790721fffe3SKacheong Poon 		 * acceptor is going back to be a listener and a packet
2791721fffe3SKacheong Poon 		 * for the acceptor hits the classifier. For a socket
2792721fffe3SKacheong Poon 		 * listener, this can never happen because a listener
2793721fffe3SKacheong Poon 		 * can never accept connection on itself and hence a
2794721fffe3SKacheong Poon 		 * socket acceptor can not go back to being a listener.
2795721fffe3SKacheong Poon 		 */
2796721fffe3SKacheong Poon 		ASSERT(!TCP_IS_SOCKET(tcp));
2797721fffe3SKacheong Poon 		/*FALLTHRU*/
2798721fffe3SKacheong Poon 	case TCPS_CLOSED:
2799721fffe3SKacheong Poon 	case TCPS_BOUND: {
2800721fffe3SKacheong Poon 		conn_t	*new_connp;
2801721fffe3SKacheong Poon 		ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip;
2802721fffe3SKacheong Poon 
2803721fffe3SKacheong Poon 		/*
2804721fffe3SKacheong Poon 		 * Don't accept any input on a closed tcp as this TCP logically
2805721fffe3SKacheong Poon 		 * does not exist on the system. Don't proceed further with
2806721fffe3SKacheong Poon 		 * this TCP. For instance, this packet could trigger another
2807721fffe3SKacheong Poon 		 * close of this tcp which would be disastrous for tcp_refcnt.
2808721fffe3SKacheong Poon 		 * tcp_close_detached / tcp_clean_death / tcp_closei_local must
2809721fffe3SKacheong Poon 		 * be called at most once on a TCP. In this case we need to
2810721fffe3SKacheong Poon 		 * refeed the packet into the classifier and figure out where
2811721fffe3SKacheong Poon 		 * the packet should go.
2812721fffe3SKacheong Poon 		 */
2813721fffe3SKacheong Poon 		new_connp = ipcl_classify(mp, ira, ipst);
2814721fffe3SKacheong Poon 		if (new_connp != NULL) {
2815721fffe3SKacheong Poon 			/* Drops ref on new_connp */
2816721fffe3SKacheong Poon 			tcp_reinput(new_connp, mp, ira, ipst);
2817721fffe3SKacheong Poon 			return;
2818721fffe3SKacheong Poon 		}
2819721fffe3SKacheong Poon 		/* We failed to classify. For now just drop the packet */
2820721fffe3SKacheong Poon 		freemsg(mp);
2821721fffe3SKacheong Poon 		return;
2822721fffe3SKacheong Poon 	}
2823721fffe3SKacheong Poon 	case TCPS_IDLE:
2824721fffe3SKacheong Poon 		/*
2825721fffe3SKacheong Poon 		 * Handle the case where the tcp_clean_death() has happened
2826721fffe3SKacheong Poon 		 * on a connection (application hasn't closed yet) but a packet
2827721fffe3SKacheong Poon 		 * was already queued on squeue before tcp_clean_death()
2828721fffe3SKacheong Poon 		 * was processed. Calling tcp_clean_death() twice on same
2829721fffe3SKacheong Poon 		 * connection can result in weird behaviour.
2830721fffe3SKacheong Poon 		 */
2831721fffe3SKacheong Poon 		freemsg(mp);
2832721fffe3SKacheong Poon 		return;
2833721fffe3SKacheong Poon 	default:
2834721fffe3SKacheong Poon 		break;
2835721fffe3SKacheong Poon 	}
2836721fffe3SKacheong Poon 
2837721fffe3SKacheong Poon 	/*
2838721fffe3SKacheong Poon 	 * Already on the correct queue/perimeter.
2839721fffe3SKacheong Poon 	 * If this is a detached connection and not an eager
2840721fffe3SKacheong Poon 	 * connection hanging off a listener then new data
2841721fffe3SKacheong Poon 	 * (past the FIN) will cause a reset.
2842721fffe3SKacheong Poon 	 * We do a special check here where it
2843721fffe3SKacheong Poon 	 * is out of the main line, rather than check
2844721fffe3SKacheong Poon 	 * if we are detached every time we see new
2845721fffe3SKacheong Poon 	 * data down below.
2846721fffe3SKacheong Poon 	 */
2847721fffe3SKacheong Poon 	if (TCP_IS_DETACHED_NONEAGER(tcp) &&
2848721fffe3SKacheong Poon 	    (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) {
2849721fffe3SKacheong Poon 		TCPS_BUMP_MIB(tcps, tcpInClosed);
2850721fffe3SKacheong Poon 		DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp);
2851721fffe3SKacheong Poon 		freemsg(mp);
2852721fffe3SKacheong Poon 		tcp_xmit_ctl("new data when detached", tcp,
2853721fffe3SKacheong Poon 		    tcp->tcp_snxt, 0, TH_RST);
2854721fffe3SKacheong Poon 		(void) tcp_clean_death(tcp, EPROTO);
2855721fffe3SKacheong Poon 		return;
2856721fffe3SKacheong Poon 	}
2857721fffe3SKacheong Poon 
2858721fffe3SKacheong Poon 	mp->b_rptr = (uchar_t *)tcpha + TCP_HDR_LENGTH(tcpha);
2859721fffe3SKacheong Poon 	urp = ntohs(tcpha->tha_urp) - TCP_OLD_URP_INTERPRETATION;
2860721fffe3SKacheong Poon 	new_swnd = ntohs(tcpha->tha_win) <<
2861721fffe3SKacheong Poon 	    ((tcpha->tha_flags & TH_SYN) ? 0 : tcp->tcp_snd_ws);
2862721fffe3SKacheong Poon 
2863*1f183ba0SLauri Tirkkonen 	/*
2864*1f183ba0SLauri Tirkkonen 	 * We are interested in two TCP options: timestamps (if negotiated) and
2865*1f183ba0SLauri Tirkkonen 	 * SACK (if negotiated). Skip option parsing if neither is negotiated.
2866*1f183ba0SLauri Tirkkonen 	 */
2867*1f183ba0SLauri Tirkkonen 	if (tcp->tcp_snd_ts_ok || tcp->tcp_snd_sack_ok) {
2868*1f183ba0SLauri Tirkkonen 		int options;
2869*1f183ba0SLauri Tirkkonen 		if (tcp->tcp_snd_sack_ok)
2870*1f183ba0SLauri Tirkkonen 			tcpopt.tcp = tcp;
2871*1f183ba0SLauri Tirkkonen 		else
2872*1f183ba0SLauri Tirkkonen 			tcpopt.tcp = NULL;
2873*1f183ba0SLauri Tirkkonen 		options = tcp_parse_options(tcpha, &tcpopt);
2874*1f183ba0SLauri Tirkkonen 		/*
2875*1f183ba0SLauri Tirkkonen 		 * RST segments must not be subject to PAWS and are not
2876*1f183ba0SLauri Tirkkonen 		 * required to have timestamps.
2877*1f183ba0SLauri Tirkkonen 		 */
2878*1f183ba0SLauri Tirkkonen 		if (tcp->tcp_snd_ts_ok && !(flags & TH_RST)) {
2879*1f183ba0SLauri Tirkkonen 			/*
2880*1f183ba0SLauri Tirkkonen 			 * Per RFC 7323 section 3.2., silently drop non-RST
2881*1f183ba0SLauri Tirkkonen 			 * segments without expected TSopt. This is a 'SHOULD'
2882*1f183ba0SLauri Tirkkonen 			 * requirement.
2883*1f183ba0SLauri Tirkkonen 			 */
2884*1f183ba0SLauri Tirkkonen 			if (!(options & TCP_OPT_TSTAMP_PRESENT)) {
2885*1f183ba0SLauri Tirkkonen 				/*
2886*1f183ba0SLauri Tirkkonen 				 * Leave a breadcrumb for people to detect this
2887*1f183ba0SLauri Tirkkonen 				 * behavior.
2888*1f183ba0SLauri Tirkkonen 				 */
2889*1f183ba0SLauri Tirkkonen 				DTRACE_TCP1(droppedtimestamp, tcp_t *, tcp);
2890*1f183ba0SLauri Tirkkonen 				freemsg(mp);
2891*1f183ba0SLauri Tirkkonen 				return;
2892*1f183ba0SLauri Tirkkonen 			}
2893*1f183ba0SLauri Tirkkonen 
2894*1f183ba0SLauri Tirkkonen 			if (!tcp_paws_check(tcp, &tcpopt)) {
2895721fffe3SKacheong Poon 				/*
2896721fffe3SKacheong Poon 				 * This segment is not acceptable.
2897721fffe3SKacheong Poon 				 * Drop it and send back an ACK.
2898721fffe3SKacheong Poon 				 */
2899721fffe3SKacheong Poon 				freemsg(mp);
2900721fffe3SKacheong Poon 				flags |= TH_ACK_NEEDED;
2901721fffe3SKacheong Poon 				goto ack_check;
2902721fffe3SKacheong Poon 			}
2903*1f183ba0SLauri Tirkkonen 		}
2904721fffe3SKacheong Poon 	}
2905721fffe3SKacheong Poon try_again:;
2906721fffe3SKacheong Poon 	mss = tcp->tcp_mss;
2907721fffe3SKacheong Poon 	gap = seg_seq - tcp->tcp_rnxt;
2908721fffe3SKacheong Poon 	rgap = tcp->tcp_rwnd - (gap + seg_len);
2909721fffe3SKacheong Poon 	/*
2910721fffe3SKacheong Poon 	 * gap is the amount of sequence space between what we expect to see
2911721fffe3SKacheong Poon 	 * and what we got for seg_seq.  A positive value for gap means
2912721fffe3SKacheong Poon 	 * something got lost.  A negative value means we got some old stuff.
2913721fffe3SKacheong Poon 	 */
2914721fffe3SKacheong Poon 	if (gap < 0) {
2915721fffe3SKacheong Poon 		/* Old stuff present.  Is the SYN in there? */
2916721fffe3SKacheong Poon 		if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) &&
2917721fffe3SKacheong Poon 		    (seg_len != 0)) {
2918721fffe3SKacheong Poon 			flags &= ~TH_SYN;
2919721fffe3SKacheong Poon 			seg_seq++;
2920721fffe3SKacheong Poon 			urp--;
2921721fffe3SKacheong Poon 			/* Recompute the gaps after noting the SYN. */
2922721fffe3SKacheong Poon 			goto try_again;
2923721fffe3SKacheong Poon 		}
2924721fffe3SKacheong Poon 		TCPS_BUMP_MIB(tcps, tcpInDataDupSegs);
2925721fffe3SKacheong Poon 		TCPS_UPDATE_MIB(tcps, tcpInDataDupBytes,
2926721fffe3SKacheong Poon 		    (seg_len > -gap ? -gap : seg_len));
2927721fffe3SKacheong Poon 		/* Remove the old stuff from seg_len. */
2928721fffe3SKacheong Poon 		seg_len += gap;
2929721fffe3SKacheong Poon 		/*
2930721fffe3SKacheong Poon 		 * Anything left?
2931721fffe3SKacheong Poon 		 * Make sure to check for unack'd FIN when rest of data
2932721fffe3SKacheong Poon 		 * has been previously ack'd.
2933721fffe3SKacheong Poon 		 */
2934721fffe3SKacheong Poon 		if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) {
2935721fffe3SKacheong Poon 			/*
2936721fffe3SKacheong Poon 			 * Resets are only valid if they lie within our offered
2937721fffe3SKacheong Poon 			 * window.  If the RST bit is set, we just ignore this
2938721fffe3SKacheong Poon 			 * segment.
2939721fffe3SKacheong Poon 			 */
2940721fffe3SKacheong Poon 			if (flags & TH_RST) {
2941721fffe3SKacheong Poon 				freemsg(mp);
2942721fffe3SKacheong Poon 				return;
2943721fffe3SKacheong Poon 			}
2944721fffe3SKacheong Poon 
2945721fffe3SKacheong Poon 			/*
2946721fffe3SKacheong Poon 			 * The arriving of dup data packets indicate that we
2947721fffe3SKacheong Poon 			 * may have postponed an ack for too long, or the other
2948721fffe3SKacheong Poon 			 * side's RTT estimate is out of shape. Start acking
2949721fffe3SKacheong Poon 			 * more often.
2950721fffe3SKacheong Poon 			 */
2951721fffe3SKacheong Poon 			if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) &&
2952721fffe3SKacheong Poon 			    tcp->tcp_rack_cnt >= 1 &&
2953721fffe3SKacheong Poon 			    tcp->tcp_rack_abs_max > 2) {
2954721fffe3SKacheong Poon 				tcp->tcp_rack_abs_max--;
2955721fffe3SKacheong Poon 			}
2956721fffe3SKacheong Poon 			tcp->tcp_rack_cur_max = 1;
2957721fffe3SKacheong Poon 
2958721fffe3SKacheong Poon 			/*
2959721fffe3SKacheong Poon 			 * This segment is "unacceptable".  None of its
2960721fffe3SKacheong Poon 			 * sequence space lies within our advertized window.
2961721fffe3SKacheong Poon 			 *
2962721fffe3SKacheong Poon 			 * Adjust seg_len to the original value for tracing.
2963721fffe3SKacheong Poon 			 */
2964721fffe3SKacheong Poon 			seg_len -= gap;
2965721fffe3SKacheong Poon 			if (connp->conn_debug) {
2966721fffe3SKacheong Poon 				(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
2967721fffe3SKacheong Poon 				    "tcp_rput: unacceptable, gap %d, rgap %d, "
2968721fffe3SKacheong Poon 				    "flags 0x%x, seg_seq %u, seg_ack %u, "
2969721fffe3SKacheong Poon 				    "seg_len %d, rnxt %u, snxt %u, %s",
2970721fffe3SKacheong Poon 				    gap, rgap, flags, seg_seq, seg_ack,
2971721fffe3SKacheong Poon 				    seg_len, tcp->tcp_rnxt, tcp->tcp_snxt,
2972721fffe3SKacheong Poon 				    tcp_display(tcp, NULL,
2973721fffe3SKacheong Poon 				    DISP_ADDR_AND_PORT));
2974721fffe3SKacheong Poon 			}
2975721fffe3SKacheong Poon 
2976721fffe3SKacheong Poon 			/*
2977721fffe3SKacheong Poon 			 * Arrange to send an ACK in response to the
2978721fffe3SKacheong Poon 			 * unacceptable segment per RFC 793 page 69. There
2979721fffe3SKacheong Poon 			 * is only one small difference between ours and the
2980721fffe3SKacheong Poon 			 * acceptability test in the RFC - we accept ACK-only
2981721fffe3SKacheong Poon 			 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK
2982721fffe3SKacheong Poon 			 * will be generated.
2983721fffe3SKacheong Poon 			 *
2984721fffe3SKacheong Poon 			 * Note that we have to ACK an ACK-only packet at least
2985721fffe3SKacheong Poon 			 * for stacks that send 0-length keep-alives with
2986721fffe3SKacheong Poon 			 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122,
2987721fffe3SKacheong Poon 			 * section 4.2.3.6. As long as we don't ever generate
2988721fffe3SKacheong Poon 			 * an unacceptable packet in response to an incoming
2989721fffe3SKacheong Poon 			 * packet that is unacceptable, it should not cause
2990721fffe3SKacheong Poon 			 * "ACK wars".
2991721fffe3SKacheong Poon 			 */
2992721fffe3SKacheong Poon 			flags |=  TH_ACK_NEEDED;
2993721fffe3SKacheong Poon 
2994721fffe3SKacheong Poon 			/*
2995721fffe3SKacheong Poon 			 * Continue processing this segment in order to use the
2996721fffe3SKacheong Poon 			 * ACK information it contains, but skip all other
2997721fffe3SKacheong Poon 			 * sequence-number processing.	Processing the ACK
2998721fffe3SKacheong Poon 			 * information is necessary in order to
2999721fffe3SKacheong Poon 			 * re-synchronize connections that may have lost
3000721fffe3SKacheong Poon 			 * synchronization.
3001721fffe3SKacheong Poon 			 *
3002721fffe3SKacheong Poon 			 * We clear seg_len and flag fields related to
3003721fffe3SKacheong Poon 			 * sequence number processing as they are not
3004721fffe3SKacheong Poon 			 * to be trusted for an unacceptable segment.
3005721fffe3SKacheong Poon 			 */
3006721fffe3SKacheong Poon 			seg_len = 0;
3007721fffe3SKacheong Poon 			flags &= ~(TH_SYN | TH_FIN | TH_URG);
3008721fffe3SKacheong Poon 			goto process_ack;
3009721fffe3SKacheong Poon 		}
3010721fffe3SKacheong Poon 
3011721fffe3SKacheong Poon 		/* Fix seg_seq, and chew the gap off the front. */
3012721fffe3SKacheong Poon 		seg_seq = tcp->tcp_rnxt;
3013721fffe3SKacheong Poon 		urp += gap;
3014721fffe3SKacheong Poon 		do {
3015721fffe3SKacheong Poon 			mblk_t	*mp2;
3016721fffe3SKacheong Poon 			ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
3017721fffe3SKacheong Poon 			    (uintptr_t)UINT_MAX);
3018721fffe3SKacheong Poon 			gap += (uint_t)(mp->b_wptr - mp->b_rptr);
3019721fffe3SKacheong Poon 			if (gap > 0) {
3020721fffe3SKacheong Poon 				mp->b_rptr = mp->b_wptr - gap;
3021721fffe3SKacheong Poon 				break;
3022721fffe3SKacheong Poon 			}
3023721fffe3SKacheong Poon 			mp2 = mp;
3024721fffe3SKacheong Poon 			mp = mp->b_cont;
3025721fffe3SKacheong Poon 			freeb(mp2);
3026721fffe3SKacheong Poon 		} while (gap < 0);
3027721fffe3SKacheong Poon 		/*
3028721fffe3SKacheong Poon 		 * If the urgent data has already been acknowledged, we
3029721fffe3SKacheong Poon 		 * should ignore TH_URG below
3030721fffe3SKacheong Poon 		 */
3031721fffe3SKacheong Poon 		if (urp < 0)
3032721fffe3SKacheong Poon 			flags &= ~TH_URG;
3033721fffe3SKacheong Poon 	}
3034721fffe3SKacheong Poon 	/*
3035721fffe3SKacheong Poon 	 * rgap is the amount of stuff received out of window.  A negative
3036721fffe3SKacheong Poon 	 * value is the amount out of window.
3037721fffe3SKacheong Poon 	 */
3038721fffe3SKacheong Poon 	if (rgap < 0) {
3039721fffe3SKacheong Poon 		mblk_t	*mp2;
3040721fffe3SKacheong Poon 
3041721fffe3SKacheong Poon 		if (tcp->tcp_rwnd == 0) {
3042721fffe3SKacheong Poon 			TCPS_BUMP_MIB(tcps, tcpInWinProbe);
3043721fffe3SKacheong Poon 		} else {
3044721fffe3SKacheong Poon 			TCPS_BUMP_MIB(tcps, tcpInDataPastWinSegs);
3045721fffe3SKacheong Poon 			TCPS_UPDATE_MIB(tcps, tcpInDataPastWinBytes, -rgap);
3046721fffe3SKacheong Poon 		}
3047721fffe3SKacheong Poon 
3048721fffe3SKacheong Poon 		/*
3049721fffe3SKacheong Poon 		 * seg_len does not include the FIN, so if more than
3050721fffe3SKacheong Poon 		 * just the FIN is out of window, we act like we don't
3051721fffe3SKacheong Poon 		 * see it.  (If just the FIN is out of window, rgap
3052721fffe3SKacheong Poon 		 * will be zero and we will go ahead and acknowledge
3053721fffe3SKacheong Poon 		 * the FIN.)
3054721fffe3SKacheong Poon 		 */
3055721fffe3SKacheong Poon 		flags &= ~TH_FIN;
3056721fffe3SKacheong Poon 
3057721fffe3SKacheong Poon 		/* Fix seg_len and make sure there is something left. */
3058721fffe3SKacheong Poon 		seg_len += rgap;
3059721fffe3SKacheong Poon 		if (seg_len <= 0) {
3060721fffe3SKacheong Poon 			/*
3061721fffe3SKacheong Poon 			 * Resets are only valid if they lie within our offered
3062721fffe3SKacheong Poon 			 * window.  If the RST bit is set, we just ignore this
3063721fffe3SKacheong Poon 			 * segment.
3064721fffe3SKacheong Poon 			 */
3065721fffe3SKacheong Poon 			if (flags & TH_RST) {
3066721fffe3SKacheong Poon 				freemsg(mp);
3067721fffe3SKacheong Poon 				return;
3068721fffe3SKacheong Poon 			}
3069721fffe3SKacheong Poon 
3070721fffe3SKacheong Poon 			/* Per RFC 793, we need to send back an ACK. */
3071721fffe3SKacheong Poon 			flags |= TH_ACK_NEEDED;
3072721fffe3SKacheong Poon 
3073721fffe3SKacheong Poon 			/*
3074721fffe3SKacheong Poon 			 * Send SIGURG as soon as possible i.e. even
3075721fffe3SKacheong Poon 			 * if the TH_URG was delivered in a window probe
3076721fffe3SKacheong Poon 			 * packet (which will be unacceptable).
3077721fffe3SKacheong Poon 			 *
3078721fffe3SKacheong Poon 			 * We generate a signal if none has been generated
3079721fffe3SKacheong Poon 			 * for this connection or if this is a new urgent
3080721fffe3SKacheong Poon 			 * byte. Also send a zero-length "unmarked" message
3081721fffe3SKacheong Poon 			 * to inform SIOCATMARK that this is not the mark.
3082721fffe3SKacheong Poon 			 *
3083721fffe3SKacheong Poon 			 * tcp_urp_last_valid is cleared when the T_exdata_ind
3084721fffe3SKacheong Poon 			 * is sent up. This plus the check for old data
3085721fffe3SKacheong Poon 			 * (gap >= 0) handles the wraparound of the sequence
3086721fffe3SKacheong Poon 			 * number space without having to always track the
3087721fffe3SKacheong Poon 			 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks
3088721fffe3SKacheong Poon 			 * this max in its rcv_up variable).
3089721fffe3SKacheong Poon 			 *
3090721fffe3SKacheong Poon 			 * This prevents duplicate SIGURGS due to a "late"
3091721fffe3SKacheong Poon 			 * zero-window probe when the T_EXDATA_IND has already
3092721fffe3SKacheong Poon 			 * been sent up.
3093721fffe3SKacheong Poon 			 */
3094721fffe3SKacheong Poon 			if ((flags & TH_URG) &&
3095721fffe3SKacheong Poon 			    (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq,
3096721fffe3SKacheong Poon 			    tcp->tcp_urp_last))) {
3097721fffe3SKacheong Poon 				if (IPCL_IS_NONSTR(connp)) {
3098721fffe3SKacheong Poon 					if (!TCP_IS_DETACHED(tcp)) {
3099f2297e92SDan McDonald 						(*sockupcalls->su_signal_oob)
3100721fffe3SKacheong Poon 						    (connp->conn_upper_handle,
3101721fffe3SKacheong Poon 						    urp);
3102721fffe3SKacheong Poon 					}
3103721fffe3SKacheong Poon 				} else {
3104721fffe3SKacheong Poon 					mp1 = allocb(0, BPRI_MED);
3105721fffe3SKacheong Poon 					if (mp1 == NULL) {
3106721fffe3SKacheong Poon 						freemsg(mp);
3107721fffe3SKacheong Poon 						return;
3108721fffe3SKacheong Poon 					}
3109721fffe3SKacheong Poon 					if (!TCP_IS_DETACHED(tcp) &&
3110721fffe3SKacheong Poon 					    !putnextctl1(connp->conn_rq,
3111721fffe3SKacheong Poon 					    M_PCSIG, SIGURG)) {
3112721fffe3SKacheong Poon 						/* Try again on the rexmit. */
3113721fffe3SKacheong Poon 						freemsg(mp1);
3114721fffe3SKacheong Poon 						freemsg(mp);
3115721fffe3SKacheong Poon 						return;
3116721fffe3SKacheong Poon 					}
3117721fffe3SKacheong Poon 					/*
3118721fffe3SKacheong Poon 					 * If the next byte would be the mark
3119721fffe3SKacheong Poon 					 * then mark with MARKNEXT else mark
3120721fffe3SKacheong Poon 					 * with NOTMARKNEXT.
3121721fffe3SKacheong Poon 					 */
3122721fffe3SKacheong Poon 					if (gap == 0 && urp == 0)
3123721fffe3SKacheong Poon 						mp1->b_flag |= MSGMARKNEXT;
3124721fffe3SKacheong Poon 					else
3125721fffe3SKacheong Poon 						mp1->b_flag |= MSGNOTMARKNEXT;
3126721fffe3SKacheong Poon 					freemsg(tcp->tcp_urp_mark_mp);
3127721fffe3SKacheong Poon 					tcp->tcp_urp_mark_mp = mp1;
3128721fffe3SKacheong Poon 					flags |= TH_SEND_URP_MARK;
3129721fffe3SKacheong Poon 				}
3130721fffe3SKacheong Poon 				tcp->tcp_urp_last_valid = B_TRUE;
3131721fffe3SKacheong Poon 				tcp->tcp_urp_last = urp + seg_seq;
3132721fffe3SKacheong Poon 			}
3133721fffe3SKacheong Poon 			/*
3134721fffe3SKacheong Poon 			 * If this is a zero window probe, continue to
3135721fffe3SKacheong Poon 			 * process the ACK part.  But we need to set seg_len
3136721fffe3SKacheong Poon 			 * to 0 to avoid data processing.  Otherwise just
3137721fffe3SKacheong Poon 			 * drop the segment and send back an ACK.
3138721fffe3SKacheong Poon 			 */
3139721fffe3SKacheong Poon 			if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) {
3140721fffe3SKacheong Poon 				flags &= ~(TH_SYN | TH_URG);
3141721fffe3SKacheong Poon 				seg_len = 0;
3142721fffe3SKacheong Poon 				goto process_ack;
3143721fffe3SKacheong Poon 			} else {
3144721fffe3SKacheong Poon 				freemsg(mp);
3145721fffe3SKacheong Poon 				goto ack_check;
3146721fffe3SKacheong Poon 			}
3147721fffe3SKacheong Poon 		}
3148721fffe3SKacheong Poon 		/* Pitch out of window stuff off the end. */
3149721fffe3SKacheong Poon 		rgap = seg_len;
3150721fffe3SKacheong Poon 		mp2 = mp;
3151721fffe3SKacheong Poon 		do {
3152721fffe3SKacheong Poon 			ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <=
3153721fffe3SKacheong Poon 			    (uintptr_t)INT_MAX);
3154721fffe3SKacheong Poon 			rgap -= (int)(mp2->b_wptr - mp2->b_rptr);
3155721fffe3SKacheong Poon 			if (rgap < 0) {
3156721fffe3SKacheong Poon 				mp2->b_wptr += rgap;
3157721fffe3SKacheong Poon 				if ((mp1 = mp2->b_cont) != NULL) {
3158721fffe3SKacheong Poon 					mp2->b_cont = NULL;
3159721fffe3SKacheong Poon 					freemsg(mp1);
3160721fffe3SKacheong Poon 				}
3161721fffe3SKacheong Poon 				break;
3162721fffe3SKacheong Poon 			}
3163721fffe3SKacheong Poon 		} while ((mp2 = mp2->b_cont) != NULL);
3164721fffe3SKacheong Poon 	}
3165721fffe3SKacheong Poon ok:;
3166721fffe3SKacheong Poon 	/*
3167721fffe3SKacheong Poon 	 * TCP should check ECN info for segments inside the window only.
3168721fffe3SKacheong Poon 	 * Therefore the check should be done here.
3169721fffe3SKacheong Poon 	 */
3170721fffe3SKacheong Poon 	if (tcp->tcp_ecn_ok) {
3171721fffe3SKacheong Poon 		if (flags & TH_CWR) {
3172721fffe3SKacheong Poon 			tcp->tcp_ecn_echo_on = B_FALSE;
3173721fffe3SKacheong Poon 		}
3174721fffe3SKacheong Poon 		/*
3175721fffe3SKacheong Poon 		 * Note that both ECN_CE and CWR can be set in the
3176721fffe3SKacheong Poon 		 * same segment.  In this case, we once again turn
3177721fffe3SKacheong Poon 		 * on ECN_ECHO.
3178721fffe3SKacheong Poon 		 */
3179721fffe3SKacheong Poon 		if (connp->conn_ipversion == IPV4_VERSION) {
3180721fffe3SKacheong Poon 			uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service;
3181721fffe3SKacheong Poon 
3182721fffe3SKacheong Poon 			if ((tos & IPH_ECN_CE) == IPH_ECN_CE) {
3183721fffe3SKacheong Poon 				tcp->tcp_ecn_echo_on = B_TRUE;
3184721fffe3SKacheong Poon 			}
3185721fffe3SKacheong Poon 		} else {
3186721fffe3SKacheong Poon 			uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf;
3187721fffe3SKacheong Poon 
3188721fffe3SKacheong Poon 			if ((vcf & htonl(IPH_ECN_CE << 20)) ==
3189721fffe3SKacheong Poon 			    htonl(IPH_ECN_CE << 20)) {
3190721fffe3SKacheong Poon 				tcp->tcp_ecn_echo_on = B_TRUE;
3191721fffe3SKacheong Poon 			}
3192721fffe3SKacheong Poon 		}
3193721fffe3SKacheong Poon 	}
3194721fffe3SKacheong Poon 
3195721fffe3SKacheong Poon 	/*
3196*1f183ba0SLauri Tirkkonen 	 * Check whether we can update tcp_ts_recent. This test is from RFC
3197*1f183ba0SLauri Tirkkonen 	 * 7323, section 5.3.
3198721fffe3SKacheong Poon 	 */
3199*1f183ba0SLauri Tirkkonen 	if (tcp->tcp_snd_ts_ok && !(flags & TH_RST) &&
3200721fffe3SKacheong Poon 	    TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) &&
3201721fffe3SKacheong Poon 	    SEQ_LEQ(seg_seq, tcp->tcp_rack)) {
3202721fffe3SKacheong Poon 		tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val;
3203721fffe3SKacheong Poon 		tcp->tcp_last_rcv_lbolt = LBOLT_FASTPATH64;
3204721fffe3SKacheong Poon 	}
3205721fffe3SKacheong Poon 
3206721fffe3SKacheong Poon 	if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) {
3207721fffe3SKacheong Poon 		/*
3208721fffe3SKacheong Poon 		 * FIN in an out of order segment.  We record this in
3209721fffe3SKacheong Poon 		 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq.
3210721fffe3SKacheong Poon 		 * Clear the FIN so that any check on FIN flag will fail.
3211721fffe3SKacheong Poon 		 * Remember that FIN also counts in the sequence number
3212721fffe3SKacheong Poon 		 * space.  So we need to ack out of order FIN only segments.
3213721fffe3SKacheong Poon 		 */
3214721fffe3SKacheong Poon 		if (flags & TH_FIN) {
3215721fffe3SKacheong Poon 			tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID;
3216721fffe3SKacheong Poon 			tcp->tcp_ofo_fin_seq = seg_seq + seg_len;
3217721fffe3SKacheong Poon 			flags &= ~TH_FIN;
3218721fffe3SKacheong Poon 			flags |= TH_ACK_NEEDED;
3219721fffe3SKacheong Poon 		}
3220721fffe3SKacheong Poon 		if (seg_len > 0) {
3221721fffe3SKacheong Poon 			/* Fill in the SACK blk list. */
3222721fffe3SKacheong Poon 			if (tcp->tcp_snd_sack_ok) {
3223721fffe3SKacheong Poon 				tcp_sack_insert(tcp->tcp_sack_list,
3224721fffe3SKacheong Poon 				    seg_seq, seg_seq + seg_len,
3225721fffe3SKacheong Poon 				    &(tcp->tcp_num_sack_blk));
3226721fffe3SKacheong Poon 			}
3227721fffe3SKacheong Poon 
3228721fffe3SKacheong Poon 			/*
3229721fffe3SKacheong Poon 			 * Attempt reassembly and see if we have something
3230721fffe3SKacheong Poon 			 * ready to go.
3231721fffe3SKacheong Poon 			 */
3232721fffe3SKacheong Poon 			mp = tcp_reass(tcp, mp, seg_seq);
3233721fffe3SKacheong Poon 			/* Always ack out of order packets */
3234721fffe3SKacheong Poon 			flags |= TH_ACK_NEEDED | TH_PUSH;
3235721fffe3SKacheong Poon 			if (mp) {
3236721fffe3SKacheong Poon 				ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
3237721fffe3SKacheong Poon 				    (uintptr_t)INT_MAX);
3238721fffe3SKacheong Poon 				seg_len = mp->b_cont ? msgdsize(mp) :
3239721fffe3SKacheong Poon 				    (int)(mp->b_wptr - mp->b_rptr);
3240721fffe3SKacheong Poon 				seg_seq = tcp->tcp_rnxt;
3241721fffe3SKacheong Poon 				/*
3242721fffe3SKacheong Poon 				 * A gap is filled and the seq num and len
3243721fffe3SKacheong Poon 				 * of the gap match that of a previously
3244721fffe3SKacheong Poon 				 * received FIN, put the FIN flag back in.
3245721fffe3SKacheong Poon 				 */
3246721fffe3SKacheong Poon 				if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) &&
3247721fffe3SKacheong Poon 				    seg_seq + seg_len == tcp->tcp_ofo_fin_seq) {
3248721fffe3SKacheong Poon 					flags |= TH_FIN;
3249721fffe3SKacheong Poon 					tcp->tcp_valid_bits &=
3250721fffe3SKacheong Poon 					    ~TCP_OFO_FIN_VALID;
3251721fffe3SKacheong Poon 				}
3252721fffe3SKacheong Poon 				if (tcp->tcp_reass_tid != 0) {
3253721fffe3SKacheong Poon 					(void) TCP_TIMER_CANCEL(tcp,
3254721fffe3SKacheong Poon 					    tcp->tcp_reass_tid);
3255721fffe3SKacheong Poon 					/*
3256721fffe3SKacheong Poon 					 * Restart the timer if there is still
3257721fffe3SKacheong Poon 					 * data in the reassembly queue.
3258721fffe3SKacheong Poon 					 */
3259721fffe3SKacheong Poon 					if (tcp->tcp_reass_head != NULL) {
3260721fffe3SKacheong Poon 						tcp->tcp_reass_tid = TCP_TIMER(
3261721fffe3SKacheong Poon 						    tcp, tcp_reass_timer,
326266cd0f60SKacheong Poon 						    tcps->tcps_reass_timeout);
3263721fffe3SKacheong Poon 					} else {
3264721fffe3SKacheong Poon 						tcp->tcp_reass_tid = 0;
3265721fffe3SKacheong Poon 					}
3266721fffe3SKacheong Poon 				}
3267721fffe3SKacheong Poon 			} else {
3268721fffe3SKacheong Poon 				/*
3269721fffe3SKacheong Poon 				 * Keep going even with NULL mp.
3270721fffe3SKacheong Poon 				 * There may be a useful ACK or something else
3271721fffe3SKacheong Poon 				 * we don't want to miss.
3272721fffe3SKacheong Poon 				 *
3273721fffe3SKacheong Poon 				 * But TCP should not perform fast retransmit
3274721fffe3SKacheong Poon 				 * because of the ack number.  TCP uses
3275721fffe3SKacheong Poon 				 * seg_len == 0 to determine if it is a pure
3276721fffe3SKacheong Poon 				 * ACK.  And this is not a pure ACK.
3277721fffe3SKacheong Poon 				 */
3278721fffe3SKacheong Poon 				seg_len = 0;
3279721fffe3SKacheong Poon 				ofo_seg = B_TRUE;
3280721fffe3SKacheong Poon 
3281721fffe3SKacheong Poon 				if (tcps->tcps_reass_timeout != 0 &&
3282721fffe3SKacheong Poon 				    tcp->tcp_reass_tid == 0) {
3283721fffe3SKacheong Poon 					tcp->tcp_reass_tid = TCP_TIMER(tcp,
328466cd0f60SKacheong Poon 					    tcp_reass_timer,
328566cd0f60SKacheong Poon 					    tcps->tcps_reass_timeout);
3286721fffe3SKacheong Poon 				}
3287721fffe3SKacheong Poon 			}
3288721fffe3SKacheong Poon 		}
3289721fffe3SKacheong Poon 	} else if (seg_len > 0) {
3290721fffe3SKacheong Poon 		TCPS_BUMP_MIB(tcps, tcpInDataInorderSegs);
3291721fffe3SKacheong Poon 		TCPS_UPDATE_MIB(tcps, tcpInDataInorderBytes, seg_len);
3292721fffe3SKacheong Poon 		/*
3293721fffe3SKacheong Poon 		 * If an out of order FIN was received before, and the seq
3294721fffe3SKacheong Poon 		 * num and len of the new segment match that of the FIN,
3295721fffe3SKacheong Poon 		 * put the FIN flag back in.
3296721fffe3SKacheong Poon 		 */
3297721fffe3SKacheong Poon 		if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) &&
3298721fffe3SKacheong Poon 		    seg_seq + seg_len == tcp->tcp_ofo_fin_seq) {
3299721fffe3SKacheong Poon 			flags |= TH_FIN;
3300721fffe3SKacheong Poon 			tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID;
3301721fffe3SKacheong Poon 		}
3302721fffe3SKacheong Poon 	}
3303721fffe3SKacheong Poon 	if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) {
3304721fffe3SKacheong Poon 	if (flags & TH_RST) {
3305721fffe3SKacheong Poon 		freemsg(mp);
3306721fffe3SKacheong Poon 		switch (tcp->tcp_state) {
3307721fffe3SKacheong Poon 		case TCPS_SYN_RCVD:
3308721fffe3SKacheong Poon 			(void) tcp_clean_death(tcp, ECONNREFUSED);
3309721fffe3SKacheong Poon 			break;
3310721fffe3SKacheong Poon 		case TCPS_ESTABLISHED:
3311721fffe3SKacheong Poon 		case TCPS_FIN_WAIT_1:
3312721fffe3SKacheong Poon 		case TCPS_FIN_WAIT_2:
3313721fffe3SKacheong Poon 		case TCPS_CLOSE_WAIT:
3314721fffe3SKacheong Poon 			(void) tcp_clean_death(tcp, ECONNRESET);
3315721fffe3SKacheong Poon 			break;
3316721fffe3SKacheong Poon 		case TCPS_CLOSING:
3317721fffe3SKacheong Poon 		case TCPS_LAST_ACK:
3318721fffe3SKacheong Poon 			(void) tcp_clean_death(tcp, 0);
3319721fffe3SKacheong Poon 			break;
3320721fffe3SKacheong Poon 		default:
3321721fffe3SKacheong Poon 			ASSERT(tcp->tcp_state != TCPS_TIME_WAIT);
3322721fffe3SKacheong Poon 			(void) tcp_clean_death(tcp, ENXIO);
3323721fffe3SKacheong Poon 			break;
3324721fffe3SKacheong Poon 		}
3325721fffe3SKacheong Poon 		return;
3326721fffe3SKacheong Poon 	}
3327721fffe3SKacheong Poon 	if (flags & TH_SYN) {
3328721fffe3SKacheong Poon 		/*
3329721fffe3SKacheong Poon 		 * See RFC 793, Page 71
3330721fffe3SKacheong Poon 		 *
3331721fffe3SKacheong Poon 		 * The seq number must be in the window as it should
3332721fffe3SKacheong Poon 		 * be "fixed" above.  If it is outside window, it should
3333721fffe3SKacheong Poon 		 * be already rejected.  Note that we allow seg_seq to be
3334721fffe3SKacheong Poon 		 * rnxt + rwnd because we want to accept 0 window probe.
3335721fffe3SKacheong Poon 		 */
3336721fffe3SKacheong Poon 		ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) &&
3337721fffe3SKacheong Poon 		    SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd));
3338721fffe3SKacheong Poon 		freemsg(mp);
3339721fffe3SKacheong Poon 		/*
3340721fffe3SKacheong Poon 		 * If the ACK flag is not set, just use our snxt as the
3341721fffe3SKacheong Poon 		 * seq number of the RST segment.
3342721fffe3SKacheong Poon 		 */
3343721fffe3SKacheong Poon 		if (!(flags & TH_ACK)) {
3344721fffe3SKacheong Poon 			seg_ack = tcp->tcp_snxt;
3345721fffe3SKacheong Poon 		}
3346721fffe3SKacheong Poon 		tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1,
3347721fffe3SKacheong Poon 		    TH_RST|TH_ACK);
3348721fffe3SKacheong Poon 		ASSERT(tcp->tcp_state != TCPS_TIME_WAIT);
3349721fffe3SKacheong Poon 		(void) tcp_clean_death(tcp, ECONNRESET);
3350721fffe3SKacheong Poon 		return;
3351721fffe3SKacheong Poon 	}
3352721fffe3SKacheong Poon 	/*
3353721fffe3SKacheong Poon 	 * urp could be -1 when the urp field in the packet is 0
3354721fffe3SKacheong Poon 	 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent
3355721fffe3SKacheong Poon 	 * byte was at seg_seq - 1, in which case we ignore the urgent flag.
3356721fffe3SKacheong Poon 	 */
3357721fffe3SKacheong Poon 	if (flags & TH_URG && urp >= 0) {
3358721fffe3SKacheong Poon 		if (!tcp->tcp_urp_last_valid ||
3359721fffe3SKacheong Poon 		    SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) {
3360721fffe3SKacheong Poon 			/*
3361721fffe3SKacheong Poon 			 * Non-STREAMS sockets handle the urgent data a litte
3362721fffe3SKacheong Poon 			 * differently from STREAMS based sockets. There is no
3363721fffe3SKacheong Poon 			 * need to mark any mblks with the MSG{NOT,}MARKNEXT
3364721fffe3SKacheong Poon 			 * flags to keep SIOCATMARK happy. Instead a
3365721fffe3SKacheong Poon 			 * su_signal_oob upcall is made to update the mark.
3366721fffe3SKacheong Poon 			 * Neither is a T_EXDATA_IND mblk needed to be
3367721fffe3SKacheong Poon 			 * prepended to the urgent data. The urgent data is
3368721fffe3SKacheong Poon 			 * delivered using the su_recv upcall, where we set
3369721fffe3SKacheong Poon 			 * the MSG_OOB flag to indicate that it is urg data.
3370721fffe3SKacheong Poon 			 *
3371721fffe3SKacheong Poon 			 * Neither TH_SEND_URP_MARK nor TH_MARKNEXT_NEEDED
3372721fffe3SKacheong Poon 			 * are used by non-STREAMS sockets.
3373721fffe3SKacheong Poon 			 */
3374721fffe3SKacheong Poon 			if (IPCL_IS_NONSTR(connp)) {
3375721fffe3SKacheong Poon 				if (!TCP_IS_DETACHED(tcp)) {
3376f2297e92SDan McDonald 					(*sockupcalls->su_signal_oob)
3377721fffe3SKacheong Poon 					    (connp->conn_upper_handle, urp);
3378721fffe3SKacheong Poon 				}
3379721fffe3SKacheong Poon 			} else {
3380721fffe3SKacheong Poon 				/*
3381721fffe3SKacheong Poon 				 * If we haven't generated the signal yet for
3382721fffe3SKacheong Poon 				 * this urgent pointer value, do it now.  Also,
3383721fffe3SKacheong Poon 				 * send up a zero-length M_DATA indicating
3384721fffe3SKacheong Poon 				 * whether or not this is the mark. The latter
3385721fffe3SKacheong Poon 				 * is not needed when a T_EXDATA_IND is sent up.
3386721fffe3SKacheong Poon 				 * However, if there are allocation failures
3387721fffe3SKacheong Poon 				 * this code relies on the sender retransmitting
3388721fffe3SKacheong Poon 				 * and the socket code for determining the mark
3389721fffe3SKacheong Poon 				 * should not block waiting for the peer to
3390721fffe3SKacheong Poon 				 * transmit. Thus, for simplicity we always
3391721fffe3SKacheong Poon 				 * send up the mark indication.
3392721fffe3SKacheong Poon 				 */
3393721fffe3SKacheong Poon 				mp1 = allocb(0, BPRI_MED);
3394721fffe3SKacheong Poon 				if (mp1 == NULL) {
3395721fffe3SKacheong Poon 					freemsg(mp);
3396721fffe3SKacheong Poon 					return;
3397721fffe3SKacheong Poon 				}
3398721fffe3SKacheong Poon 				if (!TCP_IS_DETACHED(tcp) &&
3399721fffe3SKacheong Poon 				    !putnextctl1(connp->conn_rq, M_PCSIG,
3400721fffe3SKacheong Poon 				    SIGURG)) {
3401721fffe3SKacheong Poon 					/* Try again on the rexmit. */
3402721fffe3SKacheong Poon 					freemsg(mp1);
3403721fffe3SKacheong Poon 					freemsg(mp);
3404721fffe3SKacheong Poon 					return;
3405721fffe3SKacheong Poon 				}
3406721fffe3SKacheong Poon 				/*
3407721fffe3SKacheong Poon 				 * Mark with NOTMARKNEXT for now.
3408721fffe3SKacheong Poon 				 * The code below will change this to MARKNEXT
3409721fffe3SKacheong Poon 				 * if we are at the mark.
3410721fffe3SKacheong Poon 				 *
3411721fffe3SKacheong Poon 				 * If there are allocation failures (e.g. in
3412721fffe3SKacheong Poon 				 * dupmsg below) the next time tcp_input_data
3413721fffe3SKacheong Poon 				 * sees the urgent segment it will send up the
3414721fffe3SKacheong Poon 				 * MSGMARKNEXT message.
3415721fffe3SKacheong Poon 				 */
3416721fffe3SKacheong Poon 				mp1->b_flag |= MSGNOTMARKNEXT;
3417721fffe3SKacheong Poon 				freemsg(tcp->tcp_urp_mark_mp);
3418721fffe3SKacheong Poon 				tcp->tcp_urp_mark_mp = mp1;
3419721fffe3SKacheong Poon 				flags |= TH_SEND_URP_MARK;
3420721fffe3SKacheong Poon #ifdef DEBUG
3421721fffe3SKacheong Poon 				(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
3422721fffe3SKacheong Poon 				    "tcp_rput: sent M_PCSIG 2 seq %x urp %x "
3423721fffe3SKacheong Poon 				    "last %x, %s",
3424721fffe3SKacheong Poon 				    seg_seq, urp, tcp->tcp_urp_last,
3425721fffe3SKacheong Poon 				    tcp_display(tcp, NULL, DISP_PORT_ONLY));
3426721fffe3SKacheong Poon #endif /* DEBUG */
3427721fffe3SKacheong Poon 			}
3428721fffe3SKacheong Poon 			tcp->tcp_urp_last_valid = B_TRUE;
3429721fffe3SKacheong Poon 			tcp->tcp_urp_last = urp + seg_seq;
3430721fffe3SKacheong Poon 		} else if (tcp->tcp_urp_mark_mp != NULL) {
3431721fffe3SKacheong Poon 			/*
3432721fffe3SKacheong Poon 			 * An allocation failure prevented the previous
3433721fffe3SKacheong Poon 			 * tcp_input_data from sending up the allocated
3434721fffe3SKacheong Poon 			 * MSG*MARKNEXT message - send it up this time
3435721fffe3SKacheong Poon 			 * around.
3436721fffe3SKacheong Poon 			 */
3437721fffe3SKacheong Poon 			flags |= TH_SEND_URP_MARK;
3438721fffe3SKacheong Poon 		}
3439721fffe3SKacheong Poon 
3440721fffe3SKacheong Poon 		/*
3441721fffe3SKacheong Poon 		 * If the urgent byte is in this segment, make sure that it is
3442721fffe3SKacheong Poon 		 * all by itself.  This makes it much easier to deal with the
3443721fffe3SKacheong Poon 		 * possibility of an allocation failure on the T_exdata_ind.
3444721fffe3SKacheong Poon 		 * Note that seg_len is the number of bytes in the segment, and
3445721fffe3SKacheong Poon 		 * urp is the offset into the segment of the urgent byte.
3446721fffe3SKacheong Poon 		 * urp < seg_len means that the urgent byte is in this segment.
3447721fffe3SKacheong Poon 		 */
3448721fffe3SKacheong Poon 		if (urp < seg_len) {
3449721fffe3SKacheong Poon 			if (seg_len != 1) {
3450721fffe3SKacheong Poon 				uint32_t  tmp_rnxt;
3451721fffe3SKacheong Poon 				/*
3452721fffe3SKacheong Poon 				 * Break it up and feed it back in.
3453721fffe3SKacheong Poon 				 * Re-attach the IP header.
3454721fffe3SKacheong Poon 				 */
3455721fffe3SKacheong Poon 				mp->b_rptr = iphdr;
3456721fffe3SKacheong Poon 				if (urp > 0) {
3457721fffe3SKacheong Poon 					/*
3458721fffe3SKacheong Poon 					 * There is stuff before the urgent
3459721fffe3SKacheong Poon 					 * byte.
3460721fffe3SKacheong Poon 					 */
3461721fffe3SKacheong Poon 					mp1 = dupmsg(mp);
3462721fffe3SKacheong Poon 					if (!mp1) {
3463721fffe3SKacheong Poon 						/*
3464721fffe3SKacheong Poon 						 * Trim from urgent byte on.
3465721fffe3SKacheong Poon 						 * The rest will come back.
3466721fffe3SKacheong Poon 						 */
3467721fffe3SKacheong Poon 						(void) adjmsg(mp,
3468721fffe3SKacheong Poon 						    urp - seg_len);
3469721fffe3SKacheong Poon 						tcp_input_data(connp,
3470721fffe3SKacheong Poon 						    mp, NULL, ira);
3471721fffe3SKacheong Poon 						return;
3472721fffe3SKacheong Poon 					}
3473721fffe3SKacheong Poon 					(void) adjmsg(mp1, urp - seg_len);
3474721fffe3SKacheong Poon 					/* Feed this piece back in. */
3475721fffe3SKacheong Poon 					tmp_rnxt = tcp->tcp_rnxt;
3476721fffe3SKacheong Poon 					tcp_input_data(connp, mp1, NULL, ira);
3477721fffe3SKacheong Poon 					/*
3478721fffe3SKacheong Poon 					 * If the data passed back in was not
3479721fffe3SKacheong Poon 					 * processed (ie: bad ACK) sending
3480721fffe3SKacheong Poon 					 * the remainder back in will cause a
3481721fffe3SKacheong Poon 					 * loop. In this case, drop the
3482721fffe3SKacheong Poon 					 * packet and let the sender try
3483721fffe3SKacheong Poon 					 * sending a good packet.
3484721fffe3SKacheong Poon 					 */
3485721fffe3SKacheong Poon 					if (tmp_rnxt == tcp->tcp_rnxt) {
3486721fffe3SKacheong Poon 						freemsg(mp);
3487721fffe3SKacheong Poon 						return;
3488721fffe3SKacheong Poon 					}
3489721fffe3SKacheong Poon 				}
3490721fffe3SKacheong Poon 				if (urp != seg_len - 1) {
3491721fffe3SKacheong Poon 					uint32_t  tmp_rnxt;
3492721fffe3SKacheong Poon 					/*
3493721fffe3SKacheong Poon 					 * There is stuff after the urgent
3494721fffe3SKacheong Poon 					 * byte.
3495721fffe3SKacheong Poon 					 */
3496721fffe3SKacheong Poon 					mp1 = dupmsg(mp);
3497721fffe3SKacheong Poon 					if (!mp1) {
3498721fffe3SKacheong Poon 						/*
3499721fffe3SKacheong Poon 						 * Trim everything beyond the
3500721fffe3SKacheong Poon 						 * urgent byte.  The rest will
3501721fffe3SKacheong Poon 						 * come back.
3502721fffe3SKacheong Poon 						 */
3503721fffe3SKacheong Poon 						(void) adjmsg(mp,
3504721fffe3SKacheong Poon 						    urp + 1 - seg_len);
3505721fffe3SKacheong Poon 						tcp_input_data(connp,
3506721fffe3SKacheong Poon 						    mp, NULL, ira);
3507721fffe3SKacheong Poon 						return;
3508721fffe3SKacheong Poon 					}
3509721fffe3SKacheong Poon 					(void) adjmsg(mp1, urp + 1 - seg_len);
3510721fffe3SKacheong Poon 					tmp_rnxt = tcp->tcp_rnxt;
3511721fffe3SKacheong Poon 					tcp_input_data(connp, mp1, NULL, ira);
3512721fffe3SKacheong Poon 					/*
3513721fffe3SKacheong Poon 					 * If the data passed back in was not
3514721fffe3SKacheong Poon 					 * processed (ie: bad ACK) sending
3515721fffe3SKacheong Poon 					 * the remainder back in will cause a
3516721fffe3SKacheong Poon 					 * loop. In this case, drop the
3517721fffe3SKacheong Poon 					 * packet and let the sender try
3518721fffe3SKacheong Poon 					 * sending a good packet.
3519721fffe3SKacheong Poon 					 */
3520721fffe3SKacheong Poon 					if (tmp_rnxt == tcp->tcp_rnxt) {
3521721fffe3SKacheong Poon 						freemsg(mp);
3522721fffe3SKacheong Poon 						return;
3523721fffe3SKacheong Poon 					}
3524721fffe3SKacheong Poon 				}
3525721fffe3SKacheong Poon 				tcp_input_data(connp, mp, NULL, ira);
3526721fffe3SKacheong Poon 				return;
3527721fffe3SKacheong Poon 			}
3528721fffe3SKacheong Poon 			/*
3529721fffe3SKacheong Poon 			 * This segment contains only the urgent byte.  We
3530721fffe3SKacheong Poon 			 * have to allocate the T_exdata_ind, if we can.
3531721fffe3SKacheong Poon 			 */
3532721fffe3SKacheong Poon 			if (IPCL_IS_NONSTR(connp)) {
3533721fffe3SKacheong Poon 				int error;
3534721fffe3SKacheong Poon 
3535f2297e92SDan McDonald 				(*sockupcalls->su_recv)
3536721fffe3SKacheong Poon 				    (connp->conn_upper_handle, mp, seg_len,
3537721fffe3SKacheong Poon 				    MSG_OOB, &error, NULL);
3538721fffe3SKacheong Poon 				/*
3539721fffe3SKacheong Poon 				 * We should never be in middle of a
3540721fffe3SKacheong Poon 				 * fallback, the squeue guarantees that.
3541721fffe3SKacheong Poon 				 */
3542721fffe3SKacheong Poon 				ASSERT(error != EOPNOTSUPP);
3543721fffe3SKacheong Poon 				mp = NULL;
3544721fffe3SKacheong Poon 				goto update_ack;
3545721fffe3SKacheong Poon 			} else if (!tcp->tcp_urp_mp) {
3546721fffe3SKacheong Poon 				struct T_exdata_ind *tei;
3547721fffe3SKacheong Poon 				mp1 = allocb(sizeof (struct T_exdata_ind),
3548721fffe3SKacheong Poon 				    BPRI_MED);
3549721fffe3SKacheong Poon 				if (!mp1) {
3550721fffe3SKacheong Poon 					/*
3551721fffe3SKacheong Poon 					 * Sigh... It'll be back.
3552721fffe3SKacheong Poon 					 * Generate any MSG*MARK message now.
3553721fffe3SKacheong Poon 					 */
3554721fffe3SKacheong Poon 					freemsg(mp);
3555721fffe3SKacheong Poon 					seg_len = 0;
3556721fffe3SKacheong Poon 					if (flags & TH_SEND_URP_MARK) {
3557721fffe3SKacheong Poon 
3558721fffe3SKacheong Poon 
3559721fffe3SKacheong Poon 						ASSERT(tcp->tcp_urp_mark_mp);
3560721fffe3SKacheong Poon 						tcp->tcp_urp_mark_mp->b_flag &=
3561721fffe3SKacheong Poon 						    ~MSGNOTMARKNEXT;
3562721fffe3SKacheong Poon 						tcp->tcp_urp_mark_mp->b_flag |=
3563721fffe3SKacheong Poon 						    MSGMARKNEXT;
3564721fffe3SKacheong Poon 					}
3565721fffe3SKacheong Poon 					goto ack_check;
3566721fffe3SKacheong Poon 				}
3567721fffe3SKacheong Poon 				mp1->b_datap->db_type = M_PROTO;
3568721fffe3SKacheong Poon 				tei = (struct T_exdata_ind *)mp1->b_rptr;
3569721fffe3SKacheong Poon 				tei->PRIM_type = T_EXDATA_IND;
3570721fffe3SKacheong Poon 				tei->MORE_flag = 0;
3571721fffe3SKacheong Poon 				mp1->b_wptr = (uchar_t *)&tei[1];
3572721fffe3SKacheong Poon 				tcp->tcp_urp_mp = mp1;
3573721fffe3SKacheong Poon #ifdef DEBUG
3574721fffe3SKacheong Poon 				(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
3575721fffe3SKacheong Poon 				    "tcp_rput: allocated exdata_ind %s",
3576721fffe3SKacheong Poon 				    tcp_display(tcp, NULL,
3577721fffe3SKacheong Poon 				    DISP_PORT_ONLY));
3578721fffe3SKacheong Poon #endif /* DEBUG */
3579721fffe3SKacheong Poon 				/*
3580721fffe3SKacheong Poon 				 * There is no need to send a separate MSG*MARK
3581721fffe3SKacheong Poon 				 * message since the T_EXDATA_IND will be sent
3582721fffe3SKacheong Poon 				 * now.
3583721fffe3SKacheong Poon 				 */
3584721fffe3SKacheong Poon 				flags &= ~TH_SEND_URP_MARK;
3585721fffe3SKacheong Poon 				freemsg(tcp->tcp_urp_mark_mp);
3586721fffe3SKacheong Poon 				tcp->tcp_urp_mark_mp = NULL;
3587721fffe3SKacheong Poon 			}
3588721fffe3SKacheong Poon 			/*
3589721fffe3SKacheong Poon 			 * Now we are all set.  On the next putnext upstream,
3590721fffe3SKacheong Poon 			 * tcp_urp_mp will be non-NULL and will get prepended
3591721fffe3SKacheong Poon 			 * to what has to be this piece containing the urgent
3592721fffe3SKacheong Poon 			 * byte.  If for any reason we abort this segment below,
3593721fffe3SKacheong Poon 			 * if it comes back, we will have this ready, or it
3594721fffe3SKacheong Poon 			 * will get blown off in close.
3595721fffe3SKacheong Poon 			 */
3596721fffe3SKacheong Poon 		} else if (urp == seg_len) {
3597721fffe3SKacheong Poon 			/*
3598721fffe3SKacheong Poon 			 * The urgent byte is the next byte after this sequence
3599721fffe3SKacheong Poon 			 * number. If this endpoint is non-STREAMS, then there
3600721fffe3SKacheong Poon 			 * is nothing to do here since the socket has already
3601721fffe3SKacheong Poon 			 * been notified about the urg pointer by the
3602721fffe3SKacheong Poon 			 * su_signal_oob call above.
3603721fffe3SKacheong Poon 			 *
3604721fffe3SKacheong Poon 			 * In case of STREAMS, some more work might be needed.
3605721fffe3SKacheong Poon 			 * If there is data it is marked with MSGMARKNEXT and
3606721fffe3SKacheong Poon 			 * and any tcp_urp_mark_mp is discarded since it is not
3607721fffe3SKacheong Poon 			 * needed. Otherwise, if the code above just allocated
3608721fffe3SKacheong Poon 			 * a zero-length tcp_urp_mark_mp message, that message
3609721fffe3SKacheong Poon 			 * is tagged with MSGMARKNEXT. Sending up these
3610721fffe3SKacheong Poon 			 * MSGMARKNEXT messages makes SIOCATMARK work correctly
3611721fffe3SKacheong Poon 			 * even though the T_EXDATA_IND will not be sent up
3612721fffe3SKacheong Poon 			 * until the urgent byte arrives.
3613721fffe3SKacheong Poon 			 */
3614721fffe3SKacheong Poon 			if (!IPCL_IS_NONSTR(tcp->tcp_connp)) {
3615721fffe3SKacheong Poon 				if (seg_len != 0) {
3616721fffe3SKacheong Poon 					flags |= TH_MARKNEXT_NEEDED;
3617721fffe3SKacheong Poon 					freemsg(tcp->tcp_urp_mark_mp);
3618721fffe3SKacheong Poon 					tcp->tcp_urp_mark_mp = NULL;
3619721fffe3SKacheong Poon 					flags &= ~TH_SEND_URP_MARK;
3620721fffe3SKacheong Poon 				} else if (tcp->tcp_urp_mark_mp != NULL) {
3621721fffe3SKacheong Poon 					flags |= TH_SEND_URP_MARK;
3622721fffe3SKacheong Poon 					tcp->tcp_urp_mark_mp->b_flag &=
3623721fffe3SKacheong Poon 					    ~MSGNOTMARKNEXT;
3624721fffe3SKacheong Poon 					tcp->tcp_urp_mark_mp->b_flag |=
3625721fffe3SKacheong Poon 					    MSGMARKNEXT;
3626721fffe3SKacheong Poon 				}
3627721fffe3SKacheong Poon 			}
3628721fffe3SKacheong Poon #ifdef DEBUG
3629721fffe3SKacheong Poon 			(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
3630721fffe3SKacheong Poon 			    "tcp_rput: AT MARK, len %d, flags 0x%x, %s",
3631721fffe3SKacheong Poon 			    seg_len, flags,
3632721fffe3SKacheong Poon 			    tcp_display(tcp, NULL, DISP_PORT_ONLY));
3633721fffe3SKacheong Poon #endif /* DEBUG */
3634721fffe3SKacheong Poon 		}
3635721fffe3SKacheong Poon #ifdef DEBUG
3636721fffe3SKacheong Poon 		else {
3637721fffe3SKacheong Poon 			/* Data left until we hit mark */
3638721fffe3SKacheong Poon 			(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
3639721fffe3SKacheong Poon 			    "tcp_rput: URP %d bytes left, %s",
3640721fffe3SKacheong Poon 			    urp - seg_len, tcp_display(tcp, NULL,
3641721fffe3SKacheong Poon 			    DISP_PORT_ONLY));
3642721fffe3SKacheong Poon 		}
3643721fffe3SKacheong Poon #endif /* DEBUG */
3644721fffe3SKacheong Poon 	}
3645721fffe3SKacheong Poon 
3646721fffe3SKacheong Poon process_ack:
3647721fffe3SKacheong Poon 	if (!(flags & TH_ACK)) {
3648721fffe3SKacheong Poon 		freemsg(mp);
3649721fffe3SKacheong Poon 		goto xmit_check;
3650721fffe3SKacheong Poon 	}
3651721fffe3SKacheong Poon 	}
3652721fffe3SKacheong Poon 	bytes_acked = (int)(seg_ack - tcp->tcp_suna);
3653721fffe3SKacheong Poon 
3654721fffe3SKacheong Poon 	if (bytes_acked > 0)
3655721fffe3SKacheong Poon 		tcp->tcp_ip_forward_progress = B_TRUE;
3656721fffe3SKacheong Poon 	if (tcp->tcp_state == TCPS_SYN_RCVD) {
36573e95bd4aSAnders Persson 		/*
36583e95bd4aSAnders Persson 		 * tcp_sendmsg() checks tcp_state without entering
36593e95bd4aSAnders Persson 		 * the squeue so tcp_state should be updated before
36603e95bd4aSAnders Persson 		 * sending up a connection confirmation or a new
36613e95bd4aSAnders Persson 		 * connection indication.
36623e95bd4aSAnders Persson 		 */
36633e95bd4aSAnders Persson 		tcp->tcp_state = TCPS_ESTABLISHED;
36643e95bd4aSAnders Persson 
36653e95bd4aSAnders Persson 		/*
36663e95bd4aSAnders Persson 		 * We are seeing the final ack in the three way
36673e95bd4aSAnders Persson 		 * hand shake of a active open'ed connection
36683e95bd4aSAnders Persson 		 * so we must send up a T_CONN_CON
36693e95bd4aSAnders Persson 		 */
36703e95bd4aSAnders Persson 		if (tcp->tcp_active_open) {
36713e95bd4aSAnders Persson 			if (!tcp_conn_con(tcp, iphdr, mp, NULL, ira)) {
36723e95bd4aSAnders Persson 				freemsg(mp);
36733e95bd4aSAnders Persson 				tcp->tcp_state = TCPS_SYN_RCVD;
36743e95bd4aSAnders Persson 				return;
36753e95bd4aSAnders Persson 			}
36763e95bd4aSAnders Persson 			/*
36773e95bd4aSAnders Persson 			 * Don't fuse the loopback endpoints for
36783e95bd4aSAnders Persson 			 * simultaneous active opens.
36793e95bd4aSAnders Persson 			 */
36803e95bd4aSAnders Persson 			if (tcp->tcp_loopback) {
36813e95bd4aSAnders Persson 				TCP_STAT(tcps, tcp_fusion_unfusable);
36823e95bd4aSAnders Persson 				tcp->tcp_unfusable = B_TRUE;
36833e95bd4aSAnders Persson 			}
36843e95bd4aSAnders Persson 			/*
36853e95bd4aSAnders Persson 			 * For simultaneous active open, trace receipt of final
36863e95bd4aSAnders Persson 			 * ACK as tcp:::connect-established.
36873e95bd4aSAnders Persson 			 */
36883e95bd4aSAnders Persson 			DTRACE_TCP5(connect__established, mblk_t *, NULL,
36893e95bd4aSAnders Persson 			    ip_xmit_attr_t *, connp->conn_ixa, void_ip_t *,
36903e95bd4aSAnders Persson 			    iphdr, tcp_t *, tcp, tcph_t *, tcpha);
36913e95bd4aSAnders Persson 		} else if (IPCL_IS_NONSTR(connp)) {
36923e95bd4aSAnders Persson 			/*
36933e95bd4aSAnders Persson 			 * 3-way handshake has completed, so notify socket
36943e95bd4aSAnders Persson 			 * of the new connection.
36953e95bd4aSAnders Persson 			 *
36963e95bd4aSAnders Persson 			 * We are here means eager is fine but it can
36973e95bd4aSAnders Persson 			 * get a TH_RST at any point between now and till
36983e95bd4aSAnders Persson 			 * accept completes and disappear. We need to
36993e95bd4aSAnders Persson 			 * ensure that reference to eager is valid after
37003e95bd4aSAnders Persson 			 * we get out of eager's perimeter. So we do
37013e95bd4aSAnders Persson 			 * an extra refhold.
37023e95bd4aSAnders Persson 			 */
37033e95bd4aSAnders Persson 			CONN_INC_REF(connp);
37043e95bd4aSAnders Persson 
37053e95bd4aSAnders Persson 			if (!tcp_newconn_notify(tcp, ira)) {
3706e82bc0baSAnders Persson 				/*
3707e82bc0baSAnders Persson 				 * The state-change probe for SYN_RCVD ->
3708e82bc0baSAnders Persson 				 * ESTABLISHED has not fired yet. We reset
3709e82bc0baSAnders Persson 				 * the state to SYN_RCVD so that future
3710e82bc0baSAnders Persson 				 * state-change probes report correct state
3711e82bc0baSAnders Persson 				 * transistions.
3712e82bc0baSAnders Persson 				 */
3713e82bc0baSAnders Persson 				tcp->tcp_state = TCPS_SYN_RCVD;
37143e95bd4aSAnders Persson 				freemsg(mp);
37153e95bd4aSAnders Persson 				/* notification did not go up, so drop ref */
37163e95bd4aSAnders Persson 				CONN_DEC_REF(connp);
3717e82bc0baSAnders Persson 				/* ... and close the eager */
3718e82bc0baSAnders Persson 				ASSERT(TCP_IS_DETACHED(tcp));
3719e82bc0baSAnders Persson 				(void) tcp_close_detached(tcp);
37203e95bd4aSAnders Persson 				return;
37213e95bd4aSAnders Persson 			}
37223e95bd4aSAnders Persson 			/*
3723a897f6d2SDan McDonald 			 * tcp_newconn_notify() changes conn_upcalls and
3724a897f6d2SDan McDonald 			 * connp->conn_upper_handle.  Fix things now, in case
3725a897f6d2SDan McDonald 			 * there's data attached to this ack.
3726a897f6d2SDan McDonald 			 */
3727a897f6d2SDan McDonald 			if (connp->conn_upcalls != NULL)
3728a897f6d2SDan McDonald 				sockupcalls = connp->conn_upcalls;
3729a897f6d2SDan McDonald 			/*
37303e95bd4aSAnders Persson 			 * For passive open, trace receipt of final ACK as
37313e95bd4aSAnders Persson 			 * tcp:::accept-established.
37323e95bd4aSAnders Persson 			 */
37333e95bd4aSAnders Persson 			DTRACE_TCP5(accept__established, mlbk_t *, NULL,
37343e95bd4aSAnders Persson 			    ip_xmit_attr_t *, connp->conn_ixa, void_ip_t *,
37353e95bd4aSAnders Persson 			    iphdr, tcp_t *, tcp, tcph_t *, tcpha);
3736dd49f125SAnders Persson 		} else {
37373e95bd4aSAnders Persson 			/*
37383e95bd4aSAnders Persson 			 * 3-way handshake complete - this is a STREAMS based
37393e95bd4aSAnders Persson 			 * socket, so pass up the T_CONN_IND.
37403e95bd4aSAnders Persson 			 */
3741721fffe3SKacheong Poon 			tcp_t	*listener = tcp->tcp_listener;
3742721fffe3SKacheong Poon 			mblk_t	*mp = tcp->tcp_conn.tcp_eager_conn_ind;
3743721fffe3SKacheong Poon 
3744721fffe3SKacheong Poon 			tcp->tcp_tconnind_started = B_TRUE;
3745721fffe3SKacheong Poon 			tcp->tcp_conn.tcp_eager_conn_ind = NULL;
37463e95bd4aSAnders Persson 			ASSERT(mp != NULL);
3747721fffe3SKacheong Poon 			/*
3748721fffe3SKacheong Poon 			 * We are here means eager is fine but it can
3749721fffe3SKacheong Poon 			 * get a TH_RST at any point between now and till
3750721fffe3SKacheong Poon 			 * accept completes and disappear. We need to
3751721fffe3SKacheong Poon 			 * ensure that reference to eager is valid after
3752721fffe3SKacheong Poon 			 * we get out of eager's perimeter. So we do
3753721fffe3SKacheong Poon 			 * an extra refhold.
3754721fffe3SKacheong Poon 			 */
3755721fffe3SKacheong Poon 			CONN_INC_REF(connp);
3756721fffe3SKacheong Poon 
3757721fffe3SKacheong Poon 			/*
3758721fffe3SKacheong Poon 			 * The listener also exists because of the refhold
3759721fffe3SKacheong Poon 			 * done in tcp_input_listener. Its possible that it
3760721fffe3SKacheong Poon 			 * might have closed. We will check that once we
3761721fffe3SKacheong Poon 			 * get inside listeners context.
3762721fffe3SKacheong Poon 			 */
3763721fffe3SKacheong Poon 			CONN_INC_REF(listener->tcp_connp);
3764721fffe3SKacheong Poon 			if (listener->tcp_connp->conn_sqp ==
3765721fffe3SKacheong Poon 			    connp->conn_sqp) {
3766721fffe3SKacheong Poon 				/*
3767721fffe3SKacheong Poon 				 * We optimize by not calling an SQUEUE_ENTER
3768721fffe3SKacheong Poon 				 * on the listener since we know that the
3769721fffe3SKacheong Poon 				 * listener and eager squeues are the same.
3770721fffe3SKacheong Poon 				 * We are able to make this check safely only
3771721fffe3SKacheong Poon 				 * because neither the eager nor the listener
3772721fffe3SKacheong Poon 				 * can change its squeue. Only an active connect
3773721fffe3SKacheong Poon 				 * can change its squeue
3774721fffe3SKacheong Poon 				 */
3775721fffe3SKacheong Poon 				tcp_send_conn_ind(listener->tcp_connp, mp,
3776721fffe3SKacheong Poon 				    listener->tcp_connp->conn_sqp);
3777721fffe3SKacheong Poon 				CONN_DEC_REF(listener->tcp_connp);
3778721fffe3SKacheong Poon 			} else if (!tcp->tcp_loopback) {
3779721fffe3SKacheong Poon 				SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp,
3780721fffe3SKacheong Poon 				    mp, tcp_send_conn_ind,
3781721fffe3SKacheong Poon 				    listener->tcp_connp, NULL, SQ_FILL,
3782721fffe3SKacheong Poon 				    SQTAG_TCP_CONN_IND);
3783721fffe3SKacheong Poon 			} else {
3784721fffe3SKacheong Poon 				SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp,
3785721fffe3SKacheong Poon 				    mp, tcp_send_conn_ind,
3786c7274c2eSGeorge Shepherd 				    listener->tcp_connp, NULL, SQ_NODRAIN,
3787721fffe3SKacheong Poon 				    SQTAG_TCP_CONN_IND);
3788721fffe3SKacheong Poon 			}
37899cd928feSAlan Maguire 			/*
37909cd928feSAlan Maguire 			 * For passive open, trace receipt of final ACK as
37919cd928feSAlan Maguire 			 * tcp:::accept-established.
37929cd928feSAlan Maguire 			 */
37939cd928feSAlan Maguire 			DTRACE_TCP5(accept__established, mlbk_t *, NULL,
37949cd928feSAlan Maguire 			    ip_xmit_attr_t *, connp->conn_ixa, void_ip_t *,
37959cd928feSAlan Maguire 			    iphdr, tcp_t *, tcp, tcph_t *, tcpha);
3796721fffe3SKacheong Poon 		}
3797721fffe3SKacheong Poon 		TCPS_CONN_INC(tcps);
3798721fffe3SKacheong Poon 
3799721fffe3SKacheong Poon 		tcp->tcp_suna = tcp->tcp_iss + 1;	/* One for the SYN */
3800721fffe3SKacheong Poon 		bytes_acked--;
3801721fffe3SKacheong Poon 		/* SYN was acked - making progress */
3802721fffe3SKacheong Poon 		tcp->tcp_ip_forward_progress = B_TRUE;
3803721fffe3SKacheong Poon 
3804721fffe3SKacheong Poon 		/*
3805721fffe3SKacheong Poon 		 * If SYN was retransmitted, need to reset all
3806721fffe3SKacheong Poon 		 * retransmission info as this segment will be
3807721fffe3SKacheong Poon 		 * treated as a dup ACK.
3808721fffe3SKacheong Poon 		 */
3809721fffe3SKacheong Poon 		if (tcp->tcp_rexmit) {
3810721fffe3SKacheong Poon 			tcp->tcp_rexmit = B_FALSE;
3811721fffe3SKacheong Poon 			tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
3812721fffe3SKacheong Poon 			tcp->tcp_rexmit_max = tcp->tcp_snxt;
3813721fffe3SKacheong Poon 			tcp->tcp_ms_we_have_waited = 0;
3814721fffe3SKacheong Poon 			tcp->tcp_cwnd = mss;
3815721fffe3SKacheong Poon 		}
3816721fffe3SKacheong Poon 
3817721fffe3SKacheong Poon 		/*
3818721fffe3SKacheong Poon 		 * We set the send window to zero here.
3819721fffe3SKacheong Poon 		 * This is needed if there is data to be
3820721fffe3SKacheong Poon 		 * processed already on the queue.
3821721fffe3SKacheong Poon 		 * Later (at swnd_update label), the
3822721fffe3SKacheong Poon 		 * "new_swnd > tcp_swnd" condition is satisfied
3823721fffe3SKacheong Poon 		 * the XMIT_NEEDED flag is set in the current
3824721fffe3SKacheong Poon 		 * (SYN_RCVD) state. This ensures tcp_wput_data() is
3825721fffe3SKacheong Poon 		 * called if there is already data on queue in
3826721fffe3SKacheong Poon 		 * this state.
3827721fffe3SKacheong Poon 		 */
3828721fffe3SKacheong Poon 		tcp->tcp_swnd = 0;
3829721fffe3SKacheong Poon 
3830721fffe3SKacheong Poon 		if (new_swnd > tcp->tcp_max_swnd)
3831721fffe3SKacheong Poon 			tcp->tcp_max_swnd = new_swnd;
3832721fffe3SKacheong Poon 		tcp->tcp_swl1 = seg_seq;
3833721fffe3SKacheong Poon 		tcp->tcp_swl2 = seg_ack;
3834721fffe3SKacheong Poon 		tcp->tcp_valid_bits &= ~TCP_ISS_VALID;
3835721fffe3SKacheong Poon 
38369cd928feSAlan Maguire 		/* Trace change from SYN_RCVD -> ESTABLISHED here */
38379cd928feSAlan Maguire 		DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
38389cd928feSAlan Maguire 		    connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
38399cd928feSAlan Maguire 		    int32_t, TCPS_SYN_RCVD);
38409cd928feSAlan Maguire 
3841721fffe3SKacheong Poon 		/* Fuse when both sides are in ESTABLISHED state */
3842721fffe3SKacheong Poon 		if (tcp->tcp_loopback && do_tcp_fusion)
3843721fffe3SKacheong Poon 			tcp_fuse(tcp, iphdr, tcpha);
3844721fffe3SKacheong Poon 
3845721fffe3SKacheong Poon 	}
3846721fffe3SKacheong Poon 	/* This code follows 4.4BSD-Lite2 mostly. */
3847721fffe3SKacheong Poon 	if (bytes_acked < 0)
3848721fffe3SKacheong Poon 		goto est;
3849721fffe3SKacheong Poon 
3850721fffe3SKacheong Poon 	/*
3851721fffe3SKacheong Poon 	 * If TCP is ECN capable and the congestion experience bit is
3852721fffe3SKacheong Poon 	 * set, reduce tcp_cwnd and tcp_ssthresh.  But this should only be
3853721fffe3SKacheong Poon 	 * done once per window (or more loosely, per RTT).
3854721fffe3SKacheong Poon 	 */
3855721fffe3SKacheong Poon 	if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max))
3856721fffe3SKacheong Poon 		tcp->tcp_cwr = B_FALSE;
3857721fffe3SKacheong Poon 	if (tcp->tcp_ecn_ok && (flags & TH_ECE)) {
3858721fffe3SKacheong Poon 		if (!tcp->tcp_cwr) {
3859721fffe3SKacheong Poon 			npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss;
3860721fffe3SKacheong Poon 			tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss;
3861721fffe3SKacheong Poon 			tcp->tcp_cwnd = npkt * mss;
3862721fffe3SKacheong Poon 			/*
3863721fffe3SKacheong Poon 			 * If the cwnd is 0, use the timer to clock out
3864721fffe3SKacheong Poon 			 * new segments.  This is required by the ECN spec.
3865721fffe3SKacheong Poon 			 */
3866721fffe3SKacheong Poon 			if (npkt == 0) {
3867721fffe3SKacheong Poon 				TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
3868721fffe3SKacheong Poon 				/*
3869721fffe3SKacheong Poon 				 * This makes sure that when the ACK comes
3870721fffe3SKacheong Poon 				 * back, we will increase tcp_cwnd by 1 MSS.
3871721fffe3SKacheong Poon 				 */
3872721fffe3SKacheong Poon 				tcp->tcp_cwnd_cnt = 0;
3873721fffe3SKacheong Poon 			}
3874721fffe3SKacheong Poon 			tcp->tcp_cwr = B_TRUE;
3875721fffe3SKacheong Poon 			/*
3876721fffe3SKacheong Poon 			 * This marks the end of the current window of in
3877721fffe3SKacheong Poon 			 * flight data.  That is why we don't use
3878721fffe3SKacheong Poon 			 * tcp_suna + tcp_swnd.  Only data in flight can
3879721fffe3SKacheong Poon 			 * provide ECN info.
3880721fffe3SKacheong Poon 			 */
3881721fffe3SKacheong Poon 			tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
3882721fffe3SKacheong Poon 			tcp->tcp_ecn_cwr_sent = B_FALSE;
3883721fffe3SKacheong Poon 		}
3884721fffe3SKacheong Poon 	}
3885721fffe3SKacheong Poon 
3886721fffe3SKacheong Poon 	mp1 = tcp->tcp_xmit_head;
3887721fffe3SKacheong Poon 	if (bytes_acked == 0) {
3888721fffe3SKacheong Poon 		if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) {
3889721fffe3SKacheong Poon 			int dupack_cnt;
3890721fffe3SKacheong Poon 
3891721fffe3SKacheong Poon 			TCPS_BUMP_MIB(tcps, tcpInDupAck);
3892721fffe3SKacheong Poon 			/*
3893721fffe3SKacheong Poon 			 * Fast retransmit.  When we have seen exactly three
3894721fffe3SKacheong Poon 			 * identical ACKs while we have unacked data
3895721fffe3SKacheong Poon 			 * outstanding we take it as a hint that our peer
3896721fffe3SKacheong Poon 			 * dropped something.
3897721fffe3SKacheong Poon 			 *
3898721fffe3SKacheong Poon 			 * If TCP is retransmitting, don't do fast retransmit.
3899721fffe3SKacheong Poon 			 */
3900721fffe3SKacheong Poon 			if (mp1 && tcp->tcp_suna != tcp->tcp_snxt &&
3901721fffe3SKacheong Poon 			    ! tcp->tcp_rexmit) {
3902721fffe3SKacheong Poon 				/* Do Limited Transmit */
3903721fffe3SKacheong Poon 				if ((dupack_cnt = ++tcp->tcp_dupack_cnt) <
3904721fffe3SKacheong Poon 				    tcps->tcps_dupack_fast_retransmit) {
3905721fffe3SKacheong Poon 					/*
3906721fffe3SKacheong Poon 					 * RFC 3042
3907721fffe3SKacheong Poon 					 *
3908721fffe3SKacheong Poon 					 * What we need to do is temporarily
3909721fffe3SKacheong Poon 					 * increase tcp_cwnd so that new
3910721fffe3SKacheong Poon 					 * data can be sent if it is allowed
3911721fffe3SKacheong Poon 					 * by the receive window (tcp_rwnd).
3912721fffe3SKacheong Poon 					 * tcp_wput_data() will take care of
3913721fffe3SKacheong Poon 					 * the rest.
3914721fffe3SKacheong Poon 					 *
3915721fffe3SKacheong Poon 					 * If the connection is SACK capable,
3916721fffe3SKacheong Poon 					 * only do limited xmit when there
3917721fffe3SKacheong Poon 					 * is SACK info.
3918721fffe3SKacheong Poon 					 *
3919721fffe3SKacheong Poon 					 * Note how tcp_cwnd is incremented.
3920721fffe3SKacheong Poon 					 * The first dup ACK will increase
3921721fffe3SKacheong Poon 					 * it by 1 MSS.  The second dup ACK
3922721fffe3SKacheong Poon 					 * will increase it by 2 MSS.  This
3923721fffe3SKacheong Poon 					 * means that only 1 new segment will
3924721fffe3SKacheong Poon 					 * be sent for each dup ACK.
3925721fffe3SKacheong Poon 					 */
3926721fffe3SKacheong Poon 					if (tcp->tcp_unsent > 0 &&
3927721fffe3SKacheong Poon 					    (!tcp->tcp_snd_sack_ok ||
3928721fffe3SKacheong Poon 					    (tcp->tcp_snd_sack_ok &&
3929721fffe3SKacheong Poon 					    tcp->tcp_notsack_list != NULL))) {
3930721fffe3SKacheong Poon 						tcp->tcp_cwnd += mss <<
3931721fffe3SKacheong Poon 						    (tcp->tcp_dupack_cnt - 1);
3932721fffe3SKacheong Poon 						flags |= TH_LIMIT_XMIT;
3933721fffe3SKacheong Poon 					}
3934721fffe3SKacheong Poon 				} else if (dupack_cnt ==
3935721fffe3SKacheong Poon 				    tcps->tcps_dupack_fast_retransmit) {
3936721fffe3SKacheong Poon 
3937721fffe3SKacheong Poon 				/*
3938721fffe3SKacheong Poon 				 * If we have reduced tcp_ssthresh
3939721fffe3SKacheong Poon 				 * because of ECN, do not reduce it again
3940721fffe3SKacheong Poon 				 * unless it is already one window of data
3941721fffe3SKacheong Poon 				 * away.  After one window of data, tcp_cwr
3942721fffe3SKacheong Poon 				 * should then be cleared.  Note that
3943721fffe3SKacheong Poon 				 * for non ECN capable connection, tcp_cwr
3944721fffe3SKacheong Poon 				 * should always be false.
3945721fffe3SKacheong Poon 				 *
3946721fffe3SKacheong Poon 				 * Adjust cwnd since the duplicate
3947721fffe3SKacheong Poon 				 * ack indicates that a packet was
3948721fffe3SKacheong Poon 				 * dropped (due to congestion.)
3949721fffe3SKacheong Poon 				 */
3950721fffe3SKacheong Poon 				if (!tcp->tcp_cwr) {
3951721fffe3SKacheong Poon 					npkt = ((tcp->tcp_snxt -
3952721fffe3SKacheong Poon 					    tcp->tcp_suna) >> 1) / mss;
3953721fffe3SKacheong Poon 					tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) *
3954721fffe3SKacheong Poon 					    mss;
3955721fffe3SKacheong Poon 					tcp->tcp_cwnd = (npkt +
3956721fffe3SKacheong Poon 					    tcp->tcp_dupack_cnt) * mss;
3957721fffe3SKacheong Poon 				}
3958721fffe3SKacheong Poon 				if (tcp->tcp_ecn_ok) {
3959721fffe3SKacheong Poon 					tcp->tcp_cwr = B_TRUE;
3960721fffe3SKacheong Poon 					tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
3961721fffe3SKacheong Poon 					tcp->tcp_ecn_cwr_sent = B_FALSE;
3962721fffe3SKacheong Poon 				}
3963721fffe3SKacheong Poon 
3964721fffe3SKacheong Poon 				/*
3965721fffe3SKacheong Poon 				 * We do Hoe's algorithm.  Refer to her
3966721fffe3SKacheong Poon 				 * paper "Improving the Start-up Behavior
3967721fffe3SKacheong Poon 				 * of a Congestion Control Scheme for TCP,"
3968721fffe3SKacheong Poon 				 * appeared in SIGCOMM'96.
3969721fffe3SKacheong Poon 				 *
3970721fffe3SKacheong Poon 				 * Save highest seq no we have sent so far.
3971721fffe3SKacheong Poon 				 * Be careful about the invisible FIN byte.
3972721fffe3SKacheong Poon 				 */
3973721fffe3SKacheong Poon 				if ((tcp->tcp_valid_bits & TCP_FSS_VALID) &&
3974721fffe3SKacheong Poon 				    (tcp->tcp_unsent == 0)) {
3975721fffe3SKacheong Poon 					tcp->tcp_rexmit_max = tcp->tcp_fss;
3976721fffe3SKacheong Poon 				} else {
3977721fffe3SKacheong Poon 					tcp->tcp_rexmit_max = tcp->tcp_snxt;
3978721fffe3SKacheong Poon 				}
3979721fffe3SKacheong Poon 
3980721fffe3SKacheong Poon 				/*
3981721fffe3SKacheong Poon 				 * For SACK:
3982721fffe3SKacheong Poon 				 * Calculate tcp_pipe, which is the
3983721fffe3SKacheong Poon 				 * estimated number of bytes in
3984721fffe3SKacheong Poon 				 * network.
3985721fffe3SKacheong Poon 				 *
3986721fffe3SKacheong Poon 				 * tcp_fack is the highest sack'ed seq num
3987721fffe3SKacheong Poon 				 * TCP has received.
3988721fffe3SKacheong Poon 				 *
3989721fffe3SKacheong Poon 				 * tcp_pipe is explained in the above quoted
3990721fffe3SKacheong Poon 				 * Fall and Floyd's paper.  tcp_fack is
3991721fffe3SKacheong Poon 				 * explained in Mathis and Mahdavi's
3992721fffe3SKacheong Poon 				 * "Forward Acknowledgment: Refining TCP
3993721fffe3SKacheong Poon 				 * Congestion Control" in SIGCOMM '96.
3994721fffe3SKacheong Poon 				 */
3995721fffe3SKacheong Poon 				if (tcp->tcp_snd_sack_ok) {
3996721fffe3SKacheong Poon 					if (tcp->tcp_notsack_list != NULL) {
3997721fffe3SKacheong Poon 						tcp->tcp_pipe = tcp->tcp_snxt -
3998721fffe3SKacheong Poon 						    tcp->tcp_fack;
3999721fffe3SKacheong Poon 						tcp->tcp_sack_snxt = seg_ack;
4000721fffe3SKacheong Poon 						flags |= TH_NEED_SACK_REXMIT;
4001721fffe3SKacheong Poon 					} else {
4002721fffe3SKacheong Poon 						/*
4003721fffe3SKacheong Poon 						 * Always initialize tcp_pipe
4004721fffe3SKacheong Poon 						 * even though we don't have
4005721fffe3SKacheong Poon 						 * any SACK info.  If later
4006721fffe3SKacheong Poon 						 * we get SACK info and
4007721fffe3SKacheong Poon 						 * tcp_pipe is not initialized,
4008721fffe3SKacheong Poon 						 * funny things will happen.
4009721fffe3SKacheong Poon 						 */
4010721fffe3SKacheong Poon 						tcp->tcp_pipe =
4011721fffe3SKacheong Poon 						    tcp->tcp_cwnd_ssthresh;
4012721fffe3SKacheong Poon 					}
4013721fffe3SKacheong Poon 				} else {
4014721fffe3SKacheong Poon 					flags |= TH_REXMIT_NEEDED;
4015721fffe3SKacheong Poon 				} /* tcp_snd_sack_ok */
4016721fffe3SKacheong Poon 
4017721fffe3SKacheong Poon 				} else {
4018721fffe3SKacheong Poon 					/*
4019721fffe3SKacheong Poon 					 * Here we perform congestion
4020721fffe3SKacheong Poon 					 * avoidance, but NOT slow start.
4021721fffe3SKacheong Poon 					 * This is known as the Fast
4022721fffe3SKacheong Poon 					 * Recovery Algorithm.
4023721fffe3SKacheong Poon 					 */
4024721fffe3SKacheong Poon 					if (tcp->tcp_snd_sack_ok &&
4025721fffe3SKacheong Poon 					    tcp->tcp_notsack_list != NULL) {
4026721fffe3SKacheong Poon 						flags |= TH_NEED_SACK_REXMIT;
4027721fffe3SKacheong Poon 						tcp->tcp_pipe -= mss;
4028721fffe3SKacheong Poon 						if (tcp->tcp_pipe < 0)
4029721fffe3SKacheong Poon 							tcp->tcp_pipe = 0;
4030721fffe3SKacheong Poon 					} else {
4031721fffe3SKacheong Poon 					/*
4032721fffe3SKacheong Poon 					 * We know that one more packet has
4033721fffe3SKacheong Poon 					 * left the pipe thus we can update
4034721fffe3SKacheong Poon 					 * cwnd.
4035721fffe3SKacheong Poon 					 */
4036721fffe3SKacheong Poon 					cwnd = tcp->tcp_cwnd + mss;
4037721fffe3SKacheong Poon 					if (cwnd > tcp->tcp_cwnd_max)
4038721fffe3SKacheong Poon 						cwnd = tcp->tcp_cwnd_max;
4039721fffe3SKacheong Poon 					tcp->tcp_cwnd = cwnd;
4040721fffe3SKacheong Poon 					if (tcp->tcp_unsent > 0)
4041721fffe3SKacheong Poon 						flags |= TH_XMIT_NEEDED;
4042721fffe3SKacheong Poon 					}
4043721fffe3SKacheong Poon 				}
4044721fffe3SKacheong Poon 			}
4045721fffe3SKacheong Poon 		} else if (tcp->tcp_zero_win_probe) {
4046721fffe3SKacheong Poon 			/*
4047721fffe3SKacheong Poon 			 * If the window has opened, need to arrange
4048721fffe3SKacheong Poon 			 * to send additional data.
4049721fffe3SKacheong Poon 			 */
4050721fffe3SKacheong Poon 			if (new_swnd != 0) {
4051721fffe3SKacheong Poon 				/* tcp_suna != tcp_snxt */
4052721fffe3SKacheong Poon 				/* Packet contains a window update */
4053721fffe3SKacheong Poon 				TCPS_BUMP_MIB(tcps, tcpInWinUpdate);
4054721fffe3SKacheong Poon 				tcp->tcp_zero_win_probe = 0;
4055721fffe3SKacheong Poon 				tcp->tcp_timer_backoff = 0;
4056721fffe3SKacheong Poon 				tcp->tcp_ms_we_have_waited = 0;
4057721fffe3SKacheong Poon 
4058721fffe3SKacheong Poon 				/*
4059721fffe3SKacheong Poon 				 * Transmit starting with tcp_suna since
4060721fffe3SKacheong Poon 				 * the one byte probe is not ack'ed.
4061721fffe3SKacheong Poon 				 * If TCP has sent more than one identical
4062721fffe3SKacheong Poon 				 * probe, tcp_rexmit will be set.  That means
4063721fffe3SKacheong Poon 				 * tcp_ss_rexmit() will send out the one
4064721fffe3SKacheong Poon 				 * byte along with new data.  Otherwise,
4065721fffe3SKacheong Poon 				 * fake the retransmission.
4066721fffe3SKacheong Poon 				 */
4067721fffe3SKacheong Poon 				flags |= TH_XMIT_NEEDED;
4068721fffe3SKacheong Poon 				if (!tcp->tcp_rexmit) {
4069721fffe3SKacheong Poon 					tcp->tcp_rexmit = B_TRUE;
4070721fffe3SKacheong Poon 					tcp->tcp_dupack_cnt = 0;
4071721fffe3SKacheong Poon 					tcp->tcp_rexmit_nxt = tcp->tcp_suna;
4072721fffe3SKacheong Poon 					tcp->tcp_rexmit_max = tcp->tcp_suna + 1;
4073721fffe3SKacheong Poon 				}
4074721fffe3SKacheong Poon 			}
4075721fffe3SKacheong Poon 		}
4076721fffe3SKacheong Poon 		goto swnd_update;
4077721fffe3SKacheong Poon 	}
4078721fffe3SKacheong Poon 
4079721fffe3SKacheong Poon 	/*
4080721fffe3SKacheong Poon 	 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73.
4081721fffe3SKacheong Poon 	 * If the ACK value acks something that we have not yet sent, it might
4082721fffe3SKacheong Poon 	 * be an old duplicate segment.  Send an ACK to re-synchronize the
4083721fffe3SKacheong Poon 	 * other side.
4084721fffe3SKacheong Poon 	 * Note: reset in response to unacceptable ACK in SYN_RECEIVE
4085721fffe3SKacheong Poon 	 * state is handled above, so we can always just drop the segment and
4086721fffe3SKacheong Poon 	 * send an ACK here.
4087721fffe3SKacheong Poon 	 *
4088721fffe3SKacheong Poon 	 * In the case where the peer shrinks the window, we see the new window
4089721fffe3SKacheong Poon 	 * update, but all the data sent previously is queued up by the peer.
4090721fffe3SKacheong Poon 	 * To account for this, in tcp_process_shrunk_swnd(), the sequence
4091721fffe3SKacheong Poon 	 * number, which was already sent, and within window, is recorded.
4092721fffe3SKacheong Poon 	 * tcp_snxt is then updated.
4093721fffe3SKacheong Poon 	 *
4094721fffe3SKacheong Poon 	 * If the window has previously shrunk, and an ACK for data not yet
4095721fffe3SKacheong Poon 	 * sent, according to tcp_snxt is recieved, it may still be valid. If
4096721fffe3SKacheong Poon 	 * the ACK is for data within the window at the time the window was
4097721fffe3SKacheong Poon 	 * shrunk, then the ACK is acceptable. In this case tcp_snxt is set to
4098721fffe3SKacheong Poon 	 * the sequence number ACK'ed.
4099721fffe3SKacheong Poon 	 *
4100721fffe3SKacheong Poon 	 * If the ACK covers all the data sent at the time the window was
4101721fffe3SKacheong Poon 	 * shrunk, we can now set tcp_is_wnd_shrnk to B_FALSE.
4102721fffe3SKacheong Poon 	 *
4103721fffe3SKacheong Poon 	 * Should we send ACKs in response to ACK only segments?
4104721fffe3SKacheong Poon 	 */
4105721fffe3SKacheong Poon 
4106721fffe3SKacheong Poon 	if (SEQ_GT(seg_ack, tcp->tcp_snxt)) {
4107721fffe3SKacheong Poon 		if ((tcp->tcp_is_wnd_shrnk) &&
4108721fffe3SKacheong Poon 		    (SEQ_LEQ(seg_ack, tcp->tcp_snxt_shrunk))) {
4109721fffe3SKacheong Poon 			uint32_t data_acked_ahead_snxt;
4110721fffe3SKacheong Poon 
4111721fffe3SKacheong Poon 			data_acked_ahead_snxt = seg_ack - tcp->tcp_snxt;
4112721fffe3SKacheong Poon 			tcp_update_xmit_tail(tcp, seg_ack);
4113721fffe3SKacheong Poon 			tcp->tcp_unsent -= data_acked_ahead_snxt;
4114721fffe3SKacheong Poon 		} else {
4115721fffe3SKacheong Poon 			TCPS_BUMP_MIB(tcps, tcpInAckUnsent);
4116721fffe3SKacheong Poon 			/* drop the received segment */
4117721fffe3SKacheong Poon 			freemsg(mp);
4118721fffe3SKacheong Poon 
4119721fffe3SKacheong Poon 			/*
4120721fffe3SKacheong Poon 			 * Send back an ACK.  If tcp_drop_ack_unsent_cnt is
4121721fffe3SKacheong Poon 			 * greater than 0, check if the number of such
4122721fffe3SKacheong Poon 			 * bogus ACks is greater than that count.  If yes,
4123721fffe3SKacheong Poon 			 * don't send back any ACK.  This prevents TCP from
4124721fffe3SKacheong Poon 			 * getting into an ACK storm if somehow an attacker
4125721fffe3SKacheong Poon 			 * successfully spoofs an acceptable segment to our
4126721fffe3SKacheong Poon 			 * peer.  If this continues (count > 2 X threshold),
4127721fffe3SKacheong Poon 			 * we should abort this connection.
4128721fffe3SKacheong Poon 			 */
4129721fffe3SKacheong Poon 			if (tcp_drop_ack_unsent_cnt > 0 &&
4130721fffe3SKacheong Poon 			    ++tcp->tcp_in_ack_unsent >
4131721fffe3SKacheong Poon 			    tcp_drop_ack_unsent_cnt) {
4132721fffe3SKacheong Poon 				TCP_STAT(tcps, tcp_in_ack_unsent_drop);
4133721fffe3SKacheong Poon 				if (tcp->tcp_in_ack_unsent > 2 *
4134721fffe3SKacheong Poon 				    tcp_drop_ack_unsent_cnt) {
4135721fffe3SKacheong Poon 					(void) tcp_clean_death(tcp, EPROTO);
4136721fffe3SKacheong Poon 				}
4137721fffe3SKacheong Poon 				return;
4138721fffe3SKacheong Poon 			}
4139721fffe3SKacheong Poon 			mp = tcp_ack_mp(tcp);
4140721fffe3SKacheong Poon 			if (mp != NULL) {
4141721fffe3SKacheong Poon 				BUMP_LOCAL(tcp->tcp_obsegs);
4142721fffe3SKacheong Poon 				TCPS_BUMP_MIB(tcps, tcpOutAck);
4143721fffe3SKacheong Poon 				tcp_send_data(tcp, mp);
4144721fffe3SKacheong Poon 			}
4145721fffe3SKacheong Poon 			return;
4146721fffe3SKacheong Poon 		}
4147721fffe3SKacheong Poon 	} else if (tcp->tcp_is_wnd_shrnk && SEQ_GEQ(seg_ack,
4148721fffe3SKacheong Poon 	    tcp->tcp_snxt_shrunk)) {
4149721fffe3SKacheong Poon 			tcp->tcp_is_wnd_shrnk = B_FALSE;
4150721fffe3SKacheong Poon 	}
4151721fffe3SKacheong Poon 
4152721fffe3SKacheong Poon 	/*
4153721fffe3SKacheong Poon 	 * TCP gets a new ACK, update the notsack'ed list to delete those
4154721fffe3SKacheong Poon 	 * blocks that are covered by this ACK.
4155721fffe3SKacheong Poon 	 */
4156721fffe3SKacheong Poon 	if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) {
4157721fffe3SKacheong Poon 		tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack,
4158721fffe3SKacheong Poon 		    &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list));
4159721fffe3SKacheong Poon 	}
4160721fffe3SKacheong Poon 
4161721fffe3SKacheong Poon 	/*
4162721fffe3SKacheong Poon 	 * If we got an ACK after fast retransmit, check to see
4163721fffe3SKacheong Poon 	 * if it is a partial ACK.  If it is not and the congestion
4164721fffe3SKacheong Poon 	 * window was inflated to account for the other side's
4165721fffe3SKacheong Poon 	 * cached packets, retract it.  If it is, do Hoe's algorithm.
4166721fffe3SKacheong Poon 	 */
4167721fffe3SKacheong Poon 	if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) {
4168721fffe3SKacheong Poon 		ASSERT(tcp->tcp_rexmit == B_FALSE);
4169721fffe3SKacheong Poon 		if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) {
4170721fffe3SKacheong Poon 			tcp->tcp_dupack_cnt = 0;
4171721fffe3SKacheong Poon 			/*
4172721fffe3SKacheong Poon 			 * Restore the orig tcp_cwnd_ssthresh after
4173721fffe3SKacheong Poon 			 * fast retransmit phase.
4174721fffe3SKacheong Poon 			 */
4175721fffe3SKacheong Poon 			if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) {
4176721fffe3SKacheong Poon 				tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh;
4177721fffe3SKacheong Poon 			}
4178721fffe3SKacheong Poon 			tcp->tcp_rexmit_max = seg_ack;
4179721fffe3SKacheong Poon 			tcp->tcp_cwnd_cnt = 0;
4180721fffe3SKacheong Poon 
4181721fffe3SKacheong Poon 			/*
4182721fffe3SKacheong Poon 			 * Remove all notsack info to avoid confusion with
4183721fffe3SKacheong Poon 			 * the next fast retrasnmit/recovery phase.
4184721fffe3SKacheong Poon 			 */
418566cd0f60SKacheong Poon 			if (tcp->tcp_snd_sack_ok) {
4186721fffe3SKacheong Poon 				TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list,
4187721fffe3SKacheong Poon 				    tcp);
4188721fffe3SKacheong Poon 			}
4189721fffe3SKacheong Poon 		} else {
4190721fffe3SKacheong Poon 			if (tcp->tcp_snd_sack_ok &&
4191721fffe3SKacheong Poon 			    tcp->tcp_notsack_list != NULL) {
4192721fffe3SKacheong Poon 				flags |= TH_NEED_SACK_REXMIT;
4193721fffe3SKacheong Poon 				tcp->tcp_pipe -= mss;
4194721fffe3SKacheong Poon 				if (tcp->tcp_pipe < 0)
4195721fffe3SKacheong Poon 					tcp->tcp_pipe = 0;
4196721fffe3SKacheong Poon 			} else {
4197721fffe3SKacheong Poon 				/*
4198721fffe3SKacheong Poon 				 * Hoe's algorithm:
4199721fffe3SKacheong Poon 				 *
4200721fffe3SKacheong Poon 				 * Retransmit the unack'ed segment and
4201721fffe3SKacheong Poon 				 * restart fast recovery.  Note that we
4202721fffe3SKacheong Poon 				 * need to scale back tcp_cwnd to the
4203721fffe3SKacheong Poon 				 * original value when we started fast
4204721fffe3SKacheong Poon 				 * recovery.  This is to prevent overly
4205721fffe3SKacheong Poon 				 * aggressive behaviour in sending new
4206721fffe3SKacheong Poon 				 * segments.
4207721fffe3SKacheong Poon 				 */
4208721fffe3SKacheong Poon 				tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh +
4209721fffe3SKacheong Poon 				    tcps->tcps_dupack_fast_retransmit * mss;
4210721fffe3SKacheong Poon 				tcp->tcp_cwnd_cnt = tcp->tcp_cwnd;
4211721fffe3SKacheong Poon 				flags |= TH_REXMIT_NEEDED;
4212721fffe3SKacheong Poon 			}
4213721fffe3SKacheong Poon 		}
4214721fffe3SKacheong Poon 	} else {
4215721fffe3SKacheong Poon 		tcp->tcp_dupack_cnt = 0;
4216721fffe3SKacheong Poon 		if (tcp->tcp_rexmit) {
4217721fffe3SKacheong Poon 			/*
4218721fffe3SKacheong Poon 			 * TCP is retranmitting.  If the ACK ack's all
4219721fffe3SKacheong Poon 			 * outstanding data, update tcp_rexmit_max and
4220721fffe3SKacheong Poon 			 * tcp_rexmit_nxt.  Otherwise, update tcp_rexmit_nxt
4221721fffe3SKacheong Poon 			 * to the correct value.
4222721fffe3SKacheong Poon 			 *
4223721fffe3SKacheong Poon 			 * Note that SEQ_LEQ() is used.  This is to avoid
4224721fffe3SKacheong Poon 			 * unnecessary fast retransmit caused by dup ACKs
4225721fffe3SKacheong Poon 			 * received when TCP does slow start retransmission
4226721fffe3SKacheong Poon 			 * after a time out.  During this phase, TCP may
4227721fffe3SKacheong Poon 			 * send out segments which are already received.
4228721fffe3SKacheong Poon 			 * This causes dup ACKs to be sent back.
4229721fffe3SKacheong Poon 			 */
4230721fffe3SKacheong Poon 			if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) {
4231721fffe3SKacheong Poon 				if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) {
4232721fffe3SKacheong Poon 					tcp->tcp_rexmit_nxt = seg_ack;
4233721fffe3SKacheong Poon 				}
4234721fffe3SKacheong Poon 				if (seg_ack != tcp->tcp_rexmit_max) {
4235721fffe3SKacheong Poon 					flags |= TH_XMIT_NEEDED;
4236721fffe3SKacheong Poon 				}
4237721fffe3SKacheong Poon 			} else {
4238721fffe3SKacheong Poon 				tcp->tcp_rexmit = B_FALSE;
4239721fffe3SKacheong Poon 				tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
4240721fffe3SKacheong Poon 			}
4241721fffe3SKacheong Poon 			tcp->tcp_ms_we_have_waited = 0;
4242721fffe3SKacheong Poon 		}
4243721fffe3SKacheong Poon 	}
4244721fffe3SKacheong Poon 
4245721fffe3SKacheong Poon 	TCPS_BUMP_MIB(tcps, tcpInAckSegs);
4246721fffe3SKacheong Poon 	TCPS_UPDATE_MIB(tcps, tcpInAckBytes, bytes_acked);
4247721fffe3SKacheong Poon 	tcp->tcp_suna = seg_ack;
4248721fffe3SKacheong Poon 	if (tcp->tcp_zero_win_probe != 0) {
4249721fffe3SKacheong Poon 		tcp->tcp_zero_win_probe = 0;
4250721fffe3SKacheong Poon 		tcp->tcp_timer_backoff = 0;
4251721fffe3SKacheong Poon 	}
4252721fffe3SKacheong Poon 
4253721fffe3SKacheong Poon 	/*
4254721fffe3SKacheong Poon 	 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed.
4255721fffe3SKacheong Poon 	 * Note that it cannot be the SYN being ack'ed.  The code flow
4256721fffe3SKacheong Poon 	 * will not reach here.
4257721fffe3SKacheong Poon 	 */
4258721fffe3SKacheong Poon 	if (mp1 == NULL) {
4259721fffe3SKacheong Poon 		goto fin_acked;
4260721fffe3SKacheong Poon 	}
4261721fffe3SKacheong Poon 
4262721fffe3SKacheong Poon 	/*
4263721fffe3SKacheong Poon 	 * Update the congestion window.
4264721fffe3SKacheong Poon 	 *
4265721fffe3SKacheong Poon 	 * If TCP is not ECN capable or TCP is ECN capable but the
4266721fffe3SKacheong Poon 	 * congestion experience bit is not set, increase the tcp_cwnd as
4267721fffe3SKacheong Poon 	 * usual.
4268721fffe3SKacheong Poon 	 */
4269721fffe3SKacheong Poon 	if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) {
4270721fffe3SKacheong Poon 		cwnd = tcp->tcp_cwnd;
4271721fffe3SKacheong Poon 		add = mss;
4272721fffe3SKacheong Poon 
4273721fffe3SKacheong Poon 		if (cwnd >= tcp->tcp_cwnd_ssthresh) {
4274721fffe3SKacheong Poon 			/*
4275721fffe3SKacheong Poon 			 * This is to prevent an increase of less than 1 MSS of
4276721fffe3SKacheong Poon 			 * tcp_cwnd.  With partial increase, tcp_wput_data()
4277721fffe3SKacheong Poon 			 * may send out tinygrams in order to preserve mblk
4278721fffe3SKacheong Poon 			 * boundaries.
4279721fffe3SKacheong Poon 			 *
4280721fffe3SKacheong Poon 			 * By initializing tcp_cwnd_cnt to new tcp_cwnd and
4281721fffe3SKacheong Poon 			 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is
4282721fffe3SKacheong Poon 			 * increased by 1 MSS for every RTTs.
4283721fffe3SKacheong Poon 			 */
4284721fffe3SKacheong Poon 			if (tcp->tcp_cwnd_cnt <= 0) {
4285721fffe3SKacheong Poon 				tcp->tcp_cwnd_cnt = cwnd + add;
4286721fffe3SKacheong Poon 			} else {
4287721fffe3SKacheong Poon 				tcp->tcp_cwnd_cnt -= add;
4288721fffe3SKacheong Poon 				add = 0;
4289721fffe3SKacheong Poon 			}
4290721fffe3SKacheong Poon 		}
4291721fffe3SKacheong Poon 		tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max);
4292721fffe3SKacheong Poon 	}
4293721fffe3SKacheong Poon 
4294721fffe3SKacheong Poon 	/* See if the latest urgent data has been acknowledged */
4295721fffe3SKacheong Poon 	if ((tcp->tcp_valid_bits & TCP_URG_VALID) &&
4296721fffe3SKacheong Poon 	    SEQ_GT(seg_ack, tcp->tcp_urg))
4297721fffe3SKacheong Poon 		tcp->tcp_valid_bits &= ~TCP_URG_VALID;
4298721fffe3SKacheong Poon 
4299721fffe3SKacheong Poon 	/* Can we update the RTT estimates? */
4300721fffe3SKacheong Poon 	if (tcp->tcp_snd_ts_ok) {
4301721fffe3SKacheong Poon 		/* Ignore zero timestamp echo-reply. */
4302721fffe3SKacheong Poon 		if (tcpopt.tcp_opt_ts_ecr != 0) {
4303721fffe3SKacheong Poon 			tcp_set_rto(tcp, (int32_t)LBOLT_FASTPATH -
4304721fffe3SKacheong Poon 			    (int32_t)tcpopt.tcp_opt_ts_ecr);
4305721fffe3SKacheong Poon 		}
4306721fffe3SKacheong Poon 
4307721fffe3SKacheong Poon 		/* If needed, restart the timer. */
4308721fffe3SKacheong Poon 		if (tcp->tcp_set_timer == 1) {
4309721fffe3SKacheong Poon 			TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
4310721fffe3SKacheong Poon 			tcp->tcp_set_timer = 0;
4311721fffe3SKacheong Poon 		}
4312721fffe3SKacheong Poon 		/*
4313721fffe3SKacheong Poon 		 * Update tcp_csuna in case the other side stops sending
4314721fffe3SKacheong Poon 		 * us timestamps.
4315721fffe3SKacheong Poon 		 */
4316721fffe3SKacheong Poon 		tcp->tcp_csuna = tcp->tcp_snxt;
4317721fffe3SKacheong Poon 	} else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) {
4318721fffe3SKacheong Poon 		/*
4319721fffe3SKacheong Poon 		 * An ACK sequence we haven't seen before, so get the RTT
4320721fffe3SKacheong Poon 		 * and update the RTO. But first check if the timestamp is
4321721fffe3SKacheong Poon 		 * valid to use.
4322721fffe3SKacheong Poon 		 */
4323721fffe3SKacheong Poon 		if ((mp1->b_next != NULL) &&
4324721fffe3SKacheong Poon 		    SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next)))
4325721fffe3SKacheong Poon 			tcp_set_rto(tcp, (int32_t)LBOLT_FASTPATH -
4326721fffe3SKacheong Poon 			    (int32_t)(intptr_t)mp1->b_prev);
4327721fffe3SKacheong Poon 		else
4328721fffe3SKacheong Poon 			TCPS_BUMP_MIB(tcps, tcpRttNoUpdate);
4329721fffe3SKacheong Poon 
4330721fffe3SKacheong Poon 		/* Remeber the last sequence to be ACKed */
4331721fffe3SKacheong Poon 		tcp->tcp_csuna = seg_ack;
4332721fffe3SKacheong Poon 		if (tcp->tcp_set_timer == 1) {
4333721fffe3SKacheong Poon 			TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
4334721fffe3SKacheong Poon 			tcp->tcp_set_timer = 0;
4335721fffe3SKacheong Poon 		}
4336721fffe3SKacheong Poon 	} else {
4337721fffe3SKacheong Poon 		TCPS_BUMP_MIB(tcps, tcpRttNoUpdate);
4338721fffe3SKacheong Poon 	}
4339721fffe3SKacheong Poon 
4340721fffe3SKacheong Poon 	/* Eat acknowledged bytes off the xmit queue. */
4341721fffe3SKacheong Poon 	for (;;) {
4342721fffe3SKacheong Poon 		mblk_t	*mp2;
4343721fffe3SKacheong Poon 		uchar_t	*wptr;
4344721fffe3SKacheong Poon 
4345721fffe3SKacheong Poon 		wptr = mp1->b_wptr;
4346721fffe3SKacheong Poon 		ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX);
4347721fffe3SKacheong Poon 		bytes_acked -= (int)(wptr - mp1->b_rptr);
4348721fffe3SKacheong Poon 		if (bytes_acked < 0) {
4349721fffe3SKacheong Poon 			mp1->b_rptr = wptr + bytes_acked;
4350721fffe3SKacheong Poon 			/*
4351721fffe3SKacheong Poon 			 * Set a new timestamp if all the bytes timed by the
4352721fffe3SKacheong Poon 			 * old timestamp have been ack'ed.
4353721fffe3SKacheong Poon 			 */
4354721fffe3SKacheong Poon 			if (SEQ_GT(seg_ack,
4355721fffe3SKacheong Poon 			    (uint32_t)(uintptr_t)(mp1->b_next))) {
4356721fffe3SKacheong Poon 				mp1->b_prev =
4357721fffe3SKacheong Poon 				    (mblk_t *)(uintptr_t)LBOLT_FASTPATH;
4358721fffe3SKacheong Poon 				mp1->b_next = NULL;
4359721fffe3SKacheong Poon 			}
4360721fffe3SKacheong Poon 			break;
4361721fffe3SKacheong Poon 		}
4362721fffe3SKacheong Poon 		mp1->b_next = NULL;
4363721fffe3SKacheong Poon 		mp1->b_prev = NULL;
4364721fffe3SKacheong Poon 		mp2 = mp1;
4365721fffe3SKacheong Poon 		mp1 = mp1->b_cont;
4366721fffe3SKacheong Poon 
4367721fffe3SKacheong Poon 		/*
4368721fffe3SKacheong Poon 		 * This notification is required for some zero-copy
4369721fffe3SKacheong Poon 		 * clients to maintain a copy semantic. After the data
4370721fffe3SKacheong Poon 		 * is ack'ed, client is safe to modify or reuse the buffer.
4371721fffe3SKacheong Poon 		 */
4372721fffe3SKacheong Poon 		if (tcp->tcp_snd_zcopy_aware &&
4373721fffe3SKacheong Poon 		    (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY))
4374721fffe3SKacheong Poon 			tcp_zcopy_notify(tcp);
4375721fffe3SKacheong Poon 		freeb(mp2);
4376721fffe3SKacheong Poon 		if (bytes_acked == 0) {
4377721fffe3SKacheong Poon 			if (mp1 == NULL) {
4378721fffe3SKacheong Poon 				/* Everything is ack'ed, clear the tail. */
4379721fffe3SKacheong Poon 				tcp->tcp_xmit_tail = NULL;
4380721fffe3SKacheong Poon 				/*
4381721fffe3SKacheong Poon 				 * Cancel the timer unless we are still
4382721fffe3SKacheong Poon 				 * waiting for an ACK for the FIN packet.
4383721fffe3SKacheong Poon 				 */
4384721fffe3SKacheong Poon 				if (tcp->tcp_timer_tid != 0 &&
4385721fffe3SKacheong Poon 				    tcp->tcp_snxt == tcp->tcp_suna) {
4386721fffe3SKacheong Poon 					(void) TCP_TIMER_CANCEL(tcp,
4387721fffe3SKacheong Poon 					    tcp->tcp_timer_tid);
4388721fffe3SKacheong Poon 					tcp->tcp_timer_tid = 0;
4389721fffe3SKacheong Poon 				}
4390721fffe3SKacheong Poon 				goto pre_swnd_update;
4391721fffe3SKacheong Poon 			}
4392721fffe3SKacheong Poon 			if (mp2 != tcp->tcp_xmit_tail)
4393721fffe3SKacheong Poon 				break;
4394721fffe3SKacheong Poon 			tcp->tcp_xmit_tail = mp1;
4395721fffe3SKacheong Poon 			ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <=
4396721fffe3SKacheong Poon 			    (uintptr_t)INT_MAX);
4397721fffe3SKacheong Poon 			tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr -
4398721fffe3SKacheong Poon 			    mp1->b_rptr);
4399721fffe3SKacheong Poon 			break;
4400721fffe3SKacheong Poon 		}
4401721fffe3SKacheong Poon 		if (mp1 == NULL) {
4402721fffe3SKacheong Poon 			/*
4403721fffe3SKacheong Poon 			 * More was acked but there is nothing more
4404721fffe3SKacheong Poon 			 * outstanding.  This means that the FIN was
4405721fffe3SKacheong Poon 			 * just acked or that we're talking to a clown.
4406721fffe3SKacheong Poon 			 */
4407721fffe3SKacheong Poon fin_acked:
4408721fffe3SKacheong Poon 			ASSERT(tcp->tcp_fin_sent);
4409721fffe3SKacheong Poon 			tcp->tcp_xmit_tail = NULL;
4410721fffe3SKacheong Poon 			if (tcp->tcp_fin_sent) {
4411721fffe3SKacheong Poon 				/* FIN was acked - making progress */
4412721fffe3SKacheong Poon 				if (!tcp->tcp_fin_acked)
4413721fffe3SKacheong Poon 					tcp->tcp_ip_forward_progress = B_TRUE;
4414721fffe3SKacheong Poon 				tcp->tcp_fin_acked = B_TRUE;
4415721fffe3SKacheong Poon 				if (tcp->tcp_linger_tid != 0 &&
4416721fffe3SKacheong Poon 				    TCP_TIMER_CANCEL(tcp,
4417721fffe3SKacheong Poon 				    tcp->tcp_linger_tid) >= 0) {
4418721fffe3SKacheong Poon 					tcp_stop_lingering(tcp);
4419721fffe3SKacheong Poon 					freemsg(mp);
4420721fffe3SKacheong Poon 					mp = NULL;
4421721fffe3SKacheong Poon 				}
4422721fffe3SKacheong Poon 			} else {
4423721fffe3SKacheong Poon 				/*
4424721fffe3SKacheong Poon 				 * We should never get here because
4425721fffe3SKacheong Poon 				 * we have already checked that the
4426721fffe3SKacheong Poon 				 * number of bytes ack'ed should be
4427721fffe3SKacheong Poon 				 * smaller than or equal to what we
4428721fffe3SKacheong Poon 				 * have sent so far (it is the
4429721fffe3SKacheong Poon 				 * acceptability check of the ACK).
4430721fffe3SKacheong Poon 				 * We can only get here if the send
4431721fffe3SKacheong Poon 				 * queue is corrupted.
4432721fffe3SKacheong Poon 				 *
4433721fffe3SKacheong Poon 				 * Terminate the connection and
4434721fffe3SKacheong Poon 				 * panic the system.  It is better
4435721fffe3SKacheong Poon 				 * for us to panic instead of
4436721fffe3SKacheong Poon 				 * continuing to avoid other disaster.
4437721fffe3SKacheong Poon 				 */
4438721fffe3SKacheong Poon 				tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt,
4439721fffe3SKacheong Poon 				    tcp->tcp_rnxt, TH_RST|TH_ACK);
4440721fffe3SKacheong Poon 				panic("Memory corruption "
4441721fffe3SKacheong Poon 				    "detected for connection %s.",
4442721fffe3SKacheong Poon 				    tcp_display(tcp, NULL,
4443721fffe3SKacheong Poon 				    DISP_ADDR_AND_PORT));
4444721fffe3SKacheong Poon 				/*NOTREACHED*/
4445721fffe3SKacheong Poon 			}
4446721fffe3SKacheong Poon 			goto pre_swnd_update;
4447721fffe3SKacheong Poon 		}
4448721fffe3SKacheong Poon 		ASSERT(mp2 != tcp->tcp_xmit_tail);
4449721fffe3SKacheong Poon 	}
4450721fffe3SKacheong Poon 	if (tcp->tcp_unsent) {
4451721fffe3SKacheong Poon 		flags |= TH_XMIT_NEEDED;
4452721fffe3SKacheong Poon 	}
4453721fffe3SKacheong Poon pre_swnd_update:
4454721fffe3SKacheong Poon 	tcp->tcp_xmit_head = mp1;
4455721fffe3SKacheong Poon swnd_update:
4456721fffe3SKacheong Poon 	/*
4457721fffe3SKacheong Poon 	 * The following check is different from most other implementations.
4458721fffe3SKacheong Poon 	 * For bi-directional transfer, when segments are dropped, the
4459721fffe3SKacheong Poon 	 * "normal" check will not accept a window update in those
4460721fffe3SKacheong Poon 	 * retransmitted segemnts.  Failing to do that, TCP may send out
4461721fffe3SKacheong Poon 	 * segments which are outside receiver's window.  As TCP accepts
4462721fffe3SKacheong Poon 	 * the ack in those retransmitted segments, if the window update in
4463721fffe3SKacheong Poon 	 * the same segment is not accepted, TCP will incorrectly calculates
4464721fffe3SKacheong Poon 	 * that it can send more segments.  This can create a deadlock
4465721fffe3SKacheong Poon 	 * with the receiver if its window becomes zero.
4466721fffe3SKacheong Poon 	 */
4467721fffe3SKacheong Poon 	if (SEQ_LT(tcp->tcp_swl2, seg_ack) ||
4468721fffe3SKacheong Poon 	    SEQ_LT(tcp->tcp_swl1, seg_seq) ||
4469721fffe3SKacheong Poon 	    (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) {
4470721fffe3SKacheong Poon 		/*
4471721fffe3SKacheong Poon 		 * The criteria for update is:
4472721fffe3SKacheong Poon 		 *
4473721fffe3SKacheong Poon 		 * 1. the segment acknowledges some data.  Or
4474721fffe3SKacheong Poon 		 * 2. the segment is new, i.e. it has a higher seq num. Or
4475721fffe3SKacheong Poon 		 * 3. the segment is not old and the advertised window is
4476721fffe3SKacheong Poon 		 * larger than the previous advertised window.
4477721fffe3SKacheong Poon 		 */
4478721fffe3SKacheong Poon 		if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd)
4479721fffe3SKacheong Poon 			flags |= TH_XMIT_NEEDED;
4480721fffe3SKacheong Poon 		tcp->tcp_swnd = new_swnd;
4481721fffe3SKacheong Poon 		if (new_swnd > tcp->tcp_max_swnd)
4482721fffe3SKacheong Poon 			tcp->tcp_max_swnd = new_swnd;
4483721fffe3SKacheong Poon 		tcp->tcp_swl1 = seg_seq;
4484721fffe3SKacheong Poon 		tcp->tcp_swl2 = seg_ack;
4485721fffe3SKacheong Poon 	}
4486721fffe3SKacheong Poon est:
4487721fffe3SKacheong Poon 	if (tcp->tcp_state > TCPS_ESTABLISHED) {
4488721fffe3SKacheong Poon 
4489721fffe3SKacheong Poon 		switch (tcp->tcp_state) {
4490721fffe3SKacheong Poon 		case TCPS_FIN_WAIT_1:
4491721fffe3SKacheong Poon 			if (tcp->tcp_fin_acked) {
4492721fffe3SKacheong Poon 				tcp->tcp_state = TCPS_FIN_WAIT_2;
44939cd928feSAlan Maguire 				DTRACE_TCP6(state__change, void, NULL,
44949cd928feSAlan Maguire 				    ip_xmit_attr_t *, connp->conn_ixa,
44959cd928feSAlan Maguire 				    void, NULL, tcp_t *, tcp, void, NULL,
44969cd928feSAlan Maguire 				    int32_t, TCPS_FIN_WAIT_1);
4497721fffe3SKacheong Poon 				/*
4498721fffe3SKacheong Poon 				 * We implement the non-standard BSD/SunOS
4499721fffe3SKacheong Poon 				 * FIN_WAIT_2 flushing algorithm.
4500721fffe3SKacheong Poon 				 * If there is no user attached to this
4501721fffe3SKacheong Poon 				 * TCP endpoint, then this TCP struct
4502721fffe3SKacheong Poon 				 * could hang around forever in FIN_WAIT_2
4503721fffe3SKacheong Poon 				 * state if the peer forgets to send us
4504721fffe3SKacheong Poon 				 * a FIN.  To prevent this, we wait only
4505721fffe3SKacheong Poon 				 * 2*MSL (a convenient time value) for
4506721fffe3SKacheong Poon 				 * the FIN to arrive.  If it doesn't show up,
4507721fffe3SKacheong Poon 				 * we flush the TCP endpoint.  This algorithm,
4508721fffe3SKacheong Poon 				 * though a violation of RFC-793, has worked
4509721fffe3SKacheong Poon 				 * for over 10 years in BSD systems.
4510721fffe3SKacheong Poon 				 * Note: SunOS 4.x waits 675 seconds before
4511721fffe3SKacheong Poon 				 * flushing the FIN_WAIT_2 connection.
4512721fffe3SKacheong Poon 				 */
4513721fffe3SKacheong Poon 				TCP_TIMER_RESTART(tcp,
4514707e74bcSKacheong Poon 				    tcp->tcp_fin_wait_2_flush_interval);
4515721fffe3SKacheong Poon 			}
4516721fffe3SKacheong Poon 			break;
4517721fffe3SKacheong Poon 		case TCPS_FIN_WAIT_2:
4518721fffe3SKacheong Poon 			break;	/* Shutdown hook? */
4519721fffe3SKacheong Poon 		case TCPS_LAST_ACK:
4520721fffe3SKacheong Poon 			freemsg(mp);
4521721fffe3SKacheong Poon 			if (tcp->tcp_fin_acked) {
4522721fffe3SKacheong Poon 				(void) tcp_clean_death(tcp, 0);
4523721fffe3SKacheong Poon 				return;
4524721fffe3SKacheong Poon 			}
4525721fffe3SKacheong Poon 			goto xmit_check;
4526721fffe3SKacheong Poon 		case TCPS_CLOSING:
45279cd928feSAlan Maguire 			if (tcp->tcp_fin_acked) {
4528721fffe3SKacheong Poon 				SET_TIME_WAIT(tcps, tcp, connp);
45299cd928feSAlan Maguire 				DTRACE_TCP6(state__change, void, NULL,
45309cd928feSAlan Maguire 				    ip_xmit_attr_t *, connp->conn_ixa, void,
45319cd928feSAlan Maguire 				    NULL, tcp_t *, tcp, void, NULL, int32_t,
45329cd928feSAlan Maguire 				    TCPS_CLOSING);
45339cd928feSAlan Maguire 			}
4534721fffe3SKacheong Poon 			/*FALLTHRU*/
4535721fffe3SKacheong Poon 		case TCPS_CLOSE_WAIT:
4536721fffe3SKacheong Poon 			freemsg(mp);
4537721fffe3SKacheong Poon 			goto xmit_check;
4538721fffe3SKacheong Poon 		default:
4539721fffe3SKacheong Poon 			ASSERT(tcp->tcp_state != TCPS_TIME_WAIT);
4540721fffe3SKacheong Poon 			break;
4541721fffe3SKacheong Poon 		}
4542721fffe3SKacheong Poon 	}
4543721fffe3SKacheong Poon 	if (flags & TH_FIN) {
4544721fffe3SKacheong Poon 		/* Make sure we ack the fin */
4545721fffe3SKacheong Poon 		flags |= TH_ACK_NEEDED;
4546721fffe3SKacheong Poon 		if (!tcp->tcp_fin_rcvd) {
4547721fffe3SKacheong Poon 			tcp->tcp_fin_rcvd = B_TRUE;
4548721fffe3SKacheong Poon 			tcp->tcp_rnxt++;
4549721fffe3SKacheong Poon 			tcpha = tcp->tcp_tcpha;
4550721fffe3SKacheong Poon 			tcpha->tha_ack = htonl(tcp->tcp_rnxt);
4551721fffe3SKacheong Poon 
4552721fffe3SKacheong Poon 			/*
45533e95bd4aSAnders Persson 			 * Generate the ordrel_ind at the end unless the
45543e95bd4aSAnders Persson 			 * conn is detached or it is a STREAMS based eager.
45553e95bd4aSAnders Persson 			 * In the eager case we defer the notification until
45563e95bd4aSAnders Persson 			 * tcp_accept_finish has run.
4557721fffe3SKacheong Poon 			 */
45583e95bd4aSAnders Persson 			if (!TCP_IS_DETACHED(tcp) && (IPCL_IS_NONSTR(connp) ||
45593e95bd4aSAnders Persson 			    (tcp->tcp_listener == NULL &&
45603e95bd4aSAnders Persson 			    !tcp->tcp_hard_binding)))
4561721fffe3SKacheong Poon 				flags |= TH_ORDREL_NEEDED;
4562721fffe3SKacheong Poon 			switch (tcp->tcp_state) {
4563721fffe3SKacheong Poon 			case TCPS_SYN_RCVD:
45649cd928feSAlan Maguire 				tcp->tcp_state = TCPS_CLOSE_WAIT;
45659cd928feSAlan Maguire 				DTRACE_TCP6(state__change, void, NULL,
45669cd928feSAlan Maguire 				    ip_xmit_attr_t *, connp->conn_ixa,
45679cd928feSAlan Maguire 				    void, NULL, tcp_t *, tcp, void, NULL,
45689cd928feSAlan Maguire 				    int32_t, TCPS_SYN_RCVD);
45699cd928feSAlan Maguire 				/* Keepalive? */
45709cd928feSAlan Maguire 				break;
4571721fffe3SKacheong Poon 			case TCPS_ESTABLISHED:
4572721fffe3SKacheong Poon 				tcp->tcp_state = TCPS_CLOSE_WAIT;
45739cd928feSAlan Maguire 				DTRACE_TCP6(state__change, void, NULL,
45749cd928feSAlan Maguire 				    ip_xmit_attr_t *, connp->conn_ixa,
45759cd928feSAlan Maguire 				    void, NULL, tcp_t *, tcp, void, NULL,
45769cd928feSAlan Maguire 				    int32_t, TCPS_ESTABLISHED);
4577721fffe3SKacheong Poon 				/* Keepalive? */
4578721fffe3SKacheong Poon 				break;
4579721fffe3SKacheong Poon 			case TCPS_FIN_WAIT_1:
4580721fffe3SKacheong Poon 				if (!tcp->tcp_fin_acked) {
4581721fffe3SKacheong Poon 					tcp->tcp_state = TCPS_CLOSING;
45829cd928feSAlan Maguire 					DTRACE_TCP6(state__change, void, NULL,
45839cd928feSAlan Maguire 					    ip_xmit_attr_t *, connp->conn_ixa,
45849cd928feSAlan Maguire 					    void, NULL, tcp_t *, tcp, void,
45859cd928feSAlan Maguire 					    NULL, int32_t, TCPS_FIN_WAIT_1);
4586721fffe3SKacheong Poon 					break;
4587721fffe3SKacheong Poon 				}
4588721fffe3SKacheong Poon 				/* FALLTHRU */
4589721fffe3SKacheong Poon 			case TCPS_FIN_WAIT_2:
4590721fffe3SKacheong Poon 				SET_TIME_WAIT(tcps, tcp, connp);
45919cd928feSAlan Maguire 				DTRACE_TCP6(state__change, void, NULL,
45929cd928feSAlan Maguire 				    ip_xmit_attr_t *, connp->conn_ixa, void,
45939cd928feSAlan Maguire 				    NULL, tcp_t *, tcp, void, NULL, int32_t,
45949cd928feSAlan Maguire 				    TCPS_FIN_WAIT_2);
4595721fffe3SKacheong Poon 				if (seg_len) {
4596721fffe3SKacheong Poon 					/*
4597721fffe3SKacheong Poon 					 * implies data piggybacked on FIN.
4598721fffe3SKacheong Poon 					 * break to handle data.
4599721fffe3SKacheong Poon 					 */
4600721fffe3SKacheong Poon 					break;
4601721fffe3SKacheong Poon 				}
4602721fffe3SKacheong Poon 				freemsg(mp);
4603721fffe3SKacheong Poon 				goto ack_check;
4604721fffe3SKacheong Poon 			}
4605721fffe3SKacheong Poon 		}
4606721fffe3SKacheong Poon 	}
4607721fffe3SKacheong Poon 	if (mp == NULL)
4608721fffe3SKacheong Poon 		goto xmit_check;
4609721fffe3SKacheong Poon 	if (seg_len == 0) {
4610721fffe3SKacheong Poon 		freemsg(mp);
4611721fffe3SKacheong Poon 		goto xmit_check;
4612721fffe3SKacheong Poon 	}
4613721fffe3SKacheong Poon 	if (mp->b_rptr == mp->b_wptr) {
4614721fffe3SKacheong Poon 		/*
4615721fffe3SKacheong Poon 		 * The header has been consumed, so we remove the
4616721fffe3SKacheong Poon 		 * zero-length mblk here.
4617721fffe3SKacheong Poon 		 */
4618721fffe3SKacheong Poon 		mp1 = mp;
4619721fffe3SKacheong Poon 		mp = mp->b_cont;
4620721fffe3SKacheong Poon 		freeb(mp1);
4621721fffe3SKacheong Poon 	}
4622721fffe3SKacheong Poon update_ack:
4623721fffe3SKacheong Poon 	tcpha = tcp->tcp_tcpha;
4624721fffe3SKacheong Poon 	tcp->tcp_rack_cnt++;
4625721fffe3SKacheong Poon 	{
4626721fffe3SKacheong Poon 		uint32_t cur_max;
4627721fffe3SKacheong Poon 
4628721fffe3SKacheong Poon 		cur_max = tcp->tcp_rack_cur_max;
4629721fffe3SKacheong Poon 		if (tcp->tcp_rack_cnt >= cur_max) {
4630721fffe3SKacheong Poon 			/*
4631721fffe3SKacheong Poon 			 * We have more unacked data than we should - send
4632721fffe3SKacheong Poon 			 * an ACK now.
4633721fffe3SKacheong Poon 			 */
4634721fffe3SKacheong Poon 			flags |= TH_ACK_NEEDED;
4635721fffe3SKacheong Poon 			cur_max++;
4636721fffe3SKacheong Poon 			if (cur_max > tcp->tcp_rack_abs_max)
4637721fffe3SKacheong Poon 				tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max;
4638721fffe3SKacheong Poon 			else
4639721fffe3SKacheong Poon 				tcp->tcp_rack_cur_max = cur_max;
4640721fffe3SKacheong Poon 		} else if (TCP_IS_DETACHED(tcp)) {
4641721fffe3SKacheong Poon 			/* We don't have an ACK timer for detached TCP. */
4642721fffe3SKacheong Poon 			flags |= TH_ACK_NEEDED;
4643721fffe3SKacheong Poon 		} else if (seg_len < mss) {
4644721fffe3SKacheong Poon 			/*
4645721fffe3SKacheong Poon 			 * If we get a segment that is less than an mss, and we
4646721fffe3SKacheong Poon 			 * already have unacknowledged data, and the amount
4647721fffe3SKacheong Poon 			 * unacknowledged is not a multiple of mss, then we
4648721fffe3SKacheong Poon 			 * better generate an ACK now.  Otherwise, this may be
4649721fffe3SKacheong Poon 			 * the tail piece of a transaction, and we would rather
4650721fffe3SKacheong Poon 			 * wait for the response.
4651721fffe3SKacheong Poon 			 */
4652721fffe3SKacheong Poon 			uint32_t udif;
4653721fffe3SKacheong Poon 			ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <=
4654721fffe3SKacheong Poon 			    (uintptr_t)INT_MAX);
4655721fffe3SKacheong Poon 			udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack);
4656721fffe3SKacheong Poon 			if (udif && (udif % mss))
4657721fffe3SKacheong Poon 				flags |= TH_ACK_NEEDED;
4658721fffe3SKacheong Poon 			else
4659721fffe3SKacheong Poon 				flags |= TH_ACK_TIMER_NEEDED;
4660721fffe3SKacheong Poon 		} else {
4661721fffe3SKacheong Poon 			/* Start delayed ack timer */
4662721fffe3SKacheong Poon 			flags |= TH_ACK_TIMER_NEEDED;
4663721fffe3SKacheong Poon 		}
4664721fffe3SKacheong Poon 	}
4665721fffe3SKacheong Poon 	tcp->tcp_rnxt += seg_len;
4666721fffe3SKacheong Poon 	tcpha->tha_ack = htonl(tcp->tcp_rnxt);
4667721fffe3SKacheong Poon 
4668721fffe3SKacheong Poon 	if (mp == NULL)
4669721fffe3SKacheong Poon 		goto xmit_check;
4670721fffe3SKacheong Poon 
4671721fffe3SKacheong Poon 	/* Update SACK list */
4672721fffe3SKacheong Poon 	if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
4673721fffe3SKacheong Poon 		tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt,
4674721fffe3SKacheong Poon 		    &(tcp->tcp_num_sack_blk));
4675721fffe3SKacheong Poon 	}
4676721fffe3SKacheong Poon 
4677721fffe3SKacheong Poon 	if (tcp->tcp_urp_mp) {
4678721fffe3SKacheong Poon 		tcp->tcp_urp_mp->b_cont = mp;
4679721fffe3SKacheong Poon 		mp = tcp->tcp_urp_mp;
4680721fffe3SKacheong Poon 		tcp->tcp_urp_mp = NULL;
4681721fffe3SKacheong Poon 		/* Ready for a new signal. */
4682721fffe3SKacheong Poon 		tcp->tcp_urp_last_valid = B_FALSE;
4683721fffe3SKacheong Poon #ifdef DEBUG
4684721fffe3SKacheong Poon 		(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
4685721fffe3SKacheong Poon 		    "tcp_rput: sending exdata_ind %s",
4686721fffe3SKacheong Poon 		    tcp_display(tcp, NULL, DISP_PORT_ONLY));
4687721fffe3SKacheong Poon #endif /* DEBUG */
4688721fffe3SKacheong Poon 	}
4689721fffe3SKacheong Poon 
4690721fffe3SKacheong Poon 	/*
4691721fffe3SKacheong Poon 	 * Check for ancillary data changes compared to last segment.
4692721fffe3SKacheong Poon 	 */
4693721fffe3SKacheong Poon 	if (connp->conn_recv_ancillary.crb_all != 0) {
4694721fffe3SKacheong Poon 		mp = tcp_input_add_ancillary(tcp, mp, &ipp, ira);
4695721fffe3SKacheong Poon 		if (mp == NULL)
4696721fffe3SKacheong Poon 			return;
4697721fffe3SKacheong Poon 	}
4698721fffe3SKacheong Poon 
46993e95bd4aSAnders Persson 	if (IPCL_IS_NONSTR(connp)) {
4700721fffe3SKacheong Poon 		/*
4701721fffe3SKacheong Poon 		 * Non-STREAMS socket
4702721fffe3SKacheong Poon 		 */
4703721fffe3SKacheong Poon 		boolean_t push = flags & (TH_PUSH|TH_FIN);
4704721fffe3SKacheong Poon 		int error;
4705721fffe3SKacheong Poon 
4706f2297e92SDan McDonald 		if ((*sockupcalls->su_recv)(connp->conn_upper_handle,
4707721fffe3SKacheong Poon 		    mp, seg_len, 0, &error, &push) <= 0) {
4708721fffe3SKacheong Poon 			/*
4709721fffe3SKacheong Poon 			 * We should never be in middle of a
4710721fffe3SKacheong Poon 			 * fallback, the squeue guarantees that.
4711721fffe3SKacheong Poon 			 */
4712721fffe3SKacheong Poon 			ASSERT(error != EOPNOTSUPP);
4713721fffe3SKacheong Poon 			if (error == ENOSPC)
4714721fffe3SKacheong Poon 				tcp->tcp_rwnd -= seg_len;
4715721fffe3SKacheong Poon 		} else if (push) {
4716721fffe3SKacheong Poon 			/* PUSH bit set and sockfs is not flow controlled */
4717721fffe3SKacheong Poon 			flags |= tcp_rwnd_reopen(tcp);
4718721fffe3SKacheong Poon 		}
47193e95bd4aSAnders Persson 	} else if (tcp->tcp_listener != NULL || tcp->tcp_hard_binding) {
47203e95bd4aSAnders Persson 		/*
47213e95bd4aSAnders Persson 		 * Side queue inbound data until the accept happens.
47223e95bd4aSAnders Persson 		 * tcp_accept/tcp_rput drains this when the accept happens.
47233e95bd4aSAnders Persson 		 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or
47243e95bd4aSAnders Persson 		 * T_EXDATA_IND) it is queued on b_next.
47253e95bd4aSAnders Persson 		 * XXX Make urgent data use this. Requires:
47263e95bd4aSAnders Persson 		 *	Removing tcp_listener check for TH_URG
47273e95bd4aSAnders Persson 		 *	Making M_PCPROTO and MARK messages skip the eager case
47283e95bd4aSAnders Persson 		 */
47293e95bd4aSAnders Persson 
47303e95bd4aSAnders Persson 		tcp_rcv_enqueue(tcp, mp, seg_len, ira->ira_cred);
47313e95bd4aSAnders Persson 	} else {
47323e95bd4aSAnders Persson 		/* Active STREAMS socket */
4733721fffe3SKacheong Poon 		if (mp->b_datap->db_type != M_DATA ||
4734721fffe3SKacheong Poon 		    (flags & TH_MARKNEXT_NEEDED)) {
4735721fffe3SKacheong Poon 			if (tcp->tcp_rcv_list != NULL) {
4736721fffe3SKacheong Poon 				flags |= tcp_rcv_drain(tcp);
4737721fffe3SKacheong Poon 			}
4738721fffe3SKacheong Poon 			ASSERT(tcp->tcp_rcv_list == NULL ||
4739721fffe3SKacheong Poon 			    tcp->tcp_fused_sigurg);
4740721fffe3SKacheong Poon 
4741721fffe3SKacheong Poon 			if (flags & TH_MARKNEXT_NEEDED) {
4742721fffe3SKacheong Poon #ifdef DEBUG
4743721fffe3SKacheong Poon 				(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
4744721fffe3SKacheong Poon 				    "tcp_rput: sending MSGMARKNEXT %s",
4745721fffe3SKacheong Poon 				    tcp_display(tcp, NULL,
4746721fffe3SKacheong Poon 				    DISP_PORT_ONLY));
4747721fffe3SKacheong Poon #endif /* DEBUG */
4748721fffe3SKacheong Poon 				mp->b_flag |= MSGMARKNEXT;
4749721fffe3SKacheong Poon 				flags &= ~TH_MARKNEXT_NEEDED;
4750721fffe3SKacheong Poon 			}
4751721fffe3SKacheong Poon 
4752721fffe3SKacheong Poon 			if (is_system_labeled())
4753721fffe3SKacheong Poon 				tcp_setcred_data(mp, ira);
4754721fffe3SKacheong Poon 
4755721fffe3SKacheong Poon 			putnext(connp->conn_rq, mp);
4756721fffe3SKacheong Poon 			if (!canputnext(connp->conn_rq))
4757721fffe3SKacheong Poon 				tcp->tcp_rwnd -= seg_len;
4758721fffe3SKacheong Poon 		} else if ((flags & (TH_PUSH|TH_FIN)) ||
4759721fffe3SKacheong Poon 		    tcp->tcp_rcv_cnt + seg_len >= connp->conn_rcvbuf >> 3) {
4760721fffe3SKacheong Poon 			if (tcp->tcp_rcv_list != NULL) {
4761721fffe3SKacheong Poon 				/*
4762721fffe3SKacheong Poon 				 * Enqueue the new segment first and then
4763721fffe3SKacheong Poon 				 * call tcp_rcv_drain() to send all data
4764721fffe3SKacheong Poon 				 * up.  The other way to do this is to
4765721fffe3SKacheong Poon 				 * send all queued data up and then call
4766721fffe3SKacheong Poon 				 * putnext() to send the new segment up.
4767721fffe3SKacheong Poon 				 * This way can remove the else part later
4768721fffe3SKacheong Poon 				 * on.
4769721fffe3SKacheong Poon 				 *
4770721fffe3SKacheong Poon 				 * We don't do this to avoid one more call to
4771721fffe3SKacheong Poon 				 * canputnext() as tcp_rcv_drain() needs to
4772721fffe3SKacheong Poon 				 * call canputnext().
4773721fffe3SKacheong Poon 				 */
4774721fffe3SKacheong Poon 				tcp_rcv_enqueue(tcp, mp, seg_len,
4775721fffe3SKacheong Poon 				    ira->ira_cred);
4776721fffe3SKacheong Poon 				flags |= tcp_rcv_drain(tcp);
4777721fffe3SKacheong Poon 			} else {
4778721fffe3SKacheong Poon 				if (is_system_labeled())
4779721fffe3SKacheong Poon 					tcp_setcred_data(mp, ira);
4780721fffe3SKacheong Poon 
4781721fffe3SKacheong Poon 				putnext(connp->conn_rq, mp);
4782721fffe3SKacheong Poon 				if (!canputnext(connp->conn_rq))
4783721fffe3SKacheong Poon 					tcp->tcp_rwnd -= seg_len;
4784721fffe3SKacheong Poon 			}
4785721fffe3SKacheong Poon 		} else {
4786721fffe3SKacheong Poon 			/*
4787721fffe3SKacheong Poon 			 * Enqueue all packets when processing an mblk
4788721fffe3SKacheong Poon 			 * from the co queue and also enqueue normal packets.
4789721fffe3SKacheong Poon 			 */
4790721fffe3SKacheong Poon 			tcp_rcv_enqueue(tcp, mp, seg_len, ira->ira_cred);
4791721fffe3SKacheong Poon 		}
4792721fffe3SKacheong Poon 		/*
4793721fffe3SKacheong Poon 		 * Make sure the timer is running if we have data waiting
4794721fffe3SKacheong Poon 		 * for a push bit. This provides resiliency against
4795721fffe3SKacheong Poon 		 * implementations that do not correctly generate push bits.
4796721fffe3SKacheong Poon 		 */
4797721fffe3SKacheong Poon 		if (tcp->tcp_rcv_list != NULL && tcp->tcp_push_tid == 0) {
4798721fffe3SKacheong Poon 			/*
4799721fffe3SKacheong Poon 			 * The connection may be closed at this point, so don't
4800721fffe3SKacheong Poon 			 * do anything for a detached tcp.
4801721fffe3SKacheong Poon 			 */
4802721fffe3SKacheong Poon 			if (!TCP_IS_DETACHED(tcp))
4803721fffe3SKacheong Poon 				tcp->tcp_push_tid = TCP_TIMER(tcp,
4804721fffe3SKacheong Poon 				    tcp_push_timer,
480566cd0f60SKacheong Poon 				    tcps->tcps_push_timer_interval);
4806721fffe3SKacheong Poon 		}
4807721fffe3SKacheong Poon 	}
4808721fffe3SKacheong Poon 
4809721fffe3SKacheong Poon xmit_check:
4810721fffe3SKacheong Poon 	/* Is there anything left to do? */
4811721fffe3SKacheong Poon 	ASSERT(!(flags & TH_MARKNEXT_NEEDED));
4812721fffe3SKacheong Poon 	if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED|
4813721fffe3SKacheong Poon 	    TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED|
4814721fffe3SKacheong Poon 	    TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0)
4815721fffe3SKacheong Poon 		goto done;
4816721fffe3SKacheong Poon 
4817721fffe3SKacheong Poon 	/* Any transmit work to do and a non-zero window? */
4818721fffe3SKacheong Poon 	if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT|
4819721fffe3SKacheong Poon 	    TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) {
4820721fffe3SKacheong Poon 		if (flags & TH_REXMIT_NEEDED) {
4821721fffe3SKacheong Poon 			uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna;
4822721fffe3SKacheong Poon 
4823721fffe3SKacheong Poon 			TCPS_BUMP_MIB(tcps, tcpOutFastRetrans);
4824721fffe3SKacheong Poon 			if (snd_size > mss)
4825721fffe3SKacheong Poon 				snd_size = mss;
4826721fffe3SKacheong Poon 			if (snd_size > tcp->tcp_swnd)
4827721fffe3SKacheong Poon 				snd_size = tcp->tcp_swnd;
4828721fffe3SKacheong Poon 			mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size,
4829721fffe3SKacheong Poon 			    NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size,
4830721fffe3SKacheong Poon 			    B_TRUE);
4831721fffe3SKacheong Poon 
4832721fffe3SKacheong Poon 			if (mp1 != NULL) {
4833721fffe3SKacheong Poon 				tcp->tcp_xmit_head->b_prev =
4834721fffe3SKacheong Poon 				    (mblk_t *)LBOLT_FASTPATH;
4835721fffe3SKacheong Poon 				tcp->tcp_csuna = tcp->tcp_snxt;
4836721fffe3SKacheong Poon 				TCPS_BUMP_MIB(tcps, tcpRetransSegs);
4837721fffe3SKacheong Poon 				TCPS_UPDATE_MIB(tcps, tcpRetransBytes,
4838721fffe3SKacheong Poon 				    snd_size);
4839721fffe3SKacheong Poon 				tcp_send_data(tcp, mp1);
4840721fffe3SKacheong Poon 			}
4841721fffe3SKacheong Poon 		}
4842721fffe3SKacheong Poon 		if (flags & TH_NEED_SACK_REXMIT) {
4843721fffe3SKacheong Poon 			tcp_sack_rexmit(tcp, &flags);
4844721fffe3SKacheong Poon 		}
4845721fffe3SKacheong Poon 		/*
4846721fffe3SKacheong Poon 		 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send
4847721fffe3SKacheong Poon 		 * out new segment.  Note that tcp_rexmit should not be
4848721fffe3SKacheong Poon 		 * set, otherwise TH_LIMIT_XMIT should not be set.
4849721fffe3SKacheong Poon 		 */
4850721fffe3SKacheong Poon 		if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) {
4851721fffe3SKacheong Poon 			if (!tcp->tcp_rexmit) {
4852721fffe3SKacheong Poon 				tcp_wput_data(tcp, NULL, B_FALSE);
4853721fffe3SKacheong Poon 			} else {
4854721fffe3SKacheong Poon 				tcp_ss_rexmit(tcp);
4855721fffe3SKacheong Poon 			}
4856721fffe3SKacheong Poon 		}
4857721fffe3SKacheong Poon 		/*
4858721fffe3SKacheong Poon 		 * Adjust tcp_cwnd back to normal value after sending
4859721fffe3SKacheong Poon 		 * new data segments.
4860721fffe3SKacheong Poon 		 */
4861721fffe3SKacheong Poon 		if (flags & TH_LIMIT_XMIT) {
4862721fffe3SKacheong Poon 			tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1);
4863721fffe3SKacheong Poon 			/*
4864721fffe3SKacheong Poon 			 * This will restart the timer.  Restarting the
4865721fffe3SKacheong Poon 			 * timer is used to avoid a timeout before the
4866721fffe3SKacheong Poon 			 * limited transmitted segment's ACK gets back.
4867721fffe3SKacheong Poon 			 */
4868721fffe3SKacheong Poon 			if (tcp->tcp_xmit_head != NULL)
4869721fffe3SKacheong Poon 				tcp->tcp_xmit_head->b_prev =
4870721fffe3SKacheong Poon 				    (mblk_t *)LBOLT_FASTPATH;
4871721fffe3SKacheong Poon 		}
4872721fffe3SKacheong Poon 
4873721fffe3SKacheong Poon 		/* Anything more to do? */
4874721fffe3SKacheong Poon 		if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED|
4875721fffe3SKacheong Poon 		    TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0)
4876721fffe3SKacheong Poon 			goto done;
4877721fffe3SKacheong Poon 	}
4878721fffe3SKacheong Poon ack_check:
4879721fffe3SKacheong Poon 	if (flags & TH_SEND_URP_MARK) {
4880721fffe3SKacheong Poon 		ASSERT(tcp->tcp_urp_mark_mp);
4881721fffe3SKacheong Poon 		ASSERT(!IPCL_IS_NONSTR(connp));
4882721fffe3SKacheong Poon 		/*
4883721fffe3SKacheong Poon 		 * Send up any queued data and then send the mark message
4884721fffe3SKacheong Poon 		 */
4885721fffe3SKacheong Poon 		if (tcp->tcp_rcv_list != NULL) {
4886721fffe3SKacheong Poon 			flags |= tcp_rcv_drain(tcp);
4887721fffe3SKacheong Poon 
4888721fffe3SKacheong Poon 		}
4889721fffe3SKacheong Poon 		ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg);
4890721fffe3SKacheong Poon 		mp1 = tcp->tcp_urp_mark_mp;
4891721fffe3SKacheong Poon 		tcp->tcp_urp_mark_mp = NULL;
4892721fffe3SKacheong Poon 		if (is_system_labeled())
4893721fffe3SKacheong Poon 			tcp_setcred_data(mp1, ira);
4894721fffe3SKacheong Poon 
4895721fffe3SKacheong Poon 		putnext(connp->conn_rq, mp1);
4896721fffe3SKacheong Poon #ifdef DEBUG
4897721fffe3SKacheong Poon 		(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
4898721fffe3SKacheong Poon 		    "tcp_rput: sending zero-length %s %s",
4899721fffe3SKacheong Poon 		    ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" :
4900721fffe3SKacheong Poon 		    "MSGNOTMARKNEXT"),
4901721fffe3SKacheong Poon 		    tcp_display(tcp, NULL, DISP_PORT_ONLY));
4902721fffe3SKacheong Poon #endif /* DEBUG */
4903721fffe3SKacheong Poon 		flags &= ~TH_SEND_URP_MARK;
4904721fffe3SKacheong Poon 	}
4905721fffe3SKacheong Poon 	if (flags & TH_ACK_NEEDED) {
4906721fffe3SKacheong Poon 		/*
4907721fffe3SKacheong Poon 		 * Time to send an ack for some reason.
4908721fffe3SKacheong Poon 		 */
4909721fffe3SKacheong Poon 		mp1 = tcp_ack_mp(tcp);
4910721fffe3SKacheong Poon 
4911721fffe3SKacheong Poon 		if (mp1 != NULL) {
4912721fffe3SKacheong Poon 			tcp_send_data(tcp, mp1);
4913721fffe3SKacheong Poon 			BUMP_LOCAL(tcp->tcp_obsegs);
4914721fffe3SKacheong Poon 			TCPS_BUMP_MIB(tcps, tcpOutAck);
4915721fffe3SKacheong Poon 		}
4916721fffe3SKacheong Poon 		if (tcp->tcp_ack_tid != 0) {
4917721fffe3SKacheong Poon 			(void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid);
4918721fffe3SKacheong Poon 			tcp->tcp_ack_tid = 0;
4919721fffe3SKacheong Poon 		}
4920721fffe3SKacheong Poon 	}
4921721fffe3SKacheong Poon 	if (flags & TH_ACK_TIMER_NEEDED) {
4922721fffe3SKacheong Poon 		/*
4923721fffe3SKacheong Poon 		 * Arrange for deferred ACK or push wait timeout.
4924721fffe3SKacheong Poon 		 * Start timer if it is not already running.
4925721fffe3SKacheong Poon 		 */
4926721fffe3SKacheong Poon 		if (tcp->tcp_ack_tid == 0) {
4927721fffe3SKacheong Poon 			tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer,
492866cd0f60SKacheong Poon 			    tcp->tcp_localnet ?
492966cd0f60SKacheong Poon 			    tcps->tcps_local_dack_interval :
493066cd0f60SKacheong Poon 			    tcps->tcps_deferred_ack_interval);
4931721fffe3SKacheong Poon 		}
4932721fffe3SKacheong Poon 	}
4933721fffe3SKacheong Poon 	if (flags & TH_ORDREL_NEEDED) {
4934721fffe3SKacheong Poon 		/*
49353e95bd4aSAnders Persson 		 * Notify upper layer about an orderly release. If this is
49363e95bd4aSAnders Persson 		 * a non-STREAMS socket, then just make an upcall. For STREAMS
49373e95bd4aSAnders Persson 		 * we send up an ordrel_ind, unless this is an eager, in which
49383e95bd4aSAnders Persson 		 * case the ordrel will be sent when tcp_accept_finish runs.
49393e95bd4aSAnders Persson 		 * Note that for non-STREAMS we make an upcall even if it is an
49403e95bd4aSAnders Persson 		 * eager, because we have an upper handle to send it to.
4941721fffe3SKacheong Poon 		 */
49423e95bd4aSAnders Persson 		ASSERT(IPCL_IS_NONSTR(connp) || tcp->tcp_listener == NULL);
4943721fffe3SKacheong Poon 		ASSERT(!tcp->tcp_detached);
4944721fffe3SKacheong Poon 
4945721fffe3SKacheong Poon 		if (IPCL_IS_NONSTR(connp)) {
4946721fffe3SKacheong Poon 			ASSERT(tcp->tcp_ordrel_mp == NULL);
4947721fffe3SKacheong Poon 			tcp->tcp_ordrel_done = B_TRUE;
4948f2297e92SDan McDonald 			(*sockupcalls->su_opctl)(connp->conn_upper_handle,
4949f2297e92SDan McDonald 			    SOCK_OPCTL_SHUT_RECV, 0);
4950721fffe3SKacheong Poon 			goto done;
4951721fffe3SKacheong Poon 		}
4952721fffe3SKacheong Poon 
4953721fffe3SKacheong Poon 		if (tcp->tcp_rcv_list != NULL) {
4954721fffe3SKacheong Poon 			/*
4955721fffe3SKacheong Poon 			 * Push any mblk(s) enqueued from co processing.
4956721fffe3SKacheong Poon 			 */
4957721fffe3SKacheong Poon 			flags |= tcp_rcv_drain(tcp);
4958721fffe3SKacheong Poon 		}
4959721fffe3SKacheong Poon 		ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg);
4960721fffe3SKacheong Poon 
4961721fffe3SKacheong Poon 		mp1 = tcp->tcp_ordrel_mp;
4962721fffe3SKacheong Poon 		tcp->tcp_ordrel_mp = NULL;
4963721fffe3SKacheong Poon 		tcp->tcp_ordrel_done = B_TRUE;
4964721fffe3SKacheong Poon 		putnext(connp->conn_rq, mp1);
4965721fffe3SKacheong Poon 	}
4966721fffe3SKacheong Poon done:
4967721fffe3SKacheong Poon 	ASSERT(!(flags & TH_MARKNEXT_NEEDED));
4968721fffe3SKacheong Poon }
4969721fffe3SKacheong Poon 
4970721fffe3SKacheong Poon /*
4971721fffe3SKacheong Poon  * Attach ancillary data to a received TCP segments for the
4972721fffe3SKacheong Poon  * ancillary pieces requested by the application that are
4973721fffe3SKacheong Poon  * different than they were in the previous data segment.
4974721fffe3SKacheong Poon  *
4975721fffe3SKacheong Poon  * Save the "current" values once memory allocation is ok so that
4976721fffe3SKacheong Poon  * when memory allocation fails we can just wait for the next data segment.
4977721fffe3SKacheong Poon  */
4978721fffe3SKacheong Poon static mblk_t *
4979721fffe3SKacheong Poon tcp_input_add_ancillary(tcp_t *tcp, mblk_t *mp, ip_pkt_t *ipp,
4980721fffe3SKacheong Poon     ip_recv_attr_t *ira)
4981721fffe3SKacheong Poon {
4982721fffe3SKacheong Poon 	struct T_optdata_ind *todi;
4983721fffe3SKacheong Poon 	int optlen;
4984721fffe3SKacheong Poon 	uchar_t *optptr;
4985721fffe3SKacheong Poon 	struct T_opthdr *toh;
4986721fffe3SKacheong Poon 	crb_t addflag;	/* Which pieces to add */
4987721fffe3SKacheong Poon 	mblk_t *mp1;
4988721fffe3SKacheong Poon 	conn_t	*connp = tcp->tcp_connp;
4989721fffe3SKacheong Poon 
4990721fffe3SKacheong Poon 	optlen = 0;
4991721fffe3SKacheong Poon 	addflag.crb_all = 0;
4992721fffe3SKacheong Poon 	/* If app asked for pktinfo and the index has changed ... */
4993721fffe3SKacheong Poon 	if (connp->conn_recv_ancillary.crb_ip_recvpktinfo &&
4994721fffe3SKacheong Poon 	    ira->ira_ruifindex != tcp->tcp_recvifindex) {
4995721fffe3SKacheong Poon 		optlen += sizeof (struct T_opthdr) +
4996721fffe3SKacheong Poon 		    sizeof (struct in6_pktinfo);
4997721fffe3SKacheong Poon 		addflag.crb_ip_recvpktinfo = 1;
4998721fffe3SKacheong Poon 	}
4999721fffe3SKacheong Poon 	/* If app asked for hoplimit and it has changed ... */
5000721fffe3SKacheong Poon 	if (connp->conn_recv_ancillary.crb_ipv6_recvhoplimit &&
5001721fffe3SKacheong Poon 	    ipp->ipp_hoplimit != tcp->tcp_recvhops) {
5002721fffe3SKacheong Poon 		optlen += sizeof (struct T_opthdr) + sizeof (uint_t);
5003721fffe3SKacheong Poon 		addflag.crb_ipv6_recvhoplimit = 1;
5004721fffe3SKacheong Poon 	}
5005721fffe3SKacheong Poon 	/* If app asked for tclass and it has changed ... */
5006721fffe3SKacheong Poon 	if (connp->conn_recv_ancillary.crb_ipv6_recvtclass &&
5007721fffe3SKacheong Poon 	    ipp->ipp_tclass != tcp->tcp_recvtclass) {
5008721fffe3SKacheong Poon 		optlen += sizeof (struct T_opthdr) + sizeof (uint_t);
5009721fffe3SKacheong Poon 		addflag.crb_ipv6_recvtclass = 1;
5010721fffe3SKacheong Poon 	}
5011721fffe3SKacheong Poon 	/*
5012721fffe3SKacheong Poon 	 * If app asked for hopbyhop headers and it has changed ...
5013721fffe3SKacheong Poon 	 * For security labels, note that (1) security labels can't change on
5014721fffe3SKacheong Poon 	 * a connected socket at all, (2) we're connected to at most one peer,
5015721fffe3SKacheong Poon 	 * (3) if anything changes, then it must be some other extra option.
5016721fffe3SKacheong Poon 	 */
5017721fffe3SKacheong Poon 	if (connp->conn_recv_ancillary.crb_ipv6_recvhopopts &&
5018721fffe3SKacheong Poon 	    ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen,
5019721fffe3SKacheong Poon 	    (ipp->ipp_fields & IPPF_HOPOPTS),
5020721fffe3SKacheong Poon 	    ipp->ipp_hopopts, ipp->ipp_hopoptslen)) {
5021721fffe3SKacheong Poon 		optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen;
5022721fffe3SKacheong Poon 		addflag.crb_ipv6_recvhopopts = 1;
5023721fffe3SKacheong Poon 		if (!ip_allocbuf((void **)&tcp->tcp_hopopts,
5024721fffe3SKacheong Poon 		    &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS),
5025721fffe3SKacheong Poon 		    ipp->ipp_hopopts, ipp->ipp_hopoptslen))
5026721fffe3SKacheong Poon 			return (mp);
5027721fffe3SKacheong Poon 	}
5028721fffe3SKacheong Poon 	/* If app asked for dst headers before routing headers ... */
5029721fffe3SKacheong Poon 	if (connp->conn_recv_ancillary.crb_ipv6_recvrthdrdstopts &&
5030721fffe3SKacheong Poon 	    ip_cmpbuf(tcp->tcp_rthdrdstopts, tcp->tcp_rthdrdstoptslen,
5031721fffe3SKacheong Poon 	    (ipp->ipp_fields & IPPF_RTHDRDSTOPTS),
5032721fffe3SKacheong Poon 	    ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen)) {
5033721fffe3SKacheong Poon 		optlen += sizeof (struct T_opthdr) +
5034721fffe3SKacheong Poon 		    ipp->ipp_rthdrdstoptslen;
5035721fffe3SKacheong Poon 		addflag.crb_ipv6_recvrthdrdstopts = 1;
5036721fffe3SKacheong Poon 		if (!ip_allocbuf((void **)&tcp->tcp_rthdrdstopts,
5037721fffe3SKacheong Poon 		    &tcp->tcp_rthdrdstoptslen,
5038721fffe3SKacheong Poon 		    (ipp->ipp_fields & IPPF_RTHDRDSTOPTS),
5039721fffe3SKacheong Poon 		    ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen))
5040721fffe3SKacheong Poon 			return (mp);
5041721fffe3SKacheong Poon 	}
5042721fffe3SKacheong Poon 	/* If app asked for routing headers and it has changed ... */
5043721fffe3SKacheong Poon 	if (connp->conn_recv_ancillary.crb_ipv6_recvrthdr &&
5044721fffe3SKacheong Poon 	    ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen,
5045721fffe3SKacheong Poon 	    (ipp->ipp_fields & IPPF_RTHDR),
5046721fffe3SKacheong Poon 	    ipp->ipp_rthdr, ipp->ipp_rthdrlen)) {
5047721fffe3SKacheong Poon 		optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen;
5048721fffe3SKacheong Poon 		addflag.crb_ipv6_recvrthdr = 1;
5049721fffe3SKacheong Poon 		if (!ip_allocbuf((void **)&tcp->tcp_rthdr,
5050721fffe3SKacheong Poon 		    &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR),
5051721fffe3SKacheong Poon 		    ipp->ipp_rthdr, ipp->ipp_rthdrlen))
5052721fffe3SKacheong Poon 			return (mp);
5053721fffe3SKacheong Poon 	}
5054721fffe3SKacheong Poon 	/* If app asked for dest headers and it has changed ... */
5055721fffe3SKacheong Poon 	if ((connp->conn_recv_ancillary.crb_ipv6_recvdstopts ||
5056721fffe3SKacheong Poon 	    connp->conn_recv_ancillary.crb_old_ipv6_recvdstopts) &&
5057721fffe3SKacheong Poon 	    ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen,
5058721fffe3SKacheong Poon 	    (ipp->ipp_fields & IPPF_DSTOPTS),
5059721fffe3SKacheong Poon 	    ipp->ipp_dstopts, ipp->ipp_dstoptslen)) {
5060721fffe3SKacheong Poon 		optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen;
5061721fffe3SKacheong Poon 		addflag.crb_ipv6_recvdstopts = 1;
5062721fffe3SKacheong Poon 		if (!ip_allocbuf((void **)&tcp->tcp_dstopts,
5063721fffe3SKacheong Poon 		    &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS),
5064721fffe3SKacheong Poon 		    ipp->ipp_dstopts, ipp->ipp_dstoptslen))
5065721fffe3SKacheong Poon 			return (mp);
5066721fffe3SKacheong Poon 	}
5067721fffe3SKacheong Poon 
5068721fffe3SKacheong Poon 	if (optlen == 0) {
5069721fffe3SKacheong Poon 		/* Nothing to add */
5070721fffe3SKacheong Poon 		return (mp);
5071721fffe3SKacheong Poon 	}
5072721fffe3SKacheong Poon 	mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED);
5073721fffe3SKacheong Poon 	if (mp1 == NULL) {
5074721fffe3SKacheong Poon 		/*
5075721fffe3SKacheong Poon 		 * Defer sending ancillary data until the next TCP segment
5076721fffe3SKacheong Poon 		 * arrives.
5077721fffe3SKacheong Poon 		 */
5078721fffe3SKacheong Poon 		return (mp);
5079721fffe3SKacheong Poon 	}
5080721fffe3SKacheong Poon 	mp1->b_cont = mp;
5081721fffe3SKacheong Poon 	mp = mp1;
5082721fffe3SKacheong Poon 	mp->b_wptr += sizeof (*todi) + optlen;
5083721fffe3SKacheong Poon 	mp->b_datap->db_type = M_PROTO;
5084721fffe3SKacheong Poon 	todi = (struct T_optdata_ind *)mp->b_rptr;
5085721fffe3SKacheong Poon 	todi->PRIM_type = T_OPTDATA_IND;
5086721fffe3SKacheong Poon 	todi->DATA_flag = 1;	/* MORE data */
5087721fffe3SKacheong Poon 	todi->OPT_length = optlen;
5088721fffe3SKacheong Poon 	todi->OPT_offset = sizeof (*todi);
5089721fffe3SKacheong Poon 	optptr = (uchar_t *)&todi[1];
5090721fffe3SKacheong Poon 	/*
5091721fffe3SKacheong Poon 	 * If app asked for pktinfo and the index has changed ...
5092721fffe3SKacheong Poon 	 * Note that the local address never changes for the connection.
5093721fffe3SKacheong Poon 	 */
5094721fffe3SKacheong Poon 	if (addflag.crb_ip_recvpktinfo) {
5095721fffe3SKacheong Poon 		struct in6_pktinfo *pkti;
5096721fffe3SKacheong Poon 		uint_t ifindex;
5097721fffe3SKacheong Poon 
5098721fffe3SKacheong Poon 		ifindex = ira->ira_ruifindex;
5099721fffe3SKacheong Poon 		toh = (struct T_opthdr *)optptr;
5100721fffe3SKacheong Poon 		toh->level = IPPROTO_IPV6;
5101721fffe3SKacheong Poon 		toh->name = IPV6_PKTINFO;
5102721fffe3SKacheong Poon 		toh->len = sizeof (*toh) + sizeof (*pkti);
5103721fffe3SKacheong Poon 		toh->status = 0;
5104721fffe3SKacheong Poon 		optptr += sizeof (*toh);
5105721fffe3SKacheong Poon 		pkti = (struct in6_pktinfo *)optptr;
5106721fffe3SKacheong Poon 		pkti->ipi6_addr = connp->conn_laddr_v6;
5107721fffe3SKacheong Poon 		pkti->ipi6_ifindex = ifindex;
5108721fffe3SKacheong Poon 		optptr += sizeof (*pkti);
5109721fffe3SKacheong Poon 		ASSERT(OK_32PTR(optptr));
5110721fffe3SKacheong Poon 		/* Save as "last" value */
5111721fffe3SKacheong Poon 		tcp->tcp_recvifindex = ifindex;
5112721fffe3SKacheong Poon 	}
5113721fffe3SKacheong Poon 	/* If app asked for hoplimit and it has changed ... */
5114721fffe3SKacheong Poon 	if (addflag.crb_ipv6_recvhoplimit) {
5115721fffe3SKacheong Poon 		toh = (struct T_opthdr *)optptr;
5116721fffe3SKacheong Poon 		toh->level = IPPROTO_IPV6;
5117721fffe3SKacheong Poon 		toh->name = IPV6_HOPLIMIT;
5118721fffe3SKacheong Poon 		toh->len = sizeof (*toh) + sizeof (uint_t);
5119721fffe3SKacheong Poon 		toh->status = 0;
5120721fffe3SKacheong Poon 		optptr += sizeof (*toh);
5121721fffe3SKacheong Poon 		*(uint_t *)optptr = ipp->ipp_hoplimit;
5122721fffe3SKacheong Poon 		optptr += sizeof (uint_t);
5123721fffe3SKacheong Poon 		ASSERT(OK_32PTR(optptr));
5124721fffe3SKacheong Poon 		/* Save as "last" value */
5125721fffe3SKacheong Poon 		tcp->tcp_recvhops = ipp->ipp_hoplimit;
5126721fffe3SKacheong Poon 	}
5127721fffe3SKacheong Poon 	/* If app asked for tclass and it has changed ... */
5128721fffe3SKacheong Poon 	if (addflag.crb_ipv6_recvtclass) {
5129721fffe3SKacheong Poon 		toh = (struct T_opthdr *)optptr;
5130721fffe3SKacheong Poon 		toh->level = IPPROTO_IPV6;
5131721fffe3SKacheong Poon 		toh->name = IPV6_TCLASS;
5132721fffe3SKacheong Poon 		toh->len = sizeof (*toh) + sizeof (uint_t);
5133721fffe3SKacheong Poon 		toh->status = 0;
5134721fffe3SKacheong Poon 		optptr += sizeof (*toh);
5135721fffe3SKacheong Poon 		*(uint_t *)optptr = ipp->ipp_tclass;
5136721fffe3SKacheong Poon 		optptr += sizeof (uint_t);
5137721fffe3SKacheong Poon 		ASSERT(OK_32PTR(optptr));
5138721fffe3SKacheong Poon 		/* Save as "last" value */
5139721fffe3SKacheong Poon 		tcp->tcp_recvtclass = ipp->ipp_tclass;
5140721fffe3SKacheong Poon 	}
5141721fffe3SKacheong Poon 	if (addflag.crb_ipv6_recvhopopts) {
5142721fffe3SKacheong Poon 		toh = (struct T_opthdr *)optptr;
5143721fffe3SKacheong Poon 		toh->level = IPPROTO_IPV6;
5144721fffe3SKacheong Poon 		toh->name = IPV6_HOPOPTS;
5145721fffe3SKacheong Poon 		toh->len = sizeof (*toh) + ipp->ipp_hopoptslen;
5146721fffe3SKacheong Poon 		toh->status = 0;
5147721fffe3SKacheong Poon 		optptr += sizeof (*toh);
5148721fffe3SKacheong Poon 		bcopy((uchar_t *)ipp->ipp_hopopts, optptr, ipp->ipp_hopoptslen);
5149721fffe3SKacheong Poon 		optptr += ipp->ipp_hopoptslen;
5150721fffe3SKacheong Poon 		ASSERT(OK_32PTR(optptr));
5151721fffe3SKacheong Poon 		/* Save as last value */
5152721fffe3SKacheong Poon 		ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen,
5153721fffe3SKacheong Poon 		    (ipp->ipp_fields & IPPF_HOPOPTS),
5154721fffe3SKacheong Poon 		    ipp->ipp_hopopts, ipp->ipp_hopoptslen);
5155721fffe3SKacheong Poon 	}
5156721fffe3SKacheong Poon 	if (addflag.crb_ipv6_recvrthdrdstopts) {
5157721fffe3SKacheong Poon 		toh = (struct T_opthdr *)optptr;
5158721fffe3SKacheong Poon 		toh->level = IPPROTO_IPV6;
5159721fffe3SKacheong Poon 		toh->name = IPV6_RTHDRDSTOPTS;
5160721fffe3SKacheong Poon 		toh->len = sizeof (*toh) + ipp->ipp_rthdrdstoptslen;
5161721fffe3SKacheong Poon 		toh->status = 0;
5162721fffe3SKacheong Poon 		optptr += sizeof (*toh);
5163721fffe3SKacheong Poon 		bcopy(ipp->ipp_rthdrdstopts, optptr, ipp->ipp_rthdrdstoptslen);
5164721fffe3SKacheong Poon 		optptr += ipp->ipp_rthdrdstoptslen;
5165721fffe3SKacheong Poon 		ASSERT(OK_32PTR(optptr));
5166721fffe3SKacheong Poon 		/* Save as last value */
5167721fffe3SKacheong Poon 		ip_savebuf((void **)&tcp->tcp_rthdrdstopts,
5168721fffe3SKacheong Poon 		    &tcp->tcp_rthdrdstoptslen,
5169721fffe3SKacheong Poon 		    (ipp->ipp_fields & IPPF_RTHDRDSTOPTS),
5170721fffe3SKacheong Poon 		    ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen);
5171721fffe3SKacheong Poon 	}
5172721fffe3SKacheong Poon 	if (addflag.crb_ipv6_recvrthdr) {
5173721fffe3SKacheong Poon 		toh = (struct T_opthdr *)optptr;
5174721fffe3SKacheong Poon 		toh->level = IPPROTO_IPV6;
5175721fffe3SKacheong Poon 		toh->name = IPV6_RTHDR;
5176721fffe3SKacheong Poon 		toh->len = sizeof (*toh) + ipp->ipp_rthdrlen;
5177721fffe3SKacheong Poon 		toh->status = 0;
5178721fffe3SKacheong Poon 		optptr += sizeof (*toh);
5179721fffe3SKacheong Poon 		bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen);
5180721fffe3SKacheong Poon 		optptr += ipp->ipp_rthdrlen;
5181721fffe3SKacheong Poon 		ASSERT(OK_32PTR(optptr));
5182721fffe3SKacheong Poon 		/* Save as last value */
5183721fffe3SKacheong Poon 		ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen,
5184721fffe3SKacheong Poon 		    (ipp->ipp_fields & IPPF_RTHDR),
5185721fffe3SKacheong Poon 		    ipp->ipp_rthdr, ipp->ipp_rthdrlen);
5186721fffe3SKacheong Poon 	}
5187721fffe3SKacheong Poon 	if (addflag.crb_ipv6_recvdstopts) {
5188721fffe3SKacheong Poon 		toh = (struct T_opthdr *)optptr;
5189721fffe3SKacheong Poon 		toh->level = IPPROTO_IPV6;
5190721fffe3SKacheong Poon 		toh->name = IPV6_DSTOPTS;
5191721fffe3SKacheong Poon 		toh->len = sizeof (*toh) + ipp->ipp_dstoptslen;
5192721fffe3SKacheong Poon 		toh->status = 0;
5193721fffe3SKacheong Poon 		optptr += sizeof (*toh);
5194721fffe3SKacheong Poon 		bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen);
5195721fffe3SKacheong Poon 		optptr += ipp->ipp_dstoptslen;
5196721fffe3SKacheong Poon 		ASSERT(OK_32PTR(optptr));
5197721fffe3SKacheong Poon 		/* Save as last value */
5198721fffe3SKacheong Poon 		ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen,
5199721fffe3SKacheong Poon 		    (ipp->ipp_fields & IPPF_DSTOPTS),
5200721fffe3SKacheong Poon 		    ipp->ipp_dstopts, ipp->ipp_dstoptslen);
5201721fffe3SKacheong Poon 	}
5202721fffe3SKacheong Poon 	ASSERT(optptr == mp->b_wptr);
5203721fffe3SKacheong Poon 	return (mp);
5204721fffe3SKacheong Poon }
5205721fffe3SKacheong Poon 
5206721fffe3SKacheong Poon /* The minimum of smoothed mean deviation in RTO calculation. */
5207721fffe3SKacheong Poon #define	TCP_SD_MIN	400
5208721fffe3SKacheong Poon 
5209721fffe3SKacheong Poon /*
5210721fffe3SKacheong Poon  * Set RTO for this connection.  The formula is from Jacobson and Karels'
5211721fffe3SKacheong Poon  * "Congestion Avoidance and Control" in SIGCOMM '88.  The variable names
5212721fffe3SKacheong Poon  * are the same as those in Appendix A.2 of that paper.
5213721fffe3SKacheong Poon  *
5214721fffe3SKacheong Poon  * m = new measurement
5215721fffe3SKacheong Poon  * sa = smoothed RTT average (8 * average estimates).
5216721fffe3SKacheong Poon  * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates).
5217721fffe3SKacheong Poon  */
5218721fffe3SKacheong Poon static void
5219721fffe3SKacheong Poon tcp_set_rto(tcp_t *tcp, clock_t rtt)
5220721fffe3SKacheong Poon {
5221721fffe3SKacheong Poon 	long m = TICK_TO_MSEC(rtt);
5222721fffe3SKacheong Poon 	clock_t sa = tcp->tcp_rtt_sa;
5223721fffe3SKacheong Poon 	clock_t sv = tcp->tcp_rtt_sd;
5224721fffe3SKacheong Poon 	clock_t rto;
5225721fffe3SKacheong Poon 	tcp_stack_t	*tcps = tcp->tcp_tcps;
5226721fffe3SKacheong Poon 
5227721fffe3SKacheong Poon 	TCPS_BUMP_MIB(tcps, tcpRttUpdate);
5228721fffe3SKacheong Poon 	tcp->tcp_rtt_update++;
5229721fffe3SKacheong Poon 
5230721fffe3SKacheong Poon 	/* tcp_rtt_sa is not 0 means this is a new sample. */
5231721fffe3SKacheong Poon 	if (sa != 0) {
5232721fffe3SKacheong Poon 		/*
5233721fffe3SKacheong Poon 		 * Update average estimator:
5234721fffe3SKacheong Poon 		 *	new rtt = 7/8 old rtt + 1/8 Error
5235721fffe3SKacheong Poon 		 */
5236721fffe3SKacheong Poon 
5237721fffe3SKacheong Poon 		/* m is now Error in estimate. */
5238721fffe3SKacheong Poon 		m -= sa >> 3;
5239721fffe3SKacheong Poon 		if ((sa += m) <= 0) {
5240721fffe3SKacheong Poon 			/*
5241721fffe3SKacheong Poon 			 * Don't allow the smoothed average to be negative.
5242721fffe3SKacheong Poon 			 * We use 0 to denote reinitialization of the
5243721fffe3SKacheong Poon 			 * variables.
5244721fffe3SKacheong Poon 			 */
5245721fffe3SKacheong Poon 			sa = 1;
5246721fffe3SKacheong Poon 		}
5247721fffe3SKacheong Poon 
5248721fffe3SKacheong Poon 		/*
5249721fffe3SKacheong Poon 		 * Update deviation estimator:
5250721fffe3SKacheong Poon 		 *	new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev)
5251721fffe3SKacheong Poon 		 */
5252721fffe3SKacheong Poon 		if (m < 0)
5253721fffe3SKacheong Poon 			m = -m;
5254721fffe3SKacheong Poon 		m -= sv >> 2;
5255721fffe3SKacheong Poon 		sv += m;
5256721fffe3SKacheong Poon 	} else {
5257721fffe3SKacheong Poon 		/*
5258721fffe3SKacheong Poon 		 * This follows BSD's implementation.  So the reinitialized
5259721fffe3SKacheong Poon 		 * RTO is 3 * m.  We cannot go less than 2 because if the
5260721fffe3SKacheong Poon 		 * link is bandwidth dominated, doubling the window size
5261721fffe3SKacheong Poon 		 * during slow start means doubling the RTT.  We want to be
5262721fffe3SKacheong Poon 		 * more conservative when we reinitialize our estimates.  3
5263721fffe3SKacheong Poon 		 * is just a convenient number.
5264721fffe3SKacheong Poon 		 */
5265721fffe3SKacheong Poon 		sa = m << 3;
5266721fffe3SKacheong Poon 		sv = m << 1;
5267721fffe3SKacheong Poon 	}
5268721fffe3SKacheong Poon 	if (sv < TCP_SD_MIN) {
5269721fffe3SKacheong Poon 		/*
5270721fffe3SKacheong Poon 		 * We do not know that if sa captures the delay ACK
5271721fffe3SKacheong Poon 		 * effect as in a long train of segments, a receiver
5272721fffe3SKacheong Poon 		 * does not delay its ACKs.  So set the minimum of sv
5273721fffe3SKacheong Poon 		 * to be TCP_SD_MIN, which is default to 400 ms, twice
5274721fffe3SKacheong Poon 		 * of BSD DATO.  That means the minimum of mean
5275721fffe3SKacheong Poon 		 * deviation is 100 ms.
5276721fffe3SKacheong Poon 		 *
5277721fffe3SKacheong Poon 		 */
5278721fffe3SKacheong Poon 		sv = TCP_SD_MIN;
5279721fffe3SKacheong Poon 	}
5280721fffe3SKacheong Poon 	tcp->tcp_rtt_sa = sa;
5281721fffe3SKacheong Poon 	tcp->tcp_rtt_sd = sv;
5282721fffe3SKacheong Poon 	/*
5283721fffe3SKacheong Poon 	 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv)
5284721fffe3SKacheong Poon 	 *
5285721fffe3SKacheong Poon 	 * Add tcp_rexmit_interval extra in case of extreme environment
5286721fffe3SKacheong Poon 	 * where the algorithm fails to work.  The default value of
5287721fffe3SKacheong Poon 	 * tcp_rexmit_interval_extra should be 0.
5288721fffe3SKacheong Poon 	 *
5289721fffe3SKacheong Poon 	 * As we use a finer grained clock than BSD and update
5290721fffe3SKacheong Poon 	 * RTO for every ACKs, add in another .25 of RTT to the
5291721fffe3SKacheong Poon 	 * deviation of RTO to accomodate burstiness of 1/4 of
5292721fffe3SKacheong Poon 	 * window size.
5293721fffe3SKacheong Poon 	 */
5294721fffe3SKacheong Poon 	rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5);
5295721fffe3SKacheong Poon 
5296707e74bcSKacheong Poon 	TCP_SET_RTO(tcp, rto);
5297721fffe3SKacheong Poon 
5298721fffe3SKacheong Poon 	/* Now, we can reset tcp_timer_backoff to use the new RTO... */
5299721fffe3SKacheong Poon 	tcp->tcp_timer_backoff = 0;
5300721fffe3SKacheong Poon }
5301721fffe3SKacheong Poon 
5302721fffe3SKacheong Poon /*
5303721fffe3SKacheong Poon  * On a labeled system we have some protocols above TCP, such as RPC, which
5304721fffe3SKacheong Poon  * appear to assume that every mblk in a chain has a db_credp.
5305721fffe3SKacheong Poon  */
5306721fffe3SKacheong Poon static void
5307721fffe3SKacheong Poon tcp_setcred_data(mblk_t *mp, ip_recv_attr_t *ira)
5308721fffe3SKacheong Poon {
5309721fffe3SKacheong Poon 	ASSERT(is_system_labeled());
5310721fffe3SKacheong Poon 	ASSERT(ira->ira_cred != NULL);
5311721fffe3SKacheong Poon 
5312721fffe3SKacheong Poon 	while (mp != NULL) {
5313721fffe3SKacheong Poon 		mblk_setcred(mp, ira->ira_cred, NOPID);
5314721fffe3SKacheong Poon 		mp = mp->b_cont;
5315721fffe3SKacheong Poon 	}
5316721fffe3SKacheong Poon }
5317721fffe3SKacheong Poon 
5318721fffe3SKacheong Poon uint_t
5319721fffe3SKacheong Poon tcp_rwnd_reopen(tcp_t *tcp)
5320721fffe3SKacheong Poon {
5321721fffe3SKacheong Poon 	uint_t ret = 0;
5322721fffe3SKacheong Poon 	uint_t thwin;
5323721fffe3SKacheong Poon 	conn_t *connp = tcp->tcp_connp;
5324721fffe3SKacheong Poon 
5325721fffe3SKacheong Poon 	/* Learn the latest rwnd information that we sent to the other side. */
5326721fffe3SKacheong Poon 	thwin = ((uint_t)ntohs(tcp->tcp_tcpha->tha_win))
5327721fffe3SKacheong Poon 	    << tcp->tcp_rcv_ws;
5328721fffe3SKacheong Poon 	/* This is peer's calculated send window (our receive window). */
5329721fffe3SKacheong Poon 	thwin -= tcp->tcp_rnxt - tcp->tcp_rack;
5330721fffe3SKacheong Poon 	/*
5331721fffe3SKacheong Poon 	 * Increase the receive window to max.  But we need to do receiver
5332721fffe3SKacheong Poon 	 * SWS avoidance.  This means that we need to check the increase of
5333721fffe3SKacheong Poon 	 * of receive window is at least 1 MSS.
5334721fffe3SKacheong Poon 	 */
5335721fffe3SKacheong Poon 	if (connp->conn_rcvbuf - thwin >= tcp->tcp_mss) {
5336721fffe3SKacheong Poon 		/*
5337721fffe3SKacheong Poon 		 * If the window that the other side knows is less than max
5338721fffe3SKacheong Poon 		 * deferred acks segments, send an update immediately.
5339721fffe3SKacheong Poon 		 */
5340721fffe3SKacheong Poon 		if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) {
5341721fffe3SKacheong Poon 			TCPS_BUMP_MIB(tcp->tcp_tcps, tcpOutWinUpdate);
5342721fffe3SKacheong Poon 			ret = TH_ACK_NEEDED;
5343721fffe3SKacheong Poon 		}
5344721fffe3SKacheong Poon 		tcp->tcp_rwnd = connp->conn_rcvbuf;
5345721fffe3SKacheong Poon 	}
5346721fffe3SKacheong Poon 	return (ret);
5347721fffe3SKacheong Poon }
5348721fffe3SKacheong Poon 
5349721fffe3SKacheong Poon /*
5350721fffe3SKacheong Poon  * Handle a packet that has been reclassified by TCP.
5351721fffe3SKacheong Poon  * This function drops the ref on connp that the caller had.
5352721fffe3SKacheong Poon  */
5353721fffe3SKacheong Poon void
5354721fffe3SKacheong Poon tcp_reinput(conn_t *connp, mblk_t *mp, ip_recv_attr_t *ira, ip_stack_t *ipst)
5355721fffe3SKacheong Poon {
5356721fffe3SKacheong Poon 	ipsec_stack_t	*ipss = ipst->ips_netstack->netstack_ipsec;
5357721fffe3SKacheong Poon 
5358721fffe3SKacheong Poon 	if (connp->conn_incoming_ifindex != 0 &&
5359721fffe3SKacheong Poon 	    connp->conn_incoming_ifindex != ira->ira_ruifindex) {
5360721fffe3SKacheong Poon 		freemsg(mp);
5361721fffe3SKacheong Poon 		CONN_DEC_REF(connp);
5362721fffe3SKacheong Poon 		return;
5363721fffe3SKacheong Poon 	}
5364721fffe3SKacheong Poon 
5365721fffe3SKacheong Poon 	if (CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss) ||
5366721fffe3SKacheong Poon 	    (ira->ira_flags & IRAF_IPSEC_SECURE)) {
5367721fffe3SKacheong Poon 		ip6_t *ip6h;
5368721fffe3SKacheong Poon 		ipha_t *ipha;
5369721fffe3SKacheong Poon 
5370721fffe3SKacheong Poon 		if (ira->ira_flags & IRAF_IS_IPV4) {
5371721fffe3SKacheong Poon 			ipha = (ipha_t *)mp->b_rptr;
5372721fffe3SKacheong Poon 			ip6h = NULL;
5373721fffe3SKacheong Poon 		} else {
5374721fffe3SKacheong Poon 			ipha = NULL;
5375721fffe3SKacheong Poon 			ip6h = (ip6_t *)mp->b_rptr;
5376721fffe3SKacheong Poon 		}
5377721fffe3SKacheong Poon 		mp = ipsec_check_inbound_policy(mp, connp, ipha, ip6h, ira);
5378721fffe3SKacheong Poon 		if (mp == NULL) {
5379721fffe3SKacheong Poon 			BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
5380721fffe3SKacheong Poon 			/* Note that mp is NULL */
5381721fffe3SKacheong Poon 			ip_drop_input("ipIfStatsInDiscards", mp, NULL);
5382721fffe3SKacheong Poon 			CONN_DEC_REF(connp);
5383721fffe3SKacheong Poon 			return;
5384721fffe3SKacheong Poon 		}
5385721fffe3SKacheong Poon 	}
5386721fffe3SKacheong Poon 
5387721fffe3SKacheong Poon 	if (IPCL_IS_TCP(connp)) {
5388721fffe3SKacheong Poon 		/*
5389721fffe3SKacheong Poon 		 * do not drain, certain use cases can blow
5390721fffe3SKacheong Poon 		 * the stack
5391721fffe3SKacheong Poon 		 */
5392721fffe3SKacheong Poon 		SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
5393721fffe3SKacheong Poon 		    connp->conn_recv, connp, ira,
5394721fffe3SKacheong Poon 		    SQ_NODRAIN, SQTAG_IP_TCP_INPUT);
5395721fffe3SKacheong Poon 	} else {
5396721fffe3SKacheong Poon 		/* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
5397721fffe3SKacheong Poon 		(connp->conn_recv)(connp, mp, NULL,
5398721fffe3SKacheong Poon 		    ira);
5399721fffe3SKacheong Poon 		CONN_DEC_REF(connp);
5400721fffe3SKacheong Poon 	}
5401721fffe3SKacheong Poon 
5402721fffe3SKacheong Poon }
5403721fffe3SKacheong Poon 
5404721fffe3SKacheong Poon /* ARGSUSED */
5405721fffe3SKacheong Poon static void
5406721fffe3SKacheong Poon tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
5407721fffe3SKacheong Poon {
5408721fffe3SKacheong Poon 	conn_t	*connp = (conn_t *)arg;
5409721fffe3SKacheong Poon 	tcp_t	*tcp = connp->conn_tcp;
5410721fffe3SKacheong Poon 	queue_t	*q = connp->conn_rq;
5411721fffe3SKacheong Poon 
5412721fffe3SKacheong Poon 	ASSERT(!IPCL_IS_NONSTR(connp));
5413721fffe3SKacheong Poon 	mutex_enter(&tcp->tcp_rsrv_mp_lock);
5414721fffe3SKacheong Poon 	tcp->tcp_rsrv_mp = mp;
5415721fffe3SKacheong Poon 	mutex_exit(&tcp->tcp_rsrv_mp_lock);
5416721fffe3SKacheong Poon 
5417721fffe3SKacheong Poon 	if (TCP_IS_DETACHED(tcp) || q == NULL) {
5418721fffe3SKacheong Poon 		return;
5419721fffe3SKacheong Poon 	}
5420721fffe3SKacheong Poon 
5421721fffe3SKacheong Poon 	if (tcp->tcp_fused) {
5422721fffe3SKacheong Poon 		tcp_fuse_backenable(tcp);
5423721fffe3SKacheong Poon 		return;
5424721fffe3SKacheong Poon 	}
5425721fffe3SKacheong Poon 
5426721fffe3SKacheong Poon 	if (canputnext(q)) {
5427721fffe3SKacheong Poon 		/* Not flow-controlled, open rwnd */
5428721fffe3SKacheong Poon 		tcp->tcp_rwnd = connp->conn_rcvbuf;
5429721fffe3SKacheong Poon 
5430721fffe3SKacheong Poon 		/*
5431721fffe3SKacheong Poon 		 * Send back a window update immediately if TCP is above
5432721fffe3SKacheong Poon 		 * ESTABLISHED state and the increase of the rcv window
5433721fffe3SKacheong Poon 		 * that the other side knows is at least 1 MSS after flow
5434721fffe3SKacheong Poon 		 * control is lifted.
5435721fffe3SKacheong Poon 		 */
5436721fffe3SKacheong Poon 		if (tcp->tcp_state >= TCPS_ESTABLISHED &&
5437721fffe3SKacheong Poon 		    tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) {
5438721fffe3SKacheong Poon 			tcp_xmit_ctl(NULL, tcp,
5439721fffe3SKacheong Poon 			    (tcp->tcp_swnd == 0) ? tcp->tcp_suna :
5440721fffe3SKacheong Poon 			    tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK);
5441721fffe3SKacheong Poon 		}
5442721fffe3SKacheong Poon 	}
5443721fffe3SKacheong Poon }
5444721fffe3SKacheong Poon 
5445721fffe3SKacheong Poon /*
5446721fffe3SKacheong Poon  * The read side service routine is called mostly when we get back-enabled as a
5447721fffe3SKacheong Poon  * result of flow control relief.  Since we don't actually queue anything in
5448721fffe3SKacheong Poon  * TCP, we have no data to send out of here.  What we do is clear the receive
5449721fffe3SKacheong Poon  * window, and send out a window update.
5450721fffe3SKacheong Poon  */
5451721fffe3SKacheong Poon void
5452721fffe3SKacheong Poon tcp_rsrv(queue_t *q)
5453721fffe3SKacheong Poon {
5454721fffe3SKacheong Poon 	conn_t		*connp = Q_TO_CONN(q);
5455721fffe3SKacheong Poon 	tcp_t		*tcp = connp->conn_tcp;
5456721fffe3SKacheong Poon 	mblk_t		*mp;
5457721fffe3SKacheong Poon 
5458721fffe3SKacheong Poon 	/* No code does a putq on the read side */
5459721fffe3SKacheong Poon 	ASSERT(q->q_first == NULL);
5460721fffe3SKacheong Poon 
5461721fffe3SKacheong Poon 	/*
5462721fffe3SKacheong Poon 	 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already
5463721fffe3SKacheong Poon 	 * been run.  So just return.
5464721fffe3SKacheong Poon 	 */
5465721fffe3SKacheong Poon 	mutex_enter(&tcp->tcp_rsrv_mp_lock);
5466721fffe3SKacheong Poon 	if ((mp = tcp->tcp_rsrv_mp) == NULL) {
5467721fffe3SKacheong Poon 		mutex_exit(&tcp->tcp_rsrv_mp_lock);
5468721fffe3SKacheong Poon 		return;
5469721fffe3SKacheong Poon 	}
5470721fffe3SKacheong Poon 	tcp->tcp_rsrv_mp = NULL;
5471721fffe3SKacheong Poon 	mutex_exit(&tcp->tcp_rsrv_mp_lock);
5472721fffe3SKacheong Poon 
5473721fffe3SKacheong Poon 	CONN_INC_REF(connp);
5474721fffe3SKacheong Poon 	SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp,
5475721fffe3SKacheong Poon 	    NULL, SQ_PROCESS, SQTAG_TCP_RSRV);
5476721fffe3SKacheong Poon }
5477721fffe3SKacheong Poon 
5478721fffe3SKacheong Poon /* At minimum we need 8 bytes in the TCP header for the lookup */
5479721fffe3SKacheong Poon #define	ICMP_MIN_TCP_HDR	8
5480721fffe3SKacheong Poon 
5481721fffe3SKacheong Poon /*
5482721fffe3SKacheong Poon  * tcp_icmp_input is called as conn_recvicmp to process ICMP error messages
5483721fffe3SKacheong Poon  * passed up by IP. The message is always received on the correct tcp_t.
5484721fffe3SKacheong Poon  * Assumes that IP has pulled up everything up to and including the ICMP header.
5485721fffe3SKacheong Poon  */
5486721fffe3SKacheong Poon /* ARGSUSED2 */
5487721fffe3SKacheong Poon void
5488721fffe3SKacheong Poon tcp_icmp_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
5489721fffe3SKacheong Poon {
5490721fffe3SKacheong Poon 	conn_t		*connp = (conn_t *)arg1;
5491721fffe3SKacheong Poon 	icmph_t		*icmph;
5492721fffe3SKacheong Poon 	ipha_t		*ipha;
5493721fffe3SKacheong Poon 	int		iph_hdr_length;
5494721fffe3SKacheong Poon 	tcpha_t		*tcpha;
5495721fffe3SKacheong Poon 	uint32_t	seg_seq;
5496721fffe3SKacheong Poon 	tcp_t		*tcp = connp->conn_tcp;
5497721fffe3SKacheong Poon 
5498721fffe3SKacheong Poon 	/* Assume IP provides aligned packets */
5499721fffe3SKacheong Poon 	ASSERT(OK_32PTR(mp->b_rptr));
5500721fffe3SKacheong Poon 	ASSERT((MBLKL(mp) >= sizeof (ipha_t)));
5501721fffe3SKacheong Poon 
5502721fffe3SKacheong Poon 	/*
55038b21eaf3SRobert Mustacchi 	 * It's possible we have a closed, but not yet destroyed, TCP
55048b21eaf3SRobert Mustacchi 	 * connection. Several fields (e.g. conn_ixa->ixa_ire) are invalid
55058b21eaf3SRobert Mustacchi 	 * in the closed state, so don't take any chances and drop the packet.
55068b21eaf3SRobert Mustacchi 	 */
55078b21eaf3SRobert Mustacchi 	if (tcp->tcp_state == TCPS_CLOSED) {
55088b21eaf3SRobert Mustacchi 		freemsg(mp);
55098b21eaf3SRobert Mustacchi 		return;
55108b21eaf3SRobert Mustacchi 	}
55118b21eaf3SRobert Mustacchi 
55128b21eaf3SRobert Mustacchi 	/*
5513721fffe3SKacheong Poon 	 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent
5514721fffe3SKacheong Poon 	 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6.
5515721fffe3SKacheong Poon 	 */
5516721fffe3SKacheong Poon 	if (!(ira->ira_flags & IRAF_IS_IPV4)) {
5517721fffe3SKacheong Poon 		tcp_icmp_error_ipv6(tcp, mp, ira);
5518721fffe3SKacheong Poon 		return;
5519721fffe3SKacheong Poon 	}
5520721fffe3SKacheong Poon 
5521721fffe3SKacheong Poon 	/* Skip past the outer IP and ICMP headers */
5522721fffe3SKacheong Poon 	iph_hdr_length = ira->ira_ip_hdr_length;
5523721fffe3SKacheong Poon 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
5524721fffe3SKacheong Poon 	/*
5525721fffe3SKacheong Poon 	 * If we don't have the correct outer IP header length
5526721fffe3SKacheong Poon 	 * or if we don't have a complete inner IP header
5527721fffe3SKacheong Poon 	 * drop it.
5528721fffe3SKacheong Poon 	 */
5529721fffe3SKacheong Poon 	if (iph_hdr_length < sizeof (ipha_t) ||
5530721fffe3SKacheong Poon 	    (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) {
5531721fffe3SKacheong Poon noticmpv4:
5532721fffe3SKacheong Poon 		freemsg(mp);
5533721fffe3SKacheong Poon 		return;
5534721fffe3SKacheong Poon 	}
5535721fffe3SKacheong Poon 	ipha = (ipha_t *)&icmph[1];
5536721fffe3SKacheong Poon 
5537721fffe3SKacheong Poon 	/* Skip past the inner IP and find the ULP header */
5538721fffe3SKacheong Poon 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
5539721fffe3SKacheong Poon 	tcpha = (tcpha_t *)((char *)ipha + iph_hdr_length);
5540721fffe3SKacheong Poon 	/*
5541721fffe3SKacheong Poon 	 * If we don't have the correct inner IP header length or if the ULP
5542721fffe3SKacheong Poon 	 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR
5543721fffe3SKacheong Poon 	 * bytes of TCP header, drop it.
5544721fffe3SKacheong Poon 	 */
5545721fffe3SKacheong Poon 	if (iph_hdr_length < sizeof (ipha_t) ||
5546721fffe3SKacheong Poon 	    ipha->ipha_protocol != IPPROTO_TCP ||
5547721fffe3SKacheong Poon 	    (uchar_t *)tcpha + ICMP_MIN_TCP_HDR > mp->b_wptr) {
5548721fffe3SKacheong Poon 		goto noticmpv4;
5549721fffe3SKacheong Poon 	}
5550721fffe3SKacheong Poon 
5551721fffe3SKacheong Poon 	seg_seq = ntohl(tcpha->tha_seq);
5552721fffe3SKacheong Poon 	switch (icmph->icmph_type) {
5553721fffe3SKacheong Poon 	case ICMP_DEST_UNREACHABLE:
5554721fffe3SKacheong Poon 		switch (icmph->icmph_code) {
5555721fffe3SKacheong Poon 		case ICMP_FRAGMENTATION_NEEDED:
5556721fffe3SKacheong Poon 			/*
5557721fffe3SKacheong Poon 			 * Update Path MTU, then try to send something out.
5558721fffe3SKacheong Poon 			 */
5559721fffe3SKacheong Poon 			tcp_update_pmtu(tcp, B_TRUE);
5560721fffe3SKacheong Poon 			tcp_rexmit_after_error(tcp);
5561721fffe3SKacheong Poon 			break;
5562721fffe3SKacheong Poon 		case ICMP_PORT_UNREACHABLE:
5563721fffe3SKacheong Poon 		case ICMP_PROTOCOL_UNREACHABLE:
5564721fffe3SKacheong Poon 			switch (tcp->tcp_state) {
5565721fffe3SKacheong Poon 			case TCPS_SYN_SENT:
5566721fffe3SKacheong Poon 			case TCPS_SYN_RCVD:
5567721fffe3SKacheong Poon 				/*
5568721fffe3SKacheong Poon 				 * ICMP can snipe away incipient
5569721fffe3SKacheong Poon 				 * TCP connections as long as
5570721fffe3SKacheong Poon 				 * seq number is same as initial
5571721fffe3SKacheong Poon 				 * send seq number.
5572721fffe3SKacheong Poon 				 */
5573721fffe3SKacheong Poon 				if (seg_seq == tcp->tcp_iss) {
5574721fffe3SKacheong Poon 					(void) tcp_clean_death(tcp,
5575721fffe3SKacheong Poon 					    ECONNREFUSED);
5576721fffe3SKacheong Poon 				}
5577721fffe3SKacheong Poon 				break;
5578721fffe3SKacheong Poon 			}
5579721fffe3SKacheong Poon 			break;
5580721fffe3SKacheong Poon 		case ICMP_HOST_UNREACHABLE:
5581721fffe3SKacheong Poon 		case ICMP_NET_UNREACHABLE:
5582721fffe3SKacheong Poon 			/* Record the error in case we finally time out. */
5583721fffe3SKacheong Poon 			if (icmph->icmph_code == ICMP_HOST_UNREACHABLE)
5584721fffe3SKacheong Poon 				tcp->tcp_client_errno = EHOSTUNREACH;
5585721fffe3SKacheong Poon 			else
5586721fffe3SKacheong Poon 				tcp->tcp_client_errno = ENETUNREACH;
5587721fffe3SKacheong Poon 			if (tcp->tcp_state == TCPS_SYN_RCVD) {
5588721fffe3SKacheong Poon 				if (tcp->tcp_listener != NULL &&
5589721fffe3SKacheong Poon 				    tcp->tcp_listener->tcp_syn_defense) {
5590721fffe3SKacheong Poon 					/*
5591721fffe3SKacheong Poon 					 * Ditch the half-open connection if we
5592721fffe3SKacheong Poon 					 * suspect a SYN attack is under way.
5593721fffe3SKacheong Poon 					 */
5594721fffe3SKacheong Poon 					(void) tcp_clean_death(tcp,
5595721fffe3SKacheong Poon 					    tcp->tcp_client_errno);
5596721fffe3SKacheong Poon 				}
5597721fffe3SKacheong Poon 			}
5598721fffe3SKacheong Poon 			break;
5599721fffe3SKacheong Poon 		default:
5600721fffe3SKacheong Poon 			break;
5601721fffe3SKacheong Poon 		}
5602721fffe3SKacheong Poon 		break;
5603721fffe3SKacheong Poon 	case ICMP_SOURCE_QUENCH: {
5604721fffe3SKacheong Poon 		/*
5605721fffe3SKacheong Poon 		 * use a global boolean to control
5606721fffe3SKacheong Poon 		 * whether TCP should respond to ICMP_SOURCE_QUENCH.
5607721fffe3SKacheong Poon 		 * The default is false.
5608721fffe3SKacheong Poon 		 */
5609721fffe3SKacheong Poon 		if (tcp_icmp_source_quench) {
5610721fffe3SKacheong Poon 			/*
5611721fffe3SKacheong Poon 			 * Reduce the sending rate as if we got a
5612721fffe3SKacheong Poon 			 * retransmit timeout
5613721fffe3SKacheong Poon 			 */
5614721fffe3SKacheong Poon 			uint32_t npkt;
5615721fffe3SKacheong Poon 
5616721fffe3SKacheong Poon 			npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) /
5617721fffe3SKacheong Poon 			    tcp->tcp_mss;
5618721fffe3SKacheong Poon 			tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss;
5619721fffe3SKacheong Poon 			tcp->tcp_cwnd = tcp->tcp_mss;
5620721fffe3SKacheong Poon 			tcp->tcp_cwnd_cnt = 0;
5621721fffe3SKacheong Poon 		}
5622721fffe3SKacheong Poon 		break;
5623721fffe3SKacheong Poon 	}
5624721fffe3SKacheong Poon 	}
5625721fffe3SKacheong Poon 	freemsg(mp);
5626721fffe3SKacheong Poon }
5627721fffe3SKacheong Poon 
5628721fffe3SKacheong Poon /*
5629721fffe3SKacheong Poon  * tcp_icmp_error_ipv6 is called from tcp_icmp_input to process ICMPv6
5630721fffe3SKacheong Poon  * error messages passed up by IP.
5631721fffe3SKacheong Poon  * Assumes that IP has pulled up all the extension headers as well
5632721fffe3SKacheong Poon  * as the ICMPv6 header.
5633721fffe3SKacheong Poon  */
5634721fffe3SKacheong Poon static void
5635721fffe3SKacheong Poon tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, ip_recv_attr_t *ira)
5636721fffe3SKacheong Poon {
5637721fffe3SKacheong Poon 	icmp6_t		*icmp6;
5638721fffe3SKacheong Poon 	ip6_t		*ip6h;
5639721fffe3SKacheong Poon 	uint16_t	iph_hdr_length = ira->ira_ip_hdr_length;
5640721fffe3SKacheong Poon 	tcpha_t		*tcpha;
5641721fffe3SKacheong Poon 	uint8_t		*nexthdrp;
5642721fffe3SKacheong Poon 	uint32_t	seg_seq;
5643721fffe3SKacheong Poon 
5644721fffe3SKacheong Poon 	/*
5645721fffe3SKacheong Poon 	 * Verify that we have a complete IP header.
5646721fffe3SKacheong Poon 	 */
5647721fffe3SKacheong Poon 	ASSERT((MBLKL(mp) >= sizeof (ip6_t)));
5648721fffe3SKacheong Poon 
5649721fffe3SKacheong Poon 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
5650721fffe3SKacheong Poon 	ip6h = (ip6_t *)&icmp6[1];
5651721fffe3SKacheong Poon 	/*
5652721fffe3SKacheong Poon 	 * Verify if we have a complete ICMP and inner IP header.
5653721fffe3SKacheong Poon 	 */
5654721fffe3SKacheong Poon 	if ((uchar_t *)&ip6h[1] > mp->b_wptr) {
5655721fffe3SKacheong Poon noticmpv6:
5656721fffe3SKacheong Poon 		freemsg(mp);
5657721fffe3SKacheong Poon 		return;
5658721fffe3SKacheong Poon 	}
5659721fffe3SKacheong Poon 
5660721fffe3SKacheong Poon 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp))
5661721fffe3SKacheong Poon 		goto noticmpv6;
5662721fffe3SKacheong Poon 	tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length);
5663721fffe3SKacheong Poon 	/*
5664721fffe3SKacheong Poon 	 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't
5665721fffe3SKacheong Poon 	 * have at least ICMP_MIN_TCP_HDR bytes of  TCP header drop the
5666721fffe3SKacheong Poon 	 * packet.
5667721fffe3SKacheong Poon 	 */
5668721fffe3SKacheong Poon 	if ((*nexthdrp != IPPROTO_TCP) ||
5669721fffe3SKacheong Poon 	    ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) {
5670721fffe3SKacheong Poon 		goto noticmpv6;
5671721fffe3SKacheong Poon 	}
5672721fffe3SKacheong Poon 
5673721fffe3SKacheong Poon 	seg_seq = ntohl(tcpha->tha_seq);
5674721fffe3SKacheong Poon 	switch (icmp6->icmp6_type) {
5675721fffe3SKacheong Poon 	case ICMP6_PACKET_TOO_BIG:
5676721fffe3SKacheong Poon 		/*
5677721fffe3SKacheong Poon 		 * Update Path MTU, then try to send something out.
5678721fffe3SKacheong Poon 		 */
5679721fffe3SKacheong Poon 		tcp_update_pmtu(tcp, B_TRUE);
5680721fffe3SKacheong Poon 		tcp_rexmit_after_error(tcp);
5681721fffe3SKacheong Poon 		break;
5682721fffe3SKacheong Poon 	case ICMP6_DST_UNREACH:
5683721fffe3SKacheong Poon 		switch (icmp6->icmp6_code) {
5684721fffe3SKacheong Poon 		case ICMP6_DST_UNREACH_NOPORT:
5685721fffe3SKacheong Poon 			if (((tcp->tcp_state == TCPS_SYN_SENT) ||
5686721fffe3SKacheong Poon 			    (tcp->tcp_state == TCPS_SYN_RCVD)) &&
5687721fffe3SKacheong Poon 			    (seg_seq == tcp->tcp_iss)) {
5688721fffe3SKacheong Poon 				(void) tcp_clean_death(tcp, ECONNREFUSED);
5689721fffe3SKacheong Poon 			}
5690721fffe3SKacheong Poon 			break;
5691721fffe3SKacheong Poon 		case ICMP6_DST_UNREACH_ADMIN:
5692721fffe3SKacheong Poon 		case ICMP6_DST_UNREACH_NOROUTE:
5693721fffe3SKacheong Poon 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
5694721fffe3SKacheong Poon 		case ICMP6_DST_UNREACH_ADDR:
5695721fffe3SKacheong Poon 			/* Record the error in case we finally time out. */
5696721fffe3SKacheong Poon 			tcp->tcp_client_errno = EHOSTUNREACH;
5697721fffe3SKacheong Poon 			if (((tcp->tcp_state == TCPS_SYN_SENT) ||
5698721fffe3SKacheong Poon 			    (tcp->tcp_state == TCPS_SYN_RCVD)) &&
5699721fffe3SKacheong Poon 			    (seg_seq == tcp->tcp_iss)) {
5700721fffe3SKacheong Poon 				if (tcp->tcp_listener != NULL &&
5701721fffe3SKacheong Poon 				    tcp->tcp_listener->tcp_syn_defense) {
5702721fffe3SKacheong Poon 					/*
5703721fffe3SKacheong Poon 					 * Ditch the half-open connection if we
5704721fffe3SKacheong Poon 					 * suspect a SYN attack is under way.
5705721fffe3SKacheong Poon 					 */
5706721fffe3SKacheong Poon 					(void) tcp_clean_death(tcp,
5707721fffe3SKacheong Poon 					    tcp->tcp_client_errno);
5708721fffe3SKacheong Poon 				}
5709721fffe3SKacheong Poon 			}
5710721fffe3SKacheong Poon 
5711721fffe3SKacheong Poon 
5712721fffe3SKacheong Poon 			break;
5713721fffe3SKacheong Poon 		default:
5714721fffe3SKacheong Poon 			break;
5715721fffe3SKacheong Poon 		}
5716721fffe3SKacheong Poon 		break;
5717721fffe3SKacheong Poon 	case ICMP6_PARAM_PROB:
5718721fffe3SKacheong Poon 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
5719721fffe3SKacheong Poon 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
5720721fffe3SKacheong Poon 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
5721721fffe3SKacheong Poon 		    (uchar_t *)nexthdrp) {
5722721fffe3SKacheong Poon 			if (tcp->tcp_state == TCPS_SYN_SENT ||
5723721fffe3SKacheong Poon 			    tcp->tcp_state == TCPS_SYN_RCVD) {
5724721fffe3SKacheong Poon 				(void) tcp_clean_death(tcp, ECONNREFUSED);
5725721fffe3SKacheong Poon 			}
5726721fffe3SKacheong Poon 			break;
5727721fffe3SKacheong Poon 		}
5728721fffe3SKacheong Poon 		break;
5729721fffe3SKacheong Poon 
5730721fffe3SKacheong Poon 	case ICMP6_TIME_EXCEEDED:
5731721fffe3SKacheong Poon 	default:
5732721fffe3SKacheong Poon 		break;
5733721fffe3SKacheong Poon 	}
5734721fffe3SKacheong Poon 	freemsg(mp);
5735721fffe3SKacheong Poon }
5736721fffe3SKacheong Poon 
5737721fffe3SKacheong Poon /*
5738721fffe3SKacheong Poon  * CALLED OUTSIDE OF SQUEUE! It can not follow any pointers that tcp might
5739721fffe3SKacheong Poon  * change. But it can refer to fields like tcp_suna and tcp_snxt.
5740721fffe3SKacheong Poon  *
5741721fffe3SKacheong Poon  * Function tcp_verifyicmp is called as conn_verifyicmp to verify the ICMP
5742721fffe3SKacheong Poon  * error messages received by IP. The message is always received on the correct
5743721fffe3SKacheong Poon  * tcp_t.
5744721fffe3SKacheong Poon  */
5745721fffe3SKacheong Poon /* ARGSUSED */
5746721fffe3SKacheong Poon boolean_t
5747721fffe3SKacheong Poon tcp_verifyicmp(conn_t *connp, void *arg2, icmph_t *icmph, icmp6_t *icmp6,
5748721fffe3SKacheong Poon     ip_recv_attr_t *ira)
5749721fffe3SKacheong Poon {
5750721fffe3SKacheong Poon 	tcpha_t		*tcpha = (tcpha_t *)arg2;
5751721fffe3SKacheong Poon 	uint32_t	seq = ntohl(tcpha->tha_seq);
5752721fffe3SKacheong Poon 	tcp_t		*tcp = connp->conn_tcp;
5753721fffe3SKacheong Poon 
5754721fffe3SKacheong Poon 	/*
5755721fffe3SKacheong Poon 	 * TCP sequence number contained in payload of the ICMP error message
5756721fffe3SKacheong Poon 	 * should be within the range SND.UNA <= SEG.SEQ < SND.NXT. Otherwise,
5757721fffe3SKacheong Poon 	 * the message is either a stale ICMP error, or an attack from the
5758721fffe3SKacheong Poon 	 * network. Fail the verification.
5759721fffe3SKacheong Poon 	 */
5760721fffe3SKacheong Poon 	if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt))
5761721fffe3SKacheong Poon 		return (B_FALSE);
5762721fffe3SKacheong Poon 
5763721fffe3SKacheong Poon 	/* For "too big" we also check the ignore flag */
5764721fffe3SKacheong Poon 	if (ira->ira_flags & IRAF_IS_IPV4) {
5765721fffe3SKacheong Poon 		ASSERT(icmph != NULL);
5766721fffe3SKacheong Poon 		if (icmph->icmph_type == ICMP_DEST_UNREACHABLE &&
5767721fffe3SKacheong Poon 		    icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED &&
5768721fffe3SKacheong Poon 		    tcp->tcp_tcps->tcps_ignore_path_mtu)
5769721fffe3SKacheong Poon 			return (B_FALSE);
5770721fffe3SKacheong Poon 	} else {
5771721fffe3SKacheong Poon 		ASSERT(icmp6 != NULL);
5772721fffe3SKacheong Poon 		if (icmp6->icmp6_type == ICMP6_PACKET_TOO_BIG &&
5773721fffe3SKacheong Poon 		    tcp->tcp_tcps->tcps_ignore_path_mtu)
5774721fffe3SKacheong Poon 			return (B_FALSE);
5775721fffe3SKacheong Poon 	}
5776721fffe3SKacheong Poon 	return (B_TRUE);
5777721fffe3SKacheong Poon }
5778