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 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* Copyright (c) 1990 Mentat Inc. */ 27 28 #include <sys/types.h> 29 #include <sys/stream.h> 30 #include <sys/strsun.h> 31 #include <sys/strsubr.h> 32 #include <sys/stropts.h> 33 #include <sys/strlog.h> 34 #define _SUN_TPI_VERSION 2 35 #include <sys/tihdr.h> 36 #include <sys/timod.h> 37 #include <sys/ddi.h> 38 #include <sys/sunddi.h> 39 #include <sys/suntpi.h> 40 #include <sys/xti_inet.h> 41 #include <sys/cmn_err.h> 42 #include <sys/debug.h> 43 #include <sys/sdt.h> 44 #include <sys/vtrace.h> 45 #include <sys/kmem.h> 46 #include <sys/ethernet.h> 47 #include <sys/cpuvar.h> 48 #include <sys/dlpi.h> 49 #include <sys/multidata.h> 50 #include <sys/multidata_impl.h> 51 #include <sys/pattr.h> 52 #include <sys/policy.h> 53 #include <sys/priv.h> 54 #include <sys/zone.h> 55 #include <sys/sunldi.h> 56 57 #include <sys/errno.h> 58 #include <sys/signal.h> 59 #include <sys/socket.h> 60 #include <sys/socketvar.h> 61 #include <sys/sockio.h> 62 #include <sys/isa_defs.h> 63 #include <sys/md5.h> 64 #include <sys/random.h> 65 #include <sys/uio.h> 66 #include <sys/systm.h> 67 #include <netinet/in.h> 68 #include <netinet/tcp.h> 69 #include <netinet/ip6.h> 70 #include <netinet/icmp6.h> 71 #include <net/if.h> 72 #include <net/route.h> 73 #include <inet/ipsec_impl.h> 74 75 #include <inet/common.h> 76 #include <inet/ip.h> 77 #include <inet/ip_impl.h> 78 #include <inet/ip6.h> 79 #include <inet/ip_ndp.h> 80 #include <inet/proto_set.h> 81 #include <inet/mib2.h> 82 #include <inet/nd.h> 83 #include <inet/optcom.h> 84 #include <inet/snmpcom.h> 85 #include <inet/kstatcom.h> 86 #include <inet/tcp.h> 87 #include <inet/tcp_impl.h> 88 #include <inet/udp_impl.h> 89 #include <net/pfkeyv2.h> 90 #include <inet/ipsec_info.h> 91 #include <inet/ipdrop.h> 92 93 #include <inet/ipclassifier.h> 94 #include <inet/ip_ire.h> 95 #include <inet/ip_ftable.h> 96 #include <inet/ip_if.h> 97 #include <inet/ipp_common.h> 98 #include <inet/ip_netinfo.h> 99 #include <sys/squeue_impl.h> 100 #include <sys/squeue.h> 101 #include <inet/kssl/ksslapi.h> 102 #include <sys/tsol/label.h> 103 #include <sys/tsol/tnet.h> 104 #include <rpc/pmap_prot.h> 105 #include <sys/callo.h> 106 107 /* 108 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433) 109 * 110 * (Read the detailed design doc in PSARC case directory) 111 * 112 * The entire tcp state is contained in tcp_t and conn_t structure 113 * which are allocated in tandem using ipcl_conn_create() and passing 114 * IPCL_CONNTCP as a flag. We use 'conn_ref' and 'conn_lock' to protect 115 * the references on the tcp_t. The tcp_t structure is never compressed 116 * and packets always land on the correct TCP perimeter from the time 117 * eager is created till the time tcp_t dies (as such the old mentat 118 * TCP global queue is not used for detached state and no IPSEC checking 119 * is required). The global queue is still allocated to send out resets 120 * for connection which have no listeners and IP directly calls 121 * tcp_xmit_listeners_reset() which does any policy check. 122 * 123 * Protection and Synchronisation mechanism: 124 * 125 * The tcp data structure does not use any kind of lock for protecting 126 * its state but instead uses 'squeues' for mutual exclusion from various 127 * read and write side threads. To access a tcp member, the thread should 128 * always be behind squeue (via squeue_enter with flags as SQ_FILL, SQ_PROCESS, 129 * or SQ_NODRAIN). Since the squeues allow a direct function call, caller 130 * can pass any tcp function having prototype of edesc_t as argument 131 * (different from traditional STREAMs model where packets come in only 132 * designated entry points). The list of functions that can be directly 133 * called via squeue are listed before the usual function prototype. 134 * 135 * Referencing: 136 * 137 * TCP is MT-Hot and we use a reference based scheme to make sure that the 138 * tcp structure doesn't disappear when its needed. When the application 139 * creates an outgoing connection or accepts an incoming connection, we 140 * start out with 2 references on 'conn_ref'. One for TCP and one for IP. 141 * The IP reference is just a symbolic reference since ip_tcpclose() 142 * looks at tcp structure after tcp_close_output() returns which could 143 * have dropped the last TCP reference. So as long as the connection is 144 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the 145 * conn_t. The classifier puts its own reference when the connection is 146 * inserted in listen or connected hash. Anytime a thread needs to enter 147 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr 148 * on write side or by doing a classify on read side and then puts a 149 * reference on the conn before doing squeue_enter/tryenter/fill. For 150 * read side, the classifier itself puts the reference under fanout lock 151 * to make sure that tcp can't disappear before it gets processed. The 152 * squeue will drop this reference automatically so the called function 153 * doesn't have to do a DEC_REF. 154 * 155 * Opening a new connection: 156 * 157 * The outgoing connection open is pretty simple. tcp_open() does the 158 * work in creating the conn/tcp structure and initializing it. The 159 * squeue assignment is done based on the CPU the application 160 * is running on. So for outbound connections, processing is always done 161 * on application CPU which might be different from the incoming CPU 162 * being interrupted by the NIC. An optimal way would be to figure out 163 * the NIC <-> CPU binding at listen time, and assign the outgoing 164 * connection to the squeue attached to the CPU that will be interrupted 165 * for incoming packets (we know the NIC based on the bind IP address). 166 * This might seem like a problem if more data is going out but the 167 * fact is that in most cases the transmit is ACK driven transmit where 168 * the outgoing data normally sits on TCP's xmit queue waiting to be 169 * transmitted. 170 * 171 * Accepting a connection: 172 * 173 * This is a more interesting case because of various races involved in 174 * establishing a eager in its own perimeter. Read the meta comment on 175 * top of tcp_conn_request(). But briefly, the squeue is picked by 176 * ip_tcp_input()/ip_fanout_tcp_v6() based on the interrupted CPU. 177 * 178 * Closing a connection: 179 * 180 * The close is fairly straight forward. tcp_close() calls tcp_close_output() 181 * via squeue to do the close and mark the tcp as detached if the connection 182 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its 183 * reference but tcp_close() drop IP's reference always. So if tcp was 184 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP 185 * and 1 because it is in classifier's connected hash. This is the condition 186 * we use to determine that its OK to clean up the tcp outside of squeue 187 * when time wait expires (check the ref under fanout and conn_lock and 188 * if it is 2, remove it from fanout hash and kill it). 189 * 190 * Although close just drops the necessary references and marks the 191 * tcp_detached state, tcp_close needs to know the tcp_detached has been 192 * set (under squeue) before letting the STREAM go away (because a 193 * inbound packet might attempt to go up the STREAM while the close 194 * has happened and tcp_detached is not set). So a special lock and 195 * flag is used along with a condition variable (tcp_closelock, tcp_closed, 196 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked 197 * tcp_detached. 198 * 199 * Special provisions and fast paths: 200 * 201 * We make special provision for (AF_INET, SOCK_STREAM) sockets which 202 * can't have 'ipv6_recvpktinfo' set and for these type of sockets, IP 203 * will never send a M_CTL to TCP. As such, ip_tcp_input() which handles 204 * all TCP packets from the wire makes a IPCL_IS_TCP4_CONNECTED_NO_POLICY 205 * check to send packets directly to tcp_rput_data via squeue. Everyone 206 * else comes through tcp_input() on the read side. 207 * 208 * We also make special provisions for sockfs by marking tcp_issocket 209 * whenever we have only sockfs on top of TCP. This allows us to skip 210 * putting the tcp in acceptor hash since a sockfs listener can never 211 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM 212 * since eager has already been allocated and the accept now happens 213 * on acceptor STREAM. There is a big blob of comment on top of 214 * tcp_conn_request explaining the new accept. When socket is POP'd, 215 * sockfs sends us an ioctl to mark the fact and we go back to old 216 * behaviour. Once tcp_issocket is unset, its never set for the 217 * life of that connection. 218 * 219 * IPsec notes : 220 * 221 * Since a packet is always executed on the correct TCP perimeter 222 * all IPsec processing is defered to IP including checking new 223 * connections and setting IPSEC policies for new connection. The 224 * only exception is tcp_xmit_listeners_reset() which is called 225 * directly from IP and needs to policy check to see if TH_RST 226 * can be sent out. 227 * 228 * PFHooks notes : 229 * 230 * For mdt case, one meta buffer contains multiple packets. Mblks for every 231 * packet are assembled and passed to the hooks. When packets are blocked, 232 * or boundary of any packet is changed, the mdt processing is stopped, and 233 * packets of the meta buffer are send to the IP path one by one. 234 */ 235 236 /* 237 * Values for squeue switch: 238 * 1: SQ_NODRAIN 239 * 2: SQ_PROCESS 240 * 3: SQ_FILL 241 */ 242 int tcp_squeue_wput = 2; /* /etc/systems */ 243 int tcp_squeue_flag; 244 245 /* 246 * This controls how tiny a write must be before we try to copy it 247 * into the the mblk on the tail of the transmit queue. Not much 248 * speedup is observed for values larger than sixteen. Zero will 249 * disable the optimisation. 250 */ 251 int tcp_tx_pull_len = 16; 252 253 /* 254 * TCP Statistics. 255 * 256 * How TCP statistics work. 257 * 258 * There are two types of statistics invoked by two macros. 259 * 260 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 261 * supposed to be used in non MT-hot paths of the code. 262 * 263 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 264 * supposed to be used for DEBUG purposes and may be used on a hot path. 265 * 266 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 267 * (use "kstat tcp" to get them). 268 * 269 * There is also additional debugging facility that marks tcp_clean_death() 270 * instances and saves them in tcp_t structure. It is triggered by 271 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 272 * tcp_clean_death() calls that counts the number of times each tag was hit. It 273 * is triggered by TCP_CLD_COUNTERS define. 274 * 275 * How to add new counters. 276 * 277 * 1) Add a field in the tcp_stat structure describing your counter. 278 * 2) Add a line in the template in tcp_kstat2_init() with the name 279 * of the counter. 280 * 281 * IMPORTANT!! - make sure that both are in sync !! 282 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 283 * 284 * Please avoid using private counters which are not kstat-exported. 285 * 286 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 287 * in tcp_t structure. 288 * 289 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 290 */ 291 292 #ifndef TCP_DEBUG_COUNTER 293 #ifdef DEBUG 294 #define TCP_DEBUG_COUNTER 1 295 #else 296 #define TCP_DEBUG_COUNTER 0 297 #endif 298 #endif 299 300 #define TCP_CLD_COUNTERS 0 301 302 #define TCP_TAG_CLEAN_DEATH 1 303 #define TCP_MAX_CLEAN_DEATH_TAG 32 304 305 #ifdef lint 306 static int _lint_dummy_; 307 #endif 308 309 #if TCP_CLD_COUNTERS 310 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 311 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 312 #elif defined(lint) 313 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 314 #else 315 #define TCP_CLD_STAT(x) 316 #endif 317 318 #if TCP_DEBUG_COUNTER 319 #define TCP_DBGSTAT(tcps, x) \ 320 atomic_add_64(&((tcps)->tcps_statistics.x.value.ui64), 1) 321 #define TCP_G_DBGSTAT(x) \ 322 atomic_add_64(&(tcp_g_statistics.x.value.ui64), 1) 323 #elif defined(lint) 324 #define TCP_DBGSTAT(tcps, x) ASSERT(_lint_dummy_ == 0); 325 #define TCP_G_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 326 #else 327 #define TCP_DBGSTAT(tcps, x) 328 #define TCP_G_DBGSTAT(x) 329 #endif 330 331 #define TCP_G_STAT(x) (tcp_g_statistics.x.value.ui64++) 332 333 tcp_g_stat_t tcp_g_statistics; 334 kstat_t *tcp_g_kstat; 335 336 /* 337 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 338 * tcp write side. 339 */ 340 #define CALL_IP_WPUT(connp, q, mp) { \ 341 ASSERT(((q)->q_flag & QREADR) == 0); \ 342 TCP_DBGSTAT(connp->conn_netstack->netstack_tcp, tcp_ip_output); \ 343 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 344 } 345 346 /* Macros for timestamp comparisons */ 347 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 348 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 349 350 /* 351 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 352 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 353 * by adding three components: a time component which grows by 1 every 4096 354 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 355 * a per-connection component which grows by 125000 for every new connection; 356 * and an "extra" component that grows by a random amount centered 357 * approximately on 64000. This causes the the ISS generator to cycle every 358 * 4.89 hours if no TCP connections are made, and faster if connections are 359 * made. 360 * 361 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 362 * components: a time component which grows by 250000 every second; and 363 * a per-connection component which grows by 125000 for every new connections. 364 * 365 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 366 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 367 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 368 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 369 * password. 370 */ 371 #define ISS_INCR 250000 372 #define ISS_NSEC_SHT 12 373 374 static sin_t sin_null; /* Zero address for quick clears */ 375 static sin6_t sin6_null; /* Zero address for quick clears */ 376 377 /* 378 * This implementation follows the 4.3BSD interpretation of the urgent 379 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 380 * incompatible changes in protocols like telnet and rlogin. 381 */ 382 #define TCP_OLD_URP_INTERPRETATION 1 383 384 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 385 (TCP_IS_DETACHED(tcp) && \ 386 (!(tcp)->tcp_hard_binding)) 387 388 /* 389 * TCP reassembly macros. We hide starting and ending sequence numbers in 390 * b_next and b_prev of messages on the reassembly queue. The messages are 391 * chained using b_cont. These macros are used in tcp_reass() so we don't 392 * have to see the ugly casts and assignments. 393 */ 394 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 395 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 396 (mblk_t *)(uintptr_t)(u)) 397 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 398 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 399 (mblk_t *)(uintptr_t)(u)) 400 401 /* 402 * Implementation of TCP Timers. 403 * ============================= 404 * 405 * INTERFACE: 406 * 407 * There are two basic functions dealing with tcp timers: 408 * 409 * timeout_id_t tcp_timeout(connp, func, time) 410 * clock_t tcp_timeout_cancel(connp, timeout_id) 411 * TCP_TIMER_RESTART(tcp, intvl) 412 * 413 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 414 * after 'time' ticks passed. The function called by timeout() must adhere to 415 * the same restrictions as a driver soft interrupt handler - it must not sleep 416 * or call other functions that might sleep. The value returned is the opaque 417 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 418 * cancel the request. The call to tcp_timeout() may fail in which case it 419 * returns zero. This is different from the timeout(9F) function which never 420 * fails. 421 * 422 * The call-back function 'func' always receives 'connp' as its single 423 * argument. It is always executed in the squeue corresponding to the tcp 424 * structure. The tcp structure is guaranteed to be present at the time the 425 * call-back is called. 426 * 427 * NOTE: The call-back function 'func' is never called if tcp is in 428 * the TCPS_CLOSED state. 429 * 430 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 431 * request. locks acquired by the call-back routine should not be held across 432 * the call to tcp_timeout_cancel() or a deadlock may result. 433 * 434 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 435 * Otherwise, it returns an integer value greater than or equal to 0. In 436 * particular, if the call-back function is already placed on the squeue, it can 437 * not be canceled. 438 * 439 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 440 * within squeue context corresponding to the tcp instance. Since the 441 * call-back is also called via the same squeue, there are no race 442 * conditions described in untimeout(9F) manual page since all calls are 443 * strictly serialized. 444 * 445 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 446 * stored in tcp_timer_tid and starts a new one using 447 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 448 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 449 * field. 450 * 451 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 452 * call-back may still be called, so it is possible tcp_timer() will be 453 * called several times. This should not be a problem since tcp_timer() 454 * should always check the tcp instance state. 455 * 456 * 457 * IMPLEMENTATION: 458 * 459 * TCP timers are implemented using three-stage process. The call to 460 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 461 * when the timer expires. The tcp_timer_callback() arranges the call of the 462 * tcp_timer_handler() function via squeue corresponding to the tcp 463 * instance. The tcp_timer_handler() calls actual requested timeout call-back 464 * and passes tcp instance as an argument to it. Information is passed between 465 * stages using the tcp_timer_t structure which contains the connp pointer, the 466 * tcp call-back to call and the timeout id returned by the timeout(9F). 467 * 468 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 469 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 470 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 471 * returns the pointer to this mblk. 472 * 473 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 474 * looks like a normal mblk without actual dblk attached to it. 475 * 476 * To optimize performance each tcp instance holds a small cache of timer 477 * mblocks. In the current implementation it caches up to two timer mblocks per 478 * tcp instance. The cache is preserved over tcp frees and is only freed when 479 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 480 * timer processing happens on a corresponding squeue, the cache manipulation 481 * does not require any locks. Experiments show that majority of timer mblocks 482 * allocations are satisfied from the tcp cache and do not involve kmem calls. 483 * 484 * The tcp_timeout() places a refhold on the connp instance which guarantees 485 * that it will be present at the time the call-back function fires. The 486 * tcp_timer_handler() drops the reference after calling the call-back, so the 487 * call-back function does not need to manipulate the references explicitly. 488 */ 489 490 typedef struct tcp_timer_s { 491 conn_t *connp; 492 void (*tcpt_proc)(void *); 493 callout_id_t tcpt_tid; 494 } tcp_timer_t; 495 496 static kmem_cache_t *tcp_timercache; 497 kmem_cache_t *tcp_sack_info_cache; 498 kmem_cache_t *tcp_iphc_cache; 499 500 /* 501 * For scalability, we must not run a timer for every TCP connection 502 * in TIME_WAIT state. To see why, consider (for time wait interval of 503 * 4 minutes): 504 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 505 * 506 * This list is ordered by time, so you need only delete from the head 507 * until you get to entries which aren't old enough to delete yet. 508 * The list consists of only the detached TIME_WAIT connections. 509 * 510 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 511 * becomes detached TIME_WAIT (either by changing the state and already 512 * being detached or the other way around). This means that the TIME_WAIT 513 * state can be extended (up to doubled) if the connection doesn't become 514 * detached for a long time. 515 * 516 * The list manipulations (including tcp_time_wait_next/prev) 517 * are protected by the tcp_time_wait_lock. The content of the 518 * detached TIME_WAIT connections is protected by the normal perimeters. 519 * 520 * This list is per squeue and squeues are shared across the tcp_stack_t's. 521 * Things on tcp_time_wait_head remain associated with the tcp_stack_t 522 * and conn_netstack. 523 * The tcp_t's that are added to tcp_free_list are disassociated and 524 * have NULL tcp_tcps and conn_netstack pointers. 525 */ 526 typedef struct tcp_squeue_priv_s { 527 kmutex_t tcp_time_wait_lock; 528 callout_id_t tcp_time_wait_tid; 529 tcp_t *tcp_time_wait_head; 530 tcp_t *tcp_time_wait_tail; 531 tcp_t *tcp_free_list; 532 uint_t tcp_free_list_cnt; 533 } tcp_squeue_priv_t; 534 535 /* 536 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 537 * Running it every 5 seconds seems to give the best results. 538 */ 539 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 540 541 /* 542 * To prevent memory hog, limit the number of entries in tcp_free_list 543 * to 1% of available memory / number of cpus 544 */ 545 uint_t tcp_free_list_max_cnt = 0; 546 547 #define TCP_XMIT_LOWATER 4096 548 #define TCP_XMIT_HIWATER 49152 549 #define TCP_RECV_LOWATER 2048 550 #define TCP_RECV_HIWATER 49152 551 552 /* 553 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 554 */ 555 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 556 557 #define TIDUSZ 4096 /* transport interface data unit size */ 558 559 /* 560 * Bind hash list size and has function. It has to be a power of 2 for 561 * hashing. 562 */ 563 #define TCP_BIND_FANOUT_SIZE 512 564 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 565 /* 566 * Size of listen and acceptor hash list. It has to be a power of 2 for 567 * hashing. 568 */ 569 #define TCP_FANOUT_SIZE 256 570 571 #ifdef _ILP32 572 #define TCP_ACCEPTOR_HASH(accid) \ 573 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 574 #else 575 #define TCP_ACCEPTOR_HASH(accid) \ 576 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 577 #endif /* _ILP32 */ 578 579 #define IP_ADDR_CACHE_SIZE 2048 580 #define IP_ADDR_CACHE_HASH(faddr) \ 581 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 582 583 /* 584 * TCP options struct returned from tcp_parse_options. 585 */ 586 typedef struct tcp_opt_s { 587 uint32_t tcp_opt_mss; 588 uint32_t tcp_opt_wscale; 589 uint32_t tcp_opt_ts_val; 590 uint32_t tcp_opt_ts_ecr; 591 tcp_t *tcp; 592 } tcp_opt_t; 593 594 /* 595 * TCP option struct passing information b/w lisenter and eager. 596 */ 597 struct tcp_options { 598 uint_t to_flags; 599 ssize_t to_boundif; /* IPV6_BOUND_IF */ 600 }; 601 602 #define TCPOPT_BOUNDIF 0x00000001 /* set IPV6_BOUND_IF */ 603 #define TCPOPT_RECVPKTINFO 0x00000002 /* set IPV6_RECVPKTINFO */ 604 605 /* 606 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 607 */ 608 609 #ifdef _BIG_ENDIAN 610 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 611 (TCPOPT_TSTAMP << 8) | 10) 612 #else 613 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 614 (TCPOPT_NOP << 8) | TCPOPT_NOP) 615 #endif 616 617 /* 618 * Flags returned from tcp_parse_options. 619 */ 620 #define TCP_OPT_MSS_PRESENT 1 621 #define TCP_OPT_WSCALE_PRESENT 2 622 #define TCP_OPT_TSTAMP_PRESENT 4 623 #define TCP_OPT_SACK_OK_PRESENT 8 624 #define TCP_OPT_SACK_PRESENT 16 625 626 /* TCP option length */ 627 #define TCPOPT_NOP_LEN 1 628 #define TCPOPT_MAXSEG_LEN 4 629 #define TCPOPT_WS_LEN 3 630 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 631 #define TCPOPT_TSTAMP_LEN 10 632 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 633 #define TCPOPT_SACK_OK_LEN 2 634 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 635 #define TCPOPT_REAL_SACK_LEN 4 636 #define TCPOPT_MAX_SACK_LEN 36 637 #define TCPOPT_HEADER_LEN 2 638 639 /* TCP cwnd burst factor. */ 640 #define TCP_CWND_INFINITE 65535 641 #define TCP_CWND_SS 3 642 #define TCP_CWND_NORMAL 5 643 644 /* Maximum TCP initial cwin (start/restart). */ 645 #define TCP_MAX_INIT_CWND 8 646 647 /* 648 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 649 * either tcp_slow_start_initial or tcp_slow_start_after idle 650 * depending on the caller. If the upper layer has not used the 651 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 652 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 653 * If the upper layer has changed set the tcp_init_cwnd, just use 654 * it to calculate the tcp_cwnd. 655 */ 656 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 657 { \ 658 if ((tcp)->tcp_init_cwnd == 0) { \ 659 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 660 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 661 } else { \ 662 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 663 } \ 664 tcp->tcp_cwnd_cnt = 0; \ 665 } 666 667 /* TCP Timer control structure */ 668 typedef struct tcpt_s { 669 pfv_t tcpt_pfv; /* The routine we are to call */ 670 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 671 } tcpt_t; 672 673 /* 674 * Functions called directly via squeue having a prototype of edesc_t. 675 */ 676 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 677 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 678 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 679 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 680 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 681 void tcp_input(void *arg, mblk_t *mp, void *arg2); 682 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 683 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 684 void tcp_output(void *arg, mblk_t *mp, void *arg2); 685 void tcp_output_urgent(void *arg, mblk_t *mp, void *arg2); 686 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 687 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 688 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 689 690 691 /* Prototype for TCP functions */ 692 static void tcp_random_init(void); 693 int tcp_random(void); 694 static void tcp_tli_accept(tcp_t *tcp, mblk_t *mp); 695 static int tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 696 tcp_t *eager); 697 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 698 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 699 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 700 boolean_t user_specified); 701 static void tcp_closei_local(tcp_t *tcp); 702 static void tcp_close_detached(tcp_t *tcp); 703 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 704 mblk_t *idmp, mblk_t **defermp); 705 static void tcp_tpi_connect(tcp_t *tcp, mblk_t *mp); 706 static int tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, 707 in_port_t dstport, uint_t srcid, cred_t *cr, pid_t pid); 708 static int tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, 709 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 710 uint32_t scope_id, cred_t *cr, pid_t pid); 711 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 712 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 713 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 714 static char *tcp_display(tcp_t *tcp, char *, char); 715 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 716 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 717 static void tcp_eager_unlink(tcp_t *tcp); 718 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 719 int unixerr); 720 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 721 int tlierr, int unixerr); 722 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 723 cred_t *cr); 724 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 725 char *value, caddr_t cp, cred_t *cr); 726 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 727 char *value, caddr_t cp, cred_t *cr); 728 static int tcp_tpistate(tcp_t *tcp); 729 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 730 int caller_holds_lock); 731 static void tcp_bind_hash_remove(tcp_t *tcp); 732 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 733 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 734 static void tcp_acceptor_hash_remove(tcp_t *tcp); 735 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 736 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 737 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 738 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 739 void tcp_g_q_setup(tcp_stack_t *); 740 void tcp_g_q_create(tcp_stack_t *); 741 void tcp_g_q_destroy(tcp_stack_t *); 742 static int tcp_header_init_ipv4(tcp_t *tcp); 743 static int tcp_header_init_ipv6(tcp_t *tcp); 744 int tcp_init(tcp_t *tcp, queue_t *q); 745 static int tcp_init_values(tcp_t *tcp); 746 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 747 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 748 static void tcp_ip_notify(tcp_t *tcp); 749 static mblk_t *tcp_ire_mp(mblk_t **mpp); 750 static void tcp_iss_init(tcp_t *tcp); 751 static void tcp_keepalive_killer(void *arg); 752 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 753 static void tcp_mss_set(tcp_t *tcp, uint32_t size, boolean_t do_ss); 754 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 755 int *do_disconnectp, int *t_errorp, int *sys_errorp); 756 static boolean_t tcp_allow_connopt_set(int level, int name); 757 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 758 int tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 759 int tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, 760 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 761 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 762 mblk_t *mblk); 763 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 764 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 765 uchar_t *ptr, uint_t len); 766 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 767 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 768 tcp_stack_t *); 769 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 770 caddr_t cp, cred_t *cr); 771 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 772 caddr_t cp, cred_t *cr); 773 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 774 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 775 caddr_t cp, cred_t *cr); 776 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 777 static void tcp_update_xmit_tail(tcp_t *tcp, uint32_t snxt); 778 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 779 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 780 static void tcp_reinit(tcp_t *tcp); 781 static void tcp_reinit_values(tcp_t *tcp); 782 783 static uint_t tcp_rwnd_reopen(tcp_t *tcp); 784 static uint_t tcp_rcv_drain(tcp_t *tcp); 785 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 786 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 787 static void tcp_ss_rexmit(tcp_t *tcp); 788 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 789 static void tcp_process_options(tcp_t *, tcph_t *); 790 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 791 static void tcp_rsrv(queue_t *q); 792 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 793 static int tcp_snmp_state(tcp_t *tcp); 794 static void tcp_timer(void *arg); 795 static void tcp_timer_callback(void *); 796 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 797 boolean_t random); 798 static in_port_t tcp_get_next_priv_port(const tcp_t *); 799 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 800 static void tcp_wput_fallback(queue_t *q, mblk_t *mp); 801 void tcp_tpi_accept(queue_t *q, mblk_t *mp); 802 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 803 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 804 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 805 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 806 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 807 const int num_sack_blk, int *usable, uint_t *snxt, 808 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 809 const int mdt_thres); 810 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 811 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 812 const int num_sack_blk, int *usable, uint_t *snxt, 813 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 814 const int mdt_thres); 815 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 816 int num_sack_blk); 817 static void tcp_wsrv(queue_t *q); 818 static int tcp_xmit_end(tcp_t *tcp); 819 static void tcp_ack_timer(void *arg); 820 static mblk_t *tcp_ack_mp(tcp_t *tcp); 821 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 822 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 823 zoneid_t zoneid, tcp_stack_t *, conn_t *connp); 824 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 825 uint32_t ack, int ctl); 826 static int setmaxps(queue_t *q, int maxpsz); 827 static void tcp_set_rto(tcp_t *, time_t); 828 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 829 boolean_t, boolean_t); 830 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 831 boolean_t ipsec_mctl); 832 static int tcp_build_hdrs(tcp_t *); 833 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 834 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 835 tcph_t *tcph); 836 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 837 static mblk_t *tcp_mdt_info_mp(mblk_t *); 838 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 839 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 840 const boolean_t, const uint32_t, const uint32_t, 841 const uint32_t, const uint32_t, tcp_stack_t *); 842 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 843 const uint_t, const uint_t, boolean_t *); 844 static mblk_t *tcp_lso_info_mp(mblk_t *); 845 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 846 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 847 extern mblk_t *tcp_timermp_alloc(int); 848 extern void tcp_timermp_free(tcp_t *); 849 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 850 static void tcp_stop_lingering(tcp_t *tcp); 851 static void tcp_close_linger_timeout(void *arg); 852 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 853 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 854 static void tcp_stack_fini(netstackid_t stackid, void *arg); 855 static void *tcp_g_kstat_init(tcp_g_stat_t *); 856 static void tcp_g_kstat_fini(kstat_t *); 857 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 858 static void tcp_kstat_fini(netstackid_t, kstat_t *); 859 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 860 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 861 static int tcp_kstat_update(kstat_t *kp, int rw); 862 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 863 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 864 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 865 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 866 tcph_t *tcph, mblk_t *idmp); 867 static int tcp_squeue_switch(int); 868 869 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t); 870 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *); 871 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *); 872 static int tcp_tpi_close(queue_t *, int); 873 static int tcp_tpi_close_accept(queue_t *); 874 875 static void tcp_squeue_add(squeue_t *); 876 static boolean_t tcp_zcopy_check(tcp_t *); 877 static void tcp_zcopy_notify(tcp_t *); 878 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 879 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 880 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 881 882 extern void tcp_kssl_input(tcp_t *, mblk_t *); 883 884 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 885 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 886 887 static int tcp_accept(sock_lower_handle_t, sock_lower_handle_t, 888 sock_upper_handle_t, cred_t *); 889 static int tcp_listen(sock_lower_handle_t, int, cred_t *); 890 static int tcp_post_ip_bind(tcp_t *, mblk_t *, int, cred_t *, pid_t); 891 static int tcp_do_listen(conn_t *, struct sockaddr *, socklen_t, int, cred_t *, 892 boolean_t); 893 static int tcp_do_connect(conn_t *, const struct sockaddr *, socklen_t, 894 cred_t *, pid_t); 895 static int tcp_do_bind(conn_t *, struct sockaddr *, socklen_t, cred_t *, 896 boolean_t); 897 static int tcp_do_unbind(conn_t *); 898 static int tcp_bind_check(conn_t *, struct sockaddr *, socklen_t, cred_t *, 899 boolean_t); 900 901 static void tcp_ulp_newconn(conn_t *, conn_t *, mblk_t *); 902 903 /* 904 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 905 * 906 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 907 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 908 * (defined in tcp.h) needs to be filled in and passed into the kernel 909 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 910 * structure contains the four-tuple of a TCP connection and a range of TCP 911 * states (specified by ac_start and ac_end). The use of wildcard addresses 912 * and ports is allowed. Connections with a matching four tuple and a state 913 * within the specified range will be aborted. The valid states for the 914 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 915 * inclusive. 916 * 917 * An application which has its connection aborted by this ioctl will receive 918 * an error that is dependent on the connection state at the time of the abort. 919 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 920 * though a RST packet has been received. If the connection state is equal to 921 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 922 * and all resources associated with the connection will be freed. 923 */ 924 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 925 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 926 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 927 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *, tcp_stack_t *tcps); 928 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 929 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 930 boolean_t, tcp_stack_t *); 931 932 static struct module_info tcp_rinfo = { 933 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 934 }; 935 936 static struct module_info tcp_winfo = { 937 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 938 }; 939 940 /* 941 * Entry points for TCP as a device. The normal case which supports 942 * the TCP functionality. 943 * We have separate open functions for the /dev/tcp and /dev/tcp6 devices. 944 */ 945 struct qinit tcp_rinitv4 = { 946 NULL, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, NULL, &tcp_rinfo 947 }; 948 949 struct qinit tcp_rinitv6 = { 950 NULL, (pfi_t)tcp_rsrv, tcp_openv6, tcp_tpi_close, NULL, &tcp_rinfo 951 }; 952 953 struct qinit tcp_winit = { 954 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 955 }; 956 957 /* Initial entry point for TCP in socket mode. */ 958 struct qinit tcp_sock_winit = { 959 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 960 }; 961 962 /* TCP entry point during fallback */ 963 struct qinit tcp_fallback_sock_winit = { 964 (pfi_t)tcp_wput_fallback, NULL, NULL, NULL, NULL, &tcp_winfo 965 }; 966 967 /* 968 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 969 * an accept. Avoid allocating data structures since eager has already 970 * been created. 971 */ 972 struct qinit tcp_acceptor_rinit = { 973 NULL, (pfi_t)tcp_rsrv, NULL, tcp_tpi_close_accept, NULL, &tcp_winfo 974 }; 975 976 struct qinit tcp_acceptor_winit = { 977 (pfi_t)tcp_tpi_accept, NULL, NULL, NULL, NULL, &tcp_winfo 978 }; 979 980 /* For AF_INET aka /dev/tcp */ 981 struct streamtab tcpinfov4 = { 982 &tcp_rinitv4, &tcp_winit 983 }; 984 985 /* For AF_INET6 aka /dev/tcp6 */ 986 struct streamtab tcpinfov6 = { 987 &tcp_rinitv6, &tcp_winit 988 }; 989 990 sock_downcalls_t sock_tcp_downcalls; 991 992 /* 993 * Have to ensure that tcp_g_q_close is not done by an 994 * interrupt thread. 995 */ 996 static taskq_t *tcp_taskq; 997 998 /* Setable only in /etc/system. Move to ndd? */ 999 boolean_t tcp_icmp_source_quench = B_FALSE; 1000 1001 /* 1002 * Following assumes TPI alignment requirements stay along 32 bit 1003 * boundaries 1004 */ 1005 #define ROUNDUP32(x) \ 1006 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1007 1008 /* Template for response to info request. */ 1009 static struct T_info_ack tcp_g_t_info_ack = { 1010 T_INFO_ACK, /* PRIM_type */ 1011 0, /* TSDU_size */ 1012 T_INFINITE, /* ETSDU_size */ 1013 T_INVALID, /* CDATA_size */ 1014 T_INVALID, /* DDATA_size */ 1015 sizeof (sin_t), /* ADDR_size */ 1016 0, /* OPT_size - not initialized here */ 1017 TIDUSZ, /* TIDU_size */ 1018 T_COTS_ORD, /* SERV_type */ 1019 TCPS_IDLE, /* CURRENT_state */ 1020 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1021 }; 1022 1023 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1024 T_INFO_ACK, /* PRIM_type */ 1025 0, /* TSDU_size */ 1026 T_INFINITE, /* ETSDU_size */ 1027 T_INVALID, /* CDATA_size */ 1028 T_INVALID, /* DDATA_size */ 1029 sizeof (sin6_t), /* ADDR_size */ 1030 0, /* OPT_size - not initialized here */ 1031 TIDUSZ, /* TIDU_size */ 1032 T_COTS_ORD, /* SERV_type */ 1033 TCPS_IDLE, /* CURRENT_state */ 1034 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1035 }; 1036 1037 #define MS 1L 1038 #define SECONDS (1000 * MS) 1039 #define MINUTES (60 * SECONDS) 1040 #define HOURS (60 * MINUTES) 1041 #define DAYS (24 * HOURS) 1042 1043 #define PARAM_MAX (~(uint32_t)0) 1044 1045 /* Max size IP datagram is 64k - 1 */ 1046 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1047 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1048 /* Max of the above */ 1049 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1050 1051 /* Largest TCP port number */ 1052 #define TCP_MAX_PORT (64 * 1024 - 1) 1053 1054 /* 1055 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1056 * layer header. It has to be a multiple of 4. 1057 */ 1058 static tcpparam_t lcl_tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1059 #define tcps_wroff_xtra tcps_wroff_xtra_param->tcp_param_val 1060 1061 /* 1062 * All of these are alterable, within the min/max values given, at run time. 1063 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1064 * per the TCP spec. 1065 */ 1066 /* BEGIN CSTYLED */ 1067 static tcpparam_t lcl_tcp_param_arr[] = { 1068 /*min max value name */ 1069 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1070 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1071 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1072 { 1, 1024, 1, "tcp_conn_req_min" }, 1073 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1074 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1075 { 0, 10, 0, "tcp_debug" }, 1076 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1077 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1078 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1079 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1080 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1081 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1082 { 1, 255, 64, "tcp_ipv4_ttl"}, 1083 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1084 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1085 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1086 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1087 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1088 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1089 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1090 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1091 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1092 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1093 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1094 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1095 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1096 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1097 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1098 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1099 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1100 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1101 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1102 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1103 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1104 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1105 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1106 /* 1107 * Question: What default value should I set for tcp_strong_iss? 1108 */ 1109 { 0, 2, 1, "tcp_strong_iss"}, 1110 { 0, 65536, 20, "tcp_rtt_updates"}, 1111 { 0, 1, 1, "tcp_wscale_always"}, 1112 { 0, 1, 0, "tcp_tstamp_always"}, 1113 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1114 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1115 { 0, 16, 2, "tcp_deferred_acks_max"}, 1116 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1117 { 1, 4, 4, "tcp_slow_start_initial"}, 1118 { 0, 2, 2, "tcp_sack_permitted"}, 1119 { 0, 1, 1, "tcp_compression_enabled"}, 1120 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1121 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1122 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1123 { 0, 1, 0, "tcp_rev_src_routes"}, 1124 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1125 { 0, 16, 8, "tcp_local_dacks_max"}, 1126 { 0, 2, 1, "tcp_ecn_permitted"}, 1127 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1128 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1129 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1130 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1131 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1132 }; 1133 /* END CSTYLED */ 1134 1135 /* 1136 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1137 * each header fragment in the header buffer. Each parameter value has 1138 * to be a multiple of 4 (32-bit aligned). 1139 */ 1140 static tcpparam_t lcl_tcp_mdt_head_param = 1141 { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1142 static tcpparam_t lcl_tcp_mdt_tail_param = 1143 { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1144 #define tcps_mdt_hdr_head_min tcps_mdt_head_param->tcp_param_val 1145 #define tcps_mdt_hdr_tail_min tcps_mdt_tail_param->tcp_param_val 1146 1147 /* 1148 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1149 * the maximum number of payload buffers associated per Multidata. 1150 */ 1151 static tcpparam_t lcl_tcp_mdt_max_pbufs_param = 1152 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1153 #define tcps_mdt_max_pbufs tcps_mdt_max_pbufs_param->tcp_param_val 1154 1155 /* Round up the value to the nearest mss. */ 1156 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1157 1158 /* 1159 * Set ECN capable transport (ECT) code point in IP header. 1160 * 1161 * Note that there are 2 ECT code points '01' and '10', which are called 1162 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1163 * point ECT(0) for TCP as described in RFC 2481. 1164 */ 1165 #define SET_ECT(tcp, iph) \ 1166 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1167 /* We need to clear the code point first. */ \ 1168 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1169 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1170 } else { \ 1171 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1172 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1173 } 1174 1175 /* 1176 * The format argument to pass to tcp_display(). 1177 * DISP_PORT_ONLY means that the returned string has only port info. 1178 * DISP_ADDR_AND_PORT means that the returned string also contains the 1179 * remote and local IP address. 1180 */ 1181 #define DISP_PORT_ONLY 1 1182 #define DISP_ADDR_AND_PORT 2 1183 1184 #define IS_VMLOANED_MBLK(mp) \ 1185 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1186 1187 1188 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1189 boolean_t tcp_mdt_chain = B_TRUE; 1190 1191 /* 1192 * MDT threshold in the form of effective send MSS multiplier; we take 1193 * the MDT path if the amount of unsent data exceeds the threshold value 1194 * (default threshold is 1*SMSS). 1195 */ 1196 uint_t tcp_mdt_smss_threshold = 1; 1197 1198 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1199 1200 /* 1201 * Forces all connections to obey the value of the tcps_maxpsz_multiplier 1202 * tunable settable via NDD. Otherwise, the per-connection behavior is 1203 * determined dynamically during tcp_adapt_ire(), which is the default. 1204 */ 1205 boolean_t tcp_static_maxpsz = B_FALSE; 1206 1207 /* Setable in /etc/system */ 1208 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1209 uint32_t tcp_random_anon_port = 1; 1210 1211 /* 1212 * To reach to an eager in Q0 which can be dropped due to an incoming 1213 * new SYN request when Q0 is full, a new doubly linked list is 1214 * introduced. This list allows to select an eager from Q0 in O(1) time. 1215 * This is needed to avoid spending too much time walking through the 1216 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1217 * this new list has to be a member of Q0. 1218 * This list is headed by listener's tcp_t. When the list is empty, 1219 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1220 * of listener's tcp_t point to listener's tcp_t itself. 1221 * 1222 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1223 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1224 * These macros do not affect the eager's membership to Q0. 1225 */ 1226 1227 1228 #define MAKE_DROPPABLE(listener, eager) \ 1229 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1230 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1231 = (eager); \ 1232 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1233 (eager)->tcp_eager_next_drop_q0 = \ 1234 (listener)->tcp_eager_next_drop_q0; \ 1235 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1236 } 1237 1238 #define MAKE_UNDROPPABLE(eager) \ 1239 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1240 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1241 = (eager)->tcp_eager_prev_drop_q0; \ 1242 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1243 = (eager)->tcp_eager_next_drop_q0; \ 1244 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1245 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1246 } 1247 1248 /* 1249 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1250 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1251 * data, TCP will not respond with an ACK. RFC 793 requires that 1252 * TCP responds with an ACK for such a bogus ACK. By not following 1253 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1254 * an attacker successfully spoofs an acceptable segment to our 1255 * peer; or when our peer is "confused." 1256 */ 1257 uint32_t tcp_drop_ack_unsent_cnt = 10; 1258 1259 /* 1260 * Hook functions to enable cluster networking 1261 * On non-clustered systems these vectors must always be NULL. 1262 */ 1263 1264 void (*cl_inet_listen)(netstackid_t stack_id, uint8_t protocol, 1265 sa_family_t addr_family, uint8_t *laddrp, 1266 in_port_t lport, void *args) = NULL; 1267 void (*cl_inet_unlisten)(netstackid_t stack_id, uint8_t protocol, 1268 sa_family_t addr_family, uint8_t *laddrp, 1269 in_port_t lport, void *args) = NULL; 1270 1271 int (*cl_inet_connect2)(netstackid_t stack_id, uint8_t protocol, 1272 boolean_t is_outgoing, 1273 sa_family_t addr_family, 1274 uint8_t *laddrp, in_port_t lport, 1275 uint8_t *faddrp, in_port_t fport, 1276 void *args) = NULL; 1277 1278 void (*cl_inet_disconnect)(netstackid_t stack_id, uint8_t protocol, 1279 sa_family_t addr_family, uint8_t *laddrp, 1280 in_port_t lport, uint8_t *faddrp, 1281 in_port_t fport, void *args) = NULL; 1282 1283 /* 1284 * The following are defined in ip.c 1285 */ 1286 extern int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol, 1287 sa_family_t addr_family, uint8_t *laddrp, 1288 void *args); 1289 extern uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol, 1290 sa_family_t addr_family, uint8_t *laddrp, 1291 uint8_t *faddrp, void *args); 1292 1293 1294 /* 1295 * int CL_INET_CONNECT(conn_t *cp, tcp_t *tcp, boolean_t is_outgoing, int err) 1296 */ 1297 #define CL_INET_CONNECT(connp, tcp, is_outgoing, err) { \ 1298 (err) = 0; \ 1299 if (cl_inet_connect2 != NULL) { \ 1300 /* \ 1301 * Running in cluster mode - register active connection \ 1302 * information \ 1303 */ \ 1304 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1305 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1306 (err) = (*cl_inet_connect2)( \ 1307 (connp)->conn_netstack->netstack_stackid,\ 1308 IPPROTO_TCP, is_outgoing, AF_INET, \ 1309 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1310 (in_port_t)(tcp)->tcp_lport, \ 1311 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1312 (in_port_t)(tcp)->tcp_fport, NULL); \ 1313 } \ 1314 } else { \ 1315 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1316 &(tcp)->tcp_ip6h->ip6_src)) { \ 1317 (err) = (*cl_inet_connect2)( \ 1318 (connp)->conn_netstack->netstack_stackid,\ 1319 IPPROTO_TCP, is_outgoing, AF_INET6, \ 1320 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1321 (in_port_t)(tcp)->tcp_lport, \ 1322 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1323 (in_port_t)(tcp)->tcp_fport, NULL); \ 1324 } \ 1325 } \ 1326 } \ 1327 } 1328 1329 #define CL_INET_DISCONNECT(connp, tcp) { \ 1330 if (cl_inet_disconnect != NULL) { \ 1331 /* \ 1332 * Running in cluster mode - deregister active \ 1333 * connection information \ 1334 */ \ 1335 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1336 if ((tcp)->tcp_ip_src != 0) { \ 1337 (*cl_inet_disconnect)( \ 1338 (connp)->conn_netstack->netstack_stackid,\ 1339 IPPROTO_TCP, AF_INET, \ 1340 (uint8_t *)(&((tcp)->tcp_ip_src)), \ 1341 (in_port_t)(tcp)->tcp_lport, \ 1342 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1343 (in_port_t)(tcp)->tcp_fport, NULL); \ 1344 } \ 1345 } else { \ 1346 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1347 &(tcp)->tcp_ip_src_v6)) { \ 1348 (*cl_inet_disconnect)( \ 1349 (connp)->conn_netstack->netstack_stackid,\ 1350 IPPROTO_TCP, AF_INET6, \ 1351 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1352 (in_port_t)(tcp)->tcp_lport, \ 1353 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1354 (in_port_t)(tcp)->tcp_fport, NULL); \ 1355 } \ 1356 } \ 1357 } \ 1358 } 1359 1360 /* 1361 * Cluster networking hook for traversing current connection list. 1362 * This routine is used to extract the current list of live connections 1363 * which must continue to to be dispatched to this node. 1364 */ 1365 int cl_tcp_walk_list(netstackid_t stack_id, 1366 int (*callback)(cl_tcp_info_t *, void *), void *arg); 1367 1368 static int cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), 1369 void *arg, tcp_stack_t *tcps); 1370 1371 #define DTRACE_IP_FASTPATH(mp, iph, ill, ipha, ip6h) \ 1372 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL, void_ip_t *, \ 1373 iph, __dtrace_ipsr_ill_t *, ill, ipha_t *, ipha, \ 1374 ip6_t *, ip6h, int, 0); 1375 1376 /* 1377 * Figure out the value of window scale opton. Note that the rwnd is 1378 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1379 * We cannot find the scale value and then do a round up of tcp_rwnd 1380 * because the scale value may not be correct after that. 1381 * 1382 * Set the compiler flag to make this function inline. 1383 */ 1384 static void 1385 tcp_set_ws_value(tcp_t *tcp) 1386 { 1387 int i; 1388 uint32_t rwnd = tcp->tcp_rwnd; 1389 1390 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1391 i++, rwnd >>= 1) 1392 ; 1393 tcp->tcp_rcv_ws = i; 1394 } 1395 1396 /* 1397 * Remove a connection from the list of detached TIME_WAIT connections. 1398 * It returns B_FALSE if it can't remove the connection from the list 1399 * as the connection has already been removed from the list due to an 1400 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1401 */ 1402 static boolean_t 1403 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1404 { 1405 boolean_t locked = B_FALSE; 1406 1407 if (tcp_time_wait == NULL) { 1408 tcp_time_wait = *((tcp_squeue_priv_t **) 1409 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1410 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1411 locked = B_TRUE; 1412 } else { 1413 ASSERT(MUTEX_HELD(&tcp_time_wait->tcp_time_wait_lock)); 1414 } 1415 1416 if (tcp->tcp_time_wait_expire == 0) { 1417 ASSERT(tcp->tcp_time_wait_next == NULL); 1418 ASSERT(tcp->tcp_time_wait_prev == NULL); 1419 if (locked) 1420 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1421 return (B_FALSE); 1422 } 1423 ASSERT(TCP_IS_DETACHED(tcp)); 1424 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1425 1426 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1427 ASSERT(tcp->tcp_time_wait_prev == NULL); 1428 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1429 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1430 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1431 NULL; 1432 } else { 1433 tcp_time_wait->tcp_time_wait_tail = NULL; 1434 } 1435 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1436 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1437 ASSERT(tcp->tcp_time_wait_next == NULL); 1438 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1439 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1440 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1441 } else { 1442 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1443 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1444 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1445 tcp->tcp_time_wait_next; 1446 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1447 tcp->tcp_time_wait_prev; 1448 } 1449 tcp->tcp_time_wait_next = NULL; 1450 tcp->tcp_time_wait_prev = NULL; 1451 tcp->tcp_time_wait_expire = 0; 1452 1453 if (locked) 1454 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1455 return (B_TRUE); 1456 } 1457 1458 /* 1459 * Add a connection to the list of detached TIME_WAIT connections 1460 * and set its time to expire. 1461 */ 1462 static void 1463 tcp_time_wait_append(tcp_t *tcp) 1464 { 1465 tcp_stack_t *tcps = tcp->tcp_tcps; 1466 tcp_squeue_priv_t *tcp_time_wait = 1467 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1468 SQPRIVATE_TCP)); 1469 1470 tcp_timers_stop(tcp); 1471 1472 /* Freed above */ 1473 ASSERT(tcp->tcp_timer_tid == 0); 1474 ASSERT(tcp->tcp_ack_tid == 0); 1475 1476 /* must have happened at the time of detaching the tcp */ 1477 ASSERT(tcp->tcp_ptpahn == NULL); 1478 ASSERT(tcp->tcp_flow_stopped == 0); 1479 ASSERT(tcp->tcp_time_wait_next == NULL); 1480 ASSERT(tcp->tcp_time_wait_prev == NULL); 1481 ASSERT(tcp->tcp_time_wait_expire == NULL); 1482 ASSERT(tcp->tcp_listener == NULL); 1483 1484 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1485 /* 1486 * The value computed below in tcp->tcp_time_wait_expire may 1487 * appear negative or wrap around. That is ok since our 1488 * interest is only in the difference between the current lbolt 1489 * value and tcp->tcp_time_wait_expire. But the value should not 1490 * be zero, since it means the tcp is not in the TIME_WAIT list. 1491 * The corresponding comparison in tcp_time_wait_collector() uses 1492 * modular arithmetic. 1493 */ 1494 tcp->tcp_time_wait_expire += 1495 drv_usectohz(tcps->tcps_time_wait_interval * 1000); 1496 if (tcp->tcp_time_wait_expire == 0) 1497 tcp->tcp_time_wait_expire = 1; 1498 1499 ASSERT(TCP_IS_DETACHED(tcp)); 1500 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1501 ASSERT(tcp->tcp_time_wait_next == NULL); 1502 ASSERT(tcp->tcp_time_wait_prev == NULL); 1503 TCP_DBGSTAT(tcps, tcp_time_wait); 1504 1505 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1506 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1507 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1508 tcp_time_wait->tcp_time_wait_head = tcp; 1509 } else { 1510 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1511 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1512 TCPS_TIME_WAIT); 1513 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1514 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1515 } 1516 tcp_time_wait->tcp_time_wait_tail = tcp; 1517 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1518 } 1519 1520 /* ARGSUSED */ 1521 void 1522 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1523 { 1524 conn_t *connp = (conn_t *)arg; 1525 tcp_t *tcp = connp->conn_tcp; 1526 tcp_stack_t *tcps = tcp->tcp_tcps; 1527 1528 ASSERT(tcp != NULL); 1529 if (tcp->tcp_state == TCPS_CLOSED) { 1530 return; 1531 } 1532 1533 ASSERT((tcp->tcp_family == AF_INET && 1534 tcp->tcp_ipversion == IPV4_VERSION) || 1535 (tcp->tcp_family == AF_INET6 && 1536 (tcp->tcp_ipversion == IPV4_VERSION || 1537 tcp->tcp_ipversion == IPV6_VERSION))); 1538 ASSERT(!tcp->tcp_listener); 1539 1540 TCP_STAT(tcps, tcp_time_wait_reap); 1541 ASSERT(TCP_IS_DETACHED(tcp)); 1542 1543 /* 1544 * Because they have no upstream client to rebind or tcp_close() 1545 * them later, we axe the connection here and now. 1546 */ 1547 tcp_close_detached(tcp); 1548 } 1549 1550 /* 1551 * Remove cached/latched IPsec references. 1552 */ 1553 void 1554 tcp_ipsec_cleanup(tcp_t *tcp) 1555 { 1556 conn_t *connp = tcp->tcp_connp; 1557 1558 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1559 1560 if (connp->conn_latch != NULL) { 1561 IPLATCH_REFRELE(connp->conn_latch, 1562 connp->conn_netstack); 1563 connp->conn_latch = NULL; 1564 } 1565 if (connp->conn_policy != NULL) { 1566 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 1567 connp->conn_policy = NULL; 1568 } 1569 } 1570 1571 /* 1572 * Cleaup before placing on free list. 1573 * Disassociate from the netstack/tcp_stack_t since the freelist 1574 * is per squeue and not per netstack. 1575 */ 1576 void 1577 tcp_cleanup(tcp_t *tcp) 1578 { 1579 mblk_t *mp; 1580 char *tcp_iphc; 1581 int tcp_iphc_len; 1582 int tcp_hdr_grown; 1583 tcp_sack_info_t *tcp_sack_info; 1584 conn_t *connp = tcp->tcp_connp; 1585 tcp_stack_t *tcps = tcp->tcp_tcps; 1586 netstack_t *ns = tcps->tcps_netstack; 1587 mblk_t *tcp_rsrv_mp; 1588 1589 tcp_bind_hash_remove(tcp); 1590 1591 /* Cleanup that which needs the netstack first */ 1592 tcp_ipsec_cleanup(tcp); 1593 1594 tcp_free(tcp); 1595 1596 /* Release any SSL context */ 1597 if (tcp->tcp_kssl_ent != NULL) { 1598 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1599 tcp->tcp_kssl_ent = NULL; 1600 } 1601 1602 if (tcp->tcp_kssl_ctx != NULL) { 1603 kssl_release_ctx(tcp->tcp_kssl_ctx); 1604 tcp->tcp_kssl_ctx = NULL; 1605 } 1606 tcp->tcp_kssl_pending = B_FALSE; 1607 1608 conn_delete_ire(connp, NULL); 1609 1610 /* 1611 * Since we will bzero the entire structure, we need to 1612 * remove it and reinsert it in global hash list. We 1613 * know the walkers can't get to this conn because we 1614 * had set CONDEMNED flag earlier and checked reference 1615 * under conn_lock so walker won't pick it and when we 1616 * go the ipcl_globalhash_remove() below, no walker 1617 * can get to it. 1618 */ 1619 ipcl_globalhash_remove(connp); 1620 1621 /* 1622 * Now it is safe to decrement the reference counts. 1623 * This might be the last reference on the netstack and TCPS 1624 * in which case it will cause the tcp_g_q_close and 1625 * the freeing of the IP Instance. 1626 */ 1627 connp->conn_netstack = NULL; 1628 netstack_rele(ns); 1629 ASSERT(tcps != NULL); 1630 tcp->tcp_tcps = NULL; 1631 TCPS_REFRELE(tcps); 1632 1633 /* Save some state */ 1634 mp = tcp->tcp_timercache; 1635 1636 tcp_sack_info = tcp->tcp_sack_info; 1637 tcp_iphc = tcp->tcp_iphc; 1638 tcp_iphc_len = tcp->tcp_iphc_len; 1639 tcp_hdr_grown = tcp->tcp_hdr_grown; 1640 tcp_rsrv_mp = tcp->tcp_rsrv_mp; 1641 1642 if (connp->conn_cred != NULL) { 1643 crfree(connp->conn_cred); 1644 connp->conn_cred = NULL; 1645 } 1646 if (connp->conn_effective_cred != NULL) { 1647 crfree(connp->conn_effective_cred); 1648 connp->conn_effective_cred = NULL; 1649 } 1650 ipcl_conn_cleanup(connp); 1651 connp->conn_flags = IPCL_TCPCONN; 1652 bzero(tcp, sizeof (tcp_t)); 1653 1654 /* restore the state */ 1655 tcp->tcp_timercache = mp; 1656 1657 tcp->tcp_sack_info = tcp_sack_info; 1658 tcp->tcp_iphc = tcp_iphc; 1659 tcp->tcp_iphc_len = tcp_iphc_len; 1660 tcp->tcp_hdr_grown = tcp_hdr_grown; 1661 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 1662 1663 tcp->tcp_connp = connp; 1664 1665 ASSERT(connp->conn_tcp == tcp); 1666 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1667 connp->conn_state_flags = CONN_INCIPIENT; 1668 ASSERT(connp->conn_ulp == IPPROTO_TCP); 1669 ASSERT(connp->conn_ref == 1); 1670 } 1671 1672 /* 1673 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1674 * is done forwards from the head. 1675 * This walks all stack instances since 1676 * tcp_time_wait remains global across all stacks. 1677 */ 1678 /* ARGSUSED */ 1679 void 1680 tcp_time_wait_collector(void *arg) 1681 { 1682 tcp_t *tcp; 1683 clock_t now; 1684 mblk_t *mp; 1685 conn_t *connp; 1686 kmutex_t *lock; 1687 boolean_t removed; 1688 1689 squeue_t *sqp = (squeue_t *)arg; 1690 tcp_squeue_priv_t *tcp_time_wait = 1691 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1692 1693 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1694 tcp_time_wait->tcp_time_wait_tid = 0; 1695 1696 if (tcp_time_wait->tcp_free_list != NULL && 1697 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1698 TCP_G_STAT(tcp_freelist_cleanup); 1699 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1700 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1701 tcp->tcp_time_wait_next = NULL; 1702 tcp_time_wait->tcp_free_list_cnt--; 1703 ASSERT(tcp->tcp_tcps == NULL); 1704 CONN_DEC_REF(tcp->tcp_connp); 1705 } 1706 ASSERT(tcp_time_wait->tcp_free_list_cnt == 0); 1707 } 1708 1709 /* 1710 * In order to reap time waits reliably, we should use a 1711 * source of time that is not adjustable by the user -- hence 1712 * the call to ddi_get_lbolt(). 1713 */ 1714 now = ddi_get_lbolt(); 1715 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1716 /* 1717 * Compare times using modular arithmetic, since 1718 * lbolt can wrapover. 1719 */ 1720 if ((now - tcp->tcp_time_wait_expire) < 0) { 1721 break; 1722 } 1723 1724 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1725 ASSERT(removed); 1726 1727 connp = tcp->tcp_connp; 1728 ASSERT(connp->conn_fanout != NULL); 1729 lock = &connp->conn_fanout->connf_lock; 1730 /* 1731 * This is essentially a TW reclaim fast path optimization for 1732 * performance where the timewait collector checks under the 1733 * fanout lock (so that no one else can get access to the 1734 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1735 * the classifier hash list. If ref count is indeed 2, we can 1736 * just remove the conn under the fanout lock and avoid 1737 * cleaning up the conn under the squeue, provided that 1738 * clustering callbacks are not enabled. If clustering is 1739 * enabled, we need to make the clustering callback before 1740 * setting the CONDEMNED flag and after dropping all locks and 1741 * so we forego this optimization and fall back to the slow 1742 * path. Also please see the comments in tcp_closei_local 1743 * regarding the refcnt logic. 1744 * 1745 * Since we are holding the tcp_time_wait_lock, its better 1746 * not to block on the fanout_lock because other connections 1747 * can't add themselves to time_wait list. So we do a 1748 * tryenter instead of mutex_enter. 1749 */ 1750 if (mutex_tryenter(lock)) { 1751 mutex_enter(&connp->conn_lock); 1752 if ((connp->conn_ref == 2) && 1753 (cl_inet_disconnect == NULL)) { 1754 ipcl_hash_remove_locked(connp, 1755 connp->conn_fanout); 1756 /* 1757 * Set the CONDEMNED flag now itself so that 1758 * the refcnt cannot increase due to any 1759 * walker. But we have still not cleaned up 1760 * conn_ire_cache. This is still ok since 1761 * we are going to clean it up in tcp_cleanup 1762 * immediately and any interface unplumb 1763 * thread will wait till the ire is blown away 1764 */ 1765 connp->conn_state_flags |= CONN_CONDEMNED; 1766 mutex_exit(lock); 1767 mutex_exit(&connp->conn_lock); 1768 if (tcp_time_wait->tcp_free_list_cnt < 1769 tcp_free_list_max_cnt) { 1770 /* Add to head of tcp_free_list */ 1771 mutex_exit( 1772 &tcp_time_wait->tcp_time_wait_lock); 1773 tcp_cleanup(tcp); 1774 ASSERT(connp->conn_latch == NULL); 1775 ASSERT(connp->conn_policy == NULL); 1776 ASSERT(tcp->tcp_tcps == NULL); 1777 ASSERT(connp->conn_netstack == NULL); 1778 1779 mutex_enter( 1780 &tcp_time_wait->tcp_time_wait_lock); 1781 tcp->tcp_time_wait_next = 1782 tcp_time_wait->tcp_free_list; 1783 tcp_time_wait->tcp_free_list = tcp; 1784 tcp_time_wait->tcp_free_list_cnt++; 1785 continue; 1786 } else { 1787 /* Do not add to tcp_free_list */ 1788 mutex_exit( 1789 &tcp_time_wait->tcp_time_wait_lock); 1790 tcp_bind_hash_remove(tcp); 1791 conn_delete_ire(tcp->tcp_connp, NULL); 1792 tcp_ipsec_cleanup(tcp); 1793 CONN_DEC_REF(tcp->tcp_connp); 1794 } 1795 } else { 1796 CONN_INC_REF_LOCKED(connp); 1797 mutex_exit(lock); 1798 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1799 mutex_exit(&connp->conn_lock); 1800 /* 1801 * We can reuse the closemp here since conn has 1802 * detached (otherwise we wouldn't even be in 1803 * time_wait list). tcp_closemp_used can safely 1804 * be changed without taking a lock as no other 1805 * thread can concurrently access it at this 1806 * point in the connection lifecycle. 1807 */ 1808 1809 if (tcp->tcp_closemp.b_prev == NULL) 1810 tcp->tcp_closemp_used = B_TRUE; 1811 else 1812 cmn_err(CE_PANIC, 1813 "tcp_timewait_collector: " 1814 "concurrent use of tcp_closemp: " 1815 "connp %p tcp %p\n", (void *)connp, 1816 (void *)tcp); 1817 1818 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1819 mp = &tcp->tcp_closemp; 1820 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 1821 tcp_timewait_output, connp, 1822 SQ_FILL, SQTAG_TCP_TIMEWAIT); 1823 } 1824 } else { 1825 mutex_enter(&connp->conn_lock); 1826 CONN_INC_REF_LOCKED(connp); 1827 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1828 mutex_exit(&connp->conn_lock); 1829 /* 1830 * We can reuse the closemp here since conn has 1831 * detached (otherwise we wouldn't even be in 1832 * time_wait list). tcp_closemp_used can safely 1833 * be changed without taking a lock as no other 1834 * thread can concurrently access it at this 1835 * point in the connection lifecycle. 1836 */ 1837 1838 if (tcp->tcp_closemp.b_prev == NULL) 1839 tcp->tcp_closemp_used = B_TRUE; 1840 else 1841 cmn_err(CE_PANIC, "tcp_timewait_collector: " 1842 "concurrent use of tcp_closemp: " 1843 "connp %p tcp %p\n", (void *)connp, 1844 (void *)tcp); 1845 1846 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1847 mp = &tcp->tcp_closemp; 1848 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 1849 tcp_timewait_output, connp, 1850 SQ_FILL, SQTAG_TCP_TIMEWAIT); 1851 } 1852 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1853 } 1854 1855 if (tcp_time_wait->tcp_free_list != NULL) 1856 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1857 1858 tcp_time_wait->tcp_time_wait_tid = 1859 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 1860 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 1861 CALLOUT_FLAG_ROUNDUP); 1862 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1863 } 1864 1865 /* 1866 * Reply to a clients T_CONN_RES TPI message. This function 1867 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1868 * on the acceptor STREAM and processed in tcp_wput_accept(). 1869 * Read the block comment on top of tcp_conn_request(). 1870 */ 1871 static void 1872 tcp_tli_accept(tcp_t *listener, mblk_t *mp) 1873 { 1874 tcp_t *acceptor; 1875 tcp_t *eager; 1876 tcp_t *tcp; 1877 struct T_conn_res *tcr; 1878 t_uscalar_t acceptor_id; 1879 t_scalar_t seqnum; 1880 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1881 struct tcp_options *tcpopt; 1882 mblk_t *ok_mp; 1883 mblk_t *mp1; 1884 tcp_stack_t *tcps = listener->tcp_tcps; 1885 int error; 1886 1887 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1888 tcp_err_ack(listener, mp, TPROTO, 0); 1889 return; 1890 } 1891 tcr = (struct T_conn_res *)mp->b_rptr; 1892 1893 /* 1894 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1895 * read side queue of the streams device underneath us i.e. the 1896 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1897 * look it up in the queue_hash. Under LP64 it sends down the 1898 * minor_t of the accepting endpoint. 1899 * 1900 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1901 * fanout hash lock is held. 1902 * This prevents any thread from entering the acceptor queue from 1903 * below (since it has not been hard bound yet i.e. any inbound 1904 * packets will arrive on the listener or default tcp queue and 1905 * go through tcp_lookup). 1906 * The CONN_INC_REF will prevent the acceptor from closing. 1907 * 1908 * XXX It is still possible for a tli application to send down data 1909 * on the accepting stream while another thread calls t_accept. 1910 * This should not be a problem for well-behaved applications since 1911 * the T_OK_ACK is sent after the queue swapping is completed. 1912 * 1913 * If the accepting fd is the same as the listening fd, avoid 1914 * queue hash lookup since that will return an eager listener in a 1915 * already established state. 1916 */ 1917 acceptor_id = tcr->ACCEPTOR_id; 1918 mutex_enter(&listener->tcp_eager_lock); 1919 if (listener->tcp_acceptor_id == acceptor_id) { 1920 eager = listener->tcp_eager_next_q; 1921 /* only count how many T_CONN_INDs so don't count q0 */ 1922 if ((listener->tcp_conn_req_cnt_q != 1) || 1923 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 1924 mutex_exit(&listener->tcp_eager_lock); 1925 tcp_err_ack(listener, mp, TBADF, 0); 1926 return; 1927 } 1928 if (listener->tcp_conn_req_cnt_q0 != 0) { 1929 /* Throw away all the eagers on q0. */ 1930 tcp_eager_cleanup(listener, 1); 1931 } 1932 if (listener->tcp_syn_defense) { 1933 listener->tcp_syn_defense = B_FALSE; 1934 if (listener->tcp_ip_addr_cache != NULL) { 1935 kmem_free(listener->tcp_ip_addr_cache, 1936 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 1937 listener->tcp_ip_addr_cache = NULL; 1938 } 1939 } 1940 /* 1941 * Transfer tcp_conn_req_max to the eager so that when 1942 * a disconnect occurs we can revert the endpoint to the 1943 * listen state. 1944 */ 1945 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 1946 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 1947 /* 1948 * Get a reference on the acceptor just like the 1949 * tcp_acceptor_hash_lookup below. 1950 */ 1951 acceptor = listener; 1952 CONN_INC_REF(acceptor->tcp_connp); 1953 } else { 1954 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 1955 if (acceptor == NULL) { 1956 if (listener->tcp_debug) { 1957 (void) strlog(TCP_MOD_ID, 0, 1, 1958 SL_ERROR|SL_TRACE, 1959 "tcp_accept: did not find acceptor 0x%x\n", 1960 acceptor_id); 1961 } 1962 mutex_exit(&listener->tcp_eager_lock); 1963 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 1964 return; 1965 } 1966 /* 1967 * Verify acceptor state. The acceptable states for an acceptor 1968 * include TCPS_IDLE and TCPS_BOUND. 1969 */ 1970 switch (acceptor->tcp_state) { 1971 case TCPS_IDLE: 1972 /* FALLTHRU */ 1973 case TCPS_BOUND: 1974 break; 1975 default: 1976 CONN_DEC_REF(acceptor->tcp_connp); 1977 mutex_exit(&listener->tcp_eager_lock); 1978 tcp_err_ack(listener, mp, TOUTSTATE, 0); 1979 return; 1980 } 1981 } 1982 1983 /* The listener must be in TCPS_LISTEN */ 1984 if (listener->tcp_state != TCPS_LISTEN) { 1985 CONN_DEC_REF(acceptor->tcp_connp); 1986 mutex_exit(&listener->tcp_eager_lock); 1987 tcp_err_ack(listener, mp, TOUTSTATE, 0); 1988 return; 1989 } 1990 1991 /* 1992 * Rendezvous with an eager connection request packet hanging off 1993 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 1994 * tcp structure when the connection packet arrived in 1995 * tcp_conn_request(). 1996 */ 1997 seqnum = tcr->SEQ_number; 1998 eager = listener; 1999 do { 2000 eager = eager->tcp_eager_next_q; 2001 if (eager == NULL) { 2002 CONN_DEC_REF(acceptor->tcp_connp); 2003 mutex_exit(&listener->tcp_eager_lock); 2004 tcp_err_ack(listener, mp, TBADSEQ, 0); 2005 return; 2006 } 2007 } while (eager->tcp_conn_req_seqnum != seqnum); 2008 mutex_exit(&listener->tcp_eager_lock); 2009 2010 /* 2011 * At this point, both acceptor and listener have 2 ref 2012 * that they begin with. Acceptor has one additional ref 2013 * we placed in lookup while listener has 3 additional 2014 * ref for being behind the squeue (tcp_accept() is 2015 * done on listener's squeue); being in classifier hash; 2016 * and eager's ref on listener. 2017 */ 2018 ASSERT(listener->tcp_connp->conn_ref >= 5); 2019 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2020 2021 /* 2022 * The eager at this point is set in its own squeue and 2023 * could easily have been killed (tcp_accept_finish will 2024 * deal with that) because of a TH_RST so we can only 2025 * ASSERT for a single ref. 2026 */ 2027 ASSERT(eager->tcp_connp->conn_ref >= 1); 2028 2029 /* Pre allocate the stroptions mblk also */ 2030 opt_mp = allocb(MAX(sizeof (struct tcp_options), 2031 sizeof (struct T_conn_res)), BPRI_HI); 2032 if (opt_mp == NULL) { 2033 CONN_DEC_REF(acceptor->tcp_connp); 2034 CONN_DEC_REF(eager->tcp_connp); 2035 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2036 return; 2037 } 2038 DB_TYPE(opt_mp) = M_SETOPTS; 2039 opt_mp->b_wptr += sizeof (struct tcp_options); 2040 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 2041 tcpopt->to_flags = 0; 2042 2043 /* 2044 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2045 * from listener to acceptor. 2046 */ 2047 if (listener->tcp_bound_if != 0) { 2048 tcpopt->to_flags |= TCPOPT_BOUNDIF; 2049 tcpopt->to_boundif = listener->tcp_bound_if; 2050 } 2051 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2052 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 2053 } 2054 2055 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2056 if ((mp1 = copymsg(mp)) == NULL) { 2057 CONN_DEC_REF(acceptor->tcp_connp); 2058 CONN_DEC_REF(eager->tcp_connp); 2059 freemsg(opt_mp); 2060 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2061 return; 2062 } 2063 2064 tcr = (struct T_conn_res *)mp1->b_rptr; 2065 2066 /* 2067 * This is an expanded version of mi_tpi_ok_ack_alloc() 2068 * which allocates a larger mblk and appends the new 2069 * local address to the ok_ack. The address is copied by 2070 * soaccept() for getsockname(). 2071 */ 2072 { 2073 int extra; 2074 2075 extra = (eager->tcp_family == AF_INET) ? 2076 sizeof (sin_t) : sizeof (sin6_t); 2077 2078 /* 2079 * Try to re-use mp, if possible. Otherwise, allocate 2080 * an mblk and return it as ok_mp. In any case, mp 2081 * is no longer usable upon return. 2082 */ 2083 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2084 CONN_DEC_REF(acceptor->tcp_connp); 2085 CONN_DEC_REF(eager->tcp_connp); 2086 freemsg(opt_mp); 2087 /* Original mp has been freed by now, so use mp1 */ 2088 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2089 return; 2090 } 2091 2092 mp = NULL; /* We should never use mp after this point */ 2093 2094 switch (extra) { 2095 case sizeof (sin_t): { 2096 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2097 2098 ok_mp->b_wptr += extra; 2099 sin->sin_family = AF_INET; 2100 sin->sin_port = eager->tcp_lport; 2101 sin->sin_addr.s_addr = 2102 eager->tcp_ipha->ipha_src; 2103 break; 2104 } 2105 case sizeof (sin6_t): { 2106 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2107 2108 ok_mp->b_wptr += extra; 2109 sin6->sin6_family = AF_INET6; 2110 sin6->sin6_port = eager->tcp_lport; 2111 if (eager->tcp_ipversion == IPV4_VERSION) { 2112 sin6->sin6_flowinfo = 0; 2113 IN6_IPADDR_TO_V4MAPPED( 2114 eager->tcp_ipha->ipha_src, 2115 &sin6->sin6_addr); 2116 } else { 2117 ASSERT(eager->tcp_ip6h != NULL); 2118 sin6->sin6_flowinfo = 2119 eager->tcp_ip6h->ip6_vcf & 2120 ~IPV6_VERS_AND_FLOW_MASK; 2121 sin6->sin6_addr = 2122 eager->tcp_ip6h->ip6_src; 2123 } 2124 sin6->sin6_scope_id = 0; 2125 sin6->__sin6_src_id = 0; 2126 break; 2127 } 2128 default: 2129 break; 2130 } 2131 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2132 } 2133 2134 /* 2135 * If there are no options we know that the T_CONN_RES will 2136 * succeed. However, we can't send the T_OK_ACK upstream until 2137 * the tcp_accept_swap is done since it would be dangerous to 2138 * let the application start using the new fd prior to the swap. 2139 */ 2140 error = tcp_accept_swap(listener, acceptor, eager); 2141 if (error != 0) { 2142 CONN_DEC_REF(acceptor->tcp_connp); 2143 CONN_DEC_REF(eager->tcp_connp); 2144 freemsg(ok_mp); 2145 /* Original mp has been freed by now, so use mp1 */ 2146 tcp_err_ack(listener, mp1, TSYSERR, error); 2147 return; 2148 } 2149 2150 /* 2151 * tcp_accept_swap unlinks eager from listener but does not drop 2152 * the eager's reference on the listener. 2153 */ 2154 ASSERT(eager->tcp_listener == NULL); 2155 ASSERT(listener->tcp_connp->conn_ref >= 5); 2156 2157 /* 2158 * The eager is now associated with its own queue. Insert in 2159 * the hash so that the connection can be reused for a future 2160 * T_CONN_RES. 2161 */ 2162 tcp_acceptor_hash_insert(acceptor_id, eager); 2163 2164 /* 2165 * We now do the processing of options with T_CONN_RES. 2166 * We delay till now since we wanted to have queue to pass to 2167 * option processing routines that points back to the right 2168 * instance structure which does not happen until after 2169 * tcp_accept_swap(). 2170 * 2171 * Note: 2172 * The sanity of the logic here assumes that whatever options 2173 * are appropriate to inherit from listner=>eager are done 2174 * before this point, and whatever were to be overridden (or not) 2175 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2176 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2177 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2178 * This may not be true at this point in time but can be fixed 2179 * independently. This option processing code starts with 2180 * the instantiated acceptor instance and the final queue at 2181 * this point. 2182 */ 2183 2184 if (tcr->OPT_length != 0) { 2185 /* Options to process */ 2186 int t_error = 0; 2187 int sys_error = 0; 2188 int do_disconnect = 0; 2189 2190 if (tcp_conprim_opt_process(eager, mp1, 2191 &do_disconnect, &t_error, &sys_error) < 0) { 2192 eager->tcp_accept_error = 1; 2193 if (do_disconnect) { 2194 /* 2195 * An option failed which does not allow 2196 * connection to be accepted. 2197 * 2198 * We allow T_CONN_RES to succeed and 2199 * put a T_DISCON_IND on the eager queue. 2200 */ 2201 ASSERT(t_error == 0 && sys_error == 0); 2202 eager->tcp_send_discon_ind = 1; 2203 } else { 2204 ASSERT(t_error != 0); 2205 freemsg(ok_mp); 2206 /* 2207 * Original mp was either freed or set 2208 * to ok_mp above, so use mp1 instead. 2209 */ 2210 tcp_err_ack(listener, mp1, t_error, sys_error); 2211 goto finish; 2212 } 2213 } 2214 /* 2215 * Most likely success in setting options (except if 2216 * eager->tcp_send_discon_ind set). 2217 * mp1 option buffer represented by OPT_length/offset 2218 * potentially modified and contains results of setting 2219 * options at this point 2220 */ 2221 } 2222 2223 /* We no longer need mp1, since all options processing has passed */ 2224 freemsg(mp1); 2225 2226 putnext(listener->tcp_rq, ok_mp); 2227 2228 mutex_enter(&listener->tcp_eager_lock); 2229 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2230 tcp_t *tail; 2231 mblk_t *conn_ind; 2232 2233 /* 2234 * This path should not be executed if listener and 2235 * acceptor streams are the same. 2236 */ 2237 ASSERT(listener != acceptor); 2238 2239 tcp = listener->tcp_eager_prev_q0; 2240 /* 2241 * listener->tcp_eager_prev_q0 points to the TAIL of the 2242 * deferred T_conn_ind queue. We need to get to the head of 2243 * the queue in order to send up T_conn_ind the same order as 2244 * how the 3WHS is completed. 2245 */ 2246 while (tcp != listener) { 2247 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2248 break; 2249 else 2250 tcp = tcp->tcp_eager_prev_q0; 2251 } 2252 ASSERT(tcp != listener); 2253 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2254 ASSERT(conn_ind != NULL); 2255 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2256 2257 /* Move from q0 to q */ 2258 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2259 listener->tcp_conn_req_cnt_q0--; 2260 listener->tcp_conn_req_cnt_q++; 2261 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2262 tcp->tcp_eager_prev_q0; 2263 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2264 tcp->tcp_eager_next_q0; 2265 tcp->tcp_eager_prev_q0 = NULL; 2266 tcp->tcp_eager_next_q0 = NULL; 2267 tcp->tcp_conn_def_q0 = B_FALSE; 2268 2269 /* Make sure the tcp isn't in the list of droppables */ 2270 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2271 tcp->tcp_eager_prev_drop_q0 == NULL); 2272 2273 /* 2274 * Insert at end of the queue because sockfs sends 2275 * down T_CONN_RES in chronological order. Leaving 2276 * the older conn indications at front of the queue 2277 * helps reducing search time. 2278 */ 2279 tail = listener->tcp_eager_last_q; 2280 if (tail != NULL) 2281 tail->tcp_eager_next_q = tcp; 2282 else 2283 listener->tcp_eager_next_q = tcp; 2284 listener->tcp_eager_last_q = tcp; 2285 tcp->tcp_eager_next_q = NULL; 2286 mutex_exit(&listener->tcp_eager_lock); 2287 putnext(tcp->tcp_rq, conn_ind); 2288 } else { 2289 mutex_exit(&listener->tcp_eager_lock); 2290 } 2291 2292 /* 2293 * Done with the acceptor - free it 2294 * 2295 * Note: from this point on, no access to listener should be made 2296 * as listener can be equal to acceptor. 2297 */ 2298 finish: 2299 ASSERT(acceptor->tcp_detached); 2300 ASSERT(tcps->tcps_g_q != NULL); 2301 ASSERT(!IPCL_IS_NONSTR(acceptor->tcp_connp)); 2302 acceptor->tcp_rq = tcps->tcps_g_q; 2303 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2304 (void) tcp_clean_death(acceptor, 0, 2); 2305 CONN_DEC_REF(acceptor->tcp_connp); 2306 2307 /* 2308 * In case we already received a FIN we have to make tcp_rput send 2309 * the ordrel_ind. This will also send up a window update if the window 2310 * has opened up. 2311 * 2312 * In the normal case of a successful connection acceptance 2313 * we give the O_T_BIND_REQ to the read side put procedure as an 2314 * indication that this was just accepted. This tells tcp_rput to 2315 * pass up any data queued in tcp_rcv_list. 2316 * 2317 * In the fringe case where options sent with T_CONN_RES failed and 2318 * we required, we would be indicating a T_DISCON_IND to blow 2319 * away this connection. 2320 */ 2321 2322 /* 2323 * XXX: we currently have a problem if XTI application closes the 2324 * acceptor stream in between. This problem exists in on10-gate also 2325 * and is well know but nothing can be done short of major rewrite 2326 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2327 * eager same squeue as listener (we can distinguish non socket 2328 * listeners at the time of handling a SYN in tcp_conn_request) 2329 * and do most of the work that tcp_accept_finish does here itself 2330 * and then get behind the acceptor squeue to access the acceptor 2331 * queue. 2332 */ 2333 /* 2334 * We already have a ref on tcp so no need to do one before squeue_enter 2335 */ 2336 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, opt_mp, tcp_accept_finish, 2337 eager->tcp_connp, SQ_FILL, SQTAG_TCP_ACCEPT_FINISH); 2338 } 2339 2340 /* 2341 * Swap information between the eager and acceptor for a TLI/XTI client. 2342 * The sockfs accept is done on the acceptor stream and control goes 2343 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2344 * called. In either case, both the eager and listener are in their own 2345 * perimeter (squeue) and the code has to deal with potential race. 2346 * 2347 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2348 */ 2349 static int 2350 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2351 { 2352 conn_t *econnp, *aconnp; 2353 cred_t *effective_cred = NULL; 2354 2355 ASSERT(eager->tcp_rq == listener->tcp_rq); 2356 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2357 ASSERT(!eager->tcp_hard_bound); 2358 ASSERT(!TCP_IS_SOCKET(acceptor)); 2359 ASSERT(!TCP_IS_SOCKET(eager)); 2360 ASSERT(!TCP_IS_SOCKET(listener)); 2361 2362 econnp = eager->tcp_connp; 2363 aconnp = acceptor->tcp_connp; 2364 2365 /* 2366 * Trusted Extensions may need to use a security label that is 2367 * different from the acceptor's label on MLP and MAC-Exempt 2368 * sockets. If this is the case, the required security label 2369 * already exists in econnp->conn_effective_cred. Use this label 2370 * to generate a new effective cred for the acceptor. 2371 * 2372 * We allow for potential application level retry attempts by 2373 * checking for transient errors before modifying eager. 2374 */ 2375 if (is_system_labeled() && 2376 aconnp->conn_cred != NULL && econnp->conn_effective_cred != NULL) { 2377 effective_cred = copycred_from_tslabel(aconnp->conn_cred, 2378 crgetlabel(econnp->conn_effective_cred), KM_NOSLEEP); 2379 if (effective_cred == NULL) 2380 return (ENOMEM); 2381 } 2382 2383 acceptor->tcp_detached = B_TRUE; 2384 /* 2385 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2386 * the acceptor id. 2387 */ 2388 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2389 2390 /* remove eager from listen list... */ 2391 mutex_enter(&listener->tcp_eager_lock); 2392 tcp_eager_unlink(eager); 2393 ASSERT(eager->tcp_eager_next_q == NULL && 2394 eager->tcp_eager_last_q == NULL); 2395 ASSERT(eager->tcp_eager_next_q0 == NULL && 2396 eager->tcp_eager_prev_q0 == NULL); 2397 mutex_exit(&listener->tcp_eager_lock); 2398 eager->tcp_rq = acceptor->tcp_rq; 2399 eager->tcp_wq = acceptor->tcp_wq; 2400 2401 eager->tcp_rq->q_ptr = econnp; 2402 eager->tcp_wq->q_ptr = econnp; 2403 2404 /* 2405 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2406 * which might be a different squeue from our peer TCP instance. 2407 * For TCP Fusion, the peer expects that whenever tcp_detached is 2408 * clear, our TCP queues point to the acceptor's queues. Thus, use 2409 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2410 * above reach global visibility prior to the clearing of tcp_detached. 2411 */ 2412 membar_producer(); 2413 eager->tcp_detached = B_FALSE; 2414 2415 ASSERT(eager->tcp_ack_tid == 0); 2416 2417 econnp->conn_dev = aconnp->conn_dev; 2418 econnp->conn_minor_arena = aconnp->conn_minor_arena; 2419 2420 ASSERT(econnp->conn_minor_arena != NULL); 2421 if (eager->tcp_cred != NULL) 2422 crfree(eager->tcp_cred); 2423 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2424 if (econnp->conn_effective_cred != NULL) 2425 crfree(econnp->conn_effective_cred); 2426 econnp->conn_effective_cred = effective_cred; 2427 aconnp->conn_cred = NULL; 2428 ASSERT(aconnp->conn_effective_cred == NULL); 2429 2430 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2431 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2432 2433 econnp->conn_zoneid = aconnp->conn_zoneid; 2434 econnp->conn_allzones = aconnp->conn_allzones; 2435 2436 aconnp->conn_mac_exempt = B_FALSE; 2437 2438 /* Do the IPC initialization */ 2439 CONN_INC_REF(econnp); 2440 2441 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2442 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2443 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2444 2445 /* Done with old IPC. Drop its ref on its connp */ 2446 CONN_DEC_REF(aconnp); 2447 return (0); 2448 } 2449 2450 2451 /* 2452 * Adapt to the information, such as rtt and rtt_sd, provided from the 2453 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2454 * 2455 * Checks for multicast and broadcast destination address. 2456 * Returns zero on failure; non-zero if ok. 2457 * 2458 * Note that the MSS calculation here is based on the info given in 2459 * the IRE. We do not do any calculation based on TCP options. They 2460 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2461 * knows which options to use. 2462 * 2463 * Note on how TCP gets its parameters for a connection. 2464 * 2465 * When a tcp_t structure is allocated, it gets all the default parameters. 2466 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2467 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2468 * default. 2469 * 2470 * An incoming SYN with a multicast or broadcast destination address, is dropped 2471 * in 1 of 2 places. 2472 * 2473 * 1. If the packet was received over the wire it is dropped in 2474 * ip_rput_process_broadcast() 2475 * 2476 * 2. If the packet was received through internal IP loopback, i.e. the packet 2477 * was generated and received on the same machine, it is dropped in 2478 * ip_wput_local() 2479 * 2480 * An incoming SYN with a multicast or broadcast source address is always 2481 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2482 * reject an attempt to connect to a broadcast or multicast (destination) 2483 * address. 2484 */ 2485 static int 2486 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2487 { 2488 ire_t *ire; 2489 ire_t *sire = NULL; 2490 iulp_t *ire_uinfo = NULL; 2491 uint32_t mss_max; 2492 uint32_t mss; 2493 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2494 conn_t *connp = tcp->tcp_connp; 2495 boolean_t ire_cacheable = B_FALSE; 2496 zoneid_t zoneid = connp->conn_zoneid; 2497 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2498 MATCH_IRE_SECATTR; 2499 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2500 ill_t *ill = NULL; 2501 boolean_t incoming = (ire_mp == NULL); 2502 tcp_stack_t *tcps = tcp->tcp_tcps; 2503 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2504 2505 ASSERT(connp->conn_ire_cache == NULL); 2506 2507 if (tcp->tcp_ipversion == IPV4_VERSION) { 2508 2509 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2510 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2511 return (0); 2512 } 2513 /* 2514 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2515 * for the destination with the nexthop as gateway. 2516 * ire_ctable_lookup() is used because this particular 2517 * ire, if it exists, will be marked private. 2518 * If that is not available, use the interface ire 2519 * for the nexthop. 2520 * 2521 * TSol: tcp_update_label will detect label mismatches based 2522 * only on the destination's label, but that would not 2523 * detect label mismatches based on the security attributes 2524 * of routes or next hop gateway. Hence we need to pass the 2525 * label to ire_ftable_lookup below in order to locate the 2526 * right prefix (and/or) ire cache. Similarly we also need 2527 * pass the label to the ire_cache_lookup below to locate 2528 * the right ire that also matches on the label. 2529 */ 2530 if (tcp->tcp_connp->conn_nexthop_set) { 2531 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2532 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2533 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2534 ipst); 2535 if (ire == NULL) { 2536 ire = ire_ftable_lookup( 2537 tcp->tcp_connp->conn_nexthop_v4, 2538 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2539 tsl, match_flags, ipst); 2540 if (ire == NULL) 2541 return (0); 2542 } else { 2543 ire_uinfo = &ire->ire_uinfo; 2544 } 2545 } else { 2546 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2547 zoneid, tsl, ipst); 2548 if (ire != NULL) { 2549 ire_cacheable = B_TRUE; 2550 ire_uinfo = (ire_mp != NULL) ? 2551 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2552 &ire->ire_uinfo; 2553 2554 } else { 2555 if (ire_mp == NULL) { 2556 ire = ire_ftable_lookup( 2557 tcp->tcp_connp->conn_rem, 2558 0, 0, 0, NULL, &sire, zoneid, 0, 2559 tsl, (MATCH_IRE_RECURSIVE | 2560 MATCH_IRE_DEFAULT), ipst); 2561 if (ire == NULL) 2562 return (0); 2563 ire_uinfo = (sire != NULL) ? 2564 &sire->ire_uinfo : 2565 &ire->ire_uinfo; 2566 } else { 2567 ire = (ire_t *)ire_mp->b_rptr; 2568 ire_uinfo = 2569 &((ire_t *) 2570 ire_mp->b_rptr)->ire_uinfo; 2571 } 2572 } 2573 } 2574 ASSERT(ire != NULL); 2575 2576 if ((ire->ire_src_addr == INADDR_ANY) || 2577 (ire->ire_type & IRE_BROADCAST)) { 2578 /* 2579 * ire->ire_mp is non null when ire_mp passed in is used 2580 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2581 */ 2582 if (ire->ire_mp == NULL) 2583 ire_refrele(ire); 2584 if (sire != NULL) 2585 ire_refrele(sire); 2586 return (0); 2587 } 2588 2589 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2590 ipaddr_t src_addr; 2591 2592 /* 2593 * ip_bind_connected() has stored the correct source 2594 * address in conn_src. 2595 */ 2596 src_addr = tcp->tcp_connp->conn_src; 2597 tcp->tcp_ipha->ipha_src = src_addr; 2598 /* 2599 * Copy of the src addr. in tcp_t is needed 2600 * for the lookup funcs. 2601 */ 2602 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2603 } 2604 /* 2605 * Set the fragment bit so that IP will tell us if the MTU 2606 * should change. IP tells us the latest setting of 2607 * ip_path_mtu_discovery through ire_frag_flag. 2608 */ 2609 if (ipst->ips_ip_path_mtu_discovery) { 2610 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2611 htons(IPH_DF); 2612 } 2613 /* 2614 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2615 * for IP_NEXTHOP. No cache ire has been found for the 2616 * destination and we are working with the nexthop's 2617 * interface ire. Since we need to forward all packets 2618 * to the nexthop first, we "blindly" set tcp_localnet 2619 * to false, eventhough the destination may also be 2620 * onlink. 2621 */ 2622 if (ire_uinfo == NULL) 2623 tcp->tcp_localnet = 0; 2624 else 2625 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2626 } else { 2627 /* 2628 * For incoming connection ire_mp = NULL 2629 * For outgoing connection ire_mp != NULL 2630 * Technically we should check conn_incoming_ill 2631 * when ire_mp is NULL and conn_outgoing_ill when 2632 * ire_mp is non-NULL. But this is performance 2633 * critical path and for IPV*_BOUND_IF, outgoing 2634 * and incoming ill are always set to the same value. 2635 */ 2636 ill_t *dst_ill = NULL; 2637 ipif_t *dst_ipif = NULL; 2638 2639 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2640 2641 if (connp->conn_outgoing_ill != NULL) { 2642 /* Outgoing or incoming path */ 2643 int err; 2644 2645 dst_ill = conn_get_held_ill(connp, 2646 &connp->conn_outgoing_ill, &err); 2647 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2648 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2649 return (0); 2650 } 2651 match_flags |= MATCH_IRE_ILL; 2652 dst_ipif = dst_ill->ill_ipif; 2653 } 2654 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2655 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2656 2657 if (ire != NULL) { 2658 ire_cacheable = B_TRUE; 2659 ire_uinfo = (ire_mp != NULL) ? 2660 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2661 &ire->ire_uinfo; 2662 } else { 2663 if (ire_mp == NULL) { 2664 ire = ire_ftable_lookup_v6( 2665 &tcp->tcp_connp->conn_remv6, 2666 0, 0, 0, dst_ipif, &sire, zoneid, 2667 0, tsl, match_flags, ipst); 2668 if (ire == NULL) { 2669 if (dst_ill != NULL) 2670 ill_refrele(dst_ill); 2671 return (0); 2672 } 2673 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2674 &ire->ire_uinfo; 2675 } else { 2676 ire = (ire_t *)ire_mp->b_rptr; 2677 ire_uinfo = 2678 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2679 } 2680 } 2681 if (dst_ill != NULL) 2682 ill_refrele(dst_ill); 2683 2684 ASSERT(ire != NULL); 2685 ASSERT(ire_uinfo != NULL); 2686 2687 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2688 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2689 /* 2690 * ire->ire_mp is non null when ire_mp passed in is used 2691 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2692 */ 2693 if (ire->ire_mp == NULL) 2694 ire_refrele(ire); 2695 if (sire != NULL) 2696 ire_refrele(sire); 2697 return (0); 2698 } 2699 2700 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2701 in6_addr_t src_addr; 2702 2703 /* 2704 * ip_bind_connected_v6() has stored the correct source 2705 * address per IPv6 addr. selection policy in 2706 * conn_src_v6. 2707 */ 2708 src_addr = tcp->tcp_connp->conn_srcv6; 2709 2710 tcp->tcp_ip6h->ip6_src = src_addr; 2711 /* 2712 * Copy of the src addr. in tcp_t is needed 2713 * for the lookup funcs. 2714 */ 2715 tcp->tcp_ip_src_v6 = src_addr; 2716 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2717 &connp->conn_srcv6)); 2718 } 2719 tcp->tcp_localnet = 2720 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2721 } 2722 2723 /* 2724 * This allows applications to fail quickly when connections are made 2725 * to dead hosts. Hosts can be labeled dead by adding a reject route 2726 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2727 */ 2728 if ((ire->ire_flags & RTF_REJECT) && 2729 (ire->ire_flags & RTF_PRIVATE)) 2730 goto error; 2731 2732 /* 2733 * Make use of the cached rtt and rtt_sd values to calculate the 2734 * initial RTO. Note that they are already initialized in 2735 * tcp_init_values(). 2736 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2737 * IP_NEXTHOP, but instead are using the interface ire for the 2738 * nexthop, then we do not use the ire_uinfo from that ire to 2739 * do any initializations. 2740 */ 2741 if (ire_uinfo != NULL) { 2742 if (ire_uinfo->iulp_rtt != 0) { 2743 clock_t rto; 2744 2745 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2746 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2747 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2748 tcps->tcps_rexmit_interval_extra + 2749 (tcp->tcp_rtt_sa >> 5); 2750 2751 if (rto > tcps->tcps_rexmit_interval_max) { 2752 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2753 } else if (rto < tcps->tcps_rexmit_interval_min) { 2754 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2755 } else { 2756 tcp->tcp_rto = rto; 2757 } 2758 } 2759 if (ire_uinfo->iulp_ssthresh != 0) 2760 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2761 else 2762 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2763 if (ire_uinfo->iulp_spipe > 0) { 2764 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2765 tcps->tcps_max_buf); 2766 if (tcps->tcps_snd_lowat_fraction != 0) 2767 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2768 tcps->tcps_snd_lowat_fraction; 2769 (void) tcp_maxpsz_set(tcp, B_TRUE); 2770 } 2771 /* 2772 * Note that up till now, acceptor always inherits receive 2773 * window from the listener. But if there is a metrics 2774 * associated with a host, we should use that instead of 2775 * inheriting it from listener. Thus we need to pass this 2776 * info back to the caller. 2777 */ 2778 if (ire_uinfo->iulp_rpipe > 0) { 2779 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2780 tcps->tcps_max_buf); 2781 } 2782 2783 if (ire_uinfo->iulp_rtomax > 0) { 2784 tcp->tcp_second_timer_threshold = 2785 ire_uinfo->iulp_rtomax; 2786 } 2787 2788 /* 2789 * Use the metric option settings, iulp_tstamp_ok and 2790 * iulp_wscale_ok, only for active open. What this means 2791 * is that if the other side uses timestamp or window 2792 * scale option, TCP will also use those options. That 2793 * is for passive open. If the application sets a 2794 * large window, window scale is enabled regardless of 2795 * the value in iulp_wscale_ok. This is the behavior 2796 * since 2.6. So we keep it. 2797 * The only case left in passive open processing is the 2798 * check for SACK. 2799 * For ECN, it should probably be like SACK. But the 2800 * current value is binary, so we treat it like the other 2801 * cases. The metric only controls active open.For passive 2802 * open, the ndd param, tcp_ecn_permitted, controls the 2803 * behavior. 2804 */ 2805 if (!tcp_detached) { 2806 /* 2807 * The if check means that the following can only 2808 * be turned on by the metrics only IRE, but not off. 2809 */ 2810 if (ire_uinfo->iulp_tstamp_ok) 2811 tcp->tcp_snd_ts_ok = B_TRUE; 2812 if (ire_uinfo->iulp_wscale_ok) 2813 tcp->tcp_snd_ws_ok = B_TRUE; 2814 if (ire_uinfo->iulp_sack == 2) 2815 tcp->tcp_snd_sack_ok = B_TRUE; 2816 if (ire_uinfo->iulp_ecn_ok) 2817 tcp->tcp_ecn_ok = B_TRUE; 2818 } else { 2819 /* 2820 * Passive open. 2821 * 2822 * As above, the if check means that SACK can only be 2823 * turned on by the metric only IRE. 2824 */ 2825 if (ire_uinfo->iulp_sack > 0) { 2826 tcp->tcp_snd_sack_ok = B_TRUE; 2827 } 2828 } 2829 } 2830 2831 2832 /* 2833 * XXX: Note that currently, ire_max_frag can be as small as 68 2834 * because of PMTUd. So tcp_mss may go to negative if combined 2835 * length of all those options exceeds 28 bytes. But because 2836 * of the tcp_mss_min check below, we may not have a problem if 2837 * tcp_mss_min is of a reasonable value. The default is 1 so 2838 * the negative problem still exists. And the check defeats PMTUd. 2839 * In fact, if PMTUd finds that the MSS should be smaller than 2840 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2841 * value. 2842 * 2843 * We do not deal with that now. All those problems related to 2844 * PMTUd will be fixed later. 2845 */ 2846 ASSERT(ire->ire_max_frag != 0); 2847 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2848 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2849 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2850 mss = MIN(mss, IPV6_MIN_MTU); 2851 } 2852 } 2853 2854 /* Sanity check for MSS value. */ 2855 if (tcp->tcp_ipversion == IPV4_VERSION) 2856 mss_max = tcps->tcps_mss_max_ipv4; 2857 else 2858 mss_max = tcps->tcps_mss_max_ipv6; 2859 2860 if (tcp->tcp_ipversion == IPV6_VERSION && 2861 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2862 /* 2863 * After receiving an ICMPv6 "packet too big" message with a 2864 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2865 * will insert a 8-byte fragment header in every packet; we 2866 * reduce the MSS by that amount here. 2867 */ 2868 mss -= sizeof (ip6_frag_t); 2869 } 2870 2871 if (tcp->tcp_ipsec_overhead == 0) 2872 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2873 2874 mss -= tcp->tcp_ipsec_overhead; 2875 2876 if (mss < tcps->tcps_mss_min) 2877 mss = tcps->tcps_mss_min; 2878 if (mss > mss_max) 2879 mss = mss_max; 2880 2881 /* Note that this is the maximum MSS, excluding all options. */ 2882 tcp->tcp_mss = mss; 2883 2884 /* 2885 * Initialize the ISS here now that we have the full connection ID. 2886 * The RFC 1948 method of initial sequence number generation requires 2887 * knowledge of the full connection ID before setting the ISS. 2888 */ 2889 2890 tcp_iss_init(tcp); 2891 2892 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2893 tcp->tcp_loopback = B_TRUE; 2894 2895 if (sire != NULL) 2896 IRE_REFRELE(sire); 2897 2898 /* 2899 * If we got an IRE_CACHE and an ILL, go through their properties; 2900 * otherwise, this is deferred until later when we have an IRE_CACHE. 2901 */ 2902 if (tcp->tcp_loopback || 2903 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2904 /* 2905 * For incoming, see if this tcp may be MDT-capable. For 2906 * outgoing, this process has been taken care of through 2907 * tcp_rput_other. 2908 */ 2909 tcp_ire_ill_check(tcp, ire, ill, incoming); 2910 tcp->tcp_ire_ill_check_done = B_TRUE; 2911 } 2912 2913 mutex_enter(&connp->conn_lock); 2914 /* 2915 * Make sure that conn is not marked incipient 2916 * for incoming connections. A blind 2917 * removal of incipient flag is cheaper than 2918 * check and removal. 2919 */ 2920 connp->conn_state_flags &= ~CONN_INCIPIENT; 2921 2922 /* 2923 * Must not cache forwarding table routes 2924 * or recache an IRE after the conn_t has 2925 * had conn_ire_cache cleared and is flagged 2926 * unusable, (see the CONN_CACHE_IRE() macro). 2927 */ 2928 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 2929 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2930 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2931 connp->conn_ire_cache = ire; 2932 IRE_UNTRACE_REF(ire); 2933 rw_exit(&ire->ire_bucket->irb_lock); 2934 mutex_exit(&connp->conn_lock); 2935 return (1); 2936 } 2937 rw_exit(&ire->ire_bucket->irb_lock); 2938 } 2939 mutex_exit(&connp->conn_lock); 2940 2941 if (ire->ire_mp == NULL) 2942 ire_refrele(ire); 2943 return (1); 2944 2945 error: 2946 if (ire->ire_mp == NULL) 2947 ire_refrele(ire); 2948 if (sire != NULL) 2949 ire_refrele(sire); 2950 return (0); 2951 } 2952 2953 static void 2954 tcp_tpi_bind(tcp_t *tcp, mblk_t *mp) 2955 { 2956 int error; 2957 conn_t *connp = tcp->tcp_connp; 2958 struct sockaddr *sa; 2959 mblk_t *mp1; 2960 struct T_bind_req *tbr; 2961 int backlog; 2962 socklen_t len; 2963 sin_t *sin; 2964 sin6_t *sin6; 2965 cred_t *cr; 2966 2967 /* 2968 * All Solaris components should pass a db_credp 2969 * for this TPI message, hence we ASSERT. 2970 * But in case there is some other M_PROTO that looks 2971 * like a TPI message sent by some other kernel 2972 * component, we check and return an error. 2973 */ 2974 cr = msg_getcred(mp, NULL); 2975 ASSERT(cr != NULL); 2976 if (cr == NULL) { 2977 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 2978 return; 2979 } 2980 2981 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 2982 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 2983 if (tcp->tcp_debug) { 2984 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 2985 "tcp_tpi_bind: bad req, len %u", 2986 (uint_t)(mp->b_wptr - mp->b_rptr)); 2987 } 2988 tcp_err_ack(tcp, mp, TPROTO, 0); 2989 return; 2990 } 2991 /* Make sure the largest address fits */ 2992 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 2993 if (mp1 == NULL) { 2994 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 2995 return; 2996 } 2997 mp = mp1; 2998 tbr = (struct T_bind_req *)mp->b_rptr; 2999 3000 backlog = tbr->CONIND_number; 3001 len = tbr->ADDR_length; 3002 3003 switch (len) { 3004 case 0: /* request for a generic port */ 3005 tbr->ADDR_offset = sizeof (struct T_bind_req); 3006 if (tcp->tcp_family == AF_INET) { 3007 tbr->ADDR_length = sizeof (sin_t); 3008 sin = (sin_t *)&tbr[1]; 3009 *sin = sin_null; 3010 sin->sin_family = AF_INET; 3011 sa = (struct sockaddr *)sin; 3012 len = sizeof (sin_t); 3013 mp->b_wptr = (uchar_t *)&sin[1]; 3014 } else { 3015 ASSERT(tcp->tcp_family == AF_INET6); 3016 tbr->ADDR_length = sizeof (sin6_t); 3017 sin6 = (sin6_t *)&tbr[1]; 3018 *sin6 = sin6_null; 3019 sin6->sin6_family = AF_INET6; 3020 sa = (struct sockaddr *)sin6; 3021 len = sizeof (sin6_t); 3022 mp->b_wptr = (uchar_t *)&sin6[1]; 3023 } 3024 break; 3025 3026 case sizeof (sin_t): /* Complete IPv4 address */ 3027 sa = (struct sockaddr *)mi_offset_param(mp, tbr->ADDR_offset, 3028 sizeof (sin_t)); 3029 break; 3030 3031 case sizeof (sin6_t): /* Complete IPv6 address */ 3032 sa = (struct sockaddr *)mi_offset_param(mp, 3033 tbr->ADDR_offset, sizeof (sin6_t)); 3034 break; 3035 3036 default: 3037 if (tcp->tcp_debug) { 3038 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3039 "tcp_tpi_bind: bad address length, %d", 3040 tbr->ADDR_length); 3041 } 3042 tcp_err_ack(tcp, mp, TBADADDR, 0); 3043 return; 3044 } 3045 3046 if (backlog > 0) { 3047 error = tcp_do_listen(connp, sa, len, backlog, DB_CRED(mp), 3048 tbr->PRIM_type != O_T_BIND_REQ); 3049 } else { 3050 error = tcp_do_bind(connp, sa, len, DB_CRED(mp), 3051 tbr->PRIM_type != O_T_BIND_REQ); 3052 } 3053 done: 3054 if (error > 0) { 3055 tcp_err_ack(tcp, mp, TSYSERR, error); 3056 } else if (error < 0) { 3057 tcp_err_ack(tcp, mp, -error, 0); 3058 } else { 3059 /* 3060 * Update port information as sockfs/tpi needs it for checking 3061 */ 3062 if (tcp->tcp_family == AF_INET) { 3063 sin = (sin_t *)sa; 3064 sin->sin_port = tcp->tcp_lport; 3065 } else { 3066 sin6 = (sin6_t *)sa; 3067 sin6->sin6_port = tcp->tcp_lport; 3068 } 3069 mp->b_datap->db_type = M_PCPROTO; 3070 tbr->PRIM_type = T_BIND_ACK; 3071 putnext(tcp->tcp_rq, mp); 3072 } 3073 } 3074 3075 /* 3076 * If the "bind_to_req_port_only" parameter is set, if the requested port 3077 * number is available, return it, If not return 0 3078 * 3079 * If "bind_to_req_port_only" parameter is not set and 3080 * If the requested port number is available, return it. If not, return 3081 * the first anonymous port we happen across. If no anonymous ports are 3082 * available, return 0. addr is the requested local address, if any. 3083 * 3084 * In either case, when succeeding update the tcp_t to record the port number 3085 * and insert it in the bind hash table. 3086 * 3087 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3088 * without setting SO_REUSEADDR. This is needed so that they 3089 * can be viewed as two independent transport protocols. 3090 */ 3091 static in_port_t 3092 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3093 int reuseaddr, boolean_t quick_connect, 3094 boolean_t bind_to_req_port_only, boolean_t user_specified) 3095 { 3096 /* number of times we have run around the loop */ 3097 int count = 0; 3098 /* maximum number of times to run around the loop */ 3099 int loopmax; 3100 conn_t *connp = tcp->tcp_connp; 3101 zoneid_t zoneid = connp->conn_zoneid; 3102 tcp_stack_t *tcps = tcp->tcp_tcps; 3103 3104 /* 3105 * Lookup for free addresses is done in a loop and "loopmax" 3106 * influences how long we spin in the loop 3107 */ 3108 if (bind_to_req_port_only) { 3109 /* 3110 * If the requested port is busy, don't bother to look 3111 * for a new one. Setting loop maximum count to 1 has 3112 * that effect. 3113 */ 3114 loopmax = 1; 3115 } else { 3116 /* 3117 * If the requested port is busy, look for a free one 3118 * in the anonymous port range. 3119 * Set loopmax appropriately so that one does not look 3120 * forever in the case all of the anonymous ports are in use. 3121 */ 3122 if (tcp->tcp_anon_priv_bind) { 3123 /* 3124 * loopmax = 3125 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3126 */ 3127 loopmax = IPPORT_RESERVED - 3128 tcps->tcps_min_anonpriv_port; 3129 } else { 3130 loopmax = (tcps->tcps_largest_anon_port - 3131 tcps->tcps_smallest_anon_port + 1); 3132 } 3133 } 3134 do { 3135 uint16_t lport; 3136 tf_t *tbf; 3137 tcp_t *ltcp; 3138 conn_t *lconnp; 3139 3140 lport = htons(port); 3141 3142 /* 3143 * Ensure that the tcp_t is not currently in the bind hash. 3144 * Hold the lock on the hash bucket to ensure that 3145 * the duplicate check plus the insertion is an atomic 3146 * operation. 3147 * 3148 * This function does an inline lookup on the bind hash list 3149 * Make sure that we access only members of tcp_t 3150 * and that we don't look at tcp_tcp, since we are not 3151 * doing a CONN_INC_REF. 3152 */ 3153 tcp_bind_hash_remove(tcp); 3154 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3155 mutex_enter(&tbf->tf_lock); 3156 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3157 ltcp = ltcp->tcp_bind_hash) { 3158 if (lport == ltcp->tcp_lport) 3159 break; 3160 } 3161 3162 for (; ltcp != NULL; ltcp = ltcp->tcp_bind_hash_port) { 3163 boolean_t not_socket; 3164 boolean_t exclbind; 3165 3166 lconnp = ltcp->tcp_connp; 3167 3168 /* 3169 * On a labeled system, we must treat bindings to ports 3170 * on shared IP addresses by sockets with MAC exemption 3171 * privilege as being in all zones, as there's 3172 * otherwise no way to identify the right receiver. 3173 */ 3174 if (!(IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) || 3175 IPCL_ZONE_MATCH(connp, 3176 ltcp->tcp_connp->conn_zoneid)) && 3177 !lconnp->conn_mac_exempt && 3178 !connp->conn_mac_exempt) 3179 continue; 3180 3181 /* 3182 * If TCP_EXCLBIND is set for either the bound or 3183 * binding endpoint, the semantics of bind 3184 * is changed according to the following. 3185 * 3186 * spec = specified address (v4 or v6) 3187 * unspec = unspecified address (v4 or v6) 3188 * A = specified addresses are different for endpoints 3189 * 3190 * bound bind to allowed 3191 * ------------------------------------- 3192 * unspec unspec no 3193 * unspec spec no 3194 * spec unspec no 3195 * spec spec yes if A 3196 * 3197 * For labeled systems, SO_MAC_EXEMPT behaves the same 3198 * as TCP_EXCLBIND, except that zoneid is ignored. 3199 * 3200 * Note: 3201 * 3202 * 1. Because of TLI semantics, an endpoint can go 3203 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3204 * TCPS_BOUND, depending on whether it is originally 3205 * a listener or not. That is why we need to check 3206 * for states greater than or equal to TCPS_BOUND 3207 * here. 3208 * 3209 * 2. Ideally, we should only check for state equals 3210 * to TCPS_LISTEN. And the following check should be 3211 * added. 3212 * 3213 * if (ltcp->tcp_state == TCPS_LISTEN || 3214 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3215 * ... 3216 * } 3217 * 3218 * The semantics will be changed to this. If the 3219 * endpoint on the list is in state not equal to 3220 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3221 * set, let the bind succeed. 3222 * 3223 * Because of (1), we cannot do that for TLI 3224 * endpoints. But we can do that for socket endpoints. 3225 * If in future, we can change this going back 3226 * semantics, we can use the above check for TLI also. 3227 */ 3228 not_socket = !(TCP_IS_SOCKET(ltcp) && 3229 TCP_IS_SOCKET(tcp)); 3230 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3231 3232 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3233 (exclbind && (not_socket || 3234 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3235 if (V6_OR_V4_INADDR_ANY( 3236 ltcp->tcp_bound_source_v6) || 3237 V6_OR_V4_INADDR_ANY(*laddr) || 3238 IN6_ARE_ADDR_EQUAL(laddr, 3239 <cp->tcp_bound_source_v6)) { 3240 break; 3241 } 3242 continue; 3243 } 3244 3245 /* 3246 * Check ipversion to allow IPv4 and IPv6 sockets to 3247 * have disjoint port number spaces, if *_EXCLBIND 3248 * is not set and only if the application binds to a 3249 * specific port. We use the same autoassigned port 3250 * number space for IPv4 and IPv6 sockets. 3251 */ 3252 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3253 bind_to_req_port_only) 3254 continue; 3255 3256 /* 3257 * Ideally, we should make sure that the source 3258 * address, remote address, and remote port in the 3259 * four tuple for this tcp-connection is unique. 3260 * However, trying to find out the local source 3261 * address would require too much code duplication 3262 * with IP, since IP needs needs to have that code 3263 * to support userland TCP implementations. 3264 */ 3265 if (quick_connect && 3266 (ltcp->tcp_state > TCPS_LISTEN) && 3267 ((tcp->tcp_fport != ltcp->tcp_fport) || 3268 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3269 <cp->tcp_remote_v6))) 3270 continue; 3271 3272 if (!reuseaddr) { 3273 /* 3274 * No socket option SO_REUSEADDR. 3275 * If existing port is bound to 3276 * a non-wildcard IP address 3277 * and the requesting stream is 3278 * bound to a distinct 3279 * different IP addresses 3280 * (non-wildcard, also), keep 3281 * going. 3282 */ 3283 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3284 !V6_OR_V4_INADDR_ANY( 3285 ltcp->tcp_bound_source_v6) && 3286 !IN6_ARE_ADDR_EQUAL(laddr, 3287 <cp->tcp_bound_source_v6)) 3288 continue; 3289 if (ltcp->tcp_state >= TCPS_BOUND) { 3290 /* 3291 * This port is being used and 3292 * its state is >= TCPS_BOUND, 3293 * so we can't bind to it. 3294 */ 3295 break; 3296 } 3297 } else { 3298 /* 3299 * socket option SO_REUSEADDR is set on the 3300 * binding tcp_t. 3301 * 3302 * If two streams are bound to 3303 * same IP address or both addr 3304 * and bound source are wildcards 3305 * (INADDR_ANY), we want to stop 3306 * searching. 3307 * We have found a match of IP source 3308 * address and source port, which is 3309 * refused regardless of the 3310 * SO_REUSEADDR setting, so we break. 3311 */ 3312 if (IN6_ARE_ADDR_EQUAL(laddr, 3313 <cp->tcp_bound_source_v6) && 3314 (ltcp->tcp_state == TCPS_LISTEN || 3315 ltcp->tcp_state == TCPS_BOUND)) 3316 break; 3317 } 3318 } 3319 if (ltcp != NULL) { 3320 /* The port number is busy */ 3321 mutex_exit(&tbf->tf_lock); 3322 } else { 3323 /* 3324 * This port is ours. Insert in fanout and mark as 3325 * bound to prevent others from getting the port 3326 * number. 3327 */ 3328 tcp->tcp_state = TCPS_BOUND; 3329 tcp->tcp_lport = htons(port); 3330 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3331 3332 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3333 tcp->tcp_lport)] == tbf); 3334 tcp_bind_hash_insert(tbf, tcp, 1); 3335 3336 mutex_exit(&tbf->tf_lock); 3337 3338 /* 3339 * We don't want tcp_next_port_to_try to "inherit" 3340 * a port number supplied by the user in a bind. 3341 */ 3342 if (user_specified) 3343 return (port); 3344 3345 /* 3346 * This is the only place where tcp_next_port_to_try 3347 * is updated. After the update, it may or may not 3348 * be in the valid range. 3349 */ 3350 if (!tcp->tcp_anon_priv_bind) 3351 tcps->tcps_next_port_to_try = port + 1; 3352 return (port); 3353 } 3354 3355 if (tcp->tcp_anon_priv_bind) { 3356 port = tcp_get_next_priv_port(tcp); 3357 } else { 3358 if (count == 0 && user_specified) { 3359 /* 3360 * We may have to return an anonymous port. So 3361 * get one to start with. 3362 */ 3363 port = 3364 tcp_update_next_port( 3365 tcps->tcps_next_port_to_try, 3366 tcp, B_TRUE); 3367 user_specified = B_FALSE; 3368 } else { 3369 port = tcp_update_next_port(port + 1, tcp, 3370 B_FALSE); 3371 } 3372 } 3373 if (port == 0) 3374 break; 3375 3376 /* 3377 * Don't let this loop run forever in the case where 3378 * all of the anonymous ports are in use. 3379 */ 3380 } while (++count < loopmax); 3381 return (0); 3382 } 3383 3384 /* 3385 * tcp_clean_death / tcp_close_detached must not be called more than once 3386 * on a tcp. Thus every function that potentially calls tcp_clean_death 3387 * must check for the tcp state before calling tcp_clean_death. 3388 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3389 * tcp_timer_handler, all check for the tcp state. 3390 */ 3391 /* ARGSUSED */ 3392 void 3393 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3394 { 3395 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3396 3397 freemsg(mp); 3398 if (tcp->tcp_state > TCPS_BOUND) 3399 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, 3400 ETIMEDOUT, 5); 3401 } 3402 3403 /* 3404 * We are dying for some reason. Try to do it gracefully. (May be called 3405 * as writer.) 3406 * 3407 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3408 * done by a service procedure). 3409 * TBD - Should the return value distinguish between the tcp_t being 3410 * freed and it being reinitialized? 3411 */ 3412 static int 3413 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3414 { 3415 mblk_t *mp; 3416 queue_t *q; 3417 conn_t *connp = tcp->tcp_connp; 3418 tcp_stack_t *tcps = tcp->tcp_tcps; 3419 3420 TCP_CLD_STAT(tag); 3421 3422 #if TCP_TAG_CLEAN_DEATH 3423 tcp->tcp_cleandeathtag = tag; 3424 #endif 3425 3426 if (tcp->tcp_fused) 3427 tcp_unfuse(tcp); 3428 3429 if (tcp->tcp_linger_tid != 0 && 3430 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3431 tcp_stop_lingering(tcp); 3432 } 3433 3434 ASSERT(tcp != NULL); 3435 ASSERT((tcp->tcp_family == AF_INET && 3436 tcp->tcp_ipversion == IPV4_VERSION) || 3437 (tcp->tcp_family == AF_INET6 && 3438 (tcp->tcp_ipversion == IPV4_VERSION || 3439 tcp->tcp_ipversion == IPV6_VERSION))); 3440 3441 if (TCP_IS_DETACHED(tcp)) { 3442 if (tcp->tcp_hard_binding) { 3443 /* 3444 * Its an eager that we are dealing with. We close the 3445 * eager but in case a conn_ind has already gone to the 3446 * listener, let tcp_accept_finish() send a discon_ind 3447 * to the listener and drop the last reference. If the 3448 * listener doesn't even know about the eager i.e. the 3449 * conn_ind hasn't gone up, blow away the eager and drop 3450 * the last reference as well. If the conn_ind has gone 3451 * up, state should be BOUND. tcp_accept_finish 3452 * will figure out that the connection has received a 3453 * RST and will send a DISCON_IND to the application. 3454 */ 3455 tcp_closei_local(tcp); 3456 if (!tcp->tcp_tconnind_started) { 3457 CONN_DEC_REF(connp); 3458 } else { 3459 tcp->tcp_state = TCPS_BOUND; 3460 } 3461 } else { 3462 tcp_close_detached(tcp); 3463 } 3464 return (0); 3465 } 3466 3467 TCP_STAT(tcps, tcp_clean_death_nondetached); 3468 3469 q = tcp->tcp_rq; 3470 3471 /* Trash all inbound data */ 3472 if (!IPCL_IS_NONSTR(connp)) { 3473 ASSERT(q != NULL); 3474 flushq(q, FLUSHALL); 3475 } 3476 3477 /* 3478 * If we are at least part way open and there is error 3479 * (err==0 implies no error) 3480 * notify our client by a T_DISCON_IND. 3481 */ 3482 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3483 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3484 !TCP_IS_SOCKET(tcp)) { 3485 /* 3486 * Send M_FLUSH according to TPI. Because sockets will 3487 * (and must) ignore FLUSHR we do that only for TPI 3488 * endpoints and sockets in STREAMS mode. 3489 */ 3490 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3491 } 3492 if (tcp->tcp_debug) { 3493 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3494 "tcp_clean_death: discon err %d", err); 3495 } 3496 if (IPCL_IS_NONSTR(connp)) { 3497 /* Direct socket, use upcall */ 3498 (*connp->conn_upcalls->su_disconnected)( 3499 connp->conn_upper_handle, tcp->tcp_connid, err); 3500 } else { 3501 mp = mi_tpi_discon_ind(NULL, err, 0); 3502 if (mp != NULL) { 3503 putnext(q, mp); 3504 } else { 3505 if (tcp->tcp_debug) { 3506 (void) strlog(TCP_MOD_ID, 0, 1, 3507 SL_ERROR|SL_TRACE, 3508 "tcp_clean_death, sending M_ERROR"); 3509 } 3510 (void) putnextctl1(q, M_ERROR, EPROTO); 3511 } 3512 } 3513 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3514 /* SYN_SENT or SYN_RCVD */ 3515 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3516 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3517 /* ESTABLISHED or CLOSE_WAIT */ 3518 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3519 } 3520 } 3521 3522 tcp_reinit(tcp); 3523 if (IPCL_IS_NONSTR(connp)) 3524 (void) tcp_do_unbind(connp); 3525 3526 return (-1); 3527 } 3528 3529 /* 3530 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3531 * to expire, stop the wait and finish the close. 3532 */ 3533 static void 3534 tcp_stop_lingering(tcp_t *tcp) 3535 { 3536 clock_t delta = 0; 3537 tcp_stack_t *tcps = tcp->tcp_tcps; 3538 3539 tcp->tcp_linger_tid = 0; 3540 if (tcp->tcp_state > TCPS_LISTEN) { 3541 tcp_acceptor_hash_remove(tcp); 3542 mutex_enter(&tcp->tcp_non_sq_lock); 3543 if (tcp->tcp_flow_stopped) { 3544 tcp_clrqfull(tcp); 3545 } 3546 mutex_exit(&tcp->tcp_non_sq_lock); 3547 3548 if (tcp->tcp_timer_tid != 0) { 3549 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3550 tcp->tcp_timer_tid = 0; 3551 } 3552 /* 3553 * Need to cancel those timers which will not be used when 3554 * TCP is detached. This has to be done before the tcp_wq 3555 * is set to the global queue. 3556 */ 3557 tcp_timers_stop(tcp); 3558 3559 tcp->tcp_detached = B_TRUE; 3560 ASSERT(tcps->tcps_g_q != NULL); 3561 tcp->tcp_rq = tcps->tcps_g_q; 3562 tcp->tcp_wq = WR(tcps->tcps_g_q); 3563 3564 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3565 tcp_time_wait_append(tcp); 3566 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3567 goto finish; 3568 } 3569 3570 /* 3571 * If delta is zero the timer event wasn't executed and was 3572 * successfully canceled. In this case we need to restart it 3573 * with the minimal delta possible. 3574 */ 3575 if (delta >= 0) { 3576 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3577 delta ? delta : 1); 3578 } 3579 } else { 3580 tcp_closei_local(tcp); 3581 CONN_DEC_REF(tcp->tcp_connp); 3582 } 3583 finish: 3584 /* Signal closing thread that it can complete close */ 3585 mutex_enter(&tcp->tcp_closelock); 3586 tcp->tcp_detached = B_TRUE; 3587 ASSERT(tcps->tcps_g_q != NULL); 3588 3589 tcp->tcp_rq = tcps->tcps_g_q; 3590 tcp->tcp_wq = WR(tcps->tcps_g_q); 3591 3592 tcp->tcp_closed = 1; 3593 cv_signal(&tcp->tcp_closecv); 3594 mutex_exit(&tcp->tcp_closelock); 3595 } 3596 3597 /* 3598 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3599 * expires. 3600 */ 3601 static void 3602 tcp_close_linger_timeout(void *arg) 3603 { 3604 conn_t *connp = (conn_t *)arg; 3605 tcp_t *tcp = connp->conn_tcp; 3606 3607 tcp->tcp_client_errno = ETIMEDOUT; 3608 tcp_stop_lingering(tcp); 3609 } 3610 3611 static void 3612 tcp_close_common(conn_t *connp, int flags) 3613 { 3614 tcp_t *tcp = connp->conn_tcp; 3615 mblk_t *mp = &tcp->tcp_closemp; 3616 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3617 mblk_t *bp; 3618 3619 ASSERT(connp->conn_ref >= 2); 3620 3621 /* 3622 * Mark the conn as closing. ill_pending_mp_add will not 3623 * add any mp to the pending mp list, after this conn has 3624 * started closing. Same for sq_pending_mp_add 3625 */ 3626 mutex_enter(&connp->conn_lock); 3627 connp->conn_state_flags |= CONN_CLOSING; 3628 if (connp->conn_oper_pending_ill != NULL) 3629 conn_ioctl_cleanup_reqd = B_TRUE; 3630 CONN_INC_REF_LOCKED(connp); 3631 mutex_exit(&connp->conn_lock); 3632 tcp->tcp_closeflags = (uint8_t)flags; 3633 ASSERT(connp->conn_ref >= 3); 3634 3635 /* 3636 * tcp_closemp_used is used below without any protection of a lock 3637 * as we don't expect any one else to use it concurrently at this 3638 * point otherwise it would be a major defect. 3639 */ 3640 3641 if (mp->b_prev == NULL) 3642 tcp->tcp_closemp_used = B_TRUE; 3643 else 3644 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: " 3645 "connp %p tcp %p\n", (void *)connp, (void *)tcp); 3646 3647 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 3648 3649 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_close_output, connp, 3650 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3651 3652 mutex_enter(&tcp->tcp_closelock); 3653 while (!tcp->tcp_closed) { 3654 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 3655 /* 3656 * The cv_wait_sig() was interrupted. We now do the 3657 * following: 3658 * 3659 * 1) If the endpoint was lingering, we allow this 3660 * to be interrupted by cancelling the linger timeout 3661 * and closing normally. 3662 * 3663 * 2) Revert to calling cv_wait() 3664 * 3665 * We revert to using cv_wait() to avoid an 3666 * infinite loop which can occur if the calling 3667 * thread is higher priority than the squeue worker 3668 * thread and is bound to the same cpu. 3669 */ 3670 if (tcp->tcp_linger && tcp->tcp_lingertime > 0) { 3671 mutex_exit(&tcp->tcp_closelock); 3672 /* Entering squeue, bump ref count. */ 3673 CONN_INC_REF(connp); 3674 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 3675 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, 3676 tcp_linger_interrupted, connp, 3677 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3678 mutex_enter(&tcp->tcp_closelock); 3679 } 3680 break; 3681 } 3682 } 3683 while (!tcp->tcp_closed) 3684 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 3685 mutex_exit(&tcp->tcp_closelock); 3686 3687 /* 3688 * In the case of listener streams that have eagers in the q or q0 3689 * we wait for the eagers to drop their reference to us. tcp_rq and 3690 * tcp_wq of the eagers point to our queues. By waiting for the 3691 * refcnt to drop to 1, we are sure that the eagers have cleaned 3692 * up their queue pointers and also dropped their references to us. 3693 */ 3694 if (tcp->tcp_wait_for_eagers) { 3695 mutex_enter(&connp->conn_lock); 3696 while (connp->conn_ref != 1) { 3697 cv_wait(&connp->conn_cv, &connp->conn_lock); 3698 } 3699 mutex_exit(&connp->conn_lock); 3700 } 3701 /* 3702 * ioctl cleanup. The mp is queued in the 3703 * ill_pending_mp or in the sq_pending_mp. 3704 */ 3705 if (conn_ioctl_cleanup_reqd) 3706 conn_ioctl_cleanup(connp); 3707 3708 tcp->tcp_cpid = -1; 3709 } 3710 3711 static int 3712 tcp_tpi_close(queue_t *q, int flags) 3713 { 3714 conn_t *connp; 3715 3716 ASSERT(WR(q)->q_next == NULL); 3717 3718 if (flags & SO_FALLBACK) { 3719 /* 3720 * stream is being closed while in fallback 3721 * simply free the resources that were allocated 3722 */ 3723 inet_minor_free(WR(q)->q_ptr, (dev_t)(RD(q)->q_ptr)); 3724 qprocsoff(q); 3725 goto done; 3726 } 3727 3728 connp = Q_TO_CONN(q); 3729 /* 3730 * We are being closed as /dev/tcp or /dev/tcp6. 3731 */ 3732 tcp_close_common(connp, flags); 3733 3734 qprocsoff(q); 3735 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 3736 3737 /* 3738 * Drop IP's reference on the conn. This is the last reference 3739 * on the connp if the state was less than established. If the 3740 * connection has gone into timewait state, then we will have 3741 * one ref for the TCP and one more ref (total of two) for the 3742 * classifier connected hash list (a timewait connections stays 3743 * in connected hash till closed). 3744 * 3745 * We can't assert the references because there might be other 3746 * transient reference places because of some walkers or queued 3747 * packets in squeue for the timewait state. 3748 */ 3749 CONN_DEC_REF(connp); 3750 done: 3751 q->q_ptr = WR(q)->q_ptr = NULL; 3752 return (0); 3753 } 3754 3755 static int 3756 tcp_tpi_close_accept(queue_t *q) 3757 { 3758 vmem_t *minor_arena; 3759 dev_t conn_dev; 3760 3761 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 3762 3763 /* 3764 * We had opened an acceptor STREAM for sockfs which is 3765 * now being closed due to some error. 3766 */ 3767 qprocsoff(q); 3768 3769 minor_arena = (vmem_t *)WR(q)->q_ptr; 3770 conn_dev = (dev_t)RD(q)->q_ptr; 3771 ASSERT(minor_arena != NULL); 3772 ASSERT(conn_dev != 0); 3773 inet_minor_free(minor_arena, conn_dev); 3774 q->q_ptr = WR(q)->q_ptr = NULL; 3775 return (0); 3776 } 3777 3778 /* 3779 * Called by tcp_close() routine via squeue when lingering is 3780 * interrupted by a signal. 3781 */ 3782 3783 /* ARGSUSED */ 3784 static void 3785 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 3786 { 3787 conn_t *connp = (conn_t *)arg; 3788 tcp_t *tcp = connp->conn_tcp; 3789 3790 freeb(mp); 3791 if (tcp->tcp_linger_tid != 0 && 3792 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3793 tcp_stop_lingering(tcp); 3794 tcp->tcp_client_errno = EINTR; 3795 } 3796 } 3797 3798 /* 3799 * Called by streams close routine via squeues when our client blows off her 3800 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 3801 * connection politely" When SO_LINGER is set (with a non-zero linger time and 3802 * it is not a nonblocking socket) then this routine sleeps until the FIN is 3803 * acked. 3804 * 3805 * NOTE: tcp_close potentially returns error when lingering. 3806 * However, the stream head currently does not pass these errors 3807 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 3808 * errors to the application (from tsleep()) and not errors 3809 * like ECONNRESET caused by receiving a reset packet. 3810 */ 3811 3812 /* ARGSUSED */ 3813 static void 3814 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 3815 { 3816 char *msg; 3817 conn_t *connp = (conn_t *)arg; 3818 tcp_t *tcp = connp->conn_tcp; 3819 clock_t delta = 0; 3820 tcp_stack_t *tcps = tcp->tcp_tcps; 3821 3822 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 3823 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 3824 3825 mutex_enter(&tcp->tcp_eager_lock); 3826 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 3827 /* Cleanup for listener */ 3828 tcp_eager_cleanup(tcp, 0); 3829 tcp->tcp_wait_for_eagers = 1; 3830 } 3831 mutex_exit(&tcp->tcp_eager_lock); 3832 3833 connp->conn_mdt_ok = B_FALSE; 3834 tcp->tcp_mdt = B_FALSE; 3835 3836 connp->conn_lso_ok = B_FALSE; 3837 tcp->tcp_lso = B_FALSE; 3838 3839 msg = NULL; 3840 switch (tcp->tcp_state) { 3841 case TCPS_CLOSED: 3842 case TCPS_IDLE: 3843 case TCPS_BOUND: 3844 case TCPS_LISTEN: 3845 break; 3846 case TCPS_SYN_SENT: 3847 msg = "tcp_close, during connect"; 3848 break; 3849 case TCPS_SYN_RCVD: 3850 /* 3851 * Close during the connect 3-way handshake 3852 * but here there may or may not be pending data 3853 * already on queue. Process almost same as in 3854 * the ESTABLISHED state. 3855 */ 3856 /* FALLTHRU */ 3857 default: 3858 if (tcp->tcp_fused) 3859 tcp_unfuse(tcp); 3860 3861 /* 3862 * If SO_LINGER has set a zero linger time, abort the 3863 * connection with a reset. 3864 */ 3865 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 3866 msg = "tcp_close, zero lingertime"; 3867 break; 3868 } 3869 3870 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 3871 /* 3872 * Abort connection if there is unread data queued. 3873 */ 3874 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 3875 msg = "tcp_close, unread data"; 3876 break; 3877 } 3878 /* 3879 * tcp_hard_bound is now cleared thus all packets go through 3880 * tcp_lookup. This fact is used by tcp_detach below. 3881 * 3882 * We have done a qwait() above which could have possibly 3883 * drained more messages in turn causing transition to a 3884 * different state. Check whether we have to do the rest 3885 * of the processing or not. 3886 */ 3887 if (tcp->tcp_state <= TCPS_LISTEN) 3888 break; 3889 3890 /* 3891 * Transmit the FIN before detaching the tcp_t. 3892 * After tcp_detach returns this queue/perimeter 3893 * no longer owns the tcp_t thus others can modify it. 3894 */ 3895 (void) tcp_xmit_end(tcp); 3896 3897 /* 3898 * If lingering on close then wait until the fin is acked, 3899 * the SO_LINGER time passes, or a reset is sent/received. 3900 */ 3901 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 3902 !(tcp->tcp_fin_acked) && 3903 tcp->tcp_state >= TCPS_ESTABLISHED) { 3904 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 3905 tcp->tcp_client_errno = EWOULDBLOCK; 3906 } else if (tcp->tcp_client_errno == 0) { 3907 3908 ASSERT(tcp->tcp_linger_tid == 0); 3909 3910 tcp->tcp_linger_tid = TCP_TIMER(tcp, 3911 tcp_close_linger_timeout, 3912 tcp->tcp_lingertime * hz); 3913 3914 /* tcp_close_linger_timeout will finish close */ 3915 if (tcp->tcp_linger_tid == 0) 3916 tcp->tcp_client_errno = ENOSR; 3917 else 3918 return; 3919 } 3920 3921 /* 3922 * Check if we need to detach or just close 3923 * the instance. 3924 */ 3925 if (tcp->tcp_state <= TCPS_LISTEN) 3926 break; 3927 } 3928 3929 /* 3930 * Make sure that no other thread will access the tcp_rq of 3931 * this instance (through lookups etc.) as tcp_rq will go 3932 * away shortly. 3933 */ 3934 tcp_acceptor_hash_remove(tcp); 3935 3936 mutex_enter(&tcp->tcp_non_sq_lock); 3937 if (tcp->tcp_flow_stopped) { 3938 tcp_clrqfull(tcp); 3939 } 3940 mutex_exit(&tcp->tcp_non_sq_lock); 3941 3942 if (tcp->tcp_timer_tid != 0) { 3943 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3944 tcp->tcp_timer_tid = 0; 3945 } 3946 /* 3947 * Need to cancel those timers which will not be used when 3948 * TCP is detached. This has to be done before the tcp_wq 3949 * is set to the global queue. 3950 */ 3951 tcp_timers_stop(tcp); 3952 3953 tcp->tcp_detached = B_TRUE; 3954 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3955 tcp_time_wait_append(tcp); 3956 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3957 ASSERT(connp->conn_ref >= 3); 3958 goto finish; 3959 } 3960 3961 /* 3962 * If delta is zero the timer event wasn't executed and was 3963 * successfully canceled. In this case we need to restart it 3964 * with the minimal delta possible. 3965 */ 3966 if (delta >= 0) 3967 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3968 delta ? delta : 1); 3969 3970 ASSERT(connp->conn_ref >= 3); 3971 goto finish; 3972 } 3973 3974 /* Detach did not complete. Still need to remove q from stream. */ 3975 if (msg) { 3976 if (tcp->tcp_state == TCPS_ESTABLISHED || 3977 tcp->tcp_state == TCPS_CLOSE_WAIT) 3978 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3979 if (tcp->tcp_state == TCPS_SYN_SENT || 3980 tcp->tcp_state == TCPS_SYN_RCVD) 3981 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3982 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 3983 } 3984 3985 tcp_closei_local(tcp); 3986 CONN_DEC_REF(connp); 3987 ASSERT(connp->conn_ref >= 2); 3988 3989 finish: 3990 /* 3991 * Although packets are always processed on the correct 3992 * tcp's perimeter and access is serialized via squeue's, 3993 * IP still needs a queue when sending packets in time_wait 3994 * state so use WR(tcps_g_q) till ip_output() can be 3995 * changed to deal with just connp. For read side, we 3996 * could have set tcp_rq to NULL but there are some cases 3997 * in tcp_rput_data() from early days of this code which 3998 * do a putnext without checking if tcp is closed. Those 3999 * need to be identified before both tcp_rq and tcp_wq 4000 * can be set to NULL and tcps_g_q can disappear forever. 4001 */ 4002 mutex_enter(&tcp->tcp_closelock); 4003 /* 4004 * Don't change the queues in the case of a listener that has 4005 * eagers in its q or q0. It could surprise the eagers. 4006 * Instead wait for the eagers outside the squeue. 4007 */ 4008 if (!tcp->tcp_wait_for_eagers) { 4009 tcp->tcp_detached = B_TRUE; 4010 /* 4011 * When default queue is closing we set tcps_g_q to NULL 4012 * after the close is done. 4013 */ 4014 ASSERT(tcps->tcps_g_q != NULL); 4015 tcp->tcp_rq = tcps->tcps_g_q; 4016 tcp->tcp_wq = WR(tcps->tcps_g_q); 4017 } 4018 4019 /* Signal tcp_close() to finish closing. */ 4020 tcp->tcp_closed = 1; 4021 cv_signal(&tcp->tcp_closecv); 4022 mutex_exit(&tcp->tcp_closelock); 4023 } 4024 4025 /* 4026 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4027 * Some stream heads get upset if they see these later on as anything but NULL. 4028 */ 4029 static void 4030 tcp_close_mpp(mblk_t **mpp) 4031 { 4032 mblk_t *mp; 4033 4034 if ((mp = *mpp) != NULL) { 4035 do { 4036 mp->b_next = NULL; 4037 mp->b_prev = NULL; 4038 } while ((mp = mp->b_cont) != NULL); 4039 4040 mp = *mpp; 4041 *mpp = NULL; 4042 freemsg(mp); 4043 } 4044 } 4045 4046 /* Do detached close. */ 4047 static void 4048 tcp_close_detached(tcp_t *tcp) 4049 { 4050 if (tcp->tcp_fused) 4051 tcp_unfuse(tcp); 4052 4053 /* 4054 * Clustering code serializes TCP disconnect callbacks and 4055 * cluster tcp list walks by blocking a TCP disconnect callback 4056 * if a cluster tcp list walk is in progress. This ensures 4057 * accurate accounting of TCPs in the cluster code even though 4058 * the TCP list walk itself is not atomic. 4059 */ 4060 tcp_closei_local(tcp); 4061 CONN_DEC_REF(tcp->tcp_connp); 4062 } 4063 4064 /* 4065 * Stop all TCP timers, and free the timer mblks if requested. 4066 */ 4067 void 4068 tcp_timers_stop(tcp_t *tcp) 4069 { 4070 if (tcp->tcp_timer_tid != 0) { 4071 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4072 tcp->tcp_timer_tid = 0; 4073 } 4074 if (tcp->tcp_ka_tid != 0) { 4075 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4076 tcp->tcp_ka_tid = 0; 4077 } 4078 if (tcp->tcp_ack_tid != 0) { 4079 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4080 tcp->tcp_ack_tid = 0; 4081 } 4082 if (tcp->tcp_push_tid != 0) { 4083 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4084 tcp->tcp_push_tid = 0; 4085 } 4086 } 4087 4088 /* 4089 * The tcp_t is going away. Remove it from all lists and set it 4090 * to TCPS_CLOSED. The freeing up of memory is deferred until 4091 * tcp_inactive. This is needed since a thread in tcp_rput might have 4092 * done a CONN_INC_REF on this structure before it was removed from the 4093 * hashes. 4094 */ 4095 static void 4096 tcp_closei_local(tcp_t *tcp) 4097 { 4098 ire_t *ire; 4099 conn_t *connp = tcp->tcp_connp; 4100 tcp_stack_t *tcps = tcp->tcp_tcps; 4101 4102 if (!TCP_IS_SOCKET(tcp)) 4103 tcp_acceptor_hash_remove(tcp); 4104 4105 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4106 tcp->tcp_ibsegs = 0; 4107 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4108 tcp->tcp_obsegs = 0; 4109 4110 /* 4111 * If we are an eager connection hanging off a listener that 4112 * hasn't formally accepted the connection yet, get off his 4113 * list and blow off any data that we have accumulated. 4114 */ 4115 if (tcp->tcp_listener != NULL) { 4116 tcp_t *listener = tcp->tcp_listener; 4117 mutex_enter(&listener->tcp_eager_lock); 4118 /* 4119 * tcp_tconnind_started == B_TRUE means that the 4120 * conn_ind has already gone to listener. At 4121 * this point, eager will be closed but we 4122 * leave it in listeners eager list so that 4123 * if listener decides to close without doing 4124 * accept, we can clean this up. In tcp_wput_accept 4125 * we take care of the case of accept on closed 4126 * eager. 4127 */ 4128 if (!tcp->tcp_tconnind_started) { 4129 tcp_eager_unlink(tcp); 4130 mutex_exit(&listener->tcp_eager_lock); 4131 /* 4132 * We don't want to have any pointers to the 4133 * listener queue, after we have released our 4134 * reference on the listener 4135 */ 4136 ASSERT(tcps->tcps_g_q != NULL); 4137 tcp->tcp_rq = tcps->tcps_g_q; 4138 tcp->tcp_wq = WR(tcps->tcps_g_q); 4139 CONN_DEC_REF(listener->tcp_connp); 4140 } else { 4141 mutex_exit(&listener->tcp_eager_lock); 4142 } 4143 } 4144 4145 /* Stop all the timers */ 4146 tcp_timers_stop(tcp); 4147 4148 if (tcp->tcp_state == TCPS_LISTEN) { 4149 if (tcp->tcp_ip_addr_cache) { 4150 kmem_free((void *)tcp->tcp_ip_addr_cache, 4151 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4152 tcp->tcp_ip_addr_cache = NULL; 4153 } 4154 } 4155 mutex_enter(&tcp->tcp_non_sq_lock); 4156 if (tcp->tcp_flow_stopped) 4157 tcp_clrqfull(tcp); 4158 mutex_exit(&tcp->tcp_non_sq_lock); 4159 4160 tcp_bind_hash_remove(tcp); 4161 /* 4162 * If the tcp_time_wait_collector (which runs outside the squeue) 4163 * is trying to remove this tcp from the time wait list, we will 4164 * block in tcp_time_wait_remove while trying to acquire the 4165 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4166 * requires the ipcl_hash_remove to be ordered after the 4167 * tcp_time_wait_remove for the refcnt checks to work correctly. 4168 */ 4169 if (tcp->tcp_state == TCPS_TIME_WAIT) 4170 (void) tcp_time_wait_remove(tcp, NULL); 4171 CL_INET_DISCONNECT(connp, tcp); 4172 ipcl_hash_remove(connp); 4173 4174 /* 4175 * Delete the cached ire in conn_ire_cache and also mark 4176 * the conn as CONDEMNED 4177 */ 4178 mutex_enter(&connp->conn_lock); 4179 connp->conn_state_flags |= CONN_CONDEMNED; 4180 ire = connp->conn_ire_cache; 4181 connp->conn_ire_cache = NULL; 4182 mutex_exit(&connp->conn_lock); 4183 if (ire != NULL) 4184 IRE_REFRELE_NOTR(ire); 4185 4186 /* Need to cleanup any pending ioctls */ 4187 ASSERT(tcp->tcp_time_wait_next == NULL); 4188 ASSERT(tcp->tcp_time_wait_prev == NULL); 4189 ASSERT(tcp->tcp_time_wait_expire == 0); 4190 tcp->tcp_state = TCPS_CLOSED; 4191 4192 /* Release any SSL context */ 4193 if (tcp->tcp_kssl_ent != NULL) { 4194 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4195 tcp->tcp_kssl_ent = NULL; 4196 } 4197 if (tcp->tcp_kssl_ctx != NULL) { 4198 kssl_release_ctx(tcp->tcp_kssl_ctx); 4199 tcp->tcp_kssl_ctx = NULL; 4200 } 4201 tcp->tcp_kssl_pending = B_FALSE; 4202 4203 tcp_ipsec_cleanup(tcp); 4204 } 4205 4206 /* 4207 * tcp is dying (called from ipcl_conn_destroy and error cases). 4208 * Free the tcp_t in either case. 4209 */ 4210 void 4211 tcp_free(tcp_t *tcp) 4212 { 4213 mblk_t *mp; 4214 ip6_pkt_t *ipp; 4215 4216 ASSERT(tcp != NULL); 4217 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4218 4219 tcp->tcp_rq = NULL; 4220 tcp->tcp_wq = NULL; 4221 4222 tcp_close_mpp(&tcp->tcp_xmit_head); 4223 tcp_close_mpp(&tcp->tcp_reass_head); 4224 if (tcp->tcp_rcv_list != NULL) { 4225 /* Free b_next chain */ 4226 tcp_close_mpp(&tcp->tcp_rcv_list); 4227 } 4228 if ((mp = tcp->tcp_urp_mp) != NULL) { 4229 freemsg(mp); 4230 } 4231 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4232 freemsg(mp); 4233 } 4234 4235 if (tcp->tcp_fused_sigurg_mp != NULL) { 4236 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4237 freeb(tcp->tcp_fused_sigurg_mp); 4238 tcp->tcp_fused_sigurg_mp = NULL; 4239 } 4240 4241 if (tcp->tcp_ordrel_mp != NULL) { 4242 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4243 freeb(tcp->tcp_ordrel_mp); 4244 tcp->tcp_ordrel_mp = NULL; 4245 } 4246 4247 if (tcp->tcp_sack_info != NULL) { 4248 if (tcp->tcp_notsack_list != NULL) { 4249 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, 4250 tcp); 4251 } 4252 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4253 } 4254 4255 if (tcp->tcp_hopopts != NULL) { 4256 mi_free(tcp->tcp_hopopts); 4257 tcp->tcp_hopopts = NULL; 4258 tcp->tcp_hopoptslen = 0; 4259 } 4260 ASSERT(tcp->tcp_hopoptslen == 0); 4261 if (tcp->tcp_dstopts != NULL) { 4262 mi_free(tcp->tcp_dstopts); 4263 tcp->tcp_dstopts = NULL; 4264 tcp->tcp_dstoptslen = 0; 4265 } 4266 ASSERT(tcp->tcp_dstoptslen == 0); 4267 if (tcp->tcp_rtdstopts != NULL) { 4268 mi_free(tcp->tcp_rtdstopts); 4269 tcp->tcp_rtdstopts = NULL; 4270 tcp->tcp_rtdstoptslen = 0; 4271 } 4272 ASSERT(tcp->tcp_rtdstoptslen == 0); 4273 if (tcp->tcp_rthdr != NULL) { 4274 mi_free(tcp->tcp_rthdr); 4275 tcp->tcp_rthdr = NULL; 4276 tcp->tcp_rthdrlen = 0; 4277 } 4278 ASSERT(tcp->tcp_rthdrlen == 0); 4279 4280 ipp = &tcp->tcp_sticky_ipp; 4281 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4282 IPPF_RTHDR)) 4283 ip6_pkt_free(ipp); 4284 4285 /* 4286 * Free memory associated with the tcp/ip header template. 4287 */ 4288 4289 if (tcp->tcp_iphc != NULL) 4290 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4291 4292 /* 4293 * Following is really a blowing away a union. 4294 * It happens to have exactly two members of identical size 4295 * the following code is enough. 4296 */ 4297 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4298 } 4299 4300 4301 /* 4302 * Put a connection confirmation message upstream built from the 4303 * address information within 'iph' and 'tcph'. Report our success or failure. 4304 */ 4305 static boolean_t 4306 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4307 mblk_t **defermp) 4308 { 4309 sin_t sin; 4310 sin6_t sin6; 4311 mblk_t *mp; 4312 char *optp = NULL; 4313 int optlen = 0; 4314 4315 if (defermp != NULL) 4316 *defermp = NULL; 4317 4318 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4319 /* 4320 * Return in T_CONN_CON results of option negotiation through 4321 * the T_CONN_REQ. Note: If there is an real end-to-end option 4322 * negotiation, then what is received from remote end needs 4323 * to be taken into account but there is no such thing (yet?) 4324 * in our TCP/IP. 4325 * Note: We do not use mi_offset_param() here as 4326 * tcp_opts_conn_req contents do not directly come from 4327 * an application and are either generated in kernel or 4328 * from user input that was already verified. 4329 */ 4330 mp = tcp->tcp_conn.tcp_opts_conn_req; 4331 optp = (char *)(mp->b_rptr + 4332 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4333 optlen = (int) 4334 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4335 } 4336 4337 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4338 ipha_t *ipha = (ipha_t *)iphdr; 4339 4340 /* packet is IPv4 */ 4341 if (tcp->tcp_family == AF_INET) { 4342 sin = sin_null; 4343 sin.sin_addr.s_addr = ipha->ipha_src; 4344 sin.sin_port = *(uint16_t *)tcph->th_lport; 4345 sin.sin_family = AF_INET; 4346 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4347 (int)sizeof (sin_t), optp, optlen); 4348 } else { 4349 sin6 = sin6_null; 4350 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4351 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4352 sin6.sin6_family = AF_INET6; 4353 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4354 (int)sizeof (sin6_t), optp, optlen); 4355 4356 } 4357 } else { 4358 ip6_t *ip6h = (ip6_t *)iphdr; 4359 4360 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4361 ASSERT(tcp->tcp_family == AF_INET6); 4362 sin6 = sin6_null; 4363 sin6.sin6_addr = ip6h->ip6_src; 4364 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4365 sin6.sin6_family = AF_INET6; 4366 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4367 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4368 (int)sizeof (sin6_t), optp, optlen); 4369 } 4370 4371 if (!mp) 4372 return (B_FALSE); 4373 4374 mblk_copycred(mp, idmp); 4375 4376 if (defermp == NULL) { 4377 conn_t *connp = tcp->tcp_connp; 4378 if (IPCL_IS_NONSTR(connp)) { 4379 cred_t *cr; 4380 pid_t cpid; 4381 4382 cr = msg_getcred(mp, &cpid); 4383 (*connp->conn_upcalls->su_connected) 4384 (connp->conn_upper_handle, tcp->tcp_connid, cr, 4385 cpid); 4386 freemsg(mp); 4387 } else { 4388 putnext(tcp->tcp_rq, mp); 4389 } 4390 } else { 4391 *defermp = mp; 4392 } 4393 4394 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4395 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4396 return (B_TRUE); 4397 } 4398 4399 /* 4400 * Defense for the SYN attack - 4401 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4402 * one from the list of droppable eagers. This list is a subset of q0. 4403 * see comments before the definition of MAKE_DROPPABLE(). 4404 * 2. Don't drop a SYN request before its first timeout. This gives every 4405 * request at least til the first timeout to complete its 3-way handshake. 4406 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4407 * requests currently on the queue that has timed out. This will be used 4408 * as an indicator of whether an attack is under way, so that appropriate 4409 * actions can be taken. (It's incremented in tcp_timer() and decremented 4410 * either when eager goes into ESTABLISHED, or gets freed up.) 4411 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4412 * # of timeout drops back to <= q0len/32 => SYN alert off 4413 */ 4414 static boolean_t 4415 tcp_drop_q0(tcp_t *tcp) 4416 { 4417 tcp_t *eager; 4418 mblk_t *mp; 4419 tcp_stack_t *tcps = tcp->tcp_tcps; 4420 4421 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4422 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4423 4424 /* Pick oldest eager from the list of droppable eagers */ 4425 eager = tcp->tcp_eager_prev_drop_q0; 4426 4427 /* If list is empty. return B_FALSE */ 4428 if (eager == tcp) { 4429 return (B_FALSE); 4430 } 4431 4432 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4433 if ((mp = allocb(0, BPRI_HI)) == NULL) 4434 return (B_FALSE); 4435 4436 /* 4437 * Take this eager out from the list of droppable eagers since we are 4438 * going to drop it. 4439 */ 4440 MAKE_UNDROPPABLE(eager); 4441 4442 if (tcp->tcp_debug) { 4443 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4444 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4445 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4446 tcp->tcp_conn_req_cnt_q0, 4447 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4448 } 4449 4450 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4451 4452 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4453 CONN_INC_REF(eager->tcp_connp); 4454 4455 /* Mark the IRE created for this SYN request temporary */ 4456 tcp_ip_ire_mark_advice(eager); 4457 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 4458 tcp_clean_death_wrapper, eager->tcp_connp, 4459 SQ_FILL, SQTAG_TCP_DROP_Q0); 4460 4461 return (B_TRUE); 4462 } 4463 4464 int 4465 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4466 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4467 { 4468 tcp_t *ltcp = lconnp->conn_tcp; 4469 tcp_t *tcp = connp->conn_tcp; 4470 mblk_t *tpi_mp; 4471 ipha_t *ipha; 4472 ip6_t *ip6h; 4473 sin6_t sin6; 4474 in6_addr_t v6dst; 4475 int err; 4476 int ifindex = 0; 4477 tcp_stack_t *tcps = tcp->tcp_tcps; 4478 4479 if (ipvers == IPV4_VERSION) { 4480 ipha = (ipha_t *)mp->b_rptr; 4481 4482 connp->conn_send = ip_output; 4483 connp->conn_recv = tcp_input; 4484 4485 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4486 &connp->conn_bound_source_v6); 4487 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4488 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4489 4490 sin6 = sin6_null; 4491 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4492 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4493 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4494 sin6.sin6_family = AF_INET6; 4495 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4496 lconnp->conn_zoneid, tcps->tcps_netstack); 4497 if (tcp->tcp_recvdstaddr) { 4498 sin6_t sin6d; 4499 4500 sin6d = sin6_null; 4501 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4502 &sin6d.sin6_addr); 4503 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4504 sin6d.sin6_family = AF_INET; 4505 tpi_mp = mi_tpi_extconn_ind(NULL, 4506 (char *)&sin6d, sizeof (sin6_t), 4507 (char *)&tcp, 4508 (t_scalar_t)sizeof (intptr_t), 4509 (char *)&sin6d, sizeof (sin6_t), 4510 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4511 } else { 4512 tpi_mp = mi_tpi_conn_ind(NULL, 4513 (char *)&sin6, sizeof (sin6_t), 4514 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4515 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4516 } 4517 } else { 4518 ip6h = (ip6_t *)mp->b_rptr; 4519 4520 connp->conn_send = ip_output_v6; 4521 connp->conn_recv = tcp_input; 4522 4523 connp->conn_bound_source_v6 = ip6h->ip6_dst; 4524 connp->conn_srcv6 = ip6h->ip6_dst; 4525 connp->conn_remv6 = ip6h->ip6_src; 4526 4527 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4528 ifindex = (int)DB_CKSUMSTUFF(mp); 4529 DB_CKSUMSTUFF(mp) = 0; 4530 4531 sin6 = sin6_null; 4532 sin6.sin6_addr = ip6h->ip6_src; 4533 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4534 sin6.sin6_family = AF_INET6; 4535 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4536 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4537 lconnp->conn_zoneid, tcps->tcps_netstack); 4538 4539 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4540 /* Pass up the scope_id of remote addr */ 4541 sin6.sin6_scope_id = ifindex; 4542 } else { 4543 sin6.sin6_scope_id = 0; 4544 } 4545 if (tcp->tcp_recvdstaddr) { 4546 sin6_t sin6d; 4547 4548 sin6d = sin6_null; 4549 sin6.sin6_addr = ip6h->ip6_dst; 4550 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4551 sin6d.sin6_family = AF_INET; 4552 tpi_mp = mi_tpi_extconn_ind(NULL, 4553 (char *)&sin6d, sizeof (sin6_t), 4554 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4555 (char *)&sin6d, sizeof (sin6_t), 4556 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4557 } else { 4558 tpi_mp = mi_tpi_conn_ind(NULL, 4559 (char *)&sin6, sizeof (sin6_t), 4560 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4561 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4562 } 4563 } 4564 4565 if (tpi_mp == NULL) 4566 return (ENOMEM); 4567 4568 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4569 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4570 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4571 connp->conn_fully_bound = B_FALSE; 4572 4573 /* Inherit information from the "parent" */ 4574 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4575 tcp->tcp_family = ltcp->tcp_family; 4576 4577 tcp->tcp_wq = ltcp->tcp_wq; 4578 tcp->tcp_rq = ltcp->tcp_rq; 4579 4580 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4581 tcp->tcp_detached = B_TRUE; 4582 SOCK_CONNID_INIT(tcp->tcp_connid); 4583 if ((err = tcp_init_values(tcp)) != 0) { 4584 freemsg(tpi_mp); 4585 return (err); 4586 } 4587 4588 if (ipvers == IPV4_VERSION) { 4589 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4590 freemsg(tpi_mp); 4591 return (err); 4592 } 4593 ASSERT(tcp->tcp_ipha != NULL); 4594 } else { 4595 /* ifindex must be already set */ 4596 ASSERT(ifindex != 0); 4597 4598 if (ltcp->tcp_bound_if != 0) 4599 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4600 else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) 4601 tcp->tcp_bound_if = ifindex; 4602 4603 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4604 tcp->tcp_recvifindex = 0; 4605 tcp->tcp_recvhops = 0xffffffffU; 4606 ASSERT(tcp->tcp_ip6h != NULL); 4607 } 4608 4609 tcp->tcp_lport = ltcp->tcp_lport; 4610 4611 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4612 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4613 /* 4614 * Listener had options of some sort; eager inherits. 4615 * Free up the eager template and allocate one 4616 * of the right size. 4617 */ 4618 if (tcp->tcp_hdr_grown) { 4619 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4620 } else { 4621 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4622 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4623 } 4624 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4625 KM_NOSLEEP); 4626 if (tcp->tcp_iphc == NULL) { 4627 tcp->tcp_iphc_len = 0; 4628 freemsg(tpi_mp); 4629 return (ENOMEM); 4630 } 4631 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4632 tcp->tcp_hdr_grown = B_TRUE; 4633 } 4634 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4635 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4636 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4637 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4638 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4639 4640 /* 4641 * Copy the IP+TCP header template from listener to eager 4642 */ 4643 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4644 if (tcp->tcp_ipversion == IPV6_VERSION) { 4645 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4646 IPPROTO_RAW) { 4647 tcp->tcp_ip6h = 4648 (ip6_t *)(tcp->tcp_iphc + 4649 sizeof (ip6i_t)); 4650 } else { 4651 tcp->tcp_ip6h = 4652 (ip6_t *)(tcp->tcp_iphc); 4653 } 4654 tcp->tcp_ipha = NULL; 4655 } else { 4656 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4657 tcp->tcp_ip6h = NULL; 4658 } 4659 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4660 tcp->tcp_ip_hdr_len); 4661 } else { 4662 /* 4663 * only valid case when ipversion of listener and 4664 * eager differ is when listener is IPv6 and 4665 * eager is IPv4. 4666 * Eager header template has been initialized to the 4667 * maximum v4 header sizes, which includes space for 4668 * TCP and IP options. 4669 */ 4670 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 4671 (tcp->tcp_ipversion == IPV4_VERSION)); 4672 ASSERT(tcp->tcp_iphc_len >= 4673 TCP_MAX_COMBINED_HEADER_LENGTH); 4674 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4675 /* copy IP header fields individually */ 4676 tcp->tcp_ipha->ipha_ttl = 4677 ltcp->tcp_ip6h->ip6_hops; 4678 bcopy(ltcp->tcp_tcph->th_lport, 4679 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 4680 } 4681 4682 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4683 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 4684 sizeof (in_port_t)); 4685 4686 if (ltcp->tcp_lport == 0) { 4687 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 4688 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 4689 sizeof (in_port_t)); 4690 } 4691 4692 if (tcp->tcp_ipversion == IPV4_VERSION) { 4693 ASSERT(ipha != NULL); 4694 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4695 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4696 4697 /* Source routing option copyover (reverse it) */ 4698 if (tcps->tcps_rev_src_routes) 4699 tcp_opt_reverse(tcp, ipha); 4700 } else { 4701 ASSERT(ip6h != NULL); 4702 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 4703 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 4704 } 4705 4706 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4707 ASSERT(!tcp->tcp_tconnind_started); 4708 /* 4709 * If the SYN contains a credential, it's a loopback packet; attach 4710 * the credential to the TPI message. 4711 */ 4712 mblk_copycred(tpi_mp, idmp); 4713 4714 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4715 4716 /* Inherit the listener's SSL protection state */ 4717 4718 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4719 kssl_hold_ent(tcp->tcp_kssl_ent); 4720 tcp->tcp_kssl_pending = B_TRUE; 4721 } 4722 4723 /* Inherit the listener's non-STREAMS flag */ 4724 if (IPCL_IS_NONSTR(lconnp)) { 4725 connp->conn_flags |= IPCL_NONSTR; 4726 } 4727 4728 return (0); 4729 } 4730 4731 4732 int 4733 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 4734 tcph_t *tcph, mblk_t *idmp) 4735 { 4736 tcp_t *ltcp = lconnp->conn_tcp; 4737 tcp_t *tcp = connp->conn_tcp; 4738 sin_t sin; 4739 mblk_t *tpi_mp = NULL; 4740 int err; 4741 tcp_stack_t *tcps = tcp->tcp_tcps; 4742 4743 sin = sin_null; 4744 sin.sin_addr.s_addr = ipha->ipha_src; 4745 sin.sin_port = *(uint16_t *)tcph->th_lport; 4746 sin.sin_family = AF_INET; 4747 if (ltcp->tcp_recvdstaddr) { 4748 sin_t sind; 4749 4750 sind = sin_null; 4751 sind.sin_addr.s_addr = ipha->ipha_dst; 4752 sind.sin_port = *(uint16_t *)tcph->th_fport; 4753 sind.sin_family = AF_INET; 4754 tpi_mp = mi_tpi_extconn_ind(NULL, 4755 (char *)&sind, sizeof (sin_t), (char *)&tcp, 4756 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 4757 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4758 } else { 4759 tpi_mp = mi_tpi_conn_ind(NULL, 4760 (char *)&sin, sizeof (sin_t), 4761 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4762 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4763 } 4764 4765 if (tpi_mp == NULL) { 4766 return (ENOMEM); 4767 } 4768 4769 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 4770 connp->conn_send = ip_output; 4771 connp->conn_recv = tcp_input; 4772 connp->conn_fully_bound = B_FALSE; 4773 4774 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_bound_source_v6); 4775 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4776 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4777 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4778 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4779 4780 /* Inherit information from the "parent" */ 4781 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4782 tcp->tcp_family = ltcp->tcp_family; 4783 tcp->tcp_wq = ltcp->tcp_wq; 4784 tcp->tcp_rq = ltcp->tcp_rq; 4785 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 4786 tcp->tcp_detached = B_TRUE; 4787 SOCK_CONNID_INIT(tcp->tcp_connid); 4788 if ((err = tcp_init_values(tcp)) != 0) { 4789 freemsg(tpi_mp); 4790 return (err); 4791 } 4792 4793 /* 4794 * Let's make sure that eager tcp template has enough space to 4795 * copy IPv4 listener's tcp template. Since the conn_t structure is 4796 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 4797 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 4798 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 4799 * extension headers or with ip6i_t struct). Note that bcopy() below 4800 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 4801 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 4802 */ 4803 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 4804 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 4805 4806 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4807 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4808 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4809 tcp->tcp_ttl = ltcp->tcp_ttl; 4810 tcp->tcp_tos = ltcp->tcp_tos; 4811 4812 /* Copy the IP+TCP header template from listener to eager */ 4813 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4814 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4815 tcp->tcp_ip6h = NULL; 4816 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4817 tcp->tcp_ip_hdr_len); 4818 4819 /* Initialize the IP addresses and Ports */ 4820 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4821 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4822 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4823 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 4824 4825 /* Source routing option copyover (reverse it) */ 4826 if (tcps->tcps_rev_src_routes) 4827 tcp_opt_reverse(tcp, ipha); 4828 4829 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4830 ASSERT(!tcp->tcp_tconnind_started); 4831 4832 /* 4833 * If the SYN contains a credential, it's a loopback packet; attach 4834 * the credential to the TPI message. 4835 */ 4836 mblk_copycred(tpi_mp, idmp); 4837 4838 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4839 4840 /* Inherit the listener's SSL protection state */ 4841 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4842 kssl_hold_ent(tcp->tcp_kssl_ent); 4843 tcp->tcp_kssl_pending = B_TRUE; 4844 } 4845 4846 /* Inherit the listener's non-STREAMS flag */ 4847 if (IPCL_IS_NONSTR(lconnp)) { 4848 connp->conn_flags |= IPCL_NONSTR; 4849 } 4850 4851 return (0); 4852 } 4853 4854 /* 4855 * sets up conn for ipsec. 4856 * if the first mblk is M_CTL it is consumed and mpp is updated. 4857 * in case of error mpp is freed. 4858 */ 4859 conn_t * 4860 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 4861 { 4862 conn_t *connp = tcp->tcp_connp; 4863 conn_t *econnp; 4864 squeue_t *new_sqp; 4865 mblk_t *first_mp = *mpp; 4866 mblk_t *mp = *mpp; 4867 boolean_t mctl_present = B_FALSE; 4868 uint_t ipvers; 4869 4870 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 4871 if (econnp == NULL) { 4872 freemsg(first_mp); 4873 return (NULL); 4874 } 4875 if (DB_TYPE(mp) == M_CTL) { 4876 if (mp->b_cont == NULL || 4877 mp->b_cont->b_datap->db_type != M_DATA) { 4878 freemsg(first_mp); 4879 return (NULL); 4880 } 4881 mp = mp->b_cont; 4882 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 4883 freemsg(first_mp); 4884 return (NULL); 4885 } 4886 4887 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 4888 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4889 mctl_present = B_TRUE; 4890 } else { 4891 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 4892 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4893 } 4894 4895 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 4896 DB_CKSUMSTART(mp) = 0; 4897 4898 ASSERT(OK_32PTR(mp->b_rptr)); 4899 ipvers = IPH_HDR_VERSION(mp->b_rptr); 4900 if (ipvers == IPV4_VERSION) { 4901 uint16_t *up; 4902 uint32_t ports; 4903 ipha_t *ipha; 4904 4905 ipha = (ipha_t *)mp->b_rptr; 4906 up = (uint16_t *)((uchar_t *)ipha + 4907 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 4908 ports = *(uint32_t *)up; 4909 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 4910 ipha->ipha_dst, ipha->ipha_src, ports); 4911 } else { 4912 uint16_t *up; 4913 uint32_t ports; 4914 uint16_t ip_hdr_len; 4915 uint8_t *nexthdrp; 4916 ip6_t *ip6h; 4917 tcph_t *tcph; 4918 4919 ip6h = (ip6_t *)mp->b_rptr; 4920 if (ip6h->ip6_nxt == IPPROTO_TCP) { 4921 ip_hdr_len = IPV6_HDR_LEN; 4922 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 4923 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 4924 CONN_DEC_REF(econnp); 4925 freemsg(first_mp); 4926 return (NULL); 4927 } 4928 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 4929 up = (uint16_t *)tcph->th_lport; 4930 ports = *(uint32_t *)up; 4931 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 4932 ip6h->ip6_dst, ip6h->ip6_src, ports); 4933 } 4934 4935 /* 4936 * The caller already ensured that there is a sqp present. 4937 */ 4938 econnp->conn_sqp = new_sqp; 4939 econnp->conn_initial_sqp = new_sqp; 4940 4941 if (connp->conn_policy != NULL) { 4942 ipsec_in_t *ii; 4943 ii = (ipsec_in_t *)(first_mp->b_rptr); 4944 ASSERT(ii->ipsec_in_policy == NULL); 4945 IPPH_REFHOLD(connp->conn_policy); 4946 ii->ipsec_in_policy = connp->conn_policy; 4947 4948 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 4949 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 4950 CONN_DEC_REF(econnp); 4951 freemsg(first_mp); 4952 return (NULL); 4953 } 4954 } 4955 4956 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 4957 CONN_DEC_REF(econnp); 4958 freemsg(first_mp); 4959 return (NULL); 4960 } 4961 4962 /* 4963 * If we know we have some policy, pass the "IPSEC" 4964 * options size TCP uses this adjust the MSS. 4965 */ 4966 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 4967 if (mctl_present) { 4968 freeb(first_mp); 4969 *mpp = mp; 4970 } 4971 4972 return (econnp); 4973 } 4974 4975 /* 4976 * tcp_get_conn/tcp_free_conn 4977 * 4978 * tcp_get_conn is used to get a clean tcp connection structure. 4979 * It tries to reuse the connections put on the freelist by the 4980 * time_wait_collector failing which it goes to kmem_cache. This 4981 * way has two benefits compared to just allocating from and 4982 * freeing to kmem_cache. 4983 * 1) The time_wait_collector can free (which includes the cleanup) 4984 * outside the squeue. So when the interrupt comes, we have a clean 4985 * connection sitting in the freelist. Obviously, this buys us 4986 * performance. 4987 * 4988 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 4989 * has multiple disadvantages - tying up the squeue during alloc, and the 4990 * fact that IPSec policy initialization has to happen here which 4991 * requires us sending a M_CTL and checking for it i.e. real ugliness. 4992 * But allocating the conn/tcp in IP land is also not the best since 4993 * we can't check the 'q' and 'q0' which are protected by squeue and 4994 * blindly allocate memory which might have to be freed here if we are 4995 * not allowed to accept the connection. By using the freelist and 4996 * putting the conn/tcp back in freelist, we don't pay a penalty for 4997 * allocating memory without checking 'q/q0' and freeing it if we can't 4998 * accept the connection. 4999 * 5000 * Care should be taken to put the conn back in the same squeue's freelist 5001 * from which it was allocated. Best results are obtained if conn is 5002 * allocated from listener's squeue and freed to the same. Time wait 5003 * collector will free up the freelist is the connection ends up sitting 5004 * there for too long. 5005 */ 5006 void * 5007 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5008 { 5009 tcp_t *tcp = NULL; 5010 conn_t *connp = NULL; 5011 squeue_t *sqp = (squeue_t *)arg; 5012 tcp_squeue_priv_t *tcp_time_wait; 5013 netstack_t *ns; 5014 mblk_t *tcp_rsrv_mp = NULL; 5015 5016 tcp_time_wait = 5017 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5018 5019 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5020 tcp = tcp_time_wait->tcp_free_list; 5021 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5022 if (tcp != NULL) { 5023 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5024 tcp_time_wait->tcp_free_list_cnt--; 5025 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5026 tcp->tcp_time_wait_next = NULL; 5027 connp = tcp->tcp_connp; 5028 connp->conn_flags |= IPCL_REUSED; 5029 5030 ASSERT(tcp->tcp_tcps == NULL); 5031 ASSERT(connp->conn_netstack == NULL); 5032 ASSERT(tcp->tcp_rsrv_mp != NULL); 5033 ns = tcps->tcps_netstack; 5034 netstack_hold(ns); 5035 connp->conn_netstack = ns; 5036 tcp->tcp_tcps = tcps; 5037 TCPS_REFHOLD(tcps); 5038 ipcl_globalhash_insert(connp); 5039 return ((void *)connp); 5040 } 5041 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5042 /* 5043 * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed until 5044 * this conn_t/tcp_t is freed at ipcl_conn_destroy(). 5045 */ 5046 tcp_rsrv_mp = allocb(0, BPRI_HI); 5047 if (tcp_rsrv_mp == NULL) 5048 return (NULL); 5049 5050 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5051 tcps->tcps_netstack)) == NULL) { 5052 freeb(tcp_rsrv_mp); 5053 return (NULL); 5054 } 5055 5056 tcp = connp->conn_tcp; 5057 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 5058 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL); 5059 5060 tcp->tcp_tcps = tcps; 5061 TCPS_REFHOLD(tcps); 5062 5063 return ((void *)connp); 5064 } 5065 5066 /* 5067 * Update the cached label for the given tcp_t. This should be called once per 5068 * connection, and before any packets are sent or tcp_process_options is 5069 * invoked. Returns B_FALSE if the correct label could not be constructed. 5070 */ 5071 static boolean_t 5072 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5073 { 5074 conn_t *connp = tcp->tcp_connp; 5075 5076 if (tcp->tcp_ipversion == IPV4_VERSION) { 5077 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5078 int added; 5079 5080 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5081 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5082 return (B_FALSE); 5083 5084 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5085 if (added == -1) 5086 return (B_FALSE); 5087 tcp->tcp_hdr_len += added; 5088 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5089 tcp->tcp_ip_hdr_len += added; 5090 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5091 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5092 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5093 tcp->tcp_hdr_len); 5094 if (added == -1) 5095 return (B_FALSE); 5096 tcp->tcp_hdr_len += added; 5097 tcp->tcp_tcph = (tcph_t *) 5098 ((uchar_t *)tcp->tcp_tcph + added); 5099 tcp->tcp_ip_hdr_len += added; 5100 } 5101 } else { 5102 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5103 5104 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5105 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5106 return (B_FALSE); 5107 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5108 &tcp->tcp_label_len, optbuf) != 0) 5109 return (B_FALSE); 5110 if (tcp_build_hdrs(tcp) != 0) 5111 return (B_FALSE); 5112 } 5113 5114 connp->conn_ulp_labeled = 1; 5115 5116 return (B_TRUE); 5117 } 5118 5119 /* BEGIN CSTYLED */ 5120 /* 5121 * 5122 * The sockfs ACCEPT path: 5123 * ======================= 5124 * 5125 * The eager is now established in its own perimeter as soon as SYN is 5126 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5127 * completes the accept processing on the acceptor STREAM. The sending 5128 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5129 * listener but a TLI/XTI listener completes the accept processing 5130 * on the listener perimeter. 5131 * 5132 * Common control flow for 3 way handshake: 5133 * ---------------------------------------- 5134 * 5135 * incoming SYN (listener perimeter) -> tcp_rput_data() 5136 * -> tcp_conn_request() 5137 * 5138 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5139 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5140 * 5141 * Sockfs ACCEPT Path: 5142 * ------------------- 5143 * 5144 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5145 * as STREAM entry point) 5146 * 5147 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5148 * 5149 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5150 * association (we are not behind eager's squeue but sockfs is protecting us 5151 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5152 * is changed to point at tcp_wput(). 5153 * 5154 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5155 * listener (done on listener's perimeter). 5156 * 5157 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5158 * accept. 5159 * 5160 * TLI/XTI client ACCEPT path: 5161 * --------------------------- 5162 * 5163 * soaccept() sends T_CONN_RES on the listener STREAM. 5164 * 5165 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5166 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5167 * 5168 * Locks: 5169 * ====== 5170 * 5171 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5172 * and listeners->tcp_eager_next_q. 5173 * 5174 * Referencing: 5175 * ============ 5176 * 5177 * 1) We start out in tcp_conn_request by eager placing a ref on 5178 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5179 * 5180 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5181 * doing so we place a ref on the eager. This ref is finally dropped at the 5182 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5183 * reference is dropped by the squeue framework. 5184 * 5185 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5186 * 5187 * The reference must be released by the same entity that added the reference 5188 * In the above scheme, the eager is the entity that adds and releases the 5189 * references. Note that tcp_accept_finish executes in the squeue of the eager 5190 * (albeit after it is attached to the acceptor stream). Though 1. executes 5191 * in the listener's squeue, the eager is nascent at this point and the 5192 * reference can be considered to have been added on behalf of the eager. 5193 * 5194 * Eager getting a Reset or listener closing: 5195 * ========================================== 5196 * 5197 * Once the listener and eager are linked, the listener never does the unlink. 5198 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5199 * a message on all eager perimeter. The eager then does the unlink, clears 5200 * any pointers to the listener's queue and drops the reference to the 5201 * listener. The listener waits in tcp_close outside the squeue until its 5202 * refcount has dropped to 1. This ensures that the listener has waited for 5203 * all eagers to clear their association with the listener. 5204 * 5205 * Similarly, if eager decides to go away, it can unlink itself and close. 5206 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5207 * the reference to eager is still valid because of the extra ref we put 5208 * in tcp_send_conn_ind. 5209 * 5210 * Listener can always locate the eager under the protection 5211 * of the listener->tcp_eager_lock, and then do a refhold 5212 * on the eager during the accept processing. 5213 * 5214 * The acceptor stream accesses the eager in the accept processing 5215 * based on the ref placed on eager before sending T_conn_ind. 5216 * The only entity that can negate this refhold is a listener close 5217 * which is mutually exclusive with an active acceptor stream. 5218 * 5219 * Eager's reference on the listener 5220 * =================================== 5221 * 5222 * If the accept happens (even on a closed eager) the eager drops its 5223 * reference on the listener at the start of tcp_accept_finish. If the 5224 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5225 * the reference is dropped in tcp_closei_local. If the listener closes, 5226 * the reference is dropped in tcp_eager_kill. In all cases the reference 5227 * is dropped while executing in the eager's context (squeue). 5228 */ 5229 /* END CSTYLED */ 5230 5231 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5232 5233 /* 5234 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5235 * tcp_rput_data will not see any SYN packets. 5236 */ 5237 /* ARGSUSED */ 5238 void 5239 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5240 { 5241 tcph_t *tcph; 5242 uint32_t seg_seq; 5243 tcp_t *eager; 5244 uint_t ipvers; 5245 ipha_t *ipha; 5246 ip6_t *ip6h; 5247 int err; 5248 conn_t *econnp = NULL; 5249 squeue_t *new_sqp; 5250 mblk_t *mp1; 5251 uint_t ip_hdr_len; 5252 conn_t *connp = (conn_t *)arg; 5253 tcp_t *tcp = connp->conn_tcp; 5254 cred_t *credp; 5255 tcp_stack_t *tcps = tcp->tcp_tcps; 5256 ip_stack_t *ipst; 5257 5258 if (tcp->tcp_state != TCPS_LISTEN) 5259 goto error2; 5260 5261 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5262 5263 mutex_enter(&tcp->tcp_eager_lock); 5264 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5265 mutex_exit(&tcp->tcp_eager_lock); 5266 TCP_STAT(tcps, tcp_listendrop); 5267 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5268 if (tcp->tcp_debug) { 5269 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5270 "tcp_conn_request: listen backlog (max=%d) " 5271 "overflow (%d pending) on %s", 5272 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5273 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5274 } 5275 goto error2; 5276 } 5277 5278 if (tcp->tcp_conn_req_cnt_q0 >= 5279 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5280 /* 5281 * Q0 is full. Drop a pending half-open req from the queue 5282 * to make room for the new SYN req. Also mark the time we 5283 * drop a SYN. 5284 * 5285 * A more aggressive defense against SYN attack will 5286 * be to set the "tcp_syn_defense" flag now. 5287 */ 5288 TCP_STAT(tcps, tcp_listendropq0); 5289 tcp->tcp_last_rcv_lbolt = lbolt64; 5290 if (!tcp_drop_q0(tcp)) { 5291 mutex_exit(&tcp->tcp_eager_lock); 5292 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5293 if (tcp->tcp_debug) { 5294 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5295 "tcp_conn_request: listen half-open queue " 5296 "(max=%d) full (%d pending) on %s", 5297 tcps->tcps_conn_req_max_q0, 5298 tcp->tcp_conn_req_cnt_q0, 5299 tcp_display(tcp, NULL, 5300 DISP_PORT_ONLY)); 5301 } 5302 goto error2; 5303 } 5304 } 5305 mutex_exit(&tcp->tcp_eager_lock); 5306 5307 /* 5308 * IP adds STRUIO_EAGER and ensures that the received packet is 5309 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5310 * link local address. If IPSec is enabled, db_struioflag has 5311 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5312 * otherwise an error case if neither of them is set. 5313 */ 5314 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5315 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5316 DB_CKSUMSTART(mp) = 0; 5317 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5318 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5319 if (econnp == NULL) 5320 goto error2; 5321 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5322 econnp->conn_sqp = new_sqp; 5323 econnp->conn_initial_sqp = new_sqp; 5324 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5325 /* 5326 * mp is updated in tcp_get_ipsec_conn(). 5327 */ 5328 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5329 if (econnp == NULL) { 5330 /* 5331 * mp freed by tcp_get_ipsec_conn. 5332 */ 5333 return; 5334 } 5335 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5336 } else { 5337 goto error2; 5338 } 5339 5340 ASSERT(DB_TYPE(mp) == M_DATA); 5341 5342 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5343 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5344 ASSERT(OK_32PTR(mp->b_rptr)); 5345 if (ipvers == IPV4_VERSION) { 5346 ipha = (ipha_t *)mp->b_rptr; 5347 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5348 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5349 } else { 5350 ip6h = (ip6_t *)mp->b_rptr; 5351 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5352 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5353 } 5354 5355 if (tcp->tcp_family == AF_INET) { 5356 ASSERT(ipvers == IPV4_VERSION); 5357 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5358 } else { 5359 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5360 } 5361 5362 if (err) 5363 goto error3; 5364 5365 eager = econnp->conn_tcp; 5366 ASSERT(eager->tcp_ordrel_mp == NULL); 5367 5368 if (!IPCL_IS_NONSTR(econnp)) { 5369 /* 5370 * Pre-allocate the T_ordrel_ind mblk for TPI socket so that 5371 * at close time, we will always have that to send up. 5372 * Otherwise, we need to do special handling in case the 5373 * allocation fails at that time. 5374 */ 5375 if ((eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) 5376 goto error3; 5377 } 5378 /* Inherit various TCP parameters from the listener */ 5379 eager->tcp_naglim = tcp->tcp_naglim; 5380 eager->tcp_first_timer_threshold = tcp->tcp_first_timer_threshold; 5381 eager->tcp_second_timer_threshold = tcp->tcp_second_timer_threshold; 5382 5383 eager->tcp_first_ctimer_threshold = tcp->tcp_first_ctimer_threshold; 5384 eager->tcp_second_ctimer_threshold = tcp->tcp_second_ctimer_threshold; 5385 5386 /* 5387 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5388 * If it does not, the eager's receive window will be set to the 5389 * listener's receive window later in this function. 5390 */ 5391 eager->tcp_rwnd = 0; 5392 5393 /* 5394 * Inherit listener's tcp_init_cwnd. Need to do this before 5395 * calling tcp_process_options() where tcp_mss_set() is called 5396 * to set the initial cwnd. 5397 */ 5398 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5399 5400 /* 5401 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5402 * zone id before the accept is completed in tcp_wput_accept(). 5403 */ 5404 econnp->conn_zoneid = connp->conn_zoneid; 5405 econnp->conn_allzones = connp->conn_allzones; 5406 5407 /* Copy nexthop information from listener to eager */ 5408 if (connp->conn_nexthop_set) { 5409 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5410 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5411 } 5412 5413 /* 5414 * TSOL: tsol_input_proc() needs the eager's cred before the 5415 * eager is accepted 5416 */ 5417 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5418 crhold(credp); 5419 5420 ASSERT(econnp->conn_effective_cred == NULL); 5421 if (is_system_labeled()) { 5422 cred_t *cr; 5423 ts_label_t *tsl; 5424 5425 /* 5426 * If this is an MLP connection or a MAC-Exempt connection 5427 * with an unlabeled node, packets are to be 5428 * exchanged using the security label of the received 5429 * SYN packet instead of the server application's label. 5430 */ 5431 if ((cr = msg_getcred(mp, NULL)) != NULL && 5432 (tsl = crgetlabel(cr)) != NULL && 5433 (connp->conn_mlp_type != mlptSingle || 5434 (connp->conn_mac_exempt == B_TRUE && 5435 (tsl->tsl_flags & TSLF_UNLABELED)))) { 5436 if ((econnp->conn_effective_cred = 5437 copycred_from_tslabel(econnp->conn_cred, 5438 tsl, KM_NOSLEEP)) != NULL) { 5439 DTRACE_PROBE2( 5440 syn_accept_peerlabel, 5441 conn_t *, econnp, cred_t *, 5442 econnp->conn_effective_cred); 5443 } else { 5444 DTRACE_PROBE3( 5445 tx__ip__log__error__set__eagercred__tcp, 5446 char *, 5447 "SYN mp(1) label on eager connp(2) failed", 5448 mblk_t *, mp, conn_t *, econnp); 5449 goto error3; 5450 } 5451 } else { 5452 DTRACE_PROBE2(syn_accept, conn_t *, 5453 econnp, cred_t *, econnp->conn_cred) 5454 } 5455 5456 /* 5457 * Verify the destination is allowed to receive packets 5458 * at the security label of the SYN-ACK we are generating. 5459 * tsol_check_dest() may create a new effective cred for 5460 * this connection with a modified label or label flags. 5461 */ 5462 if (IN6_IS_ADDR_V4MAPPED(&econnp->conn_remv6)) { 5463 uint32_t dst; 5464 IN6_V4MAPPED_TO_IPADDR(&econnp->conn_remv6, dst); 5465 err = tsol_check_dest(CONN_CRED(econnp), &dst, 5466 IPV4_VERSION, B_FALSE, &cr); 5467 } else { 5468 err = tsol_check_dest(CONN_CRED(econnp), 5469 &econnp->conn_remv6, IPV6_VERSION, 5470 B_FALSE, &cr); 5471 } 5472 if (err != 0) 5473 goto error3; 5474 if (cr != NULL) { 5475 if (econnp->conn_effective_cred != NULL) 5476 crfree(econnp->conn_effective_cred); 5477 econnp->conn_effective_cred = cr; 5478 } 5479 5480 /* 5481 * Generate the security label to be used in the text of 5482 * this connection's outgoing packets. 5483 */ 5484 if (!tcp_update_label(eager, CONN_CRED(econnp))) { 5485 DTRACE_PROBE3( 5486 tx__ip__log__error__connrequest__tcp, 5487 char *, "eager connp(1) label on SYN mp(2) failed", 5488 conn_t *, econnp, mblk_t *, mp); 5489 goto error3; 5490 } 5491 } 5492 5493 eager->tcp_hard_binding = B_TRUE; 5494 5495 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5496 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5497 5498 CL_INET_CONNECT(connp, eager, B_FALSE, err); 5499 if (err != 0) { 5500 tcp_bind_hash_remove(eager); 5501 goto error3; 5502 } 5503 5504 /* 5505 * No need to check for multicast destination since ip will only pass 5506 * up multicasts to those that have expressed interest 5507 * TODO: what about rejecting broadcasts? 5508 * Also check that source is not a multicast or broadcast address. 5509 */ 5510 eager->tcp_state = TCPS_SYN_RCVD; 5511 5512 5513 /* 5514 * There should be no ire in the mp as we are being called after 5515 * receiving the SYN. 5516 */ 5517 ASSERT(tcp_ire_mp(&mp) == NULL); 5518 5519 /* 5520 * Adapt our mss, ttl, ... according to information provided in IRE. 5521 */ 5522 5523 if (tcp_adapt_ire(eager, NULL) == 0) { 5524 /* Undo the bind_hash_insert */ 5525 tcp_bind_hash_remove(eager); 5526 goto error3; 5527 } 5528 5529 /* Process all TCP options. */ 5530 tcp_process_options(eager, tcph); 5531 5532 /* Is the other end ECN capable? */ 5533 if (tcps->tcps_ecn_permitted >= 1 && 5534 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5535 eager->tcp_ecn_ok = B_TRUE; 5536 } 5537 5538 /* 5539 * listener->tcp_rq->q_hiwat should be the default window size or a 5540 * window size changed via SO_RCVBUF option. First round up the 5541 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5542 * scale option value if needed. Call tcp_rwnd_set() to finish the 5543 * setting. 5544 * 5545 * Note if there is a rpipe metric associated with the remote host, 5546 * we should not inherit receive window size from listener. 5547 */ 5548 eager->tcp_rwnd = MSS_ROUNDUP( 5549 (eager->tcp_rwnd == 0 ? tcp->tcp_recv_hiwater: 5550 eager->tcp_rwnd), eager->tcp_mss); 5551 if (eager->tcp_snd_ws_ok) 5552 tcp_set_ws_value(eager); 5553 /* 5554 * Note that this is the only place tcp_rwnd_set() is called for 5555 * accepting a connection. We need to call it here instead of 5556 * after the 3-way handshake because we need to tell the other 5557 * side our rwnd in the SYN-ACK segment. 5558 */ 5559 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5560 5561 /* 5562 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5563 * via soaccept()->soinheritoptions() which essentially applies 5564 * all the listener options to the new STREAM. The options that we 5565 * need to take care of are: 5566 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5567 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5568 * SO_SNDBUF, SO_RCVBUF. 5569 * 5570 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5571 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5572 * tcp_maxpsz_set() gets called later from 5573 * tcp_accept_finish(), the option takes effect. 5574 * 5575 */ 5576 /* Set the TCP options */ 5577 eager->tcp_recv_hiwater = tcp->tcp_recv_hiwater; 5578 eager->tcp_recv_lowater = tcp->tcp_recv_lowater; 5579 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5580 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5581 eager->tcp_oobinline = tcp->tcp_oobinline; 5582 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5583 eager->tcp_broadcast = tcp->tcp_broadcast; 5584 eager->tcp_useloopback = tcp->tcp_useloopback; 5585 eager->tcp_dontroute = tcp->tcp_dontroute; 5586 eager->tcp_debug = tcp->tcp_debug; 5587 eager->tcp_linger = tcp->tcp_linger; 5588 eager->tcp_lingertime = tcp->tcp_lingertime; 5589 if (tcp->tcp_ka_enabled) 5590 eager->tcp_ka_enabled = 1; 5591 5592 /* Set the IP options */ 5593 econnp->conn_broadcast = connp->conn_broadcast; 5594 econnp->conn_loopback = connp->conn_loopback; 5595 econnp->conn_dontroute = connp->conn_dontroute; 5596 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5597 5598 /* Put a ref on the listener for the eager. */ 5599 CONN_INC_REF(connp); 5600 mutex_enter(&tcp->tcp_eager_lock); 5601 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5602 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5603 tcp->tcp_eager_next_q0 = eager; 5604 eager->tcp_eager_prev_q0 = tcp; 5605 5606 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5607 eager->tcp_listener = tcp; 5608 eager->tcp_saved_listener = tcp; 5609 5610 /* 5611 * Tag this detached tcp vector for later retrieval 5612 * by our listener client in tcp_accept(). 5613 */ 5614 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5615 tcp->tcp_conn_req_cnt_q0++; 5616 if (++tcp->tcp_conn_req_seqnum == -1) { 5617 /* 5618 * -1 is "special" and defined in TPI as something 5619 * that should never be used in T_CONN_IND 5620 */ 5621 ++tcp->tcp_conn_req_seqnum; 5622 } 5623 mutex_exit(&tcp->tcp_eager_lock); 5624 5625 if (tcp->tcp_syn_defense) { 5626 /* Don't drop the SYN that comes from a good IP source */ 5627 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5628 if (addr_cache != NULL && eager->tcp_remote == 5629 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5630 eager->tcp_dontdrop = B_TRUE; 5631 } 5632 } 5633 5634 /* 5635 * We need to insert the eager in its own perimeter but as soon 5636 * as we do that, we expose the eager to the classifier and 5637 * should not touch any field outside the eager's perimeter. 5638 * So do all the work necessary before inserting the eager 5639 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5640 * will succeed but undo everything if it fails. 5641 */ 5642 seg_seq = ABE32_TO_U32(tcph->th_seq); 5643 eager->tcp_irs = seg_seq; 5644 eager->tcp_rack = seg_seq; 5645 eager->tcp_rnxt = seg_seq + 1; 5646 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5647 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5648 eager->tcp_state = TCPS_SYN_RCVD; 5649 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5650 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5651 if (mp1 == NULL) { 5652 /* 5653 * Increment the ref count as we are going to 5654 * enqueueing an mp in squeue 5655 */ 5656 CONN_INC_REF(econnp); 5657 goto error; 5658 } 5659 5660 /* 5661 * Note that in theory this should use the current pid 5662 * so that getpeerucred on the client returns the actual listener 5663 * that does accept. But accept() hasn't been called yet. We could use 5664 * the pid of the process that did bind/listen on the server. 5665 * However, with common usage like inetd() the bind/listen can be done 5666 * by a different process than the accept(). 5667 * Hence we do the simple thing of using the open pid here. 5668 * Note that db_credp is set later in tcp_send_data(). 5669 */ 5670 mblk_setcred(mp1, credp, tcp->tcp_cpid); 5671 eager->tcp_cpid = tcp->tcp_cpid; 5672 eager->tcp_open_time = lbolt64; 5673 5674 /* 5675 * We need to start the rto timer. In normal case, we start 5676 * the timer after sending the packet on the wire (or at 5677 * least believing that packet was sent by waiting for 5678 * CALL_IP_WPUT() to return). Since this is the first packet 5679 * being sent on the wire for the eager, our initial tcp_rto 5680 * is at least tcp_rexmit_interval_min which is a fairly 5681 * large value to allow the algorithm to adjust slowly to large 5682 * fluctuations of RTT during first few transmissions. 5683 * 5684 * Starting the timer first and then sending the packet in this 5685 * case shouldn't make much difference since tcp_rexmit_interval_min 5686 * is of the order of several 100ms and starting the timer 5687 * first and then sending the packet will result in difference 5688 * of few micro seconds. 5689 * 5690 * Without this optimization, we are forced to hold the fanout 5691 * lock across the ipcl_bind_insert() and sending the packet 5692 * so that we don't race against an incoming packet (maybe RST) 5693 * for this eager. 5694 * 5695 * It is necessary to acquire an extra reference on the eager 5696 * at this point and hold it until after tcp_send_data() to 5697 * ensure against an eager close race. 5698 */ 5699 5700 CONN_INC_REF(eager->tcp_connp); 5701 5702 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5703 5704 /* 5705 * Insert the eager in its own perimeter now. We are ready to deal 5706 * with any packets on eager. 5707 */ 5708 if (eager->tcp_ipversion == IPV4_VERSION) { 5709 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5710 goto error; 5711 } 5712 } else { 5713 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5714 goto error; 5715 } 5716 } 5717 5718 /* mark conn as fully-bound */ 5719 econnp->conn_fully_bound = B_TRUE; 5720 5721 /* Send the SYN-ACK */ 5722 tcp_send_data(eager, eager->tcp_wq, mp1); 5723 CONN_DEC_REF(eager->tcp_connp); 5724 freemsg(mp); 5725 5726 return; 5727 error: 5728 freemsg(mp1); 5729 eager->tcp_closemp_used = B_TRUE; 5730 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 5731 mp1 = &eager->tcp_closemp; 5732 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_eager_kill, 5733 econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_2); 5734 5735 /* 5736 * If a connection already exists, send the mp to that connections so 5737 * that it can be appropriately dealt with. 5738 */ 5739 ipst = tcps->tcps_netstack->netstack_ip; 5740 5741 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 5742 if (!IPCL_IS_CONNECTED(econnp)) { 5743 /* 5744 * Something bad happened. ipcl_conn_insert() 5745 * failed because a connection already existed 5746 * in connected hash but we can't find it 5747 * anymore (someone blew it away). Just 5748 * free this message and hopefully remote 5749 * will retransmit at which time the SYN can be 5750 * treated as a new connection or dealth with 5751 * a TH_RST if a connection already exists. 5752 */ 5753 CONN_DEC_REF(econnp); 5754 freemsg(mp); 5755 } else { 5756 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp, 5757 tcp_input, econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_1); 5758 } 5759 } else { 5760 /* Nobody wants this packet */ 5761 freemsg(mp); 5762 } 5763 return; 5764 error3: 5765 CONN_DEC_REF(econnp); 5766 error2: 5767 freemsg(mp); 5768 } 5769 5770 /* 5771 * In an ideal case of vertical partition in NUMA architecture, its 5772 * beneficial to have the listener and all the incoming connections 5773 * tied to the same squeue. The other constraint is that incoming 5774 * connections should be tied to the squeue attached to interrupted 5775 * CPU for obvious locality reason so this leaves the listener to 5776 * be tied to the same squeue. Our only problem is that when listener 5777 * is binding, the CPU that will get interrupted by the NIC whose 5778 * IP address the listener is binding to is not even known. So 5779 * the code below allows us to change that binding at the time the 5780 * CPU is interrupted by virtue of incoming connection's squeue. 5781 * 5782 * This is usefull only in case of a listener bound to a specific IP 5783 * address. For other kind of listeners, they get bound the 5784 * very first time and there is no attempt to rebind them. 5785 */ 5786 void 5787 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 5788 { 5789 conn_t *connp = (conn_t *)arg; 5790 squeue_t *sqp = (squeue_t *)arg2; 5791 squeue_t *new_sqp; 5792 uint32_t conn_flags; 5793 5794 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5795 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5796 } else { 5797 goto done; 5798 } 5799 5800 if (connp->conn_fanout == NULL) 5801 goto done; 5802 5803 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 5804 mutex_enter(&connp->conn_fanout->connf_lock); 5805 mutex_enter(&connp->conn_lock); 5806 /* 5807 * No one from read or write side can access us now 5808 * except for already queued packets on this squeue. 5809 * But since we haven't changed the squeue yet, they 5810 * can't execute. If they are processed after we have 5811 * changed the squeue, they are sent back to the 5812 * correct squeue down below. 5813 * But a listner close can race with processing of 5814 * incoming SYN. If incoming SYN processing changes 5815 * the squeue then the listener close which is waiting 5816 * to enter the squeue would operate on the wrong 5817 * squeue. Hence we don't change the squeue here unless 5818 * the refcount is exactly the minimum refcount. The 5819 * minimum refcount of 4 is counted as - 1 each for 5820 * TCP and IP, 1 for being in the classifier hash, and 5821 * 1 for the mblk being processed. 5822 */ 5823 5824 if (connp->conn_ref != 4 || 5825 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 5826 mutex_exit(&connp->conn_lock); 5827 mutex_exit(&connp->conn_fanout->connf_lock); 5828 goto done; 5829 } 5830 if (connp->conn_sqp != new_sqp) { 5831 while (connp->conn_sqp != new_sqp) 5832 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 5833 } 5834 5835 do { 5836 conn_flags = connp->conn_flags; 5837 conn_flags |= IPCL_FULLY_BOUND; 5838 (void) cas32(&connp->conn_flags, connp->conn_flags, 5839 conn_flags); 5840 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 5841 5842 mutex_exit(&connp->conn_fanout->connf_lock); 5843 mutex_exit(&connp->conn_lock); 5844 } 5845 5846 done: 5847 if (connp->conn_sqp != sqp) { 5848 CONN_INC_REF(connp); 5849 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 5850 SQ_FILL, SQTAG_TCP_CONN_REQ_UNBOUND); 5851 } else { 5852 tcp_conn_request(connp, mp, sqp); 5853 } 5854 } 5855 5856 /* 5857 * Successful connect request processing begins when our client passes 5858 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 5859 * our T_OK_ACK reply message upstream. The control flow looks like this: 5860 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_tpi_connect() -> IP 5861 * upstream <- tcp_rput() <- IP 5862 * After various error checks are completed, tcp_tpi_connect() lays 5863 * the target address and port into the composite header template, 5864 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 5865 * request followed by an IRE request, and passes the three mblk message 5866 * down to IP looking like this: 5867 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 5868 * Processing continues in tcp_rput() when we receive the following message: 5869 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 5870 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 5871 * to fire off the connection request, and then passes the T_OK_ACK mblk 5872 * upstream that we filled in below. There are, of course, numerous 5873 * error conditions along the way which truncate the processing described 5874 * above. 5875 */ 5876 static void 5877 tcp_tpi_connect(tcp_t *tcp, mblk_t *mp) 5878 { 5879 sin_t *sin; 5880 queue_t *q = tcp->tcp_wq; 5881 struct T_conn_req *tcr; 5882 struct sockaddr *sa; 5883 socklen_t len; 5884 int error; 5885 cred_t *cr; 5886 pid_t cpid; 5887 5888 /* 5889 * All Solaris components should pass a db_credp 5890 * for this TPI message, hence we ASSERT. 5891 * But in case there is some other M_PROTO that looks 5892 * like a TPI message sent by some other kernel 5893 * component, we check and return an error. 5894 */ 5895 cr = msg_getcred(mp, &cpid); 5896 ASSERT(cr != NULL); 5897 if (cr == NULL) { 5898 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5899 return; 5900 } 5901 5902 tcr = (struct T_conn_req *)mp->b_rptr; 5903 5904 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 5905 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 5906 tcp_err_ack(tcp, mp, TPROTO, 0); 5907 return; 5908 } 5909 5910 /* 5911 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 5912 * will always have that to send up. Otherwise, we need to do 5913 * special handling in case the allocation fails at that time. 5914 * If the end point is TPI, the tcp_t can be reused and the 5915 * tcp_ordrel_mp may be allocated already. 5916 */ 5917 if (tcp->tcp_ordrel_mp == NULL) { 5918 if ((tcp->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) { 5919 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5920 return; 5921 } 5922 } 5923 5924 /* 5925 * Determine packet type based on type of address passed in 5926 * the request should contain an IPv4 or IPv6 address. 5927 * Make sure that address family matches the type of 5928 * family of the the address passed down 5929 */ 5930 switch (tcr->DEST_length) { 5931 default: 5932 tcp_err_ack(tcp, mp, TBADADDR, 0); 5933 return; 5934 5935 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 5936 /* 5937 * XXX: The check for valid DEST_length was not there 5938 * in earlier releases and some buggy 5939 * TLI apps (e.g Sybase) got away with not feeding 5940 * in sin_zero part of address. 5941 * We allow that bug to keep those buggy apps humming. 5942 * Test suites require the check on DEST_length. 5943 * We construct a new mblk with valid DEST_length 5944 * free the original so the rest of the code does 5945 * not have to keep track of this special shorter 5946 * length address case. 5947 */ 5948 mblk_t *nmp; 5949 struct T_conn_req *ntcr; 5950 sin_t *nsin; 5951 5952 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 5953 tcr->OPT_length, BPRI_HI); 5954 if (nmp == NULL) { 5955 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5956 return; 5957 } 5958 ntcr = (struct T_conn_req *)nmp->b_rptr; 5959 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 5960 ntcr->PRIM_type = T_CONN_REQ; 5961 ntcr->DEST_length = sizeof (sin_t); 5962 ntcr->DEST_offset = sizeof (struct T_conn_req); 5963 5964 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 5965 *nsin = sin_null; 5966 /* Get pointer to shorter address to copy from original mp */ 5967 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 5968 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 5969 if (sin == NULL || !OK_32PTR((char *)sin)) { 5970 freemsg(nmp); 5971 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5972 return; 5973 } 5974 nsin->sin_family = sin->sin_family; 5975 nsin->sin_port = sin->sin_port; 5976 nsin->sin_addr = sin->sin_addr; 5977 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 5978 nmp->b_wptr = (uchar_t *)&nsin[1]; 5979 if (tcr->OPT_length != 0) { 5980 ntcr->OPT_length = tcr->OPT_length; 5981 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 5982 bcopy((uchar_t *)tcr + tcr->OPT_offset, 5983 (uchar_t *)ntcr + ntcr->OPT_offset, 5984 tcr->OPT_length); 5985 nmp->b_wptr += tcr->OPT_length; 5986 } 5987 freemsg(mp); /* original mp freed */ 5988 mp = nmp; /* re-initialize original variables */ 5989 tcr = ntcr; 5990 } 5991 /* FALLTHRU */ 5992 5993 case sizeof (sin_t): 5994 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 5995 sizeof (sin_t)); 5996 len = sizeof (sin_t); 5997 break; 5998 5999 case sizeof (sin6_t): 6000 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 6001 sizeof (sin6_t)); 6002 len = sizeof (sin6_t); 6003 break; 6004 } 6005 6006 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 6007 if (error != 0) { 6008 tcp_err_ack(tcp, mp, TSYSERR, error); 6009 return; 6010 } 6011 6012 /* 6013 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6014 * should key on their sequence number and cut them loose. 6015 */ 6016 6017 /* 6018 * If options passed in, feed it for verification and handling 6019 */ 6020 if (tcr->OPT_length != 0) { 6021 mblk_t *ok_mp; 6022 mblk_t *discon_mp; 6023 mblk_t *conn_opts_mp; 6024 int t_error, sys_error, do_disconnect; 6025 6026 conn_opts_mp = NULL; 6027 6028 if (tcp_conprim_opt_process(tcp, mp, 6029 &do_disconnect, &t_error, &sys_error) < 0) { 6030 if (do_disconnect) { 6031 ASSERT(t_error == 0 && sys_error == 0); 6032 discon_mp = mi_tpi_discon_ind(NULL, 6033 ECONNREFUSED, 0); 6034 if (!discon_mp) { 6035 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6036 TSYSERR, ENOMEM); 6037 return; 6038 } 6039 ok_mp = mi_tpi_ok_ack_alloc(mp); 6040 if (!ok_mp) { 6041 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6042 TSYSERR, ENOMEM); 6043 return; 6044 } 6045 qreply(q, ok_mp); 6046 qreply(q, discon_mp); /* no flush! */ 6047 } else { 6048 ASSERT(t_error != 0); 6049 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6050 sys_error); 6051 } 6052 return; 6053 } 6054 /* 6055 * Success in setting options, the mp option buffer represented 6056 * by OPT_length/offset has been potentially modified and 6057 * contains results of option processing. We copy it in 6058 * another mp to save it for potentially influencing returning 6059 * it in T_CONN_CONN. 6060 */ 6061 if (tcr->OPT_length != 0) { /* there are resulting options */ 6062 conn_opts_mp = copyb(mp); 6063 if (!conn_opts_mp) { 6064 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6065 TSYSERR, ENOMEM); 6066 return; 6067 } 6068 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6069 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6070 /* 6071 * Note: 6072 * These resulting option negotiation can include any 6073 * end-to-end negotiation options but there no such 6074 * thing (yet?) in our TCP/IP. 6075 */ 6076 } 6077 } 6078 6079 /* call the non-TPI version */ 6080 error = tcp_do_connect(tcp->tcp_connp, sa, len, cr, cpid); 6081 if (error < 0) { 6082 mp = mi_tpi_err_ack_alloc(mp, -error, 0); 6083 } else if (error > 0) { 6084 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 6085 } else { 6086 mp = mi_tpi_ok_ack_alloc(mp); 6087 } 6088 6089 /* 6090 * Note: Code below is the "failure" case 6091 */ 6092 /* return error ack and blow away saved option results if any */ 6093 connect_failed: 6094 if (mp != NULL) 6095 putnext(tcp->tcp_rq, mp); 6096 else { 6097 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6098 TSYSERR, ENOMEM); 6099 } 6100 } 6101 6102 /* 6103 * Handle connect to IPv4 destinations, including connections for AF_INET6 6104 * sockets connecting to IPv4 mapped IPv6 destinations. 6105 */ 6106 static int 6107 tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, in_port_t dstport, 6108 uint_t srcid, cred_t *cr, pid_t pid) 6109 { 6110 tcph_t *tcph; 6111 mblk_t *mp; 6112 ipaddr_t dstaddr = *dstaddrp; 6113 int32_t oldstate; 6114 uint16_t lport; 6115 int error = 0; 6116 tcp_stack_t *tcps = tcp->tcp_tcps; 6117 6118 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6119 6120 /* Check for attempt to connect to INADDR_ANY */ 6121 if (dstaddr == INADDR_ANY) { 6122 /* 6123 * SunOS 4.x and 4.3 BSD allow an application 6124 * to connect a TCP socket to INADDR_ANY. 6125 * When they do this, the kernel picks the 6126 * address of one interface and uses it 6127 * instead. The kernel usually ends up 6128 * picking the address of the loopback 6129 * interface. This is an undocumented feature. 6130 * However, we provide the same thing here 6131 * in order to have source and binary 6132 * compatibility with SunOS 4.x. 6133 * Update the T_CONN_REQ (sin/sin6) since it is used to 6134 * generate the T_CONN_CON. 6135 */ 6136 dstaddr = htonl(INADDR_LOOPBACK); 6137 *dstaddrp = dstaddr; 6138 } 6139 6140 /* Handle __sin6_src_id if socket not bound to an IP address */ 6141 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6142 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6143 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6144 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6145 tcp->tcp_ipha->ipha_src); 6146 } 6147 6148 /* 6149 * Don't let an endpoint connect to itself. Note that 6150 * the test here does not catch the case where the 6151 * source IP addr was left unspecified by the user. In 6152 * this case, the source addr is set in tcp_adapt_ire() 6153 * using the reply to the T_BIND message that we send 6154 * down to IP here and the check is repeated in tcp_rput_other. 6155 */ 6156 if (dstaddr == tcp->tcp_ipha->ipha_src && 6157 dstport == tcp->tcp_lport) { 6158 error = -TBADADDR; 6159 goto failed; 6160 } 6161 6162 /* 6163 * Verify the destination is allowed to receive packets 6164 * at the security label of the connection we are initiating. 6165 * tsol_check_dest() may create a new effective cred for this 6166 * connection with a modified label or label flags. 6167 */ 6168 if (is_system_labeled()) { 6169 ASSERT(tcp->tcp_connp->conn_effective_cred == NULL); 6170 if ((error = tsol_check_dest(CONN_CRED(tcp->tcp_connp), 6171 &dstaddr, IPV4_VERSION, tcp->tcp_connp->conn_mac_exempt, 6172 &tcp->tcp_connp->conn_effective_cred)) != 0) { 6173 if (error != EHOSTUNREACH) 6174 error = -TSYSERR; 6175 goto failed; 6176 } 6177 } 6178 6179 tcp->tcp_ipha->ipha_dst = dstaddr; 6180 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6181 6182 /* 6183 * Massage a source route if any putting the first hop 6184 * in iph_dst. Compute a starting value for the checksum which 6185 * takes into account that the original iph_dst should be 6186 * included in the checksum but that ip will include the 6187 * first hop in the source route in the tcp checksum. 6188 */ 6189 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6190 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6191 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6192 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6193 if ((int)tcp->tcp_sum < 0) 6194 tcp->tcp_sum--; 6195 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6196 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6197 (tcp->tcp_sum >> 16)); 6198 tcph = tcp->tcp_tcph; 6199 *(uint16_t *)tcph->th_fport = dstport; 6200 tcp->tcp_fport = dstport; 6201 6202 oldstate = tcp->tcp_state; 6203 /* 6204 * At this point the remote destination address and remote port fields 6205 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6206 * have to see which state tcp was in so we can take apropriate action. 6207 */ 6208 if (oldstate == TCPS_IDLE) { 6209 /* 6210 * We support a quick connect capability here, allowing 6211 * clients to transition directly from IDLE to SYN_SENT 6212 * tcp_bindi will pick an unused port, insert the connection 6213 * in the bind hash and transition to BOUND state. 6214 */ 6215 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6216 tcp, B_TRUE); 6217 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6218 B_FALSE, B_FALSE); 6219 if (lport == 0) { 6220 error = -TNOADDR; 6221 goto failed; 6222 } 6223 } 6224 tcp->tcp_state = TCPS_SYN_SENT; 6225 6226 mp = allocb(sizeof (ire_t), BPRI_HI); 6227 if (mp == NULL) { 6228 tcp->tcp_state = oldstate; 6229 error = ENOMEM; 6230 goto failed; 6231 } 6232 6233 mp->b_wptr += sizeof (ire_t); 6234 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6235 tcp->tcp_hard_binding = 1; 6236 6237 /* 6238 * We need to make sure that the conn_recv is set to a non-null 6239 * value before we insert the conn_t into the classifier table. 6240 * This is to avoid a race with an incoming packet which does 6241 * an ipcl_classify(). 6242 */ 6243 tcp->tcp_connp->conn_recv = tcp_input; 6244 6245 if (tcp->tcp_family == AF_INET) { 6246 error = ip_proto_bind_connected_v4(tcp->tcp_connp, &mp, 6247 IPPROTO_TCP, &tcp->tcp_ipha->ipha_src, tcp->tcp_lport, 6248 tcp->tcp_remote, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6249 } else { 6250 in6_addr_t v6src; 6251 if (tcp->tcp_ipversion == IPV4_VERSION) { 6252 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6253 } else { 6254 v6src = tcp->tcp_ip6h->ip6_src; 6255 } 6256 error = ip_proto_bind_connected_v6(tcp->tcp_connp, &mp, 6257 IPPROTO_TCP, &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6258 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6259 } 6260 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6261 tcp->tcp_active_open = 1; 6262 6263 6264 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6265 failed: 6266 /* return error ack and blow away saved option results if any */ 6267 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6268 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6269 return (error); 6270 } 6271 6272 /* 6273 * Handle connect to IPv6 destinations. 6274 */ 6275 static int 6276 tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, in_port_t dstport, 6277 uint32_t flowinfo, uint_t srcid, uint32_t scope_id, cred_t *cr, pid_t pid) 6278 { 6279 tcph_t *tcph; 6280 mblk_t *mp; 6281 ip6_rthdr_t *rth; 6282 int32_t oldstate; 6283 uint16_t lport; 6284 tcp_stack_t *tcps = tcp->tcp_tcps; 6285 int error = 0; 6286 conn_t *connp = tcp->tcp_connp; 6287 6288 ASSERT(tcp->tcp_family == AF_INET6); 6289 6290 /* 6291 * If we're here, it means that the destination address is a native 6292 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6293 * reason why it might not be IPv6 is if the socket was bound to an 6294 * IPv4-mapped IPv6 address. 6295 */ 6296 if (tcp->tcp_ipversion != IPV6_VERSION) { 6297 return (-TBADADDR); 6298 } 6299 6300 /* 6301 * Interpret a zero destination to mean loopback. 6302 * Update the T_CONN_REQ (sin/sin6) since it is used to 6303 * generate the T_CONN_CON. 6304 */ 6305 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6306 *dstaddrp = ipv6_loopback; 6307 } 6308 6309 /* Handle __sin6_src_id if socket not bound to an IP address */ 6310 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6311 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6312 connp->conn_zoneid, tcps->tcps_netstack); 6313 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6314 } 6315 6316 /* 6317 * Take care of the scope_id now and add ip6i_t 6318 * if ip6i_t is not already allocated through TCP 6319 * sticky options. At this point tcp_ip6h does not 6320 * have dst info, thus use dstaddrp. 6321 */ 6322 if (scope_id != 0 && 6323 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6324 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6325 ip6i_t *ip6i; 6326 6327 ipp->ipp_ifindex = scope_id; 6328 ip6i = (ip6i_t *)tcp->tcp_iphc; 6329 6330 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6331 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6332 /* Already allocated */ 6333 ip6i->ip6i_flags |= IP6I_IFINDEX; 6334 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6335 ipp->ipp_fields |= IPPF_SCOPE_ID; 6336 } else { 6337 int reterr; 6338 6339 ipp->ipp_fields |= IPPF_SCOPE_ID; 6340 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6341 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6342 reterr = tcp_build_hdrs(tcp); 6343 if (reterr != 0) 6344 goto failed; 6345 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6346 } 6347 } 6348 6349 /* 6350 * Don't let an endpoint connect to itself. Note that 6351 * the test here does not catch the case where the 6352 * source IP addr was left unspecified by the user. In 6353 * this case, the source addr is set in tcp_adapt_ire() 6354 * using the reply to the T_BIND message that we send 6355 * down to IP here and the check is repeated in tcp_rput_other. 6356 */ 6357 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6358 (dstport == tcp->tcp_lport)) { 6359 error = -TBADADDR; 6360 goto failed; 6361 } 6362 6363 /* 6364 * Verify the destination is allowed to receive packets 6365 * at the security label of the connection we are initiating. 6366 * check_dest may create a new effective cred for this 6367 * connection with a modified label or label flags. 6368 */ 6369 if (is_system_labeled()) { 6370 ASSERT(tcp->tcp_connp->conn_effective_cred == NULL); 6371 if ((error = tsol_check_dest(CONN_CRED(tcp->tcp_connp), 6372 dstaddrp, IPV6_VERSION, tcp->tcp_connp->conn_mac_exempt, 6373 &tcp->tcp_connp->conn_effective_cred)) != 0) { 6374 if (error != EHOSTUNREACH) 6375 error = -TSYSERR; 6376 goto failed; 6377 } 6378 } 6379 6380 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6381 tcp->tcp_remote_v6 = *dstaddrp; 6382 tcp->tcp_ip6h->ip6_vcf = 6383 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6384 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6385 6386 /* 6387 * Massage a routing header (if present) putting the first hop 6388 * in ip6_dst. Compute a starting value for the checksum which 6389 * takes into account that the original ip6_dst should be 6390 * included in the checksum but that ip will include the 6391 * first hop in the source route in the tcp checksum. 6392 */ 6393 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6394 if (rth != NULL) { 6395 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6396 tcps->tcps_netstack); 6397 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6398 (tcp->tcp_sum >> 16)); 6399 } else { 6400 tcp->tcp_sum = 0; 6401 } 6402 6403 tcph = tcp->tcp_tcph; 6404 *(uint16_t *)tcph->th_fport = dstport; 6405 tcp->tcp_fport = dstport; 6406 6407 oldstate = tcp->tcp_state; 6408 /* 6409 * At this point the remote destination address and remote port fields 6410 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6411 * have to see which state tcp was in so we can take apropriate action. 6412 */ 6413 if (oldstate == TCPS_IDLE) { 6414 /* 6415 * We support a quick connect capability here, allowing 6416 * clients to transition directly from IDLE to SYN_SENT 6417 * tcp_bindi will pick an unused port, insert the connection 6418 * in the bind hash and transition to BOUND state. 6419 */ 6420 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6421 tcp, B_TRUE); 6422 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6423 B_FALSE, B_FALSE); 6424 if (lport == 0) { 6425 error = -TNOADDR; 6426 goto failed; 6427 } 6428 } 6429 tcp->tcp_state = TCPS_SYN_SENT; 6430 6431 mp = allocb(sizeof (ire_t), BPRI_HI); 6432 if (mp != NULL) { 6433 in6_addr_t v6src; 6434 6435 mp->b_wptr += sizeof (ire_t); 6436 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6437 6438 tcp->tcp_hard_binding = 1; 6439 6440 /* 6441 * We need to make sure that the conn_recv is set to a non-null 6442 * value before we insert the conn_t into the classifier table. 6443 * This is to avoid a race with an incoming packet which does 6444 * an ipcl_classify(). 6445 */ 6446 tcp->tcp_connp->conn_recv = tcp_input; 6447 6448 if (tcp->tcp_ipversion == IPV4_VERSION) { 6449 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6450 } else { 6451 v6src = tcp->tcp_ip6h->ip6_src; 6452 } 6453 error = ip_proto_bind_connected_v6(connp, &mp, IPPROTO_TCP, 6454 &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6455 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6456 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6457 tcp->tcp_active_open = 1; 6458 6459 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6460 } 6461 /* Error case */ 6462 tcp->tcp_state = oldstate; 6463 error = ENOMEM; 6464 6465 failed: 6466 /* return error ack and blow away saved option results if any */ 6467 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6468 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6469 return (error); 6470 } 6471 6472 /* 6473 * We need a stream q for detached closing tcp connections 6474 * to use. Our client hereby indicates that this q is the 6475 * one to use. 6476 */ 6477 static void 6478 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6479 { 6480 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6481 queue_t *q = tcp->tcp_wq; 6482 tcp_stack_t *tcps = tcp->tcp_tcps; 6483 6484 #ifdef NS_DEBUG 6485 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6486 tcps->tcps_netstack->netstack_stackid); 6487 #endif 6488 mp->b_datap->db_type = M_IOCACK; 6489 iocp->ioc_count = 0; 6490 mutex_enter(&tcps->tcps_g_q_lock); 6491 if (tcps->tcps_g_q != NULL) { 6492 mutex_exit(&tcps->tcps_g_q_lock); 6493 iocp->ioc_error = EALREADY; 6494 } else { 6495 int error = 0; 6496 conn_t *connp = tcp->tcp_connp; 6497 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 6498 6499 tcps->tcps_g_q = tcp->tcp_rq; 6500 mutex_exit(&tcps->tcps_g_q_lock); 6501 iocp->ioc_error = 0; 6502 iocp->ioc_rval = 0; 6503 /* 6504 * We are passing tcp_sticky_ipp as NULL 6505 * as it is not useful for tcp_default queue 6506 * 6507 * Set conn_recv just in case. 6508 */ 6509 tcp->tcp_connp->conn_recv = tcp_conn_request; 6510 6511 ASSERT(connp->conn_af_isv6); 6512 connp->conn_ulp = IPPROTO_TCP; 6513 6514 if (ipst->ips_ipcl_proto_fanout_v6[IPPROTO_TCP].connf_head != 6515 NULL || connp->conn_mac_exempt) { 6516 error = -TBADADDR; 6517 } else { 6518 connp->conn_srcv6 = ipv6_all_zeros; 6519 ipcl_proto_insert_v6(connp, IPPROTO_TCP); 6520 } 6521 6522 (void) tcp_post_ip_bind(tcp, NULL, error, NULL, 0); 6523 } 6524 qreply(q, mp); 6525 } 6526 6527 static int 6528 tcp_disconnect_common(tcp_t *tcp, t_scalar_t seqnum) 6529 { 6530 tcp_t *ltcp = NULL; 6531 conn_t *connp; 6532 tcp_stack_t *tcps = tcp->tcp_tcps; 6533 6534 /* 6535 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6536 * when the stream is in BOUND state. Do not send a reset, 6537 * since the destination IP address is not valid, and it can 6538 * be the initialized value of all zeros (broadcast address). 6539 * 6540 * XXX There won't be any pending bind request to IP. 6541 */ 6542 if (tcp->tcp_state <= TCPS_BOUND) { 6543 if (tcp->tcp_debug) { 6544 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6545 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6546 } 6547 return (TOUTSTATE); 6548 } 6549 6550 6551 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6552 6553 /* 6554 * According to TPI, for non-listeners, ignore seqnum 6555 * and disconnect. 6556 * Following interpretation of -1 seqnum is historical 6557 * and implied TPI ? (TPI only states that for T_CONN_IND, 6558 * a valid seqnum should not be -1). 6559 * 6560 * -1 means disconnect everything 6561 * regardless even on a listener. 6562 */ 6563 6564 int old_state = tcp->tcp_state; 6565 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6566 6567 /* 6568 * The connection can't be on the tcp_time_wait_head list 6569 * since it is not detached. 6570 */ 6571 ASSERT(tcp->tcp_time_wait_next == NULL); 6572 ASSERT(tcp->tcp_time_wait_prev == NULL); 6573 ASSERT(tcp->tcp_time_wait_expire == 0); 6574 ltcp = NULL; 6575 /* 6576 * If it used to be a listener, check to make sure no one else 6577 * has taken the port before switching back to LISTEN state. 6578 */ 6579 if (tcp->tcp_ipversion == IPV4_VERSION) { 6580 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6581 tcp->tcp_ipha->ipha_src, 6582 tcp->tcp_connp->conn_zoneid, ipst); 6583 if (connp != NULL) 6584 ltcp = connp->conn_tcp; 6585 } else { 6586 /* Allow tcp_bound_if listeners? */ 6587 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6588 &tcp->tcp_ip6h->ip6_src, 0, 6589 tcp->tcp_connp->conn_zoneid, ipst); 6590 if (connp != NULL) 6591 ltcp = connp->conn_tcp; 6592 } 6593 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6594 tcp->tcp_state = TCPS_LISTEN; 6595 } else if (old_state > TCPS_BOUND) { 6596 tcp->tcp_conn_req_max = 0; 6597 tcp->tcp_state = TCPS_BOUND; 6598 } 6599 if (ltcp != NULL) 6600 CONN_DEC_REF(ltcp->tcp_connp); 6601 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6602 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6603 } else if (old_state == TCPS_ESTABLISHED || 6604 old_state == TCPS_CLOSE_WAIT) { 6605 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6606 } 6607 6608 if (tcp->tcp_fused) 6609 tcp_unfuse(tcp); 6610 6611 mutex_enter(&tcp->tcp_eager_lock); 6612 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6613 (tcp->tcp_conn_req_cnt_q != 0)) { 6614 tcp_eager_cleanup(tcp, 0); 6615 } 6616 mutex_exit(&tcp->tcp_eager_lock); 6617 6618 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6619 tcp->tcp_rnxt, TH_RST | TH_ACK); 6620 6621 tcp_reinit(tcp); 6622 6623 return (0); 6624 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6625 return (TBADSEQ); 6626 } 6627 return (0); 6628 } 6629 6630 /* 6631 * Our client hereby directs us to reject the connection request 6632 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6633 * of sending the appropriate RST, not an ICMP error. 6634 */ 6635 static void 6636 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6637 { 6638 t_scalar_t seqnum; 6639 int error; 6640 6641 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6642 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6643 tcp_err_ack(tcp, mp, TPROTO, 0); 6644 return; 6645 } 6646 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6647 error = tcp_disconnect_common(tcp, seqnum); 6648 if (error != 0) 6649 tcp_err_ack(tcp, mp, error, 0); 6650 else { 6651 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6652 /* Send M_FLUSH according to TPI */ 6653 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6654 } 6655 mp = mi_tpi_ok_ack_alloc(mp); 6656 if (mp) 6657 putnext(tcp->tcp_rq, mp); 6658 } 6659 } 6660 6661 /* 6662 * Diagnostic routine used to return a string associated with the tcp state. 6663 * Note that if the caller does not supply a buffer, it will use an internal 6664 * static string. This means that if multiple threads call this function at 6665 * the same time, output can be corrupted... Note also that this function 6666 * does not check the size of the supplied buffer. The caller has to make 6667 * sure that it is big enough. 6668 */ 6669 static char * 6670 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6671 { 6672 char buf1[30]; 6673 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6674 char *buf; 6675 char *cp; 6676 in6_addr_t local, remote; 6677 char local_addrbuf[INET6_ADDRSTRLEN]; 6678 char remote_addrbuf[INET6_ADDRSTRLEN]; 6679 6680 if (sup_buf != NULL) 6681 buf = sup_buf; 6682 else 6683 buf = priv_buf; 6684 6685 if (tcp == NULL) 6686 return ("NULL_TCP"); 6687 switch (tcp->tcp_state) { 6688 case TCPS_CLOSED: 6689 cp = "TCP_CLOSED"; 6690 break; 6691 case TCPS_IDLE: 6692 cp = "TCP_IDLE"; 6693 break; 6694 case TCPS_BOUND: 6695 cp = "TCP_BOUND"; 6696 break; 6697 case TCPS_LISTEN: 6698 cp = "TCP_LISTEN"; 6699 break; 6700 case TCPS_SYN_SENT: 6701 cp = "TCP_SYN_SENT"; 6702 break; 6703 case TCPS_SYN_RCVD: 6704 cp = "TCP_SYN_RCVD"; 6705 break; 6706 case TCPS_ESTABLISHED: 6707 cp = "TCP_ESTABLISHED"; 6708 break; 6709 case TCPS_CLOSE_WAIT: 6710 cp = "TCP_CLOSE_WAIT"; 6711 break; 6712 case TCPS_FIN_WAIT_1: 6713 cp = "TCP_FIN_WAIT_1"; 6714 break; 6715 case TCPS_CLOSING: 6716 cp = "TCP_CLOSING"; 6717 break; 6718 case TCPS_LAST_ACK: 6719 cp = "TCP_LAST_ACK"; 6720 break; 6721 case TCPS_FIN_WAIT_2: 6722 cp = "TCP_FIN_WAIT_2"; 6723 break; 6724 case TCPS_TIME_WAIT: 6725 cp = "TCP_TIME_WAIT"; 6726 break; 6727 default: 6728 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 6729 cp = buf1; 6730 break; 6731 } 6732 switch (format) { 6733 case DISP_ADDR_AND_PORT: 6734 if (tcp->tcp_ipversion == IPV4_VERSION) { 6735 /* 6736 * Note that we use the remote address in the tcp_b 6737 * structure. This means that it will print out 6738 * the real destination address, not the next hop's 6739 * address if source routing is used. 6740 */ 6741 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 6742 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 6743 6744 } else { 6745 local = tcp->tcp_ip_src_v6; 6746 remote = tcp->tcp_remote_v6; 6747 } 6748 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 6749 sizeof (local_addrbuf)); 6750 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 6751 sizeof (remote_addrbuf)); 6752 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 6753 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 6754 ntohs(tcp->tcp_fport), cp); 6755 break; 6756 case DISP_PORT_ONLY: 6757 default: 6758 (void) mi_sprintf(buf, "[%u, %u] %s", 6759 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 6760 break; 6761 } 6762 6763 return (buf); 6764 } 6765 6766 /* 6767 * Called via squeue to get on to eager's perimeter. It sends a 6768 * TH_RST if eager is in the fanout table. The listener wants the 6769 * eager to disappear either by means of tcp_eager_blowoff() or 6770 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 6771 * called (via squeue) if the eager cannot be inserted in the 6772 * fanout table in tcp_conn_request(). 6773 */ 6774 /* ARGSUSED */ 6775 void 6776 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 6777 { 6778 conn_t *econnp = (conn_t *)arg; 6779 tcp_t *eager = econnp->conn_tcp; 6780 tcp_t *listener = eager->tcp_listener; 6781 tcp_stack_t *tcps = eager->tcp_tcps; 6782 6783 /* 6784 * We could be called because listener is closing. Since 6785 * the eager is using listener's queue's, its not safe. 6786 * Better use the default queue just to send the TH_RST 6787 * out. 6788 */ 6789 ASSERT(tcps->tcps_g_q != NULL); 6790 eager->tcp_rq = tcps->tcps_g_q; 6791 eager->tcp_wq = WR(tcps->tcps_g_q); 6792 6793 /* 6794 * An eager's conn_fanout will be NULL if it's a duplicate 6795 * for an existing 4-tuples in the conn fanout table. 6796 * We don't want to send an RST out in such case. 6797 */ 6798 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 6799 tcp_xmit_ctl("tcp_eager_kill, can't wait", 6800 eager, eager->tcp_snxt, 0, TH_RST); 6801 } 6802 6803 /* We are here because listener wants this eager gone */ 6804 if (listener != NULL) { 6805 mutex_enter(&listener->tcp_eager_lock); 6806 tcp_eager_unlink(eager); 6807 if (eager->tcp_tconnind_started) { 6808 /* 6809 * The eager has sent a conn_ind up to the 6810 * listener but listener decides to close 6811 * instead. We need to drop the extra ref 6812 * placed on eager in tcp_rput_data() before 6813 * sending the conn_ind to listener. 6814 */ 6815 CONN_DEC_REF(econnp); 6816 } 6817 mutex_exit(&listener->tcp_eager_lock); 6818 CONN_DEC_REF(listener->tcp_connp); 6819 } 6820 6821 if (eager->tcp_state != TCPS_CLOSED) 6822 tcp_close_detached(eager); 6823 } 6824 6825 /* 6826 * Reset any eager connection hanging off this listener marked 6827 * with 'seqnum' and then reclaim it's resources. 6828 */ 6829 static boolean_t 6830 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 6831 { 6832 tcp_t *eager; 6833 mblk_t *mp; 6834 tcp_stack_t *tcps = listener->tcp_tcps; 6835 6836 TCP_STAT(tcps, tcp_eager_blowoff_calls); 6837 eager = listener; 6838 mutex_enter(&listener->tcp_eager_lock); 6839 do { 6840 eager = eager->tcp_eager_next_q; 6841 if (eager == NULL) { 6842 mutex_exit(&listener->tcp_eager_lock); 6843 return (B_FALSE); 6844 } 6845 } while (eager->tcp_conn_req_seqnum != seqnum); 6846 6847 if (eager->tcp_closemp_used) { 6848 mutex_exit(&listener->tcp_eager_lock); 6849 return (B_TRUE); 6850 } 6851 eager->tcp_closemp_used = B_TRUE; 6852 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6853 CONN_INC_REF(eager->tcp_connp); 6854 mutex_exit(&listener->tcp_eager_lock); 6855 mp = &eager->tcp_closemp; 6856 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 6857 eager->tcp_connp, SQ_FILL, SQTAG_TCP_EAGER_BLOWOFF); 6858 return (B_TRUE); 6859 } 6860 6861 /* 6862 * Reset any eager connection hanging off this listener 6863 * and then reclaim it's resources. 6864 */ 6865 static void 6866 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 6867 { 6868 tcp_t *eager; 6869 mblk_t *mp; 6870 tcp_stack_t *tcps = listener->tcp_tcps; 6871 6872 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6873 6874 if (!q0_only) { 6875 /* First cleanup q */ 6876 TCP_STAT(tcps, tcp_eager_blowoff_q); 6877 eager = listener->tcp_eager_next_q; 6878 while (eager != NULL) { 6879 if (!eager->tcp_closemp_used) { 6880 eager->tcp_closemp_used = B_TRUE; 6881 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6882 CONN_INC_REF(eager->tcp_connp); 6883 mp = &eager->tcp_closemp; 6884 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6885 tcp_eager_kill, eager->tcp_connp, 6886 SQ_FILL, SQTAG_TCP_EAGER_CLEANUP); 6887 } 6888 eager = eager->tcp_eager_next_q; 6889 } 6890 } 6891 /* Then cleanup q0 */ 6892 TCP_STAT(tcps, tcp_eager_blowoff_q0); 6893 eager = listener->tcp_eager_next_q0; 6894 while (eager != listener) { 6895 if (!eager->tcp_closemp_used) { 6896 eager->tcp_closemp_used = B_TRUE; 6897 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6898 CONN_INC_REF(eager->tcp_connp); 6899 mp = &eager->tcp_closemp; 6900 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6901 tcp_eager_kill, eager->tcp_connp, SQ_FILL, 6902 SQTAG_TCP_EAGER_CLEANUP_Q0); 6903 } 6904 eager = eager->tcp_eager_next_q0; 6905 } 6906 } 6907 6908 /* 6909 * If we are an eager connection hanging off a listener that hasn't 6910 * formally accepted the connection yet, get off his list and blow off 6911 * any data that we have accumulated. 6912 */ 6913 static void 6914 tcp_eager_unlink(tcp_t *tcp) 6915 { 6916 tcp_t *listener = tcp->tcp_listener; 6917 6918 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6919 ASSERT(listener != NULL); 6920 if (tcp->tcp_eager_next_q0 != NULL) { 6921 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 6922 6923 /* Remove the eager tcp from q0 */ 6924 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 6925 tcp->tcp_eager_prev_q0; 6926 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 6927 tcp->tcp_eager_next_q0; 6928 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 6929 listener->tcp_conn_req_cnt_q0--; 6930 6931 tcp->tcp_eager_next_q0 = NULL; 6932 tcp->tcp_eager_prev_q0 = NULL; 6933 6934 /* 6935 * Take the eager out, if it is in the list of droppable 6936 * eagers. 6937 */ 6938 MAKE_UNDROPPABLE(tcp); 6939 6940 if (tcp->tcp_syn_rcvd_timeout != 0) { 6941 /* we have timed out before */ 6942 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 6943 listener->tcp_syn_rcvd_timeout--; 6944 } 6945 } else { 6946 tcp_t **tcpp = &listener->tcp_eager_next_q; 6947 tcp_t *prev = NULL; 6948 6949 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 6950 if (tcpp[0] == tcp) { 6951 if (listener->tcp_eager_last_q == tcp) { 6952 /* 6953 * If we are unlinking the last 6954 * element on the list, adjust 6955 * tail pointer. Set tail pointer 6956 * to nil when list is empty. 6957 */ 6958 ASSERT(tcp->tcp_eager_next_q == NULL); 6959 if (listener->tcp_eager_last_q == 6960 listener->tcp_eager_next_q) { 6961 listener->tcp_eager_last_q = 6962 NULL; 6963 } else { 6964 /* 6965 * We won't get here if there 6966 * is only one eager in the 6967 * list. 6968 */ 6969 ASSERT(prev != NULL); 6970 listener->tcp_eager_last_q = 6971 prev; 6972 } 6973 } 6974 tcpp[0] = tcp->tcp_eager_next_q; 6975 tcp->tcp_eager_next_q = NULL; 6976 tcp->tcp_eager_last_q = NULL; 6977 ASSERT(listener->tcp_conn_req_cnt_q > 0); 6978 listener->tcp_conn_req_cnt_q--; 6979 break; 6980 } 6981 prev = tcpp[0]; 6982 } 6983 } 6984 tcp->tcp_listener = NULL; 6985 } 6986 6987 /* Shorthand to generate and send TPI error acks to our client */ 6988 static void 6989 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 6990 { 6991 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 6992 putnext(tcp->tcp_rq, mp); 6993 } 6994 6995 /* Shorthand to generate and send TPI error acks to our client */ 6996 static void 6997 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 6998 int t_error, int sys_error) 6999 { 7000 struct T_error_ack *teackp; 7001 7002 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7003 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7004 teackp = (struct T_error_ack *)mp->b_rptr; 7005 teackp->ERROR_prim = primitive; 7006 teackp->TLI_error = t_error; 7007 teackp->UNIX_error = sys_error; 7008 putnext(tcp->tcp_rq, mp); 7009 } 7010 } 7011 7012 /* 7013 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7014 * but instead the code relies on: 7015 * - the fact that the address of the array and its size never changes 7016 * - the atomic assignment of the elements of the array 7017 */ 7018 /* ARGSUSED */ 7019 static int 7020 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7021 { 7022 int i; 7023 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7024 7025 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7026 if (tcps->tcps_g_epriv_ports[i] != 0) 7027 (void) mi_mpprintf(mp, "%d ", 7028 tcps->tcps_g_epriv_ports[i]); 7029 } 7030 return (0); 7031 } 7032 7033 /* 7034 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7035 * threads from changing it at the same time. 7036 */ 7037 /* ARGSUSED */ 7038 static int 7039 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7040 cred_t *cr) 7041 { 7042 long new_value; 7043 int i; 7044 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7045 7046 /* 7047 * Fail the request if the new value does not lie within the 7048 * port number limits. 7049 */ 7050 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7051 new_value <= 0 || new_value >= 65536) { 7052 return (EINVAL); 7053 } 7054 7055 mutex_enter(&tcps->tcps_epriv_port_lock); 7056 /* Check if the value is already in the list */ 7057 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7058 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7059 mutex_exit(&tcps->tcps_epriv_port_lock); 7060 return (EEXIST); 7061 } 7062 } 7063 /* Find an empty slot */ 7064 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7065 if (tcps->tcps_g_epriv_ports[i] == 0) 7066 break; 7067 } 7068 if (i == tcps->tcps_g_num_epriv_ports) { 7069 mutex_exit(&tcps->tcps_epriv_port_lock); 7070 return (EOVERFLOW); 7071 } 7072 /* Set the new value */ 7073 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7074 mutex_exit(&tcps->tcps_epriv_port_lock); 7075 return (0); 7076 } 7077 7078 /* 7079 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7080 * threads from changing it at the same time. 7081 */ 7082 /* ARGSUSED */ 7083 static int 7084 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7085 cred_t *cr) 7086 { 7087 long new_value; 7088 int i; 7089 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7090 7091 /* 7092 * Fail the request if the new value does not lie within the 7093 * port number limits. 7094 */ 7095 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7096 new_value >= 65536) { 7097 return (EINVAL); 7098 } 7099 7100 mutex_enter(&tcps->tcps_epriv_port_lock); 7101 /* Check that the value is already in the list */ 7102 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7103 if (tcps->tcps_g_epriv_ports[i] == new_value) 7104 break; 7105 } 7106 if (i == tcps->tcps_g_num_epriv_ports) { 7107 mutex_exit(&tcps->tcps_epriv_port_lock); 7108 return (ESRCH); 7109 } 7110 /* Clear the value */ 7111 tcps->tcps_g_epriv_ports[i] = 0; 7112 mutex_exit(&tcps->tcps_epriv_port_lock); 7113 return (0); 7114 } 7115 7116 /* Return the TPI/TLI equivalent of our current tcp_state */ 7117 static int 7118 tcp_tpistate(tcp_t *tcp) 7119 { 7120 switch (tcp->tcp_state) { 7121 case TCPS_IDLE: 7122 return (TS_UNBND); 7123 case TCPS_LISTEN: 7124 /* 7125 * Return whether there are outstanding T_CONN_IND waiting 7126 * for the matching T_CONN_RES. Therefore don't count q0. 7127 */ 7128 if (tcp->tcp_conn_req_cnt_q > 0) 7129 return (TS_WRES_CIND); 7130 else 7131 return (TS_IDLE); 7132 case TCPS_BOUND: 7133 return (TS_IDLE); 7134 case TCPS_SYN_SENT: 7135 return (TS_WCON_CREQ); 7136 case TCPS_SYN_RCVD: 7137 /* 7138 * Note: assumption: this has to the active open SYN_RCVD. 7139 * The passive instance is detached in SYN_RCVD stage of 7140 * incoming connection processing so we cannot get request 7141 * for T_info_ack on it. 7142 */ 7143 return (TS_WACK_CRES); 7144 case TCPS_ESTABLISHED: 7145 return (TS_DATA_XFER); 7146 case TCPS_CLOSE_WAIT: 7147 return (TS_WREQ_ORDREL); 7148 case TCPS_FIN_WAIT_1: 7149 return (TS_WIND_ORDREL); 7150 case TCPS_FIN_WAIT_2: 7151 return (TS_WIND_ORDREL); 7152 7153 case TCPS_CLOSING: 7154 case TCPS_LAST_ACK: 7155 case TCPS_TIME_WAIT: 7156 case TCPS_CLOSED: 7157 /* 7158 * Following TS_WACK_DREQ7 is a rendition of "not 7159 * yet TS_IDLE" TPI state. There is no best match to any 7160 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7161 * choose a value chosen that will map to TLI/XTI level 7162 * state of TSTATECHNG (state is process of changing) which 7163 * captures what this dummy state represents. 7164 */ 7165 return (TS_WACK_DREQ7); 7166 default: 7167 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7168 tcp->tcp_state, tcp_display(tcp, NULL, 7169 DISP_PORT_ONLY)); 7170 return (TS_UNBND); 7171 } 7172 } 7173 7174 static void 7175 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7176 { 7177 tcp_stack_t *tcps = tcp->tcp_tcps; 7178 7179 if (tcp->tcp_family == AF_INET6) 7180 *tia = tcp_g_t_info_ack_v6; 7181 else 7182 *tia = tcp_g_t_info_ack; 7183 tia->CURRENT_state = tcp_tpistate(tcp); 7184 tia->OPT_size = tcp_max_optsize; 7185 if (tcp->tcp_mss == 0) { 7186 /* Not yet set - tcp_open does not set mss */ 7187 if (tcp->tcp_ipversion == IPV4_VERSION) 7188 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7189 else 7190 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7191 } else { 7192 tia->TIDU_size = tcp->tcp_mss; 7193 } 7194 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7195 } 7196 7197 static void 7198 tcp_do_capability_ack(tcp_t *tcp, struct T_capability_ack *tcap, 7199 t_uscalar_t cap_bits1) 7200 { 7201 tcap->CAP_bits1 = 0; 7202 7203 if (cap_bits1 & TC1_INFO) { 7204 tcp_copy_info(&tcap->INFO_ack, tcp); 7205 tcap->CAP_bits1 |= TC1_INFO; 7206 } 7207 7208 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7209 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7210 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7211 } 7212 7213 } 7214 7215 /* 7216 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7217 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7218 * tcp_g_t_info_ack. The current state of the stream is copied from 7219 * tcp_state. 7220 */ 7221 static void 7222 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7223 { 7224 t_uscalar_t cap_bits1; 7225 struct T_capability_ack *tcap; 7226 7227 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7228 freemsg(mp); 7229 return; 7230 } 7231 7232 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7233 7234 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7235 mp->b_datap->db_type, T_CAPABILITY_ACK); 7236 if (mp == NULL) 7237 return; 7238 7239 tcap = (struct T_capability_ack *)mp->b_rptr; 7240 tcp_do_capability_ack(tcp, tcap, cap_bits1); 7241 7242 putnext(tcp->tcp_rq, mp); 7243 } 7244 7245 /* 7246 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7247 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7248 * The current state of the stream is copied from tcp_state. 7249 */ 7250 static void 7251 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7252 { 7253 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7254 T_INFO_ACK); 7255 if (!mp) { 7256 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7257 return; 7258 } 7259 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7260 putnext(tcp->tcp_rq, mp); 7261 } 7262 7263 /* Respond to the TPI addr request */ 7264 static void 7265 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7266 { 7267 sin_t *sin; 7268 mblk_t *ackmp; 7269 struct T_addr_ack *taa; 7270 7271 /* Make it large enough for worst case */ 7272 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7273 2 * sizeof (sin6_t), 1); 7274 if (ackmp == NULL) { 7275 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7276 return; 7277 } 7278 7279 if (tcp->tcp_ipversion == IPV6_VERSION) { 7280 tcp_addr_req_ipv6(tcp, ackmp); 7281 return; 7282 } 7283 taa = (struct T_addr_ack *)ackmp->b_rptr; 7284 7285 bzero(taa, sizeof (struct T_addr_ack)); 7286 ackmp->b_wptr = (uchar_t *)&taa[1]; 7287 7288 taa->PRIM_type = T_ADDR_ACK; 7289 ackmp->b_datap->db_type = M_PCPROTO; 7290 7291 /* 7292 * Note: Following code assumes 32 bit alignment of basic 7293 * data structures like sin_t and struct T_addr_ack. 7294 */ 7295 if (tcp->tcp_state >= TCPS_BOUND) { 7296 /* 7297 * Fill in local address 7298 */ 7299 taa->LOCADDR_length = sizeof (sin_t); 7300 taa->LOCADDR_offset = sizeof (*taa); 7301 7302 sin = (sin_t *)&taa[1]; 7303 7304 /* Fill zeroes and then intialize non-zero fields */ 7305 *sin = sin_null; 7306 7307 sin->sin_family = AF_INET; 7308 7309 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7310 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7311 7312 ackmp->b_wptr = (uchar_t *)&sin[1]; 7313 7314 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7315 /* 7316 * Fill in Remote address 7317 */ 7318 taa->REMADDR_length = sizeof (sin_t); 7319 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7320 taa->LOCADDR_length); 7321 7322 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7323 *sin = sin_null; 7324 sin->sin_family = AF_INET; 7325 sin->sin_addr.s_addr = tcp->tcp_remote; 7326 sin->sin_port = tcp->tcp_fport; 7327 7328 ackmp->b_wptr = (uchar_t *)&sin[1]; 7329 } 7330 } 7331 putnext(tcp->tcp_rq, ackmp); 7332 } 7333 7334 /* Assumes that tcp_addr_req gets enough space and alignment */ 7335 static void 7336 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7337 { 7338 sin6_t *sin6; 7339 struct T_addr_ack *taa; 7340 7341 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7342 ASSERT(OK_32PTR(ackmp->b_rptr)); 7343 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7344 2 * sizeof (sin6_t)); 7345 7346 taa = (struct T_addr_ack *)ackmp->b_rptr; 7347 7348 bzero(taa, sizeof (struct T_addr_ack)); 7349 ackmp->b_wptr = (uchar_t *)&taa[1]; 7350 7351 taa->PRIM_type = T_ADDR_ACK; 7352 ackmp->b_datap->db_type = M_PCPROTO; 7353 7354 /* 7355 * Note: Following code assumes 32 bit alignment of basic 7356 * data structures like sin6_t and struct T_addr_ack. 7357 */ 7358 if (tcp->tcp_state >= TCPS_BOUND) { 7359 /* 7360 * Fill in local address 7361 */ 7362 taa->LOCADDR_length = sizeof (sin6_t); 7363 taa->LOCADDR_offset = sizeof (*taa); 7364 7365 sin6 = (sin6_t *)&taa[1]; 7366 *sin6 = sin6_null; 7367 7368 sin6->sin6_family = AF_INET6; 7369 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7370 sin6->sin6_port = tcp->tcp_lport; 7371 7372 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7373 7374 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7375 /* 7376 * Fill in Remote address 7377 */ 7378 taa->REMADDR_length = sizeof (sin6_t); 7379 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7380 taa->LOCADDR_length); 7381 7382 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7383 *sin6 = sin6_null; 7384 sin6->sin6_family = AF_INET6; 7385 sin6->sin6_flowinfo = 7386 tcp->tcp_ip6h->ip6_vcf & 7387 ~IPV6_VERS_AND_FLOW_MASK; 7388 sin6->sin6_addr = tcp->tcp_remote_v6; 7389 sin6->sin6_port = tcp->tcp_fport; 7390 7391 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7392 } 7393 } 7394 putnext(tcp->tcp_rq, ackmp); 7395 } 7396 7397 /* 7398 * Handle reinitialization of a tcp structure. 7399 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7400 */ 7401 static void 7402 tcp_reinit(tcp_t *tcp) 7403 { 7404 mblk_t *mp; 7405 int err; 7406 tcp_stack_t *tcps = tcp->tcp_tcps; 7407 7408 TCP_STAT(tcps, tcp_reinit_calls); 7409 7410 /* tcp_reinit should never be called for detached tcp_t's */ 7411 ASSERT(tcp->tcp_listener == NULL); 7412 ASSERT((tcp->tcp_family == AF_INET && 7413 tcp->tcp_ipversion == IPV4_VERSION) || 7414 (tcp->tcp_family == AF_INET6 && 7415 (tcp->tcp_ipversion == IPV4_VERSION || 7416 tcp->tcp_ipversion == IPV6_VERSION))); 7417 7418 /* Cancel outstanding timers */ 7419 tcp_timers_stop(tcp); 7420 7421 /* 7422 * Reset everything in the state vector, after updating global 7423 * MIB data from instance counters. 7424 */ 7425 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7426 tcp->tcp_ibsegs = 0; 7427 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7428 tcp->tcp_obsegs = 0; 7429 7430 tcp_close_mpp(&tcp->tcp_xmit_head); 7431 if (tcp->tcp_snd_zcopy_aware) 7432 tcp_zcopy_notify(tcp); 7433 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7434 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7435 mutex_enter(&tcp->tcp_non_sq_lock); 7436 if (tcp->tcp_flow_stopped && 7437 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7438 tcp_clrqfull(tcp); 7439 } 7440 mutex_exit(&tcp->tcp_non_sq_lock); 7441 tcp_close_mpp(&tcp->tcp_reass_head); 7442 tcp->tcp_reass_tail = NULL; 7443 if (tcp->tcp_rcv_list != NULL) { 7444 /* Free b_next chain */ 7445 tcp_close_mpp(&tcp->tcp_rcv_list); 7446 tcp->tcp_rcv_last_head = NULL; 7447 tcp->tcp_rcv_last_tail = NULL; 7448 tcp->tcp_rcv_cnt = 0; 7449 } 7450 tcp->tcp_rcv_last_tail = NULL; 7451 7452 if ((mp = tcp->tcp_urp_mp) != NULL) { 7453 freemsg(mp); 7454 tcp->tcp_urp_mp = NULL; 7455 } 7456 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7457 freemsg(mp); 7458 tcp->tcp_urp_mark_mp = NULL; 7459 } 7460 if (tcp->tcp_fused_sigurg_mp != NULL) { 7461 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7462 freeb(tcp->tcp_fused_sigurg_mp); 7463 tcp->tcp_fused_sigurg_mp = NULL; 7464 } 7465 if (tcp->tcp_ordrel_mp != NULL) { 7466 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7467 freeb(tcp->tcp_ordrel_mp); 7468 tcp->tcp_ordrel_mp = NULL; 7469 } 7470 7471 /* 7472 * Following is a union with two members which are 7473 * identical types and size so the following cleanup 7474 * is enough. 7475 */ 7476 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7477 7478 CL_INET_DISCONNECT(tcp->tcp_connp, tcp); 7479 7480 /* 7481 * The connection can't be on the tcp_time_wait_head list 7482 * since it is not detached. 7483 */ 7484 ASSERT(tcp->tcp_time_wait_next == NULL); 7485 ASSERT(tcp->tcp_time_wait_prev == NULL); 7486 ASSERT(tcp->tcp_time_wait_expire == 0); 7487 7488 if (tcp->tcp_kssl_pending) { 7489 tcp->tcp_kssl_pending = B_FALSE; 7490 7491 /* Don't reset if the initialized by bind. */ 7492 if (tcp->tcp_kssl_ent != NULL) { 7493 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7494 KSSL_NO_PROXY); 7495 } 7496 } 7497 if (tcp->tcp_kssl_ctx != NULL) { 7498 kssl_release_ctx(tcp->tcp_kssl_ctx); 7499 tcp->tcp_kssl_ctx = NULL; 7500 } 7501 7502 /* 7503 * Reset/preserve other values 7504 */ 7505 tcp_reinit_values(tcp); 7506 ipcl_hash_remove(tcp->tcp_connp); 7507 conn_delete_ire(tcp->tcp_connp, NULL); 7508 tcp_ipsec_cleanup(tcp); 7509 7510 if (tcp->tcp_connp->conn_effective_cred != NULL) { 7511 crfree(tcp->tcp_connp->conn_effective_cred); 7512 tcp->tcp_connp->conn_effective_cred = NULL; 7513 } 7514 7515 if (tcp->tcp_conn_req_max != 0) { 7516 /* 7517 * This is the case when a TLI program uses the same 7518 * transport end point to accept a connection. This 7519 * makes the TCP both a listener and acceptor. When 7520 * this connection is closed, we need to set the state 7521 * back to TCPS_LISTEN. Make sure that the eager list 7522 * is reinitialized. 7523 * 7524 * Note that this stream is still bound to the four 7525 * tuples of the previous connection in IP. If a new 7526 * SYN with different foreign address comes in, IP will 7527 * not find it and will send it to the global queue. In 7528 * the global queue, TCP will do a tcp_lookup_listener() 7529 * to find this stream. This works because this stream 7530 * is only removed from connected hash. 7531 * 7532 */ 7533 tcp->tcp_state = TCPS_LISTEN; 7534 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7535 tcp->tcp_eager_next_drop_q0 = tcp; 7536 tcp->tcp_eager_prev_drop_q0 = tcp; 7537 tcp->tcp_connp->conn_recv = tcp_conn_request; 7538 if (tcp->tcp_family == AF_INET6) { 7539 ASSERT(tcp->tcp_connp->conn_af_isv6); 7540 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7541 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7542 } else { 7543 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7544 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7545 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7546 } 7547 } else { 7548 tcp->tcp_state = TCPS_BOUND; 7549 } 7550 7551 /* 7552 * Initialize to default values 7553 * Can't fail since enough header template space already allocated 7554 * at open(). 7555 */ 7556 err = tcp_init_values(tcp); 7557 ASSERT(err == 0); 7558 /* Restore state in tcp_tcph */ 7559 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7560 if (tcp->tcp_ipversion == IPV4_VERSION) 7561 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7562 else 7563 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7564 /* 7565 * Copy of the src addr. in tcp_t is needed in tcp_t 7566 * since the lookup funcs can only lookup on tcp_t 7567 */ 7568 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7569 7570 ASSERT(tcp->tcp_ptpbhn != NULL); 7571 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 7572 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7573 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 7574 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 7575 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7576 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7577 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7578 } 7579 7580 /* 7581 * Force values to zero that need be zero. 7582 * Do not touch values asociated with the BOUND or LISTEN state 7583 * since the connection will end up in that state after the reinit. 7584 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7585 * structure! 7586 */ 7587 static void 7588 tcp_reinit_values(tcp) 7589 tcp_t *tcp; 7590 { 7591 tcp_stack_t *tcps = tcp->tcp_tcps; 7592 7593 #ifndef lint 7594 #define DONTCARE(x) 7595 #define PRESERVE(x) 7596 #else 7597 #define DONTCARE(x) ((x) = (x)) 7598 #define PRESERVE(x) ((x) = (x)) 7599 #endif /* lint */ 7600 7601 PRESERVE(tcp->tcp_bind_hash_port); 7602 PRESERVE(tcp->tcp_bind_hash); 7603 PRESERVE(tcp->tcp_ptpbhn); 7604 PRESERVE(tcp->tcp_acceptor_hash); 7605 PRESERVE(tcp->tcp_ptpahn); 7606 7607 /* Should be ASSERT NULL on these with new code! */ 7608 ASSERT(tcp->tcp_time_wait_next == NULL); 7609 ASSERT(tcp->tcp_time_wait_prev == NULL); 7610 ASSERT(tcp->tcp_time_wait_expire == 0); 7611 PRESERVE(tcp->tcp_state); 7612 PRESERVE(tcp->tcp_rq); 7613 PRESERVE(tcp->tcp_wq); 7614 7615 ASSERT(tcp->tcp_xmit_head == NULL); 7616 ASSERT(tcp->tcp_xmit_last == NULL); 7617 ASSERT(tcp->tcp_unsent == 0); 7618 ASSERT(tcp->tcp_xmit_tail == NULL); 7619 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7620 7621 tcp->tcp_snxt = 0; /* Displayed in mib */ 7622 tcp->tcp_suna = 0; /* Displayed in mib */ 7623 tcp->tcp_swnd = 0; 7624 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7625 7626 ASSERT(tcp->tcp_ibsegs == 0); 7627 ASSERT(tcp->tcp_obsegs == 0); 7628 7629 if (tcp->tcp_iphc != NULL) { 7630 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7631 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7632 } 7633 7634 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7635 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7636 DONTCARE(tcp->tcp_ipha); 7637 DONTCARE(tcp->tcp_ip6h); 7638 DONTCARE(tcp->tcp_ip_hdr_len); 7639 DONTCARE(tcp->tcp_tcph); 7640 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7641 tcp->tcp_valid_bits = 0; 7642 7643 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7644 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7645 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7646 tcp->tcp_last_rcv_lbolt = 0; 7647 7648 tcp->tcp_init_cwnd = 0; 7649 7650 tcp->tcp_urp_last_valid = 0; 7651 tcp->tcp_hard_binding = 0; 7652 tcp->tcp_hard_bound = 0; 7653 PRESERVE(tcp->tcp_cred); 7654 PRESERVE(tcp->tcp_cpid); 7655 PRESERVE(tcp->tcp_open_time); 7656 PRESERVE(tcp->tcp_exclbind); 7657 7658 tcp->tcp_fin_acked = 0; 7659 tcp->tcp_fin_rcvd = 0; 7660 tcp->tcp_fin_sent = 0; 7661 tcp->tcp_ordrel_done = 0; 7662 7663 tcp->tcp_debug = 0; 7664 tcp->tcp_dontroute = 0; 7665 tcp->tcp_broadcast = 0; 7666 7667 tcp->tcp_useloopback = 0; 7668 tcp->tcp_reuseaddr = 0; 7669 tcp->tcp_oobinline = 0; 7670 tcp->tcp_dgram_errind = 0; 7671 7672 tcp->tcp_detached = 0; 7673 tcp->tcp_bind_pending = 0; 7674 tcp->tcp_unbind_pending = 0; 7675 7676 tcp->tcp_snd_ws_ok = B_FALSE; 7677 tcp->tcp_snd_ts_ok = B_FALSE; 7678 tcp->tcp_linger = 0; 7679 tcp->tcp_ka_enabled = 0; 7680 tcp->tcp_zero_win_probe = 0; 7681 7682 tcp->tcp_loopback = 0; 7683 tcp->tcp_refuse = 0; 7684 tcp->tcp_localnet = 0; 7685 tcp->tcp_syn_defense = 0; 7686 tcp->tcp_set_timer = 0; 7687 7688 tcp->tcp_active_open = 0; 7689 tcp->tcp_rexmit = B_FALSE; 7690 tcp->tcp_xmit_zc_clean = B_FALSE; 7691 7692 tcp->tcp_snd_sack_ok = B_FALSE; 7693 PRESERVE(tcp->tcp_recvdstaddr); 7694 tcp->tcp_hwcksum = B_FALSE; 7695 7696 tcp->tcp_ire_ill_check_done = B_FALSE; 7697 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7698 7699 tcp->tcp_mdt = B_FALSE; 7700 tcp->tcp_mdt_hdr_head = 0; 7701 tcp->tcp_mdt_hdr_tail = 0; 7702 7703 tcp->tcp_conn_def_q0 = 0; 7704 tcp->tcp_ip_forward_progress = B_FALSE; 7705 tcp->tcp_anon_priv_bind = 0; 7706 tcp->tcp_ecn_ok = B_FALSE; 7707 7708 tcp->tcp_cwr = B_FALSE; 7709 tcp->tcp_ecn_echo_on = B_FALSE; 7710 tcp->tcp_is_wnd_shrnk = B_FALSE; 7711 7712 if (tcp->tcp_sack_info != NULL) { 7713 if (tcp->tcp_notsack_list != NULL) { 7714 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, 7715 tcp); 7716 } 7717 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7718 tcp->tcp_sack_info = NULL; 7719 } 7720 7721 tcp->tcp_rcv_ws = 0; 7722 tcp->tcp_snd_ws = 0; 7723 tcp->tcp_ts_recent = 0; 7724 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7725 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7726 tcp->tcp_if_mtu = 0; 7727 7728 ASSERT(tcp->tcp_reass_head == NULL); 7729 ASSERT(tcp->tcp_reass_tail == NULL); 7730 7731 tcp->tcp_cwnd_cnt = 0; 7732 7733 ASSERT(tcp->tcp_rcv_list == NULL); 7734 ASSERT(tcp->tcp_rcv_last_head == NULL); 7735 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7736 ASSERT(tcp->tcp_rcv_cnt == 0); 7737 7738 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7739 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7740 tcp->tcp_csuna = 0; 7741 7742 tcp->tcp_rto = 0; /* Displayed in MIB */ 7743 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 7744 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 7745 tcp->tcp_rtt_update = 0; 7746 7747 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7748 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7749 7750 tcp->tcp_rack = 0; /* Displayed in mib */ 7751 tcp->tcp_rack_cnt = 0; 7752 tcp->tcp_rack_cur_max = 0; 7753 tcp->tcp_rack_abs_max = 0; 7754 7755 tcp->tcp_max_swnd = 0; 7756 7757 ASSERT(tcp->tcp_listener == NULL); 7758 7759 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 7760 7761 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 7762 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 7763 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 7764 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 7765 7766 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 7767 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 7768 PRESERVE(tcp->tcp_conn_req_max); 7769 PRESERVE(tcp->tcp_conn_req_seqnum); 7770 7771 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 7772 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 7773 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 7774 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 7775 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 7776 7777 tcp->tcp_lingertime = 0; 7778 7779 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 7780 ASSERT(tcp->tcp_urp_mp == NULL); 7781 ASSERT(tcp->tcp_urp_mark_mp == NULL); 7782 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 7783 7784 ASSERT(tcp->tcp_eager_next_q == NULL); 7785 ASSERT(tcp->tcp_eager_last_q == NULL); 7786 ASSERT((tcp->tcp_eager_next_q0 == NULL && 7787 tcp->tcp_eager_prev_q0 == NULL) || 7788 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 7789 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 7790 7791 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 7792 tcp->tcp_eager_prev_drop_q0 == NULL) || 7793 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 7794 7795 tcp->tcp_client_errno = 0; 7796 7797 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 7798 7799 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 7800 7801 PRESERVE(tcp->tcp_bound_source_v6); 7802 tcp->tcp_last_sent_len = 0; 7803 tcp->tcp_dupack_cnt = 0; 7804 7805 tcp->tcp_fport = 0; /* Displayed in MIB */ 7806 PRESERVE(tcp->tcp_lport); 7807 7808 PRESERVE(tcp->tcp_acceptor_lockp); 7809 7810 ASSERT(tcp->tcp_ordrel_mp == NULL); 7811 PRESERVE(tcp->tcp_acceptor_id); 7812 DONTCARE(tcp->tcp_ipsec_overhead); 7813 7814 PRESERVE(tcp->tcp_family); 7815 if (tcp->tcp_family == AF_INET6) { 7816 tcp->tcp_ipversion = IPV6_VERSION; 7817 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 7818 } else { 7819 tcp->tcp_ipversion = IPV4_VERSION; 7820 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 7821 } 7822 7823 tcp->tcp_bound_if = 0; 7824 tcp->tcp_ipv6_recvancillary = 0; 7825 tcp->tcp_recvifindex = 0; 7826 tcp->tcp_recvhops = 0; 7827 tcp->tcp_closed = 0; 7828 tcp->tcp_cleandeathtag = 0; 7829 if (tcp->tcp_hopopts != NULL) { 7830 mi_free(tcp->tcp_hopopts); 7831 tcp->tcp_hopopts = NULL; 7832 tcp->tcp_hopoptslen = 0; 7833 } 7834 ASSERT(tcp->tcp_hopoptslen == 0); 7835 if (tcp->tcp_dstopts != NULL) { 7836 mi_free(tcp->tcp_dstopts); 7837 tcp->tcp_dstopts = NULL; 7838 tcp->tcp_dstoptslen = 0; 7839 } 7840 ASSERT(tcp->tcp_dstoptslen == 0); 7841 if (tcp->tcp_rtdstopts != NULL) { 7842 mi_free(tcp->tcp_rtdstopts); 7843 tcp->tcp_rtdstopts = NULL; 7844 tcp->tcp_rtdstoptslen = 0; 7845 } 7846 ASSERT(tcp->tcp_rtdstoptslen == 0); 7847 if (tcp->tcp_rthdr != NULL) { 7848 mi_free(tcp->tcp_rthdr); 7849 tcp->tcp_rthdr = NULL; 7850 tcp->tcp_rthdrlen = 0; 7851 } 7852 ASSERT(tcp->tcp_rthdrlen == 0); 7853 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 7854 7855 /* Reset fusion-related fields */ 7856 tcp->tcp_fused = B_FALSE; 7857 tcp->tcp_unfusable = B_FALSE; 7858 tcp->tcp_fused_sigurg = B_FALSE; 7859 tcp->tcp_loopback_peer = NULL; 7860 tcp->tcp_fuse_rcv_hiwater = 0; 7861 7862 tcp->tcp_lso = B_FALSE; 7863 7864 tcp->tcp_in_ack_unsent = 0; 7865 tcp->tcp_cork = B_FALSE; 7866 tcp->tcp_tconnind_started = B_FALSE; 7867 7868 PRESERVE(tcp->tcp_squeue_bytes); 7869 7870 ASSERT(tcp->tcp_kssl_ctx == NULL); 7871 ASSERT(!tcp->tcp_kssl_pending); 7872 PRESERVE(tcp->tcp_kssl_ent); 7873 7874 tcp->tcp_closemp_used = B_FALSE; 7875 7876 PRESERVE(tcp->tcp_rsrv_mp); 7877 PRESERVE(tcp->tcp_rsrv_mp_lock); 7878 7879 #ifdef DEBUG 7880 DONTCARE(tcp->tcmp_stk[0]); 7881 #endif 7882 7883 PRESERVE(tcp->tcp_connid); 7884 7885 7886 #undef DONTCARE 7887 #undef PRESERVE 7888 } 7889 7890 /* 7891 * Allocate necessary resources and initialize state vector. 7892 * Guaranteed not to fail so that when an error is returned, 7893 * the caller doesn't need to do any additional cleanup. 7894 */ 7895 int 7896 tcp_init(tcp_t *tcp, queue_t *q) 7897 { 7898 int err; 7899 7900 tcp->tcp_rq = q; 7901 tcp->tcp_wq = WR(q); 7902 tcp->tcp_state = TCPS_IDLE; 7903 if ((err = tcp_init_values(tcp)) != 0) 7904 tcp_timers_stop(tcp); 7905 return (err); 7906 } 7907 7908 static int 7909 tcp_init_values(tcp_t *tcp) 7910 { 7911 int err; 7912 tcp_stack_t *tcps = tcp->tcp_tcps; 7913 7914 ASSERT((tcp->tcp_family == AF_INET && 7915 tcp->tcp_ipversion == IPV4_VERSION) || 7916 (tcp->tcp_family == AF_INET6 && 7917 (tcp->tcp_ipversion == IPV4_VERSION || 7918 tcp->tcp_ipversion == IPV6_VERSION))); 7919 7920 /* 7921 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 7922 * will be close to tcp_rexmit_interval_initial. By doing this, we 7923 * allow the algorithm to adjust slowly to large fluctuations of RTT 7924 * during first few transmissions of a connection as seen in slow 7925 * links. 7926 */ 7927 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 7928 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 7929 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 7930 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 7931 tcps->tcps_conn_grace_period; 7932 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 7933 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 7934 tcp->tcp_timer_backoff = 0; 7935 tcp->tcp_ms_we_have_waited = 0; 7936 tcp->tcp_last_recv_time = lbolt; 7937 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 7938 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 7939 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 7940 7941 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 7942 7943 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 7944 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 7945 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 7946 /* 7947 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 7948 * passive open. 7949 */ 7950 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 7951 7952 tcp->tcp_naglim = tcps->tcps_naglim_def; 7953 7954 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 7955 7956 tcp->tcp_mdt_hdr_head = 0; 7957 tcp->tcp_mdt_hdr_tail = 0; 7958 7959 /* Reset fusion-related fields */ 7960 tcp->tcp_fused = B_FALSE; 7961 tcp->tcp_unfusable = B_FALSE; 7962 tcp->tcp_fused_sigurg = B_FALSE; 7963 tcp->tcp_loopback_peer = NULL; 7964 tcp->tcp_fuse_rcv_hiwater = 0; 7965 7966 /* Initialize the header template */ 7967 if (tcp->tcp_ipversion == IPV4_VERSION) { 7968 err = tcp_header_init_ipv4(tcp); 7969 } else { 7970 err = tcp_header_init_ipv6(tcp); 7971 } 7972 if (err) 7973 return (err); 7974 7975 /* 7976 * Init the window scale to the max so tcp_rwnd_set() won't pare 7977 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 7978 */ 7979 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 7980 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 7981 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 7982 7983 tcp->tcp_cork = B_FALSE; 7984 /* 7985 * Init the tcp_debug option. This value determines whether TCP 7986 * calls strlog() to print out debug messages. Doing this 7987 * initialization here means that this value is not inherited thru 7988 * tcp_reinit(). 7989 */ 7990 tcp->tcp_debug = tcps->tcps_dbg; 7991 7992 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 7993 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 7994 7995 return (0); 7996 } 7997 7998 /* 7999 * Initialize the IPv4 header. Loses any record of any IP options. 8000 */ 8001 static int 8002 tcp_header_init_ipv4(tcp_t *tcp) 8003 { 8004 tcph_t *tcph; 8005 uint32_t sum; 8006 conn_t *connp; 8007 tcp_stack_t *tcps = tcp->tcp_tcps; 8008 8009 /* 8010 * This is a simple initialization. If there's 8011 * already a template, it should never be too small, 8012 * so reuse it. Otherwise, allocate space for the new one. 8013 */ 8014 if (tcp->tcp_iphc == NULL) { 8015 ASSERT(tcp->tcp_iphc_len == 0); 8016 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8017 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8018 if (tcp->tcp_iphc == NULL) { 8019 tcp->tcp_iphc_len = 0; 8020 return (ENOMEM); 8021 } 8022 } 8023 8024 /* options are gone; may need a new label */ 8025 connp = tcp->tcp_connp; 8026 connp->conn_mlp_type = mlptSingle; 8027 connp->conn_ulp_labeled = !is_system_labeled(); 8028 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8029 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8030 tcp->tcp_ip6h = NULL; 8031 tcp->tcp_ipversion = IPV4_VERSION; 8032 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8033 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8034 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8035 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8036 tcp->tcp_ipha->ipha_version_and_hdr_length 8037 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8038 tcp->tcp_ipha->ipha_ident = 0; 8039 8040 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8041 tcp->tcp_tos = 0; 8042 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8043 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8044 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8045 8046 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8047 tcp->tcp_tcph = tcph; 8048 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8049 /* 8050 * IP wants our header length in the checksum field to 8051 * allow it to perform a single pseudo-header+checksum 8052 * calculation on behalf of TCP. 8053 * Include the adjustment for a source route once IP_OPTIONS is set. 8054 */ 8055 sum = sizeof (tcph_t) + tcp->tcp_sum; 8056 sum = (sum >> 16) + (sum & 0xFFFF); 8057 U16_TO_ABE16(sum, tcph->th_sum); 8058 return (0); 8059 } 8060 8061 /* 8062 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8063 */ 8064 static int 8065 tcp_header_init_ipv6(tcp_t *tcp) 8066 { 8067 tcph_t *tcph; 8068 uint32_t sum; 8069 conn_t *connp; 8070 tcp_stack_t *tcps = tcp->tcp_tcps; 8071 8072 /* 8073 * This is a simple initialization. If there's 8074 * already a template, it should never be too small, 8075 * so reuse it. Otherwise, allocate space for the new one. 8076 * Ensure that there is enough space to "downgrade" the tcp_t 8077 * to an IPv4 tcp_t. This requires having space for a full load 8078 * of IPv4 options, as well as a full load of TCP options 8079 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8080 * than a v6 header and a TCP header with a full load of TCP options 8081 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8082 * We want to avoid reallocation in the "downgraded" case when 8083 * processing outbound IPv4 options. 8084 */ 8085 if (tcp->tcp_iphc == NULL) { 8086 ASSERT(tcp->tcp_iphc_len == 0); 8087 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8088 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8089 if (tcp->tcp_iphc == NULL) { 8090 tcp->tcp_iphc_len = 0; 8091 return (ENOMEM); 8092 } 8093 } 8094 8095 /* options are gone; may need a new label */ 8096 connp = tcp->tcp_connp; 8097 connp->conn_mlp_type = mlptSingle; 8098 connp->conn_ulp_labeled = !is_system_labeled(); 8099 8100 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8101 tcp->tcp_ipversion = IPV6_VERSION; 8102 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8103 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8104 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8105 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8106 tcp->tcp_ipha = NULL; 8107 8108 /* Initialize the header template */ 8109 8110 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8111 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8112 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8113 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8114 8115 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8116 tcp->tcp_tcph = tcph; 8117 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8118 /* 8119 * IP wants our header length in the checksum field to 8120 * allow it to perform a single psuedo-header+checksum 8121 * calculation on behalf of TCP. 8122 * Include the adjustment for a source route when IPV6_RTHDR is set. 8123 */ 8124 sum = sizeof (tcph_t) + tcp->tcp_sum; 8125 sum = (sum >> 16) + (sum & 0xFFFF); 8126 U16_TO_ABE16(sum, tcph->th_sum); 8127 return (0); 8128 } 8129 8130 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8131 #define ICMP_MIN_TCP_HDR 8 8132 8133 /* 8134 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8135 * passed up by IP. The message is always received on the correct tcp_t. 8136 * Assumes that IP has pulled up everything up to and including the ICMP header. 8137 */ 8138 void 8139 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8140 { 8141 icmph_t *icmph; 8142 ipha_t *ipha; 8143 int iph_hdr_length; 8144 tcph_t *tcph; 8145 boolean_t ipsec_mctl = B_FALSE; 8146 boolean_t secure; 8147 mblk_t *first_mp = mp; 8148 int32_t new_mss; 8149 uint32_t ratio; 8150 size_t mp_size = MBLKL(mp); 8151 uint32_t seg_seq; 8152 tcp_stack_t *tcps = tcp->tcp_tcps; 8153 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 8154 8155 /* Assume IP provides aligned packets - otherwise toss */ 8156 if (!OK_32PTR(mp->b_rptr)) { 8157 freemsg(mp); 8158 return; 8159 } 8160 8161 /* 8162 * Since ICMP errors are normal data marked with M_CTL when sent 8163 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8164 * packets starting with an ipsec_info_t, see ipsec_info.h. 8165 */ 8166 if ((mp_size == sizeof (ipsec_info_t)) && 8167 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8168 ASSERT(mp->b_cont != NULL); 8169 mp = mp->b_cont; 8170 /* IP should have done this */ 8171 ASSERT(OK_32PTR(mp->b_rptr)); 8172 mp_size = MBLKL(mp); 8173 ipsec_mctl = B_TRUE; 8174 } 8175 8176 /* 8177 * Verify that we have a complete outer IP header. If not, drop it. 8178 */ 8179 if (mp_size < sizeof (ipha_t)) { 8180 noticmpv4: 8181 freemsg(first_mp); 8182 return; 8183 } 8184 8185 ipha = (ipha_t *)mp->b_rptr; 8186 /* 8187 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8188 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8189 */ 8190 switch (IPH_HDR_VERSION(ipha)) { 8191 case IPV6_VERSION: 8192 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8193 return; 8194 case IPV4_VERSION: 8195 break; 8196 default: 8197 goto noticmpv4; 8198 } 8199 8200 /* Skip past the outer IP and ICMP headers */ 8201 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8202 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8203 /* 8204 * If we don't have the correct outer IP header length or if the ULP 8205 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8206 * send it upstream. 8207 */ 8208 if (iph_hdr_length < sizeof (ipha_t) || 8209 ipha->ipha_protocol != IPPROTO_ICMP || 8210 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8211 goto noticmpv4; 8212 } 8213 ipha = (ipha_t *)&icmph[1]; 8214 8215 /* Skip past the inner IP and find the ULP header */ 8216 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8217 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8218 /* 8219 * If we don't have the correct inner IP header length or if the ULP 8220 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8221 * bytes of TCP header, drop it. 8222 */ 8223 if (iph_hdr_length < sizeof (ipha_t) || 8224 ipha->ipha_protocol != IPPROTO_TCP || 8225 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8226 goto noticmpv4; 8227 } 8228 8229 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8230 if (ipsec_mctl) { 8231 secure = ipsec_in_is_secure(first_mp); 8232 } else { 8233 secure = B_FALSE; 8234 } 8235 if (secure) { 8236 /* 8237 * If we are willing to accept this in clear 8238 * we don't have to verify policy. 8239 */ 8240 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8241 if (!tcp_check_policy(tcp, first_mp, 8242 ipha, NULL, secure, ipsec_mctl)) { 8243 /* 8244 * tcp_check_policy called 8245 * ip_drop_packet() on failure. 8246 */ 8247 return; 8248 } 8249 } 8250 } 8251 } else if (ipsec_mctl) { 8252 /* 8253 * This is a hard_bound connection. IP has already 8254 * verified policy. We don't have to do it again. 8255 */ 8256 freeb(first_mp); 8257 first_mp = mp; 8258 ipsec_mctl = B_FALSE; 8259 } 8260 8261 seg_seq = ABE32_TO_U32(tcph->th_seq); 8262 /* 8263 * TCP SHOULD check that the TCP sequence number contained in 8264 * payload of the ICMP error message is within the range 8265 * SND.UNA <= SEG.SEQ < SND.NXT. 8266 */ 8267 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8268 /* 8269 * The ICMP message is bogus, just drop it. But if this is 8270 * an ICMP too big message, IP has already changed 8271 * the ire_max_frag to the bogus value. We need to change 8272 * it back. 8273 */ 8274 if (icmph->icmph_type == ICMP_DEST_UNREACHABLE && 8275 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 8276 conn_t *connp = tcp->tcp_connp; 8277 ire_t *ire; 8278 int flag; 8279 8280 if (tcp->tcp_ipversion == IPV4_VERSION) { 8281 flag = tcp->tcp_ipha-> 8282 ipha_fragment_offset_and_flags; 8283 } else { 8284 flag = 0; 8285 } 8286 mutex_enter(&connp->conn_lock); 8287 if ((ire = connp->conn_ire_cache) != NULL) { 8288 mutex_enter(&ire->ire_lock); 8289 mutex_exit(&connp->conn_lock); 8290 ire->ire_max_frag = tcp->tcp_if_mtu; 8291 ire->ire_frag_flag |= flag; 8292 mutex_exit(&ire->ire_lock); 8293 } else { 8294 mutex_exit(&connp->conn_lock); 8295 } 8296 } 8297 goto noticmpv4; 8298 } 8299 8300 switch (icmph->icmph_type) { 8301 case ICMP_DEST_UNREACHABLE: 8302 switch (icmph->icmph_code) { 8303 case ICMP_FRAGMENTATION_NEEDED: 8304 /* 8305 * Reduce the MSS based on the new MTU. This will 8306 * eliminate any fragmentation locally. 8307 * N.B. There may well be some funny side-effects on 8308 * the local send policy and the remote receive policy. 8309 * Pending further research, we provide 8310 * tcp_ignore_path_mtu just in case this proves 8311 * disastrous somewhere. 8312 * 8313 * After updating the MSS, retransmit part of the 8314 * dropped segment using the new mss by calling 8315 * tcp_wput_data(). Need to adjust all those 8316 * params to make sure tcp_wput_data() work properly. 8317 */ 8318 if (tcps->tcps_ignore_path_mtu || 8319 tcp->tcp_ipha->ipha_fragment_offset_and_flags == 0) 8320 break; 8321 8322 /* 8323 * Decrease the MSS by time stamp options 8324 * IP options and IPSEC options. tcp_hdr_len 8325 * includes time stamp option and IP option 8326 * length. Note that new_mss may be negative 8327 * if tcp_ipsec_overhead is large and the 8328 * icmph_du_mtu is the minimum value, which is 68. 8329 */ 8330 new_mss = ntohs(icmph->icmph_du_mtu) - 8331 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8332 8333 DTRACE_PROBE2(tcp__pmtu__change, tcp_t *, tcp, int, 8334 new_mss); 8335 8336 /* 8337 * Only update the MSS if the new one is 8338 * smaller than the previous one. This is 8339 * to avoid problems when getting multiple 8340 * ICMP errors for the same MTU. 8341 */ 8342 if (new_mss >= tcp->tcp_mss) 8343 break; 8344 8345 /* 8346 * Note that we are using the template header's DF 8347 * bit in the fast path sending. So we need to compare 8348 * the new mss with both tcps_mss_min and ip_pmtu_min. 8349 * And stop doing IPv4 PMTUd if new_mss is less than 8350 * MAX(tcps_mss_min, ip_pmtu_min). 8351 */ 8352 if (new_mss < tcps->tcps_mss_min || 8353 new_mss < ipst->ips_ip_pmtu_min) { 8354 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8355 0; 8356 } 8357 8358 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8359 ASSERT(ratio >= 1); 8360 tcp_mss_set(tcp, new_mss, B_TRUE); 8361 8362 /* 8363 * Make sure we have something to 8364 * send. 8365 */ 8366 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8367 (tcp->tcp_xmit_head != NULL)) { 8368 /* 8369 * Shrink tcp_cwnd in 8370 * proportion to the old MSS/new MSS. 8371 */ 8372 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8373 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8374 (tcp->tcp_unsent == 0)) { 8375 tcp->tcp_rexmit_max = tcp->tcp_fss; 8376 } else { 8377 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8378 } 8379 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8380 tcp->tcp_rexmit = B_TRUE; 8381 tcp->tcp_dupack_cnt = 0; 8382 tcp->tcp_snd_burst = TCP_CWND_SS; 8383 tcp_ss_rexmit(tcp); 8384 } 8385 break; 8386 case ICMP_PORT_UNREACHABLE: 8387 case ICMP_PROTOCOL_UNREACHABLE: 8388 switch (tcp->tcp_state) { 8389 case TCPS_SYN_SENT: 8390 case TCPS_SYN_RCVD: 8391 /* 8392 * ICMP can snipe away incipient 8393 * TCP connections as long as 8394 * seq number is same as initial 8395 * send seq number. 8396 */ 8397 if (seg_seq == tcp->tcp_iss) { 8398 (void) tcp_clean_death(tcp, 8399 ECONNREFUSED, 6); 8400 } 8401 break; 8402 } 8403 break; 8404 case ICMP_HOST_UNREACHABLE: 8405 case ICMP_NET_UNREACHABLE: 8406 /* Record the error in case we finally time out. */ 8407 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8408 tcp->tcp_client_errno = EHOSTUNREACH; 8409 else 8410 tcp->tcp_client_errno = ENETUNREACH; 8411 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8412 if (tcp->tcp_listener != NULL && 8413 tcp->tcp_listener->tcp_syn_defense) { 8414 /* 8415 * Ditch the half-open connection if we 8416 * suspect a SYN attack is under way. 8417 */ 8418 tcp_ip_ire_mark_advice(tcp); 8419 (void) tcp_clean_death(tcp, 8420 tcp->tcp_client_errno, 7); 8421 } 8422 } 8423 break; 8424 default: 8425 break; 8426 } 8427 break; 8428 case ICMP_SOURCE_QUENCH: { 8429 /* 8430 * use a global boolean to control 8431 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8432 * The default is false. 8433 */ 8434 if (tcp_icmp_source_quench) { 8435 /* 8436 * Reduce the sending rate as if we got a 8437 * retransmit timeout 8438 */ 8439 uint32_t npkt; 8440 8441 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8442 tcp->tcp_mss; 8443 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8444 tcp->tcp_cwnd = tcp->tcp_mss; 8445 tcp->tcp_cwnd_cnt = 0; 8446 } 8447 break; 8448 } 8449 } 8450 freemsg(first_mp); 8451 } 8452 8453 /* 8454 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8455 * error messages passed up by IP. 8456 * Assumes that IP has pulled up all the extension headers as well 8457 * as the ICMPv6 header. 8458 */ 8459 static void 8460 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8461 { 8462 icmp6_t *icmp6; 8463 ip6_t *ip6h; 8464 uint16_t iph_hdr_length; 8465 tcpha_t *tcpha; 8466 uint8_t *nexthdrp; 8467 uint32_t new_mss; 8468 uint32_t ratio; 8469 boolean_t secure; 8470 mblk_t *first_mp = mp; 8471 size_t mp_size; 8472 uint32_t seg_seq; 8473 tcp_stack_t *tcps = tcp->tcp_tcps; 8474 8475 /* 8476 * The caller has determined if this is an IPSEC_IN packet and 8477 * set ipsec_mctl appropriately (see tcp_icmp_error). 8478 */ 8479 if (ipsec_mctl) 8480 mp = mp->b_cont; 8481 8482 mp_size = MBLKL(mp); 8483 8484 /* 8485 * Verify that we have a complete IP header. If not, send it upstream. 8486 */ 8487 if (mp_size < sizeof (ip6_t)) { 8488 noticmpv6: 8489 freemsg(first_mp); 8490 return; 8491 } 8492 8493 /* 8494 * Verify this is an ICMPV6 packet, else send it upstream. 8495 */ 8496 ip6h = (ip6_t *)mp->b_rptr; 8497 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8498 iph_hdr_length = IPV6_HDR_LEN; 8499 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8500 &nexthdrp) || 8501 *nexthdrp != IPPROTO_ICMPV6) { 8502 goto noticmpv6; 8503 } 8504 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8505 ip6h = (ip6_t *)&icmp6[1]; 8506 /* 8507 * Verify if we have a complete ICMP and inner IP header. 8508 */ 8509 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8510 goto noticmpv6; 8511 8512 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8513 goto noticmpv6; 8514 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8515 /* 8516 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8517 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8518 * packet. 8519 */ 8520 if ((*nexthdrp != IPPROTO_TCP) || 8521 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8522 goto noticmpv6; 8523 } 8524 8525 /* 8526 * ICMP errors come on the right queue or come on 8527 * listener/global queue for detached connections and 8528 * get switched to the right queue. If it comes on the 8529 * right queue, policy check has already been done by IP 8530 * and thus free the first_mp without verifying the policy. 8531 * If it has come for a non-hard bound connection, we need 8532 * to verify policy as IP may not have done it. 8533 */ 8534 if (!tcp->tcp_hard_bound) { 8535 if (ipsec_mctl) { 8536 secure = ipsec_in_is_secure(first_mp); 8537 } else { 8538 secure = B_FALSE; 8539 } 8540 if (secure) { 8541 /* 8542 * If we are willing to accept this in clear 8543 * we don't have to verify policy. 8544 */ 8545 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8546 if (!tcp_check_policy(tcp, first_mp, 8547 NULL, ip6h, secure, ipsec_mctl)) { 8548 /* 8549 * tcp_check_policy called 8550 * ip_drop_packet() on failure. 8551 */ 8552 return; 8553 } 8554 } 8555 } 8556 } else if (ipsec_mctl) { 8557 /* 8558 * This is a hard_bound connection. IP has already 8559 * verified policy. We don't have to do it again. 8560 */ 8561 freeb(first_mp); 8562 first_mp = mp; 8563 ipsec_mctl = B_FALSE; 8564 } 8565 8566 seg_seq = ntohl(tcpha->tha_seq); 8567 /* 8568 * TCP SHOULD check that the TCP sequence number contained in 8569 * payload of the ICMP error message is within the range 8570 * SND.UNA <= SEG.SEQ < SND.NXT. 8571 */ 8572 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8573 /* 8574 * If the ICMP message is bogus, should we kill the 8575 * connection, or should we just drop the bogus ICMP 8576 * message? It would probably make more sense to just 8577 * drop the message so that if this one managed to get 8578 * in, the real connection should not suffer. 8579 */ 8580 goto noticmpv6; 8581 } 8582 8583 switch (icmp6->icmp6_type) { 8584 case ICMP6_PACKET_TOO_BIG: 8585 /* 8586 * Reduce the MSS based on the new MTU. This will 8587 * eliminate any fragmentation locally. 8588 * N.B. There may well be some funny side-effects on 8589 * the local send policy and the remote receive policy. 8590 * Pending further research, we provide 8591 * tcp_ignore_path_mtu just in case this proves 8592 * disastrous somewhere. 8593 * 8594 * After updating the MSS, retransmit part of the 8595 * dropped segment using the new mss by calling 8596 * tcp_wput_data(). Need to adjust all those 8597 * params to make sure tcp_wput_data() work properly. 8598 */ 8599 if (tcps->tcps_ignore_path_mtu) 8600 break; 8601 8602 /* 8603 * Decrease the MSS by time stamp options 8604 * IP options and IPSEC options. tcp_hdr_len 8605 * includes time stamp option and IP option 8606 * length. 8607 */ 8608 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8609 tcp->tcp_ipsec_overhead; 8610 8611 /* 8612 * Only update the MSS if the new one is 8613 * smaller than the previous one. This is 8614 * to avoid problems when getting multiple 8615 * ICMP errors for the same MTU. 8616 */ 8617 if (new_mss >= tcp->tcp_mss) 8618 break; 8619 8620 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8621 ASSERT(ratio >= 1); 8622 tcp_mss_set(tcp, new_mss, B_TRUE); 8623 8624 /* 8625 * Make sure we have something to 8626 * send. 8627 */ 8628 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8629 (tcp->tcp_xmit_head != NULL)) { 8630 /* 8631 * Shrink tcp_cwnd in 8632 * proportion to the old MSS/new MSS. 8633 */ 8634 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8635 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8636 (tcp->tcp_unsent == 0)) { 8637 tcp->tcp_rexmit_max = tcp->tcp_fss; 8638 } else { 8639 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8640 } 8641 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8642 tcp->tcp_rexmit = B_TRUE; 8643 tcp->tcp_dupack_cnt = 0; 8644 tcp->tcp_snd_burst = TCP_CWND_SS; 8645 tcp_ss_rexmit(tcp); 8646 } 8647 break; 8648 8649 case ICMP6_DST_UNREACH: 8650 switch (icmp6->icmp6_code) { 8651 case ICMP6_DST_UNREACH_NOPORT: 8652 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8653 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8654 (seg_seq == tcp->tcp_iss)) { 8655 (void) tcp_clean_death(tcp, 8656 ECONNREFUSED, 8); 8657 } 8658 break; 8659 8660 case ICMP6_DST_UNREACH_ADMIN: 8661 case ICMP6_DST_UNREACH_NOROUTE: 8662 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8663 case ICMP6_DST_UNREACH_ADDR: 8664 /* Record the error in case we finally time out. */ 8665 tcp->tcp_client_errno = EHOSTUNREACH; 8666 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8667 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8668 (seg_seq == tcp->tcp_iss)) { 8669 if (tcp->tcp_listener != NULL && 8670 tcp->tcp_listener->tcp_syn_defense) { 8671 /* 8672 * Ditch the half-open connection if we 8673 * suspect a SYN attack is under way. 8674 */ 8675 tcp_ip_ire_mark_advice(tcp); 8676 (void) tcp_clean_death(tcp, 8677 tcp->tcp_client_errno, 9); 8678 } 8679 } 8680 8681 8682 break; 8683 default: 8684 break; 8685 } 8686 break; 8687 8688 case ICMP6_PARAM_PROB: 8689 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8690 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8691 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8692 (uchar_t *)nexthdrp) { 8693 if (tcp->tcp_state == TCPS_SYN_SENT || 8694 tcp->tcp_state == TCPS_SYN_RCVD) { 8695 (void) tcp_clean_death(tcp, 8696 ECONNREFUSED, 10); 8697 } 8698 break; 8699 } 8700 break; 8701 8702 case ICMP6_TIME_EXCEEDED: 8703 default: 8704 break; 8705 } 8706 freemsg(first_mp); 8707 } 8708 8709 /* 8710 * Notify IP that we are having trouble with this connection. IP should 8711 * blow the IRE away and start over. 8712 */ 8713 static void 8714 tcp_ip_notify(tcp_t *tcp) 8715 { 8716 struct iocblk *iocp; 8717 ipid_t *ipid; 8718 mblk_t *mp; 8719 8720 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 8721 if (tcp->tcp_ipversion == IPV6_VERSION) 8722 return; 8723 8724 mp = mkiocb(IP_IOCTL); 8725 if (mp == NULL) 8726 return; 8727 8728 iocp = (struct iocblk *)mp->b_rptr; 8729 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 8730 8731 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 8732 if (!mp->b_cont) { 8733 freeb(mp); 8734 return; 8735 } 8736 8737 ipid = (ipid_t *)mp->b_cont->b_rptr; 8738 mp->b_cont->b_wptr += iocp->ioc_count; 8739 bzero(ipid, sizeof (*ipid)); 8740 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 8741 ipid->ipid_ire_type = IRE_CACHE; 8742 ipid->ipid_addr_offset = sizeof (ipid_t); 8743 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 8744 /* 8745 * Note: in the case of source routing we want to blow away the 8746 * route to the first source route hop. 8747 */ 8748 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 8749 sizeof (tcp->tcp_ipha->ipha_dst)); 8750 8751 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 8752 } 8753 8754 /* Unlink and return any mblk that looks like it contains an ire */ 8755 static mblk_t * 8756 tcp_ire_mp(mblk_t **mpp) 8757 { 8758 mblk_t *mp = *mpp; 8759 mblk_t *prev_mp = NULL; 8760 8761 for (;;) { 8762 switch (DB_TYPE(mp)) { 8763 case IRE_DB_TYPE: 8764 case IRE_DB_REQ_TYPE: 8765 if (mp == *mpp) { 8766 *mpp = mp->b_cont; 8767 } else { 8768 prev_mp->b_cont = mp->b_cont; 8769 } 8770 mp->b_cont = NULL; 8771 return (mp); 8772 default: 8773 break; 8774 } 8775 prev_mp = mp; 8776 mp = mp->b_cont; 8777 if (mp == NULL) 8778 break; 8779 } 8780 return (mp); 8781 } 8782 8783 /* 8784 * Timer callback routine for keepalive probe. We do a fake resend of 8785 * last ACKed byte. Then set a timer using RTO. When the timer expires, 8786 * check to see if we have heard anything from the other end for the last 8787 * RTO period. If we have, set the timer to expire for another 8788 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 8789 * RTO << 1 and check again when it expires. Keep exponentially increasing 8790 * the timeout if we have not heard from the other side. If for more than 8791 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 8792 * kill the connection unless the keepalive abort threshold is 0. In 8793 * that case, we will probe "forever." 8794 */ 8795 static void 8796 tcp_keepalive_killer(void *arg) 8797 { 8798 mblk_t *mp; 8799 conn_t *connp = (conn_t *)arg; 8800 tcp_t *tcp = connp->conn_tcp; 8801 int32_t firetime; 8802 int32_t idletime; 8803 int32_t ka_intrvl; 8804 tcp_stack_t *tcps = tcp->tcp_tcps; 8805 8806 tcp->tcp_ka_tid = 0; 8807 8808 if (tcp->tcp_fused) 8809 return; 8810 8811 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 8812 ka_intrvl = tcp->tcp_ka_interval; 8813 8814 /* 8815 * Keepalive probe should only be sent if the application has not 8816 * done a close on the connection. 8817 */ 8818 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 8819 return; 8820 } 8821 /* Timer fired too early, restart it. */ 8822 if (tcp->tcp_state < TCPS_ESTABLISHED) { 8823 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8824 MSEC_TO_TICK(ka_intrvl)); 8825 return; 8826 } 8827 8828 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 8829 /* 8830 * If we have not heard from the other side for a long 8831 * time, kill the connection unless the keepalive abort 8832 * threshold is 0. In that case, we will probe "forever." 8833 */ 8834 if (tcp->tcp_ka_abort_thres != 0 && 8835 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 8836 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 8837 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 8838 tcp->tcp_client_errno : ETIMEDOUT, 11); 8839 return; 8840 } 8841 8842 if (tcp->tcp_snxt == tcp->tcp_suna && 8843 idletime >= ka_intrvl) { 8844 /* Fake resend of last ACKed byte. */ 8845 mblk_t *mp1 = allocb(1, BPRI_LO); 8846 8847 if (mp1 != NULL) { 8848 *mp1->b_wptr++ = '\0'; 8849 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 8850 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 8851 freeb(mp1); 8852 /* 8853 * if allocation failed, fall through to start the 8854 * timer back. 8855 */ 8856 if (mp != NULL) { 8857 tcp_send_data(tcp, tcp->tcp_wq, mp); 8858 BUMP_MIB(&tcps->tcps_mib, 8859 tcpTimKeepaliveProbe); 8860 if (tcp->tcp_ka_last_intrvl != 0) { 8861 int max; 8862 /* 8863 * We should probe again at least 8864 * in ka_intrvl, but not more than 8865 * tcp_rexmit_interval_max. 8866 */ 8867 max = tcps->tcps_rexmit_interval_max; 8868 firetime = MIN(ka_intrvl - 1, 8869 tcp->tcp_ka_last_intrvl << 1); 8870 if (firetime > max) 8871 firetime = max; 8872 } else { 8873 firetime = tcp->tcp_rto; 8874 } 8875 tcp->tcp_ka_tid = TCP_TIMER(tcp, 8876 tcp_keepalive_killer, 8877 MSEC_TO_TICK(firetime)); 8878 tcp->tcp_ka_last_intrvl = firetime; 8879 return; 8880 } 8881 } 8882 } else { 8883 tcp->tcp_ka_last_intrvl = 0; 8884 } 8885 8886 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 8887 if ((firetime = ka_intrvl - idletime) < 0) { 8888 firetime = ka_intrvl; 8889 } 8890 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8891 MSEC_TO_TICK(firetime)); 8892 } 8893 8894 int 8895 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 8896 { 8897 queue_t *q = tcp->tcp_rq; 8898 int32_t mss = tcp->tcp_mss; 8899 int maxpsz; 8900 conn_t *connp = tcp->tcp_connp; 8901 8902 if (TCP_IS_DETACHED(tcp)) 8903 return (mss); 8904 if (tcp->tcp_fused) { 8905 maxpsz = tcp_fuse_maxpsz_set(tcp); 8906 mss = INFPSZ; 8907 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 8908 /* 8909 * Set the sd_qn_maxpsz according to the socket send buffer 8910 * size, and sd_maxblk to INFPSZ (-1). This will essentially 8911 * instruct the stream head to copyin user data into contiguous 8912 * kernel-allocated buffers without breaking it up into smaller 8913 * chunks. We round up the buffer size to the nearest SMSS. 8914 */ 8915 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 8916 if (tcp->tcp_kssl_ctx == NULL) 8917 mss = INFPSZ; 8918 else 8919 mss = SSL3_MAX_RECORD_LEN; 8920 } else { 8921 /* 8922 * Set sd_qn_maxpsz to approx half the (receivers) buffer 8923 * (and a multiple of the mss). This instructs the stream 8924 * head to break down larger than SMSS writes into SMSS- 8925 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 8926 */ 8927 /* XXX tune this with ndd tcp_maxpsz_multiplier */ 8928 maxpsz = tcp->tcp_maxpsz * mss; 8929 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 8930 maxpsz = tcp->tcp_xmit_hiwater/2; 8931 /* Round up to nearest mss */ 8932 maxpsz = MSS_ROUNDUP(maxpsz, mss); 8933 } 8934 } 8935 8936 (void) proto_set_maxpsz(q, connp, maxpsz); 8937 if (!(IPCL_IS_NONSTR(connp))) { 8938 /* XXX do it in set_maxpsz()? */ 8939 tcp->tcp_wq->q_maxpsz = maxpsz; 8940 } 8941 8942 if (set_maxblk) 8943 (void) proto_set_tx_maxblk(q, connp, mss); 8944 return (mss); 8945 } 8946 8947 /* 8948 * Extract option values from a tcp header. We put any found values into the 8949 * tcpopt struct and return a bitmask saying which options were found. 8950 */ 8951 static int 8952 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 8953 { 8954 uchar_t *endp; 8955 int len; 8956 uint32_t mss; 8957 uchar_t *up = (uchar_t *)tcph; 8958 int found = 0; 8959 int32_t sack_len; 8960 tcp_seq sack_begin, sack_end; 8961 tcp_t *tcp; 8962 8963 endp = up + TCP_HDR_LENGTH(tcph); 8964 up += TCP_MIN_HEADER_LENGTH; 8965 while (up < endp) { 8966 len = endp - up; 8967 switch (*up) { 8968 case TCPOPT_EOL: 8969 break; 8970 8971 case TCPOPT_NOP: 8972 up++; 8973 continue; 8974 8975 case TCPOPT_MAXSEG: 8976 if (len < TCPOPT_MAXSEG_LEN || 8977 up[1] != TCPOPT_MAXSEG_LEN) 8978 break; 8979 8980 mss = BE16_TO_U16(up+2); 8981 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 8982 tcpopt->tcp_opt_mss = mss; 8983 found |= TCP_OPT_MSS_PRESENT; 8984 8985 up += TCPOPT_MAXSEG_LEN; 8986 continue; 8987 8988 case TCPOPT_WSCALE: 8989 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 8990 break; 8991 8992 if (up[2] > TCP_MAX_WINSHIFT) 8993 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 8994 else 8995 tcpopt->tcp_opt_wscale = up[2]; 8996 found |= TCP_OPT_WSCALE_PRESENT; 8997 8998 up += TCPOPT_WS_LEN; 8999 continue; 9000 9001 case TCPOPT_SACK_PERMITTED: 9002 if (len < TCPOPT_SACK_OK_LEN || 9003 up[1] != TCPOPT_SACK_OK_LEN) 9004 break; 9005 found |= TCP_OPT_SACK_OK_PRESENT; 9006 up += TCPOPT_SACK_OK_LEN; 9007 continue; 9008 9009 case TCPOPT_SACK: 9010 if (len <= 2 || up[1] <= 2 || len < up[1]) 9011 break; 9012 9013 /* If TCP is not interested in SACK blks... */ 9014 if ((tcp = tcpopt->tcp) == NULL) { 9015 up += up[1]; 9016 continue; 9017 } 9018 sack_len = up[1] - TCPOPT_HEADER_LEN; 9019 up += TCPOPT_HEADER_LEN; 9020 9021 /* 9022 * If the list is empty, allocate one and assume 9023 * nothing is sack'ed. 9024 */ 9025 ASSERT(tcp->tcp_sack_info != NULL); 9026 if (tcp->tcp_notsack_list == NULL) { 9027 tcp_notsack_update(&(tcp->tcp_notsack_list), 9028 tcp->tcp_suna, tcp->tcp_snxt, 9029 &(tcp->tcp_num_notsack_blk), 9030 &(tcp->tcp_cnt_notsack_list)); 9031 9032 /* 9033 * Make sure tcp_notsack_list is not NULL. 9034 * This happens when kmem_alloc(KM_NOSLEEP) 9035 * returns NULL. 9036 */ 9037 if (tcp->tcp_notsack_list == NULL) { 9038 up += sack_len; 9039 continue; 9040 } 9041 tcp->tcp_fack = tcp->tcp_suna; 9042 } 9043 9044 while (sack_len > 0) { 9045 if (up + 8 > endp) { 9046 up = endp; 9047 break; 9048 } 9049 sack_begin = BE32_TO_U32(up); 9050 up += 4; 9051 sack_end = BE32_TO_U32(up); 9052 up += 4; 9053 sack_len -= 8; 9054 /* 9055 * Bounds checking. Make sure the SACK 9056 * info is within tcp_suna and tcp_snxt. 9057 * If this SACK blk is out of bound, ignore 9058 * it but continue to parse the following 9059 * blks. 9060 */ 9061 if (SEQ_LEQ(sack_end, sack_begin) || 9062 SEQ_LT(sack_begin, tcp->tcp_suna) || 9063 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9064 continue; 9065 } 9066 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9067 sack_begin, sack_end, 9068 &(tcp->tcp_num_notsack_blk), 9069 &(tcp->tcp_cnt_notsack_list)); 9070 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9071 tcp->tcp_fack = sack_end; 9072 } 9073 } 9074 found |= TCP_OPT_SACK_PRESENT; 9075 continue; 9076 9077 case TCPOPT_TSTAMP: 9078 if (len < TCPOPT_TSTAMP_LEN || 9079 up[1] != TCPOPT_TSTAMP_LEN) 9080 break; 9081 9082 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9083 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9084 9085 found |= TCP_OPT_TSTAMP_PRESENT; 9086 9087 up += TCPOPT_TSTAMP_LEN; 9088 continue; 9089 9090 default: 9091 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9092 break; 9093 up += up[1]; 9094 continue; 9095 } 9096 break; 9097 } 9098 return (found); 9099 } 9100 9101 /* 9102 * Set the mss associated with a particular tcp based on its current value, 9103 * and a new one passed in. Observe minimums and maximums, and reset 9104 * other state variables that we want to view as multiples of mss. 9105 * 9106 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9107 * highwater marks etc. need to be initialized or adjusted. 9108 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9109 * packet arrives. 9110 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9111 * ICMP6_PACKET_TOO_BIG arrives. 9112 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9113 * to increase the MSS to use the extra bytes available. 9114 * 9115 * Callers except tcp_paws_check() ensure that they only reduce mss. 9116 */ 9117 static void 9118 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9119 { 9120 uint32_t mss_max; 9121 tcp_stack_t *tcps = tcp->tcp_tcps; 9122 9123 if (tcp->tcp_ipversion == IPV4_VERSION) 9124 mss_max = tcps->tcps_mss_max_ipv4; 9125 else 9126 mss_max = tcps->tcps_mss_max_ipv6; 9127 9128 if (mss < tcps->tcps_mss_min) 9129 mss = tcps->tcps_mss_min; 9130 if (mss > mss_max) 9131 mss = mss_max; 9132 /* 9133 * Unless naglim has been set by our client to 9134 * a non-mss value, force naglim to track mss. 9135 * This can help to aggregate small writes. 9136 */ 9137 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9138 tcp->tcp_naglim = mss; 9139 /* 9140 * TCP should be able to buffer at least 4 MSS data for obvious 9141 * performance reason. 9142 */ 9143 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9144 tcp->tcp_xmit_hiwater = mss << 2; 9145 9146 /* 9147 * Set the xmit_lowater to at least twice of MSS. 9148 */ 9149 if ((mss << 1) > tcp->tcp_xmit_lowater) 9150 tcp->tcp_xmit_lowater = mss << 1; 9151 9152 if (do_ss) { 9153 /* 9154 * Either the tcp_cwnd is as yet uninitialized, or mss is 9155 * changing due to a reduction in MTU, presumably as a 9156 * result of a new path component, reset cwnd to its 9157 * "initial" value, as a multiple of the new mss. 9158 */ 9159 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9160 } else { 9161 /* 9162 * Called by tcp_paws_check(), the mss increased 9163 * marginally to allow use of space previously taken 9164 * by the timestamp option. It would be inappropriate 9165 * to apply slow start or tcp_init_cwnd values to 9166 * tcp_cwnd, simply adjust to a multiple of the new mss. 9167 */ 9168 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9169 tcp->tcp_cwnd_cnt = 0; 9170 } 9171 tcp->tcp_mss = mss; 9172 (void) tcp_maxpsz_set(tcp, B_TRUE); 9173 } 9174 9175 /* For /dev/tcp aka AF_INET open */ 9176 static int 9177 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9178 { 9179 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9180 } 9181 9182 /* For /dev/tcp6 aka AF_INET6 open */ 9183 static int 9184 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9185 { 9186 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9187 } 9188 9189 static conn_t * 9190 tcp_create_common(queue_t *q, cred_t *credp, boolean_t isv6, 9191 boolean_t issocket, int *errorp) 9192 { 9193 tcp_t *tcp = NULL; 9194 conn_t *connp; 9195 int err; 9196 zoneid_t zoneid; 9197 tcp_stack_t *tcps; 9198 squeue_t *sqp; 9199 9200 ASSERT(errorp != NULL); 9201 /* 9202 * Find the proper zoneid and netstack. 9203 */ 9204 /* 9205 * Special case for install: miniroot needs to be able to 9206 * access files via NFS as though it were always in the 9207 * global zone. 9208 */ 9209 if (credp == kcred && nfs_global_client_only != 0) { 9210 zoneid = GLOBAL_ZONEID; 9211 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9212 netstack_tcp; 9213 ASSERT(tcps != NULL); 9214 } else { 9215 netstack_t *ns; 9216 9217 ns = netstack_find_by_cred(credp); 9218 ASSERT(ns != NULL); 9219 tcps = ns->netstack_tcp; 9220 ASSERT(tcps != NULL); 9221 9222 /* 9223 * For exclusive stacks we set the zoneid to zero 9224 * to make TCP operate as if in the global zone. 9225 */ 9226 if (tcps->tcps_netstack->netstack_stackid != 9227 GLOBAL_NETSTACKID) 9228 zoneid = GLOBAL_ZONEID; 9229 else 9230 zoneid = crgetzoneid(credp); 9231 } 9232 /* 9233 * For stackid zero this is done from strplumb.c, but 9234 * non-zero stackids are handled here. 9235 */ 9236 if (tcps->tcps_g_q == NULL && 9237 tcps->tcps_netstack->netstack_stackid != 9238 GLOBAL_NETSTACKID) { 9239 tcp_g_q_setup(tcps); 9240 } 9241 9242 sqp = IP_SQUEUE_GET((uint_t)gethrtime()); 9243 connp = (conn_t *)tcp_get_conn(sqp, tcps); 9244 /* 9245 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9246 * so we drop it by one. 9247 */ 9248 netstack_rele(tcps->tcps_netstack); 9249 if (connp == NULL) { 9250 *errorp = ENOSR; 9251 return (NULL); 9252 } 9253 connp->conn_sqp = sqp; 9254 connp->conn_initial_sqp = connp->conn_sqp; 9255 tcp = connp->conn_tcp; 9256 9257 if (isv6) { 9258 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9259 connp->conn_send = ip_output_v6; 9260 connp->conn_af_isv6 = B_TRUE; 9261 connp->conn_pkt_isv6 = B_TRUE; 9262 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9263 tcp->tcp_ipversion = IPV6_VERSION; 9264 tcp->tcp_family = AF_INET6; 9265 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9266 } else { 9267 connp->conn_flags |= IPCL_TCP4; 9268 connp->conn_send = ip_output; 9269 connp->conn_af_isv6 = B_FALSE; 9270 connp->conn_pkt_isv6 = B_FALSE; 9271 tcp->tcp_ipversion = IPV4_VERSION; 9272 tcp->tcp_family = AF_INET; 9273 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9274 } 9275 9276 /* 9277 * TCP keeps a copy of cred for cache locality reasons but 9278 * we put a reference only once. If connp->conn_cred 9279 * becomes invalid, tcp_cred should also be set to NULL. 9280 */ 9281 tcp->tcp_cred = connp->conn_cred = credp; 9282 crhold(connp->conn_cred); 9283 tcp->tcp_cpid = curproc->p_pid; 9284 tcp->tcp_open_time = lbolt64; 9285 connp->conn_zoneid = zoneid; 9286 connp->conn_mlp_type = mlptSingle; 9287 connp->conn_ulp_labeled = !is_system_labeled(); 9288 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9289 ASSERT(tcp->tcp_tcps == tcps); 9290 9291 /* 9292 * If the caller has the process-wide flag set, then default to MAC 9293 * exempt mode. This allows read-down to unlabeled hosts. 9294 */ 9295 if (getpflags(NET_MAC_AWARE, credp) != 0) 9296 connp->conn_mac_exempt = B_TRUE; 9297 9298 connp->conn_dev = NULL; 9299 if (issocket) { 9300 connp->conn_flags |= IPCL_SOCKET; 9301 tcp->tcp_issocket = 1; 9302 } 9303 9304 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 9305 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9306 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 9307 9308 /* Non-zero default values */ 9309 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9310 9311 if (q == NULL) { 9312 /* 9313 * Create a helper stream for non-STREAMS socket. 9314 */ 9315 err = ip_create_helper_stream(connp, tcps->tcps_ldi_ident); 9316 if (err != 0) { 9317 ip1dbg(("tcp_create_common: create of IP helper stream " 9318 "failed\n")); 9319 CONN_DEC_REF(connp); 9320 *errorp = err; 9321 return (NULL); 9322 } 9323 q = connp->conn_rq; 9324 } else { 9325 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9326 } 9327 9328 SOCK_CONNID_INIT(tcp->tcp_connid); 9329 err = tcp_init(tcp, q); 9330 if (err != 0) { 9331 CONN_DEC_REF(connp); 9332 *errorp = err; 9333 return (NULL); 9334 } 9335 9336 return (connp); 9337 } 9338 9339 static int 9340 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9341 boolean_t isv6) 9342 { 9343 tcp_t *tcp = NULL; 9344 conn_t *connp = NULL; 9345 int err; 9346 vmem_t *minor_arena = NULL; 9347 dev_t conn_dev; 9348 boolean_t issocket; 9349 9350 if (q->q_ptr != NULL) 9351 return (0); 9352 9353 if (sflag == MODOPEN) 9354 return (EINVAL); 9355 9356 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9357 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9358 minor_arena = ip_minor_arena_la; 9359 } else { 9360 /* 9361 * Either minor numbers in the large arena were exhausted 9362 * or a non socket application is doing the open. 9363 * Try to allocate from the small arena. 9364 */ 9365 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9366 return (EBUSY); 9367 } 9368 minor_arena = ip_minor_arena_sa; 9369 } 9370 9371 ASSERT(minor_arena != NULL); 9372 9373 *devp = makedevice(getmajor(*devp), (minor_t)conn_dev); 9374 9375 if (flag & SO_FALLBACK) { 9376 /* 9377 * Non streams socket needs a stream to fallback to 9378 */ 9379 RD(q)->q_ptr = (void *)conn_dev; 9380 WR(q)->q_qinfo = &tcp_fallback_sock_winit; 9381 WR(q)->q_ptr = (void *)minor_arena; 9382 qprocson(q); 9383 return (0); 9384 } else if (flag & SO_ACCEPTOR) { 9385 q->q_qinfo = &tcp_acceptor_rinit; 9386 /* 9387 * the conn_dev and minor_arena will be subsequently used by 9388 * tcp_wput_accept() and tcp_tpi_close_accept() to figure out 9389 * the minor device number for this connection from the q_ptr. 9390 */ 9391 RD(q)->q_ptr = (void *)conn_dev; 9392 WR(q)->q_qinfo = &tcp_acceptor_winit; 9393 WR(q)->q_ptr = (void *)minor_arena; 9394 qprocson(q); 9395 return (0); 9396 } 9397 9398 issocket = flag & SO_SOCKSTR; 9399 connp = tcp_create_common(q, credp, isv6, issocket, &err); 9400 9401 if (connp == NULL) { 9402 inet_minor_free(minor_arena, conn_dev); 9403 q->q_ptr = WR(q)->q_ptr = NULL; 9404 return (err); 9405 } 9406 9407 q->q_ptr = WR(q)->q_ptr = connp; 9408 9409 connp->conn_dev = conn_dev; 9410 connp->conn_minor_arena = minor_arena; 9411 9412 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9413 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9414 9415 tcp = connp->conn_tcp; 9416 9417 if (issocket) { 9418 WR(q)->q_qinfo = &tcp_sock_winit; 9419 } else { 9420 #ifdef _ILP32 9421 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9422 #else 9423 tcp->tcp_acceptor_id = conn_dev; 9424 #endif /* _ILP32 */ 9425 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9426 } 9427 9428 /* 9429 * Put the ref for TCP. Ref for IP was already put 9430 * by ipcl_conn_create. Also Make the conn_t globally 9431 * visible to walkers 9432 */ 9433 mutex_enter(&connp->conn_lock); 9434 CONN_INC_REF_LOCKED(connp); 9435 ASSERT(connp->conn_ref == 2); 9436 connp->conn_state_flags &= ~CONN_INCIPIENT; 9437 mutex_exit(&connp->conn_lock); 9438 9439 qprocson(q); 9440 return (0); 9441 } 9442 9443 /* 9444 * Some TCP options can be "set" by requesting them in the option 9445 * buffer. This is needed for XTI feature test though we do not 9446 * allow it in general. We interpret that this mechanism is more 9447 * applicable to OSI protocols and need not be allowed in general. 9448 * This routine filters out options for which it is not allowed (most) 9449 * and lets through those (few) for which it is. [ The XTI interface 9450 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9451 * ever implemented will have to be allowed here ]. 9452 */ 9453 static boolean_t 9454 tcp_allow_connopt_set(int level, int name) 9455 { 9456 9457 switch (level) { 9458 case IPPROTO_TCP: 9459 switch (name) { 9460 case TCP_NODELAY: 9461 return (B_TRUE); 9462 default: 9463 return (B_FALSE); 9464 } 9465 /*NOTREACHED*/ 9466 default: 9467 return (B_FALSE); 9468 } 9469 /*NOTREACHED*/ 9470 } 9471 9472 /* 9473 * this routine gets default values of certain options whose default 9474 * values are maintained by protocol specific code 9475 */ 9476 /* ARGSUSED */ 9477 int 9478 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9479 { 9480 int32_t *i1 = (int32_t *)ptr; 9481 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9482 9483 switch (level) { 9484 case IPPROTO_TCP: 9485 switch (name) { 9486 case TCP_NOTIFY_THRESHOLD: 9487 *i1 = tcps->tcps_ip_notify_interval; 9488 break; 9489 case TCP_ABORT_THRESHOLD: 9490 *i1 = tcps->tcps_ip_abort_interval; 9491 break; 9492 case TCP_CONN_NOTIFY_THRESHOLD: 9493 *i1 = tcps->tcps_ip_notify_cinterval; 9494 break; 9495 case TCP_CONN_ABORT_THRESHOLD: 9496 *i1 = tcps->tcps_ip_abort_cinterval; 9497 break; 9498 default: 9499 return (-1); 9500 } 9501 break; 9502 case IPPROTO_IP: 9503 switch (name) { 9504 case IP_TTL: 9505 *i1 = tcps->tcps_ipv4_ttl; 9506 break; 9507 default: 9508 return (-1); 9509 } 9510 break; 9511 case IPPROTO_IPV6: 9512 switch (name) { 9513 case IPV6_UNICAST_HOPS: 9514 *i1 = tcps->tcps_ipv6_hoplimit; 9515 break; 9516 default: 9517 return (-1); 9518 } 9519 break; 9520 default: 9521 return (-1); 9522 } 9523 return (sizeof (int)); 9524 } 9525 9526 static int 9527 tcp_opt_get(conn_t *connp, int level, int name, uchar_t *ptr) 9528 { 9529 int *i1 = (int *)ptr; 9530 tcp_t *tcp = connp->conn_tcp; 9531 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9532 9533 switch (level) { 9534 case SOL_SOCKET: 9535 switch (name) { 9536 case SO_LINGER: { 9537 struct linger *lgr = (struct linger *)ptr; 9538 9539 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9540 lgr->l_linger = tcp->tcp_lingertime; 9541 } 9542 return (sizeof (struct linger)); 9543 case SO_DEBUG: 9544 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9545 break; 9546 case SO_KEEPALIVE: 9547 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9548 break; 9549 case SO_DONTROUTE: 9550 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9551 break; 9552 case SO_USELOOPBACK: 9553 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9554 break; 9555 case SO_BROADCAST: 9556 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9557 break; 9558 case SO_REUSEADDR: 9559 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9560 break; 9561 case SO_OOBINLINE: 9562 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9563 break; 9564 case SO_DGRAM_ERRIND: 9565 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9566 break; 9567 case SO_TYPE: 9568 *i1 = SOCK_STREAM; 9569 break; 9570 case SO_SNDBUF: 9571 *i1 = tcp->tcp_xmit_hiwater; 9572 break; 9573 case SO_RCVBUF: 9574 *i1 = tcp->tcp_recv_hiwater; 9575 break; 9576 case SO_SND_COPYAVOID: 9577 *i1 = tcp->tcp_snd_zcopy_on ? 9578 SO_SND_COPYAVOID : 0; 9579 break; 9580 case SO_ALLZONES: 9581 *i1 = connp->conn_allzones ? 1 : 0; 9582 break; 9583 case SO_ANON_MLP: 9584 *i1 = connp->conn_anon_mlp; 9585 break; 9586 case SO_MAC_EXEMPT: 9587 *i1 = connp->conn_mac_exempt; 9588 break; 9589 case SO_EXCLBIND: 9590 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9591 break; 9592 case SO_PROTOTYPE: 9593 *i1 = IPPROTO_TCP; 9594 break; 9595 case SO_DOMAIN: 9596 *i1 = tcp->tcp_family; 9597 break; 9598 case SO_ACCEPTCONN: 9599 *i1 = (tcp->tcp_state == TCPS_LISTEN); 9600 default: 9601 return (-1); 9602 } 9603 break; 9604 case IPPROTO_TCP: 9605 switch (name) { 9606 case TCP_NODELAY: 9607 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9608 break; 9609 case TCP_MAXSEG: 9610 *i1 = tcp->tcp_mss; 9611 break; 9612 case TCP_NOTIFY_THRESHOLD: 9613 *i1 = (int)tcp->tcp_first_timer_threshold; 9614 break; 9615 case TCP_ABORT_THRESHOLD: 9616 *i1 = tcp->tcp_second_timer_threshold; 9617 break; 9618 case TCP_CONN_NOTIFY_THRESHOLD: 9619 *i1 = tcp->tcp_first_ctimer_threshold; 9620 break; 9621 case TCP_CONN_ABORT_THRESHOLD: 9622 *i1 = tcp->tcp_second_ctimer_threshold; 9623 break; 9624 case TCP_RECVDSTADDR: 9625 *i1 = tcp->tcp_recvdstaddr; 9626 break; 9627 case TCP_ANONPRIVBIND: 9628 *i1 = tcp->tcp_anon_priv_bind; 9629 break; 9630 case TCP_EXCLBIND: 9631 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9632 break; 9633 case TCP_INIT_CWND: 9634 *i1 = tcp->tcp_init_cwnd; 9635 break; 9636 case TCP_KEEPALIVE_THRESHOLD: 9637 *i1 = tcp->tcp_ka_interval; 9638 break; 9639 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9640 *i1 = tcp->tcp_ka_abort_thres; 9641 break; 9642 case TCP_CORK: 9643 *i1 = tcp->tcp_cork; 9644 break; 9645 default: 9646 return (-1); 9647 } 9648 break; 9649 case IPPROTO_IP: 9650 if (tcp->tcp_family != AF_INET) 9651 return (-1); 9652 switch (name) { 9653 case IP_OPTIONS: 9654 case T_IP_OPTIONS: { 9655 /* 9656 * This is compatible with BSD in that in only return 9657 * the reverse source route with the final destination 9658 * as the last entry. The first 4 bytes of the option 9659 * will contain the final destination. 9660 */ 9661 int opt_len; 9662 9663 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9664 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9665 ASSERT(opt_len >= 0); 9666 /* Caller ensures enough space */ 9667 if (opt_len > 0) { 9668 /* 9669 * TODO: Do we have to handle getsockopt on an 9670 * initiator as well? 9671 */ 9672 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9673 } 9674 return (0); 9675 } 9676 case IP_TOS: 9677 case T_IP_TOS: 9678 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9679 break; 9680 case IP_TTL: 9681 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9682 break; 9683 case IP_NEXTHOP: 9684 /* Handled at IP level */ 9685 return (-EINVAL); 9686 default: 9687 return (-1); 9688 } 9689 break; 9690 case IPPROTO_IPV6: 9691 /* 9692 * IPPROTO_IPV6 options are only supported for sockets 9693 * that are using IPv6 on the wire. 9694 */ 9695 if (tcp->tcp_ipversion != IPV6_VERSION) { 9696 return (-1); 9697 } 9698 switch (name) { 9699 case IPV6_UNICAST_HOPS: 9700 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9701 break; /* goto sizeof (int) option return */ 9702 case IPV6_BOUND_IF: 9703 /* Zero if not set */ 9704 *i1 = tcp->tcp_bound_if; 9705 break; /* goto sizeof (int) option return */ 9706 case IPV6_RECVPKTINFO: 9707 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9708 *i1 = 1; 9709 else 9710 *i1 = 0; 9711 break; /* goto sizeof (int) option return */ 9712 case IPV6_RECVTCLASS: 9713 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9714 *i1 = 1; 9715 else 9716 *i1 = 0; 9717 break; /* goto sizeof (int) option return */ 9718 case IPV6_RECVHOPLIMIT: 9719 if (tcp->tcp_ipv6_recvancillary & 9720 TCP_IPV6_RECVHOPLIMIT) 9721 *i1 = 1; 9722 else 9723 *i1 = 0; 9724 break; /* goto sizeof (int) option return */ 9725 case IPV6_RECVHOPOPTS: 9726 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9727 *i1 = 1; 9728 else 9729 *i1 = 0; 9730 break; /* goto sizeof (int) option return */ 9731 case IPV6_RECVDSTOPTS: 9732 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9733 *i1 = 1; 9734 else 9735 *i1 = 0; 9736 break; /* goto sizeof (int) option return */ 9737 case _OLD_IPV6_RECVDSTOPTS: 9738 if (tcp->tcp_ipv6_recvancillary & 9739 TCP_OLD_IPV6_RECVDSTOPTS) 9740 *i1 = 1; 9741 else 9742 *i1 = 0; 9743 break; /* goto sizeof (int) option return */ 9744 case IPV6_RECVRTHDR: 9745 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9746 *i1 = 1; 9747 else 9748 *i1 = 0; 9749 break; /* goto sizeof (int) option return */ 9750 case IPV6_RECVRTHDRDSTOPTS: 9751 if (tcp->tcp_ipv6_recvancillary & 9752 TCP_IPV6_RECVRTDSTOPTS) 9753 *i1 = 1; 9754 else 9755 *i1 = 0; 9756 break; /* goto sizeof (int) option return */ 9757 case IPV6_PKTINFO: { 9758 /* XXX assumes that caller has room for max size! */ 9759 struct in6_pktinfo *pkti; 9760 9761 pkti = (struct in6_pktinfo *)ptr; 9762 if (ipp->ipp_fields & IPPF_IFINDEX) 9763 pkti->ipi6_ifindex = ipp->ipp_ifindex; 9764 else 9765 pkti->ipi6_ifindex = 0; 9766 if (ipp->ipp_fields & IPPF_ADDR) 9767 pkti->ipi6_addr = ipp->ipp_addr; 9768 else 9769 pkti->ipi6_addr = ipv6_all_zeros; 9770 return (sizeof (struct in6_pktinfo)); 9771 } 9772 case IPV6_TCLASS: 9773 if (ipp->ipp_fields & IPPF_TCLASS) 9774 *i1 = ipp->ipp_tclass; 9775 else 9776 *i1 = IPV6_FLOW_TCLASS( 9777 IPV6_DEFAULT_VERS_AND_FLOW); 9778 break; /* goto sizeof (int) option return */ 9779 case IPV6_NEXTHOP: { 9780 sin6_t *sin6 = (sin6_t *)ptr; 9781 9782 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 9783 return (0); 9784 *sin6 = sin6_null; 9785 sin6->sin6_family = AF_INET6; 9786 sin6->sin6_addr = ipp->ipp_nexthop; 9787 return (sizeof (sin6_t)); 9788 } 9789 case IPV6_HOPOPTS: 9790 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 9791 return (0); 9792 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 9793 return (0); 9794 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 9795 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 9796 if (tcp->tcp_label_len > 0) { 9797 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 9798 ptr[1] = (ipp->ipp_hopoptslen - 9799 tcp->tcp_label_len + 7) / 8 - 1; 9800 } 9801 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 9802 case IPV6_RTHDRDSTOPTS: 9803 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 9804 return (0); 9805 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 9806 return (ipp->ipp_rtdstoptslen); 9807 case IPV6_RTHDR: 9808 if (!(ipp->ipp_fields & IPPF_RTHDR)) 9809 return (0); 9810 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 9811 return (ipp->ipp_rthdrlen); 9812 case IPV6_DSTOPTS: 9813 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 9814 return (0); 9815 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 9816 return (ipp->ipp_dstoptslen); 9817 case IPV6_SRC_PREFERENCES: 9818 return (ip6_get_src_preferences(connp, 9819 (uint32_t *)ptr)); 9820 case IPV6_PATHMTU: { 9821 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 9822 9823 if (tcp->tcp_state < TCPS_ESTABLISHED) 9824 return (-1); 9825 9826 return (ip_fill_mtuinfo(&connp->conn_remv6, 9827 connp->conn_fport, mtuinfo, 9828 connp->conn_netstack)); 9829 } 9830 default: 9831 return (-1); 9832 } 9833 break; 9834 default: 9835 return (-1); 9836 } 9837 return (sizeof (int)); 9838 } 9839 9840 /* 9841 * TCP routine to get the values of options. 9842 */ 9843 int 9844 tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9845 { 9846 return (tcp_opt_get(Q_TO_CONN(q), level, name, ptr)); 9847 } 9848 9849 /* returns UNIX error, the optlen is a value-result arg */ 9850 int 9851 tcp_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 9852 void *optvalp, socklen_t *optlen, cred_t *cr) 9853 { 9854 conn_t *connp = (conn_t *)proto_handle; 9855 squeue_t *sqp = connp->conn_sqp; 9856 int error; 9857 t_uscalar_t max_optbuf_len; 9858 void *optvalp_buf; 9859 int len; 9860 9861 ASSERT(connp->conn_upper_handle != NULL); 9862 9863 error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len, 9864 tcp_opt_obj.odb_opt_des_arr, 9865 tcp_opt_obj.odb_opt_arr_cnt, 9866 tcp_opt_obj.odb_topmost_tpiprovider, 9867 B_FALSE, B_TRUE, cr); 9868 if (error != 0) { 9869 if (error < 0) { 9870 error = proto_tlitosyserr(-error); 9871 } 9872 return (error); 9873 } 9874 9875 optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP); 9876 9877 error = squeue_synch_enter(sqp, connp, NULL); 9878 if (error == ENOMEM) { 9879 return (ENOMEM); 9880 } 9881 9882 len = tcp_opt_get(connp, level, option_name, optvalp_buf); 9883 squeue_synch_exit(sqp, connp); 9884 9885 if (len < 0) { 9886 /* 9887 * Pass on to IP 9888 */ 9889 kmem_free(optvalp_buf, max_optbuf_len); 9890 return (ip_get_options(connp, level, option_name, 9891 optvalp, optlen, cr)); 9892 } else { 9893 /* 9894 * update optlen and copy option value 9895 */ 9896 t_uscalar_t size = MIN(len, *optlen); 9897 bcopy(optvalp_buf, optvalp, size); 9898 bcopy(&size, optlen, sizeof (size)); 9899 9900 kmem_free(optvalp_buf, max_optbuf_len); 9901 return (0); 9902 } 9903 } 9904 9905 /* 9906 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 9907 * Parameters are assumed to be verified by the caller. 9908 */ 9909 /* ARGSUSED */ 9910 int 9911 tcp_opt_set(conn_t *connp, uint_t optset_context, int level, int name, 9912 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9913 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 9914 { 9915 tcp_t *tcp = connp->conn_tcp; 9916 int *i1 = (int *)invalp; 9917 boolean_t onoff = (*i1 == 0) ? 0 : 1; 9918 boolean_t checkonly; 9919 int reterr; 9920 tcp_stack_t *tcps = tcp->tcp_tcps; 9921 9922 switch (optset_context) { 9923 case SETFN_OPTCOM_CHECKONLY: 9924 checkonly = B_TRUE; 9925 /* 9926 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9927 * inlen != 0 implies value supplied and 9928 * we have to "pretend" to set it. 9929 * inlen == 0 implies that there is no 9930 * value part in T_CHECK request and just validation 9931 * done elsewhere should be enough, we just return here. 9932 */ 9933 if (inlen == 0) { 9934 *outlenp = 0; 9935 return (0); 9936 } 9937 break; 9938 case SETFN_OPTCOM_NEGOTIATE: 9939 checkonly = B_FALSE; 9940 break; 9941 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 9942 case SETFN_CONN_NEGOTIATE: 9943 checkonly = B_FALSE; 9944 /* 9945 * Negotiating local and "association-related" options 9946 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 9947 * primitives is allowed by XTI, but we choose 9948 * to not implement this style negotiation for Internet 9949 * protocols (We interpret it is a must for OSI world but 9950 * optional for Internet protocols) for all options. 9951 * [ Will do only for the few options that enable test 9952 * suites that our XTI implementation of this feature 9953 * works for transports that do allow it ] 9954 */ 9955 if (!tcp_allow_connopt_set(level, name)) { 9956 *outlenp = 0; 9957 return (EINVAL); 9958 } 9959 break; 9960 default: 9961 /* 9962 * We should never get here 9963 */ 9964 *outlenp = 0; 9965 return (EINVAL); 9966 } 9967 9968 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9969 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9970 9971 /* 9972 * For TCP, we should have no ancillary data sent down 9973 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 9974 * has to be zero. 9975 */ 9976 ASSERT(thisdg_attrs == NULL); 9977 9978 /* 9979 * For fixed length options, no sanity check 9980 * of passed in length is done. It is assumed *_optcom_req() 9981 * routines do the right thing. 9982 */ 9983 switch (level) { 9984 case SOL_SOCKET: 9985 switch (name) { 9986 case SO_LINGER: { 9987 struct linger *lgr = (struct linger *)invalp; 9988 9989 if (!checkonly) { 9990 if (lgr->l_onoff) { 9991 tcp->tcp_linger = 1; 9992 tcp->tcp_lingertime = lgr->l_linger; 9993 } else { 9994 tcp->tcp_linger = 0; 9995 tcp->tcp_lingertime = 0; 9996 } 9997 /* struct copy */ 9998 *(struct linger *)outvalp = *lgr; 9999 } else { 10000 if (!lgr->l_onoff) { 10001 ((struct linger *) 10002 outvalp)->l_onoff = 0; 10003 ((struct linger *) 10004 outvalp)->l_linger = 0; 10005 } else { 10006 /* struct copy */ 10007 *(struct linger *)outvalp = *lgr; 10008 } 10009 } 10010 *outlenp = sizeof (struct linger); 10011 return (0); 10012 } 10013 case SO_DEBUG: 10014 if (!checkonly) 10015 tcp->tcp_debug = onoff; 10016 break; 10017 case SO_KEEPALIVE: 10018 if (checkonly) { 10019 /* check only case */ 10020 break; 10021 } 10022 10023 if (!onoff) { 10024 if (tcp->tcp_ka_enabled) { 10025 if (tcp->tcp_ka_tid != 0) { 10026 (void) TCP_TIMER_CANCEL(tcp, 10027 tcp->tcp_ka_tid); 10028 tcp->tcp_ka_tid = 0; 10029 } 10030 tcp->tcp_ka_enabled = 0; 10031 } 10032 break; 10033 } 10034 if (!tcp->tcp_ka_enabled) { 10035 /* Crank up the keepalive timer */ 10036 tcp->tcp_ka_last_intrvl = 0; 10037 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10038 tcp_keepalive_killer, 10039 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10040 tcp->tcp_ka_enabled = 1; 10041 } 10042 break; 10043 case SO_DONTROUTE: 10044 /* 10045 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10046 * only of interest to IP. We track them here only so 10047 * that we can report their current value. 10048 */ 10049 if (!checkonly) { 10050 tcp->tcp_dontroute = onoff; 10051 tcp->tcp_connp->conn_dontroute = onoff; 10052 } 10053 break; 10054 case SO_USELOOPBACK: 10055 if (!checkonly) { 10056 tcp->tcp_useloopback = onoff; 10057 tcp->tcp_connp->conn_loopback = onoff; 10058 } 10059 break; 10060 case SO_BROADCAST: 10061 if (!checkonly) { 10062 tcp->tcp_broadcast = onoff; 10063 tcp->tcp_connp->conn_broadcast = onoff; 10064 } 10065 break; 10066 case SO_REUSEADDR: 10067 if (!checkonly) { 10068 tcp->tcp_reuseaddr = onoff; 10069 tcp->tcp_connp->conn_reuseaddr = onoff; 10070 } 10071 break; 10072 case SO_OOBINLINE: 10073 if (!checkonly) { 10074 tcp->tcp_oobinline = onoff; 10075 if (IPCL_IS_NONSTR(tcp->tcp_connp)) 10076 proto_set_rx_oob_opt(connp, onoff); 10077 } 10078 break; 10079 case SO_DGRAM_ERRIND: 10080 if (!checkonly) 10081 tcp->tcp_dgram_errind = onoff; 10082 break; 10083 case SO_SNDBUF: { 10084 if (*i1 > tcps->tcps_max_buf) { 10085 *outlenp = 0; 10086 return (ENOBUFS); 10087 } 10088 if (checkonly) 10089 break; 10090 10091 tcp->tcp_xmit_hiwater = *i1; 10092 if (tcps->tcps_snd_lowat_fraction != 0) 10093 tcp->tcp_xmit_lowater = 10094 tcp->tcp_xmit_hiwater / 10095 tcps->tcps_snd_lowat_fraction; 10096 (void) tcp_maxpsz_set(tcp, B_TRUE); 10097 /* 10098 * If we are flow-controlled, recheck the condition. 10099 * There are apps that increase SO_SNDBUF size when 10100 * flow-controlled (EWOULDBLOCK), and expect the flow 10101 * control condition to be lifted right away. 10102 */ 10103 mutex_enter(&tcp->tcp_non_sq_lock); 10104 if (tcp->tcp_flow_stopped && 10105 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10106 tcp_clrqfull(tcp); 10107 } 10108 mutex_exit(&tcp->tcp_non_sq_lock); 10109 break; 10110 } 10111 case SO_RCVBUF: 10112 if (*i1 > tcps->tcps_max_buf) { 10113 *outlenp = 0; 10114 return (ENOBUFS); 10115 } 10116 /* Silently ignore zero */ 10117 if (!checkonly && *i1 != 0) { 10118 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10119 (void) tcp_rwnd_set(tcp, *i1); 10120 } 10121 /* 10122 * XXX should we return the rwnd here 10123 * and tcp_opt_get ? 10124 */ 10125 break; 10126 case SO_SND_COPYAVOID: 10127 if (!checkonly) { 10128 /* we only allow enable at most once for now */ 10129 if (tcp->tcp_loopback || 10130 (tcp->tcp_kssl_ctx != NULL) || 10131 (!tcp->tcp_snd_zcopy_aware && 10132 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10133 *outlenp = 0; 10134 return (EOPNOTSUPP); 10135 } 10136 tcp->tcp_snd_zcopy_aware = 1; 10137 } 10138 break; 10139 case SO_RCVTIMEO: 10140 case SO_SNDTIMEO: 10141 /* 10142 * Pass these two options in order for third part 10143 * protocol usage. Here just return directly. 10144 */ 10145 return (0); 10146 case SO_ALLZONES: 10147 /* Pass option along to IP level for handling */ 10148 return (-EINVAL); 10149 case SO_ANON_MLP: 10150 /* Pass option along to IP level for handling */ 10151 return (-EINVAL); 10152 case SO_MAC_EXEMPT: 10153 /* Pass option along to IP level for handling */ 10154 return (-EINVAL); 10155 case SO_EXCLBIND: 10156 if (!checkonly) 10157 tcp->tcp_exclbind = onoff; 10158 break; 10159 default: 10160 *outlenp = 0; 10161 return (EINVAL); 10162 } 10163 break; 10164 case IPPROTO_TCP: 10165 switch (name) { 10166 case TCP_NODELAY: 10167 if (!checkonly) 10168 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10169 break; 10170 case TCP_NOTIFY_THRESHOLD: 10171 if (!checkonly) 10172 tcp->tcp_first_timer_threshold = *i1; 10173 break; 10174 case TCP_ABORT_THRESHOLD: 10175 if (!checkonly) 10176 tcp->tcp_second_timer_threshold = *i1; 10177 break; 10178 case TCP_CONN_NOTIFY_THRESHOLD: 10179 if (!checkonly) 10180 tcp->tcp_first_ctimer_threshold = *i1; 10181 break; 10182 case TCP_CONN_ABORT_THRESHOLD: 10183 if (!checkonly) 10184 tcp->tcp_second_ctimer_threshold = *i1; 10185 break; 10186 case TCP_RECVDSTADDR: 10187 if (tcp->tcp_state > TCPS_LISTEN) 10188 return (EOPNOTSUPP); 10189 if (!checkonly) 10190 tcp->tcp_recvdstaddr = onoff; 10191 break; 10192 case TCP_ANONPRIVBIND: 10193 if ((reterr = secpolicy_net_privaddr(cr, 0, 10194 IPPROTO_TCP)) != 0) { 10195 *outlenp = 0; 10196 return (reterr); 10197 } 10198 if (!checkonly) { 10199 tcp->tcp_anon_priv_bind = onoff; 10200 } 10201 break; 10202 case TCP_EXCLBIND: 10203 if (!checkonly) 10204 tcp->tcp_exclbind = onoff; 10205 break; /* goto sizeof (int) option return */ 10206 case TCP_INIT_CWND: { 10207 uint32_t init_cwnd = *((uint32_t *)invalp); 10208 10209 if (checkonly) 10210 break; 10211 10212 /* 10213 * Only allow socket with network configuration 10214 * privilege to set the initial cwnd to be larger 10215 * than allowed by RFC 3390. 10216 */ 10217 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10218 tcp->tcp_init_cwnd = init_cwnd; 10219 break; 10220 } 10221 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10222 *outlenp = 0; 10223 return (reterr); 10224 } 10225 if (init_cwnd > TCP_MAX_INIT_CWND) { 10226 *outlenp = 0; 10227 return (EINVAL); 10228 } 10229 tcp->tcp_init_cwnd = init_cwnd; 10230 break; 10231 } 10232 case TCP_KEEPALIVE_THRESHOLD: 10233 if (checkonly) 10234 break; 10235 10236 if (*i1 < tcps->tcps_keepalive_interval_low || 10237 *i1 > tcps->tcps_keepalive_interval_high) { 10238 *outlenp = 0; 10239 return (EINVAL); 10240 } 10241 if (*i1 != tcp->tcp_ka_interval) { 10242 tcp->tcp_ka_interval = *i1; 10243 /* 10244 * Check if we need to restart the 10245 * keepalive timer. 10246 */ 10247 if (tcp->tcp_ka_tid != 0) { 10248 ASSERT(tcp->tcp_ka_enabled); 10249 (void) TCP_TIMER_CANCEL(tcp, 10250 tcp->tcp_ka_tid); 10251 tcp->tcp_ka_last_intrvl = 0; 10252 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10253 tcp_keepalive_killer, 10254 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10255 } 10256 } 10257 break; 10258 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10259 if (!checkonly) { 10260 if (*i1 < 10261 tcps->tcps_keepalive_abort_interval_low || 10262 *i1 > 10263 tcps->tcps_keepalive_abort_interval_high) { 10264 *outlenp = 0; 10265 return (EINVAL); 10266 } 10267 tcp->tcp_ka_abort_thres = *i1; 10268 } 10269 break; 10270 case TCP_CORK: 10271 if (!checkonly) { 10272 /* 10273 * if tcp->tcp_cork was set and is now 10274 * being unset, we have to make sure that 10275 * the remaining data gets sent out. Also 10276 * unset tcp->tcp_cork so that tcp_wput_data() 10277 * can send data even if it is less than mss 10278 */ 10279 if (tcp->tcp_cork && onoff == 0 && 10280 tcp->tcp_unsent > 0) { 10281 tcp->tcp_cork = B_FALSE; 10282 tcp_wput_data(tcp, NULL, B_FALSE); 10283 } 10284 tcp->tcp_cork = onoff; 10285 } 10286 break; 10287 default: 10288 *outlenp = 0; 10289 return (EINVAL); 10290 } 10291 break; 10292 case IPPROTO_IP: 10293 if (tcp->tcp_family != AF_INET) { 10294 *outlenp = 0; 10295 return (ENOPROTOOPT); 10296 } 10297 switch (name) { 10298 case IP_OPTIONS: 10299 case T_IP_OPTIONS: 10300 reterr = tcp_opt_set_header(tcp, checkonly, 10301 invalp, inlen); 10302 if (reterr) { 10303 *outlenp = 0; 10304 return (reterr); 10305 } 10306 /* OK return - copy input buffer into output buffer */ 10307 if (invalp != outvalp) { 10308 /* don't trust bcopy for identical src/dst */ 10309 bcopy(invalp, outvalp, inlen); 10310 } 10311 *outlenp = inlen; 10312 return (0); 10313 case IP_TOS: 10314 case T_IP_TOS: 10315 if (!checkonly) { 10316 tcp->tcp_ipha->ipha_type_of_service = 10317 (uchar_t)*i1; 10318 tcp->tcp_tos = (uchar_t)*i1; 10319 } 10320 break; 10321 case IP_TTL: 10322 if (!checkonly) { 10323 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10324 tcp->tcp_ttl = (uchar_t)*i1; 10325 } 10326 break; 10327 case IP_BOUND_IF: 10328 case IP_NEXTHOP: 10329 /* Handled at the IP level */ 10330 return (-EINVAL); 10331 case IP_SEC_OPT: 10332 /* 10333 * We should not allow policy setting after 10334 * we start listening for connections. 10335 */ 10336 if (tcp->tcp_state == TCPS_LISTEN) { 10337 return (EINVAL); 10338 } else { 10339 /* Handled at the IP level */ 10340 return (-EINVAL); 10341 } 10342 default: 10343 *outlenp = 0; 10344 return (EINVAL); 10345 } 10346 break; 10347 case IPPROTO_IPV6: { 10348 ip6_pkt_t *ipp; 10349 10350 /* 10351 * IPPROTO_IPV6 options are only supported for sockets 10352 * that are using IPv6 on the wire. 10353 */ 10354 if (tcp->tcp_ipversion != IPV6_VERSION) { 10355 *outlenp = 0; 10356 return (ENOPROTOOPT); 10357 } 10358 /* 10359 * Only sticky options; no ancillary data 10360 */ 10361 ipp = &tcp->tcp_sticky_ipp; 10362 10363 switch (name) { 10364 case IPV6_UNICAST_HOPS: 10365 /* -1 means use default */ 10366 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10367 *outlenp = 0; 10368 return (EINVAL); 10369 } 10370 if (!checkonly) { 10371 if (*i1 == -1) { 10372 tcp->tcp_ip6h->ip6_hops = 10373 ipp->ipp_unicast_hops = 10374 (uint8_t)tcps->tcps_ipv6_hoplimit; 10375 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10376 /* Pass modified value to IP. */ 10377 *i1 = tcp->tcp_ip6h->ip6_hops; 10378 } else { 10379 tcp->tcp_ip6h->ip6_hops = 10380 ipp->ipp_unicast_hops = 10381 (uint8_t)*i1; 10382 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10383 } 10384 reterr = tcp_build_hdrs(tcp); 10385 if (reterr != 0) 10386 return (reterr); 10387 } 10388 break; 10389 case IPV6_BOUND_IF: 10390 if (!checkonly) { 10391 tcp->tcp_bound_if = *i1; 10392 PASS_OPT_TO_IP(connp); 10393 } 10394 break; 10395 /* 10396 * Set boolean switches for ancillary data delivery 10397 */ 10398 case IPV6_RECVPKTINFO: 10399 if (!checkonly) { 10400 if (onoff) 10401 tcp->tcp_ipv6_recvancillary |= 10402 TCP_IPV6_RECVPKTINFO; 10403 else 10404 tcp->tcp_ipv6_recvancillary &= 10405 ~TCP_IPV6_RECVPKTINFO; 10406 /* Force it to be sent up with the next msg */ 10407 tcp->tcp_recvifindex = 0; 10408 PASS_OPT_TO_IP(connp); 10409 } 10410 break; 10411 case IPV6_RECVTCLASS: 10412 if (!checkonly) { 10413 if (onoff) 10414 tcp->tcp_ipv6_recvancillary |= 10415 TCP_IPV6_RECVTCLASS; 10416 else 10417 tcp->tcp_ipv6_recvancillary &= 10418 ~TCP_IPV6_RECVTCLASS; 10419 PASS_OPT_TO_IP(connp); 10420 } 10421 break; 10422 case IPV6_RECVHOPLIMIT: 10423 if (!checkonly) { 10424 if (onoff) 10425 tcp->tcp_ipv6_recvancillary |= 10426 TCP_IPV6_RECVHOPLIMIT; 10427 else 10428 tcp->tcp_ipv6_recvancillary &= 10429 ~TCP_IPV6_RECVHOPLIMIT; 10430 /* Force it to be sent up with the next msg */ 10431 tcp->tcp_recvhops = 0xffffffffU; 10432 PASS_OPT_TO_IP(connp); 10433 } 10434 break; 10435 case IPV6_RECVHOPOPTS: 10436 if (!checkonly) { 10437 if (onoff) 10438 tcp->tcp_ipv6_recvancillary |= 10439 TCP_IPV6_RECVHOPOPTS; 10440 else 10441 tcp->tcp_ipv6_recvancillary &= 10442 ~TCP_IPV6_RECVHOPOPTS; 10443 PASS_OPT_TO_IP(connp); 10444 } 10445 break; 10446 case IPV6_RECVDSTOPTS: 10447 if (!checkonly) { 10448 if (onoff) 10449 tcp->tcp_ipv6_recvancillary |= 10450 TCP_IPV6_RECVDSTOPTS; 10451 else 10452 tcp->tcp_ipv6_recvancillary &= 10453 ~TCP_IPV6_RECVDSTOPTS; 10454 PASS_OPT_TO_IP(connp); 10455 } 10456 break; 10457 case _OLD_IPV6_RECVDSTOPTS: 10458 if (!checkonly) { 10459 if (onoff) 10460 tcp->tcp_ipv6_recvancillary |= 10461 TCP_OLD_IPV6_RECVDSTOPTS; 10462 else 10463 tcp->tcp_ipv6_recvancillary &= 10464 ~TCP_OLD_IPV6_RECVDSTOPTS; 10465 } 10466 break; 10467 case IPV6_RECVRTHDR: 10468 if (!checkonly) { 10469 if (onoff) 10470 tcp->tcp_ipv6_recvancillary |= 10471 TCP_IPV6_RECVRTHDR; 10472 else 10473 tcp->tcp_ipv6_recvancillary &= 10474 ~TCP_IPV6_RECVRTHDR; 10475 PASS_OPT_TO_IP(connp); 10476 } 10477 break; 10478 case IPV6_RECVRTHDRDSTOPTS: 10479 if (!checkonly) { 10480 if (onoff) 10481 tcp->tcp_ipv6_recvancillary |= 10482 TCP_IPV6_RECVRTDSTOPTS; 10483 else 10484 tcp->tcp_ipv6_recvancillary &= 10485 ~TCP_IPV6_RECVRTDSTOPTS; 10486 PASS_OPT_TO_IP(connp); 10487 } 10488 break; 10489 case IPV6_PKTINFO: 10490 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10491 return (EINVAL); 10492 if (checkonly) 10493 break; 10494 10495 if (inlen == 0) { 10496 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10497 } else { 10498 struct in6_pktinfo *pkti; 10499 10500 pkti = (struct in6_pktinfo *)invalp; 10501 /* 10502 * RFC 3542 states that ipi6_addr must be 10503 * the unspecified address when setting the 10504 * IPV6_PKTINFO sticky socket option on a 10505 * TCP socket. 10506 */ 10507 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10508 return (EINVAL); 10509 /* 10510 * IP will validate the source address and 10511 * interface index. 10512 */ 10513 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 10514 reterr = ip_set_options(tcp->tcp_connp, 10515 level, name, invalp, inlen, cr); 10516 } else { 10517 reterr = ip6_set_pktinfo(cr, 10518 tcp->tcp_connp, pkti); 10519 } 10520 if (reterr != 0) 10521 return (reterr); 10522 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10523 ipp->ipp_addr = pkti->ipi6_addr; 10524 if (ipp->ipp_ifindex != 0) 10525 ipp->ipp_fields |= IPPF_IFINDEX; 10526 else 10527 ipp->ipp_fields &= ~IPPF_IFINDEX; 10528 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10529 ipp->ipp_fields |= IPPF_ADDR; 10530 else 10531 ipp->ipp_fields &= ~IPPF_ADDR; 10532 } 10533 reterr = tcp_build_hdrs(tcp); 10534 if (reterr != 0) 10535 return (reterr); 10536 break; 10537 case IPV6_TCLASS: 10538 if (inlen != 0 && inlen != sizeof (int)) 10539 return (EINVAL); 10540 if (checkonly) 10541 break; 10542 10543 if (inlen == 0) { 10544 ipp->ipp_fields &= ~IPPF_TCLASS; 10545 } else { 10546 if (*i1 > 255 || *i1 < -1) 10547 return (EINVAL); 10548 if (*i1 == -1) { 10549 ipp->ipp_tclass = 0; 10550 *i1 = 0; 10551 } else { 10552 ipp->ipp_tclass = *i1; 10553 } 10554 ipp->ipp_fields |= IPPF_TCLASS; 10555 } 10556 reterr = tcp_build_hdrs(tcp); 10557 if (reterr != 0) 10558 return (reterr); 10559 break; 10560 case IPV6_NEXTHOP: 10561 /* 10562 * IP will verify that the nexthop is reachable 10563 * and fail for sticky options. 10564 */ 10565 if (inlen != 0 && inlen != sizeof (sin6_t)) 10566 return (EINVAL); 10567 if (checkonly) 10568 break; 10569 10570 if (inlen == 0) { 10571 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10572 } else { 10573 sin6_t *sin6 = (sin6_t *)invalp; 10574 10575 if (sin6->sin6_family != AF_INET6) 10576 return (EAFNOSUPPORT); 10577 if (IN6_IS_ADDR_V4MAPPED( 10578 &sin6->sin6_addr)) 10579 return (EADDRNOTAVAIL); 10580 ipp->ipp_nexthop = sin6->sin6_addr; 10581 if (!IN6_IS_ADDR_UNSPECIFIED( 10582 &ipp->ipp_nexthop)) 10583 ipp->ipp_fields |= IPPF_NEXTHOP; 10584 else 10585 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10586 } 10587 reterr = tcp_build_hdrs(tcp); 10588 if (reterr != 0) 10589 return (reterr); 10590 PASS_OPT_TO_IP(connp); 10591 break; 10592 case IPV6_HOPOPTS: { 10593 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10594 10595 /* 10596 * Sanity checks - minimum size, size a multiple of 10597 * eight bytes, and matching size passed in. 10598 */ 10599 if (inlen != 0 && 10600 inlen != (8 * (hopts->ip6h_len + 1))) 10601 return (EINVAL); 10602 10603 if (checkonly) 10604 break; 10605 10606 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10607 (uchar_t **)&ipp->ipp_hopopts, 10608 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10609 if (reterr != 0) 10610 return (reterr); 10611 if (ipp->ipp_hopoptslen == 0) 10612 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10613 else 10614 ipp->ipp_fields |= IPPF_HOPOPTS; 10615 reterr = tcp_build_hdrs(tcp); 10616 if (reterr != 0) 10617 return (reterr); 10618 break; 10619 } 10620 case IPV6_RTHDRDSTOPTS: { 10621 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10622 10623 /* 10624 * Sanity checks - minimum size, size a multiple of 10625 * eight bytes, and matching size passed in. 10626 */ 10627 if (inlen != 0 && 10628 inlen != (8 * (dopts->ip6d_len + 1))) 10629 return (EINVAL); 10630 10631 if (checkonly) 10632 break; 10633 10634 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10635 (uchar_t **)&ipp->ipp_rtdstopts, 10636 &ipp->ipp_rtdstoptslen, 0); 10637 if (reterr != 0) 10638 return (reterr); 10639 if (ipp->ipp_rtdstoptslen == 0) 10640 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10641 else 10642 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10643 reterr = tcp_build_hdrs(tcp); 10644 if (reterr != 0) 10645 return (reterr); 10646 break; 10647 } 10648 case IPV6_DSTOPTS: { 10649 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10650 10651 /* 10652 * Sanity checks - minimum size, size a multiple of 10653 * eight bytes, and matching size passed in. 10654 */ 10655 if (inlen != 0 && 10656 inlen != (8 * (dopts->ip6d_len + 1))) 10657 return (EINVAL); 10658 10659 if (checkonly) 10660 break; 10661 10662 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10663 (uchar_t **)&ipp->ipp_dstopts, 10664 &ipp->ipp_dstoptslen, 0); 10665 if (reterr != 0) 10666 return (reterr); 10667 if (ipp->ipp_dstoptslen == 0) 10668 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10669 else 10670 ipp->ipp_fields |= IPPF_DSTOPTS; 10671 reterr = tcp_build_hdrs(tcp); 10672 if (reterr != 0) 10673 return (reterr); 10674 break; 10675 } 10676 case IPV6_RTHDR: { 10677 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10678 10679 /* 10680 * Sanity checks - minimum size, size a multiple of 10681 * eight bytes, and matching size passed in. 10682 */ 10683 if (inlen != 0 && 10684 inlen != (8 * (rt->ip6r_len + 1))) 10685 return (EINVAL); 10686 10687 if (checkonly) 10688 break; 10689 10690 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10691 (uchar_t **)&ipp->ipp_rthdr, 10692 &ipp->ipp_rthdrlen, 0); 10693 if (reterr != 0) 10694 return (reterr); 10695 if (ipp->ipp_rthdrlen == 0) 10696 ipp->ipp_fields &= ~IPPF_RTHDR; 10697 else 10698 ipp->ipp_fields |= IPPF_RTHDR; 10699 reterr = tcp_build_hdrs(tcp); 10700 if (reterr != 0) 10701 return (reterr); 10702 break; 10703 } 10704 case IPV6_V6ONLY: 10705 if (!checkonly) { 10706 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10707 } 10708 break; 10709 case IPV6_USE_MIN_MTU: 10710 if (inlen != sizeof (int)) 10711 return (EINVAL); 10712 10713 if (*i1 < -1 || *i1 > 1) 10714 return (EINVAL); 10715 10716 if (checkonly) 10717 break; 10718 10719 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10720 ipp->ipp_use_min_mtu = *i1; 10721 break; 10722 case IPV6_SEC_OPT: 10723 /* 10724 * We should not allow policy setting after 10725 * we start listening for connections. 10726 */ 10727 if (tcp->tcp_state == TCPS_LISTEN) { 10728 return (EINVAL); 10729 } else { 10730 /* Handled at the IP level */ 10731 return (-EINVAL); 10732 } 10733 case IPV6_SRC_PREFERENCES: 10734 if (inlen != sizeof (uint32_t)) 10735 return (EINVAL); 10736 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10737 *(uint32_t *)invalp); 10738 if (reterr != 0) { 10739 *outlenp = 0; 10740 return (reterr); 10741 } 10742 break; 10743 default: 10744 *outlenp = 0; 10745 return (EINVAL); 10746 } 10747 break; 10748 } /* end IPPROTO_IPV6 */ 10749 default: 10750 *outlenp = 0; 10751 return (EINVAL); 10752 } 10753 /* 10754 * Common case of OK return with outval same as inval 10755 */ 10756 if (invalp != outvalp) { 10757 /* don't trust bcopy for identical src/dst */ 10758 (void) bcopy(invalp, outvalp, inlen); 10759 } 10760 *outlenp = inlen; 10761 return (0); 10762 } 10763 10764 /* ARGSUSED */ 10765 int 10766 tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10767 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10768 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10769 { 10770 conn_t *connp = Q_TO_CONN(q); 10771 10772 return (tcp_opt_set(connp, optset_context, level, name, inlen, invalp, 10773 outlenp, outvalp, thisdg_attrs, cr, mblk)); 10774 } 10775 10776 int 10777 tcp_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 10778 const void *optvalp, socklen_t optlen, cred_t *cr) 10779 { 10780 conn_t *connp = (conn_t *)proto_handle; 10781 squeue_t *sqp = connp->conn_sqp; 10782 int error; 10783 10784 ASSERT(connp->conn_upper_handle != NULL); 10785 /* 10786 * Entering the squeue synchronously can result in a context switch, 10787 * which can cause a rather sever performance degradation. So we try to 10788 * handle whatever options we can without entering the squeue. 10789 */ 10790 if (level == IPPROTO_TCP) { 10791 switch (option_name) { 10792 case TCP_NODELAY: 10793 if (optlen != sizeof (int32_t)) 10794 return (EINVAL); 10795 mutex_enter(&connp->conn_tcp->tcp_non_sq_lock); 10796 connp->conn_tcp->tcp_naglim = *(int *)optvalp ? 1 : 10797 connp->conn_tcp->tcp_mss; 10798 mutex_exit(&connp->conn_tcp->tcp_non_sq_lock); 10799 return (0); 10800 default: 10801 break; 10802 } 10803 } 10804 10805 error = squeue_synch_enter(sqp, connp, NULL); 10806 if (error == ENOMEM) { 10807 return (ENOMEM); 10808 } 10809 10810 error = proto_opt_check(level, option_name, optlen, NULL, 10811 tcp_opt_obj.odb_opt_des_arr, 10812 tcp_opt_obj.odb_opt_arr_cnt, 10813 tcp_opt_obj.odb_topmost_tpiprovider, 10814 B_TRUE, B_FALSE, cr); 10815 10816 if (error != 0) { 10817 if (error < 0) { 10818 error = proto_tlitosyserr(-error); 10819 } 10820 squeue_synch_exit(sqp, connp); 10821 return (error); 10822 } 10823 10824 error = tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name, 10825 optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp, 10826 NULL, cr, NULL); 10827 squeue_synch_exit(sqp, connp); 10828 10829 if (error < 0) { 10830 /* 10831 * Pass on to ip 10832 */ 10833 error = ip_set_options(connp, level, option_name, optvalp, 10834 optlen, cr); 10835 } 10836 return (error); 10837 } 10838 10839 /* 10840 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10841 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10842 * headers, and the maximum size tcp header (to avoid reallocation 10843 * on the fly for additional tcp options). 10844 * Returns failure if can't allocate memory. 10845 */ 10846 static int 10847 tcp_build_hdrs(tcp_t *tcp) 10848 { 10849 char *hdrs; 10850 uint_t hdrs_len; 10851 ip6i_t *ip6i; 10852 char buf[TCP_MAX_HDR_LENGTH]; 10853 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10854 in6_addr_t src, dst; 10855 tcp_stack_t *tcps = tcp->tcp_tcps; 10856 conn_t *connp = tcp->tcp_connp; 10857 10858 /* 10859 * save the existing tcp header and source/dest IP addresses 10860 */ 10861 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10862 src = tcp->tcp_ip6h->ip6_src; 10863 dst = tcp->tcp_ip6h->ip6_dst; 10864 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10865 ASSERT(hdrs_len != 0); 10866 if (hdrs_len > tcp->tcp_iphc_len) { 10867 /* Need to reallocate */ 10868 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10869 if (hdrs == NULL) 10870 return (ENOMEM); 10871 if (tcp->tcp_iphc != NULL) { 10872 if (tcp->tcp_hdr_grown) { 10873 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10874 } else { 10875 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10876 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10877 } 10878 tcp->tcp_iphc_len = 0; 10879 } 10880 ASSERT(tcp->tcp_iphc_len == 0); 10881 tcp->tcp_iphc = hdrs; 10882 tcp->tcp_iphc_len = hdrs_len; 10883 tcp->tcp_hdr_grown = B_TRUE; 10884 } 10885 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10886 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10887 10888 /* Set header fields not in ipp */ 10889 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10890 ip6i = (ip6i_t *)tcp->tcp_iphc; 10891 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 10892 } else { 10893 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 10894 } 10895 /* 10896 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 10897 * 10898 * tcp->tcp_tcp_hdr_len doesn't change here. 10899 */ 10900 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 10901 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 10902 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 10903 10904 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 10905 10906 tcp->tcp_ip6h->ip6_src = src; 10907 tcp->tcp_ip6h->ip6_dst = dst; 10908 10909 /* 10910 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 10911 * the default value for TCP. 10912 */ 10913 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 10914 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 10915 10916 /* 10917 * If we're setting extension headers after a connection 10918 * has been established, and if we have a routing header 10919 * among the extension headers, call ip_massage_options_v6 to 10920 * manipulate the routing header/ip6_dst set the checksum 10921 * difference in the tcp header template. 10922 * (This happens in tcp_connect_ipv6 if the routing header 10923 * is set prior to the connect.) 10924 * Set the tcp_sum to zero first in case we've cleared a 10925 * routing header or don't have one at all. 10926 */ 10927 tcp->tcp_sum = 0; 10928 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 10929 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 10930 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 10931 (uint8_t *)tcp->tcp_tcph); 10932 if (rth != NULL) { 10933 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 10934 rth, tcps->tcps_netstack); 10935 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 10936 (tcp->tcp_sum >> 16)); 10937 } 10938 } 10939 10940 /* Try to get everything in a single mblk */ 10941 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 10942 hdrs_len + tcps->tcps_wroff_xtra); 10943 return (0); 10944 } 10945 10946 /* 10947 * Transfer any source route option from ipha to buf/dst in reversed form. 10948 */ 10949 static int 10950 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 10951 { 10952 ipoptp_t opts; 10953 uchar_t *opt; 10954 uint8_t optval; 10955 uint8_t optlen; 10956 uint32_t len = 0; 10957 10958 for (optval = ipoptp_first(&opts, ipha); 10959 optval != IPOPT_EOL; 10960 optval = ipoptp_next(&opts)) { 10961 opt = opts.ipoptp_cur; 10962 optlen = opts.ipoptp_len; 10963 switch (optval) { 10964 int off1, off2; 10965 case IPOPT_SSRR: 10966 case IPOPT_LSRR: 10967 10968 /* Reverse source route */ 10969 /* 10970 * First entry should be the next to last one in the 10971 * current source route (the last entry is our 10972 * address.) 10973 * The last entry should be the final destination. 10974 */ 10975 buf[IPOPT_OPTVAL] = (uint8_t)optval; 10976 buf[IPOPT_OLEN] = (uint8_t)optlen; 10977 off1 = IPOPT_MINOFF_SR - 1; 10978 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 10979 if (off2 < 0) { 10980 /* No entries in source route */ 10981 break; 10982 } 10983 bcopy(opt + off2, dst, IP_ADDR_LEN); 10984 /* 10985 * Note: use src since ipha has not had its src 10986 * and dst reversed (it is in the state it was 10987 * received. 10988 */ 10989 bcopy(&ipha->ipha_src, buf + off2, 10990 IP_ADDR_LEN); 10991 off2 -= IP_ADDR_LEN; 10992 10993 while (off2 > 0) { 10994 bcopy(opt + off2, buf + off1, 10995 IP_ADDR_LEN); 10996 off1 += IP_ADDR_LEN; 10997 off2 -= IP_ADDR_LEN; 10998 } 10999 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11000 buf += optlen; 11001 len += optlen; 11002 break; 11003 } 11004 } 11005 done: 11006 /* Pad the resulting options */ 11007 while (len & 0x3) { 11008 *buf++ = IPOPT_EOL; 11009 len++; 11010 } 11011 return (len); 11012 } 11013 11014 11015 /* 11016 * Extract and revert a source route from ipha (if any) 11017 * and then update the relevant fields in both tcp_t and the standard header. 11018 */ 11019 static void 11020 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11021 { 11022 char buf[TCP_MAX_HDR_LENGTH]; 11023 uint_t tcph_len; 11024 int len; 11025 11026 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11027 len = IPH_HDR_LENGTH(ipha); 11028 if (len == IP_SIMPLE_HDR_LENGTH) 11029 /* Nothing to do */ 11030 return; 11031 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11032 (len & 0x3)) 11033 return; 11034 11035 tcph_len = tcp->tcp_tcp_hdr_len; 11036 bcopy(tcp->tcp_tcph, buf, tcph_len); 11037 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11038 (tcp->tcp_ipha->ipha_dst & 0xffff); 11039 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11040 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11041 len += IP_SIMPLE_HDR_LENGTH; 11042 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11043 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11044 if ((int)tcp->tcp_sum < 0) 11045 tcp->tcp_sum--; 11046 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11047 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11048 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11049 bcopy(buf, tcp->tcp_tcph, tcph_len); 11050 tcp->tcp_ip_hdr_len = len; 11051 tcp->tcp_ipha->ipha_version_and_hdr_length = 11052 (IP_VERSION << 4) | (len >> 2); 11053 len += tcph_len; 11054 tcp->tcp_hdr_len = len; 11055 } 11056 11057 /* 11058 * Copy the standard header into its new location, 11059 * lay in the new options and then update the relevant 11060 * fields in both tcp_t and the standard header. 11061 */ 11062 static int 11063 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11064 { 11065 uint_t tcph_len; 11066 uint8_t *ip_optp; 11067 tcph_t *new_tcph; 11068 tcp_stack_t *tcps = tcp->tcp_tcps; 11069 conn_t *connp = tcp->tcp_connp; 11070 11071 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11072 return (EINVAL); 11073 11074 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11075 return (EINVAL); 11076 11077 if (checkonly) { 11078 /* 11079 * do not really set, just pretend to - T_CHECK 11080 */ 11081 return (0); 11082 } 11083 11084 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11085 if (tcp->tcp_label_len > 0) { 11086 int padlen; 11087 uint8_t opt; 11088 11089 /* convert list termination to no-ops */ 11090 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11091 ip_optp += ip_optp[IPOPT_OLEN]; 11092 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11093 while (--padlen >= 0) 11094 *ip_optp++ = opt; 11095 } 11096 tcph_len = tcp->tcp_tcp_hdr_len; 11097 new_tcph = (tcph_t *)(ip_optp + len); 11098 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11099 tcp->tcp_tcph = new_tcph; 11100 bcopy(ptr, ip_optp, len); 11101 11102 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11103 11104 tcp->tcp_ip_hdr_len = len; 11105 tcp->tcp_ipha->ipha_version_and_hdr_length = 11106 (IP_VERSION << 4) | (len >> 2); 11107 tcp->tcp_hdr_len = len + tcph_len; 11108 if (!TCP_IS_DETACHED(tcp)) { 11109 /* Always allocate room for all options. */ 11110 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 11111 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11112 } 11113 return (0); 11114 } 11115 11116 /* Get callback routine passed to nd_load by tcp_param_register */ 11117 /* ARGSUSED */ 11118 static int 11119 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11120 { 11121 tcpparam_t *tcppa = (tcpparam_t *)cp; 11122 11123 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11124 return (0); 11125 } 11126 11127 /* 11128 * Walk through the param array specified registering each element with the 11129 * named dispatch handler. 11130 */ 11131 static boolean_t 11132 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11133 { 11134 for (; cnt-- > 0; tcppa++) { 11135 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11136 if (!nd_load(ndp, tcppa->tcp_param_name, 11137 tcp_param_get, tcp_param_set, 11138 (caddr_t)tcppa)) { 11139 nd_free(ndp); 11140 return (B_FALSE); 11141 } 11142 } 11143 } 11144 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11145 KM_SLEEP); 11146 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11147 sizeof (tcpparam_t)); 11148 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11149 tcp_param_get, tcp_param_set_aligned, 11150 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11151 nd_free(ndp); 11152 return (B_FALSE); 11153 } 11154 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11155 KM_SLEEP); 11156 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11157 sizeof (tcpparam_t)); 11158 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11159 tcp_param_get, tcp_param_set_aligned, 11160 (caddr_t)tcps->tcps_mdt_head_param)) { 11161 nd_free(ndp); 11162 return (B_FALSE); 11163 } 11164 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11165 KM_SLEEP); 11166 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11167 sizeof (tcpparam_t)); 11168 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11169 tcp_param_get, tcp_param_set_aligned, 11170 (caddr_t)tcps->tcps_mdt_tail_param)) { 11171 nd_free(ndp); 11172 return (B_FALSE); 11173 } 11174 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11175 KM_SLEEP); 11176 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11177 sizeof (tcpparam_t)); 11178 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11179 tcp_param_get, tcp_param_set_aligned, 11180 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11181 nd_free(ndp); 11182 return (B_FALSE); 11183 } 11184 if (!nd_load(ndp, "tcp_extra_priv_ports", 11185 tcp_extra_priv_ports_get, NULL, NULL)) { 11186 nd_free(ndp); 11187 return (B_FALSE); 11188 } 11189 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11190 NULL, tcp_extra_priv_ports_add, NULL)) { 11191 nd_free(ndp); 11192 return (B_FALSE); 11193 } 11194 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11195 NULL, tcp_extra_priv_ports_del, NULL)) { 11196 nd_free(ndp); 11197 return (B_FALSE); 11198 } 11199 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11200 tcp_1948_phrase_set, NULL)) { 11201 nd_free(ndp); 11202 return (B_FALSE); 11203 } 11204 /* 11205 * Dummy ndd variables - only to convey obsolescence information 11206 * through printing of their name (no get or set routines) 11207 * XXX Remove in future releases ? 11208 */ 11209 if (!nd_load(ndp, 11210 "tcp_close_wait_interval(obsoleted - " 11211 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11212 nd_free(ndp); 11213 return (B_FALSE); 11214 } 11215 return (B_TRUE); 11216 } 11217 11218 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11219 /* ARGSUSED */ 11220 static int 11221 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11222 cred_t *cr) 11223 { 11224 long new_value; 11225 tcpparam_t *tcppa = (tcpparam_t *)cp; 11226 11227 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11228 new_value < tcppa->tcp_param_min || 11229 new_value > tcppa->tcp_param_max) { 11230 return (EINVAL); 11231 } 11232 /* 11233 * Need to make sure new_value is a multiple of 4. If it is not, 11234 * round it up. For future 64 bit requirement, we actually make it 11235 * a multiple of 8. 11236 */ 11237 if (new_value & 0x7) { 11238 new_value = (new_value & ~0x7) + 0x8; 11239 } 11240 tcppa->tcp_param_val = new_value; 11241 return (0); 11242 } 11243 11244 /* Set callback routine passed to nd_load by tcp_param_register */ 11245 /* ARGSUSED */ 11246 static int 11247 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11248 { 11249 long new_value; 11250 tcpparam_t *tcppa = (tcpparam_t *)cp; 11251 11252 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11253 new_value < tcppa->tcp_param_min || 11254 new_value > tcppa->tcp_param_max) { 11255 return (EINVAL); 11256 } 11257 tcppa->tcp_param_val = new_value; 11258 return (0); 11259 } 11260 11261 /* 11262 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11263 * is filled, return as much as we can. The message passed in may be 11264 * multi-part, chained using b_cont. "start" is the starting sequence 11265 * number for this piece. 11266 */ 11267 static mblk_t * 11268 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11269 { 11270 uint32_t end; 11271 mblk_t *mp1; 11272 mblk_t *mp2; 11273 mblk_t *next_mp; 11274 uint32_t u1; 11275 tcp_stack_t *tcps = tcp->tcp_tcps; 11276 11277 /* Walk through all the new pieces. */ 11278 do { 11279 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11280 (uintptr_t)INT_MAX); 11281 end = start + (int)(mp->b_wptr - mp->b_rptr); 11282 next_mp = mp->b_cont; 11283 if (start == end) { 11284 /* Empty. Blast it. */ 11285 freeb(mp); 11286 continue; 11287 } 11288 mp->b_cont = NULL; 11289 TCP_REASS_SET_SEQ(mp, start); 11290 TCP_REASS_SET_END(mp, end); 11291 mp1 = tcp->tcp_reass_tail; 11292 if (!mp1) { 11293 tcp->tcp_reass_tail = mp; 11294 tcp->tcp_reass_head = mp; 11295 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11296 UPDATE_MIB(&tcps->tcps_mib, 11297 tcpInDataUnorderBytes, end - start); 11298 continue; 11299 } 11300 /* New stuff completely beyond tail? */ 11301 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11302 /* Link it on end. */ 11303 mp1->b_cont = mp; 11304 tcp->tcp_reass_tail = mp; 11305 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11306 UPDATE_MIB(&tcps->tcps_mib, 11307 tcpInDataUnorderBytes, end - start); 11308 continue; 11309 } 11310 mp1 = tcp->tcp_reass_head; 11311 u1 = TCP_REASS_SEQ(mp1); 11312 /* New stuff at the front? */ 11313 if (SEQ_LT(start, u1)) { 11314 /* Yes... Check for overlap. */ 11315 mp->b_cont = mp1; 11316 tcp->tcp_reass_head = mp; 11317 tcp_reass_elim_overlap(tcp, mp); 11318 continue; 11319 } 11320 /* 11321 * The new piece fits somewhere between the head and tail. 11322 * We find our slot, where mp1 precedes us and mp2 trails. 11323 */ 11324 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11325 u1 = TCP_REASS_SEQ(mp2); 11326 if (SEQ_LEQ(start, u1)) 11327 break; 11328 } 11329 /* Link ourselves in */ 11330 mp->b_cont = mp2; 11331 mp1->b_cont = mp; 11332 11333 /* Trim overlap with following mblk(s) first */ 11334 tcp_reass_elim_overlap(tcp, mp); 11335 11336 /* Trim overlap with preceding mblk */ 11337 tcp_reass_elim_overlap(tcp, mp1); 11338 11339 } while (start = end, mp = next_mp); 11340 mp1 = tcp->tcp_reass_head; 11341 /* Anything ready to go? */ 11342 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11343 return (NULL); 11344 /* Eat what we can off the queue */ 11345 for (;;) { 11346 mp = mp1->b_cont; 11347 end = TCP_REASS_END(mp1); 11348 TCP_REASS_SET_SEQ(mp1, 0); 11349 TCP_REASS_SET_END(mp1, 0); 11350 if (!mp) { 11351 tcp->tcp_reass_tail = NULL; 11352 break; 11353 } 11354 if (end != TCP_REASS_SEQ(mp)) { 11355 mp1->b_cont = NULL; 11356 break; 11357 } 11358 mp1 = mp; 11359 } 11360 mp1 = tcp->tcp_reass_head; 11361 tcp->tcp_reass_head = mp; 11362 return (mp1); 11363 } 11364 11365 /* Eliminate any overlap that mp may have over later mblks */ 11366 static void 11367 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11368 { 11369 uint32_t end; 11370 mblk_t *mp1; 11371 uint32_t u1; 11372 tcp_stack_t *tcps = tcp->tcp_tcps; 11373 11374 end = TCP_REASS_END(mp); 11375 while ((mp1 = mp->b_cont) != NULL) { 11376 u1 = TCP_REASS_SEQ(mp1); 11377 if (!SEQ_GT(end, u1)) 11378 break; 11379 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11380 mp->b_wptr -= end - u1; 11381 TCP_REASS_SET_END(mp, u1); 11382 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11383 UPDATE_MIB(&tcps->tcps_mib, 11384 tcpInDataPartDupBytes, end - u1); 11385 break; 11386 } 11387 mp->b_cont = mp1->b_cont; 11388 TCP_REASS_SET_SEQ(mp1, 0); 11389 TCP_REASS_SET_END(mp1, 0); 11390 freeb(mp1); 11391 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11392 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11393 } 11394 if (!mp1) 11395 tcp->tcp_reass_tail = mp; 11396 } 11397 11398 static uint_t 11399 tcp_rwnd_reopen(tcp_t *tcp) 11400 { 11401 uint_t ret = 0; 11402 uint_t thwin; 11403 11404 /* Learn the latest rwnd information that we sent to the other side. */ 11405 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11406 << tcp->tcp_rcv_ws; 11407 /* This is peer's calculated send window (our receive window). */ 11408 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11409 /* 11410 * Increase the receive window to max. But we need to do receiver 11411 * SWS avoidance. This means that we need to check the increase of 11412 * of receive window is at least 1 MSS. 11413 */ 11414 if (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss) { 11415 /* 11416 * If the window that the other side knows is less than max 11417 * deferred acks segments, send an update immediately. 11418 */ 11419 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11420 BUMP_MIB(&tcp->tcp_tcps->tcps_mib, tcpOutWinUpdate); 11421 ret = TH_ACK_NEEDED; 11422 } 11423 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 11424 } 11425 return (ret); 11426 } 11427 11428 /* 11429 * Send up all messages queued on tcp_rcv_list. 11430 */ 11431 static uint_t 11432 tcp_rcv_drain(tcp_t *tcp) 11433 { 11434 mblk_t *mp; 11435 uint_t ret = 0; 11436 #ifdef DEBUG 11437 uint_t cnt = 0; 11438 #endif 11439 queue_t *q = tcp->tcp_rq; 11440 11441 /* Can't drain on an eager connection */ 11442 if (tcp->tcp_listener != NULL) 11443 return (ret); 11444 11445 /* Can't be a non-STREAMS connection */ 11446 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 11447 11448 /* No need for the push timer now. */ 11449 if (tcp->tcp_push_tid != 0) { 11450 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11451 tcp->tcp_push_tid = 0; 11452 } 11453 11454 /* 11455 * Handle two cases here: we are currently fused or we were 11456 * previously fused and have some urgent data to be delivered 11457 * upstream. The latter happens because we either ran out of 11458 * memory or were detached and therefore sending the SIGURG was 11459 * deferred until this point. In either case we pass control 11460 * over to tcp_fuse_rcv_drain() since it may need to complete 11461 * some work. 11462 */ 11463 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11464 ASSERT(IPCL_IS_NONSTR(tcp->tcp_connp) || 11465 tcp->tcp_fused_sigurg_mp != NULL); 11466 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11467 &tcp->tcp_fused_sigurg_mp)) 11468 return (ret); 11469 } 11470 11471 while ((mp = tcp->tcp_rcv_list) != NULL) { 11472 tcp->tcp_rcv_list = mp->b_next; 11473 mp->b_next = NULL; 11474 #ifdef DEBUG 11475 cnt += msgdsize(mp); 11476 #endif 11477 /* Does this need SSL processing first? */ 11478 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11479 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11480 mblk_t *, mp); 11481 tcp_kssl_input(tcp, mp); 11482 continue; 11483 } 11484 putnext(q, mp); 11485 } 11486 #ifdef DEBUG 11487 ASSERT(cnt == tcp->tcp_rcv_cnt); 11488 #endif 11489 tcp->tcp_rcv_last_head = NULL; 11490 tcp->tcp_rcv_last_tail = NULL; 11491 tcp->tcp_rcv_cnt = 0; 11492 11493 if (canputnext(q)) 11494 return (tcp_rwnd_reopen(tcp)); 11495 11496 return (ret); 11497 } 11498 11499 /* 11500 * Queue data on tcp_rcv_list which is a b_next chain. 11501 * tcp_rcv_last_head/tail is the last element of this chain. 11502 * Each element of the chain is a b_cont chain. 11503 * 11504 * M_DATA messages are added to the current element. 11505 * Other messages are added as new (b_next) elements. 11506 */ 11507 void 11508 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11509 { 11510 ASSERT(seg_len == msgdsize(mp)); 11511 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11512 11513 if (tcp->tcp_rcv_list == NULL) { 11514 ASSERT(tcp->tcp_rcv_last_head == NULL); 11515 tcp->tcp_rcv_list = mp; 11516 tcp->tcp_rcv_last_head = mp; 11517 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11518 tcp->tcp_rcv_last_tail->b_cont = mp; 11519 } else { 11520 tcp->tcp_rcv_last_head->b_next = mp; 11521 tcp->tcp_rcv_last_head = mp; 11522 } 11523 11524 while (mp->b_cont) 11525 mp = mp->b_cont; 11526 11527 tcp->tcp_rcv_last_tail = mp; 11528 tcp->tcp_rcv_cnt += seg_len; 11529 tcp->tcp_rwnd -= seg_len; 11530 } 11531 11532 /* 11533 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11534 * 11535 * This is the default entry function into TCP on the read side. TCP is 11536 * always entered via squeue i.e. using squeue's for mutual exclusion. 11537 * When classifier does a lookup to find the tcp, it also puts a reference 11538 * on the conn structure associated so the tcp is guaranteed to exist 11539 * when we come here. We still need to check the state because it might 11540 * as well has been closed. The squeue processing function i.e. squeue_enter, 11541 * is responsible for doing the CONN_DEC_REF. 11542 * 11543 * Apart from the default entry point, IP also sends packets directly to 11544 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11545 * connections. 11546 */ 11547 boolean_t tcp_outbound_squeue_switch = B_FALSE; 11548 void 11549 tcp_input(void *arg, mblk_t *mp, void *arg2) 11550 { 11551 conn_t *connp = (conn_t *)arg; 11552 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11553 11554 /* arg2 is the sqp */ 11555 ASSERT(arg2 != NULL); 11556 ASSERT(mp != NULL); 11557 11558 /* 11559 * Don't accept any input on a closed tcp as this TCP logically does 11560 * not exist on the system. Don't proceed further with this TCP. 11561 * For eg. this packet could trigger another close of this tcp 11562 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11563 * tcp_clean_death / tcp_closei_local must be called at most once 11564 * on a TCP. In this case we need to refeed the packet into the 11565 * classifier and figure out where the packet should go. Need to 11566 * preserve the recv_ill somehow. Until we figure that out, for 11567 * now just drop the packet if we can't classify the packet. 11568 */ 11569 if (tcp->tcp_state == TCPS_CLOSED || 11570 tcp->tcp_state == TCPS_BOUND) { 11571 conn_t *new_connp; 11572 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11573 11574 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11575 if (new_connp != NULL) { 11576 tcp_reinput(new_connp, mp, arg2); 11577 return; 11578 } 11579 /* We failed to classify. For now just drop the packet */ 11580 freemsg(mp); 11581 return; 11582 } 11583 11584 if (DB_TYPE(mp) != M_DATA) { 11585 tcp_rput_common(tcp, mp); 11586 return; 11587 } 11588 11589 if (mp->b_datap->db_struioflag & STRUIO_CONNECT) { 11590 squeue_t *final_sqp; 11591 11592 mp->b_datap->db_struioflag &= ~STRUIO_CONNECT; 11593 final_sqp = (squeue_t *)DB_CKSUMSTART(mp); 11594 DB_CKSUMSTART(mp) = 0; 11595 if (tcp->tcp_state == TCPS_SYN_SENT && 11596 connp->conn_final_sqp == NULL && 11597 tcp_outbound_squeue_switch) { 11598 ASSERT(connp->conn_initial_sqp == connp->conn_sqp); 11599 connp->conn_final_sqp = final_sqp; 11600 if (connp->conn_final_sqp != connp->conn_sqp) { 11601 CONN_INC_REF(connp); 11602 SQUEUE_SWITCH(connp, connp->conn_final_sqp); 11603 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 11604 tcp_rput_data, connp, ip_squeue_flag, 11605 SQTAG_CONNECT_FINISH); 11606 return; 11607 } 11608 } 11609 } 11610 tcp_rput_data(connp, mp, arg2); 11611 } 11612 11613 /* 11614 * The read side put procedure. 11615 * The packets passed up by ip are assume to be aligned according to 11616 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11617 */ 11618 static void 11619 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11620 { 11621 /* 11622 * tcp_rput_data() does not expect M_CTL except for the case 11623 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11624 * type. Need to make sure that any other M_CTLs don't make 11625 * it to tcp_rput_data since it is not expecting any and doesn't 11626 * check for it. 11627 */ 11628 if (DB_TYPE(mp) == M_CTL) { 11629 switch (*(uint32_t *)(mp->b_rptr)) { 11630 case TCP_IOC_ABORT_CONN: 11631 /* 11632 * Handle connection abort request. 11633 */ 11634 tcp_ioctl_abort_handler(tcp, mp); 11635 return; 11636 case IPSEC_IN: 11637 /* 11638 * Only secure icmp arrive in TCP and they 11639 * don't go through data path. 11640 */ 11641 tcp_icmp_error(tcp, mp); 11642 return; 11643 case IN_PKTINFO: 11644 /* 11645 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11646 * sockets that are receiving IPv4 traffic. tcp 11647 */ 11648 ASSERT(tcp->tcp_family == AF_INET6); 11649 ASSERT(tcp->tcp_ipv6_recvancillary & 11650 TCP_IPV6_RECVPKTINFO); 11651 tcp_rput_data(tcp->tcp_connp, mp, 11652 tcp->tcp_connp->conn_sqp); 11653 return; 11654 case MDT_IOC_INFO_UPDATE: 11655 /* 11656 * Handle Multidata information update; the 11657 * following routine will free the message. 11658 */ 11659 if (tcp->tcp_connp->conn_mdt_ok) { 11660 tcp_mdt_update(tcp, 11661 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11662 B_FALSE); 11663 } 11664 freemsg(mp); 11665 return; 11666 case LSO_IOC_INFO_UPDATE: 11667 /* 11668 * Handle LSO information update; the following 11669 * routine will free the message. 11670 */ 11671 if (tcp->tcp_connp->conn_lso_ok) { 11672 tcp_lso_update(tcp, 11673 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11674 } 11675 freemsg(mp); 11676 return; 11677 default: 11678 /* 11679 * tcp_icmp_err() will process the M_CTL packets. 11680 * Non-ICMP packets, if any, will be discarded in 11681 * tcp_icmp_err(). We will process the ICMP packet 11682 * even if we are TCP_IS_DETACHED_NONEAGER as the 11683 * incoming ICMP packet may result in changing 11684 * the tcp_mss, which we would need if we have 11685 * packets to retransmit. 11686 */ 11687 tcp_icmp_error(tcp, mp); 11688 return; 11689 } 11690 } 11691 11692 /* No point processing the message if tcp is already closed */ 11693 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11694 freemsg(mp); 11695 return; 11696 } 11697 11698 tcp_rput_other(tcp, mp); 11699 } 11700 11701 11702 /* The minimum of smoothed mean deviation in RTO calculation. */ 11703 #define TCP_SD_MIN 400 11704 11705 /* 11706 * Set RTO for this connection. The formula is from Jacobson and Karels' 11707 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11708 * are the same as those in Appendix A.2 of that paper. 11709 * 11710 * m = new measurement 11711 * sa = smoothed RTT average (8 * average estimates). 11712 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11713 */ 11714 static void 11715 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11716 { 11717 long m = TICK_TO_MSEC(rtt); 11718 clock_t sa = tcp->tcp_rtt_sa; 11719 clock_t sv = tcp->tcp_rtt_sd; 11720 clock_t rto; 11721 tcp_stack_t *tcps = tcp->tcp_tcps; 11722 11723 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11724 tcp->tcp_rtt_update++; 11725 11726 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11727 if (sa != 0) { 11728 /* 11729 * Update average estimator: 11730 * new rtt = 7/8 old rtt + 1/8 Error 11731 */ 11732 11733 /* m is now Error in estimate. */ 11734 m -= sa >> 3; 11735 if ((sa += m) <= 0) { 11736 /* 11737 * Don't allow the smoothed average to be negative. 11738 * We use 0 to denote reinitialization of the 11739 * variables. 11740 */ 11741 sa = 1; 11742 } 11743 11744 /* 11745 * Update deviation estimator: 11746 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11747 */ 11748 if (m < 0) 11749 m = -m; 11750 m -= sv >> 2; 11751 sv += m; 11752 } else { 11753 /* 11754 * This follows BSD's implementation. So the reinitialized 11755 * RTO is 3 * m. We cannot go less than 2 because if the 11756 * link is bandwidth dominated, doubling the window size 11757 * during slow start means doubling the RTT. We want to be 11758 * more conservative when we reinitialize our estimates. 3 11759 * is just a convenient number. 11760 */ 11761 sa = m << 3; 11762 sv = m << 1; 11763 } 11764 if (sv < TCP_SD_MIN) { 11765 /* 11766 * We do not know that if sa captures the delay ACK 11767 * effect as in a long train of segments, a receiver 11768 * does not delay its ACKs. So set the minimum of sv 11769 * to be TCP_SD_MIN, which is default to 400 ms, twice 11770 * of BSD DATO. That means the minimum of mean 11771 * deviation is 100 ms. 11772 * 11773 */ 11774 sv = TCP_SD_MIN; 11775 } 11776 tcp->tcp_rtt_sa = sa; 11777 tcp->tcp_rtt_sd = sv; 11778 /* 11779 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11780 * 11781 * Add tcp_rexmit_interval extra in case of extreme environment 11782 * where the algorithm fails to work. The default value of 11783 * tcp_rexmit_interval_extra should be 0. 11784 * 11785 * As we use a finer grained clock than BSD and update 11786 * RTO for every ACKs, add in another .25 of RTT to the 11787 * deviation of RTO to accomodate burstiness of 1/4 of 11788 * window size. 11789 */ 11790 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 11791 11792 if (rto > tcps->tcps_rexmit_interval_max) { 11793 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 11794 } else if (rto < tcps->tcps_rexmit_interval_min) { 11795 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 11796 } else { 11797 tcp->tcp_rto = rto; 11798 } 11799 11800 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11801 tcp->tcp_timer_backoff = 0; 11802 } 11803 11804 /* 11805 * tcp_get_seg_mp() is called to get the pointer to a segment in the 11806 * send queue which starts at the given sequence number. If the given 11807 * sequence number is equal to last valid sequence number (tcp_snxt), the 11808 * returned mblk is the last valid mblk, and off is set to the length of 11809 * that mblk. 11810 * 11811 * send queue which starts at the given seq. no. 11812 * 11813 * Parameters: 11814 * tcp_t *tcp: the tcp instance pointer. 11815 * uint32_t seq: the starting seq. no of the requested segment. 11816 * int32_t *off: after the execution, *off will be the offset to 11817 * the returned mblk which points to the requested seq no. 11818 * It is the caller's responsibility to send in a non-null off. 11819 * 11820 * Return: 11821 * A mblk_t pointer pointing to the requested segment in send queue. 11822 */ 11823 static mblk_t * 11824 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 11825 { 11826 int32_t cnt; 11827 mblk_t *mp; 11828 11829 /* Defensive coding. Make sure we don't send incorrect data. */ 11830 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GT(seq, tcp->tcp_snxt)) 11831 return (NULL); 11832 11833 cnt = seq - tcp->tcp_suna; 11834 mp = tcp->tcp_xmit_head; 11835 while (cnt > 0 && mp != NULL) { 11836 cnt -= mp->b_wptr - mp->b_rptr; 11837 if (cnt <= 0) { 11838 cnt += mp->b_wptr - mp->b_rptr; 11839 break; 11840 } 11841 mp = mp->b_cont; 11842 } 11843 ASSERT(mp != NULL); 11844 *off = cnt; 11845 return (mp); 11846 } 11847 11848 /* 11849 * This function handles all retransmissions if SACK is enabled for this 11850 * connection. First it calculates how many segments can be retransmitted 11851 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 11852 * segments. A segment is eligible if sack_cnt for that segment is greater 11853 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 11854 * all eligible segments, it checks to see if TCP can send some new segments 11855 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 11856 * 11857 * Parameters: 11858 * tcp_t *tcp: the tcp structure of the connection. 11859 * uint_t *flags: in return, appropriate value will be set for 11860 * tcp_rput_data(). 11861 */ 11862 static void 11863 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 11864 { 11865 notsack_blk_t *notsack_blk; 11866 int32_t usable_swnd; 11867 int32_t mss; 11868 uint32_t seg_len; 11869 mblk_t *xmit_mp; 11870 tcp_stack_t *tcps = tcp->tcp_tcps; 11871 11872 ASSERT(tcp->tcp_sack_info != NULL); 11873 ASSERT(tcp->tcp_notsack_list != NULL); 11874 ASSERT(tcp->tcp_rexmit == B_FALSE); 11875 11876 /* Defensive coding in case there is a bug... */ 11877 if (tcp->tcp_notsack_list == NULL) { 11878 return; 11879 } 11880 notsack_blk = tcp->tcp_notsack_list; 11881 mss = tcp->tcp_mss; 11882 11883 /* 11884 * Limit the num of outstanding data in the network to be 11885 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 11886 */ 11887 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11888 11889 /* At least retransmit 1 MSS of data. */ 11890 if (usable_swnd <= 0) { 11891 usable_swnd = mss; 11892 } 11893 11894 /* Make sure no new RTT samples will be taken. */ 11895 tcp->tcp_csuna = tcp->tcp_snxt; 11896 11897 notsack_blk = tcp->tcp_notsack_list; 11898 while (usable_swnd > 0) { 11899 mblk_t *snxt_mp, *tmp_mp; 11900 tcp_seq begin = tcp->tcp_sack_snxt; 11901 tcp_seq end; 11902 int32_t off; 11903 11904 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 11905 if (SEQ_GT(notsack_blk->end, begin) && 11906 (notsack_blk->sack_cnt >= 11907 tcps->tcps_dupack_fast_retransmit)) { 11908 end = notsack_blk->end; 11909 if (SEQ_LT(begin, notsack_blk->begin)) { 11910 begin = notsack_blk->begin; 11911 } 11912 break; 11913 } 11914 } 11915 /* 11916 * All holes are filled. Manipulate tcp_cwnd to send more 11917 * if we can. Note that after the SACK recovery, tcp_cwnd is 11918 * set to tcp_cwnd_ssthresh. 11919 */ 11920 if (notsack_blk == NULL) { 11921 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11922 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 11923 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 11924 ASSERT(tcp->tcp_cwnd > 0); 11925 return; 11926 } else { 11927 usable_swnd = usable_swnd / mss; 11928 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 11929 MAX(usable_swnd * mss, mss); 11930 *flags |= TH_XMIT_NEEDED; 11931 return; 11932 } 11933 } 11934 11935 /* 11936 * Note that we may send more than usable_swnd allows here 11937 * because of round off, but no more than 1 MSS of data. 11938 */ 11939 seg_len = end - begin; 11940 if (seg_len > mss) 11941 seg_len = mss; 11942 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 11943 ASSERT(snxt_mp != NULL); 11944 /* This should not happen. Defensive coding again... */ 11945 if (snxt_mp == NULL) { 11946 return; 11947 } 11948 11949 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 11950 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 11951 if (xmit_mp == NULL) 11952 return; 11953 11954 usable_swnd -= seg_len; 11955 tcp->tcp_pipe += seg_len; 11956 tcp->tcp_sack_snxt = begin + seg_len; 11957 11958 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 11959 11960 /* 11961 * Update the send timestamp to avoid false retransmission. 11962 */ 11963 snxt_mp->b_prev = (mblk_t *)lbolt; 11964 11965 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 11966 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 11967 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 11968 /* 11969 * Update tcp_rexmit_max to extend this SACK recovery phase. 11970 * This happens when new data sent during fast recovery is 11971 * also lost. If TCP retransmits those new data, it needs 11972 * to extend SACK recover phase to avoid starting another 11973 * fast retransmit/recovery unnecessarily. 11974 */ 11975 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 11976 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 11977 } 11978 } 11979 } 11980 11981 /* 11982 * This function handles policy checking at TCP level for non-hard_bound/ 11983 * detached connections. 11984 */ 11985 static boolean_t 11986 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 11987 boolean_t secure, boolean_t mctl_present) 11988 { 11989 ipsec_latch_t *ipl = NULL; 11990 ipsec_action_t *act = NULL; 11991 mblk_t *data_mp; 11992 ipsec_in_t *ii; 11993 const char *reason; 11994 kstat_named_t *counter; 11995 tcp_stack_t *tcps = tcp->tcp_tcps; 11996 ipsec_stack_t *ipss; 11997 ip_stack_t *ipst; 11998 11999 ASSERT(mctl_present || !secure); 12000 12001 ASSERT((ipha == NULL && ip6h != NULL) || 12002 (ip6h == NULL && ipha != NULL)); 12003 12004 /* 12005 * We don't necessarily have an ipsec_in_act action to verify 12006 * policy because of assymetrical policy where we have only 12007 * outbound policy and no inbound policy (possible with global 12008 * policy). 12009 */ 12010 if (!secure) { 12011 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12012 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12013 return (B_TRUE); 12014 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12015 "tcp_check_policy", ipha, ip6h, secure, 12016 tcps->tcps_netstack); 12017 ipss = tcps->tcps_netstack->netstack_ipsec; 12018 12019 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12020 DROPPER(ipss, ipds_tcp_clear), 12021 &tcps->tcps_dropper); 12022 return (B_FALSE); 12023 } 12024 12025 /* 12026 * We have a secure packet. 12027 */ 12028 if (act == NULL) { 12029 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12030 "tcp_check_policy", ipha, ip6h, secure, 12031 tcps->tcps_netstack); 12032 ipss = tcps->tcps_netstack->netstack_ipsec; 12033 12034 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12035 DROPPER(ipss, ipds_tcp_secure), 12036 &tcps->tcps_dropper); 12037 return (B_FALSE); 12038 } 12039 12040 /* 12041 * XXX This whole routine is currently incorrect. ipl should 12042 * be set to the latch pointer, but is currently not set, so 12043 * we initialize it to NULL to avoid picking up random garbage. 12044 */ 12045 if (ipl == NULL) 12046 return (B_TRUE); 12047 12048 data_mp = first_mp->b_cont; 12049 12050 ii = (ipsec_in_t *)first_mp->b_rptr; 12051 12052 ipst = tcps->tcps_netstack->netstack_ip; 12053 12054 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12055 &counter, tcp->tcp_connp)) { 12056 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12057 return (B_TRUE); 12058 } 12059 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12060 "tcp inbound policy mismatch: %s, packet dropped\n", 12061 reason); 12062 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12063 12064 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12065 &tcps->tcps_dropper); 12066 return (B_FALSE); 12067 } 12068 12069 /* 12070 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12071 * retransmission after a timeout. 12072 * 12073 * To limit the number of duplicate segments, we limit the number of segment 12074 * to be sent in one time to tcp_snd_burst, the burst variable. 12075 */ 12076 static void 12077 tcp_ss_rexmit(tcp_t *tcp) 12078 { 12079 uint32_t snxt; 12080 uint32_t smax; 12081 int32_t win; 12082 int32_t mss; 12083 int32_t off; 12084 int32_t burst = tcp->tcp_snd_burst; 12085 mblk_t *snxt_mp; 12086 tcp_stack_t *tcps = tcp->tcp_tcps; 12087 12088 /* 12089 * Note that tcp_rexmit can be set even though TCP has retransmitted 12090 * all unack'ed segments. 12091 */ 12092 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12093 smax = tcp->tcp_rexmit_max; 12094 snxt = tcp->tcp_rexmit_nxt; 12095 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12096 snxt = tcp->tcp_suna; 12097 } 12098 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12099 win -= snxt - tcp->tcp_suna; 12100 mss = tcp->tcp_mss; 12101 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12102 12103 while (SEQ_LT(snxt, smax) && (win > 0) && 12104 (burst > 0) && (snxt_mp != NULL)) { 12105 mblk_t *xmit_mp; 12106 mblk_t *old_snxt_mp = snxt_mp; 12107 uint32_t cnt = mss; 12108 12109 if (win < cnt) { 12110 cnt = win; 12111 } 12112 if (SEQ_GT(snxt + cnt, smax)) { 12113 cnt = smax - snxt; 12114 } 12115 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12116 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12117 if (xmit_mp == NULL) 12118 return; 12119 12120 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12121 12122 snxt += cnt; 12123 win -= cnt; 12124 /* 12125 * Update the send timestamp to avoid false 12126 * retransmission. 12127 */ 12128 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12129 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12130 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12131 12132 tcp->tcp_rexmit_nxt = snxt; 12133 burst--; 12134 } 12135 /* 12136 * If we have transmitted all we have at the time 12137 * we started the retranmission, we can leave 12138 * the rest of the job to tcp_wput_data(). But we 12139 * need to check the send window first. If the 12140 * win is not 0, go on with tcp_wput_data(). 12141 */ 12142 if (SEQ_LT(snxt, smax) || win == 0) { 12143 return; 12144 } 12145 } 12146 /* Only call tcp_wput_data() if there is data to be sent. */ 12147 if (tcp->tcp_unsent) { 12148 tcp_wput_data(tcp, NULL, B_FALSE); 12149 } 12150 } 12151 12152 /* 12153 * Process all TCP option in SYN segment. Note that this function should 12154 * be called after tcp_adapt_ire() is called so that the necessary info 12155 * from IRE is already set in the tcp structure. 12156 * 12157 * This function sets up the correct tcp_mss value according to the 12158 * MSS option value and our header size. It also sets up the window scale 12159 * and timestamp values, and initialize SACK info blocks. But it does not 12160 * change receive window size after setting the tcp_mss value. The caller 12161 * should do the appropriate change. 12162 */ 12163 void 12164 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12165 { 12166 int options; 12167 tcp_opt_t tcpopt; 12168 uint32_t mss_max; 12169 char *tmp_tcph; 12170 tcp_stack_t *tcps = tcp->tcp_tcps; 12171 12172 tcpopt.tcp = NULL; 12173 options = tcp_parse_options(tcph, &tcpopt); 12174 12175 /* 12176 * Process MSS option. Note that MSS option value does not account 12177 * for IP or TCP options. This means that it is equal to MTU - minimum 12178 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12179 * IPv6. 12180 */ 12181 if (!(options & TCP_OPT_MSS_PRESENT)) { 12182 if (tcp->tcp_ipversion == IPV4_VERSION) 12183 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12184 else 12185 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12186 } else { 12187 if (tcp->tcp_ipversion == IPV4_VERSION) 12188 mss_max = tcps->tcps_mss_max_ipv4; 12189 else 12190 mss_max = tcps->tcps_mss_max_ipv6; 12191 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12192 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12193 else if (tcpopt.tcp_opt_mss > mss_max) 12194 tcpopt.tcp_opt_mss = mss_max; 12195 } 12196 12197 /* Process Window Scale option. */ 12198 if (options & TCP_OPT_WSCALE_PRESENT) { 12199 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12200 tcp->tcp_snd_ws_ok = B_TRUE; 12201 } else { 12202 tcp->tcp_snd_ws = B_FALSE; 12203 tcp->tcp_snd_ws_ok = B_FALSE; 12204 tcp->tcp_rcv_ws = B_FALSE; 12205 } 12206 12207 /* Process Timestamp option. */ 12208 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12209 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12210 tmp_tcph = (char *)tcp->tcp_tcph; 12211 12212 tcp->tcp_snd_ts_ok = B_TRUE; 12213 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12214 tcp->tcp_last_rcv_lbolt = lbolt64; 12215 ASSERT(OK_32PTR(tmp_tcph)); 12216 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12217 12218 /* Fill in our template header with basic timestamp option. */ 12219 tmp_tcph += tcp->tcp_tcp_hdr_len; 12220 tmp_tcph[0] = TCPOPT_NOP; 12221 tmp_tcph[1] = TCPOPT_NOP; 12222 tmp_tcph[2] = TCPOPT_TSTAMP; 12223 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12224 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12225 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12226 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12227 } else { 12228 tcp->tcp_snd_ts_ok = B_FALSE; 12229 } 12230 12231 /* 12232 * Process SACK options. If SACK is enabled for this connection, 12233 * then allocate the SACK info structure. Note the following ways 12234 * when tcp_snd_sack_ok is set to true. 12235 * 12236 * For active connection: in tcp_adapt_ire() called in 12237 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12238 * is checked. 12239 * 12240 * For passive connection: in tcp_adapt_ire() called in 12241 * tcp_accept_comm(). 12242 * 12243 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12244 * That check makes sure that if we did not send a SACK OK option, 12245 * we will not enable SACK for this connection even though the other 12246 * side sends us SACK OK option. For active connection, the SACK 12247 * info structure has already been allocated. So we need to free 12248 * it if SACK is disabled. 12249 */ 12250 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12251 (tcp->tcp_snd_sack_ok || 12252 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12253 /* This should be true only in the passive case. */ 12254 if (tcp->tcp_sack_info == NULL) { 12255 ASSERT(TCP_IS_DETACHED(tcp)); 12256 tcp->tcp_sack_info = 12257 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12258 } 12259 if (tcp->tcp_sack_info == NULL) { 12260 tcp->tcp_snd_sack_ok = B_FALSE; 12261 } else { 12262 tcp->tcp_snd_sack_ok = B_TRUE; 12263 if (tcp->tcp_snd_ts_ok) { 12264 tcp->tcp_max_sack_blk = 3; 12265 } else { 12266 tcp->tcp_max_sack_blk = 4; 12267 } 12268 } 12269 } else { 12270 /* 12271 * Resetting tcp_snd_sack_ok to B_FALSE so that 12272 * no SACK info will be used for this 12273 * connection. This assumes that SACK usage 12274 * permission is negotiated. This may need 12275 * to be changed once this is clarified. 12276 */ 12277 if (tcp->tcp_sack_info != NULL) { 12278 ASSERT(tcp->tcp_notsack_list == NULL); 12279 kmem_cache_free(tcp_sack_info_cache, 12280 tcp->tcp_sack_info); 12281 tcp->tcp_sack_info = NULL; 12282 } 12283 tcp->tcp_snd_sack_ok = B_FALSE; 12284 } 12285 12286 /* 12287 * Now we know the exact TCP/IP header length, subtract 12288 * that from tcp_mss to get our side's MSS. 12289 */ 12290 tcp->tcp_mss -= tcp->tcp_hdr_len; 12291 /* 12292 * Here we assume that the other side's header size will be equal to 12293 * our header size. We calculate the real MSS accordingly. Need to 12294 * take into additional stuffs IPsec puts in. 12295 * 12296 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12297 */ 12298 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12299 ((tcp->tcp_ipversion == IPV4_VERSION ? 12300 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12301 12302 /* 12303 * Set MSS to the smaller one of both ends of the connection. 12304 * We should not have called tcp_mss_set() before, but our 12305 * side of the MSS should have been set to a proper value 12306 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12307 * STREAM head parameters properly. 12308 * 12309 * If we have a larger-than-16-bit window but the other side 12310 * didn't want to do window scale, tcp_rwnd_set() will take 12311 * care of that. 12312 */ 12313 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12314 } 12315 12316 /* 12317 * Sends the T_CONN_IND to the listener. The caller calls this 12318 * functions via squeue to get inside the listener's perimeter 12319 * once the 3 way hand shake is done a T_CONN_IND needs to be 12320 * sent. As an optimization, the caller can call this directly 12321 * if listener's perimeter is same as eager's. 12322 */ 12323 /* ARGSUSED */ 12324 void 12325 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12326 { 12327 conn_t *lconnp = (conn_t *)arg; 12328 tcp_t *listener = lconnp->conn_tcp; 12329 tcp_t *tcp; 12330 struct T_conn_ind *conn_ind; 12331 ipaddr_t *addr_cache; 12332 boolean_t need_send_conn_ind = B_FALSE; 12333 tcp_stack_t *tcps = listener->tcp_tcps; 12334 12335 /* retrieve the eager */ 12336 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12337 ASSERT(conn_ind->OPT_offset != 0 && 12338 conn_ind->OPT_length == sizeof (intptr_t)); 12339 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12340 conn_ind->OPT_length); 12341 12342 /* 12343 * TLI/XTI applications will get confused by 12344 * sending eager as an option since it violates 12345 * the option semantics. So remove the eager as 12346 * option since TLI/XTI app doesn't need it anyway. 12347 */ 12348 if (!TCP_IS_SOCKET(listener)) { 12349 conn_ind->OPT_length = 0; 12350 conn_ind->OPT_offset = 0; 12351 } 12352 if (listener->tcp_state != TCPS_LISTEN) { 12353 /* 12354 * If listener has closed, it would have caused a 12355 * a cleanup/blowoff to happen for the eager. We 12356 * just need to return. 12357 */ 12358 freemsg(mp); 12359 return; 12360 } 12361 12362 12363 /* 12364 * if the conn_req_q is full defer passing up the 12365 * T_CONN_IND until space is availabe after t_accept() 12366 * processing 12367 */ 12368 mutex_enter(&listener->tcp_eager_lock); 12369 12370 /* 12371 * Take the eager out, if it is in the list of droppable eagers 12372 * as we are here because the 3W handshake is over. 12373 */ 12374 MAKE_UNDROPPABLE(tcp); 12375 12376 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12377 tcp_t *tail; 12378 12379 /* 12380 * The eager already has an extra ref put in tcp_rput_data 12381 * so that it stays till accept comes back even though it 12382 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12383 */ 12384 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12385 listener->tcp_conn_req_cnt_q0--; 12386 listener->tcp_conn_req_cnt_q++; 12387 12388 /* Move from SYN_RCVD to ESTABLISHED list */ 12389 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12390 tcp->tcp_eager_prev_q0; 12391 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12392 tcp->tcp_eager_next_q0; 12393 tcp->tcp_eager_prev_q0 = NULL; 12394 tcp->tcp_eager_next_q0 = NULL; 12395 12396 /* 12397 * Insert at end of the queue because sockfs 12398 * sends down T_CONN_RES in chronological 12399 * order. Leaving the older conn indications 12400 * at front of the queue helps reducing search 12401 * time. 12402 */ 12403 tail = listener->tcp_eager_last_q; 12404 if (tail != NULL) 12405 tail->tcp_eager_next_q = tcp; 12406 else 12407 listener->tcp_eager_next_q = tcp; 12408 listener->tcp_eager_last_q = tcp; 12409 tcp->tcp_eager_next_q = NULL; 12410 /* 12411 * Delay sending up the T_conn_ind until we are 12412 * done with the eager. Once we have have sent up 12413 * the T_conn_ind, the accept can potentially complete 12414 * any time and release the refhold we have on the eager. 12415 */ 12416 need_send_conn_ind = B_TRUE; 12417 } else { 12418 /* 12419 * Defer connection on q0 and set deferred 12420 * connection bit true 12421 */ 12422 tcp->tcp_conn_def_q0 = B_TRUE; 12423 12424 /* take tcp out of q0 ... */ 12425 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12426 tcp->tcp_eager_next_q0; 12427 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12428 tcp->tcp_eager_prev_q0; 12429 12430 /* ... and place it at the end of q0 */ 12431 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12432 tcp->tcp_eager_next_q0 = listener; 12433 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12434 listener->tcp_eager_prev_q0 = tcp; 12435 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12436 } 12437 12438 /* we have timed out before */ 12439 if (tcp->tcp_syn_rcvd_timeout != 0) { 12440 tcp->tcp_syn_rcvd_timeout = 0; 12441 listener->tcp_syn_rcvd_timeout--; 12442 if (listener->tcp_syn_defense && 12443 listener->tcp_syn_rcvd_timeout <= 12444 (tcps->tcps_conn_req_max_q0 >> 5) && 12445 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12446 listener->tcp_last_rcv_lbolt)) { 12447 /* 12448 * Turn off the defense mode if we 12449 * believe the SYN attack is over. 12450 */ 12451 listener->tcp_syn_defense = B_FALSE; 12452 if (listener->tcp_ip_addr_cache) { 12453 kmem_free((void *)listener->tcp_ip_addr_cache, 12454 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12455 listener->tcp_ip_addr_cache = NULL; 12456 } 12457 } 12458 } 12459 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12460 if (addr_cache != NULL) { 12461 /* 12462 * We have finished a 3-way handshake with this 12463 * remote host. This proves the IP addr is good. 12464 * Cache it! 12465 */ 12466 addr_cache[IP_ADDR_CACHE_HASH( 12467 tcp->tcp_remote)] = tcp->tcp_remote; 12468 } 12469 mutex_exit(&listener->tcp_eager_lock); 12470 if (need_send_conn_ind) 12471 tcp_ulp_newconn(lconnp, tcp->tcp_connp, mp); 12472 } 12473 12474 /* 12475 * Send the newconn notification to ulp. The eager is blown off if the 12476 * notification fails. 12477 */ 12478 static void 12479 tcp_ulp_newconn(conn_t *lconnp, conn_t *econnp, mblk_t *mp) 12480 { 12481 if (IPCL_IS_NONSTR(lconnp)) { 12482 cred_t *cr; 12483 pid_t cpid; 12484 12485 cr = msg_getcred(mp, &cpid); 12486 12487 ASSERT(econnp->conn_tcp->tcp_listener == lconnp->conn_tcp); 12488 ASSERT(econnp->conn_tcp->tcp_saved_listener == 12489 lconnp->conn_tcp); 12490 12491 /* Keep the message around in case of a fallback to TPI */ 12492 econnp->conn_tcp->tcp_conn.tcp_eager_conn_ind = mp; 12493 12494 /* 12495 * Notify the ULP about the newconn. It is guaranteed that no 12496 * tcp_accept() call will be made for the eager if the 12497 * notification fails, so it's safe to blow it off in that 12498 * case. 12499 * 12500 * The upper handle will be assigned when tcp_accept() is 12501 * called. 12502 */ 12503 if ((*lconnp->conn_upcalls->su_newconn) 12504 (lconnp->conn_upper_handle, 12505 (sock_lower_handle_t)econnp, 12506 &sock_tcp_downcalls, cr, cpid, 12507 &econnp->conn_upcalls) == NULL) { 12508 /* Failed to allocate a socket */ 12509 BUMP_MIB(&lconnp->conn_tcp->tcp_tcps->tcps_mib, 12510 tcpEstabResets); 12511 (void) tcp_eager_blowoff(lconnp->conn_tcp, 12512 econnp->conn_tcp->tcp_conn_req_seqnum); 12513 } 12514 } else { 12515 putnext(lconnp->conn_tcp->tcp_rq, mp); 12516 } 12517 } 12518 12519 mblk_t * 12520 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12521 uint_t *ifindexp, ip6_pkt_t *ippp) 12522 { 12523 ip_pktinfo_t *pinfo; 12524 ip6_t *ip6h; 12525 uchar_t *rptr; 12526 mblk_t *first_mp = mp; 12527 boolean_t mctl_present = B_FALSE; 12528 uint_t ifindex = 0; 12529 ip6_pkt_t ipp; 12530 uint_t ipvers; 12531 uint_t ip_hdr_len; 12532 tcp_stack_t *tcps = tcp->tcp_tcps; 12533 12534 rptr = mp->b_rptr; 12535 ASSERT(OK_32PTR(rptr)); 12536 ASSERT(tcp != NULL); 12537 ipp.ipp_fields = 0; 12538 12539 switch DB_TYPE(mp) { 12540 case M_CTL: 12541 mp = mp->b_cont; 12542 if (mp == NULL) { 12543 freemsg(first_mp); 12544 return (NULL); 12545 } 12546 if (DB_TYPE(mp) != M_DATA) { 12547 freemsg(first_mp); 12548 return (NULL); 12549 } 12550 mctl_present = B_TRUE; 12551 break; 12552 case M_DATA: 12553 break; 12554 default: 12555 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12556 freemsg(mp); 12557 return (NULL); 12558 } 12559 ipvers = IPH_HDR_VERSION(rptr); 12560 if (ipvers == IPV4_VERSION) { 12561 if (tcp == NULL) { 12562 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12563 goto done; 12564 } 12565 12566 ipp.ipp_fields |= IPPF_HOPLIMIT; 12567 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12568 12569 /* 12570 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12571 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12572 */ 12573 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12574 mctl_present) { 12575 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12576 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12577 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12578 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12579 ipp.ipp_fields |= IPPF_IFINDEX; 12580 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12581 ifindex = pinfo->ip_pkt_ifindex; 12582 } 12583 freeb(first_mp); 12584 mctl_present = B_FALSE; 12585 } 12586 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12587 } else { 12588 ip6h = (ip6_t *)rptr; 12589 12590 ASSERT(ipvers == IPV6_VERSION); 12591 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12592 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12593 ipp.ipp_hoplimit = ip6h->ip6_hops; 12594 12595 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12596 uint8_t nexthdrp; 12597 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12598 12599 /* Look for ifindex information */ 12600 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12601 ip6i_t *ip6i = (ip6i_t *)ip6h; 12602 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12603 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12604 freemsg(first_mp); 12605 return (NULL); 12606 } 12607 12608 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12609 ASSERT(ip6i->ip6i_ifindex != 0); 12610 ipp.ipp_fields |= IPPF_IFINDEX; 12611 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12612 ifindex = ip6i->ip6i_ifindex; 12613 } 12614 rptr = (uchar_t *)&ip6i[1]; 12615 mp->b_rptr = rptr; 12616 if (rptr == mp->b_wptr) { 12617 mblk_t *mp1; 12618 mp1 = mp->b_cont; 12619 freeb(mp); 12620 mp = mp1; 12621 rptr = mp->b_rptr; 12622 } 12623 if (MBLKL(mp) < IPV6_HDR_LEN + 12624 sizeof (tcph_t)) { 12625 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12626 freemsg(first_mp); 12627 return (NULL); 12628 } 12629 ip6h = (ip6_t *)rptr; 12630 } 12631 12632 /* 12633 * Find any potentially interesting extension headers 12634 * as well as the length of the IPv6 + extension 12635 * headers. 12636 */ 12637 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12638 /* Verify if this is a TCP packet */ 12639 if (nexthdrp != IPPROTO_TCP) { 12640 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12641 freemsg(first_mp); 12642 return (NULL); 12643 } 12644 } else { 12645 ip_hdr_len = IPV6_HDR_LEN; 12646 } 12647 } 12648 12649 done: 12650 if (ipversp != NULL) 12651 *ipversp = ipvers; 12652 if (ip_hdr_lenp != NULL) 12653 *ip_hdr_lenp = ip_hdr_len; 12654 if (ippp != NULL) 12655 *ippp = ipp; 12656 if (ifindexp != NULL) 12657 *ifindexp = ifindex; 12658 if (mctl_present) { 12659 freeb(first_mp); 12660 } 12661 return (mp); 12662 } 12663 12664 /* 12665 * Handle M_DATA messages from IP. Its called directly from IP via 12666 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12667 * in this path. 12668 * 12669 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12670 * v4 and v6), we are called through tcp_input() and a M_CTL can 12671 * be present for options but tcp_find_pktinfo() deals with it. We 12672 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12673 * 12674 * The first argument is always the connp/tcp to which the mp belongs. 12675 * There are no exceptions to this rule. The caller has already put 12676 * a reference on this connp/tcp and once tcp_rput_data() returns, 12677 * the squeue will do the refrele. 12678 * 12679 * The TH_SYN for the listener directly go to tcp_conn_request via 12680 * squeue. 12681 * 12682 * sqp: NULL = recursive, sqp != NULL means called from squeue 12683 */ 12684 void 12685 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12686 { 12687 int32_t bytes_acked; 12688 int32_t gap; 12689 mblk_t *mp1; 12690 uint_t flags; 12691 uint32_t new_swnd = 0; 12692 uchar_t *iphdr; 12693 uchar_t *rptr; 12694 int32_t rgap; 12695 uint32_t seg_ack; 12696 int seg_len; 12697 uint_t ip_hdr_len; 12698 uint32_t seg_seq; 12699 tcph_t *tcph; 12700 int urp; 12701 tcp_opt_t tcpopt; 12702 uint_t ipvers; 12703 ip6_pkt_t ipp; 12704 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12705 uint32_t cwnd; 12706 uint32_t add; 12707 int npkt; 12708 int mss; 12709 conn_t *connp = (conn_t *)arg; 12710 squeue_t *sqp = (squeue_t *)arg2; 12711 tcp_t *tcp = connp->conn_tcp; 12712 tcp_stack_t *tcps = tcp->tcp_tcps; 12713 12714 /* 12715 * RST from fused tcp loopback peer should trigger an unfuse. 12716 */ 12717 if (tcp->tcp_fused) { 12718 TCP_STAT(tcps, tcp_fusion_aborted); 12719 tcp_unfuse(tcp); 12720 } 12721 12722 iphdr = mp->b_rptr; 12723 rptr = mp->b_rptr; 12724 ASSERT(OK_32PTR(rptr)); 12725 12726 /* 12727 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12728 * processing here. For rest call tcp_find_pktinfo to fill up the 12729 * necessary information. 12730 */ 12731 if (IPCL_IS_TCP4(connp)) { 12732 ipvers = IPV4_VERSION; 12733 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12734 } else { 12735 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12736 NULL, &ipp); 12737 if (mp == NULL) { 12738 TCP_STAT(tcps, tcp_rput_v6_error); 12739 return; 12740 } 12741 iphdr = mp->b_rptr; 12742 rptr = mp->b_rptr; 12743 } 12744 ASSERT(DB_TYPE(mp) == M_DATA); 12745 ASSERT(mp->b_next == NULL); 12746 12747 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12748 seg_seq = ABE32_TO_U32(tcph->th_seq); 12749 seg_ack = ABE32_TO_U32(tcph->th_ack); 12750 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12751 seg_len = (int)(mp->b_wptr - rptr) - 12752 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12753 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12754 do { 12755 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12756 (uintptr_t)INT_MAX); 12757 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12758 } while ((mp1 = mp1->b_cont) != NULL && 12759 mp1->b_datap->db_type == M_DATA); 12760 } 12761 12762 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12763 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12764 seg_len, tcph); 12765 return; 12766 } 12767 12768 if (sqp != NULL) { 12769 /* 12770 * This is the correct place to update tcp_last_recv_time. Note 12771 * that it is also updated for tcp structure that belongs to 12772 * global and listener queues which do not really need updating. 12773 * But that should not cause any harm. And it is updated for 12774 * all kinds of incoming segments, not only for data segments. 12775 */ 12776 tcp->tcp_last_recv_time = lbolt; 12777 } 12778 12779 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12780 12781 BUMP_LOCAL(tcp->tcp_ibsegs); 12782 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 12783 12784 if ((flags & TH_URG) && sqp != NULL) { 12785 /* 12786 * TCP can't handle urgent pointers that arrive before 12787 * the connection has been accept()ed since it can't 12788 * buffer OOB data. Discard segment if this happens. 12789 * 12790 * We can't just rely on a non-null tcp_listener to indicate 12791 * that the accept() has completed since unlinking of the 12792 * eager and completion of the accept are not atomic. 12793 * tcp_detached, when it is not set (B_FALSE) indicates 12794 * that the accept() has completed. 12795 * 12796 * Nor can it reassemble urgent pointers, so discard 12797 * if it's not the next segment expected. 12798 * 12799 * Otherwise, collapse chain into one mblk (discard if 12800 * that fails). This makes sure the headers, retransmitted 12801 * data, and new data all are in the same mblk. 12802 */ 12803 ASSERT(mp != NULL); 12804 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 12805 freemsg(mp); 12806 return; 12807 } 12808 /* Update pointers into message */ 12809 iphdr = rptr = mp->b_rptr; 12810 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12811 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12812 /* 12813 * Since we can't handle any data with this urgent 12814 * pointer that is out of sequence, we expunge 12815 * the data. This allows us to still register 12816 * the urgent mark and generate the M_PCSIG, 12817 * which we can do. 12818 */ 12819 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12820 seg_len = 0; 12821 } 12822 } 12823 12824 switch (tcp->tcp_state) { 12825 case TCPS_SYN_SENT: 12826 if (flags & TH_ACK) { 12827 /* 12828 * Note that our stack cannot send data before a 12829 * connection is established, therefore the 12830 * following check is valid. Otherwise, it has 12831 * to be changed. 12832 */ 12833 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12834 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12835 freemsg(mp); 12836 if (flags & TH_RST) 12837 return; 12838 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12839 tcp, seg_ack, 0, TH_RST); 12840 return; 12841 } 12842 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12843 } 12844 if (flags & TH_RST) { 12845 freemsg(mp); 12846 if (flags & TH_ACK) 12847 (void) tcp_clean_death(tcp, 12848 ECONNREFUSED, 13); 12849 return; 12850 } 12851 if (!(flags & TH_SYN)) { 12852 freemsg(mp); 12853 return; 12854 } 12855 12856 /* Process all TCP options. */ 12857 tcp_process_options(tcp, tcph); 12858 /* 12859 * The following changes our rwnd to be a multiple of the 12860 * MIN(peer MSS, our MSS) for performance reason. 12861 */ 12862 (void) tcp_rwnd_set(tcp, 12863 MSS_ROUNDUP(tcp->tcp_recv_hiwater, tcp->tcp_mss)); 12864 12865 /* Is the other end ECN capable? */ 12866 if (tcp->tcp_ecn_ok) { 12867 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 12868 tcp->tcp_ecn_ok = B_FALSE; 12869 } 12870 } 12871 /* 12872 * Clear ECN flags because it may interfere with later 12873 * processing. 12874 */ 12875 flags &= ~(TH_ECE|TH_CWR); 12876 12877 tcp->tcp_irs = seg_seq; 12878 tcp->tcp_rack = seg_seq; 12879 tcp->tcp_rnxt = seg_seq + 1; 12880 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12881 if (!TCP_IS_DETACHED(tcp)) { 12882 /* Allocate room for SACK options if needed. */ 12883 if (tcp->tcp_snd_sack_ok) { 12884 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 12885 tcp->tcp_hdr_len + 12886 TCPOPT_MAX_SACK_LEN + 12887 (tcp->tcp_loopback ? 0 : 12888 tcps->tcps_wroff_xtra)); 12889 } else { 12890 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 12891 tcp->tcp_hdr_len + 12892 (tcp->tcp_loopback ? 0 : 12893 tcps->tcps_wroff_xtra)); 12894 } 12895 } 12896 if (flags & TH_ACK) { 12897 /* 12898 * If we can't get the confirmation upstream, pretend 12899 * we didn't even see this one. 12900 * 12901 * XXX: how can we pretend we didn't see it if we 12902 * have updated rnxt et. al. 12903 * 12904 * For loopback we defer sending up the T_CONN_CON 12905 * until after some checks below. 12906 */ 12907 mp1 = NULL; 12908 /* 12909 * tcp_sendmsg() checks tcp_state without entering 12910 * the squeue so tcp_state should be updated before 12911 * sending up connection confirmation 12912 */ 12913 tcp->tcp_state = TCPS_ESTABLISHED; 12914 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 12915 tcp->tcp_loopback ? &mp1 : NULL)) { 12916 tcp->tcp_state = TCPS_SYN_SENT; 12917 freemsg(mp); 12918 return; 12919 } 12920 /* SYN was acked - making progress */ 12921 if (tcp->tcp_ipversion == IPV6_VERSION) 12922 tcp->tcp_ip_forward_progress = B_TRUE; 12923 12924 /* One for the SYN */ 12925 tcp->tcp_suna = tcp->tcp_iss + 1; 12926 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 12927 12928 /* 12929 * If SYN was retransmitted, need to reset all 12930 * retransmission info. This is because this 12931 * segment will be treated as a dup ACK. 12932 */ 12933 if (tcp->tcp_rexmit) { 12934 tcp->tcp_rexmit = B_FALSE; 12935 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 12936 tcp->tcp_rexmit_max = tcp->tcp_snxt; 12937 tcp->tcp_snd_burst = tcp->tcp_localnet ? 12938 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 12939 tcp->tcp_ms_we_have_waited = 0; 12940 12941 /* 12942 * Set tcp_cwnd back to 1 MSS, per 12943 * recommendation from 12944 * draft-floyd-incr-init-win-01.txt, 12945 * Increasing TCP's Initial Window. 12946 */ 12947 tcp->tcp_cwnd = tcp->tcp_mss; 12948 } 12949 12950 tcp->tcp_swl1 = seg_seq; 12951 tcp->tcp_swl2 = seg_ack; 12952 12953 new_swnd = BE16_TO_U16(tcph->th_win); 12954 tcp->tcp_swnd = new_swnd; 12955 if (new_swnd > tcp->tcp_max_swnd) 12956 tcp->tcp_max_swnd = new_swnd; 12957 12958 /* 12959 * Always send the three-way handshake ack immediately 12960 * in order to make the connection complete as soon as 12961 * possible on the accepting host. 12962 */ 12963 flags |= TH_ACK_NEEDED; 12964 12965 /* 12966 * Special case for loopback. At this point we have 12967 * received SYN-ACK from the remote endpoint. In 12968 * order to ensure that both endpoints reach the 12969 * fused state prior to any data exchange, the final 12970 * ACK needs to be sent before we indicate T_CONN_CON 12971 * to the module upstream. 12972 */ 12973 if (tcp->tcp_loopback) { 12974 mblk_t *ack_mp; 12975 12976 ASSERT(!tcp->tcp_unfusable); 12977 ASSERT(mp1 != NULL); 12978 /* 12979 * For loopback, we always get a pure SYN-ACK 12980 * and only need to send back the final ACK 12981 * with no data (this is because the other 12982 * tcp is ours and we don't do T/TCP). This 12983 * final ACK triggers the passive side to 12984 * perform fusion in ESTABLISHED state. 12985 */ 12986 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 12987 if (tcp->tcp_ack_tid != 0) { 12988 (void) TCP_TIMER_CANCEL(tcp, 12989 tcp->tcp_ack_tid); 12990 tcp->tcp_ack_tid = 0; 12991 } 12992 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 12993 BUMP_LOCAL(tcp->tcp_obsegs); 12994 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 12995 12996 if (!IPCL_IS_NONSTR(connp)) { 12997 /* Send up T_CONN_CON */ 12998 putnext(tcp->tcp_rq, mp1); 12999 } else { 13000 cred_t *cr; 13001 pid_t cpid; 13002 13003 cr = msg_getcred(mp1, &cpid); 13004 (*connp->conn_upcalls-> 13005 su_connected) 13006 (connp->conn_upper_handle, 13007 tcp->tcp_connid, cr, cpid); 13008 freemsg(mp1); 13009 } 13010 13011 freemsg(mp); 13012 return; 13013 } 13014 /* 13015 * Forget fusion; we need to handle more 13016 * complex cases below. Send the deferred 13017 * T_CONN_CON message upstream and proceed 13018 * as usual. Mark this tcp as not capable 13019 * of fusion. 13020 */ 13021 TCP_STAT(tcps, tcp_fusion_unfusable); 13022 tcp->tcp_unfusable = B_TRUE; 13023 if (!IPCL_IS_NONSTR(connp)) { 13024 putnext(tcp->tcp_rq, mp1); 13025 } else { 13026 cred_t *cr; 13027 pid_t cpid; 13028 13029 cr = msg_getcred(mp1, &cpid); 13030 (*connp->conn_upcalls->su_connected) 13031 (connp->conn_upper_handle, 13032 tcp->tcp_connid, cr, cpid); 13033 freemsg(mp1); 13034 } 13035 } 13036 13037 /* 13038 * Check to see if there is data to be sent. If 13039 * yes, set the transmit flag. Then check to see 13040 * if received data processing needs to be done. 13041 * If not, go straight to xmit_check. This short 13042 * cut is OK as we don't support T/TCP. 13043 */ 13044 if (tcp->tcp_unsent) 13045 flags |= TH_XMIT_NEEDED; 13046 13047 if (seg_len == 0 && !(flags & TH_URG)) { 13048 freemsg(mp); 13049 goto xmit_check; 13050 } 13051 13052 flags &= ~TH_SYN; 13053 seg_seq++; 13054 break; 13055 } 13056 tcp->tcp_state = TCPS_SYN_RCVD; 13057 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13058 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13059 if (mp1) { 13060 /* 13061 * See comment in tcp_conn_request() for why we use 13062 * the open() time pid here. 13063 */ 13064 DB_CPID(mp1) = tcp->tcp_cpid; 13065 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13066 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13067 } 13068 freemsg(mp); 13069 return; 13070 case TCPS_SYN_RCVD: 13071 if (flags & TH_ACK) { 13072 /* 13073 * In this state, a SYN|ACK packet is either bogus 13074 * because the other side must be ACKing our SYN which 13075 * indicates it has seen the ACK for their SYN and 13076 * shouldn't retransmit it or we're crossing SYNs 13077 * on active open. 13078 */ 13079 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13080 freemsg(mp); 13081 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13082 tcp, seg_ack, 0, TH_RST); 13083 return; 13084 } 13085 /* 13086 * NOTE: RFC 793 pg. 72 says this should be 13087 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13088 * but that would mean we have an ack that ignored 13089 * our SYN. 13090 */ 13091 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13092 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13093 freemsg(mp); 13094 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13095 tcp, seg_ack, 0, TH_RST); 13096 return; 13097 } 13098 } 13099 break; 13100 case TCPS_LISTEN: 13101 /* 13102 * Only a TLI listener can come through this path when a 13103 * acceptor is going back to be a listener and a packet 13104 * for the acceptor hits the classifier. For a socket 13105 * listener, this can never happen because a listener 13106 * can never accept connection on itself and hence a 13107 * socket acceptor can not go back to being a listener. 13108 */ 13109 ASSERT(!TCP_IS_SOCKET(tcp)); 13110 /*FALLTHRU*/ 13111 case TCPS_CLOSED: 13112 case TCPS_BOUND: { 13113 conn_t *new_connp; 13114 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13115 13116 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13117 if (new_connp != NULL) { 13118 tcp_reinput(new_connp, mp, connp->conn_sqp); 13119 return; 13120 } 13121 /* We failed to classify. For now just drop the packet */ 13122 freemsg(mp); 13123 return; 13124 } 13125 case TCPS_IDLE: 13126 /* 13127 * Handle the case where the tcp_clean_death() has happened 13128 * on a connection (application hasn't closed yet) but a packet 13129 * was already queued on squeue before tcp_clean_death() 13130 * was processed. Calling tcp_clean_death() twice on same 13131 * connection can result in weird behaviour. 13132 */ 13133 freemsg(mp); 13134 return; 13135 default: 13136 break; 13137 } 13138 13139 /* 13140 * Already on the correct queue/perimeter. 13141 * If this is a detached connection and not an eager 13142 * connection hanging off a listener then new data 13143 * (past the FIN) will cause a reset. 13144 * We do a special check here where it 13145 * is out of the main line, rather than check 13146 * if we are detached every time we see new 13147 * data down below. 13148 */ 13149 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13150 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13151 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13152 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13153 13154 freemsg(mp); 13155 /* 13156 * This could be an SSL closure alert. We're detached so just 13157 * acknowledge it this last time. 13158 */ 13159 if (tcp->tcp_kssl_ctx != NULL) { 13160 kssl_release_ctx(tcp->tcp_kssl_ctx); 13161 tcp->tcp_kssl_ctx = NULL; 13162 13163 tcp->tcp_rnxt += seg_len; 13164 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13165 flags |= TH_ACK_NEEDED; 13166 goto ack_check; 13167 } 13168 13169 tcp_xmit_ctl("new data when detached", tcp, 13170 tcp->tcp_snxt, 0, TH_RST); 13171 (void) tcp_clean_death(tcp, EPROTO, 12); 13172 return; 13173 } 13174 13175 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13176 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13177 new_swnd = BE16_TO_U16(tcph->th_win) << 13178 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13179 13180 if (tcp->tcp_snd_ts_ok) { 13181 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13182 /* 13183 * This segment is not acceptable. 13184 * Drop it and send back an ACK. 13185 */ 13186 freemsg(mp); 13187 flags |= TH_ACK_NEEDED; 13188 goto ack_check; 13189 } 13190 } else if (tcp->tcp_snd_sack_ok) { 13191 ASSERT(tcp->tcp_sack_info != NULL); 13192 tcpopt.tcp = tcp; 13193 /* 13194 * SACK info in already updated in tcp_parse_options. Ignore 13195 * all other TCP options... 13196 */ 13197 (void) tcp_parse_options(tcph, &tcpopt); 13198 } 13199 try_again:; 13200 mss = tcp->tcp_mss; 13201 gap = seg_seq - tcp->tcp_rnxt; 13202 rgap = tcp->tcp_rwnd - (gap + seg_len); 13203 /* 13204 * gap is the amount of sequence space between what we expect to see 13205 * and what we got for seg_seq. A positive value for gap means 13206 * something got lost. A negative value means we got some old stuff. 13207 */ 13208 if (gap < 0) { 13209 /* Old stuff present. Is the SYN in there? */ 13210 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13211 (seg_len != 0)) { 13212 flags &= ~TH_SYN; 13213 seg_seq++; 13214 urp--; 13215 /* Recompute the gaps after noting the SYN. */ 13216 goto try_again; 13217 } 13218 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13219 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13220 (seg_len > -gap ? -gap : seg_len)); 13221 /* Remove the old stuff from seg_len. */ 13222 seg_len += gap; 13223 /* 13224 * Anything left? 13225 * Make sure to check for unack'd FIN when rest of data 13226 * has been previously ack'd. 13227 */ 13228 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13229 /* 13230 * Resets are only valid if they lie within our offered 13231 * window. If the RST bit is set, we just ignore this 13232 * segment. 13233 */ 13234 if (flags & TH_RST) { 13235 freemsg(mp); 13236 return; 13237 } 13238 13239 /* 13240 * The arriving of dup data packets indicate that we 13241 * may have postponed an ack for too long, or the other 13242 * side's RTT estimate is out of shape. Start acking 13243 * more often. 13244 */ 13245 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13246 tcp->tcp_rack_cnt >= 1 && 13247 tcp->tcp_rack_abs_max > 2) { 13248 tcp->tcp_rack_abs_max--; 13249 } 13250 tcp->tcp_rack_cur_max = 1; 13251 13252 /* 13253 * This segment is "unacceptable". None of its 13254 * sequence space lies within our advertized window. 13255 * 13256 * Adjust seg_len to the original value for tracing. 13257 */ 13258 seg_len -= gap; 13259 if (tcp->tcp_debug) { 13260 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13261 "tcp_rput: unacceptable, gap %d, rgap %d, " 13262 "flags 0x%x, seg_seq %u, seg_ack %u, " 13263 "seg_len %d, rnxt %u, snxt %u, %s", 13264 gap, rgap, flags, seg_seq, seg_ack, 13265 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13266 tcp_display(tcp, NULL, 13267 DISP_ADDR_AND_PORT)); 13268 } 13269 13270 /* 13271 * Arrange to send an ACK in response to the 13272 * unacceptable segment per RFC 793 page 69. There 13273 * is only one small difference between ours and the 13274 * acceptability test in the RFC - we accept ACK-only 13275 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13276 * will be generated. 13277 * 13278 * Note that we have to ACK an ACK-only packet at least 13279 * for stacks that send 0-length keep-alives with 13280 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13281 * section 4.2.3.6. As long as we don't ever generate 13282 * an unacceptable packet in response to an incoming 13283 * packet that is unacceptable, it should not cause 13284 * "ACK wars". 13285 */ 13286 flags |= TH_ACK_NEEDED; 13287 13288 /* 13289 * Continue processing this segment in order to use the 13290 * ACK information it contains, but skip all other 13291 * sequence-number processing. Processing the ACK 13292 * information is necessary in order to 13293 * re-synchronize connections that may have lost 13294 * synchronization. 13295 * 13296 * We clear seg_len and flag fields related to 13297 * sequence number processing as they are not 13298 * to be trusted for an unacceptable segment. 13299 */ 13300 seg_len = 0; 13301 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13302 goto process_ack; 13303 } 13304 13305 /* Fix seg_seq, and chew the gap off the front. */ 13306 seg_seq = tcp->tcp_rnxt; 13307 urp += gap; 13308 do { 13309 mblk_t *mp2; 13310 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13311 (uintptr_t)UINT_MAX); 13312 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13313 if (gap > 0) { 13314 mp->b_rptr = mp->b_wptr - gap; 13315 break; 13316 } 13317 mp2 = mp; 13318 mp = mp->b_cont; 13319 freeb(mp2); 13320 } while (gap < 0); 13321 /* 13322 * If the urgent data has already been acknowledged, we 13323 * should ignore TH_URG below 13324 */ 13325 if (urp < 0) 13326 flags &= ~TH_URG; 13327 } 13328 /* 13329 * rgap is the amount of stuff received out of window. A negative 13330 * value is the amount out of window. 13331 */ 13332 if (rgap < 0) { 13333 mblk_t *mp2; 13334 13335 if (tcp->tcp_rwnd == 0) { 13336 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13337 } else { 13338 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13339 UPDATE_MIB(&tcps->tcps_mib, 13340 tcpInDataPastWinBytes, -rgap); 13341 } 13342 13343 /* 13344 * seg_len does not include the FIN, so if more than 13345 * just the FIN is out of window, we act like we don't 13346 * see it. (If just the FIN is out of window, rgap 13347 * will be zero and we will go ahead and acknowledge 13348 * the FIN.) 13349 */ 13350 flags &= ~TH_FIN; 13351 13352 /* Fix seg_len and make sure there is something left. */ 13353 seg_len += rgap; 13354 if (seg_len <= 0) { 13355 /* 13356 * Resets are only valid if they lie within our offered 13357 * window. If the RST bit is set, we just ignore this 13358 * segment. 13359 */ 13360 if (flags & TH_RST) { 13361 freemsg(mp); 13362 return; 13363 } 13364 13365 /* Per RFC 793, we need to send back an ACK. */ 13366 flags |= TH_ACK_NEEDED; 13367 13368 /* 13369 * Send SIGURG as soon as possible i.e. even 13370 * if the TH_URG was delivered in a window probe 13371 * packet (which will be unacceptable). 13372 * 13373 * We generate a signal if none has been generated 13374 * for this connection or if this is a new urgent 13375 * byte. Also send a zero-length "unmarked" message 13376 * to inform SIOCATMARK that this is not the mark. 13377 * 13378 * tcp_urp_last_valid is cleared when the T_exdata_ind 13379 * is sent up. This plus the check for old data 13380 * (gap >= 0) handles the wraparound of the sequence 13381 * number space without having to always track the 13382 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13383 * this max in its rcv_up variable). 13384 * 13385 * This prevents duplicate SIGURGS due to a "late" 13386 * zero-window probe when the T_EXDATA_IND has already 13387 * been sent up. 13388 */ 13389 if ((flags & TH_URG) && 13390 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13391 tcp->tcp_urp_last))) { 13392 if (IPCL_IS_NONSTR(connp)) { 13393 if (!TCP_IS_DETACHED(tcp)) { 13394 (*connp->conn_upcalls-> 13395 su_signal_oob) 13396 (connp->conn_upper_handle, 13397 urp); 13398 } 13399 } else { 13400 mp1 = allocb(0, BPRI_MED); 13401 if (mp1 == NULL) { 13402 freemsg(mp); 13403 return; 13404 } 13405 if (!TCP_IS_DETACHED(tcp) && 13406 !putnextctl1(tcp->tcp_rq, 13407 M_PCSIG, SIGURG)) { 13408 /* Try again on the rexmit. */ 13409 freemsg(mp1); 13410 freemsg(mp); 13411 return; 13412 } 13413 /* 13414 * If the next byte would be the mark 13415 * then mark with MARKNEXT else mark 13416 * with NOTMARKNEXT. 13417 */ 13418 if (gap == 0 && urp == 0) 13419 mp1->b_flag |= MSGMARKNEXT; 13420 else 13421 mp1->b_flag |= MSGNOTMARKNEXT; 13422 freemsg(tcp->tcp_urp_mark_mp); 13423 tcp->tcp_urp_mark_mp = mp1; 13424 flags |= TH_SEND_URP_MARK; 13425 } 13426 tcp->tcp_urp_last_valid = B_TRUE; 13427 tcp->tcp_urp_last = urp + seg_seq; 13428 } 13429 /* 13430 * If this is a zero window probe, continue to 13431 * process the ACK part. But we need to set seg_len 13432 * to 0 to avoid data processing. Otherwise just 13433 * drop the segment and send back an ACK. 13434 */ 13435 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13436 flags &= ~(TH_SYN | TH_URG); 13437 seg_len = 0; 13438 goto process_ack; 13439 } else { 13440 freemsg(mp); 13441 goto ack_check; 13442 } 13443 } 13444 /* Pitch out of window stuff off the end. */ 13445 rgap = seg_len; 13446 mp2 = mp; 13447 do { 13448 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13449 (uintptr_t)INT_MAX); 13450 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13451 if (rgap < 0) { 13452 mp2->b_wptr += rgap; 13453 if ((mp1 = mp2->b_cont) != NULL) { 13454 mp2->b_cont = NULL; 13455 freemsg(mp1); 13456 } 13457 break; 13458 } 13459 } while ((mp2 = mp2->b_cont) != NULL); 13460 } 13461 ok:; 13462 /* 13463 * TCP should check ECN info for segments inside the window only. 13464 * Therefore the check should be done here. 13465 */ 13466 if (tcp->tcp_ecn_ok) { 13467 if (flags & TH_CWR) { 13468 tcp->tcp_ecn_echo_on = B_FALSE; 13469 } 13470 /* 13471 * Note that both ECN_CE and CWR can be set in the 13472 * same segment. In this case, we once again turn 13473 * on ECN_ECHO. 13474 */ 13475 if (tcp->tcp_ipversion == IPV4_VERSION) { 13476 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13477 13478 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13479 tcp->tcp_ecn_echo_on = B_TRUE; 13480 } 13481 } else { 13482 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13483 13484 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13485 htonl(IPH_ECN_CE << 20)) { 13486 tcp->tcp_ecn_echo_on = B_TRUE; 13487 } 13488 } 13489 } 13490 13491 /* 13492 * Check whether we can update tcp_ts_recent. This test is 13493 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13494 * Extensions for High Performance: An Update", Internet Draft. 13495 */ 13496 if (tcp->tcp_snd_ts_ok && 13497 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13498 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13499 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13500 tcp->tcp_last_rcv_lbolt = lbolt64; 13501 } 13502 13503 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13504 /* 13505 * FIN in an out of order segment. We record this in 13506 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13507 * Clear the FIN so that any check on FIN flag will fail. 13508 * Remember that FIN also counts in the sequence number 13509 * space. So we need to ack out of order FIN only segments. 13510 */ 13511 if (flags & TH_FIN) { 13512 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13513 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13514 flags &= ~TH_FIN; 13515 flags |= TH_ACK_NEEDED; 13516 } 13517 if (seg_len > 0) { 13518 /* Fill in the SACK blk list. */ 13519 if (tcp->tcp_snd_sack_ok) { 13520 ASSERT(tcp->tcp_sack_info != NULL); 13521 tcp_sack_insert(tcp->tcp_sack_list, 13522 seg_seq, seg_seq + seg_len, 13523 &(tcp->tcp_num_sack_blk)); 13524 } 13525 13526 /* 13527 * Attempt reassembly and see if we have something 13528 * ready to go. 13529 */ 13530 mp = tcp_reass(tcp, mp, seg_seq); 13531 /* Always ack out of order packets */ 13532 flags |= TH_ACK_NEEDED | TH_PUSH; 13533 if (mp) { 13534 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13535 (uintptr_t)INT_MAX); 13536 seg_len = mp->b_cont ? msgdsize(mp) : 13537 (int)(mp->b_wptr - mp->b_rptr); 13538 seg_seq = tcp->tcp_rnxt; 13539 /* 13540 * A gap is filled and the seq num and len 13541 * of the gap match that of a previously 13542 * received FIN, put the FIN flag back in. 13543 */ 13544 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13545 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13546 flags |= TH_FIN; 13547 tcp->tcp_valid_bits &= 13548 ~TCP_OFO_FIN_VALID; 13549 } 13550 } else { 13551 /* 13552 * Keep going even with NULL mp. 13553 * There may be a useful ACK or something else 13554 * we don't want to miss. 13555 * 13556 * But TCP should not perform fast retransmit 13557 * because of the ack number. TCP uses 13558 * seg_len == 0 to determine if it is a pure 13559 * ACK. And this is not a pure ACK. 13560 */ 13561 seg_len = 0; 13562 ofo_seg = B_TRUE; 13563 } 13564 } 13565 } else if (seg_len > 0) { 13566 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13567 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13568 /* 13569 * If an out of order FIN was received before, and the seq 13570 * num and len of the new segment match that of the FIN, 13571 * put the FIN flag back in. 13572 */ 13573 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13574 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13575 flags |= TH_FIN; 13576 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13577 } 13578 } 13579 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13580 if (flags & TH_RST) { 13581 freemsg(mp); 13582 switch (tcp->tcp_state) { 13583 case TCPS_SYN_RCVD: 13584 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13585 break; 13586 case TCPS_ESTABLISHED: 13587 case TCPS_FIN_WAIT_1: 13588 case TCPS_FIN_WAIT_2: 13589 case TCPS_CLOSE_WAIT: 13590 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13591 break; 13592 case TCPS_CLOSING: 13593 case TCPS_LAST_ACK: 13594 (void) tcp_clean_death(tcp, 0, 16); 13595 break; 13596 default: 13597 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13598 (void) tcp_clean_death(tcp, ENXIO, 17); 13599 break; 13600 } 13601 return; 13602 } 13603 if (flags & TH_SYN) { 13604 /* 13605 * See RFC 793, Page 71 13606 * 13607 * The seq number must be in the window as it should 13608 * be "fixed" above. If it is outside window, it should 13609 * be already rejected. Note that we allow seg_seq to be 13610 * rnxt + rwnd because we want to accept 0 window probe. 13611 */ 13612 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13613 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13614 freemsg(mp); 13615 /* 13616 * If the ACK flag is not set, just use our snxt as the 13617 * seq number of the RST segment. 13618 */ 13619 if (!(flags & TH_ACK)) { 13620 seg_ack = tcp->tcp_snxt; 13621 } 13622 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13623 TH_RST|TH_ACK); 13624 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13625 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13626 return; 13627 } 13628 /* 13629 * urp could be -1 when the urp field in the packet is 0 13630 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13631 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13632 */ 13633 if (flags & TH_URG && urp >= 0) { 13634 if (!tcp->tcp_urp_last_valid || 13635 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13636 /* 13637 * Non-STREAMS sockets handle the urgent data a litte 13638 * differently from STREAMS based sockets. There is no 13639 * need to mark any mblks with the MSG{NOT,}MARKNEXT 13640 * flags to keep SIOCATMARK happy. Instead a 13641 * su_signal_oob upcall is made to update the mark. 13642 * Neither is a T_EXDATA_IND mblk needed to be 13643 * prepended to the urgent data. The urgent data is 13644 * delivered using the su_recv upcall, where we set 13645 * the MSG_OOB flag to indicate that it is urg data. 13646 * 13647 * Neither TH_SEND_URP_MARK nor TH_MARKNEXT_NEEDED 13648 * are used by non-STREAMS sockets. 13649 */ 13650 if (IPCL_IS_NONSTR(connp)) { 13651 if (!TCP_IS_DETACHED(tcp)) { 13652 (*connp->conn_upcalls->su_signal_oob) 13653 (connp->conn_upper_handle, urp); 13654 } 13655 } else { 13656 /* 13657 * If we haven't generated the signal yet for 13658 * this urgent pointer value, do it now. Also, 13659 * send up a zero-length M_DATA indicating 13660 * whether or not this is the mark. The latter 13661 * is not needed when a T_EXDATA_IND is sent up. 13662 * However, if there are allocation failures 13663 * this code relies on the sender retransmitting 13664 * and the socket code for determining the mark 13665 * should not block waiting for the peer to 13666 * transmit. Thus, for simplicity we always 13667 * send up the mark indication. 13668 */ 13669 mp1 = allocb(0, BPRI_MED); 13670 if (mp1 == NULL) { 13671 freemsg(mp); 13672 return; 13673 } 13674 if (!TCP_IS_DETACHED(tcp) && 13675 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13676 SIGURG)) { 13677 /* Try again on the rexmit. */ 13678 freemsg(mp1); 13679 freemsg(mp); 13680 return; 13681 } 13682 /* 13683 * Mark with NOTMARKNEXT for now. 13684 * The code below will change this to MARKNEXT 13685 * if we are at the mark. 13686 * 13687 * If there are allocation failures (e.g. in 13688 * dupmsg below) the next time tcp_rput_data 13689 * sees the urgent segment it will send up the 13690 * MSGMARKNEXT message. 13691 */ 13692 mp1->b_flag |= MSGNOTMARKNEXT; 13693 freemsg(tcp->tcp_urp_mark_mp); 13694 tcp->tcp_urp_mark_mp = mp1; 13695 flags |= TH_SEND_URP_MARK; 13696 #ifdef DEBUG 13697 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13698 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13699 "last %x, %s", 13700 seg_seq, urp, tcp->tcp_urp_last, 13701 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13702 #endif /* DEBUG */ 13703 } 13704 tcp->tcp_urp_last_valid = B_TRUE; 13705 tcp->tcp_urp_last = urp + seg_seq; 13706 } else if (tcp->tcp_urp_mark_mp != NULL) { 13707 /* 13708 * An allocation failure prevented the previous 13709 * tcp_rput_data from sending up the allocated 13710 * MSG*MARKNEXT message - send it up this time 13711 * around. 13712 */ 13713 flags |= TH_SEND_URP_MARK; 13714 } 13715 13716 /* 13717 * If the urgent byte is in this segment, make sure that it is 13718 * all by itself. This makes it much easier to deal with the 13719 * possibility of an allocation failure on the T_exdata_ind. 13720 * Note that seg_len is the number of bytes in the segment, and 13721 * urp is the offset into the segment of the urgent byte. 13722 * urp < seg_len means that the urgent byte is in this segment. 13723 */ 13724 if (urp < seg_len) { 13725 if (seg_len != 1) { 13726 uint32_t tmp_rnxt; 13727 /* 13728 * Break it up and feed it back in. 13729 * Re-attach the IP header. 13730 */ 13731 mp->b_rptr = iphdr; 13732 if (urp > 0) { 13733 /* 13734 * There is stuff before the urgent 13735 * byte. 13736 */ 13737 mp1 = dupmsg(mp); 13738 if (!mp1) { 13739 /* 13740 * Trim from urgent byte on. 13741 * The rest will come back. 13742 */ 13743 (void) adjmsg(mp, 13744 urp - seg_len); 13745 tcp_rput_data(connp, 13746 mp, NULL); 13747 return; 13748 } 13749 (void) adjmsg(mp1, urp - seg_len); 13750 /* Feed this piece back in. */ 13751 tmp_rnxt = tcp->tcp_rnxt; 13752 tcp_rput_data(connp, mp1, NULL); 13753 /* 13754 * If the data passed back in was not 13755 * processed (ie: bad ACK) sending 13756 * the remainder back in will cause a 13757 * loop. In this case, drop the 13758 * packet and let the sender try 13759 * sending a good packet. 13760 */ 13761 if (tmp_rnxt == tcp->tcp_rnxt) { 13762 freemsg(mp); 13763 return; 13764 } 13765 } 13766 if (urp != seg_len - 1) { 13767 uint32_t tmp_rnxt; 13768 /* 13769 * There is stuff after the urgent 13770 * byte. 13771 */ 13772 mp1 = dupmsg(mp); 13773 if (!mp1) { 13774 /* 13775 * Trim everything beyond the 13776 * urgent byte. The rest will 13777 * come back. 13778 */ 13779 (void) adjmsg(mp, 13780 urp + 1 - seg_len); 13781 tcp_rput_data(connp, 13782 mp, NULL); 13783 return; 13784 } 13785 (void) adjmsg(mp1, urp + 1 - seg_len); 13786 tmp_rnxt = tcp->tcp_rnxt; 13787 tcp_rput_data(connp, mp1, NULL); 13788 /* 13789 * If the data passed back in was not 13790 * processed (ie: bad ACK) sending 13791 * the remainder back in will cause a 13792 * loop. In this case, drop the 13793 * packet and let the sender try 13794 * sending a good packet. 13795 */ 13796 if (tmp_rnxt == tcp->tcp_rnxt) { 13797 freemsg(mp); 13798 return; 13799 } 13800 } 13801 tcp_rput_data(connp, mp, NULL); 13802 return; 13803 } 13804 /* 13805 * This segment contains only the urgent byte. We 13806 * have to allocate the T_exdata_ind, if we can. 13807 */ 13808 if (IPCL_IS_NONSTR(connp)) { 13809 int error; 13810 13811 (*connp->conn_upcalls->su_recv) 13812 (connp->conn_upper_handle, mp, seg_len, 13813 MSG_OOB, &error, NULL); 13814 /* 13815 * We should never be in middle of a 13816 * fallback, the squeue guarantees that. 13817 */ 13818 ASSERT(error != EOPNOTSUPP); 13819 mp = NULL; 13820 goto update_ack; 13821 } else if (!tcp->tcp_urp_mp) { 13822 struct T_exdata_ind *tei; 13823 mp1 = allocb(sizeof (struct T_exdata_ind), 13824 BPRI_MED); 13825 if (!mp1) { 13826 /* 13827 * Sigh... It'll be back. 13828 * Generate any MSG*MARK message now. 13829 */ 13830 freemsg(mp); 13831 seg_len = 0; 13832 if (flags & TH_SEND_URP_MARK) { 13833 13834 13835 ASSERT(tcp->tcp_urp_mark_mp); 13836 tcp->tcp_urp_mark_mp->b_flag &= 13837 ~MSGNOTMARKNEXT; 13838 tcp->tcp_urp_mark_mp->b_flag |= 13839 MSGMARKNEXT; 13840 } 13841 goto ack_check; 13842 } 13843 mp1->b_datap->db_type = M_PROTO; 13844 tei = (struct T_exdata_ind *)mp1->b_rptr; 13845 tei->PRIM_type = T_EXDATA_IND; 13846 tei->MORE_flag = 0; 13847 mp1->b_wptr = (uchar_t *)&tei[1]; 13848 tcp->tcp_urp_mp = mp1; 13849 #ifdef DEBUG 13850 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13851 "tcp_rput: allocated exdata_ind %s", 13852 tcp_display(tcp, NULL, 13853 DISP_PORT_ONLY)); 13854 #endif /* DEBUG */ 13855 /* 13856 * There is no need to send a separate MSG*MARK 13857 * message since the T_EXDATA_IND will be sent 13858 * now. 13859 */ 13860 flags &= ~TH_SEND_URP_MARK; 13861 freemsg(tcp->tcp_urp_mark_mp); 13862 tcp->tcp_urp_mark_mp = NULL; 13863 } 13864 /* 13865 * Now we are all set. On the next putnext upstream, 13866 * tcp_urp_mp will be non-NULL and will get prepended 13867 * to what has to be this piece containing the urgent 13868 * byte. If for any reason we abort this segment below, 13869 * if it comes back, we will have this ready, or it 13870 * will get blown off in close. 13871 */ 13872 } else if (urp == seg_len) { 13873 /* 13874 * The urgent byte is the next byte after this sequence 13875 * number. If this endpoint is non-STREAMS, then there 13876 * is nothing to do here since the socket has already 13877 * been notified about the urg pointer by the 13878 * su_signal_oob call above. 13879 * 13880 * In case of STREAMS, some more work might be needed. 13881 * If there is data it is marked with MSGMARKNEXT and 13882 * and any tcp_urp_mark_mp is discarded since it is not 13883 * needed. Otherwise, if the code above just allocated 13884 * a zero-length tcp_urp_mark_mp message, that message 13885 * is tagged with MSGMARKNEXT. Sending up these 13886 * MSGMARKNEXT messages makes SIOCATMARK work correctly 13887 * even though the T_EXDATA_IND will not be sent up 13888 * until the urgent byte arrives. 13889 */ 13890 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) { 13891 if (seg_len != 0) { 13892 flags |= TH_MARKNEXT_NEEDED; 13893 freemsg(tcp->tcp_urp_mark_mp); 13894 tcp->tcp_urp_mark_mp = NULL; 13895 flags &= ~TH_SEND_URP_MARK; 13896 } else if (tcp->tcp_urp_mark_mp != NULL) { 13897 flags |= TH_SEND_URP_MARK; 13898 tcp->tcp_urp_mark_mp->b_flag &= 13899 ~MSGNOTMARKNEXT; 13900 tcp->tcp_urp_mark_mp->b_flag |= 13901 MSGMARKNEXT; 13902 } 13903 } 13904 #ifdef DEBUG 13905 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13906 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13907 seg_len, flags, 13908 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13909 #endif /* DEBUG */ 13910 } 13911 #ifdef DEBUG 13912 else { 13913 /* Data left until we hit mark */ 13914 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13915 "tcp_rput: URP %d bytes left, %s", 13916 urp - seg_len, tcp_display(tcp, NULL, 13917 DISP_PORT_ONLY)); 13918 } 13919 #endif /* DEBUG */ 13920 } 13921 13922 process_ack: 13923 if (!(flags & TH_ACK)) { 13924 freemsg(mp); 13925 goto xmit_check; 13926 } 13927 } 13928 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13929 13930 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13931 tcp->tcp_ip_forward_progress = B_TRUE; 13932 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13933 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13934 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13935 /* 3-way handshake complete - pass up the T_CONN_IND */ 13936 tcp_t *listener = tcp->tcp_listener; 13937 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13938 13939 tcp->tcp_tconnind_started = B_TRUE; 13940 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13941 /* 13942 * We are here means eager is fine but it can 13943 * get a TH_RST at any point between now and till 13944 * accept completes and disappear. We need to 13945 * ensure that reference to eager is valid after 13946 * we get out of eager's perimeter. So we do 13947 * an extra refhold. 13948 */ 13949 CONN_INC_REF(connp); 13950 13951 /* 13952 * The listener also exists because of the refhold 13953 * done in tcp_conn_request. Its possible that it 13954 * might have closed. We will check that once we 13955 * get inside listeners context. 13956 */ 13957 CONN_INC_REF(listener->tcp_connp); 13958 if (listener->tcp_connp->conn_sqp == 13959 connp->conn_sqp) { 13960 /* 13961 * We optimize by not calling an SQUEUE_ENTER 13962 * on the listener since we know that the 13963 * listener and eager squeues are the same. 13964 * We are able to make this check safely only 13965 * because neither the eager nor the listener 13966 * can change its squeue. Only an active connect 13967 * can change its squeue 13968 */ 13969 tcp_send_conn_ind(listener->tcp_connp, mp, 13970 listener->tcp_connp->conn_sqp); 13971 CONN_DEC_REF(listener->tcp_connp); 13972 } else if (!tcp->tcp_loopback) { 13973 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 13974 mp, tcp_send_conn_ind, 13975 listener->tcp_connp, SQ_FILL, 13976 SQTAG_TCP_CONN_IND); 13977 } else { 13978 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 13979 mp, tcp_send_conn_ind, 13980 listener->tcp_connp, SQ_PROCESS, 13981 SQTAG_TCP_CONN_IND); 13982 } 13983 } 13984 13985 /* 13986 * We are seeing the final ack in the three way 13987 * hand shake of a active open'ed connection 13988 * so we must send up a T_CONN_CON 13989 * 13990 * tcp_sendmsg() checks tcp_state without entering 13991 * the squeue so tcp_state should be updated before 13992 * sending up connection confirmation. 13993 */ 13994 tcp->tcp_state = TCPS_ESTABLISHED; 13995 if (tcp->tcp_active_open) { 13996 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 13997 freemsg(mp); 13998 tcp->tcp_state = TCPS_SYN_RCVD; 13999 return; 14000 } 14001 /* 14002 * Don't fuse the loopback endpoints for 14003 * simultaneous active opens. 14004 */ 14005 if (tcp->tcp_loopback) { 14006 TCP_STAT(tcps, tcp_fusion_unfusable); 14007 tcp->tcp_unfusable = B_TRUE; 14008 } 14009 } 14010 14011 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14012 bytes_acked--; 14013 /* SYN was acked - making progress */ 14014 if (tcp->tcp_ipversion == IPV6_VERSION) 14015 tcp->tcp_ip_forward_progress = B_TRUE; 14016 14017 /* 14018 * If SYN was retransmitted, need to reset all 14019 * retransmission info as this segment will be 14020 * treated as a dup ACK. 14021 */ 14022 if (tcp->tcp_rexmit) { 14023 tcp->tcp_rexmit = B_FALSE; 14024 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14025 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14026 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14027 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14028 tcp->tcp_ms_we_have_waited = 0; 14029 tcp->tcp_cwnd = mss; 14030 } 14031 14032 /* 14033 * We set the send window to zero here. 14034 * This is needed if there is data to be 14035 * processed already on the queue. 14036 * Later (at swnd_update label), the 14037 * "new_swnd > tcp_swnd" condition is satisfied 14038 * the XMIT_NEEDED flag is set in the current 14039 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14040 * called if there is already data on queue in 14041 * this state. 14042 */ 14043 tcp->tcp_swnd = 0; 14044 14045 if (new_swnd > tcp->tcp_max_swnd) 14046 tcp->tcp_max_swnd = new_swnd; 14047 tcp->tcp_swl1 = seg_seq; 14048 tcp->tcp_swl2 = seg_ack; 14049 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14050 14051 /* Fuse when both sides are in ESTABLISHED state */ 14052 if (tcp->tcp_loopback && do_tcp_fusion) 14053 tcp_fuse(tcp, iphdr, tcph); 14054 14055 } 14056 /* This code follows 4.4BSD-Lite2 mostly. */ 14057 if (bytes_acked < 0) 14058 goto est; 14059 14060 /* 14061 * If TCP is ECN capable and the congestion experience bit is 14062 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14063 * done once per window (or more loosely, per RTT). 14064 */ 14065 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14066 tcp->tcp_cwr = B_FALSE; 14067 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14068 if (!tcp->tcp_cwr) { 14069 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14070 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14071 tcp->tcp_cwnd = npkt * mss; 14072 /* 14073 * If the cwnd is 0, use the timer to clock out 14074 * new segments. This is required by the ECN spec. 14075 */ 14076 if (npkt == 0) { 14077 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14078 /* 14079 * This makes sure that when the ACK comes 14080 * back, we will increase tcp_cwnd by 1 MSS. 14081 */ 14082 tcp->tcp_cwnd_cnt = 0; 14083 } 14084 tcp->tcp_cwr = B_TRUE; 14085 /* 14086 * This marks the end of the current window of in 14087 * flight data. That is why we don't use 14088 * tcp_suna + tcp_swnd. Only data in flight can 14089 * provide ECN info. 14090 */ 14091 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14092 tcp->tcp_ecn_cwr_sent = B_FALSE; 14093 } 14094 } 14095 14096 mp1 = tcp->tcp_xmit_head; 14097 if (bytes_acked == 0) { 14098 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14099 int dupack_cnt; 14100 14101 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14102 /* 14103 * Fast retransmit. When we have seen exactly three 14104 * identical ACKs while we have unacked data 14105 * outstanding we take it as a hint that our peer 14106 * dropped something. 14107 * 14108 * If TCP is retransmitting, don't do fast retransmit. 14109 */ 14110 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14111 ! tcp->tcp_rexmit) { 14112 /* Do Limited Transmit */ 14113 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14114 tcps->tcps_dupack_fast_retransmit) { 14115 /* 14116 * RFC 3042 14117 * 14118 * What we need to do is temporarily 14119 * increase tcp_cwnd so that new 14120 * data can be sent if it is allowed 14121 * by the receive window (tcp_rwnd). 14122 * tcp_wput_data() will take care of 14123 * the rest. 14124 * 14125 * If the connection is SACK capable, 14126 * only do limited xmit when there 14127 * is SACK info. 14128 * 14129 * Note how tcp_cwnd is incremented. 14130 * The first dup ACK will increase 14131 * it by 1 MSS. The second dup ACK 14132 * will increase it by 2 MSS. This 14133 * means that only 1 new segment will 14134 * be sent for each dup ACK. 14135 */ 14136 if (tcp->tcp_unsent > 0 && 14137 (!tcp->tcp_snd_sack_ok || 14138 (tcp->tcp_snd_sack_ok && 14139 tcp->tcp_notsack_list != NULL))) { 14140 tcp->tcp_cwnd += mss << 14141 (tcp->tcp_dupack_cnt - 1); 14142 flags |= TH_LIMIT_XMIT; 14143 } 14144 } else if (dupack_cnt == 14145 tcps->tcps_dupack_fast_retransmit) { 14146 14147 /* 14148 * If we have reduced tcp_ssthresh 14149 * because of ECN, do not reduce it again 14150 * unless it is already one window of data 14151 * away. After one window of data, tcp_cwr 14152 * should then be cleared. Note that 14153 * for non ECN capable connection, tcp_cwr 14154 * should always be false. 14155 * 14156 * Adjust cwnd since the duplicate 14157 * ack indicates that a packet was 14158 * dropped (due to congestion.) 14159 */ 14160 if (!tcp->tcp_cwr) { 14161 npkt = ((tcp->tcp_snxt - 14162 tcp->tcp_suna) >> 1) / mss; 14163 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14164 mss; 14165 tcp->tcp_cwnd = (npkt + 14166 tcp->tcp_dupack_cnt) * mss; 14167 } 14168 if (tcp->tcp_ecn_ok) { 14169 tcp->tcp_cwr = B_TRUE; 14170 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14171 tcp->tcp_ecn_cwr_sent = B_FALSE; 14172 } 14173 14174 /* 14175 * We do Hoe's algorithm. Refer to her 14176 * paper "Improving the Start-up Behavior 14177 * of a Congestion Control Scheme for TCP," 14178 * appeared in SIGCOMM'96. 14179 * 14180 * Save highest seq no we have sent so far. 14181 * Be careful about the invisible FIN byte. 14182 */ 14183 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14184 (tcp->tcp_unsent == 0)) { 14185 tcp->tcp_rexmit_max = tcp->tcp_fss; 14186 } else { 14187 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14188 } 14189 14190 /* 14191 * Do not allow bursty traffic during. 14192 * fast recovery. Refer to Fall and Floyd's 14193 * paper "Simulation-based Comparisons of 14194 * Tahoe, Reno and SACK TCP" (in CCR?) 14195 * This is a best current practise. 14196 */ 14197 tcp->tcp_snd_burst = TCP_CWND_SS; 14198 14199 /* 14200 * For SACK: 14201 * Calculate tcp_pipe, which is the 14202 * estimated number of bytes in 14203 * network. 14204 * 14205 * tcp_fack is the highest sack'ed seq num 14206 * TCP has received. 14207 * 14208 * tcp_pipe is explained in the above quoted 14209 * Fall and Floyd's paper. tcp_fack is 14210 * explained in Mathis and Mahdavi's 14211 * "Forward Acknowledgment: Refining TCP 14212 * Congestion Control" in SIGCOMM '96. 14213 */ 14214 if (tcp->tcp_snd_sack_ok) { 14215 ASSERT(tcp->tcp_sack_info != NULL); 14216 if (tcp->tcp_notsack_list != NULL) { 14217 tcp->tcp_pipe = tcp->tcp_snxt - 14218 tcp->tcp_fack; 14219 tcp->tcp_sack_snxt = seg_ack; 14220 flags |= TH_NEED_SACK_REXMIT; 14221 } else { 14222 /* 14223 * Always initialize tcp_pipe 14224 * even though we don't have 14225 * any SACK info. If later 14226 * we get SACK info and 14227 * tcp_pipe is not initialized, 14228 * funny things will happen. 14229 */ 14230 tcp->tcp_pipe = 14231 tcp->tcp_cwnd_ssthresh; 14232 } 14233 } else { 14234 flags |= TH_REXMIT_NEEDED; 14235 } /* tcp_snd_sack_ok */ 14236 14237 } else { 14238 /* 14239 * Here we perform congestion 14240 * avoidance, but NOT slow start. 14241 * This is known as the Fast 14242 * Recovery Algorithm. 14243 */ 14244 if (tcp->tcp_snd_sack_ok && 14245 tcp->tcp_notsack_list != NULL) { 14246 flags |= TH_NEED_SACK_REXMIT; 14247 tcp->tcp_pipe -= mss; 14248 if (tcp->tcp_pipe < 0) 14249 tcp->tcp_pipe = 0; 14250 } else { 14251 /* 14252 * We know that one more packet has 14253 * left the pipe thus we can update 14254 * cwnd. 14255 */ 14256 cwnd = tcp->tcp_cwnd + mss; 14257 if (cwnd > tcp->tcp_cwnd_max) 14258 cwnd = tcp->tcp_cwnd_max; 14259 tcp->tcp_cwnd = cwnd; 14260 if (tcp->tcp_unsent > 0) 14261 flags |= TH_XMIT_NEEDED; 14262 } 14263 } 14264 } 14265 } else if (tcp->tcp_zero_win_probe) { 14266 /* 14267 * If the window has opened, need to arrange 14268 * to send additional data. 14269 */ 14270 if (new_swnd != 0) { 14271 /* tcp_suna != tcp_snxt */ 14272 /* Packet contains a window update */ 14273 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14274 tcp->tcp_zero_win_probe = 0; 14275 tcp->tcp_timer_backoff = 0; 14276 tcp->tcp_ms_we_have_waited = 0; 14277 14278 /* 14279 * Transmit starting with tcp_suna since 14280 * the one byte probe is not ack'ed. 14281 * If TCP has sent more than one identical 14282 * probe, tcp_rexmit will be set. That means 14283 * tcp_ss_rexmit() will send out the one 14284 * byte along with new data. Otherwise, 14285 * fake the retransmission. 14286 */ 14287 flags |= TH_XMIT_NEEDED; 14288 if (!tcp->tcp_rexmit) { 14289 tcp->tcp_rexmit = B_TRUE; 14290 tcp->tcp_dupack_cnt = 0; 14291 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14292 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14293 } 14294 } 14295 } 14296 goto swnd_update; 14297 } 14298 14299 /* 14300 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14301 * If the ACK value acks something that we have not yet sent, it might 14302 * be an old duplicate segment. Send an ACK to re-synchronize the 14303 * other side. 14304 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14305 * state is handled above, so we can always just drop the segment and 14306 * send an ACK here. 14307 * 14308 * In the case where the peer shrinks the window, we see the new window 14309 * update, but all the data sent previously is queued up by the peer. 14310 * To account for this, in tcp_process_shrunk_swnd(), the sequence 14311 * number, which was already sent, and within window, is recorded. 14312 * tcp_snxt is then updated. 14313 * 14314 * If the window has previously shrunk, and an ACK for data not yet 14315 * sent, according to tcp_snxt is recieved, it may still be valid. If 14316 * the ACK is for data within the window at the time the window was 14317 * shrunk, then the ACK is acceptable. In this case tcp_snxt is set to 14318 * the sequence number ACK'ed. 14319 * 14320 * If the ACK covers all the data sent at the time the window was 14321 * shrunk, we can now set tcp_is_wnd_shrnk to B_FALSE. 14322 * 14323 * Should we send ACKs in response to ACK only segments? 14324 */ 14325 14326 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14327 if ((tcp->tcp_is_wnd_shrnk) && 14328 (SEQ_LEQ(seg_ack, tcp->tcp_snxt_shrunk))) { 14329 uint32_t data_acked_ahead_snxt; 14330 14331 data_acked_ahead_snxt = seg_ack - tcp->tcp_snxt; 14332 tcp_update_xmit_tail(tcp, seg_ack); 14333 tcp->tcp_unsent -= data_acked_ahead_snxt; 14334 } else { 14335 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14336 /* drop the received segment */ 14337 freemsg(mp); 14338 14339 /* 14340 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14341 * greater than 0, check if the number of such 14342 * bogus ACks is greater than that count. If yes, 14343 * don't send back any ACK. This prevents TCP from 14344 * getting into an ACK storm if somehow an attacker 14345 * successfully spoofs an acceptable segment to our 14346 * peer. 14347 */ 14348 if (tcp_drop_ack_unsent_cnt > 0 && 14349 ++tcp->tcp_in_ack_unsent > 14350 tcp_drop_ack_unsent_cnt) { 14351 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14352 return; 14353 } 14354 mp = tcp_ack_mp(tcp); 14355 if (mp != NULL) { 14356 BUMP_LOCAL(tcp->tcp_obsegs); 14357 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14358 tcp_send_data(tcp, tcp->tcp_wq, mp); 14359 } 14360 return; 14361 } 14362 } else if (tcp->tcp_is_wnd_shrnk && SEQ_GEQ(seg_ack, 14363 tcp->tcp_snxt_shrunk)) { 14364 tcp->tcp_is_wnd_shrnk = B_FALSE; 14365 } 14366 14367 /* 14368 * TCP gets a new ACK, update the notsack'ed list to delete those 14369 * blocks that are covered by this ACK. 14370 */ 14371 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14372 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14373 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14374 } 14375 14376 /* 14377 * If we got an ACK after fast retransmit, check to see 14378 * if it is a partial ACK. If it is not and the congestion 14379 * window was inflated to account for the other side's 14380 * cached packets, retract it. If it is, do Hoe's algorithm. 14381 */ 14382 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14383 ASSERT(tcp->tcp_rexmit == B_FALSE); 14384 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14385 tcp->tcp_dupack_cnt = 0; 14386 /* 14387 * Restore the orig tcp_cwnd_ssthresh after 14388 * fast retransmit phase. 14389 */ 14390 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14391 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14392 } 14393 tcp->tcp_rexmit_max = seg_ack; 14394 tcp->tcp_cwnd_cnt = 0; 14395 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14396 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14397 14398 /* 14399 * Remove all notsack info to avoid confusion with 14400 * the next fast retrasnmit/recovery phase. 14401 */ 14402 if (tcp->tcp_snd_sack_ok && 14403 tcp->tcp_notsack_list != NULL) { 14404 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, 14405 tcp); 14406 } 14407 } else { 14408 if (tcp->tcp_snd_sack_ok && 14409 tcp->tcp_notsack_list != NULL) { 14410 flags |= TH_NEED_SACK_REXMIT; 14411 tcp->tcp_pipe -= mss; 14412 if (tcp->tcp_pipe < 0) 14413 tcp->tcp_pipe = 0; 14414 } else { 14415 /* 14416 * Hoe's algorithm: 14417 * 14418 * Retransmit the unack'ed segment and 14419 * restart fast recovery. Note that we 14420 * need to scale back tcp_cwnd to the 14421 * original value when we started fast 14422 * recovery. This is to prevent overly 14423 * aggressive behaviour in sending new 14424 * segments. 14425 */ 14426 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14427 tcps->tcps_dupack_fast_retransmit * mss; 14428 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14429 flags |= TH_REXMIT_NEEDED; 14430 } 14431 } 14432 } else { 14433 tcp->tcp_dupack_cnt = 0; 14434 if (tcp->tcp_rexmit) { 14435 /* 14436 * TCP is retranmitting. If the ACK ack's all 14437 * outstanding data, update tcp_rexmit_max and 14438 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14439 * to the correct value. 14440 * 14441 * Note that SEQ_LEQ() is used. This is to avoid 14442 * unnecessary fast retransmit caused by dup ACKs 14443 * received when TCP does slow start retransmission 14444 * after a time out. During this phase, TCP may 14445 * send out segments which are already received. 14446 * This causes dup ACKs to be sent back. 14447 */ 14448 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14449 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14450 tcp->tcp_rexmit_nxt = seg_ack; 14451 } 14452 if (seg_ack != tcp->tcp_rexmit_max) { 14453 flags |= TH_XMIT_NEEDED; 14454 } 14455 } else { 14456 tcp->tcp_rexmit = B_FALSE; 14457 tcp->tcp_xmit_zc_clean = B_FALSE; 14458 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14459 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14460 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14461 } 14462 tcp->tcp_ms_we_have_waited = 0; 14463 } 14464 } 14465 14466 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14467 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14468 tcp->tcp_suna = seg_ack; 14469 if (tcp->tcp_zero_win_probe != 0) { 14470 tcp->tcp_zero_win_probe = 0; 14471 tcp->tcp_timer_backoff = 0; 14472 } 14473 14474 /* 14475 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14476 * Note that it cannot be the SYN being ack'ed. The code flow 14477 * will not reach here. 14478 */ 14479 if (mp1 == NULL) { 14480 goto fin_acked; 14481 } 14482 14483 /* 14484 * Update the congestion window. 14485 * 14486 * If TCP is not ECN capable or TCP is ECN capable but the 14487 * congestion experience bit is not set, increase the tcp_cwnd as 14488 * usual. 14489 */ 14490 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14491 cwnd = tcp->tcp_cwnd; 14492 add = mss; 14493 14494 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14495 /* 14496 * This is to prevent an increase of less than 1 MSS of 14497 * tcp_cwnd. With partial increase, tcp_wput_data() 14498 * may send out tinygrams in order to preserve mblk 14499 * boundaries. 14500 * 14501 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14502 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14503 * increased by 1 MSS for every RTTs. 14504 */ 14505 if (tcp->tcp_cwnd_cnt <= 0) { 14506 tcp->tcp_cwnd_cnt = cwnd + add; 14507 } else { 14508 tcp->tcp_cwnd_cnt -= add; 14509 add = 0; 14510 } 14511 } 14512 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14513 } 14514 14515 /* See if the latest urgent data has been acknowledged */ 14516 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14517 SEQ_GT(seg_ack, tcp->tcp_urg)) 14518 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14519 14520 /* Can we update the RTT estimates? */ 14521 if (tcp->tcp_snd_ts_ok) { 14522 /* Ignore zero timestamp echo-reply. */ 14523 if (tcpopt.tcp_opt_ts_ecr != 0) { 14524 tcp_set_rto(tcp, (int32_t)lbolt - 14525 (int32_t)tcpopt.tcp_opt_ts_ecr); 14526 } 14527 14528 /* If needed, restart the timer. */ 14529 if (tcp->tcp_set_timer == 1) { 14530 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14531 tcp->tcp_set_timer = 0; 14532 } 14533 /* 14534 * Update tcp_csuna in case the other side stops sending 14535 * us timestamps. 14536 */ 14537 tcp->tcp_csuna = tcp->tcp_snxt; 14538 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14539 /* 14540 * An ACK sequence we haven't seen before, so get the RTT 14541 * and update the RTO. But first check if the timestamp is 14542 * valid to use. 14543 */ 14544 if ((mp1->b_next != NULL) && 14545 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14546 tcp_set_rto(tcp, (int32_t)lbolt - 14547 (int32_t)(intptr_t)mp1->b_prev); 14548 else 14549 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14550 14551 /* Remeber the last sequence to be ACKed */ 14552 tcp->tcp_csuna = seg_ack; 14553 if (tcp->tcp_set_timer == 1) { 14554 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14555 tcp->tcp_set_timer = 0; 14556 } 14557 } else { 14558 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14559 } 14560 14561 /* Eat acknowledged bytes off the xmit queue. */ 14562 for (;;) { 14563 mblk_t *mp2; 14564 uchar_t *wptr; 14565 14566 wptr = mp1->b_wptr; 14567 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14568 bytes_acked -= (int)(wptr - mp1->b_rptr); 14569 if (bytes_acked < 0) { 14570 mp1->b_rptr = wptr + bytes_acked; 14571 /* 14572 * Set a new timestamp if all the bytes timed by the 14573 * old timestamp have been ack'ed. 14574 */ 14575 if (SEQ_GT(seg_ack, 14576 (uint32_t)(uintptr_t)(mp1->b_next))) { 14577 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14578 mp1->b_next = NULL; 14579 } 14580 break; 14581 } 14582 mp1->b_next = NULL; 14583 mp1->b_prev = NULL; 14584 mp2 = mp1; 14585 mp1 = mp1->b_cont; 14586 14587 /* 14588 * This notification is required for some zero-copy 14589 * clients to maintain a copy semantic. After the data 14590 * is ack'ed, client is safe to modify or reuse the buffer. 14591 */ 14592 if (tcp->tcp_snd_zcopy_aware && 14593 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14594 tcp_zcopy_notify(tcp); 14595 freeb(mp2); 14596 if (bytes_acked == 0) { 14597 if (mp1 == NULL) { 14598 /* Everything is ack'ed, clear the tail. */ 14599 tcp->tcp_xmit_tail = NULL; 14600 /* 14601 * Cancel the timer unless we are still 14602 * waiting for an ACK for the FIN packet. 14603 */ 14604 if (tcp->tcp_timer_tid != 0 && 14605 tcp->tcp_snxt == tcp->tcp_suna) { 14606 (void) TCP_TIMER_CANCEL(tcp, 14607 tcp->tcp_timer_tid); 14608 tcp->tcp_timer_tid = 0; 14609 } 14610 goto pre_swnd_update; 14611 } 14612 if (mp2 != tcp->tcp_xmit_tail) 14613 break; 14614 tcp->tcp_xmit_tail = mp1; 14615 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14616 (uintptr_t)INT_MAX); 14617 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14618 mp1->b_rptr); 14619 break; 14620 } 14621 if (mp1 == NULL) { 14622 /* 14623 * More was acked but there is nothing more 14624 * outstanding. This means that the FIN was 14625 * just acked or that we're talking to a clown. 14626 */ 14627 fin_acked: 14628 ASSERT(tcp->tcp_fin_sent); 14629 tcp->tcp_xmit_tail = NULL; 14630 if (tcp->tcp_fin_sent) { 14631 /* FIN was acked - making progress */ 14632 if (tcp->tcp_ipversion == IPV6_VERSION && 14633 !tcp->tcp_fin_acked) 14634 tcp->tcp_ip_forward_progress = B_TRUE; 14635 tcp->tcp_fin_acked = B_TRUE; 14636 if (tcp->tcp_linger_tid != 0 && 14637 TCP_TIMER_CANCEL(tcp, 14638 tcp->tcp_linger_tid) >= 0) { 14639 tcp_stop_lingering(tcp); 14640 freemsg(mp); 14641 mp = NULL; 14642 } 14643 } else { 14644 /* 14645 * We should never get here because 14646 * we have already checked that the 14647 * number of bytes ack'ed should be 14648 * smaller than or equal to what we 14649 * have sent so far (it is the 14650 * acceptability check of the ACK). 14651 * We can only get here if the send 14652 * queue is corrupted. 14653 * 14654 * Terminate the connection and 14655 * panic the system. It is better 14656 * for us to panic instead of 14657 * continuing to avoid other disaster. 14658 */ 14659 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14660 tcp->tcp_rnxt, TH_RST|TH_ACK); 14661 panic("Memory corruption " 14662 "detected for connection %s.", 14663 tcp_display(tcp, NULL, 14664 DISP_ADDR_AND_PORT)); 14665 /*NOTREACHED*/ 14666 } 14667 goto pre_swnd_update; 14668 } 14669 ASSERT(mp2 != tcp->tcp_xmit_tail); 14670 } 14671 if (tcp->tcp_unsent) { 14672 flags |= TH_XMIT_NEEDED; 14673 } 14674 pre_swnd_update: 14675 tcp->tcp_xmit_head = mp1; 14676 swnd_update: 14677 /* 14678 * The following check is different from most other implementations. 14679 * For bi-directional transfer, when segments are dropped, the 14680 * "normal" check will not accept a window update in those 14681 * retransmitted segemnts. Failing to do that, TCP may send out 14682 * segments which are outside receiver's window. As TCP accepts 14683 * the ack in those retransmitted segments, if the window update in 14684 * the same segment is not accepted, TCP will incorrectly calculates 14685 * that it can send more segments. This can create a deadlock 14686 * with the receiver if its window becomes zero. 14687 */ 14688 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14689 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14690 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14691 /* 14692 * The criteria for update is: 14693 * 14694 * 1. the segment acknowledges some data. Or 14695 * 2. the segment is new, i.e. it has a higher seq num. Or 14696 * 3. the segment is not old and the advertised window is 14697 * larger than the previous advertised window. 14698 */ 14699 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14700 flags |= TH_XMIT_NEEDED; 14701 tcp->tcp_swnd = new_swnd; 14702 if (new_swnd > tcp->tcp_max_swnd) 14703 tcp->tcp_max_swnd = new_swnd; 14704 tcp->tcp_swl1 = seg_seq; 14705 tcp->tcp_swl2 = seg_ack; 14706 } 14707 est: 14708 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14709 14710 switch (tcp->tcp_state) { 14711 case TCPS_FIN_WAIT_1: 14712 if (tcp->tcp_fin_acked) { 14713 tcp->tcp_state = TCPS_FIN_WAIT_2; 14714 /* 14715 * We implement the non-standard BSD/SunOS 14716 * FIN_WAIT_2 flushing algorithm. 14717 * If there is no user attached to this 14718 * TCP endpoint, then this TCP struct 14719 * could hang around forever in FIN_WAIT_2 14720 * state if the peer forgets to send us 14721 * a FIN. To prevent this, we wait only 14722 * 2*MSL (a convenient time value) for 14723 * the FIN to arrive. If it doesn't show up, 14724 * we flush the TCP endpoint. This algorithm, 14725 * though a violation of RFC-793, has worked 14726 * for over 10 years in BSD systems. 14727 * Note: SunOS 4.x waits 675 seconds before 14728 * flushing the FIN_WAIT_2 connection. 14729 */ 14730 TCP_TIMER_RESTART(tcp, 14731 tcps->tcps_fin_wait_2_flush_interval); 14732 } 14733 break; 14734 case TCPS_FIN_WAIT_2: 14735 break; /* Shutdown hook? */ 14736 case TCPS_LAST_ACK: 14737 freemsg(mp); 14738 if (tcp->tcp_fin_acked) { 14739 (void) tcp_clean_death(tcp, 0, 19); 14740 return; 14741 } 14742 goto xmit_check; 14743 case TCPS_CLOSING: 14744 if (tcp->tcp_fin_acked) { 14745 tcp->tcp_state = TCPS_TIME_WAIT; 14746 /* 14747 * Unconditionally clear the exclusive binding 14748 * bit so this TIME-WAIT connection won't 14749 * interfere with new ones. 14750 */ 14751 tcp->tcp_exclbind = 0; 14752 if (!TCP_IS_DETACHED(tcp)) { 14753 TCP_TIMER_RESTART(tcp, 14754 tcps->tcps_time_wait_interval); 14755 } else { 14756 tcp_time_wait_append(tcp); 14757 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14758 } 14759 } 14760 /*FALLTHRU*/ 14761 case TCPS_CLOSE_WAIT: 14762 freemsg(mp); 14763 goto xmit_check; 14764 default: 14765 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14766 break; 14767 } 14768 } 14769 if (flags & TH_FIN) { 14770 /* Make sure we ack the fin */ 14771 flags |= TH_ACK_NEEDED; 14772 if (!tcp->tcp_fin_rcvd) { 14773 tcp->tcp_fin_rcvd = B_TRUE; 14774 tcp->tcp_rnxt++; 14775 tcph = tcp->tcp_tcph; 14776 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14777 14778 /* 14779 * Generate the ordrel_ind at the end unless we 14780 * are an eager guy. 14781 * In the eager case tcp_rsrv will do this when run 14782 * after tcp_accept is done. 14783 */ 14784 if (tcp->tcp_listener == NULL && 14785 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14786 flags |= TH_ORDREL_NEEDED; 14787 switch (tcp->tcp_state) { 14788 case TCPS_SYN_RCVD: 14789 case TCPS_ESTABLISHED: 14790 tcp->tcp_state = TCPS_CLOSE_WAIT; 14791 /* Keepalive? */ 14792 break; 14793 case TCPS_FIN_WAIT_1: 14794 if (!tcp->tcp_fin_acked) { 14795 tcp->tcp_state = TCPS_CLOSING; 14796 break; 14797 } 14798 /* FALLTHRU */ 14799 case TCPS_FIN_WAIT_2: 14800 tcp->tcp_state = TCPS_TIME_WAIT; 14801 /* 14802 * Unconditionally clear the exclusive binding 14803 * bit so this TIME-WAIT connection won't 14804 * interfere with new ones. 14805 */ 14806 tcp->tcp_exclbind = 0; 14807 if (!TCP_IS_DETACHED(tcp)) { 14808 TCP_TIMER_RESTART(tcp, 14809 tcps->tcps_time_wait_interval); 14810 } else { 14811 tcp_time_wait_append(tcp); 14812 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14813 } 14814 if (seg_len) { 14815 /* 14816 * implies data piggybacked on FIN. 14817 * break to handle data. 14818 */ 14819 break; 14820 } 14821 freemsg(mp); 14822 goto ack_check; 14823 } 14824 } 14825 } 14826 if (mp == NULL) 14827 goto xmit_check; 14828 if (seg_len == 0) { 14829 freemsg(mp); 14830 goto xmit_check; 14831 } 14832 if (mp->b_rptr == mp->b_wptr) { 14833 /* 14834 * The header has been consumed, so we remove the 14835 * zero-length mblk here. 14836 */ 14837 mp1 = mp; 14838 mp = mp->b_cont; 14839 freeb(mp1); 14840 } 14841 update_ack: 14842 tcph = tcp->tcp_tcph; 14843 tcp->tcp_rack_cnt++; 14844 { 14845 uint32_t cur_max; 14846 14847 cur_max = tcp->tcp_rack_cur_max; 14848 if (tcp->tcp_rack_cnt >= cur_max) { 14849 /* 14850 * We have more unacked data than we should - send 14851 * an ACK now. 14852 */ 14853 flags |= TH_ACK_NEEDED; 14854 cur_max++; 14855 if (cur_max > tcp->tcp_rack_abs_max) 14856 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14857 else 14858 tcp->tcp_rack_cur_max = cur_max; 14859 } else if (TCP_IS_DETACHED(tcp)) { 14860 /* We don't have an ACK timer for detached TCP. */ 14861 flags |= TH_ACK_NEEDED; 14862 } else if (seg_len < mss) { 14863 /* 14864 * If we get a segment that is less than an mss, and we 14865 * already have unacknowledged data, and the amount 14866 * unacknowledged is not a multiple of mss, then we 14867 * better generate an ACK now. Otherwise, this may be 14868 * the tail piece of a transaction, and we would rather 14869 * wait for the response. 14870 */ 14871 uint32_t udif; 14872 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14873 (uintptr_t)INT_MAX); 14874 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14875 if (udif && (udif % mss)) 14876 flags |= TH_ACK_NEEDED; 14877 else 14878 flags |= TH_ACK_TIMER_NEEDED; 14879 } else { 14880 /* Start delayed ack timer */ 14881 flags |= TH_ACK_TIMER_NEEDED; 14882 } 14883 } 14884 tcp->tcp_rnxt += seg_len; 14885 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14886 14887 if (mp == NULL) 14888 goto xmit_check; 14889 14890 /* Update SACK list */ 14891 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14892 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14893 &(tcp->tcp_num_sack_blk)); 14894 } 14895 14896 if (tcp->tcp_urp_mp) { 14897 tcp->tcp_urp_mp->b_cont = mp; 14898 mp = tcp->tcp_urp_mp; 14899 tcp->tcp_urp_mp = NULL; 14900 /* Ready for a new signal. */ 14901 tcp->tcp_urp_last_valid = B_FALSE; 14902 #ifdef DEBUG 14903 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14904 "tcp_rput: sending exdata_ind %s", 14905 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14906 #endif /* DEBUG */ 14907 } 14908 14909 /* 14910 * Check for ancillary data changes compared to last segment. 14911 */ 14912 if (tcp->tcp_ipv6_recvancillary != 0) { 14913 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14914 ASSERT(mp != NULL); 14915 } 14916 14917 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14918 /* 14919 * Side queue inbound data until the accept happens. 14920 * tcp_accept/tcp_rput drains this when the accept happens. 14921 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14922 * T_EXDATA_IND) it is queued on b_next. 14923 * XXX Make urgent data use this. Requires: 14924 * Removing tcp_listener check for TH_URG 14925 * Making M_PCPROTO and MARK messages skip the eager case 14926 */ 14927 14928 if (tcp->tcp_kssl_pending) { 14929 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 14930 mblk_t *, mp); 14931 tcp_kssl_input(tcp, mp); 14932 } else { 14933 tcp_rcv_enqueue(tcp, mp, seg_len); 14934 } 14935 } else if (IPCL_IS_NONSTR(connp)) { 14936 /* 14937 * Non-STREAMS socket 14938 * 14939 * Note that no KSSL processing is done here, because 14940 * KSSL is not supported for non-STREAMS sockets. 14941 */ 14942 boolean_t push = flags & (TH_PUSH|TH_FIN); 14943 int error; 14944 14945 if ((*connp->conn_upcalls->su_recv)( 14946 connp->conn_upper_handle, 14947 mp, seg_len, 0, &error, &push) <= 0) { 14948 /* 14949 * We should never be in middle of a 14950 * fallback, the squeue guarantees that. 14951 */ 14952 ASSERT(error != EOPNOTSUPP); 14953 if (error == ENOSPC) 14954 tcp->tcp_rwnd -= seg_len; 14955 } else if (push) { 14956 /* PUSH bit set and sockfs is not flow controlled */ 14957 flags |= tcp_rwnd_reopen(tcp); 14958 } 14959 } else { 14960 /* STREAMS socket */ 14961 if (mp->b_datap->db_type != M_DATA || 14962 (flags & TH_MARKNEXT_NEEDED)) { 14963 if (tcp->tcp_rcv_list != NULL) { 14964 flags |= tcp_rcv_drain(tcp); 14965 } 14966 ASSERT(tcp->tcp_rcv_list == NULL || 14967 tcp->tcp_fused_sigurg); 14968 14969 if (flags & TH_MARKNEXT_NEEDED) { 14970 #ifdef DEBUG 14971 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14972 "tcp_rput: sending MSGMARKNEXT %s", 14973 tcp_display(tcp, NULL, 14974 DISP_PORT_ONLY)); 14975 #endif /* DEBUG */ 14976 mp->b_flag |= MSGMARKNEXT; 14977 flags &= ~TH_MARKNEXT_NEEDED; 14978 } 14979 14980 /* Does this need SSL processing first? */ 14981 if ((tcp->tcp_kssl_ctx != NULL) && 14982 (DB_TYPE(mp) == M_DATA)) { 14983 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 14984 mblk_t *, mp); 14985 tcp_kssl_input(tcp, mp); 14986 } else { 14987 putnext(tcp->tcp_rq, mp); 14988 if (!canputnext(tcp->tcp_rq)) 14989 tcp->tcp_rwnd -= seg_len; 14990 } 14991 } else if ((tcp->tcp_kssl_ctx != NULL) && 14992 (DB_TYPE(mp) == M_DATA)) { 14993 /* Does this need SSL processing first? */ 14994 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, mblk_t *, mp); 14995 tcp_kssl_input(tcp, mp); 14996 } else if ((flags & (TH_PUSH|TH_FIN)) || 14997 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_recv_hiwater >> 3) { 14998 if (tcp->tcp_rcv_list != NULL) { 14999 /* 15000 * Enqueue the new segment first and then 15001 * call tcp_rcv_drain() to send all data 15002 * up. The other way to do this is to 15003 * send all queued data up and then call 15004 * putnext() to send the new segment up. 15005 * This way can remove the else part later 15006 * on. 15007 * 15008 * We don't do this to avoid one more call to 15009 * canputnext() as tcp_rcv_drain() needs to 15010 * call canputnext(). 15011 */ 15012 tcp_rcv_enqueue(tcp, mp, seg_len); 15013 flags |= tcp_rcv_drain(tcp); 15014 } else { 15015 putnext(tcp->tcp_rq, mp); 15016 if (!canputnext(tcp->tcp_rq)) 15017 tcp->tcp_rwnd -= seg_len; 15018 } 15019 } else { 15020 /* 15021 * Enqueue all packets when processing an mblk 15022 * from the co queue and also enqueue normal packets. 15023 */ 15024 tcp_rcv_enqueue(tcp, mp, seg_len); 15025 } 15026 /* 15027 * Make sure the timer is running if we have data waiting 15028 * for a push bit. This provides resiliency against 15029 * implementations that do not correctly generate push bits. 15030 */ 15031 if (tcp->tcp_rcv_list != NULL && tcp->tcp_push_tid == 0) { 15032 /* 15033 * The connection may be closed at this point, so don't 15034 * do anything for a detached tcp. 15035 */ 15036 if (!TCP_IS_DETACHED(tcp)) 15037 tcp->tcp_push_tid = TCP_TIMER(tcp, 15038 tcp_push_timer, 15039 MSEC_TO_TICK( 15040 tcps->tcps_push_timer_interval)); 15041 } 15042 } 15043 15044 xmit_check: 15045 /* Is there anything left to do? */ 15046 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15047 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15048 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15049 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15050 goto done; 15051 15052 /* Any transmit work to do and a non-zero window? */ 15053 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15054 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15055 if (flags & TH_REXMIT_NEEDED) { 15056 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15057 15058 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15059 if (snd_size > mss) 15060 snd_size = mss; 15061 if (snd_size > tcp->tcp_swnd) 15062 snd_size = tcp->tcp_swnd; 15063 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15064 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15065 B_TRUE); 15066 15067 if (mp1 != NULL) { 15068 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15069 tcp->tcp_csuna = tcp->tcp_snxt; 15070 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15071 UPDATE_MIB(&tcps->tcps_mib, 15072 tcpRetransBytes, snd_size); 15073 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15074 } 15075 } 15076 if (flags & TH_NEED_SACK_REXMIT) { 15077 tcp_sack_rxmit(tcp, &flags); 15078 } 15079 /* 15080 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15081 * out new segment. Note that tcp_rexmit should not be 15082 * set, otherwise TH_LIMIT_XMIT should not be set. 15083 */ 15084 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15085 if (!tcp->tcp_rexmit) { 15086 tcp_wput_data(tcp, NULL, B_FALSE); 15087 } else { 15088 tcp_ss_rexmit(tcp); 15089 } 15090 } 15091 /* 15092 * Adjust tcp_cwnd back to normal value after sending 15093 * new data segments. 15094 */ 15095 if (flags & TH_LIMIT_XMIT) { 15096 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15097 /* 15098 * This will restart the timer. Restarting the 15099 * timer is used to avoid a timeout before the 15100 * limited transmitted segment's ACK gets back. 15101 */ 15102 if (tcp->tcp_xmit_head != NULL) 15103 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15104 } 15105 15106 /* Anything more to do? */ 15107 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15108 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15109 goto done; 15110 } 15111 ack_check: 15112 if (flags & TH_SEND_URP_MARK) { 15113 ASSERT(tcp->tcp_urp_mark_mp); 15114 ASSERT(!IPCL_IS_NONSTR(connp)); 15115 /* 15116 * Send up any queued data and then send the mark message 15117 */ 15118 if (tcp->tcp_rcv_list != NULL) { 15119 flags |= tcp_rcv_drain(tcp); 15120 15121 } 15122 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15123 mp1 = tcp->tcp_urp_mark_mp; 15124 tcp->tcp_urp_mark_mp = NULL; 15125 putnext(tcp->tcp_rq, mp1); 15126 #ifdef DEBUG 15127 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15128 "tcp_rput: sending zero-length %s %s", 15129 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15130 "MSGNOTMARKNEXT"), 15131 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15132 #endif /* DEBUG */ 15133 flags &= ~TH_SEND_URP_MARK; 15134 } 15135 if (flags & TH_ACK_NEEDED) { 15136 /* 15137 * Time to send an ack for some reason. 15138 */ 15139 mp1 = tcp_ack_mp(tcp); 15140 15141 if (mp1 != NULL) { 15142 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15143 BUMP_LOCAL(tcp->tcp_obsegs); 15144 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15145 } 15146 if (tcp->tcp_ack_tid != 0) { 15147 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15148 tcp->tcp_ack_tid = 0; 15149 } 15150 } 15151 if (flags & TH_ACK_TIMER_NEEDED) { 15152 /* 15153 * Arrange for deferred ACK or push wait timeout. 15154 * Start timer if it is not already running. 15155 */ 15156 if (tcp->tcp_ack_tid == 0) { 15157 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15158 MSEC_TO_TICK(tcp->tcp_localnet ? 15159 (clock_t)tcps->tcps_local_dack_interval : 15160 (clock_t)tcps->tcps_deferred_ack_interval)); 15161 } 15162 } 15163 if (flags & TH_ORDREL_NEEDED) { 15164 /* 15165 * Send up the ordrel_ind unless we are an eager guy. 15166 * In the eager case tcp_rsrv will do this when run 15167 * after tcp_accept is done. 15168 */ 15169 ASSERT(tcp->tcp_listener == NULL); 15170 15171 if (IPCL_IS_NONSTR(connp)) { 15172 ASSERT(tcp->tcp_ordrel_mp == NULL); 15173 tcp->tcp_ordrel_done = B_TRUE; 15174 (*connp->conn_upcalls->su_opctl) 15175 (connp->conn_upper_handle, SOCK_OPCTL_SHUT_RECV, 0); 15176 goto done; 15177 } 15178 15179 if (tcp->tcp_rcv_list != NULL) { 15180 /* 15181 * Push any mblk(s) enqueued from co processing. 15182 */ 15183 flags |= tcp_rcv_drain(tcp); 15184 } 15185 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15186 15187 mp1 = tcp->tcp_ordrel_mp; 15188 tcp->tcp_ordrel_mp = NULL; 15189 tcp->tcp_ordrel_done = B_TRUE; 15190 putnext(tcp->tcp_rq, mp1); 15191 } 15192 done: 15193 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15194 } 15195 15196 /* 15197 * This routine adjusts next-to-send sequence number variables, in the 15198 * case where the reciever has shrunk it's window. 15199 */ 15200 static void 15201 tcp_update_xmit_tail(tcp_t *tcp, uint32_t snxt) 15202 { 15203 mblk_t *xmit_tail; 15204 int32_t offset; 15205 15206 tcp->tcp_snxt = snxt; 15207 15208 /* Get the mblk, and the offset in it, as per the shrunk window */ 15209 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 15210 ASSERT(xmit_tail != NULL); 15211 tcp->tcp_xmit_tail = xmit_tail; 15212 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - 15213 xmit_tail->b_rptr - offset; 15214 } 15215 15216 /* 15217 * This function does PAWS protection check. Returns B_TRUE if the 15218 * segment passes the PAWS test, else returns B_FALSE. 15219 */ 15220 boolean_t 15221 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15222 { 15223 uint8_t flags; 15224 int options; 15225 uint8_t *up; 15226 15227 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15228 /* 15229 * If timestamp option is aligned nicely, get values inline, 15230 * otherwise call general routine to parse. Only do that 15231 * if timestamp is the only option. 15232 */ 15233 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15234 TCPOPT_REAL_TS_LEN && 15235 OK_32PTR((up = ((uint8_t *)tcph) + 15236 TCP_MIN_HEADER_LENGTH)) && 15237 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15238 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15239 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15240 15241 options = TCP_OPT_TSTAMP_PRESENT; 15242 } else { 15243 if (tcp->tcp_snd_sack_ok) { 15244 tcpoptp->tcp = tcp; 15245 } else { 15246 tcpoptp->tcp = NULL; 15247 } 15248 options = tcp_parse_options(tcph, tcpoptp); 15249 } 15250 15251 if (options & TCP_OPT_TSTAMP_PRESENT) { 15252 /* 15253 * Do PAWS per RFC 1323 section 4.2. Accept RST 15254 * regardless of the timestamp, page 18 RFC 1323.bis. 15255 */ 15256 if ((flags & TH_RST) == 0 && 15257 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15258 tcp->tcp_ts_recent)) { 15259 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15260 PAWS_TIMEOUT)) { 15261 /* This segment is not acceptable. */ 15262 return (B_FALSE); 15263 } else { 15264 /* 15265 * Connection has been idle for 15266 * too long. Reset the timestamp 15267 * and assume the segment is valid. 15268 */ 15269 tcp->tcp_ts_recent = 15270 tcpoptp->tcp_opt_ts_val; 15271 } 15272 } 15273 } else { 15274 /* 15275 * If we don't get a timestamp on every packet, we 15276 * figure we can't really trust 'em, so we stop sending 15277 * and parsing them. 15278 */ 15279 tcp->tcp_snd_ts_ok = B_FALSE; 15280 15281 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15282 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15283 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15284 /* 15285 * Adjust the tcp_mss accordingly. We also need to 15286 * adjust tcp_cwnd here in accordance with the new mss. 15287 * But we avoid doing a slow start here so as to not 15288 * to lose on the transfer rate built up so far. 15289 */ 15290 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15291 if (tcp->tcp_snd_sack_ok) { 15292 ASSERT(tcp->tcp_sack_info != NULL); 15293 tcp->tcp_max_sack_blk = 4; 15294 } 15295 } 15296 return (B_TRUE); 15297 } 15298 15299 /* 15300 * Attach ancillary data to a received TCP segments for the 15301 * ancillary pieces requested by the application that are 15302 * different than they were in the previous data segment. 15303 * 15304 * Save the "current" values once memory allocation is ok so that 15305 * when memory allocation fails we can just wait for the next data segment. 15306 */ 15307 static mblk_t * 15308 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15309 { 15310 struct T_optdata_ind *todi; 15311 int optlen; 15312 uchar_t *optptr; 15313 struct T_opthdr *toh; 15314 uint_t addflag; /* Which pieces to add */ 15315 mblk_t *mp1; 15316 15317 optlen = 0; 15318 addflag = 0; 15319 /* If app asked for pktinfo and the index has changed ... */ 15320 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15321 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15322 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15323 optlen += sizeof (struct T_opthdr) + 15324 sizeof (struct in6_pktinfo); 15325 addflag |= TCP_IPV6_RECVPKTINFO; 15326 } 15327 /* If app asked for hoplimit and it has changed ... */ 15328 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15329 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15330 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15331 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15332 addflag |= TCP_IPV6_RECVHOPLIMIT; 15333 } 15334 /* If app asked for tclass and it has changed ... */ 15335 if ((ipp->ipp_fields & IPPF_TCLASS) && 15336 ipp->ipp_tclass != tcp->tcp_recvtclass && 15337 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15338 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15339 addflag |= TCP_IPV6_RECVTCLASS; 15340 } 15341 /* 15342 * If app asked for hopbyhop headers and it has changed ... 15343 * For security labels, note that (1) security labels can't change on 15344 * a connected socket at all, (2) we're connected to at most one peer, 15345 * (3) if anything changes, then it must be some other extra option. 15346 */ 15347 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15348 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15349 (ipp->ipp_fields & IPPF_HOPOPTS), 15350 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15351 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15352 tcp->tcp_label_len; 15353 addflag |= TCP_IPV6_RECVHOPOPTS; 15354 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15355 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15356 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15357 return (mp); 15358 } 15359 /* If app asked for dst headers before routing headers ... */ 15360 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15361 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15362 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15363 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15364 optlen += sizeof (struct T_opthdr) + 15365 ipp->ipp_rtdstoptslen; 15366 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15367 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15368 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15369 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15370 return (mp); 15371 } 15372 /* If app asked for routing headers and it has changed ... */ 15373 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15374 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15375 (ipp->ipp_fields & IPPF_RTHDR), 15376 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15377 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15378 addflag |= TCP_IPV6_RECVRTHDR; 15379 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15380 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15381 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15382 return (mp); 15383 } 15384 /* If app asked for dest headers and it has changed ... */ 15385 if ((tcp->tcp_ipv6_recvancillary & 15386 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15387 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15388 (ipp->ipp_fields & IPPF_DSTOPTS), 15389 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15390 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15391 addflag |= TCP_IPV6_RECVDSTOPTS; 15392 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15393 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15394 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15395 return (mp); 15396 } 15397 15398 if (optlen == 0) { 15399 /* Nothing to add */ 15400 return (mp); 15401 } 15402 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15403 if (mp1 == NULL) { 15404 /* 15405 * Defer sending ancillary data until the next TCP segment 15406 * arrives. 15407 */ 15408 return (mp); 15409 } 15410 mp1->b_cont = mp; 15411 mp = mp1; 15412 mp->b_wptr += sizeof (*todi) + optlen; 15413 mp->b_datap->db_type = M_PROTO; 15414 todi = (struct T_optdata_ind *)mp->b_rptr; 15415 todi->PRIM_type = T_OPTDATA_IND; 15416 todi->DATA_flag = 1; /* MORE data */ 15417 todi->OPT_length = optlen; 15418 todi->OPT_offset = sizeof (*todi); 15419 optptr = (uchar_t *)&todi[1]; 15420 /* 15421 * If app asked for pktinfo and the index has changed ... 15422 * Note that the local address never changes for the connection. 15423 */ 15424 if (addflag & TCP_IPV6_RECVPKTINFO) { 15425 struct in6_pktinfo *pkti; 15426 15427 toh = (struct T_opthdr *)optptr; 15428 toh->level = IPPROTO_IPV6; 15429 toh->name = IPV6_PKTINFO; 15430 toh->len = sizeof (*toh) + sizeof (*pkti); 15431 toh->status = 0; 15432 optptr += sizeof (*toh); 15433 pkti = (struct in6_pktinfo *)optptr; 15434 if (tcp->tcp_ipversion == IPV6_VERSION) 15435 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15436 else 15437 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15438 &pkti->ipi6_addr); 15439 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15440 optptr += sizeof (*pkti); 15441 ASSERT(OK_32PTR(optptr)); 15442 /* Save as "last" value */ 15443 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15444 } 15445 /* If app asked for hoplimit and it has changed ... */ 15446 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15447 toh = (struct T_opthdr *)optptr; 15448 toh->level = IPPROTO_IPV6; 15449 toh->name = IPV6_HOPLIMIT; 15450 toh->len = sizeof (*toh) + sizeof (uint_t); 15451 toh->status = 0; 15452 optptr += sizeof (*toh); 15453 *(uint_t *)optptr = ipp->ipp_hoplimit; 15454 optptr += sizeof (uint_t); 15455 ASSERT(OK_32PTR(optptr)); 15456 /* Save as "last" value */ 15457 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15458 } 15459 /* If app asked for tclass and it has changed ... */ 15460 if (addflag & TCP_IPV6_RECVTCLASS) { 15461 toh = (struct T_opthdr *)optptr; 15462 toh->level = IPPROTO_IPV6; 15463 toh->name = IPV6_TCLASS; 15464 toh->len = sizeof (*toh) + sizeof (uint_t); 15465 toh->status = 0; 15466 optptr += sizeof (*toh); 15467 *(uint_t *)optptr = ipp->ipp_tclass; 15468 optptr += sizeof (uint_t); 15469 ASSERT(OK_32PTR(optptr)); 15470 /* Save as "last" value */ 15471 tcp->tcp_recvtclass = ipp->ipp_tclass; 15472 } 15473 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15474 toh = (struct T_opthdr *)optptr; 15475 toh->level = IPPROTO_IPV6; 15476 toh->name = IPV6_HOPOPTS; 15477 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15478 tcp->tcp_label_len; 15479 toh->status = 0; 15480 optptr += sizeof (*toh); 15481 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15482 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15483 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15484 ASSERT(OK_32PTR(optptr)); 15485 /* Save as last value */ 15486 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15487 (ipp->ipp_fields & IPPF_HOPOPTS), 15488 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15489 } 15490 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15491 toh = (struct T_opthdr *)optptr; 15492 toh->level = IPPROTO_IPV6; 15493 toh->name = IPV6_RTHDRDSTOPTS; 15494 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15495 toh->status = 0; 15496 optptr += sizeof (*toh); 15497 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15498 optptr += ipp->ipp_rtdstoptslen; 15499 ASSERT(OK_32PTR(optptr)); 15500 /* Save as last value */ 15501 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15502 &tcp->tcp_rtdstoptslen, 15503 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15504 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15505 } 15506 if (addflag & TCP_IPV6_RECVRTHDR) { 15507 toh = (struct T_opthdr *)optptr; 15508 toh->level = IPPROTO_IPV6; 15509 toh->name = IPV6_RTHDR; 15510 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15511 toh->status = 0; 15512 optptr += sizeof (*toh); 15513 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15514 optptr += ipp->ipp_rthdrlen; 15515 ASSERT(OK_32PTR(optptr)); 15516 /* Save as last value */ 15517 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15518 (ipp->ipp_fields & IPPF_RTHDR), 15519 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15520 } 15521 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15522 toh = (struct T_opthdr *)optptr; 15523 toh->level = IPPROTO_IPV6; 15524 toh->name = IPV6_DSTOPTS; 15525 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15526 toh->status = 0; 15527 optptr += sizeof (*toh); 15528 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15529 optptr += ipp->ipp_dstoptslen; 15530 ASSERT(OK_32PTR(optptr)); 15531 /* Save as last value */ 15532 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15533 (ipp->ipp_fields & IPPF_DSTOPTS), 15534 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15535 } 15536 ASSERT(optptr == mp->b_wptr); 15537 return (mp); 15538 } 15539 15540 /* 15541 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15542 * messages. 15543 */ 15544 void 15545 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15546 { 15547 uchar_t *rptr = mp->b_rptr; 15548 queue_t *q = tcp->tcp_rq; 15549 struct T_error_ack *tea; 15550 15551 switch (mp->b_datap->db_type) { 15552 case M_PROTO: 15553 case M_PCPROTO: 15554 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15555 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15556 break; 15557 tea = (struct T_error_ack *)rptr; 15558 ASSERT(tea->PRIM_type != T_BIND_ACK); 15559 ASSERT(tea->ERROR_prim != O_T_BIND_REQ && 15560 tea->ERROR_prim != T_BIND_REQ); 15561 switch (tea->PRIM_type) { 15562 case T_ERROR_ACK: 15563 if (tcp->tcp_debug) { 15564 (void) strlog(TCP_MOD_ID, 0, 1, 15565 SL_TRACE|SL_ERROR, 15566 "tcp_rput_other: case T_ERROR_ACK, " 15567 "ERROR_prim == %d", 15568 tea->ERROR_prim); 15569 } 15570 switch (tea->ERROR_prim) { 15571 case T_SVR4_OPTMGMT_REQ: 15572 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15573 /* T_OPTMGMT_REQ generated by TCP */ 15574 printf("T_SVR4_OPTMGMT_REQ failed " 15575 "%d/%d - dropped (cnt %d)\n", 15576 tea->TLI_error, tea->UNIX_error, 15577 tcp->tcp_drop_opt_ack_cnt); 15578 freemsg(mp); 15579 tcp->tcp_drop_opt_ack_cnt--; 15580 return; 15581 } 15582 break; 15583 } 15584 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15585 tcp->tcp_drop_opt_ack_cnt > 0) { 15586 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15587 "- dropped (cnt %d)\n", 15588 tea->TLI_error, tea->UNIX_error, 15589 tcp->tcp_drop_opt_ack_cnt); 15590 freemsg(mp); 15591 tcp->tcp_drop_opt_ack_cnt--; 15592 return; 15593 } 15594 break; 15595 case T_OPTMGMT_ACK: 15596 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15597 /* T_OPTMGMT_REQ generated by TCP */ 15598 freemsg(mp); 15599 tcp->tcp_drop_opt_ack_cnt--; 15600 return; 15601 } 15602 break; 15603 default: 15604 ASSERT(tea->ERROR_prim != T_UNBIND_REQ); 15605 break; 15606 } 15607 break; 15608 case M_FLUSH: 15609 if (*rptr & FLUSHR) 15610 flushq(q, FLUSHDATA); 15611 break; 15612 default: 15613 /* M_CTL will be directly sent to tcp_icmp_error() */ 15614 ASSERT(DB_TYPE(mp) != M_CTL); 15615 break; 15616 } 15617 /* 15618 * Make sure we set this bit before sending the ACK for 15619 * bind. Otherwise accept could possibly run and free 15620 * this tcp struct. 15621 */ 15622 ASSERT(q != NULL); 15623 putnext(q, mp); 15624 } 15625 15626 /* ARGSUSED */ 15627 static void 15628 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15629 { 15630 conn_t *connp = (conn_t *)arg; 15631 tcp_t *tcp = connp->conn_tcp; 15632 queue_t *q = tcp->tcp_rq; 15633 tcp_stack_t *tcps = tcp->tcp_tcps; 15634 15635 ASSERT(!IPCL_IS_NONSTR(connp)); 15636 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15637 tcp->tcp_rsrv_mp = mp; 15638 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15639 15640 TCP_STAT(tcps, tcp_rsrv_calls); 15641 15642 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15643 return; 15644 } 15645 15646 if (tcp->tcp_fused) { 15647 tcp_fuse_backenable(tcp); 15648 return; 15649 } 15650 15651 if (canputnext(q)) { 15652 /* Not flow-controlled, open rwnd */ 15653 tcp->tcp_rwnd = q->q_hiwat; 15654 15655 /* 15656 * Send back a window update immediately if TCP is above 15657 * ESTABLISHED state and the increase of the rcv window 15658 * that the other side knows is at least 1 MSS after flow 15659 * control is lifted. 15660 */ 15661 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15662 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 15663 tcp_xmit_ctl(NULL, tcp, 15664 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15665 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15666 } 15667 } 15668 } 15669 15670 /* 15671 * The read side service routine is called mostly when we get back-enabled as a 15672 * result of flow control relief. Since we don't actually queue anything in 15673 * TCP, we have no data to send out of here. What we do is clear the receive 15674 * window, and send out a window update. 15675 */ 15676 static void 15677 tcp_rsrv(queue_t *q) 15678 { 15679 conn_t *connp = Q_TO_CONN(q); 15680 tcp_t *tcp = connp->conn_tcp; 15681 mblk_t *mp; 15682 tcp_stack_t *tcps = tcp->tcp_tcps; 15683 15684 /* No code does a putq on the read side */ 15685 ASSERT(q->q_first == NULL); 15686 15687 /* Nothing to do for the default queue */ 15688 if (q == tcps->tcps_g_q) { 15689 return; 15690 } 15691 15692 /* 15693 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 15694 * been run. So just return. 15695 */ 15696 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15697 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 15698 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15699 return; 15700 } 15701 tcp->tcp_rsrv_mp = NULL; 15702 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15703 15704 CONN_INC_REF(connp); 15705 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp, 15706 SQ_PROCESS, SQTAG_TCP_RSRV); 15707 } 15708 15709 /* 15710 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 15711 * We do not allow the receive window to shrink. After setting rwnd, 15712 * set the flow control hiwat of the stream. 15713 * 15714 * This function is called in 2 cases: 15715 * 15716 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 15717 * connection (passive open) and in tcp_rput_data() for active connect. 15718 * This is called after tcp_mss_set() when the desired MSS value is known. 15719 * This makes sure that our window size is a mutiple of the other side's 15720 * MSS. 15721 * 2) Handling SO_RCVBUF option. 15722 * 15723 * It is ASSUMED that the requested size is a multiple of the current MSS. 15724 * 15725 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 15726 * user requests so. 15727 */ 15728 static int 15729 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 15730 { 15731 uint32_t mss = tcp->tcp_mss; 15732 uint32_t old_max_rwnd; 15733 uint32_t max_transmittable_rwnd; 15734 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 15735 tcp_stack_t *tcps = tcp->tcp_tcps; 15736 15737 if (tcp->tcp_fused) { 15738 size_t sth_hiwat; 15739 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15740 15741 ASSERT(peer_tcp != NULL); 15742 /* 15743 * Record the stream head's high water mark for 15744 * this endpoint; this is used for flow-control 15745 * purposes in tcp_fuse_output(). 15746 */ 15747 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 15748 if (!tcp_detached) { 15749 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 15750 sth_hiwat); 15751 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 15752 conn_t *connp = tcp->tcp_connp; 15753 struct sock_proto_props sopp; 15754 15755 sopp.sopp_flags = SOCKOPT_RCVTHRESH; 15756 sopp.sopp_rcvthresh = sth_hiwat >> 3; 15757 15758 (*connp->conn_upcalls->su_set_proto_props) 15759 (connp->conn_upper_handle, &sopp); 15760 } 15761 } 15762 15763 /* 15764 * In the fusion case, the maxpsz stream head value of 15765 * our peer is set according to its send buffer size 15766 * and our receive buffer size; since the latter may 15767 * have changed we need to update the peer's maxpsz. 15768 */ 15769 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 15770 return (rwnd); 15771 } 15772 15773 if (tcp_detached) { 15774 old_max_rwnd = tcp->tcp_rwnd; 15775 } else { 15776 old_max_rwnd = tcp->tcp_recv_hiwater; 15777 } 15778 15779 /* 15780 * Insist on a receive window that is at least 15781 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 15782 * funny TCP interactions of Nagle algorithm, SWS avoidance 15783 * and delayed acknowledgement. 15784 */ 15785 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 15786 15787 /* 15788 * If window size info has already been exchanged, TCP should not 15789 * shrink the window. Shrinking window is doable if done carefully. 15790 * We may add that support later. But so far there is not a real 15791 * need to do that. 15792 */ 15793 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 15794 /* MSS may have changed, do a round up again. */ 15795 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 15796 } 15797 15798 /* 15799 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 15800 * can be applied even before the window scale option is decided. 15801 */ 15802 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 15803 if (rwnd > max_transmittable_rwnd) { 15804 rwnd = max_transmittable_rwnd - 15805 (max_transmittable_rwnd % mss); 15806 if (rwnd < mss) 15807 rwnd = max_transmittable_rwnd; 15808 /* 15809 * If we're over the limit we may have to back down tcp_rwnd. 15810 * The increment below won't work for us. So we set all three 15811 * here and the increment below will have no effect. 15812 */ 15813 tcp->tcp_rwnd = old_max_rwnd = rwnd; 15814 } 15815 if (tcp->tcp_localnet) { 15816 tcp->tcp_rack_abs_max = 15817 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 15818 } else { 15819 /* 15820 * For a remote host on a different subnet (through a router), 15821 * we ack every other packet to be conforming to RFC1122. 15822 * tcp_deferred_acks_max is default to 2. 15823 */ 15824 tcp->tcp_rack_abs_max = 15825 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 15826 } 15827 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 15828 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15829 else 15830 tcp->tcp_rack_cur_max = 0; 15831 /* 15832 * Increment the current rwnd by the amount the maximum grew (we 15833 * can not overwrite it since we might be in the middle of a 15834 * connection.) 15835 */ 15836 tcp->tcp_rwnd += rwnd - old_max_rwnd; 15837 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 15838 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 15839 tcp->tcp_cwnd_max = rwnd; 15840 15841 if (tcp_detached) 15842 return (rwnd); 15843 /* 15844 * We set the maximum receive window into rq->q_hiwat if it is 15845 * a STREAMS socket. 15846 * This is not actually used for flow control. 15847 */ 15848 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 15849 tcp->tcp_rq->q_hiwat = rwnd; 15850 tcp->tcp_recv_hiwater = rwnd; 15851 /* 15852 * Set the STREAM head high water mark. This doesn't have to be 15853 * here, since we are simply using default values, but we would 15854 * prefer to choose these values algorithmically, with a likely 15855 * relationship to rwnd. 15856 */ 15857 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 15858 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 15859 return (rwnd); 15860 } 15861 15862 /* 15863 * Return SNMP stuff in buffer in mpdata. 15864 */ 15865 mblk_t * 15866 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 15867 { 15868 mblk_t *mpdata; 15869 mblk_t *mp_conn_ctl = NULL; 15870 mblk_t *mp_conn_tail; 15871 mblk_t *mp_attr_ctl = NULL; 15872 mblk_t *mp_attr_tail; 15873 mblk_t *mp6_conn_ctl = NULL; 15874 mblk_t *mp6_conn_tail; 15875 mblk_t *mp6_attr_ctl = NULL; 15876 mblk_t *mp6_attr_tail; 15877 struct opthdr *optp; 15878 mib2_tcpConnEntry_t tce; 15879 mib2_tcp6ConnEntry_t tce6; 15880 mib2_transportMLPEntry_t mlp; 15881 connf_t *connfp; 15882 int i; 15883 boolean_t ispriv; 15884 zoneid_t zoneid; 15885 int v4_conn_idx; 15886 int v6_conn_idx; 15887 conn_t *connp = Q_TO_CONN(q); 15888 tcp_stack_t *tcps; 15889 ip_stack_t *ipst; 15890 mblk_t *mp2ctl; 15891 15892 /* 15893 * make a copy of the original message 15894 */ 15895 mp2ctl = copymsg(mpctl); 15896 15897 if (mpctl == NULL || 15898 (mpdata = mpctl->b_cont) == NULL || 15899 (mp_conn_ctl = copymsg(mpctl)) == NULL || 15900 (mp_attr_ctl = copymsg(mpctl)) == NULL || 15901 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 15902 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 15903 freemsg(mp_conn_ctl); 15904 freemsg(mp_attr_ctl); 15905 freemsg(mp6_conn_ctl); 15906 freemsg(mp6_attr_ctl); 15907 freemsg(mpctl); 15908 freemsg(mp2ctl); 15909 return (NULL); 15910 } 15911 15912 ipst = connp->conn_netstack->netstack_ip; 15913 tcps = connp->conn_netstack->netstack_tcp; 15914 15915 /* build table of connections -- need count in fixed part */ 15916 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 15917 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 15918 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 15919 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 15920 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 15921 15922 ispriv = 15923 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 15924 zoneid = Q_TO_CONN(q)->conn_zoneid; 15925 15926 v4_conn_idx = v6_conn_idx = 0; 15927 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 15928 15929 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 15930 ipst = tcps->tcps_netstack->netstack_ip; 15931 15932 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 15933 15934 connp = NULL; 15935 15936 while ((connp = 15937 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 15938 tcp_t *tcp; 15939 boolean_t needattr; 15940 15941 if (connp->conn_zoneid != zoneid) 15942 continue; /* not in this zone */ 15943 15944 tcp = connp->conn_tcp; 15945 UPDATE_MIB(&tcps->tcps_mib, 15946 tcpHCInSegs, tcp->tcp_ibsegs); 15947 tcp->tcp_ibsegs = 0; 15948 UPDATE_MIB(&tcps->tcps_mib, 15949 tcpHCOutSegs, tcp->tcp_obsegs); 15950 tcp->tcp_obsegs = 0; 15951 15952 tce6.tcp6ConnState = tce.tcpConnState = 15953 tcp_snmp_state(tcp); 15954 if (tce.tcpConnState == MIB2_TCP_established || 15955 tce.tcpConnState == MIB2_TCP_closeWait) 15956 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 15957 15958 needattr = B_FALSE; 15959 bzero(&mlp, sizeof (mlp)); 15960 if (connp->conn_mlp_type != mlptSingle) { 15961 if (connp->conn_mlp_type == mlptShared || 15962 connp->conn_mlp_type == mlptBoth) 15963 mlp.tme_flags |= MIB2_TMEF_SHARED; 15964 if (connp->conn_mlp_type == mlptPrivate || 15965 connp->conn_mlp_type == mlptBoth) 15966 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 15967 needattr = B_TRUE; 15968 } 15969 if (connp->conn_anon_mlp) { 15970 mlp.tme_flags |= MIB2_TMEF_ANONMLP; 15971 needattr = B_TRUE; 15972 } 15973 if (connp->conn_mac_exempt) { 15974 mlp.tme_flags |= MIB2_TMEF_MACEXEMPT; 15975 needattr = B_TRUE; 15976 } 15977 if (connp->conn_fully_bound && 15978 connp->conn_effective_cred != NULL) { 15979 ts_label_t *tsl; 15980 15981 tsl = crgetlabel(connp->conn_effective_cred); 15982 mlp.tme_flags |= MIB2_TMEF_IS_LABELED; 15983 mlp.tme_doi = label2doi(tsl); 15984 mlp.tme_label = *label2bslabel(tsl); 15985 needattr = B_TRUE; 15986 } 15987 15988 /* Create a message to report on IPv6 entries */ 15989 if (tcp->tcp_ipversion == IPV6_VERSION) { 15990 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 15991 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 15992 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 15993 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 15994 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 15995 /* Don't want just anybody seeing these... */ 15996 if (ispriv) { 15997 tce6.tcp6ConnEntryInfo.ce_snxt = 15998 tcp->tcp_snxt; 15999 tce6.tcp6ConnEntryInfo.ce_suna = 16000 tcp->tcp_suna; 16001 tce6.tcp6ConnEntryInfo.ce_rnxt = 16002 tcp->tcp_rnxt; 16003 tce6.tcp6ConnEntryInfo.ce_rack = 16004 tcp->tcp_rack; 16005 } else { 16006 /* 16007 * Netstat, unfortunately, uses this to 16008 * get send/receive queue sizes. How to fix? 16009 * Why not compute the difference only? 16010 */ 16011 tce6.tcp6ConnEntryInfo.ce_snxt = 16012 tcp->tcp_snxt - tcp->tcp_suna; 16013 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16014 tce6.tcp6ConnEntryInfo.ce_rnxt = 16015 tcp->tcp_rnxt - tcp->tcp_rack; 16016 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16017 } 16018 16019 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16020 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16021 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16022 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16023 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16024 16025 tce6.tcp6ConnCreationProcess = 16026 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16027 tcp->tcp_cpid; 16028 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16029 16030 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16031 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16032 16033 mlp.tme_connidx = v6_conn_idx++; 16034 if (needattr) 16035 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16036 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16037 } 16038 /* 16039 * Create an IPv4 table entry for IPv4 entries and also 16040 * for IPv6 entries which are bound to in6addr_any 16041 * but don't have IPV6_V6ONLY set. 16042 * (i.e. anything an IPv4 peer could connect to) 16043 */ 16044 if (tcp->tcp_ipversion == IPV4_VERSION || 16045 (tcp->tcp_state <= TCPS_LISTEN && 16046 !tcp->tcp_connp->conn_ipv6_v6only && 16047 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16048 if (tcp->tcp_ipversion == IPV6_VERSION) { 16049 tce.tcpConnRemAddress = INADDR_ANY; 16050 tce.tcpConnLocalAddress = INADDR_ANY; 16051 } else { 16052 tce.tcpConnRemAddress = 16053 tcp->tcp_remote; 16054 tce.tcpConnLocalAddress = 16055 tcp->tcp_ip_src; 16056 } 16057 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16058 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16059 /* Don't want just anybody seeing these... */ 16060 if (ispriv) { 16061 tce.tcpConnEntryInfo.ce_snxt = 16062 tcp->tcp_snxt; 16063 tce.tcpConnEntryInfo.ce_suna = 16064 tcp->tcp_suna; 16065 tce.tcpConnEntryInfo.ce_rnxt = 16066 tcp->tcp_rnxt; 16067 tce.tcpConnEntryInfo.ce_rack = 16068 tcp->tcp_rack; 16069 } else { 16070 /* 16071 * Netstat, unfortunately, uses this to 16072 * get send/receive queue sizes. How 16073 * to fix? 16074 * Why not compute the difference only? 16075 */ 16076 tce.tcpConnEntryInfo.ce_snxt = 16077 tcp->tcp_snxt - tcp->tcp_suna; 16078 tce.tcpConnEntryInfo.ce_suna = 0; 16079 tce.tcpConnEntryInfo.ce_rnxt = 16080 tcp->tcp_rnxt - tcp->tcp_rack; 16081 tce.tcpConnEntryInfo.ce_rack = 0; 16082 } 16083 16084 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16085 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16086 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16087 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16088 tce.tcpConnEntryInfo.ce_state = 16089 tcp->tcp_state; 16090 16091 tce.tcpConnCreationProcess = 16092 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16093 tcp->tcp_cpid; 16094 tce.tcpConnCreationTime = tcp->tcp_open_time; 16095 16096 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16097 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16098 16099 mlp.tme_connidx = v4_conn_idx++; 16100 if (needattr) 16101 (void) snmp_append_data2( 16102 mp_attr_ctl->b_cont, 16103 &mp_attr_tail, (char *)&mlp, 16104 sizeof (mlp)); 16105 } 16106 } 16107 } 16108 16109 /* fixed length structure for IPv4 and IPv6 counters */ 16110 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16111 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16112 sizeof (mib2_tcp6ConnEntry_t)); 16113 /* synchronize 32- and 64-bit counters */ 16114 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16115 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16116 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16117 optp->level = MIB2_TCP; 16118 optp->name = 0; 16119 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16120 sizeof (tcps->tcps_mib)); 16121 optp->len = msgdsize(mpdata); 16122 qreply(q, mpctl); 16123 16124 /* table of connections... */ 16125 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16126 sizeof (struct T_optmgmt_ack)]; 16127 optp->level = MIB2_TCP; 16128 optp->name = MIB2_TCP_CONN; 16129 optp->len = msgdsize(mp_conn_ctl->b_cont); 16130 qreply(q, mp_conn_ctl); 16131 16132 /* table of MLP attributes... */ 16133 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16134 sizeof (struct T_optmgmt_ack)]; 16135 optp->level = MIB2_TCP; 16136 optp->name = EXPER_XPORT_MLP; 16137 optp->len = msgdsize(mp_attr_ctl->b_cont); 16138 if (optp->len == 0) 16139 freemsg(mp_attr_ctl); 16140 else 16141 qreply(q, mp_attr_ctl); 16142 16143 /* table of IPv6 connections... */ 16144 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16145 sizeof (struct T_optmgmt_ack)]; 16146 optp->level = MIB2_TCP6; 16147 optp->name = MIB2_TCP6_CONN; 16148 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16149 qreply(q, mp6_conn_ctl); 16150 16151 /* table of IPv6 MLP attributes... */ 16152 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16153 sizeof (struct T_optmgmt_ack)]; 16154 optp->level = MIB2_TCP6; 16155 optp->name = EXPER_XPORT_MLP; 16156 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16157 if (optp->len == 0) 16158 freemsg(mp6_attr_ctl); 16159 else 16160 qreply(q, mp6_attr_ctl); 16161 return (mp2ctl); 16162 } 16163 16164 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16165 /* ARGSUSED */ 16166 int 16167 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16168 { 16169 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16170 16171 switch (level) { 16172 case MIB2_TCP: 16173 switch (name) { 16174 case 13: 16175 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16176 return (0); 16177 /* TODO: delete entry defined by tce */ 16178 return (1); 16179 default: 16180 return (0); 16181 } 16182 default: 16183 return (1); 16184 } 16185 } 16186 16187 /* Translate TCP state to MIB2 TCP state. */ 16188 static int 16189 tcp_snmp_state(tcp_t *tcp) 16190 { 16191 if (tcp == NULL) 16192 return (0); 16193 16194 switch (tcp->tcp_state) { 16195 case TCPS_CLOSED: 16196 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16197 case TCPS_BOUND: 16198 return (MIB2_TCP_closed); 16199 case TCPS_LISTEN: 16200 return (MIB2_TCP_listen); 16201 case TCPS_SYN_SENT: 16202 return (MIB2_TCP_synSent); 16203 case TCPS_SYN_RCVD: 16204 return (MIB2_TCP_synReceived); 16205 case TCPS_ESTABLISHED: 16206 return (MIB2_TCP_established); 16207 case TCPS_CLOSE_WAIT: 16208 return (MIB2_TCP_closeWait); 16209 case TCPS_FIN_WAIT_1: 16210 return (MIB2_TCP_finWait1); 16211 case TCPS_CLOSING: 16212 return (MIB2_TCP_closing); 16213 case TCPS_LAST_ACK: 16214 return (MIB2_TCP_lastAck); 16215 case TCPS_FIN_WAIT_2: 16216 return (MIB2_TCP_finWait2); 16217 case TCPS_TIME_WAIT: 16218 return (MIB2_TCP_timeWait); 16219 default: 16220 return (0); 16221 } 16222 } 16223 16224 /* 16225 * tcp_timer is the timer service routine. It handles the retransmission, 16226 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16227 * from the state of the tcp instance what kind of action needs to be done 16228 * at the time it is called. 16229 */ 16230 static void 16231 tcp_timer(void *arg) 16232 { 16233 mblk_t *mp; 16234 clock_t first_threshold; 16235 clock_t second_threshold; 16236 clock_t ms; 16237 uint32_t mss; 16238 conn_t *connp = (conn_t *)arg; 16239 tcp_t *tcp = connp->conn_tcp; 16240 tcp_stack_t *tcps = tcp->tcp_tcps; 16241 16242 tcp->tcp_timer_tid = 0; 16243 16244 if (tcp->tcp_fused) 16245 return; 16246 16247 first_threshold = tcp->tcp_first_timer_threshold; 16248 second_threshold = tcp->tcp_second_timer_threshold; 16249 switch (tcp->tcp_state) { 16250 case TCPS_IDLE: 16251 case TCPS_BOUND: 16252 case TCPS_LISTEN: 16253 return; 16254 case TCPS_SYN_RCVD: { 16255 tcp_t *listener = tcp->tcp_listener; 16256 16257 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16258 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16259 /* it's our first timeout */ 16260 tcp->tcp_syn_rcvd_timeout = 1; 16261 mutex_enter(&listener->tcp_eager_lock); 16262 listener->tcp_syn_rcvd_timeout++; 16263 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 16264 /* 16265 * Make this eager available for drop if we 16266 * need to drop one to accomodate a new 16267 * incoming SYN request. 16268 */ 16269 MAKE_DROPPABLE(listener, tcp); 16270 } 16271 if (!listener->tcp_syn_defense && 16272 (listener->tcp_syn_rcvd_timeout > 16273 (tcps->tcps_conn_req_max_q0 >> 2)) && 16274 (tcps->tcps_conn_req_max_q0 > 200)) { 16275 /* We may be under attack. Put on a defense. */ 16276 listener->tcp_syn_defense = B_TRUE; 16277 cmn_err(CE_WARN, "High TCP connect timeout " 16278 "rate! System (port %d) may be under a " 16279 "SYN flood attack!", 16280 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16281 16282 listener->tcp_ip_addr_cache = kmem_zalloc( 16283 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16284 KM_NOSLEEP); 16285 } 16286 mutex_exit(&listener->tcp_eager_lock); 16287 } else if (listener != NULL) { 16288 mutex_enter(&listener->tcp_eager_lock); 16289 tcp->tcp_syn_rcvd_timeout++; 16290 if (tcp->tcp_syn_rcvd_timeout > 1 && 16291 !tcp->tcp_closemp_used) { 16292 /* 16293 * This is our second timeout. Put the tcp in 16294 * the list of droppable eagers to allow it to 16295 * be dropped, if needed. We don't check 16296 * whether tcp_dontdrop is set or not to 16297 * protect ourselve from a SYN attack where a 16298 * remote host can spoof itself as one of the 16299 * good IP source and continue to hold 16300 * resources too long. 16301 */ 16302 MAKE_DROPPABLE(listener, tcp); 16303 } 16304 mutex_exit(&listener->tcp_eager_lock); 16305 } 16306 } 16307 /* FALLTHRU */ 16308 case TCPS_SYN_SENT: 16309 first_threshold = tcp->tcp_first_ctimer_threshold; 16310 second_threshold = tcp->tcp_second_ctimer_threshold; 16311 break; 16312 case TCPS_ESTABLISHED: 16313 case TCPS_FIN_WAIT_1: 16314 case TCPS_CLOSING: 16315 case TCPS_CLOSE_WAIT: 16316 case TCPS_LAST_ACK: 16317 /* If we have data to rexmit */ 16318 if (tcp->tcp_suna != tcp->tcp_snxt) { 16319 clock_t time_to_wait; 16320 16321 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 16322 if (!tcp->tcp_xmit_head) 16323 break; 16324 time_to_wait = lbolt - 16325 (clock_t)tcp->tcp_xmit_head->b_prev; 16326 time_to_wait = tcp->tcp_rto - 16327 TICK_TO_MSEC(time_to_wait); 16328 /* 16329 * If the timer fires too early, 1 clock tick earlier, 16330 * restart the timer. 16331 */ 16332 if (time_to_wait > msec_per_tick) { 16333 TCP_STAT(tcps, tcp_timer_fire_early); 16334 TCP_TIMER_RESTART(tcp, time_to_wait); 16335 return; 16336 } 16337 /* 16338 * When we probe zero windows, we force the swnd open. 16339 * If our peer acks with a closed window swnd will be 16340 * set to zero by tcp_rput(). As long as we are 16341 * receiving acks tcp_rput will 16342 * reset 'tcp_ms_we_have_waited' so as not to trip the 16343 * first and second interval actions. NOTE: the timer 16344 * interval is allowed to continue its exponential 16345 * backoff. 16346 */ 16347 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 16348 if (tcp->tcp_debug) { 16349 (void) strlog(TCP_MOD_ID, 0, 1, 16350 SL_TRACE, "tcp_timer: zero win"); 16351 } 16352 } else { 16353 /* 16354 * After retransmission, we need to do 16355 * slow start. Set the ssthresh to one 16356 * half of current effective window and 16357 * cwnd to one MSS. Also reset 16358 * tcp_cwnd_cnt. 16359 * 16360 * Note that if tcp_ssthresh is reduced because 16361 * of ECN, do not reduce it again unless it is 16362 * already one window of data away (tcp_cwr 16363 * should then be cleared) or this is a 16364 * timeout for a retransmitted segment. 16365 */ 16366 uint32_t npkt; 16367 16368 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 16369 npkt = ((tcp->tcp_timer_backoff ? 16370 tcp->tcp_cwnd_ssthresh : 16371 tcp->tcp_snxt - 16372 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 16373 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 16374 tcp->tcp_mss; 16375 } 16376 tcp->tcp_cwnd = tcp->tcp_mss; 16377 tcp->tcp_cwnd_cnt = 0; 16378 if (tcp->tcp_ecn_ok) { 16379 tcp->tcp_cwr = B_TRUE; 16380 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 16381 tcp->tcp_ecn_cwr_sent = B_FALSE; 16382 } 16383 } 16384 break; 16385 } 16386 /* 16387 * We have something to send yet we cannot send. The 16388 * reason can be: 16389 * 16390 * 1. Zero send window: we need to do zero window probe. 16391 * 2. Zero cwnd: because of ECN, we need to "clock out 16392 * segments. 16393 * 3. SWS avoidance: receiver may have shrunk window, 16394 * reset our knowledge. 16395 * 16396 * Note that condition 2 can happen with either 1 or 16397 * 3. But 1 and 3 are exclusive. 16398 */ 16399 if (tcp->tcp_unsent != 0) { 16400 if (tcp->tcp_cwnd == 0) { 16401 /* 16402 * Set tcp_cwnd to 1 MSS so that a 16403 * new segment can be sent out. We 16404 * are "clocking out" new data when 16405 * the network is really congested. 16406 */ 16407 ASSERT(tcp->tcp_ecn_ok); 16408 tcp->tcp_cwnd = tcp->tcp_mss; 16409 } 16410 if (tcp->tcp_swnd == 0) { 16411 /* Extend window for zero window probe */ 16412 tcp->tcp_swnd++; 16413 tcp->tcp_zero_win_probe = B_TRUE; 16414 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 16415 } else { 16416 /* 16417 * Handle timeout from sender SWS avoidance. 16418 * Reset our knowledge of the max send window 16419 * since the receiver might have reduced its 16420 * receive buffer. Avoid setting tcp_max_swnd 16421 * to one since that will essentially disable 16422 * the SWS checks. 16423 * 16424 * Note that since we don't have a SWS 16425 * state variable, if the timeout is set 16426 * for ECN but not for SWS, this 16427 * code will also be executed. This is 16428 * fine as tcp_max_swnd is updated 16429 * constantly and it will not affect 16430 * anything. 16431 */ 16432 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 16433 } 16434 tcp_wput_data(tcp, NULL, B_FALSE); 16435 return; 16436 } 16437 /* Is there a FIN that needs to be to re retransmitted? */ 16438 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16439 !tcp->tcp_fin_acked) 16440 break; 16441 /* Nothing to do, return without restarting timer. */ 16442 TCP_STAT(tcps, tcp_timer_fire_miss); 16443 return; 16444 case TCPS_FIN_WAIT_2: 16445 /* 16446 * User closed the TCP endpoint and peer ACK'ed our FIN. 16447 * We waited some time for for peer's FIN, but it hasn't 16448 * arrived. We flush the connection now to avoid 16449 * case where the peer has rebooted. 16450 */ 16451 if (TCP_IS_DETACHED(tcp)) { 16452 (void) tcp_clean_death(tcp, 0, 23); 16453 } else { 16454 TCP_TIMER_RESTART(tcp, 16455 tcps->tcps_fin_wait_2_flush_interval); 16456 } 16457 return; 16458 case TCPS_TIME_WAIT: 16459 (void) tcp_clean_death(tcp, 0, 24); 16460 return; 16461 default: 16462 if (tcp->tcp_debug) { 16463 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 16464 "tcp_timer: strange state (%d) %s", 16465 tcp->tcp_state, tcp_display(tcp, NULL, 16466 DISP_PORT_ONLY)); 16467 } 16468 return; 16469 } 16470 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 16471 /* 16472 * For zero window probe, we need to send indefinitely, 16473 * unless we have not heard from the other side for some 16474 * time... 16475 */ 16476 if ((tcp->tcp_zero_win_probe == 0) || 16477 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 16478 second_threshold)) { 16479 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 16480 /* 16481 * If TCP is in SYN_RCVD state, send back a 16482 * RST|ACK as BSD does. Note that tcp_zero_win_probe 16483 * should be zero in TCPS_SYN_RCVD state. 16484 */ 16485 if (tcp->tcp_state == TCPS_SYN_RCVD) { 16486 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 16487 "in SYN_RCVD", 16488 tcp, tcp->tcp_snxt, 16489 tcp->tcp_rnxt, TH_RST | TH_ACK); 16490 } 16491 (void) tcp_clean_death(tcp, 16492 tcp->tcp_client_errno ? 16493 tcp->tcp_client_errno : ETIMEDOUT, 25); 16494 return; 16495 } else { 16496 /* 16497 * Set tcp_ms_we_have_waited to second_threshold 16498 * so that in next timeout, we will do the above 16499 * check (lbolt - tcp_last_recv_time). This is 16500 * also to avoid overflow. 16501 * 16502 * We don't need to decrement tcp_timer_backoff 16503 * to avoid overflow because it will be decremented 16504 * later if new timeout value is greater than 16505 * tcp_rexmit_interval_max. In the case when 16506 * tcp_rexmit_interval_max is greater than 16507 * second_threshold, it means that we will wait 16508 * longer than second_threshold to send the next 16509 * window probe. 16510 */ 16511 tcp->tcp_ms_we_have_waited = second_threshold; 16512 } 16513 } else if (ms > first_threshold) { 16514 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 16515 tcp->tcp_xmit_head != NULL) { 16516 tcp->tcp_xmit_head = 16517 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 16518 } 16519 /* 16520 * We have been retransmitting for too long... The RTT 16521 * we calculated is probably incorrect. Reinitialize it. 16522 * Need to compensate for 0 tcp_rtt_sa. Reset 16523 * tcp_rtt_update so that we won't accidentally cache a 16524 * bad value. But only do this if this is not a zero 16525 * window probe. 16526 */ 16527 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 16528 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 16529 (tcp->tcp_rtt_sa >> 5); 16530 tcp->tcp_rtt_sa = 0; 16531 tcp_ip_notify(tcp); 16532 tcp->tcp_rtt_update = 0; 16533 } 16534 } 16535 tcp->tcp_timer_backoff++; 16536 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 16537 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 16538 tcps->tcps_rexmit_interval_min) { 16539 /* 16540 * This means the original RTO is tcp_rexmit_interval_min. 16541 * So we will use tcp_rexmit_interval_min as the RTO value 16542 * and do the backoff. 16543 */ 16544 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 16545 } else { 16546 ms <<= tcp->tcp_timer_backoff; 16547 } 16548 if (ms > tcps->tcps_rexmit_interval_max) { 16549 ms = tcps->tcps_rexmit_interval_max; 16550 /* 16551 * ms is at max, decrement tcp_timer_backoff to avoid 16552 * overflow. 16553 */ 16554 tcp->tcp_timer_backoff--; 16555 } 16556 tcp->tcp_ms_we_have_waited += ms; 16557 if (tcp->tcp_zero_win_probe == 0) { 16558 tcp->tcp_rto = ms; 16559 } 16560 TCP_TIMER_RESTART(tcp, ms); 16561 /* 16562 * This is after a timeout and tcp_rto is backed off. Set 16563 * tcp_set_timer to 1 so that next time RTO is updated, we will 16564 * restart the timer with a correct value. 16565 */ 16566 tcp->tcp_set_timer = 1; 16567 mss = tcp->tcp_snxt - tcp->tcp_suna; 16568 if (mss > tcp->tcp_mss) 16569 mss = tcp->tcp_mss; 16570 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 16571 mss = tcp->tcp_swnd; 16572 16573 if ((mp = tcp->tcp_xmit_head) != NULL) 16574 mp->b_prev = (mblk_t *)lbolt; 16575 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 16576 B_TRUE); 16577 16578 /* 16579 * When slow start after retransmission begins, start with 16580 * this seq no. tcp_rexmit_max marks the end of special slow 16581 * start phase. tcp_snd_burst controls how many segments 16582 * can be sent because of an ack. 16583 */ 16584 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 16585 tcp->tcp_snd_burst = TCP_CWND_SS; 16586 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16587 (tcp->tcp_unsent == 0)) { 16588 tcp->tcp_rexmit_max = tcp->tcp_fss; 16589 } else { 16590 tcp->tcp_rexmit_max = tcp->tcp_snxt; 16591 } 16592 tcp->tcp_rexmit = B_TRUE; 16593 tcp->tcp_dupack_cnt = 0; 16594 16595 /* 16596 * Remove all rexmit SACK blk to start from fresh. 16597 */ 16598 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) 16599 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp); 16600 if (mp == NULL) { 16601 return; 16602 } 16603 /* 16604 * Attach credentials to retransmitted initial SYNs. 16605 * In theory we should use the credentials from the connect() 16606 * call to ensure that getpeerucred() on the peer will be correct. 16607 * But we assume that SYN's are not dropped for loopback connections. 16608 */ 16609 if (tcp->tcp_state == TCPS_SYN_SENT) { 16610 mblk_setcred(mp, CONN_CRED(tcp->tcp_connp), tcp->tcp_cpid); 16611 } 16612 16613 tcp->tcp_csuna = tcp->tcp_snxt; 16614 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 16615 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 16616 tcp_send_data(tcp, tcp->tcp_wq, mp); 16617 16618 } 16619 16620 static int 16621 tcp_do_unbind(conn_t *connp) 16622 { 16623 tcp_t *tcp = connp->conn_tcp; 16624 int error = 0; 16625 16626 switch (tcp->tcp_state) { 16627 case TCPS_BOUND: 16628 case TCPS_LISTEN: 16629 break; 16630 default: 16631 return (-TOUTSTATE); 16632 } 16633 16634 /* 16635 * Need to clean up all the eagers since after the unbind, segments 16636 * will no longer be delivered to this listener stream. 16637 */ 16638 mutex_enter(&tcp->tcp_eager_lock); 16639 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 16640 tcp_eager_cleanup(tcp, 0); 16641 } 16642 mutex_exit(&tcp->tcp_eager_lock); 16643 16644 if (tcp->tcp_ipversion == IPV4_VERSION) { 16645 tcp->tcp_ipha->ipha_src = 0; 16646 } else { 16647 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 16648 } 16649 V6_SET_ZERO(tcp->tcp_ip_src_v6); 16650 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 16651 tcp_bind_hash_remove(tcp); 16652 tcp->tcp_state = TCPS_IDLE; 16653 tcp->tcp_mdt = B_FALSE; 16654 16655 connp = tcp->tcp_connp; 16656 connp->conn_mdt_ok = B_FALSE; 16657 ipcl_hash_remove(connp); 16658 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 16659 16660 return (error); 16661 } 16662 16663 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 16664 static void 16665 tcp_tpi_unbind(tcp_t *tcp, mblk_t *mp) 16666 { 16667 int error = tcp_do_unbind(tcp->tcp_connp); 16668 16669 if (error > 0) { 16670 tcp_err_ack(tcp, mp, TSYSERR, error); 16671 } else if (error < 0) { 16672 tcp_err_ack(tcp, mp, -error, 0); 16673 } else { 16674 /* Send M_FLUSH according to TPI */ 16675 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 16676 16677 mp = mi_tpi_ok_ack_alloc(mp); 16678 putnext(tcp->tcp_rq, mp); 16679 } 16680 } 16681 16682 /* 16683 * Don't let port fall into the privileged range. 16684 * Since the extra privileged ports can be arbitrary we also 16685 * ensure that we exclude those from consideration. 16686 * tcp_g_epriv_ports is not sorted thus we loop over it until 16687 * there are no changes. 16688 * 16689 * Note: No locks are held when inspecting tcp_g_*epriv_ports 16690 * but instead the code relies on: 16691 * - the fact that the address of the array and its size never changes 16692 * - the atomic assignment of the elements of the array 16693 * 16694 * Returns 0 if there are no more ports available. 16695 * 16696 * TS note: skip multilevel ports. 16697 */ 16698 static in_port_t 16699 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 16700 { 16701 int i; 16702 boolean_t restart = B_FALSE; 16703 tcp_stack_t *tcps = tcp->tcp_tcps; 16704 16705 if (random && tcp_random_anon_port != 0) { 16706 (void) random_get_pseudo_bytes((uint8_t *)&port, 16707 sizeof (in_port_t)); 16708 /* 16709 * Unless changed by a sys admin, the smallest anon port 16710 * is 32768 and the largest anon port is 65535. It is 16711 * very likely (50%) for the random port to be smaller 16712 * than the smallest anon port. When that happens, 16713 * add port % (anon port range) to the smallest anon 16714 * port to get the random port. It should fall into the 16715 * valid anon port range. 16716 */ 16717 if (port < tcps->tcps_smallest_anon_port) { 16718 port = tcps->tcps_smallest_anon_port + 16719 port % (tcps->tcps_largest_anon_port - 16720 tcps->tcps_smallest_anon_port); 16721 } 16722 } 16723 16724 retry: 16725 if (port < tcps->tcps_smallest_anon_port) 16726 port = (in_port_t)tcps->tcps_smallest_anon_port; 16727 16728 if (port > tcps->tcps_largest_anon_port) { 16729 if (restart) 16730 return (0); 16731 restart = B_TRUE; 16732 port = (in_port_t)tcps->tcps_smallest_anon_port; 16733 } 16734 16735 if (port < tcps->tcps_smallest_nonpriv_port) 16736 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 16737 16738 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 16739 if (port == tcps->tcps_g_epriv_ports[i]) { 16740 port++; 16741 /* 16742 * Make sure whether the port is in the 16743 * valid range. 16744 */ 16745 goto retry; 16746 } 16747 } 16748 if (is_system_labeled() && 16749 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 16750 IPPROTO_TCP, B_TRUE)) != 0) { 16751 port = i; 16752 goto retry; 16753 } 16754 return (port); 16755 } 16756 16757 /* 16758 * Return the next anonymous port in the privileged port range for 16759 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 16760 * downwards. This is the same behavior as documented in the userland 16761 * library call rresvport(3N). 16762 * 16763 * TS note: skip multilevel ports. 16764 */ 16765 static in_port_t 16766 tcp_get_next_priv_port(const tcp_t *tcp) 16767 { 16768 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 16769 in_port_t nextport; 16770 boolean_t restart = B_FALSE; 16771 tcp_stack_t *tcps = tcp->tcp_tcps; 16772 retry: 16773 if (next_priv_port < tcps->tcps_min_anonpriv_port || 16774 next_priv_port >= IPPORT_RESERVED) { 16775 next_priv_port = IPPORT_RESERVED - 1; 16776 if (restart) 16777 return (0); 16778 restart = B_TRUE; 16779 } 16780 if (is_system_labeled() && 16781 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 16782 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 16783 next_priv_port = nextport; 16784 goto retry; 16785 } 16786 return (next_priv_port--); 16787 } 16788 16789 /* The write side r/w procedure. */ 16790 16791 #if CCS_STATS 16792 struct { 16793 struct { 16794 int64_t count, bytes; 16795 } tot, hit; 16796 } wrw_stats; 16797 #endif 16798 16799 /* 16800 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 16801 * messages. 16802 */ 16803 /* ARGSUSED */ 16804 static void 16805 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 16806 { 16807 conn_t *connp = (conn_t *)arg; 16808 tcp_t *tcp = connp->conn_tcp; 16809 queue_t *q = tcp->tcp_wq; 16810 16811 ASSERT(DB_TYPE(mp) != M_IOCTL); 16812 /* 16813 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 16814 * Once the close starts, streamhead and sockfs will not let any data 16815 * packets come down (close ensures that there are no threads using the 16816 * queue and no new threads will come down) but since qprocsoff() 16817 * hasn't happened yet, a M_FLUSH or some non data message might 16818 * get reflected back (in response to our own FLUSHRW) and get 16819 * processed after tcp_close() is done. The conn would still be valid 16820 * because a ref would have added but we need to check the state 16821 * before actually processing the packet. 16822 */ 16823 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 16824 freemsg(mp); 16825 return; 16826 } 16827 16828 switch (DB_TYPE(mp)) { 16829 case M_IOCDATA: 16830 tcp_wput_iocdata(tcp, mp); 16831 break; 16832 case M_FLUSH: 16833 tcp_wput_flush(tcp, mp); 16834 break; 16835 default: 16836 CALL_IP_WPUT(connp, q, mp); 16837 break; 16838 } 16839 } 16840 16841 /* 16842 * The TCP fast path write put procedure. 16843 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 16844 */ 16845 /* ARGSUSED */ 16846 void 16847 tcp_output(void *arg, mblk_t *mp, void *arg2) 16848 { 16849 int len; 16850 int hdrlen; 16851 int plen; 16852 mblk_t *mp1; 16853 uchar_t *rptr; 16854 uint32_t snxt; 16855 tcph_t *tcph; 16856 struct datab *db; 16857 uint32_t suna; 16858 uint32_t mss; 16859 ipaddr_t *dst; 16860 ipaddr_t *src; 16861 uint32_t sum; 16862 int usable; 16863 conn_t *connp = (conn_t *)arg; 16864 tcp_t *tcp = connp->conn_tcp; 16865 uint32_t msize; 16866 tcp_stack_t *tcps = tcp->tcp_tcps; 16867 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 16868 16869 /* 16870 * Try and ASSERT the minimum possible references on the 16871 * conn early enough. Since we are executing on write side, 16872 * the connection is obviously not detached and that means 16873 * there is a ref each for TCP and IP. Since we are behind 16874 * the squeue, the minimum references needed are 3. If the 16875 * conn is in classifier hash list, there should be an 16876 * extra ref for that (we check both the possibilities). 16877 */ 16878 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 16879 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 16880 16881 ASSERT(DB_TYPE(mp) == M_DATA); 16882 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 16883 16884 mutex_enter(&tcp->tcp_non_sq_lock); 16885 tcp->tcp_squeue_bytes -= msize; 16886 mutex_exit(&tcp->tcp_non_sq_lock); 16887 16888 /* Check to see if this connection wants to be re-fused. */ 16889 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 16890 if (tcp->tcp_ipversion == IPV4_VERSION) { 16891 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 16892 &tcp->tcp_saved_tcph); 16893 } else { 16894 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 16895 &tcp->tcp_saved_tcph); 16896 } 16897 } 16898 /* Bypass tcp protocol for fused tcp loopback */ 16899 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 16900 return; 16901 16902 mss = tcp->tcp_mss; 16903 if (tcp->tcp_xmit_zc_clean) 16904 mp = tcp_zcopy_backoff(tcp, mp, 0); 16905 16906 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 16907 len = (int)(mp->b_wptr - mp->b_rptr); 16908 16909 /* 16910 * Criteria for fast path: 16911 * 16912 * 1. no unsent data 16913 * 2. single mblk in request 16914 * 3. connection established 16915 * 4. data in mblk 16916 * 5. len <= mss 16917 * 6. no tcp_valid bits 16918 */ 16919 if ((tcp->tcp_unsent != 0) || 16920 (tcp->tcp_cork) || 16921 (mp->b_cont != NULL) || 16922 (tcp->tcp_state != TCPS_ESTABLISHED) || 16923 (len == 0) || 16924 (len > mss) || 16925 (tcp->tcp_valid_bits != 0)) { 16926 tcp_wput_data(tcp, mp, B_FALSE); 16927 return; 16928 } 16929 16930 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 16931 ASSERT(tcp->tcp_fin_sent == 0); 16932 16933 /* queue new packet onto retransmission queue */ 16934 if (tcp->tcp_xmit_head == NULL) { 16935 tcp->tcp_xmit_head = mp; 16936 } else { 16937 tcp->tcp_xmit_last->b_cont = mp; 16938 } 16939 tcp->tcp_xmit_last = mp; 16940 tcp->tcp_xmit_tail = mp; 16941 16942 /* find out how much we can send */ 16943 /* BEGIN CSTYLED */ 16944 /* 16945 * un-acked usable 16946 * |--------------|-----------------| 16947 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 16948 */ 16949 /* END CSTYLED */ 16950 16951 /* start sending from tcp_snxt */ 16952 snxt = tcp->tcp_snxt; 16953 16954 /* 16955 * Check to see if this connection has been idled for some 16956 * time and no ACK is expected. If it is, we need to slow 16957 * start again to get back the connection's "self-clock" as 16958 * described in VJ's paper. 16959 * 16960 * Refer to the comment in tcp_mss_set() for the calculation 16961 * of tcp_cwnd after idle. 16962 */ 16963 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 16964 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 16965 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 16966 } 16967 16968 usable = tcp->tcp_swnd; /* tcp window size */ 16969 if (usable > tcp->tcp_cwnd) 16970 usable = tcp->tcp_cwnd; /* congestion window smaller */ 16971 usable -= snxt; /* subtract stuff already sent */ 16972 suna = tcp->tcp_suna; 16973 usable += suna; 16974 /* usable can be < 0 if the congestion window is smaller */ 16975 if (len > usable) { 16976 /* Can't send complete M_DATA in one shot */ 16977 goto slow; 16978 } 16979 16980 mutex_enter(&tcp->tcp_non_sq_lock); 16981 if (tcp->tcp_flow_stopped && 16982 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 16983 tcp_clrqfull(tcp); 16984 } 16985 mutex_exit(&tcp->tcp_non_sq_lock); 16986 16987 /* 16988 * determine if anything to send (Nagle). 16989 * 16990 * 1. len < tcp_mss (i.e. small) 16991 * 2. unacknowledged data present 16992 * 3. len < nagle limit 16993 * 4. last packet sent < nagle limit (previous packet sent) 16994 */ 16995 if ((len < mss) && (snxt != suna) && 16996 (len < (int)tcp->tcp_naglim) && 16997 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 16998 /* 16999 * This was the first unsent packet and normally 17000 * mss < xmit_hiwater so there is no need to worry 17001 * about flow control. The next packet will go 17002 * through the flow control check in tcp_wput_data(). 17003 */ 17004 /* leftover work from above */ 17005 tcp->tcp_unsent = len; 17006 tcp->tcp_xmit_tail_unsent = len; 17007 17008 return; 17009 } 17010 17011 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17012 17013 if (snxt == suna) { 17014 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17015 } 17016 17017 /* we have always sent something */ 17018 tcp->tcp_rack_cnt = 0; 17019 17020 tcp->tcp_snxt = snxt + len; 17021 tcp->tcp_rack = tcp->tcp_rnxt; 17022 17023 if ((mp1 = dupb(mp)) == 0) 17024 goto no_memory; 17025 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17026 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17027 17028 /* adjust tcp header information */ 17029 tcph = tcp->tcp_tcph; 17030 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17031 17032 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17033 sum = (sum >> 16) + (sum & 0xFFFF); 17034 U16_TO_ABE16(sum, tcph->th_sum); 17035 17036 U32_TO_ABE32(snxt, tcph->th_seq); 17037 17038 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 17039 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 17040 BUMP_LOCAL(tcp->tcp_obsegs); 17041 17042 /* Update the latest receive window size in TCP header. */ 17043 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17044 tcph->th_win); 17045 17046 tcp->tcp_last_sent_len = (ushort_t)len; 17047 17048 plen = len + tcp->tcp_hdr_len; 17049 17050 if (tcp->tcp_ipversion == IPV4_VERSION) { 17051 tcp->tcp_ipha->ipha_length = htons(plen); 17052 } else { 17053 tcp->tcp_ip6h->ip6_plen = htons(plen - 17054 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17055 } 17056 17057 /* see if we need to allocate a mblk for the headers */ 17058 hdrlen = tcp->tcp_hdr_len; 17059 rptr = mp1->b_rptr - hdrlen; 17060 db = mp1->b_datap; 17061 if ((db->db_ref != 2) || rptr < db->db_base || 17062 (!OK_32PTR(rptr))) { 17063 /* NOTE: we assume allocb returns an OK_32PTR */ 17064 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17065 tcps->tcps_wroff_xtra, BPRI_MED); 17066 if (!mp) { 17067 freemsg(mp1); 17068 goto no_memory; 17069 } 17070 mp->b_cont = mp1; 17071 mp1 = mp; 17072 /* Leave room for Link Level header */ 17073 /* hdrlen = tcp->tcp_hdr_len; */ 17074 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 17075 mp1->b_wptr = &rptr[hdrlen]; 17076 } 17077 mp1->b_rptr = rptr; 17078 17079 /* Fill in the timestamp option. */ 17080 if (tcp->tcp_snd_ts_ok) { 17081 U32_TO_BE32((uint32_t)lbolt, 17082 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17083 U32_TO_BE32(tcp->tcp_ts_recent, 17084 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17085 } else { 17086 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17087 } 17088 17089 /* copy header into outgoing packet */ 17090 dst = (ipaddr_t *)rptr; 17091 src = (ipaddr_t *)tcp->tcp_iphc; 17092 dst[0] = src[0]; 17093 dst[1] = src[1]; 17094 dst[2] = src[2]; 17095 dst[3] = src[3]; 17096 dst[4] = src[4]; 17097 dst[5] = src[5]; 17098 dst[6] = src[6]; 17099 dst[7] = src[7]; 17100 dst[8] = src[8]; 17101 dst[9] = src[9]; 17102 if (hdrlen -= 40) { 17103 hdrlen >>= 2; 17104 dst += 10; 17105 src += 10; 17106 do { 17107 *dst++ = *src++; 17108 } while (--hdrlen); 17109 } 17110 17111 /* 17112 * Set the ECN info in the TCP header. Note that this 17113 * is not the template header. 17114 */ 17115 if (tcp->tcp_ecn_ok) { 17116 SET_ECT(tcp, rptr); 17117 17118 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17119 if (tcp->tcp_ecn_echo_on) 17120 tcph->th_flags[0] |= TH_ECE; 17121 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17122 tcph->th_flags[0] |= TH_CWR; 17123 tcp->tcp_ecn_cwr_sent = B_TRUE; 17124 } 17125 } 17126 17127 if (tcp->tcp_ip_forward_progress) { 17128 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17129 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17130 tcp->tcp_ip_forward_progress = B_FALSE; 17131 } 17132 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17133 return; 17134 17135 /* 17136 * If we ran out of memory, we pretend to have sent the packet 17137 * and that it was lost on the wire. 17138 */ 17139 no_memory: 17140 return; 17141 17142 slow: 17143 /* leftover work from above */ 17144 tcp->tcp_unsent = len; 17145 tcp->tcp_xmit_tail_unsent = len; 17146 tcp_wput_data(tcp, NULL, B_FALSE); 17147 } 17148 17149 /* ARGSUSED */ 17150 void 17151 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17152 { 17153 conn_t *connp = (conn_t *)arg; 17154 tcp_t *tcp = connp->conn_tcp; 17155 queue_t *q = tcp->tcp_rq; 17156 struct tcp_options *tcpopt; 17157 tcp_stack_t *tcps = tcp->tcp_tcps; 17158 17159 /* socket options */ 17160 uint_t sopp_flags; 17161 ssize_t sopp_rxhiwat; 17162 ssize_t sopp_maxblk; 17163 ushort_t sopp_wroff; 17164 ushort_t sopp_tail; 17165 ushort_t sopp_copyopt; 17166 17167 tcpopt = (struct tcp_options *)mp->b_rptr; 17168 17169 /* 17170 * Drop the eager's ref on the listener, that was placed when 17171 * this eager began life in tcp_conn_request. 17172 */ 17173 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17174 if (IPCL_IS_NONSTR(connp)) { 17175 /* Safe to free conn_ind message */ 17176 freemsg(tcp->tcp_conn.tcp_eager_conn_ind); 17177 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17178 } 17179 17180 tcp->tcp_detached = B_FALSE; 17181 17182 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17183 /* 17184 * Someone blewoff the eager before we could finish 17185 * the accept. 17186 * 17187 * The only reason eager exists it because we put in 17188 * a ref on it when conn ind went up. We need to send 17189 * a disconnect indication up while the last reference 17190 * on the eager will be dropped by the squeue when we 17191 * return. 17192 */ 17193 ASSERT(tcp->tcp_listener == NULL); 17194 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17195 if (IPCL_IS_NONSTR(connp)) { 17196 ASSERT(tcp->tcp_issocket); 17197 (*connp->conn_upcalls->su_disconnected)( 17198 connp->conn_upper_handle, tcp->tcp_connid, 17199 ECONNREFUSED); 17200 freemsg(mp); 17201 } else { 17202 struct T_discon_ind *tdi; 17203 17204 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17205 /* 17206 * Let us reuse the incoming mblk to avoid 17207 * memory allocation failure problems. We know 17208 * that the size of the incoming mblk i.e. 17209 * stroptions is greater than sizeof 17210 * T_discon_ind. So the reallocb below can't 17211 * fail. 17212 */ 17213 freemsg(mp->b_cont); 17214 mp->b_cont = NULL; 17215 ASSERT(DB_REF(mp) == 1); 17216 mp = reallocb(mp, sizeof (struct T_discon_ind), 17217 B_FALSE); 17218 ASSERT(mp != NULL); 17219 DB_TYPE(mp) = M_PROTO; 17220 ((union T_primitives *)mp->b_rptr)->type = 17221 T_DISCON_IND; 17222 tdi = (struct T_discon_ind *)mp->b_rptr; 17223 if (tcp->tcp_issocket) { 17224 tdi->DISCON_reason = ECONNREFUSED; 17225 tdi->SEQ_number = 0; 17226 } else { 17227 tdi->DISCON_reason = ENOPROTOOPT; 17228 tdi->SEQ_number = 17229 tcp->tcp_conn_req_seqnum; 17230 } 17231 mp->b_wptr = mp->b_rptr + 17232 sizeof (struct T_discon_ind); 17233 putnext(q, mp); 17234 return; 17235 } 17236 } 17237 if (tcp->tcp_hard_binding) { 17238 tcp->tcp_hard_binding = B_FALSE; 17239 tcp->tcp_hard_bound = B_TRUE; 17240 } 17241 return; 17242 } 17243 17244 if (tcpopt->to_flags & TCPOPT_BOUNDIF) { 17245 int boundif = tcpopt->to_boundif; 17246 uint_t len = sizeof (int); 17247 17248 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17249 IPV6_BOUND_IF, len, (uchar_t *)&boundif, &len, 17250 (uchar_t *)&boundif, NULL, tcp->tcp_cred, NULL); 17251 } 17252 if (tcpopt->to_flags & TCPOPT_RECVPKTINFO) { 17253 uint_t on = 1; 17254 uint_t len = sizeof (uint_t); 17255 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17256 IPV6_RECVPKTINFO, len, (uchar_t *)&on, &len, 17257 (uchar_t *)&on, NULL, tcp->tcp_cred, NULL); 17258 } 17259 17260 /* 17261 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17262 * properly. This is the first time we know of the acceptor' 17263 * queue. So we do it here. 17264 * 17265 * XXX 17266 */ 17267 if (tcp->tcp_rcv_list == NULL) { 17268 /* 17269 * Recv queue is empty, tcp_rwnd should not have changed. 17270 * That means it should be equal to the listener's tcp_rwnd. 17271 */ 17272 if (!IPCL_IS_NONSTR(connp)) 17273 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17274 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 17275 } else { 17276 #ifdef DEBUG 17277 mblk_t *tmp; 17278 mblk_t *mp1; 17279 uint_t cnt = 0; 17280 17281 mp1 = tcp->tcp_rcv_list; 17282 while ((tmp = mp1) != NULL) { 17283 mp1 = tmp->b_next; 17284 cnt += msgdsize(tmp); 17285 } 17286 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17287 #endif 17288 /* There is some data, add them back to get the max. */ 17289 if (!IPCL_IS_NONSTR(connp)) 17290 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17291 tcp->tcp_recv_hiwater = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17292 } 17293 /* 17294 * This is the first time we run on the correct 17295 * queue after tcp_accept. So fix all the q parameters 17296 * here. 17297 */ 17298 sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF; 17299 sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17300 17301 /* 17302 * Record the stream head's high water mark for this endpoint; 17303 * this is used for flow-control purposes. 17304 */ 17305 sopp_rxhiwat = tcp->tcp_fused ? 17306 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 17307 MAX(tcp->tcp_recv_hiwater, tcps->tcps_sth_rcv_hiwat); 17308 17309 /* 17310 * Determine what write offset value to use depending on SACK and 17311 * whether the endpoint is fused or not. 17312 */ 17313 if (tcp->tcp_fused) { 17314 ASSERT(tcp->tcp_loopback); 17315 ASSERT(tcp->tcp_loopback_peer != NULL); 17316 /* 17317 * For fused tcp loopback, set the stream head's write 17318 * offset value to zero since we won't be needing any room 17319 * for TCP/IP headers. This would also improve performance 17320 * since it would reduce the amount of work done by kmem. 17321 * Non-fused tcp loopback case is handled separately below. 17322 */ 17323 sopp_wroff = 0; 17324 /* 17325 * Update the peer's transmit parameters according to 17326 * our recently calculated high water mark value. 17327 */ 17328 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17329 } else if (tcp->tcp_snd_sack_ok) { 17330 sopp_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17331 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 17332 } else { 17333 sopp_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17334 tcps->tcps_wroff_xtra); 17335 } 17336 17337 /* 17338 * If this is endpoint is handling SSL, then reserve extra 17339 * offset and space at the end. 17340 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17341 * overriding the previous setting. The extra cost of signing and 17342 * encrypting multiple MSS-size records (12 of them with Ethernet), 17343 * instead of a single contiguous one by the stream head 17344 * largely outweighs the statistical reduction of ACKs, when 17345 * applicable. The peer will also save on decryption and verification 17346 * costs. 17347 */ 17348 if (tcp->tcp_kssl_ctx != NULL) { 17349 sopp_wroff += SSL3_WROFFSET; 17350 17351 sopp_flags |= SOCKOPT_TAIL; 17352 sopp_tail = SSL3_MAX_TAIL_LEN; 17353 17354 sopp_flags |= SOCKOPT_ZCOPY; 17355 sopp_copyopt = ZCVMUNSAFE; 17356 17357 sopp_maxblk = SSL3_MAX_RECORD_LEN; 17358 } 17359 17360 /* Send the options up */ 17361 if (IPCL_IS_NONSTR(connp)) { 17362 struct sock_proto_props sopp; 17363 17364 sopp.sopp_flags = sopp_flags; 17365 sopp.sopp_wroff = sopp_wroff; 17366 sopp.sopp_maxblk = sopp_maxblk; 17367 sopp.sopp_rxhiwat = sopp_rxhiwat; 17368 if (sopp_flags & SOCKOPT_TAIL) { 17369 ASSERT(tcp->tcp_kssl_ctx != NULL); 17370 ASSERT(sopp_flags & SOCKOPT_ZCOPY); 17371 sopp.sopp_tail = sopp_tail; 17372 sopp.sopp_zcopyflag = sopp_copyopt; 17373 } 17374 if (tcp->tcp_loopback) { 17375 sopp.sopp_flags |= SOCKOPT_LOOPBACK; 17376 sopp.sopp_loopback = B_TRUE; 17377 } 17378 (*connp->conn_upcalls->su_set_proto_props) 17379 (connp->conn_upper_handle, &sopp); 17380 } else { 17381 struct stroptions *stropt; 17382 mblk_t *stropt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 17383 if (stropt_mp == NULL) { 17384 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 17385 return; 17386 } 17387 DB_TYPE(stropt_mp) = M_SETOPTS; 17388 stropt = (struct stroptions *)stropt_mp->b_rptr; 17389 stropt_mp->b_wptr += sizeof (struct stroptions); 17390 stropt->so_flags = SO_HIWAT | SO_WROFF | SO_MAXBLK; 17391 stropt->so_hiwat = sopp_rxhiwat; 17392 stropt->so_wroff = sopp_wroff; 17393 stropt->so_maxblk = sopp_maxblk; 17394 17395 if (sopp_flags & SOCKOPT_TAIL) { 17396 ASSERT(tcp->tcp_kssl_ctx != NULL); 17397 17398 stropt->so_flags |= SO_TAIL | SO_COPYOPT; 17399 stropt->so_tail = sopp_tail; 17400 stropt->so_copyopt = sopp_copyopt; 17401 } 17402 17403 /* Send the options up */ 17404 putnext(q, stropt_mp); 17405 } 17406 17407 freemsg(mp); 17408 /* 17409 * Pass up any data and/or a fin that has been received. 17410 * 17411 * Adjust receive window in case it had decreased 17412 * (because there is data <=> tcp_rcv_list != NULL) 17413 * while the connection was detached. Note that 17414 * in case the eager was flow-controlled, w/o this 17415 * code, the rwnd may never open up again! 17416 */ 17417 if (tcp->tcp_rcv_list != NULL) { 17418 if (IPCL_IS_NONSTR(connp)) { 17419 mblk_t *mp; 17420 int space_left; 17421 int error; 17422 boolean_t push = B_TRUE; 17423 17424 if (!tcp->tcp_fused && (*connp->conn_upcalls->su_recv) 17425 (connp->conn_upper_handle, NULL, 0, 0, &error, 17426 &push) >= 0) { 17427 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 17428 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17429 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 17430 tcp_xmit_ctl(NULL, 17431 tcp, (tcp->tcp_swnd == 0) ? 17432 tcp->tcp_suna : tcp->tcp_snxt, 17433 tcp->tcp_rnxt, TH_ACK); 17434 } 17435 } 17436 while ((mp = tcp->tcp_rcv_list) != NULL) { 17437 push = B_TRUE; 17438 tcp->tcp_rcv_list = mp->b_next; 17439 mp->b_next = NULL; 17440 space_left = (*connp->conn_upcalls->su_recv) 17441 (connp->conn_upper_handle, mp, msgdsize(mp), 17442 0, &error, &push); 17443 if (space_left < 0) { 17444 /* 17445 * We should never be in middle of a 17446 * fallback, the squeue guarantees that. 17447 */ 17448 ASSERT(error != EOPNOTSUPP); 17449 } 17450 } 17451 tcp->tcp_rcv_last_head = NULL; 17452 tcp->tcp_rcv_last_tail = NULL; 17453 tcp->tcp_rcv_cnt = 0; 17454 } else { 17455 /* We drain directly in case of fused tcp loopback */ 17456 17457 if (!tcp->tcp_fused && canputnext(q)) { 17458 tcp->tcp_rwnd = q->q_hiwat; 17459 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17460 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 17461 tcp_xmit_ctl(NULL, 17462 tcp, (tcp->tcp_swnd == 0) ? 17463 tcp->tcp_suna : tcp->tcp_snxt, 17464 tcp->tcp_rnxt, TH_ACK); 17465 } 17466 } 17467 17468 (void) tcp_rcv_drain(tcp); 17469 } 17470 17471 /* 17472 * For fused tcp loopback, back-enable peer endpoint 17473 * if it's currently flow-controlled. 17474 */ 17475 if (tcp->tcp_fused) { 17476 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 17477 17478 ASSERT(peer_tcp != NULL); 17479 ASSERT(peer_tcp->tcp_fused); 17480 17481 mutex_enter(&peer_tcp->tcp_non_sq_lock); 17482 if (peer_tcp->tcp_flow_stopped) { 17483 tcp_clrqfull(peer_tcp); 17484 TCP_STAT(tcps, tcp_fusion_backenabled); 17485 } 17486 mutex_exit(&peer_tcp->tcp_non_sq_lock); 17487 } 17488 } 17489 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 17490 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 17491 tcp->tcp_ordrel_done = B_TRUE; 17492 if (IPCL_IS_NONSTR(connp)) { 17493 ASSERT(tcp->tcp_ordrel_mp == NULL); 17494 (*connp->conn_upcalls->su_opctl)( 17495 connp->conn_upper_handle, 17496 SOCK_OPCTL_SHUT_RECV, 0); 17497 } else { 17498 mp = tcp->tcp_ordrel_mp; 17499 tcp->tcp_ordrel_mp = NULL; 17500 putnext(q, mp); 17501 } 17502 } 17503 if (tcp->tcp_hard_binding) { 17504 tcp->tcp_hard_binding = B_FALSE; 17505 tcp->tcp_hard_bound = B_TRUE; 17506 } 17507 17508 if (tcp->tcp_ka_enabled) { 17509 tcp->tcp_ka_last_intrvl = 0; 17510 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 17511 MSEC_TO_TICK(tcp->tcp_ka_interval)); 17512 } 17513 17514 /* 17515 * At this point, eager is fully established and will 17516 * have the following references - 17517 * 17518 * 2 references for connection to exist (1 for TCP and 1 for IP). 17519 * 1 reference for the squeue which will be dropped by the squeue as 17520 * soon as this function returns. 17521 * There will be 1 additonal reference for being in classifier 17522 * hash list provided something bad hasn't happened. 17523 */ 17524 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17525 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17526 } 17527 17528 /* 17529 * The function called through squeue to get behind listener's perimeter to 17530 * send a deffered conn_ind. 17531 */ 17532 /* ARGSUSED */ 17533 void 17534 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 17535 { 17536 conn_t *connp = (conn_t *)arg; 17537 tcp_t *listener = connp->conn_tcp; 17538 struct T_conn_ind *conn_ind; 17539 tcp_t *tcp; 17540 17541 conn_ind = (struct T_conn_ind *)mp->b_rptr; 17542 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 17543 conn_ind->OPT_length); 17544 17545 if (listener->tcp_state != TCPS_LISTEN) { 17546 /* 17547 * If listener has closed, it would have caused a 17548 * a cleanup/blowoff to happen for the eager, so 17549 * we don't need to do anything more. 17550 */ 17551 freemsg(mp); 17552 return; 17553 } 17554 17555 tcp_ulp_newconn(connp, tcp->tcp_connp, mp); 17556 } 17557 17558 /* ARGSUSED */ 17559 static int 17560 tcp_accept_common(conn_t *lconnp, conn_t *econnp, cred_t *cr) 17561 { 17562 tcp_t *listener, *eager; 17563 mblk_t *opt_mp; 17564 struct tcp_options *tcpopt; 17565 17566 listener = lconnp->conn_tcp; 17567 ASSERT(listener->tcp_state == TCPS_LISTEN); 17568 eager = econnp->conn_tcp; 17569 ASSERT(eager->tcp_listener != NULL); 17570 17571 ASSERT(eager->tcp_rq != NULL); 17572 17573 opt_mp = allocb(sizeof (struct tcp_options), BPRI_HI); 17574 if (opt_mp == NULL) { 17575 return (-TPROTO); 17576 } 17577 bzero((char *)opt_mp->b_rptr, sizeof (struct tcp_options)); 17578 eager->tcp_issocket = B_TRUE; 17579 17580 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 17581 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 17582 ASSERT(econnp->conn_netstack == 17583 listener->tcp_connp->conn_netstack); 17584 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 17585 17586 /* Put the ref for IP */ 17587 CONN_INC_REF(econnp); 17588 17589 /* 17590 * We should have minimum of 3 references on the conn 17591 * at this point. One each for TCP and IP and one for 17592 * the T_conn_ind that was sent up when the 3-way handshake 17593 * completed. In the normal case we would also have another 17594 * reference (making a total of 4) for the conn being in the 17595 * classifier hash list. However the eager could have received 17596 * an RST subsequently and tcp_closei_local could have removed 17597 * the eager from the classifier hash list, hence we can't 17598 * assert that reference. 17599 */ 17600 ASSERT(econnp->conn_ref >= 3); 17601 17602 opt_mp->b_datap->db_type = M_SETOPTS; 17603 opt_mp->b_wptr += sizeof (struct tcp_options); 17604 17605 /* 17606 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 17607 * from listener to acceptor. 17608 */ 17609 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 17610 tcpopt->to_flags = 0; 17611 17612 if (listener->tcp_bound_if != 0) { 17613 tcpopt->to_flags |= TCPOPT_BOUNDIF; 17614 tcpopt->to_boundif = listener->tcp_bound_if; 17615 } 17616 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 17617 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 17618 } 17619 17620 mutex_enter(&listener->tcp_eager_lock); 17621 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 17622 17623 tcp_t *tail; 17624 tcp_t *tcp; 17625 mblk_t *mp1; 17626 17627 tcp = listener->tcp_eager_prev_q0; 17628 /* 17629 * listener->tcp_eager_prev_q0 points to the TAIL of the 17630 * deferred T_conn_ind queue. We need to get to the head 17631 * of the queue in order to send up T_conn_ind the same 17632 * order as how the 3WHS is completed. 17633 */ 17634 while (tcp != listener) { 17635 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 17636 !tcp->tcp_kssl_pending) 17637 break; 17638 else 17639 tcp = tcp->tcp_eager_prev_q0; 17640 } 17641 /* None of the pending eagers can be sent up now */ 17642 if (tcp == listener) 17643 goto no_more_eagers; 17644 17645 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 17646 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17647 /* Move from q0 to q */ 17648 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 17649 listener->tcp_conn_req_cnt_q0--; 17650 listener->tcp_conn_req_cnt_q++; 17651 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 17652 tcp->tcp_eager_prev_q0; 17653 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 17654 tcp->tcp_eager_next_q0; 17655 tcp->tcp_eager_prev_q0 = NULL; 17656 tcp->tcp_eager_next_q0 = NULL; 17657 tcp->tcp_conn_def_q0 = B_FALSE; 17658 17659 /* Make sure the tcp isn't in the list of droppables */ 17660 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 17661 tcp->tcp_eager_prev_drop_q0 == NULL); 17662 17663 /* 17664 * Insert at end of the queue because sockfs sends 17665 * down T_CONN_RES in chronological order. Leaving 17666 * the older conn indications at front of the queue 17667 * helps reducing search time. 17668 */ 17669 tail = listener->tcp_eager_last_q; 17670 if (tail != NULL) { 17671 tail->tcp_eager_next_q = tcp; 17672 } else { 17673 listener->tcp_eager_next_q = tcp; 17674 } 17675 listener->tcp_eager_last_q = tcp; 17676 tcp->tcp_eager_next_q = NULL; 17677 17678 /* Need to get inside the listener perimeter */ 17679 CONN_INC_REF(listener->tcp_connp); 17680 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, mp1, 17681 tcp_send_pending, listener->tcp_connp, SQ_FILL, 17682 SQTAG_TCP_SEND_PENDING); 17683 } 17684 no_more_eagers: 17685 tcp_eager_unlink(eager); 17686 mutex_exit(&listener->tcp_eager_lock); 17687 17688 /* 17689 * At this point, the eager is detached from the listener 17690 * but we still have an extra refs on eager (apart from the 17691 * usual tcp references). The ref was placed in tcp_rput_data 17692 * before sending the conn_ind in tcp_send_conn_ind. 17693 * The ref will be dropped in tcp_accept_finish(). 17694 */ 17695 SQUEUE_ENTER_ONE(econnp->conn_sqp, opt_mp, tcp_accept_finish, 17696 econnp, SQ_NODRAIN, SQTAG_TCP_ACCEPT_FINISH_Q0); 17697 return (0); 17698 } 17699 17700 int 17701 tcp_accept(sock_lower_handle_t lproto_handle, 17702 sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle, 17703 cred_t *cr) 17704 { 17705 conn_t *lconnp, *econnp; 17706 tcp_t *listener, *eager; 17707 tcp_stack_t *tcps; 17708 17709 lconnp = (conn_t *)lproto_handle; 17710 listener = lconnp->conn_tcp; 17711 ASSERT(listener->tcp_state == TCPS_LISTEN); 17712 econnp = (conn_t *)eproto_handle; 17713 eager = econnp->conn_tcp; 17714 ASSERT(eager->tcp_listener != NULL); 17715 tcps = eager->tcp_tcps; 17716 17717 /* 17718 * It is OK to manipulate these fields outside the eager's squeue 17719 * because they will not start being used until tcp_accept_finish 17720 * has been called. 17721 */ 17722 ASSERT(lconnp->conn_upper_handle != NULL); 17723 ASSERT(econnp->conn_upper_handle == NULL); 17724 econnp->conn_upper_handle = sock_handle; 17725 econnp->conn_upcalls = lconnp->conn_upcalls; 17726 ASSERT(IPCL_IS_NONSTR(econnp)); 17727 /* 17728 * Create helper stream if it is a non-TPI TCP connection. 17729 */ 17730 if (ip_create_helper_stream(econnp, tcps->tcps_ldi_ident)) { 17731 ip1dbg(("tcp_accept: create of IP helper stream" 17732 " failed\n")); 17733 return (EPROTO); 17734 } 17735 eager->tcp_rq = econnp->conn_rq; 17736 eager->tcp_wq = econnp->conn_wq; 17737 17738 ASSERT(eager->tcp_rq != NULL); 17739 17740 return (tcp_accept_common(lconnp, econnp, cr)); 17741 } 17742 17743 17744 /* 17745 * This is the STREAMS entry point for T_CONN_RES coming down on 17746 * Acceptor STREAM when sockfs listener does accept processing. 17747 * Read the block comment on top of tcp_conn_request(). 17748 */ 17749 void 17750 tcp_tpi_accept(queue_t *q, mblk_t *mp) 17751 { 17752 queue_t *rq = RD(q); 17753 struct T_conn_res *conn_res; 17754 tcp_t *eager; 17755 tcp_t *listener; 17756 struct T_ok_ack *ok; 17757 t_scalar_t PRIM_type; 17758 conn_t *econnp; 17759 cred_t *cr; 17760 17761 ASSERT(DB_TYPE(mp) == M_PROTO); 17762 17763 /* 17764 * All Solaris components should pass a db_credp 17765 * for this TPI message, hence we ASSERT. 17766 * But in case there is some other M_PROTO that looks 17767 * like a TPI message sent by some other kernel 17768 * component, we check and return an error. 17769 */ 17770 cr = msg_getcred(mp, NULL); 17771 ASSERT(cr != NULL); 17772 if (cr == NULL) { 17773 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL); 17774 if (mp != NULL) 17775 putnext(rq, mp); 17776 return; 17777 } 17778 conn_res = (struct T_conn_res *)mp->b_rptr; 17779 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17780 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 17781 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17782 if (mp != NULL) 17783 putnext(rq, mp); 17784 return; 17785 } 17786 switch (conn_res->PRIM_type) { 17787 case O_T_CONN_RES: 17788 case T_CONN_RES: 17789 /* 17790 * We pass up an err ack if allocb fails. This will 17791 * cause sockfs to issue a T_DISCON_REQ which will cause 17792 * tcp_eager_blowoff to be called. sockfs will then call 17793 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 17794 * we need to do the allocb up here because we have to 17795 * make sure rq->q_qinfo->qi_qclose still points to the 17796 * correct function (tcp_tpi_close_accept) in case allocb 17797 * fails. 17798 */ 17799 bcopy(mp->b_rptr + conn_res->OPT_offset, 17800 &eager, conn_res->OPT_length); 17801 PRIM_type = conn_res->PRIM_type; 17802 mp->b_datap->db_type = M_PCPROTO; 17803 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 17804 ok = (struct T_ok_ack *)mp->b_rptr; 17805 ok->PRIM_type = T_OK_ACK; 17806 ok->CORRECT_prim = PRIM_type; 17807 econnp = eager->tcp_connp; 17808 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 17809 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 17810 eager->tcp_rq = rq; 17811 eager->tcp_wq = q; 17812 rq->q_ptr = econnp; 17813 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 17814 q->q_ptr = econnp; 17815 q->q_qinfo = &tcp_winit; 17816 listener = eager->tcp_listener; 17817 17818 if (tcp_accept_common(listener->tcp_connp, 17819 econnp, cr) < 0) { 17820 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17821 if (mp != NULL) 17822 putnext(rq, mp); 17823 return; 17824 } 17825 17826 /* 17827 * Send the new local address also up to sockfs. There 17828 * should already be enough space in the mp that came 17829 * down from soaccept(). 17830 */ 17831 if (eager->tcp_family == AF_INET) { 17832 sin_t *sin; 17833 17834 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17835 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 17836 sin = (sin_t *)mp->b_wptr; 17837 mp->b_wptr += sizeof (sin_t); 17838 sin->sin_family = AF_INET; 17839 sin->sin_port = eager->tcp_lport; 17840 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 17841 } else { 17842 sin6_t *sin6; 17843 17844 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17845 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 17846 sin6 = (sin6_t *)mp->b_wptr; 17847 mp->b_wptr += sizeof (sin6_t); 17848 sin6->sin6_family = AF_INET6; 17849 sin6->sin6_port = eager->tcp_lport; 17850 if (eager->tcp_ipversion == IPV4_VERSION) { 17851 sin6->sin6_flowinfo = 0; 17852 IN6_IPADDR_TO_V4MAPPED( 17853 eager->tcp_ipha->ipha_src, 17854 &sin6->sin6_addr); 17855 } else { 17856 ASSERT(eager->tcp_ip6h != NULL); 17857 sin6->sin6_flowinfo = 17858 eager->tcp_ip6h->ip6_vcf & 17859 ~IPV6_VERS_AND_FLOW_MASK; 17860 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 17861 } 17862 sin6->sin6_scope_id = 0; 17863 sin6->__sin6_src_id = 0; 17864 } 17865 17866 putnext(rq, mp); 17867 return; 17868 default: 17869 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 17870 if (mp != NULL) 17871 putnext(rq, mp); 17872 return; 17873 } 17874 } 17875 17876 static int 17877 tcp_do_getsockname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 17878 { 17879 sin_t *sin = (sin_t *)sa; 17880 sin6_t *sin6 = (sin6_t *)sa; 17881 17882 switch (tcp->tcp_family) { 17883 case AF_INET: 17884 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 17885 17886 if (*salenp < sizeof (sin_t)) 17887 return (EINVAL); 17888 17889 *sin = sin_null; 17890 sin->sin_family = AF_INET; 17891 if (tcp->tcp_state >= TCPS_BOUND) { 17892 sin->sin_port = tcp->tcp_lport; 17893 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 17894 } 17895 *salenp = sizeof (sin_t); 17896 break; 17897 17898 case AF_INET6: 17899 if (*salenp < sizeof (sin6_t)) 17900 return (EINVAL); 17901 17902 *sin6 = sin6_null; 17903 sin6->sin6_family = AF_INET6; 17904 if (tcp->tcp_state >= TCPS_BOUND) { 17905 sin6->sin6_port = tcp->tcp_lport; 17906 if (tcp->tcp_ipversion == IPV4_VERSION) { 17907 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 17908 &sin6->sin6_addr); 17909 } else { 17910 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 17911 } 17912 } 17913 *salenp = sizeof (sin6_t); 17914 break; 17915 } 17916 17917 return (0); 17918 } 17919 17920 static int 17921 tcp_do_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 17922 { 17923 sin_t *sin = (sin_t *)sa; 17924 sin6_t *sin6 = (sin6_t *)sa; 17925 17926 if (tcp->tcp_state < TCPS_SYN_RCVD) 17927 return (ENOTCONN); 17928 17929 switch (tcp->tcp_family) { 17930 case AF_INET: 17931 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 17932 17933 if (*salenp < sizeof (sin_t)) 17934 return (EINVAL); 17935 17936 *sin = sin_null; 17937 sin->sin_family = AF_INET; 17938 sin->sin_port = tcp->tcp_fport; 17939 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 17940 sin->sin_addr.s_addr); 17941 *salenp = sizeof (sin_t); 17942 break; 17943 17944 case AF_INET6: 17945 if (*salenp < sizeof (sin6_t)) 17946 return (EINVAL); 17947 17948 *sin6 = sin6_null; 17949 sin6->sin6_family = AF_INET6; 17950 sin6->sin6_port = tcp->tcp_fport; 17951 sin6->sin6_addr = tcp->tcp_remote_v6; 17952 if (tcp->tcp_ipversion == IPV6_VERSION) { 17953 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 17954 ~IPV6_VERS_AND_FLOW_MASK; 17955 } 17956 *salenp = sizeof (sin6_t); 17957 break; 17958 } 17959 17960 return (0); 17961 } 17962 17963 /* 17964 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 17965 */ 17966 static void 17967 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 17968 { 17969 void *data; 17970 mblk_t *datamp = mp->b_cont; 17971 tcp_t *tcp = Q_TO_TCP(q); 17972 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 17973 17974 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 17975 cmdp->cb_error = EPROTO; 17976 qreply(q, mp); 17977 return; 17978 } 17979 17980 data = datamp->b_rptr; 17981 17982 switch (cmdp->cb_cmd) { 17983 case TI_GETPEERNAME: 17984 cmdp->cb_error = tcp_do_getpeername(tcp, data, &cmdp->cb_len); 17985 break; 17986 case TI_GETMYNAME: 17987 cmdp->cb_error = tcp_do_getsockname(tcp, data, &cmdp->cb_len); 17988 break; 17989 default: 17990 cmdp->cb_error = EINVAL; 17991 break; 17992 } 17993 17994 qreply(q, mp); 17995 } 17996 17997 void 17998 tcp_wput(queue_t *q, mblk_t *mp) 17999 { 18000 conn_t *connp = Q_TO_CONN(q); 18001 tcp_t *tcp; 18002 void (*output_proc)(); 18003 t_scalar_t type; 18004 uchar_t *rptr; 18005 struct iocblk *iocp; 18006 size_t size; 18007 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18008 18009 ASSERT(connp->conn_ref >= 2); 18010 18011 switch (DB_TYPE(mp)) { 18012 case M_DATA: 18013 tcp = connp->conn_tcp; 18014 ASSERT(tcp != NULL); 18015 18016 size = msgdsize(mp); 18017 18018 mutex_enter(&tcp->tcp_non_sq_lock); 18019 tcp->tcp_squeue_bytes += size; 18020 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18021 tcp_setqfull(tcp); 18022 } 18023 mutex_exit(&tcp->tcp_non_sq_lock); 18024 18025 CONN_INC_REF(connp); 18026 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp, 18027 tcp_squeue_flag, SQTAG_TCP_OUTPUT); 18028 return; 18029 18030 case M_CMD: 18031 tcp_wput_cmdblk(q, mp); 18032 return; 18033 18034 case M_PROTO: 18035 case M_PCPROTO: 18036 /* 18037 * if it is a snmp message, don't get behind the squeue 18038 */ 18039 tcp = connp->conn_tcp; 18040 rptr = mp->b_rptr; 18041 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18042 type = ((union T_primitives *)rptr)->type; 18043 } else { 18044 if (tcp->tcp_debug) { 18045 (void) strlog(TCP_MOD_ID, 0, 1, 18046 SL_ERROR|SL_TRACE, 18047 "tcp_wput_proto, dropping one..."); 18048 } 18049 freemsg(mp); 18050 return; 18051 } 18052 if (type == T_SVR4_OPTMGMT_REQ) { 18053 /* 18054 * All Solaris components should pass a db_credp 18055 * for this TPI message, hence we ASSERT. 18056 * But in case there is some other M_PROTO that looks 18057 * like a TPI message sent by some other kernel 18058 * component, we check and return an error. 18059 */ 18060 cred_t *cr = msg_getcred(mp, NULL); 18061 18062 ASSERT(cr != NULL); 18063 if (cr == NULL) { 18064 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 18065 return; 18066 } 18067 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 18068 cr)) { 18069 /* 18070 * This was a SNMP request 18071 */ 18072 return; 18073 } else { 18074 output_proc = tcp_wput_proto; 18075 } 18076 } else { 18077 output_proc = tcp_wput_proto; 18078 } 18079 break; 18080 case M_IOCTL: 18081 /* 18082 * Most ioctls can be processed right away without going via 18083 * squeues - process them right here. Those that do require 18084 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18085 * are processed by tcp_wput_ioctl(). 18086 */ 18087 iocp = (struct iocblk *)mp->b_rptr; 18088 tcp = connp->conn_tcp; 18089 18090 switch (iocp->ioc_cmd) { 18091 case TCP_IOC_ABORT_CONN: 18092 tcp_ioctl_abort_conn(q, mp); 18093 return; 18094 case TI_GETPEERNAME: 18095 case TI_GETMYNAME: 18096 mi_copyin(q, mp, NULL, 18097 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18098 return; 18099 case ND_SET: 18100 /* nd_getset does the necessary checks */ 18101 case ND_GET: 18102 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18103 CALL_IP_WPUT(connp, q, mp); 18104 return; 18105 } 18106 qreply(q, mp); 18107 return; 18108 case TCP_IOC_DEFAULT_Q: 18109 /* 18110 * Wants to be the default wq. Check the credentials 18111 * first, the rest is executed via squeue. 18112 */ 18113 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18114 iocp->ioc_error = EPERM; 18115 iocp->ioc_count = 0; 18116 mp->b_datap->db_type = M_IOCACK; 18117 qreply(q, mp); 18118 return; 18119 } 18120 output_proc = tcp_wput_ioctl; 18121 break; 18122 default: 18123 output_proc = tcp_wput_ioctl; 18124 break; 18125 } 18126 break; 18127 default: 18128 output_proc = tcp_wput_nondata; 18129 break; 18130 } 18131 18132 CONN_INC_REF(connp); 18133 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp, 18134 tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER); 18135 } 18136 18137 /* 18138 * Initial STREAMS write side put() procedure for sockets. It tries to 18139 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18140 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18141 * are handled by tcp_wput() as usual. 18142 * 18143 * All further messages will also be handled by tcp_wput() because we cannot 18144 * be sure that the above short cut is safe later. 18145 */ 18146 static void 18147 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18148 { 18149 conn_t *connp = Q_TO_CONN(wq); 18150 tcp_t *tcp = connp->conn_tcp; 18151 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18152 18153 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18154 wq->q_qinfo = &tcp_winit; 18155 18156 ASSERT(IPCL_IS_TCP(connp)); 18157 ASSERT(TCP_IS_SOCKET(tcp)); 18158 18159 if (DB_TYPE(mp) == M_PCPROTO && 18160 MBLKL(mp) == sizeof (struct T_capability_req) && 18161 car->PRIM_type == T_CAPABILITY_REQ) { 18162 tcp_capability_req(tcp, mp); 18163 return; 18164 } 18165 18166 tcp_wput(wq, mp); 18167 } 18168 18169 /* ARGSUSED */ 18170 static void 18171 tcp_wput_fallback(queue_t *wq, mblk_t *mp) 18172 { 18173 #ifdef DEBUG 18174 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n"); 18175 #endif 18176 freemsg(mp); 18177 } 18178 18179 static boolean_t 18180 tcp_zcopy_check(tcp_t *tcp) 18181 { 18182 conn_t *connp = tcp->tcp_connp; 18183 ire_t *ire; 18184 boolean_t zc_enabled = B_FALSE; 18185 tcp_stack_t *tcps = tcp->tcp_tcps; 18186 18187 if (do_tcpzcopy == 2) 18188 zc_enabled = B_TRUE; 18189 else if (tcp->tcp_ipversion == IPV4_VERSION && 18190 IPCL_IS_CONNECTED(connp) && 18191 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18192 connp->conn_dontroute == 0 && 18193 !connp->conn_nexthop_set && 18194 connp->conn_outgoing_ill == NULL && 18195 do_tcpzcopy == 1) { 18196 /* 18197 * the checks above closely resemble the fast path checks 18198 * in tcp_send_data(). 18199 */ 18200 mutex_enter(&connp->conn_lock); 18201 ire = connp->conn_ire_cache; 18202 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18203 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18204 IRE_REFHOLD(ire); 18205 if (ire->ire_stq != NULL) { 18206 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18207 18208 zc_enabled = ill && (ill->ill_capabilities & 18209 ILL_CAPAB_ZEROCOPY) && 18210 (ill->ill_zerocopy_capab-> 18211 ill_zerocopy_flags != 0); 18212 } 18213 IRE_REFRELE(ire); 18214 } 18215 mutex_exit(&connp->conn_lock); 18216 } 18217 tcp->tcp_snd_zcopy_on = zc_enabled; 18218 if (!TCP_IS_DETACHED(tcp)) { 18219 if (zc_enabled) { 18220 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18221 ZCVMSAFE); 18222 TCP_STAT(tcps, tcp_zcopy_on); 18223 } else { 18224 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18225 ZCVMUNSAFE); 18226 TCP_STAT(tcps, tcp_zcopy_off); 18227 } 18228 } 18229 return (zc_enabled); 18230 } 18231 18232 static mblk_t * 18233 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18234 { 18235 tcp_stack_t *tcps = tcp->tcp_tcps; 18236 18237 if (do_tcpzcopy == 2) 18238 return (bp); 18239 else if (tcp->tcp_snd_zcopy_on) { 18240 tcp->tcp_snd_zcopy_on = B_FALSE; 18241 if (!TCP_IS_DETACHED(tcp)) { 18242 (void) proto_set_tx_copyopt(tcp->tcp_rq, tcp->tcp_connp, 18243 ZCVMUNSAFE); 18244 TCP_STAT(tcps, tcp_zcopy_disable); 18245 } 18246 } 18247 return (tcp_zcopy_backoff(tcp, bp, 0)); 18248 } 18249 18250 /* 18251 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18252 * the original desballoca'ed segmapped mblk. 18253 */ 18254 static mblk_t * 18255 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18256 { 18257 mblk_t *head, *tail, *nbp; 18258 tcp_stack_t *tcps = tcp->tcp_tcps; 18259 18260 if (IS_VMLOANED_MBLK(bp)) { 18261 TCP_STAT(tcps, tcp_zcopy_backoff); 18262 if ((head = copyb(bp)) == NULL) { 18263 /* fail to backoff; leave it for the next backoff */ 18264 tcp->tcp_xmit_zc_clean = B_FALSE; 18265 return (bp); 18266 } 18267 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18268 if (fix_xmitlist) 18269 tcp_zcopy_notify(tcp); 18270 else 18271 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18272 } 18273 nbp = bp->b_cont; 18274 if (fix_xmitlist) { 18275 head->b_prev = bp->b_prev; 18276 head->b_next = bp->b_next; 18277 if (tcp->tcp_xmit_tail == bp) 18278 tcp->tcp_xmit_tail = head; 18279 } 18280 bp->b_next = NULL; 18281 bp->b_prev = NULL; 18282 freeb(bp); 18283 } else { 18284 head = bp; 18285 nbp = bp->b_cont; 18286 } 18287 tail = head; 18288 while (nbp) { 18289 if (IS_VMLOANED_MBLK(nbp)) { 18290 TCP_STAT(tcps, tcp_zcopy_backoff); 18291 if ((tail->b_cont = copyb(nbp)) == NULL) { 18292 tcp->tcp_xmit_zc_clean = B_FALSE; 18293 tail->b_cont = nbp; 18294 return (head); 18295 } 18296 tail = tail->b_cont; 18297 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18298 if (fix_xmitlist) 18299 tcp_zcopy_notify(tcp); 18300 else 18301 tail->b_datap->db_struioflag |= 18302 STRUIO_ZCNOTIFY; 18303 } 18304 bp = nbp; 18305 nbp = nbp->b_cont; 18306 if (fix_xmitlist) { 18307 tail->b_prev = bp->b_prev; 18308 tail->b_next = bp->b_next; 18309 if (tcp->tcp_xmit_tail == bp) 18310 tcp->tcp_xmit_tail = tail; 18311 } 18312 bp->b_next = NULL; 18313 bp->b_prev = NULL; 18314 freeb(bp); 18315 } else { 18316 tail->b_cont = nbp; 18317 tail = nbp; 18318 nbp = nbp->b_cont; 18319 } 18320 } 18321 if (fix_xmitlist) { 18322 tcp->tcp_xmit_last = tail; 18323 tcp->tcp_xmit_zc_clean = B_TRUE; 18324 } 18325 return (head); 18326 } 18327 18328 static void 18329 tcp_zcopy_notify(tcp_t *tcp) 18330 { 18331 struct stdata *stp; 18332 conn_t *connp; 18333 18334 if (tcp->tcp_detached) 18335 return; 18336 connp = tcp->tcp_connp; 18337 if (IPCL_IS_NONSTR(connp)) { 18338 (*connp->conn_upcalls->su_zcopy_notify) 18339 (connp->conn_upper_handle); 18340 return; 18341 } 18342 stp = STREAM(tcp->tcp_rq); 18343 mutex_enter(&stp->sd_lock); 18344 stp->sd_flag |= STZCNOTIFY; 18345 cv_broadcast(&stp->sd_zcopy_wait); 18346 mutex_exit(&stp->sd_lock); 18347 } 18348 18349 static boolean_t 18350 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 18351 { 18352 ire_t *ire; 18353 conn_t *connp = tcp->tcp_connp; 18354 tcp_stack_t *tcps = tcp->tcp_tcps; 18355 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18356 18357 mutex_enter(&connp->conn_lock); 18358 ire = connp->conn_ire_cache; 18359 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18360 18361 if ((ire != NULL) && 18362 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 18363 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 18364 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18365 IRE_REFHOLD(ire); 18366 mutex_exit(&connp->conn_lock); 18367 } else { 18368 boolean_t cached = B_FALSE; 18369 ts_label_t *tsl; 18370 18371 /* force a recheck later on */ 18372 tcp->tcp_ire_ill_check_done = B_FALSE; 18373 18374 TCP_DBGSTAT(tcps, tcp_ire_null1); 18375 connp->conn_ire_cache = NULL; 18376 mutex_exit(&connp->conn_lock); 18377 18378 if (ire != NULL) 18379 IRE_REFRELE_NOTR(ire); 18380 18381 tsl = crgetlabel(CONN_CRED(connp)); 18382 ire = (dst ? 18383 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 18384 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18385 connp->conn_zoneid, tsl, ipst)); 18386 18387 if (ire == NULL) { 18388 TCP_STAT(tcps, tcp_ire_null); 18389 return (B_FALSE); 18390 } 18391 18392 IRE_REFHOLD_NOTR(ire); 18393 18394 mutex_enter(&connp->conn_lock); 18395 if (CONN_CACHE_IRE(connp)) { 18396 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18397 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18398 TCP_CHECK_IREINFO(tcp, ire); 18399 connp->conn_ire_cache = ire; 18400 cached = B_TRUE; 18401 } 18402 rw_exit(&ire->ire_bucket->irb_lock); 18403 } 18404 mutex_exit(&connp->conn_lock); 18405 18406 /* 18407 * We can continue to use the ire but since it was 18408 * not cached, we should drop the extra reference. 18409 */ 18410 if (!cached) 18411 IRE_REFRELE_NOTR(ire); 18412 18413 /* 18414 * Rampart note: no need to select a new label here, since 18415 * labels are not allowed to change during the life of a TCP 18416 * connection. 18417 */ 18418 } 18419 18420 *irep = ire; 18421 18422 return (B_TRUE); 18423 } 18424 18425 /* 18426 * Called from tcp_send() or tcp_send_data() to find workable IRE. 18427 * 18428 * 0 = success; 18429 * 1 = failed to find ire and ill. 18430 */ 18431 static boolean_t 18432 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 18433 { 18434 ipha_t *ipha; 18435 ipaddr_t dst; 18436 ire_t *ire; 18437 ill_t *ill; 18438 mblk_t *ire_fp_mp; 18439 tcp_stack_t *tcps = tcp->tcp_tcps; 18440 18441 if (mp != NULL) 18442 ipha = (ipha_t *)mp->b_rptr; 18443 else 18444 ipha = tcp->tcp_ipha; 18445 dst = ipha->ipha_dst; 18446 18447 if (!tcp_send_find_ire(tcp, &dst, &ire)) 18448 return (B_FALSE); 18449 18450 if ((ire->ire_flags & RTF_MULTIRT) || 18451 (ire->ire_stq == NULL) || 18452 (ire->ire_nce == NULL) || 18453 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18454 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 18455 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 18456 TCP_STAT(tcps, tcp_ip_ire_send); 18457 IRE_REFRELE(ire); 18458 return (B_FALSE); 18459 } 18460 18461 ill = ire_to_ill(ire); 18462 ASSERT(ill != NULL); 18463 18464 if (!tcp->tcp_ire_ill_check_done) { 18465 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18466 tcp->tcp_ire_ill_check_done = B_TRUE; 18467 } 18468 18469 *irep = ire; 18470 *illp = ill; 18471 18472 return (B_TRUE); 18473 } 18474 18475 static void 18476 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18477 { 18478 ipha_t *ipha; 18479 ipaddr_t src; 18480 ipaddr_t dst; 18481 uint32_t cksum; 18482 ire_t *ire; 18483 uint16_t *up; 18484 ill_t *ill; 18485 conn_t *connp = tcp->tcp_connp; 18486 uint32_t hcksum_txflags = 0; 18487 mblk_t *ire_fp_mp; 18488 uint_t ire_fp_mp_len; 18489 tcp_stack_t *tcps = tcp->tcp_tcps; 18490 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18491 cred_t *cr; 18492 pid_t cpid; 18493 18494 ASSERT(DB_TYPE(mp) == M_DATA); 18495 18496 /* 18497 * Here we need to handle the overloading of the cred_t for 18498 * both getpeerucred and TX. 18499 * If this is a SYN then the caller already set db_credp so 18500 * that getpeerucred will work. But if TX is in use we might have 18501 * a conn_effective_cred which is different, and we need to use that 18502 * cred to make TX use the correct label and label dependent route. 18503 */ 18504 if (is_system_labeled()) { 18505 cr = msg_getcred(mp, &cpid); 18506 if (cr == NULL || connp->conn_effective_cred != NULL) 18507 mblk_setcred(mp, CONN_CRED(connp), cpid); 18508 } 18509 18510 ipha = (ipha_t *)mp->b_rptr; 18511 src = ipha->ipha_src; 18512 dst = ipha->ipha_dst; 18513 18514 ASSERT(q != NULL); 18515 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 18516 18517 /* 18518 * Drop off fast path for IPv6 and also if options are present or 18519 * we need to resolve a TS label. 18520 */ 18521 if (tcp->tcp_ipversion != IPV4_VERSION || 18522 !IPCL_IS_CONNECTED(connp) || 18523 !CONN_IS_LSO_MD_FASTPATH(connp) || 18524 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18525 !connp->conn_ulp_labeled || 18526 ipha->ipha_ident == IP_HDR_INCLUDED || 18527 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18528 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 18529 if (tcp->tcp_snd_zcopy_aware) 18530 mp = tcp_zcopy_disable(tcp, mp); 18531 TCP_STAT(tcps, tcp_ip_send); 18532 CALL_IP_WPUT(connp, q, mp); 18533 return; 18534 } 18535 18536 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 18537 if (tcp->tcp_snd_zcopy_aware) 18538 mp = tcp_zcopy_backoff(tcp, mp, 0); 18539 CALL_IP_WPUT(connp, q, mp); 18540 return; 18541 } 18542 ire_fp_mp = ire->ire_nce->nce_fp_mp; 18543 ire_fp_mp_len = MBLKL(ire_fp_mp); 18544 18545 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18546 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18547 #ifndef _BIG_ENDIAN 18548 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18549 #endif 18550 18551 /* 18552 * Check to see if we need to re-enable LSO/MDT for this connection 18553 * because it was previously disabled due to changes in the ill; 18554 * note that by doing it here, this re-enabling only applies when 18555 * the packet is not dispatched through CALL_IP_WPUT(). 18556 * 18557 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 18558 * case, since that's how we ended up here. For IPv6, we do the 18559 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18560 */ 18561 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 18562 /* 18563 * Restore LSO for this connection, so that next time around 18564 * it is eligible to go through tcp_lsosend() path again. 18565 */ 18566 TCP_STAT(tcps, tcp_lso_enabled); 18567 tcp->tcp_lso = B_TRUE; 18568 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 18569 "interface %s\n", (void *)connp, ill->ill_name)); 18570 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18571 /* 18572 * Restore MDT for this connection, so that next time around 18573 * it is eligible to go through tcp_multisend() path again. 18574 */ 18575 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 18576 tcp->tcp_mdt = B_TRUE; 18577 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18578 "interface %s\n", (void *)connp, ill->ill_name)); 18579 } 18580 18581 if (tcp->tcp_snd_zcopy_aware) { 18582 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18583 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18584 mp = tcp_zcopy_disable(tcp, mp); 18585 /* 18586 * we shouldn't need to reset ipha as the mp containing 18587 * ipha should never be a zero-copy mp. 18588 */ 18589 } 18590 18591 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18592 ASSERT(ill->ill_hcksum_capab != NULL); 18593 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18594 } 18595 18596 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18597 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18598 18599 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18600 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18601 18602 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18603 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18604 18605 /* Software checksum? */ 18606 if (DB_CKSUMFLAGS(mp) == 0) { 18607 TCP_STAT(tcps, tcp_out_sw_cksum); 18608 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 18609 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18610 } 18611 18612 /* Calculate IP header checksum if hardware isn't capable */ 18613 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18614 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18615 ((uint16_t *)ipha)[4]); 18616 } 18617 18618 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18619 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18620 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18621 18622 UPDATE_OB_PKT_COUNT(ire); 18623 ire->ire_last_used_time = lbolt; 18624 18625 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 18626 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 18627 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 18628 ntohs(ipha->ipha_length)); 18629 18630 DTRACE_PROBE4(ip4__physical__out__start, 18631 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 18632 FW_HOOKS(ipst->ips_ip4_physical_out_event, 18633 ipst->ips_ipv4firewall_physical_out, 18634 NULL, ill, ipha, mp, mp, 0, ipst); 18635 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 18636 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 18637 18638 if (mp != NULL) { 18639 if (ipst->ips_ipobs_enabled) { 18640 zoneid_t szone; 18641 18642 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 18643 ipst, ALL_ZONES); 18644 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 18645 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 18646 } 18647 18648 ILL_SEND_TX(ill, ire, connp, mp, 0, NULL); 18649 } 18650 18651 IRE_REFRELE(ire); 18652 } 18653 18654 /* 18655 * This handles the case when the receiver has shrunk its win. Per RFC 1122 18656 * if the receiver shrinks the window, i.e. moves the right window to the 18657 * left, the we should not send new data, but should retransmit normally the 18658 * old unacked data between suna and suna + swnd. We might has sent data 18659 * that is now outside the new window, pretend that we didn't send it. 18660 */ 18661 static void 18662 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 18663 { 18664 uint32_t snxt = tcp->tcp_snxt; 18665 18666 ASSERT(shrunk_count > 0); 18667 18668 if (!tcp->tcp_is_wnd_shrnk) { 18669 tcp->tcp_snxt_shrunk = snxt; 18670 tcp->tcp_is_wnd_shrnk = B_TRUE; 18671 } else if (SEQ_GT(snxt, tcp->tcp_snxt_shrunk)) { 18672 tcp->tcp_snxt_shrunk = snxt; 18673 } 18674 18675 /* Pretend we didn't send the data outside the window */ 18676 snxt -= shrunk_count; 18677 18678 /* Reset all the values per the now shrunk window */ 18679 tcp_update_xmit_tail(tcp, snxt); 18680 tcp->tcp_unsent += shrunk_count; 18681 18682 /* 18683 * If the SACK option is set, delete the entire list of 18684 * notsack'ed blocks. 18685 */ 18686 if (tcp->tcp_sack_info != NULL) { 18687 if (tcp->tcp_notsack_list != NULL) 18688 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp); 18689 } 18690 18691 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 18692 /* 18693 * Make sure the timer is running so that we will probe a zero 18694 * window. 18695 */ 18696 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18697 } 18698 18699 18700 /* 18701 * The TCP normal data output path. 18702 * NOTE: the logic of the fast path is duplicated from this function. 18703 */ 18704 static void 18705 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 18706 { 18707 int len; 18708 mblk_t *local_time; 18709 mblk_t *mp1; 18710 uint32_t snxt; 18711 int tail_unsent; 18712 int tcpstate; 18713 int usable = 0; 18714 mblk_t *xmit_tail; 18715 queue_t *q = tcp->tcp_wq; 18716 int32_t mss; 18717 int32_t num_sack_blk = 0; 18718 int32_t tcp_hdr_len; 18719 int32_t tcp_tcp_hdr_len; 18720 int mdt_thres; 18721 int rc; 18722 tcp_stack_t *tcps = tcp->tcp_tcps; 18723 ip_stack_t *ipst; 18724 18725 tcpstate = tcp->tcp_state; 18726 if (mp == NULL) { 18727 /* 18728 * tcp_wput_data() with NULL mp should only be called when 18729 * there is unsent data. 18730 */ 18731 ASSERT(tcp->tcp_unsent > 0); 18732 /* Really tacky... but we need this for detached closes. */ 18733 len = tcp->tcp_unsent; 18734 goto data_null; 18735 } 18736 18737 #if CCS_STATS 18738 wrw_stats.tot.count++; 18739 wrw_stats.tot.bytes += msgdsize(mp); 18740 #endif 18741 ASSERT(mp->b_datap->db_type == M_DATA); 18742 /* 18743 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 18744 * or before a connection attempt has begun. 18745 */ 18746 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 18747 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18748 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18749 #ifdef DEBUG 18750 cmn_err(CE_WARN, 18751 "tcp_wput_data: data after ordrel, %s", 18752 tcp_display(tcp, NULL, 18753 DISP_ADDR_AND_PORT)); 18754 #else 18755 if (tcp->tcp_debug) { 18756 (void) strlog(TCP_MOD_ID, 0, 1, 18757 SL_TRACE|SL_ERROR, 18758 "tcp_wput_data: data after ordrel, %s\n", 18759 tcp_display(tcp, NULL, 18760 DISP_ADDR_AND_PORT)); 18761 } 18762 #endif /* DEBUG */ 18763 } 18764 if (tcp->tcp_snd_zcopy_aware && 18765 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 18766 tcp_zcopy_notify(tcp); 18767 freemsg(mp); 18768 mutex_enter(&tcp->tcp_non_sq_lock); 18769 if (tcp->tcp_flow_stopped && 18770 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18771 tcp_clrqfull(tcp); 18772 } 18773 mutex_exit(&tcp->tcp_non_sq_lock); 18774 return; 18775 } 18776 18777 /* Strip empties */ 18778 for (;;) { 18779 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 18780 (uintptr_t)INT_MAX); 18781 len = (int)(mp->b_wptr - mp->b_rptr); 18782 if (len > 0) 18783 break; 18784 mp1 = mp; 18785 mp = mp->b_cont; 18786 freeb(mp1); 18787 if (!mp) { 18788 return; 18789 } 18790 } 18791 18792 /* If we are the first on the list ... */ 18793 if (tcp->tcp_xmit_head == NULL) { 18794 tcp->tcp_xmit_head = mp; 18795 tcp->tcp_xmit_tail = mp; 18796 tcp->tcp_xmit_tail_unsent = len; 18797 } else { 18798 /* If tiny tx and room in txq tail, pullup to save mblks. */ 18799 struct datab *dp; 18800 18801 mp1 = tcp->tcp_xmit_last; 18802 if (len < tcp_tx_pull_len && 18803 (dp = mp1->b_datap)->db_ref == 1 && 18804 dp->db_lim - mp1->b_wptr >= len) { 18805 ASSERT(len > 0); 18806 ASSERT(!mp1->b_cont); 18807 if (len == 1) { 18808 *mp1->b_wptr++ = *mp->b_rptr; 18809 } else { 18810 bcopy(mp->b_rptr, mp1->b_wptr, len); 18811 mp1->b_wptr += len; 18812 } 18813 if (mp1 == tcp->tcp_xmit_tail) 18814 tcp->tcp_xmit_tail_unsent += len; 18815 mp1->b_cont = mp->b_cont; 18816 if (tcp->tcp_snd_zcopy_aware && 18817 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 18818 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18819 freeb(mp); 18820 mp = mp1; 18821 } else { 18822 tcp->tcp_xmit_last->b_cont = mp; 18823 } 18824 len += tcp->tcp_unsent; 18825 } 18826 18827 /* Tack on however many more positive length mblks we have */ 18828 if ((mp1 = mp->b_cont) != NULL) { 18829 do { 18830 int tlen; 18831 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 18832 (uintptr_t)INT_MAX); 18833 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 18834 if (tlen <= 0) { 18835 mp->b_cont = mp1->b_cont; 18836 freeb(mp1); 18837 } else { 18838 len += tlen; 18839 mp = mp1; 18840 } 18841 } while ((mp1 = mp->b_cont) != NULL); 18842 } 18843 tcp->tcp_xmit_last = mp; 18844 tcp->tcp_unsent = len; 18845 18846 if (urgent) 18847 usable = 1; 18848 18849 data_null: 18850 snxt = tcp->tcp_snxt; 18851 xmit_tail = tcp->tcp_xmit_tail; 18852 tail_unsent = tcp->tcp_xmit_tail_unsent; 18853 18854 /* 18855 * Note that tcp_mss has been adjusted to take into account the 18856 * timestamp option if applicable. Because SACK options do not 18857 * appear in every TCP segments and they are of variable lengths, 18858 * they cannot be included in tcp_mss. Thus we need to calculate 18859 * the actual segment length when we need to send a segment which 18860 * includes SACK options. 18861 */ 18862 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 18863 int32_t opt_len; 18864 18865 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 18866 tcp->tcp_num_sack_blk); 18867 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 18868 2 + TCPOPT_HEADER_LEN; 18869 mss = tcp->tcp_mss - opt_len; 18870 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 18871 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 18872 } else { 18873 mss = tcp->tcp_mss; 18874 tcp_hdr_len = tcp->tcp_hdr_len; 18875 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 18876 } 18877 18878 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 18879 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 18880 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 18881 } 18882 if (tcpstate == TCPS_SYN_RCVD) { 18883 /* 18884 * The three-way connection establishment handshake is not 18885 * complete yet. We want to queue the data for transmission 18886 * after entering ESTABLISHED state (RFC793). A jump to 18887 * "done" label effectively leaves data on the queue. 18888 */ 18889 goto done; 18890 } else { 18891 int usable_r; 18892 18893 /* 18894 * In the special case when cwnd is zero, which can only 18895 * happen if the connection is ECN capable, return now. 18896 * New segments is sent using tcp_timer(). The timer 18897 * is set in tcp_rput_data(). 18898 */ 18899 if (tcp->tcp_cwnd == 0) { 18900 /* 18901 * Note that tcp_cwnd is 0 before 3-way handshake is 18902 * finished. 18903 */ 18904 ASSERT(tcp->tcp_ecn_ok || 18905 tcp->tcp_state < TCPS_ESTABLISHED); 18906 return; 18907 } 18908 18909 /* NOTE: trouble if xmitting while SYN not acked? */ 18910 usable_r = snxt - tcp->tcp_suna; 18911 usable_r = tcp->tcp_swnd - usable_r; 18912 18913 /* 18914 * Check if the receiver has shrunk the window. If 18915 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 18916 * cannot be set as there is unsent data, so FIN cannot 18917 * be sent out. Otherwise, we need to take into account 18918 * of FIN as it consumes an "invisible" sequence number. 18919 */ 18920 ASSERT(tcp->tcp_fin_sent == 0); 18921 if (usable_r < 0) { 18922 /* 18923 * The receiver has shrunk the window and we have sent 18924 * -usable_r date beyond the window, re-adjust. 18925 * 18926 * If TCP window scaling is enabled, there can be 18927 * round down error as the advertised receive window 18928 * is actually right shifted n bits. This means that 18929 * the lower n bits info is wiped out. It will look 18930 * like the window is shrunk. Do a check here to 18931 * see if the shrunk amount is actually within the 18932 * error in window calculation. If it is, just 18933 * return. Note that this check is inside the 18934 * shrunk window check. This makes sure that even 18935 * though tcp_process_shrunk_swnd() is not called, 18936 * we will stop further processing. 18937 */ 18938 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 18939 tcp_process_shrunk_swnd(tcp, -usable_r); 18940 } 18941 return; 18942 } 18943 18944 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 18945 if (tcp->tcp_swnd > tcp->tcp_cwnd) 18946 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 18947 18948 /* usable = MIN(usable, unsent) */ 18949 if (usable_r > len) 18950 usable_r = len; 18951 18952 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 18953 if (usable_r > 0) { 18954 usable = usable_r; 18955 } else { 18956 /* Bypass all other unnecessary processing. */ 18957 goto done; 18958 } 18959 } 18960 18961 local_time = (mblk_t *)lbolt; 18962 18963 /* 18964 * "Our" Nagle Algorithm. This is not the same as in the old 18965 * BSD. This is more in line with the true intent of Nagle. 18966 * 18967 * The conditions are: 18968 * 1. The amount of unsent data (or amount of data which can be 18969 * sent, whichever is smaller) is less than Nagle limit. 18970 * 2. The last sent size is also less than Nagle limit. 18971 * 3. There is unack'ed data. 18972 * 4. Urgent pointer is not set. Send urgent data ignoring the 18973 * Nagle algorithm. This reduces the probability that urgent 18974 * bytes get "merged" together. 18975 * 5. The app has not closed the connection. This eliminates the 18976 * wait time of the receiving side waiting for the last piece of 18977 * (small) data. 18978 * 18979 * If all are satisified, exit without sending anything. Note 18980 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 18981 * the smaller of 1 MSS and global tcp_naglim_def (default to be 18982 * 4095). 18983 */ 18984 if (usable < (int)tcp->tcp_naglim && 18985 tcp->tcp_naglim > tcp->tcp_last_sent_len && 18986 snxt != tcp->tcp_suna && 18987 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 18988 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 18989 goto done; 18990 } 18991 18992 /* 18993 * If tcp_zero_win_probe is not set and the tcp->tcp_cork option 18994 * is set, then we have to force TCP not to send partial segment 18995 * (smaller than MSS bytes). We are calculating the usable now 18996 * based on full mss and will save the rest of remaining data for 18997 * later. When tcp_zero_win_probe is set, TCP needs to send out 18998 * something to do zero window probe. 18999 */ 19000 if (tcp->tcp_cork && !tcp->tcp_zero_win_probe) { 19001 if (usable < mss) 19002 goto done; 19003 usable = (usable / mss) * mss; 19004 } 19005 19006 /* Update the latest receive window size in TCP header. */ 19007 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19008 tcp->tcp_tcph->th_win); 19009 19010 /* 19011 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19012 * 19013 * 1. Simple TCP/IP{v4,v6} (no options). 19014 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19015 * 3. If the TCP connection is in ESTABLISHED state. 19016 * 4. The TCP is not detached. 19017 * 19018 * If any of the above conditions have changed during the 19019 * connection, stop using LSO/MDT and restore the stream head 19020 * parameters accordingly. 19021 */ 19022 ipst = tcps->tcps_netstack->netstack_ip; 19023 19024 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19025 ((tcp->tcp_ipversion == IPV4_VERSION && 19026 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19027 (tcp->tcp_ipversion == IPV6_VERSION && 19028 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19029 tcp->tcp_state != TCPS_ESTABLISHED || 19030 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19031 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19032 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19033 if (tcp->tcp_lso) { 19034 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19035 tcp->tcp_lso = B_FALSE; 19036 } else { 19037 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19038 tcp->tcp_mdt = B_FALSE; 19039 } 19040 19041 /* Anything other than detached is considered pathological */ 19042 if (!TCP_IS_DETACHED(tcp)) { 19043 if (tcp->tcp_lso) 19044 TCP_STAT(tcps, tcp_lso_disabled); 19045 else 19046 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19047 (void) tcp_maxpsz_set(tcp, B_TRUE); 19048 } 19049 } 19050 19051 /* Use MDT if sendable amount is greater than the threshold */ 19052 if (tcp->tcp_mdt && 19053 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19054 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19055 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19056 (tcp->tcp_valid_bits == 0 || 19057 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19058 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19059 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19060 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19061 local_time, mdt_thres); 19062 } else { 19063 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19064 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19065 local_time, INT_MAX); 19066 } 19067 19068 /* Pretend that all we were trying to send really got sent */ 19069 if (rc < 0 && tail_unsent < 0) { 19070 do { 19071 xmit_tail = xmit_tail->b_cont; 19072 xmit_tail->b_prev = local_time; 19073 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19074 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19075 tail_unsent += (int)(xmit_tail->b_wptr - 19076 xmit_tail->b_rptr); 19077 } while (tail_unsent < 0); 19078 } 19079 done:; 19080 tcp->tcp_xmit_tail = xmit_tail; 19081 tcp->tcp_xmit_tail_unsent = tail_unsent; 19082 len = tcp->tcp_snxt - snxt; 19083 if (len) { 19084 /* 19085 * If new data was sent, need to update the notsack 19086 * list, which is, afterall, data blocks that have 19087 * not been sack'ed by the receiver. New data is 19088 * not sack'ed. 19089 */ 19090 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19091 /* len is a negative value. */ 19092 tcp->tcp_pipe -= len; 19093 tcp_notsack_update(&(tcp->tcp_notsack_list), 19094 tcp->tcp_snxt, snxt, 19095 &(tcp->tcp_num_notsack_blk), 19096 &(tcp->tcp_cnt_notsack_list)); 19097 } 19098 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19099 tcp->tcp_rack = tcp->tcp_rnxt; 19100 tcp->tcp_rack_cnt = 0; 19101 if ((snxt + len) == tcp->tcp_suna) { 19102 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19103 } 19104 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19105 /* 19106 * Didn't send anything. Make sure the timer is running 19107 * so that we will probe a zero window. 19108 */ 19109 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19110 } 19111 /* Note that len is the amount we just sent but with a negative sign */ 19112 tcp->tcp_unsent += len; 19113 mutex_enter(&tcp->tcp_non_sq_lock); 19114 if (tcp->tcp_flow_stopped) { 19115 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19116 tcp_clrqfull(tcp); 19117 } 19118 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19119 tcp_setqfull(tcp); 19120 } 19121 mutex_exit(&tcp->tcp_non_sq_lock); 19122 } 19123 19124 /* 19125 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19126 * outgoing TCP header with the template header, as well as other 19127 * options such as time-stamp, ECN and/or SACK. 19128 */ 19129 static void 19130 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19131 { 19132 tcph_t *tcp_tmpl, *tcp_h; 19133 uint32_t *dst, *src; 19134 int hdrlen; 19135 19136 ASSERT(OK_32PTR(rptr)); 19137 19138 /* Template header */ 19139 tcp_tmpl = tcp->tcp_tcph; 19140 19141 /* Header of outgoing packet */ 19142 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19143 19144 /* dst and src are opaque 32-bit fields, used for copying */ 19145 dst = (uint32_t *)rptr; 19146 src = (uint32_t *)tcp->tcp_iphc; 19147 hdrlen = tcp->tcp_hdr_len; 19148 19149 /* Fill time-stamp option if needed */ 19150 if (tcp->tcp_snd_ts_ok) { 19151 U32_TO_BE32((uint32_t)now, 19152 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19153 U32_TO_BE32(tcp->tcp_ts_recent, 19154 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19155 } else { 19156 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19157 } 19158 19159 /* 19160 * Copy the template header; is this really more efficient than 19161 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19162 * but perhaps not for other scenarios. 19163 */ 19164 dst[0] = src[0]; 19165 dst[1] = src[1]; 19166 dst[2] = src[2]; 19167 dst[3] = src[3]; 19168 dst[4] = src[4]; 19169 dst[5] = src[5]; 19170 dst[6] = src[6]; 19171 dst[7] = src[7]; 19172 dst[8] = src[8]; 19173 dst[9] = src[9]; 19174 if (hdrlen -= 40) { 19175 hdrlen >>= 2; 19176 dst += 10; 19177 src += 10; 19178 do { 19179 *dst++ = *src++; 19180 } while (--hdrlen); 19181 } 19182 19183 /* 19184 * Set the ECN info in the TCP header if it is not a zero 19185 * window probe. Zero window probe is only sent in 19186 * tcp_wput_data() and tcp_timer(). 19187 */ 19188 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19189 SET_ECT(tcp, rptr); 19190 19191 if (tcp->tcp_ecn_echo_on) 19192 tcp_h->th_flags[0] |= TH_ECE; 19193 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19194 tcp_h->th_flags[0] |= TH_CWR; 19195 tcp->tcp_ecn_cwr_sent = B_TRUE; 19196 } 19197 } 19198 19199 /* Fill in SACK options */ 19200 if (num_sack_blk > 0) { 19201 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19202 sack_blk_t *tmp; 19203 int32_t i; 19204 19205 wptr[0] = TCPOPT_NOP; 19206 wptr[1] = TCPOPT_NOP; 19207 wptr[2] = TCPOPT_SACK; 19208 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19209 sizeof (sack_blk_t); 19210 wptr += TCPOPT_REAL_SACK_LEN; 19211 19212 tmp = tcp->tcp_sack_list; 19213 for (i = 0; i < num_sack_blk; i++) { 19214 U32_TO_BE32(tmp[i].begin, wptr); 19215 wptr += sizeof (tcp_seq); 19216 U32_TO_BE32(tmp[i].end, wptr); 19217 wptr += sizeof (tcp_seq); 19218 } 19219 tcp_h->th_offset_and_rsrvd[0] += 19220 ((num_sack_blk * 2 + 1) << 4); 19221 } 19222 } 19223 19224 /* 19225 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19226 * the destination address and SAP attribute, and if necessary, the 19227 * hardware checksum offload attribute to a Multidata message. 19228 */ 19229 static int 19230 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19231 const uint32_t start, const uint32_t stuff, const uint32_t end, 19232 const uint32_t flags, tcp_stack_t *tcps) 19233 { 19234 /* Add global destination address & SAP attribute */ 19235 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19236 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19237 "destination address+SAP\n")); 19238 19239 if (dlmp != NULL) 19240 TCP_STAT(tcps, tcp_mdt_allocfail); 19241 return (-1); 19242 } 19243 19244 /* Add global hwcksum attribute */ 19245 if (hwcksum && 19246 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19247 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19248 "checksum attribute\n")); 19249 19250 TCP_STAT(tcps, tcp_mdt_allocfail); 19251 return (-1); 19252 } 19253 19254 return (0); 19255 } 19256 19257 /* 19258 * Smaller and private version of pdescinfo_t used specifically for TCP, 19259 * which allows for only two payload spans per packet. 19260 */ 19261 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19262 19263 /* 19264 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19265 * scheme, and returns one the following: 19266 * 19267 * -1 = failed allocation. 19268 * 0 = success; burst count reached, or usable send window is too small, 19269 * and that we'd rather wait until later before sending again. 19270 */ 19271 static int 19272 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19273 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19274 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19275 const int mdt_thres) 19276 { 19277 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19278 multidata_t *mmd; 19279 uint_t obsegs, obbytes, hdr_frag_sz; 19280 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19281 int num_burst_seg, max_pld; 19282 pdesc_t *pkt; 19283 tcp_pdescinfo_t tcp_pkt_info; 19284 pdescinfo_t *pkt_info; 19285 int pbuf_idx, pbuf_idx_nxt; 19286 int seg_len, len, spill, af; 19287 boolean_t add_buffer, zcopy, clusterwide; 19288 boolean_t rconfirm = B_FALSE; 19289 boolean_t done = B_FALSE; 19290 uint32_t cksum; 19291 uint32_t hwcksum_flags; 19292 ire_t *ire = NULL; 19293 ill_t *ill; 19294 ipha_t *ipha; 19295 ip6_t *ip6h; 19296 ipaddr_t src, dst; 19297 ill_zerocopy_capab_t *zc_cap = NULL; 19298 uint16_t *up; 19299 int err; 19300 conn_t *connp; 19301 tcp_stack_t *tcps = tcp->tcp_tcps; 19302 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19303 int usable_mmd, tail_unsent_mmd; 19304 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 19305 mblk_t *xmit_tail_mmd; 19306 netstackid_t stack_id; 19307 19308 #ifdef _BIG_ENDIAN 19309 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19310 #else 19311 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19312 #endif 19313 19314 #define PREP_NEW_MULTIDATA() { \ 19315 mmd = NULL; \ 19316 md_mp = md_hbuf = NULL; \ 19317 cur_hdr_off = 0; \ 19318 max_pld = tcp->tcp_mdt_max_pld; \ 19319 pbuf_idx = pbuf_idx_nxt = -1; \ 19320 add_buffer = B_TRUE; \ 19321 zcopy = B_FALSE; \ 19322 } 19323 19324 #define PREP_NEW_PBUF() { \ 19325 md_pbuf = md_pbuf_nxt = NULL; \ 19326 pbuf_idx = pbuf_idx_nxt = -1; \ 19327 cur_pld_off = 0; \ 19328 first_snxt = *snxt; \ 19329 ASSERT(*tail_unsent > 0); \ 19330 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19331 } 19332 19333 ASSERT(mdt_thres >= mss); 19334 ASSERT(*usable > 0 && *usable > mdt_thres); 19335 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19336 ASSERT(!TCP_IS_DETACHED(tcp)); 19337 ASSERT(tcp->tcp_valid_bits == 0 || 19338 tcp->tcp_valid_bits == TCP_FSS_VALID); 19339 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19340 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19341 (tcp->tcp_ipversion == IPV6_VERSION && 19342 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19343 19344 connp = tcp->tcp_connp; 19345 ASSERT(connp != NULL); 19346 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 19347 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19348 19349 stack_id = connp->conn_netstack->netstack_stackid; 19350 19351 usable_mmd = tail_unsent_mmd = 0; 19352 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 19353 xmit_tail_mmd = NULL; 19354 /* 19355 * Note that tcp will only declare at most 2 payload spans per 19356 * packet, which is much lower than the maximum allowable number 19357 * of packet spans per Multidata. For this reason, we use the 19358 * privately declared and smaller descriptor info structure, in 19359 * order to save some stack space. 19360 */ 19361 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19362 19363 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19364 if (af == AF_INET) { 19365 dst = tcp->tcp_ipha->ipha_dst; 19366 src = tcp->tcp_ipha->ipha_src; 19367 ASSERT(!CLASSD(dst)); 19368 } 19369 ASSERT(af == AF_INET || 19370 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19371 19372 obsegs = obbytes = 0; 19373 num_burst_seg = tcp->tcp_snd_burst; 19374 md_mp_head = NULL; 19375 PREP_NEW_MULTIDATA(); 19376 19377 /* 19378 * Before we go on further, make sure there is an IRE that we can 19379 * use, and that the ILL supports MDT. Otherwise, there's no point 19380 * in proceeding any further, and we should just hand everything 19381 * off to the legacy path. 19382 */ 19383 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 19384 goto legacy_send_no_md; 19385 19386 ASSERT(ire != NULL); 19387 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19388 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19389 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19390 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19391 /* 19392 * If we do support loopback for MDT (which requires modifications 19393 * to the receiving paths), the following assertions should go away, 19394 * and we would be sending the Multidata to loopback conn later on. 19395 */ 19396 ASSERT(!IRE_IS_LOCAL(ire)); 19397 ASSERT(ire->ire_stq != NULL); 19398 19399 ill = ire_to_ill(ire); 19400 ASSERT(ill != NULL); 19401 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19402 19403 if (!tcp->tcp_ire_ill_check_done) { 19404 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19405 tcp->tcp_ire_ill_check_done = B_TRUE; 19406 } 19407 19408 /* 19409 * If the underlying interface conditions have changed, or if the 19410 * new interface does not support MDT, go back to legacy path. 19411 */ 19412 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19413 /* don't go through this path anymore for this connection */ 19414 TCP_STAT(tcps, tcp_mdt_conn_halted2); 19415 tcp->tcp_mdt = B_FALSE; 19416 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19417 "interface %s\n", (void *)connp, ill->ill_name)); 19418 /* IRE will be released prior to returning */ 19419 goto legacy_send_no_md; 19420 } 19421 19422 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19423 zc_cap = ill->ill_zerocopy_capab; 19424 19425 /* 19426 * Check if we can take tcp fast-path. Note that "incomplete" 19427 * ire's (where the link-layer for next hop is not resolved 19428 * or where the fast-path header in nce_fp_mp is not available 19429 * yet) are sent down the legacy (slow) path. 19430 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19431 */ 19432 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19433 /* IRE will be released prior to returning */ 19434 goto legacy_send_no_md; 19435 } 19436 19437 /* go to legacy path if interface doesn't support zerocopy */ 19438 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19439 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19440 /* IRE will be released prior to returning */ 19441 goto legacy_send_no_md; 19442 } 19443 19444 /* does the interface support hardware checksum offload? */ 19445 hwcksum_flags = 0; 19446 if (ILL_HCKSUM_CAPABLE(ill) && 19447 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19448 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19449 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19450 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19451 HCKSUM_IPHDRCKSUM) 19452 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19453 19454 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19455 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19456 hwcksum_flags |= HCK_FULLCKSUM; 19457 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19458 HCKSUM_INET_PARTIAL) 19459 hwcksum_flags |= HCK_PARTIALCKSUM; 19460 } 19461 19462 /* 19463 * Each header fragment consists of the leading extra space, 19464 * followed by the TCP/IP header, and the trailing extra space. 19465 * We make sure that each header fragment begins on a 32-bit 19466 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19467 * aligned in tcp_mdt_update). 19468 */ 19469 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19470 tcp->tcp_mdt_hdr_tail), 4); 19471 19472 /* are we starting from the beginning of data block? */ 19473 if (*tail_unsent == 0) { 19474 *xmit_tail = (*xmit_tail)->b_cont; 19475 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19476 *tail_unsent = (int)MBLKL(*xmit_tail); 19477 } 19478 19479 /* 19480 * Here we create one or more Multidata messages, each made up of 19481 * one header buffer and up to N payload buffers. This entire 19482 * operation is done within two loops: 19483 * 19484 * The outer loop mostly deals with creating the Multidata message, 19485 * as well as the header buffer that gets added to it. It also 19486 * links the Multidata messages together such that all of them can 19487 * be sent down to the lower layer in a single putnext call; this 19488 * linking behavior depends on the tcp_mdt_chain tunable. 19489 * 19490 * The inner loop takes an existing Multidata message, and adds 19491 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19492 * packetizes those buffers by filling up the corresponding header 19493 * buffer fragments with the proper IP and TCP headers, and by 19494 * describing the layout of each packet in the packet descriptors 19495 * that get added to the Multidata. 19496 */ 19497 do { 19498 /* 19499 * If usable send window is too small, or data blocks in 19500 * transmit list are smaller than our threshold (i.e. app 19501 * performs large writes followed by small ones), we hand 19502 * off the control over to the legacy path. Note that we'll 19503 * get back the control once it encounters a large block. 19504 */ 19505 if (*usable < mss || (*tail_unsent <= mdt_thres && 19506 (*xmit_tail)->b_cont != NULL && 19507 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19508 /* send down what we've got so far */ 19509 if (md_mp_head != NULL) { 19510 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19511 obsegs, obbytes, &rconfirm); 19512 } 19513 /* 19514 * Pass control over to tcp_send(), but tell it to 19515 * return to us once a large-size transmission is 19516 * possible. 19517 */ 19518 TCP_STAT(tcps, tcp_mdt_legacy_small); 19519 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19520 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19521 tail_unsent, xmit_tail, local_time, 19522 mdt_thres)) <= 0) { 19523 /* burst count reached, or alloc failed */ 19524 IRE_REFRELE(ire); 19525 return (err); 19526 } 19527 19528 /* tcp_send() may have sent everything, so check */ 19529 if (*usable <= 0) { 19530 IRE_REFRELE(ire); 19531 return (0); 19532 } 19533 19534 TCP_STAT(tcps, tcp_mdt_legacy_ret); 19535 /* 19536 * We may have delivered the Multidata, so make sure 19537 * to re-initialize before the next round. 19538 */ 19539 md_mp_head = NULL; 19540 obsegs = obbytes = 0; 19541 num_burst_seg = tcp->tcp_snd_burst; 19542 PREP_NEW_MULTIDATA(); 19543 19544 /* are we starting from the beginning of data block? */ 19545 if (*tail_unsent == 0) { 19546 *xmit_tail = (*xmit_tail)->b_cont; 19547 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19548 (uintptr_t)INT_MAX); 19549 *tail_unsent = (int)MBLKL(*xmit_tail); 19550 } 19551 } 19552 /* 19553 * Record current values for parameters we may need to pass 19554 * to tcp_send() or tcp_multisend_data(). We checkpoint at 19555 * each iteration of the outer loop (each multidata message 19556 * creation). If we have a failure in the inner loop, we send 19557 * any complete multidata messages we have before reverting 19558 * to using the traditional non-md path. 19559 */ 19560 snxt_mmd = *snxt; 19561 usable_mmd = *usable; 19562 xmit_tail_mmd = *xmit_tail; 19563 tail_unsent_mmd = *tail_unsent; 19564 obsegs_mmd = obsegs; 19565 obbytes_mmd = obbytes; 19566 19567 /* 19568 * max_pld limits the number of mblks in tcp's transmit 19569 * queue that can be added to a Multidata message. Once 19570 * this counter reaches zero, no more additional mblks 19571 * can be added to it. What happens afterwards depends 19572 * on whether or not we are set to chain the Multidata 19573 * messages. If we are to link them together, reset 19574 * max_pld to its original value (tcp_mdt_max_pld) and 19575 * prepare to create a new Multidata message which will 19576 * get linked to md_mp_head. Else, leave it alone and 19577 * let the inner loop break on its own. 19578 */ 19579 if (tcp_mdt_chain && max_pld == 0) 19580 PREP_NEW_MULTIDATA(); 19581 19582 /* adding a payload buffer; re-initialize values */ 19583 if (add_buffer) 19584 PREP_NEW_PBUF(); 19585 19586 /* 19587 * If we don't have a Multidata, either because we just 19588 * (re)entered this outer loop, or after we branched off 19589 * to tcp_send above, setup the Multidata and header 19590 * buffer to be used. 19591 */ 19592 if (md_mp == NULL) { 19593 int md_hbuflen; 19594 uint32_t start, stuff; 19595 19596 /* 19597 * Calculate Multidata header buffer size large enough 19598 * to hold all of the headers that can possibly be 19599 * sent at this moment. We'd rather over-estimate 19600 * the size than running out of space; this is okay 19601 * since this buffer is small anyway. 19602 */ 19603 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19604 19605 /* 19606 * Start and stuff offset for partial hardware 19607 * checksum offload; these are currently for IPv4. 19608 * For full checksum offload, they are set to zero. 19609 */ 19610 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19611 if (af == AF_INET) { 19612 start = IP_SIMPLE_HDR_LENGTH; 19613 stuff = IP_SIMPLE_HDR_LENGTH + 19614 TCP_CHECKSUM_OFFSET; 19615 } else { 19616 start = IPV6_HDR_LEN; 19617 stuff = IPV6_HDR_LEN + 19618 TCP_CHECKSUM_OFFSET; 19619 } 19620 } else { 19621 start = stuff = 0; 19622 } 19623 19624 /* 19625 * Create the header buffer, Multidata, as well as 19626 * any necessary attributes (destination address, 19627 * SAP and hardware checksum offload) that should 19628 * be associated with the Multidata message. 19629 */ 19630 ASSERT(cur_hdr_off == 0); 19631 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19632 ((md_hbuf->b_wptr += md_hbuflen), 19633 (mmd = mmd_alloc(md_hbuf, &md_mp, 19634 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19635 /* fastpath mblk */ 19636 ire->ire_nce->nce_res_mp, 19637 /* hardware checksum enabled */ 19638 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19639 /* hardware checksum offsets */ 19640 start, stuff, 0, 19641 /* hardware checksum flag */ 19642 hwcksum_flags, tcps) != 0)) { 19643 legacy_send: 19644 /* 19645 * We arrive here from a failure within the 19646 * inner (packetizer) loop or we fail one of 19647 * the conditionals above. We restore the 19648 * previously checkpointed values for: 19649 * xmit_tail 19650 * usable 19651 * tail_unsent 19652 * snxt 19653 * obbytes 19654 * obsegs 19655 * We should then be able to dispatch any 19656 * complete multidata before reverting to the 19657 * traditional path with consistent parameters 19658 * (the inner loop updates these as it 19659 * iterates). 19660 */ 19661 *xmit_tail = xmit_tail_mmd; 19662 *usable = usable_mmd; 19663 *tail_unsent = tail_unsent_mmd; 19664 *snxt = snxt_mmd; 19665 obbytes = obbytes_mmd; 19666 obsegs = obsegs_mmd; 19667 if (md_mp != NULL) { 19668 /* Unlink message from the chain */ 19669 if (md_mp_head != NULL) { 19670 err = (intptr_t)rmvb(md_mp_head, 19671 md_mp); 19672 /* 19673 * We can't assert that rmvb 19674 * did not return -1, since we 19675 * may get here before linkb 19676 * happens. We do, however, 19677 * check if we just removed the 19678 * only element in the list. 19679 */ 19680 if (err == 0) 19681 md_mp_head = NULL; 19682 } 19683 /* md_hbuf gets freed automatically */ 19684 TCP_STAT(tcps, tcp_mdt_discarded); 19685 freeb(md_mp); 19686 } else { 19687 /* Either allocb or mmd_alloc failed */ 19688 TCP_STAT(tcps, tcp_mdt_allocfail); 19689 if (md_hbuf != NULL) 19690 freeb(md_hbuf); 19691 } 19692 19693 /* send down what we've got so far */ 19694 if (md_mp_head != NULL) { 19695 tcp_multisend_data(tcp, ire, ill, 19696 md_mp_head, obsegs, obbytes, 19697 &rconfirm); 19698 } 19699 legacy_send_no_md: 19700 if (ire != NULL) 19701 IRE_REFRELE(ire); 19702 /* 19703 * Too bad; let the legacy path handle this. 19704 * We specify INT_MAX for the threshold, since 19705 * we gave up with the Multidata processings 19706 * and let the old path have it all. 19707 */ 19708 TCP_STAT(tcps, tcp_mdt_legacy_all); 19709 return (tcp_send(q, tcp, mss, tcp_hdr_len, 19710 tcp_tcp_hdr_len, num_sack_blk, usable, 19711 snxt, tail_unsent, xmit_tail, local_time, 19712 INT_MAX)); 19713 } 19714 19715 /* link to any existing ones, if applicable */ 19716 TCP_STAT(tcps, tcp_mdt_allocd); 19717 if (md_mp_head == NULL) { 19718 md_mp_head = md_mp; 19719 } else if (tcp_mdt_chain) { 19720 TCP_STAT(tcps, tcp_mdt_linked); 19721 linkb(md_mp_head, md_mp); 19722 } 19723 } 19724 19725 ASSERT(md_mp_head != NULL); 19726 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 19727 ASSERT(md_mp != NULL && mmd != NULL); 19728 ASSERT(md_hbuf != NULL); 19729 19730 /* 19731 * Packetize the transmittable portion of the data block; 19732 * each data block is essentially added to the Multidata 19733 * as a payload buffer. We also deal with adding more 19734 * than one payload buffers, which happens when the remaining 19735 * packetized portion of the current payload buffer is less 19736 * than MSS, while the next data block in transmit queue 19737 * has enough data to make up for one. This "spillover" 19738 * case essentially creates a split-packet, where portions 19739 * of the packet's payload fragments may span across two 19740 * virtually discontiguous address blocks. 19741 */ 19742 seg_len = mss; 19743 do { 19744 len = seg_len; 19745 19746 /* one must remain NULL for DTRACE_IP_FASTPATH */ 19747 ipha = NULL; 19748 ip6h = NULL; 19749 19750 ASSERT(len > 0); 19751 ASSERT(max_pld >= 0); 19752 ASSERT(!add_buffer || cur_pld_off == 0); 19753 19754 /* 19755 * First time around for this payload buffer; note 19756 * in the case of a spillover, the following has 19757 * been done prior to adding the split-packet 19758 * descriptor to Multidata, and we don't want to 19759 * repeat the process. 19760 */ 19761 if (add_buffer) { 19762 ASSERT(mmd != NULL); 19763 ASSERT(md_pbuf == NULL); 19764 ASSERT(md_pbuf_nxt == NULL); 19765 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 19766 19767 /* 19768 * Have we reached the limit? We'd get to 19769 * this case when we're not chaining the 19770 * Multidata messages together, and since 19771 * we're done, terminate this loop. 19772 */ 19773 if (max_pld == 0) 19774 break; /* done */ 19775 19776 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 19777 TCP_STAT(tcps, tcp_mdt_allocfail); 19778 goto legacy_send; /* out_of_mem */ 19779 } 19780 19781 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 19782 zc_cap != NULL) { 19783 if (!ip_md_zcopy_attr(mmd, NULL, 19784 zc_cap->ill_zerocopy_flags)) { 19785 freeb(md_pbuf); 19786 TCP_STAT(tcps, 19787 tcp_mdt_allocfail); 19788 /* out_of_mem */ 19789 goto legacy_send; 19790 } 19791 zcopy = B_TRUE; 19792 } 19793 19794 md_pbuf->b_rptr += base_pld_off; 19795 19796 /* 19797 * Add a payload buffer to the Multidata; this 19798 * operation must not fail, or otherwise our 19799 * logic in this routine is broken. There 19800 * is no memory allocation done by the 19801 * routine, so any returned failure simply 19802 * tells us that we've done something wrong. 19803 * 19804 * A failure tells us that either we're adding 19805 * the same payload buffer more than once, or 19806 * we're trying to add more buffers than 19807 * allowed (max_pld calculation is wrong). 19808 * None of the above cases should happen, and 19809 * we panic because either there's horrible 19810 * heap corruption, and/or programming mistake. 19811 */ 19812 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 19813 if (pbuf_idx < 0) { 19814 cmn_err(CE_PANIC, "tcp_multisend: " 19815 "payload buffer logic error " 19816 "detected for tcp %p mmd %p " 19817 "pbuf %p (%d)\n", 19818 (void *)tcp, (void *)mmd, 19819 (void *)md_pbuf, pbuf_idx); 19820 } 19821 19822 ASSERT(max_pld > 0); 19823 --max_pld; 19824 add_buffer = B_FALSE; 19825 } 19826 19827 ASSERT(md_mp_head != NULL); 19828 ASSERT(md_pbuf != NULL); 19829 ASSERT(md_pbuf_nxt == NULL); 19830 ASSERT(pbuf_idx != -1); 19831 ASSERT(pbuf_idx_nxt == -1); 19832 ASSERT(*usable > 0); 19833 19834 /* 19835 * We spillover to the next payload buffer only 19836 * if all of the following is true: 19837 * 19838 * 1. There is not enough data on the current 19839 * payload buffer to make up `len', 19840 * 2. We are allowed to send `len', 19841 * 3. The next payload buffer length is large 19842 * enough to accomodate `spill'. 19843 */ 19844 if ((spill = len - *tail_unsent) > 0 && 19845 *usable >= len && 19846 MBLKL((*xmit_tail)->b_cont) >= spill && 19847 max_pld > 0) { 19848 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 19849 if (md_pbuf_nxt == NULL) { 19850 TCP_STAT(tcps, tcp_mdt_allocfail); 19851 goto legacy_send; /* out_of_mem */ 19852 } 19853 19854 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 19855 zc_cap != NULL) { 19856 if (!ip_md_zcopy_attr(mmd, NULL, 19857 zc_cap->ill_zerocopy_flags)) { 19858 freeb(md_pbuf_nxt); 19859 TCP_STAT(tcps, 19860 tcp_mdt_allocfail); 19861 /* out_of_mem */ 19862 goto legacy_send; 19863 } 19864 zcopy = B_TRUE; 19865 } 19866 19867 /* 19868 * See comments above on the first call to 19869 * mmd_addpldbuf for explanation on the panic. 19870 */ 19871 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 19872 if (pbuf_idx_nxt < 0) { 19873 panic("tcp_multisend: " 19874 "next payload buffer logic error " 19875 "detected for tcp %p mmd %p " 19876 "pbuf %p (%d)\n", 19877 (void *)tcp, (void *)mmd, 19878 (void *)md_pbuf_nxt, pbuf_idx_nxt); 19879 } 19880 19881 ASSERT(max_pld > 0); 19882 --max_pld; 19883 } else if (spill > 0) { 19884 /* 19885 * If there's a spillover, but the following 19886 * xmit_tail couldn't give us enough octets 19887 * to reach "len", then stop the current 19888 * Multidata creation and let the legacy 19889 * tcp_send() path take over. We don't want 19890 * to send the tiny segment as part of this 19891 * Multidata for performance reasons; instead, 19892 * we let the legacy path deal with grouping 19893 * it with the subsequent small mblks. 19894 */ 19895 if (*usable >= len && 19896 MBLKL((*xmit_tail)->b_cont) < spill) { 19897 max_pld = 0; 19898 break; /* done */ 19899 } 19900 19901 /* 19902 * We can't spillover, and we are near 19903 * the end of the current payload buffer, 19904 * so send what's left. 19905 */ 19906 ASSERT(*tail_unsent > 0); 19907 len = *tail_unsent; 19908 } 19909 19910 /* tail_unsent is negated if there is a spillover */ 19911 *tail_unsent -= len; 19912 *usable -= len; 19913 ASSERT(*usable >= 0); 19914 19915 if (*usable < mss) 19916 seg_len = *usable; 19917 /* 19918 * Sender SWS avoidance; see comments in tcp_send(); 19919 * everything else is the same, except that we only 19920 * do this here if there is no more data to be sent 19921 * following the current xmit_tail. We don't check 19922 * for 1-byte urgent data because we shouldn't get 19923 * here if TCP_URG_VALID is set. 19924 */ 19925 if (*usable > 0 && *usable < mss && 19926 ((md_pbuf_nxt == NULL && 19927 (*xmit_tail)->b_cont == NULL) || 19928 (md_pbuf_nxt != NULL && 19929 (*xmit_tail)->b_cont->b_cont == NULL)) && 19930 seg_len < (tcp->tcp_max_swnd >> 1) && 19931 (tcp->tcp_unsent - 19932 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 19933 !tcp->tcp_zero_win_probe) { 19934 if ((*snxt + len) == tcp->tcp_snxt && 19935 (*snxt + len) == tcp->tcp_suna) { 19936 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19937 } 19938 done = B_TRUE; 19939 } 19940 19941 /* 19942 * Prime pump for IP's checksumming on our behalf; 19943 * include the adjustment for a source route if any. 19944 * Do this only for software/partial hardware checksum 19945 * offload, as this field gets zeroed out later for 19946 * the full hardware checksum offload case. 19947 */ 19948 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 19949 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 19950 cksum = (cksum >> 16) + (cksum & 0xFFFF); 19951 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 19952 } 19953 19954 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 19955 *snxt += len; 19956 19957 tcp->tcp_tcph->th_flags[0] = TH_ACK; 19958 /* 19959 * We set the PUSH bit only if TCP has no more buffered 19960 * data to be transmitted (or if sender SWS avoidance 19961 * takes place), as opposed to setting it for every 19962 * last packet in the burst. 19963 */ 19964 if (done || 19965 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 19966 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 19967 19968 /* 19969 * Set FIN bit if this is our last segment; snxt 19970 * already includes its length, and it will not 19971 * be adjusted after this point. 19972 */ 19973 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 19974 *snxt == tcp->tcp_fss) { 19975 if (!tcp->tcp_fin_acked) { 19976 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 19977 BUMP_MIB(&tcps->tcps_mib, 19978 tcpOutControl); 19979 } 19980 if (!tcp->tcp_fin_sent) { 19981 tcp->tcp_fin_sent = B_TRUE; 19982 /* 19983 * tcp state must be ESTABLISHED 19984 * in order for us to get here in 19985 * the first place. 19986 */ 19987 tcp->tcp_state = TCPS_FIN_WAIT_1; 19988 19989 /* 19990 * Upon returning from this routine, 19991 * tcp_wput_data() will set tcp_snxt 19992 * to be equal to snxt + tcp_fin_sent. 19993 * This is essentially the same as 19994 * setting it to tcp_fss + 1. 19995 */ 19996 } 19997 } 19998 19999 tcp->tcp_last_sent_len = (ushort_t)len; 20000 20001 len += tcp_hdr_len; 20002 if (tcp->tcp_ipversion == IPV4_VERSION) 20003 tcp->tcp_ipha->ipha_length = htons(len); 20004 else 20005 tcp->tcp_ip6h->ip6_plen = htons(len - 20006 ((char *)&tcp->tcp_ip6h[1] - 20007 tcp->tcp_iphc)); 20008 20009 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20010 20011 /* setup header fragment */ 20012 PDESC_HDR_ADD(pkt_info, 20013 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20014 tcp->tcp_mdt_hdr_head, /* head room */ 20015 tcp_hdr_len, /* len */ 20016 tcp->tcp_mdt_hdr_tail); /* tail room */ 20017 20018 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20019 hdr_frag_sz); 20020 ASSERT(MBLKIN(md_hbuf, 20021 (pkt_info->hdr_base - md_hbuf->b_rptr), 20022 PDESC_HDRSIZE(pkt_info))); 20023 20024 /* setup first payload fragment */ 20025 PDESC_PLD_INIT(pkt_info); 20026 PDESC_PLD_SPAN_ADD(pkt_info, 20027 pbuf_idx, /* index */ 20028 md_pbuf->b_rptr + cur_pld_off, /* start */ 20029 tcp->tcp_last_sent_len); /* len */ 20030 20031 /* create a split-packet in case of a spillover */ 20032 if (md_pbuf_nxt != NULL) { 20033 ASSERT(spill > 0); 20034 ASSERT(pbuf_idx_nxt > pbuf_idx); 20035 ASSERT(!add_buffer); 20036 20037 md_pbuf = md_pbuf_nxt; 20038 md_pbuf_nxt = NULL; 20039 pbuf_idx = pbuf_idx_nxt; 20040 pbuf_idx_nxt = -1; 20041 cur_pld_off = spill; 20042 20043 /* trim out first payload fragment */ 20044 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20045 20046 /* setup second payload fragment */ 20047 PDESC_PLD_SPAN_ADD(pkt_info, 20048 pbuf_idx, /* index */ 20049 md_pbuf->b_rptr, /* start */ 20050 spill); /* len */ 20051 20052 if ((*xmit_tail)->b_next == NULL) { 20053 /* 20054 * Store the lbolt used for RTT 20055 * estimation. We can only record one 20056 * timestamp per mblk so we do it when 20057 * we reach the end of the payload 20058 * buffer. Also we only take a new 20059 * timestamp sample when the previous 20060 * timed data from the same mblk has 20061 * been ack'ed. 20062 */ 20063 (*xmit_tail)->b_prev = local_time; 20064 (*xmit_tail)->b_next = 20065 (mblk_t *)(uintptr_t)first_snxt; 20066 } 20067 20068 first_snxt = *snxt - spill; 20069 20070 /* 20071 * Advance xmit_tail; usable could be 0 by 20072 * the time we got here, but we made sure 20073 * above that we would only spillover to 20074 * the next data block if usable includes 20075 * the spilled-over amount prior to the 20076 * subtraction. Therefore, we are sure 20077 * that xmit_tail->b_cont can't be NULL. 20078 */ 20079 ASSERT((*xmit_tail)->b_cont != NULL); 20080 *xmit_tail = (*xmit_tail)->b_cont; 20081 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20082 (uintptr_t)INT_MAX); 20083 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20084 } else { 20085 cur_pld_off += tcp->tcp_last_sent_len; 20086 } 20087 20088 /* 20089 * Fill in the header using the template header, and 20090 * add options such as time-stamp, ECN and/or SACK, 20091 * as needed. 20092 */ 20093 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20094 (clock_t)local_time, num_sack_blk); 20095 20096 /* take care of some IP header businesses */ 20097 if (af == AF_INET) { 20098 ipha = (ipha_t *)pkt_info->hdr_rptr; 20099 20100 ASSERT(OK_32PTR((uchar_t *)ipha)); 20101 ASSERT(PDESC_HDRL(pkt_info) >= 20102 IP_SIMPLE_HDR_LENGTH); 20103 ASSERT(ipha->ipha_version_and_hdr_length == 20104 IP_SIMPLE_HDR_VERSION); 20105 20106 /* 20107 * Assign ident value for current packet; see 20108 * related comments in ip_wput_ire() about the 20109 * contract private interface with clustering 20110 * group. 20111 */ 20112 clusterwide = B_FALSE; 20113 if (cl_inet_ipident != NULL) { 20114 ASSERT(cl_inet_isclusterwide != NULL); 20115 if ((*cl_inet_isclusterwide)(stack_id, 20116 IPPROTO_IP, AF_INET, 20117 (uint8_t *)(uintptr_t)src, NULL)) { 20118 ipha->ipha_ident = 20119 (*cl_inet_ipident)(stack_id, 20120 IPPROTO_IP, AF_INET, 20121 (uint8_t *)(uintptr_t)src, 20122 (uint8_t *)(uintptr_t)dst, 20123 NULL); 20124 clusterwide = B_TRUE; 20125 } 20126 } 20127 20128 if (!clusterwide) { 20129 ipha->ipha_ident = (uint16_t) 20130 atomic_add_32_nv( 20131 &ire->ire_ident, 1); 20132 } 20133 #ifndef _BIG_ENDIAN 20134 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20135 (ipha->ipha_ident >> 8); 20136 #endif 20137 } else { 20138 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20139 20140 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20141 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20142 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20143 ASSERT(PDESC_HDRL(pkt_info) >= 20144 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20145 TCP_CHECKSUM_SIZE)); 20146 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20147 20148 if (tcp->tcp_ip_forward_progress) { 20149 rconfirm = B_TRUE; 20150 tcp->tcp_ip_forward_progress = B_FALSE; 20151 } 20152 } 20153 20154 /* at least one payload span, and at most two */ 20155 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20156 20157 /* add the packet descriptor to Multidata */ 20158 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20159 KM_NOSLEEP)) == NULL) { 20160 /* 20161 * Any failure other than ENOMEM indicates 20162 * that we have passed in invalid pkt_info 20163 * or parameters to mmd_addpdesc, which must 20164 * not happen. 20165 * 20166 * EINVAL is a result of failure on boundary 20167 * checks against the pkt_info contents. It 20168 * should not happen, and we panic because 20169 * either there's horrible heap corruption, 20170 * and/or programming mistake. 20171 */ 20172 if (err != ENOMEM) { 20173 cmn_err(CE_PANIC, "tcp_multisend: " 20174 "pdesc logic error detected for " 20175 "tcp %p mmd %p pinfo %p (%d)\n", 20176 (void *)tcp, (void *)mmd, 20177 (void *)pkt_info, err); 20178 } 20179 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20180 goto legacy_send; /* out_of_mem */ 20181 } 20182 ASSERT(pkt != NULL); 20183 20184 /* calculate IP header and TCP checksums */ 20185 if (af == AF_INET) { 20186 /* calculate pseudo-header checksum */ 20187 cksum = (dst >> 16) + (dst & 0xFFFF) + 20188 (src >> 16) + (src & 0xFFFF); 20189 20190 /* offset for TCP header checksum */ 20191 up = IPH_TCPH_CHECKSUMP(ipha, 20192 IP_SIMPLE_HDR_LENGTH); 20193 } else { 20194 up = (uint16_t *)&ip6h->ip6_src; 20195 20196 /* calculate pseudo-header checksum */ 20197 cksum = up[0] + up[1] + up[2] + up[3] + 20198 up[4] + up[5] + up[6] + up[7] + 20199 up[8] + up[9] + up[10] + up[11] + 20200 up[12] + up[13] + up[14] + up[15]; 20201 20202 /* Fold the initial sum */ 20203 cksum = (cksum & 0xffff) + (cksum >> 16); 20204 20205 up = (uint16_t *)(((uchar_t *)ip6h) + 20206 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20207 } 20208 20209 if (hwcksum_flags & HCK_FULLCKSUM) { 20210 /* clear checksum field for hardware */ 20211 *up = 0; 20212 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20213 uint32_t sum; 20214 20215 /* pseudo-header checksumming */ 20216 sum = *up + cksum + IP_TCP_CSUM_COMP; 20217 sum = (sum & 0xFFFF) + (sum >> 16); 20218 *up = (sum & 0xFFFF) + (sum >> 16); 20219 } else { 20220 /* software checksumming */ 20221 TCP_STAT(tcps, tcp_out_sw_cksum); 20222 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 20223 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20224 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20225 cksum + IP_TCP_CSUM_COMP); 20226 if (*up == 0) 20227 *up = 0xFFFF; 20228 } 20229 20230 /* IPv4 header checksum */ 20231 if (af == AF_INET) { 20232 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20233 ipha->ipha_hdr_checksum = 0; 20234 } else { 20235 IP_HDR_CKSUM(ipha, cksum, 20236 ((uint32_t *)ipha)[0], 20237 ((uint16_t *)ipha)[4]); 20238 } 20239 } 20240 20241 if (af == AF_INET && 20242 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 20243 af == AF_INET6 && 20244 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 20245 mblk_t *mp, *mp1; 20246 uchar_t *hdr_rptr, *hdr_wptr; 20247 uchar_t *pld_rptr, *pld_wptr; 20248 20249 /* 20250 * We reconstruct a pseudo packet for the hooks 20251 * framework using mmd_transform_link(). 20252 * If it is a split packet we pullup the 20253 * payload. FW_HOOKS expects a pkt comprising 20254 * of two mblks: a header and the payload. 20255 */ 20256 if ((mp = mmd_transform_link(pkt)) == NULL) { 20257 TCP_STAT(tcps, tcp_mdt_allocfail); 20258 goto legacy_send; 20259 } 20260 20261 if (pkt_info->pld_cnt > 1) { 20262 /* split payload, more than one pld */ 20263 if ((mp1 = msgpullup(mp->b_cont, -1)) == 20264 NULL) { 20265 freemsg(mp); 20266 TCP_STAT(tcps, 20267 tcp_mdt_allocfail); 20268 goto legacy_send; 20269 } 20270 freemsg(mp->b_cont); 20271 mp->b_cont = mp1; 20272 } else { 20273 mp1 = mp->b_cont; 20274 } 20275 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 20276 20277 /* 20278 * Remember the message offsets. This is so we 20279 * can detect changes when we return from the 20280 * FW_HOOKS callbacks. 20281 */ 20282 hdr_rptr = mp->b_rptr; 20283 hdr_wptr = mp->b_wptr; 20284 pld_rptr = mp->b_cont->b_rptr; 20285 pld_wptr = mp->b_cont->b_wptr; 20286 20287 if (af == AF_INET) { 20288 DTRACE_PROBE4( 20289 ip4__physical__out__start, 20290 ill_t *, NULL, 20291 ill_t *, ill, 20292 ipha_t *, ipha, 20293 mblk_t *, mp); 20294 FW_HOOKS( 20295 ipst->ips_ip4_physical_out_event, 20296 ipst->ips_ipv4firewall_physical_out, 20297 NULL, ill, ipha, mp, mp, 0, ipst); 20298 DTRACE_PROBE1( 20299 ip4__physical__out__end, 20300 mblk_t *, mp); 20301 } else { 20302 DTRACE_PROBE4( 20303 ip6__physical__out_start, 20304 ill_t *, NULL, 20305 ill_t *, ill, 20306 ip6_t *, ip6h, 20307 mblk_t *, mp); 20308 FW_HOOKS6( 20309 ipst->ips_ip6_physical_out_event, 20310 ipst->ips_ipv6firewall_physical_out, 20311 NULL, ill, ip6h, mp, mp, 0, ipst); 20312 DTRACE_PROBE1( 20313 ip6__physical__out__end, 20314 mblk_t *, mp); 20315 } 20316 20317 if (mp == NULL || 20318 (mp1 = mp->b_cont) == NULL || 20319 mp->b_rptr != hdr_rptr || 20320 mp->b_wptr != hdr_wptr || 20321 mp1->b_rptr != pld_rptr || 20322 mp1->b_wptr != pld_wptr || 20323 mp1->b_cont != NULL) { 20324 /* 20325 * We abandon multidata processing and 20326 * return to the normal path, either 20327 * when a packet is blocked, or when 20328 * the boundaries of header buffer or 20329 * payload buffer have been changed by 20330 * FW_HOOKS[6]. 20331 */ 20332 if (mp != NULL) 20333 freemsg(mp); 20334 goto legacy_send; 20335 } 20336 /* Finished with the pseudo packet */ 20337 freemsg(mp); 20338 } 20339 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 20340 ill, ipha, ip6h); 20341 /* advance header offset */ 20342 cur_hdr_off += hdr_frag_sz; 20343 20344 obbytes += tcp->tcp_last_sent_len; 20345 ++obsegs; 20346 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20347 *tail_unsent > 0); 20348 20349 if ((*xmit_tail)->b_next == NULL) { 20350 /* 20351 * Store the lbolt used for RTT estimation. We can only 20352 * record one timestamp per mblk so we do it when we 20353 * reach the end of the payload buffer. Also we only 20354 * take a new timestamp sample when the previous timed 20355 * data from the same mblk has been ack'ed. 20356 */ 20357 (*xmit_tail)->b_prev = local_time; 20358 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20359 } 20360 20361 ASSERT(*tail_unsent >= 0); 20362 if (*tail_unsent > 0) { 20363 /* 20364 * We got here because we broke out of the above 20365 * loop due to of one of the following cases: 20366 * 20367 * 1. len < adjusted MSS (i.e. small), 20368 * 2. Sender SWS avoidance, 20369 * 3. max_pld is zero. 20370 * 20371 * We are done for this Multidata, so trim our 20372 * last payload buffer (if any) accordingly. 20373 */ 20374 if (md_pbuf != NULL) 20375 md_pbuf->b_wptr -= *tail_unsent; 20376 } else if (*usable > 0) { 20377 *xmit_tail = (*xmit_tail)->b_cont; 20378 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20379 (uintptr_t)INT_MAX); 20380 *tail_unsent = (int)MBLKL(*xmit_tail); 20381 add_buffer = B_TRUE; 20382 } 20383 } while (!done && *usable > 0 && num_burst_seg > 0 && 20384 (tcp_mdt_chain || max_pld > 0)); 20385 20386 if (md_mp_head != NULL) { 20387 /* send everything down */ 20388 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20389 &rconfirm); 20390 } 20391 20392 #undef PREP_NEW_MULTIDATA 20393 #undef PREP_NEW_PBUF 20394 #undef IPVER 20395 20396 IRE_REFRELE(ire); 20397 return (0); 20398 } 20399 20400 /* 20401 * A wrapper function for sending one or more Multidata messages down to 20402 * the module below ip; this routine does not release the reference of the 20403 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20404 */ 20405 static void 20406 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20407 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20408 { 20409 uint64_t delta; 20410 nce_t *nce; 20411 tcp_stack_t *tcps = tcp->tcp_tcps; 20412 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20413 20414 ASSERT(ire != NULL && ill != NULL); 20415 ASSERT(ire->ire_stq != NULL); 20416 ASSERT(md_mp_head != NULL); 20417 ASSERT(rconfirm != NULL); 20418 20419 /* adjust MIBs and IRE timestamp */ 20420 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 20421 tcp->tcp_obsegs += obsegs; 20422 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 20423 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 20424 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 20425 20426 if (tcp->tcp_ipversion == IPV4_VERSION) { 20427 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 20428 } else { 20429 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 20430 } 20431 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 20432 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 20433 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 20434 20435 ire->ire_ob_pkt_count += obsegs; 20436 if (ire->ire_ipif != NULL) 20437 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20438 ire->ire_last_used_time = lbolt; 20439 20440 if (ipst->ips_ipobs_enabled) { 20441 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 20442 pdesc_t *dl_pkt; 20443 pdescinfo_t pinfo; 20444 mblk_t *nmp; 20445 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 20446 20447 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 20448 (dl_pkt != NULL); 20449 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 20450 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 20451 continue; 20452 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 20453 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 20454 freemsg(nmp); 20455 } 20456 } 20457 20458 /* send it down */ 20459 putnext(ire->ire_stq, md_mp_head); 20460 20461 /* we're done for TCP/IPv4 */ 20462 if (tcp->tcp_ipversion == IPV4_VERSION) 20463 return; 20464 20465 nce = ire->ire_nce; 20466 20467 ASSERT(nce != NULL); 20468 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20469 ASSERT(nce->nce_state != ND_INCOMPLETE); 20470 20471 /* reachability confirmation? */ 20472 if (*rconfirm) { 20473 nce->nce_last = TICK_TO_MSEC(lbolt64); 20474 if (nce->nce_state != ND_REACHABLE) { 20475 mutex_enter(&nce->nce_lock); 20476 nce->nce_state = ND_REACHABLE; 20477 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20478 mutex_exit(&nce->nce_lock); 20479 (void) untimeout(nce->nce_timeout_id); 20480 if (ip_debug > 2) { 20481 /* ip1dbg */ 20482 pr_addr_dbg("tcp_multisend_data: state " 20483 "for %s changed to REACHABLE\n", 20484 AF_INET6, &ire->ire_addr_v6); 20485 } 20486 } 20487 /* reset transport reachability confirmation */ 20488 *rconfirm = B_FALSE; 20489 } 20490 20491 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20492 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20493 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20494 20495 if (delta > (uint64_t)ill->ill_reachable_time) { 20496 mutex_enter(&nce->nce_lock); 20497 switch (nce->nce_state) { 20498 case ND_REACHABLE: 20499 case ND_STALE: 20500 /* 20501 * ND_REACHABLE is identical to ND_STALE in this 20502 * specific case. If reachable time has expired for 20503 * this neighbor (delta is greater than reachable 20504 * time), conceptually, the neighbor cache is no 20505 * longer in REACHABLE state, but already in STALE 20506 * state. So the correct transition here is to 20507 * ND_DELAY. 20508 */ 20509 nce->nce_state = ND_DELAY; 20510 mutex_exit(&nce->nce_lock); 20511 NDP_RESTART_TIMER(nce, 20512 ipst->ips_delay_first_probe_time); 20513 if (ip_debug > 3) { 20514 /* ip2dbg */ 20515 pr_addr_dbg("tcp_multisend_data: state " 20516 "for %s changed to DELAY\n", 20517 AF_INET6, &ire->ire_addr_v6); 20518 } 20519 break; 20520 case ND_DELAY: 20521 case ND_PROBE: 20522 mutex_exit(&nce->nce_lock); 20523 /* Timers have already started */ 20524 break; 20525 case ND_UNREACHABLE: 20526 /* 20527 * ndp timer has detected that this nce is 20528 * unreachable and initiated deleting this nce 20529 * and all its associated IREs. This is a race 20530 * where we found the ire before it was deleted 20531 * and have just sent out a packet using this 20532 * unreachable nce. 20533 */ 20534 mutex_exit(&nce->nce_lock); 20535 break; 20536 default: 20537 ASSERT(0); 20538 } 20539 } 20540 } 20541 20542 /* 20543 * Derived from tcp_send_data(). 20544 */ 20545 static void 20546 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 20547 int num_lso_seg) 20548 { 20549 ipha_t *ipha; 20550 mblk_t *ire_fp_mp; 20551 uint_t ire_fp_mp_len; 20552 uint32_t hcksum_txflags = 0; 20553 ipaddr_t src; 20554 ipaddr_t dst; 20555 uint32_t cksum; 20556 uint16_t *up; 20557 tcp_stack_t *tcps = tcp->tcp_tcps; 20558 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20559 20560 ASSERT(DB_TYPE(mp) == M_DATA); 20561 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20562 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 20563 ASSERT(tcp->tcp_connp != NULL); 20564 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 20565 20566 ipha = (ipha_t *)mp->b_rptr; 20567 src = ipha->ipha_src; 20568 dst = ipha->ipha_dst; 20569 20570 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 20571 20572 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 20573 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 20574 num_lso_seg); 20575 #ifndef _BIG_ENDIAN 20576 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 20577 #endif 20578 if (tcp->tcp_snd_zcopy_aware) { 20579 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 20580 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 20581 mp = tcp_zcopy_disable(tcp, mp); 20582 } 20583 20584 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 20585 ASSERT(ill->ill_hcksum_capab != NULL); 20586 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 20587 } 20588 20589 /* 20590 * Since the TCP checksum should be recalculated by h/w, we can just 20591 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 20592 * pseudo-header checksum for HCK_PARTIALCKSUM. 20593 * The partial pseudo-header excludes TCP length, that was calculated 20594 * in tcp_send(), so to zero *up before further processing. 20595 */ 20596 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20597 20598 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 20599 *up = 0; 20600 20601 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 20602 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 20603 20604 /* 20605 * Append LSO flags and mss to the mp. 20606 */ 20607 lso_info_set(mp, mss, HW_LSO); 20608 20609 ipha->ipha_fragment_offset_and_flags |= 20610 (uint32_t)htons(ire->ire_frag_flag); 20611 20612 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20613 ire_fp_mp_len = MBLKL(ire_fp_mp); 20614 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 20615 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 20616 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 20617 20618 UPDATE_OB_PKT_COUNT(ire); 20619 ire->ire_last_used_time = lbolt; 20620 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 20621 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 20622 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 20623 ntohs(ipha->ipha_length)); 20624 20625 DTRACE_PROBE4(ip4__physical__out__start, 20626 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 20627 FW_HOOKS(ipst->ips_ip4_physical_out_event, 20628 ipst->ips_ipv4firewall_physical_out, NULL, 20629 ill, ipha, mp, mp, 0, ipst); 20630 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 20631 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 20632 20633 if (mp != NULL) { 20634 if (ipst->ips_ipobs_enabled) { 20635 zoneid_t szone; 20636 20637 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 20638 ipst, ALL_ZONES); 20639 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 20640 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 20641 } 20642 20643 ILL_SEND_TX(ill, ire, tcp->tcp_connp, mp, 0, NULL); 20644 } 20645 } 20646 20647 /* 20648 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20649 * scheme, and returns one of the following: 20650 * 20651 * -1 = failed allocation. 20652 * 0 = success; burst count reached, or usable send window is too small, 20653 * and that we'd rather wait until later before sending again. 20654 * 1 = success; we are called from tcp_multisend(), and both usable send 20655 * window and tail_unsent are greater than the MDT threshold, and thus 20656 * Multidata Transmit should be used instead. 20657 */ 20658 static int 20659 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20660 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20661 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20662 const int mdt_thres) 20663 { 20664 int num_burst_seg = tcp->tcp_snd_burst; 20665 ire_t *ire = NULL; 20666 ill_t *ill = NULL; 20667 mblk_t *ire_fp_mp = NULL; 20668 uint_t ire_fp_mp_len = 0; 20669 int num_lso_seg = 1; 20670 uint_t lso_usable; 20671 boolean_t do_lso_send = B_FALSE; 20672 tcp_stack_t *tcps = tcp->tcp_tcps; 20673 20674 /* 20675 * Check LSO capability before any further work. And the similar check 20676 * need to be done in for(;;) loop. 20677 * LSO will be deployed when therer is more than one mss of available 20678 * data and a burst transmission is allowed. 20679 */ 20680 if (tcp->tcp_lso && 20681 (tcp->tcp_valid_bits == 0 || 20682 tcp->tcp_valid_bits == TCP_FSS_VALID) && 20683 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20684 /* 20685 * Try to find usable IRE/ILL and do basic check to the ILL. 20686 * Double check LSO usability before going further, since the 20687 * underlying interface could have been changed. In case of any 20688 * change of LSO capability, set tcp_ire_ill_check_done to 20689 * B_FALSE to force to check the ILL with the next send. 20690 */ 20691 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill) && 20692 tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 20693 /* 20694 * Enable LSO with this transmission. 20695 * Since IRE has been hold in tcp_send_find_ire_ill(), 20696 * IRE_REFRELE(ire) should be called before return. 20697 */ 20698 do_lso_send = B_TRUE; 20699 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20700 ire_fp_mp_len = MBLKL(ire_fp_mp); 20701 /* Round up to multiple of 4 */ 20702 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 20703 } else { 20704 tcp->tcp_lso = B_FALSE; 20705 tcp->tcp_ire_ill_check_done = B_FALSE; 20706 do_lso_send = B_FALSE; 20707 ill = NULL; 20708 } 20709 } 20710 20711 for (;;) { 20712 struct datab *db; 20713 tcph_t *tcph; 20714 uint32_t sum; 20715 mblk_t *mp, *mp1; 20716 uchar_t *rptr; 20717 int len; 20718 20719 /* 20720 * If we're called by tcp_multisend(), and the amount of 20721 * sendable data as well as the size of current xmit_tail 20722 * is beyond the MDT threshold, return to the caller and 20723 * let the large data transmit be done using MDT. 20724 */ 20725 if (*usable > 0 && *usable > mdt_thres && 20726 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 20727 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 20728 ASSERT(tcp->tcp_mdt); 20729 return (1); /* success; do large send */ 20730 } 20731 20732 if (num_burst_seg == 0) 20733 break; /* success; burst count reached */ 20734 20735 /* 20736 * Calculate the maximum payload length we can send in *one* 20737 * time. 20738 */ 20739 if (do_lso_send) { 20740 /* 20741 * Check whether need to do LSO any more. 20742 */ 20743 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20744 lso_usable = MIN(tcp->tcp_lso_max, *usable); 20745 lso_usable = MIN(lso_usable, 20746 num_burst_seg * mss); 20747 20748 num_lso_seg = lso_usable / mss; 20749 if (lso_usable % mss) { 20750 num_lso_seg++; 20751 tcp->tcp_last_sent_len = (ushort_t) 20752 (lso_usable % mss); 20753 } else { 20754 tcp->tcp_last_sent_len = (ushort_t)mss; 20755 } 20756 } else { 20757 do_lso_send = B_FALSE; 20758 num_lso_seg = 1; 20759 lso_usable = mss; 20760 } 20761 } 20762 20763 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 20764 20765 /* 20766 * Adjust num_burst_seg here. 20767 */ 20768 num_burst_seg -= num_lso_seg; 20769 20770 len = mss; 20771 if (len > *usable) { 20772 ASSERT(do_lso_send == B_FALSE); 20773 20774 len = *usable; 20775 if (len <= 0) { 20776 /* Terminate the loop */ 20777 break; /* success; too small */ 20778 } 20779 /* 20780 * Sender silly-window avoidance. 20781 * Ignore this if we are going to send a 20782 * zero window probe out. 20783 * 20784 * TODO: force data into microscopic window? 20785 * ==> (!pushed || (unsent > usable)) 20786 */ 20787 if (len < (tcp->tcp_max_swnd >> 1) && 20788 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 20789 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 20790 len == 1) && (! tcp->tcp_zero_win_probe)) { 20791 /* 20792 * If the retransmit timer is not running 20793 * we start it so that we will retransmit 20794 * in the case when the the receiver has 20795 * decremented the window. 20796 */ 20797 if (*snxt == tcp->tcp_snxt && 20798 *snxt == tcp->tcp_suna) { 20799 /* 20800 * We are not supposed to send 20801 * anything. So let's wait a little 20802 * bit longer before breaking SWS 20803 * avoidance. 20804 * 20805 * What should the value be? 20806 * Suggestion: MAX(init rexmit time, 20807 * tcp->tcp_rto) 20808 */ 20809 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20810 } 20811 break; /* success; too small */ 20812 } 20813 } 20814 20815 tcph = tcp->tcp_tcph; 20816 20817 /* 20818 * The reason to adjust len here is that we need to set flags 20819 * and calculate checksum. 20820 */ 20821 if (do_lso_send) 20822 len = lso_usable; 20823 20824 *usable -= len; /* Approximate - can be adjusted later */ 20825 if (*usable > 0) 20826 tcph->th_flags[0] = TH_ACK; 20827 else 20828 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 20829 20830 /* 20831 * Prime pump for IP's checksumming on our behalf 20832 * Include the adjustment for a source route if any. 20833 */ 20834 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20835 sum = (sum >> 16) + (sum & 0xFFFF); 20836 U16_TO_ABE16(sum, tcph->th_sum); 20837 20838 U32_TO_ABE32(*snxt, tcph->th_seq); 20839 20840 /* 20841 * Branch off to tcp_xmit_mp() if any of the VALID bits is 20842 * set. For the case when TCP_FSS_VALID is the only valid 20843 * bit (normal active close), branch off only when we think 20844 * that the FIN flag needs to be set. Note for this case, 20845 * that (snxt + len) may not reflect the actual seg_len, 20846 * as len may be further reduced in tcp_xmit_mp(). If len 20847 * gets modified, we will end up here again. 20848 */ 20849 if (tcp->tcp_valid_bits != 0 && 20850 (tcp->tcp_valid_bits != TCP_FSS_VALID || 20851 ((*snxt + len) == tcp->tcp_fss))) { 20852 uchar_t *prev_rptr; 20853 uint32_t prev_snxt = tcp->tcp_snxt; 20854 20855 if (*tail_unsent == 0) { 20856 ASSERT((*xmit_tail)->b_cont != NULL); 20857 *xmit_tail = (*xmit_tail)->b_cont; 20858 prev_rptr = (*xmit_tail)->b_rptr; 20859 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20860 (*xmit_tail)->b_rptr); 20861 } else { 20862 prev_rptr = (*xmit_tail)->b_rptr; 20863 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 20864 *tail_unsent; 20865 } 20866 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 20867 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 20868 /* Restore tcp_snxt so we get amount sent right. */ 20869 tcp->tcp_snxt = prev_snxt; 20870 if (prev_rptr == (*xmit_tail)->b_rptr) { 20871 /* 20872 * If the previous timestamp is still in use, 20873 * don't stomp on it. 20874 */ 20875 if ((*xmit_tail)->b_next == NULL) { 20876 (*xmit_tail)->b_prev = local_time; 20877 (*xmit_tail)->b_next = 20878 (mblk_t *)(uintptr_t)(*snxt); 20879 } 20880 } else 20881 (*xmit_tail)->b_rptr = prev_rptr; 20882 20883 if (mp == NULL) { 20884 if (ire != NULL) 20885 IRE_REFRELE(ire); 20886 return (-1); 20887 } 20888 mp1 = mp->b_cont; 20889 20890 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 20891 tcp->tcp_last_sent_len = (ushort_t)len; 20892 while (mp1->b_cont) { 20893 *xmit_tail = (*xmit_tail)->b_cont; 20894 (*xmit_tail)->b_prev = local_time; 20895 (*xmit_tail)->b_next = 20896 (mblk_t *)(uintptr_t)(*snxt); 20897 mp1 = mp1->b_cont; 20898 } 20899 *snxt += len; 20900 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 20901 BUMP_LOCAL(tcp->tcp_obsegs); 20902 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 20903 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 20904 tcp_send_data(tcp, q, mp); 20905 continue; 20906 } 20907 20908 *snxt += len; /* Adjust later if we don't send all of len */ 20909 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 20910 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 20911 20912 if (*tail_unsent) { 20913 /* Are the bytes above us in flight? */ 20914 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 20915 if (rptr != (*xmit_tail)->b_rptr) { 20916 *tail_unsent -= len; 20917 if (len <= mss) /* LSO is unusable */ 20918 tcp->tcp_last_sent_len = (ushort_t)len; 20919 len += tcp_hdr_len; 20920 if (tcp->tcp_ipversion == IPV4_VERSION) 20921 tcp->tcp_ipha->ipha_length = htons(len); 20922 else 20923 tcp->tcp_ip6h->ip6_plen = 20924 htons(len - 20925 ((char *)&tcp->tcp_ip6h[1] - 20926 tcp->tcp_iphc)); 20927 mp = dupb(*xmit_tail); 20928 if (mp == NULL) { 20929 if (ire != NULL) 20930 IRE_REFRELE(ire); 20931 return (-1); /* out_of_mem */ 20932 } 20933 mp->b_rptr = rptr; 20934 /* 20935 * If the old timestamp is no longer in use, 20936 * sample a new timestamp now. 20937 */ 20938 if ((*xmit_tail)->b_next == NULL) { 20939 (*xmit_tail)->b_prev = local_time; 20940 (*xmit_tail)->b_next = 20941 (mblk_t *)(uintptr_t)(*snxt-len); 20942 } 20943 goto must_alloc; 20944 } 20945 } else { 20946 *xmit_tail = (*xmit_tail)->b_cont; 20947 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 20948 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 20949 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20950 (*xmit_tail)->b_rptr); 20951 } 20952 20953 (*xmit_tail)->b_prev = local_time; 20954 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 20955 20956 *tail_unsent -= len; 20957 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 20958 tcp->tcp_last_sent_len = (ushort_t)len; 20959 20960 len += tcp_hdr_len; 20961 if (tcp->tcp_ipversion == IPV4_VERSION) 20962 tcp->tcp_ipha->ipha_length = htons(len); 20963 else 20964 tcp->tcp_ip6h->ip6_plen = htons(len - 20965 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 20966 20967 mp = dupb(*xmit_tail); 20968 if (mp == NULL) { 20969 if (ire != NULL) 20970 IRE_REFRELE(ire); 20971 return (-1); /* out_of_mem */ 20972 } 20973 20974 len = tcp_hdr_len; 20975 /* 20976 * There are four reasons to allocate a new hdr mblk: 20977 * 1) The bytes above us are in use by another packet 20978 * 2) We don't have good alignment 20979 * 3) The mblk is being shared 20980 * 4) We don't have enough room for a header 20981 */ 20982 rptr = mp->b_rptr - len; 20983 if (!OK_32PTR(rptr) || 20984 ((db = mp->b_datap), db->db_ref != 2) || 20985 rptr < db->db_base + ire_fp_mp_len) { 20986 /* NOTE: we assume allocb returns an OK_32PTR */ 20987 20988 must_alloc:; 20989 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 20990 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 20991 if (mp1 == NULL) { 20992 freemsg(mp); 20993 if (ire != NULL) 20994 IRE_REFRELE(ire); 20995 return (-1); /* out_of_mem */ 20996 } 20997 mp1->b_cont = mp; 20998 mp = mp1; 20999 /* Leave room for Link Level header */ 21000 len = tcp_hdr_len; 21001 rptr = 21002 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21003 mp->b_wptr = &rptr[len]; 21004 } 21005 21006 /* 21007 * Fill in the header using the template header, and add 21008 * options such as time-stamp, ECN and/or SACK, as needed. 21009 */ 21010 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21011 21012 mp->b_rptr = rptr; 21013 21014 if (*tail_unsent) { 21015 int spill = *tail_unsent; 21016 21017 mp1 = mp->b_cont; 21018 if (mp1 == NULL) 21019 mp1 = mp; 21020 21021 /* 21022 * If we're a little short, tack on more mblks until 21023 * there is no more spillover. 21024 */ 21025 while (spill < 0) { 21026 mblk_t *nmp; 21027 int nmpsz; 21028 21029 nmp = (*xmit_tail)->b_cont; 21030 nmpsz = MBLKL(nmp); 21031 21032 /* 21033 * Excess data in mblk; can we split it? 21034 * If MDT is enabled for the connection, 21035 * keep on splitting as this is a transient 21036 * send path. 21037 */ 21038 if (!do_lso_send && !tcp->tcp_mdt && 21039 (spill + nmpsz > 0)) { 21040 /* 21041 * Don't split if stream head was 21042 * told to break up larger writes 21043 * into smaller ones. 21044 */ 21045 if (tcp->tcp_maxpsz > 0) 21046 break; 21047 21048 /* 21049 * Next mblk is less than SMSS/2 21050 * rounded up to nearest 64-byte; 21051 * let it get sent as part of the 21052 * next segment. 21053 */ 21054 if (tcp->tcp_localnet && 21055 !tcp->tcp_cork && 21056 (nmpsz < roundup((mss >> 1), 64))) 21057 break; 21058 } 21059 21060 *xmit_tail = nmp; 21061 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21062 /* Stash for rtt use later */ 21063 (*xmit_tail)->b_prev = local_time; 21064 (*xmit_tail)->b_next = 21065 (mblk_t *)(uintptr_t)(*snxt - len); 21066 mp1->b_cont = dupb(*xmit_tail); 21067 mp1 = mp1->b_cont; 21068 21069 spill += nmpsz; 21070 if (mp1 == NULL) { 21071 *tail_unsent = spill; 21072 freemsg(mp); 21073 if (ire != NULL) 21074 IRE_REFRELE(ire); 21075 return (-1); /* out_of_mem */ 21076 } 21077 } 21078 21079 /* Trim back any surplus on the last mblk */ 21080 if (spill >= 0) { 21081 mp1->b_wptr -= spill; 21082 *tail_unsent = spill; 21083 } else { 21084 /* 21085 * We did not send everything we could in 21086 * order to remain within the b_cont limit. 21087 */ 21088 *usable -= spill; 21089 *snxt += spill; 21090 tcp->tcp_last_sent_len += spill; 21091 UPDATE_MIB(&tcps->tcps_mib, 21092 tcpOutDataBytes, spill); 21093 /* 21094 * Adjust the checksum 21095 */ 21096 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21097 sum += spill; 21098 sum = (sum >> 16) + (sum & 0xFFFF); 21099 U16_TO_ABE16(sum, tcph->th_sum); 21100 if (tcp->tcp_ipversion == IPV4_VERSION) { 21101 sum = ntohs( 21102 ((ipha_t *)rptr)->ipha_length) + 21103 spill; 21104 ((ipha_t *)rptr)->ipha_length = 21105 htons(sum); 21106 } else { 21107 sum = ntohs( 21108 ((ip6_t *)rptr)->ip6_plen) + 21109 spill; 21110 ((ip6_t *)rptr)->ip6_plen = 21111 htons(sum); 21112 } 21113 *tail_unsent = 0; 21114 } 21115 } 21116 if (tcp->tcp_ip_forward_progress) { 21117 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21118 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21119 tcp->tcp_ip_forward_progress = B_FALSE; 21120 } 21121 21122 if (do_lso_send) { 21123 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21124 num_lso_seg); 21125 tcp->tcp_obsegs += num_lso_seg; 21126 21127 TCP_STAT(tcps, tcp_lso_times); 21128 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21129 } else { 21130 tcp_send_data(tcp, q, mp); 21131 BUMP_LOCAL(tcp->tcp_obsegs); 21132 } 21133 } 21134 21135 if (ire != NULL) 21136 IRE_REFRELE(ire); 21137 return (0); 21138 } 21139 21140 /* Unlink and return any mblk that looks like it contains a MDT info */ 21141 static mblk_t * 21142 tcp_mdt_info_mp(mblk_t *mp) 21143 { 21144 mblk_t *prev_mp; 21145 21146 for (;;) { 21147 prev_mp = mp; 21148 /* no more to process? */ 21149 if ((mp = mp->b_cont) == NULL) 21150 break; 21151 21152 switch (DB_TYPE(mp)) { 21153 case M_CTL: 21154 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21155 continue; 21156 ASSERT(prev_mp != NULL); 21157 prev_mp->b_cont = mp->b_cont; 21158 mp->b_cont = NULL; 21159 return (mp); 21160 default: 21161 break; 21162 } 21163 } 21164 return (mp); 21165 } 21166 21167 /* MDT info update routine, called when IP notifies us about MDT */ 21168 static void 21169 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21170 { 21171 boolean_t prev_state; 21172 tcp_stack_t *tcps = tcp->tcp_tcps; 21173 21174 /* 21175 * IP is telling us to abort MDT on this connection? We know 21176 * this because the capability is only turned off when IP 21177 * encounters some pathological cases, e.g. link-layer change 21178 * where the new driver doesn't support MDT, or in situation 21179 * where MDT usage on the link-layer has been switched off. 21180 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21181 * if the link-layer doesn't support MDT, and if it does, it 21182 * will indicate that the feature is to be turned on. 21183 */ 21184 prev_state = tcp->tcp_mdt; 21185 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21186 if (!tcp->tcp_mdt && !first) { 21187 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21188 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21189 (void *)tcp->tcp_connp)); 21190 } 21191 21192 /* 21193 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21194 * so disable MDT otherwise. The checks are done here 21195 * and in tcp_wput_data(). 21196 */ 21197 if (tcp->tcp_mdt && 21198 (tcp->tcp_ipversion == IPV4_VERSION && 21199 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21200 (tcp->tcp_ipversion == IPV6_VERSION && 21201 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21202 tcp->tcp_mdt = B_FALSE; 21203 21204 if (tcp->tcp_mdt) { 21205 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21206 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21207 "version (%d), expected version is %d", 21208 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21209 tcp->tcp_mdt = B_FALSE; 21210 return; 21211 } 21212 21213 /* 21214 * We need the driver to be able to handle at least three 21215 * spans per packet in order for tcp MDT to be utilized. 21216 * The first is for the header portion, while the rest are 21217 * needed to handle a packet that straddles across two 21218 * virtually non-contiguous buffers; a typical tcp packet 21219 * therefore consists of only two spans. Note that we take 21220 * a zero as "don't care". 21221 */ 21222 if (mdt_capab->ill_mdt_span_limit > 0 && 21223 mdt_capab->ill_mdt_span_limit < 3) { 21224 tcp->tcp_mdt = B_FALSE; 21225 return; 21226 } 21227 21228 /* a zero means driver wants default value */ 21229 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21230 tcps->tcps_mdt_max_pbufs); 21231 if (tcp->tcp_mdt_max_pld == 0) 21232 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 21233 21234 /* ensure 32-bit alignment */ 21235 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 21236 mdt_capab->ill_mdt_hdr_head), 4); 21237 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 21238 mdt_capab->ill_mdt_hdr_tail), 4); 21239 21240 if (!first && !prev_state) { 21241 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 21242 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21243 (void *)tcp->tcp_connp)); 21244 } 21245 } 21246 } 21247 21248 /* Unlink and return any mblk that looks like it contains a LSO info */ 21249 static mblk_t * 21250 tcp_lso_info_mp(mblk_t *mp) 21251 { 21252 mblk_t *prev_mp; 21253 21254 for (;;) { 21255 prev_mp = mp; 21256 /* no more to process? */ 21257 if ((mp = mp->b_cont) == NULL) 21258 break; 21259 21260 switch (DB_TYPE(mp)) { 21261 case M_CTL: 21262 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21263 continue; 21264 ASSERT(prev_mp != NULL); 21265 prev_mp->b_cont = mp->b_cont; 21266 mp->b_cont = NULL; 21267 return (mp); 21268 default: 21269 break; 21270 } 21271 } 21272 21273 return (mp); 21274 } 21275 21276 /* LSO info update routine, called when IP notifies us about LSO */ 21277 static void 21278 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21279 { 21280 tcp_stack_t *tcps = tcp->tcp_tcps; 21281 21282 /* 21283 * IP is telling us to abort LSO on this connection? We know 21284 * this because the capability is only turned off when IP 21285 * encounters some pathological cases, e.g. link-layer change 21286 * where the new NIC/driver doesn't support LSO, or in situation 21287 * where LSO usage on the link-layer has been switched off. 21288 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21289 * if the link-layer doesn't support LSO, and if it does, it 21290 * will indicate that the feature is to be turned on. 21291 */ 21292 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21293 TCP_STAT(tcps, tcp_lso_enabled); 21294 21295 /* 21296 * We currently only support LSO on simple TCP/IPv4, 21297 * so disable LSO otherwise. The checks are done here 21298 * and in tcp_wput_data(). 21299 */ 21300 if (tcp->tcp_lso && 21301 (tcp->tcp_ipversion == IPV4_VERSION && 21302 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21303 (tcp->tcp_ipversion == IPV6_VERSION)) { 21304 tcp->tcp_lso = B_FALSE; 21305 TCP_STAT(tcps, tcp_lso_disabled); 21306 } else { 21307 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21308 lso_capab->ill_lso_max); 21309 } 21310 } 21311 21312 static void 21313 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21314 { 21315 conn_t *connp = tcp->tcp_connp; 21316 tcp_stack_t *tcps = tcp->tcp_tcps; 21317 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21318 21319 ASSERT(ire != NULL); 21320 21321 /* 21322 * We may be in the fastpath here, and although we essentially do 21323 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21324 * we try to keep things as brief as possible. After all, these 21325 * are only best-effort checks, and we do more thorough ones prior 21326 * to calling tcp_send()/tcp_multisend(). 21327 */ 21328 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 21329 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21330 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21331 !(ire->ire_flags & RTF_MULTIRT) && 21332 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 21333 CONN_IS_LSO_MD_FASTPATH(connp)) { 21334 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 21335 /* Cache the result */ 21336 connp->conn_lso_ok = B_TRUE; 21337 21338 ASSERT(ill->ill_lso_capab != NULL); 21339 if (!ill->ill_lso_capab->ill_lso_on) { 21340 ill->ill_lso_capab->ill_lso_on = 1; 21341 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21342 "LSO for interface %s\n", (void *)connp, 21343 ill->ill_name)); 21344 } 21345 tcp_lso_update(tcp, ill->ill_lso_capab); 21346 } else if (ipst->ips_ip_multidata_outbound && 21347 ILL_MDT_CAPABLE(ill)) { 21348 /* Cache the result */ 21349 connp->conn_mdt_ok = B_TRUE; 21350 21351 ASSERT(ill->ill_mdt_capab != NULL); 21352 if (!ill->ill_mdt_capab->ill_mdt_on) { 21353 ill->ill_mdt_capab->ill_mdt_on = 1; 21354 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21355 "MDT for interface %s\n", (void *)connp, 21356 ill->ill_name)); 21357 } 21358 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21359 } 21360 } 21361 21362 /* 21363 * The goal is to reduce the number of generated tcp segments by 21364 * setting the maxpsz multiplier to 0; this will have an affect on 21365 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21366 * into each packet, up to SMSS bytes. Doing this reduces the number 21367 * of outbound segments and incoming ACKs, thus allowing for better 21368 * network and system performance. In contrast the legacy behavior 21369 * may result in sending less than SMSS size, because the last mblk 21370 * for some packets may have more data than needed to make up SMSS, 21371 * and the legacy code refused to "split" it. 21372 * 21373 * We apply the new behavior on following situations: 21374 * 21375 * 1) Loopback connections, 21376 * 2) Connections in which the remote peer is not on local subnet, 21377 * 3) Local subnet connections over the bge interface (see below). 21378 * 21379 * Ideally, we would like this behavior to apply for interfaces other 21380 * than bge. However, doing so would negatively impact drivers which 21381 * perform dynamic mapping and unmapping of DMA resources, which are 21382 * increased by setting the maxpsz multiplier to 0 (more mblks per 21383 * packet will be generated by tcp). The bge driver does not suffer 21384 * from this, as it copies the mblks into pre-mapped buffers, and 21385 * therefore does not require more I/O resources than before. 21386 * 21387 * Otherwise, this behavior is present on all network interfaces when 21388 * the destination endpoint is non-local, since reducing the number 21389 * of packets in general is good for the network. 21390 * 21391 * TODO We need to remove this hard-coded conditional for bge once 21392 * a better "self-tuning" mechanism, or a way to comprehend 21393 * the driver transmit strategy is devised. Until the solution 21394 * is found and well understood, we live with this hack. 21395 */ 21396 if (!tcp_static_maxpsz && 21397 (tcp->tcp_loopback || !tcp->tcp_localnet || 21398 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21399 /* override the default value */ 21400 tcp->tcp_maxpsz = 0; 21401 21402 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21403 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21404 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21405 } 21406 21407 /* set the stream head parameters accordingly */ 21408 (void) tcp_maxpsz_set(tcp, B_TRUE); 21409 } 21410 21411 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21412 static void 21413 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21414 { 21415 uchar_t fval = *mp->b_rptr; 21416 mblk_t *tail; 21417 queue_t *q = tcp->tcp_wq; 21418 21419 /* TODO: How should flush interact with urgent data? */ 21420 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21421 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21422 /* 21423 * Flush only data that has not yet been put on the wire. If 21424 * we flush data that we have already transmitted, life, as we 21425 * know it, may come to an end. 21426 */ 21427 tail = tcp->tcp_xmit_tail; 21428 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21429 tcp->tcp_xmit_tail_unsent = 0; 21430 tcp->tcp_unsent = 0; 21431 if (tail->b_wptr != tail->b_rptr) 21432 tail = tail->b_cont; 21433 if (tail) { 21434 mblk_t **excess = &tcp->tcp_xmit_head; 21435 for (;;) { 21436 mblk_t *mp1 = *excess; 21437 if (mp1 == tail) 21438 break; 21439 tcp->tcp_xmit_tail = mp1; 21440 tcp->tcp_xmit_last = mp1; 21441 excess = &mp1->b_cont; 21442 } 21443 *excess = NULL; 21444 tcp_close_mpp(&tail); 21445 if (tcp->tcp_snd_zcopy_aware) 21446 tcp_zcopy_notify(tcp); 21447 } 21448 /* 21449 * We have no unsent data, so unsent must be less than 21450 * tcp_xmit_lowater, so re-enable flow. 21451 */ 21452 mutex_enter(&tcp->tcp_non_sq_lock); 21453 if (tcp->tcp_flow_stopped) { 21454 tcp_clrqfull(tcp); 21455 } 21456 mutex_exit(&tcp->tcp_non_sq_lock); 21457 } 21458 /* 21459 * TODO: you can't just flush these, you have to increase rwnd for one 21460 * thing. For another, how should urgent data interact? 21461 */ 21462 if (fval & FLUSHR) { 21463 *mp->b_rptr = fval & ~FLUSHW; 21464 /* XXX */ 21465 qreply(q, mp); 21466 return; 21467 } 21468 freemsg(mp); 21469 } 21470 21471 /* 21472 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21473 * messages. 21474 */ 21475 static void 21476 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21477 { 21478 mblk_t *mp1; 21479 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 21480 STRUCT_HANDLE(strbuf, sb); 21481 queue_t *q = tcp->tcp_wq; 21482 int error; 21483 uint_t addrlen; 21484 21485 /* Make sure it is one of ours. */ 21486 switch (iocp->ioc_cmd) { 21487 case TI_GETMYNAME: 21488 case TI_GETPEERNAME: 21489 break; 21490 default: 21491 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21492 return; 21493 } 21494 switch (mi_copy_state(q, mp, &mp1)) { 21495 case -1: 21496 return; 21497 case MI_COPY_CASE(MI_COPY_IN, 1): 21498 break; 21499 case MI_COPY_CASE(MI_COPY_OUT, 1): 21500 /* Copy out the strbuf. */ 21501 mi_copyout(q, mp); 21502 return; 21503 case MI_COPY_CASE(MI_COPY_OUT, 2): 21504 /* All done. */ 21505 mi_copy_done(q, mp, 0); 21506 return; 21507 default: 21508 mi_copy_done(q, mp, EPROTO); 21509 return; 21510 } 21511 /* Check alignment of the strbuf */ 21512 if (!OK_32PTR(mp1->b_rptr)) { 21513 mi_copy_done(q, mp, EINVAL); 21514 return; 21515 } 21516 21517 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 21518 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21519 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21520 mi_copy_done(q, mp, EINVAL); 21521 return; 21522 } 21523 21524 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21525 if (mp1 == NULL) 21526 return; 21527 21528 switch (iocp->ioc_cmd) { 21529 case TI_GETMYNAME: 21530 error = tcp_do_getsockname(tcp, (void *)mp1->b_rptr, &addrlen); 21531 break; 21532 case TI_GETPEERNAME: 21533 error = tcp_do_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 21534 break; 21535 } 21536 21537 if (error != 0) { 21538 mi_copy_done(q, mp, error); 21539 } else { 21540 mp1->b_wptr += addrlen; 21541 STRUCT_FSET(sb, len, addrlen); 21542 21543 /* Copy out the address */ 21544 mi_copyout(q, mp); 21545 } 21546 } 21547 21548 static void 21549 tcp_use_pure_tpi(tcp_t *tcp) 21550 { 21551 #ifdef _ILP32 21552 tcp->tcp_acceptor_id = (t_uscalar_t)tcp->tcp_rq; 21553 #else 21554 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 21555 #endif 21556 /* 21557 * Insert this socket into the acceptor hash. 21558 * We might need it for T_CONN_RES message 21559 */ 21560 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 21561 21562 tcp->tcp_issocket = B_FALSE; 21563 TCP_STAT(tcp->tcp_tcps, tcp_sock_fallback); 21564 } 21565 21566 /* 21567 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21568 * messages. 21569 */ 21570 /* ARGSUSED */ 21571 static void 21572 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21573 { 21574 conn_t *connp = (conn_t *)arg; 21575 tcp_t *tcp = connp->conn_tcp; 21576 queue_t *q = tcp->tcp_wq; 21577 struct iocblk *iocp; 21578 21579 ASSERT(DB_TYPE(mp) == M_IOCTL); 21580 /* 21581 * Try and ASSERT the minimum possible references on the 21582 * conn early enough. Since we are executing on write side, 21583 * the connection is obviously not detached and that means 21584 * there is a ref each for TCP and IP. Since we are behind 21585 * the squeue, the minimum references needed are 3. If the 21586 * conn is in classifier hash list, there should be an 21587 * extra ref for that (we check both the possibilities). 21588 */ 21589 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21590 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21591 21592 iocp = (struct iocblk *)mp->b_rptr; 21593 switch (iocp->ioc_cmd) { 21594 case TCP_IOC_DEFAULT_Q: 21595 /* Wants to be the default wq. */ 21596 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 21597 iocp->ioc_error = EPERM; 21598 iocp->ioc_count = 0; 21599 mp->b_datap->db_type = M_IOCACK; 21600 qreply(q, mp); 21601 return; 21602 } 21603 tcp_def_q_set(tcp, mp); 21604 return; 21605 case _SIOCSOCKFALLBACK: 21606 /* 21607 * Either sockmod is about to be popped and the socket 21608 * would now be treated as a plain stream, or a module 21609 * is about to be pushed so we could no longer use read- 21610 * side synchronous streams for fused loopback tcp. 21611 * Drain any queued data and disable direct sockfs 21612 * interface from now on. 21613 */ 21614 if (!tcp->tcp_issocket) { 21615 DB_TYPE(mp) = M_IOCNAK; 21616 iocp->ioc_error = EINVAL; 21617 } else { 21618 tcp_use_pure_tpi(tcp); 21619 DB_TYPE(mp) = M_IOCACK; 21620 iocp->ioc_error = 0; 21621 } 21622 iocp->ioc_count = 0; 21623 iocp->ioc_rval = 0; 21624 qreply(q, mp); 21625 return; 21626 } 21627 CALL_IP_WPUT(connp, q, mp); 21628 } 21629 21630 /* 21631 * This routine is called by tcp_wput() to handle all TPI requests. 21632 */ 21633 /* ARGSUSED */ 21634 static void 21635 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 21636 { 21637 conn_t *connp = (conn_t *)arg; 21638 tcp_t *tcp = connp->conn_tcp; 21639 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 21640 uchar_t *rptr; 21641 t_scalar_t type; 21642 cred_t *cr; 21643 21644 /* 21645 * Try and ASSERT the minimum possible references on the 21646 * conn early enough. Since we are executing on write side, 21647 * the connection is obviously not detached and that means 21648 * there is a ref each for TCP and IP. Since we are behind 21649 * the squeue, the minimum references needed are 3. If the 21650 * conn is in classifier hash list, there should be an 21651 * extra ref for that (we check both the possibilities). 21652 */ 21653 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21654 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21655 21656 rptr = mp->b_rptr; 21657 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 21658 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 21659 type = ((union T_primitives *)rptr)->type; 21660 if (type == T_EXDATA_REQ) { 21661 tcp_output_urgent(connp, mp, arg2); 21662 } else if (type != T_DATA_REQ) { 21663 goto non_urgent_data; 21664 } else { 21665 /* TODO: options, flags, ... from user */ 21666 /* Set length to zero for reclamation below */ 21667 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 21668 freeb(mp); 21669 } 21670 return; 21671 } else { 21672 if (tcp->tcp_debug) { 21673 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21674 "tcp_wput_proto, dropping one..."); 21675 } 21676 freemsg(mp); 21677 return; 21678 } 21679 21680 non_urgent_data: 21681 21682 switch ((int)tprim->type) { 21683 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 21684 /* 21685 * save the kssl_ent_t from the next block, and convert this 21686 * back to a normal bind_req. 21687 */ 21688 if (mp->b_cont != NULL) { 21689 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 21690 21691 if (tcp->tcp_kssl_ent != NULL) { 21692 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 21693 KSSL_NO_PROXY); 21694 tcp->tcp_kssl_ent = NULL; 21695 } 21696 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 21697 sizeof (kssl_ent_t)); 21698 kssl_hold_ent(tcp->tcp_kssl_ent); 21699 freemsg(mp->b_cont); 21700 mp->b_cont = NULL; 21701 } 21702 tprim->type = T_BIND_REQ; 21703 21704 /* FALLTHROUGH */ 21705 case O_T_BIND_REQ: /* bind request */ 21706 case T_BIND_REQ: /* new semantics bind request */ 21707 tcp_tpi_bind(tcp, mp); 21708 break; 21709 case T_UNBIND_REQ: /* unbind request */ 21710 tcp_tpi_unbind(tcp, mp); 21711 break; 21712 case O_T_CONN_RES: /* old connection response XXX */ 21713 case T_CONN_RES: /* connection response */ 21714 tcp_tli_accept(tcp, mp); 21715 break; 21716 case T_CONN_REQ: /* connection request */ 21717 tcp_tpi_connect(tcp, mp); 21718 break; 21719 case T_DISCON_REQ: /* disconnect request */ 21720 tcp_disconnect(tcp, mp); 21721 break; 21722 case T_CAPABILITY_REQ: 21723 tcp_capability_req(tcp, mp); /* capability request */ 21724 break; 21725 case T_INFO_REQ: /* information request */ 21726 tcp_info_req(tcp, mp); 21727 break; 21728 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 21729 case T_OPTMGMT_REQ: 21730 /* 21731 * Note: no support for snmpcom_req() through new 21732 * T_OPTMGMT_REQ. See comments in ip.c 21733 */ 21734 21735 /* 21736 * All Solaris components should pass a db_credp 21737 * for this TPI message, hence we ASSERT. 21738 * But in case there is some other M_PROTO that looks 21739 * like a TPI message sent by some other kernel 21740 * component, we check and return an error. 21741 */ 21742 cr = msg_getcred(mp, NULL); 21743 ASSERT(cr != NULL); 21744 if (cr == NULL) { 21745 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 21746 return; 21747 } 21748 /* 21749 * If EINPROGRESS is returned, the request has been queued 21750 * for subsequent processing by ip_restart_optmgmt(), which 21751 * will do the CONN_DEC_REF(). 21752 */ 21753 CONN_INC_REF(connp); 21754 if ((int)tprim->type == T_SVR4_OPTMGMT_REQ) { 21755 if (svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 21756 B_TRUE) != EINPROGRESS) { 21757 CONN_DEC_REF(connp); 21758 } 21759 } else { 21760 if (tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 21761 B_TRUE) != EINPROGRESS) { 21762 CONN_DEC_REF(connp); 21763 } 21764 } 21765 break; 21766 21767 case T_UNITDATA_REQ: /* unitdata request */ 21768 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21769 break; 21770 case T_ORDREL_REQ: /* orderly release req */ 21771 freemsg(mp); 21772 21773 if (tcp->tcp_fused) 21774 tcp_unfuse(tcp); 21775 21776 if (tcp_xmit_end(tcp) != 0) { 21777 /* 21778 * We were crossing FINs and got a reset from 21779 * the other side. Just ignore it. 21780 */ 21781 if (tcp->tcp_debug) { 21782 (void) strlog(TCP_MOD_ID, 0, 1, 21783 SL_ERROR|SL_TRACE, 21784 "tcp_wput_proto, T_ORDREL_REQ out of " 21785 "state %s", 21786 tcp_display(tcp, NULL, 21787 DISP_ADDR_AND_PORT)); 21788 } 21789 } 21790 break; 21791 case T_ADDR_REQ: 21792 tcp_addr_req(tcp, mp); 21793 break; 21794 default: 21795 if (tcp->tcp_debug) { 21796 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21797 "tcp_wput_proto, bogus TPI msg, type %d", 21798 tprim->type); 21799 } 21800 /* 21801 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 21802 * to recover. 21803 */ 21804 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21805 break; 21806 } 21807 } 21808 21809 /* 21810 * The TCP write service routine should never be called... 21811 */ 21812 /* ARGSUSED */ 21813 static void 21814 tcp_wsrv(queue_t *q) 21815 { 21816 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 21817 21818 TCP_STAT(tcps, tcp_wsrv_called); 21819 } 21820 21821 /* Non overlapping byte exchanger */ 21822 static void 21823 tcp_xchg(uchar_t *a, uchar_t *b, int len) 21824 { 21825 uchar_t uch; 21826 21827 while (len-- > 0) { 21828 uch = a[len]; 21829 a[len] = b[len]; 21830 b[len] = uch; 21831 } 21832 } 21833 21834 /* 21835 * Send out a control packet on the tcp connection specified. This routine 21836 * is typically called where we need a simple ACK or RST generated. 21837 */ 21838 static void 21839 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 21840 { 21841 uchar_t *rptr; 21842 tcph_t *tcph; 21843 ipha_t *ipha = NULL; 21844 ip6_t *ip6h = NULL; 21845 uint32_t sum; 21846 int tcp_hdr_len; 21847 int tcp_ip_hdr_len; 21848 mblk_t *mp; 21849 tcp_stack_t *tcps = tcp->tcp_tcps; 21850 21851 /* 21852 * Save sum for use in source route later. 21853 */ 21854 ASSERT(tcp != NULL); 21855 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 21856 tcp_hdr_len = tcp->tcp_hdr_len; 21857 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 21858 21859 /* If a text string is passed in with the request, pass it to strlog. */ 21860 if (str != NULL && tcp->tcp_debug) { 21861 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21862 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 21863 str, seq, ack, ctl); 21864 } 21865 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 21866 BPRI_MED); 21867 if (mp == NULL) { 21868 return; 21869 } 21870 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 21871 mp->b_rptr = rptr; 21872 mp->b_wptr = &rptr[tcp_hdr_len]; 21873 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 21874 21875 if (tcp->tcp_ipversion == IPV4_VERSION) { 21876 ipha = (ipha_t *)rptr; 21877 ipha->ipha_length = htons(tcp_hdr_len); 21878 } else { 21879 ip6h = (ip6_t *)rptr; 21880 ASSERT(tcp != NULL); 21881 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 21882 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21883 } 21884 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 21885 tcph->th_flags[0] = (uint8_t)ctl; 21886 if (ctl & TH_RST) { 21887 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 21888 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 21889 /* 21890 * Don't send TSopt w/ TH_RST packets per RFC 1323. 21891 */ 21892 if (tcp->tcp_snd_ts_ok && 21893 tcp->tcp_state > TCPS_SYN_SENT) { 21894 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 21895 *(mp->b_wptr) = TCPOPT_EOL; 21896 if (tcp->tcp_ipversion == IPV4_VERSION) { 21897 ipha->ipha_length = htons(tcp_hdr_len - 21898 TCPOPT_REAL_TS_LEN); 21899 } else { 21900 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 21901 TCPOPT_REAL_TS_LEN); 21902 } 21903 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 21904 sum -= TCPOPT_REAL_TS_LEN; 21905 } 21906 } 21907 if (ctl & TH_ACK) { 21908 if (tcp->tcp_snd_ts_ok) { 21909 U32_TO_BE32(lbolt, 21910 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 21911 U32_TO_BE32(tcp->tcp_ts_recent, 21912 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 21913 } 21914 21915 /* Update the latest receive window size in TCP header. */ 21916 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 21917 tcph->th_win); 21918 tcp->tcp_rack = ack; 21919 tcp->tcp_rack_cnt = 0; 21920 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 21921 } 21922 BUMP_LOCAL(tcp->tcp_obsegs); 21923 U32_TO_BE32(seq, tcph->th_seq); 21924 U32_TO_BE32(ack, tcph->th_ack); 21925 /* 21926 * Include the adjustment for a source route if any. 21927 */ 21928 sum = (sum >> 16) + (sum & 0xFFFF); 21929 U16_TO_BE16(sum, tcph->th_sum); 21930 tcp_send_data(tcp, tcp->tcp_wq, mp); 21931 } 21932 21933 /* 21934 * If this routine returns B_TRUE, TCP can generate a RST in response 21935 * to a segment. If it returns B_FALSE, TCP should not respond. 21936 */ 21937 static boolean_t 21938 tcp_send_rst_chk(tcp_stack_t *tcps) 21939 { 21940 clock_t now; 21941 21942 /* 21943 * TCP needs to protect itself from generating too many RSTs. 21944 * This can be a DoS attack by sending us random segments 21945 * soliciting RSTs. 21946 * 21947 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 21948 * in each 1 second interval. In this way, TCP still generate 21949 * RSTs in normal cases but when under attack, the impact is 21950 * limited. 21951 */ 21952 if (tcps->tcps_rst_sent_rate_enabled != 0) { 21953 now = lbolt; 21954 /* lbolt can wrap around. */ 21955 if ((tcps->tcps_last_rst_intrvl > now) || 21956 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 21957 1*SECONDS)) { 21958 tcps->tcps_last_rst_intrvl = now; 21959 tcps->tcps_rst_cnt = 1; 21960 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 21961 return (B_FALSE); 21962 } 21963 } 21964 return (B_TRUE); 21965 } 21966 21967 /* 21968 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 21969 */ 21970 static void 21971 tcp_ip_ire_mark_advice(tcp_t *tcp) 21972 { 21973 mblk_t *mp; 21974 ipic_t *ipic; 21975 21976 if (tcp->tcp_ipversion == IPV4_VERSION) { 21977 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 21978 &ipic); 21979 } else { 21980 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 21981 &ipic); 21982 } 21983 if (mp == NULL) 21984 return; 21985 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 21986 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21987 } 21988 21989 /* 21990 * Return an IP advice ioctl mblk and set ipic to be the pointer 21991 * to the advice structure. 21992 */ 21993 static mblk_t * 21994 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 21995 { 21996 struct iocblk *ioc; 21997 mblk_t *mp, *mp1; 21998 21999 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22000 if (mp == NULL) 22001 return (NULL); 22002 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22003 *ipic = (ipic_t *)mp->b_rptr; 22004 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22005 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22006 22007 bcopy(addr, *ipic + 1, addr_len); 22008 22009 (*ipic)->ipic_addr_length = addr_len; 22010 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22011 22012 mp1 = mkiocb(IP_IOCTL); 22013 if (mp1 == NULL) { 22014 freemsg(mp); 22015 return (NULL); 22016 } 22017 mp1->b_cont = mp; 22018 ioc = (struct iocblk *)mp1->b_rptr; 22019 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22020 22021 return (mp1); 22022 } 22023 22024 /* 22025 * Generate a reset based on an inbound packet, connp is set by caller 22026 * when RST is in response to an unexpected inbound packet for which 22027 * there is active tcp state in the system. 22028 * 22029 * IPSEC NOTE : Try to send the reply with the same protection as it came 22030 * in. We still have the ipsec_mp that the packet was attached to. Thus 22031 * the packet will go out at the same level of protection as it came in by 22032 * converting the IPSEC_IN to IPSEC_OUT. 22033 */ 22034 static void 22035 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22036 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22037 tcp_stack_t *tcps, conn_t *connp) 22038 { 22039 ipha_t *ipha = NULL; 22040 ip6_t *ip6h = NULL; 22041 ushort_t len; 22042 tcph_t *tcph; 22043 int i; 22044 mblk_t *ipsec_mp; 22045 boolean_t mctl_present; 22046 ipic_t *ipic; 22047 ipaddr_t v4addr; 22048 in6_addr_t v6addr; 22049 int addr_len; 22050 void *addr; 22051 queue_t *q = tcps->tcps_g_q; 22052 tcp_t *tcp; 22053 cred_t *cr; 22054 pid_t pid; 22055 mblk_t *nmp; 22056 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22057 22058 if (tcps->tcps_g_q == NULL) { 22059 /* 22060 * For non-zero stackids the default queue isn't created 22061 * until the first open, thus there can be a need to send 22062 * a reset before then. But we can't do that, hence we just 22063 * drop the packet. Later during boot, when the default queue 22064 * has been setup, a retransmitted packet from the peer 22065 * will result in a reset. 22066 */ 22067 ASSERT(tcps->tcps_netstack->netstack_stackid != 22068 GLOBAL_NETSTACKID); 22069 freemsg(mp); 22070 return; 22071 } 22072 22073 if (connp != NULL) 22074 tcp = connp->conn_tcp; 22075 else 22076 tcp = Q_TO_TCP(q); 22077 22078 if (!tcp_send_rst_chk(tcps)) { 22079 tcps->tcps_rst_unsent++; 22080 freemsg(mp); 22081 return; 22082 } 22083 22084 if (mp->b_datap->db_type == M_CTL) { 22085 ipsec_mp = mp; 22086 mp = mp->b_cont; 22087 mctl_present = B_TRUE; 22088 } else { 22089 ipsec_mp = mp; 22090 mctl_present = B_FALSE; 22091 } 22092 22093 if (str && q && tcps->tcps_dbg) { 22094 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22095 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22096 "flags 0x%x", 22097 str, seq, ack, ctl); 22098 } 22099 if (mp->b_datap->db_ref != 1) { 22100 mblk_t *mp1 = copyb(mp); 22101 freemsg(mp); 22102 mp = mp1; 22103 if (!mp) { 22104 if (mctl_present) 22105 freeb(ipsec_mp); 22106 return; 22107 } else { 22108 if (mctl_present) { 22109 ipsec_mp->b_cont = mp; 22110 } else { 22111 ipsec_mp = mp; 22112 } 22113 } 22114 } else if (mp->b_cont) { 22115 freemsg(mp->b_cont); 22116 mp->b_cont = NULL; 22117 } 22118 /* 22119 * We skip reversing source route here. 22120 * (for now we replace all IP options with EOL) 22121 */ 22122 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22123 ipha = (ipha_t *)mp->b_rptr; 22124 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22125 mp->b_rptr[i] = IPOPT_EOL; 22126 /* 22127 * Make sure that src address isn't flagrantly invalid. 22128 * Not all broadcast address checking for the src address 22129 * is possible, since we don't know the netmask of the src 22130 * addr. No check for destination address is done, since 22131 * IP will not pass up a packet with a broadcast dest 22132 * address to TCP. Similar checks are done below for IPv6. 22133 */ 22134 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22135 CLASSD(ipha->ipha_src)) { 22136 freemsg(ipsec_mp); 22137 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22138 return; 22139 } 22140 } else { 22141 ip6h = (ip6_t *)mp->b_rptr; 22142 22143 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22144 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22145 freemsg(ipsec_mp); 22146 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22147 return; 22148 } 22149 22150 /* Remove any extension headers assuming partial overlay */ 22151 if (ip_hdr_len > IPV6_HDR_LEN) { 22152 uint8_t *to; 22153 22154 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22155 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22156 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22157 ip_hdr_len = IPV6_HDR_LEN; 22158 ip6h = (ip6_t *)mp->b_rptr; 22159 ip6h->ip6_nxt = IPPROTO_TCP; 22160 } 22161 } 22162 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22163 if (tcph->th_flags[0] & TH_RST) { 22164 freemsg(ipsec_mp); 22165 return; 22166 } 22167 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22168 len = ip_hdr_len + sizeof (tcph_t); 22169 mp->b_wptr = &mp->b_rptr[len]; 22170 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22171 ipha->ipha_length = htons(len); 22172 /* Swap addresses */ 22173 v4addr = ipha->ipha_src; 22174 ipha->ipha_src = ipha->ipha_dst; 22175 ipha->ipha_dst = v4addr; 22176 ipha->ipha_ident = 0; 22177 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22178 addr_len = IP_ADDR_LEN; 22179 addr = &v4addr; 22180 } else { 22181 /* No ip6i_t in this case */ 22182 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22183 /* Swap addresses */ 22184 v6addr = ip6h->ip6_src; 22185 ip6h->ip6_src = ip6h->ip6_dst; 22186 ip6h->ip6_dst = v6addr; 22187 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22188 addr_len = IPV6_ADDR_LEN; 22189 addr = &v6addr; 22190 } 22191 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22192 U32_TO_BE32(ack, tcph->th_ack); 22193 U32_TO_BE32(seq, tcph->th_seq); 22194 U16_TO_BE16(0, tcph->th_win); 22195 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22196 tcph->th_flags[0] = (uint8_t)ctl; 22197 if (ctl & TH_RST) { 22198 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22199 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22200 } 22201 22202 /* IP trusts us to set up labels when required. */ 22203 if (is_system_labeled() && (cr = msg_getcred(mp, &pid)) != NULL && 22204 crgetlabel(cr) != NULL) { 22205 int err; 22206 22207 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22208 err = tsol_check_label(cr, &mp, 22209 tcp->tcp_connp->conn_mac_exempt, 22210 tcps->tcps_netstack->netstack_ip, pid); 22211 else 22212 err = tsol_check_label_v6(cr, &mp, 22213 tcp->tcp_connp->conn_mac_exempt, 22214 tcps->tcps_netstack->netstack_ip, pid); 22215 if (mctl_present) 22216 ipsec_mp->b_cont = mp; 22217 else 22218 ipsec_mp = mp; 22219 if (err != 0) { 22220 freemsg(ipsec_mp); 22221 return; 22222 } 22223 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22224 ipha = (ipha_t *)mp->b_rptr; 22225 } else { 22226 ip6h = (ip6_t *)mp->b_rptr; 22227 } 22228 } 22229 22230 if (mctl_present) { 22231 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22232 22233 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22234 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22235 return; 22236 } 22237 } 22238 if (zoneid == ALL_ZONES) 22239 zoneid = GLOBAL_ZONEID; 22240 22241 /* Add the zoneid so ip_output routes it properly */ 22242 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 22243 freemsg(ipsec_mp); 22244 return; 22245 } 22246 ipsec_mp = nmp; 22247 22248 /* 22249 * NOTE: one might consider tracing a TCP packet here, but 22250 * this function has no active TCP state and no tcp structure 22251 * that has a trace buffer. If we traced here, we would have 22252 * to keep a local trace buffer in tcp_record_trace(). 22253 * 22254 * TSol note: The mblk that contains the incoming packet was 22255 * reused by tcp_xmit_listener_reset, so it already contains 22256 * the right credentials and we don't need to call mblk_setcred. 22257 * Also the conn's cred is not right since it is associated 22258 * with tcps_g_q. 22259 */ 22260 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22261 22262 /* 22263 * Tell IP to mark the IRE used for this destination temporary. 22264 * This way, we can limit our exposure to DoS attack because IP 22265 * creates an IRE for each destination. If there are too many, 22266 * the time to do any routing lookup will be extremely long. And 22267 * the lookup can be in interrupt context. 22268 * 22269 * Note that in normal circumstances, this marking should not 22270 * affect anything. It would be nice if only 1 message is 22271 * needed to inform IP that the IRE created for this RST should 22272 * not be added to the cache table. But there is currently 22273 * not such communication mechanism between TCP and IP. So 22274 * the best we can do now is to send the advice ioctl to IP 22275 * to mark the IRE temporary. 22276 */ 22277 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22278 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22279 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22280 } 22281 } 22282 22283 /* 22284 * Initiate closedown sequence on an active connection. (May be called as 22285 * writer.) Return value zero for OK return, non-zero for error return. 22286 */ 22287 static int 22288 tcp_xmit_end(tcp_t *tcp) 22289 { 22290 ipic_t *ipic; 22291 mblk_t *mp; 22292 tcp_stack_t *tcps = tcp->tcp_tcps; 22293 22294 if (tcp->tcp_state < TCPS_SYN_RCVD || 22295 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22296 /* 22297 * Invalid state, only states TCPS_SYN_RCVD, 22298 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22299 */ 22300 return (-1); 22301 } 22302 22303 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22304 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22305 /* 22306 * If there is nothing more unsent, send the FIN now. 22307 * Otherwise, it will go out with the last segment. 22308 */ 22309 if (tcp->tcp_unsent == 0) { 22310 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22311 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22312 22313 if (mp) { 22314 tcp_send_data(tcp, tcp->tcp_wq, mp); 22315 } else { 22316 /* 22317 * Couldn't allocate msg. Pretend we got it out. 22318 * Wait for rexmit timeout. 22319 */ 22320 tcp->tcp_snxt = tcp->tcp_fss + 1; 22321 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22322 } 22323 22324 /* 22325 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22326 * changed. 22327 */ 22328 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22329 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22330 } 22331 } else { 22332 /* 22333 * If tcp->tcp_cork is set, then the data will not get sent, 22334 * so we have to check that and unset it first. 22335 */ 22336 if (tcp->tcp_cork) 22337 tcp->tcp_cork = B_FALSE; 22338 tcp_wput_data(tcp, NULL, B_FALSE); 22339 } 22340 22341 /* 22342 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22343 * is 0, don't update the cache. 22344 */ 22345 if (tcps->tcps_rtt_updates == 0 || 22346 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 22347 return (0); 22348 22349 /* 22350 * NOTE: should not update if source routes i.e. if tcp_remote if 22351 * different from the destination. 22352 */ 22353 if (tcp->tcp_ipversion == IPV4_VERSION) { 22354 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22355 return (0); 22356 } 22357 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22358 &ipic); 22359 } else { 22360 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22361 &tcp->tcp_ip6h->ip6_dst))) { 22362 return (0); 22363 } 22364 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22365 &ipic); 22366 } 22367 22368 /* Record route attributes in the IRE for use by future connections. */ 22369 if (mp == NULL) 22370 return (0); 22371 22372 /* 22373 * We do not have a good algorithm to update ssthresh at this time. 22374 * So don't do any update. 22375 */ 22376 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22377 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22378 22379 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22380 22381 return (0); 22382 } 22383 22384 /* ARGSUSED */ 22385 void 22386 tcp_xmit_reset(void *arg, mblk_t *mp, void *arg2) 22387 { 22388 conn_t *connp = (conn_t *)arg; 22389 mblk_t *mp1; 22390 tcp_t *tcp = connp->conn_tcp; 22391 tcp_xmit_reset_event_t *eventp; 22392 22393 ASSERT(mp->b_datap->db_type == M_PROTO && 22394 MBLKL(mp) == sizeof (tcp_xmit_reset_event_t)); 22395 22396 if (tcp->tcp_state != TCPS_LISTEN) { 22397 freemsg(mp); 22398 return; 22399 } 22400 22401 mp1 = mp->b_cont; 22402 mp->b_cont = NULL; 22403 eventp = (tcp_xmit_reset_event_t *)mp->b_rptr; 22404 ASSERT(eventp->tcp_xre_tcps->tcps_netstack == 22405 connp->conn_netstack); 22406 22407 tcp_xmit_listeners_reset(mp1, eventp->tcp_xre_iphdrlen, 22408 eventp->tcp_xre_zoneid, eventp->tcp_xre_tcps, connp); 22409 freemsg(mp); 22410 } 22411 22412 /* 22413 * Generate a "no listener here" RST in response to an "unknown" segment. 22414 * connp is set by caller when RST is in response to an unexpected 22415 * inbound packet for which there is active tcp state in the system. 22416 * Note that we are reusing the incoming mp to construct the outgoing RST. 22417 */ 22418 void 22419 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 22420 tcp_stack_t *tcps, conn_t *connp) 22421 { 22422 uchar_t *rptr; 22423 uint32_t seg_len; 22424 tcph_t *tcph; 22425 uint32_t seg_seq; 22426 uint32_t seg_ack; 22427 uint_t flags; 22428 mblk_t *ipsec_mp; 22429 ipha_t *ipha; 22430 ip6_t *ip6h; 22431 boolean_t mctl_present = B_FALSE; 22432 boolean_t check = B_TRUE; 22433 boolean_t policy_present; 22434 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 22435 22436 TCP_STAT(tcps, tcp_no_listener); 22437 22438 ipsec_mp = mp; 22439 22440 if (mp->b_datap->db_type == M_CTL) { 22441 ipsec_in_t *ii; 22442 22443 mctl_present = B_TRUE; 22444 mp = mp->b_cont; 22445 22446 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22447 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22448 if (ii->ipsec_in_dont_check) { 22449 check = B_FALSE; 22450 if (!ii->ipsec_in_secure) { 22451 freeb(ipsec_mp); 22452 mctl_present = B_FALSE; 22453 ipsec_mp = mp; 22454 } 22455 } 22456 } 22457 22458 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22459 policy_present = ipss->ipsec_inbound_v4_policy_present; 22460 ipha = (ipha_t *)mp->b_rptr; 22461 ip6h = NULL; 22462 } else { 22463 policy_present = ipss->ipsec_inbound_v6_policy_present; 22464 ipha = NULL; 22465 ip6h = (ip6_t *)mp->b_rptr; 22466 } 22467 22468 if (check && policy_present) { 22469 /* 22470 * The conn_t parameter is NULL because we already know 22471 * nobody's home. 22472 */ 22473 ipsec_mp = ipsec_check_global_policy( 22474 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 22475 tcps->tcps_netstack); 22476 if (ipsec_mp == NULL) 22477 return; 22478 } 22479 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22480 DTRACE_PROBE2( 22481 tx__ip__log__error__nolistener__tcp, 22482 char *, "Could not reply with RST to mp(1)", 22483 mblk_t *, mp); 22484 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22485 freemsg(ipsec_mp); 22486 return; 22487 } 22488 22489 rptr = mp->b_rptr; 22490 22491 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22492 seg_seq = BE32_TO_U32(tcph->th_seq); 22493 seg_ack = BE32_TO_U32(tcph->th_ack); 22494 flags = tcph->th_flags[0]; 22495 22496 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22497 if (flags & TH_RST) { 22498 freemsg(ipsec_mp); 22499 } else if (flags & TH_ACK) { 22500 tcp_xmit_early_reset("no tcp, reset", 22501 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 22502 connp); 22503 } else { 22504 if (flags & TH_SYN) { 22505 seg_len++; 22506 } else { 22507 /* 22508 * Here we violate the RFC. Note that a normal 22509 * TCP will never send a segment without the ACK 22510 * flag, except for RST or SYN segment. This 22511 * segment is neither. Just drop it on the 22512 * floor. 22513 */ 22514 freemsg(ipsec_mp); 22515 tcps->tcps_rst_unsent++; 22516 return; 22517 } 22518 22519 tcp_xmit_early_reset("no tcp, reset/ack", 22520 ipsec_mp, 0, seg_seq + seg_len, 22521 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 22522 } 22523 } 22524 22525 /* 22526 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22527 * ip and tcp header ready to pass down to IP. If the mp passed in is 22528 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22529 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22530 * otherwise it will dup partial mblks.) 22531 * Otherwise, an appropriate ACK packet will be generated. This 22532 * routine is not usually called to send new data for the first time. It 22533 * is mostly called out of the timer for retransmits, and to generate ACKs. 22534 * 22535 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22536 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22537 * of the original mblk chain will be returned in *offset and *end_mp. 22538 */ 22539 mblk_t * 22540 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22541 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22542 boolean_t rexmit) 22543 { 22544 int data_length; 22545 int32_t off = 0; 22546 uint_t flags; 22547 mblk_t *mp1; 22548 mblk_t *mp2; 22549 uchar_t *rptr; 22550 tcph_t *tcph; 22551 int32_t num_sack_blk = 0; 22552 int32_t sack_opt_len = 0; 22553 tcp_stack_t *tcps = tcp->tcp_tcps; 22554 22555 /* Allocate for our maximum TCP header + link-level */ 22556 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 22557 tcps->tcps_wroff_xtra, BPRI_MED); 22558 if (!mp1) 22559 return (NULL); 22560 data_length = 0; 22561 22562 /* 22563 * Note that tcp_mss has been adjusted to take into account the 22564 * timestamp option if applicable. Because SACK options do not 22565 * appear in every TCP segments and they are of variable lengths, 22566 * they cannot be included in tcp_mss. Thus we need to calculate 22567 * the actual segment length when we need to send a segment which 22568 * includes SACK options. 22569 */ 22570 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22571 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22572 tcp->tcp_num_sack_blk); 22573 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22574 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22575 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22576 max_to_send -= sack_opt_len; 22577 } 22578 22579 if (offset != NULL) { 22580 off = *offset; 22581 /* We use offset as an indicator that end_mp is not NULL. */ 22582 *end_mp = NULL; 22583 } 22584 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22585 /* This could be faster with cooperation from downstream */ 22586 if (mp2 != mp1 && !sendall && 22587 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22588 max_to_send) 22589 /* 22590 * Don't send the next mblk since the whole mblk 22591 * does not fit. 22592 */ 22593 break; 22594 mp2->b_cont = dupb(mp); 22595 mp2 = mp2->b_cont; 22596 if (!mp2) { 22597 freemsg(mp1); 22598 return (NULL); 22599 } 22600 mp2->b_rptr += off; 22601 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 22602 (uintptr_t)INT_MAX); 22603 22604 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 22605 if (data_length > max_to_send) { 22606 mp2->b_wptr -= data_length - max_to_send; 22607 data_length = max_to_send; 22608 off = mp2->b_wptr - mp->b_rptr; 22609 break; 22610 } else { 22611 off = 0; 22612 } 22613 } 22614 if (offset != NULL) { 22615 *offset = off; 22616 *end_mp = mp; 22617 } 22618 if (seg_len != NULL) { 22619 *seg_len = data_length; 22620 } 22621 22622 /* Update the latest receive window size in TCP header. */ 22623 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22624 tcp->tcp_tcph->th_win); 22625 22626 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22627 mp1->b_rptr = rptr; 22628 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 22629 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22630 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22631 U32_TO_ABE32(seq, tcph->th_seq); 22632 22633 /* 22634 * Use tcp_unsent to determine if the PUSH bit should be used assumes 22635 * that this function was called from tcp_wput_data. Thus, when called 22636 * to retransmit data the setting of the PUSH bit may appear some 22637 * what random in that it might get set when it should not. This 22638 * should not pose any performance issues. 22639 */ 22640 if (data_length != 0 && (tcp->tcp_unsent == 0 || 22641 tcp->tcp_unsent == data_length)) { 22642 flags = TH_ACK | TH_PUSH; 22643 } else { 22644 flags = TH_ACK; 22645 } 22646 22647 if (tcp->tcp_ecn_ok) { 22648 if (tcp->tcp_ecn_echo_on) 22649 flags |= TH_ECE; 22650 22651 /* 22652 * Only set ECT bit and ECN_CWR if a segment contains new data. 22653 * There is no TCP flow control for non-data segments, and 22654 * only data segment is transmitted reliably. 22655 */ 22656 if (data_length > 0 && !rexmit) { 22657 SET_ECT(tcp, rptr); 22658 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 22659 flags |= TH_CWR; 22660 tcp->tcp_ecn_cwr_sent = B_TRUE; 22661 } 22662 } 22663 } 22664 22665 if (tcp->tcp_valid_bits) { 22666 uint32_t u1; 22667 22668 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 22669 seq == tcp->tcp_iss) { 22670 uchar_t *wptr; 22671 22672 /* 22673 * If TCP_ISS_VALID and the seq number is tcp_iss, 22674 * TCP can only be in SYN-SENT, SYN-RCVD or 22675 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 22676 * our SYN is not ack'ed but the app closes this 22677 * TCP connection. 22678 */ 22679 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 22680 tcp->tcp_state == TCPS_SYN_RCVD || 22681 tcp->tcp_state == TCPS_FIN_WAIT_1); 22682 22683 /* 22684 * Tack on the MSS option. It is always needed 22685 * for both active and passive open. 22686 * 22687 * MSS option value should be interface MTU - MIN 22688 * TCP/IP header according to RFC 793 as it means 22689 * the maximum segment size TCP can receive. But 22690 * to get around some broken middle boxes/end hosts 22691 * out there, we allow the option value to be the 22692 * same as the MSS option size on the peer side. 22693 * In this way, the other side will not send 22694 * anything larger than they can receive. 22695 * 22696 * Note that for SYN_SENT state, the ndd param 22697 * tcp_use_smss_as_mss_opt has no effect as we 22698 * don't know the peer's MSS option value. So 22699 * the only case we need to take care of is in 22700 * SYN_RCVD state, which is done later. 22701 */ 22702 wptr = mp1->b_wptr; 22703 wptr[0] = TCPOPT_MAXSEG; 22704 wptr[1] = TCPOPT_MAXSEG_LEN; 22705 wptr += 2; 22706 u1 = tcp->tcp_if_mtu - 22707 (tcp->tcp_ipversion == IPV4_VERSION ? 22708 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 22709 TCP_MIN_HEADER_LENGTH; 22710 U16_TO_BE16(u1, wptr); 22711 mp1->b_wptr = wptr + 2; 22712 /* Update the offset to cover the additional word */ 22713 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22714 22715 /* 22716 * Note that the following way of filling in 22717 * TCP options are not optimal. Some NOPs can 22718 * be saved. But there is no need at this time 22719 * to optimize it. When it is needed, we will 22720 * do it. 22721 */ 22722 switch (tcp->tcp_state) { 22723 case TCPS_SYN_SENT: 22724 flags = TH_SYN; 22725 22726 if (tcp->tcp_snd_ts_ok) { 22727 uint32_t llbolt = (uint32_t)lbolt; 22728 22729 wptr = mp1->b_wptr; 22730 wptr[0] = TCPOPT_NOP; 22731 wptr[1] = TCPOPT_NOP; 22732 wptr[2] = TCPOPT_TSTAMP; 22733 wptr[3] = TCPOPT_TSTAMP_LEN; 22734 wptr += 4; 22735 U32_TO_BE32(llbolt, wptr); 22736 wptr += 4; 22737 ASSERT(tcp->tcp_ts_recent == 0); 22738 U32_TO_BE32(0L, wptr); 22739 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 22740 tcph->th_offset_and_rsrvd[0] += 22741 (3 << 4); 22742 } 22743 22744 /* 22745 * Set up all the bits to tell other side 22746 * we are ECN capable. 22747 */ 22748 if (tcp->tcp_ecn_ok) { 22749 flags |= (TH_ECE | TH_CWR); 22750 } 22751 break; 22752 case TCPS_SYN_RCVD: 22753 flags |= TH_SYN; 22754 22755 /* 22756 * Reset the MSS option value to be SMSS 22757 * We should probably add back the bytes 22758 * for timestamp option and IPsec. We 22759 * don't do that as this is a workaround 22760 * for broken middle boxes/end hosts, it 22761 * is better for us to be more cautious. 22762 * They may not take these things into 22763 * account in their SMSS calculation. Thus 22764 * the peer's calculated SMSS may be smaller 22765 * than what it can be. This should be OK. 22766 */ 22767 if (tcps->tcps_use_smss_as_mss_opt) { 22768 u1 = tcp->tcp_mss; 22769 U16_TO_BE16(u1, wptr); 22770 } 22771 22772 /* 22773 * If the other side is ECN capable, reply 22774 * that we are also ECN capable. 22775 */ 22776 if (tcp->tcp_ecn_ok) 22777 flags |= TH_ECE; 22778 break; 22779 default: 22780 /* 22781 * The above ASSERT() makes sure that this 22782 * must be FIN-WAIT-1 state. Our SYN has 22783 * not been ack'ed so retransmit it. 22784 */ 22785 flags |= TH_SYN; 22786 break; 22787 } 22788 22789 if (tcp->tcp_snd_ws_ok) { 22790 wptr = mp1->b_wptr; 22791 wptr[0] = TCPOPT_NOP; 22792 wptr[1] = TCPOPT_WSCALE; 22793 wptr[2] = TCPOPT_WS_LEN; 22794 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 22795 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 22796 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22797 } 22798 22799 if (tcp->tcp_snd_sack_ok) { 22800 wptr = mp1->b_wptr; 22801 wptr[0] = TCPOPT_NOP; 22802 wptr[1] = TCPOPT_NOP; 22803 wptr[2] = TCPOPT_SACK_PERMITTED; 22804 wptr[3] = TCPOPT_SACK_OK_LEN; 22805 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 22806 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22807 } 22808 22809 /* allocb() of adequate mblk assures space */ 22810 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 22811 (uintptr_t)INT_MAX); 22812 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 22813 /* 22814 * Get IP set to checksum on our behalf 22815 * Include the adjustment for a source route if any. 22816 */ 22817 u1 += tcp->tcp_sum; 22818 u1 = (u1 >> 16) + (u1 & 0xFFFF); 22819 U16_TO_BE16(u1, tcph->th_sum); 22820 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22821 } 22822 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 22823 (seq + data_length) == tcp->tcp_fss) { 22824 if (!tcp->tcp_fin_acked) { 22825 flags |= TH_FIN; 22826 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22827 } 22828 if (!tcp->tcp_fin_sent) { 22829 tcp->tcp_fin_sent = B_TRUE; 22830 switch (tcp->tcp_state) { 22831 case TCPS_SYN_RCVD: 22832 case TCPS_ESTABLISHED: 22833 tcp->tcp_state = TCPS_FIN_WAIT_1; 22834 break; 22835 case TCPS_CLOSE_WAIT: 22836 tcp->tcp_state = TCPS_LAST_ACK; 22837 break; 22838 } 22839 if (tcp->tcp_suna == tcp->tcp_snxt) 22840 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22841 tcp->tcp_snxt = tcp->tcp_fss + 1; 22842 } 22843 } 22844 /* 22845 * Note the trick here. u1 is unsigned. When tcp_urg 22846 * is smaller than seq, u1 will become a very huge value. 22847 * So the comparison will fail. Also note that tcp_urp 22848 * should be positive, see RFC 793 page 17. 22849 */ 22850 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 22851 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 22852 u1 < (uint32_t)(64 * 1024)) { 22853 flags |= TH_URG; 22854 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 22855 U32_TO_ABE16(u1, tcph->th_urp); 22856 } 22857 } 22858 tcph->th_flags[0] = (uchar_t)flags; 22859 tcp->tcp_rack = tcp->tcp_rnxt; 22860 tcp->tcp_rack_cnt = 0; 22861 22862 if (tcp->tcp_snd_ts_ok) { 22863 if (tcp->tcp_state != TCPS_SYN_SENT) { 22864 uint32_t llbolt = (uint32_t)lbolt; 22865 22866 U32_TO_BE32(llbolt, 22867 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22868 U32_TO_BE32(tcp->tcp_ts_recent, 22869 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22870 } 22871 } 22872 22873 if (num_sack_blk > 0) { 22874 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22875 sack_blk_t *tmp; 22876 int32_t i; 22877 22878 wptr[0] = TCPOPT_NOP; 22879 wptr[1] = TCPOPT_NOP; 22880 wptr[2] = TCPOPT_SACK; 22881 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22882 sizeof (sack_blk_t); 22883 wptr += TCPOPT_REAL_SACK_LEN; 22884 22885 tmp = tcp->tcp_sack_list; 22886 for (i = 0; i < num_sack_blk; i++) { 22887 U32_TO_BE32(tmp[i].begin, wptr); 22888 wptr += sizeof (tcp_seq); 22889 U32_TO_BE32(tmp[i].end, wptr); 22890 wptr += sizeof (tcp_seq); 22891 } 22892 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 22893 } 22894 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22895 data_length += (int)(mp1->b_wptr - rptr); 22896 if (tcp->tcp_ipversion == IPV4_VERSION) { 22897 ((ipha_t *)rptr)->ipha_length = htons(data_length); 22898 } else { 22899 ip6_t *ip6 = (ip6_t *)(rptr + 22900 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22901 sizeof (ip6i_t) : 0)); 22902 22903 ip6->ip6_plen = htons(data_length - 22904 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22905 } 22906 22907 /* 22908 * Prime pump for IP 22909 * Include the adjustment for a source route if any. 22910 */ 22911 data_length -= tcp->tcp_ip_hdr_len; 22912 data_length += tcp->tcp_sum; 22913 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22914 U16_TO_ABE16(data_length, tcph->th_sum); 22915 if (tcp->tcp_ip_forward_progress) { 22916 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22917 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22918 tcp->tcp_ip_forward_progress = B_FALSE; 22919 } 22920 return (mp1); 22921 } 22922 22923 /* This function handles the push timeout. */ 22924 void 22925 tcp_push_timer(void *arg) 22926 { 22927 conn_t *connp = (conn_t *)arg; 22928 tcp_t *tcp = connp->conn_tcp; 22929 22930 TCP_DBGSTAT(tcp->tcp_tcps, tcp_push_timer_cnt); 22931 22932 ASSERT(tcp->tcp_listener == NULL); 22933 22934 ASSERT(!IPCL_IS_NONSTR(connp)); 22935 22936 tcp->tcp_push_tid = 0; 22937 22938 if (tcp->tcp_rcv_list != NULL && 22939 tcp_rcv_drain(tcp) == TH_ACK_NEEDED) 22940 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 22941 } 22942 22943 /* 22944 * This function handles delayed ACK timeout. 22945 */ 22946 static void 22947 tcp_ack_timer(void *arg) 22948 { 22949 conn_t *connp = (conn_t *)arg; 22950 tcp_t *tcp = connp->conn_tcp; 22951 mblk_t *mp; 22952 tcp_stack_t *tcps = tcp->tcp_tcps; 22953 22954 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 22955 22956 tcp->tcp_ack_tid = 0; 22957 22958 if (tcp->tcp_fused) 22959 return; 22960 22961 /* 22962 * Do not send ACK if there is no outstanding unack'ed data. 22963 */ 22964 if (tcp->tcp_rnxt == tcp->tcp_rack) { 22965 return; 22966 } 22967 22968 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 22969 /* 22970 * Make sure we don't allow deferred ACKs to result in 22971 * timer-based ACKing. If we have held off an ACK 22972 * when there was more than an mss here, and the timer 22973 * goes off, we have to worry about the possibility 22974 * that the sender isn't doing slow-start, or is out 22975 * of step with us for some other reason. We fall 22976 * permanently back in the direction of 22977 * ACK-every-other-packet as suggested in RFC 1122. 22978 */ 22979 if (tcp->tcp_rack_abs_max > 2) 22980 tcp->tcp_rack_abs_max--; 22981 tcp->tcp_rack_cur_max = 2; 22982 } 22983 mp = tcp_ack_mp(tcp); 22984 22985 if (mp != NULL) { 22986 BUMP_LOCAL(tcp->tcp_obsegs); 22987 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22988 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 22989 tcp_send_data(tcp, tcp->tcp_wq, mp); 22990 } 22991 } 22992 22993 22994 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 22995 static mblk_t * 22996 tcp_ack_mp(tcp_t *tcp) 22997 { 22998 uint32_t seq_no; 22999 tcp_stack_t *tcps = tcp->tcp_tcps; 23000 23001 /* 23002 * There are a few cases to be considered while setting the sequence no. 23003 * Essentially, we can come here while processing an unacceptable pkt 23004 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23005 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23006 * If we are here for a zero window probe, stick with suna. In all 23007 * other cases, we check if suna + swnd encompasses snxt and set 23008 * the sequence number to snxt, if so. If snxt falls outside the 23009 * window (the receiver probably shrunk its window), we will go with 23010 * suna + swnd, otherwise the sequence no will be unacceptable to the 23011 * receiver. 23012 */ 23013 if (tcp->tcp_zero_win_probe) { 23014 seq_no = tcp->tcp_suna; 23015 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23016 ASSERT(tcp->tcp_swnd == 0); 23017 seq_no = tcp->tcp_snxt; 23018 } else { 23019 seq_no = SEQ_GT(tcp->tcp_snxt, 23020 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23021 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23022 } 23023 23024 if (tcp->tcp_valid_bits) { 23025 /* 23026 * For the complex case where we have to send some 23027 * controls (FIN or SYN), let tcp_xmit_mp do it. 23028 */ 23029 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23030 NULL, B_FALSE)); 23031 } else { 23032 /* Generate a simple ACK */ 23033 int data_length; 23034 uchar_t *rptr; 23035 tcph_t *tcph; 23036 mblk_t *mp1; 23037 int32_t tcp_hdr_len; 23038 int32_t tcp_tcp_hdr_len; 23039 int32_t num_sack_blk = 0; 23040 int32_t sack_opt_len; 23041 23042 /* 23043 * Allocate space for TCP + IP headers 23044 * and link-level header 23045 */ 23046 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23047 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23048 tcp->tcp_num_sack_blk); 23049 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23050 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23051 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23052 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23053 } else { 23054 tcp_hdr_len = tcp->tcp_hdr_len; 23055 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23056 } 23057 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23058 if (!mp1) 23059 return (NULL); 23060 23061 /* Update the latest receive window size in TCP header. */ 23062 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23063 tcp->tcp_tcph->th_win); 23064 /* copy in prototype TCP + IP header */ 23065 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23066 mp1->b_rptr = rptr; 23067 mp1->b_wptr = rptr + tcp_hdr_len; 23068 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23069 23070 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23071 23072 /* Set the TCP sequence number. */ 23073 U32_TO_ABE32(seq_no, tcph->th_seq); 23074 23075 /* Set up the TCP flag field. */ 23076 tcph->th_flags[0] = (uchar_t)TH_ACK; 23077 if (tcp->tcp_ecn_echo_on) 23078 tcph->th_flags[0] |= TH_ECE; 23079 23080 tcp->tcp_rack = tcp->tcp_rnxt; 23081 tcp->tcp_rack_cnt = 0; 23082 23083 /* fill in timestamp option if in use */ 23084 if (tcp->tcp_snd_ts_ok) { 23085 uint32_t llbolt = (uint32_t)lbolt; 23086 23087 U32_TO_BE32(llbolt, 23088 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23089 U32_TO_BE32(tcp->tcp_ts_recent, 23090 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23091 } 23092 23093 /* Fill in SACK options */ 23094 if (num_sack_blk > 0) { 23095 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23096 sack_blk_t *tmp; 23097 int32_t i; 23098 23099 wptr[0] = TCPOPT_NOP; 23100 wptr[1] = TCPOPT_NOP; 23101 wptr[2] = TCPOPT_SACK; 23102 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23103 sizeof (sack_blk_t); 23104 wptr += TCPOPT_REAL_SACK_LEN; 23105 23106 tmp = tcp->tcp_sack_list; 23107 for (i = 0; i < num_sack_blk; i++) { 23108 U32_TO_BE32(tmp[i].begin, wptr); 23109 wptr += sizeof (tcp_seq); 23110 U32_TO_BE32(tmp[i].end, wptr); 23111 wptr += sizeof (tcp_seq); 23112 } 23113 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23114 << 4); 23115 } 23116 23117 if (tcp->tcp_ipversion == IPV4_VERSION) { 23118 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23119 } else { 23120 /* Check for ip6i_t header in sticky hdrs */ 23121 ip6_t *ip6 = (ip6_t *)(rptr + 23122 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23123 sizeof (ip6i_t) : 0)); 23124 23125 ip6->ip6_plen = htons(tcp_hdr_len - 23126 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23127 } 23128 23129 /* 23130 * Prime pump for checksum calculation in IP. Include the 23131 * adjustment for a source route if any. 23132 */ 23133 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23134 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23135 U16_TO_ABE16(data_length, tcph->th_sum); 23136 23137 if (tcp->tcp_ip_forward_progress) { 23138 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23139 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23140 tcp->tcp_ip_forward_progress = B_FALSE; 23141 } 23142 return (mp1); 23143 } 23144 } 23145 23146 /* 23147 * Hash list insertion routine for tcp_t structures. Each hash bucket 23148 * contains a list of tcp_t entries, and each entry is bound to a unique 23149 * port. If there are multiple tcp_t's that are bound to the same port, then 23150 * one of them will be linked into the hash bucket list, and the rest will 23151 * hang off of that one entry. For each port, entries bound to a specific IP 23152 * address will be inserted before those those bound to INADDR_ANY. 23153 */ 23154 static void 23155 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23156 { 23157 tcp_t **tcpp; 23158 tcp_t *tcpnext; 23159 tcp_t *tcphash; 23160 23161 if (tcp->tcp_ptpbhn != NULL) { 23162 ASSERT(!caller_holds_lock); 23163 tcp_bind_hash_remove(tcp); 23164 } 23165 tcpp = &tbf->tf_tcp; 23166 if (!caller_holds_lock) { 23167 mutex_enter(&tbf->tf_lock); 23168 } else { 23169 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23170 } 23171 tcphash = tcpp[0]; 23172 tcpnext = NULL; 23173 if (tcphash != NULL) { 23174 /* Look for an entry using the same port */ 23175 while ((tcphash = tcpp[0]) != NULL && 23176 tcp->tcp_lport != tcphash->tcp_lport) 23177 tcpp = &(tcphash->tcp_bind_hash); 23178 23179 /* The port was not found, just add to the end */ 23180 if (tcphash == NULL) 23181 goto insert; 23182 23183 /* 23184 * OK, there already exists an entry bound to the 23185 * same port. 23186 * 23187 * If the new tcp bound to the INADDR_ANY address 23188 * and the first one in the list is not bound to 23189 * INADDR_ANY we skip all entries until we find the 23190 * first one bound to INADDR_ANY. 23191 * This makes sure that applications binding to a 23192 * specific address get preference over those binding to 23193 * INADDR_ANY. 23194 */ 23195 tcpnext = tcphash; 23196 tcphash = NULL; 23197 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23198 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23199 while ((tcpnext = tcpp[0]) != NULL && 23200 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23201 tcpp = &(tcpnext->tcp_bind_hash_port); 23202 23203 if (tcpnext) { 23204 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23205 tcphash = tcpnext->tcp_bind_hash; 23206 if (tcphash != NULL) { 23207 tcphash->tcp_ptpbhn = 23208 &(tcp->tcp_bind_hash); 23209 tcpnext->tcp_bind_hash = NULL; 23210 } 23211 } 23212 } else { 23213 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23214 tcphash = tcpnext->tcp_bind_hash; 23215 if (tcphash != NULL) { 23216 tcphash->tcp_ptpbhn = 23217 &(tcp->tcp_bind_hash); 23218 tcpnext->tcp_bind_hash = NULL; 23219 } 23220 } 23221 } 23222 insert: 23223 tcp->tcp_bind_hash_port = tcpnext; 23224 tcp->tcp_bind_hash = tcphash; 23225 tcp->tcp_ptpbhn = tcpp; 23226 tcpp[0] = tcp; 23227 if (!caller_holds_lock) 23228 mutex_exit(&tbf->tf_lock); 23229 } 23230 23231 /* 23232 * Hash list removal routine for tcp_t structures. 23233 */ 23234 static void 23235 tcp_bind_hash_remove(tcp_t *tcp) 23236 { 23237 tcp_t *tcpnext; 23238 kmutex_t *lockp; 23239 tcp_stack_t *tcps = tcp->tcp_tcps; 23240 23241 if (tcp->tcp_ptpbhn == NULL) 23242 return; 23243 23244 /* 23245 * Extract the lock pointer in case there are concurrent 23246 * hash_remove's for this instance. 23247 */ 23248 ASSERT(tcp->tcp_lport != 0); 23249 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23250 23251 ASSERT(lockp != NULL); 23252 mutex_enter(lockp); 23253 if (tcp->tcp_ptpbhn) { 23254 tcpnext = tcp->tcp_bind_hash_port; 23255 if (tcpnext != NULL) { 23256 tcp->tcp_bind_hash_port = NULL; 23257 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23258 tcpnext->tcp_bind_hash = tcp->tcp_bind_hash; 23259 if (tcpnext->tcp_bind_hash != NULL) { 23260 tcpnext->tcp_bind_hash->tcp_ptpbhn = 23261 &(tcpnext->tcp_bind_hash); 23262 tcp->tcp_bind_hash = NULL; 23263 } 23264 } else if ((tcpnext = tcp->tcp_bind_hash) != NULL) { 23265 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23266 tcp->tcp_bind_hash = NULL; 23267 } 23268 *tcp->tcp_ptpbhn = tcpnext; 23269 tcp->tcp_ptpbhn = NULL; 23270 } 23271 mutex_exit(lockp); 23272 } 23273 23274 23275 /* 23276 * Hash list lookup routine for tcp_t structures. 23277 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23278 */ 23279 static tcp_t * 23280 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 23281 { 23282 tf_t *tf; 23283 tcp_t *tcp; 23284 23285 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23286 mutex_enter(&tf->tf_lock); 23287 for (tcp = tf->tf_tcp; tcp != NULL; 23288 tcp = tcp->tcp_acceptor_hash) { 23289 if (tcp->tcp_acceptor_id == id) { 23290 CONN_INC_REF(tcp->tcp_connp); 23291 mutex_exit(&tf->tf_lock); 23292 return (tcp); 23293 } 23294 } 23295 mutex_exit(&tf->tf_lock); 23296 return (NULL); 23297 } 23298 23299 23300 /* 23301 * Hash list insertion routine for tcp_t structures. 23302 */ 23303 void 23304 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 23305 { 23306 tf_t *tf; 23307 tcp_t **tcpp; 23308 tcp_t *tcpnext; 23309 tcp_stack_t *tcps = tcp->tcp_tcps; 23310 23311 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23312 23313 if (tcp->tcp_ptpahn != NULL) 23314 tcp_acceptor_hash_remove(tcp); 23315 tcpp = &tf->tf_tcp; 23316 mutex_enter(&tf->tf_lock); 23317 tcpnext = tcpp[0]; 23318 if (tcpnext) 23319 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 23320 tcp->tcp_acceptor_hash = tcpnext; 23321 tcp->tcp_ptpahn = tcpp; 23322 tcpp[0] = tcp; 23323 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 23324 mutex_exit(&tf->tf_lock); 23325 } 23326 23327 /* 23328 * Hash list removal routine for tcp_t structures. 23329 */ 23330 static void 23331 tcp_acceptor_hash_remove(tcp_t *tcp) 23332 { 23333 tcp_t *tcpnext; 23334 kmutex_t *lockp; 23335 23336 /* 23337 * Extract the lock pointer in case there are concurrent 23338 * hash_remove's for this instance. 23339 */ 23340 lockp = tcp->tcp_acceptor_lockp; 23341 23342 if (tcp->tcp_ptpahn == NULL) 23343 return; 23344 23345 ASSERT(lockp != NULL); 23346 mutex_enter(lockp); 23347 if (tcp->tcp_ptpahn) { 23348 tcpnext = tcp->tcp_acceptor_hash; 23349 if (tcpnext) { 23350 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 23351 tcp->tcp_acceptor_hash = NULL; 23352 } 23353 *tcp->tcp_ptpahn = tcpnext; 23354 tcp->tcp_ptpahn = NULL; 23355 } 23356 mutex_exit(lockp); 23357 tcp->tcp_acceptor_lockp = NULL; 23358 } 23359 23360 /* 23361 * Type three generator adapted from the random() function in 4.4 BSD: 23362 */ 23363 23364 /* 23365 * Copyright (c) 1983, 1993 23366 * The Regents of the University of California. All rights reserved. 23367 * 23368 * Redistribution and use in source and binary forms, with or without 23369 * modification, are permitted provided that the following conditions 23370 * are met: 23371 * 1. Redistributions of source code must retain the above copyright 23372 * notice, this list of conditions and the following disclaimer. 23373 * 2. Redistributions in binary form must reproduce the above copyright 23374 * notice, this list of conditions and the following disclaimer in the 23375 * documentation and/or other materials provided with the distribution. 23376 * 3. All advertising materials mentioning features or use of this software 23377 * must display the following acknowledgement: 23378 * This product includes software developed by the University of 23379 * California, Berkeley and its contributors. 23380 * 4. Neither the name of the University nor the names of its contributors 23381 * may be used to endorse or promote products derived from this software 23382 * without specific prior written permission. 23383 * 23384 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23385 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23386 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23387 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23388 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23389 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23390 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23391 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23392 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23393 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23394 * SUCH DAMAGE. 23395 */ 23396 23397 /* Type 3 -- x**31 + x**3 + 1 */ 23398 #define DEG_3 31 23399 #define SEP_3 3 23400 23401 23402 /* Protected by tcp_random_lock */ 23403 static int tcp_randtbl[DEG_3 + 1]; 23404 23405 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 23406 static int *tcp_random_rptr = &tcp_randtbl[1]; 23407 23408 static int *tcp_random_state = &tcp_randtbl[1]; 23409 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 23410 23411 kmutex_t tcp_random_lock; 23412 23413 void 23414 tcp_random_init(void) 23415 { 23416 int i; 23417 hrtime_t hrt; 23418 time_t wallclock; 23419 uint64_t result; 23420 23421 /* 23422 * Use high-res timer and current time for seed. Gethrtime() returns 23423 * a longlong, which may contain resolution down to nanoseconds. 23424 * The current time will either be a 32-bit or a 64-bit quantity. 23425 * XOR the two together in a 64-bit result variable. 23426 * Convert the result to a 32-bit value by multiplying the high-order 23427 * 32-bits by the low-order 32-bits. 23428 */ 23429 23430 hrt = gethrtime(); 23431 (void) drv_getparm(TIME, &wallclock); 23432 result = (uint64_t)wallclock ^ (uint64_t)hrt; 23433 mutex_enter(&tcp_random_lock); 23434 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 23435 (result & 0xffffffff); 23436 23437 for (i = 1; i < DEG_3; i++) 23438 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 23439 + 12345; 23440 tcp_random_fptr = &tcp_random_state[SEP_3]; 23441 tcp_random_rptr = &tcp_random_state[0]; 23442 mutex_exit(&tcp_random_lock); 23443 for (i = 0; i < 10 * DEG_3; i++) 23444 (void) tcp_random(); 23445 } 23446 23447 /* 23448 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 23449 * This range is selected to be approximately centered on TCP_ISS / 2, 23450 * and easy to compute. We get this value by generating a 32-bit random 23451 * number, selecting out the high-order 17 bits, and then adding one so 23452 * that we never return zero. 23453 */ 23454 int 23455 tcp_random(void) 23456 { 23457 int i; 23458 23459 mutex_enter(&tcp_random_lock); 23460 *tcp_random_fptr += *tcp_random_rptr; 23461 23462 /* 23463 * The high-order bits are more random than the low-order bits, 23464 * so we select out the high-order 17 bits and add one so that 23465 * we never return zero. 23466 */ 23467 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 23468 if (++tcp_random_fptr >= tcp_random_end_ptr) { 23469 tcp_random_fptr = tcp_random_state; 23470 ++tcp_random_rptr; 23471 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 23472 tcp_random_rptr = tcp_random_state; 23473 23474 mutex_exit(&tcp_random_lock); 23475 return (i); 23476 } 23477 23478 static int 23479 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 23480 int *t_errorp, int *sys_errorp) 23481 { 23482 int error; 23483 int is_absreq_failure; 23484 t_scalar_t *opt_lenp; 23485 t_scalar_t opt_offset; 23486 int prim_type; 23487 struct T_conn_req *tcreqp; 23488 struct T_conn_res *tcresp; 23489 cred_t *cr; 23490 23491 /* 23492 * All Solaris components should pass a db_credp 23493 * for this TPI message, hence we ASSERT. 23494 * But in case there is some other M_PROTO that looks 23495 * like a TPI message sent by some other kernel 23496 * component, we check and return an error. 23497 */ 23498 cr = msg_getcred(mp, NULL); 23499 ASSERT(cr != NULL); 23500 if (cr == NULL) 23501 return (-1); 23502 23503 prim_type = ((union T_primitives *)mp->b_rptr)->type; 23504 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 23505 prim_type == T_CONN_RES); 23506 23507 switch (prim_type) { 23508 case T_CONN_REQ: 23509 tcreqp = (struct T_conn_req *)mp->b_rptr; 23510 opt_offset = tcreqp->OPT_offset; 23511 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 23512 break; 23513 case O_T_CONN_RES: 23514 case T_CONN_RES: 23515 tcresp = (struct T_conn_res *)mp->b_rptr; 23516 opt_offset = tcresp->OPT_offset; 23517 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 23518 break; 23519 } 23520 23521 *t_errorp = 0; 23522 *sys_errorp = 0; 23523 *do_disconnectp = 0; 23524 23525 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 23526 opt_offset, cr, &tcp_opt_obj, 23527 NULL, &is_absreq_failure); 23528 23529 switch (error) { 23530 case 0: /* no error */ 23531 ASSERT(is_absreq_failure == 0); 23532 return (0); 23533 case ENOPROTOOPT: 23534 *t_errorp = TBADOPT; 23535 break; 23536 case EACCES: 23537 *t_errorp = TACCES; 23538 break; 23539 default: 23540 *t_errorp = TSYSERR; *sys_errorp = error; 23541 break; 23542 } 23543 if (is_absreq_failure != 0) { 23544 /* 23545 * The connection request should get the local ack 23546 * T_OK_ACK and then a T_DISCON_IND. 23547 */ 23548 *do_disconnectp = 1; 23549 } 23550 return (-1); 23551 } 23552 23553 /* 23554 * Split this function out so that if the secret changes, I'm okay. 23555 * 23556 * Initialize the tcp_iss_cookie and tcp_iss_key. 23557 */ 23558 23559 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 23560 23561 static void 23562 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 23563 { 23564 struct { 23565 int32_t current_time; 23566 uint32_t randnum; 23567 uint16_t pad; 23568 uint8_t ether[6]; 23569 uint8_t passwd[PASSWD_SIZE]; 23570 } tcp_iss_cookie; 23571 time_t t; 23572 23573 /* 23574 * Start with the current absolute time. 23575 */ 23576 (void) drv_getparm(TIME, &t); 23577 tcp_iss_cookie.current_time = t; 23578 23579 /* 23580 * XXX - Need a more random number per RFC 1750, not this crap. 23581 * OTOH, if what follows is pretty random, then I'm in better shape. 23582 */ 23583 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 23584 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 23585 23586 /* 23587 * The cpu_type_info is pretty non-random. Ugggh. It does serve 23588 * as a good template. 23589 */ 23590 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 23591 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 23592 23593 /* 23594 * The pass-phrase. Normally this is supplied by user-called NDD. 23595 */ 23596 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 23597 23598 /* 23599 * See 4010593 if this section becomes a problem again, 23600 * but the local ethernet address is useful here. 23601 */ 23602 (void) localetheraddr(NULL, 23603 (struct ether_addr *)&tcp_iss_cookie.ether); 23604 23605 /* 23606 * Hash 'em all together. The MD5Final is called per-connection. 23607 */ 23608 mutex_enter(&tcps->tcps_iss_key_lock); 23609 MD5Init(&tcps->tcps_iss_key); 23610 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 23611 sizeof (tcp_iss_cookie)); 23612 mutex_exit(&tcps->tcps_iss_key_lock); 23613 } 23614 23615 /* 23616 * Set the RFC 1948 pass phrase 23617 */ 23618 /* ARGSUSED */ 23619 static int 23620 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 23621 cred_t *cr) 23622 { 23623 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 23624 23625 /* 23626 * Basically, value contains a new pass phrase. Pass it along! 23627 */ 23628 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 23629 return (0); 23630 } 23631 23632 /* ARGSUSED */ 23633 static int 23634 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 23635 { 23636 bzero(buf, sizeof (tcp_sack_info_t)); 23637 return (0); 23638 } 23639 23640 /* ARGSUSED */ 23641 static int 23642 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 23643 { 23644 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 23645 return (0); 23646 } 23647 23648 /* 23649 * Make sure we wait until the default queue is setup, yet allow 23650 * tcp_g_q_create() to open a TCP stream. 23651 * We need to allow tcp_g_q_create() do do an open 23652 * of tcp, hence we compare curhread. 23653 * All others have to wait until the tcps_g_q has been 23654 * setup. 23655 */ 23656 void 23657 tcp_g_q_setup(tcp_stack_t *tcps) 23658 { 23659 mutex_enter(&tcps->tcps_g_q_lock); 23660 if (tcps->tcps_g_q != NULL) { 23661 mutex_exit(&tcps->tcps_g_q_lock); 23662 return; 23663 } 23664 if (tcps->tcps_g_q_creator == NULL) { 23665 /* This thread will set it up */ 23666 tcps->tcps_g_q_creator = curthread; 23667 mutex_exit(&tcps->tcps_g_q_lock); 23668 tcp_g_q_create(tcps); 23669 mutex_enter(&tcps->tcps_g_q_lock); 23670 ASSERT(tcps->tcps_g_q_creator == curthread); 23671 tcps->tcps_g_q_creator = NULL; 23672 cv_signal(&tcps->tcps_g_q_cv); 23673 ASSERT(tcps->tcps_g_q != NULL); 23674 mutex_exit(&tcps->tcps_g_q_lock); 23675 return; 23676 } 23677 /* Everybody but the creator has to wait */ 23678 if (tcps->tcps_g_q_creator != curthread) { 23679 while (tcps->tcps_g_q == NULL) 23680 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 23681 } 23682 mutex_exit(&tcps->tcps_g_q_lock); 23683 } 23684 23685 #define IP "ip" 23686 23687 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 23688 23689 /* 23690 * Create a default tcp queue here instead of in strplumb 23691 */ 23692 void 23693 tcp_g_q_create(tcp_stack_t *tcps) 23694 { 23695 int error; 23696 ldi_handle_t lh = NULL; 23697 ldi_ident_t li = NULL; 23698 int rval; 23699 cred_t *cr; 23700 major_t IP_MAJ; 23701 23702 #ifdef NS_DEBUG 23703 (void) printf("tcp_g_q_create()\n"); 23704 #endif 23705 23706 IP_MAJ = ddi_name_to_major(IP); 23707 23708 ASSERT(tcps->tcps_g_q_creator == curthread); 23709 23710 error = ldi_ident_from_major(IP_MAJ, &li); 23711 if (error) { 23712 #ifdef DEBUG 23713 printf("tcp_g_q_create: lyr ident get failed error %d\n", 23714 error); 23715 #endif 23716 return; 23717 } 23718 23719 cr = zone_get_kcred(netstackid_to_zoneid( 23720 tcps->tcps_netstack->netstack_stackid)); 23721 ASSERT(cr != NULL); 23722 /* 23723 * We set the tcp default queue to IPv6 because IPv4 falls 23724 * back to IPv6 when it can't find a client, but 23725 * IPv6 does not fall back to IPv4. 23726 */ 23727 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 23728 if (error) { 23729 #ifdef DEBUG 23730 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 23731 error); 23732 #endif 23733 goto out; 23734 } 23735 23736 /* 23737 * This ioctl causes the tcp framework to cache a pointer to 23738 * this stream, so we don't want to close the stream after 23739 * this operation. 23740 * Use the kernel credentials that are for the zone we're in. 23741 */ 23742 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 23743 (intptr_t)0, FKIOCTL, cr, &rval); 23744 if (error) { 23745 #ifdef DEBUG 23746 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 23747 "error %d\n", error); 23748 #endif 23749 goto out; 23750 } 23751 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 23752 lh = NULL; 23753 out: 23754 /* Close layered handles */ 23755 if (li) 23756 ldi_ident_release(li); 23757 /* Keep cred around until _inactive needs it */ 23758 tcps->tcps_g_q_cr = cr; 23759 } 23760 23761 /* 23762 * We keep tcp_g_q set until all other tcp_t's in the zone 23763 * has gone away, and then when tcp_g_q_inactive() is called 23764 * we clear it. 23765 */ 23766 void 23767 tcp_g_q_destroy(tcp_stack_t *tcps) 23768 { 23769 #ifdef NS_DEBUG 23770 (void) printf("tcp_g_q_destroy()for stack %d\n", 23771 tcps->tcps_netstack->netstack_stackid); 23772 #endif 23773 23774 if (tcps->tcps_g_q == NULL) { 23775 return; /* Nothing to cleanup */ 23776 } 23777 /* 23778 * Drop reference corresponding to the default queue. 23779 * This reference was added from tcp_open when the default queue 23780 * was created, hence we compensate for this extra drop in 23781 * tcp_g_q_close. If the refcnt drops to zero here it means 23782 * the default queue was the last one to be open, in which 23783 * case, then tcp_g_q_inactive will be 23784 * called as a result of the refrele. 23785 */ 23786 TCPS_REFRELE(tcps); 23787 } 23788 23789 /* 23790 * Called when last tcp_t drops reference count using TCPS_REFRELE. 23791 * Run by tcp_q_q_inactive using a taskq. 23792 */ 23793 static void 23794 tcp_g_q_close(void *arg) 23795 { 23796 tcp_stack_t *tcps = arg; 23797 int error; 23798 ldi_handle_t lh = NULL; 23799 ldi_ident_t li = NULL; 23800 cred_t *cr; 23801 major_t IP_MAJ; 23802 23803 IP_MAJ = ddi_name_to_major(IP); 23804 23805 #ifdef NS_DEBUG 23806 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 23807 tcps->tcps_netstack->netstack_stackid, 23808 tcps->tcps_netstack->netstack_refcnt); 23809 #endif 23810 lh = tcps->tcps_g_q_lh; 23811 if (lh == NULL) 23812 return; /* Nothing to cleanup */ 23813 23814 ASSERT(tcps->tcps_refcnt == 1); 23815 ASSERT(tcps->tcps_g_q != NULL); 23816 23817 error = ldi_ident_from_major(IP_MAJ, &li); 23818 if (error) { 23819 #ifdef DEBUG 23820 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 23821 error); 23822 #endif 23823 return; 23824 } 23825 23826 cr = tcps->tcps_g_q_cr; 23827 tcps->tcps_g_q_cr = NULL; 23828 ASSERT(cr != NULL); 23829 23830 /* 23831 * Make sure we can break the recursion when tcp_close decrements 23832 * the reference count causing g_q_inactive to be called again. 23833 */ 23834 tcps->tcps_g_q_lh = NULL; 23835 23836 /* close the default queue */ 23837 (void) ldi_close(lh, FREAD|FWRITE, cr); 23838 /* 23839 * At this point in time tcps and the rest of netstack_t might 23840 * have been deleted. 23841 */ 23842 tcps = NULL; 23843 23844 /* Close layered handles */ 23845 ldi_ident_release(li); 23846 crfree(cr); 23847 } 23848 23849 /* 23850 * Called when last tcp_t drops reference count using TCPS_REFRELE. 23851 * 23852 * Have to ensure that the ldi routines are not used by an 23853 * interrupt thread by using a taskq. 23854 */ 23855 void 23856 tcp_g_q_inactive(tcp_stack_t *tcps) 23857 { 23858 if (tcps->tcps_g_q_lh == NULL) 23859 return; /* Nothing to cleanup */ 23860 23861 ASSERT(tcps->tcps_refcnt == 0); 23862 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 23863 23864 if (servicing_interrupt()) { 23865 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 23866 (void *) tcps, TQ_SLEEP); 23867 } else { 23868 tcp_g_q_close(tcps); 23869 } 23870 } 23871 23872 /* 23873 * Called by IP when IP is loaded into the kernel 23874 */ 23875 void 23876 tcp_ddi_g_init(void) 23877 { 23878 tcp_timercache = kmem_cache_create("tcp_timercache", 23879 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 23880 NULL, NULL, NULL, NULL, NULL, 0); 23881 23882 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 23883 sizeof (tcp_sack_info_t), 0, 23884 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 23885 23886 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 23887 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 23888 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 23889 23890 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 23891 23892 /* Initialize the random number generator */ 23893 tcp_random_init(); 23894 23895 /* A single callback independently of how many netstacks we have */ 23896 ip_squeue_init(tcp_squeue_add); 23897 23898 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 23899 23900 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 23901 TASKQ_PREPOPULATE); 23902 23903 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput); 23904 23905 /* 23906 * We want to be informed each time a stack is created or 23907 * destroyed in the kernel, so we can maintain the 23908 * set of tcp_stack_t's. 23909 */ 23910 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 23911 tcp_stack_fini); 23912 } 23913 23914 23915 #define INET_NAME "ip" 23916 23917 /* 23918 * Initialize the TCP stack instance. 23919 */ 23920 static void * 23921 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 23922 { 23923 tcp_stack_t *tcps; 23924 tcpparam_t *pa; 23925 int i; 23926 int error = 0; 23927 major_t major; 23928 23929 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 23930 tcps->tcps_netstack = ns; 23931 23932 /* Initialize locks */ 23933 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 23934 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 23935 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 23936 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 23937 23938 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 23939 tcps->tcps_g_epriv_ports[0] = 2049; 23940 tcps->tcps_g_epriv_ports[1] = 4045; 23941 tcps->tcps_min_anonpriv_port = 512; 23942 23943 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 23944 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 23945 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 23946 TCP_FANOUT_SIZE, KM_SLEEP); 23947 23948 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 23949 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 23950 MUTEX_DEFAULT, NULL); 23951 } 23952 23953 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 23954 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 23955 MUTEX_DEFAULT, NULL); 23956 } 23957 23958 /* TCP's IPsec code calls the packet dropper. */ 23959 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 23960 23961 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 23962 tcps->tcps_params = pa; 23963 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 23964 23965 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 23966 A_CNT(lcl_tcp_param_arr), tcps); 23967 23968 /* 23969 * Note: To really walk the device tree you need the devinfo 23970 * pointer to your device which is only available after probe/attach. 23971 * The following is safe only because it uses ddi_root_node() 23972 */ 23973 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 23974 tcp_opt_obj.odb_opt_arr_cnt); 23975 23976 /* 23977 * Initialize RFC 1948 secret values. This will probably be reset once 23978 * by the boot scripts. 23979 * 23980 * Use NULL name, as the name is caught by the new lockstats. 23981 * 23982 * Initialize with some random, non-guessable string, like the global 23983 * T_INFO_ACK. 23984 */ 23985 23986 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 23987 sizeof (tcp_g_t_info_ack), tcps); 23988 23989 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 23990 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 23991 23992 major = mod_name_to_major(INET_NAME); 23993 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident); 23994 ASSERT(error == 0); 23995 return (tcps); 23996 } 23997 23998 /* 23999 * Called when the IP module is about to be unloaded. 24000 */ 24001 void 24002 tcp_ddi_g_destroy(void) 24003 { 24004 tcp_g_kstat_fini(tcp_g_kstat); 24005 tcp_g_kstat = NULL; 24006 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 24007 24008 mutex_destroy(&tcp_random_lock); 24009 24010 kmem_cache_destroy(tcp_timercache); 24011 kmem_cache_destroy(tcp_sack_info_cache); 24012 kmem_cache_destroy(tcp_iphc_cache); 24013 24014 netstack_unregister(NS_TCP); 24015 taskq_destroy(tcp_taskq); 24016 } 24017 24018 /* 24019 * Shut down the TCP stack instance. 24020 */ 24021 /* ARGSUSED */ 24022 static void 24023 tcp_stack_shutdown(netstackid_t stackid, void *arg) 24024 { 24025 tcp_stack_t *tcps = (tcp_stack_t *)arg; 24026 24027 tcp_g_q_destroy(tcps); 24028 } 24029 24030 /* 24031 * Free the TCP stack instance. 24032 */ 24033 static void 24034 tcp_stack_fini(netstackid_t stackid, void *arg) 24035 { 24036 tcp_stack_t *tcps = (tcp_stack_t *)arg; 24037 int i; 24038 24039 nd_free(&tcps->tcps_g_nd); 24040 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24041 tcps->tcps_params = NULL; 24042 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 24043 tcps->tcps_wroff_xtra_param = NULL; 24044 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 24045 tcps->tcps_mdt_head_param = NULL; 24046 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 24047 tcps->tcps_mdt_tail_param = NULL; 24048 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 24049 tcps->tcps_mdt_max_pbufs_param = NULL; 24050 24051 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24052 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 24053 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 24054 } 24055 24056 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24057 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 24058 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 24059 } 24060 24061 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 24062 tcps->tcps_bind_fanout = NULL; 24063 24064 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 24065 tcps->tcps_acceptor_fanout = NULL; 24066 24067 mutex_destroy(&tcps->tcps_iss_key_lock); 24068 mutex_destroy(&tcps->tcps_g_q_lock); 24069 cv_destroy(&tcps->tcps_g_q_cv); 24070 mutex_destroy(&tcps->tcps_epriv_port_lock); 24071 24072 ip_drop_unregister(&tcps->tcps_dropper); 24073 24074 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 24075 tcps->tcps_kstat = NULL; 24076 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 24077 24078 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 24079 tcps->tcps_mibkp = NULL; 24080 24081 ldi_ident_release(tcps->tcps_ldi_ident); 24082 kmem_free(tcps, sizeof (*tcps)); 24083 } 24084 24085 /* 24086 * Generate ISS, taking into account NDD changes may happen halfway through. 24087 * (If the iss is not zero, set it.) 24088 */ 24089 24090 static void 24091 tcp_iss_init(tcp_t *tcp) 24092 { 24093 MD5_CTX context; 24094 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 24095 uint32_t answer[4]; 24096 tcp_stack_t *tcps = tcp->tcp_tcps; 24097 24098 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 24099 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 24100 switch (tcps->tcps_strong_iss) { 24101 case 2: 24102 mutex_enter(&tcps->tcps_iss_key_lock); 24103 context = tcps->tcps_iss_key; 24104 mutex_exit(&tcps->tcps_iss_key_lock); 24105 arg.ports = tcp->tcp_ports; 24106 if (tcp->tcp_ipversion == IPV4_VERSION) { 24107 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 24108 &arg.src); 24109 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 24110 &arg.dst); 24111 } else { 24112 arg.src = tcp->tcp_ip6h->ip6_src; 24113 arg.dst = tcp->tcp_ip6h->ip6_dst; 24114 } 24115 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 24116 MD5Final((uchar_t *)answer, &context); 24117 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 24118 /* 24119 * Now that we've hashed into a unique per-connection sequence 24120 * space, add a random increment per strong_iss == 1. So I 24121 * guess we'll have to... 24122 */ 24123 /* FALLTHRU */ 24124 case 1: 24125 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 24126 break; 24127 default: 24128 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24129 break; 24130 } 24131 tcp->tcp_valid_bits = TCP_ISS_VALID; 24132 tcp->tcp_fss = tcp->tcp_iss - 1; 24133 tcp->tcp_suna = tcp->tcp_iss; 24134 tcp->tcp_snxt = tcp->tcp_iss + 1; 24135 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 24136 tcp->tcp_csuna = tcp->tcp_snxt; 24137 } 24138 24139 /* 24140 * Exported routine for extracting active tcp connection status. 24141 * 24142 * This is used by the Solaris Cluster Networking software to 24143 * gather a list of connections that need to be forwarded to 24144 * specific nodes in the cluster when configuration changes occur. 24145 * 24146 * The callback is invoked for each tcp_t structure from all netstacks, 24147 * if 'stack_id' is less than 0. Otherwise, only for tcp_t structures 24148 * from the netstack with the specified stack_id. Returning 24149 * non-zero from the callback routine terminates the search. 24150 */ 24151 int 24152 cl_tcp_walk_list(netstackid_t stack_id, 24153 int (*cl_callback)(cl_tcp_info_t *, void *), void *arg) 24154 { 24155 netstack_handle_t nh; 24156 netstack_t *ns; 24157 int ret = 0; 24158 24159 if (stack_id >= 0) { 24160 if ((ns = netstack_find_by_stackid(stack_id)) == NULL) 24161 return (EINVAL); 24162 24163 ret = cl_tcp_walk_list_stack(cl_callback, arg, 24164 ns->netstack_tcp); 24165 netstack_rele(ns); 24166 return (ret); 24167 } 24168 24169 netstack_next_init(&nh); 24170 while ((ns = netstack_next(&nh)) != NULL) { 24171 ret = cl_tcp_walk_list_stack(cl_callback, arg, 24172 ns->netstack_tcp); 24173 netstack_rele(ns); 24174 } 24175 netstack_next_fini(&nh); 24176 return (ret); 24177 } 24178 24179 static int 24180 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 24181 tcp_stack_t *tcps) 24182 { 24183 tcp_t *tcp; 24184 cl_tcp_info_t cl_tcpi; 24185 connf_t *connfp; 24186 conn_t *connp; 24187 int i; 24188 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24189 24190 ASSERT(callback != NULL); 24191 24192 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 24193 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 24194 connp = NULL; 24195 24196 while ((connp = 24197 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 24198 24199 tcp = connp->conn_tcp; 24200 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 24201 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 24202 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 24203 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 24204 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 24205 /* 24206 * The macros tcp_laddr and tcp_faddr give the IPv4 24207 * addresses. They are copied implicitly below as 24208 * mapped addresses. 24209 */ 24210 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 24211 if (tcp->tcp_ipversion == IPV4_VERSION) { 24212 cl_tcpi.cl_tcpi_faddr = 24213 tcp->tcp_ipha->ipha_dst; 24214 } else { 24215 cl_tcpi.cl_tcpi_faddr_v6 = 24216 tcp->tcp_ip6h->ip6_dst; 24217 } 24218 24219 /* 24220 * If the callback returns non-zero 24221 * we terminate the traversal. 24222 */ 24223 if ((*callback)(&cl_tcpi, arg) != 0) { 24224 CONN_DEC_REF(tcp->tcp_connp); 24225 return (1); 24226 } 24227 } 24228 } 24229 24230 return (0); 24231 } 24232 24233 /* 24234 * Macros used for accessing the different types of sockaddr 24235 * structures inside a tcp_ioc_abort_conn_t. 24236 */ 24237 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 24238 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 24239 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 24240 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 24241 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 24242 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 24243 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 24244 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 24245 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 24246 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 24247 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 24248 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 24249 24250 /* 24251 * Return the correct error code to mimic the behavior 24252 * of a connection reset. 24253 */ 24254 #define TCP_AC_GET_ERRCODE(state, err) { \ 24255 switch ((state)) { \ 24256 case TCPS_SYN_SENT: \ 24257 case TCPS_SYN_RCVD: \ 24258 (err) = ECONNREFUSED; \ 24259 break; \ 24260 case TCPS_ESTABLISHED: \ 24261 case TCPS_FIN_WAIT_1: \ 24262 case TCPS_FIN_WAIT_2: \ 24263 case TCPS_CLOSE_WAIT: \ 24264 (err) = ECONNRESET; \ 24265 break; \ 24266 case TCPS_CLOSING: \ 24267 case TCPS_LAST_ACK: \ 24268 case TCPS_TIME_WAIT: \ 24269 (err) = 0; \ 24270 break; \ 24271 default: \ 24272 (err) = ENXIO; \ 24273 } \ 24274 } 24275 24276 /* 24277 * Check if a tcp structure matches the info in acp. 24278 */ 24279 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 24280 (((acp)->ac_local.ss_family == AF_INET) ? \ 24281 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 24282 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 24283 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 24284 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 24285 (TCP_AC_V4LPORT((acp)) == 0 || \ 24286 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 24287 (TCP_AC_V4RPORT((acp)) == 0 || \ 24288 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 24289 (acp)->ac_start <= (tcp)->tcp_state && \ 24290 (acp)->ac_end >= (tcp)->tcp_state) : \ 24291 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 24292 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 24293 &(tcp)->tcp_ip_src_v6)) && \ 24294 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 24295 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 24296 &(tcp)->tcp_remote_v6)) && \ 24297 (TCP_AC_V6LPORT((acp)) == 0 || \ 24298 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 24299 (TCP_AC_V6RPORT((acp)) == 0 || \ 24300 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 24301 (acp)->ac_start <= (tcp)->tcp_state && \ 24302 (acp)->ac_end >= (tcp)->tcp_state)) 24303 24304 #define TCP_AC_MATCH(acp, tcp) \ 24305 (((acp)->ac_zoneid == ALL_ZONES || \ 24306 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 24307 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 24308 24309 /* 24310 * Build a message containing a tcp_ioc_abort_conn_t structure 24311 * which is filled in with information from acp and tp. 24312 */ 24313 static mblk_t * 24314 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 24315 { 24316 mblk_t *mp; 24317 tcp_ioc_abort_conn_t *tacp; 24318 24319 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 24320 if (mp == NULL) 24321 return (NULL); 24322 24323 mp->b_datap->db_type = M_CTL; 24324 24325 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 24326 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 24327 sizeof (uint32_t)); 24328 24329 tacp->ac_start = acp->ac_start; 24330 tacp->ac_end = acp->ac_end; 24331 tacp->ac_zoneid = acp->ac_zoneid; 24332 24333 if (acp->ac_local.ss_family == AF_INET) { 24334 tacp->ac_local.ss_family = AF_INET; 24335 tacp->ac_remote.ss_family = AF_INET; 24336 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 24337 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 24338 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 24339 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 24340 } else { 24341 tacp->ac_local.ss_family = AF_INET6; 24342 tacp->ac_remote.ss_family = AF_INET6; 24343 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 24344 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 24345 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 24346 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 24347 } 24348 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 24349 return (mp); 24350 } 24351 24352 /* 24353 * Print a tcp_ioc_abort_conn_t structure. 24354 */ 24355 static void 24356 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 24357 { 24358 char lbuf[128]; 24359 char rbuf[128]; 24360 sa_family_t af; 24361 in_port_t lport, rport; 24362 ushort_t logflags; 24363 24364 af = acp->ac_local.ss_family; 24365 24366 if (af == AF_INET) { 24367 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 24368 lbuf, 128); 24369 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 24370 rbuf, 128); 24371 lport = ntohs(TCP_AC_V4LPORT(acp)); 24372 rport = ntohs(TCP_AC_V4RPORT(acp)); 24373 } else { 24374 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 24375 lbuf, 128); 24376 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 24377 rbuf, 128); 24378 lport = ntohs(TCP_AC_V6LPORT(acp)); 24379 rport = ntohs(TCP_AC_V6RPORT(acp)); 24380 } 24381 24382 logflags = SL_TRACE | SL_NOTE; 24383 /* 24384 * Don't print this message to the console if the operation was done 24385 * to a non-global zone. 24386 */ 24387 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24388 logflags |= SL_CONSOLE; 24389 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 24390 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 24391 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 24392 acp->ac_start, acp->ac_end); 24393 } 24394 24395 /* 24396 * Called inside tcp_rput when a message built using 24397 * tcp_ioctl_abort_build_msg is put into a queue. 24398 * Note that when we get here there is no wildcard in acp any more. 24399 */ 24400 static void 24401 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 24402 { 24403 tcp_ioc_abort_conn_t *acp; 24404 24405 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 24406 if (tcp->tcp_state <= acp->ac_end) { 24407 /* 24408 * If we get here, we are already on the correct 24409 * squeue. This ioctl follows the following path 24410 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 24411 * ->tcp_ioctl_abort->squeue_enter (if on a 24412 * different squeue) 24413 */ 24414 int errcode; 24415 24416 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 24417 (void) tcp_clean_death(tcp, errcode, 26); 24418 } 24419 freemsg(mp); 24420 } 24421 24422 /* 24423 * Abort all matching connections on a hash chain. 24424 */ 24425 static int 24426 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 24427 boolean_t exact, tcp_stack_t *tcps) 24428 { 24429 int nmatch, err = 0; 24430 tcp_t *tcp; 24431 MBLKP mp, last, listhead = NULL; 24432 conn_t *tconnp; 24433 connf_t *connfp; 24434 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24435 24436 connfp = &ipst->ips_ipcl_conn_fanout[index]; 24437 24438 startover: 24439 nmatch = 0; 24440 24441 mutex_enter(&connfp->connf_lock); 24442 for (tconnp = connfp->connf_head; tconnp != NULL; 24443 tconnp = tconnp->conn_next) { 24444 tcp = tconnp->conn_tcp; 24445 if (TCP_AC_MATCH(acp, tcp)) { 24446 CONN_INC_REF(tcp->tcp_connp); 24447 mp = tcp_ioctl_abort_build_msg(acp, tcp); 24448 if (mp == NULL) { 24449 err = ENOMEM; 24450 CONN_DEC_REF(tcp->tcp_connp); 24451 break; 24452 } 24453 mp->b_prev = (mblk_t *)tcp; 24454 24455 if (listhead == NULL) { 24456 listhead = mp; 24457 last = mp; 24458 } else { 24459 last->b_next = mp; 24460 last = mp; 24461 } 24462 nmatch++; 24463 if (exact) 24464 break; 24465 } 24466 24467 /* Avoid holding lock for too long. */ 24468 if (nmatch >= 500) 24469 break; 24470 } 24471 mutex_exit(&connfp->connf_lock); 24472 24473 /* Pass mp into the correct tcp */ 24474 while ((mp = listhead) != NULL) { 24475 listhead = listhead->b_next; 24476 tcp = (tcp_t *)mp->b_prev; 24477 mp->b_next = mp->b_prev = NULL; 24478 SQUEUE_ENTER_ONE(tcp->tcp_connp->conn_sqp, mp, tcp_input, 24479 tcp->tcp_connp, SQ_FILL, SQTAG_TCP_ABORT_BUCKET); 24480 } 24481 24482 *count += nmatch; 24483 if (nmatch >= 500 && err == 0) 24484 goto startover; 24485 return (err); 24486 } 24487 24488 /* 24489 * Abort all connections that matches the attributes specified in acp. 24490 */ 24491 static int 24492 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 24493 { 24494 sa_family_t af; 24495 uint32_t ports; 24496 uint16_t *pports; 24497 int err = 0, count = 0; 24498 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 24499 int index = -1; 24500 ushort_t logflags; 24501 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24502 24503 af = acp->ac_local.ss_family; 24504 24505 if (af == AF_INET) { 24506 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 24507 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 24508 pports = (uint16_t *)&ports; 24509 pports[1] = TCP_AC_V4LPORT(acp); 24510 pports[0] = TCP_AC_V4RPORT(acp); 24511 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 24512 } 24513 } else { 24514 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 24515 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 24516 pports = (uint16_t *)&ports; 24517 pports[1] = TCP_AC_V6LPORT(acp); 24518 pports[0] = TCP_AC_V6RPORT(acp); 24519 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 24520 } 24521 } 24522 24523 /* 24524 * For cases where remote addr, local port, and remote port are non- 24525 * wildcards, tcp_ioctl_abort_bucket will only be called once. 24526 */ 24527 if (index != -1) { 24528 err = tcp_ioctl_abort_bucket(acp, index, 24529 &count, exact, tcps); 24530 } else { 24531 /* 24532 * loop through all entries for wildcard case 24533 */ 24534 for (index = 0; 24535 index < ipst->ips_ipcl_conn_fanout_size; 24536 index++) { 24537 err = tcp_ioctl_abort_bucket(acp, index, 24538 &count, exact, tcps); 24539 if (err != 0) 24540 break; 24541 } 24542 } 24543 24544 logflags = SL_TRACE | SL_NOTE; 24545 /* 24546 * Don't print this message to the console if the operation was done 24547 * to a non-global zone. 24548 */ 24549 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24550 logflags |= SL_CONSOLE; 24551 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 24552 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 24553 if (err == 0 && count == 0) 24554 err = ENOENT; 24555 return (err); 24556 } 24557 24558 /* 24559 * Process the TCP_IOC_ABORT_CONN ioctl request. 24560 */ 24561 static void 24562 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 24563 { 24564 int err; 24565 IOCP iocp; 24566 MBLKP mp1; 24567 sa_family_t laf, raf; 24568 tcp_ioc_abort_conn_t *acp; 24569 zone_t *zptr; 24570 conn_t *connp = Q_TO_CONN(q); 24571 zoneid_t zoneid = connp->conn_zoneid; 24572 tcp_t *tcp = connp->conn_tcp; 24573 tcp_stack_t *tcps = tcp->tcp_tcps; 24574 24575 iocp = (IOCP)mp->b_rptr; 24576 24577 if ((mp1 = mp->b_cont) == NULL || 24578 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 24579 err = EINVAL; 24580 goto out; 24581 } 24582 24583 /* check permissions */ 24584 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 24585 err = EPERM; 24586 goto out; 24587 } 24588 24589 if (mp1->b_cont != NULL) { 24590 freemsg(mp1->b_cont); 24591 mp1->b_cont = NULL; 24592 } 24593 24594 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 24595 laf = acp->ac_local.ss_family; 24596 raf = acp->ac_remote.ss_family; 24597 24598 /* check that a zone with the supplied zoneid exists */ 24599 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 24600 zptr = zone_find_by_id(zoneid); 24601 if (zptr != NULL) { 24602 zone_rele(zptr); 24603 } else { 24604 err = EINVAL; 24605 goto out; 24606 } 24607 } 24608 24609 /* 24610 * For exclusive stacks we set the zoneid to zero 24611 * to make TCP operate as if in the global zone. 24612 */ 24613 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 24614 acp->ac_zoneid = GLOBAL_ZONEID; 24615 24616 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 24617 acp->ac_start > acp->ac_end || laf != raf || 24618 (laf != AF_INET && laf != AF_INET6)) { 24619 err = EINVAL; 24620 goto out; 24621 } 24622 24623 tcp_ioctl_abort_dump(acp); 24624 err = tcp_ioctl_abort(acp, tcps); 24625 24626 out: 24627 if (mp1 != NULL) { 24628 freemsg(mp1); 24629 mp->b_cont = NULL; 24630 } 24631 24632 if (err != 0) 24633 miocnak(q, mp, 0, err); 24634 else 24635 miocack(q, mp, 0, 0); 24636 } 24637 24638 /* 24639 * tcp_time_wait_processing() handles processing of incoming packets when 24640 * the tcp is in the TIME_WAIT state. 24641 * A TIME_WAIT tcp that has an associated open TCP stream is never put 24642 * on the time wait list. 24643 */ 24644 void 24645 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 24646 uint32_t seg_ack, int seg_len, tcph_t *tcph) 24647 { 24648 int32_t bytes_acked; 24649 int32_t gap; 24650 int32_t rgap; 24651 tcp_opt_t tcpopt; 24652 uint_t flags; 24653 uint32_t new_swnd = 0; 24654 conn_t *connp; 24655 tcp_stack_t *tcps = tcp->tcp_tcps; 24656 24657 BUMP_LOCAL(tcp->tcp_ibsegs); 24658 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 24659 24660 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 24661 new_swnd = BE16_TO_U16(tcph->th_win) << 24662 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 24663 if (tcp->tcp_snd_ts_ok) { 24664 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 24665 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24666 tcp->tcp_rnxt, TH_ACK); 24667 goto done; 24668 } 24669 } 24670 gap = seg_seq - tcp->tcp_rnxt; 24671 rgap = tcp->tcp_rwnd - (gap + seg_len); 24672 if (gap < 0) { 24673 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 24674 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 24675 (seg_len > -gap ? -gap : seg_len)); 24676 seg_len += gap; 24677 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 24678 if (flags & TH_RST) { 24679 goto done; 24680 } 24681 if ((flags & TH_FIN) && seg_len == -1) { 24682 /* 24683 * When TCP receives a duplicate FIN in 24684 * TIME_WAIT state, restart the 2 MSL timer. 24685 * See page 73 in RFC 793. Make sure this TCP 24686 * is already on the TIME_WAIT list. If not, 24687 * just restart the timer. 24688 */ 24689 if (TCP_IS_DETACHED(tcp)) { 24690 if (tcp_time_wait_remove(tcp, NULL) == 24691 B_TRUE) { 24692 tcp_time_wait_append(tcp); 24693 TCP_DBGSTAT(tcps, 24694 tcp_rput_time_wait); 24695 } 24696 } else { 24697 ASSERT(tcp != NULL); 24698 TCP_TIMER_RESTART(tcp, 24699 tcps->tcps_time_wait_interval); 24700 } 24701 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24702 tcp->tcp_rnxt, TH_ACK); 24703 goto done; 24704 } 24705 flags |= TH_ACK_NEEDED; 24706 seg_len = 0; 24707 goto process_ack; 24708 } 24709 24710 /* Fix seg_seq, and chew the gap off the front. */ 24711 seg_seq = tcp->tcp_rnxt; 24712 } 24713 24714 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 24715 /* 24716 * Make sure that when we accept the connection, pick 24717 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 24718 * old connection. 24719 * 24720 * The next ISS generated is equal to tcp_iss_incr_extra 24721 * + ISS_INCR/2 + other components depending on the 24722 * value of tcp_strong_iss. We pre-calculate the new 24723 * ISS here and compare with tcp_snxt to determine if 24724 * we need to make adjustment to tcp_iss_incr_extra. 24725 * 24726 * The above calculation is ugly and is a 24727 * waste of CPU cycles... 24728 */ 24729 uint32_t new_iss = tcps->tcps_iss_incr_extra; 24730 int32_t adj; 24731 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24732 24733 switch (tcps->tcps_strong_iss) { 24734 case 2: { 24735 /* Add time and MD5 components. */ 24736 uint32_t answer[4]; 24737 struct { 24738 uint32_t ports; 24739 in6_addr_t src; 24740 in6_addr_t dst; 24741 } arg; 24742 MD5_CTX context; 24743 24744 mutex_enter(&tcps->tcps_iss_key_lock); 24745 context = tcps->tcps_iss_key; 24746 mutex_exit(&tcps->tcps_iss_key_lock); 24747 arg.ports = tcp->tcp_ports; 24748 /* We use MAPPED addresses in tcp_iss_init */ 24749 arg.src = tcp->tcp_ip_src_v6; 24750 if (tcp->tcp_ipversion == IPV4_VERSION) { 24751 IN6_IPADDR_TO_V4MAPPED( 24752 tcp->tcp_ipha->ipha_dst, 24753 &arg.dst); 24754 } else { 24755 arg.dst = 24756 tcp->tcp_ip6h->ip6_dst; 24757 } 24758 MD5Update(&context, (uchar_t *)&arg, 24759 sizeof (arg)); 24760 MD5Final((uchar_t *)answer, &context); 24761 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 24762 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 24763 break; 24764 } 24765 case 1: 24766 /* Add time component and min random (i.e. 1). */ 24767 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 24768 break; 24769 default: 24770 /* Add only time component. */ 24771 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24772 break; 24773 } 24774 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 24775 /* 24776 * New ISS not guaranteed to be ISS_INCR/2 24777 * ahead of the current tcp_snxt, so add the 24778 * difference to tcp_iss_incr_extra. 24779 */ 24780 tcps->tcps_iss_incr_extra += adj; 24781 } 24782 /* 24783 * If tcp_clean_death() can not perform the task now, 24784 * drop the SYN packet and let the other side re-xmit. 24785 * Otherwise pass the SYN packet back in, since the 24786 * old tcp state has been cleaned up or freed. 24787 */ 24788 if (tcp_clean_death(tcp, 0, 27) == -1) 24789 goto done; 24790 /* 24791 * We will come back to tcp_rput_data 24792 * on the global queue. Packets destined 24793 * for the global queue will be checked 24794 * with global policy. But the policy for 24795 * this packet has already been checked as 24796 * this was destined for the detached 24797 * connection. We need to bypass policy 24798 * check this time by attaching a dummy 24799 * ipsec_in with ipsec_in_dont_check set. 24800 */ 24801 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 24802 if (connp != NULL) { 24803 TCP_STAT(tcps, tcp_time_wait_syn_success); 24804 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 24805 return; 24806 } 24807 goto done; 24808 } 24809 24810 /* 24811 * rgap is the amount of stuff received out of window. A negative 24812 * value is the amount out of window. 24813 */ 24814 if (rgap < 0) { 24815 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 24816 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 24817 /* Fix seg_len and make sure there is something left. */ 24818 seg_len += rgap; 24819 if (seg_len <= 0) { 24820 if (flags & TH_RST) { 24821 goto done; 24822 } 24823 flags |= TH_ACK_NEEDED; 24824 seg_len = 0; 24825 goto process_ack; 24826 } 24827 } 24828 /* 24829 * Check whether we can update tcp_ts_recent. This test is 24830 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 24831 * Extensions for High Performance: An Update", Internet Draft. 24832 */ 24833 if (tcp->tcp_snd_ts_ok && 24834 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 24835 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 24836 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 24837 tcp->tcp_last_rcv_lbolt = lbolt64; 24838 } 24839 24840 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 24841 /* Always ack out of order packets */ 24842 flags |= TH_ACK_NEEDED; 24843 seg_len = 0; 24844 } else if (seg_len > 0) { 24845 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 24846 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 24847 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 24848 } 24849 if (flags & TH_RST) { 24850 (void) tcp_clean_death(tcp, 0, 28); 24851 goto done; 24852 } 24853 if (flags & TH_SYN) { 24854 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 24855 TH_RST|TH_ACK); 24856 /* 24857 * Do not delete the TCP structure if it is in 24858 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 24859 */ 24860 goto done; 24861 } 24862 process_ack: 24863 if (flags & TH_ACK) { 24864 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 24865 if (bytes_acked <= 0) { 24866 if (bytes_acked == 0 && seg_len == 0 && 24867 new_swnd == tcp->tcp_swnd) 24868 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 24869 } else { 24870 /* Acks something not sent */ 24871 flags |= TH_ACK_NEEDED; 24872 } 24873 } 24874 if (flags & TH_ACK_NEEDED) { 24875 /* 24876 * Time to send an ack for some reason. 24877 */ 24878 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24879 tcp->tcp_rnxt, TH_ACK); 24880 } 24881 done: 24882 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 24883 DB_CKSUMSTART(mp) = 0; 24884 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 24885 TCP_STAT(tcps, tcp_time_wait_syn_fail); 24886 } 24887 freemsg(mp); 24888 } 24889 24890 /* 24891 * TCP Timers Implementation. 24892 */ 24893 timeout_id_t 24894 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 24895 { 24896 mblk_t *mp; 24897 tcp_timer_t *tcpt; 24898 tcp_t *tcp = connp->conn_tcp; 24899 24900 ASSERT(connp->conn_sqp != NULL); 24901 24902 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_calls); 24903 24904 if (tcp->tcp_timercache == NULL) { 24905 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 24906 } else { 24907 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_cached_alloc); 24908 mp = tcp->tcp_timercache; 24909 tcp->tcp_timercache = mp->b_next; 24910 mp->b_next = NULL; 24911 ASSERT(mp->b_wptr == NULL); 24912 } 24913 24914 CONN_INC_REF(connp); 24915 tcpt = (tcp_timer_t *)mp->b_rptr; 24916 tcpt->connp = connp; 24917 tcpt->tcpt_proc = f; 24918 /* 24919 * TCP timers are normal timeouts. Plus, they do not require more than 24920 * a 10 millisecond resolution. By choosing a coarser resolution and by 24921 * rounding up the expiration to the next resolution boundary, we can 24922 * batch timers in the callout subsystem to make TCP timers more 24923 * efficient. The roundup also protects short timers from expiring too 24924 * early before they have a chance to be cancelled. 24925 */ 24926 tcpt->tcpt_tid = timeout_generic(CALLOUT_NORMAL, tcp_timer_callback, mp, 24927 TICK_TO_NSEC(tim), CALLOUT_TCP_RESOLUTION, CALLOUT_FLAG_ROUNDUP); 24928 24929 return ((timeout_id_t)mp); 24930 } 24931 24932 static void 24933 tcp_timer_callback(void *arg) 24934 { 24935 mblk_t *mp = (mblk_t *)arg; 24936 tcp_timer_t *tcpt; 24937 conn_t *connp; 24938 24939 tcpt = (tcp_timer_t *)mp->b_rptr; 24940 connp = tcpt->connp; 24941 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_timer_handler, connp, 24942 SQ_FILL, SQTAG_TCP_TIMER); 24943 } 24944 24945 static void 24946 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 24947 { 24948 tcp_timer_t *tcpt; 24949 conn_t *connp = (conn_t *)arg; 24950 tcp_t *tcp = connp->conn_tcp; 24951 24952 tcpt = (tcp_timer_t *)mp->b_rptr; 24953 ASSERT(connp == tcpt->connp); 24954 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 24955 24956 /* 24957 * If the TCP has reached the closed state, don't proceed any 24958 * further. This TCP logically does not exist on the system. 24959 * tcpt_proc could for example access queues, that have already 24960 * been qprocoff'ed off. Also see comments at the start of tcp_input 24961 */ 24962 if (tcp->tcp_state != TCPS_CLOSED) { 24963 (*tcpt->tcpt_proc)(connp); 24964 } else { 24965 tcp->tcp_timer_tid = 0; 24966 } 24967 tcp_timer_free(connp->conn_tcp, mp); 24968 } 24969 24970 /* 24971 * There is potential race with untimeout and the handler firing at the same 24972 * time. The mblock may be freed by the handler while we are trying to use 24973 * it. But since both should execute on the same squeue, this race should not 24974 * occur. 24975 */ 24976 clock_t 24977 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 24978 { 24979 mblk_t *mp = (mblk_t *)id; 24980 tcp_timer_t *tcpt; 24981 clock_t delta; 24982 24983 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_cancel_reqs); 24984 24985 if (mp == NULL) 24986 return (-1); 24987 24988 tcpt = (tcp_timer_t *)mp->b_rptr; 24989 ASSERT(tcpt->connp == connp); 24990 24991 delta = untimeout_default(tcpt->tcpt_tid, 0); 24992 24993 if (delta >= 0) { 24994 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_canceled); 24995 tcp_timer_free(connp->conn_tcp, mp); 24996 CONN_DEC_REF(connp); 24997 } 24998 24999 return (delta); 25000 } 25001 25002 /* 25003 * Allocate space for the timer event. The allocation looks like mblk, but it is 25004 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 25005 * 25006 * Dealing with failures: If we can't allocate from the timer cache we try 25007 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 25008 * points to b_rptr. 25009 * If we can't allocate anything using allocb_tryhard(), we perform a last 25010 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 25011 * save the actual allocation size in b_datap. 25012 */ 25013 mblk_t * 25014 tcp_timermp_alloc(int kmflags) 25015 { 25016 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 25017 kmflags & ~KM_PANIC); 25018 25019 if (mp != NULL) { 25020 mp->b_next = mp->b_prev = NULL; 25021 mp->b_rptr = (uchar_t *)(&mp[1]); 25022 mp->b_wptr = NULL; 25023 mp->b_datap = NULL; 25024 mp->b_queue = NULL; 25025 mp->b_cont = NULL; 25026 } else if (kmflags & KM_PANIC) { 25027 /* 25028 * Failed to allocate memory for the timer. Try allocating from 25029 * dblock caches. 25030 */ 25031 /* ipclassifier calls this from a constructor - hence no tcps */ 25032 TCP_G_STAT(tcp_timermp_allocfail); 25033 mp = allocb_tryhard(sizeof (tcp_timer_t)); 25034 if (mp == NULL) { 25035 size_t size = 0; 25036 /* 25037 * Memory is really low. Try tryhard allocation. 25038 * 25039 * ipclassifier calls this from a constructor - 25040 * hence no tcps 25041 */ 25042 TCP_G_STAT(tcp_timermp_allocdblfail); 25043 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 25044 sizeof (tcp_timer_t), &size, kmflags); 25045 mp->b_rptr = (uchar_t *)(&mp[1]); 25046 mp->b_next = mp->b_prev = NULL; 25047 mp->b_wptr = (uchar_t *)-1; 25048 mp->b_datap = (dblk_t *)size; 25049 mp->b_queue = NULL; 25050 mp->b_cont = NULL; 25051 } 25052 ASSERT(mp->b_wptr != NULL); 25053 } 25054 /* ipclassifier calls this from a constructor - hence no tcps */ 25055 TCP_G_DBGSTAT(tcp_timermp_alloced); 25056 25057 return (mp); 25058 } 25059 25060 /* 25061 * Free per-tcp timer cache. 25062 * It can only contain entries from tcp_timercache. 25063 */ 25064 void 25065 tcp_timermp_free(tcp_t *tcp) 25066 { 25067 mblk_t *mp; 25068 25069 while ((mp = tcp->tcp_timercache) != NULL) { 25070 ASSERT(mp->b_wptr == NULL); 25071 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 25072 kmem_cache_free(tcp_timercache, mp); 25073 } 25074 } 25075 25076 /* 25077 * Free timer event. Put it on the per-tcp timer cache if there is not too many 25078 * events there already (currently at most two events are cached). 25079 * If the event is not allocated from the timer cache, free it right away. 25080 */ 25081 static void 25082 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 25083 { 25084 mblk_t *mp1 = tcp->tcp_timercache; 25085 25086 if (mp->b_wptr != NULL) { 25087 /* 25088 * This allocation is not from a timer cache, free it right 25089 * away. 25090 */ 25091 if (mp->b_wptr != (uchar_t *)-1) 25092 freeb(mp); 25093 else 25094 kmem_free(mp, (size_t)mp->b_datap); 25095 } else if (mp1 == NULL || mp1->b_next == NULL) { 25096 /* Cache this timer block for future allocations */ 25097 mp->b_rptr = (uchar_t *)(&mp[1]); 25098 mp->b_next = mp1; 25099 tcp->tcp_timercache = mp; 25100 } else { 25101 kmem_cache_free(tcp_timercache, mp); 25102 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timermp_freed); 25103 } 25104 } 25105 25106 /* 25107 * End of TCP Timers implementation. 25108 */ 25109 25110 /* 25111 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 25112 * on the specified backing STREAMS q. Note, the caller may make the 25113 * decision to call based on the tcp_t.tcp_flow_stopped value which 25114 * when check outside the q's lock is only an advisory check ... 25115 */ 25116 void 25117 tcp_setqfull(tcp_t *tcp) 25118 { 25119 tcp_stack_t *tcps = tcp->tcp_tcps; 25120 conn_t *connp = tcp->tcp_connp; 25121 25122 if (tcp->tcp_closed) 25123 return; 25124 25125 if (IPCL_IS_NONSTR(connp)) { 25126 (*connp->conn_upcalls->su_txq_full) 25127 (tcp->tcp_connp->conn_upper_handle, B_TRUE); 25128 tcp->tcp_flow_stopped = B_TRUE; 25129 } else { 25130 queue_t *q = tcp->tcp_wq; 25131 25132 if (!(q->q_flag & QFULL)) { 25133 mutex_enter(QLOCK(q)); 25134 if (!(q->q_flag & QFULL)) { 25135 /* still need to set QFULL */ 25136 q->q_flag |= QFULL; 25137 tcp->tcp_flow_stopped = B_TRUE; 25138 mutex_exit(QLOCK(q)); 25139 TCP_STAT(tcps, tcp_flwctl_on); 25140 } else { 25141 mutex_exit(QLOCK(q)); 25142 } 25143 } 25144 } 25145 } 25146 25147 void 25148 tcp_clrqfull(tcp_t *tcp) 25149 { 25150 conn_t *connp = tcp->tcp_connp; 25151 25152 if (tcp->tcp_closed) 25153 return; 25154 25155 if (IPCL_IS_NONSTR(connp)) { 25156 (*connp->conn_upcalls->su_txq_full) 25157 (tcp->tcp_connp->conn_upper_handle, B_FALSE); 25158 tcp->tcp_flow_stopped = B_FALSE; 25159 } else { 25160 queue_t *q = tcp->tcp_wq; 25161 25162 if (q->q_flag & QFULL) { 25163 mutex_enter(QLOCK(q)); 25164 if (q->q_flag & QFULL) { 25165 q->q_flag &= ~QFULL; 25166 tcp->tcp_flow_stopped = B_FALSE; 25167 mutex_exit(QLOCK(q)); 25168 if (q->q_flag & QWANTW) 25169 qbackenable(q, 0); 25170 } else { 25171 mutex_exit(QLOCK(q)); 25172 } 25173 } 25174 } 25175 } 25176 25177 /* 25178 * kstats related to squeues i.e. not per IP instance 25179 */ 25180 static void * 25181 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 25182 { 25183 kstat_t *ksp; 25184 25185 tcp_g_stat_t template = { 25186 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 25187 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 25188 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 25189 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 25190 }; 25191 25192 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 25193 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 25194 KSTAT_FLAG_VIRTUAL); 25195 25196 if (ksp == NULL) 25197 return (NULL); 25198 25199 bcopy(&template, tcp_g_statp, sizeof (template)); 25200 ksp->ks_data = (void *)tcp_g_statp; 25201 25202 kstat_install(ksp); 25203 return (ksp); 25204 } 25205 25206 static void 25207 tcp_g_kstat_fini(kstat_t *ksp) 25208 { 25209 if (ksp != NULL) { 25210 kstat_delete(ksp); 25211 } 25212 } 25213 25214 25215 static void * 25216 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 25217 { 25218 kstat_t *ksp; 25219 25220 tcp_stat_t template = { 25221 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 25222 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 25223 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 25224 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 25225 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 25226 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 25227 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 25228 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 25229 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 25230 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 25231 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 25232 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 25233 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 25234 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 25235 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 25236 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 25237 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 25238 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 25239 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 25240 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 25241 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 25242 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 25243 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 25244 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 25245 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 25246 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 25247 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 25248 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 25249 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 25250 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 25251 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 25252 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 25253 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 25254 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 25255 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 25256 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 25257 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 25258 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 25259 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 25260 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 25261 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 25262 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 25263 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 25264 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 25265 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 25266 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 25267 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 25268 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 25269 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 25270 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 25271 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 25272 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 25273 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 25274 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 25275 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 25276 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 25277 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 25278 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 25279 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 25280 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 25281 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 25282 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 25283 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 25284 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 25285 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 25286 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 25287 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 25288 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 25289 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 25290 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 25291 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 25292 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 25293 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 25294 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 25295 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 25296 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 25297 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 25298 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 25299 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 25300 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 25301 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 25302 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 25303 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 25304 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 25305 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 25306 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 25307 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 25308 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 25309 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 25310 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 25311 }; 25312 25313 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 25314 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 25315 KSTAT_FLAG_VIRTUAL, stackid); 25316 25317 if (ksp == NULL) 25318 return (NULL); 25319 25320 bcopy(&template, tcps_statisticsp, sizeof (template)); 25321 ksp->ks_data = (void *)tcps_statisticsp; 25322 ksp->ks_private = (void *)(uintptr_t)stackid; 25323 25324 kstat_install(ksp); 25325 return (ksp); 25326 } 25327 25328 static void 25329 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 25330 { 25331 if (ksp != NULL) { 25332 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 25333 kstat_delete_netstack(ksp, stackid); 25334 } 25335 } 25336 25337 /* 25338 * TCP Kstats implementation 25339 */ 25340 static void * 25341 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 25342 { 25343 kstat_t *ksp; 25344 25345 tcp_named_kstat_t template = { 25346 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 25347 { "rtoMin", KSTAT_DATA_INT32, 0 }, 25348 { "rtoMax", KSTAT_DATA_INT32, 0 }, 25349 { "maxConn", KSTAT_DATA_INT32, 0 }, 25350 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 25351 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 25352 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 25353 { "estabResets", KSTAT_DATA_UINT32, 0 }, 25354 { "currEstab", KSTAT_DATA_UINT32, 0 }, 25355 { "inSegs", KSTAT_DATA_UINT64, 0 }, 25356 { "outSegs", KSTAT_DATA_UINT64, 0 }, 25357 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 25358 { "connTableSize", KSTAT_DATA_INT32, 0 }, 25359 { "outRsts", KSTAT_DATA_UINT32, 0 }, 25360 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 25361 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 25362 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 25363 { "outAck", KSTAT_DATA_UINT32, 0 }, 25364 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 25365 { "outUrg", KSTAT_DATA_UINT32, 0 }, 25366 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 25367 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 25368 { "outControl", KSTAT_DATA_UINT32, 0 }, 25369 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 25370 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 25371 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 25372 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 25373 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 25374 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 25375 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 25376 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 25377 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 25378 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 25379 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 25380 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 25381 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 25382 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 25383 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 25384 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 25385 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 25386 { "inClosed", KSTAT_DATA_UINT32, 0 }, 25387 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 25388 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 25389 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 25390 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 25391 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 25392 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 25393 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 25394 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 25395 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 25396 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 25397 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 25398 { "connTableSize6", KSTAT_DATA_INT32, 0 } 25399 }; 25400 25401 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 25402 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 25403 25404 if (ksp == NULL) 25405 return (NULL); 25406 25407 template.rtoAlgorithm.value.ui32 = 4; 25408 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 25409 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 25410 template.maxConn.value.i32 = -1; 25411 25412 bcopy(&template, ksp->ks_data, sizeof (template)); 25413 ksp->ks_update = tcp_kstat_update; 25414 ksp->ks_private = (void *)(uintptr_t)stackid; 25415 25416 kstat_install(ksp); 25417 return (ksp); 25418 } 25419 25420 static void 25421 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 25422 { 25423 if (ksp != NULL) { 25424 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 25425 kstat_delete_netstack(ksp, stackid); 25426 } 25427 } 25428 25429 static int 25430 tcp_kstat_update(kstat_t *kp, int rw) 25431 { 25432 tcp_named_kstat_t *tcpkp; 25433 tcp_t *tcp; 25434 connf_t *connfp; 25435 conn_t *connp; 25436 int i; 25437 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 25438 netstack_t *ns; 25439 tcp_stack_t *tcps; 25440 ip_stack_t *ipst; 25441 25442 if ((kp == NULL) || (kp->ks_data == NULL)) 25443 return (EIO); 25444 25445 if (rw == KSTAT_WRITE) 25446 return (EACCES); 25447 25448 ns = netstack_find_by_stackid(stackid); 25449 if (ns == NULL) 25450 return (-1); 25451 tcps = ns->netstack_tcp; 25452 if (tcps == NULL) { 25453 netstack_rele(ns); 25454 return (-1); 25455 } 25456 25457 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 25458 25459 tcpkp->currEstab.value.ui32 = 0; 25460 25461 ipst = ns->netstack_ip; 25462 25463 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25464 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25465 connp = NULL; 25466 while ((connp = 25467 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25468 tcp = connp->conn_tcp; 25469 switch (tcp_snmp_state(tcp)) { 25470 case MIB2_TCP_established: 25471 case MIB2_TCP_closeWait: 25472 tcpkp->currEstab.value.ui32++; 25473 break; 25474 } 25475 } 25476 } 25477 25478 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 25479 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 25480 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 25481 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 25482 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 25483 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 25484 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 25485 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 25486 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 25487 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 25488 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 25489 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 25490 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 25491 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 25492 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 25493 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 25494 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 25495 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 25496 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 25497 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 25498 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 25499 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 25500 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 25501 tcpkp->inDataInorderSegs.value.ui32 = 25502 tcps->tcps_mib.tcpInDataInorderSegs; 25503 tcpkp->inDataInorderBytes.value.ui32 = 25504 tcps->tcps_mib.tcpInDataInorderBytes; 25505 tcpkp->inDataUnorderSegs.value.ui32 = 25506 tcps->tcps_mib.tcpInDataUnorderSegs; 25507 tcpkp->inDataUnorderBytes.value.ui32 = 25508 tcps->tcps_mib.tcpInDataUnorderBytes; 25509 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 25510 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 25511 tcpkp->inDataPartDupSegs.value.ui32 = 25512 tcps->tcps_mib.tcpInDataPartDupSegs; 25513 tcpkp->inDataPartDupBytes.value.ui32 = 25514 tcps->tcps_mib.tcpInDataPartDupBytes; 25515 tcpkp->inDataPastWinSegs.value.ui32 = 25516 tcps->tcps_mib.tcpInDataPastWinSegs; 25517 tcpkp->inDataPastWinBytes.value.ui32 = 25518 tcps->tcps_mib.tcpInDataPastWinBytes; 25519 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 25520 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 25521 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 25522 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 25523 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 25524 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 25525 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 25526 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 25527 tcpkp->timKeepaliveProbe.value.ui32 = 25528 tcps->tcps_mib.tcpTimKeepaliveProbe; 25529 tcpkp->timKeepaliveDrop.value.ui32 = 25530 tcps->tcps_mib.tcpTimKeepaliveDrop; 25531 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 25532 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 25533 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 25534 tcpkp->outSackRetransSegs.value.ui32 = 25535 tcps->tcps_mib.tcpOutSackRetransSegs; 25536 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 25537 25538 netstack_rele(ns); 25539 return (0); 25540 } 25541 25542 void 25543 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 25544 { 25545 uint16_t hdr_len; 25546 ipha_t *ipha; 25547 uint8_t *nexthdrp; 25548 tcph_t *tcph; 25549 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 25550 25551 /* Already has an eager */ 25552 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25553 TCP_STAT(tcps, tcp_reinput_syn); 25554 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 25555 SQ_PROCESS, SQTAG_TCP_REINPUT_EAGER); 25556 return; 25557 } 25558 25559 switch (IPH_HDR_VERSION(mp->b_rptr)) { 25560 case IPV4_VERSION: 25561 ipha = (ipha_t *)mp->b_rptr; 25562 hdr_len = IPH_HDR_LENGTH(ipha); 25563 break; 25564 case IPV6_VERSION: 25565 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 25566 &hdr_len, &nexthdrp)) { 25567 CONN_DEC_REF(connp); 25568 freemsg(mp); 25569 return; 25570 } 25571 break; 25572 } 25573 25574 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 25575 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 25576 mp->b_datap->db_struioflag |= STRUIO_EAGER; 25577 DB_CKSUMSTART(mp) = (intptr_t)sqp; 25578 } 25579 25580 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 25581 SQ_FILL, SQTAG_TCP_REINPUT); 25582 } 25583 25584 static int 25585 tcp_squeue_switch(int val) 25586 { 25587 int rval = SQ_FILL; 25588 25589 switch (val) { 25590 case 1: 25591 rval = SQ_NODRAIN; 25592 break; 25593 case 2: 25594 rval = SQ_PROCESS; 25595 break; 25596 default: 25597 break; 25598 } 25599 return (rval); 25600 } 25601 25602 /* 25603 * This is called once for each squeue - globally for all stack 25604 * instances. 25605 */ 25606 static void 25607 tcp_squeue_add(squeue_t *sqp) 25608 { 25609 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 25610 sizeof (tcp_squeue_priv_t), KM_SLEEP); 25611 25612 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 25613 tcp_time_wait->tcp_time_wait_tid = 25614 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 25615 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 25616 CALLOUT_FLAG_ROUNDUP); 25617 if (tcp_free_list_max_cnt == 0) { 25618 int tcp_ncpus = ((boot_max_ncpus == -1) ? 25619 max_ncpus : boot_max_ncpus); 25620 25621 /* 25622 * Limit number of entries to 1% of availble memory / tcp_ncpus 25623 */ 25624 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 25625 (tcp_ncpus * sizeof (tcp_t) * 100); 25626 } 25627 tcp_time_wait->tcp_free_list_cnt = 0; 25628 } 25629 25630 static int 25631 tcp_post_ip_bind(tcp_t *tcp, mblk_t *mp, int error, cred_t *cr, pid_t pid) 25632 { 25633 mblk_t *ire_mp = NULL; 25634 mblk_t *syn_mp; 25635 mblk_t *mdti; 25636 mblk_t *lsoi; 25637 int retval; 25638 tcph_t *tcph; 25639 cred_t *ecr; 25640 ts_label_t *tsl; 25641 uint32_t mss; 25642 queue_t *q = tcp->tcp_rq; 25643 conn_t *connp = tcp->tcp_connp; 25644 tcp_stack_t *tcps = tcp->tcp_tcps; 25645 25646 if (error == 0) { 25647 /* 25648 * Adapt Multidata information, if any. The 25649 * following tcp_mdt_update routine will free 25650 * the message. 25651 */ 25652 if (mp != NULL && ((mdti = tcp_mdt_info_mp(mp)) != NULL)) { 25653 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 25654 b_rptr)->mdt_capab, B_TRUE); 25655 freemsg(mdti); 25656 } 25657 25658 /* 25659 * Check to update LSO information with tcp, and 25660 * tcp_lso_update routine will free the message. 25661 */ 25662 if (mp != NULL && ((lsoi = tcp_lso_info_mp(mp)) != NULL)) { 25663 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 25664 b_rptr)->lso_capab); 25665 freemsg(lsoi); 25666 } 25667 25668 /* Get the IRE, if we had requested for it */ 25669 if (mp != NULL) 25670 ire_mp = tcp_ire_mp(&mp); 25671 25672 if (tcp->tcp_hard_binding) { 25673 tcp->tcp_hard_binding = B_FALSE; 25674 tcp->tcp_hard_bound = B_TRUE; 25675 CL_INET_CONNECT(tcp->tcp_connp, tcp, B_TRUE, retval); 25676 if (retval != 0) { 25677 error = EADDRINUSE; 25678 goto bind_failed; 25679 } 25680 } else { 25681 if (ire_mp != NULL) 25682 freeb(ire_mp); 25683 goto after_syn_sent; 25684 } 25685 25686 retval = tcp_adapt_ire(tcp, ire_mp); 25687 if (ire_mp != NULL) 25688 freeb(ire_mp); 25689 if (retval == 0) { 25690 error = (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 25691 ENETUNREACH : EADDRNOTAVAIL); 25692 goto ipcl_rm; 25693 } 25694 /* 25695 * Don't let an endpoint connect to itself. 25696 * Also checked in tcp_connect() but that 25697 * check can't handle the case when the 25698 * local IP address is INADDR_ANY. 25699 */ 25700 if (tcp->tcp_ipversion == IPV4_VERSION) { 25701 if ((tcp->tcp_ipha->ipha_dst == 25702 tcp->tcp_ipha->ipha_src) && 25703 (BE16_EQL(tcp->tcp_tcph->th_lport, 25704 tcp->tcp_tcph->th_fport))) { 25705 error = EADDRNOTAVAIL; 25706 goto ipcl_rm; 25707 } 25708 } else { 25709 if (IN6_ARE_ADDR_EQUAL( 25710 &tcp->tcp_ip6h->ip6_dst, 25711 &tcp->tcp_ip6h->ip6_src) && 25712 (BE16_EQL(tcp->tcp_tcph->th_lport, 25713 tcp->tcp_tcph->th_fport))) { 25714 error = EADDRNOTAVAIL; 25715 goto ipcl_rm; 25716 } 25717 } 25718 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 25719 /* 25720 * This should not be possible! Just for 25721 * defensive coding... 25722 */ 25723 if (tcp->tcp_state != TCPS_SYN_SENT) 25724 goto after_syn_sent; 25725 25726 if (is_system_labeled() && 25727 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 25728 error = EHOSTUNREACH; 25729 goto ipcl_rm; 25730 } 25731 25732 /* 25733 * tcp_adapt_ire() does not adjust 25734 * for TCP/IP header length. 25735 */ 25736 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 25737 25738 /* 25739 * Just make sure our rwnd is at 25740 * least tcp_recv_hiwat_mss * MSS 25741 * large, and round up to the nearest 25742 * MSS. 25743 * 25744 * We do the round up here because 25745 * we need to get the interface 25746 * MTU first before we can do the 25747 * round up. 25748 */ 25749 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 25750 tcps->tcps_recv_hiwat_minmss * mss); 25751 if (!IPCL_IS_NONSTR(connp)) 25752 q->q_hiwat = tcp->tcp_rwnd; 25753 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 25754 tcp_set_ws_value(tcp); 25755 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 25756 tcp->tcp_tcph->th_win); 25757 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 25758 tcp->tcp_snd_ws_ok = B_TRUE; 25759 25760 /* 25761 * Set tcp_snd_ts_ok to true 25762 * so that tcp_xmit_mp will 25763 * include the timestamp 25764 * option in the SYN segment. 25765 */ 25766 if (tcps->tcps_tstamp_always || 25767 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 25768 tcp->tcp_snd_ts_ok = B_TRUE; 25769 } 25770 25771 /* 25772 * tcp_snd_sack_ok can be set in 25773 * tcp_adapt_ire() if the sack metric 25774 * is set. So check it here also. 25775 */ 25776 if (tcps->tcps_sack_permitted == 2 || 25777 tcp->tcp_snd_sack_ok) { 25778 if (tcp->tcp_sack_info == NULL) { 25779 tcp->tcp_sack_info = 25780 kmem_cache_alloc(tcp_sack_info_cache, 25781 KM_SLEEP); 25782 } 25783 tcp->tcp_snd_sack_ok = B_TRUE; 25784 } 25785 25786 /* 25787 * Should we use ECN? Note that the current 25788 * default value (SunOS 5.9) of tcp_ecn_permitted 25789 * is 1. The reason for doing this is that there 25790 * are equipments out there that will drop ECN 25791 * enabled IP packets. Setting it to 1 avoids 25792 * compatibility problems. 25793 */ 25794 if (tcps->tcps_ecn_permitted == 2) 25795 tcp->tcp_ecn_ok = B_TRUE; 25796 25797 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 25798 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 25799 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 25800 if (syn_mp) { 25801 /* 25802 * cr contains the cred from the thread calling 25803 * connect(). 25804 * 25805 * If no thread cred is available, use the 25806 * socket creator's cred instead. If still no 25807 * cred, drop the request rather than risk a 25808 * panic on production systems. 25809 */ 25810 if (cr == NULL) { 25811 cr = CONN_CRED(connp); 25812 pid = tcp->tcp_cpid; 25813 ASSERT(cr != NULL); 25814 if (cr != NULL) { 25815 mblk_setcred(syn_mp, cr, pid); 25816 } else { 25817 error = ECONNABORTED; 25818 goto ipcl_rm; 25819 } 25820 25821 /* 25822 * If an effective security label exists for 25823 * the connection, create a copy of the thread's 25824 * cred but with the effective label attached. 25825 */ 25826 } else if (is_system_labeled() && 25827 connp->conn_effective_cred != NULL && 25828 (tsl = crgetlabel(connp-> 25829 conn_effective_cred)) != NULL) { 25830 if ((ecr = copycred_from_tslabel(cr, 25831 tsl, KM_NOSLEEP)) == NULL) { 25832 error = ENOMEM; 25833 goto ipcl_rm; 25834 } 25835 mblk_setcred(syn_mp, ecr, pid); 25836 crfree(ecr); 25837 25838 /* 25839 * Default to using the thread's cred unchanged. 25840 */ 25841 } else { 25842 mblk_setcred(syn_mp, cr, pid); 25843 } 25844 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 25845 } 25846 after_syn_sent: 25847 if (mp != NULL) { 25848 ASSERT(mp->b_cont == NULL); 25849 freeb(mp); 25850 } 25851 return (error); 25852 } else { 25853 /* error */ 25854 if (tcp->tcp_debug) { 25855 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 25856 "tcp_post_ip_bind: error == %d", error); 25857 } 25858 if (mp != NULL) { 25859 freeb(mp); 25860 } 25861 } 25862 25863 ipcl_rm: 25864 /* 25865 * Need to unbind with classifier since we were just 25866 * told that our bind succeeded. a.k.a error == 0 at the entry. 25867 */ 25868 tcp->tcp_hard_bound = B_FALSE; 25869 tcp->tcp_hard_binding = B_FALSE; 25870 25871 ipcl_hash_remove(connp); 25872 25873 bind_failed: 25874 tcp->tcp_state = TCPS_IDLE; 25875 if (tcp->tcp_ipversion == IPV4_VERSION) 25876 tcp->tcp_ipha->ipha_src = 0; 25877 else 25878 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 25879 /* 25880 * Copy of the src addr. in tcp_t is needed since 25881 * the lookup funcs. can only look at tcp_t 25882 */ 25883 V6_SET_ZERO(tcp->tcp_ip_src_v6); 25884 25885 tcph = tcp->tcp_tcph; 25886 tcph->th_lport[0] = 0; 25887 tcph->th_lport[1] = 0; 25888 tcp_bind_hash_remove(tcp); 25889 bzero(&connp->u_port, sizeof (connp->u_port)); 25890 /* blow away saved option results if any */ 25891 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 25892 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 25893 25894 conn_delete_ire(tcp->tcp_connp, NULL); 25895 25896 return (error); 25897 } 25898 25899 static int 25900 tcp_bind_select_lport(tcp_t *tcp, in_port_t *requested_port_ptr, 25901 boolean_t bind_to_req_port_only, cred_t *cr) 25902 { 25903 in_port_t mlp_port; 25904 mlp_type_t addrtype, mlptype; 25905 boolean_t user_specified; 25906 in_port_t allocated_port; 25907 in_port_t requested_port = *requested_port_ptr; 25908 conn_t *connp; 25909 zone_t *zone; 25910 tcp_stack_t *tcps = tcp->tcp_tcps; 25911 in6_addr_t v6addr = tcp->tcp_ip_src_v6; 25912 25913 /* 25914 * XXX It's up to the caller to specify bind_to_req_port_only or not. 25915 */ 25916 if (cr == NULL) 25917 cr = tcp->tcp_cred; 25918 /* 25919 * Get a valid port (within the anonymous range and should not 25920 * be a privileged one) to use if the user has not given a port. 25921 * If multiple threads are here, they may all start with 25922 * with the same initial port. But, it should be fine as long as 25923 * tcp_bindi will ensure that no two threads will be assigned 25924 * the same port. 25925 * 25926 * NOTE: XXX If a privileged process asks for an anonymous port, we 25927 * still check for ports only in the range > tcp_smallest_non_priv_port, 25928 * unless TCP_ANONPRIVBIND option is set. 25929 */ 25930 mlptype = mlptSingle; 25931 mlp_port = requested_port; 25932 if (requested_port == 0) { 25933 requested_port = tcp->tcp_anon_priv_bind ? 25934 tcp_get_next_priv_port(tcp) : 25935 tcp_update_next_port(tcps->tcps_next_port_to_try, 25936 tcp, B_TRUE); 25937 if (requested_port == 0) { 25938 return (-TNOADDR); 25939 } 25940 user_specified = B_FALSE; 25941 25942 /* 25943 * If the user went through one of the RPC interfaces to create 25944 * this socket and RPC is MLP in this zone, then give him an 25945 * anonymous MLP. 25946 */ 25947 connp = tcp->tcp_connp; 25948 if (connp->conn_anon_mlp && is_system_labeled()) { 25949 zone = crgetzone(cr); 25950 addrtype = tsol_mlp_addr_type(zone->zone_id, 25951 IPV6_VERSION, &v6addr, 25952 tcps->tcps_netstack->netstack_ip); 25953 if (addrtype == mlptSingle) { 25954 return (-TNOADDR); 25955 } 25956 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 25957 PMAPPORT, addrtype); 25958 mlp_port = PMAPPORT; 25959 } 25960 } else { 25961 int i; 25962 boolean_t priv = B_FALSE; 25963 25964 /* 25965 * If the requested_port is in the well-known privileged range, 25966 * verify that the stream was opened by a privileged user. 25967 * Note: No locks are held when inspecting tcp_g_*epriv_ports 25968 * but instead the code relies on: 25969 * - the fact that the address of the array and its size never 25970 * changes 25971 * - the atomic assignment of the elements of the array 25972 */ 25973 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 25974 priv = B_TRUE; 25975 } else { 25976 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 25977 if (requested_port == 25978 tcps->tcps_g_epriv_ports[i]) { 25979 priv = B_TRUE; 25980 break; 25981 } 25982 } 25983 } 25984 if (priv) { 25985 if (secpolicy_net_privaddr(cr, requested_port, 25986 IPPROTO_TCP) != 0) { 25987 if (tcp->tcp_debug) { 25988 (void) strlog(TCP_MOD_ID, 0, 1, 25989 SL_ERROR|SL_TRACE, 25990 "tcp_bind: no priv for port %d", 25991 requested_port); 25992 } 25993 return (-TACCES); 25994 } 25995 } 25996 user_specified = B_TRUE; 25997 25998 connp = tcp->tcp_connp; 25999 if (is_system_labeled()) { 26000 zone = crgetzone(cr); 26001 addrtype = tsol_mlp_addr_type(zone->zone_id, 26002 IPV6_VERSION, &v6addr, 26003 tcps->tcps_netstack->netstack_ip); 26004 if (addrtype == mlptSingle) { 26005 return (-TNOADDR); 26006 } 26007 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26008 requested_port, addrtype); 26009 } 26010 } 26011 26012 if (mlptype != mlptSingle) { 26013 if (secpolicy_net_bindmlp(cr) != 0) { 26014 if (tcp->tcp_debug) { 26015 (void) strlog(TCP_MOD_ID, 0, 1, 26016 SL_ERROR|SL_TRACE, 26017 "tcp_bind: no priv for multilevel port %d", 26018 requested_port); 26019 } 26020 return (-TACCES); 26021 } 26022 26023 /* 26024 * If we're specifically binding a shared IP address and the 26025 * port is MLP on shared addresses, then check to see if this 26026 * zone actually owns the MLP. Reject if not. 26027 */ 26028 if (mlptype == mlptShared && addrtype == mlptShared) { 26029 /* 26030 * No need to handle exclusive-stack zones since 26031 * ALL_ZONES only applies to the shared stack. 26032 */ 26033 zoneid_t mlpzone; 26034 26035 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 26036 htons(mlp_port)); 26037 if (connp->conn_zoneid != mlpzone) { 26038 if (tcp->tcp_debug) { 26039 (void) strlog(TCP_MOD_ID, 0, 1, 26040 SL_ERROR|SL_TRACE, 26041 "tcp_bind: attempt to bind port " 26042 "%d on shared addr in zone %d " 26043 "(should be %d)", 26044 mlp_port, connp->conn_zoneid, 26045 mlpzone); 26046 } 26047 return (-TACCES); 26048 } 26049 } 26050 26051 if (!user_specified) { 26052 int err; 26053 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 26054 requested_port, B_TRUE); 26055 if (err != 0) { 26056 if (tcp->tcp_debug) { 26057 (void) strlog(TCP_MOD_ID, 0, 1, 26058 SL_ERROR|SL_TRACE, 26059 "tcp_bind: cannot establish anon " 26060 "MLP for port %d", 26061 requested_port); 26062 } 26063 return (err); 26064 } 26065 connp->conn_anon_port = B_TRUE; 26066 } 26067 connp->conn_mlp_type = mlptype; 26068 } 26069 26070 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 26071 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 26072 26073 if (allocated_port == 0) { 26074 connp->conn_mlp_type = mlptSingle; 26075 if (connp->conn_anon_port) { 26076 connp->conn_anon_port = B_FALSE; 26077 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 26078 requested_port, B_FALSE); 26079 } 26080 if (bind_to_req_port_only) { 26081 if (tcp->tcp_debug) { 26082 (void) strlog(TCP_MOD_ID, 0, 1, 26083 SL_ERROR|SL_TRACE, 26084 "tcp_bind: requested addr busy"); 26085 } 26086 return (-TADDRBUSY); 26087 } else { 26088 /* If we are out of ports, fail the bind. */ 26089 if (tcp->tcp_debug) { 26090 (void) strlog(TCP_MOD_ID, 0, 1, 26091 SL_ERROR|SL_TRACE, 26092 "tcp_bind: out of ports?"); 26093 } 26094 return (-TNOADDR); 26095 } 26096 } 26097 26098 /* Pass the allocated port back */ 26099 *requested_port_ptr = allocated_port; 26100 return (0); 26101 } 26102 26103 static int 26104 tcp_bind_check(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 26105 boolean_t bind_to_req_port_only) 26106 { 26107 tcp_t *tcp = connp->conn_tcp; 26108 sin_t *sin; 26109 sin6_t *sin6; 26110 in_port_t requested_port; 26111 ipaddr_t v4addr; 26112 in6_addr_t v6addr; 26113 uint_t origipversion; 26114 int error = 0; 26115 26116 ASSERT((uintptr_t)len <= (uintptr_t)INT_MAX); 26117 26118 if (tcp->tcp_state == TCPS_BOUND) { 26119 return (0); 26120 } else if (tcp->tcp_state > TCPS_BOUND) { 26121 if (tcp->tcp_debug) { 26122 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26123 "tcp_bind: bad state, %d", tcp->tcp_state); 26124 } 26125 return (-TOUTSTATE); 26126 } 26127 origipversion = tcp->tcp_ipversion; 26128 26129 ASSERT(sa != NULL && len != 0); 26130 26131 if (!OK_32PTR((char *)sa)) { 26132 if (tcp->tcp_debug) { 26133 (void) strlog(TCP_MOD_ID, 0, 1, 26134 SL_ERROR|SL_TRACE, 26135 "tcp_bind: bad address parameter, " 26136 "address %p, len %d", 26137 (void *)sa, len); 26138 } 26139 return (-TPROTO); 26140 } 26141 26142 switch (len) { 26143 case sizeof (sin_t): /* Complete IPv4 address */ 26144 sin = (sin_t *)sa; 26145 /* 26146 * With sockets sockfs will accept bogus sin_family in 26147 * bind() and replace it with the family used in the socket 26148 * call. 26149 */ 26150 if (sin->sin_family != AF_INET || 26151 tcp->tcp_family != AF_INET) { 26152 return (EAFNOSUPPORT); 26153 } 26154 requested_port = ntohs(sin->sin_port); 26155 tcp->tcp_ipversion = IPV4_VERSION; 26156 v4addr = sin->sin_addr.s_addr; 26157 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 26158 break; 26159 26160 case sizeof (sin6_t): /* Complete IPv6 address */ 26161 sin6 = (sin6_t *)sa; 26162 if (sin6->sin6_family != AF_INET6 || 26163 tcp->tcp_family != AF_INET6) { 26164 return (EAFNOSUPPORT); 26165 } 26166 requested_port = ntohs(sin6->sin6_port); 26167 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 26168 IPV4_VERSION : IPV6_VERSION; 26169 v6addr = sin6->sin6_addr; 26170 break; 26171 26172 default: 26173 if (tcp->tcp_debug) { 26174 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26175 "tcp_bind: bad address length, %d", len); 26176 } 26177 return (EAFNOSUPPORT); 26178 /* return (-TBADADDR); */ 26179 } 26180 26181 tcp->tcp_bound_source_v6 = v6addr; 26182 26183 /* Check for change in ipversion */ 26184 if (origipversion != tcp->tcp_ipversion) { 26185 ASSERT(tcp->tcp_family == AF_INET6); 26186 error = tcp->tcp_ipversion == IPV6_VERSION ? 26187 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 26188 if (error) { 26189 return (ENOMEM); 26190 } 26191 } 26192 26193 /* 26194 * Initialize family specific fields. Copy of the src addr. 26195 * in tcp_t is needed for the lookup funcs. 26196 */ 26197 if (tcp->tcp_ipversion == IPV6_VERSION) { 26198 tcp->tcp_ip6h->ip6_src = v6addr; 26199 } else { 26200 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 26201 } 26202 tcp->tcp_ip_src_v6 = v6addr; 26203 26204 bind_to_req_port_only = requested_port != 0 && bind_to_req_port_only; 26205 26206 error = tcp_bind_select_lport(tcp, &requested_port, 26207 bind_to_req_port_only, cr); 26208 26209 return (error); 26210 } 26211 26212 /* 26213 * Return unix error is tli error is TSYSERR, otherwise return a negative 26214 * tli error. 26215 */ 26216 int 26217 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 26218 boolean_t bind_to_req_port_only) 26219 { 26220 int error; 26221 tcp_t *tcp = connp->conn_tcp; 26222 26223 if (tcp->tcp_state >= TCPS_BOUND) { 26224 if (tcp->tcp_debug) { 26225 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26226 "tcp_bind: bad state, %d", tcp->tcp_state); 26227 } 26228 return (-TOUTSTATE); 26229 } 26230 26231 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only); 26232 if (error != 0) 26233 return (error); 26234 26235 ASSERT(tcp->tcp_state == TCPS_BOUND); 26236 26237 tcp->tcp_conn_req_max = 0; 26238 26239 if (tcp->tcp_family == AF_INET6) { 26240 ASSERT(tcp->tcp_connp->conn_af_isv6); 26241 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 26242 &tcp->tcp_bound_source_v6, 0, B_FALSE); 26243 } else { 26244 ASSERT(!tcp->tcp_connp->conn_af_isv6); 26245 error = ip_proto_bind_laddr_v4(connp, NULL, IPPROTO_TCP, 26246 tcp->tcp_ipha->ipha_src, 0, B_FALSE); 26247 } 26248 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 26249 } 26250 26251 int 26252 tcp_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa, 26253 socklen_t len, cred_t *cr) 26254 { 26255 int error; 26256 conn_t *connp = (conn_t *)proto_handle; 26257 squeue_t *sqp = connp->conn_sqp; 26258 26259 /* All Solaris components should pass a cred for this operation. */ 26260 ASSERT(cr != NULL); 26261 26262 ASSERT(sqp != NULL); 26263 ASSERT(connp->conn_upper_handle != NULL); 26264 26265 error = squeue_synch_enter(sqp, connp, NULL); 26266 if (error != 0) { 26267 /* failed to enter */ 26268 return (ENOSR); 26269 } 26270 26271 /* binding to a NULL address really means unbind */ 26272 if (sa == NULL) { 26273 if (connp->conn_tcp->tcp_state < TCPS_LISTEN) 26274 error = tcp_do_unbind(connp); 26275 else 26276 error = EINVAL; 26277 } else { 26278 error = tcp_do_bind(connp, sa, len, cr, B_TRUE); 26279 } 26280 26281 squeue_synch_exit(sqp, connp); 26282 26283 if (error < 0) { 26284 if (error == -TOUTSTATE) 26285 error = EINVAL; 26286 else 26287 error = proto_tlitosyserr(-error); 26288 } 26289 26290 return (error); 26291 } 26292 26293 /* 26294 * If the return value from this function is positive, it's a UNIX error. 26295 * Otherwise, if it's negative, then the absolute value is a TLI error. 26296 * the TPI routine tcp_tpi_connect() is a wrapper function for this. 26297 */ 26298 int 26299 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len, 26300 cred_t *cr, pid_t pid) 26301 { 26302 tcp_t *tcp = connp->conn_tcp; 26303 sin_t *sin = (sin_t *)sa; 26304 sin6_t *sin6 = (sin6_t *)sa; 26305 ipaddr_t *dstaddrp; 26306 in_port_t dstport; 26307 uint_t srcid; 26308 int error = 0; 26309 26310 switch (len) { 26311 default: 26312 /* 26313 * Should never happen 26314 */ 26315 return (EINVAL); 26316 26317 case sizeof (sin_t): 26318 sin = (sin_t *)sa; 26319 if (sin->sin_port == 0) { 26320 return (-TBADADDR); 26321 } 26322 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 26323 return (EAFNOSUPPORT); 26324 } 26325 break; 26326 26327 case sizeof (sin6_t): 26328 sin6 = (sin6_t *)sa; 26329 if (sin6->sin6_port == 0) { 26330 return (-TBADADDR); 26331 } 26332 break; 26333 } 26334 /* 26335 * If we're connecting to an IPv4-mapped IPv6 address, we need to 26336 * make sure that the template IP header in the tcp structure is an 26337 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 26338 * need to this before we call tcp_bindi() so that the port lookup 26339 * code will look for ports in the correct port space (IPv4 and 26340 * IPv6 have separate port spaces). 26341 */ 26342 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 26343 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 26344 int err = 0; 26345 26346 err = tcp_header_init_ipv4(tcp); 26347 if (err != 0) { 26348 error = ENOMEM; 26349 goto connect_failed; 26350 } 26351 if (tcp->tcp_lport != 0) 26352 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 26353 } 26354 26355 switch (tcp->tcp_state) { 26356 case TCPS_LISTEN: 26357 /* 26358 * Listening sockets are not allowed to issue connect(). 26359 */ 26360 if (IPCL_IS_NONSTR(connp)) 26361 return (EOPNOTSUPP); 26362 /* FALLTHRU */ 26363 case TCPS_IDLE: 26364 /* 26365 * We support quick connect, refer to comments in 26366 * tcp_connect_*() 26367 */ 26368 /* FALLTHRU */ 26369 case TCPS_BOUND: 26370 /* 26371 * We must bump the generation before the operation start. 26372 * This is done to ensure that any upcall made later on sends 26373 * up the right generation to the socket. 26374 */ 26375 SOCK_CONNID_BUMP(tcp->tcp_connid); 26376 26377 if (tcp->tcp_family == AF_INET6) { 26378 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 26379 return (tcp_connect_ipv6(tcp, 26380 &sin6->sin6_addr, 26381 sin6->sin6_port, sin6->sin6_flowinfo, 26382 sin6->__sin6_src_id, sin6->sin6_scope_id, 26383 cr, pid)); 26384 } 26385 /* 26386 * Destination adress is mapped IPv6 address. 26387 * Source bound address should be unspecified or 26388 * IPv6 mapped address as well. 26389 */ 26390 if (!IN6_IS_ADDR_UNSPECIFIED( 26391 &tcp->tcp_bound_source_v6) && 26392 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 26393 return (EADDRNOTAVAIL); 26394 } 26395 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 26396 dstport = sin6->sin6_port; 26397 srcid = sin6->__sin6_src_id; 26398 } else { 26399 dstaddrp = &sin->sin_addr.s_addr; 26400 dstport = sin->sin_port; 26401 srcid = 0; 26402 } 26403 26404 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid, cr, 26405 pid); 26406 break; 26407 default: 26408 return (-TOUTSTATE); 26409 } 26410 /* 26411 * Note: Code below is the "failure" case 26412 */ 26413 connect_failed: 26414 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 26415 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 26416 return (error); 26417 } 26418 26419 int 26420 tcp_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa, 26421 socklen_t len, sock_connid_t *id, cred_t *cr) 26422 { 26423 conn_t *connp = (conn_t *)proto_handle; 26424 tcp_t *tcp = connp->conn_tcp; 26425 squeue_t *sqp = connp->conn_sqp; 26426 int error; 26427 26428 ASSERT(connp->conn_upper_handle != NULL); 26429 26430 /* All Solaris components should pass a cred for this operation. */ 26431 ASSERT(cr != NULL); 26432 26433 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 26434 if (error != 0) { 26435 return (error); 26436 } 26437 26438 error = squeue_synch_enter(sqp, connp, NULL); 26439 if (error != 0) { 26440 /* failed to enter */ 26441 return (ENOSR); 26442 } 26443 26444 /* 26445 * TCP supports quick connect, so no need to do an implicit bind 26446 */ 26447 error = tcp_do_connect(connp, sa, len, cr, curproc->p_pid); 26448 if (error == 0) { 26449 *id = connp->conn_tcp->tcp_connid; 26450 } else if (error < 0) { 26451 if (error == -TOUTSTATE) { 26452 switch (connp->conn_tcp->tcp_state) { 26453 case TCPS_SYN_SENT: 26454 error = EALREADY; 26455 break; 26456 case TCPS_ESTABLISHED: 26457 error = EISCONN; 26458 break; 26459 case TCPS_LISTEN: 26460 error = EOPNOTSUPP; 26461 break; 26462 default: 26463 error = EINVAL; 26464 break; 26465 } 26466 } else { 26467 error = proto_tlitosyserr(-error); 26468 } 26469 } 26470 26471 if (tcp->tcp_loopback) { 26472 struct sock_proto_props sopp; 26473 26474 sopp.sopp_flags = SOCKOPT_LOOPBACK; 26475 sopp.sopp_loopback = B_TRUE; 26476 26477 (*connp->conn_upcalls->su_set_proto_props)( 26478 connp->conn_upper_handle, &sopp); 26479 } 26480 done: 26481 squeue_synch_exit(sqp, connp); 26482 26483 return ((error == 0) ? EINPROGRESS : error); 26484 } 26485 26486 /* ARGSUSED */ 26487 sock_lower_handle_t 26488 tcp_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls, 26489 uint_t *smodep, int *errorp, int flags, cred_t *credp) 26490 { 26491 conn_t *connp; 26492 boolean_t isv6 = family == AF_INET6; 26493 if (type != SOCK_STREAM || (family != AF_INET && family != AF_INET6) || 26494 (proto != 0 && proto != IPPROTO_TCP)) { 26495 *errorp = EPROTONOSUPPORT; 26496 return (NULL); 26497 } 26498 26499 connp = tcp_create_common(NULL, credp, isv6, B_TRUE, errorp); 26500 if (connp == NULL) { 26501 return (NULL); 26502 } 26503 26504 /* 26505 * Put the ref for TCP. Ref for IP was already put 26506 * by ipcl_conn_create. Also Make the conn_t globally 26507 * visible to walkers 26508 */ 26509 mutex_enter(&connp->conn_lock); 26510 CONN_INC_REF_LOCKED(connp); 26511 ASSERT(connp->conn_ref == 2); 26512 connp->conn_state_flags &= ~CONN_INCIPIENT; 26513 26514 connp->conn_flags |= IPCL_NONSTR; 26515 mutex_exit(&connp->conn_lock); 26516 26517 ASSERT(errorp != NULL); 26518 *errorp = 0; 26519 *sock_downcalls = &sock_tcp_downcalls; 26520 *smodep = SM_CONNREQUIRED | SM_EXDATA | SM_ACCEPTSUPP | 26521 SM_SENDFILESUPP; 26522 26523 return ((sock_lower_handle_t)connp); 26524 } 26525 26526 /* ARGSUSED */ 26527 void 26528 tcp_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle, 26529 sock_upcalls_t *sock_upcalls, int flags, cred_t *cr) 26530 { 26531 conn_t *connp = (conn_t *)proto_handle; 26532 struct sock_proto_props sopp; 26533 26534 ASSERT(connp->conn_upper_handle == NULL); 26535 26536 /* All Solaris components should pass a cred for this operation. */ 26537 ASSERT(cr != NULL); 26538 26539 sopp.sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | 26540 SOCKOPT_MAXPSZ | SOCKOPT_MAXBLK | SOCKOPT_RCVTIMER | 26541 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ; 26542 26543 sopp.sopp_rxhiwat = SOCKET_RECVHIWATER; 26544 sopp.sopp_rxlowat = SOCKET_RECVLOWATER; 26545 sopp.sopp_maxpsz = INFPSZ; 26546 sopp.sopp_maxblk = INFPSZ; 26547 sopp.sopp_rcvtimer = SOCKET_TIMER_INTERVAL; 26548 sopp.sopp_rcvthresh = SOCKET_RECVHIWATER >> 3; 26549 sopp.sopp_maxaddrlen = sizeof (sin6_t); 26550 sopp.sopp_minpsz = (tcp_rinfo.mi_minpsz == 1) ? 0 : 26551 tcp_rinfo.mi_minpsz; 26552 26553 connp->conn_upcalls = sock_upcalls; 26554 connp->conn_upper_handle = sock_handle; 26555 26556 (*sock_upcalls->su_set_proto_props)(sock_handle, &sopp); 26557 } 26558 26559 /* ARGSUSED */ 26560 int 26561 tcp_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr) 26562 { 26563 conn_t *connp = (conn_t *)proto_handle; 26564 26565 ASSERT(connp->conn_upper_handle != NULL); 26566 26567 /* All Solaris components should pass a cred for this operation. */ 26568 ASSERT(cr != NULL); 26569 26570 tcp_close_common(connp, flags); 26571 26572 ip_free_helper_stream(connp); 26573 26574 /* 26575 * Drop IP's reference on the conn. This is the last reference 26576 * on the connp if the state was less than established. If the 26577 * connection has gone into timewait state, then we will have 26578 * one ref for the TCP and one more ref (total of two) for the 26579 * classifier connected hash list (a timewait connections stays 26580 * in connected hash till closed). 26581 * 26582 * We can't assert the references because there might be other 26583 * transient reference places because of some walkers or queued 26584 * packets in squeue for the timewait state. 26585 */ 26586 CONN_DEC_REF(connp); 26587 return (0); 26588 } 26589 26590 /* ARGSUSED */ 26591 int 26592 tcp_sendmsg(sock_lower_handle_t proto_handle, mblk_t *mp, struct nmsghdr *msg, 26593 cred_t *cr) 26594 { 26595 tcp_t *tcp; 26596 uint32_t msize; 26597 conn_t *connp = (conn_t *)proto_handle; 26598 int32_t tcpstate; 26599 26600 /* All Solaris components should pass a cred for this operation. */ 26601 ASSERT(cr != NULL); 26602 26603 ASSERT(connp->conn_ref >= 2); 26604 ASSERT(connp->conn_upper_handle != NULL); 26605 26606 if (msg->msg_controllen != 0) { 26607 return (EOPNOTSUPP); 26608 26609 } 26610 switch (DB_TYPE(mp)) { 26611 case M_DATA: 26612 tcp = connp->conn_tcp; 26613 ASSERT(tcp != NULL); 26614 26615 tcpstate = tcp->tcp_state; 26616 if (tcpstate < TCPS_ESTABLISHED) { 26617 freemsg(mp); 26618 return (ENOTCONN); 26619 } else if (tcpstate > TCPS_CLOSE_WAIT) { 26620 freemsg(mp); 26621 return (EPIPE); 26622 } 26623 26624 msize = msgdsize(mp); 26625 26626 mutex_enter(&tcp->tcp_non_sq_lock); 26627 tcp->tcp_squeue_bytes += msize; 26628 /* 26629 * Squeue Flow Control 26630 */ 26631 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 26632 tcp_setqfull(tcp); 26633 } 26634 mutex_exit(&tcp->tcp_non_sq_lock); 26635 26636 /* 26637 * The application may pass in an address in the msghdr, but 26638 * we ignore the address on connection-oriented sockets. 26639 * Just like BSD this code does not generate an error for 26640 * TCP (a CONNREQUIRED socket) when sending to an address 26641 * passed in with sendto/sendmsg. Instead the data is 26642 * delivered on the connection as if no address had been 26643 * supplied. 26644 */ 26645 CONN_INC_REF(connp); 26646 26647 if (msg != NULL && msg->msg_flags & MSG_OOB) { 26648 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 26649 tcp_output_urgent, connp, tcp_squeue_flag, 26650 SQTAG_TCP_OUTPUT); 26651 } else { 26652 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, 26653 connp, tcp_squeue_flag, SQTAG_TCP_OUTPUT); 26654 } 26655 26656 return (0); 26657 26658 default: 26659 ASSERT(0); 26660 } 26661 26662 freemsg(mp); 26663 return (0); 26664 } 26665 26666 /* ARGSUSED */ 26667 void 26668 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2) 26669 { 26670 int len; 26671 uint32_t msize; 26672 conn_t *connp = (conn_t *)arg; 26673 tcp_t *tcp = connp->conn_tcp; 26674 26675 msize = msgdsize(mp); 26676 26677 len = msize - 1; 26678 if (len < 0) { 26679 freemsg(mp); 26680 return; 26681 } 26682 26683 /* 26684 * Try to force urgent data out on the wire. Even if we have unsent 26685 * data this will at least send the urgent flag. 26686 * XXX does not handle more flag correctly. 26687 */ 26688 len += tcp->tcp_unsent; 26689 len += tcp->tcp_snxt; 26690 tcp->tcp_urg = len; 26691 tcp->tcp_valid_bits |= TCP_URG_VALID; 26692 26693 /* Bypass tcp protocol for fused tcp loopback */ 26694 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 26695 return; 26696 26697 /* Strip off the T_EXDATA_REQ if the data is from TPI */ 26698 if (DB_TYPE(mp) != M_DATA) { 26699 mblk_t *mp1 = mp; 26700 ASSERT(!IPCL_IS_NONSTR(connp)); 26701 mp = mp->b_cont; 26702 freeb(mp1); 26703 } 26704 tcp_wput_data(tcp, mp, B_TRUE); 26705 } 26706 26707 /* ARGSUSED */ 26708 int 26709 tcp_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr, 26710 socklen_t *addrlenp, cred_t *cr) 26711 { 26712 conn_t *connp = (conn_t *)proto_handle; 26713 tcp_t *tcp = connp->conn_tcp; 26714 26715 ASSERT(connp->conn_upper_handle != NULL); 26716 /* All Solaris components should pass a cred for this operation. */ 26717 ASSERT(cr != NULL); 26718 26719 ASSERT(tcp != NULL); 26720 26721 return (tcp_do_getpeername(tcp, addr, addrlenp)); 26722 } 26723 26724 /* ARGSUSED */ 26725 int 26726 tcp_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr, 26727 socklen_t *addrlenp, cred_t *cr) 26728 { 26729 conn_t *connp = (conn_t *)proto_handle; 26730 tcp_t *tcp = connp->conn_tcp; 26731 26732 /* All Solaris components should pass a cred for this operation. */ 26733 ASSERT(cr != NULL); 26734 26735 ASSERT(connp->conn_upper_handle != NULL); 26736 26737 return (tcp_do_getsockname(tcp, addr, addrlenp)); 26738 } 26739 26740 /* 26741 * tcp_fallback 26742 * 26743 * A direct socket is falling back to using STREAMS. The queue 26744 * that is being passed down was created using tcp_open() with 26745 * the SO_FALLBACK flag set. As a result, the queue is not 26746 * associated with a conn, and the q_ptrs instead contain the 26747 * dev and minor area that should be used. 26748 * 26749 * The 'issocket' flag indicates whether the FireEngine 26750 * optimizations should be used. The common case would be that 26751 * optimizations are enabled, and they might be subsequently 26752 * disabled using the _SIOCSOCKFALLBACK ioctl. 26753 */ 26754 26755 /* 26756 * An active connection is falling back to TPI. Gather all the information 26757 * required by the STREAM head and TPI sonode and send it up. 26758 */ 26759 void 26760 tcp_fallback_noneager(tcp_t *tcp, mblk_t *stropt_mp, queue_t *q, 26761 boolean_t issocket, so_proto_quiesced_cb_t quiesced_cb) 26762 { 26763 conn_t *connp = tcp->tcp_connp; 26764 struct stroptions *stropt; 26765 struct T_capability_ack tca; 26766 struct sockaddr_in6 laddr, faddr; 26767 socklen_t laddrlen, faddrlen; 26768 short opts; 26769 int error; 26770 mblk_t *mp; 26771 26772 connp->conn_dev = (dev_t)RD(q)->q_ptr; 26773 connp->conn_minor_arena = WR(q)->q_ptr; 26774 26775 RD(q)->q_ptr = WR(q)->q_ptr = connp; 26776 26777 connp->conn_tcp->tcp_rq = connp->conn_rq = RD(q); 26778 connp->conn_tcp->tcp_wq = connp->conn_wq = WR(q); 26779 26780 WR(q)->q_qinfo = &tcp_sock_winit; 26781 26782 if (!issocket) 26783 tcp_use_pure_tpi(tcp); 26784 26785 /* 26786 * free the helper stream 26787 */ 26788 ip_free_helper_stream(connp); 26789 26790 /* 26791 * Notify the STREAM head about options 26792 */ 26793 DB_TYPE(stropt_mp) = M_SETOPTS; 26794 stropt = (struct stroptions *)stropt_mp->b_rptr; 26795 stropt_mp->b_wptr += sizeof (struct stroptions); 26796 stropt->so_flags = SO_HIWAT | SO_WROFF | SO_MAXBLK; 26797 26798 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 26799 tcp->tcp_tcps->tcps_wroff_xtra); 26800 if (tcp->tcp_snd_sack_ok) 26801 stropt->so_wroff += TCPOPT_MAX_SACK_LEN; 26802 stropt->so_hiwat = tcp->tcp_fused ? 26803 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 26804 MAX(tcp->tcp_recv_hiwater, tcp->tcp_tcps->tcps_sth_rcv_hiwat); 26805 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 26806 26807 putnext(RD(q), stropt_mp); 26808 26809 /* 26810 * Collect the information needed to sync with the sonode 26811 */ 26812 tcp_do_capability_ack(tcp, &tca, TC1_INFO|TC1_ACCEPTOR_ID); 26813 26814 laddrlen = faddrlen = sizeof (sin6_t); 26815 (void) tcp_do_getsockname(tcp, (struct sockaddr *)&laddr, &laddrlen); 26816 error = tcp_do_getpeername(tcp, (struct sockaddr *)&faddr, &faddrlen); 26817 if (error != 0) 26818 faddrlen = 0; 26819 26820 opts = 0; 26821 if (tcp->tcp_oobinline) 26822 opts |= SO_OOBINLINE; 26823 if (tcp->tcp_dontroute) 26824 opts |= SO_DONTROUTE; 26825 26826 /* 26827 * Notify the socket that the protocol is now quiescent, 26828 * and it's therefore safe move data from the socket 26829 * to the stream head. 26830 */ 26831 (*quiesced_cb)(connp->conn_upper_handle, q, &tca, 26832 (struct sockaddr *)&laddr, laddrlen, 26833 (struct sockaddr *)&faddr, faddrlen, opts); 26834 26835 while ((mp = tcp->tcp_rcv_list) != NULL) { 26836 tcp->tcp_rcv_list = mp->b_next; 26837 mp->b_next = NULL; 26838 putnext(q, mp); 26839 } 26840 tcp->tcp_rcv_last_head = NULL; 26841 tcp->tcp_rcv_last_tail = NULL; 26842 tcp->tcp_rcv_cnt = 0; 26843 } 26844 26845 /* 26846 * An eager is falling back to TPI. All we have to do is send 26847 * up a T_CONN_IND. 26848 */ 26849 void 26850 tcp_fallback_eager(tcp_t *eager, boolean_t direct_sockfs) 26851 { 26852 tcp_t *listener = eager->tcp_listener; 26853 mblk_t *mp = eager->tcp_conn.tcp_eager_conn_ind; 26854 26855 ASSERT(listener != NULL); 26856 ASSERT(mp != NULL); 26857 26858 eager->tcp_conn.tcp_eager_conn_ind = NULL; 26859 26860 /* 26861 * TLI/XTI applications will get confused by 26862 * sending eager as an option since it violates 26863 * the option semantics. So remove the eager as 26864 * option since TLI/XTI app doesn't need it anyway. 26865 */ 26866 if (!direct_sockfs) { 26867 struct T_conn_ind *conn_ind; 26868 26869 conn_ind = (struct T_conn_ind *)mp->b_rptr; 26870 conn_ind->OPT_length = 0; 26871 conn_ind->OPT_offset = 0; 26872 } 26873 26874 /* 26875 * Sockfs guarantees that the listener will not be closed 26876 * during fallback. So we can safely use the listener's queue. 26877 */ 26878 putnext(listener->tcp_rq, mp); 26879 } 26880 26881 int 26882 tcp_fallback(sock_lower_handle_t proto_handle, queue_t *q, 26883 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 26884 { 26885 tcp_t *tcp; 26886 conn_t *connp = (conn_t *)proto_handle; 26887 int error; 26888 mblk_t *stropt_mp; 26889 mblk_t *ordrel_mp; 26890 26891 tcp = connp->conn_tcp; 26892 26893 stropt_mp = allocb_wait(sizeof (struct stroptions), BPRI_HI, STR_NOSIG, 26894 NULL); 26895 26896 /* Pre-allocate the T_ordrel_ind mblk. */ 26897 ASSERT(tcp->tcp_ordrel_mp == NULL); 26898 ordrel_mp = allocb_wait(sizeof (struct T_ordrel_ind), BPRI_HI, 26899 STR_NOSIG, NULL); 26900 ordrel_mp->b_datap->db_type = M_PROTO; 26901 ((struct T_ordrel_ind *)ordrel_mp->b_rptr)->PRIM_type = T_ORDREL_IND; 26902 ordrel_mp->b_wptr += sizeof (struct T_ordrel_ind); 26903 26904 /* 26905 * Enter the squeue so that no new packets can come in 26906 */ 26907 error = squeue_synch_enter(connp->conn_sqp, connp, NULL); 26908 if (error != 0) { 26909 /* failed to enter, free all the pre-allocated messages. */ 26910 freeb(stropt_mp); 26911 freeb(ordrel_mp); 26912 /* 26913 * We cannot process the eager, so at least send out a 26914 * RST so the peer can reconnect. 26915 */ 26916 if (tcp->tcp_listener != NULL) { 26917 (void) tcp_eager_blowoff(tcp->tcp_listener, 26918 tcp->tcp_conn_req_seqnum); 26919 } 26920 return (ENOMEM); 26921 } 26922 26923 /* 26924 * Both endpoints must be of the same type (either STREAMS or 26925 * non-STREAMS) for fusion to be enabled. So if we are fused, 26926 * we have to unfuse. 26927 */ 26928 if (tcp->tcp_fused) 26929 tcp_unfuse(tcp); 26930 26931 /* 26932 * No longer a direct socket 26933 */ 26934 connp->conn_flags &= ~IPCL_NONSTR; 26935 tcp->tcp_ordrel_mp = ordrel_mp; 26936 26937 if (tcp->tcp_listener != NULL) { 26938 /* The eager will deal with opts when accept() is called */ 26939 freeb(stropt_mp); 26940 tcp_fallback_eager(tcp, direct_sockfs); 26941 } else { 26942 tcp_fallback_noneager(tcp, stropt_mp, q, direct_sockfs, 26943 quiesced_cb); 26944 } 26945 26946 /* 26947 * There should be atleast two ref's (IP + TCP) 26948 */ 26949 ASSERT(connp->conn_ref >= 2); 26950 squeue_synch_exit(connp->conn_sqp, connp); 26951 26952 return (0); 26953 } 26954 26955 /* ARGSUSED */ 26956 static void 26957 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2) 26958 { 26959 conn_t *connp = (conn_t *)arg; 26960 tcp_t *tcp = connp->conn_tcp; 26961 26962 freemsg(mp); 26963 26964 if (tcp->tcp_fused) 26965 tcp_unfuse(tcp); 26966 26967 if (tcp_xmit_end(tcp) != 0) { 26968 /* 26969 * We were crossing FINs and got a reset from 26970 * the other side. Just ignore it. 26971 */ 26972 if (tcp->tcp_debug) { 26973 (void) strlog(TCP_MOD_ID, 0, 1, 26974 SL_ERROR|SL_TRACE, 26975 "tcp_shutdown_output() out of state %s", 26976 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT)); 26977 } 26978 } 26979 } 26980 26981 /* ARGSUSED */ 26982 int 26983 tcp_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr) 26984 { 26985 conn_t *connp = (conn_t *)proto_handle; 26986 tcp_t *tcp = connp->conn_tcp; 26987 26988 ASSERT(connp->conn_upper_handle != NULL); 26989 26990 /* All Solaris components should pass a cred for this operation. */ 26991 ASSERT(cr != NULL); 26992 26993 /* 26994 * X/Open requires that we check the connected state. 26995 */ 26996 if (tcp->tcp_state < TCPS_SYN_SENT) 26997 return (ENOTCONN); 26998 26999 /* shutdown the send side */ 27000 if (how != SHUT_RD) { 27001 mblk_t *bp; 27002 27003 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 27004 CONN_INC_REF(connp); 27005 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, tcp_shutdown_output, 27006 connp, SQ_NODRAIN, SQTAG_TCP_SHUTDOWN_OUTPUT); 27007 27008 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27009 SOCK_OPCTL_SHUT_SEND, 0); 27010 } 27011 27012 /* shutdown the recv side */ 27013 if (how != SHUT_WR) 27014 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27015 SOCK_OPCTL_SHUT_RECV, 0); 27016 27017 return (0); 27018 } 27019 27020 /* 27021 * SOP_LISTEN() calls into tcp_listen(). 27022 */ 27023 /* ARGSUSED */ 27024 int 27025 tcp_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr) 27026 { 27027 conn_t *connp = (conn_t *)proto_handle; 27028 int error; 27029 squeue_t *sqp = connp->conn_sqp; 27030 27031 ASSERT(connp->conn_upper_handle != NULL); 27032 27033 /* All Solaris components should pass a cred for this operation. */ 27034 ASSERT(cr != NULL); 27035 27036 error = squeue_synch_enter(sqp, connp, NULL); 27037 if (error != 0) { 27038 /* failed to enter */ 27039 return (ENOBUFS); 27040 } 27041 27042 error = tcp_do_listen(connp, NULL, 0, backlog, cr, FALSE); 27043 if (error == 0) { 27044 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27045 SOCK_OPCTL_ENAB_ACCEPT, (uintptr_t)backlog); 27046 } else if (error < 0) { 27047 if (error == -TOUTSTATE) 27048 error = EINVAL; 27049 else 27050 error = proto_tlitosyserr(-error); 27051 } 27052 squeue_synch_exit(sqp, connp); 27053 return (error); 27054 } 27055 27056 static int 27057 tcp_do_listen(conn_t *connp, struct sockaddr *sa, socklen_t len, 27058 int backlog, cred_t *cr, boolean_t bind_to_req_port_only) 27059 { 27060 tcp_t *tcp = connp->conn_tcp; 27061 int error = 0; 27062 tcp_stack_t *tcps = tcp->tcp_tcps; 27063 27064 /* All Solaris components should pass a cred for this operation. */ 27065 ASSERT(cr != NULL); 27066 27067 if (tcp->tcp_state >= TCPS_BOUND) { 27068 if ((tcp->tcp_state == TCPS_BOUND || 27069 tcp->tcp_state == TCPS_LISTEN) && backlog > 0) { 27070 /* 27071 * Handle listen() increasing backlog. 27072 * This is more "liberal" then what the TPI spec 27073 * requires but is needed to avoid a t_unbind 27074 * when handling listen() since the port number 27075 * might be "stolen" between the unbind and bind. 27076 */ 27077 goto do_listen; 27078 } 27079 if (tcp->tcp_debug) { 27080 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27081 "tcp_listen: bad state, %d", tcp->tcp_state); 27082 } 27083 return (-TOUTSTATE); 27084 } else { 27085 if (sa == NULL) { 27086 sin6_t addr; 27087 sin_t *sin; 27088 sin6_t *sin6; 27089 27090 ASSERT(IPCL_IS_NONSTR(connp)); 27091 27092 /* Do an implicit bind: Request for a generic port. */ 27093 if (tcp->tcp_family == AF_INET) { 27094 len = sizeof (sin_t); 27095 sin = (sin_t *)&addr; 27096 *sin = sin_null; 27097 sin->sin_family = AF_INET; 27098 tcp->tcp_ipversion = IPV4_VERSION; 27099 } else { 27100 ASSERT(tcp->tcp_family == AF_INET6); 27101 len = sizeof (sin6_t); 27102 sin6 = (sin6_t *)&addr; 27103 *sin6 = sin6_null; 27104 sin6->sin6_family = AF_INET6; 27105 tcp->tcp_ipversion = IPV6_VERSION; 27106 } 27107 sa = (struct sockaddr *)&addr; 27108 } 27109 27110 error = tcp_bind_check(connp, sa, len, cr, 27111 bind_to_req_port_only); 27112 if (error) 27113 return (error); 27114 /* Fall through and do the fanout insertion */ 27115 } 27116 27117 do_listen: 27118 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN); 27119 tcp->tcp_conn_req_max = backlog; 27120 if (tcp->tcp_conn_req_max) { 27121 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 27122 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 27123 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 27124 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 27125 /* 27126 * If this is a listener, do not reset the eager list 27127 * and other stuffs. Note that we don't check if the 27128 * existing eager list meets the new tcp_conn_req_max 27129 * requirement. 27130 */ 27131 if (tcp->tcp_state != TCPS_LISTEN) { 27132 tcp->tcp_state = TCPS_LISTEN; 27133 /* Initialize the chain. Don't need the eager_lock */ 27134 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 27135 tcp->tcp_eager_next_drop_q0 = tcp; 27136 tcp->tcp_eager_prev_drop_q0 = tcp; 27137 tcp->tcp_second_ctimer_threshold = 27138 tcps->tcps_ip_abort_linterval; 27139 } 27140 } 27141 27142 /* 27143 * We can call ip_bind directly, the processing continues 27144 * in tcp_post_ip_bind(). 27145 * 27146 * We need to make sure that the conn_recv is set to a non-null 27147 * value before we insert the conn into the classifier table. 27148 * This is to avoid a race with an incoming packet which does an 27149 * ipcl_classify(). 27150 */ 27151 connp->conn_recv = tcp_conn_request; 27152 if (tcp->tcp_family == AF_INET) { 27153 error = ip_proto_bind_laddr_v4(connp, NULL, 27154 IPPROTO_TCP, tcp->tcp_bound_source, tcp->tcp_lport, B_TRUE); 27155 } else { 27156 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 27157 &tcp->tcp_bound_source_v6, tcp->tcp_lport, B_TRUE); 27158 } 27159 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 27160 } 27161 27162 void 27163 tcp_clr_flowctrl(sock_lower_handle_t proto_handle) 27164 { 27165 conn_t *connp = (conn_t *)proto_handle; 27166 tcp_t *tcp = connp->conn_tcp; 27167 mblk_t *mp; 27168 int error; 27169 27170 ASSERT(connp->conn_upper_handle != NULL); 27171 27172 /* 27173 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_clr_flowctrl() 27174 * is currently running. 27175 */ 27176 mutex_enter(&tcp->tcp_rsrv_mp_lock); 27177 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 27178 mutex_exit(&tcp->tcp_rsrv_mp_lock); 27179 return; 27180 } 27181 tcp->tcp_rsrv_mp = NULL; 27182 mutex_exit(&tcp->tcp_rsrv_mp_lock); 27183 27184 error = squeue_synch_enter(connp->conn_sqp, connp, mp); 27185 ASSERT(error == 0); 27186 27187 mutex_enter(&tcp->tcp_rsrv_mp_lock); 27188 tcp->tcp_rsrv_mp = mp; 27189 mutex_exit(&tcp->tcp_rsrv_mp_lock); 27190 27191 if (tcp->tcp_fused) { 27192 tcp_fuse_backenable(tcp); 27193 } else { 27194 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 27195 /* 27196 * Send back a window update immediately if TCP is above 27197 * ESTABLISHED state and the increase of the rcv window 27198 * that the other side knows is at least 1 MSS after flow 27199 * control is lifted. 27200 */ 27201 if (tcp->tcp_state >= TCPS_ESTABLISHED && 27202 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 27203 tcp_xmit_ctl(NULL, tcp, 27204 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 27205 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 27206 } 27207 } 27208 27209 squeue_synch_exit(connp->conn_sqp, connp); 27210 } 27211 27212 /* ARGSUSED */ 27213 int 27214 tcp_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg, 27215 int mode, int32_t *rvalp, cred_t *cr) 27216 { 27217 conn_t *connp = (conn_t *)proto_handle; 27218 int error; 27219 27220 ASSERT(connp->conn_upper_handle != NULL); 27221 27222 /* All Solaris components should pass a cred for this operation. */ 27223 ASSERT(cr != NULL); 27224 27225 switch (cmd) { 27226 case ND_SET: 27227 case ND_GET: 27228 case TCP_IOC_DEFAULT_Q: 27229 case _SIOCSOCKFALLBACK: 27230 case TCP_IOC_ABORT_CONN: 27231 case TI_GETPEERNAME: 27232 case TI_GETMYNAME: 27233 ip1dbg(("tcp_ioctl: cmd 0x%x on non sreams socket", 27234 cmd)); 27235 error = EINVAL; 27236 break; 27237 default: 27238 /* 27239 * Pass on to IP using helper stream 27240 */ 27241 error = ldi_ioctl(connp->conn_helper_info->iphs_handle, 27242 cmd, arg, mode, cr, rvalp); 27243 break; 27244 } 27245 return (error); 27246 } 27247 27248 sock_downcalls_t sock_tcp_downcalls = { 27249 tcp_activate, 27250 tcp_accept, 27251 tcp_bind, 27252 tcp_listen, 27253 tcp_connect, 27254 tcp_getpeername, 27255 tcp_getsockname, 27256 tcp_getsockopt, 27257 tcp_setsockopt, 27258 tcp_sendmsg, 27259 NULL, 27260 NULL, 27261 NULL, 27262 tcp_shutdown, 27263 tcp_clr_flowctrl, 27264 tcp_ioctl, 27265 tcp_close, 27266 }; 27267