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.
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.
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]
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.
29 /* This file contains all TCP input processing functions. */
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>
44 #include <inet/common.h>
47 #include <inet/tcp_impl.h>
48 #include <inet/proto_set.h>
49 #include <inet/ipsec_impl.h>
52 * RFC7323-recommended phrasing of TSTAMP option, for easier parsing
56 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
57 (TCPOPT_TSTAMP << 8) | 10)
59 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \
60 (TCPOPT_NOP << 8) | TCPOPT_NOP)
64 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days
66 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz))
69 * Since tcp_listener is not cleared atomically with tcp_detached
70 * being cleared we need this extra bit to tell a detached connection
71 * apart from one that is in the process of being accepted.
73 #define TCP_IS_DETACHED_NONEAGER(tcp) \
74 (TCP_IS_DETACHED(tcp) && \
75 (!(tcp)->tcp_hard_binding))
78 * Steps to do when a tcp_t moves to TIME-WAIT state.
80 * This connection is done, we don't need to account for it. Decrement
81 * the listener connection counter if needed.
83 * Decrement the connection counter of the stack. Note that this counter
84 * is per CPU. So the total number of connections in a stack is the sum of all
85 * of them. Since there is no lock for handling all of them exclusively, the
86 * resulting sum is only an approximation.
88 * Unconditionally clear the exclusive binding bit so this TIME-WAIT
89 * connection won't interfere with new ones.
91 * Start the TIME-WAIT timer. If upper layer has not closed the connection,
92 * the timer is handled within the context of this tcp_t. When the timer
93 * fires, tcp_clean_death() is called. If upper layer closes the connection
94 * during this period, tcp_time_wait_append() will be called to add this
95 * tcp_t to the global TIME-WAIT list. Note that this means that the
96 * actual wait time in TIME-WAIT state will be longer than the
97 * tcps_time_wait_interval since the period before upper layer closes the
98 * connection is not accounted for when tcp_time_wait_append() is called.
100 * If upper layer has closed the connection, call tcp_time_wait_append()
104 #define SET_TIME_WAIT(tcps, tcp, connp) \
106 (tcp)->tcp_state = TCPS_TIME_WAIT; \
107 if ((tcp)->tcp_listen_cnt != NULL) \
108 TCP_DECR_LISTEN_CNT(tcp); \
110 (uint64_t *)&(tcps)->tcps_sc[CPU->cpu_seqid]->tcp_sc_conn_cnt); \
111 (connp)->conn_exclbind = 0; \
112 if (!TCP_IS_DETACHED(tcp)) { \
113 TCP_TIMER_RESTART(tcp, (tcps)->tcps_time_wait_interval); \
115 tcp_time_wait_append(tcp); \
116 TCP_DBGSTAT(tcps, tcp_rput_time_wait); \
121 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more
122 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent
123 * data, TCP will not respond with an ACK. RFC 793 requires that
124 * TCP responds with an ACK for such a bogus ACK. By not following
125 * the RFC, we prevent TCP from getting into an ACK storm if somehow
126 * an attacker successfully spoofs an acceptable segment to our
127 * peer; or when our peer is "confused."
129 static uint32_t tcp_drop_ack_unsent_cnt
= 10;
132 * To protect TCP against attacker using a small window and requesting
133 * large amount of data (DoS attack by conuming memory), TCP checks the
134 * window advertised in the last ACK of the 3-way handshake. TCP uses
135 * the tcp_mss (the size of one packet) value for comparion. The window
136 * should be larger than tcp_mss. But while a sane TCP should advertise
137 * a receive window larger than or equal to 4*MSS to avoid stop and go
138 * tarrfic, not all TCP stacks do that. This is especially true when
139 * tcp_mss is a big value.
141 * To work around this issue, an additional fixed value for comparison
142 * is also used. If the advertised window is smaller than both tcp_mss
143 * and tcp_init_wnd_chk, the ACK is considered as invalid. So for large
144 * tcp_mss value (say, 8K), a window larger than tcp_init_wnd_chk but
145 * smaller than 8K is considered to be OK.
147 static uint32_t tcp_init_wnd_chk
= 4096;
149 /* Process ICMP source quench message or not. */
150 static boolean_t tcp_icmp_source_quench
= B_FALSE
;
152 static boolean_t tcp_outbound_squeue_switch
= B_FALSE
;
154 static mblk_t
*tcp_conn_create_v4(conn_t
*, conn_t
*, mblk_t
*,
156 static mblk_t
*tcp_conn_create_v6(conn_t
*, conn_t
*, mblk_t
*,
158 static boolean_t
tcp_drop_q0(tcp_t
*);
159 static void tcp_icmp_error_ipv6(tcp_t
*, mblk_t
*, ip_recv_attr_t
*);
160 static mblk_t
*tcp_input_add_ancillary(tcp_t
*, mblk_t
*, ip_pkt_t
*,
162 static void tcp_input_listener(void *, mblk_t
*, void *, ip_recv_attr_t
*);
163 static void tcp_process_options(tcp_t
*, tcpha_t
*);
164 static mblk_t
*tcp_reass(tcp_t
*, mblk_t
*, uint32_t);
165 static void tcp_reass_elim_overlap(tcp_t
*, mblk_t
*);
166 static void tcp_rsrv_input(void *, mblk_t
*, void *, ip_recv_attr_t
*);
167 static void tcp_set_rto(tcp_t
*, time_t);
170 * Set the MSS associated with a particular tcp based on its current value,
171 * and a new one passed in. Observe minimums and maximums, and reset other
172 * state variables that we want to view as multiples of MSS.
174 * The value of MSS could be either increased or descreased.
177 tcp_mss_set(tcp_t
*tcp
, uint32_t mss
)
180 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
181 conn_t
*connp
= tcp
->tcp_connp
;
183 if (connp
->conn_ipversion
== IPV4_VERSION
)
184 mss_max
= tcps
->tcps_mss_max_ipv4
;
186 mss_max
= tcps
->tcps_mss_max_ipv6
;
188 if (mss
< tcps
->tcps_mss_min
)
189 mss
= tcps
->tcps_mss_min
;
193 * Unless naglim has been set by our client to
194 * a non-mss value, force naglim to track mss.
195 * This can help to aggregate small writes.
197 if (mss
< tcp
->tcp_naglim
|| tcp
->tcp_mss
== tcp
->tcp_naglim
)
198 tcp
->tcp_naglim
= mss
;
200 * TCP should be able to buffer at least 4 MSS data for obvious
201 * performance reason.
203 if ((mss
<< 2) > connp
->conn_sndbuf
)
204 connp
->conn_sndbuf
= mss
<< 2;
207 * Set the send lowater to at least twice of MSS.
209 if ((mss
<< 1) > connp
->conn_sndlowat
)
210 connp
->conn_sndlowat
= mss
<< 1;
213 * Update tcp_cwnd according to the new value of MSS. Keep the
214 * previous ratio to preserve the transmit rate.
216 tcp
->tcp_cwnd
= (tcp
->tcp_cwnd
/ tcp
->tcp_mss
) * mss
;
217 tcp
->tcp_cwnd_cnt
= 0;
220 (void) tcp_maxpsz_set(tcp
, B_TRUE
);
224 * Extract option values from a tcp header. We put any found values into the
225 * tcpopt struct and return a bitmask saying which options were found.
228 tcp_parse_options(tcpha_t
*tcpha
, tcp_opt_t
*tcpopt
)
233 uchar_t
*up
= (uchar_t
*)tcpha
;
236 tcp_seq sack_begin
, sack_end
;
239 endp
= up
+ TCP_HDR_LENGTH(tcpha
);
240 up
+= TCP_MIN_HEADER_LENGTH
;
242 * If timestamp option is aligned as recommended in RFC 7323 Appendix
243 * A, and is the only option, return quickly.
245 if (TCP_HDR_LENGTH(tcpha
) == (uint32_t)TCP_MIN_HEADER_LENGTH
+
246 TCPOPT_REAL_TS_LEN
&&
248 *(uint32_t *)up
== TCPOPT_NOP_NOP_TSTAMP
) {
249 tcpopt
->tcp_opt_ts_val
= ABE32_TO_U32((up
+4));
250 tcpopt
->tcp_opt_ts_ecr
= ABE32_TO_U32((up
+8));
252 return (TCP_OPT_TSTAMP_PRESENT
);
265 if (len
< TCPOPT_MAXSEG_LEN
||
266 up
[1] != TCPOPT_MAXSEG_LEN
)
269 mss
= BE16_TO_U16(up
+2);
270 /* Caller must handle tcp_mss_min and tcp_mss_max_* */
271 tcpopt
->tcp_opt_mss
= mss
;
272 found
|= TCP_OPT_MSS_PRESENT
;
274 up
+= TCPOPT_MAXSEG_LEN
;
278 if (len
< TCPOPT_WS_LEN
|| up
[1] != TCPOPT_WS_LEN
)
281 if (up
[2] > TCP_MAX_WINSHIFT
)
282 tcpopt
->tcp_opt_wscale
= TCP_MAX_WINSHIFT
;
284 tcpopt
->tcp_opt_wscale
= up
[2];
285 found
|= TCP_OPT_WSCALE_PRESENT
;
290 case TCPOPT_SACK_PERMITTED
:
291 if (len
< TCPOPT_SACK_OK_LEN
||
292 up
[1] != TCPOPT_SACK_OK_LEN
)
294 found
|= TCP_OPT_SACK_OK_PRESENT
;
295 up
+= TCPOPT_SACK_OK_LEN
;
299 if (len
<= 2 || up
[1] <= 2 || len
< up
[1])
302 /* If TCP is not interested in SACK blks... */
303 if ((tcp
= tcpopt
->tcp
) == NULL
) {
307 sack_len
= up
[1] - TCPOPT_HEADER_LEN
;
308 up
+= TCPOPT_HEADER_LEN
;
311 * If the list is empty, allocate one and assume
312 * nothing is sack'ed.
314 if (tcp
->tcp_notsack_list
== NULL
) {
315 tcp_notsack_update(&(tcp
->tcp_notsack_list
),
316 tcp
->tcp_suna
, tcp
->tcp_snxt
,
317 &(tcp
->tcp_num_notsack_blk
),
318 &(tcp
->tcp_cnt_notsack_list
));
321 * Make sure tcp_notsack_list is not NULL.
322 * This happens when kmem_alloc(KM_NOSLEEP)
325 if (tcp
->tcp_notsack_list
== NULL
) {
329 tcp
->tcp_fack
= tcp
->tcp_suna
;
332 while (sack_len
> 0) {
337 sack_begin
= BE32_TO_U32(up
);
339 sack_end
= BE32_TO_U32(up
);
343 * Bounds checking. Make sure the SACK
344 * info is within tcp_suna and tcp_snxt.
345 * If this SACK blk is out of bound, ignore
346 * it but continue to parse the following
349 if (SEQ_LEQ(sack_end
, sack_begin
) ||
350 SEQ_LT(sack_begin
, tcp
->tcp_suna
) ||
351 SEQ_GT(sack_end
, tcp
->tcp_snxt
)) {
354 tcp_notsack_insert(&(tcp
->tcp_notsack_list
),
355 sack_begin
, sack_end
,
356 &(tcp
->tcp_num_notsack_blk
),
357 &(tcp
->tcp_cnt_notsack_list
));
358 if (SEQ_GT(sack_end
, tcp
->tcp_fack
)) {
359 tcp
->tcp_fack
= sack_end
;
362 found
|= TCP_OPT_SACK_PRESENT
;
366 if (len
< TCPOPT_TSTAMP_LEN
||
367 up
[1] != TCPOPT_TSTAMP_LEN
)
370 tcpopt
->tcp_opt_ts_val
= BE32_TO_U32(up
+2);
371 tcpopt
->tcp_opt_ts_ecr
= BE32_TO_U32(up
+6);
373 found
|= TCP_OPT_TSTAMP_PRESENT
;
375 up
+= TCPOPT_TSTAMP_LEN
;
379 if (len
<= 1 || len
< (int)up
[1] || up
[1] == 0)
390 * Process all TCP option in SYN segment. Note that this function should
391 * be called after tcp_set_destination() is called so that the necessary info
392 * from IRE is already set in the tcp structure.
394 * This function sets up the correct tcp_mss value according to the
395 * MSS option value and our header size. It also sets up the window scale
396 * and timestamp values, and initialize SACK info blocks. But it does not
397 * change receive window size after setting the tcp_mss value. The caller
398 * should do the appropriate change.
401 tcp_process_options(tcp_t
*tcp
, tcpha_t
*tcpha
)
407 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
408 conn_t
*connp
= tcp
->tcp_connp
;
411 options
= tcp_parse_options(tcpha
, &tcpopt
);
414 * Process MSS option. Note that MSS option value does not account
415 * for IP or TCP options. This means that it is equal to MTU - minimum
416 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for
419 if (!(options
& TCP_OPT_MSS_PRESENT
)) {
420 if (connp
->conn_ipversion
== IPV4_VERSION
)
421 tcpopt
.tcp_opt_mss
= tcps
->tcps_mss_def_ipv4
;
423 tcpopt
.tcp_opt_mss
= tcps
->tcps_mss_def_ipv6
;
425 if (connp
->conn_ipversion
== IPV4_VERSION
)
426 mss_max
= tcps
->tcps_mss_max_ipv4
;
428 mss_max
= tcps
->tcps_mss_max_ipv6
;
429 if (tcpopt
.tcp_opt_mss
< tcps
->tcps_mss_min
)
430 tcpopt
.tcp_opt_mss
= tcps
->tcps_mss_min
;
431 else if (tcpopt
.tcp_opt_mss
> mss_max
)
432 tcpopt
.tcp_opt_mss
= mss_max
;
435 /* Process Window Scale option. */
436 if (options
& TCP_OPT_WSCALE_PRESENT
) {
437 tcp
->tcp_snd_ws
= tcpopt
.tcp_opt_wscale
;
438 tcp
->tcp_snd_ws_ok
= B_TRUE
;
440 tcp
->tcp_snd_ws
= B_FALSE
;
441 tcp
->tcp_snd_ws_ok
= B_FALSE
;
442 tcp
->tcp_rcv_ws
= B_FALSE
;
445 /* Process Timestamp option. */
446 if ((options
& TCP_OPT_TSTAMP_PRESENT
) &&
447 (tcp
->tcp_snd_ts_ok
|| TCP_IS_DETACHED(tcp
))) {
448 tmp_tcph
= (char *)tcp
->tcp_tcpha
;
450 tcp
->tcp_snd_ts_ok
= B_TRUE
;
451 tcp
->tcp_ts_recent
= tcpopt
.tcp_opt_ts_val
;
452 tcp
->tcp_last_rcv_lbolt
= ddi_get_lbolt64();
453 ASSERT(OK_32PTR(tmp_tcph
));
454 ASSERT(connp
->conn_ht_ulp_len
== TCP_MIN_HEADER_LENGTH
);
456 /* Fill in our template header with basic timestamp option. */
457 tmp_tcph
+= connp
->conn_ht_ulp_len
;
458 tmp_tcph
[0] = TCPOPT_NOP
;
459 tmp_tcph
[1] = TCPOPT_NOP
;
460 tmp_tcph
[2] = TCPOPT_TSTAMP
;
461 tmp_tcph
[3] = TCPOPT_TSTAMP_LEN
;
462 connp
->conn_ht_iphc_len
+= TCPOPT_REAL_TS_LEN
;
463 connp
->conn_ht_ulp_len
+= TCPOPT_REAL_TS_LEN
;
464 tcp
->tcp_tcpha
->tha_offset_and_reserved
+= (3 << 4);
466 tcp
->tcp_snd_ts_ok
= B_FALSE
;
470 * Process SACK options. If SACK is enabled for this connection,
471 * then allocate the SACK info structure. Note the following ways
472 * when tcp_snd_sack_ok is set to true.
474 * For active connection: in tcp_set_destination() called in
477 * For passive connection: in tcp_set_destination() called in
478 * tcp_input_listener().
480 * That's the reason why the extra TCP_IS_DETACHED() check is there.
481 * That check makes sure that if we did not send a SACK OK option,
482 * we will not enable SACK for this connection even though the other
483 * side sends us SACK OK option. For active connection, the SACK
484 * info structure has already been allocated. So we need to free
485 * it if SACK is disabled.
487 if ((options
& TCP_OPT_SACK_OK_PRESENT
) &&
488 (tcp
->tcp_snd_sack_ok
||
489 (tcps
->tcps_sack_permitted
!= 0 && TCP_IS_DETACHED(tcp
)))) {
490 ASSERT(tcp
->tcp_num_sack_blk
== 0);
491 ASSERT(tcp
->tcp_notsack_list
== NULL
);
493 tcp
->tcp_snd_sack_ok
= B_TRUE
;
494 if (tcp
->tcp_snd_ts_ok
) {
495 tcp
->tcp_max_sack_blk
= 3;
497 tcp
->tcp_max_sack_blk
= 4;
499 } else if (tcp
->tcp_snd_sack_ok
) {
501 * Resetting tcp_snd_sack_ok to B_FALSE so that
502 * no SACK info will be used for this
503 * connection. This assumes that SACK usage
504 * permission is negotiated. This may need
505 * to be changed once this is clarified.
507 ASSERT(tcp
->tcp_num_sack_blk
== 0);
508 ASSERT(tcp
->tcp_notsack_list
== NULL
);
509 tcp
->tcp_snd_sack_ok
= B_FALSE
;
513 * Now we know the exact TCP/IP header length, subtract
514 * that from tcp_mss to get our side's MSS.
516 tcp
->tcp_mss
-= connp
->conn_ht_iphc_len
;
519 * Here we assume that the other side's header size will be equal to
520 * our header size. We calculate the real MSS accordingly. Need to
521 * take into additional stuffs IPsec puts in.
523 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header)
525 tcpopt
.tcp_opt_mss
-= connp
->conn_ht_iphc_len
+
526 tcp
->tcp_ipsec_overhead
-
527 ((connp
->conn_ipversion
== IPV4_VERSION
?
528 IP_SIMPLE_HDR_LENGTH
: IPV6_HDR_LEN
) + TCP_MIN_HEADER_LENGTH
);
531 * Set MSS to the smaller one of both ends of the connection.
532 * We should not have called tcp_mss_set() before, but our
533 * side of the MSS should have been set to a proper value
534 * by tcp_set_destination(). tcp_mss_set() will also set up the
535 * STREAM head parameters properly.
537 * If we have a larger-than-16-bit window but the other side
538 * didn't want to do window scale, tcp_rwnd_set() will take
541 tcp_mss_set(tcp
, MIN(tcpopt
.tcp_opt_mss
, tcp
->tcp_mss
));
544 * Initialize tcp_cwnd value. After tcp_mss_set(), tcp_mss has been
547 TCP_SET_INIT_CWND(tcp
, tcp
->tcp_mss
, tcps
->tcps_slow_start_initial
);
551 * Add a new piece to the tcp reassembly queue. If the gap at the beginning
552 * is filled, return as much as we can. The message passed in may be
553 * multi-part, chained using b_cont. "start" is the starting sequence
554 * number for this piece.
557 tcp_reass(tcp_t
*tcp
, mblk_t
*mp
, uint32_t start
)
564 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
567 /* Walk through all the new pieces. */
569 ASSERT((uintptr_t)(mp
->b_wptr
- mp
->b_rptr
) <=
571 end
= start
+ (int)(mp
->b_wptr
- mp
->b_rptr
);
572 next_mp
= mp
->b_cont
;
574 /* Empty. Blast it. */
579 TCP_REASS_SET_SEQ(mp
, start
);
580 TCP_REASS_SET_END(mp
, end
);
581 mp1
= tcp
->tcp_reass_tail
;
583 tcp
->tcp_reass_tail
= mp
;
584 tcp
->tcp_reass_head
= mp
;
585 TCPS_BUMP_MIB(tcps
, tcpInDataUnorderSegs
);
586 TCPS_UPDATE_MIB(tcps
, tcpInDataUnorderBytes
,
590 /* New stuff completely beyond tail? */
591 if (SEQ_GEQ(start
, TCP_REASS_END(mp1
))) {
592 /* Link it on end. */
594 tcp
->tcp_reass_tail
= mp
;
595 TCPS_BUMP_MIB(tcps
, tcpInDataUnorderSegs
);
596 TCPS_UPDATE_MIB(tcps
, tcpInDataUnorderBytes
,
600 mp1
= tcp
->tcp_reass_head
;
601 u1
= TCP_REASS_SEQ(mp1
);
602 /* New stuff at the front? */
603 if (SEQ_LT(start
, u1
)) {
604 /* Yes... Check for overlap. */
606 tcp
->tcp_reass_head
= mp
;
607 tcp_reass_elim_overlap(tcp
, mp
);
611 * The new piece fits somewhere between the head and tail.
612 * We find our slot, where mp1 precedes us and mp2 trails.
614 for (; (mp2
= mp1
->b_cont
) != NULL
; mp1
= mp2
) {
615 u1
= TCP_REASS_SEQ(mp2
);
616 if (SEQ_LEQ(start
, u1
))
619 /* Link ourselves in */
623 /* Trim overlap with following mblk(s) first */
624 tcp_reass_elim_overlap(tcp
, mp
);
626 /* Trim overlap with preceding mblk */
627 tcp_reass_elim_overlap(tcp
, mp1
);
629 } while (start
= end
, mp
= next_mp
);
630 mp1
= tcp
->tcp_reass_head
;
631 /* Anything ready to go? */
632 if (TCP_REASS_SEQ(mp1
) != tcp
->tcp_rnxt
)
634 /* Eat what we can off the queue */
637 end
= TCP_REASS_END(mp1
);
638 TCP_REASS_SET_SEQ(mp1
, 0);
639 TCP_REASS_SET_END(mp1
, 0);
641 tcp
->tcp_reass_tail
= NULL
;
644 if (end
!= TCP_REASS_SEQ(mp
)) {
650 mp1
= tcp
->tcp_reass_head
;
651 tcp
->tcp_reass_head
= mp
;
655 /* Eliminate any overlap that mp may have over later mblks */
657 tcp_reass_elim_overlap(tcp_t
*tcp
, mblk_t
*mp
)
662 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
664 end
= TCP_REASS_END(mp
);
665 while ((mp1
= mp
->b_cont
) != NULL
) {
666 u1
= TCP_REASS_SEQ(mp1
);
667 if (!SEQ_GT(end
, u1
))
669 if (!SEQ_GEQ(end
, TCP_REASS_END(mp1
))) {
670 mp
->b_wptr
-= end
- u1
;
671 TCP_REASS_SET_END(mp
, u1
);
672 TCPS_BUMP_MIB(tcps
, tcpInDataPartDupSegs
);
673 TCPS_UPDATE_MIB(tcps
, tcpInDataPartDupBytes
,
677 mp
->b_cont
= mp1
->b_cont
;
678 TCP_REASS_SET_SEQ(mp1
, 0);
679 TCP_REASS_SET_END(mp1
, 0);
681 TCPS_BUMP_MIB(tcps
, tcpInDataDupSegs
);
682 TCPS_UPDATE_MIB(tcps
, tcpInDataDupBytes
, end
- u1
);
685 tcp
->tcp_reass_tail
= mp
;
689 * This function does PAWS protection check, per RFC 7323 section 5. Requires
690 * that timestamp options are already processed into tcpoptp. Returns B_TRUE if
691 * the segment passes the PAWS test, else returns B_FALSE.
694 tcp_paws_check(tcp_t
*tcp
, const tcp_opt_t
*tcpoptp
)
696 if (TSTMP_LT(tcpoptp
->tcp_opt_ts_val
,
697 tcp
->tcp_ts_recent
)) {
698 if (LBOLT_FASTPATH64
<
699 (tcp
->tcp_last_rcv_lbolt
+ PAWS_TIMEOUT
)) {
700 /* This segment is not acceptable. */
704 * Connection has been idle for
705 * too long. Reset the timestamp
708 tcpoptp
->tcp_opt_ts_val
;
715 * Defense for the SYN attack -
716 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest
717 * one from the list of droppable eagers. This list is a subset of q0.
718 * see comments before the definition of MAKE_DROPPABLE().
719 * 2. Don't drop a SYN request before its first timeout. This gives every
720 * request at least til the first timeout to complete its 3-way handshake.
721 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many
722 * requests currently on the queue that has timed out. This will be used
723 * as an indicator of whether an attack is under way, so that appropriate
724 * actions can be taken. (It's incremented in tcp_timer() and decremented
725 * either when eager goes into ESTABLISHED, or gets freed up.)
726 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on
727 * # of timeout drops back to <= q0len/32 => SYN alert off
730 tcp_drop_q0(tcp_t
*tcp
)
734 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
736 ASSERT(MUTEX_HELD(&tcp
->tcp_eager_lock
));
737 ASSERT(tcp
->tcp_eager_next_q0
!= tcp
->tcp_eager_prev_q0
);
739 /* Pick oldest eager from the list of droppable eagers */
740 eager
= tcp
->tcp_eager_prev_drop_q0
;
742 /* If list is empty. return B_FALSE */
747 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */
748 if ((mp
= allocb(0, BPRI_HI
)) == NULL
)
752 * Take this eager out from the list of droppable eagers since we are
755 MAKE_UNDROPPABLE(eager
);
757 if (tcp
->tcp_connp
->conn_debug
) {
758 (void) strlog(TCP_MOD_ID
, 0, 3, SL_TRACE
,
759 "tcp_drop_q0: listen half-open queue (max=%d) overflow"
760 " (%d pending) on %s, drop one", tcps
->tcps_conn_req_max_q0
,
761 tcp
->tcp_conn_req_cnt_q0
,
762 tcp_display(tcp
, NULL
, DISP_PORT_ONLY
));
765 TCPS_BUMP_MIB(tcps
, tcpHalfOpenDrop
);
767 /* Put a reference on the conn as we are enqueueing it in the sqeue */
768 CONN_INC_REF(eager
->tcp_connp
);
770 SQUEUE_ENTER_ONE(eager
->tcp_connp
->conn_sqp
, mp
,
771 tcp_clean_death_wrapper
, eager
->tcp_connp
, NULL
,
772 SQ_FILL
, SQTAG_TCP_DROP_Q0
);
778 * Handle a SYN on an AF_INET6 socket; can be either IPv4 or IPv6
781 tcp_conn_create_v6(conn_t
*lconnp
, conn_t
*connp
, mblk_t
*mp
,
784 tcp_t
*ltcp
= lconnp
->conn_tcp
;
785 tcp_t
*tcp
= connp
->conn_tcp
;
790 uint_t ifindex
= ira
->ira_ruifindex
;
791 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
793 if (ira
->ira_flags
& IRAF_IS_IPV4
) {
794 ipha
= (ipha_t
*)mp
->b_rptr
;
796 connp
->conn_ipversion
= IPV4_VERSION
;
797 IN6_IPADDR_TO_V4MAPPED(ipha
->ipha_dst
, &connp
->conn_laddr_v6
);
798 IN6_IPADDR_TO_V4MAPPED(ipha
->ipha_src
, &connp
->conn_faddr_v6
);
799 connp
->conn_saddr_v6
= connp
->conn_laddr_v6
;
802 sin6
.sin6_addr
= connp
->conn_faddr_v6
;
803 sin6
.sin6_port
= connp
->conn_fport
;
804 sin6
.sin6_family
= AF_INET6
;
805 sin6
.__sin6_src_id
= ip_srcid_find_addr(&connp
->conn_laddr_v6
,
806 IPCL_ZONEID(lconnp
), tcps
->tcps_netstack
);
808 if (connp
->conn_recv_ancillary
.crb_recvdstaddr
) {
812 sin6d
.sin6_addr
= connp
->conn_laddr_v6
;
813 sin6d
.sin6_port
= connp
->conn_lport
;
814 sin6d
.sin6_family
= AF_INET
;
815 tpi_mp
= mi_tpi_extconn_ind(NULL
,
816 (char *)&sin6d
, sizeof (sin6_t
),
818 (t_scalar_t
)sizeof (intptr_t),
819 (char *)&sin6d
, sizeof (sin6_t
),
820 (t_scalar_t
)ltcp
->tcp_conn_req_seqnum
);
822 tpi_mp
= mi_tpi_conn_ind(NULL
,
823 (char *)&sin6
, sizeof (sin6_t
),
824 (char *)&tcp
, (t_scalar_t
)sizeof (intptr_t),
825 (t_scalar_t
)ltcp
->tcp_conn_req_seqnum
);
828 ip6h
= (ip6_t
*)mp
->b_rptr
;
830 connp
->conn_ipversion
= IPV6_VERSION
;
831 connp
->conn_laddr_v6
= ip6h
->ip6_dst
;
832 connp
->conn_faddr_v6
= ip6h
->ip6_src
;
833 connp
->conn_saddr_v6
= connp
->conn_laddr_v6
;
836 sin6
.sin6_addr
= connp
->conn_faddr_v6
;
837 sin6
.sin6_port
= connp
->conn_fport
;
838 sin6
.sin6_family
= AF_INET6
;
839 sin6
.sin6_flowinfo
= ip6h
->ip6_vcf
& ~IPV6_VERS_AND_FLOW_MASK
;
840 sin6
.__sin6_src_id
= ip_srcid_find_addr(&connp
->conn_laddr_v6
,
841 IPCL_ZONEID(lconnp
), tcps
->tcps_netstack
);
843 if (IN6_IS_ADDR_LINKSCOPE(&ip6h
->ip6_src
)) {
844 /* Pass up the scope_id of remote addr */
845 sin6
.sin6_scope_id
= ifindex
;
847 sin6
.sin6_scope_id
= 0;
849 if (connp
->conn_recv_ancillary
.crb_recvdstaddr
) {
853 sin6
.sin6_addr
= connp
->conn_laddr_v6
;
854 sin6d
.sin6_port
= connp
->conn_lport
;
855 sin6d
.sin6_family
= AF_INET6
;
856 if (IN6_IS_ADDR_LINKSCOPE(&connp
->conn_laddr_v6
))
857 sin6d
.sin6_scope_id
= ifindex
;
859 tpi_mp
= mi_tpi_extconn_ind(NULL
,
860 (char *)&sin6d
, sizeof (sin6_t
),
861 (char *)&tcp
, (t_scalar_t
)sizeof (intptr_t),
862 (char *)&sin6d
, sizeof (sin6_t
),
863 (t_scalar_t
)ltcp
->tcp_conn_req_seqnum
);
865 tpi_mp
= mi_tpi_conn_ind(NULL
,
866 (char *)&sin6
, sizeof (sin6_t
),
867 (char *)&tcp
, (t_scalar_t
)sizeof (intptr_t),
868 (t_scalar_t
)ltcp
->tcp_conn_req_seqnum
);
872 tcp
->tcp_mss
= tcps
->tcps_mss_def_ipv6
;
876 /* Handle a SYN on an AF_INET socket */
878 tcp_conn_create_v4(conn_t
*lconnp
, conn_t
*connp
, mblk_t
*mp
,
881 tcp_t
*ltcp
= lconnp
->conn_tcp
;
882 tcp_t
*tcp
= connp
->conn_tcp
;
884 mblk_t
*tpi_mp
= NULL
;
885 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
888 ASSERT(ira
->ira_flags
& IRAF_IS_IPV4
);
889 ipha
= (ipha_t
*)mp
->b_rptr
;
891 connp
->conn_ipversion
= IPV4_VERSION
;
892 IN6_IPADDR_TO_V4MAPPED(ipha
->ipha_dst
, &connp
->conn_laddr_v6
);
893 IN6_IPADDR_TO_V4MAPPED(ipha
->ipha_src
, &connp
->conn_faddr_v6
);
894 connp
->conn_saddr_v6
= connp
->conn_laddr_v6
;
897 sin
.sin_addr
.s_addr
= connp
->conn_faddr_v4
;
898 sin
.sin_port
= connp
->conn_fport
;
899 sin
.sin_family
= AF_INET
;
900 if (lconnp
->conn_recv_ancillary
.crb_recvdstaddr
) {
904 sind
.sin_addr
.s_addr
= connp
->conn_laddr_v4
;
905 sind
.sin_port
= connp
->conn_lport
;
906 sind
.sin_family
= AF_INET
;
907 tpi_mp
= mi_tpi_extconn_ind(NULL
,
908 (char *)&sind
, sizeof (sin_t
), (char *)&tcp
,
909 (t_scalar_t
)sizeof (intptr_t), (char *)&sind
,
910 sizeof (sin_t
), (t_scalar_t
)ltcp
->tcp_conn_req_seqnum
);
912 tpi_mp
= mi_tpi_conn_ind(NULL
,
913 (char *)&sin
, sizeof (sin_t
),
914 (char *)&tcp
, (t_scalar_t
)sizeof (intptr_t),
915 (t_scalar_t
)ltcp
->tcp_conn_req_seqnum
);
918 tcp
->tcp_mss
= tcps
->tcps_mss_def_ipv4
;
923 * Called via squeue to get on to eager's perimeter. It sends a
924 * TH_RST if eager is in the fanout table. The listener wants the
925 * eager to disappear either by means of tcp_eager_blowoff() or
926 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be
927 * called (via squeue) if the eager cannot be inserted in the
928 * fanout table in tcp_input_listener().
932 tcp_eager_kill(void *arg
, mblk_t
*mp
, void *arg2
, ip_recv_attr_t
*dummy
)
934 conn_t
*econnp
= (conn_t
*)arg
;
935 tcp_t
*eager
= econnp
->conn_tcp
;
936 tcp_t
*listener
= eager
->tcp_listener
;
939 * We could be called because listener is closing. Since
940 * the eager was using listener's queue's, we avoid
941 * using the listeners queues from now on.
943 ASSERT(eager
->tcp_detached
);
944 econnp
->conn_rq
= NULL
;
945 econnp
->conn_wq
= NULL
;
948 * An eager's conn_fanout will be NULL if it's a duplicate
949 * for an existing 4-tuples in the conn fanout table.
950 * We don't want to send an RST out in such case.
952 if (econnp
->conn_fanout
!= NULL
&& eager
->tcp_state
> TCPS_LISTEN
) {
953 tcp_xmit_ctl("tcp_eager_kill, can't wait",
954 eager
, eager
->tcp_snxt
, 0, TH_RST
);
957 /* We are here because listener wants this eager gone */
958 if (listener
!= NULL
) {
959 mutex_enter(&listener
->tcp_eager_lock
);
960 tcp_eager_unlink(eager
);
961 if (eager
->tcp_tconnind_started
) {
963 * The eager has sent a conn_ind up to the
964 * listener but listener decides to close
965 * instead. We need to drop the extra ref
966 * placed on eager in tcp_input_data() before
967 * sending the conn_ind to listener.
969 CONN_DEC_REF(econnp
);
971 mutex_exit(&listener
->tcp_eager_lock
);
972 CONN_DEC_REF(listener
->tcp_connp
);
975 if (eager
->tcp_state
!= TCPS_CLOSED
)
976 tcp_close_detached(eager
);
980 * Reset any eager connection hanging off this listener marked
981 * with 'seqnum' and then reclaim it's resources.
984 tcp_eager_blowoff(tcp_t
*listener
, t_scalar_t seqnum
)
990 mutex_enter(&listener
->tcp_eager_lock
);
992 eager
= eager
->tcp_eager_next_q
;
994 mutex_exit(&listener
->tcp_eager_lock
);
997 } while (eager
->tcp_conn_req_seqnum
!= seqnum
);
999 if (eager
->tcp_closemp_used
) {
1000 mutex_exit(&listener
->tcp_eager_lock
);
1003 eager
->tcp_closemp_used
= B_TRUE
;
1004 TCP_DEBUG_GETPCSTACK(eager
->tcmp_stk
, 15);
1005 CONN_INC_REF(eager
->tcp_connp
);
1006 mutex_exit(&listener
->tcp_eager_lock
);
1007 mp
= &eager
->tcp_closemp
;
1008 SQUEUE_ENTER_ONE(eager
->tcp_connp
->conn_sqp
, mp
, tcp_eager_kill
,
1009 eager
->tcp_connp
, NULL
, SQ_FILL
, SQTAG_TCP_EAGER_BLOWOFF
);
1014 * Reset any eager connection hanging off this listener
1015 * and then reclaim it's resources.
1018 tcp_eager_cleanup(tcp_t
*listener
, boolean_t q0_only
)
1022 tcp_stack_t
*tcps
= listener
->tcp_tcps
;
1024 ASSERT(MUTEX_HELD(&listener
->tcp_eager_lock
));
1027 /* First cleanup q */
1028 TCP_STAT(tcps
, tcp_eager_blowoff_q
);
1029 eager
= listener
->tcp_eager_next_q
;
1030 while (eager
!= NULL
) {
1031 if (!eager
->tcp_closemp_used
) {
1032 eager
->tcp_closemp_used
= B_TRUE
;
1033 TCP_DEBUG_GETPCSTACK(eager
->tcmp_stk
, 15);
1034 CONN_INC_REF(eager
->tcp_connp
);
1035 mp
= &eager
->tcp_closemp
;
1036 SQUEUE_ENTER_ONE(eager
->tcp_connp
->conn_sqp
, mp
,
1037 tcp_eager_kill
, eager
->tcp_connp
, NULL
,
1038 SQ_FILL
, SQTAG_TCP_EAGER_CLEANUP
);
1040 eager
= eager
->tcp_eager_next_q
;
1043 /* Then cleanup q0 */
1044 TCP_STAT(tcps
, tcp_eager_blowoff_q0
);
1045 eager
= listener
->tcp_eager_next_q0
;
1046 while (eager
!= listener
) {
1047 if (!eager
->tcp_closemp_used
) {
1048 eager
->tcp_closemp_used
= B_TRUE
;
1049 TCP_DEBUG_GETPCSTACK(eager
->tcmp_stk
, 15);
1050 CONN_INC_REF(eager
->tcp_connp
);
1051 mp
= &eager
->tcp_closemp
;
1052 SQUEUE_ENTER_ONE(eager
->tcp_connp
->conn_sqp
, mp
,
1053 tcp_eager_kill
, eager
->tcp_connp
, NULL
, SQ_FILL
,
1054 SQTAG_TCP_EAGER_CLEANUP_Q0
);
1056 eager
= eager
->tcp_eager_next_q0
;
1061 * If we are an eager connection hanging off a listener that hasn't
1062 * formally accepted the connection yet, get off his list and blow off
1063 * any data that we have accumulated.
1066 tcp_eager_unlink(tcp_t
*tcp
)
1068 tcp_t
*listener
= tcp
->tcp_listener
;
1070 ASSERT(listener
!= NULL
);
1071 ASSERT(MUTEX_HELD(&listener
->tcp_eager_lock
));
1072 if (tcp
->tcp_eager_next_q0
!= NULL
) {
1073 ASSERT(tcp
->tcp_eager_prev_q0
!= NULL
);
1075 /* Remove the eager tcp from q0 */
1076 tcp
->tcp_eager_next_q0
->tcp_eager_prev_q0
=
1077 tcp
->tcp_eager_prev_q0
;
1078 tcp
->tcp_eager_prev_q0
->tcp_eager_next_q0
=
1079 tcp
->tcp_eager_next_q0
;
1080 ASSERT(listener
->tcp_conn_req_cnt_q0
> 0);
1081 listener
->tcp_conn_req_cnt_q0
--;
1083 tcp
->tcp_eager_next_q0
= NULL
;
1084 tcp
->tcp_eager_prev_q0
= NULL
;
1087 * Take the eager out, if it is in the list of droppable
1090 MAKE_UNDROPPABLE(tcp
);
1092 if (tcp
->tcp_syn_rcvd_timeout
!= 0) {
1093 /* we have timed out before */
1094 ASSERT(listener
->tcp_syn_rcvd_timeout
> 0);
1095 listener
->tcp_syn_rcvd_timeout
--;
1098 tcp_t
**tcpp
= &listener
->tcp_eager_next_q
;
1101 for (; tcpp
[0]; tcpp
= &tcpp
[0]->tcp_eager_next_q
) {
1102 if (tcpp
[0] == tcp
) {
1103 if (listener
->tcp_eager_last_q
== tcp
) {
1105 * If we are unlinking the last
1106 * element on the list, adjust
1107 * tail pointer. Set tail pointer
1108 * to nil when list is empty.
1110 ASSERT(tcp
->tcp_eager_next_q
== NULL
);
1111 if (listener
->tcp_eager_last_q
==
1112 listener
->tcp_eager_next_q
) {
1113 listener
->tcp_eager_last_q
=
1117 * We won't get here if there
1118 * is only one eager in the
1121 ASSERT(prev
!= NULL
);
1122 listener
->tcp_eager_last_q
=
1126 tcpp
[0] = tcp
->tcp_eager_next_q
;
1127 tcp
->tcp_eager_next_q
= NULL
;
1128 tcp
->tcp_eager_last_q
= NULL
;
1129 ASSERT(listener
->tcp_conn_req_cnt_q
> 0);
1130 listener
->tcp_conn_req_cnt_q
--;
1136 tcp
->tcp_listener
= NULL
;
1142 * The sockfs ACCEPT path:
1143 * =======================
1145 * The eager is now established in its own perimeter as soon as SYN is
1146 * received in tcp_input_listener(). When sockfs receives conn_ind, it
1147 * completes the accept processing on the acceptor STREAM. The sending
1148 * of conn_ind part is common for both sockfs listener and a TLI/XTI
1149 * listener but a TLI/XTI listener completes the accept processing
1150 * on the listener perimeter.
1152 * Common control flow for 3 way handshake:
1153 * ----------------------------------------
1155 * incoming SYN (listener perimeter) -> tcp_input_listener()
1157 * incoming SYN-ACK-ACK (eager perim) -> tcp_input_data()
1158 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind()
1160 * Sockfs ACCEPT Path:
1161 * -------------------
1163 * open acceptor stream (tcp_open allocates tcp_tli_accept()
1164 * as STREAM entry point)
1166 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_tli_accept()
1168 * tcp_tli_accept() extracts the eager and makes the q->q_ptr <-> eager
1169 * association (we are not behind eager's squeue but sockfs is protecting us
1170 * and no one knows about this stream yet. The STREAMS entry point q->q_info
1171 * is changed to point at tcp_wput().
1173 * tcp_accept_common() sends any deferred eagers via tcp_send_pending() to
1174 * listener (done on listener's perimeter).
1176 * tcp_tli_accept() calls tcp_accept_finish() on eagers perimeter to finish
1179 * TLI/XTI client ACCEPT path:
1180 * ---------------------------
1182 * soaccept() sends T_CONN_RES on the listener STREAM.
1184 * tcp_tli_accept() -> tcp_accept_swap() complete the processing and send
1185 * a M_SETOPS mblk to eager perimeter to finish accept (tcp_accept_finish()).
1190 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and
1191 * and listeners->tcp_eager_next_q.
1196 * 1) We start out in tcp_input_listener by eager placing a ref on
1197 * listener and listener adding eager to listeners->tcp_eager_next_q0.
1199 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before
1200 * doing so we place a ref on the eager. This ref is finally dropped at the
1201 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the
1202 * reference is dropped by the squeue framework.
1204 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish
1206 * The reference must be released by the same entity that added the reference
1207 * In the above scheme, the eager is the entity that adds and releases the
1208 * references. Note that tcp_accept_finish executes in the squeue of the eager
1209 * (albeit after it is attached to the acceptor stream). Though 1. executes
1210 * in the listener's squeue, the eager is nascent at this point and the
1211 * reference can be considered to have been added on behalf of the eager.
1213 * Eager getting a Reset or listener closing:
1214 * ==========================================
1216 * Once the listener and eager are linked, the listener never does the unlink.
1217 * If the listener needs to close, tcp_eager_cleanup() is called which queues
1218 * a message on all eager perimeter. The eager then does the unlink, clears
1219 * any pointers to the listener's queue and drops the reference to the
1220 * listener. The listener waits in tcp_close outside the squeue until its
1221 * refcount has dropped to 1. This ensures that the listener has waited for
1222 * all eagers to clear their association with the listener.
1224 * Similarly, if eager decides to go away, it can unlink itself and close.
1225 * When the T_CONN_RES comes down, we check if eager has closed. Note that
1226 * the reference to eager is still valid because of the extra ref we put
1227 * in tcp_send_conn_ind.
1229 * Listener can always locate the eager under the protection
1230 * of the listener->tcp_eager_lock, and then do a refhold
1231 * on the eager during the accept processing.
1233 * The acceptor stream accesses the eager in the accept processing
1234 * based on the ref placed on eager before sending T_conn_ind.
1235 * The only entity that can negate this refhold is a listener close
1236 * which is mutually exclusive with an active acceptor stream.
1238 * Eager's reference on the listener
1239 * ===================================
1241 * If the accept happens (even on a closed eager) the eager drops its
1242 * reference on the listener at the start of tcp_accept_finish. If the
1243 * eager is killed due to an incoming RST before the T_conn_ind is sent up,
1244 * the reference is dropped in tcp_closei_local. If the listener closes,
1245 * the reference is dropped in tcp_eager_kill. In all cases the reference
1246 * is dropped while executing in the eager's context (squeue).
1250 /* Process the SYN packet, mp, directed at the listener 'tcp' */
1253 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN.
1254 * tcp_input_data will not see any packets for listeners since the listener
1255 * has conn_recv set to tcp_input_listener.
1259 tcp_input_listener(void *arg
, mblk_t
*mp
, void *arg2
, ip_recv_attr_t
*ira
)
1265 conn_t
*econnp
= NULL
;
1269 conn_t
*lconnp
= (conn_t
*)arg
;
1270 tcp_t
*listener
= lconnp
->conn_tcp
;
1271 tcp_stack_t
*tcps
= listener
->tcp_tcps
;
1272 ip_stack_t
*ipst
= tcps
->tcps_netstack
->netstack_ip
;
1275 uint_t ifindex
= ira
->ira_ruifindex
;
1276 boolean_t tlc_set
= B_FALSE
;
1278 ip_hdr_len
= ira
->ira_ip_hdr_length
;
1279 tcpha
= (tcpha_t
*)&mp
->b_rptr
[ip_hdr_len
];
1280 flags
= (unsigned int)tcpha
->tha_flags
& 0xFF;
1282 DTRACE_TCP5(receive
, mblk_t
*, NULL
, ip_xmit_attr_t
*, lconnp
->conn_ixa
,
1283 __dtrace_tcp_void_ip_t
*, mp
->b_rptr
, tcp_t
*, listener
,
1284 __dtrace_tcp_tcph_t
*, tcpha
);
1286 if (!(flags
& TH_SYN
)) {
1287 if ((flags
& TH_RST
) || (flags
& TH_URG
)) {
1291 if (flags
& TH_ACK
) {
1292 /* Note this executes in listener's squeue */
1293 tcp_xmit_listeners_reset(mp
, ira
, ipst
, lconnp
);
1301 if (listener
->tcp_state
!= TCPS_LISTEN
)
1304 ASSERT(IPCL_IS_BOUND(lconnp
));
1306 mutex_enter(&listener
->tcp_eager_lock
);
1309 * The system is under memory pressure, so we need to do our part
1310 * to relieve the pressure. So we only accept new request if there
1311 * is nothing waiting to be accepted or waiting to complete the 3-way
1312 * handshake. This means that busy listener will not get too many
1313 * new requests which they cannot handle in time while non-busy
1314 * listener is still functioning properly.
1316 if (tcps
->tcps_reclaim
&& (listener
->tcp_conn_req_cnt_q
> 0 ||
1317 listener
->tcp_conn_req_cnt_q0
> 0)) {
1318 mutex_exit(&listener
->tcp_eager_lock
);
1319 TCP_STAT(tcps
, tcp_listen_mem_drop
);
1323 if (listener
->tcp_conn_req_cnt_q
>= listener
->tcp_conn_req_max
) {
1324 mutex_exit(&listener
->tcp_eager_lock
);
1325 TCP_STAT(tcps
, tcp_listendrop
);
1326 TCPS_BUMP_MIB(tcps
, tcpListenDrop
);
1327 if (lconnp
->conn_debug
) {
1328 (void) strlog(TCP_MOD_ID
, 0, 1, SL_TRACE
|SL_ERROR
,
1329 "tcp_input_listener: listen backlog (max=%d) "
1330 "overflow (%d pending) on %s",
1331 listener
->tcp_conn_req_max
,
1332 listener
->tcp_conn_req_cnt_q
,
1333 tcp_display(listener
, NULL
, DISP_PORT_ONLY
));
1338 if (listener
->tcp_conn_req_cnt_q0
>=
1339 listener
->tcp_conn_req_max
+ tcps
->tcps_conn_req_max_q0
) {
1341 * Q0 is full. Drop a pending half-open req from the queue
1342 * to make room for the new SYN req. Also mark the time we
1345 * A more aggressive defense against SYN attack will
1346 * be to set the "tcp_syn_defense" flag now.
1348 TCP_STAT(tcps
, tcp_listendropq0
);
1349 listener
->tcp_last_rcv_lbolt
= ddi_get_lbolt64();
1350 if (!tcp_drop_q0(listener
)) {
1351 mutex_exit(&listener
->tcp_eager_lock
);
1352 TCPS_BUMP_MIB(tcps
, tcpListenDropQ0
);
1353 if (lconnp
->conn_debug
) {
1354 (void) strlog(TCP_MOD_ID
, 0, 3, SL_TRACE
,
1355 "tcp_input_listener: listen half-open "
1356 "queue (max=%d) full (%d pending) on %s",
1357 tcps
->tcps_conn_req_max_q0
,
1358 listener
->tcp_conn_req_cnt_q0
,
1359 tcp_display(listener
, NULL
,
1367 * Enforce the limit set on the number of connections per listener.
1368 * Note that tlc_cnt starts with 1. So need to add 1 to tlc_max
1371 if (listener
->tcp_listen_cnt
!= NULL
) {
1372 tcp_listen_cnt_t
*tlc
= listener
->tcp_listen_cnt
;
1375 if (atomic_inc_32_nv(&tlc
->tlc_cnt
) > tlc
->tlc_max
+ 1) {
1376 mutex_exit(&listener
->tcp_eager_lock
);
1377 now
= ddi_get_lbolt64();
1378 atomic_dec_32(&tlc
->tlc_cnt
);
1379 TCP_STAT(tcps
, tcp_listen_cnt_drop
);
1381 if (now
- tlc
->tlc_report_time
>
1382 MSEC_TO_TICK(TCP_TLC_REPORT_INTERVAL
)) {
1383 zcmn_err(lconnp
->conn_zoneid
, CE_WARN
,
1384 "Listener (port %d) connection max (%u) "
1385 "reached: %u attempts dropped total\n",
1386 ntohs(listener
->tcp_connp
->conn_lport
),
1387 tlc
->tlc_max
, tlc
->tlc_drop
);
1388 tlc
->tlc_report_time
= now
;
1395 mutex_exit(&listener
->tcp_eager_lock
);
1398 * IP sets ira_sqp to either the senders conn_sqp (for loopback)
1399 * or based on the ring (for packets from GLD). Otherwise it is
1400 * set based on lbolt i.e., a somewhat random number.
1402 ASSERT(ira
->ira_sqp
!= NULL
);
1403 new_sqp
= ira
->ira_sqp
;
1405 econnp
= (conn_t
*)tcp_get_conn(arg2
, tcps
);
1409 ASSERT(econnp
->conn_netstack
== lconnp
->conn_netstack
);
1410 econnp
->conn_sqp
= new_sqp
;
1411 econnp
->conn_initial_sqp
= new_sqp
;
1412 econnp
->conn_ixa
->ixa_sqp
= new_sqp
;
1414 econnp
->conn_fport
= tcpha
->tha_lport
;
1415 econnp
->conn_lport
= tcpha
->tha_fport
;
1417 err
= conn_inherit_parent(lconnp
, econnp
);
1421 /* We already know the laddr of the new connection is ours */
1422 econnp
->conn_ixa
->ixa_src_generation
= ipst
->ips_src_generation
;
1424 ASSERT(OK_32PTR(mp
->b_rptr
));
1425 ASSERT(IPH_HDR_VERSION(mp
->b_rptr
) == IPV4_VERSION
||
1426 IPH_HDR_VERSION(mp
->b_rptr
) == IPV6_VERSION
);
1428 if (lconnp
->conn_family
== AF_INET
) {
1429 ASSERT(IPH_HDR_VERSION(mp
->b_rptr
) == IPV4_VERSION
);
1430 tpi_mp
= tcp_conn_create_v4(lconnp
, econnp
, mp
, ira
);
1432 tpi_mp
= tcp_conn_create_v6(lconnp
, econnp
, mp
, ira
);
1438 eager
= econnp
->conn_tcp
;
1439 eager
->tcp_detached
= B_TRUE
;
1440 SOCK_CONNID_INIT(eager
->tcp_connid
);
1443 * Initialize the eager's tcp_t and inherit some parameters from
1446 tcp_init_values(eager
, listener
);
1448 ASSERT((econnp
->conn_ixa
->ixa_flags
&
1449 (IXAF_SET_ULP_CKSUM
| IXAF_VERIFY_SOURCE
|
1450 IXAF_VERIFY_PMTU
| IXAF_VERIFY_LSO
)) ==
1451 (IXAF_SET_ULP_CKSUM
| IXAF_VERIFY_SOURCE
|
1452 IXAF_VERIFY_PMTU
| IXAF_VERIFY_LSO
));
1454 if (!tcps
->tcps_dev_flow_ctl
)
1455 econnp
->conn_ixa
->ixa_flags
|= IXAF_NO_DEV_FLOW_CTL
;
1457 /* Prepare for diffing against previous packets */
1458 eager
->tcp_recvifindex
= 0;
1459 eager
->tcp_recvhops
= 0xffffffffU
;
1461 if (!(ira
->ira_flags
& IRAF_IS_IPV4
) && econnp
->conn_bound_if
== 0) {
1462 if (IN6_IS_ADDR_LINKSCOPE(&econnp
->conn_faddr_v6
) ||
1463 IN6_IS_ADDR_LINKSCOPE(&econnp
->conn_laddr_v6
)) {
1464 econnp
->conn_incoming_ifindex
= ifindex
;
1465 econnp
->conn_ixa
->ixa_flags
|= IXAF_SCOPEID_SET
;
1466 econnp
->conn_ixa
->ixa_scopeid
= ifindex
;
1470 if ((ira
->ira_flags
& (IRAF_IS_IPV4
|IRAF_IPV4_OPTIONS
)) ==
1471 (IRAF_IS_IPV4
|IRAF_IPV4_OPTIONS
) &&
1472 tcps
->tcps_rev_src_routes
) {
1473 ipha_t
*ipha
= (ipha_t
*)mp
->b_rptr
;
1474 ip_pkt_t
*ipp
= &econnp
->conn_xmit_ipp
;
1476 /* Source routing option copyover (reverse it) */
1477 err
= ip_find_hdr_v4(ipha
, ipp
, B_TRUE
);
1482 ip_pkt_source_route_reverse_v4(ipp
);
1485 ASSERT(eager
->tcp_conn
.tcp_eager_conn_ind
== NULL
);
1486 ASSERT(!eager
->tcp_tconnind_started
);
1488 * If the SYN came with a credential, it's a loopback packet; attach
1489 * the credential to the TPI message.
1491 if (ira
->ira_cred
!= NULL
)
1492 mblk_setcred(tpi_mp
, ira
->ira_cred
, ira
->ira_cpid
);
1494 eager
->tcp_conn
.tcp_eager_conn_ind
= tpi_mp
;
1495 ASSERT(eager
->tcp_ordrel_mp
== NULL
);
1497 /* Inherit the listener's non-STREAMS flag */
1498 if (IPCL_IS_NONSTR(lconnp
)) {
1499 econnp
->conn_flags
|= IPCL_NONSTR
;
1500 /* All non-STREAMS tcp_ts are sockets */
1501 eager
->tcp_issocket
= B_TRUE
;
1504 * Pre-allocate the T_ordrel_ind mblk for TPI socket so that
1505 * at close time, we will always have that to send up.
1506 * Otherwise, we need to do special handling in case the
1507 * allocation fails at that time.
1509 if ((eager
->tcp_ordrel_mp
= mi_tpi_ordrel_ind()) == NULL
)
1513 * Now that the IP addresses and ports are setup in econnp we
1514 * can do the IPsec policy work.
1516 if (ira
->ira_flags
& IRAF_IPSEC_SECURE
) {
1517 if (lconnp
->conn_policy
!= NULL
) {
1519 * Inherit the policy from the listener; use
1522 if (!ip_ipsec_policy_inherit(econnp
, lconnp
, ira
)) {
1523 CONN_DEC_REF(econnp
);
1531 * tcp_set_destination() may set tcp_rwnd according to the route
1532 * metrics. If it does not, the eager's receive window will be set
1533 * to the listener's receive window later in this function.
1535 eager
->tcp_rwnd
= 0;
1538 * Since we will clear tcp_listener before we clear tcp_detached
1539 * in the accept code we need tcp_hard_binding aka tcp_accept_inprogress
1540 * so we can tell a TCP_IS_DETACHED_NONEAGER apart.
1542 eager
->tcp_hard_binding
= B_TRUE
;
1544 tcp_bind_hash_insert(&tcps
->tcps_bind_fanout
[
1545 TCP_BIND_HASH(econnp
->conn_lport
)], eager
, 0);
1547 SOCK_CONNID_BUMP(eager
->tcp_connid
);
1550 * Adapt our mss, ttl, ... based on the remote address.
1553 if (tcp_set_destination(eager
) != 0) {
1554 TCPS_BUMP_MIB(tcps
, tcpAttemptFails
);
1555 /* Undo the bind_hash_insert */
1556 tcp_bind_hash_remove(eager
);
1560 /* Process all TCP options. */
1561 tcp_process_options(eager
, tcpha
);
1563 /* Is the other end ECN capable? */
1564 if (tcps
->tcps_ecn_permitted
>= 1 &&
1565 (tcpha
->tha_flags
& (TH_ECE
|TH_CWR
)) == (TH_ECE
|TH_CWR
)) {
1566 eager
->tcp_ecn_ok
= B_TRUE
;
1570 * The listener's conn_rcvbuf should be the default window size or a
1571 * window size changed via SO_RCVBUF option. First round up the
1572 * eager's tcp_rwnd to the nearest MSS. Then find out the window
1573 * scale option value if needed. Call tcp_rwnd_set() to finish the
1576 * Note if there is a rpipe metric associated with the remote host,
1577 * we should not inherit receive window size from listener.
1579 eager
->tcp_rwnd
= MSS_ROUNDUP(
1580 (eager
->tcp_rwnd
== 0 ? econnp
->conn_rcvbuf
:
1581 eager
->tcp_rwnd
), eager
->tcp_mss
);
1582 if (eager
->tcp_snd_ws_ok
)
1583 tcp_set_ws_value(eager
);
1585 * Note that this is the only place tcp_rwnd_set() is called for
1586 * accepting a connection. We need to call it here instead of
1587 * after the 3-way handshake because we need to tell the other
1588 * side our rwnd in the SYN-ACK segment.
1590 (void) tcp_rwnd_set(eager
, eager
->tcp_rwnd
);
1592 ASSERT(eager
->tcp_connp
->conn_rcvbuf
!= 0 &&
1593 eager
->tcp_connp
->conn_rcvbuf
== eager
->tcp_rwnd
);
1595 ASSERT(econnp
->conn_rcvbuf
!= 0 &&
1596 econnp
->conn_rcvbuf
== eager
->tcp_rwnd
);
1598 /* Put a ref on the listener for the eager. */
1599 CONN_INC_REF(lconnp
);
1600 mutex_enter(&listener
->tcp_eager_lock
);
1601 listener
->tcp_eager_next_q0
->tcp_eager_prev_q0
= eager
;
1602 eager
->tcp_eager_next_q0
= listener
->tcp_eager_next_q0
;
1603 listener
->tcp_eager_next_q0
= eager
;
1604 eager
->tcp_eager_prev_q0
= listener
;
1606 /* Set tcp_listener before adding it to tcp_conn_fanout */
1607 eager
->tcp_listener
= listener
;
1608 eager
->tcp_saved_listener
= listener
;
1611 * Set tcp_listen_cnt so that when the connection is done, the counter
1614 eager
->tcp_listen_cnt
= listener
->tcp_listen_cnt
;
1617 * Tag this detached tcp vector for later retrieval
1618 * by our listener client in tcp_accept().
1620 eager
->tcp_conn_req_seqnum
= listener
->tcp_conn_req_seqnum
;
1621 listener
->tcp_conn_req_cnt_q0
++;
1622 if (++listener
->tcp_conn_req_seqnum
== -1) {
1624 * -1 is "special" and defined in TPI as something
1625 * that should never be used in T_CONN_IND
1627 ++listener
->tcp_conn_req_seqnum
;
1629 mutex_exit(&listener
->tcp_eager_lock
);
1631 if (listener
->tcp_syn_defense
) {
1632 /* Don't drop the SYN that comes from a good IP source */
1633 ipaddr_t
*addr_cache
;
1635 addr_cache
= (ipaddr_t
*)(listener
->tcp_ip_addr_cache
);
1636 if (addr_cache
!= NULL
&& econnp
->conn_faddr_v4
==
1637 addr_cache
[IP_ADDR_CACHE_HASH(econnp
->conn_faddr_v4
)]) {
1638 eager
->tcp_dontdrop
= B_TRUE
;
1643 * We need to insert the eager in its own perimeter but as soon
1644 * as we do that, we expose the eager to the classifier and
1645 * should not touch any field outside the eager's perimeter.
1646 * So do all the work necessary before inserting the eager
1647 * in its own perimeter. Be optimistic that conn_connect()
1648 * will succeed but undo everything if it fails.
1650 seg_seq
= ntohl(tcpha
->tha_seq
);
1651 eager
->tcp_irs
= seg_seq
;
1652 eager
->tcp_rack
= seg_seq
;
1653 eager
->tcp_rnxt
= seg_seq
+ 1;
1654 eager
->tcp_tcpha
->tha_ack
= htonl(eager
->tcp_rnxt
);
1655 TCPS_BUMP_MIB(tcps
, tcpPassiveOpens
);
1656 eager
->tcp_state
= TCPS_SYN_RCVD
;
1657 DTRACE_TCP6(state__change
, void, NULL
, ip_xmit_attr_t
*,
1658 econnp
->conn_ixa
, void, NULL
, tcp_t
*, eager
, void, NULL
,
1659 int32_t, TCPS_LISTEN
);
1661 mp1
= tcp_xmit_mp(eager
, eager
->tcp_xmit_head
, eager
->tcp_mss
,
1662 NULL
, NULL
, eager
->tcp_iss
, B_FALSE
, NULL
, B_FALSE
);
1665 * Increment the ref count as we are going to
1666 * enqueueing an mp in squeue
1668 CONN_INC_REF(econnp
);
1673 * We need to start the rto timer. In normal case, we start
1674 * the timer after sending the packet on the wire (or at
1675 * least believing that packet was sent by waiting for
1676 * conn_ip_output() to return). Since this is the first packet
1677 * being sent on the wire for the eager, our initial tcp_rto
1678 * is at least tcp_rexmit_interval_min which is a fairly
1679 * large value to allow the algorithm to adjust slowly to large
1680 * fluctuations of RTT during first few transmissions.
1682 * Starting the timer first and then sending the packet in this
1683 * case shouldn't make much difference since tcp_rexmit_interval_min
1684 * is of the order of several 100ms and starting the timer
1685 * first and then sending the packet will result in difference
1686 * of few micro seconds.
1688 * Without this optimization, we are forced to hold the fanout
1689 * lock across the ipcl_bind_insert() and sending the packet
1690 * so that we don't race against an incoming packet (maybe RST)
1693 * It is necessary to acquire an extra reference on the eager
1694 * at this point and hold it until after tcp_send_data() to
1695 * ensure against an eager close race.
1698 CONN_INC_REF(econnp
);
1700 TCP_TIMER_RESTART(eager
, eager
->tcp_rto
);
1703 * Insert the eager in its own perimeter now. We are ready to deal
1704 * with any packets on eager.
1706 if (ipcl_conn_insert(econnp
) != 0)
1709 ASSERT(econnp
->conn_ixa
->ixa_notify_cookie
== econnp
->conn_tcp
);
1712 * Send the SYN-ACK. Use the right squeue so that conn_ixa is
1713 * only used by one thread at a time.
1715 if (econnp
->conn_sqp
== lconnp
->conn_sqp
) {
1716 DTRACE_TCP5(send
, mblk_t
*, NULL
, ip_xmit_attr_t
*,
1717 econnp
->conn_ixa
, __dtrace_tcp_void_ip_t
*, mp1
->b_rptr
,
1718 tcp_t
*, eager
, __dtrace_tcp_tcph_t
*,
1719 &mp1
->b_rptr
[econnp
->conn_ixa
->ixa_ip_hdr_length
]);
1720 (void) conn_ip_output(mp1
, econnp
->conn_ixa
);
1721 CONN_DEC_REF(econnp
);
1723 SQUEUE_ENTER_ONE(econnp
->conn_sqp
, mp1
, tcp_send_synack
,
1724 econnp
, NULL
, SQ_PROCESS
, SQTAG_TCP_SEND_SYNACK
);
1729 eager
->tcp_closemp_used
= B_TRUE
;
1730 TCP_DEBUG_GETPCSTACK(eager
->tcmp_stk
, 15);
1731 mp1
= &eager
->tcp_closemp
;
1732 SQUEUE_ENTER_ONE(econnp
->conn_sqp
, mp1
, tcp_eager_kill
,
1733 econnp
, NULL
, SQ_FILL
, SQTAG_TCP_CONN_REQ_2
);
1736 * If a connection already exists, send the mp to that connections so
1737 * that it can be appropriately dealt with.
1739 ipst
= tcps
->tcps_netstack
->netstack_ip
;
1741 if ((econnp
= ipcl_classify(mp
, ira
, ipst
)) != NULL
) {
1742 if (!IPCL_IS_CONNECTED(econnp
)) {
1744 * Something bad happened. ipcl_conn_insert()
1745 * failed because a connection already existed
1746 * in connected hash but we can't find it
1747 * anymore (someone blew it away). Just
1748 * free this message and hopefully remote
1749 * will retransmit at which time the SYN can be
1750 * treated as a new connection or dealth with
1751 * a TH_RST if a connection already exists.
1753 CONN_DEC_REF(econnp
);
1756 SQUEUE_ENTER_ONE(econnp
->conn_sqp
, mp
, tcp_input_data
,
1757 econnp
, ira
, SQ_FILL
, SQTAG_TCP_CONN_REQ_1
);
1760 /* Nobody wants this packet */
1765 CONN_DEC_REF(econnp
);
1769 atomic_dec_32(&listener
->tcp_listen_cnt
->tlc_cnt
);
1773 * In an ideal case of vertical partition in NUMA architecture, its
1774 * beneficial to have the listener and all the incoming connections
1775 * tied to the same squeue. The other constraint is that incoming
1776 * connections should be tied to the squeue attached to interrupted
1777 * CPU for obvious locality reason so this leaves the listener to
1778 * be tied to the same squeue. Our only problem is that when listener
1779 * is binding, the CPU that will get interrupted by the NIC whose
1780 * IP address the listener is binding to is not even known. So
1781 * the code below allows us to change that binding at the time the
1782 * CPU is interrupted by virtue of incoming connection's squeue.
1784 * This is usefull only in case of a listener bound to a specific IP
1785 * address. For other kind of listeners, they get bound the
1786 * very first time and there is no attempt to rebind them.
1789 tcp_input_listener_unbound(void *arg
, mblk_t
*mp
, void *arg2
,
1790 ip_recv_attr_t
*ira
)
1792 conn_t
*connp
= (conn_t
*)arg
;
1793 squeue_t
*sqp
= (squeue_t
*)arg2
;
1795 uint32_t conn_flags
;
1798 * IP sets ira_sqp to either the senders conn_sqp (for loopback)
1799 * or based on the ring (for packets from GLD). Otherwise it is
1800 * set based on lbolt i.e., a somewhat random number.
1802 ASSERT(ira
->ira_sqp
!= NULL
);
1803 new_sqp
= ira
->ira_sqp
;
1805 if (connp
->conn_fanout
== NULL
)
1808 if (!(connp
->conn_flags
& IPCL_FULLY_BOUND
)) {
1809 mutex_enter(&connp
->conn_fanout
->connf_lock
);
1810 mutex_enter(&connp
->conn_lock
);
1812 * No one from read or write side can access us now
1813 * except for already queued packets on this squeue.
1814 * But since we haven't changed the squeue yet, they
1815 * can't execute. If they are processed after we have
1816 * changed the squeue, they are sent back to the
1817 * correct squeue down below.
1818 * But a listner close can race with processing of
1819 * incoming SYN. If incoming SYN processing changes
1820 * the squeue then the listener close which is waiting
1821 * to enter the squeue would operate on the wrong
1822 * squeue. Hence we don't change the squeue here unless
1823 * the refcount is exactly the minimum refcount. The
1824 * minimum refcount of 4 is counted as - 1 each for
1825 * TCP and IP, 1 for being in the classifier hash, and
1826 * 1 for the mblk being processed.
1829 if (connp
->conn_ref
!= 4 ||
1830 connp
->conn_tcp
->tcp_state
!= TCPS_LISTEN
) {
1831 mutex_exit(&connp
->conn_lock
);
1832 mutex_exit(&connp
->conn_fanout
->connf_lock
);
1835 if (connp
->conn_sqp
!= new_sqp
) {
1836 while (connp
->conn_sqp
!= new_sqp
)
1837 (void) atomic_cas_ptr(&connp
->conn_sqp
, sqp
,
1839 /* No special MT issues for outbound ixa_sqp hint */
1840 connp
->conn_ixa
->ixa_sqp
= new_sqp
;
1844 conn_flags
= connp
->conn_flags
;
1845 conn_flags
|= IPCL_FULLY_BOUND
;
1846 (void) atomic_cas_32(&connp
->conn_flags
,
1847 connp
->conn_flags
, conn_flags
);
1848 } while (!(connp
->conn_flags
& IPCL_FULLY_BOUND
));
1850 mutex_exit(&connp
->conn_fanout
->connf_lock
);
1851 mutex_exit(&connp
->conn_lock
);
1854 * Assume we have picked a good squeue for the listener. Make
1855 * subsequent SYNs not try to change the squeue.
1857 connp
->conn_recv
= tcp_input_listener
;
1861 if (connp
->conn_sqp
!= sqp
) {
1862 CONN_INC_REF(connp
);
1863 SQUEUE_ENTER_ONE(connp
->conn_sqp
, mp
, connp
->conn_recv
, connp
,
1864 ira
, SQ_FILL
, SQTAG_TCP_CONN_REQ_UNBOUND
);
1866 tcp_input_listener(connp
, mp
, sqp
, ira
);
1871 * Send up all messages queued on tcp_rcv_list.
1874 tcp_rcv_drain(tcp_t
*tcp
)
1881 queue_t
*q
= tcp
->tcp_connp
->conn_rq
;
1883 /* Can't drain on an eager connection */
1884 if (tcp
->tcp_listener
!= NULL
)
1887 /* Can't be a non-STREAMS connection */
1888 ASSERT(!IPCL_IS_NONSTR(tcp
->tcp_connp
));
1890 /* No need for the push timer now. */
1891 if (tcp
->tcp_push_tid
!= 0) {
1892 (void) TCP_TIMER_CANCEL(tcp
, tcp
->tcp_push_tid
);
1893 tcp
->tcp_push_tid
= 0;
1897 * Handle two cases here: we are currently fused or we were
1898 * previously fused and have some urgent data to be delivered
1899 * upstream. The latter happens because we either ran out of
1900 * memory or were detached and therefore sending the SIGURG was
1901 * deferred until this point. In either case we pass control
1902 * over to tcp_fuse_rcv_drain() since it may need to complete
1905 if ((tcp
->tcp_fused
|| tcp
->tcp_fused_sigurg
)) {
1906 if (tcp_fuse_rcv_drain(q
, tcp
, tcp
->tcp_fused
? NULL
:
1907 &tcp
->tcp_fused_sigurg_mp
))
1911 while ((mp
= tcp
->tcp_rcv_list
) != NULL
) {
1912 tcp
->tcp_rcv_list
= mp
->b_next
;
1915 cnt
+= msgdsize(mp
);
1920 ASSERT(cnt
== tcp
->tcp_rcv_cnt
);
1922 tcp
->tcp_rcv_last_head
= NULL
;
1923 tcp
->tcp_rcv_last_tail
= NULL
;
1924 tcp
->tcp_rcv_cnt
= 0;
1927 return (tcp_rwnd_reopen(tcp
));
1933 * Queue data on tcp_rcv_list which is a b_next chain.
1934 * tcp_rcv_last_head/tail is the last element of this chain.
1935 * Each element of the chain is a b_cont chain.
1937 * M_DATA messages are added to the current element.
1938 * Other messages are added as new (b_next) elements.
1941 tcp_rcv_enqueue(tcp_t
*tcp
, mblk_t
*mp
, uint_t seg_len
, cred_t
*cr
)
1943 ASSERT(seg_len
== msgdsize(mp
));
1944 ASSERT(tcp
->tcp_rcv_list
== NULL
|| tcp
->tcp_rcv_last_head
!= NULL
);
1946 if (tcp
->tcp_rcv_list
== NULL
) {
1947 ASSERT(tcp
->tcp_rcv_last_head
== NULL
);
1948 tcp
->tcp_rcv_list
= mp
;
1949 tcp
->tcp_rcv_last_head
= mp
;
1950 } else if (DB_TYPE(mp
) == DB_TYPE(tcp
->tcp_rcv_last_head
)) {
1951 tcp
->tcp_rcv_last_tail
->b_cont
= mp
;
1953 tcp
->tcp_rcv_last_head
->b_next
= mp
;
1954 tcp
->tcp_rcv_last_head
= mp
;
1960 tcp
->tcp_rcv_last_tail
= mp
;
1961 tcp
->tcp_rcv_cnt
+= seg_len
;
1962 tcp
->tcp_rwnd
-= seg_len
;
1965 /* Generate an ACK-only (no data) segment for a TCP endpoint */
1967 tcp_ack_mp(tcp_t
*tcp
)
1970 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
1971 conn_t
*connp
= tcp
->tcp_connp
;
1974 * There are a few cases to be considered while setting the sequence no.
1975 * Essentially, we can come here while processing an unacceptable pkt
1976 * in the TCPS_SYN_RCVD state, in which case we set the sequence number
1977 * to snxt (per RFC 793), note the swnd wouldn't have been set yet.
1978 * If we are here for a zero window probe, stick with suna. In all
1979 * other cases, we check if suna + swnd encompasses snxt and set
1980 * the sequence number to snxt, if so. If snxt falls outside the
1981 * window (the receiver probably shrunk its window), we will go with
1982 * suna + swnd, otherwise the sequence no will be unacceptable to the
1985 if (tcp
->tcp_zero_win_probe
) {
1986 seq_no
= tcp
->tcp_suna
;
1987 } else if (tcp
->tcp_state
== TCPS_SYN_RCVD
) {
1988 ASSERT(tcp
->tcp_swnd
== 0);
1989 seq_no
= tcp
->tcp_snxt
;
1991 seq_no
= SEQ_GT(tcp
->tcp_snxt
,
1992 (tcp
->tcp_suna
+ tcp
->tcp_swnd
)) ?
1993 (tcp
->tcp_suna
+ tcp
->tcp_swnd
) : tcp
->tcp_snxt
;
1996 if (tcp
->tcp_valid_bits
) {
1998 * For the complex case where we have to send some
1999 * controls (FIN or SYN), let tcp_xmit_mp do it.
2001 return (tcp_xmit_mp(tcp
, NULL
, 0, NULL
, NULL
, seq_no
, B_FALSE
,
2004 /* Generate a simple ACK */
2009 int32_t total_hdr_len
;
2010 int32_t tcp_hdr_len
;
2011 int32_t num_sack_blk
= 0;
2012 int32_t sack_opt_len
;
2013 ip_xmit_attr_t
*ixa
= connp
->conn_ixa
;
2016 * Allocate space for TCP + IP headers
2017 * and link-level header
2019 if (tcp
->tcp_snd_sack_ok
&& tcp
->tcp_num_sack_blk
> 0) {
2020 num_sack_blk
= MIN(tcp
->tcp_max_sack_blk
,
2021 tcp
->tcp_num_sack_blk
);
2022 sack_opt_len
= num_sack_blk
* sizeof (sack_blk_t
) +
2023 TCPOPT_NOP_LEN
* 2 + TCPOPT_HEADER_LEN
;
2024 total_hdr_len
= connp
->conn_ht_iphc_len
+ sack_opt_len
;
2025 tcp_hdr_len
= connp
->conn_ht_ulp_len
+ sack_opt_len
;
2027 total_hdr_len
= connp
->conn_ht_iphc_len
;
2028 tcp_hdr_len
= connp
->conn_ht_ulp_len
;
2030 mp1
= allocb(total_hdr_len
+ tcps
->tcps_wroff_xtra
, BPRI_MED
);
2034 /* Update the latest receive window size in TCP header. */
2035 tcp
->tcp_tcpha
->tha_win
=
2036 htons(tcp
->tcp_rwnd
>> tcp
->tcp_rcv_ws
);
2037 /* copy in prototype TCP + IP header */
2038 rptr
= mp1
->b_rptr
+ tcps
->tcps_wroff_xtra
;
2040 mp1
->b_wptr
= rptr
+ total_hdr_len
;
2041 bcopy(connp
->conn_ht_iphc
, rptr
, connp
->conn_ht_iphc_len
);
2043 tcpha
= (tcpha_t
*)&rptr
[ixa
->ixa_ip_hdr_length
];
2045 /* Set the TCP sequence number. */
2046 tcpha
->tha_seq
= htonl(seq_no
);
2048 /* Set up the TCP flag field. */
2049 tcpha
->tha_flags
= (uchar_t
)TH_ACK
;
2050 if (tcp
->tcp_ecn_echo_on
)
2051 tcpha
->tha_flags
|= TH_ECE
;
2053 tcp
->tcp_rack
= tcp
->tcp_rnxt
;
2054 tcp
->tcp_rack_cnt
= 0;
2056 /* fill in timestamp option if in use */
2057 if (tcp
->tcp_snd_ts_ok
) {
2058 uint32_t llbolt
= (uint32_t)LBOLT_FASTPATH
;
2061 (char *)tcpha
+ TCP_MIN_HEADER_LENGTH
+4);
2062 U32_TO_BE32(tcp
->tcp_ts_recent
,
2063 (char *)tcpha
+ TCP_MIN_HEADER_LENGTH
+8);
2066 /* Fill in SACK options */
2067 if (num_sack_blk
> 0) {
2068 uchar_t
*wptr
= (uchar_t
*)tcpha
+
2069 connp
->conn_ht_ulp_len
;
2073 wptr
[0] = TCPOPT_NOP
;
2074 wptr
[1] = TCPOPT_NOP
;
2075 wptr
[2] = TCPOPT_SACK
;
2076 wptr
[3] = TCPOPT_HEADER_LEN
+ num_sack_blk
*
2077 sizeof (sack_blk_t
);
2078 wptr
+= TCPOPT_REAL_SACK_LEN
;
2080 tmp
= tcp
->tcp_sack_list
;
2081 for (i
= 0; i
< num_sack_blk
; i
++) {
2082 U32_TO_BE32(tmp
[i
].begin
, wptr
);
2083 wptr
+= sizeof (tcp_seq
);
2084 U32_TO_BE32(tmp
[i
].end
, wptr
);
2085 wptr
+= sizeof (tcp_seq
);
2087 tcpha
->tha_offset_and_reserved
+=
2088 ((num_sack_blk
* 2 + 1) << 4);
2091 ixa
->ixa_pktlen
= total_hdr_len
;
2093 if (ixa
->ixa_flags
& IXAF_IS_IPV4
) {
2094 ((ipha_t
*)rptr
)->ipha_length
= htons(total_hdr_len
);
2096 ip6_t
*ip6
= (ip6_t
*)rptr
;
2098 ip6
->ip6_plen
= htons(total_hdr_len
- IPV6_HDR_LEN
);
2102 * Prime pump for checksum calculation in IP. Include the
2103 * adjustment for a source route if any.
2105 data_length
= tcp_hdr_len
+ connp
->conn_sum
;
2106 data_length
= (data_length
>> 16) + (data_length
& 0xFFFF);
2107 tcpha
->tha_sum
= htons(data_length
);
2109 if (tcp
->tcp_ip_forward_progress
) {
2110 tcp
->tcp_ip_forward_progress
= B_FALSE
;
2111 connp
->conn_ixa
->ixa_flags
|= IXAF_REACH_CONF
;
2113 connp
->conn_ixa
->ixa_flags
&= ~IXAF_REACH_CONF
;
2120 * Dummy socket upcalls for if/when the conn_t gets detached from a
2121 * direct-callback sonode via a user-driven close(). Easy to catch with
2122 * DTrace FBT, and should be mostly harmless.
2126 static sock_upper_handle_t
2127 tcp_dummy_newconn(sock_upper_handle_t x
, sock_lower_handle_t y
,
2128 sock_downcalls_t
*z
, cred_t
*cr
, pid_t pid
, sock_upcalls_t
**ignored
)
2130 ASSERT(0); /* Panic in debug, otherwise ignore. */
2136 tcp_dummy_connected(sock_upper_handle_t x
, sock_connid_t y
, cred_t
*cr
,
2140 /* Normally we'd crhold(cr) and attach it to socket state. */
2146 tcp_dummy_disconnected(sock_upper_handle_t x
, sock_connid_t y
, int blah
)
2148 ASSERT(0); /* Panic in debug, otherwise ignore. */
2154 tcp_dummy_opctl(sock_upper_handle_t x
, sock_opctl_action_t y
, uintptr_t blah
)
2157 /* We really want this one to be a harmless NOP for now. */
2163 tcp_dummy_recv(sock_upper_handle_t x
, mblk_t
*mp
, size_t len
, int flags
,
2164 int *error
, boolean_t
*push
)
2169 * Consume the message, set ESHUTDOWN, and return an error.
2179 tcp_dummy_set_proto_props(sock_upper_handle_t x
, struct sock_proto_props
*y
)
2181 ASSERT(0); /* Panic in debug, otherwise ignore. */
2186 tcp_dummy_txq_full(sock_upper_handle_t x
, boolean_t y
)
2188 ASSERT(0); /* Panic in debug, otherwise ignore. */
2193 tcp_dummy_signal_oob(sock_upper_handle_t x
, ssize_t len
)
2196 /* Otherwise, this would signal socket state about OOB data. */
2201 tcp_dummy_set_error(sock_upper_handle_t x
, int err
)
2203 ASSERT(0); /* Panic in debug, otherwise ignore. */
2208 tcp_dummy_onearg(sock_upper_handle_t x
)
2210 ASSERT(0); /* Panic in debug, otherwise ignore. */
2213 static sock_upcalls_t tcp_dummy_upcalls
= {
2215 tcp_dummy_connected
,
2216 tcp_dummy_disconnected
,
2219 tcp_dummy_set_proto_props
,
2221 tcp_dummy_signal_oob
,
2223 tcp_dummy_set_error
,
2228 * Handle M_DATA messages from IP. Its called directly from IP via
2229 * squeue for received IP packets.
2231 * The first argument is always the connp/tcp to which the mp belongs.
2232 * There are no exceptions to this rule. The caller has already put
2233 * a reference on this connp/tcp and once tcp_input_data() returns,
2234 * the squeue will do the refrele.
2236 * The TH_SYN for the listener directly go to tcp_input_listener via
2237 * squeue. ICMP errors go directly to tcp_icmp_input().
2239 * sqp: NULL = recursive, sqp != NULL means called from squeue
2242 tcp_input_data(void *arg
, mblk_t
*mp
, void *arg2
, ip_recv_attr_t
*ira
)
2244 int32_t bytes_acked
;
2248 uint32_t new_swnd
= 0;
2260 boolean_t ofo_seg
= B_FALSE
; /* Out of order segment */
2265 conn_t
*connp
= (conn_t
*)arg
;
2266 squeue_t
*sqp
= (squeue_t
*)arg2
;
2267 tcp_t
*tcp
= connp
->conn_tcp
;
2268 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
2269 sock_upcalls_t
*sockupcalls
;
2272 * RST from fused tcp loopback peer should trigger an unfuse.
2274 if (tcp
->tcp_fused
) {
2275 TCP_STAT(tcps
, tcp_fusion_aborted
);
2281 ASSERT(OK_32PTR(rptr
));
2283 ip_hdr_len
= ira
->ira_ip_hdr_length
;
2284 if (connp
->conn_recv_ancillary
.crb_all
!= 0) {
2286 * Record packet information in the ip_pkt_t
2289 if (ira
->ira_flags
& IRAF_IS_IPV4
) {
2290 (void) ip_find_hdr_v4((ipha_t
*)rptr
, &ipp
,
2296 * IPv6 packets can only be received by applications
2297 * that are prepared to receive IPv6 addresses.
2298 * The IP fanout must ensure this.
2300 ASSERT(connp
->conn_family
== AF_INET6
);
2302 (void) ip_find_hdr_v6(mp
, (ip6_t
*)rptr
, &ipp
,
2304 ASSERT(nexthdrp
== IPPROTO_TCP
);
2306 /* Could have caused a pullup? */
2311 ASSERT(DB_TYPE(mp
) == M_DATA
);
2312 ASSERT(mp
->b_next
== NULL
);
2314 tcpha
= (tcpha_t
*)&rptr
[ip_hdr_len
];
2315 seg_seq
= ntohl(tcpha
->tha_seq
);
2316 seg_ack
= ntohl(tcpha
->tha_ack
);
2317 ASSERT((uintptr_t)(mp
->b_wptr
- rptr
) <= (uintptr_t)INT_MAX
);
2318 seg_len
= (int)(mp
->b_wptr
- rptr
) -
2319 (ip_hdr_len
+ TCP_HDR_LENGTH(tcpha
));
2320 if ((mp1
= mp
->b_cont
) != NULL
&& mp1
->b_datap
->db_type
== M_DATA
) {
2322 ASSERT((uintptr_t)(mp1
->b_wptr
- mp1
->b_rptr
) <=
2323 (uintptr_t)INT_MAX
);
2324 seg_len
+= (int)(mp1
->b_wptr
- mp1
->b_rptr
);
2325 } while ((mp1
= mp1
->b_cont
) != NULL
&&
2326 mp1
->b_datap
->db_type
== M_DATA
);
2329 DTRACE_TCP5(receive
, mblk_t
*, NULL
, ip_xmit_attr_t
*, connp
->conn_ixa
,
2330 __dtrace_tcp_void_ip_t
*, iphdr
, tcp_t
*, tcp
,
2331 __dtrace_tcp_tcph_t
*, tcpha
);
2333 if (tcp
->tcp_state
== TCPS_TIME_WAIT
) {
2334 tcp_time_wait_processing(tcp
, mp
, seg_seq
, seg_ack
,
2335 seg_len
, tcpha
, ira
);
2341 * This is the correct place to update tcp_last_recv_time. Note
2342 * that it is also updated for tcp structure that belongs to
2343 * global and listener queues which do not really need updating.
2344 * But that should not cause any harm. And it is updated for
2345 * all kinds of incoming segments, not only for data segments.
2347 tcp
->tcp_last_recv_time
= LBOLT_FASTPATH
;
2350 flags
= (unsigned int)tcpha
->tha_flags
& 0xFF;
2352 BUMP_LOCAL(tcp
->tcp_ibsegs
);
2353 DTRACE_PROBE2(tcp__trace__recv
, mblk_t
*, mp
, tcp_t
*, tcp
);
2355 if ((flags
& TH_URG
) && sqp
!= NULL
) {
2357 * TCP can't handle urgent pointers that arrive before
2358 * the connection has been accept()ed since it can't
2359 * buffer OOB data. Discard segment if this happens.
2361 * We can't just rely on a non-null tcp_listener to indicate
2362 * that the accept() has completed since unlinking of the
2363 * eager and completion of the accept are not atomic.
2364 * tcp_detached, when it is not set (B_FALSE) indicates
2365 * that the accept() has completed.
2367 * Nor can it reassemble urgent pointers, so discard
2368 * if it's not the next segment expected.
2370 * Otherwise, collapse chain into one mblk (discard if
2371 * that fails). This makes sure the headers, retransmitted
2372 * data, and new data all are in the same mblk.
2375 if (tcp
->tcp_detached
|| !pullupmsg(mp
, -1)) {
2379 /* Update pointers into message */
2380 iphdr
= rptr
= mp
->b_rptr
;
2381 tcpha
= (tcpha_t
*)&rptr
[ip_hdr_len
];
2382 if (SEQ_GT(seg_seq
, tcp
->tcp_rnxt
)) {
2384 * Since we can't handle any data with this urgent
2385 * pointer that is out of sequence, we expunge
2386 * the data. This allows us to still register
2387 * the urgent mark and generate the M_PCSIG,
2390 mp
->b_wptr
= (uchar_t
*)tcpha
+ TCP_HDR_LENGTH(tcpha
);
2395 sockupcalls
= connp
->conn_upcalls
;
2396 /* A conn_t may have belonged to a now-closed socket. Be careful. */
2397 if (sockupcalls
== NULL
)
2398 sockupcalls
= &tcp_dummy_upcalls
;
2400 switch (tcp
->tcp_state
) {
2402 if (connp
->conn_final_sqp
== NULL
&&
2403 tcp_outbound_squeue_switch
&& sqp
!= NULL
) {
2404 ASSERT(connp
->conn_initial_sqp
== connp
->conn_sqp
);
2405 connp
->conn_final_sqp
= sqp
;
2406 if (connp
->conn_final_sqp
!= connp
->conn_sqp
) {
2407 DTRACE_PROBE1(conn__final__sqp__switch
,
2409 CONN_INC_REF(connp
);
2410 SQUEUE_SWITCH(connp
, connp
->conn_final_sqp
);
2411 SQUEUE_ENTER_ONE(connp
->conn_sqp
, mp
,
2412 tcp_input_data
, connp
, ira
, ip_squeue_flag
,
2413 SQTAG_CONNECT_FINISH
);
2416 DTRACE_PROBE1(conn__final__sqp__same
, conn_t
*, connp
);
2418 if (flags
& TH_ACK
) {
2420 * Note that our stack cannot send data before a
2421 * connection is established, therefore the
2422 * following check is valid. Otherwise, it has
2425 if (SEQ_LEQ(seg_ack
, tcp
->tcp_iss
) ||
2426 SEQ_GT(seg_ack
, tcp
->tcp_snxt
)) {
2430 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq",
2431 tcp
, seg_ack
, 0, TH_RST
);
2434 ASSERT(tcp
->tcp_suna
+ 1 == seg_ack
);
2436 if (flags
& TH_RST
) {
2437 if (flags
& TH_ACK
) {
2438 DTRACE_TCP5(connect__refused
, mblk_t
*, NULL
,
2439 ip_xmit_attr_t
*, connp
->conn_ixa
,
2440 void_ip_t
*, iphdr
, tcp_t
*, tcp
,
2442 (void) tcp_clean_death(tcp
, ECONNREFUSED
);
2447 if (!(flags
& TH_SYN
)) {
2452 /* Process all TCP options. */
2453 tcp_process_options(tcp
, tcpha
);
2455 * The following changes our rwnd to be a multiple of the
2456 * MIN(peer MSS, our MSS) for performance reason.
2458 (void) tcp_rwnd_set(tcp
, MSS_ROUNDUP(connp
->conn_rcvbuf
,
2461 /* Is the other end ECN capable? */
2462 if (tcp
->tcp_ecn_ok
) {
2463 if ((flags
& (TH_ECE
|TH_CWR
)) != TH_ECE
) {
2464 tcp
->tcp_ecn_ok
= B_FALSE
;
2468 * Clear ECN flags because it may interfere with later
2471 flags
&= ~(TH_ECE
|TH_CWR
);
2473 tcp
->tcp_irs
= seg_seq
;
2474 tcp
->tcp_rack
= seg_seq
;
2475 tcp
->tcp_rnxt
= seg_seq
+ 1;
2476 tcp
->tcp_tcpha
->tha_ack
= htonl(tcp
->tcp_rnxt
);
2477 if (!TCP_IS_DETACHED(tcp
)) {
2478 /* Allocate room for SACK options if needed. */
2479 connp
->conn_wroff
= connp
->conn_ht_iphc_len
;
2480 if (tcp
->tcp_snd_sack_ok
)
2481 connp
->conn_wroff
+= TCPOPT_MAX_SACK_LEN
;
2482 if (!tcp
->tcp_loopback
)
2483 connp
->conn_wroff
+= tcps
->tcps_wroff_xtra
;
2485 (void) proto_set_tx_wroff(connp
->conn_rq
, connp
,
2488 if (flags
& TH_ACK
) {
2490 * If we can't get the confirmation upstream, pretend
2491 * we didn't even see this one.
2493 * XXX: how can we pretend we didn't see it if we
2494 * have updated rnxt et. al.
2496 * For loopback we defer sending up the T_CONN_CON
2497 * until after some checks below.
2501 * tcp_sendmsg() checks tcp_state without entering
2502 * the squeue so tcp_state should be updated before
2503 * sending up connection confirmation. Probe the
2504 * state change below when we are sure the connection
2505 * confirmation has been sent.
2507 tcp
->tcp_state
= TCPS_ESTABLISHED
;
2508 if (!tcp_conn_con(tcp
, iphdr
, mp
,
2509 tcp
->tcp_loopback
? &mp1
: NULL
, ira
)) {
2510 tcp
->tcp_state
= TCPS_SYN_SENT
;
2514 TCPS_CONN_INC(tcps
);
2515 /* SYN was acked - making progress */
2516 tcp
->tcp_ip_forward_progress
= B_TRUE
;
2518 /* One for the SYN */
2519 tcp
->tcp_suna
= tcp
->tcp_iss
+ 1;
2520 tcp
->tcp_valid_bits
&= ~TCP_ISS_VALID
;
2523 * If SYN was retransmitted, need to reset all
2524 * retransmission info. This is because this
2525 * segment will be treated as a dup ACK.
2527 if (tcp
->tcp_rexmit
) {
2528 tcp
->tcp_rexmit
= B_FALSE
;
2529 tcp
->tcp_rexmit_nxt
= tcp
->tcp_snxt
;
2530 tcp
->tcp_rexmit_max
= tcp
->tcp_snxt
;
2531 tcp
->tcp_ms_we_have_waited
= 0;
2534 * Set tcp_cwnd back to 1 MSS, per
2535 * recommendation from
2536 * draft-floyd-incr-init-win-01.txt,
2537 * Increasing TCP's Initial Window.
2539 tcp
->tcp_cwnd
= tcp
->tcp_mss
;
2542 tcp
->tcp_swl1
= seg_seq
;
2543 tcp
->tcp_swl2
= seg_ack
;
2545 new_swnd
= ntohs(tcpha
->tha_win
);
2546 tcp
->tcp_swnd
= new_swnd
;
2547 if (new_swnd
> tcp
->tcp_max_swnd
)
2548 tcp
->tcp_max_swnd
= new_swnd
;
2551 * Always send the three-way handshake ack immediately
2552 * in order to make the connection complete as soon as
2553 * possible on the accepting host.
2555 flags
|= TH_ACK_NEEDED
;
2558 * Trace connect-established here.
2560 DTRACE_TCP5(connect__established
, mblk_t
*, NULL
,
2561 ip_xmit_attr_t
*, tcp
->tcp_connp
->conn_ixa
,
2562 void_ip_t
*, iphdr
, tcp_t
*, tcp
, tcph_t
*, tcpha
);
2564 /* Trace change from SYN_SENT -> ESTABLISHED here */
2565 DTRACE_TCP6(state__change
, void, NULL
, ip_xmit_attr_t
*,
2566 connp
->conn_ixa
, void, NULL
, tcp_t
*, tcp
,
2567 void, NULL
, int32_t, TCPS_SYN_SENT
);
2570 * Special case for loopback. At this point we have
2571 * received SYN-ACK from the remote endpoint. In
2572 * order to ensure that both endpoints reach the
2573 * fused state prior to any data exchange, the final
2574 * ACK needs to be sent before we indicate T_CONN_CON
2575 * to the module upstream.
2577 if (tcp
->tcp_loopback
) {
2580 ASSERT(!tcp
->tcp_unfusable
);
2581 ASSERT(mp1
!= NULL
);
2583 * For loopback, we always get a pure SYN-ACK
2584 * and only need to send back the final ACK
2585 * with no data (this is because the other
2586 * tcp is ours and we don't do T/TCP). This
2587 * final ACK triggers the passive side to
2588 * perform fusion in ESTABLISHED state.
2590 if ((ack_mp
= tcp_ack_mp(tcp
)) != NULL
) {
2591 if (tcp
->tcp_ack_tid
!= 0) {
2592 (void) TCP_TIMER_CANCEL(tcp
,
2594 tcp
->tcp_ack_tid
= 0;
2596 tcp_send_data(tcp
, ack_mp
);
2597 BUMP_LOCAL(tcp
->tcp_obsegs
);
2598 TCPS_BUMP_MIB(tcps
, tcpOutAck
);
2600 if (!IPCL_IS_NONSTR(connp
)) {
2601 /* Send up T_CONN_CON */
2602 if (ira
->ira_cred
!= NULL
) {
2607 putnext(connp
->conn_rq
, mp1
);
2609 (*sockupcalls
->su_connected
)
2610 (connp
->conn_upper_handle
,
2621 * Forget fusion; we need to handle more
2622 * complex cases below. Send the deferred
2623 * T_CONN_CON message upstream and proceed
2624 * as usual. Mark this tcp as not capable
2627 TCP_STAT(tcps
, tcp_fusion_unfusable
);
2628 tcp
->tcp_unfusable
= B_TRUE
;
2629 if (!IPCL_IS_NONSTR(connp
)) {
2630 if (ira
->ira_cred
!= NULL
) {
2631 mblk_setcred(mp1
, ira
->ira_cred
,
2634 putnext(connp
->conn_rq
, mp1
);
2636 (*sockupcalls
->su_connected
)
2637 (connp
->conn_upper_handle
,
2638 tcp
->tcp_connid
, ira
->ira_cred
,
2645 * Check to see if there is data to be sent. If
2646 * yes, set the transmit flag. Then check to see
2647 * if received data processing needs to be done.
2648 * If not, go straight to xmit_check. This short
2649 * cut is OK as we don't support T/TCP.
2651 if (tcp
->tcp_unsent
)
2652 flags
|= TH_XMIT_NEEDED
;
2654 if (seg_len
== 0 && !(flags
& TH_URG
)) {
2663 tcp
->tcp_state
= TCPS_SYN_RCVD
;
2664 DTRACE_TCP6(state__change
, void, NULL
, ip_xmit_attr_t
*,
2665 connp
->conn_ixa
, void_ip_t
*, NULL
, tcp_t
*, tcp
,
2666 tcph_t
*, NULL
, int32_t, TCPS_SYN_SENT
);
2667 mp1
= tcp_xmit_mp(tcp
, tcp
->tcp_xmit_head
, tcp
->tcp_mss
,
2668 NULL
, NULL
, tcp
->tcp_iss
, B_FALSE
, NULL
, B_FALSE
);
2670 tcp_send_data(tcp
, mp1
);
2671 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
2676 if (flags
& TH_ACK
) {
2680 * In this state, a SYN|ACK packet is either bogus
2681 * because the other side must be ACKing our SYN which
2682 * indicates it has seen the ACK for their SYN and
2683 * shouldn't retransmit it or we're crossing SYNs
2686 if ((flags
& TH_SYN
) && !tcp
->tcp_active_open
) {
2688 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn",
2689 tcp
, seg_ack
, 0, TH_RST
);
2693 * NOTE: RFC 793 pg. 72 says this should be
2694 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt
2695 * but that would mean we have an ack that ignored
2698 if (SEQ_LEQ(seg_ack
, tcp
->tcp_suna
) ||
2699 SEQ_GT(seg_ack
, tcp
->tcp_snxt
)) {
2701 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack",
2702 tcp
, seg_ack
, 0, TH_RST
);
2706 * No sane TCP stack will send such a small window
2707 * without receiving any data. Just drop this invalid
2708 * ACK. We also shorten the abort timeout in case
2709 * this is an attack.
2711 pinit_wnd
= ntohs(tcpha
->tha_win
) << tcp
->tcp_snd_ws
;
2712 if (pinit_wnd
< tcp
->tcp_mss
&&
2713 pinit_wnd
< tcp_init_wnd_chk
) {
2715 TCP_STAT(tcps
, tcp_zwin_ack_syn
);
2716 tcp
->tcp_second_ctimer_threshold
=
2717 tcp_early_abort
* SECONDS
;
2724 * Only a TLI listener can come through this path when a
2725 * acceptor is going back to be a listener and a packet
2726 * for the acceptor hits the classifier. For a socket
2727 * listener, this can never happen because a listener
2728 * can never accept connection on itself and hence a
2729 * socket acceptor can not go back to being a listener.
2731 ASSERT(!TCP_IS_SOCKET(tcp
));
2736 ip_stack_t
*ipst
= tcps
->tcps_netstack
->netstack_ip
;
2739 * Don't accept any input on a closed tcp as this TCP logically
2740 * does not exist on the system. Don't proceed further with
2741 * this TCP. For instance, this packet could trigger another
2742 * close of this tcp which would be disastrous for tcp_refcnt.
2743 * tcp_close_detached / tcp_clean_death / tcp_closei_local must
2744 * be called at most once on a TCP. In this case we need to
2745 * refeed the packet into the classifier and figure out where
2746 * the packet should go.
2748 new_connp
= ipcl_classify(mp
, ira
, ipst
);
2749 if (new_connp
!= NULL
) {
2750 /* Drops ref on new_connp */
2751 tcp_reinput(new_connp
, mp
, ira
, ipst
);
2754 /* We failed to classify. For now just drop the packet */
2760 * Handle the case where the tcp_clean_death() has happened
2761 * on a connection (application hasn't closed yet) but a packet
2762 * was already queued on squeue before tcp_clean_death()
2763 * was processed. Calling tcp_clean_death() twice on same
2764 * connection can result in weird behaviour.
2773 * Already on the correct queue/perimeter.
2774 * If this is a detached connection and not an eager
2775 * connection hanging off a listener then new data
2776 * (past the FIN) will cause a reset.
2777 * We do a special check here where it
2778 * is out of the main line, rather than check
2779 * if we are detached every time we see new
2782 if (TCP_IS_DETACHED_NONEAGER(tcp
) &&
2783 (seg_len
> 0 && SEQ_GT(seg_seq
+ seg_len
, tcp
->tcp_rnxt
))) {
2784 TCPS_BUMP_MIB(tcps
, tcpInClosed
);
2785 DTRACE_PROBE2(tcp__trace__recv
, mblk_t
*, mp
, tcp_t
*, tcp
);
2787 tcp_xmit_ctl("new data when detached", tcp
,
2788 tcp
->tcp_snxt
, 0, TH_RST
);
2789 (void) tcp_clean_death(tcp
, EPROTO
);
2793 mp
->b_rptr
= (uchar_t
*)tcpha
+ TCP_HDR_LENGTH(tcpha
);
2794 urp
= ntohs(tcpha
->tha_urp
) - TCP_OLD_URP_INTERPRETATION
;
2795 new_swnd
= ntohs(tcpha
->tha_win
) <<
2796 ((tcpha
->tha_flags
& TH_SYN
) ? 0 : tcp
->tcp_snd_ws
);
2799 * We are interested in two TCP options: timestamps (if negotiated) and
2800 * SACK (if negotiated). Skip option parsing if neither is negotiated.
2802 if (tcp
->tcp_snd_ts_ok
|| tcp
->tcp_snd_sack_ok
) {
2804 if (tcp
->tcp_snd_sack_ok
)
2808 options
= tcp_parse_options(tcpha
, &tcpopt
);
2810 * RST segments must not be subject to PAWS and are not
2811 * required to have timestamps.
2812 * In addition, some TCP stacks (eg. Microsoft's) send
2813 * keepalive segments without timestamps even if timestamps are
2814 * negotiated on the connection. Keepalive segments are not
2815 * well-specified, but in practice they are ACK segments,
2816 * either empty or containing one garbage byte.
2818 boolean_t keepalive
= (flags
== TH_ACK
) && (seg_len
== 0 ||
2819 seg_len
== 1) && (seg_seq
+ 1 == tcp
->tcp_rnxt
);
2820 if (tcp
->tcp_snd_ts_ok
&& !(flags
& TH_RST
) && !keepalive
) {
2822 * Per RFC 7323 section 3.2., silently drop non-RST
2823 * segments without expected TSopt. This is a 'SHOULD'
2826 if (!(options
& TCP_OPT_TSTAMP_PRESENT
)) {
2828 * Leave a breadcrumb for people to detect this
2831 DTRACE_TCP1(droppedtimestamp
, tcp_t
*, tcp
);
2836 if (!tcp_paws_check(tcp
, &tcpopt
)) {
2838 * This segment is not acceptable.
2839 * Drop it and send back an ACK.
2842 flags
|= TH_ACK_NEEDED
;
2849 gap
= seg_seq
- tcp
->tcp_rnxt
;
2850 rgap
= tcp
->tcp_rwnd
- (gap
+ seg_len
);
2852 * gap is the amount of sequence space between what we expect to see
2853 * and what we got for seg_seq. A positive value for gap means
2854 * something got lost. A negative value means we got some old stuff.
2857 /* Old stuff present. Is the SYN in there? */
2858 if (seg_seq
== tcp
->tcp_irs
&& (flags
& TH_SYN
) &&
2863 /* Recompute the gaps after noting the SYN. */
2866 TCPS_BUMP_MIB(tcps
, tcpInDataDupSegs
);
2867 TCPS_UPDATE_MIB(tcps
, tcpInDataDupBytes
,
2868 (seg_len
> -gap
? -gap
: seg_len
));
2869 /* Remove the old stuff from seg_len. */
2873 * Make sure to check for unack'd FIN when rest of data
2874 * has been previously ack'd.
2876 if (seg_len
< 0 || (seg_len
== 0 && !(flags
& TH_FIN
))) {
2878 * Resets are only valid if they lie within our offered
2879 * window. If the RST bit is set, we just ignore this
2882 if (flags
& TH_RST
) {
2888 * The arriving of dup data packets indicate that we
2889 * may have postponed an ack for too long, or the other
2890 * side's RTT estimate is out of shape. Start acking
2893 if (SEQ_GEQ(seg_seq
+ seg_len
- gap
, tcp
->tcp_rack
) &&
2894 tcp
->tcp_rack_cnt
>= 1 &&
2895 tcp
->tcp_rack_abs_max
> 2) {
2896 tcp
->tcp_rack_abs_max
--;
2898 tcp
->tcp_rack_cur_max
= 1;
2901 * This segment is "unacceptable". None of its
2902 * sequence space lies within our advertized window.
2904 * Adjust seg_len to the original value for tracing.
2907 if (connp
->conn_debug
) {
2908 (void) strlog(TCP_MOD_ID
, 0, 1, SL_TRACE
,
2909 "tcp_rput: unacceptable, gap %d, rgap %d, "
2910 "flags 0x%x, seg_seq %u, seg_ack %u, "
2911 "seg_len %d, rnxt %u, snxt %u, %s",
2912 gap
, rgap
, flags
, seg_seq
, seg_ack
,
2913 seg_len
, tcp
->tcp_rnxt
, tcp
->tcp_snxt
,
2914 tcp_display(tcp
, NULL
,
2915 DISP_ADDR_AND_PORT
));
2919 * Arrange to send an ACK in response to the
2920 * unacceptable segment per RFC 793 page 69. There
2921 * is only one small difference between ours and the
2922 * acceptability test in the RFC - we accept ACK-only
2923 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK
2924 * will be generated.
2926 * Note that we have to ACK an ACK-only packet at least
2927 * for stacks that send 0-length keep-alives with
2928 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122,
2929 * section 4.2.3.6. As long as we don't ever generate
2930 * an unacceptable packet in response to an incoming
2931 * packet that is unacceptable, it should not cause
2934 flags
|= TH_ACK_NEEDED
;
2937 * Continue processing this segment in order to use the
2938 * ACK information it contains, but skip all other
2939 * sequence-number processing. Processing the ACK
2940 * information is necessary in order to
2941 * re-synchronize connections that may have lost
2944 * We clear seg_len and flag fields related to
2945 * sequence number processing as they are not
2946 * to be trusted for an unacceptable segment.
2949 flags
&= ~(TH_SYN
| TH_FIN
| TH_URG
);
2953 /* Fix seg_seq, and chew the gap off the front. */
2954 seg_seq
= tcp
->tcp_rnxt
;
2958 ASSERT((uintptr_t)(mp
->b_wptr
- mp
->b_rptr
) <=
2959 (uintptr_t)UINT_MAX
);
2960 gap
+= (uint_t
)(mp
->b_wptr
- mp
->b_rptr
);
2962 mp
->b_rptr
= mp
->b_wptr
- gap
;
2970 * If the urgent data has already been acknowledged, we
2971 * should ignore TH_URG below
2977 * rgap is the amount of stuff received out of window. A negative
2978 * value is the amount out of window.
2983 if (tcp
->tcp_rwnd
== 0) {
2984 TCPS_BUMP_MIB(tcps
, tcpInWinProbe
);
2986 TCPS_BUMP_MIB(tcps
, tcpInDataPastWinSegs
);
2987 TCPS_UPDATE_MIB(tcps
, tcpInDataPastWinBytes
, -rgap
);
2991 * seg_len does not include the FIN, so if more than
2992 * just the FIN is out of window, we act like we don't
2993 * see it. (If just the FIN is out of window, rgap
2994 * will be zero and we will go ahead and acknowledge
2999 /* Fix seg_len and make sure there is something left. */
3003 * Resets are only valid if they lie within our offered
3004 * window. If the RST bit is set, we just ignore this
3007 if (flags
& TH_RST
) {
3012 /* Per RFC 793, we need to send back an ACK. */
3013 flags
|= TH_ACK_NEEDED
;
3016 * Send SIGURG as soon as possible i.e. even
3017 * if the TH_URG was delivered in a window probe
3018 * packet (which will be unacceptable).
3020 * We generate a signal if none has been generated
3021 * for this connection or if this is a new urgent
3022 * byte. Also send a zero-length "unmarked" message
3023 * to inform SIOCATMARK that this is not the mark.
3025 * tcp_urp_last_valid is cleared when the T_exdata_ind
3026 * is sent up. This plus the check for old data
3027 * (gap >= 0) handles the wraparound of the sequence
3028 * number space without having to always track the
3029 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks
3030 * this max in its rcv_up variable).
3032 * This prevents duplicate SIGURGS due to a "late"
3033 * zero-window probe when the T_EXDATA_IND has already
3036 if ((flags
& TH_URG
) &&
3037 (!tcp
->tcp_urp_last_valid
|| SEQ_GT(urp
+ seg_seq
,
3038 tcp
->tcp_urp_last
))) {
3039 if (IPCL_IS_NONSTR(connp
)) {
3040 if (!TCP_IS_DETACHED(tcp
)) {
3041 (*sockupcalls
->su_signal_oob
)
3042 (connp
->conn_upper_handle
,
3046 mp1
= allocb(0, BPRI_MED
);
3051 if (!TCP_IS_DETACHED(tcp
) &&
3052 !putnextctl1(connp
->conn_rq
,
3054 /* Try again on the rexmit. */
3060 * If the next byte would be the mark
3061 * then mark with MARKNEXT else mark
3064 if (gap
== 0 && urp
== 0)
3065 mp1
->b_flag
|= MSGMARKNEXT
;
3067 mp1
->b_flag
|= MSGNOTMARKNEXT
;
3068 freemsg(tcp
->tcp_urp_mark_mp
);
3069 tcp
->tcp_urp_mark_mp
= mp1
;
3070 flags
|= TH_SEND_URP_MARK
;
3072 tcp
->tcp_urp_last_valid
= B_TRUE
;
3073 tcp
->tcp_urp_last
= urp
+ seg_seq
;
3076 * If this is a zero window probe, continue to
3077 * process the ACK part. But we need to set seg_len
3078 * to 0 to avoid data processing. Otherwise just
3079 * drop the segment and send back an ACK.
3081 if (tcp
->tcp_rwnd
== 0 && seg_seq
== tcp
->tcp_rnxt
) {
3082 flags
&= ~(TH_SYN
| TH_URG
);
3090 /* Pitch out of window stuff off the end. */
3094 ASSERT((uintptr_t)(mp2
->b_wptr
- mp2
->b_rptr
) <=
3095 (uintptr_t)INT_MAX
);
3096 rgap
-= (int)(mp2
->b_wptr
- mp2
->b_rptr
);
3098 mp2
->b_wptr
+= rgap
;
3099 if ((mp1
= mp2
->b_cont
) != NULL
) {
3105 } while ((mp2
= mp2
->b_cont
) != NULL
);
3109 * TCP should check ECN info for segments inside the window only.
3110 * Therefore the check should be done here.
3112 if (tcp
->tcp_ecn_ok
) {
3113 if (flags
& TH_CWR
) {
3114 tcp
->tcp_ecn_echo_on
= B_FALSE
;
3117 * Note that both ECN_CE and CWR can be set in the
3118 * same segment. In this case, we once again turn
3121 if (connp
->conn_ipversion
== IPV4_VERSION
) {
3122 uchar_t tos
= ((ipha_t
*)rptr
)->ipha_type_of_service
;
3124 if ((tos
& IPH_ECN_CE
) == IPH_ECN_CE
) {
3125 tcp
->tcp_ecn_echo_on
= B_TRUE
;
3128 uint32_t vcf
= ((ip6_t
*)rptr
)->ip6_vcf
;
3130 if ((vcf
& htonl(IPH_ECN_CE
<< 20)) ==
3131 htonl(IPH_ECN_CE
<< 20)) {
3132 tcp
->tcp_ecn_echo_on
= B_TRUE
;
3138 * Check whether we can update tcp_ts_recent. This test is from RFC
3139 * 7323, section 5.3.
3141 if (tcp
->tcp_snd_ts_ok
&& !(flags
& TH_RST
) &&
3142 TSTMP_GEQ(tcpopt
.tcp_opt_ts_val
, tcp
->tcp_ts_recent
) &&
3143 SEQ_LEQ(seg_seq
, tcp
->tcp_rack
)) {
3144 tcp
->tcp_ts_recent
= tcpopt
.tcp_opt_ts_val
;
3145 tcp
->tcp_last_rcv_lbolt
= LBOLT_FASTPATH64
;
3148 if (seg_seq
!= tcp
->tcp_rnxt
|| tcp
->tcp_reass_head
) {
3150 * FIN in an out of order segment. We record this in
3151 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq.
3152 * Clear the FIN so that any check on FIN flag will fail.
3153 * Remember that FIN also counts in the sequence number
3154 * space. So we need to ack out of order FIN only segments.
3156 if (flags
& TH_FIN
) {
3157 tcp
->tcp_valid_bits
|= TCP_OFO_FIN_VALID
;
3158 tcp
->tcp_ofo_fin_seq
= seg_seq
+ seg_len
;
3160 flags
|= TH_ACK_NEEDED
;
3163 /* Fill in the SACK blk list. */
3164 if (tcp
->tcp_snd_sack_ok
) {
3165 tcp_sack_insert(tcp
->tcp_sack_list
,
3166 seg_seq
, seg_seq
+ seg_len
,
3167 &(tcp
->tcp_num_sack_blk
));
3171 * Attempt reassembly and see if we have something
3174 mp
= tcp_reass(tcp
, mp
, seg_seq
);
3175 /* Always ack out of order packets */
3176 flags
|= TH_ACK_NEEDED
| TH_PUSH
;
3178 ASSERT((uintptr_t)(mp
->b_wptr
- mp
->b_rptr
) <=
3179 (uintptr_t)INT_MAX
);
3180 seg_len
= mp
->b_cont
? msgdsize(mp
) :
3181 (int)(mp
->b_wptr
- mp
->b_rptr
);
3182 seg_seq
= tcp
->tcp_rnxt
;
3184 * A gap is filled and the seq num and len
3185 * of the gap match that of a previously
3186 * received FIN, put the FIN flag back in.
3188 if ((tcp
->tcp_valid_bits
& TCP_OFO_FIN_VALID
) &&
3189 seg_seq
+ seg_len
== tcp
->tcp_ofo_fin_seq
) {
3191 tcp
->tcp_valid_bits
&=
3194 if (tcp
->tcp_reass_tid
!= 0) {
3195 (void) TCP_TIMER_CANCEL(tcp
,
3196 tcp
->tcp_reass_tid
);
3198 * Restart the timer if there is still
3199 * data in the reassembly queue.
3201 if (tcp
->tcp_reass_head
!= NULL
) {
3202 tcp
->tcp_reass_tid
= TCP_TIMER(
3203 tcp
, tcp_reass_timer
,
3204 tcps
->tcps_reass_timeout
);
3206 tcp
->tcp_reass_tid
= 0;
3211 * Keep going even with NULL mp.
3212 * There may be a useful ACK or something else
3213 * we don't want to miss.
3215 * But TCP should not perform fast retransmit
3216 * because of the ack number. TCP uses
3217 * seg_len == 0 to determine if it is a pure
3218 * ACK. And this is not a pure ACK.
3223 if (tcps
->tcps_reass_timeout
!= 0 &&
3224 tcp
->tcp_reass_tid
== 0) {
3225 tcp
->tcp_reass_tid
= TCP_TIMER(tcp
,
3227 tcps
->tcps_reass_timeout
);
3231 } else if (seg_len
> 0) {
3232 TCPS_BUMP_MIB(tcps
, tcpInDataInorderSegs
);
3233 TCPS_UPDATE_MIB(tcps
, tcpInDataInorderBytes
, seg_len
);
3235 * If an out of order FIN was received before, and the seq
3236 * num and len of the new segment match that of the FIN,
3237 * put the FIN flag back in.
3239 if ((tcp
->tcp_valid_bits
& TCP_OFO_FIN_VALID
) &&
3240 seg_seq
+ seg_len
== tcp
->tcp_ofo_fin_seq
) {
3242 tcp
->tcp_valid_bits
&= ~TCP_OFO_FIN_VALID
;
3245 if ((flags
& (TH_RST
| TH_SYN
| TH_URG
| TH_ACK
)) != TH_ACK
) {
3246 if (flags
& TH_RST
) {
3248 switch (tcp
->tcp_state
) {
3250 (void) tcp_clean_death(tcp
, ECONNREFUSED
);
3252 case TCPS_ESTABLISHED
:
3253 case TCPS_FIN_WAIT_1
:
3254 case TCPS_FIN_WAIT_2
:
3255 case TCPS_CLOSE_WAIT
:
3256 (void) tcp_clean_death(tcp
, ECONNRESET
);
3260 (void) tcp_clean_death(tcp
, 0);
3263 ASSERT(tcp
->tcp_state
!= TCPS_TIME_WAIT
);
3264 (void) tcp_clean_death(tcp
, ENXIO
);
3269 if (flags
& TH_SYN
) {
3271 * See RFC 793, Page 71
3273 * The seq number must be in the window as it should
3274 * be "fixed" above. If it is outside window, it should
3275 * be already rejected. Note that we allow seg_seq to be
3276 * rnxt + rwnd because we want to accept 0 window probe.
3278 ASSERT(SEQ_GEQ(seg_seq
, tcp
->tcp_rnxt
) &&
3279 SEQ_LEQ(seg_seq
, tcp
->tcp_rnxt
+ tcp
->tcp_rwnd
));
3282 * If the ACK flag is not set, just use our snxt as the
3283 * seq number of the RST segment.
3285 if (!(flags
& TH_ACK
)) {
3286 seg_ack
= tcp
->tcp_snxt
;
3288 tcp_xmit_ctl("TH_SYN", tcp
, seg_ack
, seg_seq
+ 1,
3290 ASSERT(tcp
->tcp_state
!= TCPS_TIME_WAIT
);
3291 (void) tcp_clean_death(tcp
, ECONNRESET
);
3295 * urp could be -1 when the urp field in the packet is 0
3296 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent
3297 * byte was at seg_seq - 1, in which case we ignore the urgent flag.
3299 if (flags
& TH_URG
&& urp
>= 0) {
3300 if (!tcp
->tcp_urp_last_valid
||
3301 SEQ_GT(urp
+ seg_seq
, tcp
->tcp_urp_last
)) {
3303 * Non-STREAMS sockets handle the urgent data a litte
3304 * differently from STREAMS based sockets. There is no
3305 * need to mark any mblks with the MSG{NOT,}MARKNEXT
3306 * flags to keep SIOCATMARK happy. Instead a
3307 * su_signal_oob upcall is made to update the mark.
3308 * Neither is a T_EXDATA_IND mblk needed to be
3309 * prepended to the urgent data. The urgent data is
3310 * delivered using the su_recv upcall, where we set
3311 * the MSG_OOB flag to indicate that it is urg data.
3313 * Neither TH_SEND_URP_MARK nor TH_MARKNEXT_NEEDED
3314 * are used by non-STREAMS sockets.
3316 if (IPCL_IS_NONSTR(connp
)) {
3317 if (!TCP_IS_DETACHED(tcp
)) {
3318 (*sockupcalls
->su_signal_oob
)
3319 (connp
->conn_upper_handle
, urp
);
3323 * If we haven't generated the signal yet for
3324 * this urgent pointer value, do it now. Also,
3325 * send up a zero-length M_DATA indicating
3326 * whether or not this is the mark. The latter
3327 * is not needed when a T_EXDATA_IND is sent up.
3328 * However, if there are allocation failures
3329 * this code relies on the sender retransmitting
3330 * and the socket code for determining the mark
3331 * should not block waiting for the peer to
3332 * transmit. Thus, for simplicity we always
3333 * send up the mark indication.
3335 mp1
= allocb(0, BPRI_MED
);
3340 if (!TCP_IS_DETACHED(tcp
) &&
3341 !putnextctl1(connp
->conn_rq
, M_PCSIG
,
3343 /* Try again on the rexmit. */
3349 * Mark with NOTMARKNEXT for now.
3350 * The code below will change this to MARKNEXT
3351 * if we are at the mark.
3353 * If there are allocation failures (e.g. in
3354 * dupmsg below) the next time tcp_input_data
3355 * sees the urgent segment it will send up the
3356 * MSGMARKNEXT message.
3358 mp1
->b_flag
|= MSGNOTMARKNEXT
;
3359 freemsg(tcp
->tcp_urp_mark_mp
);
3360 tcp
->tcp_urp_mark_mp
= mp1
;
3361 flags
|= TH_SEND_URP_MARK
;
3363 (void) strlog(TCP_MOD_ID
, 0, 1, SL_TRACE
,
3364 "tcp_rput: sent M_PCSIG 2 seq %x urp %x "
3366 seg_seq
, urp
, tcp
->tcp_urp_last
,
3367 tcp_display(tcp
, NULL
, DISP_PORT_ONLY
));
3370 tcp
->tcp_urp_last_valid
= B_TRUE
;
3371 tcp
->tcp_urp_last
= urp
+ seg_seq
;
3372 } else if (tcp
->tcp_urp_mark_mp
!= NULL
) {
3374 * An allocation failure prevented the previous
3375 * tcp_input_data from sending up the allocated
3376 * MSG*MARKNEXT message - send it up this time
3379 flags
|= TH_SEND_URP_MARK
;
3383 * If the urgent byte is in this segment, make sure that it is
3384 * all by itself. This makes it much easier to deal with the
3385 * possibility of an allocation failure on the T_exdata_ind.
3386 * Note that seg_len is the number of bytes in the segment, and
3387 * urp is the offset into the segment of the urgent byte.
3388 * urp < seg_len means that the urgent byte is in this segment.
3390 if (urp
< seg_len
) {
3394 * Break it up and feed it back in.
3395 * Re-attach the IP header.
3400 * There is stuff before the urgent
3406 * Trim from urgent byte on.
3407 * The rest will come back.
3411 tcp_input_data(connp
,
3415 (void) adjmsg(mp1
, urp
- seg_len
);
3416 /* Feed this piece back in. */
3417 tmp_rnxt
= tcp
->tcp_rnxt
;
3418 tcp_input_data(connp
, mp1
, NULL
, ira
);
3420 * If the data passed back in was not
3421 * processed (ie: bad ACK) sending
3422 * the remainder back in will cause a
3423 * loop. In this case, drop the
3424 * packet and let the sender try
3425 * sending a good packet.
3427 if (tmp_rnxt
== tcp
->tcp_rnxt
) {
3432 if (urp
!= seg_len
- 1) {
3435 * There is stuff after the urgent
3441 * Trim everything beyond the
3442 * urgent byte. The rest will
3447 tcp_input_data(connp
,
3451 (void) adjmsg(mp1
, urp
+ 1 - seg_len
);
3452 tmp_rnxt
= tcp
->tcp_rnxt
;
3453 tcp_input_data(connp
, mp1
, NULL
, ira
);
3455 * If the data passed back in was not
3456 * processed (ie: bad ACK) sending
3457 * the remainder back in will cause a
3458 * loop. In this case, drop the
3459 * packet and let the sender try
3460 * sending a good packet.
3462 if (tmp_rnxt
== tcp
->tcp_rnxt
) {
3467 tcp_input_data(connp
, mp
, NULL
, ira
);
3471 * This segment contains only the urgent byte. We
3472 * have to allocate the T_exdata_ind, if we can.
3474 if (IPCL_IS_NONSTR(connp
)) {
3477 (*sockupcalls
->su_recv
)
3478 (connp
->conn_upper_handle
, mp
, seg_len
,
3479 MSG_OOB
, &error
, NULL
);
3481 * We should never be in middle of a
3482 * fallback, the squeue guarantees that.
3484 ASSERT(error
!= EOPNOTSUPP
);
3487 } else if (!tcp
->tcp_urp_mp
) {
3488 struct T_exdata_ind
*tei
;
3489 mp1
= allocb(sizeof (struct T_exdata_ind
),
3493 * Sigh... It'll be back.
3494 * Generate any MSG*MARK message now.
3498 if (flags
& TH_SEND_URP_MARK
) {
3501 ASSERT(tcp
->tcp_urp_mark_mp
);
3502 tcp
->tcp_urp_mark_mp
->b_flag
&=
3504 tcp
->tcp_urp_mark_mp
->b_flag
|=
3509 mp1
->b_datap
->db_type
= M_PROTO
;
3510 tei
= (struct T_exdata_ind
*)mp1
->b_rptr
;
3511 tei
->PRIM_type
= T_EXDATA_IND
;
3513 mp1
->b_wptr
= (uchar_t
*)&tei
[1];
3514 tcp
->tcp_urp_mp
= mp1
;
3516 (void) strlog(TCP_MOD_ID
, 0, 1, SL_TRACE
,
3517 "tcp_rput: allocated exdata_ind %s",
3518 tcp_display(tcp
, NULL
,
3522 * There is no need to send a separate MSG*MARK
3523 * message since the T_EXDATA_IND will be sent
3526 flags
&= ~TH_SEND_URP_MARK
;
3527 freemsg(tcp
->tcp_urp_mark_mp
);
3528 tcp
->tcp_urp_mark_mp
= NULL
;
3531 * Now we are all set. On the next putnext upstream,
3532 * tcp_urp_mp will be non-NULL and will get prepended
3533 * to what has to be this piece containing the urgent
3534 * byte. If for any reason we abort this segment below,
3535 * if it comes back, we will have this ready, or it
3536 * will get blown off in close.
3538 } else if (urp
== seg_len
) {
3540 * The urgent byte is the next byte after this sequence
3541 * number. If this endpoint is non-STREAMS, then there
3542 * is nothing to do here since the socket has already
3543 * been notified about the urg pointer by the
3544 * su_signal_oob call above.
3546 * In case of STREAMS, some more work might be needed.
3547 * If there is data it is marked with MSGMARKNEXT and
3548 * and any tcp_urp_mark_mp is discarded since it is not
3549 * needed. Otherwise, if the code above just allocated
3550 * a zero-length tcp_urp_mark_mp message, that message
3551 * is tagged with MSGMARKNEXT. Sending up these
3552 * MSGMARKNEXT messages makes SIOCATMARK work correctly
3553 * even though the T_EXDATA_IND will not be sent up
3554 * until the urgent byte arrives.
3556 if (!IPCL_IS_NONSTR(tcp
->tcp_connp
)) {
3558 flags
|= TH_MARKNEXT_NEEDED
;
3559 freemsg(tcp
->tcp_urp_mark_mp
);
3560 tcp
->tcp_urp_mark_mp
= NULL
;
3561 flags
&= ~TH_SEND_URP_MARK
;
3562 } else if (tcp
->tcp_urp_mark_mp
!= NULL
) {
3563 flags
|= TH_SEND_URP_MARK
;
3564 tcp
->tcp_urp_mark_mp
->b_flag
&=
3566 tcp
->tcp_urp_mark_mp
->b_flag
|=
3571 (void) strlog(TCP_MOD_ID
, 0, 1, SL_TRACE
,
3572 "tcp_rput: AT MARK, len %d, flags 0x%x, %s",
3574 tcp_display(tcp
, NULL
, DISP_PORT_ONLY
));
3579 /* Data left until we hit mark */
3580 (void) strlog(TCP_MOD_ID
, 0, 1, SL_TRACE
,
3581 "tcp_rput: URP %d bytes left, %s",
3582 urp
- seg_len
, tcp_display(tcp
, NULL
,
3589 if (!(flags
& TH_ACK
)) {
3594 bytes_acked
= (int)(seg_ack
- tcp
->tcp_suna
);
3596 if (bytes_acked
> 0)
3597 tcp
->tcp_ip_forward_progress
= B_TRUE
;
3598 if (tcp
->tcp_state
== TCPS_SYN_RCVD
) {
3600 * tcp_sendmsg() checks tcp_state without entering
3601 * the squeue so tcp_state should be updated before
3602 * sending up a connection confirmation or a new
3603 * connection indication.
3605 tcp
->tcp_state
= TCPS_ESTABLISHED
;
3608 * We are seeing the final ack in the three way
3609 * hand shake of a active open'ed connection
3610 * so we must send up a T_CONN_CON
3612 if (tcp
->tcp_active_open
) {
3613 if (!tcp_conn_con(tcp
, iphdr
, mp
, NULL
, ira
)) {
3615 tcp
->tcp_state
= TCPS_SYN_RCVD
;
3619 * Don't fuse the loopback endpoints for
3620 * simultaneous active opens.
3622 if (tcp
->tcp_loopback
) {
3623 TCP_STAT(tcps
, tcp_fusion_unfusable
);
3624 tcp
->tcp_unfusable
= B_TRUE
;
3627 * For simultaneous active open, trace receipt of final
3628 * ACK as tcp:::connect-established.
3630 DTRACE_TCP5(connect__established
, mblk_t
*, NULL
,
3631 ip_xmit_attr_t
*, connp
->conn_ixa
, void_ip_t
*,
3632 iphdr
, tcp_t
*, tcp
, tcph_t
*, tcpha
);
3633 } else if (IPCL_IS_NONSTR(connp
)) {
3635 * 3-way handshake has completed, so notify socket
3636 * of the new connection.
3638 * We are here means eager is fine but it can
3639 * get a TH_RST at any point between now and till
3640 * accept completes and disappear. We need to
3641 * ensure that reference to eager is valid after
3642 * we get out of eager's perimeter. So we do
3645 CONN_INC_REF(connp
);
3647 if (!tcp_newconn_notify(tcp
, ira
)) {
3649 * The state-change probe for SYN_RCVD ->
3650 * ESTABLISHED has not fired yet. We reset
3651 * the state to SYN_RCVD so that future
3652 * state-change probes report correct state
3655 tcp
->tcp_state
= TCPS_SYN_RCVD
;
3657 /* notification did not go up, so drop ref */
3658 CONN_DEC_REF(connp
);
3659 /* ... and close the eager */
3660 ASSERT(TCP_IS_DETACHED(tcp
));
3661 (void) tcp_close_detached(tcp
);
3665 * tcp_newconn_notify() changes conn_upcalls and
3666 * connp->conn_upper_handle. Fix things now, in case
3667 * there's data attached to this ack.
3669 if (connp
->conn_upcalls
!= NULL
)
3670 sockupcalls
= connp
->conn_upcalls
;
3672 * For passive open, trace receipt of final ACK as
3673 * tcp:::accept-established.
3675 DTRACE_TCP5(accept__established
, mlbk_t
*, NULL
,
3676 ip_xmit_attr_t
*, connp
->conn_ixa
, void_ip_t
*,
3677 iphdr
, tcp_t
*, tcp
, tcph_t
*, tcpha
);
3680 * 3-way handshake complete - this is a STREAMS based
3681 * socket, so pass up the T_CONN_IND.
3683 tcp_t
*listener
= tcp
->tcp_listener
;
3684 mblk_t
*mp
= tcp
->tcp_conn
.tcp_eager_conn_ind
;
3686 tcp
->tcp_tconnind_started
= B_TRUE
;
3687 tcp
->tcp_conn
.tcp_eager_conn_ind
= NULL
;
3690 * We are here means eager is fine but it can
3691 * get a TH_RST at any point between now and till
3692 * accept completes and disappear. We need to
3693 * ensure that reference to eager is valid after
3694 * we get out of eager's perimeter. So we do
3697 CONN_INC_REF(connp
);
3700 * The listener also exists because of the refhold
3701 * done in tcp_input_listener. Its possible that it
3702 * might have closed. We will check that once we
3703 * get inside listeners context.
3705 CONN_INC_REF(listener
->tcp_connp
);
3706 if (listener
->tcp_connp
->conn_sqp
==
3709 * We optimize by not calling an SQUEUE_ENTER
3710 * on the listener since we know that the
3711 * listener and eager squeues are the same.
3712 * We are able to make this check safely only
3713 * because neither the eager nor the listener
3714 * can change its squeue. Only an active connect
3715 * can change its squeue
3717 tcp_send_conn_ind(listener
->tcp_connp
, mp
,
3718 listener
->tcp_connp
->conn_sqp
);
3719 CONN_DEC_REF(listener
->tcp_connp
);
3720 } else if (!tcp
->tcp_loopback
) {
3721 SQUEUE_ENTER_ONE(listener
->tcp_connp
->conn_sqp
,
3722 mp
, tcp_send_conn_ind
,
3723 listener
->tcp_connp
, NULL
, SQ_FILL
,
3724 SQTAG_TCP_CONN_IND
);
3726 SQUEUE_ENTER_ONE(listener
->tcp_connp
->conn_sqp
,
3727 mp
, tcp_send_conn_ind
,
3728 listener
->tcp_connp
, NULL
, SQ_NODRAIN
,
3729 SQTAG_TCP_CONN_IND
);
3732 * For passive open, trace receipt of final ACK as
3733 * tcp:::accept-established.
3735 DTRACE_TCP5(accept__established
, mlbk_t
*, NULL
,
3736 ip_xmit_attr_t
*, connp
->conn_ixa
, void_ip_t
*,
3737 iphdr
, tcp_t
*, tcp
, tcph_t
*, tcpha
);
3739 TCPS_CONN_INC(tcps
);
3741 tcp
->tcp_suna
= tcp
->tcp_iss
+ 1; /* One for the SYN */
3743 /* SYN was acked - making progress */
3744 tcp
->tcp_ip_forward_progress
= B_TRUE
;
3747 * If SYN was retransmitted, need to reset all
3748 * retransmission info as this segment will be
3749 * treated as a dup ACK.
3751 if (tcp
->tcp_rexmit
) {
3752 tcp
->tcp_rexmit
= B_FALSE
;
3753 tcp
->tcp_rexmit_nxt
= tcp
->tcp_snxt
;
3754 tcp
->tcp_rexmit_max
= tcp
->tcp_snxt
;
3755 tcp
->tcp_ms_we_have_waited
= 0;
3756 tcp
->tcp_cwnd
= mss
;
3760 * We set the send window to zero here.
3761 * This is needed if there is data to be
3762 * processed already on the queue.
3763 * Later (at swnd_update label), the
3764 * "new_swnd > tcp_swnd" condition is satisfied
3765 * the XMIT_NEEDED flag is set in the current
3766 * (SYN_RCVD) state. This ensures tcp_wput_data() is
3767 * called if there is already data on queue in
3772 if (new_swnd
> tcp
->tcp_max_swnd
)
3773 tcp
->tcp_max_swnd
= new_swnd
;
3774 tcp
->tcp_swl1
= seg_seq
;
3775 tcp
->tcp_swl2
= seg_ack
;
3776 tcp
->tcp_valid_bits
&= ~TCP_ISS_VALID
;
3778 /* Trace change from SYN_RCVD -> ESTABLISHED here */
3779 DTRACE_TCP6(state__change
, void, NULL
, ip_xmit_attr_t
*,
3780 connp
->conn_ixa
, void, NULL
, tcp_t
*, tcp
, void, NULL
,
3781 int32_t, TCPS_SYN_RCVD
);
3783 /* Fuse when both sides are in ESTABLISHED state */
3784 if (tcp
->tcp_loopback
&& do_tcp_fusion
)
3785 tcp_fuse(tcp
, iphdr
, tcpha
);
3788 /* This code follows 4.4BSD-Lite2 mostly. */
3789 if (bytes_acked
< 0)
3793 * If TCP is ECN capable and the congestion experience bit is
3794 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be
3795 * done once per window (or more loosely, per RTT).
3797 if (tcp
->tcp_cwr
&& SEQ_GT(seg_ack
, tcp
->tcp_cwr_snd_max
))
3798 tcp
->tcp_cwr
= B_FALSE
;
3799 if (tcp
->tcp_ecn_ok
&& (flags
& TH_ECE
)) {
3800 if (!tcp
->tcp_cwr
) {
3801 npkt
= ((tcp
->tcp_snxt
- tcp
->tcp_suna
) >> 1) / mss
;
3802 tcp
->tcp_cwnd_ssthresh
= MAX(npkt
, 2) * mss
;
3803 tcp
->tcp_cwnd
= npkt
* mss
;
3805 * If the cwnd is 0, use the timer to clock out
3806 * new segments. This is required by the ECN spec.
3809 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
3811 * This makes sure that when the ACK comes
3812 * back, we will increase tcp_cwnd by 1 MSS.
3814 tcp
->tcp_cwnd_cnt
= 0;
3816 tcp
->tcp_cwr
= B_TRUE
;
3818 * This marks the end of the current window of in
3819 * flight data. That is why we don't use
3820 * tcp_suna + tcp_swnd. Only data in flight can
3823 tcp
->tcp_cwr_snd_max
= tcp
->tcp_snxt
;
3824 tcp
->tcp_ecn_cwr_sent
= B_FALSE
;
3828 mp1
= tcp
->tcp_xmit_head
;
3829 if (bytes_acked
== 0) {
3830 if (!ofo_seg
&& seg_len
== 0 && new_swnd
== tcp
->tcp_swnd
) {
3833 TCPS_BUMP_MIB(tcps
, tcpInDupAck
);
3835 * Fast retransmit. When we have seen exactly three
3836 * identical ACKs while we have unacked data
3837 * outstanding we take it as a hint that our peer
3838 * dropped something.
3840 * If TCP is retransmitting, don't do fast retransmit.
3842 if (mp1
&& tcp
->tcp_suna
!= tcp
->tcp_snxt
&&
3843 ! tcp
->tcp_rexmit
) {
3844 /* Do Limited Transmit */
3845 if ((dupack_cnt
= ++tcp
->tcp_dupack_cnt
) <
3846 tcps
->tcps_dupack_fast_retransmit
) {
3850 * What we need to do is temporarily
3851 * increase tcp_cwnd so that new
3852 * data can be sent if it is allowed
3853 * by the receive window (tcp_rwnd).
3854 * tcp_wput_data() will take care of
3857 * If the connection is SACK capable,
3858 * only do limited xmit when there
3861 * Note how tcp_cwnd is incremented.
3862 * The first dup ACK will increase
3863 * it by 1 MSS. The second dup ACK
3864 * will increase it by 2 MSS. This
3865 * means that only 1 new segment will
3866 * be sent for each dup ACK.
3868 if (tcp
->tcp_unsent
> 0 &&
3869 (!tcp
->tcp_snd_sack_ok
||
3870 (tcp
->tcp_snd_sack_ok
&&
3871 tcp
->tcp_notsack_list
!= NULL
))) {
3872 tcp
->tcp_cwnd
+= mss
<<
3873 (tcp
->tcp_dupack_cnt
- 1);
3874 flags
|= TH_LIMIT_XMIT
;
3876 } else if (dupack_cnt
==
3877 tcps
->tcps_dupack_fast_retransmit
) {
3880 * If we have reduced tcp_ssthresh
3881 * because of ECN, do not reduce it again
3882 * unless it is already one window of data
3883 * away. After one window of data, tcp_cwr
3884 * should then be cleared. Note that
3885 * for non ECN capable connection, tcp_cwr
3886 * should always be false.
3888 * Adjust cwnd since the duplicate
3889 * ack indicates that a packet was
3890 * dropped (due to congestion.)
3892 if (!tcp
->tcp_cwr
) {
3893 npkt
= ((tcp
->tcp_snxt
-
3894 tcp
->tcp_suna
) >> 1) / mss
;
3895 tcp
->tcp_cwnd_ssthresh
= MAX(npkt
, 2) *
3897 tcp
->tcp_cwnd
= (npkt
+
3898 tcp
->tcp_dupack_cnt
) * mss
;
3900 if (tcp
->tcp_ecn_ok
) {
3901 tcp
->tcp_cwr
= B_TRUE
;
3902 tcp
->tcp_cwr_snd_max
= tcp
->tcp_snxt
;
3903 tcp
->tcp_ecn_cwr_sent
= B_FALSE
;
3907 * We do Hoe's algorithm. Refer to her
3908 * paper "Improving the Start-up Behavior
3909 * of a Congestion Control Scheme for TCP,"
3910 * appeared in SIGCOMM'96.
3912 * Save highest seq no we have sent so far.
3913 * Be careful about the invisible FIN byte.
3915 if ((tcp
->tcp_valid_bits
& TCP_FSS_VALID
) &&
3916 (tcp
->tcp_unsent
== 0)) {
3917 tcp
->tcp_rexmit_max
= tcp
->tcp_fss
;
3919 tcp
->tcp_rexmit_max
= tcp
->tcp_snxt
;
3924 * Calculate tcp_pipe, which is the
3925 * estimated number of bytes in
3928 * tcp_fack is the highest sack'ed seq num
3931 * tcp_pipe is explained in the above quoted
3932 * Fall and Floyd's paper. tcp_fack is
3933 * explained in Mathis and Mahdavi's
3934 * "Forward Acknowledgment: Refining TCP
3935 * Congestion Control" in SIGCOMM '96.
3937 if (tcp
->tcp_snd_sack_ok
) {
3938 if (tcp
->tcp_notsack_list
!= NULL
) {
3939 tcp
->tcp_pipe
= tcp
->tcp_snxt
-
3941 tcp
->tcp_sack_snxt
= seg_ack
;
3942 flags
|= TH_NEED_SACK_REXMIT
;
3945 * Always initialize tcp_pipe
3946 * even though we don't have
3947 * any SACK info. If later
3948 * we get SACK info and
3949 * tcp_pipe is not initialized,
3950 * funny things will happen.
3953 tcp
->tcp_cwnd_ssthresh
;
3956 flags
|= TH_REXMIT_NEEDED
;
3957 } /* tcp_snd_sack_ok */
3961 * Here we perform congestion
3962 * avoidance, but NOT slow start.
3963 * This is known as the Fast
3964 * Recovery Algorithm.
3966 if (tcp
->tcp_snd_sack_ok
&&
3967 tcp
->tcp_notsack_list
!= NULL
) {
3968 flags
|= TH_NEED_SACK_REXMIT
;
3969 tcp
->tcp_pipe
-= mss
;
3970 if (tcp
->tcp_pipe
< 0)
3974 * We know that one more packet has
3975 * left the pipe thus we can update
3978 cwnd
= tcp
->tcp_cwnd
+ mss
;
3979 if (cwnd
> tcp
->tcp_cwnd_max
)
3980 cwnd
= tcp
->tcp_cwnd_max
;
3981 tcp
->tcp_cwnd
= cwnd
;
3982 if (tcp
->tcp_unsent
> 0)
3983 flags
|= TH_XMIT_NEEDED
;
3987 } else if (tcp
->tcp_zero_win_probe
) {
3989 * If the window has opened, need to arrange
3990 * to send additional data.
3992 if (new_swnd
!= 0) {
3993 /* tcp_suna != tcp_snxt */
3994 /* Packet contains a window update */
3995 TCPS_BUMP_MIB(tcps
, tcpInWinUpdate
);
3996 tcp
->tcp_zero_win_probe
= 0;
3997 tcp
->tcp_timer_backoff
= 0;
3998 tcp
->tcp_ms_we_have_waited
= 0;
4001 * Transmit starting with tcp_suna since
4002 * the one byte probe is not ack'ed.
4003 * If TCP has sent more than one identical
4004 * probe, tcp_rexmit will be set. That means
4005 * tcp_ss_rexmit() will send out the one
4006 * byte along with new data. Otherwise,
4007 * fake the retransmission.
4009 flags
|= TH_XMIT_NEEDED
;
4010 if (!tcp
->tcp_rexmit
) {
4011 tcp
->tcp_rexmit
= B_TRUE
;
4012 tcp
->tcp_dupack_cnt
= 0;
4013 tcp
->tcp_rexmit_nxt
= tcp
->tcp_suna
;
4014 tcp
->tcp_rexmit_max
= tcp
->tcp_suna
+ 1;
4022 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73.
4023 * If the ACK value acks something that we have not yet sent, it might
4024 * be an old duplicate segment. Send an ACK to re-synchronize the
4026 * Note: reset in response to unacceptable ACK in SYN_RECEIVE
4027 * state is handled above, so we can always just drop the segment and
4030 * In the case where the peer shrinks the window, we see the new window
4031 * update, but all the data sent previously is queued up by the peer.
4032 * To account for this, in tcp_process_shrunk_swnd(), the sequence
4033 * number, which was already sent, and within window, is recorded.
4034 * tcp_snxt is then updated.
4036 * If the window has previously shrunk, and an ACK for data not yet
4037 * sent, according to tcp_snxt is recieved, it may still be valid. If
4038 * the ACK is for data within the window at the time the window was
4039 * shrunk, then the ACK is acceptable. In this case tcp_snxt is set to
4040 * the sequence number ACK'ed.
4042 * If the ACK covers all the data sent at the time the window was
4043 * shrunk, we can now set tcp_is_wnd_shrnk to B_FALSE.
4045 * Should we send ACKs in response to ACK only segments?
4048 if (SEQ_GT(seg_ack
, tcp
->tcp_snxt
)) {
4049 if ((tcp
->tcp_is_wnd_shrnk
) &&
4050 (SEQ_LEQ(seg_ack
, tcp
->tcp_snxt_shrunk
))) {
4051 uint32_t data_acked_ahead_snxt
;
4053 data_acked_ahead_snxt
= seg_ack
- tcp
->tcp_snxt
;
4054 tcp_update_xmit_tail(tcp
, seg_ack
);
4055 tcp
->tcp_unsent
-= data_acked_ahead_snxt
;
4057 TCPS_BUMP_MIB(tcps
, tcpInAckUnsent
);
4058 /* drop the received segment */
4062 * Send back an ACK. If tcp_drop_ack_unsent_cnt is
4063 * greater than 0, check if the number of such
4064 * bogus ACks is greater than that count. If yes,
4065 * don't send back any ACK. This prevents TCP from
4066 * getting into an ACK storm if somehow an attacker
4067 * successfully spoofs an acceptable segment to our
4068 * peer. If this continues (count > 2 X threshold),
4069 * we should abort this connection.
4071 if (tcp_drop_ack_unsent_cnt
> 0 &&
4072 ++tcp
->tcp_in_ack_unsent
>
4073 tcp_drop_ack_unsent_cnt
) {
4074 TCP_STAT(tcps
, tcp_in_ack_unsent_drop
);
4075 if (tcp
->tcp_in_ack_unsent
> 2 *
4076 tcp_drop_ack_unsent_cnt
) {
4077 (void) tcp_clean_death(tcp
, EPROTO
);
4081 mp
= tcp_ack_mp(tcp
);
4083 BUMP_LOCAL(tcp
->tcp_obsegs
);
4084 TCPS_BUMP_MIB(tcps
, tcpOutAck
);
4085 tcp_send_data(tcp
, mp
);
4089 } else if (tcp
->tcp_is_wnd_shrnk
&& SEQ_GEQ(seg_ack
,
4090 tcp
->tcp_snxt_shrunk
)) {
4091 tcp
->tcp_is_wnd_shrnk
= B_FALSE
;
4095 * TCP gets a new ACK, update the notsack'ed list to delete those
4096 * blocks that are covered by this ACK.
4098 if (tcp
->tcp_snd_sack_ok
&& tcp
->tcp_notsack_list
!= NULL
) {
4099 tcp_notsack_remove(&(tcp
->tcp_notsack_list
), seg_ack
,
4100 &(tcp
->tcp_num_notsack_blk
), &(tcp
->tcp_cnt_notsack_list
));
4104 * If we got an ACK after fast retransmit, check to see
4105 * if it is a partial ACK. If it is not and the congestion
4106 * window was inflated to account for the other side's
4107 * cached packets, retract it. If it is, do Hoe's algorithm.
4109 if (tcp
->tcp_dupack_cnt
>= tcps
->tcps_dupack_fast_retransmit
) {
4110 ASSERT(tcp
->tcp_rexmit
== B_FALSE
);
4111 if (SEQ_GEQ(seg_ack
, tcp
->tcp_rexmit_max
)) {
4112 tcp
->tcp_dupack_cnt
= 0;
4114 * Restore the orig tcp_cwnd_ssthresh after
4115 * fast retransmit phase.
4117 if (tcp
->tcp_cwnd
> tcp
->tcp_cwnd_ssthresh
) {
4118 tcp
->tcp_cwnd
= tcp
->tcp_cwnd_ssthresh
;
4120 tcp
->tcp_rexmit_max
= seg_ack
;
4121 tcp
->tcp_cwnd_cnt
= 0;
4124 * Remove all notsack info to avoid confusion with
4125 * the next fast retrasnmit/recovery phase.
4127 if (tcp
->tcp_snd_sack_ok
) {
4128 TCP_NOTSACK_REMOVE_ALL(tcp
->tcp_notsack_list
,
4132 if (tcp
->tcp_snd_sack_ok
&&
4133 tcp
->tcp_notsack_list
!= NULL
) {
4134 flags
|= TH_NEED_SACK_REXMIT
;
4135 tcp
->tcp_pipe
-= mss
;
4136 if (tcp
->tcp_pipe
< 0)
4142 * Retransmit the unack'ed segment and
4143 * restart fast recovery. Note that we
4144 * need to scale back tcp_cwnd to the
4145 * original value when we started fast
4146 * recovery. This is to prevent overly
4147 * aggressive behaviour in sending new
4150 tcp
->tcp_cwnd
= tcp
->tcp_cwnd_ssthresh
+
4151 tcps
->tcps_dupack_fast_retransmit
* mss
;
4152 tcp
->tcp_cwnd_cnt
= tcp
->tcp_cwnd
;
4153 flags
|= TH_REXMIT_NEEDED
;
4157 tcp
->tcp_dupack_cnt
= 0;
4158 if (tcp
->tcp_rexmit
) {
4160 * TCP is retranmitting. If the ACK ack's all
4161 * outstanding data, update tcp_rexmit_max and
4162 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt
4163 * to the correct value.
4165 * Note that SEQ_LEQ() is used. This is to avoid
4166 * unnecessary fast retransmit caused by dup ACKs
4167 * received when TCP does slow start retransmission
4168 * after a time out. During this phase, TCP may
4169 * send out segments which are already received.
4170 * This causes dup ACKs to be sent back.
4172 if (SEQ_LEQ(seg_ack
, tcp
->tcp_rexmit_max
)) {
4173 if (SEQ_GT(seg_ack
, tcp
->tcp_rexmit_nxt
)) {
4174 tcp
->tcp_rexmit_nxt
= seg_ack
;
4176 if (seg_ack
!= tcp
->tcp_rexmit_max
) {
4177 flags
|= TH_XMIT_NEEDED
;
4180 tcp
->tcp_rexmit
= B_FALSE
;
4181 tcp
->tcp_rexmit_nxt
= tcp
->tcp_snxt
;
4183 tcp
->tcp_ms_we_have_waited
= 0;
4187 TCPS_BUMP_MIB(tcps
, tcpInAckSegs
);
4188 TCPS_UPDATE_MIB(tcps
, tcpInAckBytes
, bytes_acked
);
4189 tcp
->tcp_suna
= seg_ack
;
4190 if (tcp
->tcp_zero_win_probe
!= 0) {
4191 tcp
->tcp_zero_win_probe
= 0;
4192 tcp
->tcp_timer_backoff
= 0;
4196 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed.
4197 * Note that it cannot be the SYN being ack'ed. The code flow
4198 * will not reach here.
4205 * Update the congestion window.
4207 * If TCP is not ECN capable or TCP is ECN capable but the
4208 * congestion experience bit is not set, increase the tcp_cwnd as
4211 if (!tcp
->tcp_ecn_ok
|| !(flags
& TH_ECE
)) {
4212 cwnd
= tcp
->tcp_cwnd
;
4215 if (cwnd
>= tcp
->tcp_cwnd_ssthresh
) {
4217 * This is to prevent an increase of less than 1 MSS of
4218 * tcp_cwnd. With partial increase, tcp_wput_data()
4219 * may send out tinygrams in order to preserve mblk
4222 * By initializing tcp_cwnd_cnt to new tcp_cwnd and
4223 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is
4224 * increased by 1 MSS for every RTTs.
4226 if (tcp
->tcp_cwnd_cnt
<= 0) {
4227 tcp
->tcp_cwnd_cnt
= cwnd
+ add
;
4229 tcp
->tcp_cwnd_cnt
-= add
;
4233 tcp
->tcp_cwnd
= MIN(cwnd
+ add
, tcp
->tcp_cwnd_max
);
4236 /* See if the latest urgent data has been acknowledged */
4237 if ((tcp
->tcp_valid_bits
& TCP_URG_VALID
) &&
4238 SEQ_GT(seg_ack
, tcp
->tcp_urg
))
4239 tcp
->tcp_valid_bits
&= ~TCP_URG_VALID
;
4241 /* Can we update the RTT estimates? */
4242 if (tcp
->tcp_snd_ts_ok
) {
4243 /* Ignore zero timestamp echo-reply. */
4244 if (tcpopt
.tcp_opt_ts_ecr
!= 0) {
4245 tcp_set_rto(tcp
, (int32_t)LBOLT_FASTPATH
-
4246 (int32_t)tcpopt
.tcp_opt_ts_ecr
);
4249 /* If needed, restart the timer. */
4250 if (tcp
->tcp_set_timer
== 1) {
4251 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
4252 tcp
->tcp_set_timer
= 0;
4255 * Update tcp_csuna in case the other side stops sending
4258 tcp
->tcp_csuna
= tcp
->tcp_snxt
;
4259 } else if (SEQ_GT(seg_ack
, tcp
->tcp_csuna
)) {
4261 * An ACK sequence we haven't seen before, so get the RTT
4262 * and update the RTO. But first check if the timestamp is
4265 if ((mp1
->b_next
!= NULL
) &&
4266 SEQ_GT(seg_ack
, (uint32_t)(uintptr_t)(mp1
->b_next
)))
4267 tcp_set_rto(tcp
, (int32_t)LBOLT_FASTPATH
-
4268 (int32_t)(intptr_t)mp1
->b_prev
);
4270 TCPS_BUMP_MIB(tcps
, tcpRttNoUpdate
);
4272 /* Remeber the last sequence to be ACKed */
4273 tcp
->tcp_csuna
= seg_ack
;
4274 if (tcp
->tcp_set_timer
== 1) {
4275 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
4276 tcp
->tcp_set_timer
= 0;
4279 TCPS_BUMP_MIB(tcps
, tcpRttNoUpdate
);
4282 /* Eat acknowledged bytes off the xmit queue. */
4288 ASSERT((uintptr_t)(wptr
- mp1
->b_rptr
) <= (uintptr_t)INT_MAX
);
4289 bytes_acked
-= (int)(wptr
- mp1
->b_rptr
);
4290 if (bytes_acked
< 0) {
4291 mp1
->b_rptr
= wptr
+ bytes_acked
;
4293 * Set a new timestamp if all the bytes timed by the
4294 * old timestamp have been ack'ed.
4297 (uint32_t)(uintptr_t)(mp1
->b_next
))) {
4299 (mblk_t
*)(uintptr_t)LBOLT_FASTPATH
;
4310 * This notification is required for some zero-copy
4311 * clients to maintain a copy semantic. After the data
4312 * is ack'ed, client is safe to modify or reuse the buffer.
4314 if (tcp
->tcp_snd_zcopy_aware
&&
4315 (mp2
->b_datap
->db_struioflag
& STRUIO_ZCNOTIFY
))
4316 tcp_zcopy_notify(tcp
);
4318 if (bytes_acked
== 0) {
4320 /* Everything is ack'ed, clear the tail. */
4321 tcp
->tcp_xmit_tail
= NULL
;
4323 * Cancel the timer unless we are still
4324 * waiting for an ACK for the FIN packet.
4326 if (tcp
->tcp_timer_tid
!= 0 &&
4327 tcp
->tcp_snxt
== tcp
->tcp_suna
) {
4328 (void) TCP_TIMER_CANCEL(tcp
,
4329 tcp
->tcp_timer_tid
);
4330 tcp
->tcp_timer_tid
= 0;
4332 goto pre_swnd_update
;
4334 if (mp2
!= tcp
->tcp_xmit_tail
)
4336 tcp
->tcp_xmit_tail
= mp1
;
4337 ASSERT((uintptr_t)(mp1
->b_wptr
- mp1
->b_rptr
) <=
4338 (uintptr_t)INT_MAX
);
4339 tcp
->tcp_xmit_tail_unsent
= (int)(mp1
->b_wptr
-
4345 * More was acked but there is nothing more
4346 * outstanding. This means that the FIN was
4347 * just acked or that we're talking to a clown.
4350 ASSERT(tcp
->tcp_fin_sent
);
4351 tcp
->tcp_xmit_tail
= NULL
;
4352 if (tcp
->tcp_fin_sent
) {
4353 /* FIN was acked - making progress */
4354 if (!tcp
->tcp_fin_acked
)
4355 tcp
->tcp_ip_forward_progress
= B_TRUE
;
4356 tcp
->tcp_fin_acked
= B_TRUE
;
4357 if (tcp
->tcp_linger_tid
!= 0 &&
4358 TCP_TIMER_CANCEL(tcp
,
4359 tcp
->tcp_linger_tid
) >= 0) {
4360 tcp_stop_lingering(tcp
);
4366 * We should never get here because
4367 * we have already checked that the
4368 * number of bytes ack'ed should be
4369 * smaller than or equal to what we
4370 * have sent so far (it is the
4371 * acceptability check of the ACK).
4372 * We can only get here if the send
4373 * queue is corrupted.
4375 * Terminate the connection and
4376 * panic the system. It is better
4377 * for us to panic instead of
4378 * continuing to avoid other disaster.
4380 tcp_xmit_ctl(NULL
, tcp
, tcp
->tcp_snxt
,
4381 tcp
->tcp_rnxt
, TH_RST
|TH_ACK
);
4382 panic("Memory corruption "
4383 "detected for connection %s.",
4384 tcp_display(tcp
, NULL
,
4385 DISP_ADDR_AND_PORT
));
4388 goto pre_swnd_update
;
4390 ASSERT(mp2
!= tcp
->tcp_xmit_tail
);
4392 if (tcp
->tcp_unsent
) {
4393 flags
|= TH_XMIT_NEEDED
;
4396 tcp
->tcp_xmit_head
= mp1
;
4399 * The following check is different from most other implementations.
4400 * For bi-directional transfer, when segments are dropped, the
4401 * "normal" check will not accept a window update in those
4402 * retransmitted segemnts. Failing to do that, TCP may send out
4403 * segments which are outside receiver's window. As TCP accepts
4404 * the ack in those retransmitted segments, if the window update in
4405 * the same segment is not accepted, TCP will incorrectly calculates
4406 * that it can send more segments. This can create a deadlock
4407 * with the receiver if its window becomes zero.
4409 if (SEQ_LT(tcp
->tcp_swl2
, seg_ack
) ||
4410 SEQ_LT(tcp
->tcp_swl1
, seg_seq
) ||
4411 (tcp
->tcp_swl1
== seg_seq
&& new_swnd
> tcp
->tcp_swnd
)) {
4413 * The criteria for update is:
4415 * 1. the segment acknowledges some data. Or
4416 * 2. the segment is new, i.e. it has a higher seq num. Or
4417 * 3. the segment is not old and the advertised window is
4418 * larger than the previous advertised window.
4420 if (tcp
->tcp_unsent
&& new_swnd
> tcp
->tcp_swnd
)
4421 flags
|= TH_XMIT_NEEDED
;
4422 tcp
->tcp_swnd
= new_swnd
;
4423 if (new_swnd
> tcp
->tcp_max_swnd
)
4424 tcp
->tcp_max_swnd
= new_swnd
;
4425 tcp
->tcp_swl1
= seg_seq
;
4426 tcp
->tcp_swl2
= seg_ack
;
4429 if (tcp
->tcp_state
> TCPS_ESTABLISHED
) {
4431 switch (tcp
->tcp_state
) {
4432 case TCPS_FIN_WAIT_1
:
4433 if (tcp
->tcp_fin_acked
) {
4434 tcp
->tcp_state
= TCPS_FIN_WAIT_2
;
4435 DTRACE_TCP6(state__change
, void, NULL
,
4436 ip_xmit_attr_t
*, connp
->conn_ixa
,
4437 void, NULL
, tcp_t
*, tcp
, void, NULL
,
4438 int32_t, TCPS_FIN_WAIT_1
);
4440 * We implement the non-standard BSD/SunOS
4441 * FIN_WAIT_2 flushing algorithm.
4442 * If there is no user attached to this
4443 * TCP endpoint, then this TCP struct
4444 * could hang around forever in FIN_WAIT_2
4445 * state if the peer forgets to send us
4446 * a FIN. To prevent this, we wait only
4447 * 2*MSL (a convenient time value) for
4448 * the FIN to arrive. If it doesn't show up,
4449 * we flush the TCP endpoint. This algorithm,
4450 * though a violation of RFC-793, has worked
4451 * for over 10 years in BSD systems.
4452 * Note: SunOS 4.x waits 675 seconds before
4453 * flushing the FIN_WAIT_2 connection.
4455 TCP_TIMER_RESTART(tcp
,
4456 tcp
->tcp_fin_wait_2_flush_interval
);
4459 case TCPS_FIN_WAIT_2
:
4460 break; /* Shutdown hook? */
4463 if (tcp
->tcp_fin_acked
) {
4464 (void) tcp_clean_death(tcp
, 0);
4469 if (tcp
->tcp_fin_acked
) {
4470 SET_TIME_WAIT(tcps
, tcp
, connp
);
4471 DTRACE_TCP6(state__change
, void, NULL
,
4472 ip_xmit_attr_t
*, connp
->conn_ixa
, void,
4473 NULL
, tcp_t
*, tcp
, void, NULL
, int32_t,
4477 case TCPS_CLOSE_WAIT
:
4481 ASSERT(tcp
->tcp_state
!= TCPS_TIME_WAIT
);
4485 if (flags
& TH_FIN
) {
4486 /* Make sure we ack the fin */
4487 flags
|= TH_ACK_NEEDED
;
4488 if (!tcp
->tcp_fin_rcvd
) {
4489 tcp
->tcp_fin_rcvd
= B_TRUE
;
4491 tcpha
= tcp
->tcp_tcpha
;
4492 tcpha
->tha_ack
= htonl(tcp
->tcp_rnxt
);
4495 * Generate the ordrel_ind at the end unless the
4496 * conn is detached or it is a STREAMS based eager.
4497 * In the eager case we defer the notification until
4498 * tcp_accept_finish has run.
4500 if (!TCP_IS_DETACHED(tcp
) && (IPCL_IS_NONSTR(connp
) ||
4501 (tcp
->tcp_listener
== NULL
&&
4502 !tcp
->tcp_hard_binding
)))
4503 flags
|= TH_ORDREL_NEEDED
;
4504 switch (tcp
->tcp_state
) {
4506 tcp
->tcp_state
= TCPS_CLOSE_WAIT
;
4507 DTRACE_TCP6(state__change
, void, NULL
,
4508 ip_xmit_attr_t
*, connp
->conn_ixa
,
4509 void, NULL
, tcp_t
*, tcp
, void, NULL
,
4510 int32_t, TCPS_SYN_RCVD
);
4513 case TCPS_ESTABLISHED
:
4514 tcp
->tcp_state
= TCPS_CLOSE_WAIT
;
4515 DTRACE_TCP6(state__change
, void, NULL
,
4516 ip_xmit_attr_t
*, connp
->conn_ixa
,
4517 void, NULL
, tcp_t
*, tcp
, void, NULL
,
4518 int32_t, TCPS_ESTABLISHED
);
4521 case TCPS_FIN_WAIT_1
:
4522 if (!tcp
->tcp_fin_acked
) {
4523 tcp
->tcp_state
= TCPS_CLOSING
;
4524 DTRACE_TCP6(state__change
, void, NULL
,
4525 ip_xmit_attr_t
*, connp
->conn_ixa
,
4526 void, NULL
, tcp_t
*, tcp
, void,
4527 NULL
, int32_t, TCPS_FIN_WAIT_1
);
4531 case TCPS_FIN_WAIT_2
:
4532 SET_TIME_WAIT(tcps
, tcp
, connp
);
4533 DTRACE_TCP6(state__change
, void, NULL
,
4534 ip_xmit_attr_t
*, connp
->conn_ixa
, void,
4535 NULL
, tcp_t
*, tcp
, void, NULL
, int32_t,
4539 * implies data piggybacked on FIN.
4540 * break to handle data.
4555 if (mp
->b_rptr
== mp
->b_wptr
) {
4557 * The header has been consumed, so we remove the
4558 * zero-length mblk here.
4565 tcpha
= tcp
->tcp_tcpha
;
4566 tcp
->tcp_rack_cnt
++;
4570 cur_max
= tcp
->tcp_rack_cur_max
;
4571 if (tcp
->tcp_rack_cnt
>= cur_max
) {
4573 * We have more unacked data than we should - send
4576 flags
|= TH_ACK_NEEDED
;
4578 if (cur_max
> tcp
->tcp_rack_abs_max
)
4579 tcp
->tcp_rack_cur_max
= tcp
->tcp_rack_abs_max
;
4581 tcp
->tcp_rack_cur_max
= cur_max
;
4582 } else if (TCP_IS_DETACHED(tcp
)) {
4583 /* We don't have an ACK timer for detached TCP. */
4584 flags
|= TH_ACK_NEEDED
;
4585 } else if (seg_len
< mss
) {
4587 * If we get a segment that is less than an mss, and we
4588 * already have unacknowledged data, and the amount
4589 * unacknowledged is not a multiple of mss, then we
4590 * better generate an ACK now. Otherwise, this may be
4591 * the tail piece of a transaction, and we would rather
4592 * wait for the response.
4595 ASSERT((uintptr_t)(tcp
->tcp_rnxt
- tcp
->tcp_rack
) <=
4596 (uintptr_t)INT_MAX
);
4597 udif
= (int)(tcp
->tcp_rnxt
- tcp
->tcp_rack
);
4598 if (udif
&& (udif
% mss
))
4599 flags
|= TH_ACK_NEEDED
;
4601 flags
|= TH_ACK_TIMER_NEEDED
;
4603 /* Start delayed ack timer */
4604 flags
|= TH_ACK_TIMER_NEEDED
;
4607 tcp
->tcp_rnxt
+= seg_len
;
4608 tcpha
->tha_ack
= htonl(tcp
->tcp_rnxt
);
4613 /* Update SACK list */
4614 if (tcp
->tcp_snd_sack_ok
&& tcp
->tcp_num_sack_blk
> 0) {
4615 tcp_sack_remove(tcp
->tcp_sack_list
, tcp
->tcp_rnxt
,
4616 &(tcp
->tcp_num_sack_blk
));
4619 if (tcp
->tcp_urp_mp
) {
4620 tcp
->tcp_urp_mp
->b_cont
= mp
;
4621 mp
= tcp
->tcp_urp_mp
;
4622 tcp
->tcp_urp_mp
= NULL
;
4623 /* Ready for a new signal. */
4624 tcp
->tcp_urp_last_valid
= B_FALSE
;
4626 (void) strlog(TCP_MOD_ID
, 0, 1, SL_TRACE
,
4627 "tcp_rput: sending exdata_ind %s",
4628 tcp_display(tcp
, NULL
, DISP_PORT_ONLY
));
4633 * Check for ancillary data changes compared to last segment.
4635 if (connp
->conn_recv_ancillary
.crb_all
!= 0) {
4636 mp
= tcp_input_add_ancillary(tcp
, mp
, &ipp
, ira
);
4641 if (IPCL_IS_NONSTR(connp
)) {
4643 * Non-STREAMS socket
4645 boolean_t push
= flags
& (TH_PUSH
|TH_FIN
);
4648 if ((*sockupcalls
->su_recv
)(connp
->conn_upper_handle
,
4649 mp
, seg_len
, 0, &error
, &push
) <= 0) {
4651 * We should never be in middle of a
4652 * fallback, the squeue guarantees that.
4654 ASSERT(error
!= EOPNOTSUPP
);
4655 if (error
== ENOSPC
)
4656 tcp
->tcp_rwnd
-= seg_len
;
4658 /* PUSH bit set and sockfs is not flow controlled */
4659 flags
|= tcp_rwnd_reopen(tcp
);
4661 } else if (tcp
->tcp_listener
!= NULL
|| tcp
->tcp_hard_binding
) {
4663 * Side queue inbound data until the accept happens.
4664 * tcp_accept/tcp_rput drains this when the accept happens.
4665 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or
4666 * T_EXDATA_IND) it is queued on b_next.
4667 * XXX Make urgent data use this. Requires:
4668 * Removing tcp_listener check for TH_URG
4669 * Making M_PCPROTO and MARK messages skip the eager case
4672 tcp_rcv_enqueue(tcp
, mp
, seg_len
, ira
->ira_cred
);
4674 /* Active STREAMS socket */
4675 if (mp
->b_datap
->db_type
!= M_DATA
||
4676 (flags
& TH_MARKNEXT_NEEDED
)) {
4677 if (tcp
->tcp_rcv_list
!= NULL
) {
4678 flags
|= tcp_rcv_drain(tcp
);
4680 ASSERT(tcp
->tcp_rcv_list
== NULL
||
4681 tcp
->tcp_fused_sigurg
);
4683 if (flags
& TH_MARKNEXT_NEEDED
) {
4685 (void) strlog(TCP_MOD_ID
, 0, 1, SL_TRACE
,
4686 "tcp_rput: sending MSGMARKNEXT %s",
4687 tcp_display(tcp
, NULL
,
4690 mp
->b_flag
|= MSGMARKNEXT
;
4691 flags
&= ~TH_MARKNEXT_NEEDED
;
4694 putnext(connp
->conn_rq
, mp
);
4695 if (!canputnext(connp
->conn_rq
))
4696 tcp
->tcp_rwnd
-= seg_len
;
4697 } else if ((flags
& (TH_PUSH
|TH_FIN
)) ||
4698 tcp
->tcp_rcv_cnt
+ seg_len
>= connp
->conn_rcvbuf
>> 3) {
4699 if (tcp
->tcp_rcv_list
!= NULL
) {
4701 * Enqueue the new segment first and then
4702 * call tcp_rcv_drain() to send all data
4703 * up. The other way to do this is to
4704 * send all queued data up and then call
4705 * putnext() to send the new segment up.
4706 * This way can remove the else part later
4709 * We don't do this to avoid one more call to
4710 * canputnext() as tcp_rcv_drain() needs to
4711 * call canputnext().
4713 tcp_rcv_enqueue(tcp
, mp
, seg_len
,
4715 flags
|= tcp_rcv_drain(tcp
);
4717 putnext(connp
->conn_rq
, mp
);
4718 if (!canputnext(connp
->conn_rq
))
4719 tcp
->tcp_rwnd
-= seg_len
;
4723 * Enqueue all packets when processing an mblk
4724 * from the co queue and also enqueue normal packets.
4726 tcp_rcv_enqueue(tcp
, mp
, seg_len
, ira
->ira_cred
);
4729 * Make sure the timer is running if we have data waiting
4730 * for a push bit. This provides resiliency against
4731 * implementations that do not correctly generate push bits.
4733 if (tcp
->tcp_rcv_list
!= NULL
&& tcp
->tcp_push_tid
== 0) {
4735 * The connection may be closed at this point, so don't
4736 * do anything for a detached tcp.
4738 if (!TCP_IS_DETACHED(tcp
))
4739 tcp
->tcp_push_tid
= TCP_TIMER(tcp
,
4741 tcps
->tcps_push_timer_interval
);
4746 /* Is there anything left to do? */
4747 ASSERT(!(flags
& TH_MARKNEXT_NEEDED
));
4748 if ((flags
& (TH_REXMIT_NEEDED
|TH_XMIT_NEEDED
|TH_ACK_NEEDED
|
4749 TH_NEED_SACK_REXMIT
|TH_LIMIT_XMIT
|TH_ACK_TIMER_NEEDED
|
4750 TH_ORDREL_NEEDED
|TH_SEND_URP_MARK
)) == 0)
4753 /* Any transmit work to do and a non-zero window? */
4754 if ((flags
& (TH_REXMIT_NEEDED
|TH_XMIT_NEEDED
|TH_NEED_SACK_REXMIT
|
4755 TH_LIMIT_XMIT
)) && tcp
->tcp_swnd
!= 0) {
4756 if (flags
& TH_REXMIT_NEEDED
) {
4757 uint32_t snd_size
= tcp
->tcp_snxt
- tcp
->tcp_suna
;
4759 TCPS_BUMP_MIB(tcps
, tcpOutFastRetrans
);
4762 if (snd_size
> tcp
->tcp_swnd
)
4763 snd_size
= tcp
->tcp_swnd
;
4764 mp1
= tcp_xmit_mp(tcp
, tcp
->tcp_xmit_head
, snd_size
,
4765 NULL
, NULL
, tcp
->tcp_suna
, B_TRUE
, &snd_size
,
4769 tcp
->tcp_xmit_head
->b_prev
=
4770 (mblk_t
*)LBOLT_FASTPATH
;
4771 tcp
->tcp_csuna
= tcp
->tcp_snxt
;
4772 TCPS_BUMP_MIB(tcps
, tcpRetransSegs
);
4773 TCPS_UPDATE_MIB(tcps
, tcpRetransBytes
,
4775 tcp_send_data(tcp
, mp1
);
4778 if (flags
& TH_NEED_SACK_REXMIT
) {
4779 tcp_sack_rexmit(tcp
, &flags
);
4782 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send
4783 * out new segment. Note that tcp_rexmit should not be
4784 * set, otherwise TH_LIMIT_XMIT should not be set.
4786 if (flags
& (TH_XMIT_NEEDED
|TH_LIMIT_XMIT
)) {
4787 if (!tcp
->tcp_rexmit
) {
4788 tcp_wput_data(tcp
, NULL
, B_FALSE
);
4794 * Adjust tcp_cwnd back to normal value after sending
4795 * new data segments.
4797 if (flags
& TH_LIMIT_XMIT
) {
4798 tcp
->tcp_cwnd
-= mss
<< (tcp
->tcp_dupack_cnt
- 1);
4800 * This will restart the timer. Restarting the
4801 * timer is used to avoid a timeout before the
4802 * limited transmitted segment's ACK gets back.
4804 if (tcp
->tcp_xmit_head
!= NULL
)
4805 tcp
->tcp_xmit_head
->b_prev
=
4806 (mblk_t
*)LBOLT_FASTPATH
;
4809 /* Anything more to do? */
4810 if ((flags
& (TH_ACK_NEEDED
|TH_ACK_TIMER_NEEDED
|
4811 TH_ORDREL_NEEDED
|TH_SEND_URP_MARK
)) == 0)
4815 if (flags
& TH_SEND_URP_MARK
) {
4816 ASSERT(tcp
->tcp_urp_mark_mp
);
4817 ASSERT(!IPCL_IS_NONSTR(connp
));
4819 * Send up any queued data and then send the mark message
4821 if (tcp
->tcp_rcv_list
!= NULL
) {
4822 flags
|= tcp_rcv_drain(tcp
);
4825 ASSERT(tcp
->tcp_rcv_list
== NULL
|| tcp
->tcp_fused_sigurg
);
4826 mp1
= tcp
->tcp_urp_mark_mp
;
4827 tcp
->tcp_urp_mark_mp
= NULL
;
4829 putnext(connp
->conn_rq
, mp1
);
4831 (void) strlog(TCP_MOD_ID
, 0, 1, SL_TRACE
,
4832 "tcp_rput: sending zero-length %s %s",
4833 ((mp1
->b_flag
& MSGMARKNEXT
) ? "MSGMARKNEXT" :
4835 tcp_display(tcp
, NULL
, DISP_PORT_ONLY
));
4837 flags
&= ~TH_SEND_URP_MARK
;
4839 if (flags
& TH_ACK_NEEDED
) {
4841 * Time to send an ack for some reason.
4843 mp1
= tcp_ack_mp(tcp
);
4846 tcp_send_data(tcp
, mp1
);
4847 BUMP_LOCAL(tcp
->tcp_obsegs
);
4848 TCPS_BUMP_MIB(tcps
, tcpOutAck
);
4850 if (tcp
->tcp_ack_tid
!= 0) {
4851 (void) TCP_TIMER_CANCEL(tcp
, tcp
->tcp_ack_tid
);
4852 tcp
->tcp_ack_tid
= 0;
4855 if (flags
& TH_ACK_TIMER_NEEDED
) {
4857 * Arrange for deferred ACK or push wait timeout.
4858 * Start timer if it is not already running.
4860 if (tcp
->tcp_ack_tid
== 0) {
4861 tcp
->tcp_ack_tid
= TCP_TIMER(tcp
, tcp_ack_timer
,
4863 tcps
->tcps_local_dack_interval
:
4864 tcps
->tcps_deferred_ack_interval
);
4867 if (flags
& TH_ORDREL_NEEDED
) {
4869 * Notify upper layer about an orderly release. If this is
4870 * a non-STREAMS socket, then just make an upcall. For STREAMS
4871 * we send up an ordrel_ind, unless this is an eager, in which
4872 * case the ordrel will be sent when tcp_accept_finish runs.
4873 * Note that for non-STREAMS we make an upcall even if it is an
4874 * eager, because we have an upper handle to send it to.
4876 ASSERT(IPCL_IS_NONSTR(connp
) || tcp
->tcp_listener
== NULL
);
4877 ASSERT(!tcp
->tcp_detached
);
4879 if (IPCL_IS_NONSTR(connp
)) {
4880 ASSERT(tcp
->tcp_ordrel_mp
== NULL
);
4881 tcp
->tcp_ordrel_done
= B_TRUE
;
4882 (*sockupcalls
->su_opctl
)(connp
->conn_upper_handle
,
4883 SOCK_OPCTL_SHUT_RECV
, 0);
4887 if (tcp
->tcp_rcv_list
!= NULL
) {
4889 * Push any mblk(s) enqueued from co processing.
4891 flags
|= tcp_rcv_drain(tcp
);
4893 ASSERT(tcp
->tcp_rcv_list
== NULL
|| tcp
->tcp_fused_sigurg
);
4895 mp1
= tcp
->tcp_ordrel_mp
;
4896 tcp
->tcp_ordrel_mp
= NULL
;
4897 tcp
->tcp_ordrel_done
= B_TRUE
;
4898 putnext(connp
->conn_rq
, mp1
);
4901 ASSERT(!(flags
& TH_MARKNEXT_NEEDED
));
4905 * Attach ancillary data to a received TCP segments for the
4906 * ancillary pieces requested by the application that are
4907 * different than they were in the previous data segment.
4909 * Save the "current" values once memory allocation is ok so that
4910 * when memory allocation fails we can just wait for the next data segment.
4913 tcp_input_add_ancillary(tcp_t
*tcp
, mblk_t
*mp
, ip_pkt_t
*ipp
,
4914 ip_recv_attr_t
*ira
)
4916 struct T_optdata_ind
*todi
;
4919 struct T_opthdr
*toh
;
4920 crb_t addflag
; /* Which pieces to add */
4922 conn_t
*connp
= tcp
->tcp_connp
;
4925 addflag
.crb_all
= 0;
4926 /* If app asked for pktinfo and the index has changed ... */
4927 if (connp
->conn_recv_ancillary
.crb_ip_recvpktinfo
&&
4928 ira
->ira_ruifindex
!= tcp
->tcp_recvifindex
) {
4929 optlen
+= sizeof (struct T_opthdr
) +
4930 sizeof (struct in6_pktinfo
);
4931 addflag
.crb_ip_recvpktinfo
= 1;
4933 /* If app asked for hoplimit and it has changed ... */
4934 if (connp
->conn_recv_ancillary
.crb_ipv6_recvhoplimit
&&
4935 ipp
->ipp_hoplimit
!= tcp
->tcp_recvhops
) {
4936 optlen
+= sizeof (struct T_opthdr
) + sizeof (uint_t
);
4937 addflag
.crb_ipv6_recvhoplimit
= 1;
4939 /* If app asked for tclass and it has changed ... */
4940 if (connp
->conn_recv_ancillary
.crb_ipv6_recvtclass
&&
4941 ipp
->ipp_tclass
!= tcp
->tcp_recvtclass
) {
4942 optlen
+= sizeof (struct T_opthdr
) + sizeof (uint_t
);
4943 addflag
.crb_ipv6_recvtclass
= 1;
4946 * If app asked for hopbyhop headers and it has changed ...
4948 if (connp
->conn_recv_ancillary
.crb_ipv6_recvhopopts
&&
4949 ip_cmpbuf(tcp
->tcp_hopopts
, tcp
->tcp_hopoptslen
,
4950 (ipp
->ipp_fields
& IPPF_HOPOPTS
),
4951 ipp
->ipp_hopopts
, ipp
->ipp_hopoptslen
)) {
4952 optlen
+= sizeof (struct T_opthdr
) + ipp
->ipp_hopoptslen
;
4953 addflag
.crb_ipv6_recvhopopts
= 1;
4954 if (!ip_allocbuf((void **)&tcp
->tcp_hopopts
,
4955 &tcp
->tcp_hopoptslen
, (ipp
->ipp_fields
& IPPF_HOPOPTS
),
4956 ipp
->ipp_hopopts
, ipp
->ipp_hopoptslen
))
4959 /* If app asked for dst headers before routing headers ... */
4960 if (connp
->conn_recv_ancillary
.crb_ipv6_recvrthdrdstopts
&&
4961 ip_cmpbuf(tcp
->tcp_rthdrdstopts
, tcp
->tcp_rthdrdstoptslen
,
4962 (ipp
->ipp_fields
& IPPF_RTHDRDSTOPTS
),
4963 ipp
->ipp_rthdrdstopts
, ipp
->ipp_rthdrdstoptslen
)) {
4964 optlen
+= sizeof (struct T_opthdr
) +
4965 ipp
->ipp_rthdrdstoptslen
;
4966 addflag
.crb_ipv6_recvrthdrdstopts
= 1;
4967 if (!ip_allocbuf((void **)&tcp
->tcp_rthdrdstopts
,
4968 &tcp
->tcp_rthdrdstoptslen
,
4969 (ipp
->ipp_fields
& IPPF_RTHDRDSTOPTS
),
4970 ipp
->ipp_rthdrdstopts
, ipp
->ipp_rthdrdstoptslen
))
4973 /* If app asked for routing headers and it has changed ... */
4974 if (connp
->conn_recv_ancillary
.crb_ipv6_recvrthdr
&&
4975 ip_cmpbuf(tcp
->tcp_rthdr
, tcp
->tcp_rthdrlen
,
4976 (ipp
->ipp_fields
& IPPF_RTHDR
),
4977 ipp
->ipp_rthdr
, ipp
->ipp_rthdrlen
)) {
4978 optlen
+= sizeof (struct T_opthdr
) + ipp
->ipp_rthdrlen
;
4979 addflag
.crb_ipv6_recvrthdr
= 1;
4980 if (!ip_allocbuf((void **)&tcp
->tcp_rthdr
,
4981 &tcp
->tcp_rthdrlen
, (ipp
->ipp_fields
& IPPF_RTHDR
),
4982 ipp
->ipp_rthdr
, ipp
->ipp_rthdrlen
))
4985 /* If app asked for dest headers and it has changed ... */
4986 if ((connp
->conn_recv_ancillary
.crb_ipv6_recvdstopts
||
4987 connp
->conn_recv_ancillary
.crb_old_ipv6_recvdstopts
) &&
4988 ip_cmpbuf(tcp
->tcp_dstopts
, tcp
->tcp_dstoptslen
,
4989 (ipp
->ipp_fields
& IPPF_DSTOPTS
),
4990 ipp
->ipp_dstopts
, ipp
->ipp_dstoptslen
)) {
4991 optlen
+= sizeof (struct T_opthdr
) + ipp
->ipp_dstoptslen
;
4992 addflag
.crb_ipv6_recvdstopts
= 1;
4993 if (!ip_allocbuf((void **)&tcp
->tcp_dstopts
,
4994 &tcp
->tcp_dstoptslen
, (ipp
->ipp_fields
& IPPF_DSTOPTS
),
4995 ipp
->ipp_dstopts
, ipp
->ipp_dstoptslen
))
5000 /* Nothing to add */
5003 mp1
= allocb(sizeof (struct T_optdata_ind
) + optlen
, BPRI_MED
);
5006 * Defer sending ancillary data until the next TCP segment
5013 mp
->b_wptr
+= sizeof (*todi
) + optlen
;
5014 mp
->b_datap
->db_type
= M_PROTO
;
5015 todi
= (struct T_optdata_ind
*)mp
->b_rptr
;
5016 todi
->PRIM_type
= T_OPTDATA_IND
;
5017 todi
->DATA_flag
= 1; /* MORE data */
5018 todi
->OPT_length
= optlen
;
5019 todi
->OPT_offset
= sizeof (*todi
);
5020 optptr
= (uchar_t
*)&todi
[1];
5022 * If app asked for pktinfo and the index has changed ...
5023 * Note that the local address never changes for the connection.
5025 if (addflag
.crb_ip_recvpktinfo
) {
5026 struct in6_pktinfo
*pkti
;
5029 ifindex
= ira
->ira_ruifindex
;
5030 toh
= (struct T_opthdr
*)optptr
;
5031 toh
->level
= IPPROTO_IPV6
;
5032 toh
->name
= IPV6_PKTINFO
;
5033 toh
->len
= sizeof (*toh
) + sizeof (*pkti
);
5035 optptr
+= sizeof (*toh
);
5036 pkti
= (struct in6_pktinfo
*)optptr
;
5037 pkti
->ipi6_addr
= connp
->conn_laddr_v6
;
5038 pkti
->ipi6_ifindex
= ifindex
;
5039 optptr
+= sizeof (*pkti
);
5040 ASSERT(OK_32PTR(optptr
));
5041 /* Save as "last" value */
5042 tcp
->tcp_recvifindex
= ifindex
;
5044 /* If app asked for hoplimit and it has changed ... */
5045 if (addflag
.crb_ipv6_recvhoplimit
) {
5046 toh
= (struct T_opthdr
*)optptr
;
5047 toh
->level
= IPPROTO_IPV6
;
5048 toh
->name
= IPV6_HOPLIMIT
;
5049 toh
->len
= sizeof (*toh
) + sizeof (uint_t
);
5051 optptr
+= sizeof (*toh
);
5052 *(uint_t
*)optptr
= ipp
->ipp_hoplimit
;
5053 optptr
+= sizeof (uint_t
);
5054 ASSERT(OK_32PTR(optptr
));
5055 /* Save as "last" value */
5056 tcp
->tcp_recvhops
= ipp
->ipp_hoplimit
;
5058 /* If app asked for tclass and it has changed ... */
5059 if (addflag
.crb_ipv6_recvtclass
) {
5060 toh
= (struct T_opthdr
*)optptr
;
5061 toh
->level
= IPPROTO_IPV6
;
5062 toh
->name
= IPV6_TCLASS
;
5063 toh
->len
= sizeof (*toh
) + sizeof (uint_t
);
5065 optptr
+= sizeof (*toh
);
5066 *(uint_t
*)optptr
= ipp
->ipp_tclass
;
5067 optptr
+= sizeof (uint_t
);
5068 ASSERT(OK_32PTR(optptr
));
5069 /* Save as "last" value */
5070 tcp
->tcp_recvtclass
= ipp
->ipp_tclass
;
5072 if (addflag
.crb_ipv6_recvhopopts
) {
5073 toh
= (struct T_opthdr
*)optptr
;
5074 toh
->level
= IPPROTO_IPV6
;
5075 toh
->name
= IPV6_HOPOPTS
;
5076 toh
->len
= sizeof (*toh
) + ipp
->ipp_hopoptslen
;
5078 optptr
+= sizeof (*toh
);
5079 bcopy((uchar_t
*)ipp
->ipp_hopopts
, optptr
, ipp
->ipp_hopoptslen
);
5080 optptr
+= ipp
->ipp_hopoptslen
;
5081 ASSERT(OK_32PTR(optptr
));
5082 /* Save as last value */
5083 ip_savebuf((void **)&tcp
->tcp_hopopts
, &tcp
->tcp_hopoptslen
,
5084 (ipp
->ipp_fields
& IPPF_HOPOPTS
),
5085 ipp
->ipp_hopopts
, ipp
->ipp_hopoptslen
);
5087 if (addflag
.crb_ipv6_recvrthdrdstopts
) {
5088 toh
= (struct T_opthdr
*)optptr
;
5089 toh
->level
= IPPROTO_IPV6
;
5090 toh
->name
= IPV6_RTHDRDSTOPTS
;
5091 toh
->len
= sizeof (*toh
) + ipp
->ipp_rthdrdstoptslen
;
5093 optptr
+= sizeof (*toh
);
5094 bcopy(ipp
->ipp_rthdrdstopts
, optptr
, ipp
->ipp_rthdrdstoptslen
);
5095 optptr
+= ipp
->ipp_rthdrdstoptslen
;
5096 ASSERT(OK_32PTR(optptr
));
5097 /* Save as last value */
5098 ip_savebuf((void **)&tcp
->tcp_rthdrdstopts
,
5099 &tcp
->tcp_rthdrdstoptslen
,
5100 (ipp
->ipp_fields
& IPPF_RTHDRDSTOPTS
),
5101 ipp
->ipp_rthdrdstopts
, ipp
->ipp_rthdrdstoptslen
);
5103 if (addflag
.crb_ipv6_recvrthdr
) {
5104 toh
= (struct T_opthdr
*)optptr
;
5105 toh
->level
= IPPROTO_IPV6
;
5106 toh
->name
= IPV6_RTHDR
;
5107 toh
->len
= sizeof (*toh
) + ipp
->ipp_rthdrlen
;
5109 optptr
+= sizeof (*toh
);
5110 bcopy(ipp
->ipp_rthdr
, optptr
, ipp
->ipp_rthdrlen
);
5111 optptr
+= ipp
->ipp_rthdrlen
;
5112 ASSERT(OK_32PTR(optptr
));
5113 /* Save as last value */
5114 ip_savebuf((void **)&tcp
->tcp_rthdr
, &tcp
->tcp_rthdrlen
,
5115 (ipp
->ipp_fields
& IPPF_RTHDR
),
5116 ipp
->ipp_rthdr
, ipp
->ipp_rthdrlen
);
5118 if (addflag
.crb_ipv6_recvdstopts
) {
5119 toh
= (struct T_opthdr
*)optptr
;
5120 toh
->level
= IPPROTO_IPV6
;
5121 toh
->name
= IPV6_DSTOPTS
;
5122 toh
->len
= sizeof (*toh
) + ipp
->ipp_dstoptslen
;
5124 optptr
+= sizeof (*toh
);
5125 bcopy(ipp
->ipp_dstopts
, optptr
, ipp
->ipp_dstoptslen
);
5126 optptr
+= ipp
->ipp_dstoptslen
;
5127 ASSERT(OK_32PTR(optptr
));
5128 /* Save as last value */
5129 ip_savebuf((void **)&tcp
->tcp_dstopts
, &tcp
->tcp_dstoptslen
,
5130 (ipp
->ipp_fields
& IPPF_DSTOPTS
),
5131 ipp
->ipp_dstopts
, ipp
->ipp_dstoptslen
);
5133 ASSERT(optptr
== mp
->b_wptr
);
5137 /* The minimum of smoothed mean deviation in RTO calculation. */
5138 #define TCP_SD_MIN 400
5141 * Set RTO for this connection. The formula is from Jacobson and Karels'
5142 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names
5143 * are the same as those in Appendix A.2 of that paper.
5145 * m = new measurement
5146 * sa = smoothed RTT average (8 * average estimates).
5147 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates).
5150 tcp_set_rto(tcp_t
*tcp
, clock_t rtt
)
5152 long m
= TICK_TO_MSEC(rtt
);
5153 clock_t sa
= tcp
->tcp_rtt_sa
;
5154 clock_t sv
= tcp
->tcp_rtt_sd
;
5156 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
5158 TCPS_BUMP_MIB(tcps
, tcpRttUpdate
);
5159 tcp
->tcp_rtt_update
++;
5161 /* tcp_rtt_sa is not 0 means this is a new sample. */
5164 * Update average estimator:
5165 * new rtt = 7/8 old rtt + 1/8 Error
5168 /* m is now Error in estimate. */
5170 if ((sa
+= m
) <= 0) {
5172 * Don't allow the smoothed average to be negative.
5173 * We use 0 to denote reinitialization of the
5180 * Update deviation estimator:
5181 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev)
5189 * This follows BSD's implementation. So the reinitialized
5190 * RTO is 3 * m. We cannot go less than 2 because if the
5191 * link is bandwidth dominated, doubling the window size
5192 * during slow start means doubling the RTT. We want to be
5193 * more conservative when we reinitialize our estimates. 3
5194 * is just a convenient number.
5199 if (sv
< TCP_SD_MIN
) {
5201 * We do not know that if sa captures the delay ACK
5202 * effect as in a long train of segments, a receiver
5203 * does not delay its ACKs. So set the minimum of sv
5204 * to be TCP_SD_MIN, which is default to 400 ms, twice
5205 * of BSD DATO. That means the minimum of mean
5206 * deviation is 100 ms.
5211 tcp
->tcp_rtt_sa
= sa
;
5212 tcp
->tcp_rtt_sd
= sv
;
5214 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv)
5216 * Add tcp_rexmit_interval extra in case of extreme environment
5217 * where the algorithm fails to work. The default value of
5218 * tcp_rexmit_interval_extra should be 0.
5220 * As we use a finer grained clock than BSD and update
5221 * RTO for every ACKs, add in another .25 of RTT to the
5222 * deviation of RTO to accomodate burstiness of 1/4 of
5225 rto
= (sa
>> 3) + sv
+ tcps
->tcps_rexmit_interval_extra
+ (sa
>> 5);
5227 TCP_SET_RTO(tcp
, rto
);
5229 /* Now, we can reset tcp_timer_backoff to use the new RTO... */
5230 tcp
->tcp_timer_backoff
= 0;
5234 tcp_rwnd_reopen(tcp_t
*tcp
)
5238 conn_t
*connp
= tcp
->tcp_connp
;
5240 /* Learn the latest rwnd information that we sent to the other side. */
5241 thwin
= ((uint_t
)ntohs(tcp
->tcp_tcpha
->tha_win
))
5243 /* This is peer's calculated send window (our receive window). */
5244 thwin
-= tcp
->tcp_rnxt
- tcp
->tcp_rack
;
5246 * Increase the receive window to max. But we need to do receiver
5247 * SWS avoidance. This means that we need to check the increase of
5248 * of receive window is at least 1 MSS.
5250 if (connp
->conn_rcvbuf
- thwin
>= tcp
->tcp_mss
) {
5252 * If the window that the other side knows is less than max
5253 * deferred acks segments, send an update immediately.
5255 if (thwin
< tcp
->tcp_rack_cur_max
* tcp
->tcp_mss
) {
5256 TCPS_BUMP_MIB(tcp
->tcp_tcps
, tcpOutWinUpdate
);
5257 ret
= TH_ACK_NEEDED
;
5259 tcp
->tcp_rwnd
= connp
->conn_rcvbuf
;
5265 * Handle a packet that has been reclassified by TCP.
5266 * This function drops the ref on connp that the caller had.
5269 tcp_reinput(conn_t
*connp
, mblk_t
*mp
, ip_recv_attr_t
*ira
, ip_stack_t
*ipst
)
5271 ipsec_stack_t
*ipss
= ipst
->ips_netstack
->netstack_ipsec
;
5273 if (connp
->conn_incoming_ifindex
!= 0 &&
5274 connp
->conn_incoming_ifindex
!= ira
->ira_ruifindex
) {
5276 CONN_DEC_REF(connp
);
5280 if (CONN_INBOUND_POLICY_PRESENT_V6(connp
, ipss
) ||
5281 (ira
->ira_flags
& IRAF_IPSEC_SECURE
)) {
5285 if (ira
->ira_flags
& IRAF_IS_IPV4
) {
5286 ipha
= (ipha_t
*)mp
->b_rptr
;
5290 ip6h
= (ip6_t
*)mp
->b_rptr
;
5292 mp
= ipsec_check_inbound_policy(mp
, connp
, ipha
, ip6h
, ira
);
5294 BUMP_MIB(&ipst
->ips_ip_mib
, ipIfStatsInDiscards
);
5295 /* Note that mp is NULL */
5296 ip_drop_input("ipIfStatsInDiscards", mp
, NULL
);
5297 CONN_DEC_REF(connp
);
5302 if (IPCL_IS_TCP(connp
)) {
5304 * do not drain, certain use cases can blow
5307 SQUEUE_ENTER_ONE(connp
->conn_sqp
, mp
,
5308 connp
->conn_recv
, connp
, ira
,
5309 SQ_NODRAIN
, SQTAG_IP_TCP_INPUT
);
5311 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
5312 (connp
->conn_recv
)(connp
, mp
, NULL
,
5314 CONN_DEC_REF(connp
);
5321 tcp_rsrv_input(void *arg
, mblk_t
*mp
, void *arg2
, ip_recv_attr_t
*dummy
)
5323 conn_t
*connp
= (conn_t
*)arg
;
5324 tcp_t
*tcp
= connp
->conn_tcp
;
5325 queue_t
*q
= connp
->conn_rq
;
5327 ASSERT(!IPCL_IS_NONSTR(connp
));
5328 mutex_enter(&tcp
->tcp_rsrv_mp_lock
);
5329 tcp
->tcp_rsrv_mp
= mp
;
5330 mutex_exit(&tcp
->tcp_rsrv_mp_lock
);
5332 if (TCP_IS_DETACHED(tcp
) || q
== NULL
) {
5336 if (tcp
->tcp_fused
) {
5337 tcp_fuse_backenable(tcp
);
5341 if (canputnext(q
)) {
5342 /* Not flow-controlled, open rwnd */
5343 tcp
->tcp_rwnd
= connp
->conn_rcvbuf
;
5346 * Send back a window update immediately if TCP is above
5347 * ESTABLISHED state and the increase of the rcv window
5348 * that the other side knows is at least 1 MSS after flow
5349 * control is lifted.
5351 if (tcp
->tcp_state
>= TCPS_ESTABLISHED
&&
5352 tcp_rwnd_reopen(tcp
) == TH_ACK_NEEDED
) {
5353 tcp_xmit_ctl(NULL
, tcp
,
5354 (tcp
->tcp_swnd
== 0) ? tcp
->tcp_suna
:
5355 tcp
->tcp_snxt
, tcp
->tcp_rnxt
, TH_ACK
);
5361 * The read side service routine is called mostly when we get back-enabled as a
5362 * result of flow control relief. Since we don't actually queue anything in
5363 * TCP, we have no data to send out of here. What we do is clear the receive
5364 * window, and send out a window update.
5367 tcp_rsrv(queue_t
*q
)
5369 conn_t
*connp
= Q_TO_CONN(q
);
5370 tcp_t
*tcp
= connp
->conn_tcp
;
5373 /* No code does a putq on the read side */
5374 ASSERT(q
->q_first
== NULL
);
5377 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already
5378 * been run. So just return.
5380 mutex_enter(&tcp
->tcp_rsrv_mp_lock
);
5381 if ((mp
= tcp
->tcp_rsrv_mp
) == NULL
) {
5382 mutex_exit(&tcp
->tcp_rsrv_mp_lock
);
5385 tcp
->tcp_rsrv_mp
= NULL
;
5386 mutex_exit(&tcp
->tcp_rsrv_mp_lock
);
5388 CONN_INC_REF(connp
);
5389 SQUEUE_ENTER_ONE(connp
->conn_sqp
, mp
, tcp_rsrv_input
, connp
,
5390 NULL
, SQ_PROCESS
, SQTAG_TCP_RSRV
);
5393 /* At minimum we need 8 bytes in the TCP header for the lookup */
5394 #define ICMP_MIN_TCP_HDR 8
5397 * tcp_icmp_input is called as conn_recvicmp to process ICMP error messages
5398 * passed up by IP. The message is always received on the correct tcp_t.
5399 * Assumes that IP has pulled up everything up to and including the ICMP header.
5403 tcp_icmp_input(void *arg1
, mblk_t
*mp
, void *arg2
, ip_recv_attr_t
*ira
)
5405 conn_t
*connp
= (conn_t
*)arg1
;
5411 tcp_t
*tcp
= connp
->conn_tcp
;
5413 /* Assume IP provides aligned packets */
5414 ASSERT(OK_32PTR(mp
->b_rptr
));
5415 ASSERT((MBLKL(mp
) >= sizeof (ipha_t
)));
5418 * It's possible we have a closed, but not yet destroyed, TCP
5419 * connection. Several fields (e.g. conn_ixa->ixa_ire) are invalid
5420 * in the closed state, so don't take any chances and drop the packet.
5422 if (tcp
->tcp_state
== TCPS_CLOSED
) {
5428 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent
5429 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6.
5431 if (!(ira
->ira_flags
& IRAF_IS_IPV4
)) {
5432 tcp_icmp_error_ipv6(tcp
, mp
, ira
);
5436 /* Skip past the outer IP and ICMP headers */
5437 iph_hdr_length
= ira
->ira_ip_hdr_length
;
5438 icmph
= (icmph_t
*)&mp
->b_rptr
[iph_hdr_length
];
5440 * If we don't have the correct outer IP header length
5441 * or if we don't have a complete inner IP header
5444 if (iph_hdr_length
< sizeof (ipha_t
) ||
5445 (ipha_t
*)&icmph
[1] + 1 > (ipha_t
*)mp
->b_wptr
) {
5450 ipha
= (ipha_t
*)&icmph
[1];
5452 /* Skip past the inner IP and find the ULP header */
5453 iph_hdr_length
= IPH_HDR_LENGTH(ipha
);
5454 tcpha
= (tcpha_t
*)((char *)ipha
+ iph_hdr_length
);
5456 * If we don't have the correct inner IP header length or if the ULP
5457 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR
5458 * bytes of TCP header, drop it.
5460 if (iph_hdr_length
< sizeof (ipha_t
) ||
5461 ipha
->ipha_protocol
!= IPPROTO_TCP
||
5462 (uchar_t
*)tcpha
+ ICMP_MIN_TCP_HDR
> mp
->b_wptr
) {
5466 seg_seq
= ntohl(tcpha
->tha_seq
);
5467 switch (icmph
->icmph_type
) {
5468 case ICMP_DEST_UNREACHABLE
:
5469 switch (icmph
->icmph_code
) {
5470 case ICMP_FRAGMENTATION_NEEDED
:
5472 * Update Path MTU, then try to send something out.
5474 tcp_update_pmtu(tcp
, B_TRUE
);
5475 tcp_rexmit_after_error(tcp
);
5477 case ICMP_PORT_UNREACHABLE
:
5478 case ICMP_PROTOCOL_UNREACHABLE
:
5479 switch (tcp
->tcp_state
) {
5483 * ICMP can snipe away incipient
5484 * TCP connections as long as
5485 * seq number is same as initial
5488 if (seg_seq
== tcp
->tcp_iss
) {
5489 (void) tcp_clean_death(tcp
,
5495 case ICMP_HOST_UNREACHABLE
:
5496 case ICMP_NET_UNREACHABLE
:
5497 /* Record the error in case we finally time out. */
5498 if (icmph
->icmph_code
== ICMP_HOST_UNREACHABLE
)
5499 tcp
->tcp_client_errno
= EHOSTUNREACH
;
5501 tcp
->tcp_client_errno
= ENETUNREACH
;
5502 if (tcp
->tcp_state
== TCPS_SYN_RCVD
) {
5503 if (tcp
->tcp_listener
!= NULL
&&
5504 tcp
->tcp_listener
->tcp_syn_defense
) {
5506 * Ditch the half-open connection if we
5507 * suspect a SYN attack is under way.
5509 (void) tcp_clean_death(tcp
,
5510 tcp
->tcp_client_errno
);
5518 case ICMP_SOURCE_QUENCH
: {
5520 * use a global boolean to control
5521 * whether TCP should respond to ICMP_SOURCE_QUENCH.
5522 * The default is false.
5524 if (tcp_icmp_source_quench
) {
5526 * Reduce the sending rate as if we got a
5527 * retransmit timeout
5531 npkt
= ((tcp
->tcp_snxt
- tcp
->tcp_suna
) >> 1) /
5533 tcp
->tcp_cwnd_ssthresh
= MAX(npkt
, 2) * tcp
->tcp_mss
;
5534 tcp
->tcp_cwnd
= tcp
->tcp_mss
;
5535 tcp
->tcp_cwnd_cnt
= 0;
5544 * tcp_icmp_error_ipv6 is called from tcp_icmp_input to process ICMPv6
5545 * error messages passed up by IP.
5546 * Assumes that IP has pulled up all the extension headers as well
5547 * as the ICMPv6 header.
5550 tcp_icmp_error_ipv6(tcp_t
*tcp
, mblk_t
*mp
, ip_recv_attr_t
*ira
)
5554 uint16_t iph_hdr_length
= ira
->ira_ip_hdr_length
;
5560 * Verify that we have a complete IP header.
5562 ASSERT((MBLKL(mp
) >= sizeof (ip6_t
)));
5564 icmp6
= (icmp6_t
*)&mp
->b_rptr
[iph_hdr_length
];
5565 ip6h
= (ip6_t
*)&icmp6
[1];
5567 * Verify if we have a complete ICMP and inner IP header.
5569 if ((uchar_t
*)&ip6h
[1] > mp
->b_wptr
) {
5575 if (!ip_hdr_length_nexthdr_v6(mp
, ip6h
, &iph_hdr_length
, &nexthdrp
))
5577 tcpha
= (tcpha_t
*)((char *)ip6h
+ iph_hdr_length
);
5579 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't
5580 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the
5583 if ((*nexthdrp
!= IPPROTO_TCP
) ||
5584 ((uchar_t
*)tcpha
+ ICMP_MIN_TCP_HDR
) > mp
->b_wptr
) {
5588 seg_seq
= ntohl(tcpha
->tha_seq
);
5589 switch (icmp6
->icmp6_type
) {
5590 case ICMP6_PACKET_TOO_BIG
:
5592 * Update Path MTU, then try to send something out.
5594 tcp_update_pmtu(tcp
, B_TRUE
);
5595 tcp_rexmit_after_error(tcp
);
5597 case ICMP6_DST_UNREACH
:
5598 switch (icmp6
->icmp6_code
) {
5599 case ICMP6_DST_UNREACH_NOPORT
:
5600 if (((tcp
->tcp_state
== TCPS_SYN_SENT
) ||
5601 (tcp
->tcp_state
== TCPS_SYN_RCVD
)) &&
5602 (seg_seq
== tcp
->tcp_iss
)) {
5603 (void) tcp_clean_death(tcp
, ECONNREFUSED
);
5606 case ICMP6_DST_UNREACH_ADMIN
:
5607 case ICMP6_DST_UNREACH_NOROUTE
:
5608 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
5609 case ICMP6_DST_UNREACH_ADDR
:
5610 /* Record the error in case we finally time out. */
5611 tcp
->tcp_client_errno
= EHOSTUNREACH
;
5612 if (((tcp
->tcp_state
== TCPS_SYN_SENT
) ||
5613 (tcp
->tcp_state
== TCPS_SYN_RCVD
)) &&
5614 (seg_seq
== tcp
->tcp_iss
)) {
5615 if (tcp
->tcp_listener
!= NULL
&&
5616 tcp
->tcp_listener
->tcp_syn_defense
) {
5618 * Ditch the half-open connection if we
5619 * suspect a SYN attack is under way.
5621 (void) tcp_clean_death(tcp
,
5622 tcp
->tcp_client_errno
);
5632 case ICMP6_PARAM_PROB
:
5633 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
5634 if (icmp6
->icmp6_code
== ICMP6_PARAMPROB_NEXTHEADER
&&
5635 (uchar_t
*)ip6h
+ icmp6
->icmp6_pptr
==
5636 (uchar_t
*)nexthdrp
) {
5637 if (tcp
->tcp_state
== TCPS_SYN_SENT
||
5638 tcp
->tcp_state
== TCPS_SYN_RCVD
) {
5639 (void) tcp_clean_death(tcp
, ECONNREFUSED
);
5645 case ICMP6_TIME_EXCEEDED
:
5653 * CALLED OUTSIDE OF SQUEUE! It can not follow any pointers that tcp might
5654 * change. But it can refer to fields like tcp_suna and tcp_snxt.
5656 * Function tcp_verifyicmp is called as conn_verifyicmp to verify the ICMP
5657 * error messages received by IP. The message is always received on the correct
5662 tcp_verifyicmp(conn_t
*connp
, void *arg2
, icmph_t
*icmph
, icmp6_t
*icmp6
,
5663 ip_recv_attr_t
*ira
)
5665 tcpha_t
*tcpha
= (tcpha_t
*)arg2
;
5666 uint32_t seq
= ntohl(tcpha
->tha_seq
);
5667 tcp_t
*tcp
= connp
->conn_tcp
;
5670 * TCP sequence number contained in payload of the ICMP error message
5671 * should be within the range SND.UNA <= SEG.SEQ < SND.NXT. Otherwise,
5672 * the message is either a stale ICMP error, or an attack from the
5673 * network. Fail the verification.
5675 if (SEQ_LT(seq
, tcp
->tcp_suna
) || SEQ_GEQ(seq
, tcp
->tcp_snxt
))
5678 /* For "too big" we also check the ignore flag */
5679 if (ira
->ira_flags
& IRAF_IS_IPV4
) {
5680 ASSERT(icmph
!= NULL
);
5681 if (icmph
->icmph_type
== ICMP_DEST_UNREACHABLE
&&
5682 icmph
->icmph_code
== ICMP_FRAGMENTATION_NEEDED
&&
5683 tcp
->tcp_tcps
->tcps_ignore_path_mtu
)
5686 ASSERT(icmp6
!= NULL
);
5687 if (icmp6
->icmp6_type
== ICMP6_PACKET_TOO_BIG
&&
5688 tcp
->tcp_tcps
->tcps_ignore_path_mtu
)