1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2014, 2017 by Delphix. All rights reserved.
25 * Copyright 2020 Joyent, Inc.
26 */
27
28 /* This file contains all TCP output processing functions. */
29
30 #include <sys/types.h>
31 #include <sys/stream.h>
32 #include <sys/strsun.h>
33 #include <sys/strsubr.h>
34 #include <sys/stropts.h>
35 #include <sys/strlog.h>
36 #define _SUN_TPI_VERSION 2
37 #include <sys/tihdr.h>
38 #include <sys/suntpi.h>
39 #include <sys/xti_inet.h>
40 #include <sys/timod.h>
41 #include <sys/pattr.h>
42 #include <sys/squeue_impl.h>
43 #include <sys/squeue.h>
44 #include <sys/sockio.h>
45 #include <sys/tsol/tnet.h>
46
47 #include <inet/common.h>
48 #include <inet/ip.h>
49 #include <inet/tcp.h>
50 #include <inet/tcp_impl.h>
51 #include <inet/snmpcom.h>
52 #include <inet/proto_set.h>
53 #include <inet/ipsec_impl.h>
54 #include <inet/ip_ndp.h>
55
56 static mblk_t *tcp_get_seg_mp(tcp_t *, uint32_t, int32_t *);
57 static void tcp_wput_cmdblk(queue_t *, mblk_t *);
58 static void tcp_wput_flush(tcp_t *, mblk_t *);
59 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp);
60 static int tcp_xmit_end(tcp_t *);
61 static int tcp_send(tcp_t *, const int, const int, const int,
62 const int, int *, uint32_t *, int *, mblk_t **, mblk_t *);
63 static void tcp_xmit_early_reset(char *, mblk_t *, uint32_t, uint32_t,
64 int, ip_recv_attr_t *, ip_stack_t *, conn_t *);
65 static boolean_t tcp_send_rst_chk(tcp_stack_t *);
66 static void tcp_process_shrunk_swnd(tcp_t *, uint32_t);
67 static void tcp_fill_header(tcp_t *, uchar_t *, int);
68
69 /*
70 * Functions called directly via squeue having a prototype of edesc_t.
71 */
72 static void tcp_wput_nondata(void *, mblk_t *, void *, ip_recv_attr_t *);
73 static void tcp_wput_ioctl(void *, mblk_t *, void *, ip_recv_attr_t *);
74 static void tcp_wput_proto(void *, mblk_t *, void *, ip_recv_attr_t *);
75
76 /*
77 * This controls how tiny a write must be before we try to copy it
78 * into the mblk on the tail of the transmit queue. Not much
79 * speedup is observed for values larger than sixteen. Zero will
80 * disable the optimisation.
81 */
82 static int tcp_tx_pull_len = 16;
83
84 static void
cc_after_idle(tcp_t * tcp)85 cc_after_idle(tcp_t *tcp)
86 {
87 uint32_t old_cwnd = tcp->tcp_cwnd;
88
89 if (CC_ALGO(tcp)->after_idle != NULL)
90 CC_ALGO(tcp)->after_idle(&tcp->tcp_ccv);
91
92 DTRACE_PROBE3(cwnd__cc__after__idle, tcp_t *, tcp, uint32_t, old_cwnd,
93 uint32_t, tcp->tcp_cwnd);
94 }
95
96 int
tcp_wput(queue_t * q,mblk_t * mp)97 tcp_wput(queue_t *q, mblk_t *mp)
98 {
99 conn_t *connp = Q_TO_CONN(q);
100 tcp_t *tcp;
101 void (*output_proc)();
102 t_scalar_t type;
103 uchar_t *rptr;
104 struct iocblk *iocp;
105 size_t size;
106
107 ASSERT(connp->conn_ref >= 2);
108
109 switch (DB_TYPE(mp)) {
110 case M_DATA:
111 tcp = connp->conn_tcp;
112 ASSERT(tcp != NULL);
113
114 size = msgdsize(mp);
115
116 mutex_enter(&tcp->tcp_non_sq_lock);
117 tcp->tcp_squeue_bytes += size;
118 if (TCP_UNSENT_BYTES(tcp) > connp->conn_sndbuf) {
119 tcp_setqfull(tcp);
120 }
121 mutex_exit(&tcp->tcp_non_sq_lock);
122
123 CONN_INC_REF(connp);
124 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp,
125 NULL, tcp_squeue_flag, SQTAG_TCP_OUTPUT);
126 return (0);
127
128 case M_CMD:
129 tcp_wput_cmdblk(q, mp);
130 return (0);
131
132 case M_PROTO:
133 case M_PCPROTO:
134 /*
135 * if it is a snmp message, don't get behind the squeue
136 */
137 tcp = connp->conn_tcp;
138 rptr = mp->b_rptr;
139 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) {
140 type = ((union T_primitives *)rptr)->type;
141 } else {
142 if (connp->conn_debug) {
143 (void) strlog(TCP_MOD_ID, 0, 1,
144 SL_ERROR|SL_TRACE,
145 "tcp_wput_proto, dropping one...");
146 }
147 freemsg(mp);
148 return (0);
149 }
150 if (type == T_SVR4_OPTMGMT_REQ) {
151 /*
152 * All Solaris components should pass a db_credp
153 * for this TPI message, hence we ASSERT.
154 * But in case there is some other M_PROTO that looks
155 * like a TPI message sent by some other kernel
156 * component, we check and return an error.
157 */
158 cred_t *cr = msg_getcred(mp, NULL);
159
160 ASSERT(cr != NULL);
161 if (cr == NULL) {
162 tcp_err_ack(tcp, mp, TSYSERR, EINVAL);
163 return (0);
164 }
165 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get,
166 cr)) {
167 /*
168 * This was a SNMP request
169 */
170 return (0);
171 } else {
172 output_proc = tcp_wput_proto;
173 }
174 } else {
175 output_proc = tcp_wput_proto;
176 }
177 break;
178 case M_IOCTL:
179 /*
180 * Most ioctls can be processed right away without going via
181 * squeues - process them right here. Those that do require
182 * squeue (currently _SIOCSOCKFALLBACK)
183 * are processed by tcp_wput_ioctl().
184 */
185 iocp = (struct iocblk *)mp->b_rptr;
186 tcp = connp->conn_tcp;
187
188 switch (iocp->ioc_cmd) {
189 case TCP_IOC_ABORT_CONN:
190 tcp_ioctl_abort_conn(q, mp);
191 return (0);
192 case TI_GETPEERNAME:
193 case TI_GETMYNAME:
194 mi_copyin(q, mp, NULL,
195 SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
196 return (0);
197
198 default:
199 output_proc = tcp_wput_ioctl;
200 break;
201 }
202 break;
203 default:
204 output_proc = tcp_wput_nondata;
205 break;
206 }
207
208 CONN_INC_REF(connp);
209 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp,
210 NULL, tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER);
211 return (0);
212 }
213
214 /*
215 * The TCP normal data output path.
216 * NOTE: the logic of the fast path is duplicated from this function.
217 */
218 void
tcp_wput_data(tcp_t * tcp,mblk_t * mp,boolean_t urgent)219 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent)
220 {
221 int len;
222 mblk_t *local_time;
223 mblk_t *mp1;
224 uint32_t snxt;
225 int tail_unsent;
226 int tcpstate;
227 int usable = 0;
228 mblk_t *xmit_tail;
229 int32_t mss;
230 int32_t num_sack_blk = 0;
231 int32_t total_hdr_len;
232 int32_t tcp_hdr_len;
233 int rc;
234 conn_t *connp = tcp->tcp_connp;
235 clock_t now = LBOLT_FASTPATH;
236
237 tcpstate = tcp->tcp_state;
238 if (mp == NULL) {
239 /*
240 * tcp_wput_data() with NULL mp should only be called when
241 * there is unsent data.
242 */
243 ASSERT(tcp->tcp_unsent > 0);
244 /* Really tacky... but we need this for detached closes. */
245 len = tcp->tcp_unsent;
246 goto data_null;
247 }
248
249 ASSERT(mp->b_datap->db_type == M_DATA);
250 /*
251 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ,
252 * or before a connection attempt has begun.
253 */
254 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT ||
255 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) {
256 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) {
257 #ifdef DEBUG
258 cmn_err(CE_WARN,
259 "tcp_wput_data: data after ordrel, %s",
260 tcp_display(tcp, NULL,
261 DISP_ADDR_AND_PORT));
262 #else
263 if (connp->conn_debug) {
264 (void) strlog(TCP_MOD_ID, 0, 1,
265 SL_TRACE|SL_ERROR,
266 "tcp_wput_data: data after ordrel, %s\n",
267 tcp_display(tcp, NULL,
268 DISP_ADDR_AND_PORT));
269 }
270 #endif /* DEBUG */
271 }
272 if (tcp->tcp_snd_zcopy_aware &&
273 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY))
274 tcp_zcopy_notify(tcp);
275 freemsg(mp);
276 mutex_enter(&tcp->tcp_non_sq_lock);
277 if (tcp->tcp_flow_stopped &&
278 TCP_UNSENT_BYTES(tcp) <= connp->conn_sndlowat) {
279 tcp_clrqfull(tcp);
280 }
281 mutex_exit(&tcp->tcp_non_sq_lock);
282 return;
283 }
284
285 /* Strip empties */
286 for (;;) {
287 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
288 (uintptr_t)INT_MAX);
289 len = (int)(mp->b_wptr - mp->b_rptr);
290 if (len > 0)
291 break;
292 mp1 = mp;
293 mp = mp->b_cont;
294 freeb(mp1);
295 if (mp == NULL) {
296 return;
297 }
298 }
299
300 /* If we are the first on the list ... */
301 if (tcp->tcp_xmit_head == NULL) {
302 tcp->tcp_xmit_head = mp;
303 tcp->tcp_xmit_tail = mp;
304 tcp->tcp_xmit_tail_unsent = len;
305 } else {
306 /* If tiny tx and room in txq tail, pullup to save mblks. */
307 struct datab *dp;
308
309 mp1 = tcp->tcp_xmit_last;
310 if (len < tcp_tx_pull_len &&
311 (dp = mp1->b_datap)->db_ref == 1 &&
312 dp->db_lim - mp1->b_wptr >= len) {
313 ASSERT(len > 0);
314 ASSERT(!mp1->b_cont);
315 if (len == 1) {
316 *mp1->b_wptr++ = *mp->b_rptr;
317 } else {
318 bcopy(mp->b_rptr, mp1->b_wptr, len);
319 mp1->b_wptr += len;
320 }
321 if (mp1 == tcp->tcp_xmit_tail)
322 tcp->tcp_xmit_tail_unsent += len;
323 mp1->b_cont = mp->b_cont;
324 if (tcp->tcp_snd_zcopy_aware &&
325 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY))
326 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY;
327 freeb(mp);
328 mp = mp1;
329 } else {
330 tcp->tcp_xmit_last->b_cont = mp;
331 }
332 len += tcp->tcp_unsent;
333 }
334
335 /* Tack on however many more positive length mblks we have */
336 if ((mp1 = mp->b_cont) != NULL) {
337 do {
338 int tlen;
339 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <=
340 (uintptr_t)INT_MAX);
341 tlen = (int)(mp1->b_wptr - mp1->b_rptr);
342 if (tlen <= 0) {
343 mp->b_cont = mp1->b_cont;
344 freeb(mp1);
345 } else {
346 len += tlen;
347 mp = mp1;
348 }
349 } while ((mp1 = mp->b_cont) != NULL);
350 }
351 tcp->tcp_xmit_last = mp;
352 tcp->tcp_unsent = len;
353
354 if (urgent)
355 usable = 1;
356
357 data_null:
358 snxt = tcp->tcp_snxt;
359 xmit_tail = tcp->tcp_xmit_tail;
360 tail_unsent = tcp->tcp_xmit_tail_unsent;
361
362 /*
363 * Note that tcp_mss has been adjusted to take into account the
364 * timestamp option if applicable. Because SACK options do not
365 * appear in every TCP segments and they are of variable lengths,
366 * they cannot be included in tcp_mss. Thus we need to calculate
367 * the actual segment length when we need to send a segment which
368 * includes SACK options.
369 */
370 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
371 int32_t opt_len;
372
373 num_sack_blk = MIN(tcp->tcp_max_sack_blk,
374 tcp->tcp_num_sack_blk);
375 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN *
376 2 + TCPOPT_HEADER_LEN;
377 mss = tcp->tcp_mss - opt_len;
378 total_hdr_len = connp->conn_ht_iphc_len + opt_len;
379 tcp_hdr_len = connp->conn_ht_ulp_len + opt_len;
380 } else {
381 mss = tcp->tcp_mss;
382 total_hdr_len = connp->conn_ht_iphc_len;
383 tcp_hdr_len = connp->conn_ht_ulp_len;
384 }
385
386 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet &&
387 (TICK_TO_MSEC(now - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) {
388 cc_after_idle(tcp);
389 }
390 if (tcpstate == TCPS_SYN_RCVD) {
391 /*
392 * The three-way connection establishment handshake is not
393 * complete yet. We want to queue the data for transmission
394 * after entering ESTABLISHED state (RFC793). A jump to
395 * "done" label effectively leaves data on the queue.
396 */
397 goto done;
398 } else {
399 int usable_r;
400
401 /*
402 * In the special case when cwnd is zero, which can only
403 * happen if the connection is ECN capable, return now.
404 * New segments is sent using tcp_timer(). The timer
405 * is set in tcp_input_data().
406 */
407 if (tcp->tcp_cwnd == 0) {
408 /*
409 * Note that tcp_cwnd is 0 before 3-way handshake is
410 * finished.
411 */
412 ASSERT(tcp->tcp_ecn_ok ||
413 tcp->tcp_state < TCPS_ESTABLISHED);
414 return;
415 }
416
417 /* NOTE: trouble if xmitting while SYN not acked? */
418 usable_r = snxt - tcp->tcp_suna;
419 usable_r = tcp->tcp_swnd - usable_r;
420
421 /*
422 * Check if the receiver has shrunk the window. If
423 * tcp_wput_data() with NULL mp is called, tcp_fin_sent
424 * cannot be set as there is unsent data, so FIN cannot
425 * be sent out. Otherwise, we need to take into account
426 * of FIN as it consumes an "invisible" sequence number.
427 */
428 ASSERT(tcp->tcp_fin_sent == 0);
429 if (usable_r < 0) {
430 /*
431 * The receiver has shrunk the window and we have sent
432 * -usable_r date beyond the window, re-adjust.
433 *
434 * If TCP window scaling is enabled, there can be
435 * round down error as the advertised receive window
436 * is actually right shifted n bits. This means that
437 * the lower n bits info is wiped out. It will look
438 * like the window is shrunk. Do a check here to
439 * see if the shrunk amount is actually within the
440 * error in window calculation. If it is, just
441 * return. Note that this check is inside the
442 * shrunk window check. This makes sure that even
443 * though tcp_process_shrunk_swnd() is not called,
444 * we will stop further processing.
445 */
446 if ((-usable_r >> tcp->tcp_snd_ws) > 0) {
447 tcp_process_shrunk_swnd(tcp, -usable_r);
448 }
449 return;
450 }
451
452 /* usable = MIN(swnd, cwnd) - unacked_bytes */
453 if (tcp->tcp_swnd > tcp->tcp_cwnd)
454 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd;
455
456 /* usable = MIN(usable, unsent) */
457 if (usable_r > len)
458 usable_r = len;
459
460 /* usable = MAX(usable, {1 for urgent, 0 for data}) */
461 if (usable_r > 0) {
462 usable = usable_r;
463 } else {
464 /* Bypass all other unnecessary processing. */
465 goto done;
466 }
467 }
468
469 local_time = (mblk_t *)(intptr_t)gethrtime();
470
471 /*
472 * "Our" Nagle Algorithm. This is not the same as in the old
473 * BSD. This is more in line with the true intent of Nagle.
474 *
475 * The conditions are:
476 * 1. The amount of unsent data (or amount of data which can be
477 * sent, whichever is smaller) is less than Nagle limit.
478 * 2. The last sent size is also less than Nagle limit.
479 * 3. There is unack'ed data.
480 * 4. Urgent pointer is not set. Send urgent data ignoring the
481 * Nagle algorithm. This reduces the probability that urgent
482 * bytes get "merged" together.
483 * 5. The app has not closed the connection. This eliminates the
484 * wait time of the receiving side waiting for the last piece of
485 * (small) data.
486 *
487 * If all are satisified, exit without sending anything. Note
488 * that Nagle limit can be smaller than 1 MSS. Nagle limit is
489 * the smaller of 1 MSS and global tcp_naglim_def (default to be
490 * 4095).
491 */
492 if (usable < (int)tcp->tcp_naglim &&
493 tcp->tcp_naglim > tcp->tcp_last_sent_len &&
494 snxt != tcp->tcp_suna &&
495 !(tcp->tcp_valid_bits & TCP_URG_VALID) &&
496 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) {
497 goto done;
498 }
499
500 /*
501 * If tcp_zero_win_probe is not set and the tcp->tcp_cork option
502 * is set, then we have to force TCP not to send partial segment
503 * (smaller than MSS bytes). We are calculating the usable now
504 * based on full mss and will save the rest of remaining data for
505 * later. When tcp_zero_win_probe is set, TCP needs to send out
506 * something to do zero window probe.
507 */
508 if (tcp->tcp_cork && !tcp->tcp_zero_win_probe) {
509 if (usable < mss)
510 goto done;
511 usable = (usable / mss) * mss;
512 }
513
514 /* Update the latest receive window size in TCP header. */
515 tcp->tcp_tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
516
517 /* Send the packet. */
518 rc = tcp_send(tcp, mss, total_hdr_len, tcp_hdr_len,
519 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail,
520 local_time);
521
522 /* Pretend that all we were trying to send really got sent */
523 if (rc < 0 && tail_unsent < 0) {
524 do {
525 xmit_tail = xmit_tail->b_cont;
526 xmit_tail->b_prev = local_time;
527 ASSERT((uintptr_t)(xmit_tail->b_wptr -
528 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX);
529 tail_unsent += (int)(xmit_tail->b_wptr -
530 xmit_tail->b_rptr);
531 } while (tail_unsent < 0);
532 }
533 done:;
534 tcp->tcp_xmit_tail = xmit_tail;
535 tcp->tcp_xmit_tail_unsent = tail_unsent;
536 len = tcp->tcp_snxt - snxt;
537 if (len) {
538 /*
539 * If new data was sent, need to update the notsack
540 * list, which is, afterall, data blocks that have
541 * not been sack'ed by the receiver. New data is
542 * not sack'ed.
543 */
544 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) {
545 /* len is a negative value. */
546 tcp->tcp_pipe -= len;
547 tcp_notsack_update(&(tcp->tcp_notsack_list),
548 tcp->tcp_snxt, snxt,
549 &(tcp->tcp_num_notsack_blk),
550 &(tcp->tcp_cnt_notsack_list));
551 }
552 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent;
553 tcp->tcp_rack = tcp->tcp_rnxt;
554 tcp->tcp_rack_cnt = 0;
555 if ((snxt + len) == tcp->tcp_suna) {
556 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
557 }
558 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) {
559 /*
560 * Didn't send anything. Make sure the timer is running
561 * so that we will probe a zero window.
562 */
563 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
564 }
565 /* Note that len is the amount we just sent but with a negative sign */
566 tcp->tcp_unsent += len;
567 mutex_enter(&tcp->tcp_non_sq_lock);
568 if (tcp->tcp_flow_stopped) {
569 if (TCP_UNSENT_BYTES(tcp) <= connp->conn_sndlowat) {
570 tcp_clrqfull(tcp);
571 }
572 } else if (TCP_UNSENT_BYTES(tcp) >= connp->conn_sndbuf) {
573 if (!(tcp->tcp_detached))
574 tcp_setqfull(tcp);
575 }
576 mutex_exit(&tcp->tcp_non_sq_lock);
577 }
578
579 /*
580 * Initial STREAMS write side put() procedure for sockets. It tries to
581 * handle the T_CAPABILITY_REQ which sockfs sends down while setting
582 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages
583 * are handled by tcp_wput() as usual.
584 *
585 * All further messages will also be handled by tcp_wput() because we cannot
586 * be sure that the above short cut is safe later.
587 */
588 int
tcp_wput_sock(queue_t * wq,mblk_t * mp)589 tcp_wput_sock(queue_t *wq, mblk_t *mp)
590 {
591 conn_t *connp = Q_TO_CONN(wq);
592 tcp_t *tcp = connp->conn_tcp;
593 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr;
594
595 ASSERT(wq->q_qinfo == &tcp_sock_winit);
596 wq->q_qinfo = &tcp_winit;
597
598 ASSERT(IPCL_IS_TCP(connp));
599 ASSERT(TCP_IS_SOCKET(tcp));
600
601 if (DB_TYPE(mp) == M_PCPROTO &&
602 MBLKL(mp) == sizeof (struct T_capability_req) &&
603 car->PRIM_type == T_CAPABILITY_REQ) {
604 tcp_capability_req(tcp, mp);
605 return (0);
606 }
607
608 tcp_wput(wq, mp);
609 return (0);
610 }
611
612 /* ARGSUSED */
613 int
tcp_wput_fallback(queue_t * wq,mblk_t * mp)614 tcp_wput_fallback(queue_t *wq, mblk_t *mp)
615 {
616 #ifdef DEBUG
617 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n");
618 #endif
619 freemsg(mp);
620 return (0);
621 }
622
623 /*
624 * Call by tcp_wput() to handle misc non M_DATA messages.
625 */
626 /* ARGSUSED */
627 static void
tcp_wput_nondata(void * arg,mblk_t * mp,void * arg2,ip_recv_attr_t * dummy)628 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
629 {
630 conn_t *connp = (conn_t *)arg;
631 tcp_t *tcp = connp->conn_tcp;
632
633 ASSERT(DB_TYPE(mp) != M_IOCTL);
634 /*
635 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close.
636 * Once the close starts, streamhead and sockfs will not let any data
637 * packets come down (close ensures that there are no threads using the
638 * queue and no new threads will come down) but since qprocsoff()
639 * hasn't happened yet, a M_FLUSH or some non data message might
640 * get reflected back (in response to our own FLUSHRW) and get
641 * processed after tcp_close() is done. The conn would still be valid
642 * because a ref would have added but we need to check the state
643 * before actually processing the packet.
644 */
645 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) {
646 freemsg(mp);
647 return;
648 }
649
650 switch (DB_TYPE(mp)) {
651 case M_IOCDATA:
652 tcp_wput_iocdata(tcp, mp);
653 break;
654 case M_FLUSH:
655 tcp_wput_flush(tcp, mp);
656 break;
657 default:
658 ip_wput_nondata(connp->conn_wq, mp);
659 break;
660 }
661 }
662
663 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */
664 static void
tcp_wput_flush(tcp_t * tcp,mblk_t * mp)665 tcp_wput_flush(tcp_t *tcp, mblk_t *mp)
666 {
667 uchar_t fval = *mp->b_rptr;
668 mblk_t *tail;
669 conn_t *connp = tcp->tcp_connp;
670 queue_t *q = connp->conn_wq;
671
672 /* TODO: How should flush interact with urgent data? */
673 if ((fval & FLUSHW) && tcp->tcp_xmit_head != NULL &&
674 !(tcp->tcp_valid_bits & TCP_URG_VALID)) {
675 /*
676 * Flush only data that has not yet been put on the wire. If
677 * we flush data that we have already transmitted, life, as we
678 * know it, may come to an end.
679 */
680 tail = tcp->tcp_xmit_tail;
681 tail->b_wptr -= tcp->tcp_xmit_tail_unsent;
682 tcp->tcp_xmit_tail_unsent = 0;
683 tcp->tcp_unsent = 0;
684 if (tail->b_wptr != tail->b_rptr)
685 tail = tail->b_cont;
686 if (tail) {
687 mblk_t **excess = &tcp->tcp_xmit_head;
688 for (;;) {
689 mblk_t *mp1 = *excess;
690 if (mp1 == tail)
691 break;
692 tcp->tcp_xmit_tail = mp1;
693 tcp->tcp_xmit_last = mp1;
694 excess = &mp1->b_cont;
695 }
696 *excess = NULL;
697 tcp_close_mpp(&tail);
698 if (tcp->tcp_snd_zcopy_aware)
699 tcp_zcopy_notify(tcp);
700 }
701 /*
702 * We have no unsent data, so unsent must be less than
703 * conn_sndlowat, so re-enable flow.
704 */
705 mutex_enter(&tcp->tcp_non_sq_lock);
706 if (tcp->tcp_flow_stopped) {
707 tcp_clrqfull(tcp);
708 }
709 mutex_exit(&tcp->tcp_non_sq_lock);
710 }
711 /*
712 * TODO: you can't just flush these, you have to increase rwnd for one
713 * thing. For another, how should urgent data interact?
714 */
715 if (fval & FLUSHR) {
716 *mp->b_rptr = fval & ~FLUSHW;
717 /* XXX */
718 qreply(q, mp);
719 return;
720 }
721 freemsg(mp);
722 }
723
724 /*
725 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA
726 * messages.
727 */
728 static void
tcp_wput_iocdata(tcp_t * tcp,mblk_t * mp)729 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp)
730 {
731 mblk_t *mp1;
732 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
733 STRUCT_HANDLE(strbuf, sb);
734 uint_t addrlen;
735 conn_t *connp = tcp->tcp_connp;
736 queue_t *q = connp->conn_wq;
737
738 /* Make sure it is one of ours. */
739 switch (iocp->ioc_cmd) {
740 case TI_GETMYNAME:
741 case TI_GETPEERNAME:
742 break;
743 default:
744 /*
745 * If the conn is closing, then error the ioctl here. Otherwise
746 * use the CONN_IOCTLREF_* macros to hold off tcp_close until
747 * we're done here.
748 */
749 mutex_enter(&connp->conn_lock);
750 if (connp->conn_state_flags & CONN_CLOSING) {
751 mutex_exit(&connp->conn_lock);
752 iocp->ioc_error = EINVAL;
753 mp->b_datap->db_type = M_IOCNAK;
754 iocp->ioc_count = 0;
755 qreply(q, mp);
756 return;
757 }
758
759 CONN_INC_IOCTLREF_LOCKED(connp);
760 ip_wput_nondata(q, mp);
761 CONN_DEC_IOCTLREF(connp);
762 return;
763 }
764 switch (mi_copy_state(q, mp, &mp1)) {
765 case -1:
766 return;
767 case MI_COPY_CASE(MI_COPY_IN, 1):
768 break;
769 case MI_COPY_CASE(MI_COPY_OUT, 1):
770 /* Copy out the strbuf. */
771 mi_copyout(q, mp);
772 return;
773 case MI_COPY_CASE(MI_COPY_OUT, 2):
774 /* All done. */
775 mi_copy_done(q, mp, 0);
776 return;
777 default:
778 mi_copy_done(q, mp, EPROTO);
779 return;
780 }
781 /* Check alignment of the strbuf */
782 if (!OK_32PTR(mp1->b_rptr)) {
783 mi_copy_done(q, mp, EINVAL);
784 return;
785 }
786
787 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr);
788
789 if (connp->conn_family == AF_INET)
790 addrlen = sizeof (sin_t);
791 else
792 addrlen = sizeof (sin6_t);
793
794 if (STRUCT_FGET(sb, maxlen) < addrlen) {
795 mi_copy_done(q, mp, EINVAL);
796 return;
797 }
798
799 switch (iocp->ioc_cmd) {
800 case TI_GETMYNAME:
801 break;
802 case TI_GETPEERNAME:
803 if (tcp->tcp_state < TCPS_SYN_RCVD) {
804 mi_copy_done(q, mp, ENOTCONN);
805 return;
806 }
807 break;
808 }
809 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE);
810 if (!mp1)
811 return;
812
813 STRUCT_FSET(sb, len, addrlen);
814 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
815 case TI_GETMYNAME:
816 (void) conn_getsockname(connp, (struct sockaddr *)mp1->b_wptr,
817 &addrlen);
818 break;
819 case TI_GETPEERNAME:
820 (void) conn_getpeername(connp, (struct sockaddr *)mp1->b_wptr,
821 &addrlen);
822 break;
823 }
824 mp1->b_wptr += addrlen;
825 /* Copy out the address */
826 mi_copyout(q, mp);
827 }
828
829 /*
830 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL
831 * messages.
832 */
833 /* ARGSUSED */
834 static void
tcp_wput_ioctl(void * arg,mblk_t * mp,void * arg2,ip_recv_attr_t * dummy)835 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
836 {
837 conn_t *connp = (conn_t *)arg;
838 tcp_t *tcp = connp->conn_tcp;
839 queue_t *q = connp->conn_wq;
840 struct iocblk *iocp;
841
842 ASSERT(DB_TYPE(mp) == M_IOCTL);
843 /*
844 * Try and ASSERT the minimum possible references on the
845 * conn early enough. Since we are executing on write side,
846 * the connection is obviously not detached and that means
847 * there is a ref each for TCP and IP. Since we are behind
848 * the squeue, the minimum references needed are 3. If the
849 * conn is in classifier hash list, there should be an
850 * extra ref for that (we check both the possibilities).
851 */
852 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) ||
853 (connp->conn_fanout == NULL && connp->conn_ref >= 3));
854
855 iocp = (struct iocblk *)mp->b_rptr;
856 switch (iocp->ioc_cmd) {
857 case _SIOCSOCKFALLBACK:
858 /*
859 * Either sockmod is about to be popped and the socket
860 * would now be treated as a plain stream, or a module
861 * is about to be pushed so we could no longer use read-
862 * side synchronous streams for fused loopback tcp.
863 * Drain any queued data and disable direct sockfs
864 * interface from now on.
865 */
866 if (!tcp->tcp_issocket) {
867 DB_TYPE(mp) = M_IOCNAK;
868 iocp->ioc_error = EINVAL;
869 } else {
870 tcp_use_pure_tpi(tcp);
871 DB_TYPE(mp) = M_IOCACK;
872 iocp->ioc_error = 0;
873 }
874 iocp->ioc_count = 0;
875 iocp->ioc_rval = 0;
876 qreply(q, mp);
877 return;
878 }
879
880 /*
881 * If the conn is closing, then error the ioctl here. Otherwise bump the
882 * conn_ioctlref to hold off tcp_close until we're done here.
883 */
884 mutex_enter(&(connp)->conn_lock);
885 if ((connp)->conn_state_flags & CONN_CLOSING) {
886 mutex_exit(&(connp)->conn_lock);
887 iocp->ioc_error = EINVAL;
888 mp->b_datap->db_type = M_IOCNAK;
889 iocp->ioc_count = 0;
890 qreply(q, mp);
891 return;
892 }
893
894 CONN_INC_IOCTLREF_LOCKED(connp);
895 ip_wput_nondata(q, mp);
896 CONN_DEC_IOCTLREF(connp);
897 }
898
899 /*
900 * This routine is called by tcp_wput() to handle all TPI requests.
901 */
902 /* ARGSUSED */
903 static void
tcp_wput_proto(void * arg,mblk_t * mp,void * arg2,ip_recv_attr_t * dummy)904 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
905 {
906 conn_t *connp = (conn_t *)arg;
907 tcp_t *tcp = connp->conn_tcp;
908 union T_primitives *tprim = (union T_primitives *)mp->b_rptr;
909 uchar_t *rptr;
910 t_scalar_t type;
911 cred_t *cr;
912
913 /*
914 * Try and ASSERT the minimum possible references on the
915 * conn early enough. Since we are executing on write side,
916 * the connection is obviously not detached and that means
917 * there is a ref each for TCP and IP. Since we are behind
918 * the squeue, the minimum references needed are 3. If the
919 * conn is in classifier hash list, there should be an
920 * extra ref for that (we check both the possibilities).
921 */
922 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) ||
923 (connp->conn_fanout == NULL && connp->conn_ref >= 3));
924
925 rptr = mp->b_rptr;
926 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX);
927 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) {
928 type = ((union T_primitives *)rptr)->type;
929 if (type == T_EXDATA_REQ) {
930 tcp_output_urgent(connp, mp, arg2, NULL);
931 } else if (type != T_DATA_REQ) {
932 goto non_urgent_data;
933 } else {
934 /* TODO: options, flags, ... from user */
935 /* Set length to zero for reclamation below */
936 tcp_wput_data(tcp, mp->b_cont, B_TRUE);
937 freeb(mp);
938 }
939 return;
940 } else {
941 if (connp->conn_debug) {
942 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
943 "tcp_wput_proto, dropping one...");
944 }
945 freemsg(mp);
946 return;
947 }
948
949 non_urgent_data:
950
951 switch ((int)tprim->type) {
952 case O_T_BIND_REQ: /* bind request */
953 case T_BIND_REQ: /* new semantics bind request */
954 tcp_tpi_bind(tcp, mp);
955 break;
956 case T_UNBIND_REQ: /* unbind request */
957 tcp_tpi_unbind(tcp, mp);
958 break;
959 case O_T_CONN_RES: /* old connection response XXX */
960 case T_CONN_RES: /* connection response */
961 tcp_tli_accept(tcp, mp);
962 break;
963 case T_CONN_REQ: /* connection request */
964 tcp_tpi_connect(tcp, mp);
965 break;
966 case T_DISCON_REQ: /* disconnect request */
967 tcp_disconnect(tcp, mp);
968 break;
969 case T_CAPABILITY_REQ:
970 tcp_capability_req(tcp, mp); /* capability request */
971 break;
972 case T_INFO_REQ: /* information request */
973 tcp_info_req(tcp, mp);
974 break;
975 case T_SVR4_OPTMGMT_REQ: /* manage options req */
976 case T_OPTMGMT_REQ:
977 /*
978 * Note: no support for snmpcom_req() through new
979 * T_OPTMGMT_REQ. See comments in ip.c
980 */
981
982 /*
983 * All Solaris components should pass a db_credp
984 * for this TPI message, hence we ASSERT.
985 * But in case there is some other M_PROTO that looks
986 * like a TPI message sent by some other kernel
987 * component, we check and return an error.
988 */
989 cr = msg_getcred(mp, NULL);
990 ASSERT(cr != NULL);
991 if (cr == NULL) {
992 tcp_err_ack(tcp, mp, TSYSERR, EINVAL);
993 return;
994 }
995 /*
996 * If EINPROGRESS is returned, the request has been queued
997 * for subsequent processing by ip_restart_optmgmt(), which
998 * will do the CONN_DEC_REF().
999 */
1000 if ((int)tprim->type == T_SVR4_OPTMGMT_REQ) {
1001 svr4_optcom_req(connp->conn_wq, mp, cr, &tcp_opt_obj);
1002 } else {
1003 tpi_optcom_req(connp->conn_wq, mp, cr, &tcp_opt_obj);
1004 }
1005 break;
1006
1007 case T_UNITDATA_REQ: /* unitdata request */
1008 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0);
1009 break;
1010 case T_ORDREL_REQ: /* orderly release req */
1011 freemsg(mp);
1012
1013 if (tcp->tcp_fused)
1014 tcp_unfuse(tcp);
1015
1016 if (tcp_xmit_end(tcp) != 0) {
1017 /*
1018 * We were crossing FINs and got a reset from
1019 * the other side. Just ignore it.
1020 */
1021 if (connp->conn_debug) {
1022 (void) strlog(TCP_MOD_ID, 0, 1,
1023 SL_ERROR|SL_TRACE,
1024 "tcp_wput_proto, T_ORDREL_REQ out of "
1025 "state %s",
1026 tcp_display(tcp, NULL,
1027 DISP_ADDR_AND_PORT));
1028 }
1029 }
1030 break;
1031 case T_ADDR_REQ:
1032 tcp_addr_req(tcp, mp);
1033 break;
1034 default:
1035 if (connp->conn_debug) {
1036 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
1037 "tcp_wput_proto, bogus TPI msg, type %d",
1038 tprim->type);
1039 }
1040 /*
1041 * We used to M_ERROR. Sending TNOTSUPPORT gives the user
1042 * to recover.
1043 */
1044 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0);
1045 break;
1046 }
1047 }
1048
1049 /*
1050 * Handle special out-of-band ioctl requests (see PSARC/2008/265).
1051 */
1052 static void
tcp_wput_cmdblk(queue_t * q,mblk_t * mp)1053 tcp_wput_cmdblk(queue_t *q, mblk_t *mp)
1054 {
1055 void *data;
1056 mblk_t *datamp = mp->b_cont;
1057 conn_t *connp = Q_TO_CONN(q);
1058 tcp_t *tcp = connp->conn_tcp;
1059 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr;
1060
1061 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) {
1062 cmdp->cb_error = EPROTO;
1063 qreply(q, mp);
1064 return;
1065 }
1066
1067 data = datamp->b_rptr;
1068
1069 switch (cmdp->cb_cmd) {
1070 case TI_GETPEERNAME:
1071 if (tcp->tcp_state < TCPS_SYN_RCVD)
1072 cmdp->cb_error = ENOTCONN;
1073 else
1074 cmdp->cb_error = conn_getpeername(connp, data,
1075 &cmdp->cb_len);
1076 break;
1077 case TI_GETMYNAME:
1078 cmdp->cb_error = conn_getsockname(connp, data, &cmdp->cb_len);
1079 break;
1080 default:
1081 cmdp->cb_error = EINVAL;
1082 break;
1083 }
1084
1085 qreply(q, mp);
1086 }
1087
1088 /*
1089 * The TCP fast path write put procedure.
1090 * NOTE: the logic of the fast path is duplicated from tcp_wput_data()
1091 */
1092 /* ARGSUSED */
1093 void
tcp_output(void * arg,mblk_t * mp,void * arg2,ip_recv_attr_t * dummy)1094 tcp_output(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1095 {
1096 int len;
1097 int hdrlen;
1098 int plen;
1099 mblk_t *mp1;
1100 uchar_t *rptr;
1101 uint32_t snxt;
1102 tcpha_t *tcpha;
1103 struct datab *db;
1104 uint32_t suna;
1105 uint32_t mss;
1106 ipaddr_t *dst;
1107 ipaddr_t *src;
1108 uint32_t sum;
1109 int usable;
1110 conn_t *connp = (conn_t *)arg;
1111 tcp_t *tcp = connp->conn_tcp;
1112 uint32_t msize;
1113 tcp_stack_t *tcps = tcp->tcp_tcps;
1114 ip_xmit_attr_t *ixa;
1115 clock_t now;
1116
1117 /*
1118 * Try and ASSERT the minimum possible references on the
1119 * conn early enough. Since we are executing on write side,
1120 * the connection is obviously not detached and that means
1121 * there is a ref each for TCP and IP. Since we are behind
1122 * the squeue, the minimum references needed are 3. If the
1123 * conn is in classifier hash list, there should be an
1124 * extra ref for that (we check both the possibilities).
1125 */
1126 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) ||
1127 (connp->conn_fanout == NULL && connp->conn_ref >= 3));
1128
1129 ASSERT(DB_TYPE(mp) == M_DATA);
1130 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp);
1131
1132 mutex_enter(&tcp->tcp_non_sq_lock);
1133 tcp->tcp_squeue_bytes -= msize;
1134 mutex_exit(&tcp->tcp_non_sq_lock);
1135
1136 /* Bypass tcp protocol for fused tcp loopback */
1137 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize))
1138 return;
1139
1140 mss = tcp->tcp_mss;
1141 /*
1142 * If ZEROCOPY has turned off, try not to send any zero-copy message
1143 * down. Do backoff, now.
1144 */
1145 if (tcp->tcp_snd_zcopy_aware && !tcp->tcp_snd_zcopy_on)
1146 mp = tcp_zcopy_backoff(tcp, mp, B_FALSE);
1147
1148
1149 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX);
1150 len = (int)(mp->b_wptr - mp->b_rptr);
1151
1152 /*
1153 * Criteria for fast path:
1154 *
1155 * 1. no unsent data
1156 * 2. single mblk in request
1157 * 3. connection established
1158 * 4. data in mblk
1159 * 5. len <= mss
1160 * 6. no tcp_valid bits
1161 */
1162 if ((tcp->tcp_unsent != 0) ||
1163 (tcp->tcp_cork) ||
1164 (mp->b_cont != NULL) ||
1165 (tcp->tcp_state != TCPS_ESTABLISHED) ||
1166 (len == 0) ||
1167 (len > mss) ||
1168 (tcp->tcp_valid_bits != 0)) {
1169 tcp_wput_data(tcp, mp, B_FALSE);
1170 return;
1171 }
1172
1173 ASSERT(tcp->tcp_xmit_tail_unsent == 0);
1174 ASSERT(tcp->tcp_fin_sent == 0);
1175
1176 /* queue new packet onto retransmission queue */
1177 if (tcp->tcp_xmit_head == NULL) {
1178 tcp->tcp_xmit_head = mp;
1179 } else {
1180 tcp->tcp_xmit_last->b_cont = mp;
1181 }
1182 tcp->tcp_xmit_last = mp;
1183 tcp->tcp_xmit_tail = mp;
1184
1185 /* find out how much we can send */
1186 /* BEGIN CSTYLED */
1187 /*
1188 * un-acked usable
1189 * |--------------|-----------------|
1190 * tcp_suna tcp_snxt tcp_suna+tcp_swnd
1191 */
1192 /* END CSTYLED */
1193
1194 /* start sending from tcp_snxt */
1195 snxt = tcp->tcp_snxt;
1196
1197 /*
1198 * Check to see if this connection has been idle for some time and no
1199 * ACK is expected. If so, then the congestion window size is no longer
1200 * meaningfully tied to current network conditions.
1201 *
1202 * We reinitialize tcp_cwnd, and slow start again to get back the
1203 * connection's "self-clock" as described in Van Jacobson's 1988 paper
1204 * "Congestion avoidance and control".
1205 */
1206 now = LBOLT_FASTPATH;
1207 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet &&
1208 (TICK_TO_MSEC(now - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) {
1209 cc_after_idle(tcp);
1210 }
1211
1212 usable = tcp->tcp_swnd; /* tcp window size */
1213 if (usable > tcp->tcp_cwnd)
1214 usable = tcp->tcp_cwnd; /* congestion window smaller */
1215 usable -= snxt; /* subtract stuff already sent */
1216 suna = tcp->tcp_suna;
1217 usable += suna;
1218 /* usable can be < 0 if the congestion window is smaller */
1219 if (len > usable) {
1220 /* Can't send complete M_DATA in one shot */
1221 goto slow;
1222 }
1223
1224 mutex_enter(&tcp->tcp_non_sq_lock);
1225 if (tcp->tcp_flow_stopped &&
1226 TCP_UNSENT_BYTES(tcp) <= connp->conn_sndlowat) {
1227 tcp_clrqfull(tcp);
1228 }
1229 mutex_exit(&tcp->tcp_non_sq_lock);
1230
1231 /*
1232 * determine if anything to send (Nagle).
1233 *
1234 * 1. len < tcp_mss (i.e. small)
1235 * 2. unacknowledged data present
1236 * 3. len < nagle limit
1237 * 4. last packet sent < nagle limit (previous packet sent)
1238 */
1239 if ((len < mss) && (snxt != suna) &&
1240 (len < (int)tcp->tcp_naglim) &&
1241 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) {
1242 /*
1243 * This was the first unsent packet and normally
1244 * mss < xmit_hiwater so there is no need to worry
1245 * about flow control. The next packet will go
1246 * through the flow control check in tcp_wput_data().
1247 */
1248 /* leftover work from above */
1249 tcp->tcp_unsent = len;
1250 tcp->tcp_xmit_tail_unsent = len;
1251
1252 return;
1253 }
1254
1255 /*
1256 * len <= tcp->tcp_mss && len == unsent so no sender silly window. Can
1257 * send now.
1258 */
1259
1260 if (snxt == suna) {
1261 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
1262 }
1263
1264 /* we have always sent something */
1265 tcp->tcp_rack_cnt = 0;
1266
1267 tcp->tcp_snxt = snxt + len;
1268 tcp->tcp_rack = tcp->tcp_rnxt;
1269
1270 if ((mp1 = dupb(mp)) == 0)
1271 goto no_memory;
1272 mp->b_prev = (mblk_t *)(intptr_t)gethrtime();
1273 mp->b_next = (mblk_t *)(uintptr_t)snxt;
1274
1275 /* adjust tcp header information */
1276 tcpha = tcp->tcp_tcpha;
1277 tcpha->tha_flags = (TH_ACK|TH_PUSH);
1278
1279 sum = len + connp->conn_ht_ulp_len + connp->conn_sum;
1280 sum = (sum >> 16) + (sum & 0xFFFF);
1281 tcpha->tha_sum = htons(sum);
1282
1283 tcpha->tha_seq = htonl(snxt);
1284
1285 TCPS_BUMP_MIB(tcps, tcpOutDataSegs);
1286 TCPS_UPDATE_MIB(tcps, tcpOutDataBytes, len);
1287 TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
1288 tcp->tcp_cs.tcp_out_data_segs++;
1289 tcp->tcp_cs.tcp_out_data_bytes += len;
1290
1291 /* Update the latest receive window size in TCP header. */
1292 tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
1293
1294 tcp->tcp_last_sent_len = (ushort_t)len;
1295
1296 plen = len + connp->conn_ht_iphc_len;
1297
1298 ixa = connp->conn_ixa;
1299 ixa->ixa_pktlen = plen;
1300
1301 if (ixa->ixa_flags & IXAF_IS_IPV4) {
1302 tcp->tcp_ipha->ipha_length = htons(plen);
1303 } else {
1304 tcp->tcp_ip6h->ip6_plen = htons(plen - IPV6_HDR_LEN);
1305 }
1306
1307 /* see if we need to allocate a mblk for the headers */
1308 hdrlen = connp->conn_ht_iphc_len;
1309 rptr = mp1->b_rptr - hdrlen;
1310 db = mp1->b_datap;
1311 if ((db->db_ref != 2) || rptr < db->db_base ||
1312 (!OK_32PTR(rptr))) {
1313 /* NOTE: we assume allocb returns an OK_32PTR */
1314 mp = allocb(hdrlen + tcps->tcps_wroff_xtra, BPRI_MED);
1315 if (!mp) {
1316 freemsg(mp1);
1317 goto no_memory;
1318 }
1319 mp->b_cont = mp1;
1320 mp1 = mp;
1321 /* Leave room for Link Level header */
1322 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra];
1323 mp1->b_wptr = &rptr[hdrlen];
1324 }
1325 mp1->b_rptr = rptr;
1326
1327 /* Fill in the timestamp option. */
1328 if (tcp->tcp_snd_ts_ok) {
1329 U32_TO_BE32(now,
1330 (char *)tcpha + TCP_MIN_HEADER_LENGTH + 4);
1331 U32_TO_BE32(tcp->tcp_ts_recent,
1332 (char *)tcpha + TCP_MIN_HEADER_LENGTH + 8);
1333 } else {
1334 ASSERT(connp->conn_ht_ulp_len == TCP_MIN_HEADER_LENGTH);
1335 }
1336
1337 /* copy header into outgoing packet */
1338 dst = (ipaddr_t *)rptr;
1339 src = (ipaddr_t *)connp->conn_ht_iphc;
1340 dst[0] = src[0];
1341 dst[1] = src[1];
1342 dst[2] = src[2];
1343 dst[3] = src[3];
1344 dst[4] = src[4];
1345 dst[5] = src[5];
1346 dst[6] = src[6];
1347 dst[7] = src[7];
1348 dst[8] = src[8];
1349 dst[9] = src[9];
1350 if (hdrlen -= 40) {
1351 hdrlen >>= 2;
1352 dst += 10;
1353 src += 10;
1354 do {
1355 *dst++ = *src++;
1356 } while (--hdrlen);
1357 }
1358
1359 /*
1360 * Set the ECN info in the TCP header. Note that this
1361 * is not the template header.
1362 */
1363 if (tcp->tcp_ecn_ok) {
1364 TCP_SET_ECT(tcp, rptr);
1365
1366 tcpha = (tcpha_t *)(rptr + ixa->ixa_ip_hdr_length);
1367 if (tcp->tcp_ecn_echo_on)
1368 tcpha->tha_flags |= TH_ECE;
1369 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) {
1370 tcpha->tha_flags |= TH_CWR;
1371 tcp->tcp_ecn_cwr_sent = B_TRUE;
1372 }
1373 }
1374
1375 if (tcp->tcp_ip_forward_progress) {
1376 tcp->tcp_ip_forward_progress = B_FALSE;
1377 connp->conn_ixa->ixa_flags |= IXAF_REACH_CONF;
1378 } else {
1379 connp->conn_ixa->ixa_flags &= ~IXAF_REACH_CONF;
1380 }
1381 tcp_send_data(tcp, mp1);
1382 return;
1383
1384 /*
1385 * If we ran out of memory, we pretend to have sent the packet
1386 * and that it was lost on the wire.
1387 */
1388 no_memory:
1389 return;
1390
1391 slow:
1392 /* leftover work from above */
1393 tcp->tcp_unsent = len;
1394 tcp->tcp_xmit_tail_unsent = len;
1395 tcp_wput_data(tcp, NULL, B_FALSE);
1396 }
1397
1398 /* ARGSUSED2 */
1399 void
tcp_output_urgent(void * arg,mblk_t * mp,void * arg2,ip_recv_attr_t * dummy)1400 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1401 {
1402 int len;
1403 uint32_t msize;
1404 conn_t *connp = (conn_t *)arg;
1405 tcp_t *tcp = connp->conn_tcp;
1406
1407 msize = msgdsize(mp);
1408
1409 len = msize - 1;
1410 if (len < 0) {
1411 freemsg(mp);
1412 return;
1413 }
1414
1415 /*
1416 * Try to force urgent data out on the wire. Even if we have unsent
1417 * data this will at least send the urgent flag.
1418 * XXX does not handle more flag correctly.
1419 */
1420 len += tcp->tcp_unsent;
1421 len += tcp->tcp_snxt;
1422 tcp->tcp_urg = len;
1423 tcp->tcp_valid_bits |= TCP_URG_VALID;
1424
1425 /* Bypass tcp protocol for fused tcp loopback */
1426 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize))
1427 return;
1428
1429 /* Strip off the T_EXDATA_REQ if the data is from TPI */
1430 if (DB_TYPE(mp) != M_DATA) {
1431 mblk_t *mp1 = mp;
1432 ASSERT(!IPCL_IS_NONSTR(connp));
1433 mp = mp->b_cont;
1434 freeb(mp1);
1435 }
1436 tcp_wput_data(tcp, mp, B_TRUE);
1437 }
1438
1439 /*
1440 * Called by streams close routine via squeues when our client blows off its
1441 * descriptor, we take this to mean: "close the stream state NOW, close the tcp
1442 * connection politely" When SO_LINGER is set (with a non-zero linger time and
1443 * it is not a nonblocking socket) then this routine sleeps until the FIN is
1444 * acked.
1445 *
1446 * NOTE: tcp_close potentially returns error when lingering.
1447 * However, the stream head currently does not pass these errors
1448 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK
1449 * errors to the application (from tsleep()) and not errors
1450 * like ECONNRESET caused by receiving a reset packet.
1451 */
1452
1453 /* ARGSUSED */
1454 void
tcp_close_output(void * arg,mblk_t * mp,void * arg2,ip_recv_attr_t * dummy)1455 tcp_close_output(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1456 {
1457 char *msg;
1458 conn_t *connp = (conn_t *)arg;
1459 tcp_t *tcp = connp->conn_tcp;
1460 clock_t delta = 0;
1461 tcp_stack_t *tcps = tcp->tcp_tcps;
1462
1463 /*
1464 * When a non-STREAMS socket is being closed, it does not always
1465 * stick around waiting for tcp_close_output to run and can therefore
1466 * have dropped a reference already. So adjust the asserts accordingly.
1467 */
1468 ASSERT((connp->conn_fanout != NULL &&
1469 connp->conn_ref >= (IPCL_IS_NONSTR(connp) ? 3 : 4)) ||
1470 (connp->conn_fanout == NULL &&
1471 connp->conn_ref >= (IPCL_IS_NONSTR(connp) ? 2 : 3)));
1472
1473 mutex_enter(&tcp->tcp_eager_lock);
1474 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) {
1475 /*
1476 * Cleanup for listener. For non-STREAM sockets sockfs will
1477 * close all the eagers on 'q', so in that case only deal
1478 * with 'q0'.
1479 */
1480 tcp_eager_cleanup(tcp, IPCL_IS_NONSTR(connp) ? 1 : 0);
1481 tcp->tcp_wait_for_eagers = 1;
1482 }
1483 mutex_exit(&tcp->tcp_eager_lock);
1484
1485 tcp->tcp_lso = B_FALSE;
1486
1487 msg = NULL;
1488 switch (tcp->tcp_state) {
1489 case TCPS_CLOSED:
1490 case TCPS_IDLE:
1491 break;
1492 case TCPS_BOUND:
1493 if (tcp->tcp_listener != NULL) {
1494 ASSERT(IPCL_IS_NONSTR(connp));
1495 /*
1496 * Unlink from the listener and drop the reference
1497 * put on it by the eager. tcp_closei_local will not
1498 * do it because tcp_tconnind_started is TRUE.
1499 */
1500 mutex_enter(&tcp->tcp_saved_listener->tcp_eager_lock);
1501 tcp_eager_unlink(tcp);
1502 mutex_exit(&tcp->tcp_saved_listener->tcp_eager_lock);
1503 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp);
1504 }
1505 break;
1506 case TCPS_LISTEN:
1507 break;
1508 case TCPS_SYN_SENT:
1509 msg = "tcp_close, during connect";
1510 break;
1511 case TCPS_SYN_RCVD:
1512 /*
1513 * Close during the connect 3-way handshake
1514 * but here there may or may not be pending data
1515 * already on queue. Process almost same as in
1516 * the ESTABLISHED state.
1517 */
1518 /* FALLTHRU */
1519 default:
1520 if (tcp->tcp_fused)
1521 tcp_unfuse(tcp);
1522
1523 /*
1524 * If SO_LINGER has set a zero linger time, abort the
1525 * connection with a reset.
1526 */
1527 if (connp->conn_linger && connp->conn_lingertime == 0) {
1528 msg = "tcp_close, zero lingertime";
1529 break;
1530 }
1531
1532 /*
1533 * Abort connection if there is unread data queued.
1534 */
1535 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) {
1536 msg = "tcp_close, unread data";
1537 break;
1538 }
1539
1540 /*
1541 * Abort connection if it is being closed without first
1542 * being accepted. This can happen if a listening non-STREAM
1543 * socket wants to get rid of the socket, for example, if the
1544 * listener is closing.
1545 */
1546 if (tcp->tcp_listener != NULL) {
1547 ASSERT(IPCL_IS_NONSTR(connp));
1548 msg = "tcp_close, close before accept";
1549
1550 /*
1551 * Unlink from the listener and drop the reference
1552 * put on it by the eager. tcp_closei_local will not
1553 * do it because tcp_tconnind_started is TRUE.
1554 */
1555 mutex_enter(&tcp->tcp_saved_listener->tcp_eager_lock);
1556 tcp_eager_unlink(tcp);
1557 mutex_exit(&tcp->tcp_saved_listener->tcp_eager_lock);
1558 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp);
1559 break;
1560 }
1561
1562 /*
1563 * Transmit the FIN before detaching the tcp_t.
1564 * After tcp_detach returns this queue/perimeter
1565 * no longer owns the tcp_t thus others can modify it.
1566 */
1567 (void) tcp_xmit_end(tcp);
1568
1569 /*
1570 * If lingering on close then wait until the fin is acked,
1571 * the SO_LINGER time passes, or a reset is sent/received.
1572 */
1573 if (connp->conn_linger && connp->conn_lingertime > 0 &&
1574 !(tcp->tcp_fin_acked) &&
1575 tcp->tcp_state >= TCPS_ESTABLISHED) {
1576 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) {
1577 tcp->tcp_client_errno = EWOULDBLOCK;
1578 } else if (tcp->tcp_client_errno == 0) {
1579
1580 ASSERT(tcp->tcp_linger_tid == 0);
1581
1582 /* conn_lingertime is in sec. */
1583 tcp->tcp_linger_tid = TCP_TIMER(tcp,
1584 tcp_close_linger_timeout,
1585 connp->conn_lingertime * MILLISEC);
1586
1587 /* tcp_close_linger_timeout will finish close */
1588 if (tcp->tcp_linger_tid == 0)
1589 tcp->tcp_client_errno = ENOSR;
1590 else
1591 return;
1592 }
1593
1594 /*
1595 * Check if we need to detach or just close
1596 * the instance.
1597 */
1598 if (tcp->tcp_state <= TCPS_LISTEN)
1599 break;
1600 }
1601
1602 /*
1603 * Make sure that no other thread will access the conn_rq of
1604 * this instance (through lookups etc.) as conn_rq will go
1605 * away shortly.
1606 */
1607 tcp_acceptor_hash_remove(tcp);
1608
1609 mutex_enter(&tcp->tcp_non_sq_lock);
1610 if (tcp->tcp_flow_stopped) {
1611 tcp_clrqfull(tcp);
1612 }
1613 mutex_exit(&tcp->tcp_non_sq_lock);
1614
1615 if (tcp->tcp_timer_tid != 0) {
1616 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid);
1617 tcp->tcp_timer_tid = 0;
1618 }
1619 /*
1620 * Need to cancel those timers which will not be used when
1621 * TCP is detached. This has to be done before the conn_wq
1622 * is set to NULL.
1623 */
1624 tcp_timers_stop(tcp);
1625
1626 tcp->tcp_detached = B_TRUE;
1627 if (tcp->tcp_state == TCPS_TIME_WAIT) {
1628 tcp_time_wait_append(tcp);
1629 TCP_DBGSTAT(tcps, tcp_detach_time_wait);
1630 ASSERT(connp->conn_ref >=
1631 (IPCL_IS_NONSTR(connp) ? 2 : 3));
1632 goto finish;
1633 }
1634
1635 /*
1636 * If delta is zero the timer event wasn't executed and was
1637 * successfully canceled. In this case we need to restart it
1638 * with the minimal delta possible.
1639 */
1640 if (delta >= 0)
1641 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer,
1642 delta ? delta : 1);
1643
1644 ASSERT(connp->conn_ref >= (IPCL_IS_NONSTR(connp) ? 2 : 3));
1645 goto finish;
1646 }
1647
1648 /* Detach did not complete. Still need to remove q from stream. */
1649 if (msg) {
1650 if (tcp->tcp_state == TCPS_ESTABLISHED ||
1651 tcp->tcp_state == TCPS_CLOSE_WAIT)
1652 TCPS_BUMP_MIB(tcps, tcpEstabResets);
1653 if (tcp->tcp_state == TCPS_SYN_SENT ||
1654 tcp->tcp_state == TCPS_SYN_RCVD)
1655 TCPS_BUMP_MIB(tcps, tcpAttemptFails);
1656 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST);
1657 }
1658
1659 tcp_closei_local(tcp);
1660 CONN_DEC_REF(connp);
1661 ASSERT(connp->conn_ref >= (IPCL_IS_NONSTR(connp) ? 1 : 2));
1662
1663 finish:
1664 /*
1665 * Don't change the queues in the case of a listener that has
1666 * eagers in its q or q0. It could surprise the eagers.
1667 * Instead wait for the eagers outside the squeue.
1668 *
1669 * For non-STREAMS sockets tcp_wait_for_eagers implies that
1670 * we should delay the su_closed upcall until all eagers have
1671 * dropped their references.
1672 */
1673 if (!tcp->tcp_wait_for_eagers) {
1674 tcp->tcp_detached = B_TRUE;
1675 connp->conn_rq = NULL;
1676 connp->conn_wq = NULL;
1677
1678 /* non-STREAM socket, release the upper handle */
1679 if (IPCL_IS_NONSTR(connp)) {
1680 sock_upcalls_t *upcalls = connp->conn_upcalls;
1681 sock_upper_handle_t handle = connp->conn_upper_handle;
1682
1683 ASSERT(upcalls != NULL);
1684 ASSERT(upcalls->su_closed != NULL);
1685 ASSERT(handle != NULL);
1686 /*
1687 * Set these to NULL first because closed() will free
1688 * upper structures. Acquire conn_lock because an
1689 * external caller like conn_get_socket_info() will
1690 * upcall if these are non-NULL.
1691 */
1692 mutex_enter(&connp->conn_lock);
1693 connp->conn_upper_handle = NULL;
1694 connp->conn_upcalls = NULL;
1695 mutex_exit(&connp->conn_lock);
1696 upcalls->su_closed(handle);
1697 }
1698 }
1699
1700 /* Signal tcp_close() to finish closing. */
1701 mutex_enter(&tcp->tcp_closelock);
1702 tcp->tcp_closed = 1;
1703 cv_signal(&tcp->tcp_closecv);
1704 mutex_exit(&tcp->tcp_closelock);
1705 }
1706
1707 /* ARGSUSED */
1708 void
tcp_shutdown_output(void * arg,mblk_t * mp,void * arg2,ip_recv_attr_t * dummy)1709 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1710 {
1711 conn_t *connp = (conn_t *)arg;
1712 tcp_t *tcp = connp->conn_tcp;
1713
1714 freemsg(mp);
1715
1716 if (tcp->tcp_fused)
1717 tcp_unfuse(tcp);
1718
1719 if (tcp_xmit_end(tcp) != 0) {
1720 /*
1721 * We were crossing FINs and got a reset from
1722 * the other side. Just ignore it.
1723 */
1724 if (connp->conn_debug) {
1725 (void) strlog(TCP_MOD_ID, 0, 1,
1726 SL_ERROR|SL_TRACE,
1727 "tcp_shutdown_output() out of state %s",
1728 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT));
1729 }
1730 }
1731 }
1732
1733 #pragma inline(tcp_send_data)
1734
1735 void
tcp_send_data(tcp_t * tcp,mblk_t * mp)1736 tcp_send_data(tcp_t *tcp, mblk_t *mp)
1737 {
1738 conn_t *connp = tcp->tcp_connp;
1739
1740 /*
1741 * Check here to avoid sending zero-copy message down to IP when
1742 * ZEROCOPY capability has turned off. We only need to deal with
1743 * the race condition between sockfs and the notification here.
1744 * Since we have tried to backoff the tcp_xmit_head when turning
1745 * zero-copy off and new messages in tcp_output(), we simply drop
1746 * the dup'ed packet here and let tcp retransmit, if tcp_xmit_zc_clean
1747 * is not true.
1748 */
1749 if (tcp->tcp_snd_zcopy_aware && !tcp->tcp_snd_zcopy_on &&
1750 !tcp->tcp_xmit_zc_clean) {
1751 ip_drop_output("TCP ZC was disabled but not clean", mp, NULL);
1752 freemsg(mp);
1753 return;
1754 }
1755
1756 DTRACE_TCP5(send, mblk_t *, NULL, ip_xmit_attr_t *, connp->conn_ixa,
1757 __dtrace_tcp_void_ip_t *, mp->b_rptr, tcp_t *, tcp,
1758 __dtrace_tcp_tcph_t *,
1759 &mp->b_rptr[connp->conn_ixa->ixa_ip_hdr_length]);
1760
1761 ASSERT(connp->conn_ixa->ixa_notify_cookie == connp->conn_tcp);
1762 (void) conn_ip_output(mp, connp->conn_ixa);
1763 }
1764
1765 /* ARGSUSED2 */
1766 void
tcp_send_synack(void * arg,mblk_t * mp,void * arg2,ip_recv_attr_t * dummy)1767 tcp_send_synack(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1768 {
1769 conn_t *econnp = (conn_t *)arg;
1770 tcp_t *tcp = econnp->conn_tcp;
1771 ip_xmit_attr_t *ixa = econnp->conn_ixa;
1772
1773 /* Guard against a RST having blown it away while on the squeue */
1774 if (tcp->tcp_state == TCPS_CLOSED) {
1775 freemsg(mp);
1776 return;
1777 }
1778
1779 /*
1780 * In the off-chance that the eager received and responded to
1781 * some other packet while the SYN|ACK was queued, we recalculate
1782 * the ixa_pktlen. It would be better to fix the SYN/accept
1783 * multithreading scheme to avoid this complexity.
1784 */
1785 ixa->ixa_pktlen = msgdsize(mp);
1786 (void) conn_ip_output(mp, ixa);
1787 }
1788
1789 /*
1790 * tcp_send() is called by tcp_wput_data() and returns one of the following:
1791 *
1792 * -1 = failed allocation.
1793 * 0 = We've either successfully sent data, or our usable send window is too
1794 * small and we'd rather wait until later before sending again.
1795 */
1796 static int
tcp_send(tcp_t * tcp,const int mss,const int total_hdr_len,const int tcp_hdr_len,const int num_sack_blk,int * usable,uint32_t * snxt,int * tail_unsent,mblk_t ** xmit_tail,mblk_t * local_time)1797 tcp_send(tcp_t *tcp, const int mss, const int total_hdr_len,
1798 const int tcp_hdr_len, const int num_sack_blk, int *usable,
1799 uint32_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time)
1800 {
1801 int num_lso_seg = 1;
1802 uint_t lso_usable = 0;
1803 boolean_t do_lso_send = B_FALSE;
1804 tcp_stack_t *tcps = tcp->tcp_tcps;
1805 conn_t *connp = tcp->tcp_connp;
1806 ip_xmit_attr_t *ixa = connp->conn_ixa;
1807
1808 /*
1809 * Check LSO possibility. The value of tcp->tcp_lso indicates whether
1810 * the underlying connection is LSO capable. Will check whether having
1811 * enough available data to initiate LSO transmission in the for(){}
1812 * loops.
1813 */
1814 if (tcp->tcp_lso && (tcp->tcp_valid_bits & ~TCP_FSS_VALID) == 0)
1815 do_lso_send = B_TRUE;
1816
1817 for (;;) {
1818 struct datab *db;
1819 tcpha_t *tcpha;
1820 uint32_t sum;
1821 mblk_t *mp, *mp1;
1822 uchar_t *rptr;
1823 int len;
1824
1825 /*
1826 * Calculate the maximum payload length we can send at one
1827 * time.
1828 */
1829 if (do_lso_send) {
1830 /*
1831 * Determine whether or not it's possible to do LSO,
1832 * and if so, how much data we can send.
1833 */
1834 if ((*usable - 1) / mss >= 1) {
1835 lso_usable = MIN(tcp->tcp_lso_max, *usable);
1836 num_lso_seg = lso_usable / mss;
1837 if (lso_usable % mss) {
1838 num_lso_seg++;
1839 tcp->tcp_last_sent_len = (ushort_t)
1840 (lso_usable % mss);
1841 } else {
1842 tcp->tcp_last_sent_len = (ushort_t)mss;
1843 }
1844 } else {
1845 do_lso_send = B_FALSE;
1846 num_lso_seg = 1;
1847 lso_usable = mss;
1848 }
1849 }
1850
1851 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1);
1852
1853 len = mss;
1854 if (len > *usable) {
1855 ASSERT(do_lso_send == B_FALSE);
1856
1857 len = *usable;
1858 if (len <= 0) {
1859 /* Terminate the loop */
1860 break; /* success; too small */
1861 }
1862 /*
1863 * Sender silly-window avoidance.
1864 * Ignore this if we are going to send a
1865 * zero window probe out.
1866 *
1867 * TODO: force data into microscopic window?
1868 * ==> (!pushed || (unsent > usable))
1869 */
1870 if (len < (tcp->tcp_max_swnd >> 1) &&
1871 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len &&
1872 !((tcp->tcp_valid_bits & TCP_URG_VALID) &&
1873 len == 1) && (! tcp->tcp_zero_win_probe)) {
1874 /*
1875 * If the retransmit timer is not running
1876 * we start it so that we will retransmit
1877 * in the case when the receiver has
1878 * decremented the window.
1879 */
1880 if (*snxt == tcp->tcp_snxt &&
1881 *snxt == tcp->tcp_suna) {
1882 /*
1883 * We are not supposed to send
1884 * anything. So let's wait a little
1885 * bit longer before breaking SWS
1886 * avoidance.
1887 *
1888 * What should the value be?
1889 * Suggestion: MAX(init rexmit time,
1890 * tcp->tcp_rto)
1891 */
1892 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
1893 }
1894 break; /* success; too small */
1895 }
1896 }
1897
1898 tcpha = tcp->tcp_tcpha;
1899
1900 /*
1901 * The reason to adjust len here is that we need to set flags
1902 * and calculate checksum.
1903 */
1904 if (do_lso_send)
1905 len = lso_usable;
1906
1907 *usable -= len; /* Approximate - can be adjusted later */
1908 if (*usable > 0)
1909 tcpha->tha_flags = TH_ACK;
1910 else
1911 tcpha->tha_flags = (TH_ACK | TH_PUSH);
1912
1913 /*
1914 * Prime pump for IP's checksumming on our behalf.
1915 * Include the adjustment for a source route if any.
1916 * In case of LSO, the partial pseudo-header checksum should
1917 * exclusive TCP length, so zero tha_sum before IP calculate
1918 * pseudo-header checksum for partial checksum offload.
1919 */
1920 if (do_lso_send) {
1921 sum = 0;
1922 } else {
1923 sum = len + tcp_hdr_len + connp->conn_sum;
1924 sum = (sum >> 16) + (sum & 0xFFFF);
1925 }
1926 tcpha->tha_sum = htons(sum);
1927 tcpha->tha_seq = htonl(*snxt);
1928
1929 /*
1930 * Branch off to tcp_xmit_mp() if any of the VALID bits is
1931 * set. For the case when TCP_FSS_VALID is the only valid
1932 * bit (normal active close), branch off only when we think
1933 * that the FIN flag needs to be set. Note for this case,
1934 * that (snxt + len) may not reflect the actual seg_len,
1935 * as len may be further reduced in tcp_xmit_mp(). If len
1936 * gets modified, we will end up here again.
1937 */
1938 if (tcp->tcp_valid_bits != 0 &&
1939 (tcp->tcp_valid_bits != TCP_FSS_VALID ||
1940 ((*snxt + len) == tcp->tcp_fss))) {
1941 uchar_t *prev_rptr;
1942 uint32_t prev_snxt = tcp->tcp_snxt;
1943
1944 if (*tail_unsent == 0) {
1945 ASSERT((*xmit_tail)->b_cont != NULL);
1946 *xmit_tail = (*xmit_tail)->b_cont;
1947 prev_rptr = (*xmit_tail)->b_rptr;
1948 *tail_unsent = (int)((*xmit_tail)->b_wptr -
1949 (*xmit_tail)->b_rptr);
1950 } else {
1951 prev_rptr = (*xmit_tail)->b_rptr;
1952 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr -
1953 *tail_unsent;
1954 }
1955 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL,
1956 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE);
1957 /* Restore tcp_snxt so we get amount sent right. */
1958 tcp->tcp_snxt = prev_snxt;
1959 if (prev_rptr == (*xmit_tail)->b_rptr) {
1960 /*
1961 * If the previous timestamp is still in use,
1962 * don't stomp on it.
1963 */
1964 if ((*xmit_tail)->b_next == NULL) {
1965 (*xmit_tail)->b_prev = local_time;
1966 (*xmit_tail)->b_next =
1967 (mblk_t *)(uintptr_t)(*snxt);
1968 }
1969 } else
1970 (*xmit_tail)->b_rptr = prev_rptr;
1971
1972 if (mp == NULL) {
1973 return (-1);
1974 }
1975 mp1 = mp->b_cont;
1976
1977 if (len <= mss) /* LSO is unusable (!do_lso_send) */
1978 tcp->tcp_last_sent_len = (ushort_t)len;
1979 while (mp1->b_cont) {
1980 *xmit_tail = (*xmit_tail)->b_cont;
1981 (*xmit_tail)->b_prev = local_time;
1982 (*xmit_tail)->b_next =
1983 (mblk_t *)(uintptr_t)(*snxt);
1984 mp1 = mp1->b_cont;
1985 }
1986 *snxt += len;
1987 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr;
1988 TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
1989 TCPS_BUMP_MIB(tcps, tcpOutDataSegs);
1990 TCPS_UPDATE_MIB(tcps, tcpOutDataBytes, len);
1991 tcp->tcp_cs.tcp_out_data_segs++;
1992 tcp->tcp_cs.tcp_out_data_bytes += len;
1993 tcp_send_data(tcp, mp);
1994 continue;
1995 }
1996
1997 *snxt += len; /* Adjust later if we don't send all of len */
1998 TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
1999 TCPS_BUMP_MIB(tcps, tcpOutDataSegs);
2000 TCPS_UPDATE_MIB(tcps, tcpOutDataBytes, len);
2001 tcp->tcp_cs.tcp_out_data_segs++;
2002 tcp->tcp_cs.tcp_out_data_bytes += len;
2003
2004 if (*tail_unsent) {
2005 /* Are the bytes above us in flight? */
2006 rptr = (*xmit_tail)->b_wptr - *tail_unsent;
2007 if (rptr != (*xmit_tail)->b_rptr) {
2008 *tail_unsent -= len;
2009 if (len <= mss) /* LSO is unusable */
2010 tcp->tcp_last_sent_len = (ushort_t)len;
2011 len += total_hdr_len;
2012 ixa->ixa_pktlen = len;
2013
2014 if (ixa->ixa_flags & IXAF_IS_IPV4) {
2015 tcp->tcp_ipha->ipha_length = htons(len);
2016 } else {
2017 tcp->tcp_ip6h->ip6_plen =
2018 htons(len - IPV6_HDR_LEN);
2019 }
2020
2021 mp = dupb(*xmit_tail);
2022 if (mp == NULL) {
2023 return (-1); /* out_of_mem */
2024 }
2025 mp->b_rptr = rptr;
2026 /*
2027 * If the old timestamp is no longer in use,
2028 * sample a new timestamp now.
2029 */
2030 if ((*xmit_tail)->b_next == NULL) {
2031 (*xmit_tail)->b_prev = local_time;
2032 (*xmit_tail)->b_next =
2033 (mblk_t *)(uintptr_t)(*snxt-len);
2034 }
2035 goto must_alloc;
2036 }
2037 } else {
2038 *xmit_tail = (*xmit_tail)->b_cont;
2039 ASSERT((uintptr_t)((*xmit_tail)->b_wptr -
2040 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX);
2041 *tail_unsent = (int)((*xmit_tail)->b_wptr -
2042 (*xmit_tail)->b_rptr);
2043 }
2044
2045 (*xmit_tail)->b_prev = local_time;
2046 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len);
2047
2048 *tail_unsent -= len;
2049 if (len <= mss) /* LSO is unusable (!do_lso_send) */
2050 tcp->tcp_last_sent_len = (ushort_t)len;
2051
2052 len += total_hdr_len;
2053 ixa->ixa_pktlen = len;
2054
2055 if (ixa->ixa_flags & IXAF_IS_IPV4) {
2056 tcp->tcp_ipha->ipha_length = htons(len);
2057 } else {
2058 tcp->tcp_ip6h->ip6_plen = htons(len - IPV6_HDR_LEN);
2059 }
2060
2061 mp = dupb(*xmit_tail);
2062 if (mp == NULL) {
2063 return (-1); /* out_of_mem */
2064 }
2065
2066 len = total_hdr_len;
2067 /*
2068 * There are four reasons to allocate a new hdr mblk:
2069 * 1) The bytes above us are in use by another packet
2070 * 2) We don't have good alignment
2071 * 3) The mblk is being shared
2072 * 4) We don't have enough room for a header
2073 */
2074 rptr = mp->b_rptr - len;
2075 if (!OK_32PTR(rptr) ||
2076 ((db = mp->b_datap), db->db_ref != 2) ||
2077 rptr < db->db_base) {
2078 /* NOTE: we assume allocb returns an OK_32PTR */
2079
2080 must_alloc:;
2081 mp1 = allocb(connp->conn_ht_iphc_allocated +
2082 tcps->tcps_wroff_xtra, BPRI_MED);
2083 if (mp1 == NULL) {
2084 freemsg(mp);
2085 return (-1); /* out_of_mem */
2086 }
2087 mp1->b_cont = mp;
2088 mp = mp1;
2089 /* Leave room for Link Level header */
2090 len = total_hdr_len;
2091 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra];
2092 mp->b_wptr = &rptr[len];
2093 }
2094
2095 /*
2096 * Fill in the header using the template header, and add
2097 * options such as time-stamp, ECN and/or SACK, as needed.
2098 */
2099 tcp_fill_header(tcp, rptr, num_sack_blk);
2100
2101 mp->b_rptr = rptr;
2102
2103 if (*tail_unsent) {
2104 int spill = *tail_unsent;
2105
2106 mp1 = mp->b_cont;
2107 if (mp1 == NULL)
2108 mp1 = mp;
2109
2110 /*
2111 * If we're a little short, tack on more mblks until
2112 * there is no more spillover.
2113 */
2114 while (spill < 0) {
2115 mblk_t *nmp;
2116 int nmpsz;
2117
2118 nmp = (*xmit_tail)->b_cont;
2119 nmpsz = MBLKL(nmp);
2120
2121 /*
2122 * Excess data in mblk; can we split it?
2123 * If LSO is enabled for the connection,
2124 * keep on splitting as this is a transient
2125 * send path.
2126 */
2127 if (!do_lso_send && (spill + nmpsz > 0)) {
2128 /*
2129 * Don't split if stream head was
2130 * told to break up larger writes
2131 * into smaller ones.
2132 */
2133 if (tcp->tcp_maxpsz_multiplier > 0)
2134 break;
2135
2136 /*
2137 * Next mblk is less than SMSS/2
2138 * rounded up to nearest 64-byte;
2139 * let it get sent as part of the
2140 * next segment.
2141 */
2142 if (tcp->tcp_localnet &&
2143 !tcp->tcp_cork &&
2144 (nmpsz < roundup((mss >> 1), 64)))
2145 break;
2146 }
2147
2148 *xmit_tail = nmp;
2149 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX);
2150 /* Stash for rtt use later */
2151 (*xmit_tail)->b_prev = local_time;
2152 (*xmit_tail)->b_next =
2153 (mblk_t *)(uintptr_t)(*snxt - len);
2154 mp1->b_cont = dupb(*xmit_tail);
2155 mp1 = mp1->b_cont;
2156
2157 spill += nmpsz;
2158 if (mp1 == NULL) {
2159 *tail_unsent = spill;
2160 freemsg(mp);
2161 return (-1); /* out_of_mem */
2162 }
2163 }
2164
2165 /* Trim back any surplus on the last mblk */
2166 if (spill >= 0) {
2167 mp1->b_wptr -= spill;
2168 *tail_unsent = spill;
2169 } else {
2170 /*
2171 * We did not send everything we could in
2172 * order to remain within the b_cont limit.
2173 */
2174 *usable -= spill;
2175 *snxt += spill;
2176 tcp->tcp_last_sent_len += spill;
2177 TCPS_UPDATE_MIB(tcps, tcpOutDataBytes, spill);
2178 tcp->tcp_cs.tcp_out_data_bytes += spill;
2179 /*
2180 * Adjust the checksum
2181 */
2182 tcpha = (tcpha_t *)(rptr +
2183 ixa->ixa_ip_hdr_length);
2184 sum += spill;
2185 sum = (sum >> 16) + (sum & 0xFFFF);
2186 tcpha->tha_sum = htons(sum);
2187 if (connp->conn_ipversion == IPV4_VERSION) {
2188 sum = ntohs(
2189 ((ipha_t *)rptr)->ipha_length) +
2190 spill;
2191 ((ipha_t *)rptr)->ipha_length =
2192 htons(sum);
2193 } else {
2194 sum = ntohs(
2195 ((ip6_t *)rptr)->ip6_plen) +
2196 spill;
2197 ((ip6_t *)rptr)->ip6_plen =
2198 htons(sum);
2199 }
2200 ixa->ixa_pktlen += spill;
2201 *tail_unsent = 0;
2202 }
2203 }
2204 if (tcp->tcp_ip_forward_progress) {
2205 tcp->tcp_ip_forward_progress = B_FALSE;
2206 ixa->ixa_flags |= IXAF_REACH_CONF;
2207 } else {
2208 ixa->ixa_flags &= ~IXAF_REACH_CONF;
2209 }
2210
2211 if (do_lso_send) {
2212 /* Append LSO information to the mp. */
2213 lso_info_set(mp, mss, HW_LSO);
2214 ixa->ixa_fragsize = IP_MAXPACKET;
2215 ixa->ixa_extra_ident = num_lso_seg - 1;
2216
2217 DTRACE_PROBE2(tcp_send_lso, int, num_lso_seg,
2218 boolean_t, B_TRUE);
2219
2220 tcp_send_data(tcp, mp);
2221
2222 /*
2223 * Restore values of ixa_fragsize and ixa_extra_ident.
2224 */
2225 ixa->ixa_fragsize = ixa->ixa_pmtu;
2226 ixa->ixa_extra_ident = 0;
2227 TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
2228 TCP_STAT(tcps, tcp_lso_times);
2229 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg);
2230 } else {
2231 /*
2232 * Make sure to clean up LSO information. Wherever a
2233 * new mp uses the prepended header room after dupb(),
2234 * lso_info_cleanup() should be called.
2235 */
2236 lso_info_cleanup(mp);
2237 tcp_send_data(tcp, mp);
2238 TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
2239 }
2240 }
2241
2242 return (0);
2243 }
2244
2245 /*
2246 * Initiate closedown sequence on an active connection. (May be called as
2247 * writer.) Return value zero for OK return, non-zero for error return.
2248 */
2249 static int
tcp_xmit_end(tcp_t * tcp)2250 tcp_xmit_end(tcp_t *tcp)
2251 {
2252 mblk_t *mp;
2253 tcp_stack_t *tcps = tcp->tcp_tcps;
2254 iulp_t uinfo;
2255 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip;
2256 conn_t *connp = tcp->tcp_connp;
2257
2258 if (tcp->tcp_state < TCPS_SYN_RCVD ||
2259 tcp->tcp_state > TCPS_CLOSE_WAIT) {
2260 /*
2261 * Invalid state, only states TCPS_SYN_RCVD,
2262 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid
2263 */
2264 return (-1);
2265 }
2266
2267 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent;
2268 tcp->tcp_valid_bits |= TCP_FSS_VALID;
2269 /*
2270 * If there is nothing more unsent, send the FIN now.
2271 * Otherwise, it will go out with the last segment.
2272 */
2273 if (tcp->tcp_unsent == 0) {
2274 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL,
2275 tcp->tcp_fss, B_FALSE, NULL, B_FALSE);
2276
2277 if (mp) {
2278 tcp_send_data(tcp, mp);
2279 } else {
2280 /*
2281 * Couldn't allocate msg. Pretend we got it out.
2282 * Wait for rexmit timeout.
2283 */
2284 tcp->tcp_snxt = tcp->tcp_fss + 1;
2285 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
2286 }
2287
2288 /*
2289 * If needed, update tcp_rexmit_snxt as tcp_snxt is
2290 * changed.
2291 */
2292 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) {
2293 tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
2294 }
2295 } else {
2296 /*
2297 * If tcp->tcp_cork is set, then the data will not get sent,
2298 * so we have to check that and unset it first.
2299 */
2300 if (tcp->tcp_cork)
2301 tcp->tcp_cork = B_FALSE;
2302 tcp_wput_data(tcp, NULL, B_FALSE);
2303 }
2304
2305 /*
2306 * If TCP does not get enough samples of RTT or tcp_rtt_updates
2307 * is 0, don't update the cache.
2308 */
2309 if (tcps->tcps_rtt_updates == 0 ||
2310 tcp->tcp_rtt_update < tcps->tcps_rtt_updates)
2311 return (0);
2312
2313 /*
2314 * We do not have a good algorithm to update ssthresh at this time.
2315 * So don't do any update.
2316 */
2317 bzero(&uinfo, sizeof (uinfo));
2318 uinfo.iulp_rtt = NSEC2MSEC(tcp->tcp_rtt_sa);
2319 uinfo.iulp_rtt_sd = NSEC2MSEC(tcp->tcp_rtt_sd);
2320
2321 /*
2322 * Note that uinfo is kept for conn_faddr in the DCE. Could update even
2323 * if source routed but we don't.
2324 */
2325 if (connp->conn_ipversion == IPV4_VERSION) {
2326 if (connp->conn_faddr_v4 != tcp->tcp_ipha->ipha_dst) {
2327 return (0);
2328 }
2329 (void) dce_update_uinfo_v4(connp->conn_faddr_v4, &uinfo, ipst);
2330 } else {
2331 uint_t ifindex;
2332
2333 if (!(IN6_ARE_ADDR_EQUAL(&connp->conn_faddr_v6,
2334 &tcp->tcp_ip6h->ip6_dst))) {
2335 return (0);
2336 }
2337 ifindex = 0;
2338 if (IN6_IS_ADDR_LINKSCOPE(&connp->conn_faddr_v6)) {
2339 ip_xmit_attr_t *ixa = connp->conn_ixa;
2340
2341 /*
2342 * If we are going to create a DCE we'd better have
2343 * an ifindex
2344 */
2345 if (ixa->ixa_nce != NULL) {
2346 ifindex = ixa->ixa_nce->nce_common->ncec_ill->
2347 ill_phyint->phyint_ifindex;
2348 } else {
2349 return (0);
2350 }
2351 }
2352
2353 (void) dce_update_uinfo(&connp->conn_faddr_v6, ifindex, &uinfo,
2354 ipst);
2355 }
2356 return (0);
2357 }
2358
2359 /*
2360 * Send out a control packet on the tcp connection specified. This routine
2361 * is typically called where we need a simple ACK or RST generated.
2362 */
2363 void
tcp_xmit_ctl(char * str,tcp_t * tcp,uint32_t seq,uint32_t ack,int ctl)2364 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl)
2365 {
2366 uchar_t *rptr;
2367 tcpha_t *tcpha;
2368 ipha_t *ipha = NULL;
2369 ip6_t *ip6h = NULL;
2370 uint32_t sum;
2371 int total_hdr_len;
2372 int ip_hdr_len;
2373 mblk_t *mp;
2374 tcp_stack_t *tcps = tcp->tcp_tcps;
2375 conn_t *connp = tcp->tcp_connp;
2376 ip_xmit_attr_t *ixa = connp->conn_ixa;
2377
2378 /*
2379 * Save sum for use in source route later.
2380 */
2381 sum = connp->conn_ht_ulp_len + connp->conn_sum;
2382 total_hdr_len = connp->conn_ht_iphc_len;
2383 ip_hdr_len = ixa->ixa_ip_hdr_length;
2384
2385 /* If a text string is passed in with the request, pass it to strlog. */
2386 if (str != NULL && connp->conn_debug) {
2387 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
2388 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x",
2389 str, seq, ack, ctl);
2390 }
2391 mp = allocb(connp->conn_ht_iphc_allocated + tcps->tcps_wroff_xtra,
2392 BPRI_MED);
2393 if (mp == NULL) {
2394 return;
2395 }
2396 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra];
2397 mp->b_rptr = rptr;
2398 mp->b_wptr = &rptr[total_hdr_len];
2399 bcopy(connp->conn_ht_iphc, rptr, total_hdr_len);
2400
2401 ixa->ixa_pktlen = total_hdr_len;
2402
2403 if (ixa->ixa_flags & IXAF_IS_IPV4) {
2404 ipha = (ipha_t *)rptr;
2405 ipha->ipha_length = htons(total_hdr_len);
2406 } else {
2407 ip6h = (ip6_t *)rptr;
2408 ip6h->ip6_plen = htons(total_hdr_len - IPV6_HDR_LEN);
2409 }
2410 tcpha = (tcpha_t *)&rptr[ip_hdr_len];
2411 tcpha->tha_flags = (uint8_t)ctl;
2412 if (ctl & TH_RST) {
2413 TCPS_BUMP_MIB(tcps, tcpOutRsts);
2414 TCPS_BUMP_MIB(tcps, tcpOutControl);
2415 /*
2416 * Don't send TSopt w/ TH_RST packets per RFC 1323.
2417 */
2418 if (tcp->tcp_snd_ts_ok &&
2419 tcp->tcp_state > TCPS_SYN_SENT) {
2420 mp->b_wptr = &rptr[total_hdr_len - TCPOPT_REAL_TS_LEN];
2421 *(mp->b_wptr) = TCPOPT_EOL;
2422
2423 ixa->ixa_pktlen = total_hdr_len - TCPOPT_REAL_TS_LEN;
2424
2425 if (connp->conn_ipversion == IPV4_VERSION) {
2426 ipha->ipha_length = htons(total_hdr_len -
2427 TCPOPT_REAL_TS_LEN);
2428 } else {
2429 ip6h->ip6_plen = htons(total_hdr_len -
2430 IPV6_HDR_LEN - TCPOPT_REAL_TS_LEN);
2431 }
2432 tcpha->tha_offset_and_reserved -= (3 << 4);
2433 sum -= TCPOPT_REAL_TS_LEN;
2434 }
2435 }
2436 if (ctl & TH_ACK) {
2437 if (tcp->tcp_snd_ts_ok) {
2438 uint32_t llbolt = (uint32_t)LBOLT_FASTPATH;
2439
2440 U32_TO_BE32(llbolt,
2441 (char *)tcpha + TCP_MIN_HEADER_LENGTH+4);
2442 U32_TO_BE32(tcp->tcp_ts_recent,
2443 (char *)tcpha + TCP_MIN_HEADER_LENGTH+8);
2444 }
2445
2446 /* Update the latest receive window size in TCP header. */
2447 tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
2448 /* Track what we sent to the peer */
2449 tcp->tcp_tcpha->tha_win = tcpha->tha_win;
2450 tcp->tcp_rack = ack;
2451 tcp->tcp_rack_cnt = 0;
2452 TCPS_BUMP_MIB(tcps, tcpOutAck);
2453 }
2454 TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
2455 tcpha->tha_seq = htonl(seq);
2456 tcpha->tha_ack = htonl(ack);
2457 /*
2458 * Include the adjustment for a source route if any.
2459 */
2460 sum = (sum >> 16) + (sum & 0xFFFF);
2461 tcpha->tha_sum = htons(sum);
2462 tcp_send_data(tcp, mp);
2463 }
2464
2465 /*
2466 * Generate a reset based on an inbound packet, connp is set by caller
2467 * when RST is in response to an unexpected inbound packet for which
2468 * there is active tcp state in the system.
2469 *
2470 * IPSEC NOTE : Try to send the reply with the same protection as it came
2471 * in. We have the ip_recv_attr_t which is reversed to form the ip_xmit_attr_t.
2472 * That way the packet will go out at the same level of protection as it
2473 * came in with.
2474 */
2475 static void
tcp_xmit_early_reset(char * str,mblk_t * mp,uint32_t seq,uint32_t ack,int ctl,ip_recv_attr_t * ira,ip_stack_t * ipst,conn_t * connp)2476 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, uint32_t ack, int ctl,
2477 ip_recv_attr_t *ira, ip_stack_t *ipst, conn_t *connp)
2478 {
2479 ipha_t *ipha = NULL;
2480 ip6_t *ip6h = NULL;
2481 ushort_t len;
2482 tcpha_t *tcpha;
2483 int i;
2484 ipaddr_t v4addr;
2485 in6_addr_t v6addr;
2486 netstack_t *ns = ipst->ips_netstack;
2487 tcp_stack_t *tcps = ns->netstack_tcp;
2488 ip_xmit_attr_t ixas, *ixa;
2489 uint_t ip_hdr_len = ira->ira_ip_hdr_length;
2490 boolean_t need_refrele = B_FALSE; /* ixa_refrele(ixa) */
2491 ushort_t port;
2492
2493 if (!tcp_send_rst_chk(tcps)) {
2494 TCP_STAT(tcps, tcp_rst_unsent);
2495 freemsg(mp);
2496 return;
2497 }
2498
2499 /*
2500 * If connp != NULL we use conn_ixa to keep IP_NEXTHOP and other
2501 * options from the listener. In that case the caller must ensure that
2502 * we are running on the listener = connp squeue.
2503 *
2504 * We get a safe copy of conn_ixa so we don't need to restore anything
2505 * we or ip_output_simple might change in the ixa.
2506 */
2507 if (connp != NULL) {
2508 ASSERT(connp->conn_on_sqp);
2509
2510 ixa = conn_get_ixa_exclusive(connp);
2511 if (ixa == NULL) {
2512 TCP_STAT(tcps, tcp_rst_unsent);
2513 freemsg(mp);
2514 return;
2515 }
2516 need_refrele = B_TRUE;
2517 } else {
2518 bzero(&ixas, sizeof (ixas));
2519 ixa = &ixas;
2520 /*
2521 * IXAF_VERIFY_SOURCE is overkill since we know the
2522 * packet was for us.
2523 */
2524 ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE;
2525 ixa->ixa_protocol = IPPROTO_TCP;
2526 ixa->ixa_zoneid = ira->ira_zoneid;
2527 ixa->ixa_ifindex = 0;
2528 ixa->ixa_ipst = ipst;
2529 ixa->ixa_cred = kcred;
2530 ixa->ixa_cpid = NOPID;
2531 }
2532
2533 if (str && tcps->tcps_dbg) {
2534 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
2535 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, "
2536 "flags 0x%x",
2537 str, seq, ack, ctl);
2538 }
2539 if (mp->b_datap->db_ref != 1) {
2540 mblk_t *mp1 = copyb(mp);
2541 freemsg(mp);
2542 mp = mp1;
2543 if (mp == NULL)
2544 goto done;
2545 } else if (mp->b_cont) {
2546 freemsg(mp->b_cont);
2547 mp->b_cont = NULL;
2548 DB_CKSUMFLAGS(mp) = 0;
2549 }
2550 /*
2551 * We skip reversing source route here.
2552 * (for now we replace all IP options with EOL)
2553 */
2554 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
2555 ipha = (ipha_t *)mp->b_rptr;
2556 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++)
2557 mp->b_rptr[i] = IPOPT_EOL;
2558 /*
2559 * Make sure that src address isn't flagrantly invalid.
2560 * Not all broadcast address checking for the src address
2561 * is possible, since we don't know the netmask of the src
2562 * addr. No check for destination address is done, since
2563 * IP will not pass up a packet with a broadcast dest
2564 * address to TCP. Similar checks are done below for IPv6.
2565 */
2566 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST ||
2567 CLASSD(ipha->ipha_src)) {
2568 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
2569 ip_drop_input("ipIfStatsInDiscards", mp, NULL);
2570 freemsg(mp);
2571 goto done;
2572 }
2573 } else {
2574 ip6h = (ip6_t *)mp->b_rptr;
2575
2576 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) ||
2577 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) {
2578 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards);
2579 ip_drop_input("ipIfStatsInDiscards", mp, NULL);
2580 freemsg(mp);
2581 goto done;
2582 }
2583
2584 /* Remove any extension headers assuming partial overlay */
2585 if (ip_hdr_len > IPV6_HDR_LEN) {
2586 uint8_t *to;
2587
2588 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN;
2589 ovbcopy(ip6h, to, IPV6_HDR_LEN);
2590 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN;
2591 ip_hdr_len = IPV6_HDR_LEN;
2592 ip6h = (ip6_t *)mp->b_rptr;
2593 ip6h->ip6_nxt = IPPROTO_TCP;
2594 }
2595 }
2596 tcpha = (tcpha_t *)&mp->b_rptr[ip_hdr_len];
2597 if (tcpha->tha_flags & TH_RST) {
2598 freemsg(mp);
2599 goto done;
2600 }
2601 tcpha->tha_offset_and_reserved = (5 << 4);
2602 len = ip_hdr_len + sizeof (tcpha_t);
2603 mp->b_wptr = &mp->b_rptr[len];
2604 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
2605 ipha->ipha_length = htons(len);
2606 /* Swap addresses */
2607 v4addr = ipha->ipha_src;
2608 ipha->ipha_src = ipha->ipha_dst;
2609 ipha->ipha_dst = v4addr;
2610 ipha->ipha_ident = 0;
2611 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl;
2612 ixa->ixa_flags |= IXAF_IS_IPV4;
2613 ixa->ixa_ip_hdr_length = ip_hdr_len;
2614 } else {
2615 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN);
2616 /* Swap addresses */
2617 v6addr = ip6h->ip6_src;
2618 ip6h->ip6_src = ip6h->ip6_dst;
2619 ip6h->ip6_dst = v6addr;
2620 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit;
2621 ixa->ixa_flags &= ~IXAF_IS_IPV4;
2622
2623 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_dst)) {
2624 ixa->ixa_flags |= IXAF_SCOPEID_SET;
2625 ixa->ixa_scopeid = ira->ira_ruifindex;
2626 }
2627 ixa->ixa_ip_hdr_length = IPV6_HDR_LEN;
2628 }
2629 ixa->ixa_pktlen = len;
2630
2631 /* Swap the ports */
2632 port = tcpha->tha_fport;
2633 tcpha->tha_fport = tcpha->tha_lport;
2634 tcpha->tha_lport = port;
2635
2636 tcpha->tha_ack = htonl(ack);
2637 tcpha->tha_seq = htonl(seq);
2638 tcpha->tha_win = 0;
2639 tcpha->tha_sum = htons(sizeof (tcpha_t));
2640 tcpha->tha_flags = (uint8_t)ctl;
2641 if (ctl & TH_RST) {
2642 if (ctl & TH_ACK) {
2643 /*
2644 * Probe connection rejection here.
2645 * tcp_xmit_listeners_reset() drops non-SYN segments
2646 * that do not specify TH_ACK in their flags without
2647 * calling this function. As a consequence, if this
2648 * function is called with a TH_RST|TH_ACK ctl argument,
2649 * it is being called in response to a SYN segment
2650 * and thus the tcp:::accept-refused probe point
2651 * is valid here.
2652 */
2653 DTRACE_TCP5(accept__refused, mblk_t *, NULL,
2654 void, NULL, void_ip_t *, mp->b_rptr, tcp_t *, NULL,
2655 tcph_t *, tcpha);
2656 }
2657 TCPS_BUMP_MIB(tcps, tcpOutRsts);
2658 TCPS_BUMP_MIB(tcps, tcpOutControl);
2659 }
2660
2661 /* Discard any old label */
2662 if (ixa->ixa_free_flags & IXA_FREE_TSL) {
2663 ASSERT(ixa->ixa_tsl != NULL);
2664 label_rele(ixa->ixa_tsl);
2665 ixa->ixa_free_flags &= ~IXA_FREE_TSL;
2666 }
2667 ixa->ixa_tsl = ira->ira_tsl; /* Behave as a multi-level responder */
2668
2669 if (ira->ira_flags & IRAF_IPSEC_SECURE) {
2670 /*
2671 * Apply IPsec based on how IPsec was applied to
2672 * the packet that caused the RST.
2673 */
2674 if (!ipsec_in_to_out(ira, ixa, mp, ipha, ip6h)) {
2675 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2676 /* Note: mp already consumed and ip_drop_packet done */
2677 goto done;
2678 }
2679 } else {
2680 /*
2681 * This is in clear. The RST message we are building
2682 * here should go out in clear, independent of our policy.
2683 */
2684 ixa->ixa_flags |= IXAF_NO_IPSEC;
2685 }
2686
2687 DTRACE_TCP5(send, mblk_t *, NULL, ip_xmit_attr_t *, ixa,
2688 __dtrace_tcp_void_ip_t *, mp->b_rptr, tcp_t *, NULL,
2689 __dtrace_tcp_tcph_t *, tcpha);
2690
2691 /*
2692 * NOTE: one might consider tracing a TCP packet here, but
2693 * this function has no active TCP state and no tcp structure
2694 * that has a trace buffer. If we traced here, we would have
2695 * to keep a local trace buffer in tcp_record_trace().
2696 */
2697
2698 (void) ip_output_simple(mp, ixa);
2699 done:
2700 ixa_cleanup(ixa);
2701 if (need_refrele) {
2702 ASSERT(ixa != &ixas);
2703 ixa_refrele(ixa);
2704 }
2705 }
2706
2707 /*
2708 * Generate a "no listener here" RST in response to an "unknown" segment.
2709 * connp is set by caller when RST is in response to an unexpected
2710 * inbound packet for which there is active tcp state in the system.
2711 * Note that we are reusing the incoming mp to construct the outgoing RST.
2712 */
2713 void
tcp_xmit_listeners_reset(mblk_t * mp,ip_recv_attr_t * ira,ip_stack_t * ipst,conn_t * connp)2714 tcp_xmit_listeners_reset(mblk_t *mp, ip_recv_attr_t *ira, ip_stack_t *ipst,
2715 conn_t *connp)
2716 {
2717 uchar_t *rptr;
2718 uint32_t seg_len;
2719 tcpha_t *tcpha;
2720 uint32_t seg_seq;
2721 uint32_t seg_ack;
2722 uint_t flags;
2723 ipha_t *ipha;
2724 ip6_t *ip6h;
2725 boolean_t policy_present;
2726 netstack_t *ns = ipst->ips_netstack;
2727 tcp_stack_t *tcps = ns->netstack_tcp;
2728 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec;
2729 uint_t ip_hdr_len = ira->ira_ip_hdr_length;
2730
2731 TCP_STAT(tcps, tcp_no_listener);
2732
2733 /*
2734 * DTrace this "unknown" segment as a tcp:::receive, as we did
2735 * just receive something that was TCP.
2736 */
2737 DTRACE_TCP5(receive, mblk_t *, NULL, ip_xmit_attr_t *, NULL,
2738 __dtrace_tcp_void_ip_t *, mp->b_rptr, tcp_t *, NULL,
2739 __dtrace_tcp_tcph_t *, &mp->b_rptr[ip_hdr_len]);
2740
2741 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
2742 policy_present = ipss->ipsec_inbound_v4_policy_present;
2743 ipha = (ipha_t *)mp->b_rptr;
2744 ip6h = NULL;
2745 } else {
2746 policy_present = ipss->ipsec_inbound_v6_policy_present;
2747 ipha = NULL;
2748 ip6h = (ip6_t *)mp->b_rptr;
2749 }
2750
2751 if (policy_present) {
2752 /*
2753 * The conn_t parameter is NULL because we already know
2754 * nobody's home.
2755 */
2756 mp = ipsec_check_global_policy(mp, (conn_t *)NULL, ipha, ip6h,
2757 ira, ns);
2758 if (mp == NULL)
2759 return;
2760 }
2761 if (is_system_labeled() && !tsol_can_reply_error(mp, ira)) {
2762 DTRACE_PROBE2(
2763 tx__ip__log__error__nolistener__tcp,
2764 char *, "Could not reply with RST to mp(1)",
2765 mblk_t *, mp);
2766 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n"));
2767 freemsg(mp);
2768 return;
2769 }
2770
2771 rptr = mp->b_rptr;
2772
2773 tcpha = (tcpha_t *)&rptr[ip_hdr_len];
2774 seg_seq = ntohl(tcpha->tha_seq);
2775 seg_ack = ntohl(tcpha->tha_ack);
2776 flags = tcpha->tha_flags;
2777
2778 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcpha) + ip_hdr_len);
2779 if (flags & TH_RST) {
2780 freemsg(mp);
2781 } else if (flags & TH_ACK) {
2782 tcp_xmit_early_reset("no tcp, reset", mp, seg_ack, 0, TH_RST,
2783 ira, ipst, connp);
2784 } else {
2785 if (flags & TH_SYN) {
2786 seg_len++;
2787 } else {
2788 /*
2789 * Here we violate the RFC. Note that a normal
2790 * TCP will never send a segment without the ACK
2791 * flag, except for RST or SYN segment. This
2792 * segment is neither. Just drop it on the
2793 * floor.
2794 */
2795 freemsg(mp);
2796 TCP_STAT(tcps, tcp_rst_unsent);
2797 return;
2798 }
2799
2800 tcp_xmit_early_reset("no tcp, reset/ack", mp, 0,
2801 seg_seq + seg_len, TH_RST | TH_ACK, ira, ipst, connp);
2802 }
2803 }
2804
2805 /*
2806 * Helper function for tcp_xmit_mp() in handling connection set up flag
2807 * options setting.
2808 */
2809 static void
tcp_xmit_mp_aux_iss(tcp_t * tcp,conn_t * connp,tcpha_t * tcpha,mblk_t * mp,uint_t * flags)2810 tcp_xmit_mp_aux_iss(tcp_t *tcp, conn_t *connp, tcpha_t *tcpha, mblk_t *mp,
2811 uint_t *flags)
2812 {
2813 uint32_t u1;
2814 uint8_t *wptr = mp->b_wptr;
2815 tcp_stack_t *tcps = tcp->tcp_tcps;
2816 boolean_t add_sack = B_FALSE;
2817
2818 /*
2819 * If TCP_ISS_VALID and the seq number is tcp_iss,
2820 * TCP can only be in SYN-SENT, SYN-RCVD or
2821 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if
2822 * our SYN is not ack'ed but the app closes this
2823 * TCP connection.
2824 */
2825 ASSERT(tcp->tcp_state == TCPS_SYN_SENT ||
2826 tcp->tcp_state == TCPS_SYN_RCVD ||
2827 tcp->tcp_state == TCPS_FIN_WAIT_1);
2828
2829 /*
2830 * Tack on the MSS option. It is always needed
2831 * for both active and passive open.
2832 *
2833 * MSS option value should be interface MTU - MIN
2834 * TCP/IP header according to RFC 793 as it means
2835 * the maximum segment size TCP can receive. But
2836 * to get around some broken middle boxes/end hosts
2837 * out there, we allow the option value to be the
2838 * same as the MSS option size on the peer side.
2839 * In this way, the other side will not send
2840 * anything larger than they can receive.
2841 *
2842 * Note that for SYN_SENT state, the ndd param
2843 * tcp_use_smss_as_mss_opt has no effect as we
2844 * don't know the peer's MSS option value. So
2845 * the only case we need to take care of is in
2846 * SYN_RCVD state, which is done later.
2847 */
2848 wptr[0] = TCPOPT_MAXSEG;
2849 wptr[1] = TCPOPT_MAXSEG_LEN;
2850 wptr += 2;
2851 u1 = tcp->tcp_initial_pmtu - (connp->conn_ipversion == IPV4_VERSION ?
2852 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - TCP_MIN_HEADER_LENGTH;
2853 U16_TO_BE16(u1, wptr);
2854 wptr += 2;
2855
2856 /* Update the offset to cover the additional word */
2857 tcpha->tha_offset_and_reserved += (1 << 4);
2858
2859 switch (tcp->tcp_state) {
2860 case TCPS_SYN_SENT:
2861 *flags = TH_SYN;
2862
2863 if (tcp->tcp_snd_sack_ok)
2864 add_sack = B_TRUE;
2865
2866 if (tcp->tcp_snd_ts_ok) {
2867 uint32_t llbolt = (uint32_t)LBOLT_FASTPATH;
2868
2869 if (add_sack) {
2870 wptr[0] = TCPOPT_SACK_PERMITTED;
2871 wptr[1] = TCPOPT_SACK_OK_LEN;
2872 add_sack = B_FALSE;
2873 } else {
2874 wptr[0] = TCPOPT_NOP;
2875 wptr[1] = TCPOPT_NOP;
2876 }
2877 wptr[2] = TCPOPT_TSTAMP;
2878 wptr[3] = TCPOPT_TSTAMP_LEN;
2879 wptr += 4;
2880 U32_TO_BE32(llbolt, wptr);
2881 wptr += 4;
2882 ASSERT(tcp->tcp_ts_recent == 0);
2883 U32_TO_BE32(0L, wptr);
2884 wptr += 4;
2885 tcpha->tha_offset_and_reserved += (3 << 4);
2886 }
2887
2888 /*
2889 * Set up all the bits to tell other side
2890 * we are ECN capable.
2891 */
2892 if (tcp->tcp_ecn_ok)
2893 *flags |= (TH_ECE | TH_CWR);
2894
2895 break;
2896
2897 case TCPS_SYN_RCVD:
2898 *flags |= TH_SYN;
2899
2900 /*
2901 * Reset the MSS option value to be SMSS
2902 * We should probably add back the bytes
2903 * for timestamp option and IPsec. We
2904 * don't do that as this is a workaround
2905 * for broken middle boxes/end hosts, it
2906 * is better for us to be more cautious.
2907 * They may not take these things into
2908 * account in their SMSS calculation. Thus
2909 * the peer's calculated SMSS may be smaller
2910 * than what it can be. This should be OK.
2911 */
2912 if (tcps->tcps_use_smss_as_mss_opt) {
2913 u1 = tcp->tcp_mss;
2914 /*
2915 * Note that wptr points just past the MSS
2916 * option value.
2917 */
2918 U16_TO_BE16(u1, wptr - 2);
2919 }
2920
2921 /*
2922 * tcp_snd_ts_ok can only be set in TCPS_SYN_RCVD
2923 * when the peer also uses timestamps option. And
2924 * the TCP header template must have already been
2925 * updated to include the timestamps option.
2926 */
2927 if (tcp->tcp_snd_sack_ok) {
2928 if (tcp->tcp_snd_ts_ok) {
2929 uint8_t *tmp_wptr;
2930
2931 /*
2932 * Use the NOP in the header just
2933 * before timestamps opton.
2934 */
2935 tmp_wptr = (uint8_t *)tcpha +
2936 TCP_MIN_HEADER_LENGTH;
2937 ASSERT(tmp_wptr[0] == TCPOPT_NOP &&
2938 tmp_wptr[1] == TCPOPT_NOP);
2939 tmp_wptr[0] = TCPOPT_SACK_PERMITTED;
2940 tmp_wptr[1] = TCPOPT_SACK_OK_LEN;
2941 } else {
2942 add_sack = B_TRUE;
2943 }
2944 }
2945
2946
2947 /*
2948 * If the other side is ECN capable, reply
2949 * that we are also ECN capable.
2950 */
2951 if (tcp->tcp_ecn_ok)
2952 *flags |= TH_ECE;
2953 break;
2954
2955 default:
2956 /*
2957 * The above ASSERT() makes sure that this
2958 * must be FIN-WAIT-1 state. Our SYN has
2959 * not been ack'ed so retransmit it.
2960 */
2961 *flags |= TH_SYN;
2962 break;
2963 }
2964
2965 if (add_sack) {
2966 wptr[0] = TCPOPT_NOP;
2967 wptr[1] = TCPOPT_NOP;
2968 wptr[2] = TCPOPT_SACK_PERMITTED;
2969 wptr[3] = TCPOPT_SACK_OK_LEN;
2970 wptr += TCPOPT_REAL_SACK_OK_LEN;
2971 tcpha->tha_offset_and_reserved += (1 << 4);
2972 }
2973
2974 if (tcp->tcp_snd_ws_ok) {
2975 wptr[0] = TCPOPT_NOP;
2976 wptr[1] = TCPOPT_WSCALE;
2977 wptr[2] = TCPOPT_WS_LEN;
2978 wptr[3] = (uchar_t)tcp->tcp_rcv_ws;
2979 wptr += TCPOPT_REAL_WS_LEN;
2980 tcpha->tha_offset_and_reserved += (1 << 4);
2981 }
2982
2983 mp->b_wptr = wptr;
2984 u1 = (int)(mp->b_wptr - mp->b_rptr);
2985 /*
2986 * Get IP set to checksum on our behalf
2987 * Include the adjustment for a source route if any.
2988 */
2989 u1 += connp->conn_sum;
2990 u1 = (u1 >> 16) + (u1 & 0xFFFF);
2991 tcpha->tha_sum = htons(u1);
2992 TCPS_BUMP_MIB(tcps, tcpOutControl);
2993 }
2994
2995 /*
2996 * Helper function for tcp_xmit_mp() in handling connection tear down
2997 * flag setting and state changes.
2998 */
2999 static void
tcp_xmit_mp_aux_fss(tcp_t * tcp,ip_xmit_attr_t * ixa,uint_t * flags)3000 tcp_xmit_mp_aux_fss(tcp_t *tcp, ip_xmit_attr_t *ixa, uint_t *flags)
3001 {
3002 if (!tcp->tcp_fin_acked) {
3003 *flags |= TH_FIN;
3004 TCPS_BUMP_MIB(tcp->tcp_tcps, tcpOutControl);
3005 }
3006 if (!tcp->tcp_fin_sent) {
3007 tcp->tcp_fin_sent = B_TRUE;
3008 switch (tcp->tcp_state) {
3009 case TCPS_SYN_RCVD:
3010 tcp->tcp_state = TCPS_FIN_WAIT_1;
3011 DTRACE_TCP6(state__change, void, NULL,
3012 ip_xmit_attr_t *, ixa, void, NULL,
3013 tcp_t *, tcp, void, NULL,
3014 int32_t, TCPS_SYN_RCVD);
3015 break;
3016 case TCPS_ESTABLISHED:
3017 tcp->tcp_state = TCPS_FIN_WAIT_1;
3018 DTRACE_TCP6(state__change, void, NULL,
3019 ip_xmit_attr_t *, ixa, void, NULL,
3020 tcp_t *, tcp, void, NULL,
3021 int32_t, TCPS_ESTABLISHED);
3022 break;
3023 case TCPS_CLOSE_WAIT:
3024 tcp->tcp_state = TCPS_LAST_ACK;
3025 DTRACE_TCP6(state__change, void, NULL,
3026 ip_xmit_attr_t *, ixa, void, NULL,
3027 tcp_t *, tcp, void, NULL,
3028 int32_t, TCPS_CLOSE_WAIT);
3029 break;
3030 }
3031 if (tcp->tcp_suna == tcp->tcp_snxt)
3032 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
3033 tcp->tcp_snxt = tcp->tcp_fss + 1;
3034 }
3035 }
3036
3037 /*
3038 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with
3039 * ip and tcp header ready to pass down to IP. If the mp passed in is
3040 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that
3041 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary
3042 * otherwise it will dup partial mblks.)
3043 * Otherwise, an appropriate ACK packet will be generated. This
3044 * routine is not usually called to send new data for the first time. It
3045 * is mostly called out of the timer for retransmits, and to generate ACKs.
3046 *
3047 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will
3048 * be adjusted by *offset. And after dupb(), the offset and the ending mblk
3049 * of the original mblk chain will be returned in *offset and *end_mp.
3050 */
3051 mblk_t *
tcp_xmit_mp(tcp_t * tcp,mblk_t * mp,int32_t max_to_send,int32_t * offset,mblk_t ** end_mp,uint32_t seq,boolean_t sendall,uint32_t * seg_len,boolean_t rexmit)3052 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset,
3053 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len,
3054 boolean_t rexmit)
3055 {
3056 int data_length;
3057 int32_t off = 0;
3058 uint_t flags;
3059 mblk_t *mp1;
3060 mblk_t *mp2;
3061 uchar_t *rptr;
3062 tcpha_t *tcpha;
3063 int32_t num_sack_blk = 0;
3064 int32_t sack_opt_len = 0;
3065 tcp_stack_t *tcps = tcp->tcp_tcps;
3066 conn_t *connp = tcp->tcp_connp;
3067 ip_xmit_attr_t *ixa = connp->conn_ixa;
3068
3069 /* Allocate for our maximum TCP header + link-level */
3070 mp1 = allocb(connp->conn_ht_iphc_allocated + tcps->tcps_wroff_xtra,
3071 BPRI_MED);
3072 if (mp1 == NULL)
3073 return (NULL);
3074 data_length = 0;
3075
3076 /*
3077 * Note that tcp_mss has been adjusted to take into account the
3078 * timestamp option if applicable. Because SACK options do not
3079 * appear in every TCP segments and they are of variable lengths,
3080 * they cannot be included in tcp_mss. Thus we need to calculate
3081 * the actual segment length when we need to send a segment which
3082 * includes SACK options.
3083 */
3084 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
3085 num_sack_blk = MIN(tcp->tcp_max_sack_blk,
3086 tcp->tcp_num_sack_blk);
3087 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) +
3088 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN;
3089 if (max_to_send + sack_opt_len > tcp->tcp_mss)
3090 max_to_send -= sack_opt_len;
3091 }
3092
3093 if (offset != NULL) {
3094 off = *offset;
3095 /* We use offset as an indicator that end_mp is not NULL. */
3096 *end_mp = NULL;
3097 }
3098 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) {
3099 /* This could be faster with cooperation from downstream */
3100 if (mp2 != mp1 && !sendall &&
3101 data_length + (int)(mp->b_wptr - mp->b_rptr) >
3102 max_to_send)
3103 /*
3104 * Don't send the next mblk since the whole mblk
3105 * does not fit.
3106 */
3107 break;
3108 mp2->b_cont = dupb(mp);
3109 mp2 = mp2->b_cont;
3110 if (!mp2) {
3111 freemsg(mp1);
3112 return (NULL);
3113 }
3114 mp2->b_rptr += off;
3115 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <=
3116 (uintptr_t)INT_MAX);
3117
3118 data_length += (int)(mp2->b_wptr - mp2->b_rptr);
3119 if (data_length > max_to_send) {
3120 mp2->b_wptr -= data_length - max_to_send;
3121 data_length = max_to_send;
3122 off = mp2->b_wptr - mp->b_rptr;
3123 break;
3124 } else {
3125 off = 0;
3126 }
3127 }
3128 if (offset != NULL) {
3129 *offset = off;
3130 *end_mp = mp;
3131 }
3132 if (seg_len != NULL) {
3133 *seg_len = data_length;
3134 }
3135
3136 /* Update the latest receive window size in TCP header. */
3137 tcp->tcp_tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
3138
3139 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra;
3140 mp1->b_rptr = rptr;
3141 mp1->b_wptr = rptr + connp->conn_ht_iphc_len + sack_opt_len;
3142 bcopy(connp->conn_ht_iphc, rptr, connp->conn_ht_iphc_len);
3143 tcpha = (tcpha_t *)&rptr[ixa->ixa_ip_hdr_length];
3144 tcpha->tha_seq = htonl(seq);
3145
3146 /*
3147 * Use tcp_unsent to determine if the PUSH bit should be used assumes
3148 * that this function was called from tcp_wput_data. Thus, when called
3149 * to retransmit data the setting of the PUSH bit may appear some
3150 * what random in that it might get set when it should not. This
3151 * should not pose any performance issues.
3152 */
3153 if (data_length != 0 && (tcp->tcp_unsent == 0 ||
3154 tcp->tcp_unsent == data_length)) {
3155 flags = TH_ACK | TH_PUSH;
3156 } else {
3157 flags = TH_ACK;
3158 }
3159
3160 if (tcp->tcp_ecn_ok) {
3161 if (tcp->tcp_ecn_echo_on)
3162 flags |= TH_ECE;
3163
3164 /*
3165 * Only set ECT bit and ECN_CWR if a segment contains new data.
3166 * There is no TCP flow control for non-data segments, and
3167 * only data segment is transmitted reliably.
3168 */
3169 if (data_length > 0 && !rexmit) {
3170 TCP_SET_ECT(tcp, rptr);
3171 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) {
3172 flags |= TH_CWR;
3173 tcp->tcp_ecn_cwr_sent = B_TRUE;
3174 }
3175 }
3176 }
3177
3178 /* Check if there is any special processing needs to be done. */
3179 if (tcp->tcp_valid_bits) {
3180 uint32_t u1;
3181
3182 /* We don't allow having SYN and FIN in the same segment... */
3183 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) &&
3184 seq == tcp->tcp_iss) {
3185 /* Need to do connection set up processing. */
3186 tcp_xmit_mp_aux_iss(tcp, connp, tcpha, mp1, &flags);
3187 } else if ((tcp->tcp_valid_bits & TCP_FSS_VALID) &&
3188 (seq + data_length) == tcp->tcp_fss) {
3189 /* Need to do connection tear down processing. */
3190 tcp_xmit_mp_aux_fss(tcp, ixa, &flags);
3191 }
3192
3193 /*
3194 * Need to do urgent pointer processing.
3195 *
3196 * Note the trick here. u1 is unsigned. When tcp_urg
3197 * is smaller than seq, u1 will become a very huge value.
3198 * So the comparison will fail. Also note that tcp_urp
3199 * should be positive, see RFC 793 page 17.
3200 */
3201 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION;
3202 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 &&
3203 u1 < (uint32_t)(64 * 1024)) {
3204 flags |= TH_URG;
3205 TCPS_BUMP_MIB(tcps, tcpOutUrg);
3206 tcpha->tha_urp = htons(u1);
3207 }
3208 }
3209 tcpha->tha_flags = (uchar_t)flags;
3210 tcp->tcp_rack = tcp->tcp_rnxt;
3211 tcp->tcp_rack_cnt = 0;
3212
3213 /* Fill in the current value of timestamps option. */
3214 if (tcp->tcp_snd_ts_ok) {
3215 if (tcp->tcp_state != TCPS_SYN_SENT) {
3216 uint32_t llbolt = (uint32_t)LBOLT_FASTPATH;
3217
3218 U32_TO_BE32(llbolt,
3219 (char *)tcpha + TCP_MIN_HEADER_LENGTH+4);
3220 U32_TO_BE32(tcp->tcp_ts_recent,
3221 (char *)tcpha + TCP_MIN_HEADER_LENGTH+8);
3222 }
3223 }
3224
3225 /* Fill in the SACK blocks. */
3226 if (num_sack_blk > 0) {
3227 uchar_t *wptr = (uchar_t *)tcpha + connp->conn_ht_ulp_len;
3228 sack_blk_t *tmp;
3229 int32_t i;
3230
3231 wptr[0] = TCPOPT_NOP;
3232 wptr[1] = TCPOPT_NOP;
3233 wptr[2] = TCPOPT_SACK;
3234 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
3235 sizeof (sack_blk_t);
3236 wptr += TCPOPT_REAL_SACK_LEN;
3237
3238 tmp = tcp->tcp_sack_list;
3239 for (i = 0; i < num_sack_blk; i++) {
3240 U32_TO_BE32(tmp[i].begin, wptr);
3241 wptr += sizeof (tcp_seq);
3242 U32_TO_BE32(tmp[i].end, wptr);
3243 wptr += sizeof (tcp_seq);
3244 }
3245 tcpha->tha_offset_and_reserved += ((num_sack_blk * 2 + 1) << 4);
3246 }
3247 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX);
3248 data_length += (int)(mp1->b_wptr - rptr);
3249
3250 ixa->ixa_pktlen = data_length;
3251
3252 if (ixa->ixa_flags & IXAF_IS_IPV4) {
3253 ((ipha_t *)rptr)->ipha_length = htons(data_length);
3254 } else {
3255 ip6_t *ip6 = (ip6_t *)rptr;
3256
3257 ip6->ip6_plen = htons(data_length - IPV6_HDR_LEN);
3258 }
3259
3260 /*
3261 * Prime pump for IP
3262 * Include the adjustment for a source route if any.
3263 */
3264 data_length -= ixa->ixa_ip_hdr_length;
3265 data_length += connp->conn_sum;
3266 data_length = (data_length >> 16) + (data_length & 0xFFFF);
3267 tcpha->tha_sum = htons(data_length);
3268 if (tcp->tcp_ip_forward_progress) {
3269 tcp->tcp_ip_forward_progress = B_FALSE;
3270 connp->conn_ixa->ixa_flags |= IXAF_REACH_CONF;
3271 } else {
3272 connp->conn_ixa->ixa_flags &= ~IXAF_REACH_CONF;
3273 }
3274 return (mp1);
3275 }
3276
3277 /*
3278 * If this routine returns B_TRUE, TCP can generate a RST in response
3279 * to a segment. If it returns B_FALSE, TCP should not respond.
3280 */
3281 static boolean_t
tcp_send_rst_chk(tcp_stack_t * tcps)3282 tcp_send_rst_chk(tcp_stack_t *tcps)
3283 {
3284 int64_t now;
3285
3286 /*
3287 * TCP needs to protect itself from generating too many RSTs.
3288 * This can be a DoS attack by sending us random segments
3289 * soliciting RSTs.
3290 *
3291 * What we do here is to have a limit of tcp_rst_sent_rate RSTs
3292 * in each 1 second interval. In this way, TCP still generate
3293 * RSTs in normal cases but when under attack, the impact is
3294 * limited.
3295 */
3296 if (tcps->tcps_rst_sent_rate_enabled != 0) {
3297 now = ddi_get_lbolt64();
3298 if (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) >
3299 1*SECONDS) {
3300 tcps->tcps_last_rst_intrvl = now;
3301 tcps->tcps_rst_cnt = 1;
3302 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) {
3303 return (B_FALSE);
3304 }
3305 }
3306 return (B_TRUE);
3307 }
3308
3309 /*
3310 * This function handles all retransmissions if SACK is enabled for this
3311 * connection. First it calculates how many segments can be retransmitted
3312 * based on tcp_pipe. Then it goes thru the notsack list to find eligible
3313 * segments. A segment is eligible if sack_cnt for that segment is greater
3314 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted
3315 * all eligible segments, it checks to see if TCP can send some new segments
3316 * (fast recovery). If it can, set the appropriate flag for tcp_input_data().
3317 *
3318 * Parameters:
3319 * tcp_t *tcp: the tcp structure of the connection.
3320 * uint_t *flags: in return, appropriate value will be set for
3321 * tcp_input_data().
3322 */
3323 void
tcp_sack_rexmit(tcp_t * tcp,uint_t * flags)3324 tcp_sack_rexmit(tcp_t *tcp, uint_t *flags)
3325 {
3326 notsack_blk_t *notsack_blk;
3327 int32_t usable_swnd;
3328 int32_t mss;
3329 uint32_t seg_len;
3330 mblk_t *xmit_mp;
3331 tcp_stack_t *tcps = tcp->tcp_tcps;
3332
3333 ASSERT(tcp->tcp_notsack_list != NULL);
3334 ASSERT(tcp->tcp_rexmit == B_FALSE);
3335
3336 /* Defensive coding in case there is a bug... */
3337 if (tcp->tcp_notsack_list == NULL) {
3338 return;
3339 }
3340 notsack_blk = tcp->tcp_notsack_list;
3341 mss = tcp->tcp_mss;
3342
3343 /*
3344 * Limit the num of outstanding data in the network to be
3345 * tcp_cwnd_ssthresh, which is half of the original congestion wnd.
3346 */
3347 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe;
3348
3349 /* At least retransmit 1 MSS of data. */
3350 if (usable_swnd <= 0) {
3351 usable_swnd = mss;
3352 }
3353
3354 /* Make sure no new RTT samples will be taken. */
3355 tcp->tcp_csuna = tcp->tcp_snxt;
3356
3357 notsack_blk = tcp->tcp_notsack_list;
3358 while (usable_swnd > 0) {
3359 mblk_t *snxt_mp, *tmp_mp;
3360 tcp_seq begin = tcp->tcp_sack_snxt;
3361 tcp_seq end;
3362 int32_t off;
3363
3364 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) {
3365 if (SEQ_GT(notsack_blk->end, begin) &&
3366 (notsack_blk->sack_cnt >=
3367 tcps->tcps_dupack_fast_retransmit)) {
3368 end = notsack_blk->end;
3369 if (SEQ_LT(begin, notsack_blk->begin)) {
3370 begin = notsack_blk->begin;
3371 }
3372 break;
3373 }
3374 }
3375 /*
3376 * All holes are filled. Manipulate tcp_cwnd to send more
3377 * if we can. Note that after the SACK recovery, tcp_cwnd is
3378 * set to tcp_cwnd_ssthresh.
3379 */
3380 if (notsack_blk == NULL) {
3381 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe;
3382 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) {
3383 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna;
3384 ASSERT(tcp->tcp_cwnd > 0);
3385 return;
3386 } else {
3387 usable_swnd = usable_swnd / mss;
3388 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna +
3389 MAX(usable_swnd * mss, mss);
3390 *flags |= TH_XMIT_NEEDED;
3391 return;
3392 }
3393 }
3394
3395 /*
3396 * Note that we may send more than usable_swnd allows here
3397 * because of round off, but no more than 1 MSS of data.
3398 */
3399 seg_len = end - begin;
3400 if (seg_len > mss)
3401 seg_len = mss;
3402 snxt_mp = tcp_get_seg_mp(tcp, begin, &off);
3403 ASSERT(snxt_mp != NULL);
3404 /* This should not happen. Defensive coding again... */
3405 if (snxt_mp == NULL) {
3406 return;
3407 }
3408
3409 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off,
3410 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE);
3411 if (xmit_mp == NULL)
3412 return;
3413
3414 usable_swnd -= seg_len;
3415 tcp->tcp_pipe += seg_len;
3416 tcp->tcp_sack_snxt = begin + seg_len;
3417
3418 tcp_send_data(tcp, xmit_mp);
3419
3420 /*
3421 * Update the send timestamp to avoid false retransmission.
3422 */
3423 snxt_mp->b_prev = (mblk_t *)(intptr_t)gethrtime();
3424
3425 TCPS_BUMP_MIB(tcps, tcpRetransSegs);
3426 TCPS_UPDATE_MIB(tcps, tcpRetransBytes, seg_len);
3427 TCPS_BUMP_MIB(tcps, tcpOutSackRetransSegs);
3428 tcp->tcp_cs.tcp_out_retrans_segs++;
3429 tcp->tcp_cs.tcp_out_retrans_bytes += seg_len;
3430 /*
3431 * Update tcp_rexmit_max to extend this SACK recovery phase.
3432 * This happens when new data sent during fast recovery is
3433 * also lost. If TCP retransmits those new data, it needs
3434 * to extend SACK recover phase to avoid starting another
3435 * fast retransmit/recovery unnecessarily.
3436 */
3437 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) {
3438 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt;
3439 }
3440 }
3441 }
3442
3443 /*
3444 * tcp_ss_rexmit() is called to do slow start retransmission after a timeout
3445 * or ICMP errors.
3446 */
3447 void
tcp_ss_rexmit(tcp_t * tcp)3448 tcp_ss_rexmit(tcp_t *tcp)
3449 {
3450 uint32_t snxt;
3451 uint32_t smax;
3452 int32_t win;
3453 int32_t mss;
3454 int32_t off;
3455 mblk_t *snxt_mp;
3456 tcp_stack_t *tcps = tcp->tcp_tcps;
3457
3458 /*
3459 * Note that tcp_rexmit can be set even though TCP has retransmitted
3460 * all unack'ed segments.
3461 */
3462 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) {
3463 smax = tcp->tcp_rexmit_max;
3464 snxt = tcp->tcp_rexmit_nxt;
3465 if (SEQ_LT(snxt, tcp->tcp_suna)) {
3466 snxt = tcp->tcp_suna;
3467 }
3468 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd);
3469 win -= snxt - tcp->tcp_suna;
3470 mss = tcp->tcp_mss;
3471 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off);
3472
3473 while (SEQ_LT(snxt, smax) && (win > 0) && (snxt_mp != NULL)) {
3474 mblk_t *xmit_mp;
3475 mblk_t *old_snxt_mp = snxt_mp;
3476 uint32_t cnt = mss;
3477
3478 if (win < cnt) {
3479 cnt = win;
3480 }
3481 if (SEQ_GT(snxt + cnt, smax)) {
3482 cnt = smax - snxt;
3483 }
3484 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off,
3485 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE);
3486 if (xmit_mp == NULL)
3487 return;
3488
3489 tcp_send_data(tcp, xmit_mp);
3490
3491 snxt += cnt;
3492 win -= cnt;
3493 /*
3494 * Update the send timestamp to avoid false
3495 * retransmission.
3496 */
3497 old_snxt_mp->b_prev = (mblk_t *)(intptr_t)gethrtime();
3498 TCPS_BUMP_MIB(tcps, tcpRetransSegs);
3499 TCPS_UPDATE_MIB(tcps, tcpRetransBytes, cnt);
3500 tcp->tcp_cs.tcp_out_retrans_segs++;
3501 tcp->tcp_cs.tcp_out_retrans_bytes += cnt;
3502
3503 tcp->tcp_rexmit_nxt = snxt;
3504 }
3505 /*
3506 * If we have transmitted all we have at the time
3507 * we started the retranmission, we can leave
3508 * the rest of the job to tcp_wput_data(). But we
3509 * need to check the send window first. If the
3510 * win is not 0, go on with tcp_wput_data().
3511 */
3512 if (SEQ_LT(snxt, smax) || win == 0) {
3513 return;
3514 }
3515 }
3516 /* Only call tcp_wput_data() if there is data to be sent. */
3517 if (tcp->tcp_unsent) {
3518 tcp_wput_data(tcp, NULL, B_FALSE);
3519 }
3520 }
3521
3522 /*
3523 * Do slow start retransmission after ICMP errors of PMTU changes.
3524 */
3525 void
tcp_rexmit_after_error(tcp_t * tcp)3526 tcp_rexmit_after_error(tcp_t *tcp)
3527 {
3528 /*
3529 * All sent data has been acknowledged or no data left to send, just
3530 * to return.
3531 */
3532 if (!SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) ||
3533 (tcp->tcp_xmit_head == NULL))
3534 return;
3535
3536 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && (tcp->tcp_unsent == 0))
3537 tcp->tcp_rexmit_max = tcp->tcp_fss;
3538 else
3539 tcp->tcp_rexmit_max = tcp->tcp_snxt;
3540
3541 tcp->tcp_rexmit_nxt = tcp->tcp_suna;
3542 tcp->tcp_rexmit = B_TRUE;
3543 tcp->tcp_dupack_cnt = 0;
3544 tcp_ss_rexmit(tcp);
3545 }
3546
3547 /*
3548 * tcp_get_seg_mp() is called to get the pointer to a segment in the
3549 * send queue which starts at the given sequence number. If the given
3550 * sequence number is equal to last valid sequence number (tcp_snxt), the
3551 * returned mblk is the last valid mblk, and off is set to the length of
3552 * that mblk.
3553 *
3554 * send queue which starts at the given seq. no.
3555 *
3556 * Parameters:
3557 * tcp_t *tcp: the tcp instance pointer.
3558 * uint32_t seq: the starting seq. no of the requested segment.
3559 * int32_t *off: after the execution, *off will be the offset to
3560 * the returned mblk which points to the requested seq no.
3561 * It is the caller's responsibility to send in a non-null off.
3562 *
3563 * Return:
3564 * A mblk_t pointer pointing to the requested segment in send queue.
3565 */
3566 static mblk_t *
tcp_get_seg_mp(tcp_t * tcp,uint32_t seq,int32_t * off)3567 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off)
3568 {
3569 int32_t cnt;
3570 mblk_t *mp;
3571
3572 /* Defensive coding. Make sure we don't send incorrect data. */
3573 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GT(seq, tcp->tcp_snxt))
3574 return (NULL);
3575
3576 cnt = seq - tcp->tcp_suna;
3577 mp = tcp->tcp_xmit_head;
3578 while (cnt > 0 && mp != NULL) {
3579 cnt -= mp->b_wptr - mp->b_rptr;
3580 if (cnt <= 0) {
3581 cnt += mp->b_wptr - mp->b_rptr;
3582 break;
3583 }
3584 mp = mp->b_cont;
3585 }
3586 ASSERT(mp != NULL);
3587 *off = cnt;
3588 return (mp);
3589 }
3590
3591 /*
3592 * This routine adjusts next-to-send sequence number variables, in the
3593 * case where the reciever has shrunk it's window.
3594 */
3595 void
tcp_update_xmit_tail(tcp_t * tcp,uint32_t snxt)3596 tcp_update_xmit_tail(tcp_t *tcp, uint32_t snxt)
3597 {
3598 mblk_t *xmit_tail;
3599 int32_t offset;
3600
3601 tcp->tcp_snxt = snxt;
3602
3603 /* Get the mblk, and the offset in it, as per the shrunk window */
3604 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset);
3605 ASSERT(xmit_tail != NULL);
3606 tcp->tcp_xmit_tail = xmit_tail;
3607 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr -
3608 xmit_tail->b_rptr - offset;
3609 }
3610
3611 /*
3612 * This handles the case when the receiver has shrunk its win. Per RFC 1122
3613 * if the receiver shrinks the window, i.e. moves the right window to the
3614 * left, the we should not send new data, but should retransmit normally the
3615 * old unacked data between suna and suna + swnd. We might has sent data
3616 * that is now outside the new window, pretend that we didn't send it.
3617 */
3618 static void
tcp_process_shrunk_swnd(tcp_t * tcp,uint32_t shrunk_count)3619 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count)
3620 {
3621 uint32_t snxt = tcp->tcp_snxt;
3622
3623 ASSERT(shrunk_count > 0);
3624
3625 if (!tcp->tcp_is_wnd_shrnk) {
3626 tcp->tcp_snxt_shrunk = snxt;
3627 tcp->tcp_is_wnd_shrnk = B_TRUE;
3628 } else if (SEQ_GT(snxt, tcp->tcp_snxt_shrunk)) {
3629 tcp->tcp_snxt_shrunk = snxt;
3630 }
3631
3632 /* Pretend we didn't send the data outside the window */
3633 snxt -= shrunk_count;
3634
3635 /* Reset all the values per the now shrunk window */
3636 tcp_update_xmit_tail(tcp, snxt);
3637 tcp->tcp_unsent += shrunk_count;
3638
3639 /*
3640 * If the SACK option is set, delete the entire list of
3641 * notsack'ed blocks.
3642 */
3643 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp);
3644
3645 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0)
3646 /*
3647 * Make sure the timer is running so that we will probe a zero
3648 * window.
3649 */
3650 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
3651 }
3652
3653 /*
3654 * tcp_fill_header is called by tcp_send() to fill the outgoing TCP header
3655 * with the template header, as well as other options such as time-stamp,
3656 * ECN and/or SACK.
3657 */
3658 static void
tcp_fill_header(tcp_t * tcp,uchar_t * rptr,int num_sack_blk)3659 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, int num_sack_blk)
3660 {
3661 tcpha_t *tcp_tmpl, *tcpha;
3662 uint32_t *dst, *src;
3663 int hdrlen;
3664 conn_t *connp = tcp->tcp_connp;
3665
3666 ASSERT(OK_32PTR(rptr));
3667
3668 /* Template header */
3669 tcp_tmpl = tcp->tcp_tcpha;
3670
3671 /* Header of outgoing packet */
3672 tcpha = (tcpha_t *)(rptr + connp->conn_ixa->ixa_ip_hdr_length);
3673
3674 /* dst and src are opaque 32-bit fields, used for copying */
3675 dst = (uint32_t *)rptr;
3676 src = (uint32_t *)connp->conn_ht_iphc;
3677 hdrlen = connp->conn_ht_iphc_len;
3678
3679 /* Fill time-stamp option if needed */
3680 if (tcp->tcp_snd_ts_ok) {
3681 U32_TO_BE32(LBOLT_FASTPATH,
3682 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4);
3683 U32_TO_BE32(tcp->tcp_ts_recent,
3684 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8);
3685 } else {
3686 ASSERT(connp->conn_ht_ulp_len == TCP_MIN_HEADER_LENGTH);
3687 }
3688
3689 /*
3690 * Copy the template header; is this really more efficient than
3691 * calling bcopy()? For simple IPv4/TCP, it may be the case,
3692 * but perhaps not for other scenarios.
3693 */
3694 dst[0] = src[0];
3695 dst[1] = src[1];
3696 dst[2] = src[2];
3697 dst[3] = src[3];
3698 dst[4] = src[4];
3699 dst[5] = src[5];
3700 dst[6] = src[6];
3701 dst[7] = src[7];
3702 dst[8] = src[8];
3703 dst[9] = src[9];
3704 if (hdrlen -= 40) {
3705 hdrlen >>= 2;
3706 dst += 10;
3707 src += 10;
3708 do {
3709 *dst++ = *src++;
3710 } while (--hdrlen);
3711 }
3712
3713 /*
3714 * Set the ECN info in the TCP header if it is not a zero
3715 * window probe. Zero window probe is only sent in
3716 * tcp_wput_data() and tcp_timer().
3717 */
3718 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) {
3719 TCP_SET_ECT(tcp, rptr);
3720
3721 if (tcp->tcp_ecn_echo_on)
3722 tcpha->tha_flags |= TH_ECE;
3723 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) {
3724 tcpha->tha_flags |= TH_CWR;
3725 tcp->tcp_ecn_cwr_sent = B_TRUE;
3726 }
3727 }
3728
3729 /* Fill in SACK options */
3730 if (num_sack_blk > 0) {
3731 uchar_t *wptr = rptr + connp->conn_ht_iphc_len;
3732 sack_blk_t *tmp;
3733 int32_t i;
3734
3735 wptr[0] = TCPOPT_NOP;
3736 wptr[1] = TCPOPT_NOP;
3737 wptr[2] = TCPOPT_SACK;
3738 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
3739 sizeof (sack_blk_t);
3740 wptr += TCPOPT_REAL_SACK_LEN;
3741
3742 tmp = tcp->tcp_sack_list;
3743 for (i = 0; i < num_sack_blk; i++) {
3744 U32_TO_BE32(tmp[i].begin, wptr);
3745 wptr += sizeof (tcp_seq);
3746 U32_TO_BE32(tmp[i].end, wptr);
3747 wptr += sizeof (tcp_seq);
3748 }
3749 tcpha->tha_offset_and_reserved +=
3750 ((num_sack_blk * 2 + 1) << 4);
3751 }
3752 }
3753