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