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