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 (c) 2014 by Delphix. All rights reserved.
27 /* This file contains all TCP output processing functions. */
29 #include <sys/types.h>
30 #include <sys/stream.h>
31 #include <sys/strsun.h>
32 #include <sys/strsubr.h>
33 #include <sys/stropts.h>
34 #include <sys/strlog.h>
35 #define _SUN_TPI_VERSION 2
36 #include <sys/tihdr.h>
37 #include <sys/suntpi.h>
38 #include <sys/xti_inet.h>
39 #include <sys/timod.h>
40 #include <sys/pattr.h>
41 #include <sys/squeue_impl.h>
42 #include <sys/squeue.h>
43 #include <sys/sockio.h>
45 #include <inet/common.h>
48 #include <inet/tcp_impl.h>
49 #include <inet/snmpcom.h>
50 #include <inet/proto_set.h>
51 #include <inet/ipsec_impl.h>
52 #include <inet/ip_ndp.h>
54 static mblk_t
*tcp_get_seg_mp(tcp_t
*, uint32_t, int32_t *);
55 static void tcp_wput_cmdblk(queue_t
*, mblk_t
*);
56 static void tcp_wput_flush(tcp_t
*, mblk_t
*);
57 static void tcp_wput_iocdata(tcp_t
*tcp
, mblk_t
*mp
);
58 static int tcp_xmit_end(tcp_t
*);
59 static int tcp_send(tcp_t
*, const int, const int, const int,
60 const int, int *, uint_t
*, int *, mblk_t
**, mblk_t
*);
61 static void tcp_xmit_early_reset(char *, mblk_t
*, uint32_t, uint32_t,
62 int, ip_recv_attr_t
*, ip_stack_t
*, conn_t
*);
63 static boolean_t
tcp_send_rst_chk(tcp_stack_t
*);
64 static void tcp_process_shrunk_swnd(tcp_t
*, uint32_t);
65 static void tcp_fill_header(tcp_t
*, uchar_t
*, clock_t, int);
68 * Functions called directly via squeue having a prototype of edesc_t.
70 static void tcp_wput_nondata(void *, mblk_t
*, void *, ip_recv_attr_t
*);
71 static void tcp_wput_ioctl(void *, mblk_t
*, void *, ip_recv_attr_t
*);
72 static void tcp_wput_proto(void *, mblk_t
*, void *, ip_recv_attr_t
*);
75 * This controls how tiny a write must be before we try to copy it
76 * into the mblk on the tail of the transmit queue. Not much
77 * speedup is observed for values larger than sixteen. Zero will
78 * disable the optimisation.
80 static int tcp_tx_pull_len
= 16;
83 tcp_wput(queue_t
*q
, mblk_t
*mp
)
85 conn_t
*connp
= Q_TO_CONN(q
);
87 void (*output_proc
)();
93 ASSERT(connp
->conn_ref
>= 2);
95 switch (DB_TYPE(mp
)) {
97 tcp
= connp
->conn_tcp
;
102 mutex_enter(&tcp
->tcp_non_sq_lock
);
103 tcp
->tcp_squeue_bytes
+= size
;
104 if (TCP_UNSENT_BYTES(tcp
) > connp
->conn_sndbuf
) {
107 mutex_exit(&tcp
->tcp_non_sq_lock
);
110 SQUEUE_ENTER_ONE(connp
->conn_sqp
, mp
, tcp_output
, connp
,
111 NULL
, tcp_squeue_flag
, SQTAG_TCP_OUTPUT
);
115 tcp_wput_cmdblk(q
, mp
);
121 * if it is a snmp message, don't get behind the squeue
123 tcp
= connp
->conn_tcp
;
125 if ((mp
->b_wptr
- rptr
) >= sizeof (t_scalar_t
)) {
126 type
= ((union T_primitives
*)rptr
)->type
;
128 if (connp
->conn_debug
) {
129 (void) strlog(TCP_MOD_ID
, 0, 1,
131 "tcp_wput_proto, dropping one...");
136 if (type
== T_SVR4_OPTMGMT_REQ
) {
138 * All Solaris components should pass a db_credp
139 * for this TPI message, hence we ASSERT.
140 * But in case there is some other M_PROTO that looks
141 * like a TPI message sent by some other kernel
142 * component, we check and return an error.
144 cred_t
*cr
= msg_getcred(mp
, NULL
);
148 tcp_err_ack(tcp
, mp
, TSYSERR
, EINVAL
);
151 if (snmpcom_req(q
, mp
, tcp_snmp_set
, ip_snmp_get
,
154 * This was a SNMP request
158 output_proc
= tcp_wput_proto
;
161 output_proc
= tcp_wput_proto
;
166 * Most ioctls can be processed right away without going via
167 * squeues - process them right here. Those that do require
168 * squeue (currently _SIOCSOCKFALLBACK)
169 * are processed by tcp_wput_ioctl().
171 iocp
= (struct iocblk
*)mp
->b_rptr
;
172 tcp
= connp
->conn_tcp
;
174 switch (iocp
->ioc_cmd
) {
175 case TCP_IOC_ABORT_CONN
:
176 tcp_ioctl_abort_conn(q
, mp
);
180 mi_copyin(q
, mp
, NULL
,
181 SIZEOF_STRUCT(strbuf
, iocp
->ioc_flag
));
185 output_proc
= tcp_wput_ioctl
;
190 output_proc
= tcp_wput_nondata
;
195 SQUEUE_ENTER_ONE(connp
->conn_sqp
, mp
, output_proc
, connp
,
196 NULL
, tcp_squeue_flag
, SQTAG_TCP_WPUT_OTHER
);
200 * The TCP normal data output path.
201 * NOTE: the logic of the fast path is duplicated from this function.
204 tcp_wput_data(tcp_t
*tcp
, mblk_t
*mp
, boolean_t urgent
)
215 int32_t num_sack_blk
= 0;
216 int32_t total_hdr_len
;
219 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
220 conn_t
*connp
= tcp
->tcp_connp
;
221 clock_t now
= LBOLT_FASTPATH
;
223 tcpstate
= tcp
->tcp_state
;
226 * tcp_wput_data() with NULL mp should only be called when
227 * there is unsent data.
229 ASSERT(tcp
->tcp_unsent
> 0);
230 /* Really tacky... but we need this for detached closes. */
231 len
= tcp
->tcp_unsent
;
235 ASSERT(mp
->b_datap
->db_type
== M_DATA
);
237 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ,
238 * or before a connection attempt has begun.
240 if (tcpstate
< TCPS_SYN_SENT
|| tcpstate
> TCPS_CLOSE_WAIT
||
241 (tcp
->tcp_valid_bits
& TCP_FSS_VALID
) != 0) {
242 if ((tcp
->tcp_valid_bits
& TCP_FSS_VALID
) != 0) {
245 "tcp_wput_data: data after ordrel, %s",
246 tcp_display(tcp
, NULL
,
247 DISP_ADDR_AND_PORT
));
249 if (connp
->conn_debug
) {
250 (void) strlog(TCP_MOD_ID
, 0, 1,
252 "tcp_wput_data: data after ordrel, %s\n",
253 tcp_display(tcp
, NULL
,
254 DISP_ADDR_AND_PORT
));
258 if (tcp
->tcp_snd_zcopy_aware
&&
259 (mp
->b_datap
->db_struioflag
& STRUIO_ZCNOTIFY
))
260 tcp_zcopy_notify(tcp
);
262 mutex_enter(&tcp
->tcp_non_sq_lock
);
263 if (tcp
->tcp_flow_stopped
&&
264 TCP_UNSENT_BYTES(tcp
) <= connp
->conn_sndlowat
) {
267 mutex_exit(&tcp
->tcp_non_sq_lock
);
273 ASSERT((uintptr_t)(mp
->b_wptr
- mp
->b_rptr
) <=
275 len
= (int)(mp
->b_wptr
- mp
->b_rptr
);
286 /* If we are the first on the list ... */
287 if (tcp
->tcp_xmit_head
== NULL
) {
288 tcp
->tcp_xmit_head
= mp
;
289 tcp
->tcp_xmit_tail
= mp
;
290 tcp
->tcp_xmit_tail_unsent
= len
;
292 /* If tiny tx and room in txq tail, pullup to save mblks. */
295 mp1
= tcp
->tcp_xmit_last
;
296 if (len
< tcp_tx_pull_len
&&
297 (dp
= mp1
->b_datap
)->db_ref
== 1 &&
298 dp
->db_lim
- mp1
->b_wptr
>= len
) {
300 ASSERT(!mp1
->b_cont
);
302 *mp1
->b_wptr
++ = *mp
->b_rptr
;
304 bcopy(mp
->b_rptr
, mp1
->b_wptr
, len
);
307 if (mp1
== tcp
->tcp_xmit_tail
)
308 tcp
->tcp_xmit_tail_unsent
+= len
;
309 mp1
->b_cont
= mp
->b_cont
;
310 if (tcp
->tcp_snd_zcopy_aware
&&
311 (mp
->b_datap
->db_struioflag
& STRUIO_ZCNOTIFY
))
312 mp1
->b_datap
->db_struioflag
|= STRUIO_ZCNOTIFY
;
316 tcp
->tcp_xmit_last
->b_cont
= mp
;
318 len
+= tcp
->tcp_unsent
;
321 /* Tack on however many more positive length mblks we have */
322 if ((mp1
= mp
->b_cont
) != NULL
) {
325 ASSERT((uintptr_t)(mp1
->b_wptr
- mp1
->b_rptr
) <=
327 tlen
= (int)(mp1
->b_wptr
- mp1
->b_rptr
);
329 mp
->b_cont
= mp1
->b_cont
;
335 } while ((mp1
= mp
->b_cont
) != NULL
);
337 tcp
->tcp_xmit_last
= mp
;
338 tcp
->tcp_unsent
= len
;
344 snxt
= tcp
->tcp_snxt
;
345 xmit_tail
= tcp
->tcp_xmit_tail
;
346 tail_unsent
= tcp
->tcp_xmit_tail_unsent
;
349 * Note that tcp_mss has been adjusted to take into account the
350 * timestamp option if applicable. Because SACK options do not
351 * appear in every TCP segments and they are of variable lengths,
352 * they cannot be included in tcp_mss. Thus we need to calculate
353 * the actual segment length when we need to send a segment which
354 * includes SACK options.
356 if (tcp
->tcp_snd_sack_ok
&& tcp
->tcp_num_sack_blk
> 0) {
359 num_sack_blk
= MIN(tcp
->tcp_max_sack_blk
,
360 tcp
->tcp_num_sack_blk
);
361 opt_len
= num_sack_blk
* sizeof (sack_blk_t
) + TCPOPT_NOP_LEN
*
362 2 + TCPOPT_HEADER_LEN
;
363 mss
= tcp
->tcp_mss
- opt_len
;
364 total_hdr_len
= connp
->conn_ht_iphc_len
+ opt_len
;
365 tcp_hdr_len
= connp
->conn_ht_ulp_len
+ opt_len
;
368 total_hdr_len
= connp
->conn_ht_iphc_len
;
369 tcp_hdr_len
= connp
->conn_ht_ulp_len
;
372 if ((tcp
->tcp_suna
== snxt
) && !tcp
->tcp_localnet
&&
373 (TICK_TO_MSEC(now
- tcp
->tcp_last_recv_time
) >= tcp
->tcp_rto
)) {
374 TCP_SET_INIT_CWND(tcp
, mss
, tcps
->tcps_slow_start_after_idle
);
376 if (tcpstate
== TCPS_SYN_RCVD
) {
378 * The three-way connection establishment handshake is not
379 * complete yet. We want to queue the data for transmission
380 * after entering ESTABLISHED state (RFC793). A jump to
381 * "done" label effectively leaves data on the queue.
388 * In the special case when cwnd is zero, which can only
389 * happen if the connection is ECN capable, return now.
390 * New segments is sent using tcp_timer(). The timer
391 * is set in tcp_input_data().
393 if (tcp
->tcp_cwnd
== 0) {
395 * Note that tcp_cwnd is 0 before 3-way handshake is
398 ASSERT(tcp
->tcp_ecn_ok
||
399 tcp
->tcp_state
< TCPS_ESTABLISHED
);
403 /* NOTE: trouble if xmitting while SYN not acked? */
404 usable_r
= snxt
- tcp
->tcp_suna
;
405 usable_r
= tcp
->tcp_swnd
- usable_r
;
408 * Check if the receiver has shrunk the window. If
409 * tcp_wput_data() with NULL mp is called, tcp_fin_sent
410 * cannot be set as there is unsent data, so FIN cannot
411 * be sent out. Otherwise, we need to take into account
412 * of FIN as it consumes an "invisible" sequence number.
414 ASSERT(tcp
->tcp_fin_sent
== 0);
417 * The receiver has shrunk the window and we have sent
418 * -usable_r date beyond the window, re-adjust.
420 * If TCP window scaling is enabled, there can be
421 * round down error as the advertised receive window
422 * is actually right shifted n bits. This means that
423 * the lower n bits info is wiped out. It will look
424 * like the window is shrunk. Do a check here to
425 * see if the shrunk amount is actually within the
426 * error in window calculation. If it is, just
427 * return. Note that this check is inside the
428 * shrunk window check. This makes sure that even
429 * though tcp_process_shrunk_swnd() is not called,
430 * we will stop further processing.
432 if ((-usable_r
>> tcp
->tcp_snd_ws
) > 0) {
433 tcp_process_shrunk_swnd(tcp
, -usable_r
);
438 /* usable = MIN(swnd, cwnd) - unacked_bytes */
439 if (tcp
->tcp_swnd
> tcp
->tcp_cwnd
)
440 usable_r
-= tcp
->tcp_swnd
- tcp
->tcp_cwnd
;
442 /* usable = MIN(usable, unsent) */
446 /* usable = MAX(usable, {1 for urgent, 0 for data}) */
450 /* Bypass all other unnecessary processing. */
455 local_time
= (mblk_t
*)now
;
458 * "Our" Nagle Algorithm. This is not the same as in the old
459 * BSD. This is more in line with the true intent of Nagle.
461 * The conditions are:
462 * 1. The amount of unsent data (or amount of data which can be
463 * sent, whichever is smaller) is less than Nagle limit.
464 * 2. The last sent size is also less than Nagle limit.
465 * 3. There is unack'ed data.
466 * 4. Urgent pointer is not set. Send urgent data ignoring the
467 * Nagle algorithm. This reduces the probability that urgent
468 * bytes get "merged" together.
469 * 5. The app has not closed the connection. This eliminates the
470 * wait time of the receiving side waiting for the last piece of
473 * If all are satisified, exit without sending anything. Note
474 * that Nagle limit can be smaller than 1 MSS. Nagle limit is
475 * the smaller of 1 MSS and global tcp_naglim_def (default to be
478 if (usable
< (int)tcp
->tcp_naglim
&&
479 tcp
->tcp_naglim
> tcp
->tcp_last_sent_len
&&
480 snxt
!= tcp
->tcp_suna
&&
481 !(tcp
->tcp_valid_bits
& TCP_URG_VALID
) &&
482 !(tcp
->tcp_valid_bits
& TCP_FSS_VALID
)) {
487 * If tcp_zero_win_probe is not set and the tcp->tcp_cork option
488 * is set, then we have to force TCP not to send partial segment
489 * (smaller than MSS bytes). We are calculating the usable now
490 * based on full mss and will save the rest of remaining data for
491 * later. When tcp_zero_win_probe is set, TCP needs to send out
492 * something to do zero window probe.
494 if (tcp
->tcp_cork
&& !tcp
->tcp_zero_win_probe
) {
497 usable
= (usable
/ mss
) * mss
;
500 /* Update the latest receive window size in TCP header. */
501 tcp
->tcp_tcpha
->tha_win
= htons(tcp
->tcp_rwnd
>> tcp
->tcp_rcv_ws
);
503 /* Send the packet. */
504 rc
= tcp_send(tcp
, mss
, total_hdr_len
, tcp_hdr_len
,
505 num_sack_blk
, &usable
, &snxt
, &tail_unsent
, &xmit_tail
,
508 /* Pretend that all we were trying to send really got sent */
509 if (rc
< 0 && tail_unsent
< 0) {
511 xmit_tail
= xmit_tail
->b_cont
;
512 xmit_tail
->b_prev
= local_time
;
513 ASSERT((uintptr_t)(xmit_tail
->b_wptr
-
514 xmit_tail
->b_rptr
) <= (uintptr_t)INT_MAX
);
515 tail_unsent
+= (int)(xmit_tail
->b_wptr
-
517 } while (tail_unsent
< 0);
520 tcp
->tcp_xmit_tail
= xmit_tail
;
521 tcp
->tcp_xmit_tail_unsent
= tail_unsent
;
522 len
= tcp
->tcp_snxt
- snxt
;
525 * If new data was sent, need to update the notsack
526 * list, which is, afterall, data blocks that have
527 * not been sack'ed by the receiver. New data is
530 if (tcp
->tcp_snd_sack_ok
&& tcp
->tcp_notsack_list
!= NULL
) {
531 /* len is a negative value. */
532 tcp
->tcp_pipe
-= len
;
533 tcp_notsack_update(&(tcp
->tcp_notsack_list
),
535 &(tcp
->tcp_num_notsack_blk
),
536 &(tcp
->tcp_cnt_notsack_list
));
538 tcp
->tcp_snxt
= snxt
+ tcp
->tcp_fin_sent
;
539 tcp
->tcp_rack
= tcp
->tcp_rnxt
;
540 tcp
->tcp_rack_cnt
= 0;
541 if ((snxt
+ len
) == tcp
->tcp_suna
) {
542 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
544 } else if (snxt
== tcp
->tcp_suna
&& tcp
->tcp_swnd
== 0) {
546 * Didn't send anything. Make sure the timer is running
547 * so that we will probe a zero window.
549 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
551 /* Note that len is the amount we just sent but with a negative sign */
552 tcp
->tcp_unsent
+= len
;
553 mutex_enter(&tcp
->tcp_non_sq_lock
);
554 if (tcp
->tcp_flow_stopped
) {
555 if (TCP_UNSENT_BYTES(tcp
) <= connp
->conn_sndlowat
) {
558 } else if (TCP_UNSENT_BYTES(tcp
) >= connp
->conn_sndbuf
) {
559 if (!(tcp
->tcp_detached
))
562 mutex_exit(&tcp
->tcp_non_sq_lock
);
566 * Initial STREAMS write side put() procedure for sockets. It tries to
567 * handle the T_CAPABILITY_REQ which sockfs sends down while setting
568 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages
569 * are handled by tcp_wput() as usual.
571 * All further messages will also be handled by tcp_wput() because we cannot
572 * be sure that the above short cut is safe later.
575 tcp_wput_sock(queue_t
*wq
, mblk_t
*mp
)
577 conn_t
*connp
= Q_TO_CONN(wq
);
578 tcp_t
*tcp
= connp
->conn_tcp
;
579 struct T_capability_req
*car
= (struct T_capability_req
*)mp
->b_rptr
;
581 ASSERT(wq
->q_qinfo
== &tcp_sock_winit
);
582 wq
->q_qinfo
= &tcp_winit
;
584 ASSERT(IPCL_IS_TCP(connp
));
585 ASSERT(TCP_IS_SOCKET(tcp
));
587 if (DB_TYPE(mp
) == M_PCPROTO
&&
588 MBLKL(mp
) == sizeof (struct T_capability_req
) &&
589 car
->PRIM_type
== T_CAPABILITY_REQ
) {
590 tcp_capability_req(tcp
, mp
);
599 tcp_wput_fallback(queue_t
*wq
, mblk_t
*mp
)
602 cmn_err(CE_CONT
, "tcp_wput_fallback: Message during fallback \n");
608 * Call by tcp_wput() to handle misc non M_DATA messages.
612 tcp_wput_nondata(void *arg
, mblk_t
*mp
, void *arg2
, ip_recv_attr_t
*dummy
)
614 conn_t
*connp
= (conn_t
*)arg
;
615 tcp_t
*tcp
= connp
->conn_tcp
;
617 ASSERT(DB_TYPE(mp
) != M_IOCTL
);
619 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close.
620 * Once the close starts, streamhead and sockfs will not let any data
621 * packets come down (close ensures that there are no threads using the
622 * queue and no new threads will come down) but since qprocsoff()
623 * hasn't happened yet, a M_FLUSH or some non data message might
624 * get reflected back (in response to our own FLUSHRW) and get
625 * processed after tcp_close() is done. The conn would still be valid
626 * because a ref would have added but we need to check the state
627 * before actually processing the packet.
629 if (TCP_IS_DETACHED(tcp
) || (tcp
->tcp_state
== TCPS_CLOSED
)) {
634 switch (DB_TYPE(mp
)) {
636 tcp_wput_iocdata(tcp
, mp
);
639 tcp_wput_flush(tcp
, mp
);
642 ip_wput_nondata(connp
->conn_wq
, mp
);
647 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */
649 tcp_wput_flush(tcp_t
*tcp
, mblk_t
*mp
)
651 uchar_t fval
= *mp
->b_rptr
;
653 conn_t
*connp
= tcp
->tcp_connp
;
654 queue_t
*q
= connp
->conn_wq
;
656 /* TODO: How should flush interact with urgent data? */
657 if ((fval
& FLUSHW
) && tcp
->tcp_xmit_head
!= NULL
&&
658 !(tcp
->tcp_valid_bits
& TCP_URG_VALID
)) {
660 * Flush only data that has not yet been put on the wire. If
661 * we flush data that we have already transmitted, life, as we
662 * know it, may come to an end.
664 tail
= tcp
->tcp_xmit_tail
;
665 tail
->b_wptr
-= tcp
->tcp_xmit_tail_unsent
;
666 tcp
->tcp_xmit_tail_unsent
= 0;
668 if (tail
->b_wptr
!= tail
->b_rptr
)
671 mblk_t
**excess
= &tcp
->tcp_xmit_head
;
673 mblk_t
*mp1
= *excess
;
676 tcp
->tcp_xmit_tail
= mp1
;
677 tcp
->tcp_xmit_last
= mp1
;
678 excess
= &mp1
->b_cont
;
681 tcp_close_mpp(&tail
);
682 if (tcp
->tcp_snd_zcopy_aware
)
683 tcp_zcopy_notify(tcp
);
686 * We have no unsent data, so unsent must be less than
687 * conn_sndlowat, so re-enable flow.
689 mutex_enter(&tcp
->tcp_non_sq_lock
);
690 if (tcp
->tcp_flow_stopped
) {
693 mutex_exit(&tcp
->tcp_non_sq_lock
);
696 * TODO: you can't just flush these, you have to increase rwnd for one
697 * thing. For another, how should urgent data interact?
700 *mp
->b_rptr
= fval
& ~FLUSHW
;
709 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA
713 tcp_wput_iocdata(tcp_t
*tcp
, mblk_t
*mp
)
716 struct iocblk
*iocp
= (struct iocblk
*)mp
->b_rptr
;
717 STRUCT_HANDLE(strbuf
, sb
);
719 conn_t
*connp
= tcp
->tcp_connp
;
720 queue_t
*q
= connp
->conn_wq
;
722 /* Make sure it is one of ours. */
723 switch (iocp
->ioc_cmd
) {
729 * If the conn is closing, then error the ioctl here. Otherwise
730 * use the CONN_IOCTLREF_* macros to hold off tcp_close until
733 mutex_enter(&connp
->conn_lock
);
734 if (connp
->conn_state_flags
& CONN_CLOSING
) {
735 mutex_exit(&connp
->conn_lock
);
736 iocp
->ioc_error
= EINVAL
;
737 mp
->b_datap
->db_type
= M_IOCNAK
;
743 CONN_INC_IOCTLREF_LOCKED(connp
);
744 ip_wput_nondata(q
, mp
);
745 CONN_DEC_IOCTLREF(connp
);
748 switch (mi_copy_state(q
, mp
, &mp1
)) {
751 case MI_COPY_CASE(MI_COPY_IN
, 1):
753 case MI_COPY_CASE(MI_COPY_OUT
, 1):
754 /* Copy out the strbuf. */
757 case MI_COPY_CASE(MI_COPY_OUT
, 2):
759 mi_copy_done(q
, mp
, 0);
762 mi_copy_done(q
, mp
, EPROTO
);
765 /* Check alignment of the strbuf */
766 if (!OK_32PTR(mp1
->b_rptr
)) {
767 mi_copy_done(q
, mp
, EINVAL
);
771 STRUCT_SET_HANDLE(sb
, iocp
->ioc_flag
, (void *)mp1
->b_rptr
);
773 if (connp
->conn_family
== AF_INET
)
774 addrlen
= sizeof (sin_t
);
776 addrlen
= sizeof (sin6_t
);
778 if (STRUCT_FGET(sb
, maxlen
) < addrlen
) {
779 mi_copy_done(q
, mp
, EINVAL
);
783 switch (iocp
->ioc_cmd
) {
787 if (tcp
->tcp_state
< TCPS_SYN_RCVD
) {
788 mi_copy_done(q
, mp
, ENOTCONN
);
793 mp1
= mi_copyout_alloc(q
, mp
, STRUCT_FGETP(sb
, buf
), addrlen
, B_TRUE
);
797 STRUCT_FSET(sb
, len
, addrlen
);
798 switch (((struct iocblk
*)mp
->b_rptr
)->ioc_cmd
) {
800 (void) conn_getsockname(connp
, (struct sockaddr
*)mp1
->b_wptr
,
804 (void) conn_getpeername(connp
, (struct sockaddr
*)mp1
->b_wptr
,
808 mp1
->b_wptr
+= addrlen
;
809 /* Copy out the address */
814 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL
819 tcp_wput_ioctl(void *arg
, mblk_t
*mp
, void *arg2
, ip_recv_attr_t
*dummy
)
821 conn_t
*connp
= (conn_t
*)arg
;
822 tcp_t
*tcp
= connp
->conn_tcp
;
823 queue_t
*q
= connp
->conn_wq
;
826 ASSERT(DB_TYPE(mp
) == M_IOCTL
);
828 * Try and ASSERT the minimum possible references on the
829 * conn early enough. Since we are executing on write side,
830 * the connection is obviously not detached and that means
831 * there is a ref each for TCP and IP. Since we are behind
832 * the squeue, the minimum references needed are 3. If the
833 * conn is in classifier hash list, there should be an
834 * extra ref for that (we check both the possibilities).
836 ASSERT((connp
->conn_fanout
!= NULL
&& connp
->conn_ref
>= 4) ||
837 (connp
->conn_fanout
== NULL
&& connp
->conn_ref
>= 3));
839 iocp
= (struct iocblk
*)mp
->b_rptr
;
840 switch (iocp
->ioc_cmd
) {
841 case _SIOCSOCKFALLBACK
:
843 * Either sockmod is about to be popped and the socket
844 * would now be treated as a plain stream, or a module
845 * is about to be pushed so we could no longer use read-
846 * side synchronous streams for fused loopback tcp.
847 * Drain any queued data and disable direct sockfs
848 * interface from now on.
850 if (!tcp
->tcp_issocket
) {
851 DB_TYPE(mp
) = M_IOCNAK
;
852 iocp
->ioc_error
= EINVAL
;
854 tcp_use_pure_tpi(tcp
);
855 DB_TYPE(mp
) = M_IOCACK
;
865 * If the conn is closing, then error the ioctl here. Otherwise bump the
866 * conn_ioctlref to hold off tcp_close until we're done here.
868 mutex_enter(&(connp
)->conn_lock
);
869 if ((connp
)->conn_state_flags
& CONN_CLOSING
) {
870 mutex_exit(&(connp
)->conn_lock
);
871 iocp
->ioc_error
= EINVAL
;
872 mp
->b_datap
->db_type
= M_IOCNAK
;
878 CONN_INC_IOCTLREF_LOCKED(connp
);
879 ip_wput_nondata(q
, mp
);
880 CONN_DEC_IOCTLREF(connp
);
884 * This routine is called by tcp_wput() to handle all TPI requests.
888 tcp_wput_proto(void *arg
, mblk_t
*mp
, void *arg2
, ip_recv_attr_t
*dummy
)
890 conn_t
*connp
= (conn_t
*)arg
;
891 tcp_t
*tcp
= connp
->conn_tcp
;
892 union T_primitives
*tprim
= (union T_primitives
*)mp
->b_rptr
;
898 * Try and ASSERT the minimum possible references on the
899 * conn early enough. Since we are executing on write side,
900 * the connection is obviously not detached and that means
901 * there is a ref each for TCP and IP. Since we are behind
902 * the squeue, the minimum references needed are 3. If the
903 * conn is in classifier hash list, there should be an
904 * extra ref for that (we check both the possibilities).
906 ASSERT((connp
->conn_fanout
!= NULL
&& connp
->conn_ref
>= 4) ||
907 (connp
->conn_fanout
== NULL
&& connp
->conn_ref
>= 3));
910 ASSERT((uintptr_t)(mp
->b_wptr
- rptr
) <= (uintptr_t)INT_MAX
);
911 if ((mp
->b_wptr
- rptr
) >= sizeof (t_scalar_t
)) {
912 type
= ((union T_primitives
*)rptr
)->type
;
913 if (type
== T_EXDATA_REQ
) {
914 tcp_output_urgent(connp
, mp
, arg2
, NULL
);
915 } else if (type
!= T_DATA_REQ
) {
916 goto non_urgent_data
;
918 /* TODO: options, flags, ... from user */
919 /* Set length to zero for reclamation below */
920 tcp_wput_data(tcp
, mp
->b_cont
, B_TRUE
);
925 if (connp
->conn_debug
) {
926 (void) strlog(TCP_MOD_ID
, 0, 1, SL_ERROR
|SL_TRACE
,
927 "tcp_wput_proto, dropping one...");
935 switch ((int)tprim
->type
) {
936 case O_T_BIND_REQ
: /* bind request */
937 case T_BIND_REQ
: /* new semantics bind request */
938 tcp_tpi_bind(tcp
, mp
);
940 case T_UNBIND_REQ
: /* unbind request */
941 tcp_tpi_unbind(tcp
, mp
);
943 case O_T_CONN_RES
: /* old connection response XXX */
944 case T_CONN_RES
: /* connection response */
945 tcp_tli_accept(tcp
, mp
);
947 case T_CONN_REQ
: /* connection request */
948 tcp_tpi_connect(tcp
, mp
);
950 case T_DISCON_REQ
: /* disconnect request */
951 tcp_disconnect(tcp
, mp
);
953 case T_CAPABILITY_REQ
:
954 tcp_capability_req(tcp
, mp
); /* capability request */
956 case T_INFO_REQ
: /* information request */
957 tcp_info_req(tcp
, mp
);
959 case T_SVR4_OPTMGMT_REQ
: /* manage options req */
962 * Note: no support for snmpcom_req() through new
963 * T_OPTMGMT_REQ. See comments in ip.c
967 * All Solaris components should pass a db_credp
968 * for this TPI message, hence we ASSERT.
969 * But in case there is some other M_PROTO that looks
970 * like a TPI message sent by some other kernel
971 * component, we check and return an error.
973 cr
= msg_getcred(mp
, NULL
);
976 tcp_err_ack(tcp
, mp
, TSYSERR
, EINVAL
);
980 * If EINPROGRESS is returned, the request has been queued
981 * for subsequent processing by ip_restart_optmgmt(), which
982 * will do the CONN_DEC_REF().
984 if ((int)tprim
->type
== T_SVR4_OPTMGMT_REQ
) {
985 svr4_optcom_req(connp
->conn_wq
, mp
, cr
, &tcp_opt_obj
);
987 tpi_optcom_req(connp
->conn_wq
, mp
, cr
, &tcp_opt_obj
);
991 case T_UNITDATA_REQ
: /* unitdata request */
992 tcp_err_ack(tcp
, mp
, TNOTSUPPORT
, 0);
994 case T_ORDREL_REQ
: /* orderly release req */
1000 if (tcp_xmit_end(tcp
) != 0) {
1002 * We were crossing FINs and got a reset from
1003 * the other side. Just ignore it.
1005 if (connp
->conn_debug
) {
1006 (void) strlog(TCP_MOD_ID
, 0, 1,
1008 "tcp_wput_proto, T_ORDREL_REQ out of "
1010 tcp_display(tcp
, NULL
,
1011 DISP_ADDR_AND_PORT
));
1016 tcp_addr_req(tcp
, mp
);
1019 if (connp
->conn_debug
) {
1020 (void) strlog(TCP_MOD_ID
, 0, 1, SL_ERROR
|SL_TRACE
,
1021 "tcp_wput_proto, bogus TPI msg, type %d",
1025 * We used to M_ERROR. Sending TNOTSUPPORT gives the user
1028 tcp_err_ack(tcp
, mp
, TNOTSUPPORT
, 0);
1034 * Handle special out-of-band ioctl requests (see PSARC/2008/265).
1037 tcp_wput_cmdblk(queue_t
*q
, mblk_t
*mp
)
1040 mblk_t
*datamp
= mp
->b_cont
;
1041 conn_t
*connp
= Q_TO_CONN(q
);
1042 tcp_t
*tcp
= connp
->conn_tcp
;
1043 cmdblk_t
*cmdp
= (cmdblk_t
*)mp
->b_rptr
;
1045 if (datamp
== NULL
|| MBLKL(datamp
) < cmdp
->cb_len
) {
1046 cmdp
->cb_error
= EPROTO
;
1051 data
= datamp
->b_rptr
;
1053 switch (cmdp
->cb_cmd
) {
1054 case TI_GETPEERNAME
:
1055 if (tcp
->tcp_state
< TCPS_SYN_RCVD
)
1056 cmdp
->cb_error
= ENOTCONN
;
1058 cmdp
->cb_error
= conn_getpeername(connp
, data
,
1062 cmdp
->cb_error
= conn_getsockname(connp
, data
, &cmdp
->cb_len
);
1065 cmdp
->cb_error
= EINVAL
;
1073 * The TCP fast path write put procedure.
1074 * NOTE: the logic of the fast path is duplicated from tcp_wput_data()
1078 tcp_output(void *arg
, mblk_t
*mp
, void *arg2
, ip_recv_attr_t
*dummy
)
1094 conn_t
*connp
= (conn_t
*)arg
;
1095 tcp_t
*tcp
= connp
->conn_tcp
;
1097 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
1098 ip_xmit_attr_t
*ixa
;
1102 * Try and ASSERT the minimum possible references on the
1103 * conn early enough. Since we are executing on write side,
1104 * the connection is obviously not detached and that means
1105 * there is a ref each for TCP and IP. Since we are behind
1106 * the squeue, the minimum references needed are 3. If the
1107 * conn is in classifier hash list, there should be an
1108 * extra ref for that (we check both the possibilities).
1110 ASSERT((connp
->conn_fanout
!= NULL
&& connp
->conn_ref
>= 4) ||
1111 (connp
->conn_fanout
== NULL
&& connp
->conn_ref
>= 3));
1113 ASSERT(DB_TYPE(mp
) == M_DATA
);
1114 msize
= (mp
->b_cont
== NULL
) ? MBLKL(mp
) : msgdsize(mp
);
1116 mutex_enter(&tcp
->tcp_non_sq_lock
);
1117 tcp
->tcp_squeue_bytes
-= msize
;
1118 mutex_exit(&tcp
->tcp_non_sq_lock
);
1120 /* Bypass tcp protocol for fused tcp loopback */
1121 if (tcp
->tcp_fused
&& tcp_fuse_output(tcp
, mp
, msize
))
1126 * If ZEROCOPY has turned off, try not to send any zero-copy message
1127 * down. Do backoff, now.
1129 if (tcp
->tcp_snd_zcopy_aware
&& !tcp
->tcp_snd_zcopy_on
)
1130 mp
= tcp_zcopy_backoff(tcp
, mp
, B_FALSE
);
1133 ASSERT((uintptr_t)(mp
->b_wptr
- mp
->b_rptr
) <= (uintptr_t)INT_MAX
);
1134 len
= (int)(mp
->b_wptr
- mp
->b_rptr
);
1137 * Criteria for fast path:
1140 * 2. single mblk in request
1141 * 3. connection established
1144 * 6. no tcp_valid bits
1146 if ((tcp
->tcp_unsent
!= 0) ||
1148 (mp
->b_cont
!= NULL
) ||
1149 (tcp
->tcp_state
!= TCPS_ESTABLISHED
) ||
1152 (tcp
->tcp_valid_bits
!= 0)) {
1153 tcp_wput_data(tcp
, mp
, B_FALSE
);
1157 ASSERT(tcp
->tcp_xmit_tail_unsent
== 0);
1158 ASSERT(tcp
->tcp_fin_sent
== 0);
1160 /* queue new packet onto retransmission queue */
1161 if (tcp
->tcp_xmit_head
== NULL
) {
1162 tcp
->tcp_xmit_head
= mp
;
1164 tcp
->tcp_xmit_last
->b_cont
= mp
;
1166 tcp
->tcp_xmit_last
= mp
;
1167 tcp
->tcp_xmit_tail
= mp
;
1169 /* find out how much we can send */
1173 * |--------------|-----------------|
1174 * tcp_suna tcp_snxt tcp_suna+tcp_swnd
1178 /* start sending from tcp_snxt */
1179 snxt
= tcp
->tcp_snxt
;
1182 * Check to see if this connection has been idled for some
1183 * time and no ACK is expected. If it is, we need to slow
1184 * start again to get back the connection's "self-clock" as
1185 * described in VJ's paper.
1187 * Reinitialize tcp_cwnd after idle.
1189 now
= LBOLT_FASTPATH
;
1190 if ((tcp
->tcp_suna
== snxt
) && !tcp
->tcp_localnet
&&
1191 (TICK_TO_MSEC(now
- tcp
->tcp_last_recv_time
) >= tcp
->tcp_rto
)) {
1192 TCP_SET_INIT_CWND(tcp
, mss
, tcps
->tcps_slow_start_after_idle
);
1195 usable
= tcp
->tcp_swnd
; /* tcp window size */
1196 if (usable
> tcp
->tcp_cwnd
)
1197 usable
= tcp
->tcp_cwnd
; /* congestion window smaller */
1198 usable
-= snxt
; /* subtract stuff already sent */
1199 suna
= tcp
->tcp_suna
;
1201 /* usable can be < 0 if the congestion window is smaller */
1203 /* Can't send complete M_DATA in one shot */
1207 mutex_enter(&tcp
->tcp_non_sq_lock
);
1208 if (tcp
->tcp_flow_stopped
&&
1209 TCP_UNSENT_BYTES(tcp
) <= connp
->conn_sndlowat
) {
1212 mutex_exit(&tcp
->tcp_non_sq_lock
);
1215 * determine if anything to send (Nagle).
1217 * 1. len < tcp_mss (i.e. small)
1218 * 2. unacknowledged data present
1219 * 3. len < nagle limit
1220 * 4. last packet sent < nagle limit (previous packet sent)
1222 if ((len
< mss
) && (snxt
!= suna
) &&
1223 (len
< (int)tcp
->tcp_naglim
) &&
1224 (tcp
->tcp_last_sent_len
< tcp
->tcp_naglim
)) {
1226 * This was the first unsent packet and normally
1227 * mss < xmit_hiwater so there is no need to worry
1228 * about flow control. The next packet will go
1229 * through the flow control check in tcp_wput_data().
1231 /* leftover work from above */
1232 tcp
->tcp_unsent
= len
;
1233 tcp
->tcp_xmit_tail_unsent
= len
;
1239 * len <= tcp->tcp_mss && len == unsent so no sender silly window. Can
1244 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
1247 /* we have always sent something */
1248 tcp
->tcp_rack_cnt
= 0;
1250 tcp
->tcp_snxt
= snxt
+ len
;
1251 tcp
->tcp_rack
= tcp
->tcp_rnxt
;
1253 if ((mp1
= dupb(mp
)) == 0)
1255 mp
->b_prev
= (mblk_t
*)(uintptr_t)now
;
1256 mp
->b_next
= (mblk_t
*)(uintptr_t)snxt
;
1258 /* adjust tcp header information */
1259 tcpha
= tcp
->tcp_tcpha
;
1260 tcpha
->tha_flags
= (TH_ACK
|TH_PUSH
);
1262 sum
= len
+ connp
->conn_ht_ulp_len
+ connp
->conn_sum
;
1263 sum
= (sum
>> 16) + (sum
& 0xFFFF);
1264 tcpha
->tha_sum
= htons(sum
);
1266 tcpha
->tha_seq
= htonl(snxt
);
1268 TCPS_BUMP_MIB(tcps
, tcpOutDataSegs
);
1269 TCPS_UPDATE_MIB(tcps
, tcpOutDataBytes
, len
);
1270 BUMP_LOCAL(tcp
->tcp_obsegs
);
1272 /* Update the latest receive window size in TCP header. */
1273 tcpha
->tha_win
= htons(tcp
->tcp_rwnd
>> tcp
->tcp_rcv_ws
);
1275 tcp
->tcp_last_sent_len
= (ushort_t
)len
;
1277 plen
= len
+ connp
->conn_ht_iphc_len
;
1279 ixa
= connp
->conn_ixa
;
1280 ixa
->ixa_pktlen
= plen
;
1282 if (ixa
->ixa_flags
& IXAF_IS_IPV4
) {
1283 tcp
->tcp_ipha
->ipha_length
= htons(plen
);
1285 tcp
->tcp_ip6h
->ip6_plen
= htons(plen
- IPV6_HDR_LEN
);
1288 /* see if we need to allocate a mblk for the headers */
1289 hdrlen
= connp
->conn_ht_iphc_len
;
1290 rptr
= mp1
->b_rptr
- hdrlen
;
1292 if ((db
->db_ref
!= 2) || rptr
< db
->db_base
||
1293 (!OK_32PTR(rptr
))) {
1294 /* NOTE: we assume allocb returns an OK_32PTR */
1295 mp
= allocb(hdrlen
+ tcps
->tcps_wroff_xtra
, BPRI_MED
);
1302 /* Leave room for Link Level header */
1303 rptr
= &mp1
->b_rptr
[tcps
->tcps_wroff_xtra
];
1304 mp1
->b_wptr
= &rptr
[hdrlen
];
1308 /* Fill in the timestamp option. */
1309 if (tcp
->tcp_snd_ts_ok
) {
1310 uint32_t llbolt
= (uint32_t)LBOLT_FASTPATH
;
1313 (char *)tcpha
+ TCP_MIN_HEADER_LENGTH
+4);
1314 U32_TO_BE32(tcp
->tcp_ts_recent
,
1315 (char *)tcpha
+ TCP_MIN_HEADER_LENGTH
+8);
1317 ASSERT(connp
->conn_ht_ulp_len
== TCP_MIN_HEADER_LENGTH
);
1320 /* copy header into outgoing packet */
1321 dst
= (ipaddr_t
*)rptr
;
1322 src
= (ipaddr_t
*)connp
->conn_ht_iphc
;
1343 * Set the ECN info in the TCP header. Note that this
1344 * is not the template header.
1346 if (tcp
->tcp_ecn_ok
) {
1347 TCP_SET_ECT(tcp
, rptr
);
1349 tcpha
= (tcpha_t
*)(rptr
+ ixa
->ixa_ip_hdr_length
);
1350 if (tcp
->tcp_ecn_echo_on
)
1351 tcpha
->tha_flags
|= TH_ECE
;
1352 if (tcp
->tcp_cwr
&& !tcp
->tcp_ecn_cwr_sent
) {
1353 tcpha
->tha_flags
|= TH_CWR
;
1354 tcp
->tcp_ecn_cwr_sent
= B_TRUE
;
1358 if (tcp
->tcp_ip_forward_progress
) {
1359 tcp
->tcp_ip_forward_progress
= B_FALSE
;
1360 connp
->conn_ixa
->ixa_flags
|= IXAF_REACH_CONF
;
1362 connp
->conn_ixa
->ixa_flags
&= ~IXAF_REACH_CONF
;
1364 tcp_send_data(tcp
, mp1
);
1368 * If we ran out of memory, we pretend to have sent the packet
1369 * and that it was lost on the wire.
1375 /* leftover work from above */
1376 tcp
->tcp_unsent
= len
;
1377 tcp
->tcp_xmit_tail_unsent
= len
;
1378 tcp_wput_data(tcp
, NULL
, B_FALSE
);
1383 tcp_output_urgent(void *arg
, mblk_t
*mp
, void *arg2
, ip_recv_attr_t
*dummy
)
1387 conn_t
*connp
= (conn_t
*)arg
;
1388 tcp_t
*tcp
= connp
->conn_tcp
;
1390 msize
= msgdsize(mp
);
1399 * Try to force urgent data out on the wire. Even if we have unsent
1400 * data this will at least send the urgent flag.
1401 * XXX does not handle more flag correctly.
1403 len
+= tcp
->tcp_unsent
;
1404 len
+= tcp
->tcp_snxt
;
1406 tcp
->tcp_valid_bits
|= TCP_URG_VALID
;
1408 /* Bypass tcp protocol for fused tcp loopback */
1409 if (tcp
->tcp_fused
&& tcp_fuse_output(tcp
, mp
, msize
))
1412 /* Strip off the T_EXDATA_REQ if the data is from TPI */
1413 if (DB_TYPE(mp
) != M_DATA
) {
1415 ASSERT(!IPCL_IS_NONSTR(connp
));
1419 tcp_wput_data(tcp
, mp
, B_TRUE
);
1423 * Called by streams close routine via squeues when our client blows off its
1424 * descriptor, we take this to mean: "close the stream state NOW, close the tcp
1425 * connection politely" When SO_LINGER is set (with a non-zero linger time and
1426 * it is not a nonblocking socket) then this routine sleeps until the FIN is
1429 * NOTE: tcp_close potentially returns error when lingering.
1430 * However, the stream head currently does not pass these errors
1431 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK
1432 * errors to the application (from tsleep()) and not errors
1433 * like ECONNRESET caused by receiving a reset packet.
1438 tcp_close_output(void *arg
, mblk_t
*mp
, void *arg2
, ip_recv_attr_t
*dummy
)
1441 conn_t
*connp
= (conn_t
*)arg
;
1442 tcp_t
*tcp
= connp
->conn_tcp
;
1444 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
1447 * When a non-STREAMS socket is being closed, it does not always
1448 * stick around waiting for tcp_close_output to run and can therefore
1449 * have dropped a reference already. So adjust the asserts accordingly.
1451 ASSERT((connp
->conn_fanout
!= NULL
&&
1452 connp
->conn_ref
>= (IPCL_IS_NONSTR(connp
) ? 3 : 4)) ||
1453 (connp
->conn_fanout
== NULL
&&
1454 connp
->conn_ref
>= (IPCL_IS_NONSTR(connp
) ? 2 : 3)));
1456 mutex_enter(&tcp
->tcp_eager_lock
);
1457 if (tcp
->tcp_conn_req_cnt_q0
!= 0 || tcp
->tcp_conn_req_cnt_q
!= 0) {
1459 * Cleanup for listener. For non-STREAM sockets sockfs will
1460 * close all the eagers on 'q', so in that case only deal
1463 tcp_eager_cleanup(tcp
, IPCL_IS_NONSTR(connp
) ? 1 : 0);
1464 tcp
->tcp_wait_for_eagers
= 1;
1466 mutex_exit(&tcp
->tcp_eager_lock
);
1468 tcp
->tcp_lso
= B_FALSE
;
1471 switch (tcp
->tcp_state
) {
1476 if (tcp
->tcp_listener
!= NULL
) {
1477 ASSERT(IPCL_IS_NONSTR(connp
));
1479 * Unlink from the listener and drop the reference
1480 * put on it by the eager. tcp_closei_local will not
1481 * do it because tcp_tconnind_started is TRUE.
1483 mutex_enter(&tcp
->tcp_saved_listener
->tcp_eager_lock
);
1484 tcp_eager_unlink(tcp
);
1485 mutex_exit(&tcp
->tcp_saved_listener
->tcp_eager_lock
);
1486 CONN_DEC_REF(tcp
->tcp_saved_listener
->tcp_connp
);
1492 msg
= "tcp_close, during connect";
1496 * Close during the connect 3-way handshake
1497 * but here there may or may not be pending data
1498 * already on queue. Process almost same as in
1499 * the ESTABLISHED state.
1507 * If SO_LINGER has set a zero linger time, abort the
1508 * connection with a reset.
1510 if (connp
->conn_linger
&& connp
->conn_lingertime
== 0) {
1511 msg
= "tcp_close, zero lingertime";
1516 * Abort connection if there is unread data queued.
1518 if (tcp
->tcp_rcv_list
|| tcp
->tcp_reass_head
) {
1519 msg
= "tcp_close, unread data";
1524 * Abort connection if it is being closed without first
1525 * being accepted. This can happen if a listening non-STREAM
1526 * socket wants to get rid of the socket, for example, if the
1527 * listener is closing.
1529 if (tcp
->tcp_listener
!= NULL
) {
1530 ASSERT(IPCL_IS_NONSTR(connp
));
1531 msg
= "tcp_close, close before accept";
1534 * Unlink from the listener and drop the reference
1535 * put on it by the eager. tcp_closei_local will not
1536 * do it because tcp_tconnind_started is TRUE.
1538 mutex_enter(&tcp
->tcp_saved_listener
->tcp_eager_lock
);
1539 tcp_eager_unlink(tcp
);
1540 mutex_exit(&tcp
->tcp_saved_listener
->tcp_eager_lock
);
1541 CONN_DEC_REF(tcp
->tcp_saved_listener
->tcp_connp
);
1546 * Transmit the FIN before detaching the tcp_t.
1547 * After tcp_detach returns this queue/perimeter
1548 * no longer owns the tcp_t thus others can modify it.
1550 (void) tcp_xmit_end(tcp
);
1553 * If lingering on close then wait until the fin is acked,
1554 * the SO_LINGER time passes, or a reset is sent/received.
1556 if (connp
->conn_linger
&& connp
->conn_lingertime
> 0 &&
1557 !(tcp
->tcp_fin_acked
) &&
1558 tcp
->tcp_state
>= TCPS_ESTABLISHED
) {
1559 if (tcp
->tcp_closeflags
& (FNDELAY
|FNONBLOCK
)) {
1560 tcp
->tcp_client_errno
= EWOULDBLOCK
;
1561 } else if (tcp
->tcp_client_errno
== 0) {
1563 ASSERT(tcp
->tcp_linger_tid
== 0);
1565 /* conn_lingertime is in sec. */
1566 tcp
->tcp_linger_tid
= TCP_TIMER(tcp
,
1567 tcp_close_linger_timeout
,
1568 connp
->conn_lingertime
* MILLISEC
);
1570 /* tcp_close_linger_timeout will finish close */
1571 if (tcp
->tcp_linger_tid
== 0)
1572 tcp
->tcp_client_errno
= ENOSR
;
1578 * Check if we need to detach or just close
1581 if (tcp
->tcp_state
<= TCPS_LISTEN
)
1586 * Make sure that no other thread will access the conn_rq of
1587 * this instance (through lookups etc.) as conn_rq will go
1590 tcp_acceptor_hash_remove(tcp
);
1592 mutex_enter(&tcp
->tcp_non_sq_lock
);
1593 if (tcp
->tcp_flow_stopped
) {
1596 mutex_exit(&tcp
->tcp_non_sq_lock
);
1598 if (tcp
->tcp_timer_tid
!= 0) {
1599 delta
= TCP_TIMER_CANCEL(tcp
, tcp
->tcp_timer_tid
);
1600 tcp
->tcp_timer_tid
= 0;
1603 * Need to cancel those timers which will not be used when
1604 * TCP is detached. This has to be done before the conn_wq
1607 tcp_timers_stop(tcp
);
1609 tcp
->tcp_detached
= B_TRUE
;
1610 if (tcp
->tcp_state
== TCPS_TIME_WAIT
) {
1611 tcp_time_wait_append(tcp
);
1612 TCP_DBGSTAT(tcps
, tcp_detach_time_wait
);
1613 ASSERT(connp
->conn_ref
>=
1614 (IPCL_IS_NONSTR(connp
) ? 2 : 3));
1619 * If delta is zero the timer event wasn't executed and was
1620 * successfully canceled. In this case we need to restart it
1621 * with the minimal delta possible.
1624 tcp
->tcp_timer_tid
= TCP_TIMER(tcp
, tcp_timer
,
1627 ASSERT(connp
->conn_ref
>= (IPCL_IS_NONSTR(connp
) ? 2 : 3));
1631 /* Detach did not complete. Still need to remove q from stream. */
1633 if (tcp
->tcp_state
== TCPS_ESTABLISHED
||
1634 tcp
->tcp_state
== TCPS_CLOSE_WAIT
)
1635 TCPS_BUMP_MIB(tcps
, tcpEstabResets
);
1636 if (tcp
->tcp_state
== TCPS_SYN_SENT
||
1637 tcp
->tcp_state
== TCPS_SYN_RCVD
)
1638 TCPS_BUMP_MIB(tcps
, tcpAttemptFails
);
1639 tcp_xmit_ctl(msg
, tcp
, tcp
->tcp_snxt
, 0, TH_RST
);
1642 tcp_closei_local(tcp
);
1643 CONN_DEC_REF(connp
);
1644 ASSERT(connp
->conn_ref
>= (IPCL_IS_NONSTR(connp
) ? 1 : 2));
1648 * Don't change the queues in the case of a listener that has
1649 * eagers in its q or q0. It could surprise the eagers.
1650 * Instead wait for the eagers outside the squeue.
1652 * For non-STREAMS sockets tcp_wait_for_eagers implies that
1653 * we should delay the su_closed upcall until all eagers have
1654 * dropped their references.
1656 if (!tcp
->tcp_wait_for_eagers
) {
1657 tcp
->tcp_detached
= B_TRUE
;
1658 connp
->conn_rq
= NULL
;
1659 connp
->conn_wq
= NULL
;
1661 /* non-STREAM socket, release the upper handle */
1662 if (IPCL_IS_NONSTR(connp
)) {
1663 ASSERT(connp
->conn_upper_handle
!= NULL
);
1664 (*connp
->conn_upcalls
->su_closed
)
1665 (connp
->conn_upper_handle
);
1666 connp
->conn_upper_handle
= NULL
;
1667 connp
->conn_upcalls
= NULL
;
1671 /* Signal tcp_close() to finish closing. */
1672 mutex_enter(&tcp
->tcp_closelock
);
1673 tcp
->tcp_closed
= 1;
1674 cv_signal(&tcp
->tcp_closecv
);
1675 mutex_exit(&tcp
->tcp_closelock
);
1680 tcp_shutdown_output(void *arg
, mblk_t
*mp
, void *arg2
, ip_recv_attr_t
*dummy
)
1682 conn_t
*connp
= (conn_t
*)arg
;
1683 tcp_t
*tcp
= connp
->conn_tcp
;
1690 if (tcp_xmit_end(tcp
) != 0) {
1692 * We were crossing FINs and got a reset from
1693 * the other side. Just ignore it.
1695 if (connp
->conn_debug
) {
1696 (void) strlog(TCP_MOD_ID
, 0, 1,
1698 "tcp_shutdown_output() out of state %s",
1699 tcp_display(tcp
, NULL
, DISP_ADDR_AND_PORT
));
1704 #pragma inline(tcp_send_data)
1707 tcp_send_data(tcp_t
*tcp
, mblk_t
*mp
)
1709 conn_t
*connp
= tcp
->tcp_connp
;
1712 * Check here to avoid sending zero-copy message down to IP when
1713 * ZEROCOPY capability has turned off. We only need to deal with
1714 * the race condition between sockfs and the notification here.
1715 * Since we have tried to backoff the tcp_xmit_head when turning
1716 * zero-copy off and new messages in tcp_output(), we simply drop
1717 * the dup'ed packet here and let tcp retransmit, if tcp_xmit_zc_clean
1720 if (tcp
->tcp_snd_zcopy_aware
&& !tcp
->tcp_snd_zcopy_on
&&
1721 !tcp
->tcp_xmit_zc_clean
) {
1722 ip_drop_output("TCP ZC was disabled but not clean", mp
, NULL
);
1727 DTRACE_TCP5(send
, mblk_t
*, NULL
, ip_xmit_attr_t
*, connp
->conn_ixa
,
1728 __dtrace_tcp_void_ip_t
*, mp
->b_rptr
, tcp_t
*, tcp
,
1729 __dtrace_tcp_tcph_t
*,
1730 &mp
->b_rptr
[connp
->conn_ixa
->ixa_ip_hdr_length
]);
1732 ASSERT(connp
->conn_ixa
->ixa_notify_cookie
== connp
->conn_tcp
);
1733 (void) conn_ip_output(mp
, connp
->conn_ixa
);
1738 tcp_send_synack(void *arg
, mblk_t
*mp
, void *arg2
, ip_recv_attr_t
*dummy
)
1740 conn_t
*econnp
= (conn_t
*)arg
;
1741 tcp_t
*tcp
= econnp
->conn_tcp
;
1742 ip_xmit_attr_t
*ixa
= econnp
->conn_ixa
;
1744 /* Guard against a RST having blown it away while on the squeue */
1745 if (tcp
->tcp_state
== TCPS_CLOSED
) {
1751 * In the off-chance that the eager received and responded to
1752 * some other packet while the SYN|ACK was queued, we recalculate
1753 * the ixa_pktlen. It would be better to fix the SYN/accept
1754 * multithreading scheme to avoid this complexity.
1756 ixa
->ixa_pktlen
= msgdsize(mp
);
1757 (void) conn_ip_output(mp
, ixa
);
1761 * tcp_send() is called by tcp_wput_data() and returns one of the following:
1763 * -1 = failed allocation.
1764 * 0 = We've either successfully sent data, or our usable send window is too
1765 * small and we'd rather wait until later before sending again.
1768 tcp_send(tcp_t
*tcp
, const int mss
, const int total_hdr_len
,
1769 const int tcp_hdr_len
, const int num_sack_blk
, int *usable
,
1770 uint_t
*snxt
, int *tail_unsent
, mblk_t
**xmit_tail
, mblk_t
*local_time
)
1772 int num_lso_seg
= 1;
1774 boolean_t do_lso_send
= B_FALSE
;
1775 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
1776 conn_t
*connp
= tcp
->tcp_connp
;
1777 ip_xmit_attr_t
*ixa
= connp
->conn_ixa
;
1780 * Check LSO possibility. The value of tcp->tcp_lso indicates whether
1781 * the underlying connection is LSO capable. Will check whether having
1782 * enough available data to initiate LSO transmission in the for(){}
1785 if (tcp
->tcp_lso
&& (tcp
->tcp_valid_bits
& ~TCP_FSS_VALID
) == 0)
1786 do_lso_send
= B_TRUE
;
1797 * Calculate the maximum payload length we can send at one
1802 * Determine whether or not it's possible to do LSO,
1803 * and if so, how much data we can send.
1805 if ((*usable
- 1) / mss
>= 1) {
1806 lso_usable
= MIN(tcp
->tcp_lso_max
, *usable
);
1807 num_lso_seg
= lso_usable
/ mss
;
1808 if (lso_usable
% mss
) {
1810 tcp
->tcp_last_sent_len
= (ushort_t
)
1813 tcp
->tcp_last_sent_len
= (ushort_t
)mss
;
1816 do_lso_send
= B_FALSE
;
1822 ASSERT(num_lso_seg
<= IP_MAXPACKET
/ mss
+ 1);
1825 if (len
> *usable
) {
1826 ASSERT(do_lso_send
== B_FALSE
);
1830 /* Terminate the loop */
1831 break; /* success; too small */
1834 * Sender silly-window avoidance.
1835 * Ignore this if we are going to send a
1836 * zero window probe out.
1838 * TODO: force data into microscopic window?
1839 * ==> (!pushed || (unsent > usable))
1841 if (len
< (tcp
->tcp_max_swnd
>> 1) &&
1842 (tcp
->tcp_unsent
- (*snxt
- tcp
->tcp_snxt
)) > len
&&
1843 !((tcp
->tcp_valid_bits
& TCP_URG_VALID
) &&
1844 len
== 1) && (! tcp
->tcp_zero_win_probe
)) {
1846 * If the retransmit timer is not running
1847 * we start it so that we will retransmit
1848 * in the case when the receiver has
1849 * decremented the window.
1851 if (*snxt
== tcp
->tcp_snxt
&&
1852 *snxt
== tcp
->tcp_suna
) {
1854 * We are not supposed to send
1855 * anything. So let's wait a little
1856 * bit longer before breaking SWS
1859 * What should the value be?
1860 * Suggestion: MAX(init rexmit time,
1863 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
1865 break; /* success; too small */
1869 tcpha
= tcp
->tcp_tcpha
;
1872 * The reason to adjust len here is that we need to set flags
1873 * and calculate checksum.
1878 *usable
-= len
; /* Approximate - can be adjusted later */
1880 tcpha
->tha_flags
= TH_ACK
;
1882 tcpha
->tha_flags
= (TH_ACK
| TH_PUSH
);
1885 * Prime pump for IP's checksumming on our behalf.
1886 * Include the adjustment for a source route if any.
1887 * In case of LSO, the partial pseudo-header checksum should
1888 * exclusive TCP length, so zero tha_sum before IP calculate
1889 * pseudo-header checksum for partial checksum offload.
1894 sum
= len
+ tcp_hdr_len
+ connp
->conn_sum
;
1895 sum
= (sum
>> 16) + (sum
& 0xFFFF);
1897 tcpha
->tha_sum
= htons(sum
);
1898 tcpha
->tha_seq
= htonl(*snxt
);
1901 * Branch off to tcp_xmit_mp() if any of the VALID bits is
1902 * set. For the case when TCP_FSS_VALID is the only valid
1903 * bit (normal active close), branch off only when we think
1904 * that the FIN flag needs to be set. Note for this case,
1905 * that (snxt + len) may not reflect the actual seg_len,
1906 * as len may be further reduced in tcp_xmit_mp(). If len
1907 * gets modified, we will end up here again.
1909 if (tcp
->tcp_valid_bits
!= 0 &&
1910 (tcp
->tcp_valid_bits
!= TCP_FSS_VALID
||
1911 ((*snxt
+ len
) == tcp
->tcp_fss
))) {
1913 uint32_t prev_snxt
= tcp
->tcp_snxt
;
1915 if (*tail_unsent
== 0) {
1916 ASSERT((*xmit_tail
)->b_cont
!= NULL
);
1917 *xmit_tail
= (*xmit_tail
)->b_cont
;
1918 prev_rptr
= (*xmit_tail
)->b_rptr
;
1919 *tail_unsent
= (int)((*xmit_tail
)->b_wptr
-
1920 (*xmit_tail
)->b_rptr
);
1922 prev_rptr
= (*xmit_tail
)->b_rptr
;
1923 (*xmit_tail
)->b_rptr
= (*xmit_tail
)->b_wptr
-
1926 mp
= tcp_xmit_mp(tcp
, *xmit_tail
, len
, NULL
, NULL
,
1927 *snxt
, B_FALSE
, (uint32_t *)&len
, B_FALSE
);
1928 /* Restore tcp_snxt so we get amount sent right. */
1929 tcp
->tcp_snxt
= prev_snxt
;
1930 if (prev_rptr
== (*xmit_tail
)->b_rptr
) {
1932 * If the previous timestamp is still in use,
1933 * don't stomp on it.
1935 if ((*xmit_tail
)->b_next
== NULL
) {
1936 (*xmit_tail
)->b_prev
= local_time
;
1937 (*xmit_tail
)->b_next
=
1938 (mblk_t
*)(uintptr_t)(*snxt
);
1941 (*xmit_tail
)->b_rptr
= prev_rptr
;
1948 if (len
<= mss
) /* LSO is unusable (!do_lso_send) */
1949 tcp
->tcp_last_sent_len
= (ushort_t
)len
;
1950 while (mp1
->b_cont
) {
1951 *xmit_tail
= (*xmit_tail
)->b_cont
;
1952 (*xmit_tail
)->b_prev
= local_time
;
1953 (*xmit_tail
)->b_next
=
1954 (mblk_t
*)(uintptr_t)(*snxt
);
1958 *tail_unsent
= (*xmit_tail
)->b_wptr
- mp1
->b_wptr
;
1959 BUMP_LOCAL(tcp
->tcp_obsegs
);
1960 TCPS_BUMP_MIB(tcps
, tcpOutDataSegs
);
1961 TCPS_UPDATE_MIB(tcps
, tcpOutDataBytes
, len
);
1962 tcp_send_data(tcp
, mp
);
1966 *snxt
+= len
; /* Adjust later if we don't send all of len */
1967 TCPS_BUMP_MIB(tcps
, tcpOutDataSegs
);
1968 TCPS_UPDATE_MIB(tcps
, tcpOutDataBytes
, len
);
1971 /* Are the bytes above us in flight? */
1972 rptr
= (*xmit_tail
)->b_wptr
- *tail_unsent
;
1973 if (rptr
!= (*xmit_tail
)->b_rptr
) {
1974 *tail_unsent
-= len
;
1975 if (len
<= mss
) /* LSO is unusable */
1976 tcp
->tcp_last_sent_len
= (ushort_t
)len
;
1977 len
+= total_hdr_len
;
1978 ixa
->ixa_pktlen
= len
;
1980 if (ixa
->ixa_flags
& IXAF_IS_IPV4
) {
1981 tcp
->tcp_ipha
->ipha_length
= htons(len
);
1983 tcp
->tcp_ip6h
->ip6_plen
=
1984 htons(len
- IPV6_HDR_LEN
);
1987 mp
= dupb(*xmit_tail
);
1989 return (-1); /* out_of_mem */
1993 * If the old timestamp is no longer in use,
1994 * sample a new timestamp now.
1996 if ((*xmit_tail
)->b_next
== NULL
) {
1997 (*xmit_tail
)->b_prev
= local_time
;
1998 (*xmit_tail
)->b_next
=
1999 (mblk_t
*)(uintptr_t)(*snxt
-len
);
2004 *xmit_tail
= (*xmit_tail
)->b_cont
;
2005 ASSERT((uintptr_t)((*xmit_tail
)->b_wptr
-
2006 (*xmit_tail
)->b_rptr
) <= (uintptr_t)INT_MAX
);
2007 *tail_unsent
= (int)((*xmit_tail
)->b_wptr
-
2008 (*xmit_tail
)->b_rptr
);
2011 (*xmit_tail
)->b_prev
= local_time
;
2012 (*xmit_tail
)->b_next
= (mblk_t
*)(uintptr_t)(*snxt
- len
);
2014 *tail_unsent
-= len
;
2015 if (len
<= mss
) /* LSO is unusable (!do_lso_send) */
2016 tcp
->tcp_last_sent_len
= (ushort_t
)len
;
2018 len
+= total_hdr_len
;
2019 ixa
->ixa_pktlen
= len
;
2021 if (ixa
->ixa_flags
& IXAF_IS_IPV4
) {
2022 tcp
->tcp_ipha
->ipha_length
= htons(len
);
2024 tcp
->tcp_ip6h
->ip6_plen
= htons(len
- IPV6_HDR_LEN
);
2027 mp
= dupb(*xmit_tail
);
2029 return (-1); /* out_of_mem */
2032 len
= total_hdr_len
;
2034 * There are four reasons to allocate a new hdr mblk:
2035 * 1) The bytes above us are in use by another packet
2036 * 2) We don't have good alignment
2037 * 3) The mblk is being shared
2038 * 4) We don't have enough room for a header
2040 rptr
= mp
->b_rptr
- len
;
2041 if (!OK_32PTR(rptr
) ||
2042 ((db
= mp
->b_datap
), db
->db_ref
!= 2) ||
2043 rptr
< db
->db_base
) {
2044 /* NOTE: we assume allocb returns an OK_32PTR */
2047 mp1
= allocb(connp
->conn_ht_iphc_allocated
+
2048 tcps
->tcps_wroff_xtra
, BPRI_MED
);
2051 return (-1); /* out_of_mem */
2055 /* Leave room for Link Level header */
2056 len
= total_hdr_len
;
2057 rptr
= &mp
->b_rptr
[tcps
->tcps_wroff_xtra
];
2058 mp
->b_wptr
= &rptr
[len
];
2062 * Fill in the header using the template header, and add
2063 * options such as time-stamp, ECN and/or SACK, as needed.
2065 tcp_fill_header(tcp
, rptr
, (clock_t)local_time
, num_sack_blk
);
2070 int spill
= *tail_unsent
;
2077 * If we're a little short, tack on more mblks until
2078 * there is no more spillover.
2084 nmp
= (*xmit_tail
)->b_cont
;
2088 * Excess data in mblk; can we split it?
2089 * If LSO is enabled for the connection,
2090 * keep on splitting as this is a transient
2093 if (!do_lso_send
&& (spill
+ nmpsz
> 0)) {
2095 * Don't split if stream head was
2096 * told to break up larger writes
2097 * into smaller ones.
2099 if (tcp
->tcp_maxpsz_multiplier
> 0)
2103 * Next mblk is less than SMSS/2
2104 * rounded up to nearest 64-byte;
2105 * let it get sent as part of the
2108 if (tcp
->tcp_localnet
&&
2110 (nmpsz
< roundup((mss
>> 1), 64)))
2115 ASSERT((uintptr_t)nmpsz
<= (uintptr_t)INT_MAX
);
2116 /* Stash for rtt use later */
2117 (*xmit_tail
)->b_prev
= local_time
;
2118 (*xmit_tail
)->b_next
=
2119 (mblk_t
*)(uintptr_t)(*snxt
- len
);
2120 mp1
->b_cont
= dupb(*xmit_tail
);
2125 *tail_unsent
= spill
;
2127 return (-1); /* out_of_mem */
2131 /* Trim back any surplus on the last mblk */
2133 mp1
->b_wptr
-= spill
;
2134 *tail_unsent
= spill
;
2137 * We did not send everything we could in
2138 * order to remain within the b_cont limit.
2142 tcp
->tcp_last_sent_len
+= spill
;
2143 TCPS_UPDATE_MIB(tcps
, tcpOutDataBytes
, spill
);
2145 * Adjust the checksum
2147 tcpha
= (tcpha_t
*)(rptr
+
2148 ixa
->ixa_ip_hdr_length
);
2150 sum
= (sum
>> 16) + (sum
& 0xFFFF);
2151 tcpha
->tha_sum
= htons(sum
);
2152 if (connp
->conn_ipversion
== IPV4_VERSION
) {
2154 ((ipha_t
*)rptr
)->ipha_length
) +
2156 ((ipha_t
*)rptr
)->ipha_length
=
2160 ((ip6_t
*)rptr
)->ip6_plen
) +
2162 ((ip6_t
*)rptr
)->ip6_plen
=
2165 ixa
->ixa_pktlen
+= spill
;
2169 if (tcp
->tcp_ip_forward_progress
) {
2170 tcp
->tcp_ip_forward_progress
= B_FALSE
;
2171 ixa
->ixa_flags
|= IXAF_REACH_CONF
;
2173 ixa
->ixa_flags
&= ~IXAF_REACH_CONF
;
2177 /* Append LSO information to the mp. */
2178 lso_info_set(mp
, mss
, HW_LSO
);
2179 ixa
->ixa_fragsize
= IP_MAXPACKET
;
2180 ixa
->ixa_extra_ident
= num_lso_seg
- 1;
2182 DTRACE_PROBE2(tcp_send_lso
, int, num_lso_seg
,
2185 tcp_send_data(tcp
, mp
);
2188 * Restore values of ixa_fragsize and ixa_extra_ident.
2190 ixa
->ixa_fragsize
= ixa
->ixa_pmtu
;
2191 ixa
->ixa_extra_ident
= 0;
2192 tcp
->tcp_obsegs
+= num_lso_seg
;
2193 TCP_STAT(tcps
, tcp_lso_times
);
2194 TCP_STAT_UPDATE(tcps
, tcp_lso_pkt_out
, num_lso_seg
);
2197 * Make sure to clean up LSO information. Wherever a
2198 * new mp uses the prepended header room after dupb(),
2199 * lso_info_cleanup() should be called.
2201 lso_info_cleanup(mp
);
2202 tcp_send_data(tcp
, mp
);
2203 BUMP_LOCAL(tcp
->tcp_obsegs
);
2211 * Initiate closedown sequence on an active connection. (May be called as
2212 * writer.) Return value zero for OK return, non-zero for error return.
2215 tcp_xmit_end(tcp_t
*tcp
)
2218 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
2220 ip_stack_t
*ipst
= tcps
->tcps_netstack
->netstack_ip
;
2221 conn_t
*connp
= tcp
->tcp_connp
;
2223 if (tcp
->tcp_state
< TCPS_SYN_RCVD
||
2224 tcp
->tcp_state
> TCPS_CLOSE_WAIT
) {
2226 * Invalid state, only states TCPS_SYN_RCVD,
2227 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid
2232 tcp
->tcp_fss
= tcp
->tcp_snxt
+ tcp
->tcp_unsent
;
2233 tcp
->tcp_valid_bits
|= TCP_FSS_VALID
;
2235 * If there is nothing more unsent, send the FIN now.
2236 * Otherwise, it will go out with the last segment.
2238 if (tcp
->tcp_unsent
== 0) {
2239 mp
= tcp_xmit_mp(tcp
, NULL
, 0, NULL
, NULL
,
2240 tcp
->tcp_fss
, B_FALSE
, NULL
, B_FALSE
);
2243 tcp_send_data(tcp
, mp
);
2246 * Couldn't allocate msg. Pretend we got it out.
2247 * Wait for rexmit timeout.
2249 tcp
->tcp_snxt
= tcp
->tcp_fss
+ 1;
2250 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
2254 * If needed, update tcp_rexmit_snxt as tcp_snxt is
2257 if (tcp
->tcp_rexmit
&& tcp
->tcp_rexmit_nxt
== tcp
->tcp_fss
) {
2258 tcp
->tcp_rexmit_nxt
= tcp
->tcp_snxt
;
2262 * If tcp->tcp_cork is set, then the data will not get sent,
2263 * so we have to check that and unset it first.
2266 tcp
->tcp_cork
= B_FALSE
;
2267 tcp_wput_data(tcp
, NULL
, B_FALSE
);
2271 * If TCP does not get enough samples of RTT or tcp_rtt_updates
2272 * is 0, don't update the cache.
2274 if (tcps
->tcps_rtt_updates
== 0 ||
2275 tcp
->tcp_rtt_update
< tcps
->tcps_rtt_updates
)
2279 * We do not have a good algorithm to update ssthresh at this time.
2280 * So don't do any update.
2282 bzero(&uinfo
, sizeof (uinfo
));
2283 uinfo
.iulp_rtt
= tcp
->tcp_rtt_sa
;
2284 uinfo
.iulp_rtt_sd
= tcp
->tcp_rtt_sd
;
2287 * Note that uinfo is kept for conn_faddr in the DCE. Could update even
2288 * if source routed but we don't.
2290 if (connp
->conn_ipversion
== IPV4_VERSION
) {
2291 if (connp
->conn_faddr_v4
!= tcp
->tcp_ipha
->ipha_dst
) {
2294 (void) dce_update_uinfo_v4(connp
->conn_faddr_v4
, &uinfo
, ipst
);
2298 if (!(IN6_ARE_ADDR_EQUAL(&connp
->conn_faddr_v6
,
2299 &tcp
->tcp_ip6h
->ip6_dst
))) {
2303 if (IN6_IS_ADDR_LINKSCOPE(&connp
->conn_faddr_v6
)) {
2304 ip_xmit_attr_t
*ixa
= connp
->conn_ixa
;
2307 * If we are going to create a DCE we'd better have
2310 if (ixa
->ixa_nce
!= NULL
) {
2311 ifindex
= ixa
->ixa_nce
->nce_common
->ncec_ill
->
2312 ill_phyint
->phyint_ifindex
;
2318 (void) dce_update_uinfo(&connp
->conn_faddr_v6
, ifindex
, &uinfo
,
2325 * Send out a control packet on the tcp connection specified. This routine
2326 * is typically called where we need a simple ACK or RST generated.
2329 tcp_xmit_ctl(char *str
, tcp_t
*tcp
, uint32_t seq
, uint32_t ack
, int ctl
)
2333 ipha_t
*ipha
= NULL
;
2339 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
2340 conn_t
*connp
= tcp
->tcp_connp
;
2341 ip_xmit_attr_t
*ixa
= connp
->conn_ixa
;
2344 * Save sum for use in source route later.
2346 sum
= connp
->conn_ht_ulp_len
+ connp
->conn_sum
;
2347 total_hdr_len
= connp
->conn_ht_iphc_len
;
2348 ip_hdr_len
= ixa
->ixa_ip_hdr_length
;
2350 /* If a text string is passed in with the request, pass it to strlog. */
2351 if (str
!= NULL
&& connp
->conn_debug
) {
2352 (void) strlog(TCP_MOD_ID
, 0, 1, SL_TRACE
,
2353 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x",
2354 str
, seq
, ack
, ctl
);
2356 mp
= allocb(connp
->conn_ht_iphc_allocated
+ tcps
->tcps_wroff_xtra
,
2361 rptr
= &mp
->b_rptr
[tcps
->tcps_wroff_xtra
];
2363 mp
->b_wptr
= &rptr
[total_hdr_len
];
2364 bcopy(connp
->conn_ht_iphc
, rptr
, total_hdr_len
);
2366 ixa
->ixa_pktlen
= total_hdr_len
;
2368 if (ixa
->ixa_flags
& IXAF_IS_IPV4
) {
2369 ipha
= (ipha_t
*)rptr
;
2370 ipha
->ipha_length
= htons(total_hdr_len
);
2372 ip6h
= (ip6_t
*)rptr
;
2373 ip6h
->ip6_plen
= htons(total_hdr_len
- IPV6_HDR_LEN
);
2375 tcpha
= (tcpha_t
*)&rptr
[ip_hdr_len
];
2376 tcpha
->tha_flags
= (uint8_t)ctl
;
2378 TCPS_BUMP_MIB(tcps
, tcpOutRsts
);
2379 TCPS_BUMP_MIB(tcps
, tcpOutControl
);
2381 * Don't send TSopt w/ TH_RST packets per RFC 1323.
2383 if (tcp
->tcp_snd_ts_ok
&&
2384 tcp
->tcp_state
> TCPS_SYN_SENT
) {
2385 mp
->b_wptr
= &rptr
[total_hdr_len
- TCPOPT_REAL_TS_LEN
];
2386 *(mp
->b_wptr
) = TCPOPT_EOL
;
2388 ixa
->ixa_pktlen
= total_hdr_len
- TCPOPT_REAL_TS_LEN
;
2390 if (connp
->conn_ipversion
== IPV4_VERSION
) {
2391 ipha
->ipha_length
= htons(total_hdr_len
-
2392 TCPOPT_REAL_TS_LEN
);
2394 ip6h
->ip6_plen
= htons(total_hdr_len
-
2395 IPV6_HDR_LEN
- TCPOPT_REAL_TS_LEN
);
2397 tcpha
->tha_offset_and_reserved
-= (3 << 4);
2398 sum
-= TCPOPT_REAL_TS_LEN
;
2402 if (tcp
->tcp_snd_ts_ok
) {
2403 uint32_t llbolt
= (uint32_t)LBOLT_FASTPATH
;
2406 (char *)tcpha
+ TCP_MIN_HEADER_LENGTH
+4);
2407 U32_TO_BE32(tcp
->tcp_ts_recent
,
2408 (char *)tcpha
+ TCP_MIN_HEADER_LENGTH
+8);
2411 /* Update the latest receive window size in TCP header. */
2412 tcpha
->tha_win
= htons(tcp
->tcp_rwnd
>> tcp
->tcp_rcv_ws
);
2413 /* Track what we sent to the peer */
2414 tcp
->tcp_tcpha
->tha_win
= tcpha
->tha_win
;
2415 tcp
->tcp_rack
= ack
;
2416 tcp
->tcp_rack_cnt
= 0;
2417 TCPS_BUMP_MIB(tcps
, tcpOutAck
);
2419 BUMP_LOCAL(tcp
->tcp_obsegs
);
2420 tcpha
->tha_seq
= htonl(seq
);
2421 tcpha
->tha_ack
= htonl(ack
);
2423 * Include the adjustment for a source route if any.
2425 sum
= (sum
>> 16) + (sum
& 0xFFFF);
2426 tcpha
->tha_sum
= htons(sum
);
2427 tcp_send_data(tcp
, mp
);
2431 * Generate a reset based on an inbound packet, connp is set by caller
2432 * when RST is in response to an unexpected inbound packet for which
2433 * there is active tcp state in the system.
2435 * IPSEC NOTE : Try to send the reply with the same protection as it came
2436 * in. We have the ip_recv_attr_t which is reversed to form the ip_xmit_attr_t.
2437 * That way the packet will go out at the same level of protection as it
2441 tcp_xmit_early_reset(char *str
, mblk_t
*mp
, uint32_t seq
, uint32_t ack
, int ctl
,
2442 ip_recv_attr_t
*ira
, ip_stack_t
*ipst
, conn_t
*connp
)
2444 ipha_t
*ipha
= NULL
;
2451 netstack_t
*ns
= ipst
->ips_netstack
;
2452 tcp_stack_t
*tcps
= ns
->netstack_tcp
;
2453 ip_xmit_attr_t ixas
, *ixa
;
2454 uint_t ip_hdr_len
= ira
->ira_ip_hdr_length
;
2455 boolean_t need_refrele
= B_FALSE
; /* ixa_refrele(ixa) */
2458 if (!tcp_send_rst_chk(tcps
)) {
2459 TCP_STAT(tcps
, tcp_rst_unsent
);
2465 * If connp != NULL we use conn_ixa to keep IP_NEXTHOP and other
2466 * options from the listener. In that case the caller must ensure that
2467 * we are running on the listener = connp squeue.
2469 * We get a safe copy of conn_ixa so we don't need to restore anything
2470 * we or ip_output_simple might change in the ixa.
2472 if (connp
!= NULL
) {
2473 ASSERT(connp
->conn_on_sqp
);
2475 ixa
= conn_get_ixa_exclusive(connp
);
2477 TCP_STAT(tcps
, tcp_rst_unsent
);
2481 need_refrele
= B_TRUE
;
2483 bzero(&ixas
, sizeof (ixas
));
2486 * IXAF_VERIFY_SOURCE is overkill since we know the
2487 * packet was for us.
2489 ixa
->ixa_flags
|= IXAF_SET_ULP_CKSUM
| IXAF_VERIFY_SOURCE
;
2490 ixa
->ixa_protocol
= IPPROTO_TCP
;
2491 ixa
->ixa_zoneid
= ira
->ira_zoneid
;
2492 ixa
->ixa_ifindex
= 0;
2493 ixa
->ixa_ipst
= ipst
;
2494 ixa
->ixa_cred
= kcred
;
2495 ixa
->ixa_cpid
= NOPID
;
2498 if (str
&& tcps
->tcps_dbg
) {
2499 (void) strlog(TCP_MOD_ID
, 0, 1, SL_TRACE
,
2500 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, "
2502 str
, seq
, ack
, ctl
);
2504 if (mp
->b_datap
->db_ref
!= 1) {
2505 mblk_t
*mp1
= copyb(mp
);
2510 } else if (mp
->b_cont
) {
2511 freemsg(mp
->b_cont
);
2513 DB_CKSUMFLAGS(mp
) = 0;
2516 * We skip reversing source route here.
2517 * (for now we replace all IP options with EOL)
2519 if (IPH_HDR_VERSION(mp
->b_rptr
) == IPV4_VERSION
) {
2520 ipha
= (ipha_t
*)mp
->b_rptr
;
2521 for (i
= IP_SIMPLE_HDR_LENGTH
; i
< (int)ip_hdr_len
; i
++)
2522 mp
->b_rptr
[i
] = IPOPT_EOL
;
2524 * Make sure that src address isn't flagrantly invalid.
2525 * Not all broadcast address checking for the src address
2526 * is possible, since we don't know the netmask of the src
2527 * addr. No check for destination address is done, since
2528 * IP will not pass up a packet with a broadcast dest
2529 * address to TCP. Similar checks are done below for IPv6.
2531 if (ipha
->ipha_src
== 0 || ipha
->ipha_src
== INADDR_BROADCAST
||
2532 CLASSD(ipha
->ipha_src
)) {
2533 BUMP_MIB(&ipst
->ips_ip_mib
, ipIfStatsInDiscards
);
2534 ip_drop_input("ipIfStatsInDiscards", mp
, NULL
);
2539 ip6h
= (ip6_t
*)mp
->b_rptr
;
2541 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h
->ip6_src
) ||
2542 IN6_IS_ADDR_MULTICAST(&ip6h
->ip6_src
)) {
2543 BUMP_MIB(&ipst
->ips_ip6_mib
, ipIfStatsInDiscards
);
2544 ip_drop_input("ipIfStatsInDiscards", mp
, NULL
);
2549 /* Remove any extension headers assuming partial overlay */
2550 if (ip_hdr_len
> IPV6_HDR_LEN
) {
2553 to
= mp
->b_rptr
+ ip_hdr_len
- IPV6_HDR_LEN
;
2554 ovbcopy(ip6h
, to
, IPV6_HDR_LEN
);
2555 mp
->b_rptr
+= ip_hdr_len
- IPV6_HDR_LEN
;
2556 ip_hdr_len
= IPV6_HDR_LEN
;
2557 ip6h
= (ip6_t
*)mp
->b_rptr
;
2558 ip6h
->ip6_nxt
= IPPROTO_TCP
;
2561 tcpha
= (tcpha_t
*)&mp
->b_rptr
[ip_hdr_len
];
2562 if (tcpha
->tha_flags
& TH_RST
) {
2566 tcpha
->tha_offset_and_reserved
= (5 << 4);
2567 len
= ip_hdr_len
+ sizeof (tcpha_t
);
2568 mp
->b_wptr
= &mp
->b_rptr
[len
];
2569 if (IPH_HDR_VERSION(mp
->b_rptr
) == IPV4_VERSION
) {
2570 ipha
->ipha_length
= htons(len
);
2571 /* Swap addresses */
2572 v4addr
= ipha
->ipha_src
;
2573 ipha
->ipha_src
= ipha
->ipha_dst
;
2574 ipha
->ipha_dst
= v4addr
;
2575 ipha
->ipha_ident
= 0;
2576 ipha
->ipha_ttl
= (uchar_t
)tcps
->tcps_ipv4_ttl
;
2577 ixa
->ixa_flags
|= IXAF_IS_IPV4
;
2578 ixa
->ixa_ip_hdr_length
= ip_hdr_len
;
2580 ip6h
->ip6_plen
= htons(len
- IPV6_HDR_LEN
);
2581 /* Swap addresses */
2582 v6addr
= ip6h
->ip6_src
;
2583 ip6h
->ip6_src
= ip6h
->ip6_dst
;
2584 ip6h
->ip6_dst
= v6addr
;
2585 ip6h
->ip6_hops
= (uchar_t
)tcps
->tcps_ipv6_hoplimit
;
2586 ixa
->ixa_flags
&= ~IXAF_IS_IPV4
;
2588 if (IN6_IS_ADDR_LINKSCOPE(&ip6h
->ip6_dst
)) {
2589 ixa
->ixa_flags
|= IXAF_SCOPEID_SET
;
2590 ixa
->ixa_scopeid
= ira
->ira_ruifindex
;
2592 ixa
->ixa_ip_hdr_length
= IPV6_HDR_LEN
;
2594 ixa
->ixa_pktlen
= len
;
2596 /* Swap the ports */
2597 port
= tcpha
->tha_fport
;
2598 tcpha
->tha_fport
= tcpha
->tha_lport
;
2599 tcpha
->tha_lport
= port
;
2601 tcpha
->tha_ack
= htonl(ack
);
2602 tcpha
->tha_seq
= htonl(seq
);
2604 tcpha
->tha_sum
= htons(sizeof (tcpha_t
));
2605 tcpha
->tha_flags
= (uint8_t)ctl
;
2609 * Probe connection rejection here.
2610 * tcp_xmit_listeners_reset() drops non-SYN segments
2611 * that do not specify TH_ACK in their flags without
2612 * calling this function. As a consequence, if this
2613 * function is called with a TH_RST|TH_ACK ctl argument,
2614 * it is being called in response to a SYN segment
2615 * and thus the tcp:::accept-refused probe point
2618 DTRACE_TCP5(accept__refused
, mblk_t
*, NULL
,
2619 void, NULL
, void_ip_t
*, mp
->b_rptr
, tcp_t
*, NULL
,
2622 TCPS_BUMP_MIB(tcps
, tcpOutRsts
);
2623 TCPS_BUMP_MIB(tcps
, tcpOutControl
);
2626 if (ira
->ira_flags
& IRAF_IPSEC_SECURE
) {
2628 * Apply IPsec based on how IPsec was applied to
2629 * the packet that caused the RST.
2631 if (!ipsec_in_to_out(ira
, ixa
, mp
, ipha
, ip6h
)) {
2632 BUMP_MIB(&ipst
->ips_ip_mib
, ipIfStatsOutDiscards
);
2633 /* Note: mp already consumed and ip_drop_packet done */
2638 * This is in clear. The RST message we are building
2639 * here should go out in clear, independent of our policy.
2641 ixa
->ixa_flags
|= IXAF_NO_IPSEC
;
2644 DTRACE_TCP5(send
, mblk_t
*, NULL
, ip_xmit_attr_t
*, ixa
,
2645 __dtrace_tcp_void_ip_t
*, mp
->b_rptr
, tcp_t
*, NULL
,
2646 __dtrace_tcp_tcph_t
*, tcpha
);
2649 * NOTE: one might consider tracing a TCP packet here, but
2650 * this function has no active TCP state and no tcp structure
2651 * that has a trace buffer. If we traced here, we would have
2652 * to keep a local trace buffer in tcp_record_trace().
2655 (void) ip_output_simple(mp
, ixa
);
2659 ASSERT(ixa
!= &ixas
);
2665 * Generate a "no listener here" RST in response to an "unknown" segment.
2666 * connp is set by caller when RST is in response to an unexpected
2667 * inbound packet for which there is active tcp state in the system.
2668 * Note that we are reusing the incoming mp to construct the outgoing RST.
2671 tcp_xmit_listeners_reset(mblk_t
*mp
, ip_recv_attr_t
*ira
, ip_stack_t
*ipst
,
2682 boolean_t policy_present
;
2683 netstack_t
*ns
= ipst
->ips_netstack
;
2684 tcp_stack_t
*tcps
= ns
->netstack_tcp
;
2685 ipsec_stack_t
*ipss
= tcps
->tcps_netstack
->netstack_ipsec
;
2686 uint_t ip_hdr_len
= ira
->ira_ip_hdr_length
;
2688 TCP_STAT(tcps
, tcp_no_listener
);
2691 * DTrace this "unknown" segment as a tcp:::receive, as we did
2692 * just receive something that was TCP.
2694 DTRACE_TCP5(receive
, mblk_t
*, NULL
, ip_xmit_attr_t
*, NULL
,
2695 __dtrace_tcp_void_ip_t
*, mp
->b_rptr
, tcp_t
*, NULL
,
2696 __dtrace_tcp_tcph_t
*, &mp
->b_rptr
[ip_hdr_len
]);
2698 if (IPH_HDR_VERSION(mp
->b_rptr
) == IPV4_VERSION
) {
2699 policy_present
= ipss
->ipsec_inbound_v4_policy_present
;
2700 ipha
= (ipha_t
*)mp
->b_rptr
;
2703 policy_present
= ipss
->ipsec_inbound_v6_policy_present
;
2705 ip6h
= (ip6_t
*)mp
->b_rptr
;
2708 if (policy_present
) {
2710 * The conn_t parameter is NULL because we already know
2713 mp
= ipsec_check_global_policy(mp
, (conn_t
*)NULL
, ipha
, ip6h
,
2720 tcpha
= (tcpha_t
*)&rptr
[ip_hdr_len
];
2721 seg_seq
= ntohl(tcpha
->tha_seq
);
2722 seg_ack
= ntohl(tcpha
->tha_ack
);
2723 flags
= tcpha
->tha_flags
;
2725 seg_len
= msgdsize(mp
) - (TCP_HDR_LENGTH(tcpha
) + ip_hdr_len
);
2726 if (flags
& TH_RST
) {
2728 } else if (flags
& TH_ACK
) {
2729 tcp_xmit_early_reset("no tcp, reset", mp
, seg_ack
, 0, TH_RST
,
2732 if (flags
& TH_SYN
) {
2736 * Here we violate the RFC. Note that a normal
2737 * TCP will never send a segment without the ACK
2738 * flag, except for RST or SYN segment. This
2739 * segment is neither. Just drop it on the
2743 TCP_STAT(tcps
, tcp_rst_unsent
);
2747 tcp_xmit_early_reset("no tcp, reset/ack", mp
, 0,
2748 seg_seq
+ seg_len
, TH_RST
| TH_ACK
, ira
, ipst
, connp
);
2753 * Helper function for tcp_xmit_mp() in handling connection set up flag
2757 tcp_xmit_mp_aux_iss(tcp_t
*tcp
, conn_t
*connp
, tcpha_t
*tcpha
, mblk_t
*mp
,
2761 uint8_t *wptr
= mp
->b_wptr
;
2762 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
2763 boolean_t add_sack
= B_FALSE
;
2766 * If TCP_ISS_VALID and the seq number is tcp_iss,
2767 * TCP can only be in SYN-SENT, SYN-RCVD or
2768 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if
2769 * our SYN is not ack'ed but the app closes this
2772 ASSERT(tcp
->tcp_state
== TCPS_SYN_SENT
||
2773 tcp
->tcp_state
== TCPS_SYN_RCVD
||
2774 tcp
->tcp_state
== TCPS_FIN_WAIT_1
);
2777 * Tack on the MSS option. It is always needed
2778 * for both active and passive open.
2780 * MSS option value should be interface MTU - MIN
2781 * TCP/IP header according to RFC 793 as it means
2782 * the maximum segment size TCP can receive. But
2783 * to get around some broken middle boxes/end hosts
2784 * out there, we allow the option value to be the
2785 * same as the MSS option size on the peer side.
2786 * In this way, the other side will not send
2787 * anything larger than they can receive.
2789 * Note that for SYN_SENT state, the ndd param
2790 * tcp_use_smss_as_mss_opt has no effect as we
2791 * don't know the peer's MSS option value. So
2792 * the only case we need to take care of is in
2793 * SYN_RCVD state, which is done later.
2795 wptr
[0] = TCPOPT_MAXSEG
;
2796 wptr
[1] = TCPOPT_MAXSEG_LEN
;
2798 u1
= tcp
->tcp_initial_pmtu
- (connp
->conn_ipversion
== IPV4_VERSION
?
2799 IP_SIMPLE_HDR_LENGTH
: IPV6_HDR_LEN
) - TCP_MIN_HEADER_LENGTH
;
2800 U16_TO_BE16(u1
, wptr
);
2803 /* Update the offset to cover the additional word */
2804 tcpha
->tha_offset_and_reserved
+= (1 << 4);
2806 switch (tcp
->tcp_state
) {
2810 if (tcp
->tcp_snd_sack_ok
)
2813 if (tcp
->tcp_snd_ts_ok
) {
2814 uint32_t llbolt
= (uint32_t)LBOLT_FASTPATH
;
2817 wptr
[0] = TCPOPT_SACK_PERMITTED
;
2818 wptr
[1] = TCPOPT_SACK_OK_LEN
;
2821 wptr
[0] = TCPOPT_NOP
;
2822 wptr
[1] = TCPOPT_NOP
;
2824 wptr
[2] = TCPOPT_TSTAMP
;
2825 wptr
[3] = TCPOPT_TSTAMP_LEN
;
2827 U32_TO_BE32(llbolt
, wptr
);
2829 ASSERT(tcp
->tcp_ts_recent
== 0);
2830 U32_TO_BE32(0L, wptr
);
2832 tcpha
->tha_offset_and_reserved
+= (3 << 4);
2836 * Set up all the bits to tell other side
2837 * we are ECN capable.
2839 if (tcp
->tcp_ecn_ok
)
2840 *flags
|= (TH_ECE
| TH_CWR
);
2848 * Reset the MSS option value to be SMSS
2849 * We should probably add back the bytes
2850 * for timestamp option and IPsec. We
2851 * don't do that as this is a workaround
2852 * for broken middle boxes/end hosts, it
2853 * is better for us to be more cautious.
2854 * They may not take these things into
2855 * account in their SMSS calculation. Thus
2856 * the peer's calculated SMSS may be smaller
2857 * than what it can be. This should be OK.
2859 if (tcps
->tcps_use_smss_as_mss_opt
) {
2862 * Note that wptr points just past the MSS
2865 U16_TO_BE16(u1
, wptr
- 2);
2869 * tcp_snd_ts_ok can only be set in TCPS_SYN_RCVD
2870 * when the peer also uses timestamps option. And
2871 * the TCP header template must have already been
2872 * updated to include the timestamps option.
2874 if (tcp
->tcp_snd_sack_ok
) {
2875 if (tcp
->tcp_snd_ts_ok
) {
2879 * Use the NOP in the header just
2880 * before timestamps opton.
2882 tmp_wptr
= (uint8_t *)tcpha
+
2883 TCP_MIN_HEADER_LENGTH
;
2884 ASSERT(tmp_wptr
[0] == TCPOPT_NOP
&&
2885 tmp_wptr
[1] == TCPOPT_NOP
);
2886 tmp_wptr
[0] = TCPOPT_SACK_PERMITTED
;
2887 tmp_wptr
[1] = TCPOPT_SACK_OK_LEN
;
2895 * If the other side is ECN capable, reply
2896 * that we are also ECN capable.
2898 if (tcp
->tcp_ecn_ok
)
2904 * The above ASSERT() makes sure that this
2905 * must be FIN-WAIT-1 state. Our SYN has
2906 * not been ack'ed so retransmit it.
2913 wptr
[0] = TCPOPT_NOP
;
2914 wptr
[1] = TCPOPT_NOP
;
2915 wptr
[2] = TCPOPT_SACK_PERMITTED
;
2916 wptr
[3] = TCPOPT_SACK_OK_LEN
;
2917 wptr
+= TCPOPT_REAL_SACK_OK_LEN
;
2918 tcpha
->tha_offset_and_reserved
+= (1 << 4);
2921 if (tcp
->tcp_snd_ws_ok
) {
2922 wptr
[0] = TCPOPT_NOP
;
2923 wptr
[1] = TCPOPT_WSCALE
;
2924 wptr
[2] = TCPOPT_WS_LEN
;
2925 wptr
[3] = (uchar_t
)tcp
->tcp_rcv_ws
;
2926 wptr
+= TCPOPT_REAL_WS_LEN
;
2927 tcpha
->tha_offset_and_reserved
+= (1 << 4);
2931 u1
= (int)(mp
->b_wptr
- mp
->b_rptr
);
2933 * Get IP set to checksum on our behalf
2934 * Include the adjustment for a source route if any.
2936 u1
+= connp
->conn_sum
;
2937 u1
= (u1
>> 16) + (u1
& 0xFFFF);
2938 tcpha
->tha_sum
= htons(u1
);
2939 TCPS_BUMP_MIB(tcps
, tcpOutControl
);
2943 * Helper function for tcp_xmit_mp() in handling connection tear down
2944 * flag setting and state changes.
2947 tcp_xmit_mp_aux_fss(tcp_t
*tcp
, ip_xmit_attr_t
*ixa
, uint_t
*flags
)
2949 if (!tcp
->tcp_fin_acked
) {
2951 TCPS_BUMP_MIB(tcp
->tcp_tcps
, tcpOutControl
);
2953 if (!tcp
->tcp_fin_sent
) {
2954 tcp
->tcp_fin_sent
= B_TRUE
;
2955 switch (tcp
->tcp_state
) {
2957 tcp
->tcp_state
= TCPS_FIN_WAIT_1
;
2958 DTRACE_TCP6(state__change
, void, NULL
,
2959 ip_xmit_attr_t
*, ixa
, void, NULL
,
2960 tcp_t
*, tcp
, void, NULL
,
2961 int32_t, TCPS_SYN_RCVD
);
2963 case TCPS_ESTABLISHED
:
2964 tcp
->tcp_state
= TCPS_FIN_WAIT_1
;
2965 DTRACE_TCP6(state__change
, void, NULL
,
2966 ip_xmit_attr_t
*, ixa
, void, NULL
,
2967 tcp_t
*, tcp
, void, NULL
,
2968 int32_t, TCPS_ESTABLISHED
);
2970 case TCPS_CLOSE_WAIT
:
2971 tcp
->tcp_state
= TCPS_LAST_ACK
;
2972 DTRACE_TCP6(state__change
, void, NULL
,
2973 ip_xmit_attr_t
*, ixa
, void, NULL
,
2974 tcp_t
*, tcp
, void, NULL
,
2975 int32_t, TCPS_CLOSE_WAIT
);
2978 if (tcp
->tcp_suna
== tcp
->tcp_snxt
)
2979 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
2980 tcp
->tcp_snxt
= tcp
->tcp_fss
+ 1;
2985 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with
2986 * ip and tcp header ready to pass down to IP. If the mp passed in is
2987 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that
2988 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary
2989 * otherwise it will dup partial mblks.)
2990 * Otherwise, an appropriate ACK packet will be generated. This
2991 * routine is not usually called to send new data for the first time. It
2992 * is mostly called out of the timer for retransmits, and to generate ACKs.
2994 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will
2995 * be adjusted by *offset. And after dupb(), the offset and the ending mblk
2996 * of the original mblk chain will be returned in *offset and *end_mp.
2999 tcp_xmit_mp(tcp_t
*tcp
, mblk_t
*mp
, int32_t max_to_send
, int32_t *offset
,
3000 mblk_t
**end_mp
, uint32_t seq
, boolean_t sendall
, uint32_t *seg_len
,
3010 int32_t num_sack_blk
= 0;
3011 int32_t sack_opt_len
= 0;
3012 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
3013 conn_t
*connp
= tcp
->tcp_connp
;
3014 ip_xmit_attr_t
*ixa
= connp
->conn_ixa
;
3016 /* Allocate for our maximum TCP header + link-level */
3017 mp1
= allocb(connp
->conn_ht_iphc_allocated
+ tcps
->tcps_wroff_xtra
,
3024 * Note that tcp_mss has been adjusted to take into account the
3025 * timestamp option if applicable. Because SACK options do not
3026 * appear in every TCP segments and they are of variable lengths,
3027 * they cannot be included in tcp_mss. Thus we need to calculate
3028 * the actual segment length when we need to send a segment which
3029 * includes SACK options.
3031 if (tcp
->tcp_snd_sack_ok
&& tcp
->tcp_num_sack_blk
> 0) {
3032 num_sack_blk
= MIN(tcp
->tcp_max_sack_blk
,
3033 tcp
->tcp_num_sack_blk
);
3034 sack_opt_len
= num_sack_blk
* sizeof (sack_blk_t
) +
3035 TCPOPT_NOP_LEN
* 2 + TCPOPT_HEADER_LEN
;
3036 if (max_to_send
+ sack_opt_len
> tcp
->tcp_mss
)
3037 max_to_send
-= sack_opt_len
;
3040 if (offset
!= NULL
) {
3042 /* We use offset as an indicator that end_mp is not NULL. */
3045 for (mp2
= mp1
; mp
&& data_length
!= max_to_send
; mp
= mp
->b_cont
) {
3046 /* This could be faster with cooperation from downstream */
3047 if (mp2
!= mp1
&& !sendall
&&
3048 data_length
+ (int)(mp
->b_wptr
- mp
->b_rptr
) >
3051 * Don't send the next mblk since the whole mblk
3055 mp2
->b_cont
= dupb(mp
);
3062 ASSERT((uintptr_t)(mp2
->b_wptr
- mp2
->b_rptr
) <=
3063 (uintptr_t)INT_MAX
);
3065 data_length
+= (int)(mp2
->b_wptr
- mp2
->b_rptr
);
3066 if (data_length
> max_to_send
) {
3067 mp2
->b_wptr
-= data_length
- max_to_send
;
3068 data_length
= max_to_send
;
3069 off
= mp2
->b_wptr
- mp
->b_rptr
;
3075 if (offset
!= NULL
) {
3079 if (seg_len
!= NULL
) {
3080 *seg_len
= data_length
;
3083 /* Update the latest receive window size in TCP header. */
3084 tcp
->tcp_tcpha
->tha_win
= htons(tcp
->tcp_rwnd
>> tcp
->tcp_rcv_ws
);
3086 rptr
= mp1
->b_rptr
+ tcps
->tcps_wroff_xtra
;
3088 mp1
->b_wptr
= rptr
+ connp
->conn_ht_iphc_len
+ sack_opt_len
;
3089 bcopy(connp
->conn_ht_iphc
, rptr
, connp
->conn_ht_iphc_len
);
3090 tcpha
= (tcpha_t
*)&rptr
[ixa
->ixa_ip_hdr_length
];
3091 tcpha
->tha_seq
= htonl(seq
);
3094 * Use tcp_unsent to determine if the PUSH bit should be used assumes
3095 * that this function was called from tcp_wput_data. Thus, when called
3096 * to retransmit data the setting of the PUSH bit may appear some
3097 * what random in that it might get set when it should not. This
3098 * should not pose any performance issues.
3100 if (data_length
!= 0 && (tcp
->tcp_unsent
== 0 ||
3101 tcp
->tcp_unsent
== data_length
)) {
3102 flags
= TH_ACK
| TH_PUSH
;
3107 if (tcp
->tcp_ecn_ok
) {
3108 if (tcp
->tcp_ecn_echo_on
)
3112 * Only set ECT bit and ECN_CWR if a segment contains new data.
3113 * There is no TCP flow control for non-data segments, and
3114 * only data segment is transmitted reliably.
3116 if (data_length
> 0 && !rexmit
) {
3117 TCP_SET_ECT(tcp
, rptr
);
3118 if (tcp
->tcp_cwr
&& !tcp
->tcp_ecn_cwr_sent
) {
3120 tcp
->tcp_ecn_cwr_sent
= B_TRUE
;
3125 /* Check if there is any special processing needs to be done. */
3126 if (tcp
->tcp_valid_bits
) {
3129 /* We don't allow having SYN and FIN in the same segment... */
3130 if ((tcp
->tcp_valid_bits
& TCP_ISS_VALID
) &&
3131 seq
== tcp
->tcp_iss
) {
3132 /* Need to do connection set up processing. */
3133 tcp_xmit_mp_aux_iss(tcp
, connp
, tcpha
, mp1
, &flags
);
3134 } else if ((tcp
->tcp_valid_bits
& TCP_FSS_VALID
) &&
3135 (seq
+ data_length
) == tcp
->tcp_fss
) {
3136 /* Need to do connection tear down processing. */
3137 tcp_xmit_mp_aux_fss(tcp
, ixa
, &flags
);
3141 * Need to do urgent pointer processing.
3143 * Note the trick here. u1 is unsigned. When tcp_urg
3144 * is smaller than seq, u1 will become a very huge value.
3145 * So the comparison will fail. Also note that tcp_urp
3146 * should be positive, see RFC 793 page 17.
3148 u1
= tcp
->tcp_urg
- seq
+ TCP_OLD_URP_INTERPRETATION
;
3149 if ((tcp
->tcp_valid_bits
& TCP_URG_VALID
) && u1
!= 0 &&
3150 u1
< (uint32_t)(64 * 1024)) {
3152 TCPS_BUMP_MIB(tcps
, tcpOutUrg
);
3153 tcpha
->tha_urp
= htons(u1
);
3156 tcpha
->tha_flags
= (uchar_t
)flags
;
3157 tcp
->tcp_rack
= tcp
->tcp_rnxt
;
3158 tcp
->tcp_rack_cnt
= 0;
3160 /* Fill in the current value of timestamps option. */
3161 if (tcp
->tcp_snd_ts_ok
) {
3162 if (tcp
->tcp_state
!= TCPS_SYN_SENT
) {
3163 uint32_t llbolt
= (uint32_t)LBOLT_FASTPATH
;
3166 (char *)tcpha
+ TCP_MIN_HEADER_LENGTH
+4);
3167 U32_TO_BE32(tcp
->tcp_ts_recent
,
3168 (char *)tcpha
+ TCP_MIN_HEADER_LENGTH
+8);
3172 /* Fill in the SACK blocks. */
3173 if (num_sack_blk
> 0) {
3174 uchar_t
*wptr
= (uchar_t
*)tcpha
+ connp
->conn_ht_ulp_len
;
3178 wptr
[0] = TCPOPT_NOP
;
3179 wptr
[1] = TCPOPT_NOP
;
3180 wptr
[2] = TCPOPT_SACK
;
3181 wptr
[3] = TCPOPT_HEADER_LEN
+ num_sack_blk
*
3182 sizeof (sack_blk_t
);
3183 wptr
+= TCPOPT_REAL_SACK_LEN
;
3185 tmp
= tcp
->tcp_sack_list
;
3186 for (i
= 0; i
< num_sack_blk
; i
++) {
3187 U32_TO_BE32(tmp
[i
].begin
, wptr
);
3188 wptr
+= sizeof (tcp_seq
);
3189 U32_TO_BE32(tmp
[i
].end
, wptr
);
3190 wptr
+= sizeof (tcp_seq
);
3192 tcpha
->tha_offset_and_reserved
+= ((num_sack_blk
* 2 + 1) << 4);
3194 ASSERT((uintptr_t)(mp1
->b_wptr
- rptr
) <= (uintptr_t)INT_MAX
);
3195 data_length
+= (int)(mp1
->b_wptr
- rptr
);
3197 ixa
->ixa_pktlen
= data_length
;
3199 if (ixa
->ixa_flags
& IXAF_IS_IPV4
) {
3200 ((ipha_t
*)rptr
)->ipha_length
= htons(data_length
);
3202 ip6_t
*ip6
= (ip6_t
*)rptr
;
3204 ip6
->ip6_plen
= htons(data_length
- IPV6_HDR_LEN
);
3209 * Include the adjustment for a source route if any.
3211 data_length
-= ixa
->ixa_ip_hdr_length
;
3212 data_length
+= connp
->conn_sum
;
3213 data_length
= (data_length
>> 16) + (data_length
& 0xFFFF);
3214 tcpha
->tha_sum
= htons(data_length
);
3215 if (tcp
->tcp_ip_forward_progress
) {
3216 tcp
->tcp_ip_forward_progress
= B_FALSE
;
3217 connp
->conn_ixa
->ixa_flags
|= IXAF_REACH_CONF
;
3219 connp
->conn_ixa
->ixa_flags
&= ~IXAF_REACH_CONF
;
3225 * If this routine returns B_TRUE, TCP can generate a RST in response
3226 * to a segment. If it returns B_FALSE, TCP should not respond.
3229 tcp_send_rst_chk(tcp_stack_t
*tcps
)
3234 * TCP needs to protect itself from generating too many RSTs.
3235 * This can be a DoS attack by sending us random segments
3238 * What we do here is to have a limit of tcp_rst_sent_rate RSTs
3239 * in each 1 second interval. In this way, TCP still generate
3240 * RSTs in normal cases but when under attack, the impact is
3243 if (tcps
->tcps_rst_sent_rate_enabled
!= 0) {
3244 now
= ddi_get_lbolt64();
3245 if (TICK_TO_MSEC(now
- tcps
->tcps_last_rst_intrvl
) >
3247 tcps
->tcps_last_rst_intrvl
= now
;
3248 tcps
->tcps_rst_cnt
= 1;
3249 } else if (++tcps
->tcps_rst_cnt
> tcps
->tcps_rst_sent_rate
) {
3257 * This function handles all retransmissions if SACK is enabled for this
3258 * connection. First it calculates how many segments can be retransmitted
3259 * based on tcp_pipe. Then it goes thru the notsack list to find eligible
3260 * segments. A segment is eligible if sack_cnt for that segment is greater
3261 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted
3262 * all eligible segments, it checks to see if TCP can send some new segments
3263 * (fast recovery). If it can, set the appropriate flag for tcp_input_data().
3266 * tcp_t *tcp: the tcp structure of the connection.
3267 * uint_t *flags: in return, appropriate value will be set for
3271 tcp_sack_rexmit(tcp_t
*tcp
, uint_t
*flags
)
3273 notsack_blk_t
*notsack_blk
;
3274 int32_t usable_swnd
;
3278 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
3280 ASSERT(tcp
->tcp_notsack_list
!= NULL
);
3281 ASSERT(tcp
->tcp_rexmit
== B_FALSE
);
3283 /* Defensive coding in case there is a bug... */
3284 if (tcp
->tcp_notsack_list
== NULL
) {
3287 notsack_blk
= tcp
->tcp_notsack_list
;
3291 * Limit the num of outstanding data in the network to be
3292 * tcp_cwnd_ssthresh, which is half of the original congestion wnd.
3294 usable_swnd
= tcp
->tcp_cwnd_ssthresh
- tcp
->tcp_pipe
;
3296 /* At least retransmit 1 MSS of data. */
3297 if (usable_swnd
<= 0) {
3301 /* Make sure no new RTT samples will be taken. */
3302 tcp
->tcp_csuna
= tcp
->tcp_snxt
;
3304 notsack_blk
= tcp
->tcp_notsack_list
;
3305 while (usable_swnd
> 0) {
3306 mblk_t
*snxt_mp
, *tmp_mp
;
3307 tcp_seq begin
= tcp
->tcp_sack_snxt
;
3311 for (; notsack_blk
!= NULL
; notsack_blk
= notsack_blk
->next
) {
3312 if (SEQ_GT(notsack_blk
->end
, begin
) &&
3313 (notsack_blk
->sack_cnt
>=
3314 tcps
->tcps_dupack_fast_retransmit
)) {
3315 end
= notsack_blk
->end
;
3316 if (SEQ_LT(begin
, notsack_blk
->begin
)) {
3317 begin
= notsack_blk
->begin
;
3323 * All holes are filled. Manipulate tcp_cwnd to send more
3324 * if we can. Note that after the SACK recovery, tcp_cwnd is
3325 * set to tcp_cwnd_ssthresh.
3327 if (notsack_blk
== NULL
) {
3328 usable_swnd
= tcp
->tcp_cwnd_ssthresh
- tcp
->tcp_pipe
;
3329 if (usable_swnd
<= 0 || tcp
->tcp_unsent
== 0) {
3330 tcp
->tcp_cwnd
= tcp
->tcp_snxt
- tcp
->tcp_suna
;
3331 ASSERT(tcp
->tcp_cwnd
> 0);
3334 usable_swnd
= usable_swnd
/ mss
;
3335 tcp
->tcp_cwnd
= tcp
->tcp_snxt
- tcp
->tcp_suna
+
3336 MAX(usable_swnd
* mss
, mss
);
3337 *flags
|= TH_XMIT_NEEDED
;
3343 * Note that we may send more than usable_swnd allows here
3344 * because of round off, but no more than 1 MSS of data.
3346 seg_len
= end
- begin
;
3349 snxt_mp
= tcp_get_seg_mp(tcp
, begin
, &off
);
3350 ASSERT(snxt_mp
!= NULL
);
3351 /* This should not happen. Defensive coding again... */
3352 if (snxt_mp
== NULL
) {
3356 xmit_mp
= tcp_xmit_mp(tcp
, snxt_mp
, seg_len
, &off
,
3357 &tmp_mp
, begin
, B_TRUE
, &seg_len
, B_TRUE
);
3358 if (xmit_mp
== NULL
)
3361 usable_swnd
-= seg_len
;
3362 tcp
->tcp_pipe
+= seg_len
;
3363 tcp
->tcp_sack_snxt
= begin
+ seg_len
;
3365 tcp_send_data(tcp
, xmit_mp
);
3368 * Update the send timestamp to avoid false retransmission.
3370 snxt_mp
->b_prev
= (mblk_t
*)ddi_get_lbolt();
3372 TCPS_BUMP_MIB(tcps
, tcpRetransSegs
);
3373 TCPS_UPDATE_MIB(tcps
, tcpRetransBytes
, seg_len
);
3374 TCPS_BUMP_MIB(tcps
, tcpOutSackRetransSegs
);
3376 * Update tcp_rexmit_max to extend this SACK recovery phase.
3377 * This happens when new data sent during fast recovery is
3378 * also lost. If TCP retransmits those new data, it needs
3379 * to extend SACK recover phase to avoid starting another
3380 * fast retransmit/recovery unnecessarily.
3382 if (SEQ_GT(tcp
->tcp_sack_snxt
, tcp
->tcp_rexmit_max
)) {
3383 tcp
->tcp_rexmit_max
= tcp
->tcp_sack_snxt
;
3389 * tcp_ss_rexmit() is called to do slow start retransmission after a timeout
3393 tcp_ss_rexmit(tcp_t
*tcp
)
3401 tcp_stack_t
*tcps
= tcp
->tcp_tcps
;
3404 * Note that tcp_rexmit can be set even though TCP has retransmitted
3405 * all unack'ed segments.
3407 if (SEQ_LT(tcp
->tcp_rexmit_nxt
, tcp
->tcp_rexmit_max
)) {
3408 smax
= tcp
->tcp_rexmit_max
;
3409 snxt
= tcp
->tcp_rexmit_nxt
;
3410 if (SEQ_LT(snxt
, tcp
->tcp_suna
)) {
3411 snxt
= tcp
->tcp_suna
;
3413 win
= MIN(tcp
->tcp_cwnd
, tcp
->tcp_swnd
);
3414 win
-= snxt
- tcp
->tcp_suna
;
3416 snxt_mp
= tcp_get_seg_mp(tcp
, snxt
, &off
);
3418 while (SEQ_LT(snxt
, smax
) && (win
> 0) && (snxt_mp
!= NULL
)) {
3420 mblk_t
*old_snxt_mp
= snxt_mp
;
3426 if (SEQ_GT(snxt
+ cnt
, smax
)) {
3429 xmit_mp
= tcp_xmit_mp(tcp
, snxt_mp
, cnt
, &off
,
3430 &snxt_mp
, snxt
, B_TRUE
, &cnt
, B_TRUE
);
3431 if (xmit_mp
== NULL
)
3434 tcp_send_data(tcp
, xmit_mp
);
3439 * Update the send timestamp to avoid false
3442 old_snxt_mp
->b_prev
= (mblk_t
*)ddi_get_lbolt();
3443 TCPS_BUMP_MIB(tcps
, tcpRetransSegs
);
3444 TCPS_UPDATE_MIB(tcps
, tcpRetransBytes
, cnt
);
3446 tcp
->tcp_rexmit_nxt
= snxt
;
3449 * If we have transmitted all we have at the time
3450 * we started the retranmission, we can leave
3451 * the rest of the job to tcp_wput_data(). But we
3452 * need to check the send window first. If the
3453 * win is not 0, go on with tcp_wput_data().
3455 if (SEQ_LT(snxt
, smax
) || win
== 0) {
3459 /* Only call tcp_wput_data() if there is data to be sent. */
3460 if (tcp
->tcp_unsent
) {
3461 tcp_wput_data(tcp
, NULL
, B_FALSE
);
3466 * Do slow start retransmission after ICMP errors of PMTU changes.
3469 tcp_rexmit_after_error(tcp_t
*tcp
)
3472 * All sent data has been acknowledged or no data left to send, just
3475 if (!SEQ_LT(tcp
->tcp_suna
, tcp
->tcp_snxt
) ||
3476 (tcp
->tcp_xmit_head
== NULL
))
3479 if ((tcp
->tcp_valid_bits
& TCP_FSS_VALID
) && (tcp
->tcp_unsent
== 0))
3480 tcp
->tcp_rexmit_max
= tcp
->tcp_fss
;
3482 tcp
->tcp_rexmit_max
= tcp
->tcp_snxt
;
3484 tcp
->tcp_rexmit_nxt
= tcp
->tcp_suna
;
3485 tcp
->tcp_rexmit
= B_TRUE
;
3486 tcp
->tcp_dupack_cnt
= 0;
3491 * tcp_get_seg_mp() is called to get the pointer to a segment in the
3492 * send queue which starts at the given sequence number. If the given
3493 * sequence number is equal to last valid sequence number (tcp_snxt), the
3494 * returned mblk is the last valid mblk, and off is set to the length of
3497 * send queue which starts at the given seq. no.
3500 * tcp_t *tcp: the tcp instance pointer.
3501 * uint32_t seq: the starting seq. no of the requested segment.
3502 * int32_t *off: after the execution, *off will be the offset to
3503 * the returned mblk which points to the requested seq no.
3504 * It is the caller's responsibility to send in a non-null off.
3507 * A mblk_t pointer pointing to the requested segment in send queue.
3510 tcp_get_seg_mp(tcp_t
*tcp
, uint32_t seq
, int32_t *off
)
3515 /* Defensive coding. Make sure we don't send incorrect data. */
3516 if (SEQ_LT(seq
, tcp
->tcp_suna
) || SEQ_GT(seq
, tcp
->tcp_snxt
))
3519 cnt
= seq
- tcp
->tcp_suna
;
3520 mp
= tcp
->tcp_xmit_head
;
3521 while (cnt
> 0 && mp
!= NULL
) {
3522 cnt
-= mp
->b_wptr
- mp
->b_rptr
;
3524 cnt
+= mp
->b_wptr
- mp
->b_rptr
;
3535 * This routine adjusts next-to-send sequence number variables, in the
3536 * case where the reciever has shrunk it's window.
3539 tcp_update_xmit_tail(tcp_t
*tcp
, uint32_t snxt
)
3544 tcp
->tcp_snxt
= snxt
;
3546 /* Get the mblk, and the offset in it, as per the shrunk window */
3547 xmit_tail
= tcp_get_seg_mp(tcp
, snxt
, &offset
);
3548 ASSERT(xmit_tail
!= NULL
);
3549 tcp
->tcp_xmit_tail
= xmit_tail
;
3550 tcp
->tcp_xmit_tail_unsent
= xmit_tail
->b_wptr
-
3551 xmit_tail
->b_rptr
- offset
;
3555 * This handles the case when the receiver has shrunk its win. Per RFC 1122
3556 * if the receiver shrinks the window, i.e. moves the right window to the
3557 * left, the we should not send new data, but should retransmit normally the
3558 * old unacked data between suna and suna + swnd. We might has sent data
3559 * that is now outside the new window, pretend that we didn't send it.
3562 tcp_process_shrunk_swnd(tcp_t
*tcp
, uint32_t shrunk_count
)
3564 uint32_t snxt
= tcp
->tcp_snxt
;
3566 ASSERT(shrunk_count
> 0);
3568 if (!tcp
->tcp_is_wnd_shrnk
) {
3569 tcp
->tcp_snxt_shrunk
= snxt
;
3570 tcp
->tcp_is_wnd_shrnk
= B_TRUE
;
3571 } else if (SEQ_GT(snxt
, tcp
->tcp_snxt_shrunk
)) {
3572 tcp
->tcp_snxt_shrunk
= snxt
;
3575 /* Pretend we didn't send the data outside the window */
3576 snxt
-= shrunk_count
;
3578 /* Reset all the values per the now shrunk window */
3579 tcp_update_xmit_tail(tcp
, snxt
);
3580 tcp
->tcp_unsent
+= shrunk_count
;
3583 * If the SACK option is set, delete the entire list of
3584 * notsack'ed blocks.
3586 TCP_NOTSACK_REMOVE_ALL(tcp
->tcp_notsack_list
, tcp
);
3588 if (tcp
->tcp_suna
== tcp
->tcp_snxt
&& tcp
->tcp_swnd
== 0)
3590 * Make sure the timer is running so that we will probe a zero
3593 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
3597 * tcp_fill_header is called by tcp_send() to fill the outgoing TCP header
3598 * with the template header, as well as other options such as time-stamp,
3602 tcp_fill_header(tcp_t
*tcp
, uchar_t
*rptr
, clock_t now
, int num_sack_blk
)
3604 tcpha_t
*tcp_tmpl
, *tcpha
;
3605 uint32_t *dst
, *src
;
3607 conn_t
*connp
= tcp
->tcp_connp
;
3609 ASSERT(OK_32PTR(rptr
));
3611 /* Template header */
3612 tcp_tmpl
= tcp
->tcp_tcpha
;
3614 /* Header of outgoing packet */
3615 tcpha
= (tcpha_t
*)(rptr
+ connp
->conn_ixa
->ixa_ip_hdr_length
);
3617 /* dst and src are opaque 32-bit fields, used for copying */
3618 dst
= (uint32_t *)rptr
;
3619 src
= (uint32_t *)connp
->conn_ht_iphc
;
3620 hdrlen
= connp
->conn_ht_iphc_len
;
3622 /* Fill time-stamp option if needed */
3623 if (tcp
->tcp_snd_ts_ok
) {
3624 U32_TO_BE32((uint32_t)now
,
3625 (char *)tcp_tmpl
+ TCP_MIN_HEADER_LENGTH
+ 4);
3626 U32_TO_BE32(tcp
->tcp_ts_recent
,
3627 (char *)tcp_tmpl
+ TCP_MIN_HEADER_LENGTH
+ 8);
3629 ASSERT(connp
->conn_ht_ulp_len
== TCP_MIN_HEADER_LENGTH
);
3633 * Copy the template header; is this really more efficient than
3634 * calling bcopy()? For simple IPv4/TCP, it may be the case,
3635 * but perhaps not for other scenarios.
3657 * Set the ECN info in the TCP header if it is not a zero
3658 * window probe. Zero window probe is only sent in
3659 * tcp_wput_data() and tcp_timer().
3661 if (tcp
->tcp_ecn_ok
&& !tcp
->tcp_zero_win_probe
) {
3662 TCP_SET_ECT(tcp
, rptr
);
3664 if (tcp
->tcp_ecn_echo_on
)
3665 tcpha
->tha_flags
|= TH_ECE
;
3666 if (tcp
->tcp_cwr
&& !tcp
->tcp_ecn_cwr_sent
) {
3667 tcpha
->tha_flags
|= TH_CWR
;
3668 tcp
->tcp_ecn_cwr_sent
= B_TRUE
;
3672 /* Fill in SACK options */
3673 if (num_sack_blk
> 0) {
3674 uchar_t
*wptr
= rptr
+ connp
->conn_ht_iphc_len
;
3678 wptr
[0] = TCPOPT_NOP
;
3679 wptr
[1] = TCPOPT_NOP
;
3680 wptr
[2] = TCPOPT_SACK
;
3681 wptr
[3] = TCPOPT_HEADER_LEN
+ num_sack_blk
*
3682 sizeof (sack_blk_t
);
3683 wptr
+= TCPOPT_REAL_SACK_LEN
;
3685 tmp
= tcp
->tcp_sack_list
;
3686 for (i
= 0; i
< num_sack_blk
; i
++) {
3687 U32_TO_BE32(tmp
[i
].begin
, wptr
);
3688 wptr
+= sizeof (tcp_seq
);
3689 U32_TO_BE32(tmp
[i
].end
, wptr
);
3690 wptr
+= sizeof (tcp_seq
);
3692 tcpha
->tha_offset_and_reserved
+=
3693 ((num_sack_blk
* 2 + 1) << 4);