Merge illumos-gate
[unleashed/lotheac.git] / kernel / net / tcp / tcp_output.c
blob2716dc0bdbd57cb299afbc8f0c84463dfa8b33bd
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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>
46 #include <inet/ip.h>
47 #include <inet/tcp.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;
82 int
83 tcp_wput(queue_t *q, mblk_t *mp)
85 conn_t *connp = Q_TO_CONN(q);
86 tcp_t *tcp;
87 void (*output_proc)();
88 t_scalar_t type;
89 uchar_t *rptr;
90 struct iocblk *iocp;
91 size_t size;
93 ASSERT(connp->conn_ref >= 2);
95 switch (DB_TYPE(mp)) {
96 case M_DATA:
97 tcp = connp->conn_tcp;
98 ASSERT(tcp != NULL);
100 size = msgdsize(mp);
102 mutex_enter(&tcp->tcp_non_sq_lock);
103 tcp->tcp_squeue_bytes += size;
104 if (TCP_UNSENT_BYTES(tcp) > connp->conn_sndbuf) {
105 tcp_setqfull(tcp);
107 mutex_exit(&tcp->tcp_non_sq_lock);
109 CONN_INC_REF(connp);
110 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp,
111 NULL, tcp_squeue_flag, SQTAG_TCP_OUTPUT);
112 return (0);
114 case M_CMD:
115 tcp_wput_cmdblk(q, mp);
116 return (0);
118 case M_PROTO:
119 case M_PCPROTO:
121 * if it is a snmp message, don't get behind the squeue
123 tcp = connp->conn_tcp;
124 rptr = mp->b_rptr;
125 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) {
126 type = ((union T_primitives *)rptr)->type;
127 } else {
128 if (connp->conn_debug) {
129 (void) strlog(TCP_MOD_ID, 0, 1,
130 SL_ERROR|SL_TRACE,
131 "tcp_wput_proto, dropping one...");
133 freemsg(mp);
134 return (0);
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);
146 ASSERT(cr != NULL);
147 if (cr == NULL) {
148 tcp_err_ack(tcp, mp, TSYSERR, EINVAL);
149 return (0);
151 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get,
152 cr)) {
154 * This was a SNMP request
156 return (0);
157 } else {
158 output_proc = tcp_wput_proto;
160 } else {
161 output_proc = tcp_wput_proto;
163 break;
164 case M_IOCTL:
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);
177 return (0);
178 case TI_GETPEERNAME:
179 case TI_GETMYNAME:
180 mi_copyin(q, mp, NULL,
181 SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
182 return (0);
184 default:
185 output_proc = tcp_wput_ioctl;
186 break;
188 break;
189 default:
190 output_proc = tcp_wput_nondata;
191 break;
194 CONN_INC_REF(connp);
195 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp,
196 NULL, tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER);
197 return (0);
201 * The TCP normal data output path.
202 * NOTE: the logic of the fast path is duplicated from this function.
204 void
205 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent)
207 int len;
208 mblk_t *local_time;
209 mblk_t *mp1;
210 uint32_t snxt;
211 int tail_unsent;
212 int tcpstate;
213 int usable = 0;
214 mblk_t *xmit_tail;
215 int32_t mss;
216 int32_t num_sack_blk = 0;
217 int32_t total_hdr_len;
218 int32_t tcp_hdr_len;
219 int rc;
220 tcp_stack_t *tcps = tcp->tcp_tcps;
221 conn_t *connp = tcp->tcp_connp;
222 clock_t now = LBOLT_FASTPATH;
224 tcpstate = tcp->tcp_state;
225 if (mp == NULL) {
227 * tcp_wput_data() with NULL mp should only be called when
228 * there is unsent data.
230 ASSERT(tcp->tcp_unsent > 0);
231 /* Really tacky... but we need this for detached closes. */
232 len = tcp->tcp_unsent;
233 goto data_null;
236 ASSERT(mp->b_datap->db_type == M_DATA);
238 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ,
239 * or before a connection attempt has begun.
241 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT ||
242 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) {
243 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) {
244 #ifdef DEBUG
245 cmn_err(CE_WARN,
246 "tcp_wput_data: data after ordrel, %s",
247 tcp_display(tcp, NULL,
248 DISP_ADDR_AND_PORT));
249 #else
250 if (connp->conn_debug) {
251 (void) strlog(TCP_MOD_ID, 0, 1,
252 SL_TRACE|SL_ERROR,
253 "tcp_wput_data: data after ordrel, %s\n",
254 tcp_display(tcp, NULL,
255 DISP_ADDR_AND_PORT));
257 #endif /* DEBUG */
259 if (tcp->tcp_snd_zcopy_aware &&
260 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY))
261 tcp_zcopy_notify(tcp);
262 freemsg(mp);
263 mutex_enter(&tcp->tcp_non_sq_lock);
264 if (tcp->tcp_flow_stopped &&
265 TCP_UNSENT_BYTES(tcp) <= connp->conn_sndlowat) {
266 tcp_clrqfull(tcp);
268 mutex_exit(&tcp->tcp_non_sq_lock);
269 return;
272 /* Strip empties */
273 for (;;) {
274 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
275 (uintptr_t)INT_MAX);
276 len = (int)(mp->b_wptr - mp->b_rptr);
277 if (len > 0)
278 break;
279 mp1 = mp;
280 mp = mp->b_cont;
281 freeb(mp1);
282 if (mp == NULL) {
283 return;
287 /* If we are the first on the list ... */
288 if (tcp->tcp_xmit_head == NULL) {
289 tcp->tcp_xmit_head = mp;
290 tcp->tcp_xmit_tail = mp;
291 tcp->tcp_xmit_tail_unsent = len;
292 } else {
293 /* If tiny tx and room in txq tail, pullup to save mblks. */
294 struct datab *dp;
296 mp1 = tcp->tcp_xmit_last;
297 if (len < tcp_tx_pull_len &&
298 (dp = mp1->b_datap)->db_ref == 1 &&
299 dp->db_lim - mp1->b_wptr >= len) {
300 ASSERT(len > 0);
301 ASSERT(!mp1->b_cont);
302 if (len == 1) {
303 *mp1->b_wptr++ = *mp->b_rptr;
304 } else {
305 bcopy(mp->b_rptr, mp1->b_wptr, len);
306 mp1->b_wptr += len;
308 if (mp1 == tcp->tcp_xmit_tail)
309 tcp->tcp_xmit_tail_unsent += len;
310 mp1->b_cont = mp->b_cont;
311 if (tcp->tcp_snd_zcopy_aware &&
312 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY))
313 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY;
314 freeb(mp);
315 mp = mp1;
316 } else {
317 tcp->tcp_xmit_last->b_cont = mp;
319 len += tcp->tcp_unsent;
322 /* Tack on however many more positive length mblks we have */
323 if ((mp1 = mp->b_cont) != NULL) {
324 do {
325 int tlen;
326 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <=
327 (uintptr_t)INT_MAX);
328 tlen = (int)(mp1->b_wptr - mp1->b_rptr);
329 if (tlen <= 0) {
330 mp->b_cont = mp1->b_cont;
331 freeb(mp1);
332 } else {
333 len += tlen;
334 mp = mp1;
336 } while ((mp1 = mp->b_cont) != NULL);
338 tcp->tcp_xmit_last = mp;
339 tcp->tcp_unsent = len;
341 if (urgent)
342 usable = 1;
344 data_null:
345 snxt = tcp->tcp_snxt;
346 xmit_tail = tcp->tcp_xmit_tail;
347 tail_unsent = tcp->tcp_xmit_tail_unsent;
350 * Note that tcp_mss has been adjusted to take into account the
351 * timestamp option if applicable. Because SACK options do not
352 * appear in every TCP segments and they are of variable lengths,
353 * they cannot be included in tcp_mss. Thus we need to calculate
354 * the actual segment length when we need to send a segment which
355 * includes SACK options.
357 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
358 int32_t opt_len;
360 num_sack_blk = MIN(tcp->tcp_max_sack_blk,
361 tcp->tcp_num_sack_blk);
362 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN *
363 2 + TCPOPT_HEADER_LEN;
364 mss = tcp->tcp_mss - opt_len;
365 total_hdr_len = connp->conn_ht_iphc_len + opt_len;
366 tcp_hdr_len = connp->conn_ht_ulp_len + opt_len;
367 } else {
368 mss = tcp->tcp_mss;
369 total_hdr_len = connp->conn_ht_iphc_len;
370 tcp_hdr_len = connp->conn_ht_ulp_len;
373 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet &&
374 (TICK_TO_MSEC(now - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) {
375 TCP_SET_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle);
377 if (tcpstate == TCPS_SYN_RCVD) {
379 * The three-way connection establishment handshake is not
380 * complete yet. We want to queue the data for transmission
381 * after entering ESTABLISHED state (RFC793). A jump to
382 * "done" label effectively leaves data on the queue.
384 goto done;
385 } else {
386 int usable_r;
389 * In the special case when cwnd is zero, which can only
390 * happen if the connection is ECN capable, return now.
391 * New segments is sent using tcp_timer(). The timer
392 * is set in tcp_input_data().
394 if (tcp->tcp_cwnd == 0) {
396 * Note that tcp_cwnd is 0 before 3-way handshake is
397 * finished.
399 ASSERT(tcp->tcp_ecn_ok ||
400 tcp->tcp_state < TCPS_ESTABLISHED);
401 return;
404 /* NOTE: trouble if xmitting while SYN not acked? */
405 usable_r = snxt - tcp->tcp_suna;
406 usable_r = tcp->tcp_swnd - usable_r;
409 * Check if the receiver has shrunk the window. If
410 * tcp_wput_data() with NULL mp is called, tcp_fin_sent
411 * cannot be set as there is unsent data, so FIN cannot
412 * be sent out. Otherwise, we need to take into account
413 * of FIN as it consumes an "invisible" sequence number.
415 ASSERT(tcp->tcp_fin_sent == 0);
416 if (usable_r < 0) {
418 * The receiver has shrunk the window and we have sent
419 * -usable_r date beyond the window, re-adjust.
421 * If TCP window scaling is enabled, there can be
422 * round down error as the advertised receive window
423 * is actually right shifted n bits. This means that
424 * the lower n bits info is wiped out. It will look
425 * like the window is shrunk. Do a check here to
426 * see if the shrunk amount is actually within the
427 * error in window calculation. If it is, just
428 * return. Note that this check is inside the
429 * shrunk window check. This makes sure that even
430 * though tcp_process_shrunk_swnd() is not called,
431 * we will stop further processing.
433 if ((-usable_r >> tcp->tcp_snd_ws) > 0) {
434 tcp_process_shrunk_swnd(tcp, -usable_r);
436 return;
439 /* usable = MIN(swnd, cwnd) - unacked_bytes */
440 if (tcp->tcp_swnd > tcp->tcp_cwnd)
441 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd;
443 /* usable = MIN(usable, unsent) */
444 if (usable_r > len)
445 usable_r = len;
447 /* usable = MAX(usable, {1 for urgent, 0 for data}) */
448 if (usable_r > 0) {
449 usable = usable_r;
450 } else {
451 /* Bypass all other unnecessary processing. */
452 goto done;
456 local_time = (mblk_t *)now;
459 * "Our" Nagle Algorithm. This is not the same as in the old
460 * BSD. This is more in line with the true intent of Nagle.
462 * The conditions are:
463 * 1. The amount of unsent data (or amount of data which can be
464 * sent, whichever is smaller) is less than Nagle limit.
465 * 2. The last sent size is also less than Nagle limit.
466 * 3. There is unack'ed data.
467 * 4. Urgent pointer is not set. Send urgent data ignoring the
468 * Nagle algorithm. This reduces the probability that urgent
469 * bytes get "merged" together.
470 * 5. The app has not closed the connection. This eliminates the
471 * wait time of the receiving side waiting for the last piece of
472 * (small) data.
474 * If all are satisified, exit without sending anything. Note
475 * that Nagle limit can be smaller than 1 MSS. Nagle limit is
476 * the smaller of 1 MSS and global tcp_naglim_def (default to be
477 * 4095).
479 if (usable < (int)tcp->tcp_naglim &&
480 tcp->tcp_naglim > tcp->tcp_last_sent_len &&
481 snxt != tcp->tcp_suna &&
482 !(tcp->tcp_valid_bits & TCP_URG_VALID) &&
483 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) {
484 goto done;
488 * If tcp_zero_win_probe is not set and the tcp->tcp_cork option
489 * is set, then we have to force TCP not to send partial segment
490 * (smaller than MSS bytes). We are calculating the usable now
491 * based on full mss and will save the rest of remaining data for
492 * later. When tcp_zero_win_probe is set, TCP needs to send out
493 * something to do zero window probe.
495 if (tcp->tcp_cork && !tcp->tcp_zero_win_probe) {
496 if (usable < mss)
497 goto done;
498 usable = (usable / mss) * mss;
501 /* Update the latest receive window size in TCP header. */
502 tcp->tcp_tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
504 /* Send the packet. */
505 rc = tcp_send(tcp, mss, total_hdr_len, tcp_hdr_len,
506 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail,
507 local_time);
509 /* Pretend that all we were trying to send really got sent */
510 if (rc < 0 && tail_unsent < 0) {
511 do {
512 xmit_tail = xmit_tail->b_cont;
513 xmit_tail->b_prev = local_time;
514 ASSERT((uintptr_t)(xmit_tail->b_wptr -
515 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX);
516 tail_unsent += (int)(xmit_tail->b_wptr -
517 xmit_tail->b_rptr);
518 } while (tail_unsent < 0);
520 done:;
521 tcp->tcp_xmit_tail = xmit_tail;
522 tcp->tcp_xmit_tail_unsent = tail_unsent;
523 len = tcp->tcp_snxt - snxt;
524 if (len) {
526 * If new data was sent, need to update the notsack
527 * list, which is, afterall, data blocks that have
528 * not been sack'ed by the receiver. New data is
529 * not sack'ed.
531 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) {
532 /* len is a negative value. */
533 tcp->tcp_pipe -= len;
534 tcp_notsack_update(&(tcp->tcp_notsack_list),
535 tcp->tcp_snxt, snxt,
536 &(tcp->tcp_num_notsack_blk),
537 &(tcp->tcp_cnt_notsack_list));
539 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent;
540 tcp->tcp_rack = tcp->tcp_rnxt;
541 tcp->tcp_rack_cnt = 0;
542 if ((snxt + len) == tcp->tcp_suna) {
543 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
545 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) {
547 * Didn't send anything. Make sure the timer is running
548 * so that we will probe a zero window.
550 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
552 /* Note that len is the amount we just sent but with a negative sign */
553 tcp->tcp_unsent += len;
554 mutex_enter(&tcp->tcp_non_sq_lock);
555 if (tcp->tcp_flow_stopped) {
556 if (TCP_UNSENT_BYTES(tcp) <= connp->conn_sndlowat) {
557 tcp_clrqfull(tcp);
559 } else if (TCP_UNSENT_BYTES(tcp) >= connp->conn_sndbuf) {
560 if (!(tcp->tcp_detached))
561 tcp_setqfull(tcp);
563 mutex_exit(&tcp->tcp_non_sq_lock);
567 * Initial STREAMS write side put() procedure for sockets. It tries to
568 * handle the T_CAPABILITY_REQ which sockfs sends down while setting
569 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages
570 * are handled by tcp_wput() as usual.
572 * All further messages will also be handled by tcp_wput() because we cannot
573 * be sure that the above short cut is safe later.
576 tcp_wput_sock(queue_t *wq, mblk_t *mp)
578 conn_t *connp = Q_TO_CONN(wq);
579 tcp_t *tcp = connp->conn_tcp;
580 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr;
582 ASSERT(wq->q_qinfo == &tcp_sock_winit);
583 wq->q_qinfo = &tcp_winit;
585 ASSERT(IPCL_IS_TCP(connp));
586 ASSERT(TCP_IS_SOCKET(tcp));
588 if (DB_TYPE(mp) == M_PCPROTO &&
589 MBLKL(mp) == sizeof (struct T_capability_req) &&
590 car->PRIM_type == T_CAPABILITY_REQ) {
591 tcp_capability_req(tcp, mp);
592 return (0);
595 tcp_wput(wq, mp);
596 return (0);
599 /* ARGSUSED */
601 tcp_wput_fallback(queue_t *wq, mblk_t *mp)
603 #ifdef DEBUG
604 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n");
605 #endif
606 freemsg(mp);
607 return (0);
611 * Call by tcp_wput() to handle misc non M_DATA messages.
613 /* ARGSUSED */
614 static void
615 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
617 conn_t *connp = (conn_t *)arg;
618 tcp_t *tcp = connp->conn_tcp;
620 ASSERT(DB_TYPE(mp) != M_IOCTL);
622 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close.
623 * Once the close starts, streamhead and sockfs will not let any data
624 * packets come down (close ensures that there are no threads using the
625 * queue and no new threads will come down) but since qprocsoff()
626 * hasn't happened yet, a M_FLUSH or some non data message might
627 * get reflected back (in response to our own FLUSHRW) and get
628 * processed after tcp_close() is done. The conn would still be valid
629 * because a ref would have added but we need to check the state
630 * before actually processing the packet.
632 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) {
633 freemsg(mp);
634 return;
637 switch (DB_TYPE(mp)) {
638 case M_IOCDATA:
639 tcp_wput_iocdata(tcp, mp);
640 break;
641 case M_FLUSH:
642 tcp_wput_flush(tcp, mp);
643 break;
644 default:
645 ip_wput_nondata(connp->conn_wq, mp);
646 break;
650 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */
651 static void
652 tcp_wput_flush(tcp_t *tcp, mblk_t *mp)
654 uchar_t fval = *mp->b_rptr;
655 mblk_t *tail;
656 conn_t *connp = tcp->tcp_connp;
657 queue_t *q = connp->conn_wq;
659 /* TODO: How should flush interact with urgent data? */
660 if ((fval & FLUSHW) && tcp->tcp_xmit_head != NULL &&
661 !(tcp->tcp_valid_bits & TCP_URG_VALID)) {
663 * Flush only data that has not yet been put on the wire. If
664 * we flush data that we have already transmitted, life, as we
665 * know it, may come to an end.
667 tail = tcp->tcp_xmit_tail;
668 tail->b_wptr -= tcp->tcp_xmit_tail_unsent;
669 tcp->tcp_xmit_tail_unsent = 0;
670 tcp->tcp_unsent = 0;
671 if (tail->b_wptr != tail->b_rptr)
672 tail = tail->b_cont;
673 if (tail) {
674 mblk_t **excess = &tcp->tcp_xmit_head;
675 for (;;) {
676 mblk_t *mp1 = *excess;
677 if (mp1 == tail)
678 break;
679 tcp->tcp_xmit_tail = mp1;
680 tcp->tcp_xmit_last = mp1;
681 excess = &mp1->b_cont;
683 *excess = NULL;
684 tcp_close_mpp(&tail);
685 if (tcp->tcp_snd_zcopy_aware)
686 tcp_zcopy_notify(tcp);
689 * We have no unsent data, so unsent must be less than
690 * conn_sndlowat, so re-enable flow.
692 mutex_enter(&tcp->tcp_non_sq_lock);
693 if (tcp->tcp_flow_stopped) {
694 tcp_clrqfull(tcp);
696 mutex_exit(&tcp->tcp_non_sq_lock);
699 * TODO: you can't just flush these, you have to increase rwnd for one
700 * thing. For another, how should urgent data interact?
702 if (fval & FLUSHR) {
703 *mp->b_rptr = fval & ~FLUSHW;
704 /* XXX */
705 qreply(q, mp);
706 return;
708 freemsg(mp);
712 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA
713 * messages.
715 static void
716 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp)
718 mblk_t *mp1;
719 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
720 STRUCT_HANDLE(strbuf, sb);
721 uint_t addrlen;
722 conn_t *connp = tcp->tcp_connp;
723 queue_t *q = connp->conn_wq;
725 /* Make sure it is one of ours. */
726 switch (iocp->ioc_cmd) {
727 case TI_GETMYNAME:
728 case TI_GETPEERNAME:
729 break;
730 default:
732 * If the conn is closing, then error the ioctl here. Otherwise
733 * use the CONN_IOCTLREF_* macros to hold off tcp_close until
734 * we're done here.
736 mutex_enter(&connp->conn_lock);
737 if (connp->conn_state_flags & CONN_CLOSING) {
738 mutex_exit(&connp->conn_lock);
739 iocp->ioc_error = EINVAL;
740 mp->b_datap->db_type = M_IOCNAK;
741 iocp->ioc_count = 0;
742 qreply(q, mp);
743 return;
746 CONN_INC_IOCTLREF_LOCKED(connp);
747 ip_wput_nondata(q, mp);
748 CONN_DEC_IOCTLREF(connp);
749 return;
751 switch (mi_copy_state(q, mp, &mp1)) {
752 case -1:
753 return;
754 case MI_COPY_CASE(MI_COPY_IN, 1):
755 break;
756 case MI_COPY_CASE(MI_COPY_OUT, 1):
757 /* Copy out the strbuf. */
758 mi_copyout(q, mp);
759 return;
760 case MI_COPY_CASE(MI_COPY_OUT, 2):
761 /* All done. */
762 mi_copy_done(q, mp, 0);
763 return;
764 default:
765 mi_copy_done(q, mp, EPROTO);
766 return;
768 /* Check alignment of the strbuf */
769 if (!OK_32PTR(mp1->b_rptr)) {
770 mi_copy_done(q, mp, EINVAL);
771 return;
774 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr);
776 if (connp->conn_family == AF_INET)
777 addrlen = sizeof (sin_t);
778 else
779 addrlen = sizeof (sin6_t);
781 if (STRUCT_FGET(sb, maxlen) < addrlen) {
782 mi_copy_done(q, mp, EINVAL);
783 return;
786 switch (iocp->ioc_cmd) {
787 case TI_GETMYNAME:
788 break;
789 case TI_GETPEERNAME:
790 if (tcp->tcp_state < TCPS_SYN_RCVD) {
791 mi_copy_done(q, mp, ENOTCONN);
792 return;
794 break;
796 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE);
797 if (!mp1)
798 return;
800 STRUCT_FSET(sb, len, addrlen);
801 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
802 case TI_GETMYNAME:
803 (void) conn_getsockname(connp, (struct sockaddr *)mp1->b_wptr,
804 &addrlen);
805 break;
806 case TI_GETPEERNAME:
807 (void) conn_getpeername(connp, (struct sockaddr *)mp1->b_wptr,
808 &addrlen);
809 break;
811 mp1->b_wptr += addrlen;
812 /* Copy out the address */
813 mi_copyout(q, mp);
817 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL
818 * messages.
820 /* ARGSUSED */
821 static void
822 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
824 conn_t *connp = (conn_t *)arg;
825 tcp_t *tcp = connp->conn_tcp;
826 queue_t *q = connp->conn_wq;
827 struct iocblk *iocp;
829 ASSERT(DB_TYPE(mp) == M_IOCTL);
831 * Try and ASSERT the minimum possible references on the
832 * conn early enough. Since we are executing on write side,
833 * the connection is obviously not detached and that means
834 * there is a ref each for TCP and IP. Since we are behind
835 * the squeue, the minimum references needed are 3. If the
836 * conn is in classifier hash list, there should be an
837 * extra ref for that (we check both the possibilities).
839 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) ||
840 (connp->conn_fanout == NULL && connp->conn_ref >= 3));
842 iocp = (struct iocblk *)mp->b_rptr;
843 switch (iocp->ioc_cmd) {
844 case _SIOCSOCKFALLBACK:
846 * Either sockmod is about to be popped and the socket
847 * would now be treated as a plain stream, or a module
848 * is about to be pushed so we could no longer use read-
849 * side synchronous streams for fused loopback tcp.
850 * Drain any queued data and disable direct sockfs
851 * interface from now on.
853 if (!tcp->tcp_issocket) {
854 DB_TYPE(mp) = M_IOCNAK;
855 iocp->ioc_error = EINVAL;
856 } else {
857 tcp_use_pure_tpi(tcp);
858 DB_TYPE(mp) = M_IOCACK;
859 iocp->ioc_error = 0;
861 iocp->ioc_count = 0;
862 iocp->ioc_rval = 0;
863 qreply(q, mp);
864 return;
868 * If the conn is closing, then error the ioctl here. Otherwise bump the
869 * conn_ioctlref to hold off tcp_close until we're done here.
871 mutex_enter(&(connp)->conn_lock);
872 if ((connp)->conn_state_flags & CONN_CLOSING) {
873 mutex_exit(&(connp)->conn_lock);
874 iocp->ioc_error = EINVAL;
875 mp->b_datap->db_type = M_IOCNAK;
876 iocp->ioc_count = 0;
877 qreply(q, mp);
878 return;
881 CONN_INC_IOCTLREF_LOCKED(connp);
882 ip_wput_nondata(q, mp);
883 CONN_DEC_IOCTLREF(connp);
887 * This routine is called by tcp_wput() to handle all TPI requests.
889 /* ARGSUSED */
890 static void
891 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
893 conn_t *connp = (conn_t *)arg;
894 tcp_t *tcp = connp->conn_tcp;
895 union T_primitives *tprim = (union T_primitives *)mp->b_rptr;
896 uchar_t *rptr;
897 t_scalar_t type;
898 cred_t *cr;
901 * Try and ASSERT the minimum possible references on the
902 * conn early enough. Since we are executing on write side,
903 * the connection is obviously not detached and that means
904 * there is a ref each for TCP and IP. Since we are behind
905 * the squeue, the minimum references needed are 3. If the
906 * conn is in classifier hash list, there should be an
907 * extra ref for that (we check both the possibilities).
909 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) ||
910 (connp->conn_fanout == NULL && connp->conn_ref >= 3));
912 rptr = mp->b_rptr;
913 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX);
914 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) {
915 type = ((union T_primitives *)rptr)->type;
916 if (type == T_EXDATA_REQ) {
917 tcp_output_urgent(connp, mp, arg2, NULL);
918 } else if (type != T_DATA_REQ) {
919 goto non_urgent_data;
920 } else {
921 /* TODO: options, flags, ... from user */
922 /* Set length to zero for reclamation below */
923 tcp_wput_data(tcp, mp->b_cont, B_TRUE);
924 freeb(mp);
926 return;
927 } else {
928 if (connp->conn_debug) {
929 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
930 "tcp_wput_proto, dropping one...");
932 freemsg(mp);
933 return;
936 non_urgent_data:
938 switch ((int)tprim->type) {
939 case O_T_BIND_REQ: /* bind request */
940 case T_BIND_REQ: /* new semantics bind request */
941 tcp_tpi_bind(tcp, mp);
942 break;
943 case T_UNBIND_REQ: /* unbind request */
944 tcp_tpi_unbind(tcp, mp);
945 break;
946 case O_T_CONN_RES: /* old connection response XXX */
947 case T_CONN_RES: /* connection response */
948 tcp_tli_accept(tcp, mp);
949 break;
950 case T_CONN_REQ: /* connection request */
951 tcp_tpi_connect(tcp, mp);
952 break;
953 case T_DISCON_REQ: /* disconnect request */
954 tcp_disconnect(tcp, mp);
955 break;
956 case T_CAPABILITY_REQ:
957 tcp_capability_req(tcp, mp); /* capability request */
958 break;
959 case T_INFO_REQ: /* information request */
960 tcp_info_req(tcp, mp);
961 break;
962 case T_SVR4_OPTMGMT_REQ: /* manage options req */
963 case T_OPTMGMT_REQ:
965 * Note: no support for snmpcom_req() through new
966 * T_OPTMGMT_REQ. See comments in ip.c
970 * All Solaris components should pass a db_credp
971 * for this TPI message, hence we ASSERT.
972 * But in case there is some other M_PROTO that looks
973 * like a TPI message sent by some other kernel
974 * component, we check and return an error.
976 cr = msg_getcred(mp, NULL);
977 ASSERT(cr != NULL);
978 if (cr == NULL) {
979 tcp_err_ack(tcp, mp, TSYSERR, EINVAL);
980 return;
983 * If EINPROGRESS is returned, the request has been queued
984 * for subsequent processing by ip_restart_optmgmt(), which
985 * will do the CONN_DEC_REF().
987 if ((int)tprim->type == T_SVR4_OPTMGMT_REQ) {
988 svr4_optcom_req(connp->conn_wq, mp, cr, &tcp_opt_obj);
989 } else {
990 tpi_optcom_req(connp->conn_wq, mp, cr, &tcp_opt_obj);
992 break;
994 case T_UNITDATA_REQ: /* unitdata request */
995 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0);
996 break;
997 case T_ORDREL_REQ: /* orderly release req */
998 freemsg(mp);
1000 if (tcp->tcp_fused)
1001 tcp_unfuse(tcp);
1003 if (tcp_xmit_end(tcp) != 0) {
1005 * We were crossing FINs and got a reset from
1006 * the other side. Just ignore it.
1008 if (connp->conn_debug) {
1009 (void) strlog(TCP_MOD_ID, 0, 1,
1010 SL_ERROR|SL_TRACE,
1011 "tcp_wput_proto, T_ORDREL_REQ out of "
1012 "state %s",
1013 tcp_display(tcp, NULL,
1014 DISP_ADDR_AND_PORT));
1017 break;
1018 case T_ADDR_REQ:
1019 tcp_addr_req(tcp, mp);
1020 break;
1021 default:
1022 if (connp->conn_debug) {
1023 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
1024 "tcp_wput_proto, bogus TPI msg, type %d",
1025 tprim->type);
1028 * We used to M_ERROR. Sending TNOTSUPPORT gives the user
1029 * to recover.
1031 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0);
1032 break;
1037 * Handle special out-of-band ioctl requests (see PSARC/2008/265).
1039 static void
1040 tcp_wput_cmdblk(queue_t *q, mblk_t *mp)
1042 void *data;
1043 mblk_t *datamp = mp->b_cont;
1044 conn_t *connp = Q_TO_CONN(q);
1045 tcp_t *tcp = connp->conn_tcp;
1046 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr;
1048 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) {
1049 cmdp->cb_error = EPROTO;
1050 qreply(q, mp);
1051 return;
1054 data = datamp->b_rptr;
1056 switch (cmdp->cb_cmd) {
1057 case TI_GETPEERNAME:
1058 if (tcp->tcp_state < TCPS_SYN_RCVD)
1059 cmdp->cb_error = ENOTCONN;
1060 else
1061 cmdp->cb_error = conn_getpeername(connp, data,
1062 &cmdp->cb_len);
1063 break;
1064 case TI_GETMYNAME:
1065 cmdp->cb_error = conn_getsockname(connp, data, &cmdp->cb_len);
1066 break;
1067 default:
1068 cmdp->cb_error = EINVAL;
1069 break;
1072 qreply(q, mp);
1076 * The TCP fast path write put procedure.
1077 * NOTE: the logic of the fast path is duplicated from tcp_wput_data()
1079 /* ARGSUSED */
1080 void
1081 tcp_output(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1083 int len;
1084 int hdrlen;
1085 int plen;
1086 mblk_t *mp1;
1087 uchar_t *rptr;
1088 uint32_t snxt;
1089 tcpha_t *tcpha;
1090 struct datab *db;
1091 uint32_t suna;
1092 uint32_t mss;
1093 ipaddr_t *dst;
1094 ipaddr_t *src;
1095 uint32_t sum;
1096 int usable;
1097 conn_t *connp = (conn_t *)arg;
1098 tcp_t *tcp = connp->conn_tcp;
1099 uint32_t msize;
1100 tcp_stack_t *tcps = tcp->tcp_tcps;
1101 ip_xmit_attr_t *ixa;
1102 clock_t now;
1105 * Try and ASSERT the minimum possible references on the
1106 * conn early enough. Since we are executing on write side,
1107 * the connection is obviously not detached and that means
1108 * there is a ref each for TCP and IP. Since we are behind
1109 * the squeue, the minimum references needed are 3. If the
1110 * conn is in classifier hash list, there should be an
1111 * extra ref for that (we check both the possibilities).
1113 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) ||
1114 (connp->conn_fanout == NULL && connp->conn_ref >= 3));
1116 ASSERT(DB_TYPE(mp) == M_DATA);
1117 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp);
1119 mutex_enter(&tcp->tcp_non_sq_lock);
1120 tcp->tcp_squeue_bytes -= msize;
1121 mutex_exit(&tcp->tcp_non_sq_lock);
1123 /* Bypass tcp protocol for fused tcp loopback */
1124 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize))
1125 return;
1127 mss = tcp->tcp_mss;
1129 * If ZEROCOPY has turned off, try not to send any zero-copy message
1130 * down. Do backoff, now.
1132 if (tcp->tcp_snd_zcopy_aware && !tcp->tcp_snd_zcopy_on)
1133 mp = tcp_zcopy_backoff(tcp, mp, B_FALSE);
1136 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX);
1137 len = (int)(mp->b_wptr - mp->b_rptr);
1140 * Criteria for fast path:
1142 * 1. no unsent data
1143 * 2. single mblk in request
1144 * 3. connection established
1145 * 4. data in mblk
1146 * 5. len <= mss
1147 * 6. no tcp_valid bits
1149 if ((tcp->tcp_unsent != 0) ||
1150 (tcp->tcp_cork) ||
1151 (mp->b_cont != NULL) ||
1152 (tcp->tcp_state != TCPS_ESTABLISHED) ||
1153 (len == 0) ||
1154 (len > mss) ||
1155 (tcp->tcp_valid_bits != 0)) {
1156 tcp_wput_data(tcp, mp, B_FALSE);
1157 return;
1160 ASSERT(tcp->tcp_xmit_tail_unsent == 0);
1161 ASSERT(tcp->tcp_fin_sent == 0);
1163 /* queue new packet onto retransmission queue */
1164 if (tcp->tcp_xmit_head == NULL) {
1165 tcp->tcp_xmit_head = mp;
1166 } else {
1167 tcp->tcp_xmit_last->b_cont = mp;
1169 tcp->tcp_xmit_last = mp;
1170 tcp->tcp_xmit_tail = mp;
1172 /* find out how much we can send */
1173 /* BEGIN CSTYLED */
1175 * un-acked usable
1176 * |--------------|-----------------|
1177 * tcp_suna tcp_snxt tcp_suna+tcp_swnd
1179 /* END CSTYLED */
1181 /* start sending from tcp_snxt */
1182 snxt = tcp->tcp_snxt;
1185 * Check to see if this connection has been idled for some
1186 * time and no ACK is expected. If it is, we need to slow
1187 * start again to get back the connection's "self-clock" as
1188 * described in VJ's paper.
1190 * Reinitialize tcp_cwnd after idle.
1192 now = LBOLT_FASTPATH;
1193 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet &&
1194 (TICK_TO_MSEC(now - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) {
1195 TCP_SET_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle);
1198 usable = tcp->tcp_swnd; /* tcp window size */
1199 if (usable > tcp->tcp_cwnd)
1200 usable = tcp->tcp_cwnd; /* congestion window smaller */
1201 usable -= snxt; /* subtract stuff already sent */
1202 suna = tcp->tcp_suna;
1203 usable += suna;
1204 /* usable can be < 0 if the congestion window is smaller */
1205 if (len > usable) {
1206 /* Can't send complete M_DATA in one shot */
1207 goto slow;
1210 mutex_enter(&tcp->tcp_non_sq_lock);
1211 if (tcp->tcp_flow_stopped &&
1212 TCP_UNSENT_BYTES(tcp) <= connp->conn_sndlowat) {
1213 tcp_clrqfull(tcp);
1215 mutex_exit(&tcp->tcp_non_sq_lock);
1218 * determine if anything to send (Nagle).
1220 * 1. len < tcp_mss (i.e. small)
1221 * 2. unacknowledged data present
1222 * 3. len < nagle limit
1223 * 4. last packet sent < nagle limit (previous packet sent)
1225 if ((len < mss) && (snxt != suna) &&
1226 (len < (int)tcp->tcp_naglim) &&
1227 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) {
1229 * This was the first unsent packet and normally
1230 * mss < xmit_hiwater so there is no need to worry
1231 * about flow control. The next packet will go
1232 * through the flow control check in tcp_wput_data().
1234 /* leftover work from above */
1235 tcp->tcp_unsent = len;
1236 tcp->tcp_xmit_tail_unsent = len;
1238 return;
1242 * len <= tcp->tcp_mss && len == unsent so no sender silly window. Can
1243 * send now.
1246 if (snxt == suna) {
1247 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
1250 /* we have always sent something */
1251 tcp->tcp_rack_cnt = 0;
1253 tcp->tcp_snxt = snxt + len;
1254 tcp->tcp_rack = tcp->tcp_rnxt;
1256 if ((mp1 = dupb(mp)) == 0)
1257 goto no_memory;
1258 mp->b_prev = (mblk_t *)(uintptr_t)now;
1259 mp->b_next = (mblk_t *)(uintptr_t)snxt;
1261 /* adjust tcp header information */
1262 tcpha = tcp->tcp_tcpha;
1263 tcpha->tha_flags = (TH_ACK|TH_PUSH);
1265 sum = len + connp->conn_ht_ulp_len + connp->conn_sum;
1266 sum = (sum >> 16) + (sum & 0xFFFF);
1267 tcpha->tha_sum = htons(sum);
1269 tcpha->tha_seq = htonl(snxt);
1271 TCPS_BUMP_MIB(tcps, tcpOutDataSegs);
1272 TCPS_UPDATE_MIB(tcps, tcpOutDataBytes, len);
1273 BUMP_LOCAL(tcp->tcp_obsegs);
1275 /* Update the latest receive window size in TCP header. */
1276 tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
1278 tcp->tcp_last_sent_len = (ushort_t)len;
1280 plen = len + connp->conn_ht_iphc_len;
1282 ixa = connp->conn_ixa;
1283 ixa->ixa_pktlen = plen;
1285 if (ixa->ixa_flags & IXAF_IS_IPV4) {
1286 tcp->tcp_ipha->ipha_length = htons(plen);
1287 } else {
1288 tcp->tcp_ip6h->ip6_plen = htons(plen - IPV6_HDR_LEN);
1291 /* see if we need to allocate a mblk for the headers */
1292 hdrlen = connp->conn_ht_iphc_len;
1293 rptr = mp1->b_rptr - hdrlen;
1294 db = mp1->b_datap;
1295 if ((db->db_ref != 2) || rptr < db->db_base ||
1296 (!OK_32PTR(rptr))) {
1297 /* NOTE: we assume allocb returns an OK_32PTR */
1298 mp = allocb(hdrlen + tcps->tcps_wroff_xtra, BPRI_MED);
1299 if (!mp) {
1300 freemsg(mp1);
1301 goto no_memory;
1303 mp->b_cont = mp1;
1304 mp1 = mp;
1305 /* Leave room for Link Level header */
1306 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra];
1307 mp1->b_wptr = &rptr[hdrlen];
1309 mp1->b_rptr = rptr;
1311 /* Fill in the timestamp option. */
1312 if (tcp->tcp_snd_ts_ok) {
1313 uint32_t llbolt = (uint32_t)LBOLT_FASTPATH;
1315 U32_TO_BE32(llbolt,
1316 (char *)tcpha + TCP_MIN_HEADER_LENGTH+4);
1317 U32_TO_BE32(tcp->tcp_ts_recent,
1318 (char *)tcpha + TCP_MIN_HEADER_LENGTH+8);
1319 } else {
1320 ASSERT(connp->conn_ht_ulp_len == TCP_MIN_HEADER_LENGTH);
1323 /* copy header into outgoing packet */
1324 dst = (ipaddr_t *)rptr;
1325 src = (ipaddr_t *)connp->conn_ht_iphc;
1326 dst[0] = src[0];
1327 dst[1] = src[1];
1328 dst[2] = src[2];
1329 dst[3] = src[3];
1330 dst[4] = src[4];
1331 dst[5] = src[5];
1332 dst[6] = src[6];
1333 dst[7] = src[7];
1334 dst[8] = src[8];
1335 dst[9] = src[9];
1336 if (hdrlen -= 40) {
1337 hdrlen >>= 2;
1338 dst += 10;
1339 src += 10;
1340 do {
1341 *dst++ = *src++;
1342 } while (--hdrlen);
1346 * Set the ECN info in the TCP header. Note that this
1347 * is not the template header.
1349 if (tcp->tcp_ecn_ok) {
1350 TCP_SET_ECT(tcp, rptr);
1352 tcpha = (tcpha_t *)(rptr + ixa->ixa_ip_hdr_length);
1353 if (tcp->tcp_ecn_echo_on)
1354 tcpha->tha_flags |= TH_ECE;
1355 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) {
1356 tcpha->tha_flags |= TH_CWR;
1357 tcp->tcp_ecn_cwr_sent = B_TRUE;
1361 if (tcp->tcp_ip_forward_progress) {
1362 tcp->tcp_ip_forward_progress = B_FALSE;
1363 connp->conn_ixa->ixa_flags |= IXAF_REACH_CONF;
1364 } else {
1365 connp->conn_ixa->ixa_flags &= ~IXAF_REACH_CONF;
1367 tcp_send_data(tcp, mp1);
1368 return;
1371 * If we ran out of memory, we pretend to have sent the packet
1372 * and that it was lost on the wire.
1374 no_memory:
1375 return;
1377 slow:
1378 /* leftover work from above */
1379 tcp->tcp_unsent = len;
1380 tcp->tcp_xmit_tail_unsent = len;
1381 tcp_wput_data(tcp, NULL, B_FALSE);
1384 /* ARGSUSED2 */
1385 void
1386 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1388 int len;
1389 uint32_t msize;
1390 conn_t *connp = (conn_t *)arg;
1391 tcp_t *tcp = connp->conn_tcp;
1393 msize = msgdsize(mp);
1395 len = msize - 1;
1396 if (len < 0) {
1397 freemsg(mp);
1398 return;
1402 * Try to force urgent data out on the wire. Even if we have unsent
1403 * data this will at least send the urgent flag.
1404 * XXX does not handle more flag correctly.
1406 len += tcp->tcp_unsent;
1407 len += tcp->tcp_snxt;
1408 tcp->tcp_urg = len;
1409 tcp->tcp_valid_bits |= TCP_URG_VALID;
1411 /* Bypass tcp protocol for fused tcp loopback */
1412 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize))
1413 return;
1415 /* Strip off the T_EXDATA_REQ if the data is from TPI */
1416 if (DB_TYPE(mp) != M_DATA) {
1417 mblk_t *mp1 = mp;
1418 ASSERT(!IPCL_IS_NONSTR(connp));
1419 mp = mp->b_cont;
1420 freeb(mp1);
1422 tcp_wput_data(tcp, mp, B_TRUE);
1426 * Called by streams close routine via squeues when our client blows off its
1427 * descriptor, we take this to mean: "close the stream state NOW, close the tcp
1428 * connection politely" When SO_LINGER is set (with a non-zero linger time and
1429 * it is not a nonblocking socket) then this routine sleeps until the FIN is
1430 * acked.
1432 * NOTE: tcp_close potentially returns error when lingering.
1433 * However, the stream head currently does not pass these errors
1434 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK
1435 * errors to the application (from tsleep()) and not errors
1436 * like ECONNRESET caused by receiving a reset packet.
1439 /* ARGSUSED */
1440 void
1441 tcp_close_output(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1443 char *msg;
1444 conn_t *connp = (conn_t *)arg;
1445 tcp_t *tcp = connp->conn_tcp;
1446 clock_t delta = 0;
1447 tcp_stack_t *tcps = tcp->tcp_tcps;
1450 * When a non-STREAMS socket is being closed, it does not always
1451 * stick around waiting for tcp_close_output to run and can therefore
1452 * have dropped a reference already. So adjust the asserts accordingly.
1454 ASSERT((connp->conn_fanout != NULL &&
1455 connp->conn_ref >= (IPCL_IS_NONSTR(connp) ? 3 : 4)) ||
1456 (connp->conn_fanout == NULL &&
1457 connp->conn_ref >= (IPCL_IS_NONSTR(connp) ? 2 : 3)));
1459 mutex_enter(&tcp->tcp_eager_lock);
1460 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) {
1462 * Cleanup for listener. For non-STREAM sockets sockfs will
1463 * close all the eagers on 'q', so in that case only deal
1464 * with 'q0'.
1466 tcp_eager_cleanup(tcp, IPCL_IS_NONSTR(connp) ? 1 : 0);
1467 tcp->tcp_wait_for_eagers = 1;
1469 mutex_exit(&tcp->tcp_eager_lock);
1471 tcp->tcp_lso = B_FALSE;
1473 msg = NULL;
1474 switch (tcp->tcp_state) {
1475 case TCPS_CLOSED:
1476 case TCPS_IDLE:
1477 break;
1478 case TCPS_BOUND:
1479 if (tcp->tcp_listener != NULL) {
1480 ASSERT(IPCL_IS_NONSTR(connp));
1482 * Unlink from the listener and drop the reference
1483 * put on it by the eager. tcp_closei_local will not
1484 * do it because tcp_tconnind_started is TRUE.
1486 mutex_enter(&tcp->tcp_saved_listener->tcp_eager_lock);
1487 tcp_eager_unlink(tcp);
1488 mutex_exit(&tcp->tcp_saved_listener->tcp_eager_lock);
1489 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp);
1491 break;
1492 case TCPS_LISTEN:
1493 break;
1494 case TCPS_SYN_SENT:
1495 msg = "tcp_close, during connect";
1496 break;
1497 case TCPS_SYN_RCVD:
1499 * Close during the connect 3-way handshake
1500 * but here there may or may not be pending data
1501 * already on queue. Process almost same as in
1502 * the ESTABLISHED state.
1504 /* FALLTHRU */
1505 default:
1506 if (tcp->tcp_fused)
1507 tcp_unfuse(tcp);
1510 * If SO_LINGER has set a zero linger time, abort the
1511 * connection with a reset.
1513 if (connp->conn_linger && connp->conn_lingertime == 0) {
1514 msg = "tcp_close, zero lingertime";
1515 break;
1519 * Abort connection if there is unread data queued.
1521 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) {
1522 msg = "tcp_close, unread data";
1523 break;
1527 * Abort connection if it is being closed without first
1528 * being accepted. This can happen if a listening non-STREAM
1529 * socket wants to get rid of the socket, for example, if the
1530 * listener is closing.
1532 if (tcp->tcp_listener != NULL) {
1533 ASSERT(IPCL_IS_NONSTR(connp));
1534 msg = "tcp_close, close before accept";
1537 * Unlink from the listener and drop the reference
1538 * put on it by the eager. tcp_closei_local will not
1539 * do it because tcp_tconnind_started is TRUE.
1541 mutex_enter(&tcp->tcp_saved_listener->tcp_eager_lock);
1542 tcp_eager_unlink(tcp);
1543 mutex_exit(&tcp->tcp_saved_listener->tcp_eager_lock);
1544 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp);
1545 break;
1549 * Transmit the FIN before detaching the tcp_t.
1550 * After tcp_detach returns this queue/perimeter
1551 * no longer owns the tcp_t thus others can modify it.
1553 (void) tcp_xmit_end(tcp);
1556 * If lingering on close then wait until the fin is acked,
1557 * the SO_LINGER time passes, or a reset is sent/received.
1559 if (connp->conn_linger && connp->conn_lingertime > 0 &&
1560 !(tcp->tcp_fin_acked) &&
1561 tcp->tcp_state >= TCPS_ESTABLISHED) {
1562 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) {
1563 tcp->tcp_client_errno = EWOULDBLOCK;
1564 } else if (tcp->tcp_client_errno == 0) {
1566 ASSERT(tcp->tcp_linger_tid == 0);
1568 /* conn_lingertime is in sec. */
1569 tcp->tcp_linger_tid = TCP_TIMER(tcp,
1570 tcp_close_linger_timeout,
1571 connp->conn_lingertime * MILLISEC);
1573 /* tcp_close_linger_timeout will finish close */
1574 if (tcp->tcp_linger_tid == 0)
1575 tcp->tcp_client_errno = ENOSR;
1576 else
1577 return;
1581 * Check if we need to detach or just close
1582 * the instance.
1584 if (tcp->tcp_state <= TCPS_LISTEN)
1585 break;
1589 * Make sure that no other thread will access the conn_rq of
1590 * this instance (through lookups etc.) as conn_rq will go
1591 * away shortly.
1593 tcp_acceptor_hash_remove(tcp);
1595 mutex_enter(&tcp->tcp_non_sq_lock);
1596 if (tcp->tcp_flow_stopped) {
1597 tcp_clrqfull(tcp);
1599 mutex_exit(&tcp->tcp_non_sq_lock);
1601 if (tcp->tcp_timer_tid != 0) {
1602 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid);
1603 tcp->tcp_timer_tid = 0;
1606 * Need to cancel those timers which will not be used when
1607 * TCP is detached. This has to be done before the conn_wq
1608 * is set to NULL.
1610 tcp_timers_stop(tcp);
1612 tcp->tcp_detached = B_TRUE;
1613 if (tcp->tcp_state == TCPS_TIME_WAIT) {
1614 tcp_time_wait_append(tcp);
1615 TCP_DBGSTAT(tcps, tcp_detach_time_wait);
1616 ASSERT(connp->conn_ref >=
1617 (IPCL_IS_NONSTR(connp) ? 2 : 3));
1618 goto finish;
1622 * If delta is zero the timer event wasn't executed and was
1623 * successfully canceled. In this case we need to restart it
1624 * with the minimal delta possible.
1626 if (delta >= 0)
1627 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer,
1628 delta ? delta : 1);
1630 ASSERT(connp->conn_ref >= (IPCL_IS_NONSTR(connp) ? 2 : 3));
1631 goto finish;
1634 /* Detach did not complete. Still need to remove q from stream. */
1635 if (msg) {
1636 if (tcp->tcp_state == TCPS_ESTABLISHED ||
1637 tcp->tcp_state == TCPS_CLOSE_WAIT)
1638 TCPS_BUMP_MIB(tcps, tcpEstabResets);
1639 if (tcp->tcp_state == TCPS_SYN_SENT ||
1640 tcp->tcp_state == TCPS_SYN_RCVD)
1641 TCPS_BUMP_MIB(tcps, tcpAttemptFails);
1642 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST);
1645 tcp_closei_local(tcp);
1646 CONN_DEC_REF(connp);
1647 ASSERT(connp->conn_ref >= (IPCL_IS_NONSTR(connp) ? 1 : 2));
1649 finish:
1651 * Don't change the queues in the case of a listener that has
1652 * eagers in its q or q0. It could surprise the eagers.
1653 * Instead wait for the eagers outside the squeue.
1655 * For non-STREAMS sockets tcp_wait_for_eagers implies that
1656 * we should delay the su_closed upcall until all eagers have
1657 * dropped their references.
1659 if (!tcp->tcp_wait_for_eagers) {
1660 tcp->tcp_detached = B_TRUE;
1661 connp->conn_rq = NULL;
1662 connp->conn_wq = NULL;
1664 /* non-STREAM socket, release the upper handle */
1665 if (IPCL_IS_NONSTR(connp)) {
1666 ASSERT(connp->conn_upper_handle != NULL);
1667 (*connp->conn_upcalls->su_closed)
1668 (connp->conn_upper_handle);
1669 connp->conn_upper_handle = NULL;
1670 connp->conn_upcalls = NULL;
1674 /* Signal tcp_close() to finish closing. */
1675 mutex_enter(&tcp->tcp_closelock);
1676 tcp->tcp_closed = 1;
1677 cv_signal(&tcp->tcp_closecv);
1678 mutex_exit(&tcp->tcp_closelock);
1681 /* ARGSUSED */
1682 void
1683 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1685 conn_t *connp = (conn_t *)arg;
1686 tcp_t *tcp = connp->conn_tcp;
1688 freemsg(mp);
1690 if (tcp->tcp_fused)
1691 tcp_unfuse(tcp);
1693 if (tcp_xmit_end(tcp) != 0) {
1695 * We were crossing FINs and got a reset from
1696 * the other side. Just ignore it.
1698 if (connp->conn_debug) {
1699 (void) strlog(TCP_MOD_ID, 0, 1,
1700 SL_ERROR|SL_TRACE,
1701 "tcp_shutdown_output() out of state %s",
1702 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT));
1707 #pragma inline(tcp_send_data)
1709 void
1710 tcp_send_data(tcp_t *tcp, mblk_t *mp)
1712 conn_t *connp = tcp->tcp_connp;
1715 * Check here to avoid sending zero-copy message down to IP when
1716 * ZEROCOPY capability has turned off. We only need to deal with
1717 * the race condition between sockfs and the notification here.
1718 * Since we have tried to backoff the tcp_xmit_head when turning
1719 * zero-copy off and new messages in tcp_output(), we simply drop
1720 * the dup'ed packet here and let tcp retransmit, if tcp_xmit_zc_clean
1721 * is not true.
1723 if (tcp->tcp_snd_zcopy_aware && !tcp->tcp_snd_zcopy_on &&
1724 !tcp->tcp_xmit_zc_clean) {
1725 ip_drop_output("TCP ZC was disabled but not clean", mp, NULL);
1726 freemsg(mp);
1727 return;
1730 DTRACE_TCP5(send, mblk_t *, NULL, ip_xmit_attr_t *, connp->conn_ixa,
1731 __dtrace_tcp_void_ip_t *, mp->b_rptr, tcp_t *, tcp,
1732 __dtrace_tcp_tcph_t *,
1733 &mp->b_rptr[connp->conn_ixa->ixa_ip_hdr_length]);
1735 ASSERT(connp->conn_ixa->ixa_notify_cookie == connp->conn_tcp);
1736 (void) conn_ip_output(mp, connp->conn_ixa);
1739 /* ARGSUSED2 */
1740 void
1741 tcp_send_synack(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1743 conn_t *econnp = (conn_t *)arg;
1744 tcp_t *tcp = econnp->conn_tcp;
1745 ip_xmit_attr_t *ixa = econnp->conn_ixa;
1747 /* Guard against a RST having blown it away while on the squeue */
1748 if (tcp->tcp_state == TCPS_CLOSED) {
1749 freemsg(mp);
1750 return;
1754 * In the off-chance that the eager received and responded to
1755 * some other packet while the SYN|ACK was queued, we recalculate
1756 * the ixa_pktlen. It would be better to fix the SYN/accept
1757 * multithreading scheme to avoid this complexity.
1759 ixa->ixa_pktlen = msgdsize(mp);
1760 (void) conn_ip_output(mp, ixa);
1764 * tcp_send() is called by tcp_wput_data() and returns one of the following:
1766 * -1 = failed allocation.
1767 * 0 = We've either successfully sent data, or our usable send window is too
1768 * small and we'd rather wait until later before sending again.
1770 static int
1771 tcp_send(tcp_t *tcp, const int mss, const int total_hdr_len,
1772 const int tcp_hdr_len, const int num_sack_blk, int *usable,
1773 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time)
1775 int num_lso_seg = 1;
1776 uint_t lso_usable;
1777 boolean_t do_lso_send = B_FALSE;
1778 tcp_stack_t *tcps = tcp->tcp_tcps;
1779 conn_t *connp = tcp->tcp_connp;
1780 ip_xmit_attr_t *ixa = connp->conn_ixa;
1783 * Check LSO possibility. The value of tcp->tcp_lso indicates whether
1784 * the underlying connection is LSO capable. Will check whether having
1785 * enough available data to initiate LSO transmission in the for(){}
1786 * loops.
1788 if (tcp->tcp_lso && (tcp->tcp_valid_bits & ~TCP_FSS_VALID) == 0)
1789 do_lso_send = B_TRUE;
1791 for (;;) {
1792 struct datab *db;
1793 tcpha_t *tcpha;
1794 uint32_t sum;
1795 mblk_t *mp, *mp1;
1796 uchar_t *rptr;
1797 int len;
1800 * Calculate the maximum payload length we can send at one
1801 * time.
1803 if (do_lso_send) {
1805 * Determine whether or not it's possible to do LSO,
1806 * and if so, how much data we can send.
1808 if ((*usable - 1) / mss >= 1) {
1809 lso_usable = MIN(tcp->tcp_lso_max, *usable);
1810 num_lso_seg = lso_usable / mss;
1811 if (lso_usable % mss) {
1812 num_lso_seg++;
1813 tcp->tcp_last_sent_len = (ushort_t)
1814 (lso_usable % mss);
1815 } else {
1816 tcp->tcp_last_sent_len = (ushort_t)mss;
1818 } else {
1819 do_lso_send = B_FALSE;
1820 num_lso_seg = 1;
1821 lso_usable = mss;
1825 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1);
1827 len = mss;
1828 if (len > *usable) {
1829 ASSERT(do_lso_send == B_FALSE);
1831 len = *usable;
1832 if (len <= 0) {
1833 /* Terminate the loop */
1834 break; /* success; too small */
1837 * Sender silly-window avoidance.
1838 * Ignore this if we are going to send a
1839 * zero window probe out.
1841 * TODO: force data into microscopic window?
1842 * ==> (!pushed || (unsent > usable))
1844 if (len < (tcp->tcp_max_swnd >> 1) &&
1845 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len &&
1846 !((tcp->tcp_valid_bits & TCP_URG_VALID) &&
1847 len == 1) && (! tcp->tcp_zero_win_probe)) {
1849 * If the retransmit timer is not running
1850 * we start it so that we will retransmit
1851 * in the case when the receiver has
1852 * decremented the window.
1854 if (*snxt == tcp->tcp_snxt &&
1855 *snxt == tcp->tcp_suna) {
1857 * We are not supposed to send
1858 * anything. So let's wait a little
1859 * bit longer before breaking SWS
1860 * avoidance.
1862 * What should the value be?
1863 * Suggestion: MAX(init rexmit time,
1864 * tcp->tcp_rto)
1866 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
1868 break; /* success; too small */
1872 tcpha = tcp->tcp_tcpha;
1875 * The reason to adjust len here is that we need to set flags
1876 * and calculate checksum.
1878 if (do_lso_send)
1879 len = lso_usable;
1881 *usable -= len; /* Approximate - can be adjusted later */
1882 if (*usable > 0)
1883 tcpha->tha_flags = TH_ACK;
1884 else
1885 tcpha->tha_flags = (TH_ACK | TH_PUSH);
1888 * Prime pump for IP's checksumming on our behalf.
1889 * Include the adjustment for a source route if any.
1890 * In case of LSO, the partial pseudo-header checksum should
1891 * exclusive TCP length, so zero tha_sum before IP calculate
1892 * pseudo-header checksum for partial checksum offload.
1894 if (do_lso_send) {
1895 sum = 0;
1896 } else {
1897 sum = len + tcp_hdr_len + connp->conn_sum;
1898 sum = (sum >> 16) + (sum & 0xFFFF);
1900 tcpha->tha_sum = htons(sum);
1901 tcpha->tha_seq = htonl(*snxt);
1904 * Branch off to tcp_xmit_mp() if any of the VALID bits is
1905 * set. For the case when TCP_FSS_VALID is the only valid
1906 * bit (normal active close), branch off only when we think
1907 * that the FIN flag needs to be set. Note for this case,
1908 * that (snxt + len) may not reflect the actual seg_len,
1909 * as len may be further reduced in tcp_xmit_mp(). If len
1910 * gets modified, we will end up here again.
1912 if (tcp->tcp_valid_bits != 0 &&
1913 (tcp->tcp_valid_bits != TCP_FSS_VALID ||
1914 ((*snxt + len) == tcp->tcp_fss))) {
1915 uchar_t *prev_rptr;
1916 uint32_t prev_snxt = tcp->tcp_snxt;
1918 if (*tail_unsent == 0) {
1919 ASSERT((*xmit_tail)->b_cont != NULL);
1920 *xmit_tail = (*xmit_tail)->b_cont;
1921 prev_rptr = (*xmit_tail)->b_rptr;
1922 *tail_unsent = (int)((*xmit_tail)->b_wptr -
1923 (*xmit_tail)->b_rptr);
1924 } else {
1925 prev_rptr = (*xmit_tail)->b_rptr;
1926 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr -
1927 *tail_unsent;
1929 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL,
1930 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE);
1931 /* Restore tcp_snxt so we get amount sent right. */
1932 tcp->tcp_snxt = prev_snxt;
1933 if (prev_rptr == (*xmit_tail)->b_rptr) {
1935 * If the previous timestamp is still in use,
1936 * don't stomp on it.
1938 if ((*xmit_tail)->b_next == NULL) {
1939 (*xmit_tail)->b_prev = local_time;
1940 (*xmit_tail)->b_next =
1941 (mblk_t *)(uintptr_t)(*snxt);
1943 } else
1944 (*xmit_tail)->b_rptr = prev_rptr;
1946 if (mp == NULL) {
1947 return (-1);
1949 mp1 = mp->b_cont;
1951 if (len <= mss) /* LSO is unusable (!do_lso_send) */
1952 tcp->tcp_last_sent_len = (ushort_t)len;
1953 while (mp1->b_cont) {
1954 *xmit_tail = (*xmit_tail)->b_cont;
1955 (*xmit_tail)->b_prev = local_time;
1956 (*xmit_tail)->b_next =
1957 (mblk_t *)(uintptr_t)(*snxt);
1958 mp1 = mp1->b_cont;
1960 *snxt += len;
1961 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr;
1962 BUMP_LOCAL(tcp->tcp_obsegs);
1963 TCPS_BUMP_MIB(tcps, tcpOutDataSegs);
1964 TCPS_UPDATE_MIB(tcps, tcpOutDataBytes, len);
1965 tcp_send_data(tcp, mp);
1966 continue;
1969 *snxt += len; /* Adjust later if we don't send all of len */
1970 TCPS_BUMP_MIB(tcps, tcpOutDataSegs);
1971 TCPS_UPDATE_MIB(tcps, tcpOutDataBytes, len);
1973 if (*tail_unsent) {
1974 /* Are the bytes above us in flight? */
1975 rptr = (*xmit_tail)->b_wptr - *tail_unsent;
1976 if (rptr != (*xmit_tail)->b_rptr) {
1977 *tail_unsent -= len;
1978 if (len <= mss) /* LSO is unusable */
1979 tcp->tcp_last_sent_len = (ushort_t)len;
1980 len += total_hdr_len;
1981 ixa->ixa_pktlen = len;
1983 if (ixa->ixa_flags & IXAF_IS_IPV4) {
1984 tcp->tcp_ipha->ipha_length = htons(len);
1985 } else {
1986 tcp->tcp_ip6h->ip6_plen =
1987 htons(len - IPV6_HDR_LEN);
1990 mp = dupb(*xmit_tail);
1991 if (mp == NULL) {
1992 return (-1); /* out_of_mem */
1994 mp->b_rptr = rptr;
1996 * If the old timestamp is no longer in use,
1997 * sample a new timestamp now.
1999 if ((*xmit_tail)->b_next == NULL) {
2000 (*xmit_tail)->b_prev = local_time;
2001 (*xmit_tail)->b_next =
2002 (mblk_t *)(uintptr_t)(*snxt-len);
2004 goto must_alloc;
2006 } else {
2007 *xmit_tail = (*xmit_tail)->b_cont;
2008 ASSERT((uintptr_t)((*xmit_tail)->b_wptr -
2009 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX);
2010 *tail_unsent = (int)((*xmit_tail)->b_wptr -
2011 (*xmit_tail)->b_rptr);
2014 (*xmit_tail)->b_prev = local_time;
2015 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len);
2017 *tail_unsent -= len;
2018 if (len <= mss) /* LSO is unusable (!do_lso_send) */
2019 tcp->tcp_last_sent_len = (ushort_t)len;
2021 len += total_hdr_len;
2022 ixa->ixa_pktlen = len;
2024 if (ixa->ixa_flags & IXAF_IS_IPV4) {
2025 tcp->tcp_ipha->ipha_length = htons(len);
2026 } else {
2027 tcp->tcp_ip6h->ip6_plen = htons(len - IPV6_HDR_LEN);
2030 mp = dupb(*xmit_tail);
2031 if (mp == NULL) {
2032 return (-1); /* out_of_mem */
2035 len = total_hdr_len;
2037 * There are four reasons to allocate a new hdr mblk:
2038 * 1) The bytes above us are in use by another packet
2039 * 2) We don't have good alignment
2040 * 3) The mblk is being shared
2041 * 4) We don't have enough room for a header
2043 rptr = mp->b_rptr - len;
2044 if (!OK_32PTR(rptr) ||
2045 ((db = mp->b_datap), db->db_ref != 2) ||
2046 rptr < db->db_base) {
2047 /* NOTE: we assume allocb returns an OK_32PTR */
2049 must_alloc:;
2050 mp1 = allocb(connp->conn_ht_iphc_allocated +
2051 tcps->tcps_wroff_xtra, BPRI_MED);
2052 if (mp1 == NULL) {
2053 freemsg(mp);
2054 return (-1); /* out_of_mem */
2056 mp1->b_cont = mp;
2057 mp = mp1;
2058 /* Leave room for Link Level header */
2059 len = total_hdr_len;
2060 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra];
2061 mp->b_wptr = &rptr[len];
2065 * Fill in the header using the template header, and add
2066 * options such as time-stamp, ECN and/or SACK, as needed.
2068 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk);
2070 mp->b_rptr = rptr;
2072 if (*tail_unsent) {
2073 int spill = *tail_unsent;
2075 mp1 = mp->b_cont;
2076 if (mp1 == NULL)
2077 mp1 = mp;
2080 * If we're a little short, tack on more mblks until
2081 * there is no more spillover.
2083 while (spill < 0) {
2084 mblk_t *nmp;
2085 int nmpsz;
2087 nmp = (*xmit_tail)->b_cont;
2088 nmpsz = MBLKL(nmp);
2091 * Excess data in mblk; can we split it?
2092 * If LSO is enabled for the connection,
2093 * keep on splitting as this is a transient
2094 * send path.
2096 if (!do_lso_send && (spill + nmpsz > 0)) {
2098 * Don't split if stream head was
2099 * told to break up larger writes
2100 * into smaller ones.
2102 if (tcp->tcp_maxpsz_multiplier > 0)
2103 break;
2106 * Next mblk is less than SMSS/2
2107 * rounded up to nearest 64-byte;
2108 * let it get sent as part of the
2109 * next segment.
2111 if (tcp->tcp_localnet &&
2112 !tcp->tcp_cork &&
2113 (nmpsz < roundup((mss >> 1), 64)))
2114 break;
2117 *xmit_tail = nmp;
2118 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX);
2119 /* Stash for rtt use later */
2120 (*xmit_tail)->b_prev = local_time;
2121 (*xmit_tail)->b_next =
2122 (mblk_t *)(uintptr_t)(*snxt - len);
2123 mp1->b_cont = dupb(*xmit_tail);
2124 mp1 = mp1->b_cont;
2126 spill += nmpsz;
2127 if (mp1 == NULL) {
2128 *tail_unsent = spill;
2129 freemsg(mp);
2130 return (-1); /* out_of_mem */
2134 /* Trim back any surplus on the last mblk */
2135 if (spill >= 0) {
2136 mp1->b_wptr -= spill;
2137 *tail_unsent = spill;
2138 } else {
2140 * We did not send everything we could in
2141 * order to remain within the b_cont limit.
2143 *usable -= spill;
2144 *snxt += spill;
2145 tcp->tcp_last_sent_len += spill;
2146 TCPS_UPDATE_MIB(tcps, tcpOutDataBytes, spill);
2148 * Adjust the checksum
2150 tcpha = (tcpha_t *)(rptr +
2151 ixa->ixa_ip_hdr_length);
2152 sum += spill;
2153 sum = (sum >> 16) + (sum & 0xFFFF);
2154 tcpha->tha_sum = htons(sum);
2155 if (connp->conn_ipversion == IPV4_VERSION) {
2156 sum = ntohs(
2157 ((ipha_t *)rptr)->ipha_length) +
2158 spill;
2159 ((ipha_t *)rptr)->ipha_length =
2160 htons(sum);
2161 } else {
2162 sum = ntohs(
2163 ((ip6_t *)rptr)->ip6_plen) +
2164 spill;
2165 ((ip6_t *)rptr)->ip6_plen =
2166 htons(sum);
2168 ixa->ixa_pktlen += spill;
2169 *tail_unsent = 0;
2172 if (tcp->tcp_ip_forward_progress) {
2173 tcp->tcp_ip_forward_progress = B_FALSE;
2174 ixa->ixa_flags |= IXAF_REACH_CONF;
2175 } else {
2176 ixa->ixa_flags &= ~IXAF_REACH_CONF;
2179 if (do_lso_send) {
2180 /* Append LSO information to the mp. */
2181 lso_info_set(mp, mss, HW_LSO);
2182 ixa->ixa_fragsize = IP_MAXPACKET;
2183 ixa->ixa_extra_ident = num_lso_seg - 1;
2185 DTRACE_PROBE2(tcp_send_lso, int, num_lso_seg,
2186 boolean_t, B_TRUE);
2188 tcp_send_data(tcp, mp);
2191 * Restore values of ixa_fragsize and ixa_extra_ident.
2193 ixa->ixa_fragsize = ixa->ixa_pmtu;
2194 ixa->ixa_extra_ident = 0;
2195 tcp->tcp_obsegs += num_lso_seg;
2196 TCP_STAT(tcps, tcp_lso_times);
2197 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg);
2198 } else {
2200 * Make sure to clean up LSO information. Wherever a
2201 * new mp uses the prepended header room after dupb(),
2202 * lso_info_cleanup() should be called.
2204 lso_info_cleanup(mp);
2205 tcp_send_data(tcp, mp);
2206 BUMP_LOCAL(tcp->tcp_obsegs);
2210 return (0);
2214 * Initiate closedown sequence on an active connection. (May be called as
2215 * writer.) Return value zero for OK return, non-zero for error return.
2217 static int
2218 tcp_xmit_end(tcp_t *tcp)
2220 mblk_t *mp;
2221 tcp_stack_t *tcps = tcp->tcp_tcps;
2222 iulp_t uinfo;
2223 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip;
2224 conn_t *connp = tcp->tcp_connp;
2226 if (tcp->tcp_state < TCPS_SYN_RCVD ||
2227 tcp->tcp_state > TCPS_CLOSE_WAIT) {
2229 * Invalid state, only states TCPS_SYN_RCVD,
2230 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid
2232 return (-1);
2235 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent;
2236 tcp->tcp_valid_bits |= TCP_FSS_VALID;
2238 * If there is nothing more unsent, send the FIN now.
2239 * Otherwise, it will go out with the last segment.
2241 if (tcp->tcp_unsent == 0) {
2242 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL,
2243 tcp->tcp_fss, B_FALSE, NULL, B_FALSE);
2245 if (mp) {
2246 tcp_send_data(tcp, mp);
2247 } else {
2249 * Couldn't allocate msg. Pretend we got it out.
2250 * Wait for rexmit timeout.
2252 tcp->tcp_snxt = tcp->tcp_fss + 1;
2253 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
2257 * If needed, update tcp_rexmit_snxt as tcp_snxt is
2258 * changed.
2260 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) {
2261 tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
2263 } else {
2265 * If tcp->tcp_cork is set, then the data will not get sent,
2266 * so we have to check that and unset it first.
2268 if (tcp->tcp_cork)
2269 tcp->tcp_cork = B_FALSE;
2270 tcp_wput_data(tcp, NULL, B_FALSE);
2274 * If TCP does not get enough samples of RTT or tcp_rtt_updates
2275 * is 0, don't update the cache.
2277 if (tcps->tcps_rtt_updates == 0 ||
2278 tcp->tcp_rtt_update < tcps->tcps_rtt_updates)
2279 return (0);
2282 * We do not have a good algorithm to update ssthresh at this time.
2283 * So don't do any update.
2285 bzero(&uinfo, sizeof (uinfo));
2286 uinfo.iulp_rtt = tcp->tcp_rtt_sa;
2287 uinfo.iulp_rtt_sd = tcp->tcp_rtt_sd;
2290 * Note that uinfo is kept for conn_faddr in the DCE. Could update even
2291 * if source routed but we don't.
2293 if (connp->conn_ipversion == IPV4_VERSION) {
2294 if (connp->conn_faddr_v4 != tcp->tcp_ipha->ipha_dst) {
2295 return (0);
2297 (void) dce_update_uinfo_v4(connp->conn_faddr_v4, &uinfo, ipst);
2298 } else {
2299 uint_t ifindex;
2301 if (!(IN6_ARE_ADDR_EQUAL(&connp->conn_faddr_v6,
2302 &tcp->tcp_ip6h->ip6_dst))) {
2303 return (0);
2305 ifindex = 0;
2306 if (IN6_IS_ADDR_LINKSCOPE(&connp->conn_faddr_v6)) {
2307 ip_xmit_attr_t *ixa = connp->conn_ixa;
2310 * If we are going to create a DCE we'd better have
2311 * an ifindex
2313 if (ixa->ixa_nce != NULL) {
2314 ifindex = ixa->ixa_nce->nce_common->ncec_ill->
2315 ill_phyint->phyint_ifindex;
2316 } else {
2317 return (0);
2321 (void) dce_update_uinfo(&connp->conn_faddr_v6, ifindex, &uinfo,
2322 ipst);
2324 return (0);
2328 * Send out a control packet on the tcp connection specified. This routine
2329 * is typically called where we need a simple ACK or RST generated.
2331 void
2332 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl)
2334 uchar_t *rptr;
2335 tcpha_t *tcpha;
2336 ipha_t *ipha = NULL;
2337 ip6_t *ip6h = NULL;
2338 uint32_t sum;
2339 int total_hdr_len;
2340 int ip_hdr_len;
2341 mblk_t *mp;
2342 tcp_stack_t *tcps = tcp->tcp_tcps;
2343 conn_t *connp = tcp->tcp_connp;
2344 ip_xmit_attr_t *ixa = connp->conn_ixa;
2347 * Save sum for use in source route later.
2349 sum = connp->conn_ht_ulp_len + connp->conn_sum;
2350 total_hdr_len = connp->conn_ht_iphc_len;
2351 ip_hdr_len = ixa->ixa_ip_hdr_length;
2353 /* If a text string is passed in with the request, pass it to strlog. */
2354 if (str != NULL && connp->conn_debug) {
2355 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
2356 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x",
2357 str, seq, ack, ctl);
2359 mp = allocb(connp->conn_ht_iphc_allocated + tcps->tcps_wroff_xtra,
2360 BPRI_MED);
2361 if (mp == NULL) {
2362 return;
2364 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra];
2365 mp->b_rptr = rptr;
2366 mp->b_wptr = &rptr[total_hdr_len];
2367 bcopy(connp->conn_ht_iphc, rptr, total_hdr_len);
2369 ixa->ixa_pktlen = total_hdr_len;
2371 if (ixa->ixa_flags & IXAF_IS_IPV4) {
2372 ipha = (ipha_t *)rptr;
2373 ipha->ipha_length = htons(total_hdr_len);
2374 } else {
2375 ip6h = (ip6_t *)rptr;
2376 ip6h->ip6_plen = htons(total_hdr_len - IPV6_HDR_LEN);
2378 tcpha = (tcpha_t *)&rptr[ip_hdr_len];
2379 tcpha->tha_flags = (uint8_t)ctl;
2380 if (ctl & TH_RST) {
2381 TCPS_BUMP_MIB(tcps, tcpOutRsts);
2382 TCPS_BUMP_MIB(tcps, tcpOutControl);
2384 * Don't send TSopt w/ TH_RST packets per RFC 1323.
2386 if (tcp->tcp_snd_ts_ok &&
2387 tcp->tcp_state > TCPS_SYN_SENT) {
2388 mp->b_wptr = &rptr[total_hdr_len - TCPOPT_REAL_TS_LEN];
2389 *(mp->b_wptr) = TCPOPT_EOL;
2391 ixa->ixa_pktlen = total_hdr_len - TCPOPT_REAL_TS_LEN;
2393 if (connp->conn_ipversion == IPV4_VERSION) {
2394 ipha->ipha_length = htons(total_hdr_len -
2395 TCPOPT_REAL_TS_LEN);
2396 } else {
2397 ip6h->ip6_plen = htons(total_hdr_len -
2398 IPV6_HDR_LEN - TCPOPT_REAL_TS_LEN);
2400 tcpha->tha_offset_and_reserved -= (3 << 4);
2401 sum -= TCPOPT_REAL_TS_LEN;
2404 if (ctl & TH_ACK) {
2405 if (tcp->tcp_snd_ts_ok) {
2406 uint32_t llbolt = (uint32_t)LBOLT_FASTPATH;
2408 U32_TO_BE32(llbolt,
2409 (char *)tcpha + TCP_MIN_HEADER_LENGTH+4);
2410 U32_TO_BE32(tcp->tcp_ts_recent,
2411 (char *)tcpha + TCP_MIN_HEADER_LENGTH+8);
2414 /* Update the latest receive window size in TCP header. */
2415 tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
2416 /* Track what we sent to the peer */
2417 tcp->tcp_tcpha->tha_win = tcpha->tha_win;
2418 tcp->tcp_rack = ack;
2419 tcp->tcp_rack_cnt = 0;
2420 TCPS_BUMP_MIB(tcps, tcpOutAck);
2422 BUMP_LOCAL(tcp->tcp_obsegs);
2423 tcpha->tha_seq = htonl(seq);
2424 tcpha->tha_ack = htonl(ack);
2426 * Include the adjustment for a source route if any.
2428 sum = (sum >> 16) + (sum & 0xFFFF);
2429 tcpha->tha_sum = htons(sum);
2430 tcp_send_data(tcp, mp);
2434 * Generate a reset based on an inbound packet, connp is set by caller
2435 * when RST is in response to an unexpected inbound packet for which
2436 * there is active tcp state in the system.
2438 * IPSEC NOTE : Try to send the reply with the same protection as it came
2439 * in. We have the ip_recv_attr_t which is reversed to form the ip_xmit_attr_t.
2440 * That way the packet will go out at the same level of protection as it
2441 * came in with.
2443 static void
2444 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, uint32_t ack, int ctl,
2445 ip_recv_attr_t *ira, ip_stack_t *ipst, conn_t *connp)
2447 ipha_t *ipha = NULL;
2448 ip6_t *ip6h = NULL;
2449 ushort_t len;
2450 tcpha_t *tcpha;
2451 int i;
2452 ipaddr_t v4addr;
2453 in6_addr_t v6addr;
2454 netstack_t *ns = ipst->ips_netstack;
2455 tcp_stack_t *tcps = ns->netstack_tcp;
2456 ip_xmit_attr_t ixas, *ixa;
2457 uint_t ip_hdr_len = ira->ira_ip_hdr_length;
2458 boolean_t need_refrele = B_FALSE; /* ixa_refrele(ixa) */
2459 ushort_t port;
2461 if (!tcp_send_rst_chk(tcps)) {
2462 TCP_STAT(tcps, tcp_rst_unsent);
2463 freemsg(mp);
2464 return;
2468 * If connp != NULL we use conn_ixa to keep IP_NEXTHOP and other
2469 * options from the listener. In that case the caller must ensure that
2470 * we are running on the listener = connp squeue.
2472 * We get a safe copy of conn_ixa so we don't need to restore anything
2473 * we or ip_output_simple might change in the ixa.
2475 if (connp != NULL) {
2476 ASSERT(connp->conn_on_sqp);
2478 ixa = conn_get_ixa_exclusive(connp);
2479 if (ixa == NULL) {
2480 TCP_STAT(tcps, tcp_rst_unsent);
2481 freemsg(mp);
2482 return;
2484 need_refrele = B_TRUE;
2485 } else {
2486 bzero(&ixas, sizeof (ixas));
2487 ixa = &ixas;
2489 * IXAF_VERIFY_SOURCE is overkill since we know the
2490 * packet was for us.
2492 ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE;
2493 ixa->ixa_protocol = IPPROTO_TCP;
2494 ixa->ixa_zoneid = ira->ira_zoneid;
2495 ixa->ixa_ifindex = 0;
2496 ixa->ixa_ipst = ipst;
2497 ixa->ixa_cred = kcred;
2498 ixa->ixa_cpid = NOPID;
2501 if (str && tcps->tcps_dbg) {
2502 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
2503 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, "
2504 "flags 0x%x",
2505 str, seq, ack, ctl);
2507 if (mp->b_datap->db_ref != 1) {
2508 mblk_t *mp1 = copyb(mp);
2509 freemsg(mp);
2510 mp = mp1;
2511 if (mp == NULL)
2512 goto done;
2513 } else if (mp->b_cont) {
2514 freemsg(mp->b_cont);
2515 mp->b_cont = NULL;
2516 DB_CKSUMFLAGS(mp) = 0;
2519 * We skip reversing source route here.
2520 * (for now we replace all IP options with EOL)
2522 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
2523 ipha = (ipha_t *)mp->b_rptr;
2524 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++)
2525 mp->b_rptr[i] = IPOPT_EOL;
2527 * Make sure that src address isn't flagrantly invalid.
2528 * Not all broadcast address checking for the src address
2529 * is possible, since we don't know the netmask of the src
2530 * addr. No check for destination address is done, since
2531 * IP will not pass up a packet with a broadcast dest
2532 * address to TCP. Similar checks are done below for IPv6.
2534 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST ||
2535 CLASSD(ipha->ipha_src)) {
2536 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
2537 ip_drop_input("ipIfStatsInDiscards", mp, NULL);
2538 freemsg(mp);
2539 goto done;
2541 } else {
2542 ip6h = (ip6_t *)mp->b_rptr;
2544 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) ||
2545 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) {
2546 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards);
2547 ip_drop_input("ipIfStatsInDiscards", mp, NULL);
2548 freemsg(mp);
2549 goto done;
2552 /* Remove any extension headers assuming partial overlay */
2553 if (ip_hdr_len > IPV6_HDR_LEN) {
2554 uint8_t *to;
2556 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN;
2557 ovbcopy(ip6h, to, IPV6_HDR_LEN);
2558 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN;
2559 ip_hdr_len = IPV6_HDR_LEN;
2560 ip6h = (ip6_t *)mp->b_rptr;
2561 ip6h->ip6_nxt = IPPROTO_TCP;
2564 tcpha = (tcpha_t *)&mp->b_rptr[ip_hdr_len];
2565 if (tcpha->tha_flags & TH_RST) {
2566 freemsg(mp);
2567 goto done;
2569 tcpha->tha_offset_and_reserved = (5 << 4);
2570 len = ip_hdr_len + sizeof (tcpha_t);
2571 mp->b_wptr = &mp->b_rptr[len];
2572 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
2573 ipha->ipha_length = htons(len);
2574 /* Swap addresses */
2575 v4addr = ipha->ipha_src;
2576 ipha->ipha_src = ipha->ipha_dst;
2577 ipha->ipha_dst = v4addr;
2578 ipha->ipha_ident = 0;
2579 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl;
2580 ixa->ixa_flags |= IXAF_IS_IPV4;
2581 ixa->ixa_ip_hdr_length = ip_hdr_len;
2582 } else {
2583 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN);
2584 /* Swap addresses */
2585 v6addr = ip6h->ip6_src;
2586 ip6h->ip6_src = ip6h->ip6_dst;
2587 ip6h->ip6_dst = v6addr;
2588 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit;
2589 ixa->ixa_flags &= ~IXAF_IS_IPV4;
2591 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_dst)) {
2592 ixa->ixa_flags |= IXAF_SCOPEID_SET;
2593 ixa->ixa_scopeid = ira->ira_ruifindex;
2595 ixa->ixa_ip_hdr_length = IPV6_HDR_LEN;
2597 ixa->ixa_pktlen = len;
2599 /* Swap the ports */
2600 port = tcpha->tha_fport;
2601 tcpha->tha_fport = tcpha->tha_lport;
2602 tcpha->tha_lport = port;
2604 tcpha->tha_ack = htonl(ack);
2605 tcpha->tha_seq = htonl(seq);
2606 tcpha->tha_win = 0;
2607 tcpha->tha_sum = htons(sizeof (tcpha_t));
2608 tcpha->tha_flags = (uint8_t)ctl;
2609 if (ctl & TH_RST) {
2610 if (ctl & TH_ACK) {
2612 * Probe connection rejection here.
2613 * tcp_xmit_listeners_reset() drops non-SYN segments
2614 * that do not specify TH_ACK in their flags without
2615 * calling this function. As a consequence, if this
2616 * function is called with a TH_RST|TH_ACK ctl argument,
2617 * it is being called in response to a SYN segment
2618 * and thus the tcp:::accept-refused probe point
2619 * is valid here.
2621 DTRACE_TCP5(accept__refused, mblk_t *, NULL,
2622 void, NULL, void_ip_t *, mp->b_rptr, tcp_t *, NULL,
2623 tcph_t *, tcpha);
2625 TCPS_BUMP_MIB(tcps, tcpOutRsts);
2626 TCPS_BUMP_MIB(tcps, tcpOutControl);
2629 if (ira->ira_flags & IRAF_IPSEC_SECURE) {
2631 * Apply IPsec based on how IPsec was applied to
2632 * the packet that caused the RST.
2634 if (!ipsec_in_to_out(ira, ixa, mp, ipha, ip6h)) {
2635 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2636 /* Note: mp already consumed and ip_drop_packet done */
2637 goto done;
2639 } else {
2641 * This is in clear. The RST message we are building
2642 * here should go out in clear, independent of our policy.
2644 ixa->ixa_flags |= IXAF_NO_IPSEC;
2647 DTRACE_TCP5(send, mblk_t *, NULL, ip_xmit_attr_t *, ixa,
2648 __dtrace_tcp_void_ip_t *, mp->b_rptr, tcp_t *, NULL,
2649 __dtrace_tcp_tcph_t *, tcpha);
2652 * NOTE: one might consider tracing a TCP packet here, but
2653 * this function has no active TCP state and no tcp structure
2654 * that has a trace buffer. If we traced here, we would have
2655 * to keep a local trace buffer in tcp_record_trace().
2658 (void) ip_output_simple(mp, ixa);
2659 done:
2660 ixa_cleanup(ixa);
2661 if (need_refrele) {
2662 ASSERT(ixa != &ixas);
2663 ixa_refrele(ixa);
2668 * Generate a "no listener here" RST in response to an "unknown" segment.
2669 * connp is set by caller when RST is in response to an unexpected
2670 * inbound packet for which there is active tcp state in the system.
2671 * Note that we are reusing the incoming mp to construct the outgoing RST.
2673 void
2674 tcp_xmit_listeners_reset(mblk_t *mp, ip_recv_attr_t *ira, ip_stack_t *ipst,
2675 conn_t *connp)
2677 uchar_t *rptr;
2678 uint32_t seg_len;
2679 tcpha_t *tcpha;
2680 uint32_t seg_seq;
2681 uint32_t seg_ack;
2682 uint_t flags;
2683 ipha_t *ipha;
2684 ip6_t *ip6h;
2685 boolean_t policy_present;
2686 netstack_t *ns = ipst->ips_netstack;
2687 tcp_stack_t *tcps = ns->netstack_tcp;
2688 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec;
2689 uint_t ip_hdr_len = ira->ira_ip_hdr_length;
2691 TCP_STAT(tcps, tcp_no_listener);
2694 * DTrace this "unknown" segment as a tcp:::receive, as we did
2695 * just receive something that was TCP.
2697 DTRACE_TCP5(receive, mblk_t *, NULL, ip_xmit_attr_t *, NULL,
2698 __dtrace_tcp_void_ip_t *, mp->b_rptr, tcp_t *, NULL,
2699 __dtrace_tcp_tcph_t *, &mp->b_rptr[ip_hdr_len]);
2701 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
2702 policy_present = ipss->ipsec_inbound_v4_policy_present;
2703 ipha = (ipha_t *)mp->b_rptr;
2704 ip6h = NULL;
2705 } else {
2706 policy_present = ipss->ipsec_inbound_v6_policy_present;
2707 ipha = NULL;
2708 ip6h = (ip6_t *)mp->b_rptr;
2711 if (policy_present) {
2713 * The conn_t parameter is NULL because we already know
2714 * nobody's home.
2716 mp = ipsec_check_global_policy(mp, (conn_t *)NULL, ipha, ip6h,
2717 ira, ns);
2718 if (mp == NULL)
2719 return;
2721 rptr = mp->b_rptr;
2723 tcpha = (tcpha_t *)&rptr[ip_hdr_len];
2724 seg_seq = ntohl(tcpha->tha_seq);
2725 seg_ack = ntohl(tcpha->tha_ack);
2726 flags = tcpha->tha_flags;
2728 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcpha) + ip_hdr_len);
2729 if (flags & TH_RST) {
2730 freemsg(mp);
2731 } else if (flags & TH_ACK) {
2732 tcp_xmit_early_reset("no tcp, reset", mp, seg_ack, 0, TH_RST,
2733 ira, ipst, connp);
2734 } else {
2735 if (flags & TH_SYN) {
2736 seg_len++;
2737 } else {
2739 * Here we violate the RFC. Note that a normal
2740 * TCP will never send a segment without the ACK
2741 * flag, except for RST or SYN segment. This
2742 * segment is neither. Just drop it on the
2743 * floor.
2745 freemsg(mp);
2746 TCP_STAT(tcps, tcp_rst_unsent);
2747 return;
2750 tcp_xmit_early_reset("no tcp, reset/ack", mp, 0,
2751 seg_seq + seg_len, TH_RST | TH_ACK, ira, ipst, connp);
2756 * Helper function for tcp_xmit_mp() in handling connection set up flag
2757 * options setting.
2759 static void
2760 tcp_xmit_mp_aux_iss(tcp_t *tcp, conn_t *connp, tcpha_t *tcpha, mblk_t *mp,
2761 uint_t *flags)
2763 uint32_t u1;
2764 uint8_t *wptr = mp->b_wptr;
2765 tcp_stack_t *tcps = tcp->tcp_tcps;
2766 boolean_t add_sack = B_FALSE;
2769 * If TCP_ISS_VALID and the seq number is tcp_iss,
2770 * TCP can only be in SYN-SENT, SYN-RCVD or
2771 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if
2772 * our SYN is not ack'ed but the app closes this
2773 * TCP connection.
2775 ASSERT(tcp->tcp_state == TCPS_SYN_SENT ||
2776 tcp->tcp_state == TCPS_SYN_RCVD ||
2777 tcp->tcp_state == TCPS_FIN_WAIT_1);
2780 * Tack on the MSS option. It is always needed
2781 * for both active and passive open.
2783 * MSS option value should be interface MTU - MIN
2784 * TCP/IP header according to RFC 793 as it means
2785 * the maximum segment size TCP can receive. But
2786 * to get around some broken middle boxes/end hosts
2787 * out there, we allow the option value to be the
2788 * same as the MSS option size on the peer side.
2789 * In this way, the other side will not send
2790 * anything larger than they can receive.
2792 * Note that for SYN_SENT state, the ndd param
2793 * tcp_use_smss_as_mss_opt has no effect as we
2794 * don't know the peer's MSS option value. So
2795 * the only case we need to take care of is in
2796 * SYN_RCVD state, which is done later.
2798 wptr[0] = TCPOPT_MAXSEG;
2799 wptr[1] = TCPOPT_MAXSEG_LEN;
2800 wptr += 2;
2801 u1 = tcp->tcp_initial_pmtu - (connp->conn_ipversion == IPV4_VERSION ?
2802 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - TCP_MIN_HEADER_LENGTH;
2803 U16_TO_BE16(u1, wptr);
2804 wptr += 2;
2806 /* Update the offset to cover the additional word */
2807 tcpha->tha_offset_and_reserved += (1 << 4);
2809 switch (tcp->tcp_state) {
2810 case TCPS_SYN_SENT:
2811 *flags = TH_SYN;
2813 if (tcp->tcp_snd_sack_ok)
2814 add_sack = B_TRUE;
2816 if (tcp->tcp_snd_ts_ok) {
2817 uint32_t llbolt = (uint32_t)LBOLT_FASTPATH;
2819 if (add_sack) {
2820 wptr[0] = TCPOPT_SACK_PERMITTED;
2821 wptr[1] = TCPOPT_SACK_OK_LEN;
2822 add_sack = B_FALSE;
2823 } else {
2824 wptr[0] = TCPOPT_NOP;
2825 wptr[1] = TCPOPT_NOP;
2827 wptr[2] = TCPOPT_TSTAMP;
2828 wptr[3] = TCPOPT_TSTAMP_LEN;
2829 wptr += 4;
2830 U32_TO_BE32(llbolt, wptr);
2831 wptr += 4;
2832 ASSERT(tcp->tcp_ts_recent == 0);
2833 U32_TO_BE32(0L, wptr);
2834 wptr += 4;
2835 tcpha->tha_offset_and_reserved += (3 << 4);
2839 * Set up all the bits to tell other side
2840 * we are ECN capable.
2842 if (tcp->tcp_ecn_ok)
2843 *flags |= (TH_ECE | TH_CWR);
2845 break;
2847 case TCPS_SYN_RCVD:
2848 *flags |= TH_SYN;
2851 * Reset the MSS option value to be SMSS
2852 * We should probably add back the bytes
2853 * for timestamp option and IPsec. We
2854 * don't do that as this is a workaround
2855 * for broken middle boxes/end hosts, it
2856 * is better for us to be more cautious.
2857 * They may not take these things into
2858 * account in their SMSS calculation. Thus
2859 * the peer's calculated SMSS may be smaller
2860 * than what it can be. This should be OK.
2862 if (tcps->tcps_use_smss_as_mss_opt) {
2863 u1 = tcp->tcp_mss;
2865 * Note that wptr points just past the MSS
2866 * option value.
2868 U16_TO_BE16(u1, wptr - 2);
2872 * tcp_snd_ts_ok can only be set in TCPS_SYN_RCVD
2873 * when the peer also uses timestamps option. And
2874 * the TCP header template must have already been
2875 * updated to include the timestamps option.
2877 if (tcp->tcp_snd_sack_ok) {
2878 if (tcp->tcp_snd_ts_ok) {
2879 uint8_t *tmp_wptr;
2882 * Use the NOP in the header just
2883 * before timestamps opton.
2885 tmp_wptr = (uint8_t *)tcpha +
2886 TCP_MIN_HEADER_LENGTH;
2887 ASSERT(tmp_wptr[0] == TCPOPT_NOP &&
2888 tmp_wptr[1] == TCPOPT_NOP);
2889 tmp_wptr[0] = TCPOPT_SACK_PERMITTED;
2890 tmp_wptr[1] = TCPOPT_SACK_OK_LEN;
2891 } else {
2892 add_sack = B_TRUE;
2898 * If the other side is ECN capable, reply
2899 * that we are also ECN capable.
2901 if (tcp->tcp_ecn_ok)
2902 *flags |= TH_ECE;
2903 break;
2905 default:
2907 * The above ASSERT() makes sure that this
2908 * must be FIN-WAIT-1 state. Our SYN has
2909 * not been ack'ed so retransmit it.
2911 *flags |= TH_SYN;
2912 break;
2915 if (add_sack) {
2916 wptr[0] = TCPOPT_NOP;
2917 wptr[1] = TCPOPT_NOP;
2918 wptr[2] = TCPOPT_SACK_PERMITTED;
2919 wptr[3] = TCPOPT_SACK_OK_LEN;
2920 wptr += TCPOPT_REAL_SACK_OK_LEN;
2921 tcpha->tha_offset_and_reserved += (1 << 4);
2924 if (tcp->tcp_snd_ws_ok) {
2925 wptr[0] = TCPOPT_NOP;
2926 wptr[1] = TCPOPT_WSCALE;
2927 wptr[2] = TCPOPT_WS_LEN;
2928 wptr[3] = (uchar_t)tcp->tcp_rcv_ws;
2929 wptr += TCPOPT_REAL_WS_LEN;
2930 tcpha->tha_offset_and_reserved += (1 << 4);
2933 mp->b_wptr = wptr;
2934 u1 = (int)(mp->b_wptr - mp->b_rptr);
2936 * Get IP set to checksum on our behalf
2937 * Include the adjustment for a source route if any.
2939 u1 += connp->conn_sum;
2940 u1 = (u1 >> 16) + (u1 & 0xFFFF);
2941 tcpha->tha_sum = htons(u1);
2942 TCPS_BUMP_MIB(tcps, tcpOutControl);
2946 * Helper function for tcp_xmit_mp() in handling connection tear down
2947 * flag setting and state changes.
2949 static void
2950 tcp_xmit_mp_aux_fss(tcp_t *tcp, ip_xmit_attr_t *ixa, uint_t *flags)
2952 if (!tcp->tcp_fin_acked) {
2953 *flags |= TH_FIN;
2954 TCPS_BUMP_MIB(tcp->tcp_tcps, tcpOutControl);
2956 if (!tcp->tcp_fin_sent) {
2957 tcp->tcp_fin_sent = B_TRUE;
2958 switch (tcp->tcp_state) {
2959 case TCPS_SYN_RCVD:
2960 tcp->tcp_state = TCPS_FIN_WAIT_1;
2961 DTRACE_TCP6(state__change, void, NULL,
2962 ip_xmit_attr_t *, ixa, void, NULL,
2963 tcp_t *, tcp, void, NULL,
2964 int32_t, TCPS_SYN_RCVD);
2965 break;
2966 case TCPS_ESTABLISHED:
2967 tcp->tcp_state = TCPS_FIN_WAIT_1;
2968 DTRACE_TCP6(state__change, void, NULL,
2969 ip_xmit_attr_t *, ixa, void, NULL,
2970 tcp_t *, tcp, void, NULL,
2971 int32_t, TCPS_ESTABLISHED);
2972 break;
2973 case TCPS_CLOSE_WAIT:
2974 tcp->tcp_state = TCPS_LAST_ACK;
2975 DTRACE_TCP6(state__change, void, NULL,
2976 ip_xmit_attr_t *, ixa, void, NULL,
2977 tcp_t *, tcp, void, NULL,
2978 int32_t, TCPS_CLOSE_WAIT);
2979 break;
2981 if (tcp->tcp_suna == tcp->tcp_snxt)
2982 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
2983 tcp->tcp_snxt = tcp->tcp_fss + 1;
2988 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with
2989 * ip and tcp header ready to pass down to IP. If the mp passed in is
2990 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that
2991 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary
2992 * otherwise it will dup partial mblks.)
2993 * Otherwise, an appropriate ACK packet will be generated. This
2994 * routine is not usually called to send new data for the first time. It
2995 * is mostly called out of the timer for retransmits, and to generate ACKs.
2997 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will
2998 * be adjusted by *offset. And after dupb(), the offset and the ending mblk
2999 * of the original mblk chain will be returned in *offset and *end_mp.
3001 mblk_t *
3002 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset,
3003 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len,
3004 boolean_t rexmit)
3006 int data_length;
3007 int32_t off = 0;
3008 uint_t flags;
3009 mblk_t *mp1;
3010 mblk_t *mp2;
3011 uchar_t *rptr;
3012 tcpha_t *tcpha;
3013 int32_t num_sack_blk = 0;
3014 int32_t sack_opt_len = 0;
3015 tcp_stack_t *tcps = tcp->tcp_tcps;
3016 conn_t *connp = tcp->tcp_connp;
3017 ip_xmit_attr_t *ixa = connp->conn_ixa;
3019 /* Allocate for our maximum TCP header + link-level */
3020 mp1 = allocb(connp->conn_ht_iphc_allocated + tcps->tcps_wroff_xtra,
3021 BPRI_MED);
3022 if (mp1 == NULL)
3023 return (NULL);
3024 data_length = 0;
3027 * Note that tcp_mss has been adjusted to take into account the
3028 * timestamp option if applicable. Because SACK options do not
3029 * appear in every TCP segments and they are of variable lengths,
3030 * they cannot be included in tcp_mss. Thus we need to calculate
3031 * the actual segment length when we need to send a segment which
3032 * includes SACK options.
3034 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
3035 num_sack_blk = MIN(tcp->tcp_max_sack_blk,
3036 tcp->tcp_num_sack_blk);
3037 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) +
3038 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN;
3039 if (max_to_send + sack_opt_len > tcp->tcp_mss)
3040 max_to_send -= sack_opt_len;
3043 if (offset != NULL) {
3044 off = *offset;
3045 /* We use offset as an indicator that end_mp is not NULL. */
3046 *end_mp = NULL;
3048 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) {
3049 /* This could be faster with cooperation from downstream */
3050 if (mp2 != mp1 && !sendall &&
3051 data_length + (int)(mp->b_wptr - mp->b_rptr) >
3052 max_to_send)
3054 * Don't send the next mblk since the whole mblk
3055 * does not fit.
3057 break;
3058 mp2->b_cont = dupb(mp);
3059 mp2 = mp2->b_cont;
3060 if (!mp2) {
3061 freemsg(mp1);
3062 return (NULL);
3064 mp2->b_rptr += off;
3065 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <=
3066 (uintptr_t)INT_MAX);
3068 data_length += (int)(mp2->b_wptr - mp2->b_rptr);
3069 if (data_length > max_to_send) {
3070 mp2->b_wptr -= data_length - max_to_send;
3071 data_length = max_to_send;
3072 off = mp2->b_wptr - mp->b_rptr;
3073 break;
3074 } else {
3075 off = 0;
3078 if (offset != NULL) {
3079 *offset = off;
3080 *end_mp = mp;
3082 if (seg_len != NULL) {
3083 *seg_len = data_length;
3086 /* Update the latest receive window size in TCP header. */
3087 tcp->tcp_tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
3089 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra;
3090 mp1->b_rptr = rptr;
3091 mp1->b_wptr = rptr + connp->conn_ht_iphc_len + sack_opt_len;
3092 bcopy(connp->conn_ht_iphc, rptr, connp->conn_ht_iphc_len);
3093 tcpha = (tcpha_t *)&rptr[ixa->ixa_ip_hdr_length];
3094 tcpha->tha_seq = htonl(seq);
3097 * Use tcp_unsent to determine if the PUSH bit should be used assumes
3098 * that this function was called from tcp_wput_data. Thus, when called
3099 * to retransmit data the setting of the PUSH bit may appear some
3100 * what random in that it might get set when it should not. This
3101 * should not pose any performance issues.
3103 if (data_length != 0 && (tcp->tcp_unsent == 0 ||
3104 tcp->tcp_unsent == data_length)) {
3105 flags = TH_ACK | TH_PUSH;
3106 } else {
3107 flags = TH_ACK;
3110 if (tcp->tcp_ecn_ok) {
3111 if (tcp->tcp_ecn_echo_on)
3112 flags |= TH_ECE;
3115 * Only set ECT bit and ECN_CWR if a segment contains new data.
3116 * There is no TCP flow control for non-data segments, and
3117 * only data segment is transmitted reliably.
3119 if (data_length > 0 && !rexmit) {
3120 TCP_SET_ECT(tcp, rptr);
3121 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) {
3122 flags |= TH_CWR;
3123 tcp->tcp_ecn_cwr_sent = B_TRUE;
3128 /* Check if there is any special processing needs to be done. */
3129 if (tcp->tcp_valid_bits) {
3130 uint32_t u1;
3132 /* We don't allow having SYN and FIN in the same segment... */
3133 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) &&
3134 seq == tcp->tcp_iss) {
3135 /* Need to do connection set up processing. */
3136 tcp_xmit_mp_aux_iss(tcp, connp, tcpha, mp1, &flags);
3137 } else if ((tcp->tcp_valid_bits & TCP_FSS_VALID) &&
3138 (seq + data_length) == tcp->tcp_fss) {
3139 /* Need to do connection tear down processing. */
3140 tcp_xmit_mp_aux_fss(tcp, ixa, &flags);
3144 * Need to do urgent pointer processing.
3146 * Note the trick here. u1 is unsigned. When tcp_urg
3147 * is smaller than seq, u1 will become a very huge value.
3148 * So the comparison will fail. Also note that tcp_urp
3149 * should be positive, see RFC 793 page 17.
3151 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION;
3152 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 &&
3153 u1 < (uint32_t)(64 * 1024)) {
3154 flags |= TH_URG;
3155 TCPS_BUMP_MIB(tcps, tcpOutUrg);
3156 tcpha->tha_urp = htons(u1);
3159 tcpha->tha_flags = (uchar_t)flags;
3160 tcp->tcp_rack = tcp->tcp_rnxt;
3161 tcp->tcp_rack_cnt = 0;
3163 /* Fill in the current value of timestamps option. */
3164 if (tcp->tcp_snd_ts_ok) {
3165 if (tcp->tcp_state != TCPS_SYN_SENT) {
3166 uint32_t llbolt = (uint32_t)LBOLT_FASTPATH;
3168 U32_TO_BE32(llbolt,
3169 (char *)tcpha + TCP_MIN_HEADER_LENGTH+4);
3170 U32_TO_BE32(tcp->tcp_ts_recent,
3171 (char *)tcpha + TCP_MIN_HEADER_LENGTH+8);
3175 /* Fill in the SACK blocks. */
3176 if (num_sack_blk > 0) {
3177 uchar_t *wptr = (uchar_t *)tcpha + connp->conn_ht_ulp_len;
3178 sack_blk_t *tmp;
3179 int32_t i;
3181 wptr[0] = TCPOPT_NOP;
3182 wptr[1] = TCPOPT_NOP;
3183 wptr[2] = TCPOPT_SACK;
3184 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
3185 sizeof (sack_blk_t);
3186 wptr += TCPOPT_REAL_SACK_LEN;
3188 tmp = tcp->tcp_sack_list;
3189 for (i = 0; i < num_sack_blk; i++) {
3190 U32_TO_BE32(tmp[i].begin, wptr);
3191 wptr += sizeof (tcp_seq);
3192 U32_TO_BE32(tmp[i].end, wptr);
3193 wptr += sizeof (tcp_seq);
3195 tcpha->tha_offset_and_reserved += ((num_sack_blk * 2 + 1) << 4);
3197 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX);
3198 data_length += (int)(mp1->b_wptr - rptr);
3200 ixa->ixa_pktlen = data_length;
3202 if (ixa->ixa_flags & IXAF_IS_IPV4) {
3203 ((ipha_t *)rptr)->ipha_length = htons(data_length);
3204 } else {
3205 ip6_t *ip6 = (ip6_t *)rptr;
3207 ip6->ip6_plen = htons(data_length - IPV6_HDR_LEN);
3211 * Prime pump for IP
3212 * Include the adjustment for a source route if any.
3214 data_length -= ixa->ixa_ip_hdr_length;
3215 data_length += connp->conn_sum;
3216 data_length = (data_length >> 16) + (data_length & 0xFFFF);
3217 tcpha->tha_sum = htons(data_length);
3218 if (tcp->tcp_ip_forward_progress) {
3219 tcp->tcp_ip_forward_progress = B_FALSE;
3220 connp->conn_ixa->ixa_flags |= IXAF_REACH_CONF;
3221 } else {
3222 connp->conn_ixa->ixa_flags &= ~IXAF_REACH_CONF;
3224 return (mp1);
3228 * If this routine returns B_TRUE, TCP can generate a RST in response
3229 * to a segment. If it returns B_FALSE, TCP should not respond.
3231 static boolean_t
3232 tcp_send_rst_chk(tcp_stack_t *tcps)
3234 int64_t now;
3237 * TCP needs to protect itself from generating too many RSTs.
3238 * This can be a DoS attack by sending us random segments
3239 * soliciting RSTs.
3241 * What we do here is to have a limit of tcp_rst_sent_rate RSTs
3242 * in each 1 second interval. In this way, TCP still generate
3243 * RSTs in normal cases but when under attack, the impact is
3244 * limited.
3246 if (tcps->tcps_rst_sent_rate_enabled != 0) {
3247 now = ddi_get_lbolt64();
3248 if (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) >
3249 1*SECONDS) {
3250 tcps->tcps_last_rst_intrvl = now;
3251 tcps->tcps_rst_cnt = 1;
3252 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) {
3253 return (B_FALSE);
3256 return (B_TRUE);
3260 * This function handles all retransmissions if SACK is enabled for this
3261 * connection. First it calculates how many segments can be retransmitted
3262 * based on tcp_pipe. Then it goes thru the notsack list to find eligible
3263 * segments. A segment is eligible if sack_cnt for that segment is greater
3264 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted
3265 * all eligible segments, it checks to see if TCP can send some new segments
3266 * (fast recovery). If it can, set the appropriate flag for tcp_input_data().
3268 * Parameters:
3269 * tcp_t *tcp: the tcp structure of the connection.
3270 * uint_t *flags: in return, appropriate value will be set for
3271 * tcp_input_data().
3273 void
3274 tcp_sack_rexmit(tcp_t *tcp, uint_t *flags)
3276 notsack_blk_t *notsack_blk;
3277 int32_t usable_swnd;
3278 int32_t mss;
3279 uint32_t seg_len;
3280 mblk_t *xmit_mp;
3281 tcp_stack_t *tcps = tcp->tcp_tcps;
3283 ASSERT(tcp->tcp_notsack_list != NULL);
3284 ASSERT(tcp->tcp_rexmit == B_FALSE);
3286 /* Defensive coding in case there is a bug... */
3287 if (tcp->tcp_notsack_list == NULL) {
3288 return;
3290 notsack_blk = tcp->tcp_notsack_list;
3291 mss = tcp->tcp_mss;
3294 * Limit the num of outstanding data in the network to be
3295 * tcp_cwnd_ssthresh, which is half of the original congestion wnd.
3297 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe;
3299 /* At least retransmit 1 MSS of data. */
3300 if (usable_swnd <= 0) {
3301 usable_swnd = mss;
3304 /* Make sure no new RTT samples will be taken. */
3305 tcp->tcp_csuna = tcp->tcp_snxt;
3307 notsack_blk = tcp->tcp_notsack_list;
3308 while (usable_swnd > 0) {
3309 mblk_t *snxt_mp, *tmp_mp;
3310 tcp_seq begin = tcp->tcp_sack_snxt;
3311 tcp_seq end;
3312 int32_t off;
3314 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) {
3315 if (SEQ_GT(notsack_blk->end, begin) &&
3316 (notsack_blk->sack_cnt >=
3317 tcps->tcps_dupack_fast_retransmit)) {
3318 end = notsack_blk->end;
3319 if (SEQ_LT(begin, notsack_blk->begin)) {
3320 begin = notsack_blk->begin;
3322 break;
3326 * All holes are filled. Manipulate tcp_cwnd to send more
3327 * if we can. Note that after the SACK recovery, tcp_cwnd is
3328 * set to tcp_cwnd_ssthresh.
3330 if (notsack_blk == NULL) {
3331 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe;
3332 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) {
3333 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna;
3334 ASSERT(tcp->tcp_cwnd > 0);
3335 return;
3336 } else {
3337 usable_swnd = usable_swnd / mss;
3338 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna +
3339 MAX(usable_swnd * mss, mss);
3340 *flags |= TH_XMIT_NEEDED;
3341 return;
3346 * Note that we may send more than usable_swnd allows here
3347 * because of round off, but no more than 1 MSS of data.
3349 seg_len = end - begin;
3350 if (seg_len > mss)
3351 seg_len = mss;
3352 snxt_mp = tcp_get_seg_mp(tcp, begin, &off);
3353 ASSERT(snxt_mp != NULL);
3354 /* This should not happen. Defensive coding again... */
3355 if (snxt_mp == NULL) {
3356 return;
3359 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off,
3360 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE);
3361 if (xmit_mp == NULL)
3362 return;
3364 usable_swnd -= seg_len;
3365 tcp->tcp_pipe += seg_len;
3366 tcp->tcp_sack_snxt = begin + seg_len;
3368 tcp_send_data(tcp, xmit_mp);
3371 * Update the send timestamp to avoid false retransmission.
3373 snxt_mp->b_prev = (mblk_t *)ddi_get_lbolt();
3375 TCPS_BUMP_MIB(tcps, tcpRetransSegs);
3376 TCPS_UPDATE_MIB(tcps, tcpRetransBytes, seg_len);
3377 TCPS_BUMP_MIB(tcps, tcpOutSackRetransSegs);
3379 * Update tcp_rexmit_max to extend this SACK recovery phase.
3380 * This happens when new data sent during fast recovery is
3381 * also lost. If TCP retransmits those new data, it needs
3382 * to extend SACK recover phase to avoid starting another
3383 * fast retransmit/recovery unnecessarily.
3385 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) {
3386 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt;
3392 * tcp_ss_rexmit() is called to do slow start retransmission after a timeout
3393 * or ICMP errors.
3395 void
3396 tcp_ss_rexmit(tcp_t *tcp)
3398 uint32_t snxt;
3399 uint32_t smax;
3400 int32_t win;
3401 int32_t mss;
3402 int32_t off;
3403 mblk_t *snxt_mp;
3404 tcp_stack_t *tcps = tcp->tcp_tcps;
3407 * Note that tcp_rexmit can be set even though TCP has retransmitted
3408 * all unack'ed segments.
3410 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) {
3411 smax = tcp->tcp_rexmit_max;
3412 snxt = tcp->tcp_rexmit_nxt;
3413 if (SEQ_LT(snxt, tcp->tcp_suna)) {
3414 snxt = tcp->tcp_suna;
3416 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd);
3417 win -= snxt - tcp->tcp_suna;
3418 mss = tcp->tcp_mss;
3419 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off);
3421 while (SEQ_LT(snxt, smax) && (win > 0) && (snxt_mp != NULL)) {
3422 mblk_t *xmit_mp;
3423 mblk_t *old_snxt_mp = snxt_mp;
3424 uint32_t cnt = mss;
3426 if (win < cnt) {
3427 cnt = win;
3429 if (SEQ_GT(snxt + cnt, smax)) {
3430 cnt = smax - snxt;
3432 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off,
3433 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE);
3434 if (xmit_mp == NULL)
3435 return;
3437 tcp_send_data(tcp, xmit_mp);
3439 snxt += cnt;
3440 win -= cnt;
3442 * Update the send timestamp to avoid false
3443 * retransmission.
3445 old_snxt_mp->b_prev = (mblk_t *)ddi_get_lbolt();
3446 TCPS_BUMP_MIB(tcps, tcpRetransSegs);
3447 TCPS_UPDATE_MIB(tcps, tcpRetransBytes, cnt);
3449 tcp->tcp_rexmit_nxt = snxt;
3452 * If we have transmitted all we have at the time
3453 * we started the retranmission, we can leave
3454 * the rest of the job to tcp_wput_data(). But we
3455 * need to check the send window first. If the
3456 * win is not 0, go on with tcp_wput_data().
3458 if (SEQ_LT(snxt, smax) || win == 0) {
3459 return;
3462 /* Only call tcp_wput_data() if there is data to be sent. */
3463 if (tcp->tcp_unsent) {
3464 tcp_wput_data(tcp, NULL, B_FALSE);
3469 * Do slow start retransmission after ICMP errors of PMTU changes.
3471 void
3472 tcp_rexmit_after_error(tcp_t *tcp)
3475 * All sent data has been acknowledged or no data left to send, just
3476 * to return.
3478 if (!SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) ||
3479 (tcp->tcp_xmit_head == NULL))
3480 return;
3482 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && (tcp->tcp_unsent == 0))
3483 tcp->tcp_rexmit_max = tcp->tcp_fss;
3484 else
3485 tcp->tcp_rexmit_max = tcp->tcp_snxt;
3487 tcp->tcp_rexmit_nxt = tcp->tcp_suna;
3488 tcp->tcp_rexmit = B_TRUE;
3489 tcp->tcp_dupack_cnt = 0;
3490 tcp_ss_rexmit(tcp);
3494 * tcp_get_seg_mp() is called to get the pointer to a segment in the
3495 * send queue which starts at the given sequence number. If the given
3496 * sequence number is equal to last valid sequence number (tcp_snxt), the
3497 * returned mblk is the last valid mblk, and off is set to the length of
3498 * that mblk.
3500 * send queue which starts at the given seq. no.
3502 * Parameters:
3503 * tcp_t *tcp: the tcp instance pointer.
3504 * uint32_t seq: the starting seq. no of the requested segment.
3505 * int32_t *off: after the execution, *off will be the offset to
3506 * the returned mblk which points to the requested seq no.
3507 * It is the caller's responsibility to send in a non-null off.
3509 * Return:
3510 * A mblk_t pointer pointing to the requested segment in send queue.
3512 static mblk_t *
3513 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off)
3515 int32_t cnt;
3516 mblk_t *mp;
3518 /* Defensive coding. Make sure we don't send incorrect data. */
3519 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GT(seq, tcp->tcp_snxt))
3520 return (NULL);
3522 cnt = seq - tcp->tcp_suna;
3523 mp = tcp->tcp_xmit_head;
3524 while (cnt > 0 && mp != NULL) {
3525 cnt -= mp->b_wptr - mp->b_rptr;
3526 if (cnt <= 0) {
3527 cnt += mp->b_wptr - mp->b_rptr;
3528 break;
3530 mp = mp->b_cont;
3532 ASSERT(mp != NULL);
3533 *off = cnt;
3534 return (mp);
3538 * This routine adjusts next-to-send sequence number variables, in the
3539 * case where the reciever has shrunk it's window.
3541 void
3542 tcp_update_xmit_tail(tcp_t *tcp, uint32_t snxt)
3544 mblk_t *xmit_tail;
3545 int32_t offset;
3547 tcp->tcp_snxt = snxt;
3549 /* Get the mblk, and the offset in it, as per the shrunk window */
3550 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset);
3551 ASSERT(xmit_tail != NULL);
3552 tcp->tcp_xmit_tail = xmit_tail;
3553 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr -
3554 xmit_tail->b_rptr - offset;
3558 * This handles the case when the receiver has shrunk its win. Per RFC 1122
3559 * if the receiver shrinks the window, i.e. moves the right window to the
3560 * left, the we should not send new data, but should retransmit normally the
3561 * old unacked data between suna and suna + swnd. We might has sent data
3562 * that is now outside the new window, pretend that we didn't send it.
3564 static void
3565 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count)
3567 uint32_t snxt = tcp->tcp_snxt;
3569 ASSERT(shrunk_count > 0);
3571 if (!tcp->tcp_is_wnd_shrnk) {
3572 tcp->tcp_snxt_shrunk = snxt;
3573 tcp->tcp_is_wnd_shrnk = B_TRUE;
3574 } else if (SEQ_GT(snxt, tcp->tcp_snxt_shrunk)) {
3575 tcp->tcp_snxt_shrunk = snxt;
3578 /* Pretend we didn't send the data outside the window */
3579 snxt -= shrunk_count;
3581 /* Reset all the values per the now shrunk window */
3582 tcp_update_xmit_tail(tcp, snxt);
3583 tcp->tcp_unsent += shrunk_count;
3586 * If the SACK option is set, delete the entire list of
3587 * notsack'ed blocks.
3589 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp);
3591 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0)
3593 * Make sure the timer is running so that we will probe a zero
3594 * window.
3596 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
3600 * tcp_fill_header is called by tcp_send() to fill the outgoing TCP header
3601 * with the template header, as well as other options such as time-stamp,
3602 * ECN and/or SACK.
3604 static void
3605 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk)
3607 tcpha_t *tcp_tmpl, *tcpha;
3608 uint32_t *dst, *src;
3609 int hdrlen;
3610 conn_t *connp = tcp->tcp_connp;
3612 ASSERT(OK_32PTR(rptr));
3614 /* Template header */
3615 tcp_tmpl = tcp->tcp_tcpha;
3617 /* Header of outgoing packet */
3618 tcpha = (tcpha_t *)(rptr + connp->conn_ixa->ixa_ip_hdr_length);
3620 /* dst and src are opaque 32-bit fields, used for copying */
3621 dst = (uint32_t *)rptr;
3622 src = (uint32_t *)connp->conn_ht_iphc;
3623 hdrlen = connp->conn_ht_iphc_len;
3625 /* Fill time-stamp option if needed */
3626 if (tcp->tcp_snd_ts_ok) {
3627 U32_TO_BE32((uint32_t)now,
3628 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4);
3629 U32_TO_BE32(tcp->tcp_ts_recent,
3630 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8);
3631 } else {
3632 ASSERT(connp->conn_ht_ulp_len == TCP_MIN_HEADER_LENGTH);
3636 * Copy the template header; is this really more efficient than
3637 * calling bcopy()? For simple IPv4/TCP, it may be the case,
3638 * but perhaps not for other scenarios.
3640 dst[0] = src[0];
3641 dst[1] = src[1];
3642 dst[2] = src[2];
3643 dst[3] = src[3];
3644 dst[4] = src[4];
3645 dst[5] = src[5];
3646 dst[6] = src[6];
3647 dst[7] = src[7];
3648 dst[8] = src[8];
3649 dst[9] = src[9];
3650 if (hdrlen -= 40) {
3651 hdrlen >>= 2;
3652 dst += 10;
3653 src += 10;
3654 do {
3655 *dst++ = *src++;
3656 } while (--hdrlen);
3660 * Set the ECN info in the TCP header if it is not a zero
3661 * window probe. Zero window probe is only sent in
3662 * tcp_wput_data() and tcp_timer().
3664 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) {
3665 TCP_SET_ECT(tcp, rptr);
3667 if (tcp->tcp_ecn_echo_on)
3668 tcpha->tha_flags |= TH_ECE;
3669 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) {
3670 tcpha->tha_flags |= TH_CWR;
3671 tcp->tcp_ecn_cwr_sent = B_TRUE;
3675 /* Fill in SACK options */
3676 if (num_sack_blk > 0) {
3677 uchar_t *wptr = rptr + connp->conn_ht_iphc_len;
3678 sack_blk_t *tmp;
3679 int32_t i;
3681 wptr[0] = TCPOPT_NOP;
3682 wptr[1] = TCPOPT_NOP;
3683 wptr[2] = TCPOPT_SACK;
3684 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
3685 sizeof (sack_blk_t);
3686 wptr += TCPOPT_REAL_SACK_LEN;
3688 tmp = tcp->tcp_sack_list;
3689 for (i = 0; i < num_sack_blk; i++) {
3690 U32_TO_BE32(tmp[i].begin, wptr);
3691 wptr += sizeof (tcp_seq);
3692 U32_TO_BE32(tmp[i].end, wptr);
3693 wptr += sizeof (tcp_seq);
3695 tcpha->tha_offset_and_reserved +=
3696 ((num_sack_blk * 2 + 1) << 4);