Print this page
1426 1361 missed two codepaths
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/inet/tcp/tcp.c
+++ new/usr/src/uts/common/inet/tcp/tcp.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
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) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 2011, Joyent Inc. All rights reserved.
25 25 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
26 26 */
27 27 /* Copyright (c) 1990 Mentat Inc. */
28 28
29 29 #include <sys/types.h>
30 30 #include <sys/stream.h>
31 31 #include <sys/strsun.h>
32 32 #include <sys/strsubr.h>
33 33 #include <sys/stropts.h>
34 34 #include <sys/strlog.h>
35 35 #define _SUN_TPI_VERSION 2
36 36 #include <sys/tihdr.h>
37 37 #include <sys/timod.h>
38 38 #include <sys/ddi.h>
39 39 #include <sys/sunddi.h>
40 40 #include <sys/suntpi.h>
41 41 #include <sys/xti_inet.h>
42 42 #include <sys/cmn_err.h>
43 43 #include <sys/debug.h>
44 44 #include <sys/sdt.h>
45 45 #include <sys/vtrace.h>
46 46 #include <sys/kmem.h>
47 47 #include <sys/ethernet.h>
48 48 #include <sys/cpuvar.h>
49 49 #include <sys/dlpi.h>
50 50 #include <sys/pattr.h>
51 51 #include <sys/policy.h>
52 52 #include <sys/priv.h>
53 53 #include <sys/zone.h>
54 54 #include <sys/sunldi.h>
55 55
56 56 #include <sys/errno.h>
57 57 #include <sys/signal.h>
58 58 #include <sys/socket.h>
59 59 #include <sys/socketvar.h>
60 60 #include <sys/sockio.h>
61 61 #include <sys/isa_defs.h>
62 62 #include <sys/md5.h>
63 63 #include <sys/random.h>
64 64 #include <sys/uio.h>
65 65 #include <sys/systm.h>
66 66 #include <netinet/in.h>
67 67 #include <netinet/tcp.h>
68 68 #include <netinet/ip6.h>
69 69 #include <netinet/icmp6.h>
70 70 #include <net/if.h>
71 71 #include <net/route.h>
72 72 #include <inet/ipsec_impl.h>
73 73
74 74 #include <inet/common.h>
75 75 #include <inet/ip.h>
76 76 #include <inet/ip_impl.h>
77 77 #include <inet/ip6.h>
78 78 #include <inet/ip_ndp.h>
79 79 #include <inet/proto_set.h>
80 80 #include <inet/mib2.h>
81 81 #include <inet/optcom.h>
82 82 #include <inet/snmpcom.h>
83 83 #include <inet/kstatcom.h>
84 84 #include <inet/tcp.h>
85 85 #include <inet/tcp_impl.h>
86 86 #include <inet/tcp_cluster.h>
87 87 #include <inet/udp_impl.h>
88 88 #include <net/pfkeyv2.h>
89 89 #include <inet/ipdrop.h>
90 90
91 91 #include <inet/ipclassifier.h>
92 92 #include <inet/ip_ire.h>
93 93 #include <inet/ip_ftable.h>
94 94 #include <inet/ip_if.h>
95 95 #include <inet/ipp_common.h>
96 96 #include <inet/ip_rts.h>
97 97 #include <inet/ip_netinfo.h>
98 98 #include <sys/squeue_impl.h>
99 99 #include <sys/squeue.h>
100 100 #include <sys/tsol/label.h>
101 101 #include <sys/tsol/tnet.h>
102 102 #include <rpc/pmap_prot.h>
103 103 #include <sys/callo.h>
104 104
105 105 /*
106 106 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433)
107 107 *
108 108 * (Read the detailed design doc in PSARC case directory)
109 109 *
110 110 * The entire tcp state is contained in tcp_t and conn_t structure
111 111 * which are allocated in tandem using ipcl_conn_create() and passing
112 112 * IPCL_TCPCONN as a flag. We use 'conn_ref' and 'conn_lock' to protect
113 113 * the references on the tcp_t. The tcp_t structure is never compressed
114 114 * and packets always land on the correct TCP perimeter from the time
115 115 * eager is created till the time tcp_t dies (as such the old mentat
116 116 * TCP global queue is not used for detached state and no IPSEC checking
117 117 * is required). The global queue is still allocated to send out resets
118 118 * for connection which have no listeners and IP directly calls
119 119 * tcp_xmit_listeners_reset() which does any policy check.
120 120 *
121 121 * Protection and Synchronisation mechanism:
122 122 *
123 123 * The tcp data structure does not use any kind of lock for protecting
124 124 * its state but instead uses 'squeues' for mutual exclusion from various
125 125 * read and write side threads. To access a tcp member, the thread should
126 126 * always be behind squeue (via squeue_enter with flags as SQ_FILL, SQ_PROCESS,
127 127 * or SQ_NODRAIN). Since the squeues allow a direct function call, caller
128 128 * can pass any tcp function having prototype of edesc_t as argument
129 129 * (different from traditional STREAMs model where packets come in only
130 130 * designated entry points). The list of functions that can be directly
131 131 * called via squeue are listed before the usual function prototype.
132 132 *
133 133 * Referencing:
134 134 *
135 135 * TCP is MT-Hot and we use a reference based scheme to make sure that the
136 136 * tcp structure doesn't disappear when its needed. When the application
137 137 * creates an outgoing connection or accepts an incoming connection, we
138 138 * start out with 2 references on 'conn_ref'. One for TCP and one for IP.
139 139 * The IP reference is just a symbolic reference since ip_tcpclose()
140 140 * looks at tcp structure after tcp_close_output() returns which could
141 141 * have dropped the last TCP reference. So as long as the connection is
142 142 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the
143 143 * conn_t. The classifier puts its own reference when the connection is
144 144 * inserted in listen or connected hash. Anytime a thread needs to enter
145 145 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr
146 146 * on write side or by doing a classify on read side and then puts a
147 147 * reference on the conn before doing squeue_enter/tryenter/fill. For
148 148 * read side, the classifier itself puts the reference under fanout lock
149 149 * to make sure that tcp can't disappear before it gets processed. The
150 150 * squeue will drop this reference automatically so the called function
151 151 * doesn't have to do a DEC_REF.
152 152 *
153 153 * Opening a new connection:
154 154 *
155 155 * The outgoing connection open is pretty simple. tcp_open() does the
156 156 * work in creating the conn/tcp structure and initializing it. The
157 157 * squeue assignment is done based on the CPU the application
158 158 * is running on. So for outbound connections, processing is always done
159 159 * on application CPU which might be different from the incoming CPU
160 160 * being interrupted by the NIC. An optimal way would be to figure out
161 161 * the NIC <-> CPU binding at listen time, and assign the outgoing
162 162 * connection to the squeue attached to the CPU that will be interrupted
163 163 * for incoming packets (we know the NIC based on the bind IP address).
164 164 * This might seem like a problem if more data is going out but the
165 165 * fact is that in most cases the transmit is ACK driven transmit where
166 166 * the outgoing data normally sits on TCP's xmit queue waiting to be
167 167 * transmitted.
168 168 *
169 169 * Accepting a connection:
170 170 *
171 171 * This is a more interesting case because of various races involved in
172 172 * establishing a eager in its own perimeter. Read the meta comment on
173 173 * top of tcp_input_listener(). But briefly, the squeue is picked by
174 174 * ip_fanout based on the ring or the sender (if loopback).
175 175 *
176 176 * Closing a connection:
177 177 *
178 178 * The close is fairly straight forward. tcp_close() calls tcp_close_output()
179 179 * via squeue to do the close and mark the tcp as detached if the connection
180 180 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its
181 181 * reference but tcp_close() drop IP's reference always. So if tcp was
182 182 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP
183 183 * and 1 because it is in classifier's connected hash. This is the condition
184 184 * we use to determine that its OK to clean up the tcp outside of squeue
185 185 * when time wait expires (check the ref under fanout and conn_lock and
186 186 * if it is 2, remove it from fanout hash and kill it).
187 187 *
188 188 * Although close just drops the necessary references and marks the
189 189 * tcp_detached state, tcp_close needs to know the tcp_detached has been
190 190 * set (under squeue) before letting the STREAM go away (because a
191 191 * inbound packet might attempt to go up the STREAM while the close
192 192 * has happened and tcp_detached is not set). So a special lock and
193 193 * flag is used along with a condition variable (tcp_closelock, tcp_closed,
194 194 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked
195 195 * tcp_detached.
196 196 *
197 197 * Special provisions and fast paths:
198 198 *
199 199 * We make special provisions for sockfs by marking tcp_issocket
200 200 * whenever we have only sockfs on top of TCP. This allows us to skip
201 201 * putting the tcp in acceptor hash since a sockfs listener can never
202 202 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM
203 203 * since eager has already been allocated and the accept now happens
204 204 * on acceptor STREAM. There is a big blob of comment on top of
205 205 * tcp_input_listener explaining the new accept. When socket is POP'd,
206 206 * sockfs sends us an ioctl to mark the fact and we go back to old
207 207 * behaviour. Once tcp_issocket is unset, its never set for the
208 208 * life of that connection.
209 209 *
210 210 * IPsec notes :
211 211 *
212 212 * Since a packet is always executed on the correct TCP perimeter
213 213 * all IPsec processing is defered to IP including checking new
214 214 * connections and setting IPSEC policies for new connection. The
215 215 * only exception is tcp_xmit_listeners_reset() which is called
216 216 * directly from IP and needs to policy check to see if TH_RST
217 217 * can be sent out.
218 218 */
219 219
220 220 /*
221 221 * Values for squeue switch:
222 222 * 1: SQ_NODRAIN
223 223 * 2: SQ_PROCESS
224 224 * 3: SQ_FILL
225 225 */
226 226 int tcp_squeue_wput = 2; /* /etc/systems */
227 227 int tcp_squeue_flag;
228 228
229 229 /*
230 230 * To prevent memory hog, limit the number of entries in tcp_free_list
231 231 * to 1% of available memory / number of cpus
232 232 */
233 233 uint_t tcp_free_list_max_cnt = 0;
234 234
235 235 #define TCP_XMIT_LOWATER 4096
236 236 #define TCP_XMIT_HIWATER 49152
237 237 #define TCP_RECV_LOWATER 2048
238 238 #define TCP_RECV_HIWATER 128000
239 239
240 240 #define TIDUSZ 4096 /* transport interface data unit size */
241 241
242 242 /*
243 243 * Size of acceptor hash list. It has to be a power of 2 for hashing.
244 244 */
245 245 #define TCP_ACCEPTOR_FANOUT_SIZE 512
246 246
247 247 #ifdef _ILP32
248 248 #define TCP_ACCEPTOR_HASH(accid) \
249 249 (((uint_t)(accid) >> 8) & (TCP_ACCEPTOR_FANOUT_SIZE - 1))
250 250 #else
251 251 #define TCP_ACCEPTOR_HASH(accid) \
252 252 ((uint_t)(accid) & (TCP_ACCEPTOR_FANOUT_SIZE - 1))
253 253 #endif /* _ILP32 */
254 254
255 255 /*
256 256 * Minimum number of connections which can be created per listener. Used
257 257 * when the listener connection count is in effect.
258 258 */
259 259 static uint32_t tcp_min_conn_listener = 2;
260 260
261 261 uint32_t tcp_early_abort = 30;
262 262
263 263 /* TCP Timer control structure */
264 264 typedef struct tcpt_s {
265 265 pfv_t tcpt_pfv; /* The routine we are to call */
266 266 tcp_t *tcpt_tcp; /* The parameter we are to pass in */
267 267 } tcpt_t;
268 268
269 269 /*
270 270 * Functions called directly via squeue having a prototype of edesc_t.
271 271 */
272 272 void tcp_input_listener(void *arg, mblk_t *mp, void *arg2,
273 273 ip_recv_attr_t *ira);
274 274 void tcp_input_data(void *arg, mblk_t *mp, void *arg2,
275 275 ip_recv_attr_t *ira);
276 276 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2,
277 277 ip_recv_attr_t *dummy);
278 278
279 279
280 280 /* Prototype for TCP functions */
281 281 static void tcp_random_init(void);
282 282 int tcp_random(void);
283 283 static int tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp,
284 284 in_port_t dstport, uint_t srcid);
285 285 static int tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp,
286 286 in_port_t dstport, uint32_t flowinfo,
287 287 uint_t srcid, uint32_t scope_id);
288 288 static void tcp_iss_init(tcp_t *tcp);
289 289 static void tcp_reinit(tcp_t *tcp);
290 290 static void tcp_reinit_values(tcp_t *tcp);
291 291
292 292 static void tcp_wsrv(queue_t *q);
293 293 static void tcp_update_lso(tcp_t *tcp, ip_xmit_attr_t *ixa);
294 294 static void tcp_update_zcopy(tcp_t *tcp);
295 295 static void tcp_notify(void *, ip_xmit_attr_t *, ixa_notify_type_t,
296 296 ixa_notify_arg_t);
297 297 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns);
298 298 static void tcp_stack_fini(netstackid_t stackid, void *arg);
299 299
300 300 static int tcp_squeue_switch(int);
301 301
302 302 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t);
303 303 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *);
304 304 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *);
305 305
306 306 static void tcp_squeue_add(squeue_t *);
307 307
308 308 struct module_info tcp_rinfo = {
309 309 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER
310 310 };
311 311
312 312 static struct module_info tcp_winfo = {
313 313 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16
314 314 };
315 315
316 316 /*
317 317 * Entry points for TCP as a device. The normal case which supports
318 318 * the TCP functionality.
319 319 * We have separate open functions for the /dev/tcp and /dev/tcp6 devices.
320 320 */
321 321 struct qinit tcp_rinitv4 = {
322 322 NULL, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, NULL, &tcp_rinfo
323 323 };
324 324
325 325 struct qinit tcp_rinitv6 = {
326 326 NULL, (pfi_t)tcp_rsrv, tcp_openv6, tcp_tpi_close, NULL, &tcp_rinfo
327 327 };
328 328
329 329 struct qinit tcp_winit = {
330 330 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo
331 331 };
332 332
333 333 /* Initial entry point for TCP in socket mode. */
334 334 struct qinit tcp_sock_winit = {
335 335 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo
336 336 };
337 337
338 338 /* TCP entry point during fallback */
339 339 struct qinit tcp_fallback_sock_winit = {
340 340 (pfi_t)tcp_wput_fallback, NULL, NULL, NULL, NULL, &tcp_winfo
341 341 };
342 342
343 343 /*
344 344 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing
345 345 * an accept. Avoid allocating data structures since eager has already
346 346 * been created.
347 347 */
348 348 struct qinit tcp_acceptor_rinit = {
349 349 NULL, (pfi_t)tcp_rsrv, NULL, tcp_tpi_close_accept, NULL, &tcp_winfo
350 350 };
351 351
352 352 struct qinit tcp_acceptor_winit = {
353 353 (pfi_t)tcp_tpi_accept, NULL, NULL, NULL, NULL, &tcp_winfo
354 354 };
355 355
356 356 /* For AF_INET aka /dev/tcp */
357 357 struct streamtab tcpinfov4 = {
358 358 &tcp_rinitv4, &tcp_winit
359 359 };
360 360
361 361 /* For AF_INET6 aka /dev/tcp6 */
362 362 struct streamtab tcpinfov6 = {
363 363 &tcp_rinitv6, &tcp_winit
364 364 };
365 365
366 366 /*
367 367 * Following assumes TPI alignment requirements stay along 32 bit
368 368 * boundaries
369 369 */
370 370 #define ROUNDUP32(x) \
371 371 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1))
372 372
373 373 /* Template for response to info request. */
374 374 struct T_info_ack tcp_g_t_info_ack = {
375 375 T_INFO_ACK, /* PRIM_type */
376 376 0, /* TSDU_size */
377 377 T_INFINITE, /* ETSDU_size */
378 378 T_INVALID, /* CDATA_size */
379 379 T_INVALID, /* DDATA_size */
380 380 sizeof (sin_t), /* ADDR_size */
381 381 0, /* OPT_size - not initialized here */
382 382 TIDUSZ, /* TIDU_size */
383 383 T_COTS_ORD, /* SERV_type */
384 384 TCPS_IDLE, /* CURRENT_state */
385 385 (XPG4_1|EXPINLINE) /* PROVIDER_flag */
386 386 };
387 387
388 388 struct T_info_ack tcp_g_t_info_ack_v6 = {
389 389 T_INFO_ACK, /* PRIM_type */
390 390 0, /* TSDU_size */
391 391 T_INFINITE, /* ETSDU_size */
392 392 T_INVALID, /* CDATA_size */
393 393 T_INVALID, /* DDATA_size */
394 394 sizeof (sin6_t), /* ADDR_size */
395 395 0, /* OPT_size - not initialized here */
396 396 TIDUSZ, /* TIDU_size */
397 397 T_COTS_ORD, /* SERV_type */
398 398 TCPS_IDLE, /* CURRENT_state */
399 399 (XPG4_1|EXPINLINE) /* PROVIDER_flag */
400 400 };
401 401
402 402 /*
403 403 * TCP tunables related declarations. Definitions are in tcp_tunables.c
404 404 */
405 405 extern mod_prop_info_t tcp_propinfo_tbl[];
406 406 extern int tcp_propinfo_count;
407 407
408 408 #define IS_VMLOANED_MBLK(mp) \
409 409 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0)
410 410
411 411 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */
412 412
413 413 /*
414 414 * Forces all connections to obey the value of the tcps_maxpsz_multiplier
415 415 * tunable settable via NDD. Otherwise, the per-connection behavior is
416 416 * determined dynamically during tcp_set_destination(), which is the default.
417 417 */
418 418 boolean_t tcp_static_maxpsz = B_FALSE;
419 419
420 420 /*
421 421 * If the receive buffer size is changed, this function is called to update
422 422 * the upper socket layer on the new delayed receive wake up threshold.
423 423 */
424 424 static void
425 425 tcp_set_recv_threshold(tcp_t *tcp, uint32_t new_rcvthresh)
426 426 {
427 427 uint32_t default_threshold = SOCKET_RECVHIWATER >> 3;
428 428
429 429 if (IPCL_IS_NONSTR(tcp->tcp_connp)) {
430 430 conn_t *connp = tcp->tcp_connp;
431 431 struct sock_proto_props sopp;
432 432
433 433 /*
434 434 * only increase rcvthresh upto default_threshold
435 435 */
436 436 if (new_rcvthresh > default_threshold)
437 437 new_rcvthresh = default_threshold;
438 438
439 439 sopp.sopp_flags = SOCKOPT_RCVTHRESH;
440 440 sopp.sopp_rcvthresh = new_rcvthresh;
441 441
442 442 (*connp->conn_upcalls->su_set_proto_props)
443 443 (connp->conn_upper_handle, &sopp);
444 444 }
445 445 }
446 446
447 447 /*
448 448 * Figure out the value of window scale opton. Note that the rwnd is
449 449 * ASSUMED to be rounded up to the nearest MSS before the calculation.
450 450 * We cannot find the scale value and then do a round up of tcp_rwnd
451 451 * because the scale value may not be correct after that.
452 452 *
453 453 * Set the compiler flag to make this function inline.
454 454 */
455 455 void
456 456 tcp_set_ws_value(tcp_t *tcp)
457 457 {
458 458 int i;
459 459 uint32_t rwnd = tcp->tcp_rwnd;
460 460
461 461 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT;
462 462 i++, rwnd >>= 1)
463 463 ;
464 464 tcp->tcp_rcv_ws = i;
465 465 }
466 466
467 467 /*
468 468 * Remove cached/latched IPsec references.
469 469 */
470 470 void
471 471 tcp_ipsec_cleanup(tcp_t *tcp)
472 472 {
473 473 conn_t *connp = tcp->tcp_connp;
474 474
475 475 ASSERT(connp->conn_flags & IPCL_TCPCONN);
476 476
477 477 if (connp->conn_latch != NULL) {
478 478 IPLATCH_REFRELE(connp->conn_latch);
479 479 connp->conn_latch = NULL;
480 480 }
481 481 if (connp->conn_latch_in_policy != NULL) {
482 482 IPPOL_REFRELE(connp->conn_latch_in_policy);
483 483 connp->conn_latch_in_policy = NULL;
484 484 }
485 485 if (connp->conn_latch_in_action != NULL) {
486 486 IPACT_REFRELE(connp->conn_latch_in_action);
487 487 connp->conn_latch_in_action = NULL;
488 488 }
489 489 if (connp->conn_policy != NULL) {
490 490 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
491 491 connp->conn_policy = NULL;
492 492 }
493 493 }
494 494
495 495 /*
496 496 * Cleaup before placing on free list.
497 497 * Disassociate from the netstack/tcp_stack_t since the freelist
498 498 * is per squeue and not per netstack.
499 499 */
500 500 void
501 501 tcp_cleanup(tcp_t *tcp)
502 502 {
503 503 mblk_t *mp;
504 504 conn_t *connp = tcp->tcp_connp;
505 505 tcp_stack_t *tcps = tcp->tcp_tcps;
506 506 netstack_t *ns = tcps->tcps_netstack;
507 507 mblk_t *tcp_rsrv_mp;
508 508
509 509 tcp_bind_hash_remove(tcp);
510 510
511 511 /* Cleanup that which needs the netstack first */
512 512 tcp_ipsec_cleanup(tcp);
513 513 ixa_cleanup(connp->conn_ixa);
514 514
515 515 if (connp->conn_ht_iphc != NULL) {
516 516 kmem_free(connp->conn_ht_iphc, connp->conn_ht_iphc_allocated);
517 517 connp->conn_ht_iphc = NULL;
518 518 connp->conn_ht_iphc_allocated = 0;
519 519 connp->conn_ht_iphc_len = 0;
520 520 connp->conn_ht_ulp = NULL;
521 521 connp->conn_ht_ulp_len = 0;
522 522 tcp->tcp_ipha = NULL;
523 523 tcp->tcp_ip6h = NULL;
524 524 tcp->tcp_tcpha = NULL;
525 525 }
526 526
527 527 /* We clear any IP_OPTIONS and extension headers */
528 528 ip_pkt_free(&connp->conn_xmit_ipp);
529 529
530 530 tcp_free(tcp);
531 531
532 532 /*
533 533 * Since we will bzero the entire structure, we need to
534 534 * remove it and reinsert it in global hash list. We
535 535 * know the walkers can't get to this conn because we
536 536 * had set CONDEMNED flag earlier and checked reference
537 537 * under conn_lock so walker won't pick it and when we
538 538 * go the ipcl_globalhash_remove() below, no walker
539 539 * can get to it.
540 540 */
541 541 ipcl_globalhash_remove(connp);
542 542
543 543 /* Save some state */
544 544 mp = tcp->tcp_timercache;
545 545
546 546 tcp_rsrv_mp = tcp->tcp_rsrv_mp;
547 547
548 548 if (connp->conn_cred != NULL) {
549 549 crfree(connp->conn_cred);
550 550 connp->conn_cred = NULL;
551 551 }
552 552 ipcl_conn_cleanup(connp);
553 553 connp->conn_flags = IPCL_TCPCONN;
554 554
555 555 /*
556 556 * Now it is safe to decrement the reference counts.
557 557 * This might be the last reference on the netstack
558 558 * in which case it will cause the freeing of the IP Instance.
559 559 */
560 560 connp->conn_netstack = NULL;
561 561 connp->conn_ixa->ixa_ipst = NULL;
562 562 netstack_rele(ns);
563 563 ASSERT(tcps != NULL);
564 564 tcp->tcp_tcps = NULL;
565 565
566 566 bzero(tcp, sizeof (tcp_t));
567 567
568 568 /* restore the state */
569 569 tcp->tcp_timercache = mp;
570 570
571 571 tcp->tcp_rsrv_mp = tcp_rsrv_mp;
572 572
573 573 tcp->tcp_connp = connp;
574 574
575 575 ASSERT(connp->conn_tcp == tcp);
576 576 ASSERT(connp->conn_flags & IPCL_TCPCONN);
577 577 connp->conn_state_flags = CONN_INCIPIENT;
578 578 ASSERT(connp->conn_proto == IPPROTO_TCP);
579 579 ASSERT(connp->conn_ref == 1);
580 580 }
581 581
582 582 /*
583 583 * Adapt to the information, such as rtt and rtt_sd, provided from the
584 584 * DCE and IRE maintained by IP.
585 585 *
586 586 * Checks for multicast and broadcast destination address.
587 587 * Returns zero if ok; an errno on failure.
588 588 *
589 589 * Note that the MSS calculation here is based on the info given in
590 590 * the DCE and IRE. We do not do any calculation based on TCP options. They
591 591 * will be handled in tcp_input_data() when TCP knows which options to use.
592 592 *
593 593 * Note on how TCP gets its parameters for a connection.
594 594 *
595 595 * When a tcp_t structure is allocated, it gets all the default parameters.
596 596 * In tcp_set_destination(), it gets those metric parameters, like rtt, rtt_sd,
597 597 * spipe, rpipe, ... from the route metrics. Route metric overrides the
598 598 * default.
599 599 *
600 600 * An incoming SYN with a multicast or broadcast destination address is dropped
601 601 * in ip_fanout_v4/v6.
602 602 *
603 603 * An incoming SYN with a multicast or broadcast source address is always
604 604 * dropped in tcp_set_destination, since IPDF_ALLOW_MCBC is not set in
605 605 * conn_connect.
606 606 * The same logic in tcp_set_destination also serves to
607 607 * reject an attempt to connect to a broadcast or multicast (destination)
608 608 * address.
609 609 */
610 610 int
611 611 tcp_set_destination(tcp_t *tcp)
612 612 {
613 613 uint32_t mss_max;
614 614 uint32_t mss;
615 615 boolean_t tcp_detached = TCP_IS_DETACHED(tcp);
616 616 conn_t *connp = tcp->tcp_connp;
617 617 tcp_stack_t *tcps = tcp->tcp_tcps;
618 618 iulp_t uinfo;
619 619 int error;
620 620 uint32_t flags;
621 621
622 622 flags = IPDF_LSO | IPDF_ZCOPY;
623 623 /*
624 624 * Make sure we have a dce for the destination to avoid dce_ident
625 625 * contention for connected sockets.
626 626 */
627 627 flags |= IPDF_UNIQUE_DCE;
628 628
629 629 if (!tcps->tcps_ignore_path_mtu)
630 630 connp->conn_ixa->ixa_flags |= IXAF_PMTU_DISCOVERY;
631 631
632 632 /* Use conn_lock to satify ASSERT; tcp is already serialized */
633 633 mutex_enter(&connp->conn_lock);
634 634 error = conn_connect(connp, &uinfo, flags);
635 635 mutex_exit(&connp->conn_lock);
636 636 if (error != 0)
637 637 return (error);
638 638
639 639 error = tcp_build_hdrs(tcp);
640 640 if (error != 0)
641 641 return (error);
642 642
643 643 tcp->tcp_localnet = uinfo.iulp_localnet;
644 644
645 645 if (uinfo.iulp_rtt != 0) {
646 646 clock_t rto;
647 647
648 648 tcp->tcp_rtt_sa = uinfo.iulp_rtt;
649 649 tcp->tcp_rtt_sd = uinfo.iulp_rtt_sd;
650 650 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd +
651 651 tcps->tcps_rexmit_interval_extra +
652 652 (tcp->tcp_rtt_sa >> 5);
653 653
654 654 TCP_SET_RTO(tcp, rto);
655 655 }
656 656 if (uinfo.iulp_ssthresh != 0)
657 657 tcp->tcp_cwnd_ssthresh = uinfo.iulp_ssthresh;
658 658 else
659 659 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN;
660 660 if (uinfo.iulp_spipe > 0) {
661 661 connp->conn_sndbuf = MIN(uinfo.iulp_spipe,
662 662 tcps->tcps_max_buf);
663 663 if (tcps->tcps_snd_lowat_fraction != 0) {
664 664 connp->conn_sndlowat = connp->conn_sndbuf /
665 665 tcps->tcps_snd_lowat_fraction;
666 666 }
667 667 (void) tcp_maxpsz_set(tcp, B_TRUE);
668 668 }
669 669 /*
670 670 * Note that up till now, acceptor always inherits receive
671 671 * window from the listener. But if there is a metrics
672 672 * associated with a host, we should use that instead of
673 673 * inheriting it from listener. Thus we need to pass this
674 674 * info back to the caller.
675 675 */
676 676 if (uinfo.iulp_rpipe > 0) {
677 677 tcp->tcp_rwnd = MIN(uinfo.iulp_rpipe,
678 678 tcps->tcps_max_buf);
679 679 }
680 680
681 681 if (uinfo.iulp_rtomax > 0) {
682 682 tcp->tcp_second_timer_threshold =
683 683 uinfo.iulp_rtomax;
684 684 }
685 685
686 686 /*
687 687 * Use the metric option settings, iulp_tstamp_ok and
688 688 * iulp_wscale_ok, only for active open. What this means
689 689 * is that if the other side uses timestamp or window
690 690 * scale option, TCP will also use those options. That
691 691 * is for passive open. If the application sets a
692 692 * large window, window scale is enabled regardless of
693 693 * the value in iulp_wscale_ok. This is the behavior
694 694 * since 2.6. So we keep it.
695 695 * The only case left in passive open processing is the
696 696 * check for SACK.
697 697 * For ECN, it should probably be like SACK. But the
698 698 * current value is binary, so we treat it like the other
699 699 * cases. The metric only controls active open.For passive
700 700 * open, the ndd param, tcp_ecn_permitted, controls the
701 701 * behavior.
702 702 */
703 703 if (!tcp_detached) {
704 704 /*
705 705 * The if check means that the following can only
706 706 * be turned on by the metrics only IRE, but not off.
707 707 */
708 708 if (uinfo.iulp_tstamp_ok)
709 709 tcp->tcp_snd_ts_ok = B_TRUE;
710 710 if (uinfo.iulp_wscale_ok)
711 711 tcp->tcp_snd_ws_ok = B_TRUE;
712 712 if (uinfo.iulp_sack == 2)
713 713 tcp->tcp_snd_sack_ok = B_TRUE;
714 714 if (uinfo.iulp_ecn_ok)
715 715 tcp->tcp_ecn_ok = B_TRUE;
716 716 } else {
717 717 /*
718 718 * Passive open.
719 719 *
720 720 * As above, the if check means that SACK can only be
721 721 * turned on by the metric only IRE.
722 722 */
723 723 if (uinfo.iulp_sack > 0) {
724 724 tcp->tcp_snd_sack_ok = B_TRUE;
725 725 }
726 726 }
727 727
728 728 /*
729 729 * XXX Note that currently, iulp_mtu can be as small as 68
730 730 * because of PMTUd. So tcp_mss may go to negative if combined
731 731 * length of all those options exceeds 28 bytes. But because
732 732 * of the tcp_mss_min check below, we may not have a problem if
733 733 * tcp_mss_min is of a reasonable value. The default is 1 so
734 734 * the negative problem still exists. And the check defeats PMTUd.
735 735 * In fact, if PMTUd finds that the MSS should be smaller than
736 736 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min
737 737 * value.
738 738 *
739 739 * We do not deal with that now. All those problems related to
740 740 * PMTUd will be fixed later.
741 741 */
742 742 ASSERT(uinfo.iulp_mtu != 0);
743 743 mss = tcp->tcp_initial_pmtu = uinfo.iulp_mtu;
744 744
745 745 /* Sanity check for MSS value. */
746 746 if (connp->conn_ipversion == IPV4_VERSION)
747 747 mss_max = tcps->tcps_mss_max_ipv4;
748 748 else
749 749 mss_max = tcps->tcps_mss_max_ipv6;
750 750
751 751 if (tcp->tcp_ipsec_overhead == 0)
752 752 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp);
753 753
754 754 mss -= tcp->tcp_ipsec_overhead;
755 755
756 756 if (mss < tcps->tcps_mss_min)
757 757 mss = tcps->tcps_mss_min;
758 758 if (mss > mss_max)
759 759 mss = mss_max;
760 760
761 761 /* Note that this is the maximum MSS, excluding all options. */
762 762 tcp->tcp_mss = mss;
763 763
764 764 /*
765 765 * Update the tcp connection with LSO capability.
766 766 */
767 767 tcp_update_lso(tcp, connp->conn_ixa);
768 768
769 769 /*
770 770 * Initialize the ISS here now that we have the full connection ID.
771 771 * The RFC 1948 method of initial sequence number generation requires
772 772 * knowledge of the full connection ID before setting the ISS.
773 773 */
774 774 tcp_iss_init(tcp);
775 775
776 776 tcp->tcp_loopback = (uinfo.iulp_loopback | uinfo.iulp_local);
777 777
778 778 /*
779 779 * Make sure that conn is not marked incipient
780 780 * for incoming connections. A blind
781 781 * removal of incipient flag is cheaper than
782 782 * check and removal.
783 783 */
784 784 mutex_enter(&connp->conn_lock);
785 785 connp->conn_state_flags &= ~CONN_INCIPIENT;
786 786 mutex_exit(&connp->conn_lock);
787 787 return (0);
788 788 }
789 789
790 790 /*
791 791 * tcp_clean_death / tcp_close_detached must not be called more than once
792 792 * on a tcp. Thus every function that potentially calls tcp_clean_death
793 793 * must check for the tcp state before calling tcp_clean_death.
794 794 * Eg. tcp_input_data, tcp_eager_kill, tcp_clean_death_wrapper,
795 795 * tcp_timer_handler, all check for the tcp state.
796 796 */
797 797 /* ARGSUSED */
798 798 void
799 799 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2,
800 800 ip_recv_attr_t *dummy)
801 801 {
802 802 tcp_t *tcp = ((conn_t *)arg)->conn_tcp;
803 803
804 804 freemsg(mp);
805 805 if (tcp->tcp_state > TCPS_BOUND)
806 806 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, ETIMEDOUT);
807 807 }
808 808
809 809 /*
810 810 * We are dying for some reason. Try to do it gracefully. (May be called
811 811 * as writer.)
812 812 *
813 813 * Return -1 if the structure was not cleaned up (if the cleanup had to be
814 814 * done by a service procedure).
815 815 * TBD - Should the return value distinguish between the tcp_t being
816 816 * freed and it being reinitialized?
817 817 */
818 818 int
819 819 tcp_clean_death(tcp_t *tcp, int err)
820 820 {
821 821 mblk_t *mp;
822 822 queue_t *q;
823 823 conn_t *connp = tcp->tcp_connp;
824 824 tcp_stack_t *tcps = tcp->tcp_tcps;
825 825
826 826 if (tcp->tcp_fused)
827 827 tcp_unfuse(tcp);
828 828
829 829 if (tcp->tcp_linger_tid != 0 &&
830 830 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) {
831 831 tcp_stop_lingering(tcp);
832 832 }
833 833
834 834 ASSERT(tcp != NULL);
835 835 ASSERT((connp->conn_family == AF_INET &&
836 836 connp->conn_ipversion == IPV4_VERSION) ||
837 837 (connp->conn_family == AF_INET6 &&
838 838 (connp->conn_ipversion == IPV4_VERSION ||
839 839 connp->conn_ipversion == IPV6_VERSION)));
840 840
841 841 if (TCP_IS_DETACHED(tcp)) {
842 842 if (tcp->tcp_hard_binding) {
843 843 /*
844 844 * Its an eager that we are dealing with. We close the
845 845 * eager but in case a conn_ind has already gone to the
846 846 * listener, let tcp_accept_finish() send a discon_ind
847 847 * to the listener and drop the last reference. If the
848 848 * listener doesn't even know about the eager i.e. the
849 849 * conn_ind hasn't gone up, blow away the eager and drop
850 850 * the last reference as well. If the conn_ind has gone
851 851 * up, state should be BOUND. tcp_accept_finish
852 852 * will figure out that the connection has received a
853 853 * RST and will send a DISCON_IND to the application.
854 854 */
855 855 tcp_closei_local(tcp);
856 856 if (!tcp->tcp_tconnind_started) {
857 857 CONN_DEC_REF(connp);
858 858 } else {
859 859 tcp->tcp_state = TCPS_BOUND;
860 860 DTRACE_TCP6(state__change, void, NULL,
861 861 ip_xmit_attr_t *, connp->conn_ixa,
862 862 void, NULL, tcp_t *, tcp, void, NULL,
863 863 int32_t, TCPS_CLOSED);
864 864 }
865 865 } else {
866 866 tcp_close_detached(tcp);
867 867 }
868 868 return (0);
869 869 }
870 870
871 871 TCP_STAT(tcps, tcp_clean_death_nondetached);
872 872
873 873 /*
874 874 * The connection is dead. Decrement listener connection counter if
875 875 * necessary.
876 876 */
877 877 if (tcp->tcp_listen_cnt != NULL)
878 878 TCP_DECR_LISTEN_CNT(tcp);
879 879
880 880 /*
881 881 * When a connection is moved to TIME_WAIT state, the connection
882 882 * counter is already decremented. So no need to decrement here
883 883 * again. See SET_TIME_WAIT() macro.
884 884 */
885 885 if (tcp->tcp_state >= TCPS_ESTABLISHED &&
886 886 tcp->tcp_state < TCPS_TIME_WAIT) {
887 887 TCPS_CONN_DEC(tcps);
888 888 }
889 889
890 890 q = connp->conn_rq;
891 891
892 892 /* Trash all inbound data */
893 893 if (!IPCL_IS_NONSTR(connp)) {
894 894 ASSERT(q != NULL);
895 895 flushq(q, FLUSHALL);
896 896 }
897 897
898 898 /*
899 899 * If we are at least part way open and there is error
900 900 * (err==0 implies no error)
901 901 * notify our client by a T_DISCON_IND.
902 902 */
903 903 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) {
904 904 if (tcp->tcp_state >= TCPS_ESTABLISHED &&
905 905 !TCP_IS_SOCKET(tcp)) {
906 906 /*
907 907 * Send M_FLUSH according to TPI. Because sockets will
908 908 * (and must) ignore FLUSHR we do that only for TPI
909 909 * endpoints and sockets in STREAMS mode.
910 910 */
911 911 (void) putnextctl1(q, M_FLUSH, FLUSHR);
912 912 }
913 913 if (connp->conn_debug) {
914 914 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR,
915 915 "tcp_clean_death: discon err %d", err);
916 916 }
917 917 if (IPCL_IS_NONSTR(connp)) {
918 918 /* Direct socket, use upcall */
919 919 (*connp->conn_upcalls->su_disconnected)(
920 920 connp->conn_upper_handle, tcp->tcp_connid, err);
921 921 } else {
922 922 mp = mi_tpi_discon_ind(NULL, err, 0);
923 923 if (mp != NULL) {
924 924 putnext(q, mp);
925 925 } else {
926 926 if (connp->conn_debug) {
927 927 (void) strlog(TCP_MOD_ID, 0, 1,
928 928 SL_ERROR|SL_TRACE,
929 929 "tcp_clean_death, sending M_ERROR");
930 930 }
931 931 (void) putnextctl1(q, M_ERROR, EPROTO);
932 932 }
933 933 }
934 934 if (tcp->tcp_state <= TCPS_SYN_RCVD) {
935 935 /* SYN_SENT or SYN_RCVD */
936 936 TCPS_BUMP_MIB(tcps, tcpAttemptFails);
937 937 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) {
938 938 /* ESTABLISHED or CLOSE_WAIT */
939 939 TCPS_BUMP_MIB(tcps, tcpEstabResets);
940 940 }
941 941 }
942 942
943 943 /*
944 944 * ESTABLISHED non-STREAMS eagers are not 'detached' because
945 945 * an upper handle is obtained when the SYN-ACK comes in. So it
946 946 * should receive the 'disconnected' upcall, but tcp_reinit should
947 947 * not be called since this is an eager.
948 948 */
949 949 if (tcp->tcp_listener != NULL && IPCL_IS_NONSTR(connp)) {
950 950 tcp_closei_local(tcp);
951 951 tcp->tcp_state = TCPS_BOUND;
952 952 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
953 953 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
954 954 int32_t, TCPS_CLOSED);
955 955 return (0);
956 956 }
957 957
958 958 tcp_reinit(tcp);
959 959 if (IPCL_IS_NONSTR(connp))
960 960 (void) tcp_do_unbind(connp);
961 961
962 962 return (-1);
963 963 }
964 964
965 965 /*
966 966 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout
967 967 * to expire, stop the wait and finish the close.
968 968 */
969 969 void
970 970 tcp_stop_lingering(tcp_t *tcp)
971 971 {
972 972 clock_t delta = 0;
973 973 tcp_stack_t *tcps = tcp->tcp_tcps;
974 974 conn_t *connp = tcp->tcp_connp;
975 975
976 976 tcp->tcp_linger_tid = 0;
977 977 if (tcp->tcp_state > TCPS_LISTEN) {
978 978 tcp_acceptor_hash_remove(tcp);
979 979 mutex_enter(&tcp->tcp_non_sq_lock);
980 980 if (tcp->tcp_flow_stopped) {
981 981 tcp_clrqfull(tcp);
982 982 }
983 983 mutex_exit(&tcp->tcp_non_sq_lock);
984 984
985 985 if (tcp->tcp_timer_tid != 0) {
986 986 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid);
987 987 tcp->tcp_timer_tid = 0;
988 988 }
989 989 /*
990 990 * Need to cancel those timers which will not be used when
991 991 * TCP is detached. This has to be done before the conn_wq
992 992 * is cleared.
993 993 */
994 994 tcp_timers_stop(tcp);
995 995
996 996 tcp->tcp_detached = B_TRUE;
997 997 connp->conn_rq = NULL;
998 998 connp->conn_wq = NULL;
999 999
1000 1000 if (tcp->tcp_state == TCPS_TIME_WAIT) {
1001 1001 tcp_time_wait_append(tcp);
1002 1002 TCP_DBGSTAT(tcps, tcp_detach_time_wait);
1003 1003 goto finish;
1004 1004 }
1005 1005
1006 1006 /*
1007 1007 * If delta is zero the timer event wasn't executed and was
1008 1008 * successfully canceled. In this case we need to restart it
1009 1009 * with the minimal delta possible.
1010 1010 */
1011 1011 if (delta >= 0) {
1012 1012 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer,
1013 1013 delta ? delta : 1);
1014 1014 }
1015 1015 } else {
1016 1016 tcp_closei_local(tcp);
1017 1017 CONN_DEC_REF(connp);
1018 1018 }
1019 1019 finish:
1020 1020 tcp->tcp_detached = B_TRUE;
1021 1021 connp->conn_rq = NULL;
1022 1022 connp->conn_wq = NULL;
1023 1023
1024 1024 /* Signal closing thread that it can complete close */
1025 1025 mutex_enter(&tcp->tcp_closelock);
1026 1026 tcp->tcp_closed = 1;
1027 1027 cv_signal(&tcp->tcp_closecv);
1028 1028 mutex_exit(&tcp->tcp_closelock);
1029 1029
1030 1030 /* If we have an upper handle (socket), release it */
1031 1031 if (IPCL_IS_NONSTR(connp)) {
1032 1032 ASSERT(connp->conn_upper_handle != NULL);
1033 1033 (*connp->conn_upcalls->su_closed)(connp->conn_upper_handle);
1034 1034 connp->conn_upper_handle = NULL;
1035 1035 connp->conn_upcalls = NULL;
1036 1036 }
1037 1037 }
1038 1038
1039 1039 void
1040 1040 tcp_close_common(conn_t *connp, int flags)
1041 1041 {
1042 1042 tcp_t *tcp = connp->conn_tcp;
1043 1043 mblk_t *mp = &tcp->tcp_closemp;
1044 1044 boolean_t conn_ioctl_cleanup_reqd = B_FALSE;
1045 1045 mblk_t *bp;
1046 1046
1047 1047 ASSERT(connp->conn_ref >= 2);
1048 1048
1049 1049 /*
1050 1050 * Mark the conn as closing. ipsq_pending_mp_add will not
1051 1051 * add any mp to the pending mp list, after this conn has
1052 1052 * started closing.
1053 1053 */
1054 1054 mutex_enter(&connp->conn_lock);
1055 1055 connp->conn_state_flags |= CONN_CLOSING;
1056 1056 if (connp->conn_oper_pending_ill != NULL)
1057 1057 conn_ioctl_cleanup_reqd = B_TRUE;
1058 1058 CONN_INC_REF_LOCKED(connp);
1059 1059 mutex_exit(&connp->conn_lock);
1060 1060 tcp->tcp_closeflags = (uint8_t)flags;
1061 1061 ASSERT(connp->conn_ref >= 3);
1062 1062
1063 1063 /*
1064 1064 * tcp_closemp_used is used below without any protection of a lock
1065 1065 * as we don't expect any one else to use it concurrently at this
1066 1066 * point otherwise it would be a major defect.
1067 1067 */
1068 1068
1069 1069 if (mp->b_prev == NULL)
1070 1070 tcp->tcp_closemp_used = B_TRUE;
1071 1071 else
1072 1072 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: "
1073 1073 "connp %p tcp %p\n", (void *)connp, (void *)tcp);
1074 1074
1075 1075 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15);
1076 1076
1077 1077 /*
1078 1078 * Cleanup any queued ioctls here. This must be done before the wq/rq
1079 1079 * are re-written by tcp_close_output().
1080 1080 */
1081 1081 if (conn_ioctl_cleanup_reqd)
1082 1082 conn_ioctl_cleanup(connp);
1083 1083
1084 1084 /*
1085 1085 * As CONN_CLOSING is set, no further ioctls should be passed down to
1086 1086 * IP for this conn (see the guards in tcp_ioctl, tcp_wput_ioctl and
1087 1087 * tcp_wput_iocdata). If the ioctl was queued on an ipsq,
1088 1088 * conn_ioctl_cleanup should have found it and removed it. If the ioctl
1089 1089 * was still in flight at the time, we wait for it here. See comments
1090 1090 * for CONN_INC_IOCTLREF in ip.h for details.
1091 1091 */
1092 1092 mutex_enter(&connp->conn_lock);
1093 1093 while (connp->conn_ioctlref > 0)
1094 1094 cv_wait(&connp->conn_cv, &connp->conn_lock);
1095 1095 ASSERT(connp->conn_ioctlref == 0);
1096 1096 ASSERT(connp->conn_oper_pending_ill == NULL);
1097 1097 mutex_exit(&connp->conn_lock);
1098 1098
1099 1099 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_close_output, connp,
1100 1100 NULL, tcp_squeue_flag, SQTAG_IP_TCP_CLOSE);
1101 1101
1102 1102 /*
1103 1103 * For non-STREAMS sockets, the normal case is that the conn makes
1104 1104 * an upcall when it's finally closed, so there is no need to wait
1105 1105 * in the protocol. But in case of SO_LINGER the thread sleeps here
1106 1106 * so it can properly deal with the thread being interrupted.
1107 1107 */
1108 1108 if (IPCL_IS_NONSTR(connp) && connp->conn_linger == 0)
1109 1109 goto nowait;
1110 1110
1111 1111 mutex_enter(&tcp->tcp_closelock);
1112 1112 while (!tcp->tcp_closed) {
1113 1113 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) {
1114 1114 /*
1115 1115 * The cv_wait_sig() was interrupted. We now do the
1116 1116 * following:
1117 1117 *
1118 1118 * 1) If the endpoint was lingering, we allow this
1119 1119 * to be interrupted by cancelling the linger timeout
1120 1120 * and closing normally.
1121 1121 *
1122 1122 * 2) Revert to calling cv_wait()
1123 1123 *
1124 1124 * We revert to using cv_wait() to avoid an
1125 1125 * infinite loop which can occur if the calling
1126 1126 * thread is higher priority than the squeue worker
1127 1127 * thread and is bound to the same cpu.
1128 1128 */
1129 1129 if (connp->conn_linger && connp->conn_lingertime > 0) {
1130 1130 mutex_exit(&tcp->tcp_closelock);
1131 1131 /* Entering squeue, bump ref count. */
1132 1132 CONN_INC_REF(connp);
1133 1133 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL);
1134 1134 SQUEUE_ENTER_ONE(connp->conn_sqp, bp,
1135 1135 tcp_linger_interrupted, connp, NULL,
1136 1136 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE);
1137 1137 mutex_enter(&tcp->tcp_closelock);
1138 1138 }
1139 1139 break;
1140 1140 }
1141 1141 }
1142 1142 while (!tcp->tcp_closed)
1143 1143 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock);
1144 1144 mutex_exit(&tcp->tcp_closelock);
1145 1145
1146 1146 /*
1147 1147 * In the case of listener streams that have eagers in the q or q0
1148 1148 * we wait for the eagers to drop their reference to us. conn_rq and
1149 1149 * conn_wq of the eagers point to our queues. By waiting for the
1150 1150 * refcnt to drop to 1, we are sure that the eagers have cleaned
1151 1151 * up their queue pointers and also dropped their references to us.
1152 1152 *
1153 1153 * For non-STREAMS sockets we do not have to wait here; the
1154 1154 * listener will instead make a su_closed upcall when the last
1155 1155 * reference is dropped.
1156 1156 */
1157 1157 if (tcp->tcp_wait_for_eagers && !IPCL_IS_NONSTR(connp)) {
1158 1158 mutex_enter(&connp->conn_lock);
1159 1159 while (connp->conn_ref != 1) {
1160 1160 cv_wait(&connp->conn_cv, &connp->conn_lock);
1161 1161 }
1162 1162 mutex_exit(&connp->conn_lock);
1163 1163 }
1164 1164
1165 1165 nowait:
1166 1166 connp->conn_cpid = NOPID;
1167 1167 }
1168 1168
1169 1169 /*
1170 1170 * Called by tcp_close() routine via squeue when lingering is
1171 1171 * interrupted by a signal.
1172 1172 */
1173 1173
1174 1174 /* ARGSUSED */
1175 1175 static void
1176 1176 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1177 1177 {
1178 1178 conn_t *connp = (conn_t *)arg;
1179 1179 tcp_t *tcp = connp->conn_tcp;
1180 1180
1181 1181 freeb(mp);
1182 1182 if (tcp->tcp_linger_tid != 0 &&
1183 1183 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) {
1184 1184 tcp_stop_lingering(tcp);
1185 1185 tcp->tcp_client_errno = EINTR;
1186 1186 }
1187 1187 }
1188 1188
1189 1189 /*
1190 1190 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp.
1191 1191 * Some stream heads get upset if they see these later on as anything but NULL.
1192 1192 */
1193 1193 void
1194 1194 tcp_close_mpp(mblk_t **mpp)
1195 1195 {
1196 1196 mblk_t *mp;
1197 1197
1198 1198 if ((mp = *mpp) != NULL) {
1199 1199 do {
1200 1200 mp->b_next = NULL;
1201 1201 mp->b_prev = NULL;
1202 1202 } while ((mp = mp->b_cont) != NULL);
1203 1203
1204 1204 mp = *mpp;
1205 1205 *mpp = NULL;
1206 1206 freemsg(mp);
1207 1207 }
1208 1208 }
1209 1209
1210 1210 /* Do detached close. */
1211 1211 void
1212 1212 tcp_close_detached(tcp_t *tcp)
1213 1213 {
1214 1214 if (tcp->tcp_fused)
1215 1215 tcp_unfuse(tcp);
1216 1216
1217 1217 /*
1218 1218 * Clustering code serializes TCP disconnect callbacks and
1219 1219 * cluster tcp list walks by blocking a TCP disconnect callback
1220 1220 * if a cluster tcp list walk is in progress. This ensures
1221 1221 * accurate accounting of TCPs in the cluster code even though
1222 1222 * the TCP list walk itself is not atomic.
1223 1223 */
1224 1224 tcp_closei_local(tcp);
1225 1225 CONN_DEC_REF(tcp->tcp_connp);
1226 1226 }
1227 1227
1228 1228 /*
1229 1229 * The tcp_t is going away. Remove it from all lists and set it
1230 1230 * to TCPS_CLOSED. The freeing up of memory is deferred until
1231 1231 * tcp_inactive. This is needed since a thread in tcp_rput might have
1232 1232 * done a CONN_INC_REF on this structure before it was removed from the
1233 1233 * hashes.
1234 1234 */
1235 1235 void
1236 1236 tcp_closei_local(tcp_t *tcp)
1237 1237 {
1238 1238 conn_t *connp = tcp->tcp_connp;
1239 1239 tcp_stack_t *tcps = tcp->tcp_tcps;
1240 1240 int32_t oldstate;
1241 1241
1242 1242 if (!TCP_IS_SOCKET(tcp))
1243 1243 tcp_acceptor_hash_remove(tcp);
1244 1244
1245 1245 TCPS_UPDATE_MIB(tcps, tcpHCInSegs, tcp->tcp_ibsegs);
1246 1246 tcp->tcp_ibsegs = 0;
1247 1247 TCPS_UPDATE_MIB(tcps, tcpHCOutSegs, tcp->tcp_obsegs);
1248 1248 tcp->tcp_obsegs = 0;
1249 1249
1250 1250 /*
1251 1251 * This can be called via tcp_time_wait_processing() if TCP gets a
1252 1252 * SYN with sequence number outside the TIME-WAIT connection's
1253 1253 * window. So we need to check for TIME-WAIT state here as the
1254 1254 * connection counter is already decremented. See SET_TIME_WAIT()
1255 1255 * macro
1256 1256 */
1257 1257 if (tcp->tcp_state >= TCPS_ESTABLISHED &&
1258 1258 tcp->tcp_state < TCPS_TIME_WAIT) {
1259 1259 TCPS_CONN_DEC(tcps);
1260 1260 }
1261 1261
1262 1262 /*
1263 1263 * If we are an eager connection hanging off a listener that
1264 1264 * hasn't formally accepted the connection yet, get off his
1265 1265 * list and blow off any data that we have accumulated.
1266 1266 */
1267 1267 if (tcp->tcp_listener != NULL) {
1268 1268 tcp_t *listener = tcp->tcp_listener;
1269 1269 mutex_enter(&listener->tcp_eager_lock);
1270 1270 /*
1271 1271 * tcp_tconnind_started == B_TRUE means that the
1272 1272 * conn_ind has already gone to listener. At
1273 1273 * this point, eager will be closed but we
1274 1274 * leave it in listeners eager list so that
1275 1275 * if listener decides to close without doing
1276 1276 * accept, we can clean this up. In tcp_tli_accept
1277 1277 * we take care of the case of accept on closed
1278 1278 * eager.
1279 1279 */
1280 1280 if (!tcp->tcp_tconnind_started) {
1281 1281 tcp_eager_unlink(tcp);
1282 1282 mutex_exit(&listener->tcp_eager_lock);
1283 1283 /*
1284 1284 * We don't want to have any pointers to the
1285 1285 * listener queue, after we have released our
1286 1286 * reference on the listener
1287 1287 */
1288 1288 ASSERT(tcp->tcp_detached);
1289 1289 connp->conn_rq = NULL;
1290 1290 connp->conn_wq = NULL;
1291 1291 CONN_DEC_REF(listener->tcp_connp);
1292 1292 } else {
1293 1293 mutex_exit(&listener->tcp_eager_lock);
1294 1294 }
1295 1295 }
1296 1296
1297 1297 /* Stop all the timers */
1298 1298 tcp_timers_stop(tcp);
1299 1299
1300 1300 if (tcp->tcp_state == TCPS_LISTEN) {
1301 1301 if (tcp->tcp_ip_addr_cache) {
1302 1302 kmem_free((void *)tcp->tcp_ip_addr_cache,
1303 1303 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t));
1304 1304 tcp->tcp_ip_addr_cache = NULL;
1305 1305 }
1306 1306 }
1307 1307
1308 1308 /* Decrement listerner connection counter if necessary. */
1309 1309 if (tcp->tcp_listen_cnt != NULL)
1310 1310 TCP_DECR_LISTEN_CNT(tcp);
1311 1311
1312 1312 mutex_enter(&tcp->tcp_non_sq_lock);
1313 1313 if (tcp->tcp_flow_stopped)
1314 1314 tcp_clrqfull(tcp);
1315 1315 mutex_exit(&tcp->tcp_non_sq_lock);
1316 1316
1317 1317 tcp_bind_hash_remove(tcp);
1318 1318 /*
1319 1319 * If the tcp_time_wait_collector (which runs outside the squeue)
1320 1320 * is trying to remove this tcp from the time wait list, we will
1321 1321 * block in tcp_time_wait_remove while trying to acquire the
1322 1322 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also
1323 1323 * requires the ipcl_hash_remove to be ordered after the
1324 1324 * tcp_time_wait_remove for the refcnt checks to work correctly.
1325 1325 */
1326 1326 if (tcp->tcp_state == TCPS_TIME_WAIT)
1327 1327 (void) tcp_time_wait_remove(tcp, NULL);
1328 1328 CL_INET_DISCONNECT(connp);
1329 1329 ipcl_hash_remove(connp);
1330 1330 oldstate = tcp->tcp_state;
1331 1331 tcp->tcp_state = TCPS_CLOSED;
1332 1332 /* Need to probe before ixa_cleanup() is called */
1333 1333 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
1334 1334 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
1335 1335 int32_t, oldstate);
1336 1336 ixa_cleanup(connp->conn_ixa);
1337 1337
1338 1338 /*
1339 1339 * Mark the conn as CONDEMNED
1340 1340 */
1341 1341 mutex_enter(&connp->conn_lock);
1342 1342 connp->conn_state_flags |= CONN_CONDEMNED;
1343 1343 mutex_exit(&connp->conn_lock);
1344 1344
1345 1345 ASSERT(tcp->tcp_time_wait_next == NULL);
1346 1346 ASSERT(tcp->tcp_time_wait_prev == NULL);
1347 1347 ASSERT(tcp->tcp_time_wait_expire == 0);
1348 1348
1349 1349 tcp_ipsec_cleanup(tcp);
1350 1350 }
1351 1351
1352 1352 /*
1353 1353 * tcp is dying (called from ipcl_conn_destroy and error cases).
1354 1354 * Free the tcp_t in either case.
1355 1355 */
1356 1356 void
1357 1357 tcp_free(tcp_t *tcp)
1358 1358 {
1359 1359 mblk_t *mp;
1360 1360 conn_t *connp = tcp->tcp_connp;
1361 1361
1362 1362 ASSERT(tcp != NULL);
1363 1363 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL);
1364 1364
1365 1365 connp->conn_rq = NULL;
1366 1366 connp->conn_wq = NULL;
1367 1367
1368 1368 tcp_close_mpp(&tcp->tcp_xmit_head);
1369 1369 tcp_close_mpp(&tcp->tcp_reass_head);
1370 1370 if (tcp->tcp_rcv_list != NULL) {
1371 1371 /* Free b_next chain */
1372 1372 tcp_close_mpp(&tcp->tcp_rcv_list);
1373 1373 }
1374 1374 if ((mp = tcp->tcp_urp_mp) != NULL) {
1375 1375 freemsg(mp);
1376 1376 }
1377 1377 if ((mp = tcp->tcp_urp_mark_mp) != NULL) {
1378 1378 freemsg(mp);
1379 1379 }
1380 1380
1381 1381 if (tcp->tcp_fused_sigurg_mp != NULL) {
1382 1382 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1383 1383 freeb(tcp->tcp_fused_sigurg_mp);
1384 1384 tcp->tcp_fused_sigurg_mp = NULL;
1385 1385 }
1386 1386
1387 1387 if (tcp->tcp_ordrel_mp != NULL) {
1388 1388 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1389 1389 freeb(tcp->tcp_ordrel_mp);
1390 1390 tcp->tcp_ordrel_mp = NULL;
1391 1391 }
1392 1392
1393 1393 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp);
1394 1394 bzero(&tcp->tcp_sack_info, sizeof (tcp_sack_info_t));
1395 1395
1396 1396 if (tcp->tcp_hopopts != NULL) {
1397 1397 mi_free(tcp->tcp_hopopts);
1398 1398 tcp->tcp_hopopts = NULL;
1399 1399 tcp->tcp_hopoptslen = 0;
1400 1400 }
1401 1401 ASSERT(tcp->tcp_hopoptslen == 0);
1402 1402 if (tcp->tcp_dstopts != NULL) {
1403 1403 mi_free(tcp->tcp_dstopts);
1404 1404 tcp->tcp_dstopts = NULL;
1405 1405 tcp->tcp_dstoptslen = 0;
1406 1406 }
1407 1407 ASSERT(tcp->tcp_dstoptslen == 0);
1408 1408 if (tcp->tcp_rthdrdstopts != NULL) {
1409 1409 mi_free(tcp->tcp_rthdrdstopts);
1410 1410 tcp->tcp_rthdrdstopts = NULL;
1411 1411 tcp->tcp_rthdrdstoptslen = 0;
1412 1412 }
1413 1413 ASSERT(tcp->tcp_rthdrdstoptslen == 0);
1414 1414 if (tcp->tcp_rthdr != NULL) {
1415 1415 mi_free(tcp->tcp_rthdr);
1416 1416 tcp->tcp_rthdr = NULL;
1417 1417 tcp->tcp_rthdrlen = 0;
1418 1418 }
1419 1419 ASSERT(tcp->tcp_rthdrlen == 0);
1420 1420
1421 1421 /*
1422 1422 * Following is really a blowing away a union.
1423 1423 * It happens to have exactly two members of identical size
1424 1424 * the following code is enough.
1425 1425 */
1426 1426 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind);
1427 1427
1428 1428 /*
1429 1429 * If this is a non-STREAM socket still holding on to an upper
1430 1430 * handle, release it. As a result of fallback we might also see
1431 1431 * STREAMS based conns with upper handles, in which case there is
1432 1432 * nothing to do other than clearing the field.
1433 1433 */
1434 1434 if (connp->conn_upper_handle != NULL) {
1435 1435 if (IPCL_IS_NONSTR(connp)) {
1436 1436 (*connp->conn_upcalls->su_closed)(
1437 1437 connp->conn_upper_handle);
1438 1438 tcp->tcp_detached = B_TRUE;
1439 1439 }
1440 1440 connp->conn_upper_handle = NULL;
1441 1441 connp->conn_upcalls = NULL;
1442 1442 }
1443 1443 }
1444 1444
1445 1445 /*
1446 1446 * tcp_get_conn/tcp_free_conn
1447 1447 *
1448 1448 * tcp_get_conn is used to get a clean tcp connection structure.
1449 1449 * It tries to reuse the connections put on the freelist by the
1450 1450 * time_wait_collector failing which it goes to kmem_cache. This
1451 1451 * way has two benefits compared to just allocating from and
1452 1452 * freeing to kmem_cache.
1453 1453 * 1) The time_wait_collector can free (which includes the cleanup)
1454 1454 * outside the squeue. So when the interrupt comes, we have a clean
1455 1455 * connection sitting in the freelist. Obviously, this buys us
1456 1456 * performance.
1457 1457 *
1458 1458 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_input_listener
1459 1459 * has multiple disadvantages - tying up the squeue during alloc.
1460 1460 * But allocating the conn/tcp in IP land is also not the best since
1461 1461 * we can't check the 'q' and 'q0' which are protected by squeue and
1462 1462 * blindly allocate memory which might have to be freed here if we are
1463 1463 * not allowed to accept the connection. By using the freelist and
1464 1464 * putting the conn/tcp back in freelist, we don't pay a penalty for
1465 1465 * allocating memory without checking 'q/q0' and freeing it if we can't
1466 1466 * accept the connection.
1467 1467 *
1468 1468 * Care should be taken to put the conn back in the same squeue's freelist
1469 1469 * from which it was allocated. Best results are obtained if conn is
1470 1470 * allocated from listener's squeue and freed to the same. Time wait
1471 1471 * collector will free up the freelist is the connection ends up sitting
1472 1472 * there for too long.
1473 1473 */
1474 1474 void *
1475 1475 tcp_get_conn(void *arg, tcp_stack_t *tcps)
1476 1476 {
1477 1477 tcp_t *tcp = NULL;
1478 1478 conn_t *connp = NULL;
1479 1479 squeue_t *sqp = (squeue_t *)arg;
1480 1480 tcp_squeue_priv_t *tcp_time_wait;
1481 1481 netstack_t *ns;
1482 1482 mblk_t *tcp_rsrv_mp = NULL;
1483 1483
1484 1484 tcp_time_wait =
1485 1485 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP));
1486 1486
1487 1487 mutex_enter(&tcp_time_wait->tcp_time_wait_lock);
1488 1488 tcp = tcp_time_wait->tcp_free_list;
1489 1489 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0));
1490 1490 if (tcp != NULL) {
1491 1491 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next;
1492 1492 tcp_time_wait->tcp_free_list_cnt--;
1493 1493 mutex_exit(&tcp_time_wait->tcp_time_wait_lock);
1494 1494 tcp->tcp_time_wait_next = NULL;
1495 1495 connp = tcp->tcp_connp;
1496 1496 connp->conn_flags |= IPCL_REUSED;
1497 1497
1498 1498 ASSERT(tcp->tcp_tcps == NULL);
1499 1499 ASSERT(connp->conn_netstack == NULL);
1500 1500 ASSERT(tcp->tcp_rsrv_mp != NULL);
1501 1501 ns = tcps->tcps_netstack;
1502 1502 netstack_hold(ns);
1503 1503 connp->conn_netstack = ns;
1504 1504 connp->conn_ixa->ixa_ipst = ns->netstack_ip;
1505 1505 tcp->tcp_tcps = tcps;
1506 1506 ipcl_globalhash_insert(connp);
1507 1507
1508 1508 connp->conn_ixa->ixa_notify_cookie = tcp;
1509 1509 ASSERT(connp->conn_ixa->ixa_notify == tcp_notify);
1510 1510 connp->conn_recv = tcp_input_data;
1511 1511 ASSERT(connp->conn_recvicmp == tcp_icmp_input);
1512 1512 ASSERT(connp->conn_verifyicmp == tcp_verifyicmp);
1513 1513 return ((void *)connp);
1514 1514 }
1515 1515 mutex_exit(&tcp_time_wait->tcp_time_wait_lock);
1516 1516 /*
1517 1517 * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed until
1518 1518 * this conn_t/tcp_t is freed at ipcl_conn_destroy().
1519 1519 */
1520 1520 tcp_rsrv_mp = allocb(0, BPRI_HI);
1521 1521 if (tcp_rsrv_mp == NULL)
1522 1522 return (NULL);
1523 1523
1524 1524 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
1525 1525 tcps->tcps_netstack)) == NULL) {
1526 1526 freeb(tcp_rsrv_mp);
1527 1527 return (NULL);
1528 1528 }
1529 1529
1530 1530 tcp = connp->conn_tcp;
1531 1531 tcp->tcp_rsrv_mp = tcp_rsrv_mp;
1532 1532 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL);
1533 1533
1534 1534 tcp->tcp_tcps = tcps;
1535 1535
1536 1536 connp->conn_recv = tcp_input_data;
1537 1537 connp->conn_recvicmp = tcp_icmp_input;
1538 1538 connp->conn_verifyicmp = tcp_verifyicmp;
1539 1539
1540 1540 /*
1541 1541 * Register tcp_notify to listen to capability changes detected by IP.
1542 1542 * This upcall is made in the context of the call to conn_ip_output
1543 1543 * thus it is inside the squeue.
1544 1544 */
1545 1545 connp->conn_ixa->ixa_notify = tcp_notify;
1546 1546 connp->conn_ixa->ixa_notify_cookie = tcp;
1547 1547
1548 1548 return ((void *)connp);
1549 1549 }
1550 1550
1551 1551 /*
1552 1552 * Handle connect to IPv4 destinations, including connections for AF_INET6
1553 1553 * sockets connecting to IPv4 mapped IPv6 destinations.
1554 1554 * Returns zero if OK, a positive errno, or a negative TLI error.
1555 1555 */
1556 1556 static int
1557 1557 tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, in_port_t dstport,
1558 1558 uint_t srcid)
1559 1559 {
1560 1560 ipaddr_t dstaddr = *dstaddrp;
1561 1561 uint16_t lport;
1562 1562 conn_t *connp = tcp->tcp_connp;
1563 1563 tcp_stack_t *tcps = tcp->tcp_tcps;
1564 1564 int error;
1565 1565
1566 1566 ASSERT(connp->conn_ipversion == IPV4_VERSION);
1567 1567
1568 1568 /* Check for attempt to connect to INADDR_ANY */
1569 1569 if (dstaddr == INADDR_ANY) {
1570 1570 /*
1571 1571 * SunOS 4.x and 4.3 BSD allow an application
1572 1572 * to connect a TCP socket to INADDR_ANY.
1573 1573 * When they do this, the kernel picks the
1574 1574 * address of one interface and uses it
1575 1575 * instead. The kernel usually ends up
1576 1576 * picking the address of the loopback
1577 1577 * interface. This is an undocumented feature.
1578 1578 * However, we provide the same thing here
1579 1579 * in order to have source and binary
1580 1580 * compatibility with SunOS 4.x.
1581 1581 * Update the T_CONN_REQ (sin/sin6) since it is used to
1582 1582 * generate the T_CONN_CON.
1583 1583 */
1584 1584 dstaddr = htonl(INADDR_LOOPBACK);
1585 1585 *dstaddrp = dstaddr;
1586 1586 }
1587 1587
1588 1588 /* Handle __sin6_src_id if socket not bound to an IP address */
1589 1589 if (srcid != 0 && connp->conn_laddr_v4 == INADDR_ANY) {
1590 1590 ip_srcid_find_id(srcid, &connp->conn_laddr_v6,
1591 1591 IPCL_ZONEID(connp), tcps->tcps_netstack);
1592 1592 connp->conn_saddr_v6 = connp->conn_laddr_v6;
1593 1593 }
1594 1594
1595 1595 IN6_IPADDR_TO_V4MAPPED(dstaddr, &connp->conn_faddr_v6);
1596 1596 connp->conn_fport = dstport;
1597 1597
1598 1598 /*
1599 1599 * At this point the remote destination address and remote port fields
1600 1600 * in the tcp-four-tuple have been filled in the tcp structure. Now we
1601 1601 * have to see which state tcp was in so we can take appropriate action.
1602 1602 */
1603 1603 if (tcp->tcp_state == TCPS_IDLE) {
1604 1604 /*
1605 1605 * We support a quick connect capability here, allowing
1606 1606 * clients to transition directly from IDLE to SYN_SENT
1607 1607 * tcp_bindi will pick an unused port, insert the connection
1608 1608 * in the bind hash and transition to BOUND state.
1609 1609 */
1610 1610 lport = tcp_update_next_port(tcps->tcps_next_port_to_try,
1611 1611 tcp, B_TRUE);
1612 1612 lport = tcp_bindi(tcp, lport, &connp->conn_laddr_v6, 0, B_TRUE,
1613 1613 B_FALSE, B_FALSE);
1614 1614 if (lport == 0)
1615 1615 return (-TNOADDR);
1616 1616 }
1617 1617
1618 1618 /*
1619 1619 * Lookup the route to determine a source address and the uinfo.
1620 1620 * Setup TCP parameters based on the metrics/DCE.
1621 1621 */
1622 1622 error = tcp_set_destination(tcp);
1623 1623 if (error != 0)
1624 1624 return (error);
1625 1625
1626 1626 /*
1627 1627 * Don't let an endpoint connect to itself.
1628 1628 */
1629 1629 if (connp->conn_faddr_v4 == connp->conn_laddr_v4 &&
1630 1630 connp->conn_fport == connp->conn_lport)
1631 1631 return (-TBADADDR);
1632 1632
1633 1633 tcp->tcp_state = TCPS_SYN_SENT;
1634 1634
1635 1635 return (ipcl_conn_insert_v4(connp));
1636 1636 }
1637 1637
1638 1638 /*
1639 1639 * Handle connect to IPv6 destinations.
1640 1640 * Returns zero if OK, a positive errno, or a negative TLI error.
1641 1641 */
1642 1642 static int
1643 1643 tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, in_port_t dstport,
1644 1644 uint32_t flowinfo, uint_t srcid, uint32_t scope_id)
1645 1645 {
1646 1646 uint16_t lport;
1647 1647 conn_t *connp = tcp->tcp_connp;
1648 1648 tcp_stack_t *tcps = tcp->tcp_tcps;
1649 1649 int error;
1650 1650
1651 1651 ASSERT(connp->conn_family == AF_INET6);
1652 1652
1653 1653 /*
1654 1654 * If we're here, it means that the destination address is a native
1655 1655 * IPv6 address. Return an error if conn_ipversion is not IPv6. A
1656 1656 * reason why it might not be IPv6 is if the socket was bound to an
1657 1657 * IPv4-mapped IPv6 address.
1658 1658 */
1659 1659 if (connp->conn_ipversion != IPV6_VERSION)
1660 1660 return (-TBADADDR);
1661 1661
1662 1662 /*
1663 1663 * Interpret a zero destination to mean loopback.
1664 1664 * Update the T_CONN_REQ (sin/sin6) since it is used to
1665 1665 * generate the T_CONN_CON.
1666 1666 */
1667 1667 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp))
1668 1668 *dstaddrp = ipv6_loopback;
1669 1669
1670 1670 /* Handle __sin6_src_id if socket not bound to an IP address */
1671 1671 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6)) {
1672 1672 ip_srcid_find_id(srcid, &connp->conn_laddr_v6,
1673 1673 IPCL_ZONEID(connp), tcps->tcps_netstack);
1674 1674 connp->conn_saddr_v6 = connp->conn_laddr_v6;
1675 1675 }
1676 1676
1677 1677 /*
1678 1678 * Take care of the scope_id now.
1679 1679 */
1680 1680 if (scope_id != 0 && IN6_IS_ADDR_LINKSCOPE(dstaddrp)) {
1681 1681 connp->conn_ixa->ixa_flags |= IXAF_SCOPEID_SET;
1682 1682 connp->conn_ixa->ixa_scopeid = scope_id;
1683 1683 } else {
1684 1684 connp->conn_ixa->ixa_flags &= ~IXAF_SCOPEID_SET;
1685 1685 }
1686 1686
1687 1687 connp->conn_flowinfo = flowinfo;
1688 1688 connp->conn_faddr_v6 = *dstaddrp;
1689 1689 connp->conn_fport = dstport;
1690 1690
1691 1691 /*
1692 1692 * At this point the remote destination address and remote port fields
1693 1693 * in the tcp-four-tuple have been filled in the tcp structure. Now we
1694 1694 * have to see which state tcp was in so we can take appropriate action.
1695 1695 */
1696 1696 if (tcp->tcp_state == TCPS_IDLE) {
1697 1697 /*
1698 1698 * We support a quick connect capability here, allowing
1699 1699 * clients to transition directly from IDLE to SYN_SENT
1700 1700 * tcp_bindi will pick an unused port, insert the connection
1701 1701 * in the bind hash and transition to BOUND state.
1702 1702 */
1703 1703 lport = tcp_update_next_port(tcps->tcps_next_port_to_try,
1704 1704 tcp, B_TRUE);
1705 1705 lport = tcp_bindi(tcp, lport, &connp->conn_laddr_v6, 0, B_TRUE,
1706 1706 B_FALSE, B_FALSE);
1707 1707 if (lport == 0)
1708 1708 return (-TNOADDR);
1709 1709 }
1710 1710
1711 1711 /*
1712 1712 * Lookup the route to determine a source address and the uinfo.
1713 1713 * Setup TCP parameters based on the metrics/DCE.
1714 1714 */
1715 1715 error = tcp_set_destination(tcp);
1716 1716 if (error != 0)
1717 1717 return (error);
1718 1718
1719 1719 /*
1720 1720 * Don't let an endpoint connect to itself.
1721 1721 */
1722 1722 if (IN6_ARE_ADDR_EQUAL(&connp->conn_faddr_v6, &connp->conn_laddr_v6) &&
1723 1723 connp->conn_fport == connp->conn_lport)
1724 1724 return (-TBADADDR);
1725 1725
1726 1726 tcp->tcp_state = TCPS_SYN_SENT;
1727 1727
1728 1728 return (ipcl_conn_insert_v6(connp));
1729 1729 }
1730 1730
1731 1731 /*
1732 1732 * Disconnect
1733 1733 * Note that unlike other functions this returns a positive tli error
1734 1734 * when it fails; it never returns an errno.
1735 1735 */
1736 1736 static int
1737 1737 tcp_disconnect_common(tcp_t *tcp, t_scalar_t seqnum)
1738 1738 {
1739 1739 conn_t *lconnp;
1740 1740 tcp_stack_t *tcps = tcp->tcp_tcps;
1741 1741 conn_t *connp = tcp->tcp_connp;
1742 1742
1743 1743 /*
1744 1744 * Right now, upper modules pass down a T_DISCON_REQ to TCP,
1745 1745 * when the stream is in BOUND state. Do not send a reset,
1746 1746 * since the destination IP address is not valid, and it can
1747 1747 * be the initialized value of all zeros (broadcast address).
1748 1748 */
1749 1749 if (tcp->tcp_state <= TCPS_BOUND) {
1750 1750 if (connp->conn_debug) {
1751 1751 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
1752 1752 "tcp_disconnect: bad state, %d", tcp->tcp_state);
1753 1753 }
1754 1754 return (TOUTSTATE);
1755 1755 } else if (tcp->tcp_state >= TCPS_ESTABLISHED) {
1756 1756 TCPS_CONN_DEC(tcps);
1757 1757 }
1758 1758
1759 1759 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) {
1760 1760
1761 1761 /*
1762 1762 * According to TPI, for non-listeners, ignore seqnum
1763 1763 * and disconnect.
1764 1764 * Following interpretation of -1 seqnum is historical
1765 1765 * and implied TPI ? (TPI only states that for T_CONN_IND,
1766 1766 * a valid seqnum should not be -1).
1767 1767 *
1768 1768 * -1 means disconnect everything
1769 1769 * regardless even on a listener.
1770 1770 */
1771 1771
1772 1772 int old_state = tcp->tcp_state;
1773 1773 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip;
1774 1774
1775 1775 /*
1776 1776 * The connection can't be on the tcp_time_wait_head list
1777 1777 * since it is not detached.
1778 1778 */
1779 1779 ASSERT(tcp->tcp_time_wait_next == NULL);
1780 1780 ASSERT(tcp->tcp_time_wait_prev == NULL);
1781 1781 ASSERT(tcp->tcp_time_wait_expire == 0);
1782 1782 /*
1783 1783 * If it used to be a listener, check to make sure no one else
1784 1784 * has taken the port before switching back to LISTEN state.
1785 1785 */
1786 1786 if (connp->conn_ipversion == IPV4_VERSION) {
1787 1787 lconnp = ipcl_lookup_listener_v4(connp->conn_lport,
1788 1788 connp->conn_laddr_v4, IPCL_ZONEID(connp), ipst);
1789 1789 } else {
1790 1790 uint_t ifindex = 0;
1791 1791
1792 1792 if (connp->conn_ixa->ixa_flags & IXAF_SCOPEID_SET)
1793 1793 ifindex = connp->conn_ixa->ixa_scopeid;
1794 1794
1795 1795 /* Allow conn_bound_if listeners? */
1796 1796 lconnp = ipcl_lookup_listener_v6(connp->conn_lport,
1797 1797 &connp->conn_laddr_v6, ifindex, IPCL_ZONEID(connp),
1798 1798 ipst);
1799 1799 }
1800 1800 if (tcp->tcp_conn_req_max && lconnp == NULL) {
1801 1801 tcp->tcp_state = TCPS_LISTEN;
1802 1802 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
1803 1803 connp->conn_ixa, void, NULL, tcp_t *, tcp, void,
1804 1804 NULL, int32_t, old_state);
1805 1805 } else if (old_state > TCPS_BOUND) {
1806 1806 tcp->tcp_conn_req_max = 0;
1807 1807 tcp->tcp_state = TCPS_BOUND;
1808 1808 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
1809 1809 connp->conn_ixa, void, NULL, tcp_t *, tcp, void,
1810 1810 NULL, int32_t, old_state);
1811 1811
1812 1812 /*
1813 1813 * If this end point is not going to become a listener,
1814 1814 * decrement the listener connection count if
1815 1815 * necessary. Note that we do not do this if it is
1816 1816 * going to be a listner (the above if case) since
1817 1817 * then it may remove the counter struct.
1818 1818 */
1819 1819 if (tcp->tcp_listen_cnt != NULL)
1820 1820 TCP_DECR_LISTEN_CNT(tcp);
1821 1821 }
1822 1822 if (lconnp != NULL)
1823 1823 CONN_DEC_REF(lconnp);
1824 1824 switch (old_state) {
1825 1825 case TCPS_SYN_SENT:
1826 1826 case TCPS_SYN_RCVD:
1827 1827 TCPS_BUMP_MIB(tcps, tcpAttemptFails);
1828 1828 break;
1829 1829 case TCPS_ESTABLISHED:
1830 1830 case TCPS_CLOSE_WAIT:
1831 1831 TCPS_BUMP_MIB(tcps, tcpEstabResets);
1832 1832 break;
1833 1833 }
1834 1834
1835 1835 if (tcp->tcp_fused)
1836 1836 tcp_unfuse(tcp);
1837 1837
1838 1838 mutex_enter(&tcp->tcp_eager_lock);
1839 1839 if ((tcp->tcp_conn_req_cnt_q0 != 0) ||
1840 1840 (tcp->tcp_conn_req_cnt_q != 0)) {
1841 1841 tcp_eager_cleanup(tcp, 0);
1842 1842 }
1843 1843 mutex_exit(&tcp->tcp_eager_lock);
1844 1844
1845 1845 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt,
1846 1846 tcp->tcp_rnxt, TH_RST | TH_ACK);
1847 1847
1848 1848 tcp_reinit(tcp);
1849 1849
1850 1850 return (0);
1851 1851 } else if (!tcp_eager_blowoff(tcp, seqnum)) {
1852 1852 return (TBADSEQ);
1853 1853 }
1854 1854 return (0);
1855 1855 }
1856 1856
1857 1857 /*
1858 1858 * Our client hereby directs us to reject the connection request
1859 1859 * that tcp_input_listener() marked with 'seqnum'. Rejection consists
1860 1860 * of sending the appropriate RST, not an ICMP error.
1861 1861 */
1862 1862 void
1863 1863 tcp_disconnect(tcp_t *tcp, mblk_t *mp)
1864 1864 {
1865 1865 t_scalar_t seqnum;
1866 1866 int error;
1867 1867 conn_t *connp = tcp->tcp_connp;
1868 1868
1869 1869 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX);
1870 1870 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) {
1871 1871 tcp_err_ack(tcp, mp, TPROTO, 0);
1872 1872 return;
1873 1873 }
1874 1874 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number;
1875 1875 error = tcp_disconnect_common(tcp, seqnum);
1876 1876 if (error != 0)
1877 1877 tcp_err_ack(tcp, mp, error, 0);
1878 1878 else {
1879 1879 if (tcp->tcp_state >= TCPS_ESTABLISHED) {
1880 1880 /* Send M_FLUSH according to TPI */
1881 1881 (void) putnextctl1(connp->conn_rq, M_FLUSH, FLUSHRW);
1882 1882 }
1883 1883 mp = mi_tpi_ok_ack_alloc(mp);
1884 1884 if (mp != NULL)
1885 1885 putnext(connp->conn_rq, mp);
1886 1886 }
1887 1887 }
1888 1888
1889 1889 /*
1890 1890 * Handle reinitialization of a tcp structure.
1891 1891 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE.
1892 1892 */
1893 1893 static void
1894 1894 tcp_reinit(tcp_t *tcp)
1895 1895 {
1896 1896 mblk_t *mp;
1897 1897 tcp_stack_t *tcps = tcp->tcp_tcps;
1898 1898 conn_t *connp = tcp->tcp_connp;
1899 1899 int32_t oldstate;
1900 1900
1901 1901 /* tcp_reinit should never be called for detached tcp_t's */
1902 1902 ASSERT(tcp->tcp_listener == NULL);
1903 1903 ASSERT((connp->conn_family == AF_INET &&
1904 1904 connp->conn_ipversion == IPV4_VERSION) ||
1905 1905 (connp->conn_family == AF_INET6 &&
1906 1906 (connp->conn_ipversion == IPV4_VERSION ||
1907 1907 connp->conn_ipversion == IPV6_VERSION)));
1908 1908
1909 1909 /* Cancel outstanding timers */
1910 1910 tcp_timers_stop(tcp);
1911 1911
1912 1912 /*
1913 1913 * Reset everything in the state vector, after updating global
1914 1914 * MIB data from instance counters.
1915 1915 */
1916 1916 TCPS_UPDATE_MIB(tcps, tcpHCInSegs, tcp->tcp_ibsegs);
1917 1917 tcp->tcp_ibsegs = 0;
1918 1918 TCPS_UPDATE_MIB(tcps, tcpHCOutSegs, tcp->tcp_obsegs);
1919 1919 tcp->tcp_obsegs = 0;
1920 1920
1921 1921 tcp_close_mpp(&tcp->tcp_xmit_head);
1922 1922 if (tcp->tcp_snd_zcopy_aware)
1923 1923 tcp_zcopy_notify(tcp);
1924 1924 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL;
1925 1925 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0;
1926 1926 mutex_enter(&tcp->tcp_non_sq_lock);
1927 1927 if (tcp->tcp_flow_stopped &&
1928 1928 TCP_UNSENT_BYTES(tcp) <= connp->conn_sndlowat) {
1929 1929 tcp_clrqfull(tcp);
1930 1930 }
1931 1931 mutex_exit(&tcp->tcp_non_sq_lock);
1932 1932 tcp_close_mpp(&tcp->tcp_reass_head);
1933 1933 tcp->tcp_reass_tail = NULL;
1934 1934 if (tcp->tcp_rcv_list != NULL) {
1935 1935 /* Free b_next chain */
1936 1936 tcp_close_mpp(&tcp->tcp_rcv_list);
1937 1937 tcp->tcp_rcv_last_head = NULL;
1938 1938 tcp->tcp_rcv_last_tail = NULL;
1939 1939 tcp->tcp_rcv_cnt = 0;
1940 1940 }
1941 1941 tcp->tcp_rcv_last_tail = NULL;
1942 1942
1943 1943 if ((mp = tcp->tcp_urp_mp) != NULL) {
1944 1944 freemsg(mp);
1945 1945 tcp->tcp_urp_mp = NULL;
1946 1946 }
1947 1947 if ((mp = tcp->tcp_urp_mark_mp) != NULL) {
1948 1948 freemsg(mp);
1949 1949 tcp->tcp_urp_mark_mp = NULL;
1950 1950 }
1951 1951 if (tcp->tcp_fused_sigurg_mp != NULL) {
1952 1952 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1953 1953 freeb(tcp->tcp_fused_sigurg_mp);
1954 1954 tcp->tcp_fused_sigurg_mp = NULL;
1955 1955 }
1956 1956 if (tcp->tcp_ordrel_mp != NULL) {
1957 1957 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1958 1958 freeb(tcp->tcp_ordrel_mp);
1959 1959 tcp->tcp_ordrel_mp = NULL;
1960 1960 }
1961 1961
1962 1962 /*
1963 1963 * Following is a union with two members which are
1964 1964 * identical types and size so the following cleanup
1965 1965 * is enough.
1966 1966 */
1967 1967 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind);
1968 1968
1969 1969 CL_INET_DISCONNECT(connp);
1970 1970
1971 1971 /*
1972 1972 * The connection can't be on the tcp_time_wait_head list
1973 1973 * since it is not detached.
1974 1974 */
1975 1975 ASSERT(tcp->tcp_time_wait_next == NULL);
1976 1976 ASSERT(tcp->tcp_time_wait_prev == NULL);
1977 1977 ASSERT(tcp->tcp_time_wait_expire == 0);
1978 1978
1979 1979 /*
1980 1980 * Reset/preserve other values
1981 1981 */
1982 1982 tcp_reinit_values(tcp);
1983 1983 ipcl_hash_remove(connp);
1984 1984 /* Note that ixa_cred gets cleared in ixa_cleanup */
1985 1985 ixa_cleanup(connp->conn_ixa);
1986 1986 tcp_ipsec_cleanup(tcp);
1987 1987
1988 1988 connp->conn_laddr_v6 = connp->conn_bound_addr_v6;
1989 1989 connp->conn_saddr_v6 = connp->conn_bound_addr_v6;
1990 1990 oldstate = tcp->tcp_state;
1991 1991
1992 1992 if (tcp->tcp_conn_req_max != 0) {
1993 1993 /*
1994 1994 * This is the case when a TLI program uses the same
1995 1995 * transport end point to accept a connection. This
1996 1996 * makes the TCP both a listener and acceptor. When
1997 1997 * this connection is closed, we need to set the state
1998 1998 * back to TCPS_LISTEN. Make sure that the eager list
1999 1999 * is reinitialized.
2000 2000 *
2001 2001 * Note that this stream is still bound to the four
2002 2002 * tuples of the previous connection in IP. If a new
2003 2003 * SYN with different foreign address comes in, IP will
2004 2004 * not find it and will send it to the global queue. In
2005 2005 * the global queue, TCP will do a tcp_lookup_listener()
2006 2006 * to find this stream. This works because this stream
2007 2007 * is only removed from connected hash.
2008 2008 *
2009 2009 */
2010 2010 tcp->tcp_state = TCPS_LISTEN;
2011 2011 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp;
2012 2012 tcp->tcp_eager_next_drop_q0 = tcp;
2013 2013 tcp->tcp_eager_prev_drop_q0 = tcp;
2014 2014 /*
2015 2015 * Initially set conn_recv to tcp_input_listener_unbound to try
2016 2016 * to pick a good squeue for the listener when the first SYN
2017 2017 * arrives. tcp_input_listener_unbound sets it to
2018 2018 * tcp_input_listener on that first SYN.
2019 2019 */
2020 2020 connp->conn_recv = tcp_input_listener_unbound;
2021 2021
2022 2022 connp->conn_proto = IPPROTO_TCP;
2023 2023 connp->conn_faddr_v6 = ipv6_all_zeros;
2024 2024 connp->conn_fport = 0;
2025 2025
2026 2026 (void) ipcl_bind_insert(connp);
2027 2027 } else {
2028 2028 tcp->tcp_state = TCPS_BOUND;
2029 2029 }
2030 2030
2031 2031 /*
2032 2032 * Initialize to default values
2033 2033 */
2034 2034 tcp_init_values(tcp, NULL);
2035 2035
2036 2036 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
2037 2037 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
2038 2038 int32_t, oldstate);
2039 2039
2040 2040 ASSERT(tcp->tcp_ptpbhn != NULL);
2041 2041 tcp->tcp_rwnd = connp->conn_rcvbuf;
2042 2042 tcp->tcp_mss = connp->conn_ipversion != IPV4_VERSION ?
2043 2043 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4;
2044 2044 }
2045 2045
2046 2046 /*
2047 2047 * Force values to zero that need be zero.
2048 2048 * Do not touch values asociated with the BOUND or LISTEN state
2049 2049 * since the connection will end up in that state after the reinit.
2050 2050 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t
2051 2051 * structure!
2052 2052 */
2053 2053 static void
2054 2054 tcp_reinit_values(tcp)
2055 2055 tcp_t *tcp;
2056 2056 {
2057 2057 tcp_stack_t *tcps = tcp->tcp_tcps;
2058 2058 conn_t *connp = tcp->tcp_connp;
2059 2059
2060 2060 #ifndef lint
2061 2061 #define DONTCARE(x)
2062 2062 #define PRESERVE(x)
2063 2063 #else
2064 2064 #define DONTCARE(x) ((x) = (x))
2065 2065 #define PRESERVE(x) ((x) = (x))
2066 2066 #endif /* lint */
2067 2067
2068 2068 PRESERVE(tcp->tcp_bind_hash_port);
2069 2069 PRESERVE(tcp->tcp_bind_hash);
2070 2070 PRESERVE(tcp->tcp_ptpbhn);
2071 2071 PRESERVE(tcp->tcp_acceptor_hash);
2072 2072 PRESERVE(tcp->tcp_ptpahn);
2073 2073
2074 2074 /* Should be ASSERT NULL on these with new code! */
2075 2075 ASSERT(tcp->tcp_time_wait_next == NULL);
2076 2076 ASSERT(tcp->tcp_time_wait_prev == NULL);
2077 2077 ASSERT(tcp->tcp_time_wait_expire == 0);
2078 2078 PRESERVE(tcp->tcp_state);
2079 2079 PRESERVE(connp->conn_rq);
2080 2080 PRESERVE(connp->conn_wq);
2081 2081
2082 2082 ASSERT(tcp->tcp_xmit_head == NULL);
2083 2083 ASSERT(tcp->tcp_xmit_last == NULL);
2084 2084 ASSERT(tcp->tcp_unsent == 0);
2085 2085 ASSERT(tcp->tcp_xmit_tail == NULL);
2086 2086 ASSERT(tcp->tcp_xmit_tail_unsent == 0);
2087 2087
2088 2088 tcp->tcp_snxt = 0; /* Displayed in mib */
2089 2089 tcp->tcp_suna = 0; /* Displayed in mib */
2090 2090 tcp->tcp_swnd = 0;
2091 2091 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_process_options */
2092 2092
2093 2093 ASSERT(tcp->tcp_ibsegs == 0);
2094 2094 ASSERT(tcp->tcp_obsegs == 0);
2095 2095
2096 2096 if (connp->conn_ht_iphc != NULL) {
2097 2097 kmem_free(connp->conn_ht_iphc, connp->conn_ht_iphc_allocated);
2098 2098 connp->conn_ht_iphc = NULL;
2099 2099 connp->conn_ht_iphc_allocated = 0;
2100 2100 connp->conn_ht_iphc_len = 0;
2101 2101 connp->conn_ht_ulp = NULL;
2102 2102 connp->conn_ht_ulp_len = 0;
2103 2103 tcp->tcp_ipha = NULL;
2104 2104 tcp->tcp_ip6h = NULL;
2105 2105 tcp->tcp_tcpha = NULL;
2106 2106 }
2107 2107
2108 2108 /* We clear any IP_OPTIONS and extension headers */
2109 2109 ip_pkt_free(&connp->conn_xmit_ipp);
2110 2110
2111 2111 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */
2112 2112 DONTCARE(tcp->tcp_ipha);
2113 2113 DONTCARE(tcp->tcp_ip6h);
2114 2114 DONTCARE(tcp->tcp_tcpha);
2115 2115 tcp->tcp_valid_bits = 0;
2116 2116
2117 2117 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */
2118 2118 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */
2119 2119 tcp->tcp_last_rcv_lbolt = 0;
2120 2120
2121 2121 tcp->tcp_init_cwnd = 0;
2122 2122
2123 2123 tcp->tcp_urp_last_valid = 0;
2124 2124 tcp->tcp_hard_binding = 0;
2125 2125
2126 2126 tcp->tcp_fin_acked = 0;
2127 2127 tcp->tcp_fin_rcvd = 0;
2128 2128 tcp->tcp_fin_sent = 0;
2129 2129 tcp->tcp_ordrel_done = 0;
2130 2130
2131 2131 tcp->tcp_detached = 0;
2132 2132
2133 2133 tcp->tcp_snd_ws_ok = B_FALSE;
2134 2134 tcp->tcp_snd_ts_ok = B_FALSE;
2135 2135 tcp->tcp_zero_win_probe = 0;
2136 2136
2137 2137 tcp->tcp_loopback = 0;
2138 2138 tcp->tcp_localnet = 0;
2139 2139 tcp->tcp_syn_defense = 0;
2140 2140 tcp->tcp_set_timer = 0;
2141 2141
2142 2142 tcp->tcp_active_open = 0;
2143 2143 tcp->tcp_rexmit = B_FALSE;
2144 2144 tcp->tcp_xmit_zc_clean = B_FALSE;
2145 2145
2146 2146 tcp->tcp_snd_sack_ok = B_FALSE;
2147 2147 tcp->tcp_hwcksum = B_FALSE;
2148 2148
2149 2149 DONTCARE(tcp->tcp_maxpsz_multiplier); /* Init in tcp_init_values */
2150 2150
2151 2151 tcp->tcp_conn_def_q0 = 0;
2152 2152 tcp->tcp_ip_forward_progress = B_FALSE;
2153 2153 tcp->tcp_ecn_ok = B_FALSE;
2154 2154
2155 2155 tcp->tcp_cwr = B_FALSE;
2156 2156 tcp->tcp_ecn_echo_on = B_FALSE;
2157 2157 tcp->tcp_is_wnd_shrnk = B_FALSE;
2158 2158
2159 2159 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp);
2160 2160 bzero(&tcp->tcp_sack_info, sizeof (tcp_sack_info_t));
2161 2161
2162 2162 tcp->tcp_rcv_ws = 0;
2163 2163 tcp->tcp_snd_ws = 0;
2164 2164 tcp->tcp_ts_recent = 0;
2165 2165 tcp->tcp_rnxt = 0; /* Displayed in mib */
2166 2166 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */
2167 2167 tcp->tcp_initial_pmtu = 0;
2168 2168
2169 2169 ASSERT(tcp->tcp_reass_head == NULL);
2170 2170 ASSERT(tcp->tcp_reass_tail == NULL);
2171 2171
2172 2172 tcp->tcp_cwnd_cnt = 0;
2173 2173
2174 2174 ASSERT(tcp->tcp_rcv_list == NULL);
2175 2175 ASSERT(tcp->tcp_rcv_last_head == NULL);
2176 2176 ASSERT(tcp->tcp_rcv_last_tail == NULL);
2177 2177 ASSERT(tcp->tcp_rcv_cnt == 0);
2178 2178
2179 2179 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_set_destination */
2180 2180 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */
2181 2181 tcp->tcp_csuna = 0;
2182 2182
2183 2183 tcp->tcp_rto = 0; /* Displayed in MIB */
2184 2184 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */
2185 2185 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */
2186 2186 tcp->tcp_rtt_update = 0;
2187 2187
2188 2188 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */
2189 2189 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */
2190 2190
2191 2191 tcp->tcp_rack = 0; /* Displayed in mib */
2192 2192 tcp->tcp_rack_cnt = 0;
2193 2193 tcp->tcp_rack_cur_max = 0;
2194 2194 tcp->tcp_rack_abs_max = 0;
2195 2195
2196 2196 tcp->tcp_max_swnd = 0;
2197 2197
2198 2198 ASSERT(tcp->tcp_listener == NULL);
2199 2199
2200 2200 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */
2201 2201 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */
2202 2202 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */
2203 2203 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */
2204 2204
2205 2205 ASSERT(tcp->tcp_conn_req_cnt_q == 0);
2206 2206 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0);
2207 2207 PRESERVE(tcp->tcp_conn_req_max);
2208 2208 PRESERVE(tcp->tcp_conn_req_seqnum);
2209 2209
2210 2210 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */
2211 2211 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */
2212 2212 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */
2213 2213 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */
2214 2214
2215 2215 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */
2216 2216 ASSERT(tcp->tcp_urp_mp == NULL);
2217 2217 ASSERT(tcp->tcp_urp_mark_mp == NULL);
2218 2218 ASSERT(tcp->tcp_fused_sigurg_mp == NULL);
2219 2219
2220 2220 ASSERT(tcp->tcp_eager_next_q == NULL);
|
↓ open down ↓ |
2220 lines elided |
↑ open up ↑ |
2221 2221 ASSERT(tcp->tcp_eager_last_q == NULL);
2222 2222 ASSERT((tcp->tcp_eager_next_q0 == NULL &&
2223 2223 tcp->tcp_eager_prev_q0 == NULL) ||
2224 2224 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0);
2225 2225 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL);
2226 2226
2227 2227 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL &&
2228 2228 tcp->tcp_eager_prev_drop_q0 == NULL) ||
2229 2229 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0);
2230 2230
2231 + DONTCARE(tcp->tcp_ka_rinterval); /* Init in tcp_init_values */
2232 + DONTCARE(tcp->tcp_ka_ka_abort_thres); /* Init in tcp_init_values */
2233 + DONTCARE(tcp->tcp_ka_ka_cnt); /* Init in tcp_init_values */
2234 +
2231 2235 tcp->tcp_client_errno = 0;
2232 2236
2233 2237 DONTCARE(connp->conn_sum); /* Init in tcp_init_values */
2234 2238
2235 2239 connp->conn_faddr_v6 = ipv6_all_zeros; /* Displayed in MIB */
2236 2240
2237 2241 PRESERVE(connp->conn_bound_addr_v6);
2238 2242 tcp->tcp_last_sent_len = 0;
2239 2243 tcp->tcp_dupack_cnt = 0;
2240 2244
2241 2245 connp->conn_fport = 0; /* Displayed in MIB */
2242 2246 PRESERVE(connp->conn_lport);
2243 2247
2244 2248 PRESERVE(tcp->tcp_acceptor_lockp);
2245 2249
2246 2250 ASSERT(tcp->tcp_ordrel_mp == NULL);
2247 2251 PRESERVE(tcp->tcp_acceptor_id);
2248 2252 DONTCARE(tcp->tcp_ipsec_overhead);
2249 2253
2250 2254 PRESERVE(connp->conn_family);
2251 2255 /* Remove any remnants of mapped address binding */
2252 2256 if (connp->conn_family == AF_INET6) {
2253 2257 connp->conn_ipversion = IPV6_VERSION;
2254 2258 tcp->tcp_mss = tcps->tcps_mss_def_ipv6;
2255 2259 } else {
2256 2260 connp->conn_ipversion = IPV4_VERSION;
2257 2261 tcp->tcp_mss = tcps->tcps_mss_def_ipv4;
2258 2262 }
2259 2263
2260 2264 connp->conn_bound_if = 0;
2261 2265 connp->conn_recv_ancillary.crb_all = 0;
2262 2266 tcp->tcp_recvifindex = 0;
2263 2267 tcp->tcp_recvhops = 0;
2264 2268 tcp->tcp_closed = 0;
2265 2269 if (tcp->tcp_hopopts != NULL) {
2266 2270 mi_free(tcp->tcp_hopopts);
2267 2271 tcp->tcp_hopopts = NULL;
2268 2272 tcp->tcp_hopoptslen = 0;
2269 2273 }
2270 2274 ASSERT(tcp->tcp_hopoptslen == 0);
2271 2275 if (tcp->tcp_dstopts != NULL) {
2272 2276 mi_free(tcp->tcp_dstopts);
2273 2277 tcp->tcp_dstopts = NULL;
2274 2278 tcp->tcp_dstoptslen = 0;
2275 2279 }
2276 2280 ASSERT(tcp->tcp_dstoptslen == 0);
2277 2281 if (tcp->tcp_rthdrdstopts != NULL) {
2278 2282 mi_free(tcp->tcp_rthdrdstopts);
2279 2283 tcp->tcp_rthdrdstopts = NULL;
2280 2284 tcp->tcp_rthdrdstoptslen = 0;
2281 2285 }
2282 2286 ASSERT(tcp->tcp_rthdrdstoptslen == 0);
2283 2287 if (tcp->tcp_rthdr != NULL) {
2284 2288 mi_free(tcp->tcp_rthdr);
2285 2289 tcp->tcp_rthdr = NULL;
2286 2290 tcp->tcp_rthdrlen = 0;
2287 2291 }
2288 2292 ASSERT(tcp->tcp_rthdrlen == 0);
2289 2293
2290 2294 /* Reset fusion-related fields */
2291 2295 tcp->tcp_fused = B_FALSE;
2292 2296 tcp->tcp_unfusable = B_FALSE;
2293 2297 tcp->tcp_fused_sigurg = B_FALSE;
2294 2298 tcp->tcp_loopback_peer = NULL;
2295 2299
2296 2300 tcp->tcp_lso = B_FALSE;
2297 2301
2298 2302 tcp->tcp_in_ack_unsent = 0;
2299 2303 tcp->tcp_cork = B_FALSE;
2300 2304 tcp->tcp_tconnind_started = B_FALSE;
2301 2305
2302 2306 PRESERVE(tcp->tcp_squeue_bytes);
2303 2307
2304 2308 tcp->tcp_closemp_used = B_FALSE;
2305 2309
2306 2310 PRESERVE(tcp->tcp_rsrv_mp);
2307 2311 PRESERVE(tcp->tcp_rsrv_mp_lock);
2308 2312
2309 2313 #ifdef DEBUG
2310 2314 DONTCARE(tcp->tcmp_stk[0]);
2311 2315 #endif
2312 2316
2313 2317 PRESERVE(tcp->tcp_connid);
2314 2318
2315 2319 ASSERT(tcp->tcp_listen_cnt == NULL);
2316 2320 ASSERT(tcp->tcp_reass_tid == 0);
2317 2321
2318 2322 #undef DONTCARE
2319 2323 #undef PRESERVE
2320 2324 }
2321 2325
2322 2326 /*
2323 2327 * Initialize the various fields in tcp_t. If parent (the listener) is non
2324 2328 * NULL, certain values will be inheritted from it.
2325 2329 */
2326 2330 void
2327 2331 tcp_init_values(tcp_t *tcp, tcp_t *parent)
2328 2332 {
2329 2333 tcp_stack_t *tcps = tcp->tcp_tcps;
2330 2334 conn_t *connp = tcp->tcp_connp;
2331 2335 clock_t rto;
2332 2336
2333 2337 ASSERT((connp->conn_family == AF_INET &&
2334 2338 connp->conn_ipversion == IPV4_VERSION) ||
2335 2339 (connp->conn_family == AF_INET6 &&
2336 2340 (connp->conn_ipversion == IPV4_VERSION ||
2337 2341 connp->conn_ipversion == IPV6_VERSION)));
2338 2342
2339 2343 if (parent == NULL) {
2340 2344 tcp->tcp_naglim = tcps->tcps_naglim_def;
2341 2345
2342 2346 tcp->tcp_rto_initial = tcps->tcps_rexmit_interval_initial;
2343 2347 tcp->tcp_rto_min = tcps->tcps_rexmit_interval_min;
2344 2348 tcp->tcp_rto_max = tcps->tcps_rexmit_interval_max;
2345 2349
2346 2350 tcp->tcp_first_ctimer_threshold =
2347 2351 tcps->tcps_ip_notify_cinterval;
2348 2352 tcp->tcp_second_ctimer_threshold =
2349 2353 tcps->tcps_ip_abort_cinterval;
2350 2354 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval;
2351 2355 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval;
2352 2356
2353 2357 tcp->tcp_fin_wait_2_flush_interval =
2354 2358 tcps->tcps_fin_wait_2_flush_interval;
2355 2359
2356 2360 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval;
2357 2361 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval;
2358 2362 tcp->tcp_ka_cnt = 0;
2359 2363 tcp->tcp_ka_rinterval = 0;
2360 2364
2361 2365 /*
2362 2366 * Default value of tcp_init_cwnd is 0, so no need to set here
2363 2367 * if parent is NULL. But we need to inherit it from parent.
2364 2368 */
2365 2369 } else {
2366 2370 /* Inherit various TCP parameters from the parent. */
2367 2371 tcp->tcp_naglim = parent->tcp_naglim;
2368 2372
2369 2373 tcp->tcp_rto_initial = parent->tcp_rto_initial;
2370 2374 tcp->tcp_rto_min = parent->tcp_rto_min;
2371 2375 tcp->tcp_rto_max = parent->tcp_rto_max;
2372 2376
2373 2377 tcp->tcp_first_ctimer_threshold =
2374 2378 parent->tcp_first_ctimer_threshold;
2375 2379 tcp->tcp_second_ctimer_threshold =
2376 2380 parent->tcp_second_ctimer_threshold;
|
↓ open down ↓ |
136 lines elided |
↑ open up ↑ |
2377 2381 tcp->tcp_first_timer_threshold =
2378 2382 parent->tcp_first_timer_threshold;
2379 2383 tcp->tcp_second_timer_threshold =
2380 2384 parent->tcp_second_timer_threshold;
2381 2385
2382 2386 tcp->tcp_fin_wait_2_flush_interval =
2383 2387 parent->tcp_fin_wait_2_flush_interval;
2384 2388
2385 2389 tcp->tcp_ka_interval = parent->tcp_ka_interval;
2386 2390 tcp->tcp_ka_abort_thres = parent->tcp_ka_abort_thres;
2391 + tcp->tcp_ka_cnt = parent->tcp_ka_cnt;
2392 + tcp->tcp_ka_rinterval = parent->tcp_ka_rinterval;
2387 2393
2388 2394 tcp->tcp_init_cwnd = parent->tcp_init_cwnd;
2389 2395 }
2390 2396
2391 2397 /*
2392 2398 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO
2393 2399 * will be close to tcp_rexmit_interval_initial. By doing this, we
2394 2400 * allow the algorithm to adjust slowly to large fluctuations of RTT
2395 2401 * during first few transmissions of a connection as seen in slow
2396 2402 * links.
2397 2403 */
2398 2404 tcp->tcp_rtt_sa = tcp->tcp_rto_initial << 2;
2399 2405 tcp->tcp_rtt_sd = tcp->tcp_rto_initial >> 1;
2400 2406 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd +
2401 2407 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) +
2402 2408 tcps->tcps_conn_grace_period;
2403 2409 TCP_SET_RTO(tcp, rto);
2404 2410
2405 2411 tcp->tcp_timer_backoff = 0;
2406 2412 tcp->tcp_ms_we_have_waited = 0;
2407 2413 tcp->tcp_last_recv_time = ddi_get_lbolt();
2408 2414 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_;
2409 2415 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN;
2410 2416 tcp->tcp_snd_burst = TCP_CWND_INFINITE;
2411 2417
2412 2418 tcp->tcp_maxpsz_multiplier = tcps->tcps_maxpsz_multiplier;
2413 2419
2414 2420 /* NOTE: ISS is now set in tcp_set_destination(). */
2415 2421
2416 2422 /* Reset fusion-related fields */
2417 2423 tcp->tcp_fused = B_FALSE;
2418 2424 tcp->tcp_unfusable = B_FALSE;
2419 2425 tcp->tcp_fused_sigurg = B_FALSE;
2420 2426 tcp->tcp_loopback_peer = NULL;
2421 2427
2422 2428 /* We rebuild the header template on the next connect/conn_request */
2423 2429
2424 2430 connp->conn_mlp_type = mlptSingle;
2425 2431
2426 2432 /*
2427 2433 * Init the window scale to the max so tcp_rwnd_set() won't pare
2428 2434 * down tcp_rwnd. tcp_set_destination() will set the right value later.
2429 2435 */
2430 2436 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT;
2431 2437 tcp->tcp_rwnd = connp->conn_rcvbuf;
2432 2438
2433 2439 tcp->tcp_cork = B_FALSE;
2434 2440 /*
2435 2441 * Init the tcp_debug option if it wasn't already set. This value
2436 2442 * determines whether TCP
2437 2443 * calls strlog() to print out debug messages. Doing this
2438 2444 * initialization here means that this value is not inherited thru
2439 2445 * tcp_reinit().
2440 2446 */
2441 2447 if (!connp->conn_debug)
2442 2448 connp->conn_debug = tcps->tcps_dbg;
2443 2449 }
2444 2450
2445 2451 /*
2446 2452 * Update the TCP connection according to change of PMTU.
2447 2453 *
2448 2454 * Path MTU might have changed by either increase or decrease, so need to
2449 2455 * adjust the MSS based on the value of ixa_pmtu. No need to handle tiny
2450 2456 * or negative MSS, since tcp_mss_set() will do it.
2451 2457 */
2452 2458 void
2453 2459 tcp_update_pmtu(tcp_t *tcp, boolean_t decrease_only)
2454 2460 {
2455 2461 uint32_t pmtu;
2456 2462 int32_t mss;
2457 2463 conn_t *connp = tcp->tcp_connp;
2458 2464 ip_xmit_attr_t *ixa = connp->conn_ixa;
2459 2465 iaflags_t ixaflags;
2460 2466
2461 2467 if (tcp->tcp_tcps->tcps_ignore_path_mtu)
2462 2468 return;
2463 2469
2464 2470 if (tcp->tcp_state < TCPS_ESTABLISHED)
2465 2471 return;
2466 2472
2467 2473 /*
2468 2474 * Always call ip_get_pmtu() to make sure that IP has updated
2469 2475 * ixa_flags properly.
2470 2476 */
2471 2477 pmtu = ip_get_pmtu(ixa);
2472 2478 ixaflags = ixa->ixa_flags;
2473 2479
2474 2480 /*
2475 2481 * Calculate the MSS by decreasing the PMTU by conn_ht_iphc_len and
2476 2482 * IPsec overhead if applied. Make sure to use the most recent
2477 2483 * IPsec information.
2478 2484 */
2479 2485 mss = pmtu - connp->conn_ht_iphc_len - conn_ipsec_length(connp);
2480 2486
2481 2487 /*
2482 2488 * Nothing to change, so just return.
2483 2489 */
2484 2490 if (mss == tcp->tcp_mss)
2485 2491 return;
2486 2492
2487 2493 /*
2488 2494 * Currently, for ICMP errors, only PMTU decrease is handled.
2489 2495 */
2490 2496 if (mss > tcp->tcp_mss && decrease_only)
2491 2497 return;
2492 2498
2493 2499 DTRACE_PROBE2(tcp_update_pmtu, int32_t, tcp->tcp_mss, uint32_t, mss);
2494 2500
2495 2501 /*
2496 2502 * Update ixa_fragsize and ixa_pmtu.
2497 2503 */
2498 2504 ixa->ixa_fragsize = ixa->ixa_pmtu = pmtu;
2499 2505
2500 2506 /*
2501 2507 * Adjust MSS and all relevant variables.
2502 2508 */
2503 2509 tcp_mss_set(tcp, mss);
2504 2510
2505 2511 /*
2506 2512 * If the PMTU is below the min size maintained by IP, then ip_get_pmtu
2507 2513 * has set IXAF_PMTU_TOO_SMALL and cleared IXAF_PMTU_IPV4_DF. Since TCP
2508 2514 * has a (potentially different) min size we do the same. Make sure to
2509 2515 * clear IXAF_DONTFRAG, which is used by IP to decide whether to
2510 2516 * fragment the packet.
2511 2517 *
2512 2518 * LSO over IPv6 can not be fragmented. So need to disable LSO
2513 2519 * when IPv6 fragmentation is needed.
2514 2520 */
2515 2521 if (mss < tcp->tcp_tcps->tcps_mss_min)
2516 2522 ixaflags |= IXAF_PMTU_TOO_SMALL;
2517 2523
2518 2524 if (ixaflags & IXAF_PMTU_TOO_SMALL)
2519 2525 ixaflags &= ~(IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
2520 2526
2521 2527 if ((connp->conn_ipversion == IPV4_VERSION) &&
2522 2528 !(ixaflags & IXAF_PMTU_IPV4_DF)) {
2523 2529 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0;
2524 2530 }
2525 2531 ixa->ixa_flags = ixaflags;
2526 2532 }
2527 2533
2528 2534 int
2529 2535 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk)
2530 2536 {
2531 2537 conn_t *connp = tcp->tcp_connp;
2532 2538 queue_t *q = connp->conn_rq;
2533 2539 int32_t mss = tcp->tcp_mss;
2534 2540 int maxpsz;
2535 2541
2536 2542 if (TCP_IS_DETACHED(tcp))
2537 2543 return (mss);
2538 2544 if (tcp->tcp_fused) {
2539 2545 maxpsz = tcp_fuse_maxpsz(tcp);
2540 2546 mss = INFPSZ;
2541 2547 } else if (tcp->tcp_maxpsz_multiplier == 0) {
2542 2548 /*
2543 2549 * Set the sd_qn_maxpsz according to the socket send buffer
2544 2550 * size, and sd_maxblk to INFPSZ (-1). This will essentially
2545 2551 * instruct the stream head to copyin user data into contiguous
2546 2552 * kernel-allocated buffers without breaking it up into smaller
2547 2553 * chunks. We round up the buffer size to the nearest SMSS.
2548 2554 */
2549 2555 maxpsz = MSS_ROUNDUP(connp->conn_sndbuf, mss);
2550 2556 mss = INFPSZ;
2551 2557 } else {
2552 2558 /*
2553 2559 * Set sd_qn_maxpsz to approx half the (receivers) buffer
2554 2560 * (and a multiple of the mss). This instructs the stream
2555 2561 * head to break down larger than SMSS writes into SMSS-
2556 2562 * size mblks, up to tcp_maxpsz_multiplier mblks at a time.
2557 2563 */
2558 2564 maxpsz = tcp->tcp_maxpsz_multiplier * mss;
2559 2565 if (maxpsz > connp->conn_sndbuf / 2) {
2560 2566 maxpsz = connp->conn_sndbuf / 2;
2561 2567 /* Round up to nearest mss */
2562 2568 maxpsz = MSS_ROUNDUP(maxpsz, mss);
2563 2569 }
2564 2570 }
2565 2571
2566 2572 (void) proto_set_maxpsz(q, connp, maxpsz);
2567 2573 if (!(IPCL_IS_NONSTR(connp)))
2568 2574 connp->conn_wq->q_maxpsz = maxpsz;
2569 2575 if (set_maxblk)
2570 2576 (void) proto_set_tx_maxblk(q, connp, mss);
2571 2577 return (mss);
2572 2578 }
2573 2579
2574 2580 /* For /dev/tcp aka AF_INET open */
2575 2581 static int
2576 2582 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
2577 2583 {
2578 2584 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE));
2579 2585 }
2580 2586
2581 2587 /* For /dev/tcp6 aka AF_INET6 open */
2582 2588 static int
2583 2589 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
2584 2590 {
2585 2591 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE));
2586 2592 }
2587 2593
2588 2594 conn_t *
2589 2595 tcp_create_common(cred_t *credp, boolean_t isv6, boolean_t issocket,
2590 2596 int *errorp)
2591 2597 {
2592 2598 tcp_t *tcp = NULL;
2593 2599 conn_t *connp;
2594 2600 zoneid_t zoneid;
2595 2601 tcp_stack_t *tcps;
2596 2602 squeue_t *sqp;
2597 2603
2598 2604 ASSERT(errorp != NULL);
2599 2605 /*
2600 2606 * Find the proper zoneid and netstack.
2601 2607 */
2602 2608 /*
2603 2609 * Special case for install: miniroot needs to be able to
2604 2610 * access files via NFS as though it were always in the
2605 2611 * global zone.
2606 2612 */
2607 2613 if (credp == kcred && nfs_global_client_only != 0) {
2608 2614 zoneid = GLOBAL_ZONEID;
2609 2615 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->
2610 2616 netstack_tcp;
2611 2617 ASSERT(tcps != NULL);
2612 2618 } else {
2613 2619 netstack_t *ns;
2614 2620 int err;
2615 2621
2616 2622 if ((err = secpolicy_basic_net_access(credp)) != 0) {
2617 2623 *errorp = err;
2618 2624 return (NULL);
2619 2625 }
2620 2626
2621 2627 ns = netstack_find_by_cred(credp);
2622 2628 ASSERT(ns != NULL);
2623 2629 tcps = ns->netstack_tcp;
2624 2630 ASSERT(tcps != NULL);
2625 2631
2626 2632 /*
2627 2633 * For exclusive stacks we set the zoneid to zero
2628 2634 * to make TCP operate as if in the global zone.
2629 2635 */
2630 2636 if (tcps->tcps_netstack->netstack_stackid !=
2631 2637 GLOBAL_NETSTACKID)
2632 2638 zoneid = GLOBAL_ZONEID;
2633 2639 else
2634 2640 zoneid = crgetzoneid(credp);
2635 2641 }
2636 2642
2637 2643 sqp = IP_SQUEUE_GET((uint_t)gethrtime());
2638 2644 connp = (conn_t *)tcp_get_conn(sqp, tcps);
2639 2645 /*
2640 2646 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt,
2641 2647 * so we drop it by one.
2642 2648 */
2643 2649 netstack_rele(tcps->tcps_netstack);
2644 2650 if (connp == NULL) {
2645 2651 *errorp = ENOSR;
2646 2652 return (NULL);
2647 2653 }
2648 2654 ASSERT(connp->conn_ixa->ixa_protocol == connp->conn_proto);
2649 2655
2650 2656 connp->conn_sqp = sqp;
2651 2657 connp->conn_initial_sqp = connp->conn_sqp;
2652 2658 connp->conn_ixa->ixa_sqp = connp->conn_sqp;
2653 2659 tcp = connp->conn_tcp;
2654 2660
2655 2661 /*
2656 2662 * Besides asking IP to set the checksum for us, have conn_ip_output
2657 2663 * to do the following checks when necessary:
2658 2664 *
2659 2665 * IXAF_VERIFY_SOURCE: drop packets when our outer source goes invalid
2660 2666 * IXAF_VERIFY_PMTU: verify PMTU changes
2661 2667 * IXAF_VERIFY_LSO: verify LSO capability changes
2662 2668 */
2663 2669 connp->conn_ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
2664 2670 IXAF_VERIFY_PMTU | IXAF_VERIFY_LSO;
2665 2671
2666 2672 if (!tcps->tcps_dev_flow_ctl)
2667 2673 connp->conn_ixa->ixa_flags |= IXAF_NO_DEV_FLOW_CTL;
2668 2674
2669 2675 if (isv6) {
2670 2676 connp->conn_ixa->ixa_src_preferences = IPV6_PREFER_SRC_DEFAULT;
2671 2677 connp->conn_ipversion = IPV6_VERSION;
2672 2678 connp->conn_family = AF_INET6;
2673 2679 tcp->tcp_mss = tcps->tcps_mss_def_ipv6;
2674 2680 connp->conn_default_ttl = tcps->tcps_ipv6_hoplimit;
2675 2681 } else {
2676 2682 connp->conn_ipversion = IPV4_VERSION;
2677 2683 connp->conn_family = AF_INET;
2678 2684 tcp->tcp_mss = tcps->tcps_mss_def_ipv4;
2679 2685 connp->conn_default_ttl = tcps->tcps_ipv4_ttl;
2680 2686 }
2681 2687 connp->conn_xmit_ipp.ipp_unicast_hops = connp->conn_default_ttl;
2682 2688
2683 2689 crhold(credp);
2684 2690 connp->conn_cred = credp;
2685 2691 connp->conn_cpid = curproc->p_pid;
2686 2692 connp->conn_open_time = ddi_get_lbolt64();
2687 2693
2688 2694 /* Cache things in the ixa without any refhold */
2689 2695 ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
2690 2696 connp->conn_ixa->ixa_cred = credp;
2691 2697 connp->conn_ixa->ixa_cpid = connp->conn_cpid;
2692 2698
2693 2699 connp->conn_zoneid = zoneid;
2694 2700 /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
2695 2701 connp->conn_ixa->ixa_zoneid = zoneid;
2696 2702 connp->conn_mlp_type = mlptSingle;
2697 2703 ASSERT(connp->conn_netstack == tcps->tcps_netstack);
2698 2704 ASSERT(tcp->tcp_tcps == tcps);
2699 2705
2700 2706 /*
2701 2707 * If the caller has the process-wide flag set, then default to MAC
2702 2708 * exempt mode. This allows read-down to unlabeled hosts.
2703 2709 */
2704 2710 if (getpflags(NET_MAC_AWARE, credp) != 0)
2705 2711 connp->conn_mac_mode = CONN_MAC_AWARE;
2706 2712
2707 2713 connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID);
2708 2714
2709 2715 if (issocket) {
2710 2716 tcp->tcp_issocket = 1;
2711 2717 }
2712 2718
2713 2719 connp->conn_rcvbuf = tcps->tcps_recv_hiwat;
2714 2720 connp->conn_sndbuf = tcps->tcps_xmit_hiwat;
2715 2721 connp->conn_sndlowat = tcps->tcps_xmit_lowat;
2716 2722 connp->conn_so_type = SOCK_STREAM;
2717 2723 connp->conn_wroff = connp->conn_ht_iphc_allocated +
2718 2724 tcps->tcps_wroff_xtra;
2719 2725
2720 2726 SOCK_CONNID_INIT(tcp->tcp_connid);
2721 2727 /* DTrace ignores this - it isn't a tcp:::state-change */
2722 2728 tcp->tcp_state = TCPS_IDLE;
2723 2729 tcp_init_values(tcp, NULL);
2724 2730 return (connp);
2725 2731 }
2726 2732
2727 2733 static int
2728 2734 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
2729 2735 boolean_t isv6)
2730 2736 {
2731 2737 tcp_t *tcp = NULL;
2732 2738 conn_t *connp = NULL;
2733 2739 int err;
2734 2740 vmem_t *minor_arena = NULL;
2735 2741 dev_t conn_dev;
2736 2742 boolean_t issocket;
2737 2743
2738 2744 if (q->q_ptr != NULL)
2739 2745 return (0);
2740 2746
2741 2747 if (sflag == MODOPEN)
2742 2748 return (EINVAL);
2743 2749
2744 2750 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
2745 2751 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
2746 2752 minor_arena = ip_minor_arena_la;
2747 2753 } else {
2748 2754 /*
2749 2755 * Either minor numbers in the large arena were exhausted
2750 2756 * or a non socket application is doing the open.
2751 2757 * Try to allocate from the small arena.
2752 2758 */
2753 2759 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) {
2754 2760 return (EBUSY);
2755 2761 }
2756 2762 minor_arena = ip_minor_arena_sa;
2757 2763 }
2758 2764
2759 2765 ASSERT(minor_arena != NULL);
2760 2766
2761 2767 *devp = makedevice(getmajor(*devp), (minor_t)conn_dev);
2762 2768
2763 2769 if (flag & SO_FALLBACK) {
2764 2770 /*
2765 2771 * Non streams socket needs a stream to fallback to
2766 2772 */
2767 2773 RD(q)->q_ptr = (void *)conn_dev;
2768 2774 WR(q)->q_qinfo = &tcp_fallback_sock_winit;
2769 2775 WR(q)->q_ptr = (void *)minor_arena;
2770 2776 qprocson(q);
2771 2777 return (0);
2772 2778 } else if (flag & SO_ACCEPTOR) {
2773 2779 q->q_qinfo = &tcp_acceptor_rinit;
2774 2780 /*
2775 2781 * the conn_dev and minor_arena will be subsequently used by
2776 2782 * tcp_tli_accept() and tcp_tpi_close_accept() to figure out
2777 2783 * the minor device number for this connection from the q_ptr.
2778 2784 */
2779 2785 RD(q)->q_ptr = (void *)conn_dev;
2780 2786 WR(q)->q_qinfo = &tcp_acceptor_winit;
2781 2787 WR(q)->q_ptr = (void *)minor_arena;
2782 2788 qprocson(q);
2783 2789 return (0);
2784 2790 }
2785 2791
2786 2792 issocket = flag & SO_SOCKSTR;
2787 2793 connp = tcp_create_common(credp, isv6, issocket, &err);
2788 2794
2789 2795 if (connp == NULL) {
2790 2796 inet_minor_free(minor_arena, conn_dev);
2791 2797 q->q_ptr = WR(q)->q_ptr = NULL;
2792 2798 return (err);
2793 2799 }
2794 2800
2795 2801 connp->conn_rq = q;
2796 2802 connp->conn_wq = WR(q);
2797 2803 q->q_ptr = WR(q)->q_ptr = connp;
2798 2804
2799 2805 connp->conn_dev = conn_dev;
2800 2806 connp->conn_minor_arena = minor_arena;
2801 2807
2802 2808 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6);
2803 2809 ASSERT(WR(q)->q_qinfo == &tcp_winit);
2804 2810
2805 2811 tcp = connp->conn_tcp;
2806 2812
2807 2813 if (issocket) {
2808 2814 WR(q)->q_qinfo = &tcp_sock_winit;
2809 2815 } else {
2810 2816 #ifdef _ILP32
2811 2817 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q);
2812 2818 #else
2813 2819 tcp->tcp_acceptor_id = conn_dev;
2814 2820 #endif /* _ILP32 */
2815 2821 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp);
2816 2822 }
2817 2823
2818 2824 /*
2819 2825 * Put the ref for TCP. Ref for IP was already put
2820 2826 * by ipcl_conn_create. Also Make the conn_t globally
2821 2827 * visible to walkers
2822 2828 */
2823 2829 mutex_enter(&connp->conn_lock);
2824 2830 CONN_INC_REF_LOCKED(connp);
2825 2831 ASSERT(connp->conn_ref == 2);
2826 2832 connp->conn_state_flags &= ~CONN_INCIPIENT;
2827 2833 mutex_exit(&connp->conn_lock);
2828 2834
2829 2835 qprocson(q);
2830 2836 return (0);
2831 2837 }
2832 2838
2833 2839 /*
2834 2840 * Build/update the tcp header template (in conn_ht_iphc) based on
2835 2841 * conn_xmit_ipp. The headers include ip6_t, any extension
2836 2842 * headers, and the maximum size tcp header (to avoid reallocation
2837 2843 * on the fly for additional tcp options).
2838 2844 *
2839 2845 * Assumes the caller has already set conn_{faddr,laddr,fport,lport,flowinfo}.
2840 2846 * Returns failure if can't allocate memory.
2841 2847 */
2842 2848 int
2843 2849 tcp_build_hdrs(tcp_t *tcp)
2844 2850 {
2845 2851 tcp_stack_t *tcps = tcp->tcp_tcps;
2846 2852 conn_t *connp = tcp->tcp_connp;
2847 2853 char buf[TCP_MAX_HDR_LENGTH];
2848 2854 uint_t buflen;
2849 2855 uint_t ulplen = TCP_MIN_HEADER_LENGTH;
2850 2856 uint_t extralen = TCP_MAX_TCP_OPTIONS_LENGTH;
2851 2857 tcpha_t *tcpha;
2852 2858 uint32_t cksum;
2853 2859 int error;
2854 2860
2855 2861 /*
2856 2862 * We might be called after the connection is set up, and we might
2857 2863 * have TS options already in the TCP header. Thus we save any
2858 2864 * existing tcp header.
2859 2865 */
2860 2866 buflen = connp->conn_ht_ulp_len;
2861 2867 if (buflen != 0) {
2862 2868 bcopy(connp->conn_ht_ulp, buf, buflen);
2863 2869 extralen -= buflen - ulplen;
2864 2870 ulplen = buflen;
2865 2871 }
2866 2872
2867 2873 /* Grab lock to satisfy ASSERT; TCP is serialized using squeue */
2868 2874 mutex_enter(&connp->conn_lock);
2869 2875 error = conn_build_hdr_template(connp, ulplen, extralen,
2870 2876 &connp->conn_laddr_v6, &connp->conn_faddr_v6, connp->conn_flowinfo);
2871 2877 mutex_exit(&connp->conn_lock);
2872 2878 if (error != 0)
2873 2879 return (error);
2874 2880
2875 2881 /*
2876 2882 * Any routing header/option has been massaged. The checksum difference
2877 2883 * is stored in conn_sum for later use.
2878 2884 */
2879 2885 tcpha = (tcpha_t *)connp->conn_ht_ulp;
2880 2886 tcp->tcp_tcpha = tcpha;
2881 2887
2882 2888 /* restore any old tcp header */
2883 2889 if (buflen != 0) {
2884 2890 bcopy(buf, connp->conn_ht_ulp, buflen);
2885 2891 } else {
2886 2892 tcpha->tha_sum = 0;
2887 2893 tcpha->tha_urp = 0;
2888 2894 tcpha->tha_ack = 0;
2889 2895 tcpha->tha_offset_and_reserved = (5 << 4);
2890 2896 tcpha->tha_lport = connp->conn_lport;
2891 2897 tcpha->tha_fport = connp->conn_fport;
2892 2898 }
2893 2899
2894 2900 /*
2895 2901 * IP wants our header length in the checksum field to
2896 2902 * allow it to perform a single pseudo-header+checksum
2897 2903 * calculation on behalf of TCP.
2898 2904 * Include the adjustment for a source route once IP_OPTIONS is set.
2899 2905 */
2900 2906 cksum = sizeof (tcpha_t) + connp->conn_sum;
2901 2907 cksum = (cksum >> 16) + (cksum & 0xFFFF);
2902 2908 ASSERT(cksum < 0x10000);
2903 2909 tcpha->tha_sum = htons(cksum);
2904 2910
2905 2911 if (connp->conn_ipversion == IPV4_VERSION)
2906 2912 tcp->tcp_ipha = (ipha_t *)connp->conn_ht_iphc;
2907 2913 else
2908 2914 tcp->tcp_ip6h = (ip6_t *)connp->conn_ht_iphc;
2909 2915
2910 2916 if (connp->conn_ht_iphc_allocated + tcps->tcps_wroff_xtra >
2911 2917 connp->conn_wroff) {
2912 2918 connp->conn_wroff = connp->conn_ht_iphc_allocated +
2913 2919 tcps->tcps_wroff_xtra;
2914 2920 (void) proto_set_tx_wroff(connp->conn_rq, connp,
2915 2921 connp->conn_wroff);
2916 2922 }
2917 2923 return (0);
2918 2924 }
2919 2925
2920 2926 /*
2921 2927 * tcp_rwnd_set() is called to adjust the receive window to a desired value.
2922 2928 * We do not allow the receive window to shrink. After setting rwnd,
2923 2929 * set the flow control hiwat of the stream.
2924 2930 *
2925 2931 * This function is called in 2 cases:
2926 2932 *
2927 2933 * 1) Before data transfer begins, in tcp_input_listener() for accepting a
2928 2934 * connection (passive open) and in tcp_input_data() for active connect.
2929 2935 * This is called after tcp_mss_set() when the desired MSS value is known.
2930 2936 * This makes sure that our window size is a mutiple of the other side's
2931 2937 * MSS.
2932 2938 * 2) Handling SO_RCVBUF option.
2933 2939 *
2934 2940 * It is ASSUMED that the requested size is a multiple of the current MSS.
2935 2941 *
2936 2942 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the
2937 2943 * user requests so.
2938 2944 */
2939 2945 int
2940 2946 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd)
2941 2947 {
2942 2948 uint32_t mss = tcp->tcp_mss;
2943 2949 uint32_t old_max_rwnd;
2944 2950 uint32_t max_transmittable_rwnd;
2945 2951 boolean_t tcp_detached = TCP_IS_DETACHED(tcp);
2946 2952 tcp_stack_t *tcps = tcp->tcp_tcps;
2947 2953 conn_t *connp = tcp->tcp_connp;
2948 2954
2949 2955 /*
2950 2956 * Insist on a receive window that is at least
2951 2957 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid
2952 2958 * funny TCP interactions of Nagle algorithm, SWS avoidance
2953 2959 * and delayed acknowledgement.
2954 2960 */
2955 2961 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss);
2956 2962
2957 2963 if (tcp->tcp_fused) {
2958 2964 size_t sth_hiwat;
2959 2965 tcp_t *peer_tcp = tcp->tcp_loopback_peer;
2960 2966
2961 2967 ASSERT(peer_tcp != NULL);
2962 2968 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd);
2963 2969 if (!tcp_detached) {
2964 2970 (void) proto_set_rx_hiwat(connp->conn_rq, connp,
2965 2971 sth_hiwat);
2966 2972 tcp_set_recv_threshold(tcp, sth_hiwat >> 3);
2967 2973 }
2968 2974
2969 2975 /* Caller could have changed tcp_rwnd; update tha_win */
2970 2976 if (tcp->tcp_tcpha != NULL) {
2971 2977 tcp->tcp_tcpha->tha_win =
2972 2978 htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
2973 2979 }
2974 2980 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max)
2975 2981 tcp->tcp_cwnd_max = rwnd;
2976 2982
2977 2983 /*
2978 2984 * In the fusion case, the maxpsz stream head value of
2979 2985 * our peer is set according to its send buffer size
2980 2986 * and our receive buffer size; since the latter may
2981 2987 * have changed we need to update the peer's maxpsz.
2982 2988 */
2983 2989 (void) tcp_maxpsz_set(peer_tcp, B_TRUE);
2984 2990 return (sth_hiwat);
2985 2991 }
2986 2992
2987 2993 if (tcp_detached)
2988 2994 old_max_rwnd = tcp->tcp_rwnd;
2989 2995 else
2990 2996 old_max_rwnd = connp->conn_rcvbuf;
2991 2997
2992 2998
2993 2999 /*
2994 3000 * If window size info has already been exchanged, TCP should not
2995 3001 * shrink the window. Shrinking window is doable if done carefully.
2996 3002 * We may add that support later. But so far there is not a real
2997 3003 * need to do that.
2998 3004 */
2999 3005 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) {
3000 3006 /* MSS may have changed, do a round up again. */
3001 3007 rwnd = MSS_ROUNDUP(old_max_rwnd, mss);
3002 3008 }
3003 3009
3004 3010 /*
3005 3011 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check
3006 3012 * can be applied even before the window scale option is decided.
3007 3013 */
3008 3014 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws;
3009 3015 if (rwnd > max_transmittable_rwnd) {
3010 3016 rwnd = max_transmittable_rwnd -
3011 3017 (max_transmittable_rwnd % mss);
3012 3018 if (rwnd < mss)
3013 3019 rwnd = max_transmittable_rwnd;
3014 3020 /*
3015 3021 * If we're over the limit we may have to back down tcp_rwnd.
3016 3022 * The increment below won't work for us. So we set all three
3017 3023 * here and the increment below will have no effect.
3018 3024 */
3019 3025 tcp->tcp_rwnd = old_max_rwnd = rwnd;
3020 3026 }
3021 3027 if (tcp->tcp_localnet) {
3022 3028 tcp->tcp_rack_abs_max =
3023 3029 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2);
3024 3030 } else {
3025 3031 /*
3026 3032 * For a remote host on a different subnet (through a router),
3027 3033 * we ack every other packet to be conforming to RFC1122.
3028 3034 * tcp_deferred_acks_max is default to 2.
3029 3035 */
3030 3036 tcp->tcp_rack_abs_max =
3031 3037 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2);
3032 3038 }
3033 3039 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max)
3034 3040 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max;
3035 3041 else
3036 3042 tcp->tcp_rack_cur_max = 0;
3037 3043 /*
3038 3044 * Increment the current rwnd by the amount the maximum grew (we
3039 3045 * can not overwrite it since we might be in the middle of a
3040 3046 * connection.)
3041 3047 */
3042 3048 tcp->tcp_rwnd += rwnd - old_max_rwnd;
3043 3049 connp->conn_rcvbuf = rwnd;
3044 3050
3045 3051 /* Are we already connected? */
3046 3052 if (tcp->tcp_tcpha != NULL) {
3047 3053 tcp->tcp_tcpha->tha_win =
3048 3054 htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
3049 3055 }
3050 3056
3051 3057 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max)
3052 3058 tcp->tcp_cwnd_max = rwnd;
3053 3059
3054 3060 if (tcp_detached)
3055 3061 return (rwnd);
3056 3062
3057 3063 tcp_set_recv_threshold(tcp, rwnd >> 3);
3058 3064
3059 3065 (void) proto_set_rx_hiwat(connp->conn_rq, connp, rwnd);
3060 3066 return (rwnd);
3061 3067 }
3062 3068
3063 3069 int
3064 3070 tcp_do_unbind(conn_t *connp)
3065 3071 {
3066 3072 tcp_t *tcp = connp->conn_tcp;
3067 3073 int32_t oldstate;
3068 3074
3069 3075 switch (tcp->tcp_state) {
3070 3076 case TCPS_BOUND:
3071 3077 case TCPS_LISTEN:
3072 3078 break;
3073 3079 default:
3074 3080 return (-TOUTSTATE);
3075 3081 }
3076 3082
3077 3083 /*
3078 3084 * Need to clean up all the eagers since after the unbind, segments
3079 3085 * will no longer be delivered to this listener stream.
3080 3086 */
3081 3087 mutex_enter(&tcp->tcp_eager_lock);
3082 3088 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) {
3083 3089 tcp_eager_cleanup(tcp, 0);
3084 3090 }
3085 3091 mutex_exit(&tcp->tcp_eager_lock);
3086 3092
3087 3093 /* Clean up the listener connection counter if necessary. */
3088 3094 if (tcp->tcp_listen_cnt != NULL)
3089 3095 TCP_DECR_LISTEN_CNT(tcp);
3090 3096 connp->conn_laddr_v6 = ipv6_all_zeros;
3091 3097 connp->conn_saddr_v6 = ipv6_all_zeros;
3092 3098 tcp_bind_hash_remove(tcp);
3093 3099 oldstate = tcp->tcp_state;
3094 3100 tcp->tcp_state = TCPS_IDLE;
3095 3101 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
3096 3102 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
3097 3103 int32_t, oldstate);
3098 3104
3099 3105 ip_unbind(connp);
3100 3106 bzero(&connp->conn_ports, sizeof (connp->conn_ports));
3101 3107
3102 3108 return (0);
3103 3109 }
3104 3110
3105 3111 /*
3106 3112 * Collect protocol properties to send to the upper handle.
3107 3113 */
3108 3114 void
3109 3115 tcp_get_proto_props(tcp_t *tcp, struct sock_proto_props *sopp)
3110 3116 {
3111 3117 conn_t *connp = tcp->tcp_connp;
3112 3118
3113 3119 sopp->sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF;
3114 3120 sopp->sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE);
3115 3121
3116 3122 sopp->sopp_rxhiwat = tcp->tcp_fused ?
3117 3123 tcp_fuse_set_rcv_hiwat(tcp, connp->conn_rcvbuf) :
3118 3124 connp->conn_rcvbuf;
3119 3125 /*
3120 3126 * Determine what write offset value to use depending on SACK and
3121 3127 * whether the endpoint is fused or not.
3122 3128 */
3123 3129 if (tcp->tcp_fused) {
3124 3130 ASSERT(tcp->tcp_loopback);
3125 3131 ASSERT(tcp->tcp_loopback_peer != NULL);
3126 3132 /*
3127 3133 * For fused tcp loopback, set the stream head's write
3128 3134 * offset value to zero since we won't be needing any room
3129 3135 * for TCP/IP headers. This would also improve performance
3130 3136 * since it would reduce the amount of work done by kmem.
3131 3137 * Non-fused tcp loopback case is handled separately below.
3132 3138 */
3133 3139 sopp->sopp_wroff = 0;
3134 3140 /*
3135 3141 * Update the peer's transmit parameters according to
3136 3142 * our recently calculated high water mark value.
3137 3143 */
3138 3144 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE);
3139 3145 } else if (tcp->tcp_snd_sack_ok) {
3140 3146 sopp->sopp_wroff = connp->conn_ht_iphc_allocated +
3141 3147 (tcp->tcp_loopback ? 0 : tcp->tcp_tcps->tcps_wroff_xtra);
3142 3148 } else {
3143 3149 sopp->sopp_wroff = connp->conn_ht_iphc_len +
3144 3150 (tcp->tcp_loopback ? 0 : tcp->tcp_tcps->tcps_wroff_xtra);
3145 3151 }
3146 3152
3147 3153 if (tcp->tcp_loopback) {
3148 3154 sopp->sopp_flags |= SOCKOPT_LOOPBACK;
3149 3155 sopp->sopp_loopback = B_TRUE;
3150 3156 }
3151 3157 }
3152 3158
3153 3159 /*
3154 3160 * Check the usability of ZEROCOPY. It's instead checking the flag set by IP.
3155 3161 */
3156 3162 boolean_t
3157 3163 tcp_zcopy_check(tcp_t *tcp)
3158 3164 {
3159 3165 conn_t *connp = tcp->tcp_connp;
3160 3166 ip_xmit_attr_t *ixa = connp->conn_ixa;
3161 3167 boolean_t zc_enabled = B_FALSE;
3162 3168 tcp_stack_t *tcps = tcp->tcp_tcps;
3163 3169
3164 3170 if (do_tcpzcopy == 2)
3165 3171 zc_enabled = B_TRUE;
3166 3172 else if ((do_tcpzcopy == 1) && (ixa->ixa_flags & IXAF_ZCOPY_CAPAB))
3167 3173 zc_enabled = B_TRUE;
3168 3174
3169 3175 tcp->tcp_snd_zcopy_on = zc_enabled;
3170 3176 if (!TCP_IS_DETACHED(tcp)) {
3171 3177 if (zc_enabled) {
3172 3178 ixa->ixa_flags |= IXAF_VERIFY_ZCOPY;
3173 3179 (void) proto_set_tx_copyopt(connp->conn_rq, connp,
3174 3180 ZCVMSAFE);
3175 3181 TCP_STAT(tcps, tcp_zcopy_on);
3176 3182 } else {
3177 3183 ixa->ixa_flags &= ~IXAF_VERIFY_ZCOPY;
3178 3184 (void) proto_set_tx_copyopt(connp->conn_rq, connp,
3179 3185 ZCVMUNSAFE);
3180 3186 TCP_STAT(tcps, tcp_zcopy_off);
3181 3187 }
3182 3188 }
3183 3189 return (zc_enabled);
3184 3190 }
3185 3191
3186 3192 /*
3187 3193 * Backoff from a zero-copy message by copying data to a new allocated
3188 3194 * message and freeing the original desballoca'ed segmapped message.
3189 3195 *
3190 3196 * This function is called by following two callers:
3191 3197 * 1. tcp_timer: fix_xmitlist is set to B_TRUE, because it's safe to free
3192 3198 * the origial desballoca'ed message and notify sockfs. This is in re-
3193 3199 * transmit state.
3194 3200 * 2. tcp_output: fix_xmitlist is set to B_FALSE. Flag STRUIO_ZCNOTIFY need
3195 3201 * to be copied to new message.
3196 3202 */
3197 3203 mblk_t *
3198 3204 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, boolean_t fix_xmitlist)
3199 3205 {
3200 3206 mblk_t *nbp;
3201 3207 mblk_t *head = NULL;
3202 3208 mblk_t *tail = NULL;
3203 3209 tcp_stack_t *tcps = tcp->tcp_tcps;
3204 3210
3205 3211 ASSERT(bp != NULL);
3206 3212 while (bp != NULL) {
3207 3213 if (IS_VMLOANED_MBLK(bp)) {
3208 3214 TCP_STAT(tcps, tcp_zcopy_backoff);
3209 3215 if ((nbp = copyb(bp)) == NULL) {
3210 3216 tcp->tcp_xmit_zc_clean = B_FALSE;
3211 3217 if (tail != NULL)
3212 3218 tail->b_cont = bp;
3213 3219 return ((head == NULL) ? bp : head);
3214 3220 }
3215 3221
3216 3222 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) {
3217 3223 if (fix_xmitlist)
3218 3224 tcp_zcopy_notify(tcp);
3219 3225 else
3220 3226 nbp->b_datap->db_struioflag |=
3221 3227 STRUIO_ZCNOTIFY;
3222 3228 }
3223 3229 nbp->b_cont = bp->b_cont;
3224 3230
3225 3231 /*
3226 3232 * Copy saved information and adjust tcp_xmit_tail
3227 3233 * if needed.
3228 3234 */
3229 3235 if (fix_xmitlist) {
3230 3236 nbp->b_prev = bp->b_prev;
3231 3237 nbp->b_next = bp->b_next;
3232 3238
3233 3239 if (tcp->tcp_xmit_tail == bp)
3234 3240 tcp->tcp_xmit_tail = nbp;
3235 3241 }
3236 3242
3237 3243 /* Free the original message. */
3238 3244 bp->b_prev = NULL;
3239 3245 bp->b_next = NULL;
3240 3246 freeb(bp);
3241 3247
3242 3248 bp = nbp;
3243 3249 }
3244 3250
3245 3251 if (head == NULL) {
3246 3252 head = bp;
3247 3253 }
3248 3254 if (tail == NULL) {
3249 3255 tail = bp;
3250 3256 } else {
3251 3257 tail->b_cont = bp;
3252 3258 tail = bp;
3253 3259 }
3254 3260
3255 3261 /* Move forward. */
3256 3262 bp = bp->b_cont;
3257 3263 }
3258 3264
3259 3265 if (fix_xmitlist) {
3260 3266 tcp->tcp_xmit_last = tail;
3261 3267 tcp->tcp_xmit_zc_clean = B_TRUE;
3262 3268 }
3263 3269
3264 3270 return (head);
3265 3271 }
3266 3272
3267 3273 void
3268 3274 tcp_zcopy_notify(tcp_t *tcp)
3269 3275 {
3270 3276 struct stdata *stp;
3271 3277 conn_t *connp;
3272 3278
3273 3279 if (tcp->tcp_detached)
3274 3280 return;
3275 3281 connp = tcp->tcp_connp;
3276 3282 if (IPCL_IS_NONSTR(connp)) {
3277 3283 (*connp->conn_upcalls->su_zcopy_notify)
3278 3284 (connp->conn_upper_handle);
3279 3285 return;
3280 3286 }
3281 3287 stp = STREAM(connp->conn_rq);
3282 3288 mutex_enter(&stp->sd_lock);
3283 3289 stp->sd_flag |= STZCNOTIFY;
3284 3290 cv_broadcast(&stp->sd_zcopy_wait);
3285 3291 mutex_exit(&stp->sd_lock);
3286 3292 }
3287 3293
3288 3294 /*
3289 3295 * Update the TCP connection according to change of LSO capability.
3290 3296 */
3291 3297 static void
3292 3298 tcp_update_lso(tcp_t *tcp, ip_xmit_attr_t *ixa)
3293 3299 {
3294 3300 /*
3295 3301 * We check against IPv4 header length to preserve the old behavior
3296 3302 * of only enabling LSO when there are no IP options.
3297 3303 * But this restriction might not be necessary at all. Before removing
3298 3304 * it, need to verify how LSO is handled for source routing case, with
3299 3305 * which IP does software checksum.
3300 3306 *
3301 3307 * For IPv6, whenever any extension header is needed, LSO is supressed.
3302 3308 */
3303 3309 if (ixa->ixa_ip_hdr_length != ((ixa->ixa_flags & IXAF_IS_IPV4) ?
3304 3310 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN))
3305 3311 return;
3306 3312
3307 3313 /*
3308 3314 * Either the LSO capability newly became usable, or it has changed.
3309 3315 */
3310 3316 if (ixa->ixa_flags & IXAF_LSO_CAPAB) {
3311 3317 ill_lso_capab_t *lsoc = &ixa->ixa_lso_capab;
3312 3318
3313 3319 ASSERT(lsoc->ill_lso_max > 0);
3314 3320 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, lsoc->ill_lso_max);
3315 3321
3316 3322 DTRACE_PROBE3(tcp_update_lso, boolean_t, tcp->tcp_lso,
3317 3323 boolean_t, B_TRUE, uint32_t, tcp->tcp_lso_max);
3318 3324
3319 3325 /*
3320 3326 * If LSO to be enabled, notify the STREAM header with larger
3321 3327 * data block.
3322 3328 */
3323 3329 if (!tcp->tcp_lso)
3324 3330 tcp->tcp_maxpsz_multiplier = 0;
3325 3331
3326 3332 tcp->tcp_lso = B_TRUE;
3327 3333 TCP_STAT(tcp->tcp_tcps, tcp_lso_enabled);
3328 3334 } else { /* LSO capability is not usable any more. */
3329 3335 DTRACE_PROBE3(tcp_update_lso, boolean_t, tcp->tcp_lso,
3330 3336 boolean_t, B_FALSE, uint32_t, tcp->tcp_lso_max);
3331 3337
3332 3338 /*
3333 3339 * If LSO to be disabled, notify the STREAM header with smaller
3334 3340 * data block. And need to restore fragsize to PMTU.
3335 3341 */
3336 3342 if (tcp->tcp_lso) {
3337 3343 tcp->tcp_maxpsz_multiplier =
3338 3344 tcp->tcp_tcps->tcps_maxpsz_multiplier;
3339 3345 ixa->ixa_fragsize = ixa->ixa_pmtu;
3340 3346 tcp->tcp_lso = B_FALSE;
3341 3347 TCP_STAT(tcp->tcp_tcps, tcp_lso_disabled);
3342 3348 }
3343 3349 }
3344 3350
3345 3351 (void) tcp_maxpsz_set(tcp, B_TRUE);
3346 3352 }
3347 3353
3348 3354 /*
3349 3355 * Update the TCP connection according to change of ZEROCOPY capability.
3350 3356 */
3351 3357 static void
3352 3358 tcp_update_zcopy(tcp_t *tcp)
3353 3359 {
3354 3360 conn_t *connp = tcp->tcp_connp;
3355 3361 tcp_stack_t *tcps = tcp->tcp_tcps;
3356 3362
3357 3363 if (tcp->tcp_snd_zcopy_on) {
3358 3364 tcp->tcp_snd_zcopy_on = B_FALSE;
3359 3365 if (!TCP_IS_DETACHED(tcp)) {
3360 3366 (void) proto_set_tx_copyopt(connp->conn_rq, connp,
3361 3367 ZCVMUNSAFE);
3362 3368 TCP_STAT(tcps, tcp_zcopy_off);
3363 3369 }
3364 3370 } else {
3365 3371 tcp->tcp_snd_zcopy_on = B_TRUE;
3366 3372 if (!TCP_IS_DETACHED(tcp)) {
3367 3373 (void) proto_set_tx_copyopt(connp->conn_rq, connp,
3368 3374 ZCVMSAFE);
3369 3375 TCP_STAT(tcps, tcp_zcopy_on);
3370 3376 }
3371 3377 }
3372 3378 }
3373 3379
3374 3380 /*
3375 3381 * Notify function registered with ip_xmit_attr_t. It's called in the squeue
3376 3382 * so it's safe to update the TCP connection.
3377 3383 */
3378 3384 /* ARGSUSED1 */
3379 3385 static void
3380 3386 tcp_notify(void *arg, ip_xmit_attr_t *ixa, ixa_notify_type_t ntype,
3381 3387 ixa_notify_arg_t narg)
3382 3388 {
3383 3389 tcp_t *tcp = (tcp_t *)arg;
3384 3390 conn_t *connp = tcp->tcp_connp;
3385 3391
3386 3392 switch (ntype) {
3387 3393 case IXAN_LSO:
3388 3394 tcp_update_lso(tcp, connp->conn_ixa);
3389 3395 break;
3390 3396 case IXAN_PMTU:
3391 3397 tcp_update_pmtu(tcp, B_FALSE);
3392 3398 break;
3393 3399 case IXAN_ZCOPY:
3394 3400 tcp_update_zcopy(tcp);
3395 3401 break;
3396 3402 default:
3397 3403 break;
3398 3404 }
3399 3405 }
3400 3406
3401 3407 /*
3402 3408 * The TCP write service routine should never be called...
3403 3409 */
3404 3410 /* ARGSUSED */
3405 3411 static void
3406 3412 tcp_wsrv(queue_t *q)
3407 3413 {
3408 3414 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps;
3409 3415
3410 3416 TCP_STAT(tcps, tcp_wsrv_called);
3411 3417 }
3412 3418
3413 3419 /*
3414 3420 * Hash list lookup routine for tcp_t structures.
3415 3421 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF.
3416 3422 */
3417 3423 tcp_t *
3418 3424 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps)
3419 3425 {
3420 3426 tf_t *tf;
3421 3427 tcp_t *tcp;
3422 3428
3423 3429 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)];
3424 3430 mutex_enter(&tf->tf_lock);
3425 3431 for (tcp = tf->tf_tcp; tcp != NULL;
3426 3432 tcp = tcp->tcp_acceptor_hash) {
3427 3433 if (tcp->tcp_acceptor_id == id) {
3428 3434 CONN_INC_REF(tcp->tcp_connp);
3429 3435 mutex_exit(&tf->tf_lock);
3430 3436 return (tcp);
3431 3437 }
3432 3438 }
3433 3439 mutex_exit(&tf->tf_lock);
3434 3440 return (NULL);
3435 3441 }
3436 3442
3437 3443 /*
3438 3444 * Hash list insertion routine for tcp_t structures.
3439 3445 */
3440 3446 void
3441 3447 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp)
3442 3448 {
3443 3449 tf_t *tf;
3444 3450 tcp_t **tcpp;
3445 3451 tcp_t *tcpnext;
3446 3452 tcp_stack_t *tcps = tcp->tcp_tcps;
3447 3453
3448 3454 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)];
3449 3455
3450 3456 if (tcp->tcp_ptpahn != NULL)
3451 3457 tcp_acceptor_hash_remove(tcp);
3452 3458 tcpp = &tf->tf_tcp;
3453 3459 mutex_enter(&tf->tf_lock);
3454 3460 tcpnext = tcpp[0];
3455 3461 if (tcpnext)
3456 3462 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash;
3457 3463 tcp->tcp_acceptor_hash = tcpnext;
3458 3464 tcp->tcp_ptpahn = tcpp;
3459 3465 tcpp[0] = tcp;
3460 3466 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */
3461 3467 mutex_exit(&tf->tf_lock);
3462 3468 }
3463 3469
3464 3470 /*
3465 3471 * Hash list removal routine for tcp_t structures.
3466 3472 */
3467 3473 void
3468 3474 tcp_acceptor_hash_remove(tcp_t *tcp)
3469 3475 {
3470 3476 tcp_t *tcpnext;
3471 3477 kmutex_t *lockp;
3472 3478
3473 3479 /*
3474 3480 * Extract the lock pointer in case there are concurrent
3475 3481 * hash_remove's for this instance.
3476 3482 */
3477 3483 lockp = tcp->tcp_acceptor_lockp;
3478 3484
3479 3485 if (tcp->tcp_ptpahn == NULL)
3480 3486 return;
3481 3487
3482 3488 ASSERT(lockp != NULL);
3483 3489 mutex_enter(lockp);
3484 3490 if (tcp->tcp_ptpahn) {
3485 3491 tcpnext = tcp->tcp_acceptor_hash;
3486 3492 if (tcpnext) {
3487 3493 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn;
3488 3494 tcp->tcp_acceptor_hash = NULL;
3489 3495 }
3490 3496 *tcp->tcp_ptpahn = tcpnext;
3491 3497 tcp->tcp_ptpahn = NULL;
3492 3498 }
3493 3499 mutex_exit(lockp);
3494 3500 tcp->tcp_acceptor_lockp = NULL;
3495 3501 }
3496 3502
3497 3503 /*
3498 3504 * Type three generator adapted from the random() function in 4.4 BSD:
3499 3505 */
3500 3506
3501 3507 /*
3502 3508 * Copyright (c) 1983, 1993
3503 3509 * The Regents of the University of California. All rights reserved.
3504 3510 *
3505 3511 * Redistribution and use in source and binary forms, with or without
3506 3512 * modification, are permitted provided that the following conditions
3507 3513 * are met:
3508 3514 * 1. Redistributions of source code must retain the above copyright
3509 3515 * notice, this list of conditions and the following disclaimer.
3510 3516 * 2. Redistributions in binary form must reproduce the above copyright
3511 3517 * notice, this list of conditions and the following disclaimer in the
3512 3518 * documentation and/or other materials provided with the distribution.
3513 3519 * 3. All advertising materials mentioning features or use of this software
3514 3520 * must display the following acknowledgement:
3515 3521 * This product includes software developed by the University of
3516 3522 * California, Berkeley and its contributors.
3517 3523 * 4. Neither the name of the University nor the names of its contributors
3518 3524 * may be used to endorse or promote products derived from this software
3519 3525 * without specific prior written permission.
3520 3526 *
3521 3527 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3522 3528 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3523 3529 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3524 3530 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3525 3531 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3526 3532 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3527 3533 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3528 3534 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3529 3535 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3530 3536 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3531 3537 * SUCH DAMAGE.
3532 3538 */
3533 3539
3534 3540 /* Type 3 -- x**31 + x**3 + 1 */
3535 3541 #define DEG_3 31
3536 3542 #define SEP_3 3
3537 3543
3538 3544
3539 3545 /* Protected by tcp_random_lock */
3540 3546 static int tcp_randtbl[DEG_3 + 1];
3541 3547
3542 3548 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1];
3543 3549 static int *tcp_random_rptr = &tcp_randtbl[1];
3544 3550
3545 3551 static int *tcp_random_state = &tcp_randtbl[1];
3546 3552 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1];
3547 3553
3548 3554 kmutex_t tcp_random_lock;
3549 3555
3550 3556 void
3551 3557 tcp_random_init(void)
3552 3558 {
3553 3559 int i;
3554 3560 hrtime_t hrt;
3555 3561 time_t wallclock;
3556 3562 uint64_t result;
3557 3563
3558 3564 /*
3559 3565 * Use high-res timer and current time for seed. Gethrtime() returns
3560 3566 * a longlong, which may contain resolution down to nanoseconds.
3561 3567 * The current time will either be a 32-bit or a 64-bit quantity.
3562 3568 * XOR the two together in a 64-bit result variable.
3563 3569 * Convert the result to a 32-bit value by multiplying the high-order
3564 3570 * 32-bits by the low-order 32-bits.
3565 3571 */
3566 3572
3567 3573 hrt = gethrtime();
3568 3574 (void) drv_getparm(TIME, &wallclock);
3569 3575 result = (uint64_t)wallclock ^ (uint64_t)hrt;
3570 3576 mutex_enter(&tcp_random_lock);
3571 3577 tcp_random_state[0] = ((result >> 32) & 0xffffffff) *
3572 3578 (result & 0xffffffff);
3573 3579
3574 3580 for (i = 1; i < DEG_3; i++)
3575 3581 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1]
3576 3582 + 12345;
3577 3583 tcp_random_fptr = &tcp_random_state[SEP_3];
3578 3584 tcp_random_rptr = &tcp_random_state[0];
3579 3585 mutex_exit(&tcp_random_lock);
3580 3586 for (i = 0; i < 10 * DEG_3; i++)
3581 3587 (void) tcp_random();
3582 3588 }
3583 3589
3584 3590 /*
3585 3591 * tcp_random: Return a random number in the range [1 - (128K + 1)].
3586 3592 * This range is selected to be approximately centered on TCP_ISS / 2,
3587 3593 * and easy to compute. We get this value by generating a 32-bit random
3588 3594 * number, selecting out the high-order 17 bits, and then adding one so
3589 3595 * that we never return zero.
3590 3596 */
3591 3597 int
3592 3598 tcp_random(void)
3593 3599 {
3594 3600 int i;
3595 3601
3596 3602 mutex_enter(&tcp_random_lock);
3597 3603 *tcp_random_fptr += *tcp_random_rptr;
3598 3604
3599 3605 /*
3600 3606 * The high-order bits are more random than the low-order bits,
3601 3607 * so we select out the high-order 17 bits and add one so that
3602 3608 * we never return zero.
3603 3609 */
3604 3610 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1;
3605 3611 if (++tcp_random_fptr >= tcp_random_end_ptr) {
3606 3612 tcp_random_fptr = tcp_random_state;
3607 3613 ++tcp_random_rptr;
3608 3614 } else if (++tcp_random_rptr >= tcp_random_end_ptr)
3609 3615 tcp_random_rptr = tcp_random_state;
3610 3616
3611 3617 mutex_exit(&tcp_random_lock);
3612 3618 return (i);
3613 3619 }
3614 3620
3615 3621 /*
3616 3622 * Split this function out so that if the secret changes, I'm okay.
3617 3623 *
3618 3624 * Initialize the tcp_iss_cookie and tcp_iss_key.
3619 3625 */
3620 3626
3621 3627 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */
3622 3628
3623 3629 void
3624 3630 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps)
3625 3631 {
3626 3632 struct {
3627 3633 int32_t current_time;
3628 3634 uint32_t randnum;
3629 3635 uint16_t pad;
3630 3636 uint8_t ether[6];
3631 3637 uint8_t passwd[PASSWD_SIZE];
3632 3638 } tcp_iss_cookie;
3633 3639 time_t t;
3634 3640
3635 3641 /*
3636 3642 * Start with the current absolute time.
3637 3643 */
3638 3644 (void) drv_getparm(TIME, &t);
3639 3645 tcp_iss_cookie.current_time = t;
3640 3646
3641 3647 /*
3642 3648 * XXX - Need a more random number per RFC 1750, not this crap.
3643 3649 * OTOH, if what follows is pretty random, then I'm in better shape.
3644 3650 */
3645 3651 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random());
3646 3652 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */
3647 3653
3648 3654 /*
3649 3655 * The cpu_type_info is pretty non-random. Ugggh. It does serve
3650 3656 * as a good template.
3651 3657 */
3652 3658 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd,
3653 3659 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info)));
3654 3660
3655 3661 /*
3656 3662 * The pass-phrase. Normally this is supplied by user-called NDD.
3657 3663 */
3658 3664 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len));
3659 3665
3660 3666 /*
3661 3667 * See 4010593 if this section becomes a problem again,
3662 3668 * but the local ethernet address is useful here.
3663 3669 */
3664 3670 (void) localetheraddr(NULL,
3665 3671 (struct ether_addr *)&tcp_iss_cookie.ether);
3666 3672
3667 3673 /*
3668 3674 * Hash 'em all together. The MD5Final is called per-connection.
3669 3675 */
3670 3676 mutex_enter(&tcps->tcps_iss_key_lock);
3671 3677 MD5Init(&tcps->tcps_iss_key);
3672 3678 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie,
3673 3679 sizeof (tcp_iss_cookie));
3674 3680 mutex_exit(&tcps->tcps_iss_key_lock);
3675 3681 }
3676 3682
3677 3683 /*
3678 3684 * Called by IP when IP is loaded into the kernel
3679 3685 */
3680 3686 void
3681 3687 tcp_ddi_g_init(void)
3682 3688 {
3683 3689 tcp_timercache = kmem_cache_create("tcp_timercache",
3684 3690 sizeof (tcp_timer_t) + sizeof (mblk_t), 0,
3685 3691 NULL, NULL, NULL, NULL, NULL, 0);
3686 3692
3687 3693 tcp_notsack_blk_cache = kmem_cache_create("tcp_notsack_blk_cache",
3688 3694 sizeof (notsack_blk_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3689 3695
3690 3696 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL);
3691 3697
3692 3698 /* Initialize the random number generator */
3693 3699 tcp_random_init();
3694 3700
3695 3701 /* A single callback independently of how many netstacks we have */
3696 3702 ip_squeue_init(tcp_squeue_add);
3697 3703
3698 3704 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics);
3699 3705
3700 3706 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput);
3701 3707
3702 3708 /*
3703 3709 * We want to be informed each time a stack is created or
3704 3710 * destroyed in the kernel, so we can maintain the
3705 3711 * set of tcp_stack_t's.
3706 3712 */
3707 3713 netstack_register(NS_TCP, tcp_stack_init, NULL, tcp_stack_fini);
3708 3714 }
3709 3715
3710 3716
3711 3717 #define INET_NAME "ip"
3712 3718
3713 3719 /*
3714 3720 * Initialize the TCP stack instance.
3715 3721 */
3716 3722 static void *
3717 3723 tcp_stack_init(netstackid_t stackid, netstack_t *ns)
3718 3724 {
3719 3725 tcp_stack_t *tcps;
3720 3726 int i;
3721 3727 int error = 0;
3722 3728 major_t major;
3723 3729 size_t arrsz;
3724 3730
3725 3731 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP);
3726 3732 tcps->tcps_netstack = ns;
3727 3733
3728 3734 /* Initialize locks */
3729 3735 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL);
3730 3736 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
3731 3737
3732 3738 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS;
3733 3739 tcps->tcps_g_epriv_ports[0] = ULP_DEF_EPRIV_PORT1;
3734 3740 tcps->tcps_g_epriv_ports[1] = ULP_DEF_EPRIV_PORT2;
3735 3741 tcps->tcps_min_anonpriv_port = 512;
3736 3742
3737 3743 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) *
3738 3744 TCP_BIND_FANOUT_SIZE, KM_SLEEP);
3739 3745 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) *
3740 3746 TCP_ACCEPTOR_FANOUT_SIZE, KM_SLEEP);
3741 3747
3742 3748 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) {
3743 3749 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL,
3744 3750 MUTEX_DEFAULT, NULL);
3745 3751 }
3746 3752
3747 3753 for (i = 0; i < TCP_ACCEPTOR_FANOUT_SIZE; i++) {
3748 3754 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL,
3749 3755 MUTEX_DEFAULT, NULL);
3750 3756 }
3751 3757
3752 3758 /* TCP's IPsec code calls the packet dropper. */
3753 3759 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement");
3754 3760
3755 3761 arrsz = tcp_propinfo_count * sizeof (mod_prop_info_t);
3756 3762 tcps->tcps_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz,
3757 3763 KM_SLEEP);
3758 3764 bcopy(tcp_propinfo_tbl, tcps->tcps_propinfo_tbl, arrsz);
3759 3765
3760 3766 /*
3761 3767 * Note: To really walk the device tree you need the devinfo
3762 3768 * pointer to your device which is only available after probe/attach.
3763 3769 * The following is safe only because it uses ddi_root_node()
3764 3770 */
3765 3771 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr,
3766 3772 tcp_opt_obj.odb_opt_arr_cnt);
3767 3773
3768 3774 /*
3769 3775 * Initialize RFC 1948 secret values. This will probably be reset once
3770 3776 * by the boot scripts.
3771 3777 *
3772 3778 * Use NULL name, as the name is caught by the new lockstats.
3773 3779 *
3774 3780 * Initialize with some random, non-guessable string, like the global
3775 3781 * T_INFO_ACK.
3776 3782 */
3777 3783
3778 3784 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack,
3779 3785 sizeof (tcp_g_t_info_ack), tcps);
3780 3786
3781 3787 tcps->tcps_kstat = tcp_kstat2_init(stackid);
3782 3788 tcps->tcps_mibkp = tcp_kstat_init(stackid);
3783 3789
3784 3790 major = mod_name_to_major(INET_NAME);
3785 3791 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident);
3786 3792 ASSERT(error == 0);
3787 3793 tcps->tcps_ixa_cleanup_mp = allocb_wait(0, BPRI_MED, STR_NOSIG, NULL);
3788 3794 ASSERT(tcps->tcps_ixa_cleanup_mp != NULL);
3789 3795 cv_init(&tcps->tcps_ixa_cleanup_cv, NULL, CV_DEFAULT, NULL);
3790 3796 mutex_init(&tcps->tcps_ixa_cleanup_lock, NULL, MUTEX_DEFAULT, NULL);
3791 3797
3792 3798 mutex_init(&tcps->tcps_reclaim_lock, NULL, MUTEX_DEFAULT, NULL);
3793 3799 tcps->tcps_reclaim = B_FALSE;
3794 3800 tcps->tcps_reclaim_tid = 0;
3795 3801 tcps->tcps_reclaim_period = tcps->tcps_rexmit_interval_max;
3796 3802
3797 3803 /*
3798 3804 * ncpus is the current number of CPUs, which can be bigger than
3799 3805 * boot_ncpus. But we don't want to use ncpus to allocate all the
3800 3806 * tcp_stats_cpu_t at system boot up time since it will be 1. While
3801 3807 * we handle adding CPU in tcp_cpu_update(), it will be slow if
3802 3808 * there are many CPUs as we will be adding them 1 by 1.
3803 3809 *
3804 3810 * Note that tcps_sc_cnt never decreases and the tcps_sc[x] pointers
3805 3811 * are not freed until the stack is going away. So there is no need
3806 3812 * to grab a lock to access the per CPU tcps_sc[x] pointer.
3807 3813 */
3808 3814 mutex_enter(&cpu_lock);
3809 3815 tcps->tcps_sc_cnt = MAX(ncpus, boot_ncpus);
3810 3816 mutex_exit(&cpu_lock);
3811 3817 tcps->tcps_sc = kmem_zalloc(max_ncpus * sizeof (tcp_stats_cpu_t *),
3812 3818 KM_SLEEP);
3813 3819 for (i = 0; i < tcps->tcps_sc_cnt; i++) {
3814 3820 tcps->tcps_sc[i] = kmem_zalloc(sizeof (tcp_stats_cpu_t),
3815 3821 KM_SLEEP);
3816 3822 }
3817 3823
3818 3824 mutex_init(&tcps->tcps_listener_conf_lock, NULL, MUTEX_DEFAULT, NULL);
3819 3825 list_create(&tcps->tcps_listener_conf, sizeof (tcp_listener_t),
3820 3826 offsetof(tcp_listener_t, tl_link));
3821 3827
3822 3828 return (tcps);
3823 3829 }
3824 3830
3825 3831 /*
3826 3832 * Called when the IP module is about to be unloaded.
3827 3833 */
3828 3834 void
3829 3835 tcp_ddi_g_destroy(void)
3830 3836 {
3831 3837 tcp_g_kstat_fini(tcp_g_kstat);
3832 3838 tcp_g_kstat = NULL;
3833 3839 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics));
3834 3840
3835 3841 mutex_destroy(&tcp_random_lock);
3836 3842
3837 3843 kmem_cache_destroy(tcp_timercache);
3838 3844 kmem_cache_destroy(tcp_notsack_blk_cache);
3839 3845
3840 3846 netstack_unregister(NS_TCP);
3841 3847 }
3842 3848
3843 3849 /*
3844 3850 * Free the TCP stack instance.
3845 3851 */
3846 3852 static void
3847 3853 tcp_stack_fini(netstackid_t stackid, void *arg)
3848 3854 {
3849 3855 tcp_stack_t *tcps = (tcp_stack_t *)arg;
3850 3856 int i;
3851 3857
3852 3858 freeb(tcps->tcps_ixa_cleanup_mp);
3853 3859 tcps->tcps_ixa_cleanup_mp = NULL;
3854 3860 cv_destroy(&tcps->tcps_ixa_cleanup_cv);
3855 3861 mutex_destroy(&tcps->tcps_ixa_cleanup_lock);
3856 3862
3857 3863 /*
3858 3864 * Set tcps_reclaim to false tells tcp_reclaim_timer() not to restart
3859 3865 * the timer.
3860 3866 */
3861 3867 mutex_enter(&tcps->tcps_reclaim_lock);
3862 3868 tcps->tcps_reclaim = B_FALSE;
3863 3869 mutex_exit(&tcps->tcps_reclaim_lock);
3864 3870 if (tcps->tcps_reclaim_tid != 0)
3865 3871 (void) untimeout(tcps->tcps_reclaim_tid);
3866 3872 mutex_destroy(&tcps->tcps_reclaim_lock);
3867 3873
3868 3874 tcp_listener_conf_cleanup(tcps);
3869 3875
3870 3876 for (i = 0; i < tcps->tcps_sc_cnt; i++)
3871 3877 kmem_free(tcps->tcps_sc[i], sizeof (tcp_stats_cpu_t));
3872 3878 kmem_free(tcps->tcps_sc, max_ncpus * sizeof (tcp_stats_cpu_t *));
3873 3879
3874 3880 kmem_free(tcps->tcps_propinfo_tbl,
3875 3881 tcp_propinfo_count * sizeof (mod_prop_info_t));
3876 3882 tcps->tcps_propinfo_tbl = NULL;
3877 3883
3878 3884 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) {
3879 3885 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL);
3880 3886 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock);
3881 3887 }
3882 3888
3883 3889 for (i = 0; i < TCP_ACCEPTOR_FANOUT_SIZE; i++) {
3884 3890 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL);
3885 3891 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock);
3886 3892 }
3887 3893
3888 3894 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE);
3889 3895 tcps->tcps_bind_fanout = NULL;
3890 3896
3891 3897 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) *
3892 3898 TCP_ACCEPTOR_FANOUT_SIZE);
3893 3899 tcps->tcps_acceptor_fanout = NULL;
3894 3900
3895 3901 mutex_destroy(&tcps->tcps_iss_key_lock);
3896 3902 mutex_destroy(&tcps->tcps_epriv_port_lock);
3897 3903
3898 3904 ip_drop_unregister(&tcps->tcps_dropper);
3899 3905
3900 3906 tcp_kstat2_fini(stackid, tcps->tcps_kstat);
3901 3907 tcps->tcps_kstat = NULL;
3902 3908
3903 3909 tcp_kstat_fini(stackid, tcps->tcps_mibkp);
3904 3910 tcps->tcps_mibkp = NULL;
3905 3911
3906 3912 ldi_ident_release(tcps->tcps_ldi_ident);
3907 3913 kmem_free(tcps, sizeof (*tcps));
3908 3914 }
3909 3915
3910 3916 /*
3911 3917 * Generate ISS, taking into account NDD changes may happen halfway through.
3912 3918 * (If the iss is not zero, set it.)
3913 3919 */
3914 3920
3915 3921 static void
3916 3922 tcp_iss_init(tcp_t *tcp)
3917 3923 {
3918 3924 MD5_CTX context;
3919 3925 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg;
3920 3926 uint32_t answer[4];
3921 3927 tcp_stack_t *tcps = tcp->tcp_tcps;
3922 3928 conn_t *connp = tcp->tcp_connp;
3923 3929
3924 3930 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1);
3925 3931 tcp->tcp_iss = tcps->tcps_iss_incr_extra;
3926 3932 switch (tcps->tcps_strong_iss) {
3927 3933 case 2:
3928 3934 mutex_enter(&tcps->tcps_iss_key_lock);
3929 3935 context = tcps->tcps_iss_key;
3930 3936 mutex_exit(&tcps->tcps_iss_key_lock);
3931 3937 arg.ports = connp->conn_ports;
3932 3938 arg.src = connp->conn_laddr_v6;
3933 3939 arg.dst = connp->conn_faddr_v6;
3934 3940 MD5Update(&context, (uchar_t *)&arg, sizeof (arg));
3935 3941 MD5Final((uchar_t *)answer, &context);
3936 3942 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3];
3937 3943 /*
3938 3944 * Now that we've hashed into a unique per-connection sequence
3939 3945 * space, add a random increment per strong_iss == 1. So I
3940 3946 * guess we'll have to...
3941 3947 */
3942 3948 /* FALLTHRU */
3943 3949 case 1:
3944 3950 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random();
3945 3951 break;
3946 3952 default:
3947 3953 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR;
3948 3954 break;
3949 3955 }
3950 3956 tcp->tcp_valid_bits = TCP_ISS_VALID;
3951 3957 tcp->tcp_fss = tcp->tcp_iss - 1;
3952 3958 tcp->tcp_suna = tcp->tcp_iss;
3953 3959 tcp->tcp_snxt = tcp->tcp_iss + 1;
3954 3960 tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
3955 3961 tcp->tcp_csuna = tcp->tcp_snxt;
3956 3962 }
3957 3963
3958 3964 /*
3959 3965 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL
3960 3966 * on the specified backing STREAMS q. Note, the caller may make the
3961 3967 * decision to call based on the tcp_t.tcp_flow_stopped value which
3962 3968 * when check outside the q's lock is only an advisory check ...
3963 3969 */
3964 3970 void
3965 3971 tcp_setqfull(tcp_t *tcp)
3966 3972 {
3967 3973 tcp_stack_t *tcps = tcp->tcp_tcps;
3968 3974 conn_t *connp = tcp->tcp_connp;
3969 3975
3970 3976 if (tcp->tcp_closed)
3971 3977 return;
3972 3978
3973 3979 conn_setqfull(connp, &tcp->tcp_flow_stopped);
3974 3980 if (tcp->tcp_flow_stopped)
3975 3981 TCP_STAT(tcps, tcp_flwctl_on);
3976 3982 }
3977 3983
3978 3984 void
3979 3985 tcp_clrqfull(tcp_t *tcp)
3980 3986 {
3981 3987 conn_t *connp = tcp->tcp_connp;
3982 3988
3983 3989 if (tcp->tcp_closed)
3984 3990 return;
3985 3991 conn_clrqfull(connp, &tcp->tcp_flow_stopped);
3986 3992 }
3987 3993
3988 3994 static int
3989 3995 tcp_squeue_switch(int val)
3990 3996 {
3991 3997 int rval = SQ_FILL;
3992 3998
3993 3999 switch (val) {
3994 4000 case 1:
3995 4001 rval = SQ_NODRAIN;
3996 4002 break;
3997 4003 case 2:
3998 4004 rval = SQ_PROCESS;
3999 4005 break;
4000 4006 default:
4001 4007 break;
4002 4008 }
4003 4009 return (rval);
4004 4010 }
4005 4011
4006 4012 /*
4007 4013 * This is called once for each squeue - globally for all stack
4008 4014 * instances.
4009 4015 */
4010 4016 static void
4011 4017 tcp_squeue_add(squeue_t *sqp)
4012 4018 {
4013 4019 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc(
4014 4020 sizeof (tcp_squeue_priv_t), KM_SLEEP);
4015 4021
4016 4022 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait;
4017 4023 if (tcp_free_list_max_cnt == 0) {
4018 4024 int tcp_ncpus = ((boot_max_ncpus == -1) ?
4019 4025 max_ncpus : boot_max_ncpus);
4020 4026
4021 4027 /*
4022 4028 * Limit number of entries to 1% of availble memory / tcp_ncpus
4023 4029 */
4024 4030 tcp_free_list_max_cnt = (freemem * PAGESIZE) /
4025 4031 (tcp_ncpus * sizeof (tcp_t) * 100);
4026 4032 }
4027 4033 tcp_time_wait->tcp_free_list_cnt = 0;
4028 4034 }
4029 4035 /*
4030 4036 * Return unix error is tli error is TSYSERR, otherwise return a negative
4031 4037 * tli error.
4032 4038 */
4033 4039 int
4034 4040 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr,
4035 4041 boolean_t bind_to_req_port_only)
4036 4042 {
4037 4043 int error;
4038 4044 tcp_t *tcp = connp->conn_tcp;
4039 4045
4040 4046 if (tcp->tcp_state >= TCPS_BOUND) {
4041 4047 if (connp->conn_debug) {
4042 4048 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
4043 4049 "tcp_bind: bad state, %d", tcp->tcp_state);
4044 4050 }
4045 4051 return (-TOUTSTATE);
4046 4052 }
4047 4053
4048 4054 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only);
4049 4055 if (error != 0)
4050 4056 return (error);
4051 4057
4052 4058 ASSERT(tcp->tcp_state == TCPS_BOUND);
4053 4059 tcp->tcp_conn_req_max = 0;
4054 4060 return (0);
4055 4061 }
4056 4062
4057 4063 /*
4058 4064 * If the return value from this function is positive, it's a UNIX error.
4059 4065 * Otherwise, if it's negative, then the absolute value is a TLI error.
4060 4066 * the TPI routine tcp_tpi_connect() is a wrapper function for this.
4061 4067 */
4062 4068 int
4063 4069 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len,
4064 4070 cred_t *cr, pid_t pid)
4065 4071 {
4066 4072 tcp_t *tcp = connp->conn_tcp;
4067 4073 sin_t *sin = (sin_t *)sa;
4068 4074 sin6_t *sin6 = (sin6_t *)sa;
4069 4075 ipaddr_t *dstaddrp;
4070 4076 in_port_t dstport;
4071 4077 uint_t srcid;
4072 4078 int error;
4073 4079 uint32_t mss;
4074 4080 mblk_t *syn_mp;
4075 4081 tcp_stack_t *tcps = tcp->tcp_tcps;
4076 4082 int32_t oldstate;
4077 4083 ip_xmit_attr_t *ixa = connp->conn_ixa;
4078 4084
4079 4085 oldstate = tcp->tcp_state;
4080 4086
4081 4087 switch (len) {
4082 4088 default:
4083 4089 /*
4084 4090 * Should never happen
4085 4091 */
4086 4092 return (EINVAL);
4087 4093
4088 4094 case sizeof (sin_t):
4089 4095 sin = (sin_t *)sa;
4090 4096 if (sin->sin_port == 0) {
4091 4097 return (-TBADADDR);
4092 4098 }
4093 4099 if (connp->conn_ipv6_v6only) {
4094 4100 return (EAFNOSUPPORT);
4095 4101 }
4096 4102 break;
4097 4103
4098 4104 case sizeof (sin6_t):
4099 4105 sin6 = (sin6_t *)sa;
4100 4106 if (sin6->sin6_port == 0) {
4101 4107 return (-TBADADDR);
4102 4108 }
4103 4109 break;
4104 4110 }
4105 4111 /*
4106 4112 * If we're connecting to an IPv4-mapped IPv6 address, we need to
4107 4113 * make sure that the conn_ipversion is IPV4_VERSION. We
4108 4114 * need to this before we call tcp_bindi() so that the port lookup
4109 4115 * code will look for ports in the correct port space (IPv4 and
4110 4116 * IPv6 have separate port spaces).
4111 4117 */
4112 4118 if (connp->conn_family == AF_INET6 &&
4113 4119 connp->conn_ipversion == IPV6_VERSION &&
4114 4120 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
4115 4121 if (connp->conn_ipv6_v6only)
4116 4122 return (EADDRNOTAVAIL);
4117 4123
4118 4124 connp->conn_ipversion = IPV4_VERSION;
4119 4125 }
4120 4126
4121 4127 switch (tcp->tcp_state) {
4122 4128 case TCPS_LISTEN:
4123 4129 /*
4124 4130 * Listening sockets are not allowed to issue connect().
4125 4131 */
4126 4132 if (IPCL_IS_NONSTR(connp))
4127 4133 return (EOPNOTSUPP);
4128 4134 /* FALLTHRU */
4129 4135 case TCPS_IDLE:
4130 4136 /*
4131 4137 * We support quick connect, refer to comments in
4132 4138 * tcp_connect_*()
4133 4139 */
4134 4140 /* FALLTHRU */
4135 4141 case TCPS_BOUND:
4136 4142 break;
4137 4143 default:
4138 4144 return (-TOUTSTATE);
4139 4145 }
4140 4146
4141 4147 /*
4142 4148 * We update our cred/cpid based on the caller of connect
4143 4149 */
4144 4150 if (connp->conn_cred != cr) {
4145 4151 crhold(cr);
4146 4152 crfree(connp->conn_cred);
4147 4153 connp->conn_cred = cr;
4148 4154 }
4149 4155 connp->conn_cpid = pid;
4150 4156
4151 4157 /* Cache things in the ixa without any refhold */
4152 4158 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
4153 4159 ixa->ixa_cred = cr;
4154 4160 ixa->ixa_cpid = pid;
4155 4161 if (is_system_labeled()) {
4156 4162 /* We need to restart with a label based on the cred */
4157 4163 ip_xmit_attr_restore_tsl(ixa, ixa->ixa_cred);
4158 4164 }
4159 4165
4160 4166 if (connp->conn_family == AF_INET6) {
4161 4167 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
4162 4168 error = tcp_connect_ipv6(tcp, &sin6->sin6_addr,
4163 4169 sin6->sin6_port, sin6->sin6_flowinfo,
4164 4170 sin6->__sin6_src_id, sin6->sin6_scope_id);
4165 4171 } else {
4166 4172 /*
4167 4173 * Destination adress is mapped IPv6 address.
4168 4174 * Source bound address should be unspecified or
4169 4175 * IPv6 mapped address as well.
4170 4176 */
4171 4177 if (!IN6_IS_ADDR_UNSPECIFIED(
4172 4178 &connp->conn_bound_addr_v6) &&
4173 4179 !IN6_IS_ADDR_V4MAPPED(&connp->conn_bound_addr_v6)) {
4174 4180 return (EADDRNOTAVAIL);
4175 4181 }
4176 4182 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr));
4177 4183 dstport = sin6->sin6_port;
4178 4184 srcid = sin6->__sin6_src_id;
4179 4185 error = tcp_connect_ipv4(tcp, dstaddrp, dstport,
4180 4186 srcid);
4181 4187 }
4182 4188 } else {
4183 4189 dstaddrp = &sin->sin_addr.s_addr;
4184 4190 dstport = sin->sin_port;
4185 4191 srcid = 0;
4186 4192 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid);
4187 4193 }
4188 4194
4189 4195 if (error != 0)
4190 4196 goto connect_failed;
4191 4197
4192 4198 CL_INET_CONNECT(connp, B_TRUE, error);
4193 4199 if (error != 0)
4194 4200 goto connect_failed;
4195 4201
4196 4202 /* connect succeeded */
4197 4203 TCPS_BUMP_MIB(tcps, tcpActiveOpens);
4198 4204 tcp->tcp_active_open = 1;
4199 4205
4200 4206 /*
4201 4207 * tcp_set_destination() does not adjust for TCP/IP header length.
4202 4208 */
4203 4209 mss = tcp->tcp_mss - connp->conn_ht_iphc_len;
4204 4210
4205 4211 /*
4206 4212 * Just make sure our rwnd is at least rcvbuf * MSS large, and round up
4207 4213 * to the nearest MSS.
4208 4214 *
4209 4215 * We do the round up here because we need to get the interface MTU
4210 4216 * first before we can do the round up.
4211 4217 */
4212 4218 tcp->tcp_rwnd = connp->conn_rcvbuf;
4213 4219 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss),
4214 4220 tcps->tcps_recv_hiwat_minmss * mss);
4215 4221 connp->conn_rcvbuf = tcp->tcp_rwnd;
4216 4222 tcp_set_ws_value(tcp);
4217 4223 tcp->tcp_tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
4218 4224 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always)
4219 4225 tcp->tcp_snd_ws_ok = B_TRUE;
4220 4226
4221 4227 /*
4222 4228 * Set tcp_snd_ts_ok to true
4223 4229 * so that tcp_xmit_mp will
4224 4230 * include the timestamp
4225 4231 * option in the SYN segment.
4226 4232 */
4227 4233 if (tcps->tcps_tstamp_always ||
4228 4234 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) {
4229 4235 tcp->tcp_snd_ts_ok = B_TRUE;
4230 4236 }
4231 4237
4232 4238 /*
4233 4239 * Note that tcp_snd_sack_ok can be set in tcp_set_destination() if
4234 4240 * the SACK metric is set. So here we just check the per stack SACK
4235 4241 * permitted param.
4236 4242 */
4237 4243 if (tcps->tcps_sack_permitted == 2) {
4238 4244 ASSERT(tcp->tcp_num_sack_blk == 0);
4239 4245 ASSERT(tcp->tcp_notsack_list == NULL);
4240 4246 tcp->tcp_snd_sack_ok = B_TRUE;
4241 4247 }
4242 4248
4243 4249 /*
4244 4250 * Should we use ECN? Note that the current
4245 4251 * default value (SunOS 5.9) of tcp_ecn_permitted
4246 4252 * is 1. The reason for doing this is that there
4247 4253 * are equipments out there that will drop ECN
4248 4254 * enabled IP packets. Setting it to 1 avoids
4249 4255 * compatibility problems.
4250 4256 */
4251 4257 if (tcps->tcps_ecn_permitted == 2)
4252 4258 tcp->tcp_ecn_ok = B_TRUE;
4253 4259
4254 4260 /* Trace change from BOUND -> SYN_SENT here */
4255 4261 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
4256 4262 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
4257 4263 int32_t, TCPS_BOUND);
4258 4264
4259 4265 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
4260 4266 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL,
4261 4267 tcp->tcp_iss, B_FALSE, NULL, B_FALSE);
4262 4268 if (syn_mp != NULL) {
4263 4269 /*
4264 4270 * We must bump the generation before sending the syn
4265 4271 * to ensure that we use the right generation in case
4266 4272 * this thread issues a "connected" up call.
4267 4273 */
4268 4274 SOCK_CONNID_BUMP(tcp->tcp_connid);
4269 4275 /*
4270 4276 * DTrace sending the first SYN as a
4271 4277 * tcp:::connect-request event.
4272 4278 */
4273 4279 DTRACE_TCP5(connect__request, mblk_t *, NULL,
4274 4280 ip_xmit_attr_t *, connp->conn_ixa,
4275 4281 void_ip_t *, syn_mp->b_rptr, tcp_t *, tcp,
4276 4282 tcph_t *,
4277 4283 &syn_mp->b_rptr[connp->conn_ixa->ixa_ip_hdr_length]);
4278 4284 tcp_send_data(tcp, syn_mp);
4279 4285 }
4280 4286
4281 4287 if (tcp->tcp_conn.tcp_opts_conn_req != NULL)
4282 4288 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req);
4283 4289 return (0);
4284 4290
4285 4291 connect_failed:
4286 4292 connp->conn_faddr_v6 = ipv6_all_zeros;
4287 4293 connp->conn_fport = 0;
4288 4294 tcp->tcp_state = oldstate;
4289 4295 if (tcp->tcp_conn.tcp_opts_conn_req != NULL)
4290 4296 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req);
4291 4297 return (error);
4292 4298 }
4293 4299
4294 4300 int
4295 4301 tcp_do_listen(conn_t *connp, struct sockaddr *sa, socklen_t len,
4296 4302 int backlog, cred_t *cr, boolean_t bind_to_req_port_only)
4297 4303 {
4298 4304 tcp_t *tcp = connp->conn_tcp;
4299 4305 int error = 0;
4300 4306 tcp_stack_t *tcps = tcp->tcp_tcps;
4301 4307 int32_t oldstate;
4302 4308
4303 4309 /* All Solaris components should pass a cred for this operation. */
4304 4310 ASSERT(cr != NULL);
4305 4311
4306 4312 if (tcp->tcp_state >= TCPS_BOUND) {
4307 4313 if ((tcp->tcp_state == TCPS_BOUND ||
4308 4314 tcp->tcp_state == TCPS_LISTEN) && backlog > 0) {
4309 4315 /*
4310 4316 * Handle listen() increasing backlog.
4311 4317 * This is more "liberal" then what the TPI spec
4312 4318 * requires but is needed to avoid a t_unbind
4313 4319 * when handling listen() since the port number
4314 4320 * might be "stolen" between the unbind and bind.
4315 4321 */
4316 4322 goto do_listen;
4317 4323 }
4318 4324 if (connp->conn_debug) {
4319 4325 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
4320 4326 "tcp_listen: bad state, %d", tcp->tcp_state);
4321 4327 }
4322 4328 return (-TOUTSTATE);
4323 4329 } else {
4324 4330 if (sa == NULL) {
4325 4331 sin6_t addr;
4326 4332 sin_t *sin;
4327 4333 sin6_t *sin6;
4328 4334
4329 4335 ASSERT(IPCL_IS_NONSTR(connp));
4330 4336 /* Do an implicit bind: Request for a generic port. */
4331 4337 if (connp->conn_family == AF_INET) {
4332 4338 len = sizeof (sin_t);
4333 4339 sin = (sin_t *)&addr;
4334 4340 *sin = sin_null;
4335 4341 sin->sin_family = AF_INET;
4336 4342 } else {
4337 4343 ASSERT(connp->conn_family == AF_INET6);
4338 4344 len = sizeof (sin6_t);
4339 4345 sin6 = (sin6_t *)&addr;
4340 4346 *sin6 = sin6_null;
4341 4347 sin6->sin6_family = AF_INET6;
4342 4348 }
4343 4349 sa = (struct sockaddr *)&addr;
4344 4350 }
4345 4351
4346 4352 error = tcp_bind_check(connp, sa, len, cr,
4347 4353 bind_to_req_port_only);
4348 4354 if (error)
4349 4355 return (error);
4350 4356 /* Fall through and do the fanout insertion */
4351 4357 }
4352 4358
4353 4359 do_listen:
4354 4360 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN);
4355 4361 tcp->tcp_conn_req_max = backlog;
4356 4362 if (tcp->tcp_conn_req_max) {
4357 4363 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min)
4358 4364 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min;
4359 4365 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q)
4360 4366 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q;
4361 4367 /*
4362 4368 * If this is a listener, do not reset the eager list
4363 4369 * and other stuffs. Note that we don't check if the
4364 4370 * existing eager list meets the new tcp_conn_req_max
4365 4371 * requirement.
4366 4372 */
4367 4373 if (tcp->tcp_state != TCPS_LISTEN) {
4368 4374 tcp->tcp_state = TCPS_LISTEN;
4369 4375 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
4370 4376 connp->conn_ixa, void, NULL, tcp_t *, tcp,
4371 4377 void, NULL, int32_t, TCPS_BOUND);
4372 4378 /* Initialize the chain. Don't need the eager_lock */
4373 4379 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp;
4374 4380 tcp->tcp_eager_next_drop_q0 = tcp;
4375 4381 tcp->tcp_eager_prev_drop_q0 = tcp;
4376 4382 tcp->tcp_second_ctimer_threshold =
4377 4383 tcps->tcps_ip_abort_linterval;
4378 4384 }
4379 4385 }
4380 4386
4381 4387 /*
4382 4388 * We need to make sure that the conn_recv is set to a non-null
4383 4389 * value before we insert the conn into the classifier table.
4384 4390 * This is to avoid a race with an incoming packet which does an
4385 4391 * ipcl_classify().
4386 4392 * We initially set it to tcp_input_listener_unbound to try to
4387 4393 * pick a good squeue for the listener when the first SYN arrives.
4388 4394 * tcp_input_listener_unbound sets it to tcp_input_listener on that
4389 4395 * first SYN.
4390 4396 */
4391 4397 connp->conn_recv = tcp_input_listener_unbound;
4392 4398
4393 4399 /* Insert the listener in the classifier table */
4394 4400 error = ip_laddr_fanout_insert(connp);
4395 4401 if (error != 0) {
4396 4402 /* Undo the bind - release the port number */
4397 4403 oldstate = tcp->tcp_state;
4398 4404 tcp->tcp_state = TCPS_IDLE;
4399 4405 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
4400 4406 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
4401 4407 int32_t, oldstate);
4402 4408 connp->conn_bound_addr_v6 = ipv6_all_zeros;
4403 4409
4404 4410 connp->conn_laddr_v6 = ipv6_all_zeros;
4405 4411 connp->conn_saddr_v6 = ipv6_all_zeros;
4406 4412 connp->conn_ports = 0;
4407 4413
4408 4414 if (connp->conn_anon_port) {
4409 4415 zone_t *zone;
4410 4416
4411 4417 zone = crgetzone(cr);
4412 4418 connp->conn_anon_port = B_FALSE;
4413 4419 (void) tsol_mlp_anon(zone, connp->conn_mlp_type,
4414 4420 connp->conn_proto, connp->conn_lport, B_FALSE);
4415 4421 }
4416 4422 connp->conn_mlp_type = mlptSingle;
4417 4423
4418 4424 tcp_bind_hash_remove(tcp);
4419 4425 return (error);
4420 4426 } else {
4421 4427 /*
4422 4428 * If there is a connection limit, allocate and initialize
4423 4429 * the counter struct. Note that since listen can be called
4424 4430 * multiple times, the struct may have been allready allocated.
4425 4431 */
4426 4432 if (!list_is_empty(&tcps->tcps_listener_conf) &&
4427 4433 tcp->tcp_listen_cnt == NULL) {
4428 4434 tcp_listen_cnt_t *tlc;
4429 4435 uint32_t ratio;
4430 4436
4431 4437 ratio = tcp_find_listener_conf(tcps,
4432 4438 ntohs(connp->conn_lport));
4433 4439 if (ratio != 0) {
4434 4440 uint32_t mem_ratio, tot_buf;
4435 4441
4436 4442 tlc = kmem_alloc(sizeof (tcp_listen_cnt_t),
4437 4443 KM_SLEEP);
4438 4444 /*
4439 4445 * Calculate the connection limit based on
4440 4446 * the configured ratio and maxusers. Maxusers
4441 4447 * are calculated based on memory size,
4442 4448 * ~ 1 user per MB. Note that the conn_rcvbuf
4443 4449 * and conn_sndbuf may change after a
4444 4450 * connection is accepted. So what we have
4445 4451 * is only an approximation.
4446 4452 */
4447 4453 if ((tot_buf = connp->conn_rcvbuf +
4448 4454 connp->conn_sndbuf) < MB) {
4449 4455 mem_ratio = MB / tot_buf;
4450 4456 tlc->tlc_max = maxusers / ratio *
4451 4457 mem_ratio;
4452 4458 } else {
4453 4459 mem_ratio = tot_buf / MB;
4454 4460 tlc->tlc_max = maxusers / ratio /
4455 4461 mem_ratio;
4456 4462 }
4457 4463 /* At least we should allow two connections! */
4458 4464 if (tlc->tlc_max <= tcp_min_conn_listener)
4459 4465 tlc->tlc_max = tcp_min_conn_listener;
4460 4466 tlc->tlc_cnt = 1;
4461 4467 tlc->tlc_drop = 0;
4462 4468 tcp->tcp_listen_cnt = tlc;
4463 4469 }
4464 4470 }
4465 4471 }
4466 4472 return (error);
4467 4473 }
|
↓ open down ↓ |
2071 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX