Print this page
11546 Track TCP round-trip time in nanoseconds
Portions contributed by: Cody Peter Mello <cody.mello@joyent.com>
Portions contributed by: Brandon Baker <bbaker@delphix.com>
Reviewed by: Jason King <jason.king@joyent.com>
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Dan McDonald <danmcd@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/inet/tcp/tcp_input.c
+++ new/usr/src/uts/common/inet/tcp/tcp_input.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 - * Copyright 2016 Joyent, Inc.
26 - * Copyright (c) 2014 by Delphix. All rights reserved.
25 + * Copyright 2019 Joyent, Inc.
26 + * Copyright (c) 2014, 2016 by Delphix. All rights reserved.
27 27 */
28 28
29 29 /* This file contains all TCP input processing functions. */
30 30
31 31 #include <sys/types.h>
32 32 #include <sys/stream.h>
33 33 #include <sys/strsun.h>
34 34 #include <sys/strsubr.h>
35 35 #include <sys/stropts.h>
36 36 #include <sys/strlog.h>
37 37 #define _SUN_TPI_VERSION 2
38 38 #include <sys/tihdr.h>
39 39 #include <sys/suntpi.h>
40 40 #include <sys/xti_inet.h>
41 41 #include <sys/squeue_impl.h>
42 42 #include <sys/squeue.h>
43 43 #include <sys/tsol/tnet.h>
44 44
45 45 #include <inet/common.h>
46 46 #include <inet/ip.h>
47 47 #include <inet/tcp.h>
48 48 #include <inet/tcp_impl.h>
49 49 #include <inet/tcp_cluster.h>
50 50 #include <inet/proto_set.h>
51 51 #include <inet/ipsec_impl.h>
52 52
53 53 /*
54 54 * RFC7323-recommended phrasing of TSTAMP option, for easier parsing
55 55 */
56 56
57 57 #ifdef _BIG_ENDIAN
58 58 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
59 59 (TCPOPT_TSTAMP << 8) | 10)
60 60 #else
61 61 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \
62 62 (TCPOPT_NOP << 8) | TCPOPT_NOP)
63 63 #endif
64 64
65 65 /*
66 66 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days
67 67 */
68 68 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz))
69 69
70 70 /*
71 71 * Since tcp_listener is not cleared atomically with tcp_detached
72 72 * being cleared we need this extra bit to tell a detached connection
73 73 * apart from one that is in the process of being accepted.
74 74 */
75 75 #define TCP_IS_DETACHED_NONEAGER(tcp) \
76 76 (TCP_IS_DETACHED(tcp) && \
77 77 (!(tcp)->tcp_hard_binding))
78 78
79 79 /*
80 80 * Steps to do when a tcp_t moves to TIME-WAIT state.
81 81 *
82 82 * This connection is done, we don't need to account for it. Decrement
83 83 * the listener connection counter if needed.
84 84 *
85 85 * Decrement the connection counter of the stack. Note that this counter
86 86 * is per CPU. So the total number of connections in a stack is the sum of all
87 87 * of them. Since there is no lock for handling all of them exclusively, the
88 88 * resulting sum is only an approximation.
89 89 *
90 90 * Unconditionally clear the exclusive binding bit so this TIME-WAIT
91 91 * connection won't interfere with new ones.
92 92 *
93 93 * Start the TIME-WAIT timer. If upper layer has not closed the connection,
94 94 * the timer is handled within the context of this tcp_t. When the timer
95 95 * fires, tcp_clean_death() is called. If upper layer closes the connection
96 96 * during this period, tcp_time_wait_append() will be called to add this
97 97 * tcp_t to the global TIME-WAIT list. Note that this means that the
98 98 * actual wait time in TIME-WAIT state will be longer than the
99 99 * tcps_time_wait_interval since the period before upper layer closes the
100 100 * connection is not accounted for when tcp_time_wait_append() is called.
101 101 *
102 102 * If upper layer has closed the connection, call tcp_time_wait_append()
103 103 * directly.
104 104 *
105 105 */
106 106 #define SET_TIME_WAIT(tcps, tcp, connp) \
107 107 { \
108 108 (tcp)->tcp_state = TCPS_TIME_WAIT; \
109 109 if ((tcp)->tcp_listen_cnt != NULL) \
110 110 TCP_DECR_LISTEN_CNT(tcp); \
111 111 atomic_dec_64( \
112 112 (uint64_t *)&(tcps)->tcps_sc[CPU->cpu_seqid]->tcp_sc_conn_cnt); \
113 113 (connp)->conn_exclbind = 0; \
114 114 if (!TCP_IS_DETACHED(tcp)) { \
115 115 TCP_TIMER_RESTART(tcp, (tcps)->tcps_time_wait_interval); \
116 116 } else { \
117 117 tcp_time_wait_append(tcp); \
118 118 TCP_DBGSTAT(tcps, tcp_rput_time_wait); \
119 119 } \
120 120 }
121 121
122 122 /*
123 123 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more
124 124 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent
125 125 * data, TCP will not respond with an ACK. RFC 793 requires that
126 126 * TCP responds with an ACK for such a bogus ACK. By not following
127 127 * the RFC, we prevent TCP from getting into an ACK storm if somehow
128 128 * an attacker successfully spoofs an acceptable segment to our
129 129 * peer; or when our peer is "confused."
130 130 */
131 131 static uint32_t tcp_drop_ack_unsent_cnt = 10;
132 132
133 133 /*
134 134 * To protect TCP against attacker using a small window and requesting
135 135 * large amount of data (DoS attack by conuming memory), TCP checks the
136 136 * window advertised in the last ACK of the 3-way handshake. TCP uses
137 137 * the tcp_mss (the size of one packet) value for comparion. The window
138 138 * should be larger than tcp_mss. But while a sane TCP should advertise
139 139 * a receive window larger than or equal to 4*MSS to avoid stop and go
140 140 * tarrfic, not all TCP stacks do that. This is especially true when
141 141 * tcp_mss is a big value.
142 142 *
143 143 * To work around this issue, an additional fixed value for comparison
144 144 * is also used. If the advertised window is smaller than both tcp_mss
145 145 * and tcp_init_wnd_chk, the ACK is considered as invalid. So for large
146 146 * tcp_mss value (say, 8K), a window larger than tcp_init_wnd_chk but
147 147 * smaller than 8K is considered to be OK.
148 148 */
149 149 static uint32_t tcp_init_wnd_chk = 4096;
150 150
151 151 /* Process ICMP source quench message or not. */
152 152 static boolean_t tcp_icmp_source_quench = B_FALSE;
153 153
154 154 static boolean_t tcp_outbound_squeue_switch = B_FALSE;
155 155
156 156 static mblk_t *tcp_conn_create_v4(conn_t *, conn_t *, mblk_t *,
157 157 ip_recv_attr_t *);
158 158 static mblk_t *tcp_conn_create_v6(conn_t *, conn_t *, mblk_t *,
↓ open down ↓ |
122 lines elided |
↑ open up ↑ |
159 159 ip_recv_attr_t *);
160 160 static boolean_t tcp_drop_q0(tcp_t *);
161 161 static void tcp_icmp_error_ipv6(tcp_t *, mblk_t *, ip_recv_attr_t *);
162 162 static mblk_t *tcp_input_add_ancillary(tcp_t *, mblk_t *, ip_pkt_t *,
163 163 ip_recv_attr_t *);
164 164 static void tcp_input_listener(void *, mblk_t *, void *, ip_recv_attr_t *);
165 165 static void tcp_process_options(tcp_t *, tcpha_t *);
166 166 static mblk_t *tcp_reass(tcp_t *, mblk_t *, uint32_t);
167 167 static void tcp_reass_elim_overlap(tcp_t *, mblk_t *);
168 168 static void tcp_rsrv_input(void *, mblk_t *, void *, ip_recv_attr_t *);
169 -static void tcp_set_rto(tcp_t *, time_t);
169 +static void tcp_set_rto(tcp_t *, hrtime_t);
170 170 static void tcp_setcred_data(mblk_t *, ip_recv_attr_t *);
171 171
172 172 /*
173 173 * Set the MSS associated with a particular tcp based on its current value,
174 174 * and a new one passed in. Observe minimums and maximums, and reset other
175 175 * state variables that we want to view as multiples of MSS.
176 176 *
177 177 * The value of MSS could be either increased or descreased.
178 178 */
179 179 void
180 180 tcp_mss_set(tcp_t *tcp, uint32_t mss)
181 181 {
182 182 uint32_t mss_max;
183 183 tcp_stack_t *tcps = tcp->tcp_tcps;
184 184 conn_t *connp = tcp->tcp_connp;
185 185
186 186 if (connp->conn_ipversion == IPV4_VERSION)
187 187 mss_max = tcps->tcps_mss_max_ipv4;
188 188 else
189 189 mss_max = tcps->tcps_mss_max_ipv6;
190 190
191 191 if (mss < tcps->tcps_mss_min)
192 192 mss = tcps->tcps_mss_min;
193 193 if (mss > mss_max)
194 194 mss = mss_max;
195 195 /*
196 196 * Unless naglim has been set by our client to
197 197 * a non-mss value, force naglim to track mss.
198 198 * This can help to aggregate small writes.
199 199 */
200 200 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim)
201 201 tcp->tcp_naglim = mss;
202 202 /*
203 203 * TCP should be able to buffer at least 4 MSS data for obvious
204 204 * performance reason.
205 205 */
206 206 if ((mss << 2) > connp->conn_sndbuf)
207 207 connp->conn_sndbuf = mss << 2;
208 208
209 209 /*
210 210 * Set the send lowater to at least twice of MSS.
211 211 */
212 212 if ((mss << 1) > connp->conn_sndlowat)
213 213 connp->conn_sndlowat = mss << 1;
214 214
215 215 /*
216 216 * Update tcp_cwnd according to the new value of MSS. Keep the
217 217 * previous ratio to preserve the transmit rate.
218 218 */
219 219 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss;
220 220 tcp->tcp_cwnd_cnt = 0;
221 221
222 222 tcp->tcp_mss = mss;
223 223 (void) tcp_maxpsz_set(tcp, B_TRUE);
224 224 }
225 225
226 226 /*
227 227 * Extract option values from a tcp header. We put any found values into the
228 228 * tcpopt struct and return a bitmask saying which options were found.
229 229 */
230 230 int
231 231 tcp_parse_options(tcpha_t *tcpha, tcp_opt_t *tcpopt)
232 232 {
233 233 uchar_t *endp;
234 234 int len;
235 235 uint32_t mss;
236 236 uchar_t *up = (uchar_t *)tcpha;
237 237 int found = 0;
238 238 int32_t sack_len;
239 239 tcp_seq sack_begin, sack_end;
240 240 tcp_t *tcp;
241 241
242 242 endp = up + TCP_HDR_LENGTH(tcpha);
243 243 up += TCP_MIN_HEADER_LENGTH;
244 244 /*
245 245 * If timestamp option is aligned as recommended in RFC 7323 Appendix
246 246 * A, and is the only option, return quickly.
247 247 */
248 248 if (TCP_HDR_LENGTH(tcpha) == (uint32_t)TCP_MIN_HEADER_LENGTH +
249 249 TCPOPT_REAL_TS_LEN &&
250 250 OK_32PTR(up) &&
251 251 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) {
252 252 tcpopt->tcp_opt_ts_val = ABE32_TO_U32((up+4));
253 253 tcpopt->tcp_opt_ts_ecr = ABE32_TO_U32((up+8));
254 254
255 255 return (TCP_OPT_TSTAMP_PRESENT);
256 256 }
257 257 while (up < endp) {
258 258 len = endp - up;
259 259 switch (*up) {
260 260 case TCPOPT_EOL:
261 261 break;
262 262
263 263 case TCPOPT_NOP:
264 264 up++;
265 265 continue;
266 266
267 267 case TCPOPT_MAXSEG:
268 268 if (len < TCPOPT_MAXSEG_LEN ||
269 269 up[1] != TCPOPT_MAXSEG_LEN)
270 270 break;
271 271
272 272 mss = BE16_TO_U16(up+2);
273 273 /* Caller must handle tcp_mss_min and tcp_mss_max_* */
274 274 tcpopt->tcp_opt_mss = mss;
275 275 found |= TCP_OPT_MSS_PRESENT;
276 276
277 277 up += TCPOPT_MAXSEG_LEN;
278 278 continue;
279 279
280 280 case TCPOPT_WSCALE:
281 281 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN)
282 282 break;
283 283
284 284 if (up[2] > TCP_MAX_WINSHIFT)
285 285 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT;
286 286 else
287 287 tcpopt->tcp_opt_wscale = up[2];
288 288 found |= TCP_OPT_WSCALE_PRESENT;
289 289
290 290 up += TCPOPT_WS_LEN;
291 291 continue;
292 292
293 293 case TCPOPT_SACK_PERMITTED:
294 294 if (len < TCPOPT_SACK_OK_LEN ||
295 295 up[1] != TCPOPT_SACK_OK_LEN)
296 296 break;
297 297 found |= TCP_OPT_SACK_OK_PRESENT;
298 298 up += TCPOPT_SACK_OK_LEN;
299 299 continue;
300 300
301 301 case TCPOPT_SACK:
302 302 if (len <= 2 || up[1] <= 2 || len < up[1])
303 303 break;
304 304
305 305 /* If TCP is not interested in SACK blks... */
306 306 if ((tcp = tcpopt->tcp) == NULL) {
307 307 up += up[1];
308 308 continue;
309 309 }
310 310 sack_len = up[1] - TCPOPT_HEADER_LEN;
311 311 up += TCPOPT_HEADER_LEN;
312 312
313 313 /*
314 314 * If the list is empty, allocate one and assume
315 315 * nothing is sack'ed.
316 316 */
317 317 if (tcp->tcp_notsack_list == NULL) {
318 318 tcp_notsack_update(&(tcp->tcp_notsack_list),
319 319 tcp->tcp_suna, tcp->tcp_snxt,
320 320 &(tcp->tcp_num_notsack_blk),
321 321 &(tcp->tcp_cnt_notsack_list));
322 322
323 323 /*
324 324 * Make sure tcp_notsack_list is not NULL.
325 325 * This happens when kmem_alloc(KM_NOSLEEP)
326 326 * returns NULL.
327 327 */
328 328 if (tcp->tcp_notsack_list == NULL) {
329 329 up += sack_len;
330 330 continue;
331 331 }
332 332 tcp->tcp_fack = tcp->tcp_suna;
333 333 }
334 334
335 335 while (sack_len > 0) {
336 336 if (up + 8 > endp) {
337 337 up = endp;
338 338 break;
339 339 }
340 340 sack_begin = BE32_TO_U32(up);
341 341 up += 4;
342 342 sack_end = BE32_TO_U32(up);
343 343 up += 4;
344 344 sack_len -= 8;
345 345 /*
346 346 * Bounds checking. Make sure the SACK
347 347 * info is within tcp_suna and tcp_snxt.
348 348 * If this SACK blk is out of bound, ignore
349 349 * it but continue to parse the following
350 350 * blks.
351 351 */
352 352 if (SEQ_LEQ(sack_end, sack_begin) ||
353 353 SEQ_LT(sack_begin, tcp->tcp_suna) ||
354 354 SEQ_GT(sack_end, tcp->tcp_snxt)) {
355 355 continue;
356 356 }
357 357 tcp_notsack_insert(&(tcp->tcp_notsack_list),
358 358 sack_begin, sack_end,
359 359 &(tcp->tcp_num_notsack_blk),
360 360 &(tcp->tcp_cnt_notsack_list));
361 361 if (SEQ_GT(sack_end, tcp->tcp_fack)) {
362 362 tcp->tcp_fack = sack_end;
363 363 }
364 364 }
365 365 found |= TCP_OPT_SACK_PRESENT;
366 366 continue;
367 367
368 368 case TCPOPT_TSTAMP:
369 369 if (len < TCPOPT_TSTAMP_LEN ||
370 370 up[1] != TCPOPT_TSTAMP_LEN)
371 371 break;
372 372
373 373 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2);
374 374 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6);
375 375
376 376 found |= TCP_OPT_TSTAMP_PRESENT;
377 377
378 378 up += TCPOPT_TSTAMP_LEN;
379 379 continue;
380 380
381 381 default:
382 382 if (len <= 1 || len < (int)up[1] || up[1] == 0)
383 383 break;
384 384 up += up[1];
385 385 continue;
386 386 }
387 387 break;
388 388 }
389 389 return (found);
390 390 }
391 391
392 392 /*
393 393 * Process all TCP option in SYN segment. Note that this function should
394 394 * be called after tcp_set_destination() is called so that the necessary info
395 395 * from IRE is already set in the tcp structure.
396 396 *
397 397 * This function sets up the correct tcp_mss value according to the
398 398 * MSS option value and our header size. It also sets up the window scale
399 399 * and timestamp values, and initialize SACK info blocks. But it does not
400 400 * change receive window size after setting the tcp_mss value. The caller
401 401 * should do the appropriate change.
402 402 */
403 403 static void
404 404 tcp_process_options(tcp_t *tcp, tcpha_t *tcpha)
405 405 {
406 406 int options;
407 407 tcp_opt_t tcpopt;
408 408 uint32_t mss_max;
409 409 char *tmp_tcph;
410 410 tcp_stack_t *tcps = tcp->tcp_tcps;
411 411 conn_t *connp = tcp->tcp_connp;
412 412
413 413 tcpopt.tcp = NULL;
414 414 options = tcp_parse_options(tcpha, &tcpopt);
415 415
416 416 /*
417 417 * Process MSS option. Note that MSS option value does not account
418 418 * for IP or TCP options. This means that it is equal to MTU - minimum
419 419 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for
420 420 * IPv6.
421 421 */
422 422 if (!(options & TCP_OPT_MSS_PRESENT)) {
423 423 if (connp->conn_ipversion == IPV4_VERSION)
424 424 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4;
425 425 else
426 426 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6;
427 427 } else {
428 428 if (connp->conn_ipversion == IPV4_VERSION)
429 429 mss_max = tcps->tcps_mss_max_ipv4;
430 430 else
431 431 mss_max = tcps->tcps_mss_max_ipv6;
432 432 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min)
433 433 tcpopt.tcp_opt_mss = tcps->tcps_mss_min;
434 434 else if (tcpopt.tcp_opt_mss > mss_max)
435 435 tcpopt.tcp_opt_mss = mss_max;
436 436 }
437 437
438 438 /* Process Window Scale option. */
439 439 if (options & TCP_OPT_WSCALE_PRESENT) {
440 440 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale;
441 441 tcp->tcp_snd_ws_ok = B_TRUE;
442 442 } else {
443 443 tcp->tcp_snd_ws = B_FALSE;
444 444 tcp->tcp_snd_ws_ok = B_FALSE;
445 445 tcp->tcp_rcv_ws = B_FALSE;
446 446 }
447 447
448 448 /* Process Timestamp option. */
449 449 if ((options & TCP_OPT_TSTAMP_PRESENT) &&
450 450 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) {
451 451 tmp_tcph = (char *)tcp->tcp_tcpha;
452 452
453 453 tcp->tcp_snd_ts_ok = B_TRUE;
454 454 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val;
455 455 tcp->tcp_last_rcv_lbolt = ddi_get_lbolt64();
456 456 ASSERT(OK_32PTR(tmp_tcph));
457 457 ASSERT(connp->conn_ht_ulp_len == TCP_MIN_HEADER_LENGTH);
458 458
459 459 /* Fill in our template header with basic timestamp option. */
460 460 tmp_tcph += connp->conn_ht_ulp_len;
461 461 tmp_tcph[0] = TCPOPT_NOP;
462 462 tmp_tcph[1] = TCPOPT_NOP;
463 463 tmp_tcph[2] = TCPOPT_TSTAMP;
464 464 tmp_tcph[3] = TCPOPT_TSTAMP_LEN;
465 465 connp->conn_ht_iphc_len += TCPOPT_REAL_TS_LEN;
466 466 connp->conn_ht_ulp_len += TCPOPT_REAL_TS_LEN;
467 467 tcp->tcp_tcpha->tha_offset_and_reserved += (3 << 4);
468 468 } else {
469 469 tcp->tcp_snd_ts_ok = B_FALSE;
470 470 }
471 471
472 472 /*
473 473 * Process SACK options. If SACK is enabled for this connection,
474 474 * then allocate the SACK info structure. Note the following ways
475 475 * when tcp_snd_sack_ok is set to true.
476 476 *
477 477 * For active connection: in tcp_set_destination() called in
478 478 * tcp_connect().
479 479 *
480 480 * For passive connection: in tcp_set_destination() called in
481 481 * tcp_input_listener().
482 482 *
483 483 * That's the reason why the extra TCP_IS_DETACHED() check is there.
484 484 * That check makes sure that if we did not send a SACK OK option,
485 485 * we will not enable SACK for this connection even though the other
486 486 * side sends us SACK OK option. For active connection, the SACK
487 487 * info structure has already been allocated. So we need to free
488 488 * it if SACK is disabled.
489 489 */
490 490 if ((options & TCP_OPT_SACK_OK_PRESENT) &&
491 491 (tcp->tcp_snd_sack_ok ||
492 492 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) {
493 493 ASSERT(tcp->tcp_num_sack_blk == 0);
494 494 ASSERT(tcp->tcp_notsack_list == NULL);
495 495
496 496 tcp->tcp_snd_sack_ok = B_TRUE;
497 497 if (tcp->tcp_snd_ts_ok) {
498 498 tcp->tcp_max_sack_blk = 3;
499 499 } else {
500 500 tcp->tcp_max_sack_blk = 4;
501 501 }
502 502 } else if (tcp->tcp_snd_sack_ok) {
503 503 /*
504 504 * Resetting tcp_snd_sack_ok to B_FALSE so that
505 505 * no SACK info will be used for this
506 506 * connection. This assumes that SACK usage
507 507 * permission is negotiated. This may need
508 508 * to be changed once this is clarified.
509 509 */
510 510 ASSERT(tcp->tcp_num_sack_blk == 0);
511 511 ASSERT(tcp->tcp_notsack_list == NULL);
512 512 tcp->tcp_snd_sack_ok = B_FALSE;
513 513 }
514 514
515 515 /*
516 516 * Now we know the exact TCP/IP header length, subtract
517 517 * that from tcp_mss to get our side's MSS.
518 518 */
519 519 tcp->tcp_mss -= connp->conn_ht_iphc_len;
520 520
521 521 /*
522 522 * Here we assume that the other side's header size will be equal to
523 523 * our header size. We calculate the real MSS accordingly. Need to
524 524 * take into additional stuffs IPsec puts in.
525 525 *
526 526 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header)
527 527 */
528 528 tcpopt.tcp_opt_mss -= connp->conn_ht_iphc_len +
529 529 tcp->tcp_ipsec_overhead -
530 530 ((connp->conn_ipversion == IPV4_VERSION ?
531 531 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH);
532 532
533 533 /*
534 534 * Set MSS to the smaller one of both ends of the connection.
535 535 * We should not have called tcp_mss_set() before, but our
536 536 * side of the MSS should have been set to a proper value
537 537 * by tcp_set_destination(). tcp_mss_set() will also set up the
538 538 * STREAM head parameters properly.
539 539 *
540 540 * If we have a larger-than-16-bit window but the other side
541 541 * didn't want to do window scale, tcp_rwnd_set() will take
542 542 * care of that.
543 543 */
544 544 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss));
545 545
546 546 /*
547 547 * Initialize tcp_cwnd value. After tcp_mss_set(), tcp_mss has been
548 548 * updated properly.
549 549 */
550 550 TCP_SET_INIT_CWND(tcp, tcp->tcp_mss, tcps->tcps_slow_start_initial);
551 551 }
552 552
553 553 /*
554 554 * Add a new piece to the tcp reassembly queue. If the gap at the beginning
555 555 * is filled, return as much as we can. The message passed in may be
556 556 * multi-part, chained using b_cont. "start" is the starting sequence
557 557 * number for this piece.
558 558 */
559 559 static mblk_t *
560 560 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start)
561 561 {
562 562 uint32_t end;
563 563 mblk_t *mp1;
564 564 mblk_t *mp2;
565 565 mblk_t *next_mp;
566 566 uint32_t u1;
567 567 tcp_stack_t *tcps = tcp->tcp_tcps;
568 568
569 569
570 570 /* Walk through all the new pieces. */
571 571 do {
572 572 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
573 573 (uintptr_t)INT_MAX);
574 574 end = start + (int)(mp->b_wptr - mp->b_rptr);
575 575 next_mp = mp->b_cont;
576 576 if (start == end) {
577 577 /* Empty. Blast it. */
578 578 freeb(mp);
579 579 continue;
580 580 }
581 581 mp->b_cont = NULL;
582 582 TCP_REASS_SET_SEQ(mp, start);
583 583 TCP_REASS_SET_END(mp, end);
584 584 mp1 = tcp->tcp_reass_tail;
585 585 if (!mp1) {
586 586 tcp->tcp_reass_tail = mp;
587 587 tcp->tcp_reass_head = mp;
588 588 TCPS_BUMP_MIB(tcps, tcpInDataUnorderSegs);
589 589 TCPS_UPDATE_MIB(tcps, tcpInDataUnorderBytes,
590 590 end - start);
591 591 continue;
592 592 }
593 593 /* New stuff completely beyond tail? */
594 594 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) {
595 595 /* Link it on end. */
596 596 mp1->b_cont = mp;
597 597 tcp->tcp_reass_tail = mp;
598 598 TCPS_BUMP_MIB(tcps, tcpInDataUnorderSegs);
599 599 TCPS_UPDATE_MIB(tcps, tcpInDataUnorderBytes,
600 600 end - start);
601 601 continue;
602 602 }
603 603 mp1 = tcp->tcp_reass_head;
604 604 u1 = TCP_REASS_SEQ(mp1);
605 605 /* New stuff at the front? */
606 606 if (SEQ_LT(start, u1)) {
607 607 /* Yes... Check for overlap. */
608 608 mp->b_cont = mp1;
609 609 tcp->tcp_reass_head = mp;
610 610 tcp_reass_elim_overlap(tcp, mp);
611 611 continue;
612 612 }
613 613 /*
614 614 * The new piece fits somewhere between the head and tail.
615 615 * We find our slot, where mp1 precedes us and mp2 trails.
616 616 */
617 617 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) {
618 618 u1 = TCP_REASS_SEQ(mp2);
619 619 if (SEQ_LEQ(start, u1))
620 620 break;
621 621 }
622 622 /* Link ourselves in */
623 623 mp->b_cont = mp2;
624 624 mp1->b_cont = mp;
625 625
626 626 /* Trim overlap with following mblk(s) first */
627 627 tcp_reass_elim_overlap(tcp, mp);
628 628
629 629 /* Trim overlap with preceding mblk */
630 630 tcp_reass_elim_overlap(tcp, mp1);
631 631
632 632 } while (start = end, mp = next_mp);
633 633 mp1 = tcp->tcp_reass_head;
634 634 /* Anything ready to go? */
635 635 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt)
636 636 return (NULL);
637 637 /* Eat what we can off the queue */
638 638 for (;;) {
639 639 mp = mp1->b_cont;
640 640 end = TCP_REASS_END(mp1);
641 641 TCP_REASS_SET_SEQ(mp1, 0);
642 642 TCP_REASS_SET_END(mp1, 0);
643 643 if (!mp) {
644 644 tcp->tcp_reass_tail = NULL;
645 645 break;
646 646 }
647 647 if (end != TCP_REASS_SEQ(mp)) {
648 648 mp1->b_cont = NULL;
649 649 break;
650 650 }
651 651 mp1 = mp;
652 652 }
653 653 mp1 = tcp->tcp_reass_head;
654 654 tcp->tcp_reass_head = mp;
655 655 return (mp1);
656 656 }
657 657
658 658 /* Eliminate any overlap that mp may have over later mblks */
659 659 static void
660 660 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp)
661 661 {
662 662 uint32_t end;
663 663 mblk_t *mp1;
664 664 uint32_t u1;
665 665 tcp_stack_t *tcps = tcp->tcp_tcps;
666 666
667 667 end = TCP_REASS_END(mp);
668 668 while ((mp1 = mp->b_cont) != NULL) {
669 669 u1 = TCP_REASS_SEQ(mp1);
670 670 if (!SEQ_GT(end, u1))
671 671 break;
672 672 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) {
673 673 mp->b_wptr -= end - u1;
674 674 TCP_REASS_SET_END(mp, u1);
675 675 TCPS_BUMP_MIB(tcps, tcpInDataPartDupSegs);
676 676 TCPS_UPDATE_MIB(tcps, tcpInDataPartDupBytes,
677 677 end - u1);
678 678 break;
679 679 }
680 680 mp->b_cont = mp1->b_cont;
681 681 TCP_REASS_SET_SEQ(mp1, 0);
682 682 TCP_REASS_SET_END(mp1, 0);
683 683 freeb(mp1);
684 684 TCPS_BUMP_MIB(tcps, tcpInDataDupSegs);
685 685 TCPS_UPDATE_MIB(tcps, tcpInDataDupBytes, end - u1);
686 686 }
687 687 if (!mp1)
688 688 tcp->tcp_reass_tail = mp;
689 689 }
690 690
691 691 /*
692 692 * This function does PAWS protection check, per RFC 7323 section 5. Requires
693 693 * that timestamp options are already processed into tcpoptp. Returns B_TRUE if
694 694 * the segment passes the PAWS test, else returns B_FALSE.
695 695 */
696 696 boolean_t
697 697 tcp_paws_check(tcp_t *tcp, const tcp_opt_t *tcpoptp)
698 698 {
699 699 if (TSTMP_LT(tcpoptp->tcp_opt_ts_val,
700 700 tcp->tcp_ts_recent)) {
701 701 if (LBOLT_FASTPATH64 <
702 702 (tcp->tcp_last_rcv_lbolt + PAWS_TIMEOUT)) {
703 703 /* This segment is not acceptable. */
704 704 return (B_FALSE);
705 705 } else {
706 706 /*
707 707 * Connection has been idle for
708 708 * too long. Reset the timestamp
709 709 */
710 710 tcp->tcp_ts_recent =
711 711 tcpoptp->tcp_opt_ts_val;
712 712 }
713 713 }
714 714 return (B_TRUE);
715 715 }
716 716
717 717 /*
718 718 * Defense for the SYN attack -
719 719 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest
720 720 * one from the list of droppable eagers. This list is a subset of q0.
721 721 * see comments before the definition of MAKE_DROPPABLE().
722 722 * 2. Don't drop a SYN request before its first timeout. This gives every
723 723 * request at least til the first timeout to complete its 3-way handshake.
724 724 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many
725 725 * requests currently on the queue that has timed out. This will be used
726 726 * as an indicator of whether an attack is under way, so that appropriate
727 727 * actions can be taken. (It's incremented in tcp_timer() and decremented
728 728 * either when eager goes into ESTABLISHED, or gets freed up.)
729 729 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on
730 730 * # of timeout drops back to <= q0len/32 => SYN alert off
731 731 */
732 732 static boolean_t
733 733 tcp_drop_q0(tcp_t *tcp)
734 734 {
735 735 tcp_t *eager;
736 736 mblk_t *mp;
737 737 tcp_stack_t *tcps = tcp->tcp_tcps;
738 738
739 739 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock));
740 740 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0);
741 741
742 742 /* Pick oldest eager from the list of droppable eagers */
743 743 eager = tcp->tcp_eager_prev_drop_q0;
744 744
745 745 /* If list is empty. return B_FALSE */
746 746 if (eager == tcp) {
747 747 return (B_FALSE);
748 748 }
749 749
750 750 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */
751 751 if ((mp = allocb(0, BPRI_HI)) == NULL)
752 752 return (B_FALSE);
753 753
754 754 /*
755 755 * Take this eager out from the list of droppable eagers since we are
756 756 * going to drop it.
757 757 */
758 758 MAKE_UNDROPPABLE(eager);
759 759
760 760 if (tcp->tcp_connp->conn_debug) {
761 761 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE,
762 762 "tcp_drop_q0: listen half-open queue (max=%d) overflow"
763 763 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0,
764 764 tcp->tcp_conn_req_cnt_q0,
765 765 tcp_display(tcp, NULL, DISP_PORT_ONLY));
766 766 }
767 767
768 768 TCPS_BUMP_MIB(tcps, tcpHalfOpenDrop);
769 769
770 770 /* Put a reference on the conn as we are enqueueing it in the sqeue */
771 771 CONN_INC_REF(eager->tcp_connp);
772 772
773 773 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp,
774 774 tcp_clean_death_wrapper, eager->tcp_connp, NULL,
775 775 SQ_FILL, SQTAG_TCP_DROP_Q0);
776 776
777 777 return (B_TRUE);
778 778 }
779 779
780 780 /*
781 781 * Handle a SYN on an AF_INET6 socket; can be either IPv4 or IPv6
782 782 */
783 783 static mblk_t *
784 784 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp,
785 785 ip_recv_attr_t *ira)
786 786 {
787 787 tcp_t *ltcp = lconnp->conn_tcp;
788 788 tcp_t *tcp = connp->conn_tcp;
789 789 mblk_t *tpi_mp;
790 790 ipha_t *ipha;
791 791 ip6_t *ip6h;
792 792 sin6_t sin6;
793 793 uint_t ifindex = ira->ira_ruifindex;
794 794 tcp_stack_t *tcps = tcp->tcp_tcps;
795 795
796 796 if (ira->ira_flags & IRAF_IS_IPV4) {
797 797 ipha = (ipha_t *)mp->b_rptr;
798 798
799 799 connp->conn_ipversion = IPV4_VERSION;
800 800 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_laddr_v6);
801 801 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_faddr_v6);
802 802 connp->conn_saddr_v6 = connp->conn_laddr_v6;
803 803
804 804 sin6 = sin6_null;
805 805 sin6.sin6_addr = connp->conn_faddr_v6;
806 806 sin6.sin6_port = connp->conn_fport;
807 807 sin6.sin6_family = AF_INET6;
808 808 sin6.__sin6_src_id = ip_srcid_find_addr(&connp->conn_laddr_v6,
809 809 IPCL_ZONEID(lconnp), tcps->tcps_netstack);
810 810
811 811 if (connp->conn_recv_ancillary.crb_recvdstaddr) {
812 812 sin6_t sin6d;
813 813
814 814 sin6d = sin6_null;
815 815 sin6d.sin6_addr = connp->conn_laddr_v6;
816 816 sin6d.sin6_port = connp->conn_lport;
817 817 sin6d.sin6_family = AF_INET;
818 818 tpi_mp = mi_tpi_extconn_ind(NULL,
819 819 (char *)&sin6d, sizeof (sin6_t),
820 820 (char *)&tcp,
821 821 (t_scalar_t)sizeof (intptr_t),
822 822 (char *)&sin6d, sizeof (sin6_t),
823 823 (t_scalar_t)ltcp->tcp_conn_req_seqnum);
824 824 } else {
825 825 tpi_mp = mi_tpi_conn_ind(NULL,
826 826 (char *)&sin6, sizeof (sin6_t),
827 827 (char *)&tcp, (t_scalar_t)sizeof (intptr_t),
828 828 (t_scalar_t)ltcp->tcp_conn_req_seqnum);
829 829 }
830 830 } else {
831 831 ip6h = (ip6_t *)mp->b_rptr;
832 832
833 833 connp->conn_ipversion = IPV6_VERSION;
834 834 connp->conn_laddr_v6 = ip6h->ip6_dst;
835 835 connp->conn_faddr_v6 = ip6h->ip6_src;
836 836 connp->conn_saddr_v6 = connp->conn_laddr_v6;
837 837
838 838 sin6 = sin6_null;
839 839 sin6.sin6_addr = connp->conn_faddr_v6;
840 840 sin6.sin6_port = connp->conn_fport;
841 841 sin6.sin6_family = AF_INET6;
842 842 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK;
843 843 sin6.__sin6_src_id = ip_srcid_find_addr(&connp->conn_laddr_v6,
844 844 IPCL_ZONEID(lconnp), tcps->tcps_netstack);
845 845
846 846 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) {
847 847 /* Pass up the scope_id of remote addr */
848 848 sin6.sin6_scope_id = ifindex;
849 849 } else {
850 850 sin6.sin6_scope_id = 0;
851 851 }
852 852 if (connp->conn_recv_ancillary.crb_recvdstaddr) {
853 853 sin6_t sin6d;
854 854
855 855 sin6d = sin6_null;
856 856 sin6.sin6_addr = connp->conn_laddr_v6;
857 857 sin6d.sin6_port = connp->conn_lport;
858 858 sin6d.sin6_family = AF_INET6;
859 859 if (IN6_IS_ADDR_LINKSCOPE(&connp->conn_laddr_v6))
860 860 sin6d.sin6_scope_id = ifindex;
861 861
862 862 tpi_mp = mi_tpi_extconn_ind(NULL,
863 863 (char *)&sin6d, sizeof (sin6_t),
864 864 (char *)&tcp, (t_scalar_t)sizeof (intptr_t),
865 865 (char *)&sin6d, sizeof (sin6_t),
866 866 (t_scalar_t)ltcp->tcp_conn_req_seqnum);
867 867 } else {
868 868 tpi_mp = mi_tpi_conn_ind(NULL,
869 869 (char *)&sin6, sizeof (sin6_t),
870 870 (char *)&tcp, (t_scalar_t)sizeof (intptr_t),
871 871 (t_scalar_t)ltcp->tcp_conn_req_seqnum);
872 872 }
873 873 }
874 874
875 875 tcp->tcp_mss = tcps->tcps_mss_def_ipv6;
876 876 return (tpi_mp);
877 877 }
878 878
879 879 /* Handle a SYN on an AF_INET socket */
880 880 static mblk_t *
881 881 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, mblk_t *mp,
882 882 ip_recv_attr_t *ira)
883 883 {
884 884 tcp_t *ltcp = lconnp->conn_tcp;
885 885 tcp_t *tcp = connp->conn_tcp;
886 886 sin_t sin;
887 887 mblk_t *tpi_mp = NULL;
888 888 tcp_stack_t *tcps = tcp->tcp_tcps;
889 889 ipha_t *ipha;
890 890
891 891 ASSERT(ira->ira_flags & IRAF_IS_IPV4);
892 892 ipha = (ipha_t *)mp->b_rptr;
893 893
894 894 connp->conn_ipversion = IPV4_VERSION;
895 895 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_laddr_v6);
896 896 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_faddr_v6);
897 897 connp->conn_saddr_v6 = connp->conn_laddr_v6;
898 898
899 899 sin = sin_null;
900 900 sin.sin_addr.s_addr = connp->conn_faddr_v4;
901 901 sin.sin_port = connp->conn_fport;
902 902 sin.sin_family = AF_INET;
903 903 if (lconnp->conn_recv_ancillary.crb_recvdstaddr) {
904 904 sin_t sind;
905 905
906 906 sind = sin_null;
907 907 sind.sin_addr.s_addr = connp->conn_laddr_v4;
908 908 sind.sin_port = connp->conn_lport;
909 909 sind.sin_family = AF_INET;
910 910 tpi_mp = mi_tpi_extconn_ind(NULL,
911 911 (char *)&sind, sizeof (sin_t), (char *)&tcp,
912 912 (t_scalar_t)sizeof (intptr_t), (char *)&sind,
913 913 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum);
914 914 } else {
915 915 tpi_mp = mi_tpi_conn_ind(NULL,
916 916 (char *)&sin, sizeof (sin_t),
917 917 (char *)&tcp, (t_scalar_t)sizeof (intptr_t),
918 918 (t_scalar_t)ltcp->tcp_conn_req_seqnum);
919 919 }
920 920
921 921 tcp->tcp_mss = tcps->tcps_mss_def_ipv4;
922 922 return (tpi_mp);
923 923 }
924 924
925 925 /*
926 926 * Called via squeue to get on to eager's perimeter. It sends a
927 927 * TH_RST if eager is in the fanout table. The listener wants the
928 928 * eager to disappear either by means of tcp_eager_blowoff() or
929 929 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be
930 930 * called (via squeue) if the eager cannot be inserted in the
931 931 * fanout table in tcp_input_listener().
932 932 */
933 933 /* ARGSUSED */
934 934 void
935 935 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
936 936 {
937 937 conn_t *econnp = (conn_t *)arg;
938 938 tcp_t *eager = econnp->conn_tcp;
939 939 tcp_t *listener = eager->tcp_listener;
940 940
941 941 /*
942 942 * We could be called because listener is closing. Since
943 943 * the eager was using listener's queue's, we avoid
944 944 * using the listeners queues from now on.
945 945 */
946 946 ASSERT(eager->tcp_detached);
947 947 econnp->conn_rq = NULL;
948 948 econnp->conn_wq = NULL;
949 949
950 950 /*
951 951 * An eager's conn_fanout will be NULL if it's a duplicate
952 952 * for an existing 4-tuples in the conn fanout table.
953 953 * We don't want to send an RST out in such case.
954 954 */
955 955 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) {
956 956 tcp_xmit_ctl("tcp_eager_kill, can't wait",
957 957 eager, eager->tcp_snxt, 0, TH_RST);
958 958 }
959 959
960 960 /* We are here because listener wants this eager gone */
961 961 if (listener != NULL) {
962 962 mutex_enter(&listener->tcp_eager_lock);
963 963 tcp_eager_unlink(eager);
964 964 if (eager->tcp_tconnind_started) {
965 965 /*
966 966 * The eager has sent a conn_ind up to the
967 967 * listener but listener decides to close
968 968 * instead. We need to drop the extra ref
969 969 * placed on eager in tcp_input_data() before
970 970 * sending the conn_ind to listener.
971 971 */
972 972 CONN_DEC_REF(econnp);
973 973 }
974 974 mutex_exit(&listener->tcp_eager_lock);
975 975 CONN_DEC_REF(listener->tcp_connp);
976 976 }
977 977
978 978 if (eager->tcp_state != TCPS_CLOSED)
979 979 tcp_close_detached(eager);
980 980 }
981 981
982 982 /*
983 983 * Reset any eager connection hanging off this listener marked
984 984 * with 'seqnum' and then reclaim it's resources.
985 985 */
986 986 boolean_t
987 987 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum)
988 988 {
989 989 tcp_t *eager;
990 990 mblk_t *mp;
991 991
992 992 eager = listener;
993 993 mutex_enter(&listener->tcp_eager_lock);
994 994 do {
995 995 eager = eager->tcp_eager_next_q;
996 996 if (eager == NULL) {
997 997 mutex_exit(&listener->tcp_eager_lock);
998 998 return (B_FALSE);
999 999 }
1000 1000 } while (eager->tcp_conn_req_seqnum != seqnum);
1001 1001
1002 1002 if (eager->tcp_closemp_used) {
1003 1003 mutex_exit(&listener->tcp_eager_lock);
1004 1004 return (B_TRUE);
1005 1005 }
1006 1006 eager->tcp_closemp_used = B_TRUE;
1007 1007 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15);
1008 1008 CONN_INC_REF(eager->tcp_connp);
1009 1009 mutex_exit(&listener->tcp_eager_lock);
1010 1010 mp = &eager->tcp_closemp;
1011 1011 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill,
1012 1012 eager->tcp_connp, NULL, SQ_FILL, SQTAG_TCP_EAGER_BLOWOFF);
1013 1013 return (B_TRUE);
1014 1014 }
1015 1015
1016 1016 /*
1017 1017 * Reset any eager connection hanging off this listener
1018 1018 * and then reclaim it's resources.
1019 1019 */
1020 1020 void
1021 1021 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only)
1022 1022 {
1023 1023 tcp_t *eager;
1024 1024 mblk_t *mp;
1025 1025 tcp_stack_t *tcps = listener->tcp_tcps;
1026 1026
1027 1027 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock));
1028 1028
1029 1029 if (!q0_only) {
1030 1030 /* First cleanup q */
1031 1031 TCP_STAT(tcps, tcp_eager_blowoff_q);
1032 1032 eager = listener->tcp_eager_next_q;
1033 1033 while (eager != NULL) {
1034 1034 if (!eager->tcp_closemp_used) {
1035 1035 eager->tcp_closemp_used = B_TRUE;
1036 1036 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15);
1037 1037 CONN_INC_REF(eager->tcp_connp);
1038 1038 mp = &eager->tcp_closemp;
1039 1039 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp,
1040 1040 tcp_eager_kill, eager->tcp_connp, NULL,
1041 1041 SQ_FILL, SQTAG_TCP_EAGER_CLEANUP);
1042 1042 }
1043 1043 eager = eager->tcp_eager_next_q;
1044 1044 }
1045 1045 }
1046 1046 /* Then cleanup q0 */
1047 1047 TCP_STAT(tcps, tcp_eager_blowoff_q0);
1048 1048 eager = listener->tcp_eager_next_q0;
1049 1049 while (eager != listener) {
1050 1050 if (!eager->tcp_closemp_used) {
1051 1051 eager->tcp_closemp_used = B_TRUE;
1052 1052 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15);
1053 1053 CONN_INC_REF(eager->tcp_connp);
1054 1054 mp = &eager->tcp_closemp;
1055 1055 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp,
1056 1056 tcp_eager_kill, eager->tcp_connp, NULL, SQ_FILL,
1057 1057 SQTAG_TCP_EAGER_CLEANUP_Q0);
1058 1058 }
1059 1059 eager = eager->tcp_eager_next_q0;
1060 1060 }
1061 1061 }
1062 1062
1063 1063 /*
1064 1064 * If we are an eager connection hanging off a listener that hasn't
1065 1065 * formally accepted the connection yet, get off its list and blow off
1066 1066 * any data that we have accumulated.
1067 1067 */
1068 1068 void
1069 1069 tcp_eager_unlink(tcp_t *tcp)
1070 1070 {
1071 1071 tcp_t *listener = tcp->tcp_listener;
1072 1072
1073 1073 ASSERT(listener != NULL);
1074 1074 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock));
1075 1075 if (tcp->tcp_eager_next_q0 != NULL) {
1076 1076 ASSERT(tcp->tcp_eager_prev_q0 != NULL);
1077 1077
1078 1078 /* Remove the eager tcp from q0 */
1079 1079 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 =
1080 1080 tcp->tcp_eager_prev_q0;
1081 1081 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 =
1082 1082 tcp->tcp_eager_next_q0;
1083 1083 ASSERT(listener->tcp_conn_req_cnt_q0 > 0);
1084 1084 listener->tcp_conn_req_cnt_q0--;
1085 1085
1086 1086 tcp->tcp_eager_next_q0 = NULL;
1087 1087 tcp->tcp_eager_prev_q0 = NULL;
1088 1088
1089 1089 /*
1090 1090 * Take the eager out, if it is in the list of droppable
1091 1091 * eagers.
1092 1092 */
1093 1093 MAKE_UNDROPPABLE(tcp);
1094 1094
1095 1095 if (tcp->tcp_syn_rcvd_timeout != 0) {
1096 1096 /* we have timed out before */
1097 1097 ASSERT(listener->tcp_syn_rcvd_timeout > 0);
1098 1098 listener->tcp_syn_rcvd_timeout--;
1099 1099 }
1100 1100 } else {
1101 1101 tcp_t **tcpp = &listener->tcp_eager_next_q;
1102 1102 tcp_t *prev = NULL;
1103 1103
1104 1104 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) {
1105 1105 if (tcpp[0] == tcp) {
1106 1106 if (listener->tcp_eager_last_q == tcp) {
1107 1107 /*
1108 1108 * If we are unlinking the last
1109 1109 * element on the list, adjust
1110 1110 * tail pointer. Set tail pointer
1111 1111 * to nil when list is empty.
1112 1112 */
1113 1113 ASSERT(tcp->tcp_eager_next_q == NULL);
1114 1114 if (listener->tcp_eager_last_q ==
1115 1115 listener->tcp_eager_next_q) {
1116 1116 listener->tcp_eager_last_q =
1117 1117 NULL;
1118 1118 } else {
1119 1119 /*
1120 1120 * We won't get here if there
1121 1121 * is only one eager in the
1122 1122 * list.
1123 1123 */
1124 1124 ASSERT(prev != NULL);
1125 1125 listener->tcp_eager_last_q =
1126 1126 prev;
1127 1127 }
1128 1128 }
1129 1129 tcpp[0] = tcp->tcp_eager_next_q;
1130 1130 tcp->tcp_eager_next_q = NULL;
1131 1131 tcp->tcp_eager_last_q = NULL;
1132 1132 ASSERT(listener->tcp_conn_req_cnt_q > 0);
1133 1133 listener->tcp_conn_req_cnt_q--;
1134 1134 break;
1135 1135 }
1136 1136 prev = tcpp[0];
1137 1137 }
1138 1138 }
1139 1139 tcp->tcp_listener = NULL;
1140 1140 }
1141 1141
1142 1142 /* BEGIN CSTYLED */
1143 1143 /*
1144 1144 *
1145 1145 * The sockfs ACCEPT path:
1146 1146 * =======================
1147 1147 *
1148 1148 * The eager is now established in its own perimeter as soon as SYN is
1149 1149 * received in tcp_input_listener(). When sockfs receives conn_ind, it
1150 1150 * completes the accept processing on the acceptor STREAM. The sending
1151 1151 * of conn_ind part is common for both sockfs listener and a TLI/XTI
1152 1152 * listener but a TLI/XTI listener completes the accept processing
1153 1153 * on the listener perimeter.
1154 1154 *
1155 1155 * Common control flow for 3 way handshake:
1156 1156 * ----------------------------------------
1157 1157 *
1158 1158 * incoming SYN (listener perimeter) -> tcp_input_listener()
1159 1159 *
1160 1160 * incoming SYN-ACK-ACK (eager perim) -> tcp_input_data()
1161 1161 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind()
1162 1162 *
1163 1163 * Sockfs ACCEPT Path:
1164 1164 * -------------------
1165 1165 *
1166 1166 * open acceptor stream (tcp_open allocates tcp_tli_accept()
1167 1167 * as STREAM entry point)
1168 1168 *
1169 1169 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_tli_accept()
1170 1170 *
1171 1171 * tcp_tli_accept() extracts the eager and makes the q->q_ptr <-> eager
1172 1172 * association (we are not behind eager's squeue but sockfs is protecting us
1173 1173 * and no one knows about this stream yet. The STREAMS entry point q->q_info
1174 1174 * is changed to point at tcp_wput().
1175 1175 *
1176 1176 * tcp_accept_common() sends any deferred eagers via tcp_send_pending() to
1177 1177 * listener (done on listener's perimeter).
1178 1178 *
1179 1179 * tcp_tli_accept() calls tcp_accept_finish() on eagers perimeter to finish
1180 1180 * accept.
1181 1181 *
1182 1182 * TLI/XTI client ACCEPT path:
1183 1183 * ---------------------------
1184 1184 *
1185 1185 * soaccept() sends T_CONN_RES on the listener STREAM.
1186 1186 *
1187 1187 * tcp_tli_accept() -> tcp_accept_swap() complete the processing and send
1188 1188 * a M_SETOPS mblk to eager perimeter to finish accept (tcp_accept_finish()).
1189 1189 *
1190 1190 * Locks:
1191 1191 * ======
1192 1192 *
1193 1193 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and
1194 1194 * and listeners->tcp_eager_next_q.
1195 1195 *
1196 1196 * Referencing:
1197 1197 * ============
1198 1198 *
1199 1199 * 1) We start out in tcp_input_listener by eager placing a ref on
1200 1200 * listener and listener adding eager to listeners->tcp_eager_next_q0.
1201 1201 *
1202 1202 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before
1203 1203 * doing so we place a ref on the eager. This ref is finally dropped at the
1204 1204 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the
1205 1205 * reference is dropped by the squeue framework.
1206 1206 *
1207 1207 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish
1208 1208 *
1209 1209 * The reference must be released by the same entity that added the reference
1210 1210 * In the above scheme, the eager is the entity that adds and releases the
1211 1211 * references. Note that tcp_accept_finish executes in the squeue of the eager
1212 1212 * (albeit after it is attached to the acceptor stream). Though 1. executes
1213 1213 * in the listener's squeue, the eager is nascent at this point and the
1214 1214 * reference can be considered to have been added on behalf of the eager.
1215 1215 *
1216 1216 * Eager getting a Reset or listener closing:
1217 1217 * ==========================================
1218 1218 *
1219 1219 * Once the listener and eager are linked, the listener never does the unlink.
1220 1220 * If the listener needs to close, tcp_eager_cleanup() is called which queues
1221 1221 * a message on all eager perimeter. The eager then does the unlink, clears
1222 1222 * any pointers to the listener's queue and drops the reference to the
1223 1223 * listener. The listener waits in tcp_close outside the squeue until its
1224 1224 * refcount has dropped to 1. This ensures that the listener has waited for
1225 1225 * all eagers to clear their association with the listener.
1226 1226 *
1227 1227 * Similarly, if eager decides to go away, it can unlink itself and close.
1228 1228 * When the T_CONN_RES comes down, we check if eager has closed. Note that
1229 1229 * the reference to eager is still valid because of the extra ref we put
1230 1230 * in tcp_send_conn_ind.
1231 1231 *
1232 1232 * Listener can always locate the eager under the protection
1233 1233 * of the listener->tcp_eager_lock, and then do a refhold
1234 1234 * on the eager during the accept processing.
1235 1235 *
1236 1236 * The acceptor stream accesses the eager in the accept processing
1237 1237 * based on the ref placed on eager before sending T_conn_ind.
1238 1238 * The only entity that can negate this refhold is a listener close
1239 1239 * which is mutually exclusive with an active acceptor stream.
1240 1240 *
1241 1241 * Eager's reference on the listener
1242 1242 * ===================================
1243 1243 *
1244 1244 * If the accept happens (even on a closed eager) the eager drops its
1245 1245 * reference on the listener at the start of tcp_accept_finish. If the
1246 1246 * eager is killed due to an incoming RST before the T_conn_ind is sent up,
1247 1247 * the reference is dropped in tcp_closei_local. If the listener closes,
1248 1248 * the reference is dropped in tcp_eager_kill. In all cases the reference
1249 1249 * is dropped while executing in the eager's context (squeue).
1250 1250 */
1251 1251 /* END CSTYLED */
1252 1252
1253 1253 /* Process the SYN packet, mp, directed at the listener 'tcp' */
1254 1254
1255 1255 /*
1256 1256 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN.
1257 1257 * tcp_input_data will not see any packets for listeners since the listener
1258 1258 * has conn_recv set to tcp_input_listener.
1259 1259 */
1260 1260 /* ARGSUSED */
1261 1261 static void
1262 1262 tcp_input_listener(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
1263 1263 {
1264 1264 tcpha_t *tcpha;
1265 1265 uint32_t seg_seq;
1266 1266 tcp_t *eager;
1267 1267 int err;
1268 1268 conn_t *econnp = NULL;
1269 1269 squeue_t *new_sqp;
1270 1270 mblk_t *mp1;
1271 1271 uint_t ip_hdr_len;
1272 1272 conn_t *lconnp = (conn_t *)arg;
1273 1273 tcp_t *listener = lconnp->conn_tcp;
1274 1274 tcp_stack_t *tcps = listener->tcp_tcps;
1275 1275 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip;
1276 1276 uint_t flags;
1277 1277 mblk_t *tpi_mp;
1278 1278 uint_t ifindex = ira->ira_ruifindex;
1279 1279 boolean_t tlc_set = B_FALSE;
1280 1280
1281 1281 ip_hdr_len = ira->ira_ip_hdr_length;
1282 1282 tcpha = (tcpha_t *)&mp->b_rptr[ip_hdr_len];
1283 1283 flags = (unsigned int)tcpha->tha_flags & 0xFF;
1284 1284
1285 1285 DTRACE_TCP5(receive, mblk_t *, NULL, ip_xmit_attr_t *, lconnp->conn_ixa,
1286 1286 __dtrace_tcp_void_ip_t *, mp->b_rptr, tcp_t *, listener,
1287 1287 __dtrace_tcp_tcph_t *, tcpha);
1288 1288
1289 1289 if (!(flags & TH_SYN)) {
1290 1290 if ((flags & TH_RST) || (flags & TH_URG)) {
1291 1291 freemsg(mp);
1292 1292 return;
1293 1293 }
1294 1294 if (flags & TH_ACK) {
1295 1295 /* Note this executes in listener's squeue */
1296 1296 tcp_xmit_listeners_reset(mp, ira, ipst, lconnp);
1297 1297 return;
1298 1298 }
1299 1299
1300 1300 freemsg(mp);
1301 1301 return;
1302 1302 }
1303 1303
1304 1304 if (listener->tcp_state != TCPS_LISTEN)
1305 1305 goto error2;
1306 1306
1307 1307 ASSERT(IPCL_IS_BOUND(lconnp));
1308 1308
1309 1309 mutex_enter(&listener->tcp_eager_lock);
1310 1310
1311 1311 /*
1312 1312 * The system is under memory pressure, so we need to do our part
1313 1313 * to relieve the pressure. So we only accept new request if there
1314 1314 * is nothing waiting to be accepted or waiting to complete the 3-way
1315 1315 * handshake. This means that busy listener will not get too many
1316 1316 * new requests which they cannot handle in time while non-busy
1317 1317 * listener is still functioning properly.
1318 1318 */
1319 1319 if (tcps->tcps_reclaim && (listener->tcp_conn_req_cnt_q > 0 ||
1320 1320 listener->tcp_conn_req_cnt_q0 > 0)) {
1321 1321 mutex_exit(&listener->tcp_eager_lock);
1322 1322 TCP_STAT(tcps, tcp_listen_mem_drop);
1323 1323 goto error2;
1324 1324 }
1325 1325
1326 1326 if (listener->tcp_conn_req_cnt_q >= listener->tcp_conn_req_max) {
1327 1327 mutex_exit(&listener->tcp_eager_lock);
1328 1328 TCP_STAT(tcps, tcp_listendrop);
1329 1329 TCPS_BUMP_MIB(tcps, tcpListenDrop);
1330 1330 if (lconnp->conn_debug) {
1331 1331 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR,
1332 1332 "tcp_input_listener: listen backlog (max=%d) "
1333 1333 "overflow (%d pending) on %s",
1334 1334 listener->tcp_conn_req_max,
1335 1335 listener->tcp_conn_req_cnt_q,
1336 1336 tcp_display(listener, NULL, DISP_PORT_ONLY));
1337 1337 }
1338 1338 goto error2;
1339 1339 }
1340 1340
1341 1341 if (listener->tcp_conn_req_cnt_q0 >=
1342 1342 listener->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) {
1343 1343 /*
1344 1344 * Q0 is full. Drop a pending half-open req from the queue
1345 1345 * to make room for the new SYN req. Also mark the time we
1346 1346 * drop a SYN.
1347 1347 *
1348 1348 * A more aggressive defense against SYN attack will
1349 1349 * be to set the "tcp_syn_defense" flag now.
1350 1350 */
1351 1351 TCP_STAT(tcps, tcp_listendropq0);
1352 1352 listener->tcp_last_rcv_lbolt = ddi_get_lbolt64();
1353 1353 if (!tcp_drop_q0(listener)) {
1354 1354 mutex_exit(&listener->tcp_eager_lock);
1355 1355 TCPS_BUMP_MIB(tcps, tcpListenDropQ0);
1356 1356 if (lconnp->conn_debug) {
1357 1357 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE,
1358 1358 "tcp_input_listener: listen half-open "
1359 1359 "queue (max=%d) full (%d pending) on %s",
1360 1360 tcps->tcps_conn_req_max_q0,
1361 1361 listener->tcp_conn_req_cnt_q0,
1362 1362 tcp_display(listener, NULL,
1363 1363 DISP_PORT_ONLY));
1364 1364 }
1365 1365 goto error2;
1366 1366 }
1367 1367 }
1368 1368
1369 1369 /*
1370 1370 * Enforce the limit set on the number of connections per listener.
1371 1371 * Note that tlc_cnt starts with 1. So need to add 1 to tlc_max
1372 1372 * for comparison.
1373 1373 */
1374 1374 if (listener->tcp_listen_cnt != NULL) {
1375 1375 tcp_listen_cnt_t *tlc = listener->tcp_listen_cnt;
1376 1376 int64_t now;
1377 1377
1378 1378 if (atomic_inc_32_nv(&tlc->tlc_cnt) > tlc->tlc_max + 1) {
1379 1379 mutex_exit(&listener->tcp_eager_lock);
1380 1380 now = ddi_get_lbolt64();
1381 1381 atomic_dec_32(&tlc->tlc_cnt);
1382 1382 TCP_STAT(tcps, tcp_listen_cnt_drop);
1383 1383 tlc->tlc_drop++;
1384 1384 if (now - tlc->tlc_report_time >
1385 1385 MSEC_TO_TICK(TCP_TLC_REPORT_INTERVAL)) {
1386 1386 zcmn_err(lconnp->conn_zoneid, CE_WARN,
1387 1387 "Listener (port %d) connection max (%u) "
1388 1388 "reached: %u attempts dropped total\n",
1389 1389 ntohs(listener->tcp_connp->conn_lport),
1390 1390 tlc->tlc_max, tlc->tlc_drop);
1391 1391 tlc->tlc_report_time = now;
1392 1392 }
1393 1393 goto error2;
1394 1394 }
1395 1395 tlc_set = B_TRUE;
1396 1396 }
1397 1397
1398 1398 mutex_exit(&listener->tcp_eager_lock);
1399 1399
1400 1400 /*
1401 1401 * IP sets ira_sqp to either the senders conn_sqp (for loopback)
1402 1402 * or based on the ring (for packets from GLD). Otherwise it is
1403 1403 * set based on lbolt i.e., a somewhat random number.
1404 1404 */
1405 1405 ASSERT(ira->ira_sqp != NULL);
1406 1406 new_sqp = ira->ira_sqp;
1407 1407
1408 1408 econnp = (conn_t *)tcp_get_conn(arg2, tcps);
1409 1409 if (econnp == NULL)
1410 1410 goto error2;
1411 1411
1412 1412 ASSERT(econnp->conn_netstack == lconnp->conn_netstack);
1413 1413 econnp->conn_sqp = new_sqp;
1414 1414 econnp->conn_initial_sqp = new_sqp;
1415 1415 econnp->conn_ixa->ixa_sqp = new_sqp;
1416 1416
1417 1417 econnp->conn_fport = tcpha->tha_lport;
1418 1418 econnp->conn_lport = tcpha->tha_fport;
1419 1419
1420 1420 err = conn_inherit_parent(lconnp, econnp);
1421 1421 if (err != 0)
1422 1422 goto error3;
1423 1423
1424 1424 /* We already know the laddr of the new connection is ours */
1425 1425 econnp->conn_ixa->ixa_src_generation = ipst->ips_src_generation;
1426 1426
1427 1427 ASSERT(OK_32PTR(mp->b_rptr));
1428 1428 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION ||
1429 1429 IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
1430 1430
1431 1431 if (lconnp->conn_family == AF_INET) {
1432 1432 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
1433 1433 tpi_mp = tcp_conn_create_v4(lconnp, econnp, mp, ira);
1434 1434 } else {
1435 1435 tpi_mp = tcp_conn_create_v6(lconnp, econnp, mp, ira);
1436 1436 }
1437 1437
1438 1438 if (tpi_mp == NULL)
1439 1439 goto error3;
1440 1440
1441 1441 eager = econnp->conn_tcp;
1442 1442 eager->tcp_detached = B_TRUE;
1443 1443 SOCK_CONNID_INIT(eager->tcp_connid);
1444 1444
1445 1445 /*
1446 1446 * Initialize the eager's tcp_t and inherit some parameters from
1447 1447 * the listener.
1448 1448 */
1449 1449 tcp_init_values(eager, listener);
1450 1450
1451 1451 ASSERT((econnp->conn_ixa->ixa_flags &
1452 1452 (IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
1453 1453 IXAF_VERIFY_PMTU | IXAF_VERIFY_LSO)) ==
1454 1454 (IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
1455 1455 IXAF_VERIFY_PMTU | IXAF_VERIFY_LSO));
1456 1456
1457 1457 if (!tcps->tcps_dev_flow_ctl)
1458 1458 econnp->conn_ixa->ixa_flags |= IXAF_NO_DEV_FLOW_CTL;
1459 1459
1460 1460 /* Prepare for diffing against previous packets */
1461 1461 eager->tcp_recvifindex = 0;
1462 1462 eager->tcp_recvhops = 0xffffffffU;
1463 1463
1464 1464 if (!(ira->ira_flags & IRAF_IS_IPV4) && econnp->conn_bound_if == 0) {
1465 1465 if (IN6_IS_ADDR_LINKSCOPE(&econnp->conn_faddr_v6) ||
1466 1466 IN6_IS_ADDR_LINKSCOPE(&econnp->conn_laddr_v6)) {
1467 1467 econnp->conn_incoming_ifindex = ifindex;
1468 1468 econnp->conn_ixa->ixa_flags |= IXAF_SCOPEID_SET;
1469 1469 econnp->conn_ixa->ixa_scopeid = ifindex;
1470 1470 }
1471 1471 }
1472 1472
1473 1473 if ((ira->ira_flags & (IRAF_IS_IPV4|IRAF_IPV4_OPTIONS)) ==
1474 1474 (IRAF_IS_IPV4|IRAF_IPV4_OPTIONS) &&
1475 1475 tcps->tcps_rev_src_routes) {
1476 1476 ipha_t *ipha = (ipha_t *)mp->b_rptr;
1477 1477 ip_pkt_t *ipp = &econnp->conn_xmit_ipp;
1478 1478
1479 1479 /* Source routing option copyover (reverse it) */
1480 1480 err = ip_find_hdr_v4(ipha, ipp, B_TRUE);
1481 1481 if (err != 0) {
1482 1482 freemsg(tpi_mp);
1483 1483 goto error3;
1484 1484 }
1485 1485 ip_pkt_source_route_reverse_v4(ipp);
1486 1486 }
1487 1487
1488 1488 ASSERT(eager->tcp_conn.tcp_eager_conn_ind == NULL);
1489 1489 ASSERT(!eager->tcp_tconnind_started);
1490 1490 /*
1491 1491 * If the SYN came with a credential, it's a loopback packet or a
1492 1492 * labeled packet; attach the credential to the TPI message.
1493 1493 */
1494 1494 if (ira->ira_cred != NULL)
1495 1495 mblk_setcred(tpi_mp, ira->ira_cred, ira->ira_cpid);
1496 1496
1497 1497 eager->tcp_conn.tcp_eager_conn_ind = tpi_mp;
1498 1498 ASSERT(eager->tcp_ordrel_mp == NULL);
1499 1499
1500 1500 /* Inherit the listener's non-STREAMS flag */
1501 1501 if (IPCL_IS_NONSTR(lconnp)) {
1502 1502 econnp->conn_flags |= IPCL_NONSTR;
1503 1503 /* All non-STREAMS tcp_ts are sockets */
1504 1504 eager->tcp_issocket = B_TRUE;
1505 1505 } else {
1506 1506 /*
1507 1507 * Pre-allocate the T_ordrel_ind mblk for TPI socket so that
1508 1508 * at close time, we will always have that to send up.
1509 1509 * Otherwise, we need to do special handling in case the
1510 1510 * allocation fails at that time.
1511 1511 */
1512 1512 if ((eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL)
1513 1513 goto error3;
1514 1514 }
1515 1515 /*
1516 1516 * Now that the IP addresses and ports are setup in econnp we
1517 1517 * can do the IPsec policy work.
1518 1518 */
1519 1519 if (ira->ira_flags & IRAF_IPSEC_SECURE) {
1520 1520 if (lconnp->conn_policy != NULL) {
1521 1521 /*
1522 1522 * Inherit the policy from the listener; use
1523 1523 * actions from ira
1524 1524 */
1525 1525 if (!ip_ipsec_policy_inherit(econnp, lconnp, ira)) {
1526 1526 CONN_DEC_REF(econnp);
1527 1527 freemsg(mp);
1528 1528 goto error3;
1529 1529 }
1530 1530 }
1531 1531 }
1532 1532
1533 1533 /*
1534 1534 * tcp_set_destination() may set tcp_rwnd according to the route
1535 1535 * metrics. If it does not, the eager's receive window will be set
1536 1536 * to the listener's receive window later in this function.
1537 1537 */
1538 1538 eager->tcp_rwnd = 0;
1539 1539
1540 1540 if (is_system_labeled()) {
1541 1541 ip_xmit_attr_t *ixa = econnp->conn_ixa;
1542 1542
1543 1543 ASSERT(ira->ira_tsl != NULL);
1544 1544 /* Discard any old label */
1545 1545 if (ixa->ixa_free_flags & IXA_FREE_TSL) {
1546 1546 ASSERT(ixa->ixa_tsl != NULL);
1547 1547 label_rele(ixa->ixa_tsl);
1548 1548 ixa->ixa_free_flags &= ~IXA_FREE_TSL;
1549 1549 ixa->ixa_tsl = NULL;
1550 1550 }
1551 1551 if ((lconnp->conn_mlp_type != mlptSingle ||
1552 1552 lconnp->conn_mac_mode != CONN_MAC_DEFAULT) &&
1553 1553 ira->ira_tsl != NULL) {
1554 1554 /*
1555 1555 * If this is an MLP connection or a MAC-Exempt
1556 1556 * connection with an unlabeled node, packets are to be
1557 1557 * exchanged using the security label of the received
1558 1558 * SYN packet instead of the server application's label.
1559 1559 * tsol_check_dest called from ip_set_destination
1560 1560 * might later update TSF_UNLABELED by replacing
1561 1561 * ixa_tsl with a new label.
1562 1562 */
1563 1563 label_hold(ira->ira_tsl);
1564 1564 ip_xmit_attr_replace_tsl(ixa, ira->ira_tsl);
1565 1565 DTRACE_PROBE2(mlp_syn_accept, conn_t *,
1566 1566 econnp, ts_label_t *, ixa->ixa_tsl)
1567 1567 } else {
1568 1568 ixa->ixa_tsl = crgetlabel(econnp->conn_cred);
1569 1569 DTRACE_PROBE2(syn_accept, conn_t *,
1570 1570 econnp, ts_label_t *, ixa->ixa_tsl)
1571 1571 }
1572 1572 /*
1573 1573 * conn_connect() called from tcp_set_destination will verify
1574 1574 * the destination is allowed to receive packets at the
1575 1575 * security label of the SYN-ACK we are generating. As part of
1576 1576 * that, tsol_check_dest() may create a new effective label for
1577 1577 * this connection.
1578 1578 * Finally conn_connect() will call conn_update_label.
1579 1579 * All that remains for TCP to do is to call
1580 1580 * conn_build_hdr_template which is done as part of
1581 1581 * tcp_set_destination.
1582 1582 */
1583 1583 }
1584 1584
1585 1585 /*
1586 1586 * Since we will clear tcp_listener before we clear tcp_detached
1587 1587 * in the accept code we need tcp_hard_binding aka tcp_accept_inprogress
1588 1588 * so we can tell a TCP_IS_DETACHED_NONEAGER apart.
1589 1589 */
1590 1590 eager->tcp_hard_binding = B_TRUE;
1591 1591
1592 1592 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[
1593 1593 TCP_BIND_HASH(econnp->conn_lport)], eager, 0);
1594 1594
1595 1595 CL_INET_CONNECT(econnp, B_FALSE, err);
1596 1596 if (err != 0) {
1597 1597 tcp_bind_hash_remove(eager);
1598 1598 goto error3;
1599 1599 }
1600 1600
1601 1601 SOCK_CONNID_BUMP(eager->tcp_connid);
1602 1602
1603 1603 /*
1604 1604 * Adapt our mss, ttl, ... based on the remote address.
1605 1605 */
1606 1606
1607 1607 if (tcp_set_destination(eager) != 0) {
1608 1608 TCPS_BUMP_MIB(tcps, tcpAttemptFails);
1609 1609 /* Undo the bind_hash_insert */
1610 1610 tcp_bind_hash_remove(eager);
1611 1611 goto error3;
1612 1612 }
1613 1613
1614 1614 /* Process all TCP options. */
1615 1615 tcp_process_options(eager, tcpha);
1616 1616
1617 1617 /* Is the other end ECN capable? */
1618 1618 if (tcps->tcps_ecn_permitted >= 1 &&
1619 1619 (tcpha->tha_flags & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) {
1620 1620 eager->tcp_ecn_ok = B_TRUE;
1621 1621 }
1622 1622
1623 1623 /*
1624 1624 * The listener's conn_rcvbuf should be the default window size or a
1625 1625 * window size changed via SO_RCVBUF option. First round up the
1626 1626 * eager's tcp_rwnd to the nearest MSS. Then find out the window
1627 1627 * scale option value if needed. Call tcp_rwnd_set() to finish the
1628 1628 * setting.
1629 1629 *
1630 1630 * Note if there is a rpipe metric associated with the remote host,
1631 1631 * we should not inherit receive window size from listener.
1632 1632 */
1633 1633 eager->tcp_rwnd = MSS_ROUNDUP(
1634 1634 (eager->tcp_rwnd == 0 ? econnp->conn_rcvbuf :
1635 1635 eager->tcp_rwnd), eager->tcp_mss);
1636 1636 if (eager->tcp_snd_ws_ok)
1637 1637 tcp_set_ws_value(eager);
1638 1638 /*
1639 1639 * Note that this is the only place tcp_rwnd_set() is called for
1640 1640 * accepting a connection. We need to call it here instead of
1641 1641 * after the 3-way handshake because we need to tell the other
1642 1642 * side our rwnd in the SYN-ACK segment.
1643 1643 */
1644 1644 (void) tcp_rwnd_set(eager, eager->tcp_rwnd);
1645 1645
1646 1646 ASSERT(eager->tcp_connp->conn_rcvbuf != 0 &&
1647 1647 eager->tcp_connp->conn_rcvbuf == eager->tcp_rwnd);
1648 1648
1649 1649 ASSERT(econnp->conn_rcvbuf != 0 &&
1650 1650 econnp->conn_rcvbuf == eager->tcp_rwnd);
1651 1651
1652 1652 /* Put a ref on the listener for the eager. */
1653 1653 CONN_INC_REF(lconnp);
1654 1654 mutex_enter(&listener->tcp_eager_lock);
1655 1655 listener->tcp_eager_next_q0->tcp_eager_prev_q0 = eager;
1656 1656 eager->tcp_eager_next_q0 = listener->tcp_eager_next_q0;
1657 1657 listener->tcp_eager_next_q0 = eager;
1658 1658 eager->tcp_eager_prev_q0 = listener;
1659 1659
1660 1660 /* Set tcp_listener before adding it to tcp_conn_fanout */
1661 1661 eager->tcp_listener = listener;
1662 1662 eager->tcp_saved_listener = listener;
1663 1663
1664 1664 /*
1665 1665 * Set tcp_listen_cnt so that when the connection is done, the counter
1666 1666 * is decremented.
1667 1667 */
1668 1668 eager->tcp_listen_cnt = listener->tcp_listen_cnt;
1669 1669
1670 1670 /*
1671 1671 * Tag this detached tcp vector for later retrieval
1672 1672 * by our listener client in tcp_accept().
1673 1673 */
1674 1674 eager->tcp_conn_req_seqnum = listener->tcp_conn_req_seqnum;
1675 1675 listener->tcp_conn_req_cnt_q0++;
1676 1676 if (++listener->tcp_conn_req_seqnum == -1) {
1677 1677 /*
1678 1678 * -1 is "special" and defined in TPI as something
1679 1679 * that should never be used in T_CONN_IND
1680 1680 */
1681 1681 ++listener->tcp_conn_req_seqnum;
1682 1682 }
1683 1683 mutex_exit(&listener->tcp_eager_lock);
1684 1684
1685 1685 if (listener->tcp_syn_defense) {
1686 1686 /* Don't drop the SYN that comes from a good IP source */
1687 1687 ipaddr_t *addr_cache;
1688 1688
1689 1689 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache);
1690 1690 if (addr_cache != NULL && econnp->conn_faddr_v4 ==
1691 1691 addr_cache[IP_ADDR_CACHE_HASH(econnp->conn_faddr_v4)]) {
1692 1692 eager->tcp_dontdrop = B_TRUE;
1693 1693 }
1694 1694 }
1695 1695
1696 1696 /*
1697 1697 * We need to insert the eager in its own perimeter but as soon
1698 1698 * as we do that, we expose the eager to the classifier and
1699 1699 * should not touch any field outside the eager's perimeter.
1700 1700 * So do all the work necessary before inserting the eager
1701 1701 * in its own perimeter. Be optimistic that conn_connect()
1702 1702 * will succeed but undo everything if it fails.
1703 1703 */
1704 1704 seg_seq = ntohl(tcpha->tha_seq);
1705 1705 eager->tcp_irs = seg_seq;
1706 1706 eager->tcp_rack = seg_seq;
1707 1707 eager->tcp_rnxt = seg_seq + 1;
1708 1708 eager->tcp_tcpha->tha_ack = htonl(eager->tcp_rnxt);
1709 1709 TCPS_BUMP_MIB(tcps, tcpPassiveOpens);
1710 1710 eager->tcp_state = TCPS_SYN_RCVD;
1711 1711 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
1712 1712 econnp->conn_ixa, void, NULL, tcp_t *, eager, void, NULL,
1713 1713 int32_t, TCPS_LISTEN);
1714 1714
1715 1715 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss,
1716 1716 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE);
1717 1717 if (mp1 == NULL) {
1718 1718 /*
1719 1719 * Increment the ref count as we are going to
1720 1720 * enqueueing an mp in squeue
1721 1721 */
1722 1722 CONN_INC_REF(econnp);
1723 1723 goto error;
1724 1724 }
1725 1725
1726 1726 /*
1727 1727 * We need to start the rto timer. In normal case, we start
1728 1728 * the timer after sending the packet on the wire (or at
1729 1729 * least believing that packet was sent by waiting for
1730 1730 * conn_ip_output() to return). Since this is the first packet
1731 1731 * being sent on the wire for the eager, our initial tcp_rto
1732 1732 * is at least tcp_rexmit_interval_min which is a fairly
1733 1733 * large value to allow the algorithm to adjust slowly to large
1734 1734 * fluctuations of RTT during first few transmissions.
1735 1735 *
1736 1736 * Starting the timer first and then sending the packet in this
1737 1737 * case shouldn't make much difference since tcp_rexmit_interval_min
1738 1738 * is of the order of several 100ms and starting the timer
1739 1739 * first and then sending the packet will result in difference
1740 1740 * of few micro seconds.
1741 1741 *
1742 1742 * Without this optimization, we are forced to hold the fanout
1743 1743 * lock across the ipcl_bind_insert() and sending the packet
1744 1744 * so that we don't race against an incoming packet (maybe RST)
1745 1745 * for this eager.
1746 1746 *
1747 1747 * It is necessary to acquire an extra reference on the eager
1748 1748 * at this point and hold it until after tcp_send_data() to
1749 1749 * ensure against an eager close race.
1750 1750 */
1751 1751
1752 1752 CONN_INC_REF(econnp);
1753 1753
1754 1754 TCP_TIMER_RESTART(eager, eager->tcp_rto);
1755 1755
1756 1756 /*
1757 1757 * Insert the eager in its own perimeter now. We are ready to deal
1758 1758 * with any packets on eager.
1759 1759 */
1760 1760 if (ipcl_conn_insert(econnp) != 0)
1761 1761 goto error;
1762 1762
1763 1763 ASSERT(econnp->conn_ixa->ixa_notify_cookie == econnp->conn_tcp);
1764 1764 freemsg(mp);
1765 1765 /*
1766 1766 * Send the SYN-ACK. Use the right squeue so that conn_ixa is
1767 1767 * only used by one thread at a time.
1768 1768 */
1769 1769 if (econnp->conn_sqp == lconnp->conn_sqp) {
1770 1770 DTRACE_TCP5(send, mblk_t *, NULL, ip_xmit_attr_t *,
1771 1771 econnp->conn_ixa, __dtrace_tcp_void_ip_t *, mp1->b_rptr,
1772 1772 tcp_t *, eager, __dtrace_tcp_tcph_t *,
1773 1773 &mp1->b_rptr[econnp->conn_ixa->ixa_ip_hdr_length]);
1774 1774 (void) conn_ip_output(mp1, econnp->conn_ixa);
1775 1775 CONN_DEC_REF(econnp);
1776 1776 } else {
1777 1777 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_send_synack,
1778 1778 econnp, NULL, SQ_PROCESS, SQTAG_TCP_SEND_SYNACK);
1779 1779 }
1780 1780 return;
1781 1781 error:
1782 1782 freemsg(mp1);
1783 1783 eager->tcp_closemp_used = B_TRUE;
1784 1784 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15);
1785 1785 mp1 = &eager->tcp_closemp;
1786 1786 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_eager_kill,
1787 1787 econnp, NULL, SQ_FILL, SQTAG_TCP_CONN_REQ_2);
1788 1788
1789 1789 /*
1790 1790 * If a connection already exists, send the mp to that connections so
1791 1791 * that it can be appropriately dealt with.
1792 1792 */
1793 1793 ipst = tcps->tcps_netstack->netstack_ip;
1794 1794
1795 1795 if ((econnp = ipcl_classify(mp, ira, ipst)) != NULL) {
1796 1796 if (!IPCL_IS_CONNECTED(econnp)) {
1797 1797 /*
1798 1798 * Something bad happened. ipcl_conn_insert()
1799 1799 * failed because a connection already existed
1800 1800 * in connected hash but we can't find it
1801 1801 * anymore (someone blew it away). Just
1802 1802 * free this message and hopefully remote
1803 1803 * will retransmit at which time the SYN can be
1804 1804 * treated as a new connection or dealth with
1805 1805 * a TH_RST if a connection already exists.
1806 1806 */
1807 1807 CONN_DEC_REF(econnp);
1808 1808 freemsg(mp);
1809 1809 } else {
1810 1810 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp, tcp_input_data,
1811 1811 econnp, ira, SQ_FILL, SQTAG_TCP_CONN_REQ_1);
1812 1812 }
1813 1813 } else {
1814 1814 /* Nobody wants this packet */
1815 1815 freemsg(mp);
1816 1816 }
1817 1817 return;
1818 1818 error3:
1819 1819 CONN_DEC_REF(econnp);
1820 1820 error2:
1821 1821 freemsg(mp);
1822 1822 if (tlc_set)
1823 1823 atomic_dec_32(&listener->tcp_listen_cnt->tlc_cnt);
1824 1824 }
1825 1825
1826 1826 /*
1827 1827 * In an ideal case of vertical partition in NUMA architecture, its
1828 1828 * beneficial to have the listener and all the incoming connections
1829 1829 * tied to the same squeue. The other constraint is that incoming
1830 1830 * connections should be tied to the squeue attached to interrupted
1831 1831 * CPU for obvious locality reason so this leaves the listener to
1832 1832 * be tied to the same squeue. Our only problem is that when listener
1833 1833 * is binding, the CPU that will get interrupted by the NIC whose
1834 1834 * IP address the listener is binding to is not even known. So
1835 1835 * the code below allows us to change that binding at the time the
1836 1836 * CPU is interrupted by virtue of incoming connection's squeue.
1837 1837 *
1838 1838 * This is usefull only in case of a listener bound to a specific IP
1839 1839 * address. For other kind of listeners, they get bound the
1840 1840 * very first time and there is no attempt to rebind them.
1841 1841 */
1842 1842 void
1843 1843 tcp_input_listener_unbound(void *arg, mblk_t *mp, void *arg2,
1844 1844 ip_recv_attr_t *ira)
1845 1845 {
1846 1846 conn_t *connp = (conn_t *)arg;
1847 1847 squeue_t *sqp = (squeue_t *)arg2;
1848 1848 squeue_t *new_sqp;
1849 1849 uint32_t conn_flags;
1850 1850
1851 1851 /*
1852 1852 * IP sets ira_sqp to either the senders conn_sqp (for loopback)
1853 1853 * or based on the ring (for packets from GLD). Otherwise it is
1854 1854 * set based on lbolt i.e., a somewhat random number.
1855 1855 */
1856 1856 ASSERT(ira->ira_sqp != NULL);
1857 1857 new_sqp = ira->ira_sqp;
1858 1858
1859 1859 if (connp->conn_fanout == NULL)
1860 1860 goto done;
1861 1861
1862 1862 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) {
1863 1863 mutex_enter(&connp->conn_fanout->connf_lock);
1864 1864 mutex_enter(&connp->conn_lock);
1865 1865 /*
1866 1866 * No one from read or write side can access us now
1867 1867 * except for already queued packets on this squeue.
1868 1868 * But since we haven't changed the squeue yet, they
1869 1869 * can't execute. If they are processed after we have
1870 1870 * changed the squeue, they are sent back to the
1871 1871 * correct squeue down below.
1872 1872 * But a listner close can race with processing of
1873 1873 * incoming SYN. If incoming SYN processing changes
1874 1874 * the squeue then the listener close which is waiting
1875 1875 * to enter the squeue would operate on the wrong
1876 1876 * squeue. Hence we don't change the squeue here unless
1877 1877 * the refcount is exactly the minimum refcount. The
1878 1878 * minimum refcount of 4 is counted as - 1 each for
1879 1879 * TCP and IP, 1 for being in the classifier hash, and
1880 1880 * 1 for the mblk being processed.
1881 1881 */
1882 1882
1883 1883 if (connp->conn_ref != 4 ||
1884 1884 connp->conn_tcp->tcp_state != TCPS_LISTEN) {
1885 1885 mutex_exit(&connp->conn_lock);
1886 1886 mutex_exit(&connp->conn_fanout->connf_lock);
1887 1887 goto done;
1888 1888 }
1889 1889 if (connp->conn_sqp != new_sqp) {
1890 1890 while (connp->conn_sqp != new_sqp)
1891 1891 (void) atomic_cas_ptr(&connp->conn_sqp, sqp,
1892 1892 new_sqp);
1893 1893 /* No special MT issues for outbound ixa_sqp hint */
1894 1894 connp->conn_ixa->ixa_sqp = new_sqp;
1895 1895 }
1896 1896
1897 1897 do {
1898 1898 conn_flags = connp->conn_flags;
1899 1899 conn_flags |= IPCL_FULLY_BOUND;
1900 1900 (void) atomic_cas_32(&connp->conn_flags,
1901 1901 connp->conn_flags, conn_flags);
1902 1902 } while (!(connp->conn_flags & IPCL_FULLY_BOUND));
1903 1903
1904 1904 mutex_exit(&connp->conn_fanout->connf_lock);
1905 1905 mutex_exit(&connp->conn_lock);
1906 1906
1907 1907 /*
1908 1908 * Assume we have picked a good squeue for the listener. Make
1909 1909 * subsequent SYNs not try to change the squeue.
1910 1910 */
1911 1911 connp->conn_recv = tcp_input_listener;
1912 1912 }
1913 1913
1914 1914 done:
1915 1915 if (connp->conn_sqp != sqp) {
1916 1916 CONN_INC_REF(connp);
1917 1917 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp,
1918 1918 ira, SQ_FILL, SQTAG_TCP_CONN_REQ_UNBOUND);
1919 1919 } else {
1920 1920 tcp_input_listener(connp, mp, sqp, ira);
1921 1921 }
1922 1922 }
1923 1923
1924 1924 /*
1925 1925 * Send up all messages queued on tcp_rcv_list.
1926 1926 */
1927 1927 uint_t
1928 1928 tcp_rcv_drain(tcp_t *tcp)
1929 1929 {
1930 1930 mblk_t *mp;
1931 1931 uint_t ret = 0;
1932 1932 #ifdef DEBUG
1933 1933 uint_t cnt = 0;
1934 1934 #endif
1935 1935 queue_t *q = tcp->tcp_connp->conn_rq;
1936 1936
1937 1937 /* Can't drain on an eager connection */
1938 1938 if (tcp->tcp_listener != NULL)
1939 1939 return (ret);
1940 1940
1941 1941 /* Can't be a non-STREAMS connection */
1942 1942 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1943 1943
1944 1944 /* No need for the push timer now. */
1945 1945 if (tcp->tcp_push_tid != 0) {
1946 1946 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid);
1947 1947 tcp->tcp_push_tid = 0;
1948 1948 }
1949 1949
1950 1950 /*
1951 1951 * Handle two cases here: we are currently fused or we were
1952 1952 * previously fused and have some urgent data to be delivered
1953 1953 * upstream. The latter happens because we either ran out of
1954 1954 * memory or were detached and therefore sending the SIGURG was
1955 1955 * deferred until this point. In either case we pass control
1956 1956 * over to tcp_fuse_rcv_drain() since it may need to complete
1957 1957 * some work.
1958 1958 */
1959 1959 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) {
1960 1960 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL :
1961 1961 &tcp->tcp_fused_sigurg_mp))
1962 1962 return (ret);
1963 1963 }
1964 1964
1965 1965 while ((mp = tcp->tcp_rcv_list) != NULL) {
1966 1966 tcp->tcp_rcv_list = mp->b_next;
1967 1967 mp->b_next = NULL;
1968 1968 #ifdef DEBUG
1969 1969 cnt += msgdsize(mp);
1970 1970 #endif
1971 1971 putnext(q, mp);
1972 1972 }
1973 1973 #ifdef DEBUG
1974 1974 ASSERT(cnt == tcp->tcp_rcv_cnt);
1975 1975 #endif
1976 1976 tcp->tcp_rcv_last_head = NULL;
1977 1977 tcp->tcp_rcv_last_tail = NULL;
1978 1978 tcp->tcp_rcv_cnt = 0;
1979 1979
1980 1980 if (canputnext(q))
1981 1981 return (tcp_rwnd_reopen(tcp));
1982 1982
1983 1983 return (ret);
1984 1984 }
1985 1985
1986 1986 /*
1987 1987 * Queue data on tcp_rcv_list which is a b_next chain.
1988 1988 * tcp_rcv_last_head/tail is the last element of this chain.
1989 1989 * Each element of the chain is a b_cont chain.
1990 1990 *
1991 1991 * M_DATA messages are added to the current element.
1992 1992 * Other messages are added as new (b_next) elements.
1993 1993 */
1994 1994 void
1995 1995 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len, cred_t *cr)
1996 1996 {
1997 1997 ASSERT(seg_len == msgdsize(mp));
1998 1998 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL);
1999 1999
2000 2000 if (is_system_labeled()) {
2001 2001 ASSERT(cr != NULL || msg_getcred(mp, NULL) != NULL);
2002 2002 /*
2003 2003 * Provide for protocols above TCP such as RPC. NOPID leaves
2004 2004 * db_cpid unchanged.
2005 2005 * The cred could have already been set.
2006 2006 */
2007 2007 if (cr != NULL)
2008 2008 mblk_setcred(mp, cr, NOPID);
2009 2009 }
2010 2010
2011 2011 if (tcp->tcp_rcv_list == NULL) {
2012 2012 ASSERT(tcp->tcp_rcv_last_head == NULL);
2013 2013 tcp->tcp_rcv_list = mp;
2014 2014 tcp->tcp_rcv_last_head = mp;
2015 2015 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) {
2016 2016 tcp->tcp_rcv_last_tail->b_cont = mp;
2017 2017 } else {
2018 2018 tcp->tcp_rcv_last_head->b_next = mp;
2019 2019 tcp->tcp_rcv_last_head = mp;
2020 2020 }
2021 2021
2022 2022 while (mp->b_cont)
2023 2023 mp = mp->b_cont;
2024 2024
2025 2025 tcp->tcp_rcv_last_tail = mp;
2026 2026 tcp->tcp_rcv_cnt += seg_len;
2027 2027 tcp->tcp_rwnd -= seg_len;
2028 2028 }
2029 2029
2030 2030 /* Generate an ACK-only (no data) segment for a TCP endpoint */
2031 2031 mblk_t *
2032 2032 tcp_ack_mp(tcp_t *tcp)
2033 2033 {
2034 2034 uint32_t seq_no;
2035 2035 tcp_stack_t *tcps = tcp->tcp_tcps;
2036 2036 conn_t *connp = tcp->tcp_connp;
2037 2037
2038 2038 /*
2039 2039 * There are a few cases to be considered while setting the sequence no.
2040 2040 * Essentially, we can come here while processing an unacceptable pkt
2041 2041 * in the TCPS_SYN_RCVD state, in which case we set the sequence number
2042 2042 * to snxt (per RFC 793), note the swnd wouldn't have been set yet.
2043 2043 * If we are here for a zero window probe, stick with suna. In all
2044 2044 * other cases, we check if suna + swnd encompasses snxt and set
2045 2045 * the sequence number to snxt, if so. If snxt falls outside the
2046 2046 * window (the receiver probably shrunk its window), we will go with
2047 2047 * suna + swnd, otherwise the sequence no will be unacceptable to the
2048 2048 * receiver.
2049 2049 */
2050 2050 if (tcp->tcp_zero_win_probe) {
2051 2051 seq_no = tcp->tcp_suna;
2052 2052 } else if (tcp->tcp_state == TCPS_SYN_RCVD) {
2053 2053 ASSERT(tcp->tcp_swnd == 0);
2054 2054 seq_no = tcp->tcp_snxt;
2055 2055 } else {
2056 2056 seq_no = SEQ_GT(tcp->tcp_snxt,
2057 2057 (tcp->tcp_suna + tcp->tcp_swnd)) ?
2058 2058 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt;
2059 2059 }
2060 2060
2061 2061 if (tcp->tcp_valid_bits) {
2062 2062 /*
2063 2063 * For the complex case where we have to send some
2064 2064 * controls (FIN or SYN), let tcp_xmit_mp do it.
2065 2065 */
2066 2066 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE,
2067 2067 NULL, B_FALSE));
2068 2068 } else {
2069 2069 /* Generate a simple ACK */
2070 2070 int data_length;
2071 2071 uchar_t *rptr;
2072 2072 tcpha_t *tcpha;
2073 2073 mblk_t *mp1;
2074 2074 int32_t total_hdr_len;
2075 2075 int32_t tcp_hdr_len;
2076 2076 int32_t num_sack_blk = 0;
2077 2077 int32_t sack_opt_len;
2078 2078 ip_xmit_attr_t *ixa = connp->conn_ixa;
2079 2079
2080 2080 /*
2081 2081 * Allocate space for TCP + IP headers
2082 2082 * and link-level header
2083 2083 */
2084 2084 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
2085 2085 num_sack_blk = MIN(tcp->tcp_max_sack_blk,
2086 2086 tcp->tcp_num_sack_blk);
2087 2087 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) +
2088 2088 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN;
2089 2089 total_hdr_len = connp->conn_ht_iphc_len + sack_opt_len;
2090 2090 tcp_hdr_len = connp->conn_ht_ulp_len + sack_opt_len;
2091 2091 } else {
2092 2092 total_hdr_len = connp->conn_ht_iphc_len;
2093 2093 tcp_hdr_len = connp->conn_ht_ulp_len;
2094 2094 }
2095 2095 mp1 = allocb(total_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED);
2096 2096 if (!mp1)
2097 2097 return (NULL);
2098 2098
2099 2099 /* Update the latest receive window size in TCP header. */
2100 2100 tcp->tcp_tcpha->tha_win =
2101 2101 htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
2102 2102 /* copy in prototype TCP + IP header */
2103 2103 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra;
2104 2104 mp1->b_rptr = rptr;
2105 2105 mp1->b_wptr = rptr + total_hdr_len;
2106 2106 bcopy(connp->conn_ht_iphc, rptr, connp->conn_ht_iphc_len);
2107 2107
2108 2108 tcpha = (tcpha_t *)&rptr[ixa->ixa_ip_hdr_length];
2109 2109
2110 2110 /* Set the TCP sequence number. */
2111 2111 tcpha->tha_seq = htonl(seq_no);
2112 2112
2113 2113 /* Set up the TCP flag field. */
2114 2114 tcpha->tha_flags = (uchar_t)TH_ACK;
2115 2115 if (tcp->tcp_ecn_echo_on)
2116 2116 tcpha->tha_flags |= TH_ECE;
2117 2117
2118 2118 tcp->tcp_rack = tcp->tcp_rnxt;
2119 2119 tcp->tcp_rack_cnt = 0;
2120 2120
2121 2121 /* fill in timestamp option if in use */
2122 2122 if (tcp->tcp_snd_ts_ok) {
2123 2123 uint32_t llbolt = (uint32_t)LBOLT_FASTPATH;
2124 2124
2125 2125 U32_TO_BE32(llbolt,
2126 2126 (char *)tcpha + TCP_MIN_HEADER_LENGTH+4);
2127 2127 U32_TO_BE32(tcp->tcp_ts_recent,
2128 2128 (char *)tcpha + TCP_MIN_HEADER_LENGTH+8);
2129 2129 }
2130 2130
2131 2131 /* Fill in SACK options */
2132 2132 if (num_sack_blk > 0) {
2133 2133 uchar_t *wptr = (uchar_t *)tcpha +
2134 2134 connp->conn_ht_ulp_len;
2135 2135 sack_blk_t *tmp;
2136 2136 int32_t i;
2137 2137
2138 2138 wptr[0] = TCPOPT_NOP;
2139 2139 wptr[1] = TCPOPT_NOP;
2140 2140 wptr[2] = TCPOPT_SACK;
2141 2141 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
2142 2142 sizeof (sack_blk_t);
2143 2143 wptr += TCPOPT_REAL_SACK_LEN;
2144 2144
2145 2145 tmp = tcp->tcp_sack_list;
2146 2146 for (i = 0; i < num_sack_blk; i++) {
2147 2147 U32_TO_BE32(tmp[i].begin, wptr);
2148 2148 wptr += sizeof (tcp_seq);
2149 2149 U32_TO_BE32(tmp[i].end, wptr);
2150 2150 wptr += sizeof (tcp_seq);
2151 2151 }
2152 2152 tcpha->tha_offset_and_reserved +=
2153 2153 ((num_sack_blk * 2 + 1) << 4);
2154 2154 }
2155 2155
2156 2156 ixa->ixa_pktlen = total_hdr_len;
2157 2157
2158 2158 if (ixa->ixa_flags & IXAF_IS_IPV4) {
2159 2159 ((ipha_t *)rptr)->ipha_length = htons(total_hdr_len);
2160 2160 } else {
2161 2161 ip6_t *ip6 = (ip6_t *)rptr;
2162 2162
2163 2163 ip6->ip6_plen = htons(total_hdr_len - IPV6_HDR_LEN);
2164 2164 }
2165 2165
2166 2166 /*
2167 2167 * Prime pump for checksum calculation in IP. Include the
2168 2168 * adjustment for a source route if any.
2169 2169 */
2170 2170 data_length = tcp_hdr_len + connp->conn_sum;
2171 2171 data_length = (data_length >> 16) + (data_length & 0xFFFF);
2172 2172 tcpha->tha_sum = htons(data_length);
2173 2173
2174 2174 if (tcp->tcp_ip_forward_progress) {
2175 2175 tcp->tcp_ip_forward_progress = B_FALSE;
2176 2176 connp->conn_ixa->ixa_flags |= IXAF_REACH_CONF;
2177 2177 } else {
2178 2178 connp->conn_ixa->ixa_flags &= ~IXAF_REACH_CONF;
2179 2179 }
2180 2180 return (mp1);
2181 2181 }
2182 2182 }
2183 2183
2184 2184 /*
2185 2185 * Dummy socket upcalls for if/when the conn_t gets detached from a
2186 2186 * direct-callback sonode via a user-driven close(). Easy to catch with
2187 2187 * DTrace FBT, and should be mostly harmless.
2188 2188 */
2189 2189
2190 2190 /* ARGSUSED */
2191 2191 static sock_upper_handle_t
2192 2192 tcp_dummy_newconn(sock_upper_handle_t x, sock_lower_handle_t y,
2193 2193 sock_downcalls_t *z, cred_t *cr, pid_t pid, sock_upcalls_t **ignored)
2194 2194 {
2195 2195 ASSERT(0); /* Panic in debug, otherwise ignore. */
2196 2196 return (NULL);
2197 2197 }
2198 2198
2199 2199 /* ARGSUSED */
2200 2200 static void
2201 2201 tcp_dummy_connected(sock_upper_handle_t x, sock_connid_t y, cred_t *cr,
2202 2202 pid_t pid)
2203 2203 {
2204 2204 ASSERT(x == NULL);
2205 2205 /* Normally we'd crhold(cr) and attach it to socket state. */
2206 2206 /* LINTED */
2207 2207 }
2208 2208
2209 2209 /* ARGSUSED */
2210 2210 static int
2211 2211 tcp_dummy_disconnected(sock_upper_handle_t x, sock_connid_t y, int blah)
2212 2212 {
2213 2213 ASSERT(0); /* Panic in debug, otherwise ignore. */
2214 2214 return (-1);
2215 2215 }
2216 2216
2217 2217 /* ARGSUSED */
2218 2218 static void
2219 2219 tcp_dummy_opctl(sock_upper_handle_t x, sock_opctl_action_t y, uintptr_t blah)
2220 2220 {
2221 2221 ASSERT(x == NULL);
2222 2222 /* We really want this one to be a harmless NOP for now. */
2223 2223 /* LINTED */
2224 2224 }
2225 2225
2226 2226 /* ARGSUSED */
2227 2227 static ssize_t
2228 2228 tcp_dummy_recv(sock_upper_handle_t x, mblk_t *mp, size_t len, int flags,
2229 2229 int *error, boolean_t *push)
2230 2230 {
2231 2231 ASSERT(x == NULL);
2232 2232
2233 2233 /*
2234 2234 * Consume the message, set ESHUTDOWN, and return an error.
2235 2235 * Nobody's home!
2236 2236 */
2237 2237 freemsg(mp);
2238 2238 *error = ESHUTDOWN;
2239 2239 return (-1);
2240 2240 }
2241 2241
2242 2242 /* ARGSUSED */
2243 2243 static void
2244 2244 tcp_dummy_set_proto_props(sock_upper_handle_t x, struct sock_proto_props *y)
2245 2245 {
2246 2246 ASSERT(0); /* Panic in debug, otherwise ignore. */
2247 2247 }
2248 2248
2249 2249 /* ARGSUSED */
2250 2250 static void
2251 2251 tcp_dummy_txq_full(sock_upper_handle_t x, boolean_t y)
2252 2252 {
2253 2253 ASSERT(0); /* Panic in debug, otherwise ignore. */
2254 2254 }
2255 2255
2256 2256 /* ARGSUSED */
2257 2257 static void
2258 2258 tcp_dummy_signal_oob(sock_upper_handle_t x, ssize_t len)
2259 2259 {
2260 2260 ASSERT(x == NULL);
2261 2261 /* Otherwise, this would signal socket state about OOB data. */
2262 2262 }
2263 2263
2264 2264 /* ARGSUSED */
2265 2265 static void
2266 2266 tcp_dummy_set_error(sock_upper_handle_t x, int err)
2267 2267 {
2268 2268 ASSERT(0); /* Panic in debug, otherwise ignore. */
2269 2269 }
2270 2270
2271 2271 /* ARGSUSED */
2272 2272 static void
2273 2273 tcp_dummy_onearg(sock_upper_handle_t x)
2274 2274 {
2275 2275 ASSERT(0); /* Panic in debug, otherwise ignore. */
2276 2276 }
2277 2277
2278 2278 static sock_upcalls_t tcp_dummy_upcalls = {
2279 2279 tcp_dummy_newconn,
2280 2280 tcp_dummy_connected,
2281 2281 tcp_dummy_disconnected,
2282 2282 tcp_dummy_opctl,
2283 2283 tcp_dummy_recv,
2284 2284 tcp_dummy_set_proto_props,
2285 2285 tcp_dummy_txq_full,
2286 2286 tcp_dummy_signal_oob,
2287 2287 tcp_dummy_onearg,
2288 2288 tcp_dummy_set_error,
2289 2289 tcp_dummy_onearg
2290 2290 };
2291 2291
2292 2292 /*
2293 2293 * Handle M_DATA messages from IP. Its called directly from IP via
2294 2294 * squeue for received IP packets.
2295 2295 *
2296 2296 * The first argument is always the connp/tcp to which the mp belongs.
2297 2297 * There are no exceptions to this rule. The caller has already put
2298 2298 * a reference on this connp/tcp and once tcp_input_data() returns,
2299 2299 * the squeue will do the refrele.
2300 2300 *
2301 2301 * The TH_SYN for the listener directly go to tcp_input_listener via
2302 2302 * squeue. ICMP errors go directly to tcp_icmp_input().
2303 2303 *
2304 2304 * sqp: NULL = recursive, sqp != NULL means called from squeue
2305 2305 */
2306 2306 void
2307 2307 tcp_input_data(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
2308 2308 {
2309 2309 int32_t bytes_acked;
2310 2310 int32_t gap;
2311 2311 mblk_t *mp1;
2312 2312 uint_t flags;
2313 2313 uint32_t new_swnd = 0;
2314 2314 uchar_t *iphdr;
2315 2315 uchar_t *rptr;
2316 2316 int32_t rgap;
2317 2317 uint32_t seg_ack;
2318 2318 int seg_len;
2319 2319 uint_t ip_hdr_len;
2320 2320 uint32_t seg_seq;
2321 2321 tcpha_t *tcpha;
2322 2322 int urp;
2323 2323 tcp_opt_t tcpopt;
2324 2324 ip_pkt_t ipp;
2325 2325 boolean_t ofo_seg = B_FALSE; /* Out of order segment */
2326 2326 uint32_t cwnd;
2327 2327 uint32_t add;
2328 2328 int npkt;
2329 2329 int mss;
2330 2330 conn_t *connp = (conn_t *)arg;
2331 2331 squeue_t *sqp = (squeue_t *)arg2;
2332 2332 tcp_t *tcp = connp->conn_tcp;
2333 2333 tcp_stack_t *tcps = tcp->tcp_tcps;
2334 2334 sock_upcalls_t *sockupcalls;
2335 2335
2336 2336 /*
2337 2337 * RST from fused tcp loopback peer should trigger an unfuse.
2338 2338 */
2339 2339 if (tcp->tcp_fused) {
2340 2340 TCP_STAT(tcps, tcp_fusion_aborted);
2341 2341 tcp_unfuse(tcp);
2342 2342 }
2343 2343
2344 2344 iphdr = mp->b_rptr;
2345 2345 rptr = mp->b_rptr;
2346 2346 ASSERT(OK_32PTR(rptr));
2347 2347
2348 2348 ip_hdr_len = ira->ira_ip_hdr_length;
2349 2349 if (connp->conn_recv_ancillary.crb_all != 0) {
2350 2350 /*
2351 2351 * Record packet information in the ip_pkt_t
2352 2352 */
2353 2353 ipp.ipp_fields = 0;
2354 2354 if (ira->ira_flags & IRAF_IS_IPV4) {
2355 2355 (void) ip_find_hdr_v4((ipha_t *)rptr, &ipp,
2356 2356 B_FALSE);
2357 2357 } else {
2358 2358 uint8_t nexthdrp;
2359 2359
2360 2360 /*
2361 2361 * IPv6 packets can only be received by applications
2362 2362 * that are prepared to receive IPv6 addresses.
2363 2363 * The IP fanout must ensure this.
2364 2364 */
2365 2365 ASSERT(connp->conn_family == AF_INET6);
2366 2366
2367 2367 (void) ip_find_hdr_v6(mp, (ip6_t *)rptr, B_TRUE, &ipp,
2368 2368 &nexthdrp);
2369 2369 ASSERT(nexthdrp == IPPROTO_TCP);
2370 2370
2371 2371 /* Could have caused a pullup? */
2372 2372 iphdr = mp->b_rptr;
2373 2373 rptr = mp->b_rptr;
2374 2374 }
2375 2375 }
2376 2376 ASSERT(DB_TYPE(mp) == M_DATA);
2377 2377 ASSERT(mp->b_next == NULL);
2378 2378
2379 2379 tcpha = (tcpha_t *)&rptr[ip_hdr_len];
2380 2380 seg_seq = ntohl(tcpha->tha_seq);
2381 2381 seg_ack = ntohl(tcpha->tha_ack);
2382 2382 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX);
2383 2383 seg_len = (int)(mp->b_wptr - rptr) -
2384 2384 (ip_hdr_len + TCP_HDR_LENGTH(tcpha));
2385 2385 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) {
2386 2386 do {
2387 2387 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <=
2388 2388 (uintptr_t)INT_MAX);
2389 2389 seg_len += (int)(mp1->b_wptr - mp1->b_rptr);
2390 2390 } while ((mp1 = mp1->b_cont) != NULL &&
2391 2391 mp1->b_datap->db_type == M_DATA);
2392 2392 }
2393 2393
2394 2394 DTRACE_TCP5(receive, mblk_t *, NULL, ip_xmit_attr_t *, connp->conn_ixa,
2395 2395 __dtrace_tcp_void_ip_t *, iphdr, tcp_t *, tcp,
2396 2396 __dtrace_tcp_tcph_t *, tcpha);
2397 2397
2398 2398 if (tcp->tcp_state == TCPS_TIME_WAIT) {
2399 2399 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack,
2400 2400 seg_len, tcpha, ira);
2401 2401 return;
2402 2402 }
2403 2403
2404 2404 if (sqp != NULL) {
2405 2405 /*
2406 2406 * This is the correct place to update tcp_last_recv_time. Note
2407 2407 * that it is also updated for tcp structure that belongs to
2408 2408 * global and listener queues which do not really need updating.
2409 2409 * But that should not cause any harm. And it is updated for
2410 2410 * all kinds of incoming segments, not only for data segments.
2411 2411 */
2412 2412 tcp->tcp_last_recv_time = LBOLT_FASTPATH;
2413 2413 }
2414 2414
2415 2415 flags = (unsigned int)tcpha->tha_flags & 0xFF;
2416 2416
2417 2417 BUMP_LOCAL(tcp->tcp_ibsegs);
2418 2418 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp);
2419 2419
2420 2420 if ((flags & TH_URG) && sqp != NULL) {
2421 2421 /*
2422 2422 * TCP can't handle urgent pointers that arrive before
2423 2423 * the connection has been accept()ed since it can't
2424 2424 * buffer OOB data. Discard segment if this happens.
2425 2425 *
2426 2426 * We can't just rely on a non-null tcp_listener to indicate
2427 2427 * that the accept() has completed since unlinking of the
2428 2428 * eager and completion of the accept are not atomic.
2429 2429 * tcp_detached, when it is not set (B_FALSE) indicates
2430 2430 * that the accept() has completed.
2431 2431 *
2432 2432 * Nor can it reassemble urgent pointers, so discard
2433 2433 * if it's not the next segment expected.
2434 2434 *
2435 2435 * Otherwise, collapse chain into one mblk (discard if
2436 2436 * that fails). This makes sure the headers, retransmitted
2437 2437 * data, and new data all are in the same mblk.
2438 2438 */
2439 2439 ASSERT(mp != NULL);
2440 2440 if (tcp->tcp_detached || !pullupmsg(mp, -1)) {
2441 2441 freemsg(mp);
2442 2442 return;
2443 2443 }
2444 2444 /* Update pointers into message */
2445 2445 iphdr = rptr = mp->b_rptr;
2446 2446 tcpha = (tcpha_t *)&rptr[ip_hdr_len];
2447 2447 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) {
2448 2448 /*
2449 2449 * Since we can't handle any data with this urgent
2450 2450 * pointer that is out of sequence, we expunge
2451 2451 * the data. This allows us to still register
2452 2452 * the urgent mark and generate the M_PCSIG,
2453 2453 * which we can do.
2454 2454 */
2455 2455 mp->b_wptr = (uchar_t *)tcpha + TCP_HDR_LENGTH(tcpha);
2456 2456 seg_len = 0;
2457 2457 }
2458 2458 }
2459 2459
2460 2460 sockupcalls = connp->conn_upcalls;
2461 2461 /* A conn_t may have belonged to a now-closed socket. Be careful. */
2462 2462 if (sockupcalls == NULL)
2463 2463 sockupcalls = &tcp_dummy_upcalls;
2464 2464
2465 2465 switch (tcp->tcp_state) {
2466 2466 case TCPS_SYN_SENT:
2467 2467 if (connp->conn_final_sqp == NULL &&
2468 2468 tcp_outbound_squeue_switch && sqp != NULL) {
2469 2469 ASSERT(connp->conn_initial_sqp == connp->conn_sqp);
2470 2470 connp->conn_final_sqp = sqp;
2471 2471 if (connp->conn_final_sqp != connp->conn_sqp) {
2472 2472 DTRACE_PROBE1(conn__final__sqp__switch,
2473 2473 conn_t *, connp);
2474 2474 CONN_INC_REF(connp);
2475 2475 SQUEUE_SWITCH(connp, connp->conn_final_sqp);
2476 2476 SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
2477 2477 tcp_input_data, connp, ira, ip_squeue_flag,
2478 2478 SQTAG_CONNECT_FINISH);
2479 2479 return;
2480 2480 }
2481 2481 DTRACE_PROBE1(conn__final__sqp__same, conn_t *, connp);
2482 2482 }
2483 2483 if (flags & TH_ACK) {
2484 2484 /*
2485 2485 * Note that our stack cannot send data before a
2486 2486 * connection is established, therefore the
2487 2487 * following check is valid. Otherwise, it has
2488 2488 * to be changed.
2489 2489 */
2490 2490 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) ||
2491 2491 SEQ_GT(seg_ack, tcp->tcp_snxt)) {
2492 2492 freemsg(mp);
2493 2493 if (flags & TH_RST)
2494 2494 return;
2495 2495 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq",
2496 2496 tcp, seg_ack, 0, TH_RST);
2497 2497 return;
2498 2498 }
2499 2499 ASSERT(tcp->tcp_suna + 1 == seg_ack);
2500 2500 }
2501 2501 if (flags & TH_RST) {
2502 2502 if (flags & TH_ACK) {
2503 2503 DTRACE_TCP5(connect__refused, mblk_t *, NULL,
2504 2504 ip_xmit_attr_t *, connp->conn_ixa,
2505 2505 void_ip_t *, iphdr, tcp_t *, tcp,
2506 2506 tcph_t *, tcpha);
2507 2507 (void) tcp_clean_death(tcp, ECONNREFUSED);
2508 2508 }
2509 2509 freemsg(mp);
2510 2510 return;
2511 2511 }
2512 2512 if (!(flags & TH_SYN)) {
2513 2513 freemsg(mp);
2514 2514 return;
2515 2515 }
2516 2516
2517 2517 /* Process all TCP options. */
2518 2518 tcp_process_options(tcp, tcpha);
2519 2519 /*
2520 2520 * The following changes our rwnd to be a multiple of the
2521 2521 * MIN(peer MSS, our MSS) for performance reason.
2522 2522 */
2523 2523 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(connp->conn_rcvbuf,
2524 2524 tcp->tcp_mss));
2525 2525
2526 2526 /* Is the other end ECN capable? */
2527 2527 if (tcp->tcp_ecn_ok) {
2528 2528 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) {
2529 2529 tcp->tcp_ecn_ok = B_FALSE;
2530 2530 }
2531 2531 }
2532 2532 /*
2533 2533 * Clear ECN flags because it may interfere with later
2534 2534 * processing.
2535 2535 */
2536 2536 flags &= ~(TH_ECE|TH_CWR);
2537 2537
2538 2538 tcp->tcp_irs = seg_seq;
2539 2539 tcp->tcp_rack = seg_seq;
2540 2540 tcp->tcp_rnxt = seg_seq + 1;
2541 2541 tcp->tcp_tcpha->tha_ack = htonl(tcp->tcp_rnxt);
2542 2542 if (!TCP_IS_DETACHED(tcp)) {
2543 2543 /* Allocate room for SACK options if needed. */
2544 2544 connp->conn_wroff = connp->conn_ht_iphc_len;
2545 2545 if (tcp->tcp_snd_sack_ok)
2546 2546 connp->conn_wroff += TCPOPT_MAX_SACK_LEN;
2547 2547 if (!tcp->tcp_loopback)
2548 2548 connp->conn_wroff += tcps->tcps_wroff_xtra;
2549 2549
2550 2550 (void) proto_set_tx_wroff(connp->conn_rq, connp,
2551 2551 connp->conn_wroff);
2552 2552 }
2553 2553 if (flags & TH_ACK) {
2554 2554 /*
2555 2555 * If we can't get the confirmation upstream, pretend
2556 2556 * we didn't even see this one.
2557 2557 *
2558 2558 * XXX: how can we pretend we didn't see it if we
2559 2559 * have updated rnxt et. al.
2560 2560 *
2561 2561 * For loopback we defer sending up the T_CONN_CON
2562 2562 * until after some checks below.
2563 2563 */
2564 2564 mp1 = NULL;
2565 2565 /*
2566 2566 * tcp_sendmsg() checks tcp_state without entering
2567 2567 * the squeue so tcp_state should be updated before
2568 2568 * sending up connection confirmation. Probe the
2569 2569 * state change below when we are sure the connection
2570 2570 * confirmation has been sent.
2571 2571 */
2572 2572 tcp->tcp_state = TCPS_ESTABLISHED;
2573 2573 if (!tcp_conn_con(tcp, iphdr, mp,
2574 2574 tcp->tcp_loopback ? &mp1 : NULL, ira)) {
2575 2575 tcp->tcp_state = TCPS_SYN_SENT;
2576 2576 freemsg(mp);
2577 2577 return;
2578 2578 }
2579 2579 TCPS_CONN_INC(tcps);
2580 2580 /* SYN was acked - making progress */
2581 2581 tcp->tcp_ip_forward_progress = B_TRUE;
2582 2582
2583 2583 /* One for the SYN */
2584 2584 tcp->tcp_suna = tcp->tcp_iss + 1;
2585 2585 tcp->tcp_valid_bits &= ~TCP_ISS_VALID;
2586 2586
2587 2587 /*
2588 2588 * If SYN was retransmitted, need to reset all
2589 2589 * retransmission info. This is because this
2590 2590 * segment will be treated as a dup ACK.
2591 2591 */
2592 2592 if (tcp->tcp_rexmit) {
2593 2593 tcp->tcp_rexmit = B_FALSE;
2594 2594 tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
2595 2595 tcp->tcp_rexmit_max = tcp->tcp_snxt;
2596 2596 tcp->tcp_ms_we_have_waited = 0;
2597 2597
2598 2598 /*
2599 2599 * Set tcp_cwnd back to 1 MSS, per
2600 2600 * recommendation from
2601 2601 * draft-floyd-incr-init-win-01.txt,
2602 2602 * Increasing TCP's Initial Window.
2603 2603 */
2604 2604 tcp->tcp_cwnd = tcp->tcp_mss;
2605 2605 }
2606 2606
2607 2607 tcp->tcp_swl1 = seg_seq;
2608 2608 tcp->tcp_swl2 = seg_ack;
2609 2609
2610 2610 new_swnd = ntohs(tcpha->tha_win);
2611 2611 tcp->tcp_swnd = new_swnd;
2612 2612 if (new_swnd > tcp->tcp_max_swnd)
2613 2613 tcp->tcp_max_swnd = new_swnd;
2614 2614
2615 2615 /*
2616 2616 * Always send the three-way handshake ack immediately
2617 2617 * in order to make the connection complete as soon as
2618 2618 * possible on the accepting host.
2619 2619 */
2620 2620 flags |= TH_ACK_NEEDED;
2621 2621
2622 2622 /*
2623 2623 * Trace connect-established here.
2624 2624 */
2625 2625 DTRACE_TCP5(connect__established, mblk_t *, NULL,
2626 2626 ip_xmit_attr_t *, tcp->tcp_connp->conn_ixa,
2627 2627 void_ip_t *, iphdr, tcp_t *, tcp, tcph_t *, tcpha);
2628 2628
2629 2629 /* Trace change from SYN_SENT -> ESTABLISHED here */
2630 2630 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
2631 2631 connp->conn_ixa, void, NULL, tcp_t *, tcp,
2632 2632 void, NULL, int32_t, TCPS_SYN_SENT);
2633 2633
2634 2634 /*
2635 2635 * Special case for loopback. At this point we have
2636 2636 * received SYN-ACK from the remote endpoint. In
2637 2637 * order to ensure that both endpoints reach the
2638 2638 * fused state prior to any data exchange, the final
2639 2639 * ACK needs to be sent before we indicate T_CONN_CON
2640 2640 * to the module upstream.
2641 2641 */
2642 2642 if (tcp->tcp_loopback) {
2643 2643 mblk_t *ack_mp;
2644 2644
2645 2645 ASSERT(!tcp->tcp_unfusable);
2646 2646 ASSERT(mp1 != NULL);
2647 2647 /*
2648 2648 * For loopback, we always get a pure SYN-ACK
2649 2649 * and only need to send back the final ACK
2650 2650 * with no data (this is because the other
2651 2651 * tcp is ours and we don't do T/TCP). This
2652 2652 * final ACK triggers the passive side to
2653 2653 * perform fusion in ESTABLISHED state.
2654 2654 */
2655 2655 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) {
2656 2656 if (tcp->tcp_ack_tid != 0) {
2657 2657 (void) TCP_TIMER_CANCEL(tcp,
2658 2658 tcp->tcp_ack_tid);
2659 2659 tcp->tcp_ack_tid = 0;
2660 2660 }
2661 2661 tcp_send_data(tcp, ack_mp);
2662 2662 BUMP_LOCAL(tcp->tcp_obsegs);
2663 2663 TCPS_BUMP_MIB(tcps, tcpOutAck);
2664 2664
2665 2665 if (!IPCL_IS_NONSTR(connp)) {
2666 2666 /* Send up T_CONN_CON */
2667 2667 if (ira->ira_cred != NULL) {
2668 2668 mblk_setcred(mp1,
2669 2669 ira->ira_cred,
2670 2670 ira->ira_cpid);
2671 2671 }
2672 2672 putnext(connp->conn_rq, mp1);
2673 2673 } else {
2674 2674 (*sockupcalls->su_connected)
2675 2675 (connp->conn_upper_handle,
2676 2676 tcp->tcp_connid,
2677 2677 ira->ira_cred,
2678 2678 ira->ira_cpid);
2679 2679 freemsg(mp1);
2680 2680 }
2681 2681
2682 2682 freemsg(mp);
2683 2683 return;
2684 2684 }
2685 2685 /*
2686 2686 * Forget fusion; we need to handle more
2687 2687 * complex cases below. Send the deferred
2688 2688 * T_CONN_CON message upstream and proceed
2689 2689 * as usual. Mark this tcp as not capable
2690 2690 * of fusion.
2691 2691 */
2692 2692 TCP_STAT(tcps, tcp_fusion_unfusable);
2693 2693 tcp->tcp_unfusable = B_TRUE;
2694 2694 if (!IPCL_IS_NONSTR(connp)) {
2695 2695 if (ira->ira_cred != NULL) {
2696 2696 mblk_setcred(mp1, ira->ira_cred,
2697 2697 ira->ira_cpid);
2698 2698 }
2699 2699 putnext(connp->conn_rq, mp1);
2700 2700 } else {
2701 2701 (*sockupcalls->su_connected)
2702 2702 (connp->conn_upper_handle,
2703 2703 tcp->tcp_connid, ira->ira_cred,
2704 2704 ira->ira_cpid);
2705 2705 freemsg(mp1);
2706 2706 }
2707 2707 }
2708 2708
2709 2709 /*
2710 2710 * Check to see if there is data to be sent. If
2711 2711 * yes, set the transmit flag. Then check to see
2712 2712 * if received data processing needs to be done.
2713 2713 * If not, go straight to xmit_check. This short
2714 2714 * cut is OK as we don't support T/TCP.
2715 2715 */
2716 2716 if (tcp->tcp_unsent)
2717 2717 flags |= TH_XMIT_NEEDED;
2718 2718
2719 2719 if (seg_len == 0 && !(flags & TH_URG)) {
2720 2720 freemsg(mp);
2721 2721 goto xmit_check;
2722 2722 }
2723 2723
2724 2724 flags &= ~TH_SYN;
2725 2725 seg_seq++;
2726 2726 break;
2727 2727 }
2728 2728 tcp->tcp_state = TCPS_SYN_RCVD;
2729 2729 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
2730 2730 connp->conn_ixa, void_ip_t *, NULL, tcp_t *, tcp,
2731 2731 tcph_t *, NULL, int32_t, TCPS_SYN_SENT);
2732 2732 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss,
2733 2733 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE);
2734 2734 if (mp1 != NULL) {
2735 2735 tcp_send_data(tcp, mp1);
2736 2736 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
2737 2737 }
2738 2738 freemsg(mp);
2739 2739 return;
2740 2740 case TCPS_SYN_RCVD:
2741 2741 if (flags & TH_ACK) {
2742 2742 uint32_t pinit_wnd;
2743 2743
2744 2744 /*
2745 2745 * In this state, a SYN|ACK packet is either bogus
2746 2746 * because the other side must be ACKing our SYN which
2747 2747 * indicates it has seen the ACK for their SYN and
2748 2748 * shouldn't retransmit it or we're crossing SYNs
2749 2749 * on active open.
2750 2750 */
2751 2751 if ((flags & TH_SYN) && !tcp->tcp_active_open) {
2752 2752 freemsg(mp);
2753 2753 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn",
2754 2754 tcp, seg_ack, 0, TH_RST);
2755 2755 return;
2756 2756 }
2757 2757 /*
2758 2758 * NOTE: RFC 793 pg. 72 says this should be
2759 2759 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt
2760 2760 * but that would mean we have an ack that ignored
2761 2761 * our SYN.
2762 2762 */
2763 2763 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) ||
2764 2764 SEQ_GT(seg_ack, tcp->tcp_snxt)) {
2765 2765 freemsg(mp);
2766 2766 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack",
2767 2767 tcp, seg_ack, 0, TH_RST);
2768 2768 return;
2769 2769 }
2770 2770 /*
2771 2771 * No sane TCP stack will send such a small window
2772 2772 * without receiving any data. Just drop this invalid
2773 2773 * ACK. We also shorten the abort timeout in case
2774 2774 * this is an attack.
2775 2775 */
2776 2776 pinit_wnd = ntohs(tcpha->tha_win) << tcp->tcp_snd_ws;
2777 2777 if (pinit_wnd < tcp->tcp_mss &&
2778 2778 pinit_wnd < tcp_init_wnd_chk) {
2779 2779 freemsg(mp);
2780 2780 TCP_STAT(tcps, tcp_zwin_ack_syn);
2781 2781 tcp->tcp_second_ctimer_threshold =
2782 2782 tcp_early_abort * SECONDS;
2783 2783 return;
2784 2784 }
2785 2785 }
2786 2786 break;
2787 2787 case TCPS_LISTEN:
2788 2788 /*
2789 2789 * Only a TLI listener can come through this path when a
2790 2790 * acceptor is going back to be a listener and a packet
2791 2791 * for the acceptor hits the classifier. For a socket
2792 2792 * listener, this can never happen because a listener
2793 2793 * can never accept connection on itself and hence a
2794 2794 * socket acceptor can not go back to being a listener.
2795 2795 */
2796 2796 ASSERT(!TCP_IS_SOCKET(tcp));
2797 2797 /*FALLTHRU*/
2798 2798 case TCPS_CLOSED:
2799 2799 case TCPS_BOUND: {
2800 2800 conn_t *new_connp;
2801 2801 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip;
2802 2802
2803 2803 /*
2804 2804 * Don't accept any input on a closed tcp as this TCP logically
2805 2805 * does not exist on the system. Don't proceed further with
2806 2806 * this TCP. For instance, this packet could trigger another
2807 2807 * close of this tcp which would be disastrous for tcp_refcnt.
2808 2808 * tcp_close_detached / tcp_clean_death / tcp_closei_local must
2809 2809 * be called at most once on a TCP. In this case we need to
2810 2810 * refeed the packet into the classifier and figure out where
2811 2811 * the packet should go.
2812 2812 */
2813 2813 new_connp = ipcl_classify(mp, ira, ipst);
2814 2814 if (new_connp != NULL) {
2815 2815 /* Drops ref on new_connp */
2816 2816 tcp_reinput(new_connp, mp, ira, ipst);
2817 2817 return;
2818 2818 }
2819 2819 /* We failed to classify. For now just drop the packet */
2820 2820 freemsg(mp);
2821 2821 return;
2822 2822 }
2823 2823 case TCPS_IDLE:
2824 2824 /*
2825 2825 * Handle the case where the tcp_clean_death() has happened
2826 2826 * on a connection (application hasn't closed yet) but a packet
2827 2827 * was already queued on squeue before tcp_clean_death()
2828 2828 * was processed. Calling tcp_clean_death() twice on same
2829 2829 * connection can result in weird behaviour.
2830 2830 */
2831 2831 freemsg(mp);
2832 2832 return;
2833 2833 default:
2834 2834 break;
2835 2835 }
2836 2836
2837 2837 /*
2838 2838 * Already on the correct queue/perimeter.
2839 2839 * If this is a detached connection and not an eager
2840 2840 * connection hanging off a listener then new data
2841 2841 * (past the FIN) will cause a reset.
2842 2842 * We do a special check here where it
2843 2843 * is out of the main line, rather than check
2844 2844 * if we are detached every time we see new
2845 2845 * data down below.
2846 2846 */
2847 2847 if (TCP_IS_DETACHED_NONEAGER(tcp) &&
2848 2848 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) {
2849 2849 TCPS_BUMP_MIB(tcps, tcpInClosed);
2850 2850 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp);
2851 2851 freemsg(mp);
2852 2852 tcp_xmit_ctl("new data when detached", tcp,
2853 2853 tcp->tcp_snxt, 0, TH_RST);
2854 2854 (void) tcp_clean_death(tcp, EPROTO);
2855 2855 return;
2856 2856 }
2857 2857
2858 2858 mp->b_rptr = (uchar_t *)tcpha + TCP_HDR_LENGTH(tcpha);
2859 2859 urp = ntohs(tcpha->tha_urp) - TCP_OLD_URP_INTERPRETATION;
2860 2860 new_swnd = ntohs(tcpha->tha_win) <<
2861 2861 ((tcpha->tha_flags & TH_SYN) ? 0 : tcp->tcp_snd_ws);
2862 2862
2863 2863 /*
2864 2864 * We are interested in two TCP options: timestamps (if negotiated) and
2865 2865 * SACK (if negotiated). Skip option parsing if neither is negotiated.
2866 2866 */
2867 2867 if (tcp->tcp_snd_ts_ok || tcp->tcp_snd_sack_ok) {
2868 2868 int options;
2869 2869 if (tcp->tcp_snd_sack_ok)
2870 2870 tcpopt.tcp = tcp;
2871 2871 else
2872 2872 tcpopt.tcp = NULL;
2873 2873 options = tcp_parse_options(tcpha, &tcpopt);
2874 2874 /*
2875 2875 * RST segments must not be subject to PAWS and are not
2876 2876 * required to have timestamps.
2877 2877 * We do not drop keepalive segments without
2878 2878 * timestamps, to maintain compatibility with legacy TCP stacks.
2879 2879 */
2880 2880 boolean_t keepalive = (seg_len == 0 || seg_len == 1) &&
2881 2881 (seg_seq + 1 == tcp->tcp_rnxt);
2882 2882 if (tcp->tcp_snd_ts_ok && !(flags & TH_RST) && !keepalive) {
2883 2883 /*
2884 2884 * Per RFC 7323 section 3.2., silently drop non-RST
2885 2885 * segments without expected TSopt. This is a 'SHOULD'
2886 2886 * requirement.
2887 2887 * We accept keepalives without TSopt to maintain
2888 2888 * interoperability with tcp implementations that omit
2889 2889 * the TSopt on these. Keepalive data is discarded, so
2890 2890 * there is no risk corrupting data by accepting these.
2891 2891 */
2892 2892 if (!(options & TCP_OPT_TSTAMP_PRESENT)) {
2893 2893 /*
2894 2894 * Leave a breadcrumb for people to detect this
2895 2895 * behavior.
2896 2896 */
2897 2897 DTRACE_TCP1(droppedtimestamp, tcp_t *, tcp);
2898 2898 freemsg(mp);
2899 2899 return;
2900 2900 }
2901 2901
2902 2902 if (!tcp_paws_check(tcp, &tcpopt)) {
2903 2903 /*
2904 2904 * This segment is not acceptable.
2905 2905 * Drop it and send back an ACK.
2906 2906 */
2907 2907 freemsg(mp);
2908 2908 flags |= TH_ACK_NEEDED;
2909 2909 goto ack_check;
2910 2910 }
2911 2911 }
2912 2912 }
2913 2913 try_again:;
2914 2914 mss = tcp->tcp_mss;
2915 2915 gap = seg_seq - tcp->tcp_rnxt;
2916 2916 rgap = tcp->tcp_rwnd - (gap + seg_len);
2917 2917 /*
2918 2918 * gap is the amount of sequence space between what we expect to see
2919 2919 * and what we got for seg_seq. A positive value for gap means
2920 2920 * something got lost. A negative value means we got some old stuff.
2921 2921 */
2922 2922 if (gap < 0) {
2923 2923 /* Old stuff present. Is the SYN in there? */
2924 2924 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) &&
2925 2925 (seg_len != 0)) {
2926 2926 flags &= ~TH_SYN;
2927 2927 seg_seq++;
2928 2928 urp--;
2929 2929 /* Recompute the gaps after noting the SYN. */
2930 2930 goto try_again;
2931 2931 }
2932 2932 TCPS_BUMP_MIB(tcps, tcpInDataDupSegs);
2933 2933 TCPS_UPDATE_MIB(tcps, tcpInDataDupBytes,
2934 2934 (seg_len > -gap ? -gap : seg_len));
2935 2935 /* Remove the old stuff from seg_len. */
2936 2936 seg_len += gap;
2937 2937 /*
2938 2938 * Anything left?
2939 2939 * Make sure to check for unack'd FIN when rest of data
2940 2940 * has been previously ack'd.
2941 2941 */
2942 2942 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) {
2943 2943 /*
2944 2944 * Resets are only valid if they lie within our offered
2945 2945 * window. If the RST bit is set, we just ignore this
2946 2946 * segment.
2947 2947 */
2948 2948 if (flags & TH_RST) {
2949 2949 freemsg(mp);
2950 2950 return;
2951 2951 }
2952 2952
2953 2953 /*
2954 2954 * The arriving of dup data packets indicate that we
2955 2955 * may have postponed an ack for too long, or the other
2956 2956 * side's RTT estimate is out of shape. Start acking
2957 2957 * more often.
2958 2958 */
2959 2959 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) &&
2960 2960 tcp->tcp_rack_cnt >= 1 &&
2961 2961 tcp->tcp_rack_abs_max > 2) {
2962 2962 tcp->tcp_rack_abs_max--;
2963 2963 }
2964 2964 tcp->tcp_rack_cur_max = 1;
2965 2965
2966 2966 /*
2967 2967 * This segment is "unacceptable". None of its
2968 2968 * sequence space lies within our advertized window.
2969 2969 *
2970 2970 * Adjust seg_len to the original value for tracing.
2971 2971 */
2972 2972 seg_len -= gap;
2973 2973 if (connp->conn_debug) {
2974 2974 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
2975 2975 "tcp_rput: unacceptable, gap %d, rgap %d, "
2976 2976 "flags 0x%x, seg_seq %u, seg_ack %u, "
2977 2977 "seg_len %d, rnxt %u, snxt %u, %s",
2978 2978 gap, rgap, flags, seg_seq, seg_ack,
2979 2979 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt,
2980 2980 tcp_display(tcp, NULL,
2981 2981 DISP_ADDR_AND_PORT));
2982 2982 }
2983 2983
2984 2984 /*
2985 2985 * Arrange to send an ACK in response to the
2986 2986 * unacceptable segment per RFC 793 page 69. There
2987 2987 * is only one small difference between ours and the
2988 2988 * acceptability test in the RFC - we accept ACK-only
2989 2989 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK
2990 2990 * will be generated.
2991 2991 *
2992 2992 * Note that we have to ACK an ACK-only packet at least
2993 2993 * for stacks that send 0-length keep-alives with
2994 2994 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122,
2995 2995 * section 4.2.3.6. As long as we don't ever generate
2996 2996 * an unacceptable packet in response to an incoming
2997 2997 * packet that is unacceptable, it should not cause
2998 2998 * "ACK wars".
2999 2999 */
3000 3000 flags |= TH_ACK_NEEDED;
3001 3001
3002 3002 /*
3003 3003 * Continue processing this segment in order to use the
3004 3004 * ACK information it contains, but skip all other
3005 3005 * sequence-number processing. Processing the ACK
3006 3006 * information is necessary in order to
3007 3007 * re-synchronize connections that may have lost
3008 3008 * synchronization.
3009 3009 *
3010 3010 * We clear seg_len and flag fields related to
3011 3011 * sequence number processing as they are not
3012 3012 * to be trusted for an unacceptable segment.
3013 3013 */
3014 3014 seg_len = 0;
3015 3015 flags &= ~(TH_SYN | TH_FIN | TH_URG);
3016 3016 goto process_ack;
3017 3017 }
3018 3018
3019 3019 /* Fix seg_seq, and chew the gap off the front. */
3020 3020 seg_seq = tcp->tcp_rnxt;
3021 3021 urp += gap;
3022 3022 do {
3023 3023 mblk_t *mp2;
3024 3024 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
3025 3025 (uintptr_t)UINT_MAX);
3026 3026 gap += (uint_t)(mp->b_wptr - mp->b_rptr);
3027 3027 if (gap > 0) {
3028 3028 mp->b_rptr = mp->b_wptr - gap;
3029 3029 break;
3030 3030 }
3031 3031 mp2 = mp;
3032 3032 mp = mp->b_cont;
3033 3033 freeb(mp2);
3034 3034 } while (gap < 0);
3035 3035 /*
3036 3036 * If the urgent data has already been acknowledged, we
3037 3037 * should ignore TH_URG below
3038 3038 */
3039 3039 if (urp < 0)
3040 3040 flags &= ~TH_URG;
3041 3041 }
3042 3042 /*
3043 3043 * rgap is the amount of stuff received out of window. A negative
3044 3044 * value is the amount out of window.
3045 3045 */
3046 3046 if (rgap < 0) {
3047 3047 mblk_t *mp2;
3048 3048
3049 3049 if (tcp->tcp_rwnd == 0) {
3050 3050 TCPS_BUMP_MIB(tcps, tcpInWinProbe);
3051 3051 } else {
3052 3052 TCPS_BUMP_MIB(tcps, tcpInDataPastWinSegs);
3053 3053 TCPS_UPDATE_MIB(tcps, tcpInDataPastWinBytes, -rgap);
3054 3054 }
3055 3055
3056 3056 /*
3057 3057 * seg_len does not include the FIN, so if more than
3058 3058 * just the FIN is out of window, we act like we don't
3059 3059 * see it. (If just the FIN is out of window, rgap
3060 3060 * will be zero and we will go ahead and acknowledge
3061 3061 * the FIN.)
3062 3062 */
3063 3063 flags &= ~TH_FIN;
3064 3064
3065 3065 /* Fix seg_len and make sure there is something left. */
3066 3066 seg_len += rgap;
3067 3067 if (seg_len <= 0) {
3068 3068 /*
3069 3069 * Resets are only valid if they lie within our offered
3070 3070 * window. If the RST bit is set, we just ignore this
3071 3071 * segment.
3072 3072 */
3073 3073 if (flags & TH_RST) {
3074 3074 freemsg(mp);
3075 3075 return;
3076 3076 }
3077 3077
3078 3078 /* Per RFC 793, we need to send back an ACK. */
3079 3079 flags |= TH_ACK_NEEDED;
3080 3080
3081 3081 /*
3082 3082 * Send SIGURG as soon as possible i.e. even
3083 3083 * if the TH_URG was delivered in a window probe
3084 3084 * packet (which will be unacceptable).
3085 3085 *
3086 3086 * We generate a signal if none has been generated
3087 3087 * for this connection or if this is a new urgent
3088 3088 * byte. Also send a zero-length "unmarked" message
3089 3089 * to inform SIOCATMARK that this is not the mark.
3090 3090 *
3091 3091 * tcp_urp_last_valid is cleared when the T_exdata_ind
3092 3092 * is sent up. This plus the check for old data
3093 3093 * (gap >= 0) handles the wraparound of the sequence
3094 3094 * number space without having to always track the
3095 3095 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks
3096 3096 * this max in its rcv_up variable).
3097 3097 *
3098 3098 * This prevents duplicate SIGURGS due to a "late"
3099 3099 * zero-window probe when the T_EXDATA_IND has already
3100 3100 * been sent up.
3101 3101 */
3102 3102 if ((flags & TH_URG) &&
3103 3103 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq,
3104 3104 tcp->tcp_urp_last))) {
3105 3105 if (IPCL_IS_NONSTR(connp)) {
3106 3106 if (!TCP_IS_DETACHED(tcp)) {
3107 3107 (*sockupcalls->su_signal_oob)
3108 3108 (connp->conn_upper_handle,
3109 3109 urp);
3110 3110 }
3111 3111 } else {
3112 3112 mp1 = allocb(0, BPRI_MED);
3113 3113 if (mp1 == NULL) {
3114 3114 freemsg(mp);
3115 3115 return;
3116 3116 }
3117 3117 if (!TCP_IS_DETACHED(tcp) &&
3118 3118 !putnextctl1(connp->conn_rq,
3119 3119 M_PCSIG, SIGURG)) {
3120 3120 /* Try again on the rexmit. */
3121 3121 freemsg(mp1);
3122 3122 freemsg(mp);
3123 3123 return;
3124 3124 }
3125 3125 /*
3126 3126 * If the next byte would be the mark
3127 3127 * then mark with MARKNEXT else mark
3128 3128 * with NOTMARKNEXT.
3129 3129 */
3130 3130 if (gap == 0 && urp == 0)
3131 3131 mp1->b_flag |= MSGMARKNEXT;
3132 3132 else
3133 3133 mp1->b_flag |= MSGNOTMARKNEXT;
3134 3134 freemsg(tcp->tcp_urp_mark_mp);
3135 3135 tcp->tcp_urp_mark_mp = mp1;
3136 3136 flags |= TH_SEND_URP_MARK;
3137 3137 }
3138 3138 tcp->tcp_urp_last_valid = B_TRUE;
3139 3139 tcp->tcp_urp_last = urp + seg_seq;
3140 3140 }
3141 3141 /*
3142 3142 * If this is a zero window probe, continue to
3143 3143 * process the ACK part. But we need to set seg_len
3144 3144 * to 0 to avoid data processing. Otherwise just
3145 3145 * drop the segment and send back an ACK.
3146 3146 */
3147 3147 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) {
3148 3148 flags &= ~(TH_SYN | TH_URG);
3149 3149 seg_len = 0;
3150 3150 goto process_ack;
3151 3151 } else {
3152 3152 freemsg(mp);
3153 3153 goto ack_check;
3154 3154 }
3155 3155 }
3156 3156 /* Pitch out of window stuff off the end. */
3157 3157 rgap = seg_len;
3158 3158 mp2 = mp;
3159 3159 do {
3160 3160 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <=
3161 3161 (uintptr_t)INT_MAX);
3162 3162 rgap -= (int)(mp2->b_wptr - mp2->b_rptr);
3163 3163 if (rgap < 0) {
3164 3164 mp2->b_wptr += rgap;
3165 3165 if ((mp1 = mp2->b_cont) != NULL) {
3166 3166 mp2->b_cont = NULL;
3167 3167 freemsg(mp1);
3168 3168 }
3169 3169 break;
3170 3170 }
3171 3171 } while ((mp2 = mp2->b_cont) != NULL);
3172 3172 }
3173 3173 ok:;
3174 3174 /*
3175 3175 * TCP should check ECN info for segments inside the window only.
3176 3176 * Therefore the check should be done here.
3177 3177 */
3178 3178 if (tcp->tcp_ecn_ok) {
3179 3179 if (flags & TH_CWR) {
3180 3180 tcp->tcp_ecn_echo_on = B_FALSE;
3181 3181 }
3182 3182 /*
3183 3183 * Note that both ECN_CE and CWR can be set in the
3184 3184 * same segment. In this case, we once again turn
3185 3185 * on ECN_ECHO.
3186 3186 */
3187 3187 if (connp->conn_ipversion == IPV4_VERSION) {
3188 3188 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service;
3189 3189
3190 3190 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) {
3191 3191 tcp->tcp_ecn_echo_on = B_TRUE;
3192 3192 }
3193 3193 } else {
3194 3194 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf;
3195 3195
3196 3196 if ((vcf & htonl(IPH_ECN_CE << 20)) ==
3197 3197 htonl(IPH_ECN_CE << 20)) {
3198 3198 tcp->tcp_ecn_echo_on = B_TRUE;
3199 3199 }
3200 3200 }
3201 3201 }
3202 3202
3203 3203 /*
3204 3204 * Check whether we can update tcp_ts_recent. This test is from RFC
3205 3205 * 7323, section 5.3.
3206 3206 */
3207 3207 if (tcp->tcp_snd_ts_ok && !(flags & TH_RST) &&
3208 3208 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) &&
3209 3209 SEQ_LEQ(seg_seq, tcp->tcp_rack)) {
3210 3210 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val;
3211 3211 tcp->tcp_last_rcv_lbolt = LBOLT_FASTPATH64;
3212 3212 }
3213 3213
3214 3214 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) {
3215 3215 /*
3216 3216 * FIN in an out of order segment. We record this in
3217 3217 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq.
3218 3218 * Clear the FIN so that any check on FIN flag will fail.
3219 3219 * Remember that FIN also counts in the sequence number
3220 3220 * space. So we need to ack out of order FIN only segments.
3221 3221 */
3222 3222 if (flags & TH_FIN) {
3223 3223 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID;
3224 3224 tcp->tcp_ofo_fin_seq = seg_seq + seg_len;
3225 3225 flags &= ~TH_FIN;
3226 3226 flags |= TH_ACK_NEEDED;
3227 3227 }
3228 3228 if (seg_len > 0) {
3229 3229 /* Fill in the SACK blk list. */
3230 3230 if (tcp->tcp_snd_sack_ok) {
3231 3231 tcp_sack_insert(tcp->tcp_sack_list,
3232 3232 seg_seq, seg_seq + seg_len,
3233 3233 &(tcp->tcp_num_sack_blk));
3234 3234 }
3235 3235
3236 3236 /*
3237 3237 * Attempt reassembly and see if we have something
3238 3238 * ready to go.
3239 3239 */
3240 3240 mp = tcp_reass(tcp, mp, seg_seq);
3241 3241 /* Always ack out of order packets */
3242 3242 flags |= TH_ACK_NEEDED | TH_PUSH;
3243 3243 if (mp) {
3244 3244 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
3245 3245 (uintptr_t)INT_MAX);
3246 3246 seg_len = mp->b_cont ? msgdsize(mp) :
3247 3247 (int)(mp->b_wptr - mp->b_rptr);
3248 3248 seg_seq = tcp->tcp_rnxt;
3249 3249 /*
3250 3250 * A gap is filled and the seq num and len
3251 3251 * of the gap match that of a previously
3252 3252 * received FIN, put the FIN flag back in.
3253 3253 */
3254 3254 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) &&
3255 3255 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) {
3256 3256 flags |= TH_FIN;
3257 3257 tcp->tcp_valid_bits &=
3258 3258 ~TCP_OFO_FIN_VALID;
3259 3259 }
3260 3260 if (tcp->tcp_reass_tid != 0) {
3261 3261 (void) TCP_TIMER_CANCEL(tcp,
3262 3262 tcp->tcp_reass_tid);
3263 3263 /*
3264 3264 * Restart the timer if there is still
3265 3265 * data in the reassembly queue.
3266 3266 */
3267 3267 if (tcp->tcp_reass_head != NULL) {
3268 3268 tcp->tcp_reass_tid = TCP_TIMER(
3269 3269 tcp, tcp_reass_timer,
3270 3270 tcps->tcps_reass_timeout);
3271 3271 } else {
3272 3272 tcp->tcp_reass_tid = 0;
3273 3273 }
3274 3274 }
3275 3275 } else {
3276 3276 /*
3277 3277 * Keep going even with NULL mp.
3278 3278 * There may be a useful ACK or something else
3279 3279 * we don't want to miss.
3280 3280 *
3281 3281 * But TCP should not perform fast retransmit
3282 3282 * because of the ack number. TCP uses
3283 3283 * seg_len == 0 to determine if it is a pure
3284 3284 * ACK. And this is not a pure ACK.
3285 3285 */
3286 3286 seg_len = 0;
3287 3287 ofo_seg = B_TRUE;
3288 3288
3289 3289 if (tcps->tcps_reass_timeout != 0 &&
3290 3290 tcp->tcp_reass_tid == 0) {
3291 3291 tcp->tcp_reass_tid = TCP_TIMER(tcp,
3292 3292 tcp_reass_timer,
3293 3293 tcps->tcps_reass_timeout);
3294 3294 }
3295 3295 }
3296 3296 }
3297 3297 } else if (seg_len > 0) {
3298 3298 TCPS_BUMP_MIB(tcps, tcpInDataInorderSegs);
3299 3299 TCPS_UPDATE_MIB(tcps, tcpInDataInorderBytes, seg_len);
3300 3300 /*
3301 3301 * If an out of order FIN was received before, and the seq
3302 3302 * num and len of the new segment match that of the FIN,
3303 3303 * put the FIN flag back in.
3304 3304 */
3305 3305 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) &&
3306 3306 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) {
3307 3307 flags |= TH_FIN;
3308 3308 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID;
3309 3309 }
3310 3310 }
3311 3311 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) {
3312 3312 if (flags & TH_RST) {
3313 3313 freemsg(mp);
3314 3314 switch (tcp->tcp_state) {
3315 3315 case TCPS_SYN_RCVD:
3316 3316 (void) tcp_clean_death(tcp, ECONNREFUSED);
3317 3317 break;
3318 3318 case TCPS_ESTABLISHED:
3319 3319 case TCPS_FIN_WAIT_1:
3320 3320 case TCPS_FIN_WAIT_2:
3321 3321 case TCPS_CLOSE_WAIT:
3322 3322 (void) tcp_clean_death(tcp, ECONNRESET);
3323 3323 break;
3324 3324 case TCPS_CLOSING:
3325 3325 case TCPS_LAST_ACK:
3326 3326 (void) tcp_clean_death(tcp, 0);
3327 3327 break;
3328 3328 default:
3329 3329 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT);
3330 3330 (void) tcp_clean_death(tcp, ENXIO);
3331 3331 break;
3332 3332 }
3333 3333 return;
3334 3334 }
3335 3335 if (flags & TH_SYN) {
3336 3336 /*
3337 3337 * See RFC 793, Page 71
3338 3338 *
3339 3339 * The seq number must be in the window as it should
3340 3340 * be "fixed" above. If it is outside window, it should
3341 3341 * be already rejected. Note that we allow seg_seq to be
3342 3342 * rnxt + rwnd because we want to accept 0 window probe.
3343 3343 */
3344 3344 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) &&
3345 3345 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd));
3346 3346 freemsg(mp);
3347 3347 /*
3348 3348 * If the ACK flag is not set, just use our snxt as the
3349 3349 * seq number of the RST segment.
3350 3350 */
3351 3351 if (!(flags & TH_ACK)) {
3352 3352 seg_ack = tcp->tcp_snxt;
3353 3353 }
3354 3354 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1,
↓ open down ↓ |
3175 lines elided |
↑ open up ↑ |
3355 3355 TH_RST|TH_ACK);
3356 3356 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT);
3357 3357 (void) tcp_clean_death(tcp, ECONNRESET);
3358 3358 return;
3359 3359 }
3360 3360 /*
3361 3361 * urp could be -1 when the urp field in the packet is 0
3362 3362 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent
3363 3363 * byte was at seg_seq - 1, in which case we ignore the urgent flag.
3364 3364 */
3365 - if (flags & TH_URG && urp >= 0) {
3365 + if ((flags & TH_URG) && urp >= 0) {
3366 3366 if (!tcp->tcp_urp_last_valid ||
3367 3367 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) {
3368 3368 /*
3369 3369 * Non-STREAMS sockets handle the urgent data a litte
3370 3370 * differently from STREAMS based sockets. There is no
3371 3371 * need to mark any mblks with the MSG{NOT,}MARKNEXT
3372 3372 * flags to keep SIOCATMARK happy. Instead a
3373 3373 * su_signal_oob upcall is made to update the mark.
3374 3374 * Neither is a T_EXDATA_IND mblk needed to be
3375 3375 * prepended to the urgent data. The urgent data is
3376 3376 * delivered using the su_recv upcall, where we set
3377 3377 * the MSG_OOB flag to indicate that it is urg data.
3378 3378 *
3379 3379 * Neither TH_SEND_URP_MARK nor TH_MARKNEXT_NEEDED
3380 3380 * are used by non-STREAMS sockets.
3381 3381 */
3382 3382 if (IPCL_IS_NONSTR(connp)) {
3383 3383 if (!TCP_IS_DETACHED(tcp)) {
3384 3384 (*sockupcalls->su_signal_oob)
3385 3385 (connp->conn_upper_handle, urp);
3386 3386 }
3387 3387 } else {
3388 3388 /*
3389 3389 * If we haven't generated the signal yet for
3390 3390 * this urgent pointer value, do it now. Also,
3391 3391 * send up a zero-length M_DATA indicating
3392 3392 * whether or not this is the mark. The latter
3393 3393 * is not needed when a T_EXDATA_IND is sent up.
3394 3394 * However, if there are allocation failures
3395 3395 * this code relies on the sender retransmitting
3396 3396 * and the socket code for determining the mark
3397 3397 * should not block waiting for the peer to
3398 3398 * transmit. Thus, for simplicity we always
3399 3399 * send up the mark indication.
3400 3400 */
3401 3401 mp1 = allocb(0, BPRI_MED);
3402 3402 if (mp1 == NULL) {
3403 3403 freemsg(mp);
3404 3404 return;
3405 3405 }
3406 3406 if (!TCP_IS_DETACHED(tcp) &&
3407 3407 !putnextctl1(connp->conn_rq, M_PCSIG,
3408 3408 SIGURG)) {
3409 3409 /* Try again on the rexmit. */
3410 3410 freemsg(mp1);
3411 3411 freemsg(mp);
3412 3412 return;
3413 3413 }
3414 3414 /*
3415 3415 * Mark with NOTMARKNEXT for now.
3416 3416 * The code below will change this to MARKNEXT
3417 3417 * if we are at the mark.
3418 3418 *
3419 3419 * If there are allocation failures (e.g. in
3420 3420 * dupmsg below) the next time tcp_input_data
3421 3421 * sees the urgent segment it will send up the
3422 3422 * MSGMARKNEXT message.
3423 3423 */
3424 3424 mp1->b_flag |= MSGNOTMARKNEXT;
3425 3425 freemsg(tcp->tcp_urp_mark_mp);
3426 3426 tcp->tcp_urp_mark_mp = mp1;
3427 3427 flags |= TH_SEND_URP_MARK;
3428 3428 #ifdef DEBUG
3429 3429 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
3430 3430 "tcp_rput: sent M_PCSIG 2 seq %x urp %x "
3431 3431 "last %x, %s",
3432 3432 seg_seq, urp, tcp->tcp_urp_last,
3433 3433 tcp_display(tcp, NULL, DISP_PORT_ONLY));
3434 3434 #endif /* DEBUG */
3435 3435 }
3436 3436 tcp->tcp_urp_last_valid = B_TRUE;
3437 3437 tcp->tcp_urp_last = urp + seg_seq;
3438 3438 } else if (tcp->tcp_urp_mark_mp != NULL) {
3439 3439 /*
3440 3440 * An allocation failure prevented the previous
3441 3441 * tcp_input_data from sending up the allocated
3442 3442 * MSG*MARKNEXT message - send it up this time
3443 3443 * around.
3444 3444 */
3445 3445 flags |= TH_SEND_URP_MARK;
3446 3446 }
3447 3447
3448 3448 /*
3449 3449 * If the urgent byte is in this segment, make sure that it is
3450 3450 * all by itself. This makes it much easier to deal with the
3451 3451 * possibility of an allocation failure on the T_exdata_ind.
3452 3452 * Note that seg_len is the number of bytes in the segment, and
3453 3453 * urp is the offset into the segment of the urgent byte.
3454 3454 * urp < seg_len means that the urgent byte is in this segment.
3455 3455 */
3456 3456 if (urp < seg_len) {
3457 3457 if (seg_len != 1) {
3458 3458 uint32_t tmp_rnxt;
3459 3459 /*
3460 3460 * Break it up and feed it back in.
3461 3461 * Re-attach the IP header.
3462 3462 */
3463 3463 mp->b_rptr = iphdr;
3464 3464 if (urp > 0) {
3465 3465 /*
3466 3466 * There is stuff before the urgent
3467 3467 * byte.
3468 3468 */
3469 3469 mp1 = dupmsg(mp);
3470 3470 if (!mp1) {
3471 3471 /*
3472 3472 * Trim from urgent byte on.
3473 3473 * The rest will come back.
3474 3474 */
3475 3475 (void) adjmsg(mp,
3476 3476 urp - seg_len);
3477 3477 tcp_input_data(connp,
3478 3478 mp, NULL, ira);
3479 3479 return;
3480 3480 }
3481 3481 (void) adjmsg(mp1, urp - seg_len);
3482 3482 /* Feed this piece back in. */
3483 3483 tmp_rnxt = tcp->tcp_rnxt;
3484 3484 tcp_input_data(connp, mp1, NULL, ira);
3485 3485 /*
3486 3486 * If the data passed back in was not
3487 3487 * processed (ie: bad ACK) sending
3488 3488 * the remainder back in will cause a
3489 3489 * loop. In this case, drop the
3490 3490 * packet and let the sender try
3491 3491 * sending a good packet.
3492 3492 */
3493 3493 if (tmp_rnxt == tcp->tcp_rnxt) {
3494 3494 freemsg(mp);
3495 3495 return;
3496 3496 }
3497 3497 }
3498 3498 if (urp != seg_len - 1) {
3499 3499 uint32_t tmp_rnxt;
3500 3500 /*
3501 3501 * There is stuff after the urgent
3502 3502 * byte.
3503 3503 */
3504 3504 mp1 = dupmsg(mp);
3505 3505 if (!mp1) {
3506 3506 /*
3507 3507 * Trim everything beyond the
3508 3508 * urgent byte. The rest will
3509 3509 * come back.
3510 3510 */
3511 3511 (void) adjmsg(mp,
3512 3512 urp + 1 - seg_len);
3513 3513 tcp_input_data(connp,
3514 3514 mp, NULL, ira);
3515 3515 return;
3516 3516 }
3517 3517 (void) adjmsg(mp1, urp + 1 - seg_len);
3518 3518 tmp_rnxt = tcp->tcp_rnxt;
3519 3519 tcp_input_data(connp, mp1, NULL, ira);
3520 3520 /*
3521 3521 * If the data passed back in was not
3522 3522 * processed (ie: bad ACK) sending
3523 3523 * the remainder back in will cause a
3524 3524 * loop. In this case, drop the
3525 3525 * packet and let the sender try
3526 3526 * sending a good packet.
3527 3527 */
3528 3528 if (tmp_rnxt == tcp->tcp_rnxt) {
3529 3529 freemsg(mp);
3530 3530 return;
3531 3531 }
3532 3532 }
3533 3533 tcp_input_data(connp, mp, NULL, ira);
3534 3534 return;
3535 3535 }
3536 3536 /*
3537 3537 * This segment contains only the urgent byte. We
3538 3538 * have to allocate the T_exdata_ind, if we can.
3539 3539 */
3540 3540 if (IPCL_IS_NONSTR(connp)) {
3541 3541 int error;
3542 3542
3543 3543 (*sockupcalls->su_recv)
3544 3544 (connp->conn_upper_handle, mp, seg_len,
3545 3545 MSG_OOB, &error, NULL);
3546 3546 /*
3547 3547 * We should never be in middle of a
3548 3548 * fallback, the squeue guarantees that.
3549 3549 */
3550 3550 ASSERT(error != EOPNOTSUPP);
3551 3551 mp = NULL;
3552 3552 goto update_ack;
3553 3553 } else if (!tcp->tcp_urp_mp) {
3554 3554 struct T_exdata_ind *tei;
3555 3555 mp1 = allocb(sizeof (struct T_exdata_ind),
3556 3556 BPRI_MED);
3557 3557 if (!mp1) {
3558 3558 /*
3559 3559 * Sigh... It'll be back.
3560 3560 * Generate any MSG*MARK message now.
3561 3561 */
3562 3562 freemsg(mp);
3563 3563 seg_len = 0;
3564 3564 if (flags & TH_SEND_URP_MARK) {
3565 3565
3566 3566
3567 3567 ASSERT(tcp->tcp_urp_mark_mp);
3568 3568 tcp->tcp_urp_mark_mp->b_flag &=
3569 3569 ~MSGNOTMARKNEXT;
3570 3570 tcp->tcp_urp_mark_mp->b_flag |=
3571 3571 MSGMARKNEXT;
3572 3572 }
3573 3573 goto ack_check;
3574 3574 }
3575 3575 mp1->b_datap->db_type = M_PROTO;
3576 3576 tei = (struct T_exdata_ind *)mp1->b_rptr;
3577 3577 tei->PRIM_type = T_EXDATA_IND;
3578 3578 tei->MORE_flag = 0;
3579 3579 mp1->b_wptr = (uchar_t *)&tei[1];
3580 3580 tcp->tcp_urp_mp = mp1;
3581 3581 #ifdef DEBUG
3582 3582 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
3583 3583 "tcp_rput: allocated exdata_ind %s",
3584 3584 tcp_display(tcp, NULL,
3585 3585 DISP_PORT_ONLY));
3586 3586 #endif /* DEBUG */
3587 3587 /*
3588 3588 * There is no need to send a separate MSG*MARK
3589 3589 * message since the T_EXDATA_IND will be sent
3590 3590 * now.
3591 3591 */
3592 3592 flags &= ~TH_SEND_URP_MARK;
3593 3593 freemsg(tcp->tcp_urp_mark_mp);
3594 3594 tcp->tcp_urp_mark_mp = NULL;
3595 3595 }
3596 3596 /*
3597 3597 * Now we are all set. On the next putnext upstream,
3598 3598 * tcp_urp_mp will be non-NULL and will get prepended
3599 3599 * to what has to be this piece containing the urgent
3600 3600 * byte. If for any reason we abort this segment below,
3601 3601 * if it comes back, we will have this ready, or it
3602 3602 * will get blown off in close.
3603 3603 */
3604 3604 } else if (urp == seg_len) {
3605 3605 /*
3606 3606 * The urgent byte is the next byte after this sequence
3607 3607 * number. If this endpoint is non-STREAMS, then there
3608 3608 * is nothing to do here since the socket has already
3609 3609 * been notified about the urg pointer by the
3610 3610 * su_signal_oob call above.
3611 3611 *
3612 3612 * In case of STREAMS, some more work might be needed.
3613 3613 * If there is data it is marked with MSGMARKNEXT and
3614 3614 * and any tcp_urp_mark_mp is discarded since it is not
3615 3615 * needed. Otherwise, if the code above just allocated
3616 3616 * a zero-length tcp_urp_mark_mp message, that message
3617 3617 * is tagged with MSGMARKNEXT. Sending up these
3618 3618 * MSGMARKNEXT messages makes SIOCATMARK work correctly
3619 3619 * even though the T_EXDATA_IND will not be sent up
3620 3620 * until the urgent byte arrives.
3621 3621 */
3622 3622 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) {
3623 3623 if (seg_len != 0) {
3624 3624 flags |= TH_MARKNEXT_NEEDED;
3625 3625 freemsg(tcp->tcp_urp_mark_mp);
3626 3626 tcp->tcp_urp_mark_mp = NULL;
3627 3627 flags &= ~TH_SEND_URP_MARK;
3628 3628 } else if (tcp->tcp_urp_mark_mp != NULL) {
3629 3629 flags |= TH_SEND_URP_MARK;
3630 3630 tcp->tcp_urp_mark_mp->b_flag &=
3631 3631 ~MSGNOTMARKNEXT;
3632 3632 tcp->tcp_urp_mark_mp->b_flag |=
3633 3633 MSGMARKNEXT;
3634 3634 }
3635 3635 }
3636 3636 #ifdef DEBUG
3637 3637 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
3638 3638 "tcp_rput: AT MARK, len %d, flags 0x%x, %s",
3639 3639 seg_len, flags,
3640 3640 tcp_display(tcp, NULL, DISP_PORT_ONLY));
3641 3641 #endif /* DEBUG */
3642 3642 }
3643 3643 #ifdef DEBUG
3644 3644 else {
3645 3645 /* Data left until we hit mark */
3646 3646 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
3647 3647 "tcp_rput: URP %d bytes left, %s",
3648 3648 urp - seg_len, tcp_display(tcp, NULL,
3649 3649 DISP_PORT_ONLY));
3650 3650 }
3651 3651 #endif /* DEBUG */
3652 3652 }
3653 3653
3654 3654 process_ack:
3655 3655 if (!(flags & TH_ACK)) {
3656 3656 freemsg(mp);
3657 3657 goto xmit_check;
3658 3658 }
3659 3659 }
3660 3660 bytes_acked = (int)(seg_ack - tcp->tcp_suna);
3661 3661
3662 3662 if (bytes_acked > 0)
3663 3663 tcp->tcp_ip_forward_progress = B_TRUE;
3664 3664 if (tcp->tcp_state == TCPS_SYN_RCVD) {
3665 3665 /*
3666 3666 * tcp_sendmsg() checks tcp_state without entering
3667 3667 * the squeue so tcp_state should be updated before
3668 3668 * sending up a connection confirmation or a new
3669 3669 * connection indication.
3670 3670 */
3671 3671 tcp->tcp_state = TCPS_ESTABLISHED;
3672 3672
3673 3673 /*
3674 3674 * We are seeing the final ack in the three way
3675 3675 * hand shake of a active open'ed connection
3676 3676 * so we must send up a T_CONN_CON
3677 3677 */
3678 3678 if (tcp->tcp_active_open) {
3679 3679 if (!tcp_conn_con(tcp, iphdr, mp, NULL, ira)) {
3680 3680 freemsg(mp);
3681 3681 tcp->tcp_state = TCPS_SYN_RCVD;
3682 3682 return;
3683 3683 }
3684 3684 /*
3685 3685 * Don't fuse the loopback endpoints for
3686 3686 * simultaneous active opens.
3687 3687 */
3688 3688 if (tcp->tcp_loopback) {
3689 3689 TCP_STAT(tcps, tcp_fusion_unfusable);
3690 3690 tcp->tcp_unfusable = B_TRUE;
3691 3691 }
3692 3692 /*
3693 3693 * For simultaneous active open, trace receipt of final
3694 3694 * ACK as tcp:::connect-established.
3695 3695 */
3696 3696 DTRACE_TCP5(connect__established, mblk_t *, NULL,
3697 3697 ip_xmit_attr_t *, connp->conn_ixa, void_ip_t *,
3698 3698 iphdr, tcp_t *, tcp, tcph_t *, tcpha);
3699 3699 } else if (IPCL_IS_NONSTR(connp)) {
3700 3700 /*
3701 3701 * 3-way handshake has completed, so notify socket
3702 3702 * of the new connection.
3703 3703 *
3704 3704 * We are here means eager is fine but it can
3705 3705 * get a TH_RST at any point between now and till
3706 3706 * accept completes and disappear. We need to
3707 3707 * ensure that reference to eager is valid after
3708 3708 * we get out of eager's perimeter. So we do
3709 3709 * an extra refhold.
3710 3710 */
3711 3711 CONN_INC_REF(connp);
3712 3712
3713 3713 if (!tcp_newconn_notify(tcp, ira)) {
3714 3714 /*
3715 3715 * The state-change probe for SYN_RCVD ->
3716 3716 * ESTABLISHED has not fired yet. We reset
3717 3717 * the state to SYN_RCVD so that future
3718 3718 * state-change probes report correct state
3719 3719 * transistions.
3720 3720 */
3721 3721 tcp->tcp_state = TCPS_SYN_RCVD;
3722 3722 freemsg(mp);
3723 3723 /* notification did not go up, so drop ref */
3724 3724 CONN_DEC_REF(connp);
3725 3725 /* ... and close the eager */
3726 3726 ASSERT(TCP_IS_DETACHED(tcp));
3727 3727 (void) tcp_close_detached(tcp);
3728 3728 return;
3729 3729 }
3730 3730 /*
3731 3731 * tcp_newconn_notify() changes conn_upcalls and
3732 3732 * connp->conn_upper_handle. Fix things now, in case
3733 3733 * there's data attached to this ack.
3734 3734 */
3735 3735 if (connp->conn_upcalls != NULL)
3736 3736 sockupcalls = connp->conn_upcalls;
3737 3737 /*
3738 3738 * For passive open, trace receipt of final ACK as
3739 3739 * tcp:::accept-established.
3740 3740 */
3741 3741 DTRACE_TCP5(accept__established, mlbk_t *, NULL,
3742 3742 ip_xmit_attr_t *, connp->conn_ixa, void_ip_t *,
3743 3743 iphdr, tcp_t *, tcp, tcph_t *, tcpha);
3744 3744 } else {
3745 3745 /*
3746 3746 * 3-way handshake complete - this is a STREAMS based
3747 3747 * socket, so pass up the T_CONN_IND.
3748 3748 */
3749 3749 tcp_t *listener = tcp->tcp_listener;
3750 3750 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind;
3751 3751
3752 3752 tcp->tcp_tconnind_started = B_TRUE;
3753 3753 tcp->tcp_conn.tcp_eager_conn_ind = NULL;
3754 3754 ASSERT(mp != NULL);
3755 3755 /*
3756 3756 * We are here means eager is fine but it can
3757 3757 * get a TH_RST at any point between now and till
3758 3758 * accept completes and disappear. We need to
3759 3759 * ensure that reference to eager is valid after
3760 3760 * we get out of eager's perimeter. So we do
3761 3761 * an extra refhold.
3762 3762 */
3763 3763 CONN_INC_REF(connp);
3764 3764
3765 3765 /*
3766 3766 * The listener also exists because of the refhold
3767 3767 * done in tcp_input_listener. Its possible that it
3768 3768 * might have closed. We will check that once we
3769 3769 * get inside listeners context.
3770 3770 */
3771 3771 CONN_INC_REF(listener->tcp_connp);
3772 3772 if (listener->tcp_connp->conn_sqp ==
3773 3773 connp->conn_sqp) {
3774 3774 /*
3775 3775 * We optimize by not calling an SQUEUE_ENTER
3776 3776 * on the listener since we know that the
3777 3777 * listener and eager squeues are the same.
3778 3778 * We are able to make this check safely only
3779 3779 * because neither the eager nor the listener
3780 3780 * can change its squeue. Only an active connect
3781 3781 * can change its squeue
3782 3782 */
3783 3783 tcp_send_conn_ind(listener->tcp_connp, mp,
3784 3784 listener->tcp_connp->conn_sqp);
3785 3785 CONN_DEC_REF(listener->tcp_connp);
3786 3786 } else if (!tcp->tcp_loopback) {
3787 3787 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp,
3788 3788 mp, tcp_send_conn_ind,
3789 3789 listener->tcp_connp, NULL, SQ_FILL,
3790 3790 SQTAG_TCP_CONN_IND);
3791 3791 } else {
3792 3792 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp,
3793 3793 mp, tcp_send_conn_ind,
3794 3794 listener->tcp_connp, NULL, SQ_NODRAIN,
3795 3795 SQTAG_TCP_CONN_IND);
3796 3796 }
3797 3797 /*
3798 3798 * For passive open, trace receipt of final ACK as
3799 3799 * tcp:::accept-established.
3800 3800 */
3801 3801 DTRACE_TCP5(accept__established, mlbk_t *, NULL,
3802 3802 ip_xmit_attr_t *, connp->conn_ixa, void_ip_t *,
3803 3803 iphdr, tcp_t *, tcp, tcph_t *, tcpha);
3804 3804 }
3805 3805 TCPS_CONN_INC(tcps);
3806 3806
3807 3807 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */
3808 3808 bytes_acked--;
3809 3809 /* SYN was acked - making progress */
3810 3810 tcp->tcp_ip_forward_progress = B_TRUE;
3811 3811
3812 3812 /*
3813 3813 * If SYN was retransmitted, need to reset all
3814 3814 * retransmission info as this segment will be
3815 3815 * treated as a dup ACK.
3816 3816 */
3817 3817 if (tcp->tcp_rexmit) {
3818 3818 tcp->tcp_rexmit = B_FALSE;
3819 3819 tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
3820 3820 tcp->tcp_rexmit_max = tcp->tcp_snxt;
3821 3821 tcp->tcp_ms_we_have_waited = 0;
3822 3822 tcp->tcp_cwnd = mss;
3823 3823 }
3824 3824
3825 3825 /*
3826 3826 * We set the send window to zero here.
3827 3827 * This is needed if there is data to be
3828 3828 * processed already on the queue.
3829 3829 * Later (at swnd_update label), the
3830 3830 * "new_swnd > tcp_swnd" condition is satisfied
3831 3831 * the XMIT_NEEDED flag is set in the current
3832 3832 * (SYN_RCVD) state. This ensures tcp_wput_data() is
3833 3833 * called if there is already data on queue in
3834 3834 * this state.
3835 3835 */
3836 3836 tcp->tcp_swnd = 0;
3837 3837
3838 3838 if (new_swnd > tcp->tcp_max_swnd)
3839 3839 tcp->tcp_max_swnd = new_swnd;
3840 3840 tcp->tcp_swl1 = seg_seq;
3841 3841 tcp->tcp_swl2 = seg_ack;
3842 3842 tcp->tcp_valid_bits &= ~TCP_ISS_VALID;
3843 3843
3844 3844 /* Trace change from SYN_RCVD -> ESTABLISHED here */
3845 3845 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
3846 3846 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
3847 3847 int32_t, TCPS_SYN_RCVD);
3848 3848
3849 3849 /* Fuse when both sides are in ESTABLISHED state */
3850 3850 if (tcp->tcp_loopback && do_tcp_fusion)
3851 3851 tcp_fuse(tcp, iphdr, tcpha);
3852 3852
3853 3853 }
3854 3854 /* This code follows 4.4BSD-Lite2 mostly. */
3855 3855 if (bytes_acked < 0)
3856 3856 goto est;
3857 3857
3858 3858 /*
3859 3859 * If TCP is ECN capable and the congestion experience bit is
3860 3860 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be
3861 3861 * done once per window (or more loosely, per RTT).
3862 3862 */
3863 3863 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max))
3864 3864 tcp->tcp_cwr = B_FALSE;
3865 3865 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) {
3866 3866 if (!tcp->tcp_cwr) {
3867 3867 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss;
3868 3868 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss;
3869 3869 tcp->tcp_cwnd = npkt * mss;
3870 3870 /*
3871 3871 * If the cwnd is 0, use the timer to clock out
3872 3872 * new segments. This is required by the ECN spec.
3873 3873 */
3874 3874 if (npkt == 0) {
3875 3875 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
3876 3876 /*
3877 3877 * This makes sure that when the ACK comes
3878 3878 * back, we will increase tcp_cwnd by 1 MSS.
3879 3879 */
3880 3880 tcp->tcp_cwnd_cnt = 0;
3881 3881 }
3882 3882 tcp->tcp_cwr = B_TRUE;
3883 3883 /*
3884 3884 * This marks the end of the current window of in
3885 3885 * flight data. That is why we don't use
3886 3886 * tcp_suna + tcp_swnd. Only data in flight can
3887 3887 * provide ECN info.
3888 3888 */
3889 3889 tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
3890 3890 tcp->tcp_ecn_cwr_sent = B_FALSE;
3891 3891 }
3892 3892 }
3893 3893
3894 3894 mp1 = tcp->tcp_xmit_head;
3895 3895 if (bytes_acked == 0) {
3896 3896 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) {
3897 3897 int dupack_cnt;
3898 3898
3899 3899 TCPS_BUMP_MIB(tcps, tcpInDupAck);
3900 3900 /*
3901 3901 * Fast retransmit. When we have seen exactly three
3902 3902 * identical ACKs while we have unacked data
3903 3903 * outstanding we take it as a hint that our peer
3904 3904 * dropped something.
3905 3905 *
3906 3906 * If TCP is retransmitting, don't do fast retransmit.
3907 3907 */
3908 3908 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt &&
3909 3909 ! tcp->tcp_rexmit) {
3910 3910 /* Do Limited Transmit */
3911 3911 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) <
3912 3912 tcps->tcps_dupack_fast_retransmit) {
3913 3913 /*
3914 3914 * RFC 3042
3915 3915 *
3916 3916 * What we need to do is temporarily
3917 3917 * increase tcp_cwnd so that new
3918 3918 * data can be sent if it is allowed
3919 3919 * by the receive window (tcp_rwnd).
3920 3920 * tcp_wput_data() will take care of
3921 3921 * the rest.
3922 3922 *
3923 3923 * If the connection is SACK capable,
3924 3924 * only do limited xmit when there
3925 3925 * is SACK info.
3926 3926 *
3927 3927 * Note how tcp_cwnd is incremented.
3928 3928 * The first dup ACK will increase
3929 3929 * it by 1 MSS. The second dup ACK
3930 3930 * will increase it by 2 MSS. This
3931 3931 * means that only 1 new segment will
3932 3932 * be sent for each dup ACK.
3933 3933 */
3934 3934 if (tcp->tcp_unsent > 0 &&
3935 3935 (!tcp->tcp_snd_sack_ok ||
3936 3936 (tcp->tcp_snd_sack_ok &&
3937 3937 tcp->tcp_notsack_list != NULL))) {
3938 3938 tcp->tcp_cwnd += mss <<
3939 3939 (tcp->tcp_dupack_cnt - 1);
3940 3940 flags |= TH_LIMIT_XMIT;
3941 3941 }
3942 3942 } else if (dupack_cnt ==
3943 3943 tcps->tcps_dupack_fast_retransmit) {
3944 3944
3945 3945 /*
3946 3946 * If we have reduced tcp_ssthresh
3947 3947 * because of ECN, do not reduce it again
3948 3948 * unless it is already one window of data
3949 3949 * away. After one window of data, tcp_cwr
3950 3950 * should then be cleared. Note that
3951 3951 * for non ECN capable connection, tcp_cwr
3952 3952 * should always be false.
3953 3953 *
3954 3954 * Adjust cwnd since the duplicate
3955 3955 * ack indicates that a packet was
3956 3956 * dropped (due to congestion.)
3957 3957 */
3958 3958 if (!tcp->tcp_cwr) {
3959 3959 npkt = ((tcp->tcp_snxt -
3960 3960 tcp->tcp_suna) >> 1) / mss;
3961 3961 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) *
3962 3962 mss;
3963 3963 tcp->tcp_cwnd = (npkt +
3964 3964 tcp->tcp_dupack_cnt) * mss;
3965 3965 }
3966 3966 if (tcp->tcp_ecn_ok) {
3967 3967 tcp->tcp_cwr = B_TRUE;
3968 3968 tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
3969 3969 tcp->tcp_ecn_cwr_sent = B_FALSE;
3970 3970 }
3971 3971
3972 3972 /*
3973 3973 * We do Hoe's algorithm. Refer to her
3974 3974 * paper "Improving the Start-up Behavior
3975 3975 * of a Congestion Control Scheme for TCP,"
3976 3976 * appeared in SIGCOMM'96.
3977 3977 *
3978 3978 * Save highest seq no we have sent so far.
3979 3979 * Be careful about the invisible FIN byte.
3980 3980 */
3981 3981 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) &&
3982 3982 (tcp->tcp_unsent == 0)) {
3983 3983 tcp->tcp_rexmit_max = tcp->tcp_fss;
3984 3984 } else {
3985 3985 tcp->tcp_rexmit_max = tcp->tcp_snxt;
3986 3986 }
3987 3987
3988 3988 /*
3989 3989 * For SACK:
3990 3990 * Calculate tcp_pipe, which is the
3991 3991 * estimated number of bytes in
3992 3992 * network.
3993 3993 *
3994 3994 * tcp_fack is the highest sack'ed seq num
3995 3995 * TCP has received.
3996 3996 *
3997 3997 * tcp_pipe is explained in the above quoted
3998 3998 * Fall and Floyd's paper. tcp_fack is
3999 3999 * explained in Mathis and Mahdavi's
4000 4000 * "Forward Acknowledgment: Refining TCP
4001 4001 * Congestion Control" in SIGCOMM '96.
4002 4002 */
4003 4003 if (tcp->tcp_snd_sack_ok) {
4004 4004 if (tcp->tcp_notsack_list != NULL) {
4005 4005 tcp->tcp_pipe = tcp->tcp_snxt -
4006 4006 tcp->tcp_fack;
4007 4007 tcp->tcp_sack_snxt = seg_ack;
4008 4008 flags |= TH_NEED_SACK_REXMIT;
4009 4009 } else {
4010 4010 /*
4011 4011 * Always initialize tcp_pipe
4012 4012 * even though we don't have
4013 4013 * any SACK info. If later
4014 4014 * we get SACK info and
4015 4015 * tcp_pipe is not initialized,
4016 4016 * funny things will happen.
4017 4017 */
4018 4018 tcp->tcp_pipe =
4019 4019 tcp->tcp_cwnd_ssthresh;
4020 4020 }
4021 4021 } else {
4022 4022 flags |= TH_REXMIT_NEEDED;
4023 4023 } /* tcp_snd_sack_ok */
4024 4024
4025 4025 } else {
4026 4026 /*
4027 4027 * Here we perform congestion
4028 4028 * avoidance, but NOT slow start.
4029 4029 * This is known as the Fast
4030 4030 * Recovery Algorithm.
4031 4031 */
4032 4032 if (tcp->tcp_snd_sack_ok &&
4033 4033 tcp->tcp_notsack_list != NULL) {
4034 4034 flags |= TH_NEED_SACK_REXMIT;
4035 4035 tcp->tcp_pipe -= mss;
4036 4036 if (tcp->tcp_pipe < 0)
4037 4037 tcp->tcp_pipe = 0;
4038 4038 } else {
4039 4039 /*
4040 4040 * We know that one more packet has
4041 4041 * left the pipe thus we can update
4042 4042 * cwnd.
4043 4043 */
4044 4044 cwnd = tcp->tcp_cwnd + mss;
4045 4045 if (cwnd > tcp->tcp_cwnd_max)
4046 4046 cwnd = tcp->tcp_cwnd_max;
4047 4047 tcp->tcp_cwnd = cwnd;
4048 4048 if (tcp->tcp_unsent > 0)
4049 4049 flags |= TH_XMIT_NEEDED;
4050 4050 }
4051 4051 }
4052 4052 }
4053 4053 } else if (tcp->tcp_zero_win_probe) {
4054 4054 /*
4055 4055 * If the window has opened, need to arrange
4056 4056 * to send additional data.
4057 4057 */
4058 4058 if (new_swnd != 0) {
4059 4059 /* tcp_suna != tcp_snxt */
4060 4060 /* Packet contains a window update */
4061 4061 TCPS_BUMP_MIB(tcps, tcpInWinUpdate);
4062 4062 tcp->tcp_zero_win_probe = 0;
4063 4063 tcp->tcp_timer_backoff = 0;
4064 4064 tcp->tcp_ms_we_have_waited = 0;
4065 4065
4066 4066 /*
4067 4067 * Transmit starting with tcp_suna since
4068 4068 * the one byte probe is not ack'ed.
4069 4069 * If TCP has sent more than one identical
4070 4070 * probe, tcp_rexmit will be set. That means
4071 4071 * tcp_ss_rexmit() will send out the one
4072 4072 * byte along with new data. Otherwise,
4073 4073 * fake the retransmission.
4074 4074 */
4075 4075 flags |= TH_XMIT_NEEDED;
4076 4076 if (!tcp->tcp_rexmit) {
4077 4077 tcp->tcp_rexmit = B_TRUE;
4078 4078 tcp->tcp_dupack_cnt = 0;
4079 4079 tcp->tcp_rexmit_nxt = tcp->tcp_suna;
4080 4080 tcp->tcp_rexmit_max = tcp->tcp_suna + 1;
4081 4081 }
4082 4082 }
4083 4083 }
4084 4084 goto swnd_update;
4085 4085 }
4086 4086
4087 4087 /*
4088 4088 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73.
4089 4089 * If the ACK value acks something that we have not yet sent, it might
4090 4090 * be an old duplicate segment. Send an ACK to re-synchronize the
4091 4091 * other side.
4092 4092 * Note: reset in response to unacceptable ACK in SYN_RECEIVE
4093 4093 * state is handled above, so we can always just drop the segment and
4094 4094 * send an ACK here.
4095 4095 *
4096 4096 * In the case where the peer shrinks the window, we see the new window
4097 4097 * update, but all the data sent previously is queued up by the peer.
4098 4098 * To account for this, in tcp_process_shrunk_swnd(), the sequence
4099 4099 * number, which was already sent, and within window, is recorded.
4100 4100 * tcp_snxt is then updated.
4101 4101 *
4102 4102 * If the window has previously shrunk, and an ACK for data not yet
4103 4103 * sent, according to tcp_snxt is recieved, it may still be valid. If
4104 4104 * the ACK is for data within the window at the time the window was
4105 4105 * shrunk, then the ACK is acceptable. In this case tcp_snxt is set to
4106 4106 * the sequence number ACK'ed.
4107 4107 *
4108 4108 * If the ACK covers all the data sent at the time the window was
4109 4109 * shrunk, we can now set tcp_is_wnd_shrnk to B_FALSE.
4110 4110 *
4111 4111 * Should we send ACKs in response to ACK only segments?
4112 4112 */
4113 4113
4114 4114 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) {
4115 4115 if ((tcp->tcp_is_wnd_shrnk) &&
4116 4116 (SEQ_LEQ(seg_ack, tcp->tcp_snxt_shrunk))) {
4117 4117 uint32_t data_acked_ahead_snxt;
4118 4118
4119 4119 data_acked_ahead_snxt = seg_ack - tcp->tcp_snxt;
4120 4120 tcp_update_xmit_tail(tcp, seg_ack);
4121 4121 tcp->tcp_unsent -= data_acked_ahead_snxt;
4122 4122 } else {
4123 4123 TCPS_BUMP_MIB(tcps, tcpInAckUnsent);
4124 4124 /* drop the received segment */
4125 4125 freemsg(mp);
4126 4126
4127 4127 /*
4128 4128 * Send back an ACK. If tcp_drop_ack_unsent_cnt is
4129 4129 * greater than 0, check if the number of such
4130 4130 * bogus ACks is greater than that count. If yes,
4131 4131 * don't send back any ACK. This prevents TCP from
4132 4132 * getting into an ACK storm if somehow an attacker
4133 4133 * successfully spoofs an acceptable segment to our
4134 4134 * peer. If this continues (count > 2 X threshold),
4135 4135 * we should abort this connection.
4136 4136 */
4137 4137 if (tcp_drop_ack_unsent_cnt > 0 &&
4138 4138 ++tcp->tcp_in_ack_unsent >
4139 4139 tcp_drop_ack_unsent_cnt) {
4140 4140 TCP_STAT(tcps, tcp_in_ack_unsent_drop);
4141 4141 if (tcp->tcp_in_ack_unsent > 2 *
4142 4142 tcp_drop_ack_unsent_cnt) {
4143 4143 (void) tcp_clean_death(tcp, EPROTO);
4144 4144 }
4145 4145 return;
4146 4146 }
4147 4147 mp = tcp_ack_mp(tcp);
4148 4148 if (mp != NULL) {
4149 4149 BUMP_LOCAL(tcp->tcp_obsegs);
4150 4150 TCPS_BUMP_MIB(tcps, tcpOutAck);
4151 4151 tcp_send_data(tcp, mp);
4152 4152 }
4153 4153 return;
4154 4154 }
4155 4155 } else if (tcp->tcp_is_wnd_shrnk && SEQ_GEQ(seg_ack,
4156 4156 tcp->tcp_snxt_shrunk)) {
4157 4157 tcp->tcp_is_wnd_shrnk = B_FALSE;
4158 4158 }
4159 4159
4160 4160 /*
4161 4161 * TCP gets a new ACK, update the notsack'ed list to delete those
4162 4162 * blocks that are covered by this ACK.
4163 4163 */
4164 4164 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) {
4165 4165 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack,
4166 4166 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list));
4167 4167 }
4168 4168
4169 4169 /*
4170 4170 * If we got an ACK after fast retransmit, check to see
4171 4171 * if it is a partial ACK. If it is not and the congestion
4172 4172 * window was inflated to account for the other side's
4173 4173 * cached packets, retract it. If it is, do Hoe's algorithm.
4174 4174 */
4175 4175 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) {
4176 4176 ASSERT(tcp->tcp_rexmit == B_FALSE);
4177 4177 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) {
4178 4178 tcp->tcp_dupack_cnt = 0;
4179 4179 /*
4180 4180 * Restore the orig tcp_cwnd_ssthresh after
4181 4181 * fast retransmit phase.
4182 4182 */
4183 4183 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) {
4184 4184 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh;
4185 4185 }
4186 4186 tcp->tcp_rexmit_max = seg_ack;
4187 4187 tcp->tcp_cwnd_cnt = 0;
4188 4188
4189 4189 /*
4190 4190 * Remove all notsack info to avoid confusion with
4191 4191 * the next fast retrasnmit/recovery phase.
4192 4192 */
4193 4193 if (tcp->tcp_snd_sack_ok) {
4194 4194 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list,
4195 4195 tcp);
4196 4196 }
4197 4197 } else {
4198 4198 if (tcp->tcp_snd_sack_ok &&
4199 4199 tcp->tcp_notsack_list != NULL) {
4200 4200 flags |= TH_NEED_SACK_REXMIT;
4201 4201 tcp->tcp_pipe -= mss;
4202 4202 if (tcp->tcp_pipe < 0)
4203 4203 tcp->tcp_pipe = 0;
4204 4204 } else {
4205 4205 /*
4206 4206 * Hoe's algorithm:
4207 4207 *
4208 4208 * Retransmit the unack'ed segment and
4209 4209 * restart fast recovery. Note that we
4210 4210 * need to scale back tcp_cwnd to the
4211 4211 * original value when we started fast
4212 4212 * recovery. This is to prevent overly
4213 4213 * aggressive behaviour in sending new
4214 4214 * segments.
4215 4215 */
4216 4216 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh +
4217 4217 tcps->tcps_dupack_fast_retransmit * mss;
4218 4218 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd;
4219 4219 flags |= TH_REXMIT_NEEDED;
4220 4220 }
4221 4221 }
4222 4222 } else {
4223 4223 tcp->tcp_dupack_cnt = 0;
4224 4224 if (tcp->tcp_rexmit) {
4225 4225 /*
4226 4226 * TCP is retranmitting. If the ACK ack's all
4227 4227 * outstanding data, update tcp_rexmit_max and
4228 4228 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt
4229 4229 * to the correct value.
4230 4230 *
4231 4231 * Note that SEQ_LEQ() is used. This is to avoid
4232 4232 * unnecessary fast retransmit caused by dup ACKs
4233 4233 * received when TCP does slow start retransmission
4234 4234 * after a time out. During this phase, TCP may
4235 4235 * send out segments which are already received.
4236 4236 * This causes dup ACKs to be sent back.
4237 4237 */
4238 4238 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) {
4239 4239 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) {
4240 4240 tcp->tcp_rexmit_nxt = seg_ack;
4241 4241 }
4242 4242 if (seg_ack != tcp->tcp_rexmit_max) {
4243 4243 flags |= TH_XMIT_NEEDED;
4244 4244 }
4245 4245 } else {
4246 4246 tcp->tcp_rexmit = B_FALSE;
4247 4247 tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
4248 4248 }
4249 4249 tcp->tcp_ms_we_have_waited = 0;
4250 4250 }
4251 4251 }
4252 4252
4253 4253 TCPS_BUMP_MIB(tcps, tcpInAckSegs);
4254 4254 TCPS_UPDATE_MIB(tcps, tcpInAckBytes, bytes_acked);
4255 4255 tcp->tcp_suna = seg_ack;
4256 4256 if (tcp->tcp_zero_win_probe != 0) {
4257 4257 tcp->tcp_zero_win_probe = 0;
4258 4258 tcp->tcp_timer_backoff = 0;
4259 4259 }
4260 4260
4261 4261 /*
4262 4262 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed.
4263 4263 * Note that it cannot be the SYN being ack'ed. The code flow
4264 4264 * will not reach here.
4265 4265 */
4266 4266 if (mp1 == NULL) {
4267 4267 goto fin_acked;
4268 4268 }
4269 4269
4270 4270 /*
4271 4271 * Update the congestion window.
4272 4272 *
4273 4273 * If TCP is not ECN capable or TCP is ECN capable but the
4274 4274 * congestion experience bit is not set, increase the tcp_cwnd as
4275 4275 * usual.
4276 4276 */
4277 4277 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) {
4278 4278 cwnd = tcp->tcp_cwnd;
4279 4279 add = mss;
4280 4280
4281 4281 if (cwnd >= tcp->tcp_cwnd_ssthresh) {
4282 4282 /*
4283 4283 * This is to prevent an increase of less than 1 MSS of
4284 4284 * tcp_cwnd. With partial increase, tcp_wput_data()
4285 4285 * may send out tinygrams in order to preserve mblk
4286 4286 * boundaries.
4287 4287 *
4288 4288 * By initializing tcp_cwnd_cnt to new tcp_cwnd and
4289 4289 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is
4290 4290 * increased by 1 MSS for every RTTs.
4291 4291 */
4292 4292 if (tcp->tcp_cwnd_cnt <= 0) {
4293 4293 tcp->tcp_cwnd_cnt = cwnd + add;
4294 4294 } else {
4295 4295 tcp->tcp_cwnd_cnt -= add;
4296 4296 add = 0;
↓ open down ↓ |
921 lines elided |
↑ open up ↑ |
4297 4297 }
4298 4298 }
4299 4299 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max);
4300 4300 }
4301 4301
4302 4302 /* See if the latest urgent data has been acknowledged */
4303 4303 if ((tcp->tcp_valid_bits & TCP_URG_VALID) &&
4304 4304 SEQ_GT(seg_ack, tcp->tcp_urg))
4305 4305 tcp->tcp_valid_bits &= ~TCP_URG_VALID;
4306 4306
4307 - /* Can we update the RTT estimates? */
4308 - if (tcp->tcp_snd_ts_ok) {
4309 - /* Ignore zero timestamp echo-reply. */
4310 - if (tcpopt.tcp_opt_ts_ecr != 0) {
4311 - tcp_set_rto(tcp, (int32_t)LBOLT_FASTPATH -
4312 - (int32_t)tcpopt.tcp_opt_ts_ecr);
4313 - }
4314 -
4315 - /* If needed, restart the timer. */
4316 - if (tcp->tcp_set_timer == 1) {
4317 - TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
4318 - tcp->tcp_set_timer = 0;
4319 - }
4307 + /*
4308 + * Update the RTT estimates. Note that we don't use the TCP
4309 + * timestamp option to calculate RTT even if one is present. This is
4310 + * because the timestamp option's resolution (CPU tick) is
4311 + * too coarse to measure modern datacenter networks' microsecond
4312 + * latencies. The timestamp field's resolution is limited by its
4313 + * 4-byte width (see RFC1323), and since we always store a
4314 + * high-resolution nanosecond presision timestamp along with the data,
4315 + * there is no point to ever using the timestamp option.
4316 + */
4317 + if (SEQ_GT(seg_ack, tcp->tcp_csuna)) {
4320 4318 /*
4321 - * Update tcp_csuna in case the other side stops sending
4322 - * us timestamps.
4323 - */
4324 - tcp->tcp_csuna = tcp->tcp_snxt;
4325 - } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) {
4326 - /*
4327 4319 * An ACK sequence we haven't seen before, so get the RTT
4328 4320 * and update the RTO. But first check if the timestamp is
4329 4321 * valid to use.
4330 4322 */
4331 4323 if ((mp1->b_next != NULL) &&
4332 - SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next)))
4333 - tcp_set_rto(tcp, (int32_t)LBOLT_FASTPATH -
4334 - (int32_t)(intptr_t)mp1->b_prev);
4335 - else
4324 + SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) {
4325 + tcp_set_rto(tcp, gethrtime() -
4326 + (hrtime_t)(intptr_t)mp1->b_prev);
4327 + } else {
4336 4328 TCPS_BUMP_MIB(tcps, tcpRttNoUpdate);
4329 + }
4337 4330
4338 4331 /* Remeber the last sequence to be ACKed */
4339 4332 tcp->tcp_csuna = seg_ack;
4340 4333 if (tcp->tcp_set_timer == 1) {
4341 4334 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
4342 4335 tcp->tcp_set_timer = 0;
4343 4336 }
4344 4337 } else {
4345 4338 TCPS_BUMP_MIB(tcps, tcpRttNoUpdate);
4346 4339 }
4347 4340
4348 4341 /* Eat acknowledged bytes off the xmit queue. */
4349 4342 for (;;) {
4350 4343 mblk_t *mp2;
4351 4344 uchar_t *wptr;
4352 4345
4353 4346 wptr = mp1->b_wptr;
4354 4347 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX);
↓ open down ↓ |
8 lines elided |
↑ open up ↑ |
4355 4348 bytes_acked -= (int)(wptr - mp1->b_rptr);
4356 4349 if (bytes_acked < 0) {
4357 4350 mp1->b_rptr = wptr + bytes_acked;
4358 4351 /*
4359 4352 * Set a new timestamp if all the bytes timed by the
4360 4353 * old timestamp have been ack'ed.
4361 4354 */
4362 4355 if (SEQ_GT(seg_ack,
4363 4356 (uint32_t)(uintptr_t)(mp1->b_next))) {
4364 4357 mp1->b_prev =
4365 - (mblk_t *)(uintptr_t)LBOLT_FASTPATH;
4358 + (mblk_t *)(intptr_t)gethrtime();
4366 4359 mp1->b_next = NULL;
4367 4360 }
4368 4361 break;
4369 4362 }
4370 4363 mp1->b_next = NULL;
4371 4364 mp1->b_prev = NULL;
4372 4365 mp2 = mp1;
4373 4366 mp1 = mp1->b_cont;
4374 4367
4375 4368 /*
4376 4369 * This notification is required for some zero-copy
4377 4370 * clients to maintain a copy semantic. After the data
4378 4371 * is ack'ed, client is safe to modify or reuse the buffer.
4379 4372 */
4380 4373 if (tcp->tcp_snd_zcopy_aware &&
4381 4374 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY))
4382 4375 tcp_zcopy_notify(tcp);
4383 4376 freeb(mp2);
4384 4377 if (bytes_acked == 0) {
4385 4378 if (mp1 == NULL) {
4386 4379 /* Everything is ack'ed, clear the tail. */
4387 4380 tcp->tcp_xmit_tail = NULL;
4388 4381 /*
4389 4382 * Cancel the timer unless we are still
4390 4383 * waiting for an ACK for the FIN packet.
4391 4384 */
4392 4385 if (tcp->tcp_timer_tid != 0 &&
4393 4386 tcp->tcp_snxt == tcp->tcp_suna) {
4394 4387 (void) TCP_TIMER_CANCEL(tcp,
4395 4388 tcp->tcp_timer_tid);
4396 4389 tcp->tcp_timer_tid = 0;
4397 4390 }
4398 4391 goto pre_swnd_update;
4399 4392 }
4400 4393 if (mp2 != tcp->tcp_xmit_tail)
4401 4394 break;
4402 4395 tcp->tcp_xmit_tail = mp1;
4403 4396 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <=
4404 4397 (uintptr_t)INT_MAX);
4405 4398 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr -
4406 4399 mp1->b_rptr);
4407 4400 break;
4408 4401 }
4409 4402 if (mp1 == NULL) {
4410 4403 /*
4411 4404 * More was acked but there is nothing more
4412 4405 * outstanding. This means that the FIN was
4413 4406 * just acked or that we're talking to a clown.
4414 4407 */
4415 4408 fin_acked:
4416 4409 ASSERT(tcp->tcp_fin_sent);
4417 4410 tcp->tcp_xmit_tail = NULL;
4418 4411 if (tcp->tcp_fin_sent) {
4419 4412 /* FIN was acked - making progress */
4420 4413 if (!tcp->tcp_fin_acked)
4421 4414 tcp->tcp_ip_forward_progress = B_TRUE;
4422 4415 tcp->tcp_fin_acked = B_TRUE;
4423 4416 if (tcp->tcp_linger_tid != 0 &&
4424 4417 TCP_TIMER_CANCEL(tcp,
4425 4418 tcp->tcp_linger_tid) >= 0) {
4426 4419 tcp_stop_lingering(tcp);
4427 4420 freemsg(mp);
4428 4421 mp = NULL;
4429 4422 }
4430 4423 } else {
4431 4424 /*
4432 4425 * We should never get here because
4433 4426 * we have already checked that the
4434 4427 * number of bytes ack'ed should be
4435 4428 * smaller than or equal to what we
4436 4429 * have sent so far (it is the
4437 4430 * acceptability check of the ACK).
4438 4431 * We can only get here if the send
4439 4432 * queue is corrupted.
4440 4433 *
4441 4434 * Terminate the connection and
4442 4435 * panic the system. It is better
4443 4436 * for us to panic instead of
4444 4437 * continuing to avoid other disaster.
4445 4438 */
4446 4439 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt,
4447 4440 tcp->tcp_rnxt, TH_RST|TH_ACK);
4448 4441 panic("Memory corruption "
4449 4442 "detected for connection %s.",
4450 4443 tcp_display(tcp, NULL,
4451 4444 DISP_ADDR_AND_PORT));
4452 4445 /*NOTREACHED*/
4453 4446 }
4454 4447 goto pre_swnd_update;
4455 4448 }
4456 4449 ASSERT(mp2 != tcp->tcp_xmit_tail);
4457 4450 }
4458 4451 if (tcp->tcp_unsent) {
4459 4452 flags |= TH_XMIT_NEEDED;
4460 4453 }
4461 4454 pre_swnd_update:
4462 4455 tcp->tcp_xmit_head = mp1;
4463 4456 swnd_update:
4464 4457 /*
4465 4458 * The following check is different from most other implementations.
4466 4459 * For bi-directional transfer, when segments are dropped, the
4467 4460 * "normal" check will not accept a window update in those
4468 4461 * retransmitted segemnts. Failing to do that, TCP may send out
4469 4462 * segments which are outside receiver's window. As TCP accepts
4470 4463 * the ack in those retransmitted segments, if the window update in
4471 4464 * the same segment is not accepted, TCP will incorrectly calculates
4472 4465 * that it can send more segments. This can create a deadlock
4473 4466 * with the receiver if its window becomes zero.
4474 4467 */
4475 4468 if (SEQ_LT(tcp->tcp_swl2, seg_ack) ||
4476 4469 SEQ_LT(tcp->tcp_swl1, seg_seq) ||
4477 4470 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) {
4478 4471 /*
4479 4472 * The criteria for update is:
4480 4473 *
4481 4474 * 1. the segment acknowledges some data. Or
4482 4475 * 2. the segment is new, i.e. it has a higher seq num. Or
4483 4476 * 3. the segment is not old and the advertised window is
4484 4477 * larger than the previous advertised window.
4485 4478 */
4486 4479 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd)
4487 4480 flags |= TH_XMIT_NEEDED;
4488 4481 tcp->tcp_swnd = new_swnd;
4489 4482 if (new_swnd > tcp->tcp_max_swnd)
4490 4483 tcp->tcp_max_swnd = new_swnd;
4491 4484 tcp->tcp_swl1 = seg_seq;
4492 4485 tcp->tcp_swl2 = seg_ack;
4493 4486 }
4494 4487 est:
4495 4488 if (tcp->tcp_state > TCPS_ESTABLISHED) {
4496 4489
4497 4490 switch (tcp->tcp_state) {
4498 4491 case TCPS_FIN_WAIT_1:
4499 4492 if (tcp->tcp_fin_acked) {
4500 4493 tcp->tcp_state = TCPS_FIN_WAIT_2;
4501 4494 DTRACE_TCP6(state__change, void, NULL,
4502 4495 ip_xmit_attr_t *, connp->conn_ixa,
4503 4496 void, NULL, tcp_t *, tcp, void, NULL,
4504 4497 int32_t, TCPS_FIN_WAIT_1);
4505 4498 /*
4506 4499 * We implement the non-standard BSD/SunOS
4507 4500 * FIN_WAIT_2 flushing algorithm.
4508 4501 * If there is no user attached to this
4509 4502 * TCP endpoint, then this TCP struct
4510 4503 * could hang around forever in FIN_WAIT_2
4511 4504 * state if the peer forgets to send us
4512 4505 * a FIN. To prevent this, we wait only
4513 4506 * 2*MSL (a convenient time value) for
4514 4507 * the FIN to arrive. If it doesn't show up,
4515 4508 * we flush the TCP endpoint. This algorithm,
4516 4509 * though a violation of RFC-793, has worked
4517 4510 * for over 10 years in BSD systems.
4518 4511 * Note: SunOS 4.x waits 675 seconds before
4519 4512 * flushing the FIN_WAIT_2 connection.
4520 4513 */
4521 4514 TCP_TIMER_RESTART(tcp,
4522 4515 tcp->tcp_fin_wait_2_flush_interval);
4523 4516 }
4524 4517 break;
4525 4518 case TCPS_FIN_WAIT_2:
4526 4519 break; /* Shutdown hook? */
4527 4520 case TCPS_LAST_ACK:
4528 4521 freemsg(mp);
4529 4522 if (tcp->tcp_fin_acked) {
4530 4523 (void) tcp_clean_death(tcp, 0);
4531 4524 return;
4532 4525 }
4533 4526 goto xmit_check;
4534 4527 case TCPS_CLOSING:
4535 4528 if (tcp->tcp_fin_acked) {
4536 4529 SET_TIME_WAIT(tcps, tcp, connp);
4537 4530 DTRACE_TCP6(state__change, void, NULL,
4538 4531 ip_xmit_attr_t *, connp->conn_ixa, void,
4539 4532 NULL, tcp_t *, tcp, void, NULL, int32_t,
4540 4533 TCPS_CLOSING);
4541 4534 }
4542 4535 /*FALLTHRU*/
4543 4536 case TCPS_CLOSE_WAIT:
4544 4537 freemsg(mp);
4545 4538 goto xmit_check;
4546 4539 default:
4547 4540 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT);
4548 4541 break;
4549 4542 }
4550 4543 }
4551 4544 if (flags & TH_FIN) {
4552 4545 /* Make sure we ack the fin */
4553 4546 flags |= TH_ACK_NEEDED;
4554 4547 if (!tcp->tcp_fin_rcvd) {
4555 4548 tcp->tcp_fin_rcvd = B_TRUE;
4556 4549 tcp->tcp_rnxt++;
4557 4550 tcpha = tcp->tcp_tcpha;
4558 4551 tcpha->tha_ack = htonl(tcp->tcp_rnxt);
4559 4552
4560 4553 /*
4561 4554 * Generate the ordrel_ind at the end unless the
4562 4555 * conn is detached or it is a STREAMS based eager.
4563 4556 * In the eager case we defer the notification until
4564 4557 * tcp_accept_finish has run.
4565 4558 */
4566 4559 if (!TCP_IS_DETACHED(tcp) && (IPCL_IS_NONSTR(connp) ||
4567 4560 (tcp->tcp_listener == NULL &&
4568 4561 !tcp->tcp_hard_binding)))
4569 4562 flags |= TH_ORDREL_NEEDED;
4570 4563 switch (tcp->tcp_state) {
4571 4564 case TCPS_SYN_RCVD:
4572 4565 tcp->tcp_state = TCPS_CLOSE_WAIT;
4573 4566 DTRACE_TCP6(state__change, void, NULL,
4574 4567 ip_xmit_attr_t *, connp->conn_ixa,
4575 4568 void, NULL, tcp_t *, tcp, void, NULL,
4576 4569 int32_t, TCPS_SYN_RCVD);
4577 4570 /* Keepalive? */
4578 4571 break;
4579 4572 case TCPS_ESTABLISHED:
4580 4573 tcp->tcp_state = TCPS_CLOSE_WAIT;
4581 4574 DTRACE_TCP6(state__change, void, NULL,
4582 4575 ip_xmit_attr_t *, connp->conn_ixa,
4583 4576 void, NULL, tcp_t *, tcp, void, NULL,
4584 4577 int32_t, TCPS_ESTABLISHED);
4585 4578 /* Keepalive? */
4586 4579 break;
4587 4580 case TCPS_FIN_WAIT_1:
4588 4581 if (!tcp->tcp_fin_acked) {
4589 4582 tcp->tcp_state = TCPS_CLOSING;
4590 4583 DTRACE_TCP6(state__change, void, NULL,
4591 4584 ip_xmit_attr_t *, connp->conn_ixa,
4592 4585 void, NULL, tcp_t *, tcp, void,
4593 4586 NULL, int32_t, TCPS_FIN_WAIT_1);
4594 4587 break;
4595 4588 }
4596 4589 /* FALLTHRU */
4597 4590 case TCPS_FIN_WAIT_2:
4598 4591 SET_TIME_WAIT(tcps, tcp, connp);
4599 4592 DTRACE_TCP6(state__change, void, NULL,
4600 4593 ip_xmit_attr_t *, connp->conn_ixa, void,
4601 4594 NULL, tcp_t *, tcp, void, NULL, int32_t,
4602 4595 TCPS_FIN_WAIT_2);
4603 4596 if (seg_len) {
4604 4597 /*
4605 4598 * implies data piggybacked on FIN.
4606 4599 * break to handle data.
4607 4600 */
4608 4601 break;
4609 4602 }
4610 4603 freemsg(mp);
4611 4604 goto ack_check;
4612 4605 }
4613 4606 }
4614 4607 }
4615 4608 if (mp == NULL)
4616 4609 goto xmit_check;
4617 4610 if (seg_len == 0) {
4618 4611 freemsg(mp);
4619 4612 goto xmit_check;
4620 4613 }
4621 4614 if (mp->b_rptr == mp->b_wptr) {
4622 4615 /*
4623 4616 * The header has been consumed, so we remove the
4624 4617 * zero-length mblk here.
4625 4618 */
4626 4619 mp1 = mp;
4627 4620 mp = mp->b_cont;
4628 4621 freeb(mp1);
4629 4622 }
4630 4623 update_ack:
4631 4624 tcpha = tcp->tcp_tcpha;
4632 4625 tcp->tcp_rack_cnt++;
4633 4626 {
4634 4627 uint32_t cur_max;
4635 4628
4636 4629 cur_max = tcp->tcp_rack_cur_max;
4637 4630 if (tcp->tcp_rack_cnt >= cur_max) {
4638 4631 /*
4639 4632 * We have more unacked data than we should - send
4640 4633 * an ACK now.
4641 4634 */
4642 4635 flags |= TH_ACK_NEEDED;
4643 4636 cur_max++;
4644 4637 if (cur_max > tcp->tcp_rack_abs_max)
4645 4638 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max;
4646 4639 else
4647 4640 tcp->tcp_rack_cur_max = cur_max;
4648 4641 } else if (TCP_IS_DETACHED(tcp)) {
4649 4642 /* We don't have an ACK timer for detached TCP. */
4650 4643 flags |= TH_ACK_NEEDED;
4651 4644 } else if (seg_len < mss) {
4652 4645 /*
4653 4646 * If we get a segment that is less than an mss, and we
4654 4647 * already have unacknowledged data, and the amount
4655 4648 * unacknowledged is not a multiple of mss, then we
4656 4649 * better generate an ACK now. Otherwise, this may be
4657 4650 * the tail piece of a transaction, and we would rather
4658 4651 * wait for the response.
4659 4652 */
4660 4653 uint32_t udif;
4661 4654 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <=
4662 4655 (uintptr_t)INT_MAX);
4663 4656 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack);
4664 4657 if (udif && (udif % mss))
4665 4658 flags |= TH_ACK_NEEDED;
4666 4659 else
4667 4660 flags |= TH_ACK_TIMER_NEEDED;
4668 4661 } else {
4669 4662 /* Start delayed ack timer */
4670 4663 flags |= TH_ACK_TIMER_NEEDED;
4671 4664 }
4672 4665 }
4673 4666 tcp->tcp_rnxt += seg_len;
4674 4667 tcpha->tha_ack = htonl(tcp->tcp_rnxt);
4675 4668
4676 4669 if (mp == NULL)
4677 4670 goto xmit_check;
4678 4671
4679 4672 /* Update SACK list */
4680 4673 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
4681 4674 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt,
4682 4675 &(tcp->tcp_num_sack_blk));
4683 4676 }
4684 4677
4685 4678 if (tcp->tcp_urp_mp) {
4686 4679 tcp->tcp_urp_mp->b_cont = mp;
4687 4680 mp = tcp->tcp_urp_mp;
4688 4681 tcp->tcp_urp_mp = NULL;
4689 4682 /* Ready for a new signal. */
4690 4683 tcp->tcp_urp_last_valid = B_FALSE;
4691 4684 #ifdef DEBUG
4692 4685 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
4693 4686 "tcp_rput: sending exdata_ind %s",
4694 4687 tcp_display(tcp, NULL, DISP_PORT_ONLY));
4695 4688 #endif /* DEBUG */
4696 4689 }
4697 4690
4698 4691 /*
4699 4692 * Check for ancillary data changes compared to last segment.
4700 4693 */
4701 4694 if (connp->conn_recv_ancillary.crb_all != 0) {
4702 4695 mp = tcp_input_add_ancillary(tcp, mp, &ipp, ira);
4703 4696 if (mp == NULL)
4704 4697 return;
4705 4698 }
4706 4699
4707 4700 if (IPCL_IS_NONSTR(connp)) {
4708 4701 /*
4709 4702 * Non-STREAMS socket
4710 4703 */
4711 4704 boolean_t push = flags & (TH_PUSH|TH_FIN);
4712 4705 int error;
4713 4706
4714 4707 if ((*sockupcalls->su_recv)(connp->conn_upper_handle,
4715 4708 mp, seg_len, 0, &error, &push) <= 0) {
4716 4709 /*
4717 4710 * We should never be in middle of a
4718 4711 * fallback, the squeue guarantees that.
4719 4712 */
4720 4713 ASSERT(error != EOPNOTSUPP);
4721 4714 if (error == ENOSPC)
4722 4715 tcp->tcp_rwnd -= seg_len;
4723 4716 } else if (push) {
4724 4717 /* PUSH bit set and sockfs is not flow controlled */
4725 4718 flags |= tcp_rwnd_reopen(tcp);
4726 4719 }
4727 4720 } else if (tcp->tcp_listener != NULL || tcp->tcp_hard_binding) {
4728 4721 /*
4729 4722 * Side queue inbound data until the accept happens.
4730 4723 * tcp_accept/tcp_rput drains this when the accept happens.
4731 4724 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or
4732 4725 * T_EXDATA_IND) it is queued on b_next.
4733 4726 * XXX Make urgent data use this. Requires:
4734 4727 * Removing tcp_listener check for TH_URG
4735 4728 * Making M_PCPROTO and MARK messages skip the eager case
4736 4729 */
4737 4730
4738 4731 tcp_rcv_enqueue(tcp, mp, seg_len, ira->ira_cred);
4739 4732 } else {
4740 4733 /* Active STREAMS socket */
4741 4734 if (mp->b_datap->db_type != M_DATA ||
4742 4735 (flags & TH_MARKNEXT_NEEDED)) {
4743 4736 if (tcp->tcp_rcv_list != NULL) {
4744 4737 flags |= tcp_rcv_drain(tcp);
4745 4738 }
4746 4739 ASSERT(tcp->tcp_rcv_list == NULL ||
4747 4740 tcp->tcp_fused_sigurg);
4748 4741
4749 4742 if (flags & TH_MARKNEXT_NEEDED) {
4750 4743 #ifdef DEBUG
4751 4744 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
4752 4745 "tcp_rput: sending MSGMARKNEXT %s",
4753 4746 tcp_display(tcp, NULL,
4754 4747 DISP_PORT_ONLY));
4755 4748 #endif /* DEBUG */
4756 4749 mp->b_flag |= MSGMARKNEXT;
4757 4750 flags &= ~TH_MARKNEXT_NEEDED;
4758 4751 }
4759 4752
4760 4753 if (is_system_labeled())
4761 4754 tcp_setcred_data(mp, ira);
4762 4755
4763 4756 putnext(connp->conn_rq, mp);
4764 4757 if (!canputnext(connp->conn_rq))
4765 4758 tcp->tcp_rwnd -= seg_len;
4766 4759 } else if ((flags & (TH_PUSH|TH_FIN)) ||
4767 4760 tcp->tcp_rcv_cnt + seg_len >= connp->conn_rcvbuf >> 3) {
4768 4761 if (tcp->tcp_rcv_list != NULL) {
4769 4762 /*
4770 4763 * Enqueue the new segment first and then
4771 4764 * call tcp_rcv_drain() to send all data
4772 4765 * up. The other way to do this is to
4773 4766 * send all queued data up and then call
4774 4767 * putnext() to send the new segment up.
4775 4768 * This way can remove the else part later
4776 4769 * on.
4777 4770 *
4778 4771 * We don't do this to avoid one more call to
4779 4772 * canputnext() as tcp_rcv_drain() needs to
4780 4773 * call canputnext().
4781 4774 */
4782 4775 tcp_rcv_enqueue(tcp, mp, seg_len,
4783 4776 ira->ira_cred);
4784 4777 flags |= tcp_rcv_drain(tcp);
4785 4778 } else {
4786 4779 if (is_system_labeled())
4787 4780 tcp_setcred_data(mp, ira);
4788 4781
4789 4782 putnext(connp->conn_rq, mp);
4790 4783 if (!canputnext(connp->conn_rq))
4791 4784 tcp->tcp_rwnd -= seg_len;
4792 4785 }
4793 4786 } else {
4794 4787 /*
4795 4788 * Enqueue all packets when processing an mblk
4796 4789 * from the co queue and also enqueue normal packets.
4797 4790 */
4798 4791 tcp_rcv_enqueue(tcp, mp, seg_len, ira->ira_cred);
4799 4792 }
4800 4793 /*
4801 4794 * Make sure the timer is running if we have data waiting
4802 4795 * for a push bit. This provides resiliency against
4803 4796 * implementations that do not correctly generate push bits.
4804 4797 */
4805 4798 if (tcp->tcp_rcv_list != NULL && tcp->tcp_push_tid == 0) {
4806 4799 /*
4807 4800 * The connection may be closed at this point, so don't
4808 4801 * do anything for a detached tcp.
4809 4802 */
4810 4803 if (!TCP_IS_DETACHED(tcp))
4811 4804 tcp->tcp_push_tid = TCP_TIMER(tcp,
4812 4805 tcp_push_timer,
4813 4806 tcps->tcps_push_timer_interval);
4814 4807 }
4815 4808 }
4816 4809
4817 4810 xmit_check:
4818 4811 /* Is there anything left to do? */
4819 4812 ASSERT(!(flags & TH_MARKNEXT_NEEDED));
4820 4813 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED|
4821 4814 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED|
4822 4815 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0)
4823 4816 goto done;
4824 4817
4825 4818 /* Any transmit work to do and a non-zero window? */
4826 4819 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT|
4827 4820 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) {
4828 4821 if (flags & TH_REXMIT_NEEDED) {
4829 4822 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna;
4830 4823
4831 4824 TCPS_BUMP_MIB(tcps, tcpOutFastRetrans);
↓ open down ↓ |
456 lines elided |
↑ open up ↑ |
4832 4825 if (snd_size > mss)
4833 4826 snd_size = mss;
4834 4827 if (snd_size > tcp->tcp_swnd)
4835 4828 snd_size = tcp->tcp_swnd;
4836 4829 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size,
4837 4830 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size,
4838 4831 B_TRUE);
4839 4832
4840 4833 if (mp1 != NULL) {
4841 4834 tcp->tcp_xmit_head->b_prev =
4842 - (mblk_t *)LBOLT_FASTPATH;
4835 + (mblk_t *)(intptr_t)gethrtime();
4843 4836 tcp->tcp_csuna = tcp->tcp_snxt;
4844 4837 TCPS_BUMP_MIB(tcps, tcpRetransSegs);
4845 4838 TCPS_UPDATE_MIB(tcps, tcpRetransBytes,
4846 4839 snd_size);
4847 4840 tcp_send_data(tcp, mp1);
4848 4841 }
4849 4842 }
4850 4843 if (flags & TH_NEED_SACK_REXMIT) {
4851 4844 tcp_sack_rexmit(tcp, &flags);
4852 4845 }
4853 4846 /*
4854 4847 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send
4855 4848 * out new segment. Note that tcp_rexmit should not be
4856 4849 * set, otherwise TH_LIMIT_XMIT should not be set.
4857 4850 */
4858 4851 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) {
4859 4852 if (!tcp->tcp_rexmit) {
4860 4853 tcp_wput_data(tcp, NULL, B_FALSE);
4861 4854 } else {
4862 4855 tcp_ss_rexmit(tcp);
4863 4856 }
4864 4857 }
4865 4858 /*
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
4866 4859 * Adjust tcp_cwnd back to normal value after sending
4867 4860 * new data segments.
4868 4861 */
4869 4862 if (flags & TH_LIMIT_XMIT) {
4870 4863 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1);
4871 4864 /*
4872 4865 * This will restart the timer. Restarting the
4873 4866 * timer is used to avoid a timeout before the
4874 4867 * limited transmitted segment's ACK gets back.
4875 4868 */
4876 - if (tcp->tcp_xmit_head != NULL)
4869 + if (tcp->tcp_xmit_head != NULL) {
4877 4870 tcp->tcp_xmit_head->b_prev =
4878 - (mblk_t *)LBOLT_FASTPATH;
4871 + (mblk_t *)(intptr_t)gethrtime();
4872 + }
4879 4873 }
4880 4874
4881 4875 /* Anything more to do? */
4882 4876 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED|
4883 4877 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0)
4884 4878 goto done;
4885 4879 }
4886 4880 ack_check:
4887 4881 if (flags & TH_SEND_URP_MARK) {
4888 4882 ASSERT(tcp->tcp_urp_mark_mp);
4889 4883 ASSERT(!IPCL_IS_NONSTR(connp));
4890 4884 /*
4891 4885 * Send up any queued data and then send the mark message
4892 4886 */
4893 4887 if (tcp->tcp_rcv_list != NULL) {
4894 4888 flags |= tcp_rcv_drain(tcp);
4895 4889
4896 4890 }
4897 4891 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg);
4898 4892 mp1 = tcp->tcp_urp_mark_mp;
4899 4893 tcp->tcp_urp_mark_mp = NULL;
4900 4894 if (is_system_labeled())
4901 4895 tcp_setcred_data(mp1, ira);
4902 4896
4903 4897 putnext(connp->conn_rq, mp1);
4904 4898 #ifdef DEBUG
4905 4899 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
4906 4900 "tcp_rput: sending zero-length %s %s",
4907 4901 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" :
4908 4902 "MSGNOTMARKNEXT"),
4909 4903 tcp_display(tcp, NULL, DISP_PORT_ONLY));
4910 4904 #endif /* DEBUG */
4911 4905 flags &= ~TH_SEND_URP_MARK;
4912 4906 }
4913 4907 if (flags & TH_ACK_NEEDED) {
4914 4908 /*
4915 4909 * Time to send an ack for some reason.
4916 4910 */
4917 4911 mp1 = tcp_ack_mp(tcp);
4918 4912
4919 4913 if (mp1 != NULL) {
4920 4914 tcp_send_data(tcp, mp1);
4921 4915 BUMP_LOCAL(tcp->tcp_obsegs);
4922 4916 TCPS_BUMP_MIB(tcps, tcpOutAck);
4923 4917 }
4924 4918 if (tcp->tcp_ack_tid != 0) {
4925 4919 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid);
4926 4920 tcp->tcp_ack_tid = 0;
4927 4921 }
4928 4922 }
4929 4923 if (flags & TH_ACK_TIMER_NEEDED) {
4930 4924 /*
4931 4925 * Arrange for deferred ACK or push wait timeout.
4932 4926 * Start timer if it is not already running.
4933 4927 */
4934 4928 if (tcp->tcp_ack_tid == 0) {
4935 4929 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer,
4936 4930 tcp->tcp_localnet ?
4937 4931 tcps->tcps_local_dack_interval :
4938 4932 tcps->tcps_deferred_ack_interval);
4939 4933 }
4940 4934 }
4941 4935 if (flags & TH_ORDREL_NEEDED) {
4942 4936 /*
4943 4937 * Notify upper layer about an orderly release. If this is
4944 4938 * a non-STREAMS socket, then just make an upcall. For STREAMS
4945 4939 * we send up an ordrel_ind, unless this is an eager, in which
4946 4940 * case the ordrel will be sent when tcp_accept_finish runs.
4947 4941 * Note that for non-STREAMS we make an upcall even if it is an
4948 4942 * eager, because we have an upper handle to send it to.
4949 4943 */
4950 4944 ASSERT(IPCL_IS_NONSTR(connp) || tcp->tcp_listener == NULL);
4951 4945 ASSERT(!tcp->tcp_detached);
4952 4946
4953 4947 if (IPCL_IS_NONSTR(connp)) {
4954 4948 ASSERT(tcp->tcp_ordrel_mp == NULL);
4955 4949 tcp->tcp_ordrel_done = B_TRUE;
4956 4950 (*sockupcalls->su_opctl)(connp->conn_upper_handle,
4957 4951 SOCK_OPCTL_SHUT_RECV, 0);
4958 4952 goto done;
4959 4953 }
4960 4954
4961 4955 if (tcp->tcp_rcv_list != NULL) {
4962 4956 /*
4963 4957 * Push any mblk(s) enqueued from co processing.
4964 4958 */
4965 4959 flags |= tcp_rcv_drain(tcp);
4966 4960 }
4967 4961 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg);
4968 4962
4969 4963 mp1 = tcp->tcp_ordrel_mp;
4970 4964 tcp->tcp_ordrel_mp = NULL;
4971 4965 tcp->tcp_ordrel_done = B_TRUE;
4972 4966 putnext(connp->conn_rq, mp1);
4973 4967 }
4974 4968 done:
4975 4969 ASSERT(!(flags & TH_MARKNEXT_NEEDED));
4976 4970 }
4977 4971
4978 4972 /*
4979 4973 * Attach ancillary data to a received TCP segments for the
4980 4974 * ancillary pieces requested by the application that are
4981 4975 * different than they were in the previous data segment.
4982 4976 *
4983 4977 * Save the "current" values once memory allocation is ok so that
4984 4978 * when memory allocation fails we can just wait for the next data segment.
4985 4979 */
4986 4980 static mblk_t *
4987 4981 tcp_input_add_ancillary(tcp_t *tcp, mblk_t *mp, ip_pkt_t *ipp,
4988 4982 ip_recv_attr_t *ira)
4989 4983 {
4990 4984 struct T_optdata_ind *todi;
4991 4985 int optlen;
4992 4986 uchar_t *optptr;
4993 4987 struct T_opthdr *toh;
4994 4988 crb_t addflag; /* Which pieces to add */
4995 4989 mblk_t *mp1;
4996 4990 conn_t *connp = tcp->tcp_connp;
4997 4991
4998 4992 optlen = 0;
4999 4993 addflag.crb_all = 0;
5000 4994 /* If app asked for pktinfo and the index has changed ... */
5001 4995 if (connp->conn_recv_ancillary.crb_ip_recvpktinfo &&
5002 4996 ira->ira_ruifindex != tcp->tcp_recvifindex) {
5003 4997 optlen += sizeof (struct T_opthdr) +
5004 4998 sizeof (struct in6_pktinfo);
5005 4999 addflag.crb_ip_recvpktinfo = 1;
5006 5000 }
5007 5001 /* If app asked for hoplimit and it has changed ... */
5008 5002 if (connp->conn_recv_ancillary.crb_ipv6_recvhoplimit &&
5009 5003 ipp->ipp_hoplimit != tcp->tcp_recvhops) {
5010 5004 optlen += sizeof (struct T_opthdr) + sizeof (uint_t);
5011 5005 addflag.crb_ipv6_recvhoplimit = 1;
5012 5006 }
5013 5007 /* If app asked for tclass and it has changed ... */
5014 5008 if (connp->conn_recv_ancillary.crb_ipv6_recvtclass &&
5015 5009 ipp->ipp_tclass != tcp->tcp_recvtclass) {
5016 5010 optlen += sizeof (struct T_opthdr) + sizeof (uint_t);
5017 5011 addflag.crb_ipv6_recvtclass = 1;
5018 5012 }
5019 5013 /*
5020 5014 * If app asked for hopbyhop headers and it has changed ...
5021 5015 * For security labels, note that (1) security labels can't change on
5022 5016 * a connected socket at all, (2) we're connected to at most one peer,
5023 5017 * (3) if anything changes, then it must be some other extra option.
5024 5018 */
5025 5019 if (connp->conn_recv_ancillary.crb_ipv6_recvhopopts &&
5026 5020 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen,
5027 5021 (ipp->ipp_fields & IPPF_HOPOPTS),
5028 5022 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) {
5029 5023 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen;
5030 5024 addflag.crb_ipv6_recvhopopts = 1;
5031 5025 if (!ip_allocbuf((void **)&tcp->tcp_hopopts,
5032 5026 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS),
5033 5027 ipp->ipp_hopopts, ipp->ipp_hopoptslen))
5034 5028 return (mp);
5035 5029 }
5036 5030 /* If app asked for dst headers before routing headers ... */
5037 5031 if (connp->conn_recv_ancillary.crb_ipv6_recvrthdrdstopts &&
5038 5032 ip_cmpbuf(tcp->tcp_rthdrdstopts, tcp->tcp_rthdrdstoptslen,
5039 5033 (ipp->ipp_fields & IPPF_RTHDRDSTOPTS),
5040 5034 ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen)) {
5041 5035 optlen += sizeof (struct T_opthdr) +
5042 5036 ipp->ipp_rthdrdstoptslen;
5043 5037 addflag.crb_ipv6_recvrthdrdstopts = 1;
5044 5038 if (!ip_allocbuf((void **)&tcp->tcp_rthdrdstopts,
5045 5039 &tcp->tcp_rthdrdstoptslen,
5046 5040 (ipp->ipp_fields & IPPF_RTHDRDSTOPTS),
5047 5041 ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen))
5048 5042 return (mp);
5049 5043 }
5050 5044 /* If app asked for routing headers and it has changed ... */
5051 5045 if (connp->conn_recv_ancillary.crb_ipv6_recvrthdr &&
5052 5046 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen,
5053 5047 (ipp->ipp_fields & IPPF_RTHDR),
5054 5048 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) {
5055 5049 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen;
5056 5050 addflag.crb_ipv6_recvrthdr = 1;
5057 5051 if (!ip_allocbuf((void **)&tcp->tcp_rthdr,
5058 5052 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR),
5059 5053 ipp->ipp_rthdr, ipp->ipp_rthdrlen))
5060 5054 return (mp);
5061 5055 }
5062 5056 /* If app asked for dest headers and it has changed ... */
5063 5057 if ((connp->conn_recv_ancillary.crb_ipv6_recvdstopts ||
5064 5058 connp->conn_recv_ancillary.crb_old_ipv6_recvdstopts) &&
5065 5059 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen,
5066 5060 (ipp->ipp_fields & IPPF_DSTOPTS),
5067 5061 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) {
5068 5062 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen;
5069 5063 addflag.crb_ipv6_recvdstopts = 1;
5070 5064 if (!ip_allocbuf((void **)&tcp->tcp_dstopts,
5071 5065 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS),
5072 5066 ipp->ipp_dstopts, ipp->ipp_dstoptslen))
5073 5067 return (mp);
5074 5068 }
5075 5069
5076 5070 if (optlen == 0) {
5077 5071 /* Nothing to add */
5078 5072 return (mp);
5079 5073 }
5080 5074 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED);
5081 5075 if (mp1 == NULL) {
5082 5076 /*
5083 5077 * Defer sending ancillary data until the next TCP segment
5084 5078 * arrives.
5085 5079 */
5086 5080 return (mp);
5087 5081 }
5088 5082 mp1->b_cont = mp;
5089 5083 mp = mp1;
5090 5084 mp->b_wptr += sizeof (*todi) + optlen;
5091 5085 mp->b_datap->db_type = M_PROTO;
5092 5086 todi = (struct T_optdata_ind *)mp->b_rptr;
5093 5087 todi->PRIM_type = T_OPTDATA_IND;
5094 5088 todi->DATA_flag = 1; /* MORE data */
5095 5089 todi->OPT_length = optlen;
5096 5090 todi->OPT_offset = sizeof (*todi);
5097 5091 optptr = (uchar_t *)&todi[1];
5098 5092 /*
5099 5093 * If app asked for pktinfo and the index has changed ...
5100 5094 * Note that the local address never changes for the connection.
5101 5095 */
5102 5096 if (addflag.crb_ip_recvpktinfo) {
5103 5097 struct in6_pktinfo *pkti;
5104 5098 uint_t ifindex;
5105 5099
5106 5100 ifindex = ira->ira_ruifindex;
5107 5101 toh = (struct T_opthdr *)optptr;
5108 5102 toh->level = IPPROTO_IPV6;
5109 5103 toh->name = IPV6_PKTINFO;
5110 5104 toh->len = sizeof (*toh) + sizeof (*pkti);
5111 5105 toh->status = 0;
5112 5106 optptr += sizeof (*toh);
5113 5107 pkti = (struct in6_pktinfo *)optptr;
5114 5108 pkti->ipi6_addr = connp->conn_laddr_v6;
5115 5109 pkti->ipi6_ifindex = ifindex;
5116 5110 optptr += sizeof (*pkti);
5117 5111 ASSERT(OK_32PTR(optptr));
5118 5112 /* Save as "last" value */
5119 5113 tcp->tcp_recvifindex = ifindex;
5120 5114 }
5121 5115 /* If app asked for hoplimit and it has changed ... */
5122 5116 if (addflag.crb_ipv6_recvhoplimit) {
5123 5117 toh = (struct T_opthdr *)optptr;
5124 5118 toh->level = IPPROTO_IPV6;
5125 5119 toh->name = IPV6_HOPLIMIT;
5126 5120 toh->len = sizeof (*toh) + sizeof (uint_t);
5127 5121 toh->status = 0;
5128 5122 optptr += sizeof (*toh);
5129 5123 *(uint_t *)optptr = ipp->ipp_hoplimit;
5130 5124 optptr += sizeof (uint_t);
5131 5125 ASSERT(OK_32PTR(optptr));
5132 5126 /* Save as "last" value */
5133 5127 tcp->tcp_recvhops = ipp->ipp_hoplimit;
5134 5128 }
5135 5129 /* If app asked for tclass and it has changed ... */
5136 5130 if (addflag.crb_ipv6_recvtclass) {
5137 5131 toh = (struct T_opthdr *)optptr;
5138 5132 toh->level = IPPROTO_IPV6;
5139 5133 toh->name = IPV6_TCLASS;
5140 5134 toh->len = sizeof (*toh) + sizeof (uint_t);
5141 5135 toh->status = 0;
5142 5136 optptr += sizeof (*toh);
5143 5137 *(uint_t *)optptr = ipp->ipp_tclass;
5144 5138 optptr += sizeof (uint_t);
5145 5139 ASSERT(OK_32PTR(optptr));
5146 5140 /* Save as "last" value */
5147 5141 tcp->tcp_recvtclass = ipp->ipp_tclass;
5148 5142 }
5149 5143 if (addflag.crb_ipv6_recvhopopts) {
5150 5144 toh = (struct T_opthdr *)optptr;
5151 5145 toh->level = IPPROTO_IPV6;
5152 5146 toh->name = IPV6_HOPOPTS;
5153 5147 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen;
5154 5148 toh->status = 0;
5155 5149 optptr += sizeof (*toh);
5156 5150 bcopy((uchar_t *)ipp->ipp_hopopts, optptr, ipp->ipp_hopoptslen);
5157 5151 optptr += ipp->ipp_hopoptslen;
5158 5152 ASSERT(OK_32PTR(optptr));
5159 5153 /* Save as last value */
5160 5154 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen,
5161 5155 (ipp->ipp_fields & IPPF_HOPOPTS),
5162 5156 ipp->ipp_hopopts, ipp->ipp_hopoptslen);
5163 5157 }
5164 5158 if (addflag.crb_ipv6_recvrthdrdstopts) {
5165 5159 toh = (struct T_opthdr *)optptr;
5166 5160 toh->level = IPPROTO_IPV6;
5167 5161 toh->name = IPV6_RTHDRDSTOPTS;
5168 5162 toh->len = sizeof (*toh) + ipp->ipp_rthdrdstoptslen;
5169 5163 toh->status = 0;
5170 5164 optptr += sizeof (*toh);
5171 5165 bcopy(ipp->ipp_rthdrdstopts, optptr, ipp->ipp_rthdrdstoptslen);
5172 5166 optptr += ipp->ipp_rthdrdstoptslen;
5173 5167 ASSERT(OK_32PTR(optptr));
5174 5168 /* Save as last value */
5175 5169 ip_savebuf((void **)&tcp->tcp_rthdrdstopts,
5176 5170 &tcp->tcp_rthdrdstoptslen,
5177 5171 (ipp->ipp_fields & IPPF_RTHDRDSTOPTS),
5178 5172 ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen);
5179 5173 }
5180 5174 if (addflag.crb_ipv6_recvrthdr) {
5181 5175 toh = (struct T_opthdr *)optptr;
5182 5176 toh->level = IPPROTO_IPV6;
5183 5177 toh->name = IPV6_RTHDR;
5184 5178 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen;
5185 5179 toh->status = 0;
5186 5180 optptr += sizeof (*toh);
5187 5181 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen);
5188 5182 optptr += ipp->ipp_rthdrlen;
5189 5183 ASSERT(OK_32PTR(optptr));
5190 5184 /* Save as last value */
5191 5185 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen,
5192 5186 (ipp->ipp_fields & IPPF_RTHDR),
5193 5187 ipp->ipp_rthdr, ipp->ipp_rthdrlen);
5194 5188 }
5195 5189 if (addflag.crb_ipv6_recvdstopts) {
5196 5190 toh = (struct T_opthdr *)optptr;
5197 5191 toh->level = IPPROTO_IPV6;
5198 5192 toh->name = IPV6_DSTOPTS;
5199 5193 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen;
5200 5194 toh->status = 0;
5201 5195 optptr += sizeof (*toh);
5202 5196 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen);
5203 5197 optptr += ipp->ipp_dstoptslen;
↓ open down ↓ |
315 lines elided |
↑ open up ↑ |
5204 5198 ASSERT(OK_32PTR(optptr));
5205 5199 /* Save as last value */
5206 5200 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen,
5207 5201 (ipp->ipp_fields & IPPF_DSTOPTS),
5208 5202 ipp->ipp_dstopts, ipp->ipp_dstoptslen);
5209 5203 }
5210 5204 ASSERT(optptr == mp->b_wptr);
5211 5205 return (mp);
5212 5206 }
5213 5207
5214 -/* The minimum of smoothed mean deviation in RTO calculation. */
5215 -#define TCP_SD_MIN 400
5208 +/* The minimum of smoothed mean deviation in RTO calculation (nsec). */
5209 +#define TCP_SD_MIN 400000000
5216 5210
5217 5211 /*
5218 - * Set RTO for this connection. The formula is from Jacobson and Karels'
5219 - * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names
5220 - * are the same as those in Appendix A.2 of that paper.
5212 + * Set RTO for this connection based on a new round-trip time measurement.
5213 + * The formula is from Jacobson and Karels' "Congestion Avoidance and Control"
5214 + * in SIGCOMM '88. The variable names are the same as those in Appendix A.2
5215 + * of that paper.
5221 5216 *
5222 5217 * m = new measurement
5223 5218 * sa = smoothed RTT average (8 * average estimates).
5224 5219 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates).
5225 5220 */
5226 5221 static void
5227 -tcp_set_rto(tcp_t *tcp, clock_t rtt)
5222 +tcp_set_rto(tcp_t *tcp, hrtime_t rtt)
5228 5223 {
5229 - long m = TICK_TO_MSEC(rtt);
5230 - clock_t sa = tcp->tcp_rtt_sa;
5231 - clock_t sv = tcp->tcp_rtt_sd;
5232 - clock_t rto;
5233 - tcp_stack_t *tcps = tcp->tcp_tcps;
5224 + hrtime_t m = rtt;
5225 + hrtime_t sa = tcp->tcp_rtt_sa;
5226 + hrtime_t sv = tcp->tcp_rtt_sd;
5227 + tcp_stack_t *tcps = tcp->tcp_tcps;
5234 5228
5235 5229 TCPS_BUMP_MIB(tcps, tcpRttUpdate);
5236 5230 tcp->tcp_rtt_update++;
5237 5231
5238 5232 /* tcp_rtt_sa is not 0 means this is a new sample. */
5239 5233 if (sa != 0) {
5240 5234 /*
5241 - * Update average estimator:
5242 - * new rtt = 7/8 old rtt + 1/8 Error
5235 + * Update average estimator (see section 2.3 of RFC6298):
5236 + * SRTT = 7/8 SRTT + 1/8 rtt
5237 + *
5238 + * We maintain tcp_rtt_sa as 8 * SRTT, so this reduces to:
5239 + * tcp_rtt_sa = 7 * SRTT + rtt
5240 + * tcp_rtt_sa = 7 * (tcp_rtt_sa / 8) + rtt
5241 + * tcp_rtt_sa = tcp_rtt_sa - (tcp_rtt_sa / 8) + rtt
5242 + * tcp_rtt_sa = tcp_rtt_sa + (rtt - (tcp_rtt_sa / 8))
5243 + * tcp_rtt_sa = tcp_rtt_sa + (rtt - (tcp_rtt_sa / 2^3))
5244 + * tcp_rtt_sa = tcp_rtt_sa + (rtt - (tcp_rtt_sa >> 3))
5245 + *
5246 + * (rtt - tcp_rtt_sa / 8) is simply the difference
5247 + * between the new rtt measurement and the existing smoothed
5248 + * RTT average. This is referred to as "Error" in subsequent
5249 + * calculations.
5243 5250 */
5244 5251
5245 - /* m is now Error in estimate. */
5252 + /* m is now Error. */
5246 5253 m -= sa >> 3;
5247 5254 if ((sa += m) <= 0) {
5248 5255 /*
5249 5256 * Don't allow the smoothed average to be negative.
5250 5257 * We use 0 to denote reinitialization of the
5251 5258 * variables.
5252 5259 */
5253 5260 sa = 1;
5254 5261 }
5255 5262
5256 5263 /*
5257 5264 * Update deviation estimator:
5258 - * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev)
5265 + * mdev = 3/4 mdev + 1/4 abs(Error)
5266 + *
5267 + * We maintain tcp_rtt_sd as 4 * mdev, so this reduces to:
5268 + * tcp_rtt_sd = 3 * mdev + abs(Error)
5269 + * tcp_rtt_sd = tcp_rtt_sd - (tcp_rtt_sd / 4) + abs(Error)
5270 + * tcp_rtt_sd = tcp_rtt_sd - (tcp_rtt_sd / 2^2) + abs(Error)
5271 + * tcp_rtt_sd = tcp_rtt_sd - (tcp_rtt_sd >> 2) + abs(Error)
5259 5272 */
5260 5273 if (m < 0)
5261 5274 m = -m;
5262 5275 m -= sv >> 2;
5263 5276 sv += m;
5264 5277 } else {
5265 5278 /*
5266 5279 * This follows BSD's implementation. So the reinitialized
5267 5280 * RTO is 3 * m. We cannot go less than 2 because if the
5268 5281 * link is bandwidth dominated, doubling the window size
5269 5282 * during slow start means doubling the RTT. We want to be
5270 5283 * more conservative when we reinitialize our estimates. 3
5271 5284 * is just a convenient number.
5272 5285 */
5273 5286 sa = m << 3;
5274 5287 sv = m << 1;
5275 5288 }
5276 5289 if (sv < TCP_SD_MIN) {
5277 5290 /*
5278 - * We do not know that if sa captures the delay ACK
5279 - * effect as in a long train of segments, a receiver
5280 - * does not delay its ACKs. So set the minimum of sv
5281 - * to be TCP_SD_MIN, which is default to 400 ms, twice
5282 - * of BSD DATO. That means the minimum of mean
5291 + * Since a receiver doesn't delay its ACKs during a long run of
5292 + * segments, sa may not have captured the effect of delayed ACK
5293 + * timeouts on the RTT. To make sure we always account for the
5294 + * possible delay (and avoid the unnecessary retransmission),
5295 + * TCP_SD_MIN is set to 400ms, twice the delayed ACK timeout of
5296 + * 200ms on older SunOS/BSD systems and modern Windows systems
5297 + * (as of 2019). This means that the minimum possible mean
5283 5298 * deviation is 100 ms.
5284 - *
5285 5299 */
5286 5300 sv = TCP_SD_MIN;
5287 5301 }
5288 5302 tcp->tcp_rtt_sa = sa;
5289 5303 tcp->tcp_rtt_sd = sv;
5290 - /*
5291 - * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv)
5292 - *
5293 - * Add tcp_rexmit_interval extra in case of extreme environment
5294 - * where the algorithm fails to work. The default value of
5295 - * tcp_rexmit_interval_extra should be 0.
5296 - *
5297 - * As we use a finer grained clock than BSD and update
5298 - * RTO for every ACKs, add in another .25 of RTT to the
5299 - * deviation of RTO to accomodate burstiness of 1/4 of
5300 - * window size.
5301 - */
5302 - rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5);
5303 5304
5304 - TCP_SET_RTO(tcp, rto);
5305 + tcp->tcp_rto = tcp_calculate_rto(tcp, tcps, 0);
5305 5306
5306 5307 /* Now, we can reset tcp_timer_backoff to use the new RTO... */
5307 5308 tcp->tcp_timer_backoff = 0;
5308 5309 }
5309 5310
5310 5311 /*
5311 5312 * On a labeled system we have some protocols above TCP, such as RPC, which
5312 5313 * appear to assume that every mblk in a chain has a db_credp.
5313 5314 */
5314 5315 static void
5315 5316 tcp_setcred_data(mblk_t *mp, ip_recv_attr_t *ira)
5316 5317 {
5317 5318 ASSERT(is_system_labeled());
5318 5319 ASSERT(ira->ira_cred != NULL);
5319 5320
5320 5321 while (mp != NULL) {
5321 5322 mblk_setcred(mp, ira->ira_cred, NOPID);
5322 5323 mp = mp->b_cont;
5323 5324 }
5324 5325 }
5325 5326
5326 5327 uint_t
5327 5328 tcp_rwnd_reopen(tcp_t *tcp)
5328 5329 {
5329 5330 uint_t ret = 0;
5330 5331 uint_t thwin;
5331 5332 conn_t *connp = tcp->tcp_connp;
5332 5333
5333 5334 /* Learn the latest rwnd information that we sent to the other side. */
5334 5335 thwin = ((uint_t)ntohs(tcp->tcp_tcpha->tha_win))
5335 5336 << tcp->tcp_rcv_ws;
5336 5337 /* This is peer's calculated send window (our receive window). */
5337 5338 thwin -= tcp->tcp_rnxt - tcp->tcp_rack;
5338 5339 /*
5339 5340 * Increase the receive window to max. But we need to do receiver
5340 5341 * SWS avoidance. This means that we need to check the increase of
5341 5342 * of receive window is at least 1 MSS.
5342 5343 */
5343 5344 if (connp->conn_rcvbuf - thwin >= tcp->tcp_mss) {
5344 5345 /*
5345 5346 * If the window that the other side knows is less than max
5346 5347 * deferred acks segments, send an update immediately.
5347 5348 */
5348 5349 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) {
5349 5350 TCPS_BUMP_MIB(tcp->tcp_tcps, tcpOutWinUpdate);
5350 5351 ret = TH_ACK_NEEDED;
5351 5352 }
5352 5353 tcp->tcp_rwnd = connp->conn_rcvbuf;
5353 5354 }
5354 5355 return (ret);
5355 5356 }
5356 5357
5357 5358 /*
5358 5359 * Handle a packet that has been reclassified by TCP.
5359 5360 * This function drops the ref on connp that the caller had.
5360 5361 */
5361 5362 void
5362 5363 tcp_reinput(conn_t *connp, mblk_t *mp, ip_recv_attr_t *ira, ip_stack_t *ipst)
5363 5364 {
5364 5365 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
5365 5366
5366 5367 if (connp->conn_incoming_ifindex != 0 &&
5367 5368 connp->conn_incoming_ifindex != ira->ira_ruifindex) {
5368 5369 freemsg(mp);
5369 5370 CONN_DEC_REF(connp);
5370 5371 return;
5371 5372 }
5372 5373
5373 5374 if (CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss) ||
5374 5375 (ira->ira_flags & IRAF_IPSEC_SECURE)) {
5375 5376 ip6_t *ip6h;
5376 5377 ipha_t *ipha;
5377 5378
5378 5379 if (ira->ira_flags & IRAF_IS_IPV4) {
5379 5380 ipha = (ipha_t *)mp->b_rptr;
5380 5381 ip6h = NULL;
5381 5382 } else {
5382 5383 ipha = NULL;
5383 5384 ip6h = (ip6_t *)mp->b_rptr;
5384 5385 }
5385 5386 mp = ipsec_check_inbound_policy(mp, connp, ipha, ip6h, ira);
5386 5387 if (mp == NULL) {
5387 5388 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
5388 5389 /* Note that mp is NULL */
5389 5390 ip_drop_input("ipIfStatsInDiscards", mp, NULL);
5390 5391 CONN_DEC_REF(connp);
5391 5392 return;
5392 5393 }
5393 5394 }
5394 5395
5395 5396 if (IPCL_IS_TCP(connp)) {
5396 5397 /*
5397 5398 * do not drain, certain use cases can blow
5398 5399 * the stack
5399 5400 */
5400 5401 SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
5401 5402 connp->conn_recv, connp, ira,
5402 5403 SQ_NODRAIN, SQTAG_IP_TCP_INPUT);
5403 5404 } else {
5404 5405 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
5405 5406 (connp->conn_recv)(connp, mp, NULL,
5406 5407 ira);
5407 5408 CONN_DEC_REF(connp);
5408 5409 }
5409 5410
5410 5411 }
5411 5412
5412 5413 /* ARGSUSED */
5413 5414 static void
5414 5415 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
5415 5416 {
5416 5417 conn_t *connp = (conn_t *)arg;
5417 5418 tcp_t *tcp = connp->conn_tcp;
5418 5419 queue_t *q = connp->conn_rq;
5419 5420
5420 5421 ASSERT(!IPCL_IS_NONSTR(connp));
5421 5422 mutex_enter(&tcp->tcp_rsrv_mp_lock);
5422 5423 tcp->tcp_rsrv_mp = mp;
5423 5424 mutex_exit(&tcp->tcp_rsrv_mp_lock);
5424 5425
5425 5426 if (TCP_IS_DETACHED(tcp) || q == NULL) {
5426 5427 return;
5427 5428 }
5428 5429
5429 5430 if (tcp->tcp_fused) {
5430 5431 tcp_fuse_backenable(tcp);
5431 5432 return;
5432 5433 }
5433 5434
5434 5435 if (canputnext(q)) {
5435 5436 /* Not flow-controlled, open rwnd */
5436 5437 tcp->tcp_rwnd = connp->conn_rcvbuf;
5437 5438
5438 5439 /*
5439 5440 * Send back a window update immediately if TCP is above
5440 5441 * ESTABLISHED state and the increase of the rcv window
5441 5442 * that the other side knows is at least 1 MSS after flow
5442 5443 * control is lifted.
5443 5444 */
5444 5445 if (tcp->tcp_state >= TCPS_ESTABLISHED &&
5445 5446 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) {
5446 5447 tcp_xmit_ctl(NULL, tcp,
5447 5448 (tcp->tcp_swnd == 0) ? tcp->tcp_suna :
5448 5449 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK);
5449 5450 }
5450 5451 }
5451 5452 }
5452 5453
5453 5454 /*
5454 5455 * The read side service routine is called mostly when we get back-enabled as a
5455 5456 * result of flow control relief. Since we don't actually queue anything in
5456 5457 * TCP, we have no data to send out of here. What we do is clear the receive
5457 5458 * window, and send out a window update.
5458 5459 */
5459 5460 int
5460 5461 tcp_rsrv(queue_t *q)
5461 5462 {
5462 5463 conn_t *connp = Q_TO_CONN(q);
5463 5464 tcp_t *tcp = connp->conn_tcp;
5464 5465 mblk_t *mp;
5465 5466
5466 5467 /* No code does a putq on the read side */
5467 5468 ASSERT(q->q_first == NULL);
5468 5469
5469 5470 /*
5470 5471 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already
5471 5472 * been run. So just return.
5472 5473 */
5473 5474 mutex_enter(&tcp->tcp_rsrv_mp_lock);
5474 5475 if ((mp = tcp->tcp_rsrv_mp) == NULL) {
5475 5476 mutex_exit(&tcp->tcp_rsrv_mp_lock);
5476 5477 return (0);
5477 5478 }
5478 5479 tcp->tcp_rsrv_mp = NULL;
5479 5480 mutex_exit(&tcp->tcp_rsrv_mp_lock);
5480 5481
5481 5482 CONN_INC_REF(connp);
5482 5483 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp,
5483 5484 NULL, SQ_PROCESS, SQTAG_TCP_RSRV);
5484 5485 return (0);
5485 5486 }
5486 5487
5487 5488 /* At minimum we need 8 bytes in the TCP header for the lookup */
5488 5489 #define ICMP_MIN_TCP_HDR 8
5489 5490
5490 5491 /*
5491 5492 * tcp_icmp_input is called as conn_recvicmp to process ICMP error messages
5492 5493 * passed up by IP. The message is always received on the correct tcp_t.
5493 5494 * Assumes that IP has pulled up everything up to and including the ICMP header.
5494 5495 */
5495 5496 /* ARGSUSED2 */
5496 5497 void
5497 5498 tcp_icmp_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
5498 5499 {
5499 5500 conn_t *connp = (conn_t *)arg1;
5500 5501 icmph_t *icmph;
5501 5502 ipha_t *ipha;
5502 5503 int iph_hdr_length;
5503 5504 tcpha_t *tcpha;
5504 5505 uint32_t seg_seq;
5505 5506 tcp_t *tcp = connp->conn_tcp;
5506 5507
5507 5508 /* Assume IP provides aligned packets */
5508 5509 ASSERT(OK_32PTR(mp->b_rptr));
5509 5510 ASSERT((MBLKL(mp) >= sizeof (ipha_t)));
5510 5511
5511 5512 /*
5512 5513 * It's possible we have a closed, but not yet destroyed, TCP
5513 5514 * connection. Several fields (e.g. conn_ixa->ixa_ire) are invalid
5514 5515 * in the closed state, so don't take any chances and drop the packet.
5515 5516 */
5516 5517 if (tcp->tcp_state == TCPS_CLOSED) {
5517 5518 freemsg(mp);
5518 5519 return;
5519 5520 }
5520 5521
5521 5522 /*
5522 5523 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent
5523 5524 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6.
5524 5525 */
5525 5526 if (!(ira->ira_flags & IRAF_IS_IPV4)) {
5526 5527 tcp_icmp_error_ipv6(tcp, mp, ira);
5527 5528 return;
5528 5529 }
5529 5530
5530 5531 /* Skip past the outer IP and ICMP headers */
5531 5532 iph_hdr_length = ira->ira_ip_hdr_length;
5532 5533 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
5533 5534 /*
5534 5535 * If we don't have the correct outer IP header length
5535 5536 * or if we don't have a complete inner IP header
5536 5537 * drop it.
5537 5538 */
5538 5539 if (iph_hdr_length < sizeof (ipha_t) ||
5539 5540 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) {
5540 5541 noticmpv4:
5541 5542 freemsg(mp);
5542 5543 return;
5543 5544 }
5544 5545 ipha = (ipha_t *)&icmph[1];
5545 5546
5546 5547 /* Skip past the inner IP and find the ULP header */
5547 5548 iph_hdr_length = IPH_HDR_LENGTH(ipha);
5548 5549 tcpha = (tcpha_t *)((char *)ipha + iph_hdr_length);
5549 5550 /*
5550 5551 * If we don't have the correct inner IP header length or if the ULP
5551 5552 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR
5552 5553 * bytes of TCP header, drop it.
5553 5554 */
5554 5555 if (iph_hdr_length < sizeof (ipha_t) ||
5555 5556 ipha->ipha_protocol != IPPROTO_TCP ||
5556 5557 (uchar_t *)tcpha + ICMP_MIN_TCP_HDR > mp->b_wptr) {
5557 5558 goto noticmpv4;
5558 5559 }
5559 5560
5560 5561 seg_seq = ntohl(tcpha->tha_seq);
5561 5562 switch (icmph->icmph_type) {
5562 5563 case ICMP_DEST_UNREACHABLE:
5563 5564 switch (icmph->icmph_code) {
5564 5565 case ICMP_FRAGMENTATION_NEEDED:
5565 5566 /*
5566 5567 * Update Path MTU, then try to send something out.
5567 5568 */
5568 5569 tcp_update_pmtu(tcp, B_TRUE);
5569 5570 tcp_rexmit_after_error(tcp);
5570 5571 break;
5571 5572 case ICMP_PORT_UNREACHABLE:
5572 5573 case ICMP_PROTOCOL_UNREACHABLE:
5573 5574 switch (tcp->tcp_state) {
5574 5575 case TCPS_SYN_SENT:
5575 5576 case TCPS_SYN_RCVD:
5576 5577 /*
5577 5578 * ICMP can snipe away incipient
5578 5579 * TCP connections as long as
5579 5580 * seq number is same as initial
5580 5581 * send seq number.
5581 5582 */
5582 5583 if (seg_seq == tcp->tcp_iss) {
5583 5584 (void) tcp_clean_death(tcp,
5584 5585 ECONNREFUSED);
5585 5586 }
5586 5587 break;
5587 5588 }
5588 5589 break;
5589 5590 case ICMP_HOST_UNREACHABLE:
5590 5591 case ICMP_NET_UNREACHABLE:
5591 5592 /* Record the error in case we finally time out. */
5592 5593 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE)
5593 5594 tcp->tcp_client_errno = EHOSTUNREACH;
5594 5595 else
5595 5596 tcp->tcp_client_errno = ENETUNREACH;
5596 5597 if (tcp->tcp_state == TCPS_SYN_RCVD) {
5597 5598 if (tcp->tcp_listener != NULL &&
5598 5599 tcp->tcp_listener->tcp_syn_defense) {
5599 5600 /*
5600 5601 * Ditch the half-open connection if we
5601 5602 * suspect a SYN attack is under way.
5602 5603 */
5603 5604 (void) tcp_clean_death(tcp,
5604 5605 tcp->tcp_client_errno);
5605 5606 }
5606 5607 }
5607 5608 break;
5608 5609 default:
5609 5610 break;
5610 5611 }
5611 5612 break;
5612 5613 case ICMP_SOURCE_QUENCH: {
5613 5614 /*
5614 5615 * use a global boolean to control
5615 5616 * whether TCP should respond to ICMP_SOURCE_QUENCH.
5616 5617 * The default is false.
5617 5618 */
5618 5619 if (tcp_icmp_source_quench) {
5619 5620 /*
5620 5621 * Reduce the sending rate as if we got a
5621 5622 * retransmit timeout
5622 5623 */
5623 5624 uint32_t npkt;
5624 5625
5625 5626 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) /
5626 5627 tcp->tcp_mss;
5627 5628 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss;
5628 5629 tcp->tcp_cwnd = tcp->tcp_mss;
5629 5630 tcp->tcp_cwnd_cnt = 0;
5630 5631 }
5631 5632 break;
5632 5633 }
5633 5634 }
5634 5635 freemsg(mp);
5635 5636 }
5636 5637
5637 5638 /*
5638 5639 * tcp_icmp_error_ipv6 is called from tcp_icmp_input to process ICMPv6
5639 5640 * error messages passed up by IP.
5640 5641 * Assumes that IP has pulled up all the extension headers as well
5641 5642 * as the ICMPv6 header.
5642 5643 */
5643 5644 static void
5644 5645 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, ip_recv_attr_t *ira)
5645 5646 {
5646 5647 icmp6_t *icmp6;
5647 5648 ip6_t *ip6h;
5648 5649 uint16_t iph_hdr_length = ira->ira_ip_hdr_length;
5649 5650 tcpha_t *tcpha;
5650 5651 uint8_t *nexthdrp;
5651 5652 uint32_t seg_seq;
5652 5653
5653 5654 /*
5654 5655 * Verify that we have a complete IP header.
5655 5656 */
5656 5657 ASSERT((MBLKL(mp) >= sizeof (ip6_t)));
5657 5658
5658 5659 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
5659 5660 ip6h = (ip6_t *)&icmp6[1];
5660 5661 /*
5661 5662 * Verify if we have a complete ICMP and inner IP header.
5662 5663 */
5663 5664 if ((uchar_t *)&ip6h[1] > mp->b_wptr) {
5664 5665 noticmpv6:
5665 5666 freemsg(mp);
5666 5667 return;
5667 5668 }
5668 5669
5669 5670 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp))
5670 5671 goto noticmpv6;
5671 5672 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length);
5672 5673 /*
5673 5674 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't
5674 5675 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the
5675 5676 * packet.
5676 5677 */
5677 5678 if ((*nexthdrp != IPPROTO_TCP) ||
5678 5679 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) {
5679 5680 goto noticmpv6;
5680 5681 }
5681 5682
5682 5683 seg_seq = ntohl(tcpha->tha_seq);
5683 5684 switch (icmp6->icmp6_type) {
5684 5685 case ICMP6_PACKET_TOO_BIG:
5685 5686 /*
5686 5687 * Update Path MTU, then try to send something out.
5687 5688 */
5688 5689 tcp_update_pmtu(tcp, B_TRUE);
5689 5690 tcp_rexmit_after_error(tcp);
5690 5691 break;
5691 5692 case ICMP6_DST_UNREACH:
5692 5693 switch (icmp6->icmp6_code) {
5693 5694 case ICMP6_DST_UNREACH_NOPORT:
5694 5695 if (((tcp->tcp_state == TCPS_SYN_SENT) ||
5695 5696 (tcp->tcp_state == TCPS_SYN_RCVD)) &&
5696 5697 (seg_seq == tcp->tcp_iss)) {
5697 5698 (void) tcp_clean_death(tcp, ECONNREFUSED);
5698 5699 }
5699 5700 break;
5700 5701 case ICMP6_DST_UNREACH_ADMIN:
5701 5702 case ICMP6_DST_UNREACH_NOROUTE:
5702 5703 case ICMP6_DST_UNREACH_BEYONDSCOPE:
5703 5704 case ICMP6_DST_UNREACH_ADDR:
5704 5705 /* Record the error in case we finally time out. */
5705 5706 tcp->tcp_client_errno = EHOSTUNREACH;
5706 5707 if (((tcp->tcp_state == TCPS_SYN_SENT) ||
5707 5708 (tcp->tcp_state == TCPS_SYN_RCVD)) &&
5708 5709 (seg_seq == tcp->tcp_iss)) {
5709 5710 if (tcp->tcp_listener != NULL &&
5710 5711 tcp->tcp_listener->tcp_syn_defense) {
5711 5712 /*
5712 5713 * Ditch the half-open connection if we
5713 5714 * suspect a SYN attack is under way.
5714 5715 */
5715 5716 (void) tcp_clean_death(tcp,
5716 5717 tcp->tcp_client_errno);
5717 5718 }
5718 5719 }
5719 5720
5720 5721
5721 5722 break;
5722 5723 default:
5723 5724 break;
5724 5725 }
5725 5726 break;
5726 5727 case ICMP6_PARAM_PROB:
5727 5728 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
5728 5729 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
5729 5730 (uchar_t *)ip6h + icmp6->icmp6_pptr ==
5730 5731 (uchar_t *)nexthdrp) {
5731 5732 if (tcp->tcp_state == TCPS_SYN_SENT ||
5732 5733 tcp->tcp_state == TCPS_SYN_RCVD) {
5733 5734 (void) tcp_clean_death(tcp, ECONNREFUSED);
5734 5735 }
5735 5736 break;
5736 5737 }
5737 5738 break;
5738 5739
5739 5740 case ICMP6_TIME_EXCEEDED:
5740 5741 default:
5741 5742 break;
5742 5743 }
5743 5744 freemsg(mp);
5744 5745 }
5745 5746
5746 5747 /*
5747 5748 * CALLED OUTSIDE OF SQUEUE! It can not follow any pointers that tcp might
5748 5749 * change. But it can refer to fields like tcp_suna and tcp_snxt.
5749 5750 *
5750 5751 * Function tcp_verifyicmp is called as conn_verifyicmp to verify the ICMP
5751 5752 * error messages received by IP. The message is always received on the correct
5752 5753 * tcp_t.
5753 5754 */
5754 5755 /* ARGSUSED */
5755 5756 boolean_t
5756 5757 tcp_verifyicmp(conn_t *connp, void *arg2, icmph_t *icmph, icmp6_t *icmp6,
5757 5758 ip_recv_attr_t *ira)
5758 5759 {
5759 5760 tcpha_t *tcpha = (tcpha_t *)arg2;
5760 5761 uint32_t seq = ntohl(tcpha->tha_seq);
5761 5762 tcp_t *tcp = connp->conn_tcp;
5762 5763
5763 5764 /*
5764 5765 * TCP sequence number contained in payload of the ICMP error message
5765 5766 * should be within the range SND.UNA <= SEG.SEQ < SND.NXT. Otherwise,
5766 5767 * the message is either a stale ICMP error, or an attack from the
5767 5768 * network. Fail the verification.
5768 5769 */
5769 5770 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt))
5770 5771 return (B_FALSE);
5771 5772
5772 5773 /* For "too big" we also check the ignore flag */
5773 5774 if (ira->ira_flags & IRAF_IS_IPV4) {
5774 5775 ASSERT(icmph != NULL);
5775 5776 if (icmph->icmph_type == ICMP_DEST_UNREACHABLE &&
5776 5777 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED &&
5777 5778 tcp->tcp_tcps->tcps_ignore_path_mtu)
5778 5779 return (B_FALSE);
5779 5780 } else {
5780 5781 ASSERT(icmp6 != NULL);
5781 5782 if (icmp6->icmp6_type == ICMP6_PACKET_TOO_BIG &&
5782 5783 tcp->tcp_tcps->tcps_ignore_path_mtu)
5783 5784 return (B_FALSE);
5784 5785 }
5785 5786 return (B_TRUE);
5786 5787 }
↓ open down ↓ |
472 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX