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