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 #include <sys/types.h> 29 #include <sys/stream.h> 30 #include <sys/strsun.h> 31 #include <sys/strsubr.h> 32 #include <sys/stropts.h> 33 #include <sys/strlog.h> 34 #include <sys/strsun.h> 35 #define _SUN_TPI_VERSION 2 36 #include <sys/tihdr.h> 37 #include <sys/timod.h> 38 #include <sys/ddi.h> 39 #include <sys/sunddi.h> 40 #include <sys/suntpi.h> 41 #include <sys/xti_inet.h> 42 #include <sys/cmn_err.h> 43 #include <sys/debug.h> 44 #include <sys/sdt.h> 45 #include <sys/vtrace.h> 46 #include <sys/kmem.h> 47 #include <sys/ethernet.h> 48 #include <sys/cpuvar.h> 49 #include <sys/dlpi.h> 50 #include <sys/multidata.h> 51 #include <sys/multidata_impl.h> 52 #include <sys/pattr.h> 53 #include <sys/policy.h> 54 #include <sys/priv.h> 55 #include <sys/zone.h> 56 #include <sys/sunldi.h> 57 58 #include <sys/errno.h> 59 #include <sys/signal.h> 60 #include <sys/socket.h> 61 #include <sys/sockio.h> 62 #include <sys/isa_defs.h> 63 #include <sys/md5.h> 64 #include <sys/random.h> 65 #include <sys/sodirect.h> 66 #include <sys/uio.h> 67 #include <sys/systm.h> 68 #include <netinet/in.h> 69 #include <netinet/tcp.h> 70 #include <netinet/ip6.h> 71 #include <netinet/icmp6.h> 72 #include <net/if.h> 73 #include <net/route.h> 74 #include <inet/ipsec_impl.h> 75 76 #include <inet/common.h> 77 #include <inet/ip.h> 78 #include <inet/ip_impl.h> 79 #include <inet/ip6.h> 80 #include <inet/ip_ndp.h> 81 #include <inet/mi.h> 82 #include <inet/mib2.h> 83 #include <inet/nd.h> 84 #include <inet/optcom.h> 85 #include <inet/snmpcom.h> 86 #include <inet/kstatcom.h> 87 #include <inet/tcp.h> 88 #include <inet/tcp_impl.h> 89 #include <net/pfkeyv2.h> 90 #include <inet/ipsec_info.h> 91 #include <inet/ipdrop.h> 92 93 #include <inet/ipclassifier.h> 94 #include <inet/ip_ire.h> 95 #include <inet/ip_ftable.h> 96 #include <inet/ip_if.h> 97 #include <inet/ipp_common.h> 98 #include <inet/ip_netinfo.h> 99 #include <sys/squeue.h> 100 #include <inet/kssl/ksslapi.h> 101 #include <sys/tsol/label.h> 102 #include <sys/tsol/tnet.h> 103 #include <rpc/pmap_prot.h> 104 #include <sys/callo.h> 105 106 /* 107 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433) 108 * 109 * (Read the detailed design doc in PSARC case directory) 110 * 111 * The entire tcp state is contained in tcp_t and conn_t structure 112 * which are allocated in tandem using ipcl_conn_create() and passing 113 * IPCL_CONNTCP as a flag. We use 'conn_ref' and 'conn_lock' to protect 114 * the references on the tcp_t. The tcp_t structure is never compressed 115 * and packets always land on the correct TCP perimeter from the time 116 * eager is created till the time tcp_t dies (as such the old mentat 117 * TCP global queue is not used for detached state and no IPSEC checking 118 * is required). The global queue is still allocated to send out resets 119 * for connection which have no listeners and IP directly calls 120 * tcp_xmit_listeners_reset() which does any policy check. 121 * 122 * Protection and Synchronisation mechanism: 123 * 124 * The tcp data structure does not use any kind of lock for protecting 125 * its state but instead uses 'squeues' for mutual exclusion from various 126 * read and write side threads. To access a tcp member, the thread should 127 * always be behind squeue (via squeue_enter, squeue_enter_nodrain, or 128 * squeue_fill). Since the squeues allow a direct function call, caller 129 * can pass any tcp function having prototype of edesc_t as argument 130 * (different from traditional STREAMs model where packets come in only 131 * designated entry points). The list of functions that can be directly 132 * called via squeue are listed before the usual function prototype. 133 * 134 * Referencing: 135 * 136 * TCP is MT-Hot and we use a reference based scheme to make sure that the 137 * tcp structure doesn't disappear when its needed. When the application 138 * creates an outgoing connection or accepts an incoming connection, we 139 * start out with 2 references on 'conn_ref'. One for TCP and one for IP. 140 * The IP reference is just a symbolic reference since ip_tcpclose() 141 * looks at tcp structure after tcp_close_output() returns which could 142 * have dropped the last TCP reference. So as long as the connection is 143 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the 144 * conn_t. The classifier puts its own reference when the connection is 145 * inserted in listen or connected hash. Anytime a thread needs to enter 146 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr 147 * on write side or by doing a classify on read side and then puts a 148 * reference on the conn before doing squeue_enter/tryenter/fill. For 149 * read side, the classifier itself puts the reference under fanout lock 150 * to make sure that tcp can't disappear before it gets processed. The 151 * squeue will drop this reference automatically so the called function 152 * doesn't have to do a DEC_REF. 153 * 154 * Opening a new connection: 155 * 156 * The outgoing connection open is pretty simple. tcp_open() does the 157 * work in creating the conn/tcp structure and initializing it. The 158 * squeue assignment is done based on the CPU the application 159 * is running on. So for outbound connections, processing is always done 160 * on application CPU which might be different from the incoming CPU 161 * being interrupted by the NIC. An optimal way would be to figure out 162 * the NIC <-> CPU binding at listen time, and assign the outgoing 163 * connection to the squeue attached to the CPU that will be interrupted 164 * for incoming packets (we know the NIC based on the bind IP address). 165 * This might seem like a problem if more data is going out but the 166 * fact is that in most cases the transmit is ACK driven transmit where 167 * the outgoing data normally sits on TCP's xmit queue waiting to be 168 * transmitted. 169 * 170 * Accepting a connection: 171 * 172 * This is a more interesting case because of various races involved in 173 * establishing a eager in its own perimeter. Read the meta comment on 174 * top of tcp_conn_request(). But briefly, the squeue is picked by 175 * ip_tcp_input()/ip_fanout_tcp_v6() based on the interrupted CPU. 176 * 177 * Closing a connection: 178 * 179 * The close is fairly straight forward. tcp_close() calls tcp_close_output() 180 * via squeue to do the close and mark the tcp as detached if the connection 181 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its 182 * reference but tcp_close() drop IP's reference always. So if tcp was 183 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP 184 * and 1 because it is in classifier's connected hash. This is the condition 185 * we use to determine that its OK to clean up the tcp outside of squeue 186 * when time wait expires (check the ref under fanout and conn_lock and 187 * if it is 2, remove it from fanout hash and kill it). 188 * 189 * Although close just drops the necessary references and marks the 190 * tcp_detached state, tcp_close needs to know the tcp_detached has been 191 * set (under squeue) before letting the STREAM go away (because a 192 * inbound packet might attempt to go up the STREAM while the close 193 * has happened and tcp_detached is not set). So a special lock and 194 * flag is used along with a condition variable (tcp_closelock, tcp_closed, 195 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked 196 * tcp_detached. 197 * 198 * Special provisions and fast paths: 199 * 200 * We make special provision for (AF_INET, SOCK_STREAM) sockets which 201 * can't have 'ipv6_recvpktinfo' set and for these type of sockets, IP 202 * will never send a M_CTL to TCP. As such, ip_tcp_input() which handles 203 * all TCP packets from the wire makes a IPCL_IS_TCP4_CONNECTED_NO_POLICY 204 * check to send packets directly to tcp_rput_data via squeue. Everyone 205 * else comes through tcp_input() on the read side. 206 * 207 * We also make special provisions for sockfs by marking tcp_issocket 208 * whenever we have only sockfs on top of TCP. This allows us to skip 209 * putting the tcp in acceptor hash since a sockfs listener can never 210 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM 211 * since eager has already been allocated and the accept now happens 212 * on acceptor STREAM. There is a big blob of comment on top of 213 * tcp_conn_request explaining the new accept. When socket is POP'd, 214 * sockfs sends us an ioctl to mark the fact and we go back to old 215 * behaviour. Once tcp_issocket is unset, its never set for the 216 * life of that connection. 217 * 218 * In support of on-board asynchronous DMA hardware (e.g. Intel I/OAT) 219 * two consoldiation private KAPIs are used to enqueue M_DATA mblk_t's 220 * directly to the socket (sodirect) and start an asynchronous copyout 221 * to a user-land receive-side buffer (uioa) when a blocking socket read 222 * (e.g. read, recv, ...) is pending. 223 * 224 * This is accomplished when tcp_issocket is set and tcp_sodirect is not 225 * NULL so points to an sodirect_t and if marked enabled then we enqueue 226 * all mblk_t's directly to the socket. 227 * 228 * Further, if the sodirect_t sod_uioa and if marked enabled (due to a 229 * blocking socket read, e.g. user-land read, recv, ...) then an asynchronous 230 * copyout will be started directly to the user-land uio buffer. Also, as we 231 * have a pending read, TCP's push logic can take into account the number of 232 * bytes to be received and only awake the blocked read()er when the uioa_t 233 * byte count has been satisfied. 234 * 235 * IPsec notes : 236 * 237 * Since a packet is always executed on the correct TCP perimeter 238 * all IPsec processing is defered to IP including checking new 239 * connections and setting IPSEC policies for new connection. The 240 * only exception is tcp_xmit_listeners_reset() which is called 241 * directly from IP and needs to policy check to see if TH_RST 242 * can be sent out. 243 * 244 * PFHooks notes : 245 * 246 * For mdt case, one meta buffer contains multiple packets. Mblks for every 247 * packet are assembled and passed to the hooks. When packets are blocked, 248 * or boundary of any packet is changed, the mdt processing is stopped, and 249 * packets of the meta buffer are send to the IP path one by one. 250 */ 251 252 /* 253 * Values for squeue switch: 254 * 1: squeue_enter_nodrain 255 * 2: squeue_enter 256 * 3: squeue_fill 257 */ 258 int tcp_squeue_close = 2; /* Setable in /etc/system */ 259 int tcp_squeue_wput = 2; 260 261 squeue_func_t tcp_squeue_close_proc; 262 squeue_func_t tcp_squeue_wput_proc; 263 264 /* 265 * Macros for sodirect: 266 * 267 * SOD_PTR_ENTER(tcp, sodp) - for the tcp_t pointer "tcp" set the 268 * sodirect_t pointer "sodp" to the socket/tcp shared sodirect_t 269 * if it exists and is enabled, else to NULL. Note, in the current 270 * sodirect implementation the sod_lockp must not be held across any 271 * STREAMS call (e.g. putnext) else a "recursive mutex_enter" PANIC 272 * will result as sod_lockp is the streamhead stdata.sd_lock. 273 * 274 * SOD_NOT_ENABLED(tcp) - return true if not a sodirect tcp_t or the 275 * sodirect_t isn't enabled, usefull for ASSERT()ing that a recieve 276 * side tcp code path dealing with a tcp_rcv_list or putnext() isn't 277 * being used when sodirect code paths should be. 278 */ 279 280 #define SOD_PTR_ENTER(tcp, sodp) \ 281 (sodp) = (tcp)->tcp_sodirect; \ 282 \ 283 if ((sodp) != NULL) { \ 284 mutex_enter((sodp)->sod_lockp); \ 285 if (!((sodp)->sod_state & SOD_ENABLED)) { \ 286 mutex_exit((sodp)->sod_lockp); \ 287 (sodp) = NULL; \ 288 } \ 289 } 290 291 #define SOD_NOT_ENABLED(tcp) \ 292 ((tcp)->tcp_sodirect == NULL || \ 293 !((tcp)->tcp_sodirect->sod_state & SOD_ENABLED)) 294 295 /* 296 * This controls how tiny a write must be before we try to copy it 297 * into the the mblk on the tail of the transmit queue. Not much 298 * speedup is observed for values larger than sixteen. Zero will 299 * disable the optimisation. 300 */ 301 int tcp_tx_pull_len = 16; 302 303 /* 304 * TCP Statistics. 305 * 306 * How TCP statistics work. 307 * 308 * There are two types of statistics invoked by two macros. 309 * 310 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 311 * supposed to be used in non MT-hot paths of the code. 312 * 313 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 314 * supposed to be used for DEBUG purposes and may be used on a hot path. 315 * 316 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 317 * (use "kstat tcp" to get them). 318 * 319 * There is also additional debugging facility that marks tcp_clean_death() 320 * instances and saves them in tcp_t structure. It is triggered by 321 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 322 * tcp_clean_death() calls that counts the number of times each tag was hit. It 323 * is triggered by TCP_CLD_COUNTERS define. 324 * 325 * How to add new counters. 326 * 327 * 1) Add a field in the tcp_stat structure describing your counter. 328 * 2) Add a line in the template in tcp_kstat2_init() with the name 329 * of the counter. 330 * 331 * IMPORTANT!! - make sure that both are in sync !! 332 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 333 * 334 * Please avoid using private counters which are not kstat-exported. 335 * 336 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 337 * in tcp_t structure. 338 * 339 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 340 */ 341 342 #ifndef TCP_DEBUG_COUNTER 343 #ifdef DEBUG 344 #define TCP_DEBUG_COUNTER 1 345 #else 346 #define TCP_DEBUG_COUNTER 0 347 #endif 348 #endif 349 350 #define TCP_CLD_COUNTERS 0 351 352 #define TCP_TAG_CLEAN_DEATH 1 353 #define TCP_MAX_CLEAN_DEATH_TAG 32 354 355 #ifdef lint 356 static int _lint_dummy_; 357 #endif 358 359 #if TCP_CLD_COUNTERS 360 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 361 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 362 #elif defined(lint) 363 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 364 #else 365 #define TCP_CLD_STAT(x) 366 #endif 367 368 #if TCP_DEBUG_COUNTER 369 #define TCP_DBGSTAT(tcps, x) \ 370 atomic_add_64(&((tcps)->tcps_statistics.x.value.ui64), 1) 371 #define TCP_G_DBGSTAT(x) \ 372 atomic_add_64(&(tcp_g_statistics.x.value.ui64), 1) 373 #elif defined(lint) 374 #define TCP_DBGSTAT(tcps, x) ASSERT(_lint_dummy_ == 0); 375 #define TCP_G_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 376 #else 377 #define TCP_DBGSTAT(tcps, x) 378 #define TCP_G_DBGSTAT(x) 379 #endif 380 381 #define TCP_G_STAT(x) (tcp_g_statistics.x.value.ui64++) 382 383 tcp_g_stat_t tcp_g_statistics; 384 kstat_t *tcp_g_kstat; 385 386 /* 387 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 388 * tcp write side. 389 */ 390 #define CALL_IP_WPUT(connp, q, mp) { \ 391 tcp_stack_t *tcps; \ 392 \ 393 tcps = connp->conn_netstack->netstack_tcp; \ 394 ASSERT(((q)->q_flag & QREADR) == 0); \ 395 TCP_DBGSTAT(tcps, tcp_ip_output); \ 396 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 397 } 398 399 /* Macros for timestamp comparisons */ 400 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 401 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 402 403 /* 404 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 405 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 406 * by adding three components: a time component which grows by 1 every 4096 407 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 408 * a per-connection component which grows by 125000 for every new connection; 409 * and an "extra" component that grows by a random amount centered 410 * approximately on 64000. This causes the the ISS generator to cycle every 411 * 4.89 hours if no TCP connections are made, and faster if connections are 412 * made. 413 * 414 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 415 * components: a time component which grows by 250000 every second; and 416 * a per-connection component which grows by 125000 for every new connections. 417 * 418 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 419 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 420 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 421 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 422 * password. 423 */ 424 #define ISS_INCR 250000 425 #define ISS_NSEC_SHT 12 426 427 static sin_t sin_null; /* Zero address for quick clears */ 428 static sin6_t sin6_null; /* Zero address for quick clears */ 429 430 /* 431 * This implementation follows the 4.3BSD interpretation of the urgent 432 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 433 * incompatible changes in protocols like telnet and rlogin. 434 */ 435 #define TCP_OLD_URP_INTERPRETATION 1 436 437 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 438 (TCP_IS_DETACHED(tcp) && \ 439 (!(tcp)->tcp_hard_binding)) 440 441 /* 442 * TCP reassembly macros. We hide starting and ending sequence numbers in 443 * b_next and b_prev of messages on the reassembly queue. The messages are 444 * chained using b_cont. These macros are used in tcp_reass() so we don't 445 * have to see the ugly casts and assignments. 446 */ 447 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 448 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 449 (mblk_t *)(uintptr_t)(u)) 450 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 451 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 452 (mblk_t *)(uintptr_t)(u)) 453 454 /* 455 * Implementation of TCP Timers. 456 * ============================= 457 * 458 * INTERFACE: 459 * 460 * There are two basic functions dealing with tcp timers: 461 * 462 * timeout_id_t tcp_timeout(connp, func, time) 463 * clock_t tcp_timeout_cancel(connp, timeout_id) 464 * TCP_TIMER_RESTART(tcp, intvl) 465 * 466 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 467 * after 'time' ticks passed. The function called by timeout() must adhere to 468 * the same restrictions as a driver soft interrupt handler - it must not sleep 469 * or call other functions that might sleep. The value returned is the opaque 470 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 471 * cancel the request. The call to tcp_timeout() may fail in which case it 472 * returns zero. This is different from the timeout(9F) function which never 473 * fails. 474 * 475 * The call-back function 'func' always receives 'connp' as its single 476 * argument. It is always executed in the squeue corresponding to the tcp 477 * structure. The tcp structure is guaranteed to be present at the time the 478 * call-back is called. 479 * 480 * NOTE: The call-back function 'func' is never called if tcp is in 481 * the TCPS_CLOSED state. 482 * 483 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 484 * request. locks acquired by the call-back routine should not be held across 485 * the call to tcp_timeout_cancel() or a deadlock may result. 486 * 487 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 488 * Otherwise, it returns an integer value greater than or equal to 0. In 489 * particular, if the call-back function is already placed on the squeue, it can 490 * not be canceled. 491 * 492 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 493 * within squeue context corresponding to the tcp instance. Since the 494 * call-back is also called via the same squeue, there are no race 495 * conditions described in untimeout(9F) manual page since all calls are 496 * strictly serialized. 497 * 498 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 499 * stored in tcp_timer_tid and starts a new one using 500 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 501 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 502 * field. 503 * 504 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 505 * call-back may still be called, so it is possible tcp_timer() will be 506 * called several times. This should not be a problem since tcp_timer() 507 * should always check the tcp instance state. 508 * 509 * 510 * IMPLEMENTATION: 511 * 512 * TCP timers are implemented using three-stage process. The call to 513 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 514 * when the timer expires. The tcp_timer_callback() arranges the call of the 515 * tcp_timer_handler() function via squeue corresponding to the tcp 516 * instance. The tcp_timer_handler() calls actual requested timeout call-back 517 * and passes tcp instance as an argument to it. Information is passed between 518 * stages using the tcp_timer_t structure which contains the connp pointer, the 519 * tcp call-back to call and the timeout id returned by the timeout(9F). 520 * 521 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 522 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 523 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 524 * returns the pointer to this mblk. 525 * 526 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 527 * looks like a normal mblk without actual dblk attached to it. 528 * 529 * To optimize performance each tcp instance holds a small cache of timer 530 * mblocks. In the current implementation it caches up to two timer mblocks per 531 * tcp instance. The cache is preserved over tcp frees and is only freed when 532 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 533 * timer processing happens on a corresponding squeue, the cache manipulation 534 * does not require any locks. Experiments show that majority of timer mblocks 535 * allocations are satisfied from the tcp cache and do not involve kmem calls. 536 * 537 * The tcp_timeout() places a refhold on the connp instance which guarantees 538 * that it will be present at the time the call-back function fires. The 539 * tcp_timer_handler() drops the reference after calling the call-back, so the 540 * call-back function does not need to manipulate the references explicitly. 541 */ 542 543 typedef struct tcp_timer_s { 544 conn_t *connp; 545 void (*tcpt_proc)(void *); 546 callout_id_t tcpt_tid; 547 } tcp_timer_t; 548 549 static kmem_cache_t *tcp_timercache; 550 kmem_cache_t *tcp_sack_info_cache; 551 kmem_cache_t *tcp_iphc_cache; 552 553 /* 554 * For scalability, we must not run a timer for every TCP connection 555 * in TIME_WAIT state. To see why, consider (for time wait interval of 556 * 4 minutes): 557 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 558 * 559 * This list is ordered by time, so you need only delete from the head 560 * until you get to entries which aren't old enough to delete yet. 561 * The list consists of only the detached TIME_WAIT connections. 562 * 563 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 564 * becomes detached TIME_WAIT (either by changing the state and already 565 * being detached or the other way around). This means that the TIME_WAIT 566 * state can be extended (up to doubled) if the connection doesn't become 567 * detached for a long time. 568 * 569 * The list manipulations (including tcp_time_wait_next/prev) 570 * are protected by the tcp_time_wait_lock. The content of the 571 * detached TIME_WAIT connections is protected by the normal perimeters. 572 * 573 * This list is per squeue and squeues are shared across the tcp_stack_t's. 574 * Things on tcp_time_wait_head remain associated with the tcp_stack_t 575 * and conn_netstack. 576 * The tcp_t's that are added to tcp_free_list are disassociated and 577 * have NULL tcp_tcps and conn_netstack pointers. 578 */ 579 typedef struct tcp_squeue_priv_s { 580 kmutex_t tcp_time_wait_lock; 581 callout_id_t tcp_time_wait_tid; 582 tcp_t *tcp_time_wait_head; 583 tcp_t *tcp_time_wait_tail; 584 tcp_t *tcp_free_list; 585 uint_t tcp_free_list_cnt; 586 } tcp_squeue_priv_t; 587 588 /* 589 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 590 * Running it every 5 seconds seems to give the best results. 591 */ 592 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 593 594 /* 595 * To prevent memory hog, limit the number of entries in tcp_free_list 596 * to 1% of available memory / number of cpus 597 */ 598 uint_t tcp_free_list_max_cnt = 0; 599 600 #define TCP_XMIT_LOWATER 4096 601 #define TCP_XMIT_HIWATER 49152 602 #define TCP_RECV_LOWATER 2048 603 #define TCP_RECV_HIWATER 49152 604 605 /* 606 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 607 */ 608 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 609 610 #define TIDUSZ 4096 /* transport interface data unit size */ 611 612 /* 613 * Bind hash list size and has function. It has to be a power of 2 for 614 * hashing. 615 */ 616 #define TCP_BIND_FANOUT_SIZE 512 617 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 618 /* 619 * Size of listen and acceptor hash list. It has to be a power of 2 for 620 * hashing. 621 */ 622 #define TCP_FANOUT_SIZE 256 623 624 #ifdef _ILP32 625 #define TCP_ACCEPTOR_HASH(accid) \ 626 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 627 #else 628 #define TCP_ACCEPTOR_HASH(accid) \ 629 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 630 #endif /* _ILP32 */ 631 632 #define IP_ADDR_CACHE_SIZE 2048 633 #define IP_ADDR_CACHE_HASH(faddr) \ 634 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 635 636 /* Hash for HSPs uses all 32 bits, since both networks and hosts are in table */ 637 #define TCP_HSP_HASH_SIZE 256 638 639 #define TCP_HSP_HASH(addr) \ 640 (((addr>>24) ^ (addr >>16) ^ \ 641 (addr>>8) ^ (addr)) % TCP_HSP_HASH_SIZE) 642 643 /* 644 * TCP options struct returned from tcp_parse_options. 645 */ 646 typedef struct tcp_opt_s { 647 uint32_t tcp_opt_mss; 648 uint32_t tcp_opt_wscale; 649 uint32_t tcp_opt_ts_val; 650 uint32_t tcp_opt_ts_ecr; 651 tcp_t *tcp; 652 } tcp_opt_t; 653 654 /* 655 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 656 */ 657 658 #ifdef _BIG_ENDIAN 659 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 660 (TCPOPT_TSTAMP << 8) | 10) 661 #else 662 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 663 (TCPOPT_NOP << 8) | TCPOPT_NOP) 664 #endif 665 666 /* 667 * Flags returned from tcp_parse_options. 668 */ 669 #define TCP_OPT_MSS_PRESENT 1 670 #define TCP_OPT_WSCALE_PRESENT 2 671 #define TCP_OPT_TSTAMP_PRESENT 4 672 #define TCP_OPT_SACK_OK_PRESENT 8 673 #define TCP_OPT_SACK_PRESENT 16 674 675 /* TCP option length */ 676 #define TCPOPT_NOP_LEN 1 677 #define TCPOPT_MAXSEG_LEN 4 678 #define TCPOPT_WS_LEN 3 679 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 680 #define TCPOPT_TSTAMP_LEN 10 681 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 682 #define TCPOPT_SACK_OK_LEN 2 683 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 684 #define TCPOPT_REAL_SACK_LEN 4 685 #define TCPOPT_MAX_SACK_LEN 36 686 #define TCPOPT_HEADER_LEN 2 687 688 /* TCP cwnd burst factor. */ 689 #define TCP_CWND_INFINITE 65535 690 #define TCP_CWND_SS 3 691 #define TCP_CWND_NORMAL 5 692 693 /* Maximum TCP initial cwin (start/restart). */ 694 #define TCP_MAX_INIT_CWND 8 695 696 /* 697 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 698 * either tcp_slow_start_initial or tcp_slow_start_after idle 699 * depending on the caller. If the upper layer has not used the 700 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 701 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 702 * If the upper layer has changed set the tcp_init_cwnd, just use 703 * it to calculate the tcp_cwnd. 704 */ 705 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 706 { \ 707 if ((tcp)->tcp_init_cwnd == 0) { \ 708 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 709 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 710 } else { \ 711 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 712 } \ 713 tcp->tcp_cwnd_cnt = 0; \ 714 } 715 716 /* TCP Timer control structure */ 717 typedef struct tcpt_s { 718 pfv_t tcpt_pfv; /* The routine we are to call */ 719 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 720 } tcpt_t; 721 722 /* Host Specific Parameter structure */ 723 typedef struct tcp_hsp { 724 struct tcp_hsp *tcp_hsp_next; 725 in6_addr_t tcp_hsp_addr_v6; 726 in6_addr_t tcp_hsp_subnet_v6; 727 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 728 int32_t tcp_hsp_sendspace; 729 int32_t tcp_hsp_recvspace; 730 int32_t tcp_hsp_tstamp; 731 } tcp_hsp_t; 732 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 733 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 734 735 /* 736 * Functions called directly via squeue having a prototype of edesc_t. 737 */ 738 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 739 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 740 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 741 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 742 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 743 void tcp_input(void *arg, mblk_t *mp, void *arg2); 744 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 745 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 746 void tcp_output(void *arg, mblk_t *mp, void *arg2); 747 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 748 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 749 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 750 751 752 /* Prototype for TCP functions */ 753 static void tcp_random_init(void); 754 int tcp_random(void); 755 static void tcp_accept(tcp_t *tcp, mblk_t *mp); 756 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 757 tcp_t *eager); 758 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 759 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 760 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 761 boolean_t user_specified); 762 static void tcp_closei_local(tcp_t *tcp); 763 static void tcp_close_detached(tcp_t *tcp); 764 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 765 mblk_t *idmp, mblk_t **defermp); 766 static void tcp_connect(tcp_t *tcp, mblk_t *mp); 767 static void tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, 768 in_port_t dstport, uint_t srcid); 769 static void tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 770 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 771 uint32_t scope_id); 772 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 773 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 774 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 775 static char *tcp_display(tcp_t *tcp, char *, char); 776 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 777 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 778 static void tcp_eager_unlink(tcp_t *tcp); 779 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 780 int unixerr); 781 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 782 int tlierr, int unixerr); 783 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 784 cred_t *cr); 785 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 786 char *value, caddr_t cp, cred_t *cr); 787 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 788 char *value, caddr_t cp, cred_t *cr); 789 static int tcp_tpistate(tcp_t *tcp); 790 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 791 int caller_holds_lock); 792 static void tcp_bind_hash_remove(tcp_t *tcp); 793 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 794 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 795 static void tcp_acceptor_hash_remove(tcp_t *tcp); 796 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 797 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 798 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 799 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 800 void tcp_g_q_setup(tcp_stack_t *); 801 void tcp_g_q_create(tcp_stack_t *); 802 void tcp_g_q_destroy(tcp_stack_t *); 803 static int tcp_header_init_ipv4(tcp_t *tcp); 804 static int tcp_header_init_ipv6(tcp_t *tcp); 805 int tcp_init(tcp_t *tcp, queue_t *q); 806 static int tcp_init_values(tcp_t *tcp); 807 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 808 static mblk_t *tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, 809 t_scalar_t addr_length); 810 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 811 static void tcp_ip_notify(tcp_t *tcp); 812 static mblk_t *tcp_ire_mp(mblk_t *mp); 813 static void tcp_iss_init(tcp_t *tcp); 814 static void tcp_keepalive_killer(void *arg); 815 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 816 static void tcp_mss_set(tcp_t *tcp, uint32_t size, boolean_t do_ss); 817 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 818 int *do_disconnectp, int *t_errorp, int *sys_errorp); 819 static boolean_t tcp_allow_connopt_set(int level, int name); 820 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 821 int tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 822 int tcp_opt_set(queue_t *q, uint_t optset_context, int level, 823 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 824 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 825 mblk_t *mblk); 826 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 827 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 828 uchar_t *ptr, uint_t len); 829 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 830 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 831 tcp_stack_t *); 832 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 833 caddr_t cp, cred_t *cr); 834 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 835 caddr_t cp, cred_t *cr); 836 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 837 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 838 caddr_t cp, cred_t *cr); 839 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 840 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 841 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 842 static void tcp_reinit(tcp_t *tcp); 843 static void tcp_reinit_values(tcp_t *tcp); 844 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 845 tcp_t *thisstream, cred_t *cr); 846 847 static uint_t tcp_rcv_drain(queue_t *q, tcp_t *tcp); 848 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 849 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 850 static void tcp_ss_rexmit(tcp_t *tcp); 851 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 852 static void tcp_process_options(tcp_t *, tcph_t *); 853 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 854 static void tcp_rsrv(queue_t *q); 855 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 856 static int tcp_snmp_state(tcp_t *tcp); 857 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 858 cred_t *cr); 859 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 860 cred_t *cr); 861 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 862 cred_t *cr); 863 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 864 cred_t *cr); 865 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 866 cred_t *cr); 867 static void tcp_timer(void *arg); 868 static void tcp_timer_callback(void *); 869 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 870 boolean_t random); 871 static in_port_t tcp_get_next_priv_port(const tcp_t *); 872 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 873 void tcp_wput_accept(queue_t *q, mblk_t *mp); 874 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 875 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 876 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 877 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 878 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 879 const int num_sack_blk, int *usable, uint_t *snxt, 880 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 881 const int mdt_thres); 882 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 883 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 884 const int num_sack_blk, int *usable, uint_t *snxt, 885 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 886 const int mdt_thres); 887 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 888 int num_sack_blk); 889 static void tcp_wsrv(queue_t *q); 890 static int tcp_xmit_end(tcp_t *tcp); 891 static void tcp_ack_timer(void *arg); 892 static mblk_t *tcp_ack_mp(tcp_t *tcp); 893 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 894 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 895 zoneid_t zoneid, tcp_stack_t *, conn_t *connp); 896 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 897 uint32_t ack, int ctl); 898 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *); 899 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr, tcp_stack_t *); 900 static int setmaxps(queue_t *q, int maxpsz); 901 static void tcp_set_rto(tcp_t *, time_t); 902 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 903 boolean_t, boolean_t); 904 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 905 boolean_t ipsec_mctl); 906 static mblk_t *tcp_setsockopt_mp(int level, int cmd, 907 char *opt, int optlen); 908 static int tcp_build_hdrs(queue_t *, tcp_t *); 909 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 910 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 911 tcph_t *tcph); 912 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 913 static mblk_t *tcp_mdt_info_mp(mblk_t *); 914 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 915 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 916 const boolean_t, const uint32_t, const uint32_t, 917 const uint32_t, const uint32_t, tcp_stack_t *); 918 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 919 const uint_t, const uint_t, boolean_t *); 920 static mblk_t *tcp_lso_info_mp(mblk_t *); 921 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 922 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 923 extern mblk_t *tcp_timermp_alloc(int); 924 extern void tcp_timermp_free(tcp_t *); 925 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 926 static void tcp_stop_lingering(tcp_t *tcp); 927 static void tcp_close_linger_timeout(void *arg); 928 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 929 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 930 static void tcp_stack_fini(netstackid_t stackid, void *arg); 931 static void *tcp_g_kstat_init(tcp_g_stat_t *); 932 static void tcp_g_kstat_fini(kstat_t *); 933 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 934 static void tcp_kstat_fini(netstackid_t, kstat_t *); 935 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 936 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 937 static int tcp_kstat_update(kstat_t *kp, int rw); 938 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 939 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 940 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 941 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 942 tcph_t *tcph, mblk_t *idmp); 943 static squeue_func_t tcp_squeue_switch(int); 944 945 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t); 946 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *); 947 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *); 948 static int tcp_close(queue_t *, int); 949 static int tcpclose_accept(queue_t *); 950 951 static void tcp_squeue_add(squeue_t *); 952 static boolean_t tcp_zcopy_check(tcp_t *); 953 static void tcp_zcopy_notify(tcp_t *); 954 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 955 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 956 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 957 958 extern void tcp_kssl_input(tcp_t *, mblk_t *); 959 960 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 961 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 962 963 /* 964 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 965 * 966 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 967 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 968 * (defined in tcp.h) needs to be filled in and passed into the kernel 969 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 970 * structure contains the four-tuple of a TCP connection and a range of TCP 971 * states (specified by ac_start and ac_end). The use of wildcard addresses 972 * and ports is allowed. Connections with a matching four tuple and a state 973 * within the specified range will be aborted. The valid states for the 974 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 975 * inclusive. 976 * 977 * An application which has its connection aborted by this ioctl will receive 978 * an error that is dependent on the connection state at the time of the abort. 979 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 980 * though a RST packet has been received. If the connection state is equal to 981 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 982 * and all resources associated with the connection will be freed. 983 */ 984 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 985 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 986 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 987 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *, tcp_stack_t *tcps); 988 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 989 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 990 boolean_t, tcp_stack_t *); 991 992 static struct module_info tcp_rinfo = { 993 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 994 }; 995 996 static struct module_info tcp_winfo = { 997 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 998 }; 999 1000 /* 1001 * Entry points for TCP as a device. The normal case which supports 1002 * the TCP functionality. 1003 * We have separate open functions for the /dev/tcp and /dev/tcp6 devices. 1004 */ 1005 struct qinit tcp_rinitv4 = { 1006 NULL, (pfi_t)tcp_rsrv, tcp_openv4, tcp_close, NULL, &tcp_rinfo 1007 }; 1008 1009 struct qinit tcp_rinitv6 = { 1010 NULL, (pfi_t)tcp_rsrv, tcp_openv6, tcp_close, NULL, &tcp_rinfo 1011 }; 1012 1013 struct qinit tcp_winit = { 1014 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1015 }; 1016 1017 /* Initial entry point for TCP in socket mode. */ 1018 struct qinit tcp_sock_winit = { 1019 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1020 }; 1021 1022 /* 1023 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 1024 * an accept. Avoid allocating data structures since eager has already 1025 * been created. 1026 */ 1027 struct qinit tcp_acceptor_rinit = { 1028 NULL, (pfi_t)tcp_rsrv, NULL, tcpclose_accept, NULL, &tcp_winfo 1029 }; 1030 1031 struct qinit tcp_acceptor_winit = { 1032 (pfi_t)tcp_wput_accept, NULL, NULL, NULL, NULL, &tcp_winfo 1033 }; 1034 1035 /* 1036 * Entry points for TCP loopback (read side only) 1037 * The open routine is only used for reopens, thus no need to 1038 * have a separate one for tcp_openv6. 1039 */ 1040 struct qinit tcp_loopback_rinit = { 1041 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_openv4, tcp_close, (pfi_t)0, 1042 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 1043 }; 1044 1045 /* For AF_INET aka /dev/tcp */ 1046 struct streamtab tcpinfov4 = { 1047 &tcp_rinitv4, &tcp_winit 1048 }; 1049 1050 /* For AF_INET6 aka /dev/tcp6 */ 1051 struct streamtab tcpinfov6 = { 1052 &tcp_rinitv6, &tcp_winit 1053 }; 1054 1055 /* 1056 * Have to ensure that tcp_g_q_close is not done by an 1057 * interrupt thread. 1058 */ 1059 static taskq_t *tcp_taskq; 1060 1061 /* Setable only in /etc/system. Move to ndd? */ 1062 boolean_t tcp_icmp_source_quench = B_FALSE; 1063 1064 /* 1065 * Following assumes TPI alignment requirements stay along 32 bit 1066 * boundaries 1067 */ 1068 #define ROUNDUP32(x) \ 1069 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1070 1071 /* Template for response to info request. */ 1072 static struct T_info_ack tcp_g_t_info_ack = { 1073 T_INFO_ACK, /* PRIM_type */ 1074 0, /* TSDU_size */ 1075 T_INFINITE, /* ETSDU_size */ 1076 T_INVALID, /* CDATA_size */ 1077 T_INVALID, /* DDATA_size */ 1078 sizeof (sin_t), /* ADDR_size */ 1079 0, /* OPT_size - not initialized here */ 1080 TIDUSZ, /* TIDU_size */ 1081 T_COTS_ORD, /* SERV_type */ 1082 TCPS_IDLE, /* CURRENT_state */ 1083 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1084 }; 1085 1086 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1087 T_INFO_ACK, /* PRIM_type */ 1088 0, /* TSDU_size */ 1089 T_INFINITE, /* ETSDU_size */ 1090 T_INVALID, /* CDATA_size */ 1091 T_INVALID, /* DDATA_size */ 1092 sizeof (sin6_t), /* ADDR_size */ 1093 0, /* OPT_size - not initialized here */ 1094 TIDUSZ, /* TIDU_size */ 1095 T_COTS_ORD, /* SERV_type */ 1096 TCPS_IDLE, /* CURRENT_state */ 1097 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1098 }; 1099 1100 #define MS 1L 1101 #define SECONDS (1000 * MS) 1102 #define MINUTES (60 * SECONDS) 1103 #define HOURS (60 * MINUTES) 1104 #define DAYS (24 * HOURS) 1105 1106 #define PARAM_MAX (~(uint32_t)0) 1107 1108 /* Max size IP datagram is 64k - 1 */ 1109 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1110 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1111 /* Max of the above */ 1112 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1113 1114 /* Largest TCP port number */ 1115 #define TCP_MAX_PORT (64 * 1024 - 1) 1116 1117 /* 1118 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1119 * layer header. It has to be a multiple of 4. 1120 */ 1121 static tcpparam_t lcl_tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1122 #define tcps_wroff_xtra tcps_wroff_xtra_param->tcp_param_val 1123 1124 /* 1125 * All of these are alterable, within the min/max values given, at run time. 1126 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1127 * per the TCP spec. 1128 */ 1129 /* BEGIN CSTYLED */ 1130 static tcpparam_t lcl_tcp_param_arr[] = { 1131 /*min max value name */ 1132 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1133 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1134 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1135 { 1, 1024, 1, "tcp_conn_req_min" }, 1136 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1137 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1138 { 0, 10, 0, "tcp_debug" }, 1139 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1140 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1141 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1142 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1143 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1144 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1145 { 1, 255, 64, "tcp_ipv4_ttl"}, 1146 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1147 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1148 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1149 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1150 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1151 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1152 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1153 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1154 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1155 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1156 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1157 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1158 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1159 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1160 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1161 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1162 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1163 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1164 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1165 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1166 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1167 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1168 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1169 /* 1170 * Question: What default value should I set for tcp_strong_iss? 1171 */ 1172 { 0, 2, 1, "tcp_strong_iss"}, 1173 { 0, 65536, 20, "tcp_rtt_updates"}, 1174 { 0, 1, 1, "tcp_wscale_always"}, 1175 { 0, 1, 0, "tcp_tstamp_always"}, 1176 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1177 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1178 { 0, 16, 2, "tcp_deferred_acks_max"}, 1179 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1180 { 1, 4, 4, "tcp_slow_start_initial"}, 1181 { 0, 2, 2, "tcp_sack_permitted"}, 1182 { 0, 1, 1, "tcp_compression_enabled"}, 1183 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1184 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1185 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1186 { 0, 1, 0, "tcp_rev_src_routes"}, 1187 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1188 { 100*MS, 60*SECONDS, 1*SECONDS, "tcp_ndd_get_info_interval"}, 1189 { 0, 16, 8, "tcp_local_dacks_max"}, 1190 { 0, 2, 1, "tcp_ecn_permitted"}, 1191 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1192 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1193 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1194 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1195 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1196 }; 1197 /* END CSTYLED */ 1198 1199 /* 1200 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1201 * each header fragment in the header buffer. Each parameter value has 1202 * to be a multiple of 4 (32-bit aligned). 1203 */ 1204 static tcpparam_t lcl_tcp_mdt_head_param = 1205 { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1206 static tcpparam_t lcl_tcp_mdt_tail_param = 1207 { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1208 #define tcps_mdt_hdr_head_min tcps_mdt_head_param->tcp_param_val 1209 #define tcps_mdt_hdr_tail_min tcps_mdt_tail_param->tcp_param_val 1210 1211 /* 1212 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1213 * the maximum number of payload buffers associated per Multidata. 1214 */ 1215 static tcpparam_t lcl_tcp_mdt_max_pbufs_param = 1216 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1217 #define tcps_mdt_max_pbufs tcps_mdt_max_pbufs_param->tcp_param_val 1218 1219 /* Round up the value to the nearest mss. */ 1220 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1221 1222 /* 1223 * Set ECN capable transport (ECT) code point in IP header. 1224 * 1225 * Note that there are 2 ECT code points '01' and '10', which are called 1226 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1227 * point ECT(0) for TCP as described in RFC 2481. 1228 */ 1229 #define SET_ECT(tcp, iph) \ 1230 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1231 /* We need to clear the code point first. */ \ 1232 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1233 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1234 } else { \ 1235 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1236 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1237 } 1238 1239 /* 1240 * The format argument to pass to tcp_display(). 1241 * DISP_PORT_ONLY means that the returned string has only port info. 1242 * DISP_ADDR_AND_PORT means that the returned string also contains the 1243 * remote and local IP address. 1244 */ 1245 #define DISP_PORT_ONLY 1 1246 #define DISP_ADDR_AND_PORT 2 1247 1248 #define NDD_TOO_QUICK_MSG \ 1249 "ndd get info rate too high for non-privileged users, try again " \ 1250 "later.\n" 1251 #define NDD_OUT_OF_BUF_MSG "<< Out of buffer >>\n" 1252 1253 #define IS_VMLOANED_MBLK(mp) \ 1254 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1255 1256 1257 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1258 boolean_t tcp_mdt_chain = B_TRUE; 1259 1260 /* 1261 * MDT threshold in the form of effective send MSS multiplier; we take 1262 * the MDT path if the amount of unsent data exceeds the threshold value 1263 * (default threshold is 1*SMSS). 1264 */ 1265 uint_t tcp_mdt_smss_threshold = 1; 1266 1267 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1268 1269 /* 1270 * Forces all connections to obey the value of the tcps_maxpsz_multiplier 1271 * tunable settable via NDD. Otherwise, the per-connection behavior is 1272 * determined dynamically during tcp_adapt_ire(), which is the default. 1273 */ 1274 boolean_t tcp_static_maxpsz = B_FALSE; 1275 1276 /* Setable in /etc/system */ 1277 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1278 uint32_t tcp_random_anon_port = 1; 1279 1280 /* 1281 * To reach to an eager in Q0 which can be dropped due to an incoming 1282 * new SYN request when Q0 is full, a new doubly linked list is 1283 * introduced. This list allows to select an eager from Q0 in O(1) time. 1284 * This is needed to avoid spending too much time walking through the 1285 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1286 * this new list has to be a member of Q0. 1287 * This list is headed by listener's tcp_t. When the list is empty, 1288 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1289 * of listener's tcp_t point to listener's tcp_t itself. 1290 * 1291 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1292 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1293 * These macros do not affect the eager's membership to Q0. 1294 */ 1295 1296 1297 #define MAKE_DROPPABLE(listener, eager) \ 1298 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1299 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1300 = (eager); \ 1301 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1302 (eager)->tcp_eager_next_drop_q0 = \ 1303 (listener)->tcp_eager_next_drop_q0; \ 1304 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1305 } 1306 1307 #define MAKE_UNDROPPABLE(eager) \ 1308 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1309 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1310 = (eager)->tcp_eager_prev_drop_q0; \ 1311 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1312 = (eager)->tcp_eager_next_drop_q0; \ 1313 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1314 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1315 } 1316 1317 /* 1318 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1319 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1320 * data, TCP will not respond with an ACK. RFC 793 requires that 1321 * TCP responds with an ACK for such a bogus ACK. By not following 1322 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1323 * an attacker successfully spoofs an acceptable segment to our 1324 * peer; or when our peer is "confused." 1325 */ 1326 uint32_t tcp_drop_ack_unsent_cnt = 10; 1327 1328 /* 1329 * Hook functions to enable cluster networking 1330 * On non-clustered systems these vectors must always be NULL. 1331 */ 1332 1333 void (*cl_inet_listen)(uint8_t protocol, sa_family_t addr_family, 1334 uint8_t *laddrp, in_port_t lport) = NULL; 1335 void (*cl_inet_unlisten)(uint8_t protocol, sa_family_t addr_family, 1336 uint8_t *laddrp, in_port_t lport) = NULL; 1337 void (*cl_inet_connect)(uint8_t protocol, sa_family_t addr_family, 1338 uint8_t *laddrp, in_port_t lport, 1339 uint8_t *faddrp, in_port_t fport) = NULL; 1340 void (*cl_inet_disconnect)(uint8_t protocol, sa_family_t addr_family, 1341 uint8_t *laddrp, in_port_t lport, 1342 uint8_t *faddrp, in_port_t fport) = NULL; 1343 1344 /* 1345 * The following are defined in ip.c 1346 */ 1347 extern int (*cl_inet_isclusterwide)(uint8_t protocol, sa_family_t addr_family, 1348 uint8_t *laddrp); 1349 extern uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 1350 uint8_t *laddrp, uint8_t *faddrp); 1351 1352 #define CL_INET_CONNECT(tcp) { \ 1353 if (cl_inet_connect != NULL) { \ 1354 /* \ 1355 * Running in cluster mode - register active connection \ 1356 * information \ 1357 */ \ 1358 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1359 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1360 (*cl_inet_connect)(IPPROTO_TCP, AF_INET,\ 1361 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1362 (in_port_t)(tcp)->tcp_lport, \ 1363 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1364 (in_port_t)(tcp)->tcp_fport); \ 1365 } \ 1366 } else { \ 1367 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1368 &(tcp)->tcp_ip6h->ip6_src)) {\ 1369 (*cl_inet_connect)(IPPROTO_TCP, AF_INET6,\ 1370 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1371 (in_port_t)(tcp)->tcp_lport, \ 1372 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1373 (in_port_t)(tcp)->tcp_fport); \ 1374 } \ 1375 } \ 1376 } \ 1377 } 1378 1379 #define CL_INET_DISCONNECT(tcp) { \ 1380 if (cl_inet_disconnect != NULL) { \ 1381 /* \ 1382 * Running in cluster mode - deregister active \ 1383 * connection information \ 1384 */ \ 1385 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1386 if ((tcp)->tcp_ip_src != 0) { \ 1387 (*cl_inet_disconnect)(IPPROTO_TCP, \ 1388 AF_INET, \ 1389 (uint8_t *)(&((tcp)->tcp_ip_src)),\ 1390 (in_port_t)(tcp)->tcp_lport, \ 1391 (uint8_t *) \ 1392 (&((tcp)->tcp_ipha->ipha_dst)),\ 1393 (in_port_t)(tcp)->tcp_fport); \ 1394 } \ 1395 } else { \ 1396 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1397 &(tcp)->tcp_ip_src_v6)) { \ 1398 (*cl_inet_disconnect)(IPPROTO_TCP, AF_INET6,\ 1399 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1400 (in_port_t)(tcp)->tcp_lport, \ 1401 (uint8_t *) \ 1402 (&((tcp)->tcp_ip6h->ip6_dst)),\ 1403 (in_port_t)(tcp)->tcp_fport); \ 1404 } \ 1405 } \ 1406 } \ 1407 } 1408 1409 /* 1410 * Cluster networking hook for traversing current connection list. 1411 * This routine is used to extract the current list of live connections 1412 * which must continue to to be dispatched to this node. 1413 */ 1414 int cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg); 1415 1416 static int cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), 1417 void *arg, tcp_stack_t *tcps); 1418 1419 #define DTRACE_IP_FASTPATH(mp, iph, ill, ipha, ip6h) \ 1420 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL, void_ip_t *, \ 1421 iph, __dtrace_ipsr_ill_t *, ill, ipha_t *, ipha, \ 1422 ip6_t *, ip6h, int, 0); 1423 1424 /* 1425 * Figure out the value of window scale opton. Note that the rwnd is 1426 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1427 * We cannot find the scale value and then do a round up of tcp_rwnd 1428 * because the scale value may not be correct after that. 1429 * 1430 * Set the compiler flag to make this function inline. 1431 */ 1432 static void 1433 tcp_set_ws_value(tcp_t *tcp) 1434 { 1435 int i; 1436 uint32_t rwnd = tcp->tcp_rwnd; 1437 1438 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1439 i++, rwnd >>= 1) 1440 ; 1441 tcp->tcp_rcv_ws = i; 1442 } 1443 1444 /* 1445 * Remove a connection from the list of detached TIME_WAIT connections. 1446 * It returns B_FALSE if it can't remove the connection from the list 1447 * as the connection has already been removed from the list due to an 1448 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1449 */ 1450 static boolean_t 1451 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1452 { 1453 boolean_t locked = B_FALSE; 1454 1455 if (tcp_time_wait == NULL) { 1456 tcp_time_wait = *((tcp_squeue_priv_t **) 1457 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1458 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1459 locked = B_TRUE; 1460 } else { 1461 ASSERT(MUTEX_HELD(&tcp_time_wait->tcp_time_wait_lock)); 1462 } 1463 1464 if (tcp->tcp_time_wait_expire == 0) { 1465 ASSERT(tcp->tcp_time_wait_next == NULL); 1466 ASSERT(tcp->tcp_time_wait_prev == NULL); 1467 if (locked) 1468 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1469 return (B_FALSE); 1470 } 1471 ASSERT(TCP_IS_DETACHED(tcp)); 1472 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1473 1474 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1475 ASSERT(tcp->tcp_time_wait_prev == NULL); 1476 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1477 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1478 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1479 NULL; 1480 } else { 1481 tcp_time_wait->tcp_time_wait_tail = NULL; 1482 } 1483 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1484 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1485 ASSERT(tcp->tcp_time_wait_next == NULL); 1486 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1487 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1488 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1489 } else { 1490 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1491 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1492 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1493 tcp->tcp_time_wait_next; 1494 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1495 tcp->tcp_time_wait_prev; 1496 } 1497 tcp->tcp_time_wait_next = NULL; 1498 tcp->tcp_time_wait_prev = NULL; 1499 tcp->tcp_time_wait_expire = 0; 1500 1501 if (locked) 1502 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1503 return (B_TRUE); 1504 } 1505 1506 /* 1507 * Add a connection to the list of detached TIME_WAIT connections 1508 * and set its time to expire. 1509 */ 1510 static void 1511 tcp_time_wait_append(tcp_t *tcp) 1512 { 1513 tcp_stack_t *tcps = tcp->tcp_tcps; 1514 tcp_squeue_priv_t *tcp_time_wait = 1515 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1516 SQPRIVATE_TCP)); 1517 1518 tcp_timers_stop(tcp); 1519 1520 /* Freed above */ 1521 ASSERT(tcp->tcp_timer_tid == 0); 1522 ASSERT(tcp->tcp_ack_tid == 0); 1523 1524 /* must have happened at the time of detaching the tcp */ 1525 ASSERT(tcp->tcp_ptpahn == NULL); 1526 ASSERT(tcp->tcp_flow_stopped == 0); 1527 ASSERT(tcp->tcp_time_wait_next == NULL); 1528 ASSERT(tcp->tcp_time_wait_prev == NULL); 1529 ASSERT(tcp->tcp_time_wait_expire == NULL); 1530 ASSERT(tcp->tcp_listener == NULL); 1531 1532 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1533 /* 1534 * The value computed below in tcp->tcp_time_wait_expire may 1535 * appear negative or wrap around. That is ok since our 1536 * interest is only in the difference between the current lbolt 1537 * value and tcp->tcp_time_wait_expire. But the value should not 1538 * be zero, since it means the tcp is not in the TIME_WAIT list. 1539 * The corresponding comparison in tcp_time_wait_collector() uses 1540 * modular arithmetic. 1541 */ 1542 tcp->tcp_time_wait_expire += 1543 drv_usectohz(tcps->tcps_time_wait_interval * 1000); 1544 if (tcp->tcp_time_wait_expire == 0) 1545 tcp->tcp_time_wait_expire = 1; 1546 1547 ASSERT(TCP_IS_DETACHED(tcp)); 1548 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1549 ASSERT(tcp->tcp_time_wait_next == NULL); 1550 ASSERT(tcp->tcp_time_wait_prev == NULL); 1551 TCP_DBGSTAT(tcps, tcp_time_wait); 1552 1553 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1554 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1555 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1556 tcp_time_wait->tcp_time_wait_head = tcp; 1557 } else { 1558 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1559 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1560 TCPS_TIME_WAIT); 1561 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1562 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1563 } 1564 tcp_time_wait->tcp_time_wait_tail = tcp; 1565 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1566 } 1567 1568 /* ARGSUSED */ 1569 void 1570 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1571 { 1572 conn_t *connp = (conn_t *)arg; 1573 tcp_t *tcp = connp->conn_tcp; 1574 tcp_stack_t *tcps = tcp->tcp_tcps; 1575 1576 ASSERT(tcp != NULL); 1577 if (tcp->tcp_state == TCPS_CLOSED) { 1578 return; 1579 } 1580 1581 ASSERT((tcp->tcp_family == AF_INET && 1582 tcp->tcp_ipversion == IPV4_VERSION) || 1583 (tcp->tcp_family == AF_INET6 && 1584 (tcp->tcp_ipversion == IPV4_VERSION || 1585 tcp->tcp_ipversion == IPV6_VERSION))); 1586 ASSERT(!tcp->tcp_listener); 1587 1588 TCP_STAT(tcps, tcp_time_wait_reap); 1589 ASSERT(TCP_IS_DETACHED(tcp)); 1590 1591 /* 1592 * Because they have no upstream client to rebind or tcp_close() 1593 * them later, we axe the connection here and now. 1594 */ 1595 tcp_close_detached(tcp); 1596 } 1597 1598 /* 1599 * Remove cached/latched IPsec references. 1600 */ 1601 void 1602 tcp_ipsec_cleanup(tcp_t *tcp) 1603 { 1604 conn_t *connp = tcp->tcp_connp; 1605 1606 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1607 1608 if (connp->conn_latch != NULL) { 1609 IPLATCH_REFRELE(connp->conn_latch, 1610 connp->conn_netstack); 1611 connp->conn_latch = NULL; 1612 } 1613 if (connp->conn_policy != NULL) { 1614 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 1615 connp->conn_policy = NULL; 1616 } 1617 } 1618 1619 /* 1620 * Cleaup before placing on free list. 1621 * Disassociate from the netstack/tcp_stack_t since the freelist 1622 * is per squeue and not per netstack. 1623 */ 1624 void 1625 tcp_cleanup(tcp_t *tcp) 1626 { 1627 mblk_t *mp; 1628 char *tcp_iphc; 1629 int tcp_iphc_len; 1630 int tcp_hdr_grown; 1631 tcp_sack_info_t *tcp_sack_info; 1632 conn_t *connp = tcp->tcp_connp; 1633 tcp_stack_t *tcps = tcp->tcp_tcps; 1634 netstack_t *ns = tcps->tcps_netstack; 1635 mblk_t *tcp_rsrv_mp; 1636 1637 tcp_bind_hash_remove(tcp); 1638 1639 /* Cleanup that which needs the netstack first */ 1640 tcp_ipsec_cleanup(tcp); 1641 1642 tcp_free(tcp); 1643 1644 /* Release any SSL context */ 1645 if (tcp->tcp_kssl_ent != NULL) { 1646 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1647 tcp->tcp_kssl_ent = NULL; 1648 } 1649 1650 if (tcp->tcp_kssl_ctx != NULL) { 1651 kssl_release_ctx(tcp->tcp_kssl_ctx); 1652 tcp->tcp_kssl_ctx = NULL; 1653 } 1654 tcp->tcp_kssl_pending = B_FALSE; 1655 1656 conn_delete_ire(connp, NULL); 1657 1658 /* 1659 * Since we will bzero the entire structure, we need to 1660 * remove it and reinsert it in global hash list. We 1661 * know the walkers can't get to this conn because we 1662 * had set CONDEMNED flag earlier and checked reference 1663 * under conn_lock so walker won't pick it and when we 1664 * go the ipcl_globalhash_remove() below, no walker 1665 * can get to it. 1666 */ 1667 ipcl_globalhash_remove(connp); 1668 1669 /* 1670 * Now it is safe to decrement the reference counts. 1671 * This might be the last reference on the netstack and TCPS 1672 * in which case it will cause the tcp_g_q_close and 1673 * the freeing of the IP Instance. 1674 */ 1675 connp->conn_netstack = NULL; 1676 netstack_rele(ns); 1677 ASSERT(tcps != NULL); 1678 tcp->tcp_tcps = NULL; 1679 TCPS_REFRELE(tcps); 1680 1681 /* Save some state */ 1682 mp = tcp->tcp_timercache; 1683 1684 tcp_sack_info = tcp->tcp_sack_info; 1685 tcp_iphc = tcp->tcp_iphc; 1686 tcp_iphc_len = tcp->tcp_iphc_len; 1687 tcp_hdr_grown = tcp->tcp_hdr_grown; 1688 tcp_rsrv_mp = tcp->tcp_rsrv_mp; 1689 1690 if (connp->conn_cred != NULL) { 1691 crfree(connp->conn_cred); 1692 connp->conn_cred = NULL; 1693 } 1694 if (connp->conn_peercred != NULL) { 1695 crfree(connp->conn_peercred); 1696 connp->conn_peercred = NULL; 1697 } 1698 ipcl_conn_cleanup(connp); 1699 connp->conn_flags = IPCL_TCPCONN; 1700 bzero(tcp, sizeof (tcp_t)); 1701 1702 /* restore the state */ 1703 tcp->tcp_timercache = mp; 1704 1705 tcp->tcp_sack_info = tcp_sack_info; 1706 tcp->tcp_iphc = tcp_iphc; 1707 tcp->tcp_iphc_len = tcp_iphc_len; 1708 tcp->tcp_hdr_grown = tcp_hdr_grown; 1709 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 1710 1711 tcp->tcp_connp = connp; 1712 1713 ASSERT(connp->conn_tcp == tcp); 1714 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1715 connp->conn_state_flags = CONN_INCIPIENT; 1716 ASSERT(connp->conn_ulp == IPPROTO_TCP); 1717 ASSERT(connp->conn_ref == 1); 1718 } 1719 1720 /* 1721 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1722 * is done forwards from the head. 1723 * This walks all stack instances since 1724 * tcp_time_wait remains global across all stacks. 1725 */ 1726 /* ARGSUSED */ 1727 void 1728 tcp_time_wait_collector(void *arg) 1729 { 1730 tcp_t *tcp; 1731 clock_t now; 1732 mblk_t *mp; 1733 conn_t *connp; 1734 kmutex_t *lock; 1735 boolean_t removed; 1736 1737 squeue_t *sqp = (squeue_t *)arg; 1738 tcp_squeue_priv_t *tcp_time_wait = 1739 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1740 1741 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1742 tcp_time_wait->tcp_time_wait_tid = 0; 1743 1744 if (tcp_time_wait->tcp_free_list != NULL && 1745 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1746 TCP_G_STAT(tcp_freelist_cleanup); 1747 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1748 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1749 tcp->tcp_time_wait_next = NULL; 1750 tcp_time_wait->tcp_free_list_cnt--; 1751 ASSERT(tcp->tcp_tcps == NULL); 1752 CONN_DEC_REF(tcp->tcp_connp); 1753 } 1754 ASSERT(tcp_time_wait->tcp_free_list_cnt == 0); 1755 } 1756 1757 /* 1758 * In order to reap time waits reliably, we should use a 1759 * source of time that is not adjustable by the user -- hence 1760 * the call to ddi_get_lbolt(). 1761 */ 1762 now = ddi_get_lbolt(); 1763 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1764 /* 1765 * Compare times using modular arithmetic, since 1766 * lbolt can wrapover. 1767 */ 1768 if ((now - tcp->tcp_time_wait_expire) < 0) { 1769 break; 1770 } 1771 1772 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1773 ASSERT(removed); 1774 1775 connp = tcp->tcp_connp; 1776 ASSERT(connp->conn_fanout != NULL); 1777 lock = &connp->conn_fanout->connf_lock; 1778 /* 1779 * This is essentially a TW reclaim fast path optimization for 1780 * performance where the timewait collector checks under the 1781 * fanout lock (so that no one else can get access to the 1782 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1783 * the classifier hash list. If ref count is indeed 2, we can 1784 * just remove the conn under the fanout lock and avoid 1785 * cleaning up the conn under the squeue, provided that 1786 * clustering callbacks are not enabled. If clustering is 1787 * enabled, we need to make the clustering callback before 1788 * setting the CONDEMNED flag and after dropping all locks and 1789 * so we forego this optimization and fall back to the slow 1790 * path. Also please see the comments in tcp_closei_local 1791 * regarding the refcnt logic. 1792 * 1793 * Since we are holding the tcp_time_wait_lock, its better 1794 * not to block on the fanout_lock because other connections 1795 * can't add themselves to time_wait list. So we do a 1796 * tryenter instead of mutex_enter. 1797 */ 1798 if (mutex_tryenter(lock)) { 1799 mutex_enter(&connp->conn_lock); 1800 if ((connp->conn_ref == 2) && 1801 (cl_inet_disconnect == NULL)) { 1802 ipcl_hash_remove_locked(connp, 1803 connp->conn_fanout); 1804 /* 1805 * Set the CONDEMNED flag now itself so that 1806 * the refcnt cannot increase due to any 1807 * walker. But we have still not cleaned up 1808 * conn_ire_cache. This is still ok since 1809 * we are going to clean it up in tcp_cleanup 1810 * immediately and any interface unplumb 1811 * thread will wait till the ire is blown away 1812 */ 1813 connp->conn_state_flags |= CONN_CONDEMNED; 1814 mutex_exit(lock); 1815 mutex_exit(&connp->conn_lock); 1816 if (tcp_time_wait->tcp_free_list_cnt < 1817 tcp_free_list_max_cnt) { 1818 /* Add to head of tcp_free_list */ 1819 mutex_exit( 1820 &tcp_time_wait->tcp_time_wait_lock); 1821 tcp_cleanup(tcp); 1822 ASSERT(connp->conn_latch == NULL); 1823 ASSERT(connp->conn_policy == NULL); 1824 ASSERT(tcp->tcp_tcps == NULL); 1825 ASSERT(connp->conn_netstack == NULL); 1826 1827 mutex_enter( 1828 &tcp_time_wait->tcp_time_wait_lock); 1829 tcp->tcp_time_wait_next = 1830 tcp_time_wait->tcp_free_list; 1831 tcp_time_wait->tcp_free_list = tcp; 1832 tcp_time_wait->tcp_free_list_cnt++; 1833 continue; 1834 } else { 1835 /* Do not add to tcp_free_list */ 1836 mutex_exit( 1837 &tcp_time_wait->tcp_time_wait_lock); 1838 tcp_bind_hash_remove(tcp); 1839 conn_delete_ire(tcp->tcp_connp, NULL); 1840 tcp_ipsec_cleanup(tcp); 1841 CONN_DEC_REF(tcp->tcp_connp); 1842 } 1843 } else { 1844 CONN_INC_REF_LOCKED(connp); 1845 mutex_exit(lock); 1846 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1847 mutex_exit(&connp->conn_lock); 1848 /* 1849 * We can reuse the closemp here since conn has 1850 * detached (otherwise we wouldn't even be in 1851 * time_wait list). tcp_closemp_used can safely 1852 * be changed without taking a lock as no other 1853 * thread can concurrently access it at this 1854 * point in the connection lifecycle. 1855 */ 1856 1857 if (tcp->tcp_closemp.b_prev == NULL) 1858 tcp->tcp_closemp_used = B_TRUE; 1859 else 1860 cmn_err(CE_PANIC, 1861 "tcp_timewait_collector: " 1862 "concurrent use of tcp_closemp: " 1863 "connp %p tcp %p\n", (void *)connp, 1864 (void *)tcp); 1865 1866 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1867 mp = &tcp->tcp_closemp; 1868 squeue_fill(connp->conn_sqp, mp, 1869 tcp_timewait_output, connp, 1870 SQTAG_TCP_TIMEWAIT); 1871 } 1872 } else { 1873 mutex_enter(&connp->conn_lock); 1874 CONN_INC_REF_LOCKED(connp); 1875 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1876 mutex_exit(&connp->conn_lock); 1877 /* 1878 * We can reuse the closemp here since conn has 1879 * detached (otherwise we wouldn't even be in 1880 * time_wait list). tcp_closemp_used can safely 1881 * be changed without taking a lock as no other 1882 * thread can concurrently access it at this 1883 * point in the connection lifecycle. 1884 */ 1885 1886 if (tcp->tcp_closemp.b_prev == NULL) 1887 tcp->tcp_closemp_used = B_TRUE; 1888 else 1889 cmn_err(CE_PANIC, "tcp_timewait_collector: " 1890 "concurrent use of tcp_closemp: " 1891 "connp %p tcp %p\n", (void *)connp, 1892 (void *)tcp); 1893 1894 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1895 mp = &tcp->tcp_closemp; 1896 squeue_fill(connp->conn_sqp, mp, 1897 tcp_timewait_output, connp, 0); 1898 } 1899 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1900 } 1901 1902 if (tcp_time_wait->tcp_free_list != NULL) 1903 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1904 1905 tcp_time_wait->tcp_time_wait_tid = 1906 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 1907 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 1908 CALLOUT_FLAG_ROUNDUP); 1909 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1910 } 1911 /* 1912 * Reply to a clients T_CONN_RES TPI message. This function 1913 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1914 * on the acceptor STREAM and processed in tcp_wput_accept(). 1915 * Read the block comment on top of tcp_conn_request(). 1916 */ 1917 static void 1918 tcp_accept(tcp_t *listener, mblk_t *mp) 1919 { 1920 tcp_t *acceptor; 1921 tcp_t *eager; 1922 tcp_t *tcp; 1923 struct T_conn_res *tcr; 1924 t_uscalar_t acceptor_id; 1925 t_scalar_t seqnum; 1926 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1927 mblk_t *ok_mp; 1928 mblk_t *mp1; 1929 tcp_stack_t *tcps = listener->tcp_tcps; 1930 1931 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1932 tcp_err_ack(listener, mp, TPROTO, 0); 1933 return; 1934 } 1935 tcr = (struct T_conn_res *)mp->b_rptr; 1936 1937 /* 1938 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1939 * read side queue of the streams device underneath us i.e. the 1940 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1941 * look it up in the queue_hash. Under LP64 it sends down the 1942 * minor_t of the accepting endpoint. 1943 * 1944 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1945 * fanout hash lock is held. 1946 * This prevents any thread from entering the acceptor queue from 1947 * below (since it has not been hard bound yet i.e. any inbound 1948 * packets will arrive on the listener or default tcp queue and 1949 * go through tcp_lookup). 1950 * The CONN_INC_REF will prevent the acceptor from closing. 1951 * 1952 * XXX It is still possible for a tli application to send down data 1953 * on the accepting stream while another thread calls t_accept. 1954 * This should not be a problem for well-behaved applications since 1955 * the T_OK_ACK is sent after the queue swapping is completed. 1956 * 1957 * If the accepting fd is the same as the listening fd, avoid 1958 * queue hash lookup since that will return an eager listener in a 1959 * already established state. 1960 */ 1961 acceptor_id = tcr->ACCEPTOR_id; 1962 mutex_enter(&listener->tcp_eager_lock); 1963 if (listener->tcp_acceptor_id == acceptor_id) { 1964 eager = listener->tcp_eager_next_q; 1965 /* only count how many T_CONN_INDs so don't count q0 */ 1966 if ((listener->tcp_conn_req_cnt_q != 1) || 1967 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 1968 mutex_exit(&listener->tcp_eager_lock); 1969 tcp_err_ack(listener, mp, TBADF, 0); 1970 return; 1971 } 1972 if (listener->tcp_conn_req_cnt_q0 != 0) { 1973 /* Throw away all the eagers on q0. */ 1974 tcp_eager_cleanup(listener, 1); 1975 } 1976 if (listener->tcp_syn_defense) { 1977 listener->tcp_syn_defense = B_FALSE; 1978 if (listener->tcp_ip_addr_cache != NULL) { 1979 kmem_free(listener->tcp_ip_addr_cache, 1980 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 1981 listener->tcp_ip_addr_cache = NULL; 1982 } 1983 } 1984 /* 1985 * Transfer tcp_conn_req_max to the eager so that when 1986 * a disconnect occurs we can revert the endpoint to the 1987 * listen state. 1988 */ 1989 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 1990 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 1991 /* 1992 * Get a reference on the acceptor just like the 1993 * tcp_acceptor_hash_lookup below. 1994 */ 1995 acceptor = listener; 1996 CONN_INC_REF(acceptor->tcp_connp); 1997 } else { 1998 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 1999 if (acceptor == NULL) { 2000 if (listener->tcp_debug) { 2001 (void) strlog(TCP_MOD_ID, 0, 1, 2002 SL_ERROR|SL_TRACE, 2003 "tcp_accept: did not find acceptor 0x%x\n", 2004 acceptor_id); 2005 } 2006 mutex_exit(&listener->tcp_eager_lock); 2007 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 2008 return; 2009 } 2010 /* 2011 * Verify acceptor state. The acceptable states for an acceptor 2012 * include TCPS_IDLE and TCPS_BOUND. 2013 */ 2014 switch (acceptor->tcp_state) { 2015 case TCPS_IDLE: 2016 /* FALLTHRU */ 2017 case TCPS_BOUND: 2018 break; 2019 default: 2020 CONN_DEC_REF(acceptor->tcp_connp); 2021 mutex_exit(&listener->tcp_eager_lock); 2022 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2023 return; 2024 } 2025 } 2026 2027 /* The listener must be in TCPS_LISTEN */ 2028 if (listener->tcp_state != TCPS_LISTEN) { 2029 CONN_DEC_REF(acceptor->tcp_connp); 2030 mutex_exit(&listener->tcp_eager_lock); 2031 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2032 return; 2033 } 2034 2035 /* 2036 * Rendezvous with an eager connection request packet hanging off 2037 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2038 * tcp structure when the connection packet arrived in 2039 * tcp_conn_request(). 2040 */ 2041 seqnum = tcr->SEQ_number; 2042 eager = listener; 2043 do { 2044 eager = eager->tcp_eager_next_q; 2045 if (eager == NULL) { 2046 CONN_DEC_REF(acceptor->tcp_connp); 2047 mutex_exit(&listener->tcp_eager_lock); 2048 tcp_err_ack(listener, mp, TBADSEQ, 0); 2049 return; 2050 } 2051 } while (eager->tcp_conn_req_seqnum != seqnum); 2052 mutex_exit(&listener->tcp_eager_lock); 2053 2054 /* 2055 * At this point, both acceptor and listener have 2 ref 2056 * that they begin with. Acceptor has one additional ref 2057 * we placed in lookup while listener has 3 additional 2058 * ref for being behind the squeue (tcp_accept() is 2059 * done on listener's squeue); being in classifier hash; 2060 * and eager's ref on listener. 2061 */ 2062 ASSERT(listener->tcp_connp->conn_ref >= 5); 2063 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2064 2065 /* 2066 * The eager at this point is set in its own squeue and 2067 * could easily have been killed (tcp_accept_finish will 2068 * deal with that) because of a TH_RST so we can only 2069 * ASSERT for a single ref. 2070 */ 2071 ASSERT(eager->tcp_connp->conn_ref >= 1); 2072 2073 /* Pre allocate the stroptions mblk also */ 2074 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 2075 if (opt_mp == NULL) { 2076 CONN_DEC_REF(acceptor->tcp_connp); 2077 CONN_DEC_REF(eager->tcp_connp); 2078 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2079 return; 2080 } 2081 DB_TYPE(opt_mp) = M_SETOPTS; 2082 opt_mp->b_wptr += sizeof (struct stroptions); 2083 2084 /* 2085 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2086 * from listener to acceptor. The message is chained on opt_mp 2087 * which will be sent onto eager's squeue. 2088 */ 2089 if (listener->tcp_bound_if != 0) { 2090 /* allocate optmgmt req */ 2091 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2092 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 2093 sizeof (int)); 2094 if (mp1 != NULL) 2095 linkb(opt_mp, mp1); 2096 } 2097 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2098 uint_t on = 1; 2099 2100 /* allocate optmgmt req */ 2101 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2102 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 2103 if (mp1 != NULL) 2104 linkb(opt_mp, mp1); 2105 } 2106 2107 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2108 if ((mp1 = copymsg(mp)) == NULL) { 2109 CONN_DEC_REF(acceptor->tcp_connp); 2110 CONN_DEC_REF(eager->tcp_connp); 2111 freemsg(opt_mp); 2112 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2113 return; 2114 } 2115 2116 tcr = (struct T_conn_res *)mp1->b_rptr; 2117 2118 /* 2119 * This is an expanded version of mi_tpi_ok_ack_alloc() 2120 * which allocates a larger mblk and appends the new 2121 * local address to the ok_ack. The address is copied by 2122 * soaccept() for getsockname(). 2123 */ 2124 { 2125 int extra; 2126 2127 extra = (eager->tcp_family == AF_INET) ? 2128 sizeof (sin_t) : sizeof (sin6_t); 2129 2130 /* 2131 * Try to re-use mp, if possible. Otherwise, allocate 2132 * an mblk and return it as ok_mp. In any case, mp 2133 * is no longer usable upon return. 2134 */ 2135 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2136 CONN_DEC_REF(acceptor->tcp_connp); 2137 CONN_DEC_REF(eager->tcp_connp); 2138 freemsg(opt_mp); 2139 /* Original mp has been freed by now, so use mp1 */ 2140 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2141 return; 2142 } 2143 2144 mp = NULL; /* We should never use mp after this point */ 2145 2146 switch (extra) { 2147 case sizeof (sin_t): { 2148 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2149 2150 ok_mp->b_wptr += extra; 2151 sin->sin_family = AF_INET; 2152 sin->sin_port = eager->tcp_lport; 2153 sin->sin_addr.s_addr = 2154 eager->tcp_ipha->ipha_src; 2155 break; 2156 } 2157 case sizeof (sin6_t): { 2158 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2159 2160 ok_mp->b_wptr += extra; 2161 sin6->sin6_family = AF_INET6; 2162 sin6->sin6_port = eager->tcp_lport; 2163 if (eager->tcp_ipversion == IPV4_VERSION) { 2164 sin6->sin6_flowinfo = 0; 2165 IN6_IPADDR_TO_V4MAPPED( 2166 eager->tcp_ipha->ipha_src, 2167 &sin6->sin6_addr); 2168 } else { 2169 ASSERT(eager->tcp_ip6h != NULL); 2170 sin6->sin6_flowinfo = 2171 eager->tcp_ip6h->ip6_vcf & 2172 ~IPV6_VERS_AND_FLOW_MASK; 2173 sin6->sin6_addr = 2174 eager->tcp_ip6h->ip6_src; 2175 } 2176 sin6->sin6_scope_id = 0; 2177 sin6->__sin6_src_id = 0; 2178 break; 2179 } 2180 default: 2181 break; 2182 } 2183 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2184 } 2185 2186 /* 2187 * If there are no options we know that the T_CONN_RES will 2188 * succeed. However, we can't send the T_OK_ACK upstream until 2189 * the tcp_accept_swap is done since it would be dangerous to 2190 * let the application start using the new fd prior to the swap. 2191 */ 2192 tcp_accept_swap(listener, acceptor, eager); 2193 2194 /* 2195 * tcp_accept_swap unlinks eager from listener but does not drop 2196 * the eager's reference on the listener. 2197 */ 2198 ASSERT(eager->tcp_listener == NULL); 2199 ASSERT(listener->tcp_connp->conn_ref >= 5); 2200 2201 /* 2202 * The eager is now associated with its own queue. Insert in 2203 * the hash so that the connection can be reused for a future 2204 * T_CONN_RES. 2205 */ 2206 tcp_acceptor_hash_insert(acceptor_id, eager); 2207 2208 /* 2209 * We now do the processing of options with T_CONN_RES. 2210 * We delay till now since we wanted to have queue to pass to 2211 * option processing routines that points back to the right 2212 * instance structure which does not happen until after 2213 * tcp_accept_swap(). 2214 * 2215 * Note: 2216 * The sanity of the logic here assumes that whatever options 2217 * are appropriate to inherit from listner=>eager are done 2218 * before this point, and whatever were to be overridden (or not) 2219 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2220 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2221 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2222 * This may not be true at this point in time but can be fixed 2223 * independently. This option processing code starts with 2224 * the instantiated acceptor instance and the final queue at 2225 * this point. 2226 */ 2227 2228 if (tcr->OPT_length != 0) { 2229 /* Options to process */ 2230 int t_error = 0; 2231 int sys_error = 0; 2232 int do_disconnect = 0; 2233 2234 if (tcp_conprim_opt_process(eager, mp1, 2235 &do_disconnect, &t_error, &sys_error) < 0) { 2236 eager->tcp_accept_error = 1; 2237 if (do_disconnect) { 2238 /* 2239 * An option failed which does not allow 2240 * connection to be accepted. 2241 * 2242 * We allow T_CONN_RES to succeed and 2243 * put a T_DISCON_IND on the eager queue. 2244 */ 2245 ASSERT(t_error == 0 && sys_error == 0); 2246 eager->tcp_send_discon_ind = 1; 2247 } else { 2248 ASSERT(t_error != 0); 2249 freemsg(ok_mp); 2250 /* 2251 * Original mp was either freed or set 2252 * to ok_mp above, so use mp1 instead. 2253 */ 2254 tcp_err_ack(listener, mp1, t_error, sys_error); 2255 goto finish; 2256 } 2257 } 2258 /* 2259 * Most likely success in setting options (except if 2260 * eager->tcp_send_discon_ind set). 2261 * mp1 option buffer represented by OPT_length/offset 2262 * potentially modified and contains results of setting 2263 * options at this point 2264 */ 2265 } 2266 2267 /* We no longer need mp1, since all options processing has passed */ 2268 freemsg(mp1); 2269 2270 putnext(listener->tcp_rq, ok_mp); 2271 2272 mutex_enter(&listener->tcp_eager_lock); 2273 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2274 tcp_t *tail; 2275 mblk_t *conn_ind; 2276 2277 /* 2278 * This path should not be executed if listener and 2279 * acceptor streams are the same. 2280 */ 2281 ASSERT(listener != acceptor); 2282 2283 tcp = listener->tcp_eager_prev_q0; 2284 /* 2285 * listener->tcp_eager_prev_q0 points to the TAIL of the 2286 * deferred T_conn_ind queue. We need to get to the head of 2287 * the queue in order to send up T_conn_ind the same order as 2288 * how the 3WHS is completed. 2289 */ 2290 while (tcp != listener) { 2291 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2292 break; 2293 else 2294 tcp = tcp->tcp_eager_prev_q0; 2295 } 2296 ASSERT(tcp != listener); 2297 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2298 ASSERT(conn_ind != NULL); 2299 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2300 2301 /* Move from q0 to q */ 2302 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2303 listener->tcp_conn_req_cnt_q0--; 2304 listener->tcp_conn_req_cnt_q++; 2305 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2306 tcp->tcp_eager_prev_q0; 2307 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2308 tcp->tcp_eager_next_q0; 2309 tcp->tcp_eager_prev_q0 = NULL; 2310 tcp->tcp_eager_next_q0 = NULL; 2311 tcp->tcp_conn_def_q0 = B_FALSE; 2312 2313 /* Make sure the tcp isn't in the list of droppables */ 2314 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2315 tcp->tcp_eager_prev_drop_q0 == NULL); 2316 2317 /* 2318 * Insert at end of the queue because sockfs sends 2319 * down T_CONN_RES in chronological order. Leaving 2320 * the older conn indications at front of the queue 2321 * helps reducing search time. 2322 */ 2323 tail = listener->tcp_eager_last_q; 2324 if (tail != NULL) 2325 tail->tcp_eager_next_q = tcp; 2326 else 2327 listener->tcp_eager_next_q = tcp; 2328 listener->tcp_eager_last_q = tcp; 2329 tcp->tcp_eager_next_q = NULL; 2330 mutex_exit(&listener->tcp_eager_lock); 2331 putnext(tcp->tcp_rq, conn_ind); 2332 } else { 2333 mutex_exit(&listener->tcp_eager_lock); 2334 } 2335 2336 /* 2337 * Done with the acceptor - free it 2338 * 2339 * Note: from this point on, no access to listener should be made 2340 * as listener can be equal to acceptor. 2341 */ 2342 finish: 2343 ASSERT(acceptor->tcp_detached); 2344 ASSERT(tcps->tcps_g_q != NULL); 2345 acceptor->tcp_rq = tcps->tcps_g_q; 2346 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2347 (void) tcp_clean_death(acceptor, 0, 2); 2348 CONN_DEC_REF(acceptor->tcp_connp); 2349 2350 /* 2351 * In case we already received a FIN we have to make tcp_rput send 2352 * the ordrel_ind. This will also send up a window update if the window 2353 * has opened up. 2354 * 2355 * In the normal case of a successful connection acceptance 2356 * we give the O_T_BIND_REQ to the read side put procedure as an 2357 * indication that this was just accepted. This tells tcp_rput to 2358 * pass up any data queued in tcp_rcv_list. 2359 * 2360 * In the fringe case where options sent with T_CONN_RES failed and 2361 * we required, we would be indicating a T_DISCON_IND to blow 2362 * away this connection. 2363 */ 2364 2365 /* 2366 * XXX: we currently have a problem if XTI application closes the 2367 * acceptor stream in between. This problem exists in on10-gate also 2368 * and is well know but nothing can be done short of major rewrite 2369 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2370 * eager same squeue as listener (we can distinguish non socket 2371 * listeners at the time of handling a SYN in tcp_conn_request) 2372 * and do most of the work that tcp_accept_finish does here itself 2373 * and then get behind the acceptor squeue to access the acceptor 2374 * queue. 2375 */ 2376 /* 2377 * We already have a ref on tcp so no need to do one before squeue_fill 2378 */ 2379 squeue_fill(eager->tcp_connp->conn_sqp, opt_mp, 2380 tcp_accept_finish, eager->tcp_connp, SQTAG_TCP_ACCEPT_FINISH); 2381 } 2382 2383 /* 2384 * Swap information between the eager and acceptor for a TLI/XTI client. 2385 * The sockfs accept is done on the acceptor stream and control goes 2386 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2387 * called. In either case, both the eager and listener are in their own 2388 * perimeter (squeue) and the code has to deal with potential race. 2389 * 2390 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2391 */ 2392 static void 2393 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2394 { 2395 conn_t *econnp, *aconnp; 2396 2397 ASSERT(eager->tcp_rq == listener->tcp_rq); 2398 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2399 ASSERT(!eager->tcp_hard_bound); 2400 ASSERT(!TCP_IS_SOCKET(acceptor)); 2401 ASSERT(!TCP_IS_SOCKET(eager)); 2402 ASSERT(!TCP_IS_SOCKET(listener)); 2403 2404 acceptor->tcp_detached = B_TRUE; 2405 /* 2406 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2407 * the acceptor id. 2408 */ 2409 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2410 2411 /* remove eager from listen list... */ 2412 mutex_enter(&listener->tcp_eager_lock); 2413 tcp_eager_unlink(eager); 2414 ASSERT(eager->tcp_eager_next_q == NULL && 2415 eager->tcp_eager_last_q == NULL); 2416 ASSERT(eager->tcp_eager_next_q0 == NULL && 2417 eager->tcp_eager_prev_q0 == NULL); 2418 mutex_exit(&listener->tcp_eager_lock); 2419 eager->tcp_rq = acceptor->tcp_rq; 2420 eager->tcp_wq = acceptor->tcp_wq; 2421 2422 econnp = eager->tcp_connp; 2423 aconnp = acceptor->tcp_connp; 2424 2425 eager->tcp_rq->q_ptr = econnp; 2426 eager->tcp_wq->q_ptr = econnp; 2427 2428 /* 2429 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2430 * which might be a different squeue from our peer TCP instance. 2431 * For TCP Fusion, the peer expects that whenever tcp_detached is 2432 * clear, our TCP queues point to the acceptor's queues. Thus, use 2433 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2434 * above reach global visibility prior to the clearing of tcp_detached. 2435 */ 2436 membar_producer(); 2437 eager->tcp_detached = B_FALSE; 2438 2439 ASSERT(eager->tcp_ack_tid == 0); 2440 2441 econnp->conn_dev = aconnp->conn_dev; 2442 econnp->conn_minor_arena = aconnp->conn_minor_arena; 2443 ASSERT(econnp->conn_minor_arena != NULL); 2444 if (eager->tcp_cred != NULL) 2445 crfree(eager->tcp_cred); 2446 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2447 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2448 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2449 2450 aconnp->conn_cred = NULL; 2451 2452 econnp->conn_zoneid = aconnp->conn_zoneid; 2453 econnp->conn_allzones = aconnp->conn_allzones; 2454 2455 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2456 aconnp->conn_mac_exempt = B_FALSE; 2457 2458 ASSERT(aconnp->conn_peercred == NULL); 2459 2460 /* Do the IPC initialization */ 2461 CONN_INC_REF(econnp); 2462 2463 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2464 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2465 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2466 2467 /* Done with old IPC. Drop its ref on its connp */ 2468 CONN_DEC_REF(aconnp); 2469 } 2470 2471 2472 /* 2473 * Adapt to the information, such as rtt and rtt_sd, provided from the 2474 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2475 * 2476 * Checks for multicast and broadcast destination address. 2477 * Returns zero on failure; non-zero if ok. 2478 * 2479 * Note that the MSS calculation here is based on the info given in 2480 * the IRE. We do not do any calculation based on TCP options. They 2481 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2482 * knows which options to use. 2483 * 2484 * Note on how TCP gets its parameters for a connection. 2485 * 2486 * When a tcp_t structure is allocated, it gets all the default parameters. 2487 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2488 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2489 * default. 2490 * 2491 * An incoming SYN with a multicast or broadcast destination address, is dropped 2492 * in 1 of 2 places. 2493 * 2494 * 1. If the packet was received over the wire it is dropped in 2495 * ip_rput_process_broadcast() 2496 * 2497 * 2. If the packet was received through internal IP loopback, i.e. the packet 2498 * was generated and received on the same machine, it is dropped in 2499 * ip_wput_local() 2500 * 2501 * An incoming SYN with a multicast or broadcast source address is always 2502 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2503 * reject an attempt to connect to a broadcast or multicast (destination) 2504 * address. 2505 */ 2506 static int 2507 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2508 { 2509 tcp_hsp_t *hsp; 2510 ire_t *ire; 2511 ire_t *sire = NULL; 2512 iulp_t *ire_uinfo = NULL; 2513 uint32_t mss_max; 2514 uint32_t mss; 2515 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2516 conn_t *connp = tcp->tcp_connp; 2517 boolean_t ire_cacheable = B_FALSE; 2518 zoneid_t zoneid = connp->conn_zoneid; 2519 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2520 MATCH_IRE_SECATTR; 2521 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2522 ill_t *ill = NULL; 2523 boolean_t incoming = (ire_mp == NULL); 2524 tcp_stack_t *tcps = tcp->tcp_tcps; 2525 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2526 2527 ASSERT(connp->conn_ire_cache == NULL); 2528 2529 if (tcp->tcp_ipversion == IPV4_VERSION) { 2530 2531 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2532 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2533 return (0); 2534 } 2535 /* 2536 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2537 * for the destination with the nexthop as gateway. 2538 * ire_ctable_lookup() is used because this particular 2539 * ire, if it exists, will be marked private. 2540 * If that is not available, use the interface ire 2541 * for the nexthop. 2542 * 2543 * TSol: tcp_update_label will detect label mismatches based 2544 * only on the destination's label, but that would not 2545 * detect label mismatches based on the security attributes 2546 * of routes or next hop gateway. Hence we need to pass the 2547 * label to ire_ftable_lookup below in order to locate the 2548 * right prefix (and/or) ire cache. Similarly we also need 2549 * pass the label to the ire_cache_lookup below to locate 2550 * the right ire that also matches on the label. 2551 */ 2552 if (tcp->tcp_connp->conn_nexthop_set) { 2553 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2554 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2555 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2556 ipst); 2557 if (ire == NULL) { 2558 ire = ire_ftable_lookup( 2559 tcp->tcp_connp->conn_nexthop_v4, 2560 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2561 tsl, match_flags, ipst); 2562 if (ire == NULL) 2563 return (0); 2564 } else { 2565 ire_uinfo = &ire->ire_uinfo; 2566 } 2567 } else { 2568 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2569 zoneid, tsl, ipst); 2570 if (ire != NULL) { 2571 ire_cacheable = B_TRUE; 2572 ire_uinfo = (ire_mp != NULL) ? 2573 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2574 &ire->ire_uinfo; 2575 2576 } else { 2577 if (ire_mp == NULL) { 2578 ire = ire_ftable_lookup( 2579 tcp->tcp_connp->conn_rem, 2580 0, 0, 0, NULL, &sire, zoneid, 0, 2581 tsl, (MATCH_IRE_RECURSIVE | 2582 MATCH_IRE_DEFAULT), ipst); 2583 if (ire == NULL) 2584 return (0); 2585 ire_uinfo = (sire != NULL) ? 2586 &sire->ire_uinfo : 2587 &ire->ire_uinfo; 2588 } else { 2589 ire = (ire_t *)ire_mp->b_rptr; 2590 ire_uinfo = 2591 &((ire_t *) 2592 ire_mp->b_rptr)->ire_uinfo; 2593 } 2594 } 2595 } 2596 ASSERT(ire != NULL); 2597 2598 if ((ire->ire_src_addr == INADDR_ANY) || 2599 (ire->ire_type & IRE_BROADCAST)) { 2600 /* 2601 * ire->ire_mp is non null when ire_mp passed in is used 2602 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2603 */ 2604 if (ire->ire_mp == NULL) 2605 ire_refrele(ire); 2606 if (sire != NULL) 2607 ire_refrele(sire); 2608 return (0); 2609 } 2610 2611 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2612 ipaddr_t src_addr; 2613 2614 /* 2615 * ip_bind_connected() has stored the correct source 2616 * address in conn_src. 2617 */ 2618 src_addr = tcp->tcp_connp->conn_src; 2619 tcp->tcp_ipha->ipha_src = src_addr; 2620 /* 2621 * Copy of the src addr. in tcp_t is needed 2622 * for the lookup funcs. 2623 */ 2624 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2625 } 2626 /* 2627 * Set the fragment bit so that IP will tell us if the MTU 2628 * should change. IP tells us the latest setting of 2629 * ip_path_mtu_discovery through ire_frag_flag. 2630 */ 2631 if (ipst->ips_ip_path_mtu_discovery) { 2632 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2633 htons(IPH_DF); 2634 } 2635 /* 2636 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2637 * for IP_NEXTHOP. No cache ire has been found for the 2638 * destination and we are working with the nexthop's 2639 * interface ire. Since we need to forward all packets 2640 * to the nexthop first, we "blindly" set tcp_localnet 2641 * to false, eventhough the destination may also be 2642 * onlink. 2643 */ 2644 if (ire_uinfo == NULL) 2645 tcp->tcp_localnet = 0; 2646 else 2647 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2648 } else { 2649 /* 2650 * For incoming connection ire_mp = NULL 2651 * For outgoing connection ire_mp != NULL 2652 * Technically we should check conn_incoming_ill 2653 * when ire_mp is NULL and conn_outgoing_ill when 2654 * ire_mp is non-NULL. But this is performance 2655 * critical path and for IPV*_BOUND_IF, outgoing 2656 * and incoming ill are always set to the same value. 2657 */ 2658 ill_t *dst_ill = NULL; 2659 ipif_t *dst_ipif = NULL; 2660 2661 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2662 2663 if (connp->conn_outgoing_ill != NULL) { 2664 /* Outgoing or incoming path */ 2665 int err; 2666 2667 dst_ill = conn_get_held_ill(connp, 2668 &connp->conn_outgoing_ill, &err); 2669 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2670 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2671 return (0); 2672 } 2673 match_flags |= MATCH_IRE_ILL; 2674 dst_ipif = dst_ill->ill_ipif; 2675 } 2676 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2677 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2678 2679 if (ire != NULL) { 2680 ire_cacheable = B_TRUE; 2681 ire_uinfo = (ire_mp != NULL) ? 2682 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2683 &ire->ire_uinfo; 2684 } else { 2685 if (ire_mp == NULL) { 2686 ire = ire_ftable_lookup_v6( 2687 &tcp->tcp_connp->conn_remv6, 2688 0, 0, 0, dst_ipif, &sire, zoneid, 2689 0, tsl, match_flags, ipst); 2690 if (ire == NULL) { 2691 if (dst_ill != NULL) 2692 ill_refrele(dst_ill); 2693 return (0); 2694 } 2695 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2696 &ire->ire_uinfo; 2697 } else { 2698 ire = (ire_t *)ire_mp->b_rptr; 2699 ire_uinfo = 2700 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2701 } 2702 } 2703 if (dst_ill != NULL) 2704 ill_refrele(dst_ill); 2705 2706 ASSERT(ire != NULL); 2707 ASSERT(ire_uinfo != NULL); 2708 2709 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2710 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2711 /* 2712 * ire->ire_mp is non null when ire_mp passed in is used 2713 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2714 */ 2715 if (ire->ire_mp == NULL) 2716 ire_refrele(ire); 2717 if (sire != NULL) 2718 ire_refrele(sire); 2719 return (0); 2720 } 2721 2722 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2723 in6_addr_t src_addr; 2724 2725 /* 2726 * ip_bind_connected_v6() has stored the correct source 2727 * address per IPv6 addr. selection policy in 2728 * conn_src_v6. 2729 */ 2730 src_addr = tcp->tcp_connp->conn_srcv6; 2731 2732 tcp->tcp_ip6h->ip6_src = src_addr; 2733 /* 2734 * Copy of the src addr. in tcp_t is needed 2735 * for the lookup funcs. 2736 */ 2737 tcp->tcp_ip_src_v6 = src_addr; 2738 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2739 &connp->conn_srcv6)); 2740 } 2741 tcp->tcp_localnet = 2742 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2743 } 2744 2745 /* 2746 * This allows applications to fail quickly when connections are made 2747 * to dead hosts. Hosts can be labeled dead by adding a reject route 2748 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2749 */ 2750 if ((ire->ire_flags & RTF_REJECT) && 2751 (ire->ire_flags & RTF_PRIVATE)) 2752 goto error; 2753 2754 /* 2755 * Make use of the cached rtt and rtt_sd values to calculate the 2756 * initial RTO. Note that they are already initialized in 2757 * tcp_init_values(). 2758 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2759 * IP_NEXTHOP, but instead are using the interface ire for the 2760 * nexthop, then we do not use the ire_uinfo from that ire to 2761 * do any initializations. 2762 */ 2763 if (ire_uinfo != NULL) { 2764 if (ire_uinfo->iulp_rtt != 0) { 2765 clock_t rto; 2766 2767 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2768 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2769 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2770 tcps->tcps_rexmit_interval_extra + 2771 (tcp->tcp_rtt_sa >> 5); 2772 2773 if (rto > tcps->tcps_rexmit_interval_max) { 2774 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2775 } else if (rto < tcps->tcps_rexmit_interval_min) { 2776 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2777 } else { 2778 tcp->tcp_rto = rto; 2779 } 2780 } 2781 if (ire_uinfo->iulp_ssthresh != 0) 2782 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2783 else 2784 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2785 if (ire_uinfo->iulp_spipe > 0) { 2786 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2787 tcps->tcps_max_buf); 2788 if (tcps->tcps_snd_lowat_fraction != 0) 2789 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2790 tcps->tcps_snd_lowat_fraction; 2791 (void) tcp_maxpsz_set(tcp, B_TRUE); 2792 } 2793 /* 2794 * Note that up till now, acceptor always inherits receive 2795 * window from the listener. But if there is a metrics 2796 * associated with a host, we should use that instead of 2797 * inheriting it from listener. Thus we need to pass this 2798 * info back to the caller. 2799 */ 2800 if (ire_uinfo->iulp_rpipe > 0) { 2801 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2802 tcps->tcps_max_buf); 2803 } 2804 2805 if (ire_uinfo->iulp_rtomax > 0) { 2806 tcp->tcp_second_timer_threshold = 2807 ire_uinfo->iulp_rtomax; 2808 } 2809 2810 /* 2811 * Use the metric option settings, iulp_tstamp_ok and 2812 * iulp_wscale_ok, only for active open. What this means 2813 * is that if the other side uses timestamp or window 2814 * scale option, TCP will also use those options. That 2815 * is for passive open. If the application sets a 2816 * large window, window scale is enabled regardless of 2817 * the value in iulp_wscale_ok. This is the behavior 2818 * since 2.6. So we keep it. 2819 * The only case left in passive open processing is the 2820 * check for SACK. 2821 * For ECN, it should probably be like SACK. But the 2822 * current value is binary, so we treat it like the other 2823 * cases. The metric only controls active open.For passive 2824 * open, the ndd param, tcp_ecn_permitted, controls the 2825 * behavior. 2826 */ 2827 if (!tcp_detached) { 2828 /* 2829 * The if check means that the following can only 2830 * be turned on by the metrics only IRE, but not off. 2831 */ 2832 if (ire_uinfo->iulp_tstamp_ok) 2833 tcp->tcp_snd_ts_ok = B_TRUE; 2834 if (ire_uinfo->iulp_wscale_ok) 2835 tcp->tcp_snd_ws_ok = B_TRUE; 2836 if (ire_uinfo->iulp_sack == 2) 2837 tcp->tcp_snd_sack_ok = B_TRUE; 2838 if (ire_uinfo->iulp_ecn_ok) 2839 tcp->tcp_ecn_ok = B_TRUE; 2840 } else { 2841 /* 2842 * Passive open. 2843 * 2844 * As above, the if check means that SACK can only be 2845 * turned on by the metric only IRE. 2846 */ 2847 if (ire_uinfo->iulp_sack > 0) { 2848 tcp->tcp_snd_sack_ok = B_TRUE; 2849 } 2850 } 2851 } 2852 2853 2854 /* 2855 * XXX: Note that currently, ire_max_frag can be as small as 68 2856 * because of PMTUd. So tcp_mss may go to negative if combined 2857 * length of all those options exceeds 28 bytes. But because 2858 * of the tcp_mss_min check below, we may not have a problem if 2859 * tcp_mss_min is of a reasonable value. The default is 1 so 2860 * the negative problem still exists. And the check defeats PMTUd. 2861 * In fact, if PMTUd finds that the MSS should be smaller than 2862 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2863 * value. 2864 * 2865 * We do not deal with that now. All those problems related to 2866 * PMTUd will be fixed later. 2867 */ 2868 ASSERT(ire->ire_max_frag != 0); 2869 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2870 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2871 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2872 mss = MIN(mss, IPV6_MIN_MTU); 2873 } 2874 } 2875 2876 /* Sanity check for MSS value. */ 2877 if (tcp->tcp_ipversion == IPV4_VERSION) 2878 mss_max = tcps->tcps_mss_max_ipv4; 2879 else 2880 mss_max = tcps->tcps_mss_max_ipv6; 2881 2882 if (tcp->tcp_ipversion == IPV6_VERSION && 2883 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2884 /* 2885 * After receiving an ICMPv6 "packet too big" message with a 2886 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2887 * will insert a 8-byte fragment header in every packet; we 2888 * reduce the MSS by that amount here. 2889 */ 2890 mss -= sizeof (ip6_frag_t); 2891 } 2892 2893 if (tcp->tcp_ipsec_overhead == 0) 2894 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2895 2896 mss -= tcp->tcp_ipsec_overhead; 2897 2898 if (mss < tcps->tcps_mss_min) 2899 mss = tcps->tcps_mss_min; 2900 if (mss > mss_max) 2901 mss = mss_max; 2902 2903 /* Note that this is the maximum MSS, excluding all options. */ 2904 tcp->tcp_mss = mss; 2905 2906 /* 2907 * Initialize the ISS here now that we have the full connection ID. 2908 * The RFC 1948 method of initial sequence number generation requires 2909 * knowledge of the full connection ID before setting the ISS. 2910 */ 2911 2912 tcp_iss_init(tcp); 2913 2914 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2915 tcp->tcp_loopback = B_TRUE; 2916 2917 if (tcp->tcp_ipversion == IPV4_VERSION) { 2918 hsp = tcp_hsp_lookup(tcp->tcp_remote, tcps); 2919 } else { 2920 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6, tcps); 2921 } 2922 2923 if (hsp != NULL) { 2924 /* Only modify if we're going to make them bigger */ 2925 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2926 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2927 if (tcps->tcps_snd_lowat_fraction != 0) 2928 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2929 tcps->tcps_snd_lowat_fraction; 2930 } 2931 2932 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2933 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2934 } 2935 2936 /* Copy timestamp flag only for active open */ 2937 if (!tcp_detached) 2938 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 2939 } 2940 2941 if (sire != NULL) 2942 IRE_REFRELE(sire); 2943 2944 /* 2945 * If we got an IRE_CACHE and an ILL, go through their properties; 2946 * otherwise, this is deferred until later when we have an IRE_CACHE. 2947 */ 2948 if (tcp->tcp_loopback || 2949 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2950 /* 2951 * For incoming, see if this tcp may be MDT-capable. For 2952 * outgoing, this process has been taken care of through 2953 * tcp_rput_other. 2954 */ 2955 tcp_ire_ill_check(tcp, ire, ill, incoming); 2956 tcp->tcp_ire_ill_check_done = B_TRUE; 2957 } 2958 2959 mutex_enter(&connp->conn_lock); 2960 /* 2961 * Make sure that conn is not marked incipient 2962 * for incoming connections. A blind 2963 * removal of incipient flag is cheaper than 2964 * check and removal. 2965 */ 2966 connp->conn_state_flags &= ~CONN_INCIPIENT; 2967 2968 /* 2969 * Must not cache forwarding table routes 2970 * or recache an IRE after the conn_t has 2971 * had conn_ire_cache cleared and is flagged 2972 * unusable, (see the CONN_CACHE_IRE() macro). 2973 */ 2974 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 2975 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2976 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2977 connp->conn_ire_cache = ire; 2978 IRE_UNTRACE_REF(ire); 2979 rw_exit(&ire->ire_bucket->irb_lock); 2980 mutex_exit(&connp->conn_lock); 2981 return (1); 2982 } 2983 rw_exit(&ire->ire_bucket->irb_lock); 2984 } 2985 mutex_exit(&connp->conn_lock); 2986 2987 if (ire->ire_mp == NULL) 2988 ire_refrele(ire); 2989 return (1); 2990 2991 error: 2992 if (ire->ire_mp == NULL) 2993 ire_refrele(ire); 2994 if (sire != NULL) 2995 ire_refrele(sire); 2996 return (0); 2997 } 2998 2999 /* 3000 * tcp_bind is called (holding the writer lock) by tcp_wput_proto to process a 3001 * O_T_BIND_REQ/T_BIND_REQ message. 3002 */ 3003 static void 3004 tcp_bind(tcp_t *tcp, mblk_t *mp) 3005 { 3006 sin_t *sin; 3007 sin6_t *sin6; 3008 mblk_t *mp1; 3009 in_port_t requested_port; 3010 in_port_t allocated_port; 3011 struct T_bind_req *tbr; 3012 boolean_t bind_to_req_port_only; 3013 boolean_t backlog_update = B_FALSE; 3014 boolean_t user_specified; 3015 in6_addr_t v6addr; 3016 ipaddr_t v4addr; 3017 uint_t origipversion; 3018 int err; 3019 queue_t *q = tcp->tcp_wq; 3020 conn_t *connp = tcp->tcp_connp; 3021 mlp_type_t addrtype, mlptype; 3022 zone_t *zone; 3023 cred_t *cr; 3024 in_port_t mlp_port; 3025 tcp_stack_t *tcps = tcp->tcp_tcps; 3026 3027 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3028 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3029 if (tcp->tcp_debug) { 3030 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3031 "tcp_bind: bad req, len %u", 3032 (uint_t)(mp->b_wptr - mp->b_rptr)); 3033 } 3034 tcp_err_ack(tcp, mp, TPROTO, 0); 3035 return; 3036 } 3037 /* Make sure the largest address fits */ 3038 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3039 if (mp1 == NULL) { 3040 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3041 return; 3042 } 3043 mp = mp1; 3044 tbr = (struct T_bind_req *)mp->b_rptr; 3045 if (tcp->tcp_state >= TCPS_BOUND) { 3046 if ((tcp->tcp_state == TCPS_BOUND || 3047 tcp->tcp_state == TCPS_LISTEN) && 3048 tcp->tcp_conn_req_max != tbr->CONIND_number && 3049 tbr->CONIND_number > 0) { 3050 /* 3051 * Handle listen() increasing CONIND_number. 3052 * This is more "liberal" then what the TPI spec 3053 * requires but is needed to avoid a t_unbind 3054 * when handling listen() since the port number 3055 * might be "stolen" between the unbind and bind. 3056 */ 3057 backlog_update = B_TRUE; 3058 goto do_bind; 3059 } 3060 if (tcp->tcp_debug) { 3061 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3062 "tcp_bind: bad state, %d", tcp->tcp_state); 3063 } 3064 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 3065 return; 3066 } 3067 origipversion = tcp->tcp_ipversion; 3068 3069 switch (tbr->ADDR_length) { 3070 case 0: /* request for a generic port */ 3071 tbr->ADDR_offset = sizeof (struct T_bind_req); 3072 if (tcp->tcp_family == AF_INET) { 3073 tbr->ADDR_length = sizeof (sin_t); 3074 sin = (sin_t *)&tbr[1]; 3075 *sin = sin_null; 3076 sin->sin_family = AF_INET; 3077 mp->b_wptr = (uchar_t *)&sin[1]; 3078 tcp->tcp_ipversion = IPV4_VERSION; 3079 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 3080 } else { 3081 ASSERT(tcp->tcp_family == AF_INET6); 3082 tbr->ADDR_length = sizeof (sin6_t); 3083 sin6 = (sin6_t *)&tbr[1]; 3084 *sin6 = sin6_null; 3085 sin6->sin6_family = AF_INET6; 3086 mp->b_wptr = (uchar_t *)&sin6[1]; 3087 tcp->tcp_ipversion = IPV6_VERSION; 3088 V6_SET_ZERO(v6addr); 3089 } 3090 requested_port = 0; 3091 break; 3092 3093 case sizeof (sin_t): /* Complete IPv4 address */ 3094 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3095 sizeof (sin_t)); 3096 if (sin == NULL || !OK_32PTR((char *)sin)) { 3097 if (tcp->tcp_debug) { 3098 (void) strlog(TCP_MOD_ID, 0, 1, 3099 SL_ERROR|SL_TRACE, 3100 "tcp_bind: bad address parameter, " 3101 "offset %d, len %d", 3102 tbr->ADDR_offset, tbr->ADDR_length); 3103 } 3104 tcp_err_ack(tcp, mp, TPROTO, 0); 3105 return; 3106 } 3107 /* 3108 * With sockets sockfs will accept bogus sin_family in 3109 * bind() and replace it with the family used in the socket 3110 * call. 3111 */ 3112 if (sin->sin_family != AF_INET || 3113 tcp->tcp_family != AF_INET) { 3114 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3115 return; 3116 } 3117 requested_port = ntohs(sin->sin_port); 3118 tcp->tcp_ipversion = IPV4_VERSION; 3119 v4addr = sin->sin_addr.s_addr; 3120 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 3121 break; 3122 3123 case sizeof (sin6_t): /* Complete IPv6 address */ 3124 sin6 = (sin6_t *)mi_offset_param(mp, 3125 tbr->ADDR_offset, sizeof (sin6_t)); 3126 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3127 if (tcp->tcp_debug) { 3128 (void) strlog(TCP_MOD_ID, 0, 1, 3129 SL_ERROR|SL_TRACE, 3130 "tcp_bind: bad IPv6 address parameter, " 3131 "offset %d, len %d", tbr->ADDR_offset, 3132 tbr->ADDR_length); 3133 } 3134 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3135 return; 3136 } 3137 if (sin6->sin6_family != AF_INET6 || 3138 tcp->tcp_family != AF_INET6) { 3139 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3140 return; 3141 } 3142 requested_port = ntohs(sin6->sin6_port); 3143 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 3144 IPV4_VERSION : IPV6_VERSION; 3145 v6addr = sin6->sin6_addr; 3146 break; 3147 3148 default: 3149 if (tcp->tcp_debug) { 3150 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3151 "tcp_bind: bad address length, %d", 3152 tbr->ADDR_length); 3153 } 3154 tcp_err_ack(tcp, mp, TBADADDR, 0); 3155 return; 3156 } 3157 tcp->tcp_bound_source_v6 = v6addr; 3158 3159 /* Check for change in ipversion */ 3160 if (origipversion != tcp->tcp_ipversion) { 3161 ASSERT(tcp->tcp_family == AF_INET6); 3162 err = tcp->tcp_ipversion == IPV6_VERSION ? 3163 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 3164 if (err) { 3165 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3166 return; 3167 } 3168 } 3169 3170 /* 3171 * Initialize family specific fields. Copy of the src addr. 3172 * in tcp_t is needed for the lookup funcs. 3173 */ 3174 if (tcp->tcp_ipversion == IPV6_VERSION) { 3175 tcp->tcp_ip6h->ip6_src = v6addr; 3176 } else { 3177 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 3178 } 3179 tcp->tcp_ip_src_v6 = v6addr; 3180 3181 /* 3182 * For O_T_BIND_REQ: 3183 * Verify that the target port/addr is available, or choose 3184 * another. 3185 * For T_BIND_REQ: 3186 * Verify that the target port/addr is available or fail. 3187 * In both cases when it succeeds the tcp is inserted in the 3188 * bind hash table. This ensures that the operation is atomic 3189 * under the lock on the hash bucket. 3190 */ 3191 bind_to_req_port_only = requested_port != 0 && 3192 tbr->PRIM_type != O_T_BIND_REQ; 3193 /* 3194 * Get a valid port (within the anonymous range and should not 3195 * be a privileged one) to use if the user has not given a port. 3196 * If multiple threads are here, they may all start with 3197 * with the same initial port. But, it should be fine as long as 3198 * tcp_bindi will ensure that no two threads will be assigned 3199 * the same port. 3200 * 3201 * NOTE: XXX If a privileged process asks for an anonymous port, we 3202 * still check for ports only in the range > tcp_smallest_non_priv_port, 3203 * unless TCP_ANONPRIVBIND option is set. 3204 */ 3205 mlptype = mlptSingle; 3206 mlp_port = requested_port; 3207 if (requested_port == 0) { 3208 requested_port = tcp->tcp_anon_priv_bind ? 3209 tcp_get_next_priv_port(tcp) : 3210 tcp_update_next_port(tcps->tcps_next_port_to_try, 3211 tcp, B_TRUE); 3212 if (requested_port == 0) { 3213 tcp_err_ack(tcp, mp, TNOADDR, 0); 3214 return; 3215 } 3216 user_specified = B_FALSE; 3217 3218 /* 3219 * If the user went through one of the RPC interfaces to create 3220 * this socket and RPC is MLP in this zone, then give him an 3221 * anonymous MLP. 3222 */ 3223 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3224 if (connp->conn_anon_mlp && is_system_labeled()) { 3225 zone = crgetzone(cr); 3226 addrtype = tsol_mlp_addr_type(zone->zone_id, 3227 IPV6_VERSION, &v6addr, 3228 tcps->tcps_netstack->netstack_ip); 3229 if (addrtype == mlptSingle) { 3230 tcp_err_ack(tcp, mp, TNOADDR, 0); 3231 return; 3232 } 3233 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3234 PMAPPORT, addrtype); 3235 mlp_port = PMAPPORT; 3236 } 3237 } else { 3238 int i; 3239 boolean_t priv = B_FALSE; 3240 3241 /* 3242 * If the requested_port is in the well-known privileged range, 3243 * verify that the stream was opened by a privileged user. 3244 * Note: No locks are held when inspecting tcp_g_*epriv_ports 3245 * but instead the code relies on: 3246 * - the fact that the address of the array and its size never 3247 * changes 3248 * - the atomic assignment of the elements of the array 3249 */ 3250 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3251 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 3252 priv = B_TRUE; 3253 } else { 3254 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 3255 if (requested_port == 3256 tcps->tcps_g_epriv_ports[i]) { 3257 priv = B_TRUE; 3258 break; 3259 } 3260 } 3261 } 3262 if (priv) { 3263 if (secpolicy_net_privaddr(cr, requested_port, 3264 IPPROTO_TCP) != 0) { 3265 if (tcp->tcp_debug) { 3266 (void) strlog(TCP_MOD_ID, 0, 1, 3267 SL_ERROR|SL_TRACE, 3268 "tcp_bind: no priv for port %d", 3269 requested_port); 3270 } 3271 tcp_err_ack(tcp, mp, TACCES, 0); 3272 return; 3273 } 3274 } 3275 user_specified = B_TRUE; 3276 3277 if (is_system_labeled()) { 3278 zone = crgetzone(cr); 3279 addrtype = tsol_mlp_addr_type(zone->zone_id, 3280 IPV6_VERSION, &v6addr, 3281 tcps->tcps_netstack->netstack_ip); 3282 if (addrtype == mlptSingle) { 3283 tcp_err_ack(tcp, mp, TNOADDR, 0); 3284 return; 3285 } 3286 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3287 requested_port, addrtype); 3288 } 3289 } 3290 3291 if (mlptype != mlptSingle) { 3292 if (secpolicy_net_bindmlp(cr) != 0) { 3293 if (tcp->tcp_debug) { 3294 (void) strlog(TCP_MOD_ID, 0, 1, 3295 SL_ERROR|SL_TRACE, 3296 "tcp_bind: no priv for multilevel port %d", 3297 requested_port); 3298 } 3299 tcp_err_ack(tcp, mp, TACCES, 0); 3300 return; 3301 } 3302 3303 /* 3304 * If we're specifically binding a shared IP address and the 3305 * port is MLP on shared addresses, then check to see if this 3306 * zone actually owns the MLP. Reject if not. 3307 */ 3308 if (mlptype == mlptShared && addrtype == mlptShared) { 3309 /* 3310 * No need to handle exclusive-stack zones since 3311 * ALL_ZONES only applies to the shared stack. 3312 */ 3313 zoneid_t mlpzone; 3314 3315 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 3316 htons(mlp_port)); 3317 if (connp->conn_zoneid != mlpzone) { 3318 if (tcp->tcp_debug) { 3319 (void) strlog(TCP_MOD_ID, 0, 1, 3320 SL_ERROR|SL_TRACE, 3321 "tcp_bind: attempt to bind port " 3322 "%d on shared addr in zone %d " 3323 "(should be %d)", 3324 mlp_port, connp->conn_zoneid, 3325 mlpzone); 3326 } 3327 tcp_err_ack(tcp, mp, TACCES, 0); 3328 return; 3329 } 3330 } 3331 3332 if (!user_specified) { 3333 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3334 requested_port, B_TRUE); 3335 if (err != 0) { 3336 if (tcp->tcp_debug) { 3337 (void) strlog(TCP_MOD_ID, 0, 1, 3338 SL_ERROR|SL_TRACE, 3339 "tcp_bind: cannot establish anon " 3340 "MLP for port %d", 3341 requested_port); 3342 } 3343 tcp_err_ack(tcp, mp, TSYSERR, err); 3344 return; 3345 } 3346 connp->conn_anon_port = B_TRUE; 3347 } 3348 connp->conn_mlp_type = mlptype; 3349 } 3350 3351 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 3352 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 3353 3354 if (allocated_port == 0) { 3355 connp->conn_mlp_type = mlptSingle; 3356 if (connp->conn_anon_port) { 3357 connp->conn_anon_port = B_FALSE; 3358 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3359 requested_port, B_FALSE); 3360 } 3361 if (bind_to_req_port_only) { 3362 if (tcp->tcp_debug) { 3363 (void) strlog(TCP_MOD_ID, 0, 1, 3364 SL_ERROR|SL_TRACE, 3365 "tcp_bind: requested addr busy"); 3366 } 3367 tcp_err_ack(tcp, mp, TADDRBUSY, 0); 3368 } else { 3369 /* If we are out of ports, fail the bind. */ 3370 if (tcp->tcp_debug) { 3371 (void) strlog(TCP_MOD_ID, 0, 1, 3372 SL_ERROR|SL_TRACE, 3373 "tcp_bind: out of ports?"); 3374 } 3375 tcp_err_ack(tcp, mp, TNOADDR, 0); 3376 } 3377 return; 3378 } 3379 ASSERT(tcp->tcp_state == TCPS_BOUND); 3380 do_bind: 3381 if (!backlog_update) { 3382 if (tcp->tcp_family == AF_INET) 3383 sin->sin_port = htons(allocated_port); 3384 else 3385 sin6->sin6_port = htons(allocated_port); 3386 } 3387 if (tcp->tcp_family == AF_INET) { 3388 if (tbr->CONIND_number != 0) { 3389 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3390 sizeof (sin_t)); 3391 } else { 3392 /* Just verify the local IP address */ 3393 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, IP_ADDR_LEN); 3394 } 3395 } else { 3396 if (tbr->CONIND_number != 0) { 3397 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3398 sizeof (sin6_t)); 3399 } else { 3400 /* Just verify the local IP address */ 3401 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3402 IPV6_ADDR_LEN); 3403 } 3404 } 3405 if (mp1 == NULL) { 3406 if (connp->conn_anon_port) { 3407 connp->conn_anon_port = B_FALSE; 3408 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3409 requested_port, B_FALSE); 3410 } 3411 connp->conn_mlp_type = mlptSingle; 3412 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3413 return; 3414 } 3415 3416 tbr->PRIM_type = T_BIND_ACK; 3417 mp->b_datap->db_type = M_PCPROTO; 3418 3419 /* Chain in the reply mp for tcp_rput() */ 3420 mp1->b_cont = mp; 3421 mp = mp1; 3422 3423 tcp->tcp_conn_req_max = tbr->CONIND_number; 3424 if (tcp->tcp_conn_req_max) { 3425 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 3426 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 3427 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 3428 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 3429 /* 3430 * If this is a listener, do not reset the eager list 3431 * and other stuffs. Note that we don't check if the 3432 * existing eager list meets the new tcp_conn_req_max 3433 * requirement. 3434 */ 3435 if (tcp->tcp_state != TCPS_LISTEN) { 3436 tcp->tcp_state = TCPS_LISTEN; 3437 /* Initialize the chain. Don't need the eager_lock */ 3438 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 3439 tcp->tcp_eager_next_drop_q0 = tcp; 3440 tcp->tcp_eager_prev_drop_q0 = tcp; 3441 tcp->tcp_second_ctimer_threshold = 3442 tcps->tcps_ip_abort_linterval; 3443 } 3444 } 3445 3446 /* 3447 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 3448 * processing continues in tcp_rput_other(). 3449 * 3450 * We need to make sure that the conn_recv is set to a non-null 3451 * value before we insert the conn into the classifier table. 3452 * This is to avoid a race with an incoming packet which does an 3453 * ipcl_classify(). 3454 */ 3455 connp->conn_recv = tcp_conn_request; 3456 if (tcp->tcp_family == AF_INET6) { 3457 ASSERT(tcp->tcp_connp->conn_af_isv6); 3458 mp = ip_bind_v6(q, mp, tcp->tcp_connp, &tcp->tcp_sticky_ipp); 3459 } else { 3460 ASSERT(!tcp->tcp_connp->conn_af_isv6); 3461 mp = ip_bind_v4(q, mp, tcp->tcp_connp); 3462 } 3463 /* 3464 * If the bind cannot complete immediately 3465 * IP will arrange to call tcp_rput_other 3466 * when the bind completes. 3467 */ 3468 if (mp != NULL) { 3469 tcp_rput_other(tcp, mp); 3470 } else { 3471 /* 3472 * Bind will be resumed later. Need to ensure 3473 * that conn doesn't disappear when that happens. 3474 * This will be decremented in ip_resume_tcp_bind(). 3475 */ 3476 CONN_INC_REF(tcp->tcp_connp); 3477 } 3478 } 3479 3480 3481 /* 3482 * If the "bind_to_req_port_only" parameter is set, if the requested port 3483 * number is available, return it, If not return 0 3484 * 3485 * If "bind_to_req_port_only" parameter is not set and 3486 * If the requested port number is available, return it. If not, return 3487 * the first anonymous port we happen across. If no anonymous ports are 3488 * available, return 0. addr is the requested local address, if any. 3489 * 3490 * In either case, when succeeding update the tcp_t to record the port number 3491 * and insert it in the bind hash table. 3492 * 3493 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3494 * without setting SO_REUSEADDR. This is needed so that they 3495 * can be viewed as two independent transport protocols. 3496 */ 3497 static in_port_t 3498 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3499 int reuseaddr, boolean_t quick_connect, 3500 boolean_t bind_to_req_port_only, boolean_t user_specified) 3501 { 3502 /* number of times we have run around the loop */ 3503 int count = 0; 3504 /* maximum number of times to run around the loop */ 3505 int loopmax; 3506 conn_t *connp = tcp->tcp_connp; 3507 zoneid_t zoneid = connp->conn_zoneid; 3508 tcp_stack_t *tcps = tcp->tcp_tcps; 3509 3510 /* 3511 * Lookup for free addresses is done in a loop and "loopmax" 3512 * influences how long we spin in the loop 3513 */ 3514 if (bind_to_req_port_only) { 3515 /* 3516 * If the requested port is busy, don't bother to look 3517 * for a new one. Setting loop maximum count to 1 has 3518 * that effect. 3519 */ 3520 loopmax = 1; 3521 } else { 3522 /* 3523 * If the requested port is busy, look for a free one 3524 * in the anonymous port range. 3525 * Set loopmax appropriately so that one does not look 3526 * forever in the case all of the anonymous ports are in use. 3527 */ 3528 if (tcp->tcp_anon_priv_bind) { 3529 /* 3530 * loopmax = 3531 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3532 */ 3533 loopmax = IPPORT_RESERVED - 3534 tcps->tcps_min_anonpriv_port; 3535 } else { 3536 loopmax = (tcps->tcps_largest_anon_port - 3537 tcps->tcps_smallest_anon_port + 1); 3538 } 3539 } 3540 do { 3541 uint16_t lport; 3542 tf_t *tbf; 3543 tcp_t *ltcp; 3544 conn_t *lconnp; 3545 3546 lport = htons(port); 3547 3548 /* 3549 * Ensure that the tcp_t is not currently in the bind hash. 3550 * Hold the lock on the hash bucket to ensure that 3551 * the duplicate check plus the insertion is an atomic 3552 * operation. 3553 * 3554 * This function does an inline lookup on the bind hash list 3555 * Make sure that we access only members of tcp_t 3556 * and that we don't look at tcp_tcp, since we are not 3557 * doing a CONN_INC_REF. 3558 */ 3559 tcp_bind_hash_remove(tcp); 3560 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3561 mutex_enter(&tbf->tf_lock); 3562 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3563 ltcp = ltcp->tcp_bind_hash) { 3564 boolean_t not_socket; 3565 boolean_t exclbind; 3566 3567 if (lport != ltcp->tcp_lport) 3568 continue; 3569 3570 lconnp = ltcp->tcp_connp; 3571 3572 /* 3573 * On a labeled system, we must treat bindings to ports 3574 * on shared IP addresses by sockets with MAC exemption 3575 * privilege as being in all zones, as there's 3576 * otherwise no way to identify the right receiver. 3577 */ 3578 if (!(IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) || 3579 IPCL_ZONE_MATCH(connp, 3580 ltcp->tcp_connp->conn_zoneid)) && 3581 !lconnp->conn_mac_exempt && 3582 !connp->conn_mac_exempt) 3583 continue; 3584 3585 /* 3586 * If TCP_EXCLBIND is set for either the bound or 3587 * binding endpoint, the semantics of bind 3588 * is changed according to the following. 3589 * 3590 * spec = specified address (v4 or v6) 3591 * unspec = unspecified address (v4 or v6) 3592 * A = specified addresses are different for endpoints 3593 * 3594 * bound bind to allowed 3595 * ------------------------------------- 3596 * unspec unspec no 3597 * unspec spec no 3598 * spec unspec no 3599 * spec spec yes if A 3600 * 3601 * For labeled systems, SO_MAC_EXEMPT behaves the same 3602 * as TCP_EXCLBIND, except that zoneid is ignored. 3603 * 3604 * Note: 3605 * 3606 * 1. Because of TLI semantics, an endpoint can go 3607 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3608 * TCPS_BOUND, depending on whether it is originally 3609 * a listener or not. That is why we need to check 3610 * for states greater than or equal to TCPS_BOUND 3611 * here. 3612 * 3613 * 2. Ideally, we should only check for state equals 3614 * to TCPS_LISTEN. And the following check should be 3615 * added. 3616 * 3617 * if (ltcp->tcp_state == TCPS_LISTEN || 3618 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3619 * ... 3620 * } 3621 * 3622 * The semantics will be changed to this. If the 3623 * endpoint on the list is in state not equal to 3624 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3625 * set, let the bind succeed. 3626 * 3627 * Because of (1), we cannot do that for TLI 3628 * endpoints. But we can do that for socket endpoints. 3629 * If in future, we can change this going back 3630 * semantics, we can use the above check for TLI also. 3631 */ 3632 not_socket = !(TCP_IS_SOCKET(ltcp) && 3633 TCP_IS_SOCKET(tcp)); 3634 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3635 3636 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3637 (exclbind && (not_socket || 3638 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3639 if (V6_OR_V4_INADDR_ANY( 3640 ltcp->tcp_bound_source_v6) || 3641 V6_OR_V4_INADDR_ANY(*laddr) || 3642 IN6_ARE_ADDR_EQUAL(laddr, 3643 <cp->tcp_bound_source_v6)) { 3644 break; 3645 } 3646 continue; 3647 } 3648 3649 /* 3650 * Check ipversion to allow IPv4 and IPv6 sockets to 3651 * have disjoint port number spaces, if *_EXCLBIND 3652 * is not set and only if the application binds to a 3653 * specific port. We use the same autoassigned port 3654 * number space for IPv4 and IPv6 sockets. 3655 */ 3656 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3657 bind_to_req_port_only) 3658 continue; 3659 3660 /* 3661 * Ideally, we should make sure that the source 3662 * address, remote address, and remote port in the 3663 * four tuple for this tcp-connection is unique. 3664 * However, trying to find out the local source 3665 * address would require too much code duplication 3666 * with IP, since IP needs needs to have that code 3667 * to support userland TCP implementations. 3668 */ 3669 if (quick_connect && 3670 (ltcp->tcp_state > TCPS_LISTEN) && 3671 ((tcp->tcp_fport != ltcp->tcp_fport) || 3672 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3673 <cp->tcp_remote_v6))) 3674 continue; 3675 3676 if (!reuseaddr) { 3677 /* 3678 * No socket option SO_REUSEADDR. 3679 * If existing port is bound to 3680 * a non-wildcard IP address 3681 * and the requesting stream is 3682 * bound to a distinct 3683 * different IP addresses 3684 * (non-wildcard, also), keep 3685 * going. 3686 */ 3687 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3688 !V6_OR_V4_INADDR_ANY( 3689 ltcp->tcp_bound_source_v6) && 3690 !IN6_ARE_ADDR_EQUAL(laddr, 3691 <cp->tcp_bound_source_v6)) 3692 continue; 3693 if (ltcp->tcp_state >= TCPS_BOUND) { 3694 /* 3695 * This port is being used and 3696 * its state is >= TCPS_BOUND, 3697 * so we can't bind to it. 3698 */ 3699 break; 3700 } 3701 } else { 3702 /* 3703 * socket option SO_REUSEADDR is set on the 3704 * binding tcp_t. 3705 * 3706 * If two streams are bound to 3707 * same IP address or both addr 3708 * and bound source are wildcards 3709 * (INADDR_ANY), we want to stop 3710 * searching. 3711 * We have found a match of IP source 3712 * address and source port, which is 3713 * refused regardless of the 3714 * SO_REUSEADDR setting, so we break. 3715 */ 3716 if (IN6_ARE_ADDR_EQUAL(laddr, 3717 <cp->tcp_bound_source_v6) && 3718 (ltcp->tcp_state == TCPS_LISTEN || 3719 ltcp->tcp_state == TCPS_BOUND)) 3720 break; 3721 } 3722 } 3723 if (ltcp != NULL) { 3724 /* The port number is busy */ 3725 mutex_exit(&tbf->tf_lock); 3726 } else { 3727 /* 3728 * This port is ours. Insert in fanout and mark as 3729 * bound to prevent others from getting the port 3730 * number. 3731 */ 3732 tcp->tcp_state = TCPS_BOUND; 3733 tcp->tcp_lport = htons(port); 3734 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3735 3736 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3737 tcp->tcp_lport)] == tbf); 3738 tcp_bind_hash_insert(tbf, tcp, 1); 3739 3740 mutex_exit(&tbf->tf_lock); 3741 3742 /* 3743 * We don't want tcp_next_port_to_try to "inherit" 3744 * a port number supplied by the user in a bind. 3745 */ 3746 if (user_specified) 3747 return (port); 3748 3749 /* 3750 * This is the only place where tcp_next_port_to_try 3751 * is updated. After the update, it may or may not 3752 * be in the valid range. 3753 */ 3754 if (!tcp->tcp_anon_priv_bind) 3755 tcps->tcps_next_port_to_try = port + 1; 3756 return (port); 3757 } 3758 3759 if (tcp->tcp_anon_priv_bind) { 3760 port = tcp_get_next_priv_port(tcp); 3761 } else { 3762 if (count == 0 && user_specified) { 3763 /* 3764 * We may have to return an anonymous port. So 3765 * get one to start with. 3766 */ 3767 port = 3768 tcp_update_next_port( 3769 tcps->tcps_next_port_to_try, 3770 tcp, B_TRUE); 3771 user_specified = B_FALSE; 3772 } else { 3773 port = tcp_update_next_port(port + 1, tcp, 3774 B_FALSE); 3775 } 3776 } 3777 if (port == 0) 3778 break; 3779 3780 /* 3781 * Don't let this loop run forever in the case where 3782 * all of the anonymous ports are in use. 3783 */ 3784 } while (++count < loopmax); 3785 return (0); 3786 } 3787 3788 /* 3789 * tcp_clean_death / tcp_close_detached must not be called more than once 3790 * on a tcp. Thus every function that potentially calls tcp_clean_death 3791 * must check for the tcp state before calling tcp_clean_death. 3792 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3793 * tcp_timer_handler, all check for the tcp state. 3794 */ 3795 /* ARGSUSED */ 3796 void 3797 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3798 { 3799 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3800 3801 freemsg(mp); 3802 if (tcp->tcp_state > TCPS_BOUND) 3803 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, 3804 ETIMEDOUT, 5); 3805 } 3806 3807 /* 3808 * We are dying for some reason. Try to do it gracefully. (May be called 3809 * as writer.) 3810 * 3811 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3812 * done by a service procedure). 3813 * TBD - Should the return value distinguish between the tcp_t being 3814 * freed and it being reinitialized? 3815 */ 3816 static int 3817 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3818 { 3819 mblk_t *mp; 3820 queue_t *q; 3821 tcp_stack_t *tcps = tcp->tcp_tcps; 3822 sodirect_t *sodp; 3823 3824 TCP_CLD_STAT(tag); 3825 3826 #if TCP_TAG_CLEAN_DEATH 3827 tcp->tcp_cleandeathtag = tag; 3828 #endif 3829 3830 if (tcp->tcp_fused) 3831 tcp_unfuse(tcp); 3832 3833 if (tcp->tcp_linger_tid != 0 && 3834 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3835 tcp_stop_lingering(tcp); 3836 } 3837 3838 ASSERT(tcp != NULL); 3839 ASSERT((tcp->tcp_family == AF_INET && 3840 tcp->tcp_ipversion == IPV4_VERSION) || 3841 (tcp->tcp_family == AF_INET6 && 3842 (tcp->tcp_ipversion == IPV4_VERSION || 3843 tcp->tcp_ipversion == IPV6_VERSION))); 3844 3845 if (TCP_IS_DETACHED(tcp)) { 3846 if (tcp->tcp_hard_binding) { 3847 /* 3848 * Its an eager that we are dealing with. We close the 3849 * eager but in case a conn_ind has already gone to the 3850 * listener, let tcp_accept_finish() send a discon_ind 3851 * to the listener and drop the last reference. If the 3852 * listener doesn't even know about the eager i.e. the 3853 * conn_ind hasn't gone up, blow away the eager and drop 3854 * the last reference as well. If the conn_ind has gone 3855 * up, state should be BOUND. tcp_accept_finish 3856 * will figure out that the connection has received a 3857 * RST and will send a DISCON_IND to the application. 3858 */ 3859 tcp_closei_local(tcp); 3860 if (!tcp->tcp_tconnind_started) { 3861 CONN_DEC_REF(tcp->tcp_connp); 3862 } else { 3863 tcp->tcp_state = TCPS_BOUND; 3864 } 3865 } else { 3866 tcp_close_detached(tcp); 3867 } 3868 return (0); 3869 } 3870 3871 TCP_STAT(tcps, tcp_clean_death_nondetached); 3872 3873 /* If sodirect, not anymore */ 3874 SOD_PTR_ENTER(tcp, sodp); 3875 if (sodp != NULL) { 3876 tcp->tcp_sodirect = NULL; 3877 mutex_exit(sodp->sod_lockp); 3878 } 3879 3880 q = tcp->tcp_rq; 3881 3882 /* Trash all inbound data */ 3883 flushq(q, FLUSHALL); 3884 3885 /* 3886 * If we are at least part way open and there is error 3887 * (err==0 implies no error) 3888 * notify our client by a T_DISCON_IND. 3889 */ 3890 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3891 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3892 !TCP_IS_SOCKET(tcp)) { 3893 /* 3894 * Send M_FLUSH according to TPI. Because sockets will 3895 * (and must) ignore FLUSHR we do that only for TPI 3896 * endpoints and sockets in STREAMS mode. 3897 */ 3898 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3899 } 3900 if (tcp->tcp_debug) { 3901 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3902 "tcp_clean_death: discon err %d", err); 3903 } 3904 mp = mi_tpi_discon_ind(NULL, err, 0); 3905 if (mp != NULL) { 3906 putnext(q, mp); 3907 } else { 3908 if (tcp->tcp_debug) { 3909 (void) strlog(TCP_MOD_ID, 0, 1, 3910 SL_ERROR|SL_TRACE, 3911 "tcp_clean_death, sending M_ERROR"); 3912 } 3913 (void) putnextctl1(q, M_ERROR, EPROTO); 3914 } 3915 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3916 /* SYN_SENT or SYN_RCVD */ 3917 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3918 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3919 /* ESTABLISHED or CLOSE_WAIT */ 3920 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3921 } 3922 } 3923 3924 tcp_reinit(tcp); 3925 return (-1); 3926 } 3927 3928 /* 3929 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3930 * to expire, stop the wait and finish the close. 3931 */ 3932 static void 3933 tcp_stop_lingering(tcp_t *tcp) 3934 { 3935 clock_t delta = 0; 3936 tcp_stack_t *tcps = tcp->tcp_tcps; 3937 3938 tcp->tcp_linger_tid = 0; 3939 if (tcp->tcp_state > TCPS_LISTEN) { 3940 tcp_acceptor_hash_remove(tcp); 3941 mutex_enter(&tcp->tcp_non_sq_lock); 3942 if (tcp->tcp_flow_stopped) { 3943 tcp_clrqfull(tcp); 3944 } 3945 mutex_exit(&tcp->tcp_non_sq_lock); 3946 3947 if (tcp->tcp_timer_tid != 0) { 3948 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3949 tcp->tcp_timer_tid = 0; 3950 } 3951 /* 3952 * Need to cancel those timers which will not be used when 3953 * TCP is detached. This has to be done before the tcp_wq 3954 * is set to the global queue. 3955 */ 3956 tcp_timers_stop(tcp); 3957 3958 3959 tcp->tcp_detached = B_TRUE; 3960 ASSERT(tcps->tcps_g_q != NULL); 3961 tcp->tcp_rq = tcps->tcps_g_q; 3962 tcp->tcp_wq = WR(tcps->tcps_g_q); 3963 3964 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3965 tcp_time_wait_append(tcp); 3966 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3967 goto finish; 3968 } 3969 3970 /* 3971 * If delta is zero the timer event wasn't executed and was 3972 * successfully canceled. In this case we need to restart it 3973 * with the minimal delta possible. 3974 */ 3975 if (delta >= 0) { 3976 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3977 delta ? delta : 1); 3978 } 3979 } else { 3980 tcp_closei_local(tcp); 3981 CONN_DEC_REF(tcp->tcp_connp); 3982 } 3983 finish: 3984 /* Signal closing thread that it can complete close */ 3985 mutex_enter(&tcp->tcp_closelock); 3986 tcp->tcp_detached = B_TRUE; 3987 ASSERT(tcps->tcps_g_q != NULL); 3988 tcp->tcp_rq = tcps->tcps_g_q; 3989 tcp->tcp_wq = WR(tcps->tcps_g_q); 3990 tcp->tcp_closed = 1; 3991 cv_signal(&tcp->tcp_closecv); 3992 mutex_exit(&tcp->tcp_closelock); 3993 } 3994 3995 /* 3996 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3997 * expires. 3998 */ 3999 static void 4000 tcp_close_linger_timeout(void *arg) 4001 { 4002 conn_t *connp = (conn_t *)arg; 4003 tcp_t *tcp = connp->conn_tcp; 4004 4005 tcp->tcp_client_errno = ETIMEDOUT; 4006 tcp_stop_lingering(tcp); 4007 } 4008 4009 static int 4010 tcp_close(queue_t *q, int flags) 4011 { 4012 conn_t *connp = Q_TO_CONN(q); 4013 tcp_t *tcp = connp->conn_tcp; 4014 mblk_t *mp = &tcp->tcp_closemp; 4015 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 4016 mblk_t *bp; 4017 4018 ASSERT(WR(q)->q_next == NULL); 4019 ASSERT(connp->conn_ref >= 2); 4020 4021 /* 4022 * We are being closed as /dev/tcp or /dev/tcp6. 4023 * 4024 * Mark the conn as closing. ill_pending_mp_add will not 4025 * add any mp to the pending mp list, after this conn has 4026 * started closing. Same for sq_pending_mp_add 4027 */ 4028 mutex_enter(&connp->conn_lock); 4029 connp->conn_state_flags |= CONN_CLOSING; 4030 if (connp->conn_oper_pending_ill != NULL) 4031 conn_ioctl_cleanup_reqd = B_TRUE; 4032 CONN_INC_REF_LOCKED(connp); 4033 mutex_exit(&connp->conn_lock); 4034 tcp->tcp_closeflags = (uint8_t)flags; 4035 ASSERT(connp->conn_ref >= 3); 4036 4037 /* 4038 * tcp_closemp_used is used below without any protection of a lock 4039 * as we don't expect any one else to use it concurrently at this 4040 * point otherwise it would be a major defect. 4041 */ 4042 4043 if (mp->b_prev == NULL) 4044 tcp->tcp_closemp_used = B_TRUE; 4045 else 4046 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: " 4047 "connp %p tcp %p\n", (void *)connp, (void *)tcp); 4048 4049 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 4050 4051 (*tcp_squeue_close_proc)(connp->conn_sqp, mp, 4052 tcp_close_output, connp, SQTAG_IP_TCP_CLOSE); 4053 4054 mutex_enter(&tcp->tcp_closelock); 4055 while (!tcp->tcp_closed) { 4056 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 4057 /* 4058 * The cv_wait_sig() was interrupted. We now do the 4059 * following: 4060 * 4061 * 1) If the endpoint was lingering, we allow this 4062 * to be interrupted by cancelling the linger timeout 4063 * and closing normally. 4064 * 4065 * 2) Revert to calling cv_wait() 4066 * 4067 * We revert to using cv_wait() to avoid an 4068 * infinite loop which can occur if the calling 4069 * thread is higher priority than the squeue worker 4070 * thread and is bound to the same cpu. 4071 */ 4072 if (tcp->tcp_linger && tcp->tcp_lingertime > 0) { 4073 mutex_exit(&tcp->tcp_closelock); 4074 /* Entering squeue, bump ref count. */ 4075 CONN_INC_REF(connp); 4076 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 4077 squeue_enter(connp->conn_sqp, bp, 4078 tcp_linger_interrupted, connp, 4079 SQTAG_IP_TCP_CLOSE); 4080 mutex_enter(&tcp->tcp_closelock); 4081 } 4082 break; 4083 } 4084 } 4085 while (!tcp->tcp_closed) 4086 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 4087 mutex_exit(&tcp->tcp_closelock); 4088 4089 /* 4090 * In the case of listener streams that have eagers in the q or q0 4091 * we wait for the eagers to drop their reference to us. tcp_rq and 4092 * tcp_wq of the eagers point to our queues. By waiting for the 4093 * refcnt to drop to 1, we are sure that the eagers have cleaned 4094 * up their queue pointers and also dropped their references to us. 4095 */ 4096 if (tcp->tcp_wait_for_eagers) { 4097 mutex_enter(&connp->conn_lock); 4098 while (connp->conn_ref != 1) { 4099 cv_wait(&connp->conn_cv, &connp->conn_lock); 4100 } 4101 mutex_exit(&connp->conn_lock); 4102 } 4103 /* 4104 * ioctl cleanup. The mp is queued in the 4105 * ill_pending_mp or in the sq_pending_mp. 4106 */ 4107 if (conn_ioctl_cleanup_reqd) 4108 conn_ioctl_cleanup(connp); 4109 4110 qprocsoff(q); 4111 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 4112 4113 tcp->tcp_cpid = -1; 4114 4115 /* 4116 * Drop IP's reference on the conn. This is the last reference 4117 * on the connp if the state was less than established. If the 4118 * connection has gone into timewait state, then we will have 4119 * one ref for the TCP and one more ref (total of two) for the 4120 * classifier connected hash list (a timewait connections stays 4121 * in connected hash till closed). 4122 * 4123 * We can't assert the references because there might be other 4124 * transient reference places because of some walkers or queued 4125 * packets in squeue for the timewait state. 4126 */ 4127 CONN_DEC_REF(connp); 4128 q->q_ptr = WR(q)->q_ptr = NULL; 4129 return (0); 4130 } 4131 4132 static int 4133 tcpclose_accept(queue_t *q) 4134 { 4135 vmem_t *minor_arena; 4136 dev_t conn_dev; 4137 4138 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 4139 4140 /* 4141 * We had opened an acceptor STREAM for sockfs which is 4142 * now being closed due to some error. 4143 */ 4144 qprocsoff(q); 4145 4146 minor_arena = (vmem_t *)WR(q)->q_ptr; 4147 conn_dev = (dev_t)RD(q)->q_ptr; 4148 ASSERT(minor_arena != NULL); 4149 ASSERT(conn_dev != 0); 4150 inet_minor_free(minor_arena, conn_dev); 4151 q->q_ptr = WR(q)->q_ptr = NULL; 4152 return (0); 4153 } 4154 4155 /* 4156 * Called by tcp_close() routine via squeue when lingering is 4157 * interrupted by a signal. 4158 */ 4159 4160 /* ARGSUSED */ 4161 static void 4162 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 4163 { 4164 conn_t *connp = (conn_t *)arg; 4165 tcp_t *tcp = connp->conn_tcp; 4166 4167 freeb(mp); 4168 if (tcp->tcp_linger_tid != 0 && 4169 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 4170 tcp_stop_lingering(tcp); 4171 tcp->tcp_client_errno = EINTR; 4172 } 4173 } 4174 4175 /* 4176 * Called by streams close routine via squeues when our client blows off her 4177 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 4178 * connection politely" When SO_LINGER is set (with a non-zero linger time and 4179 * it is not a nonblocking socket) then this routine sleeps until the FIN is 4180 * acked. 4181 * 4182 * NOTE: tcp_close potentially returns error when lingering. 4183 * However, the stream head currently does not pass these errors 4184 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 4185 * errors to the application (from tsleep()) and not errors 4186 * like ECONNRESET caused by receiving a reset packet. 4187 */ 4188 4189 /* ARGSUSED */ 4190 static void 4191 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 4192 { 4193 char *msg; 4194 conn_t *connp = (conn_t *)arg; 4195 tcp_t *tcp = connp->conn_tcp; 4196 clock_t delta = 0; 4197 tcp_stack_t *tcps = tcp->tcp_tcps; 4198 4199 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 4200 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 4201 4202 mutex_enter(&tcp->tcp_eager_lock); 4203 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 4204 /* Cleanup for listener */ 4205 tcp_eager_cleanup(tcp, 0); 4206 tcp->tcp_wait_for_eagers = 1; 4207 } 4208 mutex_exit(&tcp->tcp_eager_lock); 4209 4210 connp->conn_mdt_ok = B_FALSE; 4211 tcp->tcp_mdt = B_FALSE; 4212 4213 connp->conn_lso_ok = B_FALSE; 4214 tcp->tcp_lso = B_FALSE; 4215 4216 msg = NULL; 4217 switch (tcp->tcp_state) { 4218 case TCPS_CLOSED: 4219 case TCPS_IDLE: 4220 case TCPS_BOUND: 4221 case TCPS_LISTEN: 4222 break; 4223 case TCPS_SYN_SENT: 4224 msg = "tcp_close, during connect"; 4225 break; 4226 case TCPS_SYN_RCVD: 4227 /* 4228 * Close during the connect 3-way handshake 4229 * but here there may or may not be pending data 4230 * already on queue. Process almost same as in 4231 * the ESTABLISHED state. 4232 */ 4233 /* FALLTHRU */ 4234 default: 4235 if (tcp->tcp_sodirect != NULL) { 4236 /* Ok, no more sodirect */ 4237 tcp->tcp_sodirect = NULL; 4238 } 4239 4240 if (tcp->tcp_fused) 4241 tcp_unfuse(tcp); 4242 4243 /* 4244 * If SO_LINGER has set a zero linger time, abort the 4245 * connection with a reset. 4246 */ 4247 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 4248 msg = "tcp_close, zero lingertime"; 4249 break; 4250 } 4251 4252 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 4253 /* 4254 * Abort connection if there is unread data queued. 4255 */ 4256 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 4257 msg = "tcp_close, unread data"; 4258 break; 4259 } 4260 /* 4261 * tcp_hard_bound is now cleared thus all packets go through 4262 * tcp_lookup. This fact is used by tcp_detach below. 4263 * 4264 * We have done a qwait() above which could have possibly 4265 * drained more messages in turn causing transition to a 4266 * different state. Check whether we have to do the rest 4267 * of the processing or not. 4268 */ 4269 if (tcp->tcp_state <= TCPS_LISTEN) 4270 break; 4271 4272 /* 4273 * Transmit the FIN before detaching the tcp_t. 4274 * After tcp_detach returns this queue/perimeter 4275 * no longer owns the tcp_t thus others can modify it. 4276 */ 4277 (void) tcp_xmit_end(tcp); 4278 4279 /* 4280 * If lingering on close then wait until the fin is acked, 4281 * the SO_LINGER time passes, or a reset is sent/received. 4282 */ 4283 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4284 !(tcp->tcp_fin_acked) && 4285 tcp->tcp_state >= TCPS_ESTABLISHED) { 4286 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4287 tcp->tcp_client_errno = EWOULDBLOCK; 4288 } else if (tcp->tcp_client_errno == 0) { 4289 4290 ASSERT(tcp->tcp_linger_tid == 0); 4291 4292 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4293 tcp_close_linger_timeout, 4294 tcp->tcp_lingertime * hz); 4295 4296 /* tcp_close_linger_timeout will finish close */ 4297 if (tcp->tcp_linger_tid == 0) 4298 tcp->tcp_client_errno = ENOSR; 4299 else 4300 return; 4301 } 4302 4303 /* 4304 * Check if we need to detach or just close 4305 * the instance. 4306 */ 4307 if (tcp->tcp_state <= TCPS_LISTEN) 4308 break; 4309 } 4310 4311 /* 4312 * Make sure that no other thread will access the tcp_rq of 4313 * this instance (through lookups etc.) as tcp_rq will go 4314 * away shortly. 4315 */ 4316 tcp_acceptor_hash_remove(tcp); 4317 4318 mutex_enter(&tcp->tcp_non_sq_lock); 4319 if (tcp->tcp_flow_stopped) { 4320 tcp_clrqfull(tcp); 4321 } 4322 mutex_exit(&tcp->tcp_non_sq_lock); 4323 4324 if (tcp->tcp_timer_tid != 0) { 4325 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4326 tcp->tcp_timer_tid = 0; 4327 } 4328 /* 4329 * Need to cancel those timers which will not be used when 4330 * TCP is detached. This has to be done before the tcp_wq 4331 * is set to the global queue. 4332 */ 4333 tcp_timers_stop(tcp); 4334 4335 tcp->tcp_detached = B_TRUE; 4336 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4337 tcp_time_wait_append(tcp); 4338 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4339 ASSERT(connp->conn_ref >= 3); 4340 goto finish; 4341 } 4342 4343 /* 4344 * If delta is zero the timer event wasn't executed and was 4345 * successfully canceled. In this case we need to restart it 4346 * with the minimal delta possible. 4347 */ 4348 if (delta >= 0) 4349 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4350 delta ? delta : 1); 4351 4352 ASSERT(connp->conn_ref >= 3); 4353 goto finish; 4354 } 4355 4356 /* Detach did not complete. Still need to remove q from stream. */ 4357 if (msg) { 4358 if (tcp->tcp_state == TCPS_ESTABLISHED || 4359 tcp->tcp_state == TCPS_CLOSE_WAIT) 4360 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4361 if (tcp->tcp_state == TCPS_SYN_SENT || 4362 tcp->tcp_state == TCPS_SYN_RCVD) 4363 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4364 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4365 } 4366 4367 tcp_closei_local(tcp); 4368 CONN_DEC_REF(connp); 4369 ASSERT(connp->conn_ref >= 2); 4370 4371 finish: 4372 /* 4373 * Although packets are always processed on the correct 4374 * tcp's perimeter and access is serialized via squeue's, 4375 * IP still needs a queue when sending packets in time_wait 4376 * state so use WR(tcps_g_q) till ip_output() can be 4377 * changed to deal with just connp. For read side, we 4378 * could have set tcp_rq to NULL but there are some cases 4379 * in tcp_rput_data() from early days of this code which 4380 * do a putnext without checking if tcp is closed. Those 4381 * need to be identified before both tcp_rq and tcp_wq 4382 * can be set to NULL and tcps_g_q can disappear forever. 4383 */ 4384 mutex_enter(&tcp->tcp_closelock); 4385 /* 4386 * Don't change the queues in the case of a listener that has 4387 * eagers in its q or q0. It could surprise the eagers. 4388 * Instead wait for the eagers outside the squeue. 4389 */ 4390 if (!tcp->tcp_wait_for_eagers) { 4391 tcp->tcp_detached = B_TRUE; 4392 /* 4393 * When default queue is closing we set tcps_g_q to NULL 4394 * after the close is done. 4395 */ 4396 ASSERT(tcps->tcps_g_q != NULL); 4397 tcp->tcp_rq = tcps->tcps_g_q; 4398 tcp->tcp_wq = WR(tcps->tcps_g_q); 4399 } 4400 4401 /* Signal tcp_close() to finish closing. */ 4402 tcp->tcp_closed = 1; 4403 cv_signal(&tcp->tcp_closecv); 4404 mutex_exit(&tcp->tcp_closelock); 4405 } 4406 4407 4408 /* 4409 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4410 * Some stream heads get upset if they see these later on as anything but NULL. 4411 */ 4412 static void 4413 tcp_close_mpp(mblk_t **mpp) 4414 { 4415 mblk_t *mp; 4416 4417 if ((mp = *mpp) != NULL) { 4418 do { 4419 mp->b_next = NULL; 4420 mp->b_prev = NULL; 4421 } while ((mp = mp->b_cont) != NULL); 4422 4423 mp = *mpp; 4424 *mpp = NULL; 4425 freemsg(mp); 4426 } 4427 } 4428 4429 /* Do detached close. */ 4430 static void 4431 tcp_close_detached(tcp_t *tcp) 4432 { 4433 if (tcp->tcp_fused) 4434 tcp_unfuse(tcp); 4435 4436 /* 4437 * Clustering code serializes TCP disconnect callbacks and 4438 * cluster tcp list walks by blocking a TCP disconnect callback 4439 * if a cluster tcp list walk is in progress. This ensures 4440 * accurate accounting of TCPs in the cluster code even though 4441 * the TCP list walk itself is not atomic. 4442 */ 4443 tcp_closei_local(tcp); 4444 CONN_DEC_REF(tcp->tcp_connp); 4445 } 4446 4447 /* 4448 * Stop all TCP timers, and free the timer mblks if requested. 4449 */ 4450 void 4451 tcp_timers_stop(tcp_t *tcp) 4452 { 4453 if (tcp->tcp_timer_tid != 0) { 4454 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4455 tcp->tcp_timer_tid = 0; 4456 } 4457 if (tcp->tcp_ka_tid != 0) { 4458 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4459 tcp->tcp_ka_tid = 0; 4460 } 4461 if (tcp->tcp_ack_tid != 0) { 4462 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4463 tcp->tcp_ack_tid = 0; 4464 } 4465 if (tcp->tcp_push_tid != 0) { 4466 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4467 tcp->tcp_push_tid = 0; 4468 } 4469 } 4470 4471 /* 4472 * The tcp_t is going away. Remove it from all lists and set it 4473 * to TCPS_CLOSED. The freeing up of memory is deferred until 4474 * tcp_inactive. This is needed since a thread in tcp_rput might have 4475 * done a CONN_INC_REF on this structure before it was removed from the 4476 * hashes. 4477 */ 4478 static void 4479 tcp_closei_local(tcp_t *tcp) 4480 { 4481 ire_t *ire; 4482 conn_t *connp = tcp->tcp_connp; 4483 tcp_stack_t *tcps = tcp->tcp_tcps; 4484 4485 if (!TCP_IS_SOCKET(tcp)) 4486 tcp_acceptor_hash_remove(tcp); 4487 4488 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4489 tcp->tcp_ibsegs = 0; 4490 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4491 tcp->tcp_obsegs = 0; 4492 4493 /* 4494 * If we are an eager connection hanging off a listener that 4495 * hasn't formally accepted the connection yet, get off his 4496 * list and blow off any data that we have accumulated. 4497 */ 4498 if (tcp->tcp_listener != NULL) { 4499 tcp_t *listener = tcp->tcp_listener; 4500 mutex_enter(&listener->tcp_eager_lock); 4501 /* 4502 * tcp_tconnind_started == B_TRUE means that the 4503 * conn_ind has already gone to listener. At 4504 * this point, eager will be closed but we 4505 * leave it in listeners eager list so that 4506 * if listener decides to close without doing 4507 * accept, we can clean this up. In tcp_wput_accept 4508 * we take care of the case of accept on closed 4509 * eager. 4510 */ 4511 if (!tcp->tcp_tconnind_started) { 4512 tcp_eager_unlink(tcp); 4513 mutex_exit(&listener->tcp_eager_lock); 4514 /* 4515 * We don't want to have any pointers to the 4516 * listener queue, after we have released our 4517 * reference on the listener 4518 */ 4519 ASSERT(tcps->tcps_g_q != NULL); 4520 tcp->tcp_rq = tcps->tcps_g_q; 4521 tcp->tcp_wq = WR(tcps->tcps_g_q); 4522 CONN_DEC_REF(listener->tcp_connp); 4523 } else { 4524 mutex_exit(&listener->tcp_eager_lock); 4525 } 4526 } 4527 4528 /* Stop all the timers */ 4529 tcp_timers_stop(tcp); 4530 4531 if (tcp->tcp_state == TCPS_LISTEN) { 4532 if (tcp->tcp_ip_addr_cache) { 4533 kmem_free((void *)tcp->tcp_ip_addr_cache, 4534 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4535 tcp->tcp_ip_addr_cache = NULL; 4536 } 4537 } 4538 mutex_enter(&tcp->tcp_non_sq_lock); 4539 if (tcp->tcp_flow_stopped) 4540 tcp_clrqfull(tcp); 4541 mutex_exit(&tcp->tcp_non_sq_lock); 4542 4543 tcp_bind_hash_remove(tcp); 4544 /* 4545 * If the tcp_time_wait_collector (which runs outside the squeue) 4546 * is trying to remove this tcp from the time wait list, we will 4547 * block in tcp_time_wait_remove while trying to acquire the 4548 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4549 * requires the ipcl_hash_remove to be ordered after the 4550 * tcp_time_wait_remove for the refcnt checks to work correctly. 4551 */ 4552 if (tcp->tcp_state == TCPS_TIME_WAIT) 4553 (void) tcp_time_wait_remove(tcp, NULL); 4554 CL_INET_DISCONNECT(tcp); 4555 ipcl_hash_remove(connp); 4556 4557 /* 4558 * Delete the cached ire in conn_ire_cache and also mark 4559 * the conn as CONDEMNED 4560 */ 4561 mutex_enter(&connp->conn_lock); 4562 connp->conn_state_flags |= CONN_CONDEMNED; 4563 ire = connp->conn_ire_cache; 4564 connp->conn_ire_cache = NULL; 4565 mutex_exit(&connp->conn_lock); 4566 if (ire != NULL) 4567 IRE_REFRELE_NOTR(ire); 4568 4569 /* Need to cleanup any pending ioctls */ 4570 ASSERT(tcp->tcp_time_wait_next == NULL); 4571 ASSERT(tcp->tcp_time_wait_prev == NULL); 4572 ASSERT(tcp->tcp_time_wait_expire == 0); 4573 tcp->tcp_state = TCPS_CLOSED; 4574 4575 /* Release any SSL context */ 4576 if (tcp->tcp_kssl_ent != NULL) { 4577 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4578 tcp->tcp_kssl_ent = NULL; 4579 } 4580 if (tcp->tcp_kssl_ctx != NULL) { 4581 kssl_release_ctx(tcp->tcp_kssl_ctx); 4582 tcp->tcp_kssl_ctx = NULL; 4583 } 4584 tcp->tcp_kssl_pending = B_FALSE; 4585 4586 tcp_ipsec_cleanup(tcp); 4587 } 4588 4589 /* 4590 * tcp is dying (called from ipcl_conn_destroy and error cases). 4591 * Free the tcp_t in either case. 4592 */ 4593 void 4594 tcp_free(tcp_t *tcp) 4595 { 4596 mblk_t *mp; 4597 ip6_pkt_t *ipp; 4598 4599 ASSERT(tcp != NULL); 4600 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4601 4602 tcp->tcp_rq = NULL; 4603 tcp->tcp_wq = NULL; 4604 4605 tcp_close_mpp(&tcp->tcp_xmit_head); 4606 tcp_close_mpp(&tcp->tcp_reass_head); 4607 if (tcp->tcp_rcv_list != NULL) { 4608 /* Free b_next chain */ 4609 tcp_close_mpp(&tcp->tcp_rcv_list); 4610 } 4611 if ((mp = tcp->tcp_urp_mp) != NULL) { 4612 freemsg(mp); 4613 } 4614 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4615 freemsg(mp); 4616 } 4617 4618 if (tcp->tcp_fused_sigurg_mp != NULL) { 4619 freeb(tcp->tcp_fused_sigurg_mp); 4620 tcp->tcp_fused_sigurg_mp = NULL; 4621 } 4622 4623 if (tcp->tcp_ordrel_mp != NULL) { 4624 freeb(tcp->tcp_ordrel_mp); 4625 tcp->tcp_ordrel_mp = NULL; 4626 } 4627 4628 if (tcp->tcp_sack_info != NULL) { 4629 if (tcp->tcp_notsack_list != NULL) { 4630 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4631 } 4632 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4633 } 4634 4635 if (tcp->tcp_hopopts != NULL) { 4636 mi_free(tcp->tcp_hopopts); 4637 tcp->tcp_hopopts = NULL; 4638 tcp->tcp_hopoptslen = 0; 4639 } 4640 ASSERT(tcp->tcp_hopoptslen == 0); 4641 if (tcp->tcp_dstopts != NULL) { 4642 mi_free(tcp->tcp_dstopts); 4643 tcp->tcp_dstopts = NULL; 4644 tcp->tcp_dstoptslen = 0; 4645 } 4646 ASSERT(tcp->tcp_dstoptslen == 0); 4647 if (tcp->tcp_rtdstopts != NULL) { 4648 mi_free(tcp->tcp_rtdstopts); 4649 tcp->tcp_rtdstopts = NULL; 4650 tcp->tcp_rtdstoptslen = 0; 4651 } 4652 ASSERT(tcp->tcp_rtdstoptslen == 0); 4653 if (tcp->tcp_rthdr != NULL) { 4654 mi_free(tcp->tcp_rthdr); 4655 tcp->tcp_rthdr = NULL; 4656 tcp->tcp_rthdrlen = 0; 4657 } 4658 ASSERT(tcp->tcp_rthdrlen == 0); 4659 4660 ipp = &tcp->tcp_sticky_ipp; 4661 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4662 IPPF_RTHDR)) 4663 ip6_pkt_free(ipp); 4664 4665 /* 4666 * Free memory associated with the tcp/ip header template. 4667 */ 4668 4669 if (tcp->tcp_iphc != NULL) 4670 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4671 4672 /* 4673 * Following is really a blowing away a union. 4674 * It happens to have exactly two members of identical size 4675 * the following code is enough. 4676 */ 4677 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4678 } 4679 4680 4681 /* 4682 * Put a connection confirmation message upstream built from the 4683 * address information within 'iph' and 'tcph'. Report our success or failure. 4684 */ 4685 static boolean_t 4686 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4687 mblk_t **defermp) 4688 { 4689 sin_t sin; 4690 sin6_t sin6; 4691 mblk_t *mp; 4692 char *optp = NULL; 4693 int optlen = 0; 4694 cred_t *cr; 4695 4696 if (defermp != NULL) 4697 *defermp = NULL; 4698 4699 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4700 /* 4701 * Return in T_CONN_CON results of option negotiation through 4702 * the T_CONN_REQ. Note: If there is an real end-to-end option 4703 * negotiation, then what is received from remote end needs 4704 * to be taken into account but there is no such thing (yet?) 4705 * in our TCP/IP. 4706 * Note: We do not use mi_offset_param() here as 4707 * tcp_opts_conn_req contents do not directly come from 4708 * an application and are either generated in kernel or 4709 * from user input that was already verified. 4710 */ 4711 mp = tcp->tcp_conn.tcp_opts_conn_req; 4712 optp = (char *)(mp->b_rptr + 4713 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4714 optlen = (int) 4715 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4716 } 4717 4718 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4719 ipha_t *ipha = (ipha_t *)iphdr; 4720 4721 /* packet is IPv4 */ 4722 if (tcp->tcp_family == AF_INET) { 4723 sin = sin_null; 4724 sin.sin_addr.s_addr = ipha->ipha_src; 4725 sin.sin_port = *(uint16_t *)tcph->th_lport; 4726 sin.sin_family = AF_INET; 4727 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4728 (int)sizeof (sin_t), optp, optlen); 4729 } else { 4730 sin6 = sin6_null; 4731 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4732 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4733 sin6.sin6_family = AF_INET6; 4734 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4735 (int)sizeof (sin6_t), optp, optlen); 4736 4737 } 4738 } else { 4739 ip6_t *ip6h = (ip6_t *)iphdr; 4740 4741 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4742 ASSERT(tcp->tcp_family == AF_INET6); 4743 sin6 = sin6_null; 4744 sin6.sin6_addr = ip6h->ip6_src; 4745 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4746 sin6.sin6_family = AF_INET6; 4747 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4748 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4749 (int)sizeof (sin6_t), optp, optlen); 4750 } 4751 4752 if (!mp) 4753 return (B_FALSE); 4754 4755 if ((cr = DB_CRED(idmp)) != NULL) { 4756 mblk_setcred(mp, cr); 4757 DB_CPID(mp) = DB_CPID(idmp); 4758 } 4759 4760 if (defermp == NULL) 4761 putnext(tcp->tcp_rq, mp); 4762 else 4763 *defermp = mp; 4764 4765 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4766 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4767 return (B_TRUE); 4768 } 4769 4770 /* 4771 * Defense for the SYN attack - 4772 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4773 * one from the list of droppable eagers. This list is a subset of q0. 4774 * see comments before the definition of MAKE_DROPPABLE(). 4775 * 2. Don't drop a SYN request before its first timeout. This gives every 4776 * request at least til the first timeout to complete its 3-way handshake. 4777 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4778 * requests currently on the queue that has timed out. This will be used 4779 * as an indicator of whether an attack is under way, so that appropriate 4780 * actions can be taken. (It's incremented in tcp_timer() and decremented 4781 * either when eager goes into ESTABLISHED, or gets freed up.) 4782 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4783 * # of timeout drops back to <= q0len/32 => SYN alert off 4784 */ 4785 static boolean_t 4786 tcp_drop_q0(tcp_t *tcp) 4787 { 4788 tcp_t *eager; 4789 mblk_t *mp; 4790 tcp_stack_t *tcps = tcp->tcp_tcps; 4791 4792 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4793 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4794 4795 /* Pick oldest eager from the list of droppable eagers */ 4796 eager = tcp->tcp_eager_prev_drop_q0; 4797 4798 /* If list is empty. return B_FALSE */ 4799 if (eager == tcp) { 4800 return (B_FALSE); 4801 } 4802 4803 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4804 if ((mp = allocb(0, BPRI_HI)) == NULL) 4805 return (B_FALSE); 4806 4807 /* 4808 * Take this eager out from the list of droppable eagers since we are 4809 * going to drop it. 4810 */ 4811 MAKE_UNDROPPABLE(eager); 4812 4813 if (tcp->tcp_debug) { 4814 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4815 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4816 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4817 tcp->tcp_conn_req_cnt_q0, 4818 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4819 } 4820 4821 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4822 4823 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4824 CONN_INC_REF(eager->tcp_connp); 4825 4826 /* Mark the IRE created for this SYN request temporary */ 4827 tcp_ip_ire_mark_advice(eager); 4828 squeue_fill(eager->tcp_connp->conn_sqp, mp, 4829 tcp_clean_death_wrapper, eager->tcp_connp, SQTAG_TCP_DROP_Q0); 4830 4831 return (B_TRUE); 4832 } 4833 4834 int 4835 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4836 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4837 { 4838 tcp_t *ltcp = lconnp->conn_tcp; 4839 tcp_t *tcp = connp->conn_tcp; 4840 mblk_t *tpi_mp; 4841 ipha_t *ipha; 4842 ip6_t *ip6h; 4843 sin6_t sin6; 4844 in6_addr_t v6dst; 4845 int err; 4846 int ifindex = 0; 4847 cred_t *cr; 4848 tcp_stack_t *tcps = tcp->tcp_tcps; 4849 4850 if (ipvers == IPV4_VERSION) { 4851 ipha = (ipha_t *)mp->b_rptr; 4852 4853 connp->conn_send = ip_output; 4854 connp->conn_recv = tcp_input; 4855 4856 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4857 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4858 4859 sin6 = sin6_null; 4860 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4861 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4862 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4863 sin6.sin6_family = AF_INET6; 4864 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4865 lconnp->conn_zoneid, tcps->tcps_netstack); 4866 if (tcp->tcp_recvdstaddr) { 4867 sin6_t sin6d; 4868 4869 sin6d = sin6_null; 4870 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4871 &sin6d.sin6_addr); 4872 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4873 sin6d.sin6_family = AF_INET; 4874 tpi_mp = mi_tpi_extconn_ind(NULL, 4875 (char *)&sin6d, sizeof (sin6_t), 4876 (char *)&tcp, 4877 (t_scalar_t)sizeof (intptr_t), 4878 (char *)&sin6d, sizeof (sin6_t), 4879 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4880 } else { 4881 tpi_mp = mi_tpi_conn_ind(NULL, 4882 (char *)&sin6, sizeof (sin6_t), 4883 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4884 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4885 } 4886 } else { 4887 ip6h = (ip6_t *)mp->b_rptr; 4888 4889 connp->conn_send = ip_output_v6; 4890 connp->conn_recv = tcp_input; 4891 4892 connp->conn_srcv6 = ip6h->ip6_dst; 4893 connp->conn_remv6 = ip6h->ip6_src; 4894 4895 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4896 ifindex = (int)DB_CKSUMSTUFF(mp); 4897 DB_CKSUMSTUFF(mp) = 0; 4898 4899 sin6 = sin6_null; 4900 sin6.sin6_addr = ip6h->ip6_src; 4901 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4902 sin6.sin6_family = AF_INET6; 4903 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4904 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4905 lconnp->conn_zoneid, tcps->tcps_netstack); 4906 4907 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4908 /* Pass up the scope_id of remote addr */ 4909 sin6.sin6_scope_id = ifindex; 4910 } else { 4911 sin6.sin6_scope_id = 0; 4912 } 4913 if (tcp->tcp_recvdstaddr) { 4914 sin6_t sin6d; 4915 4916 sin6d = sin6_null; 4917 sin6.sin6_addr = ip6h->ip6_dst; 4918 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4919 sin6d.sin6_family = AF_INET; 4920 tpi_mp = mi_tpi_extconn_ind(NULL, 4921 (char *)&sin6d, sizeof (sin6_t), 4922 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4923 (char *)&sin6d, sizeof (sin6_t), 4924 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4925 } else { 4926 tpi_mp = mi_tpi_conn_ind(NULL, 4927 (char *)&sin6, sizeof (sin6_t), 4928 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4929 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4930 } 4931 } 4932 4933 if (tpi_mp == NULL) 4934 return (ENOMEM); 4935 4936 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4937 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4938 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4939 connp->conn_fully_bound = B_FALSE; 4940 4941 /* Inherit information from the "parent" */ 4942 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4943 tcp->tcp_family = ltcp->tcp_family; 4944 tcp->tcp_wq = ltcp->tcp_wq; 4945 tcp->tcp_rq = ltcp->tcp_rq; 4946 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4947 tcp->tcp_detached = B_TRUE; 4948 if ((err = tcp_init_values(tcp)) != 0) { 4949 freemsg(tpi_mp); 4950 return (err); 4951 } 4952 4953 if (ipvers == IPV4_VERSION) { 4954 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4955 freemsg(tpi_mp); 4956 return (err); 4957 } 4958 ASSERT(tcp->tcp_ipha != NULL); 4959 } else { 4960 /* ifindex must be already set */ 4961 ASSERT(ifindex != 0); 4962 4963 if (ltcp->tcp_bound_if != 0) { 4964 /* 4965 * Set newtcp's bound_if equal to 4966 * listener's value. If ifindex is 4967 * not the same as ltcp->tcp_bound_if, 4968 * it must be a packet for the ipmp group 4969 * of interfaces 4970 */ 4971 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4972 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4973 tcp->tcp_bound_if = ifindex; 4974 } 4975 4976 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4977 tcp->tcp_recvifindex = 0; 4978 tcp->tcp_recvhops = 0xffffffffU; 4979 ASSERT(tcp->tcp_ip6h != NULL); 4980 } 4981 4982 tcp->tcp_lport = ltcp->tcp_lport; 4983 4984 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4985 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4986 /* 4987 * Listener had options of some sort; eager inherits. 4988 * Free up the eager template and allocate one 4989 * of the right size. 4990 */ 4991 if (tcp->tcp_hdr_grown) { 4992 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4993 } else { 4994 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4995 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4996 } 4997 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4998 KM_NOSLEEP); 4999 if (tcp->tcp_iphc == NULL) { 5000 tcp->tcp_iphc_len = 0; 5001 freemsg(tpi_mp); 5002 return (ENOMEM); 5003 } 5004 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 5005 tcp->tcp_hdr_grown = B_TRUE; 5006 } 5007 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5008 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5009 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5010 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 5011 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 5012 5013 /* 5014 * Copy the IP+TCP header template from listener to eager 5015 */ 5016 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5017 if (tcp->tcp_ipversion == IPV6_VERSION) { 5018 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 5019 IPPROTO_RAW) { 5020 tcp->tcp_ip6h = 5021 (ip6_t *)(tcp->tcp_iphc + 5022 sizeof (ip6i_t)); 5023 } else { 5024 tcp->tcp_ip6h = 5025 (ip6_t *)(tcp->tcp_iphc); 5026 } 5027 tcp->tcp_ipha = NULL; 5028 } else { 5029 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5030 tcp->tcp_ip6h = NULL; 5031 } 5032 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5033 tcp->tcp_ip_hdr_len); 5034 } else { 5035 /* 5036 * only valid case when ipversion of listener and 5037 * eager differ is when listener is IPv6 and 5038 * eager is IPv4. 5039 * Eager header template has been initialized to the 5040 * maximum v4 header sizes, which includes space for 5041 * TCP and IP options. 5042 */ 5043 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 5044 (tcp->tcp_ipversion == IPV4_VERSION)); 5045 ASSERT(tcp->tcp_iphc_len >= 5046 TCP_MAX_COMBINED_HEADER_LENGTH); 5047 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5048 /* copy IP header fields individually */ 5049 tcp->tcp_ipha->ipha_ttl = 5050 ltcp->tcp_ip6h->ip6_hops; 5051 bcopy(ltcp->tcp_tcph->th_lport, 5052 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 5053 } 5054 5055 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5056 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 5057 sizeof (in_port_t)); 5058 5059 if (ltcp->tcp_lport == 0) { 5060 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 5061 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 5062 sizeof (in_port_t)); 5063 } 5064 5065 if (tcp->tcp_ipversion == IPV4_VERSION) { 5066 ASSERT(ipha != NULL); 5067 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5068 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5069 5070 /* Source routing option copyover (reverse it) */ 5071 if (tcps->tcps_rev_src_routes) 5072 tcp_opt_reverse(tcp, ipha); 5073 } else { 5074 ASSERT(ip6h != NULL); 5075 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 5076 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 5077 } 5078 5079 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5080 ASSERT(!tcp->tcp_tconnind_started); 5081 /* 5082 * If the SYN contains a credential, it's a loopback packet; attach 5083 * the credential to the TPI message. 5084 */ 5085 if ((cr = DB_CRED(idmp)) != NULL) { 5086 mblk_setcred(tpi_mp, cr); 5087 DB_CPID(tpi_mp) = DB_CPID(idmp); 5088 } 5089 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5090 5091 /* Inherit the listener's SSL protection state */ 5092 5093 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5094 kssl_hold_ent(tcp->tcp_kssl_ent); 5095 tcp->tcp_kssl_pending = B_TRUE; 5096 } 5097 5098 return (0); 5099 } 5100 5101 5102 int 5103 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 5104 tcph_t *tcph, mblk_t *idmp) 5105 { 5106 tcp_t *ltcp = lconnp->conn_tcp; 5107 tcp_t *tcp = connp->conn_tcp; 5108 sin_t sin; 5109 mblk_t *tpi_mp = NULL; 5110 int err; 5111 cred_t *cr; 5112 tcp_stack_t *tcps = tcp->tcp_tcps; 5113 5114 sin = sin_null; 5115 sin.sin_addr.s_addr = ipha->ipha_src; 5116 sin.sin_port = *(uint16_t *)tcph->th_lport; 5117 sin.sin_family = AF_INET; 5118 if (ltcp->tcp_recvdstaddr) { 5119 sin_t sind; 5120 5121 sind = sin_null; 5122 sind.sin_addr.s_addr = ipha->ipha_dst; 5123 sind.sin_port = *(uint16_t *)tcph->th_fport; 5124 sind.sin_family = AF_INET; 5125 tpi_mp = mi_tpi_extconn_ind(NULL, 5126 (char *)&sind, sizeof (sin_t), (char *)&tcp, 5127 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 5128 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5129 } else { 5130 tpi_mp = mi_tpi_conn_ind(NULL, 5131 (char *)&sin, sizeof (sin_t), 5132 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 5133 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5134 } 5135 5136 if (tpi_mp == NULL) { 5137 return (ENOMEM); 5138 } 5139 5140 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 5141 connp->conn_send = ip_output; 5142 connp->conn_recv = tcp_input; 5143 connp->conn_fully_bound = B_FALSE; 5144 5145 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 5146 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 5147 connp->conn_fport = *(uint16_t *)tcph->th_lport; 5148 connp->conn_lport = *(uint16_t *)tcph->th_fport; 5149 5150 /* Inherit information from the "parent" */ 5151 tcp->tcp_ipversion = ltcp->tcp_ipversion; 5152 tcp->tcp_family = ltcp->tcp_family; 5153 tcp->tcp_wq = ltcp->tcp_wq; 5154 tcp->tcp_rq = ltcp->tcp_rq; 5155 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 5156 tcp->tcp_detached = B_TRUE; 5157 if ((err = tcp_init_values(tcp)) != 0) { 5158 freemsg(tpi_mp); 5159 return (err); 5160 } 5161 5162 /* 5163 * Let's make sure that eager tcp template has enough space to 5164 * copy IPv4 listener's tcp template. Since the conn_t structure is 5165 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 5166 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 5167 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 5168 * extension headers or with ip6i_t struct). Note that bcopy() below 5169 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 5170 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 5171 */ 5172 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 5173 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 5174 5175 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5176 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5177 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5178 tcp->tcp_ttl = ltcp->tcp_ttl; 5179 tcp->tcp_tos = ltcp->tcp_tos; 5180 5181 /* Copy the IP+TCP header template from listener to eager */ 5182 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5183 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5184 tcp->tcp_ip6h = NULL; 5185 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5186 tcp->tcp_ip_hdr_len); 5187 5188 /* Initialize the IP addresses and Ports */ 5189 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5190 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5191 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5192 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 5193 5194 /* Source routing option copyover (reverse it) */ 5195 if (tcps->tcps_rev_src_routes) 5196 tcp_opt_reverse(tcp, ipha); 5197 5198 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5199 ASSERT(!tcp->tcp_tconnind_started); 5200 5201 /* 5202 * If the SYN contains a credential, it's a loopback packet; attach 5203 * the credential to the TPI message. 5204 */ 5205 if ((cr = DB_CRED(idmp)) != NULL) { 5206 mblk_setcred(tpi_mp, cr); 5207 DB_CPID(tpi_mp) = DB_CPID(idmp); 5208 } 5209 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5210 5211 /* Inherit the listener's SSL protection state */ 5212 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5213 kssl_hold_ent(tcp->tcp_kssl_ent); 5214 tcp->tcp_kssl_pending = B_TRUE; 5215 } 5216 5217 return (0); 5218 } 5219 5220 /* 5221 * sets up conn for ipsec. 5222 * if the first mblk is M_CTL it is consumed and mpp is updated. 5223 * in case of error mpp is freed. 5224 */ 5225 conn_t * 5226 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 5227 { 5228 conn_t *connp = tcp->tcp_connp; 5229 conn_t *econnp; 5230 squeue_t *new_sqp; 5231 mblk_t *first_mp = *mpp; 5232 mblk_t *mp = *mpp; 5233 boolean_t mctl_present = B_FALSE; 5234 uint_t ipvers; 5235 5236 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 5237 if (econnp == NULL) { 5238 freemsg(first_mp); 5239 return (NULL); 5240 } 5241 if (DB_TYPE(mp) == M_CTL) { 5242 if (mp->b_cont == NULL || 5243 mp->b_cont->b_datap->db_type != M_DATA) { 5244 freemsg(first_mp); 5245 return (NULL); 5246 } 5247 mp = mp->b_cont; 5248 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 5249 freemsg(first_mp); 5250 return (NULL); 5251 } 5252 5253 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5254 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5255 mctl_present = B_TRUE; 5256 } else { 5257 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 5258 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5259 } 5260 5261 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5262 DB_CKSUMSTART(mp) = 0; 5263 5264 ASSERT(OK_32PTR(mp->b_rptr)); 5265 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5266 if (ipvers == IPV4_VERSION) { 5267 uint16_t *up; 5268 uint32_t ports; 5269 ipha_t *ipha; 5270 5271 ipha = (ipha_t *)mp->b_rptr; 5272 up = (uint16_t *)((uchar_t *)ipha + 5273 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5274 ports = *(uint32_t *)up; 5275 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5276 ipha->ipha_dst, ipha->ipha_src, ports); 5277 } else { 5278 uint16_t *up; 5279 uint32_t ports; 5280 uint16_t ip_hdr_len; 5281 uint8_t *nexthdrp; 5282 ip6_t *ip6h; 5283 tcph_t *tcph; 5284 5285 ip6h = (ip6_t *)mp->b_rptr; 5286 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5287 ip_hdr_len = IPV6_HDR_LEN; 5288 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5289 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5290 CONN_DEC_REF(econnp); 5291 freemsg(first_mp); 5292 return (NULL); 5293 } 5294 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5295 up = (uint16_t *)tcph->th_lport; 5296 ports = *(uint32_t *)up; 5297 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5298 ip6h->ip6_dst, ip6h->ip6_src, ports); 5299 } 5300 5301 /* 5302 * The caller already ensured that there is a sqp present. 5303 */ 5304 econnp->conn_sqp = new_sqp; 5305 5306 if (connp->conn_policy != NULL) { 5307 ipsec_in_t *ii; 5308 ii = (ipsec_in_t *)(first_mp->b_rptr); 5309 ASSERT(ii->ipsec_in_policy == NULL); 5310 IPPH_REFHOLD(connp->conn_policy); 5311 ii->ipsec_in_policy = connp->conn_policy; 5312 5313 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5314 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5315 CONN_DEC_REF(econnp); 5316 freemsg(first_mp); 5317 return (NULL); 5318 } 5319 } 5320 5321 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5322 CONN_DEC_REF(econnp); 5323 freemsg(first_mp); 5324 return (NULL); 5325 } 5326 5327 /* 5328 * If we know we have some policy, pass the "IPSEC" 5329 * options size TCP uses this adjust the MSS. 5330 */ 5331 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5332 if (mctl_present) { 5333 freeb(first_mp); 5334 *mpp = mp; 5335 } 5336 5337 return (econnp); 5338 } 5339 5340 /* 5341 * tcp_get_conn/tcp_free_conn 5342 * 5343 * tcp_get_conn is used to get a clean tcp connection structure. 5344 * It tries to reuse the connections put on the freelist by the 5345 * time_wait_collector failing which it goes to kmem_cache. This 5346 * way has two benefits compared to just allocating from and 5347 * freeing to kmem_cache. 5348 * 1) The time_wait_collector can free (which includes the cleanup) 5349 * outside the squeue. So when the interrupt comes, we have a clean 5350 * connection sitting in the freelist. Obviously, this buys us 5351 * performance. 5352 * 5353 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5354 * has multiple disadvantages - tying up the squeue during alloc, and the 5355 * fact that IPSec policy initialization has to happen here which 5356 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5357 * But allocating the conn/tcp in IP land is also not the best since 5358 * we can't check the 'q' and 'q0' which are protected by squeue and 5359 * blindly allocate memory which might have to be freed here if we are 5360 * not allowed to accept the connection. By using the freelist and 5361 * putting the conn/tcp back in freelist, we don't pay a penalty for 5362 * allocating memory without checking 'q/q0' and freeing it if we can't 5363 * accept the connection. 5364 * 5365 * Care should be taken to put the conn back in the same squeue's freelist 5366 * from which it was allocated. Best results are obtained if conn is 5367 * allocated from listener's squeue and freed to the same. Time wait 5368 * collector will free up the freelist is the connection ends up sitting 5369 * there for too long. 5370 */ 5371 void * 5372 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5373 { 5374 tcp_t *tcp = NULL; 5375 conn_t *connp = NULL; 5376 squeue_t *sqp = (squeue_t *)arg; 5377 tcp_squeue_priv_t *tcp_time_wait; 5378 netstack_t *ns; 5379 5380 tcp_time_wait = 5381 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5382 5383 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5384 tcp = tcp_time_wait->tcp_free_list; 5385 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5386 if (tcp != NULL) { 5387 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5388 tcp_time_wait->tcp_free_list_cnt--; 5389 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5390 tcp->tcp_time_wait_next = NULL; 5391 connp = tcp->tcp_connp; 5392 connp->conn_flags |= IPCL_REUSED; 5393 5394 ASSERT(tcp->tcp_tcps == NULL); 5395 ASSERT(connp->conn_netstack == NULL); 5396 ASSERT(tcp->tcp_rsrv_mp != NULL); 5397 ns = tcps->tcps_netstack; 5398 netstack_hold(ns); 5399 connp->conn_netstack = ns; 5400 tcp->tcp_tcps = tcps; 5401 TCPS_REFHOLD(tcps); 5402 ipcl_globalhash_insert(connp); 5403 return ((void *)connp); 5404 } 5405 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5406 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5407 tcps->tcps_netstack)) == NULL) 5408 return (NULL); 5409 tcp = connp->conn_tcp; 5410 /* 5411 * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed 5412 * until this conn_t/tcp_t is freed at ipcl_conn_destroy(). 5413 */ 5414 if ((tcp->tcp_rsrv_mp = allocb(0, BPRI_HI)) == NULL) { 5415 ipcl_conn_destroy(connp); 5416 return (NULL); 5417 } 5418 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL); 5419 tcp->tcp_tcps = tcps; 5420 TCPS_REFHOLD(tcps); 5421 5422 return ((void *)connp); 5423 } 5424 5425 /* 5426 * Update the cached label for the given tcp_t. This should be called once per 5427 * connection, and before any packets are sent or tcp_process_options is 5428 * invoked. Returns B_FALSE if the correct label could not be constructed. 5429 */ 5430 static boolean_t 5431 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5432 { 5433 conn_t *connp = tcp->tcp_connp; 5434 5435 if (tcp->tcp_ipversion == IPV4_VERSION) { 5436 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5437 int added; 5438 5439 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5440 connp->conn_mac_exempt, 5441 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5442 return (B_FALSE); 5443 5444 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5445 if (added == -1) 5446 return (B_FALSE); 5447 tcp->tcp_hdr_len += added; 5448 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5449 tcp->tcp_ip_hdr_len += added; 5450 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5451 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5452 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5453 tcp->tcp_hdr_len); 5454 if (added == -1) 5455 return (B_FALSE); 5456 tcp->tcp_hdr_len += added; 5457 tcp->tcp_tcph = (tcph_t *) 5458 ((uchar_t *)tcp->tcp_tcph + added); 5459 tcp->tcp_ip_hdr_len += added; 5460 } 5461 } else { 5462 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5463 5464 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5465 connp->conn_mac_exempt, 5466 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5467 return (B_FALSE); 5468 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5469 &tcp->tcp_label_len, optbuf) != 0) 5470 return (B_FALSE); 5471 if (tcp_build_hdrs(tcp->tcp_rq, tcp) != 0) 5472 return (B_FALSE); 5473 } 5474 5475 connp->conn_ulp_labeled = 1; 5476 5477 return (B_TRUE); 5478 } 5479 5480 /* BEGIN CSTYLED */ 5481 /* 5482 * 5483 * The sockfs ACCEPT path: 5484 * ======================= 5485 * 5486 * The eager is now established in its own perimeter as soon as SYN is 5487 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5488 * completes the accept processing on the acceptor STREAM. The sending 5489 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5490 * listener but a TLI/XTI listener completes the accept processing 5491 * on the listener perimeter. 5492 * 5493 * Common control flow for 3 way handshake: 5494 * ---------------------------------------- 5495 * 5496 * incoming SYN (listener perimeter) -> tcp_rput_data() 5497 * -> tcp_conn_request() 5498 * 5499 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5500 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5501 * 5502 * Sockfs ACCEPT Path: 5503 * ------------------- 5504 * 5505 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5506 * as STREAM entry point) 5507 * 5508 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5509 * 5510 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5511 * association (we are not behind eager's squeue but sockfs is protecting us 5512 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5513 * is changed to point at tcp_wput(). 5514 * 5515 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5516 * listener (done on listener's perimeter). 5517 * 5518 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5519 * accept. 5520 * 5521 * TLI/XTI client ACCEPT path: 5522 * --------------------------- 5523 * 5524 * soaccept() sends T_CONN_RES on the listener STREAM. 5525 * 5526 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5527 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5528 * 5529 * Locks: 5530 * ====== 5531 * 5532 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5533 * and listeners->tcp_eager_next_q. 5534 * 5535 * Referencing: 5536 * ============ 5537 * 5538 * 1) We start out in tcp_conn_request by eager placing a ref on 5539 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5540 * 5541 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5542 * doing so we place a ref on the eager. This ref is finally dropped at the 5543 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5544 * reference is dropped by the squeue framework. 5545 * 5546 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5547 * 5548 * The reference must be released by the same entity that added the reference 5549 * In the above scheme, the eager is the entity that adds and releases the 5550 * references. Note that tcp_accept_finish executes in the squeue of the eager 5551 * (albeit after it is attached to the acceptor stream). Though 1. executes 5552 * in the listener's squeue, the eager is nascent at this point and the 5553 * reference can be considered to have been added on behalf of the eager. 5554 * 5555 * Eager getting a Reset or listener closing: 5556 * ========================================== 5557 * 5558 * Once the listener and eager are linked, the listener never does the unlink. 5559 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5560 * a message on all eager perimeter. The eager then does the unlink, clears 5561 * any pointers to the listener's queue and drops the reference to the 5562 * listener. The listener waits in tcp_close outside the squeue until its 5563 * refcount has dropped to 1. This ensures that the listener has waited for 5564 * all eagers to clear their association with the listener. 5565 * 5566 * Similarly, if eager decides to go away, it can unlink itself and close. 5567 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5568 * the reference to eager is still valid because of the extra ref we put 5569 * in tcp_send_conn_ind. 5570 * 5571 * Listener can always locate the eager under the protection 5572 * of the listener->tcp_eager_lock, and then do a refhold 5573 * on the eager during the accept processing. 5574 * 5575 * The acceptor stream accesses the eager in the accept processing 5576 * based on the ref placed on eager before sending T_conn_ind. 5577 * The only entity that can negate this refhold is a listener close 5578 * which is mutually exclusive with an active acceptor stream. 5579 * 5580 * Eager's reference on the listener 5581 * =================================== 5582 * 5583 * If the accept happens (even on a closed eager) the eager drops its 5584 * reference on the listener at the start of tcp_accept_finish. If the 5585 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5586 * the reference is dropped in tcp_closei_local. If the listener closes, 5587 * the reference is dropped in tcp_eager_kill. In all cases the reference 5588 * is dropped while executing in the eager's context (squeue). 5589 */ 5590 /* END CSTYLED */ 5591 5592 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5593 5594 /* 5595 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5596 * tcp_rput_data will not see any SYN packets. 5597 */ 5598 /* ARGSUSED */ 5599 void 5600 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5601 { 5602 tcph_t *tcph; 5603 uint32_t seg_seq; 5604 tcp_t *eager; 5605 uint_t ipvers; 5606 ipha_t *ipha; 5607 ip6_t *ip6h; 5608 int err; 5609 conn_t *econnp = NULL; 5610 squeue_t *new_sqp; 5611 mblk_t *mp1; 5612 uint_t ip_hdr_len; 5613 conn_t *connp = (conn_t *)arg; 5614 tcp_t *tcp = connp->conn_tcp; 5615 cred_t *credp; 5616 tcp_stack_t *tcps = tcp->tcp_tcps; 5617 ip_stack_t *ipst; 5618 5619 if (tcp->tcp_state != TCPS_LISTEN) 5620 goto error2; 5621 5622 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5623 5624 mutex_enter(&tcp->tcp_eager_lock); 5625 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5626 mutex_exit(&tcp->tcp_eager_lock); 5627 TCP_STAT(tcps, tcp_listendrop); 5628 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5629 if (tcp->tcp_debug) { 5630 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5631 "tcp_conn_request: listen backlog (max=%d) " 5632 "overflow (%d pending) on %s", 5633 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5634 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5635 } 5636 goto error2; 5637 } 5638 5639 if (tcp->tcp_conn_req_cnt_q0 >= 5640 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5641 /* 5642 * Q0 is full. Drop a pending half-open req from the queue 5643 * to make room for the new SYN req. Also mark the time we 5644 * drop a SYN. 5645 * 5646 * A more aggressive defense against SYN attack will 5647 * be to set the "tcp_syn_defense" flag now. 5648 */ 5649 TCP_STAT(tcps, tcp_listendropq0); 5650 tcp->tcp_last_rcv_lbolt = lbolt64; 5651 if (!tcp_drop_q0(tcp)) { 5652 mutex_exit(&tcp->tcp_eager_lock); 5653 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5654 if (tcp->tcp_debug) { 5655 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5656 "tcp_conn_request: listen half-open queue " 5657 "(max=%d) full (%d pending) on %s", 5658 tcps->tcps_conn_req_max_q0, 5659 tcp->tcp_conn_req_cnt_q0, 5660 tcp_display(tcp, NULL, 5661 DISP_PORT_ONLY)); 5662 } 5663 goto error2; 5664 } 5665 } 5666 mutex_exit(&tcp->tcp_eager_lock); 5667 5668 /* 5669 * IP adds STRUIO_EAGER and ensures that the received packet is 5670 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5671 * link local address. If IPSec is enabled, db_struioflag has 5672 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5673 * otherwise an error case if neither of them is set. 5674 */ 5675 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5676 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5677 DB_CKSUMSTART(mp) = 0; 5678 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5679 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5680 if (econnp == NULL) 5681 goto error2; 5682 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5683 econnp->conn_sqp = new_sqp; 5684 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5685 /* 5686 * mp is updated in tcp_get_ipsec_conn(). 5687 */ 5688 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5689 if (econnp == NULL) { 5690 /* 5691 * mp freed by tcp_get_ipsec_conn. 5692 */ 5693 return; 5694 } 5695 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5696 } else { 5697 goto error2; 5698 } 5699 5700 ASSERT(DB_TYPE(mp) == M_DATA); 5701 5702 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5703 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5704 ASSERT(OK_32PTR(mp->b_rptr)); 5705 if (ipvers == IPV4_VERSION) { 5706 ipha = (ipha_t *)mp->b_rptr; 5707 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5708 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5709 } else { 5710 ip6h = (ip6_t *)mp->b_rptr; 5711 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5712 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5713 } 5714 5715 if (tcp->tcp_family == AF_INET) { 5716 ASSERT(ipvers == IPV4_VERSION); 5717 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5718 } else { 5719 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5720 } 5721 5722 if (err) 5723 goto error3; 5724 5725 eager = econnp->conn_tcp; 5726 5727 /* 5728 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 5729 * will always have that to send up. Otherwise, we need to do 5730 * special handling in case the allocation fails at that time. 5731 */ 5732 ASSERT(eager->tcp_ordrel_mp == NULL); 5733 if ((eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) 5734 goto error3; 5735 5736 /* Inherit various TCP parameters from the listener */ 5737 eager->tcp_naglim = tcp->tcp_naglim; 5738 eager->tcp_first_timer_threshold = 5739 tcp->tcp_first_timer_threshold; 5740 eager->tcp_second_timer_threshold = 5741 tcp->tcp_second_timer_threshold; 5742 5743 eager->tcp_first_ctimer_threshold = 5744 tcp->tcp_first_ctimer_threshold; 5745 eager->tcp_second_ctimer_threshold = 5746 tcp->tcp_second_ctimer_threshold; 5747 5748 /* 5749 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5750 * If it does not, the eager's receive window will be set to the 5751 * listener's receive window later in this function. 5752 */ 5753 eager->tcp_rwnd = 0; 5754 5755 /* 5756 * Inherit listener's tcp_init_cwnd. Need to do this before 5757 * calling tcp_process_options() where tcp_mss_set() is called 5758 * to set the initial cwnd. 5759 */ 5760 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5761 5762 /* 5763 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5764 * zone id before the accept is completed in tcp_wput_accept(). 5765 */ 5766 econnp->conn_zoneid = connp->conn_zoneid; 5767 econnp->conn_allzones = connp->conn_allzones; 5768 5769 /* Copy nexthop information from listener to eager */ 5770 if (connp->conn_nexthop_set) { 5771 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5772 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5773 } 5774 5775 /* 5776 * TSOL: tsol_input_proc() needs the eager's cred before the 5777 * eager is accepted 5778 */ 5779 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5780 crhold(credp); 5781 5782 /* 5783 * If the caller has the process-wide flag set, then default to MAC 5784 * exempt mode. This allows read-down to unlabeled hosts. 5785 */ 5786 if (getpflags(NET_MAC_AWARE, credp) != 0) 5787 econnp->conn_mac_exempt = B_TRUE; 5788 5789 if (is_system_labeled()) { 5790 cred_t *cr; 5791 5792 if (connp->conn_mlp_type != mlptSingle) { 5793 cr = econnp->conn_peercred = DB_CRED(mp); 5794 if (cr != NULL) 5795 crhold(cr); 5796 else 5797 cr = econnp->conn_cred; 5798 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5799 econnp, cred_t *, cr) 5800 } else { 5801 cr = econnp->conn_cred; 5802 DTRACE_PROBE2(syn_accept, conn_t *, 5803 econnp, cred_t *, cr) 5804 } 5805 5806 if (!tcp_update_label(eager, cr)) { 5807 DTRACE_PROBE3( 5808 tx__ip__log__error__connrequest__tcp, 5809 char *, "eager connp(1) label on SYN mp(2) failed", 5810 conn_t *, econnp, mblk_t *, mp); 5811 goto error3; 5812 } 5813 } 5814 5815 eager->tcp_hard_binding = B_TRUE; 5816 5817 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5818 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5819 5820 CL_INET_CONNECT(eager); 5821 5822 /* 5823 * No need to check for multicast destination since ip will only pass 5824 * up multicasts to those that have expressed interest 5825 * TODO: what about rejecting broadcasts? 5826 * Also check that source is not a multicast or broadcast address. 5827 */ 5828 eager->tcp_state = TCPS_SYN_RCVD; 5829 5830 5831 /* 5832 * There should be no ire in the mp as we are being called after 5833 * receiving the SYN. 5834 */ 5835 ASSERT(tcp_ire_mp(mp) == NULL); 5836 5837 /* 5838 * Adapt our mss, ttl, ... according to information provided in IRE. 5839 */ 5840 5841 if (tcp_adapt_ire(eager, NULL) == 0) { 5842 /* Undo the bind_hash_insert */ 5843 tcp_bind_hash_remove(eager); 5844 goto error3; 5845 } 5846 5847 /* Process all TCP options. */ 5848 tcp_process_options(eager, tcph); 5849 5850 /* Is the other end ECN capable? */ 5851 if (tcps->tcps_ecn_permitted >= 1 && 5852 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5853 eager->tcp_ecn_ok = B_TRUE; 5854 } 5855 5856 /* 5857 * listener->tcp_rq->q_hiwat should be the default window size or a 5858 * window size changed via SO_RCVBUF option. First round up the 5859 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5860 * scale option value if needed. Call tcp_rwnd_set() to finish the 5861 * setting. 5862 * 5863 * Note if there is a rpipe metric associated with the remote host, 5864 * we should not inherit receive window size from listener. 5865 */ 5866 eager->tcp_rwnd = MSS_ROUNDUP( 5867 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5868 eager->tcp_rwnd), eager->tcp_mss); 5869 if (eager->tcp_snd_ws_ok) 5870 tcp_set_ws_value(eager); 5871 /* 5872 * Note that this is the only place tcp_rwnd_set() is called for 5873 * accepting a connection. We need to call it here instead of 5874 * after the 3-way handshake because we need to tell the other 5875 * side our rwnd in the SYN-ACK segment. 5876 */ 5877 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5878 5879 /* 5880 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5881 * via soaccept()->soinheritoptions() which essentially applies 5882 * all the listener options to the new STREAM. The options that we 5883 * need to take care of are: 5884 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5885 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5886 * SO_SNDBUF, SO_RCVBUF. 5887 * 5888 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5889 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5890 * tcp_maxpsz_set() gets called later from 5891 * tcp_accept_finish(), the option takes effect. 5892 * 5893 */ 5894 /* Set the TCP options */ 5895 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5896 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5897 eager->tcp_oobinline = tcp->tcp_oobinline; 5898 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5899 eager->tcp_broadcast = tcp->tcp_broadcast; 5900 eager->tcp_useloopback = tcp->tcp_useloopback; 5901 eager->tcp_dontroute = tcp->tcp_dontroute; 5902 eager->tcp_linger = tcp->tcp_linger; 5903 eager->tcp_lingertime = tcp->tcp_lingertime; 5904 if (tcp->tcp_ka_enabled) 5905 eager->tcp_ka_enabled = 1; 5906 5907 /* Set the IP options */ 5908 econnp->conn_broadcast = connp->conn_broadcast; 5909 econnp->conn_loopback = connp->conn_loopback; 5910 econnp->conn_dontroute = connp->conn_dontroute; 5911 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5912 5913 /* Put a ref on the listener for the eager. */ 5914 CONN_INC_REF(connp); 5915 mutex_enter(&tcp->tcp_eager_lock); 5916 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5917 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5918 tcp->tcp_eager_next_q0 = eager; 5919 eager->tcp_eager_prev_q0 = tcp; 5920 5921 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5922 eager->tcp_listener = tcp; 5923 eager->tcp_saved_listener = tcp; 5924 5925 /* 5926 * Tag this detached tcp vector for later retrieval 5927 * by our listener client in tcp_accept(). 5928 */ 5929 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5930 tcp->tcp_conn_req_cnt_q0++; 5931 if (++tcp->tcp_conn_req_seqnum == -1) { 5932 /* 5933 * -1 is "special" and defined in TPI as something 5934 * that should never be used in T_CONN_IND 5935 */ 5936 ++tcp->tcp_conn_req_seqnum; 5937 } 5938 mutex_exit(&tcp->tcp_eager_lock); 5939 5940 if (tcp->tcp_syn_defense) { 5941 /* Don't drop the SYN that comes from a good IP source */ 5942 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5943 if (addr_cache != NULL && eager->tcp_remote == 5944 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5945 eager->tcp_dontdrop = B_TRUE; 5946 } 5947 } 5948 5949 /* 5950 * We need to insert the eager in its own perimeter but as soon 5951 * as we do that, we expose the eager to the classifier and 5952 * should not touch any field outside the eager's perimeter. 5953 * So do all the work necessary before inserting the eager 5954 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5955 * will succeed but undo everything if it fails. 5956 */ 5957 seg_seq = ABE32_TO_U32(tcph->th_seq); 5958 eager->tcp_irs = seg_seq; 5959 eager->tcp_rack = seg_seq; 5960 eager->tcp_rnxt = seg_seq + 1; 5961 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5962 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5963 eager->tcp_state = TCPS_SYN_RCVD; 5964 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5965 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5966 if (mp1 == NULL) { 5967 /* 5968 * Increment the ref count as we are going to 5969 * enqueueing an mp in squeue 5970 */ 5971 CONN_INC_REF(econnp); 5972 goto error; 5973 } 5974 DB_CPID(mp1) = tcp->tcp_cpid; 5975 eager->tcp_cpid = tcp->tcp_cpid; 5976 eager->tcp_open_time = lbolt64; 5977 5978 /* 5979 * We need to start the rto timer. In normal case, we start 5980 * the timer after sending the packet on the wire (or at 5981 * least believing that packet was sent by waiting for 5982 * CALL_IP_WPUT() to return). Since this is the first packet 5983 * being sent on the wire for the eager, our initial tcp_rto 5984 * is at least tcp_rexmit_interval_min which is a fairly 5985 * large value to allow the algorithm to adjust slowly to large 5986 * fluctuations of RTT during first few transmissions. 5987 * 5988 * Starting the timer first and then sending the packet in this 5989 * case shouldn't make much difference since tcp_rexmit_interval_min 5990 * is of the order of several 100ms and starting the timer 5991 * first and then sending the packet will result in difference 5992 * of few micro seconds. 5993 * 5994 * Without this optimization, we are forced to hold the fanout 5995 * lock across the ipcl_bind_insert() and sending the packet 5996 * so that we don't race against an incoming packet (maybe RST) 5997 * for this eager. 5998 * 5999 * It is necessary to acquire an extra reference on the eager 6000 * at this point and hold it until after tcp_send_data() to 6001 * ensure against an eager close race. 6002 */ 6003 6004 CONN_INC_REF(eager->tcp_connp); 6005 6006 TCP_TIMER_RESTART(eager, eager->tcp_rto); 6007 6008 /* 6009 * Insert the eager in its own perimeter now. We are ready to deal 6010 * with any packets on eager. 6011 */ 6012 if (eager->tcp_ipversion == IPV4_VERSION) { 6013 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 6014 goto error; 6015 } 6016 } else { 6017 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 6018 goto error; 6019 } 6020 } 6021 6022 /* mark conn as fully-bound */ 6023 econnp->conn_fully_bound = B_TRUE; 6024 6025 /* Send the SYN-ACK */ 6026 tcp_send_data(eager, eager->tcp_wq, mp1); 6027 CONN_DEC_REF(eager->tcp_connp); 6028 freemsg(mp); 6029 6030 return; 6031 error: 6032 freemsg(mp1); 6033 eager->tcp_closemp_used = B_TRUE; 6034 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6035 squeue_fill(econnp->conn_sqp, &eager->tcp_closemp, tcp_eager_kill, 6036 econnp, SQTAG_TCP_CONN_REQ_2); 6037 6038 /* 6039 * If a connection already exists, send the mp to that connections so 6040 * that it can be appropriately dealt with. 6041 */ 6042 ipst = tcps->tcps_netstack->netstack_ip; 6043 6044 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 6045 if (!IPCL_IS_CONNECTED(econnp)) { 6046 /* 6047 * Something bad happened. ipcl_conn_insert() 6048 * failed because a connection already existed 6049 * in connected hash but we can't find it 6050 * anymore (someone blew it away). Just 6051 * free this message and hopefully remote 6052 * will retransmit at which time the SYN can be 6053 * treated as a new connection or dealth with 6054 * a TH_RST if a connection already exists. 6055 */ 6056 CONN_DEC_REF(econnp); 6057 freemsg(mp); 6058 } else { 6059 squeue_fill(econnp->conn_sqp, mp, tcp_input, 6060 econnp, SQTAG_TCP_CONN_REQ_1); 6061 } 6062 } else { 6063 /* Nobody wants this packet */ 6064 freemsg(mp); 6065 } 6066 return; 6067 error3: 6068 CONN_DEC_REF(econnp); 6069 error2: 6070 freemsg(mp); 6071 } 6072 6073 /* 6074 * In an ideal case of vertical partition in NUMA architecture, its 6075 * beneficial to have the listener and all the incoming connections 6076 * tied to the same squeue. The other constraint is that incoming 6077 * connections should be tied to the squeue attached to interrupted 6078 * CPU for obvious locality reason so this leaves the listener to 6079 * be tied to the same squeue. Our only problem is that when listener 6080 * is binding, the CPU that will get interrupted by the NIC whose 6081 * IP address the listener is binding to is not even known. So 6082 * the code below allows us to change that binding at the time the 6083 * CPU is interrupted by virtue of incoming connection's squeue. 6084 * 6085 * This is usefull only in case of a listener bound to a specific IP 6086 * address. For other kind of listeners, they get bound the 6087 * very first time and there is no attempt to rebind them. 6088 */ 6089 void 6090 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 6091 { 6092 conn_t *connp = (conn_t *)arg; 6093 squeue_t *sqp = (squeue_t *)arg2; 6094 squeue_t *new_sqp; 6095 uint32_t conn_flags; 6096 6097 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 6098 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 6099 } else { 6100 goto done; 6101 } 6102 6103 if (connp->conn_fanout == NULL) 6104 goto done; 6105 6106 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 6107 mutex_enter(&connp->conn_fanout->connf_lock); 6108 mutex_enter(&connp->conn_lock); 6109 /* 6110 * No one from read or write side can access us now 6111 * except for already queued packets on this squeue. 6112 * But since we haven't changed the squeue yet, they 6113 * can't execute. If they are processed after we have 6114 * changed the squeue, they are sent back to the 6115 * correct squeue down below. 6116 * But a listner close can race with processing of 6117 * incoming SYN. If incoming SYN processing changes 6118 * the squeue then the listener close which is waiting 6119 * to enter the squeue would operate on the wrong 6120 * squeue. Hence we don't change the squeue here unless 6121 * the refcount is exactly the minimum refcount. The 6122 * minimum refcount of 4 is counted as - 1 each for 6123 * TCP and IP, 1 for being in the classifier hash, and 6124 * 1 for the mblk being processed. 6125 */ 6126 6127 if (connp->conn_ref != 4 || 6128 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 6129 mutex_exit(&connp->conn_lock); 6130 mutex_exit(&connp->conn_fanout->connf_lock); 6131 goto done; 6132 } 6133 if (connp->conn_sqp != new_sqp) { 6134 while (connp->conn_sqp != new_sqp) 6135 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 6136 } 6137 6138 do { 6139 conn_flags = connp->conn_flags; 6140 conn_flags |= IPCL_FULLY_BOUND; 6141 (void) cas32(&connp->conn_flags, connp->conn_flags, 6142 conn_flags); 6143 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 6144 6145 mutex_exit(&connp->conn_fanout->connf_lock); 6146 mutex_exit(&connp->conn_lock); 6147 } 6148 6149 done: 6150 if (connp->conn_sqp != sqp) { 6151 CONN_INC_REF(connp); 6152 squeue_fill(connp->conn_sqp, mp, 6153 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 6154 } else { 6155 tcp_conn_request(connp, mp, sqp); 6156 } 6157 } 6158 6159 /* 6160 * Successful connect request processing begins when our client passes 6161 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 6162 * our T_OK_ACK reply message upstream. The control flow looks like this: 6163 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 6164 * upstream <- tcp_rput() <- IP 6165 * After various error checks are completed, tcp_connect() lays 6166 * the target address and port into the composite header template, 6167 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 6168 * request followed by an IRE request, and passes the three mblk message 6169 * down to IP looking like this: 6170 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 6171 * Processing continues in tcp_rput() when we receive the following message: 6172 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 6173 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 6174 * to fire off the connection request, and then passes the T_OK_ACK mblk 6175 * upstream that we filled in below. There are, of course, numerous 6176 * error conditions along the way which truncate the processing described 6177 * above. 6178 */ 6179 static void 6180 tcp_connect(tcp_t *tcp, mblk_t *mp) 6181 { 6182 sin_t *sin; 6183 sin6_t *sin6; 6184 queue_t *q = tcp->tcp_wq; 6185 struct T_conn_req *tcr; 6186 ipaddr_t *dstaddrp; 6187 in_port_t dstport; 6188 uint_t srcid; 6189 6190 tcr = (struct T_conn_req *)mp->b_rptr; 6191 6192 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6193 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 6194 tcp_err_ack(tcp, mp, TPROTO, 0); 6195 return; 6196 } 6197 6198 /* 6199 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 6200 * will always have that to send up. Otherwise, we need to do 6201 * special handling in case the allocation fails at that time. 6202 * If the end point is TPI, the tcp_t can be reused and the 6203 * tcp_ordrel_mp may be allocated already. 6204 */ 6205 if (tcp->tcp_ordrel_mp == NULL) { 6206 if ((tcp->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) { 6207 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6208 return; 6209 } 6210 } 6211 6212 /* 6213 * Determine packet type based on type of address passed in 6214 * the request should contain an IPv4 or IPv6 address. 6215 * Make sure that address family matches the type of 6216 * family of the the address passed down 6217 */ 6218 switch (tcr->DEST_length) { 6219 default: 6220 tcp_err_ack(tcp, mp, TBADADDR, 0); 6221 return; 6222 6223 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 6224 /* 6225 * XXX: The check for valid DEST_length was not there 6226 * in earlier releases and some buggy 6227 * TLI apps (e.g Sybase) got away with not feeding 6228 * in sin_zero part of address. 6229 * We allow that bug to keep those buggy apps humming. 6230 * Test suites require the check on DEST_length. 6231 * We construct a new mblk with valid DEST_length 6232 * free the original so the rest of the code does 6233 * not have to keep track of this special shorter 6234 * length address case. 6235 */ 6236 mblk_t *nmp; 6237 struct T_conn_req *ntcr; 6238 sin_t *nsin; 6239 6240 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6241 tcr->OPT_length, BPRI_HI); 6242 if (nmp == NULL) { 6243 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6244 return; 6245 } 6246 ntcr = (struct T_conn_req *)nmp->b_rptr; 6247 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6248 ntcr->PRIM_type = T_CONN_REQ; 6249 ntcr->DEST_length = sizeof (sin_t); 6250 ntcr->DEST_offset = sizeof (struct T_conn_req); 6251 6252 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6253 *nsin = sin_null; 6254 /* Get pointer to shorter address to copy from original mp */ 6255 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6256 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6257 if (sin == NULL || !OK_32PTR((char *)sin)) { 6258 freemsg(nmp); 6259 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6260 return; 6261 } 6262 nsin->sin_family = sin->sin_family; 6263 nsin->sin_port = sin->sin_port; 6264 nsin->sin_addr = sin->sin_addr; 6265 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6266 nmp->b_wptr = (uchar_t *)&nsin[1]; 6267 if (tcr->OPT_length != 0) { 6268 ntcr->OPT_length = tcr->OPT_length; 6269 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6270 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6271 (uchar_t *)ntcr + ntcr->OPT_offset, 6272 tcr->OPT_length); 6273 nmp->b_wptr += tcr->OPT_length; 6274 } 6275 freemsg(mp); /* original mp freed */ 6276 mp = nmp; /* re-initialize original variables */ 6277 tcr = ntcr; 6278 } 6279 /* FALLTHRU */ 6280 6281 case sizeof (sin_t): 6282 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6283 sizeof (sin_t)); 6284 if (sin == NULL || !OK_32PTR((char *)sin)) { 6285 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6286 return; 6287 } 6288 if (tcp->tcp_family != AF_INET || 6289 sin->sin_family != AF_INET) { 6290 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6291 return; 6292 } 6293 if (sin->sin_port == 0) { 6294 tcp_err_ack(tcp, mp, TBADADDR, 0); 6295 return; 6296 } 6297 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 6298 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6299 return; 6300 } 6301 6302 break; 6303 6304 case sizeof (sin6_t): 6305 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 6306 sizeof (sin6_t)); 6307 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 6308 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6309 return; 6310 } 6311 if (tcp->tcp_family != AF_INET6 || 6312 sin6->sin6_family != AF_INET6) { 6313 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6314 return; 6315 } 6316 if (sin6->sin6_port == 0) { 6317 tcp_err_ack(tcp, mp, TBADADDR, 0); 6318 return; 6319 } 6320 break; 6321 } 6322 /* 6323 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6324 * should key on their sequence number and cut them loose. 6325 */ 6326 6327 /* 6328 * If options passed in, feed it for verification and handling 6329 */ 6330 if (tcr->OPT_length != 0) { 6331 mblk_t *ok_mp; 6332 mblk_t *discon_mp; 6333 mblk_t *conn_opts_mp; 6334 int t_error, sys_error, do_disconnect; 6335 6336 conn_opts_mp = NULL; 6337 6338 if (tcp_conprim_opt_process(tcp, mp, 6339 &do_disconnect, &t_error, &sys_error) < 0) { 6340 if (do_disconnect) { 6341 ASSERT(t_error == 0 && sys_error == 0); 6342 discon_mp = mi_tpi_discon_ind(NULL, 6343 ECONNREFUSED, 0); 6344 if (!discon_mp) { 6345 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6346 TSYSERR, ENOMEM); 6347 return; 6348 } 6349 ok_mp = mi_tpi_ok_ack_alloc(mp); 6350 if (!ok_mp) { 6351 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6352 TSYSERR, ENOMEM); 6353 return; 6354 } 6355 qreply(q, ok_mp); 6356 qreply(q, discon_mp); /* no flush! */ 6357 } else { 6358 ASSERT(t_error != 0); 6359 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6360 sys_error); 6361 } 6362 return; 6363 } 6364 /* 6365 * Success in setting options, the mp option buffer represented 6366 * by OPT_length/offset has been potentially modified and 6367 * contains results of option processing. We copy it in 6368 * another mp to save it for potentially influencing returning 6369 * it in T_CONN_CONN. 6370 */ 6371 if (tcr->OPT_length != 0) { /* there are resulting options */ 6372 conn_opts_mp = copyb(mp); 6373 if (!conn_opts_mp) { 6374 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6375 TSYSERR, ENOMEM); 6376 return; 6377 } 6378 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6379 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6380 /* 6381 * Note: 6382 * These resulting option negotiation can include any 6383 * end-to-end negotiation options but there no such 6384 * thing (yet?) in our TCP/IP. 6385 */ 6386 } 6387 } 6388 6389 /* 6390 * If we're connecting to an IPv4-mapped IPv6 address, we need to 6391 * make sure that the template IP header in the tcp structure is an 6392 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 6393 * need to this before we call tcp_bindi() so that the port lookup 6394 * code will look for ports in the correct port space (IPv4 and 6395 * IPv6 have separate port spaces). 6396 */ 6397 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 6398 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6399 int err = 0; 6400 6401 err = tcp_header_init_ipv4(tcp); 6402 if (err != 0) { 6403 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6404 goto connect_failed; 6405 } 6406 if (tcp->tcp_lport != 0) 6407 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 6408 } 6409 6410 if (tcp->tcp_issocket) { 6411 /* 6412 * TCP is _D_SODIRECT and sockfs is directly above so save 6413 * the shared sonode sodirect_t pointer (if any) to enable 6414 * TCP sodirect. 6415 */ 6416 tcp->tcp_sodirect = SOD_QTOSODP(tcp->tcp_rq); 6417 } 6418 6419 switch (tcp->tcp_state) { 6420 case TCPS_IDLE: 6421 /* 6422 * We support quick connect, refer to comments in 6423 * tcp_connect_*() 6424 */ 6425 /* FALLTHRU */ 6426 case TCPS_BOUND: 6427 case TCPS_LISTEN: 6428 if (tcp->tcp_family == AF_INET6) { 6429 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6430 tcp_connect_ipv6(tcp, mp, 6431 &sin6->sin6_addr, 6432 sin6->sin6_port, sin6->sin6_flowinfo, 6433 sin6->__sin6_src_id, sin6->sin6_scope_id); 6434 return; 6435 } 6436 /* 6437 * Destination adress is mapped IPv6 address. 6438 * Source bound address should be unspecified or 6439 * IPv6 mapped address as well. 6440 */ 6441 if (!IN6_IS_ADDR_UNSPECIFIED( 6442 &tcp->tcp_bound_source_v6) && 6443 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 6444 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 6445 EADDRNOTAVAIL); 6446 break; 6447 } 6448 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 6449 dstport = sin6->sin6_port; 6450 srcid = sin6->__sin6_src_id; 6451 } else { 6452 dstaddrp = &sin->sin_addr.s_addr; 6453 dstport = sin->sin_port; 6454 srcid = 0; 6455 } 6456 6457 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 6458 return; 6459 default: 6460 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 6461 break; 6462 } 6463 /* 6464 * Note: Code below is the "failure" case 6465 */ 6466 /* return error ack and blow away saved option results if any */ 6467 connect_failed: 6468 if (mp != NULL) 6469 putnext(tcp->tcp_rq, mp); 6470 else { 6471 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6472 TSYSERR, ENOMEM); 6473 } 6474 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6475 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6476 } 6477 6478 /* 6479 * Handle connect to IPv4 destinations, including connections for AF_INET6 6480 * sockets connecting to IPv4 mapped IPv6 destinations. 6481 */ 6482 static void 6483 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6484 uint_t srcid) 6485 { 6486 tcph_t *tcph; 6487 mblk_t *mp1; 6488 ipaddr_t dstaddr = *dstaddrp; 6489 int32_t oldstate; 6490 uint16_t lport; 6491 tcp_stack_t *tcps = tcp->tcp_tcps; 6492 6493 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6494 6495 /* Check for attempt to connect to INADDR_ANY */ 6496 if (dstaddr == INADDR_ANY) { 6497 /* 6498 * SunOS 4.x and 4.3 BSD allow an application 6499 * to connect a TCP socket to INADDR_ANY. 6500 * When they do this, the kernel picks the 6501 * address of one interface and uses it 6502 * instead. The kernel usually ends up 6503 * picking the address of the loopback 6504 * interface. This is an undocumented feature. 6505 * However, we provide the same thing here 6506 * in order to have source and binary 6507 * compatibility with SunOS 4.x. 6508 * Update the T_CONN_REQ (sin/sin6) since it is used to 6509 * generate the T_CONN_CON. 6510 */ 6511 dstaddr = htonl(INADDR_LOOPBACK); 6512 *dstaddrp = dstaddr; 6513 } 6514 6515 /* Handle __sin6_src_id if socket not bound to an IP address */ 6516 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6517 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6518 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6519 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6520 tcp->tcp_ipha->ipha_src); 6521 } 6522 6523 /* 6524 * Don't let an endpoint connect to itself. Note that 6525 * the test here does not catch the case where the 6526 * source IP addr was left unspecified by the user. In 6527 * this case, the source addr is set in tcp_adapt_ire() 6528 * using the reply to the T_BIND message that we send 6529 * down to IP here and the check is repeated in tcp_rput_other. 6530 */ 6531 if (dstaddr == tcp->tcp_ipha->ipha_src && 6532 dstport == tcp->tcp_lport) { 6533 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6534 goto failed; 6535 } 6536 6537 tcp->tcp_ipha->ipha_dst = dstaddr; 6538 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6539 6540 /* 6541 * Massage a source route if any putting the first hop 6542 * in iph_dst. Compute a starting value for the checksum which 6543 * takes into account that the original iph_dst should be 6544 * included in the checksum but that ip will include the 6545 * first hop in the source route in the tcp checksum. 6546 */ 6547 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6548 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6549 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6550 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6551 if ((int)tcp->tcp_sum < 0) 6552 tcp->tcp_sum--; 6553 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6554 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6555 (tcp->tcp_sum >> 16)); 6556 tcph = tcp->tcp_tcph; 6557 *(uint16_t *)tcph->th_fport = dstport; 6558 tcp->tcp_fport = dstport; 6559 6560 oldstate = tcp->tcp_state; 6561 /* 6562 * At this point the remote destination address and remote port fields 6563 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6564 * have to see which state tcp was in so we can take apropriate action. 6565 */ 6566 if (oldstate == TCPS_IDLE) { 6567 /* 6568 * We support a quick connect capability here, allowing 6569 * clients to transition directly from IDLE to SYN_SENT 6570 * tcp_bindi will pick an unused port, insert the connection 6571 * in the bind hash and transition to BOUND state. 6572 */ 6573 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6574 tcp, B_TRUE); 6575 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6576 B_FALSE, B_FALSE); 6577 if (lport == 0) { 6578 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6579 goto failed; 6580 } 6581 } 6582 tcp->tcp_state = TCPS_SYN_SENT; 6583 6584 /* 6585 * TODO: allow data with connect requests 6586 * by unlinking M_DATA trailers here and 6587 * linking them in behind the T_OK_ACK mblk. 6588 * The tcp_rput() bind ack handler would then 6589 * feed them to tcp_wput_data() rather than call 6590 * tcp_timer(). 6591 */ 6592 mp = mi_tpi_ok_ack_alloc(mp); 6593 if (!mp) { 6594 tcp->tcp_state = oldstate; 6595 goto failed; 6596 } 6597 if (tcp->tcp_family == AF_INET) { 6598 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6599 sizeof (ipa_conn_t)); 6600 } else { 6601 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6602 sizeof (ipa6_conn_t)); 6603 } 6604 if (mp1) { 6605 /* 6606 * We need to make sure that the conn_recv is set to a non-null 6607 * value before we insert the conn_t into the classifier table. 6608 * This is to avoid a race with an incoming packet which does 6609 * an ipcl_classify(). 6610 */ 6611 tcp->tcp_connp->conn_recv = tcp_input; 6612 6613 /* Hang onto the T_OK_ACK for later. */ 6614 linkb(mp1, mp); 6615 mblk_setcred(mp1, tcp->tcp_cred); 6616 if (tcp->tcp_family == AF_INET) 6617 mp1 = ip_bind_v4(tcp->tcp_wq, mp1, tcp->tcp_connp); 6618 else { 6619 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6620 &tcp->tcp_sticky_ipp); 6621 } 6622 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6623 tcp->tcp_active_open = 1; 6624 /* 6625 * If the bind cannot complete immediately 6626 * IP will arrange to call tcp_rput_other 6627 * when the bind completes. 6628 */ 6629 if (mp1 != NULL) 6630 tcp_rput_other(tcp, mp1); 6631 return; 6632 } 6633 /* Error case */ 6634 tcp->tcp_state = oldstate; 6635 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6636 6637 failed: 6638 /* return error ack and blow away saved option results if any */ 6639 if (mp != NULL) 6640 putnext(tcp->tcp_rq, mp); 6641 else { 6642 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6643 TSYSERR, ENOMEM); 6644 } 6645 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6646 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6647 6648 } 6649 6650 /* 6651 * Handle connect to IPv6 destinations. 6652 */ 6653 static void 6654 tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 6655 in_port_t dstport, uint32_t flowinfo, uint_t srcid, uint32_t scope_id) 6656 { 6657 tcph_t *tcph; 6658 mblk_t *mp1; 6659 ip6_rthdr_t *rth; 6660 int32_t oldstate; 6661 uint16_t lport; 6662 tcp_stack_t *tcps = tcp->tcp_tcps; 6663 6664 ASSERT(tcp->tcp_family == AF_INET6); 6665 6666 /* 6667 * If we're here, it means that the destination address is a native 6668 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6669 * reason why it might not be IPv6 is if the socket was bound to an 6670 * IPv4-mapped IPv6 address. 6671 */ 6672 if (tcp->tcp_ipversion != IPV6_VERSION) { 6673 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6674 goto failed; 6675 } 6676 6677 /* 6678 * Interpret a zero destination to mean loopback. 6679 * Update the T_CONN_REQ (sin/sin6) since it is used to 6680 * generate the T_CONN_CON. 6681 */ 6682 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6683 *dstaddrp = ipv6_loopback; 6684 } 6685 6686 /* Handle __sin6_src_id if socket not bound to an IP address */ 6687 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6688 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6689 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6690 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6691 } 6692 6693 /* 6694 * Take care of the scope_id now and add ip6i_t 6695 * if ip6i_t is not already allocated through TCP 6696 * sticky options. At this point tcp_ip6h does not 6697 * have dst info, thus use dstaddrp. 6698 */ 6699 if (scope_id != 0 && 6700 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6701 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6702 ip6i_t *ip6i; 6703 6704 ipp->ipp_ifindex = scope_id; 6705 ip6i = (ip6i_t *)tcp->tcp_iphc; 6706 6707 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6708 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6709 /* Already allocated */ 6710 ip6i->ip6i_flags |= IP6I_IFINDEX; 6711 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6712 ipp->ipp_fields |= IPPF_SCOPE_ID; 6713 } else { 6714 int reterr; 6715 6716 ipp->ipp_fields |= IPPF_SCOPE_ID; 6717 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6718 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6719 reterr = tcp_build_hdrs(tcp->tcp_rq, tcp); 6720 if (reterr != 0) 6721 goto failed; 6722 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6723 } 6724 } 6725 6726 /* 6727 * Don't let an endpoint connect to itself. Note that 6728 * the test here does not catch the case where the 6729 * source IP addr was left unspecified by the user. In 6730 * this case, the source addr is set in tcp_adapt_ire() 6731 * using the reply to the T_BIND message that we send 6732 * down to IP here and the check is repeated in tcp_rput_other. 6733 */ 6734 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6735 (dstport == tcp->tcp_lport)) { 6736 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6737 goto failed; 6738 } 6739 6740 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6741 tcp->tcp_remote_v6 = *dstaddrp; 6742 tcp->tcp_ip6h->ip6_vcf = 6743 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6744 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6745 6746 6747 /* 6748 * Massage a routing header (if present) putting the first hop 6749 * in ip6_dst. Compute a starting value for the checksum which 6750 * takes into account that the original ip6_dst should be 6751 * included in the checksum but that ip will include the 6752 * first hop in the source route in the tcp checksum. 6753 */ 6754 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6755 if (rth != NULL) { 6756 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6757 tcps->tcps_netstack); 6758 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6759 (tcp->tcp_sum >> 16)); 6760 } else { 6761 tcp->tcp_sum = 0; 6762 } 6763 6764 tcph = tcp->tcp_tcph; 6765 *(uint16_t *)tcph->th_fport = dstport; 6766 tcp->tcp_fport = dstport; 6767 6768 oldstate = tcp->tcp_state; 6769 /* 6770 * At this point the remote destination address and remote port fields 6771 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6772 * have to see which state tcp was in so we can take apropriate action. 6773 */ 6774 if (oldstate == TCPS_IDLE) { 6775 /* 6776 * We support a quick connect capability here, allowing 6777 * clients to transition directly from IDLE to SYN_SENT 6778 * tcp_bindi will pick an unused port, insert the connection 6779 * in the bind hash and transition to BOUND state. 6780 */ 6781 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6782 tcp, B_TRUE); 6783 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6784 B_FALSE, B_FALSE); 6785 if (lport == 0) { 6786 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6787 goto failed; 6788 } 6789 } 6790 tcp->tcp_state = TCPS_SYN_SENT; 6791 /* 6792 * TODO: allow data with connect requests 6793 * by unlinking M_DATA trailers here and 6794 * linking them in behind the T_OK_ACK mblk. 6795 * The tcp_rput() bind ack handler would then 6796 * feed them to tcp_wput_data() rather than call 6797 * tcp_timer(). 6798 */ 6799 mp = mi_tpi_ok_ack_alloc(mp); 6800 if (!mp) { 6801 tcp->tcp_state = oldstate; 6802 goto failed; 6803 } 6804 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 6805 if (mp1) { 6806 /* 6807 * We need to make sure that the conn_recv is set to a non-null 6808 * value before we insert the conn_t into the classifier table. 6809 * This is to avoid a race with an incoming packet which does 6810 * an ipcl_classify(). 6811 */ 6812 tcp->tcp_connp->conn_recv = tcp_input; 6813 6814 /* Hang onto the T_OK_ACK for later. */ 6815 linkb(mp1, mp); 6816 mblk_setcred(mp1, tcp->tcp_cred); 6817 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6818 &tcp->tcp_sticky_ipp); 6819 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6820 tcp->tcp_active_open = 1; 6821 /* ip_bind_v6() may return ACK or ERROR */ 6822 if (mp1 != NULL) 6823 tcp_rput_other(tcp, mp1); 6824 return; 6825 } 6826 /* Error case */ 6827 tcp->tcp_state = oldstate; 6828 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6829 6830 failed: 6831 /* return error ack and blow away saved option results if any */ 6832 if (mp != NULL) 6833 putnext(tcp->tcp_rq, mp); 6834 else { 6835 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6836 TSYSERR, ENOMEM); 6837 } 6838 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6839 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6840 } 6841 6842 /* 6843 * We need a stream q for detached closing tcp connections 6844 * to use. Our client hereby indicates that this q is the 6845 * one to use. 6846 */ 6847 static void 6848 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6849 { 6850 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6851 queue_t *q = tcp->tcp_wq; 6852 tcp_stack_t *tcps = tcp->tcp_tcps; 6853 6854 #ifdef NS_DEBUG 6855 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6856 tcps->tcps_netstack->netstack_stackid); 6857 #endif 6858 mp->b_datap->db_type = M_IOCACK; 6859 iocp->ioc_count = 0; 6860 mutex_enter(&tcps->tcps_g_q_lock); 6861 if (tcps->tcps_g_q != NULL) { 6862 mutex_exit(&tcps->tcps_g_q_lock); 6863 iocp->ioc_error = EALREADY; 6864 } else { 6865 mblk_t *mp1; 6866 6867 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6868 if (mp1 == NULL) { 6869 mutex_exit(&tcps->tcps_g_q_lock); 6870 iocp->ioc_error = ENOMEM; 6871 } else { 6872 tcps->tcps_g_q = tcp->tcp_rq; 6873 mutex_exit(&tcps->tcps_g_q_lock); 6874 iocp->ioc_error = 0; 6875 iocp->ioc_rval = 0; 6876 /* 6877 * We are passing tcp_sticky_ipp as NULL 6878 * as it is not useful for tcp_default queue 6879 * 6880 * Set conn_recv just in case. 6881 */ 6882 tcp->tcp_connp->conn_recv = tcp_conn_request; 6883 6884 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6885 if (mp1 != NULL) 6886 tcp_rput_other(tcp, mp1); 6887 } 6888 } 6889 qreply(q, mp); 6890 } 6891 6892 /* 6893 * Our client hereby directs us to reject the connection request 6894 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6895 * of sending the appropriate RST, not an ICMP error. 6896 */ 6897 static void 6898 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6899 { 6900 tcp_t *ltcp = NULL; 6901 t_scalar_t seqnum; 6902 conn_t *connp; 6903 tcp_stack_t *tcps = tcp->tcp_tcps; 6904 6905 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6906 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6907 tcp_err_ack(tcp, mp, TPROTO, 0); 6908 return; 6909 } 6910 6911 /* 6912 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6913 * when the stream is in BOUND state. Do not send a reset, 6914 * since the destination IP address is not valid, and it can 6915 * be the initialized value of all zeros (broadcast address). 6916 * 6917 * If TCP has sent down a bind request to IP and has not 6918 * received the reply, reject the request. Otherwise, TCP 6919 * will be confused. 6920 */ 6921 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6922 if (tcp->tcp_debug) { 6923 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6924 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6925 } 6926 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6927 return; 6928 } 6929 6930 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6931 6932 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6933 6934 /* 6935 * According to TPI, for non-listeners, ignore seqnum 6936 * and disconnect. 6937 * Following interpretation of -1 seqnum is historical 6938 * and implied TPI ? (TPI only states that for T_CONN_IND, 6939 * a valid seqnum should not be -1). 6940 * 6941 * -1 means disconnect everything 6942 * regardless even on a listener. 6943 */ 6944 6945 int old_state = tcp->tcp_state; 6946 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6947 6948 /* 6949 * The connection can't be on the tcp_time_wait_head list 6950 * since it is not detached. 6951 */ 6952 ASSERT(tcp->tcp_time_wait_next == NULL); 6953 ASSERT(tcp->tcp_time_wait_prev == NULL); 6954 ASSERT(tcp->tcp_time_wait_expire == 0); 6955 ltcp = NULL; 6956 /* 6957 * If it used to be a listener, check to make sure no one else 6958 * has taken the port before switching back to LISTEN state. 6959 */ 6960 if (tcp->tcp_ipversion == IPV4_VERSION) { 6961 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6962 tcp->tcp_ipha->ipha_src, 6963 tcp->tcp_connp->conn_zoneid, ipst); 6964 if (connp != NULL) 6965 ltcp = connp->conn_tcp; 6966 } else { 6967 /* Allow tcp_bound_if listeners? */ 6968 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6969 &tcp->tcp_ip6h->ip6_src, 0, 6970 tcp->tcp_connp->conn_zoneid, ipst); 6971 if (connp != NULL) 6972 ltcp = connp->conn_tcp; 6973 } 6974 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6975 tcp->tcp_state = TCPS_LISTEN; 6976 } else if (old_state > TCPS_BOUND) { 6977 tcp->tcp_conn_req_max = 0; 6978 tcp->tcp_state = TCPS_BOUND; 6979 } 6980 if (ltcp != NULL) 6981 CONN_DEC_REF(ltcp->tcp_connp); 6982 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6983 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6984 } else if (old_state == TCPS_ESTABLISHED || 6985 old_state == TCPS_CLOSE_WAIT) { 6986 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6987 } 6988 6989 if (tcp->tcp_fused) 6990 tcp_unfuse(tcp); 6991 6992 mutex_enter(&tcp->tcp_eager_lock); 6993 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6994 (tcp->tcp_conn_req_cnt_q != 0)) { 6995 tcp_eager_cleanup(tcp, 0); 6996 } 6997 mutex_exit(&tcp->tcp_eager_lock); 6998 6999 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 7000 tcp->tcp_rnxt, TH_RST | TH_ACK); 7001 7002 tcp_reinit(tcp); 7003 7004 if (old_state >= TCPS_ESTABLISHED) { 7005 /* Send M_FLUSH according to TPI */ 7006 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 7007 } 7008 mp = mi_tpi_ok_ack_alloc(mp); 7009 if (mp) 7010 putnext(tcp->tcp_rq, mp); 7011 return; 7012 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 7013 tcp_err_ack(tcp, mp, TBADSEQ, 0); 7014 return; 7015 } 7016 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 7017 /* Send M_FLUSH according to TPI */ 7018 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 7019 } 7020 mp = mi_tpi_ok_ack_alloc(mp); 7021 if (mp) 7022 putnext(tcp->tcp_rq, mp); 7023 } 7024 7025 /* 7026 * Diagnostic routine used to return a string associated with the tcp state. 7027 * Note that if the caller does not supply a buffer, it will use an internal 7028 * static string. This means that if multiple threads call this function at 7029 * the same time, output can be corrupted... Note also that this function 7030 * does not check the size of the supplied buffer. The caller has to make 7031 * sure that it is big enough. 7032 */ 7033 static char * 7034 tcp_display(tcp_t *tcp, char *sup_buf, char format) 7035 { 7036 char buf1[30]; 7037 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 7038 char *buf; 7039 char *cp; 7040 in6_addr_t local, remote; 7041 char local_addrbuf[INET6_ADDRSTRLEN]; 7042 char remote_addrbuf[INET6_ADDRSTRLEN]; 7043 7044 if (sup_buf != NULL) 7045 buf = sup_buf; 7046 else 7047 buf = priv_buf; 7048 7049 if (tcp == NULL) 7050 return ("NULL_TCP"); 7051 switch (tcp->tcp_state) { 7052 case TCPS_CLOSED: 7053 cp = "TCP_CLOSED"; 7054 break; 7055 case TCPS_IDLE: 7056 cp = "TCP_IDLE"; 7057 break; 7058 case TCPS_BOUND: 7059 cp = "TCP_BOUND"; 7060 break; 7061 case TCPS_LISTEN: 7062 cp = "TCP_LISTEN"; 7063 break; 7064 case TCPS_SYN_SENT: 7065 cp = "TCP_SYN_SENT"; 7066 break; 7067 case TCPS_SYN_RCVD: 7068 cp = "TCP_SYN_RCVD"; 7069 break; 7070 case TCPS_ESTABLISHED: 7071 cp = "TCP_ESTABLISHED"; 7072 break; 7073 case TCPS_CLOSE_WAIT: 7074 cp = "TCP_CLOSE_WAIT"; 7075 break; 7076 case TCPS_FIN_WAIT_1: 7077 cp = "TCP_FIN_WAIT_1"; 7078 break; 7079 case TCPS_CLOSING: 7080 cp = "TCP_CLOSING"; 7081 break; 7082 case TCPS_LAST_ACK: 7083 cp = "TCP_LAST_ACK"; 7084 break; 7085 case TCPS_FIN_WAIT_2: 7086 cp = "TCP_FIN_WAIT_2"; 7087 break; 7088 case TCPS_TIME_WAIT: 7089 cp = "TCP_TIME_WAIT"; 7090 break; 7091 default: 7092 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 7093 cp = buf1; 7094 break; 7095 } 7096 switch (format) { 7097 case DISP_ADDR_AND_PORT: 7098 if (tcp->tcp_ipversion == IPV4_VERSION) { 7099 /* 7100 * Note that we use the remote address in the tcp_b 7101 * structure. This means that it will print out 7102 * the real destination address, not the next hop's 7103 * address if source routing is used. 7104 */ 7105 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 7106 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 7107 7108 } else { 7109 local = tcp->tcp_ip_src_v6; 7110 remote = tcp->tcp_remote_v6; 7111 } 7112 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 7113 sizeof (local_addrbuf)); 7114 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 7115 sizeof (remote_addrbuf)); 7116 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 7117 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 7118 ntohs(tcp->tcp_fport), cp); 7119 break; 7120 case DISP_PORT_ONLY: 7121 default: 7122 (void) mi_sprintf(buf, "[%u, %u] %s", 7123 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 7124 break; 7125 } 7126 7127 return (buf); 7128 } 7129 7130 /* 7131 * Called via squeue to get on to eager's perimeter. It sends a 7132 * TH_RST if eager is in the fanout table. The listener wants the 7133 * eager to disappear either by means of tcp_eager_blowoff() or 7134 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 7135 * called (via squeue) if the eager cannot be inserted in the 7136 * fanout table in tcp_conn_request(). 7137 */ 7138 /* ARGSUSED */ 7139 void 7140 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 7141 { 7142 conn_t *econnp = (conn_t *)arg; 7143 tcp_t *eager = econnp->conn_tcp; 7144 tcp_t *listener = eager->tcp_listener; 7145 tcp_stack_t *tcps = eager->tcp_tcps; 7146 7147 /* 7148 * We could be called because listener is closing. Since 7149 * the eager is using listener's queue's, its not safe. 7150 * Better use the default queue just to send the TH_RST 7151 * out. 7152 */ 7153 ASSERT(tcps->tcps_g_q != NULL); 7154 eager->tcp_rq = tcps->tcps_g_q; 7155 eager->tcp_wq = WR(tcps->tcps_g_q); 7156 7157 /* 7158 * An eager's conn_fanout will be NULL if it's a duplicate 7159 * for an existing 4-tuples in the conn fanout table. 7160 * We don't want to send an RST out in such case. 7161 */ 7162 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 7163 tcp_xmit_ctl("tcp_eager_kill, can't wait", 7164 eager, eager->tcp_snxt, 0, TH_RST); 7165 } 7166 7167 /* We are here because listener wants this eager gone */ 7168 if (listener != NULL) { 7169 mutex_enter(&listener->tcp_eager_lock); 7170 tcp_eager_unlink(eager); 7171 if (eager->tcp_tconnind_started) { 7172 /* 7173 * The eager has sent a conn_ind up to the 7174 * listener but listener decides to close 7175 * instead. We need to drop the extra ref 7176 * placed on eager in tcp_rput_data() before 7177 * sending the conn_ind to listener. 7178 */ 7179 CONN_DEC_REF(econnp); 7180 } 7181 mutex_exit(&listener->tcp_eager_lock); 7182 CONN_DEC_REF(listener->tcp_connp); 7183 } 7184 7185 if (eager->tcp_state > TCPS_BOUND) 7186 tcp_close_detached(eager); 7187 } 7188 7189 /* 7190 * Reset any eager connection hanging off this listener marked 7191 * with 'seqnum' and then reclaim it's resources. 7192 */ 7193 static boolean_t 7194 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 7195 { 7196 tcp_t *eager; 7197 mblk_t *mp; 7198 tcp_stack_t *tcps = listener->tcp_tcps; 7199 7200 TCP_STAT(tcps, tcp_eager_blowoff_calls); 7201 eager = listener; 7202 mutex_enter(&listener->tcp_eager_lock); 7203 do { 7204 eager = eager->tcp_eager_next_q; 7205 if (eager == NULL) { 7206 mutex_exit(&listener->tcp_eager_lock); 7207 return (B_FALSE); 7208 } 7209 } while (eager->tcp_conn_req_seqnum != seqnum); 7210 7211 if (eager->tcp_closemp_used) { 7212 mutex_exit(&listener->tcp_eager_lock); 7213 return (B_TRUE); 7214 } 7215 eager->tcp_closemp_used = B_TRUE; 7216 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7217 CONN_INC_REF(eager->tcp_connp); 7218 mutex_exit(&listener->tcp_eager_lock); 7219 mp = &eager->tcp_closemp; 7220 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 7221 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 7222 return (B_TRUE); 7223 } 7224 7225 /* 7226 * Reset any eager connection hanging off this listener 7227 * and then reclaim it's resources. 7228 */ 7229 static void 7230 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 7231 { 7232 tcp_t *eager; 7233 mblk_t *mp; 7234 tcp_stack_t *tcps = listener->tcp_tcps; 7235 7236 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7237 7238 if (!q0_only) { 7239 /* First cleanup q */ 7240 TCP_STAT(tcps, tcp_eager_blowoff_q); 7241 eager = listener->tcp_eager_next_q; 7242 while (eager != NULL) { 7243 if (!eager->tcp_closemp_used) { 7244 eager->tcp_closemp_used = B_TRUE; 7245 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7246 CONN_INC_REF(eager->tcp_connp); 7247 mp = &eager->tcp_closemp; 7248 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7249 tcp_eager_kill, eager->tcp_connp, 7250 SQTAG_TCP_EAGER_CLEANUP); 7251 } 7252 eager = eager->tcp_eager_next_q; 7253 } 7254 } 7255 /* Then cleanup q0 */ 7256 TCP_STAT(tcps, tcp_eager_blowoff_q0); 7257 eager = listener->tcp_eager_next_q0; 7258 while (eager != listener) { 7259 if (!eager->tcp_closemp_used) { 7260 eager->tcp_closemp_used = B_TRUE; 7261 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7262 CONN_INC_REF(eager->tcp_connp); 7263 mp = &eager->tcp_closemp; 7264 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7265 tcp_eager_kill, eager->tcp_connp, 7266 SQTAG_TCP_EAGER_CLEANUP_Q0); 7267 } 7268 eager = eager->tcp_eager_next_q0; 7269 } 7270 } 7271 7272 /* 7273 * If we are an eager connection hanging off a listener that hasn't 7274 * formally accepted the connection yet, get off his list and blow off 7275 * any data that we have accumulated. 7276 */ 7277 static void 7278 tcp_eager_unlink(tcp_t *tcp) 7279 { 7280 tcp_t *listener = tcp->tcp_listener; 7281 7282 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7283 ASSERT(listener != NULL); 7284 if (tcp->tcp_eager_next_q0 != NULL) { 7285 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 7286 7287 /* Remove the eager tcp from q0 */ 7288 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 7289 tcp->tcp_eager_prev_q0; 7290 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 7291 tcp->tcp_eager_next_q0; 7292 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 7293 listener->tcp_conn_req_cnt_q0--; 7294 7295 tcp->tcp_eager_next_q0 = NULL; 7296 tcp->tcp_eager_prev_q0 = NULL; 7297 7298 /* 7299 * Take the eager out, if it is in the list of droppable 7300 * eagers. 7301 */ 7302 MAKE_UNDROPPABLE(tcp); 7303 7304 if (tcp->tcp_syn_rcvd_timeout != 0) { 7305 /* we have timed out before */ 7306 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 7307 listener->tcp_syn_rcvd_timeout--; 7308 } 7309 } else { 7310 tcp_t **tcpp = &listener->tcp_eager_next_q; 7311 tcp_t *prev = NULL; 7312 7313 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 7314 if (tcpp[0] == tcp) { 7315 if (listener->tcp_eager_last_q == tcp) { 7316 /* 7317 * If we are unlinking the last 7318 * element on the list, adjust 7319 * tail pointer. Set tail pointer 7320 * to nil when list is empty. 7321 */ 7322 ASSERT(tcp->tcp_eager_next_q == NULL); 7323 if (listener->tcp_eager_last_q == 7324 listener->tcp_eager_next_q) { 7325 listener->tcp_eager_last_q = 7326 NULL; 7327 } else { 7328 /* 7329 * We won't get here if there 7330 * is only one eager in the 7331 * list. 7332 */ 7333 ASSERT(prev != NULL); 7334 listener->tcp_eager_last_q = 7335 prev; 7336 } 7337 } 7338 tcpp[0] = tcp->tcp_eager_next_q; 7339 tcp->tcp_eager_next_q = NULL; 7340 tcp->tcp_eager_last_q = NULL; 7341 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7342 listener->tcp_conn_req_cnt_q--; 7343 break; 7344 } 7345 prev = tcpp[0]; 7346 } 7347 } 7348 tcp->tcp_listener = NULL; 7349 } 7350 7351 /* Shorthand to generate and send TPI error acks to our client */ 7352 static void 7353 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7354 { 7355 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7356 putnext(tcp->tcp_rq, mp); 7357 } 7358 7359 /* Shorthand to generate and send TPI error acks to our client */ 7360 static void 7361 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7362 int t_error, int sys_error) 7363 { 7364 struct T_error_ack *teackp; 7365 7366 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7367 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7368 teackp = (struct T_error_ack *)mp->b_rptr; 7369 teackp->ERROR_prim = primitive; 7370 teackp->TLI_error = t_error; 7371 teackp->UNIX_error = sys_error; 7372 putnext(tcp->tcp_rq, mp); 7373 } 7374 } 7375 7376 /* 7377 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7378 * but instead the code relies on: 7379 * - the fact that the address of the array and its size never changes 7380 * - the atomic assignment of the elements of the array 7381 */ 7382 /* ARGSUSED */ 7383 static int 7384 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7385 { 7386 int i; 7387 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7388 7389 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7390 if (tcps->tcps_g_epriv_ports[i] != 0) 7391 (void) mi_mpprintf(mp, "%d ", 7392 tcps->tcps_g_epriv_ports[i]); 7393 } 7394 return (0); 7395 } 7396 7397 /* 7398 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7399 * threads from changing it at the same time. 7400 */ 7401 /* ARGSUSED */ 7402 static int 7403 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7404 cred_t *cr) 7405 { 7406 long new_value; 7407 int i; 7408 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7409 7410 /* 7411 * Fail the request if the new value does not lie within the 7412 * port number limits. 7413 */ 7414 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7415 new_value <= 0 || new_value >= 65536) { 7416 return (EINVAL); 7417 } 7418 7419 mutex_enter(&tcps->tcps_epriv_port_lock); 7420 /* Check if the value is already in the list */ 7421 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7422 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7423 mutex_exit(&tcps->tcps_epriv_port_lock); 7424 return (EEXIST); 7425 } 7426 } 7427 /* Find an empty slot */ 7428 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7429 if (tcps->tcps_g_epriv_ports[i] == 0) 7430 break; 7431 } 7432 if (i == tcps->tcps_g_num_epriv_ports) { 7433 mutex_exit(&tcps->tcps_epriv_port_lock); 7434 return (EOVERFLOW); 7435 } 7436 /* Set the new value */ 7437 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7438 mutex_exit(&tcps->tcps_epriv_port_lock); 7439 return (0); 7440 } 7441 7442 /* 7443 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7444 * threads from changing it at the same time. 7445 */ 7446 /* ARGSUSED */ 7447 static int 7448 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7449 cred_t *cr) 7450 { 7451 long new_value; 7452 int i; 7453 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7454 7455 /* 7456 * Fail the request if the new value does not lie within the 7457 * port number limits. 7458 */ 7459 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7460 new_value >= 65536) { 7461 return (EINVAL); 7462 } 7463 7464 mutex_enter(&tcps->tcps_epriv_port_lock); 7465 /* Check that the value is already in the list */ 7466 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7467 if (tcps->tcps_g_epriv_ports[i] == new_value) 7468 break; 7469 } 7470 if (i == tcps->tcps_g_num_epriv_ports) { 7471 mutex_exit(&tcps->tcps_epriv_port_lock); 7472 return (ESRCH); 7473 } 7474 /* Clear the value */ 7475 tcps->tcps_g_epriv_ports[i] = 0; 7476 mutex_exit(&tcps->tcps_epriv_port_lock); 7477 return (0); 7478 } 7479 7480 /* Return the TPI/TLI equivalent of our current tcp_state */ 7481 static int 7482 tcp_tpistate(tcp_t *tcp) 7483 { 7484 switch (tcp->tcp_state) { 7485 case TCPS_IDLE: 7486 return (TS_UNBND); 7487 case TCPS_LISTEN: 7488 /* 7489 * Return whether there are outstanding T_CONN_IND waiting 7490 * for the matching T_CONN_RES. Therefore don't count q0. 7491 */ 7492 if (tcp->tcp_conn_req_cnt_q > 0) 7493 return (TS_WRES_CIND); 7494 else 7495 return (TS_IDLE); 7496 case TCPS_BOUND: 7497 return (TS_IDLE); 7498 case TCPS_SYN_SENT: 7499 return (TS_WCON_CREQ); 7500 case TCPS_SYN_RCVD: 7501 /* 7502 * Note: assumption: this has to the active open SYN_RCVD. 7503 * The passive instance is detached in SYN_RCVD stage of 7504 * incoming connection processing so we cannot get request 7505 * for T_info_ack on it. 7506 */ 7507 return (TS_WACK_CRES); 7508 case TCPS_ESTABLISHED: 7509 return (TS_DATA_XFER); 7510 case TCPS_CLOSE_WAIT: 7511 return (TS_WREQ_ORDREL); 7512 case TCPS_FIN_WAIT_1: 7513 return (TS_WIND_ORDREL); 7514 case TCPS_FIN_WAIT_2: 7515 return (TS_WIND_ORDREL); 7516 7517 case TCPS_CLOSING: 7518 case TCPS_LAST_ACK: 7519 case TCPS_TIME_WAIT: 7520 case TCPS_CLOSED: 7521 /* 7522 * Following TS_WACK_DREQ7 is a rendition of "not 7523 * yet TS_IDLE" TPI state. There is no best match to any 7524 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7525 * choose a value chosen that will map to TLI/XTI level 7526 * state of TSTATECHNG (state is process of changing) which 7527 * captures what this dummy state represents. 7528 */ 7529 return (TS_WACK_DREQ7); 7530 default: 7531 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7532 tcp->tcp_state, tcp_display(tcp, NULL, 7533 DISP_PORT_ONLY)); 7534 return (TS_UNBND); 7535 } 7536 } 7537 7538 static void 7539 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7540 { 7541 tcp_stack_t *tcps = tcp->tcp_tcps; 7542 7543 if (tcp->tcp_family == AF_INET6) 7544 *tia = tcp_g_t_info_ack_v6; 7545 else 7546 *tia = tcp_g_t_info_ack; 7547 tia->CURRENT_state = tcp_tpistate(tcp); 7548 tia->OPT_size = tcp_max_optsize; 7549 if (tcp->tcp_mss == 0) { 7550 /* Not yet set - tcp_open does not set mss */ 7551 if (tcp->tcp_ipversion == IPV4_VERSION) 7552 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7553 else 7554 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7555 } else { 7556 tia->TIDU_size = tcp->tcp_mss; 7557 } 7558 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7559 } 7560 7561 /* 7562 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7563 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7564 * tcp_g_t_info_ack. The current state of the stream is copied from 7565 * tcp_state. 7566 */ 7567 static void 7568 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7569 { 7570 t_uscalar_t cap_bits1; 7571 struct T_capability_ack *tcap; 7572 7573 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7574 freemsg(mp); 7575 return; 7576 } 7577 7578 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7579 7580 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7581 mp->b_datap->db_type, T_CAPABILITY_ACK); 7582 if (mp == NULL) 7583 return; 7584 7585 tcap = (struct T_capability_ack *)mp->b_rptr; 7586 tcap->CAP_bits1 = 0; 7587 7588 if (cap_bits1 & TC1_INFO) { 7589 tcp_copy_info(&tcap->INFO_ack, tcp); 7590 tcap->CAP_bits1 |= TC1_INFO; 7591 } 7592 7593 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7594 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7595 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7596 } 7597 7598 putnext(tcp->tcp_rq, mp); 7599 } 7600 7601 /* 7602 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7603 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7604 * The current state of the stream is copied from tcp_state. 7605 */ 7606 static void 7607 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7608 { 7609 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7610 T_INFO_ACK); 7611 if (!mp) { 7612 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7613 return; 7614 } 7615 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7616 putnext(tcp->tcp_rq, mp); 7617 } 7618 7619 /* Respond to the TPI addr request */ 7620 static void 7621 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7622 { 7623 sin_t *sin; 7624 mblk_t *ackmp; 7625 struct T_addr_ack *taa; 7626 7627 /* Make it large enough for worst case */ 7628 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7629 2 * sizeof (sin6_t), 1); 7630 if (ackmp == NULL) { 7631 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7632 return; 7633 } 7634 7635 if (tcp->tcp_ipversion == IPV6_VERSION) { 7636 tcp_addr_req_ipv6(tcp, ackmp); 7637 return; 7638 } 7639 taa = (struct T_addr_ack *)ackmp->b_rptr; 7640 7641 bzero(taa, sizeof (struct T_addr_ack)); 7642 ackmp->b_wptr = (uchar_t *)&taa[1]; 7643 7644 taa->PRIM_type = T_ADDR_ACK; 7645 ackmp->b_datap->db_type = M_PCPROTO; 7646 7647 /* 7648 * Note: Following code assumes 32 bit alignment of basic 7649 * data structures like sin_t and struct T_addr_ack. 7650 */ 7651 if (tcp->tcp_state >= TCPS_BOUND) { 7652 /* 7653 * Fill in local address 7654 */ 7655 taa->LOCADDR_length = sizeof (sin_t); 7656 taa->LOCADDR_offset = sizeof (*taa); 7657 7658 sin = (sin_t *)&taa[1]; 7659 7660 /* Fill zeroes and then intialize non-zero fields */ 7661 *sin = sin_null; 7662 7663 sin->sin_family = AF_INET; 7664 7665 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7666 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7667 7668 ackmp->b_wptr = (uchar_t *)&sin[1]; 7669 7670 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7671 /* 7672 * Fill in Remote address 7673 */ 7674 taa->REMADDR_length = sizeof (sin_t); 7675 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7676 taa->LOCADDR_length); 7677 7678 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7679 *sin = sin_null; 7680 sin->sin_family = AF_INET; 7681 sin->sin_addr.s_addr = tcp->tcp_remote; 7682 sin->sin_port = tcp->tcp_fport; 7683 7684 ackmp->b_wptr = (uchar_t *)&sin[1]; 7685 } 7686 } 7687 putnext(tcp->tcp_rq, ackmp); 7688 } 7689 7690 /* Assumes that tcp_addr_req gets enough space and alignment */ 7691 static void 7692 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7693 { 7694 sin6_t *sin6; 7695 struct T_addr_ack *taa; 7696 7697 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7698 ASSERT(OK_32PTR(ackmp->b_rptr)); 7699 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7700 2 * sizeof (sin6_t)); 7701 7702 taa = (struct T_addr_ack *)ackmp->b_rptr; 7703 7704 bzero(taa, sizeof (struct T_addr_ack)); 7705 ackmp->b_wptr = (uchar_t *)&taa[1]; 7706 7707 taa->PRIM_type = T_ADDR_ACK; 7708 ackmp->b_datap->db_type = M_PCPROTO; 7709 7710 /* 7711 * Note: Following code assumes 32 bit alignment of basic 7712 * data structures like sin6_t and struct T_addr_ack. 7713 */ 7714 if (tcp->tcp_state >= TCPS_BOUND) { 7715 /* 7716 * Fill in local address 7717 */ 7718 taa->LOCADDR_length = sizeof (sin6_t); 7719 taa->LOCADDR_offset = sizeof (*taa); 7720 7721 sin6 = (sin6_t *)&taa[1]; 7722 *sin6 = sin6_null; 7723 7724 sin6->sin6_family = AF_INET6; 7725 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7726 sin6->sin6_port = tcp->tcp_lport; 7727 7728 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7729 7730 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7731 /* 7732 * Fill in Remote address 7733 */ 7734 taa->REMADDR_length = sizeof (sin6_t); 7735 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7736 taa->LOCADDR_length); 7737 7738 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7739 *sin6 = sin6_null; 7740 sin6->sin6_family = AF_INET6; 7741 sin6->sin6_flowinfo = 7742 tcp->tcp_ip6h->ip6_vcf & 7743 ~IPV6_VERS_AND_FLOW_MASK; 7744 sin6->sin6_addr = tcp->tcp_remote_v6; 7745 sin6->sin6_port = tcp->tcp_fport; 7746 7747 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7748 } 7749 } 7750 putnext(tcp->tcp_rq, ackmp); 7751 } 7752 7753 /* 7754 * Handle reinitialization of a tcp structure. 7755 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7756 */ 7757 static void 7758 tcp_reinit(tcp_t *tcp) 7759 { 7760 mblk_t *mp; 7761 int err; 7762 tcp_stack_t *tcps = tcp->tcp_tcps; 7763 7764 TCP_STAT(tcps, tcp_reinit_calls); 7765 7766 /* tcp_reinit should never be called for detached tcp_t's */ 7767 ASSERT(tcp->tcp_listener == NULL); 7768 ASSERT((tcp->tcp_family == AF_INET && 7769 tcp->tcp_ipversion == IPV4_VERSION) || 7770 (tcp->tcp_family == AF_INET6 && 7771 (tcp->tcp_ipversion == IPV4_VERSION || 7772 tcp->tcp_ipversion == IPV6_VERSION))); 7773 7774 /* Cancel outstanding timers */ 7775 tcp_timers_stop(tcp); 7776 7777 /* 7778 * Reset everything in the state vector, after updating global 7779 * MIB data from instance counters. 7780 */ 7781 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7782 tcp->tcp_ibsegs = 0; 7783 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7784 tcp->tcp_obsegs = 0; 7785 7786 tcp_close_mpp(&tcp->tcp_xmit_head); 7787 if (tcp->tcp_snd_zcopy_aware) 7788 tcp_zcopy_notify(tcp); 7789 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7790 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7791 mutex_enter(&tcp->tcp_non_sq_lock); 7792 if (tcp->tcp_flow_stopped && 7793 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7794 tcp_clrqfull(tcp); 7795 } 7796 mutex_exit(&tcp->tcp_non_sq_lock); 7797 tcp_close_mpp(&tcp->tcp_reass_head); 7798 tcp->tcp_reass_tail = NULL; 7799 if (tcp->tcp_rcv_list != NULL) { 7800 /* Free b_next chain */ 7801 tcp_close_mpp(&tcp->tcp_rcv_list); 7802 tcp->tcp_rcv_last_head = NULL; 7803 tcp->tcp_rcv_last_tail = NULL; 7804 tcp->tcp_rcv_cnt = 0; 7805 } 7806 tcp->tcp_rcv_last_tail = NULL; 7807 7808 if ((mp = tcp->tcp_urp_mp) != NULL) { 7809 freemsg(mp); 7810 tcp->tcp_urp_mp = NULL; 7811 } 7812 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7813 freemsg(mp); 7814 tcp->tcp_urp_mark_mp = NULL; 7815 } 7816 if (tcp->tcp_fused_sigurg_mp != NULL) { 7817 freeb(tcp->tcp_fused_sigurg_mp); 7818 tcp->tcp_fused_sigurg_mp = NULL; 7819 } 7820 if (tcp->tcp_ordrel_mp != NULL) { 7821 freeb(tcp->tcp_ordrel_mp); 7822 tcp->tcp_ordrel_mp = NULL; 7823 } 7824 7825 /* 7826 * Following is a union with two members which are 7827 * identical types and size so the following cleanup 7828 * is enough. 7829 */ 7830 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7831 7832 CL_INET_DISCONNECT(tcp); 7833 7834 /* 7835 * The connection can't be on the tcp_time_wait_head list 7836 * since it is not detached. 7837 */ 7838 ASSERT(tcp->tcp_time_wait_next == NULL); 7839 ASSERT(tcp->tcp_time_wait_prev == NULL); 7840 ASSERT(tcp->tcp_time_wait_expire == 0); 7841 7842 if (tcp->tcp_kssl_pending) { 7843 tcp->tcp_kssl_pending = B_FALSE; 7844 7845 /* Don't reset if the initialized by bind. */ 7846 if (tcp->tcp_kssl_ent != NULL) { 7847 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7848 KSSL_NO_PROXY); 7849 } 7850 } 7851 if (tcp->tcp_kssl_ctx != NULL) { 7852 kssl_release_ctx(tcp->tcp_kssl_ctx); 7853 tcp->tcp_kssl_ctx = NULL; 7854 } 7855 7856 /* 7857 * Reset/preserve other values 7858 */ 7859 tcp_reinit_values(tcp); 7860 ipcl_hash_remove(tcp->tcp_connp); 7861 conn_delete_ire(tcp->tcp_connp, NULL); 7862 tcp_ipsec_cleanup(tcp); 7863 7864 if (tcp->tcp_conn_req_max != 0) { 7865 /* 7866 * This is the case when a TLI program uses the same 7867 * transport end point to accept a connection. This 7868 * makes the TCP both a listener and acceptor. When 7869 * this connection is closed, we need to set the state 7870 * back to TCPS_LISTEN. Make sure that the eager list 7871 * is reinitialized. 7872 * 7873 * Note that this stream is still bound to the four 7874 * tuples of the previous connection in IP. If a new 7875 * SYN with different foreign address comes in, IP will 7876 * not find it and will send it to the global queue. In 7877 * the global queue, TCP will do a tcp_lookup_listener() 7878 * to find this stream. This works because this stream 7879 * is only removed from connected hash. 7880 * 7881 */ 7882 tcp->tcp_state = TCPS_LISTEN; 7883 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7884 tcp->tcp_eager_next_drop_q0 = tcp; 7885 tcp->tcp_eager_prev_drop_q0 = tcp; 7886 tcp->tcp_connp->conn_recv = tcp_conn_request; 7887 if (tcp->tcp_family == AF_INET6) { 7888 ASSERT(tcp->tcp_connp->conn_af_isv6); 7889 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7890 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7891 } else { 7892 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7893 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7894 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7895 } 7896 } else { 7897 tcp->tcp_state = TCPS_BOUND; 7898 } 7899 7900 /* 7901 * Initialize to default values 7902 * Can't fail since enough header template space already allocated 7903 * at open(). 7904 */ 7905 err = tcp_init_values(tcp); 7906 ASSERT(err == 0); 7907 /* Restore state in tcp_tcph */ 7908 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7909 if (tcp->tcp_ipversion == IPV4_VERSION) 7910 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7911 else 7912 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7913 /* 7914 * Copy of the src addr. in tcp_t is needed in tcp_t 7915 * since the lookup funcs can only lookup on tcp_t 7916 */ 7917 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7918 7919 ASSERT(tcp->tcp_ptpbhn != NULL); 7920 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7921 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7922 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7923 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7924 } 7925 7926 /* 7927 * Force values to zero that need be zero. 7928 * Do not touch values asociated with the BOUND or LISTEN state 7929 * since the connection will end up in that state after the reinit. 7930 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7931 * structure! 7932 */ 7933 static void 7934 tcp_reinit_values(tcp) 7935 tcp_t *tcp; 7936 { 7937 tcp_stack_t *tcps = tcp->tcp_tcps; 7938 7939 #ifndef lint 7940 #define DONTCARE(x) 7941 #define PRESERVE(x) 7942 #else 7943 #define DONTCARE(x) ((x) = (x)) 7944 #define PRESERVE(x) ((x) = (x)) 7945 #endif /* lint */ 7946 7947 PRESERVE(tcp->tcp_bind_hash); 7948 PRESERVE(tcp->tcp_ptpbhn); 7949 PRESERVE(tcp->tcp_acceptor_hash); 7950 PRESERVE(tcp->tcp_ptpahn); 7951 7952 /* Should be ASSERT NULL on these with new code! */ 7953 ASSERT(tcp->tcp_time_wait_next == NULL); 7954 ASSERT(tcp->tcp_time_wait_prev == NULL); 7955 ASSERT(tcp->tcp_time_wait_expire == 0); 7956 PRESERVE(tcp->tcp_state); 7957 PRESERVE(tcp->tcp_rq); 7958 PRESERVE(tcp->tcp_wq); 7959 7960 ASSERT(tcp->tcp_xmit_head == NULL); 7961 ASSERT(tcp->tcp_xmit_last == NULL); 7962 ASSERT(tcp->tcp_unsent == 0); 7963 ASSERT(tcp->tcp_xmit_tail == NULL); 7964 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7965 7966 tcp->tcp_snxt = 0; /* Displayed in mib */ 7967 tcp->tcp_suna = 0; /* Displayed in mib */ 7968 tcp->tcp_swnd = 0; 7969 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7970 7971 ASSERT(tcp->tcp_ibsegs == 0); 7972 ASSERT(tcp->tcp_obsegs == 0); 7973 7974 if (tcp->tcp_iphc != NULL) { 7975 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7976 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7977 } 7978 7979 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7980 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7981 DONTCARE(tcp->tcp_ipha); 7982 DONTCARE(tcp->tcp_ip6h); 7983 DONTCARE(tcp->tcp_ip_hdr_len); 7984 DONTCARE(tcp->tcp_tcph); 7985 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7986 tcp->tcp_valid_bits = 0; 7987 7988 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7989 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7990 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7991 tcp->tcp_last_rcv_lbolt = 0; 7992 7993 tcp->tcp_init_cwnd = 0; 7994 7995 tcp->tcp_urp_last_valid = 0; 7996 tcp->tcp_hard_binding = 0; 7997 tcp->tcp_hard_bound = 0; 7998 PRESERVE(tcp->tcp_cred); 7999 PRESERVE(tcp->tcp_cpid); 8000 PRESERVE(tcp->tcp_open_time); 8001 PRESERVE(tcp->tcp_exclbind); 8002 8003 tcp->tcp_fin_acked = 0; 8004 tcp->tcp_fin_rcvd = 0; 8005 tcp->tcp_fin_sent = 0; 8006 tcp->tcp_ordrel_done = 0; 8007 8008 tcp->tcp_debug = 0; 8009 tcp->tcp_dontroute = 0; 8010 tcp->tcp_broadcast = 0; 8011 8012 tcp->tcp_useloopback = 0; 8013 tcp->tcp_reuseaddr = 0; 8014 tcp->tcp_oobinline = 0; 8015 tcp->tcp_dgram_errind = 0; 8016 8017 tcp->tcp_detached = 0; 8018 tcp->tcp_bind_pending = 0; 8019 tcp->tcp_unbind_pending = 0; 8020 8021 tcp->tcp_snd_ws_ok = B_FALSE; 8022 tcp->tcp_snd_ts_ok = B_FALSE; 8023 tcp->tcp_linger = 0; 8024 tcp->tcp_ka_enabled = 0; 8025 tcp->tcp_zero_win_probe = 0; 8026 8027 tcp->tcp_loopback = 0; 8028 tcp->tcp_refuse = 0; 8029 tcp->tcp_localnet = 0; 8030 tcp->tcp_syn_defense = 0; 8031 tcp->tcp_set_timer = 0; 8032 8033 tcp->tcp_active_open = 0; 8034 tcp->tcp_rexmit = B_FALSE; 8035 tcp->tcp_xmit_zc_clean = B_FALSE; 8036 8037 tcp->tcp_snd_sack_ok = B_FALSE; 8038 PRESERVE(tcp->tcp_recvdstaddr); 8039 tcp->tcp_hwcksum = B_FALSE; 8040 8041 tcp->tcp_ire_ill_check_done = B_FALSE; 8042 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 8043 8044 tcp->tcp_mdt = B_FALSE; 8045 tcp->tcp_mdt_hdr_head = 0; 8046 tcp->tcp_mdt_hdr_tail = 0; 8047 8048 tcp->tcp_conn_def_q0 = 0; 8049 tcp->tcp_ip_forward_progress = B_FALSE; 8050 tcp->tcp_anon_priv_bind = 0; 8051 tcp->tcp_ecn_ok = B_FALSE; 8052 8053 tcp->tcp_cwr = B_FALSE; 8054 tcp->tcp_ecn_echo_on = B_FALSE; 8055 8056 if (tcp->tcp_sack_info != NULL) { 8057 if (tcp->tcp_notsack_list != NULL) { 8058 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 8059 } 8060 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 8061 tcp->tcp_sack_info = NULL; 8062 } 8063 8064 tcp->tcp_rcv_ws = 0; 8065 tcp->tcp_snd_ws = 0; 8066 tcp->tcp_ts_recent = 0; 8067 tcp->tcp_rnxt = 0; /* Displayed in mib */ 8068 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 8069 tcp->tcp_if_mtu = 0; 8070 8071 ASSERT(tcp->tcp_reass_head == NULL); 8072 ASSERT(tcp->tcp_reass_tail == NULL); 8073 8074 tcp->tcp_cwnd_cnt = 0; 8075 8076 ASSERT(tcp->tcp_rcv_list == NULL); 8077 ASSERT(tcp->tcp_rcv_last_head == NULL); 8078 ASSERT(tcp->tcp_rcv_last_tail == NULL); 8079 ASSERT(tcp->tcp_rcv_cnt == 0); 8080 8081 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 8082 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 8083 tcp->tcp_csuna = 0; 8084 8085 tcp->tcp_rto = 0; /* Displayed in MIB */ 8086 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 8087 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 8088 tcp->tcp_rtt_update = 0; 8089 8090 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8091 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8092 8093 tcp->tcp_rack = 0; /* Displayed in mib */ 8094 tcp->tcp_rack_cnt = 0; 8095 tcp->tcp_rack_cur_max = 0; 8096 tcp->tcp_rack_abs_max = 0; 8097 8098 tcp->tcp_max_swnd = 0; 8099 8100 ASSERT(tcp->tcp_listener == NULL); 8101 8102 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 8103 8104 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 8105 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 8106 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 8107 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 8108 8109 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 8110 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 8111 PRESERVE(tcp->tcp_conn_req_max); 8112 PRESERVE(tcp->tcp_conn_req_seqnum); 8113 8114 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 8115 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 8116 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 8117 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 8118 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 8119 8120 tcp->tcp_lingertime = 0; 8121 8122 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 8123 ASSERT(tcp->tcp_urp_mp == NULL); 8124 ASSERT(tcp->tcp_urp_mark_mp == NULL); 8125 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 8126 8127 ASSERT(tcp->tcp_eager_next_q == NULL); 8128 ASSERT(tcp->tcp_eager_last_q == NULL); 8129 ASSERT((tcp->tcp_eager_next_q0 == NULL && 8130 tcp->tcp_eager_prev_q0 == NULL) || 8131 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 8132 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 8133 8134 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 8135 tcp->tcp_eager_prev_drop_q0 == NULL) || 8136 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 8137 8138 tcp->tcp_client_errno = 0; 8139 8140 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 8141 8142 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 8143 8144 PRESERVE(tcp->tcp_bound_source_v6); 8145 tcp->tcp_last_sent_len = 0; 8146 tcp->tcp_dupack_cnt = 0; 8147 8148 tcp->tcp_fport = 0; /* Displayed in MIB */ 8149 PRESERVE(tcp->tcp_lport); 8150 8151 PRESERVE(tcp->tcp_acceptor_lockp); 8152 8153 ASSERT(tcp->tcp_ordrel_mp == NULL); 8154 PRESERVE(tcp->tcp_acceptor_id); 8155 DONTCARE(tcp->tcp_ipsec_overhead); 8156 8157 PRESERVE(tcp->tcp_family); 8158 if (tcp->tcp_family == AF_INET6) { 8159 tcp->tcp_ipversion = IPV6_VERSION; 8160 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 8161 } else { 8162 tcp->tcp_ipversion = IPV4_VERSION; 8163 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 8164 } 8165 8166 tcp->tcp_bound_if = 0; 8167 tcp->tcp_ipv6_recvancillary = 0; 8168 tcp->tcp_recvifindex = 0; 8169 tcp->tcp_recvhops = 0; 8170 tcp->tcp_closed = 0; 8171 tcp->tcp_cleandeathtag = 0; 8172 if (tcp->tcp_hopopts != NULL) { 8173 mi_free(tcp->tcp_hopopts); 8174 tcp->tcp_hopopts = NULL; 8175 tcp->tcp_hopoptslen = 0; 8176 } 8177 ASSERT(tcp->tcp_hopoptslen == 0); 8178 if (tcp->tcp_dstopts != NULL) { 8179 mi_free(tcp->tcp_dstopts); 8180 tcp->tcp_dstopts = NULL; 8181 tcp->tcp_dstoptslen = 0; 8182 } 8183 ASSERT(tcp->tcp_dstoptslen == 0); 8184 if (tcp->tcp_rtdstopts != NULL) { 8185 mi_free(tcp->tcp_rtdstopts); 8186 tcp->tcp_rtdstopts = NULL; 8187 tcp->tcp_rtdstoptslen = 0; 8188 } 8189 ASSERT(tcp->tcp_rtdstoptslen == 0); 8190 if (tcp->tcp_rthdr != NULL) { 8191 mi_free(tcp->tcp_rthdr); 8192 tcp->tcp_rthdr = NULL; 8193 tcp->tcp_rthdrlen = 0; 8194 } 8195 ASSERT(tcp->tcp_rthdrlen == 0); 8196 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 8197 8198 /* Reset fusion-related fields */ 8199 tcp->tcp_fused = B_FALSE; 8200 tcp->tcp_unfusable = B_FALSE; 8201 tcp->tcp_fused_sigurg = B_FALSE; 8202 tcp->tcp_direct_sockfs = B_FALSE; 8203 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8204 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8205 tcp->tcp_loopback_peer = NULL; 8206 tcp->tcp_fuse_rcv_hiwater = 0; 8207 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8208 tcp->tcp_fuse_rcv_unread_cnt = 0; 8209 8210 tcp->tcp_lso = B_FALSE; 8211 8212 tcp->tcp_in_ack_unsent = 0; 8213 tcp->tcp_cork = B_FALSE; 8214 tcp->tcp_tconnind_started = B_FALSE; 8215 8216 PRESERVE(tcp->tcp_squeue_bytes); 8217 8218 ASSERT(tcp->tcp_kssl_ctx == NULL); 8219 ASSERT(!tcp->tcp_kssl_pending); 8220 PRESERVE(tcp->tcp_kssl_ent); 8221 8222 /* Sodirect */ 8223 tcp->tcp_sodirect = NULL; 8224 8225 tcp->tcp_closemp_used = B_FALSE; 8226 8227 PRESERVE(tcp->tcp_rsrv_mp); 8228 PRESERVE(tcp->tcp_rsrv_mp_lock); 8229 8230 #ifdef DEBUG 8231 DONTCARE(tcp->tcmp_stk[0]); 8232 #endif 8233 8234 8235 #undef DONTCARE 8236 #undef PRESERVE 8237 } 8238 8239 /* 8240 * Allocate necessary resources and initialize state vector. 8241 * Guaranteed not to fail so that when an error is returned, 8242 * the caller doesn't need to do any additional cleanup. 8243 */ 8244 int 8245 tcp_init(tcp_t *tcp, queue_t *q) 8246 { 8247 int err; 8248 8249 tcp->tcp_rq = q; 8250 tcp->tcp_wq = WR(q); 8251 tcp->tcp_state = TCPS_IDLE; 8252 if ((err = tcp_init_values(tcp)) != 0) 8253 tcp_timers_stop(tcp); 8254 return (err); 8255 } 8256 8257 static int 8258 tcp_init_values(tcp_t *tcp) 8259 { 8260 int err; 8261 tcp_stack_t *tcps = tcp->tcp_tcps; 8262 8263 ASSERT((tcp->tcp_family == AF_INET && 8264 tcp->tcp_ipversion == IPV4_VERSION) || 8265 (tcp->tcp_family == AF_INET6 && 8266 (tcp->tcp_ipversion == IPV4_VERSION || 8267 tcp->tcp_ipversion == IPV6_VERSION))); 8268 8269 /* 8270 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 8271 * will be close to tcp_rexmit_interval_initial. By doing this, we 8272 * allow the algorithm to adjust slowly to large fluctuations of RTT 8273 * during first few transmissions of a connection as seen in slow 8274 * links. 8275 */ 8276 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 8277 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 8278 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 8279 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 8280 tcps->tcps_conn_grace_period; 8281 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 8282 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 8283 tcp->tcp_timer_backoff = 0; 8284 tcp->tcp_ms_we_have_waited = 0; 8285 tcp->tcp_last_recv_time = lbolt; 8286 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 8287 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 8288 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 8289 8290 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 8291 8292 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 8293 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 8294 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 8295 /* 8296 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 8297 * passive open. 8298 */ 8299 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 8300 8301 tcp->tcp_naglim = tcps->tcps_naglim_def; 8302 8303 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 8304 8305 tcp->tcp_mdt_hdr_head = 0; 8306 tcp->tcp_mdt_hdr_tail = 0; 8307 8308 /* Reset fusion-related fields */ 8309 tcp->tcp_fused = B_FALSE; 8310 tcp->tcp_unfusable = B_FALSE; 8311 tcp->tcp_fused_sigurg = B_FALSE; 8312 tcp->tcp_direct_sockfs = B_FALSE; 8313 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8314 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8315 tcp->tcp_loopback_peer = NULL; 8316 tcp->tcp_fuse_rcv_hiwater = 0; 8317 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8318 tcp->tcp_fuse_rcv_unread_cnt = 0; 8319 8320 /* Sodirect */ 8321 tcp->tcp_sodirect = NULL; 8322 8323 /* Initialize the header template */ 8324 if (tcp->tcp_ipversion == IPV4_VERSION) { 8325 err = tcp_header_init_ipv4(tcp); 8326 } else { 8327 err = tcp_header_init_ipv6(tcp); 8328 } 8329 if (err) 8330 return (err); 8331 8332 /* 8333 * Init the window scale to the max so tcp_rwnd_set() won't pare 8334 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8335 */ 8336 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8337 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8338 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8339 8340 tcp->tcp_cork = B_FALSE; 8341 /* 8342 * Init the tcp_debug option. This value determines whether TCP 8343 * calls strlog() to print out debug messages. Doing this 8344 * initialization here means that this value is not inherited thru 8345 * tcp_reinit(). 8346 */ 8347 tcp->tcp_debug = tcps->tcps_dbg; 8348 8349 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8350 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8351 8352 return (0); 8353 } 8354 8355 /* 8356 * Initialize the IPv4 header. Loses any record of any IP options. 8357 */ 8358 static int 8359 tcp_header_init_ipv4(tcp_t *tcp) 8360 { 8361 tcph_t *tcph; 8362 uint32_t sum; 8363 conn_t *connp; 8364 tcp_stack_t *tcps = tcp->tcp_tcps; 8365 8366 /* 8367 * This is a simple initialization. If there's 8368 * already a template, it should never be too small, 8369 * so reuse it. Otherwise, allocate space for the new one. 8370 */ 8371 if (tcp->tcp_iphc == NULL) { 8372 ASSERT(tcp->tcp_iphc_len == 0); 8373 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8374 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8375 if (tcp->tcp_iphc == NULL) { 8376 tcp->tcp_iphc_len = 0; 8377 return (ENOMEM); 8378 } 8379 } 8380 8381 /* options are gone; may need a new label */ 8382 connp = tcp->tcp_connp; 8383 connp->conn_mlp_type = mlptSingle; 8384 connp->conn_ulp_labeled = !is_system_labeled(); 8385 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8386 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8387 tcp->tcp_ip6h = NULL; 8388 tcp->tcp_ipversion = IPV4_VERSION; 8389 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8390 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8391 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8392 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8393 tcp->tcp_ipha->ipha_version_and_hdr_length 8394 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8395 tcp->tcp_ipha->ipha_ident = 0; 8396 8397 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8398 tcp->tcp_tos = 0; 8399 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8400 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8401 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8402 8403 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8404 tcp->tcp_tcph = tcph; 8405 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8406 /* 8407 * IP wants our header length in the checksum field to 8408 * allow it to perform a single pseudo-header+checksum 8409 * calculation on behalf of TCP. 8410 * Include the adjustment for a source route once IP_OPTIONS is set. 8411 */ 8412 sum = sizeof (tcph_t) + tcp->tcp_sum; 8413 sum = (sum >> 16) + (sum & 0xFFFF); 8414 U16_TO_ABE16(sum, tcph->th_sum); 8415 return (0); 8416 } 8417 8418 /* 8419 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8420 */ 8421 static int 8422 tcp_header_init_ipv6(tcp_t *tcp) 8423 { 8424 tcph_t *tcph; 8425 uint32_t sum; 8426 conn_t *connp; 8427 tcp_stack_t *tcps = tcp->tcp_tcps; 8428 8429 /* 8430 * This is a simple initialization. If there's 8431 * already a template, it should never be too small, 8432 * so reuse it. Otherwise, allocate space for the new one. 8433 * Ensure that there is enough space to "downgrade" the tcp_t 8434 * to an IPv4 tcp_t. This requires having space for a full load 8435 * of IPv4 options, as well as a full load of TCP options 8436 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8437 * than a v6 header and a TCP header with a full load of TCP options 8438 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8439 * We want to avoid reallocation in the "downgraded" case when 8440 * processing outbound IPv4 options. 8441 */ 8442 if (tcp->tcp_iphc == NULL) { 8443 ASSERT(tcp->tcp_iphc_len == 0); 8444 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8445 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8446 if (tcp->tcp_iphc == NULL) { 8447 tcp->tcp_iphc_len = 0; 8448 return (ENOMEM); 8449 } 8450 } 8451 8452 /* options are gone; may need a new label */ 8453 connp = tcp->tcp_connp; 8454 connp->conn_mlp_type = mlptSingle; 8455 connp->conn_ulp_labeled = !is_system_labeled(); 8456 8457 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8458 tcp->tcp_ipversion = IPV6_VERSION; 8459 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8460 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8461 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8462 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8463 tcp->tcp_ipha = NULL; 8464 8465 /* Initialize the header template */ 8466 8467 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8468 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8469 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8470 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8471 8472 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8473 tcp->tcp_tcph = tcph; 8474 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8475 /* 8476 * IP wants our header length in the checksum field to 8477 * allow it to perform a single psuedo-header+checksum 8478 * calculation on behalf of TCP. 8479 * Include the adjustment for a source route when IPV6_RTHDR is set. 8480 */ 8481 sum = sizeof (tcph_t) + tcp->tcp_sum; 8482 sum = (sum >> 16) + (sum & 0xFFFF); 8483 U16_TO_ABE16(sum, tcph->th_sum); 8484 return (0); 8485 } 8486 8487 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8488 #define ICMP_MIN_TCP_HDR 8 8489 8490 /* 8491 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8492 * passed up by IP. The message is always received on the correct tcp_t. 8493 * Assumes that IP has pulled up everything up to and including the ICMP header. 8494 */ 8495 void 8496 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8497 { 8498 icmph_t *icmph; 8499 ipha_t *ipha; 8500 int iph_hdr_length; 8501 tcph_t *tcph; 8502 boolean_t ipsec_mctl = B_FALSE; 8503 boolean_t secure; 8504 mblk_t *first_mp = mp; 8505 int32_t new_mss; 8506 uint32_t ratio; 8507 size_t mp_size = MBLKL(mp); 8508 uint32_t seg_seq; 8509 tcp_stack_t *tcps = tcp->tcp_tcps; 8510 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 8511 8512 /* Assume IP provides aligned packets - otherwise toss */ 8513 if (!OK_32PTR(mp->b_rptr)) { 8514 freemsg(mp); 8515 return; 8516 } 8517 8518 /* 8519 * Since ICMP errors are normal data marked with M_CTL when sent 8520 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8521 * packets starting with an ipsec_info_t, see ipsec_info.h. 8522 */ 8523 if ((mp_size == sizeof (ipsec_info_t)) && 8524 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8525 ASSERT(mp->b_cont != NULL); 8526 mp = mp->b_cont; 8527 /* IP should have done this */ 8528 ASSERT(OK_32PTR(mp->b_rptr)); 8529 mp_size = MBLKL(mp); 8530 ipsec_mctl = B_TRUE; 8531 } 8532 8533 /* 8534 * Verify that we have a complete outer IP header. If not, drop it. 8535 */ 8536 if (mp_size < sizeof (ipha_t)) { 8537 noticmpv4: 8538 freemsg(first_mp); 8539 return; 8540 } 8541 8542 ipha = (ipha_t *)mp->b_rptr; 8543 /* 8544 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8545 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8546 */ 8547 switch (IPH_HDR_VERSION(ipha)) { 8548 case IPV6_VERSION: 8549 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8550 return; 8551 case IPV4_VERSION: 8552 break; 8553 default: 8554 goto noticmpv4; 8555 } 8556 8557 /* Skip past the outer IP and ICMP headers */ 8558 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8559 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8560 /* 8561 * If we don't have the correct outer IP header length or if the ULP 8562 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8563 * send it upstream. 8564 */ 8565 if (iph_hdr_length < sizeof (ipha_t) || 8566 ipha->ipha_protocol != IPPROTO_ICMP || 8567 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8568 goto noticmpv4; 8569 } 8570 ipha = (ipha_t *)&icmph[1]; 8571 8572 /* Skip past the inner IP and find the ULP header */ 8573 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8574 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8575 /* 8576 * If we don't have the correct inner IP header length or if the ULP 8577 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8578 * bytes of TCP header, drop it. 8579 */ 8580 if (iph_hdr_length < sizeof (ipha_t) || 8581 ipha->ipha_protocol != IPPROTO_TCP || 8582 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8583 goto noticmpv4; 8584 } 8585 8586 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8587 if (ipsec_mctl) { 8588 secure = ipsec_in_is_secure(first_mp); 8589 } else { 8590 secure = B_FALSE; 8591 } 8592 if (secure) { 8593 /* 8594 * If we are willing to accept this in clear 8595 * we don't have to verify policy. 8596 */ 8597 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8598 if (!tcp_check_policy(tcp, first_mp, 8599 ipha, NULL, secure, ipsec_mctl)) { 8600 /* 8601 * tcp_check_policy called 8602 * ip_drop_packet() on failure. 8603 */ 8604 return; 8605 } 8606 } 8607 } 8608 } else if (ipsec_mctl) { 8609 /* 8610 * This is a hard_bound connection. IP has already 8611 * verified policy. We don't have to do it again. 8612 */ 8613 freeb(first_mp); 8614 first_mp = mp; 8615 ipsec_mctl = B_FALSE; 8616 } 8617 8618 seg_seq = ABE32_TO_U32(tcph->th_seq); 8619 /* 8620 * TCP SHOULD check that the TCP sequence number contained in 8621 * payload of the ICMP error message is within the range 8622 * SND.UNA <= SEG.SEQ < SND.NXT. 8623 */ 8624 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8625 /* 8626 * The ICMP message is bogus, just drop it. But if this is 8627 * an ICMP too big message, IP has already changed 8628 * the ire_max_frag to the bogus value. We need to change 8629 * it back. 8630 */ 8631 if (icmph->icmph_type == ICMP_DEST_UNREACHABLE && 8632 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 8633 conn_t *connp = tcp->tcp_connp; 8634 ire_t *ire; 8635 int flag; 8636 8637 if (tcp->tcp_ipversion == IPV4_VERSION) { 8638 flag = tcp->tcp_ipha-> 8639 ipha_fragment_offset_and_flags; 8640 } else { 8641 flag = 0; 8642 } 8643 mutex_enter(&connp->conn_lock); 8644 if ((ire = connp->conn_ire_cache) != NULL) { 8645 mutex_enter(&ire->ire_lock); 8646 mutex_exit(&connp->conn_lock); 8647 ire->ire_max_frag = tcp->tcp_if_mtu; 8648 ire->ire_frag_flag |= flag; 8649 mutex_exit(&ire->ire_lock); 8650 } else { 8651 mutex_exit(&connp->conn_lock); 8652 } 8653 } 8654 goto noticmpv4; 8655 } 8656 8657 switch (icmph->icmph_type) { 8658 case ICMP_DEST_UNREACHABLE: 8659 switch (icmph->icmph_code) { 8660 case ICMP_FRAGMENTATION_NEEDED: 8661 /* 8662 * Reduce the MSS based on the new MTU. This will 8663 * eliminate any fragmentation locally. 8664 * N.B. There may well be some funny side-effects on 8665 * the local send policy and the remote receive policy. 8666 * Pending further research, we provide 8667 * tcp_ignore_path_mtu just in case this proves 8668 * disastrous somewhere. 8669 * 8670 * After updating the MSS, retransmit part of the 8671 * dropped segment using the new mss by calling 8672 * tcp_wput_data(). Need to adjust all those 8673 * params to make sure tcp_wput_data() work properly. 8674 */ 8675 if (tcps->tcps_ignore_path_mtu || 8676 tcp->tcp_ipha->ipha_fragment_offset_and_flags == 0) 8677 break; 8678 8679 /* 8680 * Decrease the MSS by time stamp options 8681 * IP options and IPSEC options. tcp_hdr_len 8682 * includes time stamp option and IP option 8683 * length. Note that new_mss may be negative 8684 * if tcp_ipsec_overhead is large and the 8685 * icmph_du_mtu is the minimum value, which is 68. 8686 */ 8687 new_mss = ntohs(icmph->icmph_du_mtu) - 8688 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8689 8690 DTRACE_PROBE2(tcp__pmtu__change, tcp_t *, tcp, int, 8691 new_mss); 8692 8693 /* 8694 * Only update the MSS if the new one is 8695 * smaller than the previous one. This is 8696 * to avoid problems when getting multiple 8697 * ICMP errors for the same MTU. 8698 */ 8699 if (new_mss >= tcp->tcp_mss) 8700 break; 8701 8702 /* 8703 * Note that we are using the template header's DF 8704 * bit in the fast path sending. So we need to compare 8705 * the new mss with both tcps_mss_min and ip_pmtu_min. 8706 * And stop doing IPv4 PMTUd if new_mss is less than 8707 * MAX(tcps_mss_min, ip_pmtu_min). 8708 */ 8709 if (new_mss < tcps->tcps_mss_min || 8710 new_mss < ipst->ips_ip_pmtu_min) { 8711 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8712 0; 8713 } 8714 8715 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8716 ASSERT(ratio >= 1); 8717 tcp_mss_set(tcp, new_mss, B_TRUE); 8718 8719 /* 8720 * Make sure we have something to 8721 * send. 8722 */ 8723 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8724 (tcp->tcp_xmit_head != NULL)) { 8725 /* 8726 * Shrink tcp_cwnd in 8727 * proportion to the old MSS/new MSS. 8728 */ 8729 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8730 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8731 (tcp->tcp_unsent == 0)) { 8732 tcp->tcp_rexmit_max = tcp->tcp_fss; 8733 } else { 8734 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8735 } 8736 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8737 tcp->tcp_rexmit = B_TRUE; 8738 tcp->tcp_dupack_cnt = 0; 8739 tcp->tcp_snd_burst = TCP_CWND_SS; 8740 tcp_ss_rexmit(tcp); 8741 } 8742 break; 8743 case ICMP_PORT_UNREACHABLE: 8744 case ICMP_PROTOCOL_UNREACHABLE: 8745 switch (tcp->tcp_state) { 8746 case TCPS_SYN_SENT: 8747 case TCPS_SYN_RCVD: 8748 /* 8749 * ICMP can snipe away incipient 8750 * TCP connections as long as 8751 * seq number is same as initial 8752 * send seq number. 8753 */ 8754 if (seg_seq == tcp->tcp_iss) { 8755 (void) tcp_clean_death(tcp, 8756 ECONNREFUSED, 6); 8757 } 8758 break; 8759 } 8760 break; 8761 case ICMP_HOST_UNREACHABLE: 8762 case ICMP_NET_UNREACHABLE: 8763 /* Record the error in case we finally time out. */ 8764 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8765 tcp->tcp_client_errno = EHOSTUNREACH; 8766 else 8767 tcp->tcp_client_errno = ENETUNREACH; 8768 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8769 if (tcp->tcp_listener != NULL && 8770 tcp->tcp_listener->tcp_syn_defense) { 8771 /* 8772 * Ditch the half-open connection if we 8773 * suspect a SYN attack is under way. 8774 */ 8775 tcp_ip_ire_mark_advice(tcp); 8776 (void) tcp_clean_death(tcp, 8777 tcp->tcp_client_errno, 7); 8778 } 8779 } 8780 break; 8781 default: 8782 break; 8783 } 8784 break; 8785 case ICMP_SOURCE_QUENCH: { 8786 /* 8787 * use a global boolean to control 8788 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8789 * The default is false. 8790 */ 8791 if (tcp_icmp_source_quench) { 8792 /* 8793 * Reduce the sending rate as if we got a 8794 * retransmit timeout 8795 */ 8796 uint32_t npkt; 8797 8798 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8799 tcp->tcp_mss; 8800 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8801 tcp->tcp_cwnd = tcp->tcp_mss; 8802 tcp->tcp_cwnd_cnt = 0; 8803 } 8804 break; 8805 } 8806 } 8807 freemsg(first_mp); 8808 } 8809 8810 /* 8811 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8812 * error messages passed up by IP. 8813 * Assumes that IP has pulled up all the extension headers as well 8814 * as the ICMPv6 header. 8815 */ 8816 static void 8817 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8818 { 8819 icmp6_t *icmp6; 8820 ip6_t *ip6h; 8821 uint16_t iph_hdr_length; 8822 tcpha_t *tcpha; 8823 uint8_t *nexthdrp; 8824 uint32_t new_mss; 8825 uint32_t ratio; 8826 boolean_t secure; 8827 mblk_t *first_mp = mp; 8828 size_t mp_size; 8829 uint32_t seg_seq; 8830 tcp_stack_t *tcps = tcp->tcp_tcps; 8831 8832 /* 8833 * The caller has determined if this is an IPSEC_IN packet and 8834 * set ipsec_mctl appropriately (see tcp_icmp_error). 8835 */ 8836 if (ipsec_mctl) 8837 mp = mp->b_cont; 8838 8839 mp_size = MBLKL(mp); 8840 8841 /* 8842 * Verify that we have a complete IP header. If not, send it upstream. 8843 */ 8844 if (mp_size < sizeof (ip6_t)) { 8845 noticmpv6: 8846 freemsg(first_mp); 8847 return; 8848 } 8849 8850 /* 8851 * Verify this is an ICMPV6 packet, else send it upstream. 8852 */ 8853 ip6h = (ip6_t *)mp->b_rptr; 8854 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8855 iph_hdr_length = IPV6_HDR_LEN; 8856 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8857 &nexthdrp) || 8858 *nexthdrp != IPPROTO_ICMPV6) { 8859 goto noticmpv6; 8860 } 8861 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8862 ip6h = (ip6_t *)&icmp6[1]; 8863 /* 8864 * Verify if we have a complete ICMP and inner IP header. 8865 */ 8866 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8867 goto noticmpv6; 8868 8869 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8870 goto noticmpv6; 8871 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8872 /* 8873 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8874 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8875 * packet. 8876 */ 8877 if ((*nexthdrp != IPPROTO_TCP) || 8878 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8879 goto noticmpv6; 8880 } 8881 8882 /* 8883 * ICMP errors come on the right queue or come on 8884 * listener/global queue for detached connections and 8885 * get switched to the right queue. If it comes on the 8886 * right queue, policy check has already been done by IP 8887 * and thus free the first_mp without verifying the policy. 8888 * If it has come for a non-hard bound connection, we need 8889 * to verify policy as IP may not have done it. 8890 */ 8891 if (!tcp->tcp_hard_bound) { 8892 if (ipsec_mctl) { 8893 secure = ipsec_in_is_secure(first_mp); 8894 } else { 8895 secure = B_FALSE; 8896 } 8897 if (secure) { 8898 /* 8899 * If we are willing to accept this in clear 8900 * we don't have to verify policy. 8901 */ 8902 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8903 if (!tcp_check_policy(tcp, first_mp, 8904 NULL, ip6h, secure, ipsec_mctl)) { 8905 /* 8906 * tcp_check_policy called 8907 * ip_drop_packet() on failure. 8908 */ 8909 return; 8910 } 8911 } 8912 } 8913 } else if (ipsec_mctl) { 8914 /* 8915 * This is a hard_bound connection. IP has already 8916 * verified policy. We don't have to do it again. 8917 */ 8918 freeb(first_mp); 8919 first_mp = mp; 8920 ipsec_mctl = B_FALSE; 8921 } 8922 8923 seg_seq = ntohl(tcpha->tha_seq); 8924 /* 8925 * TCP SHOULD check that the TCP sequence number contained in 8926 * payload of the ICMP error message is within the range 8927 * SND.UNA <= SEG.SEQ < SND.NXT. 8928 */ 8929 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8930 /* 8931 * If the ICMP message is bogus, should we kill the 8932 * connection, or should we just drop the bogus ICMP 8933 * message? It would probably make more sense to just 8934 * drop the message so that if this one managed to get 8935 * in, the real connection should not suffer. 8936 */ 8937 goto noticmpv6; 8938 } 8939 8940 switch (icmp6->icmp6_type) { 8941 case ICMP6_PACKET_TOO_BIG: 8942 /* 8943 * Reduce the MSS based on the new MTU. This will 8944 * eliminate any fragmentation locally. 8945 * N.B. There may well be some funny side-effects on 8946 * the local send policy and the remote receive policy. 8947 * Pending further research, we provide 8948 * tcp_ignore_path_mtu just in case this proves 8949 * disastrous somewhere. 8950 * 8951 * After updating the MSS, retransmit part of the 8952 * dropped segment using the new mss by calling 8953 * tcp_wput_data(). Need to adjust all those 8954 * params to make sure tcp_wput_data() work properly. 8955 */ 8956 if (tcps->tcps_ignore_path_mtu) 8957 break; 8958 8959 /* 8960 * Decrease the MSS by time stamp options 8961 * IP options and IPSEC options. tcp_hdr_len 8962 * includes time stamp option and IP option 8963 * length. 8964 */ 8965 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8966 tcp->tcp_ipsec_overhead; 8967 8968 /* 8969 * Only update the MSS if the new one is 8970 * smaller than the previous one. This is 8971 * to avoid problems when getting multiple 8972 * ICMP errors for the same MTU. 8973 */ 8974 if (new_mss >= tcp->tcp_mss) 8975 break; 8976 8977 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8978 ASSERT(ratio >= 1); 8979 tcp_mss_set(tcp, new_mss, B_TRUE); 8980 8981 /* 8982 * Make sure we have something to 8983 * send. 8984 */ 8985 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8986 (tcp->tcp_xmit_head != NULL)) { 8987 /* 8988 * Shrink tcp_cwnd in 8989 * proportion to the old MSS/new MSS. 8990 */ 8991 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8992 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8993 (tcp->tcp_unsent == 0)) { 8994 tcp->tcp_rexmit_max = tcp->tcp_fss; 8995 } else { 8996 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8997 } 8998 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8999 tcp->tcp_rexmit = B_TRUE; 9000 tcp->tcp_dupack_cnt = 0; 9001 tcp->tcp_snd_burst = TCP_CWND_SS; 9002 tcp_ss_rexmit(tcp); 9003 } 9004 break; 9005 9006 case ICMP6_DST_UNREACH: 9007 switch (icmp6->icmp6_code) { 9008 case ICMP6_DST_UNREACH_NOPORT: 9009 if (((tcp->tcp_state == TCPS_SYN_SENT) || 9010 (tcp->tcp_state == TCPS_SYN_RCVD)) && 9011 (seg_seq == tcp->tcp_iss)) { 9012 (void) tcp_clean_death(tcp, 9013 ECONNREFUSED, 8); 9014 } 9015 break; 9016 9017 case ICMP6_DST_UNREACH_ADMIN: 9018 case ICMP6_DST_UNREACH_NOROUTE: 9019 case ICMP6_DST_UNREACH_BEYONDSCOPE: 9020 case ICMP6_DST_UNREACH_ADDR: 9021 /* Record the error in case we finally time out. */ 9022 tcp->tcp_client_errno = EHOSTUNREACH; 9023 if (((tcp->tcp_state == TCPS_SYN_SENT) || 9024 (tcp->tcp_state == TCPS_SYN_RCVD)) && 9025 (seg_seq == tcp->tcp_iss)) { 9026 if (tcp->tcp_listener != NULL && 9027 tcp->tcp_listener->tcp_syn_defense) { 9028 /* 9029 * Ditch the half-open connection if we 9030 * suspect a SYN attack is under way. 9031 */ 9032 tcp_ip_ire_mark_advice(tcp); 9033 (void) tcp_clean_death(tcp, 9034 tcp->tcp_client_errno, 9); 9035 } 9036 } 9037 9038 9039 break; 9040 default: 9041 break; 9042 } 9043 break; 9044 9045 case ICMP6_PARAM_PROB: 9046 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 9047 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 9048 (uchar_t *)ip6h + icmp6->icmp6_pptr == 9049 (uchar_t *)nexthdrp) { 9050 if (tcp->tcp_state == TCPS_SYN_SENT || 9051 tcp->tcp_state == TCPS_SYN_RCVD) { 9052 (void) tcp_clean_death(tcp, 9053 ECONNREFUSED, 10); 9054 } 9055 break; 9056 } 9057 break; 9058 9059 case ICMP6_TIME_EXCEEDED: 9060 default: 9061 break; 9062 } 9063 freemsg(first_mp); 9064 } 9065 9066 /* 9067 * IP recognizes seven kinds of bind requests: 9068 * 9069 * - A zero-length address binds only to the protocol number. 9070 * 9071 * - A 4-byte address is treated as a request to 9072 * validate that the address is a valid local IPv4 9073 * address, appropriate for an application to bind to. 9074 * IP does the verification, but does not make any note 9075 * of the address at this time. 9076 * 9077 * - A 16-byte address contains is treated as a request 9078 * to validate a local IPv6 address, as the 4-byte 9079 * address case above. 9080 * 9081 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 9082 * use it for the inbound fanout of packets. 9083 * 9084 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 9085 * use it for the inbound fanout of packets. 9086 * 9087 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 9088 * information consisting of local and remote addresses 9089 * and ports. In this case, the addresses are both 9090 * validated as appropriate for this operation, and, if 9091 * so, the information is retained for use in the 9092 * inbound fanout. 9093 * 9094 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 9095 * fanout information, like the 12-byte case above. 9096 * 9097 * IP will also fill in the IRE request mblk with information 9098 * regarding our peer. In all cases, we notify IP of our protocol 9099 * type by appending a single protocol byte to the bind request. 9100 */ 9101 static mblk_t * 9102 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 9103 { 9104 char *cp; 9105 mblk_t *mp; 9106 struct T_bind_req *tbr; 9107 ipa_conn_t *ac; 9108 ipa6_conn_t *ac6; 9109 sin_t *sin; 9110 sin6_t *sin6; 9111 9112 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 9113 ASSERT((tcp->tcp_family == AF_INET && 9114 tcp->tcp_ipversion == IPV4_VERSION) || 9115 (tcp->tcp_family == AF_INET6 && 9116 (tcp->tcp_ipversion == IPV4_VERSION || 9117 tcp->tcp_ipversion == IPV6_VERSION))); 9118 9119 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 9120 if (!mp) 9121 return (mp); 9122 mp->b_datap->db_type = M_PROTO; 9123 tbr = (struct T_bind_req *)mp->b_rptr; 9124 tbr->PRIM_type = bind_prim; 9125 tbr->ADDR_offset = sizeof (*tbr); 9126 tbr->CONIND_number = 0; 9127 tbr->ADDR_length = addr_length; 9128 cp = (char *)&tbr[1]; 9129 switch (addr_length) { 9130 case sizeof (ipa_conn_t): 9131 ASSERT(tcp->tcp_family == AF_INET); 9132 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9133 9134 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9135 if (mp->b_cont == NULL) { 9136 freemsg(mp); 9137 return (NULL); 9138 } 9139 mp->b_cont->b_wptr += sizeof (ire_t); 9140 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9141 9142 /* cp known to be 32 bit aligned */ 9143 ac = (ipa_conn_t *)cp; 9144 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 9145 ac->ac_faddr = tcp->tcp_remote; 9146 ac->ac_fport = tcp->tcp_fport; 9147 ac->ac_lport = tcp->tcp_lport; 9148 tcp->tcp_hard_binding = 1; 9149 break; 9150 9151 case sizeof (ipa6_conn_t): 9152 ASSERT(tcp->tcp_family == AF_INET6); 9153 9154 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9155 if (mp->b_cont == NULL) { 9156 freemsg(mp); 9157 return (NULL); 9158 } 9159 mp->b_cont->b_wptr += sizeof (ire_t); 9160 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9161 9162 /* cp known to be 32 bit aligned */ 9163 ac6 = (ipa6_conn_t *)cp; 9164 if (tcp->tcp_ipversion == IPV4_VERSION) { 9165 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 9166 &ac6->ac6_laddr); 9167 } else { 9168 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 9169 } 9170 ac6->ac6_faddr = tcp->tcp_remote_v6; 9171 ac6->ac6_fport = tcp->tcp_fport; 9172 ac6->ac6_lport = tcp->tcp_lport; 9173 tcp->tcp_hard_binding = 1; 9174 break; 9175 9176 case sizeof (sin_t): 9177 /* 9178 * NOTE: IPV6_ADDR_LEN also has same size. 9179 * Use family to discriminate. 9180 */ 9181 if (tcp->tcp_family == AF_INET) { 9182 sin = (sin_t *)cp; 9183 9184 *sin = sin_null; 9185 sin->sin_family = AF_INET; 9186 sin->sin_addr.s_addr = tcp->tcp_bound_source; 9187 sin->sin_port = tcp->tcp_lport; 9188 break; 9189 } else { 9190 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 9191 } 9192 break; 9193 9194 case sizeof (sin6_t): 9195 ASSERT(tcp->tcp_family == AF_INET6); 9196 sin6 = (sin6_t *)cp; 9197 9198 *sin6 = sin6_null; 9199 sin6->sin6_family = AF_INET6; 9200 sin6->sin6_addr = tcp->tcp_bound_source_v6; 9201 sin6->sin6_port = tcp->tcp_lport; 9202 break; 9203 9204 case IP_ADDR_LEN: 9205 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9206 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 9207 break; 9208 9209 } 9210 /* Add protocol number to end */ 9211 cp[addr_length] = (char)IPPROTO_TCP; 9212 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 9213 return (mp); 9214 } 9215 9216 /* 9217 * Notify IP that we are having trouble with this connection. IP should 9218 * blow the IRE away and start over. 9219 */ 9220 static void 9221 tcp_ip_notify(tcp_t *tcp) 9222 { 9223 struct iocblk *iocp; 9224 ipid_t *ipid; 9225 mblk_t *mp; 9226 9227 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 9228 if (tcp->tcp_ipversion == IPV6_VERSION) 9229 return; 9230 9231 mp = mkiocb(IP_IOCTL); 9232 if (mp == NULL) 9233 return; 9234 9235 iocp = (struct iocblk *)mp->b_rptr; 9236 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 9237 9238 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 9239 if (!mp->b_cont) { 9240 freeb(mp); 9241 return; 9242 } 9243 9244 ipid = (ipid_t *)mp->b_cont->b_rptr; 9245 mp->b_cont->b_wptr += iocp->ioc_count; 9246 bzero(ipid, sizeof (*ipid)); 9247 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 9248 ipid->ipid_ire_type = IRE_CACHE; 9249 ipid->ipid_addr_offset = sizeof (ipid_t); 9250 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 9251 /* 9252 * Note: in the case of source routing we want to blow away the 9253 * route to the first source route hop. 9254 */ 9255 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 9256 sizeof (tcp->tcp_ipha->ipha_dst)); 9257 9258 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 9259 } 9260 9261 /* Unlink and return any mblk that looks like it contains an ire */ 9262 static mblk_t * 9263 tcp_ire_mp(mblk_t *mp) 9264 { 9265 mblk_t *prev_mp; 9266 9267 for (;;) { 9268 prev_mp = mp; 9269 mp = mp->b_cont; 9270 if (mp == NULL) 9271 break; 9272 switch (DB_TYPE(mp)) { 9273 case IRE_DB_TYPE: 9274 case IRE_DB_REQ_TYPE: 9275 if (prev_mp != NULL) 9276 prev_mp->b_cont = mp->b_cont; 9277 mp->b_cont = NULL; 9278 return (mp); 9279 default: 9280 break; 9281 } 9282 } 9283 return (mp); 9284 } 9285 9286 /* 9287 * Timer callback routine for keepalive probe. We do a fake resend of 9288 * last ACKed byte. Then set a timer using RTO. When the timer expires, 9289 * check to see if we have heard anything from the other end for the last 9290 * RTO period. If we have, set the timer to expire for another 9291 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 9292 * RTO << 1 and check again when it expires. Keep exponentially increasing 9293 * the timeout if we have not heard from the other side. If for more than 9294 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 9295 * kill the connection unless the keepalive abort threshold is 0. In 9296 * that case, we will probe "forever." 9297 */ 9298 static void 9299 tcp_keepalive_killer(void *arg) 9300 { 9301 mblk_t *mp; 9302 conn_t *connp = (conn_t *)arg; 9303 tcp_t *tcp = connp->conn_tcp; 9304 int32_t firetime; 9305 int32_t idletime; 9306 int32_t ka_intrvl; 9307 tcp_stack_t *tcps = tcp->tcp_tcps; 9308 9309 tcp->tcp_ka_tid = 0; 9310 9311 if (tcp->tcp_fused) 9312 return; 9313 9314 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 9315 ka_intrvl = tcp->tcp_ka_interval; 9316 9317 /* 9318 * Keepalive probe should only be sent if the application has not 9319 * done a close on the connection. 9320 */ 9321 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 9322 return; 9323 } 9324 /* Timer fired too early, restart it. */ 9325 if (tcp->tcp_state < TCPS_ESTABLISHED) { 9326 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9327 MSEC_TO_TICK(ka_intrvl)); 9328 return; 9329 } 9330 9331 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 9332 /* 9333 * If we have not heard from the other side for a long 9334 * time, kill the connection unless the keepalive abort 9335 * threshold is 0. In that case, we will probe "forever." 9336 */ 9337 if (tcp->tcp_ka_abort_thres != 0 && 9338 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 9339 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 9340 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 9341 tcp->tcp_client_errno : ETIMEDOUT, 11); 9342 return; 9343 } 9344 9345 if (tcp->tcp_snxt == tcp->tcp_suna && 9346 idletime >= ka_intrvl) { 9347 /* Fake resend of last ACKed byte. */ 9348 mblk_t *mp1 = allocb(1, BPRI_LO); 9349 9350 if (mp1 != NULL) { 9351 *mp1->b_wptr++ = '\0'; 9352 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 9353 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 9354 freeb(mp1); 9355 /* 9356 * if allocation failed, fall through to start the 9357 * timer back. 9358 */ 9359 if (mp != NULL) { 9360 tcp_send_data(tcp, tcp->tcp_wq, mp); 9361 BUMP_MIB(&tcps->tcps_mib, 9362 tcpTimKeepaliveProbe); 9363 if (tcp->tcp_ka_last_intrvl != 0) { 9364 int max; 9365 /* 9366 * We should probe again at least 9367 * in ka_intrvl, but not more than 9368 * tcp_rexmit_interval_max. 9369 */ 9370 max = tcps->tcps_rexmit_interval_max; 9371 firetime = MIN(ka_intrvl - 1, 9372 tcp->tcp_ka_last_intrvl << 1); 9373 if (firetime > max) 9374 firetime = max; 9375 } else { 9376 firetime = tcp->tcp_rto; 9377 } 9378 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9379 tcp_keepalive_killer, 9380 MSEC_TO_TICK(firetime)); 9381 tcp->tcp_ka_last_intrvl = firetime; 9382 return; 9383 } 9384 } 9385 } else { 9386 tcp->tcp_ka_last_intrvl = 0; 9387 } 9388 9389 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9390 if ((firetime = ka_intrvl - idletime) < 0) { 9391 firetime = ka_intrvl; 9392 } 9393 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9394 MSEC_TO_TICK(firetime)); 9395 } 9396 9397 int 9398 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9399 { 9400 queue_t *q = tcp->tcp_rq; 9401 int32_t mss = tcp->tcp_mss; 9402 int maxpsz; 9403 9404 if (TCP_IS_DETACHED(tcp)) 9405 return (mss); 9406 9407 if (tcp->tcp_fused) { 9408 maxpsz = tcp_fuse_maxpsz_set(tcp); 9409 mss = INFPSZ; 9410 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 9411 /* 9412 * Set the sd_qn_maxpsz according to the socket send buffer 9413 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9414 * instruct the stream head to copyin user data into contiguous 9415 * kernel-allocated buffers without breaking it up into smaller 9416 * chunks. We round up the buffer size to the nearest SMSS. 9417 */ 9418 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9419 if (tcp->tcp_kssl_ctx == NULL) 9420 mss = INFPSZ; 9421 else 9422 mss = SSL3_MAX_RECORD_LEN; 9423 } else { 9424 /* 9425 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9426 * (and a multiple of the mss). This instructs the stream 9427 * head to break down larger than SMSS writes into SMSS- 9428 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9429 */ 9430 maxpsz = tcp->tcp_maxpsz * mss; 9431 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9432 maxpsz = tcp->tcp_xmit_hiwater/2; 9433 /* Round up to nearest mss */ 9434 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9435 } 9436 } 9437 (void) setmaxps(q, maxpsz); 9438 tcp->tcp_wq->q_maxpsz = maxpsz; 9439 9440 if (set_maxblk) 9441 (void) mi_set_sth_maxblk(q, mss); 9442 9443 return (mss); 9444 } 9445 9446 /* 9447 * Extract option values from a tcp header. We put any found values into the 9448 * tcpopt struct and return a bitmask saying which options were found. 9449 */ 9450 static int 9451 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9452 { 9453 uchar_t *endp; 9454 int len; 9455 uint32_t mss; 9456 uchar_t *up = (uchar_t *)tcph; 9457 int found = 0; 9458 int32_t sack_len; 9459 tcp_seq sack_begin, sack_end; 9460 tcp_t *tcp; 9461 9462 endp = up + TCP_HDR_LENGTH(tcph); 9463 up += TCP_MIN_HEADER_LENGTH; 9464 while (up < endp) { 9465 len = endp - up; 9466 switch (*up) { 9467 case TCPOPT_EOL: 9468 break; 9469 9470 case TCPOPT_NOP: 9471 up++; 9472 continue; 9473 9474 case TCPOPT_MAXSEG: 9475 if (len < TCPOPT_MAXSEG_LEN || 9476 up[1] != TCPOPT_MAXSEG_LEN) 9477 break; 9478 9479 mss = BE16_TO_U16(up+2); 9480 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9481 tcpopt->tcp_opt_mss = mss; 9482 found |= TCP_OPT_MSS_PRESENT; 9483 9484 up += TCPOPT_MAXSEG_LEN; 9485 continue; 9486 9487 case TCPOPT_WSCALE: 9488 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9489 break; 9490 9491 if (up[2] > TCP_MAX_WINSHIFT) 9492 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9493 else 9494 tcpopt->tcp_opt_wscale = up[2]; 9495 found |= TCP_OPT_WSCALE_PRESENT; 9496 9497 up += TCPOPT_WS_LEN; 9498 continue; 9499 9500 case TCPOPT_SACK_PERMITTED: 9501 if (len < TCPOPT_SACK_OK_LEN || 9502 up[1] != TCPOPT_SACK_OK_LEN) 9503 break; 9504 found |= TCP_OPT_SACK_OK_PRESENT; 9505 up += TCPOPT_SACK_OK_LEN; 9506 continue; 9507 9508 case TCPOPT_SACK: 9509 if (len <= 2 || up[1] <= 2 || len < up[1]) 9510 break; 9511 9512 /* If TCP is not interested in SACK blks... */ 9513 if ((tcp = tcpopt->tcp) == NULL) { 9514 up += up[1]; 9515 continue; 9516 } 9517 sack_len = up[1] - TCPOPT_HEADER_LEN; 9518 up += TCPOPT_HEADER_LEN; 9519 9520 /* 9521 * If the list is empty, allocate one and assume 9522 * nothing is sack'ed. 9523 */ 9524 ASSERT(tcp->tcp_sack_info != NULL); 9525 if (tcp->tcp_notsack_list == NULL) { 9526 tcp_notsack_update(&(tcp->tcp_notsack_list), 9527 tcp->tcp_suna, tcp->tcp_snxt, 9528 &(tcp->tcp_num_notsack_blk), 9529 &(tcp->tcp_cnt_notsack_list)); 9530 9531 /* 9532 * Make sure tcp_notsack_list is not NULL. 9533 * This happens when kmem_alloc(KM_NOSLEEP) 9534 * returns NULL. 9535 */ 9536 if (tcp->tcp_notsack_list == NULL) { 9537 up += sack_len; 9538 continue; 9539 } 9540 tcp->tcp_fack = tcp->tcp_suna; 9541 } 9542 9543 while (sack_len > 0) { 9544 if (up + 8 > endp) { 9545 up = endp; 9546 break; 9547 } 9548 sack_begin = BE32_TO_U32(up); 9549 up += 4; 9550 sack_end = BE32_TO_U32(up); 9551 up += 4; 9552 sack_len -= 8; 9553 /* 9554 * Bounds checking. Make sure the SACK 9555 * info is within tcp_suna and tcp_snxt. 9556 * If this SACK blk is out of bound, ignore 9557 * it but continue to parse the following 9558 * blks. 9559 */ 9560 if (SEQ_LEQ(sack_end, sack_begin) || 9561 SEQ_LT(sack_begin, tcp->tcp_suna) || 9562 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9563 continue; 9564 } 9565 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9566 sack_begin, sack_end, 9567 &(tcp->tcp_num_notsack_blk), 9568 &(tcp->tcp_cnt_notsack_list)); 9569 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9570 tcp->tcp_fack = sack_end; 9571 } 9572 } 9573 found |= TCP_OPT_SACK_PRESENT; 9574 continue; 9575 9576 case TCPOPT_TSTAMP: 9577 if (len < TCPOPT_TSTAMP_LEN || 9578 up[1] != TCPOPT_TSTAMP_LEN) 9579 break; 9580 9581 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9582 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9583 9584 found |= TCP_OPT_TSTAMP_PRESENT; 9585 9586 up += TCPOPT_TSTAMP_LEN; 9587 continue; 9588 9589 default: 9590 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9591 break; 9592 up += up[1]; 9593 continue; 9594 } 9595 break; 9596 } 9597 return (found); 9598 } 9599 9600 /* 9601 * Set the mss associated with a particular tcp based on its current value, 9602 * and a new one passed in. Observe minimums and maximums, and reset 9603 * other state variables that we want to view as multiples of mss. 9604 * 9605 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9606 * highwater marks etc. need to be initialized or adjusted. 9607 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9608 * packet arrives. 9609 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9610 * ICMP6_PACKET_TOO_BIG arrives. 9611 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9612 * to increase the MSS to use the extra bytes available. 9613 * 9614 * Callers except tcp_paws_check() ensure that they only reduce mss. 9615 */ 9616 static void 9617 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9618 { 9619 uint32_t mss_max; 9620 tcp_stack_t *tcps = tcp->tcp_tcps; 9621 9622 if (tcp->tcp_ipversion == IPV4_VERSION) 9623 mss_max = tcps->tcps_mss_max_ipv4; 9624 else 9625 mss_max = tcps->tcps_mss_max_ipv6; 9626 9627 if (mss < tcps->tcps_mss_min) 9628 mss = tcps->tcps_mss_min; 9629 if (mss > mss_max) 9630 mss = mss_max; 9631 /* 9632 * Unless naglim has been set by our client to 9633 * a non-mss value, force naglim to track mss. 9634 * This can help to aggregate small writes. 9635 */ 9636 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9637 tcp->tcp_naglim = mss; 9638 /* 9639 * TCP should be able to buffer at least 4 MSS data for obvious 9640 * performance reason. 9641 */ 9642 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9643 tcp->tcp_xmit_hiwater = mss << 2; 9644 9645 if (do_ss) { 9646 /* 9647 * Either the tcp_cwnd is as yet uninitialized, or mss is 9648 * changing due to a reduction in MTU, presumably as a 9649 * result of a new path component, reset cwnd to its 9650 * "initial" value, as a multiple of the new mss. 9651 */ 9652 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9653 } else { 9654 /* 9655 * Called by tcp_paws_check(), the mss increased 9656 * marginally to allow use of space previously taken 9657 * by the timestamp option. It would be inappropriate 9658 * to apply slow start or tcp_init_cwnd values to 9659 * tcp_cwnd, simply adjust to a multiple of the new mss. 9660 */ 9661 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9662 tcp->tcp_cwnd_cnt = 0; 9663 } 9664 tcp->tcp_mss = mss; 9665 (void) tcp_maxpsz_set(tcp, B_TRUE); 9666 } 9667 9668 /* For /dev/tcp aka AF_INET open */ 9669 static int 9670 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9671 { 9672 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9673 } 9674 9675 /* For /dev/tcp6 aka AF_INET6 open */ 9676 static int 9677 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9678 { 9679 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9680 } 9681 9682 static int 9683 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9684 boolean_t isv6) 9685 { 9686 tcp_t *tcp = NULL; 9687 conn_t *connp; 9688 int err; 9689 vmem_t *minor_arena = NULL; 9690 dev_t conn_dev; 9691 zoneid_t zoneid; 9692 tcp_stack_t *tcps = NULL; 9693 9694 if (q->q_ptr != NULL) 9695 return (0); 9696 9697 if (sflag == MODOPEN) 9698 return (EINVAL); 9699 9700 if (!(flag & SO_ACCEPTOR)) { 9701 /* 9702 * Special case for install: miniroot needs to be able to 9703 * access files via NFS as though it were always in the 9704 * global zone. 9705 */ 9706 if (credp == kcred && nfs_global_client_only != 0) { 9707 zoneid = GLOBAL_ZONEID; 9708 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9709 netstack_tcp; 9710 ASSERT(tcps != NULL); 9711 } else { 9712 netstack_t *ns; 9713 9714 ns = netstack_find_by_cred(credp); 9715 ASSERT(ns != NULL); 9716 tcps = ns->netstack_tcp; 9717 ASSERT(tcps != NULL); 9718 9719 /* 9720 * For exclusive stacks we set the zoneid to zero 9721 * to make TCP operate as if in the global zone. 9722 */ 9723 if (tcps->tcps_netstack->netstack_stackid != 9724 GLOBAL_NETSTACKID) 9725 zoneid = GLOBAL_ZONEID; 9726 else 9727 zoneid = crgetzoneid(credp); 9728 } 9729 /* 9730 * For stackid zero this is done from strplumb.c, but 9731 * non-zero stackids are handled here. 9732 */ 9733 if (tcps->tcps_g_q == NULL && 9734 tcps->tcps_netstack->netstack_stackid != 9735 GLOBAL_NETSTACKID) { 9736 tcp_g_q_setup(tcps); 9737 } 9738 } 9739 9740 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9741 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9742 minor_arena = ip_minor_arena_la; 9743 } else { 9744 /* 9745 * Either minor numbers in the large arena were exhausted 9746 * or a non socket application is doing the open. 9747 * Try to allocate from the small arena. 9748 */ 9749 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9750 if (tcps != NULL) 9751 netstack_rele(tcps->tcps_netstack); 9752 return (EBUSY); 9753 } 9754 minor_arena = ip_minor_arena_sa; 9755 } 9756 ASSERT(minor_arena != NULL); 9757 9758 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9759 9760 if (flag & SO_ACCEPTOR) { 9761 /* No netstack_find_by_cred, hence no netstack_rele needed */ 9762 ASSERT(tcps == NULL); 9763 q->q_qinfo = &tcp_acceptor_rinit; 9764 /* 9765 * the conn_dev and minor_arena will be subsequently used by 9766 * tcp_wput_accept() and tcpclose_accept() to figure out the 9767 * minor device number for this connection from the q_ptr. 9768 */ 9769 RD(q)->q_ptr = (void *)conn_dev; 9770 WR(q)->q_qinfo = &tcp_acceptor_winit; 9771 WR(q)->q_ptr = (void *)minor_arena; 9772 qprocson(q); 9773 return (0); 9774 } 9775 9776 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9777 /* 9778 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9779 * so we drop it by one. 9780 */ 9781 netstack_rele(tcps->tcps_netstack); 9782 if (connp == NULL) { 9783 inet_minor_free(minor_arena, conn_dev); 9784 q->q_ptr = NULL; 9785 return (ENOSR); 9786 } 9787 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9788 tcp = connp->conn_tcp; 9789 9790 q->q_ptr = WR(q)->q_ptr = connp; 9791 if (isv6) { 9792 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9793 connp->conn_send = ip_output_v6; 9794 connp->conn_af_isv6 = B_TRUE; 9795 connp->conn_pkt_isv6 = B_TRUE; 9796 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9797 tcp->tcp_ipversion = IPV6_VERSION; 9798 tcp->tcp_family = AF_INET6; 9799 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9800 } else { 9801 connp->conn_flags |= IPCL_TCP4; 9802 connp->conn_send = ip_output; 9803 connp->conn_af_isv6 = B_FALSE; 9804 connp->conn_pkt_isv6 = B_FALSE; 9805 tcp->tcp_ipversion = IPV4_VERSION; 9806 tcp->tcp_family = AF_INET; 9807 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9808 } 9809 9810 /* 9811 * TCP keeps a copy of cred for cache locality reasons but 9812 * we put a reference only once. If connp->conn_cred 9813 * becomes invalid, tcp_cred should also be set to NULL. 9814 */ 9815 tcp->tcp_cred = connp->conn_cred = credp; 9816 crhold(connp->conn_cred); 9817 tcp->tcp_cpid = curproc->p_pid; 9818 tcp->tcp_open_time = lbolt64; 9819 connp->conn_zoneid = zoneid; 9820 connp->conn_mlp_type = mlptSingle; 9821 connp->conn_ulp_labeled = !is_system_labeled(); 9822 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9823 ASSERT(tcp->tcp_tcps == tcps); 9824 9825 /* 9826 * If the caller has the process-wide flag set, then default to MAC 9827 * exempt mode. This allows read-down to unlabeled hosts. 9828 */ 9829 if (getpflags(NET_MAC_AWARE, credp) != 0) 9830 connp->conn_mac_exempt = B_TRUE; 9831 9832 connp->conn_dev = conn_dev; 9833 connp->conn_minor_arena = minor_arena; 9834 9835 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9836 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9837 9838 if (flag & SO_SOCKSTR) { 9839 /* 9840 * No need to insert a socket in tcp acceptor hash. 9841 * If it was a socket acceptor stream, we dealt with 9842 * it above. A socket listener can never accept a 9843 * connection and doesn't need acceptor_id. 9844 */ 9845 connp->conn_flags |= IPCL_SOCKET; 9846 tcp->tcp_issocket = 1; 9847 WR(q)->q_qinfo = &tcp_sock_winit; 9848 } else { 9849 #ifdef _ILP32 9850 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9851 #else 9852 tcp->tcp_acceptor_id = conn_dev; 9853 #endif /* _ILP32 */ 9854 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9855 } 9856 9857 err = tcp_init(tcp, q); 9858 if (err != 0) { 9859 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 9860 tcp_acceptor_hash_remove(tcp); 9861 CONN_DEC_REF(connp); 9862 q->q_ptr = WR(q)->q_ptr = NULL; 9863 return (err); 9864 } 9865 9866 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9867 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9868 9869 /* Non-zero default values */ 9870 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9871 /* 9872 * Put the ref for TCP. Ref for IP was already put 9873 * by ipcl_conn_create. Also Make the conn_t globally 9874 * visible to walkers 9875 */ 9876 mutex_enter(&connp->conn_lock); 9877 CONN_INC_REF_LOCKED(connp); 9878 ASSERT(connp->conn_ref == 2); 9879 connp->conn_state_flags &= ~CONN_INCIPIENT; 9880 mutex_exit(&connp->conn_lock); 9881 9882 qprocson(q); 9883 return (0); 9884 } 9885 9886 /* 9887 * Some TCP options can be "set" by requesting them in the option 9888 * buffer. This is needed for XTI feature test though we do not 9889 * allow it in general. We interpret that this mechanism is more 9890 * applicable to OSI protocols and need not be allowed in general. 9891 * This routine filters out options for which it is not allowed (most) 9892 * and lets through those (few) for which it is. [ The XTI interface 9893 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9894 * ever implemented will have to be allowed here ]. 9895 */ 9896 static boolean_t 9897 tcp_allow_connopt_set(int level, int name) 9898 { 9899 9900 switch (level) { 9901 case IPPROTO_TCP: 9902 switch (name) { 9903 case TCP_NODELAY: 9904 return (B_TRUE); 9905 default: 9906 return (B_FALSE); 9907 } 9908 /*NOTREACHED*/ 9909 default: 9910 return (B_FALSE); 9911 } 9912 /*NOTREACHED*/ 9913 } 9914 9915 /* 9916 * This routine gets default values of certain options whose default 9917 * values are maintained by protocol specific code 9918 */ 9919 /* ARGSUSED */ 9920 int 9921 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9922 { 9923 int32_t *i1 = (int32_t *)ptr; 9924 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9925 9926 switch (level) { 9927 case IPPROTO_TCP: 9928 switch (name) { 9929 case TCP_NOTIFY_THRESHOLD: 9930 *i1 = tcps->tcps_ip_notify_interval; 9931 break; 9932 case TCP_ABORT_THRESHOLD: 9933 *i1 = tcps->tcps_ip_abort_interval; 9934 break; 9935 case TCP_CONN_NOTIFY_THRESHOLD: 9936 *i1 = tcps->tcps_ip_notify_cinterval; 9937 break; 9938 case TCP_CONN_ABORT_THRESHOLD: 9939 *i1 = tcps->tcps_ip_abort_cinterval; 9940 break; 9941 default: 9942 return (-1); 9943 } 9944 break; 9945 case IPPROTO_IP: 9946 switch (name) { 9947 case IP_TTL: 9948 *i1 = tcps->tcps_ipv4_ttl; 9949 break; 9950 default: 9951 return (-1); 9952 } 9953 break; 9954 case IPPROTO_IPV6: 9955 switch (name) { 9956 case IPV6_UNICAST_HOPS: 9957 *i1 = tcps->tcps_ipv6_hoplimit; 9958 break; 9959 default: 9960 return (-1); 9961 } 9962 break; 9963 default: 9964 return (-1); 9965 } 9966 return (sizeof (int)); 9967 } 9968 9969 9970 /* 9971 * TCP routine to get the values of options. 9972 */ 9973 int 9974 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9975 { 9976 int *i1 = (int *)ptr; 9977 conn_t *connp = Q_TO_CONN(q); 9978 tcp_t *tcp = connp->conn_tcp; 9979 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9980 9981 switch (level) { 9982 case SOL_SOCKET: 9983 switch (name) { 9984 case SO_LINGER: { 9985 struct linger *lgr = (struct linger *)ptr; 9986 9987 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9988 lgr->l_linger = tcp->tcp_lingertime; 9989 } 9990 return (sizeof (struct linger)); 9991 case SO_DEBUG: 9992 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9993 break; 9994 case SO_KEEPALIVE: 9995 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9996 break; 9997 case SO_DONTROUTE: 9998 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9999 break; 10000 case SO_USELOOPBACK: 10001 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 10002 break; 10003 case SO_BROADCAST: 10004 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 10005 break; 10006 case SO_REUSEADDR: 10007 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 10008 break; 10009 case SO_OOBINLINE: 10010 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 10011 break; 10012 case SO_DGRAM_ERRIND: 10013 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 10014 break; 10015 case SO_TYPE: 10016 *i1 = SOCK_STREAM; 10017 break; 10018 case SO_SNDBUF: 10019 *i1 = tcp->tcp_xmit_hiwater; 10020 break; 10021 case SO_RCVBUF: 10022 *i1 = RD(q)->q_hiwat; 10023 break; 10024 case SO_SND_COPYAVOID: 10025 *i1 = tcp->tcp_snd_zcopy_on ? 10026 SO_SND_COPYAVOID : 0; 10027 break; 10028 case SO_ALLZONES: 10029 *i1 = connp->conn_allzones ? 1 : 0; 10030 break; 10031 case SO_ANON_MLP: 10032 *i1 = connp->conn_anon_mlp; 10033 break; 10034 case SO_MAC_EXEMPT: 10035 *i1 = connp->conn_mac_exempt; 10036 break; 10037 case SO_EXCLBIND: 10038 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 10039 break; 10040 case SO_PROTOTYPE: 10041 *i1 = IPPROTO_TCP; 10042 break; 10043 case SO_DOMAIN: 10044 *i1 = tcp->tcp_family; 10045 break; 10046 default: 10047 return (-1); 10048 } 10049 break; 10050 case IPPROTO_TCP: 10051 switch (name) { 10052 case TCP_NODELAY: 10053 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 10054 break; 10055 case TCP_MAXSEG: 10056 *i1 = tcp->tcp_mss; 10057 break; 10058 case TCP_NOTIFY_THRESHOLD: 10059 *i1 = (int)tcp->tcp_first_timer_threshold; 10060 break; 10061 case TCP_ABORT_THRESHOLD: 10062 *i1 = tcp->tcp_second_timer_threshold; 10063 break; 10064 case TCP_CONN_NOTIFY_THRESHOLD: 10065 *i1 = tcp->tcp_first_ctimer_threshold; 10066 break; 10067 case TCP_CONN_ABORT_THRESHOLD: 10068 *i1 = tcp->tcp_second_ctimer_threshold; 10069 break; 10070 case TCP_RECVDSTADDR: 10071 *i1 = tcp->tcp_recvdstaddr; 10072 break; 10073 case TCP_ANONPRIVBIND: 10074 *i1 = tcp->tcp_anon_priv_bind; 10075 break; 10076 case TCP_EXCLBIND: 10077 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 10078 break; 10079 case TCP_INIT_CWND: 10080 *i1 = tcp->tcp_init_cwnd; 10081 break; 10082 case TCP_KEEPALIVE_THRESHOLD: 10083 *i1 = tcp->tcp_ka_interval; 10084 break; 10085 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10086 *i1 = tcp->tcp_ka_abort_thres; 10087 break; 10088 case TCP_CORK: 10089 *i1 = tcp->tcp_cork; 10090 break; 10091 default: 10092 return (-1); 10093 } 10094 break; 10095 case IPPROTO_IP: 10096 if (tcp->tcp_family != AF_INET) 10097 return (-1); 10098 switch (name) { 10099 case IP_OPTIONS: 10100 case T_IP_OPTIONS: { 10101 /* 10102 * This is compatible with BSD in that in only return 10103 * the reverse source route with the final destination 10104 * as the last entry. The first 4 bytes of the option 10105 * will contain the final destination. 10106 */ 10107 int opt_len; 10108 10109 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 10110 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 10111 ASSERT(opt_len >= 0); 10112 /* Caller ensures enough space */ 10113 if (opt_len > 0) { 10114 /* 10115 * TODO: Do we have to handle getsockopt on an 10116 * initiator as well? 10117 */ 10118 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 10119 } 10120 return (0); 10121 } 10122 case IP_TOS: 10123 case T_IP_TOS: 10124 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 10125 break; 10126 case IP_TTL: 10127 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 10128 break; 10129 case IP_NEXTHOP: 10130 /* Handled at IP level */ 10131 return (-EINVAL); 10132 default: 10133 return (-1); 10134 } 10135 break; 10136 case IPPROTO_IPV6: 10137 /* 10138 * IPPROTO_IPV6 options are only supported for sockets 10139 * that are using IPv6 on the wire. 10140 */ 10141 if (tcp->tcp_ipversion != IPV6_VERSION) { 10142 return (-1); 10143 } 10144 switch (name) { 10145 case IPV6_UNICAST_HOPS: 10146 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 10147 break; /* goto sizeof (int) option return */ 10148 case IPV6_BOUND_IF: 10149 /* Zero if not set */ 10150 *i1 = tcp->tcp_bound_if; 10151 break; /* goto sizeof (int) option return */ 10152 case IPV6_RECVPKTINFO: 10153 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 10154 *i1 = 1; 10155 else 10156 *i1 = 0; 10157 break; /* goto sizeof (int) option return */ 10158 case IPV6_RECVTCLASS: 10159 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 10160 *i1 = 1; 10161 else 10162 *i1 = 0; 10163 break; /* goto sizeof (int) option return */ 10164 case IPV6_RECVHOPLIMIT: 10165 if (tcp->tcp_ipv6_recvancillary & 10166 TCP_IPV6_RECVHOPLIMIT) 10167 *i1 = 1; 10168 else 10169 *i1 = 0; 10170 break; /* goto sizeof (int) option return */ 10171 case IPV6_RECVHOPOPTS: 10172 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 10173 *i1 = 1; 10174 else 10175 *i1 = 0; 10176 break; /* goto sizeof (int) option return */ 10177 case IPV6_RECVDSTOPTS: 10178 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 10179 *i1 = 1; 10180 else 10181 *i1 = 0; 10182 break; /* goto sizeof (int) option return */ 10183 case _OLD_IPV6_RECVDSTOPTS: 10184 if (tcp->tcp_ipv6_recvancillary & 10185 TCP_OLD_IPV6_RECVDSTOPTS) 10186 *i1 = 1; 10187 else 10188 *i1 = 0; 10189 break; /* goto sizeof (int) option return */ 10190 case IPV6_RECVRTHDR: 10191 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 10192 *i1 = 1; 10193 else 10194 *i1 = 0; 10195 break; /* goto sizeof (int) option return */ 10196 case IPV6_RECVRTHDRDSTOPTS: 10197 if (tcp->tcp_ipv6_recvancillary & 10198 TCP_IPV6_RECVRTDSTOPTS) 10199 *i1 = 1; 10200 else 10201 *i1 = 0; 10202 break; /* goto sizeof (int) option return */ 10203 case IPV6_PKTINFO: { 10204 /* XXX assumes that caller has room for max size! */ 10205 struct in6_pktinfo *pkti; 10206 10207 pkti = (struct in6_pktinfo *)ptr; 10208 if (ipp->ipp_fields & IPPF_IFINDEX) 10209 pkti->ipi6_ifindex = ipp->ipp_ifindex; 10210 else 10211 pkti->ipi6_ifindex = 0; 10212 if (ipp->ipp_fields & IPPF_ADDR) 10213 pkti->ipi6_addr = ipp->ipp_addr; 10214 else 10215 pkti->ipi6_addr = ipv6_all_zeros; 10216 return (sizeof (struct in6_pktinfo)); 10217 } 10218 case IPV6_TCLASS: 10219 if (ipp->ipp_fields & IPPF_TCLASS) 10220 *i1 = ipp->ipp_tclass; 10221 else 10222 *i1 = IPV6_FLOW_TCLASS( 10223 IPV6_DEFAULT_VERS_AND_FLOW); 10224 break; /* goto sizeof (int) option return */ 10225 case IPV6_NEXTHOP: { 10226 sin6_t *sin6 = (sin6_t *)ptr; 10227 10228 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 10229 return (0); 10230 *sin6 = sin6_null; 10231 sin6->sin6_family = AF_INET6; 10232 sin6->sin6_addr = ipp->ipp_nexthop; 10233 return (sizeof (sin6_t)); 10234 } 10235 case IPV6_HOPOPTS: 10236 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 10237 return (0); 10238 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 10239 return (0); 10240 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 10241 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 10242 if (tcp->tcp_label_len > 0) { 10243 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 10244 ptr[1] = (ipp->ipp_hopoptslen - 10245 tcp->tcp_label_len + 7) / 8 - 1; 10246 } 10247 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 10248 case IPV6_RTHDRDSTOPTS: 10249 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 10250 return (0); 10251 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 10252 return (ipp->ipp_rtdstoptslen); 10253 case IPV6_RTHDR: 10254 if (!(ipp->ipp_fields & IPPF_RTHDR)) 10255 return (0); 10256 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 10257 return (ipp->ipp_rthdrlen); 10258 case IPV6_DSTOPTS: 10259 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 10260 return (0); 10261 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 10262 return (ipp->ipp_dstoptslen); 10263 case IPV6_SRC_PREFERENCES: 10264 return (ip6_get_src_preferences(connp, 10265 (uint32_t *)ptr)); 10266 case IPV6_PATHMTU: { 10267 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 10268 10269 if (tcp->tcp_state < TCPS_ESTABLISHED) 10270 return (-1); 10271 10272 return (ip_fill_mtuinfo(&connp->conn_remv6, 10273 connp->conn_fport, mtuinfo, 10274 connp->conn_netstack)); 10275 } 10276 default: 10277 return (-1); 10278 } 10279 break; 10280 default: 10281 return (-1); 10282 } 10283 return (sizeof (int)); 10284 } 10285 10286 /* 10287 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 10288 * Parameters are assumed to be verified by the caller. 10289 */ 10290 /* ARGSUSED */ 10291 int 10292 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10293 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10294 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10295 { 10296 conn_t *connp = Q_TO_CONN(q); 10297 tcp_t *tcp = connp->conn_tcp; 10298 int *i1 = (int *)invalp; 10299 boolean_t onoff = (*i1 == 0) ? 0 : 1; 10300 boolean_t checkonly; 10301 int reterr; 10302 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 10303 10304 switch (optset_context) { 10305 case SETFN_OPTCOM_CHECKONLY: 10306 checkonly = B_TRUE; 10307 /* 10308 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10309 * inlen != 0 implies value supplied and 10310 * we have to "pretend" to set it. 10311 * inlen == 0 implies that there is no 10312 * value part in T_CHECK request and just validation 10313 * done elsewhere should be enough, we just return here. 10314 */ 10315 if (inlen == 0) { 10316 *outlenp = 0; 10317 return (0); 10318 } 10319 break; 10320 case SETFN_OPTCOM_NEGOTIATE: 10321 checkonly = B_FALSE; 10322 break; 10323 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 10324 case SETFN_CONN_NEGOTIATE: 10325 checkonly = B_FALSE; 10326 /* 10327 * Negotiating local and "association-related" options 10328 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 10329 * primitives is allowed by XTI, but we choose 10330 * to not implement this style negotiation for Internet 10331 * protocols (We interpret it is a must for OSI world but 10332 * optional for Internet protocols) for all options. 10333 * [ Will do only for the few options that enable test 10334 * suites that our XTI implementation of this feature 10335 * works for transports that do allow it ] 10336 */ 10337 if (!tcp_allow_connopt_set(level, name)) { 10338 *outlenp = 0; 10339 return (EINVAL); 10340 } 10341 break; 10342 default: 10343 /* 10344 * We should never get here 10345 */ 10346 *outlenp = 0; 10347 return (EINVAL); 10348 } 10349 10350 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10351 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10352 10353 /* 10354 * For TCP, we should have no ancillary data sent down 10355 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10356 * has to be zero. 10357 */ 10358 ASSERT(thisdg_attrs == NULL); 10359 10360 /* 10361 * For fixed length options, no sanity check 10362 * of passed in length is done. It is assumed *_optcom_req() 10363 * routines do the right thing. 10364 */ 10365 10366 switch (level) { 10367 case SOL_SOCKET: 10368 switch (name) { 10369 case SO_LINGER: { 10370 struct linger *lgr = (struct linger *)invalp; 10371 10372 if (!checkonly) { 10373 if (lgr->l_onoff) { 10374 tcp->tcp_linger = 1; 10375 tcp->tcp_lingertime = lgr->l_linger; 10376 } else { 10377 tcp->tcp_linger = 0; 10378 tcp->tcp_lingertime = 0; 10379 } 10380 /* struct copy */ 10381 *(struct linger *)outvalp = *lgr; 10382 } else { 10383 if (!lgr->l_onoff) { 10384 ((struct linger *) 10385 outvalp)->l_onoff = 0; 10386 ((struct linger *) 10387 outvalp)->l_linger = 0; 10388 } else { 10389 /* struct copy */ 10390 *(struct linger *)outvalp = *lgr; 10391 } 10392 } 10393 *outlenp = sizeof (struct linger); 10394 return (0); 10395 } 10396 case SO_DEBUG: 10397 if (!checkonly) 10398 tcp->tcp_debug = onoff; 10399 break; 10400 case SO_KEEPALIVE: 10401 if (checkonly) { 10402 /* T_CHECK case */ 10403 break; 10404 } 10405 10406 if (!onoff) { 10407 if (tcp->tcp_ka_enabled) { 10408 if (tcp->tcp_ka_tid != 0) { 10409 (void) TCP_TIMER_CANCEL(tcp, 10410 tcp->tcp_ka_tid); 10411 tcp->tcp_ka_tid = 0; 10412 } 10413 tcp->tcp_ka_enabled = 0; 10414 } 10415 break; 10416 } 10417 if (!tcp->tcp_ka_enabled) { 10418 /* Crank up the keepalive timer */ 10419 tcp->tcp_ka_last_intrvl = 0; 10420 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10421 tcp_keepalive_killer, 10422 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10423 tcp->tcp_ka_enabled = 1; 10424 } 10425 break; 10426 case SO_DONTROUTE: 10427 /* 10428 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10429 * only of interest to IP. We track them here only so 10430 * that we can report their current value. 10431 */ 10432 if (!checkonly) { 10433 tcp->tcp_dontroute = onoff; 10434 tcp->tcp_connp->conn_dontroute = onoff; 10435 } 10436 break; 10437 case SO_USELOOPBACK: 10438 if (!checkonly) { 10439 tcp->tcp_useloopback = onoff; 10440 tcp->tcp_connp->conn_loopback = onoff; 10441 } 10442 break; 10443 case SO_BROADCAST: 10444 if (!checkonly) { 10445 tcp->tcp_broadcast = onoff; 10446 tcp->tcp_connp->conn_broadcast = onoff; 10447 } 10448 break; 10449 case SO_REUSEADDR: 10450 if (!checkonly) { 10451 tcp->tcp_reuseaddr = onoff; 10452 tcp->tcp_connp->conn_reuseaddr = onoff; 10453 } 10454 break; 10455 case SO_OOBINLINE: 10456 if (!checkonly) 10457 tcp->tcp_oobinline = onoff; 10458 break; 10459 case SO_DGRAM_ERRIND: 10460 if (!checkonly) 10461 tcp->tcp_dgram_errind = onoff; 10462 break; 10463 case SO_SNDBUF: { 10464 if (*i1 > tcps->tcps_max_buf) { 10465 *outlenp = 0; 10466 return (ENOBUFS); 10467 } 10468 if (checkonly) 10469 break; 10470 10471 tcp->tcp_xmit_hiwater = *i1; 10472 if (tcps->tcps_snd_lowat_fraction != 0) 10473 tcp->tcp_xmit_lowater = 10474 tcp->tcp_xmit_hiwater / 10475 tcps->tcps_snd_lowat_fraction; 10476 (void) tcp_maxpsz_set(tcp, B_TRUE); 10477 /* 10478 * If we are flow-controlled, recheck the condition. 10479 * There are apps that increase SO_SNDBUF size when 10480 * flow-controlled (EWOULDBLOCK), and expect the flow 10481 * control condition to be lifted right away. 10482 */ 10483 mutex_enter(&tcp->tcp_non_sq_lock); 10484 if (tcp->tcp_flow_stopped && 10485 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10486 tcp_clrqfull(tcp); 10487 } 10488 mutex_exit(&tcp->tcp_non_sq_lock); 10489 break; 10490 } 10491 case SO_RCVBUF: 10492 if (*i1 > tcps->tcps_max_buf) { 10493 *outlenp = 0; 10494 return (ENOBUFS); 10495 } 10496 /* Silently ignore zero */ 10497 if (!checkonly && *i1 != 0) { 10498 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10499 (void) tcp_rwnd_set(tcp, *i1); 10500 } 10501 /* 10502 * XXX should we return the rwnd here 10503 * and tcp_opt_get ? 10504 */ 10505 break; 10506 case SO_SND_COPYAVOID: 10507 if (!checkonly) { 10508 /* we only allow enable at most once for now */ 10509 if (tcp->tcp_loopback || 10510 (tcp->tcp_kssl_ctx != NULL) || 10511 (!tcp->tcp_snd_zcopy_aware && 10512 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10513 *outlenp = 0; 10514 return (EOPNOTSUPP); 10515 } 10516 tcp->tcp_snd_zcopy_aware = 1; 10517 } 10518 break; 10519 case SO_ALLZONES: 10520 /* Pass option along to IP level for handling */ 10521 return (-EINVAL); 10522 case SO_ANON_MLP: 10523 /* Pass option along to IP level for handling */ 10524 return (-EINVAL); 10525 case SO_MAC_EXEMPT: 10526 /* Pass option along to IP level for handling */ 10527 return (-EINVAL); 10528 case SO_EXCLBIND: 10529 if (!checkonly) 10530 tcp->tcp_exclbind = onoff; 10531 break; 10532 default: 10533 *outlenp = 0; 10534 return (EINVAL); 10535 } 10536 break; 10537 case IPPROTO_TCP: 10538 switch (name) { 10539 case TCP_NODELAY: 10540 if (!checkonly) 10541 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10542 break; 10543 case TCP_NOTIFY_THRESHOLD: 10544 if (!checkonly) 10545 tcp->tcp_first_timer_threshold = *i1; 10546 break; 10547 case TCP_ABORT_THRESHOLD: 10548 if (!checkonly) 10549 tcp->tcp_second_timer_threshold = *i1; 10550 break; 10551 case TCP_CONN_NOTIFY_THRESHOLD: 10552 if (!checkonly) 10553 tcp->tcp_first_ctimer_threshold = *i1; 10554 break; 10555 case TCP_CONN_ABORT_THRESHOLD: 10556 if (!checkonly) 10557 tcp->tcp_second_ctimer_threshold = *i1; 10558 break; 10559 case TCP_RECVDSTADDR: 10560 if (tcp->tcp_state > TCPS_LISTEN) 10561 return (EOPNOTSUPP); 10562 if (!checkonly) 10563 tcp->tcp_recvdstaddr = onoff; 10564 break; 10565 case TCP_ANONPRIVBIND: 10566 if ((reterr = secpolicy_net_privaddr(cr, 0, 10567 IPPROTO_TCP)) != 0) { 10568 *outlenp = 0; 10569 return (reterr); 10570 } 10571 if (!checkonly) { 10572 tcp->tcp_anon_priv_bind = onoff; 10573 } 10574 break; 10575 case TCP_EXCLBIND: 10576 if (!checkonly) 10577 tcp->tcp_exclbind = onoff; 10578 break; /* goto sizeof (int) option return */ 10579 case TCP_INIT_CWND: { 10580 uint32_t init_cwnd = *((uint32_t *)invalp); 10581 10582 if (checkonly) 10583 break; 10584 10585 /* 10586 * Only allow socket with network configuration 10587 * privilege to set the initial cwnd to be larger 10588 * than allowed by RFC 3390. 10589 */ 10590 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10591 tcp->tcp_init_cwnd = init_cwnd; 10592 break; 10593 } 10594 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10595 *outlenp = 0; 10596 return (reterr); 10597 } 10598 if (init_cwnd > TCP_MAX_INIT_CWND) { 10599 *outlenp = 0; 10600 return (EINVAL); 10601 } 10602 tcp->tcp_init_cwnd = init_cwnd; 10603 break; 10604 } 10605 case TCP_KEEPALIVE_THRESHOLD: 10606 if (checkonly) 10607 break; 10608 10609 if (*i1 < tcps->tcps_keepalive_interval_low || 10610 *i1 > tcps->tcps_keepalive_interval_high) { 10611 *outlenp = 0; 10612 return (EINVAL); 10613 } 10614 if (*i1 != tcp->tcp_ka_interval) { 10615 tcp->tcp_ka_interval = *i1; 10616 /* 10617 * Check if we need to restart the 10618 * keepalive timer. 10619 */ 10620 if (tcp->tcp_ka_tid != 0) { 10621 ASSERT(tcp->tcp_ka_enabled); 10622 (void) TCP_TIMER_CANCEL(tcp, 10623 tcp->tcp_ka_tid); 10624 tcp->tcp_ka_last_intrvl = 0; 10625 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10626 tcp_keepalive_killer, 10627 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10628 } 10629 } 10630 break; 10631 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10632 if (!checkonly) { 10633 if (*i1 < 10634 tcps->tcps_keepalive_abort_interval_low || 10635 *i1 > 10636 tcps->tcps_keepalive_abort_interval_high) { 10637 *outlenp = 0; 10638 return (EINVAL); 10639 } 10640 tcp->tcp_ka_abort_thres = *i1; 10641 } 10642 break; 10643 case TCP_CORK: 10644 if (!checkonly) { 10645 /* 10646 * if tcp->tcp_cork was set and is now 10647 * being unset, we have to make sure that 10648 * the remaining data gets sent out. Also 10649 * unset tcp->tcp_cork so that tcp_wput_data() 10650 * can send data even if it is less than mss 10651 */ 10652 if (tcp->tcp_cork && onoff == 0 && 10653 tcp->tcp_unsent > 0) { 10654 tcp->tcp_cork = B_FALSE; 10655 tcp_wput_data(tcp, NULL, B_FALSE); 10656 } 10657 tcp->tcp_cork = onoff; 10658 } 10659 break; 10660 default: 10661 *outlenp = 0; 10662 return (EINVAL); 10663 } 10664 break; 10665 case IPPROTO_IP: 10666 if (tcp->tcp_family != AF_INET) { 10667 *outlenp = 0; 10668 return (ENOPROTOOPT); 10669 } 10670 switch (name) { 10671 case IP_OPTIONS: 10672 case T_IP_OPTIONS: 10673 reterr = tcp_opt_set_header(tcp, checkonly, 10674 invalp, inlen); 10675 if (reterr) { 10676 *outlenp = 0; 10677 return (reterr); 10678 } 10679 /* OK return - copy input buffer into output buffer */ 10680 if (invalp != outvalp) { 10681 /* don't trust bcopy for identical src/dst */ 10682 bcopy(invalp, outvalp, inlen); 10683 } 10684 *outlenp = inlen; 10685 return (0); 10686 case IP_TOS: 10687 case T_IP_TOS: 10688 if (!checkonly) { 10689 tcp->tcp_ipha->ipha_type_of_service = 10690 (uchar_t)*i1; 10691 tcp->tcp_tos = (uchar_t)*i1; 10692 } 10693 break; 10694 case IP_TTL: 10695 if (!checkonly) { 10696 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10697 tcp->tcp_ttl = (uchar_t)*i1; 10698 } 10699 break; 10700 case IP_BOUND_IF: 10701 case IP_NEXTHOP: 10702 /* Handled at the IP level */ 10703 return (-EINVAL); 10704 case IP_SEC_OPT: 10705 /* 10706 * We should not allow policy setting after 10707 * we start listening for connections. 10708 */ 10709 if (tcp->tcp_state == TCPS_LISTEN) { 10710 return (EINVAL); 10711 } else { 10712 /* Handled at the IP level */ 10713 return (-EINVAL); 10714 } 10715 default: 10716 *outlenp = 0; 10717 return (EINVAL); 10718 } 10719 break; 10720 case IPPROTO_IPV6: { 10721 ip6_pkt_t *ipp; 10722 10723 /* 10724 * IPPROTO_IPV6 options are only supported for sockets 10725 * that are using IPv6 on the wire. 10726 */ 10727 if (tcp->tcp_ipversion != IPV6_VERSION) { 10728 *outlenp = 0; 10729 return (ENOPROTOOPT); 10730 } 10731 /* 10732 * Only sticky options; no ancillary data 10733 */ 10734 ASSERT(thisdg_attrs == NULL); 10735 ipp = &tcp->tcp_sticky_ipp; 10736 10737 switch (name) { 10738 case IPV6_UNICAST_HOPS: 10739 /* -1 means use default */ 10740 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10741 *outlenp = 0; 10742 return (EINVAL); 10743 } 10744 if (!checkonly) { 10745 if (*i1 == -1) { 10746 tcp->tcp_ip6h->ip6_hops = 10747 ipp->ipp_unicast_hops = 10748 (uint8_t)tcps->tcps_ipv6_hoplimit; 10749 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10750 /* Pass modified value to IP. */ 10751 *i1 = tcp->tcp_ip6h->ip6_hops; 10752 } else { 10753 tcp->tcp_ip6h->ip6_hops = 10754 ipp->ipp_unicast_hops = 10755 (uint8_t)*i1; 10756 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10757 } 10758 reterr = tcp_build_hdrs(q, tcp); 10759 if (reterr != 0) 10760 return (reterr); 10761 } 10762 break; 10763 case IPV6_BOUND_IF: 10764 if (!checkonly) { 10765 int error = 0; 10766 10767 tcp->tcp_bound_if = *i1; 10768 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10769 B_TRUE, checkonly, level, name, mblk); 10770 if (error != 0) { 10771 *outlenp = 0; 10772 return (error); 10773 } 10774 } 10775 break; 10776 /* 10777 * Set boolean switches for ancillary data delivery 10778 */ 10779 case IPV6_RECVPKTINFO: 10780 if (!checkonly) { 10781 if (onoff) 10782 tcp->tcp_ipv6_recvancillary |= 10783 TCP_IPV6_RECVPKTINFO; 10784 else 10785 tcp->tcp_ipv6_recvancillary &= 10786 ~TCP_IPV6_RECVPKTINFO; 10787 /* Force it to be sent up with the next msg */ 10788 tcp->tcp_recvifindex = 0; 10789 } 10790 break; 10791 case IPV6_RECVTCLASS: 10792 if (!checkonly) { 10793 if (onoff) 10794 tcp->tcp_ipv6_recvancillary |= 10795 TCP_IPV6_RECVTCLASS; 10796 else 10797 tcp->tcp_ipv6_recvancillary &= 10798 ~TCP_IPV6_RECVTCLASS; 10799 } 10800 break; 10801 case IPV6_RECVHOPLIMIT: 10802 if (!checkonly) { 10803 if (onoff) 10804 tcp->tcp_ipv6_recvancillary |= 10805 TCP_IPV6_RECVHOPLIMIT; 10806 else 10807 tcp->tcp_ipv6_recvancillary &= 10808 ~TCP_IPV6_RECVHOPLIMIT; 10809 /* Force it to be sent up with the next msg */ 10810 tcp->tcp_recvhops = 0xffffffffU; 10811 } 10812 break; 10813 case IPV6_RECVHOPOPTS: 10814 if (!checkonly) { 10815 if (onoff) 10816 tcp->tcp_ipv6_recvancillary |= 10817 TCP_IPV6_RECVHOPOPTS; 10818 else 10819 tcp->tcp_ipv6_recvancillary &= 10820 ~TCP_IPV6_RECVHOPOPTS; 10821 } 10822 break; 10823 case IPV6_RECVDSTOPTS: 10824 if (!checkonly) { 10825 if (onoff) 10826 tcp->tcp_ipv6_recvancillary |= 10827 TCP_IPV6_RECVDSTOPTS; 10828 else 10829 tcp->tcp_ipv6_recvancillary &= 10830 ~TCP_IPV6_RECVDSTOPTS; 10831 } 10832 break; 10833 case _OLD_IPV6_RECVDSTOPTS: 10834 if (!checkonly) { 10835 if (onoff) 10836 tcp->tcp_ipv6_recvancillary |= 10837 TCP_OLD_IPV6_RECVDSTOPTS; 10838 else 10839 tcp->tcp_ipv6_recvancillary &= 10840 ~TCP_OLD_IPV6_RECVDSTOPTS; 10841 } 10842 break; 10843 case IPV6_RECVRTHDR: 10844 if (!checkonly) { 10845 if (onoff) 10846 tcp->tcp_ipv6_recvancillary |= 10847 TCP_IPV6_RECVRTHDR; 10848 else 10849 tcp->tcp_ipv6_recvancillary &= 10850 ~TCP_IPV6_RECVRTHDR; 10851 } 10852 break; 10853 case IPV6_RECVRTHDRDSTOPTS: 10854 if (!checkonly) { 10855 if (onoff) 10856 tcp->tcp_ipv6_recvancillary |= 10857 TCP_IPV6_RECVRTDSTOPTS; 10858 else 10859 tcp->tcp_ipv6_recvancillary &= 10860 ~TCP_IPV6_RECVRTDSTOPTS; 10861 } 10862 break; 10863 case IPV6_PKTINFO: 10864 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10865 return (EINVAL); 10866 if (checkonly) 10867 break; 10868 10869 if (inlen == 0) { 10870 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10871 } else { 10872 struct in6_pktinfo *pkti; 10873 10874 pkti = (struct in6_pktinfo *)invalp; 10875 /* 10876 * RFC 3542 states that ipi6_addr must be 10877 * the unspecified address when setting the 10878 * IPV6_PKTINFO sticky socket option on a 10879 * TCP socket. 10880 */ 10881 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10882 return (EINVAL); 10883 /* 10884 * ip6_set_pktinfo() validates the source 10885 * address and interface index. 10886 */ 10887 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10888 pkti, mblk); 10889 if (reterr != 0) 10890 return (reterr); 10891 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10892 ipp->ipp_addr = pkti->ipi6_addr; 10893 if (ipp->ipp_ifindex != 0) 10894 ipp->ipp_fields |= IPPF_IFINDEX; 10895 else 10896 ipp->ipp_fields &= ~IPPF_IFINDEX; 10897 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10898 ipp->ipp_fields |= IPPF_ADDR; 10899 else 10900 ipp->ipp_fields &= ~IPPF_ADDR; 10901 } 10902 reterr = tcp_build_hdrs(q, tcp); 10903 if (reterr != 0) 10904 return (reterr); 10905 break; 10906 case IPV6_TCLASS: 10907 if (inlen != 0 && inlen != sizeof (int)) 10908 return (EINVAL); 10909 if (checkonly) 10910 break; 10911 10912 if (inlen == 0) { 10913 ipp->ipp_fields &= ~IPPF_TCLASS; 10914 } else { 10915 if (*i1 > 255 || *i1 < -1) 10916 return (EINVAL); 10917 if (*i1 == -1) { 10918 ipp->ipp_tclass = 0; 10919 *i1 = 0; 10920 } else { 10921 ipp->ipp_tclass = *i1; 10922 } 10923 ipp->ipp_fields |= IPPF_TCLASS; 10924 } 10925 reterr = tcp_build_hdrs(q, tcp); 10926 if (reterr != 0) 10927 return (reterr); 10928 break; 10929 case IPV6_NEXTHOP: 10930 /* 10931 * IP will verify that the nexthop is reachable 10932 * and fail for sticky options. 10933 */ 10934 if (inlen != 0 && inlen != sizeof (sin6_t)) 10935 return (EINVAL); 10936 if (checkonly) 10937 break; 10938 10939 if (inlen == 0) { 10940 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10941 } else { 10942 sin6_t *sin6 = (sin6_t *)invalp; 10943 10944 if (sin6->sin6_family != AF_INET6) 10945 return (EAFNOSUPPORT); 10946 if (IN6_IS_ADDR_V4MAPPED( 10947 &sin6->sin6_addr)) 10948 return (EADDRNOTAVAIL); 10949 ipp->ipp_nexthop = sin6->sin6_addr; 10950 if (!IN6_IS_ADDR_UNSPECIFIED( 10951 &ipp->ipp_nexthop)) 10952 ipp->ipp_fields |= IPPF_NEXTHOP; 10953 else 10954 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10955 } 10956 reterr = tcp_build_hdrs(q, tcp); 10957 if (reterr != 0) 10958 return (reterr); 10959 break; 10960 case IPV6_HOPOPTS: { 10961 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10962 10963 /* 10964 * Sanity checks - minimum size, size a multiple of 10965 * eight bytes, and matching size passed in. 10966 */ 10967 if (inlen != 0 && 10968 inlen != (8 * (hopts->ip6h_len + 1))) 10969 return (EINVAL); 10970 10971 if (checkonly) 10972 break; 10973 10974 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10975 (uchar_t **)&ipp->ipp_hopopts, 10976 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10977 if (reterr != 0) 10978 return (reterr); 10979 if (ipp->ipp_hopoptslen == 0) 10980 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10981 else 10982 ipp->ipp_fields |= IPPF_HOPOPTS; 10983 reterr = tcp_build_hdrs(q, tcp); 10984 if (reterr != 0) 10985 return (reterr); 10986 break; 10987 } 10988 case IPV6_RTHDRDSTOPTS: { 10989 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10990 10991 /* 10992 * Sanity checks - minimum size, size a multiple of 10993 * eight bytes, and matching size passed in. 10994 */ 10995 if (inlen != 0 && 10996 inlen != (8 * (dopts->ip6d_len + 1))) 10997 return (EINVAL); 10998 10999 if (checkonly) 11000 break; 11001 11002 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 11003 (uchar_t **)&ipp->ipp_rtdstopts, 11004 &ipp->ipp_rtdstoptslen, 0); 11005 if (reterr != 0) 11006 return (reterr); 11007 if (ipp->ipp_rtdstoptslen == 0) 11008 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 11009 else 11010 ipp->ipp_fields |= IPPF_RTDSTOPTS; 11011 reterr = tcp_build_hdrs(q, tcp); 11012 if (reterr != 0) 11013 return (reterr); 11014 break; 11015 } 11016 case IPV6_DSTOPTS: { 11017 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 11018 11019 /* 11020 * Sanity checks - minimum size, size a multiple of 11021 * eight bytes, and matching size passed in. 11022 */ 11023 if (inlen != 0 && 11024 inlen != (8 * (dopts->ip6d_len + 1))) 11025 return (EINVAL); 11026 11027 if (checkonly) 11028 break; 11029 11030 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 11031 (uchar_t **)&ipp->ipp_dstopts, 11032 &ipp->ipp_dstoptslen, 0); 11033 if (reterr != 0) 11034 return (reterr); 11035 if (ipp->ipp_dstoptslen == 0) 11036 ipp->ipp_fields &= ~IPPF_DSTOPTS; 11037 else 11038 ipp->ipp_fields |= IPPF_DSTOPTS; 11039 reterr = tcp_build_hdrs(q, tcp); 11040 if (reterr != 0) 11041 return (reterr); 11042 break; 11043 } 11044 case IPV6_RTHDR: { 11045 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 11046 11047 /* 11048 * Sanity checks - minimum size, size a multiple of 11049 * eight bytes, and matching size passed in. 11050 */ 11051 if (inlen != 0 && 11052 inlen != (8 * (rt->ip6r_len + 1))) 11053 return (EINVAL); 11054 11055 if (checkonly) 11056 break; 11057 11058 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 11059 (uchar_t **)&ipp->ipp_rthdr, 11060 &ipp->ipp_rthdrlen, 0); 11061 if (reterr != 0) 11062 return (reterr); 11063 if (ipp->ipp_rthdrlen == 0) 11064 ipp->ipp_fields &= ~IPPF_RTHDR; 11065 else 11066 ipp->ipp_fields |= IPPF_RTHDR; 11067 reterr = tcp_build_hdrs(q, tcp); 11068 if (reterr != 0) 11069 return (reterr); 11070 break; 11071 } 11072 case IPV6_V6ONLY: 11073 if (!checkonly) 11074 tcp->tcp_connp->conn_ipv6_v6only = onoff; 11075 break; 11076 case IPV6_USE_MIN_MTU: 11077 if (inlen != sizeof (int)) 11078 return (EINVAL); 11079 11080 if (*i1 < -1 || *i1 > 1) 11081 return (EINVAL); 11082 11083 if (checkonly) 11084 break; 11085 11086 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 11087 ipp->ipp_use_min_mtu = *i1; 11088 break; 11089 case IPV6_BOUND_PIF: 11090 /* Handled at the IP level */ 11091 return (-EINVAL); 11092 case IPV6_SEC_OPT: 11093 /* 11094 * We should not allow policy setting after 11095 * we start listening for connections. 11096 */ 11097 if (tcp->tcp_state == TCPS_LISTEN) { 11098 return (EINVAL); 11099 } else { 11100 /* Handled at the IP level */ 11101 return (-EINVAL); 11102 } 11103 case IPV6_SRC_PREFERENCES: 11104 if (inlen != sizeof (uint32_t)) 11105 return (EINVAL); 11106 reterr = ip6_set_src_preferences(tcp->tcp_connp, 11107 *(uint32_t *)invalp); 11108 if (reterr != 0) { 11109 *outlenp = 0; 11110 return (reterr); 11111 } 11112 break; 11113 default: 11114 *outlenp = 0; 11115 return (EINVAL); 11116 } 11117 break; 11118 } /* end IPPROTO_IPV6 */ 11119 default: 11120 *outlenp = 0; 11121 return (EINVAL); 11122 } 11123 /* 11124 * Common case of OK return with outval same as inval 11125 */ 11126 if (invalp != outvalp) { 11127 /* don't trust bcopy for identical src/dst */ 11128 (void) bcopy(invalp, outvalp, inlen); 11129 } 11130 *outlenp = inlen; 11131 return (0); 11132 } 11133 11134 /* 11135 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 11136 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 11137 * headers, and the maximum size tcp header (to avoid reallocation 11138 * on the fly for additional tcp options). 11139 * Returns failure if can't allocate memory. 11140 */ 11141 static int 11142 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 11143 { 11144 char *hdrs; 11145 uint_t hdrs_len; 11146 ip6i_t *ip6i; 11147 char buf[TCP_MAX_HDR_LENGTH]; 11148 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 11149 in6_addr_t src, dst; 11150 tcp_stack_t *tcps = tcp->tcp_tcps; 11151 11152 /* 11153 * save the existing tcp header and source/dest IP addresses 11154 */ 11155 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 11156 src = tcp->tcp_ip6h->ip6_src; 11157 dst = tcp->tcp_ip6h->ip6_dst; 11158 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 11159 ASSERT(hdrs_len != 0); 11160 if (hdrs_len > tcp->tcp_iphc_len) { 11161 /* Need to reallocate */ 11162 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 11163 if (hdrs == NULL) 11164 return (ENOMEM); 11165 if (tcp->tcp_iphc != NULL) { 11166 if (tcp->tcp_hdr_grown) { 11167 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 11168 } else { 11169 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 11170 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 11171 } 11172 tcp->tcp_iphc_len = 0; 11173 } 11174 ASSERT(tcp->tcp_iphc_len == 0); 11175 tcp->tcp_iphc = hdrs; 11176 tcp->tcp_iphc_len = hdrs_len; 11177 tcp->tcp_hdr_grown = B_TRUE; 11178 } 11179 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 11180 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 11181 11182 /* Set header fields not in ipp */ 11183 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 11184 ip6i = (ip6i_t *)tcp->tcp_iphc; 11185 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 11186 } else { 11187 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 11188 } 11189 /* 11190 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 11191 * 11192 * tcp->tcp_tcp_hdr_len doesn't change here. 11193 */ 11194 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 11195 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 11196 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 11197 11198 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 11199 11200 tcp->tcp_ip6h->ip6_src = src; 11201 tcp->tcp_ip6h->ip6_dst = dst; 11202 11203 /* 11204 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 11205 * the default value for TCP. 11206 */ 11207 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 11208 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 11209 11210 /* 11211 * If we're setting extension headers after a connection 11212 * has been established, and if we have a routing header 11213 * among the extension headers, call ip_massage_options_v6 to 11214 * manipulate the routing header/ip6_dst set the checksum 11215 * difference in the tcp header template. 11216 * (This happens in tcp_connect_ipv6 if the routing header 11217 * is set prior to the connect.) 11218 * Set the tcp_sum to zero first in case we've cleared a 11219 * routing header or don't have one at all. 11220 */ 11221 tcp->tcp_sum = 0; 11222 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 11223 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 11224 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 11225 (uint8_t *)tcp->tcp_tcph); 11226 if (rth != NULL) { 11227 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 11228 rth, tcps->tcps_netstack); 11229 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 11230 (tcp->tcp_sum >> 16)); 11231 } 11232 } 11233 11234 /* Try to get everything in a single mblk */ 11235 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcps->tcps_wroff_xtra); 11236 return (0); 11237 } 11238 11239 /* 11240 * Transfer any source route option from ipha to buf/dst in reversed form. 11241 */ 11242 static int 11243 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 11244 { 11245 ipoptp_t opts; 11246 uchar_t *opt; 11247 uint8_t optval; 11248 uint8_t optlen; 11249 uint32_t len = 0; 11250 11251 for (optval = ipoptp_first(&opts, ipha); 11252 optval != IPOPT_EOL; 11253 optval = ipoptp_next(&opts)) { 11254 opt = opts.ipoptp_cur; 11255 optlen = opts.ipoptp_len; 11256 switch (optval) { 11257 int off1, off2; 11258 case IPOPT_SSRR: 11259 case IPOPT_LSRR: 11260 11261 /* Reverse source route */ 11262 /* 11263 * First entry should be the next to last one in the 11264 * current source route (the last entry is our 11265 * address.) 11266 * The last entry should be the final destination. 11267 */ 11268 buf[IPOPT_OPTVAL] = (uint8_t)optval; 11269 buf[IPOPT_OLEN] = (uint8_t)optlen; 11270 off1 = IPOPT_MINOFF_SR - 1; 11271 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 11272 if (off2 < 0) { 11273 /* No entries in source route */ 11274 break; 11275 } 11276 bcopy(opt + off2, dst, IP_ADDR_LEN); 11277 /* 11278 * Note: use src since ipha has not had its src 11279 * and dst reversed (it is in the state it was 11280 * received. 11281 */ 11282 bcopy(&ipha->ipha_src, buf + off2, 11283 IP_ADDR_LEN); 11284 off2 -= IP_ADDR_LEN; 11285 11286 while (off2 > 0) { 11287 bcopy(opt + off2, buf + off1, 11288 IP_ADDR_LEN); 11289 off1 += IP_ADDR_LEN; 11290 off2 -= IP_ADDR_LEN; 11291 } 11292 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11293 buf += optlen; 11294 len += optlen; 11295 break; 11296 } 11297 } 11298 done: 11299 /* Pad the resulting options */ 11300 while (len & 0x3) { 11301 *buf++ = IPOPT_EOL; 11302 len++; 11303 } 11304 return (len); 11305 } 11306 11307 11308 /* 11309 * Extract and revert a source route from ipha (if any) 11310 * and then update the relevant fields in both tcp_t and the standard header. 11311 */ 11312 static void 11313 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11314 { 11315 char buf[TCP_MAX_HDR_LENGTH]; 11316 uint_t tcph_len; 11317 int len; 11318 11319 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11320 len = IPH_HDR_LENGTH(ipha); 11321 if (len == IP_SIMPLE_HDR_LENGTH) 11322 /* Nothing to do */ 11323 return; 11324 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11325 (len & 0x3)) 11326 return; 11327 11328 tcph_len = tcp->tcp_tcp_hdr_len; 11329 bcopy(tcp->tcp_tcph, buf, tcph_len); 11330 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11331 (tcp->tcp_ipha->ipha_dst & 0xffff); 11332 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11333 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11334 len += IP_SIMPLE_HDR_LENGTH; 11335 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11336 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11337 if ((int)tcp->tcp_sum < 0) 11338 tcp->tcp_sum--; 11339 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11340 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11341 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11342 bcopy(buf, tcp->tcp_tcph, tcph_len); 11343 tcp->tcp_ip_hdr_len = len; 11344 tcp->tcp_ipha->ipha_version_and_hdr_length = 11345 (IP_VERSION << 4) | (len >> 2); 11346 len += tcph_len; 11347 tcp->tcp_hdr_len = len; 11348 } 11349 11350 /* 11351 * Copy the standard header into its new location, 11352 * lay in the new options and then update the relevant 11353 * fields in both tcp_t and the standard header. 11354 */ 11355 static int 11356 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11357 { 11358 uint_t tcph_len; 11359 uint8_t *ip_optp; 11360 tcph_t *new_tcph; 11361 tcp_stack_t *tcps = tcp->tcp_tcps; 11362 11363 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11364 return (EINVAL); 11365 11366 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11367 return (EINVAL); 11368 11369 if (checkonly) { 11370 /* 11371 * do not really set, just pretend to - T_CHECK 11372 */ 11373 return (0); 11374 } 11375 11376 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11377 if (tcp->tcp_label_len > 0) { 11378 int padlen; 11379 uint8_t opt; 11380 11381 /* convert list termination to no-ops */ 11382 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11383 ip_optp += ip_optp[IPOPT_OLEN]; 11384 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11385 while (--padlen >= 0) 11386 *ip_optp++ = opt; 11387 } 11388 tcph_len = tcp->tcp_tcp_hdr_len; 11389 new_tcph = (tcph_t *)(ip_optp + len); 11390 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11391 tcp->tcp_tcph = new_tcph; 11392 bcopy(ptr, ip_optp, len); 11393 11394 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11395 11396 tcp->tcp_ip_hdr_len = len; 11397 tcp->tcp_ipha->ipha_version_and_hdr_length = 11398 (IP_VERSION << 4) | (len >> 2); 11399 tcp->tcp_hdr_len = len + tcph_len; 11400 if (!TCP_IS_DETACHED(tcp)) { 11401 /* Always allocate room for all options. */ 11402 (void) mi_set_sth_wroff(tcp->tcp_rq, 11403 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11404 } 11405 return (0); 11406 } 11407 11408 /* Get callback routine passed to nd_load by tcp_param_register */ 11409 /* ARGSUSED */ 11410 static int 11411 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11412 { 11413 tcpparam_t *tcppa = (tcpparam_t *)cp; 11414 11415 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11416 return (0); 11417 } 11418 11419 /* 11420 * Walk through the param array specified registering each element with the 11421 * named dispatch handler. 11422 */ 11423 static boolean_t 11424 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11425 { 11426 for (; cnt-- > 0; tcppa++) { 11427 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11428 if (!nd_load(ndp, tcppa->tcp_param_name, 11429 tcp_param_get, tcp_param_set, 11430 (caddr_t)tcppa)) { 11431 nd_free(ndp); 11432 return (B_FALSE); 11433 } 11434 } 11435 } 11436 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11437 KM_SLEEP); 11438 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11439 sizeof (tcpparam_t)); 11440 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11441 tcp_param_get, tcp_param_set_aligned, 11442 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11443 nd_free(ndp); 11444 return (B_FALSE); 11445 } 11446 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11447 KM_SLEEP); 11448 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11449 sizeof (tcpparam_t)); 11450 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11451 tcp_param_get, tcp_param_set_aligned, 11452 (caddr_t)tcps->tcps_mdt_head_param)) { 11453 nd_free(ndp); 11454 return (B_FALSE); 11455 } 11456 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11457 KM_SLEEP); 11458 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11459 sizeof (tcpparam_t)); 11460 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11461 tcp_param_get, tcp_param_set_aligned, 11462 (caddr_t)tcps->tcps_mdt_tail_param)) { 11463 nd_free(ndp); 11464 return (B_FALSE); 11465 } 11466 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11467 KM_SLEEP); 11468 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11469 sizeof (tcpparam_t)); 11470 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11471 tcp_param_get, tcp_param_set_aligned, 11472 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11473 nd_free(ndp); 11474 return (B_FALSE); 11475 } 11476 if (!nd_load(ndp, "tcp_extra_priv_ports", 11477 tcp_extra_priv_ports_get, NULL, NULL)) { 11478 nd_free(ndp); 11479 return (B_FALSE); 11480 } 11481 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11482 NULL, tcp_extra_priv_ports_add, NULL)) { 11483 nd_free(ndp); 11484 return (B_FALSE); 11485 } 11486 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11487 NULL, tcp_extra_priv_ports_del, NULL)) { 11488 nd_free(ndp); 11489 return (B_FALSE); 11490 } 11491 if (!nd_load(ndp, "tcp_status", tcp_status_report, NULL, 11492 NULL)) { 11493 nd_free(ndp); 11494 return (B_FALSE); 11495 } 11496 if (!nd_load(ndp, "tcp_bind_hash", tcp_bind_hash_report, 11497 NULL, NULL)) { 11498 nd_free(ndp); 11499 return (B_FALSE); 11500 } 11501 if (!nd_load(ndp, "tcp_listen_hash", 11502 tcp_listen_hash_report, NULL, NULL)) { 11503 nd_free(ndp); 11504 return (B_FALSE); 11505 } 11506 if (!nd_load(ndp, "tcp_conn_hash", tcp_conn_hash_report, 11507 NULL, NULL)) { 11508 nd_free(ndp); 11509 return (B_FALSE); 11510 } 11511 if (!nd_load(ndp, "tcp_acceptor_hash", 11512 tcp_acceptor_hash_report, NULL, NULL)) { 11513 nd_free(ndp); 11514 return (B_FALSE); 11515 } 11516 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11517 tcp_1948_phrase_set, NULL)) { 11518 nd_free(ndp); 11519 return (B_FALSE); 11520 } 11521 /* 11522 * Dummy ndd variables - only to convey obsolescence information 11523 * through printing of their name (no get or set routines) 11524 * XXX Remove in future releases ? 11525 */ 11526 if (!nd_load(ndp, 11527 "tcp_close_wait_interval(obsoleted - " 11528 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11529 nd_free(ndp); 11530 return (B_FALSE); 11531 } 11532 return (B_TRUE); 11533 } 11534 11535 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11536 /* ARGSUSED */ 11537 static int 11538 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11539 cred_t *cr) 11540 { 11541 long new_value; 11542 tcpparam_t *tcppa = (tcpparam_t *)cp; 11543 11544 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11545 new_value < tcppa->tcp_param_min || 11546 new_value > tcppa->tcp_param_max) { 11547 return (EINVAL); 11548 } 11549 /* 11550 * Need to make sure new_value is a multiple of 4. If it is not, 11551 * round it up. For future 64 bit requirement, we actually make it 11552 * a multiple of 8. 11553 */ 11554 if (new_value & 0x7) { 11555 new_value = (new_value & ~0x7) + 0x8; 11556 } 11557 tcppa->tcp_param_val = new_value; 11558 return (0); 11559 } 11560 11561 /* Set callback routine passed to nd_load by tcp_param_register */ 11562 /* ARGSUSED */ 11563 static int 11564 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11565 { 11566 long new_value; 11567 tcpparam_t *tcppa = (tcpparam_t *)cp; 11568 11569 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11570 new_value < tcppa->tcp_param_min || 11571 new_value > tcppa->tcp_param_max) { 11572 return (EINVAL); 11573 } 11574 tcppa->tcp_param_val = new_value; 11575 return (0); 11576 } 11577 11578 /* 11579 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11580 * is filled, return as much as we can. The message passed in may be 11581 * multi-part, chained using b_cont. "start" is the starting sequence 11582 * number for this piece. 11583 */ 11584 static mblk_t * 11585 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11586 { 11587 uint32_t end; 11588 mblk_t *mp1; 11589 mblk_t *mp2; 11590 mblk_t *next_mp; 11591 uint32_t u1; 11592 tcp_stack_t *tcps = tcp->tcp_tcps; 11593 11594 /* Walk through all the new pieces. */ 11595 do { 11596 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11597 (uintptr_t)INT_MAX); 11598 end = start + (int)(mp->b_wptr - mp->b_rptr); 11599 next_mp = mp->b_cont; 11600 if (start == end) { 11601 /* Empty. Blast it. */ 11602 freeb(mp); 11603 continue; 11604 } 11605 mp->b_cont = NULL; 11606 TCP_REASS_SET_SEQ(mp, start); 11607 TCP_REASS_SET_END(mp, end); 11608 mp1 = tcp->tcp_reass_tail; 11609 if (!mp1) { 11610 tcp->tcp_reass_tail = mp; 11611 tcp->tcp_reass_head = mp; 11612 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11613 UPDATE_MIB(&tcps->tcps_mib, 11614 tcpInDataUnorderBytes, end - start); 11615 continue; 11616 } 11617 /* New stuff completely beyond tail? */ 11618 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11619 /* Link it on end. */ 11620 mp1->b_cont = mp; 11621 tcp->tcp_reass_tail = mp; 11622 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11623 UPDATE_MIB(&tcps->tcps_mib, 11624 tcpInDataUnorderBytes, end - start); 11625 continue; 11626 } 11627 mp1 = tcp->tcp_reass_head; 11628 u1 = TCP_REASS_SEQ(mp1); 11629 /* New stuff at the front? */ 11630 if (SEQ_LT(start, u1)) { 11631 /* Yes... Check for overlap. */ 11632 mp->b_cont = mp1; 11633 tcp->tcp_reass_head = mp; 11634 tcp_reass_elim_overlap(tcp, mp); 11635 continue; 11636 } 11637 /* 11638 * The new piece fits somewhere between the head and tail. 11639 * We find our slot, where mp1 precedes us and mp2 trails. 11640 */ 11641 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11642 u1 = TCP_REASS_SEQ(mp2); 11643 if (SEQ_LEQ(start, u1)) 11644 break; 11645 } 11646 /* Link ourselves in */ 11647 mp->b_cont = mp2; 11648 mp1->b_cont = mp; 11649 11650 /* Trim overlap with following mblk(s) first */ 11651 tcp_reass_elim_overlap(tcp, mp); 11652 11653 /* Trim overlap with preceding mblk */ 11654 tcp_reass_elim_overlap(tcp, mp1); 11655 11656 } while (start = end, mp = next_mp); 11657 mp1 = tcp->tcp_reass_head; 11658 /* Anything ready to go? */ 11659 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11660 return (NULL); 11661 /* Eat what we can off the queue */ 11662 for (;;) { 11663 mp = mp1->b_cont; 11664 end = TCP_REASS_END(mp1); 11665 TCP_REASS_SET_SEQ(mp1, 0); 11666 TCP_REASS_SET_END(mp1, 0); 11667 if (!mp) { 11668 tcp->tcp_reass_tail = NULL; 11669 break; 11670 } 11671 if (end != TCP_REASS_SEQ(mp)) { 11672 mp1->b_cont = NULL; 11673 break; 11674 } 11675 mp1 = mp; 11676 } 11677 mp1 = tcp->tcp_reass_head; 11678 tcp->tcp_reass_head = mp; 11679 return (mp1); 11680 } 11681 11682 /* Eliminate any overlap that mp may have over later mblks */ 11683 static void 11684 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11685 { 11686 uint32_t end; 11687 mblk_t *mp1; 11688 uint32_t u1; 11689 tcp_stack_t *tcps = tcp->tcp_tcps; 11690 11691 end = TCP_REASS_END(mp); 11692 while ((mp1 = mp->b_cont) != NULL) { 11693 u1 = TCP_REASS_SEQ(mp1); 11694 if (!SEQ_GT(end, u1)) 11695 break; 11696 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11697 mp->b_wptr -= end - u1; 11698 TCP_REASS_SET_END(mp, u1); 11699 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11700 UPDATE_MIB(&tcps->tcps_mib, 11701 tcpInDataPartDupBytes, end - u1); 11702 break; 11703 } 11704 mp->b_cont = mp1->b_cont; 11705 TCP_REASS_SET_SEQ(mp1, 0); 11706 TCP_REASS_SET_END(mp1, 0); 11707 freeb(mp1); 11708 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11709 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11710 } 11711 if (!mp1) 11712 tcp->tcp_reass_tail = mp; 11713 } 11714 11715 /* 11716 * Send up all messages queued on tcp_rcv_list. 11717 */ 11718 static uint_t 11719 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11720 { 11721 mblk_t *mp; 11722 uint_t ret = 0; 11723 uint_t thwin; 11724 #ifdef DEBUG 11725 uint_t cnt = 0; 11726 #endif 11727 tcp_stack_t *tcps = tcp->tcp_tcps; 11728 11729 /* Can't drain on an eager connection */ 11730 if (tcp->tcp_listener != NULL) 11731 return (ret); 11732 11733 /* Can't be sodirect enabled */ 11734 ASSERT(SOD_NOT_ENABLED(tcp)); 11735 11736 /* No need for the push timer now. */ 11737 if (tcp->tcp_push_tid != 0) { 11738 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11739 tcp->tcp_push_tid = 0; 11740 } 11741 11742 /* 11743 * Handle two cases here: we are currently fused or we were 11744 * previously fused and have some urgent data to be delivered 11745 * upstream. The latter happens because we either ran out of 11746 * memory or were detached and therefore sending the SIGURG was 11747 * deferred until this point. In either case we pass control 11748 * over to tcp_fuse_rcv_drain() since it may need to complete 11749 * some work. 11750 */ 11751 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11752 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11753 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11754 &tcp->tcp_fused_sigurg_mp)) 11755 return (ret); 11756 } 11757 11758 while ((mp = tcp->tcp_rcv_list) != NULL) { 11759 tcp->tcp_rcv_list = mp->b_next; 11760 mp->b_next = NULL; 11761 #ifdef DEBUG 11762 cnt += msgdsize(mp); 11763 #endif 11764 /* Does this need SSL processing first? */ 11765 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11766 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11767 mblk_t *, mp); 11768 tcp_kssl_input(tcp, mp); 11769 continue; 11770 } 11771 putnext(q, mp); 11772 } 11773 ASSERT(cnt == tcp->tcp_rcv_cnt); 11774 tcp->tcp_rcv_last_head = NULL; 11775 tcp->tcp_rcv_last_tail = NULL; 11776 tcp->tcp_rcv_cnt = 0; 11777 11778 /* Learn the latest rwnd information that we sent to the other side. */ 11779 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11780 << tcp->tcp_rcv_ws; 11781 /* This is peer's calculated send window (our receive window). */ 11782 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11783 /* 11784 * Increase the receive window to max. But we need to do receiver 11785 * SWS avoidance. This means that we need to check the increase of 11786 * of receive window is at least 1 MSS. 11787 */ 11788 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11789 /* 11790 * If the window that the other side knows is less than max 11791 * deferred acks segments, send an update immediately. 11792 */ 11793 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11794 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11795 ret = TH_ACK_NEEDED; 11796 } 11797 tcp->tcp_rwnd = q->q_hiwat; 11798 } 11799 return (ret); 11800 } 11801 11802 /* 11803 * Queue data on tcp_rcv_list which is a b_next chain. 11804 * tcp_rcv_last_head/tail is the last element of this chain. 11805 * Each element of the chain is a b_cont chain. 11806 * 11807 * M_DATA messages are added to the current element. 11808 * Other messages are added as new (b_next) elements. 11809 */ 11810 void 11811 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11812 { 11813 ASSERT(seg_len == msgdsize(mp)); 11814 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11815 11816 if (tcp->tcp_rcv_list == NULL) { 11817 ASSERT(tcp->tcp_rcv_last_head == NULL); 11818 tcp->tcp_rcv_list = mp; 11819 tcp->tcp_rcv_last_head = mp; 11820 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11821 tcp->tcp_rcv_last_tail->b_cont = mp; 11822 } else { 11823 tcp->tcp_rcv_last_head->b_next = mp; 11824 tcp->tcp_rcv_last_head = mp; 11825 } 11826 11827 while (mp->b_cont) 11828 mp = mp->b_cont; 11829 11830 tcp->tcp_rcv_last_tail = mp; 11831 tcp->tcp_rcv_cnt += seg_len; 11832 tcp->tcp_rwnd -= seg_len; 11833 } 11834 11835 /* 11836 * The tcp_rcv_sod_XXX() functions enqueue data directly to the socket 11837 * above, in addition when uioa is enabled schedule an asynchronous uio 11838 * prior to enqueuing. They implement the combinhed semantics of the 11839 * tcp_rcv_XXX() functions, tcp_rcv_list push logic, and STREAMS putnext() 11840 * canputnext(), i.e. flow-control with backenable. 11841 * 11842 * tcp_sod_wakeup() is called where tcp_rcv_drain() would be called in the 11843 * non sodirect connection but as there are no tcp_tcv_list mblk_t's we deal 11844 * with the rcv_wnd and push timer and call the sodirect wakeup function. 11845 * 11846 * Must be called with sodp->sod_lockp held and will return with the lock 11847 * released. 11848 */ 11849 static uint_t 11850 tcp_rcv_sod_wakeup(tcp_t *tcp, sodirect_t *sodp) 11851 { 11852 queue_t *q = tcp->tcp_rq; 11853 uint_t thwin; 11854 tcp_stack_t *tcps = tcp->tcp_tcps; 11855 uint_t ret = 0; 11856 11857 /* Can't be an eager connection */ 11858 ASSERT(tcp->tcp_listener == NULL); 11859 11860 /* Caller must have lock held */ 11861 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11862 11863 /* Sodirect mode so must not be a tcp_rcv_list */ 11864 ASSERT(tcp->tcp_rcv_list == NULL); 11865 11866 if (SOD_QFULL(sodp)) { 11867 /* Q is full, mark Q for need backenable */ 11868 SOD_QSETBE(sodp); 11869 } 11870 /* Last advertised rwnd, i.e. rwnd last sent in a packet */ 11871 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11872 << tcp->tcp_rcv_ws; 11873 /* This is peer's calculated send window (our available rwnd). */ 11874 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11875 /* 11876 * Increase the receive window to max. But we need to do receiver 11877 * SWS avoidance. This means that we need to check the increase of 11878 * of receive window is at least 1 MSS. 11879 */ 11880 if (!SOD_QFULL(sodp) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11881 /* 11882 * If the window that the other side knows is less than max 11883 * deferred acks segments, send an update immediately. 11884 */ 11885 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11886 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11887 ret = TH_ACK_NEEDED; 11888 } 11889 tcp->tcp_rwnd = q->q_hiwat; 11890 } 11891 11892 if (!SOD_QEMPTY(sodp)) { 11893 /* Wakeup to socket */ 11894 sodp->sod_state &= SOD_WAKE_CLR; 11895 sodp->sod_state |= SOD_WAKE_DONE; 11896 (sodp->sod_wakeup)(sodp); 11897 /* wakeup() does the mutex_ext() */ 11898 } else { 11899 /* Q is empty, no need to wake */ 11900 sodp->sod_state &= SOD_WAKE_CLR; 11901 sodp->sod_state |= SOD_WAKE_NOT; 11902 mutex_exit(sodp->sod_lockp); 11903 } 11904 11905 /* No need for the push timer now. */ 11906 if (tcp->tcp_push_tid != 0) { 11907 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11908 tcp->tcp_push_tid = 0; 11909 } 11910 11911 return (ret); 11912 } 11913 11914 /* 11915 * Called where tcp_rcv_enqueue()/putnext(RD(q)) would be. For M_DATA 11916 * mblk_t's if uioa enabled then start a uioa asynchronous copy directly 11917 * to the user-land buffer and flag the mblk_t as such. 11918 * 11919 * Also, handle tcp_rwnd. 11920 */ 11921 uint_t 11922 tcp_rcv_sod_enqueue(tcp_t *tcp, sodirect_t *sodp, mblk_t *mp, uint_t seg_len) 11923 { 11924 uioa_t *uioap = &sodp->sod_uioa; 11925 boolean_t qfull; 11926 uint_t thwin; 11927 11928 /* Can't be an eager connection */ 11929 ASSERT(tcp->tcp_listener == NULL); 11930 11931 /* Caller must have lock held */ 11932 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11933 11934 /* Sodirect mode so must not be a tcp_rcv_list */ 11935 ASSERT(tcp->tcp_rcv_list == NULL); 11936 11937 /* Passed in segment length must be equal to mblk_t chain data size */ 11938 ASSERT(seg_len == msgdsize(mp)); 11939 11940 if (DB_TYPE(mp) != M_DATA) { 11941 /* Only process M_DATA mblk_t's */ 11942 goto enq; 11943 } 11944 if (uioap->uioa_state & UIOA_ENABLED) { 11945 /* Uioa is enabled */ 11946 mblk_t *mp1 = mp; 11947 mblk_t *lmp = NULL; 11948 11949 if (seg_len > uioap->uio_resid) { 11950 /* 11951 * There isn't enough uio space for the mblk_t chain 11952 * so disable uioa such that this and any additional 11953 * mblk_t data is handled by the socket and schedule 11954 * the socket for wakeup to finish this uioa. 11955 */ 11956 uioap->uioa_state &= UIOA_CLR; 11957 uioap->uioa_state |= UIOA_FINI; 11958 if (sodp->sod_state & SOD_WAKE_NOT) { 11959 sodp->sod_state &= SOD_WAKE_CLR; 11960 sodp->sod_state |= SOD_WAKE_NEED; 11961 } 11962 goto enq; 11963 } 11964 do { 11965 uint32_t len = MBLKL(mp1); 11966 11967 if (!uioamove(mp1->b_rptr, len, UIO_READ, uioap)) { 11968 /* Scheduled, mark dblk_t as such */ 11969 DB_FLAGS(mp1) |= DBLK_UIOA; 11970 } else { 11971 /* Error, turn off async processing */ 11972 uioap->uioa_state &= UIOA_CLR; 11973 uioap->uioa_state |= UIOA_FINI; 11974 break; 11975 } 11976 lmp = mp1; 11977 } while ((mp1 = mp1->b_cont) != NULL); 11978 11979 if (mp1 != NULL || uioap->uio_resid == 0) { 11980 /* 11981 * Not all mblk_t(s) uioamoved (error) or all uio 11982 * space has been consumed so schedule the socket 11983 * for wakeup to finish this uio. 11984 */ 11985 sodp->sod_state &= SOD_WAKE_CLR; 11986 sodp->sod_state |= SOD_WAKE_NEED; 11987 11988 /* Break the mblk chain if neccessary. */ 11989 if (mp1 != NULL && lmp != NULL) { 11990 mp->b_next = mp1; 11991 lmp->b_cont = NULL; 11992 } 11993 } 11994 } else if (uioap->uioa_state & UIOA_FINI) { 11995 /* 11996 * Post UIO_ENABLED waiting for socket to finish processing 11997 * so just enqueue and update tcp_rwnd. 11998 */ 11999 if (SOD_QFULL(sodp)) 12000 tcp->tcp_rwnd -= seg_len; 12001 } else if (sodp->sod_want > 0) { 12002 /* 12003 * Uioa isn't enabled but sodirect has a pending read(). 12004 */ 12005 if (SOD_QCNT(sodp) + seg_len >= sodp->sod_want) { 12006 if (sodp->sod_state & SOD_WAKE_NOT) { 12007 /* Schedule socket for wakeup */ 12008 sodp->sod_state &= SOD_WAKE_CLR; 12009 sodp->sod_state |= SOD_WAKE_NEED; 12010 } 12011 tcp->tcp_rwnd -= seg_len; 12012 } 12013 } else if (SOD_QCNT(sodp) + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 12014 /* 12015 * No pending sodirect read() so used the default 12016 * TCP push logic to guess that a push is needed. 12017 */ 12018 if (sodp->sod_state & SOD_WAKE_NOT) { 12019 /* Schedule socket for wakeup */ 12020 sodp->sod_state &= SOD_WAKE_CLR; 12021 sodp->sod_state |= SOD_WAKE_NEED; 12022 } 12023 tcp->tcp_rwnd -= seg_len; 12024 } else { 12025 /* Just update tcp_rwnd */ 12026 tcp->tcp_rwnd -= seg_len; 12027 } 12028 enq: 12029 qfull = SOD_QFULL(sodp); 12030 12031 (sodp->sod_enqueue)(sodp, mp); 12032 12033 if (! qfull && SOD_QFULL(sodp)) { 12034 /* Wasn't QFULL, now QFULL, need back-enable */ 12035 SOD_QSETBE(sodp); 12036 } 12037 12038 /* 12039 * Check to see if remote avail swnd < mss due to delayed ACK, 12040 * first get advertised rwnd. 12041 */ 12042 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)); 12043 /* Minus delayed ACK count */ 12044 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 12045 if (thwin < tcp->tcp_mss) { 12046 /* Remote avail swnd < mss, need ACK now */ 12047 return (TH_ACK_NEEDED); 12048 } 12049 12050 return (0); 12051 } 12052 12053 /* 12054 * DEFAULT TCP ENTRY POINT via squeue on READ side. 12055 * 12056 * This is the default entry function into TCP on the read side. TCP is 12057 * always entered via squeue i.e. using squeue's for mutual exclusion. 12058 * When classifier does a lookup to find the tcp, it also puts a reference 12059 * on the conn structure associated so the tcp is guaranteed to exist 12060 * when we come here. We still need to check the state because it might 12061 * as well has been closed. The squeue processing function i.e. squeue_enter, 12062 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 12063 * CONN_DEC_REF. 12064 * 12065 * Apart from the default entry point, IP also sends packets directly to 12066 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 12067 * connections. 12068 */ 12069 void 12070 tcp_input(void *arg, mblk_t *mp, void *arg2) 12071 { 12072 conn_t *connp = (conn_t *)arg; 12073 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 12074 12075 /* arg2 is the sqp */ 12076 ASSERT(arg2 != NULL); 12077 ASSERT(mp != NULL); 12078 12079 /* 12080 * Don't accept any input on a closed tcp as this TCP logically does 12081 * not exist on the system. Don't proceed further with this TCP. 12082 * For eg. this packet could trigger another close of this tcp 12083 * which would be disastrous for tcp_refcnt. tcp_close_detached / 12084 * tcp_clean_death / tcp_closei_local must be called at most once 12085 * on a TCP. In this case we need to refeed the packet into the 12086 * classifier and figure out where the packet should go. Need to 12087 * preserve the recv_ill somehow. Until we figure that out, for 12088 * now just drop the packet if we can't classify the packet. 12089 */ 12090 if (tcp->tcp_state == TCPS_CLOSED || 12091 tcp->tcp_state == TCPS_BOUND) { 12092 conn_t *new_connp; 12093 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 12094 12095 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 12096 if (new_connp != NULL) { 12097 tcp_reinput(new_connp, mp, arg2); 12098 return; 12099 } 12100 /* We failed to classify. For now just drop the packet */ 12101 freemsg(mp); 12102 return; 12103 } 12104 12105 if (DB_TYPE(mp) == M_DATA) 12106 tcp_rput_data(connp, mp, arg2); 12107 else 12108 tcp_rput_common(tcp, mp); 12109 } 12110 12111 /* 12112 * The read side put procedure. 12113 * The packets passed up by ip are assume to be aligned according to 12114 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 12115 */ 12116 static void 12117 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 12118 { 12119 /* 12120 * tcp_rput_data() does not expect M_CTL except for the case 12121 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 12122 * type. Need to make sure that any other M_CTLs don't make 12123 * it to tcp_rput_data since it is not expecting any and doesn't 12124 * check for it. 12125 */ 12126 if (DB_TYPE(mp) == M_CTL) { 12127 switch (*(uint32_t *)(mp->b_rptr)) { 12128 case TCP_IOC_ABORT_CONN: 12129 /* 12130 * Handle connection abort request. 12131 */ 12132 tcp_ioctl_abort_handler(tcp, mp); 12133 return; 12134 case IPSEC_IN: 12135 /* 12136 * Only secure icmp arrive in TCP and they 12137 * don't go through data path. 12138 */ 12139 tcp_icmp_error(tcp, mp); 12140 return; 12141 case IN_PKTINFO: 12142 /* 12143 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 12144 * sockets that are receiving IPv4 traffic. tcp 12145 */ 12146 ASSERT(tcp->tcp_family == AF_INET6); 12147 ASSERT(tcp->tcp_ipv6_recvancillary & 12148 TCP_IPV6_RECVPKTINFO); 12149 tcp_rput_data(tcp->tcp_connp, mp, 12150 tcp->tcp_connp->conn_sqp); 12151 return; 12152 case MDT_IOC_INFO_UPDATE: 12153 /* 12154 * Handle Multidata information update; the 12155 * following routine will free the message. 12156 */ 12157 if (tcp->tcp_connp->conn_mdt_ok) { 12158 tcp_mdt_update(tcp, 12159 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 12160 B_FALSE); 12161 } 12162 freemsg(mp); 12163 return; 12164 case LSO_IOC_INFO_UPDATE: 12165 /* 12166 * Handle LSO information update; the following 12167 * routine will free the message. 12168 */ 12169 if (tcp->tcp_connp->conn_lso_ok) { 12170 tcp_lso_update(tcp, 12171 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 12172 } 12173 freemsg(mp); 12174 return; 12175 default: 12176 /* 12177 * tcp_icmp_err() will process the M_CTL packets. 12178 * Non-ICMP packets, if any, will be discarded in 12179 * tcp_icmp_err(). We will process the ICMP packet 12180 * even if we are TCP_IS_DETACHED_NONEAGER as the 12181 * incoming ICMP packet may result in changing 12182 * the tcp_mss, which we would need if we have 12183 * packets to retransmit. 12184 */ 12185 tcp_icmp_error(tcp, mp); 12186 return; 12187 } 12188 } 12189 12190 /* No point processing the message if tcp is already closed */ 12191 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 12192 freemsg(mp); 12193 return; 12194 } 12195 12196 tcp_rput_other(tcp, mp); 12197 } 12198 12199 12200 /* The minimum of smoothed mean deviation in RTO calculation. */ 12201 #define TCP_SD_MIN 400 12202 12203 /* 12204 * Set RTO for this connection. The formula is from Jacobson and Karels' 12205 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 12206 * are the same as those in Appendix A.2 of that paper. 12207 * 12208 * m = new measurement 12209 * sa = smoothed RTT average (8 * average estimates). 12210 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 12211 */ 12212 static void 12213 tcp_set_rto(tcp_t *tcp, clock_t rtt) 12214 { 12215 long m = TICK_TO_MSEC(rtt); 12216 clock_t sa = tcp->tcp_rtt_sa; 12217 clock_t sv = tcp->tcp_rtt_sd; 12218 clock_t rto; 12219 tcp_stack_t *tcps = tcp->tcp_tcps; 12220 12221 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 12222 tcp->tcp_rtt_update++; 12223 12224 /* tcp_rtt_sa is not 0 means this is a new sample. */ 12225 if (sa != 0) { 12226 /* 12227 * Update average estimator: 12228 * new rtt = 7/8 old rtt + 1/8 Error 12229 */ 12230 12231 /* m is now Error in estimate. */ 12232 m -= sa >> 3; 12233 if ((sa += m) <= 0) { 12234 /* 12235 * Don't allow the smoothed average to be negative. 12236 * We use 0 to denote reinitialization of the 12237 * variables. 12238 */ 12239 sa = 1; 12240 } 12241 12242 /* 12243 * Update deviation estimator: 12244 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 12245 */ 12246 if (m < 0) 12247 m = -m; 12248 m -= sv >> 2; 12249 sv += m; 12250 } else { 12251 /* 12252 * This follows BSD's implementation. So the reinitialized 12253 * RTO is 3 * m. We cannot go less than 2 because if the 12254 * link is bandwidth dominated, doubling the window size 12255 * during slow start means doubling the RTT. We want to be 12256 * more conservative when we reinitialize our estimates. 3 12257 * is just a convenient number. 12258 */ 12259 sa = m << 3; 12260 sv = m << 1; 12261 } 12262 if (sv < TCP_SD_MIN) { 12263 /* 12264 * We do not know that if sa captures the delay ACK 12265 * effect as in a long train of segments, a receiver 12266 * does not delay its ACKs. So set the minimum of sv 12267 * to be TCP_SD_MIN, which is default to 400 ms, twice 12268 * of BSD DATO. That means the minimum of mean 12269 * deviation is 100 ms. 12270 * 12271 */ 12272 sv = TCP_SD_MIN; 12273 } 12274 tcp->tcp_rtt_sa = sa; 12275 tcp->tcp_rtt_sd = sv; 12276 /* 12277 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 12278 * 12279 * Add tcp_rexmit_interval extra in case of extreme environment 12280 * where the algorithm fails to work. The default value of 12281 * tcp_rexmit_interval_extra should be 0. 12282 * 12283 * As we use a finer grained clock than BSD and update 12284 * RTO for every ACKs, add in another .25 of RTT to the 12285 * deviation of RTO to accomodate burstiness of 1/4 of 12286 * window size. 12287 */ 12288 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 12289 12290 if (rto > tcps->tcps_rexmit_interval_max) { 12291 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 12292 } else if (rto < tcps->tcps_rexmit_interval_min) { 12293 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 12294 } else { 12295 tcp->tcp_rto = rto; 12296 } 12297 12298 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 12299 tcp->tcp_timer_backoff = 0; 12300 } 12301 12302 /* 12303 * tcp_get_seg_mp() is called to get the pointer to a segment in the 12304 * send queue which starts at the given seq. no. 12305 * 12306 * Parameters: 12307 * tcp_t *tcp: the tcp instance pointer. 12308 * uint32_t seq: the starting seq. no of the requested segment. 12309 * int32_t *off: after the execution, *off will be the offset to 12310 * the returned mblk which points to the requested seq no. 12311 * It is the caller's responsibility to send in a non-null off. 12312 * 12313 * Return: 12314 * A mblk_t pointer pointing to the requested segment in send queue. 12315 */ 12316 static mblk_t * 12317 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 12318 { 12319 int32_t cnt; 12320 mblk_t *mp; 12321 12322 /* Defensive coding. Make sure we don't send incorrect data. */ 12323 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 12324 return (NULL); 12325 12326 cnt = seq - tcp->tcp_suna; 12327 mp = tcp->tcp_xmit_head; 12328 while (cnt > 0 && mp != NULL) { 12329 cnt -= mp->b_wptr - mp->b_rptr; 12330 if (cnt < 0) { 12331 cnt += mp->b_wptr - mp->b_rptr; 12332 break; 12333 } 12334 mp = mp->b_cont; 12335 } 12336 ASSERT(mp != NULL); 12337 *off = cnt; 12338 return (mp); 12339 } 12340 12341 /* 12342 * This function handles all retransmissions if SACK is enabled for this 12343 * connection. First it calculates how many segments can be retransmitted 12344 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12345 * segments. A segment is eligible if sack_cnt for that segment is greater 12346 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12347 * all eligible segments, it checks to see if TCP can send some new segments 12348 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12349 * 12350 * Parameters: 12351 * tcp_t *tcp: the tcp structure of the connection. 12352 * uint_t *flags: in return, appropriate value will be set for 12353 * tcp_rput_data(). 12354 */ 12355 static void 12356 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12357 { 12358 notsack_blk_t *notsack_blk; 12359 int32_t usable_swnd; 12360 int32_t mss; 12361 uint32_t seg_len; 12362 mblk_t *xmit_mp; 12363 tcp_stack_t *tcps = tcp->tcp_tcps; 12364 12365 ASSERT(tcp->tcp_sack_info != NULL); 12366 ASSERT(tcp->tcp_notsack_list != NULL); 12367 ASSERT(tcp->tcp_rexmit == B_FALSE); 12368 12369 /* Defensive coding in case there is a bug... */ 12370 if (tcp->tcp_notsack_list == NULL) { 12371 return; 12372 } 12373 notsack_blk = tcp->tcp_notsack_list; 12374 mss = tcp->tcp_mss; 12375 12376 /* 12377 * Limit the num of outstanding data in the network to be 12378 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12379 */ 12380 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12381 12382 /* At least retransmit 1 MSS of data. */ 12383 if (usable_swnd <= 0) { 12384 usable_swnd = mss; 12385 } 12386 12387 /* Make sure no new RTT samples will be taken. */ 12388 tcp->tcp_csuna = tcp->tcp_snxt; 12389 12390 notsack_blk = tcp->tcp_notsack_list; 12391 while (usable_swnd > 0) { 12392 mblk_t *snxt_mp, *tmp_mp; 12393 tcp_seq begin = tcp->tcp_sack_snxt; 12394 tcp_seq end; 12395 int32_t off; 12396 12397 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12398 if (SEQ_GT(notsack_blk->end, begin) && 12399 (notsack_blk->sack_cnt >= 12400 tcps->tcps_dupack_fast_retransmit)) { 12401 end = notsack_blk->end; 12402 if (SEQ_LT(begin, notsack_blk->begin)) { 12403 begin = notsack_blk->begin; 12404 } 12405 break; 12406 } 12407 } 12408 /* 12409 * All holes are filled. Manipulate tcp_cwnd to send more 12410 * if we can. Note that after the SACK recovery, tcp_cwnd is 12411 * set to tcp_cwnd_ssthresh. 12412 */ 12413 if (notsack_blk == NULL) { 12414 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12415 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12416 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12417 ASSERT(tcp->tcp_cwnd > 0); 12418 return; 12419 } else { 12420 usable_swnd = usable_swnd / mss; 12421 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12422 MAX(usable_swnd * mss, mss); 12423 *flags |= TH_XMIT_NEEDED; 12424 return; 12425 } 12426 } 12427 12428 /* 12429 * Note that we may send more than usable_swnd allows here 12430 * because of round off, but no more than 1 MSS of data. 12431 */ 12432 seg_len = end - begin; 12433 if (seg_len > mss) 12434 seg_len = mss; 12435 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12436 ASSERT(snxt_mp != NULL); 12437 /* This should not happen. Defensive coding again... */ 12438 if (snxt_mp == NULL) { 12439 return; 12440 } 12441 12442 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12443 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12444 if (xmit_mp == NULL) 12445 return; 12446 12447 usable_swnd -= seg_len; 12448 tcp->tcp_pipe += seg_len; 12449 tcp->tcp_sack_snxt = begin + seg_len; 12450 12451 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12452 12453 /* 12454 * Update the send timestamp to avoid false retransmission. 12455 */ 12456 snxt_mp->b_prev = (mblk_t *)lbolt; 12457 12458 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12459 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12460 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12461 /* 12462 * Update tcp_rexmit_max to extend this SACK recovery phase. 12463 * This happens when new data sent during fast recovery is 12464 * also lost. If TCP retransmits those new data, it needs 12465 * to extend SACK recover phase to avoid starting another 12466 * fast retransmit/recovery unnecessarily. 12467 */ 12468 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12469 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12470 } 12471 } 12472 } 12473 12474 /* 12475 * This function handles policy checking at TCP level for non-hard_bound/ 12476 * detached connections. 12477 */ 12478 static boolean_t 12479 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12480 boolean_t secure, boolean_t mctl_present) 12481 { 12482 ipsec_latch_t *ipl = NULL; 12483 ipsec_action_t *act = NULL; 12484 mblk_t *data_mp; 12485 ipsec_in_t *ii; 12486 const char *reason; 12487 kstat_named_t *counter; 12488 tcp_stack_t *tcps = tcp->tcp_tcps; 12489 ipsec_stack_t *ipss; 12490 ip_stack_t *ipst; 12491 12492 ASSERT(mctl_present || !secure); 12493 12494 ASSERT((ipha == NULL && ip6h != NULL) || 12495 (ip6h == NULL && ipha != NULL)); 12496 12497 /* 12498 * We don't necessarily have an ipsec_in_act action to verify 12499 * policy because of assymetrical policy where we have only 12500 * outbound policy and no inbound policy (possible with global 12501 * policy). 12502 */ 12503 if (!secure) { 12504 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12505 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12506 return (B_TRUE); 12507 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12508 "tcp_check_policy", ipha, ip6h, secure, 12509 tcps->tcps_netstack); 12510 ipss = tcps->tcps_netstack->netstack_ipsec; 12511 12512 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12513 DROPPER(ipss, ipds_tcp_clear), 12514 &tcps->tcps_dropper); 12515 return (B_FALSE); 12516 } 12517 12518 /* 12519 * We have a secure packet. 12520 */ 12521 if (act == NULL) { 12522 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12523 "tcp_check_policy", ipha, ip6h, secure, 12524 tcps->tcps_netstack); 12525 ipss = tcps->tcps_netstack->netstack_ipsec; 12526 12527 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12528 DROPPER(ipss, ipds_tcp_secure), 12529 &tcps->tcps_dropper); 12530 return (B_FALSE); 12531 } 12532 12533 /* 12534 * XXX This whole routine is currently incorrect. ipl should 12535 * be set to the latch pointer, but is currently not set, so 12536 * we initialize it to NULL to avoid picking up random garbage. 12537 */ 12538 if (ipl == NULL) 12539 return (B_TRUE); 12540 12541 data_mp = first_mp->b_cont; 12542 12543 ii = (ipsec_in_t *)first_mp->b_rptr; 12544 12545 ipst = tcps->tcps_netstack->netstack_ip; 12546 12547 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12548 &counter, tcp->tcp_connp)) { 12549 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12550 return (B_TRUE); 12551 } 12552 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12553 "tcp inbound policy mismatch: %s, packet dropped\n", 12554 reason); 12555 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12556 12557 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12558 &tcps->tcps_dropper); 12559 return (B_FALSE); 12560 } 12561 12562 /* 12563 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12564 * retransmission after a timeout. 12565 * 12566 * To limit the number of duplicate segments, we limit the number of segment 12567 * to be sent in one time to tcp_snd_burst, the burst variable. 12568 */ 12569 static void 12570 tcp_ss_rexmit(tcp_t *tcp) 12571 { 12572 uint32_t snxt; 12573 uint32_t smax; 12574 int32_t win; 12575 int32_t mss; 12576 int32_t off; 12577 int32_t burst = tcp->tcp_snd_burst; 12578 mblk_t *snxt_mp; 12579 tcp_stack_t *tcps = tcp->tcp_tcps; 12580 12581 /* 12582 * Note that tcp_rexmit can be set even though TCP has retransmitted 12583 * all unack'ed segments. 12584 */ 12585 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12586 smax = tcp->tcp_rexmit_max; 12587 snxt = tcp->tcp_rexmit_nxt; 12588 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12589 snxt = tcp->tcp_suna; 12590 } 12591 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12592 win -= snxt - tcp->tcp_suna; 12593 mss = tcp->tcp_mss; 12594 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12595 12596 while (SEQ_LT(snxt, smax) && (win > 0) && 12597 (burst > 0) && (snxt_mp != NULL)) { 12598 mblk_t *xmit_mp; 12599 mblk_t *old_snxt_mp = snxt_mp; 12600 uint32_t cnt = mss; 12601 12602 if (win < cnt) { 12603 cnt = win; 12604 } 12605 if (SEQ_GT(snxt + cnt, smax)) { 12606 cnt = smax - snxt; 12607 } 12608 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12609 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12610 if (xmit_mp == NULL) 12611 return; 12612 12613 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12614 12615 snxt += cnt; 12616 win -= cnt; 12617 /* 12618 * Update the send timestamp to avoid false 12619 * retransmission. 12620 */ 12621 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12622 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12623 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12624 12625 tcp->tcp_rexmit_nxt = snxt; 12626 burst--; 12627 } 12628 /* 12629 * If we have transmitted all we have at the time 12630 * we started the retranmission, we can leave 12631 * the rest of the job to tcp_wput_data(). But we 12632 * need to check the send window first. If the 12633 * win is not 0, go on with tcp_wput_data(). 12634 */ 12635 if (SEQ_LT(snxt, smax) || win == 0) { 12636 return; 12637 } 12638 } 12639 /* Only call tcp_wput_data() if there is data to be sent. */ 12640 if (tcp->tcp_unsent) { 12641 tcp_wput_data(tcp, NULL, B_FALSE); 12642 } 12643 } 12644 12645 /* 12646 * Process all TCP option in SYN segment. Note that this function should 12647 * be called after tcp_adapt_ire() is called so that the necessary info 12648 * from IRE is already set in the tcp structure. 12649 * 12650 * This function sets up the correct tcp_mss value according to the 12651 * MSS option value and our header size. It also sets up the window scale 12652 * and timestamp values, and initialize SACK info blocks. But it does not 12653 * change receive window size after setting the tcp_mss value. The caller 12654 * should do the appropriate change. 12655 */ 12656 void 12657 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12658 { 12659 int options; 12660 tcp_opt_t tcpopt; 12661 uint32_t mss_max; 12662 char *tmp_tcph; 12663 tcp_stack_t *tcps = tcp->tcp_tcps; 12664 12665 tcpopt.tcp = NULL; 12666 options = tcp_parse_options(tcph, &tcpopt); 12667 12668 /* 12669 * Process MSS option. Note that MSS option value does not account 12670 * for IP or TCP options. This means that it is equal to MTU - minimum 12671 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12672 * IPv6. 12673 */ 12674 if (!(options & TCP_OPT_MSS_PRESENT)) { 12675 if (tcp->tcp_ipversion == IPV4_VERSION) 12676 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12677 else 12678 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12679 } else { 12680 if (tcp->tcp_ipversion == IPV4_VERSION) 12681 mss_max = tcps->tcps_mss_max_ipv4; 12682 else 12683 mss_max = tcps->tcps_mss_max_ipv6; 12684 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12685 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12686 else if (tcpopt.tcp_opt_mss > mss_max) 12687 tcpopt.tcp_opt_mss = mss_max; 12688 } 12689 12690 /* Process Window Scale option. */ 12691 if (options & TCP_OPT_WSCALE_PRESENT) { 12692 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12693 tcp->tcp_snd_ws_ok = B_TRUE; 12694 } else { 12695 tcp->tcp_snd_ws = B_FALSE; 12696 tcp->tcp_snd_ws_ok = B_FALSE; 12697 tcp->tcp_rcv_ws = B_FALSE; 12698 } 12699 12700 /* Process Timestamp option. */ 12701 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12702 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12703 tmp_tcph = (char *)tcp->tcp_tcph; 12704 12705 tcp->tcp_snd_ts_ok = B_TRUE; 12706 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12707 tcp->tcp_last_rcv_lbolt = lbolt64; 12708 ASSERT(OK_32PTR(tmp_tcph)); 12709 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12710 12711 /* Fill in our template header with basic timestamp option. */ 12712 tmp_tcph += tcp->tcp_tcp_hdr_len; 12713 tmp_tcph[0] = TCPOPT_NOP; 12714 tmp_tcph[1] = TCPOPT_NOP; 12715 tmp_tcph[2] = TCPOPT_TSTAMP; 12716 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12717 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12718 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12719 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12720 } else { 12721 tcp->tcp_snd_ts_ok = B_FALSE; 12722 } 12723 12724 /* 12725 * Process SACK options. If SACK is enabled for this connection, 12726 * then allocate the SACK info structure. Note the following ways 12727 * when tcp_snd_sack_ok is set to true. 12728 * 12729 * For active connection: in tcp_adapt_ire() called in 12730 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12731 * is checked. 12732 * 12733 * For passive connection: in tcp_adapt_ire() called in 12734 * tcp_accept_comm(). 12735 * 12736 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12737 * That check makes sure that if we did not send a SACK OK option, 12738 * we will not enable SACK for this connection even though the other 12739 * side sends us SACK OK option. For active connection, the SACK 12740 * info structure has already been allocated. So we need to free 12741 * it if SACK is disabled. 12742 */ 12743 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12744 (tcp->tcp_snd_sack_ok || 12745 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12746 /* This should be true only in the passive case. */ 12747 if (tcp->tcp_sack_info == NULL) { 12748 ASSERT(TCP_IS_DETACHED(tcp)); 12749 tcp->tcp_sack_info = 12750 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12751 } 12752 if (tcp->tcp_sack_info == NULL) { 12753 tcp->tcp_snd_sack_ok = B_FALSE; 12754 } else { 12755 tcp->tcp_snd_sack_ok = B_TRUE; 12756 if (tcp->tcp_snd_ts_ok) { 12757 tcp->tcp_max_sack_blk = 3; 12758 } else { 12759 tcp->tcp_max_sack_blk = 4; 12760 } 12761 } 12762 } else { 12763 /* 12764 * Resetting tcp_snd_sack_ok to B_FALSE so that 12765 * no SACK info will be used for this 12766 * connection. This assumes that SACK usage 12767 * permission is negotiated. This may need 12768 * to be changed once this is clarified. 12769 */ 12770 if (tcp->tcp_sack_info != NULL) { 12771 ASSERT(tcp->tcp_notsack_list == NULL); 12772 kmem_cache_free(tcp_sack_info_cache, 12773 tcp->tcp_sack_info); 12774 tcp->tcp_sack_info = NULL; 12775 } 12776 tcp->tcp_snd_sack_ok = B_FALSE; 12777 } 12778 12779 /* 12780 * Now we know the exact TCP/IP header length, subtract 12781 * that from tcp_mss to get our side's MSS. 12782 */ 12783 tcp->tcp_mss -= tcp->tcp_hdr_len; 12784 /* 12785 * Here we assume that the other side's header size will be equal to 12786 * our header size. We calculate the real MSS accordingly. Need to 12787 * take into additional stuffs IPsec puts in. 12788 * 12789 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12790 */ 12791 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12792 ((tcp->tcp_ipversion == IPV4_VERSION ? 12793 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12794 12795 /* 12796 * Set MSS to the smaller one of both ends of the connection. 12797 * We should not have called tcp_mss_set() before, but our 12798 * side of the MSS should have been set to a proper value 12799 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12800 * STREAM head parameters properly. 12801 * 12802 * If we have a larger-than-16-bit window but the other side 12803 * didn't want to do window scale, tcp_rwnd_set() will take 12804 * care of that. 12805 */ 12806 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12807 } 12808 12809 /* 12810 * Sends the T_CONN_IND to the listener. The caller calls this 12811 * functions via squeue to get inside the listener's perimeter 12812 * once the 3 way hand shake is done a T_CONN_IND needs to be 12813 * sent. As an optimization, the caller can call this directly 12814 * if listener's perimeter is same as eager's. 12815 */ 12816 /* ARGSUSED */ 12817 void 12818 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12819 { 12820 conn_t *lconnp = (conn_t *)arg; 12821 tcp_t *listener = lconnp->conn_tcp; 12822 tcp_t *tcp; 12823 struct T_conn_ind *conn_ind; 12824 ipaddr_t *addr_cache; 12825 boolean_t need_send_conn_ind = B_FALSE; 12826 tcp_stack_t *tcps = listener->tcp_tcps; 12827 12828 /* retrieve the eager */ 12829 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12830 ASSERT(conn_ind->OPT_offset != 0 && 12831 conn_ind->OPT_length == sizeof (intptr_t)); 12832 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12833 conn_ind->OPT_length); 12834 12835 /* 12836 * TLI/XTI applications will get confused by 12837 * sending eager as an option since it violates 12838 * the option semantics. So remove the eager as 12839 * option since TLI/XTI app doesn't need it anyway. 12840 */ 12841 if (!TCP_IS_SOCKET(listener)) { 12842 conn_ind->OPT_length = 0; 12843 conn_ind->OPT_offset = 0; 12844 } 12845 if (listener->tcp_state == TCPS_CLOSED || 12846 TCP_IS_DETACHED(listener)) { 12847 /* 12848 * If listener has closed, it would have caused a 12849 * a cleanup/blowoff to happen for the eager. We 12850 * just need to return. 12851 */ 12852 freemsg(mp); 12853 return; 12854 } 12855 12856 12857 /* 12858 * if the conn_req_q is full defer passing up the 12859 * T_CONN_IND until space is availabe after t_accept() 12860 * processing 12861 */ 12862 mutex_enter(&listener->tcp_eager_lock); 12863 12864 /* 12865 * Take the eager out, if it is in the list of droppable eagers 12866 * as we are here because the 3W handshake is over. 12867 */ 12868 MAKE_UNDROPPABLE(tcp); 12869 12870 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12871 tcp_t *tail; 12872 12873 /* 12874 * The eager already has an extra ref put in tcp_rput_data 12875 * so that it stays till accept comes back even though it 12876 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12877 */ 12878 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12879 listener->tcp_conn_req_cnt_q0--; 12880 listener->tcp_conn_req_cnt_q++; 12881 12882 /* Move from SYN_RCVD to ESTABLISHED list */ 12883 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12884 tcp->tcp_eager_prev_q0; 12885 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12886 tcp->tcp_eager_next_q0; 12887 tcp->tcp_eager_prev_q0 = NULL; 12888 tcp->tcp_eager_next_q0 = NULL; 12889 12890 /* 12891 * Insert at end of the queue because sockfs 12892 * sends down T_CONN_RES in chronological 12893 * order. Leaving the older conn indications 12894 * at front of the queue helps reducing search 12895 * time. 12896 */ 12897 tail = listener->tcp_eager_last_q; 12898 if (tail != NULL) 12899 tail->tcp_eager_next_q = tcp; 12900 else 12901 listener->tcp_eager_next_q = tcp; 12902 listener->tcp_eager_last_q = tcp; 12903 tcp->tcp_eager_next_q = NULL; 12904 /* 12905 * Delay sending up the T_conn_ind until we are 12906 * done with the eager. Once we have have sent up 12907 * the T_conn_ind, the accept can potentially complete 12908 * any time and release the refhold we have on the eager. 12909 */ 12910 need_send_conn_ind = B_TRUE; 12911 } else { 12912 /* 12913 * Defer connection on q0 and set deferred 12914 * connection bit true 12915 */ 12916 tcp->tcp_conn_def_q0 = B_TRUE; 12917 12918 /* take tcp out of q0 ... */ 12919 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12920 tcp->tcp_eager_next_q0; 12921 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12922 tcp->tcp_eager_prev_q0; 12923 12924 /* ... and place it at the end of q0 */ 12925 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12926 tcp->tcp_eager_next_q0 = listener; 12927 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12928 listener->tcp_eager_prev_q0 = tcp; 12929 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12930 } 12931 12932 /* we have timed out before */ 12933 if (tcp->tcp_syn_rcvd_timeout != 0) { 12934 tcp->tcp_syn_rcvd_timeout = 0; 12935 listener->tcp_syn_rcvd_timeout--; 12936 if (listener->tcp_syn_defense && 12937 listener->tcp_syn_rcvd_timeout <= 12938 (tcps->tcps_conn_req_max_q0 >> 5) && 12939 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12940 listener->tcp_last_rcv_lbolt)) { 12941 /* 12942 * Turn off the defense mode if we 12943 * believe the SYN attack is over. 12944 */ 12945 listener->tcp_syn_defense = B_FALSE; 12946 if (listener->tcp_ip_addr_cache) { 12947 kmem_free((void *)listener->tcp_ip_addr_cache, 12948 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12949 listener->tcp_ip_addr_cache = NULL; 12950 } 12951 } 12952 } 12953 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12954 if (addr_cache != NULL) { 12955 /* 12956 * We have finished a 3-way handshake with this 12957 * remote host. This proves the IP addr is good. 12958 * Cache it! 12959 */ 12960 addr_cache[IP_ADDR_CACHE_HASH( 12961 tcp->tcp_remote)] = tcp->tcp_remote; 12962 } 12963 mutex_exit(&listener->tcp_eager_lock); 12964 if (need_send_conn_ind) 12965 putnext(listener->tcp_rq, mp); 12966 } 12967 12968 mblk_t * 12969 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12970 uint_t *ifindexp, ip6_pkt_t *ippp) 12971 { 12972 ip_pktinfo_t *pinfo; 12973 ip6_t *ip6h; 12974 uchar_t *rptr; 12975 mblk_t *first_mp = mp; 12976 boolean_t mctl_present = B_FALSE; 12977 uint_t ifindex = 0; 12978 ip6_pkt_t ipp; 12979 uint_t ipvers; 12980 uint_t ip_hdr_len; 12981 tcp_stack_t *tcps = tcp->tcp_tcps; 12982 12983 rptr = mp->b_rptr; 12984 ASSERT(OK_32PTR(rptr)); 12985 ASSERT(tcp != NULL); 12986 ipp.ipp_fields = 0; 12987 12988 switch DB_TYPE(mp) { 12989 case M_CTL: 12990 mp = mp->b_cont; 12991 if (mp == NULL) { 12992 freemsg(first_mp); 12993 return (NULL); 12994 } 12995 if (DB_TYPE(mp) != M_DATA) { 12996 freemsg(first_mp); 12997 return (NULL); 12998 } 12999 mctl_present = B_TRUE; 13000 break; 13001 case M_DATA: 13002 break; 13003 default: 13004 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 13005 freemsg(mp); 13006 return (NULL); 13007 } 13008 ipvers = IPH_HDR_VERSION(rptr); 13009 if (ipvers == IPV4_VERSION) { 13010 if (tcp == NULL) { 13011 ip_hdr_len = IPH_HDR_LENGTH(rptr); 13012 goto done; 13013 } 13014 13015 ipp.ipp_fields |= IPPF_HOPLIMIT; 13016 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 13017 13018 /* 13019 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 13020 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 13021 */ 13022 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 13023 mctl_present) { 13024 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 13025 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 13026 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 13027 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 13028 ipp.ipp_fields |= IPPF_IFINDEX; 13029 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 13030 ifindex = pinfo->ip_pkt_ifindex; 13031 } 13032 freeb(first_mp); 13033 mctl_present = B_FALSE; 13034 } 13035 ip_hdr_len = IPH_HDR_LENGTH(rptr); 13036 } else { 13037 ip6h = (ip6_t *)rptr; 13038 13039 ASSERT(ipvers == IPV6_VERSION); 13040 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 13041 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 13042 ipp.ipp_hoplimit = ip6h->ip6_hops; 13043 13044 if (ip6h->ip6_nxt != IPPROTO_TCP) { 13045 uint8_t nexthdrp; 13046 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13047 13048 /* Look for ifindex information */ 13049 if (ip6h->ip6_nxt == IPPROTO_RAW) { 13050 ip6i_t *ip6i = (ip6i_t *)ip6h; 13051 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 13052 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13053 freemsg(first_mp); 13054 return (NULL); 13055 } 13056 13057 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 13058 ASSERT(ip6i->ip6i_ifindex != 0); 13059 ipp.ipp_fields |= IPPF_IFINDEX; 13060 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 13061 ifindex = ip6i->ip6i_ifindex; 13062 } 13063 rptr = (uchar_t *)&ip6i[1]; 13064 mp->b_rptr = rptr; 13065 if (rptr == mp->b_wptr) { 13066 mblk_t *mp1; 13067 mp1 = mp->b_cont; 13068 freeb(mp); 13069 mp = mp1; 13070 rptr = mp->b_rptr; 13071 } 13072 if (MBLKL(mp) < IPV6_HDR_LEN + 13073 sizeof (tcph_t)) { 13074 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13075 freemsg(first_mp); 13076 return (NULL); 13077 } 13078 ip6h = (ip6_t *)rptr; 13079 } 13080 13081 /* 13082 * Find any potentially interesting extension headers 13083 * as well as the length of the IPv6 + extension 13084 * headers. 13085 */ 13086 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 13087 /* Verify if this is a TCP packet */ 13088 if (nexthdrp != IPPROTO_TCP) { 13089 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13090 freemsg(first_mp); 13091 return (NULL); 13092 } 13093 } else { 13094 ip_hdr_len = IPV6_HDR_LEN; 13095 } 13096 } 13097 13098 done: 13099 if (ipversp != NULL) 13100 *ipversp = ipvers; 13101 if (ip_hdr_lenp != NULL) 13102 *ip_hdr_lenp = ip_hdr_len; 13103 if (ippp != NULL) 13104 *ippp = ipp; 13105 if (ifindexp != NULL) 13106 *ifindexp = ifindex; 13107 if (mctl_present) { 13108 freeb(first_mp); 13109 } 13110 return (mp); 13111 } 13112 13113 /* 13114 * Handle M_DATA messages from IP. Its called directly from IP via 13115 * squeue for AF_INET type sockets fast path. No M_CTL are expected 13116 * in this path. 13117 * 13118 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 13119 * v4 and v6), we are called through tcp_input() and a M_CTL can 13120 * be present for options but tcp_find_pktinfo() deals with it. We 13121 * only expect M_DATA packets after tcp_find_pktinfo() is done. 13122 * 13123 * The first argument is always the connp/tcp to which the mp belongs. 13124 * There are no exceptions to this rule. The caller has already put 13125 * a reference on this connp/tcp and once tcp_rput_data() returns, 13126 * the squeue will do the refrele. 13127 * 13128 * The TH_SYN for the listener directly go to tcp_conn_request via 13129 * squeue. 13130 * 13131 * sqp: NULL = recursive, sqp != NULL means called from squeue 13132 */ 13133 void 13134 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 13135 { 13136 int32_t bytes_acked; 13137 int32_t gap; 13138 mblk_t *mp1; 13139 uint_t flags; 13140 uint32_t new_swnd = 0; 13141 uchar_t *iphdr; 13142 uchar_t *rptr; 13143 int32_t rgap; 13144 uint32_t seg_ack; 13145 int seg_len; 13146 uint_t ip_hdr_len; 13147 uint32_t seg_seq; 13148 tcph_t *tcph; 13149 int urp; 13150 tcp_opt_t tcpopt; 13151 uint_t ipvers; 13152 ip6_pkt_t ipp; 13153 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 13154 uint32_t cwnd; 13155 uint32_t add; 13156 int npkt; 13157 int mss; 13158 conn_t *connp = (conn_t *)arg; 13159 squeue_t *sqp = (squeue_t *)arg2; 13160 tcp_t *tcp = connp->conn_tcp; 13161 tcp_stack_t *tcps = tcp->tcp_tcps; 13162 13163 /* 13164 * RST from fused tcp loopback peer should trigger an unfuse. 13165 */ 13166 if (tcp->tcp_fused) { 13167 TCP_STAT(tcps, tcp_fusion_aborted); 13168 tcp_unfuse(tcp); 13169 } 13170 13171 iphdr = mp->b_rptr; 13172 rptr = mp->b_rptr; 13173 ASSERT(OK_32PTR(rptr)); 13174 13175 /* 13176 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 13177 * processing here. For rest call tcp_find_pktinfo to fill up the 13178 * necessary information. 13179 */ 13180 if (IPCL_IS_TCP4(connp)) { 13181 ipvers = IPV4_VERSION; 13182 ip_hdr_len = IPH_HDR_LENGTH(rptr); 13183 } else { 13184 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 13185 NULL, &ipp); 13186 if (mp == NULL) { 13187 TCP_STAT(tcps, tcp_rput_v6_error); 13188 return; 13189 } 13190 iphdr = mp->b_rptr; 13191 rptr = mp->b_rptr; 13192 } 13193 ASSERT(DB_TYPE(mp) == M_DATA); 13194 13195 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13196 seg_seq = ABE32_TO_U32(tcph->th_seq); 13197 seg_ack = ABE32_TO_U32(tcph->th_ack); 13198 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 13199 seg_len = (int)(mp->b_wptr - rptr) - 13200 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 13201 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 13202 do { 13203 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 13204 (uintptr_t)INT_MAX); 13205 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 13206 } while ((mp1 = mp1->b_cont) != NULL && 13207 mp1->b_datap->db_type == M_DATA); 13208 } 13209 13210 if (tcp->tcp_state == TCPS_TIME_WAIT) { 13211 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 13212 seg_len, tcph); 13213 return; 13214 } 13215 13216 if (sqp != NULL) { 13217 /* 13218 * This is the correct place to update tcp_last_recv_time. Note 13219 * that it is also updated for tcp structure that belongs to 13220 * global and listener queues which do not really need updating. 13221 * But that should not cause any harm. And it is updated for 13222 * all kinds of incoming segments, not only for data segments. 13223 */ 13224 tcp->tcp_last_recv_time = lbolt; 13225 } 13226 13227 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 13228 13229 BUMP_LOCAL(tcp->tcp_ibsegs); 13230 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13231 13232 if ((flags & TH_URG) && sqp != NULL) { 13233 /* 13234 * TCP can't handle urgent pointers that arrive before 13235 * the connection has been accept()ed since it can't 13236 * buffer OOB data. Discard segment if this happens. 13237 * 13238 * We can't just rely on a non-null tcp_listener to indicate 13239 * that the accept() has completed since unlinking of the 13240 * eager and completion of the accept are not atomic. 13241 * tcp_detached, when it is not set (B_FALSE) indicates 13242 * that the accept() has completed. 13243 * 13244 * Nor can it reassemble urgent pointers, so discard 13245 * if it's not the next segment expected. 13246 * 13247 * Otherwise, collapse chain into one mblk (discard if 13248 * that fails). This makes sure the headers, retransmitted 13249 * data, and new data all are in the same mblk. 13250 */ 13251 ASSERT(mp != NULL); 13252 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 13253 freemsg(mp); 13254 return; 13255 } 13256 /* Update pointers into message */ 13257 iphdr = rptr = mp->b_rptr; 13258 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13259 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 13260 /* 13261 * Since we can't handle any data with this urgent 13262 * pointer that is out of sequence, we expunge 13263 * the data. This allows us to still register 13264 * the urgent mark and generate the M_PCSIG, 13265 * which we can do. 13266 */ 13267 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13268 seg_len = 0; 13269 } 13270 } 13271 13272 switch (tcp->tcp_state) { 13273 case TCPS_SYN_SENT: 13274 if (flags & TH_ACK) { 13275 /* 13276 * Note that our stack cannot send data before a 13277 * connection is established, therefore the 13278 * following check is valid. Otherwise, it has 13279 * to be changed. 13280 */ 13281 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 13282 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13283 freemsg(mp); 13284 if (flags & TH_RST) 13285 return; 13286 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 13287 tcp, seg_ack, 0, TH_RST); 13288 return; 13289 } 13290 ASSERT(tcp->tcp_suna + 1 == seg_ack); 13291 } 13292 if (flags & TH_RST) { 13293 freemsg(mp); 13294 if (flags & TH_ACK) 13295 (void) tcp_clean_death(tcp, 13296 ECONNREFUSED, 13); 13297 return; 13298 } 13299 if (!(flags & TH_SYN)) { 13300 freemsg(mp); 13301 return; 13302 } 13303 13304 /* Process all TCP options. */ 13305 tcp_process_options(tcp, tcph); 13306 /* 13307 * The following changes our rwnd to be a multiple of the 13308 * MIN(peer MSS, our MSS) for performance reason. 13309 */ 13310 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 13311 tcp->tcp_mss)); 13312 13313 /* Is the other end ECN capable? */ 13314 if (tcp->tcp_ecn_ok) { 13315 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 13316 tcp->tcp_ecn_ok = B_FALSE; 13317 } 13318 } 13319 /* 13320 * Clear ECN flags because it may interfere with later 13321 * processing. 13322 */ 13323 flags &= ~(TH_ECE|TH_CWR); 13324 13325 tcp->tcp_irs = seg_seq; 13326 tcp->tcp_rack = seg_seq; 13327 tcp->tcp_rnxt = seg_seq + 1; 13328 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13329 if (!TCP_IS_DETACHED(tcp)) { 13330 /* Allocate room for SACK options if needed. */ 13331 if (tcp->tcp_snd_sack_ok) { 13332 (void) mi_set_sth_wroff(tcp->tcp_rq, 13333 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 13334 (tcp->tcp_loopback ? 0 : 13335 tcps->tcps_wroff_xtra)); 13336 } else { 13337 (void) mi_set_sth_wroff(tcp->tcp_rq, 13338 tcp->tcp_hdr_len + 13339 (tcp->tcp_loopback ? 0 : 13340 tcps->tcps_wroff_xtra)); 13341 } 13342 } 13343 if (flags & TH_ACK) { 13344 /* 13345 * If we can't get the confirmation upstream, pretend 13346 * we didn't even see this one. 13347 * 13348 * XXX: how can we pretend we didn't see it if we 13349 * have updated rnxt et. al. 13350 * 13351 * For loopback we defer sending up the T_CONN_CON 13352 * until after some checks below. 13353 */ 13354 mp1 = NULL; 13355 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13356 tcp->tcp_loopback ? &mp1 : NULL)) { 13357 freemsg(mp); 13358 return; 13359 } 13360 /* SYN was acked - making progress */ 13361 if (tcp->tcp_ipversion == IPV6_VERSION) 13362 tcp->tcp_ip_forward_progress = B_TRUE; 13363 13364 /* One for the SYN */ 13365 tcp->tcp_suna = tcp->tcp_iss + 1; 13366 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13367 tcp->tcp_state = TCPS_ESTABLISHED; 13368 13369 /* 13370 * If SYN was retransmitted, need to reset all 13371 * retransmission info. This is because this 13372 * segment will be treated as a dup ACK. 13373 */ 13374 if (tcp->tcp_rexmit) { 13375 tcp->tcp_rexmit = B_FALSE; 13376 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13377 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13378 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13379 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13380 tcp->tcp_ms_we_have_waited = 0; 13381 13382 /* 13383 * Set tcp_cwnd back to 1 MSS, per 13384 * recommendation from 13385 * draft-floyd-incr-init-win-01.txt, 13386 * Increasing TCP's Initial Window. 13387 */ 13388 tcp->tcp_cwnd = tcp->tcp_mss; 13389 } 13390 13391 tcp->tcp_swl1 = seg_seq; 13392 tcp->tcp_swl2 = seg_ack; 13393 13394 new_swnd = BE16_TO_U16(tcph->th_win); 13395 tcp->tcp_swnd = new_swnd; 13396 if (new_swnd > tcp->tcp_max_swnd) 13397 tcp->tcp_max_swnd = new_swnd; 13398 13399 /* 13400 * Always send the three-way handshake ack immediately 13401 * in order to make the connection complete as soon as 13402 * possible on the accepting host. 13403 */ 13404 flags |= TH_ACK_NEEDED; 13405 13406 /* 13407 * Special case for loopback. At this point we have 13408 * received SYN-ACK from the remote endpoint. In 13409 * order to ensure that both endpoints reach the 13410 * fused state prior to any data exchange, the final 13411 * ACK needs to be sent before we indicate T_CONN_CON 13412 * to the module upstream. 13413 */ 13414 if (tcp->tcp_loopback) { 13415 mblk_t *ack_mp; 13416 13417 ASSERT(!tcp->tcp_unfusable); 13418 ASSERT(mp1 != NULL); 13419 /* 13420 * For loopback, we always get a pure SYN-ACK 13421 * and only need to send back the final ACK 13422 * with no data (this is because the other 13423 * tcp is ours and we don't do T/TCP). This 13424 * final ACK triggers the passive side to 13425 * perform fusion in ESTABLISHED state. 13426 */ 13427 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13428 if (tcp->tcp_ack_tid != 0) { 13429 (void) TCP_TIMER_CANCEL(tcp, 13430 tcp->tcp_ack_tid); 13431 tcp->tcp_ack_tid = 0; 13432 } 13433 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13434 BUMP_LOCAL(tcp->tcp_obsegs); 13435 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13436 13437 /* Send up T_CONN_CON */ 13438 putnext(tcp->tcp_rq, mp1); 13439 13440 freemsg(mp); 13441 return; 13442 } 13443 /* 13444 * Forget fusion; we need to handle more 13445 * complex cases below. Send the deferred 13446 * T_CONN_CON message upstream and proceed 13447 * as usual. Mark this tcp as not capable 13448 * of fusion. 13449 */ 13450 TCP_STAT(tcps, tcp_fusion_unfusable); 13451 tcp->tcp_unfusable = B_TRUE; 13452 putnext(tcp->tcp_rq, mp1); 13453 } 13454 13455 /* 13456 * Check to see if there is data to be sent. If 13457 * yes, set the transmit flag. Then check to see 13458 * if received data processing needs to be done. 13459 * If not, go straight to xmit_check. This short 13460 * cut is OK as we don't support T/TCP. 13461 */ 13462 if (tcp->tcp_unsent) 13463 flags |= TH_XMIT_NEEDED; 13464 13465 if (seg_len == 0 && !(flags & TH_URG)) { 13466 freemsg(mp); 13467 goto xmit_check; 13468 } 13469 13470 flags &= ~TH_SYN; 13471 seg_seq++; 13472 break; 13473 } 13474 tcp->tcp_state = TCPS_SYN_RCVD; 13475 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13476 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13477 if (mp1) { 13478 DB_CPID(mp1) = tcp->tcp_cpid; 13479 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13480 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13481 } 13482 freemsg(mp); 13483 return; 13484 case TCPS_SYN_RCVD: 13485 if (flags & TH_ACK) { 13486 /* 13487 * In this state, a SYN|ACK packet is either bogus 13488 * because the other side must be ACKing our SYN which 13489 * indicates it has seen the ACK for their SYN and 13490 * shouldn't retransmit it or we're crossing SYNs 13491 * on active open. 13492 */ 13493 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13494 freemsg(mp); 13495 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13496 tcp, seg_ack, 0, TH_RST); 13497 return; 13498 } 13499 /* 13500 * NOTE: RFC 793 pg. 72 says this should be 13501 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13502 * but that would mean we have an ack that ignored 13503 * our SYN. 13504 */ 13505 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13506 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13507 freemsg(mp); 13508 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13509 tcp, seg_ack, 0, TH_RST); 13510 return; 13511 } 13512 } 13513 break; 13514 case TCPS_LISTEN: 13515 /* 13516 * Only a TLI listener can come through this path when a 13517 * acceptor is going back to be a listener and a packet 13518 * for the acceptor hits the classifier. For a socket 13519 * listener, this can never happen because a listener 13520 * can never accept connection on itself and hence a 13521 * socket acceptor can not go back to being a listener. 13522 */ 13523 ASSERT(!TCP_IS_SOCKET(tcp)); 13524 /*FALLTHRU*/ 13525 case TCPS_CLOSED: 13526 case TCPS_BOUND: { 13527 conn_t *new_connp; 13528 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13529 13530 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13531 if (new_connp != NULL) { 13532 tcp_reinput(new_connp, mp, connp->conn_sqp); 13533 return; 13534 } 13535 /* We failed to classify. For now just drop the packet */ 13536 freemsg(mp); 13537 return; 13538 } 13539 case TCPS_IDLE: 13540 /* 13541 * Handle the case where the tcp_clean_death() has happened 13542 * on a connection (application hasn't closed yet) but a packet 13543 * was already queued on squeue before tcp_clean_death() 13544 * was processed. Calling tcp_clean_death() twice on same 13545 * connection can result in weird behaviour. 13546 */ 13547 freemsg(mp); 13548 return; 13549 default: 13550 break; 13551 } 13552 13553 /* 13554 * Already on the correct queue/perimeter. 13555 * If this is a detached connection and not an eager 13556 * connection hanging off a listener then new data 13557 * (past the FIN) will cause a reset. 13558 * We do a special check here where it 13559 * is out of the main line, rather than check 13560 * if we are detached every time we see new 13561 * data down below. 13562 */ 13563 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13564 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13565 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13566 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13567 13568 freemsg(mp); 13569 /* 13570 * This could be an SSL closure alert. We're detached so just 13571 * acknowledge it this last time. 13572 */ 13573 if (tcp->tcp_kssl_ctx != NULL) { 13574 kssl_release_ctx(tcp->tcp_kssl_ctx); 13575 tcp->tcp_kssl_ctx = NULL; 13576 13577 tcp->tcp_rnxt += seg_len; 13578 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13579 flags |= TH_ACK_NEEDED; 13580 goto ack_check; 13581 } 13582 13583 tcp_xmit_ctl("new data when detached", tcp, 13584 tcp->tcp_snxt, 0, TH_RST); 13585 (void) tcp_clean_death(tcp, EPROTO, 12); 13586 return; 13587 } 13588 13589 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13590 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13591 new_swnd = BE16_TO_U16(tcph->th_win) << 13592 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13593 13594 if (tcp->tcp_snd_ts_ok) { 13595 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13596 /* 13597 * This segment is not acceptable. 13598 * Drop it and send back an ACK. 13599 */ 13600 freemsg(mp); 13601 flags |= TH_ACK_NEEDED; 13602 goto ack_check; 13603 } 13604 } else if (tcp->tcp_snd_sack_ok) { 13605 ASSERT(tcp->tcp_sack_info != NULL); 13606 tcpopt.tcp = tcp; 13607 /* 13608 * SACK info in already updated in tcp_parse_options. Ignore 13609 * all other TCP options... 13610 */ 13611 (void) tcp_parse_options(tcph, &tcpopt); 13612 } 13613 try_again:; 13614 mss = tcp->tcp_mss; 13615 gap = seg_seq - tcp->tcp_rnxt; 13616 rgap = tcp->tcp_rwnd - (gap + seg_len); 13617 /* 13618 * gap is the amount of sequence space between what we expect to see 13619 * and what we got for seg_seq. A positive value for gap means 13620 * something got lost. A negative value means we got some old stuff. 13621 */ 13622 if (gap < 0) { 13623 /* Old stuff present. Is the SYN in there? */ 13624 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13625 (seg_len != 0)) { 13626 flags &= ~TH_SYN; 13627 seg_seq++; 13628 urp--; 13629 /* Recompute the gaps after noting the SYN. */ 13630 goto try_again; 13631 } 13632 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13633 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13634 (seg_len > -gap ? -gap : seg_len)); 13635 /* Remove the old stuff from seg_len. */ 13636 seg_len += gap; 13637 /* 13638 * Anything left? 13639 * Make sure to check for unack'd FIN when rest of data 13640 * has been previously ack'd. 13641 */ 13642 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13643 /* 13644 * Resets are only valid if they lie within our offered 13645 * window. If the RST bit is set, we just ignore this 13646 * segment. 13647 */ 13648 if (flags & TH_RST) { 13649 freemsg(mp); 13650 return; 13651 } 13652 13653 /* 13654 * The arriving of dup data packets indicate that we 13655 * may have postponed an ack for too long, or the other 13656 * side's RTT estimate is out of shape. Start acking 13657 * more often. 13658 */ 13659 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13660 tcp->tcp_rack_cnt >= 1 && 13661 tcp->tcp_rack_abs_max > 2) { 13662 tcp->tcp_rack_abs_max--; 13663 } 13664 tcp->tcp_rack_cur_max = 1; 13665 13666 /* 13667 * This segment is "unacceptable". None of its 13668 * sequence space lies within our advertized window. 13669 * 13670 * Adjust seg_len to the original value for tracing. 13671 */ 13672 seg_len -= gap; 13673 if (tcp->tcp_debug) { 13674 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13675 "tcp_rput: unacceptable, gap %d, rgap %d, " 13676 "flags 0x%x, seg_seq %u, seg_ack %u, " 13677 "seg_len %d, rnxt %u, snxt %u, %s", 13678 gap, rgap, flags, seg_seq, seg_ack, 13679 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13680 tcp_display(tcp, NULL, 13681 DISP_ADDR_AND_PORT)); 13682 } 13683 13684 /* 13685 * Arrange to send an ACK in response to the 13686 * unacceptable segment per RFC 793 page 69. There 13687 * is only one small difference between ours and the 13688 * acceptability test in the RFC - we accept ACK-only 13689 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13690 * will be generated. 13691 * 13692 * Note that we have to ACK an ACK-only packet at least 13693 * for stacks that send 0-length keep-alives with 13694 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13695 * section 4.2.3.6. As long as we don't ever generate 13696 * an unacceptable packet in response to an incoming 13697 * packet that is unacceptable, it should not cause 13698 * "ACK wars". 13699 */ 13700 flags |= TH_ACK_NEEDED; 13701 13702 /* 13703 * Continue processing this segment in order to use the 13704 * ACK information it contains, but skip all other 13705 * sequence-number processing. Processing the ACK 13706 * information is necessary in order to 13707 * re-synchronize connections that may have lost 13708 * synchronization. 13709 * 13710 * We clear seg_len and flag fields related to 13711 * sequence number processing as they are not 13712 * to be trusted for an unacceptable segment. 13713 */ 13714 seg_len = 0; 13715 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13716 goto process_ack; 13717 } 13718 13719 /* Fix seg_seq, and chew the gap off the front. */ 13720 seg_seq = tcp->tcp_rnxt; 13721 urp += gap; 13722 do { 13723 mblk_t *mp2; 13724 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13725 (uintptr_t)UINT_MAX); 13726 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13727 if (gap > 0) { 13728 mp->b_rptr = mp->b_wptr - gap; 13729 break; 13730 } 13731 mp2 = mp; 13732 mp = mp->b_cont; 13733 freeb(mp2); 13734 } while (gap < 0); 13735 /* 13736 * If the urgent data has already been acknowledged, we 13737 * should ignore TH_URG below 13738 */ 13739 if (urp < 0) 13740 flags &= ~TH_URG; 13741 } 13742 /* 13743 * rgap is the amount of stuff received out of window. A negative 13744 * value is the amount out of window. 13745 */ 13746 if (rgap < 0) { 13747 mblk_t *mp2; 13748 13749 if (tcp->tcp_rwnd == 0) { 13750 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13751 } else { 13752 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13753 UPDATE_MIB(&tcps->tcps_mib, 13754 tcpInDataPastWinBytes, -rgap); 13755 } 13756 13757 /* 13758 * seg_len does not include the FIN, so if more than 13759 * just the FIN is out of window, we act like we don't 13760 * see it. (If just the FIN is out of window, rgap 13761 * will be zero and we will go ahead and acknowledge 13762 * the FIN.) 13763 */ 13764 flags &= ~TH_FIN; 13765 13766 /* Fix seg_len and make sure there is something left. */ 13767 seg_len += rgap; 13768 if (seg_len <= 0) { 13769 /* 13770 * Resets are only valid if they lie within our offered 13771 * window. If the RST bit is set, we just ignore this 13772 * segment. 13773 */ 13774 if (flags & TH_RST) { 13775 freemsg(mp); 13776 return; 13777 } 13778 13779 /* Per RFC 793, we need to send back an ACK. */ 13780 flags |= TH_ACK_NEEDED; 13781 13782 /* 13783 * Send SIGURG as soon as possible i.e. even 13784 * if the TH_URG was delivered in a window probe 13785 * packet (which will be unacceptable). 13786 * 13787 * We generate a signal if none has been generated 13788 * for this connection or if this is a new urgent 13789 * byte. Also send a zero-length "unmarked" message 13790 * to inform SIOCATMARK that this is not the mark. 13791 * 13792 * tcp_urp_last_valid is cleared when the T_exdata_ind 13793 * is sent up. This plus the check for old data 13794 * (gap >= 0) handles the wraparound of the sequence 13795 * number space without having to always track the 13796 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13797 * this max in its rcv_up variable). 13798 * 13799 * This prevents duplicate SIGURGS due to a "late" 13800 * zero-window probe when the T_EXDATA_IND has already 13801 * been sent up. 13802 */ 13803 if ((flags & TH_URG) && 13804 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13805 tcp->tcp_urp_last))) { 13806 mp1 = allocb(0, BPRI_MED); 13807 if (mp1 == NULL) { 13808 freemsg(mp); 13809 return; 13810 } 13811 if (!TCP_IS_DETACHED(tcp) && 13812 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13813 SIGURG)) { 13814 /* Try again on the rexmit. */ 13815 freemsg(mp1); 13816 freemsg(mp); 13817 return; 13818 } 13819 /* 13820 * If the next byte would be the mark 13821 * then mark with MARKNEXT else mark 13822 * with NOTMARKNEXT. 13823 */ 13824 if (gap == 0 && urp == 0) 13825 mp1->b_flag |= MSGMARKNEXT; 13826 else 13827 mp1->b_flag |= MSGNOTMARKNEXT; 13828 freemsg(tcp->tcp_urp_mark_mp); 13829 tcp->tcp_urp_mark_mp = mp1; 13830 flags |= TH_SEND_URP_MARK; 13831 tcp->tcp_urp_last_valid = B_TRUE; 13832 tcp->tcp_urp_last = urp + seg_seq; 13833 } 13834 /* 13835 * If this is a zero window probe, continue to 13836 * process the ACK part. But we need to set seg_len 13837 * to 0 to avoid data processing. Otherwise just 13838 * drop the segment and send back an ACK. 13839 */ 13840 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13841 flags &= ~(TH_SYN | TH_URG); 13842 seg_len = 0; 13843 goto process_ack; 13844 } else { 13845 freemsg(mp); 13846 goto ack_check; 13847 } 13848 } 13849 /* Pitch out of window stuff off the end. */ 13850 rgap = seg_len; 13851 mp2 = mp; 13852 do { 13853 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13854 (uintptr_t)INT_MAX); 13855 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13856 if (rgap < 0) { 13857 mp2->b_wptr += rgap; 13858 if ((mp1 = mp2->b_cont) != NULL) { 13859 mp2->b_cont = NULL; 13860 freemsg(mp1); 13861 } 13862 break; 13863 } 13864 } while ((mp2 = mp2->b_cont) != NULL); 13865 } 13866 ok:; 13867 /* 13868 * TCP should check ECN info for segments inside the window only. 13869 * Therefore the check should be done here. 13870 */ 13871 if (tcp->tcp_ecn_ok) { 13872 if (flags & TH_CWR) { 13873 tcp->tcp_ecn_echo_on = B_FALSE; 13874 } 13875 /* 13876 * Note that both ECN_CE and CWR can be set in the 13877 * same segment. In this case, we once again turn 13878 * on ECN_ECHO. 13879 */ 13880 if (tcp->tcp_ipversion == IPV4_VERSION) { 13881 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13882 13883 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13884 tcp->tcp_ecn_echo_on = B_TRUE; 13885 } 13886 } else { 13887 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13888 13889 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13890 htonl(IPH_ECN_CE << 20)) { 13891 tcp->tcp_ecn_echo_on = B_TRUE; 13892 } 13893 } 13894 } 13895 13896 /* 13897 * Check whether we can update tcp_ts_recent. This test is 13898 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13899 * Extensions for High Performance: An Update", Internet Draft. 13900 */ 13901 if (tcp->tcp_snd_ts_ok && 13902 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13903 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13904 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13905 tcp->tcp_last_rcv_lbolt = lbolt64; 13906 } 13907 13908 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13909 /* 13910 * FIN in an out of order segment. We record this in 13911 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13912 * Clear the FIN so that any check on FIN flag will fail. 13913 * Remember that FIN also counts in the sequence number 13914 * space. So we need to ack out of order FIN only segments. 13915 */ 13916 if (flags & TH_FIN) { 13917 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13918 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13919 flags &= ~TH_FIN; 13920 flags |= TH_ACK_NEEDED; 13921 } 13922 if (seg_len > 0) { 13923 /* Fill in the SACK blk list. */ 13924 if (tcp->tcp_snd_sack_ok) { 13925 ASSERT(tcp->tcp_sack_info != NULL); 13926 tcp_sack_insert(tcp->tcp_sack_list, 13927 seg_seq, seg_seq + seg_len, 13928 &(tcp->tcp_num_sack_blk)); 13929 } 13930 13931 /* 13932 * Attempt reassembly and see if we have something 13933 * ready to go. 13934 */ 13935 mp = tcp_reass(tcp, mp, seg_seq); 13936 /* Always ack out of order packets */ 13937 flags |= TH_ACK_NEEDED | TH_PUSH; 13938 if (mp) { 13939 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13940 (uintptr_t)INT_MAX); 13941 seg_len = mp->b_cont ? msgdsize(mp) : 13942 (int)(mp->b_wptr - mp->b_rptr); 13943 seg_seq = tcp->tcp_rnxt; 13944 /* 13945 * A gap is filled and the seq num and len 13946 * of the gap match that of a previously 13947 * received FIN, put the FIN flag back in. 13948 */ 13949 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13950 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13951 flags |= TH_FIN; 13952 tcp->tcp_valid_bits &= 13953 ~TCP_OFO_FIN_VALID; 13954 } 13955 } else { 13956 /* 13957 * Keep going even with NULL mp. 13958 * There may be a useful ACK or something else 13959 * we don't want to miss. 13960 * 13961 * But TCP should not perform fast retransmit 13962 * because of the ack number. TCP uses 13963 * seg_len == 0 to determine if it is a pure 13964 * ACK. And this is not a pure ACK. 13965 */ 13966 seg_len = 0; 13967 ofo_seg = B_TRUE; 13968 } 13969 } 13970 } else if (seg_len > 0) { 13971 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13972 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13973 /* 13974 * If an out of order FIN was received before, and the seq 13975 * num and len of the new segment match that of the FIN, 13976 * put the FIN flag back in. 13977 */ 13978 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13979 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13980 flags |= TH_FIN; 13981 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13982 } 13983 } 13984 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13985 if (flags & TH_RST) { 13986 freemsg(mp); 13987 switch (tcp->tcp_state) { 13988 case TCPS_SYN_RCVD: 13989 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13990 break; 13991 case TCPS_ESTABLISHED: 13992 case TCPS_FIN_WAIT_1: 13993 case TCPS_FIN_WAIT_2: 13994 case TCPS_CLOSE_WAIT: 13995 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13996 break; 13997 case TCPS_CLOSING: 13998 case TCPS_LAST_ACK: 13999 (void) tcp_clean_death(tcp, 0, 16); 14000 break; 14001 default: 14002 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14003 (void) tcp_clean_death(tcp, ENXIO, 17); 14004 break; 14005 } 14006 return; 14007 } 14008 if (flags & TH_SYN) { 14009 /* 14010 * See RFC 793, Page 71 14011 * 14012 * The seq number must be in the window as it should 14013 * be "fixed" above. If it is outside window, it should 14014 * be already rejected. Note that we allow seg_seq to be 14015 * rnxt + rwnd because we want to accept 0 window probe. 14016 */ 14017 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 14018 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 14019 freemsg(mp); 14020 /* 14021 * If the ACK flag is not set, just use our snxt as the 14022 * seq number of the RST segment. 14023 */ 14024 if (!(flags & TH_ACK)) { 14025 seg_ack = tcp->tcp_snxt; 14026 } 14027 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 14028 TH_RST|TH_ACK); 14029 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14030 (void) tcp_clean_death(tcp, ECONNRESET, 18); 14031 return; 14032 } 14033 /* 14034 * urp could be -1 when the urp field in the packet is 0 14035 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 14036 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 14037 */ 14038 if (flags & TH_URG && urp >= 0) { 14039 if (!tcp->tcp_urp_last_valid || 14040 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 14041 /* 14042 * If we haven't generated the signal yet for this 14043 * urgent pointer value, do it now. Also, send up a 14044 * zero-length M_DATA indicating whether or not this is 14045 * the mark. The latter is not needed when a 14046 * T_EXDATA_IND is sent up. However, if there are 14047 * allocation failures this code relies on the sender 14048 * retransmitting and the socket code for determining 14049 * the mark should not block waiting for the peer to 14050 * transmit. Thus, for simplicity we always send up the 14051 * mark indication. 14052 */ 14053 mp1 = allocb(0, BPRI_MED); 14054 if (mp1 == NULL) { 14055 freemsg(mp); 14056 return; 14057 } 14058 if (!TCP_IS_DETACHED(tcp) && 14059 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 14060 /* Try again on the rexmit. */ 14061 freemsg(mp1); 14062 freemsg(mp); 14063 return; 14064 } 14065 /* 14066 * Mark with NOTMARKNEXT for now. 14067 * The code below will change this to MARKNEXT 14068 * if we are at the mark. 14069 * 14070 * If there are allocation failures (e.g. in dupmsg 14071 * below) the next time tcp_rput_data sees the urgent 14072 * segment it will send up the MSG*MARKNEXT message. 14073 */ 14074 mp1->b_flag |= MSGNOTMARKNEXT; 14075 freemsg(tcp->tcp_urp_mark_mp); 14076 tcp->tcp_urp_mark_mp = mp1; 14077 flags |= TH_SEND_URP_MARK; 14078 #ifdef DEBUG 14079 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14080 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 14081 "last %x, %s", 14082 seg_seq, urp, tcp->tcp_urp_last, 14083 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14084 #endif /* DEBUG */ 14085 tcp->tcp_urp_last_valid = B_TRUE; 14086 tcp->tcp_urp_last = urp + seg_seq; 14087 } else if (tcp->tcp_urp_mark_mp != NULL) { 14088 /* 14089 * An allocation failure prevented the previous 14090 * tcp_rput_data from sending up the allocated 14091 * MSG*MARKNEXT message - send it up this time 14092 * around. 14093 */ 14094 flags |= TH_SEND_URP_MARK; 14095 } 14096 14097 /* 14098 * If the urgent byte is in this segment, make sure that it is 14099 * all by itself. This makes it much easier to deal with the 14100 * possibility of an allocation failure on the T_exdata_ind. 14101 * Note that seg_len is the number of bytes in the segment, and 14102 * urp is the offset into the segment of the urgent byte. 14103 * urp < seg_len means that the urgent byte is in this segment. 14104 */ 14105 if (urp < seg_len) { 14106 if (seg_len != 1) { 14107 uint32_t tmp_rnxt; 14108 /* 14109 * Break it up and feed it back in. 14110 * Re-attach the IP header. 14111 */ 14112 mp->b_rptr = iphdr; 14113 if (urp > 0) { 14114 /* 14115 * There is stuff before the urgent 14116 * byte. 14117 */ 14118 mp1 = dupmsg(mp); 14119 if (!mp1) { 14120 /* 14121 * Trim from urgent byte on. 14122 * The rest will come back. 14123 */ 14124 (void) adjmsg(mp, 14125 urp - seg_len); 14126 tcp_rput_data(connp, 14127 mp, NULL); 14128 return; 14129 } 14130 (void) adjmsg(mp1, urp - seg_len); 14131 /* Feed this piece back in. */ 14132 tmp_rnxt = tcp->tcp_rnxt; 14133 tcp_rput_data(connp, mp1, NULL); 14134 /* 14135 * If the data passed back in was not 14136 * processed (ie: bad ACK) sending 14137 * the remainder back in will cause a 14138 * loop. In this case, drop the 14139 * packet and let the sender try 14140 * sending a good packet. 14141 */ 14142 if (tmp_rnxt == tcp->tcp_rnxt) { 14143 freemsg(mp); 14144 return; 14145 } 14146 } 14147 if (urp != seg_len - 1) { 14148 uint32_t tmp_rnxt; 14149 /* 14150 * There is stuff after the urgent 14151 * byte. 14152 */ 14153 mp1 = dupmsg(mp); 14154 if (!mp1) { 14155 /* 14156 * Trim everything beyond the 14157 * urgent byte. The rest will 14158 * come back. 14159 */ 14160 (void) adjmsg(mp, 14161 urp + 1 - seg_len); 14162 tcp_rput_data(connp, 14163 mp, NULL); 14164 return; 14165 } 14166 (void) adjmsg(mp1, urp + 1 - seg_len); 14167 tmp_rnxt = tcp->tcp_rnxt; 14168 tcp_rput_data(connp, mp1, NULL); 14169 /* 14170 * If the data passed back in was not 14171 * processed (ie: bad ACK) sending 14172 * the remainder back in will cause a 14173 * loop. In this case, drop the 14174 * packet and let the sender try 14175 * sending a good packet. 14176 */ 14177 if (tmp_rnxt == tcp->tcp_rnxt) { 14178 freemsg(mp); 14179 return; 14180 } 14181 } 14182 tcp_rput_data(connp, mp, NULL); 14183 return; 14184 } 14185 /* 14186 * This segment contains only the urgent byte. We 14187 * have to allocate the T_exdata_ind, if we can. 14188 */ 14189 if (!tcp->tcp_urp_mp) { 14190 struct T_exdata_ind *tei; 14191 mp1 = allocb(sizeof (struct T_exdata_ind), 14192 BPRI_MED); 14193 if (!mp1) { 14194 /* 14195 * Sigh... It'll be back. 14196 * Generate any MSG*MARK message now. 14197 */ 14198 freemsg(mp); 14199 seg_len = 0; 14200 if (flags & TH_SEND_URP_MARK) { 14201 14202 14203 ASSERT(tcp->tcp_urp_mark_mp); 14204 tcp->tcp_urp_mark_mp->b_flag &= 14205 ~MSGNOTMARKNEXT; 14206 tcp->tcp_urp_mark_mp->b_flag |= 14207 MSGMARKNEXT; 14208 } 14209 goto ack_check; 14210 } 14211 mp1->b_datap->db_type = M_PROTO; 14212 tei = (struct T_exdata_ind *)mp1->b_rptr; 14213 tei->PRIM_type = T_EXDATA_IND; 14214 tei->MORE_flag = 0; 14215 mp1->b_wptr = (uchar_t *)&tei[1]; 14216 tcp->tcp_urp_mp = mp1; 14217 #ifdef DEBUG 14218 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14219 "tcp_rput: allocated exdata_ind %s", 14220 tcp_display(tcp, NULL, 14221 DISP_PORT_ONLY)); 14222 #endif /* DEBUG */ 14223 /* 14224 * There is no need to send a separate MSG*MARK 14225 * message since the T_EXDATA_IND will be sent 14226 * now. 14227 */ 14228 flags &= ~TH_SEND_URP_MARK; 14229 freemsg(tcp->tcp_urp_mark_mp); 14230 tcp->tcp_urp_mark_mp = NULL; 14231 } 14232 /* 14233 * Now we are all set. On the next putnext upstream, 14234 * tcp_urp_mp will be non-NULL and will get prepended 14235 * to what has to be this piece containing the urgent 14236 * byte. If for any reason we abort this segment below, 14237 * if it comes back, we will have this ready, or it 14238 * will get blown off in close. 14239 */ 14240 } else if (urp == seg_len) { 14241 /* 14242 * The urgent byte is the next byte after this sequence 14243 * number. If there is data it is marked with 14244 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 14245 * since it is not needed. Otherwise, if the code 14246 * above just allocated a zero-length tcp_urp_mark_mp 14247 * message, that message is tagged with MSGMARKNEXT. 14248 * Sending up these MSGMARKNEXT messages makes 14249 * SIOCATMARK work correctly even though 14250 * the T_EXDATA_IND will not be sent up until the 14251 * urgent byte arrives. 14252 */ 14253 if (seg_len != 0) { 14254 flags |= TH_MARKNEXT_NEEDED; 14255 freemsg(tcp->tcp_urp_mark_mp); 14256 tcp->tcp_urp_mark_mp = NULL; 14257 flags &= ~TH_SEND_URP_MARK; 14258 } else if (tcp->tcp_urp_mark_mp != NULL) { 14259 flags |= TH_SEND_URP_MARK; 14260 tcp->tcp_urp_mark_mp->b_flag &= 14261 ~MSGNOTMARKNEXT; 14262 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 14263 } 14264 #ifdef DEBUG 14265 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14266 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 14267 seg_len, flags, 14268 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14269 #endif /* DEBUG */ 14270 } else { 14271 /* Data left until we hit mark */ 14272 #ifdef DEBUG 14273 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14274 "tcp_rput: URP %d bytes left, %s", 14275 urp - seg_len, tcp_display(tcp, NULL, 14276 DISP_PORT_ONLY)); 14277 #endif /* DEBUG */ 14278 } 14279 } 14280 14281 process_ack: 14282 if (!(flags & TH_ACK)) { 14283 freemsg(mp); 14284 goto xmit_check; 14285 } 14286 } 14287 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 14288 14289 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 14290 tcp->tcp_ip_forward_progress = B_TRUE; 14291 if (tcp->tcp_state == TCPS_SYN_RCVD) { 14292 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 14293 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 14294 /* 3-way handshake complete - pass up the T_CONN_IND */ 14295 tcp_t *listener = tcp->tcp_listener; 14296 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 14297 14298 tcp->tcp_tconnind_started = B_TRUE; 14299 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 14300 /* 14301 * We are here means eager is fine but it can 14302 * get a TH_RST at any point between now and till 14303 * accept completes and disappear. We need to 14304 * ensure that reference to eager is valid after 14305 * we get out of eager's perimeter. So we do 14306 * an extra refhold. 14307 */ 14308 CONN_INC_REF(connp); 14309 14310 /* 14311 * The listener also exists because of the refhold 14312 * done in tcp_conn_request. Its possible that it 14313 * might have closed. We will check that once we 14314 * get inside listeners context. 14315 */ 14316 CONN_INC_REF(listener->tcp_connp); 14317 if (listener->tcp_connp->conn_sqp == 14318 connp->conn_sqp) { 14319 tcp_send_conn_ind(listener->tcp_connp, mp, 14320 listener->tcp_connp->conn_sqp); 14321 CONN_DEC_REF(listener->tcp_connp); 14322 } else if (!tcp->tcp_loopback) { 14323 squeue_fill(listener->tcp_connp->conn_sqp, mp, 14324 tcp_send_conn_ind, 14325 listener->tcp_connp, SQTAG_TCP_CONN_IND); 14326 } else { 14327 squeue_enter(listener->tcp_connp->conn_sqp, mp, 14328 tcp_send_conn_ind, listener->tcp_connp, 14329 SQTAG_TCP_CONN_IND); 14330 } 14331 } 14332 14333 if (tcp->tcp_active_open) { 14334 /* 14335 * We are seeing the final ack in the three way 14336 * hand shake of a active open'ed connection 14337 * so we must send up a T_CONN_CON 14338 */ 14339 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14340 freemsg(mp); 14341 return; 14342 } 14343 /* 14344 * Don't fuse the loopback endpoints for 14345 * simultaneous active opens. 14346 */ 14347 if (tcp->tcp_loopback) { 14348 TCP_STAT(tcps, tcp_fusion_unfusable); 14349 tcp->tcp_unfusable = B_TRUE; 14350 } 14351 } 14352 14353 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14354 bytes_acked--; 14355 /* SYN was acked - making progress */ 14356 if (tcp->tcp_ipversion == IPV6_VERSION) 14357 tcp->tcp_ip_forward_progress = B_TRUE; 14358 14359 /* 14360 * If SYN was retransmitted, need to reset all 14361 * retransmission info as this segment will be 14362 * treated as a dup ACK. 14363 */ 14364 if (tcp->tcp_rexmit) { 14365 tcp->tcp_rexmit = B_FALSE; 14366 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14367 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14368 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14369 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14370 tcp->tcp_ms_we_have_waited = 0; 14371 tcp->tcp_cwnd = mss; 14372 } 14373 14374 /* 14375 * We set the send window to zero here. 14376 * This is needed if there is data to be 14377 * processed already on the queue. 14378 * Later (at swnd_update label), the 14379 * "new_swnd > tcp_swnd" condition is satisfied 14380 * the XMIT_NEEDED flag is set in the current 14381 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14382 * called if there is already data on queue in 14383 * this state. 14384 */ 14385 tcp->tcp_swnd = 0; 14386 14387 if (new_swnd > tcp->tcp_max_swnd) 14388 tcp->tcp_max_swnd = new_swnd; 14389 tcp->tcp_swl1 = seg_seq; 14390 tcp->tcp_swl2 = seg_ack; 14391 tcp->tcp_state = TCPS_ESTABLISHED; 14392 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14393 14394 /* Fuse when both sides are in ESTABLISHED state */ 14395 if (tcp->tcp_loopback && do_tcp_fusion) 14396 tcp_fuse(tcp, iphdr, tcph); 14397 14398 } 14399 /* This code follows 4.4BSD-Lite2 mostly. */ 14400 if (bytes_acked < 0) 14401 goto est; 14402 14403 /* 14404 * If TCP is ECN capable and the congestion experience bit is 14405 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14406 * done once per window (or more loosely, per RTT). 14407 */ 14408 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14409 tcp->tcp_cwr = B_FALSE; 14410 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14411 if (!tcp->tcp_cwr) { 14412 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14413 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14414 tcp->tcp_cwnd = npkt * mss; 14415 /* 14416 * If the cwnd is 0, use the timer to clock out 14417 * new segments. This is required by the ECN spec. 14418 */ 14419 if (npkt == 0) { 14420 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14421 /* 14422 * This makes sure that when the ACK comes 14423 * back, we will increase tcp_cwnd by 1 MSS. 14424 */ 14425 tcp->tcp_cwnd_cnt = 0; 14426 } 14427 tcp->tcp_cwr = B_TRUE; 14428 /* 14429 * This marks the end of the current window of in 14430 * flight data. That is why we don't use 14431 * tcp_suna + tcp_swnd. Only data in flight can 14432 * provide ECN info. 14433 */ 14434 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14435 tcp->tcp_ecn_cwr_sent = B_FALSE; 14436 } 14437 } 14438 14439 mp1 = tcp->tcp_xmit_head; 14440 if (bytes_acked == 0) { 14441 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14442 int dupack_cnt; 14443 14444 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14445 /* 14446 * Fast retransmit. When we have seen exactly three 14447 * identical ACKs while we have unacked data 14448 * outstanding we take it as a hint that our peer 14449 * dropped something. 14450 * 14451 * If TCP is retransmitting, don't do fast retransmit. 14452 */ 14453 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14454 ! tcp->tcp_rexmit) { 14455 /* Do Limited Transmit */ 14456 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14457 tcps->tcps_dupack_fast_retransmit) { 14458 /* 14459 * RFC 3042 14460 * 14461 * What we need to do is temporarily 14462 * increase tcp_cwnd so that new 14463 * data can be sent if it is allowed 14464 * by the receive window (tcp_rwnd). 14465 * tcp_wput_data() will take care of 14466 * the rest. 14467 * 14468 * If the connection is SACK capable, 14469 * only do limited xmit when there 14470 * is SACK info. 14471 * 14472 * Note how tcp_cwnd is incremented. 14473 * The first dup ACK will increase 14474 * it by 1 MSS. The second dup ACK 14475 * will increase it by 2 MSS. This 14476 * means that only 1 new segment will 14477 * be sent for each dup ACK. 14478 */ 14479 if (tcp->tcp_unsent > 0 && 14480 (!tcp->tcp_snd_sack_ok || 14481 (tcp->tcp_snd_sack_ok && 14482 tcp->tcp_notsack_list != NULL))) { 14483 tcp->tcp_cwnd += mss << 14484 (tcp->tcp_dupack_cnt - 1); 14485 flags |= TH_LIMIT_XMIT; 14486 } 14487 } else if (dupack_cnt == 14488 tcps->tcps_dupack_fast_retransmit) { 14489 14490 /* 14491 * If we have reduced tcp_ssthresh 14492 * because of ECN, do not reduce it again 14493 * unless it is already one window of data 14494 * away. After one window of data, tcp_cwr 14495 * should then be cleared. Note that 14496 * for non ECN capable connection, tcp_cwr 14497 * should always be false. 14498 * 14499 * Adjust cwnd since the duplicate 14500 * ack indicates that a packet was 14501 * dropped (due to congestion.) 14502 */ 14503 if (!tcp->tcp_cwr) { 14504 npkt = ((tcp->tcp_snxt - 14505 tcp->tcp_suna) >> 1) / mss; 14506 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14507 mss; 14508 tcp->tcp_cwnd = (npkt + 14509 tcp->tcp_dupack_cnt) * mss; 14510 } 14511 if (tcp->tcp_ecn_ok) { 14512 tcp->tcp_cwr = B_TRUE; 14513 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14514 tcp->tcp_ecn_cwr_sent = B_FALSE; 14515 } 14516 14517 /* 14518 * We do Hoe's algorithm. Refer to her 14519 * paper "Improving the Start-up Behavior 14520 * of a Congestion Control Scheme for TCP," 14521 * appeared in SIGCOMM'96. 14522 * 14523 * Save highest seq no we have sent so far. 14524 * Be careful about the invisible FIN byte. 14525 */ 14526 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14527 (tcp->tcp_unsent == 0)) { 14528 tcp->tcp_rexmit_max = tcp->tcp_fss; 14529 } else { 14530 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14531 } 14532 14533 /* 14534 * Do not allow bursty traffic during. 14535 * fast recovery. Refer to Fall and Floyd's 14536 * paper "Simulation-based Comparisons of 14537 * Tahoe, Reno and SACK TCP" (in CCR?) 14538 * This is a best current practise. 14539 */ 14540 tcp->tcp_snd_burst = TCP_CWND_SS; 14541 14542 /* 14543 * For SACK: 14544 * Calculate tcp_pipe, which is the 14545 * estimated number of bytes in 14546 * network. 14547 * 14548 * tcp_fack is the highest sack'ed seq num 14549 * TCP has received. 14550 * 14551 * tcp_pipe is explained in the above quoted 14552 * Fall and Floyd's paper. tcp_fack is 14553 * explained in Mathis and Mahdavi's 14554 * "Forward Acknowledgment: Refining TCP 14555 * Congestion Control" in SIGCOMM '96. 14556 */ 14557 if (tcp->tcp_snd_sack_ok) { 14558 ASSERT(tcp->tcp_sack_info != NULL); 14559 if (tcp->tcp_notsack_list != NULL) { 14560 tcp->tcp_pipe = tcp->tcp_snxt - 14561 tcp->tcp_fack; 14562 tcp->tcp_sack_snxt = seg_ack; 14563 flags |= TH_NEED_SACK_REXMIT; 14564 } else { 14565 /* 14566 * Always initialize tcp_pipe 14567 * even though we don't have 14568 * any SACK info. If later 14569 * we get SACK info and 14570 * tcp_pipe is not initialized, 14571 * funny things will happen. 14572 */ 14573 tcp->tcp_pipe = 14574 tcp->tcp_cwnd_ssthresh; 14575 } 14576 } else { 14577 flags |= TH_REXMIT_NEEDED; 14578 } /* tcp_snd_sack_ok */ 14579 14580 } else { 14581 /* 14582 * Here we perform congestion 14583 * avoidance, but NOT slow start. 14584 * This is known as the Fast 14585 * Recovery Algorithm. 14586 */ 14587 if (tcp->tcp_snd_sack_ok && 14588 tcp->tcp_notsack_list != NULL) { 14589 flags |= TH_NEED_SACK_REXMIT; 14590 tcp->tcp_pipe -= mss; 14591 if (tcp->tcp_pipe < 0) 14592 tcp->tcp_pipe = 0; 14593 } else { 14594 /* 14595 * We know that one more packet has 14596 * left the pipe thus we can update 14597 * cwnd. 14598 */ 14599 cwnd = tcp->tcp_cwnd + mss; 14600 if (cwnd > tcp->tcp_cwnd_max) 14601 cwnd = tcp->tcp_cwnd_max; 14602 tcp->tcp_cwnd = cwnd; 14603 if (tcp->tcp_unsent > 0) 14604 flags |= TH_XMIT_NEEDED; 14605 } 14606 } 14607 } 14608 } else if (tcp->tcp_zero_win_probe) { 14609 /* 14610 * If the window has opened, need to arrange 14611 * to send additional data. 14612 */ 14613 if (new_swnd != 0) { 14614 /* tcp_suna != tcp_snxt */ 14615 /* Packet contains a window update */ 14616 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14617 tcp->tcp_zero_win_probe = 0; 14618 tcp->tcp_timer_backoff = 0; 14619 tcp->tcp_ms_we_have_waited = 0; 14620 14621 /* 14622 * Transmit starting with tcp_suna since 14623 * the one byte probe is not ack'ed. 14624 * If TCP has sent more than one identical 14625 * probe, tcp_rexmit will be set. That means 14626 * tcp_ss_rexmit() will send out the one 14627 * byte along with new data. Otherwise, 14628 * fake the retransmission. 14629 */ 14630 flags |= TH_XMIT_NEEDED; 14631 if (!tcp->tcp_rexmit) { 14632 tcp->tcp_rexmit = B_TRUE; 14633 tcp->tcp_dupack_cnt = 0; 14634 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14635 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14636 } 14637 } 14638 } 14639 goto swnd_update; 14640 } 14641 14642 /* 14643 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14644 * If the ACK value acks something that we have not yet sent, it might 14645 * be an old duplicate segment. Send an ACK to re-synchronize the 14646 * other side. 14647 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14648 * state is handled above, so we can always just drop the segment and 14649 * send an ACK here. 14650 * 14651 * Should we send ACKs in response to ACK only segments? 14652 */ 14653 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14654 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14655 /* drop the received segment */ 14656 freemsg(mp); 14657 14658 /* 14659 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14660 * greater than 0, check if the number of such 14661 * bogus ACks is greater than that count. If yes, 14662 * don't send back any ACK. This prevents TCP from 14663 * getting into an ACK storm if somehow an attacker 14664 * successfully spoofs an acceptable segment to our 14665 * peer. 14666 */ 14667 if (tcp_drop_ack_unsent_cnt > 0 && 14668 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14669 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14670 return; 14671 } 14672 mp = tcp_ack_mp(tcp); 14673 if (mp != NULL) { 14674 BUMP_LOCAL(tcp->tcp_obsegs); 14675 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14676 tcp_send_data(tcp, tcp->tcp_wq, mp); 14677 } 14678 return; 14679 } 14680 14681 /* 14682 * TCP gets a new ACK, update the notsack'ed list to delete those 14683 * blocks that are covered by this ACK. 14684 */ 14685 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14686 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14687 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14688 } 14689 14690 /* 14691 * If we got an ACK after fast retransmit, check to see 14692 * if it is a partial ACK. If it is not and the congestion 14693 * window was inflated to account for the other side's 14694 * cached packets, retract it. If it is, do Hoe's algorithm. 14695 */ 14696 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14697 ASSERT(tcp->tcp_rexmit == B_FALSE); 14698 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14699 tcp->tcp_dupack_cnt = 0; 14700 /* 14701 * Restore the orig tcp_cwnd_ssthresh after 14702 * fast retransmit phase. 14703 */ 14704 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14705 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14706 } 14707 tcp->tcp_rexmit_max = seg_ack; 14708 tcp->tcp_cwnd_cnt = 0; 14709 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14710 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14711 14712 /* 14713 * Remove all notsack info to avoid confusion with 14714 * the next fast retrasnmit/recovery phase. 14715 */ 14716 if (tcp->tcp_snd_sack_ok && 14717 tcp->tcp_notsack_list != NULL) { 14718 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14719 } 14720 } else { 14721 if (tcp->tcp_snd_sack_ok && 14722 tcp->tcp_notsack_list != NULL) { 14723 flags |= TH_NEED_SACK_REXMIT; 14724 tcp->tcp_pipe -= mss; 14725 if (tcp->tcp_pipe < 0) 14726 tcp->tcp_pipe = 0; 14727 } else { 14728 /* 14729 * Hoe's algorithm: 14730 * 14731 * Retransmit the unack'ed segment and 14732 * restart fast recovery. Note that we 14733 * need to scale back tcp_cwnd to the 14734 * original value when we started fast 14735 * recovery. This is to prevent overly 14736 * aggressive behaviour in sending new 14737 * segments. 14738 */ 14739 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14740 tcps->tcps_dupack_fast_retransmit * mss; 14741 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14742 flags |= TH_REXMIT_NEEDED; 14743 } 14744 } 14745 } else { 14746 tcp->tcp_dupack_cnt = 0; 14747 if (tcp->tcp_rexmit) { 14748 /* 14749 * TCP is retranmitting. If the ACK ack's all 14750 * outstanding data, update tcp_rexmit_max and 14751 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14752 * to the correct value. 14753 * 14754 * Note that SEQ_LEQ() is used. This is to avoid 14755 * unnecessary fast retransmit caused by dup ACKs 14756 * received when TCP does slow start retransmission 14757 * after a time out. During this phase, TCP may 14758 * send out segments which are already received. 14759 * This causes dup ACKs to be sent back. 14760 */ 14761 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14762 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14763 tcp->tcp_rexmit_nxt = seg_ack; 14764 } 14765 if (seg_ack != tcp->tcp_rexmit_max) { 14766 flags |= TH_XMIT_NEEDED; 14767 } 14768 } else { 14769 tcp->tcp_rexmit = B_FALSE; 14770 tcp->tcp_xmit_zc_clean = B_FALSE; 14771 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14772 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14773 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14774 } 14775 tcp->tcp_ms_we_have_waited = 0; 14776 } 14777 } 14778 14779 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14780 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14781 tcp->tcp_suna = seg_ack; 14782 if (tcp->tcp_zero_win_probe != 0) { 14783 tcp->tcp_zero_win_probe = 0; 14784 tcp->tcp_timer_backoff = 0; 14785 } 14786 14787 /* 14788 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14789 * Note that it cannot be the SYN being ack'ed. The code flow 14790 * will not reach here. 14791 */ 14792 if (mp1 == NULL) { 14793 goto fin_acked; 14794 } 14795 14796 /* 14797 * Update the congestion window. 14798 * 14799 * If TCP is not ECN capable or TCP is ECN capable but the 14800 * congestion experience bit is not set, increase the tcp_cwnd as 14801 * usual. 14802 */ 14803 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14804 cwnd = tcp->tcp_cwnd; 14805 add = mss; 14806 14807 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14808 /* 14809 * This is to prevent an increase of less than 1 MSS of 14810 * tcp_cwnd. With partial increase, tcp_wput_data() 14811 * may send out tinygrams in order to preserve mblk 14812 * boundaries. 14813 * 14814 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14815 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14816 * increased by 1 MSS for every RTTs. 14817 */ 14818 if (tcp->tcp_cwnd_cnt <= 0) { 14819 tcp->tcp_cwnd_cnt = cwnd + add; 14820 } else { 14821 tcp->tcp_cwnd_cnt -= add; 14822 add = 0; 14823 } 14824 } 14825 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14826 } 14827 14828 /* See if the latest urgent data has been acknowledged */ 14829 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14830 SEQ_GT(seg_ack, tcp->tcp_urg)) 14831 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14832 14833 /* Can we update the RTT estimates? */ 14834 if (tcp->tcp_snd_ts_ok) { 14835 /* Ignore zero timestamp echo-reply. */ 14836 if (tcpopt.tcp_opt_ts_ecr != 0) { 14837 tcp_set_rto(tcp, (int32_t)lbolt - 14838 (int32_t)tcpopt.tcp_opt_ts_ecr); 14839 } 14840 14841 /* If needed, restart the timer. */ 14842 if (tcp->tcp_set_timer == 1) { 14843 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14844 tcp->tcp_set_timer = 0; 14845 } 14846 /* 14847 * Update tcp_csuna in case the other side stops sending 14848 * us timestamps. 14849 */ 14850 tcp->tcp_csuna = tcp->tcp_snxt; 14851 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14852 /* 14853 * An ACK sequence we haven't seen before, so get the RTT 14854 * and update the RTO. But first check if the timestamp is 14855 * valid to use. 14856 */ 14857 if ((mp1->b_next != NULL) && 14858 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14859 tcp_set_rto(tcp, (int32_t)lbolt - 14860 (int32_t)(intptr_t)mp1->b_prev); 14861 else 14862 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14863 14864 /* Remeber the last sequence to be ACKed */ 14865 tcp->tcp_csuna = seg_ack; 14866 if (tcp->tcp_set_timer == 1) { 14867 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14868 tcp->tcp_set_timer = 0; 14869 } 14870 } else { 14871 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14872 } 14873 14874 /* Eat acknowledged bytes off the xmit queue. */ 14875 for (;;) { 14876 mblk_t *mp2; 14877 uchar_t *wptr; 14878 14879 wptr = mp1->b_wptr; 14880 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14881 bytes_acked -= (int)(wptr - mp1->b_rptr); 14882 if (bytes_acked < 0) { 14883 mp1->b_rptr = wptr + bytes_acked; 14884 /* 14885 * Set a new timestamp if all the bytes timed by the 14886 * old timestamp have been ack'ed. 14887 */ 14888 if (SEQ_GT(seg_ack, 14889 (uint32_t)(uintptr_t)(mp1->b_next))) { 14890 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14891 mp1->b_next = NULL; 14892 } 14893 break; 14894 } 14895 mp1->b_next = NULL; 14896 mp1->b_prev = NULL; 14897 mp2 = mp1; 14898 mp1 = mp1->b_cont; 14899 14900 /* 14901 * This notification is required for some zero-copy 14902 * clients to maintain a copy semantic. After the data 14903 * is ack'ed, client is safe to modify or reuse the buffer. 14904 */ 14905 if (tcp->tcp_snd_zcopy_aware && 14906 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14907 tcp_zcopy_notify(tcp); 14908 freeb(mp2); 14909 if (bytes_acked == 0) { 14910 if (mp1 == NULL) { 14911 /* Everything is ack'ed, clear the tail. */ 14912 tcp->tcp_xmit_tail = NULL; 14913 /* 14914 * Cancel the timer unless we are still 14915 * waiting for an ACK for the FIN packet. 14916 */ 14917 if (tcp->tcp_timer_tid != 0 && 14918 tcp->tcp_snxt == tcp->tcp_suna) { 14919 (void) TCP_TIMER_CANCEL(tcp, 14920 tcp->tcp_timer_tid); 14921 tcp->tcp_timer_tid = 0; 14922 } 14923 goto pre_swnd_update; 14924 } 14925 if (mp2 != tcp->tcp_xmit_tail) 14926 break; 14927 tcp->tcp_xmit_tail = mp1; 14928 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14929 (uintptr_t)INT_MAX); 14930 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14931 mp1->b_rptr); 14932 break; 14933 } 14934 if (mp1 == NULL) { 14935 /* 14936 * More was acked but there is nothing more 14937 * outstanding. This means that the FIN was 14938 * just acked or that we're talking to a clown. 14939 */ 14940 fin_acked: 14941 ASSERT(tcp->tcp_fin_sent); 14942 tcp->tcp_xmit_tail = NULL; 14943 if (tcp->tcp_fin_sent) { 14944 /* FIN was acked - making progress */ 14945 if (tcp->tcp_ipversion == IPV6_VERSION && 14946 !tcp->tcp_fin_acked) 14947 tcp->tcp_ip_forward_progress = B_TRUE; 14948 tcp->tcp_fin_acked = B_TRUE; 14949 if (tcp->tcp_linger_tid != 0 && 14950 TCP_TIMER_CANCEL(tcp, 14951 tcp->tcp_linger_tid) >= 0) { 14952 tcp_stop_lingering(tcp); 14953 freemsg(mp); 14954 mp = NULL; 14955 } 14956 } else { 14957 /* 14958 * We should never get here because 14959 * we have already checked that the 14960 * number of bytes ack'ed should be 14961 * smaller than or equal to what we 14962 * have sent so far (it is the 14963 * acceptability check of the ACK). 14964 * We can only get here if the send 14965 * queue is corrupted. 14966 * 14967 * Terminate the connection and 14968 * panic the system. It is better 14969 * for us to panic instead of 14970 * continuing to avoid other disaster. 14971 */ 14972 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14973 tcp->tcp_rnxt, TH_RST|TH_ACK); 14974 panic("Memory corruption " 14975 "detected for connection %s.", 14976 tcp_display(tcp, NULL, 14977 DISP_ADDR_AND_PORT)); 14978 /*NOTREACHED*/ 14979 } 14980 goto pre_swnd_update; 14981 } 14982 ASSERT(mp2 != tcp->tcp_xmit_tail); 14983 } 14984 if (tcp->tcp_unsent) { 14985 flags |= TH_XMIT_NEEDED; 14986 } 14987 pre_swnd_update: 14988 tcp->tcp_xmit_head = mp1; 14989 swnd_update: 14990 /* 14991 * The following check is different from most other implementations. 14992 * For bi-directional transfer, when segments are dropped, the 14993 * "normal" check will not accept a window update in those 14994 * retransmitted segemnts. Failing to do that, TCP may send out 14995 * segments which are outside receiver's window. As TCP accepts 14996 * the ack in those retransmitted segments, if the window update in 14997 * the same segment is not accepted, TCP will incorrectly calculates 14998 * that it can send more segments. This can create a deadlock 14999 * with the receiver if its window becomes zero. 15000 */ 15001 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 15002 SEQ_LT(tcp->tcp_swl1, seg_seq) || 15003 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 15004 /* 15005 * The criteria for update is: 15006 * 15007 * 1. the segment acknowledges some data. Or 15008 * 2. the segment is new, i.e. it has a higher seq num. Or 15009 * 3. the segment is not old and the advertised window is 15010 * larger than the previous advertised window. 15011 */ 15012 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 15013 flags |= TH_XMIT_NEEDED; 15014 tcp->tcp_swnd = new_swnd; 15015 if (new_swnd > tcp->tcp_max_swnd) 15016 tcp->tcp_max_swnd = new_swnd; 15017 tcp->tcp_swl1 = seg_seq; 15018 tcp->tcp_swl2 = seg_ack; 15019 } 15020 est: 15021 if (tcp->tcp_state > TCPS_ESTABLISHED) { 15022 15023 switch (tcp->tcp_state) { 15024 case TCPS_FIN_WAIT_1: 15025 if (tcp->tcp_fin_acked) { 15026 tcp->tcp_state = TCPS_FIN_WAIT_2; 15027 /* 15028 * We implement the non-standard BSD/SunOS 15029 * FIN_WAIT_2 flushing algorithm. 15030 * If there is no user attached to this 15031 * TCP endpoint, then this TCP struct 15032 * could hang around forever in FIN_WAIT_2 15033 * state if the peer forgets to send us 15034 * a FIN. To prevent this, we wait only 15035 * 2*MSL (a convenient time value) for 15036 * the FIN to arrive. If it doesn't show up, 15037 * we flush the TCP endpoint. This algorithm, 15038 * though a violation of RFC-793, has worked 15039 * for over 10 years in BSD systems. 15040 * Note: SunOS 4.x waits 675 seconds before 15041 * flushing the FIN_WAIT_2 connection. 15042 */ 15043 TCP_TIMER_RESTART(tcp, 15044 tcps->tcps_fin_wait_2_flush_interval); 15045 } 15046 break; 15047 case TCPS_FIN_WAIT_2: 15048 break; /* Shutdown hook? */ 15049 case TCPS_LAST_ACK: 15050 freemsg(mp); 15051 if (tcp->tcp_fin_acked) { 15052 (void) tcp_clean_death(tcp, 0, 19); 15053 return; 15054 } 15055 goto xmit_check; 15056 case TCPS_CLOSING: 15057 if (tcp->tcp_fin_acked) { 15058 tcp->tcp_state = TCPS_TIME_WAIT; 15059 /* 15060 * Unconditionally clear the exclusive binding 15061 * bit so this TIME-WAIT connection won't 15062 * interfere with new ones. 15063 */ 15064 tcp->tcp_exclbind = 0; 15065 if (!TCP_IS_DETACHED(tcp)) { 15066 TCP_TIMER_RESTART(tcp, 15067 tcps->tcps_time_wait_interval); 15068 } else { 15069 tcp_time_wait_append(tcp); 15070 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 15071 } 15072 } 15073 /*FALLTHRU*/ 15074 case TCPS_CLOSE_WAIT: 15075 freemsg(mp); 15076 goto xmit_check; 15077 default: 15078 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 15079 break; 15080 } 15081 } 15082 if (flags & TH_FIN) { 15083 /* Make sure we ack the fin */ 15084 flags |= TH_ACK_NEEDED; 15085 if (!tcp->tcp_fin_rcvd) { 15086 tcp->tcp_fin_rcvd = B_TRUE; 15087 tcp->tcp_rnxt++; 15088 tcph = tcp->tcp_tcph; 15089 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15090 15091 /* 15092 * Generate the ordrel_ind at the end unless we 15093 * are an eager guy. 15094 * In the eager case tcp_rsrv will do this when run 15095 * after tcp_accept is done. 15096 */ 15097 if (tcp->tcp_listener == NULL && 15098 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 15099 flags |= TH_ORDREL_NEEDED; 15100 switch (tcp->tcp_state) { 15101 case TCPS_SYN_RCVD: 15102 case TCPS_ESTABLISHED: 15103 tcp->tcp_state = TCPS_CLOSE_WAIT; 15104 /* Keepalive? */ 15105 break; 15106 case TCPS_FIN_WAIT_1: 15107 if (!tcp->tcp_fin_acked) { 15108 tcp->tcp_state = TCPS_CLOSING; 15109 break; 15110 } 15111 /* FALLTHRU */ 15112 case TCPS_FIN_WAIT_2: 15113 tcp->tcp_state = TCPS_TIME_WAIT; 15114 /* 15115 * Unconditionally clear the exclusive binding 15116 * bit so this TIME-WAIT connection won't 15117 * interfere with new ones. 15118 */ 15119 tcp->tcp_exclbind = 0; 15120 if (!TCP_IS_DETACHED(tcp)) { 15121 TCP_TIMER_RESTART(tcp, 15122 tcps->tcps_time_wait_interval); 15123 } else { 15124 tcp_time_wait_append(tcp); 15125 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 15126 } 15127 if (seg_len) { 15128 /* 15129 * implies data piggybacked on FIN. 15130 * break to handle data. 15131 */ 15132 break; 15133 } 15134 freemsg(mp); 15135 goto ack_check; 15136 } 15137 } 15138 } 15139 if (mp == NULL) 15140 goto xmit_check; 15141 if (seg_len == 0) { 15142 freemsg(mp); 15143 goto xmit_check; 15144 } 15145 if (mp->b_rptr == mp->b_wptr) { 15146 /* 15147 * The header has been consumed, so we remove the 15148 * zero-length mblk here. 15149 */ 15150 mp1 = mp; 15151 mp = mp->b_cont; 15152 freeb(mp1); 15153 } 15154 tcph = tcp->tcp_tcph; 15155 tcp->tcp_rack_cnt++; 15156 { 15157 uint32_t cur_max; 15158 15159 cur_max = tcp->tcp_rack_cur_max; 15160 if (tcp->tcp_rack_cnt >= cur_max) { 15161 /* 15162 * We have more unacked data than we should - send 15163 * an ACK now. 15164 */ 15165 flags |= TH_ACK_NEEDED; 15166 cur_max++; 15167 if (cur_max > tcp->tcp_rack_abs_max) 15168 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15169 else 15170 tcp->tcp_rack_cur_max = cur_max; 15171 } else if (TCP_IS_DETACHED(tcp)) { 15172 /* We don't have an ACK timer for detached TCP. */ 15173 flags |= TH_ACK_NEEDED; 15174 } else if (seg_len < mss) { 15175 /* 15176 * If we get a segment that is less than an mss, and we 15177 * already have unacknowledged data, and the amount 15178 * unacknowledged is not a multiple of mss, then we 15179 * better generate an ACK now. Otherwise, this may be 15180 * the tail piece of a transaction, and we would rather 15181 * wait for the response. 15182 */ 15183 uint32_t udif; 15184 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 15185 (uintptr_t)INT_MAX); 15186 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 15187 if (udif && (udif % mss)) 15188 flags |= TH_ACK_NEEDED; 15189 else 15190 flags |= TH_ACK_TIMER_NEEDED; 15191 } else { 15192 /* Start delayed ack timer */ 15193 flags |= TH_ACK_TIMER_NEEDED; 15194 } 15195 } 15196 tcp->tcp_rnxt += seg_len; 15197 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15198 15199 /* Update SACK list */ 15200 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 15201 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 15202 &(tcp->tcp_num_sack_blk)); 15203 } 15204 15205 if (tcp->tcp_urp_mp) { 15206 tcp->tcp_urp_mp->b_cont = mp; 15207 mp = tcp->tcp_urp_mp; 15208 tcp->tcp_urp_mp = NULL; 15209 /* Ready for a new signal. */ 15210 tcp->tcp_urp_last_valid = B_FALSE; 15211 #ifdef DEBUG 15212 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15213 "tcp_rput: sending exdata_ind %s", 15214 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15215 #endif /* DEBUG */ 15216 } 15217 15218 /* 15219 * Check for ancillary data changes compared to last segment. 15220 */ 15221 if (tcp->tcp_ipv6_recvancillary != 0) { 15222 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 15223 ASSERT(mp != NULL); 15224 } 15225 15226 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 15227 /* 15228 * Side queue inbound data until the accept happens. 15229 * tcp_accept/tcp_rput drains this when the accept happens. 15230 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 15231 * T_EXDATA_IND) it is queued on b_next. 15232 * XXX Make urgent data use this. Requires: 15233 * Removing tcp_listener check for TH_URG 15234 * Making M_PCPROTO and MARK messages skip the eager case 15235 */ 15236 15237 if (tcp->tcp_kssl_pending) { 15238 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 15239 mblk_t *, mp); 15240 tcp_kssl_input(tcp, mp); 15241 } else { 15242 tcp_rcv_enqueue(tcp, mp, seg_len); 15243 } 15244 } else { 15245 sodirect_t *sodp = tcp->tcp_sodirect; 15246 15247 /* 15248 * If an sodirect connection and an enabled sodirect_t then 15249 * sodp will be set to point to the tcp_t/sonode_t shared 15250 * sodirect_t and the sodirect_t's lock will be held. 15251 */ 15252 if (sodp != NULL) { 15253 mutex_enter(sodp->sod_lockp); 15254 if (!(sodp->sod_state & SOD_ENABLED) || 15255 (tcp->tcp_kssl_ctx != NULL && 15256 DB_TYPE(mp) == M_DATA)) { 15257 mutex_exit(sodp->sod_lockp); 15258 sodp = NULL; 15259 } 15260 } 15261 if (mp->b_datap->db_type != M_DATA || 15262 (flags & TH_MARKNEXT_NEEDED)) { 15263 if (sodp != NULL) { 15264 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15265 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15266 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15267 } 15268 if (!SOD_QEMPTY(sodp) && 15269 (sodp->sod_state & SOD_WAKE_NOT)) { 15270 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15271 /* sod_wakeup() did the mutex_exit() */ 15272 } else { 15273 mutex_exit(sodp->sod_lockp); 15274 } 15275 } else if (tcp->tcp_rcv_list != NULL) { 15276 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15277 } 15278 ASSERT(tcp->tcp_rcv_list == NULL || 15279 tcp->tcp_fused_sigurg); 15280 15281 if (flags & TH_MARKNEXT_NEEDED) { 15282 #ifdef DEBUG 15283 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15284 "tcp_rput: sending MSGMARKNEXT %s", 15285 tcp_display(tcp, NULL, 15286 DISP_PORT_ONLY)); 15287 #endif /* DEBUG */ 15288 mp->b_flag |= MSGMARKNEXT; 15289 flags &= ~TH_MARKNEXT_NEEDED; 15290 } 15291 15292 /* Does this need SSL processing first? */ 15293 if ((tcp->tcp_kssl_ctx != NULL) && 15294 (DB_TYPE(mp) == M_DATA)) { 15295 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 15296 mblk_t *, mp); 15297 tcp_kssl_input(tcp, mp); 15298 } else { 15299 putnext(tcp->tcp_rq, mp); 15300 if (!canputnext(tcp->tcp_rq)) 15301 tcp->tcp_rwnd -= seg_len; 15302 } 15303 } else if ((tcp->tcp_kssl_ctx != NULL) && 15304 (DB_TYPE(mp) == M_DATA)) { 15305 /* Do SSL processing first */ 15306 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, 15307 mblk_t *, mp); 15308 tcp_kssl_input(tcp, mp); 15309 } else if (sodp != NULL) { 15310 /* 15311 * Sodirect so all mblk_t's are queued on the 15312 * socket directly, check for wakeup of blocked 15313 * reader (if any), and last if flow-controled. 15314 */ 15315 flags |= tcp_rcv_sod_enqueue(tcp, sodp, mp, seg_len); 15316 if ((sodp->sod_state & SOD_WAKE_NEED) || 15317 (flags & (TH_PUSH|TH_FIN))) { 15318 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15319 /* sod_wakeup() did the mutex_exit() */ 15320 } else { 15321 if (SOD_QFULL(sodp)) { 15322 /* Q is full, need backenable */ 15323 SOD_QSETBE(sodp); 15324 } 15325 mutex_exit(sodp->sod_lockp); 15326 } 15327 } else if ((flags & (TH_PUSH|TH_FIN)) || 15328 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 15329 if (tcp->tcp_rcv_list != NULL) { 15330 /* 15331 * Enqueue the new segment first and then 15332 * call tcp_rcv_drain() to send all data 15333 * up. The other way to do this is to 15334 * send all queued data up and then call 15335 * putnext() to send the new segment up. 15336 * This way can remove the else part later 15337 * on. 15338 * 15339 * We don't this to avoid one more call to 15340 * canputnext() as tcp_rcv_drain() needs to 15341 * call canputnext(). 15342 */ 15343 tcp_rcv_enqueue(tcp, mp, seg_len); 15344 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15345 } else { 15346 putnext(tcp->tcp_rq, mp); 15347 if (!canputnext(tcp->tcp_rq)) 15348 tcp->tcp_rwnd -= seg_len; 15349 } 15350 } else { 15351 /* 15352 * Enqueue all packets when processing an mblk 15353 * from the co queue and also enqueue normal packets. 15354 */ 15355 tcp_rcv_enqueue(tcp, mp, seg_len); 15356 } 15357 /* 15358 * Make sure the timer is running if we have data waiting 15359 * for a push bit. This provides resiliency against 15360 * implementations that do not correctly generate push bits. 15361 * 15362 * Note, for sodirect if Q isn't empty and there's not a 15363 * pending wakeup then we need a timer. Also note that sodp 15364 * is assumed to be still valid after exit()ing the sod_lockp 15365 * above and while the SOD state can change it can only change 15366 * such that the Q is empty now even though data was added 15367 * above. 15368 */ 15369 if (((sodp != NULL && !SOD_QEMPTY(sodp) && 15370 (sodp->sod_state & SOD_WAKE_NOT)) || 15371 (sodp == NULL && tcp->tcp_rcv_list != NULL)) && 15372 tcp->tcp_push_tid == 0) { 15373 /* 15374 * The connection may be closed at this point, so don't 15375 * do anything for a detached tcp. 15376 */ 15377 if (!TCP_IS_DETACHED(tcp)) 15378 tcp->tcp_push_tid = TCP_TIMER(tcp, 15379 tcp_push_timer, 15380 MSEC_TO_TICK( 15381 tcps->tcps_push_timer_interval)); 15382 } 15383 } 15384 15385 xmit_check: 15386 /* Is there anything left to do? */ 15387 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15388 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15389 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15390 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15391 goto done; 15392 15393 /* Any transmit work to do and a non-zero window? */ 15394 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15395 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15396 if (flags & TH_REXMIT_NEEDED) { 15397 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15398 15399 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15400 if (snd_size > mss) 15401 snd_size = mss; 15402 if (snd_size > tcp->tcp_swnd) 15403 snd_size = tcp->tcp_swnd; 15404 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15405 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15406 B_TRUE); 15407 15408 if (mp1 != NULL) { 15409 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15410 tcp->tcp_csuna = tcp->tcp_snxt; 15411 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15412 UPDATE_MIB(&tcps->tcps_mib, 15413 tcpRetransBytes, snd_size); 15414 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15415 } 15416 } 15417 if (flags & TH_NEED_SACK_REXMIT) { 15418 tcp_sack_rxmit(tcp, &flags); 15419 } 15420 /* 15421 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15422 * out new segment. Note that tcp_rexmit should not be 15423 * set, otherwise TH_LIMIT_XMIT should not be set. 15424 */ 15425 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15426 if (!tcp->tcp_rexmit) { 15427 tcp_wput_data(tcp, NULL, B_FALSE); 15428 } else { 15429 tcp_ss_rexmit(tcp); 15430 } 15431 } 15432 /* 15433 * Adjust tcp_cwnd back to normal value after sending 15434 * new data segments. 15435 */ 15436 if (flags & TH_LIMIT_XMIT) { 15437 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15438 /* 15439 * This will restart the timer. Restarting the 15440 * timer is used to avoid a timeout before the 15441 * limited transmitted segment's ACK gets back. 15442 */ 15443 if (tcp->tcp_xmit_head != NULL) 15444 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15445 } 15446 15447 /* Anything more to do? */ 15448 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15449 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15450 goto done; 15451 } 15452 ack_check: 15453 if (flags & TH_SEND_URP_MARK) { 15454 ASSERT(tcp->tcp_urp_mark_mp); 15455 /* 15456 * Send up any queued data and then send the mark message 15457 */ 15458 sodirect_t *sodp; 15459 15460 SOD_PTR_ENTER(tcp, sodp); 15461 15462 mp1 = tcp->tcp_urp_mark_mp; 15463 tcp->tcp_urp_mark_mp = NULL; 15464 if (sodp != NULL) { 15465 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15466 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15467 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15468 } 15469 ASSERT(tcp->tcp_rcv_list == NULL); 15470 15471 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15472 /* sod_wakeup() does the mutex_exit() */ 15473 } else if (tcp->tcp_rcv_list != NULL) { 15474 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15475 15476 ASSERT(tcp->tcp_rcv_list == NULL || 15477 tcp->tcp_fused_sigurg); 15478 15479 } 15480 putnext(tcp->tcp_rq, mp1); 15481 #ifdef DEBUG 15482 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15483 "tcp_rput: sending zero-length %s %s", 15484 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15485 "MSGNOTMARKNEXT"), 15486 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15487 #endif /* DEBUG */ 15488 flags &= ~TH_SEND_URP_MARK; 15489 } 15490 if (flags & TH_ACK_NEEDED) { 15491 /* 15492 * Time to send an ack for some reason. 15493 */ 15494 mp1 = tcp_ack_mp(tcp); 15495 15496 if (mp1 != NULL) { 15497 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15498 BUMP_LOCAL(tcp->tcp_obsegs); 15499 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15500 } 15501 if (tcp->tcp_ack_tid != 0) { 15502 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15503 tcp->tcp_ack_tid = 0; 15504 } 15505 } 15506 if (flags & TH_ACK_TIMER_NEEDED) { 15507 /* 15508 * Arrange for deferred ACK or push wait timeout. 15509 * Start timer if it is not already running. 15510 */ 15511 if (tcp->tcp_ack_tid == 0) { 15512 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15513 MSEC_TO_TICK(tcp->tcp_localnet ? 15514 (clock_t)tcps->tcps_local_dack_interval : 15515 (clock_t)tcps->tcps_deferred_ack_interval)); 15516 } 15517 } 15518 if (flags & TH_ORDREL_NEEDED) { 15519 /* 15520 * Send up the ordrel_ind unless we are an eager guy. 15521 * In the eager case tcp_rsrv will do this when run 15522 * after tcp_accept is done. 15523 */ 15524 sodirect_t *sodp; 15525 15526 ASSERT(tcp->tcp_listener == NULL); 15527 15528 SOD_PTR_ENTER(tcp, sodp); 15529 if (sodp != NULL) { 15530 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15531 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15532 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15533 } 15534 /* No more sodirect */ 15535 tcp->tcp_sodirect = NULL; 15536 if (!SOD_QEMPTY(sodp)) { 15537 /* Mblk(s) to process, notify */ 15538 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15539 /* sod_wakeup() does the mutex_exit() */ 15540 } else { 15541 /* Nothing to process */ 15542 mutex_exit(sodp->sod_lockp); 15543 } 15544 } else if (tcp->tcp_rcv_list != NULL) { 15545 /* 15546 * Push any mblk(s) enqueued from co processing. 15547 */ 15548 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15549 15550 ASSERT(tcp->tcp_rcv_list == NULL || 15551 tcp->tcp_fused_sigurg); 15552 } 15553 15554 mp1 = tcp->tcp_ordrel_mp; 15555 tcp->tcp_ordrel_mp = NULL; 15556 tcp->tcp_ordrel_done = B_TRUE; 15557 putnext(tcp->tcp_rq, mp1); 15558 } 15559 done: 15560 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15561 } 15562 15563 /* 15564 * This function does PAWS protection check. Returns B_TRUE if the 15565 * segment passes the PAWS test, else returns B_FALSE. 15566 */ 15567 boolean_t 15568 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15569 { 15570 uint8_t flags; 15571 int options; 15572 uint8_t *up; 15573 15574 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15575 /* 15576 * If timestamp option is aligned nicely, get values inline, 15577 * otherwise call general routine to parse. Only do that 15578 * if timestamp is the only option. 15579 */ 15580 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15581 TCPOPT_REAL_TS_LEN && 15582 OK_32PTR((up = ((uint8_t *)tcph) + 15583 TCP_MIN_HEADER_LENGTH)) && 15584 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15585 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15586 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15587 15588 options = TCP_OPT_TSTAMP_PRESENT; 15589 } else { 15590 if (tcp->tcp_snd_sack_ok) { 15591 tcpoptp->tcp = tcp; 15592 } else { 15593 tcpoptp->tcp = NULL; 15594 } 15595 options = tcp_parse_options(tcph, tcpoptp); 15596 } 15597 15598 if (options & TCP_OPT_TSTAMP_PRESENT) { 15599 /* 15600 * Do PAWS per RFC 1323 section 4.2. Accept RST 15601 * regardless of the timestamp, page 18 RFC 1323.bis. 15602 */ 15603 if ((flags & TH_RST) == 0 && 15604 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15605 tcp->tcp_ts_recent)) { 15606 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15607 PAWS_TIMEOUT)) { 15608 /* This segment is not acceptable. */ 15609 return (B_FALSE); 15610 } else { 15611 /* 15612 * Connection has been idle for 15613 * too long. Reset the timestamp 15614 * and assume the segment is valid. 15615 */ 15616 tcp->tcp_ts_recent = 15617 tcpoptp->tcp_opt_ts_val; 15618 } 15619 } 15620 } else { 15621 /* 15622 * If we don't get a timestamp on every packet, we 15623 * figure we can't really trust 'em, so we stop sending 15624 * and parsing them. 15625 */ 15626 tcp->tcp_snd_ts_ok = B_FALSE; 15627 15628 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15629 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15630 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15631 /* 15632 * Adjust the tcp_mss accordingly. We also need to 15633 * adjust tcp_cwnd here in accordance with the new mss. 15634 * But we avoid doing a slow start here so as to not 15635 * to lose on the transfer rate built up so far. 15636 */ 15637 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15638 if (tcp->tcp_snd_sack_ok) { 15639 ASSERT(tcp->tcp_sack_info != NULL); 15640 tcp->tcp_max_sack_blk = 4; 15641 } 15642 } 15643 return (B_TRUE); 15644 } 15645 15646 /* 15647 * Attach ancillary data to a received TCP segments for the 15648 * ancillary pieces requested by the application that are 15649 * different than they were in the previous data segment. 15650 * 15651 * Save the "current" values once memory allocation is ok so that 15652 * when memory allocation fails we can just wait for the next data segment. 15653 */ 15654 static mblk_t * 15655 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15656 { 15657 struct T_optdata_ind *todi; 15658 int optlen; 15659 uchar_t *optptr; 15660 struct T_opthdr *toh; 15661 uint_t addflag; /* Which pieces to add */ 15662 mblk_t *mp1; 15663 15664 optlen = 0; 15665 addflag = 0; 15666 /* If app asked for pktinfo and the index has changed ... */ 15667 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15668 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15669 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15670 optlen += sizeof (struct T_opthdr) + 15671 sizeof (struct in6_pktinfo); 15672 addflag |= TCP_IPV6_RECVPKTINFO; 15673 } 15674 /* If app asked for hoplimit and it has changed ... */ 15675 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15676 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15677 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15678 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15679 addflag |= TCP_IPV6_RECVHOPLIMIT; 15680 } 15681 /* If app asked for tclass and it has changed ... */ 15682 if ((ipp->ipp_fields & IPPF_TCLASS) && 15683 ipp->ipp_tclass != tcp->tcp_recvtclass && 15684 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15685 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15686 addflag |= TCP_IPV6_RECVTCLASS; 15687 } 15688 /* 15689 * If app asked for hopbyhop headers and it has changed ... 15690 * For security labels, note that (1) security labels can't change on 15691 * a connected socket at all, (2) we're connected to at most one peer, 15692 * (3) if anything changes, then it must be some other extra option. 15693 */ 15694 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15695 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15696 (ipp->ipp_fields & IPPF_HOPOPTS), 15697 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15698 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15699 tcp->tcp_label_len; 15700 addflag |= TCP_IPV6_RECVHOPOPTS; 15701 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15702 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15703 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15704 return (mp); 15705 } 15706 /* If app asked for dst headers before routing headers ... */ 15707 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15708 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15709 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15710 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15711 optlen += sizeof (struct T_opthdr) + 15712 ipp->ipp_rtdstoptslen; 15713 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15714 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15715 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15716 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15717 return (mp); 15718 } 15719 /* If app asked for routing headers and it has changed ... */ 15720 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15721 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15722 (ipp->ipp_fields & IPPF_RTHDR), 15723 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15724 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15725 addflag |= TCP_IPV6_RECVRTHDR; 15726 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15727 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15728 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15729 return (mp); 15730 } 15731 /* If app asked for dest headers and it has changed ... */ 15732 if ((tcp->tcp_ipv6_recvancillary & 15733 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15734 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15735 (ipp->ipp_fields & IPPF_DSTOPTS), 15736 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15737 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15738 addflag |= TCP_IPV6_RECVDSTOPTS; 15739 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15740 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15741 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15742 return (mp); 15743 } 15744 15745 if (optlen == 0) { 15746 /* Nothing to add */ 15747 return (mp); 15748 } 15749 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15750 if (mp1 == NULL) { 15751 /* 15752 * Defer sending ancillary data until the next TCP segment 15753 * arrives. 15754 */ 15755 return (mp); 15756 } 15757 mp1->b_cont = mp; 15758 mp = mp1; 15759 mp->b_wptr += sizeof (*todi) + optlen; 15760 mp->b_datap->db_type = M_PROTO; 15761 todi = (struct T_optdata_ind *)mp->b_rptr; 15762 todi->PRIM_type = T_OPTDATA_IND; 15763 todi->DATA_flag = 1; /* MORE data */ 15764 todi->OPT_length = optlen; 15765 todi->OPT_offset = sizeof (*todi); 15766 optptr = (uchar_t *)&todi[1]; 15767 /* 15768 * If app asked for pktinfo and the index has changed ... 15769 * Note that the local address never changes for the connection. 15770 */ 15771 if (addflag & TCP_IPV6_RECVPKTINFO) { 15772 struct in6_pktinfo *pkti; 15773 15774 toh = (struct T_opthdr *)optptr; 15775 toh->level = IPPROTO_IPV6; 15776 toh->name = IPV6_PKTINFO; 15777 toh->len = sizeof (*toh) + sizeof (*pkti); 15778 toh->status = 0; 15779 optptr += sizeof (*toh); 15780 pkti = (struct in6_pktinfo *)optptr; 15781 if (tcp->tcp_ipversion == IPV6_VERSION) 15782 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15783 else 15784 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15785 &pkti->ipi6_addr); 15786 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15787 optptr += sizeof (*pkti); 15788 ASSERT(OK_32PTR(optptr)); 15789 /* Save as "last" value */ 15790 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15791 } 15792 /* If app asked for hoplimit and it has changed ... */ 15793 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15794 toh = (struct T_opthdr *)optptr; 15795 toh->level = IPPROTO_IPV6; 15796 toh->name = IPV6_HOPLIMIT; 15797 toh->len = sizeof (*toh) + sizeof (uint_t); 15798 toh->status = 0; 15799 optptr += sizeof (*toh); 15800 *(uint_t *)optptr = ipp->ipp_hoplimit; 15801 optptr += sizeof (uint_t); 15802 ASSERT(OK_32PTR(optptr)); 15803 /* Save as "last" value */ 15804 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15805 } 15806 /* If app asked for tclass and it has changed ... */ 15807 if (addflag & TCP_IPV6_RECVTCLASS) { 15808 toh = (struct T_opthdr *)optptr; 15809 toh->level = IPPROTO_IPV6; 15810 toh->name = IPV6_TCLASS; 15811 toh->len = sizeof (*toh) + sizeof (uint_t); 15812 toh->status = 0; 15813 optptr += sizeof (*toh); 15814 *(uint_t *)optptr = ipp->ipp_tclass; 15815 optptr += sizeof (uint_t); 15816 ASSERT(OK_32PTR(optptr)); 15817 /* Save as "last" value */ 15818 tcp->tcp_recvtclass = ipp->ipp_tclass; 15819 } 15820 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15821 toh = (struct T_opthdr *)optptr; 15822 toh->level = IPPROTO_IPV6; 15823 toh->name = IPV6_HOPOPTS; 15824 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15825 tcp->tcp_label_len; 15826 toh->status = 0; 15827 optptr += sizeof (*toh); 15828 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15829 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15830 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15831 ASSERT(OK_32PTR(optptr)); 15832 /* Save as last value */ 15833 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15834 (ipp->ipp_fields & IPPF_HOPOPTS), 15835 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15836 } 15837 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15838 toh = (struct T_opthdr *)optptr; 15839 toh->level = IPPROTO_IPV6; 15840 toh->name = IPV6_RTHDRDSTOPTS; 15841 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15842 toh->status = 0; 15843 optptr += sizeof (*toh); 15844 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15845 optptr += ipp->ipp_rtdstoptslen; 15846 ASSERT(OK_32PTR(optptr)); 15847 /* Save as last value */ 15848 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15849 &tcp->tcp_rtdstoptslen, 15850 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15851 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15852 } 15853 if (addflag & TCP_IPV6_RECVRTHDR) { 15854 toh = (struct T_opthdr *)optptr; 15855 toh->level = IPPROTO_IPV6; 15856 toh->name = IPV6_RTHDR; 15857 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15858 toh->status = 0; 15859 optptr += sizeof (*toh); 15860 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15861 optptr += ipp->ipp_rthdrlen; 15862 ASSERT(OK_32PTR(optptr)); 15863 /* Save as last value */ 15864 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15865 (ipp->ipp_fields & IPPF_RTHDR), 15866 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15867 } 15868 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15869 toh = (struct T_opthdr *)optptr; 15870 toh->level = IPPROTO_IPV6; 15871 toh->name = IPV6_DSTOPTS; 15872 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15873 toh->status = 0; 15874 optptr += sizeof (*toh); 15875 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15876 optptr += ipp->ipp_dstoptslen; 15877 ASSERT(OK_32PTR(optptr)); 15878 /* Save as last value */ 15879 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15880 (ipp->ipp_fields & IPPF_DSTOPTS), 15881 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15882 } 15883 ASSERT(optptr == mp->b_wptr); 15884 return (mp); 15885 } 15886 15887 15888 /* 15889 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15890 * or a "bad" IRE detected by tcp_adapt_ire. 15891 * We can't tell if the failure was due to the laddr or the faddr 15892 * thus we clear out all addresses and ports. 15893 */ 15894 static void 15895 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15896 { 15897 queue_t *q = tcp->tcp_rq; 15898 tcph_t *tcph; 15899 struct T_error_ack *tea; 15900 conn_t *connp = tcp->tcp_connp; 15901 15902 15903 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15904 15905 if (mp->b_cont) { 15906 freemsg(mp->b_cont); 15907 mp->b_cont = NULL; 15908 } 15909 tea = (struct T_error_ack *)mp->b_rptr; 15910 switch (tea->PRIM_type) { 15911 case T_BIND_ACK: 15912 /* 15913 * Need to unbind with classifier since we were just told that 15914 * our bind succeeded. 15915 */ 15916 tcp->tcp_hard_bound = B_FALSE; 15917 tcp->tcp_hard_binding = B_FALSE; 15918 15919 ipcl_hash_remove(connp); 15920 /* Reuse the mblk if possible */ 15921 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15922 sizeof (*tea)); 15923 mp->b_rptr = mp->b_datap->db_base; 15924 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15925 tea = (struct T_error_ack *)mp->b_rptr; 15926 tea->PRIM_type = T_ERROR_ACK; 15927 tea->TLI_error = TSYSERR; 15928 tea->UNIX_error = error; 15929 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15930 tea->ERROR_prim = T_CONN_REQ; 15931 } else { 15932 tea->ERROR_prim = O_T_BIND_REQ; 15933 } 15934 break; 15935 15936 case T_ERROR_ACK: 15937 if (tcp->tcp_state >= TCPS_SYN_SENT) 15938 tea->ERROR_prim = T_CONN_REQ; 15939 break; 15940 default: 15941 panic("tcp_bind_failed: unexpected TPI type"); 15942 /*NOTREACHED*/ 15943 } 15944 15945 tcp->tcp_state = TCPS_IDLE; 15946 if (tcp->tcp_ipversion == IPV4_VERSION) 15947 tcp->tcp_ipha->ipha_src = 0; 15948 else 15949 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15950 /* 15951 * Copy of the src addr. in tcp_t is needed since 15952 * the lookup funcs. can only look at tcp_t 15953 */ 15954 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15955 15956 tcph = tcp->tcp_tcph; 15957 tcph->th_lport[0] = 0; 15958 tcph->th_lport[1] = 0; 15959 tcp_bind_hash_remove(tcp); 15960 bzero(&connp->u_port, sizeof (connp->u_port)); 15961 /* blow away saved option results if any */ 15962 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15963 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15964 15965 conn_delete_ire(tcp->tcp_connp, NULL); 15966 putnext(q, mp); 15967 } 15968 15969 /* 15970 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15971 * messages. 15972 */ 15973 void 15974 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15975 { 15976 mblk_t *mp1; 15977 uchar_t *rptr = mp->b_rptr; 15978 queue_t *q = tcp->tcp_rq; 15979 struct T_error_ack *tea; 15980 uint32_t mss; 15981 mblk_t *syn_mp; 15982 mblk_t *mdti; 15983 mblk_t *lsoi; 15984 int retval; 15985 mblk_t *ire_mp; 15986 tcp_stack_t *tcps = tcp->tcp_tcps; 15987 15988 switch (mp->b_datap->db_type) { 15989 case M_PROTO: 15990 case M_PCPROTO: 15991 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15992 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15993 break; 15994 tea = (struct T_error_ack *)rptr; 15995 switch (tea->PRIM_type) { 15996 case T_BIND_ACK: 15997 /* 15998 * Adapt Multidata information, if any. The 15999 * following tcp_mdt_update routine will free 16000 * the message. 16001 */ 16002 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 16003 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 16004 b_rptr)->mdt_capab, B_TRUE); 16005 freemsg(mdti); 16006 } 16007 16008 /* 16009 * Check to update LSO information with tcp, and 16010 * tcp_lso_update routine will free the message. 16011 */ 16012 if ((lsoi = tcp_lso_info_mp(mp)) != NULL) { 16013 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 16014 b_rptr)->lso_capab); 16015 freemsg(lsoi); 16016 } 16017 16018 /* Get the IRE, if we had requested for it */ 16019 ire_mp = tcp_ire_mp(mp); 16020 16021 if (tcp->tcp_hard_binding) { 16022 tcp->tcp_hard_binding = B_FALSE; 16023 tcp->tcp_hard_bound = B_TRUE; 16024 CL_INET_CONNECT(tcp); 16025 } else { 16026 if (ire_mp != NULL) 16027 freeb(ire_mp); 16028 goto after_syn_sent; 16029 } 16030 16031 retval = tcp_adapt_ire(tcp, ire_mp); 16032 if (ire_mp != NULL) 16033 freeb(ire_mp); 16034 if (retval == 0) { 16035 tcp_bind_failed(tcp, mp, 16036 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 16037 ENETUNREACH : EADDRNOTAVAIL)); 16038 return; 16039 } 16040 /* 16041 * Don't let an endpoint connect to itself. 16042 * Also checked in tcp_connect() but that 16043 * check can't handle the case when the 16044 * local IP address is INADDR_ANY. 16045 */ 16046 if (tcp->tcp_ipversion == IPV4_VERSION) { 16047 if ((tcp->tcp_ipha->ipha_dst == 16048 tcp->tcp_ipha->ipha_src) && 16049 (BE16_EQL(tcp->tcp_tcph->th_lport, 16050 tcp->tcp_tcph->th_fport))) { 16051 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 16052 return; 16053 } 16054 } else { 16055 if (IN6_ARE_ADDR_EQUAL( 16056 &tcp->tcp_ip6h->ip6_dst, 16057 &tcp->tcp_ip6h->ip6_src) && 16058 (BE16_EQL(tcp->tcp_tcph->th_lport, 16059 tcp->tcp_tcph->th_fport))) { 16060 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 16061 return; 16062 } 16063 } 16064 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 16065 /* 16066 * This should not be possible! Just for 16067 * defensive coding... 16068 */ 16069 if (tcp->tcp_state != TCPS_SYN_SENT) 16070 goto after_syn_sent; 16071 16072 if (is_system_labeled() && 16073 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 16074 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 16075 return; 16076 } 16077 16078 ASSERT(q == tcp->tcp_rq); 16079 /* 16080 * tcp_adapt_ire() does not adjust 16081 * for TCP/IP header length. 16082 */ 16083 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 16084 16085 /* 16086 * Just make sure our rwnd is at 16087 * least tcp_recv_hiwat_mss * MSS 16088 * large, and round up to the nearest 16089 * MSS. 16090 * 16091 * We do the round up here because 16092 * we need to get the interface 16093 * MTU first before we can do the 16094 * round up. 16095 */ 16096 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 16097 tcps->tcps_recv_hiwat_minmss * mss); 16098 q->q_hiwat = tcp->tcp_rwnd; 16099 tcp_set_ws_value(tcp); 16100 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 16101 tcp->tcp_tcph->th_win); 16102 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 16103 tcp->tcp_snd_ws_ok = B_TRUE; 16104 16105 /* 16106 * Set tcp_snd_ts_ok to true 16107 * so that tcp_xmit_mp will 16108 * include the timestamp 16109 * option in the SYN segment. 16110 */ 16111 if (tcps->tcps_tstamp_always || 16112 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 16113 tcp->tcp_snd_ts_ok = B_TRUE; 16114 } 16115 16116 /* 16117 * tcp_snd_sack_ok can be set in 16118 * tcp_adapt_ire() if the sack metric 16119 * is set. So check it here also. 16120 */ 16121 if (tcps->tcps_sack_permitted == 2 || 16122 tcp->tcp_snd_sack_ok) { 16123 if (tcp->tcp_sack_info == NULL) { 16124 tcp->tcp_sack_info = 16125 kmem_cache_alloc( 16126 tcp_sack_info_cache, 16127 KM_SLEEP); 16128 } 16129 tcp->tcp_snd_sack_ok = B_TRUE; 16130 } 16131 16132 /* 16133 * Should we use ECN? Note that the current 16134 * default value (SunOS 5.9) of tcp_ecn_permitted 16135 * is 1. The reason for doing this is that there 16136 * are equipments out there that will drop ECN 16137 * enabled IP packets. Setting it to 1 avoids 16138 * compatibility problems. 16139 */ 16140 if (tcps->tcps_ecn_permitted == 2) 16141 tcp->tcp_ecn_ok = B_TRUE; 16142 16143 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 16144 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 16145 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 16146 if (syn_mp) { 16147 cred_t *cr; 16148 pid_t pid; 16149 16150 /* 16151 * Obtain the credential from the 16152 * thread calling connect(); the credential 16153 * lives on in the second mblk which 16154 * originated from T_CONN_REQ and is echoed 16155 * with the T_BIND_ACK from ip. If none 16156 * can be found, default to the creator 16157 * of the socket. 16158 */ 16159 if (mp->b_cont == NULL || 16160 (cr = DB_CRED(mp->b_cont)) == NULL) { 16161 cr = tcp->tcp_cred; 16162 pid = tcp->tcp_cpid; 16163 } else { 16164 pid = DB_CPID(mp->b_cont); 16165 } 16166 mblk_setcred(syn_mp, cr); 16167 DB_CPID(syn_mp) = pid; 16168 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 16169 } 16170 after_syn_sent: 16171 /* 16172 * A trailer mblk indicates a waiting client upstream. 16173 * We complete here the processing begun in 16174 * either tcp_bind() or tcp_connect() by passing 16175 * upstream the reply message they supplied. 16176 */ 16177 mp1 = mp; 16178 mp = mp->b_cont; 16179 freeb(mp1); 16180 if (mp) 16181 break; 16182 return; 16183 case T_ERROR_ACK: 16184 if (tcp->tcp_debug) { 16185 (void) strlog(TCP_MOD_ID, 0, 1, 16186 SL_TRACE|SL_ERROR, 16187 "tcp_rput_other: case T_ERROR_ACK, " 16188 "ERROR_prim == %d", 16189 tea->ERROR_prim); 16190 } 16191 switch (tea->ERROR_prim) { 16192 case O_T_BIND_REQ: 16193 case T_BIND_REQ: 16194 tcp_bind_failed(tcp, mp, 16195 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 16196 ENETUNREACH : EADDRNOTAVAIL)); 16197 return; 16198 case T_UNBIND_REQ: 16199 tcp->tcp_hard_binding = B_FALSE; 16200 tcp->tcp_hard_bound = B_FALSE; 16201 if (mp->b_cont) { 16202 freemsg(mp->b_cont); 16203 mp->b_cont = NULL; 16204 } 16205 if (tcp->tcp_unbind_pending) 16206 tcp->tcp_unbind_pending = 0; 16207 else { 16208 /* From tcp_ip_unbind() - free */ 16209 freemsg(mp); 16210 return; 16211 } 16212 break; 16213 case T_SVR4_OPTMGMT_REQ: 16214 if (tcp->tcp_drop_opt_ack_cnt > 0) { 16215 /* T_OPTMGMT_REQ generated by TCP */ 16216 printf("T_SVR4_OPTMGMT_REQ failed " 16217 "%d/%d - dropped (cnt %d)\n", 16218 tea->TLI_error, tea->UNIX_error, 16219 tcp->tcp_drop_opt_ack_cnt); 16220 freemsg(mp); 16221 tcp->tcp_drop_opt_ack_cnt--; 16222 return; 16223 } 16224 break; 16225 } 16226 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 16227 tcp->tcp_drop_opt_ack_cnt > 0) { 16228 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 16229 "- dropped (cnt %d)\n", 16230 tea->TLI_error, tea->UNIX_error, 16231 tcp->tcp_drop_opt_ack_cnt); 16232 freemsg(mp); 16233 tcp->tcp_drop_opt_ack_cnt--; 16234 return; 16235 } 16236 break; 16237 case T_OPTMGMT_ACK: 16238 if (tcp->tcp_drop_opt_ack_cnt > 0) { 16239 /* T_OPTMGMT_REQ generated by TCP */ 16240 freemsg(mp); 16241 tcp->tcp_drop_opt_ack_cnt--; 16242 return; 16243 } 16244 break; 16245 default: 16246 break; 16247 } 16248 break; 16249 case M_FLUSH: 16250 if (*rptr & FLUSHR) 16251 flushq(q, FLUSHDATA); 16252 break; 16253 default: 16254 /* M_CTL will be directly sent to tcp_icmp_error() */ 16255 ASSERT(DB_TYPE(mp) != M_CTL); 16256 break; 16257 } 16258 /* 16259 * Make sure we set this bit before sending the ACK for 16260 * bind. Otherwise accept could possibly run and free 16261 * this tcp struct. 16262 */ 16263 putnext(q, mp); 16264 } 16265 16266 /* ARGSUSED */ 16267 static void 16268 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 16269 { 16270 conn_t *connp = (conn_t *)arg; 16271 tcp_t *tcp = connp->conn_tcp; 16272 queue_t *q = tcp->tcp_rq; 16273 uint_t thwin; 16274 tcp_stack_t *tcps = tcp->tcp_tcps; 16275 sodirect_t *sodp; 16276 boolean_t fc; 16277 16278 mutex_enter(&tcp->tcp_rsrv_mp_lock); 16279 tcp->tcp_rsrv_mp = mp; 16280 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16281 16282 TCP_STAT(tcps, tcp_rsrv_calls); 16283 16284 if (TCP_IS_DETACHED(tcp) || q == NULL) { 16285 return; 16286 } 16287 16288 if (tcp->tcp_fused) { 16289 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16290 16291 ASSERT(tcp->tcp_fused); 16292 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 16293 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 16294 ASSERT(!TCP_IS_DETACHED(tcp)); 16295 ASSERT(tcp->tcp_connp->conn_sqp == 16296 peer_tcp->tcp_connp->conn_sqp); 16297 16298 /* 16299 * Normally we would not get backenabled in synchronous 16300 * streams mode, but in case this happens, we need to plug 16301 * synchronous streams during our drain to prevent a race 16302 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 16303 */ 16304 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 16305 if (tcp->tcp_rcv_list != NULL) 16306 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 16307 16308 if (peer_tcp > tcp) { 16309 mutex_enter(&peer_tcp->tcp_non_sq_lock); 16310 mutex_enter(&tcp->tcp_non_sq_lock); 16311 } else { 16312 mutex_enter(&tcp->tcp_non_sq_lock); 16313 mutex_enter(&peer_tcp->tcp_non_sq_lock); 16314 } 16315 16316 if (peer_tcp->tcp_flow_stopped && 16317 (TCP_UNSENT_BYTES(peer_tcp) <= 16318 peer_tcp->tcp_xmit_lowater)) { 16319 tcp_clrqfull(peer_tcp); 16320 } 16321 mutex_exit(&peer_tcp->tcp_non_sq_lock); 16322 mutex_exit(&tcp->tcp_non_sq_lock); 16323 16324 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 16325 TCP_STAT(tcps, tcp_fusion_backenabled); 16326 return; 16327 } 16328 16329 SOD_PTR_ENTER(tcp, sodp); 16330 if (sodp != NULL) { 16331 /* An sodirect connection */ 16332 if (SOD_QFULL(sodp)) { 16333 /* Flow-controlled, need another back-enable */ 16334 fc = B_TRUE; 16335 SOD_QSETBE(sodp); 16336 } else { 16337 /* Not flow-controlled */ 16338 fc = B_FALSE; 16339 } 16340 mutex_exit(sodp->sod_lockp); 16341 } else if (canputnext(q)) { 16342 /* STREAMS, not flow-controlled */ 16343 fc = B_FALSE; 16344 } else { 16345 /* STREAMS, flow-controlled */ 16346 fc = B_TRUE; 16347 } 16348 if (!fc) { 16349 /* Not flow-controlled, open rwnd */ 16350 tcp->tcp_rwnd = q->q_hiwat; 16351 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 16352 << tcp->tcp_rcv_ws; 16353 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 16354 /* 16355 * Send back a window update immediately if TCP is above 16356 * ESTABLISHED state and the increase of the rcv window 16357 * that the other side knows is at least 1 MSS after flow 16358 * control is lifted. 16359 */ 16360 if (tcp->tcp_state >= TCPS_ESTABLISHED && 16361 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 16362 tcp_xmit_ctl(NULL, tcp, 16363 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 16364 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 16365 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 16366 } 16367 } 16368 } 16369 16370 /* 16371 * The read side service routine is called mostly when we get back-enabled as a 16372 * result of flow control relief. Since we don't actually queue anything in 16373 * TCP, we have no data to send out of here. What we do is clear the receive 16374 * window, and send out a window update. 16375 */ 16376 static void 16377 tcp_rsrv(queue_t *q) 16378 { 16379 conn_t *connp = Q_TO_CONN(q); 16380 tcp_t *tcp = connp->conn_tcp; 16381 mblk_t *mp; 16382 tcp_stack_t *tcps = tcp->tcp_tcps; 16383 16384 /* No code does a putq on the read side */ 16385 ASSERT(q->q_first == NULL); 16386 16387 /* Nothing to do for the default queue */ 16388 if (q == tcps->tcps_g_q) { 16389 return; 16390 } 16391 16392 /* 16393 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 16394 * been run. So just return. 16395 */ 16396 mutex_enter(&tcp->tcp_rsrv_mp_lock); 16397 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 16398 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16399 return; 16400 } 16401 tcp->tcp_rsrv_mp = NULL; 16402 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16403 16404 CONN_INC_REF(connp); 16405 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16406 SQTAG_TCP_RSRV); 16407 } 16408 16409 /* 16410 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16411 * We do not allow the receive window to shrink. After setting rwnd, 16412 * set the flow control hiwat of the stream. 16413 * 16414 * This function is called in 2 cases: 16415 * 16416 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16417 * connection (passive open) and in tcp_rput_data() for active connect. 16418 * This is called after tcp_mss_set() when the desired MSS value is known. 16419 * This makes sure that our window size is a mutiple of the other side's 16420 * MSS. 16421 * 2) Handling SO_RCVBUF option. 16422 * 16423 * It is ASSUMED that the requested size is a multiple of the current MSS. 16424 * 16425 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16426 * user requests so. 16427 */ 16428 static int 16429 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16430 { 16431 uint32_t mss = tcp->tcp_mss; 16432 uint32_t old_max_rwnd; 16433 uint32_t max_transmittable_rwnd; 16434 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16435 tcp_stack_t *tcps = tcp->tcp_tcps; 16436 16437 if (tcp->tcp_fused) { 16438 size_t sth_hiwat; 16439 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16440 16441 ASSERT(peer_tcp != NULL); 16442 /* 16443 * Record the stream head's high water mark for 16444 * this endpoint; this is used for flow-control 16445 * purposes in tcp_fuse_output(). 16446 */ 16447 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16448 if (!tcp_detached) 16449 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 16450 16451 /* 16452 * In the fusion case, the maxpsz stream head value of 16453 * our peer is set according to its send buffer size 16454 * and our receive buffer size; since the latter may 16455 * have changed we need to update the peer's maxpsz. 16456 */ 16457 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16458 return (rwnd); 16459 } 16460 16461 if (tcp_detached) 16462 old_max_rwnd = tcp->tcp_rwnd; 16463 else 16464 old_max_rwnd = tcp->tcp_rq->q_hiwat; 16465 16466 /* 16467 * Insist on a receive window that is at least 16468 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16469 * funny TCP interactions of Nagle algorithm, SWS avoidance 16470 * and delayed acknowledgement. 16471 */ 16472 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16473 16474 /* 16475 * If window size info has already been exchanged, TCP should not 16476 * shrink the window. Shrinking window is doable if done carefully. 16477 * We may add that support later. But so far there is not a real 16478 * need to do that. 16479 */ 16480 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16481 /* MSS may have changed, do a round up again. */ 16482 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16483 } 16484 16485 /* 16486 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16487 * can be applied even before the window scale option is decided. 16488 */ 16489 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16490 if (rwnd > max_transmittable_rwnd) { 16491 rwnd = max_transmittable_rwnd - 16492 (max_transmittable_rwnd % mss); 16493 if (rwnd < mss) 16494 rwnd = max_transmittable_rwnd; 16495 /* 16496 * If we're over the limit we may have to back down tcp_rwnd. 16497 * The increment below won't work for us. So we set all three 16498 * here and the increment below will have no effect. 16499 */ 16500 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16501 } 16502 if (tcp->tcp_localnet) { 16503 tcp->tcp_rack_abs_max = 16504 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16505 } else { 16506 /* 16507 * For a remote host on a different subnet (through a router), 16508 * we ack every other packet to be conforming to RFC1122. 16509 * tcp_deferred_acks_max is default to 2. 16510 */ 16511 tcp->tcp_rack_abs_max = 16512 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16513 } 16514 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16515 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16516 else 16517 tcp->tcp_rack_cur_max = 0; 16518 /* 16519 * Increment the current rwnd by the amount the maximum grew (we 16520 * can not overwrite it since we might be in the middle of a 16521 * connection.) 16522 */ 16523 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16524 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16525 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16526 tcp->tcp_cwnd_max = rwnd; 16527 16528 if (tcp_detached) 16529 return (rwnd); 16530 /* 16531 * We set the maximum receive window into rq->q_hiwat. 16532 * This is not actually used for flow control. 16533 */ 16534 tcp->tcp_rq->q_hiwat = rwnd; 16535 /* 16536 * Set the Stream head high water mark. This doesn't have to be 16537 * here, since we are simply using default values, but we would 16538 * prefer to choose these values algorithmically, with a likely 16539 * relationship to rwnd. 16540 */ 16541 (void) mi_set_sth_hiwat(tcp->tcp_rq, 16542 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16543 return (rwnd); 16544 } 16545 16546 /* 16547 * Return SNMP stuff in buffer in mpdata. 16548 */ 16549 mblk_t * 16550 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16551 { 16552 mblk_t *mpdata; 16553 mblk_t *mp_conn_ctl = NULL; 16554 mblk_t *mp_conn_tail; 16555 mblk_t *mp_attr_ctl = NULL; 16556 mblk_t *mp_attr_tail; 16557 mblk_t *mp6_conn_ctl = NULL; 16558 mblk_t *mp6_conn_tail; 16559 mblk_t *mp6_attr_ctl = NULL; 16560 mblk_t *mp6_attr_tail; 16561 struct opthdr *optp; 16562 mib2_tcpConnEntry_t tce; 16563 mib2_tcp6ConnEntry_t tce6; 16564 mib2_transportMLPEntry_t mlp; 16565 connf_t *connfp; 16566 int i; 16567 boolean_t ispriv; 16568 zoneid_t zoneid; 16569 int v4_conn_idx; 16570 int v6_conn_idx; 16571 conn_t *connp = Q_TO_CONN(q); 16572 tcp_stack_t *tcps; 16573 ip_stack_t *ipst; 16574 mblk_t *mp2ctl; 16575 16576 /* 16577 * make a copy of the original message 16578 */ 16579 mp2ctl = copymsg(mpctl); 16580 16581 if (mpctl == NULL || 16582 (mpdata = mpctl->b_cont) == NULL || 16583 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16584 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16585 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16586 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16587 freemsg(mp_conn_ctl); 16588 freemsg(mp_attr_ctl); 16589 freemsg(mp6_conn_ctl); 16590 freemsg(mp6_attr_ctl); 16591 freemsg(mpctl); 16592 freemsg(mp2ctl); 16593 return (NULL); 16594 } 16595 16596 ipst = connp->conn_netstack->netstack_ip; 16597 tcps = connp->conn_netstack->netstack_tcp; 16598 16599 /* build table of connections -- need count in fixed part */ 16600 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16601 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16602 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16603 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16604 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16605 16606 ispriv = 16607 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16608 zoneid = Q_TO_CONN(q)->conn_zoneid; 16609 16610 v4_conn_idx = v6_conn_idx = 0; 16611 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16612 16613 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16614 ipst = tcps->tcps_netstack->netstack_ip; 16615 16616 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16617 16618 connp = NULL; 16619 16620 while ((connp = 16621 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16622 tcp_t *tcp; 16623 boolean_t needattr; 16624 16625 if (connp->conn_zoneid != zoneid) 16626 continue; /* not in this zone */ 16627 16628 tcp = connp->conn_tcp; 16629 UPDATE_MIB(&tcps->tcps_mib, 16630 tcpHCInSegs, tcp->tcp_ibsegs); 16631 tcp->tcp_ibsegs = 0; 16632 UPDATE_MIB(&tcps->tcps_mib, 16633 tcpHCOutSegs, tcp->tcp_obsegs); 16634 tcp->tcp_obsegs = 0; 16635 16636 tce6.tcp6ConnState = tce.tcpConnState = 16637 tcp_snmp_state(tcp); 16638 if (tce.tcpConnState == MIB2_TCP_established || 16639 tce.tcpConnState == MIB2_TCP_closeWait) 16640 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16641 16642 needattr = B_FALSE; 16643 bzero(&mlp, sizeof (mlp)); 16644 if (connp->conn_mlp_type != mlptSingle) { 16645 if (connp->conn_mlp_type == mlptShared || 16646 connp->conn_mlp_type == mlptBoth) 16647 mlp.tme_flags |= MIB2_TMEF_SHARED; 16648 if (connp->conn_mlp_type == mlptPrivate || 16649 connp->conn_mlp_type == mlptBoth) 16650 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16651 needattr = B_TRUE; 16652 } 16653 if (connp->conn_peercred != NULL) { 16654 ts_label_t *tsl; 16655 16656 tsl = crgetlabel(connp->conn_peercred); 16657 mlp.tme_doi = label2doi(tsl); 16658 mlp.tme_label = *label2bslabel(tsl); 16659 needattr = B_TRUE; 16660 } 16661 16662 /* Create a message to report on IPv6 entries */ 16663 if (tcp->tcp_ipversion == IPV6_VERSION) { 16664 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16665 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16666 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16667 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16668 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16669 /* Don't want just anybody seeing these... */ 16670 if (ispriv) { 16671 tce6.tcp6ConnEntryInfo.ce_snxt = 16672 tcp->tcp_snxt; 16673 tce6.tcp6ConnEntryInfo.ce_suna = 16674 tcp->tcp_suna; 16675 tce6.tcp6ConnEntryInfo.ce_rnxt = 16676 tcp->tcp_rnxt; 16677 tce6.tcp6ConnEntryInfo.ce_rack = 16678 tcp->tcp_rack; 16679 } else { 16680 /* 16681 * Netstat, unfortunately, uses this to 16682 * get send/receive queue sizes. How to fix? 16683 * Why not compute the difference only? 16684 */ 16685 tce6.tcp6ConnEntryInfo.ce_snxt = 16686 tcp->tcp_snxt - tcp->tcp_suna; 16687 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16688 tce6.tcp6ConnEntryInfo.ce_rnxt = 16689 tcp->tcp_rnxt - tcp->tcp_rack; 16690 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16691 } 16692 16693 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16694 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16695 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16696 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16697 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16698 16699 tce6.tcp6ConnCreationProcess = 16700 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16701 tcp->tcp_cpid; 16702 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16703 16704 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16705 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16706 16707 mlp.tme_connidx = v6_conn_idx++; 16708 if (needattr) 16709 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16710 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16711 } 16712 /* 16713 * Create an IPv4 table entry for IPv4 entries and also 16714 * for IPv6 entries which are bound to in6addr_any 16715 * but don't have IPV6_V6ONLY set. 16716 * (i.e. anything an IPv4 peer could connect to) 16717 */ 16718 if (tcp->tcp_ipversion == IPV4_VERSION || 16719 (tcp->tcp_state <= TCPS_LISTEN && 16720 !tcp->tcp_connp->conn_ipv6_v6only && 16721 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16722 if (tcp->tcp_ipversion == IPV6_VERSION) { 16723 tce.tcpConnRemAddress = INADDR_ANY; 16724 tce.tcpConnLocalAddress = INADDR_ANY; 16725 } else { 16726 tce.tcpConnRemAddress = 16727 tcp->tcp_remote; 16728 tce.tcpConnLocalAddress = 16729 tcp->tcp_ip_src; 16730 } 16731 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16732 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16733 /* Don't want just anybody seeing these... */ 16734 if (ispriv) { 16735 tce.tcpConnEntryInfo.ce_snxt = 16736 tcp->tcp_snxt; 16737 tce.tcpConnEntryInfo.ce_suna = 16738 tcp->tcp_suna; 16739 tce.tcpConnEntryInfo.ce_rnxt = 16740 tcp->tcp_rnxt; 16741 tce.tcpConnEntryInfo.ce_rack = 16742 tcp->tcp_rack; 16743 } else { 16744 /* 16745 * Netstat, unfortunately, uses this to 16746 * get send/receive queue sizes. How 16747 * to fix? 16748 * Why not compute the difference only? 16749 */ 16750 tce.tcpConnEntryInfo.ce_snxt = 16751 tcp->tcp_snxt - tcp->tcp_suna; 16752 tce.tcpConnEntryInfo.ce_suna = 0; 16753 tce.tcpConnEntryInfo.ce_rnxt = 16754 tcp->tcp_rnxt - tcp->tcp_rack; 16755 tce.tcpConnEntryInfo.ce_rack = 0; 16756 } 16757 16758 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16759 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16760 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16761 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16762 tce.tcpConnEntryInfo.ce_state = 16763 tcp->tcp_state; 16764 16765 tce.tcpConnCreationProcess = 16766 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16767 tcp->tcp_cpid; 16768 tce.tcpConnCreationTime = tcp->tcp_open_time; 16769 16770 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16771 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16772 16773 mlp.tme_connidx = v4_conn_idx++; 16774 if (needattr) 16775 (void) snmp_append_data2( 16776 mp_attr_ctl->b_cont, 16777 &mp_attr_tail, (char *)&mlp, 16778 sizeof (mlp)); 16779 } 16780 } 16781 } 16782 16783 /* fixed length structure for IPv4 and IPv6 counters */ 16784 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16785 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16786 sizeof (mib2_tcp6ConnEntry_t)); 16787 /* synchronize 32- and 64-bit counters */ 16788 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16789 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16790 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16791 optp->level = MIB2_TCP; 16792 optp->name = 0; 16793 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16794 sizeof (tcps->tcps_mib)); 16795 optp->len = msgdsize(mpdata); 16796 qreply(q, mpctl); 16797 16798 /* table of connections... */ 16799 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16800 sizeof (struct T_optmgmt_ack)]; 16801 optp->level = MIB2_TCP; 16802 optp->name = MIB2_TCP_CONN; 16803 optp->len = msgdsize(mp_conn_ctl->b_cont); 16804 qreply(q, mp_conn_ctl); 16805 16806 /* table of MLP attributes... */ 16807 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16808 sizeof (struct T_optmgmt_ack)]; 16809 optp->level = MIB2_TCP; 16810 optp->name = EXPER_XPORT_MLP; 16811 optp->len = msgdsize(mp_attr_ctl->b_cont); 16812 if (optp->len == 0) 16813 freemsg(mp_attr_ctl); 16814 else 16815 qreply(q, mp_attr_ctl); 16816 16817 /* table of IPv6 connections... */ 16818 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16819 sizeof (struct T_optmgmt_ack)]; 16820 optp->level = MIB2_TCP6; 16821 optp->name = MIB2_TCP6_CONN; 16822 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16823 qreply(q, mp6_conn_ctl); 16824 16825 /* table of IPv6 MLP attributes... */ 16826 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16827 sizeof (struct T_optmgmt_ack)]; 16828 optp->level = MIB2_TCP6; 16829 optp->name = EXPER_XPORT_MLP; 16830 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16831 if (optp->len == 0) 16832 freemsg(mp6_attr_ctl); 16833 else 16834 qreply(q, mp6_attr_ctl); 16835 return (mp2ctl); 16836 } 16837 16838 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16839 /* ARGSUSED */ 16840 int 16841 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16842 { 16843 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16844 16845 switch (level) { 16846 case MIB2_TCP: 16847 switch (name) { 16848 case 13: 16849 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16850 return (0); 16851 /* TODO: delete entry defined by tce */ 16852 return (1); 16853 default: 16854 return (0); 16855 } 16856 default: 16857 return (1); 16858 } 16859 } 16860 16861 /* Translate TCP state to MIB2 TCP state. */ 16862 static int 16863 tcp_snmp_state(tcp_t *tcp) 16864 { 16865 if (tcp == NULL) 16866 return (0); 16867 16868 switch (tcp->tcp_state) { 16869 case TCPS_CLOSED: 16870 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16871 case TCPS_BOUND: 16872 return (MIB2_TCP_closed); 16873 case TCPS_LISTEN: 16874 return (MIB2_TCP_listen); 16875 case TCPS_SYN_SENT: 16876 return (MIB2_TCP_synSent); 16877 case TCPS_SYN_RCVD: 16878 return (MIB2_TCP_synReceived); 16879 case TCPS_ESTABLISHED: 16880 return (MIB2_TCP_established); 16881 case TCPS_CLOSE_WAIT: 16882 return (MIB2_TCP_closeWait); 16883 case TCPS_FIN_WAIT_1: 16884 return (MIB2_TCP_finWait1); 16885 case TCPS_CLOSING: 16886 return (MIB2_TCP_closing); 16887 case TCPS_LAST_ACK: 16888 return (MIB2_TCP_lastAck); 16889 case TCPS_FIN_WAIT_2: 16890 return (MIB2_TCP_finWait2); 16891 case TCPS_TIME_WAIT: 16892 return (MIB2_TCP_timeWait); 16893 default: 16894 return (0); 16895 } 16896 } 16897 16898 static char tcp_report_header[] = 16899 "TCP " MI_COL_HDRPAD_STR 16900 "zone dest snxt suna " 16901 "swnd rnxt rack rwnd rto mss w sw rw t " 16902 "recent [lport,fport] state"; 16903 16904 /* 16905 * TCP status report triggered via the Named Dispatch mechanism. 16906 */ 16907 /* ARGSUSED */ 16908 static void 16909 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16910 cred_t *cr) 16911 { 16912 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16913 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16914 char cflag; 16915 in6_addr_t v6dst; 16916 char buf[80]; 16917 uint_t print_len, buf_len; 16918 16919 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16920 if (buf_len <= 0) 16921 return; 16922 16923 if (hashval >= 0) 16924 (void) sprintf(hash, "%03d ", hashval); 16925 else 16926 hash[0] = '\0'; 16927 16928 /* 16929 * Note that we use the remote address in the tcp_b structure. 16930 * This means that it will print out the real destination address, 16931 * not the next hop's address if source routing is used. This 16932 * avoid the confusion on the output because user may not 16933 * know that source routing is used for a connection. 16934 */ 16935 if (tcp->tcp_ipversion == IPV4_VERSION) { 16936 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16937 } else { 16938 v6dst = tcp->tcp_remote_v6; 16939 } 16940 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16941 /* 16942 * the ispriv checks are so that normal users cannot determine 16943 * sequence number information using NDD. 16944 */ 16945 16946 if (TCP_IS_DETACHED(tcp)) 16947 cflag = '*'; 16948 else 16949 cflag = ' '; 16950 print_len = snprintf((char *)mp->b_wptr, buf_len, 16951 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16952 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16953 hash, 16954 (void *)tcp, 16955 tcp->tcp_connp->conn_zoneid, 16956 addrbuf, 16957 (ispriv) ? tcp->tcp_snxt : 0, 16958 (ispriv) ? tcp->tcp_suna : 0, 16959 tcp->tcp_swnd, 16960 (ispriv) ? tcp->tcp_rnxt : 0, 16961 (ispriv) ? tcp->tcp_rack : 0, 16962 tcp->tcp_rwnd, 16963 tcp->tcp_rto, 16964 tcp->tcp_mss, 16965 tcp->tcp_snd_ws_ok, 16966 tcp->tcp_snd_ws, 16967 tcp->tcp_rcv_ws, 16968 tcp->tcp_snd_ts_ok, 16969 tcp->tcp_ts_recent, 16970 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16971 if (print_len < buf_len) { 16972 ((mblk_t *)mp)->b_wptr += print_len; 16973 } else { 16974 ((mblk_t *)mp)->b_wptr += buf_len; 16975 } 16976 } 16977 16978 /* 16979 * TCP status report (for listeners only) triggered via the Named Dispatch 16980 * mechanism. 16981 */ 16982 /* ARGSUSED */ 16983 static void 16984 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16985 { 16986 char addrbuf[INET6_ADDRSTRLEN]; 16987 in6_addr_t v6dst; 16988 uint_t print_len, buf_len; 16989 16990 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16991 if (buf_len <= 0) 16992 return; 16993 16994 if (tcp->tcp_ipversion == IPV4_VERSION) { 16995 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16996 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16997 } else { 16998 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16999 addrbuf, sizeof (addrbuf)); 17000 } 17001 print_len = snprintf((char *)mp->b_wptr, buf_len, 17002 "%03d " 17003 MI_COL_PTRFMT_STR 17004 "%d %s %05u %08u %d/%d/%d%c\n", 17005 hashval, (void *)tcp, 17006 tcp->tcp_connp->conn_zoneid, 17007 addrbuf, 17008 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 17009 tcp->tcp_conn_req_seqnum, 17010 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 17011 tcp->tcp_conn_req_max, 17012 tcp->tcp_syn_defense ? '*' : ' '); 17013 if (print_len < buf_len) { 17014 ((mblk_t *)mp)->b_wptr += print_len; 17015 } else { 17016 ((mblk_t *)mp)->b_wptr += buf_len; 17017 } 17018 } 17019 17020 /* TCP status report triggered via the Named Dispatch mechanism. */ 17021 /* ARGSUSED */ 17022 static int 17023 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17024 { 17025 tcp_t *tcp; 17026 int i; 17027 conn_t *connp; 17028 connf_t *connfp; 17029 zoneid_t zoneid; 17030 tcp_stack_t *tcps; 17031 ip_stack_t *ipst; 17032 17033 zoneid = Q_TO_CONN(q)->conn_zoneid; 17034 tcps = Q_TO_TCP(q)->tcp_tcps; 17035 17036 /* 17037 * Because of the ndd constraint, at most we can have 64K buffer 17038 * to put in all TCP info. So to be more efficient, just 17039 * allocate a 64K buffer here, assuming we need that large buffer. 17040 * This may be a problem as any user can read tcp_status. Therefore 17041 * we limit the rate of doing this using tcp_ndd_get_info_interval. 17042 * This should be OK as normal users should not do this too often. 17043 */ 17044 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17045 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17046 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17047 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17048 return (0); 17049 } 17050 } 17051 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17052 /* The following may work even if we cannot get a large buf. */ 17053 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17054 return (0); 17055 } 17056 17057 (void) mi_mpprintf(mp, "%s", tcp_report_header); 17058 17059 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 17060 17061 ipst = tcps->tcps_netstack->netstack_ip; 17062 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 17063 17064 connp = NULL; 17065 17066 while ((connp = 17067 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17068 tcp = connp->conn_tcp; 17069 if (zoneid != GLOBAL_ZONEID && 17070 zoneid != connp->conn_zoneid) 17071 continue; 17072 tcp_report_item(mp->b_cont, tcp, -1, tcp, 17073 cr); 17074 } 17075 17076 } 17077 17078 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17079 return (0); 17080 } 17081 17082 /* TCP status report triggered via the Named Dispatch mechanism. */ 17083 /* ARGSUSED */ 17084 static int 17085 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17086 { 17087 tf_t *tbf; 17088 tcp_t *tcp; 17089 int i; 17090 zoneid_t zoneid; 17091 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 17092 17093 zoneid = Q_TO_CONN(q)->conn_zoneid; 17094 17095 /* Refer to comments in tcp_status_report(). */ 17096 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17097 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17098 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17099 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17100 return (0); 17101 } 17102 } 17103 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17104 /* The following may work even if we cannot get a large buf. */ 17105 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17106 return (0); 17107 } 17108 17109 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17110 17111 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 17112 tbf = &tcps->tcps_bind_fanout[i]; 17113 mutex_enter(&tbf->tf_lock); 17114 for (tcp = tbf->tf_tcp; tcp != NULL; 17115 tcp = tcp->tcp_bind_hash) { 17116 if (zoneid != GLOBAL_ZONEID && 17117 zoneid != tcp->tcp_connp->conn_zoneid) 17118 continue; 17119 CONN_INC_REF(tcp->tcp_connp); 17120 tcp_report_item(mp->b_cont, tcp, i, 17121 Q_TO_TCP(q), cr); 17122 CONN_DEC_REF(tcp->tcp_connp); 17123 } 17124 mutex_exit(&tbf->tf_lock); 17125 } 17126 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17127 return (0); 17128 } 17129 17130 /* TCP status report triggered via the Named Dispatch mechanism. */ 17131 /* ARGSUSED */ 17132 static int 17133 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17134 { 17135 connf_t *connfp; 17136 conn_t *connp; 17137 tcp_t *tcp; 17138 int i; 17139 zoneid_t zoneid; 17140 tcp_stack_t *tcps; 17141 ip_stack_t *ipst; 17142 17143 zoneid = Q_TO_CONN(q)->conn_zoneid; 17144 tcps = Q_TO_TCP(q)->tcp_tcps; 17145 17146 /* Refer to comments in tcp_status_report(). */ 17147 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17148 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17149 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17150 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17151 return (0); 17152 } 17153 } 17154 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17155 /* The following may work even if we cannot get a large buf. */ 17156 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17157 return (0); 17158 } 17159 17160 (void) mi_mpprintf(mp, 17161 " TCP " MI_COL_HDRPAD_STR 17162 "zone IP addr port seqnum backlog (q0/q/max)"); 17163 17164 ipst = tcps->tcps_netstack->netstack_ip; 17165 17166 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 17167 connfp = &ipst->ips_ipcl_bind_fanout[i]; 17168 connp = NULL; 17169 while ((connp = 17170 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17171 tcp = connp->conn_tcp; 17172 if (zoneid != GLOBAL_ZONEID && 17173 zoneid != connp->conn_zoneid) 17174 continue; 17175 tcp_report_listener(mp->b_cont, tcp, i); 17176 } 17177 } 17178 17179 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17180 return (0); 17181 } 17182 17183 /* TCP status report triggered via the Named Dispatch mechanism. */ 17184 /* ARGSUSED */ 17185 static int 17186 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17187 { 17188 connf_t *connfp; 17189 conn_t *connp; 17190 tcp_t *tcp; 17191 int i; 17192 zoneid_t zoneid; 17193 tcp_stack_t *tcps; 17194 ip_stack_t *ipst; 17195 17196 zoneid = Q_TO_CONN(q)->conn_zoneid; 17197 tcps = Q_TO_TCP(q)->tcp_tcps; 17198 ipst = tcps->tcps_netstack->netstack_ip; 17199 17200 /* Refer to comments in tcp_status_report(). */ 17201 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17202 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17203 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17204 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17205 return (0); 17206 } 17207 } 17208 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17209 /* The following may work even if we cannot get a large buf. */ 17210 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17211 return (0); 17212 } 17213 17214 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 17215 ipst->ips_ipcl_conn_fanout_size); 17216 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17217 17218 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 17219 connfp = &ipst->ips_ipcl_conn_fanout[i]; 17220 connp = NULL; 17221 while ((connp = 17222 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17223 tcp = connp->conn_tcp; 17224 if (zoneid != GLOBAL_ZONEID && 17225 zoneid != connp->conn_zoneid) 17226 continue; 17227 tcp_report_item(mp->b_cont, tcp, i, 17228 Q_TO_TCP(q), cr); 17229 } 17230 } 17231 17232 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17233 return (0); 17234 } 17235 17236 /* TCP status report triggered via the Named Dispatch mechanism. */ 17237 /* ARGSUSED */ 17238 static int 17239 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17240 { 17241 tf_t *tf; 17242 tcp_t *tcp; 17243 int i; 17244 zoneid_t zoneid; 17245 tcp_stack_t *tcps; 17246 17247 zoneid = Q_TO_CONN(q)->conn_zoneid; 17248 tcps = Q_TO_TCP(q)->tcp_tcps; 17249 17250 /* Refer to comments in tcp_status_report(). */ 17251 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17252 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17253 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17254 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17255 return (0); 17256 } 17257 } 17258 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17259 /* The following may work even if we cannot get a large buf. */ 17260 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17261 return (0); 17262 } 17263 17264 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17265 17266 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 17267 tf = &tcps->tcps_acceptor_fanout[i]; 17268 mutex_enter(&tf->tf_lock); 17269 for (tcp = tf->tf_tcp; tcp != NULL; 17270 tcp = tcp->tcp_acceptor_hash) { 17271 if (zoneid != GLOBAL_ZONEID && 17272 zoneid != tcp->tcp_connp->conn_zoneid) 17273 continue; 17274 tcp_report_item(mp->b_cont, tcp, i, 17275 Q_TO_TCP(q), cr); 17276 } 17277 mutex_exit(&tf->tf_lock); 17278 } 17279 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17280 return (0); 17281 } 17282 17283 /* 17284 * tcp_timer is the timer service routine. It handles the retransmission, 17285 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 17286 * from the state of the tcp instance what kind of action needs to be done 17287 * at the time it is called. 17288 */ 17289 static void 17290 tcp_timer(void *arg) 17291 { 17292 mblk_t *mp; 17293 clock_t first_threshold; 17294 clock_t second_threshold; 17295 clock_t ms; 17296 uint32_t mss; 17297 conn_t *connp = (conn_t *)arg; 17298 tcp_t *tcp = connp->conn_tcp; 17299 tcp_stack_t *tcps = tcp->tcp_tcps; 17300 17301 tcp->tcp_timer_tid = 0; 17302 17303 if (tcp->tcp_fused) 17304 return; 17305 17306 first_threshold = tcp->tcp_first_timer_threshold; 17307 second_threshold = tcp->tcp_second_timer_threshold; 17308 switch (tcp->tcp_state) { 17309 case TCPS_IDLE: 17310 case TCPS_BOUND: 17311 case TCPS_LISTEN: 17312 return; 17313 case TCPS_SYN_RCVD: { 17314 tcp_t *listener = tcp->tcp_listener; 17315 17316 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 17317 ASSERT(tcp->tcp_rq == listener->tcp_rq); 17318 /* it's our first timeout */ 17319 tcp->tcp_syn_rcvd_timeout = 1; 17320 mutex_enter(&listener->tcp_eager_lock); 17321 listener->tcp_syn_rcvd_timeout++; 17322 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 17323 /* 17324 * Make this eager available for drop if we 17325 * need to drop one to accomodate a new 17326 * incoming SYN request. 17327 */ 17328 MAKE_DROPPABLE(listener, tcp); 17329 } 17330 if (!listener->tcp_syn_defense && 17331 (listener->tcp_syn_rcvd_timeout > 17332 (tcps->tcps_conn_req_max_q0 >> 2)) && 17333 (tcps->tcps_conn_req_max_q0 > 200)) { 17334 /* We may be under attack. Put on a defense. */ 17335 listener->tcp_syn_defense = B_TRUE; 17336 cmn_err(CE_WARN, "High TCP connect timeout " 17337 "rate! System (port %d) may be under a " 17338 "SYN flood attack!", 17339 BE16_TO_U16(listener->tcp_tcph->th_lport)); 17340 17341 listener->tcp_ip_addr_cache = kmem_zalloc( 17342 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 17343 KM_NOSLEEP); 17344 } 17345 mutex_exit(&listener->tcp_eager_lock); 17346 } else if (listener != NULL) { 17347 mutex_enter(&listener->tcp_eager_lock); 17348 tcp->tcp_syn_rcvd_timeout++; 17349 if (tcp->tcp_syn_rcvd_timeout > 1 && 17350 !tcp->tcp_closemp_used) { 17351 /* 17352 * This is our second timeout. Put the tcp in 17353 * the list of droppable eagers to allow it to 17354 * be dropped, if needed. We don't check 17355 * whether tcp_dontdrop is set or not to 17356 * protect ourselve from a SYN attack where a 17357 * remote host can spoof itself as one of the 17358 * good IP source and continue to hold 17359 * resources too long. 17360 */ 17361 MAKE_DROPPABLE(listener, tcp); 17362 } 17363 mutex_exit(&listener->tcp_eager_lock); 17364 } 17365 } 17366 /* FALLTHRU */ 17367 case TCPS_SYN_SENT: 17368 first_threshold = tcp->tcp_first_ctimer_threshold; 17369 second_threshold = tcp->tcp_second_ctimer_threshold; 17370 break; 17371 case TCPS_ESTABLISHED: 17372 case TCPS_FIN_WAIT_1: 17373 case TCPS_CLOSING: 17374 case TCPS_CLOSE_WAIT: 17375 case TCPS_LAST_ACK: 17376 /* If we have data to rexmit */ 17377 if (tcp->tcp_suna != tcp->tcp_snxt) { 17378 clock_t time_to_wait; 17379 17380 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 17381 if (!tcp->tcp_xmit_head) 17382 break; 17383 time_to_wait = lbolt - 17384 (clock_t)tcp->tcp_xmit_head->b_prev; 17385 time_to_wait = tcp->tcp_rto - 17386 TICK_TO_MSEC(time_to_wait); 17387 /* 17388 * If the timer fires too early, 1 clock tick earlier, 17389 * restart the timer. 17390 */ 17391 if (time_to_wait > msec_per_tick) { 17392 TCP_STAT(tcps, tcp_timer_fire_early); 17393 TCP_TIMER_RESTART(tcp, time_to_wait); 17394 return; 17395 } 17396 /* 17397 * When we probe zero windows, we force the swnd open. 17398 * If our peer acks with a closed window swnd will be 17399 * set to zero by tcp_rput(). As long as we are 17400 * receiving acks tcp_rput will 17401 * reset 'tcp_ms_we_have_waited' so as not to trip the 17402 * first and second interval actions. NOTE: the timer 17403 * interval is allowed to continue its exponential 17404 * backoff. 17405 */ 17406 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17407 if (tcp->tcp_debug) { 17408 (void) strlog(TCP_MOD_ID, 0, 1, 17409 SL_TRACE, "tcp_timer: zero win"); 17410 } 17411 } else { 17412 /* 17413 * After retransmission, we need to do 17414 * slow start. Set the ssthresh to one 17415 * half of current effective window and 17416 * cwnd to one MSS. Also reset 17417 * tcp_cwnd_cnt. 17418 * 17419 * Note that if tcp_ssthresh is reduced because 17420 * of ECN, do not reduce it again unless it is 17421 * already one window of data away (tcp_cwr 17422 * should then be cleared) or this is a 17423 * timeout for a retransmitted segment. 17424 */ 17425 uint32_t npkt; 17426 17427 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17428 npkt = ((tcp->tcp_timer_backoff ? 17429 tcp->tcp_cwnd_ssthresh : 17430 tcp->tcp_snxt - 17431 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17432 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17433 tcp->tcp_mss; 17434 } 17435 tcp->tcp_cwnd = tcp->tcp_mss; 17436 tcp->tcp_cwnd_cnt = 0; 17437 if (tcp->tcp_ecn_ok) { 17438 tcp->tcp_cwr = B_TRUE; 17439 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17440 tcp->tcp_ecn_cwr_sent = B_FALSE; 17441 } 17442 } 17443 break; 17444 } 17445 /* 17446 * We have something to send yet we cannot send. The 17447 * reason can be: 17448 * 17449 * 1. Zero send window: we need to do zero window probe. 17450 * 2. Zero cwnd: because of ECN, we need to "clock out 17451 * segments. 17452 * 3. SWS avoidance: receiver may have shrunk window, 17453 * reset our knowledge. 17454 * 17455 * Note that condition 2 can happen with either 1 or 17456 * 3. But 1 and 3 are exclusive. 17457 */ 17458 if (tcp->tcp_unsent != 0) { 17459 if (tcp->tcp_cwnd == 0) { 17460 /* 17461 * Set tcp_cwnd to 1 MSS so that a 17462 * new segment can be sent out. We 17463 * are "clocking out" new data when 17464 * the network is really congested. 17465 */ 17466 ASSERT(tcp->tcp_ecn_ok); 17467 tcp->tcp_cwnd = tcp->tcp_mss; 17468 } 17469 if (tcp->tcp_swnd == 0) { 17470 /* Extend window for zero window probe */ 17471 tcp->tcp_swnd++; 17472 tcp->tcp_zero_win_probe = B_TRUE; 17473 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17474 } else { 17475 /* 17476 * Handle timeout from sender SWS avoidance. 17477 * Reset our knowledge of the max send window 17478 * since the receiver might have reduced its 17479 * receive buffer. Avoid setting tcp_max_swnd 17480 * to one since that will essentially disable 17481 * the SWS checks. 17482 * 17483 * Note that since we don't have a SWS 17484 * state variable, if the timeout is set 17485 * for ECN but not for SWS, this 17486 * code will also be executed. This is 17487 * fine as tcp_max_swnd is updated 17488 * constantly and it will not affect 17489 * anything. 17490 */ 17491 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17492 } 17493 tcp_wput_data(tcp, NULL, B_FALSE); 17494 return; 17495 } 17496 /* Is there a FIN that needs to be to re retransmitted? */ 17497 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17498 !tcp->tcp_fin_acked) 17499 break; 17500 /* Nothing to do, return without restarting timer. */ 17501 TCP_STAT(tcps, tcp_timer_fire_miss); 17502 return; 17503 case TCPS_FIN_WAIT_2: 17504 /* 17505 * User closed the TCP endpoint and peer ACK'ed our FIN. 17506 * We waited some time for for peer's FIN, but it hasn't 17507 * arrived. We flush the connection now to avoid 17508 * case where the peer has rebooted. 17509 */ 17510 if (TCP_IS_DETACHED(tcp)) { 17511 (void) tcp_clean_death(tcp, 0, 23); 17512 } else { 17513 TCP_TIMER_RESTART(tcp, 17514 tcps->tcps_fin_wait_2_flush_interval); 17515 } 17516 return; 17517 case TCPS_TIME_WAIT: 17518 (void) tcp_clean_death(tcp, 0, 24); 17519 return; 17520 default: 17521 if (tcp->tcp_debug) { 17522 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17523 "tcp_timer: strange state (%d) %s", 17524 tcp->tcp_state, tcp_display(tcp, NULL, 17525 DISP_PORT_ONLY)); 17526 } 17527 return; 17528 } 17529 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17530 /* 17531 * For zero window probe, we need to send indefinitely, 17532 * unless we have not heard from the other side for some 17533 * time... 17534 */ 17535 if ((tcp->tcp_zero_win_probe == 0) || 17536 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17537 second_threshold)) { 17538 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17539 /* 17540 * If TCP is in SYN_RCVD state, send back a 17541 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17542 * should be zero in TCPS_SYN_RCVD state. 17543 */ 17544 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17545 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17546 "in SYN_RCVD", 17547 tcp, tcp->tcp_snxt, 17548 tcp->tcp_rnxt, TH_RST | TH_ACK); 17549 } 17550 (void) tcp_clean_death(tcp, 17551 tcp->tcp_client_errno ? 17552 tcp->tcp_client_errno : ETIMEDOUT, 25); 17553 return; 17554 } else { 17555 /* 17556 * Set tcp_ms_we_have_waited to second_threshold 17557 * so that in next timeout, we will do the above 17558 * check (lbolt - tcp_last_recv_time). This is 17559 * also to avoid overflow. 17560 * 17561 * We don't need to decrement tcp_timer_backoff 17562 * to avoid overflow because it will be decremented 17563 * later if new timeout value is greater than 17564 * tcp_rexmit_interval_max. In the case when 17565 * tcp_rexmit_interval_max is greater than 17566 * second_threshold, it means that we will wait 17567 * longer than second_threshold to send the next 17568 * window probe. 17569 */ 17570 tcp->tcp_ms_we_have_waited = second_threshold; 17571 } 17572 } else if (ms > first_threshold) { 17573 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17574 tcp->tcp_xmit_head != NULL) { 17575 tcp->tcp_xmit_head = 17576 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17577 } 17578 /* 17579 * We have been retransmitting for too long... The RTT 17580 * we calculated is probably incorrect. Reinitialize it. 17581 * Need to compensate for 0 tcp_rtt_sa. Reset 17582 * tcp_rtt_update so that we won't accidentally cache a 17583 * bad value. But only do this if this is not a zero 17584 * window probe. 17585 */ 17586 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17587 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17588 (tcp->tcp_rtt_sa >> 5); 17589 tcp->tcp_rtt_sa = 0; 17590 tcp_ip_notify(tcp); 17591 tcp->tcp_rtt_update = 0; 17592 } 17593 } 17594 tcp->tcp_timer_backoff++; 17595 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17596 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17597 tcps->tcps_rexmit_interval_min) { 17598 /* 17599 * This means the original RTO is tcp_rexmit_interval_min. 17600 * So we will use tcp_rexmit_interval_min as the RTO value 17601 * and do the backoff. 17602 */ 17603 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17604 } else { 17605 ms <<= tcp->tcp_timer_backoff; 17606 } 17607 if (ms > tcps->tcps_rexmit_interval_max) { 17608 ms = tcps->tcps_rexmit_interval_max; 17609 /* 17610 * ms is at max, decrement tcp_timer_backoff to avoid 17611 * overflow. 17612 */ 17613 tcp->tcp_timer_backoff--; 17614 } 17615 tcp->tcp_ms_we_have_waited += ms; 17616 if (tcp->tcp_zero_win_probe == 0) { 17617 tcp->tcp_rto = ms; 17618 } 17619 TCP_TIMER_RESTART(tcp, ms); 17620 /* 17621 * This is after a timeout and tcp_rto is backed off. Set 17622 * tcp_set_timer to 1 so that next time RTO is updated, we will 17623 * restart the timer with a correct value. 17624 */ 17625 tcp->tcp_set_timer = 1; 17626 mss = tcp->tcp_snxt - tcp->tcp_suna; 17627 if (mss > tcp->tcp_mss) 17628 mss = tcp->tcp_mss; 17629 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17630 mss = tcp->tcp_swnd; 17631 17632 if ((mp = tcp->tcp_xmit_head) != NULL) 17633 mp->b_prev = (mblk_t *)lbolt; 17634 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17635 B_TRUE); 17636 17637 /* 17638 * When slow start after retransmission begins, start with 17639 * this seq no. tcp_rexmit_max marks the end of special slow 17640 * start phase. tcp_snd_burst controls how many segments 17641 * can be sent because of an ack. 17642 */ 17643 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17644 tcp->tcp_snd_burst = TCP_CWND_SS; 17645 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17646 (tcp->tcp_unsent == 0)) { 17647 tcp->tcp_rexmit_max = tcp->tcp_fss; 17648 } else { 17649 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17650 } 17651 tcp->tcp_rexmit = B_TRUE; 17652 tcp->tcp_dupack_cnt = 0; 17653 17654 /* 17655 * Remove all rexmit SACK blk to start from fresh. 17656 */ 17657 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17658 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17659 tcp->tcp_num_notsack_blk = 0; 17660 tcp->tcp_cnt_notsack_list = 0; 17661 } 17662 if (mp == NULL) { 17663 return; 17664 } 17665 /* Attach credentials to retransmitted initial SYNs. */ 17666 if (tcp->tcp_state == TCPS_SYN_SENT) { 17667 mblk_setcred(mp, tcp->tcp_cred); 17668 DB_CPID(mp) = tcp->tcp_cpid; 17669 } 17670 17671 tcp->tcp_csuna = tcp->tcp_snxt; 17672 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17673 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17674 tcp_send_data(tcp, tcp->tcp_wq, mp); 17675 17676 } 17677 17678 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17679 static void 17680 tcp_unbind(tcp_t *tcp, mblk_t *mp) 17681 { 17682 conn_t *connp; 17683 17684 switch (tcp->tcp_state) { 17685 case TCPS_BOUND: 17686 case TCPS_LISTEN: 17687 break; 17688 default: 17689 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 17690 return; 17691 } 17692 17693 /* 17694 * Need to clean up all the eagers since after the unbind, segments 17695 * will no longer be delivered to this listener stream. 17696 */ 17697 mutex_enter(&tcp->tcp_eager_lock); 17698 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17699 tcp_eager_cleanup(tcp, 0); 17700 } 17701 mutex_exit(&tcp->tcp_eager_lock); 17702 17703 if (tcp->tcp_ipversion == IPV4_VERSION) { 17704 tcp->tcp_ipha->ipha_src = 0; 17705 } else { 17706 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17707 } 17708 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17709 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17710 tcp_bind_hash_remove(tcp); 17711 tcp->tcp_state = TCPS_IDLE; 17712 tcp->tcp_mdt = B_FALSE; 17713 /* Send M_FLUSH according to TPI */ 17714 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17715 connp = tcp->tcp_connp; 17716 connp->conn_mdt_ok = B_FALSE; 17717 ipcl_hash_remove(connp); 17718 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17719 mp = mi_tpi_ok_ack_alloc(mp); 17720 putnext(tcp->tcp_rq, mp); 17721 } 17722 17723 /* 17724 * Don't let port fall into the privileged range. 17725 * Since the extra privileged ports can be arbitrary we also 17726 * ensure that we exclude those from consideration. 17727 * tcp_g_epriv_ports is not sorted thus we loop over it until 17728 * there are no changes. 17729 * 17730 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17731 * but instead the code relies on: 17732 * - the fact that the address of the array and its size never changes 17733 * - the atomic assignment of the elements of the array 17734 * 17735 * Returns 0 if there are no more ports available. 17736 * 17737 * TS note: skip multilevel ports. 17738 */ 17739 static in_port_t 17740 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17741 { 17742 int i; 17743 boolean_t restart = B_FALSE; 17744 tcp_stack_t *tcps = tcp->tcp_tcps; 17745 17746 if (random && tcp_random_anon_port != 0) { 17747 (void) random_get_pseudo_bytes((uint8_t *)&port, 17748 sizeof (in_port_t)); 17749 /* 17750 * Unless changed by a sys admin, the smallest anon port 17751 * is 32768 and the largest anon port is 65535. It is 17752 * very likely (50%) for the random port to be smaller 17753 * than the smallest anon port. When that happens, 17754 * add port % (anon port range) to the smallest anon 17755 * port to get the random port. It should fall into the 17756 * valid anon port range. 17757 */ 17758 if (port < tcps->tcps_smallest_anon_port) { 17759 port = tcps->tcps_smallest_anon_port + 17760 port % (tcps->tcps_largest_anon_port - 17761 tcps->tcps_smallest_anon_port); 17762 } 17763 } 17764 17765 retry: 17766 if (port < tcps->tcps_smallest_anon_port) 17767 port = (in_port_t)tcps->tcps_smallest_anon_port; 17768 17769 if (port > tcps->tcps_largest_anon_port) { 17770 if (restart) 17771 return (0); 17772 restart = B_TRUE; 17773 port = (in_port_t)tcps->tcps_smallest_anon_port; 17774 } 17775 17776 if (port < tcps->tcps_smallest_nonpriv_port) 17777 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17778 17779 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17780 if (port == tcps->tcps_g_epriv_ports[i]) { 17781 port++; 17782 /* 17783 * Make sure whether the port is in the 17784 * valid range. 17785 */ 17786 goto retry; 17787 } 17788 } 17789 if (is_system_labeled() && 17790 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17791 IPPROTO_TCP, B_TRUE)) != 0) { 17792 port = i; 17793 goto retry; 17794 } 17795 return (port); 17796 } 17797 17798 /* 17799 * Return the next anonymous port in the privileged port range for 17800 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17801 * downwards. This is the same behavior as documented in the userland 17802 * library call rresvport(3N). 17803 * 17804 * TS note: skip multilevel ports. 17805 */ 17806 static in_port_t 17807 tcp_get_next_priv_port(const tcp_t *tcp) 17808 { 17809 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17810 in_port_t nextport; 17811 boolean_t restart = B_FALSE; 17812 tcp_stack_t *tcps = tcp->tcp_tcps; 17813 retry: 17814 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17815 next_priv_port >= IPPORT_RESERVED) { 17816 next_priv_port = IPPORT_RESERVED - 1; 17817 if (restart) 17818 return (0); 17819 restart = B_TRUE; 17820 } 17821 if (is_system_labeled() && 17822 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17823 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17824 next_priv_port = nextport; 17825 goto retry; 17826 } 17827 return (next_priv_port--); 17828 } 17829 17830 /* The write side r/w procedure. */ 17831 17832 #if CCS_STATS 17833 struct { 17834 struct { 17835 int64_t count, bytes; 17836 } tot, hit; 17837 } wrw_stats; 17838 #endif 17839 17840 /* 17841 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17842 * messages. 17843 */ 17844 /* ARGSUSED */ 17845 static void 17846 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17847 { 17848 conn_t *connp = (conn_t *)arg; 17849 tcp_t *tcp = connp->conn_tcp; 17850 queue_t *q = tcp->tcp_wq; 17851 17852 ASSERT(DB_TYPE(mp) != M_IOCTL); 17853 /* 17854 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17855 * Once the close starts, streamhead and sockfs will not let any data 17856 * packets come down (close ensures that there are no threads using the 17857 * queue and no new threads will come down) but since qprocsoff() 17858 * hasn't happened yet, a M_FLUSH or some non data message might 17859 * get reflected back (in response to our own FLUSHRW) and get 17860 * processed after tcp_close() is done. The conn would still be valid 17861 * because a ref would have added but we need to check the state 17862 * before actually processing the packet. 17863 */ 17864 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17865 freemsg(mp); 17866 return; 17867 } 17868 17869 switch (DB_TYPE(mp)) { 17870 case M_IOCDATA: 17871 tcp_wput_iocdata(tcp, mp); 17872 break; 17873 case M_FLUSH: 17874 tcp_wput_flush(tcp, mp); 17875 break; 17876 default: 17877 CALL_IP_WPUT(connp, q, mp); 17878 break; 17879 } 17880 } 17881 17882 /* 17883 * The TCP fast path write put procedure. 17884 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17885 */ 17886 /* ARGSUSED */ 17887 void 17888 tcp_output(void *arg, mblk_t *mp, void *arg2) 17889 { 17890 int len; 17891 int hdrlen; 17892 int plen; 17893 mblk_t *mp1; 17894 uchar_t *rptr; 17895 uint32_t snxt; 17896 tcph_t *tcph; 17897 struct datab *db; 17898 uint32_t suna; 17899 uint32_t mss; 17900 ipaddr_t *dst; 17901 ipaddr_t *src; 17902 uint32_t sum; 17903 int usable; 17904 conn_t *connp = (conn_t *)arg; 17905 tcp_t *tcp = connp->conn_tcp; 17906 uint32_t msize; 17907 tcp_stack_t *tcps = tcp->tcp_tcps; 17908 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 17909 17910 /* 17911 * Try and ASSERT the minimum possible references on the 17912 * conn early enough. Since we are executing on write side, 17913 * the connection is obviously not detached and that means 17914 * there is a ref each for TCP and IP. Since we are behind 17915 * the squeue, the minimum references needed are 3. If the 17916 * conn is in classifier hash list, there should be an 17917 * extra ref for that (we check both the possibilities). 17918 */ 17919 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17920 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17921 17922 ASSERT(DB_TYPE(mp) == M_DATA); 17923 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17924 17925 mutex_enter(&tcp->tcp_non_sq_lock); 17926 tcp->tcp_squeue_bytes -= msize; 17927 mutex_exit(&tcp->tcp_non_sq_lock); 17928 17929 /* Check to see if this connection wants to be re-fused. */ 17930 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 17931 if (tcp->tcp_ipversion == IPV4_VERSION) { 17932 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 17933 &tcp->tcp_saved_tcph); 17934 } else { 17935 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 17936 &tcp->tcp_saved_tcph); 17937 } 17938 } 17939 /* Bypass tcp protocol for fused tcp loopback */ 17940 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17941 return; 17942 17943 mss = tcp->tcp_mss; 17944 if (tcp->tcp_xmit_zc_clean) 17945 mp = tcp_zcopy_backoff(tcp, mp, 0); 17946 17947 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17948 len = (int)(mp->b_wptr - mp->b_rptr); 17949 17950 /* 17951 * Criteria for fast path: 17952 * 17953 * 1. no unsent data 17954 * 2. single mblk in request 17955 * 3. connection established 17956 * 4. data in mblk 17957 * 5. len <= mss 17958 * 6. no tcp_valid bits 17959 */ 17960 if ((tcp->tcp_unsent != 0) || 17961 (tcp->tcp_cork) || 17962 (mp->b_cont != NULL) || 17963 (tcp->tcp_state != TCPS_ESTABLISHED) || 17964 (len == 0) || 17965 (len > mss) || 17966 (tcp->tcp_valid_bits != 0)) { 17967 tcp_wput_data(tcp, mp, B_FALSE); 17968 return; 17969 } 17970 17971 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17972 ASSERT(tcp->tcp_fin_sent == 0); 17973 17974 /* queue new packet onto retransmission queue */ 17975 if (tcp->tcp_xmit_head == NULL) { 17976 tcp->tcp_xmit_head = mp; 17977 } else { 17978 tcp->tcp_xmit_last->b_cont = mp; 17979 } 17980 tcp->tcp_xmit_last = mp; 17981 tcp->tcp_xmit_tail = mp; 17982 17983 /* find out how much we can send */ 17984 /* BEGIN CSTYLED */ 17985 /* 17986 * un-acked usable 17987 * |--------------|-----------------| 17988 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17989 */ 17990 /* END CSTYLED */ 17991 17992 /* start sending from tcp_snxt */ 17993 snxt = tcp->tcp_snxt; 17994 17995 /* 17996 * Check to see if this connection has been idled for some 17997 * time and no ACK is expected. If it is, we need to slow 17998 * start again to get back the connection's "self-clock" as 17999 * described in VJ's paper. 18000 * 18001 * Refer to the comment in tcp_mss_set() for the calculation 18002 * of tcp_cwnd after idle. 18003 */ 18004 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 18005 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 18006 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 18007 } 18008 18009 usable = tcp->tcp_swnd; /* tcp window size */ 18010 if (usable > tcp->tcp_cwnd) 18011 usable = tcp->tcp_cwnd; /* congestion window smaller */ 18012 usable -= snxt; /* subtract stuff already sent */ 18013 suna = tcp->tcp_suna; 18014 usable += suna; 18015 /* usable can be < 0 if the congestion window is smaller */ 18016 if (len > usable) { 18017 /* Can't send complete M_DATA in one shot */ 18018 goto slow; 18019 } 18020 18021 mutex_enter(&tcp->tcp_non_sq_lock); 18022 if (tcp->tcp_flow_stopped && 18023 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18024 tcp_clrqfull(tcp); 18025 } 18026 mutex_exit(&tcp->tcp_non_sq_lock); 18027 18028 /* 18029 * determine if anything to send (Nagle). 18030 * 18031 * 1. len < tcp_mss (i.e. small) 18032 * 2. unacknowledged data present 18033 * 3. len < nagle limit 18034 * 4. last packet sent < nagle limit (previous packet sent) 18035 */ 18036 if ((len < mss) && (snxt != suna) && 18037 (len < (int)tcp->tcp_naglim) && 18038 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 18039 /* 18040 * This was the first unsent packet and normally 18041 * mss < xmit_hiwater so there is no need to worry 18042 * about flow control. The next packet will go 18043 * through the flow control check in tcp_wput_data(). 18044 */ 18045 /* leftover work from above */ 18046 tcp->tcp_unsent = len; 18047 tcp->tcp_xmit_tail_unsent = len; 18048 18049 return; 18050 } 18051 18052 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 18053 18054 if (snxt == suna) { 18055 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18056 } 18057 18058 /* we have always sent something */ 18059 tcp->tcp_rack_cnt = 0; 18060 18061 tcp->tcp_snxt = snxt + len; 18062 tcp->tcp_rack = tcp->tcp_rnxt; 18063 18064 if ((mp1 = dupb(mp)) == 0) 18065 goto no_memory; 18066 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 18067 mp->b_next = (mblk_t *)(uintptr_t)snxt; 18068 18069 /* adjust tcp header information */ 18070 tcph = tcp->tcp_tcph; 18071 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 18072 18073 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 18074 sum = (sum >> 16) + (sum & 0xFFFF); 18075 U16_TO_ABE16(sum, tcph->th_sum); 18076 18077 U32_TO_ABE32(snxt, tcph->th_seq); 18078 18079 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 18080 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 18081 BUMP_LOCAL(tcp->tcp_obsegs); 18082 18083 /* Update the latest receive window size in TCP header. */ 18084 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 18085 tcph->th_win); 18086 18087 tcp->tcp_last_sent_len = (ushort_t)len; 18088 18089 plen = len + tcp->tcp_hdr_len; 18090 18091 if (tcp->tcp_ipversion == IPV4_VERSION) { 18092 tcp->tcp_ipha->ipha_length = htons(plen); 18093 } else { 18094 tcp->tcp_ip6h->ip6_plen = htons(plen - 18095 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 18096 } 18097 18098 /* see if we need to allocate a mblk for the headers */ 18099 hdrlen = tcp->tcp_hdr_len; 18100 rptr = mp1->b_rptr - hdrlen; 18101 db = mp1->b_datap; 18102 if ((db->db_ref != 2) || rptr < db->db_base || 18103 (!OK_32PTR(rptr))) { 18104 /* NOTE: we assume allocb returns an OK_32PTR */ 18105 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 18106 tcps->tcps_wroff_xtra, BPRI_MED); 18107 if (!mp) { 18108 freemsg(mp1); 18109 goto no_memory; 18110 } 18111 mp->b_cont = mp1; 18112 mp1 = mp; 18113 /* Leave room for Link Level header */ 18114 /* hdrlen = tcp->tcp_hdr_len; */ 18115 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 18116 mp1->b_wptr = &rptr[hdrlen]; 18117 } 18118 mp1->b_rptr = rptr; 18119 18120 /* Fill in the timestamp option. */ 18121 if (tcp->tcp_snd_ts_ok) { 18122 U32_TO_BE32((uint32_t)lbolt, 18123 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 18124 U32_TO_BE32(tcp->tcp_ts_recent, 18125 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 18126 } else { 18127 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 18128 } 18129 18130 /* copy header into outgoing packet */ 18131 dst = (ipaddr_t *)rptr; 18132 src = (ipaddr_t *)tcp->tcp_iphc; 18133 dst[0] = src[0]; 18134 dst[1] = src[1]; 18135 dst[2] = src[2]; 18136 dst[3] = src[3]; 18137 dst[4] = src[4]; 18138 dst[5] = src[5]; 18139 dst[6] = src[6]; 18140 dst[7] = src[7]; 18141 dst[8] = src[8]; 18142 dst[9] = src[9]; 18143 if (hdrlen -= 40) { 18144 hdrlen >>= 2; 18145 dst += 10; 18146 src += 10; 18147 do { 18148 *dst++ = *src++; 18149 } while (--hdrlen); 18150 } 18151 18152 /* 18153 * Set the ECN info in the TCP header. Note that this 18154 * is not the template header. 18155 */ 18156 if (tcp->tcp_ecn_ok) { 18157 SET_ECT(tcp, rptr); 18158 18159 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 18160 if (tcp->tcp_ecn_echo_on) 18161 tcph->th_flags[0] |= TH_ECE; 18162 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 18163 tcph->th_flags[0] |= TH_CWR; 18164 tcp->tcp_ecn_cwr_sent = B_TRUE; 18165 } 18166 } 18167 18168 if (tcp->tcp_ip_forward_progress) { 18169 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 18170 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 18171 tcp->tcp_ip_forward_progress = B_FALSE; 18172 } 18173 tcp_send_data(tcp, tcp->tcp_wq, mp1); 18174 return; 18175 18176 /* 18177 * If we ran out of memory, we pretend to have sent the packet 18178 * and that it was lost on the wire. 18179 */ 18180 no_memory: 18181 return; 18182 18183 slow: 18184 /* leftover work from above */ 18185 tcp->tcp_unsent = len; 18186 tcp->tcp_xmit_tail_unsent = len; 18187 tcp_wput_data(tcp, NULL, B_FALSE); 18188 } 18189 18190 /* 18191 * The function called through squeue to get behind eager's perimeter to 18192 * finish the accept processing. 18193 */ 18194 /* ARGSUSED */ 18195 void 18196 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 18197 { 18198 conn_t *connp = (conn_t *)arg; 18199 tcp_t *tcp = connp->conn_tcp; 18200 queue_t *q = tcp->tcp_rq; 18201 mblk_t *mp1; 18202 mblk_t *stropt_mp = mp; 18203 struct stroptions *stropt; 18204 uint_t thwin; 18205 tcp_stack_t *tcps = tcp->tcp_tcps; 18206 18207 /* 18208 * Drop the eager's ref on the listener, that was placed when 18209 * this eager began life in tcp_conn_request. 18210 */ 18211 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 18212 18213 tcp->tcp_detached = B_FALSE; 18214 18215 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 18216 /* 18217 * Someone blewoff the eager before we could finish 18218 * the accept. 18219 * 18220 * The only reason eager exists it because we put in 18221 * a ref on it when conn ind went up. We need to send 18222 * a disconnect indication up while the last reference 18223 * on the eager will be dropped by the squeue when we 18224 * return. 18225 */ 18226 ASSERT(tcp->tcp_listener == NULL); 18227 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 18228 struct T_discon_ind *tdi; 18229 18230 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 18231 /* 18232 * Let us reuse the incoming mblk to avoid memory 18233 * allocation failure problems. We know that the 18234 * size of the incoming mblk i.e. stroptions is greater 18235 * than sizeof T_discon_ind. So the reallocb below 18236 * can't fail. 18237 */ 18238 freemsg(mp->b_cont); 18239 mp->b_cont = NULL; 18240 ASSERT(DB_REF(mp) == 1); 18241 mp = reallocb(mp, sizeof (struct T_discon_ind), 18242 B_FALSE); 18243 ASSERT(mp != NULL); 18244 DB_TYPE(mp) = M_PROTO; 18245 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 18246 tdi = (struct T_discon_ind *)mp->b_rptr; 18247 if (tcp->tcp_issocket) { 18248 tdi->DISCON_reason = ECONNREFUSED; 18249 tdi->SEQ_number = 0; 18250 } else { 18251 tdi->DISCON_reason = ENOPROTOOPT; 18252 tdi->SEQ_number = 18253 tcp->tcp_conn_req_seqnum; 18254 } 18255 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 18256 putnext(q, mp); 18257 } else { 18258 freemsg(mp); 18259 } 18260 if (tcp->tcp_hard_binding) { 18261 tcp->tcp_hard_binding = B_FALSE; 18262 tcp->tcp_hard_bound = B_TRUE; 18263 } 18264 return; 18265 } 18266 18267 mp1 = stropt_mp->b_cont; 18268 stropt_mp->b_cont = NULL; 18269 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 18270 stropt = (struct stroptions *)stropt_mp->b_rptr; 18271 18272 while (mp1 != NULL) { 18273 mp = mp1; 18274 mp1 = mp1->b_cont; 18275 mp->b_cont = NULL; 18276 tcp->tcp_drop_opt_ack_cnt++; 18277 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 18278 } 18279 mp = NULL; 18280 18281 /* 18282 * For a loopback connection with tcp_direct_sockfs on, note that 18283 * we don't have to protect tcp_rcv_list yet because synchronous 18284 * streams has not yet been enabled and tcp_fuse_rrw() cannot 18285 * possibly race with us. 18286 */ 18287 18288 /* 18289 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 18290 * properly. This is the first time we know of the acceptor' 18291 * queue. So we do it here. 18292 */ 18293 if (tcp->tcp_rcv_list == NULL) { 18294 /* 18295 * Recv queue is empty, tcp_rwnd should not have changed. 18296 * That means it should be equal to the listener's tcp_rwnd. 18297 */ 18298 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 18299 } else { 18300 #ifdef DEBUG 18301 uint_t cnt = 0; 18302 18303 mp1 = tcp->tcp_rcv_list; 18304 while ((mp = mp1) != NULL) { 18305 mp1 = mp->b_next; 18306 cnt += msgdsize(mp); 18307 } 18308 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 18309 #endif 18310 /* There is some data, add them back to get the max. */ 18311 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 18312 } 18313 /* 18314 * This is the first time we run on the correct 18315 * queue after tcp_accept. So fix all the q parameters 18316 * here. 18317 */ 18318 stropt->so_flags = SO_HIWAT | SO_MAXBLK | SO_WROFF; 18319 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 18320 18321 /* 18322 * Record the stream head's high water mark for this endpoint; 18323 * this is used for flow-control purposes. 18324 */ 18325 stropt->so_hiwat = tcp->tcp_fused ? 18326 tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat) : 18327 MAX(q->q_hiwat, tcps->tcps_sth_rcv_hiwat); 18328 18329 /* 18330 * Determine what write offset value to use depending on SACK and 18331 * whether the endpoint is fused or not. 18332 */ 18333 if (tcp->tcp_fused) { 18334 ASSERT(tcp->tcp_loopback); 18335 ASSERT(tcp->tcp_loopback_peer != NULL); 18336 /* 18337 * For fused tcp loopback, set the stream head's write 18338 * offset value to zero since we won't be needing any room 18339 * for TCP/IP headers. This would also improve performance 18340 * since it would reduce the amount of work done by kmem. 18341 * Non-fused tcp loopback case is handled separately below. 18342 */ 18343 stropt->so_wroff = 0; 18344 /* 18345 * Update the peer's transmit parameters according to 18346 * our recently calculated high water mark value. 18347 */ 18348 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 18349 } else if (tcp->tcp_snd_sack_ok) { 18350 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 18351 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 18352 } else { 18353 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 18354 tcps->tcps_wroff_xtra); 18355 } 18356 18357 /* 18358 * If this is endpoint is handling SSL, then reserve extra 18359 * offset and space at the end. 18360 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 18361 * overriding the previous setting. The extra cost of signing and 18362 * encrypting multiple MSS-size records (12 of them with Ethernet), 18363 * instead of a single contiguous one by the stream head 18364 * largely outweighs the statistical reduction of ACKs, when 18365 * applicable. The peer will also save on decryption and verification 18366 * costs. 18367 */ 18368 if (tcp->tcp_kssl_ctx != NULL) { 18369 stropt->so_wroff += SSL3_WROFFSET; 18370 18371 stropt->so_flags |= SO_TAIL; 18372 stropt->so_tail = SSL3_MAX_TAIL_LEN; 18373 18374 stropt->so_flags |= SO_COPYOPT; 18375 stropt->so_copyopt = ZCVMUNSAFE; 18376 18377 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 18378 } 18379 18380 /* Send the options up */ 18381 putnext(q, stropt_mp); 18382 18383 /* 18384 * Pass up any data and/or a fin that has been received. 18385 * 18386 * Adjust receive window in case it had decreased 18387 * (because there is data <=> tcp_rcv_list != NULL) 18388 * while the connection was detached. Note that 18389 * in case the eager was flow-controlled, w/o this 18390 * code, the rwnd may never open up again! 18391 */ 18392 if (tcp->tcp_rcv_list != NULL) { 18393 /* We drain directly in case of fused tcp loopback */ 18394 sodirect_t *sodp; 18395 18396 if (!tcp->tcp_fused && canputnext(q)) { 18397 tcp->tcp_rwnd = q->q_hiwat; 18398 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 18399 << tcp->tcp_rcv_ws; 18400 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 18401 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18402 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 18403 tcp_xmit_ctl(NULL, 18404 tcp, (tcp->tcp_swnd == 0) ? 18405 tcp->tcp_suna : tcp->tcp_snxt, 18406 tcp->tcp_rnxt, TH_ACK); 18407 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 18408 } 18409 18410 } 18411 18412 SOD_PTR_ENTER(tcp, sodp); 18413 if (sodp != NULL) { 18414 /* Sodirect, move from rcv_list */ 18415 ASSERT(!tcp->tcp_fused); 18416 while ((mp = tcp->tcp_rcv_list) != NULL) { 18417 tcp->tcp_rcv_list = mp->b_next; 18418 mp->b_next = NULL; 18419 (void) tcp_rcv_sod_enqueue(tcp, sodp, mp, 18420 msgdsize(mp)); 18421 } 18422 tcp->tcp_rcv_last_head = NULL; 18423 tcp->tcp_rcv_last_tail = NULL; 18424 tcp->tcp_rcv_cnt = 0; 18425 (void) tcp_rcv_sod_wakeup(tcp, sodp); 18426 /* sod_wakeup() did the mutex_exit() */ 18427 } else { 18428 /* Not sodirect, drain */ 18429 (void) tcp_rcv_drain(q, tcp); 18430 } 18431 18432 /* 18433 * For fused tcp loopback, back-enable peer endpoint 18434 * if it's currently flow-controlled. 18435 */ 18436 if (tcp->tcp_fused) { 18437 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18438 18439 ASSERT(peer_tcp != NULL); 18440 ASSERT(peer_tcp->tcp_fused); 18441 /* 18442 * In order to change the peer's tcp_flow_stopped, 18443 * we need to take locks for both end points. The 18444 * highest address is taken first. 18445 */ 18446 if (peer_tcp > tcp) { 18447 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18448 mutex_enter(&tcp->tcp_non_sq_lock); 18449 } else { 18450 mutex_enter(&tcp->tcp_non_sq_lock); 18451 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18452 } 18453 if (peer_tcp->tcp_flow_stopped) { 18454 tcp_clrqfull(peer_tcp); 18455 TCP_STAT(tcps, tcp_fusion_backenabled); 18456 } 18457 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18458 mutex_exit(&tcp->tcp_non_sq_lock); 18459 } 18460 } 18461 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18462 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18463 mp = tcp->tcp_ordrel_mp; 18464 tcp->tcp_ordrel_mp = NULL; 18465 tcp->tcp_ordrel_done = B_TRUE; 18466 putnext(q, mp); 18467 } 18468 if (tcp->tcp_hard_binding) { 18469 tcp->tcp_hard_binding = B_FALSE; 18470 tcp->tcp_hard_bound = B_TRUE; 18471 } 18472 18473 /* We can enable synchronous streams now */ 18474 if (tcp->tcp_fused) { 18475 tcp_fuse_syncstr_enable_pair(tcp); 18476 } 18477 18478 if (tcp->tcp_ka_enabled) { 18479 tcp->tcp_ka_last_intrvl = 0; 18480 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18481 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18482 } 18483 18484 /* 18485 * At this point, eager is fully established and will 18486 * have the following references - 18487 * 18488 * 2 references for connection to exist (1 for TCP and 1 for IP). 18489 * 1 reference for the squeue which will be dropped by the squeue as 18490 * soon as this function returns. 18491 * There will be 1 additonal reference for being in classifier 18492 * hash list provided something bad hasn't happened. 18493 */ 18494 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18495 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18496 } 18497 18498 /* 18499 * The function called through squeue to get behind listener's perimeter to 18500 * send a deffered conn_ind. 18501 */ 18502 /* ARGSUSED */ 18503 void 18504 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18505 { 18506 conn_t *connp = (conn_t *)arg; 18507 tcp_t *listener = connp->conn_tcp; 18508 18509 if (listener->tcp_state == TCPS_CLOSED || 18510 TCP_IS_DETACHED(listener)) { 18511 /* 18512 * If listener has closed, it would have caused a 18513 * a cleanup/blowoff to happen for the eager. 18514 */ 18515 tcp_t *tcp; 18516 struct T_conn_ind *conn_ind; 18517 18518 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18519 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18520 conn_ind->OPT_length); 18521 /* 18522 * We need to drop the ref on eager that was put 18523 * tcp_rput_data() before trying to send the conn_ind 18524 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18525 * and tcp_wput_accept() is sending this deferred conn_ind but 18526 * listener is closed so we drop the ref. 18527 */ 18528 CONN_DEC_REF(tcp->tcp_connp); 18529 freemsg(mp); 18530 return; 18531 } 18532 putnext(listener->tcp_rq, mp); 18533 } 18534 18535 18536 /* 18537 * This is the STREAMS entry point for T_CONN_RES coming down on 18538 * Acceptor STREAM when sockfs listener does accept processing. 18539 * Read the block comment on top of tcp_conn_request(). 18540 */ 18541 void 18542 tcp_wput_accept(queue_t *q, mblk_t *mp) 18543 { 18544 queue_t *rq = RD(q); 18545 struct T_conn_res *conn_res; 18546 tcp_t *eager; 18547 tcp_t *listener; 18548 struct T_ok_ack *ok; 18549 t_scalar_t PRIM_type; 18550 mblk_t *opt_mp; 18551 conn_t *econnp; 18552 18553 ASSERT(DB_TYPE(mp) == M_PROTO); 18554 18555 conn_res = (struct T_conn_res *)mp->b_rptr; 18556 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18557 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18558 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18559 if (mp != NULL) 18560 putnext(rq, mp); 18561 return; 18562 } 18563 switch (conn_res->PRIM_type) { 18564 case O_T_CONN_RES: 18565 case T_CONN_RES: 18566 /* 18567 * We pass up an err ack if allocb fails. This will 18568 * cause sockfs to issue a T_DISCON_REQ which will cause 18569 * tcp_eager_blowoff to be called. sockfs will then call 18570 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18571 * we need to do the allocb up here because we have to 18572 * make sure rq->q_qinfo->qi_qclose still points to the 18573 * correct function (tcpclose_accept) in case allocb 18574 * fails. 18575 */ 18576 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18577 if (opt_mp == NULL) { 18578 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18579 if (mp != NULL) 18580 putnext(rq, mp); 18581 return; 18582 } 18583 18584 bcopy(mp->b_rptr + conn_res->OPT_offset, 18585 &eager, conn_res->OPT_length); 18586 PRIM_type = conn_res->PRIM_type; 18587 mp->b_datap->db_type = M_PCPROTO; 18588 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18589 ok = (struct T_ok_ack *)mp->b_rptr; 18590 ok->PRIM_type = T_OK_ACK; 18591 ok->CORRECT_prim = PRIM_type; 18592 econnp = eager->tcp_connp; 18593 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 18594 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 18595 eager->tcp_rq = rq; 18596 eager->tcp_wq = q; 18597 rq->q_ptr = econnp; 18598 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 18599 q->q_ptr = econnp; 18600 q->q_qinfo = &tcp_winit; 18601 listener = eager->tcp_listener; 18602 eager->tcp_issocket = B_TRUE; 18603 18604 /* 18605 * TCP is _D_SODIRECT and sockfs is directly above so 18606 * save shared sodirect_t pointer (if any). 18607 * 18608 * If tcp_fused and sodirect enabled disable it. 18609 */ 18610 eager->tcp_sodirect = SOD_QTOSODP(eager->tcp_rq); 18611 if (eager->tcp_fused && eager->tcp_sodirect != NULL) { 18612 /* Fused, disable sodirect */ 18613 mutex_enter(eager->tcp_sodirect->sod_lockp); 18614 SOD_DISABLE(eager->tcp_sodirect); 18615 mutex_exit(eager->tcp_sodirect->sod_lockp); 18616 eager->tcp_sodirect = NULL; 18617 } 18618 18619 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18620 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18621 ASSERT(econnp->conn_netstack == 18622 listener->tcp_connp->conn_netstack); 18623 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18624 18625 /* Put the ref for IP */ 18626 CONN_INC_REF(econnp); 18627 18628 /* 18629 * We should have minimum of 3 references on the conn 18630 * at this point. One each for TCP and IP and one for 18631 * the T_conn_ind that was sent up when the 3-way handshake 18632 * completed. In the normal case we would also have another 18633 * reference (making a total of 4) for the conn being in the 18634 * classifier hash list. However the eager could have received 18635 * an RST subsequently and tcp_closei_local could have removed 18636 * the eager from the classifier hash list, hence we can't 18637 * assert that reference. 18638 */ 18639 ASSERT(econnp->conn_ref >= 3); 18640 18641 /* 18642 * Send the new local address also up to sockfs. There 18643 * should already be enough space in the mp that came 18644 * down from soaccept(). 18645 */ 18646 if (eager->tcp_family == AF_INET) { 18647 sin_t *sin; 18648 18649 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18650 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18651 sin = (sin_t *)mp->b_wptr; 18652 mp->b_wptr += sizeof (sin_t); 18653 sin->sin_family = AF_INET; 18654 sin->sin_port = eager->tcp_lport; 18655 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18656 } else { 18657 sin6_t *sin6; 18658 18659 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18660 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18661 sin6 = (sin6_t *)mp->b_wptr; 18662 mp->b_wptr += sizeof (sin6_t); 18663 sin6->sin6_family = AF_INET6; 18664 sin6->sin6_port = eager->tcp_lport; 18665 if (eager->tcp_ipversion == IPV4_VERSION) { 18666 sin6->sin6_flowinfo = 0; 18667 IN6_IPADDR_TO_V4MAPPED( 18668 eager->tcp_ipha->ipha_src, 18669 &sin6->sin6_addr); 18670 } else { 18671 ASSERT(eager->tcp_ip6h != NULL); 18672 sin6->sin6_flowinfo = 18673 eager->tcp_ip6h->ip6_vcf & 18674 ~IPV6_VERS_AND_FLOW_MASK; 18675 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18676 } 18677 sin6->sin6_scope_id = 0; 18678 sin6->__sin6_src_id = 0; 18679 } 18680 18681 putnext(rq, mp); 18682 18683 opt_mp->b_datap->db_type = M_SETOPTS; 18684 opt_mp->b_wptr += sizeof (struct stroptions); 18685 18686 /* 18687 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18688 * from listener to acceptor. The message is chained on the 18689 * bind_mp which tcp_rput_other will send down to IP. 18690 */ 18691 if (listener->tcp_bound_if != 0) { 18692 /* allocate optmgmt req */ 18693 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18694 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 18695 sizeof (int)); 18696 if (mp != NULL) 18697 linkb(opt_mp, mp); 18698 } 18699 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18700 uint_t on = 1; 18701 18702 /* allocate optmgmt req */ 18703 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18704 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 18705 if (mp != NULL) 18706 linkb(opt_mp, mp); 18707 } 18708 18709 18710 mutex_enter(&listener->tcp_eager_lock); 18711 18712 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18713 18714 tcp_t *tail; 18715 tcp_t *tcp; 18716 mblk_t *mp1; 18717 18718 tcp = listener->tcp_eager_prev_q0; 18719 /* 18720 * listener->tcp_eager_prev_q0 points to the TAIL of the 18721 * deferred T_conn_ind queue. We need to get to the head 18722 * of the queue in order to send up T_conn_ind the same 18723 * order as how the 3WHS is completed. 18724 */ 18725 while (tcp != listener) { 18726 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18727 !tcp->tcp_kssl_pending) 18728 break; 18729 else 18730 tcp = tcp->tcp_eager_prev_q0; 18731 } 18732 /* None of the pending eagers can be sent up now */ 18733 if (tcp == listener) 18734 goto no_more_eagers; 18735 18736 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18737 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18738 /* Move from q0 to q */ 18739 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18740 listener->tcp_conn_req_cnt_q0--; 18741 listener->tcp_conn_req_cnt_q++; 18742 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18743 tcp->tcp_eager_prev_q0; 18744 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18745 tcp->tcp_eager_next_q0; 18746 tcp->tcp_eager_prev_q0 = NULL; 18747 tcp->tcp_eager_next_q0 = NULL; 18748 tcp->tcp_conn_def_q0 = B_FALSE; 18749 18750 /* Make sure the tcp isn't in the list of droppables */ 18751 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18752 tcp->tcp_eager_prev_drop_q0 == NULL); 18753 18754 /* 18755 * Insert at end of the queue because sockfs sends 18756 * down T_CONN_RES in chronological order. Leaving 18757 * the older conn indications at front of the queue 18758 * helps reducing search time. 18759 */ 18760 tail = listener->tcp_eager_last_q; 18761 if (tail != NULL) { 18762 tail->tcp_eager_next_q = tcp; 18763 } else { 18764 listener->tcp_eager_next_q = tcp; 18765 } 18766 listener->tcp_eager_last_q = tcp; 18767 tcp->tcp_eager_next_q = NULL; 18768 18769 /* Need to get inside the listener perimeter */ 18770 CONN_INC_REF(listener->tcp_connp); 18771 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 18772 tcp_send_pending, listener->tcp_connp, 18773 SQTAG_TCP_SEND_PENDING); 18774 } 18775 no_more_eagers: 18776 tcp_eager_unlink(eager); 18777 mutex_exit(&listener->tcp_eager_lock); 18778 18779 /* 18780 * At this point, the eager is detached from the listener 18781 * but we still have an extra refs on eager (apart from the 18782 * usual tcp references). The ref was placed in tcp_rput_data 18783 * before sending the conn_ind in tcp_send_conn_ind. 18784 * The ref will be dropped in tcp_accept_finish(). 18785 */ 18786 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 18787 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 18788 return; 18789 default: 18790 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18791 if (mp != NULL) 18792 putnext(rq, mp); 18793 return; 18794 } 18795 } 18796 18797 static int 18798 tcp_getmyname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18799 { 18800 sin_t *sin = (sin_t *)sa; 18801 sin6_t *sin6 = (sin6_t *)sa; 18802 18803 switch (tcp->tcp_family) { 18804 case AF_INET: 18805 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18806 18807 if (*salenp < sizeof (sin_t)) 18808 return (EINVAL); 18809 18810 *sin = sin_null; 18811 sin->sin_family = AF_INET; 18812 sin->sin_port = tcp->tcp_lport; 18813 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 18814 break; 18815 18816 case AF_INET6: 18817 if (*salenp < sizeof (sin6_t)) 18818 return (EINVAL); 18819 18820 *sin6 = sin6_null; 18821 sin6->sin6_family = AF_INET6; 18822 sin6->sin6_port = tcp->tcp_lport; 18823 if (tcp->tcp_ipversion == IPV4_VERSION) { 18824 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 18825 &sin6->sin6_addr); 18826 } else { 18827 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 18828 } 18829 break; 18830 } 18831 18832 return (0); 18833 } 18834 18835 static int 18836 tcp_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18837 { 18838 sin_t *sin = (sin_t *)sa; 18839 sin6_t *sin6 = (sin6_t *)sa; 18840 18841 if (tcp->tcp_state < TCPS_SYN_RCVD) 18842 return (ENOTCONN); 18843 18844 switch (tcp->tcp_family) { 18845 case AF_INET: 18846 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18847 18848 if (*salenp < sizeof (sin_t)) 18849 return (EINVAL); 18850 18851 *sin = sin_null; 18852 sin->sin_family = AF_INET; 18853 sin->sin_port = tcp->tcp_fport; 18854 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 18855 sin->sin_addr.s_addr); 18856 break; 18857 18858 case AF_INET6: 18859 if (*salenp < sizeof (sin6_t)) 18860 return (EINVAL); 18861 18862 *sin6 = sin6_null; 18863 sin6->sin6_family = AF_INET6; 18864 sin6->sin6_port = tcp->tcp_fport; 18865 sin6->sin6_addr = tcp->tcp_remote_v6; 18866 if (tcp->tcp_ipversion == IPV6_VERSION) { 18867 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 18868 ~IPV6_VERS_AND_FLOW_MASK; 18869 } 18870 break; 18871 } 18872 18873 return (0); 18874 } 18875 18876 /* 18877 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 18878 */ 18879 static void 18880 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 18881 { 18882 void *data; 18883 mblk_t *datamp = mp->b_cont; 18884 tcp_t *tcp = Q_TO_TCP(q); 18885 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 18886 18887 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 18888 cmdp->cb_error = EPROTO; 18889 qreply(q, mp); 18890 return; 18891 } 18892 18893 data = datamp->b_rptr; 18894 18895 switch (cmdp->cb_cmd) { 18896 case TI_GETPEERNAME: 18897 cmdp->cb_error = tcp_getpeername(tcp, data, &cmdp->cb_len); 18898 break; 18899 case TI_GETMYNAME: 18900 cmdp->cb_error = tcp_getmyname(tcp, data, &cmdp->cb_len); 18901 break; 18902 default: 18903 cmdp->cb_error = EINVAL; 18904 break; 18905 } 18906 18907 qreply(q, mp); 18908 } 18909 18910 void 18911 tcp_wput(queue_t *q, mblk_t *mp) 18912 { 18913 conn_t *connp = Q_TO_CONN(q); 18914 tcp_t *tcp; 18915 void (*output_proc)(); 18916 t_scalar_t type; 18917 uchar_t *rptr; 18918 struct iocblk *iocp; 18919 uint32_t msize; 18920 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18921 18922 ASSERT(connp->conn_ref >= 2); 18923 18924 switch (DB_TYPE(mp)) { 18925 case M_DATA: 18926 tcp = connp->conn_tcp; 18927 ASSERT(tcp != NULL); 18928 18929 msize = msgdsize(mp); 18930 18931 mutex_enter(&tcp->tcp_non_sq_lock); 18932 tcp->tcp_squeue_bytes += msize; 18933 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18934 tcp_setqfull(tcp); 18935 } 18936 mutex_exit(&tcp->tcp_non_sq_lock); 18937 18938 CONN_INC_REF(connp); 18939 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18940 tcp_output, connp, SQTAG_TCP_OUTPUT); 18941 return; 18942 18943 case M_CMD: 18944 tcp_wput_cmdblk(q, mp); 18945 return; 18946 18947 case M_PROTO: 18948 case M_PCPROTO: 18949 /* 18950 * if it is a snmp message, don't get behind the squeue 18951 */ 18952 tcp = connp->conn_tcp; 18953 rptr = mp->b_rptr; 18954 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18955 type = ((union T_primitives *)rptr)->type; 18956 } else { 18957 if (tcp->tcp_debug) { 18958 (void) strlog(TCP_MOD_ID, 0, 1, 18959 SL_ERROR|SL_TRACE, 18960 "tcp_wput_proto, dropping one..."); 18961 } 18962 freemsg(mp); 18963 return; 18964 } 18965 if (type == T_SVR4_OPTMGMT_REQ) { 18966 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18967 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 18968 cr)) { 18969 /* 18970 * This was a SNMP request 18971 */ 18972 return; 18973 } else { 18974 output_proc = tcp_wput_proto; 18975 } 18976 } else { 18977 output_proc = tcp_wput_proto; 18978 } 18979 break; 18980 case M_IOCTL: 18981 /* 18982 * Most ioctls can be processed right away without going via 18983 * squeues - process them right here. Those that do require 18984 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18985 * are processed by tcp_wput_ioctl(). 18986 */ 18987 iocp = (struct iocblk *)mp->b_rptr; 18988 tcp = connp->conn_tcp; 18989 18990 switch (iocp->ioc_cmd) { 18991 case TCP_IOC_ABORT_CONN: 18992 tcp_ioctl_abort_conn(q, mp); 18993 return; 18994 case TI_GETPEERNAME: 18995 case TI_GETMYNAME: 18996 mi_copyin(q, mp, NULL, 18997 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18998 return; 18999 case ND_SET: 19000 /* nd_getset does the necessary checks */ 19001 case ND_GET: 19002 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 19003 CALL_IP_WPUT(connp, q, mp); 19004 return; 19005 } 19006 qreply(q, mp); 19007 return; 19008 case TCP_IOC_DEFAULT_Q: 19009 /* 19010 * Wants to be the default wq. Check the credentials 19011 * first, the rest is executed via squeue. 19012 */ 19013 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 19014 iocp->ioc_error = EPERM; 19015 iocp->ioc_count = 0; 19016 mp->b_datap->db_type = M_IOCACK; 19017 qreply(q, mp); 19018 return; 19019 } 19020 output_proc = tcp_wput_ioctl; 19021 break; 19022 default: 19023 output_proc = tcp_wput_ioctl; 19024 break; 19025 } 19026 break; 19027 default: 19028 output_proc = tcp_wput_nondata; 19029 break; 19030 } 19031 19032 CONN_INC_REF(connp); 19033 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 19034 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 19035 } 19036 19037 /* 19038 * Initial STREAMS write side put() procedure for sockets. It tries to 19039 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 19040 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 19041 * are handled by tcp_wput() as usual. 19042 * 19043 * All further messages will also be handled by tcp_wput() because we cannot 19044 * be sure that the above short cut is safe later. 19045 */ 19046 static void 19047 tcp_wput_sock(queue_t *wq, mblk_t *mp) 19048 { 19049 conn_t *connp = Q_TO_CONN(wq); 19050 tcp_t *tcp = connp->conn_tcp; 19051 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 19052 19053 ASSERT(wq->q_qinfo == &tcp_sock_winit); 19054 wq->q_qinfo = &tcp_winit; 19055 19056 ASSERT(IPCL_IS_TCP(connp)); 19057 ASSERT(TCP_IS_SOCKET(tcp)); 19058 19059 if (DB_TYPE(mp) == M_PCPROTO && 19060 MBLKL(mp) == sizeof (struct T_capability_req) && 19061 car->PRIM_type == T_CAPABILITY_REQ) { 19062 tcp_capability_req(tcp, mp); 19063 return; 19064 } 19065 19066 tcp_wput(wq, mp); 19067 } 19068 19069 static boolean_t 19070 tcp_zcopy_check(tcp_t *tcp) 19071 { 19072 conn_t *connp = tcp->tcp_connp; 19073 ire_t *ire; 19074 boolean_t zc_enabled = B_FALSE; 19075 tcp_stack_t *tcps = tcp->tcp_tcps; 19076 19077 if (do_tcpzcopy == 2) 19078 zc_enabled = B_TRUE; 19079 else if (tcp->tcp_ipversion == IPV4_VERSION && 19080 IPCL_IS_CONNECTED(connp) && 19081 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 19082 connp->conn_dontroute == 0 && 19083 !connp->conn_nexthop_set && 19084 connp->conn_outgoing_ill == NULL && 19085 connp->conn_nofailover_ill == NULL && 19086 do_tcpzcopy == 1) { 19087 /* 19088 * the checks above closely resemble the fast path checks 19089 * in tcp_send_data(). 19090 */ 19091 mutex_enter(&connp->conn_lock); 19092 ire = connp->conn_ire_cache; 19093 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19094 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19095 IRE_REFHOLD(ire); 19096 if (ire->ire_stq != NULL) { 19097 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 19098 19099 zc_enabled = ill && (ill->ill_capabilities & 19100 ILL_CAPAB_ZEROCOPY) && 19101 (ill->ill_zerocopy_capab-> 19102 ill_zerocopy_flags != 0); 19103 } 19104 IRE_REFRELE(ire); 19105 } 19106 mutex_exit(&connp->conn_lock); 19107 } 19108 tcp->tcp_snd_zcopy_on = zc_enabled; 19109 if (!TCP_IS_DETACHED(tcp)) { 19110 if (zc_enabled) { 19111 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 19112 TCP_STAT(tcps, tcp_zcopy_on); 19113 } else { 19114 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 19115 TCP_STAT(tcps, tcp_zcopy_off); 19116 } 19117 } 19118 return (zc_enabled); 19119 } 19120 19121 static mblk_t * 19122 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 19123 { 19124 tcp_stack_t *tcps = tcp->tcp_tcps; 19125 19126 if (do_tcpzcopy == 2) 19127 return (bp); 19128 else if (tcp->tcp_snd_zcopy_on) { 19129 tcp->tcp_snd_zcopy_on = B_FALSE; 19130 if (!TCP_IS_DETACHED(tcp)) { 19131 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 19132 TCP_STAT(tcps, tcp_zcopy_disable); 19133 } 19134 } 19135 return (tcp_zcopy_backoff(tcp, bp, 0)); 19136 } 19137 19138 /* 19139 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 19140 * the original desballoca'ed segmapped mblk. 19141 */ 19142 static mblk_t * 19143 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 19144 { 19145 mblk_t *head, *tail, *nbp; 19146 tcp_stack_t *tcps = tcp->tcp_tcps; 19147 19148 if (IS_VMLOANED_MBLK(bp)) { 19149 TCP_STAT(tcps, tcp_zcopy_backoff); 19150 if ((head = copyb(bp)) == NULL) { 19151 /* fail to backoff; leave it for the next backoff */ 19152 tcp->tcp_xmit_zc_clean = B_FALSE; 19153 return (bp); 19154 } 19155 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19156 if (fix_xmitlist) 19157 tcp_zcopy_notify(tcp); 19158 else 19159 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19160 } 19161 nbp = bp->b_cont; 19162 if (fix_xmitlist) { 19163 head->b_prev = bp->b_prev; 19164 head->b_next = bp->b_next; 19165 if (tcp->tcp_xmit_tail == bp) 19166 tcp->tcp_xmit_tail = head; 19167 } 19168 bp->b_next = NULL; 19169 bp->b_prev = NULL; 19170 freeb(bp); 19171 } else { 19172 head = bp; 19173 nbp = bp->b_cont; 19174 } 19175 tail = head; 19176 while (nbp) { 19177 if (IS_VMLOANED_MBLK(nbp)) { 19178 TCP_STAT(tcps, tcp_zcopy_backoff); 19179 if ((tail->b_cont = copyb(nbp)) == NULL) { 19180 tcp->tcp_xmit_zc_clean = B_FALSE; 19181 tail->b_cont = nbp; 19182 return (head); 19183 } 19184 tail = tail->b_cont; 19185 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19186 if (fix_xmitlist) 19187 tcp_zcopy_notify(tcp); 19188 else 19189 tail->b_datap->db_struioflag |= 19190 STRUIO_ZCNOTIFY; 19191 } 19192 bp = nbp; 19193 nbp = nbp->b_cont; 19194 if (fix_xmitlist) { 19195 tail->b_prev = bp->b_prev; 19196 tail->b_next = bp->b_next; 19197 if (tcp->tcp_xmit_tail == bp) 19198 tcp->tcp_xmit_tail = tail; 19199 } 19200 bp->b_next = NULL; 19201 bp->b_prev = NULL; 19202 freeb(bp); 19203 } else { 19204 tail->b_cont = nbp; 19205 tail = nbp; 19206 nbp = nbp->b_cont; 19207 } 19208 } 19209 if (fix_xmitlist) { 19210 tcp->tcp_xmit_last = tail; 19211 tcp->tcp_xmit_zc_clean = B_TRUE; 19212 } 19213 return (head); 19214 } 19215 19216 static void 19217 tcp_zcopy_notify(tcp_t *tcp) 19218 { 19219 struct stdata *stp; 19220 19221 if (tcp->tcp_detached) 19222 return; 19223 stp = STREAM(tcp->tcp_rq); 19224 mutex_enter(&stp->sd_lock); 19225 stp->sd_flag |= STZCNOTIFY; 19226 cv_broadcast(&stp->sd_zcopy_wait); 19227 mutex_exit(&stp->sd_lock); 19228 } 19229 19230 static boolean_t 19231 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 19232 { 19233 ire_t *ire; 19234 conn_t *connp = tcp->tcp_connp; 19235 tcp_stack_t *tcps = tcp->tcp_tcps; 19236 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19237 19238 mutex_enter(&connp->conn_lock); 19239 ire = connp->conn_ire_cache; 19240 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19241 19242 if ((ire != NULL) && 19243 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 19244 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 19245 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19246 IRE_REFHOLD(ire); 19247 mutex_exit(&connp->conn_lock); 19248 } else { 19249 boolean_t cached = B_FALSE; 19250 ts_label_t *tsl; 19251 19252 /* force a recheck later on */ 19253 tcp->tcp_ire_ill_check_done = B_FALSE; 19254 19255 TCP_DBGSTAT(tcps, tcp_ire_null1); 19256 connp->conn_ire_cache = NULL; 19257 mutex_exit(&connp->conn_lock); 19258 19259 if (ire != NULL) 19260 IRE_REFRELE_NOTR(ire); 19261 19262 tsl = crgetlabel(CONN_CRED(connp)); 19263 ire = (dst ? 19264 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 19265 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 19266 connp->conn_zoneid, tsl, ipst)); 19267 19268 if (ire == NULL) { 19269 TCP_STAT(tcps, tcp_ire_null); 19270 return (B_FALSE); 19271 } 19272 19273 IRE_REFHOLD_NOTR(ire); 19274 19275 mutex_enter(&connp->conn_lock); 19276 if (CONN_CACHE_IRE(connp)) { 19277 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 19278 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19279 TCP_CHECK_IREINFO(tcp, ire); 19280 connp->conn_ire_cache = ire; 19281 cached = B_TRUE; 19282 } 19283 rw_exit(&ire->ire_bucket->irb_lock); 19284 } 19285 mutex_exit(&connp->conn_lock); 19286 19287 /* 19288 * We can continue to use the ire but since it was 19289 * not cached, we should drop the extra reference. 19290 */ 19291 if (!cached) 19292 IRE_REFRELE_NOTR(ire); 19293 19294 /* 19295 * Rampart note: no need to select a new label here, since 19296 * labels are not allowed to change during the life of a TCP 19297 * connection. 19298 */ 19299 } 19300 19301 *irep = ire; 19302 19303 return (B_TRUE); 19304 } 19305 19306 /* 19307 * Called from tcp_send() or tcp_send_data() to find workable IRE. 19308 * 19309 * 0 = success; 19310 * 1 = failed to find ire and ill. 19311 */ 19312 static boolean_t 19313 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 19314 { 19315 ipha_t *ipha; 19316 ipaddr_t dst; 19317 ire_t *ire; 19318 ill_t *ill; 19319 conn_t *connp = tcp->tcp_connp; 19320 mblk_t *ire_fp_mp; 19321 tcp_stack_t *tcps = tcp->tcp_tcps; 19322 19323 if (mp != NULL) 19324 ipha = (ipha_t *)mp->b_rptr; 19325 else 19326 ipha = tcp->tcp_ipha; 19327 dst = ipha->ipha_dst; 19328 19329 if (!tcp_send_find_ire(tcp, &dst, &ire)) 19330 return (B_FALSE); 19331 19332 if ((ire->ire_flags & RTF_MULTIRT) || 19333 (ire->ire_stq == NULL) || 19334 (ire->ire_nce == NULL) || 19335 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 19336 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 19337 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 19338 TCP_STAT(tcps, tcp_ip_ire_send); 19339 IRE_REFRELE(ire); 19340 return (B_FALSE); 19341 } 19342 19343 ill = ire_to_ill(ire); 19344 if (connp->conn_outgoing_ill != NULL) { 19345 ill_t *conn_outgoing_ill = NULL; 19346 /* 19347 * Choose a good ill in the group to send the packets on. 19348 */ 19349 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 19350 ill = ire_to_ill(ire); 19351 } 19352 ASSERT(ill != NULL); 19353 19354 if (!tcp->tcp_ire_ill_check_done) { 19355 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19356 tcp->tcp_ire_ill_check_done = B_TRUE; 19357 } 19358 19359 *irep = ire; 19360 *illp = ill; 19361 19362 return (B_TRUE); 19363 } 19364 19365 static void 19366 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 19367 { 19368 ipha_t *ipha; 19369 ipaddr_t src; 19370 ipaddr_t dst; 19371 uint32_t cksum; 19372 ire_t *ire; 19373 uint16_t *up; 19374 ill_t *ill; 19375 conn_t *connp = tcp->tcp_connp; 19376 uint32_t hcksum_txflags = 0; 19377 mblk_t *ire_fp_mp; 19378 uint_t ire_fp_mp_len; 19379 tcp_stack_t *tcps = tcp->tcp_tcps; 19380 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19381 19382 ASSERT(DB_TYPE(mp) == M_DATA); 19383 19384 if (DB_CRED(mp) == NULL) 19385 mblk_setcred(mp, CONN_CRED(connp)); 19386 19387 ipha = (ipha_t *)mp->b_rptr; 19388 src = ipha->ipha_src; 19389 dst = ipha->ipha_dst; 19390 19391 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 19392 19393 /* 19394 * Drop off fast path for IPv6 and also if options are present or 19395 * we need to resolve a TS label. 19396 */ 19397 if (tcp->tcp_ipversion != IPV4_VERSION || 19398 !IPCL_IS_CONNECTED(connp) || 19399 !CONN_IS_LSO_MD_FASTPATH(connp) || 19400 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 19401 !connp->conn_ulp_labeled || 19402 ipha->ipha_ident == IP_HDR_INCLUDED || 19403 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 19404 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 19405 if (tcp->tcp_snd_zcopy_aware) 19406 mp = tcp_zcopy_disable(tcp, mp); 19407 TCP_STAT(tcps, tcp_ip_send); 19408 CALL_IP_WPUT(connp, q, mp); 19409 return; 19410 } 19411 19412 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 19413 if (tcp->tcp_snd_zcopy_aware) 19414 mp = tcp_zcopy_backoff(tcp, mp, 0); 19415 CALL_IP_WPUT(connp, q, mp); 19416 return; 19417 } 19418 ire_fp_mp = ire->ire_nce->nce_fp_mp; 19419 ire_fp_mp_len = MBLKL(ire_fp_mp); 19420 19421 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 19422 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 19423 #ifndef _BIG_ENDIAN 19424 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 19425 #endif 19426 19427 /* 19428 * Check to see if we need to re-enable LSO/MDT for this connection 19429 * because it was previously disabled due to changes in the ill; 19430 * note that by doing it here, this re-enabling only applies when 19431 * the packet is not dispatched through CALL_IP_WPUT(). 19432 * 19433 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 19434 * case, since that's how we ended up here. For IPv6, we do the 19435 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 19436 */ 19437 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 19438 /* 19439 * Restore LSO for this connection, so that next time around 19440 * it is eligible to go through tcp_lsosend() path again. 19441 */ 19442 TCP_STAT(tcps, tcp_lso_enabled); 19443 tcp->tcp_lso = B_TRUE; 19444 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 19445 "interface %s\n", (void *)connp, ill->ill_name)); 19446 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 19447 /* 19448 * Restore MDT for this connection, so that next time around 19449 * it is eligible to go through tcp_multisend() path again. 19450 */ 19451 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 19452 tcp->tcp_mdt = B_TRUE; 19453 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 19454 "interface %s\n", (void *)connp, ill->ill_name)); 19455 } 19456 19457 if (tcp->tcp_snd_zcopy_aware) { 19458 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 19459 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 19460 mp = tcp_zcopy_disable(tcp, mp); 19461 /* 19462 * we shouldn't need to reset ipha as the mp containing 19463 * ipha should never be a zero-copy mp. 19464 */ 19465 } 19466 19467 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 19468 ASSERT(ill->ill_hcksum_capab != NULL); 19469 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 19470 } 19471 19472 /* pseudo-header checksum (do it in parts for IP header checksum) */ 19473 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 19474 19475 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 19476 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 19477 19478 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 19479 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 19480 19481 /* Software checksum? */ 19482 if (DB_CKSUMFLAGS(mp) == 0) { 19483 TCP_STAT(tcps, tcp_out_sw_cksum); 19484 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 19485 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 19486 } 19487 19488 /* Calculate IP header checksum if hardware isn't capable */ 19489 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 19490 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 19491 ((uint16_t *)ipha)[4]); 19492 } 19493 19494 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 19495 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 19496 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 19497 19498 UPDATE_OB_PKT_COUNT(ire); 19499 ire->ire_last_used_time = lbolt; 19500 19501 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 19502 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 19503 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 19504 ntohs(ipha->ipha_length)); 19505 19506 if (ILL_DLS_CAPABLE(ill)) { 19507 /* 19508 * Send the packet directly to DLD, where it may be queued 19509 * depending on the availability of transmit resources at 19510 * the media layer. 19511 */ 19512 IP_DLS_ILL_TX(ill, ipha, mp, ipst, ire_fp_mp_len); 19513 } else { 19514 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 19515 DTRACE_PROBE4(ip4__physical__out__start, 19516 ill_t *, NULL, ill_t *, out_ill, 19517 ipha_t *, ipha, mblk_t *, mp); 19518 FW_HOOKS(ipst->ips_ip4_physical_out_event, 19519 ipst->ips_ipv4firewall_physical_out, 19520 NULL, out_ill, ipha, mp, mp, 0, ipst); 19521 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19522 19523 if (mp != NULL) { 19524 if (ipst->ips_ipobs_enabled) { 19525 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, 19526 IP_REAL_ZONEID(connp->conn_zoneid, ipst), 19527 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, 19528 ipst); 19529 } 19530 DTRACE_IP_FASTPATH(mp, ipha, out_ill, ipha, NULL); 19531 putnext(ire->ire_stq, mp); 19532 } 19533 } 19534 IRE_REFRELE(ire); 19535 } 19536 19537 /* 19538 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19539 * if the receiver shrinks the window, i.e. moves the right window to the 19540 * left, the we should not send new data, but should retransmit normally the 19541 * old unacked data between suna and suna + swnd. We might has sent data 19542 * that is now outside the new window, pretend that we didn't send it. 19543 */ 19544 static void 19545 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19546 { 19547 uint32_t snxt = tcp->tcp_snxt; 19548 mblk_t *xmit_tail; 19549 int32_t offset; 19550 19551 ASSERT(shrunk_count > 0); 19552 19553 /* Pretend we didn't send the data outside the window */ 19554 snxt -= shrunk_count; 19555 19556 /* Get the mblk and the offset in it per the shrunk window */ 19557 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19558 19559 ASSERT(xmit_tail != NULL); 19560 19561 /* Reset all the values per the now shrunk window */ 19562 tcp->tcp_snxt = snxt; 19563 tcp->tcp_xmit_tail = xmit_tail; 19564 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19565 offset; 19566 tcp->tcp_unsent += shrunk_count; 19567 19568 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19569 /* 19570 * Make sure the timer is running so that we will probe a zero 19571 * window. 19572 */ 19573 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19574 } 19575 19576 19577 /* 19578 * The TCP normal data output path. 19579 * NOTE: the logic of the fast path is duplicated from this function. 19580 */ 19581 static void 19582 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19583 { 19584 int len; 19585 mblk_t *local_time; 19586 mblk_t *mp1; 19587 uint32_t snxt; 19588 int tail_unsent; 19589 int tcpstate; 19590 int usable = 0; 19591 mblk_t *xmit_tail; 19592 queue_t *q = tcp->tcp_wq; 19593 int32_t mss; 19594 int32_t num_sack_blk = 0; 19595 int32_t tcp_hdr_len; 19596 int32_t tcp_tcp_hdr_len; 19597 int mdt_thres; 19598 int rc; 19599 tcp_stack_t *tcps = tcp->tcp_tcps; 19600 ip_stack_t *ipst; 19601 19602 tcpstate = tcp->tcp_state; 19603 if (mp == NULL) { 19604 /* 19605 * tcp_wput_data() with NULL mp should only be called when 19606 * there is unsent data. 19607 */ 19608 ASSERT(tcp->tcp_unsent > 0); 19609 /* Really tacky... but we need this for detached closes. */ 19610 len = tcp->tcp_unsent; 19611 goto data_null; 19612 } 19613 19614 #if CCS_STATS 19615 wrw_stats.tot.count++; 19616 wrw_stats.tot.bytes += msgdsize(mp); 19617 #endif 19618 ASSERT(mp->b_datap->db_type == M_DATA); 19619 /* 19620 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19621 * or before a connection attempt has begun. 19622 */ 19623 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19624 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19625 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19626 #ifdef DEBUG 19627 cmn_err(CE_WARN, 19628 "tcp_wput_data: data after ordrel, %s", 19629 tcp_display(tcp, NULL, 19630 DISP_ADDR_AND_PORT)); 19631 #else 19632 if (tcp->tcp_debug) { 19633 (void) strlog(TCP_MOD_ID, 0, 1, 19634 SL_TRACE|SL_ERROR, 19635 "tcp_wput_data: data after ordrel, %s\n", 19636 tcp_display(tcp, NULL, 19637 DISP_ADDR_AND_PORT)); 19638 } 19639 #endif /* DEBUG */ 19640 } 19641 if (tcp->tcp_snd_zcopy_aware && 19642 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19643 tcp_zcopy_notify(tcp); 19644 freemsg(mp); 19645 mutex_enter(&tcp->tcp_non_sq_lock); 19646 if (tcp->tcp_flow_stopped && 19647 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19648 tcp_clrqfull(tcp); 19649 } 19650 mutex_exit(&tcp->tcp_non_sq_lock); 19651 return; 19652 } 19653 19654 /* Strip empties */ 19655 for (;;) { 19656 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19657 (uintptr_t)INT_MAX); 19658 len = (int)(mp->b_wptr - mp->b_rptr); 19659 if (len > 0) 19660 break; 19661 mp1 = mp; 19662 mp = mp->b_cont; 19663 freeb(mp1); 19664 if (!mp) { 19665 return; 19666 } 19667 } 19668 19669 /* If we are the first on the list ... */ 19670 if (tcp->tcp_xmit_head == NULL) { 19671 tcp->tcp_xmit_head = mp; 19672 tcp->tcp_xmit_tail = mp; 19673 tcp->tcp_xmit_tail_unsent = len; 19674 } else { 19675 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19676 struct datab *dp; 19677 19678 mp1 = tcp->tcp_xmit_last; 19679 if (len < tcp_tx_pull_len && 19680 (dp = mp1->b_datap)->db_ref == 1 && 19681 dp->db_lim - mp1->b_wptr >= len) { 19682 ASSERT(len > 0); 19683 ASSERT(!mp1->b_cont); 19684 if (len == 1) { 19685 *mp1->b_wptr++ = *mp->b_rptr; 19686 } else { 19687 bcopy(mp->b_rptr, mp1->b_wptr, len); 19688 mp1->b_wptr += len; 19689 } 19690 if (mp1 == tcp->tcp_xmit_tail) 19691 tcp->tcp_xmit_tail_unsent += len; 19692 mp1->b_cont = mp->b_cont; 19693 if (tcp->tcp_snd_zcopy_aware && 19694 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19695 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19696 freeb(mp); 19697 mp = mp1; 19698 } else { 19699 tcp->tcp_xmit_last->b_cont = mp; 19700 } 19701 len += tcp->tcp_unsent; 19702 } 19703 19704 /* Tack on however many more positive length mblks we have */ 19705 if ((mp1 = mp->b_cont) != NULL) { 19706 do { 19707 int tlen; 19708 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19709 (uintptr_t)INT_MAX); 19710 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19711 if (tlen <= 0) { 19712 mp->b_cont = mp1->b_cont; 19713 freeb(mp1); 19714 } else { 19715 len += tlen; 19716 mp = mp1; 19717 } 19718 } while ((mp1 = mp->b_cont) != NULL); 19719 } 19720 tcp->tcp_xmit_last = mp; 19721 tcp->tcp_unsent = len; 19722 19723 if (urgent) 19724 usable = 1; 19725 19726 data_null: 19727 snxt = tcp->tcp_snxt; 19728 xmit_tail = tcp->tcp_xmit_tail; 19729 tail_unsent = tcp->tcp_xmit_tail_unsent; 19730 19731 /* 19732 * Note that tcp_mss has been adjusted to take into account the 19733 * timestamp option if applicable. Because SACK options do not 19734 * appear in every TCP segments and they are of variable lengths, 19735 * they cannot be included in tcp_mss. Thus we need to calculate 19736 * the actual segment length when we need to send a segment which 19737 * includes SACK options. 19738 */ 19739 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19740 int32_t opt_len; 19741 19742 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19743 tcp->tcp_num_sack_blk); 19744 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19745 2 + TCPOPT_HEADER_LEN; 19746 mss = tcp->tcp_mss - opt_len; 19747 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19748 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19749 } else { 19750 mss = tcp->tcp_mss; 19751 tcp_hdr_len = tcp->tcp_hdr_len; 19752 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19753 } 19754 19755 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19756 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19757 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19758 } 19759 if (tcpstate == TCPS_SYN_RCVD) { 19760 /* 19761 * The three-way connection establishment handshake is not 19762 * complete yet. We want to queue the data for transmission 19763 * after entering ESTABLISHED state (RFC793). A jump to 19764 * "done" label effectively leaves data on the queue. 19765 */ 19766 goto done; 19767 } else { 19768 int usable_r; 19769 19770 /* 19771 * In the special case when cwnd is zero, which can only 19772 * happen if the connection is ECN capable, return now. 19773 * New segments is sent using tcp_timer(). The timer 19774 * is set in tcp_rput_data(). 19775 */ 19776 if (tcp->tcp_cwnd == 0) { 19777 /* 19778 * Note that tcp_cwnd is 0 before 3-way handshake is 19779 * finished. 19780 */ 19781 ASSERT(tcp->tcp_ecn_ok || 19782 tcp->tcp_state < TCPS_ESTABLISHED); 19783 return; 19784 } 19785 19786 /* NOTE: trouble if xmitting while SYN not acked? */ 19787 usable_r = snxt - tcp->tcp_suna; 19788 usable_r = tcp->tcp_swnd - usable_r; 19789 19790 /* 19791 * Check if the receiver has shrunk the window. If 19792 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19793 * cannot be set as there is unsent data, so FIN cannot 19794 * be sent out. Otherwise, we need to take into account 19795 * of FIN as it consumes an "invisible" sequence number. 19796 */ 19797 ASSERT(tcp->tcp_fin_sent == 0); 19798 if (usable_r < 0) { 19799 /* 19800 * The receiver has shrunk the window and we have sent 19801 * -usable_r date beyond the window, re-adjust. 19802 * 19803 * If TCP window scaling is enabled, there can be 19804 * round down error as the advertised receive window 19805 * is actually right shifted n bits. This means that 19806 * the lower n bits info is wiped out. It will look 19807 * like the window is shrunk. Do a check here to 19808 * see if the shrunk amount is actually within the 19809 * error in window calculation. If it is, just 19810 * return. Note that this check is inside the 19811 * shrunk window check. This makes sure that even 19812 * though tcp_process_shrunk_swnd() is not called, 19813 * we will stop further processing. 19814 */ 19815 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19816 tcp_process_shrunk_swnd(tcp, -usable_r); 19817 } 19818 return; 19819 } 19820 19821 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19822 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19823 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19824 19825 /* usable = MIN(usable, unsent) */ 19826 if (usable_r > len) 19827 usable_r = len; 19828 19829 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19830 if (usable_r > 0) { 19831 usable = usable_r; 19832 } else { 19833 /* Bypass all other unnecessary processing. */ 19834 goto done; 19835 } 19836 } 19837 19838 local_time = (mblk_t *)lbolt; 19839 19840 /* 19841 * "Our" Nagle Algorithm. This is not the same as in the old 19842 * BSD. This is more in line with the true intent of Nagle. 19843 * 19844 * The conditions are: 19845 * 1. The amount of unsent data (or amount of data which can be 19846 * sent, whichever is smaller) is less than Nagle limit. 19847 * 2. The last sent size is also less than Nagle limit. 19848 * 3. There is unack'ed data. 19849 * 4. Urgent pointer is not set. Send urgent data ignoring the 19850 * Nagle algorithm. This reduces the probability that urgent 19851 * bytes get "merged" together. 19852 * 5. The app has not closed the connection. This eliminates the 19853 * wait time of the receiving side waiting for the last piece of 19854 * (small) data. 19855 * 19856 * If all are satisified, exit without sending anything. Note 19857 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19858 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19859 * 4095). 19860 */ 19861 if (usable < (int)tcp->tcp_naglim && 19862 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19863 snxt != tcp->tcp_suna && 19864 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19865 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19866 goto done; 19867 } 19868 19869 if (tcp->tcp_cork) { 19870 /* 19871 * if the tcp->tcp_cork option is set, then we have to force 19872 * TCP not to send partial segment (smaller than MSS bytes). 19873 * We are calculating the usable now based on full mss and 19874 * will save the rest of remaining data for later. 19875 */ 19876 if (usable < mss) 19877 goto done; 19878 usable = (usable / mss) * mss; 19879 } 19880 19881 /* Update the latest receive window size in TCP header. */ 19882 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19883 tcp->tcp_tcph->th_win); 19884 19885 /* 19886 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19887 * 19888 * 1. Simple TCP/IP{v4,v6} (no options). 19889 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19890 * 3. If the TCP connection is in ESTABLISHED state. 19891 * 4. The TCP is not detached. 19892 * 19893 * If any of the above conditions have changed during the 19894 * connection, stop using LSO/MDT and restore the stream head 19895 * parameters accordingly. 19896 */ 19897 ipst = tcps->tcps_netstack->netstack_ip; 19898 19899 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19900 ((tcp->tcp_ipversion == IPV4_VERSION && 19901 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19902 (tcp->tcp_ipversion == IPV6_VERSION && 19903 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19904 tcp->tcp_state != TCPS_ESTABLISHED || 19905 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19906 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19907 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19908 if (tcp->tcp_lso) { 19909 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19910 tcp->tcp_lso = B_FALSE; 19911 } else { 19912 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19913 tcp->tcp_mdt = B_FALSE; 19914 } 19915 19916 /* Anything other than detached is considered pathological */ 19917 if (!TCP_IS_DETACHED(tcp)) { 19918 if (tcp->tcp_lso) 19919 TCP_STAT(tcps, tcp_lso_disabled); 19920 else 19921 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19922 (void) tcp_maxpsz_set(tcp, B_TRUE); 19923 } 19924 } 19925 19926 /* Use MDT if sendable amount is greater than the threshold */ 19927 if (tcp->tcp_mdt && 19928 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19929 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19930 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19931 (tcp->tcp_valid_bits == 0 || 19932 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19933 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19934 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19935 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19936 local_time, mdt_thres); 19937 } else { 19938 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19939 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19940 local_time, INT_MAX); 19941 } 19942 19943 /* Pretend that all we were trying to send really got sent */ 19944 if (rc < 0 && tail_unsent < 0) { 19945 do { 19946 xmit_tail = xmit_tail->b_cont; 19947 xmit_tail->b_prev = local_time; 19948 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19949 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19950 tail_unsent += (int)(xmit_tail->b_wptr - 19951 xmit_tail->b_rptr); 19952 } while (tail_unsent < 0); 19953 } 19954 done:; 19955 tcp->tcp_xmit_tail = xmit_tail; 19956 tcp->tcp_xmit_tail_unsent = tail_unsent; 19957 len = tcp->tcp_snxt - snxt; 19958 if (len) { 19959 /* 19960 * If new data was sent, need to update the notsack 19961 * list, which is, afterall, data blocks that have 19962 * not been sack'ed by the receiver. New data is 19963 * not sack'ed. 19964 */ 19965 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19966 /* len is a negative value. */ 19967 tcp->tcp_pipe -= len; 19968 tcp_notsack_update(&(tcp->tcp_notsack_list), 19969 tcp->tcp_snxt, snxt, 19970 &(tcp->tcp_num_notsack_blk), 19971 &(tcp->tcp_cnt_notsack_list)); 19972 } 19973 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19974 tcp->tcp_rack = tcp->tcp_rnxt; 19975 tcp->tcp_rack_cnt = 0; 19976 if ((snxt + len) == tcp->tcp_suna) { 19977 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19978 } 19979 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19980 /* 19981 * Didn't send anything. Make sure the timer is running 19982 * so that we will probe a zero window. 19983 */ 19984 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19985 } 19986 /* Note that len is the amount we just sent but with a negative sign */ 19987 tcp->tcp_unsent += len; 19988 mutex_enter(&tcp->tcp_non_sq_lock); 19989 if (tcp->tcp_flow_stopped) { 19990 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19991 tcp_clrqfull(tcp); 19992 } 19993 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19994 tcp_setqfull(tcp); 19995 } 19996 mutex_exit(&tcp->tcp_non_sq_lock); 19997 } 19998 19999 /* 20000 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 20001 * outgoing TCP header with the template header, as well as other 20002 * options such as time-stamp, ECN and/or SACK. 20003 */ 20004 static void 20005 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 20006 { 20007 tcph_t *tcp_tmpl, *tcp_h; 20008 uint32_t *dst, *src; 20009 int hdrlen; 20010 20011 ASSERT(OK_32PTR(rptr)); 20012 20013 /* Template header */ 20014 tcp_tmpl = tcp->tcp_tcph; 20015 20016 /* Header of outgoing packet */ 20017 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 20018 20019 /* dst and src are opaque 32-bit fields, used for copying */ 20020 dst = (uint32_t *)rptr; 20021 src = (uint32_t *)tcp->tcp_iphc; 20022 hdrlen = tcp->tcp_hdr_len; 20023 20024 /* Fill time-stamp option if needed */ 20025 if (tcp->tcp_snd_ts_ok) { 20026 U32_TO_BE32((uint32_t)now, 20027 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 20028 U32_TO_BE32(tcp->tcp_ts_recent, 20029 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 20030 } else { 20031 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 20032 } 20033 20034 /* 20035 * Copy the template header; is this really more efficient than 20036 * calling bcopy()? For simple IPv4/TCP, it may be the case, 20037 * but perhaps not for other scenarios. 20038 */ 20039 dst[0] = src[0]; 20040 dst[1] = src[1]; 20041 dst[2] = src[2]; 20042 dst[3] = src[3]; 20043 dst[4] = src[4]; 20044 dst[5] = src[5]; 20045 dst[6] = src[6]; 20046 dst[7] = src[7]; 20047 dst[8] = src[8]; 20048 dst[9] = src[9]; 20049 if (hdrlen -= 40) { 20050 hdrlen >>= 2; 20051 dst += 10; 20052 src += 10; 20053 do { 20054 *dst++ = *src++; 20055 } while (--hdrlen); 20056 } 20057 20058 /* 20059 * Set the ECN info in the TCP header if it is not a zero 20060 * window probe. Zero window probe is only sent in 20061 * tcp_wput_data() and tcp_timer(). 20062 */ 20063 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 20064 SET_ECT(tcp, rptr); 20065 20066 if (tcp->tcp_ecn_echo_on) 20067 tcp_h->th_flags[0] |= TH_ECE; 20068 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 20069 tcp_h->th_flags[0] |= TH_CWR; 20070 tcp->tcp_ecn_cwr_sent = B_TRUE; 20071 } 20072 } 20073 20074 /* Fill in SACK options */ 20075 if (num_sack_blk > 0) { 20076 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 20077 sack_blk_t *tmp; 20078 int32_t i; 20079 20080 wptr[0] = TCPOPT_NOP; 20081 wptr[1] = TCPOPT_NOP; 20082 wptr[2] = TCPOPT_SACK; 20083 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 20084 sizeof (sack_blk_t); 20085 wptr += TCPOPT_REAL_SACK_LEN; 20086 20087 tmp = tcp->tcp_sack_list; 20088 for (i = 0; i < num_sack_blk; i++) { 20089 U32_TO_BE32(tmp[i].begin, wptr); 20090 wptr += sizeof (tcp_seq); 20091 U32_TO_BE32(tmp[i].end, wptr); 20092 wptr += sizeof (tcp_seq); 20093 } 20094 tcp_h->th_offset_and_rsrvd[0] += 20095 ((num_sack_blk * 2 + 1) << 4); 20096 } 20097 } 20098 20099 /* 20100 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 20101 * the destination address and SAP attribute, and if necessary, the 20102 * hardware checksum offload attribute to a Multidata message. 20103 */ 20104 static int 20105 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 20106 const uint32_t start, const uint32_t stuff, const uint32_t end, 20107 const uint32_t flags, tcp_stack_t *tcps) 20108 { 20109 /* Add global destination address & SAP attribute */ 20110 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 20111 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 20112 "destination address+SAP\n")); 20113 20114 if (dlmp != NULL) 20115 TCP_STAT(tcps, tcp_mdt_allocfail); 20116 return (-1); 20117 } 20118 20119 /* Add global hwcksum attribute */ 20120 if (hwcksum && 20121 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 20122 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 20123 "checksum attribute\n")); 20124 20125 TCP_STAT(tcps, tcp_mdt_allocfail); 20126 return (-1); 20127 } 20128 20129 return (0); 20130 } 20131 20132 /* 20133 * Smaller and private version of pdescinfo_t used specifically for TCP, 20134 * which allows for only two payload spans per packet. 20135 */ 20136 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 20137 20138 /* 20139 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 20140 * scheme, and returns one the following: 20141 * 20142 * -1 = failed allocation. 20143 * 0 = success; burst count reached, or usable send window is too small, 20144 * and that we'd rather wait until later before sending again. 20145 */ 20146 static int 20147 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20148 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20149 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20150 const int mdt_thres) 20151 { 20152 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 20153 multidata_t *mmd; 20154 uint_t obsegs, obbytes, hdr_frag_sz; 20155 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 20156 int num_burst_seg, max_pld; 20157 pdesc_t *pkt; 20158 tcp_pdescinfo_t tcp_pkt_info; 20159 pdescinfo_t *pkt_info; 20160 int pbuf_idx, pbuf_idx_nxt; 20161 int seg_len, len, spill, af; 20162 boolean_t add_buffer, zcopy, clusterwide; 20163 boolean_t rconfirm = B_FALSE; 20164 boolean_t done = B_FALSE; 20165 uint32_t cksum; 20166 uint32_t hwcksum_flags; 20167 ire_t *ire = NULL; 20168 ill_t *ill; 20169 ipha_t *ipha; 20170 ip6_t *ip6h; 20171 ipaddr_t src, dst; 20172 ill_zerocopy_capab_t *zc_cap = NULL; 20173 uint16_t *up; 20174 int err; 20175 conn_t *connp; 20176 tcp_stack_t *tcps = tcp->tcp_tcps; 20177 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20178 int usable_mmd, tail_unsent_mmd; 20179 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 20180 mblk_t *xmit_tail_mmd; 20181 20182 #ifdef _BIG_ENDIAN 20183 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 20184 #else 20185 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 20186 #endif 20187 20188 #define PREP_NEW_MULTIDATA() { \ 20189 mmd = NULL; \ 20190 md_mp = md_hbuf = NULL; \ 20191 cur_hdr_off = 0; \ 20192 max_pld = tcp->tcp_mdt_max_pld; \ 20193 pbuf_idx = pbuf_idx_nxt = -1; \ 20194 add_buffer = B_TRUE; \ 20195 zcopy = B_FALSE; \ 20196 } 20197 20198 #define PREP_NEW_PBUF() { \ 20199 md_pbuf = md_pbuf_nxt = NULL; \ 20200 pbuf_idx = pbuf_idx_nxt = -1; \ 20201 cur_pld_off = 0; \ 20202 first_snxt = *snxt; \ 20203 ASSERT(*tail_unsent > 0); \ 20204 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 20205 } 20206 20207 ASSERT(mdt_thres >= mss); 20208 ASSERT(*usable > 0 && *usable > mdt_thres); 20209 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20210 ASSERT(!TCP_IS_DETACHED(tcp)); 20211 ASSERT(tcp->tcp_valid_bits == 0 || 20212 tcp->tcp_valid_bits == TCP_FSS_VALID); 20213 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 20214 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 20215 (tcp->tcp_ipversion == IPV6_VERSION && 20216 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 20217 20218 connp = tcp->tcp_connp; 20219 ASSERT(connp != NULL); 20220 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 20221 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 20222 20223 usable_mmd = tail_unsent_mmd = 0; 20224 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 20225 xmit_tail_mmd = NULL; 20226 /* 20227 * Note that tcp will only declare at most 2 payload spans per 20228 * packet, which is much lower than the maximum allowable number 20229 * of packet spans per Multidata. For this reason, we use the 20230 * privately declared and smaller descriptor info structure, in 20231 * order to save some stack space. 20232 */ 20233 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 20234 20235 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 20236 if (af == AF_INET) { 20237 dst = tcp->tcp_ipha->ipha_dst; 20238 src = tcp->tcp_ipha->ipha_src; 20239 ASSERT(!CLASSD(dst)); 20240 } 20241 ASSERT(af == AF_INET || 20242 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 20243 20244 obsegs = obbytes = 0; 20245 num_burst_seg = tcp->tcp_snd_burst; 20246 md_mp_head = NULL; 20247 PREP_NEW_MULTIDATA(); 20248 20249 /* 20250 * Before we go on further, make sure there is an IRE that we can 20251 * use, and that the ILL supports MDT. Otherwise, there's no point 20252 * in proceeding any further, and we should just hand everything 20253 * off to the legacy path. 20254 */ 20255 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 20256 goto legacy_send_no_md; 20257 20258 ASSERT(ire != NULL); 20259 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 20260 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 20261 ASSERT(af == AF_INET || ire->ire_nce != NULL); 20262 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 20263 /* 20264 * If we do support loopback for MDT (which requires modifications 20265 * to the receiving paths), the following assertions should go away, 20266 * and we would be sending the Multidata to loopback conn later on. 20267 */ 20268 ASSERT(!IRE_IS_LOCAL(ire)); 20269 ASSERT(ire->ire_stq != NULL); 20270 20271 ill = ire_to_ill(ire); 20272 ASSERT(ill != NULL); 20273 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 20274 20275 if (!tcp->tcp_ire_ill_check_done) { 20276 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 20277 tcp->tcp_ire_ill_check_done = B_TRUE; 20278 } 20279 20280 /* 20281 * If the underlying interface conditions have changed, or if the 20282 * new interface does not support MDT, go back to legacy path. 20283 */ 20284 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 20285 /* don't go through this path anymore for this connection */ 20286 TCP_STAT(tcps, tcp_mdt_conn_halted2); 20287 tcp->tcp_mdt = B_FALSE; 20288 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 20289 "interface %s\n", (void *)connp, ill->ill_name)); 20290 /* IRE will be released prior to returning */ 20291 goto legacy_send_no_md; 20292 } 20293 20294 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 20295 zc_cap = ill->ill_zerocopy_capab; 20296 20297 /* 20298 * Check if we can take tcp fast-path. Note that "incomplete" 20299 * ire's (where the link-layer for next hop is not resolved 20300 * or where the fast-path header in nce_fp_mp is not available 20301 * yet) are sent down the legacy (slow) path. 20302 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 20303 */ 20304 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 20305 /* IRE will be released prior to returning */ 20306 goto legacy_send_no_md; 20307 } 20308 20309 /* go to legacy path if interface doesn't support zerocopy */ 20310 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 20311 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 20312 /* IRE will be released prior to returning */ 20313 goto legacy_send_no_md; 20314 } 20315 20316 /* does the interface support hardware checksum offload? */ 20317 hwcksum_flags = 0; 20318 if (ILL_HCKSUM_CAPABLE(ill) && 20319 (ill->ill_hcksum_capab->ill_hcksum_txflags & 20320 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 20321 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 20322 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20323 HCKSUM_IPHDRCKSUM) 20324 hwcksum_flags = HCK_IPV4_HDRCKSUM; 20325 20326 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20327 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 20328 hwcksum_flags |= HCK_FULLCKSUM; 20329 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20330 HCKSUM_INET_PARTIAL) 20331 hwcksum_flags |= HCK_PARTIALCKSUM; 20332 } 20333 20334 /* 20335 * Each header fragment consists of the leading extra space, 20336 * followed by the TCP/IP header, and the trailing extra space. 20337 * We make sure that each header fragment begins on a 32-bit 20338 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 20339 * aligned in tcp_mdt_update). 20340 */ 20341 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 20342 tcp->tcp_mdt_hdr_tail), 4); 20343 20344 /* are we starting from the beginning of data block? */ 20345 if (*tail_unsent == 0) { 20346 *xmit_tail = (*xmit_tail)->b_cont; 20347 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 20348 *tail_unsent = (int)MBLKL(*xmit_tail); 20349 } 20350 20351 /* 20352 * Here we create one or more Multidata messages, each made up of 20353 * one header buffer and up to N payload buffers. This entire 20354 * operation is done within two loops: 20355 * 20356 * The outer loop mostly deals with creating the Multidata message, 20357 * as well as the header buffer that gets added to it. It also 20358 * links the Multidata messages together such that all of them can 20359 * be sent down to the lower layer in a single putnext call; this 20360 * linking behavior depends on the tcp_mdt_chain tunable. 20361 * 20362 * The inner loop takes an existing Multidata message, and adds 20363 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 20364 * packetizes those buffers by filling up the corresponding header 20365 * buffer fragments with the proper IP and TCP headers, and by 20366 * describing the layout of each packet in the packet descriptors 20367 * that get added to the Multidata. 20368 */ 20369 do { 20370 /* 20371 * If usable send window is too small, or data blocks in 20372 * transmit list are smaller than our threshold (i.e. app 20373 * performs large writes followed by small ones), we hand 20374 * off the control over to the legacy path. Note that we'll 20375 * get back the control once it encounters a large block. 20376 */ 20377 if (*usable < mss || (*tail_unsent <= mdt_thres && 20378 (*xmit_tail)->b_cont != NULL && 20379 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 20380 /* send down what we've got so far */ 20381 if (md_mp_head != NULL) { 20382 tcp_multisend_data(tcp, ire, ill, md_mp_head, 20383 obsegs, obbytes, &rconfirm); 20384 } 20385 /* 20386 * Pass control over to tcp_send(), but tell it to 20387 * return to us once a large-size transmission is 20388 * possible. 20389 */ 20390 TCP_STAT(tcps, tcp_mdt_legacy_small); 20391 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 20392 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 20393 tail_unsent, xmit_tail, local_time, 20394 mdt_thres)) <= 0) { 20395 /* burst count reached, or alloc failed */ 20396 IRE_REFRELE(ire); 20397 return (err); 20398 } 20399 20400 /* tcp_send() may have sent everything, so check */ 20401 if (*usable <= 0) { 20402 IRE_REFRELE(ire); 20403 return (0); 20404 } 20405 20406 TCP_STAT(tcps, tcp_mdt_legacy_ret); 20407 /* 20408 * We may have delivered the Multidata, so make sure 20409 * to re-initialize before the next round. 20410 */ 20411 md_mp_head = NULL; 20412 obsegs = obbytes = 0; 20413 num_burst_seg = tcp->tcp_snd_burst; 20414 PREP_NEW_MULTIDATA(); 20415 20416 /* are we starting from the beginning of data block? */ 20417 if (*tail_unsent == 0) { 20418 *xmit_tail = (*xmit_tail)->b_cont; 20419 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20420 (uintptr_t)INT_MAX); 20421 *tail_unsent = (int)MBLKL(*xmit_tail); 20422 } 20423 } 20424 /* 20425 * Record current values for parameters we may need to pass 20426 * to tcp_send() or tcp_multisend_data(). We checkpoint at 20427 * each iteration of the outer loop (each multidata message 20428 * creation). If we have a failure in the inner loop, we send 20429 * any complete multidata messages we have before reverting 20430 * to using the traditional non-md path. 20431 */ 20432 snxt_mmd = *snxt; 20433 usable_mmd = *usable; 20434 xmit_tail_mmd = *xmit_tail; 20435 tail_unsent_mmd = *tail_unsent; 20436 obsegs_mmd = obsegs; 20437 obbytes_mmd = obbytes; 20438 20439 /* 20440 * max_pld limits the number of mblks in tcp's transmit 20441 * queue that can be added to a Multidata message. Once 20442 * this counter reaches zero, no more additional mblks 20443 * can be added to it. What happens afterwards depends 20444 * on whether or not we are set to chain the Multidata 20445 * messages. If we are to link them together, reset 20446 * max_pld to its original value (tcp_mdt_max_pld) and 20447 * prepare to create a new Multidata message which will 20448 * get linked to md_mp_head. Else, leave it alone and 20449 * let the inner loop break on its own. 20450 */ 20451 if (tcp_mdt_chain && max_pld == 0) 20452 PREP_NEW_MULTIDATA(); 20453 20454 /* adding a payload buffer; re-initialize values */ 20455 if (add_buffer) 20456 PREP_NEW_PBUF(); 20457 20458 /* 20459 * If we don't have a Multidata, either because we just 20460 * (re)entered this outer loop, or after we branched off 20461 * to tcp_send above, setup the Multidata and header 20462 * buffer to be used. 20463 */ 20464 if (md_mp == NULL) { 20465 int md_hbuflen; 20466 uint32_t start, stuff; 20467 20468 /* 20469 * Calculate Multidata header buffer size large enough 20470 * to hold all of the headers that can possibly be 20471 * sent at this moment. We'd rather over-estimate 20472 * the size than running out of space; this is okay 20473 * since this buffer is small anyway. 20474 */ 20475 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 20476 20477 /* 20478 * Start and stuff offset for partial hardware 20479 * checksum offload; these are currently for IPv4. 20480 * For full checksum offload, they are set to zero. 20481 */ 20482 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 20483 if (af == AF_INET) { 20484 start = IP_SIMPLE_HDR_LENGTH; 20485 stuff = IP_SIMPLE_HDR_LENGTH + 20486 TCP_CHECKSUM_OFFSET; 20487 } else { 20488 start = IPV6_HDR_LEN; 20489 stuff = IPV6_HDR_LEN + 20490 TCP_CHECKSUM_OFFSET; 20491 } 20492 } else { 20493 start = stuff = 0; 20494 } 20495 20496 /* 20497 * Create the header buffer, Multidata, as well as 20498 * any necessary attributes (destination address, 20499 * SAP and hardware checksum offload) that should 20500 * be associated with the Multidata message. 20501 */ 20502 ASSERT(cur_hdr_off == 0); 20503 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 20504 ((md_hbuf->b_wptr += md_hbuflen), 20505 (mmd = mmd_alloc(md_hbuf, &md_mp, 20506 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 20507 /* fastpath mblk */ 20508 ire->ire_nce->nce_res_mp, 20509 /* hardware checksum enabled */ 20510 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 20511 /* hardware checksum offsets */ 20512 start, stuff, 0, 20513 /* hardware checksum flag */ 20514 hwcksum_flags, tcps) != 0)) { 20515 legacy_send: 20516 /* 20517 * We arrive here from a failure within the 20518 * inner (packetizer) loop or we fail one of 20519 * the conditionals above. We restore the 20520 * previously checkpointed values for: 20521 * xmit_tail 20522 * usable 20523 * tail_unsent 20524 * snxt 20525 * obbytes 20526 * obsegs 20527 * We should then be able to dispatch any 20528 * complete multidata before reverting to the 20529 * traditional path with consistent parameters 20530 * (the inner loop updates these as it 20531 * iterates). 20532 */ 20533 *xmit_tail = xmit_tail_mmd; 20534 *usable = usable_mmd; 20535 *tail_unsent = tail_unsent_mmd; 20536 *snxt = snxt_mmd; 20537 obbytes = obbytes_mmd; 20538 obsegs = obsegs_mmd; 20539 if (md_mp != NULL) { 20540 /* Unlink message from the chain */ 20541 if (md_mp_head != NULL) { 20542 err = (intptr_t)rmvb(md_mp_head, 20543 md_mp); 20544 /* 20545 * We can't assert that rmvb 20546 * did not return -1, since we 20547 * may get here before linkb 20548 * happens. We do, however, 20549 * check if we just removed the 20550 * only element in the list. 20551 */ 20552 if (err == 0) 20553 md_mp_head = NULL; 20554 } 20555 /* md_hbuf gets freed automatically */ 20556 TCP_STAT(tcps, tcp_mdt_discarded); 20557 freeb(md_mp); 20558 } else { 20559 /* Either allocb or mmd_alloc failed */ 20560 TCP_STAT(tcps, tcp_mdt_allocfail); 20561 if (md_hbuf != NULL) 20562 freeb(md_hbuf); 20563 } 20564 20565 /* send down what we've got so far */ 20566 if (md_mp_head != NULL) { 20567 tcp_multisend_data(tcp, ire, ill, 20568 md_mp_head, obsegs, obbytes, 20569 &rconfirm); 20570 } 20571 legacy_send_no_md: 20572 if (ire != NULL) 20573 IRE_REFRELE(ire); 20574 /* 20575 * Too bad; let the legacy path handle this. 20576 * We specify INT_MAX for the threshold, since 20577 * we gave up with the Multidata processings 20578 * and let the old path have it all. 20579 */ 20580 TCP_STAT(tcps, tcp_mdt_legacy_all); 20581 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20582 tcp_tcp_hdr_len, num_sack_blk, usable, 20583 snxt, tail_unsent, xmit_tail, local_time, 20584 INT_MAX)); 20585 } 20586 20587 /* link to any existing ones, if applicable */ 20588 TCP_STAT(tcps, tcp_mdt_allocd); 20589 if (md_mp_head == NULL) { 20590 md_mp_head = md_mp; 20591 } else if (tcp_mdt_chain) { 20592 TCP_STAT(tcps, tcp_mdt_linked); 20593 linkb(md_mp_head, md_mp); 20594 } 20595 } 20596 20597 ASSERT(md_mp_head != NULL); 20598 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20599 ASSERT(md_mp != NULL && mmd != NULL); 20600 ASSERT(md_hbuf != NULL); 20601 20602 /* 20603 * Packetize the transmittable portion of the data block; 20604 * each data block is essentially added to the Multidata 20605 * as a payload buffer. We also deal with adding more 20606 * than one payload buffers, which happens when the remaining 20607 * packetized portion of the current payload buffer is less 20608 * than MSS, while the next data block in transmit queue 20609 * has enough data to make up for one. This "spillover" 20610 * case essentially creates a split-packet, where portions 20611 * of the packet's payload fragments may span across two 20612 * virtually discontiguous address blocks. 20613 */ 20614 seg_len = mss; 20615 do { 20616 len = seg_len; 20617 20618 /* one must remain NULL for DTRACE_IP_FASTPATH */ 20619 ipha = NULL; 20620 ip6h = NULL; 20621 20622 ASSERT(len > 0); 20623 ASSERT(max_pld >= 0); 20624 ASSERT(!add_buffer || cur_pld_off == 0); 20625 20626 /* 20627 * First time around for this payload buffer; note 20628 * in the case of a spillover, the following has 20629 * been done prior to adding the split-packet 20630 * descriptor to Multidata, and we don't want to 20631 * repeat the process. 20632 */ 20633 if (add_buffer) { 20634 ASSERT(mmd != NULL); 20635 ASSERT(md_pbuf == NULL); 20636 ASSERT(md_pbuf_nxt == NULL); 20637 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20638 20639 /* 20640 * Have we reached the limit? We'd get to 20641 * this case when we're not chaining the 20642 * Multidata messages together, and since 20643 * we're done, terminate this loop. 20644 */ 20645 if (max_pld == 0) 20646 break; /* done */ 20647 20648 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20649 TCP_STAT(tcps, tcp_mdt_allocfail); 20650 goto legacy_send; /* out_of_mem */ 20651 } 20652 20653 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20654 zc_cap != NULL) { 20655 if (!ip_md_zcopy_attr(mmd, NULL, 20656 zc_cap->ill_zerocopy_flags)) { 20657 freeb(md_pbuf); 20658 TCP_STAT(tcps, 20659 tcp_mdt_allocfail); 20660 /* out_of_mem */ 20661 goto legacy_send; 20662 } 20663 zcopy = B_TRUE; 20664 } 20665 20666 md_pbuf->b_rptr += base_pld_off; 20667 20668 /* 20669 * Add a payload buffer to the Multidata; this 20670 * operation must not fail, or otherwise our 20671 * logic in this routine is broken. There 20672 * is no memory allocation done by the 20673 * routine, so any returned failure simply 20674 * tells us that we've done something wrong. 20675 * 20676 * A failure tells us that either we're adding 20677 * the same payload buffer more than once, or 20678 * we're trying to add more buffers than 20679 * allowed (max_pld calculation is wrong). 20680 * None of the above cases should happen, and 20681 * we panic because either there's horrible 20682 * heap corruption, and/or programming mistake. 20683 */ 20684 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20685 if (pbuf_idx < 0) { 20686 cmn_err(CE_PANIC, "tcp_multisend: " 20687 "payload buffer logic error " 20688 "detected for tcp %p mmd %p " 20689 "pbuf %p (%d)\n", 20690 (void *)tcp, (void *)mmd, 20691 (void *)md_pbuf, pbuf_idx); 20692 } 20693 20694 ASSERT(max_pld > 0); 20695 --max_pld; 20696 add_buffer = B_FALSE; 20697 } 20698 20699 ASSERT(md_mp_head != NULL); 20700 ASSERT(md_pbuf != NULL); 20701 ASSERT(md_pbuf_nxt == NULL); 20702 ASSERT(pbuf_idx != -1); 20703 ASSERT(pbuf_idx_nxt == -1); 20704 ASSERT(*usable > 0); 20705 20706 /* 20707 * We spillover to the next payload buffer only 20708 * if all of the following is true: 20709 * 20710 * 1. There is not enough data on the current 20711 * payload buffer to make up `len', 20712 * 2. We are allowed to send `len', 20713 * 3. The next payload buffer length is large 20714 * enough to accomodate `spill'. 20715 */ 20716 if ((spill = len - *tail_unsent) > 0 && 20717 *usable >= len && 20718 MBLKL((*xmit_tail)->b_cont) >= spill && 20719 max_pld > 0) { 20720 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20721 if (md_pbuf_nxt == NULL) { 20722 TCP_STAT(tcps, tcp_mdt_allocfail); 20723 goto legacy_send; /* out_of_mem */ 20724 } 20725 20726 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20727 zc_cap != NULL) { 20728 if (!ip_md_zcopy_attr(mmd, NULL, 20729 zc_cap->ill_zerocopy_flags)) { 20730 freeb(md_pbuf_nxt); 20731 TCP_STAT(tcps, 20732 tcp_mdt_allocfail); 20733 /* out_of_mem */ 20734 goto legacy_send; 20735 } 20736 zcopy = B_TRUE; 20737 } 20738 20739 /* 20740 * See comments above on the first call to 20741 * mmd_addpldbuf for explanation on the panic. 20742 */ 20743 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20744 if (pbuf_idx_nxt < 0) { 20745 panic("tcp_multisend: " 20746 "next payload buffer logic error " 20747 "detected for tcp %p mmd %p " 20748 "pbuf %p (%d)\n", 20749 (void *)tcp, (void *)mmd, 20750 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20751 } 20752 20753 ASSERT(max_pld > 0); 20754 --max_pld; 20755 } else if (spill > 0) { 20756 /* 20757 * If there's a spillover, but the following 20758 * xmit_tail couldn't give us enough octets 20759 * to reach "len", then stop the current 20760 * Multidata creation and let the legacy 20761 * tcp_send() path take over. We don't want 20762 * to send the tiny segment as part of this 20763 * Multidata for performance reasons; instead, 20764 * we let the legacy path deal with grouping 20765 * it with the subsequent small mblks. 20766 */ 20767 if (*usable >= len && 20768 MBLKL((*xmit_tail)->b_cont) < spill) { 20769 max_pld = 0; 20770 break; /* done */ 20771 } 20772 20773 /* 20774 * We can't spillover, and we are near 20775 * the end of the current payload buffer, 20776 * so send what's left. 20777 */ 20778 ASSERT(*tail_unsent > 0); 20779 len = *tail_unsent; 20780 } 20781 20782 /* tail_unsent is negated if there is a spillover */ 20783 *tail_unsent -= len; 20784 *usable -= len; 20785 ASSERT(*usable >= 0); 20786 20787 if (*usable < mss) 20788 seg_len = *usable; 20789 /* 20790 * Sender SWS avoidance; see comments in tcp_send(); 20791 * everything else is the same, except that we only 20792 * do this here if there is no more data to be sent 20793 * following the current xmit_tail. We don't check 20794 * for 1-byte urgent data because we shouldn't get 20795 * here if TCP_URG_VALID is set. 20796 */ 20797 if (*usable > 0 && *usable < mss && 20798 ((md_pbuf_nxt == NULL && 20799 (*xmit_tail)->b_cont == NULL) || 20800 (md_pbuf_nxt != NULL && 20801 (*xmit_tail)->b_cont->b_cont == NULL)) && 20802 seg_len < (tcp->tcp_max_swnd >> 1) && 20803 (tcp->tcp_unsent - 20804 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20805 !tcp->tcp_zero_win_probe) { 20806 if ((*snxt + len) == tcp->tcp_snxt && 20807 (*snxt + len) == tcp->tcp_suna) { 20808 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20809 } 20810 done = B_TRUE; 20811 } 20812 20813 /* 20814 * Prime pump for IP's checksumming on our behalf; 20815 * include the adjustment for a source route if any. 20816 * Do this only for software/partial hardware checksum 20817 * offload, as this field gets zeroed out later for 20818 * the full hardware checksum offload case. 20819 */ 20820 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20821 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20822 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20823 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20824 } 20825 20826 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20827 *snxt += len; 20828 20829 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20830 /* 20831 * We set the PUSH bit only if TCP has no more buffered 20832 * data to be transmitted (or if sender SWS avoidance 20833 * takes place), as opposed to setting it for every 20834 * last packet in the burst. 20835 */ 20836 if (done || 20837 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20838 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20839 20840 /* 20841 * Set FIN bit if this is our last segment; snxt 20842 * already includes its length, and it will not 20843 * be adjusted after this point. 20844 */ 20845 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20846 *snxt == tcp->tcp_fss) { 20847 if (!tcp->tcp_fin_acked) { 20848 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20849 BUMP_MIB(&tcps->tcps_mib, 20850 tcpOutControl); 20851 } 20852 if (!tcp->tcp_fin_sent) { 20853 tcp->tcp_fin_sent = B_TRUE; 20854 /* 20855 * tcp state must be ESTABLISHED 20856 * in order for us to get here in 20857 * the first place. 20858 */ 20859 tcp->tcp_state = TCPS_FIN_WAIT_1; 20860 20861 /* 20862 * Upon returning from this routine, 20863 * tcp_wput_data() will set tcp_snxt 20864 * to be equal to snxt + tcp_fin_sent. 20865 * This is essentially the same as 20866 * setting it to tcp_fss + 1. 20867 */ 20868 } 20869 } 20870 20871 tcp->tcp_last_sent_len = (ushort_t)len; 20872 20873 len += tcp_hdr_len; 20874 if (tcp->tcp_ipversion == IPV4_VERSION) 20875 tcp->tcp_ipha->ipha_length = htons(len); 20876 else 20877 tcp->tcp_ip6h->ip6_plen = htons(len - 20878 ((char *)&tcp->tcp_ip6h[1] - 20879 tcp->tcp_iphc)); 20880 20881 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20882 20883 /* setup header fragment */ 20884 PDESC_HDR_ADD(pkt_info, 20885 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20886 tcp->tcp_mdt_hdr_head, /* head room */ 20887 tcp_hdr_len, /* len */ 20888 tcp->tcp_mdt_hdr_tail); /* tail room */ 20889 20890 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20891 hdr_frag_sz); 20892 ASSERT(MBLKIN(md_hbuf, 20893 (pkt_info->hdr_base - md_hbuf->b_rptr), 20894 PDESC_HDRSIZE(pkt_info))); 20895 20896 /* setup first payload fragment */ 20897 PDESC_PLD_INIT(pkt_info); 20898 PDESC_PLD_SPAN_ADD(pkt_info, 20899 pbuf_idx, /* index */ 20900 md_pbuf->b_rptr + cur_pld_off, /* start */ 20901 tcp->tcp_last_sent_len); /* len */ 20902 20903 /* create a split-packet in case of a spillover */ 20904 if (md_pbuf_nxt != NULL) { 20905 ASSERT(spill > 0); 20906 ASSERT(pbuf_idx_nxt > pbuf_idx); 20907 ASSERT(!add_buffer); 20908 20909 md_pbuf = md_pbuf_nxt; 20910 md_pbuf_nxt = NULL; 20911 pbuf_idx = pbuf_idx_nxt; 20912 pbuf_idx_nxt = -1; 20913 cur_pld_off = spill; 20914 20915 /* trim out first payload fragment */ 20916 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20917 20918 /* setup second payload fragment */ 20919 PDESC_PLD_SPAN_ADD(pkt_info, 20920 pbuf_idx, /* index */ 20921 md_pbuf->b_rptr, /* start */ 20922 spill); /* len */ 20923 20924 if ((*xmit_tail)->b_next == NULL) { 20925 /* 20926 * Store the lbolt used for RTT 20927 * estimation. We can only record one 20928 * timestamp per mblk so we do it when 20929 * we reach the end of the payload 20930 * buffer. Also we only take a new 20931 * timestamp sample when the previous 20932 * timed data from the same mblk has 20933 * been ack'ed. 20934 */ 20935 (*xmit_tail)->b_prev = local_time; 20936 (*xmit_tail)->b_next = 20937 (mblk_t *)(uintptr_t)first_snxt; 20938 } 20939 20940 first_snxt = *snxt - spill; 20941 20942 /* 20943 * Advance xmit_tail; usable could be 0 by 20944 * the time we got here, but we made sure 20945 * above that we would only spillover to 20946 * the next data block if usable includes 20947 * the spilled-over amount prior to the 20948 * subtraction. Therefore, we are sure 20949 * that xmit_tail->b_cont can't be NULL. 20950 */ 20951 ASSERT((*xmit_tail)->b_cont != NULL); 20952 *xmit_tail = (*xmit_tail)->b_cont; 20953 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20954 (uintptr_t)INT_MAX); 20955 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20956 } else { 20957 cur_pld_off += tcp->tcp_last_sent_len; 20958 } 20959 20960 /* 20961 * Fill in the header using the template header, and 20962 * add options such as time-stamp, ECN and/or SACK, 20963 * as needed. 20964 */ 20965 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20966 (clock_t)local_time, num_sack_blk); 20967 20968 /* take care of some IP header businesses */ 20969 if (af == AF_INET) { 20970 ipha = (ipha_t *)pkt_info->hdr_rptr; 20971 20972 ASSERT(OK_32PTR((uchar_t *)ipha)); 20973 ASSERT(PDESC_HDRL(pkt_info) >= 20974 IP_SIMPLE_HDR_LENGTH); 20975 ASSERT(ipha->ipha_version_and_hdr_length == 20976 IP_SIMPLE_HDR_VERSION); 20977 20978 /* 20979 * Assign ident value for current packet; see 20980 * related comments in ip_wput_ire() about the 20981 * contract private interface with clustering 20982 * group. 20983 */ 20984 clusterwide = B_FALSE; 20985 if (cl_inet_ipident != NULL) { 20986 ASSERT(cl_inet_isclusterwide != NULL); 20987 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 20988 AF_INET, 20989 (uint8_t *)(uintptr_t)src)) { 20990 ipha->ipha_ident = 20991 (*cl_inet_ipident) 20992 (IPPROTO_IP, AF_INET, 20993 (uint8_t *)(uintptr_t)src, 20994 (uint8_t *)(uintptr_t)dst); 20995 clusterwide = B_TRUE; 20996 } 20997 } 20998 20999 if (!clusterwide) { 21000 ipha->ipha_ident = (uint16_t) 21001 atomic_add_32_nv( 21002 &ire->ire_ident, 1); 21003 } 21004 #ifndef _BIG_ENDIAN 21005 ipha->ipha_ident = (ipha->ipha_ident << 8) | 21006 (ipha->ipha_ident >> 8); 21007 #endif 21008 } else { 21009 ip6h = (ip6_t *)pkt_info->hdr_rptr; 21010 21011 ASSERT(OK_32PTR((uchar_t *)ip6h)); 21012 ASSERT(IPVER(ip6h) == IPV6_VERSION); 21013 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 21014 ASSERT(PDESC_HDRL(pkt_info) >= 21015 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 21016 TCP_CHECKSUM_SIZE)); 21017 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21018 21019 if (tcp->tcp_ip_forward_progress) { 21020 rconfirm = B_TRUE; 21021 tcp->tcp_ip_forward_progress = B_FALSE; 21022 } 21023 } 21024 21025 /* at least one payload span, and at most two */ 21026 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 21027 21028 /* add the packet descriptor to Multidata */ 21029 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 21030 KM_NOSLEEP)) == NULL) { 21031 /* 21032 * Any failure other than ENOMEM indicates 21033 * that we have passed in invalid pkt_info 21034 * or parameters to mmd_addpdesc, which must 21035 * not happen. 21036 * 21037 * EINVAL is a result of failure on boundary 21038 * checks against the pkt_info contents. It 21039 * should not happen, and we panic because 21040 * either there's horrible heap corruption, 21041 * and/or programming mistake. 21042 */ 21043 if (err != ENOMEM) { 21044 cmn_err(CE_PANIC, "tcp_multisend: " 21045 "pdesc logic error detected for " 21046 "tcp %p mmd %p pinfo %p (%d)\n", 21047 (void *)tcp, (void *)mmd, 21048 (void *)pkt_info, err); 21049 } 21050 TCP_STAT(tcps, tcp_mdt_addpdescfail); 21051 goto legacy_send; /* out_of_mem */ 21052 } 21053 ASSERT(pkt != NULL); 21054 21055 /* calculate IP header and TCP checksums */ 21056 if (af == AF_INET) { 21057 /* calculate pseudo-header checksum */ 21058 cksum = (dst >> 16) + (dst & 0xFFFF) + 21059 (src >> 16) + (src & 0xFFFF); 21060 21061 /* offset for TCP header checksum */ 21062 up = IPH_TCPH_CHECKSUMP(ipha, 21063 IP_SIMPLE_HDR_LENGTH); 21064 } else { 21065 up = (uint16_t *)&ip6h->ip6_src; 21066 21067 /* calculate pseudo-header checksum */ 21068 cksum = up[0] + up[1] + up[2] + up[3] + 21069 up[4] + up[5] + up[6] + up[7] + 21070 up[8] + up[9] + up[10] + up[11] + 21071 up[12] + up[13] + up[14] + up[15]; 21072 21073 /* Fold the initial sum */ 21074 cksum = (cksum & 0xffff) + (cksum >> 16); 21075 21076 up = (uint16_t *)(((uchar_t *)ip6h) + 21077 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 21078 } 21079 21080 if (hwcksum_flags & HCK_FULLCKSUM) { 21081 /* clear checksum field for hardware */ 21082 *up = 0; 21083 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 21084 uint32_t sum; 21085 21086 /* pseudo-header checksumming */ 21087 sum = *up + cksum + IP_TCP_CSUM_COMP; 21088 sum = (sum & 0xFFFF) + (sum >> 16); 21089 *up = (sum & 0xFFFF) + (sum >> 16); 21090 } else { 21091 /* software checksumming */ 21092 TCP_STAT(tcps, tcp_out_sw_cksum); 21093 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 21094 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 21095 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 21096 cksum + IP_TCP_CSUM_COMP); 21097 if (*up == 0) 21098 *up = 0xFFFF; 21099 } 21100 21101 /* IPv4 header checksum */ 21102 if (af == AF_INET) { 21103 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 21104 ipha->ipha_hdr_checksum = 0; 21105 } else { 21106 IP_HDR_CKSUM(ipha, cksum, 21107 ((uint32_t *)ipha)[0], 21108 ((uint16_t *)ipha)[4]); 21109 } 21110 } 21111 21112 if (af == AF_INET && 21113 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 21114 af == AF_INET6 && 21115 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 21116 mblk_t *mp, *mp1; 21117 uchar_t *hdr_rptr, *hdr_wptr; 21118 uchar_t *pld_rptr, *pld_wptr; 21119 21120 /* 21121 * We reconstruct a pseudo packet for the hooks 21122 * framework using mmd_transform_link(). 21123 * If it is a split packet we pullup the 21124 * payload. FW_HOOKS expects a pkt comprising 21125 * of two mblks: a header and the payload. 21126 */ 21127 if ((mp = mmd_transform_link(pkt)) == NULL) { 21128 TCP_STAT(tcps, tcp_mdt_allocfail); 21129 goto legacy_send; 21130 } 21131 21132 if (pkt_info->pld_cnt > 1) { 21133 /* split payload, more than one pld */ 21134 if ((mp1 = msgpullup(mp->b_cont, -1)) == 21135 NULL) { 21136 freemsg(mp); 21137 TCP_STAT(tcps, 21138 tcp_mdt_allocfail); 21139 goto legacy_send; 21140 } 21141 freemsg(mp->b_cont); 21142 mp->b_cont = mp1; 21143 } else { 21144 mp1 = mp->b_cont; 21145 } 21146 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 21147 21148 /* 21149 * Remember the message offsets. This is so we 21150 * can detect changes when we return from the 21151 * FW_HOOKS callbacks. 21152 */ 21153 hdr_rptr = mp->b_rptr; 21154 hdr_wptr = mp->b_wptr; 21155 pld_rptr = mp->b_cont->b_rptr; 21156 pld_wptr = mp->b_cont->b_wptr; 21157 21158 if (af == AF_INET) { 21159 DTRACE_PROBE4( 21160 ip4__physical__out__start, 21161 ill_t *, NULL, 21162 ill_t *, ill, 21163 ipha_t *, ipha, 21164 mblk_t *, mp); 21165 FW_HOOKS( 21166 ipst->ips_ip4_physical_out_event, 21167 ipst->ips_ipv4firewall_physical_out, 21168 NULL, ill, ipha, mp, mp, 0, ipst); 21169 DTRACE_PROBE1( 21170 ip4__physical__out__end, 21171 mblk_t *, mp); 21172 } else { 21173 DTRACE_PROBE4( 21174 ip6__physical__out_start, 21175 ill_t *, NULL, 21176 ill_t *, ill, 21177 ip6_t *, ip6h, 21178 mblk_t *, mp); 21179 FW_HOOKS6( 21180 ipst->ips_ip6_physical_out_event, 21181 ipst->ips_ipv6firewall_physical_out, 21182 NULL, ill, ip6h, mp, mp, 0, ipst); 21183 DTRACE_PROBE1( 21184 ip6__physical__out__end, 21185 mblk_t *, mp); 21186 } 21187 21188 if (mp == NULL || 21189 (mp1 = mp->b_cont) == NULL || 21190 mp->b_rptr != hdr_rptr || 21191 mp->b_wptr != hdr_wptr || 21192 mp1->b_rptr != pld_rptr || 21193 mp1->b_wptr != pld_wptr || 21194 mp1->b_cont != NULL) { 21195 /* 21196 * We abandon multidata processing and 21197 * return to the normal path, either 21198 * when a packet is blocked, or when 21199 * the boundaries of header buffer or 21200 * payload buffer have been changed by 21201 * FW_HOOKS[6]. 21202 */ 21203 if (mp != NULL) 21204 freemsg(mp); 21205 goto legacy_send; 21206 } 21207 /* Finished with the pseudo packet */ 21208 freemsg(mp); 21209 } 21210 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 21211 ill, ipha, ip6h); 21212 /* advance header offset */ 21213 cur_hdr_off += hdr_frag_sz; 21214 21215 obbytes += tcp->tcp_last_sent_len; 21216 ++obsegs; 21217 } while (!done && *usable > 0 && --num_burst_seg > 0 && 21218 *tail_unsent > 0); 21219 21220 if ((*xmit_tail)->b_next == NULL) { 21221 /* 21222 * Store the lbolt used for RTT estimation. We can only 21223 * record one timestamp per mblk so we do it when we 21224 * reach the end of the payload buffer. Also we only 21225 * take a new timestamp sample when the previous timed 21226 * data from the same mblk has been ack'ed. 21227 */ 21228 (*xmit_tail)->b_prev = local_time; 21229 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 21230 } 21231 21232 ASSERT(*tail_unsent >= 0); 21233 if (*tail_unsent > 0) { 21234 /* 21235 * We got here because we broke out of the above 21236 * loop due to of one of the following cases: 21237 * 21238 * 1. len < adjusted MSS (i.e. small), 21239 * 2. Sender SWS avoidance, 21240 * 3. max_pld is zero. 21241 * 21242 * We are done for this Multidata, so trim our 21243 * last payload buffer (if any) accordingly. 21244 */ 21245 if (md_pbuf != NULL) 21246 md_pbuf->b_wptr -= *tail_unsent; 21247 } else if (*usable > 0) { 21248 *xmit_tail = (*xmit_tail)->b_cont; 21249 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 21250 (uintptr_t)INT_MAX); 21251 *tail_unsent = (int)MBLKL(*xmit_tail); 21252 add_buffer = B_TRUE; 21253 } 21254 } while (!done && *usable > 0 && num_burst_seg > 0 && 21255 (tcp_mdt_chain || max_pld > 0)); 21256 21257 if (md_mp_head != NULL) { 21258 /* send everything down */ 21259 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 21260 &rconfirm); 21261 } 21262 21263 #undef PREP_NEW_MULTIDATA 21264 #undef PREP_NEW_PBUF 21265 #undef IPVER 21266 21267 IRE_REFRELE(ire); 21268 return (0); 21269 } 21270 21271 /* 21272 * A wrapper function for sending one or more Multidata messages down to 21273 * the module below ip; this routine does not release the reference of the 21274 * IRE (caller does that). This routine is analogous to tcp_send_data(). 21275 */ 21276 static void 21277 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 21278 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 21279 { 21280 uint64_t delta; 21281 nce_t *nce; 21282 tcp_stack_t *tcps = tcp->tcp_tcps; 21283 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21284 21285 ASSERT(ire != NULL && ill != NULL); 21286 ASSERT(ire->ire_stq != NULL); 21287 ASSERT(md_mp_head != NULL); 21288 ASSERT(rconfirm != NULL); 21289 21290 /* adjust MIBs and IRE timestamp */ 21291 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 21292 tcp->tcp_obsegs += obsegs; 21293 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 21294 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 21295 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 21296 21297 if (tcp->tcp_ipversion == IPV4_VERSION) { 21298 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 21299 } else { 21300 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 21301 } 21302 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 21303 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 21304 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 21305 21306 ire->ire_ob_pkt_count += obsegs; 21307 if (ire->ire_ipif != NULL) 21308 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 21309 ire->ire_last_used_time = lbolt; 21310 21311 if (ipst->ips_ipobs_enabled) { 21312 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 21313 pdesc_t *dl_pkt; 21314 pdescinfo_t pinfo; 21315 mblk_t *nmp; 21316 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 21317 21318 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 21319 (dl_pkt != NULL); 21320 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 21321 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 21322 continue; 21323 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 21324 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 21325 freemsg(nmp); 21326 } 21327 } 21328 21329 /* send it down */ 21330 if (ILL_DLS_CAPABLE(ill)) { 21331 ill_dls_capab_t *ill_dls = ill->ill_dls_capab; 21332 ill_dls->ill_tx(ill_dls->ill_tx_handle, md_mp_head); 21333 } else { 21334 putnext(ire->ire_stq, md_mp_head); 21335 } 21336 21337 /* we're done for TCP/IPv4 */ 21338 if (tcp->tcp_ipversion == IPV4_VERSION) 21339 return; 21340 21341 nce = ire->ire_nce; 21342 21343 ASSERT(nce != NULL); 21344 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 21345 ASSERT(nce->nce_state != ND_INCOMPLETE); 21346 21347 /* reachability confirmation? */ 21348 if (*rconfirm) { 21349 nce->nce_last = TICK_TO_MSEC(lbolt64); 21350 if (nce->nce_state != ND_REACHABLE) { 21351 mutex_enter(&nce->nce_lock); 21352 nce->nce_state = ND_REACHABLE; 21353 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 21354 mutex_exit(&nce->nce_lock); 21355 (void) untimeout(nce->nce_timeout_id); 21356 if (ip_debug > 2) { 21357 /* ip1dbg */ 21358 pr_addr_dbg("tcp_multisend_data: state " 21359 "for %s changed to REACHABLE\n", 21360 AF_INET6, &ire->ire_addr_v6); 21361 } 21362 } 21363 /* reset transport reachability confirmation */ 21364 *rconfirm = B_FALSE; 21365 } 21366 21367 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 21368 ip1dbg(("tcp_multisend_data: delta = %" PRId64 21369 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 21370 21371 if (delta > (uint64_t)ill->ill_reachable_time) { 21372 mutex_enter(&nce->nce_lock); 21373 switch (nce->nce_state) { 21374 case ND_REACHABLE: 21375 case ND_STALE: 21376 /* 21377 * ND_REACHABLE is identical to ND_STALE in this 21378 * specific case. If reachable time has expired for 21379 * this neighbor (delta is greater than reachable 21380 * time), conceptually, the neighbor cache is no 21381 * longer in REACHABLE state, but already in STALE 21382 * state. So the correct transition here is to 21383 * ND_DELAY. 21384 */ 21385 nce->nce_state = ND_DELAY; 21386 mutex_exit(&nce->nce_lock); 21387 NDP_RESTART_TIMER(nce, 21388 ipst->ips_delay_first_probe_time); 21389 if (ip_debug > 3) { 21390 /* ip2dbg */ 21391 pr_addr_dbg("tcp_multisend_data: state " 21392 "for %s changed to DELAY\n", 21393 AF_INET6, &ire->ire_addr_v6); 21394 } 21395 break; 21396 case ND_DELAY: 21397 case ND_PROBE: 21398 mutex_exit(&nce->nce_lock); 21399 /* Timers have already started */ 21400 break; 21401 case ND_UNREACHABLE: 21402 /* 21403 * ndp timer has detected that this nce is 21404 * unreachable and initiated deleting this nce 21405 * and all its associated IREs. This is a race 21406 * where we found the ire before it was deleted 21407 * and have just sent out a packet using this 21408 * unreachable nce. 21409 */ 21410 mutex_exit(&nce->nce_lock); 21411 break; 21412 default: 21413 ASSERT(0); 21414 } 21415 } 21416 } 21417 21418 /* 21419 * Derived from tcp_send_data(). 21420 */ 21421 static void 21422 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 21423 int num_lso_seg) 21424 { 21425 ipha_t *ipha; 21426 mblk_t *ire_fp_mp; 21427 uint_t ire_fp_mp_len; 21428 uint32_t hcksum_txflags = 0; 21429 ipaddr_t src; 21430 ipaddr_t dst; 21431 uint32_t cksum; 21432 uint16_t *up; 21433 tcp_stack_t *tcps = tcp->tcp_tcps; 21434 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21435 21436 ASSERT(DB_TYPE(mp) == M_DATA); 21437 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 21438 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 21439 ASSERT(tcp->tcp_connp != NULL); 21440 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 21441 21442 ipha = (ipha_t *)mp->b_rptr; 21443 src = ipha->ipha_src; 21444 dst = ipha->ipha_dst; 21445 21446 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 21447 21448 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 21449 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 21450 num_lso_seg); 21451 #ifndef _BIG_ENDIAN 21452 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 21453 #endif 21454 if (tcp->tcp_snd_zcopy_aware) { 21455 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 21456 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 21457 mp = tcp_zcopy_disable(tcp, mp); 21458 } 21459 21460 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 21461 ASSERT(ill->ill_hcksum_capab != NULL); 21462 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 21463 } 21464 21465 /* 21466 * Since the TCP checksum should be recalculated by h/w, we can just 21467 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 21468 * pseudo-header checksum for HCK_PARTIALCKSUM. 21469 * The partial pseudo-header excludes TCP length, that was calculated 21470 * in tcp_send(), so to zero *up before further processing. 21471 */ 21472 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 21473 21474 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 21475 *up = 0; 21476 21477 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 21478 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 21479 21480 /* 21481 * Append LSO flag to DB_LSOFLAGS(mp) and set the mss to DB_LSOMSS(mp). 21482 */ 21483 DB_LSOFLAGS(mp) |= HW_LSO; 21484 DB_LSOMSS(mp) = mss; 21485 21486 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21487 ire_fp_mp_len = MBLKL(ire_fp_mp); 21488 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 21489 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 21490 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 21491 21492 UPDATE_OB_PKT_COUNT(ire); 21493 ire->ire_last_used_time = lbolt; 21494 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 21495 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 21496 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 21497 ntohs(ipha->ipha_length)); 21498 21499 if (ILL_DLS_CAPABLE(ill)) { 21500 /* 21501 * Send the packet directly to DLD, where it may be queued 21502 * depending on the availability of transmit resources at 21503 * the media layer. 21504 */ 21505 IP_DLS_ILL_TX(ill, ipha, mp, ipst, ire_fp_mp_len); 21506 } else { 21507 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 21508 DTRACE_PROBE4(ip4__physical__out__start, 21509 ill_t *, NULL, ill_t *, out_ill, 21510 ipha_t *, ipha, mblk_t *, mp); 21511 FW_HOOKS(ipst->ips_ip4_physical_out_event, 21512 ipst->ips_ipv4firewall_physical_out, 21513 NULL, out_ill, ipha, mp, mp, 0, ipst); 21514 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 21515 21516 if (mp != NULL) { 21517 if (ipst->ips_ipobs_enabled) { 21518 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 21519 21520 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 21521 ALL_ZONES, ill, tcp->tcp_ipversion, 21522 ire_fp_mp_len, ipst); 21523 } 21524 DTRACE_IP_FASTPATH(mp, ipha, out_ill, ipha, NULL); 21525 putnext(ire->ire_stq, mp); 21526 } 21527 } 21528 } 21529 21530 /* 21531 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 21532 * scheme, and returns one of the following: 21533 * 21534 * -1 = failed allocation. 21535 * 0 = success; burst count reached, or usable send window is too small, 21536 * and that we'd rather wait until later before sending again. 21537 * 1 = success; we are called from tcp_multisend(), and both usable send 21538 * window and tail_unsent are greater than the MDT threshold, and thus 21539 * Multidata Transmit should be used instead. 21540 */ 21541 static int 21542 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 21543 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 21544 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 21545 const int mdt_thres) 21546 { 21547 int num_burst_seg = tcp->tcp_snd_burst; 21548 ire_t *ire = NULL; 21549 ill_t *ill = NULL; 21550 mblk_t *ire_fp_mp = NULL; 21551 uint_t ire_fp_mp_len = 0; 21552 int num_lso_seg = 1; 21553 uint_t lso_usable; 21554 boolean_t do_lso_send = B_FALSE; 21555 tcp_stack_t *tcps = tcp->tcp_tcps; 21556 21557 /* 21558 * Check LSO capability before any further work. And the similar check 21559 * need to be done in for(;;) loop. 21560 * LSO will be deployed when therer is more than one mss of available 21561 * data and a burst transmission is allowed. 21562 */ 21563 if (tcp->tcp_lso && 21564 (tcp->tcp_valid_bits == 0 || 21565 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21566 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21567 /* 21568 * Try to find usable IRE/ILL and do basic check to the ILL. 21569 */ 21570 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 21571 /* 21572 * Enable LSO with this transmission. 21573 * Since IRE has been hold in 21574 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 21575 * should be called before return. 21576 */ 21577 do_lso_send = B_TRUE; 21578 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21579 ire_fp_mp_len = MBLKL(ire_fp_mp); 21580 /* Round up to multiple of 4 */ 21581 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21582 } else { 21583 do_lso_send = B_FALSE; 21584 ill = NULL; 21585 } 21586 } 21587 21588 for (;;) { 21589 struct datab *db; 21590 tcph_t *tcph; 21591 uint32_t sum; 21592 mblk_t *mp, *mp1; 21593 uchar_t *rptr; 21594 int len; 21595 21596 /* 21597 * If we're called by tcp_multisend(), and the amount of 21598 * sendable data as well as the size of current xmit_tail 21599 * is beyond the MDT threshold, return to the caller and 21600 * let the large data transmit be done using MDT. 21601 */ 21602 if (*usable > 0 && *usable > mdt_thres && 21603 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21604 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21605 ASSERT(tcp->tcp_mdt); 21606 return (1); /* success; do large send */ 21607 } 21608 21609 if (num_burst_seg == 0) 21610 break; /* success; burst count reached */ 21611 21612 /* 21613 * Calculate the maximum payload length we can send in *one* 21614 * time. 21615 */ 21616 if (do_lso_send) { 21617 /* 21618 * Check whether need to do LSO any more. 21619 */ 21620 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21621 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21622 lso_usable = MIN(lso_usable, 21623 num_burst_seg * mss); 21624 21625 num_lso_seg = lso_usable / mss; 21626 if (lso_usable % mss) { 21627 num_lso_seg++; 21628 tcp->tcp_last_sent_len = (ushort_t) 21629 (lso_usable % mss); 21630 } else { 21631 tcp->tcp_last_sent_len = (ushort_t)mss; 21632 } 21633 } else { 21634 do_lso_send = B_FALSE; 21635 num_lso_seg = 1; 21636 lso_usable = mss; 21637 } 21638 } 21639 21640 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21641 21642 /* 21643 * Adjust num_burst_seg here. 21644 */ 21645 num_burst_seg -= num_lso_seg; 21646 21647 len = mss; 21648 if (len > *usable) { 21649 ASSERT(do_lso_send == B_FALSE); 21650 21651 len = *usable; 21652 if (len <= 0) { 21653 /* Terminate the loop */ 21654 break; /* success; too small */ 21655 } 21656 /* 21657 * Sender silly-window avoidance. 21658 * Ignore this if we are going to send a 21659 * zero window probe out. 21660 * 21661 * TODO: force data into microscopic window? 21662 * ==> (!pushed || (unsent > usable)) 21663 */ 21664 if (len < (tcp->tcp_max_swnd >> 1) && 21665 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21666 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21667 len == 1) && (! tcp->tcp_zero_win_probe)) { 21668 /* 21669 * If the retransmit timer is not running 21670 * we start it so that we will retransmit 21671 * in the case when the the receiver has 21672 * decremented the window. 21673 */ 21674 if (*snxt == tcp->tcp_snxt && 21675 *snxt == tcp->tcp_suna) { 21676 /* 21677 * We are not supposed to send 21678 * anything. So let's wait a little 21679 * bit longer before breaking SWS 21680 * avoidance. 21681 * 21682 * What should the value be? 21683 * Suggestion: MAX(init rexmit time, 21684 * tcp->tcp_rto) 21685 */ 21686 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21687 } 21688 break; /* success; too small */ 21689 } 21690 } 21691 21692 tcph = tcp->tcp_tcph; 21693 21694 /* 21695 * The reason to adjust len here is that we need to set flags 21696 * and calculate checksum. 21697 */ 21698 if (do_lso_send) 21699 len = lso_usable; 21700 21701 *usable -= len; /* Approximate - can be adjusted later */ 21702 if (*usable > 0) 21703 tcph->th_flags[0] = TH_ACK; 21704 else 21705 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21706 21707 /* 21708 * Prime pump for IP's checksumming on our behalf 21709 * Include the adjustment for a source route if any. 21710 */ 21711 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21712 sum = (sum >> 16) + (sum & 0xFFFF); 21713 U16_TO_ABE16(sum, tcph->th_sum); 21714 21715 U32_TO_ABE32(*snxt, tcph->th_seq); 21716 21717 /* 21718 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21719 * set. For the case when TCP_FSS_VALID is the only valid 21720 * bit (normal active close), branch off only when we think 21721 * that the FIN flag needs to be set. Note for this case, 21722 * that (snxt + len) may not reflect the actual seg_len, 21723 * as len may be further reduced in tcp_xmit_mp(). If len 21724 * gets modified, we will end up here again. 21725 */ 21726 if (tcp->tcp_valid_bits != 0 && 21727 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21728 ((*snxt + len) == tcp->tcp_fss))) { 21729 uchar_t *prev_rptr; 21730 uint32_t prev_snxt = tcp->tcp_snxt; 21731 21732 if (*tail_unsent == 0) { 21733 ASSERT((*xmit_tail)->b_cont != NULL); 21734 *xmit_tail = (*xmit_tail)->b_cont; 21735 prev_rptr = (*xmit_tail)->b_rptr; 21736 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21737 (*xmit_tail)->b_rptr); 21738 } else { 21739 prev_rptr = (*xmit_tail)->b_rptr; 21740 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21741 *tail_unsent; 21742 } 21743 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21744 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21745 /* Restore tcp_snxt so we get amount sent right. */ 21746 tcp->tcp_snxt = prev_snxt; 21747 if (prev_rptr == (*xmit_tail)->b_rptr) { 21748 /* 21749 * If the previous timestamp is still in use, 21750 * don't stomp on it. 21751 */ 21752 if ((*xmit_tail)->b_next == NULL) { 21753 (*xmit_tail)->b_prev = local_time; 21754 (*xmit_tail)->b_next = 21755 (mblk_t *)(uintptr_t)(*snxt); 21756 } 21757 } else 21758 (*xmit_tail)->b_rptr = prev_rptr; 21759 21760 if (mp == NULL) { 21761 if (ire != NULL) 21762 IRE_REFRELE(ire); 21763 return (-1); 21764 } 21765 mp1 = mp->b_cont; 21766 21767 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21768 tcp->tcp_last_sent_len = (ushort_t)len; 21769 while (mp1->b_cont) { 21770 *xmit_tail = (*xmit_tail)->b_cont; 21771 (*xmit_tail)->b_prev = local_time; 21772 (*xmit_tail)->b_next = 21773 (mblk_t *)(uintptr_t)(*snxt); 21774 mp1 = mp1->b_cont; 21775 } 21776 *snxt += len; 21777 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21778 BUMP_LOCAL(tcp->tcp_obsegs); 21779 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21780 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21781 tcp_send_data(tcp, q, mp); 21782 continue; 21783 } 21784 21785 *snxt += len; /* Adjust later if we don't send all of len */ 21786 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21787 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21788 21789 if (*tail_unsent) { 21790 /* Are the bytes above us in flight? */ 21791 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21792 if (rptr != (*xmit_tail)->b_rptr) { 21793 *tail_unsent -= len; 21794 if (len <= mss) /* LSO is unusable */ 21795 tcp->tcp_last_sent_len = (ushort_t)len; 21796 len += tcp_hdr_len; 21797 if (tcp->tcp_ipversion == IPV4_VERSION) 21798 tcp->tcp_ipha->ipha_length = htons(len); 21799 else 21800 tcp->tcp_ip6h->ip6_plen = 21801 htons(len - 21802 ((char *)&tcp->tcp_ip6h[1] - 21803 tcp->tcp_iphc)); 21804 mp = dupb(*xmit_tail); 21805 if (mp == NULL) { 21806 if (ire != NULL) 21807 IRE_REFRELE(ire); 21808 return (-1); /* out_of_mem */ 21809 } 21810 mp->b_rptr = rptr; 21811 /* 21812 * If the old timestamp is no longer in use, 21813 * sample a new timestamp now. 21814 */ 21815 if ((*xmit_tail)->b_next == NULL) { 21816 (*xmit_tail)->b_prev = local_time; 21817 (*xmit_tail)->b_next = 21818 (mblk_t *)(uintptr_t)(*snxt-len); 21819 } 21820 goto must_alloc; 21821 } 21822 } else { 21823 *xmit_tail = (*xmit_tail)->b_cont; 21824 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21825 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21826 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21827 (*xmit_tail)->b_rptr); 21828 } 21829 21830 (*xmit_tail)->b_prev = local_time; 21831 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21832 21833 *tail_unsent -= len; 21834 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21835 tcp->tcp_last_sent_len = (ushort_t)len; 21836 21837 len += tcp_hdr_len; 21838 if (tcp->tcp_ipversion == IPV4_VERSION) 21839 tcp->tcp_ipha->ipha_length = htons(len); 21840 else 21841 tcp->tcp_ip6h->ip6_plen = htons(len - 21842 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21843 21844 mp = dupb(*xmit_tail); 21845 if (mp == NULL) { 21846 if (ire != NULL) 21847 IRE_REFRELE(ire); 21848 return (-1); /* out_of_mem */ 21849 } 21850 21851 len = tcp_hdr_len; 21852 /* 21853 * There are four reasons to allocate a new hdr mblk: 21854 * 1) The bytes above us are in use by another packet 21855 * 2) We don't have good alignment 21856 * 3) The mblk is being shared 21857 * 4) We don't have enough room for a header 21858 */ 21859 rptr = mp->b_rptr - len; 21860 if (!OK_32PTR(rptr) || 21861 ((db = mp->b_datap), db->db_ref != 2) || 21862 rptr < db->db_base + ire_fp_mp_len) { 21863 /* NOTE: we assume allocb returns an OK_32PTR */ 21864 21865 must_alloc:; 21866 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21867 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21868 if (mp1 == NULL) { 21869 freemsg(mp); 21870 if (ire != NULL) 21871 IRE_REFRELE(ire); 21872 return (-1); /* out_of_mem */ 21873 } 21874 mp1->b_cont = mp; 21875 mp = mp1; 21876 /* Leave room for Link Level header */ 21877 len = tcp_hdr_len; 21878 rptr = 21879 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21880 mp->b_wptr = &rptr[len]; 21881 } 21882 21883 /* 21884 * Fill in the header using the template header, and add 21885 * options such as time-stamp, ECN and/or SACK, as needed. 21886 */ 21887 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21888 21889 mp->b_rptr = rptr; 21890 21891 if (*tail_unsent) { 21892 int spill = *tail_unsent; 21893 21894 mp1 = mp->b_cont; 21895 if (mp1 == NULL) 21896 mp1 = mp; 21897 21898 /* 21899 * If we're a little short, tack on more mblks until 21900 * there is no more spillover. 21901 */ 21902 while (spill < 0) { 21903 mblk_t *nmp; 21904 int nmpsz; 21905 21906 nmp = (*xmit_tail)->b_cont; 21907 nmpsz = MBLKL(nmp); 21908 21909 /* 21910 * Excess data in mblk; can we split it? 21911 * If MDT is enabled for the connection, 21912 * keep on splitting as this is a transient 21913 * send path. 21914 */ 21915 if (!do_lso_send && !tcp->tcp_mdt && 21916 (spill + nmpsz > 0)) { 21917 /* 21918 * Don't split if stream head was 21919 * told to break up larger writes 21920 * into smaller ones. 21921 */ 21922 if (tcp->tcp_maxpsz > 0) 21923 break; 21924 21925 /* 21926 * Next mblk is less than SMSS/2 21927 * rounded up to nearest 64-byte; 21928 * let it get sent as part of the 21929 * next segment. 21930 */ 21931 if (tcp->tcp_localnet && 21932 !tcp->tcp_cork && 21933 (nmpsz < roundup((mss >> 1), 64))) 21934 break; 21935 } 21936 21937 *xmit_tail = nmp; 21938 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21939 /* Stash for rtt use later */ 21940 (*xmit_tail)->b_prev = local_time; 21941 (*xmit_tail)->b_next = 21942 (mblk_t *)(uintptr_t)(*snxt - len); 21943 mp1->b_cont = dupb(*xmit_tail); 21944 mp1 = mp1->b_cont; 21945 21946 spill += nmpsz; 21947 if (mp1 == NULL) { 21948 *tail_unsent = spill; 21949 freemsg(mp); 21950 if (ire != NULL) 21951 IRE_REFRELE(ire); 21952 return (-1); /* out_of_mem */ 21953 } 21954 } 21955 21956 /* Trim back any surplus on the last mblk */ 21957 if (spill >= 0) { 21958 mp1->b_wptr -= spill; 21959 *tail_unsent = spill; 21960 } else { 21961 /* 21962 * We did not send everything we could in 21963 * order to remain within the b_cont limit. 21964 */ 21965 *usable -= spill; 21966 *snxt += spill; 21967 tcp->tcp_last_sent_len += spill; 21968 UPDATE_MIB(&tcps->tcps_mib, 21969 tcpOutDataBytes, spill); 21970 /* 21971 * Adjust the checksum 21972 */ 21973 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21974 sum += spill; 21975 sum = (sum >> 16) + (sum & 0xFFFF); 21976 U16_TO_ABE16(sum, tcph->th_sum); 21977 if (tcp->tcp_ipversion == IPV4_VERSION) { 21978 sum = ntohs( 21979 ((ipha_t *)rptr)->ipha_length) + 21980 spill; 21981 ((ipha_t *)rptr)->ipha_length = 21982 htons(sum); 21983 } else { 21984 sum = ntohs( 21985 ((ip6_t *)rptr)->ip6_plen) + 21986 spill; 21987 ((ip6_t *)rptr)->ip6_plen = 21988 htons(sum); 21989 } 21990 *tail_unsent = 0; 21991 } 21992 } 21993 if (tcp->tcp_ip_forward_progress) { 21994 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21995 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21996 tcp->tcp_ip_forward_progress = B_FALSE; 21997 } 21998 21999 if (do_lso_send) { 22000 tcp_lsosend_data(tcp, mp, ire, ill, mss, 22001 num_lso_seg); 22002 tcp->tcp_obsegs += num_lso_seg; 22003 22004 TCP_STAT(tcps, tcp_lso_times); 22005 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 22006 } else { 22007 tcp_send_data(tcp, q, mp); 22008 BUMP_LOCAL(tcp->tcp_obsegs); 22009 } 22010 } 22011 22012 if (ire != NULL) 22013 IRE_REFRELE(ire); 22014 return (0); 22015 } 22016 22017 /* Unlink and return any mblk that looks like it contains a MDT info */ 22018 static mblk_t * 22019 tcp_mdt_info_mp(mblk_t *mp) 22020 { 22021 mblk_t *prev_mp; 22022 22023 for (;;) { 22024 prev_mp = mp; 22025 /* no more to process? */ 22026 if ((mp = mp->b_cont) == NULL) 22027 break; 22028 22029 switch (DB_TYPE(mp)) { 22030 case M_CTL: 22031 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 22032 continue; 22033 ASSERT(prev_mp != NULL); 22034 prev_mp->b_cont = mp->b_cont; 22035 mp->b_cont = NULL; 22036 return (mp); 22037 default: 22038 break; 22039 } 22040 } 22041 return (mp); 22042 } 22043 22044 /* MDT info update routine, called when IP notifies us about MDT */ 22045 static void 22046 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 22047 { 22048 boolean_t prev_state; 22049 tcp_stack_t *tcps = tcp->tcp_tcps; 22050 22051 /* 22052 * IP is telling us to abort MDT on this connection? We know 22053 * this because the capability is only turned off when IP 22054 * encounters some pathological cases, e.g. link-layer change 22055 * where the new driver doesn't support MDT, or in situation 22056 * where MDT usage on the link-layer has been switched off. 22057 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 22058 * if the link-layer doesn't support MDT, and if it does, it 22059 * will indicate that the feature is to be turned on. 22060 */ 22061 prev_state = tcp->tcp_mdt; 22062 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 22063 if (!tcp->tcp_mdt && !first) { 22064 TCP_STAT(tcps, tcp_mdt_conn_halted3); 22065 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 22066 (void *)tcp->tcp_connp)); 22067 } 22068 22069 /* 22070 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 22071 * so disable MDT otherwise. The checks are done here 22072 * and in tcp_wput_data(). 22073 */ 22074 if (tcp->tcp_mdt && 22075 (tcp->tcp_ipversion == IPV4_VERSION && 22076 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 22077 (tcp->tcp_ipversion == IPV6_VERSION && 22078 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 22079 tcp->tcp_mdt = B_FALSE; 22080 22081 if (tcp->tcp_mdt) { 22082 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 22083 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 22084 "version (%d), expected version is %d", 22085 mdt_capab->ill_mdt_version, MDT_VERSION_2); 22086 tcp->tcp_mdt = B_FALSE; 22087 return; 22088 } 22089 22090 /* 22091 * We need the driver to be able to handle at least three 22092 * spans per packet in order for tcp MDT to be utilized. 22093 * The first is for the header portion, while the rest are 22094 * needed to handle a packet that straddles across two 22095 * virtually non-contiguous buffers; a typical tcp packet 22096 * therefore consists of only two spans. Note that we take 22097 * a zero as "don't care". 22098 */ 22099 if (mdt_capab->ill_mdt_span_limit > 0 && 22100 mdt_capab->ill_mdt_span_limit < 3) { 22101 tcp->tcp_mdt = B_FALSE; 22102 return; 22103 } 22104 22105 /* a zero means driver wants default value */ 22106 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 22107 tcps->tcps_mdt_max_pbufs); 22108 if (tcp->tcp_mdt_max_pld == 0) 22109 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 22110 22111 /* ensure 32-bit alignment */ 22112 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 22113 mdt_capab->ill_mdt_hdr_head), 4); 22114 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 22115 mdt_capab->ill_mdt_hdr_tail), 4); 22116 22117 if (!first && !prev_state) { 22118 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 22119 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 22120 (void *)tcp->tcp_connp)); 22121 } 22122 } 22123 } 22124 22125 /* Unlink and return any mblk that looks like it contains a LSO info */ 22126 static mblk_t * 22127 tcp_lso_info_mp(mblk_t *mp) 22128 { 22129 mblk_t *prev_mp; 22130 22131 for (;;) { 22132 prev_mp = mp; 22133 /* no more to process? */ 22134 if ((mp = mp->b_cont) == NULL) 22135 break; 22136 22137 switch (DB_TYPE(mp)) { 22138 case M_CTL: 22139 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 22140 continue; 22141 ASSERT(prev_mp != NULL); 22142 prev_mp->b_cont = mp->b_cont; 22143 mp->b_cont = NULL; 22144 return (mp); 22145 default: 22146 break; 22147 } 22148 } 22149 22150 return (mp); 22151 } 22152 22153 /* LSO info update routine, called when IP notifies us about LSO */ 22154 static void 22155 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 22156 { 22157 tcp_stack_t *tcps = tcp->tcp_tcps; 22158 22159 /* 22160 * IP is telling us to abort LSO on this connection? We know 22161 * this because the capability is only turned off when IP 22162 * encounters some pathological cases, e.g. link-layer change 22163 * where the new NIC/driver doesn't support LSO, or in situation 22164 * where LSO usage on the link-layer has been switched off. 22165 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 22166 * if the link-layer doesn't support LSO, and if it does, it 22167 * will indicate that the feature is to be turned on. 22168 */ 22169 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 22170 TCP_STAT(tcps, tcp_lso_enabled); 22171 22172 /* 22173 * We currently only support LSO on simple TCP/IPv4, 22174 * so disable LSO otherwise. The checks are done here 22175 * and in tcp_wput_data(). 22176 */ 22177 if (tcp->tcp_lso && 22178 (tcp->tcp_ipversion == IPV4_VERSION && 22179 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 22180 (tcp->tcp_ipversion == IPV6_VERSION)) { 22181 tcp->tcp_lso = B_FALSE; 22182 TCP_STAT(tcps, tcp_lso_disabled); 22183 } else { 22184 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 22185 lso_capab->ill_lso_max); 22186 } 22187 } 22188 22189 static void 22190 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 22191 { 22192 conn_t *connp = tcp->tcp_connp; 22193 tcp_stack_t *tcps = tcp->tcp_tcps; 22194 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22195 22196 ASSERT(ire != NULL); 22197 22198 /* 22199 * We may be in the fastpath here, and although we essentially do 22200 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 22201 * we try to keep things as brief as possible. After all, these 22202 * are only best-effort checks, and we do more thorough ones prior 22203 * to calling tcp_send()/tcp_multisend(). 22204 */ 22205 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 22206 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 22207 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 22208 !(ire->ire_flags & RTF_MULTIRT) && 22209 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 22210 CONN_IS_LSO_MD_FASTPATH(connp)) { 22211 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 22212 /* Cache the result */ 22213 connp->conn_lso_ok = B_TRUE; 22214 22215 ASSERT(ill->ill_lso_capab != NULL); 22216 if (!ill->ill_lso_capab->ill_lso_on) { 22217 ill->ill_lso_capab->ill_lso_on = 1; 22218 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22219 "LSO for interface %s\n", (void *)connp, 22220 ill->ill_name)); 22221 } 22222 tcp_lso_update(tcp, ill->ill_lso_capab); 22223 } else if (ipst->ips_ip_multidata_outbound && 22224 ILL_MDT_CAPABLE(ill)) { 22225 /* Cache the result */ 22226 connp->conn_mdt_ok = B_TRUE; 22227 22228 ASSERT(ill->ill_mdt_capab != NULL); 22229 if (!ill->ill_mdt_capab->ill_mdt_on) { 22230 ill->ill_mdt_capab->ill_mdt_on = 1; 22231 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22232 "MDT for interface %s\n", (void *)connp, 22233 ill->ill_name)); 22234 } 22235 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 22236 } 22237 } 22238 22239 /* 22240 * The goal is to reduce the number of generated tcp segments by 22241 * setting the maxpsz multiplier to 0; this will have an affect on 22242 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 22243 * into each packet, up to SMSS bytes. Doing this reduces the number 22244 * of outbound segments and incoming ACKs, thus allowing for better 22245 * network and system performance. In contrast the legacy behavior 22246 * may result in sending less than SMSS size, because the last mblk 22247 * for some packets may have more data than needed to make up SMSS, 22248 * and the legacy code refused to "split" it. 22249 * 22250 * We apply the new behavior on following situations: 22251 * 22252 * 1) Loopback connections, 22253 * 2) Connections in which the remote peer is not on local subnet, 22254 * 3) Local subnet connections over the bge interface (see below). 22255 * 22256 * Ideally, we would like this behavior to apply for interfaces other 22257 * than bge. However, doing so would negatively impact drivers which 22258 * perform dynamic mapping and unmapping of DMA resources, which are 22259 * increased by setting the maxpsz multiplier to 0 (more mblks per 22260 * packet will be generated by tcp). The bge driver does not suffer 22261 * from this, as it copies the mblks into pre-mapped buffers, and 22262 * therefore does not require more I/O resources than before. 22263 * 22264 * Otherwise, this behavior is present on all network interfaces when 22265 * the destination endpoint is non-local, since reducing the number 22266 * of packets in general is good for the network. 22267 * 22268 * TODO We need to remove this hard-coded conditional for bge once 22269 * a better "self-tuning" mechanism, or a way to comprehend 22270 * the driver transmit strategy is devised. Until the solution 22271 * is found and well understood, we live with this hack. 22272 */ 22273 if (!tcp_static_maxpsz && 22274 (tcp->tcp_loopback || !tcp->tcp_localnet || 22275 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 22276 /* override the default value */ 22277 tcp->tcp_maxpsz = 0; 22278 22279 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 22280 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 22281 ill != NULL ? ill->ill_name : ipif_loopback_name)); 22282 } 22283 22284 /* set the stream head parameters accordingly */ 22285 (void) tcp_maxpsz_set(tcp, B_TRUE); 22286 } 22287 22288 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 22289 static void 22290 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 22291 { 22292 uchar_t fval = *mp->b_rptr; 22293 mblk_t *tail; 22294 queue_t *q = tcp->tcp_wq; 22295 22296 /* TODO: How should flush interact with urgent data? */ 22297 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 22298 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 22299 /* 22300 * Flush only data that has not yet been put on the wire. If 22301 * we flush data that we have already transmitted, life, as we 22302 * know it, may come to an end. 22303 */ 22304 tail = tcp->tcp_xmit_tail; 22305 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 22306 tcp->tcp_xmit_tail_unsent = 0; 22307 tcp->tcp_unsent = 0; 22308 if (tail->b_wptr != tail->b_rptr) 22309 tail = tail->b_cont; 22310 if (tail) { 22311 mblk_t **excess = &tcp->tcp_xmit_head; 22312 for (;;) { 22313 mblk_t *mp1 = *excess; 22314 if (mp1 == tail) 22315 break; 22316 tcp->tcp_xmit_tail = mp1; 22317 tcp->tcp_xmit_last = mp1; 22318 excess = &mp1->b_cont; 22319 } 22320 *excess = NULL; 22321 tcp_close_mpp(&tail); 22322 if (tcp->tcp_snd_zcopy_aware) 22323 tcp_zcopy_notify(tcp); 22324 } 22325 /* 22326 * We have no unsent data, so unsent must be less than 22327 * tcp_xmit_lowater, so re-enable flow. 22328 */ 22329 mutex_enter(&tcp->tcp_non_sq_lock); 22330 if (tcp->tcp_flow_stopped) { 22331 tcp_clrqfull(tcp); 22332 } 22333 mutex_exit(&tcp->tcp_non_sq_lock); 22334 } 22335 /* 22336 * TODO: you can't just flush these, you have to increase rwnd for one 22337 * thing. For another, how should urgent data interact? 22338 */ 22339 if (fval & FLUSHR) { 22340 *mp->b_rptr = fval & ~FLUSHW; 22341 /* XXX */ 22342 qreply(q, mp); 22343 return; 22344 } 22345 freemsg(mp); 22346 } 22347 22348 /* 22349 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 22350 * messages. 22351 */ 22352 static void 22353 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 22354 { 22355 mblk_t *mp1; 22356 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 22357 STRUCT_HANDLE(strbuf, sb); 22358 queue_t *q = tcp->tcp_wq; 22359 int error; 22360 uint_t addrlen; 22361 22362 /* Make sure it is one of ours. */ 22363 switch (iocp->ioc_cmd) { 22364 case TI_GETMYNAME: 22365 case TI_GETPEERNAME: 22366 break; 22367 default: 22368 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 22369 return; 22370 } 22371 switch (mi_copy_state(q, mp, &mp1)) { 22372 case -1: 22373 return; 22374 case MI_COPY_CASE(MI_COPY_IN, 1): 22375 break; 22376 case MI_COPY_CASE(MI_COPY_OUT, 1): 22377 /* Copy out the strbuf. */ 22378 mi_copyout(q, mp); 22379 return; 22380 case MI_COPY_CASE(MI_COPY_OUT, 2): 22381 /* All done. */ 22382 mi_copy_done(q, mp, 0); 22383 return; 22384 default: 22385 mi_copy_done(q, mp, EPROTO); 22386 return; 22387 } 22388 /* Check alignment of the strbuf */ 22389 if (!OK_32PTR(mp1->b_rptr)) { 22390 mi_copy_done(q, mp, EINVAL); 22391 return; 22392 } 22393 22394 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 22395 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 22396 if (STRUCT_FGET(sb, maxlen) < addrlen) { 22397 mi_copy_done(q, mp, EINVAL); 22398 return; 22399 } 22400 22401 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 22402 if (mp1 == NULL) 22403 return; 22404 22405 switch (iocp->ioc_cmd) { 22406 case TI_GETMYNAME: 22407 error = tcp_getmyname(tcp, (void *)mp1->b_rptr, &addrlen); 22408 break; 22409 case TI_GETPEERNAME: 22410 error = tcp_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 22411 break; 22412 } 22413 22414 if (error != 0) { 22415 mi_copy_done(q, mp, error); 22416 } else { 22417 mp1->b_wptr += addrlen; 22418 STRUCT_FSET(sb, len, addrlen); 22419 22420 /* Copy out the address */ 22421 mi_copyout(q, mp); 22422 } 22423 } 22424 22425 /* 22426 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 22427 * messages. 22428 */ 22429 /* ARGSUSED */ 22430 static void 22431 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 22432 { 22433 conn_t *connp = (conn_t *)arg; 22434 tcp_t *tcp = connp->conn_tcp; 22435 queue_t *q = tcp->tcp_wq; 22436 struct iocblk *iocp; 22437 tcp_stack_t *tcps = tcp->tcp_tcps; 22438 22439 ASSERT(DB_TYPE(mp) == M_IOCTL); 22440 /* 22441 * Try and ASSERT the minimum possible references on the 22442 * conn early enough. Since we are executing on write side, 22443 * the connection is obviously not detached and that means 22444 * there is a ref each for TCP and IP. Since we are behind 22445 * the squeue, the minimum references needed are 3. If the 22446 * conn is in classifier hash list, there should be an 22447 * extra ref for that (we check both the possibilities). 22448 */ 22449 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22450 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22451 22452 iocp = (struct iocblk *)mp->b_rptr; 22453 switch (iocp->ioc_cmd) { 22454 case TCP_IOC_DEFAULT_Q: 22455 /* Wants to be the default wq. */ 22456 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 22457 iocp->ioc_error = EPERM; 22458 iocp->ioc_count = 0; 22459 mp->b_datap->db_type = M_IOCACK; 22460 qreply(q, mp); 22461 return; 22462 } 22463 tcp_def_q_set(tcp, mp); 22464 return; 22465 case _SIOCSOCKFALLBACK: 22466 /* 22467 * Either sockmod is about to be popped and the socket 22468 * would now be treated as a plain stream, or a module 22469 * is about to be pushed so we could no longer use read- 22470 * side synchronous streams for fused loopback tcp. 22471 * Drain any queued data and disable direct sockfs 22472 * interface from now on. 22473 */ 22474 if (!tcp->tcp_issocket) { 22475 DB_TYPE(mp) = M_IOCNAK; 22476 iocp->ioc_error = EINVAL; 22477 } else { 22478 #ifdef _ILP32 22479 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 22480 #else 22481 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 22482 #endif 22483 /* 22484 * Insert this socket into the acceptor hash. 22485 * We might need it for T_CONN_RES message 22486 */ 22487 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 22488 22489 if (tcp->tcp_fused) { 22490 /* 22491 * This is a fused loopback tcp; disable 22492 * read-side synchronous streams interface 22493 * and drain any queued data. It is okay 22494 * to do this for non-synchronous streams 22495 * fused tcp as well. 22496 */ 22497 tcp_fuse_disable_pair(tcp, B_FALSE); 22498 } 22499 tcp->tcp_issocket = B_FALSE; 22500 tcp->tcp_sodirect = NULL; 22501 TCP_STAT(tcps, tcp_sock_fallback); 22502 22503 DB_TYPE(mp) = M_IOCACK; 22504 iocp->ioc_error = 0; 22505 } 22506 iocp->ioc_count = 0; 22507 iocp->ioc_rval = 0; 22508 qreply(q, mp); 22509 return; 22510 } 22511 CALL_IP_WPUT(connp, q, mp); 22512 } 22513 22514 /* 22515 * This routine is called by tcp_wput() to handle all TPI requests. 22516 */ 22517 /* ARGSUSED */ 22518 static void 22519 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22520 { 22521 conn_t *connp = (conn_t *)arg; 22522 tcp_t *tcp = connp->conn_tcp; 22523 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22524 uchar_t *rptr; 22525 t_scalar_t type; 22526 int len; 22527 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 22528 22529 /* 22530 * Try and ASSERT the minimum possible references on the 22531 * conn early enough. Since we are executing on write side, 22532 * the connection is obviously not detached and that means 22533 * there is a ref each for TCP and IP. Since we are behind 22534 * the squeue, the minimum references needed are 3. If the 22535 * conn is in classifier hash list, there should be an 22536 * extra ref for that (we check both the possibilities). 22537 */ 22538 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22539 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22540 22541 rptr = mp->b_rptr; 22542 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22543 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22544 type = ((union T_primitives *)rptr)->type; 22545 if (type == T_EXDATA_REQ) { 22546 uint32_t msize = msgdsize(mp->b_cont); 22547 22548 len = msize - 1; 22549 if (len < 0) { 22550 freemsg(mp); 22551 return; 22552 } 22553 /* 22554 * Try to force urgent data out on the wire. 22555 * Even if we have unsent data this will 22556 * at least send the urgent flag. 22557 * XXX does not handle more flag correctly. 22558 */ 22559 len += tcp->tcp_unsent; 22560 len += tcp->tcp_snxt; 22561 tcp->tcp_urg = len; 22562 tcp->tcp_valid_bits |= TCP_URG_VALID; 22563 22564 /* Bypass tcp protocol for fused tcp loopback */ 22565 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 22566 return; 22567 } else if (type != T_DATA_REQ) { 22568 goto non_urgent_data; 22569 } 22570 /* TODO: options, flags, ... from user */ 22571 /* Set length to zero for reclamation below */ 22572 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22573 freeb(mp); 22574 return; 22575 } else { 22576 if (tcp->tcp_debug) { 22577 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22578 "tcp_wput_proto, dropping one..."); 22579 } 22580 freemsg(mp); 22581 return; 22582 } 22583 22584 non_urgent_data: 22585 22586 switch ((int)tprim->type) { 22587 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22588 /* 22589 * save the kssl_ent_t from the next block, and convert this 22590 * back to a normal bind_req. 22591 */ 22592 if (mp->b_cont != NULL) { 22593 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22594 22595 if (tcp->tcp_kssl_ent != NULL) { 22596 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22597 KSSL_NO_PROXY); 22598 tcp->tcp_kssl_ent = NULL; 22599 } 22600 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22601 sizeof (kssl_ent_t)); 22602 kssl_hold_ent(tcp->tcp_kssl_ent); 22603 freemsg(mp->b_cont); 22604 mp->b_cont = NULL; 22605 } 22606 tprim->type = T_BIND_REQ; 22607 22608 /* FALLTHROUGH */ 22609 case O_T_BIND_REQ: /* bind request */ 22610 case T_BIND_REQ: /* new semantics bind request */ 22611 tcp_bind(tcp, mp); 22612 break; 22613 case T_UNBIND_REQ: /* unbind request */ 22614 tcp_unbind(tcp, mp); 22615 break; 22616 case O_T_CONN_RES: /* old connection response XXX */ 22617 case T_CONN_RES: /* connection response */ 22618 tcp_accept(tcp, mp); 22619 break; 22620 case T_CONN_REQ: /* connection request */ 22621 tcp_connect(tcp, mp); 22622 break; 22623 case T_DISCON_REQ: /* disconnect request */ 22624 tcp_disconnect(tcp, mp); 22625 break; 22626 case T_CAPABILITY_REQ: 22627 tcp_capability_req(tcp, mp); /* capability request */ 22628 break; 22629 case T_INFO_REQ: /* information request */ 22630 tcp_info_req(tcp, mp); 22631 break; 22632 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22633 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, 22634 &tcp_opt_obj, B_TRUE); 22635 break; 22636 case T_OPTMGMT_REQ: 22637 /* 22638 * Note: no support for snmpcom_req() through new 22639 * T_OPTMGMT_REQ. See comments in ip.c 22640 */ 22641 /* Only IP is allowed to return meaningful value */ 22642 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22643 B_TRUE); 22644 break; 22645 22646 case T_UNITDATA_REQ: /* unitdata request */ 22647 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22648 break; 22649 case T_ORDREL_REQ: /* orderly release req */ 22650 freemsg(mp); 22651 22652 if (tcp->tcp_fused) 22653 tcp_unfuse(tcp); 22654 22655 if (tcp_xmit_end(tcp) != 0) { 22656 /* 22657 * We were crossing FINs and got a reset from 22658 * the other side. Just ignore it. 22659 */ 22660 if (tcp->tcp_debug) { 22661 (void) strlog(TCP_MOD_ID, 0, 1, 22662 SL_ERROR|SL_TRACE, 22663 "tcp_wput_proto, T_ORDREL_REQ out of " 22664 "state %s", 22665 tcp_display(tcp, NULL, 22666 DISP_ADDR_AND_PORT)); 22667 } 22668 } 22669 break; 22670 case T_ADDR_REQ: 22671 tcp_addr_req(tcp, mp); 22672 break; 22673 default: 22674 if (tcp->tcp_debug) { 22675 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22676 "tcp_wput_proto, bogus TPI msg, type %d", 22677 tprim->type); 22678 } 22679 /* 22680 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22681 * to recover. 22682 */ 22683 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22684 break; 22685 } 22686 } 22687 22688 /* 22689 * The TCP write service routine should never be called... 22690 */ 22691 /* ARGSUSED */ 22692 static void 22693 tcp_wsrv(queue_t *q) 22694 { 22695 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22696 22697 TCP_STAT(tcps, tcp_wsrv_called); 22698 } 22699 22700 /* Non overlapping byte exchanger */ 22701 static void 22702 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22703 { 22704 uchar_t uch; 22705 22706 while (len-- > 0) { 22707 uch = a[len]; 22708 a[len] = b[len]; 22709 b[len] = uch; 22710 } 22711 } 22712 22713 /* 22714 * Send out a control packet on the tcp connection specified. This routine 22715 * is typically called where we need a simple ACK or RST generated. 22716 */ 22717 static void 22718 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22719 { 22720 uchar_t *rptr; 22721 tcph_t *tcph; 22722 ipha_t *ipha = NULL; 22723 ip6_t *ip6h = NULL; 22724 uint32_t sum; 22725 int tcp_hdr_len; 22726 int tcp_ip_hdr_len; 22727 mblk_t *mp; 22728 tcp_stack_t *tcps = tcp->tcp_tcps; 22729 22730 /* 22731 * Save sum for use in source route later. 22732 */ 22733 ASSERT(tcp != NULL); 22734 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22735 tcp_hdr_len = tcp->tcp_hdr_len; 22736 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22737 22738 /* If a text string is passed in with the request, pass it to strlog. */ 22739 if (str != NULL && tcp->tcp_debug) { 22740 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22741 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22742 str, seq, ack, ctl); 22743 } 22744 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22745 BPRI_MED); 22746 if (mp == NULL) { 22747 return; 22748 } 22749 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22750 mp->b_rptr = rptr; 22751 mp->b_wptr = &rptr[tcp_hdr_len]; 22752 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22753 22754 if (tcp->tcp_ipversion == IPV4_VERSION) { 22755 ipha = (ipha_t *)rptr; 22756 ipha->ipha_length = htons(tcp_hdr_len); 22757 } else { 22758 ip6h = (ip6_t *)rptr; 22759 ASSERT(tcp != NULL); 22760 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22761 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22762 } 22763 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22764 tcph->th_flags[0] = (uint8_t)ctl; 22765 if (ctl & TH_RST) { 22766 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22767 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22768 /* 22769 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22770 */ 22771 if (tcp->tcp_snd_ts_ok && 22772 tcp->tcp_state > TCPS_SYN_SENT) { 22773 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22774 *(mp->b_wptr) = TCPOPT_EOL; 22775 if (tcp->tcp_ipversion == IPV4_VERSION) { 22776 ipha->ipha_length = htons(tcp_hdr_len - 22777 TCPOPT_REAL_TS_LEN); 22778 } else { 22779 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22780 TCPOPT_REAL_TS_LEN); 22781 } 22782 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22783 sum -= TCPOPT_REAL_TS_LEN; 22784 } 22785 } 22786 if (ctl & TH_ACK) { 22787 if (tcp->tcp_snd_ts_ok) { 22788 U32_TO_BE32(lbolt, 22789 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22790 U32_TO_BE32(tcp->tcp_ts_recent, 22791 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22792 } 22793 22794 /* Update the latest receive window size in TCP header. */ 22795 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22796 tcph->th_win); 22797 tcp->tcp_rack = ack; 22798 tcp->tcp_rack_cnt = 0; 22799 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22800 } 22801 BUMP_LOCAL(tcp->tcp_obsegs); 22802 U32_TO_BE32(seq, tcph->th_seq); 22803 U32_TO_BE32(ack, tcph->th_ack); 22804 /* 22805 * Include the adjustment for a source route if any. 22806 */ 22807 sum = (sum >> 16) + (sum & 0xFFFF); 22808 U16_TO_BE16(sum, tcph->th_sum); 22809 tcp_send_data(tcp, tcp->tcp_wq, mp); 22810 } 22811 22812 /* 22813 * If this routine returns B_TRUE, TCP can generate a RST in response 22814 * to a segment. If it returns B_FALSE, TCP should not respond. 22815 */ 22816 static boolean_t 22817 tcp_send_rst_chk(tcp_stack_t *tcps) 22818 { 22819 clock_t now; 22820 22821 /* 22822 * TCP needs to protect itself from generating too many RSTs. 22823 * This can be a DoS attack by sending us random segments 22824 * soliciting RSTs. 22825 * 22826 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22827 * in each 1 second interval. In this way, TCP still generate 22828 * RSTs in normal cases but when under attack, the impact is 22829 * limited. 22830 */ 22831 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22832 now = lbolt; 22833 /* lbolt can wrap around. */ 22834 if ((tcps->tcps_last_rst_intrvl > now) || 22835 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22836 1*SECONDS)) { 22837 tcps->tcps_last_rst_intrvl = now; 22838 tcps->tcps_rst_cnt = 1; 22839 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22840 return (B_FALSE); 22841 } 22842 } 22843 return (B_TRUE); 22844 } 22845 22846 /* 22847 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22848 */ 22849 static void 22850 tcp_ip_ire_mark_advice(tcp_t *tcp) 22851 { 22852 mblk_t *mp; 22853 ipic_t *ipic; 22854 22855 if (tcp->tcp_ipversion == IPV4_VERSION) { 22856 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22857 &ipic); 22858 } else { 22859 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22860 &ipic); 22861 } 22862 if (mp == NULL) 22863 return; 22864 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22865 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22866 } 22867 22868 /* 22869 * Return an IP advice ioctl mblk and set ipic to be the pointer 22870 * to the advice structure. 22871 */ 22872 static mblk_t * 22873 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22874 { 22875 struct iocblk *ioc; 22876 mblk_t *mp, *mp1; 22877 22878 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22879 if (mp == NULL) 22880 return (NULL); 22881 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22882 *ipic = (ipic_t *)mp->b_rptr; 22883 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22884 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22885 22886 bcopy(addr, *ipic + 1, addr_len); 22887 22888 (*ipic)->ipic_addr_length = addr_len; 22889 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22890 22891 mp1 = mkiocb(IP_IOCTL); 22892 if (mp1 == NULL) { 22893 freemsg(mp); 22894 return (NULL); 22895 } 22896 mp1->b_cont = mp; 22897 ioc = (struct iocblk *)mp1->b_rptr; 22898 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22899 22900 return (mp1); 22901 } 22902 22903 /* 22904 * Generate a reset based on an inbound packet, connp is set by caller 22905 * when RST is in response to an unexpected inbound packet for which 22906 * there is active tcp state in the system. 22907 * 22908 * IPSEC NOTE : Try to send the reply with the same protection as it came 22909 * in. We still have the ipsec_mp that the packet was attached to. Thus 22910 * the packet will go out at the same level of protection as it came in by 22911 * converting the IPSEC_IN to IPSEC_OUT. 22912 */ 22913 static void 22914 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22915 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22916 tcp_stack_t *tcps, conn_t *connp) 22917 { 22918 ipha_t *ipha = NULL; 22919 ip6_t *ip6h = NULL; 22920 ushort_t len; 22921 tcph_t *tcph; 22922 int i; 22923 mblk_t *ipsec_mp; 22924 boolean_t mctl_present; 22925 ipic_t *ipic; 22926 ipaddr_t v4addr; 22927 in6_addr_t v6addr; 22928 int addr_len; 22929 void *addr; 22930 queue_t *q = tcps->tcps_g_q; 22931 tcp_t *tcp; 22932 cred_t *cr; 22933 mblk_t *nmp; 22934 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22935 22936 if (tcps->tcps_g_q == NULL) { 22937 /* 22938 * For non-zero stackids the default queue isn't created 22939 * until the first open, thus there can be a need to send 22940 * a reset before then. But we can't do that, hence we just 22941 * drop the packet. Later during boot, when the default queue 22942 * has been setup, a retransmitted packet from the peer 22943 * will result in a reset. 22944 */ 22945 ASSERT(tcps->tcps_netstack->netstack_stackid != 22946 GLOBAL_NETSTACKID); 22947 freemsg(mp); 22948 return; 22949 } 22950 22951 if (connp != NULL) 22952 tcp = connp->conn_tcp; 22953 else 22954 tcp = Q_TO_TCP(q); 22955 22956 if (!tcp_send_rst_chk(tcps)) { 22957 tcps->tcps_rst_unsent++; 22958 freemsg(mp); 22959 return; 22960 } 22961 22962 if (mp->b_datap->db_type == M_CTL) { 22963 ipsec_mp = mp; 22964 mp = mp->b_cont; 22965 mctl_present = B_TRUE; 22966 } else { 22967 ipsec_mp = mp; 22968 mctl_present = B_FALSE; 22969 } 22970 22971 if (str && q && tcps->tcps_dbg) { 22972 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22973 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22974 "flags 0x%x", 22975 str, seq, ack, ctl); 22976 } 22977 if (mp->b_datap->db_ref != 1) { 22978 mblk_t *mp1 = copyb(mp); 22979 freemsg(mp); 22980 mp = mp1; 22981 if (!mp) { 22982 if (mctl_present) 22983 freeb(ipsec_mp); 22984 return; 22985 } else { 22986 if (mctl_present) { 22987 ipsec_mp->b_cont = mp; 22988 } else { 22989 ipsec_mp = mp; 22990 } 22991 } 22992 } else if (mp->b_cont) { 22993 freemsg(mp->b_cont); 22994 mp->b_cont = NULL; 22995 } 22996 /* 22997 * We skip reversing source route here. 22998 * (for now we replace all IP options with EOL) 22999 */ 23000 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23001 ipha = (ipha_t *)mp->b_rptr; 23002 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 23003 mp->b_rptr[i] = IPOPT_EOL; 23004 /* 23005 * Make sure that src address isn't flagrantly invalid. 23006 * Not all broadcast address checking for the src address 23007 * is possible, since we don't know the netmask of the src 23008 * addr. No check for destination address is done, since 23009 * IP will not pass up a packet with a broadcast dest 23010 * address to TCP. Similar checks are done below for IPv6. 23011 */ 23012 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 23013 CLASSD(ipha->ipha_src)) { 23014 freemsg(ipsec_mp); 23015 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 23016 return; 23017 } 23018 } else { 23019 ip6h = (ip6_t *)mp->b_rptr; 23020 23021 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 23022 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 23023 freemsg(ipsec_mp); 23024 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 23025 return; 23026 } 23027 23028 /* Remove any extension headers assuming partial overlay */ 23029 if (ip_hdr_len > IPV6_HDR_LEN) { 23030 uint8_t *to; 23031 23032 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 23033 ovbcopy(ip6h, to, IPV6_HDR_LEN); 23034 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 23035 ip_hdr_len = IPV6_HDR_LEN; 23036 ip6h = (ip6_t *)mp->b_rptr; 23037 ip6h->ip6_nxt = IPPROTO_TCP; 23038 } 23039 } 23040 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 23041 if (tcph->th_flags[0] & TH_RST) { 23042 freemsg(ipsec_mp); 23043 return; 23044 } 23045 tcph->th_offset_and_rsrvd[0] = (5 << 4); 23046 len = ip_hdr_len + sizeof (tcph_t); 23047 mp->b_wptr = &mp->b_rptr[len]; 23048 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23049 ipha->ipha_length = htons(len); 23050 /* Swap addresses */ 23051 v4addr = ipha->ipha_src; 23052 ipha->ipha_src = ipha->ipha_dst; 23053 ipha->ipha_dst = v4addr; 23054 ipha->ipha_ident = 0; 23055 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 23056 addr_len = IP_ADDR_LEN; 23057 addr = &v4addr; 23058 } else { 23059 /* No ip6i_t in this case */ 23060 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 23061 /* Swap addresses */ 23062 v6addr = ip6h->ip6_src; 23063 ip6h->ip6_src = ip6h->ip6_dst; 23064 ip6h->ip6_dst = v6addr; 23065 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 23066 addr_len = IPV6_ADDR_LEN; 23067 addr = &v6addr; 23068 } 23069 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 23070 U32_TO_BE32(ack, tcph->th_ack); 23071 U32_TO_BE32(seq, tcph->th_seq); 23072 U16_TO_BE16(0, tcph->th_win); 23073 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 23074 tcph->th_flags[0] = (uint8_t)ctl; 23075 if (ctl & TH_RST) { 23076 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 23077 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23078 } 23079 23080 /* IP trusts us to set up labels when required. */ 23081 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 23082 crgetlabel(cr) != NULL) { 23083 int err; 23084 23085 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 23086 err = tsol_check_label(cr, &mp, 23087 tcp->tcp_connp->conn_mac_exempt, 23088 tcps->tcps_netstack->netstack_ip); 23089 else 23090 err = tsol_check_label_v6(cr, &mp, 23091 tcp->tcp_connp->conn_mac_exempt, 23092 tcps->tcps_netstack->netstack_ip); 23093 if (mctl_present) 23094 ipsec_mp->b_cont = mp; 23095 else 23096 ipsec_mp = mp; 23097 if (err != 0) { 23098 freemsg(ipsec_mp); 23099 return; 23100 } 23101 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23102 ipha = (ipha_t *)mp->b_rptr; 23103 } else { 23104 ip6h = (ip6_t *)mp->b_rptr; 23105 } 23106 } 23107 23108 if (mctl_present) { 23109 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23110 23111 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23112 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 23113 return; 23114 } 23115 } 23116 if (zoneid == ALL_ZONES) 23117 zoneid = GLOBAL_ZONEID; 23118 23119 /* Add the zoneid so ip_output routes it properly */ 23120 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 23121 freemsg(ipsec_mp); 23122 return; 23123 } 23124 ipsec_mp = nmp; 23125 23126 /* 23127 * NOTE: one might consider tracing a TCP packet here, but 23128 * this function has no active TCP state and no tcp structure 23129 * that has a trace buffer. If we traced here, we would have 23130 * to keep a local trace buffer in tcp_record_trace(). 23131 * 23132 * TSol note: The mblk that contains the incoming packet was 23133 * reused by tcp_xmit_listener_reset, so it already contains 23134 * the right credentials and we don't need to call mblk_setcred. 23135 * Also the conn's cred is not right since it is associated 23136 * with tcps_g_q. 23137 */ 23138 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 23139 23140 /* 23141 * Tell IP to mark the IRE used for this destination temporary. 23142 * This way, we can limit our exposure to DoS attack because IP 23143 * creates an IRE for each destination. If there are too many, 23144 * the time to do any routing lookup will be extremely long. And 23145 * the lookup can be in interrupt context. 23146 * 23147 * Note that in normal circumstances, this marking should not 23148 * affect anything. It would be nice if only 1 message is 23149 * needed to inform IP that the IRE created for this RST should 23150 * not be added to the cache table. But there is currently 23151 * not such communication mechanism between TCP and IP. So 23152 * the best we can do now is to send the advice ioctl to IP 23153 * to mark the IRE temporary. 23154 */ 23155 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 23156 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 23157 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23158 } 23159 } 23160 23161 /* 23162 * Initiate closedown sequence on an active connection. (May be called as 23163 * writer.) Return value zero for OK return, non-zero for error return. 23164 */ 23165 static int 23166 tcp_xmit_end(tcp_t *tcp) 23167 { 23168 ipic_t *ipic; 23169 mblk_t *mp; 23170 tcp_stack_t *tcps = tcp->tcp_tcps; 23171 23172 if (tcp->tcp_state < TCPS_SYN_RCVD || 23173 tcp->tcp_state > TCPS_CLOSE_WAIT) { 23174 /* 23175 * Invalid state, only states TCPS_SYN_RCVD, 23176 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 23177 */ 23178 return (-1); 23179 } 23180 23181 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 23182 tcp->tcp_valid_bits |= TCP_FSS_VALID; 23183 /* 23184 * If there is nothing more unsent, send the FIN now. 23185 * Otherwise, it will go out with the last segment. 23186 */ 23187 if (tcp->tcp_unsent == 0) { 23188 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 23189 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 23190 23191 if (mp) { 23192 tcp_send_data(tcp, tcp->tcp_wq, mp); 23193 } else { 23194 /* 23195 * Couldn't allocate msg. Pretend we got it out. 23196 * Wait for rexmit timeout. 23197 */ 23198 tcp->tcp_snxt = tcp->tcp_fss + 1; 23199 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23200 } 23201 23202 /* 23203 * If needed, update tcp_rexmit_snxt as tcp_snxt is 23204 * changed. 23205 */ 23206 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 23207 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 23208 } 23209 } else { 23210 /* 23211 * If tcp->tcp_cork is set, then the data will not get sent, 23212 * so we have to check that and unset it first. 23213 */ 23214 if (tcp->tcp_cork) 23215 tcp->tcp_cork = B_FALSE; 23216 tcp_wput_data(tcp, NULL, B_FALSE); 23217 } 23218 23219 /* 23220 * If TCP does not get enough samples of RTT or tcp_rtt_updates 23221 * is 0, don't update the cache. 23222 */ 23223 if (tcps->tcps_rtt_updates == 0 || 23224 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 23225 return (0); 23226 23227 /* 23228 * NOTE: should not update if source routes i.e. if tcp_remote if 23229 * different from the destination. 23230 */ 23231 if (tcp->tcp_ipversion == IPV4_VERSION) { 23232 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 23233 return (0); 23234 } 23235 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 23236 &ipic); 23237 } else { 23238 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 23239 &tcp->tcp_ip6h->ip6_dst))) { 23240 return (0); 23241 } 23242 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 23243 &ipic); 23244 } 23245 23246 /* Record route attributes in the IRE for use by future connections. */ 23247 if (mp == NULL) 23248 return (0); 23249 23250 /* 23251 * We do not have a good algorithm to update ssthresh at this time. 23252 * So don't do any update. 23253 */ 23254 ipic->ipic_rtt = tcp->tcp_rtt_sa; 23255 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 23256 23257 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23258 return (0); 23259 } 23260 23261 /* 23262 * Generate a "no listener here" RST in response to an "unknown" segment. 23263 * connp is set by caller when RST is in response to an unexpected 23264 * inbound packet for which there is active tcp state in the system. 23265 * Note that we are reusing the incoming mp to construct the outgoing RST. 23266 */ 23267 void 23268 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 23269 tcp_stack_t *tcps, conn_t *connp) 23270 { 23271 uchar_t *rptr; 23272 uint32_t seg_len; 23273 tcph_t *tcph; 23274 uint32_t seg_seq; 23275 uint32_t seg_ack; 23276 uint_t flags; 23277 mblk_t *ipsec_mp; 23278 ipha_t *ipha; 23279 ip6_t *ip6h; 23280 boolean_t mctl_present = B_FALSE; 23281 boolean_t check = B_TRUE; 23282 boolean_t policy_present; 23283 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 23284 23285 TCP_STAT(tcps, tcp_no_listener); 23286 23287 ipsec_mp = mp; 23288 23289 if (mp->b_datap->db_type == M_CTL) { 23290 ipsec_in_t *ii; 23291 23292 mctl_present = B_TRUE; 23293 mp = mp->b_cont; 23294 23295 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23296 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23297 if (ii->ipsec_in_dont_check) { 23298 check = B_FALSE; 23299 if (!ii->ipsec_in_secure) { 23300 freeb(ipsec_mp); 23301 mctl_present = B_FALSE; 23302 ipsec_mp = mp; 23303 } 23304 } 23305 } 23306 23307 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23308 policy_present = ipss->ipsec_inbound_v4_policy_present; 23309 ipha = (ipha_t *)mp->b_rptr; 23310 ip6h = NULL; 23311 } else { 23312 policy_present = ipss->ipsec_inbound_v6_policy_present; 23313 ipha = NULL; 23314 ip6h = (ip6_t *)mp->b_rptr; 23315 } 23316 23317 if (check && policy_present) { 23318 /* 23319 * The conn_t parameter is NULL because we already know 23320 * nobody's home. 23321 */ 23322 ipsec_mp = ipsec_check_global_policy( 23323 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 23324 tcps->tcps_netstack); 23325 if (ipsec_mp == NULL) 23326 return; 23327 } 23328 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 23329 DTRACE_PROBE2( 23330 tx__ip__log__error__nolistener__tcp, 23331 char *, "Could not reply with RST to mp(1)", 23332 mblk_t *, mp); 23333 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 23334 freemsg(ipsec_mp); 23335 return; 23336 } 23337 23338 rptr = mp->b_rptr; 23339 23340 tcph = (tcph_t *)&rptr[ip_hdr_len]; 23341 seg_seq = BE32_TO_U32(tcph->th_seq); 23342 seg_ack = BE32_TO_U32(tcph->th_ack); 23343 flags = tcph->th_flags[0]; 23344 23345 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 23346 if (flags & TH_RST) { 23347 freemsg(ipsec_mp); 23348 } else if (flags & TH_ACK) { 23349 tcp_xmit_early_reset("no tcp, reset", 23350 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 23351 connp); 23352 } else { 23353 if (flags & TH_SYN) { 23354 seg_len++; 23355 } else { 23356 /* 23357 * Here we violate the RFC. Note that a normal 23358 * TCP will never send a segment without the ACK 23359 * flag, except for RST or SYN segment. This 23360 * segment is neither. Just drop it on the 23361 * floor. 23362 */ 23363 freemsg(ipsec_mp); 23364 tcps->tcps_rst_unsent++; 23365 return; 23366 } 23367 23368 tcp_xmit_early_reset("no tcp, reset/ack", 23369 ipsec_mp, 0, seg_seq + seg_len, 23370 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 23371 } 23372 } 23373 23374 /* 23375 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 23376 * ip and tcp header ready to pass down to IP. If the mp passed in is 23377 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 23378 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 23379 * otherwise it will dup partial mblks.) 23380 * Otherwise, an appropriate ACK packet will be generated. This 23381 * routine is not usually called to send new data for the first time. It 23382 * is mostly called out of the timer for retransmits, and to generate ACKs. 23383 * 23384 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 23385 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 23386 * of the original mblk chain will be returned in *offset and *end_mp. 23387 */ 23388 mblk_t * 23389 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 23390 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 23391 boolean_t rexmit) 23392 { 23393 int data_length; 23394 int32_t off = 0; 23395 uint_t flags; 23396 mblk_t *mp1; 23397 mblk_t *mp2; 23398 uchar_t *rptr; 23399 tcph_t *tcph; 23400 int32_t num_sack_blk = 0; 23401 int32_t sack_opt_len = 0; 23402 tcp_stack_t *tcps = tcp->tcp_tcps; 23403 23404 /* Allocate for our maximum TCP header + link-level */ 23405 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 23406 tcps->tcps_wroff_xtra, BPRI_MED); 23407 if (!mp1) 23408 return (NULL); 23409 data_length = 0; 23410 23411 /* 23412 * Note that tcp_mss has been adjusted to take into account the 23413 * timestamp option if applicable. Because SACK options do not 23414 * appear in every TCP segments and they are of variable lengths, 23415 * they cannot be included in tcp_mss. Thus we need to calculate 23416 * the actual segment length when we need to send a segment which 23417 * includes SACK options. 23418 */ 23419 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23420 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23421 tcp->tcp_num_sack_blk); 23422 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23423 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23424 if (max_to_send + sack_opt_len > tcp->tcp_mss) 23425 max_to_send -= sack_opt_len; 23426 } 23427 23428 if (offset != NULL) { 23429 off = *offset; 23430 /* We use offset as an indicator that end_mp is not NULL. */ 23431 *end_mp = NULL; 23432 } 23433 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 23434 /* This could be faster with cooperation from downstream */ 23435 if (mp2 != mp1 && !sendall && 23436 data_length + (int)(mp->b_wptr - mp->b_rptr) > 23437 max_to_send) 23438 /* 23439 * Don't send the next mblk since the whole mblk 23440 * does not fit. 23441 */ 23442 break; 23443 mp2->b_cont = dupb(mp); 23444 mp2 = mp2->b_cont; 23445 if (!mp2) { 23446 freemsg(mp1); 23447 return (NULL); 23448 } 23449 mp2->b_rptr += off; 23450 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 23451 (uintptr_t)INT_MAX); 23452 23453 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 23454 if (data_length > max_to_send) { 23455 mp2->b_wptr -= data_length - max_to_send; 23456 data_length = max_to_send; 23457 off = mp2->b_wptr - mp->b_rptr; 23458 break; 23459 } else { 23460 off = 0; 23461 } 23462 } 23463 if (offset != NULL) { 23464 *offset = off; 23465 *end_mp = mp; 23466 } 23467 if (seg_len != NULL) { 23468 *seg_len = data_length; 23469 } 23470 23471 /* Update the latest receive window size in TCP header. */ 23472 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23473 tcp->tcp_tcph->th_win); 23474 23475 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23476 mp1->b_rptr = rptr; 23477 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 23478 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23479 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23480 U32_TO_ABE32(seq, tcph->th_seq); 23481 23482 /* 23483 * Use tcp_unsent to determine if the PUSH bit should be used assumes 23484 * that this function was called from tcp_wput_data. Thus, when called 23485 * to retransmit data the setting of the PUSH bit may appear some 23486 * what random in that it might get set when it should not. This 23487 * should not pose any performance issues. 23488 */ 23489 if (data_length != 0 && (tcp->tcp_unsent == 0 || 23490 tcp->tcp_unsent == data_length)) { 23491 flags = TH_ACK | TH_PUSH; 23492 } else { 23493 flags = TH_ACK; 23494 } 23495 23496 if (tcp->tcp_ecn_ok) { 23497 if (tcp->tcp_ecn_echo_on) 23498 flags |= TH_ECE; 23499 23500 /* 23501 * Only set ECT bit and ECN_CWR if a segment contains new data. 23502 * There is no TCP flow control for non-data segments, and 23503 * only data segment is transmitted reliably. 23504 */ 23505 if (data_length > 0 && !rexmit) { 23506 SET_ECT(tcp, rptr); 23507 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23508 flags |= TH_CWR; 23509 tcp->tcp_ecn_cwr_sent = B_TRUE; 23510 } 23511 } 23512 } 23513 23514 if (tcp->tcp_valid_bits) { 23515 uint32_t u1; 23516 23517 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23518 seq == tcp->tcp_iss) { 23519 uchar_t *wptr; 23520 23521 /* 23522 * If TCP_ISS_VALID and the seq number is tcp_iss, 23523 * TCP can only be in SYN-SENT, SYN-RCVD or 23524 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23525 * our SYN is not ack'ed but the app closes this 23526 * TCP connection. 23527 */ 23528 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23529 tcp->tcp_state == TCPS_SYN_RCVD || 23530 tcp->tcp_state == TCPS_FIN_WAIT_1); 23531 23532 /* 23533 * Tack on the MSS option. It is always needed 23534 * for both active and passive open. 23535 * 23536 * MSS option value should be interface MTU - MIN 23537 * TCP/IP header according to RFC 793 as it means 23538 * the maximum segment size TCP can receive. But 23539 * to get around some broken middle boxes/end hosts 23540 * out there, we allow the option value to be the 23541 * same as the MSS option size on the peer side. 23542 * In this way, the other side will not send 23543 * anything larger than they can receive. 23544 * 23545 * Note that for SYN_SENT state, the ndd param 23546 * tcp_use_smss_as_mss_opt has no effect as we 23547 * don't know the peer's MSS option value. So 23548 * the only case we need to take care of is in 23549 * SYN_RCVD state, which is done later. 23550 */ 23551 wptr = mp1->b_wptr; 23552 wptr[0] = TCPOPT_MAXSEG; 23553 wptr[1] = TCPOPT_MAXSEG_LEN; 23554 wptr += 2; 23555 u1 = tcp->tcp_if_mtu - 23556 (tcp->tcp_ipversion == IPV4_VERSION ? 23557 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23558 TCP_MIN_HEADER_LENGTH; 23559 U16_TO_BE16(u1, wptr); 23560 mp1->b_wptr = wptr + 2; 23561 /* Update the offset to cover the additional word */ 23562 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23563 23564 /* 23565 * Note that the following way of filling in 23566 * TCP options are not optimal. Some NOPs can 23567 * be saved. But there is no need at this time 23568 * to optimize it. When it is needed, we will 23569 * do it. 23570 */ 23571 switch (tcp->tcp_state) { 23572 case TCPS_SYN_SENT: 23573 flags = TH_SYN; 23574 23575 if (tcp->tcp_snd_ts_ok) { 23576 uint32_t llbolt = (uint32_t)lbolt; 23577 23578 wptr = mp1->b_wptr; 23579 wptr[0] = TCPOPT_NOP; 23580 wptr[1] = TCPOPT_NOP; 23581 wptr[2] = TCPOPT_TSTAMP; 23582 wptr[3] = TCPOPT_TSTAMP_LEN; 23583 wptr += 4; 23584 U32_TO_BE32(llbolt, wptr); 23585 wptr += 4; 23586 ASSERT(tcp->tcp_ts_recent == 0); 23587 U32_TO_BE32(0L, wptr); 23588 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23589 tcph->th_offset_and_rsrvd[0] += 23590 (3 << 4); 23591 } 23592 23593 /* 23594 * Set up all the bits to tell other side 23595 * we are ECN capable. 23596 */ 23597 if (tcp->tcp_ecn_ok) { 23598 flags |= (TH_ECE | TH_CWR); 23599 } 23600 break; 23601 case TCPS_SYN_RCVD: 23602 flags |= TH_SYN; 23603 23604 /* 23605 * Reset the MSS option value to be SMSS 23606 * We should probably add back the bytes 23607 * for timestamp option and IPsec. We 23608 * don't do that as this is a workaround 23609 * for broken middle boxes/end hosts, it 23610 * is better for us to be more cautious. 23611 * They may not take these things into 23612 * account in their SMSS calculation. Thus 23613 * the peer's calculated SMSS may be smaller 23614 * than what it can be. This should be OK. 23615 */ 23616 if (tcps->tcps_use_smss_as_mss_opt) { 23617 u1 = tcp->tcp_mss; 23618 U16_TO_BE16(u1, wptr); 23619 } 23620 23621 /* 23622 * If the other side is ECN capable, reply 23623 * that we are also ECN capable. 23624 */ 23625 if (tcp->tcp_ecn_ok) 23626 flags |= TH_ECE; 23627 break; 23628 default: 23629 /* 23630 * The above ASSERT() makes sure that this 23631 * must be FIN-WAIT-1 state. Our SYN has 23632 * not been ack'ed so retransmit it. 23633 */ 23634 flags |= TH_SYN; 23635 break; 23636 } 23637 23638 if (tcp->tcp_snd_ws_ok) { 23639 wptr = mp1->b_wptr; 23640 wptr[0] = TCPOPT_NOP; 23641 wptr[1] = TCPOPT_WSCALE; 23642 wptr[2] = TCPOPT_WS_LEN; 23643 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23644 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23645 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23646 } 23647 23648 if (tcp->tcp_snd_sack_ok) { 23649 wptr = mp1->b_wptr; 23650 wptr[0] = TCPOPT_NOP; 23651 wptr[1] = TCPOPT_NOP; 23652 wptr[2] = TCPOPT_SACK_PERMITTED; 23653 wptr[3] = TCPOPT_SACK_OK_LEN; 23654 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23655 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23656 } 23657 23658 /* allocb() of adequate mblk assures space */ 23659 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23660 (uintptr_t)INT_MAX); 23661 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23662 /* 23663 * Get IP set to checksum on our behalf 23664 * Include the adjustment for a source route if any. 23665 */ 23666 u1 += tcp->tcp_sum; 23667 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23668 U16_TO_BE16(u1, tcph->th_sum); 23669 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23670 } 23671 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23672 (seq + data_length) == tcp->tcp_fss) { 23673 if (!tcp->tcp_fin_acked) { 23674 flags |= TH_FIN; 23675 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23676 } 23677 if (!tcp->tcp_fin_sent) { 23678 tcp->tcp_fin_sent = B_TRUE; 23679 switch (tcp->tcp_state) { 23680 case TCPS_SYN_RCVD: 23681 case TCPS_ESTABLISHED: 23682 tcp->tcp_state = TCPS_FIN_WAIT_1; 23683 break; 23684 case TCPS_CLOSE_WAIT: 23685 tcp->tcp_state = TCPS_LAST_ACK; 23686 break; 23687 } 23688 if (tcp->tcp_suna == tcp->tcp_snxt) 23689 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23690 tcp->tcp_snxt = tcp->tcp_fss + 1; 23691 } 23692 } 23693 /* 23694 * Note the trick here. u1 is unsigned. When tcp_urg 23695 * is smaller than seq, u1 will become a very huge value. 23696 * So the comparison will fail. Also note that tcp_urp 23697 * should be positive, see RFC 793 page 17. 23698 */ 23699 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23700 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23701 u1 < (uint32_t)(64 * 1024)) { 23702 flags |= TH_URG; 23703 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23704 U32_TO_ABE16(u1, tcph->th_urp); 23705 } 23706 } 23707 tcph->th_flags[0] = (uchar_t)flags; 23708 tcp->tcp_rack = tcp->tcp_rnxt; 23709 tcp->tcp_rack_cnt = 0; 23710 23711 if (tcp->tcp_snd_ts_ok) { 23712 if (tcp->tcp_state != TCPS_SYN_SENT) { 23713 uint32_t llbolt = (uint32_t)lbolt; 23714 23715 U32_TO_BE32(llbolt, 23716 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23717 U32_TO_BE32(tcp->tcp_ts_recent, 23718 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23719 } 23720 } 23721 23722 if (num_sack_blk > 0) { 23723 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23724 sack_blk_t *tmp; 23725 int32_t i; 23726 23727 wptr[0] = TCPOPT_NOP; 23728 wptr[1] = TCPOPT_NOP; 23729 wptr[2] = TCPOPT_SACK; 23730 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23731 sizeof (sack_blk_t); 23732 wptr += TCPOPT_REAL_SACK_LEN; 23733 23734 tmp = tcp->tcp_sack_list; 23735 for (i = 0; i < num_sack_blk; i++) { 23736 U32_TO_BE32(tmp[i].begin, wptr); 23737 wptr += sizeof (tcp_seq); 23738 U32_TO_BE32(tmp[i].end, wptr); 23739 wptr += sizeof (tcp_seq); 23740 } 23741 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23742 } 23743 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23744 data_length += (int)(mp1->b_wptr - rptr); 23745 if (tcp->tcp_ipversion == IPV4_VERSION) { 23746 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23747 } else { 23748 ip6_t *ip6 = (ip6_t *)(rptr + 23749 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23750 sizeof (ip6i_t) : 0)); 23751 23752 ip6->ip6_plen = htons(data_length - 23753 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23754 } 23755 23756 /* 23757 * Prime pump for IP 23758 * Include the adjustment for a source route if any. 23759 */ 23760 data_length -= tcp->tcp_ip_hdr_len; 23761 data_length += tcp->tcp_sum; 23762 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23763 U16_TO_ABE16(data_length, tcph->th_sum); 23764 if (tcp->tcp_ip_forward_progress) { 23765 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23766 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23767 tcp->tcp_ip_forward_progress = B_FALSE; 23768 } 23769 return (mp1); 23770 } 23771 23772 /* This function handles the push timeout. */ 23773 void 23774 tcp_push_timer(void *arg) 23775 { 23776 conn_t *connp = (conn_t *)arg; 23777 tcp_t *tcp = connp->conn_tcp; 23778 tcp_stack_t *tcps = tcp->tcp_tcps; 23779 uint_t flags; 23780 sodirect_t *sodp; 23781 23782 TCP_DBGSTAT(tcps, tcp_push_timer_cnt); 23783 23784 ASSERT(tcp->tcp_listener == NULL); 23785 23786 /* 23787 * We need to plug synchronous streams during our drain to prevent 23788 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23789 */ 23790 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23791 tcp->tcp_push_tid = 0; 23792 23793 SOD_PTR_ENTER(tcp, sodp); 23794 if (sodp != NULL) { 23795 flags = tcp_rcv_sod_wakeup(tcp, sodp); 23796 /* sod_wakeup() does the mutex_exit() */ 23797 } else if (tcp->tcp_rcv_list != NULL) { 23798 flags = tcp_rcv_drain(tcp->tcp_rq, tcp); 23799 } 23800 if (flags == TH_ACK_NEEDED) 23801 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23802 23803 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23804 } 23805 23806 /* 23807 * This function handles delayed ACK timeout. 23808 */ 23809 static void 23810 tcp_ack_timer(void *arg) 23811 { 23812 conn_t *connp = (conn_t *)arg; 23813 tcp_t *tcp = connp->conn_tcp; 23814 mblk_t *mp; 23815 tcp_stack_t *tcps = tcp->tcp_tcps; 23816 23817 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23818 23819 tcp->tcp_ack_tid = 0; 23820 23821 if (tcp->tcp_fused) 23822 return; 23823 23824 /* 23825 * Do not send ACK if there is no outstanding unack'ed data. 23826 */ 23827 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23828 return; 23829 } 23830 23831 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23832 /* 23833 * Make sure we don't allow deferred ACKs to result in 23834 * timer-based ACKing. If we have held off an ACK 23835 * when there was more than an mss here, and the timer 23836 * goes off, we have to worry about the possibility 23837 * that the sender isn't doing slow-start, or is out 23838 * of step with us for some other reason. We fall 23839 * permanently back in the direction of 23840 * ACK-every-other-packet as suggested in RFC 1122. 23841 */ 23842 if (tcp->tcp_rack_abs_max > 2) 23843 tcp->tcp_rack_abs_max--; 23844 tcp->tcp_rack_cur_max = 2; 23845 } 23846 mp = tcp_ack_mp(tcp); 23847 23848 if (mp != NULL) { 23849 BUMP_LOCAL(tcp->tcp_obsegs); 23850 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23851 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23852 tcp_send_data(tcp, tcp->tcp_wq, mp); 23853 } 23854 } 23855 23856 23857 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23858 static mblk_t * 23859 tcp_ack_mp(tcp_t *tcp) 23860 { 23861 uint32_t seq_no; 23862 tcp_stack_t *tcps = tcp->tcp_tcps; 23863 23864 /* 23865 * There are a few cases to be considered while setting the sequence no. 23866 * Essentially, we can come here while processing an unacceptable pkt 23867 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23868 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23869 * If we are here for a zero window probe, stick with suna. In all 23870 * other cases, we check if suna + swnd encompasses snxt and set 23871 * the sequence number to snxt, if so. If snxt falls outside the 23872 * window (the receiver probably shrunk its window), we will go with 23873 * suna + swnd, otherwise the sequence no will be unacceptable to the 23874 * receiver. 23875 */ 23876 if (tcp->tcp_zero_win_probe) { 23877 seq_no = tcp->tcp_suna; 23878 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23879 ASSERT(tcp->tcp_swnd == 0); 23880 seq_no = tcp->tcp_snxt; 23881 } else { 23882 seq_no = SEQ_GT(tcp->tcp_snxt, 23883 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23884 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23885 } 23886 23887 if (tcp->tcp_valid_bits) { 23888 /* 23889 * For the complex case where we have to send some 23890 * controls (FIN or SYN), let tcp_xmit_mp do it. 23891 */ 23892 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23893 NULL, B_FALSE)); 23894 } else { 23895 /* Generate a simple ACK */ 23896 int data_length; 23897 uchar_t *rptr; 23898 tcph_t *tcph; 23899 mblk_t *mp1; 23900 int32_t tcp_hdr_len; 23901 int32_t tcp_tcp_hdr_len; 23902 int32_t num_sack_blk = 0; 23903 int32_t sack_opt_len; 23904 23905 /* 23906 * Allocate space for TCP + IP headers 23907 * and link-level header 23908 */ 23909 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23910 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23911 tcp->tcp_num_sack_blk); 23912 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23913 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23914 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23915 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23916 } else { 23917 tcp_hdr_len = tcp->tcp_hdr_len; 23918 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23919 } 23920 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23921 if (!mp1) 23922 return (NULL); 23923 23924 /* Update the latest receive window size in TCP header. */ 23925 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23926 tcp->tcp_tcph->th_win); 23927 /* copy in prototype TCP + IP header */ 23928 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23929 mp1->b_rptr = rptr; 23930 mp1->b_wptr = rptr + tcp_hdr_len; 23931 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23932 23933 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23934 23935 /* Set the TCP sequence number. */ 23936 U32_TO_ABE32(seq_no, tcph->th_seq); 23937 23938 /* Set up the TCP flag field. */ 23939 tcph->th_flags[0] = (uchar_t)TH_ACK; 23940 if (tcp->tcp_ecn_echo_on) 23941 tcph->th_flags[0] |= TH_ECE; 23942 23943 tcp->tcp_rack = tcp->tcp_rnxt; 23944 tcp->tcp_rack_cnt = 0; 23945 23946 /* fill in timestamp option if in use */ 23947 if (tcp->tcp_snd_ts_ok) { 23948 uint32_t llbolt = (uint32_t)lbolt; 23949 23950 U32_TO_BE32(llbolt, 23951 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23952 U32_TO_BE32(tcp->tcp_ts_recent, 23953 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23954 } 23955 23956 /* Fill in SACK options */ 23957 if (num_sack_blk > 0) { 23958 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23959 sack_blk_t *tmp; 23960 int32_t i; 23961 23962 wptr[0] = TCPOPT_NOP; 23963 wptr[1] = TCPOPT_NOP; 23964 wptr[2] = TCPOPT_SACK; 23965 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23966 sizeof (sack_blk_t); 23967 wptr += TCPOPT_REAL_SACK_LEN; 23968 23969 tmp = tcp->tcp_sack_list; 23970 for (i = 0; i < num_sack_blk; i++) { 23971 U32_TO_BE32(tmp[i].begin, wptr); 23972 wptr += sizeof (tcp_seq); 23973 U32_TO_BE32(tmp[i].end, wptr); 23974 wptr += sizeof (tcp_seq); 23975 } 23976 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23977 << 4); 23978 } 23979 23980 if (tcp->tcp_ipversion == IPV4_VERSION) { 23981 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23982 } else { 23983 /* Check for ip6i_t header in sticky hdrs */ 23984 ip6_t *ip6 = (ip6_t *)(rptr + 23985 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23986 sizeof (ip6i_t) : 0)); 23987 23988 ip6->ip6_plen = htons(tcp_hdr_len - 23989 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23990 } 23991 23992 /* 23993 * Prime pump for checksum calculation in IP. Include the 23994 * adjustment for a source route if any. 23995 */ 23996 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23997 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23998 U16_TO_ABE16(data_length, tcph->th_sum); 23999 24000 if (tcp->tcp_ip_forward_progress) { 24001 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 24002 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 24003 tcp->tcp_ip_forward_progress = B_FALSE; 24004 } 24005 return (mp1); 24006 } 24007 } 24008 24009 /* 24010 * Hash list insertion routine for tcp_t structures. 24011 * Inserts entries with the ones bound to a specific IP address first 24012 * followed by those bound to INADDR_ANY. 24013 */ 24014 static void 24015 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 24016 { 24017 tcp_t **tcpp; 24018 tcp_t *tcpnext; 24019 24020 if (tcp->tcp_ptpbhn != NULL) { 24021 ASSERT(!caller_holds_lock); 24022 tcp_bind_hash_remove(tcp); 24023 } 24024 tcpp = &tbf->tf_tcp; 24025 if (!caller_holds_lock) { 24026 mutex_enter(&tbf->tf_lock); 24027 } else { 24028 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 24029 } 24030 tcpnext = tcpp[0]; 24031 if (tcpnext) { 24032 /* 24033 * If the new tcp bound to the INADDR_ANY address 24034 * and the first one in the list is not bound to 24035 * INADDR_ANY we skip all entries until we find the 24036 * first one bound to INADDR_ANY. 24037 * This makes sure that applications binding to a 24038 * specific address get preference over those binding to 24039 * INADDR_ANY. 24040 */ 24041 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 24042 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 24043 while ((tcpnext = tcpp[0]) != NULL && 24044 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 24045 tcpp = &(tcpnext->tcp_bind_hash); 24046 if (tcpnext) 24047 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 24048 } else 24049 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 24050 } 24051 tcp->tcp_bind_hash = tcpnext; 24052 tcp->tcp_ptpbhn = tcpp; 24053 tcpp[0] = tcp; 24054 if (!caller_holds_lock) 24055 mutex_exit(&tbf->tf_lock); 24056 } 24057 24058 /* 24059 * Hash list removal routine for tcp_t structures. 24060 */ 24061 static void 24062 tcp_bind_hash_remove(tcp_t *tcp) 24063 { 24064 tcp_t *tcpnext; 24065 kmutex_t *lockp; 24066 tcp_stack_t *tcps = tcp->tcp_tcps; 24067 24068 if (tcp->tcp_ptpbhn == NULL) 24069 return; 24070 24071 /* 24072 * Extract the lock pointer in case there are concurrent 24073 * hash_remove's for this instance. 24074 */ 24075 ASSERT(tcp->tcp_lport != 0); 24076 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 24077 24078 ASSERT(lockp != NULL); 24079 mutex_enter(lockp); 24080 if (tcp->tcp_ptpbhn) { 24081 tcpnext = tcp->tcp_bind_hash; 24082 if (tcpnext) { 24083 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 24084 tcp->tcp_bind_hash = NULL; 24085 } 24086 *tcp->tcp_ptpbhn = tcpnext; 24087 tcp->tcp_ptpbhn = NULL; 24088 } 24089 mutex_exit(lockp); 24090 } 24091 24092 24093 /* 24094 * Hash list lookup routine for tcp_t structures. 24095 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 24096 */ 24097 static tcp_t * 24098 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 24099 { 24100 tf_t *tf; 24101 tcp_t *tcp; 24102 24103 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24104 mutex_enter(&tf->tf_lock); 24105 for (tcp = tf->tf_tcp; tcp != NULL; 24106 tcp = tcp->tcp_acceptor_hash) { 24107 if (tcp->tcp_acceptor_id == id) { 24108 CONN_INC_REF(tcp->tcp_connp); 24109 mutex_exit(&tf->tf_lock); 24110 return (tcp); 24111 } 24112 } 24113 mutex_exit(&tf->tf_lock); 24114 return (NULL); 24115 } 24116 24117 24118 /* 24119 * Hash list insertion routine for tcp_t structures. 24120 */ 24121 void 24122 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 24123 { 24124 tf_t *tf; 24125 tcp_t **tcpp; 24126 tcp_t *tcpnext; 24127 tcp_stack_t *tcps = tcp->tcp_tcps; 24128 24129 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24130 24131 if (tcp->tcp_ptpahn != NULL) 24132 tcp_acceptor_hash_remove(tcp); 24133 tcpp = &tf->tf_tcp; 24134 mutex_enter(&tf->tf_lock); 24135 tcpnext = tcpp[0]; 24136 if (tcpnext) 24137 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 24138 tcp->tcp_acceptor_hash = tcpnext; 24139 tcp->tcp_ptpahn = tcpp; 24140 tcpp[0] = tcp; 24141 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 24142 mutex_exit(&tf->tf_lock); 24143 } 24144 24145 /* 24146 * Hash list removal routine for tcp_t structures. 24147 */ 24148 static void 24149 tcp_acceptor_hash_remove(tcp_t *tcp) 24150 { 24151 tcp_t *tcpnext; 24152 kmutex_t *lockp; 24153 24154 /* 24155 * Extract the lock pointer in case there are concurrent 24156 * hash_remove's for this instance. 24157 */ 24158 lockp = tcp->tcp_acceptor_lockp; 24159 24160 if (tcp->tcp_ptpahn == NULL) 24161 return; 24162 24163 ASSERT(lockp != NULL); 24164 mutex_enter(lockp); 24165 if (tcp->tcp_ptpahn) { 24166 tcpnext = tcp->tcp_acceptor_hash; 24167 if (tcpnext) { 24168 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24169 tcp->tcp_acceptor_hash = NULL; 24170 } 24171 *tcp->tcp_ptpahn = tcpnext; 24172 tcp->tcp_ptpahn = NULL; 24173 } 24174 mutex_exit(lockp); 24175 tcp->tcp_acceptor_lockp = NULL; 24176 } 24177 24178 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24179 24180 static ipaddr_t netmasks[] = { 24181 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24182 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24183 }; 24184 24185 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24186 24187 /* 24188 * XXX This routine should go away and instead we should use the metrics 24189 * associated with the routes to determine the default sndspace and rcvspace. 24190 */ 24191 static tcp_hsp_t * 24192 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24193 { 24194 tcp_hsp_t *hsp = NULL; 24195 24196 /* Quick check without acquiring the lock. */ 24197 if (tcps->tcps_hsp_hash == NULL) 24198 return (NULL); 24199 24200 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24201 24202 /* This routine finds the best-matching HSP for address addr. */ 24203 24204 if (tcps->tcps_hsp_hash) { 24205 int i; 24206 ipaddr_t srchaddr; 24207 tcp_hsp_t *hsp_net; 24208 24209 /* We do three passes: host, network, and subnet. */ 24210 24211 srchaddr = addr; 24212 24213 for (i = 1; i <= 3; i++) { 24214 /* Look for exact match on srchaddr */ 24215 24216 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24217 while (hsp) { 24218 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24219 hsp->tcp_hsp_addr == srchaddr) 24220 break; 24221 hsp = hsp->tcp_hsp_next; 24222 } 24223 ASSERT(hsp == NULL || 24224 hsp->tcp_hsp_vers == IPV4_VERSION); 24225 24226 /* 24227 * If this is the first pass: 24228 * If we found a match, great, return it. 24229 * If not, search for the network on the second pass. 24230 */ 24231 24232 if (i == 1) 24233 if (hsp) 24234 break; 24235 else 24236 { 24237 srchaddr = addr & netmask(addr); 24238 continue; 24239 } 24240 24241 /* 24242 * If this is the second pass: 24243 * If we found a match, but there's a subnet mask, 24244 * save the match but try again using the subnet 24245 * mask on the third pass. 24246 * Otherwise, return whatever we found. 24247 */ 24248 24249 if (i == 2) { 24250 if (hsp && hsp->tcp_hsp_subnet) { 24251 hsp_net = hsp; 24252 srchaddr = addr & hsp->tcp_hsp_subnet; 24253 continue; 24254 } else { 24255 break; 24256 } 24257 } 24258 24259 /* 24260 * This must be the third pass. If we didn't find 24261 * anything, return the saved network HSP instead. 24262 */ 24263 24264 if (!hsp) 24265 hsp = hsp_net; 24266 } 24267 } 24268 24269 rw_exit(&tcps->tcps_hsp_lock); 24270 return (hsp); 24271 } 24272 24273 /* 24274 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24275 * match lookup. 24276 */ 24277 static tcp_hsp_t * 24278 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24279 { 24280 tcp_hsp_t *hsp = NULL; 24281 24282 /* Quick check without acquiring the lock. */ 24283 if (tcps->tcps_hsp_hash == NULL) 24284 return (NULL); 24285 24286 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24287 24288 /* This routine finds the best-matching HSP for address addr. */ 24289 24290 if (tcps->tcps_hsp_hash) { 24291 int i; 24292 in6_addr_t v6srchaddr; 24293 tcp_hsp_t *hsp_net; 24294 24295 /* We do three passes: host, network, and subnet. */ 24296 24297 v6srchaddr = *v6addr; 24298 24299 for (i = 1; i <= 3; i++) { 24300 /* Look for exact match on srchaddr */ 24301 24302 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24303 V4_PART_OF_V6(v6srchaddr))]; 24304 while (hsp) { 24305 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24306 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24307 &v6srchaddr)) 24308 break; 24309 hsp = hsp->tcp_hsp_next; 24310 } 24311 24312 /* 24313 * If this is the first pass: 24314 * If we found a match, great, return it. 24315 * If not, search for the network on the second pass. 24316 */ 24317 24318 if (i == 1) 24319 if (hsp) 24320 break; 24321 else { 24322 /* Assume a 64 bit mask */ 24323 v6srchaddr.s6_addr32[0] = 24324 v6addr->s6_addr32[0]; 24325 v6srchaddr.s6_addr32[1] = 24326 v6addr->s6_addr32[1]; 24327 v6srchaddr.s6_addr32[2] = 0; 24328 v6srchaddr.s6_addr32[3] = 0; 24329 continue; 24330 } 24331 24332 /* 24333 * If this is the second pass: 24334 * If we found a match, but there's a subnet mask, 24335 * save the match but try again using the subnet 24336 * mask on the third pass. 24337 * Otherwise, return whatever we found. 24338 */ 24339 24340 if (i == 2) { 24341 ASSERT(hsp == NULL || 24342 hsp->tcp_hsp_vers == IPV6_VERSION); 24343 if (hsp && 24344 !IN6_IS_ADDR_UNSPECIFIED( 24345 &hsp->tcp_hsp_subnet_v6)) { 24346 hsp_net = hsp; 24347 V6_MASK_COPY(*v6addr, 24348 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24349 continue; 24350 } else { 24351 break; 24352 } 24353 } 24354 24355 /* 24356 * This must be the third pass. If we didn't find 24357 * anything, return the saved network HSP instead. 24358 */ 24359 24360 if (!hsp) 24361 hsp = hsp_net; 24362 } 24363 } 24364 24365 rw_exit(&tcps->tcps_hsp_lock); 24366 return (hsp); 24367 } 24368 24369 /* 24370 * Type three generator adapted from the random() function in 4.4 BSD: 24371 */ 24372 24373 /* 24374 * Copyright (c) 1983, 1993 24375 * The Regents of the University of California. All rights reserved. 24376 * 24377 * Redistribution and use in source and binary forms, with or without 24378 * modification, are permitted provided that the following conditions 24379 * are met: 24380 * 1. Redistributions of source code must retain the above copyright 24381 * notice, this list of conditions and the following disclaimer. 24382 * 2. Redistributions in binary form must reproduce the above copyright 24383 * notice, this list of conditions and the following disclaimer in the 24384 * documentation and/or other materials provided with the distribution. 24385 * 3. All advertising materials mentioning features or use of this software 24386 * must display the following acknowledgement: 24387 * This product includes software developed by the University of 24388 * California, Berkeley and its contributors. 24389 * 4. Neither the name of the University nor the names of its contributors 24390 * may be used to endorse or promote products derived from this software 24391 * without specific prior written permission. 24392 * 24393 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24394 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24395 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24396 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24397 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24398 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24399 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24400 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24401 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24402 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24403 * SUCH DAMAGE. 24404 */ 24405 24406 /* Type 3 -- x**31 + x**3 + 1 */ 24407 #define DEG_3 31 24408 #define SEP_3 3 24409 24410 24411 /* Protected by tcp_random_lock */ 24412 static int tcp_randtbl[DEG_3 + 1]; 24413 24414 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24415 static int *tcp_random_rptr = &tcp_randtbl[1]; 24416 24417 static int *tcp_random_state = &tcp_randtbl[1]; 24418 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24419 24420 kmutex_t tcp_random_lock; 24421 24422 void 24423 tcp_random_init(void) 24424 { 24425 int i; 24426 hrtime_t hrt; 24427 time_t wallclock; 24428 uint64_t result; 24429 24430 /* 24431 * Use high-res timer and current time for seed. Gethrtime() returns 24432 * a longlong, which may contain resolution down to nanoseconds. 24433 * The current time will either be a 32-bit or a 64-bit quantity. 24434 * XOR the two together in a 64-bit result variable. 24435 * Convert the result to a 32-bit value by multiplying the high-order 24436 * 32-bits by the low-order 32-bits. 24437 */ 24438 24439 hrt = gethrtime(); 24440 (void) drv_getparm(TIME, &wallclock); 24441 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24442 mutex_enter(&tcp_random_lock); 24443 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24444 (result & 0xffffffff); 24445 24446 for (i = 1; i < DEG_3; i++) 24447 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24448 + 12345; 24449 tcp_random_fptr = &tcp_random_state[SEP_3]; 24450 tcp_random_rptr = &tcp_random_state[0]; 24451 mutex_exit(&tcp_random_lock); 24452 for (i = 0; i < 10 * DEG_3; i++) 24453 (void) tcp_random(); 24454 } 24455 24456 /* 24457 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24458 * This range is selected to be approximately centered on TCP_ISS / 2, 24459 * and easy to compute. We get this value by generating a 32-bit random 24460 * number, selecting out the high-order 17 bits, and then adding one so 24461 * that we never return zero. 24462 */ 24463 int 24464 tcp_random(void) 24465 { 24466 int i; 24467 24468 mutex_enter(&tcp_random_lock); 24469 *tcp_random_fptr += *tcp_random_rptr; 24470 24471 /* 24472 * The high-order bits are more random than the low-order bits, 24473 * so we select out the high-order 17 bits and add one so that 24474 * we never return zero. 24475 */ 24476 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24477 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24478 tcp_random_fptr = tcp_random_state; 24479 ++tcp_random_rptr; 24480 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24481 tcp_random_rptr = tcp_random_state; 24482 24483 mutex_exit(&tcp_random_lock); 24484 return (i); 24485 } 24486 24487 /* 24488 * XXX This will go away when TPI is extended to send 24489 * info reqs to sockfs/timod ..... 24490 * Given a queue, set the max packet size for the write 24491 * side of the queue below stream head. This value is 24492 * cached on the stream head. 24493 * Returns 1 on success, 0 otherwise. 24494 */ 24495 static int 24496 setmaxps(queue_t *q, int maxpsz) 24497 { 24498 struct stdata *stp; 24499 queue_t *wq; 24500 stp = STREAM(q); 24501 24502 /* 24503 * At this point change of a queue parameter is not allowed 24504 * when a multiplexor is sitting on top. 24505 */ 24506 if (stp->sd_flag & STPLEX) 24507 return (0); 24508 24509 claimstr(stp->sd_wrq); 24510 wq = stp->sd_wrq->q_next; 24511 ASSERT(wq != NULL); 24512 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 24513 releasestr(stp->sd_wrq); 24514 return (1); 24515 } 24516 24517 static int 24518 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24519 int *t_errorp, int *sys_errorp) 24520 { 24521 int error; 24522 int is_absreq_failure; 24523 t_scalar_t *opt_lenp; 24524 t_scalar_t opt_offset; 24525 int prim_type; 24526 struct T_conn_req *tcreqp; 24527 struct T_conn_res *tcresp; 24528 cred_t *cr; 24529 24530 cr = DB_CREDDEF(mp, tcp->tcp_cred); 24531 24532 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24533 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24534 prim_type == T_CONN_RES); 24535 24536 switch (prim_type) { 24537 case T_CONN_REQ: 24538 tcreqp = (struct T_conn_req *)mp->b_rptr; 24539 opt_offset = tcreqp->OPT_offset; 24540 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24541 break; 24542 case O_T_CONN_RES: 24543 case T_CONN_RES: 24544 tcresp = (struct T_conn_res *)mp->b_rptr; 24545 opt_offset = tcresp->OPT_offset; 24546 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24547 break; 24548 } 24549 24550 *t_errorp = 0; 24551 *sys_errorp = 0; 24552 *do_disconnectp = 0; 24553 24554 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24555 opt_offset, cr, &tcp_opt_obj, 24556 NULL, &is_absreq_failure); 24557 24558 switch (error) { 24559 case 0: /* no error */ 24560 ASSERT(is_absreq_failure == 0); 24561 return (0); 24562 case ENOPROTOOPT: 24563 *t_errorp = TBADOPT; 24564 break; 24565 case EACCES: 24566 *t_errorp = TACCES; 24567 break; 24568 default: 24569 *t_errorp = TSYSERR; *sys_errorp = error; 24570 break; 24571 } 24572 if (is_absreq_failure != 0) { 24573 /* 24574 * The connection request should get the local ack 24575 * T_OK_ACK and then a T_DISCON_IND. 24576 */ 24577 *do_disconnectp = 1; 24578 } 24579 return (-1); 24580 } 24581 24582 /* 24583 * Split this function out so that if the secret changes, I'm okay. 24584 * 24585 * Initialize the tcp_iss_cookie and tcp_iss_key. 24586 */ 24587 24588 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24589 24590 static void 24591 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24592 { 24593 struct { 24594 int32_t current_time; 24595 uint32_t randnum; 24596 uint16_t pad; 24597 uint8_t ether[6]; 24598 uint8_t passwd[PASSWD_SIZE]; 24599 } tcp_iss_cookie; 24600 time_t t; 24601 24602 /* 24603 * Start with the current absolute time. 24604 */ 24605 (void) drv_getparm(TIME, &t); 24606 tcp_iss_cookie.current_time = t; 24607 24608 /* 24609 * XXX - Need a more random number per RFC 1750, not this crap. 24610 * OTOH, if what follows is pretty random, then I'm in better shape. 24611 */ 24612 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24613 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24614 24615 /* 24616 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24617 * as a good template. 24618 */ 24619 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24620 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24621 24622 /* 24623 * The pass-phrase. Normally this is supplied by user-called NDD. 24624 */ 24625 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24626 24627 /* 24628 * See 4010593 if this section becomes a problem again, 24629 * but the local ethernet address is useful here. 24630 */ 24631 (void) localetheraddr(NULL, 24632 (struct ether_addr *)&tcp_iss_cookie.ether); 24633 24634 /* 24635 * Hash 'em all together. The MD5Final is called per-connection. 24636 */ 24637 mutex_enter(&tcps->tcps_iss_key_lock); 24638 MD5Init(&tcps->tcps_iss_key); 24639 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24640 sizeof (tcp_iss_cookie)); 24641 mutex_exit(&tcps->tcps_iss_key_lock); 24642 } 24643 24644 /* 24645 * Set the RFC 1948 pass phrase 24646 */ 24647 /* ARGSUSED */ 24648 static int 24649 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24650 cred_t *cr) 24651 { 24652 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24653 24654 /* 24655 * Basically, value contains a new pass phrase. Pass it along! 24656 */ 24657 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24658 return (0); 24659 } 24660 24661 /* ARGSUSED */ 24662 static int 24663 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24664 { 24665 bzero(buf, sizeof (tcp_sack_info_t)); 24666 return (0); 24667 } 24668 24669 /* ARGSUSED */ 24670 static int 24671 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24672 { 24673 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24674 return (0); 24675 } 24676 24677 /* 24678 * Make sure we wait until the default queue is setup, yet allow 24679 * tcp_g_q_create() to open a TCP stream. 24680 * We need to allow tcp_g_q_create() do do an open 24681 * of tcp, hence we compare curhread. 24682 * All others have to wait until the tcps_g_q has been 24683 * setup. 24684 */ 24685 void 24686 tcp_g_q_setup(tcp_stack_t *tcps) 24687 { 24688 mutex_enter(&tcps->tcps_g_q_lock); 24689 if (tcps->tcps_g_q != NULL) { 24690 mutex_exit(&tcps->tcps_g_q_lock); 24691 return; 24692 } 24693 if (tcps->tcps_g_q_creator == NULL) { 24694 /* This thread will set it up */ 24695 tcps->tcps_g_q_creator = curthread; 24696 mutex_exit(&tcps->tcps_g_q_lock); 24697 tcp_g_q_create(tcps); 24698 mutex_enter(&tcps->tcps_g_q_lock); 24699 ASSERT(tcps->tcps_g_q_creator == curthread); 24700 tcps->tcps_g_q_creator = NULL; 24701 cv_signal(&tcps->tcps_g_q_cv); 24702 ASSERT(tcps->tcps_g_q != NULL); 24703 mutex_exit(&tcps->tcps_g_q_lock); 24704 return; 24705 } 24706 /* Everybody but the creator has to wait */ 24707 if (tcps->tcps_g_q_creator != curthread) { 24708 while (tcps->tcps_g_q == NULL) 24709 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24710 } 24711 mutex_exit(&tcps->tcps_g_q_lock); 24712 } 24713 24714 #define IP "ip" 24715 24716 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24717 24718 /* 24719 * Create a default tcp queue here instead of in strplumb 24720 */ 24721 void 24722 tcp_g_q_create(tcp_stack_t *tcps) 24723 { 24724 int error; 24725 ldi_handle_t lh = NULL; 24726 ldi_ident_t li = NULL; 24727 int rval; 24728 cred_t *cr; 24729 major_t IP_MAJ; 24730 24731 #ifdef NS_DEBUG 24732 (void) printf("tcp_g_q_create()\n"); 24733 #endif 24734 24735 IP_MAJ = ddi_name_to_major(IP); 24736 24737 ASSERT(tcps->tcps_g_q_creator == curthread); 24738 24739 error = ldi_ident_from_major(IP_MAJ, &li); 24740 if (error) { 24741 #ifdef DEBUG 24742 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24743 error); 24744 #endif 24745 return; 24746 } 24747 24748 cr = zone_get_kcred(netstackid_to_zoneid( 24749 tcps->tcps_netstack->netstack_stackid)); 24750 ASSERT(cr != NULL); 24751 /* 24752 * We set the tcp default queue to IPv6 because IPv4 falls 24753 * back to IPv6 when it can't find a client, but 24754 * IPv6 does not fall back to IPv4. 24755 */ 24756 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24757 if (error) { 24758 #ifdef DEBUG 24759 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24760 error); 24761 #endif 24762 goto out; 24763 } 24764 24765 /* 24766 * This ioctl causes the tcp framework to cache a pointer to 24767 * this stream, so we don't want to close the stream after 24768 * this operation. 24769 * Use the kernel credentials that are for the zone we're in. 24770 */ 24771 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24772 (intptr_t)0, FKIOCTL, cr, &rval); 24773 if (error) { 24774 #ifdef DEBUG 24775 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24776 "error %d\n", error); 24777 #endif 24778 goto out; 24779 } 24780 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24781 lh = NULL; 24782 out: 24783 /* Close layered handles */ 24784 if (li) 24785 ldi_ident_release(li); 24786 /* Keep cred around until _inactive needs it */ 24787 tcps->tcps_g_q_cr = cr; 24788 } 24789 24790 /* 24791 * We keep tcp_g_q set until all other tcp_t's in the zone 24792 * has gone away, and then when tcp_g_q_inactive() is called 24793 * we clear it. 24794 */ 24795 void 24796 tcp_g_q_destroy(tcp_stack_t *tcps) 24797 { 24798 #ifdef NS_DEBUG 24799 (void) printf("tcp_g_q_destroy()for stack %d\n", 24800 tcps->tcps_netstack->netstack_stackid); 24801 #endif 24802 24803 if (tcps->tcps_g_q == NULL) { 24804 return; /* Nothing to cleanup */ 24805 } 24806 /* 24807 * Drop reference corresponding to the default queue. 24808 * This reference was added from tcp_open when the default queue 24809 * was created, hence we compensate for this extra drop in 24810 * tcp_g_q_close. If the refcnt drops to zero here it means 24811 * the default queue was the last one to be open, in which 24812 * case, then tcp_g_q_inactive will be 24813 * called as a result of the refrele. 24814 */ 24815 TCPS_REFRELE(tcps); 24816 } 24817 24818 /* 24819 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24820 * Run by tcp_q_q_inactive using a taskq. 24821 */ 24822 static void 24823 tcp_g_q_close(void *arg) 24824 { 24825 tcp_stack_t *tcps = arg; 24826 int error; 24827 ldi_handle_t lh = NULL; 24828 ldi_ident_t li = NULL; 24829 cred_t *cr; 24830 major_t IP_MAJ; 24831 24832 IP_MAJ = ddi_name_to_major(IP); 24833 24834 #ifdef NS_DEBUG 24835 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 24836 tcps->tcps_netstack->netstack_stackid, 24837 tcps->tcps_netstack->netstack_refcnt); 24838 #endif 24839 lh = tcps->tcps_g_q_lh; 24840 if (lh == NULL) 24841 return; /* Nothing to cleanup */ 24842 24843 ASSERT(tcps->tcps_refcnt == 1); 24844 ASSERT(tcps->tcps_g_q != NULL); 24845 24846 error = ldi_ident_from_major(IP_MAJ, &li); 24847 if (error) { 24848 #ifdef DEBUG 24849 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 24850 error); 24851 #endif 24852 return; 24853 } 24854 24855 cr = tcps->tcps_g_q_cr; 24856 tcps->tcps_g_q_cr = NULL; 24857 ASSERT(cr != NULL); 24858 24859 /* 24860 * Make sure we can break the recursion when tcp_close decrements 24861 * the reference count causing g_q_inactive to be called again. 24862 */ 24863 tcps->tcps_g_q_lh = NULL; 24864 24865 /* close the default queue */ 24866 (void) ldi_close(lh, FREAD|FWRITE, cr); 24867 /* 24868 * At this point in time tcps and the rest of netstack_t might 24869 * have been deleted. 24870 */ 24871 tcps = NULL; 24872 24873 /* Close layered handles */ 24874 ldi_ident_release(li); 24875 crfree(cr); 24876 } 24877 24878 /* 24879 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24880 * 24881 * Have to ensure that the ldi routines are not used by an 24882 * interrupt thread by using a taskq. 24883 */ 24884 void 24885 tcp_g_q_inactive(tcp_stack_t *tcps) 24886 { 24887 if (tcps->tcps_g_q_lh == NULL) 24888 return; /* Nothing to cleanup */ 24889 24890 ASSERT(tcps->tcps_refcnt == 0); 24891 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 24892 24893 if (servicing_interrupt()) { 24894 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 24895 (void *) tcps, TQ_SLEEP); 24896 } else { 24897 tcp_g_q_close(tcps); 24898 } 24899 } 24900 24901 /* 24902 * Called by IP when IP is loaded into the kernel 24903 */ 24904 void 24905 tcp_ddi_g_init(void) 24906 { 24907 tcp_timercache = kmem_cache_create("tcp_timercache", 24908 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 24909 NULL, NULL, NULL, NULL, NULL, 0); 24910 24911 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 24912 sizeof (tcp_sack_info_t), 0, 24913 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 24914 24915 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 24916 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 24917 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 24918 24919 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 24920 24921 /* Initialize the random number generator */ 24922 tcp_random_init(); 24923 24924 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 24925 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 24926 24927 /* A single callback independently of how many netstacks we have */ 24928 ip_squeue_init(tcp_squeue_add); 24929 24930 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 24931 24932 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 24933 TASKQ_PREPOPULATE); 24934 24935 /* 24936 * We want to be informed each time a stack is created or 24937 * destroyed in the kernel, so we can maintain the 24938 * set of tcp_stack_t's. 24939 */ 24940 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 24941 tcp_stack_fini); 24942 } 24943 24944 24945 /* 24946 * Initialize the TCP stack instance. 24947 */ 24948 static void * 24949 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 24950 { 24951 tcp_stack_t *tcps; 24952 tcpparam_t *pa; 24953 int i; 24954 24955 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 24956 tcps->tcps_netstack = ns; 24957 24958 /* Initialize locks */ 24959 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 24960 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 24961 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 24962 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 24963 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 24964 24965 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 24966 tcps->tcps_g_epriv_ports[0] = 2049; 24967 tcps->tcps_g_epriv_ports[1] = 4045; 24968 tcps->tcps_min_anonpriv_port = 512; 24969 24970 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 24971 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 24972 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 24973 TCP_FANOUT_SIZE, KM_SLEEP); 24974 24975 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24976 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 24977 MUTEX_DEFAULT, NULL); 24978 } 24979 24980 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24981 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 24982 MUTEX_DEFAULT, NULL); 24983 } 24984 24985 /* TCP's IPsec code calls the packet dropper. */ 24986 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 24987 24988 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 24989 tcps->tcps_params = pa; 24990 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24991 24992 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 24993 A_CNT(lcl_tcp_param_arr), tcps); 24994 24995 /* 24996 * Note: To really walk the device tree you need the devinfo 24997 * pointer to your device which is only available after probe/attach. 24998 * The following is safe only because it uses ddi_root_node() 24999 */ 25000 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 25001 tcp_opt_obj.odb_opt_arr_cnt); 25002 25003 /* 25004 * Initialize RFC 1948 secret values. This will probably be reset once 25005 * by the boot scripts. 25006 * 25007 * Use NULL name, as the name is caught by the new lockstats. 25008 * 25009 * Initialize with some random, non-guessable string, like the global 25010 * T_INFO_ACK. 25011 */ 25012 25013 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 25014 sizeof (tcp_g_t_info_ack), tcps); 25015 25016 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 25017 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 25018 25019 return (tcps); 25020 } 25021 25022 /* 25023 * Called when the IP module is about to be unloaded. 25024 */ 25025 void 25026 tcp_ddi_g_destroy(void) 25027 { 25028 tcp_g_kstat_fini(tcp_g_kstat); 25029 tcp_g_kstat = NULL; 25030 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 25031 25032 mutex_destroy(&tcp_random_lock); 25033 25034 kmem_cache_destroy(tcp_timercache); 25035 kmem_cache_destroy(tcp_sack_info_cache); 25036 kmem_cache_destroy(tcp_iphc_cache); 25037 25038 netstack_unregister(NS_TCP); 25039 taskq_destroy(tcp_taskq); 25040 } 25041 25042 /* 25043 * Shut down the TCP stack instance. 25044 */ 25045 /* ARGSUSED */ 25046 static void 25047 tcp_stack_shutdown(netstackid_t stackid, void *arg) 25048 { 25049 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25050 25051 tcp_g_q_destroy(tcps); 25052 } 25053 25054 /* 25055 * Free the TCP stack instance. 25056 */ 25057 static void 25058 tcp_stack_fini(netstackid_t stackid, void *arg) 25059 { 25060 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25061 int i; 25062 25063 nd_free(&tcps->tcps_g_nd); 25064 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25065 tcps->tcps_params = NULL; 25066 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 25067 tcps->tcps_wroff_xtra_param = NULL; 25068 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 25069 tcps->tcps_mdt_head_param = NULL; 25070 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 25071 tcps->tcps_mdt_tail_param = NULL; 25072 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 25073 tcps->tcps_mdt_max_pbufs_param = NULL; 25074 25075 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25076 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 25077 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 25078 } 25079 25080 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25081 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25082 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25083 } 25084 25085 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25086 tcps->tcps_bind_fanout = NULL; 25087 25088 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25089 tcps->tcps_acceptor_fanout = NULL; 25090 25091 mutex_destroy(&tcps->tcps_iss_key_lock); 25092 rw_destroy(&tcps->tcps_hsp_lock); 25093 mutex_destroy(&tcps->tcps_g_q_lock); 25094 cv_destroy(&tcps->tcps_g_q_cv); 25095 mutex_destroy(&tcps->tcps_epriv_port_lock); 25096 25097 ip_drop_unregister(&tcps->tcps_dropper); 25098 25099 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25100 tcps->tcps_kstat = NULL; 25101 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25102 25103 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25104 tcps->tcps_mibkp = NULL; 25105 25106 kmem_free(tcps, sizeof (*tcps)); 25107 } 25108 25109 /* 25110 * Generate ISS, taking into account NDD changes may happen halfway through. 25111 * (If the iss is not zero, set it.) 25112 */ 25113 25114 static void 25115 tcp_iss_init(tcp_t *tcp) 25116 { 25117 MD5_CTX context; 25118 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25119 uint32_t answer[4]; 25120 tcp_stack_t *tcps = tcp->tcp_tcps; 25121 25122 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25123 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25124 switch (tcps->tcps_strong_iss) { 25125 case 2: 25126 mutex_enter(&tcps->tcps_iss_key_lock); 25127 context = tcps->tcps_iss_key; 25128 mutex_exit(&tcps->tcps_iss_key_lock); 25129 arg.ports = tcp->tcp_ports; 25130 if (tcp->tcp_ipversion == IPV4_VERSION) { 25131 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25132 &arg.src); 25133 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25134 &arg.dst); 25135 } else { 25136 arg.src = tcp->tcp_ip6h->ip6_src; 25137 arg.dst = tcp->tcp_ip6h->ip6_dst; 25138 } 25139 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25140 MD5Final((uchar_t *)answer, &context); 25141 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25142 /* 25143 * Now that we've hashed into a unique per-connection sequence 25144 * space, add a random increment per strong_iss == 1. So I 25145 * guess we'll have to... 25146 */ 25147 /* FALLTHRU */ 25148 case 1: 25149 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25150 break; 25151 default: 25152 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25153 break; 25154 } 25155 tcp->tcp_valid_bits = TCP_ISS_VALID; 25156 tcp->tcp_fss = tcp->tcp_iss - 1; 25157 tcp->tcp_suna = tcp->tcp_iss; 25158 tcp->tcp_snxt = tcp->tcp_iss + 1; 25159 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25160 tcp->tcp_csuna = tcp->tcp_snxt; 25161 } 25162 25163 /* 25164 * Exported routine for extracting active tcp connection status. 25165 * 25166 * This is used by the Solaris Cluster Networking software to 25167 * gather a list of connections that need to be forwarded to 25168 * specific nodes in the cluster when configuration changes occur. 25169 * 25170 * The callback is invoked for each tcp_t structure. Returning 25171 * non-zero from the callback routine terminates the search. 25172 */ 25173 int 25174 cl_tcp_walk_list(int (*cl_callback)(cl_tcp_info_t *, void *), 25175 void *arg) 25176 { 25177 netstack_handle_t nh; 25178 netstack_t *ns; 25179 int ret = 0; 25180 25181 netstack_next_init(&nh); 25182 while ((ns = netstack_next(&nh)) != NULL) { 25183 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25184 ns->netstack_tcp); 25185 netstack_rele(ns); 25186 } 25187 netstack_next_fini(&nh); 25188 return (ret); 25189 } 25190 25191 static int 25192 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25193 tcp_stack_t *tcps) 25194 { 25195 tcp_t *tcp; 25196 cl_tcp_info_t cl_tcpi; 25197 connf_t *connfp; 25198 conn_t *connp; 25199 int i; 25200 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25201 25202 ASSERT(callback != NULL); 25203 25204 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25205 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25206 connp = NULL; 25207 25208 while ((connp = 25209 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25210 25211 tcp = connp->conn_tcp; 25212 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25213 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25214 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25215 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25216 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25217 /* 25218 * The macros tcp_laddr and tcp_faddr give the IPv4 25219 * addresses. They are copied implicitly below as 25220 * mapped addresses. 25221 */ 25222 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25223 if (tcp->tcp_ipversion == IPV4_VERSION) { 25224 cl_tcpi.cl_tcpi_faddr = 25225 tcp->tcp_ipha->ipha_dst; 25226 } else { 25227 cl_tcpi.cl_tcpi_faddr_v6 = 25228 tcp->tcp_ip6h->ip6_dst; 25229 } 25230 25231 /* 25232 * If the callback returns non-zero 25233 * we terminate the traversal. 25234 */ 25235 if ((*callback)(&cl_tcpi, arg) != 0) { 25236 CONN_DEC_REF(tcp->tcp_connp); 25237 return (1); 25238 } 25239 } 25240 } 25241 25242 return (0); 25243 } 25244 25245 /* 25246 * Macros used for accessing the different types of sockaddr 25247 * structures inside a tcp_ioc_abort_conn_t. 25248 */ 25249 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25250 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25251 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25252 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25253 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25254 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25255 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25256 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25257 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25258 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25259 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25260 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25261 25262 /* 25263 * Return the correct error code to mimic the behavior 25264 * of a connection reset. 25265 */ 25266 #define TCP_AC_GET_ERRCODE(state, err) { \ 25267 switch ((state)) { \ 25268 case TCPS_SYN_SENT: \ 25269 case TCPS_SYN_RCVD: \ 25270 (err) = ECONNREFUSED; \ 25271 break; \ 25272 case TCPS_ESTABLISHED: \ 25273 case TCPS_FIN_WAIT_1: \ 25274 case TCPS_FIN_WAIT_2: \ 25275 case TCPS_CLOSE_WAIT: \ 25276 (err) = ECONNRESET; \ 25277 break; \ 25278 case TCPS_CLOSING: \ 25279 case TCPS_LAST_ACK: \ 25280 case TCPS_TIME_WAIT: \ 25281 (err) = 0; \ 25282 break; \ 25283 default: \ 25284 (err) = ENXIO; \ 25285 } \ 25286 } 25287 25288 /* 25289 * Check if a tcp structure matches the info in acp. 25290 */ 25291 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25292 (((acp)->ac_local.ss_family == AF_INET) ? \ 25293 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25294 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25295 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25296 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25297 (TCP_AC_V4LPORT((acp)) == 0 || \ 25298 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25299 (TCP_AC_V4RPORT((acp)) == 0 || \ 25300 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25301 (acp)->ac_start <= (tcp)->tcp_state && \ 25302 (acp)->ac_end >= (tcp)->tcp_state) : \ 25303 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25304 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25305 &(tcp)->tcp_ip_src_v6)) && \ 25306 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25307 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25308 &(tcp)->tcp_remote_v6)) && \ 25309 (TCP_AC_V6LPORT((acp)) == 0 || \ 25310 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25311 (TCP_AC_V6RPORT((acp)) == 0 || \ 25312 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25313 (acp)->ac_start <= (tcp)->tcp_state && \ 25314 (acp)->ac_end >= (tcp)->tcp_state)) 25315 25316 #define TCP_AC_MATCH(acp, tcp) \ 25317 (((acp)->ac_zoneid == ALL_ZONES || \ 25318 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25319 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25320 25321 /* 25322 * Build a message containing a tcp_ioc_abort_conn_t structure 25323 * which is filled in with information from acp and tp. 25324 */ 25325 static mblk_t * 25326 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25327 { 25328 mblk_t *mp; 25329 tcp_ioc_abort_conn_t *tacp; 25330 25331 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25332 if (mp == NULL) 25333 return (NULL); 25334 25335 mp->b_datap->db_type = M_CTL; 25336 25337 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25338 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25339 sizeof (uint32_t)); 25340 25341 tacp->ac_start = acp->ac_start; 25342 tacp->ac_end = acp->ac_end; 25343 tacp->ac_zoneid = acp->ac_zoneid; 25344 25345 if (acp->ac_local.ss_family == AF_INET) { 25346 tacp->ac_local.ss_family = AF_INET; 25347 tacp->ac_remote.ss_family = AF_INET; 25348 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25349 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25350 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25351 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25352 } else { 25353 tacp->ac_local.ss_family = AF_INET6; 25354 tacp->ac_remote.ss_family = AF_INET6; 25355 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25356 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25357 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25358 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25359 } 25360 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25361 return (mp); 25362 } 25363 25364 /* 25365 * Print a tcp_ioc_abort_conn_t structure. 25366 */ 25367 static void 25368 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25369 { 25370 char lbuf[128]; 25371 char rbuf[128]; 25372 sa_family_t af; 25373 in_port_t lport, rport; 25374 ushort_t logflags; 25375 25376 af = acp->ac_local.ss_family; 25377 25378 if (af == AF_INET) { 25379 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25380 lbuf, 128); 25381 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25382 rbuf, 128); 25383 lport = ntohs(TCP_AC_V4LPORT(acp)); 25384 rport = ntohs(TCP_AC_V4RPORT(acp)); 25385 } else { 25386 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25387 lbuf, 128); 25388 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25389 rbuf, 128); 25390 lport = ntohs(TCP_AC_V6LPORT(acp)); 25391 rport = ntohs(TCP_AC_V6RPORT(acp)); 25392 } 25393 25394 logflags = SL_TRACE | SL_NOTE; 25395 /* 25396 * Don't print this message to the console if the operation was done 25397 * to a non-global zone. 25398 */ 25399 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25400 logflags |= SL_CONSOLE; 25401 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25402 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25403 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25404 acp->ac_start, acp->ac_end); 25405 } 25406 25407 /* 25408 * Called inside tcp_rput when a message built using 25409 * tcp_ioctl_abort_build_msg is put into a queue. 25410 * Note that when we get here there is no wildcard in acp any more. 25411 */ 25412 static void 25413 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25414 { 25415 tcp_ioc_abort_conn_t *acp; 25416 25417 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25418 if (tcp->tcp_state <= acp->ac_end) { 25419 /* 25420 * If we get here, we are already on the correct 25421 * squeue. This ioctl follows the following path 25422 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25423 * ->tcp_ioctl_abort->squeue_fill (if on a 25424 * different squeue) 25425 */ 25426 int errcode; 25427 25428 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25429 (void) tcp_clean_death(tcp, errcode, 26); 25430 } 25431 freemsg(mp); 25432 } 25433 25434 /* 25435 * Abort all matching connections on a hash chain. 25436 */ 25437 static int 25438 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25439 boolean_t exact, tcp_stack_t *tcps) 25440 { 25441 int nmatch, err = 0; 25442 tcp_t *tcp; 25443 MBLKP mp, last, listhead = NULL; 25444 conn_t *tconnp; 25445 connf_t *connfp; 25446 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25447 25448 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25449 25450 startover: 25451 nmatch = 0; 25452 25453 mutex_enter(&connfp->connf_lock); 25454 for (tconnp = connfp->connf_head; tconnp != NULL; 25455 tconnp = tconnp->conn_next) { 25456 tcp = tconnp->conn_tcp; 25457 if (TCP_AC_MATCH(acp, tcp)) { 25458 CONN_INC_REF(tcp->tcp_connp); 25459 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25460 if (mp == NULL) { 25461 err = ENOMEM; 25462 CONN_DEC_REF(tcp->tcp_connp); 25463 break; 25464 } 25465 mp->b_prev = (mblk_t *)tcp; 25466 25467 if (listhead == NULL) { 25468 listhead = mp; 25469 last = mp; 25470 } else { 25471 last->b_next = mp; 25472 last = mp; 25473 } 25474 nmatch++; 25475 if (exact) 25476 break; 25477 } 25478 25479 /* Avoid holding lock for too long. */ 25480 if (nmatch >= 500) 25481 break; 25482 } 25483 mutex_exit(&connfp->connf_lock); 25484 25485 /* Pass mp into the correct tcp */ 25486 while ((mp = listhead) != NULL) { 25487 listhead = listhead->b_next; 25488 tcp = (tcp_t *)mp->b_prev; 25489 mp->b_next = mp->b_prev = NULL; 25490 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 25491 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 25492 } 25493 25494 *count += nmatch; 25495 if (nmatch >= 500 && err == 0) 25496 goto startover; 25497 return (err); 25498 } 25499 25500 /* 25501 * Abort all connections that matches the attributes specified in acp. 25502 */ 25503 static int 25504 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25505 { 25506 sa_family_t af; 25507 uint32_t ports; 25508 uint16_t *pports; 25509 int err = 0, count = 0; 25510 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25511 int index = -1; 25512 ushort_t logflags; 25513 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25514 25515 af = acp->ac_local.ss_family; 25516 25517 if (af == AF_INET) { 25518 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25519 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25520 pports = (uint16_t *)&ports; 25521 pports[1] = TCP_AC_V4LPORT(acp); 25522 pports[0] = TCP_AC_V4RPORT(acp); 25523 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25524 } 25525 } else { 25526 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25527 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25528 pports = (uint16_t *)&ports; 25529 pports[1] = TCP_AC_V6LPORT(acp); 25530 pports[0] = TCP_AC_V6RPORT(acp); 25531 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25532 } 25533 } 25534 25535 /* 25536 * For cases where remote addr, local port, and remote port are non- 25537 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25538 */ 25539 if (index != -1) { 25540 err = tcp_ioctl_abort_bucket(acp, index, 25541 &count, exact, tcps); 25542 } else { 25543 /* 25544 * loop through all entries for wildcard case 25545 */ 25546 for (index = 0; 25547 index < ipst->ips_ipcl_conn_fanout_size; 25548 index++) { 25549 err = tcp_ioctl_abort_bucket(acp, index, 25550 &count, exact, tcps); 25551 if (err != 0) 25552 break; 25553 } 25554 } 25555 25556 logflags = SL_TRACE | SL_NOTE; 25557 /* 25558 * Don't print this message to the console if the operation was done 25559 * to a non-global zone. 25560 */ 25561 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25562 logflags |= SL_CONSOLE; 25563 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25564 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25565 if (err == 0 && count == 0) 25566 err = ENOENT; 25567 return (err); 25568 } 25569 25570 /* 25571 * Process the TCP_IOC_ABORT_CONN ioctl request. 25572 */ 25573 static void 25574 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25575 { 25576 int err; 25577 IOCP iocp; 25578 MBLKP mp1; 25579 sa_family_t laf, raf; 25580 tcp_ioc_abort_conn_t *acp; 25581 zone_t *zptr; 25582 conn_t *connp = Q_TO_CONN(q); 25583 zoneid_t zoneid = connp->conn_zoneid; 25584 tcp_t *tcp = connp->conn_tcp; 25585 tcp_stack_t *tcps = tcp->tcp_tcps; 25586 25587 iocp = (IOCP)mp->b_rptr; 25588 25589 if ((mp1 = mp->b_cont) == NULL || 25590 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25591 err = EINVAL; 25592 goto out; 25593 } 25594 25595 /* check permissions */ 25596 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25597 err = EPERM; 25598 goto out; 25599 } 25600 25601 if (mp1->b_cont != NULL) { 25602 freemsg(mp1->b_cont); 25603 mp1->b_cont = NULL; 25604 } 25605 25606 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25607 laf = acp->ac_local.ss_family; 25608 raf = acp->ac_remote.ss_family; 25609 25610 /* check that a zone with the supplied zoneid exists */ 25611 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25612 zptr = zone_find_by_id(zoneid); 25613 if (zptr != NULL) { 25614 zone_rele(zptr); 25615 } else { 25616 err = EINVAL; 25617 goto out; 25618 } 25619 } 25620 25621 /* 25622 * For exclusive stacks we set the zoneid to zero 25623 * to make TCP operate as if in the global zone. 25624 */ 25625 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25626 acp->ac_zoneid = GLOBAL_ZONEID; 25627 25628 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25629 acp->ac_start > acp->ac_end || laf != raf || 25630 (laf != AF_INET && laf != AF_INET6)) { 25631 err = EINVAL; 25632 goto out; 25633 } 25634 25635 tcp_ioctl_abort_dump(acp); 25636 err = tcp_ioctl_abort(acp, tcps); 25637 25638 out: 25639 if (mp1 != NULL) { 25640 freemsg(mp1); 25641 mp->b_cont = NULL; 25642 } 25643 25644 if (err != 0) 25645 miocnak(q, mp, 0, err); 25646 else 25647 miocack(q, mp, 0, 0); 25648 } 25649 25650 /* 25651 * tcp_time_wait_processing() handles processing of incoming packets when 25652 * the tcp is in the TIME_WAIT state. 25653 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25654 * on the time wait list. 25655 */ 25656 void 25657 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25658 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25659 { 25660 int32_t bytes_acked; 25661 int32_t gap; 25662 int32_t rgap; 25663 tcp_opt_t tcpopt; 25664 uint_t flags; 25665 uint32_t new_swnd = 0; 25666 conn_t *connp; 25667 tcp_stack_t *tcps = tcp->tcp_tcps; 25668 25669 BUMP_LOCAL(tcp->tcp_ibsegs); 25670 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 25671 25672 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25673 new_swnd = BE16_TO_U16(tcph->th_win) << 25674 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25675 if (tcp->tcp_snd_ts_ok) { 25676 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25677 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25678 tcp->tcp_rnxt, TH_ACK); 25679 goto done; 25680 } 25681 } 25682 gap = seg_seq - tcp->tcp_rnxt; 25683 rgap = tcp->tcp_rwnd - (gap + seg_len); 25684 if (gap < 0) { 25685 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25686 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25687 (seg_len > -gap ? -gap : seg_len)); 25688 seg_len += gap; 25689 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25690 if (flags & TH_RST) { 25691 goto done; 25692 } 25693 if ((flags & TH_FIN) && seg_len == -1) { 25694 /* 25695 * When TCP receives a duplicate FIN in 25696 * TIME_WAIT state, restart the 2 MSL timer. 25697 * See page 73 in RFC 793. Make sure this TCP 25698 * is already on the TIME_WAIT list. If not, 25699 * just restart the timer. 25700 */ 25701 if (TCP_IS_DETACHED(tcp)) { 25702 if (tcp_time_wait_remove(tcp, NULL) == 25703 B_TRUE) { 25704 tcp_time_wait_append(tcp); 25705 TCP_DBGSTAT(tcps, 25706 tcp_rput_time_wait); 25707 } 25708 } else { 25709 ASSERT(tcp != NULL); 25710 TCP_TIMER_RESTART(tcp, 25711 tcps->tcps_time_wait_interval); 25712 } 25713 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25714 tcp->tcp_rnxt, TH_ACK); 25715 goto done; 25716 } 25717 flags |= TH_ACK_NEEDED; 25718 seg_len = 0; 25719 goto process_ack; 25720 } 25721 25722 /* Fix seg_seq, and chew the gap off the front. */ 25723 seg_seq = tcp->tcp_rnxt; 25724 } 25725 25726 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25727 /* 25728 * Make sure that when we accept the connection, pick 25729 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25730 * old connection. 25731 * 25732 * The next ISS generated is equal to tcp_iss_incr_extra 25733 * + ISS_INCR/2 + other components depending on the 25734 * value of tcp_strong_iss. We pre-calculate the new 25735 * ISS here and compare with tcp_snxt to determine if 25736 * we need to make adjustment to tcp_iss_incr_extra. 25737 * 25738 * The above calculation is ugly and is a 25739 * waste of CPU cycles... 25740 */ 25741 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25742 int32_t adj; 25743 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25744 25745 switch (tcps->tcps_strong_iss) { 25746 case 2: { 25747 /* Add time and MD5 components. */ 25748 uint32_t answer[4]; 25749 struct { 25750 uint32_t ports; 25751 in6_addr_t src; 25752 in6_addr_t dst; 25753 } arg; 25754 MD5_CTX context; 25755 25756 mutex_enter(&tcps->tcps_iss_key_lock); 25757 context = tcps->tcps_iss_key; 25758 mutex_exit(&tcps->tcps_iss_key_lock); 25759 arg.ports = tcp->tcp_ports; 25760 /* We use MAPPED addresses in tcp_iss_init */ 25761 arg.src = tcp->tcp_ip_src_v6; 25762 if (tcp->tcp_ipversion == IPV4_VERSION) { 25763 IN6_IPADDR_TO_V4MAPPED( 25764 tcp->tcp_ipha->ipha_dst, 25765 &arg.dst); 25766 } else { 25767 arg.dst = 25768 tcp->tcp_ip6h->ip6_dst; 25769 } 25770 MD5Update(&context, (uchar_t *)&arg, 25771 sizeof (arg)); 25772 MD5Final((uchar_t *)answer, &context); 25773 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25774 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25775 break; 25776 } 25777 case 1: 25778 /* Add time component and min random (i.e. 1). */ 25779 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25780 break; 25781 default: 25782 /* Add only time component. */ 25783 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25784 break; 25785 } 25786 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25787 /* 25788 * New ISS not guaranteed to be ISS_INCR/2 25789 * ahead of the current tcp_snxt, so add the 25790 * difference to tcp_iss_incr_extra. 25791 */ 25792 tcps->tcps_iss_incr_extra += adj; 25793 } 25794 /* 25795 * If tcp_clean_death() can not perform the task now, 25796 * drop the SYN packet and let the other side re-xmit. 25797 * Otherwise pass the SYN packet back in, since the 25798 * old tcp state has been cleaned up or freed. 25799 */ 25800 if (tcp_clean_death(tcp, 0, 27) == -1) 25801 goto done; 25802 /* 25803 * We will come back to tcp_rput_data 25804 * on the global queue. Packets destined 25805 * for the global queue will be checked 25806 * with global policy. But the policy for 25807 * this packet has already been checked as 25808 * this was destined for the detached 25809 * connection. We need to bypass policy 25810 * check this time by attaching a dummy 25811 * ipsec_in with ipsec_in_dont_check set. 25812 */ 25813 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25814 if (connp != NULL) { 25815 TCP_STAT(tcps, tcp_time_wait_syn_success); 25816 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25817 return; 25818 } 25819 goto done; 25820 } 25821 25822 /* 25823 * rgap is the amount of stuff received out of window. A negative 25824 * value is the amount out of window. 25825 */ 25826 if (rgap < 0) { 25827 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 25828 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 25829 /* Fix seg_len and make sure there is something left. */ 25830 seg_len += rgap; 25831 if (seg_len <= 0) { 25832 if (flags & TH_RST) { 25833 goto done; 25834 } 25835 flags |= TH_ACK_NEEDED; 25836 seg_len = 0; 25837 goto process_ack; 25838 } 25839 } 25840 /* 25841 * Check whether we can update tcp_ts_recent. This test is 25842 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 25843 * Extensions for High Performance: An Update", Internet Draft. 25844 */ 25845 if (tcp->tcp_snd_ts_ok && 25846 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 25847 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 25848 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 25849 tcp->tcp_last_rcv_lbolt = lbolt64; 25850 } 25851 25852 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 25853 /* Always ack out of order packets */ 25854 flags |= TH_ACK_NEEDED; 25855 seg_len = 0; 25856 } else if (seg_len > 0) { 25857 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 25858 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 25859 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 25860 } 25861 if (flags & TH_RST) { 25862 (void) tcp_clean_death(tcp, 0, 28); 25863 goto done; 25864 } 25865 if (flags & TH_SYN) { 25866 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 25867 TH_RST|TH_ACK); 25868 /* 25869 * Do not delete the TCP structure if it is in 25870 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 25871 */ 25872 goto done; 25873 } 25874 process_ack: 25875 if (flags & TH_ACK) { 25876 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 25877 if (bytes_acked <= 0) { 25878 if (bytes_acked == 0 && seg_len == 0 && 25879 new_swnd == tcp->tcp_swnd) 25880 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 25881 } else { 25882 /* Acks something not sent */ 25883 flags |= TH_ACK_NEEDED; 25884 } 25885 } 25886 if (flags & TH_ACK_NEEDED) { 25887 /* 25888 * Time to send an ack for some reason. 25889 */ 25890 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25891 tcp->tcp_rnxt, TH_ACK); 25892 } 25893 done: 25894 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25895 DB_CKSUMSTART(mp) = 0; 25896 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 25897 TCP_STAT(tcps, tcp_time_wait_syn_fail); 25898 } 25899 freemsg(mp); 25900 } 25901 25902 /* 25903 * Allocate a T_SVR4_OPTMGMT_REQ. 25904 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 25905 * that tcp_rput_other can drop the acks. 25906 */ 25907 static mblk_t * 25908 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 25909 { 25910 mblk_t *mp; 25911 struct T_optmgmt_req *tor; 25912 struct opthdr *oh; 25913 uint_t size; 25914 char *optptr; 25915 25916 size = sizeof (*tor) + sizeof (*oh) + optlen; 25917 mp = allocb(size, BPRI_MED); 25918 if (mp == NULL) 25919 return (NULL); 25920 25921 mp->b_wptr += size; 25922 mp->b_datap->db_type = M_PROTO; 25923 tor = (struct T_optmgmt_req *)mp->b_rptr; 25924 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 25925 tor->MGMT_flags = T_NEGOTIATE; 25926 tor->OPT_length = sizeof (*oh) + optlen; 25927 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 25928 25929 oh = (struct opthdr *)&tor[1]; 25930 oh->level = level; 25931 oh->name = cmd; 25932 oh->len = optlen; 25933 if (optlen != 0) { 25934 optptr = (char *)&oh[1]; 25935 bcopy(opt, optptr, optlen); 25936 } 25937 return (mp); 25938 } 25939 25940 /* 25941 * TCP Timers Implementation. 25942 */ 25943 timeout_id_t 25944 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 25945 { 25946 mblk_t *mp; 25947 tcp_timer_t *tcpt; 25948 tcp_t *tcp = connp->conn_tcp; 25949 tcp_stack_t *tcps = tcp->tcp_tcps; 25950 25951 ASSERT(connp->conn_sqp != NULL); 25952 25953 TCP_DBGSTAT(tcps, tcp_timeout_calls); 25954 25955 if (tcp->tcp_timercache == NULL) { 25956 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 25957 } else { 25958 TCP_DBGSTAT(tcps, tcp_timeout_cached_alloc); 25959 mp = tcp->tcp_timercache; 25960 tcp->tcp_timercache = mp->b_next; 25961 mp->b_next = NULL; 25962 ASSERT(mp->b_wptr == NULL); 25963 } 25964 25965 CONN_INC_REF(connp); 25966 tcpt = (tcp_timer_t *)mp->b_rptr; 25967 tcpt->connp = connp; 25968 tcpt->tcpt_proc = f; 25969 /* 25970 * TCP timers are normal timeouts. Plus, they do not require more than 25971 * a 10 millisecond resolution. By choosing a coarser resolution and by 25972 * rounding up the expiration to the next resolution boundary, we can 25973 * batch timers in the callout subsystem to make TCP timers more 25974 * efficient. The roundup also protects short timers from expiring too 25975 * early before they have a chance to be cancelled. 25976 */ 25977 tcpt->tcpt_tid = timeout_generic(CALLOUT_NORMAL, tcp_timer_callback, mp, 25978 TICK_TO_NSEC(tim), CALLOUT_TCP_RESOLUTION, CALLOUT_FLAG_ROUNDUP); 25979 25980 return ((timeout_id_t)mp); 25981 } 25982 25983 static void 25984 tcp_timer_callback(void *arg) 25985 { 25986 mblk_t *mp = (mblk_t *)arg; 25987 tcp_timer_t *tcpt; 25988 conn_t *connp; 25989 25990 tcpt = (tcp_timer_t *)mp->b_rptr; 25991 connp = tcpt->connp; 25992 squeue_fill(connp->conn_sqp, mp, 25993 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 25994 } 25995 25996 static void 25997 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 25998 { 25999 tcp_timer_t *tcpt; 26000 conn_t *connp = (conn_t *)arg; 26001 tcp_t *tcp = connp->conn_tcp; 26002 26003 tcpt = (tcp_timer_t *)mp->b_rptr; 26004 ASSERT(connp == tcpt->connp); 26005 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 26006 26007 /* 26008 * If the TCP has reached the closed state, don't proceed any 26009 * further. This TCP logically does not exist on the system. 26010 * tcpt_proc could for example access queues, that have already 26011 * been qprocoff'ed off. Also see comments at the start of tcp_input 26012 */ 26013 if (tcp->tcp_state != TCPS_CLOSED) { 26014 (*tcpt->tcpt_proc)(connp); 26015 } else { 26016 tcp->tcp_timer_tid = 0; 26017 } 26018 tcp_timer_free(connp->conn_tcp, mp); 26019 } 26020 26021 /* 26022 * There is potential race with untimeout and the handler firing at the same 26023 * time. The mblock may be freed by the handler while we are trying to use 26024 * it. But since both should execute on the same squeue, this race should not 26025 * occur. 26026 */ 26027 clock_t 26028 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 26029 { 26030 mblk_t *mp = (mblk_t *)id; 26031 tcp_timer_t *tcpt; 26032 clock_t delta; 26033 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26034 26035 TCP_DBGSTAT(tcps, tcp_timeout_cancel_reqs); 26036 26037 if (mp == NULL) 26038 return (-1); 26039 26040 tcpt = (tcp_timer_t *)mp->b_rptr; 26041 ASSERT(tcpt->connp == connp); 26042 26043 delta = untimeout_default(tcpt->tcpt_tid, 0); 26044 26045 if (delta >= 0) { 26046 TCP_DBGSTAT(tcps, tcp_timeout_canceled); 26047 tcp_timer_free(connp->conn_tcp, mp); 26048 CONN_DEC_REF(connp); 26049 } 26050 26051 return (delta); 26052 } 26053 26054 /* 26055 * Allocate space for the timer event. The allocation looks like mblk, but it is 26056 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 26057 * 26058 * Dealing with failures: If we can't allocate from the timer cache we try 26059 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 26060 * points to b_rptr. 26061 * If we can't allocate anything using allocb_tryhard(), we perform a last 26062 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 26063 * save the actual allocation size in b_datap. 26064 */ 26065 mblk_t * 26066 tcp_timermp_alloc(int kmflags) 26067 { 26068 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 26069 kmflags & ~KM_PANIC); 26070 26071 if (mp != NULL) { 26072 mp->b_next = mp->b_prev = NULL; 26073 mp->b_rptr = (uchar_t *)(&mp[1]); 26074 mp->b_wptr = NULL; 26075 mp->b_datap = NULL; 26076 mp->b_queue = NULL; 26077 mp->b_cont = NULL; 26078 } else if (kmflags & KM_PANIC) { 26079 /* 26080 * Failed to allocate memory for the timer. Try allocating from 26081 * dblock caches. 26082 */ 26083 /* ipclassifier calls this from a constructor - hence no tcps */ 26084 TCP_G_STAT(tcp_timermp_allocfail); 26085 mp = allocb_tryhard(sizeof (tcp_timer_t)); 26086 if (mp == NULL) { 26087 size_t size = 0; 26088 /* 26089 * Memory is really low. Try tryhard allocation. 26090 * 26091 * ipclassifier calls this from a constructor - 26092 * hence no tcps 26093 */ 26094 TCP_G_STAT(tcp_timermp_allocdblfail); 26095 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 26096 sizeof (tcp_timer_t), &size, kmflags); 26097 mp->b_rptr = (uchar_t *)(&mp[1]); 26098 mp->b_next = mp->b_prev = NULL; 26099 mp->b_wptr = (uchar_t *)-1; 26100 mp->b_datap = (dblk_t *)size; 26101 mp->b_queue = NULL; 26102 mp->b_cont = NULL; 26103 } 26104 ASSERT(mp->b_wptr != NULL); 26105 } 26106 /* ipclassifier calls this from a constructor - hence no tcps */ 26107 TCP_G_DBGSTAT(tcp_timermp_alloced); 26108 26109 return (mp); 26110 } 26111 26112 /* 26113 * Free per-tcp timer cache. 26114 * It can only contain entries from tcp_timercache. 26115 */ 26116 void 26117 tcp_timermp_free(tcp_t *tcp) 26118 { 26119 mblk_t *mp; 26120 26121 while ((mp = tcp->tcp_timercache) != NULL) { 26122 ASSERT(mp->b_wptr == NULL); 26123 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26124 kmem_cache_free(tcp_timercache, mp); 26125 } 26126 } 26127 26128 /* 26129 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26130 * events there already (currently at most two events are cached). 26131 * If the event is not allocated from the timer cache, free it right away. 26132 */ 26133 static void 26134 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26135 { 26136 mblk_t *mp1 = tcp->tcp_timercache; 26137 tcp_stack_t *tcps = tcp->tcp_tcps; 26138 26139 if (mp->b_wptr != NULL) { 26140 /* 26141 * This allocation is not from a timer cache, free it right 26142 * away. 26143 */ 26144 if (mp->b_wptr != (uchar_t *)-1) 26145 freeb(mp); 26146 else 26147 kmem_free(mp, (size_t)mp->b_datap); 26148 } else if (mp1 == NULL || mp1->b_next == NULL) { 26149 /* Cache this timer block for future allocations */ 26150 mp->b_rptr = (uchar_t *)(&mp[1]); 26151 mp->b_next = mp1; 26152 tcp->tcp_timercache = mp; 26153 } else { 26154 kmem_cache_free(tcp_timercache, mp); 26155 TCP_DBGSTAT(tcps, tcp_timermp_freed); 26156 } 26157 } 26158 26159 /* 26160 * End of TCP Timers implementation. 26161 */ 26162 26163 /* 26164 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26165 * on the specified backing STREAMS q. Note, the caller may make the 26166 * decision to call based on the tcp_t.tcp_flow_stopped value which 26167 * when check outside the q's lock is only an advisory check ... 26168 */ 26169 26170 void 26171 tcp_setqfull(tcp_t *tcp) 26172 { 26173 queue_t *q = tcp->tcp_wq; 26174 tcp_stack_t *tcps = tcp->tcp_tcps; 26175 26176 if (!(q->q_flag & QFULL)) { 26177 mutex_enter(QLOCK(q)); 26178 if (!(q->q_flag & QFULL)) { 26179 /* still need to set QFULL */ 26180 q->q_flag |= QFULL; 26181 tcp->tcp_flow_stopped = B_TRUE; 26182 mutex_exit(QLOCK(q)); 26183 TCP_STAT(tcps, tcp_flwctl_on); 26184 } else { 26185 mutex_exit(QLOCK(q)); 26186 } 26187 } 26188 } 26189 26190 void 26191 tcp_clrqfull(tcp_t *tcp) 26192 { 26193 queue_t *q = tcp->tcp_wq; 26194 26195 if (q->q_flag & QFULL) { 26196 mutex_enter(QLOCK(q)); 26197 if (q->q_flag & QFULL) { 26198 q->q_flag &= ~QFULL; 26199 tcp->tcp_flow_stopped = B_FALSE; 26200 mutex_exit(QLOCK(q)); 26201 if (q->q_flag & QWANTW) 26202 qbackenable(q, 0); 26203 } else { 26204 mutex_exit(QLOCK(q)); 26205 } 26206 } 26207 } 26208 26209 26210 /* 26211 * kstats related to squeues i.e. not per IP instance 26212 */ 26213 static void * 26214 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26215 { 26216 kstat_t *ksp; 26217 26218 tcp_g_stat_t template = { 26219 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26220 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26221 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26222 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26223 }; 26224 26225 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26226 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26227 KSTAT_FLAG_VIRTUAL); 26228 26229 if (ksp == NULL) 26230 return (NULL); 26231 26232 bcopy(&template, tcp_g_statp, sizeof (template)); 26233 ksp->ks_data = (void *)tcp_g_statp; 26234 26235 kstat_install(ksp); 26236 return (ksp); 26237 } 26238 26239 static void 26240 tcp_g_kstat_fini(kstat_t *ksp) 26241 { 26242 if (ksp != NULL) { 26243 kstat_delete(ksp); 26244 } 26245 } 26246 26247 26248 static void * 26249 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26250 { 26251 kstat_t *ksp; 26252 26253 tcp_stat_t template = { 26254 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26255 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26256 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26257 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26258 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26259 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26260 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26261 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26262 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26263 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26264 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26265 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26266 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26267 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26268 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26269 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26270 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26271 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26272 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26273 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26274 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26275 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26276 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26277 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26278 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26279 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26280 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26281 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26282 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26283 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26284 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26285 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26286 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26287 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26288 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26289 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26290 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26291 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26292 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26293 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26294 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26295 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26296 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26297 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26298 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26299 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26300 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26301 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26302 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26303 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26304 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26305 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26306 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26307 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26308 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26309 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26310 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26311 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26312 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26313 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26314 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26315 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26316 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26317 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26318 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26319 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26320 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26321 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26322 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26323 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26324 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26325 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26326 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26327 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26328 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26329 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26330 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26331 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26332 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26333 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26334 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26335 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26336 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26337 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26338 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26339 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26340 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 26341 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 26342 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 26343 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 26344 }; 26345 26346 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26347 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26348 KSTAT_FLAG_VIRTUAL, stackid); 26349 26350 if (ksp == NULL) 26351 return (NULL); 26352 26353 bcopy(&template, tcps_statisticsp, sizeof (template)); 26354 ksp->ks_data = (void *)tcps_statisticsp; 26355 ksp->ks_private = (void *)(uintptr_t)stackid; 26356 26357 kstat_install(ksp); 26358 return (ksp); 26359 } 26360 26361 static void 26362 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26363 { 26364 if (ksp != NULL) { 26365 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26366 kstat_delete_netstack(ksp, stackid); 26367 } 26368 } 26369 26370 /* 26371 * TCP Kstats implementation 26372 */ 26373 static void * 26374 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26375 { 26376 kstat_t *ksp; 26377 26378 tcp_named_kstat_t template = { 26379 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26380 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26381 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26382 { "maxConn", KSTAT_DATA_INT32, 0 }, 26383 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26384 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26385 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26386 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26387 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26388 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26389 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26390 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26391 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26392 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26393 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26394 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26395 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26396 { "outAck", KSTAT_DATA_UINT32, 0 }, 26397 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26398 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26399 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26400 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26401 { "outControl", KSTAT_DATA_UINT32, 0 }, 26402 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26403 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26404 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26405 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26406 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26407 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26408 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26409 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26410 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26411 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26412 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26413 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26414 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26415 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26416 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26417 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26418 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26419 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26420 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26421 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26422 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26423 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26424 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26425 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26426 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26427 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26428 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26429 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26430 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26431 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26432 }; 26433 26434 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26435 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26436 26437 if (ksp == NULL) 26438 return (NULL); 26439 26440 template.rtoAlgorithm.value.ui32 = 4; 26441 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26442 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26443 template.maxConn.value.i32 = -1; 26444 26445 bcopy(&template, ksp->ks_data, sizeof (template)); 26446 ksp->ks_update = tcp_kstat_update; 26447 ksp->ks_private = (void *)(uintptr_t)stackid; 26448 26449 kstat_install(ksp); 26450 return (ksp); 26451 } 26452 26453 static void 26454 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26455 { 26456 if (ksp != NULL) { 26457 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26458 kstat_delete_netstack(ksp, stackid); 26459 } 26460 } 26461 26462 static int 26463 tcp_kstat_update(kstat_t *kp, int rw) 26464 { 26465 tcp_named_kstat_t *tcpkp; 26466 tcp_t *tcp; 26467 connf_t *connfp; 26468 conn_t *connp; 26469 int i; 26470 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26471 netstack_t *ns; 26472 tcp_stack_t *tcps; 26473 ip_stack_t *ipst; 26474 26475 if ((kp == NULL) || (kp->ks_data == NULL)) 26476 return (EIO); 26477 26478 if (rw == KSTAT_WRITE) 26479 return (EACCES); 26480 26481 ns = netstack_find_by_stackid(stackid); 26482 if (ns == NULL) 26483 return (-1); 26484 tcps = ns->netstack_tcp; 26485 if (tcps == NULL) { 26486 netstack_rele(ns); 26487 return (-1); 26488 } 26489 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26490 26491 tcpkp->currEstab.value.ui32 = 0; 26492 26493 ipst = ns->netstack_ip; 26494 26495 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26496 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26497 connp = NULL; 26498 while ((connp = 26499 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26500 tcp = connp->conn_tcp; 26501 switch (tcp_snmp_state(tcp)) { 26502 case MIB2_TCP_established: 26503 case MIB2_TCP_closeWait: 26504 tcpkp->currEstab.value.ui32++; 26505 break; 26506 } 26507 } 26508 } 26509 26510 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26511 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26512 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26513 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26514 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26515 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26516 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26517 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26518 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26519 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26520 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26521 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26522 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26523 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26524 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26525 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26526 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26527 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26528 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26529 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26530 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26531 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26532 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26533 tcpkp->inDataInorderSegs.value.ui32 = 26534 tcps->tcps_mib.tcpInDataInorderSegs; 26535 tcpkp->inDataInorderBytes.value.ui32 = 26536 tcps->tcps_mib.tcpInDataInorderBytes; 26537 tcpkp->inDataUnorderSegs.value.ui32 = 26538 tcps->tcps_mib.tcpInDataUnorderSegs; 26539 tcpkp->inDataUnorderBytes.value.ui32 = 26540 tcps->tcps_mib.tcpInDataUnorderBytes; 26541 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26542 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26543 tcpkp->inDataPartDupSegs.value.ui32 = 26544 tcps->tcps_mib.tcpInDataPartDupSegs; 26545 tcpkp->inDataPartDupBytes.value.ui32 = 26546 tcps->tcps_mib.tcpInDataPartDupBytes; 26547 tcpkp->inDataPastWinSegs.value.ui32 = 26548 tcps->tcps_mib.tcpInDataPastWinSegs; 26549 tcpkp->inDataPastWinBytes.value.ui32 = 26550 tcps->tcps_mib.tcpInDataPastWinBytes; 26551 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26552 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26553 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26554 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26555 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26556 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26557 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26558 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26559 tcpkp->timKeepaliveProbe.value.ui32 = 26560 tcps->tcps_mib.tcpTimKeepaliveProbe; 26561 tcpkp->timKeepaliveDrop.value.ui32 = 26562 tcps->tcps_mib.tcpTimKeepaliveDrop; 26563 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26564 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26565 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26566 tcpkp->outSackRetransSegs.value.ui32 = 26567 tcps->tcps_mib.tcpOutSackRetransSegs; 26568 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26569 26570 netstack_rele(ns); 26571 return (0); 26572 } 26573 26574 void 26575 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26576 { 26577 uint16_t hdr_len; 26578 ipha_t *ipha; 26579 uint8_t *nexthdrp; 26580 tcph_t *tcph; 26581 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26582 26583 /* Already has an eager */ 26584 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26585 TCP_STAT(tcps, tcp_reinput_syn); 26586 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 26587 connp, SQTAG_TCP_REINPUT_EAGER); 26588 return; 26589 } 26590 26591 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26592 case IPV4_VERSION: 26593 ipha = (ipha_t *)mp->b_rptr; 26594 hdr_len = IPH_HDR_LENGTH(ipha); 26595 break; 26596 case IPV6_VERSION: 26597 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26598 &hdr_len, &nexthdrp)) { 26599 CONN_DEC_REF(connp); 26600 freemsg(mp); 26601 return; 26602 } 26603 break; 26604 } 26605 26606 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26607 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26608 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26609 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26610 } 26611 26612 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 26613 SQTAG_TCP_REINPUT); 26614 } 26615 26616 static squeue_func_t 26617 tcp_squeue_switch(int val) 26618 { 26619 squeue_func_t rval = squeue_fill; 26620 26621 switch (val) { 26622 case 1: 26623 rval = squeue_enter_nodrain; 26624 break; 26625 case 2: 26626 rval = squeue_enter; 26627 break; 26628 default: 26629 break; 26630 } 26631 return (rval); 26632 } 26633 26634 /* 26635 * This is called once for each squeue - globally for all stack 26636 * instances. 26637 */ 26638 static void 26639 tcp_squeue_add(squeue_t *sqp) 26640 { 26641 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26642 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26643 26644 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26645 tcp_time_wait->tcp_time_wait_tid = 26646 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 26647 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 26648 CALLOUT_FLAG_ROUNDUP); 26649 if (tcp_free_list_max_cnt == 0) { 26650 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26651 max_ncpus : boot_max_ncpus); 26652 26653 /* 26654 * Limit number of entries to 1% of availble memory / tcp_ncpus 26655 */ 26656 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26657 (tcp_ncpus * sizeof (tcp_t) * 100); 26658 } 26659 tcp_time_wait->tcp_free_list_cnt = 0; 26660 } 26661