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