xref: /illumos-gate/usr/src/uts/common/inet/tcp/tcp_input.c (revision b69c34dad3717624ff6b4f32b71014ee05b6a678)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25  * Copyright 2019 Joyent, Inc.
26  * Copyright (c) 2014, 2016 by Delphix. All rights reserved.
27  */
28 
29 /* This file contains all TCP input processing functions. */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/strsun.h>
34 #include <sys/strsubr.h>
35 #include <sys/stropts.h>
36 #include <sys/strlog.h>
37 #define	_SUN_TPI_VERSION 2
38 #include <sys/tihdr.h>
39 #include <sys/suntpi.h>
40 #include <sys/xti_inet.h>
41 #include <sys/squeue_impl.h>
42 #include <sys/squeue.h>
43 #include <sys/tsol/tnet.h>
44 
45 #include <inet/common.h>
46 #include <inet/ip.h>
47 #include <inet/tcp.h>
48 #include <inet/tcp_impl.h>
49 #include <inet/tcp_cluster.h>
50 #include <inet/proto_set.h>
51 #include <inet/ipsec_impl.h>
52 
53 /*
54  * RFC7323-recommended phrasing of TSTAMP option, for easier parsing
55  */
56 
57 #ifdef _BIG_ENDIAN
58 #define	TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
59 	(TCPOPT_TSTAMP << 8) | 10)
60 #else
61 #define	TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \
62 	(TCPOPT_NOP << 8) | TCPOPT_NOP)
63 #endif
64 
65 /*
66  *  PAWS needs a timer for 24 days.  This is the number of ticks in 24 days
67  */
68 #define	PAWS_TIMEOUT	((clock_t)(24*24*60*60*hz))
69 
70 /*
71  * Since tcp_listener is not cleared atomically with tcp_detached
72  * being cleared we need this extra bit to tell a detached connection
73  * apart from one that is in the process of being accepted.
74  */
75 #define	TCP_IS_DETACHED_NONEAGER(tcp)	\
76 	(TCP_IS_DETACHED(tcp) &&	\
77 	    (!(tcp)->tcp_hard_binding))
78 
79 /*
80  * Steps to do when a tcp_t moves to TIME-WAIT state.
81  *
82  * This connection is done, we don't need to account for it.  Decrement
83  * the listener connection counter if needed.
84  *
85  * Decrement the connection counter of the stack.  Note that this counter
86  * is per CPU.  So the total number of connections in a stack is the sum of all
87  * of them.  Since there is no lock for handling all of them exclusively, the
88  * resulting sum is only an approximation.
89  *
90  * Unconditionally clear the exclusive binding bit so this TIME-WAIT
91  * connection won't interfere with new ones.
92  *
93  * Start the TIME-WAIT timer.  If upper layer has not closed the connection,
94  * the timer is handled within the context of this tcp_t.  When the timer
95  * fires, tcp_clean_death() is called.  If upper layer closes the connection
96  * during this period, tcp_time_wait_append() will be called to add this
97  * tcp_t to the global TIME-WAIT list.  Note that this means that the
98  * actual wait time in TIME-WAIT state will be longer than the
99  * tcps_time_wait_interval since the period before upper layer closes the
100  * connection is not accounted for when tcp_time_wait_append() is called.
101  *
102  * If upper layer has closed the connection, call tcp_time_wait_append()
103  * directly.
104  *
105  */
106 #define	SET_TIME_WAIT(tcps, tcp, connp)				\
107 {								\
108 	(tcp)->tcp_state = TCPS_TIME_WAIT;			\
109 	if ((tcp)->tcp_listen_cnt != NULL)			\
110 		TCP_DECR_LISTEN_CNT(tcp);			\
111 	atomic_dec_64(						\
112 	    (uint64_t *)&(tcps)->tcps_sc[CPU->cpu_seqid]->tcp_sc_conn_cnt); \
113 	(connp)->conn_exclbind = 0;				\
114 	if (!TCP_IS_DETACHED(tcp)) {				\
115 		TCP_TIMER_RESTART(tcp, (tcps)->tcps_time_wait_interval); \
116 	} else {						\
117 		tcp_time_wait_append(tcp);			\
118 		TCP_DBGSTAT(tcps, tcp_rput_time_wait);		\
119 	}							\
120 }
121 
122 /*
123  * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more
124  * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent
125  * data, TCP will not respond with an ACK.  RFC 793 requires that
126  * TCP responds with an ACK for such a bogus ACK.  By not following
127  * the RFC, we prevent TCP from getting into an ACK storm if somehow
128  * an attacker successfully spoofs an acceptable segment to our
129  * peer; or when our peer is "confused."
130  */
131 static uint32_t tcp_drop_ack_unsent_cnt = 10;
132 
133 /*
134  * To protect TCP against attacker using a small window and requesting
135  * large amount of data (DoS attack by conuming memory), TCP checks the
136  * window advertised in the last ACK of the 3-way handshake.  TCP uses
137  * the tcp_mss (the size of one packet) value for comparion.  The window
138  * should be larger than tcp_mss.  But while a sane TCP should advertise
139  * a receive window larger than or equal to 4*MSS to avoid stop and go
140  * tarrfic, not all TCP stacks do that.  This is especially true when
141  * tcp_mss is a big value.
142  *
143  * To work around this issue, an additional fixed value for comparison
144  * is also used.  If the advertised window is smaller than both tcp_mss
145  * and tcp_init_wnd_chk, the ACK is considered as invalid.  So for large
146  * tcp_mss value (say, 8K), a window larger than tcp_init_wnd_chk but
147  * smaller than 8K is considered to be OK.
148  */
149 static uint32_t tcp_init_wnd_chk = 4096;
150 
151 /* Process ICMP source quench message or not. */
152 static boolean_t tcp_icmp_source_quench = B_FALSE;
153 
154 static boolean_t tcp_outbound_squeue_switch = B_FALSE;
155 
156 static mblk_t	*tcp_conn_create_v4(conn_t *, conn_t *, mblk_t *,
157 		    ip_recv_attr_t *);
158 static mblk_t	*tcp_conn_create_v6(conn_t *, conn_t *, mblk_t *,
159 		    ip_recv_attr_t *);
160 static boolean_t	tcp_drop_q0(tcp_t *);
161 static void	tcp_icmp_error_ipv6(tcp_t *, mblk_t *, ip_recv_attr_t *);
162 static mblk_t	*tcp_input_add_ancillary(tcp_t *, mblk_t *, ip_pkt_t *,
163 		    ip_recv_attr_t *);
164 static void	tcp_input_listener(void *, mblk_t *, void *, ip_recv_attr_t *);
165 static void	tcp_process_options(tcp_t *, tcpha_t *);
166 static mblk_t	*tcp_reass(tcp_t *, mblk_t *, uint32_t);
167 static void	tcp_reass_elim_overlap(tcp_t *, mblk_t *);
168 static void	tcp_rsrv_input(void *, mblk_t *, void *, ip_recv_attr_t *);
169 static void	tcp_set_rto(tcp_t *, hrtime_t);
170 static void	tcp_setcred_data(mblk_t *, ip_recv_attr_t *);
171 
172 /*
173  * CC wrapper hook functions
174  */
175 static void
176 cc_ack_received(tcp_t *tcp, uint32_t seg_ack, int32_t bytes_acked,
177     uint16_t type)
178 {
179 	uint32_t old_cwnd = tcp->tcp_cwnd;
180 
181 	tcp->tcp_ccv.bytes_this_ack = bytes_acked;
182 	if (tcp->tcp_cwnd <= tcp->tcp_swnd)
183 		tcp->tcp_ccv.flags |= CCF_CWND_LIMITED;
184 	else
185 		tcp->tcp_ccv.flags &= ~CCF_CWND_LIMITED;
186 
187 	if (type == CC_ACK) {
188 		if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) {
189 			if (tcp->tcp_ccv.flags & CCF_RTO)
190 				tcp->tcp_ccv.flags &= ~CCF_RTO;
191 
192 			tcp->tcp_ccv.t_bytes_acked +=
193 			    min(tcp->tcp_ccv.bytes_this_ack,
194 			    tcp->tcp_tcps->tcps_abc_l_var * tcp->tcp_mss);
195 			if (tcp->tcp_ccv.t_bytes_acked >= tcp->tcp_cwnd) {
196 				tcp->tcp_ccv.t_bytes_acked -= tcp->tcp_cwnd;
197 				tcp->tcp_ccv.flags |= CCF_ABC_SENTAWND;
198 			}
199 		} else {
200 			tcp->tcp_ccv.flags &= ~CCF_ABC_SENTAWND;
201 			tcp->tcp_ccv.t_bytes_acked = 0;
202 		}
203 	}
204 
205 	if (CC_ALGO(tcp)->ack_received != NULL) {
206 		/*
207 		 * The FreeBSD code where this originated had a comment "Find
208 		 * a way to live without this" in several places where curack
209 		 * got set.  If they eventually dump curack from the cc
210 		 * variables, we'll need to adapt our code.
211 		 */
212 		tcp->tcp_ccv.curack = seg_ack;
213 		CC_ALGO(tcp)->ack_received(&tcp->tcp_ccv, type);
214 	}
215 
216 	DTRACE_PROBE3(cwnd__cc__ack__received, tcp_t *, tcp, uint32_t, old_cwnd,
217 	    uint32_t, tcp->tcp_cwnd);
218 }
219 
220 void
221 cc_cong_signal(tcp_t *tcp, uint32_t seg_ack, uint32_t type)
222 {
223 	uint32_t old_cwnd = tcp->tcp_cwnd;
224 	uint32_t old_cwnd_ssthresh = tcp->tcp_cwnd_ssthresh;
225 	switch (type) {
226 	case CC_NDUPACK:
227 		if (!IN_FASTRECOVERY(tcp->tcp_ccv.flags)) {
228 			tcp->tcp_rexmit_max = tcp->tcp_snxt;
229 			if (tcp->tcp_ecn_ok) {
230 				tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
231 				tcp->tcp_cwr = B_TRUE;
232 				tcp->tcp_ecn_cwr_sent = B_FALSE;
233 			}
234 		}
235 		break;
236 	case CC_ECN:
237 		if (!IN_CONGRECOVERY(tcp->tcp_ccv.flags)) {
238 			tcp->tcp_rexmit_max = tcp->tcp_snxt;
239 			if (tcp->tcp_ecn_ok) {
240 				tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
241 				tcp->tcp_cwr = B_TRUE;
242 				tcp->tcp_ecn_cwr_sent = B_FALSE;
243 			}
244 		}
245 		break;
246 	case CC_RTO:
247 		tcp->tcp_ccv.flags |= CCF_RTO;
248 		tcp->tcp_dupack_cnt = 0;
249 		tcp->tcp_ccv.t_bytes_acked = 0;
250 		/*
251 		 * Give up on fast recovery and congestion recovery if we were
252 		 * attempting either.
253 		 */
254 		EXIT_RECOVERY(tcp->tcp_ccv.flags);
255 		if (CC_ALGO(tcp)->cong_signal == NULL) {
256 			/*
257 			 * RFC5681 Section 3.1
258 			 * ssthresh = max (FlightSize / 2, 2*SMSS) eq (4)
259 			 */
260 			tcp->tcp_cwnd_ssthresh = max(
261 			    (tcp->tcp_snxt - tcp->tcp_suna) / 2 / tcp->tcp_mss,
262 			    2) * tcp->tcp_mss;
263 			tcp->tcp_cwnd = tcp->tcp_mss;
264 		}
265 
266 		if (tcp->tcp_ecn_ok) {
267 			tcp->tcp_cwr = B_TRUE;
268 			tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
269 			tcp->tcp_ecn_cwr_sent = B_FALSE;
270 		}
271 		break;
272 	}
273 
274 	if (CC_ALGO(tcp)->cong_signal != NULL) {
275 		tcp->tcp_ccv.curack = seg_ack;
276 		CC_ALGO(tcp)->cong_signal(&tcp->tcp_ccv, type);
277 	}
278 
279 	DTRACE_PROBE6(cwnd__cc__cong__signal, tcp_t *, tcp, uint32_t, old_cwnd,
280 	    uint32_t, tcp->tcp_cwnd, uint32_t, old_cwnd_ssthresh,
281 	    uint32_t, tcp->tcp_cwnd_ssthresh, uint32_t, type);
282 }
283 
284 static void
285 cc_post_recovery(tcp_t *tcp, uint32_t seg_ack)
286 {
287 	uint32_t old_cwnd = tcp->tcp_cwnd;
288 
289 	if (CC_ALGO(tcp)->post_recovery != NULL) {
290 		tcp->tcp_ccv.curack = seg_ack;
291 		CC_ALGO(tcp)->post_recovery(&tcp->tcp_ccv);
292 	}
293 	tcp->tcp_ccv.t_bytes_acked = 0;
294 
295 	DTRACE_PROBE3(cwnd__cc__post__recovery, tcp_t *, tcp,
296 	    uint32_t, old_cwnd, uint32_t, tcp->tcp_cwnd);
297 }
298 
299 /*
300  * Set the MSS associated with a particular tcp based on its current value,
301  * and a new one passed in. Observe minimums and maximums, and reset other
302  * state variables that we want to view as multiples of MSS.
303  *
304  * The value of MSS could be either increased or descreased.
305  */
306 void
307 tcp_mss_set(tcp_t *tcp, uint32_t mss)
308 {
309 	uint32_t	mss_max;
310 	tcp_stack_t	*tcps = tcp->tcp_tcps;
311 	conn_t		*connp = tcp->tcp_connp;
312 
313 	if (connp->conn_ipversion == IPV4_VERSION)
314 		mss_max = tcps->tcps_mss_max_ipv4;
315 	else
316 		mss_max = tcps->tcps_mss_max_ipv6;
317 
318 	if (mss < tcps->tcps_mss_min)
319 		mss = tcps->tcps_mss_min;
320 	if (mss > mss_max)
321 		mss = mss_max;
322 	/*
323 	 * Unless naglim has been set by our client to
324 	 * a non-mss value, force naglim to track mss.
325 	 * This can help to aggregate small writes.
326 	 */
327 	if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim)
328 		tcp->tcp_naglim = mss;
329 	/*
330 	 * TCP should be able to buffer at least 4 MSS data for obvious
331 	 * performance reason.
332 	 */
333 	if ((mss << 2) > connp->conn_sndbuf)
334 		connp->conn_sndbuf = mss << 2;
335 
336 	/*
337 	 * Set the send lowater to at least twice of MSS.
338 	 */
339 	if ((mss << 1) > connp->conn_sndlowat)
340 		connp->conn_sndlowat = mss << 1;
341 
342 	/*
343 	 * Update tcp_cwnd according to the new value of MSS. Keep the
344 	 * previous ratio to preserve the transmit rate.
345 	 */
346 	tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss;
347 	tcp->tcp_cwnd_cnt = 0;
348 
349 	tcp->tcp_mss = mss;
350 	(void) tcp_maxpsz_set(tcp, B_TRUE);
351 }
352 
353 /*
354  * Extract option values from a tcp header.  We put any found values into the
355  * tcpopt struct and return a bitmask saying which options were found.
356  */
357 int
358 tcp_parse_options(tcpha_t *tcpha, tcp_opt_t *tcpopt)
359 {
360 	uchar_t		*endp;
361 	int		len;
362 	uint32_t	mss;
363 	uchar_t		*up = (uchar_t *)tcpha;
364 	int		found = 0;
365 	int32_t		sack_len;
366 	tcp_seq		sack_begin, sack_end;
367 	tcp_t		*tcp;
368 
369 	endp = up + TCP_HDR_LENGTH(tcpha);
370 	up += TCP_MIN_HEADER_LENGTH;
371 	/*
372 	 * If timestamp option is aligned as recommended in RFC 7323 Appendix
373 	 * A, and is the only option, return quickly.
374 	 */
375 	if (TCP_HDR_LENGTH(tcpha) == (uint32_t)TCP_MIN_HEADER_LENGTH +
376 	    TCPOPT_REAL_TS_LEN &&
377 	    OK_32PTR(up) &&
378 	    *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) {
379 		tcpopt->tcp_opt_ts_val = ABE32_TO_U32((up+4));
380 		tcpopt->tcp_opt_ts_ecr = ABE32_TO_U32((up+8));
381 
382 		return (TCP_OPT_TSTAMP_PRESENT);
383 	}
384 	while (up < endp) {
385 		len = endp - up;
386 		switch (*up) {
387 		case TCPOPT_EOL:
388 			break;
389 
390 		case TCPOPT_NOP:
391 			up++;
392 			continue;
393 
394 		case TCPOPT_MAXSEG:
395 			if (len < TCPOPT_MAXSEG_LEN ||
396 			    up[1] != TCPOPT_MAXSEG_LEN)
397 				break;
398 
399 			mss = BE16_TO_U16(up+2);
400 			/* Caller must handle tcp_mss_min and tcp_mss_max_* */
401 			tcpopt->tcp_opt_mss = mss;
402 			found |= TCP_OPT_MSS_PRESENT;
403 
404 			up += TCPOPT_MAXSEG_LEN;
405 			continue;
406 
407 		case TCPOPT_WSCALE:
408 			if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN)
409 				break;
410 
411 			if (up[2] > TCP_MAX_WINSHIFT)
412 				tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT;
413 			else
414 				tcpopt->tcp_opt_wscale = up[2];
415 			found |= TCP_OPT_WSCALE_PRESENT;
416 
417 			up += TCPOPT_WS_LEN;
418 			continue;
419 
420 		case TCPOPT_SACK_PERMITTED:
421 			if (len < TCPOPT_SACK_OK_LEN ||
422 			    up[1] != TCPOPT_SACK_OK_LEN)
423 				break;
424 			found |= TCP_OPT_SACK_OK_PRESENT;
425 			up += TCPOPT_SACK_OK_LEN;
426 			continue;
427 
428 		case TCPOPT_SACK:
429 			if (len <= 2 || up[1] <= 2 || len < up[1])
430 				break;
431 
432 			/* If TCP is not interested in SACK blks... */
433 			if ((tcp = tcpopt->tcp) == NULL) {
434 				up += up[1];
435 				continue;
436 			}
437 			sack_len = up[1] - TCPOPT_HEADER_LEN;
438 			up += TCPOPT_HEADER_LEN;
439 
440 			/*
441 			 * If the list is empty, allocate one and assume
442 			 * nothing is sack'ed.
443 			 */
444 			if (tcp->tcp_notsack_list == NULL) {
445 				tcp_notsack_update(&(tcp->tcp_notsack_list),
446 				    tcp->tcp_suna, tcp->tcp_snxt,
447 				    &(tcp->tcp_num_notsack_blk),
448 				    &(tcp->tcp_cnt_notsack_list));
449 
450 				/*
451 				 * Make sure tcp_notsack_list is not NULL.
452 				 * This happens when kmem_alloc(KM_NOSLEEP)
453 				 * returns NULL.
454 				 */
455 				if (tcp->tcp_notsack_list == NULL) {
456 					up += sack_len;
457 					continue;
458 				}
459 				tcp->tcp_fack = tcp->tcp_suna;
460 			}
461 
462 			while (sack_len > 0) {
463 				if (up + 8 > endp) {
464 					up = endp;
465 					break;
466 				}
467 				sack_begin = BE32_TO_U32(up);
468 				up += 4;
469 				sack_end = BE32_TO_U32(up);
470 				up += 4;
471 				sack_len -= 8;
472 				/*
473 				 * Bounds checking.  Make sure the SACK
474 				 * info is within tcp_suna and tcp_snxt.
475 				 * If this SACK blk is out of bound, ignore
476 				 * it but continue to parse the following
477 				 * blks.
478 				 */
479 				if (SEQ_LEQ(sack_end, sack_begin) ||
480 				    SEQ_LT(sack_begin, tcp->tcp_suna) ||
481 				    SEQ_GT(sack_end, tcp->tcp_snxt)) {
482 					continue;
483 				}
484 				tcp_notsack_insert(&(tcp->tcp_notsack_list),
485 				    sack_begin, sack_end,
486 				    &(tcp->tcp_num_notsack_blk),
487 				    &(tcp->tcp_cnt_notsack_list));
488 				if (SEQ_GT(sack_end, tcp->tcp_fack)) {
489 					tcp->tcp_fack = sack_end;
490 				}
491 			}
492 			found |= TCP_OPT_SACK_PRESENT;
493 			continue;
494 
495 		case TCPOPT_TSTAMP:
496 			if (len < TCPOPT_TSTAMP_LEN ||
497 			    up[1] != TCPOPT_TSTAMP_LEN)
498 				break;
499 
500 			tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2);
501 			tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6);
502 
503 			found |= TCP_OPT_TSTAMP_PRESENT;
504 
505 			up += TCPOPT_TSTAMP_LEN;
506 			continue;
507 
508 		default:
509 			if (len <= 1 || len < (int)up[1] || up[1] == 0)
510 				break;
511 			up += up[1];
512 			continue;
513 		}
514 		break;
515 	}
516 	return (found);
517 }
518 
519 /*
520  * Process all TCP option in SYN segment.  Note that this function should
521  * be called after tcp_set_destination() is called so that the necessary info
522  * from IRE is already set in the tcp structure.
523  *
524  * This function sets up the correct tcp_mss value according to the
525  * MSS option value and our header size.  It also sets up the window scale
526  * and timestamp values, and initialize SACK info blocks.  But it does not
527  * change receive window size after setting the tcp_mss value.  The caller
528  * should do the appropriate change.
529  */
530 static void
531 tcp_process_options(tcp_t *tcp, tcpha_t *tcpha)
532 {
533 	int options;
534 	tcp_opt_t tcpopt;
535 	uint32_t mss_max;
536 	char *tmp_tcph;
537 	tcp_stack_t	*tcps = tcp->tcp_tcps;
538 	conn_t		*connp = tcp->tcp_connp;
539 
540 	tcpopt.tcp = NULL;
541 	options = tcp_parse_options(tcpha, &tcpopt);
542 
543 	/*
544 	 * Process MSS option.  Note that MSS option value does not account
545 	 * for IP or TCP options.  This means that it is equal to MTU - minimum
546 	 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for
547 	 * IPv6.
548 	 */
549 	if (!(options & TCP_OPT_MSS_PRESENT)) {
550 		if (connp->conn_ipversion == IPV4_VERSION)
551 			tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4;
552 		else
553 			tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6;
554 	} else {
555 		if (connp->conn_ipversion == IPV4_VERSION)
556 			mss_max = tcps->tcps_mss_max_ipv4;
557 		else
558 			mss_max = tcps->tcps_mss_max_ipv6;
559 		if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min)
560 			tcpopt.tcp_opt_mss = tcps->tcps_mss_min;
561 		else if (tcpopt.tcp_opt_mss > mss_max)
562 			tcpopt.tcp_opt_mss = mss_max;
563 	}
564 
565 	/* Process Window Scale option. */
566 	if (options & TCP_OPT_WSCALE_PRESENT) {
567 		tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale;
568 		tcp->tcp_snd_ws_ok = B_TRUE;
569 	} else {
570 		tcp->tcp_snd_ws = B_FALSE;
571 		tcp->tcp_snd_ws_ok = B_FALSE;
572 		tcp->tcp_rcv_ws = B_FALSE;
573 	}
574 
575 	/* Process Timestamp option. */
576 	if ((options & TCP_OPT_TSTAMP_PRESENT) &&
577 	    (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) {
578 		tmp_tcph = (char *)tcp->tcp_tcpha;
579 
580 		tcp->tcp_snd_ts_ok = B_TRUE;
581 		tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val;
582 		tcp->tcp_last_rcv_lbolt = ddi_get_lbolt64();
583 		ASSERT(OK_32PTR(tmp_tcph));
584 		ASSERT(connp->conn_ht_ulp_len == TCP_MIN_HEADER_LENGTH);
585 
586 		/* Fill in our template header with basic timestamp option. */
587 		tmp_tcph += connp->conn_ht_ulp_len;
588 		tmp_tcph[0] = TCPOPT_NOP;
589 		tmp_tcph[1] = TCPOPT_NOP;
590 		tmp_tcph[2] = TCPOPT_TSTAMP;
591 		tmp_tcph[3] = TCPOPT_TSTAMP_LEN;
592 		connp->conn_ht_iphc_len += TCPOPT_REAL_TS_LEN;
593 		connp->conn_ht_ulp_len += TCPOPT_REAL_TS_LEN;
594 		tcp->tcp_tcpha->tha_offset_and_reserved += (3 << 4);
595 	} else {
596 		tcp->tcp_snd_ts_ok = B_FALSE;
597 	}
598 
599 	/*
600 	 * Process SACK options.  If SACK is enabled for this connection,
601 	 * then allocate the SACK info structure.  Note the following ways
602 	 * when tcp_snd_sack_ok is set to true.
603 	 *
604 	 * For active connection: in tcp_set_destination() called in
605 	 * tcp_connect().
606 	 *
607 	 * For passive connection: in tcp_set_destination() called in
608 	 * tcp_input_listener().
609 	 *
610 	 * That's the reason why the extra TCP_IS_DETACHED() check is there.
611 	 * That check makes sure that if we did not send a SACK OK option,
612 	 * we will not enable SACK for this connection even though the other
613 	 * side sends us SACK OK option.  For active connection, the SACK
614 	 * info structure has already been allocated.  So we need to free
615 	 * it if SACK is disabled.
616 	 */
617 	if ((options & TCP_OPT_SACK_OK_PRESENT) &&
618 	    (tcp->tcp_snd_sack_ok ||
619 	    (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) {
620 		ASSERT(tcp->tcp_num_sack_blk == 0);
621 		ASSERT(tcp->tcp_notsack_list == NULL);
622 
623 		tcp->tcp_snd_sack_ok = B_TRUE;
624 		if (tcp->tcp_snd_ts_ok) {
625 			tcp->tcp_max_sack_blk = 3;
626 		} else {
627 			tcp->tcp_max_sack_blk = 4;
628 		}
629 	} else if (tcp->tcp_snd_sack_ok) {
630 		/*
631 		 * Resetting tcp_snd_sack_ok to B_FALSE so that
632 		 * no SACK info will be used for this
633 		 * connection.  This assumes that SACK usage
634 		 * permission is negotiated.  This may need
635 		 * to be changed once this is clarified.
636 		 */
637 		ASSERT(tcp->tcp_num_sack_blk == 0);
638 		ASSERT(tcp->tcp_notsack_list == NULL);
639 		tcp->tcp_snd_sack_ok = B_FALSE;
640 	}
641 
642 	/*
643 	 * Now we know the exact TCP/IP header length, subtract
644 	 * that from tcp_mss to get our side's MSS.
645 	 */
646 	tcp->tcp_mss -= connp->conn_ht_iphc_len;
647 
648 	/*
649 	 * Here we assume that the other side's header size will be equal to
650 	 * our header size.  We calculate the real MSS accordingly.  Need to
651 	 * take into additional stuffs IPsec puts in.
652 	 *
653 	 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header)
654 	 */
655 	tcpopt.tcp_opt_mss -= connp->conn_ht_iphc_len +
656 	    tcp->tcp_ipsec_overhead -
657 	    ((connp->conn_ipversion == IPV4_VERSION ?
658 	    IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH);
659 
660 	/*
661 	 * Set MSS to the smaller one of both ends of the connection.
662 	 * We should not have called tcp_mss_set() before, but our
663 	 * side of the MSS should have been set to a proper value
664 	 * by tcp_set_destination().  tcp_mss_set() will also set up the
665 	 * STREAM head parameters properly.
666 	 *
667 	 * If we have a larger-than-16-bit window but the other side
668 	 * didn't want to do window scale, tcp_rwnd_set() will take
669 	 * care of that.
670 	 */
671 	tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss));
672 
673 	/*
674 	 * Initialize tcp_cwnd value. After tcp_mss_set(), tcp_mss has been
675 	 * updated properly.
676 	 */
677 	TCP_SET_INIT_CWND(tcp, tcp->tcp_mss, tcps->tcps_slow_start_initial);
678 
679 	if (tcp->tcp_cc_algo->conn_init != NULL)
680 		tcp->tcp_cc_algo->conn_init(&tcp->tcp_ccv);
681 }
682 
683 /*
684  * Add a new piece to the tcp reassembly queue.  If the gap at the beginning
685  * is filled, return as much as we can.  The message passed in may be
686  * multi-part, chained using b_cont.  "start" is the starting sequence
687  * number for this piece.
688  */
689 static mblk_t *
690 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start)
691 {
692 	uint32_t	end, bytes;
693 	mblk_t		*mp1;
694 	mblk_t		*mp2;
695 	mblk_t		*next_mp;
696 	uint32_t	u1;
697 	tcp_stack_t	*tcps = tcp->tcp_tcps;
698 
699 
700 	/* Walk through all the new pieces. */
701 	do {
702 		ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
703 		    (uintptr_t)INT_MAX);
704 		end = start + (int)(mp->b_wptr - mp->b_rptr);
705 		next_mp = mp->b_cont;
706 		if (start == end) {
707 			/* Empty.  Blast it. */
708 			freeb(mp);
709 			continue;
710 		}
711 		bytes = end - start;
712 		mp->b_cont = NULL;
713 		TCP_REASS_SET_SEQ(mp, start);
714 		TCP_REASS_SET_END(mp, end);
715 		mp1 = tcp->tcp_reass_tail;
716 		if (mp1 == NULL || SEQ_GEQ(start, TCP_REASS_END(mp1))) {
717 			if (mp1 != NULL) {
718 				/*
719 				 * New stuff is beyond the tail; link it on the
720 				 * end.
721 				 */
722 				mp1->b_cont = mp;
723 			} else {
724 				tcp->tcp_reass_head = mp;
725 			}
726 			tcp->tcp_reass_tail = mp;
727 			TCPS_BUMP_MIB(tcps, tcpInDataUnorderSegs);
728 			TCPS_UPDATE_MIB(tcps, tcpInDataUnorderBytes, bytes);
729 			tcp->tcp_cs.tcp_in_data_unorder_segs++;
730 			tcp->tcp_cs.tcp_in_data_unorder_bytes += bytes;
731 			continue;
732 		}
733 		mp1 = tcp->tcp_reass_head;
734 		u1 = TCP_REASS_SEQ(mp1);
735 		/* New stuff at the front? */
736 		if (SEQ_LT(start, u1)) {
737 			/* Yes... Check for overlap. */
738 			mp->b_cont = mp1;
739 			tcp->tcp_reass_head = mp;
740 			tcp_reass_elim_overlap(tcp, mp);
741 			continue;
742 		}
743 		/*
744 		 * The new piece fits somewhere between the head and tail.
745 		 * We find our slot, where mp1 precedes us and mp2 trails.
746 		 */
747 		for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) {
748 			u1 = TCP_REASS_SEQ(mp2);
749 			if (SEQ_LEQ(start, u1))
750 				break;
751 		}
752 		/* Link ourselves in */
753 		mp->b_cont = mp2;
754 		mp1->b_cont = mp;
755 
756 		/* Trim overlap with following mblk(s) first */
757 		tcp_reass_elim_overlap(tcp, mp);
758 
759 		/* Trim overlap with preceding mblk */
760 		tcp_reass_elim_overlap(tcp, mp1);
761 
762 	} while (start = end, mp = next_mp);
763 	mp1 = tcp->tcp_reass_head;
764 	/* Anything ready to go? */
765 	if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt)
766 		return (NULL);
767 	/* Eat what we can off the queue */
768 	for (;;) {
769 		mp = mp1->b_cont;
770 		end = TCP_REASS_END(mp1);
771 		TCP_REASS_SET_SEQ(mp1, 0);
772 		TCP_REASS_SET_END(mp1, 0);
773 		if (!mp) {
774 			tcp->tcp_reass_tail = NULL;
775 			break;
776 		}
777 		if (end != TCP_REASS_SEQ(mp)) {
778 			mp1->b_cont = NULL;
779 			break;
780 		}
781 		mp1 = mp;
782 	}
783 	mp1 = tcp->tcp_reass_head;
784 	tcp->tcp_reass_head = mp;
785 	return (mp1);
786 }
787 
788 /* Eliminate any overlap that mp may have over later mblks */
789 static void
790 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp)
791 {
792 	uint32_t	end;
793 	mblk_t		*mp1;
794 	uint32_t	u1;
795 	tcp_stack_t	*tcps = tcp->tcp_tcps;
796 
797 	end = TCP_REASS_END(mp);
798 	while ((mp1 = mp->b_cont) != NULL) {
799 		u1 = TCP_REASS_SEQ(mp1);
800 		if (!SEQ_GT(end, u1))
801 			break;
802 		if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) {
803 			mp->b_wptr -= end - u1;
804 			TCP_REASS_SET_END(mp, u1);
805 			TCPS_BUMP_MIB(tcps, tcpInDataPartDupSegs);
806 			TCPS_UPDATE_MIB(tcps, tcpInDataPartDupBytes,
807 			    end - u1);
808 			break;
809 		}
810 		mp->b_cont = mp1->b_cont;
811 		TCP_REASS_SET_SEQ(mp1, 0);
812 		TCP_REASS_SET_END(mp1, 0);
813 		freeb(mp1);
814 		TCPS_BUMP_MIB(tcps, tcpInDataDupSegs);
815 		TCPS_UPDATE_MIB(tcps, tcpInDataDupBytes, end - u1);
816 	}
817 	if (!mp1)
818 		tcp->tcp_reass_tail = mp;
819 }
820 
821 /*
822  * This function does PAWS protection check, per RFC 7323 section 5. Requires
823  * that timestamp options are already processed into tcpoptp. Returns B_TRUE if
824  * the segment passes the PAWS test, else returns B_FALSE.
825  */
826 boolean_t
827 tcp_paws_check(tcp_t *tcp, const tcp_opt_t *tcpoptp)
828 {
829 	if (TSTMP_LT(tcpoptp->tcp_opt_ts_val,
830 	    tcp->tcp_ts_recent)) {
831 		if (LBOLT_FASTPATH64 <
832 		    (tcp->tcp_last_rcv_lbolt + PAWS_TIMEOUT)) {
833 			/* This segment is not acceptable. */
834 			return (B_FALSE);
835 		} else {
836 			/*
837 			 * Connection has been idle for
838 			 * too long.  Reset the timestamp
839 			 */
840 			tcp->tcp_ts_recent =
841 			    tcpoptp->tcp_opt_ts_val;
842 		}
843 	}
844 	return (B_TRUE);
845 }
846 
847 /*
848  * Defense for the SYN attack -
849  * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest
850  *    one from the list of droppable eagers. This list is a subset of q0.
851  *    see comments before the definition of MAKE_DROPPABLE().
852  * 2. Don't drop a SYN request before its first timeout. This gives every
853  *    request at least til the first timeout to complete its 3-way handshake.
854  * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many
855  *    requests currently on the queue that has timed out. This will be used
856  *    as an indicator of whether an attack is under way, so that appropriate
857  *    actions can be taken. (It's incremented in tcp_timer() and decremented
858  *    either when eager goes into ESTABLISHED, or gets freed up.)
859  * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on
860  *    # of timeout drops back to <= q0len/32 => SYN alert off
861  */
862 static boolean_t
863 tcp_drop_q0(tcp_t *tcp)
864 {
865 	tcp_t	*eager;
866 	mblk_t	*mp;
867 	tcp_stack_t	*tcps = tcp->tcp_tcps;
868 
869 	ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock));
870 	ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0);
871 
872 	/* Pick oldest eager from the list of droppable eagers */
873 	eager = tcp->tcp_eager_prev_drop_q0;
874 
875 	/* If list is empty. return B_FALSE */
876 	if (eager == tcp) {
877 		return (B_FALSE);
878 	}
879 
880 	/* If allocated, the mp will be freed in tcp_clean_death_wrapper() */
881 	if ((mp = allocb(0, BPRI_HI)) == NULL)
882 		return (B_FALSE);
883 
884 	/*
885 	 * Take this eager out from the list of droppable eagers since we are
886 	 * going to drop it.
887 	 */
888 	MAKE_UNDROPPABLE(eager);
889 
890 	if (tcp->tcp_connp->conn_debug) {
891 		(void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE,
892 		    "tcp_drop_q0: listen half-open queue (max=%d) overflow"
893 		    " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0,
894 		    tcp->tcp_conn_req_cnt_q0,
895 		    tcp_display(tcp, NULL, DISP_PORT_ONLY));
896 	}
897 
898 	TCPS_BUMP_MIB(tcps, tcpHalfOpenDrop);
899 
900 	/* Put a reference on the conn as we are enqueueing it in the sqeue */
901 	CONN_INC_REF(eager->tcp_connp);
902 
903 	SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp,
904 	    tcp_clean_death_wrapper, eager->tcp_connp, NULL,
905 	    SQ_FILL, SQTAG_TCP_DROP_Q0);
906 
907 	return (B_TRUE);
908 }
909 
910 /*
911  * Handle a SYN on an AF_INET6 socket; can be either IPv4 or IPv6
912  */
913 static mblk_t *
914 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp,
915     ip_recv_attr_t *ira)
916 {
917 	tcp_t		*ltcp = lconnp->conn_tcp;
918 	tcp_t		*tcp = connp->conn_tcp;
919 	mblk_t		*tpi_mp;
920 	ipha_t		*ipha;
921 	ip6_t		*ip6h;
922 	sin6_t		sin6;
923 	uint_t		ifindex = ira->ira_ruifindex;
924 	tcp_stack_t	*tcps = tcp->tcp_tcps;
925 
926 	if (ira->ira_flags & IRAF_IS_IPV4) {
927 		ipha = (ipha_t *)mp->b_rptr;
928 
929 		connp->conn_ipversion = IPV4_VERSION;
930 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_laddr_v6);
931 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_faddr_v6);
932 		connp->conn_saddr_v6 = connp->conn_laddr_v6;
933 
934 		sin6 = sin6_null;
935 		sin6.sin6_addr = connp->conn_faddr_v6;
936 		sin6.sin6_port = connp->conn_fport;
937 		sin6.sin6_family = AF_INET6;
938 		sin6.__sin6_src_id = ip_srcid_find_addr(&connp->conn_laddr_v6,
939 		    IPCL_ZONEID(lconnp), tcps->tcps_netstack);
940 
941 		if (connp->conn_recv_ancillary.crb_recvdstaddr) {
942 			sin6_t	sin6d;
943 
944 			sin6d = sin6_null;
945 			sin6d.sin6_addr = connp->conn_laddr_v6;
946 			sin6d.sin6_port = connp->conn_lport;
947 			sin6d.sin6_family = AF_INET;
948 			tpi_mp = mi_tpi_extconn_ind(NULL,
949 			    (char *)&sin6d, sizeof (sin6_t),
950 			    (char *)&tcp,
951 			    (t_scalar_t)sizeof (intptr_t),
952 			    (char *)&sin6d, sizeof (sin6_t),
953 			    (t_scalar_t)ltcp->tcp_conn_req_seqnum);
954 		} else {
955 			tpi_mp = mi_tpi_conn_ind(NULL,
956 			    (char *)&sin6, sizeof (sin6_t),
957 			    (char *)&tcp, (t_scalar_t)sizeof (intptr_t),
958 			    (t_scalar_t)ltcp->tcp_conn_req_seqnum);
959 		}
960 	} else {
961 		ip6h = (ip6_t *)mp->b_rptr;
962 
963 		connp->conn_ipversion = IPV6_VERSION;
964 		connp->conn_laddr_v6 = ip6h->ip6_dst;
965 		connp->conn_faddr_v6 = ip6h->ip6_src;
966 		connp->conn_saddr_v6 = connp->conn_laddr_v6;
967 
968 		sin6 = sin6_null;
969 		sin6.sin6_addr = connp->conn_faddr_v6;
970 		sin6.sin6_port = connp->conn_fport;
971 		sin6.sin6_family = AF_INET6;
972 		sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK;
973 		sin6.__sin6_src_id = ip_srcid_find_addr(&connp->conn_laddr_v6,
974 		    IPCL_ZONEID(lconnp), tcps->tcps_netstack);
975 
976 		if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) {
977 			/* Pass up the scope_id of remote addr */
978 			sin6.sin6_scope_id = ifindex;
979 		} else {
980 			sin6.sin6_scope_id = 0;
981 		}
982 		if (connp->conn_recv_ancillary.crb_recvdstaddr) {
983 			sin6_t	sin6d;
984 
985 			sin6d = sin6_null;
986 			sin6.sin6_addr = connp->conn_laddr_v6;
987 			sin6d.sin6_port = connp->conn_lport;
988 			sin6d.sin6_family = AF_INET6;
989 			if (IN6_IS_ADDR_LINKSCOPE(&connp->conn_laddr_v6))
990 				sin6d.sin6_scope_id = ifindex;
991 
992 			tpi_mp = mi_tpi_extconn_ind(NULL,
993 			    (char *)&sin6d, sizeof (sin6_t),
994 			    (char *)&tcp, (t_scalar_t)sizeof (intptr_t),
995 			    (char *)&sin6d, sizeof (sin6_t),
996 			    (t_scalar_t)ltcp->tcp_conn_req_seqnum);
997 		} else {
998 			tpi_mp = mi_tpi_conn_ind(NULL,
999 			    (char *)&sin6, sizeof (sin6_t),
1000 			    (char *)&tcp, (t_scalar_t)sizeof (intptr_t),
1001 			    (t_scalar_t)ltcp->tcp_conn_req_seqnum);
1002 		}
1003 	}
1004 
1005 	tcp->tcp_mss = tcps->tcps_mss_def_ipv6;
1006 	return (tpi_mp);
1007 }
1008 
1009 /* Handle a SYN on an AF_INET socket */
1010 static mblk_t *
1011 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, mblk_t *mp,
1012     ip_recv_attr_t *ira)
1013 {
1014 	tcp_t		*ltcp = lconnp->conn_tcp;
1015 	tcp_t		*tcp = connp->conn_tcp;
1016 	sin_t		sin;
1017 	mblk_t		*tpi_mp = NULL;
1018 	tcp_stack_t	*tcps = tcp->tcp_tcps;
1019 	ipha_t		*ipha;
1020 
1021 	ASSERT(ira->ira_flags & IRAF_IS_IPV4);
1022 	ipha = (ipha_t *)mp->b_rptr;
1023 
1024 	connp->conn_ipversion = IPV4_VERSION;
1025 	IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_laddr_v6);
1026 	IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_faddr_v6);
1027 	connp->conn_saddr_v6 = connp->conn_laddr_v6;
1028 
1029 	sin = sin_null;
1030 	sin.sin_addr.s_addr = connp->conn_faddr_v4;
1031 	sin.sin_port = connp->conn_fport;
1032 	sin.sin_family = AF_INET;
1033 	if (lconnp->conn_recv_ancillary.crb_recvdstaddr) {
1034 		sin_t	sind;
1035 
1036 		sind = sin_null;
1037 		sind.sin_addr.s_addr = connp->conn_laddr_v4;
1038 		sind.sin_port = connp->conn_lport;
1039 		sind.sin_family = AF_INET;
1040 		tpi_mp = mi_tpi_extconn_ind(NULL,
1041 		    (char *)&sind, sizeof (sin_t), (char *)&tcp,
1042 		    (t_scalar_t)sizeof (intptr_t), (char *)&sind,
1043 		    sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum);
1044 	} else {
1045 		tpi_mp = mi_tpi_conn_ind(NULL,
1046 		    (char *)&sin, sizeof (sin_t),
1047 		    (char *)&tcp, (t_scalar_t)sizeof (intptr_t),
1048 		    (t_scalar_t)ltcp->tcp_conn_req_seqnum);
1049 	}
1050 
1051 	tcp->tcp_mss = tcps->tcps_mss_def_ipv4;
1052 	return (tpi_mp);
1053 }
1054 
1055 /*
1056  * Called via squeue to get on to eager's perimeter. It sends a
1057  * TH_RST if eager is in the fanout table. The listener wants the
1058  * eager to disappear either by means of tcp_eager_blowoff() or
1059  * tcp_eager_cleanup() being called. tcp_eager_kill() can also be
1060  * called (via squeue) if the eager cannot be inserted in the
1061  * fanout table in tcp_input_listener().
1062  */
1063 /* ARGSUSED */
1064 void
1065 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1066 {
1067 	conn_t	*econnp = (conn_t *)arg;
1068 	tcp_t	*eager = econnp->conn_tcp;
1069 	tcp_t	*listener = eager->tcp_listener;
1070 
1071 	/*
1072 	 * We could be called because listener is closing. Since
1073 	 * the eager was using listener's queue's, we avoid
1074 	 * using the listeners queues from now on.
1075 	 */
1076 	ASSERT(eager->tcp_detached);
1077 	econnp->conn_rq = NULL;
1078 	econnp->conn_wq = NULL;
1079 
1080 	/*
1081 	 * An eager's conn_fanout will be NULL if it's a duplicate
1082 	 * for an existing 4-tuples in the conn fanout table.
1083 	 * We don't want to send an RST out in such case.
1084 	 */
1085 	if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) {
1086 		tcp_xmit_ctl("tcp_eager_kill, can't wait",
1087 		    eager, eager->tcp_snxt, 0, TH_RST);
1088 	}
1089 
1090 	/* We are here because listener wants this eager gone */
1091 	if (listener != NULL) {
1092 		mutex_enter(&listener->tcp_eager_lock);
1093 		tcp_eager_unlink(eager);
1094 		if (eager->tcp_tconnind_started) {
1095 			/*
1096 			 * The eager has sent a conn_ind up to the
1097 			 * listener but listener decides to close
1098 			 * instead. We need to drop the extra ref
1099 			 * placed on eager in tcp_input_data() before
1100 			 * sending the conn_ind to listener.
1101 			 */
1102 			CONN_DEC_REF(econnp);
1103 		}
1104 		mutex_exit(&listener->tcp_eager_lock);
1105 		CONN_DEC_REF(listener->tcp_connp);
1106 	}
1107 
1108 	if (eager->tcp_state != TCPS_CLOSED)
1109 		tcp_close_detached(eager);
1110 }
1111 
1112 /*
1113  * Reset any eager connection hanging off this listener marked
1114  * with 'seqnum' and then reclaim it's resources.
1115  */
1116 boolean_t
1117 tcp_eager_blowoff(tcp_t	*listener, t_scalar_t seqnum)
1118 {
1119 	tcp_t	*eager;
1120 	mblk_t	*mp;
1121 
1122 	eager = listener;
1123 	mutex_enter(&listener->tcp_eager_lock);
1124 	do {
1125 		eager = eager->tcp_eager_next_q;
1126 		if (eager == NULL) {
1127 			mutex_exit(&listener->tcp_eager_lock);
1128 			return (B_FALSE);
1129 		}
1130 	} while (eager->tcp_conn_req_seqnum != seqnum);
1131 
1132 	if (eager->tcp_closemp_used) {
1133 		mutex_exit(&listener->tcp_eager_lock);
1134 		return (B_TRUE);
1135 	}
1136 	eager->tcp_closemp_used = B_TRUE;
1137 	TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15);
1138 	CONN_INC_REF(eager->tcp_connp);
1139 	mutex_exit(&listener->tcp_eager_lock);
1140 	mp = &eager->tcp_closemp;
1141 	SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill,
1142 	    eager->tcp_connp, NULL, SQ_FILL, SQTAG_TCP_EAGER_BLOWOFF);
1143 	return (B_TRUE);
1144 }
1145 
1146 /*
1147  * Reset any eager connection hanging off this listener
1148  * and then reclaim it's resources.
1149  */
1150 void
1151 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only)
1152 {
1153 	tcp_t	*eager;
1154 	mblk_t	*mp;
1155 	tcp_stack_t	*tcps = listener->tcp_tcps;
1156 
1157 	ASSERT(MUTEX_HELD(&listener->tcp_eager_lock));
1158 
1159 	if (!q0_only) {
1160 		/* First cleanup q */
1161 		TCP_STAT(tcps, tcp_eager_blowoff_q);
1162 		eager = listener->tcp_eager_next_q;
1163 		while (eager != NULL) {
1164 			if (!eager->tcp_closemp_used) {
1165 				eager->tcp_closemp_used = B_TRUE;
1166 				TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15);
1167 				CONN_INC_REF(eager->tcp_connp);
1168 				mp = &eager->tcp_closemp;
1169 				SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp,
1170 				    tcp_eager_kill, eager->tcp_connp, NULL,
1171 				    SQ_FILL, SQTAG_TCP_EAGER_CLEANUP);
1172 			}
1173 			eager = eager->tcp_eager_next_q;
1174 		}
1175 	}
1176 	/* Then cleanup q0 */
1177 	TCP_STAT(tcps, tcp_eager_blowoff_q0);
1178 	eager = listener->tcp_eager_next_q0;
1179 	while (eager != listener) {
1180 		if (!eager->tcp_closemp_used) {
1181 			eager->tcp_closemp_used = B_TRUE;
1182 			TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15);
1183 			CONN_INC_REF(eager->tcp_connp);
1184 			mp = &eager->tcp_closemp;
1185 			SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp,
1186 			    tcp_eager_kill, eager->tcp_connp, NULL, SQ_FILL,
1187 			    SQTAG_TCP_EAGER_CLEANUP_Q0);
1188 		}
1189 		eager = eager->tcp_eager_next_q0;
1190 	}
1191 }
1192 
1193 /*
1194  * If we are an eager connection hanging off a listener that hasn't
1195  * formally accepted the connection yet, get off its list and blow off
1196  * any data that we have accumulated.
1197  */
1198 void
1199 tcp_eager_unlink(tcp_t *tcp)
1200 {
1201 	tcp_t	*listener = tcp->tcp_listener;
1202 
1203 	ASSERT(listener != NULL);
1204 	ASSERT(MUTEX_HELD(&listener->tcp_eager_lock));
1205 	if (tcp->tcp_eager_next_q0 != NULL) {
1206 		ASSERT(tcp->tcp_eager_prev_q0 != NULL);
1207 
1208 		/* Remove the eager tcp from q0 */
1209 		tcp->tcp_eager_next_q0->tcp_eager_prev_q0 =
1210 		    tcp->tcp_eager_prev_q0;
1211 		tcp->tcp_eager_prev_q0->tcp_eager_next_q0 =
1212 		    tcp->tcp_eager_next_q0;
1213 		ASSERT(listener->tcp_conn_req_cnt_q0 > 0);
1214 		listener->tcp_conn_req_cnt_q0--;
1215 
1216 		tcp->tcp_eager_next_q0 = NULL;
1217 		tcp->tcp_eager_prev_q0 = NULL;
1218 
1219 		/*
1220 		 * Take the eager out, if it is in the list of droppable
1221 		 * eagers.
1222 		 */
1223 		MAKE_UNDROPPABLE(tcp);
1224 
1225 		if (tcp->tcp_syn_rcvd_timeout != 0) {
1226 			/* we have timed out before */
1227 			ASSERT(listener->tcp_syn_rcvd_timeout > 0);
1228 			listener->tcp_syn_rcvd_timeout--;
1229 		}
1230 	} else {
1231 		tcp_t   **tcpp = &listener->tcp_eager_next_q;
1232 		tcp_t	*prev = NULL;
1233 
1234 		for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) {
1235 			if (tcpp[0] == tcp) {
1236 				if (listener->tcp_eager_last_q == tcp) {
1237 					/*
1238 					 * If we are unlinking the last
1239 					 * element on the list, adjust
1240 					 * tail pointer. Set tail pointer
1241 					 * to nil when list is empty.
1242 					 */
1243 					ASSERT(tcp->tcp_eager_next_q == NULL);
1244 					if (listener->tcp_eager_last_q ==
1245 					    listener->tcp_eager_next_q) {
1246 						listener->tcp_eager_last_q =
1247 						    NULL;
1248 					} else {
1249 						/*
1250 						 * We won't get here if there
1251 						 * is only one eager in the
1252 						 * list.
1253 						 */
1254 						ASSERT(prev != NULL);
1255 						listener->tcp_eager_last_q =
1256 						    prev;
1257 					}
1258 				}
1259 				tcpp[0] = tcp->tcp_eager_next_q;
1260 				tcp->tcp_eager_next_q = NULL;
1261 				tcp->tcp_eager_last_q = NULL;
1262 				ASSERT(listener->tcp_conn_req_cnt_q > 0);
1263 				listener->tcp_conn_req_cnt_q--;
1264 				break;
1265 			}
1266 			prev = tcpp[0];
1267 		}
1268 	}
1269 	tcp->tcp_listener = NULL;
1270 }
1271 
1272 /* BEGIN CSTYLED */
1273 /*
1274  *
1275  * The sockfs ACCEPT path:
1276  * =======================
1277  *
1278  * The eager is now established in its own perimeter as soon as SYN is
1279  * received in tcp_input_listener(). When sockfs receives conn_ind, it
1280  * completes the accept processing on the acceptor STREAM. The sending
1281  * of conn_ind part is common for both sockfs listener and a TLI/XTI
1282  * listener but a TLI/XTI listener completes the accept processing
1283  * on the listener perimeter.
1284  *
1285  * Common control flow for 3 way handshake:
1286  * ----------------------------------------
1287  *
1288  * incoming SYN (listener perimeter)	-> tcp_input_listener()
1289  *
1290  * incoming SYN-ACK-ACK (eager perim)	-> tcp_input_data()
1291  * send T_CONN_IND (listener perim)	-> tcp_send_conn_ind()
1292  *
1293  * Sockfs ACCEPT Path:
1294  * -------------------
1295  *
1296  * open acceptor stream (tcp_open allocates tcp_tli_accept()
1297  * as STREAM entry point)
1298  *
1299  * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_tli_accept()
1300  *
1301  * tcp_tli_accept() extracts the eager and makes the q->q_ptr <-> eager
1302  * association (we are not behind eager's squeue but sockfs is protecting us
1303  * and no one knows about this stream yet. The STREAMS entry point q->q_info
1304  * is changed to point at tcp_wput().
1305  *
1306  * tcp_accept_common() sends any deferred eagers via tcp_send_pending() to
1307  * listener (done on listener's perimeter).
1308  *
1309  * tcp_tli_accept() calls tcp_accept_finish() on eagers perimeter to finish
1310  * accept.
1311  *
1312  * TLI/XTI client ACCEPT path:
1313  * ---------------------------
1314  *
1315  * soaccept() sends T_CONN_RES on the listener STREAM.
1316  *
1317  * tcp_tli_accept() -> tcp_accept_swap() complete the processing and send
1318  * a M_SETOPS mblk to eager perimeter to finish accept (tcp_accept_finish()).
1319  *
1320  * Locks:
1321  * ======
1322  *
1323  * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and
1324  * and listeners->tcp_eager_next_q.
1325  *
1326  * Referencing:
1327  * ============
1328  *
1329  * 1) We start out in tcp_input_listener by eager placing a ref on
1330  * listener and listener adding eager to listeners->tcp_eager_next_q0.
1331  *
1332  * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before
1333  * doing so we place a ref on the eager. This ref is finally dropped at the
1334  * end of tcp_accept_finish() while unwinding from the squeue, i.e. the
1335  * reference is dropped by the squeue framework.
1336  *
1337  * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish
1338  *
1339  * The reference must be released by the same entity that added the reference
1340  * In the above scheme, the eager is the entity that adds and releases the
1341  * references. Note that tcp_accept_finish executes in the squeue of the eager
1342  * (albeit after it is attached to the acceptor stream). Though 1. executes
1343  * in the listener's squeue, the eager is nascent at this point and the
1344  * reference can be considered to have been added on behalf of the eager.
1345  *
1346  * Eager getting a Reset or listener closing:
1347  * ==========================================
1348  *
1349  * Once the listener and eager are linked, the listener never does the unlink.
1350  * If the listener needs to close, tcp_eager_cleanup() is called which queues
1351  * a message on all eager perimeter. The eager then does the unlink, clears
1352  * any pointers to the listener's queue and drops the reference to the
1353  * listener. The listener waits in tcp_close outside the squeue until its
1354  * refcount has dropped to 1. This ensures that the listener has waited for
1355  * all eagers to clear their association with the listener.
1356  *
1357  * Similarly, if eager decides to go away, it can unlink itself and close.
1358  * When the T_CONN_RES comes down, we check if eager has closed. Note that
1359  * the reference to eager is still valid because of the extra ref we put
1360  * in tcp_send_conn_ind.
1361  *
1362  * Listener can always locate the eager under the protection
1363  * of the listener->tcp_eager_lock, and then do a refhold
1364  * on the eager during the accept processing.
1365  *
1366  * The acceptor stream accesses the eager in the accept processing
1367  * based on the ref placed on eager before sending T_conn_ind.
1368  * The only entity that can negate this refhold is a listener close
1369  * which is mutually exclusive with an active acceptor stream.
1370  *
1371  * Eager's reference on the listener
1372  * ===================================
1373  *
1374  * If the accept happens (even on a closed eager) the eager drops its
1375  * reference on the listener at the start of tcp_accept_finish. If the
1376  * eager is killed due to an incoming RST before the T_conn_ind is sent up,
1377  * the reference is dropped in tcp_closei_local. If the listener closes,
1378  * the reference is dropped in tcp_eager_kill. In all cases the reference
1379  * is dropped while executing in the eager's context (squeue).
1380  */
1381 /* END CSTYLED */
1382 
1383 /* Process the SYN packet, mp, directed at the listener 'tcp' */
1384 
1385 /*
1386  * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN.
1387  * tcp_input_data will not see any packets for listeners since the listener
1388  * has conn_recv set to tcp_input_listener.
1389  */
1390 /* ARGSUSED */
1391 static void
1392 tcp_input_listener(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
1393 {
1394 	tcpha_t		*tcpha;
1395 	uint32_t	seg_seq;
1396 	tcp_t		*eager;
1397 	int		err;
1398 	conn_t		*econnp = NULL;
1399 	squeue_t	*new_sqp;
1400 	mblk_t		*mp1;
1401 	uint_t		ip_hdr_len;
1402 	conn_t		*lconnp = (conn_t *)arg;
1403 	tcp_t		*listener = lconnp->conn_tcp;
1404 	tcp_stack_t	*tcps = listener->tcp_tcps;
1405 	ip_stack_t	*ipst = tcps->tcps_netstack->netstack_ip;
1406 	uint_t		flags;
1407 	mblk_t		*tpi_mp;
1408 	uint_t		ifindex = ira->ira_ruifindex;
1409 	boolean_t	tlc_set = B_FALSE;
1410 
1411 	ip_hdr_len = ira->ira_ip_hdr_length;
1412 	tcpha = (tcpha_t *)&mp->b_rptr[ip_hdr_len];
1413 	flags = (unsigned int)tcpha->tha_flags & 0xFF;
1414 
1415 	DTRACE_TCP5(receive, mblk_t *, NULL, ip_xmit_attr_t *, lconnp->conn_ixa,
1416 	    __dtrace_tcp_void_ip_t *, mp->b_rptr, tcp_t *, listener,
1417 	    __dtrace_tcp_tcph_t *, tcpha);
1418 
1419 	if (!(flags & TH_SYN)) {
1420 		if ((flags & TH_RST) || (flags & TH_URG)) {
1421 			freemsg(mp);
1422 			return;
1423 		}
1424 		if (flags & TH_ACK) {
1425 			/* Note this executes in listener's squeue */
1426 			tcp_xmit_listeners_reset(mp, ira, ipst, lconnp);
1427 			return;
1428 		}
1429 
1430 		freemsg(mp);
1431 		return;
1432 	}
1433 
1434 	if (listener->tcp_state != TCPS_LISTEN)
1435 		goto error2;
1436 
1437 	ASSERT(IPCL_IS_BOUND(lconnp));
1438 
1439 	mutex_enter(&listener->tcp_eager_lock);
1440 
1441 	/*
1442 	 * The system is under memory pressure, so we need to do our part
1443 	 * to relieve the pressure.  So we only accept new request if there
1444 	 * is nothing waiting to be accepted or waiting to complete the 3-way
1445 	 * handshake.  This means that busy listener will not get too many
1446 	 * new requests which they cannot handle in time while non-busy
1447 	 * listener is still functioning properly.
1448 	 */
1449 	if (tcps->tcps_reclaim && (listener->tcp_conn_req_cnt_q > 0 ||
1450 	    listener->tcp_conn_req_cnt_q0 > 0)) {
1451 		mutex_exit(&listener->tcp_eager_lock);
1452 		TCP_STAT(tcps, tcp_listen_mem_drop);
1453 		goto error2;
1454 	}
1455 
1456 	if (listener->tcp_conn_req_cnt_q >= listener->tcp_conn_req_max) {
1457 		mutex_exit(&listener->tcp_eager_lock);
1458 		TCP_STAT(tcps, tcp_listendrop);
1459 		TCPS_BUMP_MIB(tcps, tcpListenDrop);
1460 		if (lconnp->conn_debug) {
1461 			(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR,
1462 			    "tcp_input_listener: listen backlog (max=%d) "
1463 			    "overflow (%d pending) on %s",
1464 			    listener->tcp_conn_req_max,
1465 			    listener->tcp_conn_req_cnt_q,
1466 			    tcp_display(listener, NULL, DISP_PORT_ONLY));
1467 		}
1468 		goto error2;
1469 	}
1470 
1471 	if (listener->tcp_conn_req_cnt_q0 >=
1472 	    listener->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) {
1473 		/*
1474 		 * Q0 is full. Drop a pending half-open req from the queue
1475 		 * to make room for the new SYN req. Also mark the time we
1476 		 * drop a SYN.
1477 		 *
1478 		 * A more aggressive defense against SYN attack will
1479 		 * be to set the "tcp_syn_defense" flag now.
1480 		 */
1481 		TCP_STAT(tcps, tcp_listendropq0);
1482 		listener->tcp_last_rcv_lbolt = ddi_get_lbolt64();
1483 		if (!tcp_drop_q0(listener)) {
1484 			mutex_exit(&listener->tcp_eager_lock);
1485 			TCPS_BUMP_MIB(tcps, tcpListenDropQ0);
1486 			if (lconnp->conn_debug) {
1487 				(void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE,
1488 				    "tcp_input_listener: listen half-open "
1489 				    "queue (max=%d) full (%d pending) on %s",
1490 				    tcps->tcps_conn_req_max_q0,
1491 				    listener->tcp_conn_req_cnt_q0,
1492 				    tcp_display(listener, NULL,
1493 				    DISP_PORT_ONLY));
1494 			}
1495 			goto error2;
1496 		}
1497 	}
1498 
1499 	/*
1500 	 * Enforce the limit set on the number of connections per listener.
1501 	 * Note that tlc_cnt starts with 1.  So need to add 1 to tlc_max
1502 	 * for comparison.
1503 	 */
1504 	if (listener->tcp_listen_cnt != NULL) {
1505 		tcp_listen_cnt_t *tlc = listener->tcp_listen_cnt;
1506 		int64_t now;
1507 
1508 		if (atomic_inc_32_nv(&tlc->tlc_cnt) > tlc->tlc_max + 1) {
1509 			mutex_exit(&listener->tcp_eager_lock);
1510 			now = ddi_get_lbolt64();
1511 			atomic_dec_32(&tlc->tlc_cnt);
1512 			TCP_STAT(tcps, tcp_listen_cnt_drop);
1513 			tlc->tlc_drop++;
1514 			if (now - tlc->tlc_report_time >
1515 			    MSEC_TO_TICK(TCP_TLC_REPORT_INTERVAL)) {
1516 				zcmn_err(lconnp->conn_zoneid, CE_WARN,
1517 				    "Listener (port %d) connection max (%u) "
1518 				    "reached: %u attempts dropped total\n",
1519 				    ntohs(listener->tcp_connp->conn_lport),
1520 				    tlc->tlc_max, tlc->tlc_drop);
1521 				tlc->tlc_report_time = now;
1522 			}
1523 			goto error2;
1524 		}
1525 		tlc_set = B_TRUE;
1526 	}
1527 
1528 	mutex_exit(&listener->tcp_eager_lock);
1529 
1530 	/*
1531 	 * IP sets ira_sqp to either the senders conn_sqp (for loopback)
1532 	 * or based on the ring (for packets from GLD). Otherwise it is
1533 	 * set based on lbolt i.e., a somewhat random number.
1534 	 */
1535 	ASSERT(ira->ira_sqp != NULL);
1536 	new_sqp = ira->ira_sqp;
1537 
1538 	econnp = tcp_get_conn(arg2, tcps);
1539 	if (econnp == NULL)
1540 		goto error2;
1541 
1542 	ASSERT(econnp->conn_netstack == lconnp->conn_netstack);
1543 	econnp->conn_sqp = new_sqp;
1544 	econnp->conn_initial_sqp = new_sqp;
1545 	econnp->conn_ixa->ixa_sqp = new_sqp;
1546 
1547 	econnp->conn_fport = tcpha->tha_lport;
1548 	econnp->conn_lport = tcpha->tha_fport;
1549 
1550 	err = conn_inherit_parent(lconnp, econnp);
1551 	if (err != 0)
1552 		goto error3;
1553 
1554 	/* We already know the laddr of the new connection is ours */
1555 	econnp->conn_ixa->ixa_src_generation = ipst->ips_src_generation;
1556 
1557 	ASSERT(OK_32PTR(mp->b_rptr));
1558 	ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION ||
1559 	    IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
1560 
1561 	if (lconnp->conn_family == AF_INET) {
1562 		ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
1563 		tpi_mp = tcp_conn_create_v4(lconnp, econnp, mp, ira);
1564 	} else {
1565 		tpi_mp = tcp_conn_create_v6(lconnp, econnp, mp, ira);
1566 	}
1567 
1568 	if (tpi_mp == NULL)
1569 		goto error3;
1570 
1571 	eager = econnp->conn_tcp;
1572 	eager->tcp_detached = B_TRUE;
1573 	SOCK_CONNID_INIT(eager->tcp_connid);
1574 
1575 	/*
1576 	 * Initialize the eager's tcp_t and inherit some parameters from
1577 	 * the listener.
1578 	 */
1579 	tcp_init_values(eager, listener);
1580 
1581 	ASSERT((econnp->conn_ixa->ixa_flags &
1582 	    (IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
1583 	    IXAF_VERIFY_PMTU | IXAF_VERIFY_LSO)) ==
1584 	    (IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
1585 	    IXAF_VERIFY_PMTU | IXAF_VERIFY_LSO));
1586 
1587 	if (!tcps->tcps_dev_flow_ctl)
1588 		econnp->conn_ixa->ixa_flags |= IXAF_NO_DEV_FLOW_CTL;
1589 
1590 	/* Prepare for diffing against previous packets */
1591 	eager->tcp_recvifindex = 0;
1592 	eager->tcp_recvhops = 0xffffffffU;
1593 
1594 	if (!(ira->ira_flags & IRAF_IS_IPV4) && econnp->conn_bound_if == 0) {
1595 		if (IN6_IS_ADDR_LINKSCOPE(&econnp->conn_faddr_v6) ||
1596 		    IN6_IS_ADDR_LINKSCOPE(&econnp->conn_laddr_v6)) {
1597 			econnp->conn_incoming_ifindex = ifindex;
1598 			econnp->conn_ixa->ixa_flags |= IXAF_SCOPEID_SET;
1599 			econnp->conn_ixa->ixa_scopeid = ifindex;
1600 		}
1601 	}
1602 
1603 	if ((ira->ira_flags & (IRAF_IS_IPV4|IRAF_IPV4_OPTIONS)) ==
1604 	    (IRAF_IS_IPV4|IRAF_IPV4_OPTIONS) &&
1605 	    tcps->tcps_rev_src_routes) {
1606 		ipha_t *ipha = (ipha_t *)mp->b_rptr;
1607 		ip_pkt_t *ipp = &econnp->conn_xmit_ipp;
1608 
1609 		/* Source routing option copyover (reverse it) */
1610 		err = ip_find_hdr_v4(ipha, ipp, B_TRUE);
1611 		if (err != 0) {
1612 			freemsg(tpi_mp);
1613 			goto error3;
1614 		}
1615 		ip_pkt_source_route_reverse_v4(ipp);
1616 	}
1617 
1618 	ASSERT(eager->tcp_conn.tcp_eager_conn_ind == NULL);
1619 	ASSERT(!eager->tcp_tconnind_started);
1620 	/*
1621 	 * If the SYN came with a credential, it's a loopback packet or a
1622 	 * labeled packet; attach the credential to the TPI message.
1623 	 */
1624 	if (ira->ira_cred != NULL)
1625 		mblk_setcred(tpi_mp, ira->ira_cred, ira->ira_cpid);
1626 
1627 	eager->tcp_conn.tcp_eager_conn_ind = tpi_mp;
1628 	ASSERT(eager->tcp_ordrel_mp == NULL);
1629 
1630 	/* Inherit the listener's non-STREAMS flag */
1631 	if (IPCL_IS_NONSTR(lconnp)) {
1632 		econnp->conn_flags |= IPCL_NONSTR;
1633 		/* All non-STREAMS tcp_ts are sockets */
1634 		eager->tcp_issocket = B_TRUE;
1635 	} else {
1636 		/*
1637 		 * Pre-allocate the T_ordrel_ind mblk for TPI socket so that
1638 		 * at close time, we will always have that to send up.
1639 		 * Otherwise, we need to do special handling in case the
1640 		 * allocation fails at that time.
1641 		 */
1642 		if ((eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL)
1643 			goto error3;
1644 	}
1645 	/*
1646 	 * Now that the IP addresses and ports are setup in econnp we
1647 	 * can do the IPsec policy work.
1648 	 */
1649 	if (ira->ira_flags & IRAF_IPSEC_SECURE) {
1650 		if (lconnp->conn_policy != NULL) {
1651 			/*
1652 			 * Inherit the policy from the listener; use
1653 			 * actions from ira
1654 			 */
1655 			if (!ip_ipsec_policy_inherit(econnp, lconnp, ira)) {
1656 				CONN_DEC_REF(econnp);
1657 				freemsg(mp);
1658 				goto error3;
1659 			}
1660 		}
1661 	}
1662 
1663 	/*
1664 	 * tcp_set_destination() may set tcp_rwnd according to the route
1665 	 * metrics. If it does not, the eager's receive window will be set
1666 	 * to the listener's receive window later in this function.
1667 	 */
1668 	eager->tcp_rwnd = 0;
1669 
1670 	if (is_system_labeled()) {
1671 		ip_xmit_attr_t *ixa = econnp->conn_ixa;
1672 
1673 		ASSERT(ira->ira_tsl != NULL);
1674 		/* Discard any old label */
1675 		if (ixa->ixa_free_flags & IXA_FREE_TSL) {
1676 			ASSERT(ixa->ixa_tsl != NULL);
1677 			label_rele(ixa->ixa_tsl);
1678 			ixa->ixa_free_flags &= ~IXA_FREE_TSL;
1679 			ixa->ixa_tsl = NULL;
1680 		}
1681 		if ((lconnp->conn_mlp_type != mlptSingle ||
1682 		    lconnp->conn_mac_mode != CONN_MAC_DEFAULT) &&
1683 		    ira->ira_tsl != NULL) {
1684 			/*
1685 			 * If this is an MLP connection or a MAC-Exempt
1686 			 * connection with an unlabeled node, packets are to be
1687 			 * exchanged using the security label of the received
1688 			 * SYN packet instead of the server application's label.
1689 			 * tsol_check_dest called from ip_set_destination
1690 			 * might later update TSF_UNLABELED by replacing
1691 			 * ixa_tsl with a new label.
1692 			 */
1693 			label_hold(ira->ira_tsl);
1694 			ip_xmit_attr_replace_tsl(ixa, ira->ira_tsl);
1695 			DTRACE_PROBE2(mlp_syn_accept, conn_t *,
1696 			    econnp, ts_label_t *, ixa->ixa_tsl)
1697 		} else {
1698 			ixa->ixa_tsl = crgetlabel(econnp->conn_cred);
1699 			DTRACE_PROBE2(syn_accept, conn_t *,
1700 			    econnp, ts_label_t *, ixa->ixa_tsl)
1701 		}
1702 		/*
1703 		 * conn_connect() called from tcp_set_destination will verify
1704 		 * the destination is allowed to receive packets at the
1705 		 * security label of the SYN-ACK we are generating. As part of
1706 		 * that, tsol_check_dest() may create a new effective label for
1707 		 * this connection.
1708 		 * Finally conn_connect() will call conn_update_label.
1709 		 * All that remains for TCP to do is to call
1710 		 * conn_build_hdr_template which is done as part of
1711 		 * tcp_set_destination.
1712 		 */
1713 	}
1714 
1715 	/*
1716 	 * Since we will clear tcp_listener before we clear tcp_detached
1717 	 * in the accept code we need tcp_hard_binding aka tcp_accept_inprogress
1718 	 * so we can tell a TCP_IS_DETACHED_NONEAGER apart.
1719 	 */
1720 	eager->tcp_hard_binding = B_TRUE;
1721 
1722 	tcp_bind_hash_insert(&tcps->tcps_bind_fanout[
1723 	    TCP_BIND_HASH(econnp->conn_lport)], eager, 0);
1724 
1725 	CL_INET_CONNECT(econnp, B_FALSE, err);
1726 	if (err != 0) {
1727 		tcp_bind_hash_remove(eager);
1728 		goto error3;
1729 	}
1730 
1731 	SOCK_CONNID_BUMP(eager->tcp_connid);
1732 
1733 	/*
1734 	 * Adapt our mss, ttl, ... based on the remote address.
1735 	 */
1736 
1737 	if (tcp_set_destination(eager) != 0) {
1738 		TCPS_BUMP_MIB(tcps, tcpAttemptFails);
1739 		/* Undo the bind_hash_insert */
1740 		tcp_bind_hash_remove(eager);
1741 		goto error3;
1742 	}
1743 
1744 	/* Process all TCP options. */
1745 	tcp_process_options(eager, tcpha);
1746 
1747 	/* Is the other end ECN capable? */
1748 	if (tcps->tcps_ecn_permitted >= 1 &&
1749 	    (tcpha->tha_flags & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) {
1750 		eager->tcp_ecn_ok = B_TRUE;
1751 	}
1752 
1753 	/*
1754 	 * The listener's conn_rcvbuf should be the default window size or a
1755 	 * window size changed via SO_RCVBUF option. First round up the
1756 	 * eager's tcp_rwnd to the nearest MSS. Then find out the window
1757 	 * scale option value if needed. Call tcp_rwnd_set() to finish the
1758 	 * setting.
1759 	 *
1760 	 * Note if there is a rpipe metric associated with the remote host,
1761 	 * we should not inherit receive window size from listener.
1762 	 */
1763 	eager->tcp_rwnd = MSS_ROUNDUP(
1764 	    (eager->tcp_rwnd == 0 ? econnp->conn_rcvbuf :
1765 	    eager->tcp_rwnd), eager->tcp_mss);
1766 	if (eager->tcp_snd_ws_ok)
1767 		tcp_set_ws_value(eager);
1768 	/*
1769 	 * Note that this is the only place tcp_rwnd_set() is called for
1770 	 * accepting a connection.  We need to call it here instead of
1771 	 * after the 3-way handshake because we need to tell the other
1772 	 * side our rwnd in the SYN-ACK segment.
1773 	 */
1774 	(void) tcp_rwnd_set(eager, eager->tcp_rwnd);
1775 
1776 	ASSERT(eager->tcp_connp->conn_rcvbuf != 0 &&
1777 	    eager->tcp_connp->conn_rcvbuf == eager->tcp_rwnd);
1778 
1779 	ASSERT(econnp->conn_rcvbuf != 0 &&
1780 	    econnp->conn_rcvbuf == eager->tcp_rwnd);
1781 
1782 	/* Put a ref on the listener for the eager. */
1783 	CONN_INC_REF(lconnp);
1784 	mutex_enter(&listener->tcp_eager_lock);
1785 	listener->tcp_eager_next_q0->tcp_eager_prev_q0 = eager;
1786 	eager->tcp_eager_next_q0 = listener->tcp_eager_next_q0;
1787 	listener->tcp_eager_next_q0 = eager;
1788 	eager->tcp_eager_prev_q0 = listener;
1789 
1790 	/* Set tcp_listener before adding it to tcp_conn_fanout */
1791 	eager->tcp_listener = listener;
1792 	eager->tcp_saved_listener = listener;
1793 
1794 	/*
1795 	 * Set tcp_listen_cnt so that when the connection is done, the counter
1796 	 * is decremented.
1797 	 */
1798 	eager->tcp_listen_cnt = listener->tcp_listen_cnt;
1799 
1800 	/*
1801 	 * Tag this detached tcp vector for later retrieval
1802 	 * by our listener client in tcp_accept().
1803 	 */
1804 	eager->tcp_conn_req_seqnum = listener->tcp_conn_req_seqnum;
1805 	listener->tcp_conn_req_cnt_q0++;
1806 	if (++listener->tcp_conn_req_seqnum == -1) {
1807 		/*
1808 		 * -1 is "special" and defined in TPI as something
1809 		 * that should never be used in T_CONN_IND
1810 		 */
1811 		++listener->tcp_conn_req_seqnum;
1812 	}
1813 	mutex_exit(&listener->tcp_eager_lock);
1814 
1815 	if (listener->tcp_syn_defense) {
1816 		/* Don't drop the SYN that comes from a good IP source */
1817 		ipaddr_t *addr_cache;
1818 
1819 		addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache);
1820 		if (addr_cache != NULL && econnp->conn_faddr_v4 ==
1821 		    addr_cache[IP_ADDR_CACHE_HASH(econnp->conn_faddr_v4)]) {
1822 			eager->tcp_dontdrop = B_TRUE;
1823 		}
1824 	}
1825 
1826 	/*
1827 	 * We need to insert the eager in its own perimeter but as soon
1828 	 * as we do that, we expose the eager to the classifier and
1829 	 * should not touch any field outside the eager's perimeter.
1830 	 * So do all the work necessary before inserting the eager
1831 	 * in its own perimeter. Be optimistic that conn_connect()
1832 	 * will succeed but undo everything if it fails.
1833 	 */
1834 	seg_seq = ntohl(tcpha->tha_seq);
1835 	eager->tcp_irs = seg_seq;
1836 	eager->tcp_rack = seg_seq;
1837 	eager->tcp_rnxt = seg_seq + 1;
1838 	eager->tcp_tcpha->tha_ack = htonl(eager->tcp_rnxt);
1839 	TCPS_BUMP_MIB(tcps, tcpPassiveOpens);
1840 	eager->tcp_state = TCPS_SYN_RCVD;
1841 	DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
1842 	    econnp->conn_ixa, void, NULL, tcp_t *, eager, void, NULL,
1843 	    int32_t, TCPS_LISTEN);
1844 
1845 	mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss,
1846 	    NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE);
1847 	if (mp1 == NULL) {
1848 		/*
1849 		 * Increment the ref count as we are going to
1850 		 * enqueueing an mp in squeue
1851 		 */
1852 		CONN_INC_REF(econnp);
1853 		goto error;
1854 	}
1855 
1856 	/*
1857 	 * We need to start the rto timer. In normal case, we start
1858 	 * the timer after sending the packet on the wire (or at
1859 	 * least believing that packet was sent by waiting for
1860 	 * conn_ip_output() to return). Since this is the first packet
1861 	 * being sent on the wire for the eager, our initial tcp_rto
1862 	 * is at least tcp_rexmit_interval_min which is a fairly
1863 	 * large value to allow the algorithm to adjust slowly to large
1864 	 * fluctuations of RTT during first few transmissions.
1865 	 *
1866 	 * Starting the timer first and then sending the packet in this
1867 	 * case shouldn't make much difference since tcp_rexmit_interval_min
1868 	 * is of the order of several 100ms and starting the timer
1869 	 * first and then sending the packet will result in difference
1870 	 * of few micro seconds.
1871 	 *
1872 	 * Without this optimization, we are forced to hold the fanout
1873 	 * lock across the ipcl_bind_insert() and sending the packet
1874 	 * so that we don't race against an incoming packet (maybe RST)
1875 	 * for this eager.
1876 	 *
1877 	 * It is necessary to acquire an extra reference on the eager
1878 	 * at this point and hold it until after tcp_send_data() to
1879 	 * ensure against an eager close race.
1880 	 */
1881 
1882 	CONN_INC_REF(econnp);
1883 
1884 	TCP_TIMER_RESTART(eager, eager->tcp_rto);
1885 
1886 	/*
1887 	 * Insert the eager in its own perimeter now. We are ready to deal
1888 	 * with any packets on eager.
1889 	 */
1890 	if (ipcl_conn_insert(econnp) != 0)
1891 		goto error;
1892 
1893 	ASSERT(econnp->conn_ixa->ixa_notify_cookie == econnp->conn_tcp);
1894 	freemsg(mp);
1895 	/*
1896 	 * Send the SYN-ACK. Use the right squeue so that conn_ixa is
1897 	 * only used by one thread at a time.
1898 	 */
1899 	if (econnp->conn_sqp == lconnp->conn_sqp) {
1900 		DTRACE_TCP5(send, mblk_t *, NULL, ip_xmit_attr_t *,
1901 		    econnp->conn_ixa, __dtrace_tcp_void_ip_t *, mp1->b_rptr,
1902 		    tcp_t *, eager, __dtrace_tcp_tcph_t *,
1903 		    &mp1->b_rptr[econnp->conn_ixa->ixa_ip_hdr_length]);
1904 		(void) conn_ip_output(mp1, econnp->conn_ixa);
1905 		CONN_DEC_REF(econnp);
1906 	} else {
1907 		SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_send_synack,
1908 		    econnp, NULL, SQ_PROCESS, SQTAG_TCP_SEND_SYNACK);
1909 	}
1910 	return;
1911 error:
1912 	freemsg(mp1);
1913 	eager->tcp_closemp_used = B_TRUE;
1914 	TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15);
1915 	mp1 = &eager->tcp_closemp;
1916 	SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_eager_kill,
1917 	    econnp, NULL, SQ_FILL, SQTAG_TCP_CONN_REQ_2);
1918 
1919 	/*
1920 	 * If a connection already exists, send the mp to that connections so
1921 	 * that it can be appropriately dealt with.
1922 	 */
1923 	ipst = tcps->tcps_netstack->netstack_ip;
1924 
1925 	if ((econnp = ipcl_classify(mp, ira, ipst)) != NULL) {
1926 		if (!IPCL_IS_CONNECTED(econnp)) {
1927 			/*
1928 			 * Something bad happened. ipcl_conn_insert()
1929 			 * failed because a connection already existed
1930 			 * in connected hash but we can't find it
1931 			 * anymore (someone blew it away). Just
1932 			 * free this message and hopefully remote
1933 			 * will retransmit at which time the SYN can be
1934 			 * treated as a new connection or dealth with
1935 			 * a TH_RST if a connection already exists.
1936 			 */
1937 			CONN_DEC_REF(econnp);
1938 			freemsg(mp);
1939 		} else {
1940 			SQUEUE_ENTER_ONE(econnp->conn_sqp, mp, tcp_input_data,
1941 			    econnp, ira, SQ_FILL, SQTAG_TCP_CONN_REQ_1);
1942 		}
1943 	} else {
1944 		/* Nobody wants this packet */
1945 		freemsg(mp);
1946 	}
1947 	return;
1948 error3:
1949 	CONN_DEC_REF(econnp);
1950 error2:
1951 	freemsg(mp);
1952 	if (tlc_set)
1953 		atomic_dec_32(&listener->tcp_listen_cnt->tlc_cnt);
1954 }
1955 
1956 /*
1957  * In an ideal case of vertical partition in NUMA architecture, its
1958  * beneficial to have the listener and all the incoming connections
1959  * tied to the same squeue. The other constraint is that incoming
1960  * connections should be tied to the squeue attached to interrupted
1961  * CPU for obvious locality reason so this leaves the listener to
1962  * be tied to the same squeue. Our only problem is that when listener
1963  * is binding, the CPU that will get interrupted by the NIC whose
1964  * IP address the listener is binding to is not even known. So
1965  * the code below allows us to change that binding at the time the
1966  * CPU is interrupted by virtue of incoming connection's squeue.
1967  *
1968  * This is usefull only in case of a listener bound to a specific IP
1969  * address. For other kind of listeners, they get bound the
1970  * very first time and there is no attempt to rebind them.
1971  */
1972 void
1973 tcp_input_listener_unbound(void *arg, mblk_t *mp, void *arg2,
1974     ip_recv_attr_t *ira)
1975 {
1976 	conn_t		*connp = (conn_t *)arg;
1977 	squeue_t	*sqp = (squeue_t *)arg2;
1978 	squeue_t	*new_sqp;
1979 	uint32_t	conn_flags;
1980 
1981 	/*
1982 	 * IP sets ira_sqp to either the senders conn_sqp (for loopback)
1983 	 * or based on the ring (for packets from GLD). Otherwise it is
1984 	 * set based on lbolt i.e., a somewhat random number.
1985 	 */
1986 	ASSERT(ira->ira_sqp != NULL);
1987 	new_sqp = ira->ira_sqp;
1988 
1989 	if (connp->conn_fanout == NULL)
1990 		goto done;
1991 
1992 	if (!(connp->conn_flags & IPCL_FULLY_BOUND)) {
1993 		mutex_enter(&connp->conn_fanout->connf_lock);
1994 		mutex_enter(&connp->conn_lock);
1995 		/*
1996 		 * No one from read or write side can access us now
1997 		 * except for already queued packets on this squeue.
1998 		 * But since we haven't changed the squeue yet, they
1999 		 * can't execute. If they are processed after we have
2000 		 * changed the squeue, they are sent back to the
2001 		 * correct squeue down below.
2002 		 * But a listner close can race with processing of
2003 		 * incoming SYN. If incoming SYN processing changes
2004 		 * the squeue then the listener close which is waiting
2005 		 * to enter the squeue would operate on the wrong
2006 		 * squeue. Hence we don't change the squeue here unless
2007 		 * the refcount is exactly the minimum refcount. The
2008 		 * minimum refcount of 4 is counted as - 1 each for
2009 		 * TCP and IP, 1 for being in the classifier hash, and
2010 		 * 1 for the mblk being processed.
2011 		 */
2012 
2013 		if (connp->conn_ref != 4 ||
2014 		    connp->conn_tcp->tcp_state != TCPS_LISTEN) {
2015 			mutex_exit(&connp->conn_lock);
2016 			mutex_exit(&connp->conn_fanout->connf_lock);
2017 			goto done;
2018 		}
2019 		if (connp->conn_sqp != new_sqp) {
2020 			while (connp->conn_sqp != new_sqp)
2021 				(void) atomic_cas_ptr(&connp->conn_sqp, sqp,
2022 				    new_sqp);
2023 			/* No special MT issues for outbound ixa_sqp hint */
2024 			connp->conn_ixa->ixa_sqp = new_sqp;
2025 		}
2026 
2027 		do {
2028 			conn_flags = connp->conn_flags;
2029 			conn_flags |= IPCL_FULLY_BOUND;
2030 			(void) atomic_cas_32(&connp->conn_flags,
2031 			    connp->conn_flags, conn_flags);
2032 		} while (!(connp->conn_flags & IPCL_FULLY_BOUND));
2033 
2034 		mutex_exit(&connp->conn_fanout->connf_lock);
2035 		mutex_exit(&connp->conn_lock);
2036 
2037 		/*
2038 		 * Assume we have picked a good squeue for the listener. Make
2039 		 * subsequent SYNs not try to change the squeue.
2040 		 */
2041 		connp->conn_recv = tcp_input_listener;
2042 	}
2043 
2044 done:
2045 	if (connp->conn_sqp != sqp) {
2046 		CONN_INC_REF(connp);
2047 		SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp,
2048 		    ira, SQ_FILL, SQTAG_TCP_CONN_REQ_UNBOUND);
2049 	} else {
2050 		tcp_input_listener(connp, mp, sqp, ira);
2051 	}
2052 }
2053 
2054 /*
2055  * Send up all messages queued on tcp_rcv_list.
2056  */
2057 uint_t
2058 tcp_rcv_drain(tcp_t *tcp)
2059 {
2060 	mblk_t *mp;
2061 	uint_t ret = 0;
2062 #ifdef DEBUG
2063 	uint_t cnt = 0;
2064 #endif
2065 	queue_t	*q = tcp->tcp_connp->conn_rq;
2066 
2067 	/* Can't drain on an eager connection */
2068 	if (tcp->tcp_listener != NULL)
2069 		return (ret);
2070 
2071 	/* Can't be a non-STREAMS connection */
2072 	ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
2073 
2074 	/* No need for the push timer now. */
2075 	if (tcp->tcp_push_tid != 0) {
2076 		(void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid);
2077 		tcp->tcp_push_tid = 0;
2078 	}
2079 
2080 	/*
2081 	 * Handle two cases here: we are currently fused or we were
2082 	 * previously fused and have some urgent data to be delivered
2083 	 * upstream.  The latter happens because we either ran out of
2084 	 * memory or were detached and therefore sending the SIGURG was
2085 	 * deferred until this point.  In either case we pass control
2086 	 * over to tcp_fuse_rcv_drain() since it may need to complete
2087 	 * some work.
2088 	 */
2089 	if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) {
2090 		if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL :
2091 		    &tcp->tcp_fused_sigurg_mp))
2092 			return (ret);
2093 	}
2094 
2095 	while ((mp = tcp->tcp_rcv_list) != NULL) {
2096 		tcp->tcp_rcv_list = mp->b_next;
2097 		mp->b_next = NULL;
2098 #ifdef DEBUG
2099 		cnt += msgdsize(mp);
2100 #endif
2101 		putnext(q, mp);
2102 	}
2103 #ifdef DEBUG
2104 	ASSERT(cnt == tcp->tcp_rcv_cnt);
2105 #endif
2106 	tcp->tcp_rcv_last_head = NULL;
2107 	tcp->tcp_rcv_last_tail = NULL;
2108 	tcp->tcp_rcv_cnt = 0;
2109 
2110 	if (canputnext(q))
2111 		return (tcp_rwnd_reopen(tcp));
2112 
2113 	return (ret);
2114 }
2115 
2116 /*
2117  * Queue data on tcp_rcv_list which is a b_next chain.
2118  * tcp_rcv_last_head/tail is the last element of this chain.
2119  * Each element of the chain is a b_cont chain.
2120  *
2121  * M_DATA messages are added to the current element.
2122  * Other messages are added as new (b_next) elements.
2123  */
2124 void
2125 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len, cred_t *cr)
2126 {
2127 	ASSERT(seg_len == msgdsize(mp));
2128 	ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL);
2129 
2130 	if (is_system_labeled()) {
2131 		ASSERT(cr != NULL || msg_getcred(mp, NULL) != NULL);
2132 		/*
2133 		 * Provide for protocols above TCP such as RPC. NOPID leaves
2134 		 * db_cpid unchanged.
2135 		 * The cred could have already been set.
2136 		 */
2137 		if (cr != NULL)
2138 			mblk_setcred(mp, cr, NOPID);
2139 	}
2140 
2141 	if (tcp->tcp_rcv_list == NULL) {
2142 		ASSERT(tcp->tcp_rcv_last_head == NULL);
2143 		tcp->tcp_rcv_list = mp;
2144 		tcp->tcp_rcv_last_head = mp;
2145 	} else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) {
2146 		tcp->tcp_rcv_last_tail->b_cont = mp;
2147 	} else {
2148 		tcp->tcp_rcv_last_head->b_next = mp;
2149 		tcp->tcp_rcv_last_head = mp;
2150 	}
2151 
2152 	while (mp->b_cont)
2153 		mp = mp->b_cont;
2154 
2155 	tcp->tcp_rcv_last_tail = mp;
2156 	tcp->tcp_rcv_cnt += seg_len;
2157 	tcp->tcp_rwnd -= seg_len;
2158 }
2159 
2160 /* Generate an ACK-only (no data) segment for a TCP endpoint */
2161 mblk_t *
2162 tcp_ack_mp(tcp_t *tcp)
2163 {
2164 	uint32_t	seq_no;
2165 	tcp_stack_t	*tcps = tcp->tcp_tcps;
2166 	conn_t		*connp = tcp->tcp_connp;
2167 
2168 	/*
2169 	 * There are a few cases to be considered while setting the sequence no.
2170 	 * Essentially, we can come here while processing an unacceptable pkt
2171 	 * in the TCPS_SYN_RCVD state, in which case we set the sequence number
2172 	 * to snxt (per RFC 793), note the swnd wouldn't have been set yet.
2173 	 * If we are here for a zero window probe, stick with suna. In all
2174 	 * other cases, we check if suna + swnd encompasses snxt and set
2175 	 * the sequence number to snxt, if so. If snxt falls outside the
2176 	 * window (the receiver probably shrunk its window), we will go with
2177 	 * suna + swnd, otherwise the sequence no will be unacceptable to the
2178 	 * receiver.
2179 	 */
2180 	if (tcp->tcp_zero_win_probe) {
2181 		seq_no = tcp->tcp_suna;
2182 	} else if (tcp->tcp_state == TCPS_SYN_RCVD) {
2183 		ASSERT(tcp->tcp_swnd == 0);
2184 		seq_no = tcp->tcp_snxt;
2185 	} else {
2186 		seq_no = SEQ_GT(tcp->tcp_snxt,
2187 		    (tcp->tcp_suna + tcp->tcp_swnd)) ?
2188 		    (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt;
2189 	}
2190 
2191 	if (tcp->tcp_valid_bits) {
2192 		/*
2193 		 * For the complex case where we have to send some
2194 		 * controls (FIN or SYN), let tcp_xmit_mp do it.
2195 		 */
2196 		return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE,
2197 		    NULL, B_FALSE));
2198 	} else {
2199 		/* Generate a simple ACK */
2200 		int	data_length;
2201 		uchar_t	*rptr;
2202 		tcpha_t	*tcpha;
2203 		mblk_t	*mp1;
2204 		int32_t	total_hdr_len;
2205 		int32_t	tcp_hdr_len;
2206 		int32_t	num_sack_blk = 0;
2207 		int32_t sack_opt_len;
2208 		ip_xmit_attr_t *ixa = connp->conn_ixa;
2209 
2210 		/*
2211 		 * Allocate space for TCP + IP headers
2212 		 * and link-level header
2213 		 */
2214 		if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
2215 			num_sack_blk = MIN(tcp->tcp_max_sack_blk,
2216 			    tcp->tcp_num_sack_blk);
2217 			sack_opt_len = num_sack_blk * sizeof (sack_blk_t) +
2218 			    TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN;
2219 			total_hdr_len = connp->conn_ht_iphc_len + sack_opt_len;
2220 			tcp_hdr_len = connp->conn_ht_ulp_len + sack_opt_len;
2221 		} else {
2222 			total_hdr_len = connp->conn_ht_iphc_len;
2223 			tcp_hdr_len = connp->conn_ht_ulp_len;
2224 		}
2225 		mp1 = allocb(total_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED);
2226 		if (!mp1)
2227 			return (NULL);
2228 
2229 		/* Update the latest receive window size in TCP header. */
2230 		tcp->tcp_tcpha->tha_win =
2231 		    htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
2232 		/* copy in prototype TCP + IP header */
2233 		rptr = mp1->b_rptr + tcps->tcps_wroff_xtra;
2234 		mp1->b_rptr = rptr;
2235 		mp1->b_wptr = rptr + total_hdr_len;
2236 		bcopy(connp->conn_ht_iphc, rptr, connp->conn_ht_iphc_len);
2237 
2238 		tcpha = (tcpha_t *)&rptr[ixa->ixa_ip_hdr_length];
2239 
2240 		/* Set the TCP sequence number. */
2241 		tcpha->tha_seq = htonl(seq_no);
2242 
2243 		/* Set up the TCP flag field. */
2244 		tcpha->tha_flags = (uchar_t)TH_ACK;
2245 		if (tcp->tcp_ecn_echo_on)
2246 			tcpha->tha_flags |= TH_ECE;
2247 
2248 		tcp->tcp_rack = tcp->tcp_rnxt;
2249 		tcp->tcp_rack_cnt = 0;
2250 
2251 		/* fill in timestamp option if in use */
2252 		if (tcp->tcp_snd_ts_ok) {
2253 			uint32_t llbolt = (uint32_t)LBOLT_FASTPATH;
2254 
2255 			U32_TO_BE32(llbolt,
2256 			    (char *)tcpha + TCP_MIN_HEADER_LENGTH+4);
2257 			U32_TO_BE32(tcp->tcp_ts_recent,
2258 			    (char *)tcpha + TCP_MIN_HEADER_LENGTH+8);
2259 		}
2260 
2261 		/* Fill in SACK options */
2262 		if (num_sack_blk > 0) {
2263 			uchar_t *wptr = (uchar_t *)tcpha +
2264 			    connp->conn_ht_ulp_len;
2265 			sack_blk_t *tmp;
2266 			int32_t	i;
2267 
2268 			wptr[0] = TCPOPT_NOP;
2269 			wptr[1] = TCPOPT_NOP;
2270 			wptr[2] = TCPOPT_SACK;
2271 			wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
2272 			    sizeof (sack_blk_t);
2273 			wptr += TCPOPT_REAL_SACK_LEN;
2274 
2275 			tmp = tcp->tcp_sack_list;
2276 			for (i = 0; i < num_sack_blk; i++) {
2277 				U32_TO_BE32(tmp[i].begin, wptr);
2278 				wptr += sizeof (tcp_seq);
2279 				U32_TO_BE32(tmp[i].end, wptr);
2280 				wptr += sizeof (tcp_seq);
2281 			}
2282 			tcpha->tha_offset_and_reserved +=
2283 			    ((num_sack_blk * 2 + 1) << 4);
2284 		}
2285 
2286 		ixa->ixa_pktlen = total_hdr_len;
2287 
2288 		if (ixa->ixa_flags & IXAF_IS_IPV4) {
2289 			((ipha_t *)rptr)->ipha_length = htons(total_hdr_len);
2290 		} else {
2291 			ip6_t *ip6 = (ip6_t *)rptr;
2292 
2293 			ip6->ip6_plen = htons(total_hdr_len - IPV6_HDR_LEN);
2294 		}
2295 
2296 		/*
2297 		 * Prime pump for checksum calculation in IP.  Include the
2298 		 * adjustment for a source route if any.
2299 		 */
2300 		data_length = tcp_hdr_len + connp->conn_sum;
2301 		data_length = (data_length >> 16) + (data_length & 0xFFFF);
2302 		tcpha->tha_sum = htons(data_length);
2303 
2304 		if (tcp->tcp_ip_forward_progress) {
2305 			tcp->tcp_ip_forward_progress = B_FALSE;
2306 			connp->conn_ixa->ixa_flags |= IXAF_REACH_CONF;
2307 		} else {
2308 			connp->conn_ixa->ixa_flags &= ~IXAF_REACH_CONF;
2309 		}
2310 		return (mp1);
2311 	}
2312 }
2313 
2314 /*
2315  * Dummy socket upcalls for if/when the conn_t gets detached from a
2316  * direct-callback sonode via a user-driven close().  Easy to catch with
2317  * DTrace FBT, and should be mostly harmless.
2318  */
2319 
2320 /* ARGSUSED */
2321 static sock_upper_handle_t
2322 tcp_dummy_newconn(sock_upper_handle_t x, sock_lower_handle_t y,
2323     sock_downcalls_t *z, cred_t *cr, pid_t pid, sock_upcalls_t **ignored)
2324 {
2325 	ASSERT(0);	/* Panic in debug, otherwise ignore. */
2326 	return (NULL);
2327 }
2328 
2329 /* ARGSUSED */
2330 static void
2331 tcp_dummy_connected(sock_upper_handle_t x, sock_connid_t y, cred_t *cr,
2332     pid_t pid)
2333 {
2334 	ASSERT(x == NULL);
2335 	/* Normally we'd crhold(cr) and attach it to socket state. */
2336 	/* LINTED */
2337 }
2338 
2339 /* ARGSUSED */
2340 static int
2341 tcp_dummy_disconnected(sock_upper_handle_t x, sock_connid_t y, int blah)
2342 {
2343 	ASSERT(0);	/* Panic in debug, otherwise ignore. */
2344 	return (-1);
2345 }
2346 
2347 /* ARGSUSED */
2348 static void
2349 tcp_dummy_opctl(sock_upper_handle_t x, sock_opctl_action_t y, uintptr_t blah)
2350 {
2351 	ASSERT(x == NULL);
2352 	/* We really want this one to be a harmless NOP for now. */
2353 	/* LINTED */
2354 }
2355 
2356 /* ARGSUSED */
2357 static ssize_t
2358 tcp_dummy_recv(sock_upper_handle_t x, mblk_t *mp, size_t len, int flags,
2359     int *error, boolean_t *push)
2360 {
2361 	ASSERT(x == NULL);
2362 
2363 	/*
2364 	 * Consume the message, set ESHUTDOWN, and return an error.
2365 	 * Nobody's home!
2366 	 */
2367 	freemsg(mp);
2368 	*error = ESHUTDOWN;
2369 	return (-1);
2370 }
2371 
2372 /* ARGSUSED */
2373 static void
2374 tcp_dummy_set_proto_props(sock_upper_handle_t x, struct sock_proto_props *y)
2375 {
2376 	ASSERT(0);	/* Panic in debug, otherwise ignore. */
2377 }
2378 
2379 /* ARGSUSED */
2380 static void
2381 tcp_dummy_txq_full(sock_upper_handle_t x, boolean_t y)
2382 {
2383 	ASSERT(0);	/* Panic in debug, otherwise ignore. */
2384 }
2385 
2386 /* ARGSUSED */
2387 static void
2388 tcp_dummy_signal_oob(sock_upper_handle_t x, ssize_t len)
2389 {
2390 	ASSERT(x == NULL);
2391 	/* Otherwise, this would signal socket state about OOB data. */
2392 }
2393 
2394 /* ARGSUSED */
2395 static void
2396 tcp_dummy_set_error(sock_upper_handle_t x, int err)
2397 {
2398 	ASSERT(0);	/* Panic in debug, otherwise ignore. */
2399 }
2400 
2401 /* ARGSUSED */
2402 static void
2403 tcp_dummy_onearg(sock_upper_handle_t x)
2404 {
2405 	ASSERT(0);	/* Panic in debug, otherwise ignore. */
2406 }
2407 
2408 static sock_upcalls_t tcp_dummy_upcalls = {
2409 	tcp_dummy_newconn,
2410 	tcp_dummy_connected,
2411 	tcp_dummy_disconnected,
2412 	tcp_dummy_opctl,
2413 	tcp_dummy_recv,
2414 	tcp_dummy_set_proto_props,
2415 	tcp_dummy_txq_full,
2416 	tcp_dummy_signal_oob,
2417 	tcp_dummy_onearg,
2418 	tcp_dummy_set_error,
2419 	tcp_dummy_onearg
2420 };
2421 
2422 /*
2423  * Handle M_DATA messages from IP. Its called directly from IP via
2424  * squeue for received IP packets.
2425  *
2426  * The first argument is always the connp/tcp to which the mp belongs.
2427  * There are no exceptions to this rule. The caller has already put
2428  * a reference on this connp/tcp and once tcp_input_data() returns,
2429  * the squeue will do the refrele.
2430  *
2431  * The TH_SYN for the listener directly go to tcp_input_listener via
2432  * squeue. ICMP errors go directly to tcp_icmp_input().
2433  *
2434  * sqp: NULL = recursive, sqp != NULL means called from squeue
2435  */
2436 void
2437 tcp_input_data(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
2438 {
2439 	int32_t		bytes_acked;
2440 	int32_t		gap;
2441 	mblk_t		*mp1;
2442 	uint_t		flags;
2443 	uint32_t	new_swnd = 0;
2444 	uchar_t		*iphdr;
2445 	uchar_t		*rptr;
2446 	int32_t		rgap;
2447 	uint32_t	seg_ack;
2448 	int		seg_len;
2449 	uint_t		ip_hdr_len;
2450 	uint32_t	seg_seq;
2451 	tcpha_t		*tcpha;
2452 	int		urp;
2453 	tcp_opt_t	tcpopt;
2454 	ip_pkt_t	ipp;
2455 	boolean_t	ofo_seg = B_FALSE; /* Out of order segment */
2456 	uint32_t	cwnd;
2457 	int		mss;
2458 	conn_t		*connp = (conn_t *)arg;
2459 	squeue_t	*sqp = (squeue_t *)arg2;
2460 	tcp_t		*tcp = connp->conn_tcp;
2461 	tcp_stack_t	*tcps = tcp->tcp_tcps;
2462 	sock_upcalls_t	*sockupcalls;
2463 
2464 	/*
2465 	 * RST from fused tcp loopback peer should trigger an unfuse.
2466 	 */
2467 	if (tcp->tcp_fused) {
2468 		TCP_STAT(tcps, tcp_fusion_aborted);
2469 		tcp_unfuse(tcp);
2470 	}
2471 
2472 	iphdr = mp->b_rptr;
2473 	rptr = mp->b_rptr;
2474 	ASSERT(OK_32PTR(rptr));
2475 
2476 	ip_hdr_len = ira->ira_ip_hdr_length;
2477 	if (connp->conn_recv_ancillary.crb_all != 0) {
2478 		/*
2479 		 * Record packet information in the ip_pkt_t
2480 		 */
2481 		ipp.ipp_fields = 0;
2482 		if (ira->ira_flags & IRAF_IS_IPV4) {
2483 			(void) ip_find_hdr_v4((ipha_t *)rptr, &ipp,
2484 			    B_FALSE);
2485 		} else {
2486 			uint8_t nexthdrp;
2487 
2488 			/*
2489 			 * IPv6 packets can only be received by applications
2490 			 * that are prepared to receive IPv6 addresses.
2491 			 * The IP fanout must ensure this.
2492 			 */
2493 			ASSERT(connp->conn_family == AF_INET6);
2494 
2495 			(void) ip_find_hdr_v6(mp, (ip6_t *)rptr, B_TRUE, &ipp,
2496 			    &nexthdrp);
2497 			ASSERT(nexthdrp == IPPROTO_TCP);
2498 
2499 			/* Could have caused a pullup? */
2500 			iphdr = mp->b_rptr;
2501 			rptr = mp->b_rptr;
2502 		}
2503 	}
2504 	ASSERT(DB_TYPE(mp) == M_DATA);
2505 	ASSERT(mp->b_next == NULL);
2506 
2507 	tcpha = (tcpha_t *)&rptr[ip_hdr_len];
2508 	seg_seq = ntohl(tcpha->tha_seq);
2509 	seg_ack = ntohl(tcpha->tha_ack);
2510 	ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX);
2511 	seg_len = (int)(mp->b_wptr - rptr) -
2512 	    (ip_hdr_len + TCP_HDR_LENGTH(tcpha));
2513 	if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) {
2514 		do {
2515 			ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <=
2516 			    (uintptr_t)INT_MAX);
2517 			seg_len += (int)(mp1->b_wptr - mp1->b_rptr);
2518 		} while ((mp1 = mp1->b_cont) != NULL &&
2519 		    mp1->b_datap->db_type == M_DATA);
2520 	}
2521 
2522 	DTRACE_TCP5(receive, mblk_t *, NULL, ip_xmit_attr_t *, connp->conn_ixa,
2523 	    __dtrace_tcp_void_ip_t *, iphdr, tcp_t *, tcp,
2524 	    __dtrace_tcp_tcph_t *, tcpha);
2525 
2526 	if (tcp->tcp_state == TCPS_TIME_WAIT) {
2527 		tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack,
2528 		    seg_len, tcpha, ira);
2529 		return;
2530 	}
2531 
2532 	if (sqp != NULL) {
2533 		/*
2534 		 * This is the correct place to update tcp_last_recv_time. Note
2535 		 * that it is also updated for tcp structure that belongs to
2536 		 * global and listener queues which do not really need updating.
2537 		 * But that should not cause any harm.  And it is updated for
2538 		 * all kinds of incoming segments, not only for data segments.
2539 		 */
2540 		tcp->tcp_last_recv_time = LBOLT_FASTPATH;
2541 	}
2542 
2543 	flags = (unsigned int)tcpha->tha_flags & 0xFF;
2544 
2545 	TCPS_BUMP_MIB(tcps, tcpHCInSegs);
2546 	DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp);
2547 
2548 	if ((flags & TH_URG) && sqp != NULL) {
2549 		/*
2550 		 * TCP can't handle urgent pointers that arrive before
2551 		 * the connection has been accept()ed since it can't
2552 		 * buffer OOB data.  Discard segment if this happens.
2553 		 *
2554 		 * We can't just rely on a non-null tcp_listener to indicate
2555 		 * that the accept() has completed since unlinking of the
2556 		 * eager and completion of the accept are not atomic.
2557 		 * tcp_detached, when it is not set (B_FALSE) indicates
2558 		 * that the accept() has completed.
2559 		 *
2560 		 * Nor can it reassemble urgent pointers, so discard
2561 		 * if it's not the next segment expected.
2562 		 *
2563 		 * Otherwise, collapse chain into one mblk (discard if
2564 		 * that fails).  This makes sure the headers, retransmitted
2565 		 * data, and new data all are in the same mblk.
2566 		 */
2567 		ASSERT(mp != NULL);
2568 		if (tcp->tcp_detached || !pullupmsg(mp, -1)) {
2569 			freemsg(mp);
2570 			return;
2571 		}
2572 		/* Update pointers into message */
2573 		iphdr = rptr = mp->b_rptr;
2574 		tcpha = (tcpha_t *)&rptr[ip_hdr_len];
2575 		if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) {
2576 			/*
2577 			 * Since we can't handle any data with this urgent
2578 			 * pointer that is out of sequence, we expunge
2579 			 * the data.  This allows us to still register
2580 			 * the urgent mark and generate the M_PCSIG,
2581 			 * which we can do.
2582 			 */
2583 			mp->b_wptr = (uchar_t *)tcpha + TCP_HDR_LENGTH(tcpha);
2584 			seg_len = 0;
2585 		}
2586 	}
2587 
2588 	sockupcalls = connp->conn_upcalls;
2589 	/* A conn_t may have belonged to a now-closed socket.  Be careful. */
2590 	if (sockupcalls == NULL)
2591 		sockupcalls = &tcp_dummy_upcalls;
2592 
2593 	switch (tcp->tcp_state) {
2594 	case TCPS_SYN_SENT:
2595 		if (connp->conn_final_sqp == NULL &&
2596 		    tcp_outbound_squeue_switch && sqp != NULL) {
2597 			ASSERT(connp->conn_initial_sqp == connp->conn_sqp);
2598 			connp->conn_final_sqp = sqp;
2599 			if (connp->conn_final_sqp != connp->conn_sqp) {
2600 				DTRACE_PROBE1(conn__final__sqp__switch,
2601 				    conn_t *, connp);
2602 				CONN_INC_REF(connp);
2603 				SQUEUE_SWITCH(connp, connp->conn_final_sqp);
2604 				SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
2605 				    tcp_input_data, connp, ira, ip_squeue_flag,
2606 				    SQTAG_CONNECT_FINISH);
2607 				return;
2608 			}
2609 			DTRACE_PROBE1(conn__final__sqp__same, conn_t *, connp);
2610 		}
2611 		if (flags & TH_ACK) {
2612 			/*
2613 			 * Note that our stack cannot send data before a
2614 			 * connection is established, therefore the
2615 			 * following check is valid.  Otherwise, it has
2616 			 * to be changed.
2617 			 */
2618 			if (SEQ_LEQ(seg_ack, tcp->tcp_iss) ||
2619 			    SEQ_GT(seg_ack, tcp->tcp_snxt)) {
2620 				freemsg(mp);
2621 				if (flags & TH_RST)
2622 					return;
2623 				tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq",
2624 				    tcp, seg_ack, 0, TH_RST);
2625 				return;
2626 			}
2627 			ASSERT(tcp->tcp_suna + 1 == seg_ack);
2628 		}
2629 		if (flags & TH_RST) {
2630 			if (flags & TH_ACK) {
2631 				DTRACE_TCP5(connect__refused, mblk_t *, NULL,
2632 				    ip_xmit_attr_t *, connp->conn_ixa,
2633 				    void_ip_t *, iphdr, tcp_t *, tcp,
2634 				    tcph_t *, tcpha);
2635 				(void) tcp_clean_death(tcp, ECONNREFUSED);
2636 			}
2637 			freemsg(mp);
2638 			return;
2639 		}
2640 		if (!(flags & TH_SYN)) {
2641 			freemsg(mp);
2642 			return;
2643 		}
2644 
2645 		/* Process all TCP options. */
2646 		tcp_process_options(tcp, tcpha);
2647 		/*
2648 		 * The following changes our rwnd to be a multiple of the
2649 		 * MIN(peer MSS, our MSS) for performance reason.
2650 		 */
2651 		(void) tcp_rwnd_set(tcp, MSS_ROUNDUP(connp->conn_rcvbuf,
2652 		    tcp->tcp_mss));
2653 
2654 		/* Is the other end ECN capable? */
2655 		if (tcp->tcp_ecn_ok) {
2656 			if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) {
2657 				tcp->tcp_ecn_ok = B_FALSE;
2658 			}
2659 		}
2660 		/*
2661 		 * Clear ECN flags because it may interfere with later
2662 		 * processing.
2663 		 */
2664 		flags &= ~(TH_ECE|TH_CWR);
2665 
2666 		tcp->tcp_irs = seg_seq;
2667 		tcp->tcp_rack = seg_seq;
2668 		tcp->tcp_rnxt = seg_seq + 1;
2669 		tcp->tcp_tcpha->tha_ack = htonl(tcp->tcp_rnxt);
2670 		if (!TCP_IS_DETACHED(tcp)) {
2671 			/* Allocate room for SACK options if needed. */
2672 			connp->conn_wroff = connp->conn_ht_iphc_len;
2673 			if (tcp->tcp_snd_sack_ok)
2674 				connp->conn_wroff += TCPOPT_MAX_SACK_LEN;
2675 			if (!tcp->tcp_loopback)
2676 				connp->conn_wroff += tcps->tcps_wroff_xtra;
2677 
2678 			(void) proto_set_tx_wroff(connp->conn_rq, connp,
2679 			    connp->conn_wroff);
2680 		}
2681 		if (flags & TH_ACK) {
2682 			/*
2683 			 * If we can't get the confirmation upstream, pretend
2684 			 * we didn't even see this one.
2685 			 *
2686 			 * XXX: how can we pretend we didn't see it if we
2687 			 * have updated rnxt et. al.
2688 			 *
2689 			 * For loopback we defer sending up the T_CONN_CON
2690 			 * until after some checks below.
2691 			 */
2692 			mp1 = NULL;
2693 			/*
2694 			 * tcp_sendmsg() checks tcp_state without entering
2695 			 * the squeue so tcp_state should be updated before
2696 			 * sending up connection confirmation.  Probe the
2697 			 * state change below when we are sure the connection
2698 			 * confirmation has been sent.
2699 			 */
2700 			tcp->tcp_state = TCPS_ESTABLISHED;
2701 			if (!tcp_conn_con(tcp, iphdr, mp,
2702 			    tcp->tcp_loopback ? &mp1 : NULL, ira)) {
2703 				tcp->tcp_state = TCPS_SYN_SENT;
2704 				freemsg(mp);
2705 				return;
2706 			}
2707 			TCPS_CONN_INC(tcps);
2708 			/* SYN was acked - making progress */
2709 			tcp->tcp_ip_forward_progress = B_TRUE;
2710 
2711 			/* One for the SYN */
2712 			tcp->tcp_suna = tcp->tcp_iss + 1;
2713 			tcp->tcp_valid_bits &= ~TCP_ISS_VALID;
2714 
2715 			/*
2716 			 * If SYN was retransmitted, need to reset all
2717 			 * retransmission info.  This is because this
2718 			 * segment will be treated as a dup ACK.
2719 			 */
2720 			if (tcp->tcp_rexmit) {
2721 				tcp->tcp_rexmit = B_FALSE;
2722 				tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
2723 				tcp->tcp_rexmit_max = tcp->tcp_snxt;
2724 				tcp->tcp_ms_we_have_waited = 0;
2725 
2726 				/*
2727 				 * Set tcp_cwnd back to 1 MSS, per
2728 				 * recommendation from
2729 				 * draft-floyd-incr-init-win-01.txt,
2730 				 * Increasing TCP's Initial Window.
2731 				 */
2732 				DTRACE_PROBE3(cwnd__retransmitted__syn,
2733 				    tcp_t *, tcp, uint32_t, tcp->tcp_cwnd,
2734 				    uint32_t, tcp->tcp_mss);
2735 				tcp->tcp_cwnd = tcp->tcp_mss;
2736 			}
2737 
2738 			tcp->tcp_swl1 = seg_seq;
2739 			tcp->tcp_swl2 = seg_ack;
2740 
2741 			new_swnd = ntohs(tcpha->tha_win);
2742 			tcp->tcp_swnd = new_swnd;
2743 			if (new_swnd > tcp->tcp_max_swnd)
2744 				tcp->tcp_max_swnd = new_swnd;
2745 
2746 			/*
2747 			 * Always send the three-way handshake ack immediately
2748 			 * in order to make the connection complete as soon as
2749 			 * possible on the accepting host.
2750 			 */
2751 			flags |= TH_ACK_NEEDED;
2752 
2753 			/*
2754 			 * Trace connect-established here.
2755 			 */
2756 			DTRACE_TCP5(connect__established, mblk_t *, NULL,
2757 			    ip_xmit_attr_t *, tcp->tcp_connp->conn_ixa,
2758 			    void_ip_t *, iphdr, tcp_t *, tcp, tcph_t *, tcpha);
2759 
2760 			/* Trace change from SYN_SENT -> ESTABLISHED here */
2761 			DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
2762 			    connp->conn_ixa, void, NULL, tcp_t *, tcp,
2763 			    void, NULL, int32_t, TCPS_SYN_SENT);
2764 
2765 			/*
2766 			 * Special case for loopback.  At this point we have
2767 			 * received SYN-ACK from the remote endpoint.  In
2768 			 * order to ensure that both endpoints reach the
2769 			 * fused state prior to any data exchange, the final
2770 			 * ACK needs to be sent before we indicate T_CONN_CON
2771 			 * to the module upstream.
2772 			 */
2773 			if (tcp->tcp_loopback) {
2774 				mblk_t *ack_mp;
2775 
2776 				ASSERT(!tcp->tcp_unfusable);
2777 				ASSERT(mp1 != NULL);
2778 				/*
2779 				 * For loopback, we always get a pure SYN-ACK
2780 				 * and only need to send back the final ACK
2781 				 * with no data (this is because the other
2782 				 * tcp is ours and we don't do T/TCP).  This
2783 				 * final ACK triggers the passive side to
2784 				 * perform fusion in ESTABLISHED state.
2785 				 */
2786 				if ((ack_mp = tcp_ack_mp(tcp)) != NULL) {
2787 					if (tcp->tcp_ack_tid != 0) {
2788 						(void) TCP_TIMER_CANCEL(tcp,
2789 						    tcp->tcp_ack_tid);
2790 						tcp->tcp_ack_tid = 0;
2791 					}
2792 					tcp_send_data(tcp, ack_mp);
2793 					TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
2794 					TCPS_BUMP_MIB(tcps, tcpOutAck);
2795 
2796 					if (!IPCL_IS_NONSTR(connp)) {
2797 						/* Send up T_CONN_CON */
2798 						if (ira->ira_cred != NULL) {
2799 							mblk_setcred(mp1,
2800 							    ira->ira_cred,
2801 							    ira->ira_cpid);
2802 						}
2803 						putnext(connp->conn_rq, mp1);
2804 					} else {
2805 						(*sockupcalls->su_connected)
2806 						    (connp->conn_upper_handle,
2807 						    tcp->tcp_connid,
2808 						    ira->ira_cred,
2809 						    ira->ira_cpid);
2810 						freemsg(mp1);
2811 					}
2812 
2813 					freemsg(mp);
2814 					return;
2815 				}
2816 				/*
2817 				 * Forget fusion; we need to handle more
2818 				 * complex cases below.  Send the deferred
2819 				 * T_CONN_CON message upstream and proceed
2820 				 * as usual.  Mark this tcp as not capable
2821 				 * of fusion.
2822 				 */
2823 				TCP_STAT(tcps, tcp_fusion_unfusable);
2824 				tcp->tcp_unfusable = B_TRUE;
2825 				if (!IPCL_IS_NONSTR(connp)) {
2826 					if (ira->ira_cred != NULL) {
2827 						mblk_setcred(mp1, ira->ira_cred,
2828 						    ira->ira_cpid);
2829 					}
2830 					putnext(connp->conn_rq, mp1);
2831 				} else {
2832 					(*sockupcalls->su_connected)
2833 					    (connp->conn_upper_handle,
2834 					    tcp->tcp_connid, ira->ira_cred,
2835 					    ira->ira_cpid);
2836 					freemsg(mp1);
2837 				}
2838 			}
2839 
2840 			/*
2841 			 * Check to see if there is data to be sent.  If
2842 			 * yes, set the transmit flag.  Then check to see
2843 			 * if received data processing needs to be done.
2844 			 * If not, go straight to xmit_check.  This short
2845 			 * cut is OK as we don't support T/TCP.
2846 			 */
2847 			if (tcp->tcp_unsent)
2848 				flags |= TH_XMIT_NEEDED;
2849 
2850 			if (seg_len == 0 && !(flags & TH_URG)) {
2851 				freemsg(mp);
2852 				goto xmit_check;
2853 			}
2854 
2855 			flags &= ~TH_SYN;
2856 			seg_seq++;
2857 			break;
2858 		}
2859 		tcp->tcp_state = TCPS_SYN_RCVD;
2860 		DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
2861 		    connp->conn_ixa, void_ip_t *, NULL, tcp_t *, tcp,
2862 		    tcph_t *, NULL, int32_t, TCPS_SYN_SENT);
2863 		mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss,
2864 		    NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE);
2865 		if (mp1 != NULL) {
2866 			tcp_send_data(tcp, mp1);
2867 			TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
2868 		}
2869 		freemsg(mp);
2870 		return;
2871 	case TCPS_SYN_RCVD:
2872 		if (flags & TH_ACK) {
2873 			uint32_t pinit_wnd;
2874 
2875 			/*
2876 			 * In this state, a SYN|ACK packet is either bogus
2877 			 * because the other side must be ACKing our SYN which
2878 			 * indicates it has seen the ACK for their SYN and
2879 			 * shouldn't retransmit it or we're crossing SYNs
2880 			 * on active open.
2881 			 */
2882 			if ((flags & TH_SYN) && !tcp->tcp_active_open) {
2883 				freemsg(mp);
2884 				tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn",
2885 				    tcp, seg_ack, 0, TH_RST);
2886 				return;
2887 			}
2888 			/*
2889 			 * NOTE: RFC 793 pg. 72 says this should be
2890 			 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt
2891 			 * but that would mean we have an ack that ignored
2892 			 * our SYN.
2893 			 */
2894 			if (SEQ_LEQ(seg_ack, tcp->tcp_suna) ||
2895 			    SEQ_GT(seg_ack, tcp->tcp_snxt)) {
2896 				freemsg(mp);
2897 				tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack",
2898 				    tcp, seg_ack, 0, TH_RST);
2899 				return;
2900 			}
2901 			/*
2902 			 * No sane TCP stack will send such a small window
2903 			 * without receiving any data.  Just drop this invalid
2904 			 * ACK.  We also shorten the abort timeout in case
2905 			 * this is an attack.
2906 			 */
2907 			pinit_wnd = ntohs(tcpha->tha_win) << tcp->tcp_snd_ws;
2908 			if (pinit_wnd < tcp->tcp_mss &&
2909 			    pinit_wnd < tcp_init_wnd_chk) {
2910 				freemsg(mp);
2911 				TCP_STAT(tcps, tcp_zwin_ack_syn);
2912 				tcp->tcp_second_ctimer_threshold =
2913 				    tcp_early_abort * SECONDS;
2914 				return;
2915 			}
2916 		}
2917 		break;
2918 	case TCPS_LISTEN:
2919 		/*
2920 		 * Only a TLI listener can come through this path when a
2921 		 * acceptor is going back to be a listener and a packet
2922 		 * for the acceptor hits the classifier. For a socket
2923 		 * listener, this can never happen because a listener
2924 		 * can never accept connection on itself and hence a
2925 		 * socket acceptor can not go back to being a listener.
2926 		 */
2927 		ASSERT(!TCP_IS_SOCKET(tcp));
2928 		/*FALLTHRU*/
2929 	case TCPS_CLOSED:
2930 	case TCPS_BOUND: {
2931 		conn_t	*new_connp;
2932 		ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip;
2933 
2934 		/*
2935 		 * Don't accept any input on a closed tcp as this TCP logically
2936 		 * does not exist on the system. Don't proceed further with
2937 		 * this TCP. For instance, this packet could trigger another
2938 		 * close of this tcp which would be disastrous for tcp_refcnt.
2939 		 * tcp_close_detached / tcp_clean_death / tcp_closei_local must
2940 		 * be called at most once on a TCP. In this case we need to
2941 		 * refeed the packet into the classifier and figure out where
2942 		 * the packet should go.
2943 		 */
2944 		new_connp = ipcl_classify(mp, ira, ipst);
2945 		if (new_connp != NULL) {
2946 			/* Drops ref on new_connp */
2947 			tcp_reinput(new_connp, mp, ira, ipst);
2948 			return;
2949 		}
2950 		/* We failed to classify. For now just drop the packet */
2951 		freemsg(mp);
2952 		return;
2953 	}
2954 	case TCPS_IDLE:
2955 		/*
2956 		 * Handle the case where the tcp_clean_death() has happened
2957 		 * on a connection (application hasn't closed yet) but a packet
2958 		 * was already queued on squeue before tcp_clean_death()
2959 		 * was processed. Calling tcp_clean_death() twice on same
2960 		 * connection can result in weird behaviour.
2961 		 */
2962 		freemsg(mp);
2963 		return;
2964 	default:
2965 		break;
2966 	}
2967 
2968 	/*
2969 	 * Already on the correct queue/perimeter.
2970 	 * If this is a detached connection and not an eager
2971 	 * connection hanging off a listener then new data
2972 	 * (past the FIN) will cause a reset.
2973 	 * We do a special check here where it
2974 	 * is out of the main line, rather than check
2975 	 * if we are detached every time we see new
2976 	 * data down below.
2977 	 */
2978 	if (TCP_IS_DETACHED_NONEAGER(tcp) &&
2979 	    (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) {
2980 		TCPS_BUMP_MIB(tcps, tcpInClosed);
2981 		DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp);
2982 		freemsg(mp);
2983 		tcp_xmit_ctl("new data when detached", tcp,
2984 		    tcp->tcp_snxt, 0, TH_RST);
2985 		(void) tcp_clean_death(tcp, EPROTO);
2986 		return;
2987 	}
2988 
2989 	mp->b_rptr = (uchar_t *)tcpha + TCP_HDR_LENGTH(tcpha);
2990 	urp = ntohs(tcpha->tha_urp) - TCP_OLD_URP_INTERPRETATION;
2991 	new_swnd = ntohs(tcpha->tha_win) <<
2992 	    ((tcpha->tha_flags & TH_SYN) ? 0 : tcp->tcp_snd_ws);
2993 
2994 	/*
2995 	 * We are interested in two TCP options: timestamps (if negotiated) and
2996 	 * SACK (if negotiated). Skip option parsing if neither is negotiated.
2997 	 */
2998 	if (tcp->tcp_snd_ts_ok || tcp->tcp_snd_sack_ok) {
2999 		int options;
3000 		if (tcp->tcp_snd_sack_ok)
3001 			tcpopt.tcp = tcp;
3002 		else
3003 			tcpopt.tcp = NULL;
3004 		options = tcp_parse_options(tcpha, &tcpopt);
3005 		/*
3006 		 * RST segments must not be subject to PAWS and are not
3007 		 * required to have timestamps.
3008 		 * We do not drop keepalive segments without
3009 		 * timestamps, to maintain compatibility with legacy TCP stacks.
3010 		 */
3011 		boolean_t keepalive = (seg_len == 0 || seg_len == 1) &&
3012 		    (seg_seq + 1 == tcp->tcp_rnxt);
3013 		if (tcp->tcp_snd_ts_ok && !(flags & TH_RST) && !keepalive) {
3014 			/*
3015 			 * Per RFC 7323 section 3.2., silently drop non-RST
3016 			 * segments without expected TSopt. This is a 'SHOULD'
3017 			 * requirement.
3018 			 * We accept keepalives without TSopt to maintain
3019 			 * interoperability with tcp implementations that omit
3020 			 * the TSopt on these. Keepalive data is discarded, so
3021 			 * there is no risk corrupting data by accepting these.
3022 			 */
3023 			if (!(options & TCP_OPT_TSTAMP_PRESENT)) {
3024 				/*
3025 				 * Leave a breadcrumb for people to detect this
3026 				 * behavior.
3027 				 */
3028 				DTRACE_TCP1(droppedtimestamp, tcp_t *, tcp);
3029 				freemsg(mp);
3030 				return;
3031 			}
3032 
3033 			if (!tcp_paws_check(tcp, &tcpopt)) {
3034 				/*
3035 				 * This segment is not acceptable.
3036 				 * Drop it and send back an ACK.
3037 				 */
3038 				freemsg(mp);
3039 				flags |= TH_ACK_NEEDED;
3040 				goto ack_check;
3041 			}
3042 		}
3043 	}
3044 try_again:;
3045 	mss = tcp->tcp_mss;
3046 	gap = seg_seq - tcp->tcp_rnxt;
3047 	rgap = tcp->tcp_rwnd - (gap + seg_len);
3048 	/*
3049 	 * gap is the amount of sequence space between what we expect to see
3050 	 * and what we got for seg_seq.  A positive value for gap means
3051 	 * something got lost.  A negative value means we got some old stuff.
3052 	 */
3053 	if (gap < 0) {
3054 		/* Old stuff present.  Is the SYN in there? */
3055 		if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) &&
3056 		    (seg_len != 0)) {
3057 			flags &= ~TH_SYN;
3058 			seg_seq++;
3059 			urp--;
3060 			/* Recompute the gaps after noting the SYN. */
3061 			goto try_again;
3062 		}
3063 		TCPS_BUMP_MIB(tcps, tcpInDataDupSegs);
3064 		TCPS_UPDATE_MIB(tcps, tcpInDataDupBytes,
3065 		    (seg_len > -gap ? -gap : seg_len));
3066 		/* Remove the old stuff from seg_len. */
3067 		seg_len += gap;
3068 		/*
3069 		 * Anything left?
3070 		 * Make sure to check for unack'd FIN when rest of data
3071 		 * has been previously ack'd.
3072 		 */
3073 		if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) {
3074 			/*
3075 			 * Resets are only valid if they lie within our offered
3076 			 * window.  If the RST bit is set, we just ignore this
3077 			 * segment.
3078 			 */
3079 			if (flags & TH_RST) {
3080 				freemsg(mp);
3081 				return;
3082 			}
3083 
3084 			/*
3085 			 * The arriving of dup data packets indicate that we
3086 			 * may have postponed an ack for too long, or the other
3087 			 * side's RTT estimate is out of shape. Start acking
3088 			 * more often.
3089 			 */
3090 			if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) &&
3091 			    tcp->tcp_rack_cnt >= 1 &&
3092 			    tcp->tcp_rack_abs_max > 2) {
3093 				tcp->tcp_rack_abs_max--;
3094 			}
3095 			tcp->tcp_rack_cur_max = 1;
3096 
3097 			/*
3098 			 * This segment is "unacceptable".  None of its
3099 			 * sequence space lies within our advertized window.
3100 			 *
3101 			 * Adjust seg_len to the original value for tracing.
3102 			 */
3103 			seg_len -= gap;
3104 			if (connp->conn_debug) {
3105 				(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
3106 				    "tcp_rput: unacceptable, gap %d, rgap %d, "
3107 				    "flags 0x%x, seg_seq %u, seg_ack %u, "
3108 				    "seg_len %d, rnxt %u, snxt %u, %s",
3109 				    gap, rgap, flags, seg_seq, seg_ack,
3110 				    seg_len, tcp->tcp_rnxt, tcp->tcp_snxt,
3111 				    tcp_display(tcp, NULL,
3112 				    DISP_ADDR_AND_PORT));
3113 			}
3114 
3115 			/*
3116 			 * Arrange to send an ACK in response to the
3117 			 * unacceptable segment per RFC 793 page 69. There
3118 			 * is only one small difference between ours and the
3119 			 * acceptability test in the RFC - we accept ACK-only
3120 			 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK
3121 			 * will be generated.
3122 			 *
3123 			 * Note that we have to ACK an ACK-only packet at least
3124 			 * for stacks that send 0-length keep-alives with
3125 			 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122,
3126 			 * section 4.2.3.6. As long as we don't ever generate
3127 			 * an unacceptable packet in response to an incoming
3128 			 * packet that is unacceptable, it should not cause
3129 			 * "ACK wars".
3130 			 */
3131 			flags |=  TH_ACK_NEEDED;
3132 
3133 			/*
3134 			 * Continue processing this segment in order to use the
3135 			 * ACK information it contains, but skip all other
3136 			 * sequence-number processing.	Processing the ACK
3137 			 * information is necessary in order to
3138 			 * re-synchronize connections that may have lost
3139 			 * synchronization.
3140 			 *
3141 			 * We clear seg_len and flag fields related to
3142 			 * sequence number processing as they are not
3143 			 * to be trusted for an unacceptable segment.
3144 			 */
3145 			seg_len = 0;
3146 			flags &= ~(TH_SYN | TH_FIN | TH_URG);
3147 			goto process_ack;
3148 		}
3149 
3150 		/* Fix seg_seq, and chew the gap off the front. */
3151 		seg_seq = tcp->tcp_rnxt;
3152 		urp += gap;
3153 		do {
3154 			mblk_t	*mp2;
3155 			ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
3156 			    (uintptr_t)UINT_MAX);
3157 			gap += (uint_t)(mp->b_wptr - mp->b_rptr);
3158 			if (gap > 0) {
3159 				mp->b_rptr = mp->b_wptr - gap;
3160 				break;
3161 			}
3162 			mp2 = mp;
3163 			mp = mp->b_cont;
3164 			freeb(mp2);
3165 		} while (gap < 0);
3166 		/*
3167 		 * If the urgent data has already been acknowledged, we
3168 		 * should ignore TH_URG below
3169 		 */
3170 		if (urp < 0)
3171 			flags &= ~TH_URG;
3172 	}
3173 	/*
3174 	 * rgap is the amount of stuff received out of window.  A negative
3175 	 * value is the amount out of window.
3176 	 */
3177 	if (rgap < 0) {
3178 		mblk_t	*mp2;
3179 
3180 		if (tcp->tcp_rwnd == 0) {
3181 			TCPS_BUMP_MIB(tcps, tcpInWinProbe);
3182 			tcp->tcp_cs.tcp_in_zwnd_probes++;
3183 		} else {
3184 			TCPS_BUMP_MIB(tcps, tcpInDataPastWinSegs);
3185 			TCPS_UPDATE_MIB(tcps, tcpInDataPastWinBytes, -rgap);
3186 		}
3187 
3188 		/*
3189 		 * seg_len does not include the FIN, so if more than
3190 		 * just the FIN is out of window, we act like we don't
3191 		 * see it.  (If just the FIN is out of window, rgap
3192 		 * will be zero and we will go ahead and acknowledge
3193 		 * the FIN.)
3194 		 */
3195 		flags &= ~TH_FIN;
3196 
3197 		/* Fix seg_len and make sure there is something left. */
3198 		seg_len += rgap;
3199 		if (seg_len <= 0) {
3200 			/*
3201 			 * Resets are only valid if they lie within our offered
3202 			 * window.  If the RST bit is set, we just ignore this
3203 			 * segment.
3204 			 */
3205 			if (flags & TH_RST) {
3206 				freemsg(mp);
3207 				return;
3208 			}
3209 
3210 			/* Per RFC 793, we need to send back an ACK. */
3211 			flags |= TH_ACK_NEEDED;
3212 
3213 			/*
3214 			 * Send SIGURG as soon as possible i.e. even
3215 			 * if the TH_URG was delivered in a window probe
3216 			 * packet (which will be unacceptable).
3217 			 *
3218 			 * We generate a signal if none has been generated
3219 			 * for this connection or if this is a new urgent
3220 			 * byte. Also send a zero-length "unmarked" message
3221 			 * to inform SIOCATMARK that this is not the mark.
3222 			 *
3223 			 * tcp_urp_last_valid is cleared when the T_exdata_ind
3224 			 * is sent up. This plus the check for old data
3225 			 * (gap >= 0) handles the wraparound of the sequence
3226 			 * number space without having to always track the
3227 			 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks
3228 			 * this max in its rcv_up variable).
3229 			 *
3230 			 * This prevents duplicate SIGURGS due to a "late"
3231 			 * zero-window probe when the T_EXDATA_IND has already
3232 			 * been sent up.
3233 			 */
3234 			if ((flags & TH_URG) &&
3235 			    (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq,
3236 			    tcp->tcp_urp_last))) {
3237 				if (IPCL_IS_NONSTR(connp)) {
3238 					if (!TCP_IS_DETACHED(tcp)) {
3239 						(*sockupcalls->su_signal_oob)
3240 						    (connp->conn_upper_handle,
3241 						    urp);
3242 					}
3243 				} else {
3244 					mp1 = allocb(0, BPRI_MED);
3245 					if (mp1 == NULL) {
3246 						freemsg(mp);
3247 						return;
3248 					}
3249 					if (!TCP_IS_DETACHED(tcp) &&
3250 					    !putnextctl1(connp->conn_rq,
3251 					    M_PCSIG, SIGURG)) {
3252 						/* Try again on the rexmit. */
3253 						freemsg(mp1);
3254 						freemsg(mp);
3255 						return;
3256 					}
3257 					/*
3258 					 * If the next byte would be the mark
3259 					 * then mark with MARKNEXT else mark
3260 					 * with NOTMARKNEXT.
3261 					 */
3262 					if (gap == 0 && urp == 0)
3263 						mp1->b_flag |= MSGMARKNEXT;
3264 					else
3265 						mp1->b_flag |= MSGNOTMARKNEXT;
3266 					freemsg(tcp->tcp_urp_mark_mp);
3267 					tcp->tcp_urp_mark_mp = mp1;
3268 					flags |= TH_SEND_URP_MARK;
3269 				}
3270 				tcp->tcp_urp_last_valid = B_TRUE;
3271 				tcp->tcp_urp_last = urp + seg_seq;
3272 			}
3273 			/*
3274 			 * If this is a zero window probe, continue to
3275 			 * process the ACK part.  But we need to set seg_len
3276 			 * to 0 to avoid data processing.  Otherwise just
3277 			 * drop the segment and send back an ACK.
3278 			 */
3279 			if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) {
3280 				flags &= ~(TH_SYN | TH_URG);
3281 				seg_len = 0;
3282 				goto process_ack;
3283 			} else {
3284 				freemsg(mp);
3285 				goto ack_check;
3286 			}
3287 		}
3288 		/* Pitch out of window stuff off the end. */
3289 		rgap = seg_len;
3290 		mp2 = mp;
3291 		do {
3292 			ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <=
3293 			    (uintptr_t)INT_MAX);
3294 			rgap -= (int)(mp2->b_wptr - mp2->b_rptr);
3295 			if (rgap < 0) {
3296 				mp2->b_wptr += rgap;
3297 				if ((mp1 = mp2->b_cont) != NULL) {
3298 					mp2->b_cont = NULL;
3299 					freemsg(mp1);
3300 				}
3301 				break;
3302 			}
3303 		} while ((mp2 = mp2->b_cont) != NULL);
3304 	}
3305 ok:;
3306 	/*
3307 	 * TCP should check ECN info for segments inside the window only.
3308 	 * Therefore the check should be done here.
3309 	 */
3310 	if (tcp->tcp_ecn_ok) {
3311 		if (flags & TH_CWR) {
3312 			tcp->tcp_ecn_echo_on = B_FALSE;
3313 		}
3314 		/*
3315 		 * Note that both ECN_CE and CWR can be set in the
3316 		 * same segment.  In this case, we once again turn
3317 		 * on ECN_ECHO.
3318 		 */
3319 		if (connp->conn_ipversion == IPV4_VERSION) {
3320 			uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service;
3321 
3322 			if ((tos & IPH_ECN_CE) == IPH_ECN_CE) {
3323 				tcp->tcp_ecn_echo_on = B_TRUE;
3324 			}
3325 		} else {
3326 			uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf;
3327 
3328 			if ((vcf & htonl(IPH_ECN_CE << 20)) ==
3329 			    htonl(IPH_ECN_CE << 20)) {
3330 				tcp->tcp_ecn_echo_on = B_TRUE;
3331 			}
3332 		}
3333 	}
3334 
3335 	/*
3336 	 * Check whether we can update tcp_ts_recent. This test is from RFC
3337 	 * 7323, section 5.3.
3338 	 */
3339 	if (tcp->tcp_snd_ts_ok && !(flags & TH_RST) &&
3340 	    TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) &&
3341 	    SEQ_LEQ(seg_seq, tcp->tcp_rack)) {
3342 		tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val;
3343 		tcp->tcp_last_rcv_lbolt = LBOLT_FASTPATH64;
3344 	}
3345 
3346 	if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) {
3347 		/*
3348 		 * FIN in an out of order segment.  We record this in
3349 		 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq.
3350 		 * Clear the FIN so that any check on FIN flag will fail.
3351 		 * Remember that FIN also counts in the sequence number
3352 		 * space.  So we need to ack out of order FIN only segments.
3353 		 */
3354 		if (flags & TH_FIN) {
3355 			tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID;
3356 			tcp->tcp_ofo_fin_seq = seg_seq + seg_len;
3357 			flags &= ~TH_FIN;
3358 			flags |= TH_ACK_NEEDED;
3359 		}
3360 		if (seg_len > 0) {
3361 			/* Fill in the SACK blk list. */
3362 			if (tcp->tcp_snd_sack_ok) {
3363 				tcp_sack_insert(tcp->tcp_sack_list,
3364 				    seg_seq, seg_seq + seg_len,
3365 				    &(tcp->tcp_num_sack_blk));
3366 			}
3367 
3368 			/*
3369 			 * Attempt reassembly and see if we have something
3370 			 * ready to go.
3371 			 */
3372 			mp = tcp_reass(tcp, mp, seg_seq);
3373 			/* Always ack out of order packets */
3374 			flags |= TH_ACK_NEEDED | TH_PUSH;
3375 			if (mp) {
3376 				ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
3377 				    (uintptr_t)INT_MAX);
3378 				seg_len = mp->b_cont ? msgdsize(mp) :
3379 				    (int)(mp->b_wptr - mp->b_rptr);
3380 				seg_seq = tcp->tcp_rnxt;
3381 				/*
3382 				 * A gap is filled and the seq num and len
3383 				 * of the gap match that of a previously
3384 				 * received FIN, put the FIN flag back in.
3385 				 */
3386 				if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) &&
3387 				    seg_seq + seg_len == tcp->tcp_ofo_fin_seq) {
3388 					flags |= TH_FIN;
3389 					tcp->tcp_valid_bits &=
3390 					    ~TCP_OFO_FIN_VALID;
3391 				}
3392 				if (tcp->tcp_reass_tid != 0) {
3393 					(void) TCP_TIMER_CANCEL(tcp,
3394 					    tcp->tcp_reass_tid);
3395 					/*
3396 					 * Restart the timer if there is still
3397 					 * data in the reassembly queue.
3398 					 */
3399 					if (tcp->tcp_reass_head != NULL) {
3400 						tcp->tcp_reass_tid = TCP_TIMER(
3401 						    tcp, tcp_reass_timer,
3402 						    tcps->tcps_reass_timeout);
3403 					} else {
3404 						tcp->tcp_reass_tid = 0;
3405 					}
3406 				}
3407 			} else {
3408 				/*
3409 				 * Keep going even with NULL mp.
3410 				 * There may be a useful ACK or something else
3411 				 * we don't want to miss.
3412 				 *
3413 				 * But TCP should not perform fast retransmit
3414 				 * because of the ack number.  TCP uses
3415 				 * seg_len == 0 to determine if it is a pure
3416 				 * ACK.  And this is not a pure ACK.
3417 				 */
3418 				seg_len = 0;
3419 				ofo_seg = B_TRUE;
3420 
3421 				if (tcps->tcps_reass_timeout != 0 &&
3422 				    tcp->tcp_reass_tid == 0) {
3423 					tcp->tcp_reass_tid = TCP_TIMER(tcp,
3424 					    tcp_reass_timer,
3425 					    tcps->tcps_reass_timeout);
3426 				}
3427 			}
3428 		}
3429 	} else if (seg_len > 0) {
3430 		TCPS_BUMP_MIB(tcps, tcpInDataInorderSegs);
3431 		TCPS_UPDATE_MIB(tcps, tcpInDataInorderBytes, seg_len);
3432 		tcp->tcp_cs.tcp_in_data_inorder_segs++;
3433 		tcp->tcp_cs.tcp_in_data_inorder_bytes += seg_len;
3434 
3435 		/*
3436 		 * If an out of order FIN was received before, and the seq
3437 		 * num and len of the new segment match that of the FIN,
3438 		 * put the FIN flag back in.
3439 		 */
3440 		if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) &&
3441 		    seg_seq + seg_len == tcp->tcp_ofo_fin_seq) {
3442 			flags |= TH_FIN;
3443 			tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID;
3444 		}
3445 	}
3446 	if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) {
3447 	if (flags & TH_RST) {
3448 		freemsg(mp);
3449 		switch (tcp->tcp_state) {
3450 		case TCPS_SYN_RCVD:
3451 			(void) tcp_clean_death(tcp, ECONNREFUSED);
3452 			break;
3453 		case TCPS_ESTABLISHED:
3454 		case TCPS_FIN_WAIT_1:
3455 		case TCPS_FIN_WAIT_2:
3456 		case TCPS_CLOSE_WAIT:
3457 			(void) tcp_clean_death(tcp, ECONNRESET);
3458 			break;
3459 		case TCPS_CLOSING:
3460 		case TCPS_LAST_ACK:
3461 			(void) tcp_clean_death(tcp, 0);
3462 			break;
3463 		default:
3464 			ASSERT(tcp->tcp_state != TCPS_TIME_WAIT);
3465 			(void) tcp_clean_death(tcp, ENXIO);
3466 			break;
3467 		}
3468 		return;
3469 	}
3470 	if (flags & TH_SYN) {
3471 		/*
3472 		 * See RFC 793, Page 71
3473 		 *
3474 		 * The seq number must be in the window as it should
3475 		 * be "fixed" above.  If it is outside window, it should
3476 		 * be already rejected.  Note that we allow seg_seq to be
3477 		 * rnxt + rwnd because we want to accept 0 window probe.
3478 		 */
3479 		ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) &&
3480 		    SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd));
3481 		freemsg(mp);
3482 		/*
3483 		 * If the ACK flag is not set, just use our snxt as the
3484 		 * seq number of the RST segment.
3485 		 */
3486 		if (!(flags & TH_ACK)) {
3487 			seg_ack = tcp->tcp_snxt;
3488 		}
3489 		tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1,
3490 		    TH_RST|TH_ACK);
3491 		ASSERT(tcp->tcp_state != TCPS_TIME_WAIT);
3492 		(void) tcp_clean_death(tcp, ECONNRESET);
3493 		return;
3494 	}
3495 	/*
3496 	 * urp could be -1 when the urp field in the packet is 0
3497 	 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent
3498 	 * byte was at seg_seq - 1, in which case we ignore the urgent flag.
3499 	 */
3500 	if ((flags & TH_URG) && urp >= 0) {
3501 		if (!tcp->tcp_urp_last_valid ||
3502 		    SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) {
3503 			/*
3504 			 * Non-STREAMS sockets handle the urgent data a litte
3505 			 * differently from STREAMS based sockets. There is no
3506 			 * need to mark any mblks with the MSG{NOT,}MARKNEXT
3507 			 * flags to keep SIOCATMARK happy. Instead a
3508 			 * su_signal_oob upcall is made to update the mark.
3509 			 * Neither is a T_EXDATA_IND mblk needed to be
3510 			 * prepended to the urgent data. The urgent data is
3511 			 * delivered using the su_recv upcall, where we set
3512 			 * the MSG_OOB flag to indicate that it is urg data.
3513 			 *
3514 			 * Neither TH_SEND_URP_MARK nor TH_MARKNEXT_NEEDED
3515 			 * are used by non-STREAMS sockets.
3516 			 */
3517 			if (IPCL_IS_NONSTR(connp)) {
3518 				if (!TCP_IS_DETACHED(tcp)) {
3519 					(*sockupcalls->su_signal_oob)
3520 					    (connp->conn_upper_handle, urp);
3521 				}
3522 			} else {
3523 				/*
3524 				 * If we haven't generated the signal yet for
3525 				 * this urgent pointer value, do it now.  Also,
3526 				 * send up a zero-length M_DATA indicating
3527 				 * whether or not this is the mark. The latter
3528 				 * is not needed when a T_EXDATA_IND is sent up.
3529 				 * However, if there are allocation failures
3530 				 * this code relies on the sender retransmitting
3531 				 * and the socket code for determining the mark
3532 				 * should not block waiting for the peer to
3533 				 * transmit. Thus, for simplicity we always
3534 				 * send up the mark indication.
3535 				 */
3536 				mp1 = allocb(0, BPRI_MED);
3537 				if (mp1 == NULL) {
3538 					freemsg(mp);
3539 					return;
3540 				}
3541 				if (!TCP_IS_DETACHED(tcp) &&
3542 				    !putnextctl1(connp->conn_rq, M_PCSIG,
3543 				    SIGURG)) {
3544 					/* Try again on the rexmit. */
3545 					freemsg(mp1);
3546 					freemsg(mp);
3547 					return;
3548 				}
3549 				/*
3550 				 * Mark with NOTMARKNEXT for now.
3551 				 * The code below will change this to MARKNEXT
3552 				 * if we are at the mark.
3553 				 *
3554 				 * If there are allocation failures (e.g. in
3555 				 * dupmsg below) the next time tcp_input_data
3556 				 * sees the urgent segment it will send up the
3557 				 * MSGMARKNEXT message.
3558 				 */
3559 				mp1->b_flag |= MSGNOTMARKNEXT;
3560 				freemsg(tcp->tcp_urp_mark_mp);
3561 				tcp->tcp_urp_mark_mp = mp1;
3562 				flags |= TH_SEND_URP_MARK;
3563 #ifdef DEBUG
3564 				(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
3565 				    "tcp_rput: sent M_PCSIG 2 seq %x urp %x "
3566 				    "last %x, %s",
3567 				    seg_seq, urp, tcp->tcp_urp_last,
3568 				    tcp_display(tcp, NULL, DISP_PORT_ONLY));
3569 #endif /* DEBUG */
3570 			}
3571 			tcp->tcp_urp_last_valid = B_TRUE;
3572 			tcp->tcp_urp_last = urp + seg_seq;
3573 		} else if (tcp->tcp_urp_mark_mp != NULL) {
3574 			/*
3575 			 * An allocation failure prevented the previous
3576 			 * tcp_input_data from sending up the allocated
3577 			 * MSG*MARKNEXT message - send it up this time
3578 			 * around.
3579 			 */
3580 			flags |= TH_SEND_URP_MARK;
3581 		}
3582 
3583 		/*
3584 		 * If the urgent byte is in this segment, make sure that it is
3585 		 * all by itself.  This makes it much easier to deal with the
3586 		 * possibility of an allocation failure on the T_exdata_ind.
3587 		 * Note that seg_len is the number of bytes in the segment, and
3588 		 * urp is the offset into the segment of the urgent byte.
3589 		 * urp < seg_len means that the urgent byte is in this segment.
3590 		 */
3591 		if (urp < seg_len) {
3592 			if (seg_len != 1) {
3593 				uint32_t  tmp_rnxt;
3594 				/*
3595 				 * Break it up and feed it back in.
3596 				 * Re-attach the IP header.
3597 				 */
3598 				mp->b_rptr = iphdr;
3599 				if (urp > 0) {
3600 					/*
3601 					 * There is stuff before the urgent
3602 					 * byte.
3603 					 */
3604 					mp1 = dupmsg(mp);
3605 					if (!mp1) {
3606 						/*
3607 						 * Trim from urgent byte on.
3608 						 * The rest will come back.
3609 						 */
3610 						(void) adjmsg(mp,
3611 						    urp - seg_len);
3612 						tcp_input_data(connp,
3613 						    mp, NULL, ira);
3614 						return;
3615 					}
3616 					(void) adjmsg(mp1, urp - seg_len);
3617 					/* Feed this piece back in. */
3618 					tmp_rnxt = tcp->tcp_rnxt;
3619 					tcp_input_data(connp, mp1, NULL, ira);
3620 					/*
3621 					 * If the data passed back in was not
3622 					 * processed (ie: bad ACK) sending
3623 					 * the remainder back in will cause a
3624 					 * loop. In this case, drop the
3625 					 * packet and let the sender try
3626 					 * sending a good packet.
3627 					 */
3628 					if (tmp_rnxt == tcp->tcp_rnxt) {
3629 						freemsg(mp);
3630 						return;
3631 					}
3632 				}
3633 				if (urp != seg_len - 1) {
3634 					uint32_t  tmp_rnxt;
3635 					/*
3636 					 * There is stuff after the urgent
3637 					 * byte.
3638 					 */
3639 					mp1 = dupmsg(mp);
3640 					if (!mp1) {
3641 						/*
3642 						 * Trim everything beyond the
3643 						 * urgent byte.  The rest will
3644 						 * come back.
3645 						 */
3646 						(void) adjmsg(mp,
3647 						    urp + 1 - seg_len);
3648 						tcp_input_data(connp,
3649 						    mp, NULL, ira);
3650 						return;
3651 					}
3652 					(void) adjmsg(mp1, urp + 1 - seg_len);
3653 					tmp_rnxt = tcp->tcp_rnxt;
3654 					tcp_input_data(connp, mp1, NULL, ira);
3655 					/*
3656 					 * If the data passed back in was not
3657 					 * processed (ie: bad ACK) sending
3658 					 * the remainder back in will cause a
3659 					 * loop. In this case, drop the
3660 					 * packet and let the sender try
3661 					 * sending a good packet.
3662 					 */
3663 					if (tmp_rnxt == tcp->tcp_rnxt) {
3664 						freemsg(mp);
3665 						return;
3666 					}
3667 				}
3668 				tcp_input_data(connp, mp, NULL, ira);
3669 				return;
3670 			}
3671 			/*
3672 			 * This segment contains only the urgent byte.  We
3673 			 * have to allocate the T_exdata_ind, if we can.
3674 			 */
3675 			if (IPCL_IS_NONSTR(connp)) {
3676 				int error;
3677 
3678 				(*sockupcalls->su_recv)
3679 				    (connp->conn_upper_handle, mp, seg_len,
3680 				    MSG_OOB, &error, NULL);
3681 				/*
3682 				 * We should never be in middle of a
3683 				 * fallback, the squeue guarantees that.
3684 				 */
3685 				ASSERT(error != EOPNOTSUPP);
3686 				mp = NULL;
3687 				goto update_ack;
3688 			} else if (!tcp->tcp_urp_mp) {
3689 				struct T_exdata_ind *tei;
3690 				mp1 = allocb(sizeof (struct T_exdata_ind),
3691 				    BPRI_MED);
3692 				if (!mp1) {
3693 					/*
3694 					 * Sigh... It'll be back.
3695 					 * Generate any MSG*MARK message now.
3696 					 */
3697 					freemsg(mp);
3698 					seg_len = 0;
3699 					if (flags & TH_SEND_URP_MARK) {
3700 
3701 
3702 						ASSERT(tcp->tcp_urp_mark_mp);
3703 						tcp->tcp_urp_mark_mp->b_flag &=
3704 						    ~MSGNOTMARKNEXT;
3705 						tcp->tcp_urp_mark_mp->b_flag |=
3706 						    MSGMARKNEXT;
3707 					}
3708 					goto ack_check;
3709 				}
3710 				mp1->b_datap->db_type = M_PROTO;
3711 				tei = (struct T_exdata_ind *)mp1->b_rptr;
3712 				tei->PRIM_type = T_EXDATA_IND;
3713 				tei->MORE_flag = 0;
3714 				mp1->b_wptr = (uchar_t *)&tei[1];
3715 				tcp->tcp_urp_mp = mp1;
3716 #ifdef DEBUG
3717 				(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
3718 				    "tcp_rput: allocated exdata_ind %s",
3719 				    tcp_display(tcp, NULL,
3720 				    DISP_PORT_ONLY));
3721 #endif /* DEBUG */
3722 				/*
3723 				 * There is no need to send a separate MSG*MARK
3724 				 * message since the T_EXDATA_IND will be sent
3725 				 * now.
3726 				 */
3727 				flags &= ~TH_SEND_URP_MARK;
3728 				freemsg(tcp->tcp_urp_mark_mp);
3729 				tcp->tcp_urp_mark_mp = NULL;
3730 			}
3731 			/*
3732 			 * Now we are all set.  On the next putnext upstream,
3733 			 * tcp_urp_mp will be non-NULL and will get prepended
3734 			 * to what has to be this piece containing the urgent
3735 			 * byte.  If for any reason we abort this segment below,
3736 			 * if it comes back, we will have this ready, or it
3737 			 * will get blown off in close.
3738 			 */
3739 		} else if (urp == seg_len) {
3740 			/*
3741 			 * The urgent byte is the next byte after this sequence
3742 			 * number. If this endpoint is non-STREAMS, then there
3743 			 * is nothing to do here since the socket has already
3744 			 * been notified about the urg pointer by the
3745 			 * su_signal_oob call above.
3746 			 *
3747 			 * In case of STREAMS, some more work might be needed.
3748 			 * If there is data it is marked with MSGMARKNEXT and
3749 			 * and any tcp_urp_mark_mp is discarded since it is not
3750 			 * needed. Otherwise, if the code above just allocated
3751 			 * a zero-length tcp_urp_mark_mp message, that message
3752 			 * is tagged with MSGMARKNEXT. Sending up these
3753 			 * MSGMARKNEXT messages makes SIOCATMARK work correctly
3754 			 * even though the T_EXDATA_IND will not be sent up
3755 			 * until the urgent byte arrives.
3756 			 */
3757 			if (!IPCL_IS_NONSTR(tcp->tcp_connp)) {
3758 				if (seg_len != 0) {
3759 					flags |= TH_MARKNEXT_NEEDED;
3760 					freemsg(tcp->tcp_urp_mark_mp);
3761 					tcp->tcp_urp_mark_mp = NULL;
3762 					flags &= ~TH_SEND_URP_MARK;
3763 				} else if (tcp->tcp_urp_mark_mp != NULL) {
3764 					flags |= TH_SEND_URP_MARK;
3765 					tcp->tcp_urp_mark_mp->b_flag &=
3766 					    ~MSGNOTMARKNEXT;
3767 					tcp->tcp_urp_mark_mp->b_flag |=
3768 					    MSGMARKNEXT;
3769 				}
3770 			}
3771 #ifdef DEBUG
3772 			(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
3773 			    "tcp_rput: AT MARK, len %d, flags 0x%x, %s",
3774 			    seg_len, flags,
3775 			    tcp_display(tcp, NULL, DISP_PORT_ONLY));
3776 #endif /* DEBUG */
3777 		}
3778 #ifdef DEBUG
3779 		else {
3780 			/* Data left until we hit mark */
3781 			(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
3782 			    "tcp_rput: URP %d bytes left, %s",
3783 			    urp - seg_len, tcp_display(tcp, NULL,
3784 			    DISP_PORT_ONLY));
3785 		}
3786 #endif /* DEBUG */
3787 	}
3788 
3789 process_ack:
3790 	if (!(flags & TH_ACK)) {
3791 		freemsg(mp);
3792 		goto xmit_check;
3793 	}
3794 	}
3795 	bytes_acked = (int)(seg_ack - tcp->tcp_suna);
3796 
3797 	if (bytes_acked > 0)
3798 		tcp->tcp_ip_forward_progress = B_TRUE;
3799 	if (tcp->tcp_state == TCPS_SYN_RCVD) {
3800 		/*
3801 		 * tcp_sendmsg() checks tcp_state without entering
3802 		 * the squeue so tcp_state should be updated before
3803 		 * sending up a connection confirmation or a new
3804 		 * connection indication.
3805 		 */
3806 		tcp->tcp_state = TCPS_ESTABLISHED;
3807 
3808 		/*
3809 		 * We are seeing the final ack in the three way
3810 		 * hand shake of a active open'ed connection
3811 		 * so we must send up a T_CONN_CON
3812 		 */
3813 		if (tcp->tcp_active_open) {
3814 			if (!tcp_conn_con(tcp, iphdr, mp, NULL, ira)) {
3815 				freemsg(mp);
3816 				tcp->tcp_state = TCPS_SYN_RCVD;
3817 				return;
3818 			}
3819 			/*
3820 			 * Don't fuse the loopback endpoints for
3821 			 * simultaneous active opens.
3822 			 */
3823 			if (tcp->tcp_loopback) {
3824 				TCP_STAT(tcps, tcp_fusion_unfusable);
3825 				tcp->tcp_unfusable = B_TRUE;
3826 			}
3827 			/*
3828 			 * For simultaneous active open, trace receipt of final
3829 			 * ACK as tcp:::connect-established.
3830 			 */
3831 			DTRACE_TCP5(connect__established, mblk_t *, NULL,
3832 			    ip_xmit_attr_t *, connp->conn_ixa, void_ip_t *,
3833 			    iphdr, tcp_t *, tcp, tcph_t *, tcpha);
3834 		} else if (IPCL_IS_NONSTR(connp)) {
3835 			/*
3836 			 * 3-way handshake has completed, so notify socket
3837 			 * of the new connection.
3838 			 *
3839 			 * We are here means eager is fine but it can
3840 			 * get a TH_RST at any point between now and till
3841 			 * accept completes and disappear. We need to
3842 			 * ensure that reference to eager is valid after
3843 			 * we get out of eager's perimeter. So we do
3844 			 * an extra refhold.
3845 			 */
3846 			CONN_INC_REF(connp);
3847 
3848 			if (!tcp_newconn_notify(tcp, ira)) {
3849 				/*
3850 				 * The state-change probe for SYN_RCVD ->
3851 				 * ESTABLISHED has not fired yet. We reset
3852 				 * the state to SYN_RCVD so that future
3853 				 * state-change probes report correct state
3854 				 * transistions.
3855 				 */
3856 				tcp->tcp_state = TCPS_SYN_RCVD;
3857 				freemsg(mp);
3858 				/* notification did not go up, so drop ref */
3859 				CONN_DEC_REF(connp);
3860 				/* ... and close the eager */
3861 				ASSERT(TCP_IS_DETACHED(tcp));
3862 				(void) tcp_close_detached(tcp);
3863 				return;
3864 			}
3865 			/*
3866 			 * tcp_newconn_notify() changes conn_upcalls and
3867 			 * connp->conn_upper_handle.  Fix things now, in case
3868 			 * there's data attached to this ack.
3869 			 */
3870 			if (connp->conn_upcalls != NULL)
3871 				sockupcalls = connp->conn_upcalls;
3872 			/*
3873 			 * For passive open, trace receipt of final ACK as
3874 			 * tcp:::accept-established.
3875 			 */
3876 			DTRACE_TCP5(accept__established, mlbk_t *, NULL,
3877 			    ip_xmit_attr_t *, connp->conn_ixa, void_ip_t *,
3878 			    iphdr, tcp_t *, tcp, tcph_t *, tcpha);
3879 		} else {
3880 			/*
3881 			 * 3-way handshake complete - this is a STREAMS based
3882 			 * socket, so pass up the T_CONN_IND.
3883 			 */
3884 			tcp_t	*listener = tcp->tcp_listener;
3885 			mblk_t	*mp = tcp->tcp_conn.tcp_eager_conn_ind;
3886 
3887 			tcp->tcp_tconnind_started = B_TRUE;
3888 			tcp->tcp_conn.tcp_eager_conn_ind = NULL;
3889 			ASSERT(mp != NULL);
3890 			/*
3891 			 * We are here means eager is fine but it can
3892 			 * get a TH_RST at any point between now and till
3893 			 * accept completes and disappear. We need to
3894 			 * ensure that reference to eager is valid after
3895 			 * we get out of eager's perimeter. So we do
3896 			 * an extra refhold.
3897 			 */
3898 			CONN_INC_REF(connp);
3899 
3900 			/*
3901 			 * The listener also exists because of the refhold
3902 			 * done in tcp_input_listener. Its possible that it
3903 			 * might have closed. We will check that once we
3904 			 * get inside listeners context.
3905 			 */
3906 			CONN_INC_REF(listener->tcp_connp);
3907 			if (listener->tcp_connp->conn_sqp ==
3908 			    connp->conn_sqp) {
3909 				/*
3910 				 * We optimize by not calling an SQUEUE_ENTER
3911 				 * on the listener since we know that the
3912 				 * listener and eager squeues are the same.
3913 				 * We are able to make this check safely only
3914 				 * because neither the eager nor the listener
3915 				 * can change its squeue. Only an active connect
3916 				 * can change its squeue
3917 				 */
3918 				tcp_send_conn_ind(listener->tcp_connp, mp,
3919 				    listener->tcp_connp->conn_sqp);
3920 				CONN_DEC_REF(listener->tcp_connp);
3921 			} else if (!tcp->tcp_loopback) {
3922 				SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp,
3923 				    mp, tcp_send_conn_ind,
3924 				    listener->tcp_connp, NULL, SQ_FILL,
3925 				    SQTAG_TCP_CONN_IND);
3926 			} else {
3927 				SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp,
3928 				    mp, tcp_send_conn_ind,
3929 				    listener->tcp_connp, NULL, SQ_NODRAIN,
3930 				    SQTAG_TCP_CONN_IND);
3931 			}
3932 			/*
3933 			 * For passive open, trace receipt of final ACK as
3934 			 * tcp:::accept-established.
3935 			 */
3936 			DTRACE_TCP5(accept__established, mlbk_t *, NULL,
3937 			    ip_xmit_attr_t *, connp->conn_ixa, void_ip_t *,
3938 			    iphdr, tcp_t *, tcp, tcph_t *, tcpha);
3939 		}
3940 		TCPS_CONN_INC(tcps);
3941 
3942 		tcp->tcp_suna = tcp->tcp_iss + 1;	/* One for the SYN */
3943 		bytes_acked--;
3944 		/* SYN was acked - making progress */
3945 		tcp->tcp_ip_forward_progress = B_TRUE;
3946 
3947 		/*
3948 		 * If SYN was retransmitted, need to reset all
3949 		 * retransmission info as this segment will be
3950 		 * treated as a dup ACK.
3951 		 */
3952 		if (tcp->tcp_rexmit) {
3953 			tcp->tcp_rexmit = B_FALSE;
3954 			tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
3955 			tcp->tcp_rexmit_max = tcp->tcp_snxt;
3956 			tcp->tcp_ms_we_have_waited = 0;
3957 			DTRACE_PROBE3(cwnd__retransmitted__syn,
3958 			    tcp_t *, tcp, uint32_t, tcp->tcp_cwnd,
3959 			    uint32_t, tcp->tcp_mss);
3960 			tcp->tcp_cwnd = mss;
3961 		}
3962 
3963 		/*
3964 		 * We set the send window to zero here.
3965 		 * This is needed if there is data to be
3966 		 * processed already on the queue.
3967 		 * Later (at swnd_update label), the
3968 		 * "new_swnd > tcp_swnd" condition is satisfied
3969 		 * the XMIT_NEEDED flag is set in the current
3970 		 * (SYN_RCVD) state. This ensures tcp_wput_data() is
3971 		 * called if there is already data on queue in
3972 		 * this state.
3973 		 */
3974 		tcp->tcp_swnd = 0;
3975 
3976 		if (new_swnd > tcp->tcp_max_swnd)
3977 			tcp->tcp_max_swnd = new_swnd;
3978 		tcp->tcp_swl1 = seg_seq;
3979 		tcp->tcp_swl2 = seg_ack;
3980 		tcp->tcp_valid_bits &= ~TCP_ISS_VALID;
3981 
3982 		/* Trace change from SYN_RCVD -> ESTABLISHED here */
3983 		DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
3984 		    connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
3985 		    int32_t, TCPS_SYN_RCVD);
3986 
3987 		/* Fuse when both sides are in ESTABLISHED state */
3988 		if (tcp->tcp_loopback && do_tcp_fusion)
3989 			tcp_fuse(tcp, iphdr, tcpha);
3990 
3991 	}
3992 	/* This code follows 4.4BSD-Lite2 mostly. */
3993 	if (bytes_acked < 0)
3994 		goto est;
3995 
3996 	/*
3997 	 * If TCP is ECN capable and the congestion experience bit is
3998 	 * set, reduce tcp_cwnd and tcp_ssthresh.  But this should only be
3999 	 * done once per window (or more loosely, per RTT).
4000 	 */
4001 	if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max))
4002 		tcp->tcp_cwr = B_FALSE;
4003 	if (tcp->tcp_ecn_ok && (flags & TH_ECE) && !tcp->tcp_cwr) {
4004 		cc_cong_signal(tcp, seg_ack, CC_ECN);
4005 		/*
4006 		 * If the cwnd is 0, use the timer to clock out
4007 		 * new segments.  This is required by the ECN spec.
4008 		 */
4009 		if (tcp->tcp_cwnd == 0)
4010 			TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
4011 		tcp->tcp_cwr = B_TRUE;
4012 		/*
4013 		 * This marks the end of the current window of in
4014 		 * flight data.  That is why we don't use
4015 		 * tcp_suna + tcp_swnd.  Only data in flight can
4016 		 * provide ECN info.
4017 		 */
4018 		tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
4019 	}
4020 
4021 	mp1 = tcp->tcp_xmit_head;
4022 	if (bytes_acked == 0) {
4023 		if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) {
4024 			int dupack_cnt;
4025 
4026 			TCPS_BUMP_MIB(tcps, tcpInDupAck);
4027 			/*
4028 			 * Fast retransmit.  When we have seen exactly three
4029 			 * identical ACKs while we have unacked data
4030 			 * outstanding we take it as a hint that our peer
4031 			 * dropped something.
4032 			 *
4033 			 * If TCP is retransmitting, don't do fast retransmit.
4034 			 */
4035 			if (mp1 && tcp->tcp_suna != tcp->tcp_snxt &&
4036 			    ! tcp->tcp_rexmit) {
4037 				/* Do Limited Transmit */
4038 				if ((dupack_cnt = ++tcp->tcp_dupack_cnt) <
4039 				    tcps->tcps_dupack_fast_retransmit) {
4040 					cc_ack_received(tcp, seg_ack,
4041 					    bytes_acked, CC_DUPACK);
4042 					/*
4043 					 * RFC 3042
4044 					 *
4045 					 * What we need to do is temporarily
4046 					 * increase tcp_cwnd so that new
4047 					 * data can be sent if it is allowed
4048 					 * by the receive window (tcp_rwnd).
4049 					 * tcp_wput_data() will take care of
4050 					 * the rest.
4051 					 *
4052 					 * If the connection is SACK capable,
4053 					 * only do limited xmit when there
4054 					 * is SACK info.
4055 					 *
4056 					 * Note how tcp_cwnd is incremented.
4057 					 * The first dup ACK will increase
4058 					 * it by 1 MSS.  The second dup ACK
4059 					 * will increase it by 2 MSS.  This
4060 					 * means that only 1 new segment will
4061 					 * be sent for each dup ACK.
4062 					 */
4063 					if (tcp->tcp_unsent > 0 &&
4064 					    (!tcp->tcp_snd_sack_ok ||
4065 					    (tcp->tcp_snd_sack_ok &&
4066 					    tcp->tcp_notsack_list != NULL))) {
4067 						tcp->tcp_cwnd += mss <<
4068 						    (tcp->tcp_dupack_cnt - 1);
4069 						flags |= TH_LIMIT_XMIT;
4070 					}
4071 				} else if (dupack_cnt ==
4072 				    tcps->tcps_dupack_fast_retransmit) {
4073 
4074 				/*
4075 				 * If we have reduced tcp_ssthresh
4076 				 * because of ECN, do not reduce it again
4077 				 * unless it is already one window of data
4078 				 * away.  After one window of data, tcp_cwr
4079 				 * should then be cleared.  Note that
4080 				 * for non ECN capable connection, tcp_cwr
4081 				 * should always be false.
4082 				 *
4083 				 * Adjust cwnd since the duplicate
4084 				 * ack indicates that a packet was
4085 				 * dropped (due to congestion.)
4086 				 */
4087 				if (!tcp->tcp_cwr) {
4088 					cc_cong_signal(tcp, seg_ack,
4089 					    CC_NDUPACK);
4090 					cc_ack_received(tcp, seg_ack,
4091 					    bytes_acked, CC_DUPACK);
4092 				}
4093 				if (tcp->tcp_ecn_ok) {
4094 					tcp->tcp_cwr = B_TRUE;
4095 					tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
4096 					tcp->tcp_ecn_cwr_sent = B_FALSE;
4097 				}
4098 
4099 				/*
4100 				 * We do Hoe's algorithm.  Refer to her
4101 				 * paper "Improving the Start-up Behavior
4102 				 * of a Congestion Control Scheme for TCP,"
4103 				 * appeared in SIGCOMM'96.
4104 				 *
4105 				 * Save highest seq no we have sent so far.
4106 				 * Be careful about the invisible FIN byte.
4107 				 */
4108 				if ((tcp->tcp_valid_bits & TCP_FSS_VALID) &&
4109 				    (tcp->tcp_unsent == 0)) {
4110 					tcp->tcp_rexmit_max = tcp->tcp_fss;
4111 				} else {
4112 					tcp->tcp_rexmit_max = tcp->tcp_snxt;
4113 				}
4114 
4115 				/*
4116 				 * For SACK:
4117 				 * Calculate tcp_pipe, which is the
4118 				 * estimated number of bytes in
4119 				 * network.
4120 				 *
4121 				 * tcp_fack is the highest sack'ed seq num
4122 				 * TCP has received.
4123 				 *
4124 				 * tcp_pipe is explained in the above quoted
4125 				 * Fall and Floyd's paper.  tcp_fack is
4126 				 * explained in Mathis and Mahdavi's
4127 				 * "Forward Acknowledgment: Refining TCP
4128 				 * Congestion Control" in SIGCOMM '96.
4129 				 */
4130 				if (tcp->tcp_snd_sack_ok) {
4131 					if (tcp->tcp_notsack_list != NULL) {
4132 						tcp->tcp_pipe = tcp->tcp_snxt -
4133 						    tcp->tcp_fack;
4134 						tcp->tcp_sack_snxt = seg_ack;
4135 						flags |= TH_NEED_SACK_REXMIT;
4136 					} else {
4137 						/*
4138 						 * Always initialize tcp_pipe
4139 						 * even though we don't have
4140 						 * any SACK info.  If later
4141 						 * we get SACK info and
4142 						 * tcp_pipe is not initialized,
4143 						 * funny things will happen.
4144 						 */
4145 						tcp->tcp_pipe =
4146 						    tcp->tcp_cwnd_ssthresh;
4147 					}
4148 				} else {
4149 					flags |= TH_REXMIT_NEEDED;
4150 				} /* tcp_snd_sack_ok */
4151 
4152 				} else {
4153 					cc_ack_received(tcp, seg_ack,
4154 					    bytes_acked, CC_DUPACK);
4155 					/*
4156 					 * Here we perform congestion
4157 					 * avoidance, but NOT slow start.
4158 					 * This is known as the Fast
4159 					 * Recovery Algorithm.
4160 					 */
4161 					if (tcp->tcp_snd_sack_ok &&
4162 					    tcp->tcp_notsack_list != NULL) {
4163 						flags |= TH_NEED_SACK_REXMIT;
4164 						tcp->tcp_pipe -= mss;
4165 						if (tcp->tcp_pipe < 0)
4166 							tcp->tcp_pipe = 0;
4167 					} else {
4168 					/*
4169 					 * We know that one more packet has
4170 					 * left the pipe thus we can update
4171 					 * cwnd.
4172 					 */
4173 					cwnd = tcp->tcp_cwnd + mss;
4174 					if (cwnd > tcp->tcp_cwnd_max)
4175 						cwnd = tcp->tcp_cwnd_max;
4176 					DTRACE_PROBE3(cwnd__fast__recovery,
4177 					    tcp_t *, tcp,
4178 					    uint32_t, tcp->tcp_cwnd,
4179 					    uint32_t, cwnd);
4180 					tcp->tcp_cwnd = cwnd;
4181 					if (tcp->tcp_unsent > 0)
4182 						flags |= TH_XMIT_NEEDED;
4183 					}
4184 				}
4185 			}
4186 		} else if (tcp->tcp_zero_win_probe) {
4187 			/*
4188 			 * If the window has opened, need to arrange
4189 			 * to send additional data.
4190 			 */
4191 			if (new_swnd != 0) {
4192 				/* tcp_suna != tcp_snxt */
4193 				/* Packet contains a window update */
4194 				TCPS_BUMP_MIB(tcps, tcpInWinUpdate);
4195 				tcp->tcp_zero_win_probe = 0;
4196 				tcp->tcp_timer_backoff = 0;
4197 				tcp->tcp_ms_we_have_waited = 0;
4198 
4199 				/*
4200 				 * Transmit starting with tcp_suna since
4201 				 * the one byte probe is not ack'ed.
4202 				 * If TCP has sent more than one identical
4203 				 * probe, tcp_rexmit will be set.  That means
4204 				 * tcp_ss_rexmit() will send out the one
4205 				 * byte along with new data.  Otherwise,
4206 				 * fake the retransmission.
4207 				 */
4208 				flags |= TH_XMIT_NEEDED;
4209 				if (!tcp->tcp_rexmit) {
4210 					tcp->tcp_rexmit = B_TRUE;
4211 					tcp->tcp_dupack_cnt = 0;
4212 					tcp->tcp_rexmit_nxt = tcp->tcp_suna;
4213 					tcp->tcp_rexmit_max = tcp->tcp_suna + 1;
4214 				}
4215 			}
4216 		}
4217 		goto swnd_update;
4218 	}
4219 
4220 	/*
4221 	 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73.
4222 	 * If the ACK value acks something that we have not yet sent, it might
4223 	 * be an old duplicate segment.  Send an ACK to re-synchronize the
4224 	 * other side.
4225 	 * Note: reset in response to unacceptable ACK in SYN_RECEIVE
4226 	 * state is handled above, so we can always just drop the segment and
4227 	 * send an ACK here.
4228 	 *
4229 	 * In the case where the peer shrinks the window, we see the new window
4230 	 * update, but all the data sent previously is queued up by the peer.
4231 	 * To account for this, in tcp_process_shrunk_swnd(), the sequence
4232 	 * number, which was already sent, and within window, is recorded.
4233 	 * tcp_snxt is then updated.
4234 	 *
4235 	 * If the window has previously shrunk, and an ACK for data not yet
4236 	 * sent, according to tcp_snxt is recieved, it may still be valid. If
4237 	 * the ACK is for data within the window at the time the window was
4238 	 * shrunk, then the ACK is acceptable. In this case tcp_snxt is set to
4239 	 * the sequence number ACK'ed.
4240 	 *
4241 	 * If the ACK covers all the data sent at the time the window was
4242 	 * shrunk, we can now set tcp_is_wnd_shrnk to B_FALSE.
4243 	 *
4244 	 * Should we send ACKs in response to ACK only segments?
4245 	 */
4246 
4247 	if (SEQ_GT(seg_ack, tcp->tcp_snxt)) {
4248 		if ((tcp->tcp_is_wnd_shrnk) &&
4249 		    (SEQ_LEQ(seg_ack, tcp->tcp_snxt_shrunk))) {
4250 			uint32_t data_acked_ahead_snxt;
4251 
4252 			data_acked_ahead_snxt = seg_ack - tcp->tcp_snxt;
4253 			tcp_update_xmit_tail(tcp, seg_ack);
4254 			tcp->tcp_unsent -= data_acked_ahead_snxt;
4255 		} else {
4256 			TCPS_BUMP_MIB(tcps, tcpInAckUnsent);
4257 			/* drop the received segment */
4258 			freemsg(mp);
4259 
4260 			/*
4261 			 * Send back an ACK.  If tcp_drop_ack_unsent_cnt is
4262 			 * greater than 0, check if the number of such
4263 			 * bogus ACks is greater than that count.  If yes,
4264 			 * don't send back any ACK.  This prevents TCP from
4265 			 * getting into an ACK storm if somehow an attacker
4266 			 * successfully spoofs an acceptable segment to our
4267 			 * peer.  If this continues (count > 2 X threshold),
4268 			 * we should abort this connection.
4269 			 */
4270 			if (tcp_drop_ack_unsent_cnt > 0 &&
4271 			    ++tcp->tcp_in_ack_unsent >
4272 			    tcp_drop_ack_unsent_cnt) {
4273 				TCP_STAT(tcps, tcp_in_ack_unsent_drop);
4274 				if (tcp->tcp_in_ack_unsent > 2 *
4275 				    tcp_drop_ack_unsent_cnt) {
4276 					(void) tcp_clean_death(tcp, EPROTO);
4277 				}
4278 				return;
4279 			}
4280 			mp = tcp_ack_mp(tcp);
4281 			if (mp != NULL) {
4282 				TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
4283 				TCPS_BUMP_MIB(tcps, tcpOutAck);
4284 				tcp_send_data(tcp, mp);
4285 			}
4286 			return;
4287 		}
4288 	} else if (tcp->tcp_is_wnd_shrnk && SEQ_GEQ(seg_ack,
4289 	    tcp->tcp_snxt_shrunk)) {
4290 			tcp->tcp_is_wnd_shrnk = B_FALSE;
4291 	}
4292 
4293 	/*
4294 	 * TCP gets a new ACK, update the notsack'ed list to delete those
4295 	 * blocks that are covered by this ACK.
4296 	 */
4297 	if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) {
4298 		tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack,
4299 		    &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list));
4300 	}
4301 
4302 	/*
4303 	 * If we got an ACK after fast retransmit, check to see
4304 	 * if it is a partial ACK.  If it is not and the congestion
4305 	 * window was inflated to account for the other side's
4306 	 * cached packets, retract it.  If it is, do Hoe's algorithm.
4307 	 */
4308 	if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) {
4309 		ASSERT(tcp->tcp_rexmit == B_FALSE);
4310 		if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) {
4311 			tcp->tcp_dupack_cnt = 0;
4312 
4313 			cc_post_recovery(tcp, seg_ack);
4314 
4315 			tcp->tcp_rexmit_max = seg_ack;
4316 
4317 			/*
4318 			 * Remove all notsack info to avoid confusion with
4319 			 * the next fast retrasnmit/recovery phase.
4320 			 */
4321 			if (tcp->tcp_snd_sack_ok) {
4322 				TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list,
4323 				    tcp);
4324 			}
4325 		} else {
4326 			if (tcp->tcp_snd_sack_ok &&
4327 			    tcp->tcp_notsack_list != NULL) {
4328 				flags |= TH_NEED_SACK_REXMIT;
4329 				tcp->tcp_pipe -= mss;
4330 				if (tcp->tcp_pipe < 0)
4331 					tcp->tcp_pipe = 0;
4332 			} else {
4333 				/*
4334 				 * Hoe's algorithm:
4335 				 *
4336 				 * Retransmit the unack'ed segment and
4337 				 * restart fast recovery.  Note that we
4338 				 * need to scale back tcp_cwnd to the
4339 				 * original value when we started fast
4340 				 * recovery.  This is to prevent overly
4341 				 * aggressive behaviour in sending new
4342 				 * segments.
4343 				 */
4344 				cwnd = tcp->tcp_cwnd_ssthresh +
4345 				    tcps->tcps_dupack_fast_retransmit * mss;
4346 				DTRACE_PROBE3(cwnd__fast__retransmit__part__ack,
4347 				    tcp_t *, tcp, uint32_t, tcp->tcp_cwnd,
4348 				    uint32_t, cwnd);
4349 				tcp->tcp_cwnd = cwnd;
4350 				tcp->tcp_cwnd_cnt = tcp->tcp_cwnd;
4351 				flags |= TH_REXMIT_NEEDED;
4352 			}
4353 		}
4354 	} else {
4355 		tcp->tcp_dupack_cnt = 0;
4356 		if (tcp->tcp_rexmit) {
4357 			/*
4358 			 * TCP is retranmitting.  If the ACK ack's all
4359 			 * outstanding data, update tcp_rexmit_max and
4360 			 * tcp_rexmit_nxt.  Otherwise, update tcp_rexmit_nxt
4361 			 * to the correct value.
4362 			 *
4363 			 * Note that SEQ_LEQ() is used.  This is to avoid
4364 			 * unnecessary fast retransmit caused by dup ACKs
4365 			 * received when TCP does slow start retransmission
4366 			 * after a time out.  During this phase, TCP may
4367 			 * send out segments which are already received.
4368 			 * This causes dup ACKs to be sent back.
4369 			 */
4370 			if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) {
4371 				if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) {
4372 					tcp->tcp_rexmit_nxt = seg_ack;
4373 				}
4374 				if (seg_ack != tcp->tcp_rexmit_max) {
4375 					flags |= TH_XMIT_NEEDED;
4376 				}
4377 			} else {
4378 				tcp->tcp_rexmit = B_FALSE;
4379 				tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
4380 			}
4381 			tcp->tcp_ms_we_have_waited = 0;
4382 		}
4383 	}
4384 
4385 	TCPS_BUMP_MIB(tcps, tcpInAckSegs);
4386 	TCPS_UPDATE_MIB(tcps, tcpInAckBytes, bytes_acked);
4387 	tcp->tcp_suna = seg_ack;
4388 	if (tcp->tcp_zero_win_probe != 0) {
4389 		tcp->tcp_zero_win_probe = 0;
4390 		tcp->tcp_timer_backoff = 0;
4391 	}
4392 
4393 	/*
4394 	 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed.
4395 	 * Note that it cannot be the SYN being ack'ed.  The code flow
4396 	 * will not reach here.
4397 	 */
4398 	if (mp1 == NULL) {
4399 		goto fin_acked;
4400 	}
4401 
4402 	/*
4403 	 * Update the congestion window.
4404 	 *
4405 	 * If TCP is not ECN capable or TCP is ECN capable but the
4406 	 * congestion experience bit is not set, increase the tcp_cwnd as
4407 	 * usual.
4408 	 */
4409 	if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) {
4410 		if (IN_RECOVERY(tcp->tcp_ccv.flags)) {
4411 			EXIT_RECOVERY(tcp->tcp_ccv.flags);
4412 		}
4413 		cc_ack_received(tcp, seg_ack, bytes_acked, CC_ACK);
4414 	}
4415 
4416 	/* See if the latest urgent data has been acknowledged */
4417 	if ((tcp->tcp_valid_bits & TCP_URG_VALID) &&
4418 	    SEQ_GT(seg_ack, tcp->tcp_urg))
4419 		tcp->tcp_valid_bits &= ~TCP_URG_VALID;
4420 
4421 	/*
4422 	 * Update the RTT estimates. Note that we don't use the TCP
4423 	 * timestamp option to calculate RTT even if one is present. This is
4424 	 * because the timestamp option's resolution (CPU tick) is
4425 	 * too coarse to measure modern datacenter networks' microsecond
4426 	 * latencies. The timestamp field's resolution is limited by its
4427 	 * 4-byte width (see RFC1323), and since we always store a
4428 	 * high-resolution nanosecond presision timestamp along with the data,
4429 	 * there is no point to ever using the timestamp option.
4430 	 */
4431 	if (SEQ_GT(seg_ack, tcp->tcp_csuna)) {
4432 		/*
4433 		 * An ACK sequence we haven't seen before, so get the RTT
4434 		 * and update the RTO. But first check if the timestamp is
4435 		 * valid to use.
4436 		 */
4437 		if ((mp1->b_next != NULL) &&
4438 		    SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) {
4439 			tcp_set_rto(tcp, gethrtime() -
4440 			    (hrtime_t)(intptr_t)mp1->b_prev);
4441 		} else {
4442 			TCPS_BUMP_MIB(tcps, tcpRttNoUpdate);
4443 		}
4444 
4445 		/* Remeber the last sequence to be ACKed */
4446 		tcp->tcp_csuna = seg_ack;
4447 		if (tcp->tcp_set_timer == 1) {
4448 			TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
4449 			tcp->tcp_set_timer = 0;
4450 		}
4451 	} else {
4452 		TCPS_BUMP_MIB(tcps, tcpRttNoUpdate);
4453 	}
4454 
4455 	/* Eat acknowledged bytes off the xmit queue. */
4456 	for (;;) {
4457 		mblk_t	*mp2;
4458 		uchar_t	*wptr;
4459 
4460 		wptr = mp1->b_wptr;
4461 		ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX);
4462 		bytes_acked -= (int)(wptr - mp1->b_rptr);
4463 		if (bytes_acked < 0) {
4464 			mp1->b_rptr = wptr + bytes_acked;
4465 			/*
4466 			 * Set a new timestamp if all the bytes timed by the
4467 			 * old timestamp have been ack'ed.
4468 			 */
4469 			if (SEQ_GT(seg_ack,
4470 			    (uint32_t)(uintptr_t)(mp1->b_next))) {
4471 				mp1->b_prev =
4472 				    (mblk_t *)(intptr_t)gethrtime();
4473 				mp1->b_next = NULL;
4474 			}
4475 			break;
4476 		}
4477 		mp1->b_next = NULL;
4478 		mp1->b_prev = NULL;
4479 		mp2 = mp1;
4480 		mp1 = mp1->b_cont;
4481 
4482 		/*
4483 		 * This notification is required for some zero-copy
4484 		 * clients to maintain a copy semantic. After the data
4485 		 * is ack'ed, client is safe to modify or reuse the buffer.
4486 		 */
4487 		if (tcp->tcp_snd_zcopy_aware &&
4488 		    (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY))
4489 			tcp_zcopy_notify(tcp);
4490 		freeb(mp2);
4491 		if (bytes_acked == 0) {
4492 			if (mp1 == NULL) {
4493 				/* Everything is ack'ed, clear the tail. */
4494 				tcp->tcp_xmit_tail = NULL;
4495 				/*
4496 				 * Cancel the timer unless we are still
4497 				 * waiting for an ACK for the FIN packet.
4498 				 */
4499 				if (tcp->tcp_timer_tid != 0 &&
4500 				    tcp->tcp_snxt == tcp->tcp_suna) {
4501 					(void) TCP_TIMER_CANCEL(tcp,
4502 					    tcp->tcp_timer_tid);
4503 					tcp->tcp_timer_tid = 0;
4504 				}
4505 				goto pre_swnd_update;
4506 			}
4507 			if (mp2 != tcp->tcp_xmit_tail)
4508 				break;
4509 			tcp->tcp_xmit_tail = mp1;
4510 			ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <=
4511 			    (uintptr_t)INT_MAX);
4512 			tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr -
4513 			    mp1->b_rptr);
4514 			break;
4515 		}
4516 		if (mp1 == NULL) {
4517 			/*
4518 			 * More was acked but there is nothing more
4519 			 * outstanding.  This means that the FIN was
4520 			 * just acked or that we're talking to a clown.
4521 			 */
4522 fin_acked:
4523 			ASSERT(tcp->tcp_fin_sent);
4524 			tcp->tcp_xmit_tail = NULL;
4525 			if (tcp->tcp_fin_sent) {
4526 				/* FIN was acked - making progress */
4527 				if (!tcp->tcp_fin_acked)
4528 					tcp->tcp_ip_forward_progress = B_TRUE;
4529 				tcp->tcp_fin_acked = B_TRUE;
4530 				if (tcp->tcp_linger_tid != 0 &&
4531 				    TCP_TIMER_CANCEL(tcp,
4532 				    tcp->tcp_linger_tid) >= 0) {
4533 					tcp_stop_lingering(tcp);
4534 					freemsg(mp);
4535 					mp = NULL;
4536 				}
4537 			} else {
4538 				/*
4539 				 * We should never get here because
4540 				 * we have already checked that the
4541 				 * number of bytes ack'ed should be
4542 				 * smaller than or equal to what we
4543 				 * have sent so far (it is the
4544 				 * acceptability check of the ACK).
4545 				 * We can only get here if the send
4546 				 * queue is corrupted.
4547 				 *
4548 				 * Terminate the connection and
4549 				 * panic the system.  It is better
4550 				 * for us to panic instead of
4551 				 * continuing to avoid other disaster.
4552 				 */
4553 				tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt,
4554 				    tcp->tcp_rnxt, TH_RST|TH_ACK);
4555 				panic("Memory corruption "
4556 				    "detected for connection %s.",
4557 				    tcp_display(tcp, NULL,
4558 				    DISP_ADDR_AND_PORT));
4559 				/*NOTREACHED*/
4560 			}
4561 			goto pre_swnd_update;
4562 		}
4563 		ASSERT(mp2 != tcp->tcp_xmit_tail);
4564 	}
4565 	if (tcp->tcp_unsent) {
4566 		flags |= TH_XMIT_NEEDED;
4567 	}
4568 pre_swnd_update:
4569 	tcp->tcp_xmit_head = mp1;
4570 swnd_update:
4571 	/*
4572 	 * The following check is different from most other implementations.
4573 	 * For bi-directional transfer, when segments are dropped, the
4574 	 * "normal" check will not accept a window update in those
4575 	 * retransmitted segemnts.  Failing to do that, TCP may send out
4576 	 * segments which are outside receiver's window.  As TCP accepts
4577 	 * the ack in those retransmitted segments, if the window update in
4578 	 * the same segment is not accepted, TCP will incorrectly calculates
4579 	 * that it can send more segments.  This can create a deadlock
4580 	 * with the receiver if its window becomes zero.
4581 	 */
4582 	if (SEQ_LT(tcp->tcp_swl2, seg_ack) ||
4583 	    SEQ_LT(tcp->tcp_swl1, seg_seq) ||
4584 	    (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) {
4585 		/*
4586 		 * The criteria for update is:
4587 		 *
4588 		 * 1. the segment acknowledges some data.  Or
4589 		 * 2. the segment is new, i.e. it has a higher seq num. Or
4590 		 * 3. the segment is not old and the advertised window is
4591 		 * larger than the previous advertised window.
4592 		 */
4593 		if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd)
4594 			flags |= TH_XMIT_NEEDED;
4595 		tcp->tcp_swnd = new_swnd;
4596 		if (new_swnd > tcp->tcp_max_swnd)
4597 			tcp->tcp_max_swnd = new_swnd;
4598 		tcp->tcp_swl1 = seg_seq;
4599 		tcp->tcp_swl2 = seg_ack;
4600 	}
4601 est:
4602 	if (tcp->tcp_state > TCPS_ESTABLISHED) {
4603 
4604 		switch (tcp->tcp_state) {
4605 		case TCPS_FIN_WAIT_1:
4606 			if (tcp->tcp_fin_acked) {
4607 				tcp->tcp_state = TCPS_FIN_WAIT_2;
4608 				DTRACE_TCP6(state__change, void, NULL,
4609 				    ip_xmit_attr_t *, connp->conn_ixa,
4610 				    void, NULL, tcp_t *, tcp, void, NULL,
4611 				    int32_t, TCPS_FIN_WAIT_1);
4612 				/*
4613 				 * We implement the non-standard BSD/SunOS
4614 				 * FIN_WAIT_2 flushing algorithm.
4615 				 * If there is no user attached to this
4616 				 * TCP endpoint, then this TCP struct
4617 				 * could hang around forever in FIN_WAIT_2
4618 				 * state if the peer forgets to send us
4619 				 * a FIN.  To prevent this, we wait only
4620 				 * 2*MSL (a convenient time value) for
4621 				 * the FIN to arrive.  If it doesn't show up,
4622 				 * we flush the TCP endpoint.  This algorithm,
4623 				 * though a violation of RFC-793, has worked
4624 				 * for over 10 years in BSD systems.
4625 				 * Note: SunOS 4.x waits 675 seconds before
4626 				 * flushing the FIN_WAIT_2 connection.
4627 				 */
4628 				TCP_TIMER_RESTART(tcp,
4629 				    tcp->tcp_fin_wait_2_flush_interval);
4630 			}
4631 			break;
4632 		case TCPS_FIN_WAIT_2:
4633 			break;	/* Shutdown hook? */
4634 		case TCPS_LAST_ACK:
4635 			freemsg(mp);
4636 			if (tcp->tcp_fin_acked) {
4637 				(void) tcp_clean_death(tcp, 0);
4638 				return;
4639 			}
4640 			goto xmit_check;
4641 		case TCPS_CLOSING:
4642 			if (tcp->tcp_fin_acked) {
4643 				SET_TIME_WAIT(tcps, tcp, connp);
4644 				DTRACE_TCP6(state__change, void, NULL,
4645 				    ip_xmit_attr_t *, connp->conn_ixa, void,
4646 				    NULL, tcp_t *, tcp, void, NULL, int32_t,
4647 				    TCPS_CLOSING);
4648 			}
4649 			/*FALLTHRU*/
4650 		case TCPS_CLOSE_WAIT:
4651 			freemsg(mp);
4652 			goto xmit_check;
4653 		default:
4654 			ASSERT(tcp->tcp_state != TCPS_TIME_WAIT);
4655 			break;
4656 		}
4657 	}
4658 	if (flags & TH_FIN) {
4659 		/* Make sure we ack the fin */
4660 		flags |= TH_ACK_NEEDED;
4661 		if (!tcp->tcp_fin_rcvd) {
4662 			tcp->tcp_fin_rcvd = B_TRUE;
4663 			tcp->tcp_rnxt++;
4664 			tcpha = tcp->tcp_tcpha;
4665 			tcpha->tha_ack = htonl(tcp->tcp_rnxt);
4666 
4667 			/*
4668 			 * Generate the ordrel_ind at the end unless the
4669 			 * conn is detached or it is a STREAMS based eager.
4670 			 * In the eager case we defer the notification until
4671 			 * tcp_accept_finish has run.
4672 			 */
4673 			if (!TCP_IS_DETACHED(tcp) && (IPCL_IS_NONSTR(connp) ||
4674 			    (tcp->tcp_listener == NULL &&
4675 			    !tcp->tcp_hard_binding)))
4676 				flags |= TH_ORDREL_NEEDED;
4677 			switch (tcp->tcp_state) {
4678 			case TCPS_SYN_RCVD:
4679 				tcp->tcp_state = TCPS_CLOSE_WAIT;
4680 				DTRACE_TCP6(state__change, void, NULL,
4681 				    ip_xmit_attr_t *, connp->conn_ixa,
4682 				    void, NULL, tcp_t *, tcp, void, NULL,
4683 				    int32_t, TCPS_SYN_RCVD);
4684 				/* Keepalive? */
4685 				break;
4686 			case TCPS_ESTABLISHED:
4687 				tcp->tcp_state = TCPS_CLOSE_WAIT;
4688 				DTRACE_TCP6(state__change, void, NULL,
4689 				    ip_xmit_attr_t *, connp->conn_ixa,
4690 				    void, NULL, tcp_t *, tcp, void, NULL,
4691 				    int32_t, TCPS_ESTABLISHED);
4692 				/* Keepalive? */
4693 				break;
4694 			case TCPS_FIN_WAIT_1:
4695 				if (!tcp->tcp_fin_acked) {
4696 					tcp->tcp_state = TCPS_CLOSING;
4697 					DTRACE_TCP6(state__change, void, NULL,
4698 					    ip_xmit_attr_t *, connp->conn_ixa,
4699 					    void, NULL, tcp_t *, tcp, void,
4700 					    NULL, int32_t, TCPS_FIN_WAIT_1);
4701 					break;
4702 				}
4703 				/* FALLTHRU */
4704 			case TCPS_FIN_WAIT_2:
4705 				SET_TIME_WAIT(tcps, tcp, connp);
4706 				DTRACE_TCP6(state__change, void, NULL,
4707 				    ip_xmit_attr_t *, connp->conn_ixa, void,
4708 				    NULL, tcp_t *, tcp, void, NULL, int32_t,
4709 				    TCPS_FIN_WAIT_2);
4710 				if (seg_len) {
4711 					/*
4712 					 * implies data piggybacked on FIN.
4713 					 * break to handle data.
4714 					 */
4715 					break;
4716 				}
4717 				freemsg(mp);
4718 				goto ack_check;
4719 			}
4720 		}
4721 	}
4722 	if (mp == NULL)
4723 		goto xmit_check;
4724 	if (seg_len == 0) {
4725 		freemsg(mp);
4726 		goto xmit_check;
4727 	}
4728 	if (mp->b_rptr == mp->b_wptr) {
4729 		/*
4730 		 * The header has been consumed, so we remove the
4731 		 * zero-length mblk here.
4732 		 */
4733 		mp1 = mp;
4734 		mp = mp->b_cont;
4735 		freeb(mp1);
4736 	}
4737 update_ack:
4738 	tcpha = tcp->tcp_tcpha;
4739 	tcp->tcp_rack_cnt++;
4740 	{
4741 		uint32_t cur_max;
4742 
4743 		cur_max = tcp->tcp_rack_cur_max;
4744 		if (tcp->tcp_rack_cnt >= cur_max) {
4745 			/*
4746 			 * We have more unacked data than we should - send
4747 			 * an ACK now.
4748 			 */
4749 			flags |= TH_ACK_NEEDED;
4750 			cur_max++;
4751 			if (cur_max > tcp->tcp_rack_abs_max)
4752 				tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max;
4753 			else
4754 				tcp->tcp_rack_cur_max = cur_max;
4755 		} else if (TCP_IS_DETACHED(tcp)) {
4756 			/* We don't have an ACK timer for detached TCP. */
4757 			flags |= TH_ACK_NEEDED;
4758 		} else if (seg_len < mss) {
4759 			/*
4760 			 * If we get a segment that is less than an mss, and we
4761 			 * already have unacknowledged data, and the amount
4762 			 * unacknowledged is not a multiple of mss, then we
4763 			 * better generate an ACK now.  Otherwise, this may be
4764 			 * the tail piece of a transaction, and we would rather
4765 			 * wait for the response.
4766 			 */
4767 			uint32_t udif;
4768 			ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <=
4769 			    (uintptr_t)INT_MAX);
4770 			udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack);
4771 			if (udif && (udif % mss))
4772 				flags |= TH_ACK_NEEDED;
4773 			else
4774 				flags |= TH_ACK_TIMER_NEEDED;
4775 		} else {
4776 			/* Start delayed ack timer */
4777 			flags |= TH_ACK_TIMER_NEEDED;
4778 		}
4779 	}
4780 	tcp->tcp_rnxt += seg_len;
4781 	tcpha->tha_ack = htonl(tcp->tcp_rnxt);
4782 
4783 	if (mp == NULL)
4784 		goto xmit_check;
4785 
4786 	/* Update SACK list */
4787 	if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
4788 		tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt,
4789 		    &(tcp->tcp_num_sack_blk));
4790 	}
4791 
4792 	if (tcp->tcp_urp_mp) {
4793 		tcp->tcp_urp_mp->b_cont = mp;
4794 		mp = tcp->tcp_urp_mp;
4795 		tcp->tcp_urp_mp = NULL;
4796 		/* Ready for a new signal. */
4797 		tcp->tcp_urp_last_valid = B_FALSE;
4798 #ifdef DEBUG
4799 		(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
4800 		    "tcp_rput: sending exdata_ind %s",
4801 		    tcp_display(tcp, NULL, DISP_PORT_ONLY));
4802 #endif /* DEBUG */
4803 	}
4804 
4805 	/*
4806 	 * Check for ancillary data changes compared to last segment.
4807 	 */
4808 	if (connp->conn_recv_ancillary.crb_all != 0) {
4809 		mp = tcp_input_add_ancillary(tcp, mp, &ipp, ira);
4810 		if (mp == NULL)
4811 			return;
4812 	}
4813 
4814 	if (IPCL_IS_NONSTR(connp)) {
4815 		/*
4816 		 * Non-STREAMS socket
4817 		 */
4818 		boolean_t push = flags & (TH_PUSH|TH_FIN);
4819 		int error;
4820 
4821 		if ((*sockupcalls->su_recv)(connp->conn_upper_handle,
4822 		    mp, seg_len, 0, &error, &push) <= 0) {
4823 			/*
4824 			 * We should never be in middle of a
4825 			 * fallback, the squeue guarantees that.
4826 			 */
4827 			ASSERT(error != EOPNOTSUPP);
4828 			if (error == ENOSPC)
4829 				tcp->tcp_rwnd -= seg_len;
4830 		} else if (push) {
4831 			/* PUSH bit set and sockfs is not flow controlled */
4832 			flags |= tcp_rwnd_reopen(tcp);
4833 		}
4834 	} else if (tcp->tcp_listener != NULL || tcp->tcp_hard_binding) {
4835 		/*
4836 		 * Side queue inbound data until the accept happens.
4837 		 * tcp_accept/tcp_rput drains this when the accept happens.
4838 		 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or
4839 		 * T_EXDATA_IND) it is queued on b_next.
4840 		 * XXX Make urgent data use this. Requires:
4841 		 *	Removing tcp_listener check for TH_URG
4842 		 *	Making M_PCPROTO and MARK messages skip the eager case
4843 		 */
4844 
4845 		tcp_rcv_enqueue(tcp, mp, seg_len, ira->ira_cred);
4846 	} else {
4847 		/* Active STREAMS socket */
4848 		if (mp->b_datap->db_type != M_DATA ||
4849 		    (flags & TH_MARKNEXT_NEEDED)) {
4850 			if (tcp->tcp_rcv_list != NULL) {
4851 				flags |= tcp_rcv_drain(tcp);
4852 			}
4853 			ASSERT(tcp->tcp_rcv_list == NULL ||
4854 			    tcp->tcp_fused_sigurg);
4855 
4856 			if (flags & TH_MARKNEXT_NEEDED) {
4857 #ifdef DEBUG
4858 				(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
4859 				    "tcp_rput: sending MSGMARKNEXT %s",
4860 				    tcp_display(tcp, NULL,
4861 				    DISP_PORT_ONLY));
4862 #endif /* DEBUG */
4863 				mp->b_flag |= MSGMARKNEXT;
4864 				flags &= ~TH_MARKNEXT_NEEDED;
4865 			}
4866 
4867 			if (is_system_labeled())
4868 				tcp_setcred_data(mp, ira);
4869 
4870 			putnext(connp->conn_rq, mp);
4871 			if (!canputnext(connp->conn_rq))
4872 				tcp->tcp_rwnd -= seg_len;
4873 		} else if ((flags & (TH_PUSH|TH_FIN)) ||
4874 		    tcp->tcp_rcv_cnt + seg_len >= connp->conn_rcvbuf >> 3) {
4875 			if (tcp->tcp_rcv_list != NULL) {
4876 				/*
4877 				 * Enqueue the new segment first and then
4878 				 * call tcp_rcv_drain() to send all data
4879 				 * up.  The other way to do this is to
4880 				 * send all queued data up and then call
4881 				 * putnext() to send the new segment up.
4882 				 * This way can remove the else part later
4883 				 * on.
4884 				 *
4885 				 * We don't do this to avoid one more call to
4886 				 * canputnext() as tcp_rcv_drain() needs to
4887 				 * call canputnext().
4888 				 */
4889 				tcp_rcv_enqueue(tcp, mp, seg_len,
4890 				    ira->ira_cred);
4891 				flags |= tcp_rcv_drain(tcp);
4892 			} else {
4893 				if (is_system_labeled())
4894 					tcp_setcred_data(mp, ira);
4895 
4896 				putnext(connp->conn_rq, mp);
4897 				if (!canputnext(connp->conn_rq))
4898 					tcp->tcp_rwnd -= seg_len;
4899 			}
4900 		} else {
4901 			/*
4902 			 * Enqueue all packets when processing an mblk
4903 			 * from the co queue and also enqueue normal packets.
4904 			 */
4905 			tcp_rcv_enqueue(tcp, mp, seg_len, ira->ira_cred);
4906 		}
4907 		/*
4908 		 * Make sure the timer is running if we have data waiting
4909 		 * for a push bit. This provides resiliency against
4910 		 * implementations that do not correctly generate push bits.
4911 		 */
4912 		if (tcp->tcp_rcv_list != NULL && tcp->tcp_push_tid == 0) {
4913 			/*
4914 			 * The connection may be closed at this point, so don't
4915 			 * do anything for a detached tcp.
4916 			 */
4917 			if (!TCP_IS_DETACHED(tcp))
4918 				tcp->tcp_push_tid = TCP_TIMER(tcp,
4919 				    tcp_push_timer,
4920 				    tcps->tcps_push_timer_interval);
4921 		}
4922 	}
4923 
4924 xmit_check:
4925 	/* Is there anything left to do? */
4926 	ASSERT(!(flags & TH_MARKNEXT_NEEDED));
4927 	if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED|
4928 	    TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED|
4929 	    TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0)
4930 		goto done;
4931 
4932 	/* Any transmit work to do and a non-zero window? */
4933 	if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT|
4934 	    TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) {
4935 		if (flags & TH_REXMIT_NEEDED) {
4936 			uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna;
4937 
4938 			TCPS_BUMP_MIB(tcps, tcpOutFastRetrans);
4939 			if (snd_size > mss)
4940 				snd_size = mss;
4941 			if (snd_size > tcp->tcp_swnd)
4942 				snd_size = tcp->tcp_swnd;
4943 			mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size,
4944 			    NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size,
4945 			    B_TRUE);
4946 
4947 			if (mp1 != NULL) {
4948 				tcp->tcp_xmit_head->b_prev =
4949 				    (mblk_t *)(intptr_t)gethrtime();
4950 				tcp->tcp_csuna = tcp->tcp_snxt;
4951 				TCPS_BUMP_MIB(tcps, tcpRetransSegs);
4952 				TCPS_UPDATE_MIB(tcps, tcpRetransBytes,
4953 				    snd_size);
4954 				tcp->tcp_cs.tcp_out_retrans_segs++;
4955 				tcp->tcp_cs.tcp_out_retrans_bytes += snd_size;
4956 				tcp_send_data(tcp, mp1);
4957 			}
4958 		}
4959 		if (flags & TH_NEED_SACK_REXMIT) {
4960 			tcp_sack_rexmit(tcp, &flags);
4961 		}
4962 		/*
4963 		 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send
4964 		 * out new segment.  Note that tcp_rexmit should not be
4965 		 * set, otherwise TH_LIMIT_XMIT should not be set.
4966 		 */
4967 		if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) {
4968 			if (!tcp->tcp_rexmit) {
4969 				tcp_wput_data(tcp, NULL, B_FALSE);
4970 			} else {
4971 				tcp_ss_rexmit(tcp);
4972 			}
4973 		}
4974 		/*
4975 		 * Adjust tcp_cwnd back to normal value after sending
4976 		 * new data segments.
4977 		 */
4978 		if (flags & TH_LIMIT_XMIT) {
4979 			tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1);
4980 			/*
4981 			 * This will restart the timer.  Restarting the
4982 			 * timer is used to avoid a timeout before the
4983 			 * limited transmitted segment's ACK gets back.
4984 			 */
4985 			if (tcp->tcp_xmit_head != NULL) {
4986 				tcp->tcp_xmit_head->b_prev =
4987 				    (mblk_t *)(intptr_t)gethrtime();
4988 			}
4989 		}
4990 
4991 		/* Anything more to do? */
4992 		if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED|
4993 		    TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0)
4994 			goto done;
4995 	}
4996 ack_check:
4997 	if (flags & TH_SEND_URP_MARK) {
4998 		ASSERT(tcp->tcp_urp_mark_mp);
4999 		ASSERT(!IPCL_IS_NONSTR(connp));
5000 		/*
5001 		 * Send up any queued data and then send the mark message
5002 		 */
5003 		if (tcp->tcp_rcv_list != NULL) {
5004 			flags |= tcp_rcv_drain(tcp);
5005 
5006 		}
5007 		ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg);
5008 		mp1 = tcp->tcp_urp_mark_mp;
5009 		tcp->tcp_urp_mark_mp = NULL;
5010 		if (is_system_labeled())
5011 			tcp_setcred_data(mp1, ira);
5012 
5013 		putnext(connp->conn_rq, mp1);
5014 #ifdef DEBUG
5015 		(void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
5016 		    "tcp_rput: sending zero-length %s %s",
5017 		    ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" :
5018 		    "MSGNOTMARKNEXT"),
5019 		    tcp_display(tcp, NULL, DISP_PORT_ONLY));
5020 #endif /* DEBUG */
5021 		flags &= ~TH_SEND_URP_MARK;
5022 	}
5023 	if (flags & TH_ACK_NEEDED) {
5024 		/*
5025 		 * Time to send an ack for some reason.
5026 		 */
5027 		mp1 = tcp_ack_mp(tcp);
5028 
5029 		if (mp1 != NULL) {
5030 			tcp_send_data(tcp, mp1);
5031 			TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
5032 			TCPS_BUMP_MIB(tcps, tcpOutAck);
5033 		}
5034 		if (tcp->tcp_ack_tid != 0) {
5035 			(void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid);
5036 			tcp->tcp_ack_tid = 0;
5037 		}
5038 	}
5039 	if (flags & TH_ACK_TIMER_NEEDED) {
5040 		/*
5041 		 * Arrange for deferred ACK or push wait timeout.
5042 		 * Start timer if it is not already running.
5043 		 */
5044 		if (tcp->tcp_ack_tid == 0) {
5045 			tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer,
5046 			    tcp->tcp_localnet ?
5047 			    tcps->tcps_local_dack_interval :
5048 			    tcps->tcps_deferred_ack_interval);
5049 		}
5050 	}
5051 	if (flags & TH_ORDREL_NEEDED) {
5052 		/*
5053 		 * Notify upper layer about an orderly release. If this is
5054 		 * a non-STREAMS socket, then just make an upcall. For STREAMS
5055 		 * we send up an ordrel_ind, unless this is an eager, in which
5056 		 * case the ordrel will be sent when tcp_accept_finish runs.
5057 		 * Note that for non-STREAMS we make an upcall even if it is an
5058 		 * eager, because we have an upper handle to send it to.
5059 		 */
5060 		ASSERT(IPCL_IS_NONSTR(connp) || tcp->tcp_listener == NULL);
5061 		ASSERT(!tcp->tcp_detached);
5062 
5063 		if (IPCL_IS_NONSTR(connp)) {
5064 			ASSERT(tcp->tcp_ordrel_mp == NULL);
5065 			tcp->tcp_ordrel_done = B_TRUE;
5066 			(*sockupcalls->su_opctl)(connp->conn_upper_handle,
5067 			    SOCK_OPCTL_SHUT_RECV, 0);
5068 			goto done;
5069 		}
5070 
5071 		if (tcp->tcp_rcv_list != NULL) {
5072 			/*
5073 			 * Push any mblk(s) enqueued from co processing.
5074 			 */
5075 			flags |= tcp_rcv_drain(tcp);
5076 		}
5077 		ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg);
5078 
5079 		mp1 = tcp->tcp_ordrel_mp;
5080 		tcp->tcp_ordrel_mp = NULL;
5081 		tcp->tcp_ordrel_done = B_TRUE;
5082 		putnext(connp->conn_rq, mp1);
5083 	}
5084 done:
5085 	ASSERT(!(flags & TH_MARKNEXT_NEEDED));
5086 }
5087 
5088 /*
5089  * Attach ancillary data to a received TCP segments for the
5090  * ancillary pieces requested by the application that are
5091  * different than they were in the previous data segment.
5092  *
5093  * Save the "current" values once memory allocation is ok so that
5094  * when memory allocation fails we can just wait for the next data segment.
5095  */
5096 static mblk_t *
5097 tcp_input_add_ancillary(tcp_t *tcp, mblk_t *mp, ip_pkt_t *ipp,
5098     ip_recv_attr_t *ira)
5099 {
5100 	struct T_optdata_ind *todi;
5101 	int optlen;
5102 	uchar_t *optptr;
5103 	struct T_opthdr *toh;
5104 	crb_t addflag;	/* Which pieces to add */
5105 	mblk_t *mp1;
5106 	conn_t	*connp = tcp->tcp_connp;
5107 
5108 	optlen = 0;
5109 	addflag.crb_all = 0;
5110 	/* If app asked for pktinfo and the index has changed ... */
5111 	if (connp->conn_recv_ancillary.crb_ip_recvpktinfo &&
5112 	    ira->ira_ruifindex != tcp->tcp_recvifindex) {
5113 		optlen += sizeof (struct T_opthdr) +
5114 		    sizeof (struct in6_pktinfo);
5115 		addflag.crb_ip_recvpktinfo = 1;
5116 	}
5117 	/* If app asked for hoplimit and it has changed ... */
5118 	if (connp->conn_recv_ancillary.crb_ipv6_recvhoplimit &&
5119 	    ipp->ipp_hoplimit != tcp->tcp_recvhops) {
5120 		optlen += sizeof (struct T_opthdr) + sizeof (uint_t);
5121 		addflag.crb_ipv6_recvhoplimit = 1;
5122 	}
5123 	/* If app asked for tclass and it has changed ... */
5124 	if (connp->conn_recv_ancillary.crb_ipv6_recvtclass &&
5125 	    ipp->ipp_tclass != tcp->tcp_recvtclass) {
5126 		optlen += sizeof (struct T_opthdr) + sizeof (uint_t);
5127 		addflag.crb_ipv6_recvtclass = 1;
5128 	}
5129 	/*
5130 	 * If app asked for hopbyhop headers and it has changed ...
5131 	 * For security labels, note that (1) security labels can't change on
5132 	 * a connected socket at all, (2) we're connected to at most one peer,
5133 	 * (3) if anything changes, then it must be some other extra option.
5134 	 */
5135 	if (connp->conn_recv_ancillary.crb_ipv6_recvhopopts &&
5136 	    ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen,
5137 	    (ipp->ipp_fields & IPPF_HOPOPTS),
5138 	    ipp->ipp_hopopts, ipp->ipp_hopoptslen)) {
5139 		optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen;
5140 		addflag.crb_ipv6_recvhopopts = 1;
5141 		if (!ip_allocbuf((void **)&tcp->tcp_hopopts,
5142 		    &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS),
5143 		    ipp->ipp_hopopts, ipp->ipp_hopoptslen))
5144 			return (mp);
5145 	}
5146 	/* If app asked for dst headers before routing headers ... */
5147 	if (connp->conn_recv_ancillary.crb_ipv6_recvrthdrdstopts &&
5148 	    ip_cmpbuf(tcp->tcp_rthdrdstopts, tcp->tcp_rthdrdstoptslen,
5149 	    (ipp->ipp_fields & IPPF_RTHDRDSTOPTS),
5150 	    ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen)) {
5151 		optlen += sizeof (struct T_opthdr) +
5152 		    ipp->ipp_rthdrdstoptslen;
5153 		addflag.crb_ipv6_recvrthdrdstopts = 1;
5154 		if (!ip_allocbuf((void **)&tcp->tcp_rthdrdstopts,
5155 		    &tcp->tcp_rthdrdstoptslen,
5156 		    (ipp->ipp_fields & IPPF_RTHDRDSTOPTS),
5157 		    ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen))
5158 			return (mp);
5159 	}
5160 	/* If app asked for routing headers and it has changed ... */
5161 	if (connp->conn_recv_ancillary.crb_ipv6_recvrthdr &&
5162 	    ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen,
5163 	    (ipp->ipp_fields & IPPF_RTHDR),
5164 	    ipp->ipp_rthdr, ipp->ipp_rthdrlen)) {
5165 		optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen;
5166 		addflag.crb_ipv6_recvrthdr = 1;
5167 		if (!ip_allocbuf((void **)&tcp->tcp_rthdr,
5168 		    &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR),
5169 		    ipp->ipp_rthdr, ipp->ipp_rthdrlen))
5170 			return (mp);
5171 	}
5172 	/* If app asked for dest headers and it has changed ... */
5173 	if ((connp->conn_recv_ancillary.crb_ipv6_recvdstopts ||
5174 	    connp->conn_recv_ancillary.crb_old_ipv6_recvdstopts) &&
5175 	    ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen,
5176 	    (ipp->ipp_fields & IPPF_DSTOPTS),
5177 	    ipp->ipp_dstopts, ipp->ipp_dstoptslen)) {
5178 		optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen;
5179 		addflag.crb_ipv6_recvdstopts = 1;
5180 		if (!ip_allocbuf((void **)&tcp->tcp_dstopts,
5181 		    &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS),
5182 		    ipp->ipp_dstopts, ipp->ipp_dstoptslen))
5183 			return (mp);
5184 	}
5185 
5186 	if (optlen == 0) {
5187 		/* Nothing to add */
5188 		return (mp);
5189 	}
5190 	mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED);
5191 	if (mp1 == NULL) {
5192 		/*
5193 		 * Defer sending ancillary data until the next TCP segment
5194 		 * arrives.
5195 		 */
5196 		return (mp);
5197 	}
5198 	mp1->b_cont = mp;
5199 	mp = mp1;
5200 	mp->b_wptr += sizeof (*todi) + optlen;
5201 	mp->b_datap->db_type = M_PROTO;
5202 	todi = (struct T_optdata_ind *)mp->b_rptr;
5203 	todi->PRIM_type = T_OPTDATA_IND;
5204 	todi->DATA_flag = 1;	/* MORE data */
5205 	todi->OPT_length = optlen;
5206 	todi->OPT_offset = sizeof (*todi);
5207 	optptr = (uchar_t *)&todi[1];
5208 	/*
5209 	 * If app asked for pktinfo and the index has changed ...
5210 	 * Note that the local address never changes for the connection.
5211 	 */
5212 	if (addflag.crb_ip_recvpktinfo) {
5213 		struct in6_pktinfo *pkti;
5214 		uint_t ifindex;
5215 
5216 		ifindex = ira->ira_ruifindex;
5217 		toh = (struct T_opthdr *)optptr;
5218 		toh->level = IPPROTO_IPV6;
5219 		toh->name = IPV6_PKTINFO;
5220 		toh->len = sizeof (*toh) + sizeof (*pkti);
5221 		toh->status = 0;
5222 		optptr += sizeof (*toh);
5223 		pkti = (struct in6_pktinfo *)optptr;
5224 		pkti->ipi6_addr = connp->conn_laddr_v6;
5225 		pkti->ipi6_ifindex = ifindex;
5226 		optptr += sizeof (*pkti);
5227 		ASSERT(OK_32PTR(optptr));
5228 		/* Save as "last" value */
5229 		tcp->tcp_recvifindex = ifindex;
5230 	}
5231 	/* If app asked for hoplimit and it has changed ... */
5232 	if (addflag.crb_ipv6_recvhoplimit) {
5233 		toh = (struct T_opthdr *)optptr;
5234 		toh->level = IPPROTO_IPV6;
5235 		toh->name = IPV6_HOPLIMIT;
5236 		toh->len = sizeof (*toh) + sizeof (uint_t);
5237 		toh->status = 0;
5238 		optptr += sizeof (*toh);
5239 		*(uint_t *)optptr = ipp->ipp_hoplimit;
5240 		optptr += sizeof (uint_t);
5241 		ASSERT(OK_32PTR(optptr));
5242 		/* Save as "last" value */
5243 		tcp->tcp_recvhops = ipp->ipp_hoplimit;
5244 	}
5245 	/* If app asked for tclass and it has changed ... */
5246 	if (addflag.crb_ipv6_recvtclass) {
5247 		toh = (struct T_opthdr *)optptr;
5248 		toh->level = IPPROTO_IPV6;
5249 		toh->name = IPV6_TCLASS;
5250 		toh->len = sizeof (*toh) + sizeof (uint_t);
5251 		toh->status = 0;
5252 		optptr += sizeof (*toh);
5253 		*(uint_t *)optptr = ipp->ipp_tclass;
5254 		optptr += sizeof (uint_t);
5255 		ASSERT(OK_32PTR(optptr));
5256 		/* Save as "last" value */
5257 		tcp->tcp_recvtclass = ipp->ipp_tclass;
5258 	}
5259 	if (addflag.crb_ipv6_recvhopopts) {
5260 		toh = (struct T_opthdr *)optptr;
5261 		toh->level = IPPROTO_IPV6;
5262 		toh->name = IPV6_HOPOPTS;
5263 		toh->len = sizeof (*toh) + ipp->ipp_hopoptslen;
5264 		toh->status = 0;
5265 		optptr += sizeof (*toh);
5266 		bcopy((uchar_t *)ipp->ipp_hopopts, optptr, ipp->ipp_hopoptslen);
5267 		optptr += ipp->ipp_hopoptslen;
5268 		ASSERT(OK_32PTR(optptr));
5269 		/* Save as last value */
5270 		ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen,
5271 		    (ipp->ipp_fields & IPPF_HOPOPTS),
5272 		    ipp->ipp_hopopts, ipp->ipp_hopoptslen);
5273 	}
5274 	if (addflag.crb_ipv6_recvrthdrdstopts) {
5275 		toh = (struct T_opthdr *)optptr;
5276 		toh->level = IPPROTO_IPV6;
5277 		toh->name = IPV6_RTHDRDSTOPTS;
5278 		toh->len = sizeof (*toh) + ipp->ipp_rthdrdstoptslen;
5279 		toh->status = 0;
5280 		optptr += sizeof (*toh);
5281 		bcopy(ipp->ipp_rthdrdstopts, optptr, ipp->ipp_rthdrdstoptslen);
5282 		optptr += ipp->ipp_rthdrdstoptslen;
5283 		ASSERT(OK_32PTR(optptr));
5284 		/* Save as last value */
5285 		ip_savebuf((void **)&tcp->tcp_rthdrdstopts,
5286 		    &tcp->tcp_rthdrdstoptslen,
5287 		    (ipp->ipp_fields & IPPF_RTHDRDSTOPTS),
5288 		    ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen);
5289 	}
5290 	if (addflag.crb_ipv6_recvrthdr) {
5291 		toh = (struct T_opthdr *)optptr;
5292 		toh->level = IPPROTO_IPV6;
5293 		toh->name = IPV6_RTHDR;
5294 		toh->len = sizeof (*toh) + ipp->ipp_rthdrlen;
5295 		toh->status = 0;
5296 		optptr += sizeof (*toh);
5297 		bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen);
5298 		optptr += ipp->ipp_rthdrlen;
5299 		ASSERT(OK_32PTR(optptr));
5300 		/* Save as last value */
5301 		ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen,
5302 		    (ipp->ipp_fields & IPPF_RTHDR),
5303 		    ipp->ipp_rthdr, ipp->ipp_rthdrlen);
5304 	}
5305 	if (addflag.crb_ipv6_recvdstopts) {
5306 		toh = (struct T_opthdr *)optptr;
5307 		toh->level = IPPROTO_IPV6;
5308 		toh->name = IPV6_DSTOPTS;
5309 		toh->len = sizeof (*toh) + ipp->ipp_dstoptslen;
5310 		toh->status = 0;
5311 		optptr += sizeof (*toh);
5312 		bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen);
5313 		optptr += ipp->ipp_dstoptslen;
5314 		ASSERT(OK_32PTR(optptr));
5315 		/* Save as last value */
5316 		ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen,
5317 		    (ipp->ipp_fields & IPPF_DSTOPTS),
5318 		    ipp->ipp_dstopts, ipp->ipp_dstoptslen);
5319 	}
5320 	ASSERT(optptr == mp->b_wptr);
5321 	return (mp);
5322 }
5323 
5324 /* The minimum of smoothed mean deviation in RTO calculation (nsec). */
5325 #define	TCP_SD_MIN	400000000
5326 
5327 /*
5328  * Set RTO for this connection based on a new round-trip time measurement.
5329  * The formula is from Jacobson and Karels' "Congestion Avoidance and Control"
5330  * in SIGCOMM '88.  The variable names are the same as those in Appendix A.2
5331  * of that paper.
5332  *
5333  * m = new measurement
5334  * sa = smoothed RTT average (8 * average estimates).
5335  * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates).
5336  */
5337 static void
5338 tcp_set_rto(tcp_t *tcp, hrtime_t rtt)
5339 {
5340 	hrtime_t m = rtt;
5341 	hrtime_t sa = tcp->tcp_rtt_sa;
5342 	hrtime_t sv = tcp->tcp_rtt_sd;
5343 	tcp_stack_t *tcps = tcp->tcp_tcps;
5344 
5345 	TCPS_BUMP_MIB(tcps, tcpRttUpdate);
5346 	tcp->tcp_rtt_update++;
5347 	tcp->tcp_rtt_sum += m;
5348 	tcp->tcp_rtt_cnt++;
5349 
5350 	/* tcp_rtt_sa is not 0 means this is a new sample. */
5351 	if (sa != 0) {
5352 		/*
5353 		 * Update average estimator (see section 2.3 of RFC6298):
5354 		 *	SRTT = 7/8 SRTT + 1/8 rtt
5355 		 *
5356 		 * We maintain tcp_rtt_sa as 8 * SRTT, so this reduces to:
5357 		 *	tcp_rtt_sa = 7 * SRTT + rtt
5358 		 *	tcp_rtt_sa = 7 * (tcp_rtt_sa / 8) + rtt
5359 		 *	tcp_rtt_sa = tcp_rtt_sa - (tcp_rtt_sa / 8) + rtt
5360 		 *	tcp_rtt_sa = tcp_rtt_sa + (rtt - (tcp_rtt_sa / 8))
5361 		 *	tcp_rtt_sa = tcp_rtt_sa + (rtt - (tcp_rtt_sa / 2^3))
5362 		 *	tcp_rtt_sa = tcp_rtt_sa + (rtt - (tcp_rtt_sa >> 3))
5363 		 *
5364 		 * (rtt - tcp_rtt_sa / 8) is simply the difference
5365 		 * between the new rtt measurement and the existing smoothed
5366 		 * RTT average. This is referred to as "Error" in subsequent
5367 		 * calculations.
5368 		 */
5369 
5370 		/* m is now Error. */
5371 		m -= sa >> 3;
5372 		if ((sa += m) <= 0) {
5373 			/*
5374 			 * Don't allow the smoothed average to be negative.
5375 			 * We use 0 to denote reinitialization of the
5376 			 * variables.
5377 			 */
5378 			sa = 1;
5379 		}
5380 
5381 		/*
5382 		 * Update deviation estimator:
5383 		 *  mdev = 3/4 mdev + 1/4 abs(Error)
5384 		 *
5385 		 * We maintain tcp_rtt_sd as 4 * mdev, so this reduces to:
5386 		 *  tcp_rtt_sd = 3 * mdev + abs(Error)
5387 		 *  tcp_rtt_sd = tcp_rtt_sd - (tcp_rtt_sd / 4) + abs(Error)
5388 		 *  tcp_rtt_sd = tcp_rtt_sd - (tcp_rtt_sd / 2^2) + abs(Error)
5389 		 *  tcp_rtt_sd = tcp_rtt_sd - (tcp_rtt_sd >> 2) + abs(Error)
5390 		 */
5391 		if (m < 0)
5392 			m = -m;
5393 		m -= sv >> 2;
5394 		sv += m;
5395 	} else {
5396 		/*
5397 		 * This follows BSD's implementation.  So the reinitialized
5398 		 * RTO is 3 * m.  We cannot go less than 2 because if the
5399 		 * link is bandwidth dominated, doubling the window size
5400 		 * during slow start means doubling the RTT.  We want to be
5401 		 * more conservative when we reinitialize our estimates.  3
5402 		 * is just a convenient number.
5403 		 */
5404 		sa = m << 3;
5405 		sv = m << 1;
5406 	}
5407 	if (sv < TCP_SD_MIN) {
5408 		/*
5409 		 * Since a receiver doesn't delay its ACKs during a long run of
5410 		 * segments, sa may not have captured the effect of delayed ACK
5411 		 * timeouts on the RTT.  To make sure we always account for the
5412 		 * possible delay (and avoid the unnecessary retransmission),
5413 		 * TCP_SD_MIN is set to 400ms, twice the delayed ACK timeout of
5414 		 * 200ms on older SunOS/BSD systems and modern Windows systems
5415 		 * (as of 2019).  This means that the minimum possible mean
5416 		 * deviation is 100 ms.
5417 		 */
5418 		sv = TCP_SD_MIN;
5419 	}
5420 	tcp->tcp_rtt_sa = sa;
5421 	tcp->tcp_rtt_sd = sv;
5422 
5423 	tcp->tcp_rto = tcp_calculate_rto(tcp, tcps, 0);
5424 
5425 	/* Now, we can reset tcp_timer_backoff to use the new RTO... */
5426 	tcp->tcp_timer_backoff = 0;
5427 }
5428 
5429 /*
5430  * On a labeled system we have some protocols above TCP, such as RPC, which
5431  * appear to assume that every mblk in a chain has a db_credp.
5432  */
5433 static void
5434 tcp_setcred_data(mblk_t *mp, ip_recv_attr_t *ira)
5435 {
5436 	ASSERT(is_system_labeled());
5437 	ASSERT(ira->ira_cred != NULL);
5438 
5439 	while (mp != NULL) {
5440 		mblk_setcred(mp, ira->ira_cred, NOPID);
5441 		mp = mp->b_cont;
5442 	}
5443 }
5444 
5445 uint_t
5446 tcp_rwnd_reopen(tcp_t *tcp)
5447 {
5448 	uint_t ret = 0;
5449 	uint_t thwin;
5450 	conn_t *connp = tcp->tcp_connp;
5451 
5452 	/* Learn the latest rwnd information that we sent to the other side. */
5453 	thwin = ((uint_t)ntohs(tcp->tcp_tcpha->tha_win))
5454 	    << tcp->tcp_rcv_ws;
5455 	/* This is peer's calculated send window (our receive window). */
5456 	thwin -= tcp->tcp_rnxt - tcp->tcp_rack;
5457 	/*
5458 	 * Increase the receive window to max.  But we need to do receiver
5459 	 * SWS avoidance.  This means that we need to check the increase of
5460 	 * of receive window is at least 1 MSS.
5461 	 */
5462 	if (connp->conn_rcvbuf - thwin >= tcp->tcp_mss) {
5463 		/*
5464 		 * If the window that the other side knows is less than max
5465 		 * deferred acks segments, send an update immediately.
5466 		 */
5467 		if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) {
5468 			TCPS_BUMP_MIB(tcp->tcp_tcps, tcpOutWinUpdate);
5469 			ret = TH_ACK_NEEDED;
5470 		}
5471 		tcp->tcp_rwnd = connp->conn_rcvbuf;
5472 	}
5473 	return (ret);
5474 }
5475 
5476 /*
5477  * Handle a packet that has been reclassified by TCP.
5478  * This function drops the ref on connp that the caller had.
5479  */
5480 void
5481 tcp_reinput(conn_t *connp, mblk_t *mp, ip_recv_attr_t *ira, ip_stack_t *ipst)
5482 {
5483 	ipsec_stack_t	*ipss = ipst->ips_netstack->netstack_ipsec;
5484 
5485 	if (connp->conn_incoming_ifindex != 0 &&
5486 	    connp->conn_incoming_ifindex != ira->ira_ruifindex) {
5487 		freemsg(mp);
5488 		CONN_DEC_REF(connp);
5489 		return;
5490 	}
5491 
5492 	if (CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss) ||
5493 	    (ira->ira_flags & IRAF_IPSEC_SECURE)) {
5494 		ip6_t *ip6h;
5495 		ipha_t *ipha;
5496 
5497 		if (ira->ira_flags & IRAF_IS_IPV4) {
5498 			ipha = (ipha_t *)mp->b_rptr;
5499 			ip6h = NULL;
5500 		} else {
5501 			ipha = NULL;
5502 			ip6h = (ip6_t *)mp->b_rptr;
5503 		}
5504 		mp = ipsec_check_inbound_policy(mp, connp, ipha, ip6h, ira);
5505 		if (mp == NULL) {
5506 			BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
5507 			/* Note that mp is NULL */
5508 			ip_drop_input("ipIfStatsInDiscards", mp, NULL);
5509 			CONN_DEC_REF(connp);
5510 			return;
5511 		}
5512 	}
5513 
5514 	if (IPCL_IS_TCP(connp)) {
5515 		/*
5516 		 * do not drain, certain use cases can blow
5517 		 * the stack
5518 		 */
5519 		SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
5520 		    connp->conn_recv, connp, ira,
5521 		    SQ_NODRAIN, SQTAG_IP_TCP_INPUT);
5522 	} else {
5523 		/* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
5524 		(connp->conn_recv)(connp, mp, NULL,
5525 		    ira);
5526 		CONN_DEC_REF(connp);
5527 	}
5528 
5529 }
5530 
5531 /* ARGSUSED */
5532 static void
5533 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
5534 {
5535 	conn_t	*connp = (conn_t *)arg;
5536 	tcp_t	*tcp = connp->conn_tcp;
5537 	queue_t	*q = connp->conn_rq;
5538 
5539 	ASSERT(!IPCL_IS_NONSTR(connp));
5540 	mutex_enter(&tcp->tcp_rsrv_mp_lock);
5541 	tcp->tcp_rsrv_mp = mp;
5542 	mutex_exit(&tcp->tcp_rsrv_mp_lock);
5543 
5544 	if (TCP_IS_DETACHED(tcp) || q == NULL) {
5545 		return;
5546 	}
5547 
5548 	if (tcp->tcp_fused) {
5549 		tcp_fuse_backenable(tcp);
5550 		return;
5551 	}
5552 
5553 	if (canputnext(q)) {
5554 		/* Not flow-controlled, open rwnd */
5555 		tcp->tcp_rwnd = connp->conn_rcvbuf;
5556 
5557 		/*
5558 		 * Send back a window update immediately if TCP is above
5559 		 * ESTABLISHED state and the increase of the rcv window
5560 		 * that the other side knows is at least 1 MSS after flow
5561 		 * control is lifted.
5562 		 */
5563 		if (tcp->tcp_state >= TCPS_ESTABLISHED &&
5564 		    tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) {
5565 			tcp_xmit_ctl(NULL, tcp,
5566 			    (tcp->tcp_swnd == 0) ? tcp->tcp_suna :
5567 			    tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK);
5568 		}
5569 	}
5570 }
5571 
5572 /*
5573  * The read side service routine is called mostly when we get back-enabled as a
5574  * result of flow control relief.  Since we don't actually queue anything in
5575  * TCP, we have no data to send out of here.  What we do is clear the receive
5576  * window, and send out a window update.
5577  */
5578 int
5579 tcp_rsrv(queue_t *q)
5580 {
5581 	conn_t		*connp = Q_TO_CONN(q);
5582 	tcp_t		*tcp = connp->conn_tcp;
5583 	mblk_t		*mp;
5584 
5585 	/* No code does a putq on the read side */
5586 	ASSERT(q->q_first == NULL);
5587 
5588 	/*
5589 	 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already
5590 	 * been run.  So just return.
5591 	 */
5592 	mutex_enter(&tcp->tcp_rsrv_mp_lock);
5593 	if ((mp = tcp->tcp_rsrv_mp) == NULL) {
5594 		mutex_exit(&tcp->tcp_rsrv_mp_lock);
5595 		return (0);
5596 	}
5597 	tcp->tcp_rsrv_mp = NULL;
5598 	mutex_exit(&tcp->tcp_rsrv_mp_lock);
5599 
5600 	CONN_INC_REF(connp);
5601 	SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp,
5602 	    NULL, SQ_PROCESS, SQTAG_TCP_RSRV);
5603 	return (0);
5604 }
5605 
5606 /* At minimum we need 8 bytes in the TCP header for the lookup */
5607 #define	ICMP_MIN_TCP_HDR	8
5608 
5609 /*
5610  * tcp_icmp_input is called as conn_recvicmp to process ICMP error messages
5611  * passed up by IP. The message is always received on the correct tcp_t.
5612  * Assumes that IP has pulled up everything up to and including the ICMP header.
5613  */
5614 /* ARGSUSED2 */
5615 void
5616 tcp_icmp_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
5617 {
5618 	conn_t		*connp = (conn_t *)arg1;
5619 	icmph_t		*icmph;
5620 	ipha_t		*ipha;
5621 	int		iph_hdr_length;
5622 	tcpha_t		*tcpha;
5623 	uint32_t	seg_seq;
5624 	tcp_t		*tcp = connp->conn_tcp;
5625 
5626 	/* Assume IP provides aligned packets */
5627 	ASSERT(OK_32PTR(mp->b_rptr));
5628 	ASSERT((MBLKL(mp) >= sizeof (ipha_t)));
5629 
5630 	/*
5631 	 * It's possible we have a closed, but not yet destroyed, TCP
5632 	 * connection. Several fields (e.g. conn_ixa->ixa_ire) are invalid
5633 	 * in the closed state, so don't take any chances and drop the packet.
5634 	 */
5635 	if (tcp->tcp_state == TCPS_CLOSED) {
5636 		freemsg(mp);
5637 		return;
5638 	}
5639 
5640 	/*
5641 	 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent
5642 	 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6.
5643 	 */
5644 	if (!(ira->ira_flags & IRAF_IS_IPV4)) {
5645 		tcp_icmp_error_ipv6(tcp, mp, ira);
5646 		return;
5647 	}
5648 
5649 	/* Skip past the outer IP and ICMP headers */
5650 	iph_hdr_length = ira->ira_ip_hdr_length;
5651 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
5652 	/*
5653 	 * If we don't have the correct outer IP header length
5654 	 * or if we don't have a complete inner IP header
5655 	 * drop it.
5656 	 */
5657 	if (iph_hdr_length < sizeof (ipha_t) ||
5658 	    (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) {
5659 noticmpv4:
5660 		freemsg(mp);
5661 		return;
5662 	}
5663 	ipha = (ipha_t *)&icmph[1];
5664 
5665 	/* Skip past the inner IP and find the ULP header */
5666 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
5667 	tcpha = (tcpha_t *)((char *)ipha + iph_hdr_length);
5668 	/*
5669 	 * If we don't have the correct inner IP header length or if the ULP
5670 	 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR
5671 	 * bytes of TCP header, drop it.
5672 	 */
5673 	if (iph_hdr_length < sizeof (ipha_t) ||
5674 	    ipha->ipha_protocol != IPPROTO_TCP ||
5675 	    (uchar_t *)tcpha + ICMP_MIN_TCP_HDR > mp->b_wptr) {
5676 		goto noticmpv4;
5677 	}
5678 
5679 	seg_seq = ntohl(tcpha->tha_seq);
5680 	switch (icmph->icmph_type) {
5681 	case ICMP_DEST_UNREACHABLE:
5682 		switch (icmph->icmph_code) {
5683 		case ICMP_FRAGMENTATION_NEEDED:
5684 			/*
5685 			 * Update Path MTU, then try to send something out.
5686 			 */
5687 			tcp_update_pmtu(tcp, B_TRUE);
5688 			tcp_rexmit_after_error(tcp);
5689 			break;
5690 		case ICMP_PORT_UNREACHABLE:
5691 		case ICMP_PROTOCOL_UNREACHABLE:
5692 			switch (tcp->tcp_state) {
5693 			case TCPS_SYN_SENT:
5694 			case TCPS_SYN_RCVD:
5695 				/*
5696 				 * ICMP can snipe away incipient
5697 				 * TCP connections as long as
5698 				 * seq number is same as initial
5699 				 * send seq number.
5700 				 */
5701 				if (seg_seq == tcp->tcp_iss) {
5702 					(void) tcp_clean_death(tcp,
5703 					    ECONNREFUSED);
5704 				}
5705 				break;
5706 			}
5707 			break;
5708 		case ICMP_HOST_UNREACHABLE:
5709 		case ICMP_NET_UNREACHABLE:
5710 			/* Record the error in case we finally time out. */
5711 			if (icmph->icmph_code == ICMP_HOST_UNREACHABLE)
5712 				tcp->tcp_client_errno = EHOSTUNREACH;
5713 			else
5714 				tcp->tcp_client_errno = ENETUNREACH;
5715 			if (tcp->tcp_state == TCPS_SYN_RCVD) {
5716 				if (tcp->tcp_listener != NULL &&
5717 				    tcp->tcp_listener->tcp_syn_defense) {
5718 					/*
5719 					 * Ditch the half-open connection if we
5720 					 * suspect a SYN attack is under way.
5721 					 */
5722 					(void) tcp_clean_death(tcp,
5723 					    tcp->tcp_client_errno);
5724 				}
5725 			}
5726 			break;
5727 		default:
5728 			break;
5729 		}
5730 		break;
5731 	case ICMP_SOURCE_QUENCH: {
5732 		/*
5733 		 * use a global boolean to control
5734 		 * whether TCP should respond to ICMP_SOURCE_QUENCH.
5735 		 * The default is false.
5736 		 */
5737 		if (tcp_icmp_source_quench) {
5738 			/*
5739 			 * Reduce the sending rate as if we got a
5740 			 * retransmit timeout
5741 			 */
5742 			uint32_t npkt;
5743 
5744 			npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) /
5745 			    tcp->tcp_mss;
5746 			tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss;
5747 
5748 			DTRACE_PROBE3(cwnd__source__quench, tcp_t *, tcp,
5749 			    uint32_t, tcp->tcp_cwnd,
5750 			    uint32_t, tcp->tcp_mss);
5751 			tcp->tcp_cwnd = tcp->tcp_mss;
5752 			tcp->tcp_cwnd_cnt = 0;
5753 		}
5754 		break;
5755 	}
5756 	}
5757 	freemsg(mp);
5758 }
5759 
5760 /*
5761  * tcp_icmp_error_ipv6 is called from tcp_icmp_input to process ICMPv6
5762  * error messages passed up by IP.
5763  * Assumes that IP has pulled up all the extension headers as well
5764  * as the ICMPv6 header.
5765  */
5766 static void
5767 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, ip_recv_attr_t *ira)
5768 {
5769 	icmp6_t		*icmp6;
5770 	ip6_t		*ip6h;
5771 	uint16_t	iph_hdr_length = ira->ira_ip_hdr_length;
5772 	tcpha_t		*tcpha;
5773 	uint8_t		*nexthdrp;
5774 	uint32_t	seg_seq;
5775 
5776 	/*
5777 	 * Verify that we have a complete IP header.
5778 	 */
5779 	ASSERT((MBLKL(mp) >= sizeof (ip6_t)));
5780 
5781 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
5782 	ip6h = (ip6_t *)&icmp6[1];
5783 	/*
5784 	 * Verify if we have a complete ICMP and inner IP header.
5785 	 */
5786 	if ((uchar_t *)&ip6h[1] > mp->b_wptr) {
5787 noticmpv6:
5788 		freemsg(mp);
5789 		return;
5790 	}
5791 
5792 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp))
5793 		goto noticmpv6;
5794 	tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length);
5795 	/*
5796 	 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't
5797 	 * have at least ICMP_MIN_TCP_HDR bytes of  TCP header drop the
5798 	 * packet.
5799 	 */
5800 	if ((*nexthdrp != IPPROTO_TCP) ||
5801 	    ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) {
5802 		goto noticmpv6;
5803 	}
5804 
5805 	seg_seq = ntohl(tcpha->tha_seq);
5806 	switch (icmp6->icmp6_type) {
5807 	case ICMP6_PACKET_TOO_BIG:
5808 		/*
5809 		 * Update Path MTU, then try to send something out.
5810 		 */
5811 		tcp_update_pmtu(tcp, B_TRUE);
5812 		tcp_rexmit_after_error(tcp);
5813 		break;
5814 	case ICMP6_DST_UNREACH:
5815 		switch (icmp6->icmp6_code) {
5816 		case ICMP6_DST_UNREACH_NOPORT:
5817 			if (((tcp->tcp_state == TCPS_SYN_SENT) ||
5818 			    (tcp->tcp_state == TCPS_SYN_RCVD)) &&
5819 			    (seg_seq == tcp->tcp_iss)) {
5820 				(void) tcp_clean_death(tcp, ECONNREFUSED);
5821 			}
5822 			break;
5823 		case ICMP6_DST_UNREACH_ADMIN:
5824 		case ICMP6_DST_UNREACH_NOROUTE:
5825 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
5826 		case ICMP6_DST_UNREACH_ADDR:
5827 			/* Record the error in case we finally time out. */
5828 			tcp->tcp_client_errno = EHOSTUNREACH;
5829 			if (((tcp->tcp_state == TCPS_SYN_SENT) ||
5830 			    (tcp->tcp_state == TCPS_SYN_RCVD)) &&
5831 			    (seg_seq == tcp->tcp_iss)) {
5832 				if (tcp->tcp_listener != NULL &&
5833 				    tcp->tcp_listener->tcp_syn_defense) {
5834 					/*
5835 					 * Ditch the half-open connection if we
5836 					 * suspect a SYN attack is under way.
5837 					 */
5838 					(void) tcp_clean_death(tcp,
5839 					    tcp->tcp_client_errno);
5840 				}
5841 			}
5842 
5843 
5844 			break;
5845 		default:
5846 			break;
5847 		}
5848 		break;
5849 	case ICMP6_PARAM_PROB:
5850 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
5851 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
5852 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
5853 		    (uchar_t *)nexthdrp) {
5854 			if (tcp->tcp_state == TCPS_SYN_SENT ||
5855 			    tcp->tcp_state == TCPS_SYN_RCVD) {
5856 				(void) tcp_clean_death(tcp, ECONNREFUSED);
5857 			}
5858 			break;
5859 		}
5860 		break;
5861 
5862 	case ICMP6_TIME_EXCEEDED:
5863 	default:
5864 		break;
5865 	}
5866 	freemsg(mp);
5867 }
5868 
5869 /*
5870  * CALLED OUTSIDE OF SQUEUE! It can not follow any pointers that tcp might
5871  * change. But it can refer to fields like tcp_suna and tcp_snxt.
5872  *
5873  * Function tcp_verifyicmp is called as conn_verifyicmp to verify the ICMP
5874  * error messages received by IP. The message is always received on the correct
5875  * tcp_t.
5876  */
5877 /* ARGSUSED */
5878 boolean_t
5879 tcp_verifyicmp(conn_t *connp, void *arg2, icmph_t *icmph, icmp6_t *icmp6,
5880     ip_recv_attr_t *ira)
5881 {
5882 	tcpha_t		*tcpha = (tcpha_t *)arg2;
5883 	uint32_t	seq = ntohl(tcpha->tha_seq);
5884 	tcp_t		*tcp = connp->conn_tcp;
5885 
5886 	/*
5887 	 * TCP sequence number contained in payload of the ICMP error message
5888 	 * should be within the range SND.UNA <= SEG.SEQ < SND.NXT. Otherwise,
5889 	 * the message is either a stale ICMP error, or an attack from the
5890 	 * network. Fail the verification.
5891 	 */
5892 	if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt))
5893 		return (B_FALSE);
5894 
5895 	/* For "too big" we also check the ignore flag */
5896 	if (ira->ira_flags & IRAF_IS_IPV4) {
5897 		ASSERT(icmph != NULL);
5898 		if (icmph->icmph_type == ICMP_DEST_UNREACHABLE &&
5899 		    icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED &&
5900 		    tcp->tcp_tcps->tcps_ignore_path_mtu)
5901 			return (B_FALSE);
5902 	} else {
5903 		ASSERT(icmp6 != NULL);
5904 		if (icmp6->icmp6_type == ICMP6_PACKET_TOO_BIG &&
5905 		    tcp->tcp_tcps->tcps_ignore_path_mtu)
5906 			return (B_FALSE);
5907 	}
5908 	return (B_TRUE);
5909 }
5910