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 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* Copyright (c) 1990 Mentat Inc. */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 const char tcp_version[] = "%Z%%M% %I% %E% SMI"; 30 31 32 #include <sys/types.h> 33 #include <sys/stream.h> 34 #include <sys/strsun.h> 35 #include <sys/strsubr.h> 36 #include <sys/stropts.h> 37 #include <sys/strlog.h> 38 #include <sys/strsun.h> 39 #define _SUN_TPI_VERSION 2 40 #include <sys/tihdr.h> 41 #include <sys/timod.h> 42 #include <sys/ddi.h> 43 #include <sys/sunddi.h> 44 #include <sys/suntpi.h> 45 #include <sys/xti_inet.h> 46 #include <sys/cmn_err.h> 47 #include <sys/debug.h> 48 #include <sys/sdt.h> 49 #include <sys/vtrace.h> 50 #include <sys/kmem.h> 51 #include <sys/ethernet.h> 52 #include <sys/cpuvar.h> 53 #include <sys/dlpi.h> 54 #include <sys/multidata.h> 55 #include <sys/multidata_impl.h> 56 #include <sys/pattr.h> 57 #include <sys/policy.h> 58 #include <sys/priv.h> 59 #include <sys/zone.h> 60 #include <sys/sunldi.h> 61 62 #include <sys/errno.h> 63 #include <sys/signal.h> 64 #include <sys/socket.h> 65 #include <sys/sockio.h> 66 #include <sys/isa_defs.h> 67 #include <sys/md5.h> 68 #include <sys/random.h> 69 #include <sys/sodirect.h> 70 #include <sys/uio.h> 71 #include <netinet/in.h> 72 #include <netinet/tcp.h> 73 #include <netinet/ip6.h> 74 #include <netinet/icmp6.h> 75 #include <net/if.h> 76 #include <net/route.h> 77 #include <inet/ipsec_impl.h> 78 79 #include <inet/common.h> 80 #include <inet/ip.h> 81 #include <inet/ip_impl.h> 82 #include <inet/ip6.h> 83 #include <inet/ip_ndp.h> 84 #include <inet/mi.h> 85 #include <inet/mib2.h> 86 #include <inet/nd.h> 87 #include <inet/optcom.h> 88 #include <inet/snmpcom.h> 89 #include <inet/kstatcom.h> 90 #include <inet/tcp.h> 91 #include <inet/tcp_impl.h> 92 #include <net/pfkeyv2.h> 93 #include <inet/ipsec_info.h> 94 #include <inet/ipdrop.h> 95 #include <inet/tcp_trace.h> 96 97 #include <inet/ipclassifier.h> 98 #include <inet/ip_ire.h> 99 #include <inet/ip_ftable.h> 100 #include <inet/ip_if.h> 101 #include <inet/ipp_common.h> 102 #include <inet/ip_netinfo.h> 103 #include <sys/squeue.h> 104 #include <inet/kssl/ksslapi.h> 105 #include <sys/tsol/label.h> 106 #include <sys/tsol/tnet.h> 107 #include <rpc/pmap_prot.h> 108 109 /* 110 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433) 111 * 112 * (Read the detailed design doc in PSARC case directory) 113 * 114 * The entire tcp state is contained in tcp_t and conn_t structure 115 * which are allocated in tandem using ipcl_conn_create() and passing 116 * IPCL_CONNTCP as a flag. We use 'conn_ref' and 'conn_lock' to protect 117 * the references on the tcp_t. The tcp_t structure is never compressed 118 * and packets always land on the correct TCP perimeter from the time 119 * eager is created till the time tcp_t dies (as such the old mentat 120 * TCP global queue is not used for detached state and no IPSEC checking 121 * is required). The global queue is still allocated to send out resets 122 * for connection which have no listeners and IP directly calls 123 * tcp_xmit_listeners_reset() which does any policy check. 124 * 125 * Protection and Synchronisation mechanism: 126 * 127 * The tcp data structure does not use any kind of lock for protecting 128 * its state but instead uses 'squeues' for mutual exclusion from various 129 * read and write side threads. To access a tcp member, the thread should 130 * always be behind squeue (via squeue_enter, squeue_enter_nodrain, or 131 * squeue_fill). Since the squeues allow a direct function call, caller 132 * can pass any tcp function having prototype of edesc_t as argument 133 * (different from traditional STREAMs model where packets come in only 134 * designated entry points). The list of functions that can be directly 135 * called via squeue are listed before the usual function prototype. 136 * 137 * Referencing: 138 * 139 * TCP is MT-Hot and we use a reference based scheme to make sure that the 140 * tcp structure doesn't disappear when its needed. When the application 141 * creates an outgoing connection or accepts an incoming connection, we 142 * start out with 2 references on 'conn_ref'. One for TCP and one for IP. 143 * The IP reference is just a symbolic reference since ip_tcpclose() 144 * looks at tcp structure after tcp_close_output() returns which could 145 * have dropped the last TCP reference. So as long as the connection is 146 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the 147 * conn_t. The classifier puts its own reference when the connection is 148 * inserted in listen or connected hash. Anytime a thread needs to enter 149 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr 150 * on write side or by doing a classify on read side and then puts a 151 * reference on the conn before doing squeue_enter/tryenter/fill. For 152 * read side, the classifier itself puts the reference under fanout lock 153 * to make sure that tcp can't disappear before it gets processed. The 154 * squeue will drop this reference automatically so the called function 155 * doesn't have to do a DEC_REF. 156 * 157 * Opening a new connection: 158 * 159 * The outgoing connection open is pretty simple. tcp_open() does the 160 * work in creating the conn/tcp structure and initializing it. The 161 * squeue assignment is done based on the CPU the application 162 * is running on. So for outbound connections, processing is always done 163 * on application CPU which might be different from the incoming CPU 164 * being interrupted by the NIC. An optimal way would be to figure out 165 * the NIC <-> CPU binding at listen time, and assign the outgoing 166 * connection to the squeue attached to the CPU that will be interrupted 167 * for incoming packets (we know the NIC based on the bind IP address). 168 * This might seem like a problem if more data is going out but the 169 * fact is that in most cases the transmit is ACK driven transmit where 170 * the outgoing data normally sits on TCP's xmit queue waiting to be 171 * transmitted. 172 * 173 * Accepting a connection: 174 * 175 * This is a more interesting case because of various races involved in 176 * establishing a eager in its own perimeter. Read the meta comment on 177 * top of tcp_conn_request(). But briefly, the squeue is picked by 178 * ip_tcp_input()/ip_fanout_tcp_v6() based on the interrupted CPU. 179 * 180 * Closing a connection: 181 * 182 * The close is fairly straight forward. tcp_close() calls tcp_close_output() 183 * via squeue to do the close and mark the tcp as detached if the connection 184 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its 185 * reference but tcp_close() drop IP's reference always. So if tcp was 186 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP 187 * and 1 because it is in classifier's connected hash. This is the condition 188 * we use to determine that its OK to clean up the tcp outside of squeue 189 * when time wait expires (check the ref under fanout and conn_lock and 190 * if it is 2, remove it from fanout hash and kill it). 191 * 192 * Although close just drops the necessary references and marks the 193 * tcp_detached state, tcp_close needs to know the tcp_detached has been 194 * set (under squeue) before letting the STREAM go away (because a 195 * inbound packet might attempt to go up the STREAM while the close 196 * has happened and tcp_detached is not set). So a special lock and 197 * flag is used along with a condition variable (tcp_closelock, tcp_closed, 198 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked 199 * tcp_detached. 200 * 201 * Special provisions and fast paths: 202 * 203 * We make special provision for (AF_INET, SOCK_STREAM) sockets which 204 * can't have 'ipv6_recvpktinfo' set and for these type of sockets, IP 205 * will never send a M_CTL to TCP. As such, ip_tcp_input() which handles 206 * all TCP packets from the wire makes a IPCL_IS_TCP4_CONNECTED_NO_POLICY 207 * check to send packets directly to tcp_rput_data via squeue. Everyone 208 * else comes through tcp_input() on the read side. 209 * 210 * We also make special provisions for sockfs by marking tcp_issocket 211 * whenever we have only sockfs on top of TCP. This allows us to skip 212 * putting the tcp in acceptor hash since a sockfs listener can never 213 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM 214 * since eager has already been allocated and the accept now happens 215 * on acceptor STREAM. There is a big blob of comment on top of 216 * tcp_conn_request explaining the new accept. When socket is POP'd, 217 * sockfs sends us an ioctl to mark the fact and we go back to old 218 * behaviour. Once tcp_issocket is unset, its never set for the 219 * life of that connection. 220 * 221 * In support of on-board asynchronous DMA hardware (e.g. Intel I/OAT) 222 * two consoldiation private KAPIs are used to enqueue M_DATA mblk_t's 223 * directly to the socket (sodirect) and start an asynchronous copyout 224 * to a user-land receive-side buffer (uioa) when a blocking socket read 225 * (e.g. read, recv, ...) is pending. 226 * 227 * This is accomplished when tcp_issocket is set and tcp_sodirect is not 228 * NULL so points to an sodirect_t and if marked enabled then we enqueue 229 * all mblk_t's directly to the socket. 230 * 231 * Further, if the sodirect_t sod_uioa and if marked enabled (due to a 232 * blocking socket read, e.g. user-land read, recv, ...) then an asynchronous 233 * copyout will be started directly to the user-land uio buffer. Also, as we 234 * have a pending read, TCP's push logic can take into account the number of 235 * bytes to be received and only awake the blocked read()er when the uioa_t 236 * byte count has been satisfied. 237 * 238 * IPsec notes : 239 * 240 * Since a packet is always executed on the correct TCP perimeter 241 * all IPsec processing is defered to IP including checking new 242 * connections and setting IPSEC policies for new connection. The 243 * only exception is tcp_xmit_listeners_reset() which is called 244 * directly from IP and needs to policy check to see if TH_RST 245 * can be sent out. 246 * 247 * PFHooks notes : 248 * 249 * For mdt case, one meta buffer contains multiple packets. Mblks for every 250 * packet are assembled and passed to the hooks. When packets are blocked, 251 * or boundary of any packet is changed, the mdt processing is stopped, and 252 * packets of the meta buffer are send to the IP path one by one. 253 */ 254 255 /* 256 * Values for squeue switch: 257 * 1: squeue_enter_nodrain 258 * 2: squeue_enter 259 * 3: squeue_fill 260 */ 261 int tcp_squeue_close = 2; /* Setable in /etc/system */ 262 int tcp_squeue_wput = 2; 263 264 squeue_func_t tcp_squeue_close_proc; 265 squeue_func_t tcp_squeue_wput_proc; 266 267 /* 268 * Macros for sodirect: 269 * 270 * SOD_PTR_ENTER(tcp, sodp) - for the tcp_t pointer "tcp" set the 271 * sodirect_t pointer "sodp" to the socket/tcp shared sodirect_t 272 * if it exists and is enabled, else to NULL. Note, in the current 273 * sodirect implementation the sod_lock must not be held across any 274 * STREAMS call (e.g. putnext) else a "recursive mutex_enter" PANIC 275 * will result as sod_lock is the streamhead stdata.sd_lock. 276 * 277 * SOD_NOT_ENABLED(tcp) - return true if not a sodirect tcp_t or the 278 * sodirect_t isn't enabled, usefull for ASSERT()ing that a recieve 279 * side tcp code path dealing with a tcp_rcv_list or putnext() isn't 280 * being used when sodirect code paths should be. 281 */ 282 283 #define SOD_PTR_ENTER(tcp, sodp) \ 284 (sodp) = (tcp)->tcp_sodirect; \ 285 \ 286 if ((sodp) != NULL) { \ 287 mutex_enter((sodp)->sod_lock); \ 288 if (!((sodp)->sod_state & SOD_ENABLED)) { \ 289 mutex_exit((sodp)->sod_lock); \ 290 (sodp) = NULL; \ 291 } \ 292 } 293 294 #define SOD_NOT_ENABLED(tcp) \ 295 ((tcp)->tcp_sodirect == NULL || \ 296 !((tcp)->tcp_sodirect->sod_state & SOD_ENABLED)) 297 298 /* 299 * This controls how tiny a write must be before we try to copy it 300 * into the the mblk on the tail of the transmit queue. Not much 301 * speedup is observed for values larger than sixteen. Zero will 302 * disable the optimisation. 303 */ 304 int tcp_tx_pull_len = 16; 305 306 /* 307 * TCP Statistics. 308 * 309 * How TCP statistics work. 310 * 311 * There are two types of statistics invoked by two macros. 312 * 313 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 314 * supposed to be used in non MT-hot paths of the code. 315 * 316 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 317 * supposed to be used for DEBUG purposes and may be used on a hot path. 318 * 319 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 320 * (use "kstat tcp" to get them). 321 * 322 * There is also additional debugging facility that marks tcp_clean_death() 323 * instances and saves them in tcp_t structure. It is triggered by 324 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 325 * tcp_clean_death() calls that counts the number of times each tag was hit. It 326 * is triggered by TCP_CLD_COUNTERS define. 327 * 328 * How to add new counters. 329 * 330 * 1) Add a field in the tcp_stat structure describing your counter. 331 * 2) Add a line in the template in tcp_kstat2_init() with the name 332 * of the counter. 333 * 334 * IMPORTANT!! - make sure that both are in sync !! 335 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 336 * 337 * Please avoid using private counters which are not kstat-exported. 338 * 339 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 340 * in tcp_t structure. 341 * 342 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 343 */ 344 345 #ifndef TCP_DEBUG_COUNTER 346 #ifdef DEBUG 347 #define TCP_DEBUG_COUNTER 1 348 #else 349 #define TCP_DEBUG_COUNTER 0 350 #endif 351 #endif 352 353 #define TCP_CLD_COUNTERS 0 354 355 #define TCP_TAG_CLEAN_DEATH 1 356 #define TCP_MAX_CLEAN_DEATH_TAG 32 357 358 #ifdef lint 359 static int _lint_dummy_; 360 #endif 361 362 #if TCP_CLD_COUNTERS 363 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 364 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 365 #elif defined(lint) 366 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 367 #else 368 #define TCP_CLD_STAT(x) 369 #endif 370 371 #if TCP_DEBUG_COUNTER 372 #define TCP_DBGSTAT(tcps, x) \ 373 atomic_add_64(&((tcps)->tcps_statistics.x.value.ui64), 1) 374 #define TCP_G_DBGSTAT(x) \ 375 atomic_add_64(&(tcp_g_statistics.x.value.ui64), 1) 376 #elif defined(lint) 377 #define TCP_DBGSTAT(tcps, x) ASSERT(_lint_dummy_ == 0); 378 #define TCP_G_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 379 #else 380 #define TCP_DBGSTAT(tcps, x) 381 #define TCP_G_DBGSTAT(x) 382 #endif 383 384 #define TCP_G_STAT(x) (tcp_g_statistics.x.value.ui64++) 385 386 tcp_g_stat_t tcp_g_statistics; 387 kstat_t *tcp_g_kstat; 388 389 /* 390 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 391 * tcp write side. 392 */ 393 #define CALL_IP_WPUT(connp, q, mp) { \ 394 tcp_stack_t *tcps; \ 395 \ 396 tcps = connp->conn_netstack->netstack_tcp; \ 397 ASSERT(((q)->q_flag & QREADR) == 0); \ 398 TCP_DBGSTAT(tcps, tcp_ip_output); \ 399 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 400 } 401 402 /* Macros for timestamp comparisons */ 403 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 404 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 405 406 /* 407 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 408 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 409 * by adding three components: a time component which grows by 1 every 4096 410 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 411 * a per-connection component which grows by 125000 for every new connection; 412 * and an "extra" component that grows by a random amount centered 413 * approximately on 64000. This causes the the ISS generator to cycle every 414 * 4.89 hours if no TCP connections are made, and faster if connections are 415 * made. 416 * 417 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 418 * components: a time component which grows by 250000 every second; and 419 * a per-connection component which grows by 125000 for every new connections. 420 * 421 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 422 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 423 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 424 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 425 * password. 426 */ 427 #define ISS_INCR 250000 428 #define ISS_NSEC_SHT 12 429 430 static sin_t sin_null; /* Zero address for quick clears */ 431 static sin6_t sin6_null; /* Zero address for quick clears */ 432 433 /* 434 * This implementation follows the 4.3BSD interpretation of the urgent 435 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 436 * incompatible changes in protocols like telnet and rlogin. 437 */ 438 #define TCP_OLD_URP_INTERPRETATION 1 439 440 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 441 (TCP_IS_DETACHED(tcp) && \ 442 (!(tcp)->tcp_hard_binding)) 443 444 /* 445 * TCP reassembly macros. We hide starting and ending sequence numbers in 446 * b_next and b_prev of messages on the reassembly queue. The messages are 447 * chained using b_cont. These macros are used in tcp_reass() so we don't 448 * have to see the ugly casts and assignments. 449 */ 450 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 451 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 452 (mblk_t *)(uintptr_t)(u)) 453 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 454 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 455 (mblk_t *)(uintptr_t)(u)) 456 457 /* 458 * Implementation of TCP Timers. 459 * ============================= 460 * 461 * INTERFACE: 462 * 463 * There are two basic functions dealing with tcp timers: 464 * 465 * timeout_id_t tcp_timeout(connp, func, time) 466 * clock_t tcp_timeout_cancel(connp, timeout_id) 467 * TCP_TIMER_RESTART(tcp, intvl) 468 * 469 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 470 * after 'time' ticks passed. The function called by timeout() must adhere to 471 * the same restrictions as a driver soft interrupt handler - it must not sleep 472 * or call other functions that might sleep. The value returned is the opaque 473 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 474 * cancel the request. The call to tcp_timeout() may fail in which case it 475 * returns zero. This is different from the timeout(9F) function which never 476 * fails. 477 * 478 * The call-back function 'func' always receives 'connp' as its single 479 * argument. It is always executed in the squeue corresponding to the tcp 480 * structure. The tcp structure is guaranteed to be present at the time the 481 * call-back is called. 482 * 483 * NOTE: The call-back function 'func' is never called if tcp is in 484 * the TCPS_CLOSED state. 485 * 486 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 487 * request. locks acquired by the call-back routine should not be held across 488 * the call to tcp_timeout_cancel() or a deadlock may result. 489 * 490 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 491 * Otherwise, it returns an integer value greater than or equal to 0. In 492 * particular, if the call-back function is already placed on the squeue, it can 493 * not be canceled. 494 * 495 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 496 * within squeue context corresponding to the tcp instance. Since the 497 * call-back is also called via the same squeue, there are no race 498 * conditions described in untimeout(9F) manual page since all calls are 499 * strictly serialized. 500 * 501 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 502 * stored in tcp_timer_tid and starts a new one using 503 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 504 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 505 * field. 506 * 507 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 508 * call-back may still be called, so it is possible tcp_timer() will be 509 * called several times. This should not be a problem since tcp_timer() 510 * should always check the tcp instance state. 511 * 512 * 513 * IMPLEMENTATION: 514 * 515 * TCP timers are implemented using three-stage process. The call to 516 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 517 * when the timer expires. The tcp_timer_callback() arranges the call of the 518 * tcp_timer_handler() function via squeue corresponding to the tcp 519 * instance. The tcp_timer_handler() calls actual requested timeout call-back 520 * and passes tcp instance as an argument to it. Information is passed between 521 * stages using the tcp_timer_t structure which contains the connp pointer, the 522 * tcp call-back to call and the timeout id returned by the timeout(9F). 523 * 524 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 525 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 526 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 527 * returns the pointer to this mblk. 528 * 529 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 530 * looks like a normal mblk without actual dblk attached to it. 531 * 532 * To optimize performance each tcp instance holds a small cache of timer 533 * mblocks. In the current implementation it caches up to two timer mblocks per 534 * tcp instance. The cache is preserved over tcp frees and is only freed when 535 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 536 * timer processing happens on a corresponding squeue, the cache manipulation 537 * does not require any locks. Experiments show that majority of timer mblocks 538 * allocations are satisfied from the tcp cache and do not involve kmem calls. 539 * 540 * The tcp_timeout() places a refhold on the connp instance which guarantees 541 * that it will be present at the time the call-back function fires. The 542 * tcp_timer_handler() drops the reference after calling the call-back, so the 543 * call-back function does not need to manipulate the references explicitly. 544 */ 545 546 typedef struct tcp_timer_s { 547 conn_t *connp; 548 void (*tcpt_proc)(void *); 549 timeout_id_t tcpt_tid; 550 } tcp_timer_t; 551 552 static kmem_cache_t *tcp_timercache; 553 kmem_cache_t *tcp_sack_info_cache; 554 kmem_cache_t *tcp_iphc_cache; 555 556 /* 557 * For scalability, we must not run a timer for every TCP connection 558 * in TIME_WAIT state. To see why, consider (for time wait interval of 559 * 4 minutes): 560 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 561 * 562 * This list is ordered by time, so you need only delete from the head 563 * until you get to entries which aren't old enough to delete yet. 564 * The list consists of only the detached TIME_WAIT connections. 565 * 566 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 567 * becomes detached TIME_WAIT (either by changing the state and already 568 * being detached or the other way around). This means that the TIME_WAIT 569 * state can be extended (up to doubled) if the connection doesn't become 570 * detached for a long time. 571 * 572 * The list manipulations (including tcp_time_wait_next/prev) 573 * are protected by the tcp_time_wait_lock. The content of the 574 * detached TIME_WAIT connections is protected by the normal perimeters. 575 * 576 * This list is per squeue and squeues are shared across the tcp_stack_t's. 577 * Things on tcp_time_wait_head remain associated with the tcp_stack_t 578 * and conn_netstack. 579 * The tcp_t's that are added to tcp_free_list are disassociated and 580 * have NULL tcp_tcps and conn_netstack pointers. 581 */ 582 typedef struct tcp_squeue_priv_s { 583 kmutex_t tcp_time_wait_lock; 584 timeout_id_t tcp_time_wait_tid; 585 tcp_t *tcp_time_wait_head; 586 tcp_t *tcp_time_wait_tail; 587 tcp_t *tcp_free_list; 588 uint_t tcp_free_list_cnt; 589 } tcp_squeue_priv_t; 590 591 /* 592 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 593 * Running it every 5 seconds seems to give the best results. 594 */ 595 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 596 597 /* 598 * To prevent memory hog, limit the number of entries in tcp_free_list 599 * to 1% of available memory / number of cpus 600 */ 601 uint_t tcp_free_list_max_cnt = 0; 602 603 #define TCP_XMIT_LOWATER 4096 604 #define TCP_XMIT_HIWATER 49152 605 #define TCP_RECV_LOWATER 2048 606 #define TCP_RECV_HIWATER 49152 607 608 /* 609 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 610 */ 611 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 612 613 #define TIDUSZ 4096 /* transport interface data unit size */ 614 615 /* 616 * Bind hash list size and has function. It has to be a power of 2 for 617 * hashing. 618 */ 619 #define TCP_BIND_FANOUT_SIZE 512 620 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 621 /* 622 * Size of listen and acceptor hash list. It has to be a power of 2 for 623 * hashing. 624 */ 625 #define TCP_FANOUT_SIZE 256 626 627 #ifdef _ILP32 628 #define TCP_ACCEPTOR_HASH(accid) \ 629 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 630 #else 631 #define TCP_ACCEPTOR_HASH(accid) \ 632 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 633 #endif /* _ILP32 */ 634 635 #define IP_ADDR_CACHE_SIZE 2048 636 #define IP_ADDR_CACHE_HASH(faddr) \ 637 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 638 639 /* Hash for HSPs uses all 32 bits, since both networks and hosts are in table */ 640 #define TCP_HSP_HASH_SIZE 256 641 642 #define TCP_HSP_HASH(addr) \ 643 (((addr>>24) ^ (addr >>16) ^ \ 644 (addr>>8) ^ (addr)) % TCP_HSP_HASH_SIZE) 645 646 /* 647 * TCP options struct returned from tcp_parse_options. 648 */ 649 typedef struct tcp_opt_s { 650 uint32_t tcp_opt_mss; 651 uint32_t tcp_opt_wscale; 652 uint32_t tcp_opt_ts_val; 653 uint32_t tcp_opt_ts_ecr; 654 tcp_t *tcp; 655 } tcp_opt_t; 656 657 /* 658 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 659 */ 660 661 #ifdef _BIG_ENDIAN 662 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 663 (TCPOPT_TSTAMP << 8) | 10) 664 #else 665 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 666 (TCPOPT_NOP << 8) | TCPOPT_NOP) 667 #endif 668 669 /* 670 * Flags returned from tcp_parse_options. 671 */ 672 #define TCP_OPT_MSS_PRESENT 1 673 #define TCP_OPT_WSCALE_PRESENT 2 674 #define TCP_OPT_TSTAMP_PRESENT 4 675 #define TCP_OPT_SACK_OK_PRESENT 8 676 #define TCP_OPT_SACK_PRESENT 16 677 678 /* TCP option length */ 679 #define TCPOPT_NOP_LEN 1 680 #define TCPOPT_MAXSEG_LEN 4 681 #define TCPOPT_WS_LEN 3 682 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 683 #define TCPOPT_TSTAMP_LEN 10 684 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 685 #define TCPOPT_SACK_OK_LEN 2 686 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 687 #define TCPOPT_REAL_SACK_LEN 4 688 #define TCPOPT_MAX_SACK_LEN 36 689 #define TCPOPT_HEADER_LEN 2 690 691 /* TCP cwnd burst factor. */ 692 #define TCP_CWND_INFINITE 65535 693 #define TCP_CWND_SS 3 694 #define TCP_CWND_NORMAL 5 695 696 /* Maximum TCP initial cwin (start/restart). */ 697 #define TCP_MAX_INIT_CWND 8 698 699 /* 700 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 701 * either tcp_slow_start_initial or tcp_slow_start_after idle 702 * depending on the caller. If the upper layer has not used the 703 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 704 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 705 * If the upper layer has changed set the tcp_init_cwnd, just use 706 * it to calculate the tcp_cwnd. 707 */ 708 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 709 { \ 710 if ((tcp)->tcp_init_cwnd == 0) { \ 711 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 712 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 713 } else { \ 714 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 715 } \ 716 tcp->tcp_cwnd_cnt = 0; \ 717 } 718 719 /* TCP Timer control structure */ 720 typedef struct tcpt_s { 721 pfv_t tcpt_pfv; /* The routine we are to call */ 722 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 723 } tcpt_t; 724 725 /* Host Specific Parameter structure */ 726 typedef struct tcp_hsp { 727 struct tcp_hsp *tcp_hsp_next; 728 in6_addr_t tcp_hsp_addr_v6; 729 in6_addr_t tcp_hsp_subnet_v6; 730 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 731 int32_t tcp_hsp_sendspace; 732 int32_t tcp_hsp_recvspace; 733 int32_t tcp_hsp_tstamp; 734 } tcp_hsp_t; 735 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 736 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 737 738 /* 739 * Functions called directly via squeue having a prototype of edesc_t. 740 */ 741 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 742 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 743 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 744 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 745 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 746 void tcp_input(void *arg, mblk_t *mp, void *arg2); 747 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 748 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 749 void tcp_output(void *arg, mblk_t *mp, void *arg2); 750 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 751 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 752 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 753 754 755 /* Prototype for TCP functions */ 756 static void tcp_random_init(void); 757 int tcp_random(void); 758 static void tcp_accept(tcp_t *tcp, mblk_t *mp); 759 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 760 tcp_t *eager); 761 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 762 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 763 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 764 boolean_t user_specified); 765 static void tcp_closei_local(tcp_t *tcp); 766 static void tcp_close_detached(tcp_t *tcp); 767 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 768 mblk_t *idmp, mblk_t **defermp); 769 static void tcp_connect(tcp_t *tcp, mblk_t *mp); 770 static void tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, 771 in_port_t dstport, uint_t srcid); 772 static void tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 773 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 774 uint32_t scope_id); 775 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 776 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 777 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 778 static char *tcp_display(tcp_t *tcp, char *, char); 779 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 780 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 781 static void tcp_eager_unlink(tcp_t *tcp); 782 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 783 int unixerr); 784 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 785 int tlierr, int unixerr); 786 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 787 cred_t *cr); 788 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 789 char *value, caddr_t cp, cred_t *cr); 790 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 791 char *value, caddr_t cp, cred_t *cr); 792 static int tcp_tpistate(tcp_t *tcp); 793 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 794 int caller_holds_lock); 795 static void tcp_bind_hash_remove(tcp_t *tcp); 796 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 797 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 798 static void tcp_acceptor_hash_remove(tcp_t *tcp); 799 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 800 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 801 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 802 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 803 void tcp_g_q_setup(tcp_stack_t *); 804 void tcp_g_q_create(tcp_stack_t *); 805 void tcp_g_q_destroy(tcp_stack_t *); 806 static int tcp_header_init_ipv4(tcp_t *tcp); 807 static int tcp_header_init_ipv6(tcp_t *tcp); 808 int tcp_init(tcp_t *tcp, queue_t *q); 809 static int tcp_init_values(tcp_t *tcp); 810 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 811 static mblk_t *tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, 812 t_scalar_t addr_length); 813 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 814 static void tcp_ip_notify(tcp_t *tcp); 815 static mblk_t *tcp_ire_mp(mblk_t *mp); 816 static void tcp_iss_init(tcp_t *tcp); 817 static void tcp_keepalive_killer(void *arg); 818 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 819 static void tcp_mss_set(tcp_t *tcp, uint32_t size, boolean_t do_ss); 820 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 821 int *do_disconnectp, int *t_errorp, int *sys_errorp); 822 static boolean_t tcp_allow_connopt_set(int level, int name); 823 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 824 int tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 825 int tcp_opt_set(queue_t *q, uint_t optset_context, int level, 826 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 827 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 828 mblk_t *mblk); 829 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 830 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 831 uchar_t *ptr, uint_t len); 832 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 833 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 834 tcp_stack_t *); 835 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 836 caddr_t cp, cred_t *cr); 837 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 838 caddr_t cp, cred_t *cr); 839 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 840 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 841 caddr_t cp, cred_t *cr); 842 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 843 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 844 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 845 static void tcp_reinit(tcp_t *tcp); 846 static void tcp_reinit_values(tcp_t *tcp); 847 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 848 tcp_t *thisstream, cred_t *cr); 849 850 static uint_t tcp_rcv_drain(queue_t *q, tcp_t *tcp); 851 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 852 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 853 static void tcp_ss_rexmit(tcp_t *tcp); 854 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 855 static void tcp_process_options(tcp_t *, tcph_t *); 856 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 857 static void tcp_rsrv(queue_t *q); 858 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 859 static int tcp_snmp_state(tcp_t *tcp); 860 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 861 cred_t *cr); 862 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 863 cred_t *cr); 864 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 865 cred_t *cr); 866 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 867 cred_t *cr); 868 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 869 cred_t *cr); 870 static int tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, 871 caddr_t cp, cred_t *cr); 872 static int tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, 873 caddr_t cp, cred_t *cr); 874 static int tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, 875 cred_t *cr); 876 static void tcp_timer(void *arg); 877 static void tcp_timer_callback(void *); 878 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 879 boolean_t random); 880 static in_port_t tcp_get_next_priv_port(const tcp_t *); 881 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 882 void tcp_wput_accept(queue_t *q, mblk_t *mp); 883 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 884 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 885 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 886 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 887 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 888 const int num_sack_blk, int *usable, uint_t *snxt, 889 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 890 const int mdt_thres); 891 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 892 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 893 const int num_sack_blk, int *usable, uint_t *snxt, 894 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 895 const int mdt_thres); 896 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 897 int num_sack_blk); 898 static void tcp_wsrv(queue_t *q); 899 static int tcp_xmit_end(tcp_t *tcp); 900 static void tcp_ack_timer(void *arg); 901 static mblk_t *tcp_ack_mp(tcp_t *tcp); 902 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 903 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 904 zoneid_t zoneid, tcp_stack_t *, conn_t *connp); 905 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 906 uint32_t ack, int ctl); 907 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *); 908 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr, tcp_stack_t *); 909 static int setmaxps(queue_t *q, int maxpsz); 910 static void tcp_set_rto(tcp_t *, time_t); 911 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 912 boolean_t, boolean_t); 913 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 914 boolean_t ipsec_mctl); 915 static mblk_t *tcp_setsockopt_mp(int level, int cmd, 916 char *opt, int optlen); 917 static int tcp_build_hdrs(queue_t *, tcp_t *); 918 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 919 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 920 tcph_t *tcph); 921 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 922 boolean_t tcp_reserved_port_add(int, in_port_t *, in_port_t *); 923 boolean_t tcp_reserved_port_del(in_port_t, in_port_t); 924 boolean_t tcp_reserved_port_check(in_port_t, tcp_stack_t *); 925 static tcp_t *tcp_alloc_temp_tcp(in_port_t, tcp_stack_t *); 926 static int tcp_reserved_port_list(queue_t *, mblk_t *, caddr_t, cred_t *); 927 static mblk_t *tcp_mdt_info_mp(mblk_t *); 928 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 929 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 930 const boolean_t, const uint32_t, const uint32_t, 931 const uint32_t, const uint32_t, tcp_stack_t *); 932 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 933 const uint_t, const uint_t, boolean_t *); 934 static mblk_t *tcp_lso_info_mp(mblk_t *); 935 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 936 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 937 extern mblk_t *tcp_timermp_alloc(int); 938 extern void tcp_timermp_free(tcp_t *); 939 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 940 static void tcp_stop_lingering(tcp_t *tcp); 941 static void tcp_close_linger_timeout(void *arg); 942 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 943 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 944 static void tcp_stack_fini(netstackid_t stackid, void *arg); 945 static void *tcp_g_kstat_init(tcp_g_stat_t *); 946 static void tcp_g_kstat_fini(kstat_t *); 947 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 948 static void tcp_kstat_fini(netstackid_t, kstat_t *); 949 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 950 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 951 static int tcp_kstat_update(kstat_t *kp, int rw); 952 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 953 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 954 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 955 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 956 tcph_t *tcph, mblk_t *idmp); 957 static squeue_func_t tcp_squeue_switch(int); 958 959 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t); 960 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *); 961 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *); 962 static int tcp_close(queue_t *, int); 963 static int tcpclose_accept(queue_t *); 964 965 static void tcp_squeue_add(squeue_t *); 966 static boolean_t tcp_zcopy_check(tcp_t *); 967 static void tcp_zcopy_notify(tcp_t *); 968 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 969 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 970 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 971 972 extern void tcp_kssl_input(tcp_t *, mblk_t *); 973 974 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 975 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 976 977 /* 978 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 979 * 980 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 981 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 982 * (defined in tcp.h) needs to be filled in and passed into the kernel 983 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 984 * structure contains the four-tuple of a TCP connection and a range of TCP 985 * states (specified by ac_start and ac_end). The use of wildcard addresses 986 * and ports is allowed. Connections with a matching four tuple and a state 987 * within the specified range will be aborted. The valid states for the 988 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 989 * inclusive. 990 * 991 * An application which has its connection aborted by this ioctl will receive 992 * an error that is dependent on the connection state at the time of the abort. 993 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 994 * though a RST packet has been received. If the connection state is equal to 995 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 996 * and all resources associated with the connection will be freed. 997 */ 998 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 999 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 1000 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 1001 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *, tcp_stack_t *tcps); 1002 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 1003 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 1004 boolean_t, tcp_stack_t *); 1005 1006 static struct module_info tcp_rinfo = { 1007 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 1008 }; 1009 1010 static struct module_info tcp_winfo = { 1011 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 1012 }; 1013 1014 /* 1015 * Entry points for TCP as a device. The normal case which supports 1016 * the TCP functionality. 1017 * We have separate open functions for the /dev/tcp and /dev/tcp6 devices. 1018 */ 1019 struct qinit tcp_rinitv4 = { 1020 NULL, (pfi_t)tcp_rsrv, tcp_openv4, tcp_close, NULL, &tcp_rinfo 1021 }; 1022 1023 struct qinit tcp_rinitv6 = { 1024 NULL, (pfi_t)tcp_rsrv, tcp_openv6, tcp_close, NULL, &tcp_rinfo 1025 }; 1026 1027 struct qinit tcp_winit = { 1028 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1029 }; 1030 1031 /* Initial entry point for TCP in socket mode. */ 1032 struct qinit tcp_sock_winit = { 1033 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1034 }; 1035 1036 /* 1037 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 1038 * an accept. Avoid allocating data structures since eager has already 1039 * been created. 1040 */ 1041 struct qinit tcp_acceptor_rinit = { 1042 NULL, (pfi_t)tcp_rsrv, NULL, tcpclose_accept, NULL, &tcp_winfo 1043 }; 1044 1045 struct qinit tcp_acceptor_winit = { 1046 (pfi_t)tcp_wput_accept, NULL, NULL, NULL, NULL, &tcp_winfo 1047 }; 1048 1049 /* 1050 * Entry points for TCP loopback (read side only) 1051 * The open routine is only used for reopens, thus no need to 1052 * have a separate one for tcp_openv6. 1053 */ 1054 struct qinit tcp_loopback_rinit = { 1055 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_openv4, tcp_close, (pfi_t)0, 1056 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 1057 }; 1058 1059 /* For AF_INET aka /dev/tcp */ 1060 struct streamtab tcpinfov4 = { 1061 &tcp_rinitv4, &tcp_winit 1062 }; 1063 1064 /* For AF_INET6 aka /dev/tcp6 */ 1065 struct streamtab tcpinfov6 = { 1066 &tcp_rinitv6, &tcp_winit 1067 }; 1068 1069 /* 1070 * Have to ensure that tcp_g_q_close is not done by an 1071 * interrupt thread. 1072 */ 1073 static taskq_t *tcp_taskq; 1074 1075 /* 1076 * TCP has a private interface for other kernel modules to reserve a 1077 * port range for them to use. Once reserved, TCP will not use any ports 1078 * in the range. This interface relies on the TCP_EXCLBIND feature. If 1079 * the semantics of TCP_EXCLBIND is changed, implementation of this interface 1080 * has to be verified. 1081 * 1082 * There can be TCP_RESERVED_PORTS_ARRAY_MAX_SIZE port ranges. Each port 1083 * range can cover at most TCP_RESERVED_PORTS_RANGE_MAX ports. A port 1084 * range is [port a, port b] inclusive. And each port range is between 1085 * TCP_LOWESET_RESERVED_PORT and TCP_LARGEST_RESERVED_PORT inclusive. 1086 * 1087 * Note that the default anonymous port range starts from 32768. There is 1088 * no port "collision" between that and the reserved port range. If there 1089 * is port collision (because the default smallest anonymous port is lowered 1090 * or some apps specifically bind to ports in the reserved port range), the 1091 * system may not be able to reserve a port range even there are enough 1092 * unbound ports as a reserved port range contains consecutive ports . 1093 */ 1094 #define TCP_RESERVED_PORTS_ARRAY_MAX_SIZE 5 1095 #define TCP_RESERVED_PORTS_RANGE_MAX 1000 1096 #define TCP_SMALLEST_RESERVED_PORT 10240 1097 #define TCP_LARGEST_RESERVED_PORT 20480 1098 1099 /* Structure to represent those reserved port ranges. */ 1100 typedef struct tcp_rport_s { 1101 in_port_t lo_port; 1102 in_port_t hi_port; 1103 tcp_t **temp_tcp_array; 1104 } tcp_rport_t; 1105 1106 /* Setable only in /etc/system. Move to ndd? */ 1107 boolean_t tcp_icmp_source_quench = B_FALSE; 1108 1109 /* 1110 * Following assumes TPI alignment requirements stay along 32 bit 1111 * boundaries 1112 */ 1113 #define ROUNDUP32(x) \ 1114 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1115 1116 /* Template for response to info request. */ 1117 static struct T_info_ack tcp_g_t_info_ack = { 1118 T_INFO_ACK, /* PRIM_type */ 1119 0, /* TSDU_size */ 1120 T_INFINITE, /* ETSDU_size */ 1121 T_INVALID, /* CDATA_size */ 1122 T_INVALID, /* DDATA_size */ 1123 sizeof (sin_t), /* ADDR_size */ 1124 0, /* OPT_size - not initialized here */ 1125 TIDUSZ, /* TIDU_size */ 1126 T_COTS_ORD, /* SERV_type */ 1127 TCPS_IDLE, /* CURRENT_state */ 1128 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1129 }; 1130 1131 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1132 T_INFO_ACK, /* PRIM_type */ 1133 0, /* TSDU_size */ 1134 T_INFINITE, /* ETSDU_size */ 1135 T_INVALID, /* CDATA_size */ 1136 T_INVALID, /* DDATA_size */ 1137 sizeof (sin6_t), /* ADDR_size */ 1138 0, /* OPT_size - not initialized here */ 1139 TIDUSZ, /* TIDU_size */ 1140 T_COTS_ORD, /* SERV_type */ 1141 TCPS_IDLE, /* CURRENT_state */ 1142 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1143 }; 1144 1145 #define MS 1L 1146 #define SECONDS (1000 * MS) 1147 #define MINUTES (60 * SECONDS) 1148 #define HOURS (60 * MINUTES) 1149 #define DAYS (24 * HOURS) 1150 1151 #define PARAM_MAX (~(uint32_t)0) 1152 1153 /* Max size IP datagram is 64k - 1 */ 1154 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1155 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1156 /* Max of the above */ 1157 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1158 1159 /* Largest TCP port number */ 1160 #define TCP_MAX_PORT (64 * 1024 - 1) 1161 1162 /* 1163 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1164 * layer header. It has to be a multiple of 4. 1165 */ 1166 static tcpparam_t lcl_tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1167 #define tcps_wroff_xtra tcps_wroff_xtra_param->tcp_param_val 1168 1169 /* 1170 * All of these are alterable, within the min/max values given, at run time. 1171 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1172 * per the TCP spec. 1173 */ 1174 /* BEGIN CSTYLED */ 1175 static tcpparam_t lcl_tcp_param_arr[] = { 1176 /*min max value name */ 1177 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1178 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1179 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1180 { 1, 1024, 1, "tcp_conn_req_min" }, 1181 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1182 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1183 { 0, 10, 0, "tcp_debug" }, 1184 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1185 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1186 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1187 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1188 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1189 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1190 { 1, 255, 64, "tcp_ipv4_ttl"}, 1191 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1192 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1193 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1194 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1195 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1196 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1197 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1198 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1199 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1200 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1201 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1202 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1203 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1204 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1205 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1206 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1207 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1208 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1209 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1210 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1211 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1212 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1213 { 0, TCP_MSS_MAX, 64, "tcp_co_min"}, 1214 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1215 /* 1216 * Question: What default value should I set for tcp_strong_iss? 1217 */ 1218 { 0, 2, 1, "tcp_strong_iss"}, 1219 { 0, 65536, 20, "tcp_rtt_updates"}, 1220 { 0, 1, 1, "tcp_wscale_always"}, 1221 { 0, 1, 0, "tcp_tstamp_always"}, 1222 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1223 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1224 { 0, 16, 2, "tcp_deferred_acks_max"}, 1225 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1226 { 1, 4, 4, "tcp_slow_start_initial"}, 1227 { 10*MS, 50*MS, 20*MS, "tcp_co_timer_interval"}, 1228 { 0, 2, 2, "tcp_sack_permitted"}, 1229 { 0, 1, 0, "tcp_trace"}, 1230 { 0, 1, 1, "tcp_compression_enabled"}, 1231 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1232 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1233 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1234 { 0, 1, 0, "tcp_rev_src_routes"}, 1235 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1236 { 100*MS, 60*SECONDS, 1*SECONDS, "tcp_ndd_get_info_interval"}, 1237 { 0, 16, 8, "tcp_local_dacks_max"}, 1238 { 0, 2, 1, "tcp_ecn_permitted"}, 1239 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1240 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1241 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1242 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1243 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1244 }; 1245 /* END CSTYLED */ 1246 1247 /* 1248 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1249 * each header fragment in the header buffer. Each parameter value has 1250 * to be a multiple of 4 (32-bit aligned). 1251 */ 1252 static tcpparam_t lcl_tcp_mdt_head_param = 1253 { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1254 static tcpparam_t lcl_tcp_mdt_tail_param = 1255 { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1256 #define tcps_mdt_hdr_head_min tcps_mdt_head_param->tcp_param_val 1257 #define tcps_mdt_hdr_tail_min tcps_mdt_tail_param->tcp_param_val 1258 1259 /* 1260 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1261 * the maximum number of payload buffers associated per Multidata. 1262 */ 1263 static tcpparam_t lcl_tcp_mdt_max_pbufs_param = 1264 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1265 #define tcps_mdt_max_pbufs tcps_mdt_max_pbufs_param->tcp_param_val 1266 1267 /* Round up the value to the nearest mss. */ 1268 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1269 1270 /* 1271 * Set ECN capable transport (ECT) code point in IP header. 1272 * 1273 * Note that there are 2 ECT code points '01' and '10', which are called 1274 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1275 * point ECT(0) for TCP as described in RFC 2481. 1276 */ 1277 #define SET_ECT(tcp, iph) \ 1278 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1279 /* We need to clear the code point first. */ \ 1280 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1281 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1282 } else { \ 1283 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1284 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1285 } 1286 1287 /* 1288 * The format argument to pass to tcp_display(). 1289 * DISP_PORT_ONLY means that the returned string has only port info. 1290 * DISP_ADDR_AND_PORT means that the returned string also contains the 1291 * remote and local IP address. 1292 */ 1293 #define DISP_PORT_ONLY 1 1294 #define DISP_ADDR_AND_PORT 2 1295 1296 #define NDD_TOO_QUICK_MSG \ 1297 "ndd get info rate too high for non-privileged users, try again " \ 1298 "later.\n" 1299 #define NDD_OUT_OF_BUF_MSG "<< Out of buffer >>\n" 1300 1301 #define IS_VMLOANED_MBLK(mp) \ 1302 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1303 1304 1305 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1306 boolean_t tcp_mdt_chain = B_TRUE; 1307 1308 /* 1309 * MDT threshold in the form of effective send MSS multiplier; we take 1310 * the MDT path if the amount of unsent data exceeds the threshold value 1311 * (default threshold is 1*SMSS). 1312 */ 1313 uint_t tcp_mdt_smss_threshold = 1; 1314 1315 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1316 1317 /* 1318 * Forces all connections to obey the value of the tcps_maxpsz_multiplier 1319 * tunable settable via NDD. Otherwise, the per-connection behavior is 1320 * determined dynamically during tcp_adapt_ire(), which is the default. 1321 */ 1322 boolean_t tcp_static_maxpsz = B_FALSE; 1323 1324 /* Setable in /etc/system */ 1325 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1326 uint32_t tcp_random_anon_port = 1; 1327 1328 /* 1329 * To reach to an eager in Q0 which can be dropped due to an incoming 1330 * new SYN request when Q0 is full, a new doubly linked list is 1331 * introduced. This list allows to select an eager from Q0 in O(1) time. 1332 * This is needed to avoid spending too much time walking through the 1333 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1334 * this new list has to be a member of Q0. 1335 * This list is headed by listener's tcp_t. When the list is empty, 1336 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1337 * of listener's tcp_t point to listener's tcp_t itself. 1338 * 1339 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1340 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1341 * These macros do not affect the eager's membership to Q0. 1342 */ 1343 1344 1345 #define MAKE_DROPPABLE(listener, eager) \ 1346 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1347 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1348 = (eager); \ 1349 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1350 (eager)->tcp_eager_next_drop_q0 = \ 1351 (listener)->tcp_eager_next_drop_q0; \ 1352 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1353 } 1354 1355 #define MAKE_UNDROPPABLE(eager) \ 1356 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1357 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1358 = (eager)->tcp_eager_prev_drop_q0; \ 1359 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1360 = (eager)->tcp_eager_next_drop_q0; \ 1361 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1362 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1363 } 1364 1365 /* 1366 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1367 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1368 * data, TCP will not respond with an ACK. RFC 793 requires that 1369 * TCP responds with an ACK for such a bogus ACK. By not following 1370 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1371 * an attacker successfully spoofs an acceptable segment to our 1372 * peer; or when our peer is "confused." 1373 */ 1374 uint32_t tcp_drop_ack_unsent_cnt = 10; 1375 1376 /* 1377 * Hook functions to enable cluster networking 1378 * On non-clustered systems these vectors must always be NULL. 1379 */ 1380 1381 void (*cl_inet_listen)(uint8_t protocol, sa_family_t addr_family, 1382 uint8_t *laddrp, in_port_t lport) = NULL; 1383 void (*cl_inet_unlisten)(uint8_t protocol, sa_family_t addr_family, 1384 uint8_t *laddrp, in_port_t lport) = NULL; 1385 void (*cl_inet_connect)(uint8_t protocol, sa_family_t addr_family, 1386 uint8_t *laddrp, in_port_t lport, 1387 uint8_t *faddrp, in_port_t fport) = NULL; 1388 void (*cl_inet_disconnect)(uint8_t protocol, sa_family_t addr_family, 1389 uint8_t *laddrp, in_port_t lport, 1390 uint8_t *faddrp, in_port_t fport) = NULL; 1391 1392 /* 1393 * The following are defined in ip.c 1394 */ 1395 extern int (*cl_inet_isclusterwide)(uint8_t protocol, sa_family_t addr_family, 1396 uint8_t *laddrp); 1397 extern uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 1398 uint8_t *laddrp, uint8_t *faddrp); 1399 1400 #define CL_INET_CONNECT(tcp) { \ 1401 if (cl_inet_connect != NULL) { \ 1402 /* \ 1403 * Running in cluster mode - register active connection \ 1404 * information \ 1405 */ \ 1406 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1407 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1408 (*cl_inet_connect)(IPPROTO_TCP, AF_INET,\ 1409 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1410 (in_port_t)(tcp)->tcp_lport, \ 1411 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1412 (in_port_t)(tcp)->tcp_fport); \ 1413 } \ 1414 } else { \ 1415 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1416 &(tcp)->tcp_ip6h->ip6_src)) {\ 1417 (*cl_inet_connect)(IPPROTO_TCP, AF_INET6,\ 1418 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1419 (in_port_t)(tcp)->tcp_lport, \ 1420 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1421 (in_port_t)(tcp)->tcp_fport); \ 1422 } \ 1423 } \ 1424 } \ 1425 } 1426 1427 #define CL_INET_DISCONNECT(tcp) { \ 1428 if (cl_inet_disconnect != NULL) { \ 1429 /* \ 1430 * Running in cluster mode - deregister active \ 1431 * connection information \ 1432 */ \ 1433 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1434 if ((tcp)->tcp_ip_src != 0) { \ 1435 (*cl_inet_disconnect)(IPPROTO_TCP, \ 1436 AF_INET, \ 1437 (uint8_t *)(&((tcp)->tcp_ip_src)),\ 1438 (in_port_t)(tcp)->tcp_lport, \ 1439 (uint8_t *) \ 1440 (&((tcp)->tcp_ipha->ipha_dst)),\ 1441 (in_port_t)(tcp)->tcp_fport); \ 1442 } \ 1443 } else { \ 1444 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1445 &(tcp)->tcp_ip_src_v6)) { \ 1446 (*cl_inet_disconnect)(IPPROTO_TCP, AF_INET6,\ 1447 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1448 (in_port_t)(tcp)->tcp_lport, \ 1449 (uint8_t *) \ 1450 (&((tcp)->tcp_ip6h->ip6_dst)),\ 1451 (in_port_t)(tcp)->tcp_fport); \ 1452 } \ 1453 } \ 1454 } \ 1455 } 1456 1457 /* 1458 * Cluster networking hook for traversing current connection list. 1459 * This routine is used to extract the current list of live connections 1460 * which must continue to to be dispatched to this node. 1461 */ 1462 int cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg); 1463 1464 static int cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), 1465 void *arg, tcp_stack_t *tcps); 1466 1467 /* 1468 * Figure out the value of window scale opton. Note that the rwnd is 1469 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1470 * We cannot find the scale value and then do a round up of tcp_rwnd 1471 * because the scale value may not be correct after that. 1472 * 1473 * Set the compiler flag to make this function inline. 1474 */ 1475 static void 1476 tcp_set_ws_value(tcp_t *tcp) 1477 { 1478 int i; 1479 uint32_t rwnd = tcp->tcp_rwnd; 1480 1481 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1482 i++, rwnd >>= 1) 1483 ; 1484 tcp->tcp_rcv_ws = i; 1485 } 1486 1487 /* 1488 * Remove a connection from the list of detached TIME_WAIT connections. 1489 * It returns B_FALSE if it can't remove the connection from the list 1490 * as the connection has already been removed from the list due to an 1491 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1492 */ 1493 static boolean_t 1494 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1495 { 1496 boolean_t locked = B_FALSE; 1497 1498 if (tcp_time_wait == NULL) { 1499 tcp_time_wait = *((tcp_squeue_priv_t **) 1500 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1501 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1502 locked = B_TRUE; 1503 } else { 1504 ASSERT(MUTEX_HELD(&tcp_time_wait->tcp_time_wait_lock)); 1505 } 1506 1507 if (tcp->tcp_time_wait_expire == 0) { 1508 ASSERT(tcp->tcp_time_wait_next == NULL); 1509 ASSERT(tcp->tcp_time_wait_prev == NULL); 1510 if (locked) 1511 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1512 return (B_FALSE); 1513 } 1514 ASSERT(TCP_IS_DETACHED(tcp)); 1515 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1516 1517 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1518 ASSERT(tcp->tcp_time_wait_prev == NULL); 1519 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1520 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1521 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1522 NULL; 1523 } else { 1524 tcp_time_wait->tcp_time_wait_tail = NULL; 1525 } 1526 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1527 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1528 ASSERT(tcp->tcp_time_wait_next == NULL); 1529 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1530 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1531 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1532 } else { 1533 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1534 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1535 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1536 tcp->tcp_time_wait_next; 1537 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1538 tcp->tcp_time_wait_prev; 1539 } 1540 tcp->tcp_time_wait_next = NULL; 1541 tcp->tcp_time_wait_prev = NULL; 1542 tcp->tcp_time_wait_expire = 0; 1543 1544 if (locked) 1545 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1546 return (B_TRUE); 1547 } 1548 1549 /* 1550 * Add a connection to the list of detached TIME_WAIT connections 1551 * and set its time to expire. 1552 */ 1553 static void 1554 tcp_time_wait_append(tcp_t *tcp) 1555 { 1556 tcp_stack_t *tcps = tcp->tcp_tcps; 1557 tcp_squeue_priv_t *tcp_time_wait = 1558 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1559 SQPRIVATE_TCP)); 1560 1561 tcp_timers_stop(tcp); 1562 1563 /* Freed above */ 1564 ASSERT(tcp->tcp_timer_tid == 0); 1565 ASSERT(tcp->tcp_ack_tid == 0); 1566 1567 /* must have happened at the time of detaching the tcp */ 1568 ASSERT(tcp->tcp_ptpahn == NULL); 1569 ASSERT(tcp->tcp_flow_stopped == 0); 1570 ASSERT(tcp->tcp_time_wait_next == NULL); 1571 ASSERT(tcp->tcp_time_wait_prev == NULL); 1572 ASSERT(tcp->tcp_time_wait_expire == NULL); 1573 ASSERT(tcp->tcp_listener == NULL); 1574 1575 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1576 /* 1577 * The value computed below in tcp->tcp_time_wait_expire may 1578 * appear negative or wrap around. That is ok since our 1579 * interest is only in the difference between the current lbolt 1580 * value and tcp->tcp_time_wait_expire. But the value should not 1581 * be zero, since it means the tcp is not in the TIME_WAIT list. 1582 * The corresponding comparison in tcp_time_wait_collector() uses 1583 * modular arithmetic. 1584 */ 1585 tcp->tcp_time_wait_expire += 1586 drv_usectohz(tcps->tcps_time_wait_interval * 1000); 1587 if (tcp->tcp_time_wait_expire == 0) 1588 tcp->tcp_time_wait_expire = 1; 1589 1590 ASSERT(TCP_IS_DETACHED(tcp)); 1591 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1592 ASSERT(tcp->tcp_time_wait_next == NULL); 1593 ASSERT(tcp->tcp_time_wait_prev == NULL); 1594 TCP_DBGSTAT(tcps, tcp_time_wait); 1595 1596 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1597 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1598 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1599 tcp_time_wait->tcp_time_wait_head = tcp; 1600 } else { 1601 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1602 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1603 TCPS_TIME_WAIT); 1604 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1605 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1606 } 1607 tcp_time_wait->tcp_time_wait_tail = tcp; 1608 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1609 } 1610 1611 /* ARGSUSED */ 1612 void 1613 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1614 { 1615 conn_t *connp = (conn_t *)arg; 1616 tcp_t *tcp = connp->conn_tcp; 1617 tcp_stack_t *tcps = tcp->tcp_tcps; 1618 1619 ASSERT(tcp != NULL); 1620 if (tcp->tcp_state == TCPS_CLOSED) { 1621 return; 1622 } 1623 1624 ASSERT((tcp->tcp_family == AF_INET && 1625 tcp->tcp_ipversion == IPV4_VERSION) || 1626 (tcp->tcp_family == AF_INET6 && 1627 (tcp->tcp_ipversion == IPV4_VERSION || 1628 tcp->tcp_ipversion == IPV6_VERSION))); 1629 ASSERT(!tcp->tcp_listener); 1630 1631 TCP_STAT(tcps, tcp_time_wait_reap); 1632 ASSERT(TCP_IS_DETACHED(tcp)); 1633 1634 /* 1635 * Because they have no upstream client to rebind or tcp_close() 1636 * them later, we axe the connection here and now. 1637 */ 1638 tcp_close_detached(tcp); 1639 } 1640 1641 /* 1642 * Remove cached/latched IPsec references. 1643 */ 1644 void 1645 tcp_ipsec_cleanup(tcp_t *tcp) 1646 { 1647 conn_t *connp = tcp->tcp_connp; 1648 1649 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1650 1651 if (connp->conn_latch != NULL) { 1652 IPLATCH_REFRELE(connp->conn_latch, 1653 connp->conn_netstack); 1654 connp->conn_latch = NULL; 1655 } 1656 if (connp->conn_policy != NULL) { 1657 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 1658 connp->conn_policy = NULL; 1659 } 1660 } 1661 1662 /* 1663 * Cleaup before placing on free list. 1664 * Disassociate from the netstack/tcp_stack_t since the freelist 1665 * is per squeue and not per netstack. 1666 */ 1667 void 1668 tcp_cleanup(tcp_t *tcp) 1669 { 1670 mblk_t *mp; 1671 char *tcp_iphc; 1672 int tcp_iphc_len; 1673 int tcp_hdr_grown; 1674 tcp_sack_info_t *tcp_sack_info; 1675 conn_t *connp = tcp->tcp_connp; 1676 tcp_stack_t *tcps = tcp->tcp_tcps; 1677 netstack_t *ns = tcps->tcps_netstack; 1678 1679 tcp_bind_hash_remove(tcp); 1680 1681 /* Cleanup that which needs the netstack first */ 1682 tcp_ipsec_cleanup(tcp); 1683 1684 tcp_free(tcp); 1685 1686 /* Release any SSL context */ 1687 if (tcp->tcp_kssl_ent != NULL) { 1688 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1689 tcp->tcp_kssl_ent = NULL; 1690 } 1691 1692 if (tcp->tcp_kssl_ctx != NULL) { 1693 kssl_release_ctx(tcp->tcp_kssl_ctx); 1694 tcp->tcp_kssl_ctx = NULL; 1695 } 1696 tcp->tcp_kssl_pending = B_FALSE; 1697 1698 conn_delete_ire(connp, NULL); 1699 1700 /* 1701 * Since we will bzero the entire structure, we need to 1702 * remove it and reinsert it in global hash list. We 1703 * know the walkers can't get to this conn because we 1704 * had set CONDEMNED flag earlier and checked reference 1705 * under conn_lock so walker won't pick it and when we 1706 * go the ipcl_globalhash_remove() below, no walker 1707 * can get to it. 1708 */ 1709 ipcl_globalhash_remove(connp); 1710 1711 /* 1712 * Now it is safe to decrement the reference counts. 1713 * This might be the last reference on the netstack and TCPS 1714 * in which case it will cause the tcp_g_q_close and 1715 * the freeing of the IP Instance. 1716 */ 1717 connp->conn_netstack = NULL; 1718 netstack_rele(ns); 1719 ASSERT(tcps != NULL); 1720 tcp->tcp_tcps = NULL; 1721 TCPS_REFRELE(tcps); 1722 1723 /* Save some state */ 1724 mp = tcp->tcp_timercache; 1725 1726 tcp_sack_info = tcp->tcp_sack_info; 1727 tcp_iphc = tcp->tcp_iphc; 1728 tcp_iphc_len = tcp->tcp_iphc_len; 1729 tcp_hdr_grown = tcp->tcp_hdr_grown; 1730 1731 if (connp->conn_cred != NULL) { 1732 crfree(connp->conn_cred); 1733 connp->conn_cred = NULL; 1734 } 1735 if (connp->conn_peercred != NULL) { 1736 crfree(connp->conn_peercred); 1737 connp->conn_peercred = NULL; 1738 } 1739 ipcl_conn_cleanup(connp); 1740 connp->conn_flags = IPCL_TCPCONN; 1741 bzero(tcp, sizeof (tcp_t)); 1742 1743 /* restore the state */ 1744 tcp->tcp_timercache = mp; 1745 1746 tcp->tcp_sack_info = tcp_sack_info; 1747 tcp->tcp_iphc = tcp_iphc; 1748 tcp->tcp_iphc_len = tcp_iphc_len; 1749 tcp->tcp_hdr_grown = tcp_hdr_grown; 1750 1751 tcp->tcp_connp = connp; 1752 1753 ASSERT(connp->conn_tcp == tcp); 1754 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1755 connp->conn_state_flags = CONN_INCIPIENT; 1756 ASSERT(connp->conn_ulp == IPPROTO_TCP); 1757 ASSERT(connp->conn_ref == 1); 1758 } 1759 1760 /* 1761 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1762 * is done forwards from the head. 1763 * This walks all stack instances since 1764 * tcp_time_wait remains global across all stacks. 1765 */ 1766 /* ARGSUSED */ 1767 void 1768 tcp_time_wait_collector(void *arg) 1769 { 1770 tcp_t *tcp; 1771 clock_t now; 1772 mblk_t *mp; 1773 conn_t *connp; 1774 kmutex_t *lock; 1775 boolean_t removed; 1776 1777 squeue_t *sqp = (squeue_t *)arg; 1778 tcp_squeue_priv_t *tcp_time_wait = 1779 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1780 1781 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1782 tcp_time_wait->tcp_time_wait_tid = 0; 1783 1784 if (tcp_time_wait->tcp_free_list != NULL && 1785 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1786 TCP_G_STAT(tcp_freelist_cleanup); 1787 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1788 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1789 tcp->tcp_time_wait_next = NULL; 1790 tcp_time_wait->tcp_free_list_cnt--; 1791 ASSERT(tcp->tcp_tcps == NULL); 1792 CONN_DEC_REF(tcp->tcp_connp); 1793 } 1794 ASSERT(tcp_time_wait->tcp_free_list_cnt == 0); 1795 } 1796 1797 /* 1798 * In order to reap time waits reliably, we should use a 1799 * source of time that is not adjustable by the user -- hence 1800 * the call to ddi_get_lbolt(). 1801 */ 1802 now = ddi_get_lbolt(); 1803 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1804 /* 1805 * Compare times using modular arithmetic, since 1806 * lbolt can wrapover. 1807 */ 1808 if ((now - tcp->tcp_time_wait_expire) < 0) { 1809 break; 1810 } 1811 1812 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1813 ASSERT(removed); 1814 1815 connp = tcp->tcp_connp; 1816 ASSERT(connp->conn_fanout != NULL); 1817 lock = &connp->conn_fanout->connf_lock; 1818 /* 1819 * This is essentially a TW reclaim fast path optimization for 1820 * performance where the timewait collector checks under the 1821 * fanout lock (so that no one else can get access to the 1822 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1823 * the classifier hash list. If ref count is indeed 2, we can 1824 * just remove the conn under the fanout lock and avoid 1825 * cleaning up the conn under the squeue, provided that 1826 * clustering callbacks are not enabled. If clustering is 1827 * enabled, we need to make the clustering callback before 1828 * setting the CONDEMNED flag and after dropping all locks and 1829 * so we forego this optimization and fall back to the slow 1830 * path. Also please see the comments in tcp_closei_local 1831 * regarding the refcnt logic. 1832 * 1833 * Since we are holding the tcp_time_wait_lock, its better 1834 * not to block on the fanout_lock because other connections 1835 * can't add themselves to time_wait list. So we do a 1836 * tryenter instead of mutex_enter. 1837 */ 1838 if (mutex_tryenter(lock)) { 1839 mutex_enter(&connp->conn_lock); 1840 if ((connp->conn_ref == 2) && 1841 (cl_inet_disconnect == NULL)) { 1842 ipcl_hash_remove_locked(connp, 1843 connp->conn_fanout); 1844 /* 1845 * Set the CONDEMNED flag now itself so that 1846 * the refcnt cannot increase due to any 1847 * walker. But we have still not cleaned up 1848 * conn_ire_cache. This is still ok since 1849 * we are going to clean it up in tcp_cleanup 1850 * immediately and any interface unplumb 1851 * thread will wait till the ire is blown away 1852 */ 1853 connp->conn_state_flags |= CONN_CONDEMNED; 1854 mutex_exit(lock); 1855 mutex_exit(&connp->conn_lock); 1856 if (tcp_time_wait->tcp_free_list_cnt < 1857 tcp_free_list_max_cnt) { 1858 /* Add to head of tcp_free_list */ 1859 mutex_exit( 1860 &tcp_time_wait->tcp_time_wait_lock); 1861 tcp_cleanup(tcp); 1862 ASSERT(connp->conn_latch == NULL); 1863 ASSERT(connp->conn_policy == NULL); 1864 ASSERT(tcp->tcp_tcps == NULL); 1865 ASSERT(connp->conn_netstack == NULL); 1866 1867 mutex_enter( 1868 &tcp_time_wait->tcp_time_wait_lock); 1869 tcp->tcp_time_wait_next = 1870 tcp_time_wait->tcp_free_list; 1871 tcp_time_wait->tcp_free_list = tcp; 1872 tcp_time_wait->tcp_free_list_cnt++; 1873 continue; 1874 } else { 1875 /* Do not add to tcp_free_list */ 1876 mutex_exit( 1877 &tcp_time_wait->tcp_time_wait_lock); 1878 tcp_bind_hash_remove(tcp); 1879 conn_delete_ire(tcp->tcp_connp, NULL); 1880 tcp_ipsec_cleanup(tcp); 1881 CONN_DEC_REF(tcp->tcp_connp); 1882 } 1883 } else { 1884 CONN_INC_REF_LOCKED(connp); 1885 mutex_exit(lock); 1886 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1887 mutex_exit(&connp->conn_lock); 1888 /* 1889 * We can reuse the closemp here since conn has 1890 * detached (otherwise we wouldn't even be in 1891 * time_wait list). tcp_closemp_used can safely 1892 * be changed without taking a lock as no other 1893 * thread can concurrently access it at this 1894 * point in the connection lifecycle. 1895 */ 1896 1897 if (tcp->tcp_closemp.b_prev == NULL) 1898 tcp->tcp_closemp_used = B_TRUE; 1899 else 1900 cmn_err(CE_PANIC, 1901 "tcp_timewait_collector: " 1902 "concurrent use of tcp_closemp: " 1903 "connp %p tcp %p\n", (void *)connp, 1904 (void *)tcp); 1905 1906 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1907 mp = &tcp->tcp_closemp; 1908 squeue_fill(connp->conn_sqp, mp, 1909 tcp_timewait_output, connp, 1910 SQTAG_TCP_TIMEWAIT); 1911 } 1912 } else { 1913 mutex_enter(&connp->conn_lock); 1914 CONN_INC_REF_LOCKED(connp); 1915 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1916 mutex_exit(&connp->conn_lock); 1917 /* 1918 * We can reuse the closemp here since conn has 1919 * detached (otherwise we wouldn't even be in 1920 * time_wait list). tcp_closemp_used can safely 1921 * be changed without taking a lock as no other 1922 * thread can concurrently access it at this 1923 * point in the connection lifecycle. 1924 */ 1925 1926 if (tcp->tcp_closemp.b_prev == NULL) 1927 tcp->tcp_closemp_used = B_TRUE; 1928 else 1929 cmn_err(CE_PANIC, "tcp_timewait_collector: " 1930 "concurrent use of tcp_closemp: " 1931 "connp %p tcp %p\n", (void *)connp, 1932 (void *)tcp); 1933 1934 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1935 mp = &tcp->tcp_closemp; 1936 squeue_fill(connp->conn_sqp, mp, 1937 tcp_timewait_output, connp, 0); 1938 } 1939 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1940 } 1941 1942 if (tcp_time_wait->tcp_free_list != NULL) 1943 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1944 1945 tcp_time_wait->tcp_time_wait_tid = 1946 timeout(tcp_time_wait_collector, sqp, TCP_TIME_WAIT_DELAY); 1947 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1948 } 1949 /* 1950 * Reply to a clients T_CONN_RES TPI message. This function 1951 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1952 * on the acceptor STREAM and processed in tcp_wput_accept(). 1953 * Read the block comment on top of tcp_conn_request(). 1954 */ 1955 static void 1956 tcp_accept(tcp_t *listener, mblk_t *mp) 1957 { 1958 tcp_t *acceptor; 1959 tcp_t *eager; 1960 tcp_t *tcp; 1961 struct T_conn_res *tcr; 1962 t_uscalar_t acceptor_id; 1963 t_scalar_t seqnum; 1964 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1965 mblk_t *ok_mp; 1966 mblk_t *mp1; 1967 tcp_stack_t *tcps = listener->tcp_tcps; 1968 1969 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1970 tcp_err_ack(listener, mp, TPROTO, 0); 1971 return; 1972 } 1973 tcr = (struct T_conn_res *)mp->b_rptr; 1974 1975 /* 1976 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1977 * read side queue of the streams device underneath us i.e. the 1978 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1979 * look it up in the queue_hash. Under LP64 it sends down the 1980 * minor_t of the accepting endpoint. 1981 * 1982 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1983 * fanout hash lock is held. 1984 * This prevents any thread from entering the acceptor queue from 1985 * below (since it has not been hard bound yet i.e. any inbound 1986 * packets will arrive on the listener or default tcp queue and 1987 * go through tcp_lookup). 1988 * The CONN_INC_REF will prevent the acceptor from closing. 1989 * 1990 * XXX It is still possible for a tli application to send down data 1991 * on the accepting stream while another thread calls t_accept. 1992 * This should not be a problem for well-behaved applications since 1993 * the T_OK_ACK is sent after the queue swapping is completed. 1994 * 1995 * If the accepting fd is the same as the listening fd, avoid 1996 * queue hash lookup since that will return an eager listener in a 1997 * already established state. 1998 */ 1999 acceptor_id = tcr->ACCEPTOR_id; 2000 mutex_enter(&listener->tcp_eager_lock); 2001 if (listener->tcp_acceptor_id == acceptor_id) { 2002 eager = listener->tcp_eager_next_q; 2003 /* only count how many T_CONN_INDs so don't count q0 */ 2004 if ((listener->tcp_conn_req_cnt_q != 1) || 2005 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 2006 mutex_exit(&listener->tcp_eager_lock); 2007 tcp_err_ack(listener, mp, TBADF, 0); 2008 return; 2009 } 2010 if (listener->tcp_conn_req_cnt_q0 != 0) { 2011 /* Throw away all the eagers on q0. */ 2012 tcp_eager_cleanup(listener, 1); 2013 } 2014 if (listener->tcp_syn_defense) { 2015 listener->tcp_syn_defense = B_FALSE; 2016 if (listener->tcp_ip_addr_cache != NULL) { 2017 kmem_free(listener->tcp_ip_addr_cache, 2018 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 2019 listener->tcp_ip_addr_cache = NULL; 2020 } 2021 } 2022 /* 2023 * Transfer tcp_conn_req_max to the eager so that when 2024 * a disconnect occurs we can revert the endpoint to the 2025 * listen state. 2026 */ 2027 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 2028 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 2029 /* 2030 * Get a reference on the acceptor just like the 2031 * tcp_acceptor_hash_lookup below. 2032 */ 2033 acceptor = listener; 2034 CONN_INC_REF(acceptor->tcp_connp); 2035 } else { 2036 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 2037 if (acceptor == NULL) { 2038 if (listener->tcp_debug) { 2039 (void) strlog(TCP_MOD_ID, 0, 1, 2040 SL_ERROR|SL_TRACE, 2041 "tcp_accept: did not find acceptor 0x%x\n", 2042 acceptor_id); 2043 } 2044 mutex_exit(&listener->tcp_eager_lock); 2045 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 2046 return; 2047 } 2048 /* 2049 * Verify acceptor state. The acceptable states for an acceptor 2050 * include TCPS_IDLE and TCPS_BOUND. 2051 */ 2052 switch (acceptor->tcp_state) { 2053 case TCPS_IDLE: 2054 /* FALLTHRU */ 2055 case TCPS_BOUND: 2056 break; 2057 default: 2058 CONN_DEC_REF(acceptor->tcp_connp); 2059 mutex_exit(&listener->tcp_eager_lock); 2060 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2061 return; 2062 } 2063 } 2064 2065 /* The listener must be in TCPS_LISTEN */ 2066 if (listener->tcp_state != TCPS_LISTEN) { 2067 CONN_DEC_REF(acceptor->tcp_connp); 2068 mutex_exit(&listener->tcp_eager_lock); 2069 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2070 return; 2071 } 2072 2073 /* 2074 * Rendezvous with an eager connection request packet hanging off 2075 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2076 * tcp structure when the connection packet arrived in 2077 * tcp_conn_request(). 2078 */ 2079 seqnum = tcr->SEQ_number; 2080 eager = listener; 2081 do { 2082 eager = eager->tcp_eager_next_q; 2083 if (eager == NULL) { 2084 CONN_DEC_REF(acceptor->tcp_connp); 2085 mutex_exit(&listener->tcp_eager_lock); 2086 tcp_err_ack(listener, mp, TBADSEQ, 0); 2087 return; 2088 } 2089 } while (eager->tcp_conn_req_seqnum != seqnum); 2090 mutex_exit(&listener->tcp_eager_lock); 2091 2092 /* 2093 * At this point, both acceptor and listener have 2 ref 2094 * that they begin with. Acceptor has one additional ref 2095 * we placed in lookup while listener has 3 additional 2096 * ref for being behind the squeue (tcp_accept() is 2097 * done on listener's squeue); being in classifier hash; 2098 * and eager's ref on listener. 2099 */ 2100 ASSERT(listener->tcp_connp->conn_ref >= 5); 2101 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2102 2103 /* 2104 * The eager at this point is set in its own squeue and 2105 * could easily have been killed (tcp_accept_finish will 2106 * deal with that) because of a TH_RST so we can only 2107 * ASSERT for a single ref. 2108 */ 2109 ASSERT(eager->tcp_connp->conn_ref >= 1); 2110 2111 /* Pre allocate the stroptions mblk also */ 2112 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 2113 if (opt_mp == NULL) { 2114 CONN_DEC_REF(acceptor->tcp_connp); 2115 CONN_DEC_REF(eager->tcp_connp); 2116 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2117 return; 2118 } 2119 DB_TYPE(opt_mp) = M_SETOPTS; 2120 opt_mp->b_wptr += sizeof (struct stroptions); 2121 2122 /* 2123 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2124 * from listener to acceptor. The message is chained on opt_mp 2125 * which will be sent onto eager's squeue. 2126 */ 2127 if (listener->tcp_bound_if != 0) { 2128 /* allocate optmgmt req */ 2129 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2130 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 2131 sizeof (int)); 2132 if (mp1 != NULL) 2133 linkb(opt_mp, mp1); 2134 } 2135 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2136 uint_t on = 1; 2137 2138 /* allocate optmgmt req */ 2139 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2140 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 2141 if (mp1 != NULL) 2142 linkb(opt_mp, mp1); 2143 } 2144 2145 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2146 if ((mp1 = copymsg(mp)) == NULL) { 2147 CONN_DEC_REF(acceptor->tcp_connp); 2148 CONN_DEC_REF(eager->tcp_connp); 2149 freemsg(opt_mp); 2150 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2151 return; 2152 } 2153 2154 tcr = (struct T_conn_res *)mp1->b_rptr; 2155 2156 /* 2157 * This is an expanded version of mi_tpi_ok_ack_alloc() 2158 * which allocates a larger mblk and appends the new 2159 * local address to the ok_ack. The address is copied by 2160 * soaccept() for getsockname(). 2161 */ 2162 { 2163 int extra; 2164 2165 extra = (eager->tcp_family == AF_INET) ? 2166 sizeof (sin_t) : sizeof (sin6_t); 2167 2168 /* 2169 * Try to re-use mp, if possible. Otherwise, allocate 2170 * an mblk and return it as ok_mp. In any case, mp 2171 * is no longer usable upon return. 2172 */ 2173 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2174 CONN_DEC_REF(acceptor->tcp_connp); 2175 CONN_DEC_REF(eager->tcp_connp); 2176 freemsg(opt_mp); 2177 /* Original mp has been freed by now, so use mp1 */ 2178 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2179 return; 2180 } 2181 2182 mp = NULL; /* We should never use mp after this point */ 2183 2184 switch (extra) { 2185 case sizeof (sin_t): { 2186 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2187 2188 ok_mp->b_wptr += extra; 2189 sin->sin_family = AF_INET; 2190 sin->sin_port = eager->tcp_lport; 2191 sin->sin_addr.s_addr = 2192 eager->tcp_ipha->ipha_src; 2193 break; 2194 } 2195 case sizeof (sin6_t): { 2196 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2197 2198 ok_mp->b_wptr += extra; 2199 sin6->sin6_family = AF_INET6; 2200 sin6->sin6_port = eager->tcp_lport; 2201 if (eager->tcp_ipversion == IPV4_VERSION) { 2202 sin6->sin6_flowinfo = 0; 2203 IN6_IPADDR_TO_V4MAPPED( 2204 eager->tcp_ipha->ipha_src, 2205 &sin6->sin6_addr); 2206 } else { 2207 ASSERT(eager->tcp_ip6h != NULL); 2208 sin6->sin6_flowinfo = 2209 eager->tcp_ip6h->ip6_vcf & 2210 ~IPV6_VERS_AND_FLOW_MASK; 2211 sin6->sin6_addr = 2212 eager->tcp_ip6h->ip6_src; 2213 } 2214 sin6->sin6_scope_id = 0; 2215 sin6->__sin6_src_id = 0; 2216 break; 2217 } 2218 default: 2219 break; 2220 } 2221 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2222 } 2223 2224 /* 2225 * If there are no options we know that the T_CONN_RES will 2226 * succeed. However, we can't send the T_OK_ACK upstream until 2227 * the tcp_accept_swap is done since it would be dangerous to 2228 * let the application start using the new fd prior to the swap. 2229 */ 2230 tcp_accept_swap(listener, acceptor, eager); 2231 2232 /* 2233 * tcp_accept_swap unlinks eager from listener but does not drop 2234 * the eager's reference on the listener. 2235 */ 2236 ASSERT(eager->tcp_listener == NULL); 2237 ASSERT(listener->tcp_connp->conn_ref >= 5); 2238 2239 /* 2240 * The eager is now associated with its own queue. Insert in 2241 * the hash so that the connection can be reused for a future 2242 * T_CONN_RES. 2243 */ 2244 tcp_acceptor_hash_insert(acceptor_id, eager); 2245 2246 /* 2247 * We now do the processing of options with T_CONN_RES. 2248 * We delay till now since we wanted to have queue to pass to 2249 * option processing routines that points back to the right 2250 * instance structure which does not happen until after 2251 * tcp_accept_swap(). 2252 * 2253 * Note: 2254 * The sanity of the logic here assumes that whatever options 2255 * are appropriate to inherit from listner=>eager are done 2256 * before this point, and whatever were to be overridden (or not) 2257 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2258 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2259 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2260 * This may not be true at this point in time but can be fixed 2261 * independently. This option processing code starts with 2262 * the instantiated acceptor instance and the final queue at 2263 * this point. 2264 */ 2265 2266 if (tcr->OPT_length != 0) { 2267 /* Options to process */ 2268 int t_error = 0; 2269 int sys_error = 0; 2270 int do_disconnect = 0; 2271 2272 if (tcp_conprim_opt_process(eager, mp1, 2273 &do_disconnect, &t_error, &sys_error) < 0) { 2274 eager->tcp_accept_error = 1; 2275 if (do_disconnect) { 2276 /* 2277 * An option failed which does not allow 2278 * connection to be accepted. 2279 * 2280 * We allow T_CONN_RES to succeed and 2281 * put a T_DISCON_IND on the eager queue. 2282 */ 2283 ASSERT(t_error == 0 && sys_error == 0); 2284 eager->tcp_send_discon_ind = 1; 2285 } else { 2286 ASSERT(t_error != 0); 2287 freemsg(ok_mp); 2288 /* 2289 * Original mp was either freed or set 2290 * to ok_mp above, so use mp1 instead. 2291 */ 2292 tcp_err_ack(listener, mp1, t_error, sys_error); 2293 goto finish; 2294 } 2295 } 2296 /* 2297 * Most likely success in setting options (except if 2298 * eager->tcp_send_discon_ind set). 2299 * mp1 option buffer represented by OPT_length/offset 2300 * potentially modified and contains results of setting 2301 * options at this point 2302 */ 2303 } 2304 2305 /* We no longer need mp1, since all options processing has passed */ 2306 freemsg(mp1); 2307 2308 putnext(listener->tcp_rq, ok_mp); 2309 2310 mutex_enter(&listener->tcp_eager_lock); 2311 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2312 tcp_t *tail; 2313 mblk_t *conn_ind; 2314 2315 /* 2316 * This path should not be executed if listener and 2317 * acceptor streams are the same. 2318 */ 2319 ASSERT(listener != acceptor); 2320 2321 tcp = listener->tcp_eager_prev_q0; 2322 /* 2323 * listener->tcp_eager_prev_q0 points to the TAIL of the 2324 * deferred T_conn_ind queue. We need to get to the head of 2325 * the queue in order to send up T_conn_ind the same order as 2326 * how the 3WHS is completed. 2327 */ 2328 while (tcp != listener) { 2329 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2330 break; 2331 else 2332 tcp = tcp->tcp_eager_prev_q0; 2333 } 2334 ASSERT(tcp != listener); 2335 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2336 ASSERT(conn_ind != NULL); 2337 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2338 2339 /* Move from q0 to q */ 2340 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2341 listener->tcp_conn_req_cnt_q0--; 2342 listener->tcp_conn_req_cnt_q++; 2343 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2344 tcp->tcp_eager_prev_q0; 2345 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2346 tcp->tcp_eager_next_q0; 2347 tcp->tcp_eager_prev_q0 = NULL; 2348 tcp->tcp_eager_next_q0 = NULL; 2349 tcp->tcp_conn_def_q0 = B_FALSE; 2350 2351 /* Make sure the tcp isn't in the list of droppables */ 2352 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2353 tcp->tcp_eager_prev_drop_q0 == NULL); 2354 2355 /* 2356 * Insert at end of the queue because sockfs sends 2357 * down T_CONN_RES in chronological order. Leaving 2358 * the older conn indications at front of the queue 2359 * helps reducing search time. 2360 */ 2361 tail = listener->tcp_eager_last_q; 2362 if (tail != NULL) 2363 tail->tcp_eager_next_q = tcp; 2364 else 2365 listener->tcp_eager_next_q = tcp; 2366 listener->tcp_eager_last_q = tcp; 2367 tcp->tcp_eager_next_q = NULL; 2368 mutex_exit(&listener->tcp_eager_lock); 2369 putnext(tcp->tcp_rq, conn_ind); 2370 } else { 2371 mutex_exit(&listener->tcp_eager_lock); 2372 } 2373 2374 /* 2375 * Done with the acceptor - free it 2376 * 2377 * Note: from this point on, no access to listener should be made 2378 * as listener can be equal to acceptor. 2379 */ 2380 finish: 2381 ASSERT(acceptor->tcp_detached); 2382 ASSERT(tcps->tcps_g_q != NULL); 2383 acceptor->tcp_rq = tcps->tcps_g_q; 2384 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2385 (void) tcp_clean_death(acceptor, 0, 2); 2386 CONN_DEC_REF(acceptor->tcp_connp); 2387 2388 /* 2389 * In case we already received a FIN we have to make tcp_rput send 2390 * the ordrel_ind. This will also send up a window update if the window 2391 * has opened up. 2392 * 2393 * In the normal case of a successful connection acceptance 2394 * we give the O_T_BIND_REQ to the read side put procedure as an 2395 * indication that this was just accepted. This tells tcp_rput to 2396 * pass up any data queued in tcp_rcv_list. 2397 * 2398 * In the fringe case where options sent with T_CONN_RES failed and 2399 * we required, we would be indicating a T_DISCON_IND to blow 2400 * away this connection. 2401 */ 2402 2403 /* 2404 * XXX: we currently have a problem if XTI application closes the 2405 * acceptor stream in between. This problem exists in on10-gate also 2406 * and is well know but nothing can be done short of major rewrite 2407 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2408 * eager same squeue as listener (we can distinguish non socket 2409 * listeners at the time of handling a SYN in tcp_conn_request) 2410 * and do most of the work that tcp_accept_finish does here itself 2411 * and then get behind the acceptor squeue to access the acceptor 2412 * queue. 2413 */ 2414 /* 2415 * We already have a ref on tcp so no need to do one before squeue_fill 2416 */ 2417 squeue_fill(eager->tcp_connp->conn_sqp, opt_mp, 2418 tcp_accept_finish, eager->tcp_connp, SQTAG_TCP_ACCEPT_FINISH); 2419 } 2420 2421 /* 2422 * Swap information between the eager and acceptor for a TLI/XTI client. 2423 * The sockfs accept is done on the acceptor stream and control goes 2424 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2425 * called. In either case, both the eager and listener are in their own 2426 * perimeter (squeue) and the code has to deal with potential race. 2427 * 2428 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2429 */ 2430 static void 2431 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2432 { 2433 conn_t *econnp, *aconnp; 2434 2435 ASSERT(eager->tcp_rq == listener->tcp_rq); 2436 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2437 ASSERT(!eager->tcp_hard_bound); 2438 ASSERT(!TCP_IS_SOCKET(acceptor)); 2439 ASSERT(!TCP_IS_SOCKET(eager)); 2440 ASSERT(!TCP_IS_SOCKET(listener)); 2441 2442 acceptor->tcp_detached = B_TRUE; 2443 /* 2444 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2445 * the acceptor id. 2446 */ 2447 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2448 2449 /* remove eager from listen list... */ 2450 mutex_enter(&listener->tcp_eager_lock); 2451 tcp_eager_unlink(eager); 2452 ASSERT(eager->tcp_eager_next_q == NULL && 2453 eager->tcp_eager_last_q == NULL); 2454 ASSERT(eager->tcp_eager_next_q0 == NULL && 2455 eager->tcp_eager_prev_q0 == NULL); 2456 mutex_exit(&listener->tcp_eager_lock); 2457 eager->tcp_rq = acceptor->tcp_rq; 2458 eager->tcp_wq = acceptor->tcp_wq; 2459 2460 econnp = eager->tcp_connp; 2461 aconnp = acceptor->tcp_connp; 2462 2463 eager->tcp_rq->q_ptr = econnp; 2464 eager->tcp_wq->q_ptr = econnp; 2465 2466 /* 2467 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2468 * which might be a different squeue from our peer TCP instance. 2469 * For TCP Fusion, the peer expects that whenever tcp_detached is 2470 * clear, our TCP queues point to the acceptor's queues. Thus, use 2471 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2472 * above reach global visibility prior to the clearing of tcp_detached. 2473 */ 2474 membar_producer(); 2475 eager->tcp_detached = B_FALSE; 2476 2477 ASSERT(eager->tcp_ack_tid == 0); 2478 2479 econnp->conn_dev = aconnp->conn_dev; 2480 econnp->conn_minor_arena = aconnp->conn_minor_arena; 2481 ASSERT(econnp->conn_minor_arena != NULL); 2482 if (eager->tcp_cred != NULL) 2483 crfree(eager->tcp_cred); 2484 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2485 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2486 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2487 2488 aconnp->conn_cred = NULL; 2489 2490 econnp->conn_zoneid = aconnp->conn_zoneid; 2491 econnp->conn_allzones = aconnp->conn_allzones; 2492 2493 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2494 aconnp->conn_mac_exempt = B_FALSE; 2495 2496 ASSERT(aconnp->conn_peercred == NULL); 2497 2498 /* Do the IPC initialization */ 2499 CONN_INC_REF(econnp); 2500 2501 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2502 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2503 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2504 2505 /* Done with old IPC. Drop its ref on its connp */ 2506 CONN_DEC_REF(aconnp); 2507 } 2508 2509 2510 /* 2511 * Adapt to the information, such as rtt and rtt_sd, provided from the 2512 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2513 * 2514 * Checks for multicast and broadcast destination address. 2515 * Returns zero on failure; non-zero if ok. 2516 * 2517 * Note that the MSS calculation here is based on the info given in 2518 * the IRE. We do not do any calculation based on TCP options. They 2519 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2520 * knows which options to use. 2521 * 2522 * Note on how TCP gets its parameters for a connection. 2523 * 2524 * When a tcp_t structure is allocated, it gets all the default parameters. 2525 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2526 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2527 * default. But if there is an associated tcp_host_param, it will override 2528 * the metrics. 2529 * 2530 * An incoming SYN with a multicast or broadcast destination address, is dropped 2531 * in 1 of 2 places. 2532 * 2533 * 1. If the packet was received over the wire it is dropped in 2534 * ip_rput_process_broadcast() 2535 * 2536 * 2. If the packet was received through internal IP loopback, i.e. the packet 2537 * was generated and received on the same machine, it is dropped in 2538 * ip_wput_local() 2539 * 2540 * An incoming SYN with a multicast or broadcast source address is always 2541 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2542 * reject an attempt to connect to a broadcast or multicast (destination) 2543 * address. 2544 */ 2545 static int 2546 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2547 { 2548 tcp_hsp_t *hsp; 2549 ire_t *ire; 2550 ire_t *sire = NULL; 2551 iulp_t *ire_uinfo = NULL; 2552 uint32_t mss_max; 2553 uint32_t mss; 2554 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2555 conn_t *connp = tcp->tcp_connp; 2556 boolean_t ire_cacheable = B_FALSE; 2557 zoneid_t zoneid = connp->conn_zoneid; 2558 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2559 MATCH_IRE_SECATTR; 2560 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2561 ill_t *ill = NULL; 2562 boolean_t incoming = (ire_mp == NULL); 2563 tcp_stack_t *tcps = tcp->tcp_tcps; 2564 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2565 2566 ASSERT(connp->conn_ire_cache == NULL); 2567 2568 if (tcp->tcp_ipversion == IPV4_VERSION) { 2569 2570 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2571 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2572 return (0); 2573 } 2574 /* 2575 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2576 * for the destination with the nexthop as gateway. 2577 * ire_ctable_lookup() is used because this particular 2578 * ire, if it exists, will be marked private. 2579 * If that is not available, use the interface ire 2580 * for the nexthop. 2581 * 2582 * TSol: tcp_update_label will detect label mismatches based 2583 * only on the destination's label, but that would not 2584 * detect label mismatches based on the security attributes 2585 * of routes or next hop gateway. Hence we need to pass the 2586 * label to ire_ftable_lookup below in order to locate the 2587 * right prefix (and/or) ire cache. Similarly we also need 2588 * pass the label to the ire_cache_lookup below to locate 2589 * the right ire that also matches on the label. 2590 */ 2591 if (tcp->tcp_connp->conn_nexthop_set) { 2592 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2593 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2594 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2595 ipst); 2596 if (ire == NULL) { 2597 ire = ire_ftable_lookup( 2598 tcp->tcp_connp->conn_nexthop_v4, 2599 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2600 tsl, match_flags, ipst); 2601 if (ire == NULL) 2602 return (0); 2603 } else { 2604 ire_uinfo = &ire->ire_uinfo; 2605 } 2606 } else { 2607 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2608 zoneid, tsl, ipst); 2609 if (ire != NULL) { 2610 ire_cacheable = B_TRUE; 2611 ire_uinfo = (ire_mp != NULL) ? 2612 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2613 &ire->ire_uinfo; 2614 2615 } else { 2616 if (ire_mp == NULL) { 2617 ire = ire_ftable_lookup( 2618 tcp->tcp_connp->conn_rem, 2619 0, 0, 0, NULL, &sire, zoneid, 0, 2620 tsl, (MATCH_IRE_RECURSIVE | 2621 MATCH_IRE_DEFAULT), ipst); 2622 if (ire == NULL) 2623 return (0); 2624 ire_uinfo = (sire != NULL) ? 2625 &sire->ire_uinfo : 2626 &ire->ire_uinfo; 2627 } else { 2628 ire = (ire_t *)ire_mp->b_rptr; 2629 ire_uinfo = 2630 &((ire_t *) 2631 ire_mp->b_rptr)->ire_uinfo; 2632 } 2633 } 2634 } 2635 ASSERT(ire != NULL); 2636 2637 if ((ire->ire_src_addr == INADDR_ANY) || 2638 (ire->ire_type & IRE_BROADCAST)) { 2639 /* 2640 * ire->ire_mp is non null when ire_mp passed in is used 2641 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2642 */ 2643 if (ire->ire_mp == NULL) 2644 ire_refrele(ire); 2645 if (sire != NULL) 2646 ire_refrele(sire); 2647 return (0); 2648 } 2649 2650 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2651 ipaddr_t src_addr; 2652 2653 /* 2654 * ip_bind_connected() has stored the correct source 2655 * address in conn_src. 2656 */ 2657 src_addr = tcp->tcp_connp->conn_src; 2658 tcp->tcp_ipha->ipha_src = src_addr; 2659 /* 2660 * Copy of the src addr. in tcp_t is needed 2661 * for the lookup funcs. 2662 */ 2663 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2664 } 2665 /* 2666 * Set the fragment bit so that IP will tell us if the MTU 2667 * should change. IP tells us the latest setting of 2668 * ip_path_mtu_discovery through ire_frag_flag. 2669 */ 2670 if (ipst->ips_ip_path_mtu_discovery) { 2671 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2672 htons(IPH_DF); 2673 } 2674 /* 2675 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2676 * for IP_NEXTHOP. No cache ire has been found for the 2677 * destination and we are working with the nexthop's 2678 * interface ire. Since we need to forward all packets 2679 * to the nexthop first, we "blindly" set tcp_localnet 2680 * to false, eventhough the destination may also be 2681 * onlink. 2682 */ 2683 if (ire_uinfo == NULL) 2684 tcp->tcp_localnet = 0; 2685 else 2686 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2687 } else { 2688 /* 2689 * For incoming connection ire_mp = NULL 2690 * For outgoing connection ire_mp != NULL 2691 * Technically we should check conn_incoming_ill 2692 * when ire_mp is NULL and conn_outgoing_ill when 2693 * ire_mp is non-NULL. But this is performance 2694 * critical path and for IPV*_BOUND_IF, outgoing 2695 * and incoming ill are always set to the same value. 2696 */ 2697 ill_t *dst_ill = NULL; 2698 ipif_t *dst_ipif = NULL; 2699 2700 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2701 2702 if (connp->conn_outgoing_ill != NULL) { 2703 /* Outgoing or incoming path */ 2704 int err; 2705 2706 dst_ill = conn_get_held_ill(connp, 2707 &connp->conn_outgoing_ill, &err); 2708 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2709 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2710 return (0); 2711 } 2712 match_flags |= MATCH_IRE_ILL; 2713 dst_ipif = dst_ill->ill_ipif; 2714 } 2715 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2716 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2717 2718 if (ire != NULL) { 2719 ire_cacheable = B_TRUE; 2720 ire_uinfo = (ire_mp != NULL) ? 2721 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2722 &ire->ire_uinfo; 2723 } else { 2724 if (ire_mp == NULL) { 2725 ire = ire_ftable_lookup_v6( 2726 &tcp->tcp_connp->conn_remv6, 2727 0, 0, 0, dst_ipif, &sire, zoneid, 2728 0, tsl, match_flags, ipst); 2729 if (ire == NULL) { 2730 if (dst_ill != NULL) 2731 ill_refrele(dst_ill); 2732 return (0); 2733 } 2734 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2735 &ire->ire_uinfo; 2736 } else { 2737 ire = (ire_t *)ire_mp->b_rptr; 2738 ire_uinfo = 2739 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2740 } 2741 } 2742 if (dst_ill != NULL) 2743 ill_refrele(dst_ill); 2744 2745 ASSERT(ire != NULL); 2746 ASSERT(ire_uinfo != NULL); 2747 2748 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2749 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2750 /* 2751 * ire->ire_mp is non null when ire_mp passed in is used 2752 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2753 */ 2754 if (ire->ire_mp == NULL) 2755 ire_refrele(ire); 2756 if (sire != NULL) 2757 ire_refrele(sire); 2758 return (0); 2759 } 2760 2761 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2762 in6_addr_t src_addr; 2763 2764 /* 2765 * ip_bind_connected_v6() has stored the correct source 2766 * address per IPv6 addr. selection policy in 2767 * conn_src_v6. 2768 */ 2769 src_addr = tcp->tcp_connp->conn_srcv6; 2770 2771 tcp->tcp_ip6h->ip6_src = src_addr; 2772 /* 2773 * Copy of the src addr. in tcp_t is needed 2774 * for the lookup funcs. 2775 */ 2776 tcp->tcp_ip_src_v6 = src_addr; 2777 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2778 &connp->conn_srcv6)); 2779 } 2780 tcp->tcp_localnet = 2781 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2782 } 2783 2784 /* 2785 * This allows applications to fail quickly when connections are made 2786 * to dead hosts. Hosts can be labeled dead by adding a reject route 2787 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2788 */ 2789 if ((ire->ire_flags & RTF_REJECT) && 2790 (ire->ire_flags & RTF_PRIVATE)) 2791 goto error; 2792 2793 /* 2794 * Make use of the cached rtt and rtt_sd values to calculate the 2795 * initial RTO. Note that they are already initialized in 2796 * tcp_init_values(). 2797 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2798 * IP_NEXTHOP, but instead are using the interface ire for the 2799 * nexthop, then we do not use the ire_uinfo from that ire to 2800 * do any initializations. 2801 */ 2802 if (ire_uinfo != NULL) { 2803 if (ire_uinfo->iulp_rtt != 0) { 2804 clock_t rto; 2805 2806 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2807 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2808 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2809 tcps->tcps_rexmit_interval_extra + 2810 (tcp->tcp_rtt_sa >> 5); 2811 2812 if (rto > tcps->tcps_rexmit_interval_max) { 2813 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2814 } else if (rto < tcps->tcps_rexmit_interval_min) { 2815 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2816 } else { 2817 tcp->tcp_rto = rto; 2818 } 2819 } 2820 if (ire_uinfo->iulp_ssthresh != 0) 2821 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2822 else 2823 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2824 if (ire_uinfo->iulp_spipe > 0) { 2825 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2826 tcps->tcps_max_buf); 2827 if (tcps->tcps_snd_lowat_fraction != 0) 2828 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2829 tcps->tcps_snd_lowat_fraction; 2830 (void) tcp_maxpsz_set(tcp, B_TRUE); 2831 } 2832 /* 2833 * Note that up till now, acceptor always inherits receive 2834 * window from the listener. But if there is a metrics 2835 * associated with a host, we should use that instead of 2836 * inheriting it from listener. Thus we need to pass this 2837 * info back to the caller. 2838 */ 2839 if (ire_uinfo->iulp_rpipe > 0) { 2840 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2841 tcps->tcps_max_buf); 2842 } 2843 2844 if (ire_uinfo->iulp_rtomax > 0) { 2845 tcp->tcp_second_timer_threshold = 2846 ire_uinfo->iulp_rtomax; 2847 } 2848 2849 /* 2850 * Use the metric option settings, iulp_tstamp_ok and 2851 * iulp_wscale_ok, only for active open. What this means 2852 * is that if the other side uses timestamp or window 2853 * scale option, TCP will also use those options. That 2854 * is for passive open. If the application sets a 2855 * large window, window scale is enabled regardless of 2856 * the value in iulp_wscale_ok. This is the behavior 2857 * since 2.6. So we keep it. 2858 * The only case left in passive open processing is the 2859 * check for SACK. 2860 * For ECN, it should probably be like SACK. But the 2861 * current value is binary, so we treat it like the other 2862 * cases. The metric only controls active open.For passive 2863 * open, the ndd param, tcp_ecn_permitted, controls the 2864 * behavior. 2865 */ 2866 if (!tcp_detached) { 2867 /* 2868 * The if check means that the following can only 2869 * be turned on by the metrics only IRE, but not off. 2870 */ 2871 if (ire_uinfo->iulp_tstamp_ok) 2872 tcp->tcp_snd_ts_ok = B_TRUE; 2873 if (ire_uinfo->iulp_wscale_ok) 2874 tcp->tcp_snd_ws_ok = B_TRUE; 2875 if (ire_uinfo->iulp_sack == 2) 2876 tcp->tcp_snd_sack_ok = B_TRUE; 2877 if (ire_uinfo->iulp_ecn_ok) 2878 tcp->tcp_ecn_ok = B_TRUE; 2879 } else { 2880 /* 2881 * Passive open. 2882 * 2883 * As above, the if check means that SACK can only be 2884 * turned on by the metric only IRE. 2885 */ 2886 if (ire_uinfo->iulp_sack > 0) { 2887 tcp->tcp_snd_sack_ok = B_TRUE; 2888 } 2889 } 2890 } 2891 2892 2893 /* 2894 * XXX: Note that currently, ire_max_frag can be as small as 68 2895 * because of PMTUd. So tcp_mss may go to negative if combined 2896 * length of all those options exceeds 28 bytes. But because 2897 * of the tcp_mss_min check below, we may not have a problem if 2898 * tcp_mss_min is of a reasonable value. The default is 1 so 2899 * the negative problem still exists. And the check defeats PMTUd. 2900 * In fact, if PMTUd finds that the MSS should be smaller than 2901 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2902 * value. 2903 * 2904 * We do not deal with that now. All those problems related to 2905 * PMTUd will be fixed later. 2906 */ 2907 ASSERT(ire->ire_max_frag != 0); 2908 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2909 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2910 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2911 mss = MIN(mss, IPV6_MIN_MTU); 2912 } 2913 } 2914 2915 /* Sanity check for MSS value. */ 2916 if (tcp->tcp_ipversion == IPV4_VERSION) 2917 mss_max = tcps->tcps_mss_max_ipv4; 2918 else 2919 mss_max = tcps->tcps_mss_max_ipv6; 2920 2921 if (tcp->tcp_ipversion == IPV6_VERSION && 2922 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2923 /* 2924 * After receiving an ICMPv6 "packet too big" message with a 2925 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2926 * will insert a 8-byte fragment header in every packet; we 2927 * reduce the MSS by that amount here. 2928 */ 2929 mss -= sizeof (ip6_frag_t); 2930 } 2931 2932 if (tcp->tcp_ipsec_overhead == 0) 2933 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2934 2935 mss -= tcp->tcp_ipsec_overhead; 2936 2937 if (mss < tcps->tcps_mss_min) 2938 mss = tcps->tcps_mss_min; 2939 if (mss > mss_max) 2940 mss = mss_max; 2941 2942 /* Note that this is the maximum MSS, excluding all options. */ 2943 tcp->tcp_mss = mss; 2944 2945 /* 2946 * Initialize the ISS here now that we have the full connection ID. 2947 * The RFC 1948 method of initial sequence number generation requires 2948 * knowledge of the full connection ID before setting the ISS. 2949 */ 2950 2951 tcp_iss_init(tcp); 2952 2953 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2954 tcp->tcp_loopback = B_TRUE; 2955 2956 if (tcp->tcp_ipversion == IPV4_VERSION) { 2957 hsp = tcp_hsp_lookup(tcp->tcp_remote, tcps); 2958 } else { 2959 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6, tcps); 2960 } 2961 2962 if (hsp != NULL) { 2963 /* Only modify if we're going to make them bigger */ 2964 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2965 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2966 if (tcps->tcps_snd_lowat_fraction != 0) 2967 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2968 tcps->tcps_snd_lowat_fraction; 2969 } 2970 2971 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2972 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2973 } 2974 2975 /* Copy timestamp flag only for active open */ 2976 if (!tcp_detached) 2977 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 2978 } 2979 2980 if (sire != NULL) 2981 IRE_REFRELE(sire); 2982 2983 /* 2984 * If we got an IRE_CACHE and an ILL, go through their properties; 2985 * otherwise, this is deferred until later when we have an IRE_CACHE. 2986 */ 2987 if (tcp->tcp_loopback || 2988 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2989 /* 2990 * For incoming, see if this tcp may be MDT-capable. For 2991 * outgoing, this process has been taken care of through 2992 * tcp_rput_other. 2993 */ 2994 tcp_ire_ill_check(tcp, ire, ill, incoming); 2995 tcp->tcp_ire_ill_check_done = B_TRUE; 2996 } 2997 2998 mutex_enter(&connp->conn_lock); 2999 /* 3000 * Make sure that conn is not marked incipient 3001 * for incoming connections. A blind 3002 * removal of incipient flag is cheaper than 3003 * check and removal. 3004 */ 3005 connp->conn_state_flags &= ~CONN_INCIPIENT; 3006 3007 /* 3008 * Must not cache forwarding table routes 3009 * or recache an IRE after the conn_t has 3010 * had conn_ire_cache cleared and is flagged 3011 * unusable, (see the CONN_CACHE_IRE() macro). 3012 */ 3013 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 3014 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 3015 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 3016 connp->conn_ire_cache = ire; 3017 IRE_UNTRACE_REF(ire); 3018 rw_exit(&ire->ire_bucket->irb_lock); 3019 mutex_exit(&connp->conn_lock); 3020 return (1); 3021 } 3022 rw_exit(&ire->ire_bucket->irb_lock); 3023 } 3024 mutex_exit(&connp->conn_lock); 3025 3026 if (ire->ire_mp == NULL) 3027 ire_refrele(ire); 3028 return (1); 3029 3030 error: 3031 if (ire->ire_mp == NULL) 3032 ire_refrele(ire); 3033 if (sire != NULL) 3034 ire_refrele(sire); 3035 return (0); 3036 } 3037 3038 /* 3039 * tcp_bind is called (holding the writer lock) by tcp_wput_proto to process a 3040 * O_T_BIND_REQ/T_BIND_REQ message. 3041 */ 3042 static void 3043 tcp_bind(tcp_t *tcp, mblk_t *mp) 3044 { 3045 sin_t *sin; 3046 sin6_t *sin6; 3047 mblk_t *mp1; 3048 in_port_t requested_port; 3049 in_port_t allocated_port; 3050 struct T_bind_req *tbr; 3051 boolean_t bind_to_req_port_only; 3052 boolean_t backlog_update = B_FALSE; 3053 boolean_t user_specified; 3054 in6_addr_t v6addr; 3055 ipaddr_t v4addr; 3056 uint_t origipversion; 3057 int err; 3058 queue_t *q = tcp->tcp_wq; 3059 conn_t *connp = tcp->tcp_connp; 3060 mlp_type_t addrtype, mlptype; 3061 zone_t *zone; 3062 cred_t *cr; 3063 in_port_t mlp_port; 3064 tcp_stack_t *tcps = tcp->tcp_tcps; 3065 3066 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3067 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3068 if (tcp->tcp_debug) { 3069 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3070 "tcp_bind: bad req, len %u", 3071 (uint_t)(mp->b_wptr - mp->b_rptr)); 3072 } 3073 tcp_err_ack(tcp, mp, TPROTO, 0); 3074 return; 3075 } 3076 /* Make sure the largest address fits */ 3077 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3078 if (mp1 == NULL) { 3079 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3080 return; 3081 } 3082 mp = mp1; 3083 tbr = (struct T_bind_req *)mp->b_rptr; 3084 if (tcp->tcp_state >= TCPS_BOUND) { 3085 if ((tcp->tcp_state == TCPS_BOUND || 3086 tcp->tcp_state == TCPS_LISTEN) && 3087 tcp->tcp_conn_req_max != tbr->CONIND_number && 3088 tbr->CONIND_number > 0) { 3089 /* 3090 * Handle listen() increasing CONIND_number. 3091 * This is more "liberal" then what the TPI spec 3092 * requires but is needed to avoid a t_unbind 3093 * when handling listen() since the port number 3094 * might be "stolen" between the unbind and bind. 3095 */ 3096 backlog_update = B_TRUE; 3097 goto do_bind; 3098 } 3099 if (tcp->tcp_debug) { 3100 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3101 "tcp_bind: bad state, %d", tcp->tcp_state); 3102 } 3103 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 3104 return; 3105 } 3106 origipversion = tcp->tcp_ipversion; 3107 3108 switch (tbr->ADDR_length) { 3109 case 0: /* request for a generic port */ 3110 tbr->ADDR_offset = sizeof (struct T_bind_req); 3111 if (tcp->tcp_family == AF_INET) { 3112 tbr->ADDR_length = sizeof (sin_t); 3113 sin = (sin_t *)&tbr[1]; 3114 *sin = sin_null; 3115 sin->sin_family = AF_INET; 3116 mp->b_wptr = (uchar_t *)&sin[1]; 3117 tcp->tcp_ipversion = IPV4_VERSION; 3118 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 3119 } else { 3120 ASSERT(tcp->tcp_family == AF_INET6); 3121 tbr->ADDR_length = sizeof (sin6_t); 3122 sin6 = (sin6_t *)&tbr[1]; 3123 *sin6 = sin6_null; 3124 sin6->sin6_family = AF_INET6; 3125 mp->b_wptr = (uchar_t *)&sin6[1]; 3126 tcp->tcp_ipversion = IPV6_VERSION; 3127 V6_SET_ZERO(v6addr); 3128 } 3129 requested_port = 0; 3130 break; 3131 3132 case sizeof (sin_t): /* Complete IPv4 address */ 3133 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3134 sizeof (sin_t)); 3135 if (sin == NULL || !OK_32PTR((char *)sin)) { 3136 if (tcp->tcp_debug) { 3137 (void) strlog(TCP_MOD_ID, 0, 1, 3138 SL_ERROR|SL_TRACE, 3139 "tcp_bind: bad address parameter, " 3140 "offset %d, len %d", 3141 tbr->ADDR_offset, tbr->ADDR_length); 3142 } 3143 tcp_err_ack(tcp, mp, TPROTO, 0); 3144 return; 3145 } 3146 /* 3147 * With sockets sockfs will accept bogus sin_family in 3148 * bind() and replace it with the family used in the socket 3149 * call. 3150 */ 3151 if (sin->sin_family != AF_INET || 3152 tcp->tcp_family != AF_INET) { 3153 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3154 return; 3155 } 3156 requested_port = ntohs(sin->sin_port); 3157 tcp->tcp_ipversion = IPV4_VERSION; 3158 v4addr = sin->sin_addr.s_addr; 3159 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 3160 break; 3161 3162 case sizeof (sin6_t): /* Complete IPv6 address */ 3163 sin6 = (sin6_t *)mi_offset_param(mp, 3164 tbr->ADDR_offset, sizeof (sin6_t)); 3165 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3166 if (tcp->tcp_debug) { 3167 (void) strlog(TCP_MOD_ID, 0, 1, 3168 SL_ERROR|SL_TRACE, 3169 "tcp_bind: bad IPv6 address parameter, " 3170 "offset %d, len %d", tbr->ADDR_offset, 3171 tbr->ADDR_length); 3172 } 3173 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3174 return; 3175 } 3176 if (sin6->sin6_family != AF_INET6 || 3177 tcp->tcp_family != AF_INET6) { 3178 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3179 return; 3180 } 3181 requested_port = ntohs(sin6->sin6_port); 3182 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 3183 IPV4_VERSION : IPV6_VERSION; 3184 v6addr = sin6->sin6_addr; 3185 break; 3186 3187 default: 3188 if (tcp->tcp_debug) { 3189 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3190 "tcp_bind: bad address length, %d", 3191 tbr->ADDR_length); 3192 } 3193 tcp_err_ack(tcp, mp, TBADADDR, 0); 3194 return; 3195 } 3196 tcp->tcp_bound_source_v6 = v6addr; 3197 3198 /* Check for change in ipversion */ 3199 if (origipversion != tcp->tcp_ipversion) { 3200 ASSERT(tcp->tcp_family == AF_INET6); 3201 err = tcp->tcp_ipversion == IPV6_VERSION ? 3202 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 3203 if (err) { 3204 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3205 return; 3206 } 3207 } 3208 3209 /* 3210 * Initialize family specific fields. Copy of the src addr. 3211 * in tcp_t is needed for the lookup funcs. 3212 */ 3213 if (tcp->tcp_ipversion == IPV6_VERSION) { 3214 tcp->tcp_ip6h->ip6_src = v6addr; 3215 } else { 3216 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 3217 } 3218 tcp->tcp_ip_src_v6 = v6addr; 3219 3220 /* 3221 * For O_T_BIND_REQ: 3222 * Verify that the target port/addr is available, or choose 3223 * another. 3224 * For T_BIND_REQ: 3225 * Verify that the target port/addr is available or fail. 3226 * In both cases when it succeeds the tcp is inserted in the 3227 * bind hash table. This ensures that the operation is atomic 3228 * under the lock on the hash bucket. 3229 */ 3230 bind_to_req_port_only = requested_port != 0 && 3231 tbr->PRIM_type != O_T_BIND_REQ; 3232 /* 3233 * Get a valid port (within the anonymous range and should not 3234 * be a privileged one) to use if the user has not given a port. 3235 * If multiple threads are here, they may all start with 3236 * with the same initial port. But, it should be fine as long as 3237 * tcp_bindi will ensure that no two threads will be assigned 3238 * the same port. 3239 * 3240 * NOTE: XXX If a privileged process asks for an anonymous port, we 3241 * still check for ports only in the range > tcp_smallest_non_priv_port, 3242 * unless TCP_ANONPRIVBIND option is set. 3243 */ 3244 mlptype = mlptSingle; 3245 mlp_port = requested_port; 3246 if (requested_port == 0) { 3247 requested_port = tcp->tcp_anon_priv_bind ? 3248 tcp_get_next_priv_port(tcp) : 3249 tcp_update_next_port(tcps->tcps_next_port_to_try, 3250 tcp, B_TRUE); 3251 if (requested_port == 0) { 3252 tcp_err_ack(tcp, mp, TNOADDR, 0); 3253 return; 3254 } 3255 user_specified = B_FALSE; 3256 3257 /* 3258 * If the user went through one of the RPC interfaces to create 3259 * this socket and RPC is MLP in this zone, then give him an 3260 * anonymous MLP. 3261 */ 3262 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3263 if (connp->conn_anon_mlp && is_system_labeled()) { 3264 zone = crgetzone(cr); 3265 addrtype = tsol_mlp_addr_type(zone->zone_id, 3266 IPV6_VERSION, &v6addr, 3267 tcps->tcps_netstack->netstack_ip); 3268 if (addrtype == mlptSingle) { 3269 tcp_err_ack(tcp, mp, TNOADDR, 0); 3270 return; 3271 } 3272 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3273 PMAPPORT, addrtype); 3274 mlp_port = PMAPPORT; 3275 } 3276 } else { 3277 int i; 3278 boolean_t priv = B_FALSE; 3279 3280 /* 3281 * If the requested_port is in the well-known privileged range, 3282 * verify that the stream was opened by a privileged user. 3283 * Note: No locks are held when inspecting tcp_g_*epriv_ports 3284 * but instead the code relies on: 3285 * - the fact that the address of the array and its size never 3286 * changes 3287 * - the atomic assignment of the elements of the array 3288 */ 3289 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3290 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 3291 priv = B_TRUE; 3292 } else { 3293 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 3294 if (requested_port == 3295 tcps->tcps_g_epriv_ports[i]) { 3296 priv = B_TRUE; 3297 break; 3298 } 3299 } 3300 } 3301 if (priv) { 3302 if (secpolicy_net_privaddr(cr, requested_port, 3303 IPPROTO_TCP) != 0) { 3304 if (tcp->tcp_debug) { 3305 (void) strlog(TCP_MOD_ID, 0, 1, 3306 SL_ERROR|SL_TRACE, 3307 "tcp_bind: no priv for port %d", 3308 requested_port); 3309 } 3310 tcp_err_ack(tcp, mp, TACCES, 0); 3311 return; 3312 } 3313 } 3314 user_specified = B_TRUE; 3315 3316 if (is_system_labeled()) { 3317 zone = crgetzone(cr); 3318 addrtype = tsol_mlp_addr_type(zone->zone_id, 3319 IPV6_VERSION, &v6addr, 3320 tcps->tcps_netstack->netstack_ip); 3321 if (addrtype == mlptSingle) { 3322 tcp_err_ack(tcp, mp, TNOADDR, 0); 3323 return; 3324 } 3325 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3326 requested_port, addrtype); 3327 } 3328 } 3329 3330 if (mlptype != mlptSingle) { 3331 if (secpolicy_net_bindmlp(cr) != 0) { 3332 if (tcp->tcp_debug) { 3333 (void) strlog(TCP_MOD_ID, 0, 1, 3334 SL_ERROR|SL_TRACE, 3335 "tcp_bind: no priv for multilevel port %d", 3336 requested_port); 3337 } 3338 tcp_err_ack(tcp, mp, TACCES, 0); 3339 return; 3340 } 3341 3342 /* 3343 * If we're specifically binding a shared IP address and the 3344 * port is MLP on shared addresses, then check to see if this 3345 * zone actually owns the MLP. Reject if not. 3346 */ 3347 if (mlptype == mlptShared && addrtype == mlptShared) { 3348 /* 3349 * No need to handle exclusive-stack zones since 3350 * ALL_ZONES only applies to the shared stack. 3351 */ 3352 zoneid_t mlpzone; 3353 3354 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 3355 htons(mlp_port)); 3356 if (connp->conn_zoneid != mlpzone) { 3357 if (tcp->tcp_debug) { 3358 (void) strlog(TCP_MOD_ID, 0, 1, 3359 SL_ERROR|SL_TRACE, 3360 "tcp_bind: attempt to bind port " 3361 "%d on shared addr in zone %d " 3362 "(should be %d)", 3363 mlp_port, connp->conn_zoneid, 3364 mlpzone); 3365 } 3366 tcp_err_ack(tcp, mp, TACCES, 0); 3367 return; 3368 } 3369 } 3370 3371 if (!user_specified) { 3372 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3373 requested_port, B_TRUE); 3374 if (err != 0) { 3375 if (tcp->tcp_debug) { 3376 (void) strlog(TCP_MOD_ID, 0, 1, 3377 SL_ERROR|SL_TRACE, 3378 "tcp_bind: cannot establish anon " 3379 "MLP for port %d", 3380 requested_port); 3381 } 3382 tcp_err_ack(tcp, mp, TSYSERR, err); 3383 return; 3384 } 3385 connp->conn_anon_port = B_TRUE; 3386 } 3387 connp->conn_mlp_type = mlptype; 3388 } 3389 3390 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 3391 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 3392 3393 if (allocated_port == 0) { 3394 connp->conn_mlp_type = mlptSingle; 3395 if (connp->conn_anon_port) { 3396 connp->conn_anon_port = B_FALSE; 3397 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3398 requested_port, B_FALSE); 3399 } 3400 if (bind_to_req_port_only) { 3401 if (tcp->tcp_debug) { 3402 (void) strlog(TCP_MOD_ID, 0, 1, 3403 SL_ERROR|SL_TRACE, 3404 "tcp_bind: requested addr busy"); 3405 } 3406 tcp_err_ack(tcp, mp, TADDRBUSY, 0); 3407 } else { 3408 /* If we are out of ports, fail the bind. */ 3409 if (tcp->tcp_debug) { 3410 (void) strlog(TCP_MOD_ID, 0, 1, 3411 SL_ERROR|SL_TRACE, 3412 "tcp_bind: out of ports?"); 3413 } 3414 tcp_err_ack(tcp, mp, TNOADDR, 0); 3415 } 3416 return; 3417 } 3418 ASSERT(tcp->tcp_state == TCPS_BOUND); 3419 do_bind: 3420 if (!backlog_update) { 3421 if (tcp->tcp_family == AF_INET) 3422 sin->sin_port = htons(allocated_port); 3423 else 3424 sin6->sin6_port = htons(allocated_port); 3425 } 3426 if (tcp->tcp_family == AF_INET) { 3427 if (tbr->CONIND_number != 0) { 3428 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3429 sizeof (sin_t)); 3430 } else { 3431 /* Just verify the local IP address */ 3432 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, IP_ADDR_LEN); 3433 } 3434 } else { 3435 if (tbr->CONIND_number != 0) { 3436 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3437 sizeof (sin6_t)); 3438 } else { 3439 /* Just verify the local IP address */ 3440 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3441 IPV6_ADDR_LEN); 3442 } 3443 } 3444 if (mp1 == NULL) { 3445 if (connp->conn_anon_port) { 3446 connp->conn_anon_port = B_FALSE; 3447 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3448 requested_port, B_FALSE); 3449 } 3450 connp->conn_mlp_type = mlptSingle; 3451 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3452 return; 3453 } 3454 3455 tbr->PRIM_type = T_BIND_ACK; 3456 mp->b_datap->db_type = M_PCPROTO; 3457 3458 /* Chain in the reply mp for tcp_rput() */ 3459 mp1->b_cont = mp; 3460 mp = mp1; 3461 3462 tcp->tcp_conn_req_max = tbr->CONIND_number; 3463 if (tcp->tcp_conn_req_max) { 3464 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 3465 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 3466 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 3467 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 3468 /* 3469 * If this is a listener, do not reset the eager list 3470 * and other stuffs. Note that we don't check if the 3471 * existing eager list meets the new tcp_conn_req_max 3472 * requirement. 3473 */ 3474 if (tcp->tcp_state != TCPS_LISTEN) { 3475 tcp->tcp_state = TCPS_LISTEN; 3476 /* Initialize the chain. Don't need the eager_lock */ 3477 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 3478 tcp->tcp_eager_next_drop_q0 = tcp; 3479 tcp->tcp_eager_prev_drop_q0 = tcp; 3480 tcp->tcp_second_ctimer_threshold = 3481 tcps->tcps_ip_abort_linterval; 3482 } 3483 } 3484 3485 /* 3486 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 3487 * processing continues in tcp_rput_other(). 3488 * 3489 * We need to make sure that the conn_recv is set to a non-null 3490 * value before we insert the conn into the classifier table. 3491 * This is to avoid a race with an incoming packet which does an 3492 * ipcl_classify(). 3493 */ 3494 connp->conn_recv = tcp_conn_request; 3495 if (tcp->tcp_family == AF_INET6) { 3496 ASSERT(tcp->tcp_connp->conn_af_isv6); 3497 mp = ip_bind_v6(q, mp, tcp->tcp_connp, &tcp->tcp_sticky_ipp); 3498 } else { 3499 ASSERT(!tcp->tcp_connp->conn_af_isv6); 3500 mp = ip_bind_v4(q, mp, tcp->tcp_connp); 3501 } 3502 /* 3503 * If the bind cannot complete immediately 3504 * IP will arrange to call tcp_rput_other 3505 * when the bind completes. 3506 */ 3507 if (mp != NULL) { 3508 tcp_rput_other(tcp, mp); 3509 } else { 3510 /* 3511 * Bind will be resumed later. Need to ensure 3512 * that conn doesn't disappear when that happens. 3513 * This will be decremented in ip_resume_tcp_bind(). 3514 */ 3515 CONN_INC_REF(tcp->tcp_connp); 3516 } 3517 } 3518 3519 3520 /* 3521 * If the "bind_to_req_port_only" parameter is set, if the requested port 3522 * number is available, return it, If not return 0 3523 * 3524 * If "bind_to_req_port_only" parameter is not set and 3525 * If the requested port number is available, return it. If not, return 3526 * the first anonymous port we happen across. If no anonymous ports are 3527 * available, return 0. addr is the requested local address, if any. 3528 * 3529 * In either case, when succeeding update the tcp_t to record the port number 3530 * and insert it in the bind hash table. 3531 * 3532 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3533 * without setting SO_REUSEADDR. This is needed so that they 3534 * can be viewed as two independent transport protocols. 3535 */ 3536 static in_port_t 3537 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3538 int reuseaddr, boolean_t quick_connect, 3539 boolean_t bind_to_req_port_only, boolean_t user_specified) 3540 { 3541 /* number of times we have run around the loop */ 3542 int count = 0; 3543 /* maximum number of times to run around the loop */ 3544 int loopmax; 3545 conn_t *connp = tcp->tcp_connp; 3546 zoneid_t zoneid = connp->conn_zoneid; 3547 tcp_stack_t *tcps = tcp->tcp_tcps; 3548 3549 /* 3550 * Lookup for free addresses is done in a loop and "loopmax" 3551 * influences how long we spin in the loop 3552 */ 3553 if (bind_to_req_port_only) { 3554 /* 3555 * If the requested port is busy, don't bother to look 3556 * for a new one. Setting loop maximum count to 1 has 3557 * that effect. 3558 */ 3559 loopmax = 1; 3560 } else { 3561 /* 3562 * If the requested port is busy, look for a free one 3563 * in the anonymous port range. 3564 * Set loopmax appropriately so that one does not look 3565 * forever in the case all of the anonymous ports are in use. 3566 */ 3567 if (tcp->tcp_anon_priv_bind) { 3568 /* 3569 * loopmax = 3570 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3571 */ 3572 loopmax = IPPORT_RESERVED - 3573 tcps->tcps_min_anonpriv_port; 3574 } else { 3575 loopmax = (tcps->tcps_largest_anon_port - 3576 tcps->tcps_smallest_anon_port + 1); 3577 } 3578 } 3579 do { 3580 uint16_t lport; 3581 tf_t *tbf; 3582 tcp_t *ltcp; 3583 conn_t *lconnp; 3584 3585 lport = htons(port); 3586 3587 /* 3588 * Ensure that the tcp_t is not currently in the bind hash. 3589 * Hold the lock on the hash bucket to ensure that 3590 * the duplicate check plus the insertion is an atomic 3591 * operation. 3592 * 3593 * This function does an inline lookup on the bind hash list 3594 * Make sure that we access only members of tcp_t 3595 * and that we don't look at tcp_tcp, since we are not 3596 * doing a CONN_INC_REF. 3597 */ 3598 tcp_bind_hash_remove(tcp); 3599 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3600 mutex_enter(&tbf->tf_lock); 3601 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3602 ltcp = ltcp->tcp_bind_hash) { 3603 boolean_t not_socket; 3604 boolean_t exclbind; 3605 3606 if (lport != ltcp->tcp_lport) 3607 continue; 3608 3609 lconnp = ltcp->tcp_connp; 3610 3611 /* 3612 * On a labeled system, we must treat bindings to ports 3613 * on shared IP addresses by sockets with MAC exemption 3614 * privilege as being in all zones, as there's 3615 * otherwise no way to identify the right receiver. 3616 */ 3617 if (!(IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) || 3618 IPCL_ZONE_MATCH(connp, 3619 ltcp->tcp_connp->conn_zoneid)) && 3620 !lconnp->conn_mac_exempt && 3621 !connp->conn_mac_exempt) 3622 continue; 3623 3624 /* 3625 * If TCP_EXCLBIND is set for either the bound or 3626 * binding endpoint, the semantics of bind 3627 * is changed according to the following. 3628 * 3629 * spec = specified address (v4 or v6) 3630 * unspec = unspecified address (v4 or v6) 3631 * A = specified addresses are different for endpoints 3632 * 3633 * bound bind to allowed 3634 * ------------------------------------- 3635 * unspec unspec no 3636 * unspec spec no 3637 * spec unspec no 3638 * spec spec yes if A 3639 * 3640 * For labeled systems, SO_MAC_EXEMPT behaves the same 3641 * as TCP_EXCLBIND, except that zoneid is ignored. 3642 * 3643 * Note: 3644 * 3645 * 1. Because of TLI semantics, an endpoint can go 3646 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3647 * TCPS_BOUND, depending on whether it is originally 3648 * a listener or not. That is why we need to check 3649 * for states greater than or equal to TCPS_BOUND 3650 * here. 3651 * 3652 * 2. Ideally, we should only check for state equals 3653 * to TCPS_LISTEN. And the following check should be 3654 * added. 3655 * 3656 * if (ltcp->tcp_state == TCPS_LISTEN || 3657 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3658 * ... 3659 * } 3660 * 3661 * The semantics will be changed to this. If the 3662 * endpoint on the list is in state not equal to 3663 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3664 * set, let the bind succeed. 3665 * 3666 * Because of (1), we cannot do that for TLI 3667 * endpoints. But we can do that for socket endpoints. 3668 * If in future, we can change this going back 3669 * semantics, we can use the above check for TLI also. 3670 */ 3671 not_socket = !(TCP_IS_SOCKET(ltcp) && 3672 TCP_IS_SOCKET(tcp)); 3673 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3674 3675 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3676 (exclbind && (not_socket || 3677 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3678 if (V6_OR_V4_INADDR_ANY( 3679 ltcp->tcp_bound_source_v6) || 3680 V6_OR_V4_INADDR_ANY(*laddr) || 3681 IN6_ARE_ADDR_EQUAL(laddr, 3682 <cp->tcp_bound_source_v6)) { 3683 break; 3684 } 3685 continue; 3686 } 3687 3688 /* 3689 * Check ipversion to allow IPv4 and IPv6 sockets to 3690 * have disjoint port number spaces, if *_EXCLBIND 3691 * is not set and only if the application binds to a 3692 * specific port. We use the same autoassigned port 3693 * number space for IPv4 and IPv6 sockets. 3694 */ 3695 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3696 bind_to_req_port_only) 3697 continue; 3698 3699 /* 3700 * Ideally, we should make sure that the source 3701 * address, remote address, and remote port in the 3702 * four tuple for this tcp-connection is unique. 3703 * However, trying to find out the local source 3704 * address would require too much code duplication 3705 * with IP, since IP needs needs to have that code 3706 * to support userland TCP implementations. 3707 */ 3708 if (quick_connect && 3709 (ltcp->tcp_state > TCPS_LISTEN) && 3710 ((tcp->tcp_fport != ltcp->tcp_fport) || 3711 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3712 <cp->tcp_remote_v6))) 3713 continue; 3714 3715 if (!reuseaddr) { 3716 /* 3717 * No socket option SO_REUSEADDR. 3718 * If existing port is bound to 3719 * a non-wildcard IP address 3720 * and the requesting stream is 3721 * bound to a distinct 3722 * different IP addresses 3723 * (non-wildcard, also), keep 3724 * going. 3725 */ 3726 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3727 !V6_OR_V4_INADDR_ANY( 3728 ltcp->tcp_bound_source_v6) && 3729 !IN6_ARE_ADDR_EQUAL(laddr, 3730 <cp->tcp_bound_source_v6)) 3731 continue; 3732 if (ltcp->tcp_state >= TCPS_BOUND) { 3733 /* 3734 * This port is being used and 3735 * its state is >= TCPS_BOUND, 3736 * so we can't bind to it. 3737 */ 3738 break; 3739 } 3740 } else { 3741 /* 3742 * socket option SO_REUSEADDR is set on the 3743 * binding tcp_t. 3744 * 3745 * If two streams are bound to 3746 * same IP address or both addr 3747 * and bound source are wildcards 3748 * (INADDR_ANY), we want to stop 3749 * searching. 3750 * We have found a match of IP source 3751 * address and source port, which is 3752 * refused regardless of the 3753 * SO_REUSEADDR setting, so we break. 3754 */ 3755 if (IN6_ARE_ADDR_EQUAL(laddr, 3756 <cp->tcp_bound_source_v6) && 3757 (ltcp->tcp_state == TCPS_LISTEN || 3758 ltcp->tcp_state == TCPS_BOUND)) 3759 break; 3760 } 3761 } 3762 if (ltcp != NULL) { 3763 /* The port number is busy */ 3764 mutex_exit(&tbf->tf_lock); 3765 } else { 3766 /* 3767 * This port is ours. Insert in fanout and mark as 3768 * bound to prevent others from getting the port 3769 * number. 3770 */ 3771 tcp->tcp_state = TCPS_BOUND; 3772 tcp->tcp_lport = htons(port); 3773 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3774 3775 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3776 tcp->tcp_lport)] == tbf); 3777 tcp_bind_hash_insert(tbf, tcp, 1); 3778 3779 mutex_exit(&tbf->tf_lock); 3780 3781 /* 3782 * We don't want tcp_next_port_to_try to "inherit" 3783 * a port number supplied by the user in a bind. 3784 */ 3785 if (user_specified) 3786 return (port); 3787 3788 /* 3789 * This is the only place where tcp_next_port_to_try 3790 * is updated. After the update, it may or may not 3791 * be in the valid range. 3792 */ 3793 if (!tcp->tcp_anon_priv_bind) 3794 tcps->tcps_next_port_to_try = port + 1; 3795 return (port); 3796 } 3797 3798 if (tcp->tcp_anon_priv_bind) { 3799 port = tcp_get_next_priv_port(tcp); 3800 } else { 3801 if (count == 0 && user_specified) { 3802 /* 3803 * We may have to return an anonymous port. So 3804 * get one to start with. 3805 */ 3806 port = 3807 tcp_update_next_port( 3808 tcps->tcps_next_port_to_try, 3809 tcp, B_TRUE); 3810 user_specified = B_FALSE; 3811 } else { 3812 port = tcp_update_next_port(port + 1, tcp, 3813 B_FALSE); 3814 } 3815 } 3816 if (port == 0) 3817 break; 3818 3819 /* 3820 * Don't let this loop run forever in the case where 3821 * all of the anonymous ports are in use. 3822 */ 3823 } while (++count < loopmax); 3824 return (0); 3825 } 3826 3827 /* 3828 * tcp_clean_death / tcp_close_detached must not be called more than once 3829 * on a tcp. Thus every function that potentially calls tcp_clean_death 3830 * must check for the tcp state before calling tcp_clean_death. 3831 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3832 * tcp_timer_handler, all check for the tcp state. 3833 */ 3834 /* ARGSUSED */ 3835 void 3836 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3837 { 3838 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3839 3840 freemsg(mp); 3841 if (tcp->tcp_state > TCPS_BOUND) 3842 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, 3843 ETIMEDOUT, 5); 3844 } 3845 3846 /* 3847 * We are dying for some reason. Try to do it gracefully. (May be called 3848 * as writer.) 3849 * 3850 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3851 * done by a service procedure). 3852 * TBD - Should the return value distinguish between the tcp_t being 3853 * freed and it being reinitialized? 3854 */ 3855 static int 3856 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3857 { 3858 mblk_t *mp; 3859 queue_t *q; 3860 tcp_stack_t *tcps = tcp->tcp_tcps; 3861 sodirect_t *sodp; 3862 3863 TCP_CLD_STAT(tag); 3864 3865 #if TCP_TAG_CLEAN_DEATH 3866 tcp->tcp_cleandeathtag = tag; 3867 #endif 3868 3869 if (tcp->tcp_fused) 3870 tcp_unfuse(tcp); 3871 3872 if (tcp->tcp_linger_tid != 0 && 3873 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3874 tcp_stop_lingering(tcp); 3875 } 3876 3877 ASSERT(tcp != NULL); 3878 ASSERT((tcp->tcp_family == AF_INET && 3879 tcp->tcp_ipversion == IPV4_VERSION) || 3880 (tcp->tcp_family == AF_INET6 && 3881 (tcp->tcp_ipversion == IPV4_VERSION || 3882 tcp->tcp_ipversion == IPV6_VERSION))); 3883 3884 if (TCP_IS_DETACHED(tcp)) { 3885 if (tcp->tcp_hard_binding) { 3886 /* 3887 * Its an eager that we are dealing with. We close the 3888 * eager but in case a conn_ind has already gone to the 3889 * listener, let tcp_accept_finish() send a discon_ind 3890 * to the listener and drop the last reference. If the 3891 * listener doesn't even know about the eager i.e. the 3892 * conn_ind hasn't gone up, blow away the eager and drop 3893 * the last reference as well. If the conn_ind has gone 3894 * up, state should be BOUND. tcp_accept_finish 3895 * will figure out that the connection has received a 3896 * RST and will send a DISCON_IND to the application. 3897 */ 3898 tcp_closei_local(tcp); 3899 if (!tcp->tcp_tconnind_started) { 3900 CONN_DEC_REF(tcp->tcp_connp); 3901 } else { 3902 tcp->tcp_state = TCPS_BOUND; 3903 } 3904 } else { 3905 tcp_close_detached(tcp); 3906 } 3907 return (0); 3908 } 3909 3910 TCP_STAT(tcps, tcp_clean_death_nondetached); 3911 3912 /* 3913 * If T_ORDREL_IND has not been sent yet (done when service routine 3914 * is run) postpone cleaning up the endpoint until service routine 3915 * has sent up the T_ORDREL_IND. Avoid clearing out an existing 3916 * client_errno since tcp_close uses the client_errno field. 3917 */ 3918 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 3919 if (err != 0) 3920 tcp->tcp_client_errno = err; 3921 3922 tcp->tcp_deferred_clean_death = B_TRUE; 3923 return (-1); 3924 } 3925 3926 /* If sodirect, not anymore */ 3927 SOD_PTR_ENTER(tcp, sodp); 3928 if (sodp != NULL) { 3929 tcp->tcp_sodirect = NULL; 3930 mutex_exit(sodp->sod_lock); 3931 } 3932 3933 q = tcp->tcp_rq; 3934 3935 /* Trash all inbound data */ 3936 flushq(q, FLUSHALL); 3937 3938 /* 3939 * If we are at least part way open and there is error 3940 * (err==0 implies no error) 3941 * notify our client by a T_DISCON_IND. 3942 */ 3943 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3944 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3945 !TCP_IS_SOCKET(tcp)) { 3946 /* 3947 * Send M_FLUSH according to TPI. Because sockets will 3948 * (and must) ignore FLUSHR we do that only for TPI 3949 * endpoints and sockets in STREAMS mode. 3950 */ 3951 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3952 } 3953 if (tcp->tcp_debug) { 3954 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3955 "tcp_clean_death: discon err %d", err); 3956 } 3957 mp = mi_tpi_discon_ind(NULL, err, 0); 3958 if (mp != NULL) { 3959 putnext(q, mp); 3960 } else { 3961 if (tcp->tcp_debug) { 3962 (void) strlog(TCP_MOD_ID, 0, 1, 3963 SL_ERROR|SL_TRACE, 3964 "tcp_clean_death, sending M_ERROR"); 3965 } 3966 (void) putnextctl1(q, M_ERROR, EPROTO); 3967 } 3968 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3969 /* SYN_SENT or SYN_RCVD */ 3970 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3971 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3972 /* ESTABLISHED or CLOSE_WAIT */ 3973 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3974 } 3975 } 3976 3977 tcp_reinit(tcp); 3978 return (-1); 3979 } 3980 3981 /* 3982 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3983 * to expire, stop the wait and finish the close. 3984 */ 3985 static void 3986 tcp_stop_lingering(tcp_t *tcp) 3987 { 3988 clock_t delta = 0; 3989 tcp_stack_t *tcps = tcp->tcp_tcps; 3990 3991 tcp->tcp_linger_tid = 0; 3992 if (tcp->tcp_state > TCPS_LISTEN) { 3993 tcp_acceptor_hash_remove(tcp); 3994 mutex_enter(&tcp->tcp_non_sq_lock); 3995 if (tcp->tcp_flow_stopped) { 3996 tcp_clrqfull(tcp); 3997 } 3998 mutex_exit(&tcp->tcp_non_sq_lock); 3999 4000 if (tcp->tcp_timer_tid != 0) { 4001 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4002 tcp->tcp_timer_tid = 0; 4003 } 4004 /* 4005 * Need to cancel those timers which will not be used when 4006 * TCP is detached. This has to be done before the tcp_wq 4007 * is set to the global queue. 4008 */ 4009 tcp_timers_stop(tcp); 4010 4011 4012 tcp->tcp_detached = B_TRUE; 4013 ASSERT(tcps->tcps_g_q != NULL); 4014 tcp->tcp_rq = tcps->tcps_g_q; 4015 tcp->tcp_wq = WR(tcps->tcps_g_q); 4016 4017 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4018 tcp_time_wait_append(tcp); 4019 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4020 goto finish; 4021 } 4022 4023 /* 4024 * If delta is zero the timer event wasn't executed and was 4025 * successfully canceled. In this case we need to restart it 4026 * with the minimal delta possible. 4027 */ 4028 if (delta >= 0) { 4029 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4030 delta ? delta : 1); 4031 } 4032 } else { 4033 tcp_closei_local(tcp); 4034 CONN_DEC_REF(tcp->tcp_connp); 4035 } 4036 finish: 4037 /* Signal closing thread that it can complete close */ 4038 mutex_enter(&tcp->tcp_closelock); 4039 tcp->tcp_detached = B_TRUE; 4040 ASSERT(tcps->tcps_g_q != NULL); 4041 tcp->tcp_rq = tcps->tcps_g_q; 4042 tcp->tcp_wq = WR(tcps->tcps_g_q); 4043 tcp->tcp_closed = 1; 4044 cv_signal(&tcp->tcp_closecv); 4045 mutex_exit(&tcp->tcp_closelock); 4046 } 4047 4048 /* 4049 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 4050 * expires. 4051 */ 4052 static void 4053 tcp_close_linger_timeout(void *arg) 4054 { 4055 conn_t *connp = (conn_t *)arg; 4056 tcp_t *tcp = connp->conn_tcp; 4057 4058 tcp->tcp_client_errno = ETIMEDOUT; 4059 tcp_stop_lingering(tcp); 4060 } 4061 4062 static int 4063 tcp_close(queue_t *q, int flags) 4064 { 4065 conn_t *connp = Q_TO_CONN(q); 4066 tcp_t *tcp = connp->conn_tcp; 4067 mblk_t *mp = &tcp->tcp_closemp; 4068 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 4069 mblk_t *bp; 4070 4071 ASSERT(WR(q)->q_next == NULL); 4072 ASSERT(connp->conn_ref >= 2); 4073 4074 /* 4075 * We are being closed as /dev/tcp or /dev/tcp6. 4076 * 4077 * Mark the conn as closing. ill_pending_mp_add will not 4078 * add any mp to the pending mp list, after this conn has 4079 * started closing. Same for sq_pending_mp_add 4080 */ 4081 mutex_enter(&connp->conn_lock); 4082 connp->conn_state_flags |= CONN_CLOSING; 4083 if (connp->conn_oper_pending_ill != NULL) 4084 conn_ioctl_cleanup_reqd = B_TRUE; 4085 CONN_INC_REF_LOCKED(connp); 4086 mutex_exit(&connp->conn_lock); 4087 tcp->tcp_closeflags = (uint8_t)flags; 4088 ASSERT(connp->conn_ref >= 3); 4089 4090 /* 4091 * tcp_closemp_used is used below without any protection of a lock 4092 * as we don't expect any one else to use it concurrently at this 4093 * point otherwise it would be a major defect. 4094 */ 4095 4096 if (mp->b_prev == NULL) 4097 tcp->tcp_closemp_used = B_TRUE; 4098 else 4099 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: " 4100 "connp %p tcp %p\n", (void *)connp, (void *)tcp); 4101 4102 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 4103 4104 (*tcp_squeue_close_proc)(connp->conn_sqp, mp, 4105 tcp_close_output, connp, SQTAG_IP_TCP_CLOSE); 4106 4107 mutex_enter(&tcp->tcp_closelock); 4108 while (!tcp->tcp_closed) { 4109 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 4110 /* 4111 * The cv_wait_sig() was interrupted. We now do the 4112 * following: 4113 * 4114 * 1) If the endpoint was lingering, we allow this 4115 * to be interrupted by cancelling the linger timeout 4116 * and closing normally. 4117 * 4118 * 2) Revert to calling cv_wait() 4119 * 4120 * We revert to using cv_wait() to avoid an 4121 * infinite loop which can occur if the calling 4122 * thread is higher priority than the squeue worker 4123 * thread and is bound to the same cpu. 4124 */ 4125 if (tcp->tcp_linger && tcp->tcp_lingertime > 0) { 4126 mutex_exit(&tcp->tcp_closelock); 4127 /* Entering squeue, bump ref count. */ 4128 CONN_INC_REF(connp); 4129 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 4130 squeue_enter(connp->conn_sqp, bp, 4131 tcp_linger_interrupted, connp, 4132 SQTAG_IP_TCP_CLOSE); 4133 mutex_enter(&tcp->tcp_closelock); 4134 } 4135 break; 4136 } 4137 } 4138 while (!tcp->tcp_closed) 4139 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 4140 mutex_exit(&tcp->tcp_closelock); 4141 4142 /* 4143 * In the case of listener streams that have eagers in the q or q0 4144 * we wait for the eagers to drop their reference to us. tcp_rq and 4145 * tcp_wq of the eagers point to our queues. By waiting for the 4146 * refcnt to drop to 1, we are sure that the eagers have cleaned 4147 * up their queue pointers and also dropped their references to us. 4148 */ 4149 if (tcp->tcp_wait_for_eagers) { 4150 mutex_enter(&connp->conn_lock); 4151 while (connp->conn_ref != 1) { 4152 cv_wait(&connp->conn_cv, &connp->conn_lock); 4153 } 4154 mutex_exit(&connp->conn_lock); 4155 } 4156 /* 4157 * ioctl cleanup. The mp is queued in the 4158 * ill_pending_mp or in the sq_pending_mp. 4159 */ 4160 if (conn_ioctl_cleanup_reqd) 4161 conn_ioctl_cleanup(connp); 4162 4163 qprocsoff(q); 4164 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 4165 4166 tcp->tcp_cpid = -1; 4167 4168 /* 4169 * Drop IP's reference on the conn. This is the last reference 4170 * on the connp if the state was less than established. If the 4171 * connection has gone into timewait state, then we will have 4172 * one ref for the TCP and one more ref (total of two) for the 4173 * classifier connected hash list (a timewait connections stays 4174 * in connected hash till closed). 4175 * 4176 * We can't assert the references because there might be other 4177 * transient reference places because of some walkers or queued 4178 * packets in squeue for the timewait state. 4179 */ 4180 CONN_DEC_REF(connp); 4181 q->q_ptr = WR(q)->q_ptr = NULL; 4182 return (0); 4183 } 4184 4185 static int 4186 tcpclose_accept(queue_t *q) 4187 { 4188 vmem_t *minor_arena; 4189 dev_t conn_dev; 4190 4191 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 4192 4193 /* 4194 * We had opened an acceptor STREAM for sockfs which is 4195 * now being closed due to some error. 4196 */ 4197 qprocsoff(q); 4198 4199 minor_arena = (vmem_t *)WR(q)->q_ptr; 4200 conn_dev = (dev_t)RD(q)->q_ptr; 4201 ASSERT(minor_arena != NULL); 4202 ASSERT(conn_dev != 0); 4203 inet_minor_free(minor_arena, conn_dev); 4204 q->q_ptr = WR(q)->q_ptr = NULL; 4205 return (0); 4206 } 4207 4208 /* 4209 * Called by tcp_close() routine via squeue when lingering is 4210 * interrupted by a signal. 4211 */ 4212 4213 /* ARGSUSED */ 4214 static void 4215 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 4216 { 4217 conn_t *connp = (conn_t *)arg; 4218 tcp_t *tcp = connp->conn_tcp; 4219 4220 freeb(mp); 4221 if (tcp->tcp_linger_tid != 0 && 4222 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 4223 tcp_stop_lingering(tcp); 4224 tcp->tcp_client_errno = EINTR; 4225 } 4226 } 4227 4228 /* 4229 * Called by streams close routine via squeues when our client blows off her 4230 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 4231 * connection politely" When SO_LINGER is set (with a non-zero linger time and 4232 * it is not a nonblocking socket) then this routine sleeps until the FIN is 4233 * acked. 4234 * 4235 * NOTE: tcp_close potentially returns error when lingering. 4236 * However, the stream head currently does not pass these errors 4237 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 4238 * errors to the application (from tsleep()) and not errors 4239 * like ECONNRESET caused by receiving a reset packet. 4240 */ 4241 4242 /* ARGSUSED */ 4243 static void 4244 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 4245 { 4246 char *msg; 4247 conn_t *connp = (conn_t *)arg; 4248 tcp_t *tcp = connp->conn_tcp; 4249 clock_t delta = 0; 4250 tcp_stack_t *tcps = tcp->tcp_tcps; 4251 4252 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 4253 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 4254 4255 /* Cancel any pending timeout */ 4256 if (tcp->tcp_ordrelid != 0) { 4257 if (tcp->tcp_timeout) { 4258 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ordrelid); 4259 } 4260 tcp->tcp_ordrelid = 0; 4261 tcp->tcp_timeout = B_FALSE; 4262 } 4263 4264 mutex_enter(&tcp->tcp_eager_lock); 4265 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 4266 /* Cleanup for listener */ 4267 tcp_eager_cleanup(tcp, 0); 4268 tcp->tcp_wait_for_eagers = 1; 4269 } 4270 mutex_exit(&tcp->tcp_eager_lock); 4271 4272 connp->conn_mdt_ok = B_FALSE; 4273 tcp->tcp_mdt = B_FALSE; 4274 4275 connp->conn_lso_ok = B_FALSE; 4276 tcp->tcp_lso = B_FALSE; 4277 4278 msg = NULL; 4279 switch (tcp->tcp_state) { 4280 case TCPS_CLOSED: 4281 case TCPS_IDLE: 4282 case TCPS_BOUND: 4283 case TCPS_LISTEN: 4284 break; 4285 case TCPS_SYN_SENT: 4286 msg = "tcp_close, during connect"; 4287 break; 4288 case TCPS_SYN_RCVD: 4289 /* 4290 * Close during the connect 3-way handshake 4291 * but here there may or may not be pending data 4292 * already on queue. Process almost same as in 4293 * the ESTABLISHED state. 4294 */ 4295 /* FALLTHRU */ 4296 default: 4297 if (tcp->tcp_sodirect != NULL) { 4298 /* Ok, no more sodirect */ 4299 tcp->tcp_sodirect = NULL; 4300 } 4301 4302 if (tcp->tcp_fused) 4303 tcp_unfuse(tcp); 4304 4305 /* 4306 * If SO_LINGER has set a zero linger time, abort the 4307 * connection with a reset. 4308 */ 4309 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 4310 msg = "tcp_close, zero lingertime"; 4311 break; 4312 } 4313 4314 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 4315 /* 4316 * Abort connection if there is unread data queued. 4317 */ 4318 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 4319 msg = "tcp_close, unread data"; 4320 break; 4321 } 4322 /* 4323 * tcp_hard_bound is now cleared thus all packets go through 4324 * tcp_lookup. This fact is used by tcp_detach below. 4325 * 4326 * We have done a qwait() above which could have possibly 4327 * drained more messages in turn causing transition to a 4328 * different state. Check whether we have to do the rest 4329 * of the processing or not. 4330 */ 4331 if (tcp->tcp_state <= TCPS_LISTEN) 4332 break; 4333 4334 /* 4335 * Transmit the FIN before detaching the tcp_t. 4336 * After tcp_detach returns this queue/perimeter 4337 * no longer owns the tcp_t thus others can modify it. 4338 */ 4339 (void) tcp_xmit_end(tcp); 4340 4341 /* 4342 * If lingering on close then wait until the fin is acked, 4343 * the SO_LINGER time passes, or a reset is sent/received. 4344 */ 4345 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4346 !(tcp->tcp_fin_acked) && 4347 tcp->tcp_state >= TCPS_ESTABLISHED) { 4348 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4349 tcp->tcp_client_errno = EWOULDBLOCK; 4350 } else if (tcp->tcp_client_errno == 0) { 4351 4352 ASSERT(tcp->tcp_linger_tid == 0); 4353 4354 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4355 tcp_close_linger_timeout, 4356 tcp->tcp_lingertime * hz); 4357 4358 /* tcp_close_linger_timeout will finish close */ 4359 if (tcp->tcp_linger_tid == 0) 4360 tcp->tcp_client_errno = ENOSR; 4361 else 4362 return; 4363 } 4364 4365 /* 4366 * Check if we need to detach or just close 4367 * the instance. 4368 */ 4369 if (tcp->tcp_state <= TCPS_LISTEN) 4370 break; 4371 } 4372 4373 /* 4374 * Make sure that no other thread will access the tcp_rq of 4375 * this instance (through lookups etc.) as tcp_rq will go 4376 * away shortly. 4377 */ 4378 tcp_acceptor_hash_remove(tcp); 4379 4380 mutex_enter(&tcp->tcp_non_sq_lock); 4381 if (tcp->tcp_flow_stopped) { 4382 tcp_clrqfull(tcp); 4383 } 4384 mutex_exit(&tcp->tcp_non_sq_lock); 4385 4386 if (tcp->tcp_timer_tid != 0) { 4387 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4388 tcp->tcp_timer_tid = 0; 4389 } 4390 /* 4391 * Need to cancel those timers which will not be used when 4392 * TCP is detached. This has to be done before the tcp_wq 4393 * is set to the global queue. 4394 */ 4395 tcp_timers_stop(tcp); 4396 4397 tcp->tcp_detached = B_TRUE; 4398 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4399 tcp_time_wait_append(tcp); 4400 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4401 ASSERT(connp->conn_ref >= 3); 4402 goto finish; 4403 } 4404 4405 /* 4406 * If delta is zero the timer event wasn't executed and was 4407 * successfully canceled. In this case we need to restart it 4408 * with the minimal delta possible. 4409 */ 4410 if (delta >= 0) 4411 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4412 delta ? delta : 1); 4413 4414 ASSERT(connp->conn_ref >= 3); 4415 goto finish; 4416 } 4417 4418 /* Detach did not complete. Still need to remove q from stream. */ 4419 if (msg) { 4420 if (tcp->tcp_state == TCPS_ESTABLISHED || 4421 tcp->tcp_state == TCPS_CLOSE_WAIT) 4422 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4423 if (tcp->tcp_state == TCPS_SYN_SENT || 4424 tcp->tcp_state == TCPS_SYN_RCVD) 4425 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4426 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4427 } 4428 4429 tcp_closei_local(tcp); 4430 CONN_DEC_REF(connp); 4431 ASSERT(connp->conn_ref >= 2); 4432 4433 finish: 4434 /* 4435 * Although packets are always processed on the correct 4436 * tcp's perimeter and access is serialized via squeue's, 4437 * IP still needs a queue when sending packets in time_wait 4438 * state so use WR(tcps_g_q) till ip_output() can be 4439 * changed to deal with just connp. For read side, we 4440 * could have set tcp_rq to NULL but there are some cases 4441 * in tcp_rput_data() from early days of this code which 4442 * do a putnext without checking if tcp is closed. Those 4443 * need to be identified before both tcp_rq and tcp_wq 4444 * can be set to NULL and tcps_g_q can disappear forever. 4445 */ 4446 mutex_enter(&tcp->tcp_closelock); 4447 /* 4448 * Don't change the queues in the case of a listener that has 4449 * eagers in its q or q0. It could surprise the eagers. 4450 * Instead wait for the eagers outside the squeue. 4451 */ 4452 if (!tcp->tcp_wait_for_eagers) { 4453 tcp->tcp_detached = B_TRUE; 4454 /* 4455 * When default queue is closing we set tcps_g_q to NULL 4456 * after the close is done. 4457 */ 4458 ASSERT(tcps->tcps_g_q != NULL); 4459 tcp->tcp_rq = tcps->tcps_g_q; 4460 tcp->tcp_wq = WR(tcps->tcps_g_q); 4461 } 4462 4463 /* Signal tcp_close() to finish closing. */ 4464 tcp->tcp_closed = 1; 4465 cv_signal(&tcp->tcp_closecv); 4466 mutex_exit(&tcp->tcp_closelock); 4467 } 4468 4469 4470 /* 4471 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4472 * Some stream heads get upset if they see these later on as anything but NULL. 4473 */ 4474 static void 4475 tcp_close_mpp(mblk_t **mpp) 4476 { 4477 mblk_t *mp; 4478 4479 if ((mp = *mpp) != NULL) { 4480 do { 4481 mp->b_next = NULL; 4482 mp->b_prev = NULL; 4483 } while ((mp = mp->b_cont) != NULL); 4484 4485 mp = *mpp; 4486 *mpp = NULL; 4487 freemsg(mp); 4488 } 4489 } 4490 4491 /* Do detached close. */ 4492 static void 4493 tcp_close_detached(tcp_t *tcp) 4494 { 4495 if (tcp->tcp_fused) 4496 tcp_unfuse(tcp); 4497 4498 /* 4499 * Clustering code serializes TCP disconnect callbacks and 4500 * cluster tcp list walks by blocking a TCP disconnect callback 4501 * if a cluster tcp list walk is in progress. This ensures 4502 * accurate accounting of TCPs in the cluster code even though 4503 * the TCP list walk itself is not atomic. 4504 */ 4505 tcp_closei_local(tcp); 4506 CONN_DEC_REF(tcp->tcp_connp); 4507 } 4508 4509 /* 4510 * Stop all TCP timers, and free the timer mblks if requested. 4511 */ 4512 void 4513 tcp_timers_stop(tcp_t *tcp) 4514 { 4515 if (tcp->tcp_timer_tid != 0) { 4516 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4517 tcp->tcp_timer_tid = 0; 4518 } 4519 if (tcp->tcp_ka_tid != 0) { 4520 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4521 tcp->tcp_ka_tid = 0; 4522 } 4523 if (tcp->tcp_ack_tid != 0) { 4524 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4525 tcp->tcp_ack_tid = 0; 4526 } 4527 if (tcp->tcp_push_tid != 0) { 4528 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4529 tcp->tcp_push_tid = 0; 4530 } 4531 } 4532 4533 /* 4534 * The tcp_t is going away. Remove it from all lists and set it 4535 * to TCPS_CLOSED. The freeing up of memory is deferred until 4536 * tcp_inactive. This is needed since a thread in tcp_rput might have 4537 * done a CONN_INC_REF on this structure before it was removed from the 4538 * hashes. 4539 */ 4540 static void 4541 tcp_closei_local(tcp_t *tcp) 4542 { 4543 ire_t *ire; 4544 conn_t *connp = tcp->tcp_connp; 4545 tcp_stack_t *tcps = tcp->tcp_tcps; 4546 4547 if (!TCP_IS_SOCKET(tcp)) 4548 tcp_acceptor_hash_remove(tcp); 4549 4550 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4551 tcp->tcp_ibsegs = 0; 4552 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4553 tcp->tcp_obsegs = 0; 4554 4555 /* 4556 * If we are an eager connection hanging off a listener that 4557 * hasn't formally accepted the connection yet, get off his 4558 * list and blow off any data that we have accumulated. 4559 */ 4560 if (tcp->tcp_listener != NULL) { 4561 tcp_t *listener = tcp->tcp_listener; 4562 mutex_enter(&listener->tcp_eager_lock); 4563 /* 4564 * tcp_tconnind_started == B_TRUE means that the 4565 * conn_ind has already gone to listener. At 4566 * this point, eager will be closed but we 4567 * leave it in listeners eager list so that 4568 * if listener decides to close without doing 4569 * accept, we can clean this up. In tcp_wput_accept 4570 * we take care of the case of accept on closed 4571 * eager. 4572 */ 4573 if (!tcp->tcp_tconnind_started) { 4574 tcp_eager_unlink(tcp); 4575 mutex_exit(&listener->tcp_eager_lock); 4576 /* 4577 * We don't want to have any pointers to the 4578 * listener queue, after we have released our 4579 * reference on the listener 4580 */ 4581 ASSERT(tcps->tcps_g_q != NULL); 4582 tcp->tcp_rq = tcps->tcps_g_q; 4583 tcp->tcp_wq = WR(tcps->tcps_g_q); 4584 CONN_DEC_REF(listener->tcp_connp); 4585 } else { 4586 mutex_exit(&listener->tcp_eager_lock); 4587 } 4588 } 4589 4590 /* Stop all the timers */ 4591 tcp_timers_stop(tcp); 4592 4593 if (tcp->tcp_state == TCPS_LISTEN) { 4594 if (tcp->tcp_ip_addr_cache) { 4595 kmem_free((void *)tcp->tcp_ip_addr_cache, 4596 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4597 tcp->tcp_ip_addr_cache = NULL; 4598 } 4599 } 4600 mutex_enter(&tcp->tcp_non_sq_lock); 4601 if (tcp->tcp_flow_stopped) 4602 tcp_clrqfull(tcp); 4603 mutex_exit(&tcp->tcp_non_sq_lock); 4604 4605 tcp_bind_hash_remove(tcp); 4606 /* 4607 * If the tcp_time_wait_collector (which runs outside the squeue) 4608 * is trying to remove this tcp from the time wait list, we will 4609 * block in tcp_time_wait_remove while trying to acquire the 4610 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4611 * requires the ipcl_hash_remove to be ordered after the 4612 * tcp_time_wait_remove for the refcnt checks to work correctly. 4613 */ 4614 if (tcp->tcp_state == TCPS_TIME_WAIT) 4615 (void) tcp_time_wait_remove(tcp, NULL); 4616 CL_INET_DISCONNECT(tcp); 4617 ipcl_hash_remove(connp); 4618 4619 /* 4620 * Delete the cached ire in conn_ire_cache and also mark 4621 * the conn as CONDEMNED 4622 */ 4623 mutex_enter(&connp->conn_lock); 4624 connp->conn_state_flags |= CONN_CONDEMNED; 4625 ire = connp->conn_ire_cache; 4626 connp->conn_ire_cache = NULL; 4627 mutex_exit(&connp->conn_lock); 4628 if (ire != NULL) 4629 IRE_REFRELE_NOTR(ire); 4630 4631 /* Need to cleanup any pending ioctls */ 4632 ASSERT(tcp->tcp_time_wait_next == NULL); 4633 ASSERT(tcp->tcp_time_wait_prev == NULL); 4634 ASSERT(tcp->tcp_time_wait_expire == 0); 4635 tcp->tcp_state = TCPS_CLOSED; 4636 4637 /* Release any SSL context */ 4638 if (tcp->tcp_kssl_ent != NULL) { 4639 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4640 tcp->tcp_kssl_ent = NULL; 4641 } 4642 if (tcp->tcp_kssl_ctx != NULL) { 4643 kssl_release_ctx(tcp->tcp_kssl_ctx); 4644 tcp->tcp_kssl_ctx = NULL; 4645 } 4646 tcp->tcp_kssl_pending = B_FALSE; 4647 4648 tcp_ipsec_cleanup(tcp); 4649 } 4650 4651 /* 4652 * tcp is dying (called from ipcl_conn_destroy and error cases). 4653 * Free the tcp_t in either case. 4654 */ 4655 void 4656 tcp_free(tcp_t *tcp) 4657 { 4658 mblk_t *mp; 4659 ip6_pkt_t *ipp; 4660 4661 ASSERT(tcp != NULL); 4662 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4663 4664 tcp->tcp_rq = NULL; 4665 tcp->tcp_wq = NULL; 4666 4667 tcp_close_mpp(&tcp->tcp_xmit_head); 4668 tcp_close_mpp(&tcp->tcp_reass_head); 4669 if (tcp->tcp_rcv_list != NULL) { 4670 /* Free b_next chain */ 4671 tcp_close_mpp(&tcp->tcp_rcv_list); 4672 } 4673 if ((mp = tcp->tcp_urp_mp) != NULL) { 4674 freemsg(mp); 4675 } 4676 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4677 freemsg(mp); 4678 } 4679 4680 if (tcp->tcp_fused_sigurg_mp != NULL) { 4681 freeb(tcp->tcp_fused_sigurg_mp); 4682 tcp->tcp_fused_sigurg_mp = NULL; 4683 } 4684 4685 if (tcp->tcp_sack_info != NULL) { 4686 if (tcp->tcp_notsack_list != NULL) { 4687 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4688 } 4689 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4690 } 4691 4692 if (tcp->tcp_hopopts != NULL) { 4693 mi_free(tcp->tcp_hopopts); 4694 tcp->tcp_hopopts = NULL; 4695 tcp->tcp_hopoptslen = 0; 4696 } 4697 ASSERT(tcp->tcp_hopoptslen == 0); 4698 if (tcp->tcp_dstopts != NULL) { 4699 mi_free(tcp->tcp_dstopts); 4700 tcp->tcp_dstopts = NULL; 4701 tcp->tcp_dstoptslen = 0; 4702 } 4703 ASSERT(tcp->tcp_dstoptslen == 0); 4704 if (tcp->tcp_rtdstopts != NULL) { 4705 mi_free(tcp->tcp_rtdstopts); 4706 tcp->tcp_rtdstopts = NULL; 4707 tcp->tcp_rtdstoptslen = 0; 4708 } 4709 ASSERT(tcp->tcp_rtdstoptslen == 0); 4710 if (tcp->tcp_rthdr != NULL) { 4711 mi_free(tcp->tcp_rthdr); 4712 tcp->tcp_rthdr = NULL; 4713 tcp->tcp_rthdrlen = 0; 4714 } 4715 ASSERT(tcp->tcp_rthdrlen == 0); 4716 4717 ipp = &tcp->tcp_sticky_ipp; 4718 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4719 IPPF_RTHDR)) 4720 ip6_pkt_free(ipp); 4721 4722 /* 4723 * Free memory associated with the tcp/ip header template. 4724 */ 4725 4726 if (tcp->tcp_iphc != NULL) 4727 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4728 4729 /* 4730 * Following is really a blowing away a union. 4731 * It happens to have exactly two members of identical size 4732 * the following code is enough. 4733 */ 4734 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4735 4736 if (tcp->tcp_tracebuf != NULL) { 4737 kmem_free(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 4738 tcp->tcp_tracebuf = NULL; 4739 } 4740 } 4741 4742 4743 /* 4744 * Put a connection confirmation message upstream built from the 4745 * address information within 'iph' and 'tcph'. Report our success or failure. 4746 */ 4747 static boolean_t 4748 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4749 mblk_t **defermp) 4750 { 4751 sin_t sin; 4752 sin6_t sin6; 4753 mblk_t *mp; 4754 char *optp = NULL; 4755 int optlen = 0; 4756 cred_t *cr; 4757 4758 if (defermp != NULL) 4759 *defermp = NULL; 4760 4761 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4762 /* 4763 * Return in T_CONN_CON results of option negotiation through 4764 * the T_CONN_REQ. Note: If there is an real end-to-end option 4765 * negotiation, then what is received from remote end needs 4766 * to be taken into account but there is no such thing (yet?) 4767 * in our TCP/IP. 4768 * Note: We do not use mi_offset_param() here as 4769 * tcp_opts_conn_req contents do not directly come from 4770 * an application and are either generated in kernel or 4771 * from user input that was already verified. 4772 */ 4773 mp = tcp->tcp_conn.tcp_opts_conn_req; 4774 optp = (char *)(mp->b_rptr + 4775 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4776 optlen = (int) 4777 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4778 } 4779 4780 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4781 ipha_t *ipha = (ipha_t *)iphdr; 4782 4783 /* packet is IPv4 */ 4784 if (tcp->tcp_family == AF_INET) { 4785 sin = sin_null; 4786 sin.sin_addr.s_addr = ipha->ipha_src; 4787 sin.sin_port = *(uint16_t *)tcph->th_lport; 4788 sin.sin_family = AF_INET; 4789 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4790 (int)sizeof (sin_t), optp, optlen); 4791 } else { 4792 sin6 = sin6_null; 4793 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4794 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4795 sin6.sin6_family = AF_INET6; 4796 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4797 (int)sizeof (sin6_t), optp, optlen); 4798 4799 } 4800 } else { 4801 ip6_t *ip6h = (ip6_t *)iphdr; 4802 4803 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4804 ASSERT(tcp->tcp_family == AF_INET6); 4805 sin6 = sin6_null; 4806 sin6.sin6_addr = ip6h->ip6_src; 4807 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4808 sin6.sin6_family = AF_INET6; 4809 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4810 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4811 (int)sizeof (sin6_t), optp, optlen); 4812 } 4813 4814 if (!mp) 4815 return (B_FALSE); 4816 4817 if ((cr = DB_CRED(idmp)) != NULL) { 4818 mblk_setcred(mp, cr); 4819 DB_CPID(mp) = DB_CPID(idmp); 4820 } 4821 4822 if (defermp == NULL) 4823 putnext(tcp->tcp_rq, mp); 4824 else 4825 *defermp = mp; 4826 4827 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4828 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4829 return (B_TRUE); 4830 } 4831 4832 /* 4833 * Defense for the SYN attack - 4834 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4835 * one from the list of droppable eagers. This list is a subset of q0. 4836 * see comments before the definition of MAKE_DROPPABLE(). 4837 * 2. Don't drop a SYN request before its first timeout. This gives every 4838 * request at least til the first timeout to complete its 3-way handshake. 4839 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4840 * requests currently on the queue that has timed out. This will be used 4841 * as an indicator of whether an attack is under way, so that appropriate 4842 * actions can be taken. (It's incremented in tcp_timer() and decremented 4843 * either when eager goes into ESTABLISHED, or gets freed up.) 4844 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4845 * # of timeout drops back to <= q0len/32 => SYN alert off 4846 */ 4847 static boolean_t 4848 tcp_drop_q0(tcp_t *tcp) 4849 { 4850 tcp_t *eager; 4851 mblk_t *mp; 4852 tcp_stack_t *tcps = tcp->tcp_tcps; 4853 4854 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4855 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4856 4857 /* Pick oldest eager from the list of droppable eagers */ 4858 eager = tcp->tcp_eager_prev_drop_q0; 4859 4860 /* If list is empty. return B_FALSE */ 4861 if (eager == tcp) { 4862 return (B_FALSE); 4863 } 4864 4865 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4866 if ((mp = allocb(0, BPRI_HI)) == NULL) 4867 return (B_FALSE); 4868 4869 /* 4870 * Take this eager out from the list of droppable eagers since we are 4871 * going to drop it. 4872 */ 4873 MAKE_UNDROPPABLE(eager); 4874 4875 if (tcp->tcp_debug) { 4876 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4877 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4878 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4879 tcp->tcp_conn_req_cnt_q0, 4880 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4881 } 4882 4883 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4884 4885 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4886 CONN_INC_REF(eager->tcp_connp); 4887 4888 /* Mark the IRE created for this SYN request temporary */ 4889 tcp_ip_ire_mark_advice(eager); 4890 squeue_fill(eager->tcp_connp->conn_sqp, mp, 4891 tcp_clean_death_wrapper, eager->tcp_connp, SQTAG_TCP_DROP_Q0); 4892 4893 return (B_TRUE); 4894 } 4895 4896 int 4897 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4898 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4899 { 4900 tcp_t *ltcp = lconnp->conn_tcp; 4901 tcp_t *tcp = connp->conn_tcp; 4902 mblk_t *tpi_mp; 4903 ipha_t *ipha; 4904 ip6_t *ip6h; 4905 sin6_t sin6; 4906 in6_addr_t v6dst; 4907 int err; 4908 int ifindex = 0; 4909 cred_t *cr; 4910 tcp_stack_t *tcps = tcp->tcp_tcps; 4911 4912 if (ipvers == IPV4_VERSION) { 4913 ipha = (ipha_t *)mp->b_rptr; 4914 4915 connp->conn_send = ip_output; 4916 connp->conn_recv = tcp_input; 4917 4918 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4919 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4920 4921 sin6 = sin6_null; 4922 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4923 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4924 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4925 sin6.sin6_family = AF_INET6; 4926 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4927 lconnp->conn_zoneid, tcps->tcps_netstack); 4928 if (tcp->tcp_recvdstaddr) { 4929 sin6_t sin6d; 4930 4931 sin6d = sin6_null; 4932 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4933 &sin6d.sin6_addr); 4934 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4935 sin6d.sin6_family = AF_INET; 4936 tpi_mp = mi_tpi_extconn_ind(NULL, 4937 (char *)&sin6d, sizeof (sin6_t), 4938 (char *)&tcp, 4939 (t_scalar_t)sizeof (intptr_t), 4940 (char *)&sin6d, sizeof (sin6_t), 4941 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4942 } else { 4943 tpi_mp = mi_tpi_conn_ind(NULL, 4944 (char *)&sin6, sizeof (sin6_t), 4945 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4946 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4947 } 4948 } else { 4949 ip6h = (ip6_t *)mp->b_rptr; 4950 4951 connp->conn_send = ip_output_v6; 4952 connp->conn_recv = tcp_input; 4953 4954 connp->conn_srcv6 = ip6h->ip6_dst; 4955 connp->conn_remv6 = ip6h->ip6_src; 4956 4957 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4958 ifindex = (int)DB_CKSUMSTUFF(mp); 4959 DB_CKSUMSTUFF(mp) = 0; 4960 4961 sin6 = sin6_null; 4962 sin6.sin6_addr = ip6h->ip6_src; 4963 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4964 sin6.sin6_family = AF_INET6; 4965 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4966 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4967 lconnp->conn_zoneid, tcps->tcps_netstack); 4968 4969 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4970 /* Pass up the scope_id of remote addr */ 4971 sin6.sin6_scope_id = ifindex; 4972 } else { 4973 sin6.sin6_scope_id = 0; 4974 } 4975 if (tcp->tcp_recvdstaddr) { 4976 sin6_t sin6d; 4977 4978 sin6d = sin6_null; 4979 sin6.sin6_addr = ip6h->ip6_dst; 4980 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4981 sin6d.sin6_family = AF_INET; 4982 tpi_mp = mi_tpi_extconn_ind(NULL, 4983 (char *)&sin6d, sizeof (sin6_t), 4984 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4985 (char *)&sin6d, sizeof (sin6_t), 4986 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4987 } else { 4988 tpi_mp = mi_tpi_conn_ind(NULL, 4989 (char *)&sin6, sizeof (sin6_t), 4990 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4991 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4992 } 4993 } 4994 4995 if (tpi_mp == NULL) 4996 return (ENOMEM); 4997 4998 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4999 connp->conn_lport = *(uint16_t *)tcph->th_fport; 5000 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 5001 connp->conn_fully_bound = B_FALSE; 5002 5003 if (tcps->tcps_trace) 5004 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 5005 5006 /* Inherit information from the "parent" */ 5007 tcp->tcp_ipversion = ltcp->tcp_ipversion; 5008 tcp->tcp_family = ltcp->tcp_family; 5009 tcp->tcp_wq = ltcp->tcp_wq; 5010 tcp->tcp_rq = ltcp->tcp_rq; 5011 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 5012 tcp->tcp_detached = B_TRUE; 5013 if ((err = tcp_init_values(tcp)) != 0) { 5014 freemsg(tpi_mp); 5015 return (err); 5016 } 5017 5018 if (ipvers == IPV4_VERSION) { 5019 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 5020 freemsg(tpi_mp); 5021 return (err); 5022 } 5023 ASSERT(tcp->tcp_ipha != NULL); 5024 } else { 5025 /* ifindex must be already set */ 5026 ASSERT(ifindex != 0); 5027 5028 if (ltcp->tcp_bound_if != 0) { 5029 /* 5030 * Set newtcp's bound_if equal to 5031 * listener's value. If ifindex is 5032 * not the same as ltcp->tcp_bound_if, 5033 * it must be a packet for the ipmp group 5034 * of interfaces 5035 */ 5036 tcp->tcp_bound_if = ltcp->tcp_bound_if; 5037 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 5038 tcp->tcp_bound_if = ifindex; 5039 } 5040 5041 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 5042 tcp->tcp_recvifindex = 0; 5043 tcp->tcp_recvhops = 0xffffffffU; 5044 ASSERT(tcp->tcp_ip6h != NULL); 5045 } 5046 5047 tcp->tcp_lport = ltcp->tcp_lport; 5048 5049 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 5050 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 5051 /* 5052 * Listener had options of some sort; eager inherits. 5053 * Free up the eager template and allocate one 5054 * of the right size. 5055 */ 5056 if (tcp->tcp_hdr_grown) { 5057 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 5058 } else { 5059 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 5060 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 5061 } 5062 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 5063 KM_NOSLEEP); 5064 if (tcp->tcp_iphc == NULL) { 5065 tcp->tcp_iphc_len = 0; 5066 freemsg(tpi_mp); 5067 return (ENOMEM); 5068 } 5069 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 5070 tcp->tcp_hdr_grown = B_TRUE; 5071 } 5072 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5073 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5074 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5075 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 5076 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 5077 5078 /* 5079 * Copy the IP+TCP header template from listener to eager 5080 */ 5081 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5082 if (tcp->tcp_ipversion == IPV6_VERSION) { 5083 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 5084 IPPROTO_RAW) { 5085 tcp->tcp_ip6h = 5086 (ip6_t *)(tcp->tcp_iphc + 5087 sizeof (ip6i_t)); 5088 } else { 5089 tcp->tcp_ip6h = 5090 (ip6_t *)(tcp->tcp_iphc); 5091 } 5092 tcp->tcp_ipha = NULL; 5093 } else { 5094 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5095 tcp->tcp_ip6h = NULL; 5096 } 5097 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5098 tcp->tcp_ip_hdr_len); 5099 } else { 5100 /* 5101 * only valid case when ipversion of listener and 5102 * eager differ is when listener is IPv6 and 5103 * eager is IPv4. 5104 * Eager header template has been initialized to the 5105 * maximum v4 header sizes, which includes space for 5106 * TCP and IP options. 5107 */ 5108 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 5109 (tcp->tcp_ipversion == IPV4_VERSION)); 5110 ASSERT(tcp->tcp_iphc_len >= 5111 TCP_MAX_COMBINED_HEADER_LENGTH); 5112 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5113 /* copy IP header fields individually */ 5114 tcp->tcp_ipha->ipha_ttl = 5115 ltcp->tcp_ip6h->ip6_hops; 5116 bcopy(ltcp->tcp_tcph->th_lport, 5117 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 5118 } 5119 5120 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5121 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 5122 sizeof (in_port_t)); 5123 5124 if (ltcp->tcp_lport == 0) { 5125 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 5126 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 5127 sizeof (in_port_t)); 5128 } 5129 5130 if (tcp->tcp_ipversion == IPV4_VERSION) { 5131 ASSERT(ipha != NULL); 5132 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5133 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5134 5135 /* Source routing option copyover (reverse it) */ 5136 if (tcps->tcps_rev_src_routes) 5137 tcp_opt_reverse(tcp, ipha); 5138 } else { 5139 ASSERT(ip6h != NULL); 5140 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 5141 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 5142 } 5143 5144 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5145 ASSERT(!tcp->tcp_tconnind_started); 5146 /* 5147 * If the SYN contains a credential, it's a loopback packet; attach 5148 * the credential to the TPI message. 5149 */ 5150 if ((cr = DB_CRED(idmp)) != NULL) { 5151 mblk_setcred(tpi_mp, cr); 5152 DB_CPID(tpi_mp) = DB_CPID(idmp); 5153 } 5154 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5155 5156 /* Inherit the listener's SSL protection state */ 5157 5158 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5159 kssl_hold_ent(tcp->tcp_kssl_ent); 5160 tcp->tcp_kssl_pending = B_TRUE; 5161 } 5162 5163 return (0); 5164 } 5165 5166 5167 int 5168 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 5169 tcph_t *tcph, mblk_t *idmp) 5170 { 5171 tcp_t *ltcp = lconnp->conn_tcp; 5172 tcp_t *tcp = connp->conn_tcp; 5173 sin_t sin; 5174 mblk_t *tpi_mp = NULL; 5175 int err; 5176 cred_t *cr; 5177 tcp_stack_t *tcps = tcp->tcp_tcps; 5178 5179 sin = sin_null; 5180 sin.sin_addr.s_addr = ipha->ipha_src; 5181 sin.sin_port = *(uint16_t *)tcph->th_lport; 5182 sin.sin_family = AF_INET; 5183 if (ltcp->tcp_recvdstaddr) { 5184 sin_t sind; 5185 5186 sind = sin_null; 5187 sind.sin_addr.s_addr = ipha->ipha_dst; 5188 sind.sin_port = *(uint16_t *)tcph->th_fport; 5189 sind.sin_family = AF_INET; 5190 tpi_mp = mi_tpi_extconn_ind(NULL, 5191 (char *)&sind, sizeof (sin_t), (char *)&tcp, 5192 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 5193 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5194 } else { 5195 tpi_mp = mi_tpi_conn_ind(NULL, 5196 (char *)&sin, sizeof (sin_t), 5197 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 5198 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5199 } 5200 5201 if (tpi_mp == NULL) { 5202 return (ENOMEM); 5203 } 5204 5205 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 5206 connp->conn_send = ip_output; 5207 connp->conn_recv = tcp_input; 5208 connp->conn_fully_bound = B_FALSE; 5209 5210 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 5211 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 5212 connp->conn_fport = *(uint16_t *)tcph->th_lport; 5213 connp->conn_lport = *(uint16_t *)tcph->th_fport; 5214 5215 if (tcps->tcps_trace) { 5216 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 5217 } 5218 5219 /* Inherit information from the "parent" */ 5220 tcp->tcp_ipversion = ltcp->tcp_ipversion; 5221 tcp->tcp_family = ltcp->tcp_family; 5222 tcp->tcp_wq = ltcp->tcp_wq; 5223 tcp->tcp_rq = ltcp->tcp_rq; 5224 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 5225 tcp->tcp_detached = B_TRUE; 5226 if ((err = tcp_init_values(tcp)) != 0) { 5227 freemsg(tpi_mp); 5228 return (err); 5229 } 5230 5231 /* 5232 * Let's make sure that eager tcp template has enough space to 5233 * copy IPv4 listener's tcp template. Since the conn_t structure is 5234 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 5235 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 5236 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 5237 * extension headers or with ip6i_t struct). Note that bcopy() below 5238 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 5239 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 5240 */ 5241 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 5242 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 5243 5244 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5245 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5246 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5247 tcp->tcp_ttl = ltcp->tcp_ttl; 5248 tcp->tcp_tos = ltcp->tcp_tos; 5249 5250 /* Copy the IP+TCP header template from listener to eager */ 5251 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5252 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5253 tcp->tcp_ip6h = NULL; 5254 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5255 tcp->tcp_ip_hdr_len); 5256 5257 /* Initialize the IP addresses and Ports */ 5258 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5259 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5260 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5261 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 5262 5263 /* Source routing option copyover (reverse it) */ 5264 if (tcps->tcps_rev_src_routes) 5265 tcp_opt_reverse(tcp, ipha); 5266 5267 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5268 ASSERT(!tcp->tcp_tconnind_started); 5269 5270 /* 5271 * If the SYN contains a credential, it's a loopback packet; attach 5272 * the credential to the TPI message. 5273 */ 5274 if ((cr = DB_CRED(idmp)) != NULL) { 5275 mblk_setcred(tpi_mp, cr); 5276 DB_CPID(tpi_mp) = DB_CPID(idmp); 5277 } 5278 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5279 5280 /* Inherit the listener's SSL protection state */ 5281 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5282 kssl_hold_ent(tcp->tcp_kssl_ent); 5283 tcp->tcp_kssl_pending = B_TRUE; 5284 } 5285 5286 return (0); 5287 } 5288 5289 /* 5290 * sets up conn for ipsec. 5291 * if the first mblk is M_CTL it is consumed and mpp is updated. 5292 * in case of error mpp is freed. 5293 */ 5294 conn_t * 5295 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 5296 { 5297 conn_t *connp = tcp->tcp_connp; 5298 conn_t *econnp; 5299 squeue_t *new_sqp; 5300 mblk_t *first_mp = *mpp; 5301 mblk_t *mp = *mpp; 5302 boolean_t mctl_present = B_FALSE; 5303 uint_t ipvers; 5304 5305 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 5306 if (econnp == NULL) { 5307 freemsg(first_mp); 5308 return (NULL); 5309 } 5310 if (DB_TYPE(mp) == M_CTL) { 5311 if (mp->b_cont == NULL || 5312 mp->b_cont->b_datap->db_type != M_DATA) { 5313 freemsg(first_mp); 5314 return (NULL); 5315 } 5316 mp = mp->b_cont; 5317 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 5318 freemsg(first_mp); 5319 return (NULL); 5320 } 5321 5322 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5323 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5324 mctl_present = B_TRUE; 5325 } else { 5326 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 5327 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5328 } 5329 5330 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5331 DB_CKSUMSTART(mp) = 0; 5332 5333 ASSERT(OK_32PTR(mp->b_rptr)); 5334 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5335 if (ipvers == IPV4_VERSION) { 5336 uint16_t *up; 5337 uint32_t ports; 5338 ipha_t *ipha; 5339 5340 ipha = (ipha_t *)mp->b_rptr; 5341 up = (uint16_t *)((uchar_t *)ipha + 5342 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5343 ports = *(uint32_t *)up; 5344 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5345 ipha->ipha_dst, ipha->ipha_src, ports); 5346 } else { 5347 uint16_t *up; 5348 uint32_t ports; 5349 uint16_t ip_hdr_len; 5350 uint8_t *nexthdrp; 5351 ip6_t *ip6h; 5352 tcph_t *tcph; 5353 5354 ip6h = (ip6_t *)mp->b_rptr; 5355 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5356 ip_hdr_len = IPV6_HDR_LEN; 5357 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5358 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5359 CONN_DEC_REF(econnp); 5360 freemsg(first_mp); 5361 return (NULL); 5362 } 5363 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5364 up = (uint16_t *)tcph->th_lport; 5365 ports = *(uint32_t *)up; 5366 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5367 ip6h->ip6_dst, ip6h->ip6_src, ports); 5368 } 5369 5370 /* 5371 * The caller already ensured that there is a sqp present. 5372 */ 5373 econnp->conn_sqp = new_sqp; 5374 5375 if (connp->conn_policy != NULL) { 5376 ipsec_in_t *ii; 5377 ii = (ipsec_in_t *)(first_mp->b_rptr); 5378 ASSERT(ii->ipsec_in_policy == NULL); 5379 IPPH_REFHOLD(connp->conn_policy); 5380 ii->ipsec_in_policy = connp->conn_policy; 5381 5382 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5383 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5384 CONN_DEC_REF(econnp); 5385 freemsg(first_mp); 5386 return (NULL); 5387 } 5388 } 5389 5390 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5391 CONN_DEC_REF(econnp); 5392 freemsg(first_mp); 5393 return (NULL); 5394 } 5395 5396 /* 5397 * If we know we have some policy, pass the "IPSEC" 5398 * options size TCP uses this adjust the MSS. 5399 */ 5400 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5401 if (mctl_present) { 5402 freeb(first_mp); 5403 *mpp = mp; 5404 } 5405 5406 return (econnp); 5407 } 5408 5409 /* 5410 * tcp_get_conn/tcp_free_conn 5411 * 5412 * tcp_get_conn is used to get a clean tcp connection structure. 5413 * It tries to reuse the connections put on the freelist by the 5414 * time_wait_collector failing which it goes to kmem_cache. This 5415 * way has two benefits compared to just allocating from and 5416 * freeing to kmem_cache. 5417 * 1) The time_wait_collector can free (which includes the cleanup) 5418 * outside the squeue. So when the interrupt comes, we have a clean 5419 * connection sitting in the freelist. Obviously, this buys us 5420 * performance. 5421 * 5422 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5423 * has multiple disadvantages - tying up the squeue during alloc, and the 5424 * fact that IPSec policy initialization has to happen here which 5425 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5426 * But allocating the conn/tcp in IP land is also not the best since 5427 * we can't check the 'q' and 'q0' which are protected by squeue and 5428 * blindly allocate memory which might have to be freed here if we are 5429 * not allowed to accept the connection. By using the freelist and 5430 * putting the conn/tcp back in freelist, we don't pay a penalty for 5431 * allocating memory without checking 'q/q0' and freeing it if we can't 5432 * accept the connection. 5433 * 5434 * Care should be taken to put the conn back in the same squeue's freelist 5435 * from which it was allocated. Best results are obtained if conn is 5436 * allocated from listener's squeue and freed to the same. Time wait 5437 * collector will free up the freelist is the connection ends up sitting 5438 * there for too long. 5439 */ 5440 void * 5441 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5442 { 5443 tcp_t *tcp = NULL; 5444 conn_t *connp = NULL; 5445 squeue_t *sqp = (squeue_t *)arg; 5446 tcp_squeue_priv_t *tcp_time_wait; 5447 netstack_t *ns; 5448 5449 tcp_time_wait = 5450 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5451 5452 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5453 tcp = tcp_time_wait->tcp_free_list; 5454 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5455 if (tcp != NULL) { 5456 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5457 tcp_time_wait->tcp_free_list_cnt--; 5458 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5459 tcp->tcp_time_wait_next = NULL; 5460 connp = tcp->tcp_connp; 5461 connp->conn_flags |= IPCL_REUSED; 5462 5463 ASSERT(tcp->tcp_tcps == NULL); 5464 ASSERT(connp->conn_netstack == NULL); 5465 ns = tcps->tcps_netstack; 5466 netstack_hold(ns); 5467 connp->conn_netstack = ns; 5468 tcp->tcp_tcps = tcps; 5469 TCPS_REFHOLD(tcps); 5470 ipcl_globalhash_insert(connp); 5471 return ((void *)connp); 5472 } 5473 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5474 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5475 tcps->tcps_netstack)) == NULL) 5476 return (NULL); 5477 tcp = connp->conn_tcp; 5478 tcp->tcp_tcps = tcps; 5479 TCPS_REFHOLD(tcps); 5480 return ((void *)connp); 5481 } 5482 5483 /* 5484 * Update the cached label for the given tcp_t. This should be called once per 5485 * connection, and before any packets are sent or tcp_process_options is 5486 * invoked. Returns B_FALSE if the correct label could not be constructed. 5487 */ 5488 static boolean_t 5489 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5490 { 5491 conn_t *connp = tcp->tcp_connp; 5492 5493 if (tcp->tcp_ipversion == IPV4_VERSION) { 5494 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5495 int added; 5496 5497 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5498 connp->conn_mac_exempt, 5499 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5500 return (B_FALSE); 5501 5502 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5503 if (added == -1) 5504 return (B_FALSE); 5505 tcp->tcp_hdr_len += added; 5506 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5507 tcp->tcp_ip_hdr_len += added; 5508 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5509 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5510 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5511 tcp->tcp_hdr_len); 5512 if (added == -1) 5513 return (B_FALSE); 5514 tcp->tcp_hdr_len += added; 5515 tcp->tcp_tcph = (tcph_t *) 5516 ((uchar_t *)tcp->tcp_tcph + added); 5517 tcp->tcp_ip_hdr_len += added; 5518 } 5519 } else { 5520 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5521 5522 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5523 connp->conn_mac_exempt, 5524 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5525 return (B_FALSE); 5526 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5527 &tcp->tcp_label_len, optbuf) != 0) 5528 return (B_FALSE); 5529 if (tcp_build_hdrs(tcp->tcp_rq, tcp) != 0) 5530 return (B_FALSE); 5531 } 5532 5533 connp->conn_ulp_labeled = 1; 5534 5535 return (B_TRUE); 5536 } 5537 5538 /* BEGIN CSTYLED */ 5539 /* 5540 * 5541 * The sockfs ACCEPT path: 5542 * ======================= 5543 * 5544 * The eager is now established in its own perimeter as soon as SYN is 5545 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5546 * completes the accept processing on the acceptor STREAM. The sending 5547 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5548 * listener but a TLI/XTI listener completes the accept processing 5549 * on the listener perimeter. 5550 * 5551 * Common control flow for 3 way handshake: 5552 * ---------------------------------------- 5553 * 5554 * incoming SYN (listener perimeter) -> tcp_rput_data() 5555 * -> tcp_conn_request() 5556 * 5557 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5558 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5559 * 5560 * Sockfs ACCEPT Path: 5561 * ------------------- 5562 * 5563 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5564 * as STREAM entry point) 5565 * 5566 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5567 * 5568 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5569 * association (we are not behind eager's squeue but sockfs is protecting us 5570 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5571 * is changed to point at tcp_wput(). 5572 * 5573 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5574 * listener (done on listener's perimeter). 5575 * 5576 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5577 * accept. 5578 * 5579 * TLI/XTI client ACCEPT path: 5580 * --------------------------- 5581 * 5582 * soaccept() sends T_CONN_RES on the listener STREAM. 5583 * 5584 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5585 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5586 * 5587 * Locks: 5588 * ====== 5589 * 5590 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5591 * and listeners->tcp_eager_next_q. 5592 * 5593 * Referencing: 5594 * ============ 5595 * 5596 * 1) We start out in tcp_conn_request by eager placing a ref on 5597 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5598 * 5599 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5600 * doing so we place a ref on the eager. This ref is finally dropped at the 5601 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5602 * reference is dropped by the squeue framework. 5603 * 5604 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5605 * 5606 * The reference must be released by the same entity that added the reference 5607 * In the above scheme, the eager is the entity that adds and releases the 5608 * references. Note that tcp_accept_finish executes in the squeue of the eager 5609 * (albeit after it is attached to the acceptor stream). Though 1. executes 5610 * in the listener's squeue, the eager is nascent at this point and the 5611 * reference can be considered to have been added on behalf of the eager. 5612 * 5613 * Eager getting a Reset or listener closing: 5614 * ========================================== 5615 * 5616 * Once the listener and eager are linked, the listener never does the unlink. 5617 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5618 * a message on all eager perimeter. The eager then does the unlink, clears 5619 * any pointers to the listener's queue and drops the reference to the 5620 * listener. The listener waits in tcp_close outside the squeue until its 5621 * refcount has dropped to 1. This ensures that the listener has waited for 5622 * all eagers to clear their association with the listener. 5623 * 5624 * Similarly, if eager decides to go away, it can unlink itself and close. 5625 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5626 * the reference to eager is still valid because of the extra ref we put 5627 * in tcp_send_conn_ind. 5628 * 5629 * Listener can always locate the eager under the protection 5630 * of the listener->tcp_eager_lock, and then do a refhold 5631 * on the eager during the accept processing. 5632 * 5633 * The acceptor stream accesses the eager in the accept processing 5634 * based on the ref placed on eager before sending T_conn_ind. 5635 * The only entity that can negate this refhold is a listener close 5636 * which is mutually exclusive with an active acceptor stream. 5637 * 5638 * Eager's reference on the listener 5639 * =================================== 5640 * 5641 * If the accept happens (even on a closed eager) the eager drops its 5642 * reference on the listener at the start of tcp_accept_finish. If the 5643 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5644 * the reference is dropped in tcp_closei_local. If the listener closes, 5645 * the reference is dropped in tcp_eager_kill. In all cases the reference 5646 * is dropped while executing in the eager's context (squeue). 5647 */ 5648 /* END CSTYLED */ 5649 5650 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5651 5652 /* 5653 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5654 * tcp_rput_data will not see any SYN packets. 5655 */ 5656 /* ARGSUSED */ 5657 void 5658 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5659 { 5660 tcph_t *tcph; 5661 uint32_t seg_seq; 5662 tcp_t *eager; 5663 uint_t ipvers; 5664 ipha_t *ipha; 5665 ip6_t *ip6h; 5666 int err; 5667 conn_t *econnp = NULL; 5668 squeue_t *new_sqp; 5669 mblk_t *mp1; 5670 uint_t ip_hdr_len; 5671 conn_t *connp = (conn_t *)arg; 5672 tcp_t *tcp = connp->conn_tcp; 5673 cred_t *credp; 5674 tcp_stack_t *tcps = tcp->tcp_tcps; 5675 ip_stack_t *ipst; 5676 5677 if (tcp->tcp_state != TCPS_LISTEN) 5678 goto error2; 5679 5680 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5681 5682 mutex_enter(&tcp->tcp_eager_lock); 5683 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5684 mutex_exit(&tcp->tcp_eager_lock); 5685 TCP_STAT(tcps, tcp_listendrop); 5686 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5687 if (tcp->tcp_debug) { 5688 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5689 "tcp_conn_request: listen backlog (max=%d) " 5690 "overflow (%d pending) on %s", 5691 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5692 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5693 } 5694 goto error2; 5695 } 5696 5697 if (tcp->tcp_conn_req_cnt_q0 >= 5698 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5699 /* 5700 * Q0 is full. Drop a pending half-open req from the queue 5701 * to make room for the new SYN req. Also mark the time we 5702 * drop a SYN. 5703 * 5704 * A more aggressive defense against SYN attack will 5705 * be to set the "tcp_syn_defense" flag now. 5706 */ 5707 TCP_STAT(tcps, tcp_listendropq0); 5708 tcp->tcp_last_rcv_lbolt = lbolt64; 5709 if (!tcp_drop_q0(tcp)) { 5710 mutex_exit(&tcp->tcp_eager_lock); 5711 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5712 if (tcp->tcp_debug) { 5713 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5714 "tcp_conn_request: listen half-open queue " 5715 "(max=%d) full (%d pending) on %s", 5716 tcps->tcps_conn_req_max_q0, 5717 tcp->tcp_conn_req_cnt_q0, 5718 tcp_display(tcp, NULL, 5719 DISP_PORT_ONLY)); 5720 } 5721 goto error2; 5722 } 5723 } 5724 mutex_exit(&tcp->tcp_eager_lock); 5725 5726 /* 5727 * IP adds STRUIO_EAGER and ensures that the received packet is 5728 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5729 * link local address. If IPSec is enabled, db_struioflag has 5730 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5731 * otherwise an error case if neither of them is set. 5732 */ 5733 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5734 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5735 DB_CKSUMSTART(mp) = 0; 5736 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5737 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5738 if (econnp == NULL) 5739 goto error2; 5740 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5741 econnp->conn_sqp = new_sqp; 5742 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5743 /* 5744 * mp is updated in tcp_get_ipsec_conn(). 5745 */ 5746 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5747 if (econnp == NULL) { 5748 /* 5749 * mp freed by tcp_get_ipsec_conn. 5750 */ 5751 return; 5752 } 5753 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5754 } else { 5755 goto error2; 5756 } 5757 5758 ASSERT(DB_TYPE(mp) == M_DATA); 5759 5760 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5761 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5762 ASSERT(OK_32PTR(mp->b_rptr)); 5763 if (ipvers == IPV4_VERSION) { 5764 ipha = (ipha_t *)mp->b_rptr; 5765 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5766 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5767 } else { 5768 ip6h = (ip6_t *)mp->b_rptr; 5769 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5770 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5771 } 5772 5773 if (tcp->tcp_family == AF_INET) { 5774 ASSERT(ipvers == IPV4_VERSION); 5775 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5776 } else { 5777 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5778 } 5779 5780 if (err) 5781 goto error3; 5782 5783 eager = econnp->conn_tcp; 5784 5785 /* Inherit various TCP parameters from the listener */ 5786 eager->tcp_naglim = tcp->tcp_naglim; 5787 eager->tcp_first_timer_threshold = 5788 tcp->tcp_first_timer_threshold; 5789 eager->tcp_second_timer_threshold = 5790 tcp->tcp_second_timer_threshold; 5791 5792 eager->tcp_first_ctimer_threshold = 5793 tcp->tcp_first_ctimer_threshold; 5794 eager->tcp_second_ctimer_threshold = 5795 tcp->tcp_second_ctimer_threshold; 5796 5797 /* 5798 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5799 * If it does not, the eager's receive window will be set to the 5800 * listener's receive window later in this function. 5801 */ 5802 eager->tcp_rwnd = 0; 5803 5804 /* 5805 * Inherit listener's tcp_init_cwnd. Need to do this before 5806 * calling tcp_process_options() where tcp_mss_set() is called 5807 * to set the initial cwnd. 5808 */ 5809 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5810 5811 /* 5812 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5813 * zone id before the accept is completed in tcp_wput_accept(). 5814 */ 5815 econnp->conn_zoneid = connp->conn_zoneid; 5816 econnp->conn_allzones = connp->conn_allzones; 5817 5818 /* Copy nexthop information from listener to eager */ 5819 if (connp->conn_nexthop_set) { 5820 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5821 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5822 } 5823 5824 /* 5825 * TSOL: tsol_input_proc() needs the eager's cred before the 5826 * eager is accepted 5827 */ 5828 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5829 crhold(credp); 5830 5831 /* 5832 * If the caller has the process-wide flag set, then default to MAC 5833 * exempt mode. This allows read-down to unlabeled hosts. 5834 */ 5835 if (getpflags(NET_MAC_AWARE, credp) != 0) 5836 econnp->conn_mac_exempt = B_TRUE; 5837 5838 if (is_system_labeled()) { 5839 cred_t *cr; 5840 5841 if (connp->conn_mlp_type != mlptSingle) { 5842 cr = econnp->conn_peercred = DB_CRED(mp); 5843 if (cr != NULL) 5844 crhold(cr); 5845 else 5846 cr = econnp->conn_cred; 5847 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5848 econnp, cred_t *, cr) 5849 } else { 5850 cr = econnp->conn_cred; 5851 DTRACE_PROBE2(syn_accept, conn_t *, 5852 econnp, cred_t *, cr) 5853 } 5854 5855 if (!tcp_update_label(eager, cr)) { 5856 DTRACE_PROBE3( 5857 tx__ip__log__error__connrequest__tcp, 5858 char *, "eager connp(1) label on SYN mp(2) failed", 5859 conn_t *, econnp, mblk_t *, mp); 5860 goto error3; 5861 } 5862 } 5863 5864 eager->tcp_hard_binding = B_TRUE; 5865 5866 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5867 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5868 5869 CL_INET_CONNECT(eager); 5870 5871 /* 5872 * No need to check for multicast destination since ip will only pass 5873 * up multicasts to those that have expressed interest 5874 * TODO: what about rejecting broadcasts? 5875 * Also check that source is not a multicast or broadcast address. 5876 */ 5877 eager->tcp_state = TCPS_SYN_RCVD; 5878 5879 5880 /* 5881 * There should be no ire in the mp as we are being called after 5882 * receiving the SYN. 5883 */ 5884 ASSERT(tcp_ire_mp(mp) == NULL); 5885 5886 /* 5887 * Adapt our mss, ttl, ... according to information provided in IRE. 5888 */ 5889 5890 if (tcp_adapt_ire(eager, NULL) == 0) { 5891 /* Undo the bind_hash_insert */ 5892 tcp_bind_hash_remove(eager); 5893 goto error3; 5894 } 5895 5896 /* Process all TCP options. */ 5897 tcp_process_options(eager, tcph); 5898 5899 /* Is the other end ECN capable? */ 5900 if (tcps->tcps_ecn_permitted >= 1 && 5901 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5902 eager->tcp_ecn_ok = B_TRUE; 5903 } 5904 5905 /* 5906 * listener->tcp_rq->q_hiwat should be the default window size or a 5907 * window size changed via SO_RCVBUF option. First round up the 5908 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5909 * scale option value if needed. Call tcp_rwnd_set() to finish the 5910 * setting. 5911 * 5912 * Note if there is a rpipe metric associated with the remote host, 5913 * we should not inherit receive window size from listener. 5914 */ 5915 eager->tcp_rwnd = MSS_ROUNDUP( 5916 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5917 eager->tcp_rwnd), eager->tcp_mss); 5918 if (eager->tcp_snd_ws_ok) 5919 tcp_set_ws_value(eager); 5920 /* 5921 * Note that this is the only place tcp_rwnd_set() is called for 5922 * accepting a connection. We need to call it here instead of 5923 * after the 3-way handshake because we need to tell the other 5924 * side our rwnd in the SYN-ACK segment. 5925 */ 5926 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5927 5928 /* 5929 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5930 * via soaccept()->soinheritoptions() which essentially applies 5931 * all the listener options to the new STREAM. The options that we 5932 * need to take care of are: 5933 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5934 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5935 * SO_SNDBUF, SO_RCVBUF. 5936 * 5937 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5938 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5939 * tcp_maxpsz_set() gets called later from 5940 * tcp_accept_finish(), the option takes effect. 5941 * 5942 */ 5943 /* Set the TCP options */ 5944 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5945 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5946 eager->tcp_oobinline = tcp->tcp_oobinline; 5947 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5948 eager->tcp_broadcast = tcp->tcp_broadcast; 5949 eager->tcp_useloopback = tcp->tcp_useloopback; 5950 eager->tcp_dontroute = tcp->tcp_dontroute; 5951 eager->tcp_linger = tcp->tcp_linger; 5952 eager->tcp_lingertime = tcp->tcp_lingertime; 5953 if (tcp->tcp_ka_enabled) 5954 eager->tcp_ka_enabled = 1; 5955 5956 /* Set the IP options */ 5957 econnp->conn_broadcast = connp->conn_broadcast; 5958 econnp->conn_loopback = connp->conn_loopback; 5959 econnp->conn_dontroute = connp->conn_dontroute; 5960 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5961 5962 /* Put a ref on the listener for the eager. */ 5963 CONN_INC_REF(connp); 5964 mutex_enter(&tcp->tcp_eager_lock); 5965 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5966 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5967 tcp->tcp_eager_next_q0 = eager; 5968 eager->tcp_eager_prev_q0 = tcp; 5969 5970 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5971 eager->tcp_listener = tcp; 5972 eager->tcp_saved_listener = tcp; 5973 5974 /* 5975 * Tag this detached tcp vector for later retrieval 5976 * by our listener client in tcp_accept(). 5977 */ 5978 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5979 tcp->tcp_conn_req_cnt_q0++; 5980 if (++tcp->tcp_conn_req_seqnum == -1) { 5981 /* 5982 * -1 is "special" and defined in TPI as something 5983 * that should never be used in T_CONN_IND 5984 */ 5985 ++tcp->tcp_conn_req_seqnum; 5986 } 5987 mutex_exit(&tcp->tcp_eager_lock); 5988 5989 if (tcp->tcp_syn_defense) { 5990 /* Don't drop the SYN that comes from a good IP source */ 5991 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5992 if (addr_cache != NULL && eager->tcp_remote == 5993 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5994 eager->tcp_dontdrop = B_TRUE; 5995 } 5996 } 5997 5998 /* 5999 * We need to insert the eager in its own perimeter but as soon 6000 * as we do that, we expose the eager to the classifier and 6001 * should not touch any field outside the eager's perimeter. 6002 * So do all the work necessary before inserting the eager 6003 * in its own perimeter. Be optimistic that ipcl_conn_insert() 6004 * will succeed but undo everything if it fails. 6005 */ 6006 seg_seq = ABE32_TO_U32(tcph->th_seq); 6007 eager->tcp_irs = seg_seq; 6008 eager->tcp_rack = seg_seq; 6009 eager->tcp_rnxt = seg_seq + 1; 6010 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 6011 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 6012 eager->tcp_state = TCPS_SYN_RCVD; 6013 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 6014 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 6015 if (mp1 == NULL) { 6016 /* 6017 * Increment the ref count as we are going to 6018 * enqueueing an mp in squeue 6019 */ 6020 CONN_INC_REF(econnp); 6021 goto error; 6022 } 6023 DB_CPID(mp1) = tcp->tcp_cpid; 6024 eager->tcp_cpid = tcp->tcp_cpid; 6025 eager->tcp_open_time = lbolt64; 6026 6027 /* 6028 * We need to start the rto timer. In normal case, we start 6029 * the timer after sending the packet on the wire (or at 6030 * least believing that packet was sent by waiting for 6031 * CALL_IP_WPUT() to return). Since this is the first packet 6032 * being sent on the wire for the eager, our initial tcp_rto 6033 * is at least tcp_rexmit_interval_min which is a fairly 6034 * large value to allow the algorithm to adjust slowly to large 6035 * fluctuations of RTT during first few transmissions. 6036 * 6037 * Starting the timer first and then sending the packet in this 6038 * case shouldn't make much difference since tcp_rexmit_interval_min 6039 * is of the order of several 100ms and starting the timer 6040 * first and then sending the packet will result in difference 6041 * of few micro seconds. 6042 * 6043 * Without this optimization, we are forced to hold the fanout 6044 * lock across the ipcl_bind_insert() and sending the packet 6045 * so that we don't race against an incoming packet (maybe RST) 6046 * for this eager. 6047 * 6048 * It is necessary to acquire an extra reference on the eager 6049 * at this point and hold it until after tcp_send_data() to 6050 * ensure against an eager close race. 6051 */ 6052 6053 CONN_INC_REF(eager->tcp_connp); 6054 6055 TCP_RECORD_TRACE(eager, mp1, TCP_TRACE_SEND_PKT); 6056 TCP_TIMER_RESTART(eager, eager->tcp_rto); 6057 6058 6059 /* 6060 * Insert the eager in its own perimeter now. We are ready to deal 6061 * with any packets on eager. 6062 */ 6063 if (eager->tcp_ipversion == IPV4_VERSION) { 6064 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 6065 goto error; 6066 } 6067 } else { 6068 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 6069 goto error; 6070 } 6071 } 6072 6073 /* mark conn as fully-bound */ 6074 econnp->conn_fully_bound = B_TRUE; 6075 6076 /* Send the SYN-ACK */ 6077 tcp_send_data(eager, eager->tcp_wq, mp1); 6078 CONN_DEC_REF(eager->tcp_connp); 6079 freemsg(mp); 6080 6081 return; 6082 error: 6083 freemsg(mp1); 6084 eager->tcp_closemp_used = B_TRUE; 6085 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6086 squeue_fill(econnp->conn_sqp, &eager->tcp_closemp, tcp_eager_kill, 6087 econnp, SQTAG_TCP_CONN_REQ_2); 6088 6089 /* 6090 * If a connection already exists, send the mp to that connections so 6091 * that it can be appropriately dealt with. 6092 */ 6093 ipst = tcps->tcps_netstack->netstack_ip; 6094 6095 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 6096 if (!IPCL_IS_CONNECTED(econnp)) { 6097 /* 6098 * Something bad happened. ipcl_conn_insert() 6099 * failed because a connection already existed 6100 * in connected hash but we can't find it 6101 * anymore (someone blew it away). Just 6102 * free this message and hopefully remote 6103 * will retransmit at which time the SYN can be 6104 * treated as a new connection or dealth with 6105 * a TH_RST if a connection already exists. 6106 */ 6107 CONN_DEC_REF(econnp); 6108 freemsg(mp); 6109 } else { 6110 squeue_fill(econnp->conn_sqp, mp, tcp_input, 6111 econnp, SQTAG_TCP_CONN_REQ_1); 6112 } 6113 } else { 6114 /* Nobody wants this packet */ 6115 freemsg(mp); 6116 } 6117 return; 6118 error3: 6119 CONN_DEC_REF(econnp); 6120 error2: 6121 freemsg(mp); 6122 } 6123 6124 /* 6125 * In an ideal case of vertical partition in NUMA architecture, its 6126 * beneficial to have the listener and all the incoming connections 6127 * tied to the same squeue. The other constraint is that incoming 6128 * connections should be tied to the squeue attached to interrupted 6129 * CPU for obvious locality reason so this leaves the listener to 6130 * be tied to the same squeue. Our only problem is that when listener 6131 * is binding, the CPU that will get interrupted by the NIC whose 6132 * IP address the listener is binding to is not even known. So 6133 * the code below allows us to change that binding at the time the 6134 * CPU is interrupted by virtue of incoming connection's squeue. 6135 * 6136 * This is usefull only in case of a listener bound to a specific IP 6137 * address. For other kind of listeners, they get bound the 6138 * very first time and there is no attempt to rebind them. 6139 */ 6140 void 6141 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 6142 { 6143 conn_t *connp = (conn_t *)arg; 6144 squeue_t *sqp = (squeue_t *)arg2; 6145 squeue_t *new_sqp; 6146 uint32_t conn_flags; 6147 6148 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 6149 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 6150 } else { 6151 goto done; 6152 } 6153 6154 if (connp->conn_fanout == NULL) 6155 goto done; 6156 6157 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 6158 mutex_enter(&connp->conn_fanout->connf_lock); 6159 mutex_enter(&connp->conn_lock); 6160 /* 6161 * No one from read or write side can access us now 6162 * except for already queued packets on this squeue. 6163 * But since we haven't changed the squeue yet, they 6164 * can't execute. If they are processed after we have 6165 * changed the squeue, they are sent back to the 6166 * correct squeue down below. 6167 * But a listner close can race with processing of 6168 * incoming SYN. If incoming SYN processing changes 6169 * the squeue then the listener close which is waiting 6170 * to enter the squeue would operate on the wrong 6171 * squeue. Hence we don't change the squeue here unless 6172 * the refcount is exactly the minimum refcount. The 6173 * minimum refcount of 4 is counted as - 1 each for 6174 * TCP and IP, 1 for being in the classifier hash, and 6175 * 1 for the mblk being processed. 6176 */ 6177 6178 if (connp->conn_ref != 4 || 6179 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 6180 mutex_exit(&connp->conn_lock); 6181 mutex_exit(&connp->conn_fanout->connf_lock); 6182 goto done; 6183 } 6184 if (connp->conn_sqp != new_sqp) { 6185 while (connp->conn_sqp != new_sqp) 6186 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 6187 } 6188 6189 do { 6190 conn_flags = connp->conn_flags; 6191 conn_flags |= IPCL_FULLY_BOUND; 6192 (void) cas32(&connp->conn_flags, connp->conn_flags, 6193 conn_flags); 6194 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 6195 6196 mutex_exit(&connp->conn_fanout->connf_lock); 6197 mutex_exit(&connp->conn_lock); 6198 } 6199 6200 done: 6201 if (connp->conn_sqp != sqp) { 6202 CONN_INC_REF(connp); 6203 squeue_fill(connp->conn_sqp, mp, 6204 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 6205 } else { 6206 tcp_conn_request(connp, mp, sqp); 6207 } 6208 } 6209 6210 /* 6211 * Successful connect request processing begins when our client passes 6212 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 6213 * our T_OK_ACK reply message upstream. The control flow looks like this: 6214 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 6215 * upstream <- tcp_rput() <- IP 6216 * After various error checks are completed, tcp_connect() lays 6217 * the target address and port into the composite header template, 6218 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 6219 * request followed by an IRE request, and passes the three mblk message 6220 * down to IP looking like this: 6221 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 6222 * Processing continues in tcp_rput() when we receive the following message: 6223 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 6224 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 6225 * to fire off the connection request, and then passes the T_OK_ACK mblk 6226 * upstream that we filled in below. There are, of course, numerous 6227 * error conditions along the way which truncate the processing described 6228 * above. 6229 */ 6230 static void 6231 tcp_connect(tcp_t *tcp, mblk_t *mp) 6232 { 6233 sin_t *sin; 6234 sin6_t *sin6; 6235 queue_t *q = tcp->tcp_wq; 6236 struct T_conn_req *tcr; 6237 ipaddr_t *dstaddrp; 6238 in_port_t dstport; 6239 uint_t srcid; 6240 6241 tcr = (struct T_conn_req *)mp->b_rptr; 6242 6243 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6244 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 6245 tcp_err_ack(tcp, mp, TPROTO, 0); 6246 return; 6247 } 6248 6249 /* 6250 * Determine packet type based on type of address passed in 6251 * the request should contain an IPv4 or IPv6 address. 6252 * Make sure that address family matches the type of 6253 * family of the the address passed down 6254 */ 6255 switch (tcr->DEST_length) { 6256 default: 6257 tcp_err_ack(tcp, mp, TBADADDR, 0); 6258 return; 6259 6260 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 6261 /* 6262 * XXX: The check for valid DEST_length was not there 6263 * in earlier releases and some buggy 6264 * TLI apps (e.g Sybase) got away with not feeding 6265 * in sin_zero part of address. 6266 * We allow that bug to keep those buggy apps humming. 6267 * Test suites require the check on DEST_length. 6268 * We construct a new mblk with valid DEST_length 6269 * free the original so the rest of the code does 6270 * not have to keep track of this special shorter 6271 * length address case. 6272 */ 6273 mblk_t *nmp; 6274 struct T_conn_req *ntcr; 6275 sin_t *nsin; 6276 6277 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6278 tcr->OPT_length, BPRI_HI); 6279 if (nmp == NULL) { 6280 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6281 return; 6282 } 6283 ntcr = (struct T_conn_req *)nmp->b_rptr; 6284 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6285 ntcr->PRIM_type = T_CONN_REQ; 6286 ntcr->DEST_length = sizeof (sin_t); 6287 ntcr->DEST_offset = sizeof (struct T_conn_req); 6288 6289 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6290 *nsin = sin_null; 6291 /* Get pointer to shorter address to copy from original mp */ 6292 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6293 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6294 if (sin == NULL || !OK_32PTR((char *)sin)) { 6295 freemsg(nmp); 6296 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6297 return; 6298 } 6299 nsin->sin_family = sin->sin_family; 6300 nsin->sin_port = sin->sin_port; 6301 nsin->sin_addr = sin->sin_addr; 6302 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6303 nmp->b_wptr = (uchar_t *)&nsin[1]; 6304 if (tcr->OPT_length != 0) { 6305 ntcr->OPT_length = tcr->OPT_length; 6306 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6307 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6308 (uchar_t *)ntcr + ntcr->OPT_offset, 6309 tcr->OPT_length); 6310 nmp->b_wptr += tcr->OPT_length; 6311 } 6312 freemsg(mp); /* original mp freed */ 6313 mp = nmp; /* re-initialize original variables */ 6314 tcr = ntcr; 6315 } 6316 /* FALLTHRU */ 6317 6318 case sizeof (sin_t): 6319 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6320 sizeof (sin_t)); 6321 if (sin == NULL || !OK_32PTR((char *)sin)) { 6322 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6323 return; 6324 } 6325 if (tcp->tcp_family != AF_INET || 6326 sin->sin_family != AF_INET) { 6327 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6328 return; 6329 } 6330 if (sin->sin_port == 0) { 6331 tcp_err_ack(tcp, mp, TBADADDR, 0); 6332 return; 6333 } 6334 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 6335 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6336 return; 6337 } 6338 6339 break; 6340 6341 case sizeof (sin6_t): 6342 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 6343 sizeof (sin6_t)); 6344 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 6345 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6346 return; 6347 } 6348 if (tcp->tcp_family != AF_INET6 || 6349 sin6->sin6_family != AF_INET6) { 6350 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6351 return; 6352 } 6353 if (sin6->sin6_port == 0) { 6354 tcp_err_ack(tcp, mp, TBADADDR, 0); 6355 return; 6356 } 6357 break; 6358 } 6359 /* 6360 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6361 * should key on their sequence number and cut them loose. 6362 */ 6363 6364 /* 6365 * If options passed in, feed it for verification and handling 6366 */ 6367 if (tcr->OPT_length != 0) { 6368 mblk_t *ok_mp; 6369 mblk_t *discon_mp; 6370 mblk_t *conn_opts_mp; 6371 int t_error, sys_error, do_disconnect; 6372 6373 conn_opts_mp = NULL; 6374 6375 if (tcp_conprim_opt_process(tcp, mp, 6376 &do_disconnect, &t_error, &sys_error) < 0) { 6377 if (do_disconnect) { 6378 ASSERT(t_error == 0 && sys_error == 0); 6379 discon_mp = mi_tpi_discon_ind(NULL, 6380 ECONNREFUSED, 0); 6381 if (!discon_mp) { 6382 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6383 TSYSERR, ENOMEM); 6384 return; 6385 } 6386 ok_mp = mi_tpi_ok_ack_alloc(mp); 6387 if (!ok_mp) { 6388 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6389 TSYSERR, ENOMEM); 6390 return; 6391 } 6392 qreply(q, ok_mp); 6393 qreply(q, discon_mp); /* no flush! */ 6394 } else { 6395 ASSERT(t_error != 0); 6396 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6397 sys_error); 6398 } 6399 return; 6400 } 6401 /* 6402 * Success in setting options, the mp option buffer represented 6403 * by OPT_length/offset has been potentially modified and 6404 * contains results of option processing. We copy it in 6405 * another mp to save it for potentially influencing returning 6406 * it in T_CONN_CONN. 6407 */ 6408 if (tcr->OPT_length != 0) { /* there are resulting options */ 6409 conn_opts_mp = copyb(mp); 6410 if (!conn_opts_mp) { 6411 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6412 TSYSERR, ENOMEM); 6413 return; 6414 } 6415 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6416 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6417 /* 6418 * Note: 6419 * These resulting option negotiation can include any 6420 * end-to-end negotiation options but there no such 6421 * thing (yet?) in our TCP/IP. 6422 */ 6423 } 6424 } 6425 6426 /* 6427 * If we're connecting to an IPv4-mapped IPv6 address, we need to 6428 * make sure that the template IP header in the tcp structure is an 6429 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 6430 * need to this before we call tcp_bindi() so that the port lookup 6431 * code will look for ports in the correct port space (IPv4 and 6432 * IPv6 have separate port spaces). 6433 */ 6434 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 6435 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6436 int err = 0; 6437 6438 err = tcp_header_init_ipv4(tcp); 6439 if (err != 0) { 6440 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6441 goto connect_failed; 6442 } 6443 if (tcp->tcp_lport != 0) 6444 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 6445 } 6446 6447 if (tcp->tcp_issocket) { 6448 /* 6449 * TCP is _D_SODIRECT and sockfs is directly above so save 6450 * the shared sonode sodirect_t pointer (if any) to enable 6451 * TCP sodirect. 6452 */ 6453 tcp->tcp_sodirect = SOD_QTOSODP(tcp->tcp_rq); 6454 } 6455 6456 switch (tcp->tcp_state) { 6457 case TCPS_IDLE: 6458 /* 6459 * We support quick connect, refer to comments in 6460 * tcp_connect_*() 6461 */ 6462 /* FALLTHRU */ 6463 case TCPS_BOUND: 6464 case TCPS_LISTEN: 6465 if (tcp->tcp_family == AF_INET6) { 6466 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6467 tcp_connect_ipv6(tcp, mp, 6468 &sin6->sin6_addr, 6469 sin6->sin6_port, sin6->sin6_flowinfo, 6470 sin6->__sin6_src_id, sin6->sin6_scope_id); 6471 return; 6472 } 6473 /* 6474 * Destination adress is mapped IPv6 address. 6475 * Source bound address should be unspecified or 6476 * IPv6 mapped address as well. 6477 */ 6478 if (!IN6_IS_ADDR_UNSPECIFIED( 6479 &tcp->tcp_bound_source_v6) && 6480 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 6481 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 6482 EADDRNOTAVAIL); 6483 break; 6484 } 6485 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 6486 dstport = sin6->sin6_port; 6487 srcid = sin6->__sin6_src_id; 6488 } else { 6489 dstaddrp = &sin->sin_addr.s_addr; 6490 dstport = sin->sin_port; 6491 srcid = 0; 6492 } 6493 6494 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 6495 return; 6496 default: 6497 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 6498 break; 6499 } 6500 /* 6501 * Note: Code below is the "failure" case 6502 */ 6503 /* return error ack and blow away saved option results if any */ 6504 connect_failed: 6505 if (mp != NULL) 6506 putnext(tcp->tcp_rq, mp); 6507 else { 6508 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6509 TSYSERR, ENOMEM); 6510 } 6511 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6512 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6513 } 6514 6515 /* 6516 * Handle connect to IPv4 destinations, including connections for AF_INET6 6517 * sockets connecting to IPv4 mapped IPv6 destinations. 6518 */ 6519 static void 6520 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6521 uint_t srcid) 6522 { 6523 tcph_t *tcph; 6524 mblk_t *mp1; 6525 ipaddr_t dstaddr = *dstaddrp; 6526 int32_t oldstate; 6527 uint16_t lport; 6528 tcp_stack_t *tcps = tcp->tcp_tcps; 6529 6530 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6531 6532 /* Check for attempt to connect to INADDR_ANY */ 6533 if (dstaddr == INADDR_ANY) { 6534 /* 6535 * SunOS 4.x and 4.3 BSD allow an application 6536 * to connect a TCP socket to INADDR_ANY. 6537 * When they do this, the kernel picks the 6538 * address of one interface and uses it 6539 * instead. The kernel usually ends up 6540 * picking the address of the loopback 6541 * interface. This is an undocumented feature. 6542 * However, we provide the same thing here 6543 * in order to have source and binary 6544 * compatibility with SunOS 4.x. 6545 * Update the T_CONN_REQ (sin/sin6) since it is used to 6546 * generate the T_CONN_CON. 6547 */ 6548 dstaddr = htonl(INADDR_LOOPBACK); 6549 *dstaddrp = dstaddr; 6550 } 6551 6552 /* Handle __sin6_src_id if socket not bound to an IP address */ 6553 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6554 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6555 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6556 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6557 tcp->tcp_ipha->ipha_src); 6558 } 6559 6560 /* 6561 * Don't let an endpoint connect to itself. Note that 6562 * the test here does not catch the case where the 6563 * source IP addr was left unspecified by the user. In 6564 * this case, the source addr is set in tcp_adapt_ire() 6565 * using the reply to the T_BIND message that we send 6566 * down to IP here and the check is repeated in tcp_rput_other. 6567 */ 6568 if (dstaddr == tcp->tcp_ipha->ipha_src && 6569 dstport == tcp->tcp_lport) { 6570 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6571 goto failed; 6572 } 6573 6574 tcp->tcp_ipha->ipha_dst = dstaddr; 6575 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6576 6577 /* 6578 * Massage a source route if any putting the first hop 6579 * in iph_dst. Compute a starting value for the checksum which 6580 * takes into account that the original iph_dst should be 6581 * included in the checksum but that ip will include the 6582 * first hop in the source route in the tcp checksum. 6583 */ 6584 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6585 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6586 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6587 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6588 if ((int)tcp->tcp_sum < 0) 6589 tcp->tcp_sum--; 6590 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6591 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6592 (tcp->tcp_sum >> 16)); 6593 tcph = tcp->tcp_tcph; 6594 *(uint16_t *)tcph->th_fport = dstport; 6595 tcp->tcp_fport = dstport; 6596 6597 oldstate = tcp->tcp_state; 6598 /* 6599 * At this point the remote destination address and remote port fields 6600 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6601 * have to see which state tcp was in so we can take apropriate action. 6602 */ 6603 if (oldstate == TCPS_IDLE) { 6604 /* 6605 * We support a quick connect capability here, allowing 6606 * clients to transition directly from IDLE to SYN_SENT 6607 * tcp_bindi will pick an unused port, insert the connection 6608 * in the bind hash and transition to BOUND state. 6609 */ 6610 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6611 tcp, B_TRUE); 6612 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6613 B_FALSE, B_FALSE); 6614 if (lport == 0) { 6615 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6616 goto failed; 6617 } 6618 } 6619 tcp->tcp_state = TCPS_SYN_SENT; 6620 6621 /* 6622 * TODO: allow data with connect requests 6623 * by unlinking M_DATA trailers here and 6624 * linking them in behind the T_OK_ACK mblk. 6625 * The tcp_rput() bind ack handler would then 6626 * feed them to tcp_wput_data() rather than call 6627 * tcp_timer(). 6628 */ 6629 mp = mi_tpi_ok_ack_alloc(mp); 6630 if (!mp) { 6631 tcp->tcp_state = oldstate; 6632 goto failed; 6633 } 6634 if (tcp->tcp_family == AF_INET) { 6635 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6636 sizeof (ipa_conn_t)); 6637 } else { 6638 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6639 sizeof (ipa6_conn_t)); 6640 } 6641 if (mp1) { 6642 /* 6643 * We need to make sure that the conn_recv is set to a non-null 6644 * value before we insert the conn_t into the classifier table. 6645 * This is to avoid a race with an incoming packet which does 6646 * an ipcl_classify(). 6647 */ 6648 tcp->tcp_connp->conn_recv = tcp_input; 6649 6650 /* Hang onto the T_OK_ACK for later. */ 6651 linkb(mp1, mp); 6652 mblk_setcred(mp1, tcp->tcp_cred); 6653 if (tcp->tcp_family == AF_INET) 6654 mp1 = ip_bind_v4(tcp->tcp_wq, mp1, tcp->tcp_connp); 6655 else { 6656 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6657 &tcp->tcp_sticky_ipp); 6658 } 6659 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6660 tcp->tcp_active_open = 1; 6661 /* 6662 * If the bind cannot complete immediately 6663 * IP will arrange to call tcp_rput_other 6664 * when the bind completes. 6665 */ 6666 if (mp1 != NULL) 6667 tcp_rput_other(tcp, mp1); 6668 return; 6669 } 6670 /* Error case */ 6671 tcp->tcp_state = oldstate; 6672 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6673 6674 failed: 6675 /* return error ack and blow away saved option results if any */ 6676 if (mp != NULL) 6677 putnext(tcp->tcp_rq, mp); 6678 else { 6679 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6680 TSYSERR, ENOMEM); 6681 } 6682 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6683 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6684 6685 } 6686 6687 /* 6688 * Handle connect to IPv6 destinations. 6689 */ 6690 static void 6691 tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 6692 in_port_t dstport, uint32_t flowinfo, uint_t srcid, uint32_t scope_id) 6693 { 6694 tcph_t *tcph; 6695 mblk_t *mp1; 6696 ip6_rthdr_t *rth; 6697 int32_t oldstate; 6698 uint16_t lport; 6699 tcp_stack_t *tcps = tcp->tcp_tcps; 6700 6701 ASSERT(tcp->tcp_family == AF_INET6); 6702 6703 /* 6704 * If we're here, it means that the destination address is a native 6705 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6706 * reason why it might not be IPv6 is if the socket was bound to an 6707 * IPv4-mapped IPv6 address. 6708 */ 6709 if (tcp->tcp_ipversion != IPV6_VERSION) { 6710 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6711 goto failed; 6712 } 6713 6714 /* 6715 * Interpret a zero destination to mean loopback. 6716 * Update the T_CONN_REQ (sin/sin6) since it is used to 6717 * generate the T_CONN_CON. 6718 */ 6719 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6720 *dstaddrp = ipv6_loopback; 6721 } 6722 6723 /* Handle __sin6_src_id if socket not bound to an IP address */ 6724 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6725 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6726 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6727 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6728 } 6729 6730 /* 6731 * Take care of the scope_id now and add ip6i_t 6732 * if ip6i_t is not already allocated through TCP 6733 * sticky options. At this point tcp_ip6h does not 6734 * have dst info, thus use dstaddrp. 6735 */ 6736 if (scope_id != 0 && 6737 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6738 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6739 ip6i_t *ip6i; 6740 6741 ipp->ipp_ifindex = scope_id; 6742 ip6i = (ip6i_t *)tcp->tcp_iphc; 6743 6744 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6745 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6746 /* Already allocated */ 6747 ip6i->ip6i_flags |= IP6I_IFINDEX; 6748 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6749 ipp->ipp_fields |= IPPF_SCOPE_ID; 6750 } else { 6751 int reterr; 6752 6753 ipp->ipp_fields |= IPPF_SCOPE_ID; 6754 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6755 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6756 reterr = tcp_build_hdrs(tcp->tcp_rq, tcp); 6757 if (reterr != 0) 6758 goto failed; 6759 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6760 } 6761 } 6762 6763 /* 6764 * Don't let an endpoint connect to itself. Note that 6765 * the test here does not catch the case where the 6766 * source IP addr was left unspecified by the user. In 6767 * this case, the source addr is set in tcp_adapt_ire() 6768 * using the reply to the T_BIND message that we send 6769 * down to IP here and the check is repeated in tcp_rput_other. 6770 */ 6771 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6772 (dstport == tcp->tcp_lport)) { 6773 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6774 goto failed; 6775 } 6776 6777 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6778 tcp->tcp_remote_v6 = *dstaddrp; 6779 tcp->tcp_ip6h->ip6_vcf = 6780 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6781 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6782 6783 6784 /* 6785 * Massage a routing header (if present) putting the first hop 6786 * in ip6_dst. Compute a starting value for the checksum which 6787 * takes into account that the original ip6_dst should be 6788 * included in the checksum but that ip will include the 6789 * first hop in the source route in the tcp checksum. 6790 */ 6791 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6792 if (rth != NULL) { 6793 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6794 tcps->tcps_netstack); 6795 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6796 (tcp->tcp_sum >> 16)); 6797 } else { 6798 tcp->tcp_sum = 0; 6799 } 6800 6801 tcph = tcp->tcp_tcph; 6802 *(uint16_t *)tcph->th_fport = dstport; 6803 tcp->tcp_fport = dstport; 6804 6805 oldstate = tcp->tcp_state; 6806 /* 6807 * At this point the remote destination address and remote port fields 6808 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6809 * have to see which state tcp was in so we can take apropriate action. 6810 */ 6811 if (oldstate == TCPS_IDLE) { 6812 /* 6813 * We support a quick connect capability here, allowing 6814 * clients to transition directly from IDLE to SYN_SENT 6815 * tcp_bindi will pick an unused port, insert the connection 6816 * in the bind hash and transition to BOUND state. 6817 */ 6818 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6819 tcp, B_TRUE); 6820 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6821 B_FALSE, B_FALSE); 6822 if (lport == 0) { 6823 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6824 goto failed; 6825 } 6826 } 6827 tcp->tcp_state = TCPS_SYN_SENT; 6828 /* 6829 * TODO: allow data with connect requests 6830 * by unlinking M_DATA trailers here and 6831 * linking them in behind the T_OK_ACK mblk. 6832 * The tcp_rput() bind ack handler would then 6833 * feed them to tcp_wput_data() rather than call 6834 * tcp_timer(). 6835 */ 6836 mp = mi_tpi_ok_ack_alloc(mp); 6837 if (!mp) { 6838 tcp->tcp_state = oldstate; 6839 goto failed; 6840 } 6841 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 6842 if (mp1) { 6843 /* 6844 * We need to make sure that the conn_recv is set to a non-null 6845 * value before we insert the conn_t into the classifier table. 6846 * This is to avoid a race with an incoming packet which does 6847 * an ipcl_classify(). 6848 */ 6849 tcp->tcp_connp->conn_recv = tcp_input; 6850 6851 /* Hang onto the T_OK_ACK for later. */ 6852 linkb(mp1, mp); 6853 mblk_setcred(mp1, tcp->tcp_cred); 6854 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6855 &tcp->tcp_sticky_ipp); 6856 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6857 tcp->tcp_active_open = 1; 6858 /* ip_bind_v6() may return ACK or ERROR */ 6859 if (mp1 != NULL) 6860 tcp_rput_other(tcp, mp1); 6861 return; 6862 } 6863 /* Error case */ 6864 tcp->tcp_state = oldstate; 6865 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6866 6867 failed: 6868 /* return error ack and blow away saved option results if any */ 6869 if (mp != NULL) 6870 putnext(tcp->tcp_rq, mp); 6871 else { 6872 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6873 TSYSERR, ENOMEM); 6874 } 6875 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6876 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6877 } 6878 6879 /* 6880 * We need a stream q for detached closing tcp connections 6881 * to use. Our client hereby indicates that this q is the 6882 * one to use. 6883 */ 6884 static void 6885 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6886 { 6887 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6888 queue_t *q = tcp->tcp_wq; 6889 tcp_stack_t *tcps = tcp->tcp_tcps; 6890 6891 #ifdef NS_DEBUG 6892 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6893 tcps->tcps_netstack->netstack_stackid); 6894 #endif 6895 mp->b_datap->db_type = M_IOCACK; 6896 iocp->ioc_count = 0; 6897 mutex_enter(&tcps->tcps_g_q_lock); 6898 if (tcps->tcps_g_q != NULL) { 6899 mutex_exit(&tcps->tcps_g_q_lock); 6900 iocp->ioc_error = EALREADY; 6901 } else { 6902 mblk_t *mp1; 6903 6904 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6905 if (mp1 == NULL) { 6906 mutex_exit(&tcps->tcps_g_q_lock); 6907 iocp->ioc_error = ENOMEM; 6908 } else { 6909 tcps->tcps_g_q = tcp->tcp_rq; 6910 mutex_exit(&tcps->tcps_g_q_lock); 6911 iocp->ioc_error = 0; 6912 iocp->ioc_rval = 0; 6913 /* 6914 * We are passing tcp_sticky_ipp as NULL 6915 * as it is not useful for tcp_default queue 6916 * 6917 * Set conn_recv just in case. 6918 */ 6919 tcp->tcp_connp->conn_recv = tcp_conn_request; 6920 6921 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6922 if (mp1 != NULL) 6923 tcp_rput_other(tcp, mp1); 6924 } 6925 } 6926 qreply(q, mp); 6927 } 6928 6929 /* 6930 * Our client hereby directs us to reject the connection request 6931 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6932 * of sending the appropriate RST, not an ICMP error. 6933 */ 6934 static void 6935 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6936 { 6937 tcp_t *ltcp = NULL; 6938 t_scalar_t seqnum; 6939 conn_t *connp; 6940 tcp_stack_t *tcps = tcp->tcp_tcps; 6941 6942 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6943 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6944 tcp_err_ack(tcp, mp, TPROTO, 0); 6945 return; 6946 } 6947 6948 /* 6949 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6950 * when the stream is in BOUND state. Do not send a reset, 6951 * since the destination IP address is not valid, and it can 6952 * be the initialized value of all zeros (broadcast address). 6953 * 6954 * If TCP has sent down a bind request to IP and has not 6955 * received the reply, reject the request. Otherwise, TCP 6956 * will be confused. 6957 */ 6958 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6959 if (tcp->tcp_debug) { 6960 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6961 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6962 } 6963 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6964 return; 6965 } 6966 6967 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6968 6969 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6970 6971 /* 6972 * According to TPI, for non-listeners, ignore seqnum 6973 * and disconnect. 6974 * Following interpretation of -1 seqnum is historical 6975 * and implied TPI ? (TPI only states that for T_CONN_IND, 6976 * a valid seqnum should not be -1). 6977 * 6978 * -1 means disconnect everything 6979 * regardless even on a listener. 6980 */ 6981 6982 int old_state = tcp->tcp_state; 6983 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6984 6985 /* 6986 * The connection can't be on the tcp_time_wait_head list 6987 * since it is not detached. 6988 */ 6989 ASSERT(tcp->tcp_time_wait_next == NULL); 6990 ASSERT(tcp->tcp_time_wait_prev == NULL); 6991 ASSERT(tcp->tcp_time_wait_expire == 0); 6992 ltcp = NULL; 6993 /* 6994 * If it used to be a listener, check to make sure no one else 6995 * has taken the port before switching back to LISTEN state. 6996 */ 6997 if (tcp->tcp_ipversion == IPV4_VERSION) { 6998 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6999 tcp->tcp_ipha->ipha_src, 7000 tcp->tcp_connp->conn_zoneid, ipst); 7001 if (connp != NULL) 7002 ltcp = connp->conn_tcp; 7003 } else { 7004 /* Allow tcp_bound_if listeners? */ 7005 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 7006 &tcp->tcp_ip6h->ip6_src, 0, 7007 tcp->tcp_connp->conn_zoneid, ipst); 7008 if (connp != NULL) 7009 ltcp = connp->conn_tcp; 7010 } 7011 if (tcp->tcp_conn_req_max && ltcp == NULL) { 7012 tcp->tcp_state = TCPS_LISTEN; 7013 } else if (old_state > TCPS_BOUND) { 7014 tcp->tcp_conn_req_max = 0; 7015 tcp->tcp_state = TCPS_BOUND; 7016 } 7017 if (ltcp != NULL) 7018 CONN_DEC_REF(ltcp->tcp_connp); 7019 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 7020 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 7021 } else if (old_state == TCPS_ESTABLISHED || 7022 old_state == TCPS_CLOSE_WAIT) { 7023 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 7024 } 7025 7026 if (tcp->tcp_fused) 7027 tcp_unfuse(tcp); 7028 7029 mutex_enter(&tcp->tcp_eager_lock); 7030 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 7031 (tcp->tcp_conn_req_cnt_q != 0)) { 7032 tcp_eager_cleanup(tcp, 0); 7033 } 7034 mutex_exit(&tcp->tcp_eager_lock); 7035 7036 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 7037 tcp->tcp_rnxt, TH_RST | TH_ACK); 7038 7039 tcp_reinit(tcp); 7040 7041 if (old_state >= TCPS_ESTABLISHED) { 7042 /* Send M_FLUSH according to TPI */ 7043 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 7044 } 7045 mp = mi_tpi_ok_ack_alloc(mp); 7046 if (mp) 7047 putnext(tcp->tcp_rq, mp); 7048 return; 7049 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 7050 tcp_err_ack(tcp, mp, TBADSEQ, 0); 7051 return; 7052 } 7053 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 7054 /* Send M_FLUSH according to TPI */ 7055 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 7056 } 7057 mp = mi_tpi_ok_ack_alloc(mp); 7058 if (mp) 7059 putnext(tcp->tcp_rq, mp); 7060 } 7061 7062 /* 7063 * Diagnostic routine used to return a string associated with the tcp state. 7064 * Note that if the caller does not supply a buffer, it will use an internal 7065 * static string. This means that if multiple threads call this function at 7066 * the same time, output can be corrupted... Note also that this function 7067 * does not check the size of the supplied buffer. The caller has to make 7068 * sure that it is big enough. 7069 */ 7070 static char * 7071 tcp_display(tcp_t *tcp, char *sup_buf, char format) 7072 { 7073 char buf1[30]; 7074 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 7075 char *buf; 7076 char *cp; 7077 in6_addr_t local, remote; 7078 char local_addrbuf[INET6_ADDRSTRLEN]; 7079 char remote_addrbuf[INET6_ADDRSTRLEN]; 7080 7081 if (sup_buf != NULL) 7082 buf = sup_buf; 7083 else 7084 buf = priv_buf; 7085 7086 if (tcp == NULL) 7087 return ("NULL_TCP"); 7088 switch (tcp->tcp_state) { 7089 case TCPS_CLOSED: 7090 cp = "TCP_CLOSED"; 7091 break; 7092 case TCPS_IDLE: 7093 cp = "TCP_IDLE"; 7094 break; 7095 case TCPS_BOUND: 7096 cp = "TCP_BOUND"; 7097 break; 7098 case TCPS_LISTEN: 7099 cp = "TCP_LISTEN"; 7100 break; 7101 case TCPS_SYN_SENT: 7102 cp = "TCP_SYN_SENT"; 7103 break; 7104 case TCPS_SYN_RCVD: 7105 cp = "TCP_SYN_RCVD"; 7106 break; 7107 case TCPS_ESTABLISHED: 7108 cp = "TCP_ESTABLISHED"; 7109 break; 7110 case TCPS_CLOSE_WAIT: 7111 cp = "TCP_CLOSE_WAIT"; 7112 break; 7113 case TCPS_FIN_WAIT_1: 7114 cp = "TCP_FIN_WAIT_1"; 7115 break; 7116 case TCPS_CLOSING: 7117 cp = "TCP_CLOSING"; 7118 break; 7119 case TCPS_LAST_ACK: 7120 cp = "TCP_LAST_ACK"; 7121 break; 7122 case TCPS_FIN_WAIT_2: 7123 cp = "TCP_FIN_WAIT_2"; 7124 break; 7125 case TCPS_TIME_WAIT: 7126 cp = "TCP_TIME_WAIT"; 7127 break; 7128 default: 7129 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 7130 cp = buf1; 7131 break; 7132 } 7133 switch (format) { 7134 case DISP_ADDR_AND_PORT: 7135 if (tcp->tcp_ipversion == IPV4_VERSION) { 7136 /* 7137 * Note that we use the remote address in the tcp_b 7138 * structure. This means that it will print out 7139 * the real destination address, not the next hop's 7140 * address if source routing is used. 7141 */ 7142 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 7143 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 7144 7145 } else { 7146 local = tcp->tcp_ip_src_v6; 7147 remote = tcp->tcp_remote_v6; 7148 } 7149 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 7150 sizeof (local_addrbuf)); 7151 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 7152 sizeof (remote_addrbuf)); 7153 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 7154 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 7155 ntohs(tcp->tcp_fport), cp); 7156 break; 7157 case DISP_PORT_ONLY: 7158 default: 7159 (void) mi_sprintf(buf, "[%u, %u] %s", 7160 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 7161 break; 7162 } 7163 7164 return (buf); 7165 } 7166 7167 /* 7168 * Called via squeue to get on to eager's perimeter. It sends a 7169 * TH_RST if eager is in the fanout table. The listener wants the 7170 * eager to disappear either by means of tcp_eager_blowoff() or 7171 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 7172 * called (via squeue) if the eager cannot be inserted in the 7173 * fanout table in tcp_conn_request(). 7174 */ 7175 /* ARGSUSED */ 7176 void 7177 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 7178 { 7179 conn_t *econnp = (conn_t *)arg; 7180 tcp_t *eager = econnp->conn_tcp; 7181 tcp_t *listener = eager->tcp_listener; 7182 tcp_stack_t *tcps = eager->tcp_tcps; 7183 7184 /* 7185 * We could be called because listener is closing. Since 7186 * the eager is using listener's queue's, its not safe. 7187 * Better use the default queue just to send the TH_RST 7188 * out. 7189 */ 7190 ASSERT(tcps->tcps_g_q != NULL); 7191 eager->tcp_rq = tcps->tcps_g_q; 7192 eager->tcp_wq = WR(tcps->tcps_g_q); 7193 7194 /* 7195 * An eager's conn_fanout will be NULL if it's a duplicate 7196 * for an existing 4-tuples in the conn fanout table. 7197 * We don't want to send an RST out in such case. 7198 */ 7199 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 7200 tcp_xmit_ctl("tcp_eager_kill, can't wait", 7201 eager, eager->tcp_snxt, 0, TH_RST); 7202 } 7203 7204 /* We are here because listener wants this eager gone */ 7205 if (listener != NULL) { 7206 mutex_enter(&listener->tcp_eager_lock); 7207 tcp_eager_unlink(eager); 7208 if (eager->tcp_tconnind_started) { 7209 /* 7210 * The eager has sent a conn_ind up to the 7211 * listener but listener decides to close 7212 * instead. We need to drop the extra ref 7213 * placed on eager in tcp_rput_data() before 7214 * sending the conn_ind to listener. 7215 */ 7216 CONN_DEC_REF(econnp); 7217 } 7218 mutex_exit(&listener->tcp_eager_lock); 7219 CONN_DEC_REF(listener->tcp_connp); 7220 } 7221 7222 if (eager->tcp_state > TCPS_BOUND) 7223 tcp_close_detached(eager); 7224 } 7225 7226 /* 7227 * Reset any eager connection hanging off this listener marked 7228 * with 'seqnum' and then reclaim it's resources. 7229 */ 7230 static boolean_t 7231 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 7232 { 7233 tcp_t *eager; 7234 mblk_t *mp; 7235 tcp_stack_t *tcps = listener->tcp_tcps; 7236 7237 TCP_STAT(tcps, tcp_eager_blowoff_calls); 7238 eager = listener; 7239 mutex_enter(&listener->tcp_eager_lock); 7240 do { 7241 eager = eager->tcp_eager_next_q; 7242 if (eager == NULL) { 7243 mutex_exit(&listener->tcp_eager_lock); 7244 return (B_FALSE); 7245 } 7246 } while (eager->tcp_conn_req_seqnum != seqnum); 7247 7248 if (eager->tcp_closemp_used) { 7249 mutex_exit(&listener->tcp_eager_lock); 7250 return (B_TRUE); 7251 } 7252 eager->tcp_closemp_used = B_TRUE; 7253 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7254 CONN_INC_REF(eager->tcp_connp); 7255 mutex_exit(&listener->tcp_eager_lock); 7256 mp = &eager->tcp_closemp; 7257 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 7258 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 7259 return (B_TRUE); 7260 } 7261 7262 /* 7263 * Reset any eager connection hanging off this listener 7264 * and then reclaim it's resources. 7265 */ 7266 static void 7267 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 7268 { 7269 tcp_t *eager; 7270 mblk_t *mp; 7271 tcp_stack_t *tcps = listener->tcp_tcps; 7272 7273 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7274 7275 if (!q0_only) { 7276 /* First cleanup q */ 7277 TCP_STAT(tcps, tcp_eager_blowoff_q); 7278 eager = listener->tcp_eager_next_q; 7279 while (eager != NULL) { 7280 if (!eager->tcp_closemp_used) { 7281 eager->tcp_closemp_used = B_TRUE; 7282 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7283 CONN_INC_REF(eager->tcp_connp); 7284 mp = &eager->tcp_closemp; 7285 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7286 tcp_eager_kill, eager->tcp_connp, 7287 SQTAG_TCP_EAGER_CLEANUP); 7288 } 7289 eager = eager->tcp_eager_next_q; 7290 } 7291 } 7292 /* Then cleanup q0 */ 7293 TCP_STAT(tcps, tcp_eager_blowoff_q0); 7294 eager = listener->tcp_eager_next_q0; 7295 while (eager != listener) { 7296 if (!eager->tcp_closemp_used) { 7297 eager->tcp_closemp_used = B_TRUE; 7298 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7299 CONN_INC_REF(eager->tcp_connp); 7300 mp = &eager->tcp_closemp; 7301 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7302 tcp_eager_kill, eager->tcp_connp, 7303 SQTAG_TCP_EAGER_CLEANUP_Q0); 7304 } 7305 eager = eager->tcp_eager_next_q0; 7306 } 7307 } 7308 7309 /* 7310 * If we are an eager connection hanging off a listener that hasn't 7311 * formally accepted the connection yet, get off his list and blow off 7312 * any data that we have accumulated. 7313 */ 7314 static void 7315 tcp_eager_unlink(tcp_t *tcp) 7316 { 7317 tcp_t *listener = tcp->tcp_listener; 7318 7319 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7320 ASSERT(listener != NULL); 7321 if (tcp->tcp_eager_next_q0 != NULL) { 7322 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 7323 7324 /* Remove the eager tcp from q0 */ 7325 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 7326 tcp->tcp_eager_prev_q0; 7327 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 7328 tcp->tcp_eager_next_q0; 7329 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 7330 listener->tcp_conn_req_cnt_q0--; 7331 7332 tcp->tcp_eager_next_q0 = NULL; 7333 tcp->tcp_eager_prev_q0 = NULL; 7334 7335 /* 7336 * Take the eager out, if it is in the list of droppable 7337 * eagers. 7338 */ 7339 MAKE_UNDROPPABLE(tcp); 7340 7341 if (tcp->tcp_syn_rcvd_timeout != 0) { 7342 /* we have timed out before */ 7343 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 7344 listener->tcp_syn_rcvd_timeout--; 7345 } 7346 } else { 7347 tcp_t **tcpp = &listener->tcp_eager_next_q; 7348 tcp_t *prev = NULL; 7349 7350 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 7351 if (tcpp[0] == tcp) { 7352 if (listener->tcp_eager_last_q == tcp) { 7353 /* 7354 * If we are unlinking the last 7355 * element on the list, adjust 7356 * tail pointer. Set tail pointer 7357 * to nil when list is empty. 7358 */ 7359 ASSERT(tcp->tcp_eager_next_q == NULL); 7360 if (listener->tcp_eager_last_q == 7361 listener->tcp_eager_next_q) { 7362 listener->tcp_eager_last_q = 7363 NULL; 7364 } else { 7365 /* 7366 * We won't get here if there 7367 * is only one eager in the 7368 * list. 7369 */ 7370 ASSERT(prev != NULL); 7371 listener->tcp_eager_last_q = 7372 prev; 7373 } 7374 } 7375 tcpp[0] = tcp->tcp_eager_next_q; 7376 tcp->tcp_eager_next_q = NULL; 7377 tcp->tcp_eager_last_q = NULL; 7378 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7379 listener->tcp_conn_req_cnt_q--; 7380 break; 7381 } 7382 prev = tcpp[0]; 7383 } 7384 } 7385 tcp->tcp_listener = NULL; 7386 } 7387 7388 /* Shorthand to generate and send TPI error acks to our client */ 7389 static void 7390 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7391 { 7392 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7393 putnext(tcp->tcp_rq, mp); 7394 } 7395 7396 /* Shorthand to generate and send TPI error acks to our client */ 7397 static void 7398 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7399 int t_error, int sys_error) 7400 { 7401 struct T_error_ack *teackp; 7402 7403 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7404 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7405 teackp = (struct T_error_ack *)mp->b_rptr; 7406 teackp->ERROR_prim = primitive; 7407 teackp->TLI_error = t_error; 7408 teackp->UNIX_error = sys_error; 7409 putnext(tcp->tcp_rq, mp); 7410 } 7411 } 7412 7413 /* 7414 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7415 * but instead the code relies on: 7416 * - the fact that the address of the array and its size never changes 7417 * - the atomic assignment of the elements of the array 7418 */ 7419 /* ARGSUSED */ 7420 static int 7421 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7422 { 7423 int i; 7424 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7425 7426 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7427 if (tcps->tcps_g_epriv_ports[i] != 0) 7428 (void) mi_mpprintf(mp, "%d ", 7429 tcps->tcps_g_epriv_ports[i]); 7430 } 7431 return (0); 7432 } 7433 7434 /* 7435 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7436 * threads from changing it at the same time. 7437 */ 7438 /* ARGSUSED */ 7439 static int 7440 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7441 cred_t *cr) 7442 { 7443 long new_value; 7444 int i; 7445 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7446 7447 /* 7448 * Fail the request if the new value does not lie within the 7449 * port number limits. 7450 */ 7451 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7452 new_value <= 0 || new_value >= 65536) { 7453 return (EINVAL); 7454 } 7455 7456 mutex_enter(&tcps->tcps_epriv_port_lock); 7457 /* Check if the value is already in the list */ 7458 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7459 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7460 mutex_exit(&tcps->tcps_epriv_port_lock); 7461 return (EEXIST); 7462 } 7463 } 7464 /* Find an empty slot */ 7465 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7466 if (tcps->tcps_g_epriv_ports[i] == 0) 7467 break; 7468 } 7469 if (i == tcps->tcps_g_num_epriv_ports) { 7470 mutex_exit(&tcps->tcps_epriv_port_lock); 7471 return (EOVERFLOW); 7472 } 7473 /* Set the new value */ 7474 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7475 mutex_exit(&tcps->tcps_epriv_port_lock); 7476 return (0); 7477 } 7478 7479 /* 7480 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7481 * threads from changing it at the same time. 7482 */ 7483 /* ARGSUSED */ 7484 static int 7485 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7486 cred_t *cr) 7487 { 7488 long new_value; 7489 int i; 7490 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7491 7492 /* 7493 * Fail the request if the new value does not lie within the 7494 * port number limits. 7495 */ 7496 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7497 new_value >= 65536) { 7498 return (EINVAL); 7499 } 7500 7501 mutex_enter(&tcps->tcps_epriv_port_lock); 7502 /* Check that the value is already in the list */ 7503 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7504 if (tcps->tcps_g_epriv_ports[i] == new_value) 7505 break; 7506 } 7507 if (i == tcps->tcps_g_num_epriv_ports) { 7508 mutex_exit(&tcps->tcps_epriv_port_lock); 7509 return (ESRCH); 7510 } 7511 /* Clear the value */ 7512 tcps->tcps_g_epriv_ports[i] = 0; 7513 mutex_exit(&tcps->tcps_epriv_port_lock); 7514 return (0); 7515 } 7516 7517 /* Return the TPI/TLI equivalent of our current tcp_state */ 7518 static int 7519 tcp_tpistate(tcp_t *tcp) 7520 { 7521 switch (tcp->tcp_state) { 7522 case TCPS_IDLE: 7523 return (TS_UNBND); 7524 case TCPS_LISTEN: 7525 /* 7526 * Return whether there are outstanding T_CONN_IND waiting 7527 * for the matching T_CONN_RES. Therefore don't count q0. 7528 */ 7529 if (tcp->tcp_conn_req_cnt_q > 0) 7530 return (TS_WRES_CIND); 7531 else 7532 return (TS_IDLE); 7533 case TCPS_BOUND: 7534 return (TS_IDLE); 7535 case TCPS_SYN_SENT: 7536 return (TS_WCON_CREQ); 7537 case TCPS_SYN_RCVD: 7538 /* 7539 * Note: assumption: this has to the active open SYN_RCVD. 7540 * The passive instance is detached in SYN_RCVD stage of 7541 * incoming connection processing so we cannot get request 7542 * for T_info_ack on it. 7543 */ 7544 return (TS_WACK_CRES); 7545 case TCPS_ESTABLISHED: 7546 return (TS_DATA_XFER); 7547 case TCPS_CLOSE_WAIT: 7548 return (TS_WREQ_ORDREL); 7549 case TCPS_FIN_WAIT_1: 7550 return (TS_WIND_ORDREL); 7551 case TCPS_FIN_WAIT_2: 7552 return (TS_WIND_ORDREL); 7553 7554 case TCPS_CLOSING: 7555 case TCPS_LAST_ACK: 7556 case TCPS_TIME_WAIT: 7557 case TCPS_CLOSED: 7558 /* 7559 * Following TS_WACK_DREQ7 is a rendition of "not 7560 * yet TS_IDLE" TPI state. There is no best match to any 7561 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7562 * choose a value chosen that will map to TLI/XTI level 7563 * state of TSTATECHNG (state is process of changing) which 7564 * captures what this dummy state represents. 7565 */ 7566 return (TS_WACK_DREQ7); 7567 default: 7568 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7569 tcp->tcp_state, tcp_display(tcp, NULL, 7570 DISP_PORT_ONLY)); 7571 return (TS_UNBND); 7572 } 7573 } 7574 7575 static void 7576 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7577 { 7578 tcp_stack_t *tcps = tcp->tcp_tcps; 7579 7580 if (tcp->tcp_family == AF_INET6) 7581 *tia = tcp_g_t_info_ack_v6; 7582 else 7583 *tia = tcp_g_t_info_ack; 7584 tia->CURRENT_state = tcp_tpistate(tcp); 7585 tia->OPT_size = tcp_max_optsize; 7586 if (tcp->tcp_mss == 0) { 7587 /* Not yet set - tcp_open does not set mss */ 7588 if (tcp->tcp_ipversion == IPV4_VERSION) 7589 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7590 else 7591 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7592 } else { 7593 tia->TIDU_size = tcp->tcp_mss; 7594 } 7595 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7596 } 7597 7598 /* 7599 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7600 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7601 * tcp_g_t_info_ack. The current state of the stream is copied from 7602 * tcp_state. 7603 */ 7604 static void 7605 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7606 { 7607 t_uscalar_t cap_bits1; 7608 struct T_capability_ack *tcap; 7609 7610 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7611 freemsg(mp); 7612 return; 7613 } 7614 7615 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7616 7617 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7618 mp->b_datap->db_type, T_CAPABILITY_ACK); 7619 if (mp == NULL) 7620 return; 7621 7622 tcap = (struct T_capability_ack *)mp->b_rptr; 7623 tcap->CAP_bits1 = 0; 7624 7625 if (cap_bits1 & TC1_INFO) { 7626 tcp_copy_info(&tcap->INFO_ack, tcp); 7627 tcap->CAP_bits1 |= TC1_INFO; 7628 } 7629 7630 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7631 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7632 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7633 } 7634 7635 putnext(tcp->tcp_rq, mp); 7636 } 7637 7638 /* 7639 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7640 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7641 * The current state of the stream is copied from tcp_state. 7642 */ 7643 static void 7644 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7645 { 7646 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7647 T_INFO_ACK); 7648 if (!mp) { 7649 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7650 return; 7651 } 7652 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7653 putnext(tcp->tcp_rq, mp); 7654 } 7655 7656 /* Respond to the TPI addr request */ 7657 static void 7658 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7659 { 7660 sin_t *sin; 7661 mblk_t *ackmp; 7662 struct T_addr_ack *taa; 7663 7664 /* Make it large enough for worst case */ 7665 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7666 2 * sizeof (sin6_t), 1); 7667 if (ackmp == NULL) { 7668 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7669 return; 7670 } 7671 7672 if (tcp->tcp_ipversion == IPV6_VERSION) { 7673 tcp_addr_req_ipv6(tcp, ackmp); 7674 return; 7675 } 7676 taa = (struct T_addr_ack *)ackmp->b_rptr; 7677 7678 bzero(taa, sizeof (struct T_addr_ack)); 7679 ackmp->b_wptr = (uchar_t *)&taa[1]; 7680 7681 taa->PRIM_type = T_ADDR_ACK; 7682 ackmp->b_datap->db_type = M_PCPROTO; 7683 7684 /* 7685 * Note: Following code assumes 32 bit alignment of basic 7686 * data structures like sin_t and struct T_addr_ack. 7687 */ 7688 if (tcp->tcp_state >= TCPS_BOUND) { 7689 /* 7690 * Fill in local address 7691 */ 7692 taa->LOCADDR_length = sizeof (sin_t); 7693 taa->LOCADDR_offset = sizeof (*taa); 7694 7695 sin = (sin_t *)&taa[1]; 7696 7697 /* Fill zeroes and then intialize non-zero fields */ 7698 *sin = sin_null; 7699 7700 sin->sin_family = AF_INET; 7701 7702 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7703 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7704 7705 ackmp->b_wptr = (uchar_t *)&sin[1]; 7706 7707 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7708 /* 7709 * Fill in Remote address 7710 */ 7711 taa->REMADDR_length = sizeof (sin_t); 7712 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7713 taa->LOCADDR_length); 7714 7715 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7716 *sin = sin_null; 7717 sin->sin_family = AF_INET; 7718 sin->sin_addr.s_addr = tcp->tcp_remote; 7719 sin->sin_port = tcp->tcp_fport; 7720 7721 ackmp->b_wptr = (uchar_t *)&sin[1]; 7722 } 7723 } 7724 putnext(tcp->tcp_rq, ackmp); 7725 } 7726 7727 /* Assumes that tcp_addr_req gets enough space and alignment */ 7728 static void 7729 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7730 { 7731 sin6_t *sin6; 7732 struct T_addr_ack *taa; 7733 7734 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7735 ASSERT(OK_32PTR(ackmp->b_rptr)); 7736 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7737 2 * sizeof (sin6_t)); 7738 7739 taa = (struct T_addr_ack *)ackmp->b_rptr; 7740 7741 bzero(taa, sizeof (struct T_addr_ack)); 7742 ackmp->b_wptr = (uchar_t *)&taa[1]; 7743 7744 taa->PRIM_type = T_ADDR_ACK; 7745 ackmp->b_datap->db_type = M_PCPROTO; 7746 7747 /* 7748 * Note: Following code assumes 32 bit alignment of basic 7749 * data structures like sin6_t and struct T_addr_ack. 7750 */ 7751 if (tcp->tcp_state >= TCPS_BOUND) { 7752 /* 7753 * Fill in local address 7754 */ 7755 taa->LOCADDR_length = sizeof (sin6_t); 7756 taa->LOCADDR_offset = sizeof (*taa); 7757 7758 sin6 = (sin6_t *)&taa[1]; 7759 *sin6 = sin6_null; 7760 7761 sin6->sin6_family = AF_INET6; 7762 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7763 sin6->sin6_port = tcp->tcp_lport; 7764 7765 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7766 7767 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7768 /* 7769 * Fill in Remote address 7770 */ 7771 taa->REMADDR_length = sizeof (sin6_t); 7772 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7773 taa->LOCADDR_length); 7774 7775 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7776 *sin6 = sin6_null; 7777 sin6->sin6_family = AF_INET6; 7778 sin6->sin6_flowinfo = 7779 tcp->tcp_ip6h->ip6_vcf & 7780 ~IPV6_VERS_AND_FLOW_MASK; 7781 sin6->sin6_addr = tcp->tcp_remote_v6; 7782 sin6->sin6_port = tcp->tcp_fport; 7783 7784 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7785 } 7786 } 7787 putnext(tcp->tcp_rq, ackmp); 7788 } 7789 7790 /* 7791 * Handle reinitialization of a tcp structure. 7792 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7793 */ 7794 static void 7795 tcp_reinit(tcp_t *tcp) 7796 { 7797 mblk_t *mp; 7798 int err; 7799 tcp_stack_t *tcps = tcp->tcp_tcps; 7800 7801 TCP_STAT(tcps, tcp_reinit_calls); 7802 7803 /* tcp_reinit should never be called for detached tcp_t's */ 7804 ASSERT(tcp->tcp_listener == NULL); 7805 ASSERT((tcp->tcp_family == AF_INET && 7806 tcp->tcp_ipversion == IPV4_VERSION) || 7807 (tcp->tcp_family == AF_INET6 && 7808 (tcp->tcp_ipversion == IPV4_VERSION || 7809 tcp->tcp_ipversion == IPV6_VERSION))); 7810 7811 /* Cancel outstanding timers */ 7812 tcp_timers_stop(tcp); 7813 7814 /* 7815 * Reset everything in the state vector, after updating global 7816 * MIB data from instance counters. 7817 */ 7818 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7819 tcp->tcp_ibsegs = 0; 7820 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7821 tcp->tcp_obsegs = 0; 7822 7823 tcp_close_mpp(&tcp->tcp_xmit_head); 7824 if (tcp->tcp_snd_zcopy_aware) 7825 tcp_zcopy_notify(tcp); 7826 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7827 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7828 mutex_enter(&tcp->tcp_non_sq_lock); 7829 if (tcp->tcp_flow_stopped && 7830 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7831 tcp_clrqfull(tcp); 7832 } 7833 mutex_exit(&tcp->tcp_non_sq_lock); 7834 tcp_close_mpp(&tcp->tcp_reass_head); 7835 tcp->tcp_reass_tail = NULL; 7836 if (tcp->tcp_rcv_list != NULL) { 7837 /* Free b_next chain */ 7838 tcp_close_mpp(&tcp->tcp_rcv_list); 7839 tcp->tcp_rcv_last_head = NULL; 7840 tcp->tcp_rcv_last_tail = NULL; 7841 tcp->tcp_rcv_cnt = 0; 7842 } 7843 tcp->tcp_rcv_last_tail = NULL; 7844 7845 if ((mp = tcp->tcp_urp_mp) != NULL) { 7846 freemsg(mp); 7847 tcp->tcp_urp_mp = NULL; 7848 } 7849 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7850 freemsg(mp); 7851 tcp->tcp_urp_mark_mp = NULL; 7852 } 7853 if (tcp->tcp_fused_sigurg_mp != NULL) { 7854 freeb(tcp->tcp_fused_sigurg_mp); 7855 tcp->tcp_fused_sigurg_mp = NULL; 7856 } 7857 7858 /* 7859 * Following is a union with two members which are 7860 * identical types and size so the following cleanup 7861 * is enough. 7862 */ 7863 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7864 7865 CL_INET_DISCONNECT(tcp); 7866 7867 /* 7868 * The connection can't be on the tcp_time_wait_head list 7869 * since it is not detached. 7870 */ 7871 ASSERT(tcp->tcp_time_wait_next == NULL); 7872 ASSERT(tcp->tcp_time_wait_prev == NULL); 7873 ASSERT(tcp->tcp_time_wait_expire == 0); 7874 7875 if (tcp->tcp_kssl_pending) { 7876 tcp->tcp_kssl_pending = B_FALSE; 7877 7878 /* Don't reset if the initialized by bind. */ 7879 if (tcp->tcp_kssl_ent != NULL) { 7880 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7881 KSSL_NO_PROXY); 7882 } 7883 } 7884 if (tcp->tcp_kssl_ctx != NULL) { 7885 kssl_release_ctx(tcp->tcp_kssl_ctx); 7886 tcp->tcp_kssl_ctx = NULL; 7887 } 7888 7889 /* 7890 * Reset/preserve other values 7891 */ 7892 tcp_reinit_values(tcp); 7893 ipcl_hash_remove(tcp->tcp_connp); 7894 conn_delete_ire(tcp->tcp_connp, NULL); 7895 tcp_ipsec_cleanup(tcp); 7896 7897 if (tcp->tcp_conn_req_max != 0) { 7898 /* 7899 * This is the case when a TLI program uses the same 7900 * transport end point to accept a connection. This 7901 * makes the TCP both a listener and acceptor. When 7902 * this connection is closed, we need to set the state 7903 * back to TCPS_LISTEN. Make sure that the eager list 7904 * is reinitialized. 7905 * 7906 * Note that this stream is still bound to the four 7907 * tuples of the previous connection in IP. If a new 7908 * SYN with different foreign address comes in, IP will 7909 * not find it and will send it to the global queue. In 7910 * the global queue, TCP will do a tcp_lookup_listener() 7911 * to find this stream. This works because this stream 7912 * is only removed from connected hash. 7913 * 7914 */ 7915 tcp->tcp_state = TCPS_LISTEN; 7916 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7917 tcp->tcp_eager_next_drop_q0 = tcp; 7918 tcp->tcp_eager_prev_drop_q0 = tcp; 7919 tcp->tcp_connp->conn_recv = tcp_conn_request; 7920 if (tcp->tcp_family == AF_INET6) { 7921 ASSERT(tcp->tcp_connp->conn_af_isv6); 7922 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7923 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7924 } else { 7925 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7926 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7927 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7928 } 7929 } else { 7930 tcp->tcp_state = TCPS_BOUND; 7931 } 7932 7933 /* 7934 * Initialize to default values 7935 * Can't fail since enough header template space already allocated 7936 * at open(). 7937 */ 7938 err = tcp_init_values(tcp); 7939 ASSERT(err == 0); 7940 /* Restore state in tcp_tcph */ 7941 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7942 if (tcp->tcp_ipversion == IPV4_VERSION) 7943 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7944 else 7945 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7946 /* 7947 * Copy of the src addr. in tcp_t is needed in tcp_t 7948 * since the lookup funcs can only lookup on tcp_t 7949 */ 7950 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7951 7952 ASSERT(tcp->tcp_ptpbhn != NULL); 7953 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7954 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7955 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7956 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7957 } 7958 7959 /* 7960 * Force values to zero that need be zero. 7961 * Do not touch values asociated with the BOUND or LISTEN state 7962 * since the connection will end up in that state after the reinit. 7963 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7964 * structure! 7965 */ 7966 static void 7967 tcp_reinit_values(tcp) 7968 tcp_t *tcp; 7969 { 7970 tcp_stack_t *tcps = tcp->tcp_tcps; 7971 7972 #ifndef lint 7973 #define DONTCARE(x) 7974 #define PRESERVE(x) 7975 #else 7976 #define DONTCARE(x) ((x) = (x)) 7977 #define PRESERVE(x) ((x) = (x)) 7978 #endif /* lint */ 7979 7980 PRESERVE(tcp->tcp_bind_hash); 7981 PRESERVE(tcp->tcp_ptpbhn); 7982 PRESERVE(tcp->tcp_acceptor_hash); 7983 PRESERVE(tcp->tcp_ptpahn); 7984 7985 /* Should be ASSERT NULL on these with new code! */ 7986 ASSERT(tcp->tcp_time_wait_next == NULL); 7987 ASSERT(tcp->tcp_time_wait_prev == NULL); 7988 ASSERT(tcp->tcp_time_wait_expire == 0); 7989 PRESERVE(tcp->tcp_state); 7990 PRESERVE(tcp->tcp_rq); 7991 PRESERVE(tcp->tcp_wq); 7992 7993 ASSERT(tcp->tcp_xmit_head == NULL); 7994 ASSERT(tcp->tcp_xmit_last == NULL); 7995 ASSERT(tcp->tcp_unsent == 0); 7996 ASSERT(tcp->tcp_xmit_tail == NULL); 7997 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7998 7999 tcp->tcp_snxt = 0; /* Displayed in mib */ 8000 tcp->tcp_suna = 0; /* Displayed in mib */ 8001 tcp->tcp_swnd = 0; 8002 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 8003 8004 ASSERT(tcp->tcp_ibsegs == 0); 8005 ASSERT(tcp->tcp_obsegs == 0); 8006 8007 if (tcp->tcp_iphc != NULL) { 8008 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8009 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 8010 } 8011 8012 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 8013 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 8014 DONTCARE(tcp->tcp_ipha); 8015 DONTCARE(tcp->tcp_ip6h); 8016 DONTCARE(tcp->tcp_ip_hdr_len); 8017 DONTCARE(tcp->tcp_tcph); 8018 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 8019 tcp->tcp_valid_bits = 0; 8020 8021 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 8022 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 8023 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 8024 tcp->tcp_last_rcv_lbolt = 0; 8025 8026 tcp->tcp_init_cwnd = 0; 8027 8028 tcp->tcp_urp_last_valid = 0; 8029 tcp->tcp_hard_binding = 0; 8030 tcp->tcp_hard_bound = 0; 8031 PRESERVE(tcp->tcp_cred); 8032 PRESERVE(tcp->tcp_cpid); 8033 PRESERVE(tcp->tcp_open_time); 8034 PRESERVE(tcp->tcp_exclbind); 8035 8036 tcp->tcp_fin_acked = 0; 8037 tcp->tcp_fin_rcvd = 0; 8038 tcp->tcp_fin_sent = 0; 8039 tcp->tcp_ordrel_done = 0; 8040 8041 tcp->tcp_debug = 0; 8042 tcp->tcp_dontroute = 0; 8043 tcp->tcp_broadcast = 0; 8044 8045 tcp->tcp_useloopback = 0; 8046 tcp->tcp_reuseaddr = 0; 8047 tcp->tcp_oobinline = 0; 8048 tcp->tcp_dgram_errind = 0; 8049 8050 tcp->tcp_detached = 0; 8051 tcp->tcp_bind_pending = 0; 8052 tcp->tcp_unbind_pending = 0; 8053 tcp->tcp_deferred_clean_death = 0; 8054 8055 tcp->tcp_snd_ws_ok = B_FALSE; 8056 tcp->tcp_snd_ts_ok = B_FALSE; 8057 tcp->tcp_linger = 0; 8058 tcp->tcp_ka_enabled = 0; 8059 tcp->tcp_zero_win_probe = 0; 8060 8061 tcp->tcp_loopback = 0; 8062 tcp->tcp_localnet = 0; 8063 tcp->tcp_syn_defense = 0; 8064 tcp->tcp_set_timer = 0; 8065 8066 tcp->tcp_active_open = 0; 8067 ASSERT(tcp->tcp_timeout == B_FALSE); 8068 tcp->tcp_rexmit = B_FALSE; 8069 tcp->tcp_xmit_zc_clean = B_FALSE; 8070 8071 tcp->tcp_snd_sack_ok = B_FALSE; 8072 PRESERVE(tcp->tcp_recvdstaddr); 8073 tcp->tcp_hwcksum = B_FALSE; 8074 8075 tcp->tcp_ire_ill_check_done = B_FALSE; 8076 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 8077 8078 tcp->tcp_mdt = B_FALSE; 8079 tcp->tcp_mdt_hdr_head = 0; 8080 tcp->tcp_mdt_hdr_tail = 0; 8081 8082 tcp->tcp_conn_def_q0 = 0; 8083 tcp->tcp_ip_forward_progress = B_FALSE; 8084 tcp->tcp_anon_priv_bind = 0; 8085 tcp->tcp_ecn_ok = B_FALSE; 8086 8087 tcp->tcp_cwr = B_FALSE; 8088 tcp->tcp_ecn_echo_on = B_FALSE; 8089 8090 if (tcp->tcp_sack_info != NULL) { 8091 if (tcp->tcp_notsack_list != NULL) { 8092 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 8093 } 8094 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 8095 tcp->tcp_sack_info = NULL; 8096 } 8097 8098 tcp->tcp_rcv_ws = 0; 8099 tcp->tcp_snd_ws = 0; 8100 tcp->tcp_ts_recent = 0; 8101 tcp->tcp_rnxt = 0; /* Displayed in mib */ 8102 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 8103 tcp->tcp_if_mtu = 0; 8104 8105 ASSERT(tcp->tcp_reass_head == NULL); 8106 ASSERT(tcp->tcp_reass_tail == NULL); 8107 8108 tcp->tcp_cwnd_cnt = 0; 8109 8110 ASSERT(tcp->tcp_rcv_list == NULL); 8111 ASSERT(tcp->tcp_rcv_last_head == NULL); 8112 ASSERT(tcp->tcp_rcv_last_tail == NULL); 8113 ASSERT(tcp->tcp_rcv_cnt == 0); 8114 8115 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 8116 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 8117 tcp->tcp_csuna = 0; 8118 8119 tcp->tcp_rto = 0; /* Displayed in MIB */ 8120 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 8121 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 8122 tcp->tcp_rtt_update = 0; 8123 8124 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8125 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8126 8127 tcp->tcp_rack = 0; /* Displayed in mib */ 8128 tcp->tcp_rack_cnt = 0; 8129 tcp->tcp_rack_cur_max = 0; 8130 tcp->tcp_rack_abs_max = 0; 8131 8132 tcp->tcp_max_swnd = 0; 8133 8134 ASSERT(tcp->tcp_listener == NULL); 8135 8136 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 8137 8138 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 8139 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 8140 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 8141 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 8142 8143 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 8144 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 8145 PRESERVE(tcp->tcp_conn_req_max); 8146 PRESERVE(tcp->tcp_conn_req_seqnum); 8147 8148 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 8149 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 8150 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 8151 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 8152 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 8153 8154 tcp->tcp_lingertime = 0; 8155 8156 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 8157 ASSERT(tcp->tcp_urp_mp == NULL); 8158 ASSERT(tcp->tcp_urp_mark_mp == NULL); 8159 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 8160 8161 ASSERT(tcp->tcp_eager_next_q == NULL); 8162 ASSERT(tcp->tcp_eager_last_q == NULL); 8163 ASSERT((tcp->tcp_eager_next_q0 == NULL && 8164 tcp->tcp_eager_prev_q0 == NULL) || 8165 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 8166 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 8167 8168 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 8169 tcp->tcp_eager_prev_drop_q0 == NULL) || 8170 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 8171 8172 tcp->tcp_client_errno = 0; 8173 8174 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 8175 8176 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 8177 8178 PRESERVE(tcp->tcp_bound_source_v6); 8179 tcp->tcp_last_sent_len = 0; 8180 tcp->tcp_dupack_cnt = 0; 8181 8182 tcp->tcp_fport = 0; /* Displayed in MIB */ 8183 PRESERVE(tcp->tcp_lport); 8184 8185 PRESERVE(tcp->tcp_acceptor_lockp); 8186 8187 ASSERT(tcp->tcp_ordrelid == 0); 8188 PRESERVE(tcp->tcp_acceptor_id); 8189 DONTCARE(tcp->tcp_ipsec_overhead); 8190 8191 /* 8192 * If tcp_tracing flag is ON (i.e. We have a trace buffer 8193 * in tcp structure and now tracing), Re-initialize all 8194 * members of tcp_traceinfo. 8195 */ 8196 if (tcp->tcp_tracebuf != NULL) { 8197 bzero(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 8198 } 8199 8200 PRESERVE(tcp->tcp_family); 8201 if (tcp->tcp_family == AF_INET6) { 8202 tcp->tcp_ipversion = IPV6_VERSION; 8203 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 8204 } else { 8205 tcp->tcp_ipversion = IPV4_VERSION; 8206 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 8207 } 8208 8209 tcp->tcp_bound_if = 0; 8210 tcp->tcp_ipv6_recvancillary = 0; 8211 tcp->tcp_recvifindex = 0; 8212 tcp->tcp_recvhops = 0; 8213 tcp->tcp_closed = 0; 8214 tcp->tcp_cleandeathtag = 0; 8215 if (tcp->tcp_hopopts != NULL) { 8216 mi_free(tcp->tcp_hopopts); 8217 tcp->tcp_hopopts = NULL; 8218 tcp->tcp_hopoptslen = 0; 8219 } 8220 ASSERT(tcp->tcp_hopoptslen == 0); 8221 if (tcp->tcp_dstopts != NULL) { 8222 mi_free(tcp->tcp_dstopts); 8223 tcp->tcp_dstopts = NULL; 8224 tcp->tcp_dstoptslen = 0; 8225 } 8226 ASSERT(tcp->tcp_dstoptslen == 0); 8227 if (tcp->tcp_rtdstopts != NULL) { 8228 mi_free(tcp->tcp_rtdstopts); 8229 tcp->tcp_rtdstopts = NULL; 8230 tcp->tcp_rtdstoptslen = 0; 8231 } 8232 ASSERT(tcp->tcp_rtdstoptslen == 0); 8233 if (tcp->tcp_rthdr != NULL) { 8234 mi_free(tcp->tcp_rthdr); 8235 tcp->tcp_rthdr = NULL; 8236 tcp->tcp_rthdrlen = 0; 8237 } 8238 ASSERT(tcp->tcp_rthdrlen == 0); 8239 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 8240 8241 /* Reset fusion-related fields */ 8242 tcp->tcp_fused = B_FALSE; 8243 tcp->tcp_unfusable = B_FALSE; 8244 tcp->tcp_fused_sigurg = B_FALSE; 8245 tcp->tcp_direct_sockfs = B_FALSE; 8246 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8247 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8248 tcp->tcp_loopback_peer = NULL; 8249 tcp->tcp_fuse_rcv_hiwater = 0; 8250 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8251 tcp->tcp_fuse_rcv_unread_cnt = 0; 8252 8253 tcp->tcp_lso = B_FALSE; 8254 8255 tcp->tcp_in_ack_unsent = 0; 8256 tcp->tcp_cork = B_FALSE; 8257 tcp->tcp_tconnind_started = B_FALSE; 8258 8259 PRESERVE(tcp->tcp_squeue_bytes); 8260 8261 ASSERT(tcp->tcp_kssl_ctx == NULL); 8262 ASSERT(!tcp->tcp_kssl_pending); 8263 PRESERVE(tcp->tcp_kssl_ent); 8264 8265 /* Sodirect */ 8266 tcp->tcp_sodirect = NULL; 8267 8268 tcp->tcp_closemp_used = B_FALSE; 8269 8270 #ifdef DEBUG 8271 DONTCARE(tcp->tcmp_stk[0]); 8272 #endif 8273 8274 8275 #undef DONTCARE 8276 #undef PRESERVE 8277 } 8278 8279 /* 8280 * Allocate necessary resources and initialize state vector. 8281 * Guaranteed not to fail so that when an error is returned, 8282 * the caller doesn't need to do any additional cleanup. 8283 */ 8284 int 8285 tcp_init(tcp_t *tcp, queue_t *q) 8286 { 8287 int err; 8288 8289 tcp->tcp_rq = q; 8290 tcp->tcp_wq = WR(q); 8291 tcp->tcp_state = TCPS_IDLE; 8292 if ((err = tcp_init_values(tcp)) != 0) 8293 tcp_timers_stop(tcp); 8294 return (err); 8295 } 8296 8297 static int 8298 tcp_init_values(tcp_t *tcp) 8299 { 8300 int err; 8301 tcp_stack_t *tcps = tcp->tcp_tcps; 8302 8303 ASSERT((tcp->tcp_family == AF_INET && 8304 tcp->tcp_ipversion == IPV4_VERSION) || 8305 (tcp->tcp_family == AF_INET6 && 8306 (tcp->tcp_ipversion == IPV4_VERSION || 8307 tcp->tcp_ipversion == IPV6_VERSION))); 8308 8309 /* 8310 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 8311 * will be close to tcp_rexmit_interval_initial. By doing this, we 8312 * allow the algorithm to adjust slowly to large fluctuations of RTT 8313 * during first few transmissions of a connection as seen in slow 8314 * links. 8315 */ 8316 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 8317 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 8318 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 8319 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 8320 tcps->tcps_conn_grace_period; 8321 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 8322 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 8323 tcp->tcp_timer_backoff = 0; 8324 tcp->tcp_ms_we_have_waited = 0; 8325 tcp->tcp_last_recv_time = lbolt; 8326 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 8327 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 8328 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 8329 8330 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 8331 8332 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 8333 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 8334 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 8335 /* 8336 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 8337 * passive open. 8338 */ 8339 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 8340 8341 tcp->tcp_naglim = tcps->tcps_naglim_def; 8342 8343 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 8344 8345 tcp->tcp_mdt_hdr_head = 0; 8346 tcp->tcp_mdt_hdr_tail = 0; 8347 8348 /* Reset fusion-related fields */ 8349 tcp->tcp_fused = B_FALSE; 8350 tcp->tcp_unfusable = B_FALSE; 8351 tcp->tcp_fused_sigurg = B_FALSE; 8352 tcp->tcp_direct_sockfs = B_FALSE; 8353 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8354 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8355 tcp->tcp_loopback_peer = NULL; 8356 tcp->tcp_fuse_rcv_hiwater = 0; 8357 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8358 tcp->tcp_fuse_rcv_unread_cnt = 0; 8359 8360 /* Sodirect */ 8361 tcp->tcp_sodirect = NULL; 8362 8363 /* Initialize the header template */ 8364 if (tcp->tcp_ipversion == IPV4_VERSION) { 8365 err = tcp_header_init_ipv4(tcp); 8366 } else { 8367 err = tcp_header_init_ipv6(tcp); 8368 } 8369 if (err) 8370 return (err); 8371 8372 /* 8373 * Init the window scale to the max so tcp_rwnd_set() won't pare 8374 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8375 */ 8376 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8377 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8378 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8379 8380 tcp->tcp_cork = B_FALSE; 8381 /* 8382 * Init the tcp_debug option. This value determines whether TCP 8383 * calls strlog() to print out debug messages. Doing this 8384 * initialization here means that this value is not inherited thru 8385 * tcp_reinit(). 8386 */ 8387 tcp->tcp_debug = tcps->tcps_dbg; 8388 8389 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8390 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8391 8392 return (0); 8393 } 8394 8395 /* 8396 * Initialize the IPv4 header. Loses any record of any IP options. 8397 */ 8398 static int 8399 tcp_header_init_ipv4(tcp_t *tcp) 8400 { 8401 tcph_t *tcph; 8402 uint32_t sum; 8403 conn_t *connp; 8404 tcp_stack_t *tcps = tcp->tcp_tcps; 8405 8406 /* 8407 * This is a simple initialization. If there's 8408 * already a template, it should never be too small, 8409 * so reuse it. Otherwise, allocate space for the new one. 8410 */ 8411 if (tcp->tcp_iphc == NULL) { 8412 ASSERT(tcp->tcp_iphc_len == 0); 8413 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8414 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8415 if (tcp->tcp_iphc == NULL) { 8416 tcp->tcp_iphc_len = 0; 8417 return (ENOMEM); 8418 } 8419 } 8420 8421 /* options are gone; may need a new label */ 8422 connp = tcp->tcp_connp; 8423 connp->conn_mlp_type = mlptSingle; 8424 connp->conn_ulp_labeled = !is_system_labeled(); 8425 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8426 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8427 tcp->tcp_ip6h = NULL; 8428 tcp->tcp_ipversion = IPV4_VERSION; 8429 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8430 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8431 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8432 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8433 tcp->tcp_ipha->ipha_version_and_hdr_length 8434 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8435 tcp->tcp_ipha->ipha_ident = 0; 8436 8437 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8438 tcp->tcp_tos = 0; 8439 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8440 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8441 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8442 8443 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8444 tcp->tcp_tcph = tcph; 8445 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8446 /* 8447 * IP wants our header length in the checksum field to 8448 * allow it to perform a single pseudo-header+checksum 8449 * calculation on behalf of TCP. 8450 * Include the adjustment for a source route once IP_OPTIONS is set. 8451 */ 8452 sum = sizeof (tcph_t) + tcp->tcp_sum; 8453 sum = (sum >> 16) + (sum & 0xFFFF); 8454 U16_TO_ABE16(sum, tcph->th_sum); 8455 return (0); 8456 } 8457 8458 /* 8459 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8460 */ 8461 static int 8462 tcp_header_init_ipv6(tcp_t *tcp) 8463 { 8464 tcph_t *tcph; 8465 uint32_t sum; 8466 conn_t *connp; 8467 tcp_stack_t *tcps = tcp->tcp_tcps; 8468 8469 /* 8470 * This is a simple initialization. If there's 8471 * already a template, it should never be too small, 8472 * so reuse it. Otherwise, allocate space for the new one. 8473 * Ensure that there is enough space to "downgrade" the tcp_t 8474 * to an IPv4 tcp_t. This requires having space for a full load 8475 * of IPv4 options, as well as a full load of TCP options 8476 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8477 * than a v6 header and a TCP header with a full load of TCP options 8478 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8479 * We want to avoid reallocation in the "downgraded" case when 8480 * processing outbound IPv4 options. 8481 */ 8482 if (tcp->tcp_iphc == NULL) { 8483 ASSERT(tcp->tcp_iphc_len == 0); 8484 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8485 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8486 if (tcp->tcp_iphc == NULL) { 8487 tcp->tcp_iphc_len = 0; 8488 return (ENOMEM); 8489 } 8490 } 8491 8492 /* options are gone; may need a new label */ 8493 connp = tcp->tcp_connp; 8494 connp->conn_mlp_type = mlptSingle; 8495 connp->conn_ulp_labeled = !is_system_labeled(); 8496 8497 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8498 tcp->tcp_ipversion = IPV6_VERSION; 8499 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8500 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8501 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8502 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8503 tcp->tcp_ipha = NULL; 8504 8505 /* Initialize the header template */ 8506 8507 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8508 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8509 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8510 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8511 8512 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8513 tcp->tcp_tcph = tcph; 8514 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8515 /* 8516 * IP wants our header length in the checksum field to 8517 * allow it to perform a single psuedo-header+checksum 8518 * calculation on behalf of TCP. 8519 * Include the adjustment for a source route when IPV6_RTHDR is set. 8520 */ 8521 sum = sizeof (tcph_t) + tcp->tcp_sum; 8522 sum = (sum >> 16) + (sum & 0xFFFF); 8523 U16_TO_ABE16(sum, tcph->th_sum); 8524 return (0); 8525 } 8526 8527 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8528 #define ICMP_MIN_TCP_HDR 8 8529 8530 /* 8531 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8532 * passed up by IP. The message is always received on the correct tcp_t. 8533 * Assumes that IP has pulled up everything up to and including the ICMP header. 8534 */ 8535 void 8536 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8537 { 8538 icmph_t *icmph; 8539 ipha_t *ipha; 8540 int iph_hdr_length; 8541 tcph_t *tcph; 8542 boolean_t ipsec_mctl = B_FALSE; 8543 boolean_t secure; 8544 mblk_t *first_mp = mp; 8545 uint32_t new_mss; 8546 uint32_t ratio; 8547 size_t mp_size = MBLKL(mp); 8548 uint32_t seg_seq; 8549 tcp_stack_t *tcps = tcp->tcp_tcps; 8550 8551 /* Assume IP provides aligned packets - otherwise toss */ 8552 if (!OK_32PTR(mp->b_rptr)) { 8553 freemsg(mp); 8554 return; 8555 } 8556 8557 /* 8558 * Since ICMP errors are normal data marked with M_CTL when sent 8559 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8560 * packets starting with an ipsec_info_t, see ipsec_info.h. 8561 */ 8562 if ((mp_size == sizeof (ipsec_info_t)) && 8563 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8564 ASSERT(mp->b_cont != NULL); 8565 mp = mp->b_cont; 8566 /* IP should have done this */ 8567 ASSERT(OK_32PTR(mp->b_rptr)); 8568 mp_size = MBLKL(mp); 8569 ipsec_mctl = B_TRUE; 8570 } 8571 8572 /* 8573 * Verify that we have a complete outer IP header. If not, drop it. 8574 */ 8575 if (mp_size < sizeof (ipha_t)) { 8576 noticmpv4: 8577 freemsg(first_mp); 8578 return; 8579 } 8580 8581 ipha = (ipha_t *)mp->b_rptr; 8582 /* 8583 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8584 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8585 */ 8586 switch (IPH_HDR_VERSION(ipha)) { 8587 case IPV6_VERSION: 8588 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8589 return; 8590 case IPV4_VERSION: 8591 break; 8592 default: 8593 goto noticmpv4; 8594 } 8595 8596 /* Skip past the outer IP and ICMP headers */ 8597 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8598 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8599 /* 8600 * If we don't have the correct outer IP header length or if the ULP 8601 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8602 * send it upstream. 8603 */ 8604 if (iph_hdr_length < sizeof (ipha_t) || 8605 ipha->ipha_protocol != IPPROTO_ICMP || 8606 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8607 goto noticmpv4; 8608 } 8609 ipha = (ipha_t *)&icmph[1]; 8610 8611 /* Skip past the inner IP and find the ULP header */ 8612 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8613 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8614 /* 8615 * If we don't have the correct inner IP header length or if the ULP 8616 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8617 * bytes of TCP header, drop it. 8618 */ 8619 if (iph_hdr_length < sizeof (ipha_t) || 8620 ipha->ipha_protocol != IPPROTO_TCP || 8621 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8622 goto noticmpv4; 8623 } 8624 8625 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8626 if (ipsec_mctl) { 8627 secure = ipsec_in_is_secure(first_mp); 8628 } else { 8629 secure = B_FALSE; 8630 } 8631 if (secure) { 8632 /* 8633 * If we are willing to accept this in clear 8634 * we don't have to verify policy. 8635 */ 8636 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8637 if (!tcp_check_policy(tcp, first_mp, 8638 ipha, NULL, secure, ipsec_mctl)) { 8639 /* 8640 * tcp_check_policy called 8641 * ip_drop_packet() on failure. 8642 */ 8643 return; 8644 } 8645 } 8646 } 8647 } else if (ipsec_mctl) { 8648 /* 8649 * This is a hard_bound connection. IP has already 8650 * verified policy. We don't have to do it again. 8651 */ 8652 freeb(first_mp); 8653 first_mp = mp; 8654 ipsec_mctl = B_FALSE; 8655 } 8656 8657 seg_seq = ABE32_TO_U32(tcph->th_seq); 8658 /* 8659 * TCP SHOULD check that the TCP sequence number contained in 8660 * payload of the ICMP error message is within the range 8661 * SND.UNA <= SEG.SEQ < SND.NXT. 8662 */ 8663 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8664 /* 8665 * If the ICMP message is bogus, should we kill the 8666 * connection, or should we just drop the bogus ICMP 8667 * message? It would probably make more sense to just 8668 * drop the message so that if this one managed to get 8669 * in, the real connection should not suffer. 8670 */ 8671 goto noticmpv4; 8672 } 8673 8674 switch (icmph->icmph_type) { 8675 case ICMP_DEST_UNREACHABLE: 8676 switch (icmph->icmph_code) { 8677 case ICMP_FRAGMENTATION_NEEDED: 8678 /* 8679 * Reduce the MSS based on the new MTU. This will 8680 * eliminate any fragmentation locally. 8681 * N.B. There may well be some funny side-effects on 8682 * the local send policy and the remote receive policy. 8683 * Pending further research, we provide 8684 * tcp_ignore_path_mtu just in case this proves 8685 * disastrous somewhere. 8686 * 8687 * After updating the MSS, retransmit part of the 8688 * dropped segment using the new mss by calling 8689 * tcp_wput_data(). Need to adjust all those 8690 * params to make sure tcp_wput_data() work properly. 8691 */ 8692 if (tcps->tcps_ignore_path_mtu) 8693 break; 8694 8695 /* 8696 * Decrease the MSS by time stamp options 8697 * IP options and IPSEC options. tcp_hdr_len 8698 * includes time stamp option and IP option 8699 * length. 8700 */ 8701 8702 new_mss = ntohs(icmph->icmph_du_mtu) - 8703 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8704 8705 /* 8706 * Only update the MSS if the new one is 8707 * smaller than the previous one. This is 8708 * to avoid problems when getting multiple 8709 * ICMP errors for the same MTU. 8710 */ 8711 if (new_mss >= tcp->tcp_mss) 8712 break; 8713 8714 /* 8715 * Stop doing PMTU if new_mss is less than 68 8716 * or less than tcp_mss_min. 8717 * The value 68 comes from rfc 1191. 8718 */ 8719 if (new_mss < MAX(68, tcps->tcps_mss_min)) 8720 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8721 0; 8722 8723 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8724 ASSERT(ratio >= 1); 8725 tcp_mss_set(tcp, new_mss, B_TRUE); 8726 8727 /* 8728 * Make sure we have something to 8729 * send. 8730 */ 8731 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8732 (tcp->tcp_xmit_head != NULL)) { 8733 /* 8734 * Shrink tcp_cwnd in 8735 * proportion to the old MSS/new MSS. 8736 */ 8737 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8738 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8739 (tcp->tcp_unsent == 0)) { 8740 tcp->tcp_rexmit_max = tcp->tcp_fss; 8741 } else { 8742 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8743 } 8744 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8745 tcp->tcp_rexmit = B_TRUE; 8746 tcp->tcp_dupack_cnt = 0; 8747 tcp->tcp_snd_burst = TCP_CWND_SS; 8748 tcp_ss_rexmit(tcp); 8749 } 8750 break; 8751 case ICMP_PORT_UNREACHABLE: 8752 case ICMP_PROTOCOL_UNREACHABLE: 8753 switch (tcp->tcp_state) { 8754 case TCPS_SYN_SENT: 8755 case TCPS_SYN_RCVD: 8756 /* 8757 * ICMP can snipe away incipient 8758 * TCP connections as long as 8759 * seq number is same as initial 8760 * send seq number. 8761 */ 8762 if (seg_seq == tcp->tcp_iss) { 8763 (void) tcp_clean_death(tcp, 8764 ECONNREFUSED, 6); 8765 } 8766 break; 8767 } 8768 break; 8769 case ICMP_HOST_UNREACHABLE: 8770 case ICMP_NET_UNREACHABLE: 8771 /* Record the error in case we finally time out. */ 8772 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8773 tcp->tcp_client_errno = EHOSTUNREACH; 8774 else 8775 tcp->tcp_client_errno = ENETUNREACH; 8776 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8777 if (tcp->tcp_listener != NULL && 8778 tcp->tcp_listener->tcp_syn_defense) { 8779 /* 8780 * Ditch the half-open connection if we 8781 * suspect a SYN attack is under way. 8782 */ 8783 tcp_ip_ire_mark_advice(tcp); 8784 (void) tcp_clean_death(tcp, 8785 tcp->tcp_client_errno, 7); 8786 } 8787 } 8788 break; 8789 default: 8790 break; 8791 } 8792 break; 8793 case ICMP_SOURCE_QUENCH: { 8794 /* 8795 * use a global boolean to control 8796 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8797 * The default is false. 8798 */ 8799 if (tcp_icmp_source_quench) { 8800 /* 8801 * Reduce the sending rate as if we got a 8802 * retransmit timeout 8803 */ 8804 uint32_t npkt; 8805 8806 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8807 tcp->tcp_mss; 8808 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8809 tcp->tcp_cwnd = tcp->tcp_mss; 8810 tcp->tcp_cwnd_cnt = 0; 8811 } 8812 break; 8813 } 8814 } 8815 freemsg(first_mp); 8816 } 8817 8818 /* 8819 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8820 * error messages passed up by IP. 8821 * Assumes that IP has pulled up all the extension headers as well 8822 * as the ICMPv6 header. 8823 */ 8824 static void 8825 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8826 { 8827 icmp6_t *icmp6; 8828 ip6_t *ip6h; 8829 uint16_t iph_hdr_length; 8830 tcpha_t *tcpha; 8831 uint8_t *nexthdrp; 8832 uint32_t new_mss; 8833 uint32_t ratio; 8834 boolean_t secure; 8835 mblk_t *first_mp = mp; 8836 size_t mp_size; 8837 uint32_t seg_seq; 8838 tcp_stack_t *tcps = tcp->tcp_tcps; 8839 8840 /* 8841 * The caller has determined if this is an IPSEC_IN packet and 8842 * set ipsec_mctl appropriately (see tcp_icmp_error). 8843 */ 8844 if (ipsec_mctl) 8845 mp = mp->b_cont; 8846 8847 mp_size = MBLKL(mp); 8848 8849 /* 8850 * Verify that we have a complete IP header. If not, send it upstream. 8851 */ 8852 if (mp_size < sizeof (ip6_t)) { 8853 noticmpv6: 8854 freemsg(first_mp); 8855 return; 8856 } 8857 8858 /* 8859 * Verify this is an ICMPV6 packet, else send it upstream. 8860 */ 8861 ip6h = (ip6_t *)mp->b_rptr; 8862 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8863 iph_hdr_length = IPV6_HDR_LEN; 8864 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8865 &nexthdrp) || 8866 *nexthdrp != IPPROTO_ICMPV6) { 8867 goto noticmpv6; 8868 } 8869 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8870 ip6h = (ip6_t *)&icmp6[1]; 8871 /* 8872 * Verify if we have a complete ICMP and inner IP header. 8873 */ 8874 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8875 goto noticmpv6; 8876 8877 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8878 goto noticmpv6; 8879 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8880 /* 8881 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8882 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8883 * packet. 8884 */ 8885 if ((*nexthdrp != IPPROTO_TCP) || 8886 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8887 goto noticmpv6; 8888 } 8889 8890 /* 8891 * ICMP errors come on the right queue or come on 8892 * listener/global queue for detached connections and 8893 * get switched to the right queue. If it comes on the 8894 * right queue, policy check has already been done by IP 8895 * and thus free the first_mp without verifying the policy. 8896 * If it has come for a non-hard bound connection, we need 8897 * to verify policy as IP may not have done it. 8898 */ 8899 if (!tcp->tcp_hard_bound) { 8900 if (ipsec_mctl) { 8901 secure = ipsec_in_is_secure(first_mp); 8902 } else { 8903 secure = B_FALSE; 8904 } 8905 if (secure) { 8906 /* 8907 * If we are willing to accept this in clear 8908 * we don't have to verify policy. 8909 */ 8910 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8911 if (!tcp_check_policy(tcp, first_mp, 8912 NULL, ip6h, secure, ipsec_mctl)) { 8913 /* 8914 * tcp_check_policy called 8915 * ip_drop_packet() on failure. 8916 */ 8917 return; 8918 } 8919 } 8920 } 8921 } else if (ipsec_mctl) { 8922 /* 8923 * This is a hard_bound connection. IP has already 8924 * verified policy. We don't have to do it again. 8925 */ 8926 freeb(first_mp); 8927 first_mp = mp; 8928 ipsec_mctl = B_FALSE; 8929 } 8930 8931 seg_seq = ntohl(tcpha->tha_seq); 8932 /* 8933 * TCP SHOULD check that the TCP sequence number contained in 8934 * payload of the ICMP error message is within the range 8935 * SND.UNA <= SEG.SEQ < SND.NXT. 8936 */ 8937 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8938 /* 8939 * If the ICMP message is bogus, should we kill the 8940 * connection, or should we just drop the bogus ICMP 8941 * message? It would probably make more sense to just 8942 * drop the message so that if this one managed to get 8943 * in, the real connection should not suffer. 8944 */ 8945 goto noticmpv6; 8946 } 8947 8948 switch (icmp6->icmp6_type) { 8949 case ICMP6_PACKET_TOO_BIG: 8950 /* 8951 * Reduce the MSS based on the new MTU. This will 8952 * eliminate any fragmentation locally. 8953 * N.B. There may well be some funny side-effects on 8954 * the local send policy and the remote receive policy. 8955 * Pending further research, we provide 8956 * tcp_ignore_path_mtu just in case this proves 8957 * disastrous somewhere. 8958 * 8959 * After updating the MSS, retransmit part of the 8960 * dropped segment using the new mss by calling 8961 * tcp_wput_data(). Need to adjust all those 8962 * params to make sure tcp_wput_data() work properly. 8963 */ 8964 if (tcps->tcps_ignore_path_mtu) 8965 break; 8966 8967 /* 8968 * Decrease the MSS by time stamp options 8969 * IP options and IPSEC options. tcp_hdr_len 8970 * includes time stamp option and IP option 8971 * length. 8972 */ 8973 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8974 tcp->tcp_ipsec_overhead; 8975 8976 /* 8977 * Only update the MSS if the new one is 8978 * smaller than the previous one. This is 8979 * to avoid problems when getting multiple 8980 * ICMP errors for the same MTU. 8981 */ 8982 if (new_mss >= tcp->tcp_mss) 8983 break; 8984 8985 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8986 ASSERT(ratio >= 1); 8987 tcp_mss_set(tcp, new_mss, B_TRUE); 8988 8989 /* 8990 * Make sure we have something to 8991 * send. 8992 */ 8993 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8994 (tcp->tcp_xmit_head != NULL)) { 8995 /* 8996 * Shrink tcp_cwnd in 8997 * proportion to the old MSS/new MSS. 8998 */ 8999 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 9000 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 9001 (tcp->tcp_unsent == 0)) { 9002 tcp->tcp_rexmit_max = tcp->tcp_fss; 9003 } else { 9004 tcp->tcp_rexmit_max = tcp->tcp_snxt; 9005 } 9006 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 9007 tcp->tcp_rexmit = B_TRUE; 9008 tcp->tcp_dupack_cnt = 0; 9009 tcp->tcp_snd_burst = TCP_CWND_SS; 9010 tcp_ss_rexmit(tcp); 9011 } 9012 break; 9013 9014 case ICMP6_DST_UNREACH: 9015 switch (icmp6->icmp6_code) { 9016 case ICMP6_DST_UNREACH_NOPORT: 9017 if (((tcp->tcp_state == TCPS_SYN_SENT) || 9018 (tcp->tcp_state == TCPS_SYN_RCVD)) && 9019 (seg_seq == tcp->tcp_iss)) { 9020 (void) tcp_clean_death(tcp, 9021 ECONNREFUSED, 8); 9022 } 9023 break; 9024 9025 case ICMP6_DST_UNREACH_ADMIN: 9026 case ICMP6_DST_UNREACH_NOROUTE: 9027 case ICMP6_DST_UNREACH_BEYONDSCOPE: 9028 case ICMP6_DST_UNREACH_ADDR: 9029 /* Record the error in case we finally time out. */ 9030 tcp->tcp_client_errno = EHOSTUNREACH; 9031 if (((tcp->tcp_state == TCPS_SYN_SENT) || 9032 (tcp->tcp_state == TCPS_SYN_RCVD)) && 9033 (seg_seq == tcp->tcp_iss)) { 9034 if (tcp->tcp_listener != NULL && 9035 tcp->tcp_listener->tcp_syn_defense) { 9036 /* 9037 * Ditch the half-open connection if we 9038 * suspect a SYN attack is under way. 9039 */ 9040 tcp_ip_ire_mark_advice(tcp); 9041 (void) tcp_clean_death(tcp, 9042 tcp->tcp_client_errno, 9); 9043 } 9044 } 9045 9046 9047 break; 9048 default: 9049 break; 9050 } 9051 break; 9052 9053 case ICMP6_PARAM_PROB: 9054 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 9055 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 9056 (uchar_t *)ip6h + icmp6->icmp6_pptr == 9057 (uchar_t *)nexthdrp) { 9058 if (tcp->tcp_state == TCPS_SYN_SENT || 9059 tcp->tcp_state == TCPS_SYN_RCVD) { 9060 (void) tcp_clean_death(tcp, 9061 ECONNREFUSED, 10); 9062 } 9063 break; 9064 } 9065 break; 9066 9067 case ICMP6_TIME_EXCEEDED: 9068 default: 9069 break; 9070 } 9071 freemsg(first_mp); 9072 } 9073 9074 /* 9075 * IP recognizes seven kinds of bind requests: 9076 * 9077 * - A zero-length address binds only to the protocol number. 9078 * 9079 * - A 4-byte address is treated as a request to 9080 * validate that the address is a valid local IPv4 9081 * address, appropriate for an application to bind to. 9082 * IP does the verification, but does not make any note 9083 * of the address at this time. 9084 * 9085 * - A 16-byte address contains is treated as a request 9086 * to validate a local IPv6 address, as the 4-byte 9087 * address case above. 9088 * 9089 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 9090 * use it for the inbound fanout of packets. 9091 * 9092 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 9093 * use it for the inbound fanout of packets. 9094 * 9095 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 9096 * information consisting of local and remote addresses 9097 * and ports. In this case, the addresses are both 9098 * validated as appropriate for this operation, and, if 9099 * so, the information is retained for use in the 9100 * inbound fanout. 9101 * 9102 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 9103 * fanout information, like the 12-byte case above. 9104 * 9105 * IP will also fill in the IRE request mblk with information 9106 * regarding our peer. In all cases, we notify IP of our protocol 9107 * type by appending a single protocol byte to the bind request. 9108 */ 9109 static mblk_t * 9110 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 9111 { 9112 char *cp; 9113 mblk_t *mp; 9114 struct T_bind_req *tbr; 9115 ipa_conn_t *ac; 9116 ipa6_conn_t *ac6; 9117 sin_t *sin; 9118 sin6_t *sin6; 9119 9120 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 9121 ASSERT((tcp->tcp_family == AF_INET && 9122 tcp->tcp_ipversion == IPV4_VERSION) || 9123 (tcp->tcp_family == AF_INET6 && 9124 (tcp->tcp_ipversion == IPV4_VERSION || 9125 tcp->tcp_ipversion == IPV6_VERSION))); 9126 9127 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 9128 if (!mp) 9129 return (mp); 9130 mp->b_datap->db_type = M_PROTO; 9131 tbr = (struct T_bind_req *)mp->b_rptr; 9132 tbr->PRIM_type = bind_prim; 9133 tbr->ADDR_offset = sizeof (*tbr); 9134 tbr->CONIND_number = 0; 9135 tbr->ADDR_length = addr_length; 9136 cp = (char *)&tbr[1]; 9137 switch (addr_length) { 9138 case sizeof (ipa_conn_t): 9139 ASSERT(tcp->tcp_family == AF_INET); 9140 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9141 9142 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9143 if (mp->b_cont == NULL) { 9144 freemsg(mp); 9145 return (NULL); 9146 } 9147 mp->b_cont->b_wptr += sizeof (ire_t); 9148 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9149 9150 /* cp known to be 32 bit aligned */ 9151 ac = (ipa_conn_t *)cp; 9152 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 9153 ac->ac_faddr = tcp->tcp_remote; 9154 ac->ac_fport = tcp->tcp_fport; 9155 ac->ac_lport = tcp->tcp_lport; 9156 tcp->tcp_hard_binding = 1; 9157 break; 9158 9159 case sizeof (ipa6_conn_t): 9160 ASSERT(tcp->tcp_family == AF_INET6); 9161 9162 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9163 if (mp->b_cont == NULL) { 9164 freemsg(mp); 9165 return (NULL); 9166 } 9167 mp->b_cont->b_wptr += sizeof (ire_t); 9168 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9169 9170 /* cp known to be 32 bit aligned */ 9171 ac6 = (ipa6_conn_t *)cp; 9172 if (tcp->tcp_ipversion == IPV4_VERSION) { 9173 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 9174 &ac6->ac6_laddr); 9175 } else { 9176 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 9177 } 9178 ac6->ac6_faddr = tcp->tcp_remote_v6; 9179 ac6->ac6_fport = tcp->tcp_fport; 9180 ac6->ac6_lport = tcp->tcp_lport; 9181 tcp->tcp_hard_binding = 1; 9182 break; 9183 9184 case sizeof (sin_t): 9185 /* 9186 * NOTE: IPV6_ADDR_LEN also has same size. 9187 * Use family to discriminate. 9188 */ 9189 if (tcp->tcp_family == AF_INET) { 9190 sin = (sin_t *)cp; 9191 9192 *sin = sin_null; 9193 sin->sin_family = AF_INET; 9194 sin->sin_addr.s_addr = tcp->tcp_bound_source; 9195 sin->sin_port = tcp->tcp_lport; 9196 break; 9197 } else { 9198 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 9199 } 9200 break; 9201 9202 case sizeof (sin6_t): 9203 ASSERT(tcp->tcp_family == AF_INET6); 9204 sin6 = (sin6_t *)cp; 9205 9206 *sin6 = sin6_null; 9207 sin6->sin6_family = AF_INET6; 9208 sin6->sin6_addr = tcp->tcp_bound_source_v6; 9209 sin6->sin6_port = tcp->tcp_lport; 9210 break; 9211 9212 case IP_ADDR_LEN: 9213 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9214 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 9215 break; 9216 9217 } 9218 /* Add protocol number to end */ 9219 cp[addr_length] = (char)IPPROTO_TCP; 9220 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 9221 return (mp); 9222 } 9223 9224 /* 9225 * Notify IP that we are having trouble with this connection. IP should 9226 * blow the IRE away and start over. 9227 */ 9228 static void 9229 tcp_ip_notify(tcp_t *tcp) 9230 { 9231 struct iocblk *iocp; 9232 ipid_t *ipid; 9233 mblk_t *mp; 9234 9235 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 9236 if (tcp->tcp_ipversion == IPV6_VERSION) 9237 return; 9238 9239 mp = mkiocb(IP_IOCTL); 9240 if (mp == NULL) 9241 return; 9242 9243 iocp = (struct iocblk *)mp->b_rptr; 9244 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 9245 9246 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 9247 if (!mp->b_cont) { 9248 freeb(mp); 9249 return; 9250 } 9251 9252 ipid = (ipid_t *)mp->b_cont->b_rptr; 9253 mp->b_cont->b_wptr += iocp->ioc_count; 9254 bzero(ipid, sizeof (*ipid)); 9255 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 9256 ipid->ipid_ire_type = IRE_CACHE; 9257 ipid->ipid_addr_offset = sizeof (ipid_t); 9258 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 9259 /* 9260 * Note: in the case of source routing we want to blow away the 9261 * route to the first source route hop. 9262 */ 9263 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 9264 sizeof (tcp->tcp_ipha->ipha_dst)); 9265 9266 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 9267 } 9268 9269 /* Unlink and return any mblk that looks like it contains an ire */ 9270 static mblk_t * 9271 tcp_ire_mp(mblk_t *mp) 9272 { 9273 mblk_t *prev_mp; 9274 9275 for (;;) { 9276 prev_mp = mp; 9277 mp = mp->b_cont; 9278 if (mp == NULL) 9279 break; 9280 switch (DB_TYPE(mp)) { 9281 case IRE_DB_TYPE: 9282 case IRE_DB_REQ_TYPE: 9283 if (prev_mp != NULL) 9284 prev_mp->b_cont = mp->b_cont; 9285 mp->b_cont = NULL; 9286 return (mp); 9287 default: 9288 break; 9289 } 9290 } 9291 return (mp); 9292 } 9293 9294 /* 9295 * Timer callback routine for keepalive probe. We do a fake resend of 9296 * last ACKed byte. Then set a timer using RTO. When the timer expires, 9297 * check to see if we have heard anything from the other end for the last 9298 * RTO period. If we have, set the timer to expire for another 9299 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 9300 * RTO << 1 and check again when it expires. Keep exponentially increasing 9301 * the timeout if we have not heard from the other side. If for more than 9302 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 9303 * kill the connection unless the keepalive abort threshold is 0. In 9304 * that case, we will probe "forever." 9305 */ 9306 static void 9307 tcp_keepalive_killer(void *arg) 9308 { 9309 mblk_t *mp; 9310 conn_t *connp = (conn_t *)arg; 9311 tcp_t *tcp = connp->conn_tcp; 9312 int32_t firetime; 9313 int32_t idletime; 9314 int32_t ka_intrvl; 9315 tcp_stack_t *tcps = tcp->tcp_tcps; 9316 9317 tcp->tcp_ka_tid = 0; 9318 9319 if (tcp->tcp_fused) 9320 return; 9321 9322 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 9323 ka_intrvl = tcp->tcp_ka_interval; 9324 9325 /* 9326 * Keepalive probe should only be sent if the application has not 9327 * done a close on the connection. 9328 */ 9329 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 9330 return; 9331 } 9332 /* Timer fired too early, restart it. */ 9333 if (tcp->tcp_state < TCPS_ESTABLISHED) { 9334 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9335 MSEC_TO_TICK(ka_intrvl)); 9336 return; 9337 } 9338 9339 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 9340 /* 9341 * If we have not heard from the other side for a long 9342 * time, kill the connection unless the keepalive abort 9343 * threshold is 0. In that case, we will probe "forever." 9344 */ 9345 if (tcp->tcp_ka_abort_thres != 0 && 9346 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 9347 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 9348 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 9349 tcp->tcp_client_errno : ETIMEDOUT, 11); 9350 return; 9351 } 9352 9353 if (tcp->tcp_snxt == tcp->tcp_suna && 9354 idletime >= ka_intrvl) { 9355 /* Fake resend of last ACKed byte. */ 9356 mblk_t *mp1 = allocb(1, BPRI_LO); 9357 9358 if (mp1 != NULL) { 9359 *mp1->b_wptr++ = '\0'; 9360 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 9361 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 9362 freeb(mp1); 9363 /* 9364 * if allocation failed, fall through to start the 9365 * timer back. 9366 */ 9367 if (mp != NULL) { 9368 TCP_RECORD_TRACE(tcp, mp, 9369 TCP_TRACE_SEND_PKT); 9370 tcp_send_data(tcp, tcp->tcp_wq, mp); 9371 BUMP_MIB(&tcps->tcps_mib, 9372 tcpTimKeepaliveProbe); 9373 if (tcp->tcp_ka_last_intrvl != 0) { 9374 int max; 9375 /* 9376 * We should probe again at least 9377 * in ka_intrvl, but not more than 9378 * tcp_rexmit_interval_max. 9379 */ 9380 max = tcps->tcps_rexmit_interval_max; 9381 firetime = MIN(ka_intrvl - 1, 9382 tcp->tcp_ka_last_intrvl << 1); 9383 if (firetime > max) 9384 firetime = max; 9385 } else { 9386 firetime = tcp->tcp_rto; 9387 } 9388 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9389 tcp_keepalive_killer, 9390 MSEC_TO_TICK(firetime)); 9391 tcp->tcp_ka_last_intrvl = firetime; 9392 return; 9393 } 9394 } 9395 } else { 9396 tcp->tcp_ka_last_intrvl = 0; 9397 } 9398 9399 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9400 if ((firetime = ka_intrvl - idletime) < 0) { 9401 firetime = ka_intrvl; 9402 } 9403 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9404 MSEC_TO_TICK(firetime)); 9405 } 9406 9407 int 9408 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9409 { 9410 queue_t *q = tcp->tcp_rq; 9411 int32_t mss = tcp->tcp_mss; 9412 int maxpsz; 9413 9414 if (TCP_IS_DETACHED(tcp)) 9415 return (mss); 9416 9417 if (tcp->tcp_fused) { 9418 maxpsz = tcp_fuse_maxpsz_set(tcp); 9419 mss = INFPSZ; 9420 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 9421 /* 9422 * Set the sd_qn_maxpsz according to the socket send buffer 9423 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9424 * instruct the stream head to copyin user data into contiguous 9425 * kernel-allocated buffers without breaking it up into smaller 9426 * chunks. We round up the buffer size to the nearest SMSS. 9427 */ 9428 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9429 if (tcp->tcp_kssl_ctx == NULL) 9430 mss = INFPSZ; 9431 else 9432 mss = SSL3_MAX_RECORD_LEN; 9433 } else { 9434 /* 9435 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9436 * (and a multiple of the mss). This instructs the stream 9437 * head to break down larger than SMSS writes into SMSS- 9438 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9439 */ 9440 maxpsz = tcp->tcp_maxpsz * mss; 9441 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9442 maxpsz = tcp->tcp_xmit_hiwater/2; 9443 /* Round up to nearest mss */ 9444 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9445 } 9446 } 9447 (void) setmaxps(q, maxpsz); 9448 tcp->tcp_wq->q_maxpsz = maxpsz; 9449 9450 if (set_maxblk) 9451 (void) mi_set_sth_maxblk(q, mss); 9452 9453 return (mss); 9454 } 9455 9456 /* 9457 * Extract option values from a tcp header. We put any found values into the 9458 * tcpopt struct and return a bitmask saying which options were found. 9459 */ 9460 static int 9461 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9462 { 9463 uchar_t *endp; 9464 int len; 9465 uint32_t mss; 9466 uchar_t *up = (uchar_t *)tcph; 9467 int found = 0; 9468 int32_t sack_len; 9469 tcp_seq sack_begin, sack_end; 9470 tcp_t *tcp; 9471 9472 endp = up + TCP_HDR_LENGTH(tcph); 9473 up += TCP_MIN_HEADER_LENGTH; 9474 while (up < endp) { 9475 len = endp - up; 9476 switch (*up) { 9477 case TCPOPT_EOL: 9478 break; 9479 9480 case TCPOPT_NOP: 9481 up++; 9482 continue; 9483 9484 case TCPOPT_MAXSEG: 9485 if (len < TCPOPT_MAXSEG_LEN || 9486 up[1] != TCPOPT_MAXSEG_LEN) 9487 break; 9488 9489 mss = BE16_TO_U16(up+2); 9490 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9491 tcpopt->tcp_opt_mss = mss; 9492 found |= TCP_OPT_MSS_PRESENT; 9493 9494 up += TCPOPT_MAXSEG_LEN; 9495 continue; 9496 9497 case TCPOPT_WSCALE: 9498 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9499 break; 9500 9501 if (up[2] > TCP_MAX_WINSHIFT) 9502 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9503 else 9504 tcpopt->tcp_opt_wscale = up[2]; 9505 found |= TCP_OPT_WSCALE_PRESENT; 9506 9507 up += TCPOPT_WS_LEN; 9508 continue; 9509 9510 case TCPOPT_SACK_PERMITTED: 9511 if (len < TCPOPT_SACK_OK_LEN || 9512 up[1] != TCPOPT_SACK_OK_LEN) 9513 break; 9514 found |= TCP_OPT_SACK_OK_PRESENT; 9515 up += TCPOPT_SACK_OK_LEN; 9516 continue; 9517 9518 case TCPOPT_SACK: 9519 if (len <= 2 || up[1] <= 2 || len < up[1]) 9520 break; 9521 9522 /* If TCP is not interested in SACK blks... */ 9523 if ((tcp = tcpopt->tcp) == NULL) { 9524 up += up[1]; 9525 continue; 9526 } 9527 sack_len = up[1] - TCPOPT_HEADER_LEN; 9528 up += TCPOPT_HEADER_LEN; 9529 9530 /* 9531 * If the list is empty, allocate one and assume 9532 * nothing is sack'ed. 9533 */ 9534 ASSERT(tcp->tcp_sack_info != NULL); 9535 if (tcp->tcp_notsack_list == NULL) { 9536 tcp_notsack_update(&(tcp->tcp_notsack_list), 9537 tcp->tcp_suna, tcp->tcp_snxt, 9538 &(tcp->tcp_num_notsack_blk), 9539 &(tcp->tcp_cnt_notsack_list)); 9540 9541 /* 9542 * Make sure tcp_notsack_list is not NULL. 9543 * This happens when kmem_alloc(KM_NOSLEEP) 9544 * returns NULL. 9545 */ 9546 if (tcp->tcp_notsack_list == NULL) { 9547 up += sack_len; 9548 continue; 9549 } 9550 tcp->tcp_fack = tcp->tcp_suna; 9551 } 9552 9553 while (sack_len > 0) { 9554 if (up + 8 > endp) { 9555 up = endp; 9556 break; 9557 } 9558 sack_begin = BE32_TO_U32(up); 9559 up += 4; 9560 sack_end = BE32_TO_U32(up); 9561 up += 4; 9562 sack_len -= 8; 9563 /* 9564 * Bounds checking. Make sure the SACK 9565 * info is within tcp_suna and tcp_snxt. 9566 * If this SACK blk is out of bound, ignore 9567 * it but continue to parse the following 9568 * blks. 9569 */ 9570 if (SEQ_LEQ(sack_end, sack_begin) || 9571 SEQ_LT(sack_begin, tcp->tcp_suna) || 9572 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9573 continue; 9574 } 9575 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9576 sack_begin, sack_end, 9577 &(tcp->tcp_num_notsack_blk), 9578 &(tcp->tcp_cnt_notsack_list)); 9579 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9580 tcp->tcp_fack = sack_end; 9581 } 9582 } 9583 found |= TCP_OPT_SACK_PRESENT; 9584 continue; 9585 9586 case TCPOPT_TSTAMP: 9587 if (len < TCPOPT_TSTAMP_LEN || 9588 up[1] != TCPOPT_TSTAMP_LEN) 9589 break; 9590 9591 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9592 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9593 9594 found |= TCP_OPT_TSTAMP_PRESENT; 9595 9596 up += TCPOPT_TSTAMP_LEN; 9597 continue; 9598 9599 default: 9600 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9601 break; 9602 up += up[1]; 9603 continue; 9604 } 9605 break; 9606 } 9607 return (found); 9608 } 9609 9610 /* 9611 * Set the mss associated with a particular tcp based on its current value, 9612 * and a new one passed in. Observe minimums and maximums, and reset 9613 * other state variables that we want to view as multiples of mss. 9614 * 9615 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9616 * highwater marks etc. need to be initialized or adjusted. 9617 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9618 * packet arrives. 9619 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9620 * ICMP6_PACKET_TOO_BIG arrives. 9621 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9622 * to increase the MSS to use the extra bytes available. 9623 * 9624 * Callers except tcp_paws_check() ensure that they only reduce mss. 9625 */ 9626 static void 9627 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9628 { 9629 uint32_t mss_max; 9630 tcp_stack_t *tcps = tcp->tcp_tcps; 9631 9632 if (tcp->tcp_ipversion == IPV4_VERSION) 9633 mss_max = tcps->tcps_mss_max_ipv4; 9634 else 9635 mss_max = tcps->tcps_mss_max_ipv6; 9636 9637 if (mss < tcps->tcps_mss_min) 9638 mss = tcps->tcps_mss_min; 9639 if (mss > mss_max) 9640 mss = mss_max; 9641 /* 9642 * Unless naglim has been set by our client to 9643 * a non-mss value, force naglim to track mss. 9644 * This can help to aggregate small writes. 9645 */ 9646 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9647 tcp->tcp_naglim = mss; 9648 /* 9649 * TCP should be able to buffer at least 4 MSS data for obvious 9650 * performance reason. 9651 */ 9652 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9653 tcp->tcp_xmit_hiwater = mss << 2; 9654 9655 if (do_ss) { 9656 /* 9657 * Either the tcp_cwnd is as yet uninitialized, or mss is 9658 * changing due to a reduction in MTU, presumably as a 9659 * result of a new path component, reset cwnd to its 9660 * "initial" value, as a multiple of the new mss. 9661 */ 9662 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9663 } else { 9664 /* 9665 * Called by tcp_paws_check(), the mss increased 9666 * marginally to allow use of space previously taken 9667 * by the timestamp option. It would be inappropriate 9668 * to apply slow start or tcp_init_cwnd values to 9669 * tcp_cwnd, simply adjust to a multiple of the new mss. 9670 */ 9671 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9672 tcp->tcp_cwnd_cnt = 0; 9673 } 9674 tcp->tcp_mss = mss; 9675 (void) tcp_maxpsz_set(tcp, B_TRUE); 9676 } 9677 9678 /* For /dev/tcp aka AF_INET open */ 9679 static int 9680 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9681 { 9682 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9683 } 9684 9685 /* For /dev/tcp6 aka AF_INET6 open */ 9686 static int 9687 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9688 { 9689 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9690 } 9691 9692 static int 9693 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9694 boolean_t isv6) 9695 { 9696 tcp_t *tcp = NULL; 9697 conn_t *connp; 9698 int err; 9699 vmem_t *minor_arena = NULL; 9700 dev_t conn_dev; 9701 zoneid_t zoneid; 9702 tcp_stack_t *tcps = NULL; 9703 9704 if (q->q_ptr != NULL) 9705 return (0); 9706 9707 if (sflag == MODOPEN) 9708 return (EINVAL); 9709 9710 if (!(flag & SO_ACCEPTOR)) { 9711 /* 9712 * Special case for install: miniroot needs to be able to 9713 * access files via NFS as though it were always in the 9714 * global zone. 9715 */ 9716 if (credp == kcred && nfs_global_client_only != 0) { 9717 zoneid = GLOBAL_ZONEID; 9718 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9719 netstack_tcp; 9720 ASSERT(tcps != NULL); 9721 } else { 9722 netstack_t *ns; 9723 9724 ns = netstack_find_by_cred(credp); 9725 ASSERT(ns != NULL); 9726 tcps = ns->netstack_tcp; 9727 ASSERT(tcps != NULL); 9728 9729 /* 9730 * For exclusive stacks we set the zoneid to zero 9731 * to make TCP operate as if in the global zone. 9732 */ 9733 if (tcps->tcps_netstack->netstack_stackid != 9734 GLOBAL_NETSTACKID) 9735 zoneid = GLOBAL_ZONEID; 9736 else 9737 zoneid = crgetzoneid(credp); 9738 } 9739 /* 9740 * For stackid zero this is done from strplumb.c, but 9741 * non-zero stackids are handled here. 9742 */ 9743 if (tcps->tcps_g_q == NULL && 9744 tcps->tcps_netstack->netstack_stackid != 9745 GLOBAL_NETSTACKID) { 9746 tcp_g_q_setup(tcps); 9747 } 9748 } 9749 9750 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9751 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9752 minor_arena = ip_minor_arena_la; 9753 } else { 9754 /* 9755 * Either minor numbers in the large arena were exhausted 9756 * or a non socket application is doing the open. 9757 * Try to allocate from the small arena. 9758 */ 9759 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9760 if (tcps != NULL) 9761 netstack_rele(tcps->tcps_netstack); 9762 return (EBUSY); 9763 } 9764 minor_arena = ip_minor_arena_sa; 9765 } 9766 ASSERT(minor_arena != NULL); 9767 9768 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9769 9770 if (flag & SO_ACCEPTOR) { 9771 /* No netstack_find_by_cred, hence no netstack_rele needed */ 9772 ASSERT(tcps == NULL); 9773 q->q_qinfo = &tcp_acceptor_rinit; 9774 /* 9775 * the conn_dev and minor_arena will be subsequently used by 9776 * tcp_wput_accept() and tcpclose_accept() to figure out the 9777 * minor device number for this connection from the q_ptr. 9778 */ 9779 RD(q)->q_ptr = (void *)conn_dev; 9780 WR(q)->q_qinfo = &tcp_acceptor_winit; 9781 WR(q)->q_ptr = (void *)minor_arena; 9782 qprocson(q); 9783 return (0); 9784 } 9785 9786 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9787 /* 9788 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9789 * so we drop it by one. 9790 */ 9791 netstack_rele(tcps->tcps_netstack); 9792 if (connp == NULL) { 9793 inet_minor_free(minor_arena, conn_dev); 9794 q->q_ptr = NULL; 9795 return (ENOSR); 9796 } 9797 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9798 tcp = connp->conn_tcp; 9799 9800 q->q_ptr = WR(q)->q_ptr = connp; 9801 if (isv6) { 9802 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9803 connp->conn_send = ip_output_v6; 9804 connp->conn_af_isv6 = B_TRUE; 9805 connp->conn_pkt_isv6 = B_TRUE; 9806 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9807 tcp->tcp_ipversion = IPV6_VERSION; 9808 tcp->tcp_family = AF_INET6; 9809 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9810 } else { 9811 connp->conn_flags |= IPCL_TCP4; 9812 connp->conn_send = ip_output; 9813 connp->conn_af_isv6 = B_FALSE; 9814 connp->conn_pkt_isv6 = B_FALSE; 9815 tcp->tcp_ipversion = IPV4_VERSION; 9816 tcp->tcp_family = AF_INET; 9817 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9818 } 9819 9820 /* 9821 * TCP keeps a copy of cred for cache locality reasons but 9822 * we put a reference only once. If connp->conn_cred 9823 * becomes invalid, tcp_cred should also be set to NULL. 9824 */ 9825 tcp->tcp_cred = connp->conn_cred = credp; 9826 crhold(connp->conn_cred); 9827 tcp->tcp_cpid = curproc->p_pid; 9828 tcp->tcp_open_time = lbolt64; 9829 connp->conn_zoneid = zoneid; 9830 connp->conn_mlp_type = mlptSingle; 9831 connp->conn_ulp_labeled = !is_system_labeled(); 9832 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9833 ASSERT(tcp->tcp_tcps == tcps); 9834 9835 /* 9836 * If the caller has the process-wide flag set, then default to MAC 9837 * exempt mode. This allows read-down to unlabeled hosts. 9838 */ 9839 if (getpflags(NET_MAC_AWARE, credp) != 0) 9840 connp->conn_mac_exempt = B_TRUE; 9841 9842 connp->conn_dev = conn_dev; 9843 connp->conn_minor_arena = minor_arena; 9844 9845 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9846 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9847 9848 if (flag & SO_SOCKSTR) { 9849 /* 9850 * No need to insert a socket in tcp acceptor hash. 9851 * If it was a socket acceptor stream, we dealt with 9852 * it above. A socket listener can never accept a 9853 * connection and doesn't need acceptor_id. 9854 */ 9855 connp->conn_flags |= IPCL_SOCKET; 9856 tcp->tcp_issocket = 1; 9857 WR(q)->q_qinfo = &tcp_sock_winit; 9858 } else { 9859 #ifdef _ILP32 9860 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9861 #else 9862 tcp->tcp_acceptor_id = conn_dev; 9863 #endif /* _ILP32 */ 9864 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9865 } 9866 9867 if (tcps->tcps_trace) 9868 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_SLEEP); 9869 9870 err = tcp_init(tcp, q); 9871 if (err != 0) { 9872 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 9873 tcp_acceptor_hash_remove(tcp); 9874 CONN_DEC_REF(connp); 9875 q->q_ptr = WR(q)->q_ptr = NULL; 9876 return (err); 9877 } 9878 9879 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9880 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9881 9882 /* Non-zero default values */ 9883 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9884 /* 9885 * Put the ref for TCP. Ref for IP was already put 9886 * by ipcl_conn_create. Also Make the conn_t globally 9887 * visible to walkers 9888 */ 9889 mutex_enter(&connp->conn_lock); 9890 CONN_INC_REF_LOCKED(connp); 9891 ASSERT(connp->conn_ref == 2); 9892 connp->conn_state_flags &= ~CONN_INCIPIENT; 9893 mutex_exit(&connp->conn_lock); 9894 9895 qprocson(q); 9896 return (0); 9897 } 9898 9899 /* 9900 * Some TCP options can be "set" by requesting them in the option 9901 * buffer. This is needed for XTI feature test though we do not 9902 * allow it in general. We interpret that this mechanism is more 9903 * applicable to OSI protocols and need not be allowed in general. 9904 * This routine filters out options for which it is not allowed (most) 9905 * and lets through those (few) for which it is. [ The XTI interface 9906 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9907 * ever implemented will have to be allowed here ]. 9908 */ 9909 static boolean_t 9910 tcp_allow_connopt_set(int level, int name) 9911 { 9912 9913 switch (level) { 9914 case IPPROTO_TCP: 9915 switch (name) { 9916 case TCP_NODELAY: 9917 return (B_TRUE); 9918 default: 9919 return (B_FALSE); 9920 } 9921 /*NOTREACHED*/ 9922 default: 9923 return (B_FALSE); 9924 } 9925 /*NOTREACHED*/ 9926 } 9927 9928 /* 9929 * This routine gets default values of certain options whose default 9930 * values are maintained by protocol specific code 9931 */ 9932 /* ARGSUSED */ 9933 int 9934 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9935 { 9936 int32_t *i1 = (int32_t *)ptr; 9937 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9938 9939 switch (level) { 9940 case IPPROTO_TCP: 9941 switch (name) { 9942 case TCP_NOTIFY_THRESHOLD: 9943 *i1 = tcps->tcps_ip_notify_interval; 9944 break; 9945 case TCP_ABORT_THRESHOLD: 9946 *i1 = tcps->tcps_ip_abort_interval; 9947 break; 9948 case TCP_CONN_NOTIFY_THRESHOLD: 9949 *i1 = tcps->tcps_ip_notify_cinterval; 9950 break; 9951 case TCP_CONN_ABORT_THRESHOLD: 9952 *i1 = tcps->tcps_ip_abort_cinterval; 9953 break; 9954 default: 9955 return (-1); 9956 } 9957 break; 9958 case IPPROTO_IP: 9959 switch (name) { 9960 case IP_TTL: 9961 *i1 = tcps->tcps_ipv4_ttl; 9962 break; 9963 default: 9964 return (-1); 9965 } 9966 break; 9967 case IPPROTO_IPV6: 9968 switch (name) { 9969 case IPV6_UNICAST_HOPS: 9970 *i1 = tcps->tcps_ipv6_hoplimit; 9971 break; 9972 default: 9973 return (-1); 9974 } 9975 break; 9976 default: 9977 return (-1); 9978 } 9979 return (sizeof (int)); 9980 } 9981 9982 9983 /* 9984 * TCP routine to get the values of options. 9985 */ 9986 int 9987 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9988 { 9989 int *i1 = (int *)ptr; 9990 conn_t *connp = Q_TO_CONN(q); 9991 tcp_t *tcp = connp->conn_tcp; 9992 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9993 9994 switch (level) { 9995 case SOL_SOCKET: 9996 switch (name) { 9997 case SO_LINGER: { 9998 struct linger *lgr = (struct linger *)ptr; 9999 10000 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 10001 lgr->l_linger = tcp->tcp_lingertime; 10002 } 10003 return (sizeof (struct linger)); 10004 case SO_DEBUG: 10005 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 10006 break; 10007 case SO_KEEPALIVE: 10008 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 10009 break; 10010 case SO_DONTROUTE: 10011 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 10012 break; 10013 case SO_USELOOPBACK: 10014 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 10015 break; 10016 case SO_BROADCAST: 10017 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 10018 break; 10019 case SO_REUSEADDR: 10020 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 10021 break; 10022 case SO_OOBINLINE: 10023 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 10024 break; 10025 case SO_DGRAM_ERRIND: 10026 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 10027 break; 10028 case SO_TYPE: 10029 *i1 = SOCK_STREAM; 10030 break; 10031 case SO_SNDBUF: 10032 *i1 = tcp->tcp_xmit_hiwater; 10033 break; 10034 case SO_RCVBUF: 10035 *i1 = RD(q)->q_hiwat; 10036 break; 10037 case SO_SND_COPYAVOID: 10038 *i1 = tcp->tcp_snd_zcopy_on ? 10039 SO_SND_COPYAVOID : 0; 10040 break; 10041 case SO_ALLZONES: 10042 *i1 = connp->conn_allzones ? 1 : 0; 10043 break; 10044 case SO_ANON_MLP: 10045 *i1 = connp->conn_anon_mlp; 10046 break; 10047 case SO_MAC_EXEMPT: 10048 *i1 = connp->conn_mac_exempt; 10049 break; 10050 case SO_EXCLBIND: 10051 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 10052 break; 10053 case SO_PROTOTYPE: 10054 *i1 = IPPROTO_TCP; 10055 break; 10056 case SO_DOMAIN: 10057 *i1 = tcp->tcp_family; 10058 break; 10059 default: 10060 return (-1); 10061 } 10062 break; 10063 case IPPROTO_TCP: 10064 switch (name) { 10065 case TCP_NODELAY: 10066 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 10067 break; 10068 case TCP_MAXSEG: 10069 *i1 = tcp->tcp_mss; 10070 break; 10071 case TCP_NOTIFY_THRESHOLD: 10072 *i1 = (int)tcp->tcp_first_timer_threshold; 10073 break; 10074 case TCP_ABORT_THRESHOLD: 10075 *i1 = tcp->tcp_second_timer_threshold; 10076 break; 10077 case TCP_CONN_NOTIFY_THRESHOLD: 10078 *i1 = tcp->tcp_first_ctimer_threshold; 10079 break; 10080 case TCP_CONN_ABORT_THRESHOLD: 10081 *i1 = tcp->tcp_second_ctimer_threshold; 10082 break; 10083 case TCP_RECVDSTADDR: 10084 *i1 = tcp->tcp_recvdstaddr; 10085 break; 10086 case TCP_ANONPRIVBIND: 10087 *i1 = tcp->tcp_anon_priv_bind; 10088 break; 10089 case TCP_EXCLBIND: 10090 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 10091 break; 10092 case TCP_INIT_CWND: 10093 *i1 = tcp->tcp_init_cwnd; 10094 break; 10095 case TCP_KEEPALIVE_THRESHOLD: 10096 *i1 = tcp->tcp_ka_interval; 10097 break; 10098 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10099 *i1 = tcp->tcp_ka_abort_thres; 10100 break; 10101 case TCP_CORK: 10102 *i1 = tcp->tcp_cork; 10103 break; 10104 default: 10105 return (-1); 10106 } 10107 break; 10108 case IPPROTO_IP: 10109 if (tcp->tcp_family != AF_INET) 10110 return (-1); 10111 switch (name) { 10112 case IP_OPTIONS: 10113 case T_IP_OPTIONS: { 10114 /* 10115 * This is compatible with BSD in that in only return 10116 * the reverse source route with the final destination 10117 * as the last entry. The first 4 bytes of the option 10118 * will contain the final destination. 10119 */ 10120 int opt_len; 10121 10122 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 10123 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 10124 ASSERT(opt_len >= 0); 10125 /* Caller ensures enough space */ 10126 if (opt_len > 0) { 10127 /* 10128 * TODO: Do we have to handle getsockopt on an 10129 * initiator as well? 10130 */ 10131 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 10132 } 10133 return (0); 10134 } 10135 case IP_TOS: 10136 case T_IP_TOS: 10137 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 10138 break; 10139 case IP_TTL: 10140 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 10141 break; 10142 case IP_NEXTHOP: 10143 /* Handled at IP level */ 10144 return (-EINVAL); 10145 default: 10146 return (-1); 10147 } 10148 break; 10149 case IPPROTO_IPV6: 10150 /* 10151 * IPPROTO_IPV6 options are only supported for sockets 10152 * that are using IPv6 on the wire. 10153 */ 10154 if (tcp->tcp_ipversion != IPV6_VERSION) { 10155 return (-1); 10156 } 10157 switch (name) { 10158 case IPV6_UNICAST_HOPS: 10159 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 10160 break; /* goto sizeof (int) option return */ 10161 case IPV6_BOUND_IF: 10162 /* Zero if not set */ 10163 *i1 = tcp->tcp_bound_if; 10164 break; /* goto sizeof (int) option return */ 10165 case IPV6_RECVPKTINFO: 10166 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 10167 *i1 = 1; 10168 else 10169 *i1 = 0; 10170 break; /* goto sizeof (int) option return */ 10171 case IPV6_RECVTCLASS: 10172 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 10173 *i1 = 1; 10174 else 10175 *i1 = 0; 10176 break; /* goto sizeof (int) option return */ 10177 case IPV6_RECVHOPLIMIT: 10178 if (tcp->tcp_ipv6_recvancillary & 10179 TCP_IPV6_RECVHOPLIMIT) 10180 *i1 = 1; 10181 else 10182 *i1 = 0; 10183 break; /* goto sizeof (int) option return */ 10184 case IPV6_RECVHOPOPTS: 10185 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 10186 *i1 = 1; 10187 else 10188 *i1 = 0; 10189 break; /* goto sizeof (int) option return */ 10190 case IPV6_RECVDSTOPTS: 10191 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 10192 *i1 = 1; 10193 else 10194 *i1 = 0; 10195 break; /* goto sizeof (int) option return */ 10196 case _OLD_IPV6_RECVDSTOPTS: 10197 if (tcp->tcp_ipv6_recvancillary & 10198 TCP_OLD_IPV6_RECVDSTOPTS) 10199 *i1 = 1; 10200 else 10201 *i1 = 0; 10202 break; /* goto sizeof (int) option return */ 10203 case IPV6_RECVRTHDR: 10204 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 10205 *i1 = 1; 10206 else 10207 *i1 = 0; 10208 break; /* goto sizeof (int) option return */ 10209 case IPV6_RECVRTHDRDSTOPTS: 10210 if (tcp->tcp_ipv6_recvancillary & 10211 TCP_IPV6_RECVRTDSTOPTS) 10212 *i1 = 1; 10213 else 10214 *i1 = 0; 10215 break; /* goto sizeof (int) option return */ 10216 case IPV6_PKTINFO: { 10217 /* XXX assumes that caller has room for max size! */ 10218 struct in6_pktinfo *pkti; 10219 10220 pkti = (struct in6_pktinfo *)ptr; 10221 if (ipp->ipp_fields & IPPF_IFINDEX) 10222 pkti->ipi6_ifindex = ipp->ipp_ifindex; 10223 else 10224 pkti->ipi6_ifindex = 0; 10225 if (ipp->ipp_fields & IPPF_ADDR) 10226 pkti->ipi6_addr = ipp->ipp_addr; 10227 else 10228 pkti->ipi6_addr = ipv6_all_zeros; 10229 return (sizeof (struct in6_pktinfo)); 10230 } 10231 case IPV6_TCLASS: 10232 if (ipp->ipp_fields & IPPF_TCLASS) 10233 *i1 = ipp->ipp_tclass; 10234 else 10235 *i1 = IPV6_FLOW_TCLASS( 10236 IPV6_DEFAULT_VERS_AND_FLOW); 10237 break; /* goto sizeof (int) option return */ 10238 case IPV6_NEXTHOP: { 10239 sin6_t *sin6 = (sin6_t *)ptr; 10240 10241 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 10242 return (0); 10243 *sin6 = sin6_null; 10244 sin6->sin6_family = AF_INET6; 10245 sin6->sin6_addr = ipp->ipp_nexthop; 10246 return (sizeof (sin6_t)); 10247 } 10248 case IPV6_HOPOPTS: 10249 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 10250 return (0); 10251 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 10252 return (0); 10253 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 10254 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 10255 if (tcp->tcp_label_len > 0) { 10256 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 10257 ptr[1] = (ipp->ipp_hopoptslen - 10258 tcp->tcp_label_len + 7) / 8 - 1; 10259 } 10260 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 10261 case IPV6_RTHDRDSTOPTS: 10262 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 10263 return (0); 10264 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 10265 return (ipp->ipp_rtdstoptslen); 10266 case IPV6_RTHDR: 10267 if (!(ipp->ipp_fields & IPPF_RTHDR)) 10268 return (0); 10269 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 10270 return (ipp->ipp_rthdrlen); 10271 case IPV6_DSTOPTS: 10272 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 10273 return (0); 10274 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 10275 return (ipp->ipp_dstoptslen); 10276 case IPV6_SRC_PREFERENCES: 10277 return (ip6_get_src_preferences(connp, 10278 (uint32_t *)ptr)); 10279 case IPV6_PATHMTU: { 10280 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 10281 10282 if (tcp->tcp_state < TCPS_ESTABLISHED) 10283 return (-1); 10284 10285 return (ip_fill_mtuinfo(&connp->conn_remv6, 10286 connp->conn_fport, mtuinfo, 10287 connp->conn_netstack)); 10288 } 10289 default: 10290 return (-1); 10291 } 10292 break; 10293 default: 10294 return (-1); 10295 } 10296 return (sizeof (int)); 10297 } 10298 10299 /* 10300 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 10301 * Parameters are assumed to be verified by the caller. 10302 */ 10303 /* ARGSUSED */ 10304 int 10305 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10306 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10307 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10308 { 10309 conn_t *connp = Q_TO_CONN(q); 10310 tcp_t *tcp = connp->conn_tcp; 10311 int *i1 = (int *)invalp; 10312 boolean_t onoff = (*i1 == 0) ? 0 : 1; 10313 boolean_t checkonly; 10314 int reterr; 10315 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 10316 10317 switch (optset_context) { 10318 case SETFN_OPTCOM_CHECKONLY: 10319 checkonly = B_TRUE; 10320 /* 10321 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10322 * inlen != 0 implies value supplied and 10323 * we have to "pretend" to set it. 10324 * inlen == 0 implies that there is no 10325 * value part in T_CHECK request and just validation 10326 * done elsewhere should be enough, we just return here. 10327 */ 10328 if (inlen == 0) { 10329 *outlenp = 0; 10330 return (0); 10331 } 10332 break; 10333 case SETFN_OPTCOM_NEGOTIATE: 10334 checkonly = B_FALSE; 10335 break; 10336 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 10337 case SETFN_CONN_NEGOTIATE: 10338 checkonly = B_FALSE; 10339 /* 10340 * Negotiating local and "association-related" options 10341 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 10342 * primitives is allowed by XTI, but we choose 10343 * to not implement this style negotiation for Internet 10344 * protocols (We interpret it is a must for OSI world but 10345 * optional for Internet protocols) for all options. 10346 * [ Will do only for the few options that enable test 10347 * suites that our XTI implementation of this feature 10348 * works for transports that do allow it ] 10349 */ 10350 if (!tcp_allow_connopt_set(level, name)) { 10351 *outlenp = 0; 10352 return (EINVAL); 10353 } 10354 break; 10355 default: 10356 /* 10357 * We should never get here 10358 */ 10359 *outlenp = 0; 10360 return (EINVAL); 10361 } 10362 10363 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10364 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10365 10366 /* 10367 * For TCP, we should have no ancillary data sent down 10368 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10369 * has to be zero. 10370 */ 10371 ASSERT(thisdg_attrs == NULL); 10372 10373 /* 10374 * For fixed length options, no sanity check 10375 * of passed in length is done. It is assumed *_optcom_req() 10376 * routines do the right thing. 10377 */ 10378 10379 switch (level) { 10380 case SOL_SOCKET: 10381 switch (name) { 10382 case SO_LINGER: { 10383 struct linger *lgr = (struct linger *)invalp; 10384 10385 if (!checkonly) { 10386 if (lgr->l_onoff) { 10387 tcp->tcp_linger = 1; 10388 tcp->tcp_lingertime = lgr->l_linger; 10389 } else { 10390 tcp->tcp_linger = 0; 10391 tcp->tcp_lingertime = 0; 10392 } 10393 /* struct copy */ 10394 *(struct linger *)outvalp = *lgr; 10395 } else { 10396 if (!lgr->l_onoff) { 10397 ((struct linger *) 10398 outvalp)->l_onoff = 0; 10399 ((struct linger *) 10400 outvalp)->l_linger = 0; 10401 } else { 10402 /* struct copy */ 10403 *(struct linger *)outvalp = *lgr; 10404 } 10405 } 10406 *outlenp = sizeof (struct linger); 10407 return (0); 10408 } 10409 case SO_DEBUG: 10410 if (!checkonly) 10411 tcp->tcp_debug = onoff; 10412 break; 10413 case SO_KEEPALIVE: 10414 if (checkonly) { 10415 /* T_CHECK case */ 10416 break; 10417 } 10418 10419 if (!onoff) { 10420 if (tcp->tcp_ka_enabled) { 10421 if (tcp->tcp_ka_tid != 0) { 10422 (void) TCP_TIMER_CANCEL(tcp, 10423 tcp->tcp_ka_tid); 10424 tcp->tcp_ka_tid = 0; 10425 } 10426 tcp->tcp_ka_enabled = 0; 10427 } 10428 break; 10429 } 10430 if (!tcp->tcp_ka_enabled) { 10431 /* Crank up the keepalive timer */ 10432 tcp->tcp_ka_last_intrvl = 0; 10433 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10434 tcp_keepalive_killer, 10435 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10436 tcp->tcp_ka_enabled = 1; 10437 } 10438 break; 10439 case SO_DONTROUTE: 10440 /* 10441 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10442 * only of interest to IP. We track them here only so 10443 * that we can report their current value. 10444 */ 10445 if (!checkonly) { 10446 tcp->tcp_dontroute = onoff; 10447 tcp->tcp_connp->conn_dontroute = onoff; 10448 } 10449 break; 10450 case SO_USELOOPBACK: 10451 if (!checkonly) { 10452 tcp->tcp_useloopback = onoff; 10453 tcp->tcp_connp->conn_loopback = onoff; 10454 } 10455 break; 10456 case SO_BROADCAST: 10457 if (!checkonly) { 10458 tcp->tcp_broadcast = onoff; 10459 tcp->tcp_connp->conn_broadcast = onoff; 10460 } 10461 break; 10462 case SO_REUSEADDR: 10463 if (!checkonly) { 10464 tcp->tcp_reuseaddr = onoff; 10465 tcp->tcp_connp->conn_reuseaddr = onoff; 10466 } 10467 break; 10468 case SO_OOBINLINE: 10469 if (!checkonly) 10470 tcp->tcp_oobinline = onoff; 10471 break; 10472 case SO_DGRAM_ERRIND: 10473 if (!checkonly) 10474 tcp->tcp_dgram_errind = onoff; 10475 break; 10476 case SO_SNDBUF: { 10477 if (*i1 > tcps->tcps_max_buf) { 10478 *outlenp = 0; 10479 return (ENOBUFS); 10480 } 10481 if (checkonly) 10482 break; 10483 10484 tcp->tcp_xmit_hiwater = *i1; 10485 if (tcps->tcps_snd_lowat_fraction != 0) 10486 tcp->tcp_xmit_lowater = 10487 tcp->tcp_xmit_hiwater / 10488 tcps->tcps_snd_lowat_fraction; 10489 (void) tcp_maxpsz_set(tcp, B_TRUE); 10490 /* 10491 * If we are flow-controlled, recheck the condition. 10492 * There are apps that increase SO_SNDBUF size when 10493 * flow-controlled (EWOULDBLOCK), and expect the flow 10494 * control condition to be lifted right away. 10495 */ 10496 mutex_enter(&tcp->tcp_non_sq_lock); 10497 if (tcp->tcp_flow_stopped && 10498 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10499 tcp_clrqfull(tcp); 10500 } 10501 mutex_exit(&tcp->tcp_non_sq_lock); 10502 break; 10503 } 10504 case SO_RCVBUF: 10505 if (*i1 > tcps->tcps_max_buf) { 10506 *outlenp = 0; 10507 return (ENOBUFS); 10508 } 10509 /* Silently ignore zero */ 10510 if (!checkonly && *i1 != 0) { 10511 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10512 (void) tcp_rwnd_set(tcp, *i1); 10513 } 10514 /* 10515 * XXX should we return the rwnd here 10516 * and tcp_opt_get ? 10517 */ 10518 break; 10519 case SO_SND_COPYAVOID: 10520 if (!checkonly) { 10521 /* we only allow enable at most once for now */ 10522 if (tcp->tcp_loopback || 10523 (tcp->tcp_kssl_ctx != NULL) || 10524 (!tcp->tcp_snd_zcopy_aware && 10525 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10526 *outlenp = 0; 10527 return (EOPNOTSUPP); 10528 } 10529 tcp->tcp_snd_zcopy_aware = 1; 10530 } 10531 break; 10532 case SO_ALLZONES: 10533 /* Handled at the IP level */ 10534 return (-EINVAL); 10535 case SO_ANON_MLP: 10536 if (!checkonly) { 10537 mutex_enter(&connp->conn_lock); 10538 connp->conn_anon_mlp = onoff; 10539 mutex_exit(&connp->conn_lock); 10540 } 10541 break; 10542 case SO_MAC_EXEMPT: 10543 if (secpolicy_net_mac_aware(cr) != 0 || 10544 IPCL_IS_BOUND(connp)) 10545 return (EACCES); 10546 if (!checkonly) { 10547 mutex_enter(&connp->conn_lock); 10548 connp->conn_mac_exempt = onoff; 10549 mutex_exit(&connp->conn_lock); 10550 } 10551 break; 10552 case SO_EXCLBIND: 10553 if (!checkonly) 10554 tcp->tcp_exclbind = onoff; 10555 break; 10556 default: 10557 *outlenp = 0; 10558 return (EINVAL); 10559 } 10560 break; 10561 case IPPROTO_TCP: 10562 switch (name) { 10563 case TCP_NODELAY: 10564 if (!checkonly) 10565 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10566 break; 10567 case TCP_NOTIFY_THRESHOLD: 10568 if (!checkonly) 10569 tcp->tcp_first_timer_threshold = *i1; 10570 break; 10571 case TCP_ABORT_THRESHOLD: 10572 if (!checkonly) 10573 tcp->tcp_second_timer_threshold = *i1; 10574 break; 10575 case TCP_CONN_NOTIFY_THRESHOLD: 10576 if (!checkonly) 10577 tcp->tcp_first_ctimer_threshold = *i1; 10578 break; 10579 case TCP_CONN_ABORT_THRESHOLD: 10580 if (!checkonly) 10581 tcp->tcp_second_ctimer_threshold = *i1; 10582 break; 10583 case TCP_RECVDSTADDR: 10584 if (tcp->tcp_state > TCPS_LISTEN) 10585 return (EOPNOTSUPP); 10586 if (!checkonly) 10587 tcp->tcp_recvdstaddr = onoff; 10588 break; 10589 case TCP_ANONPRIVBIND: 10590 if ((reterr = secpolicy_net_privaddr(cr, 0, 10591 IPPROTO_TCP)) != 0) { 10592 *outlenp = 0; 10593 return (reterr); 10594 } 10595 if (!checkonly) { 10596 tcp->tcp_anon_priv_bind = onoff; 10597 } 10598 break; 10599 case TCP_EXCLBIND: 10600 if (!checkonly) 10601 tcp->tcp_exclbind = onoff; 10602 break; /* goto sizeof (int) option return */ 10603 case TCP_INIT_CWND: { 10604 uint32_t init_cwnd = *((uint32_t *)invalp); 10605 10606 if (checkonly) 10607 break; 10608 10609 /* 10610 * Only allow socket with network configuration 10611 * privilege to set the initial cwnd to be larger 10612 * than allowed by RFC 3390. 10613 */ 10614 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10615 tcp->tcp_init_cwnd = init_cwnd; 10616 break; 10617 } 10618 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10619 *outlenp = 0; 10620 return (reterr); 10621 } 10622 if (init_cwnd > TCP_MAX_INIT_CWND) { 10623 *outlenp = 0; 10624 return (EINVAL); 10625 } 10626 tcp->tcp_init_cwnd = init_cwnd; 10627 break; 10628 } 10629 case TCP_KEEPALIVE_THRESHOLD: 10630 if (checkonly) 10631 break; 10632 10633 if (*i1 < tcps->tcps_keepalive_interval_low || 10634 *i1 > tcps->tcps_keepalive_interval_high) { 10635 *outlenp = 0; 10636 return (EINVAL); 10637 } 10638 if (*i1 != tcp->tcp_ka_interval) { 10639 tcp->tcp_ka_interval = *i1; 10640 /* 10641 * Check if we need to restart the 10642 * keepalive timer. 10643 */ 10644 if (tcp->tcp_ka_tid != 0) { 10645 ASSERT(tcp->tcp_ka_enabled); 10646 (void) TCP_TIMER_CANCEL(tcp, 10647 tcp->tcp_ka_tid); 10648 tcp->tcp_ka_last_intrvl = 0; 10649 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10650 tcp_keepalive_killer, 10651 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10652 } 10653 } 10654 break; 10655 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10656 if (!checkonly) { 10657 if (*i1 < 10658 tcps->tcps_keepalive_abort_interval_low || 10659 *i1 > 10660 tcps->tcps_keepalive_abort_interval_high) { 10661 *outlenp = 0; 10662 return (EINVAL); 10663 } 10664 tcp->tcp_ka_abort_thres = *i1; 10665 } 10666 break; 10667 case TCP_CORK: 10668 if (!checkonly) { 10669 /* 10670 * if tcp->tcp_cork was set and is now 10671 * being unset, we have to make sure that 10672 * the remaining data gets sent out. Also 10673 * unset tcp->tcp_cork so that tcp_wput_data() 10674 * can send data even if it is less than mss 10675 */ 10676 if (tcp->tcp_cork && onoff == 0 && 10677 tcp->tcp_unsent > 0) { 10678 tcp->tcp_cork = B_FALSE; 10679 tcp_wput_data(tcp, NULL, B_FALSE); 10680 } 10681 tcp->tcp_cork = onoff; 10682 } 10683 break; 10684 default: 10685 *outlenp = 0; 10686 return (EINVAL); 10687 } 10688 break; 10689 case IPPROTO_IP: 10690 if (tcp->tcp_family != AF_INET) { 10691 *outlenp = 0; 10692 return (ENOPROTOOPT); 10693 } 10694 switch (name) { 10695 case IP_OPTIONS: 10696 case T_IP_OPTIONS: 10697 reterr = tcp_opt_set_header(tcp, checkonly, 10698 invalp, inlen); 10699 if (reterr) { 10700 *outlenp = 0; 10701 return (reterr); 10702 } 10703 /* OK return - copy input buffer into output buffer */ 10704 if (invalp != outvalp) { 10705 /* don't trust bcopy for identical src/dst */ 10706 bcopy(invalp, outvalp, inlen); 10707 } 10708 *outlenp = inlen; 10709 return (0); 10710 case IP_TOS: 10711 case T_IP_TOS: 10712 if (!checkonly) { 10713 tcp->tcp_ipha->ipha_type_of_service = 10714 (uchar_t)*i1; 10715 tcp->tcp_tos = (uchar_t)*i1; 10716 } 10717 break; 10718 case IP_TTL: 10719 if (!checkonly) { 10720 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10721 tcp->tcp_ttl = (uchar_t)*i1; 10722 } 10723 break; 10724 case IP_BOUND_IF: 10725 case IP_NEXTHOP: 10726 /* Handled at the IP level */ 10727 return (-EINVAL); 10728 case IP_SEC_OPT: 10729 /* 10730 * We should not allow policy setting after 10731 * we start listening for connections. 10732 */ 10733 if (tcp->tcp_state == TCPS_LISTEN) { 10734 return (EINVAL); 10735 } else { 10736 /* Handled at the IP level */ 10737 return (-EINVAL); 10738 } 10739 default: 10740 *outlenp = 0; 10741 return (EINVAL); 10742 } 10743 break; 10744 case IPPROTO_IPV6: { 10745 ip6_pkt_t *ipp; 10746 10747 /* 10748 * IPPROTO_IPV6 options are only supported for sockets 10749 * that are using IPv6 on the wire. 10750 */ 10751 if (tcp->tcp_ipversion != IPV6_VERSION) { 10752 *outlenp = 0; 10753 return (ENOPROTOOPT); 10754 } 10755 /* 10756 * Only sticky options; no ancillary data 10757 */ 10758 ASSERT(thisdg_attrs == NULL); 10759 ipp = &tcp->tcp_sticky_ipp; 10760 10761 switch (name) { 10762 case IPV6_UNICAST_HOPS: 10763 /* -1 means use default */ 10764 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10765 *outlenp = 0; 10766 return (EINVAL); 10767 } 10768 if (!checkonly) { 10769 if (*i1 == -1) { 10770 tcp->tcp_ip6h->ip6_hops = 10771 ipp->ipp_unicast_hops = 10772 (uint8_t)tcps->tcps_ipv6_hoplimit; 10773 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10774 /* Pass modified value to IP. */ 10775 *i1 = tcp->tcp_ip6h->ip6_hops; 10776 } else { 10777 tcp->tcp_ip6h->ip6_hops = 10778 ipp->ipp_unicast_hops = 10779 (uint8_t)*i1; 10780 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10781 } 10782 reterr = tcp_build_hdrs(q, tcp); 10783 if (reterr != 0) 10784 return (reterr); 10785 } 10786 break; 10787 case IPV6_BOUND_IF: 10788 if (!checkonly) { 10789 int error = 0; 10790 10791 tcp->tcp_bound_if = *i1; 10792 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10793 B_TRUE, checkonly, level, name, mblk); 10794 if (error != 0) { 10795 *outlenp = 0; 10796 return (error); 10797 } 10798 } 10799 break; 10800 /* 10801 * Set boolean switches for ancillary data delivery 10802 */ 10803 case IPV6_RECVPKTINFO: 10804 if (!checkonly) { 10805 if (onoff) 10806 tcp->tcp_ipv6_recvancillary |= 10807 TCP_IPV6_RECVPKTINFO; 10808 else 10809 tcp->tcp_ipv6_recvancillary &= 10810 ~TCP_IPV6_RECVPKTINFO; 10811 /* Force it to be sent up with the next msg */ 10812 tcp->tcp_recvifindex = 0; 10813 } 10814 break; 10815 case IPV6_RECVTCLASS: 10816 if (!checkonly) { 10817 if (onoff) 10818 tcp->tcp_ipv6_recvancillary |= 10819 TCP_IPV6_RECVTCLASS; 10820 else 10821 tcp->tcp_ipv6_recvancillary &= 10822 ~TCP_IPV6_RECVTCLASS; 10823 } 10824 break; 10825 case IPV6_RECVHOPLIMIT: 10826 if (!checkonly) { 10827 if (onoff) 10828 tcp->tcp_ipv6_recvancillary |= 10829 TCP_IPV6_RECVHOPLIMIT; 10830 else 10831 tcp->tcp_ipv6_recvancillary &= 10832 ~TCP_IPV6_RECVHOPLIMIT; 10833 /* Force it to be sent up with the next msg */ 10834 tcp->tcp_recvhops = 0xffffffffU; 10835 } 10836 break; 10837 case IPV6_RECVHOPOPTS: 10838 if (!checkonly) { 10839 if (onoff) 10840 tcp->tcp_ipv6_recvancillary |= 10841 TCP_IPV6_RECVHOPOPTS; 10842 else 10843 tcp->tcp_ipv6_recvancillary &= 10844 ~TCP_IPV6_RECVHOPOPTS; 10845 } 10846 break; 10847 case IPV6_RECVDSTOPTS: 10848 if (!checkonly) { 10849 if (onoff) 10850 tcp->tcp_ipv6_recvancillary |= 10851 TCP_IPV6_RECVDSTOPTS; 10852 else 10853 tcp->tcp_ipv6_recvancillary &= 10854 ~TCP_IPV6_RECVDSTOPTS; 10855 } 10856 break; 10857 case _OLD_IPV6_RECVDSTOPTS: 10858 if (!checkonly) { 10859 if (onoff) 10860 tcp->tcp_ipv6_recvancillary |= 10861 TCP_OLD_IPV6_RECVDSTOPTS; 10862 else 10863 tcp->tcp_ipv6_recvancillary &= 10864 ~TCP_OLD_IPV6_RECVDSTOPTS; 10865 } 10866 break; 10867 case IPV6_RECVRTHDR: 10868 if (!checkonly) { 10869 if (onoff) 10870 tcp->tcp_ipv6_recvancillary |= 10871 TCP_IPV6_RECVRTHDR; 10872 else 10873 tcp->tcp_ipv6_recvancillary &= 10874 ~TCP_IPV6_RECVRTHDR; 10875 } 10876 break; 10877 case IPV6_RECVRTHDRDSTOPTS: 10878 if (!checkonly) { 10879 if (onoff) 10880 tcp->tcp_ipv6_recvancillary |= 10881 TCP_IPV6_RECVRTDSTOPTS; 10882 else 10883 tcp->tcp_ipv6_recvancillary &= 10884 ~TCP_IPV6_RECVRTDSTOPTS; 10885 } 10886 break; 10887 case IPV6_PKTINFO: 10888 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10889 return (EINVAL); 10890 if (checkonly) 10891 break; 10892 10893 if (inlen == 0) { 10894 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10895 } else { 10896 struct in6_pktinfo *pkti; 10897 10898 pkti = (struct in6_pktinfo *)invalp; 10899 /* 10900 * RFC 3542 states that ipi6_addr must be 10901 * the unspecified address when setting the 10902 * IPV6_PKTINFO sticky socket option on a 10903 * TCP socket. 10904 */ 10905 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10906 return (EINVAL); 10907 /* 10908 * ip6_set_pktinfo() validates the source 10909 * address and interface index. 10910 */ 10911 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10912 pkti, mblk); 10913 if (reterr != 0) 10914 return (reterr); 10915 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10916 ipp->ipp_addr = pkti->ipi6_addr; 10917 if (ipp->ipp_ifindex != 0) 10918 ipp->ipp_fields |= IPPF_IFINDEX; 10919 else 10920 ipp->ipp_fields &= ~IPPF_IFINDEX; 10921 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10922 ipp->ipp_fields |= IPPF_ADDR; 10923 else 10924 ipp->ipp_fields &= ~IPPF_ADDR; 10925 } 10926 reterr = tcp_build_hdrs(q, tcp); 10927 if (reterr != 0) 10928 return (reterr); 10929 break; 10930 case IPV6_TCLASS: 10931 if (inlen != 0 && inlen != sizeof (int)) 10932 return (EINVAL); 10933 if (checkonly) 10934 break; 10935 10936 if (inlen == 0) { 10937 ipp->ipp_fields &= ~IPPF_TCLASS; 10938 } else { 10939 if (*i1 > 255 || *i1 < -1) 10940 return (EINVAL); 10941 if (*i1 == -1) { 10942 ipp->ipp_tclass = 0; 10943 *i1 = 0; 10944 } else { 10945 ipp->ipp_tclass = *i1; 10946 } 10947 ipp->ipp_fields |= IPPF_TCLASS; 10948 } 10949 reterr = tcp_build_hdrs(q, tcp); 10950 if (reterr != 0) 10951 return (reterr); 10952 break; 10953 case IPV6_NEXTHOP: 10954 /* 10955 * IP will verify that the nexthop is reachable 10956 * and fail for sticky options. 10957 */ 10958 if (inlen != 0 && inlen != sizeof (sin6_t)) 10959 return (EINVAL); 10960 if (checkonly) 10961 break; 10962 10963 if (inlen == 0) { 10964 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10965 } else { 10966 sin6_t *sin6 = (sin6_t *)invalp; 10967 10968 if (sin6->sin6_family != AF_INET6) 10969 return (EAFNOSUPPORT); 10970 if (IN6_IS_ADDR_V4MAPPED( 10971 &sin6->sin6_addr)) 10972 return (EADDRNOTAVAIL); 10973 ipp->ipp_nexthop = sin6->sin6_addr; 10974 if (!IN6_IS_ADDR_UNSPECIFIED( 10975 &ipp->ipp_nexthop)) 10976 ipp->ipp_fields |= IPPF_NEXTHOP; 10977 else 10978 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10979 } 10980 reterr = tcp_build_hdrs(q, tcp); 10981 if (reterr != 0) 10982 return (reterr); 10983 break; 10984 case IPV6_HOPOPTS: { 10985 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10986 10987 /* 10988 * Sanity checks - minimum size, size a multiple of 10989 * eight bytes, and matching size passed in. 10990 */ 10991 if (inlen != 0 && 10992 inlen != (8 * (hopts->ip6h_len + 1))) 10993 return (EINVAL); 10994 10995 if (checkonly) 10996 break; 10997 10998 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10999 (uchar_t **)&ipp->ipp_hopopts, 11000 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 11001 if (reterr != 0) 11002 return (reterr); 11003 if (ipp->ipp_hopoptslen == 0) 11004 ipp->ipp_fields &= ~IPPF_HOPOPTS; 11005 else 11006 ipp->ipp_fields |= IPPF_HOPOPTS; 11007 reterr = tcp_build_hdrs(q, tcp); 11008 if (reterr != 0) 11009 return (reterr); 11010 break; 11011 } 11012 case IPV6_RTHDRDSTOPTS: { 11013 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 11014 11015 /* 11016 * Sanity checks - minimum size, size a multiple of 11017 * eight bytes, and matching size passed in. 11018 */ 11019 if (inlen != 0 && 11020 inlen != (8 * (dopts->ip6d_len + 1))) 11021 return (EINVAL); 11022 11023 if (checkonly) 11024 break; 11025 11026 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 11027 (uchar_t **)&ipp->ipp_rtdstopts, 11028 &ipp->ipp_rtdstoptslen, 0); 11029 if (reterr != 0) 11030 return (reterr); 11031 if (ipp->ipp_rtdstoptslen == 0) 11032 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 11033 else 11034 ipp->ipp_fields |= IPPF_RTDSTOPTS; 11035 reterr = tcp_build_hdrs(q, tcp); 11036 if (reterr != 0) 11037 return (reterr); 11038 break; 11039 } 11040 case IPV6_DSTOPTS: { 11041 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 11042 11043 /* 11044 * Sanity checks - minimum size, size a multiple of 11045 * eight bytes, and matching size passed in. 11046 */ 11047 if (inlen != 0 && 11048 inlen != (8 * (dopts->ip6d_len + 1))) 11049 return (EINVAL); 11050 11051 if (checkonly) 11052 break; 11053 11054 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 11055 (uchar_t **)&ipp->ipp_dstopts, 11056 &ipp->ipp_dstoptslen, 0); 11057 if (reterr != 0) 11058 return (reterr); 11059 if (ipp->ipp_dstoptslen == 0) 11060 ipp->ipp_fields &= ~IPPF_DSTOPTS; 11061 else 11062 ipp->ipp_fields |= IPPF_DSTOPTS; 11063 reterr = tcp_build_hdrs(q, tcp); 11064 if (reterr != 0) 11065 return (reterr); 11066 break; 11067 } 11068 case IPV6_RTHDR: { 11069 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 11070 11071 /* 11072 * Sanity checks - minimum size, size a multiple of 11073 * eight bytes, and matching size passed in. 11074 */ 11075 if (inlen != 0 && 11076 inlen != (8 * (rt->ip6r_len + 1))) 11077 return (EINVAL); 11078 11079 if (checkonly) 11080 break; 11081 11082 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 11083 (uchar_t **)&ipp->ipp_rthdr, 11084 &ipp->ipp_rthdrlen, 0); 11085 if (reterr != 0) 11086 return (reterr); 11087 if (ipp->ipp_rthdrlen == 0) 11088 ipp->ipp_fields &= ~IPPF_RTHDR; 11089 else 11090 ipp->ipp_fields |= IPPF_RTHDR; 11091 reterr = tcp_build_hdrs(q, tcp); 11092 if (reterr != 0) 11093 return (reterr); 11094 break; 11095 } 11096 case IPV6_V6ONLY: 11097 if (!checkonly) 11098 tcp->tcp_connp->conn_ipv6_v6only = onoff; 11099 break; 11100 case IPV6_USE_MIN_MTU: 11101 if (inlen != sizeof (int)) 11102 return (EINVAL); 11103 11104 if (*i1 < -1 || *i1 > 1) 11105 return (EINVAL); 11106 11107 if (checkonly) 11108 break; 11109 11110 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 11111 ipp->ipp_use_min_mtu = *i1; 11112 break; 11113 case IPV6_BOUND_PIF: 11114 /* Handled at the IP level */ 11115 return (-EINVAL); 11116 case IPV6_SEC_OPT: 11117 /* 11118 * We should not allow policy setting after 11119 * we start listening for connections. 11120 */ 11121 if (tcp->tcp_state == TCPS_LISTEN) { 11122 return (EINVAL); 11123 } else { 11124 /* Handled at the IP level */ 11125 return (-EINVAL); 11126 } 11127 case IPV6_SRC_PREFERENCES: 11128 if (inlen != sizeof (uint32_t)) 11129 return (EINVAL); 11130 reterr = ip6_set_src_preferences(tcp->tcp_connp, 11131 *(uint32_t *)invalp); 11132 if (reterr != 0) { 11133 *outlenp = 0; 11134 return (reterr); 11135 } 11136 break; 11137 default: 11138 *outlenp = 0; 11139 return (EINVAL); 11140 } 11141 break; 11142 } /* end IPPROTO_IPV6 */ 11143 default: 11144 *outlenp = 0; 11145 return (EINVAL); 11146 } 11147 /* 11148 * Common case of OK return with outval same as inval 11149 */ 11150 if (invalp != outvalp) { 11151 /* don't trust bcopy for identical src/dst */ 11152 (void) bcopy(invalp, outvalp, inlen); 11153 } 11154 *outlenp = inlen; 11155 return (0); 11156 } 11157 11158 /* 11159 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 11160 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 11161 * headers, and the maximum size tcp header (to avoid reallocation 11162 * on the fly for additional tcp options). 11163 * Returns failure if can't allocate memory. 11164 */ 11165 static int 11166 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 11167 { 11168 char *hdrs; 11169 uint_t hdrs_len; 11170 ip6i_t *ip6i; 11171 char buf[TCP_MAX_HDR_LENGTH]; 11172 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 11173 in6_addr_t src, dst; 11174 tcp_stack_t *tcps = tcp->tcp_tcps; 11175 11176 /* 11177 * save the existing tcp header and source/dest IP addresses 11178 */ 11179 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 11180 src = tcp->tcp_ip6h->ip6_src; 11181 dst = tcp->tcp_ip6h->ip6_dst; 11182 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 11183 ASSERT(hdrs_len != 0); 11184 if (hdrs_len > tcp->tcp_iphc_len) { 11185 /* Need to reallocate */ 11186 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 11187 if (hdrs == NULL) 11188 return (ENOMEM); 11189 if (tcp->tcp_iphc != NULL) { 11190 if (tcp->tcp_hdr_grown) { 11191 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 11192 } else { 11193 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 11194 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 11195 } 11196 tcp->tcp_iphc_len = 0; 11197 } 11198 ASSERT(tcp->tcp_iphc_len == 0); 11199 tcp->tcp_iphc = hdrs; 11200 tcp->tcp_iphc_len = hdrs_len; 11201 tcp->tcp_hdr_grown = B_TRUE; 11202 } 11203 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 11204 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 11205 11206 /* Set header fields not in ipp */ 11207 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 11208 ip6i = (ip6i_t *)tcp->tcp_iphc; 11209 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 11210 } else { 11211 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 11212 } 11213 /* 11214 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 11215 * 11216 * tcp->tcp_tcp_hdr_len doesn't change here. 11217 */ 11218 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 11219 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 11220 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 11221 11222 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 11223 11224 tcp->tcp_ip6h->ip6_src = src; 11225 tcp->tcp_ip6h->ip6_dst = dst; 11226 11227 /* 11228 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 11229 * the default value for TCP. 11230 */ 11231 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 11232 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 11233 11234 /* 11235 * If we're setting extension headers after a connection 11236 * has been established, and if we have a routing header 11237 * among the extension headers, call ip_massage_options_v6 to 11238 * manipulate the routing header/ip6_dst set the checksum 11239 * difference in the tcp header template. 11240 * (This happens in tcp_connect_ipv6 if the routing header 11241 * is set prior to the connect.) 11242 * Set the tcp_sum to zero first in case we've cleared a 11243 * routing header or don't have one at all. 11244 */ 11245 tcp->tcp_sum = 0; 11246 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 11247 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 11248 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 11249 (uint8_t *)tcp->tcp_tcph); 11250 if (rth != NULL) { 11251 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 11252 rth, tcps->tcps_netstack); 11253 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 11254 (tcp->tcp_sum >> 16)); 11255 } 11256 } 11257 11258 /* Try to get everything in a single mblk */ 11259 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcps->tcps_wroff_xtra); 11260 return (0); 11261 } 11262 11263 /* 11264 * Transfer any source route option from ipha to buf/dst in reversed form. 11265 */ 11266 static int 11267 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 11268 { 11269 ipoptp_t opts; 11270 uchar_t *opt; 11271 uint8_t optval; 11272 uint8_t optlen; 11273 uint32_t len = 0; 11274 11275 for (optval = ipoptp_first(&opts, ipha); 11276 optval != IPOPT_EOL; 11277 optval = ipoptp_next(&opts)) { 11278 opt = opts.ipoptp_cur; 11279 optlen = opts.ipoptp_len; 11280 switch (optval) { 11281 int off1, off2; 11282 case IPOPT_SSRR: 11283 case IPOPT_LSRR: 11284 11285 /* Reverse source route */ 11286 /* 11287 * First entry should be the next to last one in the 11288 * current source route (the last entry is our 11289 * address.) 11290 * The last entry should be the final destination. 11291 */ 11292 buf[IPOPT_OPTVAL] = (uint8_t)optval; 11293 buf[IPOPT_OLEN] = (uint8_t)optlen; 11294 off1 = IPOPT_MINOFF_SR - 1; 11295 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 11296 if (off2 < 0) { 11297 /* No entries in source route */ 11298 break; 11299 } 11300 bcopy(opt + off2, dst, IP_ADDR_LEN); 11301 /* 11302 * Note: use src since ipha has not had its src 11303 * and dst reversed (it is in the state it was 11304 * received. 11305 */ 11306 bcopy(&ipha->ipha_src, buf + off2, 11307 IP_ADDR_LEN); 11308 off2 -= IP_ADDR_LEN; 11309 11310 while (off2 > 0) { 11311 bcopy(opt + off2, buf + off1, 11312 IP_ADDR_LEN); 11313 off1 += IP_ADDR_LEN; 11314 off2 -= IP_ADDR_LEN; 11315 } 11316 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11317 buf += optlen; 11318 len += optlen; 11319 break; 11320 } 11321 } 11322 done: 11323 /* Pad the resulting options */ 11324 while (len & 0x3) { 11325 *buf++ = IPOPT_EOL; 11326 len++; 11327 } 11328 return (len); 11329 } 11330 11331 11332 /* 11333 * Extract and revert a source route from ipha (if any) 11334 * and then update the relevant fields in both tcp_t and the standard header. 11335 */ 11336 static void 11337 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11338 { 11339 char buf[TCP_MAX_HDR_LENGTH]; 11340 uint_t tcph_len; 11341 int len; 11342 11343 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11344 len = IPH_HDR_LENGTH(ipha); 11345 if (len == IP_SIMPLE_HDR_LENGTH) 11346 /* Nothing to do */ 11347 return; 11348 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11349 (len & 0x3)) 11350 return; 11351 11352 tcph_len = tcp->tcp_tcp_hdr_len; 11353 bcopy(tcp->tcp_tcph, buf, tcph_len); 11354 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11355 (tcp->tcp_ipha->ipha_dst & 0xffff); 11356 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11357 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11358 len += IP_SIMPLE_HDR_LENGTH; 11359 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11360 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11361 if ((int)tcp->tcp_sum < 0) 11362 tcp->tcp_sum--; 11363 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11364 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11365 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11366 bcopy(buf, tcp->tcp_tcph, tcph_len); 11367 tcp->tcp_ip_hdr_len = len; 11368 tcp->tcp_ipha->ipha_version_and_hdr_length = 11369 (IP_VERSION << 4) | (len >> 2); 11370 len += tcph_len; 11371 tcp->tcp_hdr_len = len; 11372 } 11373 11374 /* 11375 * Copy the standard header into its new location, 11376 * lay in the new options and then update the relevant 11377 * fields in both tcp_t and the standard header. 11378 */ 11379 static int 11380 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11381 { 11382 uint_t tcph_len; 11383 uint8_t *ip_optp; 11384 tcph_t *new_tcph; 11385 tcp_stack_t *tcps = tcp->tcp_tcps; 11386 11387 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11388 return (EINVAL); 11389 11390 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11391 return (EINVAL); 11392 11393 if (checkonly) { 11394 /* 11395 * do not really set, just pretend to - T_CHECK 11396 */ 11397 return (0); 11398 } 11399 11400 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11401 if (tcp->tcp_label_len > 0) { 11402 int padlen; 11403 uint8_t opt; 11404 11405 /* convert list termination to no-ops */ 11406 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11407 ip_optp += ip_optp[IPOPT_OLEN]; 11408 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11409 while (--padlen >= 0) 11410 *ip_optp++ = opt; 11411 } 11412 tcph_len = tcp->tcp_tcp_hdr_len; 11413 new_tcph = (tcph_t *)(ip_optp + len); 11414 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11415 tcp->tcp_tcph = new_tcph; 11416 bcopy(ptr, ip_optp, len); 11417 11418 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11419 11420 tcp->tcp_ip_hdr_len = len; 11421 tcp->tcp_ipha->ipha_version_and_hdr_length = 11422 (IP_VERSION << 4) | (len >> 2); 11423 tcp->tcp_hdr_len = len + tcph_len; 11424 if (!TCP_IS_DETACHED(tcp)) { 11425 /* Always allocate room for all options. */ 11426 (void) mi_set_sth_wroff(tcp->tcp_rq, 11427 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11428 } 11429 return (0); 11430 } 11431 11432 /* Get callback routine passed to nd_load by tcp_param_register */ 11433 /* ARGSUSED */ 11434 static int 11435 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11436 { 11437 tcpparam_t *tcppa = (tcpparam_t *)cp; 11438 11439 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11440 return (0); 11441 } 11442 11443 /* 11444 * Walk through the param array specified registering each element with the 11445 * named dispatch handler. 11446 */ 11447 static boolean_t 11448 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11449 { 11450 for (; cnt-- > 0; tcppa++) { 11451 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11452 if (!nd_load(ndp, tcppa->tcp_param_name, 11453 tcp_param_get, tcp_param_set, 11454 (caddr_t)tcppa)) { 11455 nd_free(ndp); 11456 return (B_FALSE); 11457 } 11458 } 11459 } 11460 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11461 KM_SLEEP); 11462 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11463 sizeof (tcpparam_t)); 11464 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11465 tcp_param_get, tcp_param_set_aligned, 11466 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11467 nd_free(ndp); 11468 return (B_FALSE); 11469 } 11470 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11471 KM_SLEEP); 11472 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11473 sizeof (tcpparam_t)); 11474 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11475 tcp_param_get, tcp_param_set_aligned, 11476 (caddr_t)tcps->tcps_mdt_head_param)) { 11477 nd_free(ndp); 11478 return (B_FALSE); 11479 } 11480 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11481 KM_SLEEP); 11482 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11483 sizeof (tcpparam_t)); 11484 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11485 tcp_param_get, tcp_param_set_aligned, 11486 (caddr_t)tcps->tcps_mdt_tail_param)) { 11487 nd_free(ndp); 11488 return (B_FALSE); 11489 } 11490 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11491 KM_SLEEP); 11492 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11493 sizeof (tcpparam_t)); 11494 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11495 tcp_param_get, tcp_param_set_aligned, 11496 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11497 nd_free(ndp); 11498 return (B_FALSE); 11499 } 11500 if (!nd_load(ndp, "tcp_extra_priv_ports", 11501 tcp_extra_priv_ports_get, NULL, NULL)) { 11502 nd_free(ndp); 11503 return (B_FALSE); 11504 } 11505 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11506 NULL, tcp_extra_priv_ports_add, NULL)) { 11507 nd_free(ndp); 11508 return (B_FALSE); 11509 } 11510 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11511 NULL, tcp_extra_priv_ports_del, NULL)) { 11512 nd_free(ndp); 11513 return (B_FALSE); 11514 } 11515 if (!nd_load(ndp, "tcp_status", tcp_status_report, NULL, 11516 NULL)) { 11517 nd_free(ndp); 11518 return (B_FALSE); 11519 } 11520 if (!nd_load(ndp, "tcp_bind_hash", tcp_bind_hash_report, 11521 NULL, NULL)) { 11522 nd_free(ndp); 11523 return (B_FALSE); 11524 } 11525 if (!nd_load(ndp, "tcp_listen_hash", 11526 tcp_listen_hash_report, NULL, NULL)) { 11527 nd_free(ndp); 11528 return (B_FALSE); 11529 } 11530 if (!nd_load(ndp, "tcp_conn_hash", tcp_conn_hash_report, 11531 NULL, NULL)) { 11532 nd_free(ndp); 11533 return (B_FALSE); 11534 } 11535 if (!nd_load(ndp, "tcp_acceptor_hash", 11536 tcp_acceptor_hash_report, NULL, NULL)) { 11537 nd_free(ndp); 11538 return (B_FALSE); 11539 } 11540 if (!nd_load(ndp, "tcp_host_param", tcp_host_param_report, 11541 tcp_host_param_set, NULL)) { 11542 nd_free(ndp); 11543 return (B_FALSE); 11544 } 11545 if (!nd_load(ndp, "tcp_host_param_ipv6", 11546 tcp_host_param_report, tcp_host_param_set_ipv6, NULL)) { 11547 nd_free(ndp); 11548 return (B_FALSE); 11549 } 11550 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11551 tcp_1948_phrase_set, NULL)) { 11552 nd_free(ndp); 11553 return (B_FALSE); 11554 } 11555 if (!nd_load(ndp, "tcp_reserved_port_list", 11556 tcp_reserved_port_list, NULL, NULL)) { 11557 nd_free(ndp); 11558 return (B_FALSE); 11559 } 11560 /* 11561 * Dummy ndd variables - only to convey obsolescence information 11562 * through printing of their name (no get or set routines) 11563 * XXX Remove in future releases ? 11564 */ 11565 if (!nd_load(ndp, 11566 "tcp_close_wait_interval(obsoleted - " 11567 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11568 nd_free(ndp); 11569 return (B_FALSE); 11570 } 11571 return (B_TRUE); 11572 } 11573 11574 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11575 /* ARGSUSED */ 11576 static int 11577 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11578 cred_t *cr) 11579 { 11580 long new_value; 11581 tcpparam_t *tcppa = (tcpparam_t *)cp; 11582 11583 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11584 new_value < tcppa->tcp_param_min || 11585 new_value > tcppa->tcp_param_max) { 11586 return (EINVAL); 11587 } 11588 /* 11589 * Need to make sure new_value is a multiple of 4. If it is not, 11590 * round it up. For future 64 bit requirement, we actually make it 11591 * a multiple of 8. 11592 */ 11593 if (new_value & 0x7) { 11594 new_value = (new_value & ~0x7) + 0x8; 11595 } 11596 tcppa->tcp_param_val = new_value; 11597 return (0); 11598 } 11599 11600 /* Set callback routine passed to nd_load by tcp_param_register */ 11601 /* ARGSUSED */ 11602 static int 11603 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11604 { 11605 long new_value; 11606 tcpparam_t *tcppa = (tcpparam_t *)cp; 11607 11608 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11609 new_value < tcppa->tcp_param_min || 11610 new_value > tcppa->tcp_param_max) { 11611 return (EINVAL); 11612 } 11613 tcppa->tcp_param_val = new_value; 11614 return (0); 11615 } 11616 11617 /* 11618 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11619 * is filled, return as much as we can. The message passed in may be 11620 * multi-part, chained using b_cont. "start" is the starting sequence 11621 * number for this piece. 11622 */ 11623 static mblk_t * 11624 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11625 { 11626 uint32_t end; 11627 mblk_t *mp1; 11628 mblk_t *mp2; 11629 mblk_t *next_mp; 11630 uint32_t u1; 11631 tcp_stack_t *tcps = tcp->tcp_tcps; 11632 11633 /* Walk through all the new pieces. */ 11634 do { 11635 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11636 (uintptr_t)INT_MAX); 11637 end = start + (int)(mp->b_wptr - mp->b_rptr); 11638 next_mp = mp->b_cont; 11639 if (start == end) { 11640 /* Empty. Blast it. */ 11641 freeb(mp); 11642 continue; 11643 } 11644 mp->b_cont = NULL; 11645 TCP_REASS_SET_SEQ(mp, start); 11646 TCP_REASS_SET_END(mp, end); 11647 mp1 = tcp->tcp_reass_tail; 11648 if (!mp1) { 11649 tcp->tcp_reass_tail = mp; 11650 tcp->tcp_reass_head = mp; 11651 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11652 UPDATE_MIB(&tcps->tcps_mib, 11653 tcpInDataUnorderBytes, end - start); 11654 continue; 11655 } 11656 /* New stuff completely beyond tail? */ 11657 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11658 /* Link it on end. */ 11659 mp1->b_cont = mp; 11660 tcp->tcp_reass_tail = mp; 11661 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11662 UPDATE_MIB(&tcps->tcps_mib, 11663 tcpInDataUnorderBytes, end - start); 11664 continue; 11665 } 11666 mp1 = tcp->tcp_reass_head; 11667 u1 = TCP_REASS_SEQ(mp1); 11668 /* New stuff at the front? */ 11669 if (SEQ_LT(start, u1)) { 11670 /* Yes... Check for overlap. */ 11671 mp->b_cont = mp1; 11672 tcp->tcp_reass_head = mp; 11673 tcp_reass_elim_overlap(tcp, mp); 11674 continue; 11675 } 11676 /* 11677 * The new piece fits somewhere between the head and tail. 11678 * We find our slot, where mp1 precedes us and mp2 trails. 11679 */ 11680 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11681 u1 = TCP_REASS_SEQ(mp2); 11682 if (SEQ_LEQ(start, u1)) 11683 break; 11684 } 11685 /* Link ourselves in */ 11686 mp->b_cont = mp2; 11687 mp1->b_cont = mp; 11688 11689 /* Trim overlap with following mblk(s) first */ 11690 tcp_reass_elim_overlap(tcp, mp); 11691 11692 /* Trim overlap with preceding mblk */ 11693 tcp_reass_elim_overlap(tcp, mp1); 11694 11695 } while (start = end, mp = next_mp); 11696 mp1 = tcp->tcp_reass_head; 11697 /* Anything ready to go? */ 11698 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11699 return (NULL); 11700 /* Eat what we can off the queue */ 11701 for (;;) { 11702 mp = mp1->b_cont; 11703 end = TCP_REASS_END(mp1); 11704 TCP_REASS_SET_SEQ(mp1, 0); 11705 TCP_REASS_SET_END(mp1, 0); 11706 if (!mp) { 11707 tcp->tcp_reass_tail = NULL; 11708 break; 11709 } 11710 if (end != TCP_REASS_SEQ(mp)) { 11711 mp1->b_cont = NULL; 11712 break; 11713 } 11714 mp1 = mp; 11715 } 11716 mp1 = tcp->tcp_reass_head; 11717 tcp->tcp_reass_head = mp; 11718 return (mp1); 11719 } 11720 11721 /* Eliminate any overlap that mp may have over later mblks */ 11722 static void 11723 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11724 { 11725 uint32_t end; 11726 mblk_t *mp1; 11727 uint32_t u1; 11728 tcp_stack_t *tcps = tcp->tcp_tcps; 11729 11730 end = TCP_REASS_END(mp); 11731 while ((mp1 = mp->b_cont) != NULL) { 11732 u1 = TCP_REASS_SEQ(mp1); 11733 if (!SEQ_GT(end, u1)) 11734 break; 11735 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11736 mp->b_wptr -= end - u1; 11737 TCP_REASS_SET_END(mp, u1); 11738 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11739 UPDATE_MIB(&tcps->tcps_mib, 11740 tcpInDataPartDupBytes, end - u1); 11741 break; 11742 } 11743 mp->b_cont = mp1->b_cont; 11744 TCP_REASS_SET_SEQ(mp1, 0); 11745 TCP_REASS_SET_END(mp1, 0); 11746 freeb(mp1); 11747 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11748 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11749 } 11750 if (!mp1) 11751 tcp->tcp_reass_tail = mp; 11752 } 11753 11754 /* 11755 * Send up all messages queued on tcp_rcv_list. 11756 */ 11757 static uint_t 11758 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11759 { 11760 mblk_t *mp; 11761 uint_t ret = 0; 11762 uint_t thwin; 11763 #ifdef DEBUG 11764 uint_t cnt = 0; 11765 #endif 11766 tcp_stack_t *tcps = tcp->tcp_tcps; 11767 11768 /* Can't drain on an eager connection */ 11769 if (tcp->tcp_listener != NULL) 11770 return (ret); 11771 11772 /* Can't be sodirect enabled */ 11773 ASSERT(SOD_NOT_ENABLED(tcp)); 11774 11775 /* 11776 * Handle two cases here: we are currently fused or we were 11777 * previously fused and have some urgent data to be delivered 11778 * upstream. The latter happens because we either ran out of 11779 * memory or were detached and therefore sending the SIGURG was 11780 * deferred until this point. In either case we pass control 11781 * over to tcp_fuse_rcv_drain() since it may need to complete 11782 * some work. 11783 */ 11784 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11785 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11786 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11787 &tcp->tcp_fused_sigurg_mp)) 11788 return (ret); 11789 } 11790 11791 while ((mp = tcp->tcp_rcv_list) != NULL) { 11792 tcp->tcp_rcv_list = mp->b_next; 11793 mp->b_next = NULL; 11794 #ifdef DEBUG 11795 cnt += msgdsize(mp); 11796 #endif 11797 /* Does this need SSL processing first? */ 11798 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11799 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11800 mblk_t *, mp); 11801 tcp_kssl_input(tcp, mp); 11802 continue; 11803 } 11804 putnext(q, mp); 11805 } 11806 ASSERT(cnt == tcp->tcp_rcv_cnt); 11807 tcp->tcp_rcv_last_head = NULL; 11808 tcp->tcp_rcv_last_tail = NULL; 11809 tcp->tcp_rcv_cnt = 0; 11810 11811 /* Learn the latest rwnd information that we sent to the other side. */ 11812 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11813 << tcp->tcp_rcv_ws; 11814 /* This is peer's calculated send window (our receive window). */ 11815 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11816 /* 11817 * Increase the receive window to max. But we need to do receiver 11818 * SWS avoidance. This means that we need to check the increase of 11819 * of receive window is at least 1 MSS. 11820 */ 11821 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11822 /* 11823 * If the window that the other side knows is less than max 11824 * deferred acks segments, send an update immediately. 11825 */ 11826 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11827 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11828 ret = TH_ACK_NEEDED; 11829 } 11830 tcp->tcp_rwnd = q->q_hiwat; 11831 } 11832 /* No need for the push timer now. */ 11833 if (tcp->tcp_push_tid != 0) { 11834 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11835 tcp->tcp_push_tid = 0; 11836 } 11837 return (ret); 11838 } 11839 11840 /* 11841 * Queue data on tcp_rcv_list which is a b_next chain. 11842 * tcp_rcv_last_head/tail is the last element of this chain. 11843 * Each element of the chain is a b_cont chain. 11844 * 11845 * M_DATA messages are added to the current element. 11846 * Other messages are added as new (b_next) elements. 11847 */ 11848 void 11849 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11850 { 11851 ASSERT(seg_len == msgdsize(mp)); 11852 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11853 11854 /* Can't be sodirect enabled */ 11855 ASSERT(SOD_NOT_ENABLED(tcp)); 11856 11857 if (tcp->tcp_rcv_list == NULL) { 11858 ASSERT(tcp->tcp_rcv_last_head == NULL); 11859 tcp->tcp_rcv_list = mp; 11860 tcp->tcp_rcv_last_head = mp; 11861 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11862 tcp->tcp_rcv_last_tail->b_cont = mp; 11863 } else { 11864 tcp->tcp_rcv_last_head->b_next = mp; 11865 tcp->tcp_rcv_last_head = mp; 11866 } 11867 11868 while (mp->b_cont) 11869 mp = mp->b_cont; 11870 11871 tcp->tcp_rcv_last_tail = mp; 11872 tcp->tcp_rcv_cnt += seg_len; 11873 tcp->tcp_rwnd -= seg_len; 11874 } 11875 11876 /* 11877 * The tcp_rcv_sod_XXX() functions enqueue data directly to the socket 11878 * above, in addition when uioa is enabled schedule an asynchronous uio 11879 * prior to enqueuing. They implement the combinhed semantics of the 11880 * tcp_rcv_XXX() functions, tcp_rcv_list push logic, and STREAMS putnext() 11881 * canputnext(), i.e. flow-control with backenable. 11882 * 11883 * tcp_sod_wakeup() is called where tcp_rcv_drain() would be called in the 11884 * non sodirect connection but as there are no tcp_tcv_list mblk_t's we deal 11885 * with the rcv_wnd and push timer and call the sodirect wakeup function. 11886 * 11887 * Must be called with sodp->sod_lock held and will return with the lock 11888 * released. 11889 */ 11890 static uint_t 11891 tcp_rcv_sod_wakeup(tcp_t *tcp, sodirect_t *sodp) 11892 { 11893 queue_t *q = tcp->tcp_rq; 11894 uint_t thwin; 11895 tcp_stack_t *tcps = tcp->tcp_tcps; 11896 uint_t ret = 0; 11897 11898 /* Can't be an eager connection */ 11899 ASSERT(tcp->tcp_listener == NULL); 11900 11901 /* Caller must have lock held */ 11902 ASSERT(MUTEX_HELD(sodp->sod_lock)); 11903 11904 /* Sodirect mode so must not be a tcp_rcv_list */ 11905 ASSERT(tcp->tcp_rcv_list == NULL); 11906 11907 if (SOD_QFULL(sodp)) { 11908 /* Q is full, mark Q for need backenable */ 11909 SOD_QSETBE(sodp); 11910 } 11911 /* Last advertised rwnd, i.e. rwnd last sent in a packet */ 11912 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11913 << tcp->tcp_rcv_ws; 11914 /* This is peer's calculated send window (our available rwnd). */ 11915 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11916 /* 11917 * Increase the receive window to max. But we need to do receiver 11918 * SWS avoidance. This means that we need to check the increase of 11919 * of receive window is at least 1 MSS. 11920 */ 11921 if (!SOD_QFULL(sodp) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11922 /* 11923 * If the window that the other side knows is less than max 11924 * deferred acks segments, send an update immediately. 11925 */ 11926 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11927 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11928 ret = TH_ACK_NEEDED; 11929 } 11930 tcp->tcp_rwnd = q->q_hiwat; 11931 } 11932 11933 if (!SOD_QEMPTY(sodp)) { 11934 /* Wakeup to socket */ 11935 sodp->sod_state &= SOD_WAKE_CLR; 11936 sodp->sod_state |= SOD_WAKE_DONE; 11937 (sodp->sod_wakeup)(sodp); 11938 /* wakeup() does the mutex_ext() */ 11939 } else { 11940 /* Q is empty, no need to wake */ 11941 sodp->sod_state &= SOD_WAKE_CLR; 11942 sodp->sod_state |= SOD_WAKE_NOT; 11943 mutex_exit(sodp->sod_lock); 11944 } 11945 11946 /* No need for the push timer now. */ 11947 if (tcp->tcp_push_tid != 0) { 11948 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11949 tcp->tcp_push_tid = 0; 11950 } 11951 11952 return (ret); 11953 } 11954 11955 /* 11956 * Called where tcp_rcv_enqueue()/putnext(RD(q)) would be. For M_DATA 11957 * mblk_t's if uioa enabled then start a uioa asynchronous copy directly 11958 * to the user-land buffer and flag the mblk_t as such. 11959 * 11960 * Also, handle tcp_rwnd. 11961 */ 11962 uint_t 11963 tcp_rcv_sod_enqueue(tcp_t *tcp, sodirect_t *sodp, mblk_t *mp, uint_t seg_len) 11964 { 11965 uioa_t *uioap = &sodp->sod_uioa; 11966 boolean_t qfull; 11967 uint_t thwin; 11968 11969 /* Can't be an eager connection */ 11970 ASSERT(tcp->tcp_listener == NULL); 11971 11972 /* Caller must have lock held */ 11973 ASSERT(MUTEX_HELD(sodp->sod_lock)); 11974 11975 /* Sodirect mode so must not be a tcp_rcv_list */ 11976 ASSERT(tcp->tcp_rcv_list == NULL); 11977 11978 /* Passed in segment length must be equal to mblk_t chain data size */ 11979 ASSERT(seg_len == msgdsize(mp)); 11980 11981 if (DB_TYPE(mp) != M_DATA) { 11982 /* Only process M_DATA mblk_t's */ 11983 goto enq; 11984 } 11985 if (uioap->uioa_state & UIOA_ENABLED) { 11986 /* Uioa is enabled */ 11987 mblk_t *mp1 = mp; 11988 11989 if (seg_len > uioap->uio_resid) { 11990 /* 11991 * There isn't enough uio space for the mblk_t chain 11992 * so disable uioa such that this and any additional 11993 * mblk_t data is handled by the socket and schedule 11994 * the socket for wakeup to finish this uioa. 11995 */ 11996 uioap->uioa_state &= UIOA_CLR; 11997 uioap->uioa_state |= UIOA_FINI; 11998 if (sodp->sod_state & SOD_WAKE_NOT) { 11999 sodp->sod_state &= SOD_WAKE_CLR; 12000 sodp->sod_state |= SOD_WAKE_NEED; 12001 } 12002 goto enq; 12003 } 12004 do { 12005 uint32_t len = MBLKL(mp1); 12006 12007 if (!uioamove(mp1->b_rptr, len, UIO_READ, uioap)) { 12008 /* Scheduled, mark dblk_t as such */ 12009 DB_FLAGS(mp1) |= DBLK_UIOA; 12010 } else { 12011 /* Error, turn off async processing */ 12012 uioap->uioa_state &= UIOA_CLR; 12013 uioap->uioa_state |= UIOA_FINI; 12014 break; 12015 } 12016 } while ((mp1 = mp1->b_cont) != NULL); 12017 12018 if (mp1 != NULL || uioap->uio_resid == 0) { 12019 /* 12020 * Not all mblk_t(s) uioamoved (error) or all uio 12021 * space has been consumed so schedule the socket 12022 * for wakeup to finish this uio. 12023 */ 12024 sodp->sod_state &= SOD_WAKE_CLR; 12025 sodp->sod_state |= SOD_WAKE_NEED; 12026 } 12027 } else if (uioap->uioa_state & UIOA_FINI) { 12028 /* 12029 * Post UIO_ENABLED waiting for socket to finish processing 12030 * so just enqueue and update tcp_rwnd. 12031 */ 12032 if (SOD_QFULL(sodp)) 12033 tcp->tcp_rwnd -= seg_len; 12034 } else if (sodp->sod_want > 0) { 12035 /* 12036 * Uioa isn't enabled but sodirect has a pending read(). 12037 */ 12038 if (SOD_QCNT(sodp) + seg_len >= sodp->sod_want) { 12039 if (sodp->sod_state & SOD_WAKE_NOT) { 12040 /* Schedule socket for wakeup */ 12041 sodp->sod_state &= SOD_WAKE_CLR; 12042 sodp->sod_state |= SOD_WAKE_NEED; 12043 } 12044 tcp->tcp_rwnd -= seg_len; 12045 } 12046 } else if (SOD_QCNT(sodp) + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 12047 /* 12048 * No pending sodirect read() so used the default 12049 * TCP push logic to guess that a push is needed. 12050 */ 12051 if (sodp->sod_state & SOD_WAKE_NOT) { 12052 /* Schedule socket for wakeup */ 12053 sodp->sod_state &= SOD_WAKE_CLR; 12054 sodp->sod_state |= SOD_WAKE_NEED; 12055 } 12056 tcp->tcp_rwnd -= seg_len; 12057 } else { 12058 /* Just update tcp_rwnd */ 12059 tcp->tcp_rwnd -= seg_len; 12060 } 12061 enq: 12062 qfull = SOD_QFULL(sodp); 12063 12064 (sodp->sod_enqueue)(sodp, mp); 12065 12066 if (! qfull && SOD_QFULL(sodp)) { 12067 /* Wasn't QFULL, now QFULL, need back-enable */ 12068 SOD_QSETBE(sodp); 12069 } 12070 12071 /* 12072 * Check to see if remote avail swnd < mss due to delayed ACK, 12073 * first get advertised rwnd. 12074 */ 12075 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)); 12076 /* Minus delayed ACK count */ 12077 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 12078 if (thwin < tcp->tcp_mss) { 12079 /* Remote avail swnd < mss, need ACK now */ 12080 return (TH_ACK_NEEDED); 12081 } 12082 12083 return (0); 12084 } 12085 12086 /* 12087 * DEFAULT TCP ENTRY POINT via squeue on READ side. 12088 * 12089 * This is the default entry function into TCP on the read side. TCP is 12090 * always entered via squeue i.e. using squeue's for mutual exclusion. 12091 * When classifier does a lookup to find the tcp, it also puts a reference 12092 * on the conn structure associated so the tcp is guaranteed to exist 12093 * when we come here. We still need to check the state because it might 12094 * as well has been closed. The squeue processing function i.e. squeue_enter, 12095 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 12096 * CONN_DEC_REF. 12097 * 12098 * Apart from the default entry point, IP also sends packets directly to 12099 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 12100 * connections. 12101 */ 12102 void 12103 tcp_input(void *arg, mblk_t *mp, void *arg2) 12104 { 12105 conn_t *connp = (conn_t *)arg; 12106 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 12107 12108 /* arg2 is the sqp */ 12109 ASSERT(arg2 != NULL); 12110 ASSERT(mp != NULL); 12111 12112 /* 12113 * Don't accept any input on a closed tcp as this TCP logically does 12114 * not exist on the system. Don't proceed further with this TCP. 12115 * For eg. this packet could trigger another close of this tcp 12116 * which would be disastrous for tcp_refcnt. tcp_close_detached / 12117 * tcp_clean_death / tcp_closei_local must be called at most once 12118 * on a TCP. In this case we need to refeed the packet into the 12119 * classifier and figure out where the packet should go. Need to 12120 * preserve the recv_ill somehow. Until we figure that out, for 12121 * now just drop the packet if we can't classify the packet. 12122 */ 12123 if (tcp->tcp_state == TCPS_CLOSED || 12124 tcp->tcp_state == TCPS_BOUND) { 12125 conn_t *new_connp; 12126 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 12127 12128 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 12129 if (new_connp != NULL) { 12130 tcp_reinput(new_connp, mp, arg2); 12131 return; 12132 } 12133 /* We failed to classify. For now just drop the packet */ 12134 freemsg(mp); 12135 return; 12136 } 12137 12138 if (DB_TYPE(mp) == M_DATA) 12139 tcp_rput_data(connp, mp, arg2); 12140 else 12141 tcp_rput_common(tcp, mp); 12142 } 12143 12144 /* 12145 * The read side put procedure. 12146 * The packets passed up by ip are assume to be aligned according to 12147 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 12148 */ 12149 static void 12150 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 12151 { 12152 /* 12153 * tcp_rput_data() does not expect M_CTL except for the case 12154 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 12155 * type. Need to make sure that any other M_CTLs don't make 12156 * it to tcp_rput_data since it is not expecting any and doesn't 12157 * check for it. 12158 */ 12159 if (DB_TYPE(mp) == M_CTL) { 12160 switch (*(uint32_t *)(mp->b_rptr)) { 12161 case TCP_IOC_ABORT_CONN: 12162 /* 12163 * Handle connection abort request. 12164 */ 12165 tcp_ioctl_abort_handler(tcp, mp); 12166 return; 12167 case IPSEC_IN: 12168 /* 12169 * Only secure icmp arrive in TCP and they 12170 * don't go through data path. 12171 */ 12172 tcp_icmp_error(tcp, mp); 12173 return; 12174 case IN_PKTINFO: 12175 /* 12176 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 12177 * sockets that are receiving IPv4 traffic. tcp 12178 */ 12179 ASSERT(tcp->tcp_family == AF_INET6); 12180 ASSERT(tcp->tcp_ipv6_recvancillary & 12181 TCP_IPV6_RECVPKTINFO); 12182 tcp_rput_data(tcp->tcp_connp, mp, 12183 tcp->tcp_connp->conn_sqp); 12184 return; 12185 case MDT_IOC_INFO_UPDATE: 12186 /* 12187 * Handle Multidata information update; the 12188 * following routine will free the message. 12189 */ 12190 if (tcp->tcp_connp->conn_mdt_ok) { 12191 tcp_mdt_update(tcp, 12192 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 12193 B_FALSE); 12194 } 12195 freemsg(mp); 12196 return; 12197 case LSO_IOC_INFO_UPDATE: 12198 /* 12199 * Handle LSO information update; the following 12200 * routine will free the message. 12201 */ 12202 if (tcp->tcp_connp->conn_lso_ok) { 12203 tcp_lso_update(tcp, 12204 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 12205 } 12206 freemsg(mp); 12207 return; 12208 default: 12209 /* 12210 * tcp_icmp_err() will process the M_CTL packets. 12211 * Non-ICMP packets, if any, will be discarded in 12212 * tcp_icmp_err(). We will process the ICMP packet 12213 * even if we are TCP_IS_DETACHED_NONEAGER as the 12214 * incoming ICMP packet may result in changing 12215 * the tcp_mss, which we would need if we have 12216 * packets to retransmit. 12217 */ 12218 tcp_icmp_error(tcp, mp); 12219 return; 12220 } 12221 } 12222 12223 /* No point processing the message if tcp is already closed */ 12224 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 12225 freemsg(mp); 12226 return; 12227 } 12228 12229 tcp_rput_other(tcp, mp); 12230 } 12231 12232 12233 /* The minimum of smoothed mean deviation in RTO calculation. */ 12234 #define TCP_SD_MIN 400 12235 12236 /* 12237 * Set RTO for this connection. The formula is from Jacobson and Karels' 12238 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 12239 * are the same as those in Appendix A.2 of that paper. 12240 * 12241 * m = new measurement 12242 * sa = smoothed RTT average (8 * average estimates). 12243 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 12244 */ 12245 static void 12246 tcp_set_rto(tcp_t *tcp, clock_t rtt) 12247 { 12248 long m = TICK_TO_MSEC(rtt); 12249 clock_t sa = tcp->tcp_rtt_sa; 12250 clock_t sv = tcp->tcp_rtt_sd; 12251 clock_t rto; 12252 tcp_stack_t *tcps = tcp->tcp_tcps; 12253 12254 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 12255 tcp->tcp_rtt_update++; 12256 12257 /* tcp_rtt_sa is not 0 means this is a new sample. */ 12258 if (sa != 0) { 12259 /* 12260 * Update average estimator: 12261 * new rtt = 7/8 old rtt + 1/8 Error 12262 */ 12263 12264 /* m is now Error in estimate. */ 12265 m -= sa >> 3; 12266 if ((sa += m) <= 0) { 12267 /* 12268 * Don't allow the smoothed average to be negative. 12269 * We use 0 to denote reinitialization of the 12270 * variables. 12271 */ 12272 sa = 1; 12273 } 12274 12275 /* 12276 * Update deviation estimator: 12277 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 12278 */ 12279 if (m < 0) 12280 m = -m; 12281 m -= sv >> 2; 12282 sv += m; 12283 } else { 12284 /* 12285 * This follows BSD's implementation. So the reinitialized 12286 * RTO is 3 * m. We cannot go less than 2 because if the 12287 * link is bandwidth dominated, doubling the window size 12288 * during slow start means doubling the RTT. We want to be 12289 * more conservative when we reinitialize our estimates. 3 12290 * is just a convenient number. 12291 */ 12292 sa = m << 3; 12293 sv = m << 1; 12294 } 12295 if (sv < TCP_SD_MIN) { 12296 /* 12297 * We do not know that if sa captures the delay ACK 12298 * effect as in a long train of segments, a receiver 12299 * does not delay its ACKs. So set the minimum of sv 12300 * to be TCP_SD_MIN, which is default to 400 ms, twice 12301 * of BSD DATO. That means the minimum of mean 12302 * deviation is 100 ms. 12303 * 12304 */ 12305 sv = TCP_SD_MIN; 12306 } 12307 tcp->tcp_rtt_sa = sa; 12308 tcp->tcp_rtt_sd = sv; 12309 /* 12310 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 12311 * 12312 * Add tcp_rexmit_interval extra in case of extreme environment 12313 * where the algorithm fails to work. The default value of 12314 * tcp_rexmit_interval_extra should be 0. 12315 * 12316 * As we use a finer grained clock than BSD and update 12317 * RTO for every ACKs, add in another .25 of RTT to the 12318 * deviation of RTO to accomodate burstiness of 1/4 of 12319 * window size. 12320 */ 12321 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 12322 12323 if (rto > tcps->tcps_rexmit_interval_max) { 12324 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 12325 } else if (rto < tcps->tcps_rexmit_interval_min) { 12326 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 12327 } else { 12328 tcp->tcp_rto = rto; 12329 } 12330 12331 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 12332 tcp->tcp_timer_backoff = 0; 12333 } 12334 12335 /* 12336 * tcp_get_seg_mp() is called to get the pointer to a segment in the 12337 * send queue which starts at the given seq. no. 12338 * 12339 * Parameters: 12340 * tcp_t *tcp: the tcp instance pointer. 12341 * uint32_t seq: the starting seq. no of the requested segment. 12342 * int32_t *off: after the execution, *off will be the offset to 12343 * the returned mblk which points to the requested seq no. 12344 * It is the caller's responsibility to send in a non-null off. 12345 * 12346 * Return: 12347 * A mblk_t pointer pointing to the requested segment in send queue. 12348 */ 12349 static mblk_t * 12350 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 12351 { 12352 int32_t cnt; 12353 mblk_t *mp; 12354 12355 /* Defensive coding. Make sure we don't send incorrect data. */ 12356 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 12357 return (NULL); 12358 12359 cnt = seq - tcp->tcp_suna; 12360 mp = tcp->tcp_xmit_head; 12361 while (cnt > 0 && mp != NULL) { 12362 cnt -= mp->b_wptr - mp->b_rptr; 12363 if (cnt < 0) { 12364 cnt += mp->b_wptr - mp->b_rptr; 12365 break; 12366 } 12367 mp = mp->b_cont; 12368 } 12369 ASSERT(mp != NULL); 12370 *off = cnt; 12371 return (mp); 12372 } 12373 12374 /* 12375 * This function handles all retransmissions if SACK is enabled for this 12376 * connection. First it calculates how many segments can be retransmitted 12377 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12378 * segments. A segment is eligible if sack_cnt for that segment is greater 12379 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12380 * all eligible segments, it checks to see if TCP can send some new segments 12381 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12382 * 12383 * Parameters: 12384 * tcp_t *tcp: the tcp structure of the connection. 12385 * uint_t *flags: in return, appropriate value will be set for 12386 * tcp_rput_data(). 12387 */ 12388 static void 12389 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12390 { 12391 notsack_blk_t *notsack_blk; 12392 int32_t usable_swnd; 12393 int32_t mss; 12394 uint32_t seg_len; 12395 mblk_t *xmit_mp; 12396 tcp_stack_t *tcps = tcp->tcp_tcps; 12397 12398 ASSERT(tcp->tcp_sack_info != NULL); 12399 ASSERT(tcp->tcp_notsack_list != NULL); 12400 ASSERT(tcp->tcp_rexmit == B_FALSE); 12401 12402 /* Defensive coding in case there is a bug... */ 12403 if (tcp->tcp_notsack_list == NULL) { 12404 return; 12405 } 12406 notsack_blk = tcp->tcp_notsack_list; 12407 mss = tcp->tcp_mss; 12408 12409 /* 12410 * Limit the num of outstanding data in the network to be 12411 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12412 */ 12413 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12414 12415 /* At least retransmit 1 MSS of data. */ 12416 if (usable_swnd <= 0) { 12417 usable_swnd = mss; 12418 } 12419 12420 /* Make sure no new RTT samples will be taken. */ 12421 tcp->tcp_csuna = tcp->tcp_snxt; 12422 12423 notsack_blk = tcp->tcp_notsack_list; 12424 while (usable_swnd > 0) { 12425 mblk_t *snxt_mp, *tmp_mp; 12426 tcp_seq begin = tcp->tcp_sack_snxt; 12427 tcp_seq end; 12428 int32_t off; 12429 12430 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12431 if (SEQ_GT(notsack_blk->end, begin) && 12432 (notsack_blk->sack_cnt >= 12433 tcps->tcps_dupack_fast_retransmit)) { 12434 end = notsack_blk->end; 12435 if (SEQ_LT(begin, notsack_blk->begin)) { 12436 begin = notsack_blk->begin; 12437 } 12438 break; 12439 } 12440 } 12441 /* 12442 * All holes are filled. Manipulate tcp_cwnd to send more 12443 * if we can. Note that after the SACK recovery, tcp_cwnd is 12444 * set to tcp_cwnd_ssthresh. 12445 */ 12446 if (notsack_blk == NULL) { 12447 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12448 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12449 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12450 ASSERT(tcp->tcp_cwnd > 0); 12451 return; 12452 } else { 12453 usable_swnd = usable_swnd / mss; 12454 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12455 MAX(usable_swnd * mss, mss); 12456 *flags |= TH_XMIT_NEEDED; 12457 return; 12458 } 12459 } 12460 12461 /* 12462 * Note that we may send more than usable_swnd allows here 12463 * because of round off, but no more than 1 MSS of data. 12464 */ 12465 seg_len = end - begin; 12466 if (seg_len > mss) 12467 seg_len = mss; 12468 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12469 ASSERT(snxt_mp != NULL); 12470 /* This should not happen. Defensive coding again... */ 12471 if (snxt_mp == NULL) { 12472 return; 12473 } 12474 12475 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12476 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12477 if (xmit_mp == NULL) 12478 return; 12479 12480 usable_swnd -= seg_len; 12481 tcp->tcp_pipe += seg_len; 12482 tcp->tcp_sack_snxt = begin + seg_len; 12483 TCP_RECORD_TRACE(tcp, xmit_mp, TCP_TRACE_SEND_PKT); 12484 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12485 12486 /* 12487 * Update the send timestamp to avoid false retransmission. 12488 */ 12489 snxt_mp->b_prev = (mblk_t *)lbolt; 12490 12491 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12492 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12493 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12494 /* 12495 * Update tcp_rexmit_max to extend this SACK recovery phase. 12496 * This happens when new data sent during fast recovery is 12497 * also lost. If TCP retransmits those new data, it needs 12498 * to extend SACK recover phase to avoid starting another 12499 * fast retransmit/recovery unnecessarily. 12500 */ 12501 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12502 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12503 } 12504 } 12505 } 12506 12507 /* 12508 * This function handles policy checking at TCP level for non-hard_bound/ 12509 * detached connections. 12510 */ 12511 static boolean_t 12512 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12513 boolean_t secure, boolean_t mctl_present) 12514 { 12515 ipsec_latch_t *ipl = NULL; 12516 ipsec_action_t *act = NULL; 12517 mblk_t *data_mp; 12518 ipsec_in_t *ii; 12519 const char *reason; 12520 kstat_named_t *counter; 12521 tcp_stack_t *tcps = tcp->tcp_tcps; 12522 ipsec_stack_t *ipss; 12523 ip_stack_t *ipst; 12524 12525 ASSERT(mctl_present || !secure); 12526 12527 ASSERT((ipha == NULL && ip6h != NULL) || 12528 (ip6h == NULL && ipha != NULL)); 12529 12530 /* 12531 * We don't necessarily have an ipsec_in_act action to verify 12532 * policy because of assymetrical policy where we have only 12533 * outbound policy and no inbound policy (possible with global 12534 * policy). 12535 */ 12536 if (!secure) { 12537 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12538 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12539 return (B_TRUE); 12540 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12541 "tcp_check_policy", ipha, ip6h, secure, 12542 tcps->tcps_netstack); 12543 ipss = tcps->tcps_netstack->netstack_ipsec; 12544 12545 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12546 DROPPER(ipss, ipds_tcp_clear), 12547 &tcps->tcps_dropper); 12548 return (B_FALSE); 12549 } 12550 12551 /* 12552 * We have a secure packet. 12553 */ 12554 if (act == NULL) { 12555 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12556 "tcp_check_policy", ipha, ip6h, secure, 12557 tcps->tcps_netstack); 12558 ipss = tcps->tcps_netstack->netstack_ipsec; 12559 12560 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12561 DROPPER(ipss, ipds_tcp_secure), 12562 &tcps->tcps_dropper); 12563 return (B_FALSE); 12564 } 12565 12566 /* 12567 * XXX This whole routine is currently incorrect. ipl should 12568 * be set to the latch pointer, but is currently not set, so 12569 * we initialize it to NULL to avoid picking up random garbage. 12570 */ 12571 if (ipl == NULL) 12572 return (B_TRUE); 12573 12574 data_mp = first_mp->b_cont; 12575 12576 ii = (ipsec_in_t *)first_mp->b_rptr; 12577 12578 ipst = tcps->tcps_netstack->netstack_ip; 12579 12580 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12581 &counter, tcp->tcp_connp)) { 12582 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12583 return (B_TRUE); 12584 } 12585 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12586 "tcp inbound policy mismatch: %s, packet dropped\n", 12587 reason); 12588 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12589 12590 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12591 &tcps->tcps_dropper); 12592 return (B_FALSE); 12593 } 12594 12595 /* 12596 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12597 * retransmission after a timeout. 12598 * 12599 * To limit the number of duplicate segments, we limit the number of segment 12600 * to be sent in one time to tcp_snd_burst, the burst variable. 12601 */ 12602 static void 12603 tcp_ss_rexmit(tcp_t *tcp) 12604 { 12605 uint32_t snxt; 12606 uint32_t smax; 12607 int32_t win; 12608 int32_t mss; 12609 int32_t off; 12610 int32_t burst = tcp->tcp_snd_burst; 12611 mblk_t *snxt_mp; 12612 tcp_stack_t *tcps = tcp->tcp_tcps; 12613 12614 /* 12615 * Note that tcp_rexmit can be set even though TCP has retransmitted 12616 * all unack'ed segments. 12617 */ 12618 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12619 smax = tcp->tcp_rexmit_max; 12620 snxt = tcp->tcp_rexmit_nxt; 12621 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12622 snxt = tcp->tcp_suna; 12623 } 12624 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12625 win -= snxt - tcp->tcp_suna; 12626 mss = tcp->tcp_mss; 12627 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12628 12629 while (SEQ_LT(snxt, smax) && (win > 0) && 12630 (burst > 0) && (snxt_mp != NULL)) { 12631 mblk_t *xmit_mp; 12632 mblk_t *old_snxt_mp = snxt_mp; 12633 uint32_t cnt = mss; 12634 12635 if (win < cnt) { 12636 cnt = win; 12637 } 12638 if (SEQ_GT(snxt + cnt, smax)) { 12639 cnt = smax - snxt; 12640 } 12641 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12642 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12643 if (xmit_mp == NULL) 12644 return; 12645 12646 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12647 12648 snxt += cnt; 12649 win -= cnt; 12650 /* 12651 * Update the send timestamp to avoid false 12652 * retransmission. 12653 */ 12654 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12655 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12656 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12657 12658 tcp->tcp_rexmit_nxt = snxt; 12659 burst--; 12660 } 12661 /* 12662 * If we have transmitted all we have at the time 12663 * we started the retranmission, we can leave 12664 * the rest of the job to tcp_wput_data(). But we 12665 * need to check the send window first. If the 12666 * win is not 0, go on with tcp_wput_data(). 12667 */ 12668 if (SEQ_LT(snxt, smax) || win == 0) { 12669 return; 12670 } 12671 } 12672 /* Only call tcp_wput_data() if there is data to be sent. */ 12673 if (tcp->tcp_unsent) { 12674 tcp_wput_data(tcp, NULL, B_FALSE); 12675 } 12676 } 12677 12678 /* 12679 * Process all TCP option in SYN segment. Note that this function should 12680 * be called after tcp_adapt_ire() is called so that the necessary info 12681 * from IRE is already set in the tcp structure. 12682 * 12683 * This function sets up the correct tcp_mss value according to the 12684 * MSS option value and our header size. It also sets up the window scale 12685 * and timestamp values, and initialize SACK info blocks. But it does not 12686 * change receive window size after setting the tcp_mss value. The caller 12687 * should do the appropriate change. 12688 */ 12689 void 12690 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12691 { 12692 int options; 12693 tcp_opt_t tcpopt; 12694 uint32_t mss_max; 12695 char *tmp_tcph; 12696 tcp_stack_t *tcps = tcp->tcp_tcps; 12697 12698 tcpopt.tcp = NULL; 12699 options = tcp_parse_options(tcph, &tcpopt); 12700 12701 /* 12702 * Process MSS option. Note that MSS option value does not account 12703 * for IP or TCP options. This means that it is equal to MTU - minimum 12704 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12705 * IPv6. 12706 */ 12707 if (!(options & TCP_OPT_MSS_PRESENT)) { 12708 if (tcp->tcp_ipversion == IPV4_VERSION) 12709 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12710 else 12711 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12712 } else { 12713 if (tcp->tcp_ipversion == IPV4_VERSION) 12714 mss_max = tcps->tcps_mss_max_ipv4; 12715 else 12716 mss_max = tcps->tcps_mss_max_ipv6; 12717 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12718 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12719 else if (tcpopt.tcp_opt_mss > mss_max) 12720 tcpopt.tcp_opt_mss = mss_max; 12721 } 12722 12723 /* Process Window Scale option. */ 12724 if (options & TCP_OPT_WSCALE_PRESENT) { 12725 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12726 tcp->tcp_snd_ws_ok = B_TRUE; 12727 } else { 12728 tcp->tcp_snd_ws = B_FALSE; 12729 tcp->tcp_snd_ws_ok = B_FALSE; 12730 tcp->tcp_rcv_ws = B_FALSE; 12731 } 12732 12733 /* Process Timestamp option. */ 12734 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12735 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12736 tmp_tcph = (char *)tcp->tcp_tcph; 12737 12738 tcp->tcp_snd_ts_ok = B_TRUE; 12739 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12740 tcp->tcp_last_rcv_lbolt = lbolt64; 12741 ASSERT(OK_32PTR(tmp_tcph)); 12742 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12743 12744 /* Fill in our template header with basic timestamp option. */ 12745 tmp_tcph += tcp->tcp_tcp_hdr_len; 12746 tmp_tcph[0] = TCPOPT_NOP; 12747 tmp_tcph[1] = TCPOPT_NOP; 12748 tmp_tcph[2] = TCPOPT_TSTAMP; 12749 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12750 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12751 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12752 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12753 } else { 12754 tcp->tcp_snd_ts_ok = B_FALSE; 12755 } 12756 12757 /* 12758 * Process SACK options. If SACK is enabled for this connection, 12759 * then allocate the SACK info structure. Note the following ways 12760 * when tcp_snd_sack_ok is set to true. 12761 * 12762 * For active connection: in tcp_adapt_ire() called in 12763 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12764 * is checked. 12765 * 12766 * For passive connection: in tcp_adapt_ire() called in 12767 * tcp_accept_comm(). 12768 * 12769 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12770 * That check makes sure that if we did not send a SACK OK option, 12771 * we will not enable SACK for this connection even though the other 12772 * side sends us SACK OK option. For active connection, the SACK 12773 * info structure has already been allocated. So we need to free 12774 * it if SACK is disabled. 12775 */ 12776 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12777 (tcp->tcp_snd_sack_ok || 12778 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12779 /* This should be true only in the passive case. */ 12780 if (tcp->tcp_sack_info == NULL) { 12781 ASSERT(TCP_IS_DETACHED(tcp)); 12782 tcp->tcp_sack_info = 12783 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12784 } 12785 if (tcp->tcp_sack_info == NULL) { 12786 tcp->tcp_snd_sack_ok = B_FALSE; 12787 } else { 12788 tcp->tcp_snd_sack_ok = B_TRUE; 12789 if (tcp->tcp_snd_ts_ok) { 12790 tcp->tcp_max_sack_blk = 3; 12791 } else { 12792 tcp->tcp_max_sack_blk = 4; 12793 } 12794 } 12795 } else { 12796 /* 12797 * Resetting tcp_snd_sack_ok to B_FALSE so that 12798 * no SACK info will be used for this 12799 * connection. This assumes that SACK usage 12800 * permission is negotiated. This may need 12801 * to be changed once this is clarified. 12802 */ 12803 if (tcp->tcp_sack_info != NULL) { 12804 ASSERT(tcp->tcp_notsack_list == NULL); 12805 kmem_cache_free(tcp_sack_info_cache, 12806 tcp->tcp_sack_info); 12807 tcp->tcp_sack_info = NULL; 12808 } 12809 tcp->tcp_snd_sack_ok = B_FALSE; 12810 } 12811 12812 /* 12813 * Now we know the exact TCP/IP header length, subtract 12814 * that from tcp_mss to get our side's MSS. 12815 */ 12816 tcp->tcp_mss -= tcp->tcp_hdr_len; 12817 /* 12818 * Here we assume that the other side's header size will be equal to 12819 * our header size. We calculate the real MSS accordingly. Need to 12820 * take into additional stuffs IPsec puts in. 12821 * 12822 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12823 */ 12824 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12825 ((tcp->tcp_ipversion == IPV4_VERSION ? 12826 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12827 12828 /* 12829 * Set MSS to the smaller one of both ends of the connection. 12830 * We should not have called tcp_mss_set() before, but our 12831 * side of the MSS should have been set to a proper value 12832 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12833 * STREAM head parameters properly. 12834 * 12835 * If we have a larger-than-16-bit window but the other side 12836 * didn't want to do window scale, tcp_rwnd_set() will take 12837 * care of that. 12838 */ 12839 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12840 } 12841 12842 /* 12843 * Sends the T_CONN_IND to the listener. The caller calls this 12844 * functions via squeue to get inside the listener's perimeter 12845 * once the 3 way hand shake is done a T_CONN_IND needs to be 12846 * sent. As an optimization, the caller can call this directly 12847 * if listener's perimeter is same as eager's. 12848 */ 12849 /* ARGSUSED */ 12850 void 12851 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12852 { 12853 conn_t *lconnp = (conn_t *)arg; 12854 tcp_t *listener = lconnp->conn_tcp; 12855 tcp_t *tcp; 12856 struct T_conn_ind *conn_ind; 12857 ipaddr_t *addr_cache; 12858 boolean_t need_send_conn_ind = B_FALSE; 12859 tcp_stack_t *tcps = listener->tcp_tcps; 12860 12861 /* retrieve the eager */ 12862 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12863 ASSERT(conn_ind->OPT_offset != 0 && 12864 conn_ind->OPT_length == sizeof (intptr_t)); 12865 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12866 conn_ind->OPT_length); 12867 12868 /* 12869 * TLI/XTI applications will get confused by 12870 * sending eager as an option since it violates 12871 * the option semantics. So remove the eager as 12872 * option since TLI/XTI app doesn't need it anyway. 12873 */ 12874 if (!TCP_IS_SOCKET(listener)) { 12875 conn_ind->OPT_length = 0; 12876 conn_ind->OPT_offset = 0; 12877 } 12878 if (listener->tcp_state == TCPS_CLOSED || 12879 TCP_IS_DETACHED(listener)) { 12880 /* 12881 * If listener has closed, it would have caused a 12882 * a cleanup/blowoff to happen for the eager. We 12883 * just need to return. 12884 */ 12885 freemsg(mp); 12886 return; 12887 } 12888 12889 12890 /* 12891 * if the conn_req_q is full defer passing up the 12892 * T_CONN_IND until space is availabe after t_accept() 12893 * processing 12894 */ 12895 mutex_enter(&listener->tcp_eager_lock); 12896 12897 /* 12898 * Take the eager out, if it is in the list of droppable eagers 12899 * as we are here because the 3W handshake is over. 12900 */ 12901 MAKE_UNDROPPABLE(tcp); 12902 12903 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12904 tcp_t *tail; 12905 12906 /* 12907 * The eager already has an extra ref put in tcp_rput_data 12908 * so that it stays till accept comes back even though it 12909 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12910 */ 12911 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12912 listener->tcp_conn_req_cnt_q0--; 12913 listener->tcp_conn_req_cnt_q++; 12914 12915 /* Move from SYN_RCVD to ESTABLISHED list */ 12916 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12917 tcp->tcp_eager_prev_q0; 12918 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12919 tcp->tcp_eager_next_q0; 12920 tcp->tcp_eager_prev_q0 = NULL; 12921 tcp->tcp_eager_next_q0 = NULL; 12922 12923 /* 12924 * Insert at end of the queue because sockfs 12925 * sends down T_CONN_RES in chronological 12926 * order. Leaving the older conn indications 12927 * at front of the queue helps reducing search 12928 * time. 12929 */ 12930 tail = listener->tcp_eager_last_q; 12931 if (tail != NULL) 12932 tail->tcp_eager_next_q = tcp; 12933 else 12934 listener->tcp_eager_next_q = tcp; 12935 listener->tcp_eager_last_q = tcp; 12936 tcp->tcp_eager_next_q = NULL; 12937 /* 12938 * Delay sending up the T_conn_ind until we are 12939 * done with the eager. Once we have have sent up 12940 * the T_conn_ind, the accept can potentially complete 12941 * any time and release the refhold we have on the eager. 12942 */ 12943 need_send_conn_ind = B_TRUE; 12944 } else { 12945 /* 12946 * Defer connection on q0 and set deferred 12947 * connection bit true 12948 */ 12949 tcp->tcp_conn_def_q0 = B_TRUE; 12950 12951 /* take tcp out of q0 ... */ 12952 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12953 tcp->tcp_eager_next_q0; 12954 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12955 tcp->tcp_eager_prev_q0; 12956 12957 /* ... and place it at the end of q0 */ 12958 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12959 tcp->tcp_eager_next_q0 = listener; 12960 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12961 listener->tcp_eager_prev_q0 = tcp; 12962 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12963 } 12964 12965 /* we have timed out before */ 12966 if (tcp->tcp_syn_rcvd_timeout != 0) { 12967 tcp->tcp_syn_rcvd_timeout = 0; 12968 listener->tcp_syn_rcvd_timeout--; 12969 if (listener->tcp_syn_defense && 12970 listener->tcp_syn_rcvd_timeout <= 12971 (tcps->tcps_conn_req_max_q0 >> 5) && 12972 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12973 listener->tcp_last_rcv_lbolt)) { 12974 /* 12975 * Turn off the defense mode if we 12976 * believe the SYN attack is over. 12977 */ 12978 listener->tcp_syn_defense = B_FALSE; 12979 if (listener->tcp_ip_addr_cache) { 12980 kmem_free((void *)listener->tcp_ip_addr_cache, 12981 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12982 listener->tcp_ip_addr_cache = NULL; 12983 } 12984 } 12985 } 12986 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12987 if (addr_cache != NULL) { 12988 /* 12989 * We have finished a 3-way handshake with this 12990 * remote host. This proves the IP addr is good. 12991 * Cache it! 12992 */ 12993 addr_cache[IP_ADDR_CACHE_HASH( 12994 tcp->tcp_remote)] = tcp->tcp_remote; 12995 } 12996 mutex_exit(&listener->tcp_eager_lock); 12997 if (need_send_conn_ind) 12998 putnext(listener->tcp_rq, mp); 12999 } 13000 13001 mblk_t * 13002 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 13003 uint_t *ifindexp, ip6_pkt_t *ippp) 13004 { 13005 ip_pktinfo_t *pinfo; 13006 ip6_t *ip6h; 13007 uchar_t *rptr; 13008 mblk_t *first_mp = mp; 13009 boolean_t mctl_present = B_FALSE; 13010 uint_t ifindex = 0; 13011 ip6_pkt_t ipp; 13012 uint_t ipvers; 13013 uint_t ip_hdr_len; 13014 tcp_stack_t *tcps = tcp->tcp_tcps; 13015 13016 rptr = mp->b_rptr; 13017 ASSERT(OK_32PTR(rptr)); 13018 ASSERT(tcp != NULL); 13019 ipp.ipp_fields = 0; 13020 13021 switch DB_TYPE(mp) { 13022 case M_CTL: 13023 mp = mp->b_cont; 13024 if (mp == NULL) { 13025 freemsg(first_mp); 13026 return (NULL); 13027 } 13028 if (DB_TYPE(mp) != M_DATA) { 13029 freemsg(first_mp); 13030 return (NULL); 13031 } 13032 mctl_present = B_TRUE; 13033 break; 13034 case M_DATA: 13035 break; 13036 default: 13037 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 13038 freemsg(mp); 13039 return (NULL); 13040 } 13041 ipvers = IPH_HDR_VERSION(rptr); 13042 if (ipvers == IPV4_VERSION) { 13043 if (tcp == NULL) { 13044 ip_hdr_len = IPH_HDR_LENGTH(rptr); 13045 goto done; 13046 } 13047 13048 ipp.ipp_fields |= IPPF_HOPLIMIT; 13049 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 13050 13051 /* 13052 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 13053 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 13054 */ 13055 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 13056 mctl_present) { 13057 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 13058 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 13059 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 13060 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 13061 ipp.ipp_fields |= IPPF_IFINDEX; 13062 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 13063 ifindex = pinfo->ip_pkt_ifindex; 13064 } 13065 freeb(first_mp); 13066 mctl_present = B_FALSE; 13067 } 13068 ip_hdr_len = IPH_HDR_LENGTH(rptr); 13069 } else { 13070 ip6h = (ip6_t *)rptr; 13071 13072 ASSERT(ipvers == IPV6_VERSION); 13073 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 13074 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 13075 ipp.ipp_hoplimit = ip6h->ip6_hops; 13076 13077 if (ip6h->ip6_nxt != IPPROTO_TCP) { 13078 uint8_t nexthdrp; 13079 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13080 13081 /* Look for ifindex information */ 13082 if (ip6h->ip6_nxt == IPPROTO_RAW) { 13083 ip6i_t *ip6i = (ip6i_t *)ip6h; 13084 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 13085 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13086 freemsg(first_mp); 13087 return (NULL); 13088 } 13089 13090 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 13091 ASSERT(ip6i->ip6i_ifindex != 0); 13092 ipp.ipp_fields |= IPPF_IFINDEX; 13093 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 13094 ifindex = ip6i->ip6i_ifindex; 13095 } 13096 rptr = (uchar_t *)&ip6i[1]; 13097 mp->b_rptr = rptr; 13098 if (rptr == mp->b_wptr) { 13099 mblk_t *mp1; 13100 mp1 = mp->b_cont; 13101 freeb(mp); 13102 mp = mp1; 13103 rptr = mp->b_rptr; 13104 } 13105 if (MBLKL(mp) < IPV6_HDR_LEN + 13106 sizeof (tcph_t)) { 13107 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13108 freemsg(first_mp); 13109 return (NULL); 13110 } 13111 ip6h = (ip6_t *)rptr; 13112 } 13113 13114 /* 13115 * Find any potentially interesting extension headers 13116 * as well as the length of the IPv6 + extension 13117 * headers. 13118 */ 13119 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 13120 /* Verify if this is a TCP packet */ 13121 if (nexthdrp != IPPROTO_TCP) { 13122 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13123 freemsg(first_mp); 13124 return (NULL); 13125 } 13126 } else { 13127 ip_hdr_len = IPV6_HDR_LEN; 13128 } 13129 } 13130 13131 done: 13132 if (ipversp != NULL) 13133 *ipversp = ipvers; 13134 if (ip_hdr_lenp != NULL) 13135 *ip_hdr_lenp = ip_hdr_len; 13136 if (ippp != NULL) 13137 *ippp = ipp; 13138 if (ifindexp != NULL) 13139 *ifindexp = ifindex; 13140 if (mctl_present) { 13141 freeb(first_mp); 13142 } 13143 return (mp); 13144 } 13145 13146 /* 13147 * Handle M_DATA messages from IP. Its called directly from IP via 13148 * squeue for AF_INET type sockets fast path. No M_CTL are expected 13149 * in this path. 13150 * 13151 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 13152 * v4 and v6), we are called through tcp_input() and a M_CTL can 13153 * be present for options but tcp_find_pktinfo() deals with it. We 13154 * only expect M_DATA packets after tcp_find_pktinfo() is done. 13155 * 13156 * The first argument is always the connp/tcp to which the mp belongs. 13157 * There are no exceptions to this rule. The caller has already put 13158 * a reference on this connp/tcp and once tcp_rput_data() returns, 13159 * the squeue will do the refrele. 13160 * 13161 * The TH_SYN for the listener directly go to tcp_conn_request via 13162 * squeue. 13163 * 13164 * sqp: NULL = recursive, sqp != NULL means called from squeue 13165 */ 13166 void 13167 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 13168 { 13169 int32_t bytes_acked; 13170 int32_t gap; 13171 mblk_t *mp1; 13172 uint_t flags; 13173 uint32_t new_swnd = 0; 13174 uchar_t *iphdr; 13175 uchar_t *rptr; 13176 int32_t rgap; 13177 uint32_t seg_ack; 13178 int seg_len; 13179 uint_t ip_hdr_len; 13180 uint32_t seg_seq; 13181 tcph_t *tcph; 13182 int urp; 13183 tcp_opt_t tcpopt; 13184 uint_t ipvers; 13185 ip6_pkt_t ipp; 13186 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 13187 uint32_t cwnd; 13188 uint32_t add; 13189 int npkt; 13190 int mss; 13191 conn_t *connp = (conn_t *)arg; 13192 squeue_t *sqp = (squeue_t *)arg2; 13193 tcp_t *tcp = connp->conn_tcp; 13194 tcp_stack_t *tcps = tcp->tcp_tcps; 13195 13196 /* 13197 * RST from fused tcp loopback peer should trigger an unfuse. 13198 */ 13199 if (tcp->tcp_fused) { 13200 TCP_STAT(tcps, tcp_fusion_aborted); 13201 tcp_unfuse(tcp); 13202 } 13203 13204 iphdr = mp->b_rptr; 13205 rptr = mp->b_rptr; 13206 ASSERT(OK_32PTR(rptr)); 13207 13208 /* 13209 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 13210 * processing here. For rest call tcp_find_pktinfo to fill up the 13211 * necessary information. 13212 */ 13213 if (IPCL_IS_TCP4(connp)) { 13214 ipvers = IPV4_VERSION; 13215 ip_hdr_len = IPH_HDR_LENGTH(rptr); 13216 } else { 13217 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 13218 NULL, &ipp); 13219 if (mp == NULL) { 13220 TCP_STAT(tcps, tcp_rput_v6_error); 13221 return; 13222 } 13223 iphdr = mp->b_rptr; 13224 rptr = mp->b_rptr; 13225 } 13226 ASSERT(DB_TYPE(mp) == M_DATA); 13227 13228 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13229 seg_seq = ABE32_TO_U32(tcph->th_seq); 13230 seg_ack = ABE32_TO_U32(tcph->th_ack); 13231 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 13232 seg_len = (int)(mp->b_wptr - rptr) - 13233 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 13234 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 13235 do { 13236 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 13237 (uintptr_t)INT_MAX); 13238 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 13239 } while ((mp1 = mp1->b_cont) != NULL && 13240 mp1->b_datap->db_type == M_DATA); 13241 } 13242 13243 if (tcp->tcp_state == TCPS_TIME_WAIT) { 13244 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 13245 seg_len, tcph); 13246 return; 13247 } 13248 13249 if (sqp != NULL) { 13250 /* 13251 * This is the correct place to update tcp_last_recv_time. Note 13252 * that it is also updated for tcp structure that belongs to 13253 * global and listener queues which do not really need updating. 13254 * But that should not cause any harm. And it is updated for 13255 * all kinds of incoming segments, not only for data segments. 13256 */ 13257 tcp->tcp_last_recv_time = lbolt; 13258 } 13259 13260 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 13261 13262 BUMP_LOCAL(tcp->tcp_ibsegs); 13263 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 13264 13265 if ((flags & TH_URG) && sqp != NULL) { 13266 /* 13267 * TCP can't handle urgent pointers that arrive before 13268 * the connection has been accept()ed since it can't 13269 * buffer OOB data. Discard segment if this happens. 13270 * 13271 * We can't just rely on a non-null tcp_listener to indicate 13272 * that the accept() has completed since unlinking of the 13273 * eager and completion of the accept are not atomic. 13274 * tcp_detached, when it is not set (B_FALSE) indicates 13275 * that the accept() has completed. 13276 * 13277 * Nor can it reassemble urgent pointers, so discard 13278 * if it's not the next segment expected. 13279 * 13280 * Otherwise, collapse chain into one mblk (discard if 13281 * that fails). This makes sure the headers, retransmitted 13282 * data, and new data all are in the same mblk. 13283 */ 13284 ASSERT(mp != NULL); 13285 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 13286 freemsg(mp); 13287 return; 13288 } 13289 /* Update pointers into message */ 13290 iphdr = rptr = mp->b_rptr; 13291 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13292 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 13293 /* 13294 * Since we can't handle any data with this urgent 13295 * pointer that is out of sequence, we expunge 13296 * the data. This allows us to still register 13297 * the urgent mark and generate the M_PCSIG, 13298 * which we can do. 13299 */ 13300 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13301 seg_len = 0; 13302 } 13303 } 13304 13305 switch (tcp->tcp_state) { 13306 case TCPS_SYN_SENT: 13307 if (flags & TH_ACK) { 13308 /* 13309 * Note that our stack cannot send data before a 13310 * connection is established, therefore the 13311 * following check is valid. Otherwise, it has 13312 * to be changed. 13313 */ 13314 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 13315 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13316 freemsg(mp); 13317 if (flags & TH_RST) 13318 return; 13319 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 13320 tcp, seg_ack, 0, TH_RST); 13321 return; 13322 } 13323 ASSERT(tcp->tcp_suna + 1 == seg_ack); 13324 } 13325 if (flags & TH_RST) { 13326 freemsg(mp); 13327 if (flags & TH_ACK) 13328 (void) tcp_clean_death(tcp, 13329 ECONNREFUSED, 13); 13330 return; 13331 } 13332 if (!(flags & TH_SYN)) { 13333 freemsg(mp); 13334 return; 13335 } 13336 13337 /* Process all TCP options. */ 13338 tcp_process_options(tcp, tcph); 13339 /* 13340 * The following changes our rwnd to be a multiple of the 13341 * MIN(peer MSS, our MSS) for performance reason. 13342 */ 13343 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 13344 tcp->tcp_mss)); 13345 13346 /* Is the other end ECN capable? */ 13347 if (tcp->tcp_ecn_ok) { 13348 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 13349 tcp->tcp_ecn_ok = B_FALSE; 13350 } 13351 } 13352 /* 13353 * Clear ECN flags because it may interfere with later 13354 * processing. 13355 */ 13356 flags &= ~(TH_ECE|TH_CWR); 13357 13358 tcp->tcp_irs = seg_seq; 13359 tcp->tcp_rack = seg_seq; 13360 tcp->tcp_rnxt = seg_seq + 1; 13361 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13362 if (!TCP_IS_DETACHED(tcp)) { 13363 /* Allocate room for SACK options if needed. */ 13364 if (tcp->tcp_snd_sack_ok) { 13365 (void) mi_set_sth_wroff(tcp->tcp_rq, 13366 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 13367 (tcp->tcp_loopback ? 0 : 13368 tcps->tcps_wroff_xtra)); 13369 } else { 13370 (void) mi_set_sth_wroff(tcp->tcp_rq, 13371 tcp->tcp_hdr_len + 13372 (tcp->tcp_loopback ? 0 : 13373 tcps->tcps_wroff_xtra)); 13374 } 13375 } 13376 if (flags & TH_ACK) { 13377 /* 13378 * If we can't get the confirmation upstream, pretend 13379 * we didn't even see this one. 13380 * 13381 * XXX: how can we pretend we didn't see it if we 13382 * have updated rnxt et. al. 13383 * 13384 * For loopback we defer sending up the T_CONN_CON 13385 * until after some checks below. 13386 */ 13387 mp1 = NULL; 13388 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13389 tcp->tcp_loopback ? &mp1 : NULL)) { 13390 freemsg(mp); 13391 return; 13392 } 13393 /* SYN was acked - making progress */ 13394 if (tcp->tcp_ipversion == IPV6_VERSION) 13395 tcp->tcp_ip_forward_progress = B_TRUE; 13396 13397 /* One for the SYN */ 13398 tcp->tcp_suna = tcp->tcp_iss + 1; 13399 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13400 tcp->tcp_state = TCPS_ESTABLISHED; 13401 13402 /* 13403 * If SYN was retransmitted, need to reset all 13404 * retransmission info. This is because this 13405 * segment will be treated as a dup ACK. 13406 */ 13407 if (tcp->tcp_rexmit) { 13408 tcp->tcp_rexmit = B_FALSE; 13409 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13410 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13411 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13412 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13413 tcp->tcp_ms_we_have_waited = 0; 13414 13415 /* 13416 * Set tcp_cwnd back to 1 MSS, per 13417 * recommendation from 13418 * draft-floyd-incr-init-win-01.txt, 13419 * Increasing TCP's Initial Window. 13420 */ 13421 tcp->tcp_cwnd = tcp->tcp_mss; 13422 } 13423 13424 tcp->tcp_swl1 = seg_seq; 13425 tcp->tcp_swl2 = seg_ack; 13426 13427 new_swnd = BE16_TO_U16(tcph->th_win); 13428 tcp->tcp_swnd = new_swnd; 13429 if (new_swnd > tcp->tcp_max_swnd) 13430 tcp->tcp_max_swnd = new_swnd; 13431 13432 /* 13433 * Always send the three-way handshake ack immediately 13434 * in order to make the connection complete as soon as 13435 * possible on the accepting host. 13436 */ 13437 flags |= TH_ACK_NEEDED; 13438 13439 /* 13440 * Special case for loopback. At this point we have 13441 * received SYN-ACK from the remote endpoint. In 13442 * order to ensure that both endpoints reach the 13443 * fused state prior to any data exchange, the final 13444 * ACK needs to be sent before we indicate T_CONN_CON 13445 * to the module upstream. 13446 */ 13447 if (tcp->tcp_loopback) { 13448 mblk_t *ack_mp; 13449 13450 ASSERT(!tcp->tcp_unfusable); 13451 ASSERT(mp1 != NULL); 13452 /* 13453 * For loopback, we always get a pure SYN-ACK 13454 * and only need to send back the final ACK 13455 * with no data (this is because the other 13456 * tcp is ours and we don't do T/TCP). This 13457 * final ACK triggers the passive side to 13458 * perform fusion in ESTABLISHED state. 13459 */ 13460 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13461 if (tcp->tcp_ack_tid != 0) { 13462 (void) TCP_TIMER_CANCEL(tcp, 13463 tcp->tcp_ack_tid); 13464 tcp->tcp_ack_tid = 0; 13465 } 13466 TCP_RECORD_TRACE(tcp, ack_mp, 13467 TCP_TRACE_SEND_PKT); 13468 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13469 BUMP_LOCAL(tcp->tcp_obsegs); 13470 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13471 13472 /* Send up T_CONN_CON */ 13473 putnext(tcp->tcp_rq, mp1); 13474 13475 freemsg(mp); 13476 return; 13477 } 13478 /* 13479 * Forget fusion; we need to handle more 13480 * complex cases below. Send the deferred 13481 * T_CONN_CON message upstream and proceed 13482 * as usual. Mark this tcp as not capable 13483 * of fusion. 13484 */ 13485 TCP_STAT(tcps, tcp_fusion_unfusable); 13486 tcp->tcp_unfusable = B_TRUE; 13487 putnext(tcp->tcp_rq, mp1); 13488 } 13489 13490 /* 13491 * Check to see if there is data to be sent. If 13492 * yes, set the transmit flag. Then check to see 13493 * if received data processing needs to be done. 13494 * If not, go straight to xmit_check. This short 13495 * cut is OK as we don't support T/TCP. 13496 */ 13497 if (tcp->tcp_unsent) 13498 flags |= TH_XMIT_NEEDED; 13499 13500 if (seg_len == 0 && !(flags & TH_URG)) { 13501 freemsg(mp); 13502 goto xmit_check; 13503 } 13504 13505 flags &= ~TH_SYN; 13506 seg_seq++; 13507 break; 13508 } 13509 tcp->tcp_state = TCPS_SYN_RCVD; 13510 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13511 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13512 if (mp1) { 13513 DB_CPID(mp1) = tcp->tcp_cpid; 13514 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 13515 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13516 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13517 } 13518 freemsg(mp); 13519 return; 13520 case TCPS_SYN_RCVD: 13521 if (flags & TH_ACK) { 13522 /* 13523 * In this state, a SYN|ACK packet is either bogus 13524 * because the other side must be ACKing our SYN which 13525 * indicates it has seen the ACK for their SYN and 13526 * shouldn't retransmit it or we're crossing SYNs 13527 * on active open. 13528 */ 13529 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13530 freemsg(mp); 13531 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13532 tcp, seg_ack, 0, TH_RST); 13533 return; 13534 } 13535 /* 13536 * NOTE: RFC 793 pg. 72 says this should be 13537 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13538 * but that would mean we have an ack that ignored 13539 * our SYN. 13540 */ 13541 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13542 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13543 freemsg(mp); 13544 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13545 tcp, seg_ack, 0, TH_RST); 13546 return; 13547 } 13548 } 13549 break; 13550 case TCPS_LISTEN: 13551 /* 13552 * Only a TLI listener can come through this path when a 13553 * acceptor is going back to be a listener and a packet 13554 * for the acceptor hits the classifier. For a socket 13555 * listener, this can never happen because a listener 13556 * can never accept connection on itself and hence a 13557 * socket acceptor can not go back to being a listener. 13558 */ 13559 ASSERT(!TCP_IS_SOCKET(tcp)); 13560 /*FALLTHRU*/ 13561 case TCPS_CLOSED: 13562 case TCPS_BOUND: { 13563 conn_t *new_connp; 13564 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13565 13566 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13567 if (new_connp != NULL) { 13568 tcp_reinput(new_connp, mp, connp->conn_sqp); 13569 return; 13570 } 13571 /* We failed to classify. For now just drop the packet */ 13572 freemsg(mp); 13573 return; 13574 } 13575 case TCPS_IDLE: 13576 /* 13577 * Handle the case where the tcp_clean_death() has happened 13578 * on a connection (application hasn't closed yet) but a packet 13579 * was already queued on squeue before tcp_clean_death() 13580 * was processed. Calling tcp_clean_death() twice on same 13581 * connection can result in weird behaviour. 13582 */ 13583 freemsg(mp); 13584 return; 13585 default: 13586 break; 13587 } 13588 13589 /* 13590 * Already on the correct queue/perimeter. 13591 * If this is a detached connection and not an eager 13592 * connection hanging off a listener then new data 13593 * (past the FIN) will cause a reset. 13594 * We do a special check here where it 13595 * is out of the main line, rather than check 13596 * if we are detached every time we see new 13597 * data down below. 13598 */ 13599 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13600 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13601 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13602 TCP_RECORD_TRACE(tcp, 13603 mp, TCP_TRACE_RECV_PKT); 13604 13605 freemsg(mp); 13606 /* 13607 * This could be an SSL closure alert. We're detached so just 13608 * acknowledge it this last time. 13609 */ 13610 if (tcp->tcp_kssl_ctx != NULL) { 13611 kssl_release_ctx(tcp->tcp_kssl_ctx); 13612 tcp->tcp_kssl_ctx = NULL; 13613 13614 tcp->tcp_rnxt += seg_len; 13615 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13616 flags |= TH_ACK_NEEDED; 13617 goto ack_check; 13618 } 13619 13620 tcp_xmit_ctl("new data when detached", tcp, 13621 tcp->tcp_snxt, 0, TH_RST); 13622 (void) tcp_clean_death(tcp, EPROTO, 12); 13623 return; 13624 } 13625 13626 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13627 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13628 new_swnd = BE16_TO_U16(tcph->th_win) << 13629 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13630 13631 if (tcp->tcp_snd_ts_ok) { 13632 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13633 /* 13634 * This segment is not acceptable. 13635 * Drop it and send back an ACK. 13636 */ 13637 freemsg(mp); 13638 flags |= TH_ACK_NEEDED; 13639 goto ack_check; 13640 } 13641 } else if (tcp->tcp_snd_sack_ok) { 13642 ASSERT(tcp->tcp_sack_info != NULL); 13643 tcpopt.tcp = tcp; 13644 /* 13645 * SACK info in already updated in tcp_parse_options. Ignore 13646 * all other TCP options... 13647 */ 13648 (void) tcp_parse_options(tcph, &tcpopt); 13649 } 13650 try_again:; 13651 mss = tcp->tcp_mss; 13652 gap = seg_seq - tcp->tcp_rnxt; 13653 rgap = tcp->tcp_rwnd - (gap + seg_len); 13654 /* 13655 * gap is the amount of sequence space between what we expect to see 13656 * and what we got for seg_seq. A positive value for gap means 13657 * something got lost. A negative value means we got some old stuff. 13658 */ 13659 if (gap < 0) { 13660 /* Old stuff present. Is the SYN in there? */ 13661 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13662 (seg_len != 0)) { 13663 flags &= ~TH_SYN; 13664 seg_seq++; 13665 urp--; 13666 /* Recompute the gaps after noting the SYN. */ 13667 goto try_again; 13668 } 13669 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13670 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13671 (seg_len > -gap ? -gap : seg_len)); 13672 /* Remove the old stuff from seg_len. */ 13673 seg_len += gap; 13674 /* 13675 * Anything left? 13676 * Make sure to check for unack'd FIN when rest of data 13677 * has been previously ack'd. 13678 */ 13679 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13680 /* 13681 * Resets are only valid if they lie within our offered 13682 * window. If the RST bit is set, we just ignore this 13683 * segment. 13684 */ 13685 if (flags & TH_RST) { 13686 freemsg(mp); 13687 return; 13688 } 13689 13690 /* 13691 * The arriving of dup data packets indicate that we 13692 * may have postponed an ack for too long, or the other 13693 * side's RTT estimate is out of shape. Start acking 13694 * more often. 13695 */ 13696 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13697 tcp->tcp_rack_cnt >= 1 && 13698 tcp->tcp_rack_abs_max > 2) { 13699 tcp->tcp_rack_abs_max--; 13700 } 13701 tcp->tcp_rack_cur_max = 1; 13702 13703 /* 13704 * This segment is "unacceptable". None of its 13705 * sequence space lies within our advertized window. 13706 * 13707 * Adjust seg_len to the original value for tracing. 13708 */ 13709 seg_len -= gap; 13710 if (tcp->tcp_debug) { 13711 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13712 "tcp_rput: unacceptable, gap %d, rgap %d, " 13713 "flags 0x%x, seg_seq %u, seg_ack %u, " 13714 "seg_len %d, rnxt %u, snxt %u, %s", 13715 gap, rgap, flags, seg_seq, seg_ack, 13716 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13717 tcp_display(tcp, NULL, 13718 DISP_ADDR_AND_PORT)); 13719 } 13720 13721 /* 13722 * Arrange to send an ACK in response to the 13723 * unacceptable segment per RFC 793 page 69. There 13724 * is only one small difference between ours and the 13725 * acceptability test in the RFC - we accept ACK-only 13726 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13727 * will be generated. 13728 * 13729 * Note that we have to ACK an ACK-only packet at least 13730 * for stacks that send 0-length keep-alives with 13731 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13732 * section 4.2.3.6. As long as we don't ever generate 13733 * an unacceptable packet in response to an incoming 13734 * packet that is unacceptable, it should not cause 13735 * "ACK wars". 13736 */ 13737 flags |= TH_ACK_NEEDED; 13738 13739 /* 13740 * Continue processing this segment in order to use the 13741 * ACK information it contains, but skip all other 13742 * sequence-number processing. Processing the ACK 13743 * information is necessary in order to 13744 * re-synchronize connections that may have lost 13745 * synchronization. 13746 * 13747 * We clear seg_len and flag fields related to 13748 * sequence number processing as they are not 13749 * to be trusted for an unacceptable segment. 13750 */ 13751 seg_len = 0; 13752 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13753 goto process_ack; 13754 } 13755 13756 /* Fix seg_seq, and chew the gap off the front. */ 13757 seg_seq = tcp->tcp_rnxt; 13758 urp += gap; 13759 do { 13760 mblk_t *mp2; 13761 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13762 (uintptr_t)UINT_MAX); 13763 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13764 if (gap > 0) { 13765 mp->b_rptr = mp->b_wptr - gap; 13766 break; 13767 } 13768 mp2 = mp; 13769 mp = mp->b_cont; 13770 freeb(mp2); 13771 } while (gap < 0); 13772 /* 13773 * If the urgent data has already been acknowledged, we 13774 * should ignore TH_URG below 13775 */ 13776 if (urp < 0) 13777 flags &= ~TH_URG; 13778 } 13779 /* 13780 * rgap is the amount of stuff received out of window. A negative 13781 * value is the amount out of window. 13782 */ 13783 if (rgap < 0) { 13784 mblk_t *mp2; 13785 13786 if (tcp->tcp_rwnd == 0) { 13787 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13788 } else { 13789 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13790 UPDATE_MIB(&tcps->tcps_mib, 13791 tcpInDataPastWinBytes, -rgap); 13792 } 13793 13794 /* 13795 * seg_len does not include the FIN, so if more than 13796 * just the FIN is out of window, we act like we don't 13797 * see it. (If just the FIN is out of window, rgap 13798 * will be zero and we will go ahead and acknowledge 13799 * the FIN.) 13800 */ 13801 flags &= ~TH_FIN; 13802 13803 /* Fix seg_len and make sure there is something left. */ 13804 seg_len += rgap; 13805 if (seg_len <= 0) { 13806 /* 13807 * Resets are only valid if they lie within our offered 13808 * window. If the RST bit is set, we just ignore this 13809 * segment. 13810 */ 13811 if (flags & TH_RST) { 13812 freemsg(mp); 13813 return; 13814 } 13815 13816 /* Per RFC 793, we need to send back an ACK. */ 13817 flags |= TH_ACK_NEEDED; 13818 13819 /* 13820 * Send SIGURG as soon as possible i.e. even 13821 * if the TH_URG was delivered in a window probe 13822 * packet (which will be unacceptable). 13823 * 13824 * We generate a signal if none has been generated 13825 * for this connection or if this is a new urgent 13826 * byte. Also send a zero-length "unmarked" message 13827 * to inform SIOCATMARK that this is not the mark. 13828 * 13829 * tcp_urp_last_valid is cleared when the T_exdata_ind 13830 * is sent up. This plus the check for old data 13831 * (gap >= 0) handles the wraparound of the sequence 13832 * number space without having to always track the 13833 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13834 * this max in its rcv_up variable). 13835 * 13836 * This prevents duplicate SIGURGS due to a "late" 13837 * zero-window probe when the T_EXDATA_IND has already 13838 * been sent up. 13839 */ 13840 if ((flags & TH_URG) && 13841 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13842 tcp->tcp_urp_last))) { 13843 mp1 = allocb(0, BPRI_MED); 13844 if (mp1 == NULL) { 13845 freemsg(mp); 13846 return; 13847 } 13848 if (!TCP_IS_DETACHED(tcp) && 13849 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13850 SIGURG)) { 13851 /* Try again on the rexmit. */ 13852 freemsg(mp1); 13853 freemsg(mp); 13854 return; 13855 } 13856 /* 13857 * If the next byte would be the mark 13858 * then mark with MARKNEXT else mark 13859 * with NOTMARKNEXT. 13860 */ 13861 if (gap == 0 && urp == 0) 13862 mp1->b_flag |= MSGMARKNEXT; 13863 else 13864 mp1->b_flag |= MSGNOTMARKNEXT; 13865 freemsg(tcp->tcp_urp_mark_mp); 13866 tcp->tcp_urp_mark_mp = mp1; 13867 flags |= TH_SEND_URP_MARK; 13868 tcp->tcp_urp_last_valid = B_TRUE; 13869 tcp->tcp_urp_last = urp + seg_seq; 13870 } 13871 /* 13872 * If this is a zero window probe, continue to 13873 * process the ACK part. But we need to set seg_len 13874 * to 0 to avoid data processing. Otherwise just 13875 * drop the segment and send back an ACK. 13876 */ 13877 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13878 flags &= ~(TH_SYN | TH_URG); 13879 seg_len = 0; 13880 goto process_ack; 13881 } else { 13882 freemsg(mp); 13883 goto ack_check; 13884 } 13885 } 13886 /* Pitch out of window stuff off the end. */ 13887 rgap = seg_len; 13888 mp2 = mp; 13889 do { 13890 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13891 (uintptr_t)INT_MAX); 13892 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13893 if (rgap < 0) { 13894 mp2->b_wptr += rgap; 13895 if ((mp1 = mp2->b_cont) != NULL) { 13896 mp2->b_cont = NULL; 13897 freemsg(mp1); 13898 } 13899 break; 13900 } 13901 } while ((mp2 = mp2->b_cont) != NULL); 13902 } 13903 ok:; 13904 /* 13905 * TCP should check ECN info for segments inside the window only. 13906 * Therefore the check should be done here. 13907 */ 13908 if (tcp->tcp_ecn_ok) { 13909 if (flags & TH_CWR) { 13910 tcp->tcp_ecn_echo_on = B_FALSE; 13911 } 13912 /* 13913 * Note that both ECN_CE and CWR can be set in the 13914 * same segment. In this case, we once again turn 13915 * on ECN_ECHO. 13916 */ 13917 if (tcp->tcp_ipversion == IPV4_VERSION) { 13918 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13919 13920 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13921 tcp->tcp_ecn_echo_on = B_TRUE; 13922 } 13923 } else { 13924 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13925 13926 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13927 htonl(IPH_ECN_CE << 20)) { 13928 tcp->tcp_ecn_echo_on = B_TRUE; 13929 } 13930 } 13931 } 13932 13933 /* 13934 * Check whether we can update tcp_ts_recent. This test is 13935 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13936 * Extensions for High Performance: An Update", Internet Draft. 13937 */ 13938 if (tcp->tcp_snd_ts_ok && 13939 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13940 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13941 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13942 tcp->tcp_last_rcv_lbolt = lbolt64; 13943 } 13944 13945 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13946 /* 13947 * FIN in an out of order segment. We record this in 13948 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13949 * Clear the FIN so that any check on FIN flag will fail. 13950 * Remember that FIN also counts in the sequence number 13951 * space. So we need to ack out of order FIN only segments. 13952 */ 13953 if (flags & TH_FIN) { 13954 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13955 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13956 flags &= ~TH_FIN; 13957 flags |= TH_ACK_NEEDED; 13958 } 13959 if (seg_len > 0) { 13960 /* Fill in the SACK blk list. */ 13961 if (tcp->tcp_snd_sack_ok) { 13962 ASSERT(tcp->tcp_sack_info != NULL); 13963 tcp_sack_insert(tcp->tcp_sack_list, 13964 seg_seq, seg_seq + seg_len, 13965 &(tcp->tcp_num_sack_blk)); 13966 } 13967 13968 /* 13969 * Attempt reassembly and see if we have something 13970 * ready to go. 13971 */ 13972 mp = tcp_reass(tcp, mp, seg_seq); 13973 /* Always ack out of order packets */ 13974 flags |= TH_ACK_NEEDED | TH_PUSH; 13975 if (mp) { 13976 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13977 (uintptr_t)INT_MAX); 13978 seg_len = mp->b_cont ? msgdsize(mp) : 13979 (int)(mp->b_wptr - mp->b_rptr); 13980 seg_seq = tcp->tcp_rnxt; 13981 /* 13982 * A gap is filled and the seq num and len 13983 * of the gap match that of a previously 13984 * received FIN, put the FIN flag back in. 13985 */ 13986 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13987 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13988 flags |= TH_FIN; 13989 tcp->tcp_valid_bits &= 13990 ~TCP_OFO_FIN_VALID; 13991 } 13992 } else { 13993 /* 13994 * Keep going even with NULL mp. 13995 * There may be a useful ACK or something else 13996 * we don't want to miss. 13997 * 13998 * But TCP should not perform fast retransmit 13999 * because of the ack number. TCP uses 14000 * seg_len == 0 to determine if it is a pure 14001 * ACK. And this is not a pure ACK. 14002 */ 14003 seg_len = 0; 14004 ofo_seg = B_TRUE; 14005 } 14006 } 14007 } else if (seg_len > 0) { 14008 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 14009 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 14010 /* 14011 * If an out of order FIN was received before, and the seq 14012 * num and len of the new segment match that of the FIN, 14013 * put the FIN flag back in. 14014 */ 14015 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 14016 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 14017 flags |= TH_FIN; 14018 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 14019 } 14020 } 14021 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 14022 if (flags & TH_RST) { 14023 freemsg(mp); 14024 switch (tcp->tcp_state) { 14025 case TCPS_SYN_RCVD: 14026 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 14027 break; 14028 case TCPS_ESTABLISHED: 14029 case TCPS_FIN_WAIT_1: 14030 case TCPS_FIN_WAIT_2: 14031 case TCPS_CLOSE_WAIT: 14032 (void) tcp_clean_death(tcp, ECONNRESET, 15); 14033 break; 14034 case TCPS_CLOSING: 14035 case TCPS_LAST_ACK: 14036 (void) tcp_clean_death(tcp, 0, 16); 14037 break; 14038 default: 14039 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14040 (void) tcp_clean_death(tcp, ENXIO, 17); 14041 break; 14042 } 14043 return; 14044 } 14045 if (flags & TH_SYN) { 14046 /* 14047 * See RFC 793, Page 71 14048 * 14049 * The seq number must be in the window as it should 14050 * be "fixed" above. If it is outside window, it should 14051 * be already rejected. Note that we allow seg_seq to be 14052 * rnxt + rwnd because we want to accept 0 window probe. 14053 */ 14054 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 14055 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 14056 freemsg(mp); 14057 /* 14058 * If the ACK flag is not set, just use our snxt as the 14059 * seq number of the RST segment. 14060 */ 14061 if (!(flags & TH_ACK)) { 14062 seg_ack = tcp->tcp_snxt; 14063 } 14064 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 14065 TH_RST|TH_ACK); 14066 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14067 (void) tcp_clean_death(tcp, ECONNRESET, 18); 14068 return; 14069 } 14070 /* 14071 * urp could be -1 when the urp field in the packet is 0 14072 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 14073 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 14074 */ 14075 if (flags & TH_URG && urp >= 0) { 14076 if (!tcp->tcp_urp_last_valid || 14077 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 14078 /* 14079 * If we haven't generated the signal yet for this 14080 * urgent pointer value, do it now. Also, send up a 14081 * zero-length M_DATA indicating whether or not this is 14082 * the mark. The latter is not needed when a 14083 * T_EXDATA_IND is sent up. However, if there are 14084 * allocation failures this code relies on the sender 14085 * retransmitting and the socket code for determining 14086 * the mark should not block waiting for the peer to 14087 * transmit. Thus, for simplicity we always send up the 14088 * mark indication. 14089 */ 14090 mp1 = allocb(0, BPRI_MED); 14091 if (mp1 == NULL) { 14092 freemsg(mp); 14093 return; 14094 } 14095 if (!TCP_IS_DETACHED(tcp) && 14096 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 14097 /* Try again on the rexmit. */ 14098 freemsg(mp1); 14099 freemsg(mp); 14100 return; 14101 } 14102 /* 14103 * Mark with NOTMARKNEXT for now. 14104 * The code below will change this to MARKNEXT 14105 * if we are at the mark. 14106 * 14107 * If there are allocation failures (e.g. in dupmsg 14108 * below) the next time tcp_rput_data sees the urgent 14109 * segment it will send up the MSG*MARKNEXT message. 14110 */ 14111 mp1->b_flag |= MSGNOTMARKNEXT; 14112 freemsg(tcp->tcp_urp_mark_mp); 14113 tcp->tcp_urp_mark_mp = mp1; 14114 flags |= TH_SEND_URP_MARK; 14115 #ifdef DEBUG 14116 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14117 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 14118 "last %x, %s", 14119 seg_seq, urp, tcp->tcp_urp_last, 14120 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14121 #endif /* DEBUG */ 14122 tcp->tcp_urp_last_valid = B_TRUE; 14123 tcp->tcp_urp_last = urp + seg_seq; 14124 } else if (tcp->tcp_urp_mark_mp != NULL) { 14125 /* 14126 * An allocation failure prevented the previous 14127 * tcp_rput_data from sending up the allocated 14128 * MSG*MARKNEXT message - send it up this time 14129 * around. 14130 */ 14131 flags |= TH_SEND_URP_MARK; 14132 } 14133 14134 /* 14135 * If the urgent byte is in this segment, make sure that it is 14136 * all by itself. This makes it much easier to deal with the 14137 * possibility of an allocation failure on the T_exdata_ind. 14138 * Note that seg_len is the number of bytes in the segment, and 14139 * urp is the offset into the segment of the urgent byte. 14140 * urp < seg_len means that the urgent byte is in this segment. 14141 */ 14142 if (urp < seg_len) { 14143 if (seg_len != 1) { 14144 uint32_t tmp_rnxt; 14145 /* 14146 * Break it up and feed it back in. 14147 * Re-attach the IP header. 14148 */ 14149 mp->b_rptr = iphdr; 14150 if (urp > 0) { 14151 /* 14152 * There is stuff before the urgent 14153 * byte. 14154 */ 14155 mp1 = dupmsg(mp); 14156 if (!mp1) { 14157 /* 14158 * Trim from urgent byte on. 14159 * The rest will come back. 14160 */ 14161 (void) adjmsg(mp, 14162 urp - seg_len); 14163 tcp_rput_data(connp, 14164 mp, NULL); 14165 return; 14166 } 14167 (void) adjmsg(mp1, urp - seg_len); 14168 /* Feed this piece back in. */ 14169 tmp_rnxt = tcp->tcp_rnxt; 14170 tcp_rput_data(connp, mp1, NULL); 14171 /* 14172 * If the data passed back in was not 14173 * processed (ie: bad ACK) sending 14174 * the remainder back in will cause a 14175 * loop. In this case, drop the 14176 * packet and let the sender try 14177 * sending a good packet. 14178 */ 14179 if (tmp_rnxt == tcp->tcp_rnxt) { 14180 freemsg(mp); 14181 return; 14182 } 14183 } 14184 if (urp != seg_len - 1) { 14185 uint32_t tmp_rnxt; 14186 /* 14187 * There is stuff after the urgent 14188 * byte. 14189 */ 14190 mp1 = dupmsg(mp); 14191 if (!mp1) { 14192 /* 14193 * Trim everything beyond the 14194 * urgent byte. The rest will 14195 * come back. 14196 */ 14197 (void) adjmsg(mp, 14198 urp + 1 - seg_len); 14199 tcp_rput_data(connp, 14200 mp, NULL); 14201 return; 14202 } 14203 (void) adjmsg(mp1, urp + 1 - seg_len); 14204 tmp_rnxt = tcp->tcp_rnxt; 14205 tcp_rput_data(connp, mp1, NULL); 14206 /* 14207 * If the data passed back in was not 14208 * processed (ie: bad ACK) sending 14209 * the remainder back in will cause a 14210 * loop. In this case, drop the 14211 * packet and let the sender try 14212 * sending a good packet. 14213 */ 14214 if (tmp_rnxt == tcp->tcp_rnxt) { 14215 freemsg(mp); 14216 return; 14217 } 14218 } 14219 tcp_rput_data(connp, mp, NULL); 14220 return; 14221 } 14222 /* 14223 * This segment contains only the urgent byte. We 14224 * have to allocate the T_exdata_ind, if we can. 14225 */ 14226 if (!tcp->tcp_urp_mp) { 14227 struct T_exdata_ind *tei; 14228 mp1 = allocb(sizeof (struct T_exdata_ind), 14229 BPRI_MED); 14230 if (!mp1) { 14231 /* 14232 * Sigh... It'll be back. 14233 * Generate any MSG*MARK message now. 14234 */ 14235 freemsg(mp); 14236 seg_len = 0; 14237 if (flags & TH_SEND_URP_MARK) { 14238 14239 14240 ASSERT(tcp->tcp_urp_mark_mp); 14241 tcp->tcp_urp_mark_mp->b_flag &= 14242 ~MSGNOTMARKNEXT; 14243 tcp->tcp_urp_mark_mp->b_flag |= 14244 MSGMARKNEXT; 14245 } 14246 goto ack_check; 14247 } 14248 mp1->b_datap->db_type = M_PROTO; 14249 tei = (struct T_exdata_ind *)mp1->b_rptr; 14250 tei->PRIM_type = T_EXDATA_IND; 14251 tei->MORE_flag = 0; 14252 mp1->b_wptr = (uchar_t *)&tei[1]; 14253 tcp->tcp_urp_mp = mp1; 14254 #ifdef DEBUG 14255 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14256 "tcp_rput: allocated exdata_ind %s", 14257 tcp_display(tcp, NULL, 14258 DISP_PORT_ONLY)); 14259 #endif /* DEBUG */ 14260 /* 14261 * There is no need to send a separate MSG*MARK 14262 * message since the T_EXDATA_IND will be sent 14263 * now. 14264 */ 14265 flags &= ~TH_SEND_URP_MARK; 14266 freemsg(tcp->tcp_urp_mark_mp); 14267 tcp->tcp_urp_mark_mp = NULL; 14268 } 14269 /* 14270 * Now we are all set. On the next putnext upstream, 14271 * tcp_urp_mp will be non-NULL and will get prepended 14272 * to what has to be this piece containing the urgent 14273 * byte. If for any reason we abort this segment below, 14274 * if it comes back, we will have this ready, or it 14275 * will get blown off in close. 14276 */ 14277 } else if (urp == seg_len) { 14278 /* 14279 * The urgent byte is the next byte after this sequence 14280 * number. If there is data it is marked with 14281 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 14282 * since it is not needed. Otherwise, if the code 14283 * above just allocated a zero-length tcp_urp_mark_mp 14284 * message, that message is tagged with MSGMARKNEXT. 14285 * Sending up these MSGMARKNEXT messages makes 14286 * SIOCATMARK work correctly even though 14287 * the T_EXDATA_IND will not be sent up until the 14288 * urgent byte arrives. 14289 */ 14290 if (seg_len != 0) { 14291 flags |= TH_MARKNEXT_NEEDED; 14292 freemsg(tcp->tcp_urp_mark_mp); 14293 tcp->tcp_urp_mark_mp = NULL; 14294 flags &= ~TH_SEND_URP_MARK; 14295 } else if (tcp->tcp_urp_mark_mp != NULL) { 14296 flags |= TH_SEND_URP_MARK; 14297 tcp->tcp_urp_mark_mp->b_flag &= 14298 ~MSGNOTMARKNEXT; 14299 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 14300 } 14301 #ifdef DEBUG 14302 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14303 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 14304 seg_len, flags, 14305 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14306 #endif /* DEBUG */ 14307 } else { 14308 /* Data left until we hit mark */ 14309 #ifdef DEBUG 14310 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14311 "tcp_rput: URP %d bytes left, %s", 14312 urp - seg_len, tcp_display(tcp, NULL, 14313 DISP_PORT_ONLY)); 14314 #endif /* DEBUG */ 14315 } 14316 } 14317 14318 process_ack: 14319 if (!(flags & TH_ACK)) { 14320 freemsg(mp); 14321 goto xmit_check; 14322 } 14323 } 14324 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 14325 14326 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 14327 tcp->tcp_ip_forward_progress = B_TRUE; 14328 if (tcp->tcp_state == TCPS_SYN_RCVD) { 14329 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 14330 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 14331 /* 3-way handshake complete - pass up the T_CONN_IND */ 14332 tcp_t *listener = tcp->tcp_listener; 14333 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 14334 14335 tcp->tcp_tconnind_started = B_TRUE; 14336 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 14337 /* 14338 * We are here means eager is fine but it can 14339 * get a TH_RST at any point between now and till 14340 * accept completes and disappear. We need to 14341 * ensure that reference to eager is valid after 14342 * we get out of eager's perimeter. So we do 14343 * an extra refhold. 14344 */ 14345 CONN_INC_REF(connp); 14346 14347 /* 14348 * The listener also exists because of the refhold 14349 * done in tcp_conn_request. Its possible that it 14350 * might have closed. We will check that once we 14351 * get inside listeners context. 14352 */ 14353 CONN_INC_REF(listener->tcp_connp); 14354 if (listener->tcp_connp->conn_sqp == 14355 connp->conn_sqp) { 14356 tcp_send_conn_ind(listener->tcp_connp, mp, 14357 listener->tcp_connp->conn_sqp); 14358 CONN_DEC_REF(listener->tcp_connp); 14359 } else if (!tcp->tcp_loopback) { 14360 squeue_fill(listener->tcp_connp->conn_sqp, mp, 14361 tcp_send_conn_ind, 14362 listener->tcp_connp, SQTAG_TCP_CONN_IND); 14363 } else { 14364 squeue_enter(listener->tcp_connp->conn_sqp, mp, 14365 tcp_send_conn_ind, listener->tcp_connp, 14366 SQTAG_TCP_CONN_IND); 14367 } 14368 } 14369 14370 if (tcp->tcp_active_open) { 14371 /* 14372 * We are seeing the final ack in the three way 14373 * hand shake of a active open'ed connection 14374 * so we must send up a T_CONN_CON 14375 */ 14376 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14377 freemsg(mp); 14378 return; 14379 } 14380 /* 14381 * Don't fuse the loopback endpoints for 14382 * simultaneous active opens. 14383 */ 14384 if (tcp->tcp_loopback) { 14385 TCP_STAT(tcps, tcp_fusion_unfusable); 14386 tcp->tcp_unfusable = B_TRUE; 14387 } 14388 } 14389 14390 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14391 bytes_acked--; 14392 /* SYN was acked - making progress */ 14393 if (tcp->tcp_ipversion == IPV6_VERSION) 14394 tcp->tcp_ip_forward_progress = B_TRUE; 14395 14396 /* 14397 * If SYN was retransmitted, need to reset all 14398 * retransmission info as this segment will be 14399 * treated as a dup ACK. 14400 */ 14401 if (tcp->tcp_rexmit) { 14402 tcp->tcp_rexmit = B_FALSE; 14403 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14404 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14405 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14406 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14407 tcp->tcp_ms_we_have_waited = 0; 14408 tcp->tcp_cwnd = mss; 14409 } 14410 14411 /* 14412 * We set the send window to zero here. 14413 * This is needed if there is data to be 14414 * processed already on the queue. 14415 * Later (at swnd_update label), the 14416 * "new_swnd > tcp_swnd" condition is satisfied 14417 * the XMIT_NEEDED flag is set in the current 14418 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14419 * called if there is already data on queue in 14420 * this state. 14421 */ 14422 tcp->tcp_swnd = 0; 14423 14424 if (new_swnd > tcp->tcp_max_swnd) 14425 tcp->tcp_max_swnd = new_swnd; 14426 tcp->tcp_swl1 = seg_seq; 14427 tcp->tcp_swl2 = seg_ack; 14428 tcp->tcp_state = TCPS_ESTABLISHED; 14429 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14430 14431 /* Fuse when both sides are in ESTABLISHED state */ 14432 if (tcp->tcp_loopback && do_tcp_fusion) 14433 tcp_fuse(tcp, iphdr, tcph); 14434 14435 } 14436 /* This code follows 4.4BSD-Lite2 mostly. */ 14437 if (bytes_acked < 0) 14438 goto est; 14439 14440 /* 14441 * If TCP is ECN capable and the congestion experience bit is 14442 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14443 * done once per window (or more loosely, per RTT). 14444 */ 14445 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14446 tcp->tcp_cwr = B_FALSE; 14447 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14448 if (!tcp->tcp_cwr) { 14449 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14450 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14451 tcp->tcp_cwnd = npkt * mss; 14452 /* 14453 * If the cwnd is 0, use the timer to clock out 14454 * new segments. This is required by the ECN spec. 14455 */ 14456 if (npkt == 0) { 14457 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14458 /* 14459 * This makes sure that when the ACK comes 14460 * back, we will increase tcp_cwnd by 1 MSS. 14461 */ 14462 tcp->tcp_cwnd_cnt = 0; 14463 } 14464 tcp->tcp_cwr = B_TRUE; 14465 /* 14466 * This marks the end of the current window of in 14467 * flight data. That is why we don't use 14468 * tcp_suna + tcp_swnd. Only data in flight can 14469 * provide ECN info. 14470 */ 14471 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14472 tcp->tcp_ecn_cwr_sent = B_FALSE; 14473 } 14474 } 14475 14476 mp1 = tcp->tcp_xmit_head; 14477 if (bytes_acked == 0) { 14478 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14479 int dupack_cnt; 14480 14481 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14482 /* 14483 * Fast retransmit. When we have seen exactly three 14484 * identical ACKs while we have unacked data 14485 * outstanding we take it as a hint that our peer 14486 * dropped something. 14487 * 14488 * If TCP is retransmitting, don't do fast retransmit. 14489 */ 14490 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14491 ! tcp->tcp_rexmit) { 14492 /* Do Limited Transmit */ 14493 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14494 tcps->tcps_dupack_fast_retransmit) { 14495 /* 14496 * RFC 3042 14497 * 14498 * What we need to do is temporarily 14499 * increase tcp_cwnd so that new 14500 * data can be sent if it is allowed 14501 * by the receive window (tcp_rwnd). 14502 * tcp_wput_data() will take care of 14503 * the rest. 14504 * 14505 * If the connection is SACK capable, 14506 * only do limited xmit when there 14507 * is SACK info. 14508 * 14509 * Note how tcp_cwnd is incremented. 14510 * The first dup ACK will increase 14511 * it by 1 MSS. The second dup ACK 14512 * will increase it by 2 MSS. This 14513 * means that only 1 new segment will 14514 * be sent for each dup ACK. 14515 */ 14516 if (tcp->tcp_unsent > 0 && 14517 (!tcp->tcp_snd_sack_ok || 14518 (tcp->tcp_snd_sack_ok && 14519 tcp->tcp_notsack_list != NULL))) { 14520 tcp->tcp_cwnd += mss << 14521 (tcp->tcp_dupack_cnt - 1); 14522 flags |= TH_LIMIT_XMIT; 14523 } 14524 } else if (dupack_cnt == 14525 tcps->tcps_dupack_fast_retransmit) { 14526 14527 /* 14528 * If we have reduced tcp_ssthresh 14529 * because of ECN, do not reduce it again 14530 * unless it is already one window of data 14531 * away. After one window of data, tcp_cwr 14532 * should then be cleared. Note that 14533 * for non ECN capable connection, tcp_cwr 14534 * should always be false. 14535 * 14536 * Adjust cwnd since the duplicate 14537 * ack indicates that a packet was 14538 * dropped (due to congestion.) 14539 */ 14540 if (!tcp->tcp_cwr) { 14541 npkt = ((tcp->tcp_snxt - 14542 tcp->tcp_suna) >> 1) / mss; 14543 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14544 mss; 14545 tcp->tcp_cwnd = (npkt + 14546 tcp->tcp_dupack_cnt) * mss; 14547 } 14548 if (tcp->tcp_ecn_ok) { 14549 tcp->tcp_cwr = B_TRUE; 14550 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14551 tcp->tcp_ecn_cwr_sent = B_FALSE; 14552 } 14553 14554 /* 14555 * We do Hoe's algorithm. Refer to her 14556 * paper "Improving the Start-up Behavior 14557 * of a Congestion Control Scheme for TCP," 14558 * appeared in SIGCOMM'96. 14559 * 14560 * Save highest seq no we have sent so far. 14561 * Be careful about the invisible FIN byte. 14562 */ 14563 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14564 (tcp->tcp_unsent == 0)) { 14565 tcp->tcp_rexmit_max = tcp->tcp_fss; 14566 } else { 14567 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14568 } 14569 14570 /* 14571 * Do not allow bursty traffic during. 14572 * fast recovery. Refer to Fall and Floyd's 14573 * paper "Simulation-based Comparisons of 14574 * Tahoe, Reno and SACK TCP" (in CCR?) 14575 * This is a best current practise. 14576 */ 14577 tcp->tcp_snd_burst = TCP_CWND_SS; 14578 14579 /* 14580 * For SACK: 14581 * Calculate tcp_pipe, which is the 14582 * estimated number of bytes in 14583 * network. 14584 * 14585 * tcp_fack is the highest sack'ed seq num 14586 * TCP has received. 14587 * 14588 * tcp_pipe is explained in the above quoted 14589 * Fall and Floyd's paper. tcp_fack is 14590 * explained in Mathis and Mahdavi's 14591 * "Forward Acknowledgment: Refining TCP 14592 * Congestion Control" in SIGCOMM '96. 14593 */ 14594 if (tcp->tcp_snd_sack_ok) { 14595 ASSERT(tcp->tcp_sack_info != NULL); 14596 if (tcp->tcp_notsack_list != NULL) { 14597 tcp->tcp_pipe = tcp->tcp_snxt - 14598 tcp->tcp_fack; 14599 tcp->tcp_sack_snxt = seg_ack; 14600 flags |= TH_NEED_SACK_REXMIT; 14601 } else { 14602 /* 14603 * Always initialize tcp_pipe 14604 * even though we don't have 14605 * any SACK info. If later 14606 * we get SACK info and 14607 * tcp_pipe is not initialized, 14608 * funny things will happen. 14609 */ 14610 tcp->tcp_pipe = 14611 tcp->tcp_cwnd_ssthresh; 14612 } 14613 } else { 14614 flags |= TH_REXMIT_NEEDED; 14615 } /* tcp_snd_sack_ok */ 14616 14617 } else { 14618 /* 14619 * Here we perform congestion 14620 * avoidance, but NOT slow start. 14621 * This is known as the Fast 14622 * Recovery Algorithm. 14623 */ 14624 if (tcp->tcp_snd_sack_ok && 14625 tcp->tcp_notsack_list != NULL) { 14626 flags |= TH_NEED_SACK_REXMIT; 14627 tcp->tcp_pipe -= mss; 14628 if (tcp->tcp_pipe < 0) 14629 tcp->tcp_pipe = 0; 14630 } else { 14631 /* 14632 * We know that one more packet has 14633 * left the pipe thus we can update 14634 * cwnd. 14635 */ 14636 cwnd = tcp->tcp_cwnd + mss; 14637 if (cwnd > tcp->tcp_cwnd_max) 14638 cwnd = tcp->tcp_cwnd_max; 14639 tcp->tcp_cwnd = cwnd; 14640 if (tcp->tcp_unsent > 0) 14641 flags |= TH_XMIT_NEEDED; 14642 } 14643 } 14644 } 14645 } else if (tcp->tcp_zero_win_probe) { 14646 /* 14647 * If the window has opened, need to arrange 14648 * to send additional data. 14649 */ 14650 if (new_swnd != 0) { 14651 /* tcp_suna != tcp_snxt */ 14652 /* Packet contains a window update */ 14653 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14654 tcp->tcp_zero_win_probe = 0; 14655 tcp->tcp_timer_backoff = 0; 14656 tcp->tcp_ms_we_have_waited = 0; 14657 14658 /* 14659 * Transmit starting with tcp_suna since 14660 * the one byte probe is not ack'ed. 14661 * If TCP has sent more than one identical 14662 * probe, tcp_rexmit will be set. That means 14663 * tcp_ss_rexmit() will send out the one 14664 * byte along with new data. Otherwise, 14665 * fake the retransmission. 14666 */ 14667 flags |= TH_XMIT_NEEDED; 14668 if (!tcp->tcp_rexmit) { 14669 tcp->tcp_rexmit = B_TRUE; 14670 tcp->tcp_dupack_cnt = 0; 14671 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14672 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14673 } 14674 } 14675 } 14676 goto swnd_update; 14677 } 14678 14679 /* 14680 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14681 * If the ACK value acks something that we have not yet sent, it might 14682 * be an old duplicate segment. Send an ACK to re-synchronize the 14683 * other side. 14684 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14685 * state is handled above, so we can always just drop the segment and 14686 * send an ACK here. 14687 * 14688 * Should we send ACKs in response to ACK only segments? 14689 */ 14690 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14691 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14692 /* drop the received segment */ 14693 freemsg(mp); 14694 14695 /* 14696 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14697 * greater than 0, check if the number of such 14698 * bogus ACks is greater than that count. If yes, 14699 * don't send back any ACK. This prevents TCP from 14700 * getting into an ACK storm if somehow an attacker 14701 * successfully spoofs an acceptable segment to our 14702 * peer. 14703 */ 14704 if (tcp_drop_ack_unsent_cnt > 0 && 14705 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14706 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14707 return; 14708 } 14709 mp = tcp_ack_mp(tcp); 14710 if (mp != NULL) { 14711 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 14712 BUMP_LOCAL(tcp->tcp_obsegs); 14713 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14714 tcp_send_data(tcp, tcp->tcp_wq, mp); 14715 } 14716 return; 14717 } 14718 14719 /* 14720 * TCP gets a new ACK, update the notsack'ed list to delete those 14721 * blocks that are covered by this ACK. 14722 */ 14723 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14724 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14725 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14726 } 14727 14728 /* 14729 * If we got an ACK after fast retransmit, check to see 14730 * if it is a partial ACK. If it is not and the congestion 14731 * window was inflated to account for the other side's 14732 * cached packets, retract it. If it is, do Hoe's algorithm. 14733 */ 14734 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14735 ASSERT(tcp->tcp_rexmit == B_FALSE); 14736 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14737 tcp->tcp_dupack_cnt = 0; 14738 /* 14739 * Restore the orig tcp_cwnd_ssthresh after 14740 * fast retransmit phase. 14741 */ 14742 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14743 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14744 } 14745 tcp->tcp_rexmit_max = seg_ack; 14746 tcp->tcp_cwnd_cnt = 0; 14747 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14748 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14749 14750 /* 14751 * Remove all notsack info to avoid confusion with 14752 * the next fast retrasnmit/recovery phase. 14753 */ 14754 if (tcp->tcp_snd_sack_ok && 14755 tcp->tcp_notsack_list != NULL) { 14756 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14757 } 14758 } else { 14759 if (tcp->tcp_snd_sack_ok && 14760 tcp->tcp_notsack_list != NULL) { 14761 flags |= TH_NEED_SACK_REXMIT; 14762 tcp->tcp_pipe -= mss; 14763 if (tcp->tcp_pipe < 0) 14764 tcp->tcp_pipe = 0; 14765 } else { 14766 /* 14767 * Hoe's algorithm: 14768 * 14769 * Retransmit the unack'ed segment and 14770 * restart fast recovery. Note that we 14771 * need to scale back tcp_cwnd to the 14772 * original value when we started fast 14773 * recovery. This is to prevent overly 14774 * aggressive behaviour in sending new 14775 * segments. 14776 */ 14777 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14778 tcps->tcps_dupack_fast_retransmit * mss; 14779 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14780 flags |= TH_REXMIT_NEEDED; 14781 } 14782 } 14783 } else { 14784 tcp->tcp_dupack_cnt = 0; 14785 if (tcp->tcp_rexmit) { 14786 /* 14787 * TCP is retranmitting. If the ACK ack's all 14788 * outstanding data, update tcp_rexmit_max and 14789 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14790 * to the correct value. 14791 * 14792 * Note that SEQ_LEQ() is used. This is to avoid 14793 * unnecessary fast retransmit caused by dup ACKs 14794 * received when TCP does slow start retransmission 14795 * after a time out. During this phase, TCP may 14796 * send out segments which are already received. 14797 * This causes dup ACKs to be sent back. 14798 */ 14799 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14800 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14801 tcp->tcp_rexmit_nxt = seg_ack; 14802 } 14803 if (seg_ack != tcp->tcp_rexmit_max) { 14804 flags |= TH_XMIT_NEEDED; 14805 } 14806 } else { 14807 tcp->tcp_rexmit = B_FALSE; 14808 tcp->tcp_xmit_zc_clean = B_FALSE; 14809 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14810 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14811 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14812 } 14813 tcp->tcp_ms_we_have_waited = 0; 14814 } 14815 } 14816 14817 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14818 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14819 tcp->tcp_suna = seg_ack; 14820 if (tcp->tcp_zero_win_probe != 0) { 14821 tcp->tcp_zero_win_probe = 0; 14822 tcp->tcp_timer_backoff = 0; 14823 } 14824 14825 /* 14826 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14827 * Note that it cannot be the SYN being ack'ed. The code flow 14828 * will not reach here. 14829 */ 14830 if (mp1 == NULL) { 14831 goto fin_acked; 14832 } 14833 14834 /* 14835 * Update the congestion window. 14836 * 14837 * If TCP is not ECN capable or TCP is ECN capable but the 14838 * congestion experience bit is not set, increase the tcp_cwnd as 14839 * usual. 14840 */ 14841 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14842 cwnd = tcp->tcp_cwnd; 14843 add = mss; 14844 14845 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14846 /* 14847 * This is to prevent an increase of less than 1 MSS of 14848 * tcp_cwnd. With partial increase, tcp_wput_data() 14849 * may send out tinygrams in order to preserve mblk 14850 * boundaries. 14851 * 14852 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14853 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14854 * increased by 1 MSS for every RTTs. 14855 */ 14856 if (tcp->tcp_cwnd_cnt <= 0) { 14857 tcp->tcp_cwnd_cnt = cwnd + add; 14858 } else { 14859 tcp->tcp_cwnd_cnt -= add; 14860 add = 0; 14861 } 14862 } 14863 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14864 } 14865 14866 /* See if the latest urgent data has been acknowledged */ 14867 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14868 SEQ_GT(seg_ack, tcp->tcp_urg)) 14869 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14870 14871 /* Can we update the RTT estimates? */ 14872 if (tcp->tcp_snd_ts_ok) { 14873 /* Ignore zero timestamp echo-reply. */ 14874 if (tcpopt.tcp_opt_ts_ecr != 0) { 14875 tcp_set_rto(tcp, (int32_t)lbolt - 14876 (int32_t)tcpopt.tcp_opt_ts_ecr); 14877 } 14878 14879 /* If needed, restart the timer. */ 14880 if (tcp->tcp_set_timer == 1) { 14881 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14882 tcp->tcp_set_timer = 0; 14883 } 14884 /* 14885 * Update tcp_csuna in case the other side stops sending 14886 * us timestamps. 14887 */ 14888 tcp->tcp_csuna = tcp->tcp_snxt; 14889 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14890 /* 14891 * An ACK sequence we haven't seen before, so get the RTT 14892 * and update the RTO. But first check if the timestamp is 14893 * valid to use. 14894 */ 14895 if ((mp1->b_next != NULL) && 14896 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14897 tcp_set_rto(tcp, (int32_t)lbolt - 14898 (int32_t)(intptr_t)mp1->b_prev); 14899 else 14900 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14901 14902 /* Remeber the last sequence to be ACKed */ 14903 tcp->tcp_csuna = seg_ack; 14904 if (tcp->tcp_set_timer == 1) { 14905 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14906 tcp->tcp_set_timer = 0; 14907 } 14908 } else { 14909 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14910 } 14911 14912 /* Eat acknowledged bytes off the xmit queue. */ 14913 for (;;) { 14914 mblk_t *mp2; 14915 uchar_t *wptr; 14916 14917 wptr = mp1->b_wptr; 14918 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14919 bytes_acked -= (int)(wptr - mp1->b_rptr); 14920 if (bytes_acked < 0) { 14921 mp1->b_rptr = wptr + bytes_acked; 14922 /* 14923 * Set a new timestamp if all the bytes timed by the 14924 * old timestamp have been ack'ed. 14925 */ 14926 if (SEQ_GT(seg_ack, 14927 (uint32_t)(uintptr_t)(mp1->b_next))) { 14928 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14929 mp1->b_next = NULL; 14930 } 14931 break; 14932 } 14933 mp1->b_next = NULL; 14934 mp1->b_prev = NULL; 14935 mp2 = mp1; 14936 mp1 = mp1->b_cont; 14937 14938 /* 14939 * This notification is required for some zero-copy 14940 * clients to maintain a copy semantic. After the data 14941 * is ack'ed, client is safe to modify or reuse the buffer. 14942 */ 14943 if (tcp->tcp_snd_zcopy_aware && 14944 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14945 tcp_zcopy_notify(tcp); 14946 freeb(mp2); 14947 if (bytes_acked == 0) { 14948 if (mp1 == NULL) { 14949 /* Everything is ack'ed, clear the tail. */ 14950 tcp->tcp_xmit_tail = NULL; 14951 /* 14952 * Cancel the timer unless we are still 14953 * waiting for an ACK for the FIN packet. 14954 */ 14955 if (tcp->tcp_timer_tid != 0 && 14956 tcp->tcp_snxt == tcp->tcp_suna) { 14957 (void) TCP_TIMER_CANCEL(tcp, 14958 tcp->tcp_timer_tid); 14959 tcp->tcp_timer_tid = 0; 14960 } 14961 goto pre_swnd_update; 14962 } 14963 if (mp2 != tcp->tcp_xmit_tail) 14964 break; 14965 tcp->tcp_xmit_tail = mp1; 14966 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14967 (uintptr_t)INT_MAX); 14968 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14969 mp1->b_rptr); 14970 break; 14971 } 14972 if (mp1 == NULL) { 14973 /* 14974 * More was acked but there is nothing more 14975 * outstanding. This means that the FIN was 14976 * just acked or that we're talking to a clown. 14977 */ 14978 fin_acked: 14979 ASSERT(tcp->tcp_fin_sent); 14980 tcp->tcp_xmit_tail = NULL; 14981 if (tcp->tcp_fin_sent) { 14982 /* FIN was acked - making progress */ 14983 if (tcp->tcp_ipversion == IPV6_VERSION && 14984 !tcp->tcp_fin_acked) 14985 tcp->tcp_ip_forward_progress = B_TRUE; 14986 tcp->tcp_fin_acked = B_TRUE; 14987 if (tcp->tcp_linger_tid != 0 && 14988 TCP_TIMER_CANCEL(tcp, 14989 tcp->tcp_linger_tid) >= 0) { 14990 tcp_stop_lingering(tcp); 14991 freemsg(mp); 14992 mp = NULL; 14993 } 14994 } else { 14995 /* 14996 * We should never get here because 14997 * we have already checked that the 14998 * number of bytes ack'ed should be 14999 * smaller than or equal to what we 15000 * have sent so far (it is the 15001 * acceptability check of the ACK). 15002 * We can only get here if the send 15003 * queue is corrupted. 15004 * 15005 * Terminate the connection and 15006 * panic the system. It is better 15007 * for us to panic instead of 15008 * continuing to avoid other disaster. 15009 */ 15010 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 15011 tcp->tcp_rnxt, TH_RST|TH_ACK); 15012 panic("Memory corruption " 15013 "detected for connection %s.", 15014 tcp_display(tcp, NULL, 15015 DISP_ADDR_AND_PORT)); 15016 /*NOTREACHED*/ 15017 } 15018 goto pre_swnd_update; 15019 } 15020 ASSERT(mp2 != tcp->tcp_xmit_tail); 15021 } 15022 if (tcp->tcp_unsent) { 15023 flags |= TH_XMIT_NEEDED; 15024 } 15025 pre_swnd_update: 15026 tcp->tcp_xmit_head = mp1; 15027 swnd_update: 15028 /* 15029 * The following check is different from most other implementations. 15030 * For bi-directional transfer, when segments are dropped, the 15031 * "normal" check will not accept a window update in those 15032 * retransmitted segemnts. Failing to do that, TCP may send out 15033 * segments which are outside receiver's window. As TCP accepts 15034 * the ack in those retransmitted segments, if the window update in 15035 * the same segment is not accepted, TCP will incorrectly calculates 15036 * that it can send more segments. This can create a deadlock 15037 * with the receiver if its window becomes zero. 15038 */ 15039 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 15040 SEQ_LT(tcp->tcp_swl1, seg_seq) || 15041 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 15042 /* 15043 * The criteria for update is: 15044 * 15045 * 1. the segment acknowledges some data. Or 15046 * 2. the segment is new, i.e. it has a higher seq num. Or 15047 * 3. the segment is not old and the advertised window is 15048 * larger than the previous advertised window. 15049 */ 15050 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 15051 flags |= TH_XMIT_NEEDED; 15052 tcp->tcp_swnd = new_swnd; 15053 if (new_swnd > tcp->tcp_max_swnd) 15054 tcp->tcp_max_swnd = new_swnd; 15055 tcp->tcp_swl1 = seg_seq; 15056 tcp->tcp_swl2 = seg_ack; 15057 } 15058 est: 15059 if (tcp->tcp_state > TCPS_ESTABLISHED) { 15060 15061 switch (tcp->tcp_state) { 15062 case TCPS_FIN_WAIT_1: 15063 if (tcp->tcp_fin_acked) { 15064 tcp->tcp_state = TCPS_FIN_WAIT_2; 15065 /* 15066 * We implement the non-standard BSD/SunOS 15067 * FIN_WAIT_2 flushing algorithm. 15068 * If there is no user attached to this 15069 * TCP endpoint, then this TCP struct 15070 * could hang around forever in FIN_WAIT_2 15071 * state if the peer forgets to send us 15072 * a FIN. To prevent this, we wait only 15073 * 2*MSL (a convenient time value) for 15074 * the FIN to arrive. If it doesn't show up, 15075 * we flush the TCP endpoint. This algorithm, 15076 * though a violation of RFC-793, has worked 15077 * for over 10 years in BSD systems. 15078 * Note: SunOS 4.x waits 675 seconds before 15079 * flushing the FIN_WAIT_2 connection. 15080 */ 15081 TCP_TIMER_RESTART(tcp, 15082 tcps->tcps_fin_wait_2_flush_interval); 15083 } 15084 break; 15085 case TCPS_FIN_WAIT_2: 15086 break; /* Shutdown hook? */ 15087 case TCPS_LAST_ACK: 15088 freemsg(mp); 15089 if (tcp->tcp_fin_acked) { 15090 (void) tcp_clean_death(tcp, 0, 19); 15091 return; 15092 } 15093 goto xmit_check; 15094 case TCPS_CLOSING: 15095 if (tcp->tcp_fin_acked) { 15096 tcp->tcp_state = TCPS_TIME_WAIT; 15097 /* 15098 * Unconditionally clear the exclusive binding 15099 * bit so this TIME-WAIT connection won't 15100 * interfere with new ones. 15101 */ 15102 tcp->tcp_exclbind = 0; 15103 if (!TCP_IS_DETACHED(tcp)) { 15104 TCP_TIMER_RESTART(tcp, 15105 tcps->tcps_time_wait_interval); 15106 } else { 15107 tcp_time_wait_append(tcp); 15108 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 15109 } 15110 } 15111 /*FALLTHRU*/ 15112 case TCPS_CLOSE_WAIT: 15113 freemsg(mp); 15114 goto xmit_check; 15115 default: 15116 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 15117 break; 15118 } 15119 } 15120 if (flags & TH_FIN) { 15121 /* Make sure we ack the fin */ 15122 flags |= TH_ACK_NEEDED; 15123 if (!tcp->tcp_fin_rcvd) { 15124 tcp->tcp_fin_rcvd = B_TRUE; 15125 tcp->tcp_rnxt++; 15126 tcph = tcp->tcp_tcph; 15127 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15128 15129 /* 15130 * Generate the ordrel_ind at the end unless we 15131 * are an eager guy. 15132 * In the eager case tcp_rsrv will do this when run 15133 * after tcp_accept is done. 15134 */ 15135 if (tcp->tcp_listener == NULL && 15136 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 15137 flags |= TH_ORDREL_NEEDED; 15138 switch (tcp->tcp_state) { 15139 case TCPS_SYN_RCVD: 15140 case TCPS_ESTABLISHED: 15141 tcp->tcp_state = TCPS_CLOSE_WAIT; 15142 /* Keepalive? */ 15143 break; 15144 case TCPS_FIN_WAIT_1: 15145 if (!tcp->tcp_fin_acked) { 15146 tcp->tcp_state = TCPS_CLOSING; 15147 break; 15148 } 15149 /* FALLTHRU */ 15150 case TCPS_FIN_WAIT_2: 15151 tcp->tcp_state = TCPS_TIME_WAIT; 15152 /* 15153 * Unconditionally clear the exclusive binding 15154 * bit so this TIME-WAIT connection won't 15155 * interfere with new ones. 15156 */ 15157 tcp->tcp_exclbind = 0; 15158 if (!TCP_IS_DETACHED(tcp)) { 15159 TCP_TIMER_RESTART(tcp, 15160 tcps->tcps_time_wait_interval); 15161 } else { 15162 tcp_time_wait_append(tcp); 15163 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 15164 } 15165 if (seg_len) { 15166 /* 15167 * implies data piggybacked on FIN. 15168 * break to handle data. 15169 */ 15170 break; 15171 } 15172 freemsg(mp); 15173 goto ack_check; 15174 } 15175 } 15176 } 15177 if (mp == NULL) 15178 goto xmit_check; 15179 if (seg_len == 0) { 15180 freemsg(mp); 15181 goto xmit_check; 15182 } 15183 if (mp->b_rptr == mp->b_wptr) { 15184 /* 15185 * The header has been consumed, so we remove the 15186 * zero-length mblk here. 15187 */ 15188 mp1 = mp; 15189 mp = mp->b_cont; 15190 freeb(mp1); 15191 } 15192 tcph = tcp->tcp_tcph; 15193 tcp->tcp_rack_cnt++; 15194 { 15195 uint32_t cur_max; 15196 15197 cur_max = tcp->tcp_rack_cur_max; 15198 if (tcp->tcp_rack_cnt >= cur_max) { 15199 /* 15200 * We have more unacked data than we should - send 15201 * an ACK now. 15202 */ 15203 flags |= TH_ACK_NEEDED; 15204 cur_max++; 15205 if (cur_max > tcp->tcp_rack_abs_max) 15206 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15207 else 15208 tcp->tcp_rack_cur_max = cur_max; 15209 } else if (TCP_IS_DETACHED(tcp)) { 15210 /* We don't have an ACK timer for detached TCP. */ 15211 flags |= TH_ACK_NEEDED; 15212 } else if (seg_len < mss) { 15213 /* 15214 * If we get a segment that is less than an mss, and we 15215 * already have unacknowledged data, and the amount 15216 * unacknowledged is not a multiple of mss, then we 15217 * better generate an ACK now. Otherwise, this may be 15218 * the tail piece of a transaction, and we would rather 15219 * wait for the response. 15220 */ 15221 uint32_t udif; 15222 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 15223 (uintptr_t)INT_MAX); 15224 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 15225 if (udif && (udif % mss)) 15226 flags |= TH_ACK_NEEDED; 15227 else 15228 flags |= TH_ACK_TIMER_NEEDED; 15229 } else { 15230 /* Start delayed ack timer */ 15231 flags |= TH_ACK_TIMER_NEEDED; 15232 } 15233 } 15234 tcp->tcp_rnxt += seg_len; 15235 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15236 15237 /* Update SACK list */ 15238 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 15239 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 15240 &(tcp->tcp_num_sack_blk)); 15241 } 15242 15243 if (tcp->tcp_urp_mp) { 15244 tcp->tcp_urp_mp->b_cont = mp; 15245 mp = tcp->tcp_urp_mp; 15246 tcp->tcp_urp_mp = NULL; 15247 /* Ready for a new signal. */ 15248 tcp->tcp_urp_last_valid = B_FALSE; 15249 #ifdef DEBUG 15250 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15251 "tcp_rput: sending exdata_ind %s", 15252 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15253 #endif /* DEBUG */ 15254 } 15255 15256 /* 15257 * Check for ancillary data changes compared to last segment. 15258 */ 15259 if (tcp->tcp_ipv6_recvancillary != 0) { 15260 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 15261 if (mp == NULL) 15262 return; 15263 } 15264 15265 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 15266 /* 15267 * Side queue inbound data until the accept happens. 15268 * tcp_accept/tcp_rput drains this when the accept happens. 15269 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 15270 * T_EXDATA_IND) it is queued on b_next. 15271 * XXX Make urgent data use this. Requires: 15272 * Removing tcp_listener check for TH_URG 15273 * Making M_PCPROTO and MARK messages skip the eager case 15274 */ 15275 15276 if (tcp->tcp_kssl_pending) { 15277 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 15278 mblk_t *, mp); 15279 tcp_kssl_input(tcp, mp); 15280 } else { 15281 tcp_rcv_enqueue(tcp, mp, seg_len); 15282 } 15283 } else { 15284 sodirect_t *sodp = tcp->tcp_sodirect; 15285 15286 /* 15287 * If an sodirect connection and an enabled sodirect_t then 15288 * sodp will be set to point to the tcp_t/sonode_t shared 15289 * sodirect_t and the sodirect_t's lock will be held. 15290 */ 15291 if (sodp != NULL) { 15292 mutex_enter(sodp->sod_lock); 15293 if (!(sodp->sod_state & SOD_ENABLED)) { 15294 mutex_exit(sodp->sod_lock); 15295 sodp = NULL; 15296 } else if (tcp->tcp_kssl_ctx != NULL && 15297 DB_TYPE(mp) == M_DATA) { 15298 mutex_exit(sodp->sod_lock); 15299 sodp = NULL; 15300 } 15301 } 15302 if (mp->b_datap->db_type != M_DATA || 15303 (flags & TH_MARKNEXT_NEEDED)) { 15304 if (sodp != NULL) { 15305 if (!SOD_QEMPTY(sodp) && 15306 (sodp->sod_state & SOD_WAKE_NOT)) { 15307 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15308 /* sod_wakeup() did the mutex_exit() */ 15309 mutex_enter(sodp->sod_lock); 15310 } 15311 } else if (tcp->tcp_rcv_list != NULL) { 15312 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15313 } 15314 ASSERT(tcp->tcp_rcv_list == NULL || 15315 tcp->tcp_fused_sigurg); 15316 15317 if (flags & TH_MARKNEXT_NEEDED) { 15318 #ifdef DEBUG 15319 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15320 "tcp_rput: sending MSGMARKNEXT %s", 15321 tcp_display(tcp, NULL, 15322 DISP_PORT_ONLY)); 15323 #endif /* DEBUG */ 15324 mp->b_flag |= MSGMARKNEXT; 15325 flags &= ~TH_MARKNEXT_NEEDED; 15326 } 15327 15328 /* Does this need SSL processing first? */ 15329 if ((tcp->tcp_kssl_ctx != NULL) && 15330 (DB_TYPE(mp) == M_DATA)) { 15331 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 15332 mblk_t *, mp); 15333 tcp_kssl_input(tcp, mp); 15334 } else if (sodp) { 15335 flags |= tcp_rcv_sod_enqueue( 15336 tcp, sodp, mp, seg_len); 15337 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15338 /* sod_wakeup() did the mutex_exit() */ 15339 } else { 15340 putnext(tcp->tcp_rq, mp); 15341 if (!canputnext(tcp->tcp_rq)) 15342 tcp->tcp_rwnd -= seg_len; 15343 } 15344 } else if ((tcp->tcp_kssl_ctx != NULL) && 15345 (DB_TYPE(mp) == M_DATA)) { 15346 /* Do SSL processing first */ 15347 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, 15348 mblk_t *, mp); 15349 tcp_kssl_input(tcp, mp); 15350 } else if (sodp != NULL) { 15351 /* 15352 * Sodirect so all mblk_t's are queued on the 15353 * socket directly, check for wakeup of blocked 15354 * reader (if any), and last if flow-controled. 15355 */ 15356 flags |= tcp_rcv_sod_enqueue(tcp, sodp, mp, seg_len); 15357 if ((sodp->sod_state & SOD_WAKE_NEED) || 15358 (flags & (TH_PUSH|TH_FIN))) { 15359 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15360 /* sod_wakeup() did the mutex_exit() */ 15361 } else { 15362 if (SOD_QFULL(sodp)) { 15363 /* Q is full, need backenable */ 15364 SOD_QSETBE(sodp); 15365 } 15366 mutex_exit(sodp->sod_lock); 15367 } 15368 } else if ((flags & (TH_PUSH|TH_FIN)) || 15369 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 15370 if (tcp->tcp_rcv_list != NULL) { 15371 /* 15372 * Enqueue the new segment first and then 15373 * call tcp_rcv_drain() to send all data 15374 * up. The other way to do this is to 15375 * send all queued data up and then call 15376 * putnext() to send the new segment up. 15377 * This way can remove the else part later 15378 * on. 15379 * 15380 * We don't this to avoid one more call to 15381 * canputnext() as tcp_rcv_drain() needs to 15382 * call canputnext(). 15383 */ 15384 tcp_rcv_enqueue(tcp, mp, seg_len); 15385 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15386 } else { 15387 putnext(tcp->tcp_rq, mp); 15388 if (!canputnext(tcp->tcp_rq)) 15389 tcp->tcp_rwnd -= seg_len; 15390 } 15391 } else { 15392 /* 15393 * Enqueue all packets when processing an mblk 15394 * from the co queue and also enqueue normal packets. 15395 */ 15396 tcp_rcv_enqueue(tcp, mp, seg_len); 15397 } 15398 /* 15399 * Make sure the timer is running if we have data waiting 15400 * for a push bit. This provides resiliency against 15401 * implementations that do not correctly generate push bits. 15402 * 15403 * Note, for sodirect if Q isn't empty and there's not a 15404 * pending wakeup then we need a timer. Also note that sodp 15405 * is assumed to be still valid after exit()ing the sod_lock 15406 * above and while the SOD state can change it can only change 15407 * such that the Q is empty now even though data was added 15408 * above. 15409 */ 15410 if (((sodp != NULL && !SOD_QEMPTY(sodp) && 15411 (sodp->sod_state & SOD_WAKE_NOT)) || 15412 (sodp == NULL && tcp->tcp_rcv_list != NULL)) && 15413 tcp->tcp_push_tid == 0) { 15414 /* 15415 * The connection may be closed at this point, so don't 15416 * do anything for a detached tcp. 15417 */ 15418 if (!TCP_IS_DETACHED(tcp)) 15419 tcp->tcp_push_tid = TCP_TIMER(tcp, 15420 tcp_push_timer, 15421 MSEC_TO_TICK( 15422 tcps->tcps_push_timer_interval)); 15423 } 15424 } 15425 15426 xmit_check: 15427 /* Is there anything left to do? */ 15428 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15429 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15430 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15431 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15432 goto done; 15433 15434 /* Any transmit work to do and a non-zero window? */ 15435 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15436 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15437 if (flags & TH_REXMIT_NEEDED) { 15438 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15439 15440 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15441 if (snd_size > mss) 15442 snd_size = mss; 15443 if (snd_size > tcp->tcp_swnd) 15444 snd_size = tcp->tcp_swnd; 15445 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15446 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15447 B_TRUE); 15448 15449 if (mp1 != NULL) { 15450 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15451 tcp->tcp_csuna = tcp->tcp_snxt; 15452 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15453 UPDATE_MIB(&tcps->tcps_mib, 15454 tcpRetransBytes, snd_size); 15455 TCP_RECORD_TRACE(tcp, mp1, 15456 TCP_TRACE_SEND_PKT); 15457 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15458 } 15459 } 15460 if (flags & TH_NEED_SACK_REXMIT) { 15461 tcp_sack_rxmit(tcp, &flags); 15462 } 15463 /* 15464 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15465 * out new segment. Note that tcp_rexmit should not be 15466 * set, otherwise TH_LIMIT_XMIT should not be set. 15467 */ 15468 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15469 if (!tcp->tcp_rexmit) { 15470 tcp_wput_data(tcp, NULL, B_FALSE); 15471 } else { 15472 tcp_ss_rexmit(tcp); 15473 } 15474 } 15475 /* 15476 * Adjust tcp_cwnd back to normal value after sending 15477 * new data segments. 15478 */ 15479 if (flags & TH_LIMIT_XMIT) { 15480 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15481 /* 15482 * This will restart the timer. Restarting the 15483 * timer is used to avoid a timeout before the 15484 * limited transmitted segment's ACK gets back. 15485 */ 15486 if (tcp->tcp_xmit_head != NULL) 15487 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15488 } 15489 15490 /* Anything more to do? */ 15491 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15492 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15493 goto done; 15494 } 15495 ack_check: 15496 if (flags & TH_SEND_URP_MARK) { 15497 ASSERT(tcp->tcp_urp_mark_mp); 15498 /* 15499 * Send up any queued data and then send the mark message 15500 */ 15501 sodirect_t *sodp; 15502 15503 SOD_PTR_ENTER(tcp, sodp); 15504 15505 mp1 = tcp->tcp_urp_mark_mp; 15506 tcp->tcp_urp_mark_mp = NULL; 15507 if (sodp != NULL) { 15508 15509 ASSERT(tcp->tcp_rcv_list == NULL); 15510 15511 flags |= tcp_rcv_sod_enqueue(tcp, sodp, mp1, 0); 15512 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15513 /* sod_wakeup() does the mutex_exit() */ 15514 } else if (tcp->tcp_rcv_list != NULL) { 15515 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15516 15517 ASSERT(tcp->tcp_rcv_list == NULL || 15518 tcp->tcp_fused_sigurg); 15519 15520 putnext(tcp->tcp_rq, mp1); 15521 } 15522 #ifdef DEBUG 15523 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15524 "tcp_rput: sending zero-length %s %s", 15525 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15526 "MSGNOTMARKNEXT"), 15527 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15528 #endif /* DEBUG */ 15529 flags &= ~TH_SEND_URP_MARK; 15530 } 15531 if (flags & TH_ACK_NEEDED) { 15532 /* 15533 * Time to send an ack for some reason. 15534 */ 15535 mp1 = tcp_ack_mp(tcp); 15536 15537 if (mp1 != NULL) { 15538 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 15539 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15540 BUMP_LOCAL(tcp->tcp_obsegs); 15541 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15542 } 15543 if (tcp->tcp_ack_tid != 0) { 15544 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15545 tcp->tcp_ack_tid = 0; 15546 } 15547 } 15548 if (flags & TH_ACK_TIMER_NEEDED) { 15549 /* 15550 * Arrange for deferred ACK or push wait timeout. 15551 * Start timer if it is not already running. 15552 */ 15553 if (tcp->tcp_ack_tid == 0) { 15554 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15555 MSEC_TO_TICK(tcp->tcp_localnet ? 15556 (clock_t)tcps->tcps_local_dack_interval : 15557 (clock_t)tcps->tcps_deferred_ack_interval)); 15558 } 15559 } 15560 if (flags & TH_ORDREL_NEEDED) { 15561 /* 15562 * Send up the ordrel_ind unless we are an eager guy. 15563 * In the eager case tcp_rsrv will do this when run 15564 * after tcp_accept is done. 15565 */ 15566 sodirect_t *sodp; 15567 15568 ASSERT(tcp->tcp_listener == NULL); 15569 15570 SOD_PTR_ENTER(tcp, sodp); 15571 if (sodp != NULL) { 15572 /* No more sodirect */ 15573 tcp->tcp_sodirect = NULL; 15574 if (!SOD_QEMPTY(sodp)) { 15575 /* Mblk(s) to process, notify */ 15576 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15577 /* sod_wakeup() does the mutex_exit() */ 15578 } else { 15579 /* Nothing to process */ 15580 mutex_exit(sodp->sod_lock); 15581 } 15582 } else if (tcp->tcp_rcv_list != NULL) { 15583 /* 15584 * Push any mblk(s) enqueued from co processing. 15585 */ 15586 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15587 15588 ASSERT(tcp->tcp_rcv_list == NULL || 15589 tcp->tcp_fused_sigurg); 15590 } 15591 15592 if ((mp1 = mi_tpi_ordrel_ind()) != NULL) { 15593 tcp->tcp_ordrel_done = B_TRUE; 15594 putnext(tcp->tcp_rq, mp1); 15595 if (tcp->tcp_deferred_clean_death) { 15596 /* 15597 * tcp_clean_death was deferred 15598 * for T_ORDREL_IND - do it now 15599 */ 15600 (void) tcp_clean_death(tcp, 15601 tcp->tcp_client_errno, 20); 15602 tcp->tcp_deferred_clean_death = B_FALSE; 15603 } 15604 } else { 15605 /* 15606 * Run the orderly release in the 15607 * service routine. 15608 */ 15609 qenable(tcp->tcp_rq); 15610 /* 15611 * Caveat(XXX): The machine may be so 15612 * overloaded that tcp_rsrv() is not scheduled 15613 * until after the endpoint has transitioned 15614 * to TCPS_TIME_WAIT 15615 * and tcp_time_wait_interval expires. Then 15616 * tcp_timer() will blow away state in tcp_t 15617 * and T_ORDREL_IND will never be delivered 15618 * upstream. Unlikely but potentially 15619 * a problem. 15620 */ 15621 } 15622 } 15623 done: 15624 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15625 } 15626 15627 /* 15628 * This function does PAWS protection check. Returns B_TRUE if the 15629 * segment passes the PAWS test, else returns B_FALSE. 15630 */ 15631 boolean_t 15632 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15633 { 15634 uint8_t flags; 15635 int options; 15636 uint8_t *up; 15637 15638 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15639 /* 15640 * If timestamp option is aligned nicely, get values inline, 15641 * otherwise call general routine to parse. Only do that 15642 * if timestamp is the only option. 15643 */ 15644 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15645 TCPOPT_REAL_TS_LEN && 15646 OK_32PTR((up = ((uint8_t *)tcph) + 15647 TCP_MIN_HEADER_LENGTH)) && 15648 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15649 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15650 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15651 15652 options = TCP_OPT_TSTAMP_PRESENT; 15653 } else { 15654 if (tcp->tcp_snd_sack_ok) { 15655 tcpoptp->tcp = tcp; 15656 } else { 15657 tcpoptp->tcp = NULL; 15658 } 15659 options = tcp_parse_options(tcph, tcpoptp); 15660 } 15661 15662 if (options & TCP_OPT_TSTAMP_PRESENT) { 15663 /* 15664 * Do PAWS per RFC 1323 section 4.2. Accept RST 15665 * regardless of the timestamp, page 18 RFC 1323.bis. 15666 */ 15667 if ((flags & TH_RST) == 0 && 15668 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15669 tcp->tcp_ts_recent)) { 15670 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15671 PAWS_TIMEOUT)) { 15672 /* This segment is not acceptable. */ 15673 return (B_FALSE); 15674 } else { 15675 /* 15676 * Connection has been idle for 15677 * too long. Reset the timestamp 15678 * and assume the segment is valid. 15679 */ 15680 tcp->tcp_ts_recent = 15681 tcpoptp->tcp_opt_ts_val; 15682 } 15683 } 15684 } else { 15685 /* 15686 * If we don't get a timestamp on every packet, we 15687 * figure we can't really trust 'em, so we stop sending 15688 * and parsing them. 15689 */ 15690 tcp->tcp_snd_ts_ok = B_FALSE; 15691 15692 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15693 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15694 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15695 /* 15696 * Adjust the tcp_mss accordingly. We also need to 15697 * adjust tcp_cwnd here in accordance with the new mss. 15698 * But we avoid doing a slow start here so as to not 15699 * to lose on the transfer rate built up so far. 15700 */ 15701 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15702 if (tcp->tcp_snd_sack_ok) { 15703 ASSERT(tcp->tcp_sack_info != NULL); 15704 tcp->tcp_max_sack_blk = 4; 15705 } 15706 } 15707 return (B_TRUE); 15708 } 15709 15710 /* 15711 * Attach ancillary data to a received TCP segments for the 15712 * ancillary pieces requested by the application that are 15713 * different than they were in the previous data segment. 15714 * 15715 * Save the "current" values once memory allocation is ok so that 15716 * when memory allocation fails we can just wait for the next data segment. 15717 */ 15718 static mblk_t * 15719 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15720 { 15721 struct T_optdata_ind *todi; 15722 int optlen; 15723 uchar_t *optptr; 15724 struct T_opthdr *toh; 15725 uint_t addflag; /* Which pieces to add */ 15726 mblk_t *mp1; 15727 15728 optlen = 0; 15729 addflag = 0; 15730 /* If app asked for pktinfo and the index has changed ... */ 15731 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15732 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15733 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15734 optlen += sizeof (struct T_opthdr) + 15735 sizeof (struct in6_pktinfo); 15736 addflag |= TCP_IPV6_RECVPKTINFO; 15737 } 15738 /* If app asked for hoplimit and it has changed ... */ 15739 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15740 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15741 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15742 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15743 addflag |= TCP_IPV6_RECVHOPLIMIT; 15744 } 15745 /* If app asked for tclass and it has changed ... */ 15746 if ((ipp->ipp_fields & IPPF_TCLASS) && 15747 ipp->ipp_tclass != tcp->tcp_recvtclass && 15748 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15749 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15750 addflag |= TCP_IPV6_RECVTCLASS; 15751 } 15752 /* 15753 * If app asked for hopbyhop headers and it has changed ... 15754 * For security labels, note that (1) security labels can't change on 15755 * a connected socket at all, (2) we're connected to at most one peer, 15756 * (3) if anything changes, then it must be some other extra option. 15757 */ 15758 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15759 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15760 (ipp->ipp_fields & IPPF_HOPOPTS), 15761 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15762 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15763 tcp->tcp_label_len; 15764 addflag |= TCP_IPV6_RECVHOPOPTS; 15765 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15766 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15767 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15768 return (mp); 15769 } 15770 /* If app asked for dst headers before routing headers ... */ 15771 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15772 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15773 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15774 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15775 optlen += sizeof (struct T_opthdr) + 15776 ipp->ipp_rtdstoptslen; 15777 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15778 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15779 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15780 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15781 return (mp); 15782 } 15783 /* If app asked for routing headers and it has changed ... */ 15784 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15785 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15786 (ipp->ipp_fields & IPPF_RTHDR), 15787 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15788 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15789 addflag |= TCP_IPV6_RECVRTHDR; 15790 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15791 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15792 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15793 return (mp); 15794 } 15795 /* If app asked for dest headers and it has changed ... */ 15796 if ((tcp->tcp_ipv6_recvancillary & 15797 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15798 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15799 (ipp->ipp_fields & IPPF_DSTOPTS), 15800 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15801 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15802 addflag |= TCP_IPV6_RECVDSTOPTS; 15803 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15804 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15805 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15806 return (mp); 15807 } 15808 15809 if (optlen == 0) { 15810 /* Nothing to add */ 15811 return (mp); 15812 } 15813 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15814 if (mp1 == NULL) { 15815 /* 15816 * Defer sending ancillary data until the next TCP segment 15817 * arrives. 15818 */ 15819 return (mp); 15820 } 15821 mp1->b_cont = mp; 15822 mp = mp1; 15823 mp->b_wptr += sizeof (*todi) + optlen; 15824 mp->b_datap->db_type = M_PROTO; 15825 todi = (struct T_optdata_ind *)mp->b_rptr; 15826 todi->PRIM_type = T_OPTDATA_IND; 15827 todi->DATA_flag = 1; /* MORE data */ 15828 todi->OPT_length = optlen; 15829 todi->OPT_offset = sizeof (*todi); 15830 optptr = (uchar_t *)&todi[1]; 15831 /* 15832 * If app asked for pktinfo and the index has changed ... 15833 * Note that the local address never changes for the connection. 15834 */ 15835 if (addflag & TCP_IPV6_RECVPKTINFO) { 15836 struct in6_pktinfo *pkti; 15837 15838 toh = (struct T_opthdr *)optptr; 15839 toh->level = IPPROTO_IPV6; 15840 toh->name = IPV6_PKTINFO; 15841 toh->len = sizeof (*toh) + sizeof (*pkti); 15842 toh->status = 0; 15843 optptr += sizeof (*toh); 15844 pkti = (struct in6_pktinfo *)optptr; 15845 if (tcp->tcp_ipversion == IPV6_VERSION) 15846 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15847 else 15848 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15849 &pkti->ipi6_addr); 15850 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15851 optptr += sizeof (*pkti); 15852 ASSERT(OK_32PTR(optptr)); 15853 /* Save as "last" value */ 15854 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15855 } 15856 /* If app asked for hoplimit and it has changed ... */ 15857 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15858 toh = (struct T_opthdr *)optptr; 15859 toh->level = IPPROTO_IPV6; 15860 toh->name = IPV6_HOPLIMIT; 15861 toh->len = sizeof (*toh) + sizeof (uint_t); 15862 toh->status = 0; 15863 optptr += sizeof (*toh); 15864 *(uint_t *)optptr = ipp->ipp_hoplimit; 15865 optptr += sizeof (uint_t); 15866 ASSERT(OK_32PTR(optptr)); 15867 /* Save as "last" value */ 15868 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15869 } 15870 /* If app asked for tclass and it has changed ... */ 15871 if (addflag & TCP_IPV6_RECVTCLASS) { 15872 toh = (struct T_opthdr *)optptr; 15873 toh->level = IPPROTO_IPV6; 15874 toh->name = IPV6_TCLASS; 15875 toh->len = sizeof (*toh) + sizeof (uint_t); 15876 toh->status = 0; 15877 optptr += sizeof (*toh); 15878 *(uint_t *)optptr = ipp->ipp_tclass; 15879 optptr += sizeof (uint_t); 15880 ASSERT(OK_32PTR(optptr)); 15881 /* Save as "last" value */ 15882 tcp->tcp_recvtclass = ipp->ipp_tclass; 15883 } 15884 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15885 toh = (struct T_opthdr *)optptr; 15886 toh->level = IPPROTO_IPV6; 15887 toh->name = IPV6_HOPOPTS; 15888 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15889 tcp->tcp_label_len; 15890 toh->status = 0; 15891 optptr += sizeof (*toh); 15892 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15893 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15894 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15895 ASSERT(OK_32PTR(optptr)); 15896 /* Save as last value */ 15897 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15898 (ipp->ipp_fields & IPPF_HOPOPTS), 15899 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15900 } 15901 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15902 toh = (struct T_opthdr *)optptr; 15903 toh->level = IPPROTO_IPV6; 15904 toh->name = IPV6_RTHDRDSTOPTS; 15905 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15906 toh->status = 0; 15907 optptr += sizeof (*toh); 15908 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15909 optptr += ipp->ipp_rtdstoptslen; 15910 ASSERT(OK_32PTR(optptr)); 15911 /* Save as last value */ 15912 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15913 &tcp->tcp_rtdstoptslen, 15914 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15915 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15916 } 15917 if (addflag & TCP_IPV6_RECVRTHDR) { 15918 toh = (struct T_opthdr *)optptr; 15919 toh->level = IPPROTO_IPV6; 15920 toh->name = IPV6_RTHDR; 15921 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15922 toh->status = 0; 15923 optptr += sizeof (*toh); 15924 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15925 optptr += ipp->ipp_rthdrlen; 15926 ASSERT(OK_32PTR(optptr)); 15927 /* Save as last value */ 15928 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15929 (ipp->ipp_fields & IPPF_RTHDR), 15930 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15931 } 15932 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15933 toh = (struct T_opthdr *)optptr; 15934 toh->level = IPPROTO_IPV6; 15935 toh->name = IPV6_DSTOPTS; 15936 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15937 toh->status = 0; 15938 optptr += sizeof (*toh); 15939 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15940 optptr += ipp->ipp_dstoptslen; 15941 ASSERT(OK_32PTR(optptr)); 15942 /* Save as last value */ 15943 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15944 (ipp->ipp_fields & IPPF_DSTOPTS), 15945 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15946 } 15947 ASSERT(optptr == mp->b_wptr); 15948 return (mp); 15949 } 15950 15951 15952 /* 15953 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15954 * or a "bad" IRE detected by tcp_adapt_ire. 15955 * We can't tell if the failure was due to the laddr or the faddr 15956 * thus we clear out all addresses and ports. 15957 */ 15958 static void 15959 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15960 { 15961 queue_t *q = tcp->tcp_rq; 15962 tcph_t *tcph; 15963 struct T_error_ack *tea; 15964 conn_t *connp = tcp->tcp_connp; 15965 15966 15967 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15968 15969 if (mp->b_cont) { 15970 freemsg(mp->b_cont); 15971 mp->b_cont = NULL; 15972 } 15973 tea = (struct T_error_ack *)mp->b_rptr; 15974 switch (tea->PRIM_type) { 15975 case T_BIND_ACK: 15976 /* 15977 * Need to unbind with classifier since we were just told that 15978 * our bind succeeded. 15979 */ 15980 tcp->tcp_hard_bound = B_FALSE; 15981 tcp->tcp_hard_binding = B_FALSE; 15982 15983 ipcl_hash_remove(connp); 15984 /* Reuse the mblk if possible */ 15985 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15986 sizeof (*tea)); 15987 mp->b_rptr = mp->b_datap->db_base; 15988 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15989 tea = (struct T_error_ack *)mp->b_rptr; 15990 tea->PRIM_type = T_ERROR_ACK; 15991 tea->TLI_error = TSYSERR; 15992 tea->UNIX_error = error; 15993 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15994 tea->ERROR_prim = T_CONN_REQ; 15995 } else { 15996 tea->ERROR_prim = O_T_BIND_REQ; 15997 } 15998 break; 15999 16000 case T_ERROR_ACK: 16001 if (tcp->tcp_state >= TCPS_SYN_SENT) 16002 tea->ERROR_prim = T_CONN_REQ; 16003 break; 16004 default: 16005 panic("tcp_bind_failed: unexpected TPI type"); 16006 /*NOTREACHED*/ 16007 } 16008 16009 tcp->tcp_state = TCPS_IDLE; 16010 if (tcp->tcp_ipversion == IPV4_VERSION) 16011 tcp->tcp_ipha->ipha_src = 0; 16012 else 16013 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 16014 /* 16015 * Copy of the src addr. in tcp_t is needed since 16016 * the lookup funcs. can only look at tcp_t 16017 */ 16018 V6_SET_ZERO(tcp->tcp_ip_src_v6); 16019 16020 tcph = tcp->tcp_tcph; 16021 tcph->th_lport[0] = 0; 16022 tcph->th_lport[1] = 0; 16023 tcp_bind_hash_remove(tcp); 16024 bzero(&connp->u_port, sizeof (connp->u_port)); 16025 /* blow away saved option results if any */ 16026 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 16027 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 16028 16029 conn_delete_ire(tcp->tcp_connp, NULL); 16030 putnext(q, mp); 16031 } 16032 16033 /* 16034 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 16035 * messages. 16036 */ 16037 void 16038 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 16039 { 16040 mblk_t *mp1; 16041 uchar_t *rptr = mp->b_rptr; 16042 queue_t *q = tcp->tcp_rq; 16043 struct T_error_ack *tea; 16044 uint32_t mss; 16045 mblk_t *syn_mp; 16046 mblk_t *mdti; 16047 mblk_t *lsoi; 16048 int retval; 16049 mblk_t *ire_mp; 16050 tcp_stack_t *tcps = tcp->tcp_tcps; 16051 16052 switch (mp->b_datap->db_type) { 16053 case M_PROTO: 16054 case M_PCPROTO: 16055 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 16056 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 16057 break; 16058 tea = (struct T_error_ack *)rptr; 16059 switch (tea->PRIM_type) { 16060 case T_BIND_ACK: 16061 /* 16062 * Adapt Multidata information, if any. The 16063 * following tcp_mdt_update routine will free 16064 * the message. 16065 */ 16066 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 16067 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 16068 b_rptr)->mdt_capab, B_TRUE); 16069 freemsg(mdti); 16070 } 16071 16072 /* 16073 * Check to update LSO information with tcp, and 16074 * tcp_lso_update routine will free the message. 16075 */ 16076 if ((lsoi = tcp_lso_info_mp(mp)) != NULL) { 16077 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 16078 b_rptr)->lso_capab); 16079 freemsg(lsoi); 16080 } 16081 16082 /* Get the IRE, if we had requested for it */ 16083 ire_mp = tcp_ire_mp(mp); 16084 16085 if (tcp->tcp_hard_binding) { 16086 tcp->tcp_hard_binding = B_FALSE; 16087 tcp->tcp_hard_bound = B_TRUE; 16088 CL_INET_CONNECT(tcp); 16089 } else { 16090 if (ire_mp != NULL) 16091 freeb(ire_mp); 16092 goto after_syn_sent; 16093 } 16094 16095 retval = tcp_adapt_ire(tcp, ire_mp); 16096 if (ire_mp != NULL) 16097 freeb(ire_mp); 16098 if (retval == 0) { 16099 tcp_bind_failed(tcp, mp, 16100 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 16101 ENETUNREACH : EADDRNOTAVAIL)); 16102 return; 16103 } 16104 /* 16105 * Don't let an endpoint connect to itself. 16106 * Also checked in tcp_connect() but that 16107 * check can't handle the case when the 16108 * local IP address is INADDR_ANY. 16109 */ 16110 if (tcp->tcp_ipversion == IPV4_VERSION) { 16111 if ((tcp->tcp_ipha->ipha_dst == 16112 tcp->tcp_ipha->ipha_src) && 16113 (BE16_EQL(tcp->tcp_tcph->th_lport, 16114 tcp->tcp_tcph->th_fport))) { 16115 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 16116 return; 16117 } 16118 } else { 16119 if (IN6_ARE_ADDR_EQUAL( 16120 &tcp->tcp_ip6h->ip6_dst, 16121 &tcp->tcp_ip6h->ip6_src) && 16122 (BE16_EQL(tcp->tcp_tcph->th_lport, 16123 tcp->tcp_tcph->th_fport))) { 16124 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 16125 return; 16126 } 16127 } 16128 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 16129 /* 16130 * This should not be possible! Just for 16131 * defensive coding... 16132 */ 16133 if (tcp->tcp_state != TCPS_SYN_SENT) 16134 goto after_syn_sent; 16135 16136 if (is_system_labeled() && 16137 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 16138 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 16139 return; 16140 } 16141 16142 ASSERT(q == tcp->tcp_rq); 16143 /* 16144 * tcp_adapt_ire() does not adjust 16145 * for TCP/IP header length. 16146 */ 16147 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 16148 16149 /* 16150 * Just make sure our rwnd is at 16151 * least tcp_recv_hiwat_mss * MSS 16152 * large, and round up to the nearest 16153 * MSS. 16154 * 16155 * We do the round up here because 16156 * we need to get the interface 16157 * MTU first before we can do the 16158 * round up. 16159 */ 16160 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 16161 tcps->tcps_recv_hiwat_minmss * mss); 16162 q->q_hiwat = tcp->tcp_rwnd; 16163 tcp_set_ws_value(tcp); 16164 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 16165 tcp->tcp_tcph->th_win); 16166 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 16167 tcp->tcp_snd_ws_ok = B_TRUE; 16168 16169 /* 16170 * Set tcp_snd_ts_ok to true 16171 * so that tcp_xmit_mp will 16172 * include the timestamp 16173 * option in the SYN segment. 16174 */ 16175 if (tcps->tcps_tstamp_always || 16176 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 16177 tcp->tcp_snd_ts_ok = B_TRUE; 16178 } 16179 16180 /* 16181 * tcp_snd_sack_ok can be set in 16182 * tcp_adapt_ire() if the sack metric 16183 * is set. So check it here also. 16184 */ 16185 if (tcps->tcps_sack_permitted == 2 || 16186 tcp->tcp_snd_sack_ok) { 16187 if (tcp->tcp_sack_info == NULL) { 16188 tcp->tcp_sack_info = 16189 kmem_cache_alloc( 16190 tcp_sack_info_cache, 16191 KM_SLEEP); 16192 } 16193 tcp->tcp_snd_sack_ok = B_TRUE; 16194 } 16195 16196 /* 16197 * Should we use ECN? Note that the current 16198 * default value (SunOS 5.9) of tcp_ecn_permitted 16199 * is 1. The reason for doing this is that there 16200 * are equipments out there that will drop ECN 16201 * enabled IP packets. Setting it to 1 avoids 16202 * compatibility problems. 16203 */ 16204 if (tcps->tcps_ecn_permitted == 2) 16205 tcp->tcp_ecn_ok = B_TRUE; 16206 16207 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 16208 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 16209 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 16210 if (syn_mp) { 16211 cred_t *cr; 16212 pid_t pid; 16213 16214 /* 16215 * Obtain the credential from the 16216 * thread calling connect(); the credential 16217 * lives on in the second mblk which 16218 * originated from T_CONN_REQ and is echoed 16219 * with the T_BIND_ACK from ip. If none 16220 * can be found, default to the creator 16221 * of the socket. 16222 */ 16223 if (mp->b_cont == NULL || 16224 (cr = DB_CRED(mp->b_cont)) == NULL) { 16225 cr = tcp->tcp_cred; 16226 pid = tcp->tcp_cpid; 16227 } else { 16228 pid = DB_CPID(mp->b_cont); 16229 } 16230 16231 TCP_RECORD_TRACE(tcp, syn_mp, 16232 TCP_TRACE_SEND_PKT); 16233 mblk_setcred(syn_mp, cr); 16234 DB_CPID(syn_mp) = pid; 16235 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 16236 } 16237 after_syn_sent: 16238 /* 16239 * A trailer mblk indicates a waiting client upstream. 16240 * We complete here the processing begun in 16241 * either tcp_bind() or tcp_connect() by passing 16242 * upstream the reply message they supplied. 16243 */ 16244 mp1 = mp; 16245 mp = mp->b_cont; 16246 freeb(mp1); 16247 if (mp) 16248 break; 16249 return; 16250 case T_ERROR_ACK: 16251 if (tcp->tcp_debug) { 16252 (void) strlog(TCP_MOD_ID, 0, 1, 16253 SL_TRACE|SL_ERROR, 16254 "tcp_rput_other: case T_ERROR_ACK, " 16255 "ERROR_prim == %d", 16256 tea->ERROR_prim); 16257 } 16258 switch (tea->ERROR_prim) { 16259 case O_T_BIND_REQ: 16260 case T_BIND_REQ: 16261 tcp_bind_failed(tcp, mp, 16262 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 16263 ENETUNREACH : EADDRNOTAVAIL)); 16264 return; 16265 case T_UNBIND_REQ: 16266 tcp->tcp_hard_binding = B_FALSE; 16267 tcp->tcp_hard_bound = B_FALSE; 16268 if (mp->b_cont) { 16269 freemsg(mp->b_cont); 16270 mp->b_cont = NULL; 16271 } 16272 if (tcp->tcp_unbind_pending) 16273 tcp->tcp_unbind_pending = 0; 16274 else { 16275 /* From tcp_ip_unbind() - free */ 16276 freemsg(mp); 16277 return; 16278 } 16279 break; 16280 case T_SVR4_OPTMGMT_REQ: 16281 if (tcp->tcp_drop_opt_ack_cnt > 0) { 16282 /* T_OPTMGMT_REQ generated by TCP */ 16283 printf("T_SVR4_OPTMGMT_REQ failed " 16284 "%d/%d - dropped (cnt %d)\n", 16285 tea->TLI_error, tea->UNIX_error, 16286 tcp->tcp_drop_opt_ack_cnt); 16287 freemsg(mp); 16288 tcp->tcp_drop_opt_ack_cnt--; 16289 return; 16290 } 16291 break; 16292 } 16293 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 16294 tcp->tcp_drop_opt_ack_cnt > 0) { 16295 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 16296 "- dropped (cnt %d)\n", 16297 tea->TLI_error, tea->UNIX_error, 16298 tcp->tcp_drop_opt_ack_cnt); 16299 freemsg(mp); 16300 tcp->tcp_drop_opt_ack_cnt--; 16301 return; 16302 } 16303 break; 16304 case T_OPTMGMT_ACK: 16305 if (tcp->tcp_drop_opt_ack_cnt > 0) { 16306 /* T_OPTMGMT_REQ generated by TCP */ 16307 freemsg(mp); 16308 tcp->tcp_drop_opt_ack_cnt--; 16309 return; 16310 } 16311 break; 16312 default: 16313 break; 16314 } 16315 break; 16316 case M_FLUSH: 16317 if (*rptr & FLUSHR) 16318 flushq(q, FLUSHDATA); 16319 break; 16320 default: 16321 /* M_CTL will be directly sent to tcp_icmp_error() */ 16322 ASSERT(DB_TYPE(mp) != M_CTL); 16323 break; 16324 } 16325 /* 16326 * Make sure we set this bit before sending the ACK for 16327 * bind. Otherwise accept could possibly run and free 16328 * this tcp struct. 16329 */ 16330 putnext(q, mp); 16331 } 16332 16333 /* 16334 * Called as the result of a qbufcall or a qtimeout to remedy a failure 16335 * to allocate a T_ordrel_ind in tcp_rsrv(). qenable(q) will make 16336 * tcp_rsrv() try again. 16337 */ 16338 static void 16339 tcp_ordrel_kick(void *arg) 16340 { 16341 conn_t *connp = (conn_t *)arg; 16342 tcp_t *tcp = connp->conn_tcp; 16343 16344 tcp->tcp_ordrelid = 0; 16345 tcp->tcp_timeout = B_FALSE; 16346 if (!TCP_IS_DETACHED(tcp) && tcp->tcp_rq != NULL && 16347 tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 16348 qenable(tcp->tcp_rq); 16349 } 16350 } 16351 16352 /* ARGSUSED */ 16353 static void 16354 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 16355 { 16356 conn_t *connp = (conn_t *)arg; 16357 tcp_t *tcp = connp->conn_tcp; 16358 queue_t *q = tcp->tcp_rq; 16359 uint_t thwin; 16360 tcp_stack_t *tcps = tcp->tcp_tcps; 16361 sodirect_t *sodp; 16362 boolean_t fc; 16363 16364 freeb(mp); 16365 16366 TCP_STAT(tcps, tcp_rsrv_calls); 16367 16368 if (TCP_IS_DETACHED(tcp) || q == NULL) { 16369 return; 16370 } 16371 16372 if (tcp->tcp_fused) { 16373 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16374 16375 ASSERT(tcp->tcp_fused); 16376 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 16377 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 16378 ASSERT(!TCP_IS_DETACHED(tcp)); 16379 ASSERT(tcp->tcp_connp->conn_sqp == 16380 peer_tcp->tcp_connp->conn_sqp); 16381 16382 /* 16383 * Normally we would not get backenabled in synchronous 16384 * streams mode, but in case this happens, we need to plug 16385 * synchronous streams during our drain to prevent a race 16386 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 16387 */ 16388 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 16389 if (tcp->tcp_rcv_list != NULL) 16390 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 16391 16392 if (peer_tcp > tcp) { 16393 mutex_enter(&peer_tcp->tcp_non_sq_lock); 16394 mutex_enter(&tcp->tcp_non_sq_lock); 16395 } else { 16396 mutex_enter(&tcp->tcp_non_sq_lock); 16397 mutex_enter(&peer_tcp->tcp_non_sq_lock); 16398 } 16399 16400 if (peer_tcp->tcp_flow_stopped && 16401 (TCP_UNSENT_BYTES(peer_tcp) <= 16402 peer_tcp->tcp_xmit_lowater)) { 16403 tcp_clrqfull(peer_tcp); 16404 } 16405 mutex_exit(&peer_tcp->tcp_non_sq_lock); 16406 mutex_exit(&tcp->tcp_non_sq_lock); 16407 16408 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 16409 TCP_STAT(tcps, tcp_fusion_backenabled); 16410 return; 16411 } 16412 16413 SOD_PTR_ENTER(tcp, sodp); 16414 if (sodp != NULL) { 16415 /* An sodirect connection */ 16416 if (SOD_QFULL(sodp)) { 16417 /* Flow-controlled, need another back-enable */ 16418 fc = B_TRUE; 16419 SOD_QSETBE(sodp); 16420 } else { 16421 /* Not flow-controlled */ 16422 fc = B_FALSE; 16423 } 16424 mutex_exit(sodp->sod_lock); 16425 } else if (canputnext(q)) { 16426 /* STREAMS, not flow-controlled */ 16427 fc = B_FALSE; 16428 } else { 16429 /* STREAMS, flow-controlled */ 16430 fc = B_TRUE; 16431 } 16432 if (!fc) { 16433 /* Not flow-controlled, open rwnd */ 16434 tcp->tcp_rwnd = q->q_hiwat; 16435 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 16436 << tcp->tcp_rcv_ws; 16437 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 16438 /* 16439 * Send back a window update immediately if TCP is above 16440 * ESTABLISHED state and the increase of the rcv window 16441 * that the other side knows is at least 1 MSS after flow 16442 * control is lifted. 16443 */ 16444 if (tcp->tcp_state >= TCPS_ESTABLISHED && 16445 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 16446 tcp_xmit_ctl(NULL, tcp, 16447 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 16448 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 16449 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 16450 } 16451 } 16452 16453 /* Handle a failure to allocate a T_ORDREL_IND here */ 16454 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 16455 ASSERT(tcp->tcp_listener == NULL); 16456 16457 SOD_PTR_ENTER(tcp, sodp); 16458 if (sodp != NULL) { 16459 /* No more sodirect */ 16460 tcp->tcp_sodirect = NULL; 16461 if (!SOD_QEMPTY(sodp)) { 16462 /* Notify mblk(s) to process */ 16463 (void) tcp_rcv_sod_wakeup(tcp, sodp); 16464 /* sod_wakeup() does the mutex_exit() */ 16465 } else { 16466 /* Nothing to process */ 16467 mutex_exit(sodp->sod_lock); 16468 } 16469 } else if (tcp->tcp_rcv_list != NULL) { 16470 /* 16471 * Push any mblk(s) enqueued from co processing. 16472 */ 16473 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 16474 ASSERT(tcp->tcp_rcv_list == NULL || 16475 tcp->tcp_fused_sigurg); 16476 } 16477 16478 mp = mi_tpi_ordrel_ind(); 16479 if (mp) { 16480 tcp->tcp_ordrel_done = B_TRUE; 16481 putnext(q, mp); 16482 if (tcp->tcp_deferred_clean_death) { 16483 /* 16484 * tcp_clean_death was deferred for 16485 * T_ORDREL_IND - do it now 16486 */ 16487 tcp->tcp_deferred_clean_death = B_FALSE; 16488 (void) tcp_clean_death(tcp, 16489 tcp->tcp_client_errno, 22); 16490 } 16491 } else if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 16492 /* 16493 * If there isn't already a timer running 16494 * start one. Use a 4 second 16495 * timer as a fallback since it can't fail. 16496 */ 16497 tcp->tcp_timeout = B_TRUE; 16498 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 16499 MSEC_TO_TICK(4000)); 16500 } 16501 } 16502 } 16503 16504 /* 16505 * The read side service routine is called mostly when we get back-enabled as a 16506 * result of flow control relief. Since we don't actually queue anything in 16507 * TCP, we have no data to send out of here. What we do is clear the receive 16508 * window, and send out a window update. 16509 * This routine is also called to drive an orderly release message upstream 16510 * if the attempt in tcp_rput failed. 16511 */ 16512 static void 16513 tcp_rsrv(queue_t *q) 16514 { 16515 conn_t *connp = Q_TO_CONN(q); 16516 tcp_t *tcp = connp->conn_tcp; 16517 mblk_t *mp; 16518 tcp_stack_t *tcps = tcp->tcp_tcps; 16519 16520 /* No code does a putq on the read side */ 16521 ASSERT(q->q_first == NULL); 16522 16523 /* Nothing to do for the default queue */ 16524 if (q == tcps->tcps_g_q) { 16525 return; 16526 } 16527 16528 mp = allocb(0, BPRI_HI); 16529 if (mp == NULL) { 16530 /* 16531 * We are under memory pressure. Return for now and we 16532 * we will be called again later. 16533 */ 16534 if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 16535 /* 16536 * If there isn't already a timer running 16537 * start one. Use a 4 second 16538 * timer as a fallback since it can't fail. 16539 */ 16540 tcp->tcp_timeout = B_TRUE; 16541 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 16542 MSEC_TO_TICK(4000)); 16543 } 16544 return; 16545 } 16546 CONN_INC_REF(connp); 16547 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16548 SQTAG_TCP_RSRV); 16549 } 16550 16551 /* 16552 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16553 * We do not allow the receive window to shrink. After setting rwnd, 16554 * set the flow control hiwat of the stream. 16555 * 16556 * This function is called in 2 cases: 16557 * 16558 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16559 * connection (passive open) and in tcp_rput_data() for active connect. 16560 * This is called after tcp_mss_set() when the desired MSS value is known. 16561 * This makes sure that our window size is a mutiple of the other side's 16562 * MSS. 16563 * 2) Handling SO_RCVBUF option. 16564 * 16565 * It is ASSUMED that the requested size is a multiple of the current MSS. 16566 * 16567 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16568 * user requests so. 16569 */ 16570 static int 16571 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16572 { 16573 uint32_t mss = tcp->tcp_mss; 16574 uint32_t old_max_rwnd; 16575 uint32_t max_transmittable_rwnd; 16576 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16577 tcp_stack_t *tcps = tcp->tcp_tcps; 16578 16579 if (tcp->tcp_fused) { 16580 size_t sth_hiwat; 16581 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16582 16583 ASSERT(peer_tcp != NULL); 16584 /* 16585 * Record the stream head's high water mark for 16586 * this endpoint; this is used for flow-control 16587 * purposes in tcp_fuse_output(). 16588 */ 16589 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16590 if (!tcp_detached) 16591 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 16592 16593 /* 16594 * In the fusion case, the maxpsz stream head value of 16595 * our peer is set according to its send buffer size 16596 * and our receive buffer size; since the latter may 16597 * have changed we need to update the peer's maxpsz. 16598 */ 16599 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16600 return (rwnd); 16601 } 16602 16603 if (tcp_detached) 16604 old_max_rwnd = tcp->tcp_rwnd; 16605 else 16606 old_max_rwnd = tcp->tcp_rq->q_hiwat; 16607 16608 /* 16609 * Insist on a receive window that is at least 16610 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16611 * funny TCP interactions of Nagle algorithm, SWS avoidance 16612 * and delayed acknowledgement. 16613 */ 16614 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16615 16616 /* 16617 * If window size info has already been exchanged, TCP should not 16618 * shrink the window. Shrinking window is doable if done carefully. 16619 * We may add that support later. But so far there is not a real 16620 * need to do that. 16621 */ 16622 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16623 /* MSS may have changed, do a round up again. */ 16624 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16625 } 16626 16627 /* 16628 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16629 * can be applied even before the window scale option is decided. 16630 */ 16631 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16632 if (rwnd > max_transmittable_rwnd) { 16633 rwnd = max_transmittable_rwnd - 16634 (max_transmittable_rwnd % mss); 16635 if (rwnd < mss) 16636 rwnd = max_transmittable_rwnd; 16637 /* 16638 * If we're over the limit we may have to back down tcp_rwnd. 16639 * The increment below won't work for us. So we set all three 16640 * here and the increment below will have no effect. 16641 */ 16642 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16643 } 16644 if (tcp->tcp_localnet) { 16645 tcp->tcp_rack_abs_max = 16646 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16647 } else { 16648 /* 16649 * For a remote host on a different subnet (through a router), 16650 * we ack every other packet to be conforming to RFC1122. 16651 * tcp_deferred_acks_max is default to 2. 16652 */ 16653 tcp->tcp_rack_abs_max = 16654 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16655 } 16656 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16657 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16658 else 16659 tcp->tcp_rack_cur_max = 0; 16660 /* 16661 * Increment the current rwnd by the amount the maximum grew (we 16662 * can not overwrite it since we might be in the middle of a 16663 * connection.) 16664 */ 16665 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16666 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16667 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16668 tcp->tcp_cwnd_max = rwnd; 16669 16670 if (tcp_detached) 16671 return (rwnd); 16672 /* 16673 * We set the maximum receive window into rq->q_hiwat. 16674 * This is not actually used for flow control. 16675 */ 16676 tcp->tcp_rq->q_hiwat = rwnd; 16677 /* 16678 * Set the Stream head high water mark. This doesn't have to be 16679 * here, since we are simply using default values, but we would 16680 * prefer to choose these values algorithmically, with a likely 16681 * relationship to rwnd. 16682 */ 16683 (void) mi_set_sth_hiwat(tcp->tcp_rq, 16684 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16685 return (rwnd); 16686 } 16687 16688 /* 16689 * Return SNMP stuff in buffer in mpdata. 16690 */ 16691 mblk_t * 16692 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16693 { 16694 mblk_t *mpdata; 16695 mblk_t *mp_conn_ctl = NULL; 16696 mblk_t *mp_conn_tail; 16697 mblk_t *mp_attr_ctl = NULL; 16698 mblk_t *mp_attr_tail; 16699 mblk_t *mp6_conn_ctl = NULL; 16700 mblk_t *mp6_conn_tail; 16701 mblk_t *mp6_attr_ctl = NULL; 16702 mblk_t *mp6_attr_tail; 16703 struct opthdr *optp; 16704 mib2_tcpConnEntry_t tce; 16705 mib2_tcp6ConnEntry_t tce6; 16706 mib2_transportMLPEntry_t mlp; 16707 connf_t *connfp; 16708 int i; 16709 boolean_t ispriv; 16710 zoneid_t zoneid; 16711 int v4_conn_idx; 16712 int v6_conn_idx; 16713 conn_t *connp = Q_TO_CONN(q); 16714 tcp_stack_t *tcps; 16715 ip_stack_t *ipst; 16716 mblk_t *mp2ctl; 16717 16718 /* 16719 * make a copy of the original message 16720 */ 16721 mp2ctl = copymsg(mpctl); 16722 16723 if (mpctl == NULL || 16724 (mpdata = mpctl->b_cont) == NULL || 16725 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16726 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16727 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16728 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16729 freemsg(mp_conn_ctl); 16730 freemsg(mp_attr_ctl); 16731 freemsg(mp6_conn_ctl); 16732 freemsg(mp6_attr_ctl); 16733 freemsg(mpctl); 16734 freemsg(mp2ctl); 16735 return (NULL); 16736 } 16737 16738 ipst = connp->conn_netstack->netstack_ip; 16739 tcps = connp->conn_netstack->netstack_tcp; 16740 16741 /* build table of connections -- need count in fixed part */ 16742 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16743 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16744 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16745 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16746 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16747 16748 ispriv = 16749 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16750 zoneid = Q_TO_CONN(q)->conn_zoneid; 16751 16752 v4_conn_idx = v6_conn_idx = 0; 16753 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16754 16755 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16756 ipst = tcps->tcps_netstack->netstack_ip; 16757 16758 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16759 16760 connp = NULL; 16761 16762 while ((connp = 16763 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16764 tcp_t *tcp; 16765 boolean_t needattr; 16766 16767 if (connp->conn_zoneid != zoneid) 16768 continue; /* not in this zone */ 16769 16770 tcp = connp->conn_tcp; 16771 UPDATE_MIB(&tcps->tcps_mib, 16772 tcpHCInSegs, tcp->tcp_ibsegs); 16773 tcp->tcp_ibsegs = 0; 16774 UPDATE_MIB(&tcps->tcps_mib, 16775 tcpHCOutSegs, tcp->tcp_obsegs); 16776 tcp->tcp_obsegs = 0; 16777 16778 tce6.tcp6ConnState = tce.tcpConnState = 16779 tcp_snmp_state(tcp); 16780 if (tce.tcpConnState == MIB2_TCP_established || 16781 tce.tcpConnState == MIB2_TCP_closeWait) 16782 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16783 16784 needattr = B_FALSE; 16785 bzero(&mlp, sizeof (mlp)); 16786 if (connp->conn_mlp_type != mlptSingle) { 16787 if (connp->conn_mlp_type == mlptShared || 16788 connp->conn_mlp_type == mlptBoth) 16789 mlp.tme_flags |= MIB2_TMEF_SHARED; 16790 if (connp->conn_mlp_type == mlptPrivate || 16791 connp->conn_mlp_type == mlptBoth) 16792 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16793 needattr = B_TRUE; 16794 } 16795 if (connp->conn_peercred != NULL) { 16796 ts_label_t *tsl; 16797 16798 tsl = crgetlabel(connp->conn_peercred); 16799 mlp.tme_doi = label2doi(tsl); 16800 mlp.tme_label = *label2bslabel(tsl); 16801 needattr = B_TRUE; 16802 } 16803 16804 /* Create a message to report on IPv6 entries */ 16805 if (tcp->tcp_ipversion == IPV6_VERSION) { 16806 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16807 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16808 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16809 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16810 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16811 /* Don't want just anybody seeing these... */ 16812 if (ispriv) { 16813 tce6.tcp6ConnEntryInfo.ce_snxt = 16814 tcp->tcp_snxt; 16815 tce6.tcp6ConnEntryInfo.ce_suna = 16816 tcp->tcp_suna; 16817 tce6.tcp6ConnEntryInfo.ce_rnxt = 16818 tcp->tcp_rnxt; 16819 tce6.tcp6ConnEntryInfo.ce_rack = 16820 tcp->tcp_rack; 16821 } else { 16822 /* 16823 * Netstat, unfortunately, uses this to 16824 * get send/receive queue sizes. How to fix? 16825 * Why not compute the difference only? 16826 */ 16827 tce6.tcp6ConnEntryInfo.ce_snxt = 16828 tcp->tcp_snxt - tcp->tcp_suna; 16829 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16830 tce6.tcp6ConnEntryInfo.ce_rnxt = 16831 tcp->tcp_rnxt - tcp->tcp_rack; 16832 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16833 } 16834 16835 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16836 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16837 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16838 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16839 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16840 16841 tce6.tcp6ConnCreationProcess = 16842 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16843 tcp->tcp_cpid; 16844 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16845 16846 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16847 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16848 16849 mlp.tme_connidx = v6_conn_idx++; 16850 if (needattr) 16851 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16852 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16853 } 16854 /* 16855 * Create an IPv4 table entry for IPv4 entries and also 16856 * for IPv6 entries which are bound to in6addr_any 16857 * but don't have IPV6_V6ONLY set. 16858 * (i.e. anything an IPv4 peer could connect to) 16859 */ 16860 if (tcp->tcp_ipversion == IPV4_VERSION || 16861 (tcp->tcp_state <= TCPS_LISTEN && 16862 !tcp->tcp_connp->conn_ipv6_v6only && 16863 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16864 if (tcp->tcp_ipversion == IPV6_VERSION) { 16865 tce.tcpConnRemAddress = INADDR_ANY; 16866 tce.tcpConnLocalAddress = INADDR_ANY; 16867 } else { 16868 tce.tcpConnRemAddress = 16869 tcp->tcp_remote; 16870 tce.tcpConnLocalAddress = 16871 tcp->tcp_ip_src; 16872 } 16873 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16874 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16875 /* Don't want just anybody seeing these... */ 16876 if (ispriv) { 16877 tce.tcpConnEntryInfo.ce_snxt = 16878 tcp->tcp_snxt; 16879 tce.tcpConnEntryInfo.ce_suna = 16880 tcp->tcp_suna; 16881 tce.tcpConnEntryInfo.ce_rnxt = 16882 tcp->tcp_rnxt; 16883 tce.tcpConnEntryInfo.ce_rack = 16884 tcp->tcp_rack; 16885 } else { 16886 /* 16887 * Netstat, unfortunately, uses this to 16888 * get send/receive queue sizes. How 16889 * to fix? 16890 * Why not compute the difference only? 16891 */ 16892 tce.tcpConnEntryInfo.ce_snxt = 16893 tcp->tcp_snxt - tcp->tcp_suna; 16894 tce.tcpConnEntryInfo.ce_suna = 0; 16895 tce.tcpConnEntryInfo.ce_rnxt = 16896 tcp->tcp_rnxt - tcp->tcp_rack; 16897 tce.tcpConnEntryInfo.ce_rack = 0; 16898 } 16899 16900 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16901 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16902 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16903 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16904 tce.tcpConnEntryInfo.ce_state = 16905 tcp->tcp_state; 16906 16907 tce.tcpConnCreationProcess = 16908 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16909 tcp->tcp_cpid; 16910 tce.tcpConnCreationTime = tcp->tcp_open_time; 16911 16912 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16913 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16914 16915 mlp.tme_connidx = v4_conn_idx++; 16916 if (needattr) 16917 (void) snmp_append_data2( 16918 mp_attr_ctl->b_cont, 16919 &mp_attr_tail, (char *)&mlp, 16920 sizeof (mlp)); 16921 } 16922 } 16923 } 16924 16925 /* fixed length structure for IPv4 and IPv6 counters */ 16926 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16927 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16928 sizeof (mib2_tcp6ConnEntry_t)); 16929 /* synchronize 32- and 64-bit counters */ 16930 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16931 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16932 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16933 optp->level = MIB2_TCP; 16934 optp->name = 0; 16935 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16936 sizeof (tcps->tcps_mib)); 16937 optp->len = msgdsize(mpdata); 16938 qreply(q, mpctl); 16939 16940 /* table of connections... */ 16941 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16942 sizeof (struct T_optmgmt_ack)]; 16943 optp->level = MIB2_TCP; 16944 optp->name = MIB2_TCP_CONN; 16945 optp->len = msgdsize(mp_conn_ctl->b_cont); 16946 qreply(q, mp_conn_ctl); 16947 16948 /* table of MLP attributes... */ 16949 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16950 sizeof (struct T_optmgmt_ack)]; 16951 optp->level = MIB2_TCP; 16952 optp->name = EXPER_XPORT_MLP; 16953 optp->len = msgdsize(mp_attr_ctl->b_cont); 16954 if (optp->len == 0) 16955 freemsg(mp_attr_ctl); 16956 else 16957 qreply(q, mp_attr_ctl); 16958 16959 /* table of IPv6 connections... */ 16960 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16961 sizeof (struct T_optmgmt_ack)]; 16962 optp->level = MIB2_TCP6; 16963 optp->name = MIB2_TCP6_CONN; 16964 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16965 qreply(q, mp6_conn_ctl); 16966 16967 /* table of IPv6 MLP attributes... */ 16968 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16969 sizeof (struct T_optmgmt_ack)]; 16970 optp->level = MIB2_TCP6; 16971 optp->name = EXPER_XPORT_MLP; 16972 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16973 if (optp->len == 0) 16974 freemsg(mp6_attr_ctl); 16975 else 16976 qreply(q, mp6_attr_ctl); 16977 return (mp2ctl); 16978 } 16979 16980 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16981 /* ARGSUSED */ 16982 int 16983 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16984 { 16985 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16986 16987 switch (level) { 16988 case MIB2_TCP: 16989 switch (name) { 16990 case 13: 16991 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16992 return (0); 16993 /* TODO: delete entry defined by tce */ 16994 return (1); 16995 default: 16996 return (0); 16997 } 16998 default: 16999 return (1); 17000 } 17001 } 17002 17003 /* Translate TCP state to MIB2 TCP state. */ 17004 static int 17005 tcp_snmp_state(tcp_t *tcp) 17006 { 17007 if (tcp == NULL) 17008 return (0); 17009 17010 switch (tcp->tcp_state) { 17011 case TCPS_CLOSED: 17012 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 17013 case TCPS_BOUND: 17014 return (MIB2_TCP_closed); 17015 case TCPS_LISTEN: 17016 return (MIB2_TCP_listen); 17017 case TCPS_SYN_SENT: 17018 return (MIB2_TCP_synSent); 17019 case TCPS_SYN_RCVD: 17020 return (MIB2_TCP_synReceived); 17021 case TCPS_ESTABLISHED: 17022 return (MIB2_TCP_established); 17023 case TCPS_CLOSE_WAIT: 17024 return (MIB2_TCP_closeWait); 17025 case TCPS_FIN_WAIT_1: 17026 return (MIB2_TCP_finWait1); 17027 case TCPS_CLOSING: 17028 return (MIB2_TCP_closing); 17029 case TCPS_LAST_ACK: 17030 return (MIB2_TCP_lastAck); 17031 case TCPS_FIN_WAIT_2: 17032 return (MIB2_TCP_finWait2); 17033 case TCPS_TIME_WAIT: 17034 return (MIB2_TCP_timeWait); 17035 default: 17036 return (0); 17037 } 17038 } 17039 17040 static char tcp_report_header[] = 17041 "TCP " MI_COL_HDRPAD_STR 17042 "zone dest snxt suna " 17043 "swnd rnxt rack rwnd rto mss w sw rw t " 17044 "recent [lport,fport] state"; 17045 17046 /* 17047 * TCP status report triggered via the Named Dispatch mechanism. 17048 */ 17049 /* ARGSUSED */ 17050 static void 17051 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 17052 cred_t *cr) 17053 { 17054 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 17055 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 17056 char cflag; 17057 in6_addr_t v6dst; 17058 char buf[80]; 17059 uint_t print_len, buf_len; 17060 17061 buf_len = mp->b_datap->db_lim - mp->b_wptr; 17062 if (buf_len <= 0) 17063 return; 17064 17065 if (hashval >= 0) 17066 (void) sprintf(hash, "%03d ", hashval); 17067 else 17068 hash[0] = '\0'; 17069 17070 /* 17071 * Note that we use the remote address in the tcp_b structure. 17072 * This means that it will print out the real destination address, 17073 * not the next hop's address if source routing is used. This 17074 * avoid the confusion on the output because user may not 17075 * know that source routing is used for a connection. 17076 */ 17077 if (tcp->tcp_ipversion == IPV4_VERSION) { 17078 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 17079 } else { 17080 v6dst = tcp->tcp_remote_v6; 17081 } 17082 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 17083 /* 17084 * the ispriv checks are so that normal users cannot determine 17085 * sequence number information using NDD. 17086 */ 17087 17088 if (TCP_IS_DETACHED(tcp)) 17089 cflag = '*'; 17090 else 17091 cflag = ' '; 17092 print_len = snprintf((char *)mp->b_wptr, buf_len, 17093 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 17094 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 17095 hash, 17096 (void *)tcp, 17097 tcp->tcp_connp->conn_zoneid, 17098 addrbuf, 17099 (ispriv) ? tcp->tcp_snxt : 0, 17100 (ispriv) ? tcp->tcp_suna : 0, 17101 tcp->tcp_swnd, 17102 (ispriv) ? tcp->tcp_rnxt : 0, 17103 (ispriv) ? tcp->tcp_rack : 0, 17104 tcp->tcp_rwnd, 17105 tcp->tcp_rto, 17106 tcp->tcp_mss, 17107 tcp->tcp_snd_ws_ok, 17108 tcp->tcp_snd_ws, 17109 tcp->tcp_rcv_ws, 17110 tcp->tcp_snd_ts_ok, 17111 tcp->tcp_ts_recent, 17112 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 17113 if (print_len < buf_len) { 17114 ((mblk_t *)mp)->b_wptr += print_len; 17115 } else { 17116 ((mblk_t *)mp)->b_wptr += buf_len; 17117 } 17118 } 17119 17120 /* 17121 * TCP status report (for listeners only) triggered via the Named Dispatch 17122 * mechanism. 17123 */ 17124 /* ARGSUSED */ 17125 static void 17126 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 17127 { 17128 char addrbuf[INET6_ADDRSTRLEN]; 17129 in6_addr_t v6dst; 17130 uint_t print_len, buf_len; 17131 17132 buf_len = mp->b_datap->db_lim - mp->b_wptr; 17133 if (buf_len <= 0) 17134 return; 17135 17136 if (tcp->tcp_ipversion == IPV4_VERSION) { 17137 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 17138 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 17139 } else { 17140 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 17141 addrbuf, sizeof (addrbuf)); 17142 } 17143 print_len = snprintf((char *)mp->b_wptr, buf_len, 17144 "%03d " 17145 MI_COL_PTRFMT_STR 17146 "%d %s %05u %08u %d/%d/%d%c\n", 17147 hashval, (void *)tcp, 17148 tcp->tcp_connp->conn_zoneid, 17149 addrbuf, 17150 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 17151 tcp->tcp_conn_req_seqnum, 17152 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 17153 tcp->tcp_conn_req_max, 17154 tcp->tcp_syn_defense ? '*' : ' '); 17155 if (print_len < buf_len) { 17156 ((mblk_t *)mp)->b_wptr += print_len; 17157 } else { 17158 ((mblk_t *)mp)->b_wptr += buf_len; 17159 } 17160 } 17161 17162 /* TCP status report triggered via the Named Dispatch mechanism. */ 17163 /* ARGSUSED */ 17164 static int 17165 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17166 { 17167 tcp_t *tcp; 17168 int i; 17169 conn_t *connp; 17170 connf_t *connfp; 17171 zoneid_t zoneid; 17172 tcp_stack_t *tcps; 17173 ip_stack_t *ipst; 17174 17175 zoneid = Q_TO_CONN(q)->conn_zoneid; 17176 tcps = Q_TO_TCP(q)->tcp_tcps; 17177 17178 /* 17179 * Because of the ndd constraint, at most we can have 64K buffer 17180 * to put in all TCP info. So to be more efficient, just 17181 * allocate a 64K buffer here, assuming we need that large buffer. 17182 * This may be a problem as any user can read tcp_status. Therefore 17183 * we limit the rate of doing this using tcp_ndd_get_info_interval. 17184 * This should be OK as normal users should not do this too often. 17185 */ 17186 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17187 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17188 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17189 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17190 return (0); 17191 } 17192 } 17193 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17194 /* The following may work even if we cannot get a large buf. */ 17195 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17196 return (0); 17197 } 17198 17199 (void) mi_mpprintf(mp, "%s", tcp_report_header); 17200 17201 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 17202 17203 ipst = tcps->tcps_netstack->netstack_ip; 17204 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 17205 17206 connp = NULL; 17207 17208 while ((connp = 17209 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17210 tcp = connp->conn_tcp; 17211 if (zoneid != GLOBAL_ZONEID && 17212 zoneid != connp->conn_zoneid) 17213 continue; 17214 tcp_report_item(mp->b_cont, tcp, -1, tcp, 17215 cr); 17216 } 17217 17218 } 17219 17220 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17221 return (0); 17222 } 17223 17224 /* TCP status report triggered via the Named Dispatch mechanism. */ 17225 /* ARGSUSED */ 17226 static int 17227 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17228 { 17229 tf_t *tbf; 17230 tcp_t *tcp; 17231 int i; 17232 zoneid_t zoneid; 17233 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 17234 17235 zoneid = Q_TO_CONN(q)->conn_zoneid; 17236 17237 /* Refer to comments in tcp_status_report(). */ 17238 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17239 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17240 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17241 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17242 return (0); 17243 } 17244 } 17245 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17246 /* The following may work even if we cannot get a large buf. */ 17247 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17248 return (0); 17249 } 17250 17251 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17252 17253 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 17254 tbf = &tcps->tcps_bind_fanout[i]; 17255 mutex_enter(&tbf->tf_lock); 17256 for (tcp = tbf->tf_tcp; tcp != NULL; 17257 tcp = tcp->tcp_bind_hash) { 17258 if (zoneid != GLOBAL_ZONEID && 17259 zoneid != tcp->tcp_connp->conn_zoneid) 17260 continue; 17261 CONN_INC_REF(tcp->tcp_connp); 17262 tcp_report_item(mp->b_cont, tcp, i, 17263 Q_TO_TCP(q), cr); 17264 CONN_DEC_REF(tcp->tcp_connp); 17265 } 17266 mutex_exit(&tbf->tf_lock); 17267 } 17268 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17269 return (0); 17270 } 17271 17272 /* TCP status report triggered via the Named Dispatch mechanism. */ 17273 /* ARGSUSED */ 17274 static int 17275 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17276 { 17277 connf_t *connfp; 17278 conn_t *connp; 17279 tcp_t *tcp; 17280 int i; 17281 zoneid_t zoneid; 17282 tcp_stack_t *tcps; 17283 ip_stack_t *ipst; 17284 17285 zoneid = Q_TO_CONN(q)->conn_zoneid; 17286 tcps = Q_TO_TCP(q)->tcp_tcps; 17287 17288 /* Refer to comments in tcp_status_report(). */ 17289 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17290 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17291 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17292 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17293 return (0); 17294 } 17295 } 17296 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17297 /* The following may work even if we cannot get a large buf. */ 17298 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17299 return (0); 17300 } 17301 17302 (void) mi_mpprintf(mp, 17303 " TCP " MI_COL_HDRPAD_STR 17304 "zone IP addr port seqnum backlog (q0/q/max)"); 17305 17306 ipst = tcps->tcps_netstack->netstack_ip; 17307 17308 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 17309 connfp = &ipst->ips_ipcl_bind_fanout[i]; 17310 connp = NULL; 17311 while ((connp = 17312 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17313 tcp = connp->conn_tcp; 17314 if (zoneid != GLOBAL_ZONEID && 17315 zoneid != connp->conn_zoneid) 17316 continue; 17317 tcp_report_listener(mp->b_cont, tcp, i); 17318 } 17319 } 17320 17321 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17322 return (0); 17323 } 17324 17325 /* TCP status report triggered via the Named Dispatch mechanism. */ 17326 /* ARGSUSED */ 17327 static int 17328 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17329 { 17330 connf_t *connfp; 17331 conn_t *connp; 17332 tcp_t *tcp; 17333 int i; 17334 zoneid_t zoneid; 17335 tcp_stack_t *tcps; 17336 ip_stack_t *ipst; 17337 17338 zoneid = Q_TO_CONN(q)->conn_zoneid; 17339 tcps = Q_TO_TCP(q)->tcp_tcps; 17340 ipst = tcps->tcps_netstack->netstack_ip; 17341 17342 /* Refer to comments in tcp_status_report(). */ 17343 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17344 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17345 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17346 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17347 return (0); 17348 } 17349 } 17350 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17351 /* The following may work even if we cannot get a large buf. */ 17352 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17353 return (0); 17354 } 17355 17356 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 17357 ipst->ips_ipcl_conn_fanout_size); 17358 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17359 17360 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 17361 connfp = &ipst->ips_ipcl_conn_fanout[i]; 17362 connp = NULL; 17363 while ((connp = 17364 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17365 tcp = connp->conn_tcp; 17366 if (zoneid != GLOBAL_ZONEID && 17367 zoneid != connp->conn_zoneid) 17368 continue; 17369 tcp_report_item(mp->b_cont, tcp, i, 17370 Q_TO_TCP(q), cr); 17371 } 17372 } 17373 17374 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17375 return (0); 17376 } 17377 17378 /* TCP status report triggered via the Named Dispatch mechanism. */ 17379 /* ARGSUSED */ 17380 static int 17381 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17382 { 17383 tf_t *tf; 17384 tcp_t *tcp; 17385 int i; 17386 zoneid_t zoneid; 17387 tcp_stack_t *tcps; 17388 17389 zoneid = Q_TO_CONN(q)->conn_zoneid; 17390 tcps = Q_TO_TCP(q)->tcp_tcps; 17391 17392 /* Refer to comments in tcp_status_report(). */ 17393 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17394 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17395 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17396 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17397 return (0); 17398 } 17399 } 17400 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17401 /* The following may work even if we cannot get a large buf. */ 17402 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17403 return (0); 17404 } 17405 17406 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17407 17408 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 17409 tf = &tcps->tcps_acceptor_fanout[i]; 17410 mutex_enter(&tf->tf_lock); 17411 for (tcp = tf->tf_tcp; tcp != NULL; 17412 tcp = tcp->tcp_acceptor_hash) { 17413 if (zoneid != GLOBAL_ZONEID && 17414 zoneid != tcp->tcp_connp->conn_zoneid) 17415 continue; 17416 tcp_report_item(mp->b_cont, tcp, i, 17417 Q_TO_TCP(q), cr); 17418 } 17419 mutex_exit(&tf->tf_lock); 17420 } 17421 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17422 return (0); 17423 } 17424 17425 /* 17426 * tcp_timer is the timer service routine. It handles the retransmission, 17427 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 17428 * from the state of the tcp instance what kind of action needs to be done 17429 * at the time it is called. 17430 */ 17431 static void 17432 tcp_timer(void *arg) 17433 { 17434 mblk_t *mp; 17435 clock_t first_threshold; 17436 clock_t second_threshold; 17437 clock_t ms; 17438 uint32_t mss; 17439 conn_t *connp = (conn_t *)arg; 17440 tcp_t *tcp = connp->conn_tcp; 17441 tcp_stack_t *tcps = tcp->tcp_tcps; 17442 17443 tcp->tcp_timer_tid = 0; 17444 17445 if (tcp->tcp_fused) 17446 return; 17447 17448 first_threshold = tcp->tcp_first_timer_threshold; 17449 second_threshold = tcp->tcp_second_timer_threshold; 17450 switch (tcp->tcp_state) { 17451 case TCPS_IDLE: 17452 case TCPS_BOUND: 17453 case TCPS_LISTEN: 17454 return; 17455 case TCPS_SYN_RCVD: { 17456 tcp_t *listener = tcp->tcp_listener; 17457 17458 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 17459 ASSERT(tcp->tcp_rq == listener->tcp_rq); 17460 /* it's our first timeout */ 17461 tcp->tcp_syn_rcvd_timeout = 1; 17462 mutex_enter(&listener->tcp_eager_lock); 17463 listener->tcp_syn_rcvd_timeout++; 17464 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 17465 /* 17466 * Make this eager available for drop if we 17467 * need to drop one to accomodate a new 17468 * incoming SYN request. 17469 */ 17470 MAKE_DROPPABLE(listener, tcp); 17471 } 17472 if (!listener->tcp_syn_defense && 17473 (listener->tcp_syn_rcvd_timeout > 17474 (tcps->tcps_conn_req_max_q0 >> 2)) && 17475 (tcps->tcps_conn_req_max_q0 > 200)) { 17476 /* We may be under attack. Put on a defense. */ 17477 listener->tcp_syn_defense = B_TRUE; 17478 cmn_err(CE_WARN, "High TCP connect timeout " 17479 "rate! System (port %d) may be under a " 17480 "SYN flood attack!", 17481 BE16_TO_U16(listener->tcp_tcph->th_lport)); 17482 17483 listener->tcp_ip_addr_cache = kmem_zalloc( 17484 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 17485 KM_NOSLEEP); 17486 } 17487 mutex_exit(&listener->tcp_eager_lock); 17488 } else if (listener != NULL) { 17489 mutex_enter(&listener->tcp_eager_lock); 17490 tcp->tcp_syn_rcvd_timeout++; 17491 if (tcp->tcp_syn_rcvd_timeout > 1 && 17492 !tcp->tcp_closemp_used) { 17493 /* 17494 * This is our second timeout. Put the tcp in 17495 * the list of droppable eagers to allow it to 17496 * be dropped, if needed. We don't check 17497 * whether tcp_dontdrop is set or not to 17498 * protect ourselve from a SYN attack where a 17499 * remote host can spoof itself as one of the 17500 * good IP source and continue to hold 17501 * resources too long. 17502 */ 17503 MAKE_DROPPABLE(listener, tcp); 17504 } 17505 mutex_exit(&listener->tcp_eager_lock); 17506 } 17507 } 17508 /* FALLTHRU */ 17509 case TCPS_SYN_SENT: 17510 first_threshold = tcp->tcp_first_ctimer_threshold; 17511 second_threshold = tcp->tcp_second_ctimer_threshold; 17512 break; 17513 case TCPS_ESTABLISHED: 17514 case TCPS_FIN_WAIT_1: 17515 case TCPS_CLOSING: 17516 case TCPS_CLOSE_WAIT: 17517 case TCPS_LAST_ACK: 17518 /* If we have data to rexmit */ 17519 if (tcp->tcp_suna != tcp->tcp_snxt) { 17520 clock_t time_to_wait; 17521 17522 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 17523 if (!tcp->tcp_xmit_head) 17524 break; 17525 time_to_wait = lbolt - 17526 (clock_t)tcp->tcp_xmit_head->b_prev; 17527 time_to_wait = tcp->tcp_rto - 17528 TICK_TO_MSEC(time_to_wait); 17529 /* 17530 * If the timer fires too early, 1 clock tick earlier, 17531 * restart the timer. 17532 */ 17533 if (time_to_wait > msec_per_tick) { 17534 TCP_STAT(tcps, tcp_timer_fire_early); 17535 TCP_TIMER_RESTART(tcp, time_to_wait); 17536 return; 17537 } 17538 /* 17539 * When we probe zero windows, we force the swnd open. 17540 * If our peer acks with a closed window swnd will be 17541 * set to zero by tcp_rput(). As long as we are 17542 * receiving acks tcp_rput will 17543 * reset 'tcp_ms_we_have_waited' so as not to trip the 17544 * first and second interval actions. NOTE: the timer 17545 * interval is allowed to continue its exponential 17546 * backoff. 17547 */ 17548 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17549 if (tcp->tcp_debug) { 17550 (void) strlog(TCP_MOD_ID, 0, 1, 17551 SL_TRACE, "tcp_timer: zero win"); 17552 } 17553 } else { 17554 /* 17555 * After retransmission, we need to do 17556 * slow start. Set the ssthresh to one 17557 * half of current effective window and 17558 * cwnd to one MSS. Also reset 17559 * tcp_cwnd_cnt. 17560 * 17561 * Note that if tcp_ssthresh is reduced because 17562 * of ECN, do not reduce it again unless it is 17563 * already one window of data away (tcp_cwr 17564 * should then be cleared) or this is a 17565 * timeout for a retransmitted segment. 17566 */ 17567 uint32_t npkt; 17568 17569 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17570 npkt = ((tcp->tcp_timer_backoff ? 17571 tcp->tcp_cwnd_ssthresh : 17572 tcp->tcp_snxt - 17573 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17574 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17575 tcp->tcp_mss; 17576 } 17577 tcp->tcp_cwnd = tcp->tcp_mss; 17578 tcp->tcp_cwnd_cnt = 0; 17579 if (tcp->tcp_ecn_ok) { 17580 tcp->tcp_cwr = B_TRUE; 17581 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17582 tcp->tcp_ecn_cwr_sent = B_FALSE; 17583 } 17584 } 17585 break; 17586 } 17587 /* 17588 * We have something to send yet we cannot send. The 17589 * reason can be: 17590 * 17591 * 1. Zero send window: we need to do zero window probe. 17592 * 2. Zero cwnd: because of ECN, we need to "clock out 17593 * segments. 17594 * 3. SWS avoidance: receiver may have shrunk window, 17595 * reset our knowledge. 17596 * 17597 * Note that condition 2 can happen with either 1 or 17598 * 3. But 1 and 3 are exclusive. 17599 */ 17600 if (tcp->tcp_unsent != 0) { 17601 if (tcp->tcp_cwnd == 0) { 17602 /* 17603 * Set tcp_cwnd to 1 MSS so that a 17604 * new segment can be sent out. We 17605 * are "clocking out" new data when 17606 * the network is really congested. 17607 */ 17608 ASSERT(tcp->tcp_ecn_ok); 17609 tcp->tcp_cwnd = tcp->tcp_mss; 17610 } 17611 if (tcp->tcp_swnd == 0) { 17612 /* Extend window for zero window probe */ 17613 tcp->tcp_swnd++; 17614 tcp->tcp_zero_win_probe = B_TRUE; 17615 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17616 } else { 17617 /* 17618 * Handle timeout from sender SWS avoidance. 17619 * Reset our knowledge of the max send window 17620 * since the receiver might have reduced its 17621 * receive buffer. Avoid setting tcp_max_swnd 17622 * to one since that will essentially disable 17623 * the SWS checks. 17624 * 17625 * Note that since we don't have a SWS 17626 * state variable, if the timeout is set 17627 * for ECN but not for SWS, this 17628 * code will also be executed. This is 17629 * fine as tcp_max_swnd is updated 17630 * constantly and it will not affect 17631 * anything. 17632 */ 17633 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17634 } 17635 tcp_wput_data(tcp, NULL, B_FALSE); 17636 return; 17637 } 17638 /* Is there a FIN that needs to be to re retransmitted? */ 17639 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17640 !tcp->tcp_fin_acked) 17641 break; 17642 /* Nothing to do, return without restarting timer. */ 17643 TCP_STAT(tcps, tcp_timer_fire_miss); 17644 return; 17645 case TCPS_FIN_WAIT_2: 17646 /* 17647 * User closed the TCP endpoint and peer ACK'ed our FIN. 17648 * We waited some time for for peer's FIN, but it hasn't 17649 * arrived. We flush the connection now to avoid 17650 * case where the peer has rebooted. 17651 */ 17652 if (TCP_IS_DETACHED(tcp)) { 17653 (void) tcp_clean_death(tcp, 0, 23); 17654 } else { 17655 TCP_TIMER_RESTART(tcp, 17656 tcps->tcps_fin_wait_2_flush_interval); 17657 } 17658 return; 17659 case TCPS_TIME_WAIT: 17660 (void) tcp_clean_death(tcp, 0, 24); 17661 return; 17662 default: 17663 if (tcp->tcp_debug) { 17664 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17665 "tcp_timer: strange state (%d) %s", 17666 tcp->tcp_state, tcp_display(tcp, NULL, 17667 DISP_PORT_ONLY)); 17668 } 17669 return; 17670 } 17671 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17672 /* 17673 * For zero window probe, we need to send indefinitely, 17674 * unless we have not heard from the other side for some 17675 * time... 17676 */ 17677 if ((tcp->tcp_zero_win_probe == 0) || 17678 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17679 second_threshold)) { 17680 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17681 /* 17682 * If TCP is in SYN_RCVD state, send back a 17683 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17684 * should be zero in TCPS_SYN_RCVD state. 17685 */ 17686 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17687 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17688 "in SYN_RCVD", 17689 tcp, tcp->tcp_snxt, 17690 tcp->tcp_rnxt, TH_RST | TH_ACK); 17691 } 17692 (void) tcp_clean_death(tcp, 17693 tcp->tcp_client_errno ? 17694 tcp->tcp_client_errno : ETIMEDOUT, 25); 17695 return; 17696 } else { 17697 /* 17698 * Set tcp_ms_we_have_waited to second_threshold 17699 * so that in next timeout, we will do the above 17700 * check (lbolt - tcp_last_recv_time). This is 17701 * also to avoid overflow. 17702 * 17703 * We don't need to decrement tcp_timer_backoff 17704 * to avoid overflow because it will be decremented 17705 * later if new timeout value is greater than 17706 * tcp_rexmit_interval_max. In the case when 17707 * tcp_rexmit_interval_max is greater than 17708 * second_threshold, it means that we will wait 17709 * longer than second_threshold to send the next 17710 * window probe. 17711 */ 17712 tcp->tcp_ms_we_have_waited = second_threshold; 17713 } 17714 } else if (ms > first_threshold) { 17715 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17716 tcp->tcp_xmit_head != NULL) { 17717 tcp->tcp_xmit_head = 17718 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17719 } 17720 /* 17721 * We have been retransmitting for too long... The RTT 17722 * we calculated is probably incorrect. Reinitialize it. 17723 * Need to compensate for 0 tcp_rtt_sa. Reset 17724 * tcp_rtt_update so that we won't accidentally cache a 17725 * bad value. But only do this if this is not a zero 17726 * window probe. 17727 */ 17728 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17729 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17730 (tcp->tcp_rtt_sa >> 5); 17731 tcp->tcp_rtt_sa = 0; 17732 tcp_ip_notify(tcp); 17733 tcp->tcp_rtt_update = 0; 17734 } 17735 } 17736 tcp->tcp_timer_backoff++; 17737 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17738 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17739 tcps->tcps_rexmit_interval_min) { 17740 /* 17741 * This means the original RTO is tcp_rexmit_interval_min. 17742 * So we will use tcp_rexmit_interval_min as the RTO value 17743 * and do the backoff. 17744 */ 17745 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17746 } else { 17747 ms <<= tcp->tcp_timer_backoff; 17748 } 17749 if (ms > tcps->tcps_rexmit_interval_max) { 17750 ms = tcps->tcps_rexmit_interval_max; 17751 /* 17752 * ms is at max, decrement tcp_timer_backoff to avoid 17753 * overflow. 17754 */ 17755 tcp->tcp_timer_backoff--; 17756 } 17757 tcp->tcp_ms_we_have_waited += ms; 17758 if (tcp->tcp_zero_win_probe == 0) { 17759 tcp->tcp_rto = ms; 17760 } 17761 TCP_TIMER_RESTART(tcp, ms); 17762 /* 17763 * This is after a timeout and tcp_rto is backed off. Set 17764 * tcp_set_timer to 1 so that next time RTO is updated, we will 17765 * restart the timer with a correct value. 17766 */ 17767 tcp->tcp_set_timer = 1; 17768 mss = tcp->tcp_snxt - tcp->tcp_suna; 17769 if (mss > tcp->tcp_mss) 17770 mss = tcp->tcp_mss; 17771 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17772 mss = tcp->tcp_swnd; 17773 17774 if ((mp = tcp->tcp_xmit_head) != NULL) 17775 mp->b_prev = (mblk_t *)lbolt; 17776 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17777 B_TRUE); 17778 17779 /* 17780 * When slow start after retransmission begins, start with 17781 * this seq no. tcp_rexmit_max marks the end of special slow 17782 * start phase. tcp_snd_burst controls how many segments 17783 * can be sent because of an ack. 17784 */ 17785 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17786 tcp->tcp_snd_burst = TCP_CWND_SS; 17787 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17788 (tcp->tcp_unsent == 0)) { 17789 tcp->tcp_rexmit_max = tcp->tcp_fss; 17790 } else { 17791 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17792 } 17793 tcp->tcp_rexmit = B_TRUE; 17794 tcp->tcp_dupack_cnt = 0; 17795 17796 /* 17797 * Remove all rexmit SACK blk to start from fresh. 17798 */ 17799 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17800 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17801 tcp->tcp_num_notsack_blk = 0; 17802 tcp->tcp_cnt_notsack_list = 0; 17803 } 17804 if (mp == NULL) { 17805 return; 17806 } 17807 /* Attach credentials to retransmitted initial SYNs. */ 17808 if (tcp->tcp_state == TCPS_SYN_SENT) { 17809 mblk_setcred(mp, tcp->tcp_cred); 17810 DB_CPID(mp) = tcp->tcp_cpid; 17811 } 17812 17813 tcp->tcp_csuna = tcp->tcp_snxt; 17814 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17815 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17816 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 17817 tcp_send_data(tcp, tcp->tcp_wq, mp); 17818 17819 } 17820 17821 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17822 static void 17823 tcp_unbind(tcp_t *tcp, mblk_t *mp) 17824 { 17825 conn_t *connp; 17826 17827 switch (tcp->tcp_state) { 17828 case TCPS_BOUND: 17829 case TCPS_LISTEN: 17830 break; 17831 default: 17832 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 17833 return; 17834 } 17835 17836 /* 17837 * Need to clean up all the eagers since after the unbind, segments 17838 * will no longer be delivered to this listener stream. 17839 */ 17840 mutex_enter(&tcp->tcp_eager_lock); 17841 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17842 tcp_eager_cleanup(tcp, 0); 17843 } 17844 mutex_exit(&tcp->tcp_eager_lock); 17845 17846 if (tcp->tcp_ipversion == IPV4_VERSION) { 17847 tcp->tcp_ipha->ipha_src = 0; 17848 } else { 17849 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17850 } 17851 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17852 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17853 tcp_bind_hash_remove(tcp); 17854 tcp->tcp_state = TCPS_IDLE; 17855 tcp->tcp_mdt = B_FALSE; 17856 /* Send M_FLUSH according to TPI */ 17857 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17858 connp = tcp->tcp_connp; 17859 connp->conn_mdt_ok = B_FALSE; 17860 ipcl_hash_remove(connp); 17861 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17862 mp = mi_tpi_ok_ack_alloc(mp); 17863 putnext(tcp->tcp_rq, mp); 17864 } 17865 17866 /* 17867 * Don't let port fall into the privileged range. 17868 * Since the extra privileged ports can be arbitrary we also 17869 * ensure that we exclude those from consideration. 17870 * tcp_g_epriv_ports is not sorted thus we loop over it until 17871 * there are no changes. 17872 * 17873 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17874 * but instead the code relies on: 17875 * - the fact that the address of the array and its size never changes 17876 * - the atomic assignment of the elements of the array 17877 * 17878 * Returns 0 if there are no more ports available. 17879 * 17880 * TS note: skip multilevel ports. 17881 */ 17882 static in_port_t 17883 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17884 { 17885 int i; 17886 boolean_t restart = B_FALSE; 17887 tcp_stack_t *tcps = tcp->tcp_tcps; 17888 17889 if (random && tcp_random_anon_port != 0) { 17890 (void) random_get_pseudo_bytes((uint8_t *)&port, 17891 sizeof (in_port_t)); 17892 /* 17893 * Unless changed by a sys admin, the smallest anon port 17894 * is 32768 and the largest anon port is 65535. It is 17895 * very likely (50%) for the random port to be smaller 17896 * than the smallest anon port. When that happens, 17897 * add port % (anon port range) to the smallest anon 17898 * port to get the random port. It should fall into the 17899 * valid anon port range. 17900 */ 17901 if (port < tcps->tcps_smallest_anon_port) { 17902 port = tcps->tcps_smallest_anon_port + 17903 port % (tcps->tcps_largest_anon_port - 17904 tcps->tcps_smallest_anon_port); 17905 } 17906 } 17907 17908 retry: 17909 if (port < tcps->tcps_smallest_anon_port) 17910 port = (in_port_t)tcps->tcps_smallest_anon_port; 17911 17912 if (port > tcps->tcps_largest_anon_port) { 17913 if (restart) 17914 return (0); 17915 restart = B_TRUE; 17916 port = (in_port_t)tcps->tcps_smallest_anon_port; 17917 } 17918 17919 if (port < tcps->tcps_smallest_nonpriv_port) 17920 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17921 17922 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17923 if (port == tcps->tcps_g_epriv_ports[i]) { 17924 port++; 17925 /* 17926 * Make sure whether the port is in the 17927 * valid range. 17928 */ 17929 goto retry; 17930 } 17931 } 17932 if (is_system_labeled() && 17933 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17934 IPPROTO_TCP, B_TRUE)) != 0) { 17935 port = i; 17936 goto retry; 17937 } 17938 return (port); 17939 } 17940 17941 /* 17942 * Return the next anonymous port in the privileged port range for 17943 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17944 * downwards. This is the same behavior as documented in the userland 17945 * library call rresvport(3N). 17946 * 17947 * TS note: skip multilevel ports. 17948 */ 17949 static in_port_t 17950 tcp_get_next_priv_port(const tcp_t *tcp) 17951 { 17952 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17953 in_port_t nextport; 17954 boolean_t restart = B_FALSE; 17955 tcp_stack_t *tcps = tcp->tcp_tcps; 17956 retry: 17957 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17958 next_priv_port >= IPPORT_RESERVED) { 17959 next_priv_port = IPPORT_RESERVED - 1; 17960 if (restart) 17961 return (0); 17962 restart = B_TRUE; 17963 } 17964 if (is_system_labeled() && 17965 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17966 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17967 next_priv_port = nextport; 17968 goto retry; 17969 } 17970 return (next_priv_port--); 17971 } 17972 17973 /* The write side r/w procedure. */ 17974 17975 #if CCS_STATS 17976 struct { 17977 struct { 17978 int64_t count, bytes; 17979 } tot, hit; 17980 } wrw_stats; 17981 #endif 17982 17983 /* 17984 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17985 * messages. 17986 */ 17987 /* ARGSUSED */ 17988 static void 17989 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17990 { 17991 conn_t *connp = (conn_t *)arg; 17992 tcp_t *tcp = connp->conn_tcp; 17993 queue_t *q = tcp->tcp_wq; 17994 17995 ASSERT(DB_TYPE(mp) != M_IOCTL); 17996 /* 17997 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17998 * Once the close starts, streamhead and sockfs will not let any data 17999 * packets come down (close ensures that there are no threads using the 18000 * queue and no new threads will come down) but since qprocsoff() 18001 * hasn't happened yet, a M_FLUSH or some non data message might 18002 * get reflected back (in response to our own FLUSHRW) and get 18003 * processed after tcp_close() is done. The conn would still be valid 18004 * because a ref would have added but we need to check the state 18005 * before actually processing the packet. 18006 */ 18007 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 18008 freemsg(mp); 18009 return; 18010 } 18011 18012 switch (DB_TYPE(mp)) { 18013 case M_IOCDATA: 18014 tcp_wput_iocdata(tcp, mp); 18015 break; 18016 case M_FLUSH: 18017 tcp_wput_flush(tcp, mp); 18018 break; 18019 default: 18020 CALL_IP_WPUT(connp, q, mp); 18021 break; 18022 } 18023 } 18024 18025 /* 18026 * The TCP fast path write put procedure. 18027 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 18028 */ 18029 /* ARGSUSED */ 18030 void 18031 tcp_output(void *arg, mblk_t *mp, void *arg2) 18032 { 18033 int len; 18034 int hdrlen; 18035 int plen; 18036 mblk_t *mp1; 18037 uchar_t *rptr; 18038 uint32_t snxt; 18039 tcph_t *tcph; 18040 struct datab *db; 18041 uint32_t suna; 18042 uint32_t mss; 18043 ipaddr_t *dst; 18044 ipaddr_t *src; 18045 uint32_t sum; 18046 int usable; 18047 conn_t *connp = (conn_t *)arg; 18048 tcp_t *tcp = connp->conn_tcp; 18049 uint32_t msize; 18050 tcp_stack_t *tcps = tcp->tcp_tcps; 18051 18052 /* 18053 * Try and ASSERT the minimum possible references on the 18054 * conn early enough. Since we are executing on write side, 18055 * the connection is obviously not detached and that means 18056 * there is a ref each for TCP and IP. Since we are behind 18057 * the squeue, the minimum references needed are 3. If the 18058 * conn is in classifier hash list, there should be an 18059 * extra ref for that (we check both the possibilities). 18060 */ 18061 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18062 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18063 18064 ASSERT(DB_TYPE(mp) == M_DATA); 18065 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 18066 18067 mutex_enter(&tcp->tcp_non_sq_lock); 18068 tcp->tcp_squeue_bytes -= msize; 18069 mutex_exit(&tcp->tcp_non_sq_lock); 18070 18071 /* Bypass tcp protocol for fused tcp loopback */ 18072 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 18073 return; 18074 18075 mss = tcp->tcp_mss; 18076 if (tcp->tcp_xmit_zc_clean) 18077 mp = tcp_zcopy_backoff(tcp, mp, 0); 18078 18079 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18080 len = (int)(mp->b_wptr - mp->b_rptr); 18081 18082 /* 18083 * Criteria for fast path: 18084 * 18085 * 1. no unsent data 18086 * 2. single mblk in request 18087 * 3. connection established 18088 * 4. data in mblk 18089 * 5. len <= mss 18090 * 6. no tcp_valid bits 18091 */ 18092 if ((tcp->tcp_unsent != 0) || 18093 (tcp->tcp_cork) || 18094 (mp->b_cont != NULL) || 18095 (tcp->tcp_state != TCPS_ESTABLISHED) || 18096 (len == 0) || 18097 (len > mss) || 18098 (tcp->tcp_valid_bits != 0)) { 18099 tcp_wput_data(tcp, mp, B_FALSE); 18100 return; 18101 } 18102 18103 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 18104 ASSERT(tcp->tcp_fin_sent == 0); 18105 18106 /* queue new packet onto retransmission queue */ 18107 if (tcp->tcp_xmit_head == NULL) { 18108 tcp->tcp_xmit_head = mp; 18109 } else { 18110 tcp->tcp_xmit_last->b_cont = mp; 18111 } 18112 tcp->tcp_xmit_last = mp; 18113 tcp->tcp_xmit_tail = mp; 18114 18115 /* find out how much we can send */ 18116 /* BEGIN CSTYLED */ 18117 /* 18118 * un-acked usable 18119 * |--------------|-----------------| 18120 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 18121 */ 18122 /* END CSTYLED */ 18123 18124 /* start sending from tcp_snxt */ 18125 snxt = tcp->tcp_snxt; 18126 18127 /* 18128 * Check to see if this connection has been idled for some 18129 * time and no ACK is expected. If it is, we need to slow 18130 * start again to get back the connection's "self-clock" as 18131 * described in VJ's paper. 18132 * 18133 * Refer to the comment in tcp_mss_set() for the calculation 18134 * of tcp_cwnd after idle. 18135 */ 18136 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 18137 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 18138 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 18139 } 18140 18141 usable = tcp->tcp_swnd; /* tcp window size */ 18142 if (usable > tcp->tcp_cwnd) 18143 usable = tcp->tcp_cwnd; /* congestion window smaller */ 18144 usable -= snxt; /* subtract stuff already sent */ 18145 suna = tcp->tcp_suna; 18146 usable += suna; 18147 /* usable can be < 0 if the congestion window is smaller */ 18148 if (len > usable) { 18149 /* Can't send complete M_DATA in one shot */ 18150 goto slow; 18151 } 18152 18153 mutex_enter(&tcp->tcp_non_sq_lock); 18154 if (tcp->tcp_flow_stopped && 18155 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18156 tcp_clrqfull(tcp); 18157 } 18158 mutex_exit(&tcp->tcp_non_sq_lock); 18159 18160 /* 18161 * determine if anything to send (Nagle). 18162 * 18163 * 1. len < tcp_mss (i.e. small) 18164 * 2. unacknowledged data present 18165 * 3. len < nagle limit 18166 * 4. last packet sent < nagle limit (previous packet sent) 18167 */ 18168 if ((len < mss) && (snxt != suna) && 18169 (len < (int)tcp->tcp_naglim) && 18170 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 18171 /* 18172 * This was the first unsent packet and normally 18173 * mss < xmit_hiwater so there is no need to worry 18174 * about flow control. The next packet will go 18175 * through the flow control check in tcp_wput_data(). 18176 */ 18177 /* leftover work from above */ 18178 tcp->tcp_unsent = len; 18179 tcp->tcp_xmit_tail_unsent = len; 18180 18181 return; 18182 } 18183 18184 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 18185 18186 if (snxt == suna) { 18187 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18188 } 18189 18190 /* we have always sent something */ 18191 tcp->tcp_rack_cnt = 0; 18192 18193 tcp->tcp_snxt = snxt + len; 18194 tcp->tcp_rack = tcp->tcp_rnxt; 18195 18196 if ((mp1 = dupb(mp)) == 0) 18197 goto no_memory; 18198 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 18199 mp->b_next = (mblk_t *)(uintptr_t)snxt; 18200 18201 /* adjust tcp header information */ 18202 tcph = tcp->tcp_tcph; 18203 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 18204 18205 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 18206 sum = (sum >> 16) + (sum & 0xFFFF); 18207 U16_TO_ABE16(sum, tcph->th_sum); 18208 18209 U32_TO_ABE32(snxt, tcph->th_seq); 18210 18211 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 18212 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 18213 BUMP_LOCAL(tcp->tcp_obsegs); 18214 18215 /* Update the latest receive window size in TCP header. */ 18216 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 18217 tcph->th_win); 18218 18219 tcp->tcp_last_sent_len = (ushort_t)len; 18220 18221 plen = len + tcp->tcp_hdr_len; 18222 18223 if (tcp->tcp_ipversion == IPV4_VERSION) { 18224 tcp->tcp_ipha->ipha_length = htons(plen); 18225 } else { 18226 tcp->tcp_ip6h->ip6_plen = htons(plen - 18227 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 18228 } 18229 18230 /* see if we need to allocate a mblk for the headers */ 18231 hdrlen = tcp->tcp_hdr_len; 18232 rptr = mp1->b_rptr - hdrlen; 18233 db = mp1->b_datap; 18234 if ((db->db_ref != 2) || rptr < db->db_base || 18235 (!OK_32PTR(rptr))) { 18236 /* NOTE: we assume allocb returns an OK_32PTR */ 18237 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 18238 tcps->tcps_wroff_xtra, BPRI_MED); 18239 if (!mp) { 18240 freemsg(mp1); 18241 goto no_memory; 18242 } 18243 mp->b_cont = mp1; 18244 mp1 = mp; 18245 /* Leave room for Link Level header */ 18246 /* hdrlen = tcp->tcp_hdr_len; */ 18247 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 18248 mp1->b_wptr = &rptr[hdrlen]; 18249 } 18250 mp1->b_rptr = rptr; 18251 18252 /* Fill in the timestamp option. */ 18253 if (tcp->tcp_snd_ts_ok) { 18254 U32_TO_BE32((uint32_t)lbolt, 18255 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 18256 U32_TO_BE32(tcp->tcp_ts_recent, 18257 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 18258 } else { 18259 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 18260 } 18261 18262 /* copy header into outgoing packet */ 18263 dst = (ipaddr_t *)rptr; 18264 src = (ipaddr_t *)tcp->tcp_iphc; 18265 dst[0] = src[0]; 18266 dst[1] = src[1]; 18267 dst[2] = src[2]; 18268 dst[3] = src[3]; 18269 dst[4] = src[4]; 18270 dst[5] = src[5]; 18271 dst[6] = src[6]; 18272 dst[7] = src[7]; 18273 dst[8] = src[8]; 18274 dst[9] = src[9]; 18275 if (hdrlen -= 40) { 18276 hdrlen >>= 2; 18277 dst += 10; 18278 src += 10; 18279 do { 18280 *dst++ = *src++; 18281 } while (--hdrlen); 18282 } 18283 18284 /* 18285 * Set the ECN info in the TCP header. Note that this 18286 * is not the template header. 18287 */ 18288 if (tcp->tcp_ecn_ok) { 18289 SET_ECT(tcp, rptr); 18290 18291 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 18292 if (tcp->tcp_ecn_echo_on) 18293 tcph->th_flags[0] |= TH_ECE; 18294 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 18295 tcph->th_flags[0] |= TH_CWR; 18296 tcp->tcp_ecn_cwr_sent = B_TRUE; 18297 } 18298 } 18299 18300 if (tcp->tcp_ip_forward_progress) { 18301 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 18302 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 18303 tcp->tcp_ip_forward_progress = B_FALSE; 18304 } 18305 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 18306 tcp_send_data(tcp, tcp->tcp_wq, mp1); 18307 return; 18308 18309 /* 18310 * If we ran out of memory, we pretend to have sent the packet 18311 * and that it was lost on the wire. 18312 */ 18313 no_memory: 18314 return; 18315 18316 slow: 18317 /* leftover work from above */ 18318 tcp->tcp_unsent = len; 18319 tcp->tcp_xmit_tail_unsent = len; 18320 tcp_wput_data(tcp, NULL, B_FALSE); 18321 } 18322 18323 /* 18324 * The function called through squeue to get behind eager's perimeter to 18325 * finish the accept processing. 18326 */ 18327 /* ARGSUSED */ 18328 void 18329 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 18330 { 18331 conn_t *connp = (conn_t *)arg; 18332 tcp_t *tcp = connp->conn_tcp; 18333 queue_t *q = tcp->tcp_rq; 18334 mblk_t *mp1; 18335 mblk_t *stropt_mp = mp; 18336 struct stroptions *stropt; 18337 uint_t thwin; 18338 tcp_stack_t *tcps = tcp->tcp_tcps; 18339 18340 /* 18341 * Drop the eager's ref on the listener, that was placed when 18342 * this eager began life in tcp_conn_request. 18343 */ 18344 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 18345 18346 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 18347 /* 18348 * Someone blewoff the eager before we could finish 18349 * the accept. 18350 * 18351 * The only reason eager exists it because we put in 18352 * a ref on it when conn ind went up. We need to send 18353 * a disconnect indication up while the last reference 18354 * on the eager will be dropped by the squeue when we 18355 * return. 18356 */ 18357 ASSERT(tcp->tcp_listener == NULL); 18358 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 18359 struct T_discon_ind *tdi; 18360 18361 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 18362 /* 18363 * Let us reuse the incoming mblk to avoid memory 18364 * allocation failure problems. We know that the 18365 * size of the incoming mblk i.e. stroptions is greater 18366 * than sizeof T_discon_ind. So the reallocb below 18367 * can't fail. 18368 */ 18369 freemsg(mp->b_cont); 18370 mp->b_cont = NULL; 18371 ASSERT(DB_REF(mp) == 1); 18372 mp = reallocb(mp, sizeof (struct T_discon_ind), 18373 B_FALSE); 18374 ASSERT(mp != NULL); 18375 DB_TYPE(mp) = M_PROTO; 18376 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 18377 tdi = (struct T_discon_ind *)mp->b_rptr; 18378 if (tcp->tcp_issocket) { 18379 tdi->DISCON_reason = ECONNREFUSED; 18380 tdi->SEQ_number = 0; 18381 } else { 18382 tdi->DISCON_reason = ENOPROTOOPT; 18383 tdi->SEQ_number = 18384 tcp->tcp_conn_req_seqnum; 18385 } 18386 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 18387 putnext(q, mp); 18388 } else { 18389 freemsg(mp); 18390 } 18391 if (tcp->tcp_hard_binding) { 18392 tcp->tcp_hard_binding = B_FALSE; 18393 tcp->tcp_hard_bound = B_TRUE; 18394 } 18395 tcp->tcp_detached = B_FALSE; 18396 return; 18397 } 18398 18399 mp1 = stropt_mp->b_cont; 18400 stropt_mp->b_cont = NULL; 18401 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 18402 stropt = (struct stroptions *)stropt_mp->b_rptr; 18403 18404 while (mp1 != NULL) { 18405 mp = mp1; 18406 mp1 = mp1->b_cont; 18407 mp->b_cont = NULL; 18408 tcp->tcp_drop_opt_ack_cnt++; 18409 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 18410 } 18411 mp = NULL; 18412 18413 /* 18414 * For a loopback connection with tcp_direct_sockfs on, note that 18415 * we don't have to protect tcp_rcv_list yet because synchronous 18416 * streams has not yet been enabled and tcp_fuse_rrw() cannot 18417 * possibly race with us. 18418 */ 18419 18420 /* 18421 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 18422 * properly. This is the first time we know of the acceptor' 18423 * queue. So we do it here. 18424 */ 18425 if (tcp->tcp_rcv_list == NULL) { 18426 /* 18427 * Recv queue is empty, tcp_rwnd should not have changed. 18428 * That means it should be equal to the listener's tcp_rwnd. 18429 */ 18430 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 18431 } else { 18432 #ifdef DEBUG 18433 uint_t cnt = 0; 18434 18435 mp1 = tcp->tcp_rcv_list; 18436 while ((mp = mp1) != NULL) { 18437 mp1 = mp->b_next; 18438 cnt += msgdsize(mp); 18439 } 18440 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 18441 #endif 18442 /* There is some data, add them back to get the max. */ 18443 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 18444 } 18445 18446 stropt->so_flags = SO_HIWAT; 18447 stropt->so_hiwat = MAX(q->q_hiwat, tcps->tcps_sth_rcv_hiwat); 18448 18449 stropt->so_flags |= SO_MAXBLK; 18450 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 18451 18452 /* 18453 * This is the first time we run on the correct 18454 * queue after tcp_accept. So fix all the q parameters 18455 * here. 18456 */ 18457 /* Allocate room for SACK options if needed. */ 18458 stropt->so_flags |= SO_WROFF; 18459 if (tcp->tcp_fused) { 18460 ASSERT(tcp->tcp_loopback); 18461 ASSERT(tcp->tcp_loopback_peer != NULL); 18462 /* 18463 * For fused tcp loopback, set the stream head's write 18464 * offset value to zero since we won't be needing any room 18465 * for TCP/IP headers. This would also improve performance 18466 * since it would reduce the amount of work done by kmem. 18467 * Non-fused tcp loopback case is handled separately below. 18468 */ 18469 stropt->so_wroff = 0; 18470 /* 18471 * Record the stream head's high water mark for this endpoint; 18472 * this is used for flow-control purposes in tcp_fuse_output(). 18473 */ 18474 stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat); 18475 /* 18476 * Update the peer's transmit parameters according to 18477 * our recently calculated high water mark value. 18478 */ 18479 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 18480 } else if (tcp->tcp_snd_sack_ok) { 18481 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 18482 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 18483 } else { 18484 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 18485 tcps->tcps_wroff_xtra); 18486 } 18487 18488 /* 18489 * If this is endpoint is handling SSL, then reserve extra 18490 * offset and space at the end. 18491 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 18492 * overriding the previous setting. The extra cost of signing and 18493 * encrypting multiple MSS-size records (12 of them with Ethernet), 18494 * instead of a single contiguous one by the stream head 18495 * largely outweighs the statistical reduction of ACKs, when 18496 * applicable. The peer will also save on decryption and verification 18497 * costs. 18498 */ 18499 if (tcp->tcp_kssl_ctx != NULL) { 18500 stropt->so_wroff += SSL3_WROFFSET; 18501 18502 stropt->so_flags |= SO_TAIL; 18503 stropt->so_tail = SSL3_MAX_TAIL_LEN; 18504 18505 stropt->so_flags |= SO_COPYOPT; 18506 stropt->so_copyopt = ZCVMUNSAFE; 18507 18508 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 18509 } 18510 18511 /* Send the options up */ 18512 putnext(q, stropt_mp); 18513 18514 /* 18515 * Pass up any data and/or a fin that has been received. 18516 * 18517 * Adjust receive window in case it had decreased 18518 * (because there is data <=> tcp_rcv_list != NULL) 18519 * while the connection was detached. Note that 18520 * in case the eager was flow-controlled, w/o this 18521 * code, the rwnd may never open up again! 18522 */ 18523 if (tcp->tcp_rcv_list != NULL) { 18524 /* We drain directly in case of fused tcp loopback */ 18525 sodirect_t *sodp; 18526 18527 if (!tcp->tcp_fused && canputnext(q)) { 18528 tcp->tcp_rwnd = q->q_hiwat; 18529 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 18530 << tcp->tcp_rcv_ws; 18531 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 18532 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18533 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 18534 tcp_xmit_ctl(NULL, 18535 tcp, (tcp->tcp_swnd == 0) ? 18536 tcp->tcp_suna : tcp->tcp_snxt, 18537 tcp->tcp_rnxt, TH_ACK); 18538 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 18539 } 18540 18541 } 18542 18543 SOD_PTR_ENTER(tcp, sodp); 18544 if (sodp != NULL) { 18545 /* Sodirect, move from rcv_list */ 18546 ASSERT(!tcp->tcp_fused); 18547 while ((mp = tcp->tcp_rcv_list) != NULL) { 18548 tcp->tcp_rcv_list = mp->b_next; 18549 mp->b_next = NULL; 18550 (void) tcp_rcv_sod_enqueue(tcp, sodp, mp, 18551 msgdsize(mp)); 18552 } 18553 tcp->tcp_rcv_last_head = NULL; 18554 tcp->tcp_rcv_last_tail = NULL; 18555 tcp->tcp_rcv_cnt = 0; 18556 (void) tcp_rcv_sod_wakeup(tcp, sodp); 18557 /* sod_wakeup() did the mutex_exit() */ 18558 } else { 18559 /* Not sodirect, drain */ 18560 (void) tcp_rcv_drain(q, tcp); 18561 } 18562 18563 /* 18564 * For fused tcp loopback, back-enable peer endpoint 18565 * if it's currently flow-controlled. 18566 */ 18567 if (tcp->tcp_fused) { 18568 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18569 18570 ASSERT(peer_tcp != NULL); 18571 ASSERT(peer_tcp->tcp_fused); 18572 /* 18573 * In order to change the peer's tcp_flow_stopped, 18574 * we need to take locks for both end points. The 18575 * highest address is taken first. 18576 */ 18577 if (peer_tcp > tcp) { 18578 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18579 mutex_enter(&tcp->tcp_non_sq_lock); 18580 } else { 18581 mutex_enter(&tcp->tcp_non_sq_lock); 18582 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18583 } 18584 if (peer_tcp->tcp_flow_stopped) { 18585 tcp_clrqfull(peer_tcp); 18586 TCP_STAT(tcps, tcp_fusion_backenabled); 18587 } 18588 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18589 mutex_exit(&tcp->tcp_non_sq_lock); 18590 } 18591 } 18592 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18593 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18594 mp = mi_tpi_ordrel_ind(); 18595 if (mp) { 18596 tcp->tcp_ordrel_done = B_TRUE; 18597 putnext(q, mp); 18598 if (tcp->tcp_deferred_clean_death) { 18599 /* 18600 * tcp_clean_death was deferred 18601 * for T_ORDREL_IND - do it now 18602 */ 18603 (void) tcp_clean_death(tcp, 18604 tcp->tcp_client_errno, 21); 18605 tcp->tcp_deferred_clean_death = B_FALSE; 18606 } 18607 } else { 18608 /* 18609 * Run the orderly release in the 18610 * service routine. 18611 */ 18612 qenable(q); 18613 } 18614 } 18615 if (tcp->tcp_hard_binding) { 18616 tcp->tcp_hard_binding = B_FALSE; 18617 tcp->tcp_hard_bound = B_TRUE; 18618 } 18619 18620 tcp->tcp_detached = B_FALSE; 18621 18622 /* We can enable synchronous streams now */ 18623 if (tcp->tcp_fused) { 18624 tcp_fuse_syncstr_enable_pair(tcp); 18625 } 18626 18627 if (tcp->tcp_ka_enabled) { 18628 tcp->tcp_ka_last_intrvl = 0; 18629 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18630 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18631 } 18632 18633 /* 18634 * At this point, eager is fully established and will 18635 * have the following references - 18636 * 18637 * 2 references for connection to exist (1 for TCP and 1 for IP). 18638 * 1 reference for the squeue which will be dropped by the squeue as 18639 * soon as this function returns. 18640 * There will be 1 additonal reference for being in classifier 18641 * hash list provided something bad hasn't happened. 18642 */ 18643 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18644 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18645 } 18646 18647 /* 18648 * The function called through squeue to get behind listener's perimeter to 18649 * send a deffered conn_ind. 18650 */ 18651 /* ARGSUSED */ 18652 void 18653 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18654 { 18655 conn_t *connp = (conn_t *)arg; 18656 tcp_t *listener = connp->conn_tcp; 18657 18658 if (listener->tcp_state == TCPS_CLOSED || 18659 TCP_IS_DETACHED(listener)) { 18660 /* 18661 * If listener has closed, it would have caused a 18662 * a cleanup/blowoff to happen for the eager. 18663 */ 18664 tcp_t *tcp; 18665 struct T_conn_ind *conn_ind; 18666 18667 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18668 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18669 conn_ind->OPT_length); 18670 /* 18671 * We need to drop the ref on eager that was put 18672 * tcp_rput_data() before trying to send the conn_ind 18673 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18674 * and tcp_wput_accept() is sending this deferred conn_ind but 18675 * listener is closed so we drop the ref. 18676 */ 18677 CONN_DEC_REF(tcp->tcp_connp); 18678 freemsg(mp); 18679 return; 18680 } 18681 putnext(listener->tcp_rq, mp); 18682 } 18683 18684 18685 /* 18686 * This is the STREAMS entry point for T_CONN_RES coming down on 18687 * Acceptor STREAM when sockfs listener does accept processing. 18688 * Read the block comment on top of tcp_conn_request(). 18689 */ 18690 void 18691 tcp_wput_accept(queue_t *q, mblk_t *mp) 18692 { 18693 queue_t *rq = RD(q); 18694 struct T_conn_res *conn_res; 18695 tcp_t *eager; 18696 tcp_t *listener; 18697 struct T_ok_ack *ok; 18698 t_scalar_t PRIM_type; 18699 mblk_t *opt_mp; 18700 conn_t *econnp; 18701 18702 ASSERT(DB_TYPE(mp) == M_PROTO); 18703 18704 conn_res = (struct T_conn_res *)mp->b_rptr; 18705 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18706 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18707 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18708 if (mp != NULL) 18709 putnext(rq, mp); 18710 return; 18711 } 18712 switch (conn_res->PRIM_type) { 18713 case O_T_CONN_RES: 18714 case T_CONN_RES: 18715 /* 18716 * We pass up an err ack if allocb fails. This will 18717 * cause sockfs to issue a T_DISCON_REQ which will cause 18718 * tcp_eager_blowoff to be called. sockfs will then call 18719 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18720 * we need to do the allocb up here because we have to 18721 * make sure rq->q_qinfo->qi_qclose still points to the 18722 * correct function (tcpclose_accept) in case allocb 18723 * fails. 18724 */ 18725 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18726 if (opt_mp == NULL) { 18727 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18728 if (mp != NULL) 18729 putnext(rq, mp); 18730 return; 18731 } 18732 18733 bcopy(mp->b_rptr + conn_res->OPT_offset, 18734 &eager, conn_res->OPT_length); 18735 PRIM_type = conn_res->PRIM_type; 18736 mp->b_datap->db_type = M_PCPROTO; 18737 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18738 ok = (struct T_ok_ack *)mp->b_rptr; 18739 ok->PRIM_type = T_OK_ACK; 18740 ok->CORRECT_prim = PRIM_type; 18741 econnp = eager->tcp_connp; 18742 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 18743 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 18744 eager->tcp_rq = rq; 18745 eager->tcp_wq = q; 18746 rq->q_ptr = econnp; 18747 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 18748 q->q_ptr = econnp; 18749 q->q_qinfo = &tcp_winit; 18750 listener = eager->tcp_listener; 18751 eager->tcp_issocket = B_TRUE; 18752 18753 /* 18754 * TCP is _D_SODIRECT and sockfs is directly above so 18755 * save shared sodirect_t pointer (if any). 18756 * 18757 * If tcp_fused and sodirect enabled disable it. 18758 */ 18759 eager->tcp_sodirect = SOD_QTOSODP(eager->tcp_rq); 18760 if (eager->tcp_fused && eager->tcp_sodirect != NULL) { 18761 /* Fused, disable sodirect */ 18762 mutex_enter(eager->tcp_sodirect->sod_lock); 18763 SOD_DISABLE(eager->tcp_sodirect); 18764 mutex_exit(eager->tcp_sodirect->sod_lock); 18765 eager->tcp_sodirect = NULL; 18766 } 18767 18768 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18769 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18770 ASSERT(econnp->conn_netstack == 18771 listener->tcp_connp->conn_netstack); 18772 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18773 18774 /* Put the ref for IP */ 18775 CONN_INC_REF(econnp); 18776 18777 /* 18778 * We should have minimum of 3 references on the conn 18779 * at this point. One each for TCP and IP and one for 18780 * the T_conn_ind that was sent up when the 3-way handshake 18781 * completed. In the normal case we would also have another 18782 * reference (making a total of 4) for the conn being in the 18783 * classifier hash list. However the eager could have received 18784 * an RST subsequently and tcp_closei_local could have removed 18785 * the eager from the classifier hash list, hence we can't 18786 * assert that reference. 18787 */ 18788 ASSERT(econnp->conn_ref >= 3); 18789 18790 /* 18791 * Send the new local address also up to sockfs. There 18792 * should already be enough space in the mp that came 18793 * down from soaccept(). 18794 */ 18795 if (eager->tcp_family == AF_INET) { 18796 sin_t *sin; 18797 18798 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18799 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18800 sin = (sin_t *)mp->b_wptr; 18801 mp->b_wptr += sizeof (sin_t); 18802 sin->sin_family = AF_INET; 18803 sin->sin_port = eager->tcp_lport; 18804 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18805 } else { 18806 sin6_t *sin6; 18807 18808 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18809 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18810 sin6 = (sin6_t *)mp->b_wptr; 18811 mp->b_wptr += sizeof (sin6_t); 18812 sin6->sin6_family = AF_INET6; 18813 sin6->sin6_port = eager->tcp_lport; 18814 if (eager->tcp_ipversion == IPV4_VERSION) { 18815 sin6->sin6_flowinfo = 0; 18816 IN6_IPADDR_TO_V4MAPPED( 18817 eager->tcp_ipha->ipha_src, 18818 &sin6->sin6_addr); 18819 } else { 18820 ASSERT(eager->tcp_ip6h != NULL); 18821 sin6->sin6_flowinfo = 18822 eager->tcp_ip6h->ip6_vcf & 18823 ~IPV6_VERS_AND_FLOW_MASK; 18824 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18825 } 18826 sin6->sin6_scope_id = 0; 18827 sin6->__sin6_src_id = 0; 18828 } 18829 18830 putnext(rq, mp); 18831 18832 opt_mp->b_datap->db_type = M_SETOPTS; 18833 opt_mp->b_wptr += sizeof (struct stroptions); 18834 18835 /* 18836 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18837 * from listener to acceptor. The message is chained on the 18838 * bind_mp which tcp_rput_other will send down to IP. 18839 */ 18840 if (listener->tcp_bound_if != 0) { 18841 /* allocate optmgmt req */ 18842 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18843 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 18844 sizeof (int)); 18845 if (mp != NULL) 18846 linkb(opt_mp, mp); 18847 } 18848 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18849 uint_t on = 1; 18850 18851 /* allocate optmgmt req */ 18852 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18853 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 18854 if (mp != NULL) 18855 linkb(opt_mp, mp); 18856 } 18857 18858 18859 mutex_enter(&listener->tcp_eager_lock); 18860 18861 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18862 18863 tcp_t *tail; 18864 tcp_t *tcp; 18865 mblk_t *mp1; 18866 18867 tcp = listener->tcp_eager_prev_q0; 18868 /* 18869 * listener->tcp_eager_prev_q0 points to the TAIL of the 18870 * deferred T_conn_ind queue. We need to get to the head 18871 * of the queue in order to send up T_conn_ind the same 18872 * order as how the 3WHS is completed. 18873 */ 18874 while (tcp != listener) { 18875 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18876 !tcp->tcp_kssl_pending) 18877 break; 18878 else 18879 tcp = tcp->tcp_eager_prev_q0; 18880 } 18881 /* None of the pending eagers can be sent up now */ 18882 if (tcp == listener) 18883 goto no_more_eagers; 18884 18885 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18886 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18887 /* Move from q0 to q */ 18888 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18889 listener->tcp_conn_req_cnt_q0--; 18890 listener->tcp_conn_req_cnt_q++; 18891 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18892 tcp->tcp_eager_prev_q0; 18893 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18894 tcp->tcp_eager_next_q0; 18895 tcp->tcp_eager_prev_q0 = NULL; 18896 tcp->tcp_eager_next_q0 = NULL; 18897 tcp->tcp_conn_def_q0 = B_FALSE; 18898 18899 /* Make sure the tcp isn't in the list of droppables */ 18900 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18901 tcp->tcp_eager_prev_drop_q0 == NULL); 18902 18903 /* 18904 * Insert at end of the queue because sockfs sends 18905 * down T_CONN_RES in chronological order. Leaving 18906 * the older conn indications at front of the queue 18907 * helps reducing search time. 18908 */ 18909 tail = listener->tcp_eager_last_q; 18910 if (tail != NULL) { 18911 tail->tcp_eager_next_q = tcp; 18912 } else { 18913 listener->tcp_eager_next_q = tcp; 18914 } 18915 listener->tcp_eager_last_q = tcp; 18916 tcp->tcp_eager_next_q = NULL; 18917 18918 /* Need to get inside the listener perimeter */ 18919 CONN_INC_REF(listener->tcp_connp); 18920 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 18921 tcp_send_pending, listener->tcp_connp, 18922 SQTAG_TCP_SEND_PENDING); 18923 } 18924 no_more_eagers: 18925 tcp_eager_unlink(eager); 18926 mutex_exit(&listener->tcp_eager_lock); 18927 18928 /* 18929 * At this point, the eager is detached from the listener 18930 * but we still have an extra refs on eager (apart from the 18931 * usual tcp references). The ref was placed in tcp_rput_data 18932 * before sending the conn_ind in tcp_send_conn_ind. 18933 * The ref will be dropped in tcp_accept_finish(). 18934 */ 18935 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 18936 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 18937 return; 18938 default: 18939 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18940 if (mp != NULL) 18941 putnext(rq, mp); 18942 return; 18943 } 18944 } 18945 18946 void 18947 tcp_wput(queue_t *q, mblk_t *mp) 18948 { 18949 conn_t *connp = Q_TO_CONN(q); 18950 tcp_t *tcp; 18951 void (*output_proc)(); 18952 t_scalar_t type; 18953 uchar_t *rptr; 18954 struct iocblk *iocp; 18955 uint32_t msize; 18956 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18957 18958 ASSERT(connp->conn_ref >= 2); 18959 18960 switch (DB_TYPE(mp)) { 18961 case M_DATA: 18962 tcp = connp->conn_tcp; 18963 ASSERT(tcp != NULL); 18964 18965 msize = msgdsize(mp); 18966 18967 mutex_enter(&tcp->tcp_non_sq_lock); 18968 tcp->tcp_squeue_bytes += msize; 18969 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18970 tcp_setqfull(tcp); 18971 } 18972 mutex_exit(&tcp->tcp_non_sq_lock); 18973 18974 CONN_INC_REF(connp); 18975 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18976 tcp_output, connp, SQTAG_TCP_OUTPUT); 18977 return; 18978 case M_PROTO: 18979 case M_PCPROTO: 18980 /* 18981 * if it is a snmp message, don't get behind the squeue 18982 */ 18983 tcp = connp->conn_tcp; 18984 rptr = mp->b_rptr; 18985 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18986 type = ((union T_primitives *)rptr)->type; 18987 } else { 18988 if (tcp->tcp_debug) { 18989 (void) strlog(TCP_MOD_ID, 0, 1, 18990 SL_ERROR|SL_TRACE, 18991 "tcp_wput_proto, dropping one..."); 18992 } 18993 freemsg(mp); 18994 return; 18995 } 18996 if (type == T_SVR4_OPTMGMT_REQ) { 18997 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18998 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 18999 cr)) { 19000 /* 19001 * This was a SNMP request 19002 */ 19003 return; 19004 } else { 19005 output_proc = tcp_wput_proto; 19006 } 19007 } else { 19008 output_proc = tcp_wput_proto; 19009 } 19010 break; 19011 case M_IOCTL: 19012 /* 19013 * Most ioctls can be processed right away without going via 19014 * squeues - process them right here. Those that do require 19015 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 19016 * are processed by tcp_wput_ioctl(). 19017 */ 19018 iocp = (struct iocblk *)mp->b_rptr; 19019 tcp = connp->conn_tcp; 19020 19021 switch (iocp->ioc_cmd) { 19022 case TCP_IOC_ABORT_CONN: 19023 tcp_ioctl_abort_conn(q, mp); 19024 return; 19025 case TI_GETPEERNAME: 19026 if (tcp->tcp_state < TCPS_SYN_RCVD) { 19027 iocp->ioc_error = ENOTCONN; 19028 iocp->ioc_count = 0; 19029 mp->b_datap->db_type = M_IOCACK; 19030 qreply(q, mp); 19031 return; 19032 } 19033 /* FALLTHRU */ 19034 case TI_GETMYNAME: 19035 mi_copyin(q, mp, NULL, 19036 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 19037 return; 19038 case ND_SET: 19039 /* nd_getset does the necessary checks */ 19040 case ND_GET: 19041 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 19042 CALL_IP_WPUT(connp, q, mp); 19043 return; 19044 } 19045 qreply(q, mp); 19046 return; 19047 case TCP_IOC_DEFAULT_Q: 19048 /* 19049 * Wants to be the default wq. Check the credentials 19050 * first, the rest is executed via squeue. 19051 */ 19052 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 19053 iocp->ioc_error = EPERM; 19054 iocp->ioc_count = 0; 19055 mp->b_datap->db_type = M_IOCACK; 19056 qreply(q, mp); 19057 return; 19058 } 19059 output_proc = tcp_wput_ioctl; 19060 break; 19061 default: 19062 output_proc = tcp_wput_ioctl; 19063 break; 19064 } 19065 break; 19066 default: 19067 output_proc = tcp_wput_nondata; 19068 break; 19069 } 19070 19071 CONN_INC_REF(connp); 19072 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 19073 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 19074 } 19075 19076 /* 19077 * Initial STREAMS write side put() procedure for sockets. It tries to 19078 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 19079 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 19080 * are handled by tcp_wput() as usual. 19081 * 19082 * All further messages will also be handled by tcp_wput() because we cannot 19083 * be sure that the above short cut is safe later. 19084 */ 19085 static void 19086 tcp_wput_sock(queue_t *wq, mblk_t *mp) 19087 { 19088 conn_t *connp = Q_TO_CONN(wq); 19089 tcp_t *tcp = connp->conn_tcp; 19090 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 19091 19092 ASSERT(wq->q_qinfo == &tcp_sock_winit); 19093 wq->q_qinfo = &tcp_winit; 19094 19095 ASSERT(IPCL_IS_TCP(connp)); 19096 ASSERT(TCP_IS_SOCKET(tcp)); 19097 19098 if (DB_TYPE(mp) == M_PCPROTO && 19099 MBLKL(mp) == sizeof (struct T_capability_req) && 19100 car->PRIM_type == T_CAPABILITY_REQ) { 19101 tcp_capability_req(tcp, mp); 19102 return; 19103 } 19104 19105 tcp_wput(wq, mp); 19106 } 19107 19108 static boolean_t 19109 tcp_zcopy_check(tcp_t *tcp) 19110 { 19111 conn_t *connp = tcp->tcp_connp; 19112 ire_t *ire; 19113 boolean_t zc_enabled = B_FALSE; 19114 tcp_stack_t *tcps = tcp->tcp_tcps; 19115 19116 if (do_tcpzcopy == 2) 19117 zc_enabled = B_TRUE; 19118 else if (tcp->tcp_ipversion == IPV4_VERSION && 19119 IPCL_IS_CONNECTED(connp) && 19120 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 19121 connp->conn_dontroute == 0 && 19122 !connp->conn_nexthop_set && 19123 connp->conn_outgoing_ill == NULL && 19124 connp->conn_nofailover_ill == NULL && 19125 do_tcpzcopy == 1) { 19126 /* 19127 * the checks above closely resemble the fast path checks 19128 * in tcp_send_data(). 19129 */ 19130 mutex_enter(&connp->conn_lock); 19131 ire = connp->conn_ire_cache; 19132 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19133 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19134 IRE_REFHOLD(ire); 19135 if (ire->ire_stq != NULL) { 19136 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 19137 19138 zc_enabled = ill && (ill->ill_capabilities & 19139 ILL_CAPAB_ZEROCOPY) && 19140 (ill->ill_zerocopy_capab-> 19141 ill_zerocopy_flags != 0); 19142 } 19143 IRE_REFRELE(ire); 19144 } 19145 mutex_exit(&connp->conn_lock); 19146 } 19147 tcp->tcp_snd_zcopy_on = zc_enabled; 19148 if (!TCP_IS_DETACHED(tcp)) { 19149 if (zc_enabled) { 19150 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 19151 TCP_STAT(tcps, tcp_zcopy_on); 19152 } else { 19153 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 19154 TCP_STAT(tcps, tcp_zcopy_off); 19155 } 19156 } 19157 return (zc_enabled); 19158 } 19159 19160 static mblk_t * 19161 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 19162 { 19163 tcp_stack_t *tcps = tcp->tcp_tcps; 19164 19165 if (do_tcpzcopy == 2) 19166 return (bp); 19167 else if (tcp->tcp_snd_zcopy_on) { 19168 tcp->tcp_snd_zcopy_on = B_FALSE; 19169 if (!TCP_IS_DETACHED(tcp)) { 19170 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 19171 TCP_STAT(tcps, tcp_zcopy_disable); 19172 } 19173 } 19174 return (tcp_zcopy_backoff(tcp, bp, 0)); 19175 } 19176 19177 /* 19178 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 19179 * the original desballoca'ed segmapped mblk. 19180 */ 19181 static mblk_t * 19182 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 19183 { 19184 mblk_t *head, *tail, *nbp; 19185 tcp_stack_t *tcps = tcp->tcp_tcps; 19186 19187 if (IS_VMLOANED_MBLK(bp)) { 19188 TCP_STAT(tcps, tcp_zcopy_backoff); 19189 if ((head = copyb(bp)) == NULL) { 19190 /* fail to backoff; leave it for the next backoff */ 19191 tcp->tcp_xmit_zc_clean = B_FALSE; 19192 return (bp); 19193 } 19194 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19195 if (fix_xmitlist) 19196 tcp_zcopy_notify(tcp); 19197 else 19198 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19199 } 19200 nbp = bp->b_cont; 19201 if (fix_xmitlist) { 19202 head->b_prev = bp->b_prev; 19203 head->b_next = bp->b_next; 19204 if (tcp->tcp_xmit_tail == bp) 19205 tcp->tcp_xmit_tail = head; 19206 } 19207 bp->b_next = NULL; 19208 bp->b_prev = NULL; 19209 freeb(bp); 19210 } else { 19211 head = bp; 19212 nbp = bp->b_cont; 19213 } 19214 tail = head; 19215 while (nbp) { 19216 if (IS_VMLOANED_MBLK(nbp)) { 19217 TCP_STAT(tcps, tcp_zcopy_backoff); 19218 if ((tail->b_cont = copyb(nbp)) == NULL) { 19219 tcp->tcp_xmit_zc_clean = B_FALSE; 19220 tail->b_cont = nbp; 19221 return (head); 19222 } 19223 tail = tail->b_cont; 19224 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19225 if (fix_xmitlist) 19226 tcp_zcopy_notify(tcp); 19227 else 19228 tail->b_datap->db_struioflag |= 19229 STRUIO_ZCNOTIFY; 19230 } 19231 bp = nbp; 19232 nbp = nbp->b_cont; 19233 if (fix_xmitlist) { 19234 tail->b_prev = bp->b_prev; 19235 tail->b_next = bp->b_next; 19236 if (tcp->tcp_xmit_tail == bp) 19237 tcp->tcp_xmit_tail = tail; 19238 } 19239 bp->b_next = NULL; 19240 bp->b_prev = NULL; 19241 freeb(bp); 19242 } else { 19243 tail->b_cont = nbp; 19244 tail = nbp; 19245 nbp = nbp->b_cont; 19246 } 19247 } 19248 if (fix_xmitlist) { 19249 tcp->tcp_xmit_last = tail; 19250 tcp->tcp_xmit_zc_clean = B_TRUE; 19251 } 19252 return (head); 19253 } 19254 19255 static void 19256 tcp_zcopy_notify(tcp_t *tcp) 19257 { 19258 struct stdata *stp; 19259 19260 if (tcp->tcp_detached) 19261 return; 19262 stp = STREAM(tcp->tcp_rq); 19263 mutex_enter(&stp->sd_lock); 19264 stp->sd_flag |= STZCNOTIFY; 19265 cv_broadcast(&stp->sd_zcopy_wait); 19266 mutex_exit(&stp->sd_lock); 19267 } 19268 19269 static boolean_t 19270 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 19271 { 19272 ire_t *ire; 19273 conn_t *connp = tcp->tcp_connp; 19274 tcp_stack_t *tcps = tcp->tcp_tcps; 19275 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19276 19277 mutex_enter(&connp->conn_lock); 19278 ire = connp->conn_ire_cache; 19279 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19280 19281 if ((ire != NULL) && 19282 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 19283 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 19284 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19285 IRE_REFHOLD(ire); 19286 mutex_exit(&connp->conn_lock); 19287 } else { 19288 boolean_t cached = B_FALSE; 19289 ts_label_t *tsl; 19290 19291 /* force a recheck later on */ 19292 tcp->tcp_ire_ill_check_done = B_FALSE; 19293 19294 TCP_DBGSTAT(tcps, tcp_ire_null1); 19295 connp->conn_ire_cache = NULL; 19296 mutex_exit(&connp->conn_lock); 19297 19298 if (ire != NULL) 19299 IRE_REFRELE_NOTR(ire); 19300 19301 tsl = crgetlabel(CONN_CRED(connp)); 19302 ire = (dst ? 19303 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 19304 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 19305 connp->conn_zoneid, tsl, ipst)); 19306 19307 if (ire == NULL) { 19308 TCP_STAT(tcps, tcp_ire_null); 19309 return (B_FALSE); 19310 } 19311 19312 IRE_REFHOLD_NOTR(ire); 19313 /* 19314 * Since we are inside the squeue, there cannot be another 19315 * thread in TCP trying to set the conn_ire_cache now. The 19316 * check for IRE_MARK_CONDEMNED ensures that an interface 19317 * unplumb thread has not yet started cleaning up the conns. 19318 * Hence we don't need to grab the conn lock. 19319 */ 19320 if (CONN_CACHE_IRE(connp)) { 19321 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 19322 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19323 TCP_CHECK_IREINFO(tcp, ire); 19324 connp->conn_ire_cache = ire; 19325 cached = B_TRUE; 19326 } 19327 rw_exit(&ire->ire_bucket->irb_lock); 19328 } 19329 19330 /* 19331 * We can continue to use the ire but since it was 19332 * not cached, we should drop the extra reference. 19333 */ 19334 if (!cached) 19335 IRE_REFRELE_NOTR(ire); 19336 19337 /* 19338 * Rampart note: no need to select a new label here, since 19339 * labels are not allowed to change during the life of a TCP 19340 * connection. 19341 */ 19342 } 19343 19344 *irep = ire; 19345 19346 return (B_TRUE); 19347 } 19348 19349 /* 19350 * Called from tcp_send() or tcp_send_data() to find workable IRE. 19351 * 19352 * 0 = success; 19353 * 1 = failed to find ire and ill. 19354 */ 19355 static boolean_t 19356 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 19357 { 19358 ipha_t *ipha; 19359 ipaddr_t dst; 19360 ire_t *ire; 19361 ill_t *ill; 19362 conn_t *connp = tcp->tcp_connp; 19363 mblk_t *ire_fp_mp; 19364 tcp_stack_t *tcps = tcp->tcp_tcps; 19365 19366 if (mp != NULL) 19367 ipha = (ipha_t *)mp->b_rptr; 19368 else 19369 ipha = tcp->tcp_ipha; 19370 dst = ipha->ipha_dst; 19371 19372 if (!tcp_send_find_ire(tcp, &dst, &ire)) 19373 return (B_FALSE); 19374 19375 if ((ire->ire_flags & RTF_MULTIRT) || 19376 (ire->ire_stq == NULL) || 19377 (ire->ire_nce == NULL) || 19378 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 19379 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 19380 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 19381 TCP_STAT(tcps, tcp_ip_ire_send); 19382 IRE_REFRELE(ire); 19383 return (B_FALSE); 19384 } 19385 19386 ill = ire_to_ill(ire); 19387 if (connp->conn_outgoing_ill != NULL) { 19388 ill_t *conn_outgoing_ill = NULL; 19389 /* 19390 * Choose a good ill in the group to send the packets on. 19391 */ 19392 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 19393 ill = ire_to_ill(ire); 19394 } 19395 ASSERT(ill != NULL); 19396 19397 if (!tcp->tcp_ire_ill_check_done) { 19398 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19399 tcp->tcp_ire_ill_check_done = B_TRUE; 19400 } 19401 19402 *irep = ire; 19403 *illp = ill; 19404 19405 return (B_TRUE); 19406 } 19407 19408 static void 19409 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 19410 { 19411 ipha_t *ipha; 19412 ipaddr_t src; 19413 ipaddr_t dst; 19414 uint32_t cksum; 19415 ire_t *ire; 19416 uint16_t *up; 19417 ill_t *ill; 19418 conn_t *connp = tcp->tcp_connp; 19419 uint32_t hcksum_txflags = 0; 19420 mblk_t *ire_fp_mp; 19421 uint_t ire_fp_mp_len; 19422 tcp_stack_t *tcps = tcp->tcp_tcps; 19423 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19424 19425 ASSERT(DB_TYPE(mp) == M_DATA); 19426 19427 if (DB_CRED(mp) == NULL) 19428 mblk_setcred(mp, CONN_CRED(connp)); 19429 19430 ipha = (ipha_t *)mp->b_rptr; 19431 src = ipha->ipha_src; 19432 dst = ipha->ipha_dst; 19433 19434 /* 19435 * Drop off fast path for IPv6 and also if options are present or 19436 * we need to resolve a TS label. 19437 */ 19438 if (tcp->tcp_ipversion != IPV4_VERSION || 19439 !IPCL_IS_CONNECTED(connp) || 19440 !CONN_IS_LSO_MD_FASTPATH(connp) || 19441 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 19442 !connp->conn_ulp_labeled || 19443 ipha->ipha_ident == IP_HDR_INCLUDED || 19444 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 19445 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 19446 if (tcp->tcp_snd_zcopy_aware) 19447 mp = tcp_zcopy_disable(tcp, mp); 19448 TCP_STAT(tcps, tcp_ip_send); 19449 CALL_IP_WPUT(connp, q, mp); 19450 return; 19451 } 19452 19453 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 19454 if (tcp->tcp_snd_zcopy_aware) 19455 mp = tcp_zcopy_backoff(tcp, mp, 0); 19456 CALL_IP_WPUT(connp, q, mp); 19457 return; 19458 } 19459 ire_fp_mp = ire->ire_nce->nce_fp_mp; 19460 ire_fp_mp_len = MBLKL(ire_fp_mp); 19461 19462 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 19463 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 19464 #ifndef _BIG_ENDIAN 19465 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 19466 #endif 19467 19468 /* 19469 * Check to see if we need to re-enable LSO/MDT for this connection 19470 * because it was previously disabled due to changes in the ill; 19471 * note that by doing it here, this re-enabling only applies when 19472 * the packet is not dispatched through CALL_IP_WPUT(). 19473 * 19474 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 19475 * case, since that's how we ended up here. For IPv6, we do the 19476 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 19477 */ 19478 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 19479 /* 19480 * Restore LSO for this connection, so that next time around 19481 * it is eligible to go through tcp_lsosend() path again. 19482 */ 19483 TCP_STAT(tcps, tcp_lso_enabled); 19484 tcp->tcp_lso = B_TRUE; 19485 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 19486 "interface %s\n", (void *)connp, ill->ill_name)); 19487 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 19488 /* 19489 * Restore MDT for this connection, so that next time around 19490 * it is eligible to go through tcp_multisend() path again. 19491 */ 19492 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 19493 tcp->tcp_mdt = B_TRUE; 19494 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 19495 "interface %s\n", (void *)connp, ill->ill_name)); 19496 } 19497 19498 if (tcp->tcp_snd_zcopy_aware) { 19499 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 19500 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 19501 mp = tcp_zcopy_disable(tcp, mp); 19502 /* 19503 * we shouldn't need to reset ipha as the mp containing 19504 * ipha should never be a zero-copy mp. 19505 */ 19506 } 19507 19508 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 19509 ASSERT(ill->ill_hcksum_capab != NULL); 19510 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 19511 } 19512 19513 /* pseudo-header checksum (do it in parts for IP header checksum) */ 19514 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 19515 19516 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 19517 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 19518 19519 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 19520 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 19521 19522 /* Software checksum? */ 19523 if (DB_CKSUMFLAGS(mp) == 0) { 19524 TCP_STAT(tcps, tcp_out_sw_cksum); 19525 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 19526 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 19527 } 19528 19529 ipha->ipha_fragment_offset_and_flags |= 19530 (uint32_t)htons(ire->ire_frag_flag); 19531 19532 /* Calculate IP header checksum if hardware isn't capable */ 19533 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 19534 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 19535 ((uint16_t *)ipha)[4]); 19536 } 19537 19538 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 19539 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 19540 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 19541 19542 UPDATE_OB_PKT_COUNT(ire); 19543 ire->ire_last_used_time = lbolt; 19544 19545 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 19546 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 19547 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 19548 ntohs(ipha->ipha_length)); 19549 19550 if (ILL_DLS_CAPABLE(ill)) { 19551 /* 19552 * Send the packet directly to DLD, where it may be queued 19553 * depending on the availability of transmit resources at 19554 * the media layer. 19555 */ 19556 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 19557 } else { 19558 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 19559 DTRACE_PROBE4(ip4__physical__out__start, 19560 ill_t *, NULL, ill_t *, out_ill, 19561 ipha_t *, ipha, mblk_t *, mp); 19562 FW_HOOKS(ipst->ips_ip4_physical_out_event, 19563 ipst->ips_ipv4firewall_physical_out, 19564 NULL, out_ill, ipha, mp, mp, 0, ipst); 19565 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19566 if (mp != NULL) 19567 putnext(ire->ire_stq, mp); 19568 } 19569 IRE_REFRELE(ire); 19570 } 19571 19572 /* 19573 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19574 * if the receiver shrinks the window, i.e. moves the right window to the 19575 * left, the we should not send new data, but should retransmit normally the 19576 * old unacked data between suna and suna + swnd. We might has sent data 19577 * that is now outside the new window, pretend that we didn't send it. 19578 */ 19579 static void 19580 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19581 { 19582 uint32_t snxt = tcp->tcp_snxt; 19583 mblk_t *xmit_tail; 19584 int32_t offset; 19585 19586 ASSERT(shrunk_count > 0); 19587 19588 /* Pretend we didn't send the data outside the window */ 19589 snxt -= shrunk_count; 19590 19591 /* Get the mblk and the offset in it per the shrunk window */ 19592 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19593 19594 ASSERT(xmit_tail != NULL); 19595 19596 /* Reset all the values per the now shrunk window */ 19597 tcp->tcp_snxt = snxt; 19598 tcp->tcp_xmit_tail = xmit_tail; 19599 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19600 offset; 19601 tcp->tcp_unsent += shrunk_count; 19602 19603 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19604 /* 19605 * Make sure the timer is running so that we will probe a zero 19606 * window. 19607 */ 19608 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19609 } 19610 19611 19612 /* 19613 * The TCP normal data output path. 19614 * NOTE: the logic of the fast path is duplicated from this function. 19615 */ 19616 static void 19617 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19618 { 19619 int len; 19620 mblk_t *local_time; 19621 mblk_t *mp1; 19622 uint32_t snxt; 19623 int tail_unsent; 19624 int tcpstate; 19625 int usable = 0; 19626 mblk_t *xmit_tail; 19627 queue_t *q = tcp->tcp_wq; 19628 int32_t mss; 19629 int32_t num_sack_blk = 0; 19630 int32_t tcp_hdr_len; 19631 int32_t tcp_tcp_hdr_len; 19632 int mdt_thres; 19633 int rc; 19634 tcp_stack_t *tcps = tcp->tcp_tcps; 19635 ip_stack_t *ipst; 19636 19637 tcpstate = tcp->tcp_state; 19638 if (mp == NULL) { 19639 /* 19640 * tcp_wput_data() with NULL mp should only be called when 19641 * there is unsent data. 19642 */ 19643 ASSERT(tcp->tcp_unsent > 0); 19644 /* Really tacky... but we need this for detached closes. */ 19645 len = tcp->tcp_unsent; 19646 goto data_null; 19647 } 19648 19649 #if CCS_STATS 19650 wrw_stats.tot.count++; 19651 wrw_stats.tot.bytes += msgdsize(mp); 19652 #endif 19653 ASSERT(mp->b_datap->db_type == M_DATA); 19654 /* 19655 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19656 * or before a connection attempt has begun. 19657 */ 19658 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19659 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19660 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19661 #ifdef DEBUG 19662 cmn_err(CE_WARN, 19663 "tcp_wput_data: data after ordrel, %s", 19664 tcp_display(tcp, NULL, 19665 DISP_ADDR_AND_PORT)); 19666 #else 19667 if (tcp->tcp_debug) { 19668 (void) strlog(TCP_MOD_ID, 0, 1, 19669 SL_TRACE|SL_ERROR, 19670 "tcp_wput_data: data after ordrel, %s\n", 19671 tcp_display(tcp, NULL, 19672 DISP_ADDR_AND_PORT)); 19673 } 19674 #endif /* DEBUG */ 19675 } 19676 if (tcp->tcp_snd_zcopy_aware && 19677 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19678 tcp_zcopy_notify(tcp); 19679 freemsg(mp); 19680 mutex_enter(&tcp->tcp_non_sq_lock); 19681 if (tcp->tcp_flow_stopped && 19682 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19683 tcp_clrqfull(tcp); 19684 } 19685 mutex_exit(&tcp->tcp_non_sq_lock); 19686 return; 19687 } 19688 19689 /* Strip empties */ 19690 for (;;) { 19691 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19692 (uintptr_t)INT_MAX); 19693 len = (int)(mp->b_wptr - mp->b_rptr); 19694 if (len > 0) 19695 break; 19696 mp1 = mp; 19697 mp = mp->b_cont; 19698 freeb(mp1); 19699 if (!mp) { 19700 return; 19701 } 19702 } 19703 19704 /* If we are the first on the list ... */ 19705 if (tcp->tcp_xmit_head == NULL) { 19706 tcp->tcp_xmit_head = mp; 19707 tcp->tcp_xmit_tail = mp; 19708 tcp->tcp_xmit_tail_unsent = len; 19709 } else { 19710 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19711 struct datab *dp; 19712 19713 mp1 = tcp->tcp_xmit_last; 19714 if (len < tcp_tx_pull_len && 19715 (dp = mp1->b_datap)->db_ref == 1 && 19716 dp->db_lim - mp1->b_wptr >= len) { 19717 ASSERT(len > 0); 19718 ASSERT(!mp1->b_cont); 19719 if (len == 1) { 19720 *mp1->b_wptr++ = *mp->b_rptr; 19721 } else { 19722 bcopy(mp->b_rptr, mp1->b_wptr, len); 19723 mp1->b_wptr += len; 19724 } 19725 if (mp1 == tcp->tcp_xmit_tail) 19726 tcp->tcp_xmit_tail_unsent += len; 19727 mp1->b_cont = mp->b_cont; 19728 if (tcp->tcp_snd_zcopy_aware && 19729 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19730 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19731 freeb(mp); 19732 mp = mp1; 19733 } else { 19734 tcp->tcp_xmit_last->b_cont = mp; 19735 } 19736 len += tcp->tcp_unsent; 19737 } 19738 19739 /* Tack on however many more positive length mblks we have */ 19740 if ((mp1 = mp->b_cont) != NULL) { 19741 do { 19742 int tlen; 19743 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19744 (uintptr_t)INT_MAX); 19745 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19746 if (tlen <= 0) { 19747 mp->b_cont = mp1->b_cont; 19748 freeb(mp1); 19749 } else { 19750 len += tlen; 19751 mp = mp1; 19752 } 19753 } while ((mp1 = mp->b_cont) != NULL); 19754 } 19755 tcp->tcp_xmit_last = mp; 19756 tcp->tcp_unsent = len; 19757 19758 if (urgent) 19759 usable = 1; 19760 19761 data_null: 19762 snxt = tcp->tcp_snxt; 19763 xmit_tail = tcp->tcp_xmit_tail; 19764 tail_unsent = tcp->tcp_xmit_tail_unsent; 19765 19766 /* 19767 * Note that tcp_mss has been adjusted to take into account the 19768 * timestamp option if applicable. Because SACK options do not 19769 * appear in every TCP segments and they are of variable lengths, 19770 * they cannot be included in tcp_mss. Thus we need to calculate 19771 * the actual segment length when we need to send a segment which 19772 * includes SACK options. 19773 */ 19774 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19775 int32_t opt_len; 19776 19777 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19778 tcp->tcp_num_sack_blk); 19779 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19780 2 + TCPOPT_HEADER_LEN; 19781 mss = tcp->tcp_mss - opt_len; 19782 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19783 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19784 } else { 19785 mss = tcp->tcp_mss; 19786 tcp_hdr_len = tcp->tcp_hdr_len; 19787 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19788 } 19789 19790 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19791 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19792 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19793 } 19794 if (tcpstate == TCPS_SYN_RCVD) { 19795 /* 19796 * The three-way connection establishment handshake is not 19797 * complete yet. We want to queue the data for transmission 19798 * after entering ESTABLISHED state (RFC793). A jump to 19799 * "done" label effectively leaves data on the queue. 19800 */ 19801 goto done; 19802 } else { 19803 int usable_r; 19804 19805 /* 19806 * In the special case when cwnd is zero, which can only 19807 * happen if the connection is ECN capable, return now. 19808 * New segments is sent using tcp_timer(). The timer 19809 * is set in tcp_rput_data(). 19810 */ 19811 if (tcp->tcp_cwnd == 0) { 19812 /* 19813 * Note that tcp_cwnd is 0 before 3-way handshake is 19814 * finished. 19815 */ 19816 ASSERT(tcp->tcp_ecn_ok || 19817 tcp->tcp_state < TCPS_ESTABLISHED); 19818 return; 19819 } 19820 19821 /* NOTE: trouble if xmitting while SYN not acked? */ 19822 usable_r = snxt - tcp->tcp_suna; 19823 usable_r = tcp->tcp_swnd - usable_r; 19824 19825 /* 19826 * Check if the receiver has shrunk the window. If 19827 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19828 * cannot be set as there is unsent data, so FIN cannot 19829 * be sent out. Otherwise, we need to take into account 19830 * of FIN as it consumes an "invisible" sequence number. 19831 */ 19832 ASSERT(tcp->tcp_fin_sent == 0); 19833 if (usable_r < 0) { 19834 /* 19835 * The receiver has shrunk the window and we have sent 19836 * -usable_r date beyond the window, re-adjust. 19837 * 19838 * If TCP window scaling is enabled, there can be 19839 * round down error as the advertised receive window 19840 * is actually right shifted n bits. This means that 19841 * the lower n bits info is wiped out. It will look 19842 * like the window is shrunk. Do a check here to 19843 * see if the shrunk amount is actually within the 19844 * error in window calculation. If it is, just 19845 * return. Note that this check is inside the 19846 * shrunk window check. This makes sure that even 19847 * though tcp_process_shrunk_swnd() is not called, 19848 * we will stop further processing. 19849 */ 19850 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19851 tcp_process_shrunk_swnd(tcp, -usable_r); 19852 } 19853 return; 19854 } 19855 19856 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19857 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19858 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19859 19860 /* usable = MIN(usable, unsent) */ 19861 if (usable_r > len) 19862 usable_r = len; 19863 19864 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19865 if (usable_r > 0) { 19866 usable = usable_r; 19867 } else { 19868 /* Bypass all other unnecessary processing. */ 19869 goto done; 19870 } 19871 } 19872 19873 local_time = (mblk_t *)lbolt; 19874 19875 /* 19876 * "Our" Nagle Algorithm. This is not the same as in the old 19877 * BSD. This is more in line with the true intent of Nagle. 19878 * 19879 * The conditions are: 19880 * 1. The amount of unsent data (or amount of data which can be 19881 * sent, whichever is smaller) is less than Nagle limit. 19882 * 2. The last sent size is also less than Nagle limit. 19883 * 3. There is unack'ed data. 19884 * 4. Urgent pointer is not set. Send urgent data ignoring the 19885 * Nagle algorithm. This reduces the probability that urgent 19886 * bytes get "merged" together. 19887 * 5. The app has not closed the connection. This eliminates the 19888 * wait time of the receiving side waiting for the last piece of 19889 * (small) data. 19890 * 19891 * If all are satisified, exit without sending anything. Note 19892 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19893 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19894 * 4095). 19895 */ 19896 if (usable < (int)tcp->tcp_naglim && 19897 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19898 snxt != tcp->tcp_suna && 19899 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19900 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19901 goto done; 19902 } 19903 19904 if (tcp->tcp_cork) { 19905 /* 19906 * if the tcp->tcp_cork option is set, then we have to force 19907 * TCP not to send partial segment (smaller than MSS bytes). 19908 * We are calculating the usable now based on full mss and 19909 * will save the rest of remaining data for later. 19910 */ 19911 if (usable < mss) 19912 goto done; 19913 usable = (usable / mss) * mss; 19914 } 19915 19916 /* Update the latest receive window size in TCP header. */ 19917 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19918 tcp->tcp_tcph->th_win); 19919 19920 /* 19921 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19922 * 19923 * 1. Simple TCP/IP{v4,v6} (no options). 19924 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19925 * 3. If the TCP connection is in ESTABLISHED state. 19926 * 4. The TCP is not detached. 19927 * 19928 * If any of the above conditions have changed during the 19929 * connection, stop using LSO/MDT and restore the stream head 19930 * parameters accordingly. 19931 */ 19932 ipst = tcps->tcps_netstack->netstack_ip; 19933 19934 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19935 ((tcp->tcp_ipversion == IPV4_VERSION && 19936 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19937 (tcp->tcp_ipversion == IPV6_VERSION && 19938 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19939 tcp->tcp_state != TCPS_ESTABLISHED || 19940 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19941 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19942 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19943 if (tcp->tcp_lso) { 19944 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19945 tcp->tcp_lso = B_FALSE; 19946 } else { 19947 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19948 tcp->tcp_mdt = B_FALSE; 19949 } 19950 19951 /* Anything other than detached is considered pathological */ 19952 if (!TCP_IS_DETACHED(tcp)) { 19953 if (tcp->tcp_lso) 19954 TCP_STAT(tcps, tcp_lso_disabled); 19955 else 19956 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19957 (void) tcp_maxpsz_set(tcp, B_TRUE); 19958 } 19959 } 19960 19961 /* Use MDT if sendable amount is greater than the threshold */ 19962 if (tcp->tcp_mdt && 19963 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19964 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19965 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19966 (tcp->tcp_valid_bits == 0 || 19967 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19968 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19969 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19970 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19971 local_time, mdt_thres); 19972 } else { 19973 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19974 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19975 local_time, INT_MAX); 19976 } 19977 19978 /* Pretend that all we were trying to send really got sent */ 19979 if (rc < 0 && tail_unsent < 0) { 19980 do { 19981 xmit_tail = xmit_tail->b_cont; 19982 xmit_tail->b_prev = local_time; 19983 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19984 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19985 tail_unsent += (int)(xmit_tail->b_wptr - 19986 xmit_tail->b_rptr); 19987 } while (tail_unsent < 0); 19988 } 19989 done:; 19990 tcp->tcp_xmit_tail = xmit_tail; 19991 tcp->tcp_xmit_tail_unsent = tail_unsent; 19992 len = tcp->tcp_snxt - snxt; 19993 if (len) { 19994 /* 19995 * If new data was sent, need to update the notsack 19996 * list, which is, afterall, data blocks that have 19997 * not been sack'ed by the receiver. New data is 19998 * not sack'ed. 19999 */ 20000 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 20001 /* len is a negative value. */ 20002 tcp->tcp_pipe -= len; 20003 tcp_notsack_update(&(tcp->tcp_notsack_list), 20004 tcp->tcp_snxt, snxt, 20005 &(tcp->tcp_num_notsack_blk), 20006 &(tcp->tcp_cnt_notsack_list)); 20007 } 20008 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 20009 tcp->tcp_rack = tcp->tcp_rnxt; 20010 tcp->tcp_rack_cnt = 0; 20011 if ((snxt + len) == tcp->tcp_suna) { 20012 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20013 } 20014 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 20015 /* 20016 * Didn't send anything. Make sure the timer is running 20017 * so that we will probe a zero window. 20018 */ 20019 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20020 } 20021 /* Note that len is the amount we just sent but with a negative sign */ 20022 tcp->tcp_unsent += len; 20023 mutex_enter(&tcp->tcp_non_sq_lock); 20024 if (tcp->tcp_flow_stopped) { 20025 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 20026 tcp_clrqfull(tcp); 20027 } 20028 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 20029 tcp_setqfull(tcp); 20030 } 20031 mutex_exit(&tcp->tcp_non_sq_lock); 20032 } 20033 20034 /* 20035 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 20036 * outgoing TCP header with the template header, as well as other 20037 * options such as time-stamp, ECN and/or SACK. 20038 */ 20039 static void 20040 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 20041 { 20042 tcph_t *tcp_tmpl, *tcp_h; 20043 uint32_t *dst, *src; 20044 int hdrlen; 20045 20046 ASSERT(OK_32PTR(rptr)); 20047 20048 /* Template header */ 20049 tcp_tmpl = tcp->tcp_tcph; 20050 20051 /* Header of outgoing packet */ 20052 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 20053 20054 /* dst and src are opaque 32-bit fields, used for copying */ 20055 dst = (uint32_t *)rptr; 20056 src = (uint32_t *)tcp->tcp_iphc; 20057 hdrlen = tcp->tcp_hdr_len; 20058 20059 /* Fill time-stamp option if needed */ 20060 if (tcp->tcp_snd_ts_ok) { 20061 U32_TO_BE32((uint32_t)now, 20062 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 20063 U32_TO_BE32(tcp->tcp_ts_recent, 20064 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 20065 } else { 20066 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 20067 } 20068 20069 /* 20070 * Copy the template header; is this really more efficient than 20071 * calling bcopy()? For simple IPv4/TCP, it may be the case, 20072 * but perhaps not for other scenarios. 20073 */ 20074 dst[0] = src[0]; 20075 dst[1] = src[1]; 20076 dst[2] = src[2]; 20077 dst[3] = src[3]; 20078 dst[4] = src[4]; 20079 dst[5] = src[5]; 20080 dst[6] = src[6]; 20081 dst[7] = src[7]; 20082 dst[8] = src[8]; 20083 dst[9] = src[9]; 20084 if (hdrlen -= 40) { 20085 hdrlen >>= 2; 20086 dst += 10; 20087 src += 10; 20088 do { 20089 *dst++ = *src++; 20090 } while (--hdrlen); 20091 } 20092 20093 /* 20094 * Set the ECN info in the TCP header if it is not a zero 20095 * window probe. Zero window probe is only sent in 20096 * tcp_wput_data() and tcp_timer(). 20097 */ 20098 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 20099 SET_ECT(tcp, rptr); 20100 20101 if (tcp->tcp_ecn_echo_on) 20102 tcp_h->th_flags[0] |= TH_ECE; 20103 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 20104 tcp_h->th_flags[0] |= TH_CWR; 20105 tcp->tcp_ecn_cwr_sent = B_TRUE; 20106 } 20107 } 20108 20109 /* Fill in SACK options */ 20110 if (num_sack_blk > 0) { 20111 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 20112 sack_blk_t *tmp; 20113 int32_t i; 20114 20115 wptr[0] = TCPOPT_NOP; 20116 wptr[1] = TCPOPT_NOP; 20117 wptr[2] = TCPOPT_SACK; 20118 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 20119 sizeof (sack_blk_t); 20120 wptr += TCPOPT_REAL_SACK_LEN; 20121 20122 tmp = tcp->tcp_sack_list; 20123 for (i = 0; i < num_sack_blk; i++) { 20124 U32_TO_BE32(tmp[i].begin, wptr); 20125 wptr += sizeof (tcp_seq); 20126 U32_TO_BE32(tmp[i].end, wptr); 20127 wptr += sizeof (tcp_seq); 20128 } 20129 tcp_h->th_offset_and_rsrvd[0] += 20130 ((num_sack_blk * 2 + 1) << 4); 20131 } 20132 } 20133 20134 /* 20135 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 20136 * the destination address and SAP attribute, and if necessary, the 20137 * hardware checksum offload attribute to a Multidata message. 20138 */ 20139 static int 20140 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 20141 const uint32_t start, const uint32_t stuff, const uint32_t end, 20142 const uint32_t flags, tcp_stack_t *tcps) 20143 { 20144 /* Add global destination address & SAP attribute */ 20145 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 20146 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 20147 "destination address+SAP\n")); 20148 20149 if (dlmp != NULL) 20150 TCP_STAT(tcps, tcp_mdt_allocfail); 20151 return (-1); 20152 } 20153 20154 /* Add global hwcksum attribute */ 20155 if (hwcksum && 20156 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 20157 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 20158 "checksum attribute\n")); 20159 20160 TCP_STAT(tcps, tcp_mdt_allocfail); 20161 return (-1); 20162 } 20163 20164 return (0); 20165 } 20166 20167 /* 20168 * Smaller and private version of pdescinfo_t used specifically for TCP, 20169 * which allows for only two payload spans per packet. 20170 */ 20171 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 20172 20173 /* 20174 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 20175 * scheme, and returns one the following: 20176 * 20177 * -1 = failed allocation. 20178 * 0 = success; burst count reached, or usable send window is too small, 20179 * and that we'd rather wait until later before sending again. 20180 */ 20181 static int 20182 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20183 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20184 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20185 const int mdt_thres) 20186 { 20187 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 20188 multidata_t *mmd; 20189 uint_t obsegs, obbytes, hdr_frag_sz; 20190 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 20191 int num_burst_seg, max_pld; 20192 pdesc_t *pkt; 20193 tcp_pdescinfo_t tcp_pkt_info; 20194 pdescinfo_t *pkt_info; 20195 int pbuf_idx, pbuf_idx_nxt; 20196 int seg_len, len, spill, af; 20197 boolean_t add_buffer, zcopy, clusterwide; 20198 boolean_t buf_trunked = B_FALSE; 20199 boolean_t rconfirm = B_FALSE; 20200 boolean_t done = B_FALSE; 20201 uint32_t cksum; 20202 uint32_t hwcksum_flags; 20203 ire_t *ire = NULL; 20204 ill_t *ill; 20205 ipha_t *ipha; 20206 ip6_t *ip6h; 20207 ipaddr_t src, dst; 20208 ill_zerocopy_capab_t *zc_cap = NULL; 20209 uint16_t *up; 20210 int err; 20211 conn_t *connp; 20212 mblk_t *mp, *mp1, *fw_mp_head = NULL; 20213 uchar_t *pld_start; 20214 tcp_stack_t *tcps = tcp->tcp_tcps; 20215 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20216 20217 #ifdef _BIG_ENDIAN 20218 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 20219 #else 20220 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 20221 #endif 20222 20223 #define PREP_NEW_MULTIDATA() { \ 20224 mmd = NULL; \ 20225 md_mp = md_hbuf = NULL; \ 20226 cur_hdr_off = 0; \ 20227 max_pld = tcp->tcp_mdt_max_pld; \ 20228 pbuf_idx = pbuf_idx_nxt = -1; \ 20229 add_buffer = B_TRUE; \ 20230 zcopy = B_FALSE; \ 20231 } 20232 20233 #define PREP_NEW_PBUF() { \ 20234 md_pbuf = md_pbuf_nxt = NULL; \ 20235 pbuf_idx = pbuf_idx_nxt = -1; \ 20236 cur_pld_off = 0; \ 20237 first_snxt = *snxt; \ 20238 ASSERT(*tail_unsent > 0); \ 20239 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 20240 } 20241 20242 ASSERT(mdt_thres >= mss); 20243 ASSERT(*usable > 0 && *usable > mdt_thres); 20244 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20245 ASSERT(!TCP_IS_DETACHED(tcp)); 20246 ASSERT(tcp->tcp_valid_bits == 0 || 20247 tcp->tcp_valid_bits == TCP_FSS_VALID); 20248 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 20249 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 20250 (tcp->tcp_ipversion == IPV6_VERSION && 20251 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 20252 20253 connp = tcp->tcp_connp; 20254 ASSERT(connp != NULL); 20255 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 20256 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 20257 20258 /* 20259 * Note that tcp will only declare at most 2 payload spans per 20260 * packet, which is much lower than the maximum allowable number 20261 * of packet spans per Multidata. For this reason, we use the 20262 * privately declared and smaller descriptor info structure, in 20263 * order to save some stack space. 20264 */ 20265 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 20266 20267 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 20268 if (af == AF_INET) { 20269 dst = tcp->tcp_ipha->ipha_dst; 20270 src = tcp->tcp_ipha->ipha_src; 20271 ASSERT(!CLASSD(dst)); 20272 } 20273 ASSERT(af == AF_INET || 20274 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 20275 20276 obsegs = obbytes = 0; 20277 num_burst_seg = tcp->tcp_snd_burst; 20278 md_mp_head = NULL; 20279 PREP_NEW_MULTIDATA(); 20280 20281 /* 20282 * Before we go on further, make sure there is an IRE that we can 20283 * use, and that the ILL supports MDT. Otherwise, there's no point 20284 * in proceeding any further, and we should just hand everything 20285 * off to the legacy path. 20286 */ 20287 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 20288 goto legacy_send_no_md; 20289 20290 ASSERT(ire != NULL); 20291 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 20292 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 20293 ASSERT(af == AF_INET || ire->ire_nce != NULL); 20294 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 20295 /* 20296 * If we do support loopback for MDT (which requires modifications 20297 * to the receiving paths), the following assertions should go away, 20298 * and we would be sending the Multidata to loopback conn later on. 20299 */ 20300 ASSERT(!IRE_IS_LOCAL(ire)); 20301 ASSERT(ire->ire_stq != NULL); 20302 20303 ill = ire_to_ill(ire); 20304 ASSERT(ill != NULL); 20305 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 20306 20307 if (!tcp->tcp_ire_ill_check_done) { 20308 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 20309 tcp->tcp_ire_ill_check_done = B_TRUE; 20310 } 20311 20312 /* 20313 * If the underlying interface conditions have changed, or if the 20314 * new interface does not support MDT, go back to legacy path. 20315 */ 20316 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 20317 /* don't go through this path anymore for this connection */ 20318 TCP_STAT(tcps, tcp_mdt_conn_halted2); 20319 tcp->tcp_mdt = B_FALSE; 20320 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 20321 "interface %s\n", (void *)connp, ill->ill_name)); 20322 /* IRE will be released prior to returning */ 20323 goto legacy_send_no_md; 20324 } 20325 20326 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 20327 zc_cap = ill->ill_zerocopy_capab; 20328 20329 /* 20330 * Check if we can take tcp fast-path. Note that "incomplete" 20331 * ire's (where the link-layer for next hop is not resolved 20332 * or where the fast-path header in nce_fp_mp is not available 20333 * yet) are sent down the legacy (slow) path. 20334 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 20335 */ 20336 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 20337 /* IRE will be released prior to returning */ 20338 goto legacy_send_no_md; 20339 } 20340 20341 /* go to legacy path if interface doesn't support zerocopy */ 20342 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 20343 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 20344 /* IRE will be released prior to returning */ 20345 goto legacy_send_no_md; 20346 } 20347 20348 /* does the interface support hardware checksum offload? */ 20349 hwcksum_flags = 0; 20350 if (ILL_HCKSUM_CAPABLE(ill) && 20351 (ill->ill_hcksum_capab->ill_hcksum_txflags & 20352 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 20353 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 20354 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20355 HCKSUM_IPHDRCKSUM) 20356 hwcksum_flags = HCK_IPV4_HDRCKSUM; 20357 20358 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20359 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 20360 hwcksum_flags |= HCK_FULLCKSUM; 20361 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20362 HCKSUM_INET_PARTIAL) 20363 hwcksum_flags |= HCK_PARTIALCKSUM; 20364 } 20365 20366 /* 20367 * Each header fragment consists of the leading extra space, 20368 * followed by the TCP/IP header, and the trailing extra space. 20369 * We make sure that each header fragment begins on a 32-bit 20370 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 20371 * aligned in tcp_mdt_update). 20372 */ 20373 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 20374 tcp->tcp_mdt_hdr_tail), 4); 20375 20376 /* are we starting from the beginning of data block? */ 20377 if (*tail_unsent == 0) { 20378 *xmit_tail = (*xmit_tail)->b_cont; 20379 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 20380 *tail_unsent = (int)MBLKL(*xmit_tail); 20381 } 20382 20383 /* 20384 * Here we create one or more Multidata messages, each made up of 20385 * one header buffer and up to N payload buffers. This entire 20386 * operation is done within two loops: 20387 * 20388 * The outer loop mostly deals with creating the Multidata message, 20389 * as well as the header buffer that gets added to it. It also 20390 * links the Multidata messages together such that all of them can 20391 * be sent down to the lower layer in a single putnext call; this 20392 * linking behavior depends on the tcp_mdt_chain tunable. 20393 * 20394 * The inner loop takes an existing Multidata message, and adds 20395 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 20396 * packetizes those buffers by filling up the corresponding header 20397 * buffer fragments with the proper IP and TCP headers, and by 20398 * describing the layout of each packet in the packet descriptors 20399 * that get added to the Multidata. 20400 */ 20401 do { 20402 /* 20403 * If usable send window is too small, or data blocks in 20404 * transmit list are smaller than our threshold (i.e. app 20405 * performs large writes followed by small ones), we hand 20406 * off the control over to the legacy path. Note that we'll 20407 * get back the control once it encounters a large block. 20408 */ 20409 if (*usable < mss || (*tail_unsent <= mdt_thres && 20410 (*xmit_tail)->b_cont != NULL && 20411 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 20412 /* send down what we've got so far */ 20413 if (md_mp_head != NULL) { 20414 tcp_multisend_data(tcp, ire, ill, md_mp_head, 20415 obsegs, obbytes, &rconfirm); 20416 } 20417 /* 20418 * Pass control over to tcp_send(), but tell it to 20419 * return to us once a large-size transmission is 20420 * possible. 20421 */ 20422 TCP_STAT(tcps, tcp_mdt_legacy_small); 20423 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 20424 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 20425 tail_unsent, xmit_tail, local_time, 20426 mdt_thres)) <= 0) { 20427 /* burst count reached, or alloc failed */ 20428 IRE_REFRELE(ire); 20429 return (err); 20430 } 20431 20432 /* tcp_send() may have sent everything, so check */ 20433 if (*usable <= 0) { 20434 IRE_REFRELE(ire); 20435 return (0); 20436 } 20437 20438 TCP_STAT(tcps, tcp_mdt_legacy_ret); 20439 /* 20440 * We may have delivered the Multidata, so make sure 20441 * to re-initialize before the next round. 20442 */ 20443 md_mp_head = NULL; 20444 obsegs = obbytes = 0; 20445 num_burst_seg = tcp->tcp_snd_burst; 20446 PREP_NEW_MULTIDATA(); 20447 20448 /* are we starting from the beginning of data block? */ 20449 if (*tail_unsent == 0) { 20450 *xmit_tail = (*xmit_tail)->b_cont; 20451 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20452 (uintptr_t)INT_MAX); 20453 *tail_unsent = (int)MBLKL(*xmit_tail); 20454 } 20455 } 20456 20457 /* 20458 * max_pld limits the number of mblks in tcp's transmit 20459 * queue that can be added to a Multidata message. Once 20460 * this counter reaches zero, no more additional mblks 20461 * can be added to it. What happens afterwards depends 20462 * on whether or not we are set to chain the Multidata 20463 * messages. If we are to link them together, reset 20464 * max_pld to its original value (tcp_mdt_max_pld) and 20465 * prepare to create a new Multidata message which will 20466 * get linked to md_mp_head. Else, leave it alone and 20467 * let the inner loop break on its own. 20468 */ 20469 if (tcp_mdt_chain && max_pld == 0) 20470 PREP_NEW_MULTIDATA(); 20471 20472 /* adding a payload buffer; re-initialize values */ 20473 if (add_buffer) 20474 PREP_NEW_PBUF(); 20475 20476 /* 20477 * If we don't have a Multidata, either because we just 20478 * (re)entered this outer loop, or after we branched off 20479 * to tcp_send above, setup the Multidata and header 20480 * buffer to be used. 20481 */ 20482 if (md_mp == NULL) { 20483 int md_hbuflen; 20484 uint32_t start, stuff; 20485 20486 /* 20487 * Calculate Multidata header buffer size large enough 20488 * to hold all of the headers that can possibly be 20489 * sent at this moment. We'd rather over-estimate 20490 * the size than running out of space; this is okay 20491 * since this buffer is small anyway. 20492 */ 20493 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 20494 20495 /* 20496 * Start and stuff offset for partial hardware 20497 * checksum offload; these are currently for IPv4. 20498 * For full checksum offload, they are set to zero. 20499 */ 20500 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 20501 if (af == AF_INET) { 20502 start = IP_SIMPLE_HDR_LENGTH; 20503 stuff = IP_SIMPLE_HDR_LENGTH + 20504 TCP_CHECKSUM_OFFSET; 20505 } else { 20506 start = IPV6_HDR_LEN; 20507 stuff = IPV6_HDR_LEN + 20508 TCP_CHECKSUM_OFFSET; 20509 } 20510 } else { 20511 start = stuff = 0; 20512 } 20513 20514 /* 20515 * Create the header buffer, Multidata, as well as 20516 * any necessary attributes (destination address, 20517 * SAP and hardware checksum offload) that should 20518 * be associated with the Multidata message. 20519 */ 20520 ASSERT(cur_hdr_off == 0); 20521 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 20522 ((md_hbuf->b_wptr += md_hbuflen), 20523 (mmd = mmd_alloc(md_hbuf, &md_mp, 20524 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 20525 /* fastpath mblk */ 20526 ire->ire_nce->nce_res_mp, 20527 /* hardware checksum enabled */ 20528 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 20529 /* hardware checksum offsets */ 20530 start, stuff, 0, 20531 /* hardware checksum flag */ 20532 hwcksum_flags, tcps) != 0)) { 20533 legacy_send: 20534 if (md_mp != NULL) { 20535 /* Unlink message from the chain */ 20536 if (md_mp_head != NULL) { 20537 err = (intptr_t)rmvb(md_mp_head, 20538 md_mp); 20539 /* 20540 * We can't assert that rmvb 20541 * did not return -1, since we 20542 * may get here before linkb 20543 * happens. We do, however, 20544 * check if we just removed the 20545 * only element in the list. 20546 */ 20547 if (err == 0) 20548 md_mp_head = NULL; 20549 } 20550 /* md_hbuf gets freed automatically */ 20551 TCP_STAT(tcps, tcp_mdt_discarded); 20552 freeb(md_mp); 20553 } else { 20554 /* Either allocb or mmd_alloc failed */ 20555 TCP_STAT(tcps, tcp_mdt_allocfail); 20556 if (md_hbuf != NULL) 20557 freeb(md_hbuf); 20558 } 20559 20560 /* send down what we've got so far */ 20561 if (md_mp_head != NULL) { 20562 tcp_multisend_data(tcp, ire, ill, 20563 md_mp_head, obsegs, obbytes, 20564 &rconfirm); 20565 } 20566 legacy_send_no_md: 20567 if (ire != NULL) 20568 IRE_REFRELE(ire); 20569 /* 20570 * Too bad; let the legacy path handle this. 20571 * We specify INT_MAX for the threshold, since 20572 * we gave up with the Multidata processings 20573 * and let the old path have it all. 20574 */ 20575 TCP_STAT(tcps, tcp_mdt_legacy_all); 20576 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20577 tcp_tcp_hdr_len, num_sack_blk, usable, 20578 snxt, tail_unsent, xmit_tail, local_time, 20579 INT_MAX)); 20580 } 20581 20582 /* link to any existing ones, if applicable */ 20583 TCP_STAT(tcps, tcp_mdt_allocd); 20584 if (md_mp_head == NULL) { 20585 md_mp_head = md_mp; 20586 } else if (tcp_mdt_chain) { 20587 TCP_STAT(tcps, tcp_mdt_linked); 20588 linkb(md_mp_head, md_mp); 20589 } 20590 } 20591 20592 ASSERT(md_mp_head != NULL); 20593 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20594 ASSERT(md_mp != NULL && mmd != NULL); 20595 ASSERT(md_hbuf != NULL); 20596 20597 /* 20598 * Packetize the transmittable portion of the data block; 20599 * each data block is essentially added to the Multidata 20600 * as a payload buffer. We also deal with adding more 20601 * than one payload buffers, which happens when the remaining 20602 * packetized portion of the current payload buffer is less 20603 * than MSS, while the next data block in transmit queue 20604 * has enough data to make up for one. This "spillover" 20605 * case essentially creates a split-packet, where portions 20606 * of the packet's payload fragments may span across two 20607 * virtually discontiguous address blocks. 20608 */ 20609 seg_len = mss; 20610 do { 20611 len = seg_len; 20612 20613 ASSERT(len > 0); 20614 ASSERT(max_pld >= 0); 20615 ASSERT(!add_buffer || cur_pld_off == 0); 20616 20617 /* 20618 * First time around for this payload buffer; note 20619 * in the case of a spillover, the following has 20620 * been done prior to adding the split-packet 20621 * descriptor to Multidata, and we don't want to 20622 * repeat the process. 20623 */ 20624 if (add_buffer) { 20625 ASSERT(mmd != NULL); 20626 ASSERT(md_pbuf == NULL); 20627 ASSERT(md_pbuf_nxt == NULL); 20628 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20629 20630 /* 20631 * Have we reached the limit? We'd get to 20632 * this case when we're not chaining the 20633 * Multidata messages together, and since 20634 * we're done, terminate this loop. 20635 */ 20636 if (max_pld == 0) 20637 break; /* done */ 20638 20639 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20640 TCP_STAT(tcps, tcp_mdt_allocfail); 20641 goto legacy_send; /* out_of_mem */ 20642 } 20643 20644 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20645 zc_cap != NULL) { 20646 if (!ip_md_zcopy_attr(mmd, NULL, 20647 zc_cap->ill_zerocopy_flags)) { 20648 freeb(md_pbuf); 20649 TCP_STAT(tcps, 20650 tcp_mdt_allocfail); 20651 /* out_of_mem */ 20652 goto legacy_send; 20653 } 20654 zcopy = B_TRUE; 20655 } 20656 20657 md_pbuf->b_rptr += base_pld_off; 20658 20659 /* 20660 * Add a payload buffer to the Multidata; this 20661 * operation must not fail, or otherwise our 20662 * logic in this routine is broken. There 20663 * is no memory allocation done by the 20664 * routine, so any returned failure simply 20665 * tells us that we've done something wrong. 20666 * 20667 * A failure tells us that either we're adding 20668 * the same payload buffer more than once, or 20669 * we're trying to add more buffers than 20670 * allowed (max_pld calculation is wrong). 20671 * None of the above cases should happen, and 20672 * we panic because either there's horrible 20673 * heap corruption, and/or programming mistake. 20674 */ 20675 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20676 if (pbuf_idx < 0) { 20677 cmn_err(CE_PANIC, "tcp_multisend: " 20678 "payload buffer logic error " 20679 "detected for tcp %p mmd %p " 20680 "pbuf %p (%d)\n", 20681 (void *)tcp, (void *)mmd, 20682 (void *)md_pbuf, pbuf_idx); 20683 } 20684 20685 ASSERT(max_pld > 0); 20686 --max_pld; 20687 add_buffer = B_FALSE; 20688 } 20689 20690 ASSERT(md_mp_head != NULL); 20691 ASSERT(md_pbuf != NULL); 20692 ASSERT(md_pbuf_nxt == NULL); 20693 ASSERT(pbuf_idx != -1); 20694 ASSERT(pbuf_idx_nxt == -1); 20695 ASSERT(*usable > 0); 20696 20697 /* 20698 * We spillover to the next payload buffer only 20699 * if all of the following is true: 20700 * 20701 * 1. There is not enough data on the current 20702 * payload buffer to make up `len', 20703 * 2. We are allowed to send `len', 20704 * 3. The next payload buffer length is large 20705 * enough to accomodate `spill'. 20706 */ 20707 if ((spill = len - *tail_unsent) > 0 && 20708 *usable >= len && 20709 MBLKL((*xmit_tail)->b_cont) >= spill && 20710 max_pld > 0) { 20711 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20712 if (md_pbuf_nxt == NULL) { 20713 TCP_STAT(tcps, tcp_mdt_allocfail); 20714 goto legacy_send; /* out_of_mem */ 20715 } 20716 20717 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20718 zc_cap != NULL) { 20719 if (!ip_md_zcopy_attr(mmd, NULL, 20720 zc_cap->ill_zerocopy_flags)) { 20721 freeb(md_pbuf_nxt); 20722 TCP_STAT(tcps, 20723 tcp_mdt_allocfail); 20724 /* out_of_mem */ 20725 goto legacy_send; 20726 } 20727 zcopy = B_TRUE; 20728 } 20729 20730 /* 20731 * See comments above on the first call to 20732 * mmd_addpldbuf for explanation on the panic. 20733 */ 20734 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20735 if (pbuf_idx_nxt < 0) { 20736 panic("tcp_multisend: " 20737 "next payload buffer logic error " 20738 "detected for tcp %p mmd %p " 20739 "pbuf %p (%d)\n", 20740 (void *)tcp, (void *)mmd, 20741 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20742 } 20743 20744 ASSERT(max_pld > 0); 20745 --max_pld; 20746 } else if (spill > 0) { 20747 /* 20748 * If there's a spillover, but the following 20749 * xmit_tail couldn't give us enough octets 20750 * to reach "len", then stop the current 20751 * Multidata creation and let the legacy 20752 * tcp_send() path take over. We don't want 20753 * to send the tiny segment as part of this 20754 * Multidata for performance reasons; instead, 20755 * we let the legacy path deal with grouping 20756 * it with the subsequent small mblks. 20757 */ 20758 if (*usable >= len && 20759 MBLKL((*xmit_tail)->b_cont) < spill) { 20760 max_pld = 0; 20761 break; /* done */ 20762 } 20763 20764 /* 20765 * We can't spillover, and we are near 20766 * the end of the current payload buffer, 20767 * so send what's left. 20768 */ 20769 ASSERT(*tail_unsent > 0); 20770 len = *tail_unsent; 20771 } 20772 20773 /* tail_unsent is negated if there is a spillover */ 20774 *tail_unsent -= len; 20775 *usable -= len; 20776 ASSERT(*usable >= 0); 20777 20778 if (*usable < mss) 20779 seg_len = *usable; 20780 /* 20781 * Sender SWS avoidance; see comments in tcp_send(); 20782 * everything else is the same, except that we only 20783 * do this here if there is no more data to be sent 20784 * following the current xmit_tail. We don't check 20785 * for 1-byte urgent data because we shouldn't get 20786 * here if TCP_URG_VALID is set. 20787 */ 20788 if (*usable > 0 && *usable < mss && 20789 ((md_pbuf_nxt == NULL && 20790 (*xmit_tail)->b_cont == NULL) || 20791 (md_pbuf_nxt != NULL && 20792 (*xmit_tail)->b_cont->b_cont == NULL)) && 20793 seg_len < (tcp->tcp_max_swnd >> 1) && 20794 (tcp->tcp_unsent - 20795 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20796 !tcp->tcp_zero_win_probe) { 20797 if ((*snxt + len) == tcp->tcp_snxt && 20798 (*snxt + len) == tcp->tcp_suna) { 20799 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20800 } 20801 done = B_TRUE; 20802 } 20803 20804 /* 20805 * Prime pump for IP's checksumming on our behalf; 20806 * include the adjustment for a source route if any. 20807 * Do this only for software/partial hardware checksum 20808 * offload, as this field gets zeroed out later for 20809 * the full hardware checksum offload case. 20810 */ 20811 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20812 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20813 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20814 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20815 } 20816 20817 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20818 *snxt += len; 20819 20820 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20821 /* 20822 * We set the PUSH bit only if TCP has no more buffered 20823 * data to be transmitted (or if sender SWS avoidance 20824 * takes place), as opposed to setting it for every 20825 * last packet in the burst. 20826 */ 20827 if (done || 20828 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20829 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20830 20831 /* 20832 * Set FIN bit if this is our last segment; snxt 20833 * already includes its length, and it will not 20834 * be adjusted after this point. 20835 */ 20836 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20837 *snxt == tcp->tcp_fss) { 20838 if (!tcp->tcp_fin_acked) { 20839 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20840 BUMP_MIB(&tcps->tcps_mib, 20841 tcpOutControl); 20842 } 20843 if (!tcp->tcp_fin_sent) { 20844 tcp->tcp_fin_sent = B_TRUE; 20845 /* 20846 * tcp state must be ESTABLISHED 20847 * in order for us to get here in 20848 * the first place. 20849 */ 20850 tcp->tcp_state = TCPS_FIN_WAIT_1; 20851 20852 /* 20853 * Upon returning from this routine, 20854 * tcp_wput_data() will set tcp_snxt 20855 * to be equal to snxt + tcp_fin_sent. 20856 * This is essentially the same as 20857 * setting it to tcp_fss + 1. 20858 */ 20859 } 20860 } 20861 20862 tcp->tcp_last_sent_len = (ushort_t)len; 20863 20864 len += tcp_hdr_len; 20865 if (tcp->tcp_ipversion == IPV4_VERSION) 20866 tcp->tcp_ipha->ipha_length = htons(len); 20867 else 20868 tcp->tcp_ip6h->ip6_plen = htons(len - 20869 ((char *)&tcp->tcp_ip6h[1] - 20870 tcp->tcp_iphc)); 20871 20872 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20873 20874 /* setup header fragment */ 20875 PDESC_HDR_ADD(pkt_info, 20876 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20877 tcp->tcp_mdt_hdr_head, /* head room */ 20878 tcp_hdr_len, /* len */ 20879 tcp->tcp_mdt_hdr_tail); /* tail room */ 20880 20881 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20882 hdr_frag_sz); 20883 ASSERT(MBLKIN(md_hbuf, 20884 (pkt_info->hdr_base - md_hbuf->b_rptr), 20885 PDESC_HDRSIZE(pkt_info))); 20886 20887 /* setup first payload fragment */ 20888 PDESC_PLD_INIT(pkt_info); 20889 PDESC_PLD_SPAN_ADD(pkt_info, 20890 pbuf_idx, /* index */ 20891 md_pbuf->b_rptr + cur_pld_off, /* start */ 20892 tcp->tcp_last_sent_len); /* len */ 20893 20894 /* create a split-packet in case of a spillover */ 20895 if (md_pbuf_nxt != NULL) { 20896 ASSERT(spill > 0); 20897 ASSERT(pbuf_idx_nxt > pbuf_idx); 20898 ASSERT(!add_buffer); 20899 20900 md_pbuf = md_pbuf_nxt; 20901 md_pbuf_nxt = NULL; 20902 pbuf_idx = pbuf_idx_nxt; 20903 pbuf_idx_nxt = -1; 20904 cur_pld_off = spill; 20905 20906 /* trim out first payload fragment */ 20907 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20908 20909 /* setup second payload fragment */ 20910 PDESC_PLD_SPAN_ADD(pkt_info, 20911 pbuf_idx, /* index */ 20912 md_pbuf->b_rptr, /* start */ 20913 spill); /* len */ 20914 20915 if ((*xmit_tail)->b_next == NULL) { 20916 /* 20917 * Store the lbolt used for RTT 20918 * estimation. We can only record one 20919 * timestamp per mblk so we do it when 20920 * we reach the end of the payload 20921 * buffer. Also we only take a new 20922 * timestamp sample when the previous 20923 * timed data from the same mblk has 20924 * been ack'ed. 20925 */ 20926 (*xmit_tail)->b_prev = local_time; 20927 (*xmit_tail)->b_next = 20928 (mblk_t *)(uintptr_t)first_snxt; 20929 } 20930 20931 first_snxt = *snxt - spill; 20932 20933 /* 20934 * Advance xmit_tail; usable could be 0 by 20935 * the time we got here, but we made sure 20936 * above that we would only spillover to 20937 * the next data block if usable includes 20938 * the spilled-over amount prior to the 20939 * subtraction. Therefore, we are sure 20940 * that xmit_tail->b_cont can't be NULL. 20941 */ 20942 ASSERT((*xmit_tail)->b_cont != NULL); 20943 *xmit_tail = (*xmit_tail)->b_cont; 20944 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20945 (uintptr_t)INT_MAX); 20946 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20947 } else { 20948 cur_pld_off += tcp->tcp_last_sent_len; 20949 } 20950 20951 /* 20952 * Fill in the header using the template header, and 20953 * add options such as time-stamp, ECN and/or SACK, 20954 * as needed. 20955 */ 20956 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20957 (clock_t)local_time, num_sack_blk); 20958 20959 /* take care of some IP header businesses */ 20960 if (af == AF_INET) { 20961 ipha = (ipha_t *)pkt_info->hdr_rptr; 20962 20963 ASSERT(OK_32PTR((uchar_t *)ipha)); 20964 ASSERT(PDESC_HDRL(pkt_info) >= 20965 IP_SIMPLE_HDR_LENGTH); 20966 ASSERT(ipha->ipha_version_and_hdr_length == 20967 IP_SIMPLE_HDR_VERSION); 20968 20969 /* 20970 * Assign ident value for current packet; see 20971 * related comments in ip_wput_ire() about the 20972 * contract private interface with clustering 20973 * group. 20974 */ 20975 clusterwide = B_FALSE; 20976 if (cl_inet_ipident != NULL) { 20977 ASSERT(cl_inet_isclusterwide != NULL); 20978 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 20979 AF_INET, 20980 (uint8_t *)(uintptr_t)src)) { 20981 ipha->ipha_ident = 20982 (*cl_inet_ipident) 20983 (IPPROTO_IP, AF_INET, 20984 (uint8_t *)(uintptr_t)src, 20985 (uint8_t *)(uintptr_t)dst); 20986 clusterwide = B_TRUE; 20987 } 20988 } 20989 20990 if (!clusterwide) { 20991 ipha->ipha_ident = (uint16_t) 20992 atomic_add_32_nv( 20993 &ire->ire_ident, 1); 20994 } 20995 #ifndef _BIG_ENDIAN 20996 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20997 (ipha->ipha_ident >> 8); 20998 #endif 20999 } else { 21000 ip6h = (ip6_t *)pkt_info->hdr_rptr; 21001 21002 ASSERT(OK_32PTR((uchar_t *)ip6h)); 21003 ASSERT(IPVER(ip6h) == IPV6_VERSION); 21004 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 21005 ASSERT(PDESC_HDRL(pkt_info) >= 21006 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 21007 TCP_CHECKSUM_SIZE)); 21008 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21009 21010 if (tcp->tcp_ip_forward_progress) { 21011 rconfirm = B_TRUE; 21012 tcp->tcp_ip_forward_progress = B_FALSE; 21013 } 21014 } 21015 21016 /* at least one payload span, and at most two */ 21017 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 21018 21019 /* add the packet descriptor to Multidata */ 21020 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 21021 KM_NOSLEEP)) == NULL) { 21022 /* 21023 * Any failure other than ENOMEM indicates 21024 * that we have passed in invalid pkt_info 21025 * or parameters to mmd_addpdesc, which must 21026 * not happen. 21027 * 21028 * EINVAL is a result of failure on boundary 21029 * checks against the pkt_info contents. It 21030 * should not happen, and we panic because 21031 * either there's horrible heap corruption, 21032 * and/or programming mistake. 21033 */ 21034 if (err != ENOMEM) { 21035 cmn_err(CE_PANIC, "tcp_multisend: " 21036 "pdesc logic error detected for " 21037 "tcp %p mmd %p pinfo %p (%d)\n", 21038 (void *)tcp, (void *)mmd, 21039 (void *)pkt_info, err); 21040 } 21041 TCP_STAT(tcps, tcp_mdt_addpdescfail); 21042 goto legacy_send; /* out_of_mem */ 21043 } 21044 ASSERT(pkt != NULL); 21045 21046 /* calculate IP header and TCP checksums */ 21047 if (af == AF_INET) { 21048 /* calculate pseudo-header checksum */ 21049 cksum = (dst >> 16) + (dst & 0xFFFF) + 21050 (src >> 16) + (src & 0xFFFF); 21051 21052 /* offset for TCP header checksum */ 21053 up = IPH_TCPH_CHECKSUMP(ipha, 21054 IP_SIMPLE_HDR_LENGTH); 21055 } else { 21056 up = (uint16_t *)&ip6h->ip6_src; 21057 21058 /* calculate pseudo-header checksum */ 21059 cksum = up[0] + up[1] + up[2] + up[3] + 21060 up[4] + up[5] + up[6] + up[7] + 21061 up[8] + up[9] + up[10] + up[11] + 21062 up[12] + up[13] + up[14] + up[15]; 21063 21064 /* Fold the initial sum */ 21065 cksum = (cksum & 0xffff) + (cksum >> 16); 21066 21067 up = (uint16_t *)(((uchar_t *)ip6h) + 21068 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 21069 } 21070 21071 if (hwcksum_flags & HCK_FULLCKSUM) { 21072 /* clear checksum field for hardware */ 21073 *up = 0; 21074 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 21075 uint32_t sum; 21076 21077 /* pseudo-header checksumming */ 21078 sum = *up + cksum + IP_TCP_CSUM_COMP; 21079 sum = (sum & 0xFFFF) + (sum >> 16); 21080 *up = (sum & 0xFFFF) + (sum >> 16); 21081 } else { 21082 /* software checksumming */ 21083 TCP_STAT(tcps, tcp_out_sw_cksum); 21084 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 21085 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 21086 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 21087 cksum + IP_TCP_CSUM_COMP); 21088 if (*up == 0) 21089 *up = 0xFFFF; 21090 } 21091 21092 /* IPv4 header checksum */ 21093 if (af == AF_INET) { 21094 ipha->ipha_fragment_offset_and_flags |= 21095 (uint32_t)htons(ire->ire_frag_flag); 21096 21097 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 21098 ipha->ipha_hdr_checksum = 0; 21099 } else { 21100 IP_HDR_CKSUM(ipha, cksum, 21101 ((uint32_t *)ipha)[0], 21102 ((uint16_t *)ipha)[4]); 21103 } 21104 } 21105 21106 if (af == AF_INET && 21107 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 21108 af == AF_INET6 && 21109 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 21110 /* build header(IP/TCP) mblk for this segment */ 21111 if ((mp = dupb(md_hbuf)) == NULL) 21112 goto legacy_send; 21113 21114 mp->b_rptr = pkt_info->hdr_rptr; 21115 mp->b_wptr = pkt_info->hdr_wptr; 21116 21117 /* build payload mblk for this segment */ 21118 if ((mp1 = dupb(*xmit_tail)) == NULL) { 21119 freemsg(mp); 21120 goto legacy_send; 21121 } 21122 mp1->b_wptr = md_pbuf->b_rptr + cur_pld_off; 21123 mp1->b_rptr = mp1->b_wptr - 21124 tcp->tcp_last_sent_len; 21125 linkb(mp, mp1); 21126 21127 pld_start = mp1->b_rptr; 21128 21129 if (af == AF_INET) { 21130 DTRACE_PROBE4( 21131 ip4__physical__out__start, 21132 ill_t *, NULL, 21133 ill_t *, ill, 21134 ipha_t *, ipha, 21135 mblk_t *, mp); 21136 FW_HOOKS( 21137 ipst->ips_ip4_physical_out_event, 21138 ipst->ips_ipv4firewall_physical_out, 21139 NULL, ill, ipha, mp, mp, 0, ipst); 21140 DTRACE_PROBE1( 21141 ip4__physical__out__end, 21142 mblk_t *, mp); 21143 } else { 21144 DTRACE_PROBE4( 21145 ip6__physical__out_start, 21146 ill_t *, NULL, 21147 ill_t *, ill, 21148 ip6_t *, ip6h, 21149 mblk_t *, mp); 21150 FW_HOOKS6( 21151 ipst->ips_ip6_physical_out_event, 21152 ipst->ips_ipv6firewall_physical_out, 21153 NULL, ill, ip6h, mp, mp, 0, ipst); 21154 DTRACE_PROBE1( 21155 ip6__physical__out__end, 21156 mblk_t *, mp); 21157 } 21158 21159 if (buf_trunked && mp != NULL) { 21160 /* 21161 * Need to pass it to normal path. 21162 */ 21163 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21164 } else if (mp == NULL || 21165 mp->b_rptr != pkt_info->hdr_rptr || 21166 mp->b_wptr != pkt_info->hdr_wptr || 21167 (mp1 = mp->b_cont) == NULL || 21168 mp1->b_rptr != pld_start || 21169 mp1->b_wptr != pld_start + 21170 tcp->tcp_last_sent_len || 21171 mp1->b_cont != NULL) { 21172 /* 21173 * Need to pass all packets of this 21174 * buffer to normal path, either when 21175 * packet is blocked, or when boundary 21176 * of header buffer or payload buffer 21177 * has been changed by FW_HOOKS[6]. 21178 */ 21179 buf_trunked = B_TRUE; 21180 if (md_mp_head != NULL) { 21181 err = (intptr_t)rmvb(md_mp_head, 21182 md_mp); 21183 if (err == 0) 21184 md_mp_head = NULL; 21185 } 21186 21187 /* send down what we've got so far */ 21188 if (md_mp_head != NULL) { 21189 tcp_multisend_data(tcp, ire, 21190 ill, md_mp_head, obsegs, 21191 obbytes, &rconfirm); 21192 } 21193 md_mp_head = NULL; 21194 21195 if (mp != NULL) 21196 CALL_IP_WPUT(tcp->tcp_connp, 21197 q, mp); 21198 21199 mp1 = fw_mp_head; 21200 do { 21201 mp = mp1; 21202 mp1 = mp1->b_next; 21203 mp->b_next = NULL; 21204 mp->b_prev = NULL; 21205 CALL_IP_WPUT(tcp->tcp_connp, 21206 q, mp); 21207 } while (mp1 != NULL); 21208 21209 fw_mp_head = NULL; 21210 } else { 21211 if (fw_mp_head == NULL) 21212 fw_mp_head = mp; 21213 else 21214 fw_mp_head->b_prev->b_next = mp; 21215 fw_mp_head->b_prev = mp; 21216 } 21217 } 21218 21219 /* advance header offset */ 21220 cur_hdr_off += hdr_frag_sz; 21221 21222 obbytes += tcp->tcp_last_sent_len; 21223 ++obsegs; 21224 } while (!done && *usable > 0 && --num_burst_seg > 0 && 21225 *tail_unsent > 0); 21226 21227 if ((*xmit_tail)->b_next == NULL) { 21228 /* 21229 * Store the lbolt used for RTT estimation. We can only 21230 * record one timestamp per mblk so we do it when we 21231 * reach the end of the payload buffer. Also we only 21232 * take a new timestamp sample when the previous timed 21233 * data from the same mblk has been ack'ed. 21234 */ 21235 (*xmit_tail)->b_prev = local_time; 21236 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 21237 } 21238 21239 ASSERT(*tail_unsent >= 0); 21240 if (*tail_unsent > 0) { 21241 /* 21242 * We got here because we broke out of the above 21243 * loop due to of one of the following cases: 21244 * 21245 * 1. len < adjusted MSS (i.e. small), 21246 * 2. Sender SWS avoidance, 21247 * 3. max_pld is zero. 21248 * 21249 * We are done for this Multidata, so trim our 21250 * last payload buffer (if any) accordingly. 21251 */ 21252 if (md_pbuf != NULL) 21253 md_pbuf->b_wptr -= *tail_unsent; 21254 } else if (*usable > 0) { 21255 *xmit_tail = (*xmit_tail)->b_cont; 21256 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 21257 (uintptr_t)INT_MAX); 21258 *tail_unsent = (int)MBLKL(*xmit_tail); 21259 add_buffer = B_TRUE; 21260 } 21261 21262 while (fw_mp_head) { 21263 mp = fw_mp_head; 21264 fw_mp_head = fw_mp_head->b_next; 21265 mp->b_prev = mp->b_next = NULL; 21266 freemsg(mp); 21267 } 21268 if (buf_trunked) { 21269 TCP_STAT(tcps, tcp_mdt_discarded); 21270 freeb(md_mp); 21271 buf_trunked = B_FALSE; 21272 } 21273 } while (!done && *usable > 0 && num_burst_seg > 0 && 21274 (tcp_mdt_chain || max_pld > 0)); 21275 21276 if (md_mp_head != NULL) { 21277 /* send everything down */ 21278 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 21279 &rconfirm); 21280 } 21281 21282 #undef PREP_NEW_MULTIDATA 21283 #undef PREP_NEW_PBUF 21284 #undef IPVER 21285 21286 IRE_REFRELE(ire); 21287 return (0); 21288 } 21289 21290 /* 21291 * A wrapper function for sending one or more Multidata messages down to 21292 * the module below ip; this routine does not release the reference of the 21293 * IRE (caller does that). This routine is analogous to tcp_send_data(). 21294 */ 21295 static void 21296 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 21297 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 21298 { 21299 uint64_t delta; 21300 nce_t *nce; 21301 tcp_stack_t *tcps = tcp->tcp_tcps; 21302 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21303 21304 ASSERT(ire != NULL && ill != NULL); 21305 ASSERT(ire->ire_stq != NULL); 21306 ASSERT(md_mp_head != NULL); 21307 ASSERT(rconfirm != NULL); 21308 21309 /* adjust MIBs and IRE timestamp */ 21310 TCP_RECORD_TRACE(tcp, md_mp_head, TCP_TRACE_SEND_PKT); 21311 tcp->tcp_obsegs += obsegs; 21312 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 21313 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 21314 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 21315 21316 if (tcp->tcp_ipversion == IPV4_VERSION) { 21317 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 21318 } else { 21319 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 21320 } 21321 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 21322 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 21323 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 21324 21325 ire->ire_ob_pkt_count += obsegs; 21326 if (ire->ire_ipif != NULL) 21327 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 21328 ire->ire_last_used_time = lbolt; 21329 21330 /* send it down */ 21331 if (ILL_DLS_CAPABLE(ill)) { 21332 ill_dls_capab_t *ill_dls = ill->ill_dls_capab; 21333 ill_dls->ill_tx(ill_dls->ill_tx_handle, md_mp_head); 21334 } else { 21335 putnext(ire->ire_stq, md_mp_head); 21336 } 21337 21338 /* we're done for TCP/IPv4 */ 21339 if (tcp->tcp_ipversion == IPV4_VERSION) 21340 return; 21341 21342 nce = ire->ire_nce; 21343 21344 ASSERT(nce != NULL); 21345 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 21346 ASSERT(nce->nce_state != ND_INCOMPLETE); 21347 21348 /* reachability confirmation? */ 21349 if (*rconfirm) { 21350 nce->nce_last = TICK_TO_MSEC(lbolt64); 21351 if (nce->nce_state != ND_REACHABLE) { 21352 mutex_enter(&nce->nce_lock); 21353 nce->nce_state = ND_REACHABLE; 21354 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 21355 mutex_exit(&nce->nce_lock); 21356 (void) untimeout(nce->nce_timeout_id); 21357 if (ip_debug > 2) { 21358 /* ip1dbg */ 21359 pr_addr_dbg("tcp_multisend_data: state " 21360 "for %s changed to REACHABLE\n", 21361 AF_INET6, &ire->ire_addr_v6); 21362 } 21363 } 21364 /* reset transport reachability confirmation */ 21365 *rconfirm = B_FALSE; 21366 } 21367 21368 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 21369 ip1dbg(("tcp_multisend_data: delta = %" PRId64 21370 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 21371 21372 if (delta > (uint64_t)ill->ill_reachable_time) { 21373 mutex_enter(&nce->nce_lock); 21374 switch (nce->nce_state) { 21375 case ND_REACHABLE: 21376 case ND_STALE: 21377 /* 21378 * ND_REACHABLE is identical to ND_STALE in this 21379 * specific case. If reachable time has expired for 21380 * this neighbor (delta is greater than reachable 21381 * time), conceptually, the neighbor cache is no 21382 * longer in REACHABLE state, but already in STALE 21383 * state. So the correct transition here is to 21384 * ND_DELAY. 21385 */ 21386 nce->nce_state = ND_DELAY; 21387 mutex_exit(&nce->nce_lock); 21388 NDP_RESTART_TIMER(nce, 21389 ipst->ips_delay_first_probe_time); 21390 if (ip_debug > 3) { 21391 /* ip2dbg */ 21392 pr_addr_dbg("tcp_multisend_data: state " 21393 "for %s changed to DELAY\n", 21394 AF_INET6, &ire->ire_addr_v6); 21395 } 21396 break; 21397 case ND_DELAY: 21398 case ND_PROBE: 21399 mutex_exit(&nce->nce_lock); 21400 /* Timers have already started */ 21401 break; 21402 case ND_UNREACHABLE: 21403 /* 21404 * ndp timer has detected that this nce is 21405 * unreachable and initiated deleting this nce 21406 * and all its associated IREs. This is a race 21407 * where we found the ire before it was deleted 21408 * and have just sent out a packet using this 21409 * unreachable nce. 21410 */ 21411 mutex_exit(&nce->nce_lock); 21412 break; 21413 default: 21414 ASSERT(0); 21415 } 21416 } 21417 } 21418 21419 /* 21420 * Derived from tcp_send_data(). 21421 */ 21422 static void 21423 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 21424 int num_lso_seg) 21425 { 21426 ipha_t *ipha; 21427 mblk_t *ire_fp_mp; 21428 uint_t ire_fp_mp_len; 21429 uint32_t hcksum_txflags = 0; 21430 ipaddr_t src; 21431 ipaddr_t dst; 21432 uint32_t cksum; 21433 uint16_t *up; 21434 tcp_stack_t *tcps = tcp->tcp_tcps; 21435 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21436 21437 ASSERT(DB_TYPE(mp) == M_DATA); 21438 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 21439 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 21440 ASSERT(tcp->tcp_connp != NULL); 21441 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 21442 21443 ipha = (ipha_t *)mp->b_rptr; 21444 src = ipha->ipha_src; 21445 dst = ipha->ipha_dst; 21446 21447 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 21448 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 21449 num_lso_seg); 21450 #ifndef _BIG_ENDIAN 21451 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 21452 #endif 21453 if (tcp->tcp_snd_zcopy_aware) { 21454 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 21455 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 21456 mp = tcp_zcopy_disable(tcp, mp); 21457 } 21458 21459 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 21460 ASSERT(ill->ill_hcksum_capab != NULL); 21461 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 21462 } 21463 21464 /* 21465 * Since the TCP checksum should be recalculated by h/w, we can just 21466 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 21467 * pseudo-header checksum for HCK_PARTIALCKSUM. 21468 * The partial pseudo-header excludes TCP length, that was calculated 21469 * in tcp_send(), so to zero *up before further processing. 21470 */ 21471 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 21472 21473 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 21474 *up = 0; 21475 21476 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 21477 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 21478 21479 /* 21480 * Append LSO flag to DB_LSOFLAGS(mp) and set the mss to DB_LSOMSS(mp). 21481 */ 21482 DB_LSOFLAGS(mp) |= HW_LSO; 21483 DB_LSOMSS(mp) = mss; 21484 21485 ipha->ipha_fragment_offset_and_flags |= 21486 (uint32_t)htons(ire->ire_frag_flag); 21487 21488 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21489 ire_fp_mp_len = MBLKL(ire_fp_mp); 21490 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 21491 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 21492 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 21493 21494 UPDATE_OB_PKT_COUNT(ire); 21495 ire->ire_last_used_time = lbolt; 21496 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 21497 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 21498 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 21499 ntohs(ipha->ipha_length)); 21500 21501 if (ILL_DLS_CAPABLE(ill)) { 21502 /* 21503 * Send the packet directly to DLD, where it may be queued 21504 * depending on the availability of transmit resources at 21505 * the media layer. 21506 */ 21507 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 21508 } else { 21509 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 21510 DTRACE_PROBE4(ip4__physical__out__start, 21511 ill_t *, NULL, ill_t *, out_ill, 21512 ipha_t *, ipha, mblk_t *, mp); 21513 FW_HOOKS(ipst->ips_ip4_physical_out_event, 21514 ipst->ips_ipv4firewall_physical_out, 21515 NULL, out_ill, ipha, mp, mp, 0, ipst); 21516 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 21517 if (mp != NULL) 21518 putnext(ire->ire_stq, mp); 21519 } 21520 } 21521 21522 /* 21523 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 21524 * scheme, and returns one of the following: 21525 * 21526 * -1 = failed allocation. 21527 * 0 = success; burst count reached, or usable send window is too small, 21528 * and that we'd rather wait until later before sending again. 21529 * 1 = success; we are called from tcp_multisend(), and both usable send 21530 * window and tail_unsent are greater than the MDT threshold, and thus 21531 * Multidata Transmit should be used instead. 21532 */ 21533 static int 21534 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 21535 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 21536 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 21537 const int mdt_thres) 21538 { 21539 int num_burst_seg = tcp->tcp_snd_burst; 21540 ire_t *ire = NULL; 21541 ill_t *ill = NULL; 21542 mblk_t *ire_fp_mp = NULL; 21543 uint_t ire_fp_mp_len = 0; 21544 int num_lso_seg = 1; 21545 uint_t lso_usable; 21546 boolean_t do_lso_send = B_FALSE; 21547 tcp_stack_t *tcps = tcp->tcp_tcps; 21548 21549 /* 21550 * Check LSO capability before any further work. And the similar check 21551 * need to be done in for(;;) loop. 21552 * LSO will be deployed when therer is more than one mss of available 21553 * data and a burst transmission is allowed. 21554 */ 21555 if (tcp->tcp_lso && 21556 (tcp->tcp_valid_bits == 0 || 21557 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21558 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21559 /* 21560 * Try to find usable IRE/ILL and do basic check to the ILL. 21561 */ 21562 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 21563 /* 21564 * Enable LSO with this transmission. 21565 * Since IRE has been hold in 21566 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 21567 * should be called before return. 21568 */ 21569 do_lso_send = B_TRUE; 21570 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21571 ire_fp_mp_len = MBLKL(ire_fp_mp); 21572 /* Round up to multiple of 4 */ 21573 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21574 } else { 21575 do_lso_send = B_FALSE; 21576 ill = NULL; 21577 } 21578 } 21579 21580 for (;;) { 21581 struct datab *db; 21582 tcph_t *tcph; 21583 uint32_t sum; 21584 mblk_t *mp, *mp1; 21585 uchar_t *rptr; 21586 int len; 21587 21588 /* 21589 * If we're called by tcp_multisend(), and the amount of 21590 * sendable data as well as the size of current xmit_tail 21591 * is beyond the MDT threshold, return to the caller and 21592 * let the large data transmit be done using MDT. 21593 */ 21594 if (*usable > 0 && *usable > mdt_thres && 21595 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21596 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21597 ASSERT(tcp->tcp_mdt); 21598 return (1); /* success; do large send */ 21599 } 21600 21601 if (num_burst_seg == 0) 21602 break; /* success; burst count reached */ 21603 21604 /* 21605 * Calculate the maximum payload length we can send in *one* 21606 * time. 21607 */ 21608 if (do_lso_send) { 21609 /* 21610 * Check whether need to do LSO any more. 21611 */ 21612 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21613 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21614 lso_usable = MIN(lso_usable, 21615 num_burst_seg * mss); 21616 21617 num_lso_seg = lso_usable / mss; 21618 if (lso_usable % mss) { 21619 num_lso_seg++; 21620 tcp->tcp_last_sent_len = (ushort_t) 21621 (lso_usable % mss); 21622 } else { 21623 tcp->tcp_last_sent_len = (ushort_t)mss; 21624 } 21625 } else { 21626 do_lso_send = B_FALSE; 21627 num_lso_seg = 1; 21628 lso_usable = mss; 21629 } 21630 } 21631 21632 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21633 21634 /* 21635 * Adjust num_burst_seg here. 21636 */ 21637 num_burst_seg -= num_lso_seg; 21638 21639 len = mss; 21640 if (len > *usable) { 21641 ASSERT(do_lso_send == B_FALSE); 21642 21643 len = *usable; 21644 if (len <= 0) { 21645 /* Terminate the loop */ 21646 break; /* success; too small */ 21647 } 21648 /* 21649 * Sender silly-window avoidance. 21650 * Ignore this if we are going to send a 21651 * zero window probe out. 21652 * 21653 * TODO: force data into microscopic window? 21654 * ==> (!pushed || (unsent > usable)) 21655 */ 21656 if (len < (tcp->tcp_max_swnd >> 1) && 21657 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21658 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21659 len == 1) && (! tcp->tcp_zero_win_probe)) { 21660 /* 21661 * If the retransmit timer is not running 21662 * we start it so that we will retransmit 21663 * in the case when the the receiver has 21664 * decremented the window. 21665 */ 21666 if (*snxt == tcp->tcp_snxt && 21667 *snxt == tcp->tcp_suna) { 21668 /* 21669 * We are not supposed to send 21670 * anything. So let's wait a little 21671 * bit longer before breaking SWS 21672 * avoidance. 21673 * 21674 * What should the value be? 21675 * Suggestion: MAX(init rexmit time, 21676 * tcp->tcp_rto) 21677 */ 21678 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21679 } 21680 break; /* success; too small */ 21681 } 21682 } 21683 21684 tcph = tcp->tcp_tcph; 21685 21686 /* 21687 * The reason to adjust len here is that we need to set flags 21688 * and calculate checksum. 21689 */ 21690 if (do_lso_send) 21691 len = lso_usable; 21692 21693 *usable -= len; /* Approximate - can be adjusted later */ 21694 if (*usable > 0) 21695 tcph->th_flags[0] = TH_ACK; 21696 else 21697 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21698 21699 /* 21700 * Prime pump for IP's checksumming on our behalf 21701 * Include the adjustment for a source route if any. 21702 */ 21703 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21704 sum = (sum >> 16) + (sum & 0xFFFF); 21705 U16_TO_ABE16(sum, tcph->th_sum); 21706 21707 U32_TO_ABE32(*snxt, tcph->th_seq); 21708 21709 /* 21710 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21711 * set. For the case when TCP_FSS_VALID is the only valid 21712 * bit (normal active close), branch off only when we think 21713 * that the FIN flag needs to be set. Note for this case, 21714 * that (snxt + len) may not reflect the actual seg_len, 21715 * as len may be further reduced in tcp_xmit_mp(). If len 21716 * gets modified, we will end up here again. 21717 */ 21718 if (tcp->tcp_valid_bits != 0 && 21719 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21720 ((*snxt + len) == tcp->tcp_fss))) { 21721 uchar_t *prev_rptr; 21722 uint32_t prev_snxt = tcp->tcp_snxt; 21723 21724 if (*tail_unsent == 0) { 21725 ASSERT((*xmit_tail)->b_cont != NULL); 21726 *xmit_tail = (*xmit_tail)->b_cont; 21727 prev_rptr = (*xmit_tail)->b_rptr; 21728 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21729 (*xmit_tail)->b_rptr); 21730 } else { 21731 prev_rptr = (*xmit_tail)->b_rptr; 21732 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21733 *tail_unsent; 21734 } 21735 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21736 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21737 /* Restore tcp_snxt so we get amount sent right. */ 21738 tcp->tcp_snxt = prev_snxt; 21739 if (prev_rptr == (*xmit_tail)->b_rptr) { 21740 /* 21741 * If the previous timestamp is still in use, 21742 * don't stomp on it. 21743 */ 21744 if ((*xmit_tail)->b_next == NULL) { 21745 (*xmit_tail)->b_prev = local_time; 21746 (*xmit_tail)->b_next = 21747 (mblk_t *)(uintptr_t)(*snxt); 21748 } 21749 } else 21750 (*xmit_tail)->b_rptr = prev_rptr; 21751 21752 if (mp == NULL) { 21753 if (ire != NULL) 21754 IRE_REFRELE(ire); 21755 return (-1); 21756 } 21757 mp1 = mp->b_cont; 21758 21759 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21760 tcp->tcp_last_sent_len = (ushort_t)len; 21761 while (mp1->b_cont) { 21762 *xmit_tail = (*xmit_tail)->b_cont; 21763 (*xmit_tail)->b_prev = local_time; 21764 (*xmit_tail)->b_next = 21765 (mblk_t *)(uintptr_t)(*snxt); 21766 mp1 = mp1->b_cont; 21767 } 21768 *snxt += len; 21769 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21770 BUMP_LOCAL(tcp->tcp_obsegs); 21771 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21772 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21773 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21774 tcp_send_data(tcp, q, mp); 21775 continue; 21776 } 21777 21778 *snxt += len; /* Adjust later if we don't send all of len */ 21779 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21780 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21781 21782 if (*tail_unsent) { 21783 /* Are the bytes above us in flight? */ 21784 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21785 if (rptr != (*xmit_tail)->b_rptr) { 21786 *tail_unsent -= len; 21787 if (len <= mss) /* LSO is unusable */ 21788 tcp->tcp_last_sent_len = (ushort_t)len; 21789 len += tcp_hdr_len; 21790 if (tcp->tcp_ipversion == IPV4_VERSION) 21791 tcp->tcp_ipha->ipha_length = htons(len); 21792 else 21793 tcp->tcp_ip6h->ip6_plen = 21794 htons(len - 21795 ((char *)&tcp->tcp_ip6h[1] - 21796 tcp->tcp_iphc)); 21797 mp = dupb(*xmit_tail); 21798 if (mp == NULL) { 21799 if (ire != NULL) 21800 IRE_REFRELE(ire); 21801 return (-1); /* out_of_mem */ 21802 } 21803 mp->b_rptr = rptr; 21804 /* 21805 * If the old timestamp is no longer in use, 21806 * sample a new timestamp now. 21807 */ 21808 if ((*xmit_tail)->b_next == NULL) { 21809 (*xmit_tail)->b_prev = local_time; 21810 (*xmit_tail)->b_next = 21811 (mblk_t *)(uintptr_t)(*snxt-len); 21812 } 21813 goto must_alloc; 21814 } 21815 } else { 21816 *xmit_tail = (*xmit_tail)->b_cont; 21817 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21818 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21819 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21820 (*xmit_tail)->b_rptr); 21821 } 21822 21823 (*xmit_tail)->b_prev = local_time; 21824 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21825 21826 *tail_unsent -= len; 21827 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21828 tcp->tcp_last_sent_len = (ushort_t)len; 21829 21830 len += tcp_hdr_len; 21831 if (tcp->tcp_ipversion == IPV4_VERSION) 21832 tcp->tcp_ipha->ipha_length = htons(len); 21833 else 21834 tcp->tcp_ip6h->ip6_plen = htons(len - 21835 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21836 21837 mp = dupb(*xmit_tail); 21838 if (mp == NULL) { 21839 if (ire != NULL) 21840 IRE_REFRELE(ire); 21841 return (-1); /* out_of_mem */ 21842 } 21843 21844 len = tcp_hdr_len; 21845 /* 21846 * There are four reasons to allocate a new hdr mblk: 21847 * 1) The bytes above us are in use by another packet 21848 * 2) We don't have good alignment 21849 * 3) The mblk is being shared 21850 * 4) We don't have enough room for a header 21851 */ 21852 rptr = mp->b_rptr - len; 21853 if (!OK_32PTR(rptr) || 21854 ((db = mp->b_datap), db->db_ref != 2) || 21855 rptr < db->db_base + ire_fp_mp_len) { 21856 /* NOTE: we assume allocb returns an OK_32PTR */ 21857 21858 must_alloc:; 21859 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21860 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21861 if (mp1 == NULL) { 21862 freemsg(mp); 21863 if (ire != NULL) 21864 IRE_REFRELE(ire); 21865 return (-1); /* out_of_mem */ 21866 } 21867 mp1->b_cont = mp; 21868 mp = mp1; 21869 /* Leave room for Link Level header */ 21870 len = tcp_hdr_len; 21871 rptr = 21872 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21873 mp->b_wptr = &rptr[len]; 21874 } 21875 21876 /* 21877 * Fill in the header using the template header, and add 21878 * options such as time-stamp, ECN and/or SACK, as needed. 21879 */ 21880 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21881 21882 mp->b_rptr = rptr; 21883 21884 if (*tail_unsent) { 21885 int spill = *tail_unsent; 21886 21887 mp1 = mp->b_cont; 21888 if (mp1 == NULL) 21889 mp1 = mp; 21890 21891 /* 21892 * If we're a little short, tack on more mblks until 21893 * there is no more spillover. 21894 */ 21895 while (spill < 0) { 21896 mblk_t *nmp; 21897 int nmpsz; 21898 21899 nmp = (*xmit_tail)->b_cont; 21900 nmpsz = MBLKL(nmp); 21901 21902 /* 21903 * Excess data in mblk; can we split it? 21904 * If MDT is enabled for the connection, 21905 * keep on splitting as this is a transient 21906 * send path. 21907 */ 21908 if (!do_lso_send && !tcp->tcp_mdt && 21909 (spill + nmpsz > 0)) { 21910 /* 21911 * Don't split if stream head was 21912 * told to break up larger writes 21913 * into smaller ones. 21914 */ 21915 if (tcp->tcp_maxpsz > 0) 21916 break; 21917 21918 /* 21919 * Next mblk is less than SMSS/2 21920 * rounded up to nearest 64-byte; 21921 * let it get sent as part of the 21922 * next segment. 21923 */ 21924 if (tcp->tcp_localnet && 21925 !tcp->tcp_cork && 21926 (nmpsz < roundup((mss >> 1), 64))) 21927 break; 21928 } 21929 21930 *xmit_tail = nmp; 21931 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21932 /* Stash for rtt use later */ 21933 (*xmit_tail)->b_prev = local_time; 21934 (*xmit_tail)->b_next = 21935 (mblk_t *)(uintptr_t)(*snxt - len); 21936 mp1->b_cont = dupb(*xmit_tail); 21937 mp1 = mp1->b_cont; 21938 21939 spill += nmpsz; 21940 if (mp1 == NULL) { 21941 *tail_unsent = spill; 21942 freemsg(mp); 21943 if (ire != NULL) 21944 IRE_REFRELE(ire); 21945 return (-1); /* out_of_mem */ 21946 } 21947 } 21948 21949 /* Trim back any surplus on the last mblk */ 21950 if (spill >= 0) { 21951 mp1->b_wptr -= spill; 21952 *tail_unsent = spill; 21953 } else { 21954 /* 21955 * We did not send everything we could in 21956 * order to remain within the b_cont limit. 21957 */ 21958 *usable -= spill; 21959 *snxt += spill; 21960 tcp->tcp_last_sent_len += spill; 21961 UPDATE_MIB(&tcps->tcps_mib, 21962 tcpOutDataBytes, spill); 21963 /* 21964 * Adjust the checksum 21965 */ 21966 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21967 sum += spill; 21968 sum = (sum >> 16) + (sum & 0xFFFF); 21969 U16_TO_ABE16(sum, tcph->th_sum); 21970 if (tcp->tcp_ipversion == IPV4_VERSION) { 21971 sum = ntohs( 21972 ((ipha_t *)rptr)->ipha_length) + 21973 spill; 21974 ((ipha_t *)rptr)->ipha_length = 21975 htons(sum); 21976 } else { 21977 sum = ntohs( 21978 ((ip6_t *)rptr)->ip6_plen) + 21979 spill; 21980 ((ip6_t *)rptr)->ip6_plen = 21981 htons(sum); 21982 } 21983 *tail_unsent = 0; 21984 } 21985 } 21986 if (tcp->tcp_ip_forward_progress) { 21987 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21988 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21989 tcp->tcp_ip_forward_progress = B_FALSE; 21990 } 21991 21992 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21993 if (do_lso_send) { 21994 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21995 num_lso_seg); 21996 tcp->tcp_obsegs += num_lso_seg; 21997 21998 TCP_STAT(tcps, tcp_lso_times); 21999 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 22000 } else { 22001 tcp_send_data(tcp, q, mp); 22002 BUMP_LOCAL(tcp->tcp_obsegs); 22003 } 22004 } 22005 22006 if (ire != NULL) 22007 IRE_REFRELE(ire); 22008 return (0); 22009 } 22010 22011 /* Unlink and return any mblk that looks like it contains a MDT info */ 22012 static mblk_t * 22013 tcp_mdt_info_mp(mblk_t *mp) 22014 { 22015 mblk_t *prev_mp; 22016 22017 for (;;) { 22018 prev_mp = mp; 22019 /* no more to process? */ 22020 if ((mp = mp->b_cont) == NULL) 22021 break; 22022 22023 switch (DB_TYPE(mp)) { 22024 case M_CTL: 22025 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 22026 continue; 22027 ASSERT(prev_mp != NULL); 22028 prev_mp->b_cont = mp->b_cont; 22029 mp->b_cont = NULL; 22030 return (mp); 22031 default: 22032 break; 22033 } 22034 } 22035 return (mp); 22036 } 22037 22038 /* MDT info update routine, called when IP notifies us about MDT */ 22039 static void 22040 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 22041 { 22042 boolean_t prev_state; 22043 tcp_stack_t *tcps = tcp->tcp_tcps; 22044 22045 /* 22046 * IP is telling us to abort MDT on this connection? We know 22047 * this because the capability is only turned off when IP 22048 * encounters some pathological cases, e.g. link-layer change 22049 * where the new driver doesn't support MDT, or in situation 22050 * where MDT usage on the link-layer has been switched off. 22051 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 22052 * if the link-layer doesn't support MDT, and if it does, it 22053 * will indicate that the feature is to be turned on. 22054 */ 22055 prev_state = tcp->tcp_mdt; 22056 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 22057 if (!tcp->tcp_mdt && !first) { 22058 TCP_STAT(tcps, tcp_mdt_conn_halted3); 22059 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 22060 (void *)tcp->tcp_connp)); 22061 } 22062 22063 /* 22064 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 22065 * so disable MDT otherwise. The checks are done here 22066 * and in tcp_wput_data(). 22067 */ 22068 if (tcp->tcp_mdt && 22069 (tcp->tcp_ipversion == IPV4_VERSION && 22070 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 22071 (tcp->tcp_ipversion == IPV6_VERSION && 22072 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 22073 tcp->tcp_mdt = B_FALSE; 22074 22075 if (tcp->tcp_mdt) { 22076 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 22077 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 22078 "version (%d), expected version is %d", 22079 mdt_capab->ill_mdt_version, MDT_VERSION_2); 22080 tcp->tcp_mdt = B_FALSE; 22081 return; 22082 } 22083 22084 /* 22085 * We need the driver to be able to handle at least three 22086 * spans per packet in order for tcp MDT to be utilized. 22087 * The first is for the header portion, while the rest are 22088 * needed to handle a packet that straddles across two 22089 * virtually non-contiguous buffers; a typical tcp packet 22090 * therefore consists of only two spans. Note that we take 22091 * a zero as "don't care". 22092 */ 22093 if (mdt_capab->ill_mdt_span_limit > 0 && 22094 mdt_capab->ill_mdt_span_limit < 3) { 22095 tcp->tcp_mdt = B_FALSE; 22096 return; 22097 } 22098 22099 /* a zero means driver wants default value */ 22100 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 22101 tcps->tcps_mdt_max_pbufs); 22102 if (tcp->tcp_mdt_max_pld == 0) 22103 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 22104 22105 /* ensure 32-bit alignment */ 22106 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 22107 mdt_capab->ill_mdt_hdr_head), 4); 22108 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 22109 mdt_capab->ill_mdt_hdr_tail), 4); 22110 22111 if (!first && !prev_state) { 22112 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 22113 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 22114 (void *)tcp->tcp_connp)); 22115 } 22116 } 22117 } 22118 22119 /* Unlink and return any mblk that looks like it contains a LSO info */ 22120 static mblk_t * 22121 tcp_lso_info_mp(mblk_t *mp) 22122 { 22123 mblk_t *prev_mp; 22124 22125 for (;;) { 22126 prev_mp = mp; 22127 /* no more to process? */ 22128 if ((mp = mp->b_cont) == NULL) 22129 break; 22130 22131 switch (DB_TYPE(mp)) { 22132 case M_CTL: 22133 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 22134 continue; 22135 ASSERT(prev_mp != NULL); 22136 prev_mp->b_cont = mp->b_cont; 22137 mp->b_cont = NULL; 22138 return (mp); 22139 default: 22140 break; 22141 } 22142 } 22143 22144 return (mp); 22145 } 22146 22147 /* LSO info update routine, called when IP notifies us about LSO */ 22148 static void 22149 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 22150 { 22151 tcp_stack_t *tcps = tcp->tcp_tcps; 22152 22153 /* 22154 * IP is telling us to abort LSO on this connection? We know 22155 * this because the capability is only turned off when IP 22156 * encounters some pathological cases, e.g. link-layer change 22157 * where the new NIC/driver doesn't support LSO, or in situation 22158 * where LSO usage on the link-layer has been switched off. 22159 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 22160 * if the link-layer doesn't support LSO, and if it does, it 22161 * will indicate that the feature is to be turned on. 22162 */ 22163 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 22164 TCP_STAT(tcps, tcp_lso_enabled); 22165 22166 /* 22167 * We currently only support LSO on simple TCP/IPv4, 22168 * so disable LSO otherwise. The checks are done here 22169 * and in tcp_wput_data(). 22170 */ 22171 if (tcp->tcp_lso && 22172 (tcp->tcp_ipversion == IPV4_VERSION && 22173 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 22174 (tcp->tcp_ipversion == IPV6_VERSION)) { 22175 tcp->tcp_lso = B_FALSE; 22176 TCP_STAT(tcps, tcp_lso_disabled); 22177 } else { 22178 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 22179 lso_capab->ill_lso_max); 22180 } 22181 } 22182 22183 static void 22184 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 22185 { 22186 conn_t *connp = tcp->tcp_connp; 22187 tcp_stack_t *tcps = tcp->tcp_tcps; 22188 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22189 22190 ASSERT(ire != NULL); 22191 22192 /* 22193 * We may be in the fastpath here, and although we essentially do 22194 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 22195 * we try to keep things as brief as possible. After all, these 22196 * are only best-effort checks, and we do more thorough ones prior 22197 * to calling tcp_send()/tcp_multisend(). 22198 */ 22199 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 22200 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 22201 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 22202 !(ire->ire_flags & RTF_MULTIRT) && 22203 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 22204 CONN_IS_LSO_MD_FASTPATH(connp)) { 22205 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 22206 /* Cache the result */ 22207 connp->conn_lso_ok = B_TRUE; 22208 22209 ASSERT(ill->ill_lso_capab != NULL); 22210 if (!ill->ill_lso_capab->ill_lso_on) { 22211 ill->ill_lso_capab->ill_lso_on = 1; 22212 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22213 "LSO for interface %s\n", (void *)connp, 22214 ill->ill_name)); 22215 } 22216 tcp_lso_update(tcp, ill->ill_lso_capab); 22217 } else if (ipst->ips_ip_multidata_outbound && 22218 ILL_MDT_CAPABLE(ill)) { 22219 /* Cache the result */ 22220 connp->conn_mdt_ok = B_TRUE; 22221 22222 ASSERT(ill->ill_mdt_capab != NULL); 22223 if (!ill->ill_mdt_capab->ill_mdt_on) { 22224 ill->ill_mdt_capab->ill_mdt_on = 1; 22225 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22226 "MDT for interface %s\n", (void *)connp, 22227 ill->ill_name)); 22228 } 22229 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 22230 } 22231 } 22232 22233 /* 22234 * The goal is to reduce the number of generated tcp segments by 22235 * setting the maxpsz multiplier to 0; this will have an affect on 22236 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 22237 * into each packet, up to SMSS bytes. Doing this reduces the number 22238 * of outbound segments and incoming ACKs, thus allowing for better 22239 * network and system performance. In contrast the legacy behavior 22240 * may result in sending less than SMSS size, because the last mblk 22241 * for some packets may have more data than needed to make up SMSS, 22242 * and the legacy code refused to "split" it. 22243 * 22244 * We apply the new behavior on following situations: 22245 * 22246 * 1) Loopback connections, 22247 * 2) Connections in which the remote peer is not on local subnet, 22248 * 3) Local subnet connections over the bge interface (see below). 22249 * 22250 * Ideally, we would like this behavior to apply for interfaces other 22251 * than bge. However, doing so would negatively impact drivers which 22252 * perform dynamic mapping and unmapping of DMA resources, which are 22253 * increased by setting the maxpsz multiplier to 0 (more mblks per 22254 * packet will be generated by tcp). The bge driver does not suffer 22255 * from this, as it copies the mblks into pre-mapped buffers, and 22256 * therefore does not require more I/O resources than before. 22257 * 22258 * Otherwise, this behavior is present on all network interfaces when 22259 * the destination endpoint is non-local, since reducing the number 22260 * of packets in general is good for the network. 22261 * 22262 * TODO We need to remove this hard-coded conditional for bge once 22263 * a better "self-tuning" mechanism, or a way to comprehend 22264 * the driver transmit strategy is devised. Until the solution 22265 * is found and well understood, we live with this hack. 22266 */ 22267 if (!tcp_static_maxpsz && 22268 (tcp->tcp_loopback || !tcp->tcp_localnet || 22269 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 22270 /* override the default value */ 22271 tcp->tcp_maxpsz = 0; 22272 22273 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 22274 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 22275 ill != NULL ? ill->ill_name : ipif_loopback_name)); 22276 } 22277 22278 /* set the stream head parameters accordingly */ 22279 (void) tcp_maxpsz_set(tcp, B_TRUE); 22280 } 22281 22282 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 22283 static void 22284 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 22285 { 22286 uchar_t fval = *mp->b_rptr; 22287 mblk_t *tail; 22288 queue_t *q = tcp->tcp_wq; 22289 22290 /* TODO: How should flush interact with urgent data? */ 22291 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 22292 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 22293 /* 22294 * Flush only data that has not yet been put on the wire. If 22295 * we flush data that we have already transmitted, life, as we 22296 * know it, may come to an end. 22297 */ 22298 tail = tcp->tcp_xmit_tail; 22299 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 22300 tcp->tcp_xmit_tail_unsent = 0; 22301 tcp->tcp_unsent = 0; 22302 if (tail->b_wptr != tail->b_rptr) 22303 tail = tail->b_cont; 22304 if (tail) { 22305 mblk_t **excess = &tcp->tcp_xmit_head; 22306 for (;;) { 22307 mblk_t *mp1 = *excess; 22308 if (mp1 == tail) 22309 break; 22310 tcp->tcp_xmit_tail = mp1; 22311 tcp->tcp_xmit_last = mp1; 22312 excess = &mp1->b_cont; 22313 } 22314 *excess = NULL; 22315 tcp_close_mpp(&tail); 22316 if (tcp->tcp_snd_zcopy_aware) 22317 tcp_zcopy_notify(tcp); 22318 } 22319 /* 22320 * We have no unsent data, so unsent must be less than 22321 * tcp_xmit_lowater, so re-enable flow. 22322 */ 22323 mutex_enter(&tcp->tcp_non_sq_lock); 22324 if (tcp->tcp_flow_stopped) { 22325 tcp_clrqfull(tcp); 22326 } 22327 mutex_exit(&tcp->tcp_non_sq_lock); 22328 } 22329 /* 22330 * TODO: you can't just flush these, you have to increase rwnd for one 22331 * thing. For another, how should urgent data interact? 22332 */ 22333 if (fval & FLUSHR) { 22334 *mp->b_rptr = fval & ~FLUSHW; 22335 /* XXX */ 22336 qreply(q, mp); 22337 return; 22338 } 22339 freemsg(mp); 22340 } 22341 22342 /* 22343 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 22344 * messages. 22345 */ 22346 static void 22347 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 22348 { 22349 mblk_t *mp1; 22350 STRUCT_HANDLE(strbuf, sb); 22351 uint16_t port; 22352 queue_t *q = tcp->tcp_wq; 22353 in6_addr_t v6addr; 22354 ipaddr_t v4addr; 22355 uint32_t flowinfo = 0; 22356 int addrlen; 22357 22358 /* Make sure it is one of ours. */ 22359 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 22360 case TI_GETMYNAME: 22361 case TI_GETPEERNAME: 22362 break; 22363 default: 22364 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 22365 return; 22366 } 22367 switch (mi_copy_state(q, mp, &mp1)) { 22368 case -1: 22369 return; 22370 case MI_COPY_CASE(MI_COPY_IN, 1): 22371 break; 22372 case MI_COPY_CASE(MI_COPY_OUT, 1): 22373 /* Copy out the strbuf. */ 22374 mi_copyout(q, mp); 22375 return; 22376 case MI_COPY_CASE(MI_COPY_OUT, 2): 22377 /* All done. */ 22378 mi_copy_done(q, mp, 0); 22379 return; 22380 default: 22381 mi_copy_done(q, mp, EPROTO); 22382 return; 22383 } 22384 /* Check alignment of the strbuf */ 22385 if (!OK_32PTR(mp1->b_rptr)) { 22386 mi_copy_done(q, mp, EINVAL); 22387 return; 22388 } 22389 22390 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag, 22391 (void *)mp1->b_rptr); 22392 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 22393 22394 if (STRUCT_FGET(sb, maxlen) < addrlen) { 22395 mi_copy_done(q, mp, EINVAL); 22396 return; 22397 } 22398 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 22399 case TI_GETMYNAME: 22400 if (tcp->tcp_family == AF_INET) { 22401 if (tcp->tcp_ipversion == IPV4_VERSION) { 22402 v4addr = tcp->tcp_ipha->ipha_src; 22403 } else { 22404 /* can't return an address in this case */ 22405 v4addr = 0; 22406 } 22407 } else { 22408 /* tcp->tcp_family == AF_INET6 */ 22409 if (tcp->tcp_ipversion == IPV4_VERSION) { 22410 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 22411 &v6addr); 22412 } else { 22413 v6addr = tcp->tcp_ip6h->ip6_src; 22414 } 22415 } 22416 port = tcp->tcp_lport; 22417 break; 22418 case TI_GETPEERNAME: 22419 if (tcp->tcp_family == AF_INET) { 22420 if (tcp->tcp_ipversion == IPV4_VERSION) { 22421 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 22422 v4addr); 22423 } else { 22424 /* can't return an address in this case */ 22425 v4addr = 0; 22426 } 22427 } else { 22428 /* tcp->tcp_family == AF_INET6) */ 22429 v6addr = tcp->tcp_remote_v6; 22430 if (tcp->tcp_ipversion == IPV6_VERSION) { 22431 /* 22432 * No flowinfo if tcp->tcp_ipversion is v4. 22433 * 22434 * flowinfo was already initialized to zero 22435 * where it was declared above, so only 22436 * set it if ipversion is v6. 22437 */ 22438 flowinfo = tcp->tcp_ip6h->ip6_vcf & 22439 ~IPV6_VERS_AND_FLOW_MASK; 22440 } 22441 } 22442 port = tcp->tcp_fport; 22443 break; 22444 default: 22445 mi_copy_done(q, mp, EPROTO); 22446 return; 22447 } 22448 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 22449 if (!mp1) 22450 return; 22451 22452 if (tcp->tcp_family == AF_INET) { 22453 sin_t *sin; 22454 22455 STRUCT_FSET(sb, len, (int)sizeof (sin_t)); 22456 sin = (sin_t *)mp1->b_rptr; 22457 mp1->b_wptr = (uchar_t *)&sin[1]; 22458 *sin = sin_null; 22459 sin->sin_family = AF_INET; 22460 sin->sin_addr.s_addr = v4addr; 22461 sin->sin_port = port; 22462 } else { 22463 /* tcp->tcp_family == AF_INET6 */ 22464 sin6_t *sin6; 22465 22466 STRUCT_FSET(sb, len, (int)sizeof (sin6_t)); 22467 sin6 = (sin6_t *)mp1->b_rptr; 22468 mp1->b_wptr = (uchar_t *)&sin6[1]; 22469 *sin6 = sin6_null; 22470 sin6->sin6_family = AF_INET6; 22471 sin6->sin6_flowinfo = flowinfo; 22472 sin6->sin6_addr = v6addr; 22473 sin6->sin6_port = port; 22474 } 22475 /* Copy out the address */ 22476 mi_copyout(q, mp); 22477 } 22478 22479 /* 22480 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 22481 * messages. 22482 */ 22483 /* ARGSUSED */ 22484 static void 22485 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 22486 { 22487 conn_t *connp = (conn_t *)arg; 22488 tcp_t *tcp = connp->conn_tcp; 22489 queue_t *q = tcp->tcp_wq; 22490 struct iocblk *iocp; 22491 tcp_stack_t *tcps = tcp->tcp_tcps; 22492 22493 ASSERT(DB_TYPE(mp) == M_IOCTL); 22494 /* 22495 * Try and ASSERT the minimum possible references on the 22496 * conn early enough. Since we are executing on write side, 22497 * the connection is obviously not detached and that means 22498 * there is a ref each for TCP and IP. Since we are behind 22499 * the squeue, the minimum references needed are 3. If the 22500 * conn is in classifier hash list, there should be an 22501 * extra ref for that (we check both the possibilities). 22502 */ 22503 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22504 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22505 22506 iocp = (struct iocblk *)mp->b_rptr; 22507 switch (iocp->ioc_cmd) { 22508 case TCP_IOC_DEFAULT_Q: 22509 /* Wants to be the default wq. */ 22510 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 22511 iocp->ioc_error = EPERM; 22512 iocp->ioc_count = 0; 22513 mp->b_datap->db_type = M_IOCACK; 22514 qreply(q, mp); 22515 return; 22516 } 22517 tcp_def_q_set(tcp, mp); 22518 return; 22519 case _SIOCSOCKFALLBACK: 22520 /* 22521 * Either sockmod is about to be popped and the socket 22522 * would now be treated as a plain stream, or a module 22523 * is about to be pushed so we could no longer use read- 22524 * side synchronous streams for fused loopback tcp. 22525 * Drain any queued data and disable direct sockfs 22526 * interface from now on. 22527 */ 22528 if (!tcp->tcp_issocket) { 22529 DB_TYPE(mp) = M_IOCNAK; 22530 iocp->ioc_error = EINVAL; 22531 } else { 22532 #ifdef _ILP32 22533 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 22534 #else 22535 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 22536 #endif 22537 /* 22538 * Insert this socket into the acceptor hash. 22539 * We might need it for T_CONN_RES message 22540 */ 22541 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 22542 22543 if (tcp->tcp_fused) { 22544 /* 22545 * This is a fused loopback tcp; disable 22546 * read-side synchronous streams interface 22547 * and drain any queued data. It is okay 22548 * to do this for non-synchronous streams 22549 * fused tcp as well. 22550 */ 22551 tcp_fuse_disable_pair(tcp, B_FALSE); 22552 } 22553 tcp->tcp_issocket = B_FALSE; 22554 tcp->tcp_sodirect = NULL; 22555 TCP_STAT(tcps, tcp_sock_fallback); 22556 22557 DB_TYPE(mp) = M_IOCACK; 22558 iocp->ioc_error = 0; 22559 } 22560 iocp->ioc_count = 0; 22561 iocp->ioc_rval = 0; 22562 qreply(q, mp); 22563 return; 22564 } 22565 CALL_IP_WPUT(connp, q, mp); 22566 } 22567 22568 /* 22569 * This routine is called by tcp_wput() to handle all TPI requests. 22570 */ 22571 /* ARGSUSED */ 22572 static void 22573 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22574 { 22575 conn_t *connp = (conn_t *)arg; 22576 tcp_t *tcp = connp->conn_tcp; 22577 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22578 uchar_t *rptr; 22579 t_scalar_t type; 22580 int len; 22581 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 22582 22583 /* 22584 * Try and ASSERT the minimum possible references on the 22585 * conn early enough. Since we are executing on write side, 22586 * the connection is obviously not detached and that means 22587 * there is a ref each for TCP and IP. Since we are behind 22588 * the squeue, the minimum references needed are 3. If the 22589 * conn is in classifier hash list, there should be an 22590 * extra ref for that (we check both the possibilities). 22591 */ 22592 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22593 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22594 22595 rptr = mp->b_rptr; 22596 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22597 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22598 type = ((union T_primitives *)rptr)->type; 22599 if (type == T_EXDATA_REQ) { 22600 uint32_t msize = msgdsize(mp->b_cont); 22601 22602 len = msize - 1; 22603 if (len < 0) { 22604 freemsg(mp); 22605 return; 22606 } 22607 /* 22608 * Try to force urgent data out on the wire. 22609 * Even if we have unsent data this will 22610 * at least send the urgent flag. 22611 * XXX does not handle more flag correctly. 22612 */ 22613 len += tcp->tcp_unsent; 22614 len += tcp->tcp_snxt; 22615 tcp->tcp_urg = len; 22616 tcp->tcp_valid_bits |= TCP_URG_VALID; 22617 22618 /* Bypass tcp protocol for fused tcp loopback */ 22619 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 22620 return; 22621 } else if (type != T_DATA_REQ) { 22622 goto non_urgent_data; 22623 } 22624 /* TODO: options, flags, ... from user */ 22625 /* Set length to zero for reclamation below */ 22626 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22627 freeb(mp); 22628 return; 22629 } else { 22630 if (tcp->tcp_debug) { 22631 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22632 "tcp_wput_proto, dropping one..."); 22633 } 22634 freemsg(mp); 22635 return; 22636 } 22637 22638 non_urgent_data: 22639 22640 switch ((int)tprim->type) { 22641 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22642 /* 22643 * save the kssl_ent_t from the next block, and convert this 22644 * back to a normal bind_req. 22645 */ 22646 if (mp->b_cont != NULL) { 22647 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22648 22649 if (tcp->tcp_kssl_ent != NULL) { 22650 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22651 KSSL_NO_PROXY); 22652 tcp->tcp_kssl_ent = NULL; 22653 } 22654 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22655 sizeof (kssl_ent_t)); 22656 kssl_hold_ent(tcp->tcp_kssl_ent); 22657 freemsg(mp->b_cont); 22658 mp->b_cont = NULL; 22659 } 22660 tprim->type = T_BIND_REQ; 22661 22662 /* FALLTHROUGH */ 22663 case O_T_BIND_REQ: /* bind request */ 22664 case T_BIND_REQ: /* new semantics bind request */ 22665 tcp_bind(tcp, mp); 22666 break; 22667 case T_UNBIND_REQ: /* unbind request */ 22668 tcp_unbind(tcp, mp); 22669 break; 22670 case O_T_CONN_RES: /* old connection response XXX */ 22671 case T_CONN_RES: /* connection response */ 22672 tcp_accept(tcp, mp); 22673 break; 22674 case T_CONN_REQ: /* connection request */ 22675 tcp_connect(tcp, mp); 22676 break; 22677 case T_DISCON_REQ: /* disconnect request */ 22678 tcp_disconnect(tcp, mp); 22679 break; 22680 case T_CAPABILITY_REQ: 22681 tcp_capability_req(tcp, mp); /* capability request */ 22682 break; 22683 case T_INFO_REQ: /* information request */ 22684 tcp_info_req(tcp, mp); 22685 break; 22686 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22687 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, 22688 &tcp_opt_obj, B_TRUE); 22689 break; 22690 case T_OPTMGMT_REQ: 22691 /* 22692 * Note: no support for snmpcom_req() through new 22693 * T_OPTMGMT_REQ. See comments in ip.c 22694 */ 22695 /* Only IP is allowed to return meaningful value */ 22696 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22697 B_TRUE); 22698 break; 22699 22700 case T_UNITDATA_REQ: /* unitdata request */ 22701 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22702 break; 22703 case T_ORDREL_REQ: /* orderly release req */ 22704 freemsg(mp); 22705 22706 if (tcp->tcp_fused) 22707 tcp_unfuse(tcp); 22708 22709 if (tcp_xmit_end(tcp) != 0) { 22710 /* 22711 * We were crossing FINs and got a reset from 22712 * the other side. Just ignore it. 22713 */ 22714 if (tcp->tcp_debug) { 22715 (void) strlog(TCP_MOD_ID, 0, 1, 22716 SL_ERROR|SL_TRACE, 22717 "tcp_wput_proto, T_ORDREL_REQ out of " 22718 "state %s", 22719 tcp_display(tcp, NULL, 22720 DISP_ADDR_AND_PORT)); 22721 } 22722 } 22723 break; 22724 case T_ADDR_REQ: 22725 tcp_addr_req(tcp, mp); 22726 break; 22727 default: 22728 if (tcp->tcp_debug) { 22729 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22730 "tcp_wput_proto, bogus TPI msg, type %d", 22731 tprim->type); 22732 } 22733 /* 22734 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22735 * to recover. 22736 */ 22737 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22738 break; 22739 } 22740 } 22741 22742 /* 22743 * The TCP write service routine should never be called... 22744 */ 22745 /* ARGSUSED */ 22746 static void 22747 tcp_wsrv(queue_t *q) 22748 { 22749 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22750 22751 TCP_STAT(tcps, tcp_wsrv_called); 22752 } 22753 22754 /* Non overlapping byte exchanger */ 22755 static void 22756 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22757 { 22758 uchar_t uch; 22759 22760 while (len-- > 0) { 22761 uch = a[len]; 22762 a[len] = b[len]; 22763 b[len] = uch; 22764 } 22765 } 22766 22767 /* 22768 * Send out a control packet on the tcp connection specified. This routine 22769 * is typically called where we need a simple ACK or RST generated. 22770 */ 22771 static void 22772 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22773 { 22774 uchar_t *rptr; 22775 tcph_t *tcph; 22776 ipha_t *ipha = NULL; 22777 ip6_t *ip6h = NULL; 22778 uint32_t sum; 22779 int tcp_hdr_len; 22780 int tcp_ip_hdr_len; 22781 mblk_t *mp; 22782 tcp_stack_t *tcps = tcp->tcp_tcps; 22783 22784 /* 22785 * Save sum for use in source route later. 22786 */ 22787 ASSERT(tcp != NULL); 22788 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22789 tcp_hdr_len = tcp->tcp_hdr_len; 22790 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22791 22792 /* If a text string is passed in with the request, pass it to strlog. */ 22793 if (str != NULL && tcp->tcp_debug) { 22794 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22795 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22796 str, seq, ack, ctl); 22797 } 22798 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22799 BPRI_MED); 22800 if (mp == NULL) { 22801 return; 22802 } 22803 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22804 mp->b_rptr = rptr; 22805 mp->b_wptr = &rptr[tcp_hdr_len]; 22806 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22807 22808 if (tcp->tcp_ipversion == IPV4_VERSION) { 22809 ipha = (ipha_t *)rptr; 22810 ipha->ipha_length = htons(tcp_hdr_len); 22811 } else { 22812 ip6h = (ip6_t *)rptr; 22813 ASSERT(tcp != NULL); 22814 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22815 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22816 } 22817 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22818 tcph->th_flags[0] = (uint8_t)ctl; 22819 if (ctl & TH_RST) { 22820 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22821 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22822 /* 22823 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22824 */ 22825 if (tcp->tcp_snd_ts_ok && 22826 tcp->tcp_state > TCPS_SYN_SENT) { 22827 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22828 *(mp->b_wptr) = TCPOPT_EOL; 22829 if (tcp->tcp_ipversion == IPV4_VERSION) { 22830 ipha->ipha_length = htons(tcp_hdr_len - 22831 TCPOPT_REAL_TS_LEN); 22832 } else { 22833 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22834 TCPOPT_REAL_TS_LEN); 22835 } 22836 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22837 sum -= TCPOPT_REAL_TS_LEN; 22838 } 22839 } 22840 if (ctl & TH_ACK) { 22841 if (tcp->tcp_snd_ts_ok) { 22842 U32_TO_BE32(lbolt, 22843 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22844 U32_TO_BE32(tcp->tcp_ts_recent, 22845 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22846 } 22847 22848 /* Update the latest receive window size in TCP header. */ 22849 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22850 tcph->th_win); 22851 tcp->tcp_rack = ack; 22852 tcp->tcp_rack_cnt = 0; 22853 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22854 } 22855 BUMP_LOCAL(tcp->tcp_obsegs); 22856 U32_TO_BE32(seq, tcph->th_seq); 22857 U32_TO_BE32(ack, tcph->th_ack); 22858 /* 22859 * Include the adjustment for a source route if any. 22860 */ 22861 sum = (sum >> 16) + (sum & 0xFFFF); 22862 U16_TO_BE16(sum, tcph->th_sum); 22863 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22864 tcp_send_data(tcp, tcp->tcp_wq, mp); 22865 } 22866 22867 /* 22868 * If this routine returns B_TRUE, TCP can generate a RST in response 22869 * to a segment. If it returns B_FALSE, TCP should not respond. 22870 */ 22871 static boolean_t 22872 tcp_send_rst_chk(tcp_stack_t *tcps) 22873 { 22874 clock_t now; 22875 22876 /* 22877 * TCP needs to protect itself from generating too many RSTs. 22878 * This can be a DoS attack by sending us random segments 22879 * soliciting RSTs. 22880 * 22881 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22882 * in each 1 second interval. In this way, TCP still generate 22883 * RSTs in normal cases but when under attack, the impact is 22884 * limited. 22885 */ 22886 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22887 now = lbolt; 22888 /* lbolt can wrap around. */ 22889 if ((tcps->tcps_last_rst_intrvl > now) || 22890 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22891 1*SECONDS)) { 22892 tcps->tcps_last_rst_intrvl = now; 22893 tcps->tcps_rst_cnt = 1; 22894 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22895 return (B_FALSE); 22896 } 22897 } 22898 return (B_TRUE); 22899 } 22900 22901 /* 22902 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22903 */ 22904 static void 22905 tcp_ip_ire_mark_advice(tcp_t *tcp) 22906 { 22907 mblk_t *mp; 22908 ipic_t *ipic; 22909 22910 if (tcp->tcp_ipversion == IPV4_VERSION) { 22911 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22912 &ipic); 22913 } else { 22914 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22915 &ipic); 22916 } 22917 if (mp == NULL) 22918 return; 22919 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22920 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22921 } 22922 22923 /* 22924 * Return an IP advice ioctl mblk and set ipic to be the pointer 22925 * to the advice structure. 22926 */ 22927 static mblk_t * 22928 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22929 { 22930 struct iocblk *ioc; 22931 mblk_t *mp, *mp1; 22932 22933 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22934 if (mp == NULL) 22935 return (NULL); 22936 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22937 *ipic = (ipic_t *)mp->b_rptr; 22938 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22939 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22940 22941 bcopy(addr, *ipic + 1, addr_len); 22942 22943 (*ipic)->ipic_addr_length = addr_len; 22944 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22945 22946 mp1 = mkiocb(IP_IOCTL); 22947 if (mp1 == NULL) { 22948 freemsg(mp); 22949 return (NULL); 22950 } 22951 mp1->b_cont = mp; 22952 ioc = (struct iocblk *)mp1->b_rptr; 22953 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22954 22955 return (mp1); 22956 } 22957 22958 /* 22959 * Generate a reset based on an inbound packet, connp is set by caller 22960 * when RST is in response to an unexpected inbound packet for which 22961 * there is active tcp state in the system. 22962 * 22963 * IPSEC NOTE : Try to send the reply with the same protection as it came 22964 * in. We still have the ipsec_mp that the packet was attached to. Thus 22965 * the packet will go out at the same level of protection as it came in by 22966 * converting the IPSEC_IN to IPSEC_OUT. 22967 */ 22968 static void 22969 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22970 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22971 tcp_stack_t *tcps, conn_t *connp) 22972 { 22973 ipha_t *ipha = NULL; 22974 ip6_t *ip6h = NULL; 22975 ushort_t len; 22976 tcph_t *tcph; 22977 int i; 22978 mblk_t *ipsec_mp; 22979 boolean_t mctl_present; 22980 ipic_t *ipic; 22981 ipaddr_t v4addr; 22982 in6_addr_t v6addr; 22983 int addr_len; 22984 void *addr; 22985 queue_t *q = tcps->tcps_g_q; 22986 tcp_t *tcp; 22987 cred_t *cr; 22988 mblk_t *nmp; 22989 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22990 22991 if (tcps->tcps_g_q == NULL) { 22992 /* 22993 * For non-zero stackids the default queue isn't created 22994 * until the first open, thus there can be a need to send 22995 * a reset before then. But we can't do that, hence we just 22996 * drop the packet. Later during boot, when the default queue 22997 * has been setup, a retransmitted packet from the peer 22998 * will result in a reset. 22999 */ 23000 ASSERT(tcps->tcps_netstack->netstack_stackid != 23001 GLOBAL_NETSTACKID); 23002 freemsg(mp); 23003 return; 23004 } 23005 23006 if (connp != NULL) 23007 tcp = connp->conn_tcp; 23008 else 23009 tcp = Q_TO_TCP(q); 23010 23011 if (!tcp_send_rst_chk(tcps)) { 23012 tcps->tcps_rst_unsent++; 23013 freemsg(mp); 23014 return; 23015 } 23016 23017 if (mp->b_datap->db_type == M_CTL) { 23018 ipsec_mp = mp; 23019 mp = mp->b_cont; 23020 mctl_present = B_TRUE; 23021 } else { 23022 ipsec_mp = mp; 23023 mctl_present = B_FALSE; 23024 } 23025 23026 if (str && q && tcps->tcps_dbg) { 23027 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 23028 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 23029 "flags 0x%x", 23030 str, seq, ack, ctl); 23031 } 23032 if (mp->b_datap->db_ref != 1) { 23033 mblk_t *mp1 = copyb(mp); 23034 freemsg(mp); 23035 mp = mp1; 23036 if (!mp) { 23037 if (mctl_present) 23038 freeb(ipsec_mp); 23039 return; 23040 } else { 23041 if (mctl_present) { 23042 ipsec_mp->b_cont = mp; 23043 } else { 23044 ipsec_mp = mp; 23045 } 23046 } 23047 } else if (mp->b_cont) { 23048 freemsg(mp->b_cont); 23049 mp->b_cont = NULL; 23050 } 23051 /* 23052 * We skip reversing source route here. 23053 * (for now we replace all IP options with EOL) 23054 */ 23055 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23056 ipha = (ipha_t *)mp->b_rptr; 23057 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 23058 mp->b_rptr[i] = IPOPT_EOL; 23059 /* 23060 * Make sure that src address isn't flagrantly invalid. 23061 * Not all broadcast address checking for the src address 23062 * is possible, since we don't know the netmask of the src 23063 * addr. No check for destination address is done, since 23064 * IP will not pass up a packet with a broadcast dest 23065 * address to TCP. Similar checks are done below for IPv6. 23066 */ 23067 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 23068 CLASSD(ipha->ipha_src)) { 23069 freemsg(ipsec_mp); 23070 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 23071 return; 23072 } 23073 } else { 23074 ip6h = (ip6_t *)mp->b_rptr; 23075 23076 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 23077 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 23078 freemsg(ipsec_mp); 23079 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 23080 return; 23081 } 23082 23083 /* Remove any extension headers assuming partial overlay */ 23084 if (ip_hdr_len > IPV6_HDR_LEN) { 23085 uint8_t *to; 23086 23087 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 23088 ovbcopy(ip6h, to, IPV6_HDR_LEN); 23089 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 23090 ip_hdr_len = IPV6_HDR_LEN; 23091 ip6h = (ip6_t *)mp->b_rptr; 23092 ip6h->ip6_nxt = IPPROTO_TCP; 23093 } 23094 } 23095 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 23096 if (tcph->th_flags[0] & TH_RST) { 23097 freemsg(ipsec_mp); 23098 return; 23099 } 23100 tcph->th_offset_and_rsrvd[0] = (5 << 4); 23101 len = ip_hdr_len + sizeof (tcph_t); 23102 mp->b_wptr = &mp->b_rptr[len]; 23103 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23104 ipha->ipha_length = htons(len); 23105 /* Swap addresses */ 23106 v4addr = ipha->ipha_src; 23107 ipha->ipha_src = ipha->ipha_dst; 23108 ipha->ipha_dst = v4addr; 23109 ipha->ipha_ident = 0; 23110 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 23111 addr_len = IP_ADDR_LEN; 23112 addr = &v4addr; 23113 } else { 23114 /* No ip6i_t in this case */ 23115 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 23116 /* Swap addresses */ 23117 v6addr = ip6h->ip6_src; 23118 ip6h->ip6_src = ip6h->ip6_dst; 23119 ip6h->ip6_dst = v6addr; 23120 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 23121 addr_len = IPV6_ADDR_LEN; 23122 addr = &v6addr; 23123 } 23124 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 23125 U32_TO_BE32(ack, tcph->th_ack); 23126 U32_TO_BE32(seq, tcph->th_seq); 23127 U16_TO_BE16(0, tcph->th_win); 23128 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 23129 tcph->th_flags[0] = (uint8_t)ctl; 23130 if (ctl & TH_RST) { 23131 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 23132 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23133 } 23134 23135 /* IP trusts us to set up labels when required. */ 23136 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 23137 crgetlabel(cr) != NULL) { 23138 int err, adjust; 23139 23140 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 23141 err = tsol_check_label(cr, &mp, &adjust, 23142 tcp->tcp_connp->conn_mac_exempt, 23143 tcps->tcps_netstack->netstack_ip); 23144 else 23145 err = tsol_check_label_v6(cr, &mp, &adjust, 23146 tcp->tcp_connp->conn_mac_exempt, 23147 tcps->tcps_netstack->netstack_ip); 23148 if (mctl_present) 23149 ipsec_mp->b_cont = mp; 23150 else 23151 ipsec_mp = mp; 23152 if (err != 0) { 23153 freemsg(ipsec_mp); 23154 return; 23155 } 23156 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23157 ipha = (ipha_t *)mp->b_rptr; 23158 adjust += ntohs(ipha->ipha_length); 23159 ipha->ipha_length = htons(adjust); 23160 } else { 23161 ip6h = (ip6_t *)mp->b_rptr; 23162 } 23163 } 23164 23165 if (mctl_present) { 23166 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23167 23168 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23169 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 23170 return; 23171 } 23172 } 23173 if (zoneid == ALL_ZONES) 23174 zoneid = GLOBAL_ZONEID; 23175 23176 /* Add the zoneid so ip_output routes it properly */ 23177 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 23178 freemsg(ipsec_mp); 23179 return; 23180 } 23181 ipsec_mp = nmp; 23182 23183 /* 23184 * NOTE: one might consider tracing a TCP packet here, but 23185 * this function has no active TCP state and no tcp structure 23186 * that has a trace buffer. If we traced here, we would have 23187 * to keep a local trace buffer in tcp_record_trace(). 23188 * 23189 * TSol note: The mblk that contains the incoming packet was 23190 * reused by tcp_xmit_listener_reset, so it already contains 23191 * the right credentials and we don't need to call mblk_setcred. 23192 * Also the conn's cred is not right since it is associated 23193 * with tcps_g_q. 23194 */ 23195 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 23196 23197 /* 23198 * Tell IP to mark the IRE used for this destination temporary. 23199 * This way, we can limit our exposure to DoS attack because IP 23200 * creates an IRE for each destination. If there are too many, 23201 * the time to do any routing lookup will be extremely long. And 23202 * the lookup can be in interrupt context. 23203 * 23204 * Note that in normal circumstances, this marking should not 23205 * affect anything. It would be nice if only 1 message is 23206 * needed to inform IP that the IRE created for this RST should 23207 * not be added to the cache table. But there is currently 23208 * not such communication mechanism between TCP and IP. So 23209 * the best we can do now is to send the advice ioctl to IP 23210 * to mark the IRE temporary. 23211 */ 23212 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 23213 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 23214 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23215 } 23216 } 23217 23218 /* 23219 * Initiate closedown sequence on an active connection. (May be called as 23220 * writer.) Return value zero for OK return, non-zero for error return. 23221 */ 23222 static int 23223 tcp_xmit_end(tcp_t *tcp) 23224 { 23225 ipic_t *ipic; 23226 mblk_t *mp; 23227 tcp_stack_t *tcps = tcp->tcp_tcps; 23228 23229 if (tcp->tcp_state < TCPS_SYN_RCVD || 23230 tcp->tcp_state > TCPS_CLOSE_WAIT) { 23231 /* 23232 * Invalid state, only states TCPS_SYN_RCVD, 23233 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 23234 */ 23235 return (-1); 23236 } 23237 23238 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 23239 tcp->tcp_valid_bits |= TCP_FSS_VALID; 23240 /* 23241 * If there is nothing more unsent, send the FIN now. 23242 * Otherwise, it will go out with the last segment. 23243 */ 23244 if (tcp->tcp_unsent == 0) { 23245 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 23246 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 23247 23248 if (mp) { 23249 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 23250 tcp_send_data(tcp, tcp->tcp_wq, mp); 23251 } else { 23252 /* 23253 * Couldn't allocate msg. Pretend we got it out. 23254 * Wait for rexmit timeout. 23255 */ 23256 tcp->tcp_snxt = tcp->tcp_fss + 1; 23257 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23258 } 23259 23260 /* 23261 * If needed, update tcp_rexmit_snxt as tcp_snxt is 23262 * changed. 23263 */ 23264 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 23265 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 23266 } 23267 } else { 23268 /* 23269 * If tcp->tcp_cork is set, then the data will not get sent, 23270 * so we have to check that and unset it first. 23271 */ 23272 if (tcp->tcp_cork) 23273 tcp->tcp_cork = B_FALSE; 23274 tcp_wput_data(tcp, NULL, B_FALSE); 23275 } 23276 23277 /* 23278 * If TCP does not get enough samples of RTT or tcp_rtt_updates 23279 * is 0, don't update the cache. 23280 */ 23281 if (tcps->tcps_rtt_updates == 0 || 23282 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 23283 return (0); 23284 23285 /* 23286 * NOTE: should not update if source routes i.e. if tcp_remote if 23287 * different from the destination. 23288 */ 23289 if (tcp->tcp_ipversion == IPV4_VERSION) { 23290 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 23291 return (0); 23292 } 23293 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 23294 &ipic); 23295 } else { 23296 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 23297 &tcp->tcp_ip6h->ip6_dst))) { 23298 return (0); 23299 } 23300 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 23301 &ipic); 23302 } 23303 23304 /* Record route attributes in the IRE for use by future connections. */ 23305 if (mp == NULL) 23306 return (0); 23307 23308 /* 23309 * We do not have a good algorithm to update ssthresh at this time. 23310 * So don't do any update. 23311 */ 23312 ipic->ipic_rtt = tcp->tcp_rtt_sa; 23313 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 23314 23315 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23316 return (0); 23317 } 23318 23319 /* 23320 * Generate a "no listener here" RST in response to an "unknown" segment. 23321 * connp is set by caller when RST is in response to an unexpected 23322 * inbound packet for which there is active tcp state in the system. 23323 * Note that we are reusing the incoming mp to construct the outgoing RST. 23324 */ 23325 void 23326 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 23327 tcp_stack_t *tcps, conn_t *connp) 23328 { 23329 uchar_t *rptr; 23330 uint32_t seg_len; 23331 tcph_t *tcph; 23332 uint32_t seg_seq; 23333 uint32_t seg_ack; 23334 uint_t flags; 23335 mblk_t *ipsec_mp; 23336 ipha_t *ipha; 23337 ip6_t *ip6h; 23338 boolean_t mctl_present = B_FALSE; 23339 boolean_t check = B_TRUE; 23340 boolean_t policy_present; 23341 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 23342 23343 TCP_STAT(tcps, tcp_no_listener); 23344 23345 ipsec_mp = mp; 23346 23347 if (mp->b_datap->db_type == M_CTL) { 23348 ipsec_in_t *ii; 23349 23350 mctl_present = B_TRUE; 23351 mp = mp->b_cont; 23352 23353 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23354 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23355 if (ii->ipsec_in_dont_check) { 23356 check = B_FALSE; 23357 if (!ii->ipsec_in_secure) { 23358 freeb(ipsec_mp); 23359 mctl_present = B_FALSE; 23360 ipsec_mp = mp; 23361 } 23362 } 23363 } 23364 23365 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23366 policy_present = ipss->ipsec_inbound_v4_policy_present; 23367 ipha = (ipha_t *)mp->b_rptr; 23368 ip6h = NULL; 23369 } else { 23370 policy_present = ipss->ipsec_inbound_v6_policy_present; 23371 ipha = NULL; 23372 ip6h = (ip6_t *)mp->b_rptr; 23373 } 23374 23375 if (check && policy_present) { 23376 /* 23377 * The conn_t parameter is NULL because we already know 23378 * nobody's home. 23379 */ 23380 ipsec_mp = ipsec_check_global_policy( 23381 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 23382 tcps->tcps_netstack); 23383 if (ipsec_mp == NULL) 23384 return; 23385 } 23386 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 23387 DTRACE_PROBE2( 23388 tx__ip__log__error__nolistener__tcp, 23389 char *, "Could not reply with RST to mp(1)", 23390 mblk_t *, mp); 23391 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 23392 freemsg(ipsec_mp); 23393 return; 23394 } 23395 23396 rptr = mp->b_rptr; 23397 23398 tcph = (tcph_t *)&rptr[ip_hdr_len]; 23399 seg_seq = BE32_TO_U32(tcph->th_seq); 23400 seg_ack = BE32_TO_U32(tcph->th_ack); 23401 flags = tcph->th_flags[0]; 23402 23403 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 23404 if (flags & TH_RST) { 23405 freemsg(ipsec_mp); 23406 } else if (flags & TH_ACK) { 23407 tcp_xmit_early_reset("no tcp, reset", 23408 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 23409 connp); 23410 } else { 23411 if (flags & TH_SYN) { 23412 seg_len++; 23413 } else { 23414 /* 23415 * Here we violate the RFC. Note that a normal 23416 * TCP will never send a segment without the ACK 23417 * flag, except for RST or SYN segment. This 23418 * segment is neither. Just drop it on the 23419 * floor. 23420 */ 23421 freemsg(ipsec_mp); 23422 tcps->tcps_rst_unsent++; 23423 return; 23424 } 23425 23426 tcp_xmit_early_reset("no tcp, reset/ack", 23427 ipsec_mp, 0, seg_seq + seg_len, 23428 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 23429 } 23430 } 23431 23432 /* 23433 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 23434 * ip and tcp header ready to pass down to IP. If the mp passed in is 23435 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 23436 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 23437 * otherwise it will dup partial mblks.) 23438 * Otherwise, an appropriate ACK packet will be generated. This 23439 * routine is not usually called to send new data for the first time. It 23440 * is mostly called out of the timer for retransmits, and to generate ACKs. 23441 * 23442 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 23443 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 23444 * of the original mblk chain will be returned in *offset and *end_mp. 23445 */ 23446 mblk_t * 23447 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 23448 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 23449 boolean_t rexmit) 23450 { 23451 int data_length; 23452 int32_t off = 0; 23453 uint_t flags; 23454 mblk_t *mp1; 23455 mblk_t *mp2; 23456 uchar_t *rptr; 23457 tcph_t *tcph; 23458 int32_t num_sack_blk = 0; 23459 int32_t sack_opt_len = 0; 23460 tcp_stack_t *tcps = tcp->tcp_tcps; 23461 23462 /* Allocate for our maximum TCP header + link-level */ 23463 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 23464 tcps->tcps_wroff_xtra, BPRI_MED); 23465 if (!mp1) 23466 return (NULL); 23467 data_length = 0; 23468 23469 /* 23470 * Note that tcp_mss has been adjusted to take into account the 23471 * timestamp option if applicable. Because SACK options do not 23472 * appear in every TCP segments and they are of variable lengths, 23473 * they cannot be included in tcp_mss. Thus we need to calculate 23474 * the actual segment length when we need to send a segment which 23475 * includes SACK options. 23476 */ 23477 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23478 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23479 tcp->tcp_num_sack_blk); 23480 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23481 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23482 if (max_to_send + sack_opt_len > tcp->tcp_mss) 23483 max_to_send -= sack_opt_len; 23484 } 23485 23486 if (offset != NULL) { 23487 off = *offset; 23488 /* We use offset as an indicator that end_mp is not NULL. */ 23489 *end_mp = NULL; 23490 } 23491 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 23492 /* This could be faster with cooperation from downstream */ 23493 if (mp2 != mp1 && !sendall && 23494 data_length + (int)(mp->b_wptr - mp->b_rptr) > 23495 max_to_send) 23496 /* 23497 * Don't send the next mblk since the whole mblk 23498 * does not fit. 23499 */ 23500 break; 23501 mp2->b_cont = dupb(mp); 23502 mp2 = mp2->b_cont; 23503 if (!mp2) { 23504 freemsg(mp1); 23505 return (NULL); 23506 } 23507 mp2->b_rptr += off; 23508 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 23509 (uintptr_t)INT_MAX); 23510 23511 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 23512 if (data_length > max_to_send) { 23513 mp2->b_wptr -= data_length - max_to_send; 23514 data_length = max_to_send; 23515 off = mp2->b_wptr - mp->b_rptr; 23516 break; 23517 } else { 23518 off = 0; 23519 } 23520 } 23521 if (offset != NULL) { 23522 *offset = off; 23523 *end_mp = mp; 23524 } 23525 if (seg_len != NULL) { 23526 *seg_len = data_length; 23527 } 23528 23529 /* Update the latest receive window size in TCP header. */ 23530 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23531 tcp->tcp_tcph->th_win); 23532 23533 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23534 mp1->b_rptr = rptr; 23535 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 23536 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23537 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23538 U32_TO_ABE32(seq, tcph->th_seq); 23539 23540 /* 23541 * Use tcp_unsent to determine if the PUSH bit should be used assumes 23542 * that this function was called from tcp_wput_data. Thus, when called 23543 * to retransmit data the setting of the PUSH bit may appear some 23544 * what random in that it might get set when it should not. This 23545 * should not pose any performance issues. 23546 */ 23547 if (data_length != 0 && (tcp->tcp_unsent == 0 || 23548 tcp->tcp_unsent == data_length)) { 23549 flags = TH_ACK | TH_PUSH; 23550 } else { 23551 flags = TH_ACK; 23552 } 23553 23554 if (tcp->tcp_ecn_ok) { 23555 if (tcp->tcp_ecn_echo_on) 23556 flags |= TH_ECE; 23557 23558 /* 23559 * Only set ECT bit and ECN_CWR if a segment contains new data. 23560 * There is no TCP flow control for non-data segments, and 23561 * only data segment is transmitted reliably. 23562 */ 23563 if (data_length > 0 && !rexmit) { 23564 SET_ECT(tcp, rptr); 23565 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23566 flags |= TH_CWR; 23567 tcp->tcp_ecn_cwr_sent = B_TRUE; 23568 } 23569 } 23570 } 23571 23572 if (tcp->tcp_valid_bits) { 23573 uint32_t u1; 23574 23575 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23576 seq == tcp->tcp_iss) { 23577 uchar_t *wptr; 23578 23579 /* 23580 * If TCP_ISS_VALID and the seq number is tcp_iss, 23581 * TCP can only be in SYN-SENT, SYN-RCVD or 23582 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23583 * our SYN is not ack'ed but the app closes this 23584 * TCP connection. 23585 */ 23586 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23587 tcp->tcp_state == TCPS_SYN_RCVD || 23588 tcp->tcp_state == TCPS_FIN_WAIT_1); 23589 23590 /* 23591 * Tack on the MSS option. It is always needed 23592 * for both active and passive open. 23593 * 23594 * MSS option value should be interface MTU - MIN 23595 * TCP/IP header according to RFC 793 as it means 23596 * the maximum segment size TCP can receive. But 23597 * to get around some broken middle boxes/end hosts 23598 * out there, we allow the option value to be the 23599 * same as the MSS option size on the peer side. 23600 * In this way, the other side will not send 23601 * anything larger than they can receive. 23602 * 23603 * Note that for SYN_SENT state, the ndd param 23604 * tcp_use_smss_as_mss_opt has no effect as we 23605 * don't know the peer's MSS option value. So 23606 * the only case we need to take care of is in 23607 * SYN_RCVD state, which is done later. 23608 */ 23609 wptr = mp1->b_wptr; 23610 wptr[0] = TCPOPT_MAXSEG; 23611 wptr[1] = TCPOPT_MAXSEG_LEN; 23612 wptr += 2; 23613 u1 = tcp->tcp_if_mtu - 23614 (tcp->tcp_ipversion == IPV4_VERSION ? 23615 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23616 TCP_MIN_HEADER_LENGTH; 23617 U16_TO_BE16(u1, wptr); 23618 mp1->b_wptr = wptr + 2; 23619 /* Update the offset to cover the additional word */ 23620 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23621 23622 /* 23623 * Note that the following way of filling in 23624 * TCP options are not optimal. Some NOPs can 23625 * be saved. But there is no need at this time 23626 * to optimize it. When it is needed, we will 23627 * do it. 23628 */ 23629 switch (tcp->tcp_state) { 23630 case TCPS_SYN_SENT: 23631 flags = TH_SYN; 23632 23633 if (tcp->tcp_snd_ts_ok) { 23634 uint32_t llbolt = (uint32_t)lbolt; 23635 23636 wptr = mp1->b_wptr; 23637 wptr[0] = TCPOPT_NOP; 23638 wptr[1] = TCPOPT_NOP; 23639 wptr[2] = TCPOPT_TSTAMP; 23640 wptr[3] = TCPOPT_TSTAMP_LEN; 23641 wptr += 4; 23642 U32_TO_BE32(llbolt, wptr); 23643 wptr += 4; 23644 ASSERT(tcp->tcp_ts_recent == 0); 23645 U32_TO_BE32(0L, wptr); 23646 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23647 tcph->th_offset_and_rsrvd[0] += 23648 (3 << 4); 23649 } 23650 23651 /* 23652 * Set up all the bits to tell other side 23653 * we are ECN capable. 23654 */ 23655 if (tcp->tcp_ecn_ok) { 23656 flags |= (TH_ECE | TH_CWR); 23657 } 23658 break; 23659 case TCPS_SYN_RCVD: 23660 flags |= TH_SYN; 23661 23662 /* 23663 * Reset the MSS option value to be SMSS 23664 * We should probably add back the bytes 23665 * for timestamp option and IPsec. We 23666 * don't do that as this is a workaround 23667 * for broken middle boxes/end hosts, it 23668 * is better for us to be more cautious. 23669 * They may not take these things into 23670 * account in their SMSS calculation. Thus 23671 * the peer's calculated SMSS may be smaller 23672 * than what it can be. This should be OK. 23673 */ 23674 if (tcps->tcps_use_smss_as_mss_opt) { 23675 u1 = tcp->tcp_mss; 23676 U16_TO_BE16(u1, wptr); 23677 } 23678 23679 /* 23680 * If the other side is ECN capable, reply 23681 * that we are also ECN capable. 23682 */ 23683 if (tcp->tcp_ecn_ok) 23684 flags |= TH_ECE; 23685 break; 23686 default: 23687 /* 23688 * The above ASSERT() makes sure that this 23689 * must be FIN-WAIT-1 state. Our SYN has 23690 * not been ack'ed so retransmit it. 23691 */ 23692 flags |= TH_SYN; 23693 break; 23694 } 23695 23696 if (tcp->tcp_snd_ws_ok) { 23697 wptr = mp1->b_wptr; 23698 wptr[0] = TCPOPT_NOP; 23699 wptr[1] = TCPOPT_WSCALE; 23700 wptr[2] = TCPOPT_WS_LEN; 23701 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23702 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23703 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23704 } 23705 23706 if (tcp->tcp_snd_sack_ok) { 23707 wptr = mp1->b_wptr; 23708 wptr[0] = TCPOPT_NOP; 23709 wptr[1] = TCPOPT_NOP; 23710 wptr[2] = TCPOPT_SACK_PERMITTED; 23711 wptr[3] = TCPOPT_SACK_OK_LEN; 23712 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23713 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23714 } 23715 23716 /* allocb() of adequate mblk assures space */ 23717 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23718 (uintptr_t)INT_MAX); 23719 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23720 /* 23721 * Get IP set to checksum on our behalf 23722 * Include the adjustment for a source route if any. 23723 */ 23724 u1 += tcp->tcp_sum; 23725 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23726 U16_TO_BE16(u1, tcph->th_sum); 23727 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23728 } 23729 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23730 (seq + data_length) == tcp->tcp_fss) { 23731 if (!tcp->tcp_fin_acked) { 23732 flags |= TH_FIN; 23733 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23734 } 23735 if (!tcp->tcp_fin_sent) { 23736 tcp->tcp_fin_sent = B_TRUE; 23737 switch (tcp->tcp_state) { 23738 case TCPS_SYN_RCVD: 23739 case TCPS_ESTABLISHED: 23740 tcp->tcp_state = TCPS_FIN_WAIT_1; 23741 break; 23742 case TCPS_CLOSE_WAIT: 23743 tcp->tcp_state = TCPS_LAST_ACK; 23744 break; 23745 } 23746 if (tcp->tcp_suna == tcp->tcp_snxt) 23747 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23748 tcp->tcp_snxt = tcp->tcp_fss + 1; 23749 } 23750 } 23751 /* 23752 * Note the trick here. u1 is unsigned. When tcp_urg 23753 * is smaller than seq, u1 will become a very huge value. 23754 * So the comparison will fail. Also note that tcp_urp 23755 * should be positive, see RFC 793 page 17. 23756 */ 23757 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23758 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23759 u1 < (uint32_t)(64 * 1024)) { 23760 flags |= TH_URG; 23761 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23762 U32_TO_ABE16(u1, tcph->th_urp); 23763 } 23764 } 23765 tcph->th_flags[0] = (uchar_t)flags; 23766 tcp->tcp_rack = tcp->tcp_rnxt; 23767 tcp->tcp_rack_cnt = 0; 23768 23769 if (tcp->tcp_snd_ts_ok) { 23770 if (tcp->tcp_state != TCPS_SYN_SENT) { 23771 uint32_t llbolt = (uint32_t)lbolt; 23772 23773 U32_TO_BE32(llbolt, 23774 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23775 U32_TO_BE32(tcp->tcp_ts_recent, 23776 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23777 } 23778 } 23779 23780 if (num_sack_blk > 0) { 23781 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23782 sack_blk_t *tmp; 23783 int32_t i; 23784 23785 wptr[0] = TCPOPT_NOP; 23786 wptr[1] = TCPOPT_NOP; 23787 wptr[2] = TCPOPT_SACK; 23788 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23789 sizeof (sack_blk_t); 23790 wptr += TCPOPT_REAL_SACK_LEN; 23791 23792 tmp = tcp->tcp_sack_list; 23793 for (i = 0; i < num_sack_blk; i++) { 23794 U32_TO_BE32(tmp[i].begin, wptr); 23795 wptr += sizeof (tcp_seq); 23796 U32_TO_BE32(tmp[i].end, wptr); 23797 wptr += sizeof (tcp_seq); 23798 } 23799 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23800 } 23801 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23802 data_length += (int)(mp1->b_wptr - rptr); 23803 if (tcp->tcp_ipversion == IPV4_VERSION) { 23804 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23805 } else { 23806 ip6_t *ip6 = (ip6_t *)(rptr + 23807 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23808 sizeof (ip6i_t) : 0)); 23809 23810 ip6->ip6_plen = htons(data_length - 23811 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23812 } 23813 23814 /* 23815 * Prime pump for IP 23816 * Include the adjustment for a source route if any. 23817 */ 23818 data_length -= tcp->tcp_ip_hdr_len; 23819 data_length += tcp->tcp_sum; 23820 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23821 U16_TO_ABE16(data_length, tcph->th_sum); 23822 if (tcp->tcp_ip_forward_progress) { 23823 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23824 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23825 tcp->tcp_ip_forward_progress = B_FALSE; 23826 } 23827 return (mp1); 23828 } 23829 23830 /* This function handles the push timeout. */ 23831 void 23832 tcp_push_timer(void *arg) 23833 { 23834 conn_t *connp = (conn_t *)arg; 23835 tcp_t *tcp = connp->conn_tcp; 23836 tcp_stack_t *tcps = tcp->tcp_tcps; 23837 uint_t flags; 23838 sodirect_t *sodp; 23839 23840 TCP_DBGSTAT(tcps, tcp_push_timer_cnt); 23841 23842 ASSERT(tcp->tcp_listener == NULL); 23843 23844 /* 23845 * We need to plug synchronous streams during our drain to prevent 23846 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23847 */ 23848 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23849 tcp->tcp_push_tid = 0; 23850 23851 SOD_PTR_ENTER(tcp, sodp); 23852 if (sodp != NULL) { 23853 flags = tcp_rcv_sod_wakeup(tcp, sodp); 23854 /* sod_wakeup() does the mutex_exit() */ 23855 } else if (tcp->tcp_rcv_list != NULL) { 23856 flags = tcp_rcv_drain(tcp->tcp_rq, tcp); 23857 } 23858 if (flags == TH_ACK_NEEDED) 23859 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23860 23861 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23862 } 23863 23864 /* 23865 * This function handles delayed ACK timeout. 23866 */ 23867 static void 23868 tcp_ack_timer(void *arg) 23869 { 23870 conn_t *connp = (conn_t *)arg; 23871 tcp_t *tcp = connp->conn_tcp; 23872 mblk_t *mp; 23873 tcp_stack_t *tcps = tcp->tcp_tcps; 23874 23875 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23876 23877 tcp->tcp_ack_tid = 0; 23878 23879 if (tcp->tcp_fused) 23880 return; 23881 23882 /* 23883 * Do not send ACK if there is no outstanding unack'ed data. 23884 */ 23885 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23886 return; 23887 } 23888 23889 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23890 /* 23891 * Make sure we don't allow deferred ACKs to result in 23892 * timer-based ACKing. If we have held off an ACK 23893 * when there was more than an mss here, and the timer 23894 * goes off, we have to worry about the possibility 23895 * that the sender isn't doing slow-start, or is out 23896 * of step with us for some other reason. We fall 23897 * permanently back in the direction of 23898 * ACK-every-other-packet as suggested in RFC 1122. 23899 */ 23900 if (tcp->tcp_rack_abs_max > 2) 23901 tcp->tcp_rack_abs_max--; 23902 tcp->tcp_rack_cur_max = 2; 23903 } 23904 mp = tcp_ack_mp(tcp); 23905 23906 if (mp != NULL) { 23907 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 23908 BUMP_LOCAL(tcp->tcp_obsegs); 23909 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23910 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23911 tcp_send_data(tcp, tcp->tcp_wq, mp); 23912 } 23913 } 23914 23915 23916 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23917 static mblk_t * 23918 tcp_ack_mp(tcp_t *tcp) 23919 { 23920 uint32_t seq_no; 23921 tcp_stack_t *tcps = tcp->tcp_tcps; 23922 23923 /* 23924 * There are a few cases to be considered while setting the sequence no. 23925 * Essentially, we can come here while processing an unacceptable pkt 23926 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23927 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23928 * If we are here for a zero window probe, stick with suna. In all 23929 * other cases, we check if suna + swnd encompasses snxt and set 23930 * the sequence number to snxt, if so. If snxt falls outside the 23931 * window (the receiver probably shrunk its window), we will go with 23932 * suna + swnd, otherwise the sequence no will be unacceptable to the 23933 * receiver. 23934 */ 23935 if (tcp->tcp_zero_win_probe) { 23936 seq_no = tcp->tcp_suna; 23937 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23938 ASSERT(tcp->tcp_swnd == 0); 23939 seq_no = tcp->tcp_snxt; 23940 } else { 23941 seq_no = SEQ_GT(tcp->tcp_snxt, 23942 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23943 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23944 } 23945 23946 if (tcp->tcp_valid_bits) { 23947 /* 23948 * For the complex case where we have to send some 23949 * controls (FIN or SYN), let tcp_xmit_mp do it. 23950 */ 23951 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23952 NULL, B_FALSE)); 23953 } else { 23954 /* Generate a simple ACK */ 23955 int data_length; 23956 uchar_t *rptr; 23957 tcph_t *tcph; 23958 mblk_t *mp1; 23959 int32_t tcp_hdr_len; 23960 int32_t tcp_tcp_hdr_len; 23961 int32_t num_sack_blk = 0; 23962 int32_t sack_opt_len; 23963 23964 /* 23965 * Allocate space for TCP + IP headers 23966 * and link-level header 23967 */ 23968 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23969 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23970 tcp->tcp_num_sack_blk); 23971 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23972 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23973 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23974 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23975 } else { 23976 tcp_hdr_len = tcp->tcp_hdr_len; 23977 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23978 } 23979 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23980 if (!mp1) 23981 return (NULL); 23982 23983 /* Update the latest receive window size in TCP header. */ 23984 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23985 tcp->tcp_tcph->th_win); 23986 /* copy in prototype TCP + IP header */ 23987 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23988 mp1->b_rptr = rptr; 23989 mp1->b_wptr = rptr + tcp_hdr_len; 23990 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23991 23992 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23993 23994 /* Set the TCP sequence number. */ 23995 U32_TO_ABE32(seq_no, tcph->th_seq); 23996 23997 /* Set up the TCP flag field. */ 23998 tcph->th_flags[0] = (uchar_t)TH_ACK; 23999 if (tcp->tcp_ecn_echo_on) 24000 tcph->th_flags[0] |= TH_ECE; 24001 24002 tcp->tcp_rack = tcp->tcp_rnxt; 24003 tcp->tcp_rack_cnt = 0; 24004 24005 /* fill in timestamp option if in use */ 24006 if (tcp->tcp_snd_ts_ok) { 24007 uint32_t llbolt = (uint32_t)lbolt; 24008 24009 U32_TO_BE32(llbolt, 24010 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 24011 U32_TO_BE32(tcp->tcp_ts_recent, 24012 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 24013 } 24014 24015 /* Fill in SACK options */ 24016 if (num_sack_blk > 0) { 24017 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 24018 sack_blk_t *tmp; 24019 int32_t i; 24020 24021 wptr[0] = TCPOPT_NOP; 24022 wptr[1] = TCPOPT_NOP; 24023 wptr[2] = TCPOPT_SACK; 24024 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 24025 sizeof (sack_blk_t); 24026 wptr += TCPOPT_REAL_SACK_LEN; 24027 24028 tmp = tcp->tcp_sack_list; 24029 for (i = 0; i < num_sack_blk; i++) { 24030 U32_TO_BE32(tmp[i].begin, wptr); 24031 wptr += sizeof (tcp_seq); 24032 U32_TO_BE32(tmp[i].end, wptr); 24033 wptr += sizeof (tcp_seq); 24034 } 24035 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 24036 << 4); 24037 } 24038 24039 if (tcp->tcp_ipversion == IPV4_VERSION) { 24040 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 24041 } else { 24042 /* Check for ip6i_t header in sticky hdrs */ 24043 ip6_t *ip6 = (ip6_t *)(rptr + 24044 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 24045 sizeof (ip6i_t) : 0)); 24046 24047 ip6->ip6_plen = htons(tcp_hdr_len - 24048 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 24049 } 24050 24051 /* 24052 * Prime pump for checksum calculation in IP. Include the 24053 * adjustment for a source route if any. 24054 */ 24055 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 24056 data_length = (data_length >> 16) + (data_length & 0xFFFF); 24057 U16_TO_ABE16(data_length, tcph->th_sum); 24058 24059 if (tcp->tcp_ip_forward_progress) { 24060 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 24061 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 24062 tcp->tcp_ip_forward_progress = B_FALSE; 24063 } 24064 return (mp1); 24065 } 24066 } 24067 24068 /* 24069 * To create a temporary tcp structure for inserting into bind hash list. 24070 * The parameter is assumed to be in network byte order, ready for use. 24071 */ 24072 /* ARGSUSED */ 24073 static tcp_t * 24074 tcp_alloc_temp_tcp(in_port_t port, tcp_stack_t *tcps) 24075 { 24076 conn_t *connp; 24077 tcp_t *tcp; 24078 24079 connp = ipcl_conn_create(IPCL_TCPCONN, KM_SLEEP, tcps->tcps_netstack); 24080 if (connp == NULL) 24081 return (NULL); 24082 24083 tcp = connp->conn_tcp; 24084 tcp->tcp_tcps = tcps; 24085 TCPS_REFHOLD(tcps); 24086 24087 /* 24088 * Only initialize the necessary info in those structures. Note 24089 * that since INADDR_ANY is all 0, we do not need to set 24090 * tcp_bound_source to INADDR_ANY here. 24091 */ 24092 tcp->tcp_state = TCPS_BOUND; 24093 tcp->tcp_lport = port; 24094 tcp->tcp_exclbind = 1; 24095 tcp->tcp_reserved_port = 1; 24096 24097 /* Just for place holding... */ 24098 tcp->tcp_ipversion = IPV4_VERSION; 24099 24100 return (tcp); 24101 } 24102 24103 /* 24104 * To remove a port range specified by lo_port and hi_port from the 24105 * reserved port ranges. This is one of the three public functions of 24106 * the reserved port interface. Note that a port range has to be removed 24107 * as a whole. Ports in a range cannot be removed individually. 24108 * 24109 * Params: 24110 * in_port_t lo_port: the beginning port of the reserved port range to 24111 * be deleted. 24112 * in_port_t hi_port: the ending port of the reserved port range to 24113 * be deleted. 24114 * 24115 * Return: 24116 * B_TRUE if the deletion is successful, B_FALSE otherwise. 24117 * 24118 * Assumes that nca is only for zoneid=0 24119 */ 24120 boolean_t 24121 tcp_reserved_port_del(in_port_t lo_port, in_port_t hi_port) 24122 { 24123 int i, j; 24124 int size; 24125 tcp_t **temp_tcp_array; 24126 tcp_t *tcp; 24127 tcp_stack_t *tcps; 24128 24129 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_tcp; 24130 ASSERT(tcps != NULL); 24131 24132 rw_enter(&tcps->tcps_reserved_port_lock, RW_WRITER); 24133 24134 /* First make sure that the port ranage is indeed reserved. */ 24135 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 24136 if (tcps->tcps_reserved_port[i].lo_port == lo_port) { 24137 hi_port = tcps->tcps_reserved_port[i].hi_port; 24138 temp_tcp_array = 24139 tcps->tcps_reserved_port[i].temp_tcp_array; 24140 break; 24141 } 24142 } 24143 if (i == tcps->tcps_reserved_port_array_size) { 24144 rw_exit(&tcps->tcps_reserved_port_lock); 24145 netstack_rele(tcps->tcps_netstack); 24146 return (B_FALSE); 24147 } 24148 24149 /* 24150 * Remove the range from the array. This simple loop is possible 24151 * because port ranges are inserted in ascending order. 24152 */ 24153 for (j = i; j < tcps->tcps_reserved_port_array_size - 1; j++) { 24154 tcps->tcps_reserved_port[j].lo_port = 24155 tcps->tcps_reserved_port[j+1].lo_port; 24156 tcps->tcps_reserved_port[j].hi_port = 24157 tcps->tcps_reserved_port[j+1].hi_port; 24158 tcps->tcps_reserved_port[j].temp_tcp_array = 24159 tcps->tcps_reserved_port[j+1].temp_tcp_array; 24160 } 24161 24162 /* Remove all the temporary tcp structures. */ 24163 size = hi_port - lo_port + 1; 24164 while (size > 0) { 24165 tcp = temp_tcp_array[size - 1]; 24166 ASSERT(tcp != NULL); 24167 tcp_bind_hash_remove(tcp); 24168 CONN_DEC_REF(tcp->tcp_connp); 24169 size--; 24170 } 24171 kmem_free(temp_tcp_array, (hi_port - lo_port + 1) * sizeof (tcp_t *)); 24172 tcps->tcps_reserved_port_array_size--; 24173 rw_exit(&tcps->tcps_reserved_port_lock); 24174 netstack_rele(tcps->tcps_netstack); 24175 return (B_TRUE); 24176 } 24177 24178 /* 24179 * Macro to remove temporary tcp structure from the bind hash list. The 24180 * first parameter is the list of tcp to be removed. The second parameter 24181 * is the number of tcps in the array. 24182 */ 24183 #define TCP_TMP_TCP_REMOVE(tcp_array, num, tcps) \ 24184 { \ 24185 while ((num) > 0) { \ 24186 tcp_t *tcp = (tcp_array)[(num) - 1]; \ 24187 tf_t *tbf; \ 24188 tcp_t *tcpnext; \ 24189 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)]; \ 24190 mutex_enter(&tbf->tf_lock); \ 24191 tcpnext = tcp->tcp_bind_hash; \ 24192 if (tcpnext) { \ 24193 tcpnext->tcp_ptpbhn = \ 24194 tcp->tcp_ptpbhn; \ 24195 } \ 24196 *tcp->tcp_ptpbhn = tcpnext; \ 24197 mutex_exit(&tbf->tf_lock); \ 24198 kmem_free(tcp, sizeof (tcp_t)); \ 24199 (tcp_array)[(num) - 1] = NULL; \ 24200 (num)--; \ 24201 } \ 24202 } 24203 24204 /* 24205 * The public interface for other modules to call to reserve a port range 24206 * in TCP. The caller passes in how large a port range it wants. TCP 24207 * will try to find a range and return it via lo_port and hi_port. This is 24208 * used by NCA's nca_conn_init. 24209 * NCA can only be used in the global zone so this only affects the global 24210 * zone's ports. 24211 * 24212 * Params: 24213 * int size: the size of the port range to be reserved. 24214 * in_port_t *lo_port (referenced): returns the beginning port of the 24215 * reserved port range added. 24216 * in_port_t *hi_port (referenced): returns the ending port of the 24217 * reserved port range added. 24218 * 24219 * Return: 24220 * B_TRUE if the port reservation is successful, B_FALSE otherwise. 24221 * 24222 * Assumes that nca is only for zoneid=0 24223 */ 24224 boolean_t 24225 tcp_reserved_port_add(int size, in_port_t *lo_port, in_port_t *hi_port) 24226 { 24227 tcp_t *tcp; 24228 tcp_t *tmp_tcp; 24229 tcp_t **temp_tcp_array; 24230 tf_t *tbf; 24231 in_port_t net_port; 24232 in_port_t port; 24233 int32_t cur_size; 24234 int i, j; 24235 boolean_t used; 24236 tcp_rport_t tmp_ports[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 24237 zoneid_t zoneid = GLOBAL_ZONEID; 24238 tcp_stack_t *tcps; 24239 24240 /* Sanity check. */ 24241 if (size <= 0 || size > TCP_RESERVED_PORTS_RANGE_MAX) { 24242 return (B_FALSE); 24243 } 24244 24245 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_tcp; 24246 ASSERT(tcps != NULL); 24247 24248 rw_enter(&tcps->tcps_reserved_port_lock, RW_WRITER); 24249 if (tcps->tcps_reserved_port_array_size == 24250 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE) { 24251 rw_exit(&tcps->tcps_reserved_port_lock); 24252 netstack_rele(tcps->tcps_netstack); 24253 return (B_FALSE); 24254 } 24255 24256 /* 24257 * Find the starting port to try. Since the port ranges are ordered 24258 * in the reserved port array, we can do a simple search here. 24259 */ 24260 *lo_port = TCP_SMALLEST_RESERVED_PORT; 24261 *hi_port = TCP_LARGEST_RESERVED_PORT; 24262 for (i = 0; i < tcps->tcps_reserved_port_array_size; 24263 *lo_port = tcps->tcps_reserved_port[i].hi_port + 1, i++) { 24264 if (tcps->tcps_reserved_port[i].lo_port - *lo_port >= size) { 24265 *hi_port = tcps->tcps_reserved_port[i].lo_port - 1; 24266 break; 24267 } 24268 } 24269 /* No available port range. */ 24270 if (i == tcps->tcps_reserved_port_array_size && 24271 *hi_port - *lo_port < size) { 24272 rw_exit(&tcps->tcps_reserved_port_lock); 24273 netstack_rele(tcps->tcps_netstack); 24274 return (B_FALSE); 24275 } 24276 24277 temp_tcp_array = kmem_zalloc(size * sizeof (tcp_t *), KM_NOSLEEP); 24278 if (temp_tcp_array == NULL) { 24279 rw_exit(&tcps->tcps_reserved_port_lock); 24280 netstack_rele(tcps->tcps_netstack); 24281 return (B_FALSE); 24282 } 24283 24284 /* Go thru the port range to see if some ports are already bound. */ 24285 for (port = *lo_port, cur_size = 0; 24286 cur_size < size && port <= *hi_port; 24287 cur_size++, port++) { 24288 used = B_FALSE; 24289 net_port = htons(port); 24290 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(net_port)]; 24291 mutex_enter(&tbf->tf_lock); 24292 for (tcp = tbf->tf_tcp; tcp != NULL; 24293 tcp = tcp->tcp_bind_hash) { 24294 if (IPCL_ZONE_MATCH(tcp->tcp_connp, zoneid) && 24295 net_port == tcp->tcp_lport) { 24296 /* 24297 * A port is already bound. Search again 24298 * starting from port + 1. Release all 24299 * temporary tcps. 24300 */ 24301 mutex_exit(&tbf->tf_lock); 24302 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, 24303 tcps); 24304 *lo_port = port + 1; 24305 cur_size = -1; 24306 used = B_TRUE; 24307 break; 24308 } 24309 } 24310 if (!used) { 24311 if ((tmp_tcp = tcp_alloc_temp_tcp(net_port, tcps)) == 24312 NULL) { 24313 /* 24314 * Allocation failure. Just fail the request. 24315 * Need to remove all those temporary tcp 24316 * structures. 24317 */ 24318 mutex_exit(&tbf->tf_lock); 24319 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, 24320 tcps); 24321 rw_exit(&tcps->tcps_reserved_port_lock); 24322 kmem_free(temp_tcp_array, 24323 (hi_port - lo_port + 1) * 24324 sizeof (tcp_t *)); 24325 netstack_rele(tcps->tcps_netstack); 24326 return (B_FALSE); 24327 } 24328 temp_tcp_array[cur_size] = tmp_tcp; 24329 tcp_bind_hash_insert(tbf, tmp_tcp, B_TRUE); 24330 mutex_exit(&tbf->tf_lock); 24331 } 24332 } 24333 24334 /* 24335 * The current range is not large enough. We can actually do another 24336 * search if this search is done between 2 reserved port ranges. But 24337 * for first release, we just stop here and return saying that no port 24338 * range is available. 24339 */ 24340 if (cur_size < size) { 24341 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size, tcps); 24342 rw_exit(&tcps->tcps_reserved_port_lock); 24343 kmem_free(temp_tcp_array, size * sizeof (tcp_t *)); 24344 netstack_rele(tcps->tcps_netstack); 24345 return (B_FALSE); 24346 } 24347 *hi_port = port - 1; 24348 24349 /* 24350 * Insert range into array in ascending order. Since this function 24351 * must not be called often, we choose to use the simplest method. 24352 * The above array should not consume excessive stack space as 24353 * the size must be very small. If in future releases, we find 24354 * that we should provide more reserved port ranges, this function 24355 * has to be modified to be more efficient. 24356 */ 24357 if (tcps->tcps_reserved_port_array_size == 0) { 24358 tcps->tcps_reserved_port[0].lo_port = *lo_port; 24359 tcps->tcps_reserved_port[0].hi_port = *hi_port; 24360 tcps->tcps_reserved_port[0].temp_tcp_array = temp_tcp_array; 24361 } else { 24362 for (i = 0, j = 0; i < tcps->tcps_reserved_port_array_size; 24363 i++, j++) { 24364 if (*lo_port < tcps->tcps_reserved_port[i].lo_port && 24365 i == j) { 24366 tmp_ports[j].lo_port = *lo_port; 24367 tmp_ports[j].hi_port = *hi_port; 24368 tmp_ports[j].temp_tcp_array = temp_tcp_array; 24369 j++; 24370 } 24371 tmp_ports[j].lo_port = 24372 tcps->tcps_reserved_port[i].lo_port; 24373 tmp_ports[j].hi_port = 24374 tcps->tcps_reserved_port[i].hi_port; 24375 tmp_ports[j].temp_tcp_array = 24376 tcps->tcps_reserved_port[i].temp_tcp_array; 24377 } 24378 if (j == i) { 24379 tmp_ports[j].lo_port = *lo_port; 24380 tmp_ports[j].hi_port = *hi_port; 24381 tmp_ports[j].temp_tcp_array = temp_tcp_array; 24382 } 24383 bcopy(tmp_ports, tcps->tcps_reserved_port, sizeof (tmp_ports)); 24384 } 24385 tcps->tcps_reserved_port_array_size++; 24386 rw_exit(&tcps->tcps_reserved_port_lock); 24387 netstack_rele(tcps->tcps_netstack); 24388 return (B_TRUE); 24389 } 24390 24391 /* 24392 * Check to see if a port is in any reserved port range. 24393 * 24394 * Params: 24395 * in_port_t port: the port to be verified. 24396 * 24397 * Return: 24398 * B_TRUE is the port is inside a reserved port range, B_FALSE otherwise. 24399 */ 24400 boolean_t 24401 tcp_reserved_port_check(in_port_t port, tcp_stack_t *tcps) 24402 { 24403 int i; 24404 24405 rw_enter(&tcps->tcps_reserved_port_lock, RW_READER); 24406 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 24407 if (port >= tcps->tcps_reserved_port[i].lo_port || 24408 port <= tcps->tcps_reserved_port[i].hi_port) { 24409 rw_exit(&tcps->tcps_reserved_port_lock); 24410 return (B_TRUE); 24411 } 24412 } 24413 rw_exit(&tcps->tcps_reserved_port_lock); 24414 return (B_FALSE); 24415 } 24416 24417 /* 24418 * To list all reserved port ranges. This is the function to handle 24419 * ndd tcp_reserved_port_list. 24420 */ 24421 /* ARGSUSED */ 24422 static int 24423 tcp_reserved_port_list(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 24424 { 24425 int i; 24426 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24427 24428 rw_enter(&tcps->tcps_reserved_port_lock, RW_READER); 24429 if (tcps->tcps_reserved_port_array_size > 0) 24430 (void) mi_mpprintf(mp, "The following ports are reserved:"); 24431 else 24432 (void) mi_mpprintf(mp, "No port is reserved."); 24433 for (i = 0; i < tcps->tcps_reserved_port_array_size; i++) { 24434 (void) mi_mpprintf(mp, "%d-%d", 24435 tcps->tcps_reserved_port[i].lo_port, 24436 tcps->tcps_reserved_port[i].hi_port); 24437 } 24438 rw_exit(&tcps->tcps_reserved_port_lock); 24439 return (0); 24440 } 24441 24442 /* 24443 * Hash list insertion routine for tcp_t structures. 24444 * Inserts entries with the ones bound to a specific IP address first 24445 * followed by those bound to INADDR_ANY. 24446 */ 24447 static void 24448 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 24449 { 24450 tcp_t **tcpp; 24451 tcp_t *tcpnext; 24452 24453 if (tcp->tcp_ptpbhn != NULL) { 24454 ASSERT(!caller_holds_lock); 24455 tcp_bind_hash_remove(tcp); 24456 } 24457 tcpp = &tbf->tf_tcp; 24458 if (!caller_holds_lock) { 24459 mutex_enter(&tbf->tf_lock); 24460 } else { 24461 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 24462 } 24463 tcpnext = tcpp[0]; 24464 if (tcpnext) { 24465 /* 24466 * If the new tcp bound to the INADDR_ANY address 24467 * and the first one in the list is not bound to 24468 * INADDR_ANY we skip all entries until we find the 24469 * first one bound to INADDR_ANY. 24470 * This makes sure that applications binding to a 24471 * specific address get preference over those binding to 24472 * INADDR_ANY. 24473 */ 24474 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 24475 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 24476 while ((tcpnext = tcpp[0]) != NULL && 24477 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 24478 tcpp = &(tcpnext->tcp_bind_hash); 24479 if (tcpnext) 24480 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 24481 } else 24482 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 24483 } 24484 tcp->tcp_bind_hash = tcpnext; 24485 tcp->tcp_ptpbhn = tcpp; 24486 tcpp[0] = tcp; 24487 if (!caller_holds_lock) 24488 mutex_exit(&tbf->tf_lock); 24489 } 24490 24491 /* 24492 * Hash list removal routine for tcp_t structures. 24493 */ 24494 static void 24495 tcp_bind_hash_remove(tcp_t *tcp) 24496 { 24497 tcp_t *tcpnext; 24498 kmutex_t *lockp; 24499 tcp_stack_t *tcps = tcp->tcp_tcps; 24500 24501 if (tcp->tcp_ptpbhn == NULL) 24502 return; 24503 24504 /* 24505 * Extract the lock pointer in case there are concurrent 24506 * hash_remove's for this instance. 24507 */ 24508 ASSERT(tcp->tcp_lport != 0); 24509 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 24510 24511 ASSERT(lockp != NULL); 24512 mutex_enter(lockp); 24513 if (tcp->tcp_ptpbhn) { 24514 tcpnext = tcp->tcp_bind_hash; 24515 if (tcpnext) { 24516 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 24517 tcp->tcp_bind_hash = NULL; 24518 } 24519 *tcp->tcp_ptpbhn = tcpnext; 24520 tcp->tcp_ptpbhn = NULL; 24521 } 24522 mutex_exit(lockp); 24523 } 24524 24525 24526 /* 24527 * Hash list lookup routine for tcp_t structures. 24528 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 24529 */ 24530 static tcp_t * 24531 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 24532 { 24533 tf_t *tf; 24534 tcp_t *tcp; 24535 24536 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24537 mutex_enter(&tf->tf_lock); 24538 for (tcp = tf->tf_tcp; tcp != NULL; 24539 tcp = tcp->tcp_acceptor_hash) { 24540 if (tcp->tcp_acceptor_id == id) { 24541 CONN_INC_REF(tcp->tcp_connp); 24542 mutex_exit(&tf->tf_lock); 24543 return (tcp); 24544 } 24545 } 24546 mutex_exit(&tf->tf_lock); 24547 return (NULL); 24548 } 24549 24550 24551 /* 24552 * Hash list insertion routine for tcp_t structures. 24553 */ 24554 void 24555 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 24556 { 24557 tf_t *tf; 24558 tcp_t **tcpp; 24559 tcp_t *tcpnext; 24560 tcp_stack_t *tcps = tcp->tcp_tcps; 24561 24562 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24563 24564 if (tcp->tcp_ptpahn != NULL) 24565 tcp_acceptor_hash_remove(tcp); 24566 tcpp = &tf->tf_tcp; 24567 mutex_enter(&tf->tf_lock); 24568 tcpnext = tcpp[0]; 24569 if (tcpnext) 24570 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 24571 tcp->tcp_acceptor_hash = tcpnext; 24572 tcp->tcp_ptpahn = tcpp; 24573 tcpp[0] = tcp; 24574 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 24575 mutex_exit(&tf->tf_lock); 24576 } 24577 24578 /* 24579 * Hash list removal routine for tcp_t structures. 24580 */ 24581 static void 24582 tcp_acceptor_hash_remove(tcp_t *tcp) 24583 { 24584 tcp_t *tcpnext; 24585 kmutex_t *lockp; 24586 24587 /* 24588 * Extract the lock pointer in case there are concurrent 24589 * hash_remove's for this instance. 24590 */ 24591 lockp = tcp->tcp_acceptor_lockp; 24592 24593 if (tcp->tcp_ptpahn == NULL) 24594 return; 24595 24596 ASSERT(lockp != NULL); 24597 mutex_enter(lockp); 24598 if (tcp->tcp_ptpahn) { 24599 tcpnext = tcp->tcp_acceptor_hash; 24600 if (tcpnext) { 24601 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24602 tcp->tcp_acceptor_hash = NULL; 24603 } 24604 *tcp->tcp_ptpahn = tcpnext; 24605 tcp->tcp_ptpahn = NULL; 24606 } 24607 mutex_exit(lockp); 24608 tcp->tcp_acceptor_lockp = NULL; 24609 } 24610 24611 /* ARGSUSED */ 24612 static int 24613 tcp_host_param_setvalue(queue_t *q, mblk_t *mp, char *value, caddr_t cp, int af) 24614 { 24615 int error = 0; 24616 int retval; 24617 char *end; 24618 tcp_hsp_t *hsp; 24619 tcp_hsp_t *hspprev; 24620 ipaddr_t addr = 0; /* Address we're looking for */ 24621 in6_addr_t v6addr; /* Address we're looking for */ 24622 uint32_t hash; /* Hash of that address */ 24623 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24624 24625 /* 24626 * If the following variables are still zero after parsing the input 24627 * string, the user didn't specify them and we don't change them in 24628 * the HSP. 24629 */ 24630 24631 ipaddr_t mask = 0; /* Subnet mask */ 24632 in6_addr_t v6mask; 24633 long sendspace = 0; /* Send buffer size */ 24634 long recvspace = 0; /* Receive buffer size */ 24635 long timestamp = 0; /* Originate TCP TSTAMP option, 1 = yes */ 24636 boolean_t delete = B_FALSE; /* User asked to delete this HSP */ 24637 24638 rw_enter(&tcps->tcps_hsp_lock, RW_WRITER); 24639 24640 /* Parse and validate address */ 24641 if (af == AF_INET) { 24642 retval = inet_pton(af, value, &addr); 24643 if (retval == 1) 24644 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 24645 } else if (af == AF_INET6) { 24646 retval = inet_pton(af, value, &v6addr); 24647 } else { 24648 error = EINVAL; 24649 goto done; 24650 } 24651 if (retval == 0) { 24652 error = EINVAL; 24653 goto done; 24654 } 24655 24656 while ((*value) && *value != ' ') 24657 value++; 24658 24659 /* Parse individual keywords, set variables if found */ 24660 while (*value) { 24661 /* Skip leading blanks */ 24662 24663 while (*value == ' ' || *value == '\t') 24664 value++; 24665 24666 /* If at end of string, we're done */ 24667 24668 if (!*value) 24669 break; 24670 24671 /* We have a word, figure out what it is */ 24672 24673 if (strncmp("mask", value, 4) == 0) { 24674 value += 4; 24675 while (*value == ' ' || *value == '\t') 24676 value++; 24677 /* Parse subnet mask */ 24678 if (af == AF_INET) { 24679 retval = inet_pton(af, value, &mask); 24680 if (retval == 1) { 24681 V4MASK_TO_V6(mask, v6mask); 24682 } 24683 } else if (af == AF_INET6) { 24684 retval = inet_pton(af, value, &v6mask); 24685 } 24686 if (retval != 1) { 24687 error = EINVAL; 24688 goto done; 24689 } 24690 while ((*value) && *value != ' ') 24691 value++; 24692 } else if (strncmp("sendspace", value, 9) == 0) { 24693 value += 9; 24694 24695 if (ddi_strtol(value, &end, 0, &sendspace) != 0 || 24696 sendspace < TCP_XMIT_HIWATER || 24697 sendspace >= (1L<<30)) { 24698 error = EINVAL; 24699 goto done; 24700 } 24701 value = end; 24702 } else if (strncmp("recvspace", value, 9) == 0) { 24703 value += 9; 24704 24705 if (ddi_strtol(value, &end, 0, &recvspace) != 0 || 24706 recvspace < TCP_RECV_HIWATER || 24707 recvspace >= (1L<<30)) { 24708 error = EINVAL; 24709 goto done; 24710 } 24711 value = end; 24712 } else if (strncmp("timestamp", value, 9) == 0) { 24713 value += 9; 24714 24715 if (ddi_strtol(value, &end, 0, ×tamp) != 0 || 24716 timestamp < 0 || timestamp > 1) { 24717 error = EINVAL; 24718 goto done; 24719 } 24720 24721 /* 24722 * We increment timestamp so we know it's been set; 24723 * this is undone when we put it in the HSP 24724 */ 24725 timestamp++; 24726 value = end; 24727 } else if (strncmp("delete", value, 6) == 0) { 24728 value += 6; 24729 delete = B_TRUE; 24730 } else { 24731 error = EINVAL; 24732 goto done; 24733 } 24734 } 24735 24736 /* Hash address for lookup */ 24737 24738 hash = TCP_HSP_HASH(addr); 24739 24740 if (delete) { 24741 /* 24742 * Note that deletes don't return an error if the thing 24743 * we're trying to delete isn't there. 24744 */ 24745 if (tcps->tcps_hsp_hash == NULL) 24746 goto done; 24747 hsp = tcps->tcps_hsp_hash[hash]; 24748 24749 if (hsp) { 24750 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24751 &v6addr)) { 24752 tcps->tcps_hsp_hash[hash] = hsp->tcp_hsp_next; 24753 mi_free((char *)hsp); 24754 } else { 24755 hspprev = hsp; 24756 while ((hsp = hsp->tcp_hsp_next) != NULL) { 24757 if (IN6_ARE_ADDR_EQUAL( 24758 &hsp->tcp_hsp_addr_v6, &v6addr)) { 24759 hspprev->tcp_hsp_next = 24760 hsp->tcp_hsp_next; 24761 mi_free((char *)hsp); 24762 break; 24763 } 24764 hspprev = hsp; 24765 } 24766 } 24767 } 24768 } else { 24769 /* 24770 * We're adding/modifying an HSP. If we haven't already done 24771 * so, allocate the hash table. 24772 */ 24773 24774 if (!tcps->tcps_hsp_hash) { 24775 tcps->tcps_hsp_hash = (tcp_hsp_t **) 24776 mi_zalloc(sizeof (tcp_hsp_t *) * TCP_HSP_HASH_SIZE); 24777 if (!tcps->tcps_hsp_hash) { 24778 error = EINVAL; 24779 goto done; 24780 } 24781 } 24782 24783 /* Get head of hash chain */ 24784 24785 hsp = tcps->tcps_hsp_hash[hash]; 24786 24787 /* Try to find pre-existing hsp on hash chain */ 24788 /* Doesn't handle CIDR prefixes. */ 24789 while (hsp) { 24790 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, &v6addr)) 24791 break; 24792 hsp = hsp->tcp_hsp_next; 24793 } 24794 24795 /* 24796 * If we didn't, create one with default values and put it 24797 * at head of hash chain 24798 */ 24799 24800 if (!hsp) { 24801 hsp = (tcp_hsp_t *)mi_zalloc(sizeof (tcp_hsp_t)); 24802 if (!hsp) { 24803 error = EINVAL; 24804 goto done; 24805 } 24806 hsp->tcp_hsp_next = tcps->tcps_hsp_hash[hash]; 24807 tcps->tcps_hsp_hash[hash] = hsp; 24808 } 24809 24810 /* Set values that the user asked us to change */ 24811 24812 hsp->tcp_hsp_addr_v6 = v6addr; 24813 if (IN6_IS_ADDR_V4MAPPED(&v6addr)) 24814 hsp->tcp_hsp_vers = IPV4_VERSION; 24815 else 24816 hsp->tcp_hsp_vers = IPV6_VERSION; 24817 hsp->tcp_hsp_subnet_v6 = v6mask; 24818 if (sendspace > 0) 24819 hsp->tcp_hsp_sendspace = sendspace; 24820 if (recvspace > 0) 24821 hsp->tcp_hsp_recvspace = recvspace; 24822 if (timestamp > 0) 24823 hsp->tcp_hsp_tstamp = timestamp - 1; 24824 } 24825 24826 done: 24827 rw_exit(&tcps->tcps_hsp_lock); 24828 return (error); 24829 } 24830 24831 /* Set callback routine passed to nd_load by tcp_param_register. */ 24832 /* ARGSUSED */ 24833 static int 24834 tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 24835 { 24836 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET)); 24837 } 24838 /* ARGSUSED */ 24839 static int 24840 tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24841 cred_t *cr) 24842 { 24843 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET6)); 24844 } 24845 24846 /* TCP host parameters report triggered via the Named Dispatch mechanism. */ 24847 /* ARGSUSED */ 24848 static int 24849 tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 24850 { 24851 tcp_hsp_t *hsp; 24852 int i; 24853 char addrbuf[INET6_ADDRSTRLEN], subnetbuf[INET6_ADDRSTRLEN]; 24854 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24855 24856 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24857 (void) mi_mpprintf(mp, 24858 "Hash HSP " MI_COL_HDRPAD_STR 24859 "Address Subnet Mask Send Receive TStamp"); 24860 if (tcps->tcps_hsp_hash) { 24861 for (i = 0; i < TCP_HSP_HASH_SIZE; i++) { 24862 hsp = tcps->tcps_hsp_hash[i]; 24863 while (hsp) { 24864 if (hsp->tcp_hsp_vers == IPV4_VERSION) { 24865 (void) inet_ntop(AF_INET, 24866 &hsp->tcp_hsp_addr, 24867 addrbuf, sizeof (addrbuf)); 24868 (void) inet_ntop(AF_INET, 24869 &hsp->tcp_hsp_subnet, 24870 subnetbuf, sizeof (subnetbuf)); 24871 } else { 24872 (void) inet_ntop(AF_INET6, 24873 &hsp->tcp_hsp_addr_v6, 24874 addrbuf, sizeof (addrbuf)); 24875 (void) inet_ntop(AF_INET6, 24876 &hsp->tcp_hsp_subnet_v6, 24877 subnetbuf, sizeof (subnetbuf)); 24878 } 24879 (void) mi_mpprintf(mp, 24880 " %03d " MI_COL_PTRFMT_STR 24881 "%s %s %010d %010d %d", 24882 i, 24883 (void *)hsp, 24884 addrbuf, 24885 subnetbuf, 24886 hsp->tcp_hsp_sendspace, 24887 hsp->tcp_hsp_recvspace, 24888 hsp->tcp_hsp_tstamp); 24889 24890 hsp = hsp->tcp_hsp_next; 24891 } 24892 } 24893 } 24894 rw_exit(&tcps->tcps_hsp_lock); 24895 return (0); 24896 } 24897 24898 24899 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24900 24901 static ipaddr_t netmasks[] = { 24902 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24903 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24904 }; 24905 24906 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24907 24908 /* 24909 * XXX This routine should go away and instead we should use the metrics 24910 * associated with the routes to determine the default sndspace and rcvspace. 24911 */ 24912 static tcp_hsp_t * 24913 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24914 { 24915 tcp_hsp_t *hsp = NULL; 24916 24917 /* Quick check without acquiring the lock. */ 24918 if (tcps->tcps_hsp_hash == NULL) 24919 return (NULL); 24920 24921 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24922 24923 /* This routine finds the best-matching HSP for address addr. */ 24924 24925 if (tcps->tcps_hsp_hash) { 24926 int i; 24927 ipaddr_t srchaddr; 24928 tcp_hsp_t *hsp_net; 24929 24930 /* We do three passes: host, network, and subnet. */ 24931 24932 srchaddr = addr; 24933 24934 for (i = 1; i <= 3; i++) { 24935 /* Look for exact match on srchaddr */ 24936 24937 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24938 while (hsp) { 24939 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24940 hsp->tcp_hsp_addr == srchaddr) 24941 break; 24942 hsp = hsp->tcp_hsp_next; 24943 } 24944 ASSERT(hsp == NULL || 24945 hsp->tcp_hsp_vers == IPV4_VERSION); 24946 24947 /* 24948 * If this is the first pass: 24949 * If we found a match, great, return it. 24950 * If not, search for the network on the second pass. 24951 */ 24952 24953 if (i == 1) 24954 if (hsp) 24955 break; 24956 else 24957 { 24958 srchaddr = addr & netmask(addr); 24959 continue; 24960 } 24961 24962 /* 24963 * If this is the second pass: 24964 * If we found a match, but there's a subnet mask, 24965 * save the match but try again using the subnet 24966 * mask on the third pass. 24967 * Otherwise, return whatever we found. 24968 */ 24969 24970 if (i == 2) { 24971 if (hsp && hsp->tcp_hsp_subnet) { 24972 hsp_net = hsp; 24973 srchaddr = addr & hsp->tcp_hsp_subnet; 24974 continue; 24975 } else { 24976 break; 24977 } 24978 } 24979 24980 /* 24981 * This must be the third pass. If we didn't find 24982 * anything, return the saved network HSP instead. 24983 */ 24984 24985 if (!hsp) 24986 hsp = hsp_net; 24987 } 24988 } 24989 24990 rw_exit(&tcps->tcps_hsp_lock); 24991 return (hsp); 24992 } 24993 24994 /* 24995 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24996 * match lookup. 24997 */ 24998 static tcp_hsp_t * 24999 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 25000 { 25001 tcp_hsp_t *hsp = NULL; 25002 25003 /* Quick check without acquiring the lock. */ 25004 if (tcps->tcps_hsp_hash == NULL) 25005 return (NULL); 25006 25007 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 25008 25009 /* This routine finds the best-matching HSP for address addr. */ 25010 25011 if (tcps->tcps_hsp_hash) { 25012 int i; 25013 in6_addr_t v6srchaddr; 25014 tcp_hsp_t *hsp_net; 25015 25016 /* We do three passes: host, network, and subnet. */ 25017 25018 v6srchaddr = *v6addr; 25019 25020 for (i = 1; i <= 3; i++) { 25021 /* Look for exact match on srchaddr */ 25022 25023 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 25024 V4_PART_OF_V6(v6srchaddr))]; 25025 while (hsp) { 25026 if (hsp->tcp_hsp_vers == IPV6_VERSION && 25027 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 25028 &v6srchaddr)) 25029 break; 25030 hsp = hsp->tcp_hsp_next; 25031 } 25032 25033 /* 25034 * If this is the first pass: 25035 * If we found a match, great, return it. 25036 * If not, search for the network on the second pass. 25037 */ 25038 25039 if (i == 1) 25040 if (hsp) 25041 break; 25042 else { 25043 /* Assume a 64 bit mask */ 25044 v6srchaddr.s6_addr32[0] = 25045 v6addr->s6_addr32[0]; 25046 v6srchaddr.s6_addr32[1] = 25047 v6addr->s6_addr32[1]; 25048 v6srchaddr.s6_addr32[2] = 0; 25049 v6srchaddr.s6_addr32[3] = 0; 25050 continue; 25051 } 25052 25053 /* 25054 * If this is the second pass: 25055 * If we found a match, but there's a subnet mask, 25056 * save the match but try again using the subnet 25057 * mask on the third pass. 25058 * Otherwise, return whatever we found. 25059 */ 25060 25061 if (i == 2) { 25062 ASSERT(hsp == NULL || 25063 hsp->tcp_hsp_vers == IPV6_VERSION); 25064 if (hsp && 25065 !IN6_IS_ADDR_UNSPECIFIED( 25066 &hsp->tcp_hsp_subnet_v6)) { 25067 hsp_net = hsp; 25068 V6_MASK_COPY(*v6addr, 25069 hsp->tcp_hsp_subnet_v6, v6srchaddr); 25070 continue; 25071 } else { 25072 break; 25073 } 25074 } 25075 25076 /* 25077 * This must be the third pass. If we didn't find 25078 * anything, return the saved network HSP instead. 25079 */ 25080 25081 if (!hsp) 25082 hsp = hsp_net; 25083 } 25084 } 25085 25086 rw_exit(&tcps->tcps_hsp_lock); 25087 return (hsp); 25088 } 25089 25090 /* 25091 * Type three generator adapted from the random() function in 4.4 BSD: 25092 */ 25093 25094 /* 25095 * Copyright (c) 1983, 1993 25096 * The Regents of the University of California. All rights reserved. 25097 * 25098 * Redistribution and use in source and binary forms, with or without 25099 * modification, are permitted provided that the following conditions 25100 * are met: 25101 * 1. Redistributions of source code must retain the above copyright 25102 * notice, this list of conditions and the following disclaimer. 25103 * 2. Redistributions in binary form must reproduce the above copyright 25104 * notice, this list of conditions and the following disclaimer in the 25105 * documentation and/or other materials provided with the distribution. 25106 * 3. All advertising materials mentioning features or use of this software 25107 * must display the following acknowledgement: 25108 * This product includes software developed by the University of 25109 * California, Berkeley and its contributors. 25110 * 4. Neither the name of the University nor the names of its contributors 25111 * may be used to endorse or promote products derived from this software 25112 * without specific prior written permission. 25113 * 25114 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25115 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25116 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25117 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25118 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25119 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25120 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25121 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25122 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25123 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25124 * SUCH DAMAGE. 25125 */ 25126 25127 /* Type 3 -- x**31 + x**3 + 1 */ 25128 #define DEG_3 31 25129 #define SEP_3 3 25130 25131 25132 /* Protected by tcp_random_lock */ 25133 static int tcp_randtbl[DEG_3 + 1]; 25134 25135 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 25136 static int *tcp_random_rptr = &tcp_randtbl[1]; 25137 25138 static int *tcp_random_state = &tcp_randtbl[1]; 25139 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 25140 25141 kmutex_t tcp_random_lock; 25142 25143 void 25144 tcp_random_init(void) 25145 { 25146 int i; 25147 hrtime_t hrt; 25148 time_t wallclock; 25149 uint64_t result; 25150 25151 /* 25152 * Use high-res timer and current time for seed. Gethrtime() returns 25153 * a longlong, which may contain resolution down to nanoseconds. 25154 * The current time will either be a 32-bit or a 64-bit quantity. 25155 * XOR the two together in a 64-bit result variable. 25156 * Convert the result to a 32-bit value by multiplying the high-order 25157 * 32-bits by the low-order 32-bits. 25158 */ 25159 25160 hrt = gethrtime(); 25161 (void) drv_getparm(TIME, &wallclock); 25162 result = (uint64_t)wallclock ^ (uint64_t)hrt; 25163 mutex_enter(&tcp_random_lock); 25164 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 25165 (result & 0xffffffff); 25166 25167 for (i = 1; i < DEG_3; i++) 25168 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 25169 + 12345; 25170 tcp_random_fptr = &tcp_random_state[SEP_3]; 25171 tcp_random_rptr = &tcp_random_state[0]; 25172 mutex_exit(&tcp_random_lock); 25173 for (i = 0; i < 10 * DEG_3; i++) 25174 (void) tcp_random(); 25175 } 25176 25177 /* 25178 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 25179 * This range is selected to be approximately centered on TCP_ISS / 2, 25180 * and easy to compute. We get this value by generating a 32-bit random 25181 * number, selecting out the high-order 17 bits, and then adding one so 25182 * that we never return zero. 25183 */ 25184 int 25185 tcp_random(void) 25186 { 25187 int i; 25188 25189 mutex_enter(&tcp_random_lock); 25190 *tcp_random_fptr += *tcp_random_rptr; 25191 25192 /* 25193 * The high-order bits are more random than the low-order bits, 25194 * so we select out the high-order 17 bits and add one so that 25195 * we never return zero. 25196 */ 25197 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 25198 if (++tcp_random_fptr >= tcp_random_end_ptr) { 25199 tcp_random_fptr = tcp_random_state; 25200 ++tcp_random_rptr; 25201 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 25202 tcp_random_rptr = tcp_random_state; 25203 25204 mutex_exit(&tcp_random_lock); 25205 return (i); 25206 } 25207 25208 /* 25209 * XXX This will go away when TPI is extended to send 25210 * info reqs to sockfs/timod ..... 25211 * Given a queue, set the max packet size for the write 25212 * side of the queue below stream head. This value is 25213 * cached on the stream head. 25214 * Returns 1 on success, 0 otherwise. 25215 */ 25216 static int 25217 setmaxps(queue_t *q, int maxpsz) 25218 { 25219 struct stdata *stp; 25220 queue_t *wq; 25221 stp = STREAM(q); 25222 25223 /* 25224 * At this point change of a queue parameter is not allowed 25225 * when a multiplexor is sitting on top. 25226 */ 25227 if (stp->sd_flag & STPLEX) 25228 return (0); 25229 25230 claimstr(stp->sd_wrq); 25231 wq = stp->sd_wrq->q_next; 25232 ASSERT(wq != NULL); 25233 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 25234 releasestr(stp->sd_wrq); 25235 return (1); 25236 } 25237 25238 static int 25239 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 25240 int *t_errorp, int *sys_errorp) 25241 { 25242 int error; 25243 int is_absreq_failure; 25244 t_scalar_t *opt_lenp; 25245 t_scalar_t opt_offset; 25246 int prim_type; 25247 struct T_conn_req *tcreqp; 25248 struct T_conn_res *tcresp; 25249 cred_t *cr; 25250 25251 cr = DB_CREDDEF(mp, tcp->tcp_cred); 25252 25253 prim_type = ((union T_primitives *)mp->b_rptr)->type; 25254 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 25255 prim_type == T_CONN_RES); 25256 25257 switch (prim_type) { 25258 case T_CONN_REQ: 25259 tcreqp = (struct T_conn_req *)mp->b_rptr; 25260 opt_offset = tcreqp->OPT_offset; 25261 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 25262 break; 25263 case O_T_CONN_RES: 25264 case T_CONN_RES: 25265 tcresp = (struct T_conn_res *)mp->b_rptr; 25266 opt_offset = tcresp->OPT_offset; 25267 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 25268 break; 25269 } 25270 25271 *t_errorp = 0; 25272 *sys_errorp = 0; 25273 *do_disconnectp = 0; 25274 25275 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 25276 opt_offset, cr, &tcp_opt_obj, 25277 NULL, &is_absreq_failure); 25278 25279 switch (error) { 25280 case 0: /* no error */ 25281 ASSERT(is_absreq_failure == 0); 25282 return (0); 25283 case ENOPROTOOPT: 25284 *t_errorp = TBADOPT; 25285 break; 25286 case EACCES: 25287 *t_errorp = TACCES; 25288 break; 25289 default: 25290 *t_errorp = TSYSERR; *sys_errorp = error; 25291 break; 25292 } 25293 if (is_absreq_failure != 0) { 25294 /* 25295 * The connection request should get the local ack 25296 * T_OK_ACK and then a T_DISCON_IND. 25297 */ 25298 *do_disconnectp = 1; 25299 } 25300 return (-1); 25301 } 25302 25303 /* 25304 * Split this function out so that if the secret changes, I'm okay. 25305 * 25306 * Initialize the tcp_iss_cookie and tcp_iss_key. 25307 */ 25308 25309 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 25310 25311 static void 25312 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 25313 { 25314 struct { 25315 int32_t current_time; 25316 uint32_t randnum; 25317 uint16_t pad; 25318 uint8_t ether[6]; 25319 uint8_t passwd[PASSWD_SIZE]; 25320 } tcp_iss_cookie; 25321 time_t t; 25322 25323 /* 25324 * Start with the current absolute time. 25325 */ 25326 (void) drv_getparm(TIME, &t); 25327 tcp_iss_cookie.current_time = t; 25328 25329 /* 25330 * XXX - Need a more random number per RFC 1750, not this crap. 25331 * OTOH, if what follows is pretty random, then I'm in better shape. 25332 */ 25333 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 25334 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 25335 25336 /* 25337 * The cpu_type_info is pretty non-random. Ugggh. It does serve 25338 * as a good template. 25339 */ 25340 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 25341 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 25342 25343 /* 25344 * The pass-phrase. Normally this is supplied by user-called NDD. 25345 */ 25346 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 25347 25348 /* 25349 * See 4010593 if this section becomes a problem again, 25350 * but the local ethernet address is useful here. 25351 */ 25352 (void) localetheraddr(NULL, 25353 (struct ether_addr *)&tcp_iss_cookie.ether); 25354 25355 /* 25356 * Hash 'em all together. The MD5Final is called per-connection. 25357 */ 25358 mutex_enter(&tcps->tcps_iss_key_lock); 25359 MD5Init(&tcps->tcps_iss_key); 25360 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 25361 sizeof (tcp_iss_cookie)); 25362 mutex_exit(&tcps->tcps_iss_key_lock); 25363 } 25364 25365 /* 25366 * Set the RFC 1948 pass phrase 25367 */ 25368 /* ARGSUSED */ 25369 static int 25370 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 25371 cred_t *cr) 25372 { 25373 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 25374 25375 /* 25376 * Basically, value contains a new pass phrase. Pass it along! 25377 */ 25378 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 25379 return (0); 25380 } 25381 25382 /* ARGSUSED */ 25383 static int 25384 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 25385 { 25386 bzero(buf, sizeof (tcp_sack_info_t)); 25387 return (0); 25388 } 25389 25390 /* ARGSUSED */ 25391 static int 25392 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 25393 { 25394 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 25395 return (0); 25396 } 25397 25398 /* 25399 * Make sure we wait until the default queue is setup, yet allow 25400 * tcp_g_q_create() to open a TCP stream. 25401 * We need to allow tcp_g_q_create() do do an open 25402 * of tcp, hence we compare curhread. 25403 * All others have to wait until the tcps_g_q has been 25404 * setup. 25405 */ 25406 void 25407 tcp_g_q_setup(tcp_stack_t *tcps) 25408 { 25409 mutex_enter(&tcps->tcps_g_q_lock); 25410 if (tcps->tcps_g_q != NULL) { 25411 mutex_exit(&tcps->tcps_g_q_lock); 25412 return; 25413 } 25414 if (tcps->tcps_g_q_creator == NULL) { 25415 /* This thread will set it up */ 25416 tcps->tcps_g_q_creator = curthread; 25417 mutex_exit(&tcps->tcps_g_q_lock); 25418 tcp_g_q_create(tcps); 25419 mutex_enter(&tcps->tcps_g_q_lock); 25420 ASSERT(tcps->tcps_g_q_creator == curthread); 25421 tcps->tcps_g_q_creator = NULL; 25422 cv_signal(&tcps->tcps_g_q_cv); 25423 ASSERT(tcps->tcps_g_q != NULL); 25424 mutex_exit(&tcps->tcps_g_q_lock); 25425 return; 25426 } 25427 /* Everybody but the creator has to wait */ 25428 if (tcps->tcps_g_q_creator != curthread) { 25429 while (tcps->tcps_g_q == NULL) 25430 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 25431 } 25432 mutex_exit(&tcps->tcps_g_q_lock); 25433 } 25434 25435 #define IP "ip" 25436 25437 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 25438 25439 /* 25440 * Create a default tcp queue here instead of in strplumb 25441 */ 25442 void 25443 tcp_g_q_create(tcp_stack_t *tcps) 25444 { 25445 int error; 25446 ldi_handle_t lh = NULL; 25447 ldi_ident_t li = NULL; 25448 int rval; 25449 cred_t *cr; 25450 major_t IP_MAJ; 25451 25452 #ifdef NS_DEBUG 25453 (void) printf("tcp_g_q_create()\n"); 25454 #endif 25455 25456 IP_MAJ = ddi_name_to_major(IP); 25457 25458 ASSERT(tcps->tcps_g_q_creator == curthread); 25459 25460 error = ldi_ident_from_major(IP_MAJ, &li); 25461 if (error) { 25462 #ifdef DEBUG 25463 printf("tcp_g_q_create: lyr ident get failed error %d\n", 25464 error); 25465 #endif 25466 return; 25467 } 25468 25469 cr = zone_get_kcred(netstackid_to_zoneid( 25470 tcps->tcps_netstack->netstack_stackid)); 25471 ASSERT(cr != NULL); 25472 /* 25473 * We set the tcp default queue to IPv6 because IPv4 falls 25474 * back to IPv6 when it can't find a client, but 25475 * IPv6 does not fall back to IPv4. 25476 */ 25477 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 25478 if (error) { 25479 #ifdef DEBUG 25480 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 25481 error); 25482 #endif 25483 goto out; 25484 } 25485 25486 /* 25487 * This ioctl causes the tcp framework to cache a pointer to 25488 * this stream, so we don't want to close the stream after 25489 * this operation. 25490 * Use the kernel credentials that are for the zone we're in. 25491 */ 25492 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 25493 (intptr_t)0, FKIOCTL, cr, &rval); 25494 if (error) { 25495 #ifdef DEBUG 25496 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 25497 "error %d\n", error); 25498 #endif 25499 goto out; 25500 } 25501 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 25502 lh = NULL; 25503 out: 25504 /* Close layered handles */ 25505 if (li) 25506 ldi_ident_release(li); 25507 /* Keep cred around until _inactive needs it */ 25508 tcps->tcps_g_q_cr = cr; 25509 } 25510 25511 /* 25512 * We keep tcp_g_q set until all other tcp_t's in the zone 25513 * has gone away, and then when tcp_g_q_inactive() is called 25514 * we clear it. 25515 */ 25516 void 25517 tcp_g_q_destroy(tcp_stack_t *tcps) 25518 { 25519 #ifdef NS_DEBUG 25520 (void) printf("tcp_g_q_destroy()for stack %d\n", 25521 tcps->tcps_netstack->netstack_stackid); 25522 #endif 25523 25524 if (tcps->tcps_g_q == NULL) { 25525 return; /* Nothing to cleanup */ 25526 } 25527 /* 25528 * Drop reference corresponding to the default queue. 25529 * This reference was added from tcp_open when the default queue 25530 * was created, hence we compensate for this extra drop in 25531 * tcp_g_q_close. If the refcnt drops to zero here it means 25532 * the default queue was the last one to be open, in which 25533 * case, then tcp_g_q_inactive will be 25534 * called as a result of the refrele. 25535 */ 25536 TCPS_REFRELE(tcps); 25537 } 25538 25539 /* 25540 * Called when last tcp_t drops reference count using TCPS_REFRELE. 25541 * Run by tcp_q_q_inactive using a taskq. 25542 */ 25543 static void 25544 tcp_g_q_close(void *arg) 25545 { 25546 tcp_stack_t *tcps = arg; 25547 int error; 25548 ldi_handle_t lh = NULL; 25549 ldi_ident_t li = NULL; 25550 cred_t *cr; 25551 major_t IP_MAJ; 25552 25553 IP_MAJ = ddi_name_to_major(IP); 25554 25555 #ifdef NS_DEBUG 25556 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 25557 tcps->tcps_netstack->netstack_stackid, 25558 tcps->tcps_netstack->netstack_refcnt); 25559 #endif 25560 lh = tcps->tcps_g_q_lh; 25561 if (lh == NULL) 25562 return; /* Nothing to cleanup */ 25563 25564 ASSERT(tcps->tcps_refcnt == 1); 25565 ASSERT(tcps->tcps_g_q != NULL); 25566 25567 error = ldi_ident_from_major(IP_MAJ, &li); 25568 if (error) { 25569 #ifdef DEBUG 25570 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 25571 error); 25572 #endif 25573 return; 25574 } 25575 25576 cr = tcps->tcps_g_q_cr; 25577 tcps->tcps_g_q_cr = NULL; 25578 ASSERT(cr != NULL); 25579 25580 /* 25581 * Make sure we can break the recursion when tcp_close decrements 25582 * the reference count causing g_q_inactive to be called again. 25583 */ 25584 tcps->tcps_g_q_lh = NULL; 25585 25586 /* close the default queue */ 25587 (void) ldi_close(lh, FREAD|FWRITE, cr); 25588 /* 25589 * At this point in time tcps and the rest of netstack_t might 25590 * have been deleted. 25591 */ 25592 tcps = NULL; 25593 25594 /* Close layered handles */ 25595 ldi_ident_release(li); 25596 crfree(cr); 25597 } 25598 25599 /* 25600 * Called when last tcp_t drops reference count using TCPS_REFRELE. 25601 * 25602 * Have to ensure that the ldi routines are not used by an 25603 * interrupt thread by using a taskq. 25604 */ 25605 void 25606 tcp_g_q_inactive(tcp_stack_t *tcps) 25607 { 25608 if (tcps->tcps_g_q_lh == NULL) 25609 return; /* Nothing to cleanup */ 25610 25611 ASSERT(tcps->tcps_refcnt == 0); 25612 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 25613 25614 if (servicing_interrupt()) { 25615 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 25616 (void *) tcps, TQ_SLEEP); 25617 } else { 25618 tcp_g_q_close(tcps); 25619 } 25620 } 25621 25622 /* 25623 * Called by IP when IP is loaded into the kernel 25624 */ 25625 void 25626 tcp_ddi_g_init(void) 25627 { 25628 tcp_timercache = kmem_cache_create("tcp_timercache", 25629 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 25630 NULL, NULL, NULL, NULL, NULL, 0); 25631 25632 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 25633 sizeof (tcp_sack_info_t), 0, 25634 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 25635 25636 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 25637 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 25638 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 25639 25640 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 25641 25642 /* Initialize the random number generator */ 25643 tcp_random_init(); 25644 25645 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 25646 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 25647 25648 /* A single callback independently of how many netstacks we have */ 25649 ip_squeue_init(tcp_squeue_add); 25650 25651 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 25652 25653 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 25654 TASKQ_PREPOPULATE); 25655 25656 /* 25657 * We want to be informed each time a stack is created or 25658 * destroyed in the kernel, so we can maintain the 25659 * set of tcp_stack_t's. 25660 */ 25661 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 25662 tcp_stack_fini); 25663 } 25664 25665 25666 /* 25667 * Initialize the TCP stack instance. 25668 */ 25669 static void * 25670 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 25671 { 25672 tcp_stack_t *tcps; 25673 tcpparam_t *pa; 25674 int i; 25675 25676 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 25677 tcps->tcps_netstack = ns; 25678 25679 /* Initialize locks */ 25680 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 25681 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 25682 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 25683 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 25684 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 25685 rw_init(&tcps->tcps_reserved_port_lock, NULL, RW_DEFAULT, NULL); 25686 25687 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 25688 tcps->tcps_g_epriv_ports[0] = 2049; 25689 tcps->tcps_g_epriv_ports[1] = 4045; 25690 tcps->tcps_min_anonpriv_port = 512; 25691 25692 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 25693 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 25694 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 25695 TCP_FANOUT_SIZE, KM_SLEEP); 25696 tcps->tcps_reserved_port = kmem_zalloc(sizeof (tcp_rport_t) * 25697 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE, KM_SLEEP); 25698 25699 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25700 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 25701 MUTEX_DEFAULT, NULL); 25702 } 25703 25704 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25705 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 25706 MUTEX_DEFAULT, NULL); 25707 } 25708 25709 /* TCP's IPsec code calls the packet dropper. */ 25710 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 25711 25712 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 25713 tcps->tcps_params = pa; 25714 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25715 25716 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 25717 A_CNT(lcl_tcp_param_arr), tcps); 25718 25719 /* 25720 * Note: To really walk the device tree you need the devinfo 25721 * pointer to your device which is only available after probe/attach. 25722 * The following is safe only because it uses ddi_root_node() 25723 */ 25724 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 25725 tcp_opt_obj.odb_opt_arr_cnt); 25726 25727 /* 25728 * Initialize RFC 1948 secret values. This will probably be reset once 25729 * by the boot scripts. 25730 * 25731 * Use NULL name, as the name is caught by the new lockstats. 25732 * 25733 * Initialize with some random, non-guessable string, like the global 25734 * T_INFO_ACK. 25735 */ 25736 25737 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 25738 sizeof (tcp_g_t_info_ack), tcps); 25739 25740 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 25741 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 25742 25743 return (tcps); 25744 } 25745 25746 /* 25747 * Called when the IP module is about to be unloaded. 25748 */ 25749 void 25750 tcp_ddi_g_destroy(void) 25751 { 25752 tcp_g_kstat_fini(tcp_g_kstat); 25753 tcp_g_kstat = NULL; 25754 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 25755 25756 mutex_destroy(&tcp_random_lock); 25757 25758 kmem_cache_destroy(tcp_timercache); 25759 kmem_cache_destroy(tcp_sack_info_cache); 25760 kmem_cache_destroy(tcp_iphc_cache); 25761 25762 netstack_unregister(NS_TCP); 25763 taskq_destroy(tcp_taskq); 25764 } 25765 25766 /* 25767 * Shut down the TCP stack instance. 25768 */ 25769 /* ARGSUSED */ 25770 static void 25771 tcp_stack_shutdown(netstackid_t stackid, void *arg) 25772 { 25773 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25774 25775 tcp_g_q_destroy(tcps); 25776 } 25777 25778 /* 25779 * Free the TCP stack instance. 25780 */ 25781 static void 25782 tcp_stack_fini(netstackid_t stackid, void *arg) 25783 { 25784 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25785 int i; 25786 25787 nd_free(&tcps->tcps_g_nd); 25788 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25789 tcps->tcps_params = NULL; 25790 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 25791 tcps->tcps_wroff_xtra_param = NULL; 25792 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 25793 tcps->tcps_mdt_head_param = NULL; 25794 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 25795 tcps->tcps_mdt_tail_param = NULL; 25796 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 25797 tcps->tcps_mdt_max_pbufs_param = NULL; 25798 25799 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25800 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 25801 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 25802 } 25803 25804 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25805 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25806 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25807 } 25808 25809 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25810 tcps->tcps_bind_fanout = NULL; 25811 25812 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25813 tcps->tcps_acceptor_fanout = NULL; 25814 25815 kmem_free(tcps->tcps_reserved_port, sizeof (tcp_rport_t) * 25816 TCP_RESERVED_PORTS_ARRAY_MAX_SIZE); 25817 tcps->tcps_reserved_port = NULL; 25818 25819 mutex_destroy(&tcps->tcps_iss_key_lock); 25820 rw_destroy(&tcps->tcps_hsp_lock); 25821 mutex_destroy(&tcps->tcps_g_q_lock); 25822 cv_destroy(&tcps->tcps_g_q_cv); 25823 mutex_destroy(&tcps->tcps_epriv_port_lock); 25824 rw_destroy(&tcps->tcps_reserved_port_lock); 25825 25826 ip_drop_unregister(&tcps->tcps_dropper); 25827 25828 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25829 tcps->tcps_kstat = NULL; 25830 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25831 25832 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25833 tcps->tcps_mibkp = NULL; 25834 25835 kmem_free(tcps, sizeof (*tcps)); 25836 } 25837 25838 /* 25839 * Generate ISS, taking into account NDD changes may happen halfway through. 25840 * (If the iss is not zero, set it.) 25841 */ 25842 25843 static void 25844 tcp_iss_init(tcp_t *tcp) 25845 { 25846 MD5_CTX context; 25847 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25848 uint32_t answer[4]; 25849 tcp_stack_t *tcps = tcp->tcp_tcps; 25850 25851 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25852 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25853 switch (tcps->tcps_strong_iss) { 25854 case 2: 25855 mutex_enter(&tcps->tcps_iss_key_lock); 25856 context = tcps->tcps_iss_key; 25857 mutex_exit(&tcps->tcps_iss_key_lock); 25858 arg.ports = tcp->tcp_ports; 25859 if (tcp->tcp_ipversion == IPV4_VERSION) { 25860 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25861 &arg.src); 25862 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25863 &arg.dst); 25864 } else { 25865 arg.src = tcp->tcp_ip6h->ip6_src; 25866 arg.dst = tcp->tcp_ip6h->ip6_dst; 25867 } 25868 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25869 MD5Final((uchar_t *)answer, &context); 25870 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25871 /* 25872 * Now that we've hashed into a unique per-connection sequence 25873 * space, add a random increment per strong_iss == 1. So I 25874 * guess we'll have to... 25875 */ 25876 /* FALLTHRU */ 25877 case 1: 25878 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25879 break; 25880 default: 25881 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25882 break; 25883 } 25884 tcp->tcp_valid_bits = TCP_ISS_VALID; 25885 tcp->tcp_fss = tcp->tcp_iss - 1; 25886 tcp->tcp_suna = tcp->tcp_iss; 25887 tcp->tcp_snxt = tcp->tcp_iss + 1; 25888 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25889 tcp->tcp_csuna = tcp->tcp_snxt; 25890 } 25891 25892 /* 25893 * Exported routine for extracting active tcp connection status. 25894 * 25895 * This is used by the Solaris Cluster Networking software to 25896 * gather a list of connections that need to be forwarded to 25897 * specific nodes in the cluster when configuration changes occur. 25898 * 25899 * The callback is invoked for each tcp_t structure. Returning 25900 * non-zero from the callback routine terminates the search. 25901 */ 25902 int 25903 cl_tcp_walk_list(int (*cl_callback)(cl_tcp_info_t *, void *), 25904 void *arg) 25905 { 25906 netstack_handle_t nh; 25907 netstack_t *ns; 25908 int ret = 0; 25909 25910 netstack_next_init(&nh); 25911 while ((ns = netstack_next(&nh)) != NULL) { 25912 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25913 ns->netstack_tcp); 25914 netstack_rele(ns); 25915 } 25916 netstack_next_fini(&nh); 25917 return (ret); 25918 } 25919 25920 static int 25921 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25922 tcp_stack_t *tcps) 25923 { 25924 tcp_t *tcp; 25925 cl_tcp_info_t cl_tcpi; 25926 connf_t *connfp; 25927 conn_t *connp; 25928 int i; 25929 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25930 25931 ASSERT(callback != NULL); 25932 25933 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25934 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25935 connp = NULL; 25936 25937 while ((connp = 25938 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25939 25940 tcp = connp->conn_tcp; 25941 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25942 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25943 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25944 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25945 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25946 /* 25947 * The macros tcp_laddr and tcp_faddr give the IPv4 25948 * addresses. They are copied implicitly below as 25949 * mapped addresses. 25950 */ 25951 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25952 if (tcp->tcp_ipversion == IPV4_VERSION) { 25953 cl_tcpi.cl_tcpi_faddr = 25954 tcp->tcp_ipha->ipha_dst; 25955 } else { 25956 cl_tcpi.cl_tcpi_faddr_v6 = 25957 tcp->tcp_ip6h->ip6_dst; 25958 } 25959 25960 /* 25961 * If the callback returns non-zero 25962 * we terminate the traversal. 25963 */ 25964 if ((*callback)(&cl_tcpi, arg) != 0) { 25965 CONN_DEC_REF(tcp->tcp_connp); 25966 return (1); 25967 } 25968 } 25969 } 25970 25971 return (0); 25972 } 25973 25974 /* 25975 * Macros used for accessing the different types of sockaddr 25976 * structures inside a tcp_ioc_abort_conn_t. 25977 */ 25978 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25979 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25980 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25981 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25982 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25983 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25984 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25985 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25986 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25987 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25988 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25989 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25990 25991 /* 25992 * Return the correct error code to mimic the behavior 25993 * of a connection reset. 25994 */ 25995 #define TCP_AC_GET_ERRCODE(state, err) { \ 25996 switch ((state)) { \ 25997 case TCPS_SYN_SENT: \ 25998 case TCPS_SYN_RCVD: \ 25999 (err) = ECONNREFUSED; \ 26000 break; \ 26001 case TCPS_ESTABLISHED: \ 26002 case TCPS_FIN_WAIT_1: \ 26003 case TCPS_FIN_WAIT_2: \ 26004 case TCPS_CLOSE_WAIT: \ 26005 (err) = ECONNRESET; \ 26006 break; \ 26007 case TCPS_CLOSING: \ 26008 case TCPS_LAST_ACK: \ 26009 case TCPS_TIME_WAIT: \ 26010 (err) = 0; \ 26011 break; \ 26012 default: \ 26013 (err) = ENXIO; \ 26014 } \ 26015 } 26016 26017 /* 26018 * Check if a tcp structure matches the info in acp. 26019 */ 26020 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 26021 (((acp)->ac_local.ss_family == AF_INET) ? \ 26022 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 26023 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 26024 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 26025 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 26026 (TCP_AC_V4LPORT((acp)) == 0 || \ 26027 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 26028 (TCP_AC_V4RPORT((acp)) == 0 || \ 26029 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 26030 (acp)->ac_start <= (tcp)->tcp_state && \ 26031 (acp)->ac_end >= (tcp)->tcp_state) : \ 26032 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 26033 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 26034 &(tcp)->tcp_ip_src_v6)) && \ 26035 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 26036 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 26037 &(tcp)->tcp_remote_v6)) && \ 26038 (TCP_AC_V6LPORT((acp)) == 0 || \ 26039 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 26040 (TCP_AC_V6RPORT((acp)) == 0 || \ 26041 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 26042 (acp)->ac_start <= (tcp)->tcp_state && \ 26043 (acp)->ac_end >= (tcp)->tcp_state)) 26044 26045 #define TCP_AC_MATCH(acp, tcp) \ 26046 (((acp)->ac_zoneid == ALL_ZONES || \ 26047 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 26048 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 26049 26050 /* 26051 * Build a message containing a tcp_ioc_abort_conn_t structure 26052 * which is filled in with information from acp and tp. 26053 */ 26054 static mblk_t * 26055 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 26056 { 26057 mblk_t *mp; 26058 tcp_ioc_abort_conn_t *tacp; 26059 26060 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 26061 if (mp == NULL) 26062 return (NULL); 26063 26064 mp->b_datap->db_type = M_CTL; 26065 26066 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 26067 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 26068 sizeof (uint32_t)); 26069 26070 tacp->ac_start = acp->ac_start; 26071 tacp->ac_end = acp->ac_end; 26072 tacp->ac_zoneid = acp->ac_zoneid; 26073 26074 if (acp->ac_local.ss_family == AF_INET) { 26075 tacp->ac_local.ss_family = AF_INET; 26076 tacp->ac_remote.ss_family = AF_INET; 26077 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 26078 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 26079 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 26080 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 26081 } else { 26082 tacp->ac_local.ss_family = AF_INET6; 26083 tacp->ac_remote.ss_family = AF_INET6; 26084 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 26085 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 26086 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 26087 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 26088 } 26089 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 26090 return (mp); 26091 } 26092 26093 /* 26094 * Print a tcp_ioc_abort_conn_t structure. 26095 */ 26096 static void 26097 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 26098 { 26099 char lbuf[128]; 26100 char rbuf[128]; 26101 sa_family_t af; 26102 in_port_t lport, rport; 26103 ushort_t logflags; 26104 26105 af = acp->ac_local.ss_family; 26106 26107 if (af == AF_INET) { 26108 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 26109 lbuf, 128); 26110 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 26111 rbuf, 128); 26112 lport = ntohs(TCP_AC_V4LPORT(acp)); 26113 rport = ntohs(TCP_AC_V4RPORT(acp)); 26114 } else { 26115 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 26116 lbuf, 128); 26117 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 26118 rbuf, 128); 26119 lport = ntohs(TCP_AC_V6LPORT(acp)); 26120 rport = ntohs(TCP_AC_V6RPORT(acp)); 26121 } 26122 26123 logflags = SL_TRACE | SL_NOTE; 26124 /* 26125 * Don't print this message to the console if the operation was done 26126 * to a non-global zone. 26127 */ 26128 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 26129 logflags |= SL_CONSOLE; 26130 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 26131 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 26132 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 26133 acp->ac_start, acp->ac_end); 26134 } 26135 26136 /* 26137 * Called inside tcp_rput when a message built using 26138 * tcp_ioctl_abort_build_msg is put into a queue. 26139 * Note that when we get here there is no wildcard in acp any more. 26140 */ 26141 static void 26142 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 26143 { 26144 tcp_ioc_abort_conn_t *acp; 26145 26146 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 26147 if (tcp->tcp_state <= acp->ac_end) { 26148 /* 26149 * If we get here, we are already on the correct 26150 * squeue. This ioctl follows the following path 26151 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 26152 * ->tcp_ioctl_abort->squeue_fill (if on a 26153 * different squeue) 26154 */ 26155 int errcode; 26156 26157 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 26158 (void) tcp_clean_death(tcp, errcode, 26); 26159 } 26160 freemsg(mp); 26161 } 26162 26163 /* 26164 * Abort all matching connections on a hash chain. 26165 */ 26166 static int 26167 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 26168 boolean_t exact, tcp_stack_t *tcps) 26169 { 26170 int nmatch, err = 0; 26171 tcp_t *tcp; 26172 MBLKP mp, last, listhead = NULL; 26173 conn_t *tconnp; 26174 connf_t *connfp; 26175 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 26176 26177 connfp = &ipst->ips_ipcl_conn_fanout[index]; 26178 26179 startover: 26180 nmatch = 0; 26181 26182 mutex_enter(&connfp->connf_lock); 26183 for (tconnp = connfp->connf_head; tconnp != NULL; 26184 tconnp = tconnp->conn_next) { 26185 tcp = tconnp->conn_tcp; 26186 if (TCP_AC_MATCH(acp, tcp)) { 26187 CONN_INC_REF(tcp->tcp_connp); 26188 mp = tcp_ioctl_abort_build_msg(acp, tcp); 26189 if (mp == NULL) { 26190 err = ENOMEM; 26191 CONN_DEC_REF(tcp->tcp_connp); 26192 break; 26193 } 26194 mp->b_prev = (mblk_t *)tcp; 26195 26196 if (listhead == NULL) { 26197 listhead = mp; 26198 last = mp; 26199 } else { 26200 last->b_next = mp; 26201 last = mp; 26202 } 26203 nmatch++; 26204 if (exact) 26205 break; 26206 } 26207 26208 /* Avoid holding lock for too long. */ 26209 if (nmatch >= 500) 26210 break; 26211 } 26212 mutex_exit(&connfp->connf_lock); 26213 26214 /* Pass mp into the correct tcp */ 26215 while ((mp = listhead) != NULL) { 26216 listhead = listhead->b_next; 26217 tcp = (tcp_t *)mp->b_prev; 26218 mp->b_next = mp->b_prev = NULL; 26219 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 26220 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 26221 } 26222 26223 *count += nmatch; 26224 if (nmatch >= 500 && err == 0) 26225 goto startover; 26226 return (err); 26227 } 26228 26229 /* 26230 * Abort all connections that matches the attributes specified in acp. 26231 */ 26232 static int 26233 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 26234 { 26235 sa_family_t af; 26236 uint32_t ports; 26237 uint16_t *pports; 26238 int err = 0, count = 0; 26239 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 26240 int index = -1; 26241 ushort_t logflags; 26242 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 26243 26244 af = acp->ac_local.ss_family; 26245 26246 if (af == AF_INET) { 26247 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 26248 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 26249 pports = (uint16_t *)&ports; 26250 pports[1] = TCP_AC_V4LPORT(acp); 26251 pports[0] = TCP_AC_V4RPORT(acp); 26252 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 26253 } 26254 } else { 26255 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 26256 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 26257 pports = (uint16_t *)&ports; 26258 pports[1] = TCP_AC_V6LPORT(acp); 26259 pports[0] = TCP_AC_V6RPORT(acp); 26260 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 26261 } 26262 } 26263 26264 /* 26265 * For cases where remote addr, local port, and remote port are non- 26266 * wildcards, tcp_ioctl_abort_bucket will only be called once. 26267 */ 26268 if (index != -1) { 26269 err = tcp_ioctl_abort_bucket(acp, index, 26270 &count, exact, tcps); 26271 } else { 26272 /* 26273 * loop through all entries for wildcard case 26274 */ 26275 for (index = 0; 26276 index < ipst->ips_ipcl_conn_fanout_size; 26277 index++) { 26278 err = tcp_ioctl_abort_bucket(acp, index, 26279 &count, exact, tcps); 26280 if (err != 0) 26281 break; 26282 } 26283 } 26284 26285 logflags = SL_TRACE | SL_NOTE; 26286 /* 26287 * Don't print this message to the console if the operation was done 26288 * to a non-global zone. 26289 */ 26290 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 26291 logflags |= SL_CONSOLE; 26292 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 26293 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 26294 if (err == 0 && count == 0) 26295 err = ENOENT; 26296 return (err); 26297 } 26298 26299 /* 26300 * Process the TCP_IOC_ABORT_CONN ioctl request. 26301 */ 26302 static void 26303 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 26304 { 26305 int err; 26306 IOCP iocp; 26307 MBLKP mp1; 26308 sa_family_t laf, raf; 26309 tcp_ioc_abort_conn_t *acp; 26310 zone_t *zptr; 26311 conn_t *connp = Q_TO_CONN(q); 26312 zoneid_t zoneid = connp->conn_zoneid; 26313 tcp_t *tcp = connp->conn_tcp; 26314 tcp_stack_t *tcps = tcp->tcp_tcps; 26315 26316 iocp = (IOCP)mp->b_rptr; 26317 26318 if ((mp1 = mp->b_cont) == NULL || 26319 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 26320 err = EINVAL; 26321 goto out; 26322 } 26323 26324 /* check permissions */ 26325 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 26326 err = EPERM; 26327 goto out; 26328 } 26329 26330 if (mp1->b_cont != NULL) { 26331 freemsg(mp1->b_cont); 26332 mp1->b_cont = NULL; 26333 } 26334 26335 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 26336 laf = acp->ac_local.ss_family; 26337 raf = acp->ac_remote.ss_family; 26338 26339 /* check that a zone with the supplied zoneid exists */ 26340 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 26341 zptr = zone_find_by_id(zoneid); 26342 if (zptr != NULL) { 26343 zone_rele(zptr); 26344 } else { 26345 err = EINVAL; 26346 goto out; 26347 } 26348 } 26349 26350 /* 26351 * For exclusive stacks we set the zoneid to zero 26352 * to make TCP operate as if in the global zone. 26353 */ 26354 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 26355 acp->ac_zoneid = GLOBAL_ZONEID; 26356 26357 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 26358 acp->ac_start > acp->ac_end || laf != raf || 26359 (laf != AF_INET && laf != AF_INET6)) { 26360 err = EINVAL; 26361 goto out; 26362 } 26363 26364 tcp_ioctl_abort_dump(acp); 26365 err = tcp_ioctl_abort(acp, tcps); 26366 26367 out: 26368 if (mp1 != NULL) { 26369 freemsg(mp1); 26370 mp->b_cont = NULL; 26371 } 26372 26373 if (err != 0) 26374 miocnak(q, mp, 0, err); 26375 else 26376 miocack(q, mp, 0, 0); 26377 } 26378 26379 /* 26380 * tcp_time_wait_processing() handles processing of incoming packets when 26381 * the tcp is in the TIME_WAIT state. 26382 * A TIME_WAIT tcp that has an associated open TCP stream is never put 26383 * on the time wait list. 26384 */ 26385 void 26386 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 26387 uint32_t seg_ack, int seg_len, tcph_t *tcph) 26388 { 26389 int32_t bytes_acked; 26390 int32_t gap; 26391 int32_t rgap; 26392 tcp_opt_t tcpopt; 26393 uint_t flags; 26394 uint32_t new_swnd = 0; 26395 conn_t *connp; 26396 tcp_stack_t *tcps = tcp->tcp_tcps; 26397 26398 BUMP_LOCAL(tcp->tcp_ibsegs); 26399 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 26400 26401 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 26402 new_swnd = BE16_TO_U16(tcph->th_win) << 26403 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 26404 if (tcp->tcp_snd_ts_ok) { 26405 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 26406 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 26407 tcp->tcp_rnxt, TH_ACK); 26408 goto done; 26409 } 26410 } 26411 gap = seg_seq - tcp->tcp_rnxt; 26412 rgap = tcp->tcp_rwnd - (gap + seg_len); 26413 if (gap < 0) { 26414 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 26415 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 26416 (seg_len > -gap ? -gap : seg_len)); 26417 seg_len += gap; 26418 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 26419 if (flags & TH_RST) { 26420 goto done; 26421 } 26422 if ((flags & TH_FIN) && seg_len == -1) { 26423 /* 26424 * When TCP receives a duplicate FIN in 26425 * TIME_WAIT state, restart the 2 MSL timer. 26426 * See page 73 in RFC 793. Make sure this TCP 26427 * is already on the TIME_WAIT list. If not, 26428 * just restart the timer. 26429 */ 26430 if (TCP_IS_DETACHED(tcp)) { 26431 if (tcp_time_wait_remove(tcp, NULL) == 26432 B_TRUE) { 26433 tcp_time_wait_append(tcp); 26434 TCP_DBGSTAT(tcps, 26435 tcp_rput_time_wait); 26436 } 26437 } else { 26438 ASSERT(tcp != NULL); 26439 TCP_TIMER_RESTART(tcp, 26440 tcps->tcps_time_wait_interval); 26441 } 26442 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 26443 tcp->tcp_rnxt, TH_ACK); 26444 goto done; 26445 } 26446 flags |= TH_ACK_NEEDED; 26447 seg_len = 0; 26448 goto process_ack; 26449 } 26450 26451 /* Fix seg_seq, and chew the gap off the front. */ 26452 seg_seq = tcp->tcp_rnxt; 26453 } 26454 26455 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 26456 /* 26457 * Make sure that when we accept the connection, pick 26458 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 26459 * old connection. 26460 * 26461 * The next ISS generated is equal to tcp_iss_incr_extra 26462 * + ISS_INCR/2 + other components depending on the 26463 * value of tcp_strong_iss. We pre-calculate the new 26464 * ISS here and compare with tcp_snxt to determine if 26465 * we need to make adjustment to tcp_iss_incr_extra. 26466 * 26467 * The above calculation is ugly and is a 26468 * waste of CPU cycles... 26469 */ 26470 uint32_t new_iss = tcps->tcps_iss_incr_extra; 26471 int32_t adj; 26472 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 26473 26474 switch (tcps->tcps_strong_iss) { 26475 case 2: { 26476 /* Add time and MD5 components. */ 26477 uint32_t answer[4]; 26478 struct { 26479 uint32_t ports; 26480 in6_addr_t src; 26481 in6_addr_t dst; 26482 } arg; 26483 MD5_CTX context; 26484 26485 mutex_enter(&tcps->tcps_iss_key_lock); 26486 context = tcps->tcps_iss_key; 26487 mutex_exit(&tcps->tcps_iss_key_lock); 26488 arg.ports = tcp->tcp_ports; 26489 /* We use MAPPED addresses in tcp_iss_init */ 26490 arg.src = tcp->tcp_ip_src_v6; 26491 if (tcp->tcp_ipversion == IPV4_VERSION) { 26492 IN6_IPADDR_TO_V4MAPPED( 26493 tcp->tcp_ipha->ipha_dst, 26494 &arg.dst); 26495 } else { 26496 arg.dst = 26497 tcp->tcp_ip6h->ip6_dst; 26498 } 26499 MD5Update(&context, (uchar_t *)&arg, 26500 sizeof (arg)); 26501 MD5Final((uchar_t *)answer, &context); 26502 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 26503 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 26504 break; 26505 } 26506 case 1: 26507 /* Add time component and min random (i.e. 1). */ 26508 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 26509 break; 26510 default: 26511 /* Add only time component. */ 26512 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 26513 break; 26514 } 26515 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 26516 /* 26517 * New ISS not guaranteed to be ISS_INCR/2 26518 * ahead of the current tcp_snxt, so add the 26519 * difference to tcp_iss_incr_extra. 26520 */ 26521 tcps->tcps_iss_incr_extra += adj; 26522 } 26523 /* 26524 * If tcp_clean_death() can not perform the task now, 26525 * drop the SYN packet and let the other side re-xmit. 26526 * Otherwise pass the SYN packet back in, since the 26527 * old tcp state has been cleaned up or freed. 26528 */ 26529 if (tcp_clean_death(tcp, 0, 27) == -1) 26530 goto done; 26531 /* 26532 * We will come back to tcp_rput_data 26533 * on the global queue. Packets destined 26534 * for the global queue will be checked 26535 * with global policy. But the policy for 26536 * this packet has already been checked as 26537 * this was destined for the detached 26538 * connection. We need to bypass policy 26539 * check this time by attaching a dummy 26540 * ipsec_in with ipsec_in_dont_check set. 26541 */ 26542 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 26543 if (connp != NULL) { 26544 TCP_STAT(tcps, tcp_time_wait_syn_success); 26545 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 26546 return; 26547 } 26548 goto done; 26549 } 26550 26551 /* 26552 * rgap is the amount of stuff received out of window. A negative 26553 * value is the amount out of window. 26554 */ 26555 if (rgap < 0) { 26556 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 26557 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 26558 /* Fix seg_len and make sure there is something left. */ 26559 seg_len += rgap; 26560 if (seg_len <= 0) { 26561 if (flags & TH_RST) { 26562 goto done; 26563 } 26564 flags |= TH_ACK_NEEDED; 26565 seg_len = 0; 26566 goto process_ack; 26567 } 26568 } 26569 /* 26570 * Check whether we can update tcp_ts_recent. This test is 26571 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 26572 * Extensions for High Performance: An Update", Internet Draft. 26573 */ 26574 if (tcp->tcp_snd_ts_ok && 26575 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 26576 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 26577 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 26578 tcp->tcp_last_rcv_lbolt = lbolt64; 26579 } 26580 26581 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 26582 /* Always ack out of order packets */ 26583 flags |= TH_ACK_NEEDED; 26584 seg_len = 0; 26585 } else if (seg_len > 0) { 26586 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 26587 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 26588 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 26589 } 26590 if (flags & TH_RST) { 26591 (void) tcp_clean_death(tcp, 0, 28); 26592 goto done; 26593 } 26594 if (flags & TH_SYN) { 26595 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 26596 TH_RST|TH_ACK); 26597 /* 26598 * Do not delete the TCP structure if it is in 26599 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 26600 */ 26601 goto done; 26602 } 26603 process_ack: 26604 if (flags & TH_ACK) { 26605 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 26606 if (bytes_acked <= 0) { 26607 if (bytes_acked == 0 && seg_len == 0 && 26608 new_swnd == tcp->tcp_swnd) 26609 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 26610 } else { 26611 /* Acks something not sent */ 26612 flags |= TH_ACK_NEEDED; 26613 } 26614 } 26615 if (flags & TH_ACK_NEEDED) { 26616 /* 26617 * Time to send an ack for some reason. 26618 */ 26619 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 26620 tcp->tcp_rnxt, TH_ACK); 26621 } 26622 done: 26623 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26624 DB_CKSUMSTART(mp) = 0; 26625 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 26626 TCP_STAT(tcps, tcp_time_wait_syn_fail); 26627 } 26628 freemsg(mp); 26629 } 26630 26631 /* 26632 * Allocate a T_SVR4_OPTMGMT_REQ. 26633 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 26634 * that tcp_rput_other can drop the acks. 26635 */ 26636 static mblk_t * 26637 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 26638 { 26639 mblk_t *mp; 26640 struct T_optmgmt_req *tor; 26641 struct opthdr *oh; 26642 uint_t size; 26643 char *optptr; 26644 26645 size = sizeof (*tor) + sizeof (*oh) + optlen; 26646 mp = allocb(size, BPRI_MED); 26647 if (mp == NULL) 26648 return (NULL); 26649 26650 mp->b_wptr += size; 26651 mp->b_datap->db_type = M_PROTO; 26652 tor = (struct T_optmgmt_req *)mp->b_rptr; 26653 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 26654 tor->MGMT_flags = T_NEGOTIATE; 26655 tor->OPT_length = sizeof (*oh) + optlen; 26656 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 26657 26658 oh = (struct opthdr *)&tor[1]; 26659 oh->level = level; 26660 oh->name = cmd; 26661 oh->len = optlen; 26662 if (optlen != 0) { 26663 optptr = (char *)&oh[1]; 26664 bcopy(opt, optptr, optlen); 26665 } 26666 return (mp); 26667 } 26668 26669 /* 26670 * TCP Timers Implementation. 26671 */ 26672 timeout_id_t 26673 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 26674 { 26675 mblk_t *mp; 26676 tcp_timer_t *tcpt; 26677 tcp_t *tcp = connp->conn_tcp; 26678 tcp_stack_t *tcps = tcp->tcp_tcps; 26679 26680 ASSERT(connp->conn_sqp != NULL); 26681 26682 TCP_DBGSTAT(tcps, tcp_timeout_calls); 26683 26684 if (tcp->tcp_timercache == NULL) { 26685 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 26686 } else { 26687 TCP_DBGSTAT(tcps, tcp_timeout_cached_alloc); 26688 mp = tcp->tcp_timercache; 26689 tcp->tcp_timercache = mp->b_next; 26690 mp->b_next = NULL; 26691 ASSERT(mp->b_wptr == NULL); 26692 } 26693 26694 CONN_INC_REF(connp); 26695 tcpt = (tcp_timer_t *)mp->b_rptr; 26696 tcpt->connp = connp; 26697 tcpt->tcpt_proc = f; 26698 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 26699 return ((timeout_id_t)mp); 26700 } 26701 26702 static void 26703 tcp_timer_callback(void *arg) 26704 { 26705 mblk_t *mp = (mblk_t *)arg; 26706 tcp_timer_t *tcpt; 26707 conn_t *connp; 26708 26709 tcpt = (tcp_timer_t *)mp->b_rptr; 26710 connp = tcpt->connp; 26711 squeue_fill(connp->conn_sqp, mp, 26712 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 26713 } 26714 26715 static void 26716 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 26717 { 26718 tcp_timer_t *tcpt; 26719 conn_t *connp = (conn_t *)arg; 26720 tcp_t *tcp = connp->conn_tcp; 26721 26722 tcpt = (tcp_timer_t *)mp->b_rptr; 26723 ASSERT(connp == tcpt->connp); 26724 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 26725 26726 /* 26727 * If the TCP has reached the closed state, don't proceed any 26728 * further. This TCP logically does not exist on the system. 26729 * tcpt_proc could for example access queues, that have already 26730 * been qprocoff'ed off. Also see comments at the start of tcp_input 26731 */ 26732 if (tcp->tcp_state != TCPS_CLOSED) { 26733 (*tcpt->tcpt_proc)(connp); 26734 } else { 26735 tcp->tcp_timer_tid = 0; 26736 } 26737 tcp_timer_free(connp->conn_tcp, mp); 26738 } 26739 26740 /* 26741 * There is potential race with untimeout and the handler firing at the same 26742 * time. The mblock may be freed by the handler while we are trying to use 26743 * it. But since both should execute on the same squeue, this race should not 26744 * occur. 26745 */ 26746 clock_t 26747 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 26748 { 26749 mblk_t *mp = (mblk_t *)id; 26750 tcp_timer_t *tcpt; 26751 clock_t delta; 26752 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26753 26754 TCP_DBGSTAT(tcps, tcp_timeout_cancel_reqs); 26755 26756 if (mp == NULL) 26757 return (-1); 26758 26759 tcpt = (tcp_timer_t *)mp->b_rptr; 26760 ASSERT(tcpt->connp == connp); 26761 26762 delta = untimeout(tcpt->tcpt_tid); 26763 26764 if (delta >= 0) { 26765 TCP_DBGSTAT(tcps, tcp_timeout_canceled); 26766 tcp_timer_free(connp->conn_tcp, mp); 26767 CONN_DEC_REF(connp); 26768 } 26769 26770 return (delta); 26771 } 26772 26773 /* 26774 * Allocate space for the timer event. The allocation looks like mblk, but it is 26775 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 26776 * 26777 * Dealing with failures: If we can't allocate from the timer cache we try 26778 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 26779 * points to b_rptr. 26780 * If we can't allocate anything using allocb_tryhard(), we perform a last 26781 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 26782 * save the actual allocation size in b_datap. 26783 */ 26784 mblk_t * 26785 tcp_timermp_alloc(int kmflags) 26786 { 26787 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 26788 kmflags & ~KM_PANIC); 26789 26790 if (mp != NULL) { 26791 mp->b_next = mp->b_prev = NULL; 26792 mp->b_rptr = (uchar_t *)(&mp[1]); 26793 mp->b_wptr = NULL; 26794 mp->b_datap = NULL; 26795 mp->b_queue = NULL; 26796 mp->b_cont = NULL; 26797 } else if (kmflags & KM_PANIC) { 26798 /* 26799 * Failed to allocate memory for the timer. Try allocating from 26800 * dblock caches. 26801 */ 26802 /* ipclassifier calls this from a constructor - hence no tcps */ 26803 TCP_G_STAT(tcp_timermp_allocfail); 26804 mp = allocb_tryhard(sizeof (tcp_timer_t)); 26805 if (mp == NULL) { 26806 size_t size = 0; 26807 /* 26808 * Memory is really low. Try tryhard allocation. 26809 * 26810 * ipclassifier calls this from a constructor - 26811 * hence no tcps 26812 */ 26813 TCP_G_STAT(tcp_timermp_allocdblfail); 26814 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 26815 sizeof (tcp_timer_t), &size, kmflags); 26816 mp->b_rptr = (uchar_t *)(&mp[1]); 26817 mp->b_next = mp->b_prev = NULL; 26818 mp->b_wptr = (uchar_t *)-1; 26819 mp->b_datap = (dblk_t *)size; 26820 mp->b_queue = NULL; 26821 mp->b_cont = NULL; 26822 } 26823 ASSERT(mp->b_wptr != NULL); 26824 } 26825 /* ipclassifier calls this from a constructor - hence no tcps */ 26826 TCP_G_DBGSTAT(tcp_timermp_alloced); 26827 26828 return (mp); 26829 } 26830 26831 /* 26832 * Free per-tcp timer cache. 26833 * It can only contain entries from tcp_timercache. 26834 */ 26835 void 26836 tcp_timermp_free(tcp_t *tcp) 26837 { 26838 mblk_t *mp; 26839 26840 while ((mp = tcp->tcp_timercache) != NULL) { 26841 ASSERT(mp->b_wptr == NULL); 26842 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26843 kmem_cache_free(tcp_timercache, mp); 26844 } 26845 } 26846 26847 /* 26848 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26849 * events there already (currently at most two events are cached). 26850 * If the event is not allocated from the timer cache, free it right away. 26851 */ 26852 static void 26853 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26854 { 26855 mblk_t *mp1 = tcp->tcp_timercache; 26856 tcp_stack_t *tcps = tcp->tcp_tcps; 26857 26858 if (mp->b_wptr != NULL) { 26859 /* 26860 * This allocation is not from a timer cache, free it right 26861 * away. 26862 */ 26863 if (mp->b_wptr != (uchar_t *)-1) 26864 freeb(mp); 26865 else 26866 kmem_free(mp, (size_t)mp->b_datap); 26867 } else if (mp1 == NULL || mp1->b_next == NULL) { 26868 /* Cache this timer block for future allocations */ 26869 mp->b_rptr = (uchar_t *)(&mp[1]); 26870 mp->b_next = mp1; 26871 tcp->tcp_timercache = mp; 26872 } else { 26873 kmem_cache_free(tcp_timercache, mp); 26874 TCP_DBGSTAT(tcps, tcp_timermp_freed); 26875 } 26876 } 26877 26878 /* 26879 * End of TCP Timers implementation. 26880 */ 26881 26882 /* 26883 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26884 * on the specified backing STREAMS q. Note, the caller may make the 26885 * decision to call based on the tcp_t.tcp_flow_stopped value which 26886 * when check outside the q's lock is only an advisory check ... 26887 */ 26888 26889 void 26890 tcp_setqfull(tcp_t *tcp) 26891 { 26892 queue_t *q = tcp->tcp_wq; 26893 tcp_stack_t *tcps = tcp->tcp_tcps; 26894 26895 if (!(q->q_flag & QFULL)) { 26896 mutex_enter(QLOCK(q)); 26897 if (!(q->q_flag & QFULL)) { 26898 /* still need to set QFULL */ 26899 q->q_flag |= QFULL; 26900 tcp->tcp_flow_stopped = B_TRUE; 26901 mutex_exit(QLOCK(q)); 26902 TCP_STAT(tcps, tcp_flwctl_on); 26903 } else { 26904 mutex_exit(QLOCK(q)); 26905 } 26906 } 26907 } 26908 26909 void 26910 tcp_clrqfull(tcp_t *tcp) 26911 { 26912 queue_t *q = tcp->tcp_wq; 26913 26914 if (q->q_flag & QFULL) { 26915 mutex_enter(QLOCK(q)); 26916 if (q->q_flag & QFULL) { 26917 q->q_flag &= ~QFULL; 26918 tcp->tcp_flow_stopped = B_FALSE; 26919 mutex_exit(QLOCK(q)); 26920 if (q->q_flag & QWANTW) 26921 qbackenable(q, 0); 26922 } else { 26923 mutex_exit(QLOCK(q)); 26924 } 26925 } 26926 } 26927 26928 26929 /* 26930 * kstats related to squeues i.e. not per IP instance 26931 */ 26932 static void * 26933 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26934 { 26935 kstat_t *ksp; 26936 26937 tcp_g_stat_t template = { 26938 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26939 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26940 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26941 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26942 }; 26943 26944 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26945 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26946 KSTAT_FLAG_VIRTUAL); 26947 26948 if (ksp == NULL) 26949 return (NULL); 26950 26951 bcopy(&template, tcp_g_statp, sizeof (template)); 26952 ksp->ks_data = (void *)tcp_g_statp; 26953 26954 kstat_install(ksp); 26955 return (ksp); 26956 } 26957 26958 static void 26959 tcp_g_kstat_fini(kstat_t *ksp) 26960 { 26961 if (ksp != NULL) { 26962 kstat_delete(ksp); 26963 } 26964 } 26965 26966 26967 static void * 26968 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26969 { 26970 kstat_t *ksp; 26971 26972 tcp_stat_t template = { 26973 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26974 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26975 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26976 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26977 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26978 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26979 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26980 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26981 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26982 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26983 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26984 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26985 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26986 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26987 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26988 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26989 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26990 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26991 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26992 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26993 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26994 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26995 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26996 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26997 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26998 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26999 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 27000 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 27001 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 27002 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 27003 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 27004 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 27005 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 27006 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 27007 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 27008 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 27009 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 27010 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 27011 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 27012 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 27013 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 27014 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 27015 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 27016 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 27017 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 27018 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 27019 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 27020 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 27021 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 27022 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 27023 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 27024 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 27025 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 27026 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 27027 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 27028 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 27029 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 27030 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 27031 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 27032 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 27033 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 27034 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 27035 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 27036 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 27037 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 27038 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 27039 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 27040 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 27041 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 27042 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 27043 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 27044 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 27045 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 27046 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 27047 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 27048 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 27049 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 27050 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 27051 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 27052 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 27053 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 27054 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 27055 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 27056 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 27057 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 27058 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 27059 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 27060 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 27061 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 27062 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 27063 }; 27064 27065 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 27066 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 27067 KSTAT_FLAG_VIRTUAL, stackid); 27068 27069 if (ksp == NULL) 27070 return (NULL); 27071 27072 bcopy(&template, tcps_statisticsp, sizeof (template)); 27073 ksp->ks_data = (void *)tcps_statisticsp; 27074 ksp->ks_private = (void *)(uintptr_t)stackid; 27075 27076 kstat_install(ksp); 27077 return (ksp); 27078 } 27079 27080 static void 27081 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 27082 { 27083 if (ksp != NULL) { 27084 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 27085 kstat_delete_netstack(ksp, stackid); 27086 } 27087 } 27088 27089 /* 27090 * TCP Kstats implementation 27091 */ 27092 static void * 27093 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 27094 { 27095 kstat_t *ksp; 27096 27097 tcp_named_kstat_t template = { 27098 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 27099 { "rtoMin", KSTAT_DATA_INT32, 0 }, 27100 { "rtoMax", KSTAT_DATA_INT32, 0 }, 27101 { "maxConn", KSTAT_DATA_INT32, 0 }, 27102 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 27103 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 27104 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 27105 { "estabResets", KSTAT_DATA_UINT32, 0 }, 27106 { "currEstab", KSTAT_DATA_UINT32, 0 }, 27107 { "inSegs", KSTAT_DATA_UINT64, 0 }, 27108 { "outSegs", KSTAT_DATA_UINT64, 0 }, 27109 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 27110 { "connTableSize", KSTAT_DATA_INT32, 0 }, 27111 { "outRsts", KSTAT_DATA_UINT32, 0 }, 27112 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 27113 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 27114 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 27115 { "outAck", KSTAT_DATA_UINT32, 0 }, 27116 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 27117 { "outUrg", KSTAT_DATA_UINT32, 0 }, 27118 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 27119 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 27120 { "outControl", KSTAT_DATA_UINT32, 0 }, 27121 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 27122 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 27123 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 27124 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 27125 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 27126 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 27127 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 27128 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 27129 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 27130 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 27131 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 27132 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 27133 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 27134 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 27135 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 27136 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 27137 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 27138 { "inClosed", KSTAT_DATA_UINT32, 0 }, 27139 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 27140 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 27141 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 27142 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 27143 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 27144 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 27145 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 27146 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 27147 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 27148 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 27149 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 27150 { "connTableSize6", KSTAT_DATA_INT32, 0 } 27151 }; 27152 27153 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 27154 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 27155 27156 if (ksp == NULL) 27157 return (NULL); 27158 27159 template.rtoAlgorithm.value.ui32 = 4; 27160 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 27161 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 27162 template.maxConn.value.i32 = -1; 27163 27164 bcopy(&template, ksp->ks_data, sizeof (template)); 27165 ksp->ks_update = tcp_kstat_update; 27166 ksp->ks_private = (void *)(uintptr_t)stackid; 27167 27168 kstat_install(ksp); 27169 return (ksp); 27170 } 27171 27172 static void 27173 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 27174 { 27175 if (ksp != NULL) { 27176 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 27177 kstat_delete_netstack(ksp, stackid); 27178 } 27179 } 27180 27181 static int 27182 tcp_kstat_update(kstat_t *kp, int rw) 27183 { 27184 tcp_named_kstat_t *tcpkp; 27185 tcp_t *tcp; 27186 connf_t *connfp; 27187 conn_t *connp; 27188 int i; 27189 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 27190 netstack_t *ns; 27191 tcp_stack_t *tcps; 27192 ip_stack_t *ipst; 27193 27194 if ((kp == NULL) || (kp->ks_data == NULL)) 27195 return (EIO); 27196 27197 if (rw == KSTAT_WRITE) 27198 return (EACCES); 27199 27200 ns = netstack_find_by_stackid(stackid); 27201 if (ns == NULL) 27202 return (-1); 27203 tcps = ns->netstack_tcp; 27204 if (tcps == NULL) { 27205 netstack_rele(ns); 27206 return (-1); 27207 } 27208 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 27209 27210 tcpkp->currEstab.value.ui32 = 0; 27211 27212 ipst = ns->netstack_ip; 27213 27214 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 27215 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 27216 connp = NULL; 27217 while ((connp = 27218 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 27219 tcp = connp->conn_tcp; 27220 switch (tcp_snmp_state(tcp)) { 27221 case MIB2_TCP_established: 27222 case MIB2_TCP_closeWait: 27223 tcpkp->currEstab.value.ui32++; 27224 break; 27225 } 27226 } 27227 } 27228 27229 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 27230 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 27231 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 27232 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 27233 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 27234 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 27235 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 27236 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 27237 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 27238 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 27239 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 27240 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 27241 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 27242 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 27243 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 27244 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 27245 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 27246 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 27247 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 27248 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 27249 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 27250 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 27251 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 27252 tcpkp->inDataInorderSegs.value.ui32 = 27253 tcps->tcps_mib.tcpInDataInorderSegs; 27254 tcpkp->inDataInorderBytes.value.ui32 = 27255 tcps->tcps_mib.tcpInDataInorderBytes; 27256 tcpkp->inDataUnorderSegs.value.ui32 = 27257 tcps->tcps_mib.tcpInDataUnorderSegs; 27258 tcpkp->inDataUnorderBytes.value.ui32 = 27259 tcps->tcps_mib.tcpInDataUnorderBytes; 27260 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 27261 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 27262 tcpkp->inDataPartDupSegs.value.ui32 = 27263 tcps->tcps_mib.tcpInDataPartDupSegs; 27264 tcpkp->inDataPartDupBytes.value.ui32 = 27265 tcps->tcps_mib.tcpInDataPartDupBytes; 27266 tcpkp->inDataPastWinSegs.value.ui32 = 27267 tcps->tcps_mib.tcpInDataPastWinSegs; 27268 tcpkp->inDataPastWinBytes.value.ui32 = 27269 tcps->tcps_mib.tcpInDataPastWinBytes; 27270 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 27271 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 27272 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 27273 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 27274 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 27275 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 27276 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 27277 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 27278 tcpkp->timKeepaliveProbe.value.ui32 = 27279 tcps->tcps_mib.tcpTimKeepaliveProbe; 27280 tcpkp->timKeepaliveDrop.value.ui32 = 27281 tcps->tcps_mib.tcpTimKeepaliveDrop; 27282 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 27283 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 27284 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 27285 tcpkp->outSackRetransSegs.value.ui32 = 27286 tcps->tcps_mib.tcpOutSackRetransSegs; 27287 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 27288 27289 netstack_rele(ns); 27290 return (0); 27291 } 27292 27293 void 27294 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 27295 { 27296 uint16_t hdr_len; 27297 ipha_t *ipha; 27298 uint8_t *nexthdrp; 27299 tcph_t *tcph; 27300 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 27301 27302 /* Already has an eager */ 27303 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 27304 TCP_STAT(tcps, tcp_reinput_syn); 27305 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 27306 connp, SQTAG_TCP_REINPUT_EAGER); 27307 return; 27308 } 27309 27310 switch (IPH_HDR_VERSION(mp->b_rptr)) { 27311 case IPV4_VERSION: 27312 ipha = (ipha_t *)mp->b_rptr; 27313 hdr_len = IPH_HDR_LENGTH(ipha); 27314 break; 27315 case IPV6_VERSION: 27316 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 27317 &hdr_len, &nexthdrp)) { 27318 CONN_DEC_REF(connp); 27319 freemsg(mp); 27320 return; 27321 } 27322 break; 27323 } 27324 27325 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 27326 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 27327 mp->b_datap->db_struioflag |= STRUIO_EAGER; 27328 DB_CKSUMSTART(mp) = (intptr_t)sqp; 27329 } 27330 27331 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 27332 SQTAG_TCP_REINPUT); 27333 } 27334 27335 static squeue_func_t 27336 tcp_squeue_switch(int val) 27337 { 27338 squeue_func_t rval = squeue_fill; 27339 27340 switch (val) { 27341 case 1: 27342 rval = squeue_enter_nodrain; 27343 break; 27344 case 2: 27345 rval = squeue_enter; 27346 break; 27347 default: 27348 break; 27349 } 27350 return (rval); 27351 } 27352 27353 /* 27354 * This is called once for each squeue - globally for all stack 27355 * instances. 27356 */ 27357 static void 27358 tcp_squeue_add(squeue_t *sqp) 27359 { 27360 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 27361 sizeof (tcp_squeue_priv_t), KM_SLEEP); 27362 27363 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 27364 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 27365 sqp, TCP_TIME_WAIT_DELAY); 27366 if (tcp_free_list_max_cnt == 0) { 27367 int tcp_ncpus = ((boot_max_ncpus == -1) ? 27368 max_ncpus : boot_max_ncpus); 27369 27370 /* 27371 * Limit number of entries to 1% of availble memory / tcp_ncpus 27372 */ 27373 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 27374 (tcp_ncpus * sizeof (tcp_t) * 100); 27375 } 27376 tcp_time_wait->tcp_free_list_cnt = 0; 27377 } 27378