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