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 <netinet/in.h> 68 #include <netinet/tcp.h> 69 #include <netinet/ip6.h> 70 #include <netinet/icmp6.h> 71 #include <net/if.h> 72 #include <net/route.h> 73 #include <inet/ipsec_impl.h> 74 75 #include <inet/common.h> 76 #include <inet/ip.h> 77 #include <inet/ip_impl.h> 78 #include <inet/ip6.h> 79 #include <inet/ip_ndp.h> 80 #include <inet/mi.h> 81 #include <inet/mib2.h> 82 #include <inet/nd.h> 83 #include <inet/optcom.h> 84 #include <inet/snmpcom.h> 85 #include <inet/kstatcom.h> 86 #include <inet/tcp.h> 87 #include <inet/tcp_impl.h> 88 #include <net/pfkeyv2.h> 89 #include <inet/ipsec_info.h> 90 #include <inet/ipdrop.h> 91 92 #include <inet/ipclassifier.h> 93 #include <inet/ip_ire.h> 94 #include <inet/ip_ftable.h> 95 #include <inet/ip_if.h> 96 #include <inet/ipp_common.h> 97 #include <inet/ip_netinfo.h> 98 #include <sys/squeue.h> 99 #include <inet/kssl/ksslapi.h> 100 #include <sys/tsol/label.h> 101 #include <sys/tsol/tnet.h> 102 #include <rpc/pmap_prot.h> 103 104 /* 105 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433) 106 * 107 * (Read the detailed design doc in PSARC case directory) 108 * 109 * The entire tcp state is contained in tcp_t and conn_t structure 110 * which are allocated in tandem using ipcl_conn_create() and passing 111 * IPCL_CONNTCP as a flag. We use 'conn_ref' and 'conn_lock' to protect 112 * the references on the tcp_t. The tcp_t structure is never compressed 113 * and packets always land on the correct TCP perimeter from the time 114 * eager is created till the time tcp_t dies (as such the old mentat 115 * TCP global queue is not used for detached state and no IPSEC checking 116 * is required). The global queue is still allocated to send out resets 117 * for connection which have no listeners and IP directly calls 118 * tcp_xmit_listeners_reset() which does any policy check. 119 * 120 * Protection and Synchronisation mechanism: 121 * 122 * The tcp data structure does not use any kind of lock for protecting 123 * its state but instead uses 'squeues' for mutual exclusion from various 124 * read and write side threads. To access a tcp member, the thread should 125 * always be behind squeue (via squeue_enter, squeue_enter_nodrain, or 126 * squeue_fill). Since the squeues allow a direct function call, caller 127 * can pass any tcp function having prototype of edesc_t as argument 128 * (different from traditional STREAMs model where packets come in only 129 * designated entry points). The list of functions that can be directly 130 * called via squeue are listed before the usual function prototype. 131 * 132 * Referencing: 133 * 134 * TCP is MT-Hot and we use a reference based scheme to make sure that the 135 * tcp structure doesn't disappear when its needed. When the application 136 * creates an outgoing connection or accepts an incoming connection, we 137 * start out with 2 references on 'conn_ref'. One for TCP and one for IP. 138 * The IP reference is just a symbolic reference since ip_tcpclose() 139 * looks at tcp structure after tcp_close_output() returns which could 140 * have dropped the last TCP reference. So as long as the connection is 141 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the 142 * conn_t. The classifier puts its own reference when the connection is 143 * inserted in listen or connected hash. Anytime a thread needs to enter 144 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr 145 * on write side or by doing a classify on read side and then puts a 146 * reference on the conn before doing squeue_enter/tryenter/fill. For 147 * read side, the classifier itself puts the reference under fanout lock 148 * to make sure that tcp can't disappear before it gets processed. The 149 * squeue will drop this reference automatically so the called function 150 * doesn't have to do a DEC_REF. 151 * 152 * Opening a new connection: 153 * 154 * The outgoing connection open is pretty simple. tcp_open() does the 155 * work in creating the conn/tcp structure and initializing it. The 156 * squeue assignment is done based on the CPU the application 157 * is running on. So for outbound connections, processing is always done 158 * on application CPU which might be different from the incoming CPU 159 * being interrupted by the NIC. An optimal way would be to figure out 160 * the NIC <-> CPU binding at listen time, and assign the outgoing 161 * connection to the squeue attached to the CPU that will be interrupted 162 * for incoming packets (we know the NIC based on the bind IP address). 163 * This might seem like a problem if more data is going out but the 164 * fact is that in most cases the transmit is ACK driven transmit where 165 * the outgoing data normally sits on TCP's xmit queue waiting to be 166 * transmitted. 167 * 168 * Accepting a connection: 169 * 170 * This is a more interesting case because of various races involved in 171 * establishing a eager in its own perimeter. Read the meta comment on 172 * top of tcp_conn_request(). But briefly, the squeue is picked by 173 * ip_tcp_input()/ip_fanout_tcp_v6() based on the interrupted CPU. 174 * 175 * Closing a connection: 176 * 177 * The close is fairly straight forward. tcp_close() calls tcp_close_output() 178 * via squeue to do the close and mark the tcp as detached if the connection 179 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its 180 * reference but tcp_close() drop IP's reference always. So if tcp was 181 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP 182 * and 1 because it is in classifier's connected hash. This is the condition 183 * we use to determine that its OK to clean up the tcp outside of squeue 184 * when time wait expires (check the ref under fanout and conn_lock and 185 * if it is 2, remove it from fanout hash and kill it). 186 * 187 * Although close just drops the necessary references and marks the 188 * tcp_detached state, tcp_close needs to know the tcp_detached has been 189 * set (under squeue) before letting the STREAM go away (because a 190 * inbound packet might attempt to go up the STREAM while the close 191 * has happened and tcp_detached is not set). So a special lock and 192 * flag is used along with a condition variable (tcp_closelock, tcp_closed, 193 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked 194 * tcp_detached. 195 * 196 * Special provisions and fast paths: 197 * 198 * We make special provision for (AF_INET, SOCK_STREAM) sockets which 199 * can't have 'ipv6_recvpktinfo' set and for these type of sockets, IP 200 * will never send a M_CTL to TCP. As such, ip_tcp_input() which handles 201 * all TCP packets from the wire makes a IPCL_IS_TCP4_CONNECTED_NO_POLICY 202 * check to send packets directly to tcp_rput_data via squeue. Everyone 203 * else comes through tcp_input() on the read side. 204 * 205 * We also make special provisions for sockfs by marking tcp_issocket 206 * whenever we have only sockfs on top of TCP. This allows us to skip 207 * putting the tcp in acceptor hash since a sockfs listener can never 208 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM 209 * since eager has already been allocated and the accept now happens 210 * on acceptor STREAM. There is a big blob of comment on top of 211 * tcp_conn_request explaining the new accept. When socket is POP'd, 212 * sockfs sends us an ioctl to mark the fact and we go back to old 213 * behaviour. Once tcp_issocket is unset, its never set for the 214 * life of that connection. 215 * 216 * In support of on-board asynchronous DMA hardware (e.g. Intel I/OAT) 217 * two consoldiation private KAPIs are used to enqueue M_DATA mblk_t's 218 * directly to the socket (sodirect) and start an asynchronous copyout 219 * to a user-land receive-side buffer (uioa) when a blocking socket read 220 * (e.g. read, recv, ...) is pending. 221 * 222 * This is accomplished when tcp_issocket is set and tcp_sodirect is not 223 * NULL so points to an sodirect_t and if marked enabled then we enqueue 224 * all mblk_t's directly to the socket. 225 * 226 * Further, if the sodirect_t sod_uioa and if marked enabled (due to a 227 * blocking socket read, e.g. user-land read, recv, ...) then an asynchronous 228 * copyout will be started directly to the user-land uio buffer. Also, as we 229 * have a pending read, TCP's push logic can take into account the number of 230 * bytes to be received and only awake the blocked read()er when the uioa_t 231 * byte count has been satisfied. 232 * 233 * IPsec notes : 234 * 235 * Since a packet is always executed on the correct TCP perimeter 236 * all IPsec processing is defered to IP including checking new 237 * connections and setting IPSEC policies for new connection. The 238 * only exception is tcp_xmit_listeners_reset() which is called 239 * directly from IP and needs to policy check to see if TH_RST 240 * can be sent out. 241 * 242 * PFHooks notes : 243 * 244 * For mdt case, one meta buffer contains multiple packets. Mblks for every 245 * packet are assembled and passed to the hooks. When packets are blocked, 246 * or boundary of any packet is changed, the mdt processing is stopped, and 247 * packets of the meta buffer are send to the IP path one by one. 248 */ 249 250 /* 251 * Values for squeue switch: 252 * 1: squeue_enter_nodrain 253 * 2: squeue_enter 254 * 3: squeue_fill 255 */ 256 int tcp_squeue_close = 2; /* Setable in /etc/system */ 257 int tcp_squeue_wput = 2; 258 259 squeue_func_t tcp_squeue_close_proc; 260 squeue_func_t tcp_squeue_wput_proc; 261 262 /* 263 * Macros for sodirect: 264 * 265 * SOD_PTR_ENTER(tcp, sodp) - for the tcp_t pointer "tcp" set the 266 * sodirect_t pointer "sodp" to the socket/tcp shared sodirect_t 267 * if it exists and is enabled, else to NULL. Note, in the current 268 * sodirect implementation the sod_lockp must not be held across any 269 * STREAMS call (e.g. putnext) else a "recursive mutex_enter" PANIC 270 * will result as sod_lockp is the streamhead stdata.sd_lock. 271 * 272 * SOD_NOT_ENABLED(tcp) - return true if not a sodirect tcp_t or the 273 * sodirect_t isn't enabled, usefull for ASSERT()ing that a recieve 274 * side tcp code path dealing with a tcp_rcv_list or putnext() isn't 275 * being used when sodirect code paths should be. 276 */ 277 278 #define SOD_PTR_ENTER(tcp, sodp) \ 279 (sodp) = (tcp)->tcp_sodirect; \ 280 \ 281 if ((sodp) != NULL) { \ 282 mutex_enter((sodp)->sod_lockp); \ 283 if (!((sodp)->sod_state & SOD_ENABLED)) { \ 284 mutex_exit((sodp)->sod_lockp); \ 285 (sodp) = NULL; \ 286 } \ 287 } 288 289 #define SOD_NOT_ENABLED(tcp) \ 290 ((tcp)->tcp_sodirect == NULL || \ 291 !((tcp)->tcp_sodirect->sod_state & SOD_ENABLED)) 292 293 /* 294 * This controls how tiny a write must be before we try to copy it 295 * into the the mblk on the tail of the transmit queue. Not much 296 * speedup is observed for values larger than sixteen. Zero will 297 * disable the optimisation. 298 */ 299 int tcp_tx_pull_len = 16; 300 301 /* 302 * TCP Statistics. 303 * 304 * How TCP statistics work. 305 * 306 * There are two types of statistics invoked by two macros. 307 * 308 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 309 * supposed to be used in non MT-hot paths of the code. 310 * 311 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 312 * supposed to be used for DEBUG purposes and may be used on a hot path. 313 * 314 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 315 * (use "kstat tcp" to get them). 316 * 317 * There is also additional debugging facility that marks tcp_clean_death() 318 * instances and saves them in tcp_t structure. It is triggered by 319 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 320 * tcp_clean_death() calls that counts the number of times each tag was hit. It 321 * is triggered by TCP_CLD_COUNTERS define. 322 * 323 * How to add new counters. 324 * 325 * 1) Add a field in the tcp_stat structure describing your counter. 326 * 2) Add a line in the template in tcp_kstat2_init() with the name 327 * of the counter. 328 * 329 * IMPORTANT!! - make sure that both are in sync !! 330 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 331 * 332 * Please avoid using private counters which are not kstat-exported. 333 * 334 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 335 * in tcp_t structure. 336 * 337 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 338 */ 339 340 #ifndef TCP_DEBUG_COUNTER 341 #ifdef DEBUG 342 #define TCP_DEBUG_COUNTER 1 343 #else 344 #define TCP_DEBUG_COUNTER 0 345 #endif 346 #endif 347 348 #define TCP_CLD_COUNTERS 0 349 350 #define TCP_TAG_CLEAN_DEATH 1 351 #define TCP_MAX_CLEAN_DEATH_TAG 32 352 353 #ifdef lint 354 static int _lint_dummy_; 355 #endif 356 357 #if TCP_CLD_COUNTERS 358 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 359 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 360 #elif defined(lint) 361 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 362 #else 363 #define TCP_CLD_STAT(x) 364 #endif 365 366 #if TCP_DEBUG_COUNTER 367 #define TCP_DBGSTAT(tcps, x) \ 368 atomic_add_64(&((tcps)->tcps_statistics.x.value.ui64), 1) 369 #define TCP_G_DBGSTAT(x) \ 370 atomic_add_64(&(tcp_g_statistics.x.value.ui64), 1) 371 #elif defined(lint) 372 #define TCP_DBGSTAT(tcps, x) ASSERT(_lint_dummy_ == 0); 373 #define TCP_G_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 374 #else 375 #define TCP_DBGSTAT(tcps, x) 376 #define TCP_G_DBGSTAT(x) 377 #endif 378 379 #define TCP_G_STAT(x) (tcp_g_statistics.x.value.ui64++) 380 381 tcp_g_stat_t tcp_g_statistics; 382 kstat_t *tcp_g_kstat; 383 384 /* 385 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 386 * tcp write side. 387 */ 388 #define CALL_IP_WPUT(connp, q, mp) { \ 389 tcp_stack_t *tcps; \ 390 \ 391 tcps = connp->conn_netstack->netstack_tcp; \ 392 ASSERT(((q)->q_flag & QREADR) == 0); \ 393 TCP_DBGSTAT(tcps, tcp_ip_output); \ 394 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 395 } 396 397 /* Macros for timestamp comparisons */ 398 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 399 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 400 401 /* 402 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 403 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 404 * by adding three components: a time component which grows by 1 every 4096 405 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 406 * a per-connection component which grows by 125000 for every new connection; 407 * and an "extra" component that grows by a random amount centered 408 * approximately on 64000. This causes the the ISS generator to cycle every 409 * 4.89 hours if no TCP connections are made, and faster if connections are 410 * made. 411 * 412 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 413 * components: a time component which grows by 250000 every second; and 414 * a per-connection component which grows by 125000 for every new connections. 415 * 416 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 417 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 418 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 419 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 420 * password. 421 */ 422 #define ISS_INCR 250000 423 #define ISS_NSEC_SHT 12 424 425 static sin_t sin_null; /* Zero address for quick clears */ 426 static sin6_t sin6_null; /* Zero address for quick clears */ 427 428 /* 429 * This implementation follows the 4.3BSD interpretation of the urgent 430 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 431 * incompatible changes in protocols like telnet and rlogin. 432 */ 433 #define TCP_OLD_URP_INTERPRETATION 1 434 435 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 436 (TCP_IS_DETACHED(tcp) && \ 437 (!(tcp)->tcp_hard_binding)) 438 439 /* 440 * TCP reassembly macros. We hide starting and ending sequence numbers in 441 * b_next and b_prev of messages on the reassembly queue. The messages are 442 * chained using b_cont. These macros are used in tcp_reass() so we don't 443 * have to see the ugly casts and assignments. 444 */ 445 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 446 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 447 (mblk_t *)(uintptr_t)(u)) 448 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 449 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 450 (mblk_t *)(uintptr_t)(u)) 451 452 /* 453 * Implementation of TCP Timers. 454 * ============================= 455 * 456 * INTERFACE: 457 * 458 * There are two basic functions dealing with tcp timers: 459 * 460 * timeout_id_t tcp_timeout(connp, func, time) 461 * clock_t tcp_timeout_cancel(connp, timeout_id) 462 * TCP_TIMER_RESTART(tcp, intvl) 463 * 464 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 465 * after 'time' ticks passed. The function called by timeout() must adhere to 466 * the same restrictions as a driver soft interrupt handler - it must not sleep 467 * or call other functions that might sleep. The value returned is the opaque 468 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 469 * cancel the request. The call to tcp_timeout() may fail in which case it 470 * returns zero. This is different from the timeout(9F) function which never 471 * fails. 472 * 473 * The call-back function 'func' always receives 'connp' as its single 474 * argument. It is always executed in the squeue corresponding to the tcp 475 * structure. The tcp structure is guaranteed to be present at the time the 476 * call-back is called. 477 * 478 * NOTE: The call-back function 'func' is never called if tcp is in 479 * the TCPS_CLOSED state. 480 * 481 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 482 * request. locks acquired by the call-back routine should not be held across 483 * the call to tcp_timeout_cancel() or a deadlock may result. 484 * 485 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 486 * Otherwise, it returns an integer value greater than or equal to 0. In 487 * particular, if the call-back function is already placed on the squeue, it can 488 * not be canceled. 489 * 490 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 491 * within squeue context corresponding to the tcp instance. Since the 492 * call-back is also called via the same squeue, there are no race 493 * conditions described in untimeout(9F) manual page since all calls are 494 * strictly serialized. 495 * 496 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 497 * stored in tcp_timer_tid and starts a new one using 498 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 499 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 500 * field. 501 * 502 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 503 * call-back may still be called, so it is possible tcp_timer() will be 504 * called several times. This should not be a problem since tcp_timer() 505 * should always check the tcp instance state. 506 * 507 * 508 * IMPLEMENTATION: 509 * 510 * TCP timers are implemented using three-stage process. The call to 511 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 512 * when the timer expires. The tcp_timer_callback() arranges the call of the 513 * tcp_timer_handler() function via squeue corresponding to the tcp 514 * instance. The tcp_timer_handler() calls actual requested timeout call-back 515 * and passes tcp instance as an argument to it. Information is passed between 516 * stages using the tcp_timer_t structure which contains the connp pointer, the 517 * tcp call-back to call and the timeout id returned by the timeout(9F). 518 * 519 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 520 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 521 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 522 * returns the pointer to this mblk. 523 * 524 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 525 * looks like a normal mblk without actual dblk attached to it. 526 * 527 * To optimize performance each tcp instance holds a small cache of timer 528 * mblocks. In the current implementation it caches up to two timer mblocks per 529 * tcp instance. The cache is preserved over tcp frees and is only freed when 530 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 531 * timer processing happens on a corresponding squeue, the cache manipulation 532 * does not require any locks. Experiments show that majority of timer mblocks 533 * allocations are satisfied from the tcp cache and do not involve kmem calls. 534 * 535 * The tcp_timeout() places a refhold on the connp instance which guarantees 536 * that it will be present at the time the call-back function fires. The 537 * tcp_timer_handler() drops the reference after calling the call-back, so the 538 * call-back function does not need to manipulate the references explicitly. 539 */ 540 541 typedef struct tcp_timer_s { 542 conn_t *connp; 543 void (*tcpt_proc)(void *); 544 timeout_id_t tcpt_tid; 545 } tcp_timer_t; 546 547 static kmem_cache_t *tcp_timercache; 548 kmem_cache_t *tcp_sack_info_cache; 549 kmem_cache_t *tcp_iphc_cache; 550 551 /* 552 * For scalability, we must not run a timer for every TCP connection 553 * in TIME_WAIT state. To see why, consider (for time wait interval of 554 * 4 minutes): 555 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 556 * 557 * This list is ordered by time, so you need only delete from the head 558 * until you get to entries which aren't old enough to delete yet. 559 * The list consists of only the detached TIME_WAIT connections. 560 * 561 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 562 * becomes detached TIME_WAIT (either by changing the state and already 563 * being detached or the other way around). This means that the TIME_WAIT 564 * state can be extended (up to doubled) if the connection doesn't become 565 * detached for a long time. 566 * 567 * The list manipulations (including tcp_time_wait_next/prev) 568 * are protected by the tcp_time_wait_lock. The content of the 569 * detached TIME_WAIT connections is protected by the normal perimeters. 570 * 571 * This list is per squeue and squeues are shared across the tcp_stack_t's. 572 * Things on tcp_time_wait_head remain associated with the tcp_stack_t 573 * and conn_netstack. 574 * The tcp_t's that are added to tcp_free_list are disassociated and 575 * have NULL tcp_tcps and conn_netstack pointers. 576 */ 577 typedef struct tcp_squeue_priv_s { 578 kmutex_t tcp_time_wait_lock; 579 timeout_id_t tcp_time_wait_tid; 580 tcp_t *tcp_time_wait_head; 581 tcp_t *tcp_time_wait_tail; 582 tcp_t *tcp_free_list; 583 uint_t tcp_free_list_cnt; 584 } tcp_squeue_priv_t; 585 586 /* 587 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 588 * Running it every 5 seconds seems to give the best results. 589 */ 590 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 591 592 /* 593 * To prevent memory hog, limit the number of entries in tcp_free_list 594 * to 1% of available memory / number of cpus 595 */ 596 uint_t tcp_free_list_max_cnt = 0; 597 598 #define TCP_XMIT_LOWATER 4096 599 #define TCP_XMIT_HIWATER 49152 600 #define TCP_RECV_LOWATER 2048 601 #define TCP_RECV_HIWATER 49152 602 603 /* 604 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 605 */ 606 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 607 608 #define TIDUSZ 4096 /* transport interface data unit size */ 609 610 /* 611 * Bind hash list size and has function. It has to be a power of 2 for 612 * hashing. 613 */ 614 #define TCP_BIND_FANOUT_SIZE 512 615 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 616 /* 617 * Size of listen and acceptor hash list. It has to be a power of 2 for 618 * hashing. 619 */ 620 #define TCP_FANOUT_SIZE 256 621 622 #ifdef _ILP32 623 #define TCP_ACCEPTOR_HASH(accid) \ 624 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 625 #else 626 #define TCP_ACCEPTOR_HASH(accid) \ 627 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 628 #endif /* _ILP32 */ 629 630 #define IP_ADDR_CACHE_SIZE 2048 631 #define IP_ADDR_CACHE_HASH(faddr) \ 632 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 633 634 /* Hash for HSPs uses all 32 bits, since both networks and hosts are in table */ 635 #define TCP_HSP_HASH_SIZE 256 636 637 #define TCP_HSP_HASH(addr) \ 638 (((addr>>24) ^ (addr >>16) ^ \ 639 (addr>>8) ^ (addr)) % TCP_HSP_HASH_SIZE) 640 641 /* 642 * TCP options struct returned from tcp_parse_options. 643 */ 644 typedef struct tcp_opt_s { 645 uint32_t tcp_opt_mss; 646 uint32_t tcp_opt_wscale; 647 uint32_t tcp_opt_ts_val; 648 uint32_t tcp_opt_ts_ecr; 649 tcp_t *tcp; 650 } tcp_opt_t; 651 652 /* 653 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 654 */ 655 656 #ifdef _BIG_ENDIAN 657 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 658 (TCPOPT_TSTAMP << 8) | 10) 659 #else 660 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 661 (TCPOPT_NOP << 8) | TCPOPT_NOP) 662 #endif 663 664 /* 665 * Flags returned from tcp_parse_options. 666 */ 667 #define TCP_OPT_MSS_PRESENT 1 668 #define TCP_OPT_WSCALE_PRESENT 2 669 #define TCP_OPT_TSTAMP_PRESENT 4 670 #define TCP_OPT_SACK_OK_PRESENT 8 671 #define TCP_OPT_SACK_PRESENT 16 672 673 /* TCP option length */ 674 #define TCPOPT_NOP_LEN 1 675 #define TCPOPT_MAXSEG_LEN 4 676 #define TCPOPT_WS_LEN 3 677 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 678 #define TCPOPT_TSTAMP_LEN 10 679 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 680 #define TCPOPT_SACK_OK_LEN 2 681 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 682 #define TCPOPT_REAL_SACK_LEN 4 683 #define TCPOPT_MAX_SACK_LEN 36 684 #define TCPOPT_HEADER_LEN 2 685 686 /* TCP cwnd burst factor. */ 687 #define TCP_CWND_INFINITE 65535 688 #define TCP_CWND_SS 3 689 #define TCP_CWND_NORMAL 5 690 691 /* Maximum TCP initial cwin (start/restart). */ 692 #define TCP_MAX_INIT_CWND 8 693 694 /* 695 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 696 * either tcp_slow_start_initial or tcp_slow_start_after idle 697 * depending on the caller. If the upper layer has not used the 698 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 699 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 700 * If the upper layer has changed set the tcp_init_cwnd, just use 701 * it to calculate the tcp_cwnd. 702 */ 703 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 704 { \ 705 if ((tcp)->tcp_init_cwnd == 0) { \ 706 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 707 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 708 } else { \ 709 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 710 } \ 711 tcp->tcp_cwnd_cnt = 0; \ 712 } 713 714 /* TCP Timer control structure */ 715 typedef struct tcpt_s { 716 pfv_t tcpt_pfv; /* The routine we are to call */ 717 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 718 } tcpt_t; 719 720 /* Host Specific Parameter structure */ 721 typedef struct tcp_hsp { 722 struct tcp_hsp *tcp_hsp_next; 723 in6_addr_t tcp_hsp_addr_v6; 724 in6_addr_t tcp_hsp_subnet_v6; 725 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 726 int32_t tcp_hsp_sendspace; 727 int32_t tcp_hsp_recvspace; 728 int32_t tcp_hsp_tstamp; 729 } tcp_hsp_t; 730 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 731 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 732 733 /* 734 * Functions called directly via squeue having a prototype of edesc_t. 735 */ 736 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 737 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 738 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 739 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 740 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 741 void tcp_input(void *arg, mblk_t *mp, void *arg2); 742 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 743 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 744 void tcp_output(void *arg, mblk_t *mp, void *arg2); 745 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 746 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 747 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 748 749 750 /* Prototype for TCP functions */ 751 static void tcp_random_init(void); 752 int tcp_random(void); 753 static void tcp_accept(tcp_t *tcp, mblk_t *mp); 754 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 755 tcp_t *eager); 756 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 757 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 758 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 759 boolean_t user_specified); 760 static void tcp_closei_local(tcp_t *tcp); 761 static void tcp_close_detached(tcp_t *tcp); 762 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 763 mblk_t *idmp, mblk_t **defermp); 764 static void tcp_connect(tcp_t *tcp, mblk_t *mp); 765 static void tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, 766 in_port_t dstport, uint_t srcid); 767 static void tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 768 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 769 uint32_t scope_id); 770 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 771 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 772 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 773 static char *tcp_display(tcp_t *tcp, char *, char); 774 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 775 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 776 static void tcp_eager_unlink(tcp_t *tcp); 777 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 778 int unixerr); 779 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 780 int tlierr, int unixerr); 781 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 782 cred_t *cr); 783 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 784 char *value, caddr_t cp, cred_t *cr); 785 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 786 char *value, caddr_t cp, cred_t *cr); 787 static int tcp_tpistate(tcp_t *tcp); 788 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 789 int caller_holds_lock); 790 static void tcp_bind_hash_remove(tcp_t *tcp); 791 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 792 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 793 static void tcp_acceptor_hash_remove(tcp_t *tcp); 794 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 795 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 796 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 797 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 798 void tcp_g_q_setup(tcp_stack_t *); 799 void tcp_g_q_create(tcp_stack_t *); 800 void tcp_g_q_destroy(tcp_stack_t *); 801 static int tcp_header_init_ipv4(tcp_t *tcp); 802 static int tcp_header_init_ipv6(tcp_t *tcp); 803 int tcp_init(tcp_t *tcp, queue_t *q); 804 static int tcp_init_values(tcp_t *tcp); 805 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 806 static mblk_t *tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, 807 t_scalar_t addr_length); 808 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 809 static void tcp_ip_notify(tcp_t *tcp); 810 static mblk_t *tcp_ire_mp(mblk_t *mp); 811 static void tcp_iss_init(tcp_t *tcp); 812 static void tcp_keepalive_killer(void *arg); 813 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 814 static void tcp_mss_set(tcp_t *tcp, uint32_t size, boolean_t do_ss); 815 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 816 int *do_disconnectp, int *t_errorp, int *sys_errorp); 817 static boolean_t tcp_allow_connopt_set(int level, int name); 818 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 819 int tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 820 int tcp_opt_set(queue_t *q, uint_t optset_context, int level, 821 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 822 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 823 mblk_t *mblk); 824 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 825 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 826 uchar_t *ptr, uint_t len); 827 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 828 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 829 tcp_stack_t *); 830 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 831 caddr_t cp, cred_t *cr); 832 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 833 caddr_t cp, cred_t *cr); 834 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 835 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 836 caddr_t cp, cred_t *cr); 837 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 838 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 839 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 840 static void tcp_reinit(tcp_t *tcp); 841 static void tcp_reinit_values(tcp_t *tcp); 842 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 843 tcp_t *thisstream, cred_t *cr); 844 845 static uint_t tcp_rcv_drain(queue_t *q, tcp_t *tcp); 846 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 847 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 848 static void tcp_ss_rexmit(tcp_t *tcp); 849 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 850 static void tcp_process_options(tcp_t *, tcph_t *); 851 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 852 static void tcp_rsrv(queue_t *q); 853 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 854 static int tcp_snmp_state(tcp_t *tcp); 855 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 856 cred_t *cr); 857 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 858 cred_t *cr); 859 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 860 cred_t *cr); 861 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 862 cred_t *cr); 863 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 864 cred_t *cr); 865 static void tcp_timer(void *arg); 866 static void tcp_timer_callback(void *); 867 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 868 boolean_t random); 869 static in_port_t tcp_get_next_priv_port(const tcp_t *); 870 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 871 void tcp_wput_accept(queue_t *q, mblk_t *mp); 872 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 873 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 874 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 875 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 876 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 877 const int num_sack_blk, int *usable, uint_t *snxt, 878 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 879 const int mdt_thres); 880 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 881 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 882 const int num_sack_blk, int *usable, uint_t *snxt, 883 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 884 const int mdt_thres); 885 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 886 int num_sack_blk); 887 static void tcp_wsrv(queue_t *q); 888 static int tcp_xmit_end(tcp_t *tcp); 889 static void tcp_ack_timer(void *arg); 890 static mblk_t *tcp_ack_mp(tcp_t *tcp); 891 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 892 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 893 zoneid_t zoneid, tcp_stack_t *, conn_t *connp); 894 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 895 uint32_t ack, int ctl); 896 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *); 897 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr, tcp_stack_t *); 898 static int setmaxps(queue_t *q, int maxpsz); 899 static void tcp_set_rto(tcp_t *, time_t); 900 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 901 boolean_t, boolean_t); 902 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 903 boolean_t ipsec_mctl); 904 static mblk_t *tcp_setsockopt_mp(int level, int cmd, 905 char *opt, int optlen); 906 static int tcp_build_hdrs(queue_t *, tcp_t *); 907 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 908 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 909 tcph_t *tcph); 910 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 911 static mblk_t *tcp_mdt_info_mp(mblk_t *); 912 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 913 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 914 const boolean_t, const uint32_t, const uint32_t, 915 const uint32_t, const uint32_t, tcp_stack_t *); 916 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 917 const uint_t, const uint_t, boolean_t *); 918 static mblk_t *tcp_lso_info_mp(mblk_t *); 919 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 920 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 921 extern mblk_t *tcp_timermp_alloc(int); 922 extern void tcp_timermp_free(tcp_t *); 923 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 924 static void tcp_stop_lingering(tcp_t *tcp); 925 static void tcp_close_linger_timeout(void *arg); 926 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 927 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 928 static void tcp_stack_fini(netstackid_t stackid, void *arg); 929 static void *tcp_g_kstat_init(tcp_g_stat_t *); 930 static void tcp_g_kstat_fini(kstat_t *); 931 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 932 static void tcp_kstat_fini(netstackid_t, kstat_t *); 933 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 934 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 935 static int tcp_kstat_update(kstat_t *kp, int rw); 936 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 937 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 938 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 939 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 940 tcph_t *tcph, mblk_t *idmp); 941 static squeue_func_t tcp_squeue_switch(int); 942 943 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t); 944 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *); 945 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *); 946 static int tcp_close(queue_t *, int); 947 static int tcpclose_accept(queue_t *); 948 949 static void tcp_squeue_add(squeue_t *); 950 static boolean_t tcp_zcopy_check(tcp_t *); 951 static void tcp_zcopy_notify(tcp_t *); 952 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 953 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 954 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 955 956 extern void tcp_kssl_input(tcp_t *, mblk_t *); 957 958 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 959 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 960 961 /* 962 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 963 * 964 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 965 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 966 * (defined in tcp.h) needs to be filled in and passed into the kernel 967 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 968 * structure contains the four-tuple of a TCP connection and a range of TCP 969 * states (specified by ac_start and ac_end). The use of wildcard addresses 970 * and ports is allowed. Connections with a matching four tuple and a state 971 * within the specified range will be aborted. The valid states for the 972 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 973 * inclusive. 974 * 975 * An application which has its connection aborted by this ioctl will receive 976 * an error that is dependent on the connection state at the time of the abort. 977 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 978 * though a RST packet has been received. If the connection state is equal to 979 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 980 * and all resources associated with the connection will be freed. 981 */ 982 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 983 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 984 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 985 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *, tcp_stack_t *tcps); 986 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 987 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 988 boolean_t, tcp_stack_t *); 989 990 static struct module_info tcp_rinfo = { 991 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 992 }; 993 994 static struct module_info tcp_winfo = { 995 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 996 }; 997 998 /* 999 * Entry points for TCP as a device. The normal case which supports 1000 * the TCP functionality. 1001 * We have separate open functions for the /dev/tcp and /dev/tcp6 devices. 1002 */ 1003 struct qinit tcp_rinitv4 = { 1004 NULL, (pfi_t)tcp_rsrv, tcp_openv4, tcp_close, NULL, &tcp_rinfo 1005 }; 1006 1007 struct qinit tcp_rinitv6 = { 1008 NULL, (pfi_t)tcp_rsrv, tcp_openv6, tcp_close, NULL, &tcp_rinfo 1009 }; 1010 1011 struct qinit tcp_winit = { 1012 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1013 }; 1014 1015 /* Initial entry point for TCP in socket mode. */ 1016 struct qinit tcp_sock_winit = { 1017 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1018 }; 1019 1020 /* 1021 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 1022 * an accept. Avoid allocating data structures since eager has already 1023 * been created. 1024 */ 1025 struct qinit tcp_acceptor_rinit = { 1026 NULL, (pfi_t)tcp_rsrv, NULL, tcpclose_accept, NULL, &tcp_winfo 1027 }; 1028 1029 struct qinit tcp_acceptor_winit = { 1030 (pfi_t)tcp_wput_accept, NULL, NULL, NULL, NULL, &tcp_winfo 1031 }; 1032 1033 /* 1034 * Entry points for TCP loopback (read side only) 1035 * The open routine is only used for reopens, thus no need to 1036 * have a separate one for tcp_openv6. 1037 */ 1038 struct qinit tcp_loopback_rinit = { 1039 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_openv4, tcp_close, (pfi_t)0, 1040 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 1041 }; 1042 1043 /* For AF_INET aka /dev/tcp */ 1044 struct streamtab tcpinfov4 = { 1045 &tcp_rinitv4, &tcp_winit 1046 }; 1047 1048 /* For AF_INET6 aka /dev/tcp6 */ 1049 struct streamtab tcpinfov6 = { 1050 &tcp_rinitv6, &tcp_winit 1051 }; 1052 1053 /* 1054 * Have to ensure that tcp_g_q_close is not done by an 1055 * interrupt thread. 1056 */ 1057 static taskq_t *tcp_taskq; 1058 1059 /* Setable only in /etc/system. Move to ndd? */ 1060 boolean_t tcp_icmp_source_quench = B_FALSE; 1061 1062 /* 1063 * Following assumes TPI alignment requirements stay along 32 bit 1064 * boundaries 1065 */ 1066 #define ROUNDUP32(x) \ 1067 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1068 1069 /* Template for response to info request. */ 1070 static struct T_info_ack tcp_g_t_info_ack = { 1071 T_INFO_ACK, /* PRIM_type */ 1072 0, /* TSDU_size */ 1073 T_INFINITE, /* ETSDU_size */ 1074 T_INVALID, /* CDATA_size */ 1075 T_INVALID, /* DDATA_size */ 1076 sizeof (sin_t), /* ADDR_size */ 1077 0, /* OPT_size - not initialized here */ 1078 TIDUSZ, /* TIDU_size */ 1079 T_COTS_ORD, /* SERV_type */ 1080 TCPS_IDLE, /* CURRENT_state */ 1081 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1082 }; 1083 1084 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1085 T_INFO_ACK, /* PRIM_type */ 1086 0, /* TSDU_size */ 1087 T_INFINITE, /* ETSDU_size */ 1088 T_INVALID, /* CDATA_size */ 1089 T_INVALID, /* DDATA_size */ 1090 sizeof (sin6_t), /* ADDR_size */ 1091 0, /* OPT_size - not initialized here */ 1092 TIDUSZ, /* TIDU_size */ 1093 T_COTS_ORD, /* SERV_type */ 1094 TCPS_IDLE, /* CURRENT_state */ 1095 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1096 }; 1097 1098 #define MS 1L 1099 #define SECONDS (1000 * MS) 1100 #define MINUTES (60 * SECONDS) 1101 #define HOURS (60 * MINUTES) 1102 #define DAYS (24 * HOURS) 1103 1104 #define PARAM_MAX (~(uint32_t)0) 1105 1106 /* Max size IP datagram is 64k - 1 */ 1107 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1108 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1109 /* Max of the above */ 1110 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1111 1112 /* Largest TCP port number */ 1113 #define TCP_MAX_PORT (64 * 1024 - 1) 1114 1115 /* 1116 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1117 * layer header. It has to be a multiple of 4. 1118 */ 1119 static tcpparam_t lcl_tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1120 #define tcps_wroff_xtra tcps_wroff_xtra_param->tcp_param_val 1121 1122 /* 1123 * All of these are alterable, within the min/max values given, at run time. 1124 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1125 * per the TCP spec. 1126 */ 1127 /* BEGIN CSTYLED */ 1128 static tcpparam_t lcl_tcp_param_arr[] = { 1129 /*min max value name */ 1130 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1131 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1132 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1133 { 1, 1024, 1, "tcp_conn_req_min" }, 1134 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1135 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1136 { 0, 10, 0, "tcp_debug" }, 1137 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1138 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1139 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1140 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1141 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1142 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1143 { 1, 255, 64, "tcp_ipv4_ttl"}, 1144 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1145 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1146 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1147 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1148 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1149 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1150 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1151 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1152 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1153 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1154 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1155 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1156 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1157 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1158 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1159 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1160 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1161 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1162 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1163 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1164 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1165 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1166 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1167 /* 1168 * Question: What default value should I set for tcp_strong_iss? 1169 */ 1170 { 0, 2, 1, "tcp_strong_iss"}, 1171 { 0, 65536, 20, "tcp_rtt_updates"}, 1172 { 0, 1, 1, "tcp_wscale_always"}, 1173 { 0, 1, 0, "tcp_tstamp_always"}, 1174 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1175 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1176 { 0, 16, 2, "tcp_deferred_acks_max"}, 1177 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1178 { 1, 4, 4, "tcp_slow_start_initial"}, 1179 { 0, 2, 2, "tcp_sack_permitted"}, 1180 { 0, 1, 1, "tcp_compression_enabled"}, 1181 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1182 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1183 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1184 { 0, 1, 0, "tcp_rev_src_routes"}, 1185 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1186 { 100*MS, 60*SECONDS, 1*SECONDS, "tcp_ndd_get_info_interval"}, 1187 { 0, 16, 8, "tcp_local_dacks_max"}, 1188 { 0, 2, 1, "tcp_ecn_permitted"}, 1189 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1190 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1191 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1192 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1193 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1194 }; 1195 /* END CSTYLED */ 1196 1197 /* 1198 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1199 * each header fragment in the header buffer. Each parameter value has 1200 * to be a multiple of 4 (32-bit aligned). 1201 */ 1202 static tcpparam_t lcl_tcp_mdt_head_param = 1203 { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1204 static tcpparam_t lcl_tcp_mdt_tail_param = 1205 { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1206 #define tcps_mdt_hdr_head_min tcps_mdt_head_param->tcp_param_val 1207 #define tcps_mdt_hdr_tail_min tcps_mdt_tail_param->tcp_param_val 1208 1209 /* 1210 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1211 * the maximum number of payload buffers associated per Multidata. 1212 */ 1213 static tcpparam_t lcl_tcp_mdt_max_pbufs_param = 1214 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1215 #define tcps_mdt_max_pbufs tcps_mdt_max_pbufs_param->tcp_param_val 1216 1217 /* Round up the value to the nearest mss. */ 1218 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1219 1220 /* 1221 * Set ECN capable transport (ECT) code point in IP header. 1222 * 1223 * Note that there are 2 ECT code points '01' and '10', which are called 1224 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1225 * point ECT(0) for TCP as described in RFC 2481. 1226 */ 1227 #define SET_ECT(tcp, iph) \ 1228 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1229 /* We need to clear the code point first. */ \ 1230 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1231 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1232 } else { \ 1233 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1234 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1235 } 1236 1237 /* 1238 * The format argument to pass to tcp_display(). 1239 * DISP_PORT_ONLY means that the returned string has only port info. 1240 * DISP_ADDR_AND_PORT means that the returned string also contains the 1241 * remote and local IP address. 1242 */ 1243 #define DISP_PORT_ONLY 1 1244 #define DISP_ADDR_AND_PORT 2 1245 1246 #define NDD_TOO_QUICK_MSG \ 1247 "ndd get info rate too high for non-privileged users, try again " \ 1248 "later.\n" 1249 #define NDD_OUT_OF_BUF_MSG "<< Out of buffer >>\n" 1250 1251 #define IS_VMLOANED_MBLK(mp) \ 1252 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1253 1254 1255 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1256 boolean_t tcp_mdt_chain = B_TRUE; 1257 1258 /* 1259 * MDT threshold in the form of effective send MSS multiplier; we take 1260 * the MDT path if the amount of unsent data exceeds the threshold value 1261 * (default threshold is 1*SMSS). 1262 */ 1263 uint_t tcp_mdt_smss_threshold = 1; 1264 1265 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1266 1267 /* 1268 * Forces all connections to obey the value of the tcps_maxpsz_multiplier 1269 * tunable settable via NDD. Otherwise, the per-connection behavior is 1270 * determined dynamically during tcp_adapt_ire(), which is the default. 1271 */ 1272 boolean_t tcp_static_maxpsz = B_FALSE; 1273 1274 /* Setable in /etc/system */ 1275 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1276 uint32_t tcp_random_anon_port = 1; 1277 1278 /* 1279 * To reach to an eager in Q0 which can be dropped due to an incoming 1280 * new SYN request when Q0 is full, a new doubly linked list is 1281 * introduced. This list allows to select an eager from Q0 in O(1) time. 1282 * This is needed to avoid spending too much time walking through the 1283 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1284 * this new list has to be a member of Q0. 1285 * This list is headed by listener's tcp_t. When the list is empty, 1286 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1287 * of listener's tcp_t point to listener's tcp_t itself. 1288 * 1289 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1290 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1291 * These macros do not affect the eager's membership to Q0. 1292 */ 1293 1294 1295 #define MAKE_DROPPABLE(listener, eager) \ 1296 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1297 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1298 = (eager); \ 1299 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1300 (eager)->tcp_eager_next_drop_q0 = \ 1301 (listener)->tcp_eager_next_drop_q0; \ 1302 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1303 } 1304 1305 #define MAKE_UNDROPPABLE(eager) \ 1306 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1307 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1308 = (eager)->tcp_eager_prev_drop_q0; \ 1309 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1310 = (eager)->tcp_eager_next_drop_q0; \ 1311 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1312 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1313 } 1314 1315 /* 1316 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1317 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1318 * data, TCP will not respond with an ACK. RFC 793 requires that 1319 * TCP responds with an ACK for such a bogus ACK. By not following 1320 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1321 * an attacker successfully spoofs an acceptable segment to our 1322 * peer; or when our peer is "confused." 1323 */ 1324 uint32_t tcp_drop_ack_unsent_cnt = 10; 1325 1326 /* 1327 * Hook functions to enable cluster networking 1328 * On non-clustered systems these vectors must always be NULL. 1329 */ 1330 1331 void (*cl_inet_listen)(uint8_t protocol, sa_family_t addr_family, 1332 uint8_t *laddrp, in_port_t lport) = NULL; 1333 void (*cl_inet_unlisten)(uint8_t protocol, sa_family_t addr_family, 1334 uint8_t *laddrp, in_port_t lport) = NULL; 1335 void (*cl_inet_connect)(uint8_t protocol, sa_family_t addr_family, 1336 uint8_t *laddrp, in_port_t lport, 1337 uint8_t *faddrp, in_port_t fport) = NULL; 1338 void (*cl_inet_disconnect)(uint8_t protocol, sa_family_t addr_family, 1339 uint8_t *laddrp, in_port_t lport, 1340 uint8_t *faddrp, in_port_t fport) = NULL; 1341 1342 /* 1343 * The following are defined in ip.c 1344 */ 1345 extern int (*cl_inet_isclusterwide)(uint8_t protocol, sa_family_t addr_family, 1346 uint8_t *laddrp); 1347 extern uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 1348 uint8_t *laddrp, uint8_t *faddrp); 1349 1350 #define CL_INET_CONNECT(tcp) { \ 1351 if (cl_inet_connect != NULL) { \ 1352 /* \ 1353 * Running in cluster mode - register active connection \ 1354 * information \ 1355 */ \ 1356 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1357 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1358 (*cl_inet_connect)(IPPROTO_TCP, AF_INET,\ 1359 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1360 (in_port_t)(tcp)->tcp_lport, \ 1361 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1362 (in_port_t)(tcp)->tcp_fport); \ 1363 } \ 1364 } else { \ 1365 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1366 &(tcp)->tcp_ip6h->ip6_src)) {\ 1367 (*cl_inet_connect)(IPPROTO_TCP, AF_INET6,\ 1368 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1369 (in_port_t)(tcp)->tcp_lport, \ 1370 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1371 (in_port_t)(tcp)->tcp_fport); \ 1372 } \ 1373 } \ 1374 } \ 1375 } 1376 1377 #define CL_INET_DISCONNECT(tcp) { \ 1378 if (cl_inet_disconnect != NULL) { \ 1379 /* \ 1380 * Running in cluster mode - deregister active \ 1381 * connection information \ 1382 */ \ 1383 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1384 if ((tcp)->tcp_ip_src != 0) { \ 1385 (*cl_inet_disconnect)(IPPROTO_TCP, \ 1386 AF_INET, \ 1387 (uint8_t *)(&((tcp)->tcp_ip_src)),\ 1388 (in_port_t)(tcp)->tcp_lport, \ 1389 (uint8_t *) \ 1390 (&((tcp)->tcp_ipha->ipha_dst)),\ 1391 (in_port_t)(tcp)->tcp_fport); \ 1392 } \ 1393 } else { \ 1394 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1395 &(tcp)->tcp_ip_src_v6)) { \ 1396 (*cl_inet_disconnect)(IPPROTO_TCP, AF_INET6,\ 1397 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1398 (in_port_t)(tcp)->tcp_lport, \ 1399 (uint8_t *) \ 1400 (&((tcp)->tcp_ip6h->ip6_dst)),\ 1401 (in_port_t)(tcp)->tcp_fport); \ 1402 } \ 1403 } \ 1404 } \ 1405 } 1406 1407 /* 1408 * Cluster networking hook for traversing current connection list. 1409 * This routine is used to extract the current list of live connections 1410 * which must continue to to be dispatched to this node. 1411 */ 1412 int cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg); 1413 1414 static int cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), 1415 void *arg, tcp_stack_t *tcps); 1416 1417 #define DTRACE_IP_FASTPATH(mp, iph, ill, ipha, ip6h) \ 1418 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL, void_ip_t *, \ 1419 iph, __dtrace_ipsr_ill_t *, ill, ipha_t *, ipha, \ 1420 ip6_t *, ip6h, int, 0); 1421 1422 /* 1423 * Figure out the value of window scale opton. Note that the rwnd is 1424 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1425 * We cannot find the scale value and then do a round up of tcp_rwnd 1426 * because the scale value may not be correct after that. 1427 * 1428 * Set the compiler flag to make this function inline. 1429 */ 1430 static void 1431 tcp_set_ws_value(tcp_t *tcp) 1432 { 1433 int i; 1434 uint32_t rwnd = tcp->tcp_rwnd; 1435 1436 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1437 i++, rwnd >>= 1) 1438 ; 1439 tcp->tcp_rcv_ws = i; 1440 } 1441 1442 /* 1443 * Remove a connection from the list of detached TIME_WAIT connections. 1444 * It returns B_FALSE if it can't remove the connection from the list 1445 * as the connection has already been removed from the list due to an 1446 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1447 */ 1448 static boolean_t 1449 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1450 { 1451 boolean_t locked = B_FALSE; 1452 1453 if (tcp_time_wait == NULL) { 1454 tcp_time_wait = *((tcp_squeue_priv_t **) 1455 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1456 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1457 locked = B_TRUE; 1458 } else { 1459 ASSERT(MUTEX_HELD(&tcp_time_wait->tcp_time_wait_lock)); 1460 } 1461 1462 if (tcp->tcp_time_wait_expire == 0) { 1463 ASSERT(tcp->tcp_time_wait_next == NULL); 1464 ASSERT(tcp->tcp_time_wait_prev == NULL); 1465 if (locked) 1466 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1467 return (B_FALSE); 1468 } 1469 ASSERT(TCP_IS_DETACHED(tcp)); 1470 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1471 1472 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1473 ASSERT(tcp->tcp_time_wait_prev == NULL); 1474 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1475 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1476 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1477 NULL; 1478 } else { 1479 tcp_time_wait->tcp_time_wait_tail = NULL; 1480 } 1481 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1482 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1483 ASSERT(tcp->tcp_time_wait_next == NULL); 1484 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1485 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1486 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1487 } else { 1488 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1489 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1490 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1491 tcp->tcp_time_wait_next; 1492 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1493 tcp->tcp_time_wait_prev; 1494 } 1495 tcp->tcp_time_wait_next = NULL; 1496 tcp->tcp_time_wait_prev = NULL; 1497 tcp->tcp_time_wait_expire = 0; 1498 1499 if (locked) 1500 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1501 return (B_TRUE); 1502 } 1503 1504 /* 1505 * Add a connection to the list of detached TIME_WAIT connections 1506 * and set its time to expire. 1507 */ 1508 static void 1509 tcp_time_wait_append(tcp_t *tcp) 1510 { 1511 tcp_stack_t *tcps = tcp->tcp_tcps; 1512 tcp_squeue_priv_t *tcp_time_wait = 1513 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1514 SQPRIVATE_TCP)); 1515 1516 tcp_timers_stop(tcp); 1517 1518 /* Freed above */ 1519 ASSERT(tcp->tcp_timer_tid == 0); 1520 ASSERT(tcp->tcp_ack_tid == 0); 1521 1522 /* must have happened at the time of detaching the tcp */ 1523 ASSERT(tcp->tcp_ptpahn == NULL); 1524 ASSERT(tcp->tcp_flow_stopped == 0); 1525 ASSERT(tcp->tcp_time_wait_next == NULL); 1526 ASSERT(tcp->tcp_time_wait_prev == NULL); 1527 ASSERT(tcp->tcp_time_wait_expire == NULL); 1528 ASSERT(tcp->tcp_listener == NULL); 1529 1530 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1531 /* 1532 * The value computed below in tcp->tcp_time_wait_expire may 1533 * appear negative or wrap around. That is ok since our 1534 * interest is only in the difference between the current lbolt 1535 * value and tcp->tcp_time_wait_expire. But the value should not 1536 * be zero, since it means the tcp is not in the TIME_WAIT list. 1537 * The corresponding comparison in tcp_time_wait_collector() uses 1538 * modular arithmetic. 1539 */ 1540 tcp->tcp_time_wait_expire += 1541 drv_usectohz(tcps->tcps_time_wait_interval * 1000); 1542 if (tcp->tcp_time_wait_expire == 0) 1543 tcp->tcp_time_wait_expire = 1; 1544 1545 ASSERT(TCP_IS_DETACHED(tcp)); 1546 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1547 ASSERT(tcp->tcp_time_wait_next == NULL); 1548 ASSERT(tcp->tcp_time_wait_prev == NULL); 1549 TCP_DBGSTAT(tcps, tcp_time_wait); 1550 1551 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1552 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1553 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1554 tcp_time_wait->tcp_time_wait_head = tcp; 1555 } else { 1556 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1557 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1558 TCPS_TIME_WAIT); 1559 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1560 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1561 } 1562 tcp_time_wait->tcp_time_wait_tail = tcp; 1563 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1564 } 1565 1566 /* ARGSUSED */ 1567 void 1568 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1569 { 1570 conn_t *connp = (conn_t *)arg; 1571 tcp_t *tcp = connp->conn_tcp; 1572 tcp_stack_t *tcps = tcp->tcp_tcps; 1573 1574 ASSERT(tcp != NULL); 1575 if (tcp->tcp_state == TCPS_CLOSED) { 1576 return; 1577 } 1578 1579 ASSERT((tcp->tcp_family == AF_INET && 1580 tcp->tcp_ipversion == IPV4_VERSION) || 1581 (tcp->tcp_family == AF_INET6 && 1582 (tcp->tcp_ipversion == IPV4_VERSION || 1583 tcp->tcp_ipversion == IPV6_VERSION))); 1584 ASSERT(!tcp->tcp_listener); 1585 1586 TCP_STAT(tcps, tcp_time_wait_reap); 1587 ASSERT(TCP_IS_DETACHED(tcp)); 1588 1589 /* 1590 * Because they have no upstream client to rebind or tcp_close() 1591 * them later, we axe the connection here and now. 1592 */ 1593 tcp_close_detached(tcp); 1594 } 1595 1596 /* 1597 * Remove cached/latched IPsec references. 1598 */ 1599 void 1600 tcp_ipsec_cleanup(tcp_t *tcp) 1601 { 1602 conn_t *connp = tcp->tcp_connp; 1603 1604 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1605 1606 if (connp->conn_latch != NULL) { 1607 IPLATCH_REFRELE(connp->conn_latch, 1608 connp->conn_netstack); 1609 connp->conn_latch = NULL; 1610 } 1611 if (connp->conn_policy != NULL) { 1612 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 1613 connp->conn_policy = NULL; 1614 } 1615 } 1616 1617 /* 1618 * Cleaup before placing on free list. 1619 * Disassociate from the netstack/tcp_stack_t since the freelist 1620 * is per squeue and not per netstack. 1621 */ 1622 void 1623 tcp_cleanup(tcp_t *tcp) 1624 { 1625 mblk_t *mp; 1626 char *tcp_iphc; 1627 int tcp_iphc_len; 1628 int tcp_hdr_grown; 1629 tcp_sack_info_t *tcp_sack_info; 1630 conn_t *connp = tcp->tcp_connp; 1631 tcp_stack_t *tcps = tcp->tcp_tcps; 1632 netstack_t *ns = tcps->tcps_netstack; 1633 mblk_t *tcp_rsrv_mp; 1634 1635 tcp_bind_hash_remove(tcp); 1636 1637 /* Cleanup that which needs the netstack first */ 1638 tcp_ipsec_cleanup(tcp); 1639 1640 tcp_free(tcp); 1641 1642 /* Release any SSL context */ 1643 if (tcp->tcp_kssl_ent != NULL) { 1644 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1645 tcp->tcp_kssl_ent = NULL; 1646 } 1647 1648 if (tcp->tcp_kssl_ctx != NULL) { 1649 kssl_release_ctx(tcp->tcp_kssl_ctx); 1650 tcp->tcp_kssl_ctx = NULL; 1651 } 1652 tcp->tcp_kssl_pending = B_FALSE; 1653 1654 conn_delete_ire(connp, NULL); 1655 1656 /* 1657 * Since we will bzero the entire structure, we need to 1658 * remove it and reinsert it in global hash list. We 1659 * know the walkers can't get to this conn because we 1660 * had set CONDEMNED flag earlier and checked reference 1661 * under conn_lock so walker won't pick it and when we 1662 * go the ipcl_globalhash_remove() below, no walker 1663 * can get to it. 1664 */ 1665 ipcl_globalhash_remove(connp); 1666 1667 /* 1668 * Now it is safe to decrement the reference counts. 1669 * This might be the last reference on the netstack and TCPS 1670 * in which case it will cause the tcp_g_q_close and 1671 * the freeing of the IP Instance. 1672 */ 1673 connp->conn_netstack = NULL; 1674 netstack_rele(ns); 1675 ASSERT(tcps != NULL); 1676 tcp->tcp_tcps = NULL; 1677 TCPS_REFRELE(tcps); 1678 1679 /* Save some state */ 1680 mp = tcp->tcp_timercache; 1681 1682 tcp_sack_info = tcp->tcp_sack_info; 1683 tcp_iphc = tcp->tcp_iphc; 1684 tcp_iphc_len = tcp->tcp_iphc_len; 1685 tcp_hdr_grown = tcp->tcp_hdr_grown; 1686 tcp_rsrv_mp = tcp->tcp_rsrv_mp; 1687 1688 if (connp->conn_cred != NULL) { 1689 crfree(connp->conn_cred); 1690 connp->conn_cred = NULL; 1691 } 1692 if (connp->conn_peercred != NULL) { 1693 crfree(connp->conn_peercred); 1694 connp->conn_peercred = NULL; 1695 } 1696 ipcl_conn_cleanup(connp); 1697 connp->conn_flags = IPCL_TCPCONN; 1698 bzero(tcp, sizeof (tcp_t)); 1699 1700 /* restore the state */ 1701 tcp->tcp_timercache = mp; 1702 1703 tcp->tcp_sack_info = tcp_sack_info; 1704 tcp->tcp_iphc = tcp_iphc; 1705 tcp->tcp_iphc_len = tcp_iphc_len; 1706 tcp->tcp_hdr_grown = tcp_hdr_grown; 1707 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 1708 1709 tcp->tcp_connp = connp; 1710 1711 ASSERT(connp->conn_tcp == tcp); 1712 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1713 connp->conn_state_flags = CONN_INCIPIENT; 1714 ASSERT(connp->conn_ulp == IPPROTO_TCP); 1715 ASSERT(connp->conn_ref == 1); 1716 } 1717 1718 /* 1719 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1720 * is done forwards from the head. 1721 * This walks all stack instances since 1722 * tcp_time_wait remains global across all stacks. 1723 */ 1724 /* ARGSUSED */ 1725 void 1726 tcp_time_wait_collector(void *arg) 1727 { 1728 tcp_t *tcp; 1729 clock_t now; 1730 mblk_t *mp; 1731 conn_t *connp; 1732 kmutex_t *lock; 1733 boolean_t removed; 1734 1735 squeue_t *sqp = (squeue_t *)arg; 1736 tcp_squeue_priv_t *tcp_time_wait = 1737 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1738 1739 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1740 tcp_time_wait->tcp_time_wait_tid = 0; 1741 1742 if (tcp_time_wait->tcp_free_list != NULL && 1743 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1744 TCP_G_STAT(tcp_freelist_cleanup); 1745 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1746 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1747 tcp->tcp_time_wait_next = NULL; 1748 tcp_time_wait->tcp_free_list_cnt--; 1749 ASSERT(tcp->tcp_tcps == NULL); 1750 CONN_DEC_REF(tcp->tcp_connp); 1751 } 1752 ASSERT(tcp_time_wait->tcp_free_list_cnt == 0); 1753 } 1754 1755 /* 1756 * In order to reap time waits reliably, we should use a 1757 * source of time that is not adjustable by the user -- hence 1758 * the call to ddi_get_lbolt(). 1759 */ 1760 now = ddi_get_lbolt(); 1761 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1762 /* 1763 * Compare times using modular arithmetic, since 1764 * lbolt can wrapover. 1765 */ 1766 if ((now - tcp->tcp_time_wait_expire) < 0) { 1767 break; 1768 } 1769 1770 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1771 ASSERT(removed); 1772 1773 connp = tcp->tcp_connp; 1774 ASSERT(connp->conn_fanout != NULL); 1775 lock = &connp->conn_fanout->connf_lock; 1776 /* 1777 * This is essentially a TW reclaim fast path optimization for 1778 * performance where the timewait collector checks under the 1779 * fanout lock (so that no one else can get access to the 1780 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1781 * the classifier hash list. If ref count is indeed 2, we can 1782 * just remove the conn under the fanout lock and avoid 1783 * cleaning up the conn under the squeue, provided that 1784 * clustering callbacks are not enabled. If clustering is 1785 * enabled, we need to make the clustering callback before 1786 * setting the CONDEMNED flag and after dropping all locks and 1787 * so we forego this optimization and fall back to the slow 1788 * path. Also please see the comments in tcp_closei_local 1789 * regarding the refcnt logic. 1790 * 1791 * Since we are holding the tcp_time_wait_lock, its better 1792 * not to block on the fanout_lock because other connections 1793 * can't add themselves to time_wait list. So we do a 1794 * tryenter instead of mutex_enter. 1795 */ 1796 if (mutex_tryenter(lock)) { 1797 mutex_enter(&connp->conn_lock); 1798 if ((connp->conn_ref == 2) && 1799 (cl_inet_disconnect == NULL)) { 1800 ipcl_hash_remove_locked(connp, 1801 connp->conn_fanout); 1802 /* 1803 * Set the CONDEMNED flag now itself so that 1804 * the refcnt cannot increase due to any 1805 * walker. But we have still not cleaned up 1806 * conn_ire_cache. This is still ok since 1807 * we are going to clean it up in tcp_cleanup 1808 * immediately and any interface unplumb 1809 * thread will wait till the ire is blown away 1810 */ 1811 connp->conn_state_flags |= CONN_CONDEMNED; 1812 mutex_exit(lock); 1813 mutex_exit(&connp->conn_lock); 1814 if (tcp_time_wait->tcp_free_list_cnt < 1815 tcp_free_list_max_cnt) { 1816 /* Add to head of tcp_free_list */ 1817 mutex_exit( 1818 &tcp_time_wait->tcp_time_wait_lock); 1819 tcp_cleanup(tcp); 1820 ASSERT(connp->conn_latch == NULL); 1821 ASSERT(connp->conn_policy == NULL); 1822 ASSERT(tcp->tcp_tcps == NULL); 1823 ASSERT(connp->conn_netstack == NULL); 1824 1825 mutex_enter( 1826 &tcp_time_wait->tcp_time_wait_lock); 1827 tcp->tcp_time_wait_next = 1828 tcp_time_wait->tcp_free_list; 1829 tcp_time_wait->tcp_free_list = tcp; 1830 tcp_time_wait->tcp_free_list_cnt++; 1831 continue; 1832 } else { 1833 /* Do not add to tcp_free_list */ 1834 mutex_exit( 1835 &tcp_time_wait->tcp_time_wait_lock); 1836 tcp_bind_hash_remove(tcp); 1837 conn_delete_ire(tcp->tcp_connp, NULL); 1838 tcp_ipsec_cleanup(tcp); 1839 CONN_DEC_REF(tcp->tcp_connp); 1840 } 1841 } else { 1842 CONN_INC_REF_LOCKED(connp); 1843 mutex_exit(lock); 1844 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1845 mutex_exit(&connp->conn_lock); 1846 /* 1847 * We can reuse the closemp here since conn has 1848 * detached (otherwise we wouldn't even be in 1849 * time_wait list). tcp_closemp_used can safely 1850 * be changed without taking a lock as no other 1851 * thread can concurrently access it at this 1852 * point in the connection lifecycle. 1853 */ 1854 1855 if (tcp->tcp_closemp.b_prev == NULL) 1856 tcp->tcp_closemp_used = B_TRUE; 1857 else 1858 cmn_err(CE_PANIC, 1859 "tcp_timewait_collector: " 1860 "concurrent use of tcp_closemp: " 1861 "connp %p tcp %p\n", (void *)connp, 1862 (void *)tcp); 1863 1864 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1865 mp = &tcp->tcp_closemp; 1866 squeue_fill(connp->conn_sqp, mp, 1867 tcp_timewait_output, connp, 1868 SQTAG_TCP_TIMEWAIT); 1869 } 1870 } else { 1871 mutex_enter(&connp->conn_lock); 1872 CONN_INC_REF_LOCKED(connp); 1873 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1874 mutex_exit(&connp->conn_lock); 1875 /* 1876 * We can reuse the closemp here since conn has 1877 * detached (otherwise we wouldn't even be in 1878 * time_wait list). tcp_closemp_used can safely 1879 * be changed without taking a lock as no other 1880 * thread can concurrently access it at this 1881 * point in the connection lifecycle. 1882 */ 1883 1884 if (tcp->tcp_closemp.b_prev == NULL) 1885 tcp->tcp_closemp_used = B_TRUE; 1886 else 1887 cmn_err(CE_PANIC, "tcp_timewait_collector: " 1888 "concurrent use of tcp_closemp: " 1889 "connp %p tcp %p\n", (void *)connp, 1890 (void *)tcp); 1891 1892 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1893 mp = &tcp->tcp_closemp; 1894 squeue_fill(connp->conn_sqp, mp, 1895 tcp_timewait_output, connp, 0); 1896 } 1897 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1898 } 1899 1900 if (tcp_time_wait->tcp_free_list != NULL) 1901 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1902 1903 tcp_time_wait->tcp_time_wait_tid = 1904 timeout(tcp_time_wait_collector, sqp, TCP_TIME_WAIT_DELAY); 1905 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1906 } 1907 /* 1908 * Reply to a clients T_CONN_RES TPI message. This function 1909 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1910 * on the acceptor STREAM and processed in tcp_wput_accept(). 1911 * Read the block comment on top of tcp_conn_request(). 1912 */ 1913 static void 1914 tcp_accept(tcp_t *listener, mblk_t *mp) 1915 { 1916 tcp_t *acceptor; 1917 tcp_t *eager; 1918 tcp_t *tcp; 1919 struct T_conn_res *tcr; 1920 t_uscalar_t acceptor_id; 1921 t_scalar_t seqnum; 1922 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1923 mblk_t *ok_mp; 1924 mblk_t *mp1; 1925 tcp_stack_t *tcps = listener->tcp_tcps; 1926 1927 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1928 tcp_err_ack(listener, mp, TPROTO, 0); 1929 return; 1930 } 1931 tcr = (struct T_conn_res *)mp->b_rptr; 1932 1933 /* 1934 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1935 * read side queue of the streams device underneath us i.e. the 1936 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1937 * look it up in the queue_hash. Under LP64 it sends down the 1938 * minor_t of the accepting endpoint. 1939 * 1940 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1941 * fanout hash lock is held. 1942 * This prevents any thread from entering the acceptor queue from 1943 * below (since it has not been hard bound yet i.e. any inbound 1944 * packets will arrive on the listener or default tcp queue and 1945 * go through tcp_lookup). 1946 * The CONN_INC_REF will prevent the acceptor from closing. 1947 * 1948 * XXX It is still possible for a tli application to send down data 1949 * on the accepting stream while another thread calls t_accept. 1950 * This should not be a problem for well-behaved applications since 1951 * the T_OK_ACK is sent after the queue swapping is completed. 1952 * 1953 * If the accepting fd is the same as the listening fd, avoid 1954 * queue hash lookup since that will return an eager listener in a 1955 * already established state. 1956 */ 1957 acceptor_id = tcr->ACCEPTOR_id; 1958 mutex_enter(&listener->tcp_eager_lock); 1959 if (listener->tcp_acceptor_id == acceptor_id) { 1960 eager = listener->tcp_eager_next_q; 1961 /* only count how many T_CONN_INDs so don't count q0 */ 1962 if ((listener->tcp_conn_req_cnt_q != 1) || 1963 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 1964 mutex_exit(&listener->tcp_eager_lock); 1965 tcp_err_ack(listener, mp, TBADF, 0); 1966 return; 1967 } 1968 if (listener->tcp_conn_req_cnt_q0 != 0) { 1969 /* Throw away all the eagers on q0. */ 1970 tcp_eager_cleanup(listener, 1); 1971 } 1972 if (listener->tcp_syn_defense) { 1973 listener->tcp_syn_defense = B_FALSE; 1974 if (listener->tcp_ip_addr_cache != NULL) { 1975 kmem_free(listener->tcp_ip_addr_cache, 1976 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 1977 listener->tcp_ip_addr_cache = NULL; 1978 } 1979 } 1980 /* 1981 * Transfer tcp_conn_req_max to the eager so that when 1982 * a disconnect occurs we can revert the endpoint to the 1983 * listen state. 1984 */ 1985 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 1986 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 1987 /* 1988 * Get a reference on the acceptor just like the 1989 * tcp_acceptor_hash_lookup below. 1990 */ 1991 acceptor = listener; 1992 CONN_INC_REF(acceptor->tcp_connp); 1993 } else { 1994 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 1995 if (acceptor == NULL) { 1996 if (listener->tcp_debug) { 1997 (void) strlog(TCP_MOD_ID, 0, 1, 1998 SL_ERROR|SL_TRACE, 1999 "tcp_accept: did not find acceptor 0x%x\n", 2000 acceptor_id); 2001 } 2002 mutex_exit(&listener->tcp_eager_lock); 2003 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 2004 return; 2005 } 2006 /* 2007 * Verify acceptor state. The acceptable states for an acceptor 2008 * include TCPS_IDLE and TCPS_BOUND. 2009 */ 2010 switch (acceptor->tcp_state) { 2011 case TCPS_IDLE: 2012 /* FALLTHRU */ 2013 case TCPS_BOUND: 2014 break; 2015 default: 2016 CONN_DEC_REF(acceptor->tcp_connp); 2017 mutex_exit(&listener->tcp_eager_lock); 2018 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2019 return; 2020 } 2021 } 2022 2023 /* The listener must be in TCPS_LISTEN */ 2024 if (listener->tcp_state != TCPS_LISTEN) { 2025 CONN_DEC_REF(acceptor->tcp_connp); 2026 mutex_exit(&listener->tcp_eager_lock); 2027 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2028 return; 2029 } 2030 2031 /* 2032 * Rendezvous with an eager connection request packet hanging off 2033 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2034 * tcp structure when the connection packet arrived in 2035 * tcp_conn_request(). 2036 */ 2037 seqnum = tcr->SEQ_number; 2038 eager = listener; 2039 do { 2040 eager = eager->tcp_eager_next_q; 2041 if (eager == NULL) { 2042 CONN_DEC_REF(acceptor->tcp_connp); 2043 mutex_exit(&listener->tcp_eager_lock); 2044 tcp_err_ack(listener, mp, TBADSEQ, 0); 2045 return; 2046 } 2047 } while (eager->tcp_conn_req_seqnum != seqnum); 2048 mutex_exit(&listener->tcp_eager_lock); 2049 2050 /* 2051 * At this point, both acceptor and listener have 2 ref 2052 * that they begin with. Acceptor has one additional ref 2053 * we placed in lookup while listener has 3 additional 2054 * ref for being behind the squeue (tcp_accept() is 2055 * done on listener's squeue); being in classifier hash; 2056 * and eager's ref on listener. 2057 */ 2058 ASSERT(listener->tcp_connp->conn_ref >= 5); 2059 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2060 2061 /* 2062 * The eager at this point is set in its own squeue and 2063 * could easily have been killed (tcp_accept_finish will 2064 * deal with that) because of a TH_RST so we can only 2065 * ASSERT for a single ref. 2066 */ 2067 ASSERT(eager->tcp_connp->conn_ref >= 1); 2068 2069 /* Pre allocate the stroptions mblk also */ 2070 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 2071 if (opt_mp == NULL) { 2072 CONN_DEC_REF(acceptor->tcp_connp); 2073 CONN_DEC_REF(eager->tcp_connp); 2074 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2075 return; 2076 } 2077 DB_TYPE(opt_mp) = M_SETOPTS; 2078 opt_mp->b_wptr += sizeof (struct stroptions); 2079 2080 /* 2081 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2082 * from listener to acceptor. The message is chained on opt_mp 2083 * which will be sent onto eager's squeue. 2084 */ 2085 if (listener->tcp_bound_if != 0) { 2086 /* allocate optmgmt req */ 2087 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2088 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 2089 sizeof (int)); 2090 if (mp1 != NULL) 2091 linkb(opt_mp, mp1); 2092 } 2093 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2094 uint_t on = 1; 2095 2096 /* allocate optmgmt req */ 2097 mp1 = tcp_setsockopt_mp(IPPROTO_IPV6, 2098 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 2099 if (mp1 != NULL) 2100 linkb(opt_mp, mp1); 2101 } 2102 2103 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2104 if ((mp1 = copymsg(mp)) == NULL) { 2105 CONN_DEC_REF(acceptor->tcp_connp); 2106 CONN_DEC_REF(eager->tcp_connp); 2107 freemsg(opt_mp); 2108 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2109 return; 2110 } 2111 2112 tcr = (struct T_conn_res *)mp1->b_rptr; 2113 2114 /* 2115 * This is an expanded version of mi_tpi_ok_ack_alloc() 2116 * which allocates a larger mblk and appends the new 2117 * local address to the ok_ack. The address is copied by 2118 * soaccept() for getsockname(). 2119 */ 2120 { 2121 int extra; 2122 2123 extra = (eager->tcp_family == AF_INET) ? 2124 sizeof (sin_t) : sizeof (sin6_t); 2125 2126 /* 2127 * Try to re-use mp, if possible. Otherwise, allocate 2128 * an mblk and return it as ok_mp. In any case, mp 2129 * is no longer usable upon return. 2130 */ 2131 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2132 CONN_DEC_REF(acceptor->tcp_connp); 2133 CONN_DEC_REF(eager->tcp_connp); 2134 freemsg(opt_mp); 2135 /* Original mp has been freed by now, so use mp1 */ 2136 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2137 return; 2138 } 2139 2140 mp = NULL; /* We should never use mp after this point */ 2141 2142 switch (extra) { 2143 case sizeof (sin_t): { 2144 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2145 2146 ok_mp->b_wptr += extra; 2147 sin->sin_family = AF_INET; 2148 sin->sin_port = eager->tcp_lport; 2149 sin->sin_addr.s_addr = 2150 eager->tcp_ipha->ipha_src; 2151 break; 2152 } 2153 case sizeof (sin6_t): { 2154 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2155 2156 ok_mp->b_wptr += extra; 2157 sin6->sin6_family = AF_INET6; 2158 sin6->sin6_port = eager->tcp_lport; 2159 if (eager->tcp_ipversion == IPV4_VERSION) { 2160 sin6->sin6_flowinfo = 0; 2161 IN6_IPADDR_TO_V4MAPPED( 2162 eager->tcp_ipha->ipha_src, 2163 &sin6->sin6_addr); 2164 } else { 2165 ASSERT(eager->tcp_ip6h != NULL); 2166 sin6->sin6_flowinfo = 2167 eager->tcp_ip6h->ip6_vcf & 2168 ~IPV6_VERS_AND_FLOW_MASK; 2169 sin6->sin6_addr = 2170 eager->tcp_ip6h->ip6_src; 2171 } 2172 sin6->sin6_scope_id = 0; 2173 sin6->__sin6_src_id = 0; 2174 break; 2175 } 2176 default: 2177 break; 2178 } 2179 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2180 } 2181 2182 /* 2183 * If there are no options we know that the T_CONN_RES will 2184 * succeed. However, we can't send the T_OK_ACK upstream until 2185 * the tcp_accept_swap is done since it would be dangerous to 2186 * let the application start using the new fd prior to the swap. 2187 */ 2188 tcp_accept_swap(listener, acceptor, eager); 2189 2190 /* 2191 * tcp_accept_swap unlinks eager from listener but does not drop 2192 * the eager's reference on the listener. 2193 */ 2194 ASSERT(eager->tcp_listener == NULL); 2195 ASSERT(listener->tcp_connp->conn_ref >= 5); 2196 2197 /* 2198 * The eager is now associated with its own queue. Insert in 2199 * the hash so that the connection can be reused for a future 2200 * T_CONN_RES. 2201 */ 2202 tcp_acceptor_hash_insert(acceptor_id, eager); 2203 2204 /* 2205 * We now do the processing of options with T_CONN_RES. 2206 * We delay till now since we wanted to have queue to pass to 2207 * option processing routines that points back to the right 2208 * instance structure which does not happen until after 2209 * tcp_accept_swap(). 2210 * 2211 * Note: 2212 * The sanity of the logic here assumes that whatever options 2213 * are appropriate to inherit from listner=>eager are done 2214 * before this point, and whatever were to be overridden (or not) 2215 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2216 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2217 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2218 * This may not be true at this point in time but can be fixed 2219 * independently. This option processing code starts with 2220 * the instantiated acceptor instance and the final queue at 2221 * this point. 2222 */ 2223 2224 if (tcr->OPT_length != 0) { 2225 /* Options to process */ 2226 int t_error = 0; 2227 int sys_error = 0; 2228 int do_disconnect = 0; 2229 2230 if (tcp_conprim_opt_process(eager, mp1, 2231 &do_disconnect, &t_error, &sys_error) < 0) { 2232 eager->tcp_accept_error = 1; 2233 if (do_disconnect) { 2234 /* 2235 * An option failed which does not allow 2236 * connection to be accepted. 2237 * 2238 * We allow T_CONN_RES to succeed and 2239 * put a T_DISCON_IND on the eager queue. 2240 */ 2241 ASSERT(t_error == 0 && sys_error == 0); 2242 eager->tcp_send_discon_ind = 1; 2243 } else { 2244 ASSERT(t_error != 0); 2245 freemsg(ok_mp); 2246 /* 2247 * Original mp was either freed or set 2248 * to ok_mp above, so use mp1 instead. 2249 */ 2250 tcp_err_ack(listener, mp1, t_error, sys_error); 2251 goto finish; 2252 } 2253 } 2254 /* 2255 * Most likely success in setting options (except if 2256 * eager->tcp_send_discon_ind set). 2257 * mp1 option buffer represented by OPT_length/offset 2258 * potentially modified and contains results of setting 2259 * options at this point 2260 */ 2261 } 2262 2263 /* We no longer need mp1, since all options processing has passed */ 2264 freemsg(mp1); 2265 2266 putnext(listener->tcp_rq, ok_mp); 2267 2268 mutex_enter(&listener->tcp_eager_lock); 2269 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2270 tcp_t *tail; 2271 mblk_t *conn_ind; 2272 2273 /* 2274 * This path should not be executed if listener and 2275 * acceptor streams are the same. 2276 */ 2277 ASSERT(listener != acceptor); 2278 2279 tcp = listener->tcp_eager_prev_q0; 2280 /* 2281 * listener->tcp_eager_prev_q0 points to the TAIL of the 2282 * deferred T_conn_ind queue. We need to get to the head of 2283 * the queue in order to send up T_conn_ind the same order as 2284 * how the 3WHS is completed. 2285 */ 2286 while (tcp != listener) { 2287 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2288 break; 2289 else 2290 tcp = tcp->tcp_eager_prev_q0; 2291 } 2292 ASSERT(tcp != listener); 2293 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2294 ASSERT(conn_ind != NULL); 2295 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2296 2297 /* Move from q0 to q */ 2298 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2299 listener->tcp_conn_req_cnt_q0--; 2300 listener->tcp_conn_req_cnt_q++; 2301 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2302 tcp->tcp_eager_prev_q0; 2303 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2304 tcp->tcp_eager_next_q0; 2305 tcp->tcp_eager_prev_q0 = NULL; 2306 tcp->tcp_eager_next_q0 = NULL; 2307 tcp->tcp_conn_def_q0 = B_FALSE; 2308 2309 /* Make sure the tcp isn't in the list of droppables */ 2310 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2311 tcp->tcp_eager_prev_drop_q0 == NULL); 2312 2313 /* 2314 * Insert at end of the queue because sockfs sends 2315 * down T_CONN_RES in chronological order. Leaving 2316 * the older conn indications at front of the queue 2317 * helps reducing search time. 2318 */ 2319 tail = listener->tcp_eager_last_q; 2320 if (tail != NULL) 2321 tail->tcp_eager_next_q = tcp; 2322 else 2323 listener->tcp_eager_next_q = tcp; 2324 listener->tcp_eager_last_q = tcp; 2325 tcp->tcp_eager_next_q = NULL; 2326 mutex_exit(&listener->tcp_eager_lock); 2327 putnext(tcp->tcp_rq, conn_ind); 2328 } else { 2329 mutex_exit(&listener->tcp_eager_lock); 2330 } 2331 2332 /* 2333 * Done with the acceptor - free it 2334 * 2335 * Note: from this point on, no access to listener should be made 2336 * as listener can be equal to acceptor. 2337 */ 2338 finish: 2339 ASSERT(acceptor->tcp_detached); 2340 ASSERT(tcps->tcps_g_q != NULL); 2341 acceptor->tcp_rq = tcps->tcps_g_q; 2342 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2343 (void) tcp_clean_death(acceptor, 0, 2); 2344 CONN_DEC_REF(acceptor->tcp_connp); 2345 2346 /* 2347 * In case we already received a FIN we have to make tcp_rput send 2348 * the ordrel_ind. This will also send up a window update if the window 2349 * has opened up. 2350 * 2351 * In the normal case of a successful connection acceptance 2352 * we give the O_T_BIND_REQ to the read side put procedure as an 2353 * indication that this was just accepted. This tells tcp_rput to 2354 * pass up any data queued in tcp_rcv_list. 2355 * 2356 * In the fringe case where options sent with T_CONN_RES failed and 2357 * we required, we would be indicating a T_DISCON_IND to blow 2358 * away this connection. 2359 */ 2360 2361 /* 2362 * XXX: we currently have a problem if XTI application closes the 2363 * acceptor stream in between. This problem exists in on10-gate also 2364 * and is well know but nothing can be done short of major rewrite 2365 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2366 * eager same squeue as listener (we can distinguish non socket 2367 * listeners at the time of handling a SYN in tcp_conn_request) 2368 * and do most of the work that tcp_accept_finish does here itself 2369 * and then get behind the acceptor squeue to access the acceptor 2370 * queue. 2371 */ 2372 /* 2373 * We already have a ref on tcp so no need to do one before squeue_fill 2374 */ 2375 squeue_fill(eager->tcp_connp->conn_sqp, opt_mp, 2376 tcp_accept_finish, eager->tcp_connp, SQTAG_TCP_ACCEPT_FINISH); 2377 } 2378 2379 /* 2380 * Swap information between the eager and acceptor for a TLI/XTI client. 2381 * The sockfs accept is done on the acceptor stream and control goes 2382 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2383 * called. In either case, both the eager and listener are in their own 2384 * perimeter (squeue) and the code has to deal with potential race. 2385 * 2386 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2387 */ 2388 static void 2389 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2390 { 2391 conn_t *econnp, *aconnp; 2392 2393 ASSERT(eager->tcp_rq == listener->tcp_rq); 2394 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2395 ASSERT(!eager->tcp_hard_bound); 2396 ASSERT(!TCP_IS_SOCKET(acceptor)); 2397 ASSERT(!TCP_IS_SOCKET(eager)); 2398 ASSERT(!TCP_IS_SOCKET(listener)); 2399 2400 acceptor->tcp_detached = B_TRUE; 2401 /* 2402 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2403 * the acceptor id. 2404 */ 2405 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2406 2407 /* remove eager from listen list... */ 2408 mutex_enter(&listener->tcp_eager_lock); 2409 tcp_eager_unlink(eager); 2410 ASSERT(eager->tcp_eager_next_q == NULL && 2411 eager->tcp_eager_last_q == NULL); 2412 ASSERT(eager->tcp_eager_next_q0 == NULL && 2413 eager->tcp_eager_prev_q0 == NULL); 2414 mutex_exit(&listener->tcp_eager_lock); 2415 eager->tcp_rq = acceptor->tcp_rq; 2416 eager->tcp_wq = acceptor->tcp_wq; 2417 2418 econnp = eager->tcp_connp; 2419 aconnp = acceptor->tcp_connp; 2420 2421 eager->tcp_rq->q_ptr = econnp; 2422 eager->tcp_wq->q_ptr = econnp; 2423 2424 /* 2425 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2426 * which might be a different squeue from our peer TCP instance. 2427 * For TCP Fusion, the peer expects that whenever tcp_detached is 2428 * clear, our TCP queues point to the acceptor's queues. Thus, use 2429 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2430 * above reach global visibility prior to the clearing of tcp_detached. 2431 */ 2432 membar_producer(); 2433 eager->tcp_detached = B_FALSE; 2434 2435 ASSERT(eager->tcp_ack_tid == 0); 2436 2437 econnp->conn_dev = aconnp->conn_dev; 2438 econnp->conn_minor_arena = aconnp->conn_minor_arena; 2439 ASSERT(econnp->conn_minor_arena != NULL); 2440 if (eager->tcp_cred != NULL) 2441 crfree(eager->tcp_cred); 2442 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2443 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2444 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2445 2446 aconnp->conn_cred = NULL; 2447 2448 econnp->conn_zoneid = aconnp->conn_zoneid; 2449 econnp->conn_allzones = aconnp->conn_allzones; 2450 2451 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2452 aconnp->conn_mac_exempt = B_FALSE; 2453 2454 ASSERT(aconnp->conn_peercred == NULL); 2455 2456 /* Do the IPC initialization */ 2457 CONN_INC_REF(econnp); 2458 2459 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2460 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2461 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2462 2463 /* Done with old IPC. Drop its ref on its connp */ 2464 CONN_DEC_REF(aconnp); 2465 } 2466 2467 2468 /* 2469 * Adapt to the information, such as rtt and rtt_sd, provided from the 2470 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2471 * 2472 * Checks for multicast and broadcast destination address. 2473 * Returns zero on failure; non-zero if ok. 2474 * 2475 * Note that the MSS calculation here is based on the info given in 2476 * the IRE. We do not do any calculation based on TCP options. They 2477 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2478 * knows which options to use. 2479 * 2480 * Note on how TCP gets its parameters for a connection. 2481 * 2482 * When a tcp_t structure is allocated, it gets all the default parameters. 2483 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2484 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2485 * default. 2486 * 2487 * An incoming SYN with a multicast or broadcast destination address, is dropped 2488 * in 1 of 2 places. 2489 * 2490 * 1. If the packet was received over the wire it is dropped in 2491 * ip_rput_process_broadcast() 2492 * 2493 * 2. If the packet was received through internal IP loopback, i.e. the packet 2494 * was generated and received on the same machine, it is dropped in 2495 * ip_wput_local() 2496 * 2497 * An incoming SYN with a multicast or broadcast source address is always 2498 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2499 * reject an attempt to connect to a broadcast or multicast (destination) 2500 * address. 2501 */ 2502 static int 2503 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2504 { 2505 tcp_hsp_t *hsp; 2506 ire_t *ire; 2507 ire_t *sire = NULL; 2508 iulp_t *ire_uinfo = NULL; 2509 uint32_t mss_max; 2510 uint32_t mss; 2511 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2512 conn_t *connp = tcp->tcp_connp; 2513 boolean_t ire_cacheable = B_FALSE; 2514 zoneid_t zoneid = connp->conn_zoneid; 2515 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2516 MATCH_IRE_SECATTR; 2517 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2518 ill_t *ill = NULL; 2519 boolean_t incoming = (ire_mp == NULL); 2520 tcp_stack_t *tcps = tcp->tcp_tcps; 2521 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2522 2523 ASSERT(connp->conn_ire_cache == NULL); 2524 2525 if (tcp->tcp_ipversion == IPV4_VERSION) { 2526 2527 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2528 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2529 return (0); 2530 } 2531 /* 2532 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2533 * for the destination with the nexthop as gateway. 2534 * ire_ctable_lookup() is used because this particular 2535 * ire, if it exists, will be marked private. 2536 * If that is not available, use the interface ire 2537 * for the nexthop. 2538 * 2539 * TSol: tcp_update_label will detect label mismatches based 2540 * only on the destination's label, but that would not 2541 * detect label mismatches based on the security attributes 2542 * of routes or next hop gateway. Hence we need to pass the 2543 * label to ire_ftable_lookup below in order to locate the 2544 * right prefix (and/or) ire cache. Similarly we also need 2545 * pass the label to the ire_cache_lookup below to locate 2546 * the right ire that also matches on the label. 2547 */ 2548 if (tcp->tcp_connp->conn_nexthop_set) { 2549 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2550 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2551 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2552 ipst); 2553 if (ire == NULL) { 2554 ire = ire_ftable_lookup( 2555 tcp->tcp_connp->conn_nexthop_v4, 2556 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2557 tsl, match_flags, ipst); 2558 if (ire == NULL) 2559 return (0); 2560 } else { 2561 ire_uinfo = &ire->ire_uinfo; 2562 } 2563 } else { 2564 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2565 zoneid, tsl, ipst); 2566 if (ire != NULL) { 2567 ire_cacheable = B_TRUE; 2568 ire_uinfo = (ire_mp != NULL) ? 2569 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2570 &ire->ire_uinfo; 2571 2572 } else { 2573 if (ire_mp == NULL) { 2574 ire = ire_ftable_lookup( 2575 tcp->tcp_connp->conn_rem, 2576 0, 0, 0, NULL, &sire, zoneid, 0, 2577 tsl, (MATCH_IRE_RECURSIVE | 2578 MATCH_IRE_DEFAULT), ipst); 2579 if (ire == NULL) 2580 return (0); 2581 ire_uinfo = (sire != NULL) ? 2582 &sire->ire_uinfo : 2583 &ire->ire_uinfo; 2584 } else { 2585 ire = (ire_t *)ire_mp->b_rptr; 2586 ire_uinfo = 2587 &((ire_t *) 2588 ire_mp->b_rptr)->ire_uinfo; 2589 } 2590 } 2591 } 2592 ASSERT(ire != NULL); 2593 2594 if ((ire->ire_src_addr == INADDR_ANY) || 2595 (ire->ire_type & IRE_BROADCAST)) { 2596 /* 2597 * ire->ire_mp is non null when ire_mp passed in is used 2598 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2599 */ 2600 if (ire->ire_mp == NULL) 2601 ire_refrele(ire); 2602 if (sire != NULL) 2603 ire_refrele(sire); 2604 return (0); 2605 } 2606 2607 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2608 ipaddr_t src_addr; 2609 2610 /* 2611 * ip_bind_connected() has stored the correct source 2612 * address in conn_src. 2613 */ 2614 src_addr = tcp->tcp_connp->conn_src; 2615 tcp->tcp_ipha->ipha_src = src_addr; 2616 /* 2617 * Copy of the src addr. in tcp_t is needed 2618 * for the lookup funcs. 2619 */ 2620 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2621 } 2622 /* 2623 * Set the fragment bit so that IP will tell us if the MTU 2624 * should change. IP tells us the latest setting of 2625 * ip_path_mtu_discovery through ire_frag_flag. 2626 */ 2627 if (ipst->ips_ip_path_mtu_discovery) { 2628 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2629 htons(IPH_DF); 2630 } 2631 /* 2632 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2633 * for IP_NEXTHOP. No cache ire has been found for the 2634 * destination and we are working with the nexthop's 2635 * interface ire. Since we need to forward all packets 2636 * to the nexthop first, we "blindly" set tcp_localnet 2637 * to false, eventhough the destination may also be 2638 * onlink. 2639 */ 2640 if (ire_uinfo == NULL) 2641 tcp->tcp_localnet = 0; 2642 else 2643 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2644 } else { 2645 /* 2646 * For incoming connection ire_mp = NULL 2647 * For outgoing connection ire_mp != NULL 2648 * Technically we should check conn_incoming_ill 2649 * when ire_mp is NULL and conn_outgoing_ill when 2650 * ire_mp is non-NULL. But this is performance 2651 * critical path and for IPV*_BOUND_IF, outgoing 2652 * and incoming ill are always set to the same value. 2653 */ 2654 ill_t *dst_ill = NULL; 2655 ipif_t *dst_ipif = NULL; 2656 2657 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2658 2659 if (connp->conn_outgoing_ill != NULL) { 2660 /* Outgoing or incoming path */ 2661 int err; 2662 2663 dst_ill = conn_get_held_ill(connp, 2664 &connp->conn_outgoing_ill, &err); 2665 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2666 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2667 return (0); 2668 } 2669 match_flags |= MATCH_IRE_ILL; 2670 dst_ipif = dst_ill->ill_ipif; 2671 } 2672 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2673 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2674 2675 if (ire != NULL) { 2676 ire_cacheable = B_TRUE; 2677 ire_uinfo = (ire_mp != NULL) ? 2678 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2679 &ire->ire_uinfo; 2680 } else { 2681 if (ire_mp == NULL) { 2682 ire = ire_ftable_lookup_v6( 2683 &tcp->tcp_connp->conn_remv6, 2684 0, 0, 0, dst_ipif, &sire, zoneid, 2685 0, tsl, match_flags, ipst); 2686 if (ire == NULL) { 2687 if (dst_ill != NULL) 2688 ill_refrele(dst_ill); 2689 return (0); 2690 } 2691 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2692 &ire->ire_uinfo; 2693 } else { 2694 ire = (ire_t *)ire_mp->b_rptr; 2695 ire_uinfo = 2696 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2697 } 2698 } 2699 if (dst_ill != NULL) 2700 ill_refrele(dst_ill); 2701 2702 ASSERT(ire != NULL); 2703 ASSERT(ire_uinfo != NULL); 2704 2705 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2706 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2707 /* 2708 * ire->ire_mp is non null when ire_mp passed in is used 2709 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2710 */ 2711 if (ire->ire_mp == NULL) 2712 ire_refrele(ire); 2713 if (sire != NULL) 2714 ire_refrele(sire); 2715 return (0); 2716 } 2717 2718 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2719 in6_addr_t src_addr; 2720 2721 /* 2722 * ip_bind_connected_v6() has stored the correct source 2723 * address per IPv6 addr. selection policy in 2724 * conn_src_v6. 2725 */ 2726 src_addr = tcp->tcp_connp->conn_srcv6; 2727 2728 tcp->tcp_ip6h->ip6_src = src_addr; 2729 /* 2730 * Copy of the src addr. in tcp_t is needed 2731 * for the lookup funcs. 2732 */ 2733 tcp->tcp_ip_src_v6 = src_addr; 2734 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2735 &connp->conn_srcv6)); 2736 } 2737 tcp->tcp_localnet = 2738 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2739 } 2740 2741 /* 2742 * This allows applications to fail quickly when connections are made 2743 * to dead hosts. Hosts can be labeled dead by adding a reject route 2744 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2745 */ 2746 if ((ire->ire_flags & RTF_REJECT) && 2747 (ire->ire_flags & RTF_PRIVATE)) 2748 goto error; 2749 2750 /* 2751 * Make use of the cached rtt and rtt_sd values to calculate the 2752 * initial RTO. Note that they are already initialized in 2753 * tcp_init_values(). 2754 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2755 * IP_NEXTHOP, but instead are using the interface ire for the 2756 * nexthop, then we do not use the ire_uinfo from that ire to 2757 * do any initializations. 2758 */ 2759 if (ire_uinfo != NULL) { 2760 if (ire_uinfo->iulp_rtt != 0) { 2761 clock_t rto; 2762 2763 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2764 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2765 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2766 tcps->tcps_rexmit_interval_extra + 2767 (tcp->tcp_rtt_sa >> 5); 2768 2769 if (rto > tcps->tcps_rexmit_interval_max) { 2770 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2771 } else if (rto < tcps->tcps_rexmit_interval_min) { 2772 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2773 } else { 2774 tcp->tcp_rto = rto; 2775 } 2776 } 2777 if (ire_uinfo->iulp_ssthresh != 0) 2778 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2779 else 2780 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2781 if (ire_uinfo->iulp_spipe > 0) { 2782 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2783 tcps->tcps_max_buf); 2784 if (tcps->tcps_snd_lowat_fraction != 0) 2785 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2786 tcps->tcps_snd_lowat_fraction; 2787 (void) tcp_maxpsz_set(tcp, B_TRUE); 2788 } 2789 /* 2790 * Note that up till now, acceptor always inherits receive 2791 * window from the listener. But if there is a metrics 2792 * associated with a host, we should use that instead of 2793 * inheriting it from listener. Thus we need to pass this 2794 * info back to the caller. 2795 */ 2796 if (ire_uinfo->iulp_rpipe > 0) { 2797 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2798 tcps->tcps_max_buf); 2799 } 2800 2801 if (ire_uinfo->iulp_rtomax > 0) { 2802 tcp->tcp_second_timer_threshold = 2803 ire_uinfo->iulp_rtomax; 2804 } 2805 2806 /* 2807 * Use the metric option settings, iulp_tstamp_ok and 2808 * iulp_wscale_ok, only for active open. What this means 2809 * is that if the other side uses timestamp or window 2810 * scale option, TCP will also use those options. That 2811 * is for passive open. If the application sets a 2812 * large window, window scale is enabled regardless of 2813 * the value in iulp_wscale_ok. This is the behavior 2814 * since 2.6. So we keep it. 2815 * The only case left in passive open processing is the 2816 * check for SACK. 2817 * For ECN, it should probably be like SACK. But the 2818 * current value is binary, so we treat it like the other 2819 * cases. The metric only controls active open.For passive 2820 * open, the ndd param, tcp_ecn_permitted, controls the 2821 * behavior. 2822 */ 2823 if (!tcp_detached) { 2824 /* 2825 * The if check means that the following can only 2826 * be turned on by the metrics only IRE, but not off. 2827 */ 2828 if (ire_uinfo->iulp_tstamp_ok) 2829 tcp->tcp_snd_ts_ok = B_TRUE; 2830 if (ire_uinfo->iulp_wscale_ok) 2831 tcp->tcp_snd_ws_ok = B_TRUE; 2832 if (ire_uinfo->iulp_sack == 2) 2833 tcp->tcp_snd_sack_ok = B_TRUE; 2834 if (ire_uinfo->iulp_ecn_ok) 2835 tcp->tcp_ecn_ok = B_TRUE; 2836 } else { 2837 /* 2838 * Passive open. 2839 * 2840 * As above, the if check means that SACK can only be 2841 * turned on by the metric only IRE. 2842 */ 2843 if (ire_uinfo->iulp_sack > 0) { 2844 tcp->tcp_snd_sack_ok = B_TRUE; 2845 } 2846 } 2847 } 2848 2849 2850 /* 2851 * XXX: Note that currently, ire_max_frag can be as small as 68 2852 * because of PMTUd. So tcp_mss may go to negative if combined 2853 * length of all those options exceeds 28 bytes. But because 2854 * of the tcp_mss_min check below, we may not have a problem if 2855 * tcp_mss_min is of a reasonable value. The default is 1 so 2856 * the negative problem still exists. And the check defeats PMTUd. 2857 * In fact, if PMTUd finds that the MSS should be smaller than 2858 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2859 * value. 2860 * 2861 * We do not deal with that now. All those problems related to 2862 * PMTUd will be fixed later. 2863 */ 2864 ASSERT(ire->ire_max_frag != 0); 2865 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2866 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2867 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2868 mss = MIN(mss, IPV6_MIN_MTU); 2869 } 2870 } 2871 2872 /* Sanity check for MSS value. */ 2873 if (tcp->tcp_ipversion == IPV4_VERSION) 2874 mss_max = tcps->tcps_mss_max_ipv4; 2875 else 2876 mss_max = tcps->tcps_mss_max_ipv6; 2877 2878 if (tcp->tcp_ipversion == IPV6_VERSION && 2879 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2880 /* 2881 * After receiving an ICMPv6 "packet too big" message with a 2882 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2883 * will insert a 8-byte fragment header in every packet; we 2884 * reduce the MSS by that amount here. 2885 */ 2886 mss -= sizeof (ip6_frag_t); 2887 } 2888 2889 if (tcp->tcp_ipsec_overhead == 0) 2890 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2891 2892 mss -= tcp->tcp_ipsec_overhead; 2893 2894 if (mss < tcps->tcps_mss_min) 2895 mss = tcps->tcps_mss_min; 2896 if (mss > mss_max) 2897 mss = mss_max; 2898 2899 /* Note that this is the maximum MSS, excluding all options. */ 2900 tcp->tcp_mss = mss; 2901 2902 /* 2903 * Initialize the ISS here now that we have the full connection ID. 2904 * The RFC 1948 method of initial sequence number generation requires 2905 * knowledge of the full connection ID before setting the ISS. 2906 */ 2907 2908 tcp_iss_init(tcp); 2909 2910 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2911 tcp->tcp_loopback = B_TRUE; 2912 2913 if (tcp->tcp_ipversion == IPV4_VERSION) { 2914 hsp = tcp_hsp_lookup(tcp->tcp_remote, tcps); 2915 } else { 2916 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6, tcps); 2917 } 2918 2919 if (hsp != NULL) { 2920 /* Only modify if we're going to make them bigger */ 2921 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2922 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2923 if (tcps->tcps_snd_lowat_fraction != 0) 2924 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2925 tcps->tcps_snd_lowat_fraction; 2926 } 2927 2928 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2929 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2930 } 2931 2932 /* Copy timestamp flag only for active open */ 2933 if (!tcp_detached) 2934 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 2935 } 2936 2937 if (sire != NULL) 2938 IRE_REFRELE(sire); 2939 2940 /* 2941 * If we got an IRE_CACHE and an ILL, go through their properties; 2942 * otherwise, this is deferred until later when we have an IRE_CACHE. 2943 */ 2944 if (tcp->tcp_loopback || 2945 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2946 /* 2947 * For incoming, see if this tcp may be MDT-capable. For 2948 * outgoing, this process has been taken care of through 2949 * tcp_rput_other. 2950 */ 2951 tcp_ire_ill_check(tcp, ire, ill, incoming); 2952 tcp->tcp_ire_ill_check_done = B_TRUE; 2953 } 2954 2955 mutex_enter(&connp->conn_lock); 2956 /* 2957 * Make sure that conn is not marked incipient 2958 * for incoming connections. A blind 2959 * removal of incipient flag is cheaper than 2960 * check and removal. 2961 */ 2962 connp->conn_state_flags &= ~CONN_INCIPIENT; 2963 2964 /* 2965 * Must not cache forwarding table routes 2966 * or recache an IRE after the conn_t has 2967 * had conn_ire_cache cleared and is flagged 2968 * unusable, (see the CONN_CACHE_IRE() macro). 2969 */ 2970 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 2971 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2972 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2973 connp->conn_ire_cache = ire; 2974 IRE_UNTRACE_REF(ire); 2975 rw_exit(&ire->ire_bucket->irb_lock); 2976 mutex_exit(&connp->conn_lock); 2977 return (1); 2978 } 2979 rw_exit(&ire->ire_bucket->irb_lock); 2980 } 2981 mutex_exit(&connp->conn_lock); 2982 2983 if (ire->ire_mp == NULL) 2984 ire_refrele(ire); 2985 return (1); 2986 2987 error: 2988 if (ire->ire_mp == NULL) 2989 ire_refrele(ire); 2990 if (sire != NULL) 2991 ire_refrele(sire); 2992 return (0); 2993 } 2994 2995 /* 2996 * tcp_bind is called (holding the writer lock) by tcp_wput_proto to process a 2997 * O_T_BIND_REQ/T_BIND_REQ message. 2998 */ 2999 static void 3000 tcp_bind(tcp_t *tcp, mblk_t *mp) 3001 { 3002 sin_t *sin; 3003 sin6_t *sin6; 3004 mblk_t *mp1; 3005 in_port_t requested_port; 3006 in_port_t allocated_port; 3007 struct T_bind_req *tbr; 3008 boolean_t bind_to_req_port_only; 3009 boolean_t backlog_update = B_FALSE; 3010 boolean_t user_specified; 3011 in6_addr_t v6addr; 3012 ipaddr_t v4addr; 3013 uint_t origipversion; 3014 int err; 3015 queue_t *q = tcp->tcp_wq; 3016 conn_t *connp = tcp->tcp_connp; 3017 mlp_type_t addrtype, mlptype; 3018 zone_t *zone; 3019 cred_t *cr; 3020 in_port_t mlp_port; 3021 tcp_stack_t *tcps = tcp->tcp_tcps; 3022 3023 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3024 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3025 if (tcp->tcp_debug) { 3026 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3027 "tcp_bind: bad req, len %u", 3028 (uint_t)(mp->b_wptr - mp->b_rptr)); 3029 } 3030 tcp_err_ack(tcp, mp, TPROTO, 0); 3031 return; 3032 } 3033 /* Make sure the largest address fits */ 3034 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3035 if (mp1 == NULL) { 3036 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3037 return; 3038 } 3039 mp = mp1; 3040 tbr = (struct T_bind_req *)mp->b_rptr; 3041 if (tcp->tcp_state >= TCPS_BOUND) { 3042 if ((tcp->tcp_state == TCPS_BOUND || 3043 tcp->tcp_state == TCPS_LISTEN) && 3044 tcp->tcp_conn_req_max != tbr->CONIND_number && 3045 tbr->CONIND_number > 0) { 3046 /* 3047 * Handle listen() increasing CONIND_number. 3048 * This is more "liberal" then what the TPI spec 3049 * requires but is needed to avoid a t_unbind 3050 * when handling listen() since the port number 3051 * might be "stolen" between the unbind and bind. 3052 */ 3053 backlog_update = B_TRUE; 3054 goto do_bind; 3055 } 3056 if (tcp->tcp_debug) { 3057 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3058 "tcp_bind: bad state, %d", tcp->tcp_state); 3059 } 3060 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 3061 return; 3062 } 3063 origipversion = tcp->tcp_ipversion; 3064 3065 switch (tbr->ADDR_length) { 3066 case 0: /* request for a generic port */ 3067 tbr->ADDR_offset = sizeof (struct T_bind_req); 3068 if (tcp->tcp_family == AF_INET) { 3069 tbr->ADDR_length = sizeof (sin_t); 3070 sin = (sin_t *)&tbr[1]; 3071 *sin = sin_null; 3072 sin->sin_family = AF_INET; 3073 mp->b_wptr = (uchar_t *)&sin[1]; 3074 tcp->tcp_ipversion = IPV4_VERSION; 3075 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 3076 } else { 3077 ASSERT(tcp->tcp_family == AF_INET6); 3078 tbr->ADDR_length = sizeof (sin6_t); 3079 sin6 = (sin6_t *)&tbr[1]; 3080 *sin6 = sin6_null; 3081 sin6->sin6_family = AF_INET6; 3082 mp->b_wptr = (uchar_t *)&sin6[1]; 3083 tcp->tcp_ipversion = IPV6_VERSION; 3084 V6_SET_ZERO(v6addr); 3085 } 3086 requested_port = 0; 3087 break; 3088 3089 case sizeof (sin_t): /* Complete IPv4 address */ 3090 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3091 sizeof (sin_t)); 3092 if (sin == NULL || !OK_32PTR((char *)sin)) { 3093 if (tcp->tcp_debug) { 3094 (void) strlog(TCP_MOD_ID, 0, 1, 3095 SL_ERROR|SL_TRACE, 3096 "tcp_bind: bad address parameter, " 3097 "offset %d, len %d", 3098 tbr->ADDR_offset, tbr->ADDR_length); 3099 } 3100 tcp_err_ack(tcp, mp, TPROTO, 0); 3101 return; 3102 } 3103 /* 3104 * With sockets sockfs will accept bogus sin_family in 3105 * bind() and replace it with the family used in the socket 3106 * call. 3107 */ 3108 if (sin->sin_family != AF_INET || 3109 tcp->tcp_family != AF_INET) { 3110 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3111 return; 3112 } 3113 requested_port = ntohs(sin->sin_port); 3114 tcp->tcp_ipversion = IPV4_VERSION; 3115 v4addr = sin->sin_addr.s_addr; 3116 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 3117 break; 3118 3119 case sizeof (sin6_t): /* Complete IPv6 address */ 3120 sin6 = (sin6_t *)mi_offset_param(mp, 3121 tbr->ADDR_offset, sizeof (sin6_t)); 3122 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3123 if (tcp->tcp_debug) { 3124 (void) strlog(TCP_MOD_ID, 0, 1, 3125 SL_ERROR|SL_TRACE, 3126 "tcp_bind: bad IPv6 address parameter, " 3127 "offset %d, len %d", tbr->ADDR_offset, 3128 tbr->ADDR_length); 3129 } 3130 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3131 return; 3132 } 3133 if (sin6->sin6_family != AF_INET6 || 3134 tcp->tcp_family != AF_INET6) { 3135 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3136 return; 3137 } 3138 requested_port = ntohs(sin6->sin6_port); 3139 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 3140 IPV4_VERSION : IPV6_VERSION; 3141 v6addr = sin6->sin6_addr; 3142 break; 3143 3144 default: 3145 if (tcp->tcp_debug) { 3146 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3147 "tcp_bind: bad address length, %d", 3148 tbr->ADDR_length); 3149 } 3150 tcp_err_ack(tcp, mp, TBADADDR, 0); 3151 return; 3152 } 3153 tcp->tcp_bound_source_v6 = v6addr; 3154 3155 /* Check for change in ipversion */ 3156 if (origipversion != tcp->tcp_ipversion) { 3157 ASSERT(tcp->tcp_family == AF_INET6); 3158 err = tcp->tcp_ipversion == IPV6_VERSION ? 3159 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 3160 if (err) { 3161 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3162 return; 3163 } 3164 } 3165 3166 /* 3167 * Initialize family specific fields. Copy of the src addr. 3168 * in tcp_t is needed for the lookup funcs. 3169 */ 3170 if (tcp->tcp_ipversion == IPV6_VERSION) { 3171 tcp->tcp_ip6h->ip6_src = v6addr; 3172 } else { 3173 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 3174 } 3175 tcp->tcp_ip_src_v6 = v6addr; 3176 3177 /* 3178 * For O_T_BIND_REQ: 3179 * Verify that the target port/addr is available, or choose 3180 * another. 3181 * For T_BIND_REQ: 3182 * Verify that the target port/addr is available or fail. 3183 * In both cases when it succeeds the tcp is inserted in the 3184 * bind hash table. This ensures that the operation is atomic 3185 * under the lock on the hash bucket. 3186 */ 3187 bind_to_req_port_only = requested_port != 0 && 3188 tbr->PRIM_type != O_T_BIND_REQ; 3189 /* 3190 * Get a valid port (within the anonymous range and should not 3191 * be a privileged one) to use if the user has not given a port. 3192 * If multiple threads are here, they may all start with 3193 * with the same initial port. But, it should be fine as long as 3194 * tcp_bindi will ensure that no two threads will be assigned 3195 * the same port. 3196 * 3197 * NOTE: XXX If a privileged process asks for an anonymous port, we 3198 * still check for ports only in the range > tcp_smallest_non_priv_port, 3199 * unless TCP_ANONPRIVBIND option is set. 3200 */ 3201 mlptype = mlptSingle; 3202 mlp_port = requested_port; 3203 if (requested_port == 0) { 3204 requested_port = tcp->tcp_anon_priv_bind ? 3205 tcp_get_next_priv_port(tcp) : 3206 tcp_update_next_port(tcps->tcps_next_port_to_try, 3207 tcp, B_TRUE); 3208 if (requested_port == 0) { 3209 tcp_err_ack(tcp, mp, TNOADDR, 0); 3210 return; 3211 } 3212 user_specified = B_FALSE; 3213 3214 /* 3215 * If the user went through one of the RPC interfaces to create 3216 * this socket and RPC is MLP in this zone, then give him an 3217 * anonymous MLP. 3218 */ 3219 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3220 if (connp->conn_anon_mlp && is_system_labeled()) { 3221 zone = crgetzone(cr); 3222 addrtype = tsol_mlp_addr_type(zone->zone_id, 3223 IPV6_VERSION, &v6addr, 3224 tcps->tcps_netstack->netstack_ip); 3225 if (addrtype == mlptSingle) { 3226 tcp_err_ack(tcp, mp, TNOADDR, 0); 3227 return; 3228 } 3229 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3230 PMAPPORT, addrtype); 3231 mlp_port = PMAPPORT; 3232 } 3233 } else { 3234 int i; 3235 boolean_t priv = B_FALSE; 3236 3237 /* 3238 * If the requested_port is in the well-known privileged range, 3239 * verify that the stream was opened by a privileged user. 3240 * Note: No locks are held when inspecting tcp_g_*epriv_ports 3241 * but instead the code relies on: 3242 * - the fact that the address of the array and its size never 3243 * changes 3244 * - the atomic assignment of the elements of the array 3245 */ 3246 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3247 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 3248 priv = B_TRUE; 3249 } else { 3250 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 3251 if (requested_port == 3252 tcps->tcps_g_epriv_ports[i]) { 3253 priv = B_TRUE; 3254 break; 3255 } 3256 } 3257 } 3258 if (priv) { 3259 if (secpolicy_net_privaddr(cr, requested_port, 3260 IPPROTO_TCP) != 0) { 3261 if (tcp->tcp_debug) { 3262 (void) strlog(TCP_MOD_ID, 0, 1, 3263 SL_ERROR|SL_TRACE, 3264 "tcp_bind: no priv for port %d", 3265 requested_port); 3266 } 3267 tcp_err_ack(tcp, mp, TACCES, 0); 3268 return; 3269 } 3270 } 3271 user_specified = B_TRUE; 3272 3273 if (is_system_labeled()) { 3274 zone = crgetzone(cr); 3275 addrtype = tsol_mlp_addr_type(zone->zone_id, 3276 IPV6_VERSION, &v6addr, 3277 tcps->tcps_netstack->netstack_ip); 3278 if (addrtype == mlptSingle) { 3279 tcp_err_ack(tcp, mp, TNOADDR, 0); 3280 return; 3281 } 3282 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3283 requested_port, addrtype); 3284 } 3285 } 3286 3287 if (mlptype != mlptSingle) { 3288 if (secpolicy_net_bindmlp(cr) != 0) { 3289 if (tcp->tcp_debug) { 3290 (void) strlog(TCP_MOD_ID, 0, 1, 3291 SL_ERROR|SL_TRACE, 3292 "tcp_bind: no priv for multilevel port %d", 3293 requested_port); 3294 } 3295 tcp_err_ack(tcp, mp, TACCES, 0); 3296 return; 3297 } 3298 3299 /* 3300 * If we're specifically binding a shared IP address and the 3301 * port is MLP on shared addresses, then check to see if this 3302 * zone actually owns the MLP. Reject if not. 3303 */ 3304 if (mlptype == mlptShared && addrtype == mlptShared) { 3305 /* 3306 * No need to handle exclusive-stack zones since 3307 * ALL_ZONES only applies to the shared stack. 3308 */ 3309 zoneid_t mlpzone; 3310 3311 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 3312 htons(mlp_port)); 3313 if (connp->conn_zoneid != mlpzone) { 3314 if (tcp->tcp_debug) { 3315 (void) strlog(TCP_MOD_ID, 0, 1, 3316 SL_ERROR|SL_TRACE, 3317 "tcp_bind: attempt to bind port " 3318 "%d on shared addr in zone %d " 3319 "(should be %d)", 3320 mlp_port, connp->conn_zoneid, 3321 mlpzone); 3322 } 3323 tcp_err_ack(tcp, mp, TACCES, 0); 3324 return; 3325 } 3326 } 3327 3328 if (!user_specified) { 3329 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3330 requested_port, B_TRUE); 3331 if (err != 0) { 3332 if (tcp->tcp_debug) { 3333 (void) strlog(TCP_MOD_ID, 0, 1, 3334 SL_ERROR|SL_TRACE, 3335 "tcp_bind: cannot establish anon " 3336 "MLP for port %d", 3337 requested_port); 3338 } 3339 tcp_err_ack(tcp, mp, TSYSERR, err); 3340 return; 3341 } 3342 connp->conn_anon_port = B_TRUE; 3343 } 3344 connp->conn_mlp_type = mlptype; 3345 } 3346 3347 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 3348 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 3349 3350 if (allocated_port == 0) { 3351 connp->conn_mlp_type = mlptSingle; 3352 if (connp->conn_anon_port) { 3353 connp->conn_anon_port = B_FALSE; 3354 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3355 requested_port, B_FALSE); 3356 } 3357 if (bind_to_req_port_only) { 3358 if (tcp->tcp_debug) { 3359 (void) strlog(TCP_MOD_ID, 0, 1, 3360 SL_ERROR|SL_TRACE, 3361 "tcp_bind: requested addr busy"); 3362 } 3363 tcp_err_ack(tcp, mp, TADDRBUSY, 0); 3364 } else { 3365 /* If we are out of ports, fail the bind. */ 3366 if (tcp->tcp_debug) { 3367 (void) strlog(TCP_MOD_ID, 0, 1, 3368 SL_ERROR|SL_TRACE, 3369 "tcp_bind: out of ports?"); 3370 } 3371 tcp_err_ack(tcp, mp, TNOADDR, 0); 3372 } 3373 return; 3374 } 3375 ASSERT(tcp->tcp_state == TCPS_BOUND); 3376 do_bind: 3377 if (!backlog_update) { 3378 if (tcp->tcp_family == AF_INET) 3379 sin->sin_port = htons(allocated_port); 3380 else 3381 sin6->sin6_port = htons(allocated_port); 3382 } 3383 if (tcp->tcp_family == AF_INET) { 3384 if (tbr->CONIND_number != 0) { 3385 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3386 sizeof (sin_t)); 3387 } else { 3388 /* Just verify the local IP address */ 3389 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, IP_ADDR_LEN); 3390 } 3391 } else { 3392 if (tbr->CONIND_number != 0) { 3393 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3394 sizeof (sin6_t)); 3395 } else { 3396 /* Just verify the local IP address */ 3397 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3398 IPV6_ADDR_LEN); 3399 } 3400 } 3401 if (mp1 == NULL) { 3402 if (connp->conn_anon_port) { 3403 connp->conn_anon_port = B_FALSE; 3404 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3405 requested_port, B_FALSE); 3406 } 3407 connp->conn_mlp_type = mlptSingle; 3408 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3409 return; 3410 } 3411 3412 tbr->PRIM_type = T_BIND_ACK; 3413 mp->b_datap->db_type = M_PCPROTO; 3414 3415 /* Chain in the reply mp for tcp_rput() */ 3416 mp1->b_cont = mp; 3417 mp = mp1; 3418 3419 tcp->tcp_conn_req_max = tbr->CONIND_number; 3420 if (tcp->tcp_conn_req_max) { 3421 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 3422 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 3423 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 3424 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 3425 /* 3426 * If this is a listener, do not reset the eager list 3427 * and other stuffs. Note that we don't check if the 3428 * existing eager list meets the new tcp_conn_req_max 3429 * requirement. 3430 */ 3431 if (tcp->tcp_state != TCPS_LISTEN) { 3432 tcp->tcp_state = TCPS_LISTEN; 3433 /* Initialize the chain. Don't need the eager_lock */ 3434 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 3435 tcp->tcp_eager_next_drop_q0 = tcp; 3436 tcp->tcp_eager_prev_drop_q0 = tcp; 3437 tcp->tcp_second_ctimer_threshold = 3438 tcps->tcps_ip_abort_linterval; 3439 } 3440 } 3441 3442 /* 3443 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 3444 * processing continues in tcp_rput_other(). 3445 * 3446 * We need to make sure that the conn_recv is set to a non-null 3447 * value before we insert the conn into the classifier table. 3448 * This is to avoid a race with an incoming packet which does an 3449 * ipcl_classify(). 3450 */ 3451 connp->conn_recv = tcp_conn_request; 3452 if (tcp->tcp_family == AF_INET6) { 3453 ASSERT(tcp->tcp_connp->conn_af_isv6); 3454 mp = ip_bind_v6(q, mp, tcp->tcp_connp, &tcp->tcp_sticky_ipp); 3455 } else { 3456 ASSERT(!tcp->tcp_connp->conn_af_isv6); 3457 mp = ip_bind_v4(q, mp, tcp->tcp_connp); 3458 } 3459 /* 3460 * If the bind cannot complete immediately 3461 * IP will arrange to call tcp_rput_other 3462 * when the bind completes. 3463 */ 3464 if (mp != NULL) { 3465 tcp_rput_other(tcp, mp); 3466 } else { 3467 /* 3468 * Bind will be resumed later. Need to ensure 3469 * that conn doesn't disappear when that happens. 3470 * This will be decremented in ip_resume_tcp_bind(). 3471 */ 3472 CONN_INC_REF(tcp->tcp_connp); 3473 } 3474 } 3475 3476 3477 /* 3478 * If the "bind_to_req_port_only" parameter is set, if the requested port 3479 * number is available, return it, If not return 0 3480 * 3481 * If "bind_to_req_port_only" parameter is not set and 3482 * If the requested port number is available, return it. If not, return 3483 * the first anonymous port we happen across. If no anonymous ports are 3484 * available, return 0. addr is the requested local address, if any. 3485 * 3486 * In either case, when succeeding update the tcp_t to record the port number 3487 * and insert it in the bind hash table. 3488 * 3489 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3490 * without setting SO_REUSEADDR. This is needed so that they 3491 * can be viewed as two independent transport protocols. 3492 */ 3493 static in_port_t 3494 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3495 int reuseaddr, boolean_t quick_connect, 3496 boolean_t bind_to_req_port_only, boolean_t user_specified) 3497 { 3498 /* number of times we have run around the loop */ 3499 int count = 0; 3500 /* maximum number of times to run around the loop */ 3501 int loopmax; 3502 conn_t *connp = tcp->tcp_connp; 3503 zoneid_t zoneid = connp->conn_zoneid; 3504 tcp_stack_t *tcps = tcp->tcp_tcps; 3505 3506 /* 3507 * Lookup for free addresses is done in a loop and "loopmax" 3508 * influences how long we spin in the loop 3509 */ 3510 if (bind_to_req_port_only) { 3511 /* 3512 * If the requested port is busy, don't bother to look 3513 * for a new one. Setting loop maximum count to 1 has 3514 * that effect. 3515 */ 3516 loopmax = 1; 3517 } else { 3518 /* 3519 * If the requested port is busy, look for a free one 3520 * in the anonymous port range. 3521 * Set loopmax appropriately so that one does not look 3522 * forever in the case all of the anonymous ports are in use. 3523 */ 3524 if (tcp->tcp_anon_priv_bind) { 3525 /* 3526 * loopmax = 3527 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3528 */ 3529 loopmax = IPPORT_RESERVED - 3530 tcps->tcps_min_anonpriv_port; 3531 } else { 3532 loopmax = (tcps->tcps_largest_anon_port - 3533 tcps->tcps_smallest_anon_port + 1); 3534 } 3535 } 3536 do { 3537 uint16_t lport; 3538 tf_t *tbf; 3539 tcp_t *ltcp; 3540 conn_t *lconnp; 3541 3542 lport = htons(port); 3543 3544 /* 3545 * Ensure that the tcp_t is not currently in the bind hash. 3546 * Hold the lock on the hash bucket to ensure that 3547 * the duplicate check plus the insertion is an atomic 3548 * operation. 3549 * 3550 * This function does an inline lookup on the bind hash list 3551 * Make sure that we access only members of tcp_t 3552 * and that we don't look at tcp_tcp, since we are not 3553 * doing a CONN_INC_REF. 3554 */ 3555 tcp_bind_hash_remove(tcp); 3556 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3557 mutex_enter(&tbf->tf_lock); 3558 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3559 ltcp = ltcp->tcp_bind_hash) { 3560 boolean_t not_socket; 3561 boolean_t exclbind; 3562 3563 if (lport != ltcp->tcp_lport) 3564 continue; 3565 3566 lconnp = ltcp->tcp_connp; 3567 3568 /* 3569 * On a labeled system, we must treat bindings to ports 3570 * on shared IP addresses by sockets with MAC exemption 3571 * privilege as being in all zones, as there's 3572 * otherwise no way to identify the right receiver. 3573 */ 3574 if (!(IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) || 3575 IPCL_ZONE_MATCH(connp, 3576 ltcp->tcp_connp->conn_zoneid)) && 3577 !lconnp->conn_mac_exempt && 3578 !connp->conn_mac_exempt) 3579 continue; 3580 3581 /* 3582 * If TCP_EXCLBIND is set for either the bound or 3583 * binding endpoint, the semantics of bind 3584 * is changed according to the following. 3585 * 3586 * spec = specified address (v4 or v6) 3587 * unspec = unspecified address (v4 or v6) 3588 * A = specified addresses are different for endpoints 3589 * 3590 * bound bind to allowed 3591 * ------------------------------------- 3592 * unspec unspec no 3593 * unspec spec no 3594 * spec unspec no 3595 * spec spec yes if A 3596 * 3597 * For labeled systems, SO_MAC_EXEMPT behaves the same 3598 * as TCP_EXCLBIND, except that zoneid is ignored. 3599 * 3600 * Note: 3601 * 3602 * 1. Because of TLI semantics, an endpoint can go 3603 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3604 * TCPS_BOUND, depending on whether it is originally 3605 * a listener or not. That is why we need to check 3606 * for states greater than or equal to TCPS_BOUND 3607 * here. 3608 * 3609 * 2. Ideally, we should only check for state equals 3610 * to TCPS_LISTEN. And the following check should be 3611 * added. 3612 * 3613 * if (ltcp->tcp_state == TCPS_LISTEN || 3614 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3615 * ... 3616 * } 3617 * 3618 * The semantics will be changed to this. If the 3619 * endpoint on the list is in state not equal to 3620 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3621 * set, let the bind succeed. 3622 * 3623 * Because of (1), we cannot do that for TLI 3624 * endpoints. But we can do that for socket endpoints. 3625 * If in future, we can change this going back 3626 * semantics, we can use the above check for TLI also. 3627 */ 3628 not_socket = !(TCP_IS_SOCKET(ltcp) && 3629 TCP_IS_SOCKET(tcp)); 3630 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3631 3632 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3633 (exclbind && (not_socket || 3634 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3635 if (V6_OR_V4_INADDR_ANY( 3636 ltcp->tcp_bound_source_v6) || 3637 V6_OR_V4_INADDR_ANY(*laddr) || 3638 IN6_ARE_ADDR_EQUAL(laddr, 3639 <cp->tcp_bound_source_v6)) { 3640 break; 3641 } 3642 continue; 3643 } 3644 3645 /* 3646 * Check ipversion to allow IPv4 and IPv6 sockets to 3647 * have disjoint port number spaces, if *_EXCLBIND 3648 * is not set and only if the application binds to a 3649 * specific port. We use the same autoassigned port 3650 * number space for IPv4 and IPv6 sockets. 3651 */ 3652 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3653 bind_to_req_port_only) 3654 continue; 3655 3656 /* 3657 * Ideally, we should make sure that the source 3658 * address, remote address, and remote port in the 3659 * four tuple for this tcp-connection is unique. 3660 * However, trying to find out the local source 3661 * address would require too much code duplication 3662 * with IP, since IP needs needs to have that code 3663 * to support userland TCP implementations. 3664 */ 3665 if (quick_connect && 3666 (ltcp->tcp_state > TCPS_LISTEN) && 3667 ((tcp->tcp_fport != ltcp->tcp_fport) || 3668 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3669 <cp->tcp_remote_v6))) 3670 continue; 3671 3672 if (!reuseaddr) { 3673 /* 3674 * No socket option SO_REUSEADDR. 3675 * If existing port is bound to 3676 * a non-wildcard IP address 3677 * and the requesting stream is 3678 * bound to a distinct 3679 * different IP addresses 3680 * (non-wildcard, also), keep 3681 * going. 3682 */ 3683 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3684 !V6_OR_V4_INADDR_ANY( 3685 ltcp->tcp_bound_source_v6) && 3686 !IN6_ARE_ADDR_EQUAL(laddr, 3687 <cp->tcp_bound_source_v6)) 3688 continue; 3689 if (ltcp->tcp_state >= TCPS_BOUND) { 3690 /* 3691 * This port is being used and 3692 * its state is >= TCPS_BOUND, 3693 * so we can't bind to it. 3694 */ 3695 break; 3696 } 3697 } else { 3698 /* 3699 * socket option SO_REUSEADDR is set on the 3700 * binding tcp_t. 3701 * 3702 * If two streams are bound to 3703 * same IP address or both addr 3704 * and bound source are wildcards 3705 * (INADDR_ANY), we want to stop 3706 * searching. 3707 * We have found a match of IP source 3708 * address and source port, which is 3709 * refused regardless of the 3710 * SO_REUSEADDR setting, so we break. 3711 */ 3712 if (IN6_ARE_ADDR_EQUAL(laddr, 3713 <cp->tcp_bound_source_v6) && 3714 (ltcp->tcp_state == TCPS_LISTEN || 3715 ltcp->tcp_state == TCPS_BOUND)) 3716 break; 3717 } 3718 } 3719 if (ltcp != NULL) { 3720 /* The port number is busy */ 3721 mutex_exit(&tbf->tf_lock); 3722 } else { 3723 /* 3724 * This port is ours. Insert in fanout and mark as 3725 * bound to prevent others from getting the port 3726 * number. 3727 */ 3728 tcp->tcp_state = TCPS_BOUND; 3729 tcp->tcp_lport = htons(port); 3730 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3731 3732 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3733 tcp->tcp_lport)] == tbf); 3734 tcp_bind_hash_insert(tbf, tcp, 1); 3735 3736 mutex_exit(&tbf->tf_lock); 3737 3738 /* 3739 * We don't want tcp_next_port_to_try to "inherit" 3740 * a port number supplied by the user in a bind. 3741 */ 3742 if (user_specified) 3743 return (port); 3744 3745 /* 3746 * This is the only place where tcp_next_port_to_try 3747 * is updated. After the update, it may or may not 3748 * be in the valid range. 3749 */ 3750 if (!tcp->tcp_anon_priv_bind) 3751 tcps->tcps_next_port_to_try = port + 1; 3752 return (port); 3753 } 3754 3755 if (tcp->tcp_anon_priv_bind) { 3756 port = tcp_get_next_priv_port(tcp); 3757 } else { 3758 if (count == 0 && user_specified) { 3759 /* 3760 * We may have to return an anonymous port. So 3761 * get one to start with. 3762 */ 3763 port = 3764 tcp_update_next_port( 3765 tcps->tcps_next_port_to_try, 3766 tcp, B_TRUE); 3767 user_specified = B_FALSE; 3768 } else { 3769 port = tcp_update_next_port(port + 1, tcp, 3770 B_FALSE); 3771 } 3772 } 3773 if (port == 0) 3774 break; 3775 3776 /* 3777 * Don't let this loop run forever in the case where 3778 * all of the anonymous ports are in use. 3779 */ 3780 } while (++count < loopmax); 3781 return (0); 3782 } 3783 3784 /* 3785 * tcp_clean_death / tcp_close_detached must not be called more than once 3786 * on a tcp. Thus every function that potentially calls tcp_clean_death 3787 * must check for the tcp state before calling tcp_clean_death. 3788 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3789 * tcp_timer_handler, all check for the tcp state. 3790 */ 3791 /* ARGSUSED */ 3792 void 3793 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3794 { 3795 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3796 3797 freemsg(mp); 3798 if (tcp->tcp_state > TCPS_BOUND) 3799 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, 3800 ETIMEDOUT, 5); 3801 } 3802 3803 /* 3804 * We are dying for some reason. Try to do it gracefully. (May be called 3805 * as writer.) 3806 * 3807 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3808 * done by a service procedure). 3809 * TBD - Should the return value distinguish between the tcp_t being 3810 * freed and it being reinitialized? 3811 */ 3812 static int 3813 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3814 { 3815 mblk_t *mp; 3816 queue_t *q; 3817 tcp_stack_t *tcps = tcp->tcp_tcps; 3818 sodirect_t *sodp; 3819 3820 TCP_CLD_STAT(tag); 3821 3822 #if TCP_TAG_CLEAN_DEATH 3823 tcp->tcp_cleandeathtag = tag; 3824 #endif 3825 3826 if (tcp->tcp_fused) 3827 tcp_unfuse(tcp); 3828 3829 if (tcp->tcp_linger_tid != 0 && 3830 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3831 tcp_stop_lingering(tcp); 3832 } 3833 3834 ASSERT(tcp != NULL); 3835 ASSERT((tcp->tcp_family == AF_INET && 3836 tcp->tcp_ipversion == IPV4_VERSION) || 3837 (tcp->tcp_family == AF_INET6 && 3838 (tcp->tcp_ipversion == IPV4_VERSION || 3839 tcp->tcp_ipversion == IPV6_VERSION))); 3840 3841 if (TCP_IS_DETACHED(tcp)) { 3842 if (tcp->tcp_hard_binding) { 3843 /* 3844 * Its an eager that we are dealing with. We close the 3845 * eager but in case a conn_ind has already gone to the 3846 * listener, let tcp_accept_finish() send a discon_ind 3847 * to the listener and drop the last reference. If the 3848 * listener doesn't even know about the eager i.e. the 3849 * conn_ind hasn't gone up, blow away the eager and drop 3850 * the last reference as well. If the conn_ind has gone 3851 * up, state should be BOUND. tcp_accept_finish 3852 * will figure out that the connection has received a 3853 * RST and will send a DISCON_IND to the application. 3854 */ 3855 tcp_closei_local(tcp); 3856 if (!tcp->tcp_tconnind_started) { 3857 CONN_DEC_REF(tcp->tcp_connp); 3858 } else { 3859 tcp->tcp_state = TCPS_BOUND; 3860 } 3861 } else { 3862 tcp_close_detached(tcp); 3863 } 3864 return (0); 3865 } 3866 3867 TCP_STAT(tcps, tcp_clean_death_nondetached); 3868 3869 /* If sodirect, not anymore */ 3870 SOD_PTR_ENTER(tcp, sodp); 3871 if (sodp != NULL) { 3872 tcp->tcp_sodirect = NULL; 3873 mutex_exit(sodp->sod_lockp); 3874 } 3875 3876 q = tcp->tcp_rq; 3877 3878 /* Trash all inbound data */ 3879 flushq(q, FLUSHALL); 3880 3881 /* 3882 * If we are at least part way open and there is error 3883 * (err==0 implies no error) 3884 * notify our client by a T_DISCON_IND. 3885 */ 3886 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3887 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3888 !TCP_IS_SOCKET(tcp)) { 3889 /* 3890 * Send M_FLUSH according to TPI. Because sockets will 3891 * (and must) ignore FLUSHR we do that only for TPI 3892 * endpoints and sockets in STREAMS mode. 3893 */ 3894 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3895 } 3896 if (tcp->tcp_debug) { 3897 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3898 "tcp_clean_death: discon err %d", err); 3899 } 3900 mp = mi_tpi_discon_ind(NULL, err, 0); 3901 if (mp != NULL) { 3902 putnext(q, mp); 3903 } else { 3904 if (tcp->tcp_debug) { 3905 (void) strlog(TCP_MOD_ID, 0, 1, 3906 SL_ERROR|SL_TRACE, 3907 "tcp_clean_death, sending M_ERROR"); 3908 } 3909 (void) putnextctl1(q, M_ERROR, EPROTO); 3910 } 3911 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3912 /* SYN_SENT or SYN_RCVD */ 3913 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3914 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3915 /* ESTABLISHED or CLOSE_WAIT */ 3916 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3917 } 3918 } 3919 3920 tcp_reinit(tcp); 3921 return (-1); 3922 } 3923 3924 /* 3925 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3926 * to expire, stop the wait and finish the close. 3927 */ 3928 static void 3929 tcp_stop_lingering(tcp_t *tcp) 3930 { 3931 clock_t delta = 0; 3932 tcp_stack_t *tcps = tcp->tcp_tcps; 3933 3934 tcp->tcp_linger_tid = 0; 3935 if (tcp->tcp_state > TCPS_LISTEN) { 3936 tcp_acceptor_hash_remove(tcp); 3937 mutex_enter(&tcp->tcp_non_sq_lock); 3938 if (tcp->tcp_flow_stopped) { 3939 tcp_clrqfull(tcp); 3940 } 3941 mutex_exit(&tcp->tcp_non_sq_lock); 3942 3943 if (tcp->tcp_timer_tid != 0) { 3944 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3945 tcp->tcp_timer_tid = 0; 3946 } 3947 /* 3948 * Need to cancel those timers which will not be used when 3949 * TCP is detached. This has to be done before the tcp_wq 3950 * is set to the global queue. 3951 */ 3952 tcp_timers_stop(tcp); 3953 3954 3955 tcp->tcp_detached = B_TRUE; 3956 ASSERT(tcps->tcps_g_q != NULL); 3957 tcp->tcp_rq = tcps->tcps_g_q; 3958 tcp->tcp_wq = WR(tcps->tcps_g_q); 3959 3960 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3961 tcp_time_wait_append(tcp); 3962 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3963 goto finish; 3964 } 3965 3966 /* 3967 * If delta is zero the timer event wasn't executed and was 3968 * successfully canceled. In this case we need to restart it 3969 * with the minimal delta possible. 3970 */ 3971 if (delta >= 0) { 3972 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3973 delta ? delta : 1); 3974 } 3975 } else { 3976 tcp_closei_local(tcp); 3977 CONN_DEC_REF(tcp->tcp_connp); 3978 } 3979 finish: 3980 /* Signal closing thread that it can complete close */ 3981 mutex_enter(&tcp->tcp_closelock); 3982 tcp->tcp_detached = B_TRUE; 3983 ASSERT(tcps->tcps_g_q != NULL); 3984 tcp->tcp_rq = tcps->tcps_g_q; 3985 tcp->tcp_wq = WR(tcps->tcps_g_q); 3986 tcp->tcp_closed = 1; 3987 cv_signal(&tcp->tcp_closecv); 3988 mutex_exit(&tcp->tcp_closelock); 3989 } 3990 3991 /* 3992 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3993 * expires. 3994 */ 3995 static void 3996 tcp_close_linger_timeout(void *arg) 3997 { 3998 conn_t *connp = (conn_t *)arg; 3999 tcp_t *tcp = connp->conn_tcp; 4000 4001 tcp->tcp_client_errno = ETIMEDOUT; 4002 tcp_stop_lingering(tcp); 4003 } 4004 4005 static int 4006 tcp_close(queue_t *q, int flags) 4007 { 4008 conn_t *connp = Q_TO_CONN(q); 4009 tcp_t *tcp = connp->conn_tcp; 4010 mblk_t *mp = &tcp->tcp_closemp; 4011 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 4012 mblk_t *bp; 4013 4014 ASSERT(WR(q)->q_next == NULL); 4015 ASSERT(connp->conn_ref >= 2); 4016 4017 /* 4018 * We are being closed as /dev/tcp or /dev/tcp6. 4019 * 4020 * Mark the conn as closing. ill_pending_mp_add will not 4021 * add any mp to the pending mp list, after this conn has 4022 * started closing. Same for sq_pending_mp_add 4023 */ 4024 mutex_enter(&connp->conn_lock); 4025 connp->conn_state_flags |= CONN_CLOSING; 4026 if (connp->conn_oper_pending_ill != NULL) 4027 conn_ioctl_cleanup_reqd = B_TRUE; 4028 CONN_INC_REF_LOCKED(connp); 4029 mutex_exit(&connp->conn_lock); 4030 tcp->tcp_closeflags = (uint8_t)flags; 4031 ASSERT(connp->conn_ref >= 3); 4032 4033 /* 4034 * tcp_closemp_used is used below without any protection of a lock 4035 * as we don't expect any one else to use it concurrently at this 4036 * point otherwise it would be a major defect. 4037 */ 4038 4039 if (mp->b_prev == NULL) 4040 tcp->tcp_closemp_used = B_TRUE; 4041 else 4042 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: " 4043 "connp %p tcp %p\n", (void *)connp, (void *)tcp); 4044 4045 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 4046 4047 (*tcp_squeue_close_proc)(connp->conn_sqp, mp, 4048 tcp_close_output, connp, SQTAG_IP_TCP_CLOSE); 4049 4050 mutex_enter(&tcp->tcp_closelock); 4051 while (!tcp->tcp_closed) { 4052 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 4053 /* 4054 * The cv_wait_sig() was interrupted. We now do the 4055 * following: 4056 * 4057 * 1) If the endpoint was lingering, we allow this 4058 * to be interrupted by cancelling the linger timeout 4059 * and closing normally. 4060 * 4061 * 2) Revert to calling cv_wait() 4062 * 4063 * We revert to using cv_wait() to avoid an 4064 * infinite loop which can occur if the calling 4065 * thread is higher priority than the squeue worker 4066 * thread and is bound to the same cpu. 4067 */ 4068 if (tcp->tcp_linger && tcp->tcp_lingertime > 0) { 4069 mutex_exit(&tcp->tcp_closelock); 4070 /* Entering squeue, bump ref count. */ 4071 CONN_INC_REF(connp); 4072 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 4073 squeue_enter(connp->conn_sqp, bp, 4074 tcp_linger_interrupted, connp, 4075 SQTAG_IP_TCP_CLOSE); 4076 mutex_enter(&tcp->tcp_closelock); 4077 } 4078 break; 4079 } 4080 } 4081 while (!tcp->tcp_closed) 4082 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 4083 mutex_exit(&tcp->tcp_closelock); 4084 4085 /* 4086 * In the case of listener streams that have eagers in the q or q0 4087 * we wait for the eagers to drop their reference to us. tcp_rq and 4088 * tcp_wq of the eagers point to our queues. By waiting for the 4089 * refcnt to drop to 1, we are sure that the eagers have cleaned 4090 * up their queue pointers and also dropped their references to us. 4091 */ 4092 if (tcp->tcp_wait_for_eagers) { 4093 mutex_enter(&connp->conn_lock); 4094 while (connp->conn_ref != 1) { 4095 cv_wait(&connp->conn_cv, &connp->conn_lock); 4096 } 4097 mutex_exit(&connp->conn_lock); 4098 } 4099 /* 4100 * ioctl cleanup. The mp is queued in the 4101 * ill_pending_mp or in the sq_pending_mp. 4102 */ 4103 if (conn_ioctl_cleanup_reqd) 4104 conn_ioctl_cleanup(connp); 4105 4106 qprocsoff(q); 4107 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 4108 4109 tcp->tcp_cpid = -1; 4110 4111 /* 4112 * Drop IP's reference on the conn. This is the last reference 4113 * on the connp if the state was less than established. If the 4114 * connection has gone into timewait state, then we will have 4115 * one ref for the TCP and one more ref (total of two) for the 4116 * classifier connected hash list (a timewait connections stays 4117 * in connected hash till closed). 4118 * 4119 * We can't assert the references because there might be other 4120 * transient reference places because of some walkers or queued 4121 * packets in squeue for the timewait state. 4122 */ 4123 CONN_DEC_REF(connp); 4124 q->q_ptr = WR(q)->q_ptr = NULL; 4125 return (0); 4126 } 4127 4128 static int 4129 tcpclose_accept(queue_t *q) 4130 { 4131 vmem_t *minor_arena; 4132 dev_t conn_dev; 4133 4134 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 4135 4136 /* 4137 * We had opened an acceptor STREAM for sockfs which is 4138 * now being closed due to some error. 4139 */ 4140 qprocsoff(q); 4141 4142 minor_arena = (vmem_t *)WR(q)->q_ptr; 4143 conn_dev = (dev_t)RD(q)->q_ptr; 4144 ASSERT(minor_arena != NULL); 4145 ASSERT(conn_dev != 0); 4146 inet_minor_free(minor_arena, conn_dev); 4147 q->q_ptr = WR(q)->q_ptr = NULL; 4148 return (0); 4149 } 4150 4151 /* 4152 * Called by tcp_close() routine via squeue when lingering is 4153 * interrupted by a signal. 4154 */ 4155 4156 /* ARGSUSED */ 4157 static void 4158 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 4159 { 4160 conn_t *connp = (conn_t *)arg; 4161 tcp_t *tcp = connp->conn_tcp; 4162 4163 freeb(mp); 4164 if (tcp->tcp_linger_tid != 0 && 4165 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 4166 tcp_stop_lingering(tcp); 4167 tcp->tcp_client_errno = EINTR; 4168 } 4169 } 4170 4171 /* 4172 * Called by streams close routine via squeues when our client blows off her 4173 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 4174 * connection politely" When SO_LINGER is set (with a non-zero linger time and 4175 * it is not a nonblocking socket) then this routine sleeps until the FIN is 4176 * acked. 4177 * 4178 * NOTE: tcp_close potentially returns error when lingering. 4179 * However, the stream head currently does not pass these errors 4180 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 4181 * errors to the application (from tsleep()) and not errors 4182 * like ECONNRESET caused by receiving a reset packet. 4183 */ 4184 4185 /* ARGSUSED */ 4186 static void 4187 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 4188 { 4189 char *msg; 4190 conn_t *connp = (conn_t *)arg; 4191 tcp_t *tcp = connp->conn_tcp; 4192 clock_t delta = 0; 4193 tcp_stack_t *tcps = tcp->tcp_tcps; 4194 4195 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 4196 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 4197 4198 /* End point has closed this TCP, no need to send up T_ordrel_ind. */ 4199 if (tcp->tcp_ordrel_mp != NULL) { 4200 freeb(tcp->tcp_ordrel_mp); 4201 tcp->tcp_ordrel_mp = NULL; 4202 } 4203 4204 mutex_enter(&tcp->tcp_eager_lock); 4205 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 4206 /* Cleanup for listener */ 4207 tcp_eager_cleanup(tcp, 0); 4208 tcp->tcp_wait_for_eagers = 1; 4209 } 4210 mutex_exit(&tcp->tcp_eager_lock); 4211 4212 connp->conn_mdt_ok = B_FALSE; 4213 tcp->tcp_mdt = B_FALSE; 4214 4215 connp->conn_lso_ok = B_FALSE; 4216 tcp->tcp_lso = B_FALSE; 4217 4218 msg = NULL; 4219 switch (tcp->tcp_state) { 4220 case TCPS_CLOSED: 4221 case TCPS_IDLE: 4222 case TCPS_BOUND: 4223 case TCPS_LISTEN: 4224 break; 4225 case TCPS_SYN_SENT: 4226 msg = "tcp_close, during connect"; 4227 break; 4228 case TCPS_SYN_RCVD: 4229 /* 4230 * Close during the connect 3-way handshake 4231 * but here there may or may not be pending data 4232 * already on queue. Process almost same as in 4233 * the ESTABLISHED state. 4234 */ 4235 /* FALLTHRU */ 4236 default: 4237 if (tcp->tcp_sodirect != NULL) { 4238 /* Ok, no more sodirect */ 4239 tcp->tcp_sodirect = NULL; 4240 } 4241 4242 if (tcp->tcp_fused) 4243 tcp_unfuse(tcp); 4244 4245 /* 4246 * If SO_LINGER has set a zero linger time, abort the 4247 * connection with a reset. 4248 */ 4249 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 4250 msg = "tcp_close, zero lingertime"; 4251 break; 4252 } 4253 4254 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 4255 /* 4256 * Abort connection if there is unread data queued. 4257 */ 4258 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 4259 msg = "tcp_close, unread data"; 4260 break; 4261 } 4262 /* 4263 * tcp_hard_bound is now cleared thus all packets go through 4264 * tcp_lookup. This fact is used by tcp_detach below. 4265 * 4266 * We have done a qwait() above which could have possibly 4267 * drained more messages in turn causing transition to a 4268 * different state. Check whether we have to do the rest 4269 * of the processing or not. 4270 */ 4271 if (tcp->tcp_state <= TCPS_LISTEN) 4272 break; 4273 4274 /* 4275 * Transmit the FIN before detaching the tcp_t. 4276 * After tcp_detach returns this queue/perimeter 4277 * no longer owns the tcp_t thus others can modify it. 4278 */ 4279 (void) tcp_xmit_end(tcp); 4280 4281 /* 4282 * If lingering on close then wait until the fin is acked, 4283 * the SO_LINGER time passes, or a reset is sent/received. 4284 */ 4285 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4286 !(tcp->tcp_fin_acked) && 4287 tcp->tcp_state >= TCPS_ESTABLISHED) { 4288 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4289 tcp->tcp_client_errno = EWOULDBLOCK; 4290 } else if (tcp->tcp_client_errno == 0) { 4291 4292 ASSERT(tcp->tcp_linger_tid == 0); 4293 4294 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4295 tcp_close_linger_timeout, 4296 tcp->tcp_lingertime * hz); 4297 4298 /* tcp_close_linger_timeout will finish close */ 4299 if (tcp->tcp_linger_tid == 0) 4300 tcp->tcp_client_errno = ENOSR; 4301 else 4302 return; 4303 } 4304 4305 /* 4306 * Check if we need to detach or just close 4307 * the instance. 4308 */ 4309 if (tcp->tcp_state <= TCPS_LISTEN) 4310 break; 4311 } 4312 4313 /* 4314 * Make sure that no other thread will access the tcp_rq of 4315 * this instance (through lookups etc.) as tcp_rq will go 4316 * away shortly. 4317 */ 4318 tcp_acceptor_hash_remove(tcp); 4319 4320 mutex_enter(&tcp->tcp_non_sq_lock); 4321 if (tcp->tcp_flow_stopped) { 4322 tcp_clrqfull(tcp); 4323 } 4324 mutex_exit(&tcp->tcp_non_sq_lock); 4325 4326 if (tcp->tcp_timer_tid != 0) { 4327 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4328 tcp->tcp_timer_tid = 0; 4329 } 4330 /* 4331 * Need to cancel those timers which will not be used when 4332 * TCP is detached. This has to be done before the tcp_wq 4333 * is set to the global queue. 4334 */ 4335 tcp_timers_stop(tcp); 4336 4337 tcp->tcp_detached = B_TRUE; 4338 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4339 tcp_time_wait_append(tcp); 4340 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4341 ASSERT(connp->conn_ref >= 3); 4342 goto finish; 4343 } 4344 4345 /* 4346 * If delta is zero the timer event wasn't executed and was 4347 * successfully canceled. In this case we need to restart it 4348 * with the minimal delta possible. 4349 */ 4350 if (delta >= 0) 4351 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4352 delta ? delta : 1); 4353 4354 ASSERT(connp->conn_ref >= 3); 4355 goto finish; 4356 } 4357 4358 /* Detach did not complete. Still need to remove q from stream. */ 4359 if (msg) { 4360 if (tcp->tcp_state == TCPS_ESTABLISHED || 4361 tcp->tcp_state == TCPS_CLOSE_WAIT) 4362 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4363 if (tcp->tcp_state == TCPS_SYN_SENT || 4364 tcp->tcp_state == TCPS_SYN_RCVD) 4365 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4366 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4367 } 4368 4369 tcp_closei_local(tcp); 4370 CONN_DEC_REF(connp); 4371 ASSERT(connp->conn_ref >= 2); 4372 4373 finish: 4374 /* 4375 * Although packets are always processed on the correct 4376 * tcp's perimeter and access is serialized via squeue's, 4377 * IP still needs a queue when sending packets in time_wait 4378 * state so use WR(tcps_g_q) till ip_output() can be 4379 * changed to deal with just connp. For read side, we 4380 * could have set tcp_rq to NULL but there are some cases 4381 * in tcp_rput_data() from early days of this code which 4382 * do a putnext without checking if tcp is closed. Those 4383 * need to be identified before both tcp_rq and tcp_wq 4384 * can be set to NULL and tcps_g_q can disappear forever. 4385 */ 4386 mutex_enter(&tcp->tcp_closelock); 4387 /* 4388 * Don't change the queues in the case of a listener that has 4389 * eagers in its q or q0. It could surprise the eagers. 4390 * Instead wait for the eagers outside the squeue. 4391 */ 4392 if (!tcp->tcp_wait_for_eagers) { 4393 tcp->tcp_detached = B_TRUE; 4394 /* 4395 * When default queue is closing we set tcps_g_q to NULL 4396 * after the close is done. 4397 */ 4398 ASSERT(tcps->tcps_g_q != NULL); 4399 tcp->tcp_rq = tcps->tcps_g_q; 4400 tcp->tcp_wq = WR(tcps->tcps_g_q); 4401 } 4402 4403 /* Signal tcp_close() to finish closing. */ 4404 tcp->tcp_closed = 1; 4405 cv_signal(&tcp->tcp_closecv); 4406 mutex_exit(&tcp->tcp_closelock); 4407 } 4408 4409 4410 /* 4411 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4412 * Some stream heads get upset if they see these later on as anything but NULL. 4413 */ 4414 static void 4415 tcp_close_mpp(mblk_t **mpp) 4416 { 4417 mblk_t *mp; 4418 4419 if ((mp = *mpp) != NULL) { 4420 do { 4421 mp->b_next = NULL; 4422 mp->b_prev = NULL; 4423 } while ((mp = mp->b_cont) != NULL); 4424 4425 mp = *mpp; 4426 *mpp = NULL; 4427 freemsg(mp); 4428 } 4429 } 4430 4431 /* Do detached close. */ 4432 static void 4433 tcp_close_detached(tcp_t *tcp) 4434 { 4435 if (tcp->tcp_fused) 4436 tcp_unfuse(tcp); 4437 4438 /* 4439 * Clustering code serializes TCP disconnect callbacks and 4440 * cluster tcp list walks by blocking a TCP disconnect callback 4441 * if a cluster tcp list walk is in progress. This ensures 4442 * accurate accounting of TCPs in the cluster code even though 4443 * the TCP list walk itself is not atomic. 4444 */ 4445 tcp_closei_local(tcp); 4446 CONN_DEC_REF(tcp->tcp_connp); 4447 } 4448 4449 /* 4450 * Stop all TCP timers, and free the timer mblks if requested. 4451 */ 4452 void 4453 tcp_timers_stop(tcp_t *tcp) 4454 { 4455 if (tcp->tcp_timer_tid != 0) { 4456 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4457 tcp->tcp_timer_tid = 0; 4458 } 4459 if (tcp->tcp_ka_tid != 0) { 4460 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4461 tcp->tcp_ka_tid = 0; 4462 } 4463 if (tcp->tcp_ack_tid != 0) { 4464 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4465 tcp->tcp_ack_tid = 0; 4466 } 4467 if (tcp->tcp_push_tid != 0) { 4468 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4469 tcp->tcp_push_tid = 0; 4470 } 4471 } 4472 4473 /* 4474 * The tcp_t is going away. Remove it from all lists and set it 4475 * to TCPS_CLOSED. The freeing up of memory is deferred until 4476 * tcp_inactive. This is needed since a thread in tcp_rput might have 4477 * done a CONN_INC_REF on this structure before it was removed from the 4478 * hashes. 4479 */ 4480 static void 4481 tcp_closei_local(tcp_t *tcp) 4482 { 4483 ire_t *ire; 4484 conn_t *connp = tcp->tcp_connp; 4485 tcp_stack_t *tcps = tcp->tcp_tcps; 4486 4487 if (!TCP_IS_SOCKET(tcp)) 4488 tcp_acceptor_hash_remove(tcp); 4489 4490 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4491 tcp->tcp_ibsegs = 0; 4492 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4493 tcp->tcp_obsegs = 0; 4494 4495 /* 4496 * If we are an eager connection hanging off a listener that 4497 * hasn't formally accepted the connection yet, get off his 4498 * list and blow off any data that we have accumulated. 4499 */ 4500 if (tcp->tcp_listener != NULL) { 4501 tcp_t *listener = tcp->tcp_listener; 4502 mutex_enter(&listener->tcp_eager_lock); 4503 /* 4504 * tcp_tconnind_started == B_TRUE means that the 4505 * conn_ind has already gone to listener. At 4506 * this point, eager will be closed but we 4507 * leave it in listeners eager list so that 4508 * if listener decides to close without doing 4509 * accept, we can clean this up. In tcp_wput_accept 4510 * we take care of the case of accept on closed 4511 * eager. 4512 */ 4513 if (!tcp->tcp_tconnind_started) { 4514 tcp_eager_unlink(tcp); 4515 mutex_exit(&listener->tcp_eager_lock); 4516 /* 4517 * We don't want to have any pointers to the 4518 * listener queue, after we have released our 4519 * reference on the listener 4520 */ 4521 ASSERT(tcps->tcps_g_q != NULL); 4522 tcp->tcp_rq = tcps->tcps_g_q; 4523 tcp->tcp_wq = WR(tcps->tcps_g_q); 4524 CONN_DEC_REF(listener->tcp_connp); 4525 } else { 4526 mutex_exit(&listener->tcp_eager_lock); 4527 } 4528 } 4529 4530 /* Stop all the timers */ 4531 tcp_timers_stop(tcp); 4532 4533 if (tcp->tcp_state == TCPS_LISTEN) { 4534 if (tcp->tcp_ip_addr_cache) { 4535 kmem_free((void *)tcp->tcp_ip_addr_cache, 4536 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4537 tcp->tcp_ip_addr_cache = NULL; 4538 } 4539 } 4540 mutex_enter(&tcp->tcp_non_sq_lock); 4541 if (tcp->tcp_flow_stopped) 4542 tcp_clrqfull(tcp); 4543 mutex_exit(&tcp->tcp_non_sq_lock); 4544 4545 tcp_bind_hash_remove(tcp); 4546 /* 4547 * If the tcp_time_wait_collector (which runs outside the squeue) 4548 * is trying to remove this tcp from the time wait list, we will 4549 * block in tcp_time_wait_remove while trying to acquire the 4550 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4551 * requires the ipcl_hash_remove to be ordered after the 4552 * tcp_time_wait_remove for the refcnt checks to work correctly. 4553 */ 4554 if (tcp->tcp_state == TCPS_TIME_WAIT) 4555 (void) tcp_time_wait_remove(tcp, NULL); 4556 CL_INET_DISCONNECT(tcp); 4557 ipcl_hash_remove(connp); 4558 4559 /* 4560 * Delete the cached ire in conn_ire_cache and also mark 4561 * the conn as CONDEMNED 4562 */ 4563 mutex_enter(&connp->conn_lock); 4564 connp->conn_state_flags |= CONN_CONDEMNED; 4565 ire = connp->conn_ire_cache; 4566 connp->conn_ire_cache = NULL; 4567 mutex_exit(&connp->conn_lock); 4568 if (ire != NULL) 4569 IRE_REFRELE_NOTR(ire); 4570 4571 /* Need to cleanup any pending ioctls */ 4572 ASSERT(tcp->tcp_time_wait_next == NULL); 4573 ASSERT(tcp->tcp_time_wait_prev == NULL); 4574 ASSERT(tcp->tcp_time_wait_expire == 0); 4575 tcp->tcp_state = TCPS_CLOSED; 4576 4577 /* Release any SSL context */ 4578 if (tcp->tcp_kssl_ent != NULL) { 4579 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4580 tcp->tcp_kssl_ent = NULL; 4581 } 4582 if (tcp->tcp_kssl_ctx != NULL) { 4583 kssl_release_ctx(tcp->tcp_kssl_ctx); 4584 tcp->tcp_kssl_ctx = NULL; 4585 } 4586 tcp->tcp_kssl_pending = B_FALSE; 4587 4588 tcp_ipsec_cleanup(tcp); 4589 } 4590 4591 /* 4592 * tcp is dying (called from ipcl_conn_destroy and error cases). 4593 * Free the tcp_t in either case. 4594 */ 4595 void 4596 tcp_free(tcp_t *tcp) 4597 { 4598 mblk_t *mp; 4599 ip6_pkt_t *ipp; 4600 4601 ASSERT(tcp != NULL); 4602 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4603 4604 tcp->tcp_rq = NULL; 4605 tcp->tcp_wq = NULL; 4606 4607 tcp_close_mpp(&tcp->tcp_xmit_head); 4608 tcp_close_mpp(&tcp->tcp_reass_head); 4609 if (tcp->tcp_rcv_list != NULL) { 4610 /* Free b_next chain */ 4611 tcp_close_mpp(&tcp->tcp_rcv_list); 4612 } 4613 if ((mp = tcp->tcp_urp_mp) != NULL) { 4614 freemsg(mp); 4615 } 4616 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4617 freemsg(mp); 4618 } 4619 4620 if (tcp->tcp_fused_sigurg_mp != NULL) { 4621 freeb(tcp->tcp_fused_sigurg_mp); 4622 tcp->tcp_fused_sigurg_mp = NULL; 4623 } 4624 4625 if (tcp->tcp_sack_info != NULL) { 4626 if (tcp->tcp_notsack_list != NULL) { 4627 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4628 } 4629 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4630 } 4631 4632 if (tcp->tcp_hopopts != NULL) { 4633 mi_free(tcp->tcp_hopopts); 4634 tcp->tcp_hopopts = NULL; 4635 tcp->tcp_hopoptslen = 0; 4636 } 4637 ASSERT(tcp->tcp_hopoptslen == 0); 4638 if (tcp->tcp_dstopts != NULL) { 4639 mi_free(tcp->tcp_dstopts); 4640 tcp->tcp_dstopts = NULL; 4641 tcp->tcp_dstoptslen = 0; 4642 } 4643 ASSERT(tcp->tcp_dstoptslen == 0); 4644 if (tcp->tcp_rtdstopts != NULL) { 4645 mi_free(tcp->tcp_rtdstopts); 4646 tcp->tcp_rtdstopts = NULL; 4647 tcp->tcp_rtdstoptslen = 0; 4648 } 4649 ASSERT(tcp->tcp_rtdstoptslen == 0); 4650 if (tcp->tcp_rthdr != NULL) { 4651 mi_free(tcp->tcp_rthdr); 4652 tcp->tcp_rthdr = NULL; 4653 tcp->tcp_rthdrlen = 0; 4654 } 4655 ASSERT(tcp->tcp_rthdrlen == 0); 4656 4657 ipp = &tcp->tcp_sticky_ipp; 4658 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4659 IPPF_RTHDR)) 4660 ip6_pkt_free(ipp); 4661 4662 /* 4663 * Free memory associated with the tcp/ip header template. 4664 */ 4665 4666 if (tcp->tcp_iphc != NULL) 4667 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4668 4669 /* 4670 * Following is really a blowing away a union. 4671 * It happens to have exactly two members of identical size 4672 * the following code is enough. 4673 */ 4674 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4675 } 4676 4677 4678 /* 4679 * Put a connection confirmation message upstream built from the 4680 * address information within 'iph' and 'tcph'. Report our success or failure. 4681 */ 4682 static boolean_t 4683 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4684 mblk_t **defermp) 4685 { 4686 sin_t sin; 4687 sin6_t sin6; 4688 mblk_t *mp; 4689 char *optp = NULL; 4690 int optlen = 0; 4691 cred_t *cr; 4692 4693 if (defermp != NULL) 4694 *defermp = NULL; 4695 4696 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4697 /* 4698 * Return in T_CONN_CON results of option negotiation through 4699 * the T_CONN_REQ. Note: If there is an real end-to-end option 4700 * negotiation, then what is received from remote end needs 4701 * to be taken into account but there is no such thing (yet?) 4702 * in our TCP/IP. 4703 * Note: We do not use mi_offset_param() here as 4704 * tcp_opts_conn_req contents do not directly come from 4705 * an application and are either generated in kernel or 4706 * from user input that was already verified. 4707 */ 4708 mp = tcp->tcp_conn.tcp_opts_conn_req; 4709 optp = (char *)(mp->b_rptr + 4710 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4711 optlen = (int) 4712 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4713 } 4714 4715 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4716 ipha_t *ipha = (ipha_t *)iphdr; 4717 4718 /* packet is IPv4 */ 4719 if (tcp->tcp_family == AF_INET) { 4720 sin = sin_null; 4721 sin.sin_addr.s_addr = ipha->ipha_src; 4722 sin.sin_port = *(uint16_t *)tcph->th_lport; 4723 sin.sin_family = AF_INET; 4724 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4725 (int)sizeof (sin_t), optp, optlen); 4726 } else { 4727 sin6 = sin6_null; 4728 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4729 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4730 sin6.sin6_family = AF_INET6; 4731 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4732 (int)sizeof (sin6_t), optp, optlen); 4733 4734 } 4735 } else { 4736 ip6_t *ip6h = (ip6_t *)iphdr; 4737 4738 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4739 ASSERT(tcp->tcp_family == AF_INET6); 4740 sin6 = sin6_null; 4741 sin6.sin6_addr = ip6h->ip6_src; 4742 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4743 sin6.sin6_family = AF_INET6; 4744 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4745 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4746 (int)sizeof (sin6_t), optp, optlen); 4747 } 4748 4749 if (!mp) 4750 return (B_FALSE); 4751 4752 if ((cr = DB_CRED(idmp)) != NULL) { 4753 mblk_setcred(mp, cr); 4754 DB_CPID(mp) = DB_CPID(idmp); 4755 } 4756 4757 if (defermp == NULL) 4758 putnext(tcp->tcp_rq, mp); 4759 else 4760 *defermp = mp; 4761 4762 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4763 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4764 return (B_TRUE); 4765 } 4766 4767 /* 4768 * Defense for the SYN attack - 4769 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4770 * one from the list of droppable eagers. This list is a subset of q0. 4771 * see comments before the definition of MAKE_DROPPABLE(). 4772 * 2. Don't drop a SYN request before its first timeout. This gives every 4773 * request at least til the first timeout to complete its 3-way handshake. 4774 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4775 * requests currently on the queue that has timed out. This will be used 4776 * as an indicator of whether an attack is under way, so that appropriate 4777 * actions can be taken. (It's incremented in tcp_timer() and decremented 4778 * either when eager goes into ESTABLISHED, or gets freed up.) 4779 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4780 * # of timeout drops back to <= q0len/32 => SYN alert off 4781 */ 4782 static boolean_t 4783 tcp_drop_q0(tcp_t *tcp) 4784 { 4785 tcp_t *eager; 4786 mblk_t *mp; 4787 tcp_stack_t *tcps = tcp->tcp_tcps; 4788 4789 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4790 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4791 4792 /* Pick oldest eager from the list of droppable eagers */ 4793 eager = tcp->tcp_eager_prev_drop_q0; 4794 4795 /* If list is empty. return B_FALSE */ 4796 if (eager == tcp) { 4797 return (B_FALSE); 4798 } 4799 4800 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4801 if ((mp = allocb(0, BPRI_HI)) == NULL) 4802 return (B_FALSE); 4803 4804 /* 4805 * Take this eager out from the list of droppable eagers since we are 4806 * going to drop it. 4807 */ 4808 MAKE_UNDROPPABLE(eager); 4809 4810 if (tcp->tcp_debug) { 4811 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4812 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4813 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4814 tcp->tcp_conn_req_cnt_q0, 4815 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4816 } 4817 4818 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4819 4820 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4821 CONN_INC_REF(eager->tcp_connp); 4822 4823 /* Mark the IRE created for this SYN request temporary */ 4824 tcp_ip_ire_mark_advice(eager); 4825 squeue_fill(eager->tcp_connp->conn_sqp, mp, 4826 tcp_clean_death_wrapper, eager->tcp_connp, SQTAG_TCP_DROP_Q0); 4827 4828 return (B_TRUE); 4829 } 4830 4831 int 4832 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4833 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4834 { 4835 tcp_t *ltcp = lconnp->conn_tcp; 4836 tcp_t *tcp = connp->conn_tcp; 4837 mblk_t *tpi_mp; 4838 ipha_t *ipha; 4839 ip6_t *ip6h; 4840 sin6_t sin6; 4841 in6_addr_t v6dst; 4842 int err; 4843 int ifindex = 0; 4844 cred_t *cr; 4845 tcp_stack_t *tcps = tcp->tcp_tcps; 4846 4847 if (ipvers == IPV4_VERSION) { 4848 ipha = (ipha_t *)mp->b_rptr; 4849 4850 connp->conn_send = ip_output; 4851 connp->conn_recv = tcp_input; 4852 4853 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4854 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4855 4856 sin6 = sin6_null; 4857 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4858 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4859 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4860 sin6.sin6_family = AF_INET6; 4861 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4862 lconnp->conn_zoneid, tcps->tcps_netstack); 4863 if (tcp->tcp_recvdstaddr) { 4864 sin6_t sin6d; 4865 4866 sin6d = sin6_null; 4867 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4868 &sin6d.sin6_addr); 4869 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4870 sin6d.sin6_family = AF_INET; 4871 tpi_mp = mi_tpi_extconn_ind(NULL, 4872 (char *)&sin6d, sizeof (sin6_t), 4873 (char *)&tcp, 4874 (t_scalar_t)sizeof (intptr_t), 4875 (char *)&sin6d, sizeof (sin6_t), 4876 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4877 } else { 4878 tpi_mp = mi_tpi_conn_ind(NULL, 4879 (char *)&sin6, sizeof (sin6_t), 4880 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4881 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4882 } 4883 } else { 4884 ip6h = (ip6_t *)mp->b_rptr; 4885 4886 connp->conn_send = ip_output_v6; 4887 connp->conn_recv = tcp_input; 4888 4889 connp->conn_srcv6 = ip6h->ip6_dst; 4890 connp->conn_remv6 = ip6h->ip6_src; 4891 4892 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4893 ifindex = (int)DB_CKSUMSTUFF(mp); 4894 DB_CKSUMSTUFF(mp) = 0; 4895 4896 sin6 = sin6_null; 4897 sin6.sin6_addr = ip6h->ip6_src; 4898 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4899 sin6.sin6_family = AF_INET6; 4900 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4901 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4902 lconnp->conn_zoneid, tcps->tcps_netstack); 4903 4904 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4905 /* Pass up the scope_id of remote addr */ 4906 sin6.sin6_scope_id = ifindex; 4907 } else { 4908 sin6.sin6_scope_id = 0; 4909 } 4910 if (tcp->tcp_recvdstaddr) { 4911 sin6_t sin6d; 4912 4913 sin6d = sin6_null; 4914 sin6.sin6_addr = ip6h->ip6_dst; 4915 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4916 sin6d.sin6_family = AF_INET; 4917 tpi_mp = mi_tpi_extconn_ind(NULL, 4918 (char *)&sin6d, sizeof (sin6_t), 4919 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4920 (char *)&sin6d, sizeof (sin6_t), 4921 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4922 } else { 4923 tpi_mp = mi_tpi_conn_ind(NULL, 4924 (char *)&sin6, sizeof (sin6_t), 4925 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4926 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4927 } 4928 } 4929 4930 if (tpi_mp == NULL) 4931 return (ENOMEM); 4932 4933 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4934 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4935 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4936 connp->conn_fully_bound = B_FALSE; 4937 4938 /* Inherit information from the "parent" */ 4939 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4940 tcp->tcp_family = ltcp->tcp_family; 4941 tcp->tcp_wq = ltcp->tcp_wq; 4942 tcp->tcp_rq = ltcp->tcp_rq; 4943 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4944 tcp->tcp_detached = B_TRUE; 4945 if ((err = tcp_init_values(tcp)) != 0) { 4946 freemsg(tpi_mp); 4947 return (err); 4948 } 4949 4950 if (ipvers == IPV4_VERSION) { 4951 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4952 freemsg(tpi_mp); 4953 return (err); 4954 } 4955 ASSERT(tcp->tcp_ipha != NULL); 4956 } else { 4957 /* ifindex must be already set */ 4958 ASSERT(ifindex != 0); 4959 4960 if (ltcp->tcp_bound_if != 0) { 4961 /* 4962 * Set newtcp's bound_if equal to 4963 * listener's value. If ifindex is 4964 * not the same as ltcp->tcp_bound_if, 4965 * it must be a packet for the ipmp group 4966 * of interfaces 4967 */ 4968 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4969 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4970 tcp->tcp_bound_if = ifindex; 4971 } 4972 4973 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4974 tcp->tcp_recvifindex = 0; 4975 tcp->tcp_recvhops = 0xffffffffU; 4976 ASSERT(tcp->tcp_ip6h != NULL); 4977 } 4978 4979 tcp->tcp_lport = ltcp->tcp_lport; 4980 4981 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4982 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4983 /* 4984 * Listener had options of some sort; eager inherits. 4985 * Free up the eager template and allocate one 4986 * of the right size. 4987 */ 4988 if (tcp->tcp_hdr_grown) { 4989 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4990 } else { 4991 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4992 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4993 } 4994 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4995 KM_NOSLEEP); 4996 if (tcp->tcp_iphc == NULL) { 4997 tcp->tcp_iphc_len = 0; 4998 freemsg(tpi_mp); 4999 return (ENOMEM); 5000 } 5001 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 5002 tcp->tcp_hdr_grown = B_TRUE; 5003 } 5004 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5005 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5006 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5007 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 5008 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 5009 5010 /* 5011 * Copy the IP+TCP header template from listener to eager 5012 */ 5013 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5014 if (tcp->tcp_ipversion == IPV6_VERSION) { 5015 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 5016 IPPROTO_RAW) { 5017 tcp->tcp_ip6h = 5018 (ip6_t *)(tcp->tcp_iphc + 5019 sizeof (ip6i_t)); 5020 } else { 5021 tcp->tcp_ip6h = 5022 (ip6_t *)(tcp->tcp_iphc); 5023 } 5024 tcp->tcp_ipha = NULL; 5025 } else { 5026 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5027 tcp->tcp_ip6h = NULL; 5028 } 5029 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5030 tcp->tcp_ip_hdr_len); 5031 } else { 5032 /* 5033 * only valid case when ipversion of listener and 5034 * eager differ is when listener is IPv6 and 5035 * eager is IPv4. 5036 * Eager header template has been initialized to the 5037 * maximum v4 header sizes, which includes space for 5038 * TCP and IP options. 5039 */ 5040 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 5041 (tcp->tcp_ipversion == IPV4_VERSION)); 5042 ASSERT(tcp->tcp_iphc_len >= 5043 TCP_MAX_COMBINED_HEADER_LENGTH); 5044 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5045 /* copy IP header fields individually */ 5046 tcp->tcp_ipha->ipha_ttl = 5047 ltcp->tcp_ip6h->ip6_hops; 5048 bcopy(ltcp->tcp_tcph->th_lport, 5049 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 5050 } 5051 5052 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5053 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 5054 sizeof (in_port_t)); 5055 5056 if (ltcp->tcp_lport == 0) { 5057 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 5058 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 5059 sizeof (in_port_t)); 5060 } 5061 5062 if (tcp->tcp_ipversion == IPV4_VERSION) { 5063 ASSERT(ipha != NULL); 5064 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5065 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5066 5067 /* Source routing option copyover (reverse it) */ 5068 if (tcps->tcps_rev_src_routes) 5069 tcp_opt_reverse(tcp, ipha); 5070 } else { 5071 ASSERT(ip6h != NULL); 5072 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 5073 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 5074 } 5075 5076 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5077 ASSERT(!tcp->tcp_tconnind_started); 5078 /* 5079 * If the SYN contains a credential, it's a loopback packet; attach 5080 * the credential to the TPI message. 5081 */ 5082 if ((cr = DB_CRED(idmp)) != NULL) { 5083 mblk_setcred(tpi_mp, cr); 5084 DB_CPID(tpi_mp) = DB_CPID(idmp); 5085 } 5086 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5087 5088 /* Inherit the listener's SSL protection state */ 5089 5090 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5091 kssl_hold_ent(tcp->tcp_kssl_ent); 5092 tcp->tcp_kssl_pending = B_TRUE; 5093 } 5094 5095 return (0); 5096 } 5097 5098 5099 int 5100 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 5101 tcph_t *tcph, mblk_t *idmp) 5102 { 5103 tcp_t *ltcp = lconnp->conn_tcp; 5104 tcp_t *tcp = connp->conn_tcp; 5105 sin_t sin; 5106 mblk_t *tpi_mp = NULL; 5107 int err; 5108 cred_t *cr; 5109 tcp_stack_t *tcps = tcp->tcp_tcps; 5110 5111 sin = sin_null; 5112 sin.sin_addr.s_addr = ipha->ipha_src; 5113 sin.sin_port = *(uint16_t *)tcph->th_lport; 5114 sin.sin_family = AF_INET; 5115 if (ltcp->tcp_recvdstaddr) { 5116 sin_t sind; 5117 5118 sind = sin_null; 5119 sind.sin_addr.s_addr = ipha->ipha_dst; 5120 sind.sin_port = *(uint16_t *)tcph->th_fport; 5121 sind.sin_family = AF_INET; 5122 tpi_mp = mi_tpi_extconn_ind(NULL, 5123 (char *)&sind, sizeof (sin_t), (char *)&tcp, 5124 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 5125 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5126 } else { 5127 tpi_mp = mi_tpi_conn_ind(NULL, 5128 (char *)&sin, sizeof (sin_t), 5129 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 5130 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5131 } 5132 5133 if (tpi_mp == NULL) { 5134 return (ENOMEM); 5135 } 5136 5137 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 5138 connp->conn_send = ip_output; 5139 connp->conn_recv = tcp_input; 5140 connp->conn_fully_bound = B_FALSE; 5141 5142 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 5143 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 5144 connp->conn_fport = *(uint16_t *)tcph->th_lport; 5145 connp->conn_lport = *(uint16_t *)tcph->th_fport; 5146 5147 /* Inherit information from the "parent" */ 5148 tcp->tcp_ipversion = ltcp->tcp_ipversion; 5149 tcp->tcp_family = ltcp->tcp_family; 5150 tcp->tcp_wq = ltcp->tcp_wq; 5151 tcp->tcp_rq = ltcp->tcp_rq; 5152 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 5153 tcp->tcp_detached = B_TRUE; 5154 if ((err = tcp_init_values(tcp)) != 0) { 5155 freemsg(tpi_mp); 5156 return (err); 5157 } 5158 5159 /* 5160 * Let's make sure that eager tcp template has enough space to 5161 * copy IPv4 listener's tcp template. Since the conn_t structure is 5162 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 5163 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 5164 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 5165 * extension headers or with ip6i_t struct). Note that bcopy() below 5166 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 5167 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 5168 */ 5169 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 5170 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 5171 5172 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5173 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5174 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5175 tcp->tcp_ttl = ltcp->tcp_ttl; 5176 tcp->tcp_tos = ltcp->tcp_tos; 5177 5178 /* Copy the IP+TCP header template from listener to eager */ 5179 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5180 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5181 tcp->tcp_ip6h = NULL; 5182 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5183 tcp->tcp_ip_hdr_len); 5184 5185 /* Initialize the IP addresses and Ports */ 5186 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5187 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5188 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5189 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 5190 5191 /* Source routing option copyover (reverse it) */ 5192 if (tcps->tcps_rev_src_routes) 5193 tcp_opt_reverse(tcp, ipha); 5194 5195 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5196 ASSERT(!tcp->tcp_tconnind_started); 5197 5198 /* 5199 * If the SYN contains a credential, it's a loopback packet; attach 5200 * the credential to the TPI message. 5201 */ 5202 if ((cr = DB_CRED(idmp)) != NULL) { 5203 mblk_setcred(tpi_mp, cr); 5204 DB_CPID(tpi_mp) = DB_CPID(idmp); 5205 } 5206 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5207 5208 /* Inherit the listener's SSL protection state */ 5209 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5210 kssl_hold_ent(tcp->tcp_kssl_ent); 5211 tcp->tcp_kssl_pending = B_TRUE; 5212 } 5213 5214 return (0); 5215 } 5216 5217 /* 5218 * sets up conn for ipsec. 5219 * if the first mblk is M_CTL it is consumed and mpp is updated. 5220 * in case of error mpp is freed. 5221 */ 5222 conn_t * 5223 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 5224 { 5225 conn_t *connp = tcp->tcp_connp; 5226 conn_t *econnp; 5227 squeue_t *new_sqp; 5228 mblk_t *first_mp = *mpp; 5229 mblk_t *mp = *mpp; 5230 boolean_t mctl_present = B_FALSE; 5231 uint_t ipvers; 5232 5233 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 5234 if (econnp == NULL) { 5235 freemsg(first_mp); 5236 return (NULL); 5237 } 5238 if (DB_TYPE(mp) == M_CTL) { 5239 if (mp->b_cont == NULL || 5240 mp->b_cont->b_datap->db_type != M_DATA) { 5241 freemsg(first_mp); 5242 return (NULL); 5243 } 5244 mp = mp->b_cont; 5245 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 5246 freemsg(first_mp); 5247 return (NULL); 5248 } 5249 5250 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5251 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5252 mctl_present = B_TRUE; 5253 } else { 5254 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 5255 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5256 } 5257 5258 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5259 DB_CKSUMSTART(mp) = 0; 5260 5261 ASSERT(OK_32PTR(mp->b_rptr)); 5262 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5263 if (ipvers == IPV4_VERSION) { 5264 uint16_t *up; 5265 uint32_t ports; 5266 ipha_t *ipha; 5267 5268 ipha = (ipha_t *)mp->b_rptr; 5269 up = (uint16_t *)((uchar_t *)ipha + 5270 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5271 ports = *(uint32_t *)up; 5272 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5273 ipha->ipha_dst, ipha->ipha_src, ports); 5274 } else { 5275 uint16_t *up; 5276 uint32_t ports; 5277 uint16_t ip_hdr_len; 5278 uint8_t *nexthdrp; 5279 ip6_t *ip6h; 5280 tcph_t *tcph; 5281 5282 ip6h = (ip6_t *)mp->b_rptr; 5283 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5284 ip_hdr_len = IPV6_HDR_LEN; 5285 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5286 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5287 CONN_DEC_REF(econnp); 5288 freemsg(first_mp); 5289 return (NULL); 5290 } 5291 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5292 up = (uint16_t *)tcph->th_lport; 5293 ports = *(uint32_t *)up; 5294 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5295 ip6h->ip6_dst, ip6h->ip6_src, ports); 5296 } 5297 5298 /* 5299 * The caller already ensured that there is a sqp present. 5300 */ 5301 econnp->conn_sqp = new_sqp; 5302 5303 if (connp->conn_policy != NULL) { 5304 ipsec_in_t *ii; 5305 ii = (ipsec_in_t *)(first_mp->b_rptr); 5306 ASSERT(ii->ipsec_in_policy == NULL); 5307 IPPH_REFHOLD(connp->conn_policy); 5308 ii->ipsec_in_policy = connp->conn_policy; 5309 5310 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5311 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5312 CONN_DEC_REF(econnp); 5313 freemsg(first_mp); 5314 return (NULL); 5315 } 5316 } 5317 5318 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5319 CONN_DEC_REF(econnp); 5320 freemsg(first_mp); 5321 return (NULL); 5322 } 5323 5324 /* 5325 * If we know we have some policy, pass the "IPSEC" 5326 * options size TCP uses this adjust the MSS. 5327 */ 5328 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5329 if (mctl_present) { 5330 freeb(first_mp); 5331 *mpp = mp; 5332 } 5333 5334 return (econnp); 5335 } 5336 5337 /* 5338 * tcp_get_conn/tcp_free_conn 5339 * 5340 * tcp_get_conn is used to get a clean tcp connection structure. 5341 * It tries to reuse the connections put on the freelist by the 5342 * time_wait_collector failing which it goes to kmem_cache. This 5343 * way has two benefits compared to just allocating from and 5344 * freeing to kmem_cache. 5345 * 1) The time_wait_collector can free (which includes the cleanup) 5346 * outside the squeue. So when the interrupt comes, we have a clean 5347 * connection sitting in the freelist. Obviously, this buys us 5348 * performance. 5349 * 5350 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5351 * has multiple disadvantages - tying up the squeue during alloc, and the 5352 * fact that IPSec policy initialization has to happen here which 5353 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5354 * But allocating the conn/tcp in IP land is also not the best since 5355 * we can't check the 'q' and 'q0' which are protected by squeue and 5356 * blindly allocate memory which might have to be freed here if we are 5357 * not allowed to accept the connection. By using the freelist and 5358 * putting the conn/tcp back in freelist, we don't pay a penalty for 5359 * allocating memory without checking 'q/q0' and freeing it if we can't 5360 * accept the connection. 5361 * 5362 * Care should be taken to put the conn back in the same squeue's freelist 5363 * from which it was allocated. Best results are obtained if conn is 5364 * allocated from listener's squeue and freed to the same. Time wait 5365 * collector will free up the freelist is the connection ends up sitting 5366 * there for too long. 5367 */ 5368 void * 5369 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5370 { 5371 tcp_t *tcp = NULL; 5372 conn_t *connp = NULL; 5373 squeue_t *sqp = (squeue_t *)arg; 5374 tcp_squeue_priv_t *tcp_time_wait; 5375 netstack_t *ns; 5376 5377 tcp_time_wait = 5378 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5379 5380 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5381 tcp = tcp_time_wait->tcp_free_list; 5382 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5383 if (tcp != NULL) { 5384 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5385 tcp_time_wait->tcp_free_list_cnt--; 5386 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5387 tcp->tcp_time_wait_next = NULL; 5388 connp = tcp->tcp_connp; 5389 connp->conn_flags |= IPCL_REUSED; 5390 5391 ASSERT(tcp->tcp_tcps == NULL); 5392 ASSERT(connp->conn_netstack == NULL); 5393 ASSERT(tcp->tcp_rsrv_mp != NULL); 5394 ns = tcps->tcps_netstack; 5395 netstack_hold(ns); 5396 connp->conn_netstack = ns; 5397 tcp->tcp_tcps = tcps; 5398 TCPS_REFHOLD(tcps); 5399 ipcl_globalhash_insert(connp); 5400 return ((void *)connp); 5401 } 5402 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5403 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5404 tcps->tcps_netstack)) == NULL) 5405 return (NULL); 5406 tcp = connp->conn_tcp; 5407 /* 5408 * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed 5409 * until this conn_t/tcp_t is freed at ipcl_conn_destroy(). 5410 */ 5411 if ((tcp->tcp_rsrv_mp = allocb(0, BPRI_HI)) == NULL) { 5412 ipcl_conn_destroy(connp); 5413 return (NULL); 5414 } 5415 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL); 5416 tcp->tcp_tcps = tcps; 5417 TCPS_REFHOLD(tcps); 5418 5419 return ((void *)connp); 5420 } 5421 5422 /* 5423 * Update the cached label for the given tcp_t. This should be called once per 5424 * connection, and before any packets are sent or tcp_process_options is 5425 * invoked. Returns B_FALSE if the correct label could not be constructed. 5426 */ 5427 static boolean_t 5428 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5429 { 5430 conn_t *connp = tcp->tcp_connp; 5431 5432 if (tcp->tcp_ipversion == IPV4_VERSION) { 5433 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5434 int added; 5435 5436 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5437 connp->conn_mac_exempt, 5438 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5439 return (B_FALSE); 5440 5441 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5442 if (added == -1) 5443 return (B_FALSE); 5444 tcp->tcp_hdr_len += added; 5445 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5446 tcp->tcp_ip_hdr_len += added; 5447 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5448 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5449 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5450 tcp->tcp_hdr_len); 5451 if (added == -1) 5452 return (B_FALSE); 5453 tcp->tcp_hdr_len += added; 5454 tcp->tcp_tcph = (tcph_t *) 5455 ((uchar_t *)tcp->tcp_tcph + added); 5456 tcp->tcp_ip_hdr_len += added; 5457 } 5458 } else { 5459 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5460 5461 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5462 connp->conn_mac_exempt, 5463 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5464 return (B_FALSE); 5465 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5466 &tcp->tcp_label_len, optbuf) != 0) 5467 return (B_FALSE); 5468 if (tcp_build_hdrs(tcp->tcp_rq, tcp) != 0) 5469 return (B_FALSE); 5470 } 5471 5472 connp->conn_ulp_labeled = 1; 5473 5474 return (B_TRUE); 5475 } 5476 5477 /* BEGIN CSTYLED */ 5478 /* 5479 * 5480 * The sockfs ACCEPT path: 5481 * ======================= 5482 * 5483 * The eager is now established in its own perimeter as soon as SYN is 5484 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5485 * completes the accept processing on the acceptor STREAM. The sending 5486 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5487 * listener but a TLI/XTI listener completes the accept processing 5488 * on the listener perimeter. 5489 * 5490 * Common control flow for 3 way handshake: 5491 * ---------------------------------------- 5492 * 5493 * incoming SYN (listener perimeter) -> tcp_rput_data() 5494 * -> tcp_conn_request() 5495 * 5496 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5497 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5498 * 5499 * Sockfs ACCEPT Path: 5500 * ------------------- 5501 * 5502 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5503 * as STREAM entry point) 5504 * 5505 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5506 * 5507 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5508 * association (we are not behind eager's squeue but sockfs is protecting us 5509 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5510 * is changed to point at tcp_wput(). 5511 * 5512 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5513 * listener (done on listener's perimeter). 5514 * 5515 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5516 * accept. 5517 * 5518 * TLI/XTI client ACCEPT path: 5519 * --------------------------- 5520 * 5521 * soaccept() sends T_CONN_RES on the listener STREAM. 5522 * 5523 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5524 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5525 * 5526 * Locks: 5527 * ====== 5528 * 5529 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5530 * and listeners->tcp_eager_next_q. 5531 * 5532 * Referencing: 5533 * ============ 5534 * 5535 * 1) We start out in tcp_conn_request by eager placing a ref on 5536 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5537 * 5538 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5539 * doing so we place a ref on the eager. This ref is finally dropped at the 5540 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5541 * reference is dropped by the squeue framework. 5542 * 5543 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5544 * 5545 * The reference must be released by the same entity that added the reference 5546 * In the above scheme, the eager is the entity that adds and releases the 5547 * references. Note that tcp_accept_finish executes in the squeue of the eager 5548 * (albeit after it is attached to the acceptor stream). Though 1. executes 5549 * in the listener's squeue, the eager is nascent at this point and the 5550 * reference can be considered to have been added on behalf of the eager. 5551 * 5552 * Eager getting a Reset or listener closing: 5553 * ========================================== 5554 * 5555 * Once the listener and eager are linked, the listener never does the unlink. 5556 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5557 * a message on all eager perimeter. The eager then does the unlink, clears 5558 * any pointers to the listener's queue and drops the reference to the 5559 * listener. The listener waits in tcp_close outside the squeue until its 5560 * refcount has dropped to 1. This ensures that the listener has waited for 5561 * all eagers to clear their association with the listener. 5562 * 5563 * Similarly, if eager decides to go away, it can unlink itself and close. 5564 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5565 * the reference to eager is still valid because of the extra ref we put 5566 * in tcp_send_conn_ind. 5567 * 5568 * Listener can always locate the eager under the protection 5569 * of the listener->tcp_eager_lock, and then do a refhold 5570 * on the eager during the accept processing. 5571 * 5572 * The acceptor stream accesses the eager in the accept processing 5573 * based on the ref placed on eager before sending T_conn_ind. 5574 * The only entity that can negate this refhold is a listener close 5575 * which is mutually exclusive with an active acceptor stream. 5576 * 5577 * Eager's reference on the listener 5578 * =================================== 5579 * 5580 * If the accept happens (even on a closed eager) the eager drops its 5581 * reference on the listener at the start of tcp_accept_finish. If the 5582 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5583 * the reference is dropped in tcp_closei_local. If the listener closes, 5584 * the reference is dropped in tcp_eager_kill. In all cases the reference 5585 * is dropped while executing in the eager's context (squeue). 5586 */ 5587 /* END CSTYLED */ 5588 5589 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5590 5591 /* 5592 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5593 * tcp_rput_data will not see any SYN packets. 5594 */ 5595 /* ARGSUSED */ 5596 void 5597 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5598 { 5599 tcph_t *tcph; 5600 uint32_t seg_seq; 5601 tcp_t *eager; 5602 uint_t ipvers; 5603 ipha_t *ipha; 5604 ip6_t *ip6h; 5605 int err; 5606 conn_t *econnp = NULL; 5607 squeue_t *new_sqp; 5608 mblk_t *mp1; 5609 uint_t ip_hdr_len; 5610 conn_t *connp = (conn_t *)arg; 5611 tcp_t *tcp = connp->conn_tcp; 5612 cred_t *credp; 5613 tcp_stack_t *tcps = tcp->tcp_tcps; 5614 ip_stack_t *ipst; 5615 5616 if (tcp->tcp_state != TCPS_LISTEN) 5617 goto error2; 5618 5619 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5620 5621 mutex_enter(&tcp->tcp_eager_lock); 5622 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5623 mutex_exit(&tcp->tcp_eager_lock); 5624 TCP_STAT(tcps, tcp_listendrop); 5625 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5626 if (tcp->tcp_debug) { 5627 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5628 "tcp_conn_request: listen backlog (max=%d) " 5629 "overflow (%d pending) on %s", 5630 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5631 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5632 } 5633 goto error2; 5634 } 5635 5636 if (tcp->tcp_conn_req_cnt_q0 >= 5637 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5638 /* 5639 * Q0 is full. Drop a pending half-open req from the queue 5640 * to make room for the new SYN req. Also mark the time we 5641 * drop a SYN. 5642 * 5643 * A more aggressive defense against SYN attack will 5644 * be to set the "tcp_syn_defense" flag now. 5645 */ 5646 TCP_STAT(tcps, tcp_listendropq0); 5647 tcp->tcp_last_rcv_lbolt = lbolt64; 5648 if (!tcp_drop_q0(tcp)) { 5649 mutex_exit(&tcp->tcp_eager_lock); 5650 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5651 if (tcp->tcp_debug) { 5652 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5653 "tcp_conn_request: listen half-open queue " 5654 "(max=%d) full (%d pending) on %s", 5655 tcps->tcps_conn_req_max_q0, 5656 tcp->tcp_conn_req_cnt_q0, 5657 tcp_display(tcp, NULL, 5658 DISP_PORT_ONLY)); 5659 } 5660 goto error2; 5661 } 5662 } 5663 mutex_exit(&tcp->tcp_eager_lock); 5664 5665 /* 5666 * IP adds STRUIO_EAGER and ensures that the received packet is 5667 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5668 * link local address. If IPSec is enabled, db_struioflag has 5669 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5670 * otherwise an error case if neither of them is set. 5671 */ 5672 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5673 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5674 DB_CKSUMSTART(mp) = 0; 5675 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5676 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5677 if (econnp == NULL) 5678 goto error2; 5679 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5680 econnp->conn_sqp = new_sqp; 5681 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5682 /* 5683 * mp is updated in tcp_get_ipsec_conn(). 5684 */ 5685 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5686 if (econnp == NULL) { 5687 /* 5688 * mp freed by tcp_get_ipsec_conn. 5689 */ 5690 return; 5691 } 5692 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5693 } else { 5694 goto error2; 5695 } 5696 5697 ASSERT(DB_TYPE(mp) == M_DATA); 5698 5699 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5700 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5701 ASSERT(OK_32PTR(mp->b_rptr)); 5702 if (ipvers == IPV4_VERSION) { 5703 ipha = (ipha_t *)mp->b_rptr; 5704 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5705 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5706 } else { 5707 ip6h = (ip6_t *)mp->b_rptr; 5708 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5709 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5710 } 5711 5712 if (tcp->tcp_family == AF_INET) { 5713 ASSERT(ipvers == IPV4_VERSION); 5714 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5715 } else { 5716 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5717 } 5718 5719 if (err) 5720 goto error3; 5721 5722 eager = econnp->conn_tcp; 5723 5724 /* 5725 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 5726 * will always have that to send up. Otherwise, we need to do 5727 * special handling in case the allocation fails at that time. 5728 */ 5729 ASSERT(eager->tcp_ordrel_mp == NULL); 5730 if ((eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) 5731 goto error3; 5732 5733 /* Inherit various TCP parameters from the listener */ 5734 eager->tcp_naglim = tcp->tcp_naglim; 5735 eager->tcp_first_timer_threshold = 5736 tcp->tcp_first_timer_threshold; 5737 eager->tcp_second_timer_threshold = 5738 tcp->tcp_second_timer_threshold; 5739 5740 eager->tcp_first_ctimer_threshold = 5741 tcp->tcp_first_ctimer_threshold; 5742 eager->tcp_second_ctimer_threshold = 5743 tcp->tcp_second_ctimer_threshold; 5744 5745 /* 5746 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5747 * If it does not, the eager's receive window will be set to the 5748 * listener's receive window later in this function. 5749 */ 5750 eager->tcp_rwnd = 0; 5751 5752 /* 5753 * Inherit listener's tcp_init_cwnd. Need to do this before 5754 * calling tcp_process_options() where tcp_mss_set() is called 5755 * to set the initial cwnd. 5756 */ 5757 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5758 5759 /* 5760 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5761 * zone id before the accept is completed in tcp_wput_accept(). 5762 */ 5763 econnp->conn_zoneid = connp->conn_zoneid; 5764 econnp->conn_allzones = connp->conn_allzones; 5765 5766 /* Copy nexthop information from listener to eager */ 5767 if (connp->conn_nexthop_set) { 5768 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5769 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5770 } 5771 5772 /* 5773 * TSOL: tsol_input_proc() needs the eager's cred before the 5774 * eager is accepted 5775 */ 5776 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5777 crhold(credp); 5778 5779 /* 5780 * If the caller has the process-wide flag set, then default to MAC 5781 * exempt mode. This allows read-down to unlabeled hosts. 5782 */ 5783 if (getpflags(NET_MAC_AWARE, credp) != 0) 5784 econnp->conn_mac_exempt = B_TRUE; 5785 5786 if (is_system_labeled()) { 5787 cred_t *cr; 5788 5789 if (connp->conn_mlp_type != mlptSingle) { 5790 cr = econnp->conn_peercred = DB_CRED(mp); 5791 if (cr != NULL) 5792 crhold(cr); 5793 else 5794 cr = econnp->conn_cred; 5795 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5796 econnp, cred_t *, cr) 5797 } else { 5798 cr = econnp->conn_cred; 5799 DTRACE_PROBE2(syn_accept, conn_t *, 5800 econnp, cred_t *, cr) 5801 } 5802 5803 if (!tcp_update_label(eager, cr)) { 5804 DTRACE_PROBE3( 5805 tx__ip__log__error__connrequest__tcp, 5806 char *, "eager connp(1) label on SYN mp(2) failed", 5807 conn_t *, econnp, mblk_t *, mp); 5808 goto error3; 5809 } 5810 } 5811 5812 eager->tcp_hard_binding = B_TRUE; 5813 5814 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5815 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5816 5817 CL_INET_CONNECT(eager); 5818 5819 /* 5820 * No need to check for multicast destination since ip will only pass 5821 * up multicasts to those that have expressed interest 5822 * TODO: what about rejecting broadcasts? 5823 * Also check that source is not a multicast or broadcast address. 5824 */ 5825 eager->tcp_state = TCPS_SYN_RCVD; 5826 5827 5828 /* 5829 * There should be no ire in the mp as we are being called after 5830 * receiving the SYN. 5831 */ 5832 ASSERT(tcp_ire_mp(mp) == NULL); 5833 5834 /* 5835 * Adapt our mss, ttl, ... according to information provided in IRE. 5836 */ 5837 5838 if (tcp_adapt_ire(eager, NULL) == 0) { 5839 /* Undo the bind_hash_insert */ 5840 tcp_bind_hash_remove(eager); 5841 goto error3; 5842 } 5843 5844 /* Process all TCP options. */ 5845 tcp_process_options(eager, tcph); 5846 5847 /* Is the other end ECN capable? */ 5848 if (tcps->tcps_ecn_permitted >= 1 && 5849 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5850 eager->tcp_ecn_ok = B_TRUE; 5851 } 5852 5853 /* 5854 * listener->tcp_rq->q_hiwat should be the default window size or a 5855 * window size changed via SO_RCVBUF option. First round up the 5856 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5857 * scale option value if needed. Call tcp_rwnd_set() to finish the 5858 * setting. 5859 * 5860 * Note if there is a rpipe metric associated with the remote host, 5861 * we should not inherit receive window size from listener. 5862 */ 5863 eager->tcp_rwnd = MSS_ROUNDUP( 5864 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5865 eager->tcp_rwnd), eager->tcp_mss); 5866 if (eager->tcp_snd_ws_ok) 5867 tcp_set_ws_value(eager); 5868 /* 5869 * Note that this is the only place tcp_rwnd_set() is called for 5870 * accepting a connection. We need to call it here instead of 5871 * after the 3-way handshake because we need to tell the other 5872 * side our rwnd in the SYN-ACK segment. 5873 */ 5874 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5875 5876 /* 5877 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5878 * via soaccept()->soinheritoptions() which essentially applies 5879 * all the listener options to the new STREAM. The options that we 5880 * need to take care of are: 5881 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5882 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5883 * SO_SNDBUF, SO_RCVBUF. 5884 * 5885 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5886 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5887 * tcp_maxpsz_set() gets called later from 5888 * tcp_accept_finish(), the option takes effect. 5889 * 5890 */ 5891 /* Set the TCP options */ 5892 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5893 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5894 eager->tcp_oobinline = tcp->tcp_oobinline; 5895 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5896 eager->tcp_broadcast = tcp->tcp_broadcast; 5897 eager->tcp_useloopback = tcp->tcp_useloopback; 5898 eager->tcp_dontroute = tcp->tcp_dontroute; 5899 eager->tcp_linger = tcp->tcp_linger; 5900 eager->tcp_lingertime = tcp->tcp_lingertime; 5901 if (tcp->tcp_ka_enabled) 5902 eager->tcp_ka_enabled = 1; 5903 5904 /* Set the IP options */ 5905 econnp->conn_broadcast = connp->conn_broadcast; 5906 econnp->conn_loopback = connp->conn_loopback; 5907 econnp->conn_dontroute = connp->conn_dontroute; 5908 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5909 5910 /* Put a ref on the listener for the eager. */ 5911 CONN_INC_REF(connp); 5912 mutex_enter(&tcp->tcp_eager_lock); 5913 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5914 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5915 tcp->tcp_eager_next_q0 = eager; 5916 eager->tcp_eager_prev_q0 = tcp; 5917 5918 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5919 eager->tcp_listener = tcp; 5920 eager->tcp_saved_listener = tcp; 5921 5922 /* 5923 * Tag this detached tcp vector for later retrieval 5924 * by our listener client in tcp_accept(). 5925 */ 5926 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5927 tcp->tcp_conn_req_cnt_q0++; 5928 if (++tcp->tcp_conn_req_seqnum == -1) { 5929 /* 5930 * -1 is "special" and defined in TPI as something 5931 * that should never be used in T_CONN_IND 5932 */ 5933 ++tcp->tcp_conn_req_seqnum; 5934 } 5935 mutex_exit(&tcp->tcp_eager_lock); 5936 5937 if (tcp->tcp_syn_defense) { 5938 /* Don't drop the SYN that comes from a good IP source */ 5939 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5940 if (addr_cache != NULL && eager->tcp_remote == 5941 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5942 eager->tcp_dontdrop = B_TRUE; 5943 } 5944 } 5945 5946 /* 5947 * We need to insert the eager in its own perimeter but as soon 5948 * as we do that, we expose the eager to the classifier and 5949 * should not touch any field outside the eager's perimeter. 5950 * So do all the work necessary before inserting the eager 5951 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5952 * will succeed but undo everything if it fails. 5953 */ 5954 seg_seq = ABE32_TO_U32(tcph->th_seq); 5955 eager->tcp_irs = seg_seq; 5956 eager->tcp_rack = seg_seq; 5957 eager->tcp_rnxt = seg_seq + 1; 5958 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5959 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5960 eager->tcp_state = TCPS_SYN_RCVD; 5961 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5962 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5963 if (mp1 == NULL) { 5964 /* 5965 * Increment the ref count as we are going to 5966 * enqueueing an mp in squeue 5967 */ 5968 CONN_INC_REF(econnp); 5969 goto error; 5970 } 5971 DB_CPID(mp1) = tcp->tcp_cpid; 5972 eager->tcp_cpid = tcp->tcp_cpid; 5973 eager->tcp_open_time = lbolt64; 5974 5975 /* 5976 * We need to start the rto timer. In normal case, we start 5977 * the timer after sending the packet on the wire (or at 5978 * least believing that packet was sent by waiting for 5979 * CALL_IP_WPUT() to return). Since this is the first packet 5980 * being sent on the wire for the eager, our initial tcp_rto 5981 * is at least tcp_rexmit_interval_min which is a fairly 5982 * large value to allow the algorithm to adjust slowly to large 5983 * fluctuations of RTT during first few transmissions. 5984 * 5985 * Starting the timer first and then sending the packet in this 5986 * case shouldn't make much difference since tcp_rexmit_interval_min 5987 * is of the order of several 100ms and starting the timer 5988 * first and then sending the packet will result in difference 5989 * of few micro seconds. 5990 * 5991 * Without this optimization, we are forced to hold the fanout 5992 * lock across the ipcl_bind_insert() and sending the packet 5993 * so that we don't race against an incoming packet (maybe RST) 5994 * for this eager. 5995 * 5996 * It is necessary to acquire an extra reference on the eager 5997 * at this point and hold it until after tcp_send_data() to 5998 * ensure against an eager close race. 5999 */ 6000 6001 CONN_INC_REF(eager->tcp_connp); 6002 6003 TCP_TIMER_RESTART(eager, eager->tcp_rto); 6004 6005 /* 6006 * Insert the eager in its own perimeter now. We are ready to deal 6007 * with any packets on eager. 6008 */ 6009 if (eager->tcp_ipversion == IPV4_VERSION) { 6010 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 6011 goto error; 6012 } 6013 } else { 6014 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 6015 goto error; 6016 } 6017 } 6018 6019 /* mark conn as fully-bound */ 6020 econnp->conn_fully_bound = B_TRUE; 6021 6022 /* Send the SYN-ACK */ 6023 tcp_send_data(eager, eager->tcp_wq, mp1); 6024 CONN_DEC_REF(eager->tcp_connp); 6025 freemsg(mp); 6026 6027 return; 6028 error: 6029 freemsg(mp1); 6030 eager->tcp_closemp_used = B_TRUE; 6031 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6032 squeue_fill(econnp->conn_sqp, &eager->tcp_closemp, tcp_eager_kill, 6033 econnp, SQTAG_TCP_CONN_REQ_2); 6034 6035 /* 6036 * If a connection already exists, send the mp to that connections so 6037 * that it can be appropriately dealt with. 6038 */ 6039 ipst = tcps->tcps_netstack->netstack_ip; 6040 6041 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 6042 if (!IPCL_IS_CONNECTED(econnp)) { 6043 /* 6044 * Something bad happened. ipcl_conn_insert() 6045 * failed because a connection already existed 6046 * in connected hash but we can't find it 6047 * anymore (someone blew it away). Just 6048 * free this message and hopefully remote 6049 * will retransmit at which time the SYN can be 6050 * treated as a new connection or dealth with 6051 * a TH_RST if a connection already exists. 6052 */ 6053 CONN_DEC_REF(econnp); 6054 freemsg(mp); 6055 } else { 6056 squeue_fill(econnp->conn_sqp, mp, tcp_input, 6057 econnp, SQTAG_TCP_CONN_REQ_1); 6058 } 6059 } else { 6060 /* Nobody wants this packet */ 6061 freemsg(mp); 6062 } 6063 return; 6064 error3: 6065 CONN_DEC_REF(econnp); 6066 error2: 6067 freemsg(mp); 6068 } 6069 6070 /* 6071 * In an ideal case of vertical partition in NUMA architecture, its 6072 * beneficial to have the listener and all the incoming connections 6073 * tied to the same squeue. The other constraint is that incoming 6074 * connections should be tied to the squeue attached to interrupted 6075 * CPU for obvious locality reason so this leaves the listener to 6076 * be tied to the same squeue. Our only problem is that when listener 6077 * is binding, the CPU that will get interrupted by the NIC whose 6078 * IP address the listener is binding to is not even known. So 6079 * the code below allows us to change that binding at the time the 6080 * CPU is interrupted by virtue of incoming connection's squeue. 6081 * 6082 * This is usefull only in case of a listener bound to a specific IP 6083 * address. For other kind of listeners, they get bound the 6084 * very first time and there is no attempt to rebind them. 6085 */ 6086 void 6087 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 6088 { 6089 conn_t *connp = (conn_t *)arg; 6090 squeue_t *sqp = (squeue_t *)arg2; 6091 squeue_t *new_sqp; 6092 uint32_t conn_flags; 6093 6094 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 6095 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 6096 } else { 6097 goto done; 6098 } 6099 6100 if (connp->conn_fanout == NULL) 6101 goto done; 6102 6103 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 6104 mutex_enter(&connp->conn_fanout->connf_lock); 6105 mutex_enter(&connp->conn_lock); 6106 /* 6107 * No one from read or write side can access us now 6108 * except for already queued packets on this squeue. 6109 * But since we haven't changed the squeue yet, they 6110 * can't execute. If they are processed after we have 6111 * changed the squeue, they are sent back to the 6112 * correct squeue down below. 6113 * But a listner close can race with processing of 6114 * incoming SYN. If incoming SYN processing changes 6115 * the squeue then the listener close which is waiting 6116 * to enter the squeue would operate on the wrong 6117 * squeue. Hence we don't change the squeue here unless 6118 * the refcount is exactly the minimum refcount. The 6119 * minimum refcount of 4 is counted as - 1 each for 6120 * TCP and IP, 1 for being in the classifier hash, and 6121 * 1 for the mblk being processed. 6122 */ 6123 6124 if (connp->conn_ref != 4 || 6125 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 6126 mutex_exit(&connp->conn_lock); 6127 mutex_exit(&connp->conn_fanout->connf_lock); 6128 goto done; 6129 } 6130 if (connp->conn_sqp != new_sqp) { 6131 while (connp->conn_sqp != new_sqp) 6132 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 6133 } 6134 6135 do { 6136 conn_flags = connp->conn_flags; 6137 conn_flags |= IPCL_FULLY_BOUND; 6138 (void) cas32(&connp->conn_flags, connp->conn_flags, 6139 conn_flags); 6140 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 6141 6142 mutex_exit(&connp->conn_fanout->connf_lock); 6143 mutex_exit(&connp->conn_lock); 6144 } 6145 6146 done: 6147 if (connp->conn_sqp != sqp) { 6148 CONN_INC_REF(connp); 6149 squeue_fill(connp->conn_sqp, mp, 6150 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 6151 } else { 6152 tcp_conn_request(connp, mp, sqp); 6153 } 6154 } 6155 6156 /* 6157 * Successful connect request processing begins when our client passes 6158 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 6159 * our T_OK_ACK reply message upstream. The control flow looks like this: 6160 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 6161 * upstream <- tcp_rput() <- IP 6162 * After various error checks are completed, tcp_connect() lays 6163 * the target address and port into the composite header template, 6164 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 6165 * request followed by an IRE request, and passes the three mblk message 6166 * down to IP looking like this: 6167 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 6168 * Processing continues in tcp_rput() when we receive the following message: 6169 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 6170 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 6171 * to fire off the connection request, and then passes the T_OK_ACK mblk 6172 * upstream that we filled in below. There are, of course, numerous 6173 * error conditions along the way which truncate the processing described 6174 * above. 6175 */ 6176 static void 6177 tcp_connect(tcp_t *tcp, mblk_t *mp) 6178 { 6179 sin_t *sin; 6180 sin6_t *sin6; 6181 queue_t *q = tcp->tcp_wq; 6182 struct T_conn_req *tcr; 6183 ipaddr_t *dstaddrp; 6184 in_port_t dstport; 6185 uint_t srcid; 6186 6187 tcr = (struct T_conn_req *)mp->b_rptr; 6188 6189 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6190 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 6191 tcp_err_ack(tcp, mp, TPROTO, 0); 6192 return; 6193 } 6194 6195 /* 6196 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 6197 * will always have that to send up. Otherwise, we need to do 6198 * special handling in case the allocation fails at that time. 6199 */ 6200 ASSERT(tcp->tcp_ordrel_mp == NULL); 6201 if ((tcp->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) { 6202 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6203 return; 6204 } 6205 6206 /* 6207 * Determine packet type based on type of address passed in 6208 * the request should contain an IPv4 or IPv6 address. 6209 * Make sure that address family matches the type of 6210 * family of the the address passed down 6211 */ 6212 switch (tcr->DEST_length) { 6213 default: 6214 tcp_err_ack(tcp, mp, TBADADDR, 0); 6215 return; 6216 6217 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 6218 /* 6219 * XXX: The check for valid DEST_length was not there 6220 * in earlier releases and some buggy 6221 * TLI apps (e.g Sybase) got away with not feeding 6222 * in sin_zero part of address. 6223 * We allow that bug to keep those buggy apps humming. 6224 * Test suites require the check on DEST_length. 6225 * We construct a new mblk with valid DEST_length 6226 * free the original so the rest of the code does 6227 * not have to keep track of this special shorter 6228 * length address case. 6229 */ 6230 mblk_t *nmp; 6231 struct T_conn_req *ntcr; 6232 sin_t *nsin; 6233 6234 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6235 tcr->OPT_length, BPRI_HI); 6236 if (nmp == NULL) { 6237 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6238 return; 6239 } 6240 ntcr = (struct T_conn_req *)nmp->b_rptr; 6241 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6242 ntcr->PRIM_type = T_CONN_REQ; 6243 ntcr->DEST_length = sizeof (sin_t); 6244 ntcr->DEST_offset = sizeof (struct T_conn_req); 6245 6246 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6247 *nsin = sin_null; 6248 /* Get pointer to shorter address to copy from original mp */ 6249 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6250 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6251 if (sin == NULL || !OK_32PTR((char *)sin)) { 6252 freemsg(nmp); 6253 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6254 return; 6255 } 6256 nsin->sin_family = sin->sin_family; 6257 nsin->sin_port = sin->sin_port; 6258 nsin->sin_addr = sin->sin_addr; 6259 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6260 nmp->b_wptr = (uchar_t *)&nsin[1]; 6261 if (tcr->OPT_length != 0) { 6262 ntcr->OPT_length = tcr->OPT_length; 6263 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6264 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6265 (uchar_t *)ntcr + ntcr->OPT_offset, 6266 tcr->OPT_length); 6267 nmp->b_wptr += tcr->OPT_length; 6268 } 6269 freemsg(mp); /* original mp freed */ 6270 mp = nmp; /* re-initialize original variables */ 6271 tcr = ntcr; 6272 } 6273 /* FALLTHRU */ 6274 6275 case sizeof (sin_t): 6276 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6277 sizeof (sin_t)); 6278 if (sin == NULL || !OK_32PTR((char *)sin)) { 6279 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6280 return; 6281 } 6282 if (tcp->tcp_family != AF_INET || 6283 sin->sin_family != AF_INET) { 6284 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6285 return; 6286 } 6287 if (sin->sin_port == 0) { 6288 tcp_err_ack(tcp, mp, TBADADDR, 0); 6289 return; 6290 } 6291 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 6292 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6293 return; 6294 } 6295 6296 break; 6297 6298 case sizeof (sin6_t): 6299 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 6300 sizeof (sin6_t)); 6301 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 6302 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6303 return; 6304 } 6305 if (tcp->tcp_family != AF_INET6 || 6306 sin6->sin6_family != AF_INET6) { 6307 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6308 return; 6309 } 6310 if (sin6->sin6_port == 0) { 6311 tcp_err_ack(tcp, mp, TBADADDR, 0); 6312 return; 6313 } 6314 break; 6315 } 6316 /* 6317 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6318 * should key on their sequence number and cut them loose. 6319 */ 6320 6321 /* 6322 * If options passed in, feed it for verification and handling 6323 */ 6324 if (tcr->OPT_length != 0) { 6325 mblk_t *ok_mp; 6326 mblk_t *discon_mp; 6327 mblk_t *conn_opts_mp; 6328 int t_error, sys_error, do_disconnect; 6329 6330 conn_opts_mp = NULL; 6331 6332 if (tcp_conprim_opt_process(tcp, mp, 6333 &do_disconnect, &t_error, &sys_error) < 0) { 6334 if (do_disconnect) { 6335 ASSERT(t_error == 0 && sys_error == 0); 6336 discon_mp = mi_tpi_discon_ind(NULL, 6337 ECONNREFUSED, 0); 6338 if (!discon_mp) { 6339 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6340 TSYSERR, ENOMEM); 6341 return; 6342 } 6343 ok_mp = mi_tpi_ok_ack_alloc(mp); 6344 if (!ok_mp) { 6345 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6346 TSYSERR, ENOMEM); 6347 return; 6348 } 6349 qreply(q, ok_mp); 6350 qreply(q, discon_mp); /* no flush! */ 6351 } else { 6352 ASSERT(t_error != 0); 6353 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6354 sys_error); 6355 } 6356 return; 6357 } 6358 /* 6359 * Success in setting options, the mp option buffer represented 6360 * by OPT_length/offset has been potentially modified and 6361 * contains results of option processing. We copy it in 6362 * another mp to save it for potentially influencing returning 6363 * it in T_CONN_CONN. 6364 */ 6365 if (tcr->OPT_length != 0) { /* there are resulting options */ 6366 conn_opts_mp = copyb(mp); 6367 if (!conn_opts_mp) { 6368 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6369 TSYSERR, ENOMEM); 6370 return; 6371 } 6372 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6373 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6374 /* 6375 * Note: 6376 * These resulting option negotiation can include any 6377 * end-to-end negotiation options but there no such 6378 * thing (yet?) in our TCP/IP. 6379 */ 6380 } 6381 } 6382 6383 /* 6384 * If we're connecting to an IPv4-mapped IPv6 address, we need to 6385 * make sure that the template IP header in the tcp structure is an 6386 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 6387 * need to this before we call tcp_bindi() so that the port lookup 6388 * code will look for ports in the correct port space (IPv4 and 6389 * IPv6 have separate port spaces). 6390 */ 6391 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 6392 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6393 int err = 0; 6394 6395 err = tcp_header_init_ipv4(tcp); 6396 if (err != 0) { 6397 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6398 goto connect_failed; 6399 } 6400 if (tcp->tcp_lport != 0) 6401 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 6402 } 6403 6404 if (tcp->tcp_issocket) { 6405 /* 6406 * TCP is _D_SODIRECT and sockfs is directly above so save 6407 * the shared sonode sodirect_t pointer (if any) to enable 6408 * TCP sodirect. 6409 */ 6410 tcp->tcp_sodirect = SOD_QTOSODP(tcp->tcp_rq); 6411 } 6412 6413 switch (tcp->tcp_state) { 6414 case TCPS_IDLE: 6415 /* 6416 * We support quick connect, refer to comments in 6417 * tcp_connect_*() 6418 */ 6419 /* FALLTHRU */ 6420 case TCPS_BOUND: 6421 case TCPS_LISTEN: 6422 if (tcp->tcp_family == AF_INET6) { 6423 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6424 tcp_connect_ipv6(tcp, mp, 6425 &sin6->sin6_addr, 6426 sin6->sin6_port, sin6->sin6_flowinfo, 6427 sin6->__sin6_src_id, sin6->sin6_scope_id); 6428 return; 6429 } 6430 /* 6431 * Destination adress is mapped IPv6 address. 6432 * Source bound address should be unspecified or 6433 * IPv6 mapped address as well. 6434 */ 6435 if (!IN6_IS_ADDR_UNSPECIFIED( 6436 &tcp->tcp_bound_source_v6) && 6437 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 6438 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 6439 EADDRNOTAVAIL); 6440 break; 6441 } 6442 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 6443 dstport = sin6->sin6_port; 6444 srcid = sin6->__sin6_src_id; 6445 } else { 6446 dstaddrp = &sin->sin_addr.s_addr; 6447 dstport = sin->sin_port; 6448 srcid = 0; 6449 } 6450 6451 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 6452 return; 6453 default: 6454 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 6455 break; 6456 } 6457 /* 6458 * Note: Code below is the "failure" case 6459 */ 6460 /* return error ack and blow away saved option results if any */ 6461 connect_failed: 6462 if (mp != NULL) 6463 putnext(tcp->tcp_rq, mp); 6464 else { 6465 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6466 TSYSERR, ENOMEM); 6467 } 6468 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6469 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6470 } 6471 6472 /* 6473 * Handle connect to IPv4 destinations, including connections for AF_INET6 6474 * sockets connecting to IPv4 mapped IPv6 destinations. 6475 */ 6476 static void 6477 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6478 uint_t srcid) 6479 { 6480 tcph_t *tcph; 6481 mblk_t *mp1; 6482 ipaddr_t dstaddr = *dstaddrp; 6483 int32_t oldstate; 6484 uint16_t lport; 6485 tcp_stack_t *tcps = tcp->tcp_tcps; 6486 6487 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6488 6489 /* Check for attempt to connect to INADDR_ANY */ 6490 if (dstaddr == INADDR_ANY) { 6491 /* 6492 * SunOS 4.x and 4.3 BSD allow an application 6493 * to connect a TCP socket to INADDR_ANY. 6494 * When they do this, the kernel picks the 6495 * address of one interface and uses it 6496 * instead. The kernel usually ends up 6497 * picking the address of the loopback 6498 * interface. This is an undocumented feature. 6499 * However, we provide the same thing here 6500 * in order to have source and binary 6501 * compatibility with SunOS 4.x. 6502 * Update the T_CONN_REQ (sin/sin6) since it is used to 6503 * generate the T_CONN_CON. 6504 */ 6505 dstaddr = htonl(INADDR_LOOPBACK); 6506 *dstaddrp = dstaddr; 6507 } 6508 6509 /* Handle __sin6_src_id if socket not bound to an IP address */ 6510 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6511 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6512 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6513 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6514 tcp->tcp_ipha->ipha_src); 6515 } 6516 6517 /* 6518 * Don't let an endpoint connect to itself. Note that 6519 * the test here does not catch the case where the 6520 * source IP addr was left unspecified by the user. In 6521 * this case, the source addr is set in tcp_adapt_ire() 6522 * using the reply to the T_BIND message that we send 6523 * down to IP here and the check is repeated in tcp_rput_other. 6524 */ 6525 if (dstaddr == tcp->tcp_ipha->ipha_src && 6526 dstport == tcp->tcp_lport) { 6527 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6528 goto failed; 6529 } 6530 6531 tcp->tcp_ipha->ipha_dst = dstaddr; 6532 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6533 6534 /* 6535 * Massage a source route if any putting the first hop 6536 * in iph_dst. Compute a starting value for the checksum which 6537 * takes into account that the original iph_dst should be 6538 * included in the checksum but that ip will include the 6539 * first hop in the source route in the tcp checksum. 6540 */ 6541 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6542 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6543 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6544 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6545 if ((int)tcp->tcp_sum < 0) 6546 tcp->tcp_sum--; 6547 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6548 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6549 (tcp->tcp_sum >> 16)); 6550 tcph = tcp->tcp_tcph; 6551 *(uint16_t *)tcph->th_fport = dstport; 6552 tcp->tcp_fport = dstport; 6553 6554 oldstate = tcp->tcp_state; 6555 /* 6556 * At this point the remote destination address and remote port fields 6557 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6558 * have to see which state tcp was in so we can take apropriate action. 6559 */ 6560 if (oldstate == TCPS_IDLE) { 6561 /* 6562 * We support a quick connect capability here, allowing 6563 * clients to transition directly from IDLE to SYN_SENT 6564 * tcp_bindi will pick an unused port, insert the connection 6565 * in the bind hash and transition to BOUND state. 6566 */ 6567 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6568 tcp, B_TRUE); 6569 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6570 B_FALSE, B_FALSE); 6571 if (lport == 0) { 6572 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6573 goto failed; 6574 } 6575 } 6576 tcp->tcp_state = TCPS_SYN_SENT; 6577 6578 /* 6579 * TODO: allow data with connect requests 6580 * by unlinking M_DATA trailers here and 6581 * linking them in behind the T_OK_ACK mblk. 6582 * The tcp_rput() bind ack handler would then 6583 * feed them to tcp_wput_data() rather than call 6584 * tcp_timer(). 6585 */ 6586 mp = mi_tpi_ok_ack_alloc(mp); 6587 if (!mp) { 6588 tcp->tcp_state = oldstate; 6589 goto failed; 6590 } 6591 if (tcp->tcp_family == AF_INET) { 6592 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6593 sizeof (ipa_conn_t)); 6594 } else { 6595 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6596 sizeof (ipa6_conn_t)); 6597 } 6598 if (mp1) { 6599 /* 6600 * We need to make sure that the conn_recv is set to a non-null 6601 * value before we insert the conn_t into the classifier table. 6602 * This is to avoid a race with an incoming packet which does 6603 * an ipcl_classify(). 6604 */ 6605 tcp->tcp_connp->conn_recv = tcp_input; 6606 6607 /* Hang onto the T_OK_ACK for later. */ 6608 linkb(mp1, mp); 6609 mblk_setcred(mp1, tcp->tcp_cred); 6610 if (tcp->tcp_family == AF_INET) 6611 mp1 = ip_bind_v4(tcp->tcp_wq, mp1, tcp->tcp_connp); 6612 else { 6613 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6614 &tcp->tcp_sticky_ipp); 6615 } 6616 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6617 tcp->tcp_active_open = 1; 6618 /* 6619 * If the bind cannot complete immediately 6620 * IP will arrange to call tcp_rput_other 6621 * when the bind completes. 6622 */ 6623 if (mp1 != NULL) 6624 tcp_rput_other(tcp, mp1); 6625 return; 6626 } 6627 /* Error case */ 6628 tcp->tcp_state = oldstate; 6629 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6630 6631 failed: 6632 /* return error ack and blow away saved option results if any */ 6633 if (mp != NULL) 6634 putnext(tcp->tcp_rq, mp); 6635 else { 6636 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6637 TSYSERR, ENOMEM); 6638 } 6639 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6640 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6641 6642 } 6643 6644 /* 6645 * Handle connect to IPv6 destinations. 6646 */ 6647 static void 6648 tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 6649 in_port_t dstport, uint32_t flowinfo, uint_t srcid, uint32_t scope_id) 6650 { 6651 tcph_t *tcph; 6652 mblk_t *mp1; 6653 ip6_rthdr_t *rth; 6654 int32_t oldstate; 6655 uint16_t lport; 6656 tcp_stack_t *tcps = tcp->tcp_tcps; 6657 6658 ASSERT(tcp->tcp_family == AF_INET6); 6659 6660 /* 6661 * If we're here, it means that the destination address is a native 6662 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6663 * reason why it might not be IPv6 is if the socket was bound to an 6664 * IPv4-mapped IPv6 address. 6665 */ 6666 if (tcp->tcp_ipversion != IPV6_VERSION) { 6667 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6668 goto failed; 6669 } 6670 6671 /* 6672 * Interpret a zero destination to mean loopback. 6673 * Update the T_CONN_REQ (sin/sin6) since it is used to 6674 * generate the T_CONN_CON. 6675 */ 6676 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6677 *dstaddrp = ipv6_loopback; 6678 } 6679 6680 /* Handle __sin6_src_id if socket not bound to an IP address */ 6681 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6682 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6683 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6684 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6685 } 6686 6687 /* 6688 * Take care of the scope_id now and add ip6i_t 6689 * if ip6i_t is not already allocated through TCP 6690 * sticky options. At this point tcp_ip6h does not 6691 * have dst info, thus use dstaddrp. 6692 */ 6693 if (scope_id != 0 && 6694 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6695 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6696 ip6i_t *ip6i; 6697 6698 ipp->ipp_ifindex = scope_id; 6699 ip6i = (ip6i_t *)tcp->tcp_iphc; 6700 6701 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6702 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6703 /* Already allocated */ 6704 ip6i->ip6i_flags |= IP6I_IFINDEX; 6705 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6706 ipp->ipp_fields |= IPPF_SCOPE_ID; 6707 } else { 6708 int reterr; 6709 6710 ipp->ipp_fields |= IPPF_SCOPE_ID; 6711 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6712 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6713 reterr = tcp_build_hdrs(tcp->tcp_rq, tcp); 6714 if (reterr != 0) 6715 goto failed; 6716 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6717 } 6718 } 6719 6720 /* 6721 * Don't let an endpoint connect to itself. Note that 6722 * the test here does not catch the case where the 6723 * source IP addr was left unspecified by the user. In 6724 * this case, the source addr is set in tcp_adapt_ire() 6725 * using the reply to the T_BIND message that we send 6726 * down to IP here and the check is repeated in tcp_rput_other. 6727 */ 6728 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6729 (dstport == tcp->tcp_lport)) { 6730 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6731 goto failed; 6732 } 6733 6734 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6735 tcp->tcp_remote_v6 = *dstaddrp; 6736 tcp->tcp_ip6h->ip6_vcf = 6737 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6738 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6739 6740 6741 /* 6742 * Massage a routing header (if present) putting the first hop 6743 * in ip6_dst. Compute a starting value for the checksum which 6744 * takes into account that the original ip6_dst should be 6745 * included in the checksum but that ip will include the 6746 * first hop in the source route in the tcp checksum. 6747 */ 6748 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6749 if (rth != NULL) { 6750 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6751 tcps->tcps_netstack); 6752 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6753 (tcp->tcp_sum >> 16)); 6754 } else { 6755 tcp->tcp_sum = 0; 6756 } 6757 6758 tcph = tcp->tcp_tcph; 6759 *(uint16_t *)tcph->th_fport = dstport; 6760 tcp->tcp_fport = dstport; 6761 6762 oldstate = tcp->tcp_state; 6763 /* 6764 * At this point the remote destination address and remote port fields 6765 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6766 * have to see which state tcp was in so we can take apropriate action. 6767 */ 6768 if (oldstate == TCPS_IDLE) { 6769 /* 6770 * We support a quick connect capability here, allowing 6771 * clients to transition directly from IDLE to SYN_SENT 6772 * tcp_bindi will pick an unused port, insert the connection 6773 * in the bind hash and transition to BOUND state. 6774 */ 6775 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6776 tcp, B_TRUE); 6777 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6778 B_FALSE, B_FALSE); 6779 if (lport == 0) { 6780 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6781 goto failed; 6782 } 6783 } 6784 tcp->tcp_state = TCPS_SYN_SENT; 6785 /* 6786 * TODO: allow data with connect requests 6787 * by unlinking M_DATA trailers here and 6788 * linking them in behind the T_OK_ACK mblk. 6789 * The tcp_rput() bind ack handler would then 6790 * feed them to tcp_wput_data() rather than call 6791 * tcp_timer(). 6792 */ 6793 mp = mi_tpi_ok_ack_alloc(mp); 6794 if (!mp) { 6795 tcp->tcp_state = oldstate; 6796 goto failed; 6797 } 6798 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 6799 if (mp1) { 6800 /* 6801 * We need to make sure that the conn_recv is set to a non-null 6802 * value before we insert the conn_t into the classifier table. 6803 * This is to avoid a race with an incoming packet which does 6804 * an ipcl_classify(). 6805 */ 6806 tcp->tcp_connp->conn_recv = tcp_input; 6807 6808 /* Hang onto the T_OK_ACK for later. */ 6809 linkb(mp1, mp); 6810 mblk_setcred(mp1, tcp->tcp_cred); 6811 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6812 &tcp->tcp_sticky_ipp); 6813 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6814 tcp->tcp_active_open = 1; 6815 /* ip_bind_v6() may return ACK or ERROR */ 6816 if (mp1 != NULL) 6817 tcp_rput_other(tcp, mp1); 6818 return; 6819 } 6820 /* Error case */ 6821 tcp->tcp_state = oldstate; 6822 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6823 6824 failed: 6825 /* return error ack and blow away saved option results if any */ 6826 if (mp != NULL) 6827 putnext(tcp->tcp_rq, mp); 6828 else { 6829 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6830 TSYSERR, ENOMEM); 6831 } 6832 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6833 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6834 } 6835 6836 /* 6837 * We need a stream q for detached closing tcp connections 6838 * to use. Our client hereby indicates that this q is the 6839 * one to use. 6840 */ 6841 static void 6842 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6843 { 6844 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6845 queue_t *q = tcp->tcp_wq; 6846 tcp_stack_t *tcps = tcp->tcp_tcps; 6847 6848 #ifdef NS_DEBUG 6849 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6850 tcps->tcps_netstack->netstack_stackid); 6851 #endif 6852 mp->b_datap->db_type = M_IOCACK; 6853 iocp->ioc_count = 0; 6854 mutex_enter(&tcps->tcps_g_q_lock); 6855 if (tcps->tcps_g_q != NULL) { 6856 mutex_exit(&tcps->tcps_g_q_lock); 6857 iocp->ioc_error = EALREADY; 6858 } else { 6859 mblk_t *mp1; 6860 6861 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6862 if (mp1 == NULL) { 6863 mutex_exit(&tcps->tcps_g_q_lock); 6864 iocp->ioc_error = ENOMEM; 6865 } else { 6866 tcps->tcps_g_q = tcp->tcp_rq; 6867 mutex_exit(&tcps->tcps_g_q_lock); 6868 iocp->ioc_error = 0; 6869 iocp->ioc_rval = 0; 6870 /* 6871 * We are passing tcp_sticky_ipp as NULL 6872 * as it is not useful for tcp_default queue 6873 * 6874 * Set conn_recv just in case. 6875 */ 6876 tcp->tcp_connp->conn_recv = tcp_conn_request; 6877 6878 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6879 if (mp1 != NULL) 6880 tcp_rput_other(tcp, mp1); 6881 } 6882 } 6883 qreply(q, mp); 6884 } 6885 6886 /* 6887 * Our client hereby directs us to reject the connection request 6888 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6889 * of sending the appropriate RST, not an ICMP error. 6890 */ 6891 static void 6892 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6893 { 6894 tcp_t *ltcp = NULL; 6895 t_scalar_t seqnum; 6896 conn_t *connp; 6897 tcp_stack_t *tcps = tcp->tcp_tcps; 6898 6899 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6900 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6901 tcp_err_ack(tcp, mp, TPROTO, 0); 6902 return; 6903 } 6904 6905 /* 6906 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6907 * when the stream is in BOUND state. Do not send a reset, 6908 * since the destination IP address is not valid, and it can 6909 * be the initialized value of all zeros (broadcast address). 6910 * 6911 * If TCP has sent down a bind request to IP and has not 6912 * received the reply, reject the request. Otherwise, TCP 6913 * will be confused. 6914 */ 6915 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6916 if (tcp->tcp_debug) { 6917 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6918 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6919 } 6920 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6921 return; 6922 } 6923 6924 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6925 6926 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6927 6928 /* 6929 * According to TPI, for non-listeners, ignore seqnum 6930 * and disconnect. 6931 * Following interpretation of -1 seqnum is historical 6932 * and implied TPI ? (TPI only states that for T_CONN_IND, 6933 * a valid seqnum should not be -1). 6934 * 6935 * -1 means disconnect everything 6936 * regardless even on a listener. 6937 */ 6938 6939 int old_state = tcp->tcp_state; 6940 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6941 6942 /* 6943 * The connection can't be on the tcp_time_wait_head list 6944 * since it is not detached. 6945 */ 6946 ASSERT(tcp->tcp_time_wait_next == NULL); 6947 ASSERT(tcp->tcp_time_wait_prev == NULL); 6948 ASSERT(tcp->tcp_time_wait_expire == 0); 6949 ltcp = NULL; 6950 /* 6951 * If it used to be a listener, check to make sure no one else 6952 * has taken the port before switching back to LISTEN state. 6953 */ 6954 if (tcp->tcp_ipversion == IPV4_VERSION) { 6955 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6956 tcp->tcp_ipha->ipha_src, 6957 tcp->tcp_connp->conn_zoneid, ipst); 6958 if (connp != NULL) 6959 ltcp = connp->conn_tcp; 6960 } else { 6961 /* Allow tcp_bound_if listeners? */ 6962 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6963 &tcp->tcp_ip6h->ip6_src, 0, 6964 tcp->tcp_connp->conn_zoneid, ipst); 6965 if (connp != NULL) 6966 ltcp = connp->conn_tcp; 6967 } 6968 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6969 tcp->tcp_state = TCPS_LISTEN; 6970 } else if (old_state > TCPS_BOUND) { 6971 tcp->tcp_conn_req_max = 0; 6972 tcp->tcp_state = TCPS_BOUND; 6973 } 6974 if (ltcp != NULL) 6975 CONN_DEC_REF(ltcp->tcp_connp); 6976 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6977 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6978 } else if (old_state == TCPS_ESTABLISHED || 6979 old_state == TCPS_CLOSE_WAIT) { 6980 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6981 } 6982 6983 if (tcp->tcp_fused) 6984 tcp_unfuse(tcp); 6985 6986 mutex_enter(&tcp->tcp_eager_lock); 6987 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6988 (tcp->tcp_conn_req_cnt_q != 0)) { 6989 tcp_eager_cleanup(tcp, 0); 6990 } 6991 mutex_exit(&tcp->tcp_eager_lock); 6992 6993 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6994 tcp->tcp_rnxt, TH_RST | TH_ACK); 6995 6996 tcp_reinit(tcp); 6997 6998 if (old_state >= TCPS_ESTABLISHED) { 6999 /* Send M_FLUSH according to TPI */ 7000 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 7001 } 7002 mp = mi_tpi_ok_ack_alloc(mp); 7003 if (mp) 7004 putnext(tcp->tcp_rq, mp); 7005 return; 7006 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 7007 tcp_err_ack(tcp, mp, TBADSEQ, 0); 7008 return; 7009 } 7010 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 7011 /* Send M_FLUSH according to TPI */ 7012 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 7013 } 7014 mp = mi_tpi_ok_ack_alloc(mp); 7015 if (mp) 7016 putnext(tcp->tcp_rq, mp); 7017 } 7018 7019 /* 7020 * Diagnostic routine used to return a string associated with the tcp state. 7021 * Note that if the caller does not supply a buffer, it will use an internal 7022 * static string. This means that if multiple threads call this function at 7023 * the same time, output can be corrupted... Note also that this function 7024 * does not check the size of the supplied buffer. The caller has to make 7025 * sure that it is big enough. 7026 */ 7027 static char * 7028 tcp_display(tcp_t *tcp, char *sup_buf, char format) 7029 { 7030 char buf1[30]; 7031 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 7032 char *buf; 7033 char *cp; 7034 in6_addr_t local, remote; 7035 char local_addrbuf[INET6_ADDRSTRLEN]; 7036 char remote_addrbuf[INET6_ADDRSTRLEN]; 7037 7038 if (sup_buf != NULL) 7039 buf = sup_buf; 7040 else 7041 buf = priv_buf; 7042 7043 if (tcp == NULL) 7044 return ("NULL_TCP"); 7045 switch (tcp->tcp_state) { 7046 case TCPS_CLOSED: 7047 cp = "TCP_CLOSED"; 7048 break; 7049 case TCPS_IDLE: 7050 cp = "TCP_IDLE"; 7051 break; 7052 case TCPS_BOUND: 7053 cp = "TCP_BOUND"; 7054 break; 7055 case TCPS_LISTEN: 7056 cp = "TCP_LISTEN"; 7057 break; 7058 case TCPS_SYN_SENT: 7059 cp = "TCP_SYN_SENT"; 7060 break; 7061 case TCPS_SYN_RCVD: 7062 cp = "TCP_SYN_RCVD"; 7063 break; 7064 case TCPS_ESTABLISHED: 7065 cp = "TCP_ESTABLISHED"; 7066 break; 7067 case TCPS_CLOSE_WAIT: 7068 cp = "TCP_CLOSE_WAIT"; 7069 break; 7070 case TCPS_FIN_WAIT_1: 7071 cp = "TCP_FIN_WAIT_1"; 7072 break; 7073 case TCPS_CLOSING: 7074 cp = "TCP_CLOSING"; 7075 break; 7076 case TCPS_LAST_ACK: 7077 cp = "TCP_LAST_ACK"; 7078 break; 7079 case TCPS_FIN_WAIT_2: 7080 cp = "TCP_FIN_WAIT_2"; 7081 break; 7082 case TCPS_TIME_WAIT: 7083 cp = "TCP_TIME_WAIT"; 7084 break; 7085 default: 7086 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 7087 cp = buf1; 7088 break; 7089 } 7090 switch (format) { 7091 case DISP_ADDR_AND_PORT: 7092 if (tcp->tcp_ipversion == IPV4_VERSION) { 7093 /* 7094 * Note that we use the remote address in the tcp_b 7095 * structure. This means that it will print out 7096 * the real destination address, not the next hop's 7097 * address if source routing is used. 7098 */ 7099 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 7100 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 7101 7102 } else { 7103 local = tcp->tcp_ip_src_v6; 7104 remote = tcp->tcp_remote_v6; 7105 } 7106 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 7107 sizeof (local_addrbuf)); 7108 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 7109 sizeof (remote_addrbuf)); 7110 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 7111 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 7112 ntohs(tcp->tcp_fport), cp); 7113 break; 7114 case DISP_PORT_ONLY: 7115 default: 7116 (void) mi_sprintf(buf, "[%u, %u] %s", 7117 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 7118 break; 7119 } 7120 7121 return (buf); 7122 } 7123 7124 /* 7125 * Called via squeue to get on to eager's perimeter. It sends a 7126 * TH_RST if eager is in the fanout table. The listener wants the 7127 * eager to disappear either by means of tcp_eager_blowoff() or 7128 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 7129 * called (via squeue) if the eager cannot be inserted in the 7130 * fanout table in tcp_conn_request(). 7131 */ 7132 /* ARGSUSED */ 7133 void 7134 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 7135 { 7136 conn_t *econnp = (conn_t *)arg; 7137 tcp_t *eager = econnp->conn_tcp; 7138 tcp_t *listener = eager->tcp_listener; 7139 tcp_stack_t *tcps = eager->tcp_tcps; 7140 7141 /* 7142 * We could be called because listener is closing. Since 7143 * the eager is using listener's queue's, its not safe. 7144 * Better use the default queue just to send the TH_RST 7145 * out. 7146 */ 7147 ASSERT(tcps->tcps_g_q != NULL); 7148 eager->tcp_rq = tcps->tcps_g_q; 7149 eager->tcp_wq = WR(tcps->tcps_g_q); 7150 7151 /* 7152 * An eager's conn_fanout will be NULL if it's a duplicate 7153 * for an existing 4-tuples in the conn fanout table. 7154 * We don't want to send an RST out in such case. 7155 */ 7156 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 7157 tcp_xmit_ctl("tcp_eager_kill, can't wait", 7158 eager, eager->tcp_snxt, 0, TH_RST); 7159 } 7160 7161 /* We are here because listener wants this eager gone */ 7162 if (listener != NULL) { 7163 mutex_enter(&listener->tcp_eager_lock); 7164 tcp_eager_unlink(eager); 7165 if (eager->tcp_tconnind_started) { 7166 /* 7167 * The eager has sent a conn_ind up to the 7168 * listener but listener decides to close 7169 * instead. We need to drop the extra ref 7170 * placed on eager in tcp_rput_data() before 7171 * sending the conn_ind to listener. 7172 */ 7173 CONN_DEC_REF(econnp); 7174 } 7175 mutex_exit(&listener->tcp_eager_lock); 7176 CONN_DEC_REF(listener->tcp_connp); 7177 } 7178 7179 if (eager->tcp_state > TCPS_BOUND) 7180 tcp_close_detached(eager); 7181 } 7182 7183 /* 7184 * Reset any eager connection hanging off this listener marked 7185 * with 'seqnum' and then reclaim it's resources. 7186 */ 7187 static boolean_t 7188 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 7189 { 7190 tcp_t *eager; 7191 mblk_t *mp; 7192 tcp_stack_t *tcps = listener->tcp_tcps; 7193 7194 TCP_STAT(tcps, tcp_eager_blowoff_calls); 7195 eager = listener; 7196 mutex_enter(&listener->tcp_eager_lock); 7197 do { 7198 eager = eager->tcp_eager_next_q; 7199 if (eager == NULL) { 7200 mutex_exit(&listener->tcp_eager_lock); 7201 return (B_FALSE); 7202 } 7203 } while (eager->tcp_conn_req_seqnum != seqnum); 7204 7205 if (eager->tcp_closemp_used) { 7206 mutex_exit(&listener->tcp_eager_lock); 7207 return (B_TRUE); 7208 } 7209 eager->tcp_closemp_used = B_TRUE; 7210 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7211 CONN_INC_REF(eager->tcp_connp); 7212 mutex_exit(&listener->tcp_eager_lock); 7213 mp = &eager->tcp_closemp; 7214 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 7215 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 7216 return (B_TRUE); 7217 } 7218 7219 /* 7220 * Reset any eager connection hanging off this listener 7221 * and then reclaim it's resources. 7222 */ 7223 static void 7224 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 7225 { 7226 tcp_t *eager; 7227 mblk_t *mp; 7228 tcp_stack_t *tcps = listener->tcp_tcps; 7229 7230 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7231 7232 if (!q0_only) { 7233 /* First cleanup q */ 7234 TCP_STAT(tcps, tcp_eager_blowoff_q); 7235 eager = listener->tcp_eager_next_q; 7236 while (eager != NULL) { 7237 if (!eager->tcp_closemp_used) { 7238 eager->tcp_closemp_used = B_TRUE; 7239 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7240 CONN_INC_REF(eager->tcp_connp); 7241 mp = &eager->tcp_closemp; 7242 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7243 tcp_eager_kill, eager->tcp_connp, 7244 SQTAG_TCP_EAGER_CLEANUP); 7245 } 7246 eager = eager->tcp_eager_next_q; 7247 } 7248 } 7249 /* Then cleanup q0 */ 7250 TCP_STAT(tcps, tcp_eager_blowoff_q0); 7251 eager = listener->tcp_eager_next_q0; 7252 while (eager != listener) { 7253 if (!eager->tcp_closemp_used) { 7254 eager->tcp_closemp_used = B_TRUE; 7255 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7256 CONN_INC_REF(eager->tcp_connp); 7257 mp = &eager->tcp_closemp; 7258 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7259 tcp_eager_kill, eager->tcp_connp, 7260 SQTAG_TCP_EAGER_CLEANUP_Q0); 7261 } 7262 eager = eager->tcp_eager_next_q0; 7263 } 7264 } 7265 7266 /* 7267 * If we are an eager connection hanging off a listener that hasn't 7268 * formally accepted the connection yet, get off his list and blow off 7269 * any data that we have accumulated. 7270 */ 7271 static void 7272 tcp_eager_unlink(tcp_t *tcp) 7273 { 7274 tcp_t *listener = tcp->tcp_listener; 7275 7276 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7277 ASSERT(listener != NULL); 7278 if (tcp->tcp_eager_next_q0 != NULL) { 7279 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 7280 7281 /* Remove the eager tcp from q0 */ 7282 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 7283 tcp->tcp_eager_prev_q0; 7284 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 7285 tcp->tcp_eager_next_q0; 7286 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 7287 listener->tcp_conn_req_cnt_q0--; 7288 7289 tcp->tcp_eager_next_q0 = NULL; 7290 tcp->tcp_eager_prev_q0 = NULL; 7291 7292 /* 7293 * Take the eager out, if it is in the list of droppable 7294 * eagers. 7295 */ 7296 MAKE_UNDROPPABLE(tcp); 7297 7298 if (tcp->tcp_syn_rcvd_timeout != 0) { 7299 /* we have timed out before */ 7300 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 7301 listener->tcp_syn_rcvd_timeout--; 7302 } 7303 } else { 7304 tcp_t **tcpp = &listener->tcp_eager_next_q; 7305 tcp_t *prev = NULL; 7306 7307 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 7308 if (tcpp[0] == tcp) { 7309 if (listener->tcp_eager_last_q == tcp) { 7310 /* 7311 * If we are unlinking the last 7312 * element on the list, adjust 7313 * tail pointer. Set tail pointer 7314 * to nil when list is empty. 7315 */ 7316 ASSERT(tcp->tcp_eager_next_q == NULL); 7317 if (listener->tcp_eager_last_q == 7318 listener->tcp_eager_next_q) { 7319 listener->tcp_eager_last_q = 7320 NULL; 7321 } else { 7322 /* 7323 * We won't get here if there 7324 * is only one eager in the 7325 * list. 7326 */ 7327 ASSERT(prev != NULL); 7328 listener->tcp_eager_last_q = 7329 prev; 7330 } 7331 } 7332 tcpp[0] = tcp->tcp_eager_next_q; 7333 tcp->tcp_eager_next_q = NULL; 7334 tcp->tcp_eager_last_q = NULL; 7335 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7336 listener->tcp_conn_req_cnt_q--; 7337 break; 7338 } 7339 prev = tcpp[0]; 7340 } 7341 } 7342 tcp->tcp_listener = NULL; 7343 } 7344 7345 /* Shorthand to generate and send TPI error acks to our client */ 7346 static void 7347 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7348 { 7349 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7350 putnext(tcp->tcp_rq, mp); 7351 } 7352 7353 /* Shorthand to generate and send TPI error acks to our client */ 7354 static void 7355 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7356 int t_error, int sys_error) 7357 { 7358 struct T_error_ack *teackp; 7359 7360 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7361 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7362 teackp = (struct T_error_ack *)mp->b_rptr; 7363 teackp->ERROR_prim = primitive; 7364 teackp->TLI_error = t_error; 7365 teackp->UNIX_error = sys_error; 7366 putnext(tcp->tcp_rq, mp); 7367 } 7368 } 7369 7370 /* 7371 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7372 * but instead the code relies on: 7373 * - the fact that the address of the array and its size never changes 7374 * - the atomic assignment of the elements of the array 7375 */ 7376 /* ARGSUSED */ 7377 static int 7378 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7379 { 7380 int i; 7381 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7382 7383 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7384 if (tcps->tcps_g_epriv_ports[i] != 0) 7385 (void) mi_mpprintf(mp, "%d ", 7386 tcps->tcps_g_epriv_ports[i]); 7387 } 7388 return (0); 7389 } 7390 7391 /* 7392 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7393 * threads from changing it at the same time. 7394 */ 7395 /* ARGSUSED */ 7396 static int 7397 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7398 cred_t *cr) 7399 { 7400 long new_value; 7401 int i; 7402 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7403 7404 /* 7405 * Fail the request if the new value does not lie within the 7406 * port number limits. 7407 */ 7408 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7409 new_value <= 0 || new_value >= 65536) { 7410 return (EINVAL); 7411 } 7412 7413 mutex_enter(&tcps->tcps_epriv_port_lock); 7414 /* Check if the value is already in the list */ 7415 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7416 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7417 mutex_exit(&tcps->tcps_epriv_port_lock); 7418 return (EEXIST); 7419 } 7420 } 7421 /* Find an empty slot */ 7422 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7423 if (tcps->tcps_g_epriv_ports[i] == 0) 7424 break; 7425 } 7426 if (i == tcps->tcps_g_num_epriv_ports) { 7427 mutex_exit(&tcps->tcps_epriv_port_lock); 7428 return (EOVERFLOW); 7429 } 7430 /* Set the new value */ 7431 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7432 mutex_exit(&tcps->tcps_epriv_port_lock); 7433 return (0); 7434 } 7435 7436 /* 7437 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7438 * threads from changing it at the same time. 7439 */ 7440 /* ARGSUSED */ 7441 static int 7442 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7443 cred_t *cr) 7444 { 7445 long new_value; 7446 int i; 7447 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7448 7449 /* 7450 * Fail the request if the new value does not lie within the 7451 * port number limits. 7452 */ 7453 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7454 new_value >= 65536) { 7455 return (EINVAL); 7456 } 7457 7458 mutex_enter(&tcps->tcps_epriv_port_lock); 7459 /* Check that the value is already in the list */ 7460 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7461 if (tcps->tcps_g_epriv_ports[i] == new_value) 7462 break; 7463 } 7464 if (i == tcps->tcps_g_num_epriv_ports) { 7465 mutex_exit(&tcps->tcps_epriv_port_lock); 7466 return (ESRCH); 7467 } 7468 /* Clear the value */ 7469 tcps->tcps_g_epriv_ports[i] = 0; 7470 mutex_exit(&tcps->tcps_epriv_port_lock); 7471 return (0); 7472 } 7473 7474 /* Return the TPI/TLI equivalent of our current tcp_state */ 7475 static int 7476 tcp_tpistate(tcp_t *tcp) 7477 { 7478 switch (tcp->tcp_state) { 7479 case TCPS_IDLE: 7480 return (TS_UNBND); 7481 case TCPS_LISTEN: 7482 /* 7483 * Return whether there are outstanding T_CONN_IND waiting 7484 * for the matching T_CONN_RES. Therefore don't count q0. 7485 */ 7486 if (tcp->tcp_conn_req_cnt_q > 0) 7487 return (TS_WRES_CIND); 7488 else 7489 return (TS_IDLE); 7490 case TCPS_BOUND: 7491 return (TS_IDLE); 7492 case TCPS_SYN_SENT: 7493 return (TS_WCON_CREQ); 7494 case TCPS_SYN_RCVD: 7495 /* 7496 * Note: assumption: this has to the active open SYN_RCVD. 7497 * The passive instance is detached in SYN_RCVD stage of 7498 * incoming connection processing so we cannot get request 7499 * for T_info_ack on it. 7500 */ 7501 return (TS_WACK_CRES); 7502 case TCPS_ESTABLISHED: 7503 return (TS_DATA_XFER); 7504 case TCPS_CLOSE_WAIT: 7505 return (TS_WREQ_ORDREL); 7506 case TCPS_FIN_WAIT_1: 7507 return (TS_WIND_ORDREL); 7508 case TCPS_FIN_WAIT_2: 7509 return (TS_WIND_ORDREL); 7510 7511 case TCPS_CLOSING: 7512 case TCPS_LAST_ACK: 7513 case TCPS_TIME_WAIT: 7514 case TCPS_CLOSED: 7515 /* 7516 * Following TS_WACK_DREQ7 is a rendition of "not 7517 * yet TS_IDLE" TPI state. There is no best match to any 7518 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7519 * choose a value chosen that will map to TLI/XTI level 7520 * state of TSTATECHNG (state is process of changing) which 7521 * captures what this dummy state represents. 7522 */ 7523 return (TS_WACK_DREQ7); 7524 default: 7525 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7526 tcp->tcp_state, tcp_display(tcp, NULL, 7527 DISP_PORT_ONLY)); 7528 return (TS_UNBND); 7529 } 7530 } 7531 7532 static void 7533 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7534 { 7535 tcp_stack_t *tcps = tcp->tcp_tcps; 7536 7537 if (tcp->tcp_family == AF_INET6) 7538 *tia = tcp_g_t_info_ack_v6; 7539 else 7540 *tia = tcp_g_t_info_ack; 7541 tia->CURRENT_state = tcp_tpistate(tcp); 7542 tia->OPT_size = tcp_max_optsize; 7543 if (tcp->tcp_mss == 0) { 7544 /* Not yet set - tcp_open does not set mss */ 7545 if (tcp->tcp_ipversion == IPV4_VERSION) 7546 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7547 else 7548 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7549 } else { 7550 tia->TIDU_size = tcp->tcp_mss; 7551 } 7552 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7553 } 7554 7555 /* 7556 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7557 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7558 * tcp_g_t_info_ack. The current state of the stream is copied from 7559 * tcp_state. 7560 */ 7561 static void 7562 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7563 { 7564 t_uscalar_t cap_bits1; 7565 struct T_capability_ack *tcap; 7566 7567 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7568 freemsg(mp); 7569 return; 7570 } 7571 7572 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7573 7574 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7575 mp->b_datap->db_type, T_CAPABILITY_ACK); 7576 if (mp == NULL) 7577 return; 7578 7579 tcap = (struct T_capability_ack *)mp->b_rptr; 7580 tcap->CAP_bits1 = 0; 7581 7582 if (cap_bits1 & TC1_INFO) { 7583 tcp_copy_info(&tcap->INFO_ack, tcp); 7584 tcap->CAP_bits1 |= TC1_INFO; 7585 } 7586 7587 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7588 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7589 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7590 } 7591 7592 putnext(tcp->tcp_rq, mp); 7593 } 7594 7595 /* 7596 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7597 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7598 * The current state of the stream is copied from tcp_state. 7599 */ 7600 static void 7601 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7602 { 7603 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7604 T_INFO_ACK); 7605 if (!mp) { 7606 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7607 return; 7608 } 7609 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7610 putnext(tcp->tcp_rq, mp); 7611 } 7612 7613 /* Respond to the TPI addr request */ 7614 static void 7615 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7616 { 7617 sin_t *sin; 7618 mblk_t *ackmp; 7619 struct T_addr_ack *taa; 7620 7621 /* Make it large enough for worst case */ 7622 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7623 2 * sizeof (sin6_t), 1); 7624 if (ackmp == NULL) { 7625 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7626 return; 7627 } 7628 7629 if (tcp->tcp_ipversion == IPV6_VERSION) { 7630 tcp_addr_req_ipv6(tcp, ackmp); 7631 return; 7632 } 7633 taa = (struct T_addr_ack *)ackmp->b_rptr; 7634 7635 bzero(taa, sizeof (struct T_addr_ack)); 7636 ackmp->b_wptr = (uchar_t *)&taa[1]; 7637 7638 taa->PRIM_type = T_ADDR_ACK; 7639 ackmp->b_datap->db_type = M_PCPROTO; 7640 7641 /* 7642 * Note: Following code assumes 32 bit alignment of basic 7643 * data structures like sin_t and struct T_addr_ack. 7644 */ 7645 if (tcp->tcp_state >= TCPS_BOUND) { 7646 /* 7647 * Fill in local address 7648 */ 7649 taa->LOCADDR_length = sizeof (sin_t); 7650 taa->LOCADDR_offset = sizeof (*taa); 7651 7652 sin = (sin_t *)&taa[1]; 7653 7654 /* Fill zeroes and then intialize non-zero fields */ 7655 *sin = sin_null; 7656 7657 sin->sin_family = AF_INET; 7658 7659 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7660 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7661 7662 ackmp->b_wptr = (uchar_t *)&sin[1]; 7663 7664 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7665 /* 7666 * Fill in Remote address 7667 */ 7668 taa->REMADDR_length = sizeof (sin_t); 7669 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7670 taa->LOCADDR_length); 7671 7672 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7673 *sin = sin_null; 7674 sin->sin_family = AF_INET; 7675 sin->sin_addr.s_addr = tcp->tcp_remote; 7676 sin->sin_port = tcp->tcp_fport; 7677 7678 ackmp->b_wptr = (uchar_t *)&sin[1]; 7679 } 7680 } 7681 putnext(tcp->tcp_rq, ackmp); 7682 } 7683 7684 /* Assumes that tcp_addr_req gets enough space and alignment */ 7685 static void 7686 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7687 { 7688 sin6_t *sin6; 7689 struct T_addr_ack *taa; 7690 7691 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7692 ASSERT(OK_32PTR(ackmp->b_rptr)); 7693 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7694 2 * sizeof (sin6_t)); 7695 7696 taa = (struct T_addr_ack *)ackmp->b_rptr; 7697 7698 bzero(taa, sizeof (struct T_addr_ack)); 7699 ackmp->b_wptr = (uchar_t *)&taa[1]; 7700 7701 taa->PRIM_type = T_ADDR_ACK; 7702 ackmp->b_datap->db_type = M_PCPROTO; 7703 7704 /* 7705 * Note: Following code assumes 32 bit alignment of basic 7706 * data structures like sin6_t and struct T_addr_ack. 7707 */ 7708 if (tcp->tcp_state >= TCPS_BOUND) { 7709 /* 7710 * Fill in local address 7711 */ 7712 taa->LOCADDR_length = sizeof (sin6_t); 7713 taa->LOCADDR_offset = sizeof (*taa); 7714 7715 sin6 = (sin6_t *)&taa[1]; 7716 *sin6 = sin6_null; 7717 7718 sin6->sin6_family = AF_INET6; 7719 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7720 sin6->sin6_port = tcp->tcp_lport; 7721 7722 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7723 7724 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7725 /* 7726 * Fill in Remote address 7727 */ 7728 taa->REMADDR_length = sizeof (sin6_t); 7729 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7730 taa->LOCADDR_length); 7731 7732 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7733 *sin6 = sin6_null; 7734 sin6->sin6_family = AF_INET6; 7735 sin6->sin6_flowinfo = 7736 tcp->tcp_ip6h->ip6_vcf & 7737 ~IPV6_VERS_AND_FLOW_MASK; 7738 sin6->sin6_addr = tcp->tcp_remote_v6; 7739 sin6->sin6_port = tcp->tcp_fport; 7740 7741 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7742 } 7743 } 7744 putnext(tcp->tcp_rq, ackmp); 7745 } 7746 7747 /* 7748 * Handle reinitialization of a tcp structure. 7749 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7750 */ 7751 static void 7752 tcp_reinit(tcp_t *tcp) 7753 { 7754 mblk_t *mp; 7755 int err; 7756 tcp_stack_t *tcps = tcp->tcp_tcps; 7757 7758 TCP_STAT(tcps, tcp_reinit_calls); 7759 7760 /* tcp_reinit should never be called for detached tcp_t's */ 7761 ASSERT(tcp->tcp_listener == NULL); 7762 ASSERT((tcp->tcp_family == AF_INET && 7763 tcp->tcp_ipversion == IPV4_VERSION) || 7764 (tcp->tcp_family == AF_INET6 && 7765 (tcp->tcp_ipversion == IPV4_VERSION || 7766 tcp->tcp_ipversion == IPV6_VERSION))); 7767 7768 /* Cancel outstanding timers */ 7769 tcp_timers_stop(tcp); 7770 7771 /* 7772 * Reset everything in the state vector, after updating global 7773 * MIB data from instance counters. 7774 */ 7775 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7776 tcp->tcp_ibsegs = 0; 7777 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7778 tcp->tcp_obsegs = 0; 7779 7780 tcp_close_mpp(&tcp->tcp_xmit_head); 7781 if (tcp->tcp_snd_zcopy_aware) 7782 tcp_zcopy_notify(tcp); 7783 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7784 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7785 mutex_enter(&tcp->tcp_non_sq_lock); 7786 if (tcp->tcp_flow_stopped && 7787 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7788 tcp_clrqfull(tcp); 7789 } 7790 mutex_exit(&tcp->tcp_non_sq_lock); 7791 tcp_close_mpp(&tcp->tcp_reass_head); 7792 tcp->tcp_reass_tail = NULL; 7793 if (tcp->tcp_rcv_list != NULL) { 7794 /* Free b_next chain */ 7795 tcp_close_mpp(&tcp->tcp_rcv_list); 7796 tcp->tcp_rcv_last_head = NULL; 7797 tcp->tcp_rcv_last_tail = NULL; 7798 tcp->tcp_rcv_cnt = 0; 7799 } 7800 tcp->tcp_rcv_last_tail = NULL; 7801 7802 if ((mp = tcp->tcp_urp_mp) != NULL) { 7803 freemsg(mp); 7804 tcp->tcp_urp_mp = NULL; 7805 } 7806 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7807 freemsg(mp); 7808 tcp->tcp_urp_mark_mp = NULL; 7809 } 7810 if (tcp->tcp_fused_sigurg_mp != NULL) { 7811 freeb(tcp->tcp_fused_sigurg_mp); 7812 tcp->tcp_fused_sigurg_mp = NULL; 7813 } 7814 if (tcp->tcp_ordrel_mp != NULL) { 7815 freeb(tcp->tcp_ordrel_mp); 7816 tcp->tcp_ordrel_mp = NULL; 7817 } 7818 7819 /* 7820 * Following is a union with two members which are 7821 * identical types and size so the following cleanup 7822 * is enough. 7823 */ 7824 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7825 7826 CL_INET_DISCONNECT(tcp); 7827 7828 /* 7829 * The connection can't be on the tcp_time_wait_head list 7830 * since it is not detached. 7831 */ 7832 ASSERT(tcp->tcp_time_wait_next == NULL); 7833 ASSERT(tcp->tcp_time_wait_prev == NULL); 7834 ASSERT(tcp->tcp_time_wait_expire == 0); 7835 7836 if (tcp->tcp_kssl_pending) { 7837 tcp->tcp_kssl_pending = B_FALSE; 7838 7839 /* Don't reset if the initialized by bind. */ 7840 if (tcp->tcp_kssl_ent != NULL) { 7841 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7842 KSSL_NO_PROXY); 7843 } 7844 } 7845 if (tcp->tcp_kssl_ctx != NULL) { 7846 kssl_release_ctx(tcp->tcp_kssl_ctx); 7847 tcp->tcp_kssl_ctx = NULL; 7848 } 7849 7850 /* 7851 * Reset/preserve other values 7852 */ 7853 tcp_reinit_values(tcp); 7854 ipcl_hash_remove(tcp->tcp_connp); 7855 conn_delete_ire(tcp->tcp_connp, NULL); 7856 tcp_ipsec_cleanup(tcp); 7857 7858 if (tcp->tcp_conn_req_max != 0) { 7859 /* 7860 * This is the case when a TLI program uses the same 7861 * transport end point to accept a connection. This 7862 * makes the TCP both a listener and acceptor. When 7863 * this connection is closed, we need to set the state 7864 * back to TCPS_LISTEN. Make sure that the eager list 7865 * is reinitialized. 7866 * 7867 * Note that this stream is still bound to the four 7868 * tuples of the previous connection in IP. If a new 7869 * SYN with different foreign address comes in, IP will 7870 * not find it and will send it to the global queue. In 7871 * the global queue, TCP will do a tcp_lookup_listener() 7872 * to find this stream. This works because this stream 7873 * is only removed from connected hash. 7874 * 7875 */ 7876 tcp->tcp_state = TCPS_LISTEN; 7877 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7878 tcp->tcp_eager_next_drop_q0 = tcp; 7879 tcp->tcp_eager_prev_drop_q0 = tcp; 7880 tcp->tcp_connp->conn_recv = tcp_conn_request; 7881 if (tcp->tcp_family == AF_INET6) { 7882 ASSERT(tcp->tcp_connp->conn_af_isv6); 7883 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7884 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7885 } else { 7886 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7887 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7888 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7889 } 7890 } else { 7891 tcp->tcp_state = TCPS_BOUND; 7892 } 7893 7894 /* 7895 * Initialize to default values 7896 * Can't fail since enough header template space already allocated 7897 * at open(). 7898 */ 7899 err = tcp_init_values(tcp); 7900 ASSERT(err == 0); 7901 /* Restore state in tcp_tcph */ 7902 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7903 if (tcp->tcp_ipversion == IPV4_VERSION) 7904 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7905 else 7906 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7907 /* 7908 * Copy of the src addr. in tcp_t is needed in tcp_t 7909 * since the lookup funcs can only lookup on tcp_t 7910 */ 7911 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7912 7913 ASSERT(tcp->tcp_ptpbhn != NULL); 7914 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7915 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7916 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7917 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7918 } 7919 7920 /* 7921 * Force values to zero that need be zero. 7922 * Do not touch values asociated with the BOUND or LISTEN state 7923 * since the connection will end up in that state after the reinit. 7924 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7925 * structure! 7926 */ 7927 static void 7928 tcp_reinit_values(tcp) 7929 tcp_t *tcp; 7930 { 7931 tcp_stack_t *tcps = tcp->tcp_tcps; 7932 7933 #ifndef lint 7934 #define DONTCARE(x) 7935 #define PRESERVE(x) 7936 #else 7937 #define DONTCARE(x) ((x) = (x)) 7938 #define PRESERVE(x) ((x) = (x)) 7939 #endif /* lint */ 7940 7941 PRESERVE(tcp->tcp_bind_hash); 7942 PRESERVE(tcp->tcp_ptpbhn); 7943 PRESERVE(tcp->tcp_acceptor_hash); 7944 PRESERVE(tcp->tcp_ptpahn); 7945 7946 /* Should be ASSERT NULL on these with new code! */ 7947 ASSERT(tcp->tcp_time_wait_next == NULL); 7948 ASSERT(tcp->tcp_time_wait_prev == NULL); 7949 ASSERT(tcp->tcp_time_wait_expire == 0); 7950 PRESERVE(tcp->tcp_state); 7951 PRESERVE(tcp->tcp_rq); 7952 PRESERVE(tcp->tcp_wq); 7953 7954 ASSERT(tcp->tcp_xmit_head == NULL); 7955 ASSERT(tcp->tcp_xmit_last == NULL); 7956 ASSERT(tcp->tcp_unsent == 0); 7957 ASSERT(tcp->tcp_xmit_tail == NULL); 7958 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7959 7960 tcp->tcp_snxt = 0; /* Displayed in mib */ 7961 tcp->tcp_suna = 0; /* Displayed in mib */ 7962 tcp->tcp_swnd = 0; 7963 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7964 7965 ASSERT(tcp->tcp_ibsegs == 0); 7966 ASSERT(tcp->tcp_obsegs == 0); 7967 7968 if (tcp->tcp_iphc != NULL) { 7969 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7970 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7971 } 7972 7973 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7974 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7975 DONTCARE(tcp->tcp_ipha); 7976 DONTCARE(tcp->tcp_ip6h); 7977 DONTCARE(tcp->tcp_ip_hdr_len); 7978 DONTCARE(tcp->tcp_tcph); 7979 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7980 tcp->tcp_valid_bits = 0; 7981 7982 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7983 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7984 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7985 tcp->tcp_last_rcv_lbolt = 0; 7986 7987 tcp->tcp_init_cwnd = 0; 7988 7989 tcp->tcp_urp_last_valid = 0; 7990 tcp->tcp_hard_binding = 0; 7991 tcp->tcp_hard_bound = 0; 7992 PRESERVE(tcp->tcp_cred); 7993 PRESERVE(tcp->tcp_cpid); 7994 PRESERVE(tcp->tcp_open_time); 7995 PRESERVE(tcp->tcp_exclbind); 7996 7997 tcp->tcp_fin_acked = 0; 7998 tcp->tcp_fin_rcvd = 0; 7999 tcp->tcp_fin_sent = 0; 8000 tcp->tcp_ordrel_done = 0; 8001 8002 tcp->tcp_debug = 0; 8003 tcp->tcp_dontroute = 0; 8004 tcp->tcp_broadcast = 0; 8005 8006 tcp->tcp_useloopback = 0; 8007 tcp->tcp_reuseaddr = 0; 8008 tcp->tcp_oobinline = 0; 8009 tcp->tcp_dgram_errind = 0; 8010 8011 tcp->tcp_detached = 0; 8012 tcp->tcp_bind_pending = 0; 8013 tcp->tcp_unbind_pending = 0; 8014 8015 tcp->tcp_snd_ws_ok = B_FALSE; 8016 tcp->tcp_snd_ts_ok = B_FALSE; 8017 tcp->tcp_linger = 0; 8018 tcp->tcp_ka_enabled = 0; 8019 tcp->tcp_zero_win_probe = 0; 8020 8021 tcp->tcp_loopback = 0; 8022 tcp->tcp_localnet = 0; 8023 tcp->tcp_syn_defense = 0; 8024 tcp->tcp_set_timer = 0; 8025 8026 tcp->tcp_active_open = 0; 8027 tcp->tcp_rexmit = B_FALSE; 8028 tcp->tcp_xmit_zc_clean = B_FALSE; 8029 8030 tcp->tcp_snd_sack_ok = B_FALSE; 8031 PRESERVE(tcp->tcp_recvdstaddr); 8032 tcp->tcp_hwcksum = B_FALSE; 8033 8034 tcp->tcp_ire_ill_check_done = B_FALSE; 8035 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 8036 8037 tcp->tcp_mdt = B_FALSE; 8038 tcp->tcp_mdt_hdr_head = 0; 8039 tcp->tcp_mdt_hdr_tail = 0; 8040 8041 tcp->tcp_conn_def_q0 = 0; 8042 tcp->tcp_ip_forward_progress = B_FALSE; 8043 tcp->tcp_anon_priv_bind = 0; 8044 tcp->tcp_ecn_ok = B_FALSE; 8045 8046 tcp->tcp_cwr = B_FALSE; 8047 tcp->tcp_ecn_echo_on = B_FALSE; 8048 8049 if (tcp->tcp_sack_info != NULL) { 8050 if (tcp->tcp_notsack_list != NULL) { 8051 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 8052 } 8053 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 8054 tcp->tcp_sack_info = NULL; 8055 } 8056 8057 tcp->tcp_rcv_ws = 0; 8058 tcp->tcp_snd_ws = 0; 8059 tcp->tcp_ts_recent = 0; 8060 tcp->tcp_rnxt = 0; /* Displayed in mib */ 8061 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 8062 tcp->tcp_if_mtu = 0; 8063 8064 ASSERT(tcp->tcp_reass_head == NULL); 8065 ASSERT(tcp->tcp_reass_tail == NULL); 8066 8067 tcp->tcp_cwnd_cnt = 0; 8068 8069 ASSERT(tcp->tcp_rcv_list == NULL); 8070 ASSERT(tcp->tcp_rcv_last_head == NULL); 8071 ASSERT(tcp->tcp_rcv_last_tail == NULL); 8072 ASSERT(tcp->tcp_rcv_cnt == 0); 8073 8074 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 8075 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 8076 tcp->tcp_csuna = 0; 8077 8078 tcp->tcp_rto = 0; /* Displayed in MIB */ 8079 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 8080 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 8081 tcp->tcp_rtt_update = 0; 8082 8083 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8084 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8085 8086 tcp->tcp_rack = 0; /* Displayed in mib */ 8087 tcp->tcp_rack_cnt = 0; 8088 tcp->tcp_rack_cur_max = 0; 8089 tcp->tcp_rack_abs_max = 0; 8090 8091 tcp->tcp_max_swnd = 0; 8092 8093 ASSERT(tcp->tcp_listener == NULL); 8094 8095 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 8096 8097 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 8098 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 8099 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 8100 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 8101 8102 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 8103 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 8104 PRESERVE(tcp->tcp_conn_req_max); 8105 PRESERVE(tcp->tcp_conn_req_seqnum); 8106 8107 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 8108 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 8109 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 8110 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 8111 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 8112 8113 tcp->tcp_lingertime = 0; 8114 8115 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 8116 ASSERT(tcp->tcp_urp_mp == NULL); 8117 ASSERT(tcp->tcp_urp_mark_mp == NULL); 8118 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 8119 8120 ASSERT(tcp->tcp_eager_next_q == NULL); 8121 ASSERT(tcp->tcp_eager_last_q == NULL); 8122 ASSERT((tcp->tcp_eager_next_q0 == NULL && 8123 tcp->tcp_eager_prev_q0 == NULL) || 8124 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 8125 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 8126 8127 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 8128 tcp->tcp_eager_prev_drop_q0 == NULL) || 8129 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 8130 8131 tcp->tcp_client_errno = 0; 8132 8133 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 8134 8135 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 8136 8137 PRESERVE(tcp->tcp_bound_source_v6); 8138 tcp->tcp_last_sent_len = 0; 8139 tcp->tcp_dupack_cnt = 0; 8140 8141 tcp->tcp_fport = 0; /* Displayed in MIB */ 8142 PRESERVE(tcp->tcp_lport); 8143 8144 PRESERVE(tcp->tcp_acceptor_lockp); 8145 8146 ASSERT(tcp->tcp_ordrel_mp == NULL); 8147 PRESERVE(tcp->tcp_acceptor_id); 8148 DONTCARE(tcp->tcp_ipsec_overhead); 8149 8150 PRESERVE(tcp->tcp_family); 8151 if (tcp->tcp_family == AF_INET6) { 8152 tcp->tcp_ipversion = IPV6_VERSION; 8153 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 8154 } else { 8155 tcp->tcp_ipversion = IPV4_VERSION; 8156 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 8157 } 8158 8159 tcp->tcp_bound_if = 0; 8160 tcp->tcp_ipv6_recvancillary = 0; 8161 tcp->tcp_recvifindex = 0; 8162 tcp->tcp_recvhops = 0; 8163 tcp->tcp_closed = 0; 8164 tcp->tcp_cleandeathtag = 0; 8165 if (tcp->tcp_hopopts != NULL) { 8166 mi_free(tcp->tcp_hopopts); 8167 tcp->tcp_hopopts = NULL; 8168 tcp->tcp_hopoptslen = 0; 8169 } 8170 ASSERT(tcp->tcp_hopoptslen == 0); 8171 if (tcp->tcp_dstopts != NULL) { 8172 mi_free(tcp->tcp_dstopts); 8173 tcp->tcp_dstopts = NULL; 8174 tcp->tcp_dstoptslen = 0; 8175 } 8176 ASSERT(tcp->tcp_dstoptslen == 0); 8177 if (tcp->tcp_rtdstopts != NULL) { 8178 mi_free(tcp->tcp_rtdstopts); 8179 tcp->tcp_rtdstopts = NULL; 8180 tcp->tcp_rtdstoptslen = 0; 8181 } 8182 ASSERT(tcp->tcp_rtdstoptslen == 0); 8183 if (tcp->tcp_rthdr != NULL) { 8184 mi_free(tcp->tcp_rthdr); 8185 tcp->tcp_rthdr = NULL; 8186 tcp->tcp_rthdrlen = 0; 8187 } 8188 ASSERT(tcp->tcp_rthdrlen == 0); 8189 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 8190 8191 /* Reset fusion-related fields */ 8192 tcp->tcp_fused = B_FALSE; 8193 tcp->tcp_unfusable = B_FALSE; 8194 tcp->tcp_fused_sigurg = B_FALSE; 8195 tcp->tcp_direct_sockfs = B_FALSE; 8196 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8197 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8198 tcp->tcp_loopback_peer = NULL; 8199 tcp->tcp_fuse_rcv_hiwater = 0; 8200 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8201 tcp->tcp_fuse_rcv_unread_cnt = 0; 8202 8203 tcp->tcp_lso = B_FALSE; 8204 8205 tcp->tcp_in_ack_unsent = 0; 8206 tcp->tcp_cork = B_FALSE; 8207 tcp->tcp_tconnind_started = B_FALSE; 8208 8209 PRESERVE(tcp->tcp_squeue_bytes); 8210 8211 ASSERT(tcp->tcp_kssl_ctx == NULL); 8212 ASSERT(!tcp->tcp_kssl_pending); 8213 PRESERVE(tcp->tcp_kssl_ent); 8214 8215 /* Sodirect */ 8216 tcp->tcp_sodirect = NULL; 8217 8218 tcp->tcp_closemp_used = B_FALSE; 8219 8220 PRESERVE(tcp->tcp_rsrv_mp); 8221 PRESERVE(tcp->tcp_rsrv_mp_lock); 8222 8223 #ifdef DEBUG 8224 DONTCARE(tcp->tcmp_stk[0]); 8225 #endif 8226 8227 8228 #undef DONTCARE 8229 #undef PRESERVE 8230 } 8231 8232 /* 8233 * Allocate necessary resources and initialize state vector. 8234 * Guaranteed not to fail so that when an error is returned, 8235 * the caller doesn't need to do any additional cleanup. 8236 */ 8237 int 8238 tcp_init(tcp_t *tcp, queue_t *q) 8239 { 8240 int err; 8241 8242 tcp->tcp_rq = q; 8243 tcp->tcp_wq = WR(q); 8244 tcp->tcp_state = TCPS_IDLE; 8245 if ((err = tcp_init_values(tcp)) != 0) 8246 tcp_timers_stop(tcp); 8247 return (err); 8248 } 8249 8250 static int 8251 tcp_init_values(tcp_t *tcp) 8252 { 8253 int err; 8254 tcp_stack_t *tcps = tcp->tcp_tcps; 8255 8256 ASSERT((tcp->tcp_family == AF_INET && 8257 tcp->tcp_ipversion == IPV4_VERSION) || 8258 (tcp->tcp_family == AF_INET6 && 8259 (tcp->tcp_ipversion == IPV4_VERSION || 8260 tcp->tcp_ipversion == IPV6_VERSION))); 8261 8262 /* 8263 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 8264 * will be close to tcp_rexmit_interval_initial. By doing this, we 8265 * allow the algorithm to adjust slowly to large fluctuations of RTT 8266 * during first few transmissions of a connection as seen in slow 8267 * links. 8268 */ 8269 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 8270 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 8271 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 8272 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 8273 tcps->tcps_conn_grace_period; 8274 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 8275 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 8276 tcp->tcp_timer_backoff = 0; 8277 tcp->tcp_ms_we_have_waited = 0; 8278 tcp->tcp_last_recv_time = lbolt; 8279 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 8280 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 8281 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 8282 8283 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 8284 8285 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 8286 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 8287 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 8288 /* 8289 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 8290 * passive open. 8291 */ 8292 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 8293 8294 tcp->tcp_naglim = tcps->tcps_naglim_def; 8295 8296 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 8297 8298 tcp->tcp_mdt_hdr_head = 0; 8299 tcp->tcp_mdt_hdr_tail = 0; 8300 8301 /* Reset fusion-related fields */ 8302 tcp->tcp_fused = B_FALSE; 8303 tcp->tcp_unfusable = B_FALSE; 8304 tcp->tcp_fused_sigurg = B_FALSE; 8305 tcp->tcp_direct_sockfs = B_FALSE; 8306 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8307 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8308 tcp->tcp_loopback_peer = NULL; 8309 tcp->tcp_fuse_rcv_hiwater = 0; 8310 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8311 tcp->tcp_fuse_rcv_unread_cnt = 0; 8312 8313 /* Sodirect */ 8314 tcp->tcp_sodirect = NULL; 8315 8316 /* Initialize the header template */ 8317 if (tcp->tcp_ipversion == IPV4_VERSION) { 8318 err = tcp_header_init_ipv4(tcp); 8319 } else { 8320 err = tcp_header_init_ipv6(tcp); 8321 } 8322 if (err) 8323 return (err); 8324 8325 /* 8326 * Init the window scale to the max so tcp_rwnd_set() won't pare 8327 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8328 */ 8329 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8330 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8331 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8332 8333 tcp->tcp_cork = B_FALSE; 8334 /* 8335 * Init the tcp_debug option. This value determines whether TCP 8336 * calls strlog() to print out debug messages. Doing this 8337 * initialization here means that this value is not inherited thru 8338 * tcp_reinit(). 8339 */ 8340 tcp->tcp_debug = tcps->tcps_dbg; 8341 8342 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8343 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8344 8345 return (0); 8346 } 8347 8348 /* 8349 * Initialize the IPv4 header. Loses any record of any IP options. 8350 */ 8351 static int 8352 tcp_header_init_ipv4(tcp_t *tcp) 8353 { 8354 tcph_t *tcph; 8355 uint32_t sum; 8356 conn_t *connp; 8357 tcp_stack_t *tcps = tcp->tcp_tcps; 8358 8359 /* 8360 * This is a simple initialization. If there's 8361 * already a template, it should never be too small, 8362 * so reuse it. Otherwise, allocate space for the new one. 8363 */ 8364 if (tcp->tcp_iphc == NULL) { 8365 ASSERT(tcp->tcp_iphc_len == 0); 8366 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8367 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8368 if (tcp->tcp_iphc == NULL) { 8369 tcp->tcp_iphc_len = 0; 8370 return (ENOMEM); 8371 } 8372 } 8373 8374 /* options are gone; may need a new label */ 8375 connp = tcp->tcp_connp; 8376 connp->conn_mlp_type = mlptSingle; 8377 connp->conn_ulp_labeled = !is_system_labeled(); 8378 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8379 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8380 tcp->tcp_ip6h = NULL; 8381 tcp->tcp_ipversion = IPV4_VERSION; 8382 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8383 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8384 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8385 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8386 tcp->tcp_ipha->ipha_version_and_hdr_length 8387 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8388 tcp->tcp_ipha->ipha_ident = 0; 8389 8390 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8391 tcp->tcp_tos = 0; 8392 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8393 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8394 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8395 8396 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8397 tcp->tcp_tcph = tcph; 8398 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8399 /* 8400 * IP wants our header length in the checksum field to 8401 * allow it to perform a single pseudo-header+checksum 8402 * calculation on behalf of TCP. 8403 * Include the adjustment for a source route once IP_OPTIONS is set. 8404 */ 8405 sum = sizeof (tcph_t) + tcp->tcp_sum; 8406 sum = (sum >> 16) + (sum & 0xFFFF); 8407 U16_TO_ABE16(sum, tcph->th_sum); 8408 return (0); 8409 } 8410 8411 /* 8412 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8413 */ 8414 static int 8415 tcp_header_init_ipv6(tcp_t *tcp) 8416 { 8417 tcph_t *tcph; 8418 uint32_t sum; 8419 conn_t *connp; 8420 tcp_stack_t *tcps = tcp->tcp_tcps; 8421 8422 /* 8423 * This is a simple initialization. If there's 8424 * already a template, it should never be too small, 8425 * so reuse it. Otherwise, allocate space for the new one. 8426 * Ensure that there is enough space to "downgrade" the tcp_t 8427 * to an IPv4 tcp_t. This requires having space for a full load 8428 * of IPv4 options, as well as a full load of TCP options 8429 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8430 * than a v6 header and a TCP header with a full load of TCP options 8431 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8432 * We want to avoid reallocation in the "downgraded" case when 8433 * processing outbound IPv4 options. 8434 */ 8435 if (tcp->tcp_iphc == NULL) { 8436 ASSERT(tcp->tcp_iphc_len == 0); 8437 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8438 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8439 if (tcp->tcp_iphc == NULL) { 8440 tcp->tcp_iphc_len = 0; 8441 return (ENOMEM); 8442 } 8443 } 8444 8445 /* options are gone; may need a new label */ 8446 connp = tcp->tcp_connp; 8447 connp->conn_mlp_type = mlptSingle; 8448 connp->conn_ulp_labeled = !is_system_labeled(); 8449 8450 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8451 tcp->tcp_ipversion = IPV6_VERSION; 8452 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8453 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8454 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8455 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8456 tcp->tcp_ipha = NULL; 8457 8458 /* Initialize the header template */ 8459 8460 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8461 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8462 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8463 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8464 8465 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8466 tcp->tcp_tcph = tcph; 8467 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8468 /* 8469 * IP wants our header length in the checksum field to 8470 * allow it to perform a single psuedo-header+checksum 8471 * calculation on behalf of TCP. 8472 * Include the adjustment for a source route when IPV6_RTHDR is set. 8473 */ 8474 sum = sizeof (tcph_t) + tcp->tcp_sum; 8475 sum = (sum >> 16) + (sum & 0xFFFF); 8476 U16_TO_ABE16(sum, tcph->th_sum); 8477 return (0); 8478 } 8479 8480 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8481 #define ICMP_MIN_TCP_HDR 8 8482 8483 /* 8484 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8485 * passed up by IP. The message is always received on the correct tcp_t. 8486 * Assumes that IP has pulled up everything up to and including the ICMP header. 8487 */ 8488 void 8489 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8490 { 8491 icmph_t *icmph; 8492 ipha_t *ipha; 8493 int iph_hdr_length; 8494 tcph_t *tcph; 8495 boolean_t ipsec_mctl = B_FALSE; 8496 boolean_t secure; 8497 mblk_t *first_mp = mp; 8498 uint32_t new_mss; 8499 uint32_t ratio; 8500 size_t mp_size = MBLKL(mp); 8501 uint32_t seg_seq; 8502 tcp_stack_t *tcps = tcp->tcp_tcps; 8503 8504 /* Assume IP provides aligned packets - otherwise toss */ 8505 if (!OK_32PTR(mp->b_rptr)) { 8506 freemsg(mp); 8507 return; 8508 } 8509 8510 /* 8511 * Since ICMP errors are normal data marked with M_CTL when sent 8512 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8513 * packets starting with an ipsec_info_t, see ipsec_info.h. 8514 */ 8515 if ((mp_size == sizeof (ipsec_info_t)) && 8516 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8517 ASSERT(mp->b_cont != NULL); 8518 mp = mp->b_cont; 8519 /* IP should have done this */ 8520 ASSERT(OK_32PTR(mp->b_rptr)); 8521 mp_size = MBLKL(mp); 8522 ipsec_mctl = B_TRUE; 8523 } 8524 8525 /* 8526 * Verify that we have a complete outer IP header. If not, drop it. 8527 */ 8528 if (mp_size < sizeof (ipha_t)) { 8529 noticmpv4: 8530 freemsg(first_mp); 8531 return; 8532 } 8533 8534 ipha = (ipha_t *)mp->b_rptr; 8535 /* 8536 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8537 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8538 */ 8539 switch (IPH_HDR_VERSION(ipha)) { 8540 case IPV6_VERSION: 8541 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8542 return; 8543 case IPV4_VERSION: 8544 break; 8545 default: 8546 goto noticmpv4; 8547 } 8548 8549 /* Skip past the outer IP and ICMP headers */ 8550 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8551 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8552 /* 8553 * If we don't have the correct outer IP header length or if the ULP 8554 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8555 * send it upstream. 8556 */ 8557 if (iph_hdr_length < sizeof (ipha_t) || 8558 ipha->ipha_protocol != IPPROTO_ICMP || 8559 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8560 goto noticmpv4; 8561 } 8562 ipha = (ipha_t *)&icmph[1]; 8563 8564 /* Skip past the inner IP and find the ULP header */ 8565 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8566 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8567 /* 8568 * If we don't have the correct inner IP header length or if the ULP 8569 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8570 * bytes of TCP header, drop it. 8571 */ 8572 if (iph_hdr_length < sizeof (ipha_t) || 8573 ipha->ipha_protocol != IPPROTO_TCP || 8574 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8575 goto noticmpv4; 8576 } 8577 8578 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8579 if (ipsec_mctl) { 8580 secure = ipsec_in_is_secure(first_mp); 8581 } else { 8582 secure = B_FALSE; 8583 } 8584 if (secure) { 8585 /* 8586 * If we are willing to accept this in clear 8587 * we don't have to verify policy. 8588 */ 8589 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8590 if (!tcp_check_policy(tcp, first_mp, 8591 ipha, NULL, secure, ipsec_mctl)) { 8592 /* 8593 * tcp_check_policy called 8594 * ip_drop_packet() on failure. 8595 */ 8596 return; 8597 } 8598 } 8599 } 8600 } else if (ipsec_mctl) { 8601 /* 8602 * This is a hard_bound connection. IP has already 8603 * verified policy. We don't have to do it again. 8604 */ 8605 freeb(first_mp); 8606 first_mp = mp; 8607 ipsec_mctl = B_FALSE; 8608 } 8609 8610 seg_seq = ABE32_TO_U32(tcph->th_seq); 8611 /* 8612 * TCP SHOULD check that the TCP sequence number contained in 8613 * payload of the ICMP error message is within the range 8614 * SND.UNA <= SEG.SEQ < SND.NXT. 8615 */ 8616 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8617 /* 8618 * If the ICMP message is bogus, should we kill the 8619 * connection, or should we just drop the bogus ICMP 8620 * message? It would probably make more sense to just 8621 * drop the message so that if this one managed to get 8622 * in, the real connection should not suffer. 8623 */ 8624 goto noticmpv4; 8625 } 8626 8627 switch (icmph->icmph_type) { 8628 case ICMP_DEST_UNREACHABLE: 8629 switch (icmph->icmph_code) { 8630 case ICMP_FRAGMENTATION_NEEDED: 8631 /* 8632 * Reduce the MSS based on the new MTU. This will 8633 * eliminate any fragmentation locally. 8634 * N.B. There may well be some funny side-effects on 8635 * the local send policy and the remote receive policy. 8636 * Pending further research, we provide 8637 * tcp_ignore_path_mtu just in case this proves 8638 * disastrous somewhere. 8639 * 8640 * After updating the MSS, retransmit part of the 8641 * dropped segment using the new mss by calling 8642 * tcp_wput_data(). Need to adjust all those 8643 * params to make sure tcp_wput_data() work properly. 8644 */ 8645 if (tcps->tcps_ignore_path_mtu) 8646 break; 8647 8648 /* 8649 * Decrease the MSS by time stamp options 8650 * IP options and IPSEC options. tcp_hdr_len 8651 * includes time stamp option and IP option 8652 * length. 8653 */ 8654 8655 new_mss = ntohs(icmph->icmph_du_mtu) - 8656 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8657 8658 /* 8659 * Only update the MSS if the new one is 8660 * smaller than the previous one. This is 8661 * to avoid problems when getting multiple 8662 * ICMP errors for the same MTU. 8663 */ 8664 if (new_mss >= tcp->tcp_mss) 8665 break; 8666 8667 /* 8668 * Stop doing PMTU if new_mss is less than 68 8669 * or less than tcp_mss_min. 8670 * The value 68 comes from rfc 1191. 8671 */ 8672 if (new_mss < MAX(68, tcps->tcps_mss_min)) 8673 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8674 0; 8675 8676 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8677 ASSERT(ratio >= 1); 8678 tcp_mss_set(tcp, new_mss, B_TRUE); 8679 8680 /* 8681 * Make sure we have something to 8682 * send. 8683 */ 8684 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8685 (tcp->tcp_xmit_head != NULL)) { 8686 /* 8687 * Shrink tcp_cwnd in 8688 * proportion to the old MSS/new MSS. 8689 */ 8690 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8691 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8692 (tcp->tcp_unsent == 0)) { 8693 tcp->tcp_rexmit_max = tcp->tcp_fss; 8694 } else { 8695 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8696 } 8697 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8698 tcp->tcp_rexmit = B_TRUE; 8699 tcp->tcp_dupack_cnt = 0; 8700 tcp->tcp_snd_burst = TCP_CWND_SS; 8701 tcp_ss_rexmit(tcp); 8702 } 8703 break; 8704 case ICMP_PORT_UNREACHABLE: 8705 case ICMP_PROTOCOL_UNREACHABLE: 8706 switch (tcp->tcp_state) { 8707 case TCPS_SYN_SENT: 8708 case TCPS_SYN_RCVD: 8709 /* 8710 * ICMP can snipe away incipient 8711 * TCP connections as long as 8712 * seq number is same as initial 8713 * send seq number. 8714 */ 8715 if (seg_seq == tcp->tcp_iss) { 8716 (void) tcp_clean_death(tcp, 8717 ECONNREFUSED, 6); 8718 } 8719 break; 8720 } 8721 break; 8722 case ICMP_HOST_UNREACHABLE: 8723 case ICMP_NET_UNREACHABLE: 8724 /* Record the error in case we finally time out. */ 8725 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8726 tcp->tcp_client_errno = EHOSTUNREACH; 8727 else 8728 tcp->tcp_client_errno = ENETUNREACH; 8729 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8730 if (tcp->tcp_listener != NULL && 8731 tcp->tcp_listener->tcp_syn_defense) { 8732 /* 8733 * Ditch the half-open connection if we 8734 * suspect a SYN attack is under way. 8735 */ 8736 tcp_ip_ire_mark_advice(tcp); 8737 (void) tcp_clean_death(tcp, 8738 tcp->tcp_client_errno, 7); 8739 } 8740 } 8741 break; 8742 default: 8743 break; 8744 } 8745 break; 8746 case ICMP_SOURCE_QUENCH: { 8747 /* 8748 * use a global boolean to control 8749 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8750 * The default is false. 8751 */ 8752 if (tcp_icmp_source_quench) { 8753 /* 8754 * Reduce the sending rate as if we got a 8755 * retransmit timeout 8756 */ 8757 uint32_t npkt; 8758 8759 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8760 tcp->tcp_mss; 8761 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8762 tcp->tcp_cwnd = tcp->tcp_mss; 8763 tcp->tcp_cwnd_cnt = 0; 8764 } 8765 break; 8766 } 8767 } 8768 freemsg(first_mp); 8769 } 8770 8771 /* 8772 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8773 * error messages passed up by IP. 8774 * Assumes that IP has pulled up all the extension headers as well 8775 * as the ICMPv6 header. 8776 */ 8777 static void 8778 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8779 { 8780 icmp6_t *icmp6; 8781 ip6_t *ip6h; 8782 uint16_t iph_hdr_length; 8783 tcpha_t *tcpha; 8784 uint8_t *nexthdrp; 8785 uint32_t new_mss; 8786 uint32_t ratio; 8787 boolean_t secure; 8788 mblk_t *first_mp = mp; 8789 size_t mp_size; 8790 uint32_t seg_seq; 8791 tcp_stack_t *tcps = tcp->tcp_tcps; 8792 8793 /* 8794 * The caller has determined if this is an IPSEC_IN packet and 8795 * set ipsec_mctl appropriately (see tcp_icmp_error). 8796 */ 8797 if (ipsec_mctl) 8798 mp = mp->b_cont; 8799 8800 mp_size = MBLKL(mp); 8801 8802 /* 8803 * Verify that we have a complete IP header. If not, send it upstream. 8804 */ 8805 if (mp_size < sizeof (ip6_t)) { 8806 noticmpv6: 8807 freemsg(first_mp); 8808 return; 8809 } 8810 8811 /* 8812 * Verify this is an ICMPV6 packet, else send it upstream. 8813 */ 8814 ip6h = (ip6_t *)mp->b_rptr; 8815 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8816 iph_hdr_length = IPV6_HDR_LEN; 8817 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8818 &nexthdrp) || 8819 *nexthdrp != IPPROTO_ICMPV6) { 8820 goto noticmpv6; 8821 } 8822 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8823 ip6h = (ip6_t *)&icmp6[1]; 8824 /* 8825 * Verify if we have a complete ICMP and inner IP header. 8826 */ 8827 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8828 goto noticmpv6; 8829 8830 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8831 goto noticmpv6; 8832 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8833 /* 8834 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8835 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8836 * packet. 8837 */ 8838 if ((*nexthdrp != IPPROTO_TCP) || 8839 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8840 goto noticmpv6; 8841 } 8842 8843 /* 8844 * ICMP errors come on the right queue or come on 8845 * listener/global queue for detached connections and 8846 * get switched to the right queue. If it comes on the 8847 * right queue, policy check has already been done by IP 8848 * and thus free the first_mp without verifying the policy. 8849 * If it has come for a non-hard bound connection, we need 8850 * to verify policy as IP may not have done it. 8851 */ 8852 if (!tcp->tcp_hard_bound) { 8853 if (ipsec_mctl) { 8854 secure = ipsec_in_is_secure(first_mp); 8855 } else { 8856 secure = B_FALSE; 8857 } 8858 if (secure) { 8859 /* 8860 * If we are willing to accept this in clear 8861 * we don't have to verify policy. 8862 */ 8863 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8864 if (!tcp_check_policy(tcp, first_mp, 8865 NULL, ip6h, secure, ipsec_mctl)) { 8866 /* 8867 * tcp_check_policy called 8868 * ip_drop_packet() on failure. 8869 */ 8870 return; 8871 } 8872 } 8873 } 8874 } else if (ipsec_mctl) { 8875 /* 8876 * This is a hard_bound connection. IP has already 8877 * verified policy. We don't have to do it again. 8878 */ 8879 freeb(first_mp); 8880 first_mp = mp; 8881 ipsec_mctl = B_FALSE; 8882 } 8883 8884 seg_seq = ntohl(tcpha->tha_seq); 8885 /* 8886 * TCP SHOULD check that the TCP sequence number contained in 8887 * payload of the ICMP error message is within the range 8888 * SND.UNA <= SEG.SEQ < SND.NXT. 8889 */ 8890 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8891 /* 8892 * If the ICMP message is bogus, should we kill the 8893 * connection, or should we just drop the bogus ICMP 8894 * message? It would probably make more sense to just 8895 * drop the message so that if this one managed to get 8896 * in, the real connection should not suffer. 8897 */ 8898 goto noticmpv6; 8899 } 8900 8901 switch (icmp6->icmp6_type) { 8902 case ICMP6_PACKET_TOO_BIG: 8903 /* 8904 * Reduce the MSS based on the new MTU. This will 8905 * eliminate any fragmentation locally. 8906 * N.B. There may well be some funny side-effects on 8907 * the local send policy and the remote receive policy. 8908 * Pending further research, we provide 8909 * tcp_ignore_path_mtu just in case this proves 8910 * disastrous somewhere. 8911 * 8912 * After updating the MSS, retransmit part of the 8913 * dropped segment using the new mss by calling 8914 * tcp_wput_data(). Need to adjust all those 8915 * params to make sure tcp_wput_data() work properly. 8916 */ 8917 if (tcps->tcps_ignore_path_mtu) 8918 break; 8919 8920 /* 8921 * Decrease the MSS by time stamp options 8922 * IP options and IPSEC options. tcp_hdr_len 8923 * includes time stamp option and IP option 8924 * length. 8925 */ 8926 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8927 tcp->tcp_ipsec_overhead; 8928 8929 /* 8930 * Only update the MSS if the new one is 8931 * smaller than the previous one. This is 8932 * to avoid problems when getting multiple 8933 * ICMP errors for the same MTU. 8934 */ 8935 if (new_mss >= tcp->tcp_mss) 8936 break; 8937 8938 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8939 ASSERT(ratio >= 1); 8940 tcp_mss_set(tcp, new_mss, B_TRUE); 8941 8942 /* 8943 * Make sure we have something to 8944 * send. 8945 */ 8946 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8947 (tcp->tcp_xmit_head != NULL)) { 8948 /* 8949 * Shrink tcp_cwnd in 8950 * proportion to the old MSS/new MSS. 8951 */ 8952 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8953 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8954 (tcp->tcp_unsent == 0)) { 8955 tcp->tcp_rexmit_max = tcp->tcp_fss; 8956 } else { 8957 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8958 } 8959 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8960 tcp->tcp_rexmit = B_TRUE; 8961 tcp->tcp_dupack_cnt = 0; 8962 tcp->tcp_snd_burst = TCP_CWND_SS; 8963 tcp_ss_rexmit(tcp); 8964 } 8965 break; 8966 8967 case ICMP6_DST_UNREACH: 8968 switch (icmp6->icmp6_code) { 8969 case ICMP6_DST_UNREACH_NOPORT: 8970 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8971 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8972 (seg_seq == tcp->tcp_iss)) { 8973 (void) tcp_clean_death(tcp, 8974 ECONNREFUSED, 8); 8975 } 8976 break; 8977 8978 case ICMP6_DST_UNREACH_ADMIN: 8979 case ICMP6_DST_UNREACH_NOROUTE: 8980 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8981 case ICMP6_DST_UNREACH_ADDR: 8982 /* Record the error in case we finally time out. */ 8983 tcp->tcp_client_errno = EHOSTUNREACH; 8984 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8985 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8986 (seg_seq == tcp->tcp_iss)) { 8987 if (tcp->tcp_listener != NULL && 8988 tcp->tcp_listener->tcp_syn_defense) { 8989 /* 8990 * Ditch the half-open connection if we 8991 * suspect a SYN attack is under way. 8992 */ 8993 tcp_ip_ire_mark_advice(tcp); 8994 (void) tcp_clean_death(tcp, 8995 tcp->tcp_client_errno, 9); 8996 } 8997 } 8998 8999 9000 break; 9001 default: 9002 break; 9003 } 9004 break; 9005 9006 case ICMP6_PARAM_PROB: 9007 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 9008 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 9009 (uchar_t *)ip6h + icmp6->icmp6_pptr == 9010 (uchar_t *)nexthdrp) { 9011 if (tcp->tcp_state == TCPS_SYN_SENT || 9012 tcp->tcp_state == TCPS_SYN_RCVD) { 9013 (void) tcp_clean_death(tcp, 9014 ECONNREFUSED, 10); 9015 } 9016 break; 9017 } 9018 break; 9019 9020 case ICMP6_TIME_EXCEEDED: 9021 default: 9022 break; 9023 } 9024 freemsg(first_mp); 9025 } 9026 9027 /* 9028 * IP recognizes seven kinds of bind requests: 9029 * 9030 * - A zero-length address binds only to the protocol number. 9031 * 9032 * - A 4-byte address is treated as a request to 9033 * validate that the address is a valid local IPv4 9034 * address, appropriate for an application to bind to. 9035 * IP does the verification, but does not make any note 9036 * of the address at this time. 9037 * 9038 * - A 16-byte address contains is treated as a request 9039 * to validate a local IPv6 address, as the 4-byte 9040 * address case above. 9041 * 9042 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 9043 * use it for the inbound fanout of packets. 9044 * 9045 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 9046 * use it for the inbound fanout of packets. 9047 * 9048 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 9049 * information consisting of local and remote addresses 9050 * and ports. In this case, the addresses are both 9051 * validated as appropriate for this operation, and, if 9052 * so, the information is retained for use in the 9053 * inbound fanout. 9054 * 9055 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 9056 * fanout information, like the 12-byte case above. 9057 * 9058 * IP will also fill in the IRE request mblk with information 9059 * regarding our peer. In all cases, we notify IP of our protocol 9060 * type by appending a single protocol byte to the bind request. 9061 */ 9062 static mblk_t * 9063 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 9064 { 9065 char *cp; 9066 mblk_t *mp; 9067 struct T_bind_req *tbr; 9068 ipa_conn_t *ac; 9069 ipa6_conn_t *ac6; 9070 sin_t *sin; 9071 sin6_t *sin6; 9072 9073 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 9074 ASSERT((tcp->tcp_family == AF_INET && 9075 tcp->tcp_ipversion == IPV4_VERSION) || 9076 (tcp->tcp_family == AF_INET6 && 9077 (tcp->tcp_ipversion == IPV4_VERSION || 9078 tcp->tcp_ipversion == IPV6_VERSION))); 9079 9080 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 9081 if (!mp) 9082 return (mp); 9083 mp->b_datap->db_type = M_PROTO; 9084 tbr = (struct T_bind_req *)mp->b_rptr; 9085 tbr->PRIM_type = bind_prim; 9086 tbr->ADDR_offset = sizeof (*tbr); 9087 tbr->CONIND_number = 0; 9088 tbr->ADDR_length = addr_length; 9089 cp = (char *)&tbr[1]; 9090 switch (addr_length) { 9091 case sizeof (ipa_conn_t): 9092 ASSERT(tcp->tcp_family == AF_INET); 9093 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9094 9095 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9096 if (mp->b_cont == NULL) { 9097 freemsg(mp); 9098 return (NULL); 9099 } 9100 mp->b_cont->b_wptr += sizeof (ire_t); 9101 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9102 9103 /* cp known to be 32 bit aligned */ 9104 ac = (ipa_conn_t *)cp; 9105 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 9106 ac->ac_faddr = tcp->tcp_remote; 9107 ac->ac_fport = tcp->tcp_fport; 9108 ac->ac_lport = tcp->tcp_lport; 9109 tcp->tcp_hard_binding = 1; 9110 break; 9111 9112 case sizeof (ipa6_conn_t): 9113 ASSERT(tcp->tcp_family == AF_INET6); 9114 9115 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9116 if (mp->b_cont == NULL) { 9117 freemsg(mp); 9118 return (NULL); 9119 } 9120 mp->b_cont->b_wptr += sizeof (ire_t); 9121 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9122 9123 /* cp known to be 32 bit aligned */ 9124 ac6 = (ipa6_conn_t *)cp; 9125 if (tcp->tcp_ipversion == IPV4_VERSION) { 9126 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 9127 &ac6->ac6_laddr); 9128 } else { 9129 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 9130 } 9131 ac6->ac6_faddr = tcp->tcp_remote_v6; 9132 ac6->ac6_fport = tcp->tcp_fport; 9133 ac6->ac6_lport = tcp->tcp_lport; 9134 tcp->tcp_hard_binding = 1; 9135 break; 9136 9137 case sizeof (sin_t): 9138 /* 9139 * NOTE: IPV6_ADDR_LEN also has same size. 9140 * Use family to discriminate. 9141 */ 9142 if (tcp->tcp_family == AF_INET) { 9143 sin = (sin_t *)cp; 9144 9145 *sin = sin_null; 9146 sin->sin_family = AF_INET; 9147 sin->sin_addr.s_addr = tcp->tcp_bound_source; 9148 sin->sin_port = tcp->tcp_lport; 9149 break; 9150 } else { 9151 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 9152 } 9153 break; 9154 9155 case sizeof (sin6_t): 9156 ASSERT(tcp->tcp_family == AF_INET6); 9157 sin6 = (sin6_t *)cp; 9158 9159 *sin6 = sin6_null; 9160 sin6->sin6_family = AF_INET6; 9161 sin6->sin6_addr = tcp->tcp_bound_source_v6; 9162 sin6->sin6_port = tcp->tcp_lport; 9163 break; 9164 9165 case IP_ADDR_LEN: 9166 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9167 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 9168 break; 9169 9170 } 9171 /* Add protocol number to end */ 9172 cp[addr_length] = (char)IPPROTO_TCP; 9173 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 9174 return (mp); 9175 } 9176 9177 /* 9178 * Notify IP that we are having trouble with this connection. IP should 9179 * blow the IRE away and start over. 9180 */ 9181 static void 9182 tcp_ip_notify(tcp_t *tcp) 9183 { 9184 struct iocblk *iocp; 9185 ipid_t *ipid; 9186 mblk_t *mp; 9187 9188 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 9189 if (tcp->tcp_ipversion == IPV6_VERSION) 9190 return; 9191 9192 mp = mkiocb(IP_IOCTL); 9193 if (mp == NULL) 9194 return; 9195 9196 iocp = (struct iocblk *)mp->b_rptr; 9197 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 9198 9199 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 9200 if (!mp->b_cont) { 9201 freeb(mp); 9202 return; 9203 } 9204 9205 ipid = (ipid_t *)mp->b_cont->b_rptr; 9206 mp->b_cont->b_wptr += iocp->ioc_count; 9207 bzero(ipid, sizeof (*ipid)); 9208 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 9209 ipid->ipid_ire_type = IRE_CACHE; 9210 ipid->ipid_addr_offset = sizeof (ipid_t); 9211 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 9212 /* 9213 * Note: in the case of source routing we want to blow away the 9214 * route to the first source route hop. 9215 */ 9216 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 9217 sizeof (tcp->tcp_ipha->ipha_dst)); 9218 9219 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 9220 } 9221 9222 /* Unlink and return any mblk that looks like it contains an ire */ 9223 static mblk_t * 9224 tcp_ire_mp(mblk_t *mp) 9225 { 9226 mblk_t *prev_mp; 9227 9228 for (;;) { 9229 prev_mp = mp; 9230 mp = mp->b_cont; 9231 if (mp == NULL) 9232 break; 9233 switch (DB_TYPE(mp)) { 9234 case IRE_DB_TYPE: 9235 case IRE_DB_REQ_TYPE: 9236 if (prev_mp != NULL) 9237 prev_mp->b_cont = mp->b_cont; 9238 mp->b_cont = NULL; 9239 return (mp); 9240 default: 9241 break; 9242 } 9243 } 9244 return (mp); 9245 } 9246 9247 /* 9248 * Timer callback routine for keepalive probe. We do a fake resend of 9249 * last ACKed byte. Then set a timer using RTO. When the timer expires, 9250 * check to see if we have heard anything from the other end for the last 9251 * RTO period. If we have, set the timer to expire for another 9252 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 9253 * RTO << 1 and check again when it expires. Keep exponentially increasing 9254 * the timeout if we have not heard from the other side. If for more than 9255 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 9256 * kill the connection unless the keepalive abort threshold is 0. In 9257 * that case, we will probe "forever." 9258 */ 9259 static void 9260 tcp_keepalive_killer(void *arg) 9261 { 9262 mblk_t *mp; 9263 conn_t *connp = (conn_t *)arg; 9264 tcp_t *tcp = connp->conn_tcp; 9265 int32_t firetime; 9266 int32_t idletime; 9267 int32_t ka_intrvl; 9268 tcp_stack_t *tcps = tcp->tcp_tcps; 9269 9270 tcp->tcp_ka_tid = 0; 9271 9272 if (tcp->tcp_fused) 9273 return; 9274 9275 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 9276 ka_intrvl = tcp->tcp_ka_interval; 9277 9278 /* 9279 * Keepalive probe should only be sent if the application has not 9280 * done a close on the connection. 9281 */ 9282 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 9283 return; 9284 } 9285 /* Timer fired too early, restart it. */ 9286 if (tcp->tcp_state < TCPS_ESTABLISHED) { 9287 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9288 MSEC_TO_TICK(ka_intrvl)); 9289 return; 9290 } 9291 9292 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 9293 /* 9294 * If we have not heard from the other side for a long 9295 * time, kill the connection unless the keepalive abort 9296 * threshold is 0. In that case, we will probe "forever." 9297 */ 9298 if (tcp->tcp_ka_abort_thres != 0 && 9299 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 9300 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 9301 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 9302 tcp->tcp_client_errno : ETIMEDOUT, 11); 9303 return; 9304 } 9305 9306 if (tcp->tcp_snxt == tcp->tcp_suna && 9307 idletime >= ka_intrvl) { 9308 /* Fake resend of last ACKed byte. */ 9309 mblk_t *mp1 = allocb(1, BPRI_LO); 9310 9311 if (mp1 != NULL) { 9312 *mp1->b_wptr++ = '\0'; 9313 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 9314 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 9315 freeb(mp1); 9316 /* 9317 * if allocation failed, fall through to start the 9318 * timer back. 9319 */ 9320 if (mp != NULL) { 9321 tcp_send_data(tcp, tcp->tcp_wq, mp); 9322 BUMP_MIB(&tcps->tcps_mib, 9323 tcpTimKeepaliveProbe); 9324 if (tcp->tcp_ka_last_intrvl != 0) { 9325 int max; 9326 /* 9327 * We should probe again at least 9328 * in ka_intrvl, but not more than 9329 * tcp_rexmit_interval_max. 9330 */ 9331 max = tcps->tcps_rexmit_interval_max; 9332 firetime = MIN(ka_intrvl - 1, 9333 tcp->tcp_ka_last_intrvl << 1); 9334 if (firetime > max) 9335 firetime = max; 9336 } else { 9337 firetime = tcp->tcp_rto; 9338 } 9339 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9340 tcp_keepalive_killer, 9341 MSEC_TO_TICK(firetime)); 9342 tcp->tcp_ka_last_intrvl = firetime; 9343 return; 9344 } 9345 } 9346 } else { 9347 tcp->tcp_ka_last_intrvl = 0; 9348 } 9349 9350 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9351 if ((firetime = ka_intrvl - idletime) < 0) { 9352 firetime = ka_intrvl; 9353 } 9354 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9355 MSEC_TO_TICK(firetime)); 9356 } 9357 9358 int 9359 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9360 { 9361 queue_t *q = tcp->tcp_rq; 9362 int32_t mss = tcp->tcp_mss; 9363 int maxpsz; 9364 9365 if (TCP_IS_DETACHED(tcp)) 9366 return (mss); 9367 9368 if (tcp->tcp_fused) { 9369 maxpsz = tcp_fuse_maxpsz_set(tcp); 9370 mss = INFPSZ; 9371 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 9372 /* 9373 * Set the sd_qn_maxpsz according to the socket send buffer 9374 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9375 * instruct the stream head to copyin user data into contiguous 9376 * kernel-allocated buffers without breaking it up into smaller 9377 * chunks. We round up the buffer size to the nearest SMSS. 9378 */ 9379 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9380 if (tcp->tcp_kssl_ctx == NULL) 9381 mss = INFPSZ; 9382 else 9383 mss = SSL3_MAX_RECORD_LEN; 9384 } else { 9385 /* 9386 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9387 * (and a multiple of the mss). This instructs the stream 9388 * head to break down larger than SMSS writes into SMSS- 9389 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9390 */ 9391 maxpsz = tcp->tcp_maxpsz * mss; 9392 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9393 maxpsz = tcp->tcp_xmit_hiwater/2; 9394 /* Round up to nearest mss */ 9395 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9396 } 9397 } 9398 (void) setmaxps(q, maxpsz); 9399 tcp->tcp_wq->q_maxpsz = maxpsz; 9400 9401 if (set_maxblk) 9402 (void) mi_set_sth_maxblk(q, mss); 9403 9404 return (mss); 9405 } 9406 9407 /* 9408 * Extract option values from a tcp header. We put any found values into the 9409 * tcpopt struct and return a bitmask saying which options were found. 9410 */ 9411 static int 9412 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9413 { 9414 uchar_t *endp; 9415 int len; 9416 uint32_t mss; 9417 uchar_t *up = (uchar_t *)tcph; 9418 int found = 0; 9419 int32_t sack_len; 9420 tcp_seq sack_begin, sack_end; 9421 tcp_t *tcp; 9422 9423 endp = up + TCP_HDR_LENGTH(tcph); 9424 up += TCP_MIN_HEADER_LENGTH; 9425 while (up < endp) { 9426 len = endp - up; 9427 switch (*up) { 9428 case TCPOPT_EOL: 9429 break; 9430 9431 case TCPOPT_NOP: 9432 up++; 9433 continue; 9434 9435 case TCPOPT_MAXSEG: 9436 if (len < TCPOPT_MAXSEG_LEN || 9437 up[1] != TCPOPT_MAXSEG_LEN) 9438 break; 9439 9440 mss = BE16_TO_U16(up+2); 9441 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9442 tcpopt->tcp_opt_mss = mss; 9443 found |= TCP_OPT_MSS_PRESENT; 9444 9445 up += TCPOPT_MAXSEG_LEN; 9446 continue; 9447 9448 case TCPOPT_WSCALE: 9449 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9450 break; 9451 9452 if (up[2] > TCP_MAX_WINSHIFT) 9453 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9454 else 9455 tcpopt->tcp_opt_wscale = up[2]; 9456 found |= TCP_OPT_WSCALE_PRESENT; 9457 9458 up += TCPOPT_WS_LEN; 9459 continue; 9460 9461 case TCPOPT_SACK_PERMITTED: 9462 if (len < TCPOPT_SACK_OK_LEN || 9463 up[1] != TCPOPT_SACK_OK_LEN) 9464 break; 9465 found |= TCP_OPT_SACK_OK_PRESENT; 9466 up += TCPOPT_SACK_OK_LEN; 9467 continue; 9468 9469 case TCPOPT_SACK: 9470 if (len <= 2 || up[1] <= 2 || len < up[1]) 9471 break; 9472 9473 /* If TCP is not interested in SACK blks... */ 9474 if ((tcp = tcpopt->tcp) == NULL) { 9475 up += up[1]; 9476 continue; 9477 } 9478 sack_len = up[1] - TCPOPT_HEADER_LEN; 9479 up += TCPOPT_HEADER_LEN; 9480 9481 /* 9482 * If the list is empty, allocate one and assume 9483 * nothing is sack'ed. 9484 */ 9485 ASSERT(tcp->tcp_sack_info != NULL); 9486 if (tcp->tcp_notsack_list == NULL) { 9487 tcp_notsack_update(&(tcp->tcp_notsack_list), 9488 tcp->tcp_suna, tcp->tcp_snxt, 9489 &(tcp->tcp_num_notsack_blk), 9490 &(tcp->tcp_cnt_notsack_list)); 9491 9492 /* 9493 * Make sure tcp_notsack_list is not NULL. 9494 * This happens when kmem_alloc(KM_NOSLEEP) 9495 * returns NULL. 9496 */ 9497 if (tcp->tcp_notsack_list == NULL) { 9498 up += sack_len; 9499 continue; 9500 } 9501 tcp->tcp_fack = tcp->tcp_suna; 9502 } 9503 9504 while (sack_len > 0) { 9505 if (up + 8 > endp) { 9506 up = endp; 9507 break; 9508 } 9509 sack_begin = BE32_TO_U32(up); 9510 up += 4; 9511 sack_end = BE32_TO_U32(up); 9512 up += 4; 9513 sack_len -= 8; 9514 /* 9515 * Bounds checking. Make sure the SACK 9516 * info is within tcp_suna and tcp_snxt. 9517 * If this SACK blk is out of bound, ignore 9518 * it but continue to parse the following 9519 * blks. 9520 */ 9521 if (SEQ_LEQ(sack_end, sack_begin) || 9522 SEQ_LT(sack_begin, tcp->tcp_suna) || 9523 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9524 continue; 9525 } 9526 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9527 sack_begin, sack_end, 9528 &(tcp->tcp_num_notsack_blk), 9529 &(tcp->tcp_cnt_notsack_list)); 9530 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9531 tcp->tcp_fack = sack_end; 9532 } 9533 } 9534 found |= TCP_OPT_SACK_PRESENT; 9535 continue; 9536 9537 case TCPOPT_TSTAMP: 9538 if (len < TCPOPT_TSTAMP_LEN || 9539 up[1] != TCPOPT_TSTAMP_LEN) 9540 break; 9541 9542 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9543 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9544 9545 found |= TCP_OPT_TSTAMP_PRESENT; 9546 9547 up += TCPOPT_TSTAMP_LEN; 9548 continue; 9549 9550 default: 9551 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9552 break; 9553 up += up[1]; 9554 continue; 9555 } 9556 break; 9557 } 9558 return (found); 9559 } 9560 9561 /* 9562 * Set the mss associated with a particular tcp based on its current value, 9563 * and a new one passed in. Observe minimums and maximums, and reset 9564 * other state variables that we want to view as multiples of mss. 9565 * 9566 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9567 * highwater marks etc. need to be initialized or adjusted. 9568 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9569 * packet arrives. 9570 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9571 * ICMP6_PACKET_TOO_BIG arrives. 9572 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9573 * to increase the MSS to use the extra bytes available. 9574 * 9575 * Callers except tcp_paws_check() ensure that they only reduce mss. 9576 */ 9577 static void 9578 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9579 { 9580 uint32_t mss_max; 9581 tcp_stack_t *tcps = tcp->tcp_tcps; 9582 9583 if (tcp->tcp_ipversion == IPV4_VERSION) 9584 mss_max = tcps->tcps_mss_max_ipv4; 9585 else 9586 mss_max = tcps->tcps_mss_max_ipv6; 9587 9588 if (mss < tcps->tcps_mss_min) 9589 mss = tcps->tcps_mss_min; 9590 if (mss > mss_max) 9591 mss = mss_max; 9592 /* 9593 * Unless naglim has been set by our client to 9594 * a non-mss value, force naglim to track mss. 9595 * This can help to aggregate small writes. 9596 */ 9597 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9598 tcp->tcp_naglim = mss; 9599 /* 9600 * TCP should be able to buffer at least 4 MSS data for obvious 9601 * performance reason. 9602 */ 9603 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9604 tcp->tcp_xmit_hiwater = mss << 2; 9605 9606 if (do_ss) { 9607 /* 9608 * Either the tcp_cwnd is as yet uninitialized, or mss is 9609 * changing due to a reduction in MTU, presumably as a 9610 * result of a new path component, reset cwnd to its 9611 * "initial" value, as a multiple of the new mss. 9612 */ 9613 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9614 } else { 9615 /* 9616 * Called by tcp_paws_check(), the mss increased 9617 * marginally to allow use of space previously taken 9618 * by the timestamp option. It would be inappropriate 9619 * to apply slow start or tcp_init_cwnd values to 9620 * tcp_cwnd, simply adjust to a multiple of the new mss. 9621 */ 9622 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9623 tcp->tcp_cwnd_cnt = 0; 9624 } 9625 tcp->tcp_mss = mss; 9626 (void) tcp_maxpsz_set(tcp, B_TRUE); 9627 } 9628 9629 /* For /dev/tcp aka AF_INET open */ 9630 static int 9631 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9632 { 9633 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9634 } 9635 9636 /* For /dev/tcp6 aka AF_INET6 open */ 9637 static int 9638 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9639 { 9640 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9641 } 9642 9643 static int 9644 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9645 boolean_t isv6) 9646 { 9647 tcp_t *tcp = NULL; 9648 conn_t *connp; 9649 int err; 9650 vmem_t *minor_arena = NULL; 9651 dev_t conn_dev; 9652 zoneid_t zoneid; 9653 tcp_stack_t *tcps = NULL; 9654 9655 if (q->q_ptr != NULL) 9656 return (0); 9657 9658 if (sflag == MODOPEN) 9659 return (EINVAL); 9660 9661 if (!(flag & SO_ACCEPTOR)) { 9662 /* 9663 * Special case for install: miniroot needs to be able to 9664 * access files via NFS as though it were always in the 9665 * global zone. 9666 */ 9667 if (credp == kcred && nfs_global_client_only != 0) { 9668 zoneid = GLOBAL_ZONEID; 9669 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9670 netstack_tcp; 9671 ASSERT(tcps != NULL); 9672 } else { 9673 netstack_t *ns; 9674 9675 ns = netstack_find_by_cred(credp); 9676 ASSERT(ns != NULL); 9677 tcps = ns->netstack_tcp; 9678 ASSERT(tcps != NULL); 9679 9680 /* 9681 * For exclusive stacks we set the zoneid to zero 9682 * to make TCP operate as if in the global zone. 9683 */ 9684 if (tcps->tcps_netstack->netstack_stackid != 9685 GLOBAL_NETSTACKID) 9686 zoneid = GLOBAL_ZONEID; 9687 else 9688 zoneid = crgetzoneid(credp); 9689 } 9690 /* 9691 * For stackid zero this is done from strplumb.c, but 9692 * non-zero stackids are handled here. 9693 */ 9694 if (tcps->tcps_g_q == NULL && 9695 tcps->tcps_netstack->netstack_stackid != 9696 GLOBAL_NETSTACKID) { 9697 tcp_g_q_setup(tcps); 9698 } 9699 } 9700 9701 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9702 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9703 minor_arena = ip_minor_arena_la; 9704 } else { 9705 /* 9706 * Either minor numbers in the large arena were exhausted 9707 * or a non socket application is doing the open. 9708 * Try to allocate from the small arena. 9709 */ 9710 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9711 if (tcps != NULL) 9712 netstack_rele(tcps->tcps_netstack); 9713 return (EBUSY); 9714 } 9715 minor_arena = ip_minor_arena_sa; 9716 } 9717 ASSERT(minor_arena != NULL); 9718 9719 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9720 9721 if (flag & SO_ACCEPTOR) { 9722 /* No netstack_find_by_cred, hence no netstack_rele needed */ 9723 ASSERT(tcps == NULL); 9724 q->q_qinfo = &tcp_acceptor_rinit; 9725 /* 9726 * the conn_dev and minor_arena will be subsequently used by 9727 * tcp_wput_accept() and tcpclose_accept() to figure out the 9728 * minor device number for this connection from the q_ptr. 9729 */ 9730 RD(q)->q_ptr = (void *)conn_dev; 9731 WR(q)->q_qinfo = &tcp_acceptor_winit; 9732 WR(q)->q_ptr = (void *)minor_arena; 9733 qprocson(q); 9734 return (0); 9735 } 9736 9737 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9738 /* 9739 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9740 * so we drop it by one. 9741 */ 9742 netstack_rele(tcps->tcps_netstack); 9743 if (connp == NULL) { 9744 inet_minor_free(minor_arena, conn_dev); 9745 q->q_ptr = NULL; 9746 return (ENOSR); 9747 } 9748 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9749 tcp = connp->conn_tcp; 9750 9751 q->q_ptr = WR(q)->q_ptr = connp; 9752 if (isv6) { 9753 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9754 connp->conn_send = ip_output_v6; 9755 connp->conn_af_isv6 = B_TRUE; 9756 connp->conn_pkt_isv6 = B_TRUE; 9757 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9758 tcp->tcp_ipversion = IPV6_VERSION; 9759 tcp->tcp_family = AF_INET6; 9760 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9761 } else { 9762 connp->conn_flags |= IPCL_TCP4; 9763 connp->conn_send = ip_output; 9764 connp->conn_af_isv6 = B_FALSE; 9765 connp->conn_pkt_isv6 = B_FALSE; 9766 tcp->tcp_ipversion = IPV4_VERSION; 9767 tcp->tcp_family = AF_INET; 9768 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9769 } 9770 9771 /* 9772 * TCP keeps a copy of cred for cache locality reasons but 9773 * we put a reference only once. If connp->conn_cred 9774 * becomes invalid, tcp_cred should also be set to NULL. 9775 */ 9776 tcp->tcp_cred = connp->conn_cred = credp; 9777 crhold(connp->conn_cred); 9778 tcp->tcp_cpid = curproc->p_pid; 9779 tcp->tcp_open_time = lbolt64; 9780 connp->conn_zoneid = zoneid; 9781 connp->conn_mlp_type = mlptSingle; 9782 connp->conn_ulp_labeled = !is_system_labeled(); 9783 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9784 ASSERT(tcp->tcp_tcps == tcps); 9785 9786 /* 9787 * If the caller has the process-wide flag set, then default to MAC 9788 * exempt mode. This allows read-down to unlabeled hosts. 9789 */ 9790 if (getpflags(NET_MAC_AWARE, credp) != 0) 9791 connp->conn_mac_exempt = B_TRUE; 9792 9793 connp->conn_dev = conn_dev; 9794 connp->conn_minor_arena = minor_arena; 9795 9796 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9797 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9798 9799 if (flag & SO_SOCKSTR) { 9800 /* 9801 * No need to insert a socket in tcp acceptor hash. 9802 * If it was a socket acceptor stream, we dealt with 9803 * it above. A socket listener can never accept a 9804 * connection and doesn't need acceptor_id. 9805 */ 9806 connp->conn_flags |= IPCL_SOCKET; 9807 tcp->tcp_issocket = 1; 9808 WR(q)->q_qinfo = &tcp_sock_winit; 9809 } else { 9810 #ifdef _ILP32 9811 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9812 #else 9813 tcp->tcp_acceptor_id = conn_dev; 9814 #endif /* _ILP32 */ 9815 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9816 } 9817 9818 err = tcp_init(tcp, q); 9819 if (err != 0) { 9820 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 9821 tcp_acceptor_hash_remove(tcp); 9822 CONN_DEC_REF(connp); 9823 q->q_ptr = WR(q)->q_ptr = NULL; 9824 return (err); 9825 } 9826 9827 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9828 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9829 9830 /* Non-zero default values */ 9831 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9832 /* 9833 * Put the ref for TCP. Ref for IP was already put 9834 * by ipcl_conn_create. Also Make the conn_t globally 9835 * visible to walkers 9836 */ 9837 mutex_enter(&connp->conn_lock); 9838 CONN_INC_REF_LOCKED(connp); 9839 ASSERT(connp->conn_ref == 2); 9840 connp->conn_state_flags &= ~CONN_INCIPIENT; 9841 mutex_exit(&connp->conn_lock); 9842 9843 qprocson(q); 9844 return (0); 9845 } 9846 9847 /* 9848 * Some TCP options can be "set" by requesting them in the option 9849 * buffer. This is needed for XTI feature test though we do not 9850 * allow it in general. We interpret that this mechanism is more 9851 * applicable to OSI protocols and need not be allowed in general. 9852 * This routine filters out options for which it is not allowed (most) 9853 * and lets through those (few) for which it is. [ The XTI interface 9854 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9855 * ever implemented will have to be allowed here ]. 9856 */ 9857 static boolean_t 9858 tcp_allow_connopt_set(int level, int name) 9859 { 9860 9861 switch (level) { 9862 case IPPROTO_TCP: 9863 switch (name) { 9864 case TCP_NODELAY: 9865 return (B_TRUE); 9866 default: 9867 return (B_FALSE); 9868 } 9869 /*NOTREACHED*/ 9870 default: 9871 return (B_FALSE); 9872 } 9873 /*NOTREACHED*/ 9874 } 9875 9876 /* 9877 * This routine gets default values of certain options whose default 9878 * values are maintained by protocol specific code 9879 */ 9880 /* ARGSUSED */ 9881 int 9882 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9883 { 9884 int32_t *i1 = (int32_t *)ptr; 9885 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9886 9887 switch (level) { 9888 case IPPROTO_TCP: 9889 switch (name) { 9890 case TCP_NOTIFY_THRESHOLD: 9891 *i1 = tcps->tcps_ip_notify_interval; 9892 break; 9893 case TCP_ABORT_THRESHOLD: 9894 *i1 = tcps->tcps_ip_abort_interval; 9895 break; 9896 case TCP_CONN_NOTIFY_THRESHOLD: 9897 *i1 = tcps->tcps_ip_notify_cinterval; 9898 break; 9899 case TCP_CONN_ABORT_THRESHOLD: 9900 *i1 = tcps->tcps_ip_abort_cinterval; 9901 break; 9902 default: 9903 return (-1); 9904 } 9905 break; 9906 case IPPROTO_IP: 9907 switch (name) { 9908 case IP_TTL: 9909 *i1 = tcps->tcps_ipv4_ttl; 9910 break; 9911 default: 9912 return (-1); 9913 } 9914 break; 9915 case IPPROTO_IPV6: 9916 switch (name) { 9917 case IPV6_UNICAST_HOPS: 9918 *i1 = tcps->tcps_ipv6_hoplimit; 9919 break; 9920 default: 9921 return (-1); 9922 } 9923 break; 9924 default: 9925 return (-1); 9926 } 9927 return (sizeof (int)); 9928 } 9929 9930 9931 /* 9932 * TCP routine to get the values of options. 9933 */ 9934 int 9935 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9936 { 9937 int *i1 = (int *)ptr; 9938 conn_t *connp = Q_TO_CONN(q); 9939 tcp_t *tcp = connp->conn_tcp; 9940 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9941 9942 switch (level) { 9943 case SOL_SOCKET: 9944 switch (name) { 9945 case SO_LINGER: { 9946 struct linger *lgr = (struct linger *)ptr; 9947 9948 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9949 lgr->l_linger = tcp->tcp_lingertime; 9950 } 9951 return (sizeof (struct linger)); 9952 case SO_DEBUG: 9953 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9954 break; 9955 case SO_KEEPALIVE: 9956 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9957 break; 9958 case SO_DONTROUTE: 9959 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9960 break; 9961 case SO_USELOOPBACK: 9962 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9963 break; 9964 case SO_BROADCAST: 9965 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9966 break; 9967 case SO_REUSEADDR: 9968 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9969 break; 9970 case SO_OOBINLINE: 9971 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9972 break; 9973 case SO_DGRAM_ERRIND: 9974 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9975 break; 9976 case SO_TYPE: 9977 *i1 = SOCK_STREAM; 9978 break; 9979 case SO_SNDBUF: 9980 *i1 = tcp->tcp_xmit_hiwater; 9981 break; 9982 case SO_RCVBUF: 9983 *i1 = RD(q)->q_hiwat; 9984 break; 9985 case SO_SND_COPYAVOID: 9986 *i1 = tcp->tcp_snd_zcopy_on ? 9987 SO_SND_COPYAVOID : 0; 9988 break; 9989 case SO_ALLZONES: 9990 *i1 = connp->conn_allzones ? 1 : 0; 9991 break; 9992 case SO_ANON_MLP: 9993 *i1 = connp->conn_anon_mlp; 9994 break; 9995 case SO_MAC_EXEMPT: 9996 *i1 = connp->conn_mac_exempt; 9997 break; 9998 case SO_EXCLBIND: 9999 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 10000 break; 10001 case SO_PROTOTYPE: 10002 *i1 = IPPROTO_TCP; 10003 break; 10004 case SO_DOMAIN: 10005 *i1 = tcp->tcp_family; 10006 break; 10007 default: 10008 return (-1); 10009 } 10010 break; 10011 case IPPROTO_TCP: 10012 switch (name) { 10013 case TCP_NODELAY: 10014 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 10015 break; 10016 case TCP_MAXSEG: 10017 *i1 = tcp->tcp_mss; 10018 break; 10019 case TCP_NOTIFY_THRESHOLD: 10020 *i1 = (int)tcp->tcp_first_timer_threshold; 10021 break; 10022 case TCP_ABORT_THRESHOLD: 10023 *i1 = tcp->tcp_second_timer_threshold; 10024 break; 10025 case TCP_CONN_NOTIFY_THRESHOLD: 10026 *i1 = tcp->tcp_first_ctimer_threshold; 10027 break; 10028 case TCP_CONN_ABORT_THRESHOLD: 10029 *i1 = tcp->tcp_second_ctimer_threshold; 10030 break; 10031 case TCP_RECVDSTADDR: 10032 *i1 = tcp->tcp_recvdstaddr; 10033 break; 10034 case TCP_ANONPRIVBIND: 10035 *i1 = tcp->tcp_anon_priv_bind; 10036 break; 10037 case TCP_EXCLBIND: 10038 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 10039 break; 10040 case TCP_INIT_CWND: 10041 *i1 = tcp->tcp_init_cwnd; 10042 break; 10043 case TCP_KEEPALIVE_THRESHOLD: 10044 *i1 = tcp->tcp_ka_interval; 10045 break; 10046 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10047 *i1 = tcp->tcp_ka_abort_thres; 10048 break; 10049 case TCP_CORK: 10050 *i1 = tcp->tcp_cork; 10051 break; 10052 default: 10053 return (-1); 10054 } 10055 break; 10056 case IPPROTO_IP: 10057 if (tcp->tcp_family != AF_INET) 10058 return (-1); 10059 switch (name) { 10060 case IP_OPTIONS: 10061 case T_IP_OPTIONS: { 10062 /* 10063 * This is compatible with BSD in that in only return 10064 * the reverse source route with the final destination 10065 * as the last entry. The first 4 bytes of the option 10066 * will contain the final destination. 10067 */ 10068 int opt_len; 10069 10070 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 10071 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 10072 ASSERT(opt_len >= 0); 10073 /* Caller ensures enough space */ 10074 if (opt_len > 0) { 10075 /* 10076 * TODO: Do we have to handle getsockopt on an 10077 * initiator as well? 10078 */ 10079 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 10080 } 10081 return (0); 10082 } 10083 case IP_TOS: 10084 case T_IP_TOS: 10085 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 10086 break; 10087 case IP_TTL: 10088 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 10089 break; 10090 case IP_NEXTHOP: 10091 /* Handled at IP level */ 10092 return (-EINVAL); 10093 default: 10094 return (-1); 10095 } 10096 break; 10097 case IPPROTO_IPV6: 10098 /* 10099 * IPPROTO_IPV6 options are only supported for sockets 10100 * that are using IPv6 on the wire. 10101 */ 10102 if (tcp->tcp_ipversion != IPV6_VERSION) { 10103 return (-1); 10104 } 10105 switch (name) { 10106 case IPV6_UNICAST_HOPS: 10107 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 10108 break; /* goto sizeof (int) option return */ 10109 case IPV6_BOUND_IF: 10110 /* Zero if not set */ 10111 *i1 = tcp->tcp_bound_if; 10112 break; /* goto sizeof (int) option return */ 10113 case IPV6_RECVPKTINFO: 10114 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 10115 *i1 = 1; 10116 else 10117 *i1 = 0; 10118 break; /* goto sizeof (int) option return */ 10119 case IPV6_RECVTCLASS: 10120 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 10121 *i1 = 1; 10122 else 10123 *i1 = 0; 10124 break; /* goto sizeof (int) option return */ 10125 case IPV6_RECVHOPLIMIT: 10126 if (tcp->tcp_ipv6_recvancillary & 10127 TCP_IPV6_RECVHOPLIMIT) 10128 *i1 = 1; 10129 else 10130 *i1 = 0; 10131 break; /* goto sizeof (int) option return */ 10132 case IPV6_RECVHOPOPTS: 10133 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 10134 *i1 = 1; 10135 else 10136 *i1 = 0; 10137 break; /* goto sizeof (int) option return */ 10138 case IPV6_RECVDSTOPTS: 10139 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 10140 *i1 = 1; 10141 else 10142 *i1 = 0; 10143 break; /* goto sizeof (int) option return */ 10144 case _OLD_IPV6_RECVDSTOPTS: 10145 if (tcp->tcp_ipv6_recvancillary & 10146 TCP_OLD_IPV6_RECVDSTOPTS) 10147 *i1 = 1; 10148 else 10149 *i1 = 0; 10150 break; /* goto sizeof (int) option return */ 10151 case IPV6_RECVRTHDR: 10152 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 10153 *i1 = 1; 10154 else 10155 *i1 = 0; 10156 break; /* goto sizeof (int) option return */ 10157 case IPV6_RECVRTHDRDSTOPTS: 10158 if (tcp->tcp_ipv6_recvancillary & 10159 TCP_IPV6_RECVRTDSTOPTS) 10160 *i1 = 1; 10161 else 10162 *i1 = 0; 10163 break; /* goto sizeof (int) option return */ 10164 case IPV6_PKTINFO: { 10165 /* XXX assumes that caller has room for max size! */ 10166 struct in6_pktinfo *pkti; 10167 10168 pkti = (struct in6_pktinfo *)ptr; 10169 if (ipp->ipp_fields & IPPF_IFINDEX) 10170 pkti->ipi6_ifindex = ipp->ipp_ifindex; 10171 else 10172 pkti->ipi6_ifindex = 0; 10173 if (ipp->ipp_fields & IPPF_ADDR) 10174 pkti->ipi6_addr = ipp->ipp_addr; 10175 else 10176 pkti->ipi6_addr = ipv6_all_zeros; 10177 return (sizeof (struct in6_pktinfo)); 10178 } 10179 case IPV6_TCLASS: 10180 if (ipp->ipp_fields & IPPF_TCLASS) 10181 *i1 = ipp->ipp_tclass; 10182 else 10183 *i1 = IPV6_FLOW_TCLASS( 10184 IPV6_DEFAULT_VERS_AND_FLOW); 10185 break; /* goto sizeof (int) option return */ 10186 case IPV6_NEXTHOP: { 10187 sin6_t *sin6 = (sin6_t *)ptr; 10188 10189 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 10190 return (0); 10191 *sin6 = sin6_null; 10192 sin6->sin6_family = AF_INET6; 10193 sin6->sin6_addr = ipp->ipp_nexthop; 10194 return (sizeof (sin6_t)); 10195 } 10196 case IPV6_HOPOPTS: 10197 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 10198 return (0); 10199 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 10200 return (0); 10201 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 10202 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 10203 if (tcp->tcp_label_len > 0) { 10204 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 10205 ptr[1] = (ipp->ipp_hopoptslen - 10206 tcp->tcp_label_len + 7) / 8 - 1; 10207 } 10208 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 10209 case IPV6_RTHDRDSTOPTS: 10210 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 10211 return (0); 10212 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 10213 return (ipp->ipp_rtdstoptslen); 10214 case IPV6_RTHDR: 10215 if (!(ipp->ipp_fields & IPPF_RTHDR)) 10216 return (0); 10217 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 10218 return (ipp->ipp_rthdrlen); 10219 case IPV6_DSTOPTS: 10220 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 10221 return (0); 10222 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 10223 return (ipp->ipp_dstoptslen); 10224 case IPV6_SRC_PREFERENCES: 10225 return (ip6_get_src_preferences(connp, 10226 (uint32_t *)ptr)); 10227 case IPV6_PATHMTU: { 10228 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 10229 10230 if (tcp->tcp_state < TCPS_ESTABLISHED) 10231 return (-1); 10232 10233 return (ip_fill_mtuinfo(&connp->conn_remv6, 10234 connp->conn_fport, mtuinfo, 10235 connp->conn_netstack)); 10236 } 10237 default: 10238 return (-1); 10239 } 10240 break; 10241 default: 10242 return (-1); 10243 } 10244 return (sizeof (int)); 10245 } 10246 10247 /* 10248 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 10249 * Parameters are assumed to be verified by the caller. 10250 */ 10251 /* ARGSUSED */ 10252 int 10253 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10254 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10255 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10256 { 10257 conn_t *connp = Q_TO_CONN(q); 10258 tcp_t *tcp = connp->conn_tcp; 10259 int *i1 = (int *)invalp; 10260 boolean_t onoff = (*i1 == 0) ? 0 : 1; 10261 boolean_t checkonly; 10262 int reterr; 10263 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 10264 10265 switch (optset_context) { 10266 case SETFN_OPTCOM_CHECKONLY: 10267 checkonly = B_TRUE; 10268 /* 10269 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10270 * inlen != 0 implies value supplied and 10271 * we have to "pretend" to set it. 10272 * inlen == 0 implies that there is no 10273 * value part in T_CHECK request and just validation 10274 * done elsewhere should be enough, we just return here. 10275 */ 10276 if (inlen == 0) { 10277 *outlenp = 0; 10278 return (0); 10279 } 10280 break; 10281 case SETFN_OPTCOM_NEGOTIATE: 10282 checkonly = B_FALSE; 10283 break; 10284 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 10285 case SETFN_CONN_NEGOTIATE: 10286 checkonly = B_FALSE; 10287 /* 10288 * Negotiating local and "association-related" options 10289 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 10290 * primitives is allowed by XTI, but we choose 10291 * to not implement this style negotiation for Internet 10292 * protocols (We interpret it is a must for OSI world but 10293 * optional for Internet protocols) for all options. 10294 * [ Will do only for the few options that enable test 10295 * suites that our XTI implementation of this feature 10296 * works for transports that do allow it ] 10297 */ 10298 if (!tcp_allow_connopt_set(level, name)) { 10299 *outlenp = 0; 10300 return (EINVAL); 10301 } 10302 break; 10303 default: 10304 /* 10305 * We should never get here 10306 */ 10307 *outlenp = 0; 10308 return (EINVAL); 10309 } 10310 10311 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10312 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10313 10314 /* 10315 * For TCP, we should have no ancillary data sent down 10316 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10317 * has to be zero. 10318 */ 10319 ASSERT(thisdg_attrs == NULL); 10320 10321 /* 10322 * For fixed length options, no sanity check 10323 * of passed in length is done. It is assumed *_optcom_req() 10324 * routines do the right thing. 10325 */ 10326 10327 switch (level) { 10328 case SOL_SOCKET: 10329 switch (name) { 10330 case SO_LINGER: { 10331 struct linger *lgr = (struct linger *)invalp; 10332 10333 if (!checkonly) { 10334 if (lgr->l_onoff) { 10335 tcp->tcp_linger = 1; 10336 tcp->tcp_lingertime = lgr->l_linger; 10337 } else { 10338 tcp->tcp_linger = 0; 10339 tcp->tcp_lingertime = 0; 10340 } 10341 /* struct copy */ 10342 *(struct linger *)outvalp = *lgr; 10343 } else { 10344 if (!lgr->l_onoff) { 10345 ((struct linger *) 10346 outvalp)->l_onoff = 0; 10347 ((struct linger *) 10348 outvalp)->l_linger = 0; 10349 } else { 10350 /* struct copy */ 10351 *(struct linger *)outvalp = *lgr; 10352 } 10353 } 10354 *outlenp = sizeof (struct linger); 10355 return (0); 10356 } 10357 case SO_DEBUG: 10358 if (!checkonly) 10359 tcp->tcp_debug = onoff; 10360 break; 10361 case SO_KEEPALIVE: 10362 if (checkonly) { 10363 /* T_CHECK case */ 10364 break; 10365 } 10366 10367 if (!onoff) { 10368 if (tcp->tcp_ka_enabled) { 10369 if (tcp->tcp_ka_tid != 0) { 10370 (void) TCP_TIMER_CANCEL(tcp, 10371 tcp->tcp_ka_tid); 10372 tcp->tcp_ka_tid = 0; 10373 } 10374 tcp->tcp_ka_enabled = 0; 10375 } 10376 break; 10377 } 10378 if (!tcp->tcp_ka_enabled) { 10379 /* Crank up the keepalive timer */ 10380 tcp->tcp_ka_last_intrvl = 0; 10381 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10382 tcp_keepalive_killer, 10383 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10384 tcp->tcp_ka_enabled = 1; 10385 } 10386 break; 10387 case SO_DONTROUTE: 10388 /* 10389 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10390 * only of interest to IP. We track them here only so 10391 * that we can report their current value. 10392 */ 10393 if (!checkonly) { 10394 tcp->tcp_dontroute = onoff; 10395 tcp->tcp_connp->conn_dontroute = onoff; 10396 } 10397 break; 10398 case SO_USELOOPBACK: 10399 if (!checkonly) { 10400 tcp->tcp_useloopback = onoff; 10401 tcp->tcp_connp->conn_loopback = onoff; 10402 } 10403 break; 10404 case SO_BROADCAST: 10405 if (!checkonly) { 10406 tcp->tcp_broadcast = onoff; 10407 tcp->tcp_connp->conn_broadcast = onoff; 10408 } 10409 break; 10410 case SO_REUSEADDR: 10411 if (!checkonly) { 10412 tcp->tcp_reuseaddr = onoff; 10413 tcp->tcp_connp->conn_reuseaddr = onoff; 10414 } 10415 break; 10416 case SO_OOBINLINE: 10417 if (!checkonly) 10418 tcp->tcp_oobinline = onoff; 10419 break; 10420 case SO_DGRAM_ERRIND: 10421 if (!checkonly) 10422 tcp->tcp_dgram_errind = onoff; 10423 break; 10424 case SO_SNDBUF: { 10425 if (*i1 > tcps->tcps_max_buf) { 10426 *outlenp = 0; 10427 return (ENOBUFS); 10428 } 10429 if (checkonly) 10430 break; 10431 10432 tcp->tcp_xmit_hiwater = *i1; 10433 if (tcps->tcps_snd_lowat_fraction != 0) 10434 tcp->tcp_xmit_lowater = 10435 tcp->tcp_xmit_hiwater / 10436 tcps->tcps_snd_lowat_fraction; 10437 (void) tcp_maxpsz_set(tcp, B_TRUE); 10438 /* 10439 * If we are flow-controlled, recheck the condition. 10440 * There are apps that increase SO_SNDBUF size when 10441 * flow-controlled (EWOULDBLOCK), and expect the flow 10442 * control condition to be lifted right away. 10443 */ 10444 mutex_enter(&tcp->tcp_non_sq_lock); 10445 if (tcp->tcp_flow_stopped && 10446 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10447 tcp_clrqfull(tcp); 10448 } 10449 mutex_exit(&tcp->tcp_non_sq_lock); 10450 break; 10451 } 10452 case SO_RCVBUF: 10453 if (*i1 > tcps->tcps_max_buf) { 10454 *outlenp = 0; 10455 return (ENOBUFS); 10456 } 10457 /* Silently ignore zero */ 10458 if (!checkonly && *i1 != 0) { 10459 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10460 (void) tcp_rwnd_set(tcp, *i1); 10461 } 10462 /* 10463 * XXX should we return the rwnd here 10464 * and tcp_opt_get ? 10465 */ 10466 break; 10467 case SO_SND_COPYAVOID: 10468 if (!checkonly) { 10469 /* we only allow enable at most once for now */ 10470 if (tcp->tcp_loopback || 10471 (tcp->tcp_kssl_ctx != NULL) || 10472 (!tcp->tcp_snd_zcopy_aware && 10473 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10474 *outlenp = 0; 10475 return (EOPNOTSUPP); 10476 } 10477 tcp->tcp_snd_zcopy_aware = 1; 10478 } 10479 break; 10480 case SO_ALLZONES: 10481 /* Pass option along to IP level for handling */ 10482 return (-EINVAL); 10483 case SO_ANON_MLP: 10484 /* Pass option along to IP level for handling */ 10485 return (-EINVAL); 10486 case SO_MAC_EXEMPT: 10487 /* Pass option along to IP level for handling */ 10488 return (-EINVAL); 10489 case SO_EXCLBIND: 10490 if (!checkonly) 10491 tcp->tcp_exclbind = onoff; 10492 break; 10493 default: 10494 *outlenp = 0; 10495 return (EINVAL); 10496 } 10497 break; 10498 case IPPROTO_TCP: 10499 switch (name) { 10500 case TCP_NODELAY: 10501 if (!checkonly) 10502 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10503 break; 10504 case TCP_NOTIFY_THRESHOLD: 10505 if (!checkonly) 10506 tcp->tcp_first_timer_threshold = *i1; 10507 break; 10508 case TCP_ABORT_THRESHOLD: 10509 if (!checkonly) 10510 tcp->tcp_second_timer_threshold = *i1; 10511 break; 10512 case TCP_CONN_NOTIFY_THRESHOLD: 10513 if (!checkonly) 10514 tcp->tcp_first_ctimer_threshold = *i1; 10515 break; 10516 case TCP_CONN_ABORT_THRESHOLD: 10517 if (!checkonly) 10518 tcp->tcp_second_ctimer_threshold = *i1; 10519 break; 10520 case TCP_RECVDSTADDR: 10521 if (tcp->tcp_state > TCPS_LISTEN) 10522 return (EOPNOTSUPP); 10523 if (!checkonly) 10524 tcp->tcp_recvdstaddr = onoff; 10525 break; 10526 case TCP_ANONPRIVBIND: 10527 if ((reterr = secpolicy_net_privaddr(cr, 0, 10528 IPPROTO_TCP)) != 0) { 10529 *outlenp = 0; 10530 return (reterr); 10531 } 10532 if (!checkonly) { 10533 tcp->tcp_anon_priv_bind = onoff; 10534 } 10535 break; 10536 case TCP_EXCLBIND: 10537 if (!checkonly) 10538 tcp->tcp_exclbind = onoff; 10539 break; /* goto sizeof (int) option return */ 10540 case TCP_INIT_CWND: { 10541 uint32_t init_cwnd = *((uint32_t *)invalp); 10542 10543 if (checkonly) 10544 break; 10545 10546 /* 10547 * Only allow socket with network configuration 10548 * privilege to set the initial cwnd to be larger 10549 * than allowed by RFC 3390. 10550 */ 10551 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10552 tcp->tcp_init_cwnd = init_cwnd; 10553 break; 10554 } 10555 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10556 *outlenp = 0; 10557 return (reterr); 10558 } 10559 if (init_cwnd > TCP_MAX_INIT_CWND) { 10560 *outlenp = 0; 10561 return (EINVAL); 10562 } 10563 tcp->tcp_init_cwnd = init_cwnd; 10564 break; 10565 } 10566 case TCP_KEEPALIVE_THRESHOLD: 10567 if (checkonly) 10568 break; 10569 10570 if (*i1 < tcps->tcps_keepalive_interval_low || 10571 *i1 > tcps->tcps_keepalive_interval_high) { 10572 *outlenp = 0; 10573 return (EINVAL); 10574 } 10575 if (*i1 != tcp->tcp_ka_interval) { 10576 tcp->tcp_ka_interval = *i1; 10577 /* 10578 * Check if we need to restart the 10579 * keepalive timer. 10580 */ 10581 if (tcp->tcp_ka_tid != 0) { 10582 ASSERT(tcp->tcp_ka_enabled); 10583 (void) TCP_TIMER_CANCEL(tcp, 10584 tcp->tcp_ka_tid); 10585 tcp->tcp_ka_last_intrvl = 0; 10586 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10587 tcp_keepalive_killer, 10588 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10589 } 10590 } 10591 break; 10592 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10593 if (!checkonly) { 10594 if (*i1 < 10595 tcps->tcps_keepalive_abort_interval_low || 10596 *i1 > 10597 tcps->tcps_keepalive_abort_interval_high) { 10598 *outlenp = 0; 10599 return (EINVAL); 10600 } 10601 tcp->tcp_ka_abort_thres = *i1; 10602 } 10603 break; 10604 case TCP_CORK: 10605 if (!checkonly) { 10606 /* 10607 * if tcp->tcp_cork was set and is now 10608 * being unset, we have to make sure that 10609 * the remaining data gets sent out. Also 10610 * unset tcp->tcp_cork so that tcp_wput_data() 10611 * can send data even if it is less than mss 10612 */ 10613 if (tcp->tcp_cork && onoff == 0 && 10614 tcp->tcp_unsent > 0) { 10615 tcp->tcp_cork = B_FALSE; 10616 tcp_wput_data(tcp, NULL, B_FALSE); 10617 } 10618 tcp->tcp_cork = onoff; 10619 } 10620 break; 10621 default: 10622 *outlenp = 0; 10623 return (EINVAL); 10624 } 10625 break; 10626 case IPPROTO_IP: 10627 if (tcp->tcp_family != AF_INET) { 10628 *outlenp = 0; 10629 return (ENOPROTOOPT); 10630 } 10631 switch (name) { 10632 case IP_OPTIONS: 10633 case T_IP_OPTIONS: 10634 reterr = tcp_opt_set_header(tcp, checkonly, 10635 invalp, inlen); 10636 if (reterr) { 10637 *outlenp = 0; 10638 return (reterr); 10639 } 10640 /* OK return - copy input buffer into output buffer */ 10641 if (invalp != outvalp) { 10642 /* don't trust bcopy for identical src/dst */ 10643 bcopy(invalp, outvalp, inlen); 10644 } 10645 *outlenp = inlen; 10646 return (0); 10647 case IP_TOS: 10648 case T_IP_TOS: 10649 if (!checkonly) { 10650 tcp->tcp_ipha->ipha_type_of_service = 10651 (uchar_t)*i1; 10652 tcp->tcp_tos = (uchar_t)*i1; 10653 } 10654 break; 10655 case IP_TTL: 10656 if (!checkonly) { 10657 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10658 tcp->tcp_ttl = (uchar_t)*i1; 10659 } 10660 break; 10661 case IP_BOUND_IF: 10662 case IP_NEXTHOP: 10663 /* Handled at the IP level */ 10664 return (-EINVAL); 10665 case IP_SEC_OPT: 10666 /* 10667 * We should not allow policy setting after 10668 * we start listening for connections. 10669 */ 10670 if (tcp->tcp_state == TCPS_LISTEN) { 10671 return (EINVAL); 10672 } else { 10673 /* Handled at the IP level */ 10674 return (-EINVAL); 10675 } 10676 default: 10677 *outlenp = 0; 10678 return (EINVAL); 10679 } 10680 break; 10681 case IPPROTO_IPV6: { 10682 ip6_pkt_t *ipp; 10683 10684 /* 10685 * IPPROTO_IPV6 options are only supported for sockets 10686 * that are using IPv6 on the wire. 10687 */ 10688 if (tcp->tcp_ipversion != IPV6_VERSION) { 10689 *outlenp = 0; 10690 return (ENOPROTOOPT); 10691 } 10692 /* 10693 * Only sticky options; no ancillary data 10694 */ 10695 ASSERT(thisdg_attrs == NULL); 10696 ipp = &tcp->tcp_sticky_ipp; 10697 10698 switch (name) { 10699 case IPV6_UNICAST_HOPS: 10700 /* -1 means use default */ 10701 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10702 *outlenp = 0; 10703 return (EINVAL); 10704 } 10705 if (!checkonly) { 10706 if (*i1 == -1) { 10707 tcp->tcp_ip6h->ip6_hops = 10708 ipp->ipp_unicast_hops = 10709 (uint8_t)tcps->tcps_ipv6_hoplimit; 10710 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10711 /* Pass modified value to IP. */ 10712 *i1 = tcp->tcp_ip6h->ip6_hops; 10713 } else { 10714 tcp->tcp_ip6h->ip6_hops = 10715 ipp->ipp_unicast_hops = 10716 (uint8_t)*i1; 10717 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10718 } 10719 reterr = tcp_build_hdrs(q, tcp); 10720 if (reterr != 0) 10721 return (reterr); 10722 } 10723 break; 10724 case IPV6_BOUND_IF: 10725 if (!checkonly) { 10726 int error = 0; 10727 10728 tcp->tcp_bound_if = *i1; 10729 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10730 B_TRUE, checkonly, level, name, mblk); 10731 if (error != 0) { 10732 *outlenp = 0; 10733 return (error); 10734 } 10735 } 10736 break; 10737 /* 10738 * Set boolean switches for ancillary data delivery 10739 */ 10740 case IPV6_RECVPKTINFO: 10741 if (!checkonly) { 10742 if (onoff) 10743 tcp->tcp_ipv6_recvancillary |= 10744 TCP_IPV6_RECVPKTINFO; 10745 else 10746 tcp->tcp_ipv6_recvancillary &= 10747 ~TCP_IPV6_RECVPKTINFO; 10748 /* Force it to be sent up with the next msg */ 10749 tcp->tcp_recvifindex = 0; 10750 } 10751 break; 10752 case IPV6_RECVTCLASS: 10753 if (!checkonly) { 10754 if (onoff) 10755 tcp->tcp_ipv6_recvancillary |= 10756 TCP_IPV6_RECVTCLASS; 10757 else 10758 tcp->tcp_ipv6_recvancillary &= 10759 ~TCP_IPV6_RECVTCLASS; 10760 } 10761 break; 10762 case IPV6_RECVHOPLIMIT: 10763 if (!checkonly) { 10764 if (onoff) 10765 tcp->tcp_ipv6_recvancillary |= 10766 TCP_IPV6_RECVHOPLIMIT; 10767 else 10768 tcp->tcp_ipv6_recvancillary &= 10769 ~TCP_IPV6_RECVHOPLIMIT; 10770 /* Force it to be sent up with the next msg */ 10771 tcp->tcp_recvhops = 0xffffffffU; 10772 } 10773 break; 10774 case IPV6_RECVHOPOPTS: 10775 if (!checkonly) { 10776 if (onoff) 10777 tcp->tcp_ipv6_recvancillary |= 10778 TCP_IPV6_RECVHOPOPTS; 10779 else 10780 tcp->tcp_ipv6_recvancillary &= 10781 ~TCP_IPV6_RECVHOPOPTS; 10782 } 10783 break; 10784 case IPV6_RECVDSTOPTS: 10785 if (!checkonly) { 10786 if (onoff) 10787 tcp->tcp_ipv6_recvancillary |= 10788 TCP_IPV6_RECVDSTOPTS; 10789 else 10790 tcp->tcp_ipv6_recvancillary &= 10791 ~TCP_IPV6_RECVDSTOPTS; 10792 } 10793 break; 10794 case _OLD_IPV6_RECVDSTOPTS: 10795 if (!checkonly) { 10796 if (onoff) 10797 tcp->tcp_ipv6_recvancillary |= 10798 TCP_OLD_IPV6_RECVDSTOPTS; 10799 else 10800 tcp->tcp_ipv6_recvancillary &= 10801 ~TCP_OLD_IPV6_RECVDSTOPTS; 10802 } 10803 break; 10804 case IPV6_RECVRTHDR: 10805 if (!checkonly) { 10806 if (onoff) 10807 tcp->tcp_ipv6_recvancillary |= 10808 TCP_IPV6_RECVRTHDR; 10809 else 10810 tcp->tcp_ipv6_recvancillary &= 10811 ~TCP_IPV6_RECVRTHDR; 10812 } 10813 break; 10814 case IPV6_RECVRTHDRDSTOPTS: 10815 if (!checkonly) { 10816 if (onoff) 10817 tcp->tcp_ipv6_recvancillary |= 10818 TCP_IPV6_RECVRTDSTOPTS; 10819 else 10820 tcp->tcp_ipv6_recvancillary &= 10821 ~TCP_IPV6_RECVRTDSTOPTS; 10822 } 10823 break; 10824 case IPV6_PKTINFO: 10825 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10826 return (EINVAL); 10827 if (checkonly) 10828 break; 10829 10830 if (inlen == 0) { 10831 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10832 } else { 10833 struct in6_pktinfo *pkti; 10834 10835 pkti = (struct in6_pktinfo *)invalp; 10836 /* 10837 * RFC 3542 states that ipi6_addr must be 10838 * the unspecified address when setting the 10839 * IPV6_PKTINFO sticky socket option on a 10840 * TCP socket. 10841 */ 10842 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10843 return (EINVAL); 10844 /* 10845 * ip6_set_pktinfo() validates the source 10846 * address and interface index. 10847 */ 10848 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10849 pkti, mblk); 10850 if (reterr != 0) 10851 return (reterr); 10852 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10853 ipp->ipp_addr = pkti->ipi6_addr; 10854 if (ipp->ipp_ifindex != 0) 10855 ipp->ipp_fields |= IPPF_IFINDEX; 10856 else 10857 ipp->ipp_fields &= ~IPPF_IFINDEX; 10858 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10859 ipp->ipp_fields |= IPPF_ADDR; 10860 else 10861 ipp->ipp_fields &= ~IPPF_ADDR; 10862 } 10863 reterr = tcp_build_hdrs(q, tcp); 10864 if (reterr != 0) 10865 return (reterr); 10866 break; 10867 case IPV6_TCLASS: 10868 if (inlen != 0 && inlen != sizeof (int)) 10869 return (EINVAL); 10870 if (checkonly) 10871 break; 10872 10873 if (inlen == 0) { 10874 ipp->ipp_fields &= ~IPPF_TCLASS; 10875 } else { 10876 if (*i1 > 255 || *i1 < -1) 10877 return (EINVAL); 10878 if (*i1 == -1) { 10879 ipp->ipp_tclass = 0; 10880 *i1 = 0; 10881 } else { 10882 ipp->ipp_tclass = *i1; 10883 } 10884 ipp->ipp_fields |= IPPF_TCLASS; 10885 } 10886 reterr = tcp_build_hdrs(q, tcp); 10887 if (reterr != 0) 10888 return (reterr); 10889 break; 10890 case IPV6_NEXTHOP: 10891 /* 10892 * IP will verify that the nexthop is reachable 10893 * and fail for sticky options. 10894 */ 10895 if (inlen != 0 && inlen != sizeof (sin6_t)) 10896 return (EINVAL); 10897 if (checkonly) 10898 break; 10899 10900 if (inlen == 0) { 10901 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10902 } else { 10903 sin6_t *sin6 = (sin6_t *)invalp; 10904 10905 if (sin6->sin6_family != AF_INET6) 10906 return (EAFNOSUPPORT); 10907 if (IN6_IS_ADDR_V4MAPPED( 10908 &sin6->sin6_addr)) 10909 return (EADDRNOTAVAIL); 10910 ipp->ipp_nexthop = sin6->sin6_addr; 10911 if (!IN6_IS_ADDR_UNSPECIFIED( 10912 &ipp->ipp_nexthop)) 10913 ipp->ipp_fields |= IPPF_NEXTHOP; 10914 else 10915 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10916 } 10917 reterr = tcp_build_hdrs(q, tcp); 10918 if (reterr != 0) 10919 return (reterr); 10920 break; 10921 case IPV6_HOPOPTS: { 10922 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10923 10924 /* 10925 * Sanity checks - minimum size, size a multiple of 10926 * eight bytes, and matching size passed in. 10927 */ 10928 if (inlen != 0 && 10929 inlen != (8 * (hopts->ip6h_len + 1))) 10930 return (EINVAL); 10931 10932 if (checkonly) 10933 break; 10934 10935 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10936 (uchar_t **)&ipp->ipp_hopopts, 10937 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10938 if (reterr != 0) 10939 return (reterr); 10940 if (ipp->ipp_hopoptslen == 0) 10941 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10942 else 10943 ipp->ipp_fields |= IPPF_HOPOPTS; 10944 reterr = tcp_build_hdrs(q, tcp); 10945 if (reterr != 0) 10946 return (reterr); 10947 break; 10948 } 10949 case IPV6_RTHDRDSTOPTS: { 10950 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10951 10952 /* 10953 * Sanity checks - minimum size, size a multiple of 10954 * eight bytes, and matching size passed in. 10955 */ 10956 if (inlen != 0 && 10957 inlen != (8 * (dopts->ip6d_len + 1))) 10958 return (EINVAL); 10959 10960 if (checkonly) 10961 break; 10962 10963 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10964 (uchar_t **)&ipp->ipp_rtdstopts, 10965 &ipp->ipp_rtdstoptslen, 0); 10966 if (reterr != 0) 10967 return (reterr); 10968 if (ipp->ipp_rtdstoptslen == 0) 10969 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10970 else 10971 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10972 reterr = tcp_build_hdrs(q, tcp); 10973 if (reterr != 0) 10974 return (reterr); 10975 break; 10976 } 10977 case IPV6_DSTOPTS: { 10978 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10979 10980 /* 10981 * Sanity checks - minimum size, size a multiple of 10982 * eight bytes, and matching size passed in. 10983 */ 10984 if (inlen != 0 && 10985 inlen != (8 * (dopts->ip6d_len + 1))) 10986 return (EINVAL); 10987 10988 if (checkonly) 10989 break; 10990 10991 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10992 (uchar_t **)&ipp->ipp_dstopts, 10993 &ipp->ipp_dstoptslen, 0); 10994 if (reterr != 0) 10995 return (reterr); 10996 if (ipp->ipp_dstoptslen == 0) 10997 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10998 else 10999 ipp->ipp_fields |= IPPF_DSTOPTS; 11000 reterr = tcp_build_hdrs(q, tcp); 11001 if (reterr != 0) 11002 return (reterr); 11003 break; 11004 } 11005 case IPV6_RTHDR: { 11006 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 11007 11008 /* 11009 * Sanity checks - minimum size, size a multiple of 11010 * eight bytes, and matching size passed in. 11011 */ 11012 if (inlen != 0 && 11013 inlen != (8 * (rt->ip6r_len + 1))) 11014 return (EINVAL); 11015 11016 if (checkonly) 11017 break; 11018 11019 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 11020 (uchar_t **)&ipp->ipp_rthdr, 11021 &ipp->ipp_rthdrlen, 0); 11022 if (reterr != 0) 11023 return (reterr); 11024 if (ipp->ipp_rthdrlen == 0) 11025 ipp->ipp_fields &= ~IPPF_RTHDR; 11026 else 11027 ipp->ipp_fields |= IPPF_RTHDR; 11028 reterr = tcp_build_hdrs(q, tcp); 11029 if (reterr != 0) 11030 return (reterr); 11031 break; 11032 } 11033 case IPV6_V6ONLY: 11034 if (!checkonly) 11035 tcp->tcp_connp->conn_ipv6_v6only = onoff; 11036 break; 11037 case IPV6_USE_MIN_MTU: 11038 if (inlen != sizeof (int)) 11039 return (EINVAL); 11040 11041 if (*i1 < -1 || *i1 > 1) 11042 return (EINVAL); 11043 11044 if (checkonly) 11045 break; 11046 11047 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 11048 ipp->ipp_use_min_mtu = *i1; 11049 break; 11050 case IPV6_BOUND_PIF: 11051 /* Handled at the IP level */ 11052 return (-EINVAL); 11053 case IPV6_SEC_OPT: 11054 /* 11055 * We should not allow policy setting after 11056 * we start listening for connections. 11057 */ 11058 if (tcp->tcp_state == TCPS_LISTEN) { 11059 return (EINVAL); 11060 } else { 11061 /* Handled at the IP level */ 11062 return (-EINVAL); 11063 } 11064 case IPV6_SRC_PREFERENCES: 11065 if (inlen != sizeof (uint32_t)) 11066 return (EINVAL); 11067 reterr = ip6_set_src_preferences(tcp->tcp_connp, 11068 *(uint32_t *)invalp); 11069 if (reterr != 0) { 11070 *outlenp = 0; 11071 return (reterr); 11072 } 11073 break; 11074 default: 11075 *outlenp = 0; 11076 return (EINVAL); 11077 } 11078 break; 11079 } /* end IPPROTO_IPV6 */ 11080 default: 11081 *outlenp = 0; 11082 return (EINVAL); 11083 } 11084 /* 11085 * Common case of OK return with outval same as inval 11086 */ 11087 if (invalp != outvalp) { 11088 /* don't trust bcopy for identical src/dst */ 11089 (void) bcopy(invalp, outvalp, inlen); 11090 } 11091 *outlenp = inlen; 11092 return (0); 11093 } 11094 11095 /* 11096 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 11097 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 11098 * headers, and the maximum size tcp header (to avoid reallocation 11099 * on the fly for additional tcp options). 11100 * Returns failure if can't allocate memory. 11101 */ 11102 static int 11103 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 11104 { 11105 char *hdrs; 11106 uint_t hdrs_len; 11107 ip6i_t *ip6i; 11108 char buf[TCP_MAX_HDR_LENGTH]; 11109 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 11110 in6_addr_t src, dst; 11111 tcp_stack_t *tcps = tcp->tcp_tcps; 11112 11113 /* 11114 * save the existing tcp header and source/dest IP addresses 11115 */ 11116 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 11117 src = tcp->tcp_ip6h->ip6_src; 11118 dst = tcp->tcp_ip6h->ip6_dst; 11119 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 11120 ASSERT(hdrs_len != 0); 11121 if (hdrs_len > tcp->tcp_iphc_len) { 11122 /* Need to reallocate */ 11123 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 11124 if (hdrs == NULL) 11125 return (ENOMEM); 11126 if (tcp->tcp_iphc != NULL) { 11127 if (tcp->tcp_hdr_grown) { 11128 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 11129 } else { 11130 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 11131 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 11132 } 11133 tcp->tcp_iphc_len = 0; 11134 } 11135 ASSERT(tcp->tcp_iphc_len == 0); 11136 tcp->tcp_iphc = hdrs; 11137 tcp->tcp_iphc_len = hdrs_len; 11138 tcp->tcp_hdr_grown = B_TRUE; 11139 } 11140 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 11141 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 11142 11143 /* Set header fields not in ipp */ 11144 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 11145 ip6i = (ip6i_t *)tcp->tcp_iphc; 11146 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 11147 } else { 11148 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 11149 } 11150 /* 11151 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 11152 * 11153 * tcp->tcp_tcp_hdr_len doesn't change here. 11154 */ 11155 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 11156 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 11157 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 11158 11159 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 11160 11161 tcp->tcp_ip6h->ip6_src = src; 11162 tcp->tcp_ip6h->ip6_dst = dst; 11163 11164 /* 11165 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 11166 * the default value for TCP. 11167 */ 11168 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 11169 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 11170 11171 /* 11172 * If we're setting extension headers after a connection 11173 * has been established, and if we have a routing header 11174 * among the extension headers, call ip_massage_options_v6 to 11175 * manipulate the routing header/ip6_dst set the checksum 11176 * difference in the tcp header template. 11177 * (This happens in tcp_connect_ipv6 if the routing header 11178 * is set prior to the connect.) 11179 * Set the tcp_sum to zero first in case we've cleared a 11180 * routing header or don't have one at all. 11181 */ 11182 tcp->tcp_sum = 0; 11183 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 11184 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 11185 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 11186 (uint8_t *)tcp->tcp_tcph); 11187 if (rth != NULL) { 11188 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 11189 rth, tcps->tcps_netstack); 11190 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 11191 (tcp->tcp_sum >> 16)); 11192 } 11193 } 11194 11195 /* Try to get everything in a single mblk */ 11196 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcps->tcps_wroff_xtra); 11197 return (0); 11198 } 11199 11200 /* 11201 * Transfer any source route option from ipha to buf/dst in reversed form. 11202 */ 11203 static int 11204 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 11205 { 11206 ipoptp_t opts; 11207 uchar_t *opt; 11208 uint8_t optval; 11209 uint8_t optlen; 11210 uint32_t len = 0; 11211 11212 for (optval = ipoptp_first(&opts, ipha); 11213 optval != IPOPT_EOL; 11214 optval = ipoptp_next(&opts)) { 11215 opt = opts.ipoptp_cur; 11216 optlen = opts.ipoptp_len; 11217 switch (optval) { 11218 int off1, off2; 11219 case IPOPT_SSRR: 11220 case IPOPT_LSRR: 11221 11222 /* Reverse source route */ 11223 /* 11224 * First entry should be the next to last one in the 11225 * current source route (the last entry is our 11226 * address.) 11227 * The last entry should be the final destination. 11228 */ 11229 buf[IPOPT_OPTVAL] = (uint8_t)optval; 11230 buf[IPOPT_OLEN] = (uint8_t)optlen; 11231 off1 = IPOPT_MINOFF_SR - 1; 11232 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 11233 if (off2 < 0) { 11234 /* No entries in source route */ 11235 break; 11236 } 11237 bcopy(opt + off2, dst, IP_ADDR_LEN); 11238 /* 11239 * Note: use src since ipha has not had its src 11240 * and dst reversed (it is in the state it was 11241 * received. 11242 */ 11243 bcopy(&ipha->ipha_src, buf + off2, 11244 IP_ADDR_LEN); 11245 off2 -= IP_ADDR_LEN; 11246 11247 while (off2 > 0) { 11248 bcopy(opt + off2, buf + off1, 11249 IP_ADDR_LEN); 11250 off1 += IP_ADDR_LEN; 11251 off2 -= IP_ADDR_LEN; 11252 } 11253 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11254 buf += optlen; 11255 len += optlen; 11256 break; 11257 } 11258 } 11259 done: 11260 /* Pad the resulting options */ 11261 while (len & 0x3) { 11262 *buf++ = IPOPT_EOL; 11263 len++; 11264 } 11265 return (len); 11266 } 11267 11268 11269 /* 11270 * Extract and revert a source route from ipha (if any) 11271 * and then update the relevant fields in both tcp_t and the standard header. 11272 */ 11273 static void 11274 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11275 { 11276 char buf[TCP_MAX_HDR_LENGTH]; 11277 uint_t tcph_len; 11278 int len; 11279 11280 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11281 len = IPH_HDR_LENGTH(ipha); 11282 if (len == IP_SIMPLE_HDR_LENGTH) 11283 /* Nothing to do */ 11284 return; 11285 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11286 (len & 0x3)) 11287 return; 11288 11289 tcph_len = tcp->tcp_tcp_hdr_len; 11290 bcopy(tcp->tcp_tcph, buf, tcph_len); 11291 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11292 (tcp->tcp_ipha->ipha_dst & 0xffff); 11293 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11294 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11295 len += IP_SIMPLE_HDR_LENGTH; 11296 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11297 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11298 if ((int)tcp->tcp_sum < 0) 11299 tcp->tcp_sum--; 11300 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11301 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11302 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11303 bcopy(buf, tcp->tcp_tcph, tcph_len); 11304 tcp->tcp_ip_hdr_len = len; 11305 tcp->tcp_ipha->ipha_version_and_hdr_length = 11306 (IP_VERSION << 4) | (len >> 2); 11307 len += tcph_len; 11308 tcp->tcp_hdr_len = len; 11309 } 11310 11311 /* 11312 * Copy the standard header into its new location, 11313 * lay in the new options and then update the relevant 11314 * fields in both tcp_t and the standard header. 11315 */ 11316 static int 11317 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11318 { 11319 uint_t tcph_len; 11320 uint8_t *ip_optp; 11321 tcph_t *new_tcph; 11322 tcp_stack_t *tcps = tcp->tcp_tcps; 11323 11324 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11325 return (EINVAL); 11326 11327 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11328 return (EINVAL); 11329 11330 if (checkonly) { 11331 /* 11332 * do not really set, just pretend to - T_CHECK 11333 */ 11334 return (0); 11335 } 11336 11337 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11338 if (tcp->tcp_label_len > 0) { 11339 int padlen; 11340 uint8_t opt; 11341 11342 /* convert list termination to no-ops */ 11343 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11344 ip_optp += ip_optp[IPOPT_OLEN]; 11345 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11346 while (--padlen >= 0) 11347 *ip_optp++ = opt; 11348 } 11349 tcph_len = tcp->tcp_tcp_hdr_len; 11350 new_tcph = (tcph_t *)(ip_optp + len); 11351 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11352 tcp->tcp_tcph = new_tcph; 11353 bcopy(ptr, ip_optp, len); 11354 11355 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11356 11357 tcp->tcp_ip_hdr_len = len; 11358 tcp->tcp_ipha->ipha_version_and_hdr_length = 11359 (IP_VERSION << 4) | (len >> 2); 11360 tcp->tcp_hdr_len = len + tcph_len; 11361 if (!TCP_IS_DETACHED(tcp)) { 11362 /* Always allocate room for all options. */ 11363 (void) mi_set_sth_wroff(tcp->tcp_rq, 11364 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11365 } 11366 return (0); 11367 } 11368 11369 /* Get callback routine passed to nd_load by tcp_param_register */ 11370 /* ARGSUSED */ 11371 static int 11372 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11373 { 11374 tcpparam_t *tcppa = (tcpparam_t *)cp; 11375 11376 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11377 return (0); 11378 } 11379 11380 /* 11381 * Walk through the param array specified registering each element with the 11382 * named dispatch handler. 11383 */ 11384 static boolean_t 11385 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11386 { 11387 for (; cnt-- > 0; tcppa++) { 11388 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11389 if (!nd_load(ndp, tcppa->tcp_param_name, 11390 tcp_param_get, tcp_param_set, 11391 (caddr_t)tcppa)) { 11392 nd_free(ndp); 11393 return (B_FALSE); 11394 } 11395 } 11396 } 11397 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11398 KM_SLEEP); 11399 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11400 sizeof (tcpparam_t)); 11401 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11402 tcp_param_get, tcp_param_set_aligned, 11403 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11404 nd_free(ndp); 11405 return (B_FALSE); 11406 } 11407 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11408 KM_SLEEP); 11409 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11410 sizeof (tcpparam_t)); 11411 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11412 tcp_param_get, tcp_param_set_aligned, 11413 (caddr_t)tcps->tcps_mdt_head_param)) { 11414 nd_free(ndp); 11415 return (B_FALSE); 11416 } 11417 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11418 KM_SLEEP); 11419 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11420 sizeof (tcpparam_t)); 11421 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11422 tcp_param_get, tcp_param_set_aligned, 11423 (caddr_t)tcps->tcps_mdt_tail_param)) { 11424 nd_free(ndp); 11425 return (B_FALSE); 11426 } 11427 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11428 KM_SLEEP); 11429 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11430 sizeof (tcpparam_t)); 11431 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11432 tcp_param_get, tcp_param_set_aligned, 11433 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11434 nd_free(ndp); 11435 return (B_FALSE); 11436 } 11437 if (!nd_load(ndp, "tcp_extra_priv_ports", 11438 tcp_extra_priv_ports_get, NULL, NULL)) { 11439 nd_free(ndp); 11440 return (B_FALSE); 11441 } 11442 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11443 NULL, tcp_extra_priv_ports_add, NULL)) { 11444 nd_free(ndp); 11445 return (B_FALSE); 11446 } 11447 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11448 NULL, tcp_extra_priv_ports_del, NULL)) { 11449 nd_free(ndp); 11450 return (B_FALSE); 11451 } 11452 if (!nd_load(ndp, "tcp_status", tcp_status_report, NULL, 11453 NULL)) { 11454 nd_free(ndp); 11455 return (B_FALSE); 11456 } 11457 if (!nd_load(ndp, "tcp_bind_hash", tcp_bind_hash_report, 11458 NULL, NULL)) { 11459 nd_free(ndp); 11460 return (B_FALSE); 11461 } 11462 if (!nd_load(ndp, "tcp_listen_hash", 11463 tcp_listen_hash_report, NULL, NULL)) { 11464 nd_free(ndp); 11465 return (B_FALSE); 11466 } 11467 if (!nd_load(ndp, "tcp_conn_hash", tcp_conn_hash_report, 11468 NULL, NULL)) { 11469 nd_free(ndp); 11470 return (B_FALSE); 11471 } 11472 if (!nd_load(ndp, "tcp_acceptor_hash", 11473 tcp_acceptor_hash_report, NULL, NULL)) { 11474 nd_free(ndp); 11475 return (B_FALSE); 11476 } 11477 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11478 tcp_1948_phrase_set, NULL)) { 11479 nd_free(ndp); 11480 return (B_FALSE); 11481 } 11482 /* 11483 * Dummy ndd variables - only to convey obsolescence information 11484 * through printing of their name (no get or set routines) 11485 * XXX Remove in future releases ? 11486 */ 11487 if (!nd_load(ndp, 11488 "tcp_close_wait_interval(obsoleted - " 11489 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11490 nd_free(ndp); 11491 return (B_FALSE); 11492 } 11493 return (B_TRUE); 11494 } 11495 11496 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11497 /* ARGSUSED */ 11498 static int 11499 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11500 cred_t *cr) 11501 { 11502 long new_value; 11503 tcpparam_t *tcppa = (tcpparam_t *)cp; 11504 11505 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11506 new_value < tcppa->tcp_param_min || 11507 new_value > tcppa->tcp_param_max) { 11508 return (EINVAL); 11509 } 11510 /* 11511 * Need to make sure new_value is a multiple of 4. If it is not, 11512 * round it up. For future 64 bit requirement, we actually make it 11513 * a multiple of 8. 11514 */ 11515 if (new_value & 0x7) { 11516 new_value = (new_value & ~0x7) + 0x8; 11517 } 11518 tcppa->tcp_param_val = new_value; 11519 return (0); 11520 } 11521 11522 /* Set callback routine passed to nd_load by tcp_param_register */ 11523 /* ARGSUSED */ 11524 static int 11525 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11526 { 11527 long new_value; 11528 tcpparam_t *tcppa = (tcpparam_t *)cp; 11529 11530 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11531 new_value < tcppa->tcp_param_min || 11532 new_value > tcppa->tcp_param_max) { 11533 return (EINVAL); 11534 } 11535 tcppa->tcp_param_val = new_value; 11536 return (0); 11537 } 11538 11539 /* 11540 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11541 * is filled, return as much as we can. The message passed in may be 11542 * multi-part, chained using b_cont. "start" is the starting sequence 11543 * number for this piece. 11544 */ 11545 static mblk_t * 11546 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11547 { 11548 uint32_t end; 11549 mblk_t *mp1; 11550 mblk_t *mp2; 11551 mblk_t *next_mp; 11552 uint32_t u1; 11553 tcp_stack_t *tcps = tcp->tcp_tcps; 11554 11555 /* Walk through all the new pieces. */ 11556 do { 11557 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11558 (uintptr_t)INT_MAX); 11559 end = start + (int)(mp->b_wptr - mp->b_rptr); 11560 next_mp = mp->b_cont; 11561 if (start == end) { 11562 /* Empty. Blast it. */ 11563 freeb(mp); 11564 continue; 11565 } 11566 mp->b_cont = NULL; 11567 TCP_REASS_SET_SEQ(mp, start); 11568 TCP_REASS_SET_END(mp, end); 11569 mp1 = tcp->tcp_reass_tail; 11570 if (!mp1) { 11571 tcp->tcp_reass_tail = mp; 11572 tcp->tcp_reass_head = mp; 11573 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11574 UPDATE_MIB(&tcps->tcps_mib, 11575 tcpInDataUnorderBytes, end - start); 11576 continue; 11577 } 11578 /* New stuff completely beyond tail? */ 11579 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11580 /* Link it on end. */ 11581 mp1->b_cont = mp; 11582 tcp->tcp_reass_tail = mp; 11583 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11584 UPDATE_MIB(&tcps->tcps_mib, 11585 tcpInDataUnorderBytes, end - start); 11586 continue; 11587 } 11588 mp1 = tcp->tcp_reass_head; 11589 u1 = TCP_REASS_SEQ(mp1); 11590 /* New stuff at the front? */ 11591 if (SEQ_LT(start, u1)) { 11592 /* Yes... Check for overlap. */ 11593 mp->b_cont = mp1; 11594 tcp->tcp_reass_head = mp; 11595 tcp_reass_elim_overlap(tcp, mp); 11596 continue; 11597 } 11598 /* 11599 * The new piece fits somewhere between the head and tail. 11600 * We find our slot, where mp1 precedes us and mp2 trails. 11601 */ 11602 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11603 u1 = TCP_REASS_SEQ(mp2); 11604 if (SEQ_LEQ(start, u1)) 11605 break; 11606 } 11607 /* Link ourselves in */ 11608 mp->b_cont = mp2; 11609 mp1->b_cont = mp; 11610 11611 /* Trim overlap with following mblk(s) first */ 11612 tcp_reass_elim_overlap(tcp, mp); 11613 11614 /* Trim overlap with preceding mblk */ 11615 tcp_reass_elim_overlap(tcp, mp1); 11616 11617 } while (start = end, mp = next_mp); 11618 mp1 = tcp->tcp_reass_head; 11619 /* Anything ready to go? */ 11620 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11621 return (NULL); 11622 /* Eat what we can off the queue */ 11623 for (;;) { 11624 mp = mp1->b_cont; 11625 end = TCP_REASS_END(mp1); 11626 TCP_REASS_SET_SEQ(mp1, 0); 11627 TCP_REASS_SET_END(mp1, 0); 11628 if (!mp) { 11629 tcp->tcp_reass_tail = NULL; 11630 break; 11631 } 11632 if (end != TCP_REASS_SEQ(mp)) { 11633 mp1->b_cont = NULL; 11634 break; 11635 } 11636 mp1 = mp; 11637 } 11638 mp1 = tcp->tcp_reass_head; 11639 tcp->tcp_reass_head = mp; 11640 return (mp1); 11641 } 11642 11643 /* Eliminate any overlap that mp may have over later mblks */ 11644 static void 11645 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11646 { 11647 uint32_t end; 11648 mblk_t *mp1; 11649 uint32_t u1; 11650 tcp_stack_t *tcps = tcp->tcp_tcps; 11651 11652 end = TCP_REASS_END(mp); 11653 while ((mp1 = mp->b_cont) != NULL) { 11654 u1 = TCP_REASS_SEQ(mp1); 11655 if (!SEQ_GT(end, u1)) 11656 break; 11657 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11658 mp->b_wptr -= end - u1; 11659 TCP_REASS_SET_END(mp, u1); 11660 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11661 UPDATE_MIB(&tcps->tcps_mib, 11662 tcpInDataPartDupBytes, end - u1); 11663 break; 11664 } 11665 mp->b_cont = mp1->b_cont; 11666 TCP_REASS_SET_SEQ(mp1, 0); 11667 TCP_REASS_SET_END(mp1, 0); 11668 freeb(mp1); 11669 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11670 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11671 } 11672 if (!mp1) 11673 tcp->tcp_reass_tail = mp; 11674 } 11675 11676 /* 11677 * Send up all messages queued on tcp_rcv_list. 11678 */ 11679 static uint_t 11680 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11681 { 11682 mblk_t *mp; 11683 uint_t ret = 0; 11684 uint_t thwin; 11685 #ifdef DEBUG 11686 uint_t cnt = 0; 11687 #endif 11688 tcp_stack_t *tcps = tcp->tcp_tcps; 11689 11690 /* Can't drain on an eager connection */ 11691 if (tcp->tcp_listener != NULL) 11692 return (ret); 11693 11694 /* Can't be sodirect enabled */ 11695 ASSERT(SOD_NOT_ENABLED(tcp)); 11696 11697 /* No need for the push timer now. */ 11698 if (tcp->tcp_push_tid != 0) { 11699 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11700 tcp->tcp_push_tid = 0; 11701 } 11702 11703 /* 11704 * Handle two cases here: we are currently fused or we were 11705 * previously fused and have some urgent data to be delivered 11706 * upstream. The latter happens because we either ran out of 11707 * memory or were detached and therefore sending the SIGURG was 11708 * deferred until this point. In either case we pass control 11709 * over to tcp_fuse_rcv_drain() since it may need to complete 11710 * some work. 11711 */ 11712 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11713 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11714 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11715 &tcp->tcp_fused_sigurg_mp)) 11716 return (ret); 11717 } 11718 11719 while ((mp = tcp->tcp_rcv_list) != NULL) { 11720 tcp->tcp_rcv_list = mp->b_next; 11721 mp->b_next = NULL; 11722 #ifdef DEBUG 11723 cnt += msgdsize(mp); 11724 #endif 11725 /* Does this need SSL processing first? */ 11726 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11727 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11728 mblk_t *, mp); 11729 tcp_kssl_input(tcp, mp); 11730 continue; 11731 } 11732 putnext(q, mp); 11733 } 11734 ASSERT(cnt == tcp->tcp_rcv_cnt); 11735 tcp->tcp_rcv_last_head = NULL; 11736 tcp->tcp_rcv_last_tail = NULL; 11737 tcp->tcp_rcv_cnt = 0; 11738 11739 /* Learn the latest rwnd information that we sent to the other side. */ 11740 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11741 << tcp->tcp_rcv_ws; 11742 /* This is peer's calculated send window (our receive window). */ 11743 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11744 /* 11745 * Increase the receive window to max. But we need to do receiver 11746 * SWS avoidance. This means that we need to check the increase of 11747 * of receive window is at least 1 MSS. 11748 */ 11749 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11750 /* 11751 * If the window that the other side knows is less than max 11752 * deferred acks segments, send an update immediately. 11753 */ 11754 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11755 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11756 ret = TH_ACK_NEEDED; 11757 } 11758 tcp->tcp_rwnd = q->q_hiwat; 11759 } 11760 return (ret); 11761 } 11762 11763 /* 11764 * Queue data on tcp_rcv_list which is a b_next chain. 11765 * tcp_rcv_last_head/tail is the last element of this chain. 11766 * Each element of the chain is a b_cont chain. 11767 * 11768 * M_DATA messages are added to the current element. 11769 * Other messages are added as new (b_next) elements. 11770 */ 11771 void 11772 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11773 { 11774 ASSERT(seg_len == msgdsize(mp)); 11775 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11776 11777 if (tcp->tcp_rcv_list == NULL) { 11778 ASSERT(tcp->tcp_rcv_last_head == NULL); 11779 tcp->tcp_rcv_list = mp; 11780 tcp->tcp_rcv_last_head = mp; 11781 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11782 tcp->tcp_rcv_last_tail->b_cont = mp; 11783 } else { 11784 tcp->tcp_rcv_last_head->b_next = mp; 11785 tcp->tcp_rcv_last_head = mp; 11786 } 11787 11788 while (mp->b_cont) 11789 mp = mp->b_cont; 11790 11791 tcp->tcp_rcv_last_tail = mp; 11792 tcp->tcp_rcv_cnt += seg_len; 11793 tcp->tcp_rwnd -= seg_len; 11794 } 11795 11796 /* 11797 * The tcp_rcv_sod_XXX() functions enqueue data directly to the socket 11798 * above, in addition when uioa is enabled schedule an asynchronous uio 11799 * prior to enqueuing. They implement the combinhed semantics of the 11800 * tcp_rcv_XXX() functions, tcp_rcv_list push logic, and STREAMS putnext() 11801 * canputnext(), i.e. flow-control with backenable. 11802 * 11803 * tcp_sod_wakeup() is called where tcp_rcv_drain() would be called in the 11804 * non sodirect connection but as there are no tcp_tcv_list mblk_t's we deal 11805 * with the rcv_wnd and push timer and call the sodirect wakeup function. 11806 * 11807 * Must be called with sodp->sod_lockp held and will return with the lock 11808 * released. 11809 */ 11810 static uint_t 11811 tcp_rcv_sod_wakeup(tcp_t *tcp, sodirect_t *sodp) 11812 { 11813 queue_t *q = tcp->tcp_rq; 11814 uint_t thwin; 11815 tcp_stack_t *tcps = tcp->tcp_tcps; 11816 uint_t ret = 0; 11817 11818 /* Can't be an eager connection */ 11819 ASSERT(tcp->tcp_listener == NULL); 11820 11821 /* Caller must have lock held */ 11822 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11823 11824 /* Sodirect mode so must not be a tcp_rcv_list */ 11825 ASSERT(tcp->tcp_rcv_list == NULL); 11826 11827 if (SOD_QFULL(sodp)) { 11828 /* Q is full, mark Q for need backenable */ 11829 SOD_QSETBE(sodp); 11830 } 11831 /* Last advertised rwnd, i.e. rwnd last sent in a packet */ 11832 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11833 << tcp->tcp_rcv_ws; 11834 /* This is peer's calculated send window (our available rwnd). */ 11835 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11836 /* 11837 * Increase the receive window to max. But we need to do receiver 11838 * SWS avoidance. This means that we need to check the increase of 11839 * of receive window is at least 1 MSS. 11840 */ 11841 if (!SOD_QFULL(sodp) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11842 /* 11843 * If the window that the other side knows is less than max 11844 * deferred acks segments, send an update immediately. 11845 */ 11846 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11847 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11848 ret = TH_ACK_NEEDED; 11849 } 11850 tcp->tcp_rwnd = q->q_hiwat; 11851 } 11852 11853 if (!SOD_QEMPTY(sodp)) { 11854 /* Wakeup to socket */ 11855 sodp->sod_state &= SOD_WAKE_CLR; 11856 sodp->sod_state |= SOD_WAKE_DONE; 11857 (sodp->sod_wakeup)(sodp); 11858 /* wakeup() does the mutex_ext() */ 11859 } else { 11860 /* Q is empty, no need to wake */ 11861 sodp->sod_state &= SOD_WAKE_CLR; 11862 sodp->sod_state |= SOD_WAKE_NOT; 11863 mutex_exit(sodp->sod_lockp); 11864 } 11865 11866 /* No need for the push timer now. */ 11867 if (tcp->tcp_push_tid != 0) { 11868 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11869 tcp->tcp_push_tid = 0; 11870 } 11871 11872 return (ret); 11873 } 11874 11875 /* 11876 * Called where tcp_rcv_enqueue()/putnext(RD(q)) would be. For M_DATA 11877 * mblk_t's if uioa enabled then start a uioa asynchronous copy directly 11878 * to the user-land buffer and flag the mblk_t as such. 11879 * 11880 * Also, handle tcp_rwnd. 11881 */ 11882 uint_t 11883 tcp_rcv_sod_enqueue(tcp_t *tcp, sodirect_t *sodp, mblk_t *mp, uint_t seg_len) 11884 { 11885 uioa_t *uioap = &sodp->sod_uioa; 11886 boolean_t qfull; 11887 uint_t thwin; 11888 11889 /* Can't be an eager connection */ 11890 ASSERT(tcp->tcp_listener == NULL); 11891 11892 /* Caller must have lock held */ 11893 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11894 11895 /* Sodirect mode so must not be a tcp_rcv_list */ 11896 ASSERT(tcp->tcp_rcv_list == NULL); 11897 11898 /* Passed in segment length must be equal to mblk_t chain data size */ 11899 ASSERT(seg_len == msgdsize(mp)); 11900 11901 if (DB_TYPE(mp) != M_DATA) { 11902 /* Only process M_DATA mblk_t's */ 11903 goto enq; 11904 } 11905 if (uioap->uioa_state & UIOA_ENABLED) { 11906 /* Uioa is enabled */ 11907 mblk_t *mp1 = mp; 11908 mblk_t *lmp = NULL; 11909 11910 if (seg_len > uioap->uio_resid) { 11911 /* 11912 * There isn't enough uio space for the mblk_t chain 11913 * so disable uioa such that this and any additional 11914 * mblk_t data is handled by the socket and schedule 11915 * the socket for wakeup to finish this uioa. 11916 */ 11917 uioap->uioa_state &= UIOA_CLR; 11918 uioap->uioa_state |= UIOA_FINI; 11919 if (sodp->sod_state & SOD_WAKE_NOT) { 11920 sodp->sod_state &= SOD_WAKE_CLR; 11921 sodp->sod_state |= SOD_WAKE_NEED; 11922 } 11923 goto enq; 11924 } 11925 do { 11926 uint32_t len = MBLKL(mp1); 11927 11928 if (!uioamove(mp1->b_rptr, len, UIO_READ, uioap)) { 11929 /* Scheduled, mark dblk_t as such */ 11930 DB_FLAGS(mp1) |= DBLK_UIOA; 11931 } else { 11932 /* Error, turn off async processing */ 11933 uioap->uioa_state &= UIOA_CLR; 11934 uioap->uioa_state |= UIOA_FINI; 11935 break; 11936 } 11937 lmp = mp1; 11938 } while ((mp1 = mp1->b_cont) != NULL); 11939 11940 if (mp1 != NULL || uioap->uio_resid == 0) { 11941 /* 11942 * Not all mblk_t(s) uioamoved (error) or all uio 11943 * space has been consumed so schedule the socket 11944 * for wakeup to finish this uio. 11945 */ 11946 sodp->sod_state &= SOD_WAKE_CLR; 11947 sodp->sod_state |= SOD_WAKE_NEED; 11948 11949 /* Break the mblk chain if neccessary. */ 11950 if (mp1 != NULL && lmp != NULL) { 11951 mp->b_next = mp1; 11952 lmp->b_cont = NULL; 11953 } 11954 } 11955 } else if (uioap->uioa_state & UIOA_FINI) { 11956 /* 11957 * Post UIO_ENABLED waiting for socket to finish processing 11958 * so just enqueue and update tcp_rwnd. 11959 */ 11960 if (SOD_QFULL(sodp)) 11961 tcp->tcp_rwnd -= seg_len; 11962 } else if (sodp->sod_want > 0) { 11963 /* 11964 * Uioa isn't enabled but sodirect has a pending read(). 11965 */ 11966 if (SOD_QCNT(sodp) + seg_len >= sodp->sod_want) { 11967 if (sodp->sod_state & SOD_WAKE_NOT) { 11968 /* Schedule socket for wakeup */ 11969 sodp->sod_state &= SOD_WAKE_CLR; 11970 sodp->sod_state |= SOD_WAKE_NEED; 11971 } 11972 tcp->tcp_rwnd -= seg_len; 11973 } 11974 } else if (SOD_QCNT(sodp) + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 11975 /* 11976 * No pending sodirect read() so used the default 11977 * TCP push logic to guess that a push is needed. 11978 */ 11979 if (sodp->sod_state & SOD_WAKE_NOT) { 11980 /* Schedule socket for wakeup */ 11981 sodp->sod_state &= SOD_WAKE_CLR; 11982 sodp->sod_state |= SOD_WAKE_NEED; 11983 } 11984 tcp->tcp_rwnd -= seg_len; 11985 } else { 11986 /* Just update tcp_rwnd */ 11987 tcp->tcp_rwnd -= seg_len; 11988 } 11989 enq: 11990 qfull = SOD_QFULL(sodp); 11991 11992 (sodp->sod_enqueue)(sodp, mp); 11993 11994 if (! qfull && SOD_QFULL(sodp)) { 11995 /* Wasn't QFULL, now QFULL, need back-enable */ 11996 SOD_QSETBE(sodp); 11997 } 11998 11999 /* 12000 * Check to see if remote avail swnd < mss due to delayed ACK, 12001 * first get advertised rwnd. 12002 */ 12003 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)); 12004 /* Minus delayed ACK count */ 12005 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 12006 if (thwin < tcp->tcp_mss) { 12007 /* Remote avail swnd < mss, need ACK now */ 12008 return (TH_ACK_NEEDED); 12009 } 12010 12011 return (0); 12012 } 12013 12014 /* 12015 * DEFAULT TCP ENTRY POINT via squeue on READ side. 12016 * 12017 * This is the default entry function into TCP on the read side. TCP is 12018 * always entered via squeue i.e. using squeue's for mutual exclusion. 12019 * When classifier does a lookup to find the tcp, it also puts a reference 12020 * on the conn structure associated so the tcp is guaranteed to exist 12021 * when we come here. We still need to check the state because it might 12022 * as well has been closed. The squeue processing function i.e. squeue_enter, 12023 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 12024 * CONN_DEC_REF. 12025 * 12026 * Apart from the default entry point, IP also sends packets directly to 12027 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 12028 * connections. 12029 */ 12030 void 12031 tcp_input(void *arg, mblk_t *mp, void *arg2) 12032 { 12033 conn_t *connp = (conn_t *)arg; 12034 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 12035 12036 /* arg2 is the sqp */ 12037 ASSERT(arg2 != NULL); 12038 ASSERT(mp != NULL); 12039 12040 /* 12041 * Don't accept any input on a closed tcp as this TCP logically does 12042 * not exist on the system. Don't proceed further with this TCP. 12043 * For eg. this packet could trigger another close of this tcp 12044 * which would be disastrous for tcp_refcnt. tcp_close_detached / 12045 * tcp_clean_death / tcp_closei_local must be called at most once 12046 * on a TCP. In this case we need to refeed the packet into the 12047 * classifier and figure out where the packet should go. Need to 12048 * preserve the recv_ill somehow. Until we figure that out, for 12049 * now just drop the packet if we can't classify the packet. 12050 */ 12051 if (tcp->tcp_state == TCPS_CLOSED || 12052 tcp->tcp_state == TCPS_BOUND) { 12053 conn_t *new_connp; 12054 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 12055 12056 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 12057 if (new_connp != NULL) { 12058 tcp_reinput(new_connp, mp, arg2); 12059 return; 12060 } 12061 /* We failed to classify. For now just drop the packet */ 12062 freemsg(mp); 12063 return; 12064 } 12065 12066 if (DB_TYPE(mp) == M_DATA) 12067 tcp_rput_data(connp, mp, arg2); 12068 else 12069 tcp_rput_common(tcp, mp); 12070 } 12071 12072 /* 12073 * The read side put procedure. 12074 * The packets passed up by ip are assume to be aligned according to 12075 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 12076 */ 12077 static void 12078 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 12079 { 12080 /* 12081 * tcp_rput_data() does not expect M_CTL except for the case 12082 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 12083 * type. Need to make sure that any other M_CTLs don't make 12084 * it to tcp_rput_data since it is not expecting any and doesn't 12085 * check for it. 12086 */ 12087 if (DB_TYPE(mp) == M_CTL) { 12088 switch (*(uint32_t *)(mp->b_rptr)) { 12089 case TCP_IOC_ABORT_CONN: 12090 /* 12091 * Handle connection abort request. 12092 */ 12093 tcp_ioctl_abort_handler(tcp, mp); 12094 return; 12095 case IPSEC_IN: 12096 /* 12097 * Only secure icmp arrive in TCP and they 12098 * don't go through data path. 12099 */ 12100 tcp_icmp_error(tcp, mp); 12101 return; 12102 case IN_PKTINFO: 12103 /* 12104 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 12105 * sockets that are receiving IPv4 traffic. tcp 12106 */ 12107 ASSERT(tcp->tcp_family == AF_INET6); 12108 ASSERT(tcp->tcp_ipv6_recvancillary & 12109 TCP_IPV6_RECVPKTINFO); 12110 tcp_rput_data(tcp->tcp_connp, mp, 12111 tcp->tcp_connp->conn_sqp); 12112 return; 12113 case MDT_IOC_INFO_UPDATE: 12114 /* 12115 * Handle Multidata information update; the 12116 * following routine will free the message. 12117 */ 12118 if (tcp->tcp_connp->conn_mdt_ok) { 12119 tcp_mdt_update(tcp, 12120 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 12121 B_FALSE); 12122 } 12123 freemsg(mp); 12124 return; 12125 case LSO_IOC_INFO_UPDATE: 12126 /* 12127 * Handle LSO information update; the following 12128 * routine will free the message. 12129 */ 12130 if (tcp->tcp_connp->conn_lso_ok) { 12131 tcp_lso_update(tcp, 12132 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 12133 } 12134 freemsg(mp); 12135 return; 12136 default: 12137 /* 12138 * tcp_icmp_err() will process the M_CTL packets. 12139 * Non-ICMP packets, if any, will be discarded in 12140 * tcp_icmp_err(). We will process the ICMP packet 12141 * even if we are TCP_IS_DETACHED_NONEAGER as the 12142 * incoming ICMP packet may result in changing 12143 * the tcp_mss, which we would need if we have 12144 * packets to retransmit. 12145 */ 12146 tcp_icmp_error(tcp, mp); 12147 return; 12148 } 12149 } 12150 12151 /* No point processing the message if tcp is already closed */ 12152 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 12153 freemsg(mp); 12154 return; 12155 } 12156 12157 tcp_rput_other(tcp, mp); 12158 } 12159 12160 12161 /* The minimum of smoothed mean deviation in RTO calculation. */ 12162 #define TCP_SD_MIN 400 12163 12164 /* 12165 * Set RTO for this connection. The formula is from Jacobson and Karels' 12166 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 12167 * are the same as those in Appendix A.2 of that paper. 12168 * 12169 * m = new measurement 12170 * sa = smoothed RTT average (8 * average estimates). 12171 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 12172 */ 12173 static void 12174 tcp_set_rto(tcp_t *tcp, clock_t rtt) 12175 { 12176 long m = TICK_TO_MSEC(rtt); 12177 clock_t sa = tcp->tcp_rtt_sa; 12178 clock_t sv = tcp->tcp_rtt_sd; 12179 clock_t rto; 12180 tcp_stack_t *tcps = tcp->tcp_tcps; 12181 12182 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 12183 tcp->tcp_rtt_update++; 12184 12185 /* tcp_rtt_sa is not 0 means this is a new sample. */ 12186 if (sa != 0) { 12187 /* 12188 * Update average estimator: 12189 * new rtt = 7/8 old rtt + 1/8 Error 12190 */ 12191 12192 /* m is now Error in estimate. */ 12193 m -= sa >> 3; 12194 if ((sa += m) <= 0) { 12195 /* 12196 * Don't allow the smoothed average to be negative. 12197 * We use 0 to denote reinitialization of the 12198 * variables. 12199 */ 12200 sa = 1; 12201 } 12202 12203 /* 12204 * Update deviation estimator: 12205 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 12206 */ 12207 if (m < 0) 12208 m = -m; 12209 m -= sv >> 2; 12210 sv += m; 12211 } else { 12212 /* 12213 * This follows BSD's implementation. So the reinitialized 12214 * RTO is 3 * m. We cannot go less than 2 because if the 12215 * link is bandwidth dominated, doubling the window size 12216 * during slow start means doubling the RTT. We want to be 12217 * more conservative when we reinitialize our estimates. 3 12218 * is just a convenient number. 12219 */ 12220 sa = m << 3; 12221 sv = m << 1; 12222 } 12223 if (sv < TCP_SD_MIN) { 12224 /* 12225 * We do not know that if sa captures the delay ACK 12226 * effect as in a long train of segments, a receiver 12227 * does not delay its ACKs. So set the minimum of sv 12228 * to be TCP_SD_MIN, which is default to 400 ms, twice 12229 * of BSD DATO. That means the minimum of mean 12230 * deviation is 100 ms. 12231 * 12232 */ 12233 sv = TCP_SD_MIN; 12234 } 12235 tcp->tcp_rtt_sa = sa; 12236 tcp->tcp_rtt_sd = sv; 12237 /* 12238 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 12239 * 12240 * Add tcp_rexmit_interval extra in case of extreme environment 12241 * where the algorithm fails to work. The default value of 12242 * tcp_rexmit_interval_extra should be 0. 12243 * 12244 * As we use a finer grained clock than BSD and update 12245 * RTO for every ACKs, add in another .25 of RTT to the 12246 * deviation of RTO to accomodate burstiness of 1/4 of 12247 * window size. 12248 */ 12249 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 12250 12251 if (rto > tcps->tcps_rexmit_interval_max) { 12252 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 12253 } else if (rto < tcps->tcps_rexmit_interval_min) { 12254 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 12255 } else { 12256 tcp->tcp_rto = rto; 12257 } 12258 12259 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 12260 tcp->tcp_timer_backoff = 0; 12261 } 12262 12263 /* 12264 * tcp_get_seg_mp() is called to get the pointer to a segment in the 12265 * send queue which starts at the given seq. no. 12266 * 12267 * Parameters: 12268 * tcp_t *tcp: the tcp instance pointer. 12269 * uint32_t seq: the starting seq. no of the requested segment. 12270 * int32_t *off: after the execution, *off will be the offset to 12271 * the returned mblk which points to the requested seq no. 12272 * It is the caller's responsibility to send in a non-null off. 12273 * 12274 * Return: 12275 * A mblk_t pointer pointing to the requested segment in send queue. 12276 */ 12277 static mblk_t * 12278 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 12279 { 12280 int32_t cnt; 12281 mblk_t *mp; 12282 12283 /* Defensive coding. Make sure we don't send incorrect data. */ 12284 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 12285 return (NULL); 12286 12287 cnt = seq - tcp->tcp_suna; 12288 mp = tcp->tcp_xmit_head; 12289 while (cnt > 0 && mp != NULL) { 12290 cnt -= mp->b_wptr - mp->b_rptr; 12291 if (cnt < 0) { 12292 cnt += mp->b_wptr - mp->b_rptr; 12293 break; 12294 } 12295 mp = mp->b_cont; 12296 } 12297 ASSERT(mp != NULL); 12298 *off = cnt; 12299 return (mp); 12300 } 12301 12302 /* 12303 * This function handles all retransmissions if SACK is enabled for this 12304 * connection. First it calculates how many segments can be retransmitted 12305 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12306 * segments. A segment is eligible if sack_cnt for that segment is greater 12307 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12308 * all eligible segments, it checks to see if TCP can send some new segments 12309 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12310 * 12311 * Parameters: 12312 * tcp_t *tcp: the tcp structure of the connection. 12313 * uint_t *flags: in return, appropriate value will be set for 12314 * tcp_rput_data(). 12315 */ 12316 static void 12317 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12318 { 12319 notsack_blk_t *notsack_blk; 12320 int32_t usable_swnd; 12321 int32_t mss; 12322 uint32_t seg_len; 12323 mblk_t *xmit_mp; 12324 tcp_stack_t *tcps = tcp->tcp_tcps; 12325 12326 ASSERT(tcp->tcp_sack_info != NULL); 12327 ASSERT(tcp->tcp_notsack_list != NULL); 12328 ASSERT(tcp->tcp_rexmit == B_FALSE); 12329 12330 /* Defensive coding in case there is a bug... */ 12331 if (tcp->tcp_notsack_list == NULL) { 12332 return; 12333 } 12334 notsack_blk = tcp->tcp_notsack_list; 12335 mss = tcp->tcp_mss; 12336 12337 /* 12338 * Limit the num of outstanding data in the network to be 12339 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12340 */ 12341 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12342 12343 /* At least retransmit 1 MSS of data. */ 12344 if (usable_swnd <= 0) { 12345 usable_swnd = mss; 12346 } 12347 12348 /* Make sure no new RTT samples will be taken. */ 12349 tcp->tcp_csuna = tcp->tcp_snxt; 12350 12351 notsack_blk = tcp->tcp_notsack_list; 12352 while (usable_swnd > 0) { 12353 mblk_t *snxt_mp, *tmp_mp; 12354 tcp_seq begin = tcp->tcp_sack_snxt; 12355 tcp_seq end; 12356 int32_t off; 12357 12358 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12359 if (SEQ_GT(notsack_blk->end, begin) && 12360 (notsack_blk->sack_cnt >= 12361 tcps->tcps_dupack_fast_retransmit)) { 12362 end = notsack_blk->end; 12363 if (SEQ_LT(begin, notsack_blk->begin)) { 12364 begin = notsack_blk->begin; 12365 } 12366 break; 12367 } 12368 } 12369 /* 12370 * All holes are filled. Manipulate tcp_cwnd to send more 12371 * if we can. Note that after the SACK recovery, tcp_cwnd is 12372 * set to tcp_cwnd_ssthresh. 12373 */ 12374 if (notsack_blk == NULL) { 12375 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12376 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12377 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12378 ASSERT(tcp->tcp_cwnd > 0); 12379 return; 12380 } else { 12381 usable_swnd = usable_swnd / mss; 12382 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12383 MAX(usable_swnd * mss, mss); 12384 *flags |= TH_XMIT_NEEDED; 12385 return; 12386 } 12387 } 12388 12389 /* 12390 * Note that we may send more than usable_swnd allows here 12391 * because of round off, but no more than 1 MSS of data. 12392 */ 12393 seg_len = end - begin; 12394 if (seg_len > mss) 12395 seg_len = mss; 12396 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12397 ASSERT(snxt_mp != NULL); 12398 /* This should not happen. Defensive coding again... */ 12399 if (snxt_mp == NULL) { 12400 return; 12401 } 12402 12403 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12404 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12405 if (xmit_mp == NULL) 12406 return; 12407 12408 usable_swnd -= seg_len; 12409 tcp->tcp_pipe += seg_len; 12410 tcp->tcp_sack_snxt = begin + seg_len; 12411 12412 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12413 12414 /* 12415 * Update the send timestamp to avoid false retransmission. 12416 */ 12417 snxt_mp->b_prev = (mblk_t *)lbolt; 12418 12419 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12420 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12421 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12422 /* 12423 * Update tcp_rexmit_max to extend this SACK recovery phase. 12424 * This happens when new data sent during fast recovery is 12425 * also lost. If TCP retransmits those new data, it needs 12426 * to extend SACK recover phase to avoid starting another 12427 * fast retransmit/recovery unnecessarily. 12428 */ 12429 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12430 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12431 } 12432 } 12433 } 12434 12435 /* 12436 * This function handles policy checking at TCP level for non-hard_bound/ 12437 * detached connections. 12438 */ 12439 static boolean_t 12440 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12441 boolean_t secure, boolean_t mctl_present) 12442 { 12443 ipsec_latch_t *ipl = NULL; 12444 ipsec_action_t *act = NULL; 12445 mblk_t *data_mp; 12446 ipsec_in_t *ii; 12447 const char *reason; 12448 kstat_named_t *counter; 12449 tcp_stack_t *tcps = tcp->tcp_tcps; 12450 ipsec_stack_t *ipss; 12451 ip_stack_t *ipst; 12452 12453 ASSERT(mctl_present || !secure); 12454 12455 ASSERT((ipha == NULL && ip6h != NULL) || 12456 (ip6h == NULL && ipha != NULL)); 12457 12458 /* 12459 * We don't necessarily have an ipsec_in_act action to verify 12460 * policy because of assymetrical policy where we have only 12461 * outbound policy and no inbound policy (possible with global 12462 * policy). 12463 */ 12464 if (!secure) { 12465 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12466 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12467 return (B_TRUE); 12468 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12469 "tcp_check_policy", ipha, ip6h, secure, 12470 tcps->tcps_netstack); 12471 ipss = tcps->tcps_netstack->netstack_ipsec; 12472 12473 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12474 DROPPER(ipss, ipds_tcp_clear), 12475 &tcps->tcps_dropper); 12476 return (B_FALSE); 12477 } 12478 12479 /* 12480 * We have a secure packet. 12481 */ 12482 if (act == NULL) { 12483 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12484 "tcp_check_policy", ipha, ip6h, secure, 12485 tcps->tcps_netstack); 12486 ipss = tcps->tcps_netstack->netstack_ipsec; 12487 12488 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12489 DROPPER(ipss, ipds_tcp_secure), 12490 &tcps->tcps_dropper); 12491 return (B_FALSE); 12492 } 12493 12494 /* 12495 * XXX This whole routine is currently incorrect. ipl should 12496 * be set to the latch pointer, but is currently not set, so 12497 * we initialize it to NULL to avoid picking up random garbage. 12498 */ 12499 if (ipl == NULL) 12500 return (B_TRUE); 12501 12502 data_mp = first_mp->b_cont; 12503 12504 ii = (ipsec_in_t *)first_mp->b_rptr; 12505 12506 ipst = tcps->tcps_netstack->netstack_ip; 12507 12508 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12509 &counter, tcp->tcp_connp)) { 12510 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12511 return (B_TRUE); 12512 } 12513 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12514 "tcp inbound policy mismatch: %s, packet dropped\n", 12515 reason); 12516 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12517 12518 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12519 &tcps->tcps_dropper); 12520 return (B_FALSE); 12521 } 12522 12523 /* 12524 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12525 * retransmission after a timeout. 12526 * 12527 * To limit the number of duplicate segments, we limit the number of segment 12528 * to be sent in one time to tcp_snd_burst, the burst variable. 12529 */ 12530 static void 12531 tcp_ss_rexmit(tcp_t *tcp) 12532 { 12533 uint32_t snxt; 12534 uint32_t smax; 12535 int32_t win; 12536 int32_t mss; 12537 int32_t off; 12538 int32_t burst = tcp->tcp_snd_burst; 12539 mblk_t *snxt_mp; 12540 tcp_stack_t *tcps = tcp->tcp_tcps; 12541 12542 /* 12543 * Note that tcp_rexmit can be set even though TCP has retransmitted 12544 * all unack'ed segments. 12545 */ 12546 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12547 smax = tcp->tcp_rexmit_max; 12548 snxt = tcp->tcp_rexmit_nxt; 12549 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12550 snxt = tcp->tcp_suna; 12551 } 12552 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12553 win -= snxt - tcp->tcp_suna; 12554 mss = tcp->tcp_mss; 12555 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12556 12557 while (SEQ_LT(snxt, smax) && (win > 0) && 12558 (burst > 0) && (snxt_mp != NULL)) { 12559 mblk_t *xmit_mp; 12560 mblk_t *old_snxt_mp = snxt_mp; 12561 uint32_t cnt = mss; 12562 12563 if (win < cnt) { 12564 cnt = win; 12565 } 12566 if (SEQ_GT(snxt + cnt, smax)) { 12567 cnt = smax - snxt; 12568 } 12569 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12570 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12571 if (xmit_mp == NULL) 12572 return; 12573 12574 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12575 12576 snxt += cnt; 12577 win -= cnt; 12578 /* 12579 * Update the send timestamp to avoid false 12580 * retransmission. 12581 */ 12582 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12583 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12584 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12585 12586 tcp->tcp_rexmit_nxt = snxt; 12587 burst--; 12588 } 12589 /* 12590 * If we have transmitted all we have at the time 12591 * we started the retranmission, we can leave 12592 * the rest of the job to tcp_wput_data(). But we 12593 * need to check the send window first. If the 12594 * win is not 0, go on with tcp_wput_data(). 12595 */ 12596 if (SEQ_LT(snxt, smax) || win == 0) { 12597 return; 12598 } 12599 } 12600 /* Only call tcp_wput_data() if there is data to be sent. */ 12601 if (tcp->tcp_unsent) { 12602 tcp_wput_data(tcp, NULL, B_FALSE); 12603 } 12604 } 12605 12606 /* 12607 * Process all TCP option in SYN segment. Note that this function should 12608 * be called after tcp_adapt_ire() is called so that the necessary info 12609 * from IRE is already set in the tcp structure. 12610 * 12611 * This function sets up the correct tcp_mss value according to the 12612 * MSS option value and our header size. It also sets up the window scale 12613 * and timestamp values, and initialize SACK info blocks. But it does not 12614 * change receive window size after setting the tcp_mss value. The caller 12615 * should do the appropriate change. 12616 */ 12617 void 12618 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12619 { 12620 int options; 12621 tcp_opt_t tcpopt; 12622 uint32_t mss_max; 12623 char *tmp_tcph; 12624 tcp_stack_t *tcps = tcp->tcp_tcps; 12625 12626 tcpopt.tcp = NULL; 12627 options = tcp_parse_options(tcph, &tcpopt); 12628 12629 /* 12630 * Process MSS option. Note that MSS option value does not account 12631 * for IP or TCP options. This means that it is equal to MTU - minimum 12632 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12633 * IPv6. 12634 */ 12635 if (!(options & TCP_OPT_MSS_PRESENT)) { 12636 if (tcp->tcp_ipversion == IPV4_VERSION) 12637 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12638 else 12639 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12640 } else { 12641 if (tcp->tcp_ipversion == IPV4_VERSION) 12642 mss_max = tcps->tcps_mss_max_ipv4; 12643 else 12644 mss_max = tcps->tcps_mss_max_ipv6; 12645 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12646 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12647 else if (tcpopt.tcp_opt_mss > mss_max) 12648 tcpopt.tcp_opt_mss = mss_max; 12649 } 12650 12651 /* Process Window Scale option. */ 12652 if (options & TCP_OPT_WSCALE_PRESENT) { 12653 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12654 tcp->tcp_snd_ws_ok = B_TRUE; 12655 } else { 12656 tcp->tcp_snd_ws = B_FALSE; 12657 tcp->tcp_snd_ws_ok = B_FALSE; 12658 tcp->tcp_rcv_ws = B_FALSE; 12659 } 12660 12661 /* Process Timestamp option. */ 12662 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12663 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12664 tmp_tcph = (char *)tcp->tcp_tcph; 12665 12666 tcp->tcp_snd_ts_ok = B_TRUE; 12667 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12668 tcp->tcp_last_rcv_lbolt = lbolt64; 12669 ASSERT(OK_32PTR(tmp_tcph)); 12670 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12671 12672 /* Fill in our template header with basic timestamp option. */ 12673 tmp_tcph += tcp->tcp_tcp_hdr_len; 12674 tmp_tcph[0] = TCPOPT_NOP; 12675 tmp_tcph[1] = TCPOPT_NOP; 12676 tmp_tcph[2] = TCPOPT_TSTAMP; 12677 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12678 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12679 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12680 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12681 } else { 12682 tcp->tcp_snd_ts_ok = B_FALSE; 12683 } 12684 12685 /* 12686 * Process SACK options. If SACK is enabled for this connection, 12687 * then allocate the SACK info structure. Note the following ways 12688 * when tcp_snd_sack_ok is set to true. 12689 * 12690 * For active connection: in tcp_adapt_ire() called in 12691 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12692 * is checked. 12693 * 12694 * For passive connection: in tcp_adapt_ire() called in 12695 * tcp_accept_comm(). 12696 * 12697 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12698 * That check makes sure that if we did not send a SACK OK option, 12699 * we will not enable SACK for this connection even though the other 12700 * side sends us SACK OK option. For active connection, the SACK 12701 * info structure has already been allocated. So we need to free 12702 * it if SACK is disabled. 12703 */ 12704 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12705 (tcp->tcp_snd_sack_ok || 12706 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12707 /* This should be true only in the passive case. */ 12708 if (tcp->tcp_sack_info == NULL) { 12709 ASSERT(TCP_IS_DETACHED(tcp)); 12710 tcp->tcp_sack_info = 12711 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12712 } 12713 if (tcp->tcp_sack_info == NULL) { 12714 tcp->tcp_snd_sack_ok = B_FALSE; 12715 } else { 12716 tcp->tcp_snd_sack_ok = B_TRUE; 12717 if (tcp->tcp_snd_ts_ok) { 12718 tcp->tcp_max_sack_blk = 3; 12719 } else { 12720 tcp->tcp_max_sack_blk = 4; 12721 } 12722 } 12723 } else { 12724 /* 12725 * Resetting tcp_snd_sack_ok to B_FALSE so that 12726 * no SACK info will be used for this 12727 * connection. This assumes that SACK usage 12728 * permission is negotiated. This may need 12729 * to be changed once this is clarified. 12730 */ 12731 if (tcp->tcp_sack_info != NULL) { 12732 ASSERT(tcp->tcp_notsack_list == NULL); 12733 kmem_cache_free(tcp_sack_info_cache, 12734 tcp->tcp_sack_info); 12735 tcp->tcp_sack_info = NULL; 12736 } 12737 tcp->tcp_snd_sack_ok = B_FALSE; 12738 } 12739 12740 /* 12741 * Now we know the exact TCP/IP header length, subtract 12742 * that from tcp_mss to get our side's MSS. 12743 */ 12744 tcp->tcp_mss -= tcp->tcp_hdr_len; 12745 /* 12746 * Here we assume that the other side's header size will be equal to 12747 * our header size. We calculate the real MSS accordingly. Need to 12748 * take into additional stuffs IPsec puts in. 12749 * 12750 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12751 */ 12752 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12753 ((tcp->tcp_ipversion == IPV4_VERSION ? 12754 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12755 12756 /* 12757 * Set MSS to the smaller one of both ends of the connection. 12758 * We should not have called tcp_mss_set() before, but our 12759 * side of the MSS should have been set to a proper value 12760 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12761 * STREAM head parameters properly. 12762 * 12763 * If we have a larger-than-16-bit window but the other side 12764 * didn't want to do window scale, tcp_rwnd_set() will take 12765 * care of that. 12766 */ 12767 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12768 } 12769 12770 /* 12771 * Sends the T_CONN_IND to the listener. The caller calls this 12772 * functions via squeue to get inside the listener's perimeter 12773 * once the 3 way hand shake is done a T_CONN_IND needs to be 12774 * sent. As an optimization, the caller can call this directly 12775 * if listener's perimeter is same as eager's. 12776 */ 12777 /* ARGSUSED */ 12778 void 12779 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12780 { 12781 conn_t *lconnp = (conn_t *)arg; 12782 tcp_t *listener = lconnp->conn_tcp; 12783 tcp_t *tcp; 12784 struct T_conn_ind *conn_ind; 12785 ipaddr_t *addr_cache; 12786 boolean_t need_send_conn_ind = B_FALSE; 12787 tcp_stack_t *tcps = listener->tcp_tcps; 12788 12789 /* retrieve the eager */ 12790 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12791 ASSERT(conn_ind->OPT_offset != 0 && 12792 conn_ind->OPT_length == sizeof (intptr_t)); 12793 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12794 conn_ind->OPT_length); 12795 12796 /* 12797 * TLI/XTI applications will get confused by 12798 * sending eager as an option since it violates 12799 * the option semantics. So remove the eager as 12800 * option since TLI/XTI app doesn't need it anyway. 12801 */ 12802 if (!TCP_IS_SOCKET(listener)) { 12803 conn_ind->OPT_length = 0; 12804 conn_ind->OPT_offset = 0; 12805 } 12806 if (listener->tcp_state == TCPS_CLOSED || 12807 TCP_IS_DETACHED(listener)) { 12808 /* 12809 * If listener has closed, it would have caused a 12810 * a cleanup/blowoff to happen for the eager. We 12811 * just need to return. 12812 */ 12813 freemsg(mp); 12814 return; 12815 } 12816 12817 12818 /* 12819 * if the conn_req_q is full defer passing up the 12820 * T_CONN_IND until space is availabe after t_accept() 12821 * processing 12822 */ 12823 mutex_enter(&listener->tcp_eager_lock); 12824 12825 /* 12826 * Take the eager out, if it is in the list of droppable eagers 12827 * as we are here because the 3W handshake is over. 12828 */ 12829 MAKE_UNDROPPABLE(tcp); 12830 12831 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12832 tcp_t *tail; 12833 12834 /* 12835 * The eager already has an extra ref put in tcp_rput_data 12836 * so that it stays till accept comes back even though it 12837 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12838 */ 12839 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12840 listener->tcp_conn_req_cnt_q0--; 12841 listener->tcp_conn_req_cnt_q++; 12842 12843 /* Move from SYN_RCVD to ESTABLISHED list */ 12844 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12845 tcp->tcp_eager_prev_q0; 12846 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12847 tcp->tcp_eager_next_q0; 12848 tcp->tcp_eager_prev_q0 = NULL; 12849 tcp->tcp_eager_next_q0 = NULL; 12850 12851 /* 12852 * Insert at end of the queue because sockfs 12853 * sends down T_CONN_RES in chronological 12854 * order. Leaving the older conn indications 12855 * at front of the queue helps reducing search 12856 * time. 12857 */ 12858 tail = listener->tcp_eager_last_q; 12859 if (tail != NULL) 12860 tail->tcp_eager_next_q = tcp; 12861 else 12862 listener->tcp_eager_next_q = tcp; 12863 listener->tcp_eager_last_q = tcp; 12864 tcp->tcp_eager_next_q = NULL; 12865 /* 12866 * Delay sending up the T_conn_ind until we are 12867 * done with the eager. Once we have have sent up 12868 * the T_conn_ind, the accept can potentially complete 12869 * any time and release the refhold we have on the eager. 12870 */ 12871 need_send_conn_ind = B_TRUE; 12872 } else { 12873 /* 12874 * Defer connection on q0 and set deferred 12875 * connection bit true 12876 */ 12877 tcp->tcp_conn_def_q0 = B_TRUE; 12878 12879 /* take tcp out of q0 ... */ 12880 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12881 tcp->tcp_eager_next_q0; 12882 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12883 tcp->tcp_eager_prev_q0; 12884 12885 /* ... and place it at the end of q0 */ 12886 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12887 tcp->tcp_eager_next_q0 = listener; 12888 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12889 listener->tcp_eager_prev_q0 = tcp; 12890 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12891 } 12892 12893 /* we have timed out before */ 12894 if (tcp->tcp_syn_rcvd_timeout != 0) { 12895 tcp->tcp_syn_rcvd_timeout = 0; 12896 listener->tcp_syn_rcvd_timeout--; 12897 if (listener->tcp_syn_defense && 12898 listener->tcp_syn_rcvd_timeout <= 12899 (tcps->tcps_conn_req_max_q0 >> 5) && 12900 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12901 listener->tcp_last_rcv_lbolt)) { 12902 /* 12903 * Turn off the defense mode if we 12904 * believe the SYN attack is over. 12905 */ 12906 listener->tcp_syn_defense = B_FALSE; 12907 if (listener->tcp_ip_addr_cache) { 12908 kmem_free((void *)listener->tcp_ip_addr_cache, 12909 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12910 listener->tcp_ip_addr_cache = NULL; 12911 } 12912 } 12913 } 12914 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12915 if (addr_cache != NULL) { 12916 /* 12917 * We have finished a 3-way handshake with this 12918 * remote host. This proves the IP addr is good. 12919 * Cache it! 12920 */ 12921 addr_cache[IP_ADDR_CACHE_HASH( 12922 tcp->tcp_remote)] = tcp->tcp_remote; 12923 } 12924 mutex_exit(&listener->tcp_eager_lock); 12925 if (need_send_conn_ind) 12926 putnext(listener->tcp_rq, mp); 12927 } 12928 12929 mblk_t * 12930 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12931 uint_t *ifindexp, ip6_pkt_t *ippp) 12932 { 12933 ip_pktinfo_t *pinfo; 12934 ip6_t *ip6h; 12935 uchar_t *rptr; 12936 mblk_t *first_mp = mp; 12937 boolean_t mctl_present = B_FALSE; 12938 uint_t ifindex = 0; 12939 ip6_pkt_t ipp; 12940 uint_t ipvers; 12941 uint_t ip_hdr_len; 12942 tcp_stack_t *tcps = tcp->tcp_tcps; 12943 12944 rptr = mp->b_rptr; 12945 ASSERT(OK_32PTR(rptr)); 12946 ASSERT(tcp != NULL); 12947 ipp.ipp_fields = 0; 12948 12949 switch DB_TYPE(mp) { 12950 case M_CTL: 12951 mp = mp->b_cont; 12952 if (mp == NULL) { 12953 freemsg(first_mp); 12954 return (NULL); 12955 } 12956 if (DB_TYPE(mp) != M_DATA) { 12957 freemsg(first_mp); 12958 return (NULL); 12959 } 12960 mctl_present = B_TRUE; 12961 break; 12962 case M_DATA: 12963 break; 12964 default: 12965 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12966 freemsg(mp); 12967 return (NULL); 12968 } 12969 ipvers = IPH_HDR_VERSION(rptr); 12970 if (ipvers == IPV4_VERSION) { 12971 if (tcp == NULL) { 12972 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12973 goto done; 12974 } 12975 12976 ipp.ipp_fields |= IPPF_HOPLIMIT; 12977 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12978 12979 /* 12980 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12981 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12982 */ 12983 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12984 mctl_present) { 12985 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12986 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12987 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12988 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12989 ipp.ipp_fields |= IPPF_IFINDEX; 12990 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12991 ifindex = pinfo->ip_pkt_ifindex; 12992 } 12993 freeb(first_mp); 12994 mctl_present = B_FALSE; 12995 } 12996 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12997 } else { 12998 ip6h = (ip6_t *)rptr; 12999 13000 ASSERT(ipvers == IPV6_VERSION); 13001 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 13002 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 13003 ipp.ipp_hoplimit = ip6h->ip6_hops; 13004 13005 if (ip6h->ip6_nxt != IPPROTO_TCP) { 13006 uint8_t nexthdrp; 13007 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13008 13009 /* Look for ifindex information */ 13010 if (ip6h->ip6_nxt == IPPROTO_RAW) { 13011 ip6i_t *ip6i = (ip6i_t *)ip6h; 13012 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 13013 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13014 freemsg(first_mp); 13015 return (NULL); 13016 } 13017 13018 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 13019 ASSERT(ip6i->ip6i_ifindex != 0); 13020 ipp.ipp_fields |= IPPF_IFINDEX; 13021 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 13022 ifindex = ip6i->ip6i_ifindex; 13023 } 13024 rptr = (uchar_t *)&ip6i[1]; 13025 mp->b_rptr = rptr; 13026 if (rptr == mp->b_wptr) { 13027 mblk_t *mp1; 13028 mp1 = mp->b_cont; 13029 freeb(mp); 13030 mp = mp1; 13031 rptr = mp->b_rptr; 13032 } 13033 if (MBLKL(mp) < IPV6_HDR_LEN + 13034 sizeof (tcph_t)) { 13035 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13036 freemsg(first_mp); 13037 return (NULL); 13038 } 13039 ip6h = (ip6_t *)rptr; 13040 } 13041 13042 /* 13043 * Find any potentially interesting extension headers 13044 * as well as the length of the IPv6 + extension 13045 * headers. 13046 */ 13047 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 13048 /* Verify if this is a TCP packet */ 13049 if (nexthdrp != IPPROTO_TCP) { 13050 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13051 freemsg(first_mp); 13052 return (NULL); 13053 } 13054 } else { 13055 ip_hdr_len = IPV6_HDR_LEN; 13056 } 13057 } 13058 13059 done: 13060 if (ipversp != NULL) 13061 *ipversp = ipvers; 13062 if (ip_hdr_lenp != NULL) 13063 *ip_hdr_lenp = ip_hdr_len; 13064 if (ippp != NULL) 13065 *ippp = ipp; 13066 if (ifindexp != NULL) 13067 *ifindexp = ifindex; 13068 if (mctl_present) { 13069 freeb(first_mp); 13070 } 13071 return (mp); 13072 } 13073 13074 /* 13075 * Handle M_DATA messages from IP. Its called directly from IP via 13076 * squeue for AF_INET type sockets fast path. No M_CTL are expected 13077 * in this path. 13078 * 13079 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 13080 * v4 and v6), we are called through tcp_input() and a M_CTL can 13081 * be present for options but tcp_find_pktinfo() deals with it. We 13082 * only expect M_DATA packets after tcp_find_pktinfo() is done. 13083 * 13084 * The first argument is always the connp/tcp to which the mp belongs. 13085 * There are no exceptions to this rule. The caller has already put 13086 * a reference on this connp/tcp and once tcp_rput_data() returns, 13087 * the squeue will do the refrele. 13088 * 13089 * The TH_SYN for the listener directly go to tcp_conn_request via 13090 * squeue. 13091 * 13092 * sqp: NULL = recursive, sqp != NULL means called from squeue 13093 */ 13094 void 13095 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 13096 { 13097 int32_t bytes_acked; 13098 int32_t gap; 13099 mblk_t *mp1; 13100 uint_t flags; 13101 uint32_t new_swnd = 0; 13102 uchar_t *iphdr; 13103 uchar_t *rptr; 13104 int32_t rgap; 13105 uint32_t seg_ack; 13106 int seg_len; 13107 uint_t ip_hdr_len; 13108 uint32_t seg_seq; 13109 tcph_t *tcph; 13110 int urp; 13111 tcp_opt_t tcpopt; 13112 uint_t ipvers; 13113 ip6_pkt_t ipp; 13114 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 13115 uint32_t cwnd; 13116 uint32_t add; 13117 int npkt; 13118 int mss; 13119 conn_t *connp = (conn_t *)arg; 13120 squeue_t *sqp = (squeue_t *)arg2; 13121 tcp_t *tcp = connp->conn_tcp; 13122 tcp_stack_t *tcps = tcp->tcp_tcps; 13123 13124 /* 13125 * RST from fused tcp loopback peer should trigger an unfuse. 13126 */ 13127 if (tcp->tcp_fused) { 13128 TCP_STAT(tcps, tcp_fusion_aborted); 13129 tcp_unfuse(tcp); 13130 } 13131 13132 iphdr = mp->b_rptr; 13133 rptr = mp->b_rptr; 13134 ASSERT(OK_32PTR(rptr)); 13135 13136 /* 13137 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 13138 * processing here. For rest call tcp_find_pktinfo to fill up the 13139 * necessary information. 13140 */ 13141 if (IPCL_IS_TCP4(connp)) { 13142 ipvers = IPV4_VERSION; 13143 ip_hdr_len = IPH_HDR_LENGTH(rptr); 13144 } else { 13145 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 13146 NULL, &ipp); 13147 if (mp == NULL) { 13148 TCP_STAT(tcps, tcp_rput_v6_error); 13149 return; 13150 } 13151 iphdr = mp->b_rptr; 13152 rptr = mp->b_rptr; 13153 } 13154 ASSERT(DB_TYPE(mp) == M_DATA); 13155 13156 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13157 seg_seq = ABE32_TO_U32(tcph->th_seq); 13158 seg_ack = ABE32_TO_U32(tcph->th_ack); 13159 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 13160 seg_len = (int)(mp->b_wptr - rptr) - 13161 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 13162 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 13163 do { 13164 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 13165 (uintptr_t)INT_MAX); 13166 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 13167 } while ((mp1 = mp1->b_cont) != NULL && 13168 mp1->b_datap->db_type == M_DATA); 13169 } 13170 13171 if (tcp->tcp_state == TCPS_TIME_WAIT) { 13172 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 13173 seg_len, tcph); 13174 return; 13175 } 13176 13177 if (sqp != NULL) { 13178 /* 13179 * This is the correct place to update tcp_last_recv_time. Note 13180 * that it is also updated for tcp structure that belongs to 13181 * global and listener queues which do not really need updating. 13182 * But that should not cause any harm. And it is updated for 13183 * all kinds of incoming segments, not only for data segments. 13184 */ 13185 tcp->tcp_last_recv_time = lbolt; 13186 } 13187 13188 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 13189 13190 BUMP_LOCAL(tcp->tcp_ibsegs); 13191 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13192 13193 if ((flags & TH_URG) && sqp != NULL) { 13194 /* 13195 * TCP can't handle urgent pointers that arrive before 13196 * the connection has been accept()ed since it can't 13197 * buffer OOB data. Discard segment if this happens. 13198 * 13199 * We can't just rely on a non-null tcp_listener to indicate 13200 * that the accept() has completed since unlinking of the 13201 * eager and completion of the accept are not atomic. 13202 * tcp_detached, when it is not set (B_FALSE) indicates 13203 * that the accept() has completed. 13204 * 13205 * Nor can it reassemble urgent pointers, so discard 13206 * if it's not the next segment expected. 13207 * 13208 * Otherwise, collapse chain into one mblk (discard if 13209 * that fails). This makes sure the headers, retransmitted 13210 * data, and new data all are in the same mblk. 13211 */ 13212 ASSERT(mp != NULL); 13213 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 13214 freemsg(mp); 13215 return; 13216 } 13217 /* Update pointers into message */ 13218 iphdr = rptr = mp->b_rptr; 13219 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13220 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 13221 /* 13222 * Since we can't handle any data with this urgent 13223 * pointer that is out of sequence, we expunge 13224 * the data. This allows us to still register 13225 * the urgent mark and generate the M_PCSIG, 13226 * which we can do. 13227 */ 13228 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13229 seg_len = 0; 13230 } 13231 } 13232 13233 switch (tcp->tcp_state) { 13234 case TCPS_SYN_SENT: 13235 if (flags & TH_ACK) { 13236 /* 13237 * Note that our stack cannot send data before a 13238 * connection is established, therefore the 13239 * following check is valid. Otherwise, it has 13240 * to be changed. 13241 */ 13242 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 13243 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13244 freemsg(mp); 13245 if (flags & TH_RST) 13246 return; 13247 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 13248 tcp, seg_ack, 0, TH_RST); 13249 return; 13250 } 13251 ASSERT(tcp->tcp_suna + 1 == seg_ack); 13252 } 13253 if (flags & TH_RST) { 13254 freemsg(mp); 13255 if (flags & TH_ACK) 13256 (void) tcp_clean_death(tcp, 13257 ECONNREFUSED, 13); 13258 return; 13259 } 13260 if (!(flags & TH_SYN)) { 13261 freemsg(mp); 13262 return; 13263 } 13264 13265 /* Process all TCP options. */ 13266 tcp_process_options(tcp, tcph); 13267 /* 13268 * The following changes our rwnd to be a multiple of the 13269 * MIN(peer MSS, our MSS) for performance reason. 13270 */ 13271 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 13272 tcp->tcp_mss)); 13273 13274 /* Is the other end ECN capable? */ 13275 if (tcp->tcp_ecn_ok) { 13276 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 13277 tcp->tcp_ecn_ok = B_FALSE; 13278 } 13279 } 13280 /* 13281 * Clear ECN flags because it may interfere with later 13282 * processing. 13283 */ 13284 flags &= ~(TH_ECE|TH_CWR); 13285 13286 tcp->tcp_irs = seg_seq; 13287 tcp->tcp_rack = seg_seq; 13288 tcp->tcp_rnxt = seg_seq + 1; 13289 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13290 if (!TCP_IS_DETACHED(tcp)) { 13291 /* Allocate room for SACK options if needed. */ 13292 if (tcp->tcp_snd_sack_ok) { 13293 (void) mi_set_sth_wroff(tcp->tcp_rq, 13294 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 13295 (tcp->tcp_loopback ? 0 : 13296 tcps->tcps_wroff_xtra)); 13297 } else { 13298 (void) mi_set_sth_wroff(tcp->tcp_rq, 13299 tcp->tcp_hdr_len + 13300 (tcp->tcp_loopback ? 0 : 13301 tcps->tcps_wroff_xtra)); 13302 } 13303 } 13304 if (flags & TH_ACK) { 13305 /* 13306 * If we can't get the confirmation upstream, pretend 13307 * we didn't even see this one. 13308 * 13309 * XXX: how can we pretend we didn't see it if we 13310 * have updated rnxt et. al. 13311 * 13312 * For loopback we defer sending up the T_CONN_CON 13313 * until after some checks below. 13314 */ 13315 mp1 = NULL; 13316 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13317 tcp->tcp_loopback ? &mp1 : NULL)) { 13318 freemsg(mp); 13319 return; 13320 } 13321 /* SYN was acked - making progress */ 13322 if (tcp->tcp_ipversion == IPV6_VERSION) 13323 tcp->tcp_ip_forward_progress = B_TRUE; 13324 13325 /* One for the SYN */ 13326 tcp->tcp_suna = tcp->tcp_iss + 1; 13327 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13328 tcp->tcp_state = TCPS_ESTABLISHED; 13329 13330 /* 13331 * If SYN was retransmitted, need to reset all 13332 * retransmission info. This is because this 13333 * segment will be treated as a dup ACK. 13334 */ 13335 if (tcp->tcp_rexmit) { 13336 tcp->tcp_rexmit = B_FALSE; 13337 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13338 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13339 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13340 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13341 tcp->tcp_ms_we_have_waited = 0; 13342 13343 /* 13344 * Set tcp_cwnd back to 1 MSS, per 13345 * recommendation from 13346 * draft-floyd-incr-init-win-01.txt, 13347 * Increasing TCP's Initial Window. 13348 */ 13349 tcp->tcp_cwnd = tcp->tcp_mss; 13350 } 13351 13352 tcp->tcp_swl1 = seg_seq; 13353 tcp->tcp_swl2 = seg_ack; 13354 13355 new_swnd = BE16_TO_U16(tcph->th_win); 13356 tcp->tcp_swnd = new_swnd; 13357 if (new_swnd > tcp->tcp_max_swnd) 13358 tcp->tcp_max_swnd = new_swnd; 13359 13360 /* 13361 * Always send the three-way handshake ack immediately 13362 * in order to make the connection complete as soon as 13363 * possible on the accepting host. 13364 */ 13365 flags |= TH_ACK_NEEDED; 13366 13367 /* 13368 * Special case for loopback. At this point we have 13369 * received SYN-ACK from the remote endpoint. In 13370 * order to ensure that both endpoints reach the 13371 * fused state prior to any data exchange, the final 13372 * ACK needs to be sent before we indicate T_CONN_CON 13373 * to the module upstream. 13374 */ 13375 if (tcp->tcp_loopback) { 13376 mblk_t *ack_mp; 13377 13378 ASSERT(!tcp->tcp_unfusable); 13379 ASSERT(mp1 != NULL); 13380 /* 13381 * For loopback, we always get a pure SYN-ACK 13382 * and only need to send back the final ACK 13383 * with no data (this is because the other 13384 * tcp is ours and we don't do T/TCP). This 13385 * final ACK triggers the passive side to 13386 * perform fusion in ESTABLISHED state. 13387 */ 13388 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13389 if (tcp->tcp_ack_tid != 0) { 13390 (void) TCP_TIMER_CANCEL(tcp, 13391 tcp->tcp_ack_tid); 13392 tcp->tcp_ack_tid = 0; 13393 } 13394 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13395 BUMP_LOCAL(tcp->tcp_obsegs); 13396 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13397 13398 /* Send up T_CONN_CON */ 13399 putnext(tcp->tcp_rq, mp1); 13400 13401 freemsg(mp); 13402 return; 13403 } 13404 /* 13405 * Forget fusion; we need to handle more 13406 * complex cases below. Send the deferred 13407 * T_CONN_CON message upstream and proceed 13408 * as usual. Mark this tcp as not capable 13409 * of fusion. 13410 */ 13411 TCP_STAT(tcps, tcp_fusion_unfusable); 13412 tcp->tcp_unfusable = B_TRUE; 13413 putnext(tcp->tcp_rq, mp1); 13414 } 13415 13416 /* 13417 * Check to see if there is data to be sent. If 13418 * yes, set the transmit flag. Then check to see 13419 * if received data processing needs to be done. 13420 * If not, go straight to xmit_check. This short 13421 * cut is OK as we don't support T/TCP. 13422 */ 13423 if (tcp->tcp_unsent) 13424 flags |= TH_XMIT_NEEDED; 13425 13426 if (seg_len == 0 && !(flags & TH_URG)) { 13427 freemsg(mp); 13428 goto xmit_check; 13429 } 13430 13431 flags &= ~TH_SYN; 13432 seg_seq++; 13433 break; 13434 } 13435 tcp->tcp_state = TCPS_SYN_RCVD; 13436 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13437 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13438 if (mp1) { 13439 DB_CPID(mp1) = tcp->tcp_cpid; 13440 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13441 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13442 } 13443 freemsg(mp); 13444 return; 13445 case TCPS_SYN_RCVD: 13446 if (flags & TH_ACK) { 13447 /* 13448 * In this state, a SYN|ACK packet is either bogus 13449 * because the other side must be ACKing our SYN which 13450 * indicates it has seen the ACK for their SYN and 13451 * shouldn't retransmit it or we're crossing SYNs 13452 * on active open. 13453 */ 13454 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13455 freemsg(mp); 13456 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13457 tcp, seg_ack, 0, TH_RST); 13458 return; 13459 } 13460 /* 13461 * NOTE: RFC 793 pg. 72 says this should be 13462 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13463 * but that would mean we have an ack that ignored 13464 * our SYN. 13465 */ 13466 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13467 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13468 freemsg(mp); 13469 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13470 tcp, seg_ack, 0, TH_RST); 13471 return; 13472 } 13473 } 13474 break; 13475 case TCPS_LISTEN: 13476 /* 13477 * Only a TLI listener can come through this path when a 13478 * acceptor is going back to be a listener and a packet 13479 * for the acceptor hits the classifier. For a socket 13480 * listener, this can never happen because a listener 13481 * can never accept connection on itself and hence a 13482 * socket acceptor can not go back to being a listener. 13483 */ 13484 ASSERT(!TCP_IS_SOCKET(tcp)); 13485 /*FALLTHRU*/ 13486 case TCPS_CLOSED: 13487 case TCPS_BOUND: { 13488 conn_t *new_connp; 13489 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13490 13491 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13492 if (new_connp != NULL) { 13493 tcp_reinput(new_connp, mp, connp->conn_sqp); 13494 return; 13495 } 13496 /* We failed to classify. For now just drop the packet */ 13497 freemsg(mp); 13498 return; 13499 } 13500 case TCPS_IDLE: 13501 /* 13502 * Handle the case where the tcp_clean_death() has happened 13503 * on a connection (application hasn't closed yet) but a packet 13504 * was already queued on squeue before tcp_clean_death() 13505 * was processed. Calling tcp_clean_death() twice on same 13506 * connection can result in weird behaviour. 13507 */ 13508 freemsg(mp); 13509 return; 13510 default: 13511 break; 13512 } 13513 13514 /* 13515 * Already on the correct queue/perimeter. 13516 * If this is a detached connection and not an eager 13517 * connection hanging off a listener then new data 13518 * (past the FIN) will cause a reset. 13519 * We do a special check here where it 13520 * is out of the main line, rather than check 13521 * if we are detached every time we see new 13522 * data down below. 13523 */ 13524 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13525 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13526 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13527 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13528 13529 freemsg(mp); 13530 /* 13531 * This could be an SSL closure alert. We're detached so just 13532 * acknowledge it this last time. 13533 */ 13534 if (tcp->tcp_kssl_ctx != NULL) { 13535 kssl_release_ctx(tcp->tcp_kssl_ctx); 13536 tcp->tcp_kssl_ctx = NULL; 13537 13538 tcp->tcp_rnxt += seg_len; 13539 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13540 flags |= TH_ACK_NEEDED; 13541 goto ack_check; 13542 } 13543 13544 tcp_xmit_ctl("new data when detached", tcp, 13545 tcp->tcp_snxt, 0, TH_RST); 13546 (void) tcp_clean_death(tcp, EPROTO, 12); 13547 return; 13548 } 13549 13550 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13551 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13552 new_swnd = BE16_TO_U16(tcph->th_win) << 13553 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13554 13555 if (tcp->tcp_snd_ts_ok) { 13556 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13557 /* 13558 * This segment is not acceptable. 13559 * Drop it and send back an ACK. 13560 */ 13561 freemsg(mp); 13562 flags |= TH_ACK_NEEDED; 13563 goto ack_check; 13564 } 13565 } else if (tcp->tcp_snd_sack_ok) { 13566 ASSERT(tcp->tcp_sack_info != NULL); 13567 tcpopt.tcp = tcp; 13568 /* 13569 * SACK info in already updated in tcp_parse_options. Ignore 13570 * all other TCP options... 13571 */ 13572 (void) tcp_parse_options(tcph, &tcpopt); 13573 } 13574 try_again:; 13575 mss = tcp->tcp_mss; 13576 gap = seg_seq - tcp->tcp_rnxt; 13577 rgap = tcp->tcp_rwnd - (gap + seg_len); 13578 /* 13579 * gap is the amount of sequence space between what we expect to see 13580 * and what we got for seg_seq. A positive value for gap means 13581 * something got lost. A negative value means we got some old stuff. 13582 */ 13583 if (gap < 0) { 13584 /* Old stuff present. Is the SYN in there? */ 13585 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13586 (seg_len != 0)) { 13587 flags &= ~TH_SYN; 13588 seg_seq++; 13589 urp--; 13590 /* Recompute the gaps after noting the SYN. */ 13591 goto try_again; 13592 } 13593 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13594 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13595 (seg_len > -gap ? -gap : seg_len)); 13596 /* Remove the old stuff from seg_len. */ 13597 seg_len += gap; 13598 /* 13599 * Anything left? 13600 * Make sure to check for unack'd FIN when rest of data 13601 * has been previously ack'd. 13602 */ 13603 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13604 /* 13605 * Resets are only valid if they lie within our offered 13606 * window. If the RST bit is set, we just ignore this 13607 * segment. 13608 */ 13609 if (flags & TH_RST) { 13610 freemsg(mp); 13611 return; 13612 } 13613 13614 /* 13615 * The arriving of dup data packets indicate that we 13616 * may have postponed an ack for too long, or the other 13617 * side's RTT estimate is out of shape. Start acking 13618 * more often. 13619 */ 13620 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13621 tcp->tcp_rack_cnt >= 1 && 13622 tcp->tcp_rack_abs_max > 2) { 13623 tcp->tcp_rack_abs_max--; 13624 } 13625 tcp->tcp_rack_cur_max = 1; 13626 13627 /* 13628 * This segment is "unacceptable". None of its 13629 * sequence space lies within our advertized window. 13630 * 13631 * Adjust seg_len to the original value for tracing. 13632 */ 13633 seg_len -= gap; 13634 if (tcp->tcp_debug) { 13635 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13636 "tcp_rput: unacceptable, gap %d, rgap %d, " 13637 "flags 0x%x, seg_seq %u, seg_ack %u, " 13638 "seg_len %d, rnxt %u, snxt %u, %s", 13639 gap, rgap, flags, seg_seq, seg_ack, 13640 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13641 tcp_display(tcp, NULL, 13642 DISP_ADDR_AND_PORT)); 13643 } 13644 13645 /* 13646 * Arrange to send an ACK in response to the 13647 * unacceptable segment per RFC 793 page 69. There 13648 * is only one small difference between ours and the 13649 * acceptability test in the RFC - we accept ACK-only 13650 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13651 * will be generated. 13652 * 13653 * Note that we have to ACK an ACK-only packet at least 13654 * for stacks that send 0-length keep-alives with 13655 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13656 * section 4.2.3.6. As long as we don't ever generate 13657 * an unacceptable packet in response to an incoming 13658 * packet that is unacceptable, it should not cause 13659 * "ACK wars". 13660 */ 13661 flags |= TH_ACK_NEEDED; 13662 13663 /* 13664 * Continue processing this segment in order to use the 13665 * ACK information it contains, but skip all other 13666 * sequence-number processing. Processing the ACK 13667 * information is necessary in order to 13668 * re-synchronize connections that may have lost 13669 * synchronization. 13670 * 13671 * We clear seg_len and flag fields related to 13672 * sequence number processing as they are not 13673 * to be trusted for an unacceptable segment. 13674 */ 13675 seg_len = 0; 13676 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13677 goto process_ack; 13678 } 13679 13680 /* Fix seg_seq, and chew the gap off the front. */ 13681 seg_seq = tcp->tcp_rnxt; 13682 urp += gap; 13683 do { 13684 mblk_t *mp2; 13685 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13686 (uintptr_t)UINT_MAX); 13687 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13688 if (gap > 0) { 13689 mp->b_rptr = mp->b_wptr - gap; 13690 break; 13691 } 13692 mp2 = mp; 13693 mp = mp->b_cont; 13694 freeb(mp2); 13695 } while (gap < 0); 13696 /* 13697 * If the urgent data has already been acknowledged, we 13698 * should ignore TH_URG below 13699 */ 13700 if (urp < 0) 13701 flags &= ~TH_URG; 13702 } 13703 /* 13704 * rgap is the amount of stuff received out of window. A negative 13705 * value is the amount out of window. 13706 */ 13707 if (rgap < 0) { 13708 mblk_t *mp2; 13709 13710 if (tcp->tcp_rwnd == 0) { 13711 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13712 } else { 13713 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13714 UPDATE_MIB(&tcps->tcps_mib, 13715 tcpInDataPastWinBytes, -rgap); 13716 } 13717 13718 /* 13719 * seg_len does not include the FIN, so if more than 13720 * just the FIN is out of window, we act like we don't 13721 * see it. (If just the FIN is out of window, rgap 13722 * will be zero and we will go ahead and acknowledge 13723 * the FIN.) 13724 */ 13725 flags &= ~TH_FIN; 13726 13727 /* Fix seg_len and make sure there is something left. */ 13728 seg_len += rgap; 13729 if (seg_len <= 0) { 13730 /* 13731 * Resets are only valid if they lie within our offered 13732 * window. If the RST bit is set, we just ignore this 13733 * segment. 13734 */ 13735 if (flags & TH_RST) { 13736 freemsg(mp); 13737 return; 13738 } 13739 13740 /* Per RFC 793, we need to send back an ACK. */ 13741 flags |= TH_ACK_NEEDED; 13742 13743 /* 13744 * Send SIGURG as soon as possible i.e. even 13745 * if the TH_URG was delivered in a window probe 13746 * packet (which will be unacceptable). 13747 * 13748 * We generate a signal if none has been generated 13749 * for this connection or if this is a new urgent 13750 * byte. Also send a zero-length "unmarked" message 13751 * to inform SIOCATMARK that this is not the mark. 13752 * 13753 * tcp_urp_last_valid is cleared when the T_exdata_ind 13754 * is sent up. This plus the check for old data 13755 * (gap >= 0) handles the wraparound of the sequence 13756 * number space without having to always track the 13757 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13758 * this max in its rcv_up variable). 13759 * 13760 * This prevents duplicate SIGURGS due to a "late" 13761 * zero-window probe when the T_EXDATA_IND has already 13762 * been sent up. 13763 */ 13764 if ((flags & TH_URG) && 13765 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13766 tcp->tcp_urp_last))) { 13767 mp1 = allocb(0, BPRI_MED); 13768 if (mp1 == NULL) { 13769 freemsg(mp); 13770 return; 13771 } 13772 if (!TCP_IS_DETACHED(tcp) && 13773 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13774 SIGURG)) { 13775 /* Try again on the rexmit. */ 13776 freemsg(mp1); 13777 freemsg(mp); 13778 return; 13779 } 13780 /* 13781 * If the next byte would be the mark 13782 * then mark with MARKNEXT else mark 13783 * with NOTMARKNEXT. 13784 */ 13785 if (gap == 0 && urp == 0) 13786 mp1->b_flag |= MSGMARKNEXT; 13787 else 13788 mp1->b_flag |= MSGNOTMARKNEXT; 13789 freemsg(tcp->tcp_urp_mark_mp); 13790 tcp->tcp_urp_mark_mp = mp1; 13791 flags |= TH_SEND_URP_MARK; 13792 tcp->tcp_urp_last_valid = B_TRUE; 13793 tcp->tcp_urp_last = urp + seg_seq; 13794 } 13795 /* 13796 * If this is a zero window probe, continue to 13797 * process the ACK part. But we need to set seg_len 13798 * to 0 to avoid data processing. Otherwise just 13799 * drop the segment and send back an ACK. 13800 */ 13801 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13802 flags &= ~(TH_SYN | TH_URG); 13803 seg_len = 0; 13804 goto process_ack; 13805 } else { 13806 freemsg(mp); 13807 goto ack_check; 13808 } 13809 } 13810 /* Pitch out of window stuff off the end. */ 13811 rgap = seg_len; 13812 mp2 = mp; 13813 do { 13814 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13815 (uintptr_t)INT_MAX); 13816 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13817 if (rgap < 0) { 13818 mp2->b_wptr += rgap; 13819 if ((mp1 = mp2->b_cont) != NULL) { 13820 mp2->b_cont = NULL; 13821 freemsg(mp1); 13822 } 13823 break; 13824 } 13825 } while ((mp2 = mp2->b_cont) != NULL); 13826 } 13827 ok:; 13828 /* 13829 * TCP should check ECN info for segments inside the window only. 13830 * Therefore the check should be done here. 13831 */ 13832 if (tcp->tcp_ecn_ok) { 13833 if (flags & TH_CWR) { 13834 tcp->tcp_ecn_echo_on = B_FALSE; 13835 } 13836 /* 13837 * Note that both ECN_CE and CWR can be set in the 13838 * same segment. In this case, we once again turn 13839 * on ECN_ECHO. 13840 */ 13841 if (tcp->tcp_ipversion == IPV4_VERSION) { 13842 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13843 13844 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13845 tcp->tcp_ecn_echo_on = B_TRUE; 13846 } 13847 } else { 13848 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13849 13850 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13851 htonl(IPH_ECN_CE << 20)) { 13852 tcp->tcp_ecn_echo_on = B_TRUE; 13853 } 13854 } 13855 } 13856 13857 /* 13858 * Check whether we can update tcp_ts_recent. This test is 13859 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13860 * Extensions for High Performance: An Update", Internet Draft. 13861 */ 13862 if (tcp->tcp_snd_ts_ok && 13863 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13864 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13865 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13866 tcp->tcp_last_rcv_lbolt = lbolt64; 13867 } 13868 13869 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13870 /* 13871 * FIN in an out of order segment. We record this in 13872 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13873 * Clear the FIN so that any check on FIN flag will fail. 13874 * Remember that FIN also counts in the sequence number 13875 * space. So we need to ack out of order FIN only segments. 13876 */ 13877 if (flags & TH_FIN) { 13878 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13879 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13880 flags &= ~TH_FIN; 13881 flags |= TH_ACK_NEEDED; 13882 } 13883 if (seg_len > 0) { 13884 /* Fill in the SACK blk list. */ 13885 if (tcp->tcp_snd_sack_ok) { 13886 ASSERT(tcp->tcp_sack_info != NULL); 13887 tcp_sack_insert(tcp->tcp_sack_list, 13888 seg_seq, seg_seq + seg_len, 13889 &(tcp->tcp_num_sack_blk)); 13890 } 13891 13892 /* 13893 * Attempt reassembly and see if we have something 13894 * ready to go. 13895 */ 13896 mp = tcp_reass(tcp, mp, seg_seq); 13897 /* Always ack out of order packets */ 13898 flags |= TH_ACK_NEEDED | TH_PUSH; 13899 if (mp) { 13900 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13901 (uintptr_t)INT_MAX); 13902 seg_len = mp->b_cont ? msgdsize(mp) : 13903 (int)(mp->b_wptr - mp->b_rptr); 13904 seg_seq = tcp->tcp_rnxt; 13905 /* 13906 * A gap is filled and the seq num and len 13907 * of the gap match that of a previously 13908 * received FIN, put the FIN flag back in. 13909 */ 13910 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13911 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13912 flags |= TH_FIN; 13913 tcp->tcp_valid_bits &= 13914 ~TCP_OFO_FIN_VALID; 13915 } 13916 } else { 13917 /* 13918 * Keep going even with NULL mp. 13919 * There may be a useful ACK or something else 13920 * we don't want to miss. 13921 * 13922 * But TCP should not perform fast retransmit 13923 * because of the ack number. TCP uses 13924 * seg_len == 0 to determine if it is a pure 13925 * ACK. And this is not a pure ACK. 13926 */ 13927 seg_len = 0; 13928 ofo_seg = B_TRUE; 13929 } 13930 } 13931 } else if (seg_len > 0) { 13932 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13933 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13934 /* 13935 * If an out of order FIN was received before, and the seq 13936 * num and len of the new segment match that of the FIN, 13937 * put the FIN flag back in. 13938 */ 13939 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13940 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13941 flags |= TH_FIN; 13942 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13943 } 13944 } 13945 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13946 if (flags & TH_RST) { 13947 freemsg(mp); 13948 switch (tcp->tcp_state) { 13949 case TCPS_SYN_RCVD: 13950 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13951 break; 13952 case TCPS_ESTABLISHED: 13953 case TCPS_FIN_WAIT_1: 13954 case TCPS_FIN_WAIT_2: 13955 case TCPS_CLOSE_WAIT: 13956 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13957 break; 13958 case TCPS_CLOSING: 13959 case TCPS_LAST_ACK: 13960 (void) tcp_clean_death(tcp, 0, 16); 13961 break; 13962 default: 13963 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13964 (void) tcp_clean_death(tcp, ENXIO, 17); 13965 break; 13966 } 13967 return; 13968 } 13969 if (flags & TH_SYN) { 13970 /* 13971 * See RFC 793, Page 71 13972 * 13973 * The seq number must be in the window as it should 13974 * be "fixed" above. If it is outside window, it should 13975 * be already rejected. Note that we allow seg_seq to be 13976 * rnxt + rwnd because we want to accept 0 window probe. 13977 */ 13978 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13979 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13980 freemsg(mp); 13981 /* 13982 * If the ACK flag is not set, just use our snxt as the 13983 * seq number of the RST segment. 13984 */ 13985 if (!(flags & TH_ACK)) { 13986 seg_ack = tcp->tcp_snxt; 13987 } 13988 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13989 TH_RST|TH_ACK); 13990 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13991 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13992 return; 13993 } 13994 /* 13995 * urp could be -1 when the urp field in the packet is 0 13996 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13997 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13998 */ 13999 if (flags & TH_URG && urp >= 0) { 14000 if (!tcp->tcp_urp_last_valid || 14001 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 14002 /* 14003 * If we haven't generated the signal yet for this 14004 * urgent pointer value, do it now. Also, send up a 14005 * zero-length M_DATA indicating whether or not this is 14006 * the mark. The latter is not needed when a 14007 * T_EXDATA_IND is sent up. However, if there are 14008 * allocation failures this code relies on the sender 14009 * retransmitting and the socket code for determining 14010 * the mark should not block waiting for the peer to 14011 * transmit. Thus, for simplicity we always send up the 14012 * mark indication. 14013 */ 14014 mp1 = allocb(0, BPRI_MED); 14015 if (mp1 == NULL) { 14016 freemsg(mp); 14017 return; 14018 } 14019 if (!TCP_IS_DETACHED(tcp) && 14020 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 14021 /* Try again on the rexmit. */ 14022 freemsg(mp1); 14023 freemsg(mp); 14024 return; 14025 } 14026 /* 14027 * Mark with NOTMARKNEXT for now. 14028 * The code below will change this to MARKNEXT 14029 * if we are at the mark. 14030 * 14031 * If there are allocation failures (e.g. in dupmsg 14032 * below) the next time tcp_rput_data sees the urgent 14033 * segment it will send up the MSG*MARKNEXT message. 14034 */ 14035 mp1->b_flag |= MSGNOTMARKNEXT; 14036 freemsg(tcp->tcp_urp_mark_mp); 14037 tcp->tcp_urp_mark_mp = mp1; 14038 flags |= TH_SEND_URP_MARK; 14039 #ifdef DEBUG 14040 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14041 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 14042 "last %x, %s", 14043 seg_seq, urp, tcp->tcp_urp_last, 14044 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14045 #endif /* DEBUG */ 14046 tcp->tcp_urp_last_valid = B_TRUE; 14047 tcp->tcp_urp_last = urp + seg_seq; 14048 } else if (tcp->tcp_urp_mark_mp != NULL) { 14049 /* 14050 * An allocation failure prevented the previous 14051 * tcp_rput_data from sending up the allocated 14052 * MSG*MARKNEXT message - send it up this time 14053 * around. 14054 */ 14055 flags |= TH_SEND_URP_MARK; 14056 } 14057 14058 /* 14059 * If the urgent byte is in this segment, make sure that it is 14060 * all by itself. This makes it much easier to deal with the 14061 * possibility of an allocation failure on the T_exdata_ind. 14062 * Note that seg_len is the number of bytes in the segment, and 14063 * urp is the offset into the segment of the urgent byte. 14064 * urp < seg_len means that the urgent byte is in this segment. 14065 */ 14066 if (urp < seg_len) { 14067 if (seg_len != 1) { 14068 uint32_t tmp_rnxt; 14069 /* 14070 * Break it up and feed it back in. 14071 * Re-attach the IP header. 14072 */ 14073 mp->b_rptr = iphdr; 14074 if (urp > 0) { 14075 /* 14076 * There is stuff before the urgent 14077 * byte. 14078 */ 14079 mp1 = dupmsg(mp); 14080 if (!mp1) { 14081 /* 14082 * Trim from urgent byte on. 14083 * The rest will come back. 14084 */ 14085 (void) adjmsg(mp, 14086 urp - seg_len); 14087 tcp_rput_data(connp, 14088 mp, NULL); 14089 return; 14090 } 14091 (void) adjmsg(mp1, urp - seg_len); 14092 /* Feed this piece back in. */ 14093 tmp_rnxt = tcp->tcp_rnxt; 14094 tcp_rput_data(connp, mp1, NULL); 14095 /* 14096 * If the data passed back in was not 14097 * processed (ie: bad ACK) sending 14098 * the remainder back in will cause a 14099 * loop. In this case, drop the 14100 * packet and let the sender try 14101 * sending a good packet. 14102 */ 14103 if (tmp_rnxt == tcp->tcp_rnxt) { 14104 freemsg(mp); 14105 return; 14106 } 14107 } 14108 if (urp != seg_len - 1) { 14109 uint32_t tmp_rnxt; 14110 /* 14111 * There is stuff after the urgent 14112 * byte. 14113 */ 14114 mp1 = dupmsg(mp); 14115 if (!mp1) { 14116 /* 14117 * Trim everything beyond the 14118 * urgent byte. The rest will 14119 * come back. 14120 */ 14121 (void) adjmsg(mp, 14122 urp + 1 - seg_len); 14123 tcp_rput_data(connp, 14124 mp, NULL); 14125 return; 14126 } 14127 (void) adjmsg(mp1, urp + 1 - seg_len); 14128 tmp_rnxt = tcp->tcp_rnxt; 14129 tcp_rput_data(connp, mp1, NULL); 14130 /* 14131 * If the data passed back in was not 14132 * processed (ie: bad ACK) sending 14133 * the remainder back in will cause a 14134 * loop. In this case, drop the 14135 * packet and let the sender try 14136 * sending a good packet. 14137 */ 14138 if (tmp_rnxt == tcp->tcp_rnxt) { 14139 freemsg(mp); 14140 return; 14141 } 14142 } 14143 tcp_rput_data(connp, mp, NULL); 14144 return; 14145 } 14146 /* 14147 * This segment contains only the urgent byte. We 14148 * have to allocate the T_exdata_ind, if we can. 14149 */ 14150 if (!tcp->tcp_urp_mp) { 14151 struct T_exdata_ind *tei; 14152 mp1 = allocb(sizeof (struct T_exdata_ind), 14153 BPRI_MED); 14154 if (!mp1) { 14155 /* 14156 * Sigh... It'll be back. 14157 * Generate any MSG*MARK message now. 14158 */ 14159 freemsg(mp); 14160 seg_len = 0; 14161 if (flags & TH_SEND_URP_MARK) { 14162 14163 14164 ASSERT(tcp->tcp_urp_mark_mp); 14165 tcp->tcp_urp_mark_mp->b_flag &= 14166 ~MSGNOTMARKNEXT; 14167 tcp->tcp_urp_mark_mp->b_flag |= 14168 MSGMARKNEXT; 14169 } 14170 goto ack_check; 14171 } 14172 mp1->b_datap->db_type = M_PROTO; 14173 tei = (struct T_exdata_ind *)mp1->b_rptr; 14174 tei->PRIM_type = T_EXDATA_IND; 14175 tei->MORE_flag = 0; 14176 mp1->b_wptr = (uchar_t *)&tei[1]; 14177 tcp->tcp_urp_mp = mp1; 14178 #ifdef DEBUG 14179 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14180 "tcp_rput: allocated exdata_ind %s", 14181 tcp_display(tcp, NULL, 14182 DISP_PORT_ONLY)); 14183 #endif /* DEBUG */ 14184 /* 14185 * There is no need to send a separate MSG*MARK 14186 * message since the T_EXDATA_IND will be sent 14187 * now. 14188 */ 14189 flags &= ~TH_SEND_URP_MARK; 14190 freemsg(tcp->tcp_urp_mark_mp); 14191 tcp->tcp_urp_mark_mp = NULL; 14192 } 14193 /* 14194 * Now we are all set. On the next putnext upstream, 14195 * tcp_urp_mp will be non-NULL and will get prepended 14196 * to what has to be this piece containing the urgent 14197 * byte. If for any reason we abort this segment below, 14198 * if it comes back, we will have this ready, or it 14199 * will get blown off in close. 14200 */ 14201 } else if (urp == seg_len) { 14202 /* 14203 * The urgent byte is the next byte after this sequence 14204 * number. If there is data it is marked with 14205 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 14206 * since it is not needed. Otherwise, if the code 14207 * above just allocated a zero-length tcp_urp_mark_mp 14208 * message, that message is tagged with MSGMARKNEXT. 14209 * Sending up these MSGMARKNEXT messages makes 14210 * SIOCATMARK work correctly even though 14211 * the T_EXDATA_IND will not be sent up until the 14212 * urgent byte arrives. 14213 */ 14214 if (seg_len != 0) { 14215 flags |= TH_MARKNEXT_NEEDED; 14216 freemsg(tcp->tcp_urp_mark_mp); 14217 tcp->tcp_urp_mark_mp = NULL; 14218 flags &= ~TH_SEND_URP_MARK; 14219 } else if (tcp->tcp_urp_mark_mp != NULL) { 14220 flags |= TH_SEND_URP_MARK; 14221 tcp->tcp_urp_mark_mp->b_flag &= 14222 ~MSGNOTMARKNEXT; 14223 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 14224 } 14225 #ifdef DEBUG 14226 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14227 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 14228 seg_len, flags, 14229 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14230 #endif /* DEBUG */ 14231 } else { 14232 /* Data left until we hit mark */ 14233 #ifdef DEBUG 14234 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14235 "tcp_rput: URP %d bytes left, %s", 14236 urp - seg_len, tcp_display(tcp, NULL, 14237 DISP_PORT_ONLY)); 14238 #endif /* DEBUG */ 14239 } 14240 } 14241 14242 process_ack: 14243 if (!(flags & TH_ACK)) { 14244 freemsg(mp); 14245 goto xmit_check; 14246 } 14247 } 14248 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 14249 14250 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 14251 tcp->tcp_ip_forward_progress = B_TRUE; 14252 if (tcp->tcp_state == TCPS_SYN_RCVD) { 14253 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 14254 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 14255 /* 3-way handshake complete - pass up the T_CONN_IND */ 14256 tcp_t *listener = tcp->tcp_listener; 14257 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 14258 14259 tcp->tcp_tconnind_started = B_TRUE; 14260 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 14261 /* 14262 * We are here means eager is fine but it can 14263 * get a TH_RST at any point between now and till 14264 * accept completes and disappear. We need to 14265 * ensure that reference to eager is valid after 14266 * we get out of eager's perimeter. So we do 14267 * an extra refhold. 14268 */ 14269 CONN_INC_REF(connp); 14270 14271 /* 14272 * The listener also exists because of the refhold 14273 * done in tcp_conn_request. Its possible that it 14274 * might have closed. We will check that once we 14275 * get inside listeners context. 14276 */ 14277 CONN_INC_REF(listener->tcp_connp); 14278 if (listener->tcp_connp->conn_sqp == 14279 connp->conn_sqp) { 14280 tcp_send_conn_ind(listener->tcp_connp, mp, 14281 listener->tcp_connp->conn_sqp); 14282 CONN_DEC_REF(listener->tcp_connp); 14283 } else if (!tcp->tcp_loopback) { 14284 squeue_fill(listener->tcp_connp->conn_sqp, mp, 14285 tcp_send_conn_ind, 14286 listener->tcp_connp, SQTAG_TCP_CONN_IND); 14287 } else { 14288 squeue_enter(listener->tcp_connp->conn_sqp, mp, 14289 tcp_send_conn_ind, listener->tcp_connp, 14290 SQTAG_TCP_CONN_IND); 14291 } 14292 } 14293 14294 if (tcp->tcp_active_open) { 14295 /* 14296 * We are seeing the final ack in the three way 14297 * hand shake of a active open'ed connection 14298 * so we must send up a T_CONN_CON 14299 */ 14300 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14301 freemsg(mp); 14302 return; 14303 } 14304 /* 14305 * Don't fuse the loopback endpoints for 14306 * simultaneous active opens. 14307 */ 14308 if (tcp->tcp_loopback) { 14309 TCP_STAT(tcps, tcp_fusion_unfusable); 14310 tcp->tcp_unfusable = B_TRUE; 14311 } 14312 } 14313 14314 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14315 bytes_acked--; 14316 /* SYN was acked - making progress */ 14317 if (tcp->tcp_ipversion == IPV6_VERSION) 14318 tcp->tcp_ip_forward_progress = B_TRUE; 14319 14320 /* 14321 * If SYN was retransmitted, need to reset all 14322 * retransmission info as this segment will be 14323 * treated as a dup ACK. 14324 */ 14325 if (tcp->tcp_rexmit) { 14326 tcp->tcp_rexmit = B_FALSE; 14327 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14328 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14329 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14330 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14331 tcp->tcp_ms_we_have_waited = 0; 14332 tcp->tcp_cwnd = mss; 14333 } 14334 14335 /* 14336 * We set the send window to zero here. 14337 * This is needed if there is data to be 14338 * processed already on the queue. 14339 * Later (at swnd_update label), the 14340 * "new_swnd > tcp_swnd" condition is satisfied 14341 * the XMIT_NEEDED flag is set in the current 14342 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14343 * called if there is already data on queue in 14344 * this state. 14345 */ 14346 tcp->tcp_swnd = 0; 14347 14348 if (new_swnd > tcp->tcp_max_swnd) 14349 tcp->tcp_max_swnd = new_swnd; 14350 tcp->tcp_swl1 = seg_seq; 14351 tcp->tcp_swl2 = seg_ack; 14352 tcp->tcp_state = TCPS_ESTABLISHED; 14353 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14354 14355 /* Fuse when both sides are in ESTABLISHED state */ 14356 if (tcp->tcp_loopback && do_tcp_fusion) 14357 tcp_fuse(tcp, iphdr, tcph); 14358 14359 } 14360 /* This code follows 4.4BSD-Lite2 mostly. */ 14361 if (bytes_acked < 0) 14362 goto est; 14363 14364 /* 14365 * If TCP is ECN capable and the congestion experience bit is 14366 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14367 * done once per window (or more loosely, per RTT). 14368 */ 14369 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14370 tcp->tcp_cwr = B_FALSE; 14371 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14372 if (!tcp->tcp_cwr) { 14373 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14374 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14375 tcp->tcp_cwnd = npkt * mss; 14376 /* 14377 * If the cwnd is 0, use the timer to clock out 14378 * new segments. This is required by the ECN spec. 14379 */ 14380 if (npkt == 0) { 14381 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14382 /* 14383 * This makes sure that when the ACK comes 14384 * back, we will increase tcp_cwnd by 1 MSS. 14385 */ 14386 tcp->tcp_cwnd_cnt = 0; 14387 } 14388 tcp->tcp_cwr = B_TRUE; 14389 /* 14390 * This marks the end of the current window of in 14391 * flight data. That is why we don't use 14392 * tcp_suna + tcp_swnd. Only data in flight can 14393 * provide ECN info. 14394 */ 14395 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14396 tcp->tcp_ecn_cwr_sent = B_FALSE; 14397 } 14398 } 14399 14400 mp1 = tcp->tcp_xmit_head; 14401 if (bytes_acked == 0) { 14402 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14403 int dupack_cnt; 14404 14405 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14406 /* 14407 * Fast retransmit. When we have seen exactly three 14408 * identical ACKs while we have unacked data 14409 * outstanding we take it as a hint that our peer 14410 * dropped something. 14411 * 14412 * If TCP is retransmitting, don't do fast retransmit. 14413 */ 14414 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14415 ! tcp->tcp_rexmit) { 14416 /* Do Limited Transmit */ 14417 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14418 tcps->tcps_dupack_fast_retransmit) { 14419 /* 14420 * RFC 3042 14421 * 14422 * What we need to do is temporarily 14423 * increase tcp_cwnd so that new 14424 * data can be sent if it is allowed 14425 * by the receive window (tcp_rwnd). 14426 * tcp_wput_data() will take care of 14427 * the rest. 14428 * 14429 * If the connection is SACK capable, 14430 * only do limited xmit when there 14431 * is SACK info. 14432 * 14433 * Note how tcp_cwnd is incremented. 14434 * The first dup ACK will increase 14435 * it by 1 MSS. The second dup ACK 14436 * will increase it by 2 MSS. This 14437 * means that only 1 new segment will 14438 * be sent for each dup ACK. 14439 */ 14440 if (tcp->tcp_unsent > 0 && 14441 (!tcp->tcp_snd_sack_ok || 14442 (tcp->tcp_snd_sack_ok && 14443 tcp->tcp_notsack_list != NULL))) { 14444 tcp->tcp_cwnd += mss << 14445 (tcp->tcp_dupack_cnt - 1); 14446 flags |= TH_LIMIT_XMIT; 14447 } 14448 } else if (dupack_cnt == 14449 tcps->tcps_dupack_fast_retransmit) { 14450 14451 /* 14452 * If we have reduced tcp_ssthresh 14453 * because of ECN, do not reduce it again 14454 * unless it is already one window of data 14455 * away. After one window of data, tcp_cwr 14456 * should then be cleared. Note that 14457 * for non ECN capable connection, tcp_cwr 14458 * should always be false. 14459 * 14460 * Adjust cwnd since the duplicate 14461 * ack indicates that a packet was 14462 * dropped (due to congestion.) 14463 */ 14464 if (!tcp->tcp_cwr) { 14465 npkt = ((tcp->tcp_snxt - 14466 tcp->tcp_suna) >> 1) / mss; 14467 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14468 mss; 14469 tcp->tcp_cwnd = (npkt + 14470 tcp->tcp_dupack_cnt) * mss; 14471 } 14472 if (tcp->tcp_ecn_ok) { 14473 tcp->tcp_cwr = B_TRUE; 14474 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14475 tcp->tcp_ecn_cwr_sent = B_FALSE; 14476 } 14477 14478 /* 14479 * We do Hoe's algorithm. Refer to her 14480 * paper "Improving the Start-up Behavior 14481 * of a Congestion Control Scheme for TCP," 14482 * appeared in SIGCOMM'96. 14483 * 14484 * Save highest seq no we have sent so far. 14485 * Be careful about the invisible FIN byte. 14486 */ 14487 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14488 (tcp->tcp_unsent == 0)) { 14489 tcp->tcp_rexmit_max = tcp->tcp_fss; 14490 } else { 14491 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14492 } 14493 14494 /* 14495 * Do not allow bursty traffic during. 14496 * fast recovery. Refer to Fall and Floyd's 14497 * paper "Simulation-based Comparisons of 14498 * Tahoe, Reno and SACK TCP" (in CCR?) 14499 * This is a best current practise. 14500 */ 14501 tcp->tcp_snd_burst = TCP_CWND_SS; 14502 14503 /* 14504 * For SACK: 14505 * Calculate tcp_pipe, which is the 14506 * estimated number of bytes in 14507 * network. 14508 * 14509 * tcp_fack is the highest sack'ed seq num 14510 * TCP has received. 14511 * 14512 * tcp_pipe is explained in the above quoted 14513 * Fall and Floyd's paper. tcp_fack is 14514 * explained in Mathis and Mahdavi's 14515 * "Forward Acknowledgment: Refining TCP 14516 * Congestion Control" in SIGCOMM '96. 14517 */ 14518 if (tcp->tcp_snd_sack_ok) { 14519 ASSERT(tcp->tcp_sack_info != NULL); 14520 if (tcp->tcp_notsack_list != NULL) { 14521 tcp->tcp_pipe = tcp->tcp_snxt - 14522 tcp->tcp_fack; 14523 tcp->tcp_sack_snxt = seg_ack; 14524 flags |= TH_NEED_SACK_REXMIT; 14525 } else { 14526 /* 14527 * Always initialize tcp_pipe 14528 * even though we don't have 14529 * any SACK info. If later 14530 * we get SACK info and 14531 * tcp_pipe is not initialized, 14532 * funny things will happen. 14533 */ 14534 tcp->tcp_pipe = 14535 tcp->tcp_cwnd_ssthresh; 14536 } 14537 } else { 14538 flags |= TH_REXMIT_NEEDED; 14539 } /* tcp_snd_sack_ok */ 14540 14541 } else { 14542 /* 14543 * Here we perform congestion 14544 * avoidance, but NOT slow start. 14545 * This is known as the Fast 14546 * Recovery Algorithm. 14547 */ 14548 if (tcp->tcp_snd_sack_ok && 14549 tcp->tcp_notsack_list != NULL) { 14550 flags |= TH_NEED_SACK_REXMIT; 14551 tcp->tcp_pipe -= mss; 14552 if (tcp->tcp_pipe < 0) 14553 tcp->tcp_pipe = 0; 14554 } else { 14555 /* 14556 * We know that one more packet has 14557 * left the pipe thus we can update 14558 * cwnd. 14559 */ 14560 cwnd = tcp->tcp_cwnd + mss; 14561 if (cwnd > tcp->tcp_cwnd_max) 14562 cwnd = tcp->tcp_cwnd_max; 14563 tcp->tcp_cwnd = cwnd; 14564 if (tcp->tcp_unsent > 0) 14565 flags |= TH_XMIT_NEEDED; 14566 } 14567 } 14568 } 14569 } else if (tcp->tcp_zero_win_probe) { 14570 /* 14571 * If the window has opened, need to arrange 14572 * to send additional data. 14573 */ 14574 if (new_swnd != 0) { 14575 /* tcp_suna != tcp_snxt */ 14576 /* Packet contains a window update */ 14577 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14578 tcp->tcp_zero_win_probe = 0; 14579 tcp->tcp_timer_backoff = 0; 14580 tcp->tcp_ms_we_have_waited = 0; 14581 14582 /* 14583 * Transmit starting with tcp_suna since 14584 * the one byte probe is not ack'ed. 14585 * If TCP has sent more than one identical 14586 * probe, tcp_rexmit will be set. That means 14587 * tcp_ss_rexmit() will send out the one 14588 * byte along with new data. Otherwise, 14589 * fake the retransmission. 14590 */ 14591 flags |= TH_XMIT_NEEDED; 14592 if (!tcp->tcp_rexmit) { 14593 tcp->tcp_rexmit = B_TRUE; 14594 tcp->tcp_dupack_cnt = 0; 14595 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14596 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14597 } 14598 } 14599 } 14600 goto swnd_update; 14601 } 14602 14603 /* 14604 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14605 * If the ACK value acks something that we have not yet sent, it might 14606 * be an old duplicate segment. Send an ACK to re-synchronize the 14607 * other side. 14608 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14609 * state is handled above, so we can always just drop the segment and 14610 * send an ACK here. 14611 * 14612 * Should we send ACKs in response to ACK only segments? 14613 */ 14614 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14615 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14616 /* drop the received segment */ 14617 freemsg(mp); 14618 14619 /* 14620 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14621 * greater than 0, check if the number of such 14622 * bogus ACks is greater than that count. If yes, 14623 * don't send back any ACK. This prevents TCP from 14624 * getting into an ACK storm if somehow an attacker 14625 * successfully spoofs an acceptable segment to our 14626 * peer. 14627 */ 14628 if (tcp_drop_ack_unsent_cnt > 0 && 14629 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14630 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14631 return; 14632 } 14633 mp = tcp_ack_mp(tcp); 14634 if (mp != NULL) { 14635 BUMP_LOCAL(tcp->tcp_obsegs); 14636 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14637 tcp_send_data(tcp, tcp->tcp_wq, mp); 14638 } 14639 return; 14640 } 14641 14642 /* 14643 * TCP gets a new ACK, update the notsack'ed list to delete those 14644 * blocks that are covered by this ACK. 14645 */ 14646 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14647 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14648 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14649 } 14650 14651 /* 14652 * If we got an ACK after fast retransmit, check to see 14653 * if it is a partial ACK. If it is not and the congestion 14654 * window was inflated to account for the other side's 14655 * cached packets, retract it. If it is, do Hoe's algorithm. 14656 */ 14657 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14658 ASSERT(tcp->tcp_rexmit == B_FALSE); 14659 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14660 tcp->tcp_dupack_cnt = 0; 14661 /* 14662 * Restore the orig tcp_cwnd_ssthresh after 14663 * fast retransmit phase. 14664 */ 14665 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14666 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14667 } 14668 tcp->tcp_rexmit_max = seg_ack; 14669 tcp->tcp_cwnd_cnt = 0; 14670 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14671 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14672 14673 /* 14674 * Remove all notsack info to avoid confusion with 14675 * the next fast retrasnmit/recovery phase. 14676 */ 14677 if (tcp->tcp_snd_sack_ok && 14678 tcp->tcp_notsack_list != NULL) { 14679 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14680 } 14681 } else { 14682 if (tcp->tcp_snd_sack_ok && 14683 tcp->tcp_notsack_list != NULL) { 14684 flags |= TH_NEED_SACK_REXMIT; 14685 tcp->tcp_pipe -= mss; 14686 if (tcp->tcp_pipe < 0) 14687 tcp->tcp_pipe = 0; 14688 } else { 14689 /* 14690 * Hoe's algorithm: 14691 * 14692 * Retransmit the unack'ed segment and 14693 * restart fast recovery. Note that we 14694 * need to scale back tcp_cwnd to the 14695 * original value when we started fast 14696 * recovery. This is to prevent overly 14697 * aggressive behaviour in sending new 14698 * segments. 14699 */ 14700 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14701 tcps->tcps_dupack_fast_retransmit * mss; 14702 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14703 flags |= TH_REXMIT_NEEDED; 14704 } 14705 } 14706 } else { 14707 tcp->tcp_dupack_cnt = 0; 14708 if (tcp->tcp_rexmit) { 14709 /* 14710 * TCP is retranmitting. If the ACK ack's all 14711 * outstanding data, update tcp_rexmit_max and 14712 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14713 * to the correct value. 14714 * 14715 * Note that SEQ_LEQ() is used. This is to avoid 14716 * unnecessary fast retransmit caused by dup ACKs 14717 * received when TCP does slow start retransmission 14718 * after a time out. During this phase, TCP may 14719 * send out segments which are already received. 14720 * This causes dup ACKs to be sent back. 14721 */ 14722 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14723 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14724 tcp->tcp_rexmit_nxt = seg_ack; 14725 } 14726 if (seg_ack != tcp->tcp_rexmit_max) { 14727 flags |= TH_XMIT_NEEDED; 14728 } 14729 } else { 14730 tcp->tcp_rexmit = B_FALSE; 14731 tcp->tcp_xmit_zc_clean = B_FALSE; 14732 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14733 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14734 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14735 } 14736 tcp->tcp_ms_we_have_waited = 0; 14737 } 14738 } 14739 14740 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14741 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14742 tcp->tcp_suna = seg_ack; 14743 if (tcp->tcp_zero_win_probe != 0) { 14744 tcp->tcp_zero_win_probe = 0; 14745 tcp->tcp_timer_backoff = 0; 14746 } 14747 14748 /* 14749 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14750 * Note that it cannot be the SYN being ack'ed. The code flow 14751 * will not reach here. 14752 */ 14753 if (mp1 == NULL) { 14754 goto fin_acked; 14755 } 14756 14757 /* 14758 * Update the congestion window. 14759 * 14760 * If TCP is not ECN capable or TCP is ECN capable but the 14761 * congestion experience bit is not set, increase the tcp_cwnd as 14762 * usual. 14763 */ 14764 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14765 cwnd = tcp->tcp_cwnd; 14766 add = mss; 14767 14768 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14769 /* 14770 * This is to prevent an increase of less than 1 MSS of 14771 * tcp_cwnd. With partial increase, tcp_wput_data() 14772 * may send out tinygrams in order to preserve mblk 14773 * boundaries. 14774 * 14775 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14776 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14777 * increased by 1 MSS for every RTTs. 14778 */ 14779 if (tcp->tcp_cwnd_cnt <= 0) { 14780 tcp->tcp_cwnd_cnt = cwnd + add; 14781 } else { 14782 tcp->tcp_cwnd_cnt -= add; 14783 add = 0; 14784 } 14785 } 14786 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14787 } 14788 14789 /* See if the latest urgent data has been acknowledged */ 14790 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14791 SEQ_GT(seg_ack, tcp->tcp_urg)) 14792 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14793 14794 /* Can we update the RTT estimates? */ 14795 if (tcp->tcp_snd_ts_ok) { 14796 /* Ignore zero timestamp echo-reply. */ 14797 if (tcpopt.tcp_opt_ts_ecr != 0) { 14798 tcp_set_rto(tcp, (int32_t)lbolt - 14799 (int32_t)tcpopt.tcp_opt_ts_ecr); 14800 } 14801 14802 /* If needed, restart the timer. */ 14803 if (tcp->tcp_set_timer == 1) { 14804 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14805 tcp->tcp_set_timer = 0; 14806 } 14807 /* 14808 * Update tcp_csuna in case the other side stops sending 14809 * us timestamps. 14810 */ 14811 tcp->tcp_csuna = tcp->tcp_snxt; 14812 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14813 /* 14814 * An ACK sequence we haven't seen before, so get the RTT 14815 * and update the RTO. But first check if the timestamp is 14816 * valid to use. 14817 */ 14818 if ((mp1->b_next != NULL) && 14819 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14820 tcp_set_rto(tcp, (int32_t)lbolt - 14821 (int32_t)(intptr_t)mp1->b_prev); 14822 else 14823 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14824 14825 /* Remeber the last sequence to be ACKed */ 14826 tcp->tcp_csuna = seg_ack; 14827 if (tcp->tcp_set_timer == 1) { 14828 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14829 tcp->tcp_set_timer = 0; 14830 } 14831 } else { 14832 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14833 } 14834 14835 /* Eat acknowledged bytes off the xmit queue. */ 14836 for (;;) { 14837 mblk_t *mp2; 14838 uchar_t *wptr; 14839 14840 wptr = mp1->b_wptr; 14841 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14842 bytes_acked -= (int)(wptr - mp1->b_rptr); 14843 if (bytes_acked < 0) { 14844 mp1->b_rptr = wptr + bytes_acked; 14845 /* 14846 * Set a new timestamp if all the bytes timed by the 14847 * old timestamp have been ack'ed. 14848 */ 14849 if (SEQ_GT(seg_ack, 14850 (uint32_t)(uintptr_t)(mp1->b_next))) { 14851 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14852 mp1->b_next = NULL; 14853 } 14854 break; 14855 } 14856 mp1->b_next = NULL; 14857 mp1->b_prev = NULL; 14858 mp2 = mp1; 14859 mp1 = mp1->b_cont; 14860 14861 /* 14862 * This notification is required for some zero-copy 14863 * clients to maintain a copy semantic. After the data 14864 * is ack'ed, client is safe to modify or reuse the buffer. 14865 */ 14866 if (tcp->tcp_snd_zcopy_aware && 14867 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14868 tcp_zcopy_notify(tcp); 14869 freeb(mp2); 14870 if (bytes_acked == 0) { 14871 if (mp1 == NULL) { 14872 /* Everything is ack'ed, clear the tail. */ 14873 tcp->tcp_xmit_tail = NULL; 14874 /* 14875 * Cancel the timer unless we are still 14876 * waiting for an ACK for the FIN packet. 14877 */ 14878 if (tcp->tcp_timer_tid != 0 && 14879 tcp->tcp_snxt == tcp->tcp_suna) { 14880 (void) TCP_TIMER_CANCEL(tcp, 14881 tcp->tcp_timer_tid); 14882 tcp->tcp_timer_tid = 0; 14883 } 14884 goto pre_swnd_update; 14885 } 14886 if (mp2 != tcp->tcp_xmit_tail) 14887 break; 14888 tcp->tcp_xmit_tail = mp1; 14889 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14890 (uintptr_t)INT_MAX); 14891 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14892 mp1->b_rptr); 14893 break; 14894 } 14895 if (mp1 == NULL) { 14896 /* 14897 * More was acked but there is nothing more 14898 * outstanding. This means that the FIN was 14899 * just acked or that we're talking to a clown. 14900 */ 14901 fin_acked: 14902 ASSERT(tcp->tcp_fin_sent); 14903 tcp->tcp_xmit_tail = NULL; 14904 if (tcp->tcp_fin_sent) { 14905 /* FIN was acked - making progress */ 14906 if (tcp->tcp_ipversion == IPV6_VERSION && 14907 !tcp->tcp_fin_acked) 14908 tcp->tcp_ip_forward_progress = B_TRUE; 14909 tcp->tcp_fin_acked = B_TRUE; 14910 if (tcp->tcp_linger_tid != 0 && 14911 TCP_TIMER_CANCEL(tcp, 14912 tcp->tcp_linger_tid) >= 0) { 14913 tcp_stop_lingering(tcp); 14914 freemsg(mp); 14915 mp = NULL; 14916 } 14917 } else { 14918 /* 14919 * We should never get here because 14920 * we have already checked that the 14921 * number of bytes ack'ed should be 14922 * smaller than or equal to what we 14923 * have sent so far (it is the 14924 * acceptability check of the ACK). 14925 * We can only get here if the send 14926 * queue is corrupted. 14927 * 14928 * Terminate the connection and 14929 * panic the system. It is better 14930 * for us to panic instead of 14931 * continuing to avoid other disaster. 14932 */ 14933 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14934 tcp->tcp_rnxt, TH_RST|TH_ACK); 14935 panic("Memory corruption " 14936 "detected for connection %s.", 14937 tcp_display(tcp, NULL, 14938 DISP_ADDR_AND_PORT)); 14939 /*NOTREACHED*/ 14940 } 14941 goto pre_swnd_update; 14942 } 14943 ASSERT(mp2 != tcp->tcp_xmit_tail); 14944 } 14945 if (tcp->tcp_unsent) { 14946 flags |= TH_XMIT_NEEDED; 14947 } 14948 pre_swnd_update: 14949 tcp->tcp_xmit_head = mp1; 14950 swnd_update: 14951 /* 14952 * The following check is different from most other implementations. 14953 * For bi-directional transfer, when segments are dropped, the 14954 * "normal" check will not accept a window update in those 14955 * retransmitted segemnts. Failing to do that, TCP may send out 14956 * segments which are outside receiver's window. As TCP accepts 14957 * the ack in those retransmitted segments, if the window update in 14958 * the same segment is not accepted, TCP will incorrectly calculates 14959 * that it can send more segments. This can create a deadlock 14960 * with the receiver if its window becomes zero. 14961 */ 14962 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14963 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14964 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14965 /* 14966 * The criteria for update is: 14967 * 14968 * 1. the segment acknowledges some data. Or 14969 * 2. the segment is new, i.e. it has a higher seq num. Or 14970 * 3. the segment is not old and the advertised window is 14971 * larger than the previous advertised window. 14972 */ 14973 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14974 flags |= TH_XMIT_NEEDED; 14975 tcp->tcp_swnd = new_swnd; 14976 if (new_swnd > tcp->tcp_max_swnd) 14977 tcp->tcp_max_swnd = new_swnd; 14978 tcp->tcp_swl1 = seg_seq; 14979 tcp->tcp_swl2 = seg_ack; 14980 } 14981 est: 14982 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14983 14984 switch (tcp->tcp_state) { 14985 case TCPS_FIN_WAIT_1: 14986 if (tcp->tcp_fin_acked) { 14987 tcp->tcp_state = TCPS_FIN_WAIT_2; 14988 /* 14989 * We implement the non-standard BSD/SunOS 14990 * FIN_WAIT_2 flushing algorithm. 14991 * If there is no user attached to this 14992 * TCP endpoint, then this TCP struct 14993 * could hang around forever in FIN_WAIT_2 14994 * state if the peer forgets to send us 14995 * a FIN. To prevent this, we wait only 14996 * 2*MSL (a convenient time value) for 14997 * the FIN to arrive. If it doesn't show up, 14998 * we flush the TCP endpoint. This algorithm, 14999 * though a violation of RFC-793, has worked 15000 * for over 10 years in BSD systems. 15001 * Note: SunOS 4.x waits 675 seconds before 15002 * flushing the FIN_WAIT_2 connection. 15003 */ 15004 TCP_TIMER_RESTART(tcp, 15005 tcps->tcps_fin_wait_2_flush_interval); 15006 } 15007 break; 15008 case TCPS_FIN_WAIT_2: 15009 break; /* Shutdown hook? */ 15010 case TCPS_LAST_ACK: 15011 freemsg(mp); 15012 if (tcp->tcp_fin_acked) { 15013 (void) tcp_clean_death(tcp, 0, 19); 15014 return; 15015 } 15016 goto xmit_check; 15017 case TCPS_CLOSING: 15018 if (tcp->tcp_fin_acked) { 15019 tcp->tcp_state = TCPS_TIME_WAIT; 15020 /* 15021 * Unconditionally clear the exclusive binding 15022 * bit so this TIME-WAIT connection won't 15023 * interfere with new ones. 15024 */ 15025 tcp->tcp_exclbind = 0; 15026 if (!TCP_IS_DETACHED(tcp)) { 15027 TCP_TIMER_RESTART(tcp, 15028 tcps->tcps_time_wait_interval); 15029 } else { 15030 tcp_time_wait_append(tcp); 15031 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 15032 } 15033 } 15034 /*FALLTHRU*/ 15035 case TCPS_CLOSE_WAIT: 15036 freemsg(mp); 15037 goto xmit_check; 15038 default: 15039 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 15040 break; 15041 } 15042 } 15043 if (flags & TH_FIN) { 15044 /* Make sure we ack the fin */ 15045 flags |= TH_ACK_NEEDED; 15046 if (!tcp->tcp_fin_rcvd) { 15047 tcp->tcp_fin_rcvd = B_TRUE; 15048 tcp->tcp_rnxt++; 15049 tcph = tcp->tcp_tcph; 15050 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15051 15052 /* 15053 * Generate the ordrel_ind at the end unless we 15054 * are an eager guy. 15055 * In the eager case tcp_rsrv will do this when run 15056 * after tcp_accept is done. 15057 */ 15058 if (tcp->tcp_listener == NULL && 15059 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 15060 flags |= TH_ORDREL_NEEDED; 15061 switch (tcp->tcp_state) { 15062 case TCPS_SYN_RCVD: 15063 case TCPS_ESTABLISHED: 15064 tcp->tcp_state = TCPS_CLOSE_WAIT; 15065 /* Keepalive? */ 15066 break; 15067 case TCPS_FIN_WAIT_1: 15068 if (!tcp->tcp_fin_acked) { 15069 tcp->tcp_state = TCPS_CLOSING; 15070 break; 15071 } 15072 /* FALLTHRU */ 15073 case TCPS_FIN_WAIT_2: 15074 tcp->tcp_state = TCPS_TIME_WAIT; 15075 /* 15076 * Unconditionally clear the exclusive binding 15077 * bit so this TIME-WAIT connection won't 15078 * interfere with new ones. 15079 */ 15080 tcp->tcp_exclbind = 0; 15081 if (!TCP_IS_DETACHED(tcp)) { 15082 TCP_TIMER_RESTART(tcp, 15083 tcps->tcps_time_wait_interval); 15084 } else { 15085 tcp_time_wait_append(tcp); 15086 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 15087 } 15088 if (seg_len) { 15089 /* 15090 * implies data piggybacked on FIN. 15091 * break to handle data. 15092 */ 15093 break; 15094 } 15095 freemsg(mp); 15096 goto ack_check; 15097 } 15098 } 15099 } 15100 if (mp == NULL) 15101 goto xmit_check; 15102 if (seg_len == 0) { 15103 freemsg(mp); 15104 goto xmit_check; 15105 } 15106 if (mp->b_rptr == mp->b_wptr) { 15107 /* 15108 * The header has been consumed, so we remove the 15109 * zero-length mblk here. 15110 */ 15111 mp1 = mp; 15112 mp = mp->b_cont; 15113 freeb(mp1); 15114 } 15115 tcph = tcp->tcp_tcph; 15116 tcp->tcp_rack_cnt++; 15117 { 15118 uint32_t cur_max; 15119 15120 cur_max = tcp->tcp_rack_cur_max; 15121 if (tcp->tcp_rack_cnt >= cur_max) { 15122 /* 15123 * We have more unacked data than we should - send 15124 * an ACK now. 15125 */ 15126 flags |= TH_ACK_NEEDED; 15127 cur_max++; 15128 if (cur_max > tcp->tcp_rack_abs_max) 15129 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15130 else 15131 tcp->tcp_rack_cur_max = cur_max; 15132 } else if (TCP_IS_DETACHED(tcp)) { 15133 /* We don't have an ACK timer for detached TCP. */ 15134 flags |= TH_ACK_NEEDED; 15135 } else if (seg_len < mss) { 15136 /* 15137 * If we get a segment that is less than an mss, and we 15138 * already have unacknowledged data, and the amount 15139 * unacknowledged is not a multiple of mss, then we 15140 * better generate an ACK now. Otherwise, this may be 15141 * the tail piece of a transaction, and we would rather 15142 * wait for the response. 15143 */ 15144 uint32_t udif; 15145 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 15146 (uintptr_t)INT_MAX); 15147 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 15148 if (udif && (udif % mss)) 15149 flags |= TH_ACK_NEEDED; 15150 else 15151 flags |= TH_ACK_TIMER_NEEDED; 15152 } else { 15153 /* Start delayed ack timer */ 15154 flags |= TH_ACK_TIMER_NEEDED; 15155 } 15156 } 15157 tcp->tcp_rnxt += seg_len; 15158 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15159 15160 /* Update SACK list */ 15161 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 15162 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 15163 &(tcp->tcp_num_sack_blk)); 15164 } 15165 15166 if (tcp->tcp_urp_mp) { 15167 tcp->tcp_urp_mp->b_cont = mp; 15168 mp = tcp->tcp_urp_mp; 15169 tcp->tcp_urp_mp = NULL; 15170 /* Ready for a new signal. */ 15171 tcp->tcp_urp_last_valid = B_FALSE; 15172 #ifdef DEBUG 15173 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15174 "tcp_rput: sending exdata_ind %s", 15175 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15176 #endif /* DEBUG */ 15177 } 15178 15179 /* 15180 * Check for ancillary data changes compared to last segment. 15181 */ 15182 if (tcp->tcp_ipv6_recvancillary != 0) { 15183 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 15184 ASSERT(mp != NULL); 15185 } 15186 15187 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 15188 /* 15189 * Side queue inbound data until the accept happens. 15190 * tcp_accept/tcp_rput drains this when the accept happens. 15191 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 15192 * T_EXDATA_IND) it is queued on b_next. 15193 * XXX Make urgent data use this. Requires: 15194 * Removing tcp_listener check for TH_URG 15195 * Making M_PCPROTO and MARK messages skip the eager case 15196 */ 15197 15198 if (tcp->tcp_kssl_pending) { 15199 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 15200 mblk_t *, mp); 15201 tcp_kssl_input(tcp, mp); 15202 } else { 15203 tcp_rcv_enqueue(tcp, mp, seg_len); 15204 } 15205 } else { 15206 sodirect_t *sodp = tcp->tcp_sodirect; 15207 15208 /* 15209 * If an sodirect connection and an enabled sodirect_t then 15210 * sodp will be set to point to the tcp_t/sonode_t shared 15211 * sodirect_t and the sodirect_t's lock will be held. 15212 */ 15213 if (sodp != NULL) { 15214 mutex_enter(sodp->sod_lockp); 15215 if (!(sodp->sod_state & SOD_ENABLED) || 15216 (tcp->tcp_kssl_ctx != NULL && 15217 DB_TYPE(mp) == M_DATA)) { 15218 mutex_exit(sodp->sod_lockp); 15219 sodp = NULL; 15220 } 15221 } 15222 if (mp->b_datap->db_type != M_DATA || 15223 (flags & TH_MARKNEXT_NEEDED)) { 15224 if (sodp != NULL) { 15225 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15226 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15227 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15228 } 15229 if (!SOD_QEMPTY(sodp) && 15230 (sodp->sod_state & SOD_WAKE_NOT)) { 15231 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15232 /* sod_wakeup() did the mutex_exit() */ 15233 } else { 15234 mutex_exit(sodp->sod_lockp); 15235 } 15236 } else if (tcp->tcp_rcv_list != NULL) { 15237 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15238 } 15239 ASSERT(tcp->tcp_rcv_list == NULL || 15240 tcp->tcp_fused_sigurg); 15241 15242 if (flags & TH_MARKNEXT_NEEDED) { 15243 #ifdef DEBUG 15244 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15245 "tcp_rput: sending MSGMARKNEXT %s", 15246 tcp_display(tcp, NULL, 15247 DISP_PORT_ONLY)); 15248 #endif /* DEBUG */ 15249 mp->b_flag |= MSGMARKNEXT; 15250 flags &= ~TH_MARKNEXT_NEEDED; 15251 } 15252 15253 /* Does this need SSL processing first? */ 15254 if ((tcp->tcp_kssl_ctx != NULL) && 15255 (DB_TYPE(mp) == M_DATA)) { 15256 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 15257 mblk_t *, mp); 15258 tcp_kssl_input(tcp, mp); 15259 } else { 15260 putnext(tcp->tcp_rq, mp); 15261 if (!canputnext(tcp->tcp_rq)) 15262 tcp->tcp_rwnd -= seg_len; 15263 } 15264 } else if ((tcp->tcp_kssl_ctx != NULL) && 15265 (DB_TYPE(mp) == M_DATA)) { 15266 /* Do SSL processing first */ 15267 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, 15268 mblk_t *, mp); 15269 tcp_kssl_input(tcp, mp); 15270 } else if (sodp != NULL) { 15271 /* 15272 * Sodirect so all mblk_t's are queued on the 15273 * socket directly, check for wakeup of blocked 15274 * reader (if any), and last if flow-controled. 15275 */ 15276 flags |= tcp_rcv_sod_enqueue(tcp, sodp, mp, seg_len); 15277 if ((sodp->sod_state & SOD_WAKE_NEED) || 15278 (flags & (TH_PUSH|TH_FIN))) { 15279 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15280 /* sod_wakeup() did the mutex_exit() */ 15281 } else { 15282 if (SOD_QFULL(sodp)) { 15283 /* Q is full, need backenable */ 15284 SOD_QSETBE(sodp); 15285 } 15286 mutex_exit(sodp->sod_lockp); 15287 } 15288 } else if ((flags & (TH_PUSH|TH_FIN)) || 15289 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 15290 if (tcp->tcp_rcv_list != NULL) { 15291 /* 15292 * Enqueue the new segment first and then 15293 * call tcp_rcv_drain() to send all data 15294 * up. The other way to do this is to 15295 * send all queued data up and then call 15296 * putnext() to send the new segment up. 15297 * This way can remove the else part later 15298 * on. 15299 * 15300 * We don't this to avoid one more call to 15301 * canputnext() as tcp_rcv_drain() needs to 15302 * call canputnext(). 15303 */ 15304 tcp_rcv_enqueue(tcp, mp, seg_len); 15305 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15306 } else { 15307 putnext(tcp->tcp_rq, mp); 15308 if (!canputnext(tcp->tcp_rq)) 15309 tcp->tcp_rwnd -= seg_len; 15310 } 15311 } else { 15312 /* 15313 * Enqueue all packets when processing an mblk 15314 * from the co queue and also enqueue normal packets. 15315 */ 15316 tcp_rcv_enqueue(tcp, mp, seg_len); 15317 } 15318 /* 15319 * Make sure the timer is running if we have data waiting 15320 * for a push bit. This provides resiliency against 15321 * implementations that do not correctly generate push bits. 15322 * 15323 * Note, for sodirect if Q isn't empty and there's not a 15324 * pending wakeup then we need a timer. Also note that sodp 15325 * is assumed to be still valid after exit()ing the sod_lockp 15326 * above and while the SOD state can change it can only change 15327 * such that the Q is empty now even though data was added 15328 * above. 15329 */ 15330 if (((sodp != NULL && !SOD_QEMPTY(sodp) && 15331 (sodp->sod_state & SOD_WAKE_NOT)) || 15332 (sodp == NULL && tcp->tcp_rcv_list != NULL)) && 15333 tcp->tcp_push_tid == 0) { 15334 /* 15335 * The connection may be closed at this point, so don't 15336 * do anything for a detached tcp. 15337 */ 15338 if (!TCP_IS_DETACHED(tcp)) 15339 tcp->tcp_push_tid = TCP_TIMER(tcp, 15340 tcp_push_timer, 15341 MSEC_TO_TICK( 15342 tcps->tcps_push_timer_interval)); 15343 } 15344 } 15345 15346 xmit_check: 15347 /* Is there anything left to do? */ 15348 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15349 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15350 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15351 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15352 goto done; 15353 15354 /* Any transmit work to do and a non-zero window? */ 15355 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15356 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15357 if (flags & TH_REXMIT_NEEDED) { 15358 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15359 15360 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15361 if (snd_size > mss) 15362 snd_size = mss; 15363 if (snd_size > tcp->tcp_swnd) 15364 snd_size = tcp->tcp_swnd; 15365 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15366 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15367 B_TRUE); 15368 15369 if (mp1 != NULL) { 15370 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15371 tcp->tcp_csuna = tcp->tcp_snxt; 15372 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15373 UPDATE_MIB(&tcps->tcps_mib, 15374 tcpRetransBytes, snd_size); 15375 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15376 } 15377 } 15378 if (flags & TH_NEED_SACK_REXMIT) { 15379 tcp_sack_rxmit(tcp, &flags); 15380 } 15381 /* 15382 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15383 * out new segment. Note that tcp_rexmit should not be 15384 * set, otherwise TH_LIMIT_XMIT should not be set. 15385 */ 15386 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15387 if (!tcp->tcp_rexmit) { 15388 tcp_wput_data(tcp, NULL, B_FALSE); 15389 } else { 15390 tcp_ss_rexmit(tcp); 15391 } 15392 } 15393 /* 15394 * Adjust tcp_cwnd back to normal value after sending 15395 * new data segments. 15396 */ 15397 if (flags & TH_LIMIT_XMIT) { 15398 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15399 /* 15400 * This will restart the timer. Restarting the 15401 * timer is used to avoid a timeout before the 15402 * limited transmitted segment's ACK gets back. 15403 */ 15404 if (tcp->tcp_xmit_head != NULL) 15405 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15406 } 15407 15408 /* Anything more to do? */ 15409 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15410 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15411 goto done; 15412 } 15413 ack_check: 15414 if (flags & TH_SEND_URP_MARK) { 15415 ASSERT(tcp->tcp_urp_mark_mp); 15416 /* 15417 * Send up any queued data and then send the mark message 15418 */ 15419 sodirect_t *sodp; 15420 15421 SOD_PTR_ENTER(tcp, sodp); 15422 15423 mp1 = tcp->tcp_urp_mark_mp; 15424 tcp->tcp_urp_mark_mp = NULL; 15425 if (sodp != NULL) { 15426 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15427 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15428 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15429 } 15430 ASSERT(tcp->tcp_rcv_list == NULL); 15431 15432 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15433 /* sod_wakeup() does the mutex_exit() */ 15434 } else if (tcp->tcp_rcv_list != NULL) { 15435 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15436 15437 ASSERT(tcp->tcp_rcv_list == NULL || 15438 tcp->tcp_fused_sigurg); 15439 15440 } 15441 putnext(tcp->tcp_rq, mp1); 15442 #ifdef DEBUG 15443 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15444 "tcp_rput: sending zero-length %s %s", 15445 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15446 "MSGNOTMARKNEXT"), 15447 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15448 #endif /* DEBUG */ 15449 flags &= ~TH_SEND_URP_MARK; 15450 } 15451 if (flags & TH_ACK_NEEDED) { 15452 /* 15453 * Time to send an ack for some reason. 15454 */ 15455 mp1 = tcp_ack_mp(tcp); 15456 15457 if (mp1 != NULL) { 15458 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15459 BUMP_LOCAL(tcp->tcp_obsegs); 15460 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15461 } 15462 if (tcp->tcp_ack_tid != 0) { 15463 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15464 tcp->tcp_ack_tid = 0; 15465 } 15466 } 15467 if (flags & TH_ACK_TIMER_NEEDED) { 15468 /* 15469 * Arrange for deferred ACK or push wait timeout. 15470 * Start timer if it is not already running. 15471 */ 15472 if (tcp->tcp_ack_tid == 0) { 15473 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15474 MSEC_TO_TICK(tcp->tcp_localnet ? 15475 (clock_t)tcps->tcps_local_dack_interval : 15476 (clock_t)tcps->tcps_deferred_ack_interval)); 15477 } 15478 } 15479 if (flags & TH_ORDREL_NEEDED) { 15480 /* 15481 * Send up the ordrel_ind unless we are an eager guy. 15482 * In the eager case tcp_rsrv will do this when run 15483 * after tcp_accept is done. 15484 */ 15485 sodirect_t *sodp; 15486 15487 ASSERT(tcp->tcp_listener == NULL); 15488 15489 SOD_PTR_ENTER(tcp, sodp); 15490 if (sodp != NULL) { 15491 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15492 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15493 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15494 } 15495 /* No more sodirect */ 15496 tcp->tcp_sodirect = NULL; 15497 if (!SOD_QEMPTY(sodp)) { 15498 /* Mblk(s) to process, notify */ 15499 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15500 /* sod_wakeup() does the mutex_exit() */ 15501 } else { 15502 /* Nothing to process */ 15503 mutex_exit(sodp->sod_lockp); 15504 } 15505 } else if (tcp->tcp_rcv_list != NULL) { 15506 /* 15507 * Push any mblk(s) enqueued from co processing. 15508 */ 15509 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15510 15511 ASSERT(tcp->tcp_rcv_list == NULL || 15512 tcp->tcp_fused_sigurg); 15513 } 15514 15515 mp1 = tcp->tcp_ordrel_mp; 15516 tcp->tcp_ordrel_mp = NULL; 15517 tcp->tcp_ordrel_done = B_TRUE; 15518 putnext(tcp->tcp_rq, mp1); 15519 } 15520 done: 15521 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15522 } 15523 15524 /* 15525 * This function does PAWS protection check. Returns B_TRUE if the 15526 * segment passes the PAWS test, else returns B_FALSE. 15527 */ 15528 boolean_t 15529 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15530 { 15531 uint8_t flags; 15532 int options; 15533 uint8_t *up; 15534 15535 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15536 /* 15537 * If timestamp option is aligned nicely, get values inline, 15538 * otherwise call general routine to parse. Only do that 15539 * if timestamp is the only option. 15540 */ 15541 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15542 TCPOPT_REAL_TS_LEN && 15543 OK_32PTR((up = ((uint8_t *)tcph) + 15544 TCP_MIN_HEADER_LENGTH)) && 15545 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15546 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15547 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15548 15549 options = TCP_OPT_TSTAMP_PRESENT; 15550 } else { 15551 if (tcp->tcp_snd_sack_ok) { 15552 tcpoptp->tcp = tcp; 15553 } else { 15554 tcpoptp->tcp = NULL; 15555 } 15556 options = tcp_parse_options(tcph, tcpoptp); 15557 } 15558 15559 if (options & TCP_OPT_TSTAMP_PRESENT) { 15560 /* 15561 * Do PAWS per RFC 1323 section 4.2. Accept RST 15562 * regardless of the timestamp, page 18 RFC 1323.bis. 15563 */ 15564 if ((flags & TH_RST) == 0 && 15565 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15566 tcp->tcp_ts_recent)) { 15567 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15568 PAWS_TIMEOUT)) { 15569 /* This segment is not acceptable. */ 15570 return (B_FALSE); 15571 } else { 15572 /* 15573 * Connection has been idle for 15574 * too long. Reset the timestamp 15575 * and assume the segment is valid. 15576 */ 15577 tcp->tcp_ts_recent = 15578 tcpoptp->tcp_opt_ts_val; 15579 } 15580 } 15581 } else { 15582 /* 15583 * If we don't get a timestamp on every packet, we 15584 * figure we can't really trust 'em, so we stop sending 15585 * and parsing them. 15586 */ 15587 tcp->tcp_snd_ts_ok = B_FALSE; 15588 15589 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15590 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15591 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15592 /* 15593 * Adjust the tcp_mss accordingly. We also need to 15594 * adjust tcp_cwnd here in accordance with the new mss. 15595 * But we avoid doing a slow start here so as to not 15596 * to lose on the transfer rate built up so far. 15597 */ 15598 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15599 if (tcp->tcp_snd_sack_ok) { 15600 ASSERT(tcp->tcp_sack_info != NULL); 15601 tcp->tcp_max_sack_blk = 4; 15602 } 15603 } 15604 return (B_TRUE); 15605 } 15606 15607 /* 15608 * Attach ancillary data to a received TCP segments for the 15609 * ancillary pieces requested by the application that are 15610 * different than they were in the previous data segment. 15611 * 15612 * Save the "current" values once memory allocation is ok so that 15613 * when memory allocation fails we can just wait for the next data segment. 15614 */ 15615 static mblk_t * 15616 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15617 { 15618 struct T_optdata_ind *todi; 15619 int optlen; 15620 uchar_t *optptr; 15621 struct T_opthdr *toh; 15622 uint_t addflag; /* Which pieces to add */ 15623 mblk_t *mp1; 15624 15625 optlen = 0; 15626 addflag = 0; 15627 /* If app asked for pktinfo and the index has changed ... */ 15628 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15629 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15630 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15631 optlen += sizeof (struct T_opthdr) + 15632 sizeof (struct in6_pktinfo); 15633 addflag |= TCP_IPV6_RECVPKTINFO; 15634 } 15635 /* If app asked for hoplimit and it has changed ... */ 15636 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15637 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15638 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15639 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15640 addflag |= TCP_IPV6_RECVHOPLIMIT; 15641 } 15642 /* If app asked for tclass and it has changed ... */ 15643 if ((ipp->ipp_fields & IPPF_TCLASS) && 15644 ipp->ipp_tclass != tcp->tcp_recvtclass && 15645 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15646 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15647 addflag |= TCP_IPV6_RECVTCLASS; 15648 } 15649 /* 15650 * If app asked for hopbyhop headers and it has changed ... 15651 * For security labels, note that (1) security labels can't change on 15652 * a connected socket at all, (2) we're connected to at most one peer, 15653 * (3) if anything changes, then it must be some other extra option. 15654 */ 15655 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15656 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15657 (ipp->ipp_fields & IPPF_HOPOPTS), 15658 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15659 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15660 tcp->tcp_label_len; 15661 addflag |= TCP_IPV6_RECVHOPOPTS; 15662 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15663 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15664 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15665 return (mp); 15666 } 15667 /* If app asked for dst headers before routing headers ... */ 15668 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15669 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15670 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15671 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15672 optlen += sizeof (struct T_opthdr) + 15673 ipp->ipp_rtdstoptslen; 15674 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15675 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15676 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15677 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15678 return (mp); 15679 } 15680 /* If app asked for routing headers and it has changed ... */ 15681 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15682 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15683 (ipp->ipp_fields & IPPF_RTHDR), 15684 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15685 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15686 addflag |= TCP_IPV6_RECVRTHDR; 15687 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15688 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15689 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15690 return (mp); 15691 } 15692 /* If app asked for dest headers and it has changed ... */ 15693 if ((tcp->tcp_ipv6_recvancillary & 15694 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15695 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15696 (ipp->ipp_fields & IPPF_DSTOPTS), 15697 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15698 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15699 addflag |= TCP_IPV6_RECVDSTOPTS; 15700 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15701 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15702 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15703 return (mp); 15704 } 15705 15706 if (optlen == 0) { 15707 /* Nothing to add */ 15708 return (mp); 15709 } 15710 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15711 if (mp1 == NULL) { 15712 /* 15713 * Defer sending ancillary data until the next TCP segment 15714 * arrives. 15715 */ 15716 return (mp); 15717 } 15718 mp1->b_cont = mp; 15719 mp = mp1; 15720 mp->b_wptr += sizeof (*todi) + optlen; 15721 mp->b_datap->db_type = M_PROTO; 15722 todi = (struct T_optdata_ind *)mp->b_rptr; 15723 todi->PRIM_type = T_OPTDATA_IND; 15724 todi->DATA_flag = 1; /* MORE data */ 15725 todi->OPT_length = optlen; 15726 todi->OPT_offset = sizeof (*todi); 15727 optptr = (uchar_t *)&todi[1]; 15728 /* 15729 * If app asked for pktinfo and the index has changed ... 15730 * Note that the local address never changes for the connection. 15731 */ 15732 if (addflag & TCP_IPV6_RECVPKTINFO) { 15733 struct in6_pktinfo *pkti; 15734 15735 toh = (struct T_opthdr *)optptr; 15736 toh->level = IPPROTO_IPV6; 15737 toh->name = IPV6_PKTINFO; 15738 toh->len = sizeof (*toh) + sizeof (*pkti); 15739 toh->status = 0; 15740 optptr += sizeof (*toh); 15741 pkti = (struct in6_pktinfo *)optptr; 15742 if (tcp->tcp_ipversion == IPV6_VERSION) 15743 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15744 else 15745 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15746 &pkti->ipi6_addr); 15747 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15748 optptr += sizeof (*pkti); 15749 ASSERT(OK_32PTR(optptr)); 15750 /* Save as "last" value */ 15751 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15752 } 15753 /* If app asked for hoplimit and it has changed ... */ 15754 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15755 toh = (struct T_opthdr *)optptr; 15756 toh->level = IPPROTO_IPV6; 15757 toh->name = IPV6_HOPLIMIT; 15758 toh->len = sizeof (*toh) + sizeof (uint_t); 15759 toh->status = 0; 15760 optptr += sizeof (*toh); 15761 *(uint_t *)optptr = ipp->ipp_hoplimit; 15762 optptr += sizeof (uint_t); 15763 ASSERT(OK_32PTR(optptr)); 15764 /* Save as "last" value */ 15765 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15766 } 15767 /* If app asked for tclass and it has changed ... */ 15768 if (addflag & TCP_IPV6_RECVTCLASS) { 15769 toh = (struct T_opthdr *)optptr; 15770 toh->level = IPPROTO_IPV6; 15771 toh->name = IPV6_TCLASS; 15772 toh->len = sizeof (*toh) + sizeof (uint_t); 15773 toh->status = 0; 15774 optptr += sizeof (*toh); 15775 *(uint_t *)optptr = ipp->ipp_tclass; 15776 optptr += sizeof (uint_t); 15777 ASSERT(OK_32PTR(optptr)); 15778 /* Save as "last" value */ 15779 tcp->tcp_recvtclass = ipp->ipp_tclass; 15780 } 15781 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15782 toh = (struct T_opthdr *)optptr; 15783 toh->level = IPPROTO_IPV6; 15784 toh->name = IPV6_HOPOPTS; 15785 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15786 tcp->tcp_label_len; 15787 toh->status = 0; 15788 optptr += sizeof (*toh); 15789 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15790 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15791 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15792 ASSERT(OK_32PTR(optptr)); 15793 /* Save as last value */ 15794 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15795 (ipp->ipp_fields & IPPF_HOPOPTS), 15796 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15797 } 15798 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15799 toh = (struct T_opthdr *)optptr; 15800 toh->level = IPPROTO_IPV6; 15801 toh->name = IPV6_RTHDRDSTOPTS; 15802 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15803 toh->status = 0; 15804 optptr += sizeof (*toh); 15805 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15806 optptr += ipp->ipp_rtdstoptslen; 15807 ASSERT(OK_32PTR(optptr)); 15808 /* Save as last value */ 15809 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15810 &tcp->tcp_rtdstoptslen, 15811 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15812 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15813 } 15814 if (addflag & TCP_IPV6_RECVRTHDR) { 15815 toh = (struct T_opthdr *)optptr; 15816 toh->level = IPPROTO_IPV6; 15817 toh->name = IPV6_RTHDR; 15818 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15819 toh->status = 0; 15820 optptr += sizeof (*toh); 15821 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15822 optptr += ipp->ipp_rthdrlen; 15823 ASSERT(OK_32PTR(optptr)); 15824 /* Save as last value */ 15825 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15826 (ipp->ipp_fields & IPPF_RTHDR), 15827 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15828 } 15829 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15830 toh = (struct T_opthdr *)optptr; 15831 toh->level = IPPROTO_IPV6; 15832 toh->name = IPV6_DSTOPTS; 15833 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15834 toh->status = 0; 15835 optptr += sizeof (*toh); 15836 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15837 optptr += ipp->ipp_dstoptslen; 15838 ASSERT(OK_32PTR(optptr)); 15839 /* Save as last value */ 15840 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15841 (ipp->ipp_fields & IPPF_DSTOPTS), 15842 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15843 } 15844 ASSERT(optptr == mp->b_wptr); 15845 return (mp); 15846 } 15847 15848 15849 /* 15850 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15851 * or a "bad" IRE detected by tcp_adapt_ire. 15852 * We can't tell if the failure was due to the laddr or the faddr 15853 * thus we clear out all addresses and ports. 15854 */ 15855 static void 15856 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15857 { 15858 queue_t *q = tcp->tcp_rq; 15859 tcph_t *tcph; 15860 struct T_error_ack *tea; 15861 conn_t *connp = tcp->tcp_connp; 15862 15863 15864 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15865 15866 if (mp->b_cont) { 15867 freemsg(mp->b_cont); 15868 mp->b_cont = NULL; 15869 } 15870 tea = (struct T_error_ack *)mp->b_rptr; 15871 switch (tea->PRIM_type) { 15872 case T_BIND_ACK: 15873 /* 15874 * Need to unbind with classifier since we were just told that 15875 * our bind succeeded. 15876 */ 15877 tcp->tcp_hard_bound = B_FALSE; 15878 tcp->tcp_hard_binding = B_FALSE; 15879 15880 ipcl_hash_remove(connp); 15881 /* Reuse the mblk if possible */ 15882 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15883 sizeof (*tea)); 15884 mp->b_rptr = mp->b_datap->db_base; 15885 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15886 tea = (struct T_error_ack *)mp->b_rptr; 15887 tea->PRIM_type = T_ERROR_ACK; 15888 tea->TLI_error = TSYSERR; 15889 tea->UNIX_error = error; 15890 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15891 tea->ERROR_prim = T_CONN_REQ; 15892 } else { 15893 tea->ERROR_prim = O_T_BIND_REQ; 15894 } 15895 break; 15896 15897 case T_ERROR_ACK: 15898 if (tcp->tcp_state >= TCPS_SYN_SENT) 15899 tea->ERROR_prim = T_CONN_REQ; 15900 break; 15901 default: 15902 panic("tcp_bind_failed: unexpected TPI type"); 15903 /*NOTREACHED*/ 15904 } 15905 15906 tcp->tcp_state = TCPS_IDLE; 15907 if (tcp->tcp_ipversion == IPV4_VERSION) 15908 tcp->tcp_ipha->ipha_src = 0; 15909 else 15910 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15911 /* 15912 * Copy of the src addr. in tcp_t is needed since 15913 * the lookup funcs. can only look at tcp_t 15914 */ 15915 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15916 15917 tcph = tcp->tcp_tcph; 15918 tcph->th_lport[0] = 0; 15919 tcph->th_lport[1] = 0; 15920 tcp_bind_hash_remove(tcp); 15921 bzero(&connp->u_port, sizeof (connp->u_port)); 15922 /* blow away saved option results if any */ 15923 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15924 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15925 15926 conn_delete_ire(tcp->tcp_connp, NULL); 15927 putnext(q, mp); 15928 } 15929 15930 /* 15931 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15932 * messages. 15933 */ 15934 void 15935 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15936 { 15937 mblk_t *mp1; 15938 uchar_t *rptr = mp->b_rptr; 15939 queue_t *q = tcp->tcp_rq; 15940 struct T_error_ack *tea; 15941 uint32_t mss; 15942 mblk_t *syn_mp; 15943 mblk_t *mdti; 15944 mblk_t *lsoi; 15945 int retval; 15946 mblk_t *ire_mp; 15947 tcp_stack_t *tcps = tcp->tcp_tcps; 15948 15949 switch (mp->b_datap->db_type) { 15950 case M_PROTO: 15951 case M_PCPROTO: 15952 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15953 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15954 break; 15955 tea = (struct T_error_ack *)rptr; 15956 switch (tea->PRIM_type) { 15957 case T_BIND_ACK: 15958 /* 15959 * Adapt Multidata information, if any. The 15960 * following tcp_mdt_update routine will free 15961 * the message. 15962 */ 15963 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 15964 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 15965 b_rptr)->mdt_capab, B_TRUE); 15966 freemsg(mdti); 15967 } 15968 15969 /* 15970 * Check to update LSO information with tcp, and 15971 * tcp_lso_update routine will free the message. 15972 */ 15973 if ((lsoi = tcp_lso_info_mp(mp)) != NULL) { 15974 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 15975 b_rptr)->lso_capab); 15976 freemsg(lsoi); 15977 } 15978 15979 /* Get the IRE, if we had requested for it */ 15980 ire_mp = tcp_ire_mp(mp); 15981 15982 if (tcp->tcp_hard_binding) { 15983 tcp->tcp_hard_binding = B_FALSE; 15984 tcp->tcp_hard_bound = B_TRUE; 15985 CL_INET_CONNECT(tcp); 15986 } else { 15987 if (ire_mp != NULL) 15988 freeb(ire_mp); 15989 goto after_syn_sent; 15990 } 15991 15992 retval = tcp_adapt_ire(tcp, ire_mp); 15993 if (ire_mp != NULL) 15994 freeb(ire_mp); 15995 if (retval == 0) { 15996 tcp_bind_failed(tcp, mp, 15997 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15998 ENETUNREACH : EADDRNOTAVAIL)); 15999 return; 16000 } 16001 /* 16002 * Don't let an endpoint connect to itself. 16003 * Also checked in tcp_connect() but that 16004 * check can't handle the case when the 16005 * local IP address is INADDR_ANY. 16006 */ 16007 if (tcp->tcp_ipversion == IPV4_VERSION) { 16008 if ((tcp->tcp_ipha->ipha_dst == 16009 tcp->tcp_ipha->ipha_src) && 16010 (BE16_EQL(tcp->tcp_tcph->th_lport, 16011 tcp->tcp_tcph->th_fport))) { 16012 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 16013 return; 16014 } 16015 } else { 16016 if (IN6_ARE_ADDR_EQUAL( 16017 &tcp->tcp_ip6h->ip6_dst, 16018 &tcp->tcp_ip6h->ip6_src) && 16019 (BE16_EQL(tcp->tcp_tcph->th_lport, 16020 tcp->tcp_tcph->th_fport))) { 16021 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 16022 return; 16023 } 16024 } 16025 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 16026 /* 16027 * This should not be possible! Just for 16028 * defensive coding... 16029 */ 16030 if (tcp->tcp_state != TCPS_SYN_SENT) 16031 goto after_syn_sent; 16032 16033 if (is_system_labeled() && 16034 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 16035 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 16036 return; 16037 } 16038 16039 ASSERT(q == tcp->tcp_rq); 16040 /* 16041 * tcp_adapt_ire() does not adjust 16042 * for TCP/IP header length. 16043 */ 16044 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 16045 16046 /* 16047 * Just make sure our rwnd is at 16048 * least tcp_recv_hiwat_mss * MSS 16049 * large, and round up to the nearest 16050 * MSS. 16051 * 16052 * We do the round up here because 16053 * we need to get the interface 16054 * MTU first before we can do the 16055 * round up. 16056 */ 16057 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 16058 tcps->tcps_recv_hiwat_minmss * mss); 16059 q->q_hiwat = tcp->tcp_rwnd; 16060 tcp_set_ws_value(tcp); 16061 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 16062 tcp->tcp_tcph->th_win); 16063 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 16064 tcp->tcp_snd_ws_ok = B_TRUE; 16065 16066 /* 16067 * Set tcp_snd_ts_ok to true 16068 * so that tcp_xmit_mp will 16069 * include the timestamp 16070 * option in the SYN segment. 16071 */ 16072 if (tcps->tcps_tstamp_always || 16073 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 16074 tcp->tcp_snd_ts_ok = B_TRUE; 16075 } 16076 16077 /* 16078 * tcp_snd_sack_ok can be set in 16079 * tcp_adapt_ire() if the sack metric 16080 * is set. So check it here also. 16081 */ 16082 if (tcps->tcps_sack_permitted == 2 || 16083 tcp->tcp_snd_sack_ok) { 16084 if (tcp->tcp_sack_info == NULL) { 16085 tcp->tcp_sack_info = 16086 kmem_cache_alloc( 16087 tcp_sack_info_cache, 16088 KM_SLEEP); 16089 } 16090 tcp->tcp_snd_sack_ok = B_TRUE; 16091 } 16092 16093 /* 16094 * Should we use ECN? Note that the current 16095 * default value (SunOS 5.9) of tcp_ecn_permitted 16096 * is 1. The reason for doing this is that there 16097 * are equipments out there that will drop ECN 16098 * enabled IP packets. Setting it to 1 avoids 16099 * compatibility problems. 16100 */ 16101 if (tcps->tcps_ecn_permitted == 2) 16102 tcp->tcp_ecn_ok = B_TRUE; 16103 16104 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 16105 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 16106 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 16107 if (syn_mp) { 16108 cred_t *cr; 16109 pid_t pid; 16110 16111 /* 16112 * Obtain the credential from the 16113 * thread calling connect(); the credential 16114 * lives on in the second mblk which 16115 * originated from T_CONN_REQ and is echoed 16116 * with the T_BIND_ACK from ip. If none 16117 * can be found, default to the creator 16118 * of the socket. 16119 */ 16120 if (mp->b_cont == NULL || 16121 (cr = DB_CRED(mp->b_cont)) == NULL) { 16122 cr = tcp->tcp_cred; 16123 pid = tcp->tcp_cpid; 16124 } else { 16125 pid = DB_CPID(mp->b_cont); 16126 } 16127 mblk_setcred(syn_mp, cr); 16128 DB_CPID(syn_mp) = pid; 16129 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 16130 } 16131 after_syn_sent: 16132 /* 16133 * A trailer mblk indicates a waiting client upstream. 16134 * We complete here the processing begun in 16135 * either tcp_bind() or tcp_connect() by passing 16136 * upstream the reply message they supplied. 16137 */ 16138 mp1 = mp; 16139 mp = mp->b_cont; 16140 freeb(mp1); 16141 if (mp) 16142 break; 16143 return; 16144 case T_ERROR_ACK: 16145 if (tcp->tcp_debug) { 16146 (void) strlog(TCP_MOD_ID, 0, 1, 16147 SL_TRACE|SL_ERROR, 16148 "tcp_rput_other: case T_ERROR_ACK, " 16149 "ERROR_prim == %d", 16150 tea->ERROR_prim); 16151 } 16152 switch (tea->ERROR_prim) { 16153 case O_T_BIND_REQ: 16154 case T_BIND_REQ: 16155 tcp_bind_failed(tcp, mp, 16156 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 16157 ENETUNREACH : EADDRNOTAVAIL)); 16158 return; 16159 case T_UNBIND_REQ: 16160 tcp->tcp_hard_binding = B_FALSE; 16161 tcp->tcp_hard_bound = B_FALSE; 16162 if (mp->b_cont) { 16163 freemsg(mp->b_cont); 16164 mp->b_cont = NULL; 16165 } 16166 if (tcp->tcp_unbind_pending) 16167 tcp->tcp_unbind_pending = 0; 16168 else { 16169 /* From tcp_ip_unbind() - free */ 16170 freemsg(mp); 16171 return; 16172 } 16173 break; 16174 case T_SVR4_OPTMGMT_REQ: 16175 if (tcp->tcp_drop_opt_ack_cnt > 0) { 16176 /* T_OPTMGMT_REQ generated by TCP */ 16177 printf("T_SVR4_OPTMGMT_REQ failed " 16178 "%d/%d - dropped (cnt %d)\n", 16179 tea->TLI_error, tea->UNIX_error, 16180 tcp->tcp_drop_opt_ack_cnt); 16181 freemsg(mp); 16182 tcp->tcp_drop_opt_ack_cnt--; 16183 return; 16184 } 16185 break; 16186 } 16187 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 16188 tcp->tcp_drop_opt_ack_cnt > 0) { 16189 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 16190 "- dropped (cnt %d)\n", 16191 tea->TLI_error, tea->UNIX_error, 16192 tcp->tcp_drop_opt_ack_cnt); 16193 freemsg(mp); 16194 tcp->tcp_drop_opt_ack_cnt--; 16195 return; 16196 } 16197 break; 16198 case T_OPTMGMT_ACK: 16199 if (tcp->tcp_drop_opt_ack_cnt > 0) { 16200 /* T_OPTMGMT_REQ generated by TCP */ 16201 freemsg(mp); 16202 tcp->tcp_drop_opt_ack_cnt--; 16203 return; 16204 } 16205 break; 16206 default: 16207 break; 16208 } 16209 break; 16210 case M_FLUSH: 16211 if (*rptr & FLUSHR) 16212 flushq(q, FLUSHDATA); 16213 break; 16214 default: 16215 /* M_CTL will be directly sent to tcp_icmp_error() */ 16216 ASSERT(DB_TYPE(mp) != M_CTL); 16217 break; 16218 } 16219 /* 16220 * Make sure we set this bit before sending the ACK for 16221 * bind. Otherwise accept could possibly run and free 16222 * this tcp struct. 16223 */ 16224 putnext(q, mp); 16225 } 16226 16227 /* ARGSUSED */ 16228 static void 16229 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 16230 { 16231 conn_t *connp = (conn_t *)arg; 16232 tcp_t *tcp = connp->conn_tcp; 16233 queue_t *q = tcp->tcp_rq; 16234 uint_t thwin; 16235 tcp_stack_t *tcps = tcp->tcp_tcps; 16236 sodirect_t *sodp; 16237 boolean_t fc; 16238 16239 mutex_enter(&tcp->tcp_rsrv_mp_lock); 16240 tcp->tcp_rsrv_mp = mp; 16241 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16242 16243 TCP_STAT(tcps, tcp_rsrv_calls); 16244 16245 if (TCP_IS_DETACHED(tcp) || q == NULL) { 16246 return; 16247 } 16248 16249 if (tcp->tcp_fused) { 16250 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16251 16252 ASSERT(tcp->tcp_fused); 16253 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 16254 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 16255 ASSERT(!TCP_IS_DETACHED(tcp)); 16256 ASSERT(tcp->tcp_connp->conn_sqp == 16257 peer_tcp->tcp_connp->conn_sqp); 16258 16259 /* 16260 * Normally we would not get backenabled in synchronous 16261 * streams mode, but in case this happens, we need to plug 16262 * synchronous streams during our drain to prevent a race 16263 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 16264 */ 16265 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 16266 if (tcp->tcp_rcv_list != NULL) 16267 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 16268 16269 if (peer_tcp > tcp) { 16270 mutex_enter(&peer_tcp->tcp_non_sq_lock); 16271 mutex_enter(&tcp->tcp_non_sq_lock); 16272 } else { 16273 mutex_enter(&tcp->tcp_non_sq_lock); 16274 mutex_enter(&peer_tcp->tcp_non_sq_lock); 16275 } 16276 16277 if (peer_tcp->tcp_flow_stopped && 16278 (TCP_UNSENT_BYTES(peer_tcp) <= 16279 peer_tcp->tcp_xmit_lowater)) { 16280 tcp_clrqfull(peer_tcp); 16281 } 16282 mutex_exit(&peer_tcp->tcp_non_sq_lock); 16283 mutex_exit(&tcp->tcp_non_sq_lock); 16284 16285 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 16286 TCP_STAT(tcps, tcp_fusion_backenabled); 16287 return; 16288 } 16289 16290 SOD_PTR_ENTER(tcp, sodp); 16291 if (sodp != NULL) { 16292 /* An sodirect connection */ 16293 if (SOD_QFULL(sodp)) { 16294 /* Flow-controlled, need another back-enable */ 16295 fc = B_TRUE; 16296 SOD_QSETBE(sodp); 16297 } else { 16298 /* Not flow-controlled */ 16299 fc = B_FALSE; 16300 } 16301 mutex_exit(sodp->sod_lockp); 16302 } else if (canputnext(q)) { 16303 /* STREAMS, not flow-controlled */ 16304 fc = B_FALSE; 16305 } else { 16306 /* STREAMS, flow-controlled */ 16307 fc = B_TRUE; 16308 } 16309 if (!fc) { 16310 /* Not flow-controlled, open rwnd */ 16311 tcp->tcp_rwnd = q->q_hiwat; 16312 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 16313 << tcp->tcp_rcv_ws; 16314 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 16315 /* 16316 * Send back a window update immediately if TCP is above 16317 * ESTABLISHED state and the increase of the rcv window 16318 * that the other side knows is at least 1 MSS after flow 16319 * control is lifted. 16320 */ 16321 if (tcp->tcp_state >= TCPS_ESTABLISHED && 16322 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 16323 tcp_xmit_ctl(NULL, tcp, 16324 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 16325 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 16326 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 16327 } 16328 } 16329 } 16330 16331 /* 16332 * The read side service routine is called mostly when we get back-enabled as a 16333 * result of flow control relief. Since we don't actually queue anything in 16334 * TCP, we have no data to send out of here. What we do is clear the receive 16335 * window, and send out a window update. 16336 */ 16337 static void 16338 tcp_rsrv(queue_t *q) 16339 { 16340 conn_t *connp = Q_TO_CONN(q); 16341 tcp_t *tcp = connp->conn_tcp; 16342 mblk_t *mp; 16343 tcp_stack_t *tcps = tcp->tcp_tcps; 16344 16345 /* No code does a putq on the read side */ 16346 ASSERT(q->q_first == NULL); 16347 16348 /* Nothing to do for the default queue */ 16349 if (q == tcps->tcps_g_q) { 16350 return; 16351 } 16352 16353 /* 16354 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 16355 * been run. So just return. 16356 */ 16357 mutex_enter(&tcp->tcp_rsrv_mp_lock); 16358 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 16359 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16360 return; 16361 } 16362 tcp->tcp_rsrv_mp = NULL; 16363 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16364 16365 CONN_INC_REF(connp); 16366 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16367 SQTAG_TCP_RSRV); 16368 } 16369 16370 /* 16371 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16372 * We do not allow the receive window to shrink. After setting rwnd, 16373 * set the flow control hiwat of the stream. 16374 * 16375 * This function is called in 2 cases: 16376 * 16377 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16378 * connection (passive open) and in tcp_rput_data() for active connect. 16379 * This is called after tcp_mss_set() when the desired MSS value is known. 16380 * This makes sure that our window size is a mutiple of the other side's 16381 * MSS. 16382 * 2) Handling SO_RCVBUF option. 16383 * 16384 * It is ASSUMED that the requested size is a multiple of the current MSS. 16385 * 16386 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16387 * user requests so. 16388 */ 16389 static int 16390 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16391 { 16392 uint32_t mss = tcp->tcp_mss; 16393 uint32_t old_max_rwnd; 16394 uint32_t max_transmittable_rwnd; 16395 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16396 tcp_stack_t *tcps = tcp->tcp_tcps; 16397 16398 if (tcp->tcp_fused) { 16399 size_t sth_hiwat; 16400 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16401 16402 ASSERT(peer_tcp != NULL); 16403 /* 16404 * Record the stream head's high water mark for 16405 * this endpoint; this is used for flow-control 16406 * purposes in tcp_fuse_output(). 16407 */ 16408 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16409 if (!tcp_detached) 16410 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 16411 16412 /* 16413 * In the fusion case, the maxpsz stream head value of 16414 * our peer is set according to its send buffer size 16415 * and our receive buffer size; since the latter may 16416 * have changed we need to update the peer's maxpsz. 16417 */ 16418 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16419 return (rwnd); 16420 } 16421 16422 if (tcp_detached) 16423 old_max_rwnd = tcp->tcp_rwnd; 16424 else 16425 old_max_rwnd = tcp->tcp_rq->q_hiwat; 16426 16427 /* 16428 * Insist on a receive window that is at least 16429 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16430 * funny TCP interactions of Nagle algorithm, SWS avoidance 16431 * and delayed acknowledgement. 16432 */ 16433 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16434 16435 /* 16436 * If window size info has already been exchanged, TCP should not 16437 * shrink the window. Shrinking window is doable if done carefully. 16438 * We may add that support later. But so far there is not a real 16439 * need to do that. 16440 */ 16441 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16442 /* MSS may have changed, do a round up again. */ 16443 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16444 } 16445 16446 /* 16447 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16448 * can be applied even before the window scale option is decided. 16449 */ 16450 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16451 if (rwnd > max_transmittable_rwnd) { 16452 rwnd = max_transmittable_rwnd - 16453 (max_transmittable_rwnd % mss); 16454 if (rwnd < mss) 16455 rwnd = max_transmittable_rwnd; 16456 /* 16457 * If we're over the limit we may have to back down tcp_rwnd. 16458 * The increment below won't work for us. So we set all three 16459 * here and the increment below will have no effect. 16460 */ 16461 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16462 } 16463 if (tcp->tcp_localnet) { 16464 tcp->tcp_rack_abs_max = 16465 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16466 } else { 16467 /* 16468 * For a remote host on a different subnet (through a router), 16469 * we ack every other packet to be conforming to RFC1122. 16470 * tcp_deferred_acks_max is default to 2. 16471 */ 16472 tcp->tcp_rack_abs_max = 16473 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16474 } 16475 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16476 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16477 else 16478 tcp->tcp_rack_cur_max = 0; 16479 /* 16480 * Increment the current rwnd by the amount the maximum grew (we 16481 * can not overwrite it since we might be in the middle of a 16482 * connection.) 16483 */ 16484 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16485 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16486 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16487 tcp->tcp_cwnd_max = rwnd; 16488 16489 if (tcp_detached) 16490 return (rwnd); 16491 /* 16492 * We set the maximum receive window into rq->q_hiwat. 16493 * This is not actually used for flow control. 16494 */ 16495 tcp->tcp_rq->q_hiwat = rwnd; 16496 /* 16497 * Set the Stream head high water mark. This doesn't have to be 16498 * here, since we are simply using default values, but we would 16499 * prefer to choose these values algorithmically, with a likely 16500 * relationship to rwnd. 16501 */ 16502 (void) mi_set_sth_hiwat(tcp->tcp_rq, 16503 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16504 return (rwnd); 16505 } 16506 16507 /* 16508 * Return SNMP stuff in buffer in mpdata. 16509 */ 16510 mblk_t * 16511 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16512 { 16513 mblk_t *mpdata; 16514 mblk_t *mp_conn_ctl = NULL; 16515 mblk_t *mp_conn_tail; 16516 mblk_t *mp_attr_ctl = NULL; 16517 mblk_t *mp_attr_tail; 16518 mblk_t *mp6_conn_ctl = NULL; 16519 mblk_t *mp6_conn_tail; 16520 mblk_t *mp6_attr_ctl = NULL; 16521 mblk_t *mp6_attr_tail; 16522 struct opthdr *optp; 16523 mib2_tcpConnEntry_t tce; 16524 mib2_tcp6ConnEntry_t tce6; 16525 mib2_transportMLPEntry_t mlp; 16526 connf_t *connfp; 16527 int i; 16528 boolean_t ispriv; 16529 zoneid_t zoneid; 16530 int v4_conn_idx; 16531 int v6_conn_idx; 16532 conn_t *connp = Q_TO_CONN(q); 16533 tcp_stack_t *tcps; 16534 ip_stack_t *ipst; 16535 mblk_t *mp2ctl; 16536 16537 /* 16538 * make a copy of the original message 16539 */ 16540 mp2ctl = copymsg(mpctl); 16541 16542 if (mpctl == NULL || 16543 (mpdata = mpctl->b_cont) == NULL || 16544 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16545 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16546 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16547 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16548 freemsg(mp_conn_ctl); 16549 freemsg(mp_attr_ctl); 16550 freemsg(mp6_conn_ctl); 16551 freemsg(mp6_attr_ctl); 16552 freemsg(mpctl); 16553 freemsg(mp2ctl); 16554 return (NULL); 16555 } 16556 16557 ipst = connp->conn_netstack->netstack_ip; 16558 tcps = connp->conn_netstack->netstack_tcp; 16559 16560 /* build table of connections -- need count in fixed part */ 16561 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16562 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16563 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16564 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16565 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16566 16567 ispriv = 16568 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16569 zoneid = Q_TO_CONN(q)->conn_zoneid; 16570 16571 v4_conn_idx = v6_conn_idx = 0; 16572 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16573 16574 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16575 ipst = tcps->tcps_netstack->netstack_ip; 16576 16577 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16578 16579 connp = NULL; 16580 16581 while ((connp = 16582 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16583 tcp_t *tcp; 16584 boolean_t needattr; 16585 16586 if (connp->conn_zoneid != zoneid) 16587 continue; /* not in this zone */ 16588 16589 tcp = connp->conn_tcp; 16590 UPDATE_MIB(&tcps->tcps_mib, 16591 tcpHCInSegs, tcp->tcp_ibsegs); 16592 tcp->tcp_ibsegs = 0; 16593 UPDATE_MIB(&tcps->tcps_mib, 16594 tcpHCOutSegs, tcp->tcp_obsegs); 16595 tcp->tcp_obsegs = 0; 16596 16597 tce6.tcp6ConnState = tce.tcpConnState = 16598 tcp_snmp_state(tcp); 16599 if (tce.tcpConnState == MIB2_TCP_established || 16600 tce.tcpConnState == MIB2_TCP_closeWait) 16601 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16602 16603 needattr = B_FALSE; 16604 bzero(&mlp, sizeof (mlp)); 16605 if (connp->conn_mlp_type != mlptSingle) { 16606 if (connp->conn_mlp_type == mlptShared || 16607 connp->conn_mlp_type == mlptBoth) 16608 mlp.tme_flags |= MIB2_TMEF_SHARED; 16609 if (connp->conn_mlp_type == mlptPrivate || 16610 connp->conn_mlp_type == mlptBoth) 16611 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16612 needattr = B_TRUE; 16613 } 16614 if (connp->conn_peercred != NULL) { 16615 ts_label_t *tsl; 16616 16617 tsl = crgetlabel(connp->conn_peercred); 16618 mlp.tme_doi = label2doi(tsl); 16619 mlp.tme_label = *label2bslabel(tsl); 16620 needattr = B_TRUE; 16621 } 16622 16623 /* Create a message to report on IPv6 entries */ 16624 if (tcp->tcp_ipversion == IPV6_VERSION) { 16625 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16626 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16627 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16628 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16629 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16630 /* Don't want just anybody seeing these... */ 16631 if (ispriv) { 16632 tce6.tcp6ConnEntryInfo.ce_snxt = 16633 tcp->tcp_snxt; 16634 tce6.tcp6ConnEntryInfo.ce_suna = 16635 tcp->tcp_suna; 16636 tce6.tcp6ConnEntryInfo.ce_rnxt = 16637 tcp->tcp_rnxt; 16638 tce6.tcp6ConnEntryInfo.ce_rack = 16639 tcp->tcp_rack; 16640 } else { 16641 /* 16642 * Netstat, unfortunately, uses this to 16643 * get send/receive queue sizes. How to fix? 16644 * Why not compute the difference only? 16645 */ 16646 tce6.tcp6ConnEntryInfo.ce_snxt = 16647 tcp->tcp_snxt - tcp->tcp_suna; 16648 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16649 tce6.tcp6ConnEntryInfo.ce_rnxt = 16650 tcp->tcp_rnxt - tcp->tcp_rack; 16651 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16652 } 16653 16654 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16655 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16656 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16657 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16658 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16659 16660 tce6.tcp6ConnCreationProcess = 16661 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16662 tcp->tcp_cpid; 16663 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16664 16665 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16666 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16667 16668 mlp.tme_connidx = v6_conn_idx++; 16669 if (needattr) 16670 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16671 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16672 } 16673 /* 16674 * Create an IPv4 table entry for IPv4 entries and also 16675 * for IPv6 entries which are bound to in6addr_any 16676 * but don't have IPV6_V6ONLY set. 16677 * (i.e. anything an IPv4 peer could connect to) 16678 */ 16679 if (tcp->tcp_ipversion == IPV4_VERSION || 16680 (tcp->tcp_state <= TCPS_LISTEN && 16681 !tcp->tcp_connp->conn_ipv6_v6only && 16682 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16683 if (tcp->tcp_ipversion == IPV6_VERSION) { 16684 tce.tcpConnRemAddress = INADDR_ANY; 16685 tce.tcpConnLocalAddress = INADDR_ANY; 16686 } else { 16687 tce.tcpConnRemAddress = 16688 tcp->tcp_remote; 16689 tce.tcpConnLocalAddress = 16690 tcp->tcp_ip_src; 16691 } 16692 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16693 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16694 /* Don't want just anybody seeing these... */ 16695 if (ispriv) { 16696 tce.tcpConnEntryInfo.ce_snxt = 16697 tcp->tcp_snxt; 16698 tce.tcpConnEntryInfo.ce_suna = 16699 tcp->tcp_suna; 16700 tce.tcpConnEntryInfo.ce_rnxt = 16701 tcp->tcp_rnxt; 16702 tce.tcpConnEntryInfo.ce_rack = 16703 tcp->tcp_rack; 16704 } else { 16705 /* 16706 * Netstat, unfortunately, uses this to 16707 * get send/receive queue sizes. How 16708 * to fix? 16709 * Why not compute the difference only? 16710 */ 16711 tce.tcpConnEntryInfo.ce_snxt = 16712 tcp->tcp_snxt - tcp->tcp_suna; 16713 tce.tcpConnEntryInfo.ce_suna = 0; 16714 tce.tcpConnEntryInfo.ce_rnxt = 16715 tcp->tcp_rnxt - tcp->tcp_rack; 16716 tce.tcpConnEntryInfo.ce_rack = 0; 16717 } 16718 16719 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16720 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16721 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16722 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16723 tce.tcpConnEntryInfo.ce_state = 16724 tcp->tcp_state; 16725 16726 tce.tcpConnCreationProcess = 16727 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16728 tcp->tcp_cpid; 16729 tce.tcpConnCreationTime = tcp->tcp_open_time; 16730 16731 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16732 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16733 16734 mlp.tme_connidx = v4_conn_idx++; 16735 if (needattr) 16736 (void) snmp_append_data2( 16737 mp_attr_ctl->b_cont, 16738 &mp_attr_tail, (char *)&mlp, 16739 sizeof (mlp)); 16740 } 16741 } 16742 } 16743 16744 /* fixed length structure for IPv4 and IPv6 counters */ 16745 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16746 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16747 sizeof (mib2_tcp6ConnEntry_t)); 16748 /* synchronize 32- and 64-bit counters */ 16749 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16750 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16751 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16752 optp->level = MIB2_TCP; 16753 optp->name = 0; 16754 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16755 sizeof (tcps->tcps_mib)); 16756 optp->len = msgdsize(mpdata); 16757 qreply(q, mpctl); 16758 16759 /* table of connections... */ 16760 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16761 sizeof (struct T_optmgmt_ack)]; 16762 optp->level = MIB2_TCP; 16763 optp->name = MIB2_TCP_CONN; 16764 optp->len = msgdsize(mp_conn_ctl->b_cont); 16765 qreply(q, mp_conn_ctl); 16766 16767 /* table of MLP attributes... */ 16768 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16769 sizeof (struct T_optmgmt_ack)]; 16770 optp->level = MIB2_TCP; 16771 optp->name = EXPER_XPORT_MLP; 16772 optp->len = msgdsize(mp_attr_ctl->b_cont); 16773 if (optp->len == 0) 16774 freemsg(mp_attr_ctl); 16775 else 16776 qreply(q, mp_attr_ctl); 16777 16778 /* table of IPv6 connections... */ 16779 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16780 sizeof (struct T_optmgmt_ack)]; 16781 optp->level = MIB2_TCP6; 16782 optp->name = MIB2_TCP6_CONN; 16783 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16784 qreply(q, mp6_conn_ctl); 16785 16786 /* table of IPv6 MLP attributes... */ 16787 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16788 sizeof (struct T_optmgmt_ack)]; 16789 optp->level = MIB2_TCP6; 16790 optp->name = EXPER_XPORT_MLP; 16791 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16792 if (optp->len == 0) 16793 freemsg(mp6_attr_ctl); 16794 else 16795 qreply(q, mp6_attr_ctl); 16796 return (mp2ctl); 16797 } 16798 16799 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16800 /* ARGSUSED */ 16801 int 16802 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16803 { 16804 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16805 16806 switch (level) { 16807 case MIB2_TCP: 16808 switch (name) { 16809 case 13: 16810 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16811 return (0); 16812 /* TODO: delete entry defined by tce */ 16813 return (1); 16814 default: 16815 return (0); 16816 } 16817 default: 16818 return (1); 16819 } 16820 } 16821 16822 /* Translate TCP state to MIB2 TCP state. */ 16823 static int 16824 tcp_snmp_state(tcp_t *tcp) 16825 { 16826 if (tcp == NULL) 16827 return (0); 16828 16829 switch (tcp->tcp_state) { 16830 case TCPS_CLOSED: 16831 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16832 case TCPS_BOUND: 16833 return (MIB2_TCP_closed); 16834 case TCPS_LISTEN: 16835 return (MIB2_TCP_listen); 16836 case TCPS_SYN_SENT: 16837 return (MIB2_TCP_synSent); 16838 case TCPS_SYN_RCVD: 16839 return (MIB2_TCP_synReceived); 16840 case TCPS_ESTABLISHED: 16841 return (MIB2_TCP_established); 16842 case TCPS_CLOSE_WAIT: 16843 return (MIB2_TCP_closeWait); 16844 case TCPS_FIN_WAIT_1: 16845 return (MIB2_TCP_finWait1); 16846 case TCPS_CLOSING: 16847 return (MIB2_TCP_closing); 16848 case TCPS_LAST_ACK: 16849 return (MIB2_TCP_lastAck); 16850 case TCPS_FIN_WAIT_2: 16851 return (MIB2_TCP_finWait2); 16852 case TCPS_TIME_WAIT: 16853 return (MIB2_TCP_timeWait); 16854 default: 16855 return (0); 16856 } 16857 } 16858 16859 static char tcp_report_header[] = 16860 "TCP " MI_COL_HDRPAD_STR 16861 "zone dest snxt suna " 16862 "swnd rnxt rack rwnd rto mss w sw rw t " 16863 "recent [lport,fport] state"; 16864 16865 /* 16866 * TCP status report triggered via the Named Dispatch mechanism. 16867 */ 16868 /* ARGSUSED */ 16869 static void 16870 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16871 cred_t *cr) 16872 { 16873 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16874 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16875 char cflag; 16876 in6_addr_t v6dst; 16877 char buf[80]; 16878 uint_t print_len, buf_len; 16879 16880 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16881 if (buf_len <= 0) 16882 return; 16883 16884 if (hashval >= 0) 16885 (void) sprintf(hash, "%03d ", hashval); 16886 else 16887 hash[0] = '\0'; 16888 16889 /* 16890 * Note that we use the remote address in the tcp_b structure. 16891 * This means that it will print out the real destination address, 16892 * not the next hop's address if source routing is used. This 16893 * avoid the confusion on the output because user may not 16894 * know that source routing is used for a connection. 16895 */ 16896 if (tcp->tcp_ipversion == IPV4_VERSION) { 16897 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16898 } else { 16899 v6dst = tcp->tcp_remote_v6; 16900 } 16901 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16902 /* 16903 * the ispriv checks are so that normal users cannot determine 16904 * sequence number information using NDD. 16905 */ 16906 16907 if (TCP_IS_DETACHED(tcp)) 16908 cflag = '*'; 16909 else 16910 cflag = ' '; 16911 print_len = snprintf((char *)mp->b_wptr, buf_len, 16912 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16913 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16914 hash, 16915 (void *)tcp, 16916 tcp->tcp_connp->conn_zoneid, 16917 addrbuf, 16918 (ispriv) ? tcp->tcp_snxt : 0, 16919 (ispriv) ? tcp->tcp_suna : 0, 16920 tcp->tcp_swnd, 16921 (ispriv) ? tcp->tcp_rnxt : 0, 16922 (ispriv) ? tcp->tcp_rack : 0, 16923 tcp->tcp_rwnd, 16924 tcp->tcp_rto, 16925 tcp->tcp_mss, 16926 tcp->tcp_snd_ws_ok, 16927 tcp->tcp_snd_ws, 16928 tcp->tcp_rcv_ws, 16929 tcp->tcp_snd_ts_ok, 16930 tcp->tcp_ts_recent, 16931 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16932 if (print_len < buf_len) { 16933 ((mblk_t *)mp)->b_wptr += print_len; 16934 } else { 16935 ((mblk_t *)mp)->b_wptr += buf_len; 16936 } 16937 } 16938 16939 /* 16940 * TCP status report (for listeners only) triggered via the Named Dispatch 16941 * mechanism. 16942 */ 16943 /* ARGSUSED */ 16944 static void 16945 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16946 { 16947 char addrbuf[INET6_ADDRSTRLEN]; 16948 in6_addr_t v6dst; 16949 uint_t print_len, buf_len; 16950 16951 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16952 if (buf_len <= 0) 16953 return; 16954 16955 if (tcp->tcp_ipversion == IPV4_VERSION) { 16956 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16957 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16958 } else { 16959 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16960 addrbuf, sizeof (addrbuf)); 16961 } 16962 print_len = snprintf((char *)mp->b_wptr, buf_len, 16963 "%03d " 16964 MI_COL_PTRFMT_STR 16965 "%d %s %05u %08u %d/%d/%d%c\n", 16966 hashval, (void *)tcp, 16967 tcp->tcp_connp->conn_zoneid, 16968 addrbuf, 16969 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16970 tcp->tcp_conn_req_seqnum, 16971 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16972 tcp->tcp_conn_req_max, 16973 tcp->tcp_syn_defense ? '*' : ' '); 16974 if (print_len < buf_len) { 16975 ((mblk_t *)mp)->b_wptr += print_len; 16976 } else { 16977 ((mblk_t *)mp)->b_wptr += buf_len; 16978 } 16979 } 16980 16981 /* TCP status report triggered via the Named Dispatch mechanism. */ 16982 /* ARGSUSED */ 16983 static int 16984 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16985 { 16986 tcp_t *tcp; 16987 int i; 16988 conn_t *connp; 16989 connf_t *connfp; 16990 zoneid_t zoneid; 16991 tcp_stack_t *tcps; 16992 ip_stack_t *ipst; 16993 16994 zoneid = Q_TO_CONN(q)->conn_zoneid; 16995 tcps = Q_TO_TCP(q)->tcp_tcps; 16996 16997 /* 16998 * Because of the ndd constraint, at most we can have 64K buffer 16999 * to put in all TCP info. So to be more efficient, just 17000 * allocate a 64K buffer here, assuming we need that large buffer. 17001 * This may be a problem as any user can read tcp_status. Therefore 17002 * we limit the rate of doing this using tcp_ndd_get_info_interval. 17003 * This should be OK as normal users should not do this too often. 17004 */ 17005 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17006 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17007 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17008 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17009 return (0); 17010 } 17011 } 17012 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17013 /* The following may work even if we cannot get a large buf. */ 17014 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17015 return (0); 17016 } 17017 17018 (void) mi_mpprintf(mp, "%s", tcp_report_header); 17019 17020 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 17021 17022 ipst = tcps->tcps_netstack->netstack_ip; 17023 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 17024 17025 connp = NULL; 17026 17027 while ((connp = 17028 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17029 tcp = connp->conn_tcp; 17030 if (zoneid != GLOBAL_ZONEID && 17031 zoneid != connp->conn_zoneid) 17032 continue; 17033 tcp_report_item(mp->b_cont, tcp, -1, tcp, 17034 cr); 17035 } 17036 17037 } 17038 17039 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17040 return (0); 17041 } 17042 17043 /* TCP status report triggered via the Named Dispatch mechanism. */ 17044 /* ARGSUSED */ 17045 static int 17046 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17047 { 17048 tf_t *tbf; 17049 tcp_t *tcp; 17050 int i; 17051 zoneid_t zoneid; 17052 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 17053 17054 zoneid = Q_TO_CONN(q)->conn_zoneid; 17055 17056 /* Refer to comments in tcp_status_report(). */ 17057 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17058 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17059 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17060 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17061 return (0); 17062 } 17063 } 17064 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17065 /* The following may work even if we cannot get a large buf. */ 17066 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17067 return (0); 17068 } 17069 17070 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17071 17072 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 17073 tbf = &tcps->tcps_bind_fanout[i]; 17074 mutex_enter(&tbf->tf_lock); 17075 for (tcp = tbf->tf_tcp; tcp != NULL; 17076 tcp = tcp->tcp_bind_hash) { 17077 if (zoneid != GLOBAL_ZONEID && 17078 zoneid != tcp->tcp_connp->conn_zoneid) 17079 continue; 17080 CONN_INC_REF(tcp->tcp_connp); 17081 tcp_report_item(mp->b_cont, tcp, i, 17082 Q_TO_TCP(q), cr); 17083 CONN_DEC_REF(tcp->tcp_connp); 17084 } 17085 mutex_exit(&tbf->tf_lock); 17086 } 17087 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17088 return (0); 17089 } 17090 17091 /* TCP status report triggered via the Named Dispatch mechanism. */ 17092 /* ARGSUSED */ 17093 static int 17094 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17095 { 17096 connf_t *connfp; 17097 conn_t *connp; 17098 tcp_t *tcp; 17099 int i; 17100 zoneid_t zoneid; 17101 tcp_stack_t *tcps; 17102 ip_stack_t *ipst; 17103 17104 zoneid = Q_TO_CONN(q)->conn_zoneid; 17105 tcps = Q_TO_TCP(q)->tcp_tcps; 17106 17107 /* Refer to comments in tcp_status_report(). */ 17108 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17109 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17110 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17111 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17112 return (0); 17113 } 17114 } 17115 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17116 /* The following may work even if we cannot get a large buf. */ 17117 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17118 return (0); 17119 } 17120 17121 (void) mi_mpprintf(mp, 17122 " TCP " MI_COL_HDRPAD_STR 17123 "zone IP addr port seqnum backlog (q0/q/max)"); 17124 17125 ipst = tcps->tcps_netstack->netstack_ip; 17126 17127 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 17128 connfp = &ipst->ips_ipcl_bind_fanout[i]; 17129 connp = NULL; 17130 while ((connp = 17131 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17132 tcp = connp->conn_tcp; 17133 if (zoneid != GLOBAL_ZONEID && 17134 zoneid != connp->conn_zoneid) 17135 continue; 17136 tcp_report_listener(mp->b_cont, tcp, i); 17137 } 17138 } 17139 17140 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17141 return (0); 17142 } 17143 17144 /* TCP status report triggered via the Named Dispatch mechanism. */ 17145 /* ARGSUSED */ 17146 static int 17147 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17148 { 17149 connf_t *connfp; 17150 conn_t *connp; 17151 tcp_t *tcp; 17152 int i; 17153 zoneid_t zoneid; 17154 tcp_stack_t *tcps; 17155 ip_stack_t *ipst; 17156 17157 zoneid = Q_TO_CONN(q)->conn_zoneid; 17158 tcps = Q_TO_TCP(q)->tcp_tcps; 17159 ipst = tcps->tcps_netstack->netstack_ip; 17160 17161 /* Refer to comments in tcp_status_report(). */ 17162 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17163 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17164 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17165 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17166 return (0); 17167 } 17168 } 17169 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17170 /* The following may work even if we cannot get a large buf. */ 17171 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17172 return (0); 17173 } 17174 17175 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 17176 ipst->ips_ipcl_conn_fanout_size); 17177 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17178 17179 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 17180 connfp = &ipst->ips_ipcl_conn_fanout[i]; 17181 connp = NULL; 17182 while ((connp = 17183 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17184 tcp = connp->conn_tcp; 17185 if (zoneid != GLOBAL_ZONEID && 17186 zoneid != connp->conn_zoneid) 17187 continue; 17188 tcp_report_item(mp->b_cont, tcp, i, 17189 Q_TO_TCP(q), cr); 17190 } 17191 } 17192 17193 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17194 return (0); 17195 } 17196 17197 /* TCP status report triggered via the Named Dispatch mechanism. */ 17198 /* ARGSUSED */ 17199 static int 17200 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17201 { 17202 tf_t *tf; 17203 tcp_t *tcp; 17204 int i; 17205 zoneid_t zoneid; 17206 tcp_stack_t *tcps; 17207 17208 zoneid = Q_TO_CONN(q)->conn_zoneid; 17209 tcps = Q_TO_TCP(q)->tcp_tcps; 17210 17211 /* Refer to comments in tcp_status_report(). */ 17212 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17213 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17214 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17215 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17216 return (0); 17217 } 17218 } 17219 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17220 /* The following may work even if we cannot get a large buf. */ 17221 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17222 return (0); 17223 } 17224 17225 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17226 17227 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 17228 tf = &tcps->tcps_acceptor_fanout[i]; 17229 mutex_enter(&tf->tf_lock); 17230 for (tcp = tf->tf_tcp; tcp != NULL; 17231 tcp = tcp->tcp_acceptor_hash) { 17232 if (zoneid != GLOBAL_ZONEID && 17233 zoneid != tcp->tcp_connp->conn_zoneid) 17234 continue; 17235 tcp_report_item(mp->b_cont, tcp, i, 17236 Q_TO_TCP(q), cr); 17237 } 17238 mutex_exit(&tf->tf_lock); 17239 } 17240 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17241 return (0); 17242 } 17243 17244 /* 17245 * tcp_timer is the timer service routine. It handles the retransmission, 17246 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 17247 * from the state of the tcp instance what kind of action needs to be done 17248 * at the time it is called. 17249 */ 17250 static void 17251 tcp_timer(void *arg) 17252 { 17253 mblk_t *mp; 17254 clock_t first_threshold; 17255 clock_t second_threshold; 17256 clock_t ms; 17257 uint32_t mss; 17258 conn_t *connp = (conn_t *)arg; 17259 tcp_t *tcp = connp->conn_tcp; 17260 tcp_stack_t *tcps = tcp->tcp_tcps; 17261 17262 tcp->tcp_timer_tid = 0; 17263 17264 if (tcp->tcp_fused) 17265 return; 17266 17267 first_threshold = tcp->tcp_first_timer_threshold; 17268 second_threshold = tcp->tcp_second_timer_threshold; 17269 switch (tcp->tcp_state) { 17270 case TCPS_IDLE: 17271 case TCPS_BOUND: 17272 case TCPS_LISTEN: 17273 return; 17274 case TCPS_SYN_RCVD: { 17275 tcp_t *listener = tcp->tcp_listener; 17276 17277 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 17278 ASSERT(tcp->tcp_rq == listener->tcp_rq); 17279 /* it's our first timeout */ 17280 tcp->tcp_syn_rcvd_timeout = 1; 17281 mutex_enter(&listener->tcp_eager_lock); 17282 listener->tcp_syn_rcvd_timeout++; 17283 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 17284 /* 17285 * Make this eager available for drop if we 17286 * need to drop one to accomodate a new 17287 * incoming SYN request. 17288 */ 17289 MAKE_DROPPABLE(listener, tcp); 17290 } 17291 if (!listener->tcp_syn_defense && 17292 (listener->tcp_syn_rcvd_timeout > 17293 (tcps->tcps_conn_req_max_q0 >> 2)) && 17294 (tcps->tcps_conn_req_max_q0 > 200)) { 17295 /* We may be under attack. Put on a defense. */ 17296 listener->tcp_syn_defense = B_TRUE; 17297 cmn_err(CE_WARN, "High TCP connect timeout " 17298 "rate! System (port %d) may be under a " 17299 "SYN flood attack!", 17300 BE16_TO_U16(listener->tcp_tcph->th_lport)); 17301 17302 listener->tcp_ip_addr_cache = kmem_zalloc( 17303 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 17304 KM_NOSLEEP); 17305 } 17306 mutex_exit(&listener->tcp_eager_lock); 17307 } else if (listener != NULL) { 17308 mutex_enter(&listener->tcp_eager_lock); 17309 tcp->tcp_syn_rcvd_timeout++; 17310 if (tcp->tcp_syn_rcvd_timeout > 1 && 17311 !tcp->tcp_closemp_used) { 17312 /* 17313 * This is our second timeout. Put the tcp in 17314 * the list of droppable eagers to allow it to 17315 * be dropped, if needed. We don't check 17316 * whether tcp_dontdrop is set or not to 17317 * protect ourselve from a SYN attack where a 17318 * remote host can spoof itself as one of the 17319 * good IP source and continue to hold 17320 * resources too long. 17321 */ 17322 MAKE_DROPPABLE(listener, tcp); 17323 } 17324 mutex_exit(&listener->tcp_eager_lock); 17325 } 17326 } 17327 /* FALLTHRU */ 17328 case TCPS_SYN_SENT: 17329 first_threshold = tcp->tcp_first_ctimer_threshold; 17330 second_threshold = tcp->tcp_second_ctimer_threshold; 17331 break; 17332 case TCPS_ESTABLISHED: 17333 case TCPS_FIN_WAIT_1: 17334 case TCPS_CLOSING: 17335 case TCPS_CLOSE_WAIT: 17336 case TCPS_LAST_ACK: 17337 /* If we have data to rexmit */ 17338 if (tcp->tcp_suna != tcp->tcp_snxt) { 17339 clock_t time_to_wait; 17340 17341 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 17342 if (!tcp->tcp_xmit_head) 17343 break; 17344 time_to_wait = lbolt - 17345 (clock_t)tcp->tcp_xmit_head->b_prev; 17346 time_to_wait = tcp->tcp_rto - 17347 TICK_TO_MSEC(time_to_wait); 17348 /* 17349 * If the timer fires too early, 1 clock tick earlier, 17350 * restart the timer. 17351 */ 17352 if (time_to_wait > msec_per_tick) { 17353 TCP_STAT(tcps, tcp_timer_fire_early); 17354 TCP_TIMER_RESTART(tcp, time_to_wait); 17355 return; 17356 } 17357 /* 17358 * When we probe zero windows, we force the swnd open. 17359 * If our peer acks with a closed window swnd will be 17360 * set to zero by tcp_rput(). As long as we are 17361 * receiving acks tcp_rput will 17362 * reset 'tcp_ms_we_have_waited' so as not to trip the 17363 * first and second interval actions. NOTE: the timer 17364 * interval is allowed to continue its exponential 17365 * backoff. 17366 */ 17367 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17368 if (tcp->tcp_debug) { 17369 (void) strlog(TCP_MOD_ID, 0, 1, 17370 SL_TRACE, "tcp_timer: zero win"); 17371 } 17372 } else { 17373 /* 17374 * After retransmission, we need to do 17375 * slow start. Set the ssthresh to one 17376 * half of current effective window and 17377 * cwnd to one MSS. Also reset 17378 * tcp_cwnd_cnt. 17379 * 17380 * Note that if tcp_ssthresh is reduced because 17381 * of ECN, do not reduce it again unless it is 17382 * already one window of data away (tcp_cwr 17383 * should then be cleared) or this is a 17384 * timeout for a retransmitted segment. 17385 */ 17386 uint32_t npkt; 17387 17388 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17389 npkt = ((tcp->tcp_timer_backoff ? 17390 tcp->tcp_cwnd_ssthresh : 17391 tcp->tcp_snxt - 17392 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17393 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17394 tcp->tcp_mss; 17395 } 17396 tcp->tcp_cwnd = tcp->tcp_mss; 17397 tcp->tcp_cwnd_cnt = 0; 17398 if (tcp->tcp_ecn_ok) { 17399 tcp->tcp_cwr = B_TRUE; 17400 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17401 tcp->tcp_ecn_cwr_sent = B_FALSE; 17402 } 17403 } 17404 break; 17405 } 17406 /* 17407 * We have something to send yet we cannot send. The 17408 * reason can be: 17409 * 17410 * 1. Zero send window: we need to do zero window probe. 17411 * 2. Zero cwnd: because of ECN, we need to "clock out 17412 * segments. 17413 * 3. SWS avoidance: receiver may have shrunk window, 17414 * reset our knowledge. 17415 * 17416 * Note that condition 2 can happen with either 1 or 17417 * 3. But 1 and 3 are exclusive. 17418 */ 17419 if (tcp->tcp_unsent != 0) { 17420 if (tcp->tcp_cwnd == 0) { 17421 /* 17422 * Set tcp_cwnd to 1 MSS so that a 17423 * new segment can be sent out. We 17424 * are "clocking out" new data when 17425 * the network is really congested. 17426 */ 17427 ASSERT(tcp->tcp_ecn_ok); 17428 tcp->tcp_cwnd = tcp->tcp_mss; 17429 } 17430 if (tcp->tcp_swnd == 0) { 17431 /* Extend window for zero window probe */ 17432 tcp->tcp_swnd++; 17433 tcp->tcp_zero_win_probe = B_TRUE; 17434 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17435 } else { 17436 /* 17437 * Handle timeout from sender SWS avoidance. 17438 * Reset our knowledge of the max send window 17439 * since the receiver might have reduced its 17440 * receive buffer. Avoid setting tcp_max_swnd 17441 * to one since that will essentially disable 17442 * the SWS checks. 17443 * 17444 * Note that since we don't have a SWS 17445 * state variable, if the timeout is set 17446 * for ECN but not for SWS, this 17447 * code will also be executed. This is 17448 * fine as tcp_max_swnd is updated 17449 * constantly and it will not affect 17450 * anything. 17451 */ 17452 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17453 } 17454 tcp_wput_data(tcp, NULL, B_FALSE); 17455 return; 17456 } 17457 /* Is there a FIN that needs to be to re retransmitted? */ 17458 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17459 !tcp->tcp_fin_acked) 17460 break; 17461 /* Nothing to do, return without restarting timer. */ 17462 TCP_STAT(tcps, tcp_timer_fire_miss); 17463 return; 17464 case TCPS_FIN_WAIT_2: 17465 /* 17466 * User closed the TCP endpoint and peer ACK'ed our FIN. 17467 * We waited some time for for peer's FIN, but it hasn't 17468 * arrived. We flush the connection now to avoid 17469 * case where the peer has rebooted. 17470 */ 17471 if (TCP_IS_DETACHED(tcp)) { 17472 (void) tcp_clean_death(tcp, 0, 23); 17473 } else { 17474 TCP_TIMER_RESTART(tcp, 17475 tcps->tcps_fin_wait_2_flush_interval); 17476 } 17477 return; 17478 case TCPS_TIME_WAIT: 17479 (void) tcp_clean_death(tcp, 0, 24); 17480 return; 17481 default: 17482 if (tcp->tcp_debug) { 17483 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17484 "tcp_timer: strange state (%d) %s", 17485 tcp->tcp_state, tcp_display(tcp, NULL, 17486 DISP_PORT_ONLY)); 17487 } 17488 return; 17489 } 17490 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17491 /* 17492 * For zero window probe, we need to send indefinitely, 17493 * unless we have not heard from the other side for some 17494 * time... 17495 */ 17496 if ((tcp->tcp_zero_win_probe == 0) || 17497 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17498 second_threshold)) { 17499 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17500 /* 17501 * If TCP is in SYN_RCVD state, send back a 17502 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17503 * should be zero in TCPS_SYN_RCVD state. 17504 */ 17505 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17506 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17507 "in SYN_RCVD", 17508 tcp, tcp->tcp_snxt, 17509 tcp->tcp_rnxt, TH_RST | TH_ACK); 17510 } 17511 (void) tcp_clean_death(tcp, 17512 tcp->tcp_client_errno ? 17513 tcp->tcp_client_errno : ETIMEDOUT, 25); 17514 return; 17515 } else { 17516 /* 17517 * Set tcp_ms_we_have_waited to second_threshold 17518 * so that in next timeout, we will do the above 17519 * check (lbolt - tcp_last_recv_time). This is 17520 * also to avoid overflow. 17521 * 17522 * We don't need to decrement tcp_timer_backoff 17523 * to avoid overflow because it will be decremented 17524 * later if new timeout value is greater than 17525 * tcp_rexmit_interval_max. In the case when 17526 * tcp_rexmit_interval_max is greater than 17527 * second_threshold, it means that we will wait 17528 * longer than second_threshold to send the next 17529 * window probe. 17530 */ 17531 tcp->tcp_ms_we_have_waited = second_threshold; 17532 } 17533 } else if (ms > first_threshold) { 17534 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17535 tcp->tcp_xmit_head != NULL) { 17536 tcp->tcp_xmit_head = 17537 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17538 } 17539 /* 17540 * We have been retransmitting for too long... The RTT 17541 * we calculated is probably incorrect. Reinitialize it. 17542 * Need to compensate for 0 tcp_rtt_sa. Reset 17543 * tcp_rtt_update so that we won't accidentally cache a 17544 * bad value. But only do this if this is not a zero 17545 * window probe. 17546 */ 17547 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17548 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17549 (tcp->tcp_rtt_sa >> 5); 17550 tcp->tcp_rtt_sa = 0; 17551 tcp_ip_notify(tcp); 17552 tcp->tcp_rtt_update = 0; 17553 } 17554 } 17555 tcp->tcp_timer_backoff++; 17556 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17557 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17558 tcps->tcps_rexmit_interval_min) { 17559 /* 17560 * This means the original RTO is tcp_rexmit_interval_min. 17561 * So we will use tcp_rexmit_interval_min as the RTO value 17562 * and do the backoff. 17563 */ 17564 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17565 } else { 17566 ms <<= tcp->tcp_timer_backoff; 17567 } 17568 if (ms > tcps->tcps_rexmit_interval_max) { 17569 ms = tcps->tcps_rexmit_interval_max; 17570 /* 17571 * ms is at max, decrement tcp_timer_backoff to avoid 17572 * overflow. 17573 */ 17574 tcp->tcp_timer_backoff--; 17575 } 17576 tcp->tcp_ms_we_have_waited += ms; 17577 if (tcp->tcp_zero_win_probe == 0) { 17578 tcp->tcp_rto = ms; 17579 } 17580 TCP_TIMER_RESTART(tcp, ms); 17581 /* 17582 * This is after a timeout and tcp_rto is backed off. Set 17583 * tcp_set_timer to 1 so that next time RTO is updated, we will 17584 * restart the timer with a correct value. 17585 */ 17586 tcp->tcp_set_timer = 1; 17587 mss = tcp->tcp_snxt - tcp->tcp_suna; 17588 if (mss > tcp->tcp_mss) 17589 mss = tcp->tcp_mss; 17590 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17591 mss = tcp->tcp_swnd; 17592 17593 if ((mp = tcp->tcp_xmit_head) != NULL) 17594 mp->b_prev = (mblk_t *)lbolt; 17595 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17596 B_TRUE); 17597 17598 /* 17599 * When slow start after retransmission begins, start with 17600 * this seq no. tcp_rexmit_max marks the end of special slow 17601 * start phase. tcp_snd_burst controls how many segments 17602 * can be sent because of an ack. 17603 */ 17604 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17605 tcp->tcp_snd_burst = TCP_CWND_SS; 17606 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17607 (tcp->tcp_unsent == 0)) { 17608 tcp->tcp_rexmit_max = tcp->tcp_fss; 17609 } else { 17610 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17611 } 17612 tcp->tcp_rexmit = B_TRUE; 17613 tcp->tcp_dupack_cnt = 0; 17614 17615 /* 17616 * Remove all rexmit SACK blk to start from fresh. 17617 */ 17618 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17619 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17620 tcp->tcp_num_notsack_blk = 0; 17621 tcp->tcp_cnt_notsack_list = 0; 17622 } 17623 if (mp == NULL) { 17624 return; 17625 } 17626 /* Attach credentials to retransmitted initial SYNs. */ 17627 if (tcp->tcp_state == TCPS_SYN_SENT) { 17628 mblk_setcred(mp, tcp->tcp_cred); 17629 DB_CPID(mp) = tcp->tcp_cpid; 17630 } 17631 17632 tcp->tcp_csuna = tcp->tcp_snxt; 17633 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17634 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17635 tcp_send_data(tcp, tcp->tcp_wq, mp); 17636 17637 } 17638 17639 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17640 static void 17641 tcp_unbind(tcp_t *tcp, mblk_t *mp) 17642 { 17643 conn_t *connp; 17644 17645 switch (tcp->tcp_state) { 17646 case TCPS_BOUND: 17647 case TCPS_LISTEN: 17648 break; 17649 default: 17650 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 17651 return; 17652 } 17653 17654 /* 17655 * Need to clean up all the eagers since after the unbind, segments 17656 * will no longer be delivered to this listener stream. 17657 */ 17658 mutex_enter(&tcp->tcp_eager_lock); 17659 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17660 tcp_eager_cleanup(tcp, 0); 17661 } 17662 mutex_exit(&tcp->tcp_eager_lock); 17663 17664 if (tcp->tcp_ipversion == IPV4_VERSION) { 17665 tcp->tcp_ipha->ipha_src = 0; 17666 } else { 17667 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17668 } 17669 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17670 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17671 tcp_bind_hash_remove(tcp); 17672 tcp->tcp_state = TCPS_IDLE; 17673 tcp->tcp_mdt = B_FALSE; 17674 /* Send M_FLUSH according to TPI */ 17675 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17676 connp = tcp->tcp_connp; 17677 connp->conn_mdt_ok = B_FALSE; 17678 ipcl_hash_remove(connp); 17679 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17680 mp = mi_tpi_ok_ack_alloc(mp); 17681 putnext(tcp->tcp_rq, mp); 17682 } 17683 17684 /* 17685 * Don't let port fall into the privileged range. 17686 * Since the extra privileged ports can be arbitrary we also 17687 * ensure that we exclude those from consideration. 17688 * tcp_g_epriv_ports is not sorted thus we loop over it until 17689 * there are no changes. 17690 * 17691 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17692 * but instead the code relies on: 17693 * - the fact that the address of the array and its size never changes 17694 * - the atomic assignment of the elements of the array 17695 * 17696 * Returns 0 if there are no more ports available. 17697 * 17698 * TS note: skip multilevel ports. 17699 */ 17700 static in_port_t 17701 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17702 { 17703 int i; 17704 boolean_t restart = B_FALSE; 17705 tcp_stack_t *tcps = tcp->tcp_tcps; 17706 17707 if (random && tcp_random_anon_port != 0) { 17708 (void) random_get_pseudo_bytes((uint8_t *)&port, 17709 sizeof (in_port_t)); 17710 /* 17711 * Unless changed by a sys admin, the smallest anon port 17712 * is 32768 and the largest anon port is 65535. It is 17713 * very likely (50%) for the random port to be smaller 17714 * than the smallest anon port. When that happens, 17715 * add port % (anon port range) to the smallest anon 17716 * port to get the random port. It should fall into the 17717 * valid anon port range. 17718 */ 17719 if (port < tcps->tcps_smallest_anon_port) { 17720 port = tcps->tcps_smallest_anon_port + 17721 port % (tcps->tcps_largest_anon_port - 17722 tcps->tcps_smallest_anon_port); 17723 } 17724 } 17725 17726 retry: 17727 if (port < tcps->tcps_smallest_anon_port) 17728 port = (in_port_t)tcps->tcps_smallest_anon_port; 17729 17730 if (port > tcps->tcps_largest_anon_port) { 17731 if (restart) 17732 return (0); 17733 restart = B_TRUE; 17734 port = (in_port_t)tcps->tcps_smallest_anon_port; 17735 } 17736 17737 if (port < tcps->tcps_smallest_nonpriv_port) 17738 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17739 17740 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17741 if (port == tcps->tcps_g_epriv_ports[i]) { 17742 port++; 17743 /* 17744 * Make sure whether the port is in the 17745 * valid range. 17746 */ 17747 goto retry; 17748 } 17749 } 17750 if (is_system_labeled() && 17751 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17752 IPPROTO_TCP, B_TRUE)) != 0) { 17753 port = i; 17754 goto retry; 17755 } 17756 return (port); 17757 } 17758 17759 /* 17760 * Return the next anonymous port in the privileged port range for 17761 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17762 * downwards. This is the same behavior as documented in the userland 17763 * library call rresvport(3N). 17764 * 17765 * TS note: skip multilevel ports. 17766 */ 17767 static in_port_t 17768 tcp_get_next_priv_port(const tcp_t *tcp) 17769 { 17770 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17771 in_port_t nextport; 17772 boolean_t restart = B_FALSE; 17773 tcp_stack_t *tcps = tcp->tcp_tcps; 17774 retry: 17775 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17776 next_priv_port >= IPPORT_RESERVED) { 17777 next_priv_port = IPPORT_RESERVED - 1; 17778 if (restart) 17779 return (0); 17780 restart = B_TRUE; 17781 } 17782 if (is_system_labeled() && 17783 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17784 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17785 next_priv_port = nextport; 17786 goto retry; 17787 } 17788 return (next_priv_port--); 17789 } 17790 17791 /* The write side r/w procedure. */ 17792 17793 #if CCS_STATS 17794 struct { 17795 struct { 17796 int64_t count, bytes; 17797 } tot, hit; 17798 } wrw_stats; 17799 #endif 17800 17801 /* 17802 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17803 * messages. 17804 */ 17805 /* ARGSUSED */ 17806 static void 17807 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17808 { 17809 conn_t *connp = (conn_t *)arg; 17810 tcp_t *tcp = connp->conn_tcp; 17811 queue_t *q = tcp->tcp_wq; 17812 17813 ASSERT(DB_TYPE(mp) != M_IOCTL); 17814 /* 17815 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17816 * Once the close starts, streamhead and sockfs will not let any data 17817 * packets come down (close ensures that there are no threads using the 17818 * queue and no new threads will come down) but since qprocsoff() 17819 * hasn't happened yet, a M_FLUSH or some non data message might 17820 * get reflected back (in response to our own FLUSHRW) and get 17821 * processed after tcp_close() is done. The conn would still be valid 17822 * because a ref would have added but we need to check the state 17823 * before actually processing the packet. 17824 */ 17825 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17826 freemsg(mp); 17827 return; 17828 } 17829 17830 switch (DB_TYPE(mp)) { 17831 case M_IOCDATA: 17832 tcp_wput_iocdata(tcp, mp); 17833 break; 17834 case M_FLUSH: 17835 tcp_wput_flush(tcp, mp); 17836 break; 17837 default: 17838 CALL_IP_WPUT(connp, q, mp); 17839 break; 17840 } 17841 } 17842 17843 /* 17844 * The TCP fast path write put procedure. 17845 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17846 */ 17847 /* ARGSUSED */ 17848 void 17849 tcp_output(void *arg, mblk_t *mp, void *arg2) 17850 { 17851 int len; 17852 int hdrlen; 17853 int plen; 17854 mblk_t *mp1; 17855 uchar_t *rptr; 17856 uint32_t snxt; 17857 tcph_t *tcph; 17858 struct datab *db; 17859 uint32_t suna; 17860 uint32_t mss; 17861 ipaddr_t *dst; 17862 ipaddr_t *src; 17863 uint32_t sum; 17864 int usable; 17865 conn_t *connp = (conn_t *)arg; 17866 tcp_t *tcp = connp->conn_tcp; 17867 uint32_t msize; 17868 tcp_stack_t *tcps = tcp->tcp_tcps; 17869 17870 /* 17871 * Try and ASSERT the minimum possible references on the 17872 * conn early enough. Since we are executing on write side, 17873 * the connection is obviously not detached and that means 17874 * there is a ref each for TCP and IP. Since we are behind 17875 * the squeue, the minimum references needed are 3. If the 17876 * conn is in classifier hash list, there should be an 17877 * extra ref for that (we check both the possibilities). 17878 */ 17879 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17880 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17881 17882 ASSERT(DB_TYPE(mp) == M_DATA); 17883 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17884 17885 mutex_enter(&tcp->tcp_non_sq_lock); 17886 tcp->tcp_squeue_bytes -= msize; 17887 mutex_exit(&tcp->tcp_non_sq_lock); 17888 17889 /* Bypass tcp protocol for fused tcp loopback */ 17890 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17891 return; 17892 17893 mss = tcp->tcp_mss; 17894 if (tcp->tcp_xmit_zc_clean) 17895 mp = tcp_zcopy_backoff(tcp, mp, 0); 17896 17897 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17898 len = (int)(mp->b_wptr - mp->b_rptr); 17899 17900 /* 17901 * Criteria for fast path: 17902 * 17903 * 1. no unsent data 17904 * 2. single mblk in request 17905 * 3. connection established 17906 * 4. data in mblk 17907 * 5. len <= mss 17908 * 6. no tcp_valid bits 17909 */ 17910 if ((tcp->tcp_unsent != 0) || 17911 (tcp->tcp_cork) || 17912 (mp->b_cont != NULL) || 17913 (tcp->tcp_state != TCPS_ESTABLISHED) || 17914 (len == 0) || 17915 (len > mss) || 17916 (tcp->tcp_valid_bits != 0)) { 17917 tcp_wput_data(tcp, mp, B_FALSE); 17918 return; 17919 } 17920 17921 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17922 ASSERT(tcp->tcp_fin_sent == 0); 17923 17924 /* queue new packet onto retransmission queue */ 17925 if (tcp->tcp_xmit_head == NULL) { 17926 tcp->tcp_xmit_head = mp; 17927 } else { 17928 tcp->tcp_xmit_last->b_cont = mp; 17929 } 17930 tcp->tcp_xmit_last = mp; 17931 tcp->tcp_xmit_tail = mp; 17932 17933 /* find out how much we can send */ 17934 /* BEGIN CSTYLED */ 17935 /* 17936 * un-acked usable 17937 * |--------------|-----------------| 17938 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17939 */ 17940 /* END CSTYLED */ 17941 17942 /* start sending from tcp_snxt */ 17943 snxt = tcp->tcp_snxt; 17944 17945 /* 17946 * Check to see if this connection has been idled for some 17947 * time and no ACK is expected. If it is, we need to slow 17948 * start again to get back the connection's "self-clock" as 17949 * described in VJ's paper. 17950 * 17951 * Refer to the comment in tcp_mss_set() for the calculation 17952 * of tcp_cwnd after idle. 17953 */ 17954 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17955 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17956 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17957 } 17958 17959 usable = tcp->tcp_swnd; /* tcp window size */ 17960 if (usable > tcp->tcp_cwnd) 17961 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17962 usable -= snxt; /* subtract stuff already sent */ 17963 suna = tcp->tcp_suna; 17964 usable += suna; 17965 /* usable can be < 0 if the congestion window is smaller */ 17966 if (len > usable) { 17967 /* Can't send complete M_DATA in one shot */ 17968 goto slow; 17969 } 17970 17971 mutex_enter(&tcp->tcp_non_sq_lock); 17972 if (tcp->tcp_flow_stopped && 17973 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17974 tcp_clrqfull(tcp); 17975 } 17976 mutex_exit(&tcp->tcp_non_sq_lock); 17977 17978 /* 17979 * determine if anything to send (Nagle). 17980 * 17981 * 1. len < tcp_mss (i.e. small) 17982 * 2. unacknowledged data present 17983 * 3. len < nagle limit 17984 * 4. last packet sent < nagle limit (previous packet sent) 17985 */ 17986 if ((len < mss) && (snxt != suna) && 17987 (len < (int)tcp->tcp_naglim) && 17988 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17989 /* 17990 * This was the first unsent packet and normally 17991 * mss < xmit_hiwater so there is no need to worry 17992 * about flow control. The next packet will go 17993 * through the flow control check in tcp_wput_data(). 17994 */ 17995 /* leftover work from above */ 17996 tcp->tcp_unsent = len; 17997 tcp->tcp_xmit_tail_unsent = len; 17998 17999 return; 18000 } 18001 18002 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 18003 18004 if (snxt == suna) { 18005 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18006 } 18007 18008 /* we have always sent something */ 18009 tcp->tcp_rack_cnt = 0; 18010 18011 tcp->tcp_snxt = snxt + len; 18012 tcp->tcp_rack = tcp->tcp_rnxt; 18013 18014 if ((mp1 = dupb(mp)) == 0) 18015 goto no_memory; 18016 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 18017 mp->b_next = (mblk_t *)(uintptr_t)snxt; 18018 18019 /* adjust tcp header information */ 18020 tcph = tcp->tcp_tcph; 18021 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 18022 18023 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 18024 sum = (sum >> 16) + (sum & 0xFFFF); 18025 U16_TO_ABE16(sum, tcph->th_sum); 18026 18027 U32_TO_ABE32(snxt, tcph->th_seq); 18028 18029 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 18030 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 18031 BUMP_LOCAL(tcp->tcp_obsegs); 18032 18033 /* Update the latest receive window size in TCP header. */ 18034 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 18035 tcph->th_win); 18036 18037 tcp->tcp_last_sent_len = (ushort_t)len; 18038 18039 plen = len + tcp->tcp_hdr_len; 18040 18041 if (tcp->tcp_ipversion == IPV4_VERSION) { 18042 tcp->tcp_ipha->ipha_length = htons(plen); 18043 } else { 18044 tcp->tcp_ip6h->ip6_plen = htons(plen - 18045 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 18046 } 18047 18048 /* see if we need to allocate a mblk for the headers */ 18049 hdrlen = tcp->tcp_hdr_len; 18050 rptr = mp1->b_rptr - hdrlen; 18051 db = mp1->b_datap; 18052 if ((db->db_ref != 2) || rptr < db->db_base || 18053 (!OK_32PTR(rptr))) { 18054 /* NOTE: we assume allocb returns an OK_32PTR */ 18055 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 18056 tcps->tcps_wroff_xtra, BPRI_MED); 18057 if (!mp) { 18058 freemsg(mp1); 18059 goto no_memory; 18060 } 18061 mp->b_cont = mp1; 18062 mp1 = mp; 18063 /* Leave room for Link Level header */ 18064 /* hdrlen = tcp->tcp_hdr_len; */ 18065 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 18066 mp1->b_wptr = &rptr[hdrlen]; 18067 } 18068 mp1->b_rptr = rptr; 18069 18070 /* Fill in the timestamp option. */ 18071 if (tcp->tcp_snd_ts_ok) { 18072 U32_TO_BE32((uint32_t)lbolt, 18073 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 18074 U32_TO_BE32(tcp->tcp_ts_recent, 18075 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 18076 } else { 18077 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 18078 } 18079 18080 /* copy header into outgoing packet */ 18081 dst = (ipaddr_t *)rptr; 18082 src = (ipaddr_t *)tcp->tcp_iphc; 18083 dst[0] = src[0]; 18084 dst[1] = src[1]; 18085 dst[2] = src[2]; 18086 dst[3] = src[3]; 18087 dst[4] = src[4]; 18088 dst[5] = src[5]; 18089 dst[6] = src[6]; 18090 dst[7] = src[7]; 18091 dst[8] = src[8]; 18092 dst[9] = src[9]; 18093 if (hdrlen -= 40) { 18094 hdrlen >>= 2; 18095 dst += 10; 18096 src += 10; 18097 do { 18098 *dst++ = *src++; 18099 } while (--hdrlen); 18100 } 18101 18102 /* 18103 * Set the ECN info in the TCP header. Note that this 18104 * is not the template header. 18105 */ 18106 if (tcp->tcp_ecn_ok) { 18107 SET_ECT(tcp, rptr); 18108 18109 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 18110 if (tcp->tcp_ecn_echo_on) 18111 tcph->th_flags[0] |= TH_ECE; 18112 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 18113 tcph->th_flags[0] |= TH_CWR; 18114 tcp->tcp_ecn_cwr_sent = B_TRUE; 18115 } 18116 } 18117 18118 if (tcp->tcp_ip_forward_progress) { 18119 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 18120 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 18121 tcp->tcp_ip_forward_progress = B_FALSE; 18122 } 18123 tcp_send_data(tcp, tcp->tcp_wq, mp1); 18124 return; 18125 18126 /* 18127 * If we ran out of memory, we pretend to have sent the packet 18128 * and that it was lost on the wire. 18129 */ 18130 no_memory: 18131 return; 18132 18133 slow: 18134 /* leftover work from above */ 18135 tcp->tcp_unsent = len; 18136 tcp->tcp_xmit_tail_unsent = len; 18137 tcp_wput_data(tcp, NULL, B_FALSE); 18138 } 18139 18140 /* 18141 * The function called through squeue to get behind eager's perimeter to 18142 * finish the accept processing. 18143 */ 18144 /* ARGSUSED */ 18145 void 18146 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 18147 { 18148 conn_t *connp = (conn_t *)arg; 18149 tcp_t *tcp = connp->conn_tcp; 18150 queue_t *q = tcp->tcp_rq; 18151 mblk_t *mp1; 18152 mblk_t *stropt_mp = mp; 18153 struct stroptions *stropt; 18154 uint_t thwin; 18155 tcp_stack_t *tcps = tcp->tcp_tcps; 18156 18157 /* 18158 * Drop the eager's ref on the listener, that was placed when 18159 * this eager began life in tcp_conn_request. 18160 */ 18161 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 18162 18163 tcp->tcp_detached = B_FALSE; 18164 18165 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 18166 /* 18167 * Someone blewoff the eager before we could finish 18168 * the accept. 18169 * 18170 * The only reason eager exists it because we put in 18171 * a ref on it when conn ind went up. We need to send 18172 * a disconnect indication up while the last reference 18173 * on the eager will be dropped by the squeue when we 18174 * return. 18175 */ 18176 ASSERT(tcp->tcp_listener == NULL); 18177 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 18178 struct T_discon_ind *tdi; 18179 18180 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 18181 /* 18182 * Let us reuse the incoming mblk to avoid memory 18183 * allocation failure problems. We know that the 18184 * size of the incoming mblk i.e. stroptions is greater 18185 * than sizeof T_discon_ind. So the reallocb below 18186 * can't fail. 18187 */ 18188 freemsg(mp->b_cont); 18189 mp->b_cont = NULL; 18190 ASSERT(DB_REF(mp) == 1); 18191 mp = reallocb(mp, sizeof (struct T_discon_ind), 18192 B_FALSE); 18193 ASSERT(mp != NULL); 18194 DB_TYPE(mp) = M_PROTO; 18195 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 18196 tdi = (struct T_discon_ind *)mp->b_rptr; 18197 if (tcp->tcp_issocket) { 18198 tdi->DISCON_reason = ECONNREFUSED; 18199 tdi->SEQ_number = 0; 18200 } else { 18201 tdi->DISCON_reason = ENOPROTOOPT; 18202 tdi->SEQ_number = 18203 tcp->tcp_conn_req_seqnum; 18204 } 18205 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 18206 putnext(q, mp); 18207 } else { 18208 freemsg(mp); 18209 } 18210 if (tcp->tcp_hard_binding) { 18211 tcp->tcp_hard_binding = B_FALSE; 18212 tcp->tcp_hard_bound = B_TRUE; 18213 } 18214 return; 18215 } 18216 18217 mp1 = stropt_mp->b_cont; 18218 stropt_mp->b_cont = NULL; 18219 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 18220 stropt = (struct stroptions *)stropt_mp->b_rptr; 18221 18222 while (mp1 != NULL) { 18223 mp = mp1; 18224 mp1 = mp1->b_cont; 18225 mp->b_cont = NULL; 18226 tcp->tcp_drop_opt_ack_cnt++; 18227 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 18228 } 18229 mp = NULL; 18230 18231 /* 18232 * For a loopback connection with tcp_direct_sockfs on, note that 18233 * we don't have to protect tcp_rcv_list yet because synchronous 18234 * streams has not yet been enabled and tcp_fuse_rrw() cannot 18235 * possibly race with us. 18236 */ 18237 18238 /* 18239 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 18240 * properly. This is the first time we know of the acceptor' 18241 * queue. So we do it here. 18242 */ 18243 if (tcp->tcp_rcv_list == NULL) { 18244 /* 18245 * Recv queue is empty, tcp_rwnd should not have changed. 18246 * That means it should be equal to the listener's tcp_rwnd. 18247 */ 18248 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 18249 } else { 18250 #ifdef DEBUG 18251 uint_t cnt = 0; 18252 18253 mp1 = tcp->tcp_rcv_list; 18254 while ((mp = mp1) != NULL) { 18255 mp1 = mp->b_next; 18256 cnt += msgdsize(mp); 18257 } 18258 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 18259 #endif 18260 /* There is some data, add them back to get the max. */ 18261 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 18262 } 18263 /* 18264 * This is the first time we run on the correct 18265 * queue after tcp_accept. So fix all the q parameters 18266 * here. 18267 */ 18268 stropt->so_flags = SO_HIWAT | SO_MAXBLK | SO_WROFF; 18269 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 18270 18271 /* 18272 * Record the stream head's high water mark for this endpoint; 18273 * this is used for flow-control purposes. 18274 */ 18275 stropt->so_hiwat = tcp->tcp_fused ? 18276 tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat) : 18277 MAX(q->q_hiwat, tcps->tcps_sth_rcv_hiwat); 18278 18279 /* 18280 * Determine what write offset value to use depending on SACK and 18281 * whether the endpoint is fused or not. 18282 */ 18283 if (tcp->tcp_fused) { 18284 ASSERT(tcp->tcp_loopback); 18285 ASSERT(tcp->tcp_loopback_peer != NULL); 18286 /* 18287 * For fused tcp loopback, set the stream head's write 18288 * offset value to zero since we won't be needing any room 18289 * for TCP/IP headers. This would also improve performance 18290 * since it would reduce the amount of work done by kmem. 18291 * Non-fused tcp loopback case is handled separately below. 18292 */ 18293 stropt->so_wroff = 0; 18294 /* 18295 * Update the peer's transmit parameters according to 18296 * our recently calculated high water mark value. 18297 */ 18298 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 18299 } else if (tcp->tcp_snd_sack_ok) { 18300 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 18301 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 18302 } else { 18303 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 18304 tcps->tcps_wroff_xtra); 18305 } 18306 18307 /* 18308 * If this is endpoint is handling SSL, then reserve extra 18309 * offset and space at the end. 18310 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 18311 * overriding the previous setting. The extra cost of signing and 18312 * encrypting multiple MSS-size records (12 of them with Ethernet), 18313 * instead of a single contiguous one by the stream head 18314 * largely outweighs the statistical reduction of ACKs, when 18315 * applicable. The peer will also save on decryption and verification 18316 * costs. 18317 */ 18318 if (tcp->tcp_kssl_ctx != NULL) { 18319 stropt->so_wroff += SSL3_WROFFSET; 18320 18321 stropt->so_flags |= SO_TAIL; 18322 stropt->so_tail = SSL3_MAX_TAIL_LEN; 18323 18324 stropt->so_flags |= SO_COPYOPT; 18325 stropt->so_copyopt = ZCVMUNSAFE; 18326 18327 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 18328 } 18329 18330 /* Send the options up */ 18331 putnext(q, stropt_mp); 18332 18333 /* 18334 * Pass up any data and/or a fin that has been received. 18335 * 18336 * Adjust receive window in case it had decreased 18337 * (because there is data <=> tcp_rcv_list != NULL) 18338 * while the connection was detached. Note that 18339 * in case the eager was flow-controlled, w/o this 18340 * code, the rwnd may never open up again! 18341 */ 18342 if (tcp->tcp_rcv_list != NULL) { 18343 /* We drain directly in case of fused tcp loopback */ 18344 sodirect_t *sodp; 18345 18346 if (!tcp->tcp_fused && canputnext(q)) { 18347 tcp->tcp_rwnd = q->q_hiwat; 18348 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 18349 << tcp->tcp_rcv_ws; 18350 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 18351 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18352 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 18353 tcp_xmit_ctl(NULL, 18354 tcp, (tcp->tcp_swnd == 0) ? 18355 tcp->tcp_suna : tcp->tcp_snxt, 18356 tcp->tcp_rnxt, TH_ACK); 18357 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 18358 } 18359 18360 } 18361 18362 SOD_PTR_ENTER(tcp, sodp); 18363 if (sodp != NULL) { 18364 /* Sodirect, move from rcv_list */ 18365 ASSERT(!tcp->tcp_fused); 18366 while ((mp = tcp->tcp_rcv_list) != NULL) { 18367 tcp->tcp_rcv_list = mp->b_next; 18368 mp->b_next = NULL; 18369 (void) tcp_rcv_sod_enqueue(tcp, sodp, mp, 18370 msgdsize(mp)); 18371 } 18372 tcp->tcp_rcv_last_head = NULL; 18373 tcp->tcp_rcv_last_tail = NULL; 18374 tcp->tcp_rcv_cnt = 0; 18375 (void) tcp_rcv_sod_wakeup(tcp, sodp); 18376 /* sod_wakeup() did the mutex_exit() */ 18377 } else { 18378 /* Not sodirect, drain */ 18379 (void) tcp_rcv_drain(q, tcp); 18380 } 18381 18382 /* 18383 * For fused tcp loopback, back-enable peer endpoint 18384 * if it's currently flow-controlled. 18385 */ 18386 if (tcp->tcp_fused) { 18387 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18388 18389 ASSERT(peer_tcp != NULL); 18390 ASSERT(peer_tcp->tcp_fused); 18391 /* 18392 * In order to change the peer's tcp_flow_stopped, 18393 * we need to take locks for both end points. The 18394 * highest address is taken first. 18395 */ 18396 if (peer_tcp > tcp) { 18397 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18398 mutex_enter(&tcp->tcp_non_sq_lock); 18399 } else { 18400 mutex_enter(&tcp->tcp_non_sq_lock); 18401 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18402 } 18403 if (peer_tcp->tcp_flow_stopped) { 18404 tcp_clrqfull(peer_tcp); 18405 TCP_STAT(tcps, tcp_fusion_backenabled); 18406 } 18407 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18408 mutex_exit(&tcp->tcp_non_sq_lock); 18409 } 18410 } 18411 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18412 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18413 mp = tcp->tcp_ordrel_mp; 18414 tcp->tcp_ordrel_mp = NULL; 18415 tcp->tcp_ordrel_done = B_TRUE; 18416 putnext(q, mp); 18417 } 18418 if (tcp->tcp_hard_binding) { 18419 tcp->tcp_hard_binding = B_FALSE; 18420 tcp->tcp_hard_bound = B_TRUE; 18421 } 18422 18423 /* We can enable synchronous streams now */ 18424 if (tcp->tcp_fused) { 18425 tcp_fuse_syncstr_enable_pair(tcp); 18426 } 18427 18428 if (tcp->tcp_ka_enabled) { 18429 tcp->tcp_ka_last_intrvl = 0; 18430 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18431 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18432 } 18433 18434 /* 18435 * At this point, eager is fully established and will 18436 * have the following references - 18437 * 18438 * 2 references for connection to exist (1 for TCP and 1 for IP). 18439 * 1 reference for the squeue which will be dropped by the squeue as 18440 * soon as this function returns. 18441 * There will be 1 additonal reference for being in classifier 18442 * hash list provided something bad hasn't happened. 18443 */ 18444 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18445 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18446 } 18447 18448 /* 18449 * The function called through squeue to get behind listener's perimeter to 18450 * send a deffered conn_ind. 18451 */ 18452 /* ARGSUSED */ 18453 void 18454 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18455 { 18456 conn_t *connp = (conn_t *)arg; 18457 tcp_t *listener = connp->conn_tcp; 18458 18459 if (listener->tcp_state == TCPS_CLOSED || 18460 TCP_IS_DETACHED(listener)) { 18461 /* 18462 * If listener has closed, it would have caused a 18463 * a cleanup/blowoff to happen for the eager. 18464 */ 18465 tcp_t *tcp; 18466 struct T_conn_ind *conn_ind; 18467 18468 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18469 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18470 conn_ind->OPT_length); 18471 /* 18472 * We need to drop the ref on eager that was put 18473 * tcp_rput_data() before trying to send the conn_ind 18474 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18475 * and tcp_wput_accept() is sending this deferred conn_ind but 18476 * listener is closed so we drop the ref. 18477 */ 18478 CONN_DEC_REF(tcp->tcp_connp); 18479 freemsg(mp); 18480 return; 18481 } 18482 putnext(listener->tcp_rq, mp); 18483 } 18484 18485 18486 /* 18487 * This is the STREAMS entry point for T_CONN_RES coming down on 18488 * Acceptor STREAM when sockfs listener does accept processing. 18489 * Read the block comment on top of tcp_conn_request(). 18490 */ 18491 void 18492 tcp_wput_accept(queue_t *q, mblk_t *mp) 18493 { 18494 queue_t *rq = RD(q); 18495 struct T_conn_res *conn_res; 18496 tcp_t *eager; 18497 tcp_t *listener; 18498 struct T_ok_ack *ok; 18499 t_scalar_t PRIM_type; 18500 mblk_t *opt_mp; 18501 conn_t *econnp; 18502 18503 ASSERT(DB_TYPE(mp) == M_PROTO); 18504 18505 conn_res = (struct T_conn_res *)mp->b_rptr; 18506 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18507 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18508 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18509 if (mp != NULL) 18510 putnext(rq, mp); 18511 return; 18512 } 18513 switch (conn_res->PRIM_type) { 18514 case O_T_CONN_RES: 18515 case T_CONN_RES: 18516 /* 18517 * We pass up an err ack if allocb fails. This will 18518 * cause sockfs to issue a T_DISCON_REQ which will cause 18519 * tcp_eager_blowoff to be called. sockfs will then call 18520 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18521 * we need to do the allocb up here because we have to 18522 * make sure rq->q_qinfo->qi_qclose still points to the 18523 * correct function (tcpclose_accept) in case allocb 18524 * fails. 18525 */ 18526 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18527 if (opt_mp == NULL) { 18528 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18529 if (mp != NULL) 18530 putnext(rq, mp); 18531 return; 18532 } 18533 18534 bcopy(mp->b_rptr + conn_res->OPT_offset, 18535 &eager, conn_res->OPT_length); 18536 PRIM_type = conn_res->PRIM_type; 18537 mp->b_datap->db_type = M_PCPROTO; 18538 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18539 ok = (struct T_ok_ack *)mp->b_rptr; 18540 ok->PRIM_type = T_OK_ACK; 18541 ok->CORRECT_prim = PRIM_type; 18542 econnp = eager->tcp_connp; 18543 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 18544 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 18545 eager->tcp_rq = rq; 18546 eager->tcp_wq = q; 18547 rq->q_ptr = econnp; 18548 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 18549 q->q_ptr = econnp; 18550 q->q_qinfo = &tcp_winit; 18551 listener = eager->tcp_listener; 18552 eager->tcp_issocket = B_TRUE; 18553 18554 /* 18555 * TCP is _D_SODIRECT and sockfs is directly above so 18556 * save shared sodirect_t pointer (if any). 18557 * 18558 * If tcp_fused and sodirect enabled disable it. 18559 */ 18560 eager->tcp_sodirect = SOD_QTOSODP(eager->tcp_rq); 18561 if (eager->tcp_fused && eager->tcp_sodirect != NULL) { 18562 /* Fused, disable sodirect */ 18563 mutex_enter(eager->tcp_sodirect->sod_lockp); 18564 SOD_DISABLE(eager->tcp_sodirect); 18565 mutex_exit(eager->tcp_sodirect->sod_lockp); 18566 eager->tcp_sodirect = NULL; 18567 } 18568 18569 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18570 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18571 ASSERT(econnp->conn_netstack == 18572 listener->tcp_connp->conn_netstack); 18573 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18574 18575 /* Put the ref for IP */ 18576 CONN_INC_REF(econnp); 18577 18578 /* 18579 * We should have minimum of 3 references on the conn 18580 * at this point. One each for TCP and IP and one for 18581 * the T_conn_ind that was sent up when the 3-way handshake 18582 * completed. In the normal case we would also have another 18583 * reference (making a total of 4) for the conn being in the 18584 * classifier hash list. However the eager could have received 18585 * an RST subsequently and tcp_closei_local could have removed 18586 * the eager from the classifier hash list, hence we can't 18587 * assert that reference. 18588 */ 18589 ASSERT(econnp->conn_ref >= 3); 18590 18591 /* 18592 * Send the new local address also up to sockfs. There 18593 * should already be enough space in the mp that came 18594 * down from soaccept(). 18595 */ 18596 if (eager->tcp_family == AF_INET) { 18597 sin_t *sin; 18598 18599 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18600 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18601 sin = (sin_t *)mp->b_wptr; 18602 mp->b_wptr += sizeof (sin_t); 18603 sin->sin_family = AF_INET; 18604 sin->sin_port = eager->tcp_lport; 18605 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18606 } else { 18607 sin6_t *sin6; 18608 18609 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18610 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18611 sin6 = (sin6_t *)mp->b_wptr; 18612 mp->b_wptr += sizeof (sin6_t); 18613 sin6->sin6_family = AF_INET6; 18614 sin6->sin6_port = eager->tcp_lport; 18615 if (eager->tcp_ipversion == IPV4_VERSION) { 18616 sin6->sin6_flowinfo = 0; 18617 IN6_IPADDR_TO_V4MAPPED( 18618 eager->tcp_ipha->ipha_src, 18619 &sin6->sin6_addr); 18620 } else { 18621 ASSERT(eager->tcp_ip6h != NULL); 18622 sin6->sin6_flowinfo = 18623 eager->tcp_ip6h->ip6_vcf & 18624 ~IPV6_VERS_AND_FLOW_MASK; 18625 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18626 } 18627 sin6->sin6_scope_id = 0; 18628 sin6->__sin6_src_id = 0; 18629 } 18630 18631 putnext(rq, mp); 18632 18633 opt_mp->b_datap->db_type = M_SETOPTS; 18634 opt_mp->b_wptr += sizeof (struct stroptions); 18635 18636 /* 18637 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18638 * from listener to acceptor. The message is chained on the 18639 * bind_mp which tcp_rput_other will send down to IP. 18640 */ 18641 if (listener->tcp_bound_if != 0) { 18642 /* allocate optmgmt req */ 18643 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18644 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 18645 sizeof (int)); 18646 if (mp != NULL) 18647 linkb(opt_mp, mp); 18648 } 18649 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18650 uint_t on = 1; 18651 18652 /* allocate optmgmt req */ 18653 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18654 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 18655 if (mp != NULL) 18656 linkb(opt_mp, mp); 18657 } 18658 18659 18660 mutex_enter(&listener->tcp_eager_lock); 18661 18662 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18663 18664 tcp_t *tail; 18665 tcp_t *tcp; 18666 mblk_t *mp1; 18667 18668 tcp = listener->tcp_eager_prev_q0; 18669 /* 18670 * listener->tcp_eager_prev_q0 points to the TAIL of the 18671 * deferred T_conn_ind queue. We need to get to the head 18672 * of the queue in order to send up T_conn_ind the same 18673 * order as how the 3WHS is completed. 18674 */ 18675 while (tcp != listener) { 18676 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18677 !tcp->tcp_kssl_pending) 18678 break; 18679 else 18680 tcp = tcp->tcp_eager_prev_q0; 18681 } 18682 /* None of the pending eagers can be sent up now */ 18683 if (tcp == listener) 18684 goto no_more_eagers; 18685 18686 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18687 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18688 /* Move from q0 to q */ 18689 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18690 listener->tcp_conn_req_cnt_q0--; 18691 listener->tcp_conn_req_cnt_q++; 18692 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18693 tcp->tcp_eager_prev_q0; 18694 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18695 tcp->tcp_eager_next_q0; 18696 tcp->tcp_eager_prev_q0 = NULL; 18697 tcp->tcp_eager_next_q0 = NULL; 18698 tcp->tcp_conn_def_q0 = B_FALSE; 18699 18700 /* Make sure the tcp isn't in the list of droppables */ 18701 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18702 tcp->tcp_eager_prev_drop_q0 == NULL); 18703 18704 /* 18705 * Insert at end of the queue because sockfs sends 18706 * down T_CONN_RES in chronological order. Leaving 18707 * the older conn indications at front of the queue 18708 * helps reducing search time. 18709 */ 18710 tail = listener->tcp_eager_last_q; 18711 if (tail != NULL) { 18712 tail->tcp_eager_next_q = tcp; 18713 } else { 18714 listener->tcp_eager_next_q = tcp; 18715 } 18716 listener->tcp_eager_last_q = tcp; 18717 tcp->tcp_eager_next_q = NULL; 18718 18719 /* Need to get inside the listener perimeter */ 18720 CONN_INC_REF(listener->tcp_connp); 18721 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 18722 tcp_send_pending, listener->tcp_connp, 18723 SQTAG_TCP_SEND_PENDING); 18724 } 18725 no_more_eagers: 18726 tcp_eager_unlink(eager); 18727 mutex_exit(&listener->tcp_eager_lock); 18728 18729 /* 18730 * At this point, the eager is detached from the listener 18731 * but we still have an extra refs on eager (apart from the 18732 * usual tcp references). The ref was placed in tcp_rput_data 18733 * before sending the conn_ind in tcp_send_conn_ind. 18734 * The ref will be dropped in tcp_accept_finish(). 18735 */ 18736 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 18737 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 18738 return; 18739 default: 18740 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18741 if (mp != NULL) 18742 putnext(rq, mp); 18743 return; 18744 } 18745 } 18746 18747 static int 18748 tcp_getmyname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18749 { 18750 sin_t *sin = (sin_t *)sa; 18751 sin6_t *sin6 = (sin6_t *)sa; 18752 18753 switch (tcp->tcp_family) { 18754 case AF_INET: 18755 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18756 18757 if (*salenp < sizeof (sin_t)) 18758 return (EINVAL); 18759 18760 *sin = sin_null; 18761 sin->sin_family = AF_INET; 18762 sin->sin_port = tcp->tcp_lport; 18763 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 18764 break; 18765 18766 case AF_INET6: 18767 if (*salenp < sizeof (sin6_t)) 18768 return (EINVAL); 18769 18770 *sin6 = sin6_null; 18771 sin6->sin6_family = AF_INET6; 18772 sin6->sin6_port = tcp->tcp_lport; 18773 if (tcp->tcp_ipversion == IPV4_VERSION) { 18774 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 18775 &sin6->sin6_addr); 18776 } else { 18777 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 18778 } 18779 break; 18780 } 18781 18782 return (0); 18783 } 18784 18785 static int 18786 tcp_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18787 { 18788 sin_t *sin = (sin_t *)sa; 18789 sin6_t *sin6 = (sin6_t *)sa; 18790 18791 if (tcp->tcp_state < TCPS_SYN_RCVD) 18792 return (ENOTCONN); 18793 18794 switch (tcp->tcp_family) { 18795 case AF_INET: 18796 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18797 18798 if (*salenp < sizeof (sin_t)) 18799 return (EINVAL); 18800 18801 *sin = sin_null; 18802 sin->sin_family = AF_INET; 18803 sin->sin_port = tcp->tcp_fport; 18804 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 18805 sin->sin_addr.s_addr); 18806 break; 18807 18808 case AF_INET6: 18809 if (*salenp < sizeof (sin6_t)) 18810 return (EINVAL); 18811 18812 *sin6 = sin6_null; 18813 sin6->sin6_family = AF_INET6; 18814 sin6->sin6_port = tcp->tcp_fport; 18815 sin6->sin6_addr = tcp->tcp_remote_v6; 18816 if (tcp->tcp_ipversion == IPV6_VERSION) { 18817 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 18818 ~IPV6_VERS_AND_FLOW_MASK; 18819 } 18820 break; 18821 } 18822 18823 return (0); 18824 } 18825 18826 /* 18827 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 18828 */ 18829 static void 18830 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 18831 { 18832 void *data; 18833 mblk_t *datamp = mp->b_cont; 18834 tcp_t *tcp = Q_TO_TCP(q); 18835 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 18836 18837 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 18838 cmdp->cb_error = EPROTO; 18839 qreply(q, mp); 18840 return; 18841 } 18842 18843 data = datamp->b_rptr; 18844 18845 switch (cmdp->cb_cmd) { 18846 case TI_GETPEERNAME: 18847 cmdp->cb_error = tcp_getpeername(tcp, data, &cmdp->cb_len); 18848 break; 18849 case TI_GETMYNAME: 18850 cmdp->cb_error = tcp_getmyname(tcp, data, &cmdp->cb_len); 18851 break; 18852 default: 18853 cmdp->cb_error = EINVAL; 18854 break; 18855 } 18856 18857 qreply(q, mp); 18858 } 18859 18860 void 18861 tcp_wput(queue_t *q, mblk_t *mp) 18862 { 18863 conn_t *connp = Q_TO_CONN(q); 18864 tcp_t *tcp; 18865 void (*output_proc)(); 18866 t_scalar_t type; 18867 uchar_t *rptr; 18868 struct iocblk *iocp; 18869 uint32_t msize; 18870 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18871 18872 ASSERT(connp->conn_ref >= 2); 18873 18874 switch (DB_TYPE(mp)) { 18875 case M_DATA: 18876 tcp = connp->conn_tcp; 18877 ASSERT(tcp != NULL); 18878 18879 msize = msgdsize(mp); 18880 18881 mutex_enter(&tcp->tcp_non_sq_lock); 18882 tcp->tcp_squeue_bytes += msize; 18883 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18884 tcp_setqfull(tcp); 18885 } 18886 mutex_exit(&tcp->tcp_non_sq_lock); 18887 18888 CONN_INC_REF(connp); 18889 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18890 tcp_output, connp, SQTAG_TCP_OUTPUT); 18891 return; 18892 18893 case M_CMD: 18894 tcp_wput_cmdblk(q, mp); 18895 return; 18896 18897 case M_PROTO: 18898 case M_PCPROTO: 18899 /* 18900 * if it is a snmp message, don't get behind the squeue 18901 */ 18902 tcp = connp->conn_tcp; 18903 rptr = mp->b_rptr; 18904 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18905 type = ((union T_primitives *)rptr)->type; 18906 } else { 18907 if (tcp->tcp_debug) { 18908 (void) strlog(TCP_MOD_ID, 0, 1, 18909 SL_ERROR|SL_TRACE, 18910 "tcp_wput_proto, dropping one..."); 18911 } 18912 freemsg(mp); 18913 return; 18914 } 18915 if (type == T_SVR4_OPTMGMT_REQ) { 18916 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18917 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 18918 cr)) { 18919 /* 18920 * This was a SNMP request 18921 */ 18922 return; 18923 } else { 18924 output_proc = tcp_wput_proto; 18925 } 18926 } else { 18927 output_proc = tcp_wput_proto; 18928 } 18929 break; 18930 case M_IOCTL: 18931 /* 18932 * Most ioctls can be processed right away without going via 18933 * squeues - process them right here. Those that do require 18934 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18935 * are processed by tcp_wput_ioctl(). 18936 */ 18937 iocp = (struct iocblk *)mp->b_rptr; 18938 tcp = connp->conn_tcp; 18939 18940 switch (iocp->ioc_cmd) { 18941 case TCP_IOC_ABORT_CONN: 18942 tcp_ioctl_abort_conn(q, mp); 18943 return; 18944 case TI_GETPEERNAME: 18945 case TI_GETMYNAME: 18946 mi_copyin(q, mp, NULL, 18947 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18948 return; 18949 case ND_SET: 18950 /* nd_getset does the necessary checks */ 18951 case ND_GET: 18952 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18953 CALL_IP_WPUT(connp, q, mp); 18954 return; 18955 } 18956 qreply(q, mp); 18957 return; 18958 case TCP_IOC_DEFAULT_Q: 18959 /* 18960 * Wants to be the default wq. Check the credentials 18961 * first, the rest is executed via squeue. 18962 */ 18963 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18964 iocp->ioc_error = EPERM; 18965 iocp->ioc_count = 0; 18966 mp->b_datap->db_type = M_IOCACK; 18967 qreply(q, mp); 18968 return; 18969 } 18970 output_proc = tcp_wput_ioctl; 18971 break; 18972 default: 18973 output_proc = tcp_wput_ioctl; 18974 break; 18975 } 18976 break; 18977 default: 18978 output_proc = tcp_wput_nondata; 18979 break; 18980 } 18981 18982 CONN_INC_REF(connp); 18983 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18984 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 18985 } 18986 18987 /* 18988 * Initial STREAMS write side put() procedure for sockets. It tries to 18989 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18990 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18991 * are handled by tcp_wput() as usual. 18992 * 18993 * All further messages will also be handled by tcp_wput() because we cannot 18994 * be sure that the above short cut is safe later. 18995 */ 18996 static void 18997 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18998 { 18999 conn_t *connp = Q_TO_CONN(wq); 19000 tcp_t *tcp = connp->conn_tcp; 19001 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 19002 19003 ASSERT(wq->q_qinfo == &tcp_sock_winit); 19004 wq->q_qinfo = &tcp_winit; 19005 19006 ASSERT(IPCL_IS_TCP(connp)); 19007 ASSERT(TCP_IS_SOCKET(tcp)); 19008 19009 if (DB_TYPE(mp) == M_PCPROTO && 19010 MBLKL(mp) == sizeof (struct T_capability_req) && 19011 car->PRIM_type == T_CAPABILITY_REQ) { 19012 tcp_capability_req(tcp, mp); 19013 return; 19014 } 19015 19016 tcp_wput(wq, mp); 19017 } 19018 19019 static boolean_t 19020 tcp_zcopy_check(tcp_t *tcp) 19021 { 19022 conn_t *connp = tcp->tcp_connp; 19023 ire_t *ire; 19024 boolean_t zc_enabled = B_FALSE; 19025 tcp_stack_t *tcps = tcp->tcp_tcps; 19026 19027 if (do_tcpzcopy == 2) 19028 zc_enabled = B_TRUE; 19029 else if (tcp->tcp_ipversion == IPV4_VERSION && 19030 IPCL_IS_CONNECTED(connp) && 19031 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 19032 connp->conn_dontroute == 0 && 19033 !connp->conn_nexthop_set && 19034 connp->conn_outgoing_ill == NULL && 19035 connp->conn_nofailover_ill == NULL && 19036 do_tcpzcopy == 1) { 19037 /* 19038 * the checks above closely resemble the fast path checks 19039 * in tcp_send_data(). 19040 */ 19041 mutex_enter(&connp->conn_lock); 19042 ire = connp->conn_ire_cache; 19043 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19044 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19045 IRE_REFHOLD(ire); 19046 if (ire->ire_stq != NULL) { 19047 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 19048 19049 zc_enabled = ill && (ill->ill_capabilities & 19050 ILL_CAPAB_ZEROCOPY) && 19051 (ill->ill_zerocopy_capab-> 19052 ill_zerocopy_flags != 0); 19053 } 19054 IRE_REFRELE(ire); 19055 } 19056 mutex_exit(&connp->conn_lock); 19057 } 19058 tcp->tcp_snd_zcopy_on = zc_enabled; 19059 if (!TCP_IS_DETACHED(tcp)) { 19060 if (zc_enabled) { 19061 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 19062 TCP_STAT(tcps, tcp_zcopy_on); 19063 } else { 19064 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 19065 TCP_STAT(tcps, tcp_zcopy_off); 19066 } 19067 } 19068 return (zc_enabled); 19069 } 19070 19071 static mblk_t * 19072 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 19073 { 19074 tcp_stack_t *tcps = tcp->tcp_tcps; 19075 19076 if (do_tcpzcopy == 2) 19077 return (bp); 19078 else if (tcp->tcp_snd_zcopy_on) { 19079 tcp->tcp_snd_zcopy_on = B_FALSE; 19080 if (!TCP_IS_DETACHED(tcp)) { 19081 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 19082 TCP_STAT(tcps, tcp_zcopy_disable); 19083 } 19084 } 19085 return (tcp_zcopy_backoff(tcp, bp, 0)); 19086 } 19087 19088 /* 19089 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 19090 * the original desballoca'ed segmapped mblk. 19091 */ 19092 static mblk_t * 19093 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 19094 { 19095 mblk_t *head, *tail, *nbp; 19096 tcp_stack_t *tcps = tcp->tcp_tcps; 19097 19098 if (IS_VMLOANED_MBLK(bp)) { 19099 TCP_STAT(tcps, tcp_zcopy_backoff); 19100 if ((head = copyb(bp)) == NULL) { 19101 /* fail to backoff; leave it for the next backoff */ 19102 tcp->tcp_xmit_zc_clean = B_FALSE; 19103 return (bp); 19104 } 19105 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19106 if (fix_xmitlist) 19107 tcp_zcopy_notify(tcp); 19108 else 19109 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19110 } 19111 nbp = bp->b_cont; 19112 if (fix_xmitlist) { 19113 head->b_prev = bp->b_prev; 19114 head->b_next = bp->b_next; 19115 if (tcp->tcp_xmit_tail == bp) 19116 tcp->tcp_xmit_tail = head; 19117 } 19118 bp->b_next = NULL; 19119 bp->b_prev = NULL; 19120 freeb(bp); 19121 } else { 19122 head = bp; 19123 nbp = bp->b_cont; 19124 } 19125 tail = head; 19126 while (nbp) { 19127 if (IS_VMLOANED_MBLK(nbp)) { 19128 TCP_STAT(tcps, tcp_zcopy_backoff); 19129 if ((tail->b_cont = copyb(nbp)) == NULL) { 19130 tcp->tcp_xmit_zc_clean = B_FALSE; 19131 tail->b_cont = nbp; 19132 return (head); 19133 } 19134 tail = tail->b_cont; 19135 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19136 if (fix_xmitlist) 19137 tcp_zcopy_notify(tcp); 19138 else 19139 tail->b_datap->db_struioflag |= 19140 STRUIO_ZCNOTIFY; 19141 } 19142 bp = nbp; 19143 nbp = nbp->b_cont; 19144 if (fix_xmitlist) { 19145 tail->b_prev = bp->b_prev; 19146 tail->b_next = bp->b_next; 19147 if (tcp->tcp_xmit_tail == bp) 19148 tcp->tcp_xmit_tail = tail; 19149 } 19150 bp->b_next = NULL; 19151 bp->b_prev = NULL; 19152 freeb(bp); 19153 } else { 19154 tail->b_cont = nbp; 19155 tail = nbp; 19156 nbp = nbp->b_cont; 19157 } 19158 } 19159 if (fix_xmitlist) { 19160 tcp->tcp_xmit_last = tail; 19161 tcp->tcp_xmit_zc_clean = B_TRUE; 19162 } 19163 return (head); 19164 } 19165 19166 static void 19167 tcp_zcopy_notify(tcp_t *tcp) 19168 { 19169 struct stdata *stp; 19170 19171 if (tcp->tcp_detached) 19172 return; 19173 stp = STREAM(tcp->tcp_rq); 19174 mutex_enter(&stp->sd_lock); 19175 stp->sd_flag |= STZCNOTIFY; 19176 cv_broadcast(&stp->sd_zcopy_wait); 19177 mutex_exit(&stp->sd_lock); 19178 } 19179 19180 static boolean_t 19181 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 19182 { 19183 ire_t *ire; 19184 conn_t *connp = tcp->tcp_connp; 19185 tcp_stack_t *tcps = tcp->tcp_tcps; 19186 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19187 19188 mutex_enter(&connp->conn_lock); 19189 ire = connp->conn_ire_cache; 19190 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19191 19192 if ((ire != NULL) && 19193 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 19194 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 19195 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19196 IRE_REFHOLD(ire); 19197 mutex_exit(&connp->conn_lock); 19198 } else { 19199 boolean_t cached = B_FALSE; 19200 ts_label_t *tsl; 19201 19202 /* force a recheck later on */ 19203 tcp->tcp_ire_ill_check_done = B_FALSE; 19204 19205 TCP_DBGSTAT(tcps, tcp_ire_null1); 19206 connp->conn_ire_cache = NULL; 19207 mutex_exit(&connp->conn_lock); 19208 19209 if (ire != NULL) 19210 IRE_REFRELE_NOTR(ire); 19211 19212 tsl = crgetlabel(CONN_CRED(connp)); 19213 ire = (dst ? 19214 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 19215 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 19216 connp->conn_zoneid, tsl, ipst)); 19217 19218 if (ire == NULL) { 19219 TCP_STAT(tcps, tcp_ire_null); 19220 return (B_FALSE); 19221 } 19222 19223 IRE_REFHOLD_NOTR(ire); 19224 19225 mutex_enter(&connp->conn_lock); 19226 if (CONN_CACHE_IRE(connp)) { 19227 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 19228 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19229 TCP_CHECK_IREINFO(tcp, ire); 19230 connp->conn_ire_cache = ire; 19231 cached = B_TRUE; 19232 } 19233 rw_exit(&ire->ire_bucket->irb_lock); 19234 } 19235 mutex_exit(&connp->conn_lock); 19236 19237 /* 19238 * We can continue to use the ire but since it was 19239 * not cached, we should drop the extra reference. 19240 */ 19241 if (!cached) 19242 IRE_REFRELE_NOTR(ire); 19243 19244 /* 19245 * Rampart note: no need to select a new label here, since 19246 * labels are not allowed to change during the life of a TCP 19247 * connection. 19248 */ 19249 } 19250 19251 *irep = ire; 19252 19253 return (B_TRUE); 19254 } 19255 19256 /* 19257 * Called from tcp_send() or tcp_send_data() to find workable IRE. 19258 * 19259 * 0 = success; 19260 * 1 = failed to find ire and ill. 19261 */ 19262 static boolean_t 19263 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 19264 { 19265 ipha_t *ipha; 19266 ipaddr_t dst; 19267 ire_t *ire; 19268 ill_t *ill; 19269 conn_t *connp = tcp->tcp_connp; 19270 mblk_t *ire_fp_mp; 19271 tcp_stack_t *tcps = tcp->tcp_tcps; 19272 19273 if (mp != NULL) 19274 ipha = (ipha_t *)mp->b_rptr; 19275 else 19276 ipha = tcp->tcp_ipha; 19277 dst = ipha->ipha_dst; 19278 19279 if (!tcp_send_find_ire(tcp, &dst, &ire)) 19280 return (B_FALSE); 19281 19282 if ((ire->ire_flags & RTF_MULTIRT) || 19283 (ire->ire_stq == NULL) || 19284 (ire->ire_nce == NULL) || 19285 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 19286 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 19287 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 19288 TCP_STAT(tcps, tcp_ip_ire_send); 19289 IRE_REFRELE(ire); 19290 return (B_FALSE); 19291 } 19292 19293 ill = ire_to_ill(ire); 19294 if (connp->conn_outgoing_ill != NULL) { 19295 ill_t *conn_outgoing_ill = NULL; 19296 /* 19297 * Choose a good ill in the group to send the packets on. 19298 */ 19299 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 19300 ill = ire_to_ill(ire); 19301 } 19302 ASSERT(ill != NULL); 19303 19304 if (!tcp->tcp_ire_ill_check_done) { 19305 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19306 tcp->tcp_ire_ill_check_done = B_TRUE; 19307 } 19308 19309 *irep = ire; 19310 *illp = ill; 19311 19312 return (B_TRUE); 19313 } 19314 19315 static void 19316 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 19317 { 19318 ipha_t *ipha; 19319 ipaddr_t src; 19320 ipaddr_t dst; 19321 uint32_t cksum; 19322 ire_t *ire; 19323 uint16_t *up; 19324 ill_t *ill; 19325 conn_t *connp = tcp->tcp_connp; 19326 uint32_t hcksum_txflags = 0; 19327 mblk_t *ire_fp_mp; 19328 uint_t ire_fp_mp_len; 19329 tcp_stack_t *tcps = tcp->tcp_tcps; 19330 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19331 19332 ASSERT(DB_TYPE(mp) == M_DATA); 19333 19334 if (DB_CRED(mp) == NULL) 19335 mblk_setcred(mp, CONN_CRED(connp)); 19336 19337 ipha = (ipha_t *)mp->b_rptr; 19338 src = ipha->ipha_src; 19339 dst = ipha->ipha_dst; 19340 19341 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 19342 19343 /* 19344 * Drop off fast path for IPv6 and also if options are present or 19345 * we need to resolve a TS label. 19346 */ 19347 if (tcp->tcp_ipversion != IPV4_VERSION || 19348 !IPCL_IS_CONNECTED(connp) || 19349 !CONN_IS_LSO_MD_FASTPATH(connp) || 19350 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 19351 !connp->conn_ulp_labeled || 19352 ipha->ipha_ident == IP_HDR_INCLUDED || 19353 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 19354 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 19355 if (tcp->tcp_snd_zcopy_aware) 19356 mp = tcp_zcopy_disable(tcp, mp); 19357 TCP_STAT(tcps, tcp_ip_send); 19358 CALL_IP_WPUT(connp, q, mp); 19359 return; 19360 } 19361 19362 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 19363 if (tcp->tcp_snd_zcopy_aware) 19364 mp = tcp_zcopy_backoff(tcp, mp, 0); 19365 CALL_IP_WPUT(connp, q, mp); 19366 return; 19367 } 19368 ire_fp_mp = ire->ire_nce->nce_fp_mp; 19369 ire_fp_mp_len = MBLKL(ire_fp_mp); 19370 19371 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 19372 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 19373 #ifndef _BIG_ENDIAN 19374 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 19375 #endif 19376 19377 /* 19378 * Check to see if we need to re-enable LSO/MDT for this connection 19379 * because it was previously disabled due to changes in the ill; 19380 * note that by doing it here, this re-enabling only applies when 19381 * the packet is not dispatched through CALL_IP_WPUT(). 19382 * 19383 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 19384 * case, since that's how we ended up here. For IPv6, we do the 19385 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 19386 */ 19387 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 19388 /* 19389 * Restore LSO for this connection, so that next time around 19390 * it is eligible to go through tcp_lsosend() path again. 19391 */ 19392 TCP_STAT(tcps, tcp_lso_enabled); 19393 tcp->tcp_lso = B_TRUE; 19394 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 19395 "interface %s\n", (void *)connp, ill->ill_name)); 19396 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 19397 /* 19398 * Restore MDT for this connection, so that next time around 19399 * it is eligible to go through tcp_multisend() path again. 19400 */ 19401 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 19402 tcp->tcp_mdt = B_TRUE; 19403 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 19404 "interface %s\n", (void *)connp, ill->ill_name)); 19405 } 19406 19407 if (tcp->tcp_snd_zcopy_aware) { 19408 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 19409 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 19410 mp = tcp_zcopy_disable(tcp, mp); 19411 /* 19412 * we shouldn't need to reset ipha as the mp containing 19413 * ipha should never be a zero-copy mp. 19414 */ 19415 } 19416 19417 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 19418 ASSERT(ill->ill_hcksum_capab != NULL); 19419 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 19420 } 19421 19422 /* pseudo-header checksum (do it in parts for IP header checksum) */ 19423 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 19424 19425 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 19426 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 19427 19428 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 19429 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 19430 19431 /* Software checksum? */ 19432 if (DB_CKSUMFLAGS(mp) == 0) { 19433 TCP_STAT(tcps, tcp_out_sw_cksum); 19434 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 19435 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 19436 } 19437 19438 ipha->ipha_fragment_offset_and_flags |= 19439 (uint32_t)htons(ire->ire_frag_flag); 19440 19441 /* Calculate IP header checksum if hardware isn't capable */ 19442 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 19443 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 19444 ((uint16_t *)ipha)[4]); 19445 } 19446 19447 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 19448 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 19449 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 19450 19451 UPDATE_OB_PKT_COUNT(ire); 19452 ire->ire_last_used_time = lbolt; 19453 19454 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 19455 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 19456 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 19457 ntohs(ipha->ipha_length)); 19458 19459 if (ILL_DLS_CAPABLE(ill)) { 19460 /* 19461 * Send the packet directly to DLD, where it may be queued 19462 * depending on the availability of transmit resources at 19463 * the media layer. 19464 */ 19465 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 19466 } else { 19467 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 19468 DTRACE_PROBE4(ip4__physical__out__start, 19469 ill_t *, NULL, ill_t *, out_ill, 19470 ipha_t *, ipha, mblk_t *, mp); 19471 FW_HOOKS(ipst->ips_ip4_physical_out_event, 19472 ipst->ips_ipv4firewall_physical_out, 19473 NULL, out_ill, ipha, mp, mp, 0, ipst); 19474 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19475 19476 if (mp != NULL) { 19477 DTRACE_IP_FASTPATH(mp, ipha, out_ill, ipha, NULL); 19478 putnext(ire->ire_stq, mp); 19479 } 19480 } 19481 IRE_REFRELE(ire); 19482 } 19483 19484 /* 19485 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19486 * if the receiver shrinks the window, i.e. moves the right window to the 19487 * left, the we should not send new data, but should retransmit normally the 19488 * old unacked data between suna and suna + swnd. We might has sent data 19489 * that is now outside the new window, pretend that we didn't send it. 19490 */ 19491 static void 19492 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19493 { 19494 uint32_t snxt = tcp->tcp_snxt; 19495 mblk_t *xmit_tail; 19496 int32_t offset; 19497 19498 ASSERT(shrunk_count > 0); 19499 19500 /* Pretend we didn't send the data outside the window */ 19501 snxt -= shrunk_count; 19502 19503 /* Get the mblk and the offset in it per the shrunk window */ 19504 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19505 19506 ASSERT(xmit_tail != NULL); 19507 19508 /* Reset all the values per the now shrunk window */ 19509 tcp->tcp_snxt = snxt; 19510 tcp->tcp_xmit_tail = xmit_tail; 19511 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19512 offset; 19513 tcp->tcp_unsent += shrunk_count; 19514 19515 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19516 /* 19517 * Make sure the timer is running so that we will probe a zero 19518 * window. 19519 */ 19520 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19521 } 19522 19523 19524 /* 19525 * The TCP normal data output path. 19526 * NOTE: the logic of the fast path is duplicated from this function. 19527 */ 19528 static void 19529 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19530 { 19531 int len; 19532 mblk_t *local_time; 19533 mblk_t *mp1; 19534 uint32_t snxt; 19535 int tail_unsent; 19536 int tcpstate; 19537 int usable = 0; 19538 mblk_t *xmit_tail; 19539 queue_t *q = tcp->tcp_wq; 19540 int32_t mss; 19541 int32_t num_sack_blk = 0; 19542 int32_t tcp_hdr_len; 19543 int32_t tcp_tcp_hdr_len; 19544 int mdt_thres; 19545 int rc; 19546 tcp_stack_t *tcps = tcp->tcp_tcps; 19547 ip_stack_t *ipst; 19548 19549 tcpstate = tcp->tcp_state; 19550 if (mp == NULL) { 19551 /* 19552 * tcp_wput_data() with NULL mp should only be called when 19553 * there is unsent data. 19554 */ 19555 ASSERT(tcp->tcp_unsent > 0); 19556 /* Really tacky... but we need this for detached closes. */ 19557 len = tcp->tcp_unsent; 19558 goto data_null; 19559 } 19560 19561 #if CCS_STATS 19562 wrw_stats.tot.count++; 19563 wrw_stats.tot.bytes += msgdsize(mp); 19564 #endif 19565 ASSERT(mp->b_datap->db_type == M_DATA); 19566 /* 19567 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19568 * or before a connection attempt has begun. 19569 */ 19570 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19571 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19572 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19573 #ifdef DEBUG 19574 cmn_err(CE_WARN, 19575 "tcp_wput_data: data after ordrel, %s", 19576 tcp_display(tcp, NULL, 19577 DISP_ADDR_AND_PORT)); 19578 #else 19579 if (tcp->tcp_debug) { 19580 (void) strlog(TCP_MOD_ID, 0, 1, 19581 SL_TRACE|SL_ERROR, 19582 "tcp_wput_data: data after ordrel, %s\n", 19583 tcp_display(tcp, NULL, 19584 DISP_ADDR_AND_PORT)); 19585 } 19586 #endif /* DEBUG */ 19587 } 19588 if (tcp->tcp_snd_zcopy_aware && 19589 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19590 tcp_zcopy_notify(tcp); 19591 freemsg(mp); 19592 mutex_enter(&tcp->tcp_non_sq_lock); 19593 if (tcp->tcp_flow_stopped && 19594 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19595 tcp_clrqfull(tcp); 19596 } 19597 mutex_exit(&tcp->tcp_non_sq_lock); 19598 return; 19599 } 19600 19601 /* Strip empties */ 19602 for (;;) { 19603 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19604 (uintptr_t)INT_MAX); 19605 len = (int)(mp->b_wptr - mp->b_rptr); 19606 if (len > 0) 19607 break; 19608 mp1 = mp; 19609 mp = mp->b_cont; 19610 freeb(mp1); 19611 if (!mp) { 19612 return; 19613 } 19614 } 19615 19616 /* If we are the first on the list ... */ 19617 if (tcp->tcp_xmit_head == NULL) { 19618 tcp->tcp_xmit_head = mp; 19619 tcp->tcp_xmit_tail = mp; 19620 tcp->tcp_xmit_tail_unsent = len; 19621 } else { 19622 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19623 struct datab *dp; 19624 19625 mp1 = tcp->tcp_xmit_last; 19626 if (len < tcp_tx_pull_len && 19627 (dp = mp1->b_datap)->db_ref == 1 && 19628 dp->db_lim - mp1->b_wptr >= len) { 19629 ASSERT(len > 0); 19630 ASSERT(!mp1->b_cont); 19631 if (len == 1) { 19632 *mp1->b_wptr++ = *mp->b_rptr; 19633 } else { 19634 bcopy(mp->b_rptr, mp1->b_wptr, len); 19635 mp1->b_wptr += len; 19636 } 19637 if (mp1 == tcp->tcp_xmit_tail) 19638 tcp->tcp_xmit_tail_unsent += len; 19639 mp1->b_cont = mp->b_cont; 19640 if (tcp->tcp_snd_zcopy_aware && 19641 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19642 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19643 freeb(mp); 19644 mp = mp1; 19645 } else { 19646 tcp->tcp_xmit_last->b_cont = mp; 19647 } 19648 len += tcp->tcp_unsent; 19649 } 19650 19651 /* Tack on however many more positive length mblks we have */ 19652 if ((mp1 = mp->b_cont) != NULL) { 19653 do { 19654 int tlen; 19655 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19656 (uintptr_t)INT_MAX); 19657 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19658 if (tlen <= 0) { 19659 mp->b_cont = mp1->b_cont; 19660 freeb(mp1); 19661 } else { 19662 len += tlen; 19663 mp = mp1; 19664 } 19665 } while ((mp1 = mp->b_cont) != NULL); 19666 } 19667 tcp->tcp_xmit_last = mp; 19668 tcp->tcp_unsent = len; 19669 19670 if (urgent) 19671 usable = 1; 19672 19673 data_null: 19674 snxt = tcp->tcp_snxt; 19675 xmit_tail = tcp->tcp_xmit_tail; 19676 tail_unsent = tcp->tcp_xmit_tail_unsent; 19677 19678 /* 19679 * Note that tcp_mss has been adjusted to take into account the 19680 * timestamp option if applicable. Because SACK options do not 19681 * appear in every TCP segments and they are of variable lengths, 19682 * they cannot be included in tcp_mss. Thus we need to calculate 19683 * the actual segment length when we need to send a segment which 19684 * includes SACK options. 19685 */ 19686 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19687 int32_t opt_len; 19688 19689 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19690 tcp->tcp_num_sack_blk); 19691 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19692 2 + TCPOPT_HEADER_LEN; 19693 mss = tcp->tcp_mss - opt_len; 19694 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19695 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19696 } else { 19697 mss = tcp->tcp_mss; 19698 tcp_hdr_len = tcp->tcp_hdr_len; 19699 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19700 } 19701 19702 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19703 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19704 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19705 } 19706 if (tcpstate == TCPS_SYN_RCVD) { 19707 /* 19708 * The three-way connection establishment handshake is not 19709 * complete yet. We want to queue the data for transmission 19710 * after entering ESTABLISHED state (RFC793). A jump to 19711 * "done" label effectively leaves data on the queue. 19712 */ 19713 goto done; 19714 } else { 19715 int usable_r; 19716 19717 /* 19718 * In the special case when cwnd is zero, which can only 19719 * happen if the connection is ECN capable, return now. 19720 * New segments is sent using tcp_timer(). The timer 19721 * is set in tcp_rput_data(). 19722 */ 19723 if (tcp->tcp_cwnd == 0) { 19724 /* 19725 * Note that tcp_cwnd is 0 before 3-way handshake is 19726 * finished. 19727 */ 19728 ASSERT(tcp->tcp_ecn_ok || 19729 tcp->tcp_state < TCPS_ESTABLISHED); 19730 return; 19731 } 19732 19733 /* NOTE: trouble if xmitting while SYN not acked? */ 19734 usable_r = snxt - tcp->tcp_suna; 19735 usable_r = tcp->tcp_swnd - usable_r; 19736 19737 /* 19738 * Check if the receiver has shrunk the window. If 19739 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19740 * cannot be set as there is unsent data, so FIN cannot 19741 * be sent out. Otherwise, we need to take into account 19742 * of FIN as it consumes an "invisible" sequence number. 19743 */ 19744 ASSERT(tcp->tcp_fin_sent == 0); 19745 if (usable_r < 0) { 19746 /* 19747 * The receiver has shrunk the window and we have sent 19748 * -usable_r date beyond the window, re-adjust. 19749 * 19750 * If TCP window scaling is enabled, there can be 19751 * round down error as the advertised receive window 19752 * is actually right shifted n bits. This means that 19753 * the lower n bits info is wiped out. It will look 19754 * like the window is shrunk. Do a check here to 19755 * see if the shrunk amount is actually within the 19756 * error in window calculation. If it is, just 19757 * return. Note that this check is inside the 19758 * shrunk window check. This makes sure that even 19759 * though tcp_process_shrunk_swnd() is not called, 19760 * we will stop further processing. 19761 */ 19762 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19763 tcp_process_shrunk_swnd(tcp, -usable_r); 19764 } 19765 return; 19766 } 19767 19768 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19769 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19770 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19771 19772 /* usable = MIN(usable, unsent) */ 19773 if (usable_r > len) 19774 usable_r = len; 19775 19776 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19777 if (usable_r > 0) { 19778 usable = usable_r; 19779 } else { 19780 /* Bypass all other unnecessary processing. */ 19781 goto done; 19782 } 19783 } 19784 19785 local_time = (mblk_t *)lbolt; 19786 19787 /* 19788 * "Our" Nagle Algorithm. This is not the same as in the old 19789 * BSD. This is more in line with the true intent of Nagle. 19790 * 19791 * The conditions are: 19792 * 1. The amount of unsent data (or amount of data which can be 19793 * sent, whichever is smaller) is less than Nagle limit. 19794 * 2. The last sent size is also less than Nagle limit. 19795 * 3. There is unack'ed data. 19796 * 4. Urgent pointer is not set. Send urgent data ignoring the 19797 * Nagle algorithm. This reduces the probability that urgent 19798 * bytes get "merged" together. 19799 * 5. The app has not closed the connection. This eliminates the 19800 * wait time of the receiving side waiting for the last piece of 19801 * (small) data. 19802 * 19803 * If all are satisified, exit without sending anything. Note 19804 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19805 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19806 * 4095). 19807 */ 19808 if (usable < (int)tcp->tcp_naglim && 19809 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19810 snxt != tcp->tcp_suna && 19811 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19812 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19813 goto done; 19814 } 19815 19816 if (tcp->tcp_cork) { 19817 /* 19818 * if the tcp->tcp_cork option is set, then we have to force 19819 * TCP not to send partial segment (smaller than MSS bytes). 19820 * We are calculating the usable now based on full mss and 19821 * will save the rest of remaining data for later. 19822 */ 19823 if (usable < mss) 19824 goto done; 19825 usable = (usable / mss) * mss; 19826 } 19827 19828 /* Update the latest receive window size in TCP header. */ 19829 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19830 tcp->tcp_tcph->th_win); 19831 19832 /* 19833 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19834 * 19835 * 1. Simple TCP/IP{v4,v6} (no options). 19836 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19837 * 3. If the TCP connection is in ESTABLISHED state. 19838 * 4. The TCP is not detached. 19839 * 19840 * If any of the above conditions have changed during the 19841 * connection, stop using LSO/MDT and restore the stream head 19842 * parameters accordingly. 19843 */ 19844 ipst = tcps->tcps_netstack->netstack_ip; 19845 19846 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19847 ((tcp->tcp_ipversion == IPV4_VERSION && 19848 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19849 (tcp->tcp_ipversion == IPV6_VERSION && 19850 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19851 tcp->tcp_state != TCPS_ESTABLISHED || 19852 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19853 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19854 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19855 if (tcp->tcp_lso) { 19856 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19857 tcp->tcp_lso = B_FALSE; 19858 } else { 19859 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19860 tcp->tcp_mdt = B_FALSE; 19861 } 19862 19863 /* Anything other than detached is considered pathological */ 19864 if (!TCP_IS_DETACHED(tcp)) { 19865 if (tcp->tcp_lso) 19866 TCP_STAT(tcps, tcp_lso_disabled); 19867 else 19868 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19869 (void) tcp_maxpsz_set(tcp, B_TRUE); 19870 } 19871 } 19872 19873 /* Use MDT if sendable amount is greater than the threshold */ 19874 if (tcp->tcp_mdt && 19875 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19876 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19877 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19878 (tcp->tcp_valid_bits == 0 || 19879 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19880 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19881 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19882 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19883 local_time, mdt_thres); 19884 } else { 19885 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19886 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19887 local_time, INT_MAX); 19888 } 19889 19890 /* Pretend that all we were trying to send really got sent */ 19891 if (rc < 0 && tail_unsent < 0) { 19892 do { 19893 xmit_tail = xmit_tail->b_cont; 19894 xmit_tail->b_prev = local_time; 19895 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19896 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19897 tail_unsent += (int)(xmit_tail->b_wptr - 19898 xmit_tail->b_rptr); 19899 } while (tail_unsent < 0); 19900 } 19901 done:; 19902 tcp->tcp_xmit_tail = xmit_tail; 19903 tcp->tcp_xmit_tail_unsent = tail_unsent; 19904 len = tcp->tcp_snxt - snxt; 19905 if (len) { 19906 /* 19907 * If new data was sent, need to update the notsack 19908 * list, which is, afterall, data blocks that have 19909 * not been sack'ed by the receiver. New data is 19910 * not sack'ed. 19911 */ 19912 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19913 /* len is a negative value. */ 19914 tcp->tcp_pipe -= len; 19915 tcp_notsack_update(&(tcp->tcp_notsack_list), 19916 tcp->tcp_snxt, snxt, 19917 &(tcp->tcp_num_notsack_blk), 19918 &(tcp->tcp_cnt_notsack_list)); 19919 } 19920 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19921 tcp->tcp_rack = tcp->tcp_rnxt; 19922 tcp->tcp_rack_cnt = 0; 19923 if ((snxt + len) == tcp->tcp_suna) { 19924 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19925 } 19926 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19927 /* 19928 * Didn't send anything. Make sure the timer is running 19929 * so that we will probe a zero window. 19930 */ 19931 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19932 } 19933 /* Note that len is the amount we just sent but with a negative sign */ 19934 tcp->tcp_unsent += len; 19935 mutex_enter(&tcp->tcp_non_sq_lock); 19936 if (tcp->tcp_flow_stopped) { 19937 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19938 tcp_clrqfull(tcp); 19939 } 19940 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19941 tcp_setqfull(tcp); 19942 } 19943 mutex_exit(&tcp->tcp_non_sq_lock); 19944 } 19945 19946 /* 19947 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19948 * outgoing TCP header with the template header, as well as other 19949 * options such as time-stamp, ECN and/or SACK. 19950 */ 19951 static void 19952 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19953 { 19954 tcph_t *tcp_tmpl, *tcp_h; 19955 uint32_t *dst, *src; 19956 int hdrlen; 19957 19958 ASSERT(OK_32PTR(rptr)); 19959 19960 /* Template header */ 19961 tcp_tmpl = tcp->tcp_tcph; 19962 19963 /* Header of outgoing packet */ 19964 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19965 19966 /* dst and src are opaque 32-bit fields, used for copying */ 19967 dst = (uint32_t *)rptr; 19968 src = (uint32_t *)tcp->tcp_iphc; 19969 hdrlen = tcp->tcp_hdr_len; 19970 19971 /* Fill time-stamp option if needed */ 19972 if (tcp->tcp_snd_ts_ok) { 19973 U32_TO_BE32((uint32_t)now, 19974 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19975 U32_TO_BE32(tcp->tcp_ts_recent, 19976 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19977 } else { 19978 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19979 } 19980 19981 /* 19982 * Copy the template header; is this really more efficient than 19983 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19984 * but perhaps not for other scenarios. 19985 */ 19986 dst[0] = src[0]; 19987 dst[1] = src[1]; 19988 dst[2] = src[2]; 19989 dst[3] = src[3]; 19990 dst[4] = src[4]; 19991 dst[5] = src[5]; 19992 dst[6] = src[6]; 19993 dst[7] = src[7]; 19994 dst[8] = src[8]; 19995 dst[9] = src[9]; 19996 if (hdrlen -= 40) { 19997 hdrlen >>= 2; 19998 dst += 10; 19999 src += 10; 20000 do { 20001 *dst++ = *src++; 20002 } while (--hdrlen); 20003 } 20004 20005 /* 20006 * Set the ECN info in the TCP header if it is not a zero 20007 * window probe. Zero window probe is only sent in 20008 * tcp_wput_data() and tcp_timer(). 20009 */ 20010 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 20011 SET_ECT(tcp, rptr); 20012 20013 if (tcp->tcp_ecn_echo_on) 20014 tcp_h->th_flags[0] |= TH_ECE; 20015 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 20016 tcp_h->th_flags[0] |= TH_CWR; 20017 tcp->tcp_ecn_cwr_sent = B_TRUE; 20018 } 20019 } 20020 20021 /* Fill in SACK options */ 20022 if (num_sack_blk > 0) { 20023 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 20024 sack_blk_t *tmp; 20025 int32_t i; 20026 20027 wptr[0] = TCPOPT_NOP; 20028 wptr[1] = TCPOPT_NOP; 20029 wptr[2] = TCPOPT_SACK; 20030 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 20031 sizeof (sack_blk_t); 20032 wptr += TCPOPT_REAL_SACK_LEN; 20033 20034 tmp = tcp->tcp_sack_list; 20035 for (i = 0; i < num_sack_blk; i++) { 20036 U32_TO_BE32(tmp[i].begin, wptr); 20037 wptr += sizeof (tcp_seq); 20038 U32_TO_BE32(tmp[i].end, wptr); 20039 wptr += sizeof (tcp_seq); 20040 } 20041 tcp_h->th_offset_and_rsrvd[0] += 20042 ((num_sack_blk * 2 + 1) << 4); 20043 } 20044 } 20045 20046 /* 20047 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 20048 * the destination address and SAP attribute, and if necessary, the 20049 * hardware checksum offload attribute to a Multidata message. 20050 */ 20051 static int 20052 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 20053 const uint32_t start, const uint32_t stuff, const uint32_t end, 20054 const uint32_t flags, tcp_stack_t *tcps) 20055 { 20056 /* Add global destination address & SAP attribute */ 20057 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 20058 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 20059 "destination address+SAP\n")); 20060 20061 if (dlmp != NULL) 20062 TCP_STAT(tcps, tcp_mdt_allocfail); 20063 return (-1); 20064 } 20065 20066 /* Add global hwcksum attribute */ 20067 if (hwcksum && 20068 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 20069 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 20070 "checksum attribute\n")); 20071 20072 TCP_STAT(tcps, tcp_mdt_allocfail); 20073 return (-1); 20074 } 20075 20076 return (0); 20077 } 20078 20079 /* 20080 * Smaller and private version of pdescinfo_t used specifically for TCP, 20081 * which allows for only two payload spans per packet. 20082 */ 20083 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 20084 20085 /* 20086 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 20087 * scheme, and returns one the following: 20088 * 20089 * -1 = failed allocation. 20090 * 0 = success; burst count reached, or usable send window is too small, 20091 * and that we'd rather wait until later before sending again. 20092 */ 20093 static int 20094 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20095 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20096 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20097 const int mdt_thres) 20098 { 20099 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 20100 multidata_t *mmd; 20101 uint_t obsegs, obbytes, hdr_frag_sz; 20102 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 20103 int num_burst_seg, max_pld; 20104 pdesc_t *pkt; 20105 tcp_pdescinfo_t tcp_pkt_info; 20106 pdescinfo_t *pkt_info; 20107 int pbuf_idx, pbuf_idx_nxt; 20108 int seg_len, len, spill, af; 20109 boolean_t add_buffer, zcopy, clusterwide; 20110 boolean_t rconfirm = B_FALSE; 20111 boolean_t done = B_FALSE; 20112 uint32_t cksum; 20113 uint32_t hwcksum_flags; 20114 ire_t *ire = NULL; 20115 ill_t *ill; 20116 ipha_t *ipha; 20117 ip6_t *ip6h; 20118 ipaddr_t src, dst; 20119 ill_zerocopy_capab_t *zc_cap = NULL; 20120 uint16_t *up; 20121 int err; 20122 conn_t *connp; 20123 tcp_stack_t *tcps = tcp->tcp_tcps; 20124 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20125 int usable_mmd, tail_unsent_mmd; 20126 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 20127 mblk_t *xmit_tail_mmd; 20128 20129 #ifdef _BIG_ENDIAN 20130 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 20131 #else 20132 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 20133 #endif 20134 20135 #define PREP_NEW_MULTIDATA() { \ 20136 mmd = NULL; \ 20137 md_mp = md_hbuf = NULL; \ 20138 cur_hdr_off = 0; \ 20139 max_pld = tcp->tcp_mdt_max_pld; \ 20140 pbuf_idx = pbuf_idx_nxt = -1; \ 20141 add_buffer = B_TRUE; \ 20142 zcopy = B_FALSE; \ 20143 } 20144 20145 #define PREP_NEW_PBUF() { \ 20146 md_pbuf = md_pbuf_nxt = NULL; \ 20147 pbuf_idx = pbuf_idx_nxt = -1; \ 20148 cur_pld_off = 0; \ 20149 first_snxt = *snxt; \ 20150 ASSERT(*tail_unsent > 0); \ 20151 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 20152 } 20153 20154 ASSERT(mdt_thres >= mss); 20155 ASSERT(*usable > 0 && *usable > mdt_thres); 20156 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20157 ASSERT(!TCP_IS_DETACHED(tcp)); 20158 ASSERT(tcp->tcp_valid_bits == 0 || 20159 tcp->tcp_valid_bits == TCP_FSS_VALID); 20160 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 20161 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 20162 (tcp->tcp_ipversion == IPV6_VERSION && 20163 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 20164 20165 connp = tcp->tcp_connp; 20166 ASSERT(connp != NULL); 20167 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 20168 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 20169 20170 usable_mmd = tail_unsent_mmd = 0; 20171 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 20172 xmit_tail_mmd = NULL; 20173 /* 20174 * Note that tcp will only declare at most 2 payload spans per 20175 * packet, which is much lower than the maximum allowable number 20176 * of packet spans per Multidata. For this reason, we use the 20177 * privately declared and smaller descriptor info structure, in 20178 * order to save some stack space. 20179 */ 20180 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 20181 20182 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 20183 if (af == AF_INET) { 20184 dst = tcp->tcp_ipha->ipha_dst; 20185 src = tcp->tcp_ipha->ipha_src; 20186 ASSERT(!CLASSD(dst)); 20187 } 20188 ASSERT(af == AF_INET || 20189 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 20190 20191 obsegs = obbytes = 0; 20192 num_burst_seg = tcp->tcp_snd_burst; 20193 md_mp_head = NULL; 20194 PREP_NEW_MULTIDATA(); 20195 20196 /* 20197 * Before we go on further, make sure there is an IRE that we can 20198 * use, and that the ILL supports MDT. Otherwise, there's no point 20199 * in proceeding any further, and we should just hand everything 20200 * off to the legacy path. 20201 */ 20202 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 20203 goto legacy_send_no_md; 20204 20205 ASSERT(ire != NULL); 20206 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 20207 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 20208 ASSERT(af == AF_INET || ire->ire_nce != NULL); 20209 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 20210 /* 20211 * If we do support loopback for MDT (which requires modifications 20212 * to the receiving paths), the following assertions should go away, 20213 * and we would be sending the Multidata to loopback conn later on. 20214 */ 20215 ASSERT(!IRE_IS_LOCAL(ire)); 20216 ASSERT(ire->ire_stq != NULL); 20217 20218 ill = ire_to_ill(ire); 20219 ASSERT(ill != NULL); 20220 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 20221 20222 if (!tcp->tcp_ire_ill_check_done) { 20223 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 20224 tcp->tcp_ire_ill_check_done = B_TRUE; 20225 } 20226 20227 /* 20228 * If the underlying interface conditions have changed, or if the 20229 * new interface does not support MDT, go back to legacy path. 20230 */ 20231 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 20232 /* don't go through this path anymore for this connection */ 20233 TCP_STAT(tcps, tcp_mdt_conn_halted2); 20234 tcp->tcp_mdt = B_FALSE; 20235 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 20236 "interface %s\n", (void *)connp, ill->ill_name)); 20237 /* IRE will be released prior to returning */ 20238 goto legacy_send_no_md; 20239 } 20240 20241 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 20242 zc_cap = ill->ill_zerocopy_capab; 20243 20244 /* 20245 * Check if we can take tcp fast-path. Note that "incomplete" 20246 * ire's (where the link-layer for next hop is not resolved 20247 * or where the fast-path header in nce_fp_mp is not available 20248 * yet) are sent down the legacy (slow) path. 20249 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 20250 */ 20251 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 20252 /* IRE will be released prior to returning */ 20253 goto legacy_send_no_md; 20254 } 20255 20256 /* go to legacy path if interface doesn't support zerocopy */ 20257 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 20258 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 20259 /* IRE will be released prior to returning */ 20260 goto legacy_send_no_md; 20261 } 20262 20263 /* does the interface support hardware checksum offload? */ 20264 hwcksum_flags = 0; 20265 if (ILL_HCKSUM_CAPABLE(ill) && 20266 (ill->ill_hcksum_capab->ill_hcksum_txflags & 20267 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 20268 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 20269 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20270 HCKSUM_IPHDRCKSUM) 20271 hwcksum_flags = HCK_IPV4_HDRCKSUM; 20272 20273 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20274 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 20275 hwcksum_flags |= HCK_FULLCKSUM; 20276 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20277 HCKSUM_INET_PARTIAL) 20278 hwcksum_flags |= HCK_PARTIALCKSUM; 20279 } 20280 20281 /* 20282 * Each header fragment consists of the leading extra space, 20283 * followed by the TCP/IP header, and the trailing extra space. 20284 * We make sure that each header fragment begins on a 32-bit 20285 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 20286 * aligned in tcp_mdt_update). 20287 */ 20288 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 20289 tcp->tcp_mdt_hdr_tail), 4); 20290 20291 /* are we starting from the beginning of data block? */ 20292 if (*tail_unsent == 0) { 20293 *xmit_tail = (*xmit_tail)->b_cont; 20294 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 20295 *tail_unsent = (int)MBLKL(*xmit_tail); 20296 } 20297 20298 /* 20299 * Here we create one or more Multidata messages, each made up of 20300 * one header buffer and up to N payload buffers. This entire 20301 * operation is done within two loops: 20302 * 20303 * The outer loop mostly deals with creating the Multidata message, 20304 * as well as the header buffer that gets added to it. It also 20305 * links the Multidata messages together such that all of them can 20306 * be sent down to the lower layer in a single putnext call; this 20307 * linking behavior depends on the tcp_mdt_chain tunable. 20308 * 20309 * The inner loop takes an existing Multidata message, and adds 20310 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 20311 * packetizes those buffers by filling up the corresponding header 20312 * buffer fragments with the proper IP and TCP headers, and by 20313 * describing the layout of each packet in the packet descriptors 20314 * that get added to the Multidata. 20315 */ 20316 do { 20317 /* 20318 * If usable send window is too small, or data blocks in 20319 * transmit list are smaller than our threshold (i.e. app 20320 * performs large writes followed by small ones), we hand 20321 * off the control over to the legacy path. Note that we'll 20322 * get back the control once it encounters a large block. 20323 */ 20324 if (*usable < mss || (*tail_unsent <= mdt_thres && 20325 (*xmit_tail)->b_cont != NULL && 20326 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 20327 /* send down what we've got so far */ 20328 if (md_mp_head != NULL) { 20329 tcp_multisend_data(tcp, ire, ill, md_mp_head, 20330 obsegs, obbytes, &rconfirm); 20331 } 20332 /* 20333 * Pass control over to tcp_send(), but tell it to 20334 * return to us once a large-size transmission is 20335 * possible. 20336 */ 20337 TCP_STAT(tcps, tcp_mdt_legacy_small); 20338 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 20339 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 20340 tail_unsent, xmit_tail, local_time, 20341 mdt_thres)) <= 0) { 20342 /* burst count reached, or alloc failed */ 20343 IRE_REFRELE(ire); 20344 return (err); 20345 } 20346 20347 /* tcp_send() may have sent everything, so check */ 20348 if (*usable <= 0) { 20349 IRE_REFRELE(ire); 20350 return (0); 20351 } 20352 20353 TCP_STAT(tcps, tcp_mdt_legacy_ret); 20354 /* 20355 * We may have delivered the Multidata, so make sure 20356 * to re-initialize before the next round. 20357 */ 20358 md_mp_head = NULL; 20359 obsegs = obbytes = 0; 20360 num_burst_seg = tcp->tcp_snd_burst; 20361 PREP_NEW_MULTIDATA(); 20362 20363 /* are we starting from the beginning of data block? */ 20364 if (*tail_unsent == 0) { 20365 *xmit_tail = (*xmit_tail)->b_cont; 20366 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20367 (uintptr_t)INT_MAX); 20368 *tail_unsent = (int)MBLKL(*xmit_tail); 20369 } 20370 } 20371 /* 20372 * Record current values for parameters we may need to pass 20373 * to tcp_send() or tcp_multisend_data(). We checkpoint at 20374 * each iteration of the outer loop (each multidata message 20375 * creation). If we have a failure in the inner loop, we send 20376 * any complete multidata messages we have before reverting 20377 * to using the traditional non-md path. 20378 */ 20379 snxt_mmd = *snxt; 20380 usable_mmd = *usable; 20381 xmit_tail_mmd = *xmit_tail; 20382 tail_unsent_mmd = *tail_unsent; 20383 obsegs_mmd = obsegs; 20384 obbytes_mmd = obbytes; 20385 20386 /* 20387 * max_pld limits the number of mblks in tcp's transmit 20388 * queue that can be added to a Multidata message. Once 20389 * this counter reaches zero, no more additional mblks 20390 * can be added to it. What happens afterwards depends 20391 * on whether or not we are set to chain the Multidata 20392 * messages. If we are to link them together, reset 20393 * max_pld to its original value (tcp_mdt_max_pld) and 20394 * prepare to create a new Multidata message which will 20395 * get linked to md_mp_head. Else, leave it alone and 20396 * let the inner loop break on its own. 20397 */ 20398 if (tcp_mdt_chain && max_pld == 0) 20399 PREP_NEW_MULTIDATA(); 20400 20401 /* adding a payload buffer; re-initialize values */ 20402 if (add_buffer) 20403 PREP_NEW_PBUF(); 20404 20405 /* 20406 * If we don't have a Multidata, either because we just 20407 * (re)entered this outer loop, or after we branched off 20408 * to tcp_send above, setup the Multidata and header 20409 * buffer to be used. 20410 */ 20411 if (md_mp == NULL) { 20412 int md_hbuflen; 20413 uint32_t start, stuff; 20414 20415 /* 20416 * Calculate Multidata header buffer size large enough 20417 * to hold all of the headers that can possibly be 20418 * sent at this moment. We'd rather over-estimate 20419 * the size than running out of space; this is okay 20420 * since this buffer is small anyway. 20421 */ 20422 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 20423 20424 /* 20425 * Start and stuff offset for partial hardware 20426 * checksum offload; these are currently for IPv4. 20427 * For full checksum offload, they are set to zero. 20428 */ 20429 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 20430 if (af == AF_INET) { 20431 start = IP_SIMPLE_HDR_LENGTH; 20432 stuff = IP_SIMPLE_HDR_LENGTH + 20433 TCP_CHECKSUM_OFFSET; 20434 } else { 20435 start = IPV6_HDR_LEN; 20436 stuff = IPV6_HDR_LEN + 20437 TCP_CHECKSUM_OFFSET; 20438 } 20439 } else { 20440 start = stuff = 0; 20441 } 20442 20443 /* 20444 * Create the header buffer, Multidata, as well as 20445 * any necessary attributes (destination address, 20446 * SAP and hardware checksum offload) that should 20447 * be associated with the Multidata message. 20448 */ 20449 ASSERT(cur_hdr_off == 0); 20450 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 20451 ((md_hbuf->b_wptr += md_hbuflen), 20452 (mmd = mmd_alloc(md_hbuf, &md_mp, 20453 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 20454 /* fastpath mblk */ 20455 ire->ire_nce->nce_res_mp, 20456 /* hardware checksum enabled */ 20457 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 20458 /* hardware checksum offsets */ 20459 start, stuff, 0, 20460 /* hardware checksum flag */ 20461 hwcksum_flags, tcps) != 0)) { 20462 legacy_send: 20463 /* 20464 * We arrive here from a failure within the 20465 * inner (packetizer) loop or we fail one of 20466 * the conditionals above. We restore the 20467 * previously checkpointed values for: 20468 * xmit_tail 20469 * usable 20470 * tail_unsent 20471 * snxt 20472 * obbytes 20473 * obsegs 20474 * We should then be able to dispatch any 20475 * complete multidata before reverting to the 20476 * traditional path with consistent parameters 20477 * (the inner loop updates these as it 20478 * iterates). 20479 */ 20480 *xmit_tail = xmit_tail_mmd; 20481 *usable = usable_mmd; 20482 *tail_unsent = tail_unsent_mmd; 20483 *snxt = snxt_mmd; 20484 obbytes = obbytes_mmd; 20485 obsegs = obsegs_mmd; 20486 if (md_mp != NULL) { 20487 /* Unlink message from the chain */ 20488 if (md_mp_head != NULL) { 20489 err = (intptr_t)rmvb(md_mp_head, 20490 md_mp); 20491 /* 20492 * We can't assert that rmvb 20493 * did not return -1, since we 20494 * may get here before linkb 20495 * happens. We do, however, 20496 * check if we just removed the 20497 * only element in the list. 20498 */ 20499 if (err == 0) 20500 md_mp_head = NULL; 20501 } 20502 /* md_hbuf gets freed automatically */ 20503 TCP_STAT(tcps, tcp_mdt_discarded); 20504 freeb(md_mp); 20505 } else { 20506 /* Either allocb or mmd_alloc failed */ 20507 TCP_STAT(tcps, tcp_mdt_allocfail); 20508 if (md_hbuf != NULL) 20509 freeb(md_hbuf); 20510 } 20511 20512 /* send down what we've got so far */ 20513 if (md_mp_head != NULL) { 20514 tcp_multisend_data(tcp, ire, ill, 20515 md_mp_head, obsegs, obbytes, 20516 &rconfirm); 20517 } 20518 legacy_send_no_md: 20519 if (ire != NULL) 20520 IRE_REFRELE(ire); 20521 /* 20522 * Too bad; let the legacy path handle this. 20523 * We specify INT_MAX for the threshold, since 20524 * we gave up with the Multidata processings 20525 * and let the old path have it all. 20526 */ 20527 TCP_STAT(tcps, tcp_mdt_legacy_all); 20528 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20529 tcp_tcp_hdr_len, num_sack_blk, usable, 20530 snxt, tail_unsent, xmit_tail, local_time, 20531 INT_MAX)); 20532 } 20533 20534 /* link to any existing ones, if applicable */ 20535 TCP_STAT(tcps, tcp_mdt_allocd); 20536 if (md_mp_head == NULL) { 20537 md_mp_head = md_mp; 20538 } else if (tcp_mdt_chain) { 20539 TCP_STAT(tcps, tcp_mdt_linked); 20540 linkb(md_mp_head, md_mp); 20541 } 20542 } 20543 20544 ASSERT(md_mp_head != NULL); 20545 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20546 ASSERT(md_mp != NULL && mmd != NULL); 20547 ASSERT(md_hbuf != NULL); 20548 20549 /* 20550 * Packetize the transmittable portion of the data block; 20551 * each data block is essentially added to the Multidata 20552 * as a payload buffer. We also deal with adding more 20553 * than one payload buffers, which happens when the remaining 20554 * packetized portion of the current payload buffer is less 20555 * than MSS, while the next data block in transmit queue 20556 * has enough data to make up for one. This "spillover" 20557 * case essentially creates a split-packet, where portions 20558 * of the packet's payload fragments may span across two 20559 * virtually discontiguous address blocks. 20560 */ 20561 seg_len = mss; 20562 do { 20563 len = seg_len; 20564 20565 /* one must remain NULL for DTRACE_IP_FASTPATH */ 20566 ipha = NULL; 20567 ip6h = NULL; 20568 20569 ASSERT(len > 0); 20570 ASSERT(max_pld >= 0); 20571 ASSERT(!add_buffer || cur_pld_off == 0); 20572 20573 /* 20574 * First time around for this payload buffer; note 20575 * in the case of a spillover, the following has 20576 * been done prior to adding the split-packet 20577 * descriptor to Multidata, and we don't want to 20578 * repeat the process. 20579 */ 20580 if (add_buffer) { 20581 ASSERT(mmd != NULL); 20582 ASSERT(md_pbuf == NULL); 20583 ASSERT(md_pbuf_nxt == NULL); 20584 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20585 20586 /* 20587 * Have we reached the limit? We'd get to 20588 * this case when we're not chaining the 20589 * Multidata messages together, and since 20590 * we're done, terminate this loop. 20591 */ 20592 if (max_pld == 0) 20593 break; /* done */ 20594 20595 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20596 TCP_STAT(tcps, tcp_mdt_allocfail); 20597 goto legacy_send; /* out_of_mem */ 20598 } 20599 20600 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20601 zc_cap != NULL) { 20602 if (!ip_md_zcopy_attr(mmd, NULL, 20603 zc_cap->ill_zerocopy_flags)) { 20604 freeb(md_pbuf); 20605 TCP_STAT(tcps, 20606 tcp_mdt_allocfail); 20607 /* out_of_mem */ 20608 goto legacy_send; 20609 } 20610 zcopy = B_TRUE; 20611 } 20612 20613 md_pbuf->b_rptr += base_pld_off; 20614 20615 /* 20616 * Add a payload buffer to the Multidata; this 20617 * operation must not fail, or otherwise our 20618 * logic in this routine is broken. There 20619 * is no memory allocation done by the 20620 * routine, so any returned failure simply 20621 * tells us that we've done something wrong. 20622 * 20623 * A failure tells us that either we're adding 20624 * the same payload buffer more than once, or 20625 * we're trying to add more buffers than 20626 * allowed (max_pld calculation is wrong). 20627 * None of the above cases should happen, and 20628 * we panic because either there's horrible 20629 * heap corruption, and/or programming mistake. 20630 */ 20631 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20632 if (pbuf_idx < 0) { 20633 cmn_err(CE_PANIC, "tcp_multisend: " 20634 "payload buffer logic error " 20635 "detected for tcp %p mmd %p " 20636 "pbuf %p (%d)\n", 20637 (void *)tcp, (void *)mmd, 20638 (void *)md_pbuf, pbuf_idx); 20639 } 20640 20641 ASSERT(max_pld > 0); 20642 --max_pld; 20643 add_buffer = B_FALSE; 20644 } 20645 20646 ASSERT(md_mp_head != NULL); 20647 ASSERT(md_pbuf != NULL); 20648 ASSERT(md_pbuf_nxt == NULL); 20649 ASSERT(pbuf_idx != -1); 20650 ASSERT(pbuf_idx_nxt == -1); 20651 ASSERT(*usable > 0); 20652 20653 /* 20654 * We spillover to the next payload buffer only 20655 * if all of the following is true: 20656 * 20657 * 1. There is not enough data on the current 20658 * payload buffer to make up `len', 20659 * 2. We are allowed to send `len', 20660 * 3. The next payload buffer length is large 20661 * enough to accomodate `spill'. 20662 */ 20663 if ((spill = len - *tail_unsent) > 0 && 20664 *usable >= len && 20665 MBLKL((*xmit_tail)->b_cont) >= spill && 20666 max_pld > 0) { 20667 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20668 if (md_pbuf_nxt == NULL) { 20669 TCP_STAT(tcps, tcp_mdt_allocfail); 20670 goto legacy_send; /* out_of_mem */ 20671 } 20672 20673 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20674 zc_cap != NULL) { 20675 if (!ip_md_zcopy_attr(mmd, NULL, 20676 zc_cap->ill_zerocopy_flags)) { 20677 freeb(md_pbuf_nxt); 20678 TCP_STAT(tcps, 20679 tcp_mdt_allocfail); 20680 /* out_of_mem */ 20681 goto legacy_send; 20682 } 20683 zcopy = B_TRUE; 20684 } 20685 20686 /* 20687 * See comments above on the first call to 20688 * mmd_addpldbuf for explanation on the panic. 20689 */ 20690 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20691 if (pbuf_idx_nxt < 0) { 20692 panic("tcp_multisend: " 20693 "next payload buffer logic error " 20694 "detected for tcp %p mmd %p " 20695 "pbuf %p (%d)\n", 20696 (void *)tcp, (void *)mmd, 20697 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20698 } 20699 20700 ASSERT(max_pld > 0); 20701 --max_pld; 20702 } else if (spill > 0) { 20703 /* 20704 * If there's a spillover, but the following 20705 * xmit_tail couldn't give us enough octets 20706 * to reach "len", then stop the current 20707 * Multidata creation and let the legacy 20708 * tcp_send() path take over. We don't want 20709 * to send the tiny segment as part of this 20710 * Multidata for performance reasons; instead, 20711 * we let the legacy path deal with grouping 20712 * it with the subsequent small mblks. 20713 */ 20714 if (*usable >= len && 20715 MBLKL((*xmit_tail)->b_cont) < spill) { 20716 max_pld = 0; 20717 break; /* done */ 20718 } 20719 20720 /* 20721 * We can't spillover, and we are near 20722 * the end of the current payload buffer, 20723 * so send what's left. 20724 */ 20725 ASSERT(*tail_unsent > 0); 20726 len = *tail_unsent; 20727 } 20728 20729 /* tail_unsent is negated if there is a spillover */ 20730 *tail_unsent -= len; 20731 *usable -= len; 20732 ASSERT(*usable >= 0); 20733 20734 if (*usable < mss) 20735 seg_len = *usable; 20736 /* 20737 * Sender SWS avoidance; see comments in tcp_send(); 20738 * everything else is the same, except that we only 20739 * do this here if there is no more data to be sent 20740 * following the current xmit_tail. We don't check 20741 * for 1-byte urgent data because we shouldn't get 20742 * here if TCP_URG_VALID is set. 20743 */ 20744 if (*usable > 0 && *usable < mss && 20745 ((md_pbuf_nxt == NULL && 20746 (*xmit_tail)->b_cont == NULL) || 20747 (md_pbuf_nxt != NULL && 20748 (*xmit_tail)->b_cont->b_cont == NULL)) && 20749 seg_len < (tcp->tcp_max_swnd >> 1) && 20750 (tcp->tcp_unsent - 20751 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20752 !tcp->tcp_zero_win_probe) { 20753 if ((*snxt + len) == tcp->tcp_snxt && 20754 (*snxt + len) == tcp->tcp_suna) { 20755 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20756 } 20757 done = B_TRUE; 20758 } 20759 20760 /* 20761 * Prime pump for IP's checksumming on our behalf; 20762 * include the adjustment for a source route if any. 20763 * Do this only for software/partial hardware checksum 20764 * offload, as this field gets zeroed out later for 20765 * the full hardware checksum offload case. 20766 */ 20767 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20768 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20769 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20770 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20771 } 20772 20773 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20774 *snxt += len; 20775 20776 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20777 /* 20778 * We set the PUSH bit only if TCP has no more buffered 20779 * data to be transmitted (or if sender SWS avoidance 20780 * takes place), as opposed to setting it for every 20781 * last packet in the burst. 20782 */ 20783 if (done || 20784 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20785 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20786 20787 /* 20788 * Set FIN bit if this is our last segment; snxt 20789 * already includes its length, and it will not 20790 * be adjusted after this point. 20791 */ 20792 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20793 *snxt == tcp->tcp_fss) { 20794 if (!tcp->tcp_fin_acked) { 20795 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20796 BUMP_MIB(&tcps->tcps_mib, 20797 tcpOutControl); 20798 } 20799 if (!tcp->tcp_fin_sent) { 20800 tcp->tcp_fin_sent = B_TRUE; 20801 /* 20802 * tcp state must be ESTABLISHED 20803 * in order for us to get here in 20804 * the first place. 20805 */ 20806 tcp->tcp_state = TCPS_FIN_WAIT_1; 20807 20808 /* 20809 * Upon returning from this routine, 20810 * tcp_wput_data() will set tcp_snxt 20811 * to be equal to snxt + tcp_fin_sent. 20812 * This is essentially the same as 20813 * setting it to tcp_fss + 1. 20814 */ 20815 } 20816 } 20817 20818 tcp->tcp_last_sent_len = (ushort_t)len; 20819 20820 len += tcp_hdr_len; 20821 if (tcp->tcp_ipversion == IPV4_VERSION) 20822 tcp->tcp_ipha->ipha_length = htons(len); 20823 else 20824 tcp->tcp_ip6h->ip6_plen = htons(len - 20825 ((char *)&tcp->tcp_ip6h[1] - 20826 tcp->tcp_iphc)); 20827 20828 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20829 20830 /* setup header fragment */ 20831 PDESC_HDR_ADD(pkt_info, 20832 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20833 tcp->tcp_mdt_hdr_head, /* head room */ 20834 tcp_hdr_len, /* len */ 20835 tcp->tcp_mdt_hdr_tail); /* tail room */ 20836 20837 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20838 hdr_frag_sz); 20839 ASSERT(MBLKIN(md_hbuf, 20840 (pkt_info->hdr_base - md_hbuf->b_rptr), 20841 PDESC_HDRSIZE(pkt_info))); 20842 20843 /* setup first payload fragment */ 20844 PDESC_PLD_INIT(pkt_info); 20845 PDESC_PLD_SPAN_ADD(pkt_info, 20846 pbuf_idx, /* index */ 20847 md_pbuf->b_rptr + cur_pld_off, /* start */ 20848 tcp->tcp_last_sent_len); /* len */ 20849 20850 /* create a split-packet in case of a spillover */ 20851 if (md_pbuf_nxt != NULL) { 20852 ASSERT(spill > 0); 20853 ASSERT(pbuf_idx_nxt > pbuf_idx); 20854 ASSERT(!add_buffer); 20855 20856 md_pbuf = md_pbuf_nxt; 20857 md_pbuf_nxt = NULL; 20858 pbuf_idx = pbuf_idx_nxt; 20859 pbuf_idx_nxt = -1; 20860 cur_pld_off = spill; 20861 20862 /* trim out first payload fragment */ 20863 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20864 20865 /* setup second payload fragment */ 20866 PDESC_PLD_SPAN_ADD(pkt_info, 20867 pbuf_idx, /* index */ 20868 md_pbuf->b_rptr, /* start */ 20869 spill); /* len */ 20870 20871 if ((*xmit_tail)->b_next == NULL) { 20872 /* 20873 * Store the lbolt used for RTT 20874 * estimation. We can only record one 20875 * timestamp per mblk so we do it when 20876 * we reach the end of the payload 20877 * buffer. Also we only take a new 20878 * timestamp sample when the previous 20879 * timed data from the same mblk has 20880 * been ack'ed. 20881 */ 20882 (*xmit_tail)->b_prev = local_time; 20883 (*xmit_tail)->b_next = 20884 (mblk_t *)(uintptr_t)first_snxt; 20885 } 20886 20887 first_snxt = *snxt - spill; 20888 20889 /* 20890 * Advance xmit_tail; usable could be 0 by 20891 * the time we got here, but we made sure 20892 * above that we would only spillover to 20893 * the next data block if usable includes 20894 * the spilled-over amount prior to the 20895 * subtraction. Therefore, we are sure 20896 * that xmit_tail->b_cont can't be NULL. 20897 */ 20898 ASSERT((*xmit_tail)->b_cont != NULL); 20899 *xmit_tail = (*xmit_tail)->b_cont; 20900 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20901 (uintptr_t)INT_MAX); 20902 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20903 } else { 20904 cur_pld_off += tcp->tcp_last_sent_len; 20905 } 20906 20907 /* 20908 * Fill in the header using the template header, and 20909 * add options such as time-stamp, ECN and/or SACK, 20910 * as needed. 20911 */ 20912 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20913 (clock_t)local_time, num_sack_blk); 20914 20915 /* take care of some IP header businesses */ 20916 if (af == AF_INET) { 20917 ipha = (ipha_t *)pkt_info->hdr_rptr; 20918 20919 ASSERT(OK_32PTR((uchar_t *)ipha)); 20920 ASSERT(PDESC_HDRL(pkt_info) >= 20921 IP_SIMPLE_HDR_LENGTH); 20922 ASSERT(ipha->ipha_version_and_hdr_length == 20923 IP_SIMPLE_HDR_VERSION); 20924 20925 /* 20926 * Assign ident value for current packet; see 20927 * related comments in ip_wput_ire() about the 20928 * contract private interface with clustering 20929 * group. 20930 */ 20931 clusterwide = B_FALSE; 20932 if (cl_inet_ipident != NULL) { 20933 ASSERT(cl_inet_isclusterwide != NULL); 20934 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 20935 AF_INET, 20936 (uint8_t *)(uintptr_t)src)) { 20937 ipha->ipha_ident = 20938 (*cl_inet_ipident) 20939 (IPPROTO_IP, AF_INET, 20940 (uint8_t *)(uintptr_t)src, 20941 (uint8_t *)(uintptr_t)dst); 20942 clusterwide = B_TRUE; 20943 } 20944 } 20945 20946 if (!clusterwide) { 20947 ipha->ipha_ident = (uint16_t) 20948 atomic_add_32_nv( 20949 &ire->ire_ident, 1); 20950 } 20951 #ifndef _BIG_ENDIAN 20952 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20953 (ipha->ipha_ident >> 8); 20954 #endif 20955 } else { 20956 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20957 20958 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20959 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20960 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20961 ASSERT(PDESC_HDRL(pkt_info) >= 20962 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20963 TCP_CHECKSUM_SIZE)); 20964 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20965 20966 if (tcp->tcp_ip_forward_progress) { 20967 rconfirm = B_TRUE; 20968 tcp->tcp_ip_forward_progress = B_FALSE; 20969 } 20970 } 20971 20972 /* at least one payload span, and at most two */ 20973 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20974 20975 /* add the packet descriptor to Multidata */ 20976 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20977 KM_NOSLEEP)) == NULL) { 20978 /* 20979 * Any failure other than ENOMEM indicates 20980 * that we have passed in invalid pkt_info 20981 * or parameters to mmd_addpdesc, which must 20982 * not happen. 20983 * 20984 * EINVAL is a result of failure on boundary 20985 * checks against the pkt_info contents. It 20986 * should not happen, and we panic because 20987 * either there's horrible heap corruption, 20988 * and/or programming mistake. 20989 */ 20990 if (err != ENOMEM) { 20991 cmn_err(CE_PANIC, "tcp_multisend: " 20992 "pdesc logic error detected for " 20993 "tcp %p mmd %p pinfo %p (%d)\n", 20994 (void *)tcp, (void *)mmd, 20995 (void *)pkt_info, err); 20996 } 20997 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20998 goto legacy_send; /* out_of_mem */ 20999 } 21000 ASSERT(pkt != NULL); 21001 21002 /* calculate IP header and TCP checksums */ 21003 if (af == AF_INET) { 21004 /* calculate pseudo-header checksum */ 21005 cksum = (dst >> 16) + (dst & 0xFFFF) + 21006 (src >> 16) + (src & 0xFFFF); 21007 21008 /* offset for TCP header checksum */ 21009 up = IPH_TCPH_CHECKSUMP(ipha, 21010 IP_SIMPLE_HDR_LENGTH); 21011 } else { 21012 up = (uint16_t *)&ip6h->ip6_src; 21013 21014 /* calculate pseudo-header checksum */ 21015 cksum = up[0] + up[1] + up[2] + up[3] + 21016 up[4] + up[5] + up[6] + up[7] + 21017 up[8] + up[9] + up[10] + up[11] + 21018 up[12] + up[13] + up[14] + up[15]; 21019 21020 /* Fold the initial sum */ 21021 cksum = (cksum & 0xffff) + (cksum >> 16); 21022 21023 up = (uint16_t *)(((uchar_t *)ip6h) + 21024 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 21025 } 21026 21027 if (hwcksum_flags & HCK_FULLCKSUM) { 21028 /* clear checksum field for hardware */ 21029 *up = 0; 21030 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 21031 uint32_t sum; 21032 21033 /* pseudo-header checksumming */ 21034 sum = *up + cksum + IP_TCP_CSUM_COMP; 21035 sum = (sum & 0xFFFF) + (sum >> 16); 21036 *up = (sum & 0xFFFF) + (sum >> 16); 21037 } else { 21038 /* software checksumming */ 21039 TCP_STAT(tcps, tcp_out_sw_cksum); 21040 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 21041 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 21042 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 21043 cksum + IP_TCP_CSUM_COMP); 21044 if (*up == 0) 21045 *up = 0xFFFF; 21046 } 21047 21048 /* IPv4 header checksum */ 21049 if (af == AF_INET) { 21050 ipha->ipha_fragment_offset_and_flags |= 21051 (uint32_t)htons(ire->ire_frag_flag); 21052 21053 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 21054 ipha->ipha_hdr_checksum = 0; 21055 } else { 21056 IP_HDR_CKSUM(ipha, cksum, 21057 ((uint32_t *)ipha)[0], 21058 ((uint16_t *)ipha)[4]); 21059 } 21060 } 21061 21062 if (af == AF_INET && 21063 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 21064 af == AF_INET6 && 21065 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 21066 mblk_t *mp, *mp1; 21067 uchar_t *hdr_rptr, *hdr_wptr; 21068 uchar_t *pld_rptr, *pld_wptr; 21069 21070 /* 21071 * We reconstruct a pseudo packet for the hooks 21072 * framework using mmd_transform_link(). 21073 * If it is a split packet we pullup the 21074 * payload. FW_HOOKS expects a pkt comprising 21075 * of two mblks: a header and the payload. 21076 */ 21077 if ((mp = mmd_transform_link(pkt)) == NULL) { 21078 TCP_STAT(tcps, tcp_mdt_allocfail); 21079 goto legacy_send; 21080 } 21081 21082 if (pkt_info->pld_cnt > 1) { 21083 /* split payload, more than one pld */ 21084 if ((mp1 = msgpullup(mp->b_cont, -1)) == 21085 NULL) { 21086 freemsg(mp); 21087 TCP_STAT(tcps, 21088 tcp_mdt_allocfail); 21089 goto legacy_send; 21090 } 21091 freemsg(mp->b_cont); 21092 mp->b_cont = mp1; 21093 } else { 21094 mp1 = mp->b_cont; 21095 } 21096 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 21097 21098 /* 21099 * Remember the message offsets. This is so we 21100 * can detect changes when we return from the 21101 * FW_HOOKS callbacks. 21102 */ 21103 hdr_rptr = mp->b_rptr; 21104 hdr_wptr = mp->b_wptr; 21105 pld_rptr = mp->b_cont->b_rptr; 21106 pld_wptr = mp->b_cont->b_wptr; 21107 21108 if (af == AF_INET) { 21109 DTRACE_PROBE4( 21110 ip4__physical__out__start, 21111 ill_t *, NULL, 21112 ill_t *, ill, 21113 ipha_t *, ipha, 21114 mblk_t *, mp); 21115 FW_HOOKS( 21116 ipst->ips_ip4_physical_out_event, 21117 ipst->ips_ipv4firewall_physical_out, 21118 NULL, ill, ipha, mp, mp, 0, ipst); 21119 DTRACE_PROBE1( 21120 ip4__physical__out__end, 21121 mblk_t *, mp); 21122 } else { 21123 DTRACE_PROBE4( 21124 ip6__physical__out_start, 21125 ill_t *, NULL, 21126 ill_t *, ill, 21127 ip6_t *, ip6h, 21128 mblk_t *, mp); 21129 FW_HOOKS6( 21130 ipst->ips_ip6_physical_out_event, 21131 ipst->ips_ipv6firewall_physical_out, 21132 NULL, ill, ip6h, mp, mp, 0, ipst); 21133 DTRACE_PROBE1( 21134 ip6__physical__out__end, 21135 mblk_t *, mp); 21136 } 21137 21138 if (mp == NULL || 21139 (mp1 = mp->b_cont) == NULL || 21140 mp->b_rptr != hdr_rptr || 21141 mp->b_wptr != hdr_wptr || 21142 mp1->b_rptr != pld_rptr || 21143 mp1->b_wptr != pld_wptr || 21144 mp1->b_cont != NULL) { 21145 /* 21146 * We abandon multidata processing and 21147 * return to the normal path, either 21148 * when a packet is blocked, or when 21149 * the boundaries of header buffer or 21150 * payload buffer have been changed by 21151 * FW_HOOKS[6]. 21152 */ 21153 if (mp != NULL) 21154 freemsg(mp); 21155 goto legacy_send; 21156 } 21157 /* Finished with the pseudo packet */ 21158 freemsg(mp); 21159 } 21160 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 21161 ill, ipha, ip6h); 21162 /* advance header offset */ 21163 cur_hdr_off += hdr_frag_sz; 21164 21165 obbytes += tcp->tcp_last_sent_len; 21166 ++obsegs; 21167 } while (!done && *usable > 0 && --num_burst_seg > 0 && 21168 *tail_unsent > 0); 21169 21170 if ((*xmit_tail)->b_next == NULL) { 21171 /* 21172 * Store the lbolt used for RTT estimation. We can only 21173 * record one timestamp per mblk so we do it when we 21174 * reach the end of the payload buffer. Also we only 21175 * take a new timestamp sample when the previous timed 21176 * data from the same mblk has been ack'ed. 21177 */ 21178 (*xmit_tail)->b_prev = local_time; 21179 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 21180 } 21181 21182 ASSERT(*tail_unsent >= 0); 21183 if (*tail_unsent > 0) { 21184 /* 21185 * We got here because we broke out of the above 21186 * loop due to of one of the following cases: 21187 * 21188 * 1. len < adjusted MSS (i.e. small), 21189 * 2. Sender SWS avoidance, 21190 * 3. max_pld is zero. 21191 * 21192 * We are done for this Multidata, so trim our 21193 * last payload buffer (if any) accordingly. 21194 */ 21195 if (md_pbuf != NULL) 21196 md_pbuf->b_wptr -= *tail_unsent; 21197 } else if (*usable > 0) { 21198 *xmit_tail = (*xmit_tail)->b_cont; 21199 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 21200 (uintptr_t)INT_MAX); 21201 *tail_unsent = (int)MBLKL(*xmit_tail); 21202 add_buffer = B_TRUE; 21203 } 21204 } while (!done && *usable > 0 && num_burst_seg > 0 && 21205 (tcp_mdt_chain || max_pld > 0)); 21206 21207 if (md_mp_head != NULL) { 21208 /* send everything down */ 21209 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 21210 &rconfirm); 21211 } 21212 21213 #undef PREP_NEW_MULTIDATA 21214 #undef PREP_NEW_PBUF 21215 #undef IPVER 21216 21217 IRE_REFRELE(ire); 21218 return (0); 21219 } 21220 21221 /* 21222 * A wrapper function for sending one or more Multidata messages down to 21223 * the module below ip; this routine does not release the reference of the 21224 * IRE (caller does that). This routine is analogous to tcp_send_data(). 21225 */ 21226 static void 21227 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 21228 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 21229 { 21230 uint64_t delta; 21231 nce_t *nce; 21232 tcp_stack_t *tcps = tcp->tcp_tcps; 21233 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21234 21235 ASSERT(ire != NULL && ill != NULL); 21236 ASSERT(ire->ire_stq != NULL); 21237 ASSERT(md_mp_head != NULL); 21238 ASSERT(rconfirm != NULL); 21239 21240 /* adjust MIBs and IRE timestamp */ 21241 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 21242 tcp->tcp_obsegs += obsegs; 21243 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 21244 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 21245 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 21246 21247 if (tcp->tcp_ipversion == IPV4_VERSION) { 21248 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 21249 } else { 21250 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 21251 } 21252 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 21253 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 21254 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 21255 21256 ire->ire_ob_pkt_count += obsegs; 21257 if (ire->ire_ipif != NULL) 21258 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 21259 ire->ire_last_used_time = lbolt; 21260 21261 /* send it down */ 21262 if (ILL_DLS_CAPABLE(ill)) { 21263 ill_dls_capab_t *ill_dls = ill->ill_dls_capab; 21264 ill_dls->ill_tx(ill_dls->ill_tx_handle, md_mp_head); 21265 } else { 21266 putnext(ire->ire_stq, md_mp_head); 21267 } 21268 21269 /* we're done for TCP/IPv4 */ 21270 if (tcp->tcp_ipversion == IPV4_VERSION) 21271 return; 21272 21273 nce = ire->ire_nce; 21274 21275 ASSERT(nce != NULL); 21276 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 21277 ASSERT(nce->nce_state != ND_INCOMPLETE); 21278 21279 /* reachability confirmation? */ 21280 if (*rconfirm) { 21281 nce->nce_last = TICK_TO_MSEC(lbolt64); 21282 if (nce->nce_state != ND_REACHABLE) { 21283 mutex_enter(&nce->nce_lock); 21284 nce->nce_state = ND_REACHABLE; 21285 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 21286 mutex_exit(&nce->nce_lock); 21287 (void) untimeout(nce->nce_timeout_id); 21288 if (ip_debug > 2) { 21289 /* ip1dbg */ 21290 pr_addr_dbg("tcp_multisend_data: state " 21291 "for %s changed to REACHABLE\n", 21292 AF_INET6, &ire->ire_addr_v6); 21293 } 21294 } 21295 /* reset transport reachability confirmation */ 21296 *rconfirm = B_FALSE; 21297 } 21298 21299 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 21300 ip1dbg(("tcp_multisend_data: delta = %" PRId64 21301 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 21302 21303 if (delta > (uint64_t)ill->ill_reachable_time) { 21304 mutex_enter(&nce->nce_lock); 21305 switch (nce->nce_state) { 21306 case ND_REACHABLE: 21307 case ND_STALE: 21308 /* 21309 * ND_REACHABLE is identical to ND_STALE in this 21310 * specific case. If reachable time has expired for 21311 * this neighbor (delta is greater than reachable 21312 * time), conceptually, the neighbor cache is no 21313 * longer in REACHABLE state, but already in STALE 21314 * state. So the correct transition here is to 21315 * ND_DELAY. 21316 */ 21317 nce->nce_state = ND_DELAY; 21318 mutex_exit(&nce->nce_lock); 21319 NDP_RESTART_TIMER(nce, 21320 ipst->ips_delay_first_probe_time); 21321 if (ip_debug > 3) { 21322 /* ip2dbg */ 21323 pr_addr_dbg("tcp_multisend_data: state " 21324 "for %s changed to DELAY\n", 21325 AF_INET6, &ire->ire_addr_v6); 21326 } 21327 break; 21328 case ND_DELAY: 21329 case ND_PROBE: 21330 mutex_exit(&nce->nce_lock); 21331 /* Timers have already started */ 21332 break; 21333 case ND_UNREACHABLE: 21334 /* 21335 * ndp timer has detected that this nce is 21336 * unreachable and initiated deleting this nce 21337 * and all its associated IREs. This is a race 21338 * where we found the ire before it was deleted 21339 * and have just sent out a packet using this 21340 * unreachable nce. 21341 */ 21342 mutex_exit(&nce->nce_lock); 21343 break; 21344 default: 21345 ASSERT(0); 21346 } 21347 } 21348 } 21349 21350 /* 21351 * Derived from tcp_send_data(). 21352 */ 21353 static void 21354 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 21355 int num_lso_seg) 21356 { 21357 ipha_t *ipha; 21358 mblk_t *ire_fp_mp; 21359 uint_t ire_fp_mp_len; 21360 uint32_t hcksum_txflags = 0; 21361 ipaddr_t src; 21362 ipaddr_t dst; 21363 uint32_t cksum; 21364 uint16_t *up; 21365 tcp_stack_t *tcps = tcp->tcp_tcps; 21366 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21367 21368 ASSERT(DB_TYPE(mp) == M_DATA); 21369 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 21370 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 21371 ASSERT(tcp->tcp_connp != NULL); 21372 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 21373 21374 ipha = (ipha_t *)mp->b_rptr; 21375 src = ipha->ipha_src; 21376 dst = ipha->ipha_dst; 21377 21378 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 21379 21380 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 21381 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 21382 num_lso_seg); 21383 #ifndef _BIG_ENDIAN 21384 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 21385 #endif 21386 if (tcp->tcp_snd_zcopy_aware) { 21387 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 21388 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 21389 mp = tcp_zcopy_disable(tcp, mp); 21390 } 21391 21392 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 21393 ASSERT(ill->ill_hcksum_capab != NULL); 21394 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 21395 } 21396 21397 /* 21398 * Since the TCP checksum should be recalculated by h/w, we can just 21399 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 21400 * pseudo-header checksum for HCK_PARTIALCKSUM. 21401 * The partial pseudo-header excludes TCP length, that was calculated 21402 * in tcp_send(), so to zero *up before further processing. 21403 */ 21404 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 21405 21406 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 21407 *up = 0; 21408 21409 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 21410 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 21411 21412 /* 21413 * Append LSO flag to DB_LSOFLAGS(mp) and set the mss to DB_LSOMSS(mp). 21414 */ 21415 DB_LSOFLAGS(mp) |= HW_LSO; 21416 DB_LSOMSS(mp) = mss; 21417 21418 ipha->ipha_fragment_offset_and_flags |= 21419 (uint32_t)htons(ire->ire_frag_flag); 21420 21421 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21422 ire_fp_mp_len = MBLKL(ire_fp_mp); 21423 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 21424 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 21425 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 21426 21427 UPDATE_OB_PKT_COUNT(ire); 21428 ire->ire_last_used_time = lbolt; 21429 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 21430 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 21431 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 21432 ntohs(ipha->ipha_length)); 21433 21434 if (ILL_DLS_CAPABLE(ill)) { 21435 /* 21436 * Send the packet directly to DLD, where it may be queued 21437 * depending on the availability of transmit resources at 21438 * the media layer. 21439 */ 21440 IP_DLS_ILL_TX(ill, ipha, mp, ipst); 21441 } else { 21442 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 21443 DTRACE_PROBE4(ip4__physical__out__start, 21444 ill_t *, NULL, ill_t *, out_ill, 21445 ipha_t *, ipha, mblk_t *, mp); 21446 FW_HOOKS(ipst->ips_ip4_physical_out_event, 21447 ipst->ips_ipv4firewall_physical_out, 21448 NULL, out_ill, ipha, mp, mp, 0, ipst); 21449 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 21450 21451 if (mp != NULL) { 21452 DTRACE_IP_FASTPATH(mp, ipha, out_ill, ipha, NULL); 21453 putnext(ire->ire_stq, mp); 21454 } 21455 } 21456 } 21457 21458 /* 21459 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 21460 * scheme, and returns one of the following: 21461 * 21462 * -1 = failed allocation. 21463 * 0 = success; burst count reached, or usable send window is too small, 21464 * and that we'd rather wait until later before sending again. 21465 * 1 = success; we are called from tcp_multisend(), and both usable send 21466 * window and tail_unsent are greater than the MDT threshold, and thus 21467 * Multidata Transmit should be used instead. 21468 */ 21469 static int 21470 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 21471 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 21472 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 21473 const int mdt_thres) 21474 { 21475 int num_burst_seg = tcp->tcp_snd_burst; 21476 ire_t *ire = NULL; 21477 ill_t *ill = NULL; 21478 mblk_t *ire_fp_mp = NULL; 21479 uint_t ire_fp_mp_len = 0; 21480 int num_lso_seg = 1; 21481 uint_t lso_usable; 21482 boolean_t do_lso_send = B_FALSE; 21483 tcp_stack_t *tcps = tcp->tcp_tcps; 21484 21485 /* 21486 * Check LSO capability before any further work. And the similar check 21487 * need to be done in for(;;) loop. 21488 * LSO will be deployed when therer is more than one mss of available 21489 * data and a burst transmission is allowed. 21490 */ 21491 if (tcp->tcp_lso && 21492 (tcp->tcp_valid_bits == 0 || 21493 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21494 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21495 /* 21496 * Try to find usable IRE/ILL and do basic check to the ILL. 21497 */ 21498 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 21499 /* 21500 * Enable LSO with this transmission. 21501 * Since IRE has been hold in 21502 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 21503 * should be called before return. 21504 */ 21505 do_lso_send = B_TRUE; 21506 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21507 ire_fp_mp_len = MBLKL(ire_fp_mp); 21508 /* Round up to multiple of 4 */ 21509 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21510 } else { 21511 do_lso_send = B_FALSE; 21512 ill = NULL; 21513 } 21514 } 21515 21516 for (;;) { 21517 struct datab *db; 21518 tcph_t *tcph; 21519 uint32_t sum; 21520 mblk_t *mp, *mp1; 21521 uchar_t *rptr; 21522 int len; 21523 21524 /* 21525 * If we're called by tcp_multisend(), and the amount of 21526 * sendable data as well as the size of current xmit_tail 21527 * is beyond the MDT threshold, return to the caller and 21528 * let the large data transmit be done using MDT. 21529 */ 21530 if (*usable > 0 && *usable > mdt_thres && 21531 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21532 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21533 ASSERT(tcp->tcp_mdt); 21534 return (1); /* success; do large send */ 21535 } 21536 21537 if (num_burst_seg == 0) 21538 break; /* success; burst count reached */ 21539 21540 /* 21541 * Calculate the maximum payload length we can send in *one* 21542 * time. 21543 */ 21544 if (do_lso_send) { 21545 /* 21546 * Check whether need to do LSO any more. 21547 */ 21548 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21549 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21550 lso_usable = MIN(lso_usable, 21551 num_burst_seg * mss); 21552 21553 num_lso_seg = lso_usable / mss; 21554 if (lso_usable % mss) { 21555 num_lso_seg++; 21556 tcp->tcp_last_sent_len = (ushort_t) 21557 (lso_usable % mss); 21558 } else { 21559 tcp->tcp_last_sent_len = (ushort_t)mss; 21560 } 21561 } else { 21562 do_lso_send = B_FALSE; 21563 num_lso_seg = 1; 21564 lso_usable = mss; 21565 } 21566 } 21567 21568 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21569 21570 /* 21571 * Adjust num_burst_seg here. 21572 */ 21573 num_burst_seg -= num_lso_seg; 21574 21575 len = mss; 21576 if (len > *usable) { 21577 ASSERT(do_lso_send == B_FALSE); 21578 21579 len = *usable; 21580 if (len <= 0) { 21581 /* Terminate the loop */ 21582 break; /* success; too small */ 21583 } 21584 /* 21585 * Sender silly-window avoidance. 21586 * Ignore this if we are going to send a 21587 * zero window probe out. 21588 * 21589 * TODO: force data into microscopic window? 21590 * ==> (!pushed || (unsent > usable)) 21591 */ 21592 if (len < (tcp->tcp_max_swnd >> 1) && 21593 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21594 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21595 len == 1) && (! tcp->tcp_zero_win_probe)) { 21596 /* 21597 * If the retransmit timer is not running 21598 * we start it so that we will retransmit 21599 * in the case when the the receiver has 21600 * decremented the window. 21601 */ 21602 if (*snxt == tcp->tcp_snxt && 21603 *snxt == tcp->tcp_suna) { 21604 /* 21605 * We are not supposed to send 21606 * anything. So let's wait a little 21607 * bit longer before breaking SWS 21608 * avoidance. 21609 * 21610 * What should the value be? 21611 * Suggestion: MAX(init rexmit time, 21612 * tcp->tcp_rto) 21613 */ 21614 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21615 } 21616 break; /* success; too small */ 21617 } 21618 } 21619 21620 tcph = tcp->tcp_tcph; 21621 21622 /* 21623 * The reason to adjust len here is that we need to set flags 21624 * and calculate checksum. 21625 */ 21626 if (do_lso_send) 21627 len = lso_usable; 21628 21629 *usable -= len; /* Approximate - can be adjusted later */ 21630 if (*usable > 0) 21631 tcph->th_flags[0] = TH_ACK; 21632 else 21633 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21634 21635 /* 21636 * Prime pump for IP's checksumming on our behalf 21637 * Include the adjustment for a source route if any. 21638 */ 21639 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21640 sum = (sum >> 16) + (sum & 0xFFFF); 21641 U16_TO_ABE16(sum, tcph->th_sum); 21642 21643 U32_TO_ABE32(*snxt, tcph->th_seq); 21644 21645 /* 21646 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21647 * set. For the case when TCP_FSS_VALID is the only valid 21648 * bit (normal active close), branch off only when we think 21649 * that the FIN flag needs to be set. Note for this case, 21650 * that (snxt + len) may not reflect the actual seg_len, 21651 * as len may be further reduced in tcp_xmit_mp(). If len 21652 * gets modified, we will end up here again. 21653 */ 21654 if (tcp->tcp_valid_bits != 0 && 21655 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21656 ((*snxt + len) == tcp->tcp_fss))) { 21657 uchar_t *prev_rptr; 21658 uint32_t prev_snxt = tcp->tcp_snxt; 21659 21660 if (*tail_unsent == 0) { 21661 ASSERT((*xmit_tail)->b_cont != NULL); 21662 *xmit_tail = (*xmit_tail)->b_cont; 21663 prev_rptr = (*xmit_tail)->b_rptr; 21664 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21665 (*xmit_tail)->b_rptr); 21666 } else { 21667 prev_rptr = (*xmit_tail)->b_rptr; 21668 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21669 *tail_unsent; 21670 } 21671 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21672 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21673 /* Restore tcp_snxt so we get amount sent right. */ 21674 tcp->tcp_snxt = prev_snxt; 21675 if (prev_rptr == (*xmit_tail)->b_rptr) { 21676 /* 21677 * If the previous timestamp is still in use, 21678 * don't stomp on it. 21679 */ 21680 if ((*xmit_tail)->b_next == NULL) { 21681 (*xmit_tail)->b_prev = local_time; 21682 (*xmit_tail)->b_next = 21683 (mblk_t *)(uintptr_t)(*snxt); 21684 } 21685 } else 21686 (*xmit_tail)->b_rptr = prev_rptr; 21687 21688 if (mp == NULL) { 21689 if (ire != NULL) 21690 IRE_REFRELE(ire); 21691 return (-1); 21692 } 21693 mp1 = mp->b_cont; 21694 21695 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21696 tcp->tcp_last_sent_len = (ushort_t)len; 21697 while (mp1->b_cont) { 21698 *xmit_tail = (*xmit_tail)->b_cont; 21699 (*xmit_tail)->b_prev = local_time; 21700 (*xmit_tail)->b_next = 21701 (mblk_t *)(uintptr_t)(*snxt); 21702 mp1 = mp1->b_cont; 21703 } 21704 *snxt += len; 21705 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21706 BUMP_LOCAL(tcp->tcp_obsegs); 21707 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21708 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21709 tcp_send_data(tcp, q, mp); 21710 continue; 21711 } 21712 21713 *snxt += len; /* Adjust later if we don't send all of len */ 21714 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21715 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21716 21717 if (*tail_unsent) { 21718 /* Are the bytes above us in flight? */ 21719 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21720 if (rptr != (*xmit_tail)->b_rptr) { 21721 *tail_unsent -= len; 21722 if (len <= mss) /* LSO is unusable */ 21723 tcp->tcp_last_sent_len = (ushort_t)len; 21724 len += tcp_hdr_len; 21725 if (tcp->tcp_ipversion == IPV4_VERSION) 21726 tcp->tcp_ipha->ipha_length = htons(len); 21727 else 21728 tcp->tcp_ip6h->ip6_plen = 21729 htons(len - 21730 ((char *)&tcp->tcp_ip6h[1] - 21731 tcp->tcp_iphc)); 21732 mp = dupb(*xmit_tail); 21733 if (mp == NULL) { 21734 if (ire != NULL) 21735 IRE_REFRELE(ire); 21736 return (-1); /* out_of_mem */ 21737 } 21738 mp->b_rptr = rptr; 21739 /* 21740 * If the old timestamp is no longer in use, 21741 * sample a new timestamp now. 21742 */ 21743 if ((*xmit_tail)->b_next == NULL) { 21744 (*xmit_tail)->b_prev = local_time; 21745 (*xmit_tail)->b_next = 21746 (mblk_t *)(uintptr_t)(*snxt-len); 21747 } 21748 goto must_alloc; 21749 } 21750 } else { 21751 *xmit_tail = (*xmit_tail)->b_cont; 21752 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21753 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21754 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21755 (*xmit_tail)->b_rptr); 21756 } 21757 21758 (*xmit_tail)->b_prev = local_time; 21759 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21760 21761 *tail_unsent -= len; 21762 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21763 tcp->tcp_last_sent_len = (ushort_t)len; 21764 21765 len += tcp_hdr_len; 21766 if (tcp->tcp_ipversion == IPV4_VERSION) 21767 tcp->tcp_ipha->ipha_length = htons(len); 21768 else 21769 tcp->tcp_ip6h->ip6_plen = htons(len - 21770 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21771 21772 mp = dupb(*xmit_tail); 21773 if (mp == NULL) { 21774 if (ire != NULL) 21775 IRE_REFRELE(ire); 21776 return (-1); /* out_of_mem */ 21777 } 21778 21779 len = tcp_hdr_len; 21780 /* 21781 * There are four reasons to allocate a new hdr mblk: 21782 * 1) The bytes above us are in use by another packet 21783 * 2) We don't have good alignment 21784 * 3) The mblk is being shared 21785 * 4) We don't have enough room for a header 21786 */ 21787 rptr = mp->b_rptr - len; 21788 if (!OK_32PTR(rptr) || 21789 ((db = mp->b_datap), db->db_ref != 2) || 21790 rptr < db->db_base + ire_fp_mp_len) { 21791 /* NOTE: we assume allocb returns an OK_32PTR */ 21792 21793 must_alloc:; 21794 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21795 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21796 if (mp1 == NULL) { 21797 freemsg(mp); 21798 if (ire != NULL) 21799 IRE_REFRELE(ire); 21800 return (-1); /* out_of_mem */ 21801 } 21802 mp1->b_cont = mp; 21803 mp = mp1; 21804 /* Leave room for Link Level header */ 21805 len = tcp_hdr_len; 21806 rptr = 21807 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21808 mp->b_wptr = &rptr[len]; 21809 } 21810 21811 /* 21812 * Fill in the header using the template header, and add 21813 * options such as time-stamp, ECN and/or SACK, as needed. 21814 */ 21815 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21816 21817 mp->b_rptr = rptr; 21818 21819 if (*tail_unsent) { 21820 int spill = *tail_unsent; 21821 21822 mp1 = mp->b_cont; 21823 if (mp1 == NULL) 21824 mp1 = mp; 21825 21826 /* 21827 * If we're a little short, tack on more mblks until 21828 * there is no more spillover. 21829 */ 21830 while (spill < 0) { 21831 mblk_t *nmp; 21832 int nmpsz; 21833 21834 nmp = (*xmit_tail)->b_cont; 21835 nmpsz = MBLKL(nmp); 21836 21837 /* 21838 * Excess data in mblk; can we split it? 21839 * If MDT is enabled for the connection, 21840 * keep on splitting as this is a transient 21841 * send path. 21842 */ 21843 if (!do_lso_send && !tcp->tcp_mdt && 21844 (spill + nmpsz > 0)) { 21845 /* 21846 * Don't split if stream head was 21847 * told to break up larger writes 21848 * into smaller ones. 21849 */ 21850 if (tcp->tcp_maxpsz > 0) 21851 break; 21852 21853 /* 21854 * Next mblk is less than SMSS/2 21855 * rounded up to nearest 64-byte; 21856 * let it get sent as part of the 21857 * next segment. 21858 */ 21859 if (tcp->tcp_localnet && 21860 !tcp->tcp_cork && 21861 (nmpsz < roundup((mss >> 1), 64))) 21862 break; 21863 } 21864 21865 *xmit_tail = nmp; 21866 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21867 /* Stash for rtt use later */ 21868 (*xmit_tail)->b_prev = local_time; 21869 (*xmit_tail)->b_next = 21870 (mblk_t *)(uintptr_t)(*snxt - len); 21871 mp1->b_cont = dupb(*xmit_tail); 21872 mp1 = mp1->b_cont; 21873 21874 spill += nmpsz; 21875 if (mp1 == NULL) { 21876 *tail_unsent = spill; 21877 freemsg(mp); 21878 if (ire != NULL) 21879 IRE_REFRELE(ire); 21880 return (-1); /* out_of_mem */ 21881 } 21882 } 21883 21884 /* Trim back any surplus on the last mblk */ 21885 if (spill >= 0) { 21886 mp1->b_wptr -= spill; 21887 *tail_unsent = spill; 21888 } else { 21889 /* 21890 * We did not send everything we could in 21891 * order to remain within the b_cont limit. 21892 */ 21893 *usable -= spill; 21894 *snxt += spill; 21895 tcp->tcp_last_sent_len += spill; 21896 UPDATE_MIB(&tcps->tcps_mib, 21897 tcpOutDataBytes, spill); 21898 /* 21899 * Adjust the checksum 21900 */ 21901 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21902 sum += spill; 21903 sum = (sum >> 16) + (sum & 0xFFFF); 21904 U16_TO_ABE16(sum, tcph->th_sum); 21905 if (tcp->tcp_ipversion == IPV4_VERSION) { 21906 sum = ntohs( 21907 ((ipha_t *)rptr)->ipha_length) + 21908 spill; 21909 ((ipha_t *)rptr)->ipha_length = 21910 htons(sum); 21911 } else { 21912 sum = ntohs( 21913 ((ip6_t *)rptr)->ip6_plen) + 21914 spill; 21915 ((ip6_t *)rptr)->ip6_plen = 21916 htons(sum); 21917 } 21918 *tail_unsent = 0; 21919 } 21920 } 21921 if (tcp->tcp_ip_forward_progress) { 21922 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21923 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21924 tcp->tcp_ip_forward_progress = B_FALSE; 21925 } 21926 21927 if (do_lso_send) { 21928 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21929 num_lso_seg); 21930 tcp->tcp_obsegs += num_lso_seg; 21931 21932 TCP_STAT(tcps, tcp_lso_times); 21933 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21934 } else { 21935 tcp_send_data(tcp, q, mp); 21936 BUMP_LOCAL(tcp->tcp_obsegs); 21937 } 21938 } 21939 21940 if (ire != NULL) 21941 IRE_REFRELE(ire); 21942 return (0); 21943 } 21944 21945 /* Unlink and return any mblk that looks like it contains a MDT info */ 21946 static mblk_t * 21947 tcp_mdt_info_mp(mblk_t *mp) 21948 { 21949 mblk_t *prev_mp; 21950 21951 for (;;) { 21952 prev_mp = mp; 21953 /* no more to process? */ 21954 if ((mp = mp->b_cont) == NULL) 21955 break; 21956 21957 switch (DB_TYPE(mp)) { 21958 case M_CTL: 21959 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21960 continue; 21961 ASSERT(prev_mp != NULL); 21962 prev_mp->b_cont = mp->b_cont; 21963 mp->b_cont = NULL; 21964 return (mp); 21965 default: 21966 break; 21967 } 21968 } 21969 return (mp); 21970 } 21971 21972 /* MDT info update routine, called when IP notifies us about MDT */ 21973 static void 21974 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21975 { 21976 boolean_t prev_state; 21977 tcp_stack_t *tcps = tcp->tcp_tcps; 21978 21979 /* 21980 * IP is telling us to abort MDT on this connection? We know 21981 * this because the capability is only turned off when IP 21982 * encounters some pathological cases, e.g. link-layer change 21983 * where the new driver doesn't support MDT, or in situation 21984 * where MDT usage on the link-layer has been switched off. 21985 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21986 * if the link-layer doesn't support MDT, and if it does, it 21987 * will indicate that the feature is to be turned on. 21988 */ 21989 prev_state = tcp->tcp_mdt; 21990 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21991 if (!tcp->tcp_mdt && !first) { 21992 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21993 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21994 (void *)tcp->tcp_connp)); 21995 } 21996 21997 /* 21998 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21999 * so disable MDT otherwise. The checks are done here 22000 * and in tcp_wput_data(). 22001 */ 22002 if (tcp->tcp_mdt && 22003 (tcp->tcp_ipversion == IPV4_VERSION && 22004 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 22005 (tcp->tcp_ipversion == IPV6_VERSION && 22006 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 22007 tcp->tcp_mdt = B_FALSE; 22008 22009 if (tcp->tcp_mdt) { 22010 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 22011 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 22012 "version (%d), expected version is %d", 22013 mdt_capab->ill_mdt_version, MDT_VERSION_2); 22014 tcp->tcp_mdt = B_FALSE; 22015 return; 22016 } 22017 22018 /* 22019 * We need the driver to be able to handle at least three 22020 * spans per packet in order for tcp MDT to be utilized. 22021 * The first is for the header portion, while the rest are 22022 * needed to handle a packet that straddles across two 22023 * virtually non-contiguous buffers; a typical tcp packet 22024 * therefore consists of only two spans. Note that we take 22025 * a zero as "don't care". 22026 */ 22027 if (mdt_capab->ill_mdt_span_limit > 0 && 22028 mdt_capab->ill_mdt_span_limit < 3) { 22029 tcp->tcp_mdt = B_FALSE; 22030 return; 22031 } 22032 22033 /* a zero means driver wants default value */ 22034 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 22035 tcps->tcps_mdt_max_pbufs); 22036 if (tcp->tcp_mdt_max_pld == 0) 22037 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 22038 22039 /* ensure 32-bit alignment */ 22040 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 22041 mdt_capab->ill_mdt_hdr_head), 4); 22042 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 22043 mdt_capab->ill_mdt_hdr_tail), 4); 22044 22045 if (!first && !prev_state) { 22046 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 22047 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 22048 (void *)tcp->tcp_connp)); 22049 } 22050 } 22051 } 22052 22053 /* Unlink and return any mblk that looks like it contains a LSO info */ 22054 static mblk_t * 22055 tcp_lso_info_mp(mblk_t *mp) 22056 { 22057 mblk_t *prev_mp; 22058 22059 for (;;) { 22060 prev_mp = mp; 22061 /* no more to process? */ 22062 if ((mp = mp->b_cont) == NULL) 22063 break; 22064 22065 switch (DB_TYPE(mp)) { 22066 case M_CTL: 22067 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 22068 continue; 22069 ASSERT(prev_mp != NULL); 22070 prev_mp->b_cont = mp->b_cont; 22071 mp->b_cont = NULL; 22072 return (mp); 22073 default: 22074 break; 22075 } 22076 } 22077 22078 return (mp); 22079 } 22080 22081 /* LSO info update routine, called when IP notifies us about LSO */ 22082 static void 22083 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 22084 { 22085 tcp_stack_t *tcps = tcp->tcp_tcps; 22086 22087 /* 22088 * IP is telling us to abort LSO on this connection? We know 22089 * this because the capability is only turned off when IP 22090 * encounters some pathological cases, e.g. link-layer change 22091 * where the new NIC/driver doesn't support LSO, or in situation 22092 * where LSO usage on the link-layer has been switched off. 22093 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 22094 * if the link-layer doesn't support LSO, and if it does, it 22095 * will indicate that the feature is to be turned on. 22096 */ 22097 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 22098 TCP_STAT(tcps, tcp_lso_enabled); 22099 22100 /* 22101 * We currently only support LSO on simple TCP/IPv4, 22102 * so disable LSO otherwise. The checks are done here 22103 * and in tcp_wput_data(). 22104 */ 22105 if (tcp->tcp_lso && 22106 (tcp->tcp_ipversion == IPV4_VERSION && 22107 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 22108 (tcp->tcp_ipversion == IPV6_VERSION)) { 22109 tcp->tcp_lso = B_FALSE; 22110 TCP_STAT(tcps, tcp_lso_disabled); 22111 } else { 22112 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 22113 lso_capab->ill_lso_max); 22114 } 22115 } 22116 22117 static void 22118 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 22119 { 22120 conn_t *connp = tcp->tcp_connp; 22121 tcp_stack_t *tcps = tcp->tcp_tcps; 22122 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22123 22124 ASSERT(ire != NULL); 22125 22126 /* 22127 * We may be in the fastpath here, and although we essentially do 22128 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 22129 * we try to keep things as brief as possible. After all, these 22130 * are only best-effort checks, and we do more thorough ones prior 22131 * to calling tcp_send()/tcp_multisend(). 22132 */ 22133 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 22134 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 22135 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 22136 !(ire->ire_flags & RTF_MULTIRT) && 22137 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 22138 CONN_IS_LSO_MD_FASTPATH(connp)) { 22139 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 22140 /* Cache the result */ 22141 connp->conn_lso_ok = B_TRUE; 22142 22143 ASSERT(ill->ill_lso_capab != NULL); 22144 if (!ill->ill_lso_capab->ill_lso_on) { 22145 ill->ill_lso_capab->ill_lso_on = 1; 22146 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22147 "LSO for interface %s\n", (void *)connp, 22148 ill->ill_name)); 22149 } 22150 tcp_lso_update(tcp, ill->ill_lso_capab); 22151 } else if (ipst->ips_ip_multidata_outbound && 22152 ILL_MDT_CAPABLE(ill)) { 22153 /* Cache the result */ 22154 connp->conn_mdt_ok = B_TRUE; 22155 22156 ASSERT(ill->ill_mdt_capab != NULL); 22157 if (!ill->ill_mdt_capab->ill_mdt_on) { 22158 ill->ill_mdt_capab->ill_mdt_on = 1; 22159 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22160 "MDT for interface %s\n", (void *)connp, 22161 ill->ill_name)); 22162 } 22163 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 22164 } 22165 } 22166 22167 /* 22168 * The goal is to reduce the number of generated tcp segments by 22169 * setting the maxpsz multiplier to 0; this will have an affect on 22170 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 22171 * into each packet, up to SMSS bytes. Doing this reduces the number 22172 * of outbound segments and incoming ACKs, thus allowing for better 22173 * network and system performance. In contrast the legacy behavior 22174 * may result in sending less than SMSS size, because the last mblk 22175 * for some packets may have more data than needed to make up SMSS, 22176 * and the legacy code refused to "split" it. 22177 * 22178 * We apply the new behavior on following situations: 22179 * 22180 * 1) Loopback connections, 22181 * 2) Connections in which the remote peer is not on local subnet, 22182 * 3) Local subnet connections over the bge interface (see below). 22183 * 22184 * Ideally, we would like this behavior to apply for interfaces other 22185 * than bge. However, doing so would negatively impact drivers which 22186 * perform dynamic mapping and unmapping of DMA resources, which are 22187 * increased by setting the maxpsz multiplier to 0 (more mblks per 22188 * packet will be generated by tcp). The bge driver does not suffer 22189 * from this, as it copies the mblks into pre-mapped buffers, and 22190 * therefore does not require more I/O resources than before. 22191 * 22192 * Otherwise, this behavior is present on all network interfaces when 22193 * the destination endpoint is non-local, since reducing the number 22194 * of packets in general is good for the network. 22195 * 22196 * TODO We need to remove this hard-coded conditional for bge once 22197 * a better "self-tuning" mechanism, or a way to comprehend 22198 * the driver transmit strategy is devised. Until the solution 22199 * is found and well understood, we live with this hack. 22200 */ 22201 if (!tcp_static_maxpsz && 22202 (tcp->tcp_loopback || !tcp->tcp_localnet || 22203 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 22204 /* override the default value */ 22205 tcp->tcp_maxpsz = 0; 22206 22207 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 22208 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 22209 ill != NULL ? ill->ill_name : ipif_loopback_name)); 22210 } 22211 22212 /* set the stream head parameters accordingly */ 22213 (void) tcp_maxpsz_set(tcp, B_TRUE); 22214 } 22215 22216 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 22217 static void 22218 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 22219 { 22220 uchar_t fval = *mp->b_rptr; 22221 mblk_t *tail; 22222 queue_t *q = tcp->tcp_wq; 22223 22224 /* TODO: How should flush interact with urgent data? */ 22225 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 22226 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 22227 /* 22228 * Flush only data that has not yet been put on the wire. If 22229 * we flush data that we have already transmitted, life, as we 22230 * know it, may come to an end. 22231 */ 22232 tail = tcp->tcp_xmit_tail; 22233 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 22234 tcp->tcp_xmit_tail_unsent = 0; 22235 tcp->tcp_unsent = 0; 22236 if (tail->b_wptr != tail->b_rptr) 22237 tail = tail->b_cont; 22238 if (tail) { 22239 mblk_t **excess = &tcp->tcp_xmit_head; 22240 for (;;) { 22241 mblk_t *mp1 = *excess; 22242 if (mp1 == tail) 22243 break; 22244 tcp->tcp_xmit_tail = mp1; 22245 tcp->tcp_xmit_last = mp1; 22246 excess = &mp1->b_cont; 22247 } 22248 *excess = NULL; 22249 tcp_close_mpp(&tail); 22250 if (tcp->tcp_snd_zcopy_aware) 22251 tcp_zcopy_notify(tcp); 22252 } 22253 /* 22254 * We have no unsent data, so unsent must be less than 22255 * tcp_xmit_lowater, so re-enable flow. 22256 */ 22257 mutex_enter(&tcp->tcp_non_sq_lock); 22258 if (tcp->tcp_flow_stopped) { 22259 tcp_clrqfull(tcp); 22260 } 22261 mutex_exit(&tcp->tcp_non_sq_lock); 22262 } 22263 /* 22264 * TODO: you can't just flush these, you have to increase rwnd for one 22265 * thing. For another, how should urgent data interact? 22266 */ 22267 if (fval & FLUSHR) { 22268 *mp->b_rptr = fval & ~FLUSHW; 22269 /* XXX */ 22270 qreply(q, mp); 22271 return; 22272 } 22273 freemsg(mp); 22274 } 22275 22276 /* 22277 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 22278 * messages. 22279 */ 22280 static void 22281 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 22282 { 22283 mblk_t *mp1; 22284 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 22285 STRUCT_HANDLE(strbuf, sb); 22286 queue_t *q = tcp->tcp_wq; 22287 int error; 22288 uint_t addrlen; 22289 22290 /* Make sure it is one of ours. */ 22291 switch (iocp->ioc_cmd) { 22292 case TI_GETMYNAME: 22293 case TI_GETPEERNAME: 22294 break; 22295 default: 22296 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 22297 return; 22298 } 22299 switch (mi_copy_state(q, mp, &mp1)) { 22300 case -1: 22301 return; 22302 case MI_COPY_CASE(MI_COPY_IN, 1): 22303 break; 22304 case MI_COPY_CASE(MI_COPY_OUT, 1): 22305 /* Copy out the strbuf. */ 22306 mi_copyout(q, mp); 22307 return; 22308 case MI_COPY_CASE(MI_COPY_OUT, 2): 22309 /* All done. */ 22310 mi_copy_done(q, mp, 0); 22311 return; 22312 default: 22313 mi_copy_done(q, mp, EPROTO); 22314 return; 22315 } 22316 /* Check alignment of the strbuf */ 22317 if (!OK_32PTR(mp1->b_rptr)) { 22318 mi_copy_done(q, mp, EINVAL); 22319 return; 22320 } 22321 22322 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 22323 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 22324 if (STRUCT_FGET(sb, maxlen) < addrlen) { 22325 mi_copy_done(q, mp, EINVAL); 22326 return; 22327 } 22328 22329 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 22330 if (mp1 == NULL) 22331 return; 22332 22333 switch (iocp->ioc_cmd) { 22334 case TI_GETMYNAME: 22335 error = tcp_getmyname(tcp, (void *)mp1->b_rptr, &addrlen); 22336 break; 22337 case TI_GETPEERNAME: 22338 error = tcp_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 22339 break; 22340 } 22341 22342 if (error != 0) { 22343 mi_copy_done(q, mp, error); 22344 } else { 22345 mp1->b_wptr += addrlen; 22346 STRUCT_FSET(sb, len, addrlen); 22347 22348 /* Copy out the address */ 22349 mi_copyout(q, mp); 22350 } 22351 } 22352 22353 /* 22354 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 22355 * messages. 22356 */ 22357 /* ARGSUSED */ 22358 static void 22359 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 22360 { 22361 conn_t *connp = (conn_t *)arg; 22362 tcp_t *tcp = connp->conn_tcp; 22363 queue_t *q = tcp->tcp_wq; 22364 struct iocblk *iocp; 22365 tcp_stack_t *tcps = tcp->tcp_tcps; 22366 22367 ASSERT(DB_TYPE(mp) == M_IOCTL); 22368 /* 22369 * Try and ASSERT the minimum possible references on the 22370 * conn early enough. Since we are executing on write side, 22371 * the connection is obviously not detached and that means 22372 * there is a ref each for TCP and IP. Since we are behind 22373 * the squeue, the minimum references needed are 3. If the 22374 * conn is in classifier hash list, there should be an 22375 * extra ref for that (we check both the possibilities). 22376 */ 22377 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22378 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22379 22380 iocp = (struct iocblk *)mp->b_rptr; 22381 switch (iocp->ioc_cmd) { 22382 case TCP_IOC_DEFAULT_Q: 22383 /* Wants to be the default wq. */ 22384 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 22385 iocp->ioc_error = EPERM; 22386 iocp->ioc_count = 0; 22387 mp->b_datap->db_type = M_IOCACK; 22388 qreply(q, mp); 22389 return; 22390 } 22391 tcp_def_q_set(tcp, mp); 22392 return; 22393 case _SIOCSOCKFALLBACK: 22394 /* 22395 * Either sockmod is about to be popped and the socket 22396 * would now be treated as a plain stream, or a module 22397 * is about to be pushed so we could no longer use read- 22398 * side synchronous streams for fused loopback tcp. 22399 * Drain any queued data and disable direct sockfs 22400 * interface from now on. 22401 */ 22402 if (!tcp->tcp_issocket) { 22403 DB_TYPE(mp) = M_IOCNAK; 22404 iocp->ioc_error = EINVAL; 22405 } else { 22406 #ifdef _ILP32 22407 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 22408 #else 22409 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 22410 #endif 22411 /* 22412 * Insert this socket into the acceptor hash. 22413 * We might need it for T_CONN_RES message 22414 */ 22415 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 22416 22417 if (tcp->tcp_fused) { 22418 /* 22419 * This is a fused loopback tcp; disable 22420 * read-side synchronous streams interface 22421 * and drain any queued data. It is okay 22422 * to do this for non-synchronous streams 22423 * fused tcp as well. 22424 */ 22425 tcp_fuse_disable_pair(tcp, B_FALSE); 22426 } 22427 tcp->tcp_issocket = B_FALSE; 22428 tcp->tcp_sodirect = NULL; 22429 TCP_STAT(tcps, tcp_sock_fallback); 22430 22431 DB_TYPE(mp) = M_IOCACK; 22432 iocp->ioc_error = 0; 22433 } 22434 iocp->ioc_count = 0; 22435 iocp->ioc_rval = 0; 22436 qreply(q, mp); 22437 return; 22438 } 22439 CALL_IP_WPUT(connp, q, mp); 22440 } 22441 22442 /* 22443 * This routine is called by tcp_wput() to handle all TPI requests. 22444 */ 22445 /* ARGSUSED */ 22446 static void 22447 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22448 { 22449 conn_t *connp = (conn_t *)arg; 22450 tcp_t *tcp = connp->conn_tcp; 22451 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22452 uchar_t *rptr; 22453 t_scalar_t type; 22454 int len; 22455 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 22456 22457 /* 22458 * Try and ASSERT the minimum possible references on the 22459 * conn early enough. Since we are executing on write side, 22460 * the connection is obviously not detached and that means 22461 * there is a ref each for TCP and IP. Since we are behind 22462 * the squeue, the minimum references needed are 3. If the 22463 * conn is in classifier hash list, there should be an 22464 * extra ref for that (we check both the possibilities). 22465 */ 22466 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22467 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22468 22469 rptr = mp->b_rptr; 22470 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22471 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22472 type = ((union T_primitives *)rptr)->type; 22473 if (type == T_EXDATA_REQ) { 22474 uint32_t msize = msgdsize(mp->b_cont); 22475 22476 len = msize - 1; 22477 if (len < 0) { 22478 freemsg(mp); 22479 return; 22480 } 22481 /* 22482 * Try to force urgent data out on the wire. 22483 * Even if we have unsent data this will 22484 * at least send the urgent flag. 22485 * XXX does not handle more flag correctly. 22486 */ 22487 len += tcp->tcp_unsent; 22488 len += tcp->tcp_snxt; 22489 tcp->tcp_urg = len; 22490 tcp->tcp_valid_bits |= TCP_URG_VALID; 22491 22492 /* Bypass tcp protocol for fused tcp loopback */ 22493 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 22494 return; 22495 } else if (type != T_DATA_REQ) { 22496 goto non_urgent_data; 22497 } 22498 /* TODO: options, flags, ... from user */ 22499 /* Set length to zero for reclamation below */ 22500 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22501 freeb(mp); 22502 return; 22503 } else { 22504 if (tcp->tcp_debug) { 22505 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22506 "tcp_wput_proto, dropping one..."); 22507 } 22508 freemsg(mp); 22509 return; 22510 } 22511 22512 non_urgent_data: 22513 22514 switch ((int)tprim->type) { 22515 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22516 /* 22517 * save the kssl_ent_t from the next block, and convert this 22518 * back to a normal bind_req. 22519 */ 22520 if (mp->b_cont != NULL) { 22521 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22522 22523 if (tcp->tcp_kssl_ent != NULL) { 22524 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22525 KSSL_NO_PROXY); 22526 tcp->tcp_kssl_ent = NULL; 22527 } 22528 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22529 sizeof (kssl_ent_t)); 22530 kssl_hold_ent(tcp->tcp_kssl_ent); 22531 freemsg(mp->b_cont); 22532 mp->b_cont = NULL; 22533 } 22534 tprim->type = T_BIND_REQ; 22535 22536 /* FALLTHROUGH */ 22537 case O_T_BIND_REQ: /* bind request */ 22538 case T_BIND_REQ: /* new semantics bind request */ 22539 tcp_bind(tcp, mp); 22540 break; 22541 case T_UNBIND_REQ: /* unbind request */ 22542 tcp_unbind(tcp, mp); 22543 break; 22544 case O_T_CONN_RES: /* old connection response XXX */ 22545 case T_CONN_RES: /* connection response */ 22546 tcp_accept(tcp, mp); 22547 break; 22548 case T_CONN_REQ: /* connection request */ 22549 tcp_connect(tcp, mp); 22550 break; 22551 case T_DISCON_REQ: /* disconnect request */ 22552 tcp_disconnect(tcp, mp); 22553 break; 22554 case T_CAPABILITY_REQ: 22555 tcp_capability_req(tcp, mp); /* capability request */ 22556 break; 22557 case T_INFO_REQ: /* information request */ 22558 tcp_info_req(tcp, mp); 22559 break; 22560 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22561 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, 22562 &tcp_opt_obj, B_TRUE); 22563 break; 22564 case T_OPTMGMT_REQ: 22565 /* 22566 * Note: no support for snmpcom_req() through new 22567 * T_OPTMGMT_REQ. See comments in ip.c 22568 */ 22569 /* Only IP is allowed to return meaningful value */ 22570 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22571 B_TRUE); 22572 break; 22573 22574 case T_UNITDATA_REQ: /* unitdata request */ 22575 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22576 break; 22577 case T_ORDREL_REQ: /* orderly release req */ 22578 freemsg(mp); 22579 22580 if (tcp->tcp_fused) 22581 tcp_unfuse(tcp); 22582 22583 if (tcp_xmit_end(tcp) != 0) { 22584 /* 22585 * We were crossing FINs and got a reset from 22586 * the other side. Just ignore it. 22587 */ 22588 if (tcp->tcp_debug) { 22589 (void) strlog(TCP_MOD_ID, 0, 1, 22590 SL_ERROR|SL_TRACE, 22591 "tcp_wput_proto, T_ORDREL_REQ out of " 22592 "state %s", 22593 tcp_display(tcp, NULL, 22594 DISP_ADDR_AND_PORT)); 22595 } 22596 } 22597 break; 22598 case T_ADDR_REQ: 22599 tcp_addr_req(tcp, mp); 22600 break; 22601 default: 22602 if (tcp->tcp_debug) { 22603 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22604 "tcp_wput_proto, bogus TPI msg, type %d", 22605 tprim->type); 22606 } 22607 /* 22608 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22609 * to recover. 22610 */ 22611 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22612 break; 22613 } 22614 } 22615 22616 /* 22617 * The TCP write service routine should never be called... 22618 */ 22619 /* ARGSUSED */ 22620 static void 22621 tcp_wsrv(queue_t *q) 22622 { 22623 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22624 22625 TCP_STAT(tcps, tcp_wsrv_called); 22626 } 22627 22628 /* Non overlapping byte exchanger */ 22629 static void 22630 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22631 { 22632 uchar_t uch; 22633 22634 while (len-- > 0) { 22635 uch = a[len]; 22636 a[len] = b[len]; 22637 b[len] = uch; 22638 } 22639 } 22640 22641 /* 22642 * Send out a control packet on the tcp connection specified. This routine 22643 * is typically called where we need a simple ACK or RST generated. 22644 */ 22645 static void 22646 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22647 { 22648 uchar_t *rptr; 22649 tcph_t *tcph; 22650 ipha_t *ipha = NULL; 22651 ip6_t *ip6h = NULL; 22652 uint32_t sum; 22653 int tcp_hdr_len; 22654 int tcp_ip_hdr_len; 22655 mblk_t *mp; 22656 tcp_stack_t *tcps = tcp->tcp_tcps; 22657 22658 /* 22659 * Save sum for use in source route later. 22660 */ 22661 ASSERT(tcp != NULL); 22662 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22663 tcp_hdr_len = tcp->tcp_hdr_len; 22664 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22665 22666 /* If a text string is passed in with the request, pass it to strlog. */ 22667 if (str != NULL && tcp->tcp_debug) { 22668 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22669 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22670 str, seq, ack, ctl); 22671 } 22672 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22673 BPRI_MED); 22674 if (mp == NULL) { 22675 return; 22676 } 22677 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22678 mp->b_rptr = rptr; 22679 mp->b_wptr = &rptr[tcp_hdr_len]; 22680 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22681 22682 if (tcp->tcp_ipversion == IPV4_VERSION) { 22683 ipha = (ipha_t *)rptr; 22684 ipha->ipha_length = htons(tcp_hdr_len); 22685 } else { 22686 ip6h = (ip6_t *)rptr; 22687 ASSERT(tcp != NULL); 22688 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22689 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22690 } 22691 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22692 tcph->th_flags[0] = (uint8_t)ctl; 22693 if (ctl & TH_RST) { 22694 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22695 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22696 /* 22697 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22698 */ 22699 if (tcp->tcp_snd_ts_ok && 22700 tcp->tcp_state > TCPS_SYN_SENT) { 22701 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22702 *(mp->b_wptr) = TCPOPT_EOL; 22703 if (tcp->tcp_ipversion == IPV4_VERSION) { 22704 ipha->ipha_length = htons(tcp_hdr_len - 22705 TCPOPT_REAL_TS_LEN); 22706 } else { 22707 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22708 TCPOPT_REAL_TS_LEN); 22709 } 22710 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22711 sum -= TCPOPT_REAL_TS_LEN; 22712 } 22713 } 22714 if (ctl & TH_ACK) { 22715 if (tcp->tcp_snd_ts_ok) { 22716 U32_TO_BE32(lbolt, 22717 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22718 U32_TO_BE32(tcp->tcp_ts_recent, 22719 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22720 } 22721 22722 /* Update the latest receive window size in TCP header. */ 22723 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22724 tcph->th_win); 22725 tcp->tcp_rack = ack; 22726 tcp->tcp_rack_cnt = 0; 22727 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22728 } 22729 BUMP_LOCAL(tcp->tcp_obsegs); 22730 U32_TO_BE32(seq, tcph->th_seq); 22731 U32_TO_BE32(ack, tcph->th_ack); 22732 /* 22733 * Include the adjustment for a source route if any. 22734 */ 22735 sum = (sum >> 16) + (sum & 0xFFFF); 22736 U16_TO_BE16(sum, tcph->th_sum); 22737 tcp_send_data(tcp, tcp->tcp_wq, mp); 22738 } 22739 22740 /* 22741 * If this routine returns B_TRUE, TCP can generate a RST in response 22742 * to a segment. If it returns B_FALSE, TCP should not respond. 22743 */ 22744 static boolean_t 22745 tcp_send_rst_chk(tcp_stack_t *tcps) 22746 { 22747 clock_t now; 22748 22749 /* 22750 * TCP needs to protect itself from generating too many RSTs. 22751 * This can be a DoS attack by sending us random segments 22752 * soliciting RSTs. 22753 * 22754 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22755 * in each 1 second interval. In this way, TCP still generate 22756 * RSTs in normal cases but when under attack, the impact is 22757 * limited. 22758 */ 22759 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22760 now = lbolt; 22761 /* lbolt can wrap around. */ 22762 if ((tcps->tcps_last_rst_intrvl > now) || 22763 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22764 1*SECONDS)) { 22765 tcps->tcps_last_rst_intrvl = now; 22766 tcps->tcps_rst_cnt = 1; 22767 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22768 return (B_FALSE); 22769 } 22770 } 22771 return (B_TRUE); 22772 } 22773 22774 /* 22775 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22776 */ 22777 static void 22778 tcp_ip_ire_mark_advice(tcp_t *tcp) 22779 { 22780 mblk_t *mp; 22781 ipic_t *ipic; 22782 22783 if (tcp->tcp_ipversion == IPV4_VERSION) { 22784 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22785 &ipic); 22786 } else { 22787 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22788 &ipic); 22789 } 22790 if (mp == NULL) 22791 return; 22792 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22793 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22794 } 22795 22796 /* 22797 * Return an IP advice ioctl mblk and set ipic to be the pointer 22798 * to the advice structure. 22799 */ 22800 static mblk_t * 22801 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22802 { 22803 struct iocblk *ioc; 22804 mblk_t *mp, *mp1; 22805 22806 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22807 if (mp == NULL) 22808 return (NULL); 22809 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22810 *ipic = (ipic_t *)mp->b_rptr; 22811 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22812 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22813 22814 bcopy(addr, *ipic + 1, addr_len); 22815 22816 (*ipic)->ipic_addr_length = addr_len; 22817 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22818 22819 mp1 = mkiocb(IP_IOCTL); 22820 if (mp1 == NULL) { 22821 freemsg(mp); 22822 return (NULL); 22823 } 22824 mp1->b_cont = mp; 22825 ioc = (struct iocblk *)mp1->b_rptr; 22826 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22827 22828 return (mp1); 22829 } 22830 22831 /* 22832 * Generate a reset based on an inbound packet, connp is set by caller 22833 * when RST is in response to an unexpected inbound packet for which 22834 * there is active tcp state in the system. 22835 * 22836 * IPSEC NOTE : Try to send the reply with the same protection as it came 22837 * in. We still have the ipsec_mp that the packet was attached to. Thus 22838 * the packet will go out at the same level of protection as it came in by 22839 * converting the IPSEC_IN to IPSEC_OUT. 22840 */ 22841 static void 22842 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22843 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22844 tcp_stack_t *tcps, conn_t *connp) 22845 { 22846 ipha_t *ipha = NULL; 22847 ip6_t *ip6h = NULL; 22848 ushort_t len; 22849 tcph_t *tcph; 22850 int i; 22851 mblk_t *ipsec_mp; 22852 boolean_t mctl_present; 22853 ipic_t *ipic; 22854 ipaddr_t v4addr; 22855 in6_addr_t v6addr; 22856 int addr_len; 22857 void *addr; 22858 queue_t *q = tcps->tcps_g_q; 22859 tcp_t *tcp; 22860 cred_t *cr; 22861 mblk_t *nmp; 22862 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22863 22864 if (tcps->tcps_g_q == NULL) { 22865 /* 22866 * For non-zero stackids the default queue isn't created 22867 * until the first open, thus there can be a need to send 22868 * a reset before then. But we can't do that, hence we just 22869 * drop the packet. Later during boot, when the default queue 22870 * has been setup, a retransmitted packet from the peer 22871 * will result in a reset. 22872 */ 22873 ASSERT(tcps->tcps_netstack->netstack_stackid != 22874 GLOBAL_NETSTACKID); 22875 freemsg(mp); 22876 return; 22877 } 22878 22879 if (connp != NULL) 22880 tcp = connp->conn_tcp; 22881 else 22882 tcp = Q_TO_TCP(q); 22883 22884 if (!tcp_send_rst_chk(tcps)) { 22885 tcps->tcps_rst_unsent++; 22886 freemsg(mp); 22887 return; 22888 } 22889 22890 if (mp->b_datap->db_type == M_CTL) { 22891 ipsec_mp = mp; 22892 mp = mp->b_cont; 22893 mctl_present = B_TRUE; 22894 } else { 22895 ipsec_mp = mp; 22896 mctl_present = B_FALSE; 22897 } 22898 22899 if (str && q && tcps->tcps_dbg) { 22900 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22901 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22902 "flags 0x%x", 22903 str, seq, ack, ctl); 22904 } 22905 if (mp->b_datap->db_ref != 1) { 22906 mblk_t *mp1 = copyb(mp); 22907 freemsg(mp); 22908 mp = mp1; 22909 if (!mp) { 22910 if (mctl_present) 22911 freeb(ipsec_mp); 22912 return; 22913 } else { 22914 if (mctl_present) { 22915 ipsec_mp->b_cont = mp; 22916 } else { 22917 ipsec_mp = mp; 22918 } 22919 } 22920 } else if (mp->b_cont) { 22921 freemsg(mp->b_cont); 22922 mp->b_cont = NULL; 22923 } 22924 /* 22925 * We skip reversing source route here. 22926 * (for now we replace all IP options with EOL) 22927 */ 22928 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22929 ipha = (ipha_t *)mp->b_rptr; 22930 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22931 mp->b_rptr[i] = IPOPT_EOL; 22932 /* 22933 * Make sure that src address isn't flagrantly invalid. 22934 * Not all broadcast address checking for the src address 22935 * is possible, since we don't know the netmask of the src 22936 * addr. No check for destination address is done, since 22937 * IP will not pass up a packet with a broadcast dest 22938 * address to TCP. Similar checks are done below for IPv6. 22939 */ 22940 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22941 CLASSD(ipha->ipha_src)) { 22942 freemsg(ipsec_mp); 22943 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22944 return; 22945 } 22946 } else { 22947 ip6h = (ip6_t *)mp->b_rptr; 22948 22949 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22950 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22951 freemsg(ipsec_mp); 22952 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22953 return; 22954 } 22955 22956 /* Remove any extension headers assuming partial overlay */ 22957 if (ip_hdr_len > IPV6_HDR_LEN) { 22958 uint8_t *to; 22959 22960 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22961 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22962 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22963 ip_hdr_len = IPV6_HDR_LEN; 22964 ip6h = (ip6_t *)mp->b_rptr; 22965 ip6h->ip6_nxt = IPPROTO_TCP; 22966 } 22967 } 22968 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22969 if (tcph->th_flags[0] & TH_RST) { 22970 freemsg(ipsec_mp); 22971 return; 22972 } 22973 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22974 len = ip_hdr_len + sizeof (tcph_t); 22975 mp->b_wptr = &mp->b_rptr[len]; 22976 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22977 ipha->ipha_length = htons(len); 22978 /* Swap addresses */ 22979 v4addr = ipha->ipha_src; 22980 ipha->ipha_src = ipha->ipha_dst; 22981 ipha->ipha_dst = v4addr; 22982 ipha->ipha_ident = 0; 22983 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22984 addr_len = IP_ADDR_LEN; 22985 addr = &v4addr; 22986 } else { 22987 /* No ip6i_t in this case */ 22988 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22989 /* Swap addresses */ 22990 v6addr = ip6h->ip6_src; 22991 ip6h->ip6_src = ip6h->ip6_dst; 22992 ip6h->ip6_dst = v6addr; 22993 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22994 addr_len = IPV6_ADDR_LEN; 22995 addr = &v6addr; 22996 } 22997 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22998 U32_TO_BE32(ack, tcph->th_ack); 22999 U32_TO_BE32(seq, tcph->th_seq); 23000 U16_TO_BE16(0, tcph->th_win); 23001 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 23002 tcph->th_flags[0] = (uint8_t)ctl; 23003 if (ctl & TH_RST) { 23004 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 23005 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23006 } 23007 23008 /* IP trusts us to set up labels when required. */ 23009 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 23010 crgetlabel(cr) != NULL) { 23011 int err; 23012 23013 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 23014 err = tsol_check_label(cr, &mp, 23015 tcp->tcp_connp->conn_mac_exempt, 23016 tcps->tcps_netstack->netstack_ip); 23017 else 23018 err = tsol_check_label_v6(cr, &mp, 23019 tcp->tcp_connp->conn_mac_exempt, 23020 tcps->tcps_netstack->netstack_ip); 23021 if (mctl_present) 23022 ipsec_mp->b_cont = mp; 23023 else 23024 ipsec_mp = mp; 23025 if (err != 0) { 23026 freemsg(ipsec_mp); 23027 return; 23028 } 23029 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23030 ipha = (ipha_t *)mp->b_rptr; 23031 } else { 23032 ip6h = (ip6_t *)mp->b_rptr; 23033 } 23034 } 23035 23036 if (mctl_present) { 23037 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23038 23039 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23040 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 23041 return; 23042 } 23043 } 23044 if (zoneid == ALL_ZONES) 23045 zoneid = GLOBAL_ZONEID; 23046 23047 /* Add the zoneid so ip_output routes it properly */ 23048 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 23049 freemsg(ipsec_mp); 23050 return; 23051 } 23052 ipsec_mp = nmp; 23053 23054 /* 23055 * NOTE: one might consider tracing a TCP packet here, but 23056 * this function has no active TCP state and no tcp structure 23057 * that has a trace buffer. If we traced here, we would have 23058 * to keep a local trace buffer in tcp_record_trace(). 23059 * 23060 * TSol note: The mblk that contains the incoming packet was 23061 * reused by tcp_xmit_listener_reset, so it already contains 23062 * the right credentials and we don't need to call mblk_setcred. 23063 * Also the conn's cred is not right since it is associated 23064 * with tcps_g_q. 23065 */ 23066 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 23067 23068 /* 23069 * Tell IP to mark the IRE used for this destination temporary. 23070 * This way, we can limit our exposure to DoS attack because IP 23071 * creates an IRE for each destination. If there are too many, 23072 * the time to do any routing lookup will be extremely long. And 23073 * the lookup can be in interrupt context. 23074 * 23075 * Note that in normal circumstances, this marking should not 23076 * affect anything. It would be nice if only 1 message is 23077 * needed to inform IP that the IRE created for this RST should 23078 * not be added to the cache table. But there is currently 23079 * not such communication mechanism between TCP and IP. So 23080 * the best we can do now is to send the advice ioctl to IP 23081 * to mark the IRE temporary. 23082 */ 23083 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 23084 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 23085 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23086 } 23087 } 23088 23089 /* 23090 * Initiate closedown sequence on an active connection. (May be called as 23091 * writer.) Return value zero for OK return, non-zero for error return. 23092 */ 23093 static int 23094 tcp_xmit_end(tcp_t *tcp) 23095 { 23096 ipic_t *ipic; 23097 mblk_t *mp; 23098 tcp_stack_t *tcps = tcp->tcp_tcps; 23099 23100 if (tcp->tcp_state < TCPS_SYN_RCVD || 23101 tcp->tcp_state > TCPS_CLOSE_WAIT) { 23102 /* 23103 * Invalid state, only states TCPS_SYN_RCVD, 23104 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 23105 */ 23106 return (-1); 23107 } 23108 23109 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 23110 tcp->tcp_valid_bits |= TCP_FSS_VALID; 23111 /* 23112 * If there is nothing more unsent, send the FIN now. 23113 * Otherwise, it will go out with the last segment. 23114 */ 23115 if (tcp->tcp_unsent == 0) { 23116 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 23117 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 23118 23119 if (mp) { 23120 tcp_send_data(tcp, tcp->tcp_wq, mp); 23121 } else { 23122 /* 23123 * Couldn't allocate msg. Pretend we got it out. 23124 * Wait for rexmit timeout. 23125 */ 23126 tcp->tcp_snxt = tcp->tcp_fss + 1; 23127 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23128 } 23129 23130 /* 23131 * If needed, update tcp_rexmit_snxt as tcp_snxt is 23132 * changed. 23133 */ 23134 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 23135 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 23136 } 23137 } else { 23138 /* 23139 * If tcp->tcp_cork is set, then the data will not get sent, 23140 * so we have to check that and unset it first. 23141 */ 23142 if (tcp->tcp_cork) 23143 tcp->tcp_cork = B_FALSE; 23144 tcp_wput_data(tcp, NULL, B_FALSE); 23145 } 23146 23147 /* 23148 * If TCP does not get enough samples of RTT or tcp_rtt_updates 23149 * is 0, don't update the cache. 23150 */ 23151 if (tcps->tcps_rtt_updates == 0 || 23152 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 23153 return (0); 23154 23155 /* 23156 * NOTE: should not update if source routes i.e. if tcp_remote if 23157 * different from the destination. 23158 */ 23159 if (tcp->tcp_ipversion == IPV4_VERSION) { 23160 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 23161 return (0); 23162 } 23163 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 23164 &ipic); 23165 } else { 23166 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 23167 &tcp->tcp_ip6h->ip6_dst))) { 23168 return (0); 23169 } 23170 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 23171 &ipic); 23172 } 23173 23174 /* Record route attributes in the IRE for use by future connections. */ 23175 if (mp == NULL) 23176 return (0); 23177 23178 /* 23179 * We do not have a good algorithm to update ssthresh at this time. 23180 * So don't do any update. 23181 */ 23182 ipic->ipic_rtt = tcp->tcp_rtt_sa; 23183 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 23184 23185 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23186 return (0); 23187 } 23188 23189 /* 23190 * Generate a "no listener here" RST in response to an "unknown" segment. 23191 * connp is set by caller when RST is in response to an unexpected 23192 * inbound packet for which there is active tcp state in the system. 23193 * Note that we are reusing the incoming mp to construct the outgoing RST. 23194 */ 23195 void 23196 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 23197 tcp_stack_t *tcps, conn_t *connp) 23198 { 23199 uchar_t *rptr; 23200 uint32_t seg_len; 23201 tcph_t *tcph; 23202 uint32_t seg_seq; 23203 uint32_t seg_ack; 23204 uint_t flags; 23205 mblk_t *ipsec_mp; 23206 ipha_t *ipha; 23207 ip6_t *ip6h; 23208 boolean_t mctl_present = B_FALSE; 23209 boolean_t check = B_TRUE; 23210 boolean_t policy_present; 23211 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 23212 23213 TCP_STAT(tcps, tcp_no_listener); 23214 23215 ipsec_mp = mp; 23216 23217 if (mp->b_datap->db_type == M_CTL) { 23218 ipsec_in_t *ii; 23219 23220 mctl_present = B_TRUE; 23221 mp = mp->b_cont; 23222 23223 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23224 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23225 if (ii->ipsec_in_dont_check) { 23226 check = B_FALSE; 23227 if (!ii->ipsec_in_secure) { 23228 freeb(ipsec_mp); 23229 mctl_present = B_FALSE; 23230 ipsec_mp = mp; 23231 } 23232 } 23233 } 23234 23235 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23236 policy_present = ipss->ipsec_inbound_v4_policy_present; 23237 ipha = (ipha_t *)mp->b_rptr; 23238 ip6h = NULL; 23239 } else { 23240 policy_present = ipss->ipsec_inbound_v6_policy_present; 23241 ipha = NULL; 23242 ip6h = (ip6_t *)mp->b_rptr; 23243 } 23244 23245 if (check && policy_present) { 23246 /* 23247 * The conn_t parameter is NULL because we already know 23248 * nobody's home. 23249 */ 23250 ipsec_mp = ipsec_check_global_policy( 23251 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 23252 tcps->tcps_netstack); 23253 if (ipsec_mp == NULL) 23254 return; 23255 } 23256 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 23257 DTRACE_PROBE2( 23258 tx__ip__log__error__nolistener__tcp, 23259 char *, "Could not reply with RST to mp(1)", 23260 mblk_t *, mp); 23261 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 23262 freemsg(ipsec_mp); 23263 return; 23264 } 23265 23266 rptr = mp->b_rptr; 23267 23268 tcph = (tcph_t *)&rptr[ip_hdr_len]; 23269 seg_seq = BE32_TO_U32(tcph->th_seq); 23270 seg_ack = BE32_TO_U32(tcph->th_ack); 23271 flags = tcph->th_flags[0]; 23272 23273 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 23274 if (flags & TH_RST) { 23275 freemsg(ipsec_mp); 23276 } else if (flags & TH_ACK) { 23277 tcp_xmit_early_reset("no tcp, reset", 23278 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 23279 connp); 23280 } else { 23281 if (flags & TH_SYN) { 23282 seg_len++; 23283 } else { 23284 /* 23285 * Here we violate the RFC. Note that a normal 23286 * TCP will never send a segment without the ACK 23287 * flag, except for RST or SYN segment. This 23288 * segment is neither. Just drop it on the 23289 * floor. 23290 */ 23291 freemsg(ipsec_mp); 23292 tcps->tcps_rst_unsent++; 23293 return; 23294 } 23295 23296 tcp_xmit_early_reset("no tcp, reset/ack", 23297 ipsec_mp, 0, seg_seq + seg_len, 23298 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 23299 } 23300 } 23301 23302 /* 23303 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 23304 * ip and tcp header ready to pass down to IP. If the mp passed in is 23305 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 23306 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 23307 * otherwise it will dup partial mblks.) 23308 * Otherwise, an appropriate ACK packet will be generated. This 23309 * routine is not usually called to send new data for the first time. It 23310 * is mostly called out of the timer for retransmits, and to generate ACKs. 23311 * 23312 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 23313 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 23314 * of the original mblk chain will be returned in *offset and *end_mp. 23315 */ 23316 mblk_t * 23317 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 23318 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 23319 boolean_t rexmit) 23320 { 23321 int data_length; 23322 int32_t off = 0; 23323 uint_t flags; 23324 mblk_t *mp1; 23325 mblk_t *mp2; 23326 uchar_t *rptr; 23327 tcph_t *tcph; 23328 int32_t num_sack_blk = 0; 23329 int32_t sack_opt_len = 0; 23330 tcp_stack_t *tcps = tcp->tcp_tcps; 23331 23332 /* Allocate for our maximum TCP header + link-level */ 23333 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 23334 tcps->tcps_wroff_xtra, BPRI_MED); 23335 if (!mp1) 23336 return (NULL); 23337 data_length = 0; 23338 23339 /* 23340 * Note that tcp_mss has been adjusted to take into account the 23341 * timestamp option if applicable. Because SACK options do not 23342 * appear in every TCP segments and they are of variable lengths, 23343 * they cannot be included in tcp_mss. Thus we need to calculate 23344 * the actual segment length when we need to send a segment which 23345 * includes SACK options. 23346 */ 23347 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23348 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23349 tcp->tcp_num_sack_blk); 23350 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23351 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23352 if (max_to_send + sack_opt_len > tcp->tcp_mss) 23353 max_to_send -= sack_opt_len; 23354 } 23355 23356 if (offset != NULL) { 23357 off = *offset; 23358 /* We use offset as an indicator that end_mp is not NULL. */ 23359 *end_mp = NULL; 23360 } 23361 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 23362 /* This could be faster with cooperation from downstream */ 23363 if (mp2 != mp1 && !sendall && 23364 data_length + (int)(mp->b_wptr - mp->b_rptr) > 23365 max_to_send) 23366 /* 23367 * Don't send the next mblk since the whole mblk 23368 * does not fit. 23369 */ 23370 break; 23371 mp2->b_cont = dupb(mp); 23372 mp2 = mp2->b_cont; 23373 if (!mp2) { 23374 freemsg(mp1); 23375 return (NULL); 23376 } 23377 mp2->b_rptr += off; 23378 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 23379 (uintptr_t)INT_MAX); 23380 23381 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 23382 if (data_length > max_to_send) { 23383 mp2->b_wptr -= data_length - max_to_send; 23384 data_length = max_to_send; 23385 off = mp2->b_wptr - mp->b_rptr; 23386 break; 23387 } else { 23388 off = 0; 23389 } 23390 } 23391 if (offset != NULL) { 23392 *offset = off; 23393 *end_mp = mp; 23394 } 23395 if (seg_len != NULL) { 23396 *seg_len = data_length; 23397 } 23398 23399 /* Update the latest receive window size in TCP header. */ 23400 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23401 tcp->tcp_tcph->th_win); 23402 23403 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23404 mp1->b_rptr = rptr; 23405 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 23406 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23407 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23408 U32_TO_ABE32(seq, tcph->th_seq); 23409 23410 /* 23411 * Use tcp_unsent to determine if the PUSH bit should be used assumes 23412 * that this function was called from tcp_wput_data. Thus, when called 23413 * to retransmit data the setting of the PUSH bit may appear some 23414 * what random in that it might get set when it should not. This 23415 * should not pose any performance issues. 23416 */ 23417 if (data_length != 0 && (tcp->tcp_unsent == 0 || 23418 tcp->tcp_unsent == data_length)) { 23419 flags = TH_ACK | TH_PUSH; 23420 } else { 23421 flags = TH_ACK; 23422 } 23423 23424 if (tcp->tcp_ecn_ok) { 23425 if (tcp->tcp_ecn_echo_on) 23426 flags |= TH_ECE; 23427 23428 /* 23429 * Only set ECT bit and ECN_CWR if a segment contains new data. 23430 * There is no TCP flow control for non-data segments, and 23431 * only data segment is transmitted reliably. 23432 */ 23433 if (data_length > 0 && !rexmit) { 23434 SET_ECT(tcp, rptr); 23435 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23436 flags |= TH_CWR; 23437 tcp->tcp_ecn_cwr_sent = B_TRUE; 23438 } 23439 } 23440 } 23441 23442 if (tcp->tcp_valid_bits) { 23443 uint32_t u1; 23444 23445 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23446 seq == tcp->tcp_iss) { 23447 uchar_t *wptr; 23448 23449 /* 23450 * If TCP_ISS_VALID and the seq number is tcp_iss, 23451 * TCP can only be in SYN-SENT, SYN-RCVD or 23452 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23453 * our SYN is not ack'ed but the app closes this 23454 * TCP connection. 23455 */ 23456 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23457 tcp->tcp_state == TCPS_SYN_RCVD || 23458 tcp->tcp_state == TCPS_FIN_WAIT_1); 23459 23460 /* 23461 * Tack on the MSS option. It is always needed 23462 * for both active and passive open. 23463 * 23464 * MSS option value should be interface MTU - MIN 23465 * TCP/IP header according to RFC 793 as it means 23466 * the maximum segment size TCP can receive. But 23467 * to get around some broken middle boxes/end hosts 23468 * out there, we allow the option value to be the 23469 * same as the MSS option size on the peer side. 23470 * In this way, the other side will not send 23471 * anything larger than they can receive. 23472 * 23473 * Note that for SYN_SENT state, the ndd param 23474 * tcp_use_smss_as_mss_opt has no effect as we 23475 * don't know the peer's MSS option value. So 23476 * the only case we need to take care of is in 23477 * SYN_RCVD state, which is done later. 23478 */ 23479 wptr = mp1->b_wptr; 23480 wptr[0] = TCPOPT_MAXSEG; 23481 wptr[1] = TCPOPT_MAXSEG_LEN; 23482 wptr += 2; 23483 u1 = tcp->tcp_if_mtu - 23484 (tcp->tcp_ipversion == IPV4_VERSION ? 23485 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23486 TCP_MIN_HEADER_LENGTH; 23487 U16_TO_BE16(u1, wptr); 23488 mp1->b_wptr = wptr + 2; 23489 /* Update the offset to cover the additional word */ 23490 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23491 23492 /* 23493 * Note that the following way of filling in 23494 * TCP options are not optimal. Some NOPs can 23495 * be saved. But there is no need at this time 23496 * to optimize it. When it is needed, we will 23497 * do it. 23498 */ 23499 switch (tcp->tcp_state) { 23500 case TCPS_SYN_SENT: 23501 flags = TH_SYN; 23502 23503 if (tcp->tcp_snd_ts_ok) { 23504 uint32_t llbolt = (uint32_t)lbolt; 23505 23506 wptr = mp1->b_wptr; 23507 wptr[0] = TCPOPT_NOP; 23508 wptr[1] = TCPOPT_NOP; 23509 wptr[2] = TCPOPT_TSTAMP; 23510 wptr[3] = TCPOPT_TSTAMP_LEN; 23511 wptr += 4; 23512 U32_TO_BE32(llbolt, wptr); 23513 wptr += 4; 23514 ASSERT(tcp->tcp_ts_recent == 0); 23515 U32_TO_BE32(0L, wptr); 23516 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23517 tcph->th_offset_and_rsrvd[0] += 23518 (3 << 4); 23519 } 23520 23521 /* 23522 * Set up all the bits to tell other side 23523 * we are ECN capable. 23524 */ 23525 if (tcp->tcp_ecn_ok) { 23526 flags |= (TH_ECE | TH_CWR); 23527 } 23528 break; 23529 case TCPS_SYN_RCVD: 23530 flags |= TH_SYN; 23531 23532 /* 23533 * Reset the MSS option value to be SMSS 23534 * We should probably add back the bytes 23535 * for timestamp option and IPsec. We 23536 * don't do that as this is a workaround 23537 * for broken middle boxes/end hosts, it 23538 * is better for us to be more cautious. 23539 * They may not take these things into 23540 * account in their SMSS calculation. Thus 23541 * the peer's calculated SMSS may be smaller 23542 * than what it can be. This should be OK. 23543 */ 23544 if (tcps->tcps_use_smss_as_mss_opt) { 23545 u1 = tcp->tcp_mss; 23546 U16_TO_BE16(u1, wptr); 23547 } 23548 23549 /* 23550 * If the other side is ECN capable, reply 23551 * that we are also ECN capable. 23552 */ 23553 if (tcp->tcp_ecn_ok) 23554 flags |= TH_ECE; 23555 break; 23556 default: 23557 /* 23558 * The above ASSERT() makes sure that this 23559 * must be FIN-WAIT-1 state. Our SYN has 23560 * not been ack'ed so retransmit it. 23561 */ 23562 flags |= TH_SYN; 23563 break; 23564 } 23565 23566 if (tcp->tcp_snd_ws_ok) { 23567 wptr = mp1->b_wptr; 23568 wptr[0] = TCPOPT_NOP; 23569 wptr[1] = TCPOPT_WSCALE; 23570 wptr[2] = TCPOPT_WS_LEN; 23571 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23572 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23573 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23574 } 23575 23576 if (tcp->tcp_snd_sack_ok) { 23577 wptr = mp1->b_wptr; 23578 wptr[0] = TCPOPT_NOP; 23579 wptr[1] = TCPOPT_NOP; 23580 wptr[2] = TCPOPT_SACK_PERMITTED; 23581 wptr[3] = TCPOPT_SACK_OK_LEN; 23582 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23583 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23584 } 23585 23586 /* allocb() of adequate mblk assures space */ 23587 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23588 (uintptr_t)INT_MAX); 23589 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23590 /* 23591 * Get IP set to checksum on our behalf 23592 * Include the adjustment for a source route if any. 23593 */ 23594 u1 += tcp->tcp_sum; 23595 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23596 U16_TO_BE16(u1, tcph->th_sum); 23597 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23598 } 23599 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23600 (seq + data_length) == tcp->tcp_fss) { 23601 if (!tcp->tcp_fin_acked) { 23602 flags |= TH_FIN; 23603 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23604 } 23605 if (!tcp->tcp_fin_sent) { 23606 tcp->tcp_fin_sent = B_TRUE; 23607 switch (tcp->tcp_state) { 23608 case TCPS_SYN_RCVD: 23609 case TCPS_ESTABLISHED: 23610 tcp->tcp_state = TCPS_FIN_WAIT_1; 23611 break; 23612 case TCPS_CLOSE_WAIT: 23613 tcp->tcp_state = TCPS_LAST_ACK; 23614 break; 23615 } 23616 if (tcp->tcp_suna == tcp->tcp_snxt) 23617 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23618 tcp->tcp_snxt = tcp->tcp_fss + 1; 23619 } 23620 } 23621 /* 23622 * Note the trick here. u1 is unsigned. When tcp_urg 23623 * is smaller than seq, u1 will become a very huge value. 23624 * So the comparison will fail. Also note that tcp_urp 23625 * should be positive, see RFC 793 page 17. 23626 */ 23627 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23628 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23629 u1 < (uint32_t)(64 * 1024)) { 23630 flags |= TH_URG; 23631 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23632 U32_TO_ABE16(u1, tcph->th_urp); 23633 } 23634 } 23635 tcph->th_flags[0] = (uchar_t)flags; 23636 tcp->tcp_rack = tcp->tcp_rnxt; 23637 tcp->tcp_rack_cnt = 0; 23638 23639 if (tcp->tcp_snd_ts_ok) { 23640 if (tcp->tcp_state != TCPS_SYN_SENT) { 23641 uint32_t llbolt = (uint32_t)lbolt; 23642 23643 U32_TO_BE32(llbolt, 23644 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23645 U32_TO_BE32(tcp->tcp_ts_recent, 23646 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23647 } 23648 } 23649 23650 if (num_sack_blk > 0) { 23651 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23652 sack_blk_t *tmp; 23653 int32_t i; 23654 23655 wptr[0] = TCPOPT_NOP; 23656 wptr[1] = TCPOPT_NOP; 23657 wptr[2] = TCPOPT_SACK; 23658 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23659 sizeof (sack_blk_t); 23660 wptr += TCPOPT_REAL_SACK_LEN; 23661 23662 tmp = tcp->tcp_sack_list; 23663 for (i = 0; i < num_sack_blk; i++) { 23664 U32_TO_BE32(tmp[i].begin, wptr); 23665 wptr += sizeof (tcp_seq); 23666 U32_TO_BE32(tmp[i].end, wptr); 23667 wptr += sizeof (tcp_seq); 23668 } 23669 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23670 } 23671 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23672 data_length += (int)(mp1->b_wptr - rptr); 23673 if (tcp->tcp_ipversion == IPV4_VERSION) { 23674 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23675 } else { 23676 ip6_t *ip6 = (ip6_t *)(rptr + 23677 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23678 sizeof (ip6i_t) : 0)); 23679 23680 ip6->ip6_plen = htons(data_length - 23681 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23682 } 23683 23684 /* 23685 * Prime pump for IP 23686 * Include the adjustment for a source route if any. 23687 */ 23688 data_length -= tcp->tcp_ip_hdr_len; 23689 data_length += tcp->tcp_sum; 23690 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23691 U16_TO_ABE16(data_length, tcph->th_sum); 23692 if (tcp->tcp_ip_forward_progress) { 23693 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23694 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23695 tcp->tcp_ip_forward_progress = B_FALSE; 23696 } 23697 return (mp1); 23698 } 23699 23700 /* This function handles the push timeout. */ 23701 void 23702 tcp_push_timer(void *arg) 23703 { 23704 conn_t *connp = (conn_t *)arg; 23705 tcp_t *tcp = connp->conn_tcp; 23706 tcp_stack_t *tcps = tcp->tcp_tcps; 23707 uint_t flags; 23708 sodirect_t *sodp; 23709 23710 TCP_DBGSTAT(tcps, tcp_push_timer_cnt); 23711 23712 ASSERT(tcp->tcp_listener == NULL); 23713 23714 /* 23715 * We need to plug synchronous streams during our drain to prevent 23716 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23717 */ 23718 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23719 tcp->tcp_push_tid = 0; 23720 23721 SOD_PTR_ENTER(tcp, sodp); 23722 if (sodp != NULL) { 23723 flags = tcp_rcv_sod_wakeup(tcp, sodp); 23724 /* sod_wakeup() does the mutex_exit() */ 23725 } else if (tcp->tcp_rcv_list != NULL) { 23726 flags = tcp_rcv_drain(tcp->tcp_rq, tcp); 23727 } 23728 if (flags == TH_ACK_NEEDED) 23729 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23730 23731 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23732 } 23733 23734 /* 23735 * This function handles delayed ACK timeout. 23736 */ 23737 static void 23738 tcp_ack_timer(void *arg) 23739 { 23740 conn_t *connp = (conn_t *)arg; 23741 tcp_t *tcp = connp->conn_tcp; 23742 mblk_t *mp; 23743 tcp_stack_t *tcps = tcp->tcp_tcps; 23744 23745 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23746 23747 tcp->tcp_ack_tid = 0; 23748 23749 if (tcp->tcp_fused) 23750 return; 23751 23752 /* 23753 * Do not send ACK if there is no outstanding unack'ed data. 23754 */ 23755 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23756 return; 23757 } 23758 23759 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23760 /* 23761 * Make sure we don't allow deferred ACKs to result in 23762 * timer-based ACKing. If we have held off an ACK 23763 * when there was more than an mss here, and the timer 23764 * goes off, we have to worry about the possibility 23765 * that the sender isn't doing slow-start, or is out 23766 * of step with us for some other reason. We fall 23767 * permanently back in the direction of 23768 * ACK-every-other-packet as suggested in RFC 1122. 23769 */ 23770 if (tcp->tcp_rack_abs_max > 2) 23771 tcp->tcp_rack_abs_max--; 23772 tcp->tcp_rack_cur_max = 2; 23773 } 23774 mp = tcp_ack_mp(tcp); 23775 23776 if (mp != NULL) { 23777 BUMP_LOCAL(tcp->tcp_obsegs); 23778 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23779 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23780 tcp_send_data(tcp, tcp->tcp_wq, mp); 23781 } 23782 } 23783 23784 23785 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23786 static mblk_t * 23787 tcp_ack_mp(tcp_t *tcp) 23788 { 23789 uint32_t seq_no; 23790 tcp_stack_t *tcps = tcp->tcp_tcps; 23791 23792 /* 23793 * There are a few cases to be considered while setting the sequence no. 23794 * Essentially, we can come here while processing an unacceptable pkt 23795 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23796 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23797 * If we are here for a zero window probe, stick with suna. In all 23798 * other cases, we check if suna + swnd encompasses snxt and set 23799 * the sequence number to snxt, if so. If snxt falls outside the 23800 * window (the receiver probably shrunk its window), we will go with 23801 * suna + swnd, otherwise the sequence no will be unacceptable to the 23802 * receiver. 23803 */ 23804 if (tcp->tcp_zero_win_probe) { 23805 seq_no = tcp->tcp_suna; 23806 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23807 ASSERT(tcp->tcp_swnd == 0); 23808 seq_no = tcp->tcp_snxt; 23809 } else { 23810 seq_no = SEQ_GT(tcp->tcp_snxt, 23811 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23812 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23813 } 23814 23815 if (tcp->tcp_valid_bits) { 23816 /* 23817 * For the complex case where we have to send some 23818 * controls (FIN or SYN), let tcp_xmit_mp do it. 23819 */ 23820 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23821 NULL, B_FALSE)); 23822 } else { 23823 /* Generate a simple ACK */ 23824 int data_length; 23825 uchar_t *rptr; 23826 tcph_t *tcph; 23827 mblk_t *mp1; 23828 int32_t tcp_hdr_len; 23829 int32_t tcp_tcp_hdr_len; 23830 int32_t num_sack_blk = 0; 23831 int32_t sack_opt_len; 23832 23833 /* 23834 * Allocate space for TCP + IP headers 23835 * and link-level header 23836 */ 23837 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23838 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23839 tcp->tcp_num_sack_blk); 23840 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23841 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23842 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23843 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23844 } else { 23845 tcp_hdr_len = tcp->tcp_hdr_len; 23846 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23847 } 23848 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23849 if (!mp1) 23850 return (NULL); 23851 23852 /* Update the latest receive window size in TCP header. */ 23853 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23854 tcp->tcp_tcph->th_win); 23855 /* copy in prototype TCP + IP header */ 23856 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23857 mp1->b_rptr = rptr; 23858 mp1->b_wptr = rptr + tcp_hdr_len; 23859 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23860 23861 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23862 23863 /* Set the TCP sequence number. */ 23864 U32_TO_ABE32(seq_no, tcph->th_seq); 23865 23866 /* Set up the TCP flag field. */ 23867 tcph->th_flags[0] = (uchar_t)TH_ACK; 23868 if (tcp->tcp_ecn_echo_on) 23869 tcph->th_flags[0] |= TH_ECE; 23870 23871 tcp->tcp_rack = tcp->tcp_rnxt; 23872 tcp->tcp_rack_cnt = 0; 23873 23874 /* fill in timestamp option if in use */ 23875 if (tcp->tcp_snd_ts_ok) { 23876 uint32_t llbolt = (uint32_t)lbolt; 23877 23878 U32_TO_BE32(llbolt, 23879 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23880 U32_TO_BE32(tcp->tcp_ts_recent, 23881 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23882 } 23883 23884 /* Fill in SACK options */ 23885 if (num_sack_blk > 0) { 23886 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23887 sack_blk_t *tmp; 23888 int32_t i; 23889 23890 wptr[0] = TCPOPT_NOP; 23891 wptr[1] = TCPOPT_NOP; 23892 wptr[2] = TCPOPT_SACK; 23893 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23894 sizeof (sack_blk_t); 23895 wptr += TCPOPT_REAL_SACK_LEN; 23896 23897 tmp = tcp->tcp_sack_list; 23898 for (i = 0; i < num_sack_blk; i++) { 23899 U32_TO_BE32(tmp[i].begin, wptr); 23900 wptr += sizeof (tcp_seq); 23901 U32_TO_BE32(tmp[i].end, wptr); 23902 wptr += sizeof (tcp_seq); 23903 } 23904 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23905 << 4); 23906 } 23907 23908 if (tcp->tcp_ipversion == IPV4_VERSION) { 23909 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23910 } else { 23911 /* Check for ip6i_t header in sticky hdrs */ 23912 ip6_t *ip6 = (ip6_t *)(rptr + 23913 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23914 sizeof (ip6i_t) : 0)); 23915 23916 ip6->ip6_plen = htons(tcp_hdr_len - 23917 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23918 } 23919 23920 /* 23921 * Prime pump for checksum calculation in IP. Include the 23922 * adjustment for a source route if any. 23923 */ 23924 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23925 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23926 U16_TO_ABE16(data_length, tcph->th_sum); 23927 23928 if (tcp->tcp_ip_forward_progress) { 23929 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23930 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23931 tcp->tcp_ip_forward_progress = B_FALSE; 23932 } 23933 return (mp1); 23934 } 23935 } 23936 23937 /* 23938 * Hash list insertion routine for tcp_t structures. 23939 * Inserts entries with the ones bound to a specific IP address first 23940 * followed by those bound to INADDR_ANY. 23941 */ 23942 static void 23943 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23944 { 23945 tcp_t **tcpp; 23946 tcp_t *tcpnext; 23947 23948 if (tcp->tcp_ptpbhn != NULL) { 23949 ASSERT(!caller_holds_lock); 23950 tcp_bind_hash_remove(tcp); 23951 } 23952 tcpp = &tbf->tf_tcp; 23953 if (!caller_holds_lock) { 23954 mutex_enter(&tbf->tf_lock); 23955 } else { 23956 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23957 } 23958 tcpnext = tcpp[0]; 23959 if (tcpnext) { 23960 /* 23961 * If the new tcp bound to the INADDR_ANY address 23962 * and the first one in the list is not bound to 23963 * INADDR_ANY we skip all entries until we find the 23964 * first one bound to INADDR_ANY. 23965 * This makes sure that applications binding to a 23966 * specific address get preference over those binding to 23967 * INADDR_ANY. 23968 */ 23969 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23970 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23971 while ((tcpnext = tcpp[0]) != NULL && 23972 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23973 tcpp = &(tcpnext->tcp_bind_hash); 23974 if (tcpnext) 23975 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23976 } else 23977 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 23978 } 23979 tcp->tcp_bind_hash = tcpnext; 23980 tcp->tcp_ptpbhn = tcpp; 23981 tcpp[0] = tcp; 23982 if (!caller_holds_lock) 23983 mutex_exit(&tbf->tf_lock); 23984 } 23985 23986 /* 23987 * Hash list removal routine for tcp_t structures. 23988 */ 23989 static void 23990 tcp_bind_hash_remove(tcp_t *tcp) 23991 { 23992 tcp_t *tcpnext; 23993 kmutex_t *lockp; 23994 tcp_stack_t *tcps = tcp->tcp_tcps; 23995 23996 if (tcp->tcp_ptpbhn == NULL) 23997 return; 23998 23999 /* 24000 * Extract the lock pointer in case there are concurrent 24001 * hash_remove's for this instance. 24002 */ 24003 ASSERT(tcp->tcp_lport != 0); 24004 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 24005 24006 ASSERT(lockp != NULL); 24007 mutex_enter(lockp); 24008 if (tcp->tcp_ptpbhn) { 24009 tcpnext = tcp->tcp_bind_hash; 24010 if (tcpnext) { 24011 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 24012 tcp->tcp_bind_hash = NULL; 24013 } 24014 *tcp->tcp_ptpbhn = tcpnext; 24015 tcp->tcp_ptpbhn = NULL; 24016 } 24017 mutex_exit(lockp); 24018 } 24019 24020 24021 /* 24022 * Hash list lookup routine for tcp_t structures. 24023 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 24024 */ 24025 static tcp_t * 24026 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 24027 { 24028 tf_t *tf; 24029 tcp_t *tcp; 24030 24031 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24032 mutex_enter(&tf->tf_lock); 24033 for (tcp = tf->tf_tcp; tcp != NULL; 24034 tcp = tcp->tcp_acceptor_hash) { 24035 if (tcp->tcp_acceptor_id == id) { 24036 CONN_INC_REF(tcp->tcp_connp); 24037 mutex_exit(&tf->tf_lock); 24038 return (tcp); 24039 } 24040 } 24041 mutex_exit(&tf->tf_lock); 24042 return (NULL); 24043 } 24044 24045 24046 /* 24047 * Hash list insertion routine for tcp_t structures. 24048 */ 24049 void 24050 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 24051 { 24052 tf_t *tf; 24053 tcp_t **tcpp; 24054 tcp_t *tcpnext; 24055 tcp_stack_t *tcps = tcp->tcp_tcps; 24056 24057 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24058 24059 if (tcp->tcp_ptpahn != NULL) 24060 tcp_acceptor_hash_remove(tcp); 24061 tcpp = &tf->tf_tcp; 24062 mutex_enter(&tf->tf_lock); 24063 tcpnext = tcpp[0]; 24064 if (tcpnext) 24065 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 24066 tcp->tcp_acceptor_hash = tcpnext; 24067 tcp->tcp_ptpahn = tcpp; 24068 tcpp[0] = tcp; 24069 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 24070 mutex_exit(&tf->tf_lock); 24071 } 24072 24073 /* 24074 * Hash list removal routine for tcp_t structures. 24075 */ 24076 static void 24077 tcp_acceptor_hash_remove(tcp_t *tcp) 24078 { 24079 tcp_t *tcpnext; 24080 kmutex_t *lockp; 24081 24082 /* 24083 * Extract the lock pointer in case there are concurrent 24084 * hash_remove's for this instance. 24085 */ 24086 lockp = tcp->tcp_acceptor_lockp; 24087 24088 if (tcp->tcp_ptpahn == NULL) 24089 return; 24090 24091 ASSERT(lockp != NULL); 24092 mutex_enter(lockp); 24093 if (tcp->tcp_ptpahn) { 24094 tcpnext = tcp->tcp_acceptor_hash; 24095 if (tcpnext) { 24096 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24097 tcp->tcp_acceptor_hash = NULL; 24098 } 24099 *tcp->tcp_ptpahn = tcpnext; 24100 tcp->tcp_ptpahn = NULL; 24101 } 24102 mutex_exit(lockp); 24103 tcp->tcp_acceptor_lockp = NULL; 24104 } 24105 24106 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24107 24108 static ipaddr_t netmasks[] = { 24109 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24110 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24111 }; 24112 24113 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24114 24115 /* 24116 * XXX This routine should go away and instead we should use the metrics 24117 * associated with the routes to determine the default sndspace and rcvspace. 24118 */ 24119 static tcp_hsp_t * 24120 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24121 { 24122 tcp_hsp_t *hsp = NULL; 24123 24124 /* Quick check without acquiring the lock. */ 24125 if (tcps->tcps_hsp_hash == NULL) 24126 return (NULL); 24127 24128 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24129 24130 /* This routine finds the best-matching HSP for address addr. */ 24131 24132 if (tcps->tcps_hsp_hash) { 24133 int i; 24134 ipaddr_t srchaddr; 24135 tcp_hsp_t *hsp_net; 24136 24137 /* We do three passes: host, network, and subnet. */ 24138 24139 srchaddr = addr; 24140 24141 for (i = 1; i <= 3; i++) { 24142 /* Look for exact match on srchaddr */ 24143 24144 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24145 while (hsp) { 24146 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24147 hsp->tcp_hsp_addr == srchaddr) 24148 break; 24149 hsp = hsp->tcp_hsp_next; 24150 } 24151 ASSERT(hsp == NULL || 24152 hsp->tcp_hsp_vers == IPV4_VERSION); 24153 24154 /* 24155 * If this is the first pass: 24156 * If we found a match, great, return it. 24157 * If not, search for the network on the second pass. 24158 */ 24159 24160 if (i == 1) 24161 if (hsp) 24162 break; 24163 else 24164 { 24165 srchaddr = addr & netmask(addr); 24166 continue; 24167 } 24168 24169 /* 24170 * If this is the second pass: 24171 * If we found a match, but there's a subnet mask, 24172 * save the match but try again using the subnet 24173 * mask on the third pass. 24174 * Otherwise, return whatever we found. 24175 */ 24176 24177 if (i == 2) { 24178 if (hsp && hsp->tcp_hsp_subnet) { 24179 hsp_net = hsp; 24180 srchaddr = addr & hsp->tcp_hsp_subnet; 24181 continue; 24182 } else { 24183 break; 24184 } 24185 } 24186 24187 /* 24188 * This must be the third pass. If we didn't find 24189 * anything, return the saved network HSP instead. 24190 */ 24191 24192 if (!hsp) 24193 hsp = hsp_net; 24194 } 24195 } 24196 24197 rw_exit(&tcps->tcps_hsp_lock); 24198 return (hsp); 24199 } 24200 24201 /* 24202 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24203 * match lookup. 24204 */ 24205 static tcp_hsp_t * 24206 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24207 { 24208 tcp_hsp_t *hsp = NULL; 24209 24210 /* Quick check without acquiring the lock. */ 24211 if (tcps->tcps_hsp_hash == NULL) 24212 return (NULL); 24213 24214 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24215 24216 /* This routine finds the best-matching HSP for address addr. */ 24217 24218 if (tcps->tcps_hsp_hash) { 24219 int i; 24220 in6_addr_t v6srchaddr; 24221 tcp_hsp_t *hsp_net; 24222 24223 /* We do three passes: host, network, and subnet. */ 24224 24225 v6srchaddr = *v6addr; 24226 24227 for (i = 1; i <= 3; i++) { 24228 /* Look for exact match on srchaddr */ 24229 24230 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24231 V4_PART_OF_V6(v6srchaddr))]; 24232 while (hsp) { 24233 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24234 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24235 &v6srchaddr)) 24236 break; 24237 hsp = hsp->tcp_hsp_next; 24238 } 24239 24240 /* 24241 * If this is the first pass: 24242 * If we found a match, great, return it. 24243 * If not, search for the network on the second pass. 24244 */ 24245 24246 if (i == 1) 24247 if (hsp) 24248 break; 24249 else { 24250 /* Assume a 64 bit mask */ 24251 v6srchaddr.s6_addr32[0] = 24252 v6addr->s6_addr32[0]; 24253 v6srchaddr.s6_addr32[1] = 24254 v6addr->s6_addr32[1]; 24255 v6srchaddr.s6_addr32[2] = 0; 24256 v6srchaddr.s6_addr32[3] = 0; 24257 continue; 24258 } 24259 24260 /* 24261 * If this is the second pass: 24262 * If we found a match, but there's a subnet mask, 24263 * save the match but try again using the subnet 24264 * mask on the third pass. 24265 * Otherwise, return whatever we found. 24266 */ 24267 24268 if (i == 2) { 24269 ASSERT(hsp == NULL || 24270 hsp->tcp_hsp_vers == IPV6_VERSION); 24271 if (hsp && 24272 !IN6_IS_ADDR_UNSPECIFIED( 24273 &hsp->tcp_hsp_subnet_v6)) { 24274 hsp_net = hsp; 24275 V6_MASK_COPY(*v6addr, 24276 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24277 continue; 24278 } else { 24279 break; 24280 } 24281 } 24282 24283 /* 24284 * This must be the third pass. If we didn't find 24285 * anything, return the saved network HSP instead. 24286 */ 24287 24288 if (!hsp) 24289 hsp = hsp_net; 24290 } 24291 } 24292 24293 rw_exit(&tcps->tcps_hsp_lock); 24294 return (hsp); 24295 } 24296 24297 /* 24298 * Type three generator adapted from the random() function in 4.4 BSD: 24299 */ 24300 24301 /* 24302 * Copyright (c) 1983, 1993 24303 * The Regents of the University of California. All rights reserved. 24304 * 24305 * Redistribution and use in source and binary forms, with or without 24306 * modification, are permitted provided that the following conditions 24307 * are met: 24308 * 1. Redistributions of source code must retain the above copyright 24309 * notice, this list of conditions and the following disclaimer. 24310 * 2. Redistributions in binary form must reproduce the above copyright 24311 * notice, this list of conditions and the following disclaimer in the 24312 * documentation and/or other materials provided with the distribution. 24313 * 3. All advertising materials mentioning features or use of this software 24314 * must display the following acknowledgement: 24315 * This product includes software developed by the University of 24316 * California, Berkeley and its contributors. 24317 * 4. Neither the name of the University nor the names of its contributors 24318 * may be used to endorse or promote products derived from this software 24319 * without specific prior written permission. 24320 * 24321 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24322 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24323 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24324 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24325 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24326 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24327 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24328 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24329 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24330 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24331 * SUCH DAMAGE. 24332 */ 24333 24334 /* Type 3 -- x**31 + x**3 + 1 */ 24335 #define DEG_3 31 24336 #define SEP_3 3 24337 24338 24339 /* Protected by tcp_random_lock */ 24340 static int tcp_randtbl[DEG_3 + 1]; 24341 24342 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24343 static int *tcp_random_rptr = &tcp_randtbl[1]; 24344 24345 static int *tcp_random_state = &tcp_randtbl[1]; 24346 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24347 24348 kmutex_t tcp_random_lock; 24349 24350 void 24351 tcp_random_init(void) 24352 { 24353 int i; 24354 hrtime_t hrt; 24355 time_t wallclock; 24356 uint64_t result; 24357 24358 /* 24359 * Use high-res timer and current time for seed. Gethrtime() returns 24360 * a longlong, which may contain resolution down to nanoseconds. 24361 * The current time will either be a 32-bit or a 64-bit quantity. 24362 * XOR the two together in a 64-bit result variable. 24363 * Convert the result to a 32-bit value by multiplying the high-order 24364 * 32-bits by the low-order 32-bits. 24365 */ 24366 24367 hrt = gethrtime(); 24368 (void) drv_getparm(TIME, &wallclock); 24369 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24370 mutex_enter(&tcp_random_lock); 24371 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24372 (result & 0xffffffff); 24373 24374 for (i = 1; i < DEG_3; i++) 24375 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24376 + 12345; 24377 tcp_random_fptr = &tcp_random_state[SEP_3]; 24378 tcp_random_rptr = &tcp_random_state[0]; 24379 mutex_exit(&tcp_random_lock); 24380 for (i = 0; i < 10 * DEG_3; i++) 24381 (void) tcp_random(); 24382 } 24383 24384 /* 24385 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24386 * This range is selected to be approximately centered on TCP_ISS / 2, 24387 * and easy to compute. We get this value by generating a 32-bit random 24388 * number, selecting out the high-order 17 bits, and then adding one so 24389 * that we never return zero. 24390 */ 24391 int 24392 tcp_random(void) 24393 { 24394 int i; 24395 24396 mutex_enter(&tcp_random_lock); 24397 *tcp_random_fptr += *tcp_random_rptr; 24398 24399 /* 24400 * The high-order bits are more random than the low-order bits, 24401 * so we select out the high-order 17 bits and add one so that 24402 * we never return zero. 24403 */ 24404 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24405 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24406 tcp_random_fptr = tcp_random_state; 24407 ++tcp_random_rptr; 24408 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24409 tcp_random_rptr = tcp_random_state; 24410 24411 mutex_exit(&tcp_random_lock); 24412 return (i); 24413 } 24414 24415 /* 24416 * XXX This will go away when TPI is extended to send 24417 * info reqs to sockfs/timod ..... 24418 * Given a queue, set the max packet size for the write 24419 * side of the queue below stream head. This value is 24420 * cached on the stream head. 24421 * Returns 1 on success, 0 otherwise. 24422 */ 24423 static int 24424 setmaxps(queue_t *q, int maxpsz) 24425 { 24426 struct stdata *stp; 24427 queue_t *wq; 24428 stp = STREAM(q); 24429 24430 /* 24431 * At this point change of a queue parameter is not allowed 24432 * when a multiplexor is sitting on top. 24433 */ 24434 if (stp->sd_flag & STPLEX) 24435 return (0); 24436 24437 claimstr(stp->sd_wrq); 24438 wq = stp->sd_wrq->q_next; 24439 ASSERT(wq != NULL); 24440 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 24441 releasestr(stp->sd_wrq); 24442 return (1); 24443 } 24444 24445 static int 24446 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24447 int *t_errorp, int *sys_errorp) 24448 { 24449 int error; 24450 int is_absreq_failure; 24451 t_scalar_t *opt_lenp; 24452 t_scalar_t opt_offset; 24453 int prim_type; 24454 struct T_conn_req *tcreqp; 24455 struct T_conn_res *tcresp; 24456 cred_t *cr; 24457 24458 cr = DB_CREDDEF(mp, tcp->tcp_cred); 24459 24460 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24461 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24462 prim_type == T_CONN_RES); 24463 24464 switch (prim_type) { 24465 case T_CONN_REQ: 24466 tcreqp = (struct T_conn_req *)mp->b_rptr; 24467 opt_offset = tcreqp->OPT_offset; 24468 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24469 break; 24470 case O_T_CONN_RES: 24471 case T_CONN_RES: 24472 tcresp = (struct T_conn_res *)mp->b_rptr; 24473 opt_offset = tcresp->OPT_offset; 24474 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24475 break; 24476 } 24477 24478 *t_errorp = 0; 24479 *sys_errorp = 0; 24480 *do_disconnectp = 0; 24481 24482 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24483 opt_offset, cr, &tcp_opt_obj, 24484 NULL, &is_absreq_failure); 24485 24486 switch (error) { 24487 case 0: /* no error */ 24488 ASSERT(is_absreq_failure == 0); 24489 return (0); 24490 case ENOPROTOOPT: 24491 *t_errorp = TBADOPT; 24492 break; 24493 case EACCES: 24494 *t_errorp = TACCES; 24495 break; 24496 default: 24497 *t_errorp = TSYSERR; *sys_errorp = error; 24498 break; 24499 } 24500 if (is_absreq_failure != 0) { 24501 /* 24502 * The connection request should get the local ack 24503 * T_OK_ACK and then a T_DISCON_IND. 24504 */ 24505 *do_disconnectp = 1; 24506 } 24507 return (-1); 24508 } 24509 24510 /* 24511 * Split this function out so that if the secret changes, I'm okay. 24512 * 24513 * Initialize the tcp_iss_cookie and tcp_iss_key. 24514 */ 24515 24516 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24517 24518 static void 24519 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24520 { 24521 struct { 24522 int32_t current_time; 24523 uint32_t randnum; 24524 uint16_t pad; 24525 uint8_t ether[6]; 24526 uint8_t passwd[PASSWD_SIZE]; 24527 } tcp_iss_cookie; 24528 time_t t; 24529 24530 /* 24531 * Start with the current absolute time. 24532 */ 24533 (void) drv_getparm(TIME, &t); 24534 tcp_iss_cookie.current_time = t; 24535 24536 /* 24537 * XXX - Need a more random number per RFC 1750, not this crap. 24538 * OTOH, if what follows is pretty random, then I'm in better shape. 24539 */ 24540 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24541 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24542 24543 /* 24544 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24545 * as a good template. 24546 */ 24547 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24548 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24549 24550 /* 24551 * The pass-phrase. Normally this is supplied by user-called NDD. 24552 */ 24553 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24554 24555 /* 24556 * See 4010593 if this section becomes a problem again, 24557 * but the local ethernet address is useful here. 24558 */ 24559 (void) localetheraddr(NULL, 24560 (struct ether_addr *)&tcp_iss_cookie.ether); 24561 24562 /* 24563 * Hash 'em all together. The MD5Final is called per-connection. 24564 */ 24565 mutex_enter(&tcps->tcps_iss_key_lock); 24566 MD5Init(&tcps->tcps_iss_key); 24567 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24568 sizeof (tcp_iss_cookie)); 24569 mutex_exit(&tcps->tcps_iss_key_lock); 24570 } 24571 24572 /* 24573 * Set the RFC 1948 pass phrase 24574 */ 24575 /* ARGSUSED */ 24576 static int 24577 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24578 cred_t *cr) 24579 { 24580 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24581 24582 /* 24583 * Basically, value contains a new pass phrase. Pass it along! 24584 */ 24585 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24586 return (0); 24587 } 24588 24589 /* ARGSUSED */ 24590 static int 24591 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24592 { 24593 bzero(buf, sizeof (tcp_sack_info_t)); 24594 return (0); 24595 } 24596 24597 /* ARGSUSED */ 24598 static int 24599 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24600 { 24601 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24602 return (0); 24603 } 24604 24605 /* 24606 * Make sure we wait until the default queue is setup, yet allow 24607 * tcp_g_q_create() to open a TCP stream. 24608 * We need to allow tcp_g_q_create() do do an open 24609 * of tcp, hence we compare curhread. 24610 * All others have to wait until the tcps_g_q has been 24611 * setup. 24612 */ 24613 void 24614 tcp_g_q_setup(tcp_stack_t *tcps) 24615 { 24616 mutex_enter(&tcps->tcps_g_q_lock); 24617 if (tcps->tcps_g_q != NULL) { 24618 mutex_exit(&tcps->tcps_g_q_lock); 24619 return; 24620 } 24621 if (tcps->tcps_g_q_creator == NULL) { 24622 /* This thread will set it up */ 24623 tcps->tcps_g_q_creator = curthread; 24624 mutex_exit(&tcps->tcps_g_q_lock); 24625 tcp_g_q_create(tcps); 24626 mutex_enter(&tcps->tcps_g_q_lock); 24627 ASSERT(tcps->tcps_g_q_creator == curthread); 24628 tcps->tcps_g_q_creator = NULL; 24629 cv_signal(&tcps->tcps_g_q_cv); 24630 ASSERT(tcps->tcps_g_q != NULL); 24631 mutex_exit(&tcps->tcps_g_q_lock); 24632 return; 24633 } 24634 /* Everybody but the creator has to wait */ 24635 if (tcps->tcps_g_q_creator != curthread) { 24636 while (tcps->tcps_g_q == NULL) 24637 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24638 } 24639 mutex_exit(&tcps->tcps_g_q_lock); 24640 } 24641 24642 #define IP "ip" 24643 24644 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24645 24646 /* 24647 * Create a default tcp queue here instead of in strplumb 24648 */ 24649 void 24650 tcp_g_q_create(tcp_stack_t *tcps) 24651 { 24652 int error; 24653 ldi_handle_t lh = NULL; 24654 ldi_ident_t li = NULL; 24655 int rval; 24656 cred_t *cr; 24657 major_t IP_MAJ; 24658 24659 #ifdef NS_DEBUG 24660 (void) printf("tcp_g_q_create()\n"); 24661 #endif 24662 24663 IP_MAJ = ddi_name_to_major(IP); 24664 24665 ASSERT(tcps->tcps_g_q_creator == curthread); 24666 24667 error = ldi_ident_from_major(IP_MAJ, &li); 24668 if (error) { 24669 #ifdef DEBUG 24670 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24671 error); 24672 #endif 24673 return; 24674 } 24675 24676 cr = zone_get_kcred(netstackid_to_zoneid( 24677 tcps->tcps_netstack->netstack_stackid)); 24678 ASSERT(cr != NULL); 24679 /* 24680 * We set the tcp default queue to IPv6 because IPv4 falls 24681 * back to IPv6 when it can't find a client, but 24682 * IPv6 does not fall back to IPv4. 24683 */ 24684 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24685 if (error) { 24686 #ifdef DEBUG 24687 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24688 error); 24689 #endif 24690 goto out; 24691 } 24692 24693 /* 24694 * This ioctl causes the tcp framework to cache a pointer to 24695 * this stream, so we don't want to close the stream after 24696 * this operation. 24697 * Use the kernel credentials that are for the zone we're in. 24698 */ 24699 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24700 (intptr_t)0, FKIOCTL, cr, &rval); 24701 if (error) { 24702 #ifdef DEBUG 24703 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24704 "error %d\n", error); 24705 #endif 24706 goto out; 24707 } 24708 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24709 lh = NULL; 24710 out: 24711 /* Close layered handles */ 24712 if (li) 24713 ldi_ident_release(li); 24714 /* Keep cred around until _inactive needs it */ 24715 tcps->tcps_g_q_cr = cr; 24716 } 24717 24718 /* 24719 * We keep tcp_g_q set until all other tcp_t's in the zone 24720 * has gone away, and then when tcp_g_q_inactive() is called 24721 * we clear it. 24722 */ 24723 void 24724 tcp_g_q_destroy(tcp_stack_t *tcps) 24725 { 24726 #ifdef NS_DEBUG 24727 (void) printf("tcp_g_q_destroy()for stack %d\n", 24728 tcps->tcps_netstack->netstack_stackid); 24729 #endif 24730 24731 if (tcps->tcps_g_q == NULL) { 24732 return; /* Nothing to cleanup */ 24733 } 24734 /* 24735 * Drop reference corresponding to the default queue. 24736 * This reference was added from tcp_open when the default queue 24737 * was created, hence we compensate for this extra drop in 24738 * tcp_g_q_close. If the refcnt drops to zero here it means 24739 * the default queue was the last one to be open, in which 24740 * case, then tcp_g_q_inactive will be 24741 * called as a result of the refrele. 24742 */ 24743 TCPS_REFRELE(tcps); 24744 } 24745 24746 /* 24747 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24748 * Run by tcp_q_q_inactive using a taskq. 24749 */ 24750 static void 24751 tcp_g_q_close(void *arg) 24752 { 24753 tcp_stack_t *tcps = arg; 24754 int error; 24755 ldi_handle_t lh = NULL; 24756 ldi_ident_t li = NULL; 24757 cred_t *cr; 24758 major_t IP_MAJ; 24759 24760 IP_MAJ = ddi_name_to_major(IP); 24761 24762 #ifdef NS_DEBUG 24763 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 24764 tcps->tcps_netstack->netstack_stackid, 24765 tcps->tcps_netstack->netstack_refcnt); 24766 #endif 24767 lh = tcps->tcps_g_q_lh; 24768 if (lh == NULL) 24769 return; /* Nothing to cleanup */ 24770 24771 ASSERT(tcps->tcps_refcnt == 1); 24772 ASSERT(tcps->tcps_g_q != NULL); 24773 24774 error = ldi_ident_from_major(IP_MAJ, &li); 24775 if (error) { 24776 #ifdef DEBUG 24777 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 24778 error); 24779 #endif 24780 return; 24781 } 24782 24783 cr = tcps->tcps_g_q_cr; 24784 tcps->tcps_g_q_cr = NULL; 24785 ASSERT(cr != NULL); 24786 24787 /* 24788 * Make sure we can break the recursion when tcp_close decrements 24789 * the reference count causing g_q_inactive to be called again. 24790 */ 24791 tcps->tcps_g_q_lh = NULL; 24792 24793 /* close the default queue */ 24794 (void) ldi_close(lh, FREAD|FWRITE, cr); 24795 /* 24796 * At this point in time tcps and the rest of netstack_t might 24797 * have been deleted. 24798 */ 24799 tcps = NULL; 24800 24801 /* Close layered handles */ 24802 ldi_ident_release(li); 24803 crfree(cr); 24804 } 24805 24806 /* 24807 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24808 * 24809 * Have to ensure that the ldi routines are not used by an 24810 * interrupt thread by using a taskq. 24811 */ 24812 void 24813 tcp_g_q_inactive(tcp_stack_t *tcps) 24814 { 24815 if (tcps->tcps_g_q_lh == NULL) 24816 return; /* Nothing to cleanup */ 24817 24818 ASSERT(tcps->tcps_refcnt == 0); 24819 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 24820 24821 if (servicing_interrupt()) { 24822 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 24823 (void *) tcps, TQ_SLEEP); 24824 } else { 24825 tcp_g_q_close(tcps); 24826 } 24827 } 24828 24829 /* 24830 * Called by IP when IP is loaded into the kernel 24831 */ 24832 void 24833 tcp_ddi_g_init(void) 24834 { 24835 tcp_timercache = kmem_cache_create("tcp_timercache", 24836 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 24837 NULL, NULL, NULL, NULL, NULL, 0); 24838 24839 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 24840 sizeof (tcp_sack_info_t), 0, 24841 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 24842 24843 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 24844 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 24845 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 24846 24847 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 24848 24849 /* Initialize the random number generator */ 24850 tcp_random_init(); 24851 24852 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 24853 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 24854 24855 /* A single callback independently of how many netstacks we have */ 24856 ip_squeue_init(tcp_squeue_add); 24857 24858 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 24859 24860 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 24861 TASKQ_PREPOPULATE); 24862 24863 /* 24864 * We want to be informed each time a stack is created or 24865 * destroyed in the kernel, so we can maintain the 24866 * set of tcp_stack_t's. 24867 */ 24868 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 24869 tcp_stack_fini); 24870 } 24871 24872 24873 /* 24874 * Initialize the TCP stack instance. 24875 */ 24876 static void * 24877 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 24878 { 24879 tcp_stack_t *tcps; 24880 tcpparam_t *pa; 24881 int i; 24882 24883 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 24884 tcps->tcps_netstack = ns; 24885 24886 /* Initialize locks */ 24887 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 24888 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 24889 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 24890 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 24891 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 24892 24893 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 24894 tcps->tcps_g_epriv_ports[0] = 2049; 24895 tcps->tcps_g_epriv_ports[1] = 4045; 24896 tcps->tcps_min_anonpriv_port = 512; 24897 24898 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 24899 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 24900 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 24901 TCP_FANOUT_SIZE, KM_SLEEP); 24902 24903 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24904 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 24905 MUTEX_DEFAULT, NULL); 24906 } 24907 24908 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24909 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 24910 MUTEX_DEFAULT, NULL); 24911 } 24912 24913 /* TCP's IPsec code calls the packet dropper. */ 24914 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 24915 24916 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 24917 tcps->tcps_params = pa; 24918 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24919 24920 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 24921 A_CNT(lcl_tcp_param_arr), tcps); 24922 24923 /* 24924 * Note: To really walk the device tree you need the devinfo 24925 * pointer to your device which is only available after probe/attach. 24926 * The following is safe only because it uses ddi_root_node() 24927 */ 24928 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 24929 tcp_opt_obj.odb_opt_arr_cnt); 24930 24931 /* 24932 * Initialize RFC 1948 secret values. This will probably be reset once 24933 * by the boot scripts. 24934 * 24935 * Use NULL name, as the name is caught by the new lockstats. 24936 * 24937 * Initialize with some random, non-guessable string, like the global 24938 * T_INFO_ACK. 24939 */ 24940 24941 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 24942 sizeof (tcp_g_t_info_ack), tcps); 24943 24944 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 24945 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 24946 24947 return (tcps); 24948 } 24949 24950 /* 24951 * Called when the IP module is about to be unloaded. 24952 */ 24953 void 24954 tcp_ddi_g_destroy(void) 24955 { 24956 tcp_g_kstat_fini(tcp_g_kstat); 24957 tcp_g_kstat = NULL; 24958 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 24959 24960 mutex_destroy(&tcp_random_lock); 24961 24962 kmem_cache_destroy(tcp_timercache); 24963 kmem_cache_destroy(tcp_sack_info_cache); 24964 kmem_cache_destroy(tcp_iphc_cache); 24965 24966 netstack_unregister(NS_TCP); 24967 taskq_destroy(tcp_taskq); 24968 } 24969 24970 /* 24971 * Shut down the TCP stack instance. 24972 */ 24973 /* ARGSUSED */ 24974 static void 24975 tcp_stack_shutdown(netstackid_t stackid, void *arg) 24976 { 24977 tcp_stack_t *tcps = (tcp_stack_t *)arg; 24978 24979 tcp_g_q_destroy(tcps); 24980 } 24981 24982 /* 24983 * Free the TCP stack instance. 24984 */ 24985 static void 24986 tcp_stack_fini(netstackid_t stackid, void *arg) 24987 { 24988 tcp_stack_t *tcps = (tcp_stack_t *)arg; 24989 int i; 24990 24991 nd_free(&tcps->tcps_g_nd); 24992 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24993 tcps->tcps_params = NULL; 24994 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 24995 tcps->tcps_wroff_xtra_param = NULL; 24996 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 24997 tcps->tcps_mdt_head_param = NULL; 24998 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 24999 tcps->tcps_mdt_tail_param = NULL; 25000 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 25001 tcps->tcps_mdt_max_pbufs_param = NULL; 25002 25003 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25004 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 25005 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 25006 } 25007 25008 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25009 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25010 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25011 } 25012 25013 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25014 tcps->tcps_bind_fanout = NULL; 25015 25016 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25017 tcps->tcps_acceptor_fanout = NULL; 25018 25019 mutex_destroy(&tcps->tcps_iss_key_lock); 25020 rw_destroy(&tcps->tcps_hsp_lock); 25021 mutex_destroy(&tcps->tcps_g_q_lock); 25022 cv_destroy(&tcps->tcps_g_q_cv); 25023 mutex_destroy(&tcps->tcps_epriv_port_lock); 25024 25025 ip_drop_unregister(&tcps->tcps_dropper); 25026 25027 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25028 tcps->tcps_kstat = NULL; 25029 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25030 25031 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25032 tcps->tcps_mibkp = NULL; 25033 25034 kmem_free(tcps, sizeof (*tcps)); 25035 } 25036 25037 /* 25038 * Generate ISS, taking into account NDD changes may happen halfway through. 25039 * (If the iss is not zero, set it.) 25040 */ 25041 25042 static void 25043 tcp_iss_init(tcp_t *tcp) 25044 { 25045 MD5_CTX context; 25046 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25047 uint32_t answer[4]; 25048 tcp_stack_t *tcps = tcp->tcp_tcps; 25049 25050 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25051 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25052 switch (tcps->tcps_strong_iss) { 25053 case 2: 25054 mutex_enter(&tcps->tcps_iss_key_lock); 25055 context = tcps->tcps_iss_key; 25056 mutex_exit(&tcps->tcps_iss_key_lock); 25057 arg.ports = tcp->tcp_ports; 25058 if (tcp->tcp_ipversion == IPV4_VERSION) { 25059 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25060 &arg.src); 25061 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25062 &arg.dst); 25063 } else { 25064 arg.src = tcp->tcp_ip6h->ip6_src; 25065 arg.dst = tcp->tcp_ip6h->ip6_dst; 25066 } 25067 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25068 MD5Final((uchar_t *)answer, &context); 25069 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25070 /* 25071 * Now that we've hashed into a unique per-connection sequence 25072 * space, add a random increment per strong_iss == 1. So I 25073 * guess we'll have to... 25074 */ 25075 /* FALLTHRU */ 25076 case 1: 25077 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25078 break; 25079 default: 25080 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25081 break; 25082 } 25083 tcp->tcp_valid_bits = TCP_ISS_VALID; 25084 tcp->tcp_fss = tcp->tcp_iss - 1; 25085 tcp->tcp_suna = tcp->tcp_iss; 25086 tcp->tcp_snxt = tcp->tcp_iss + 1; 25087 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25088 tcp->tcp_csuna = tcp->tcp_snxt; 25089 } 25090 25091 /* 25092 * Exported routine for extracting active tcp connection status. 25093 * 25094 * This is used by the Solaris Cluster Networking software to 25095 * gather a list of connections that need to be forwarded to 25096 * specific nodes in the cluster when configuration changes occur. 25097 * 25098 * The callback is invoked for each tcp_t structure. Returning 25099 * non-zero from the callback routine terminates the search. 25100 */ 25101 int 25102 cl_tcp_walk_list(int (*cl_callback)(cl_tcp_info_t *, void *), 25103 void *arg) 25104 { 25105 netstack_handle_t nh; 25106 netstack_t *ns; 25107 int ret = 0; 25108 25109 netstack_next_init(&nh); 25110 while ((ns = netstack_next(&nh)) != NULL) { 25111 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25112 ns->netstack_tcp); 25113 netstack_rele(ns); 25114 } 25115 netstack_next_fini(&nh); 25116 return (ret); 25117 } 25118 25119 static int 25120 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25121 tcp_stack_t *tcps) 25122 { 25123 tcp_t *tcp; 25124 cl_tcp_info_t cl_tcpi; 25125 connf_t *connfp; 25126 conn_t *connp; 25127 int i; 25128 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25129 25130 ASSERT(callback != NULL); 25131 25132 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25133 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25134 connp = NULL; 25135 25136 while ((connp = 25137 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25138 25139 tcp = connp->conn_tcp; 25140 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25141 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25142 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25143 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25144 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25145 /* 25146 * The macros tcp_laddr and tcp_faddr give the IPv4 25147 * addresses. They are copied implicitly below as 25148 * mapped addresses. 25149 */ 25150 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25151 if (tcp->tcp_ipversion == IPV4_VERSION) { 25152 cl_tcpi.cl_tcpi_faddr = 25153 tcp->tcp_ipha->ipha_dst; 25154 } else { 25155 cl_tcpi.cl_tcpi_faddr_v6 = 25156 tcp->tcp_ip6h->ip6_dst; 25157 } 25158 25159 /* 25160 * If the callback returns non-zero 25161 * we terminate the traversal. 25162 */ 25163 if ((*callback)(&cl_tcpi, arg) != 0) { 25164 CONN_DEC_REF(tcp->tcp_connp); 25165 return (1); 25166 } 25167 } 25168 } 25169 25170 return (0); 25171 } 25172 25173 /* 25174 * Macros used for accessing the different types of sockaddr 25175 * structures inside a tcp_ioc_abort_conn_t. 25176 */ 25177 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25178 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25179 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25180 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25181 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25182 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25183 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25184 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25185 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25186 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25187 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25188 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25189 25190 /* 25191 * Return the correct error code to mimic the behavior 25192 * of a connection reset. 25193 */ 25194 #define TCP_AC_GET_ERRCODE(state, err) { \ 25195 switch ((state)) { \ 25196 case TCPS_SYN_SENT: \ 25197 case TCPS_SYN_RCVD: \ 25198 (err) = ECONNREFUSED; \ 25199 break; \ 25200 case TCPS_ESTABLISHED: \ 25201 case TCPS_FIN_WAIT_1: \ 25202 case TCPS_FIN_WAIT_2: \ 25203 case TCPS_CLOSE_WAIT: \ 25204 (err) = ECONNRESET; \ 25205 break; \ 25206 case TCPS_CLOSING: \ 25207 case TCPS_LAST_ACK: \ 25208 case TCPS_TIME_WAIT: \ 25209 (err) = 0; \ 25210 break; \ 25211 default: \ 25212 (err) = ENXIO; \ 25213 } \ 25214 } 25215 25216 /* 25217 * Check if a tcp structure matches the info in acp. 25218 */ 25219 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25220 (((acp)->ac_local.ss_family == AF_INET) ? \ 25221 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25222 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25223 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25224 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25225 (TCP_AC_V4LPORT((acp)) == 0 || \ 25226 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25227 (TCP_AC_V4RPORT((acp)) == 0 || \ 25228 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25229 (acp)->ac_start <= (tcp)->tcp_state && \ 25230 (acp)->ac_end >= (tcp)->tcp_state) : \ 25231 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25232 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25233 &(tcp)->tcp_ip_src_v6)) && \ 25234 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25235 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25236 &(tcp)->tcp_remote_v6)) && \ 25237 (TCP_AC_V6LPORT((acp)) == 0 || \ 25238 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25239 (TCP_AC_V6RPORT((acp)) == 0 || \ 25240 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25241 (acp)->ac_start <= (tcp)->tcp_state && \ 25242 (acp)->ac_end >= (tcp)->tcp_state)) 25243 25244 #define TCP_AC_MATCH(acp, tcp) \ 25245 (((acp)->ac_zoneid == ALL_ZONES || \ 25246 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25247 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25248 25249 /* 25250 * Build a message containing a tcp_ioc_abort_conn_t structure 25251 * which is filled in with information from acp and tp. 25252 */ 25253 static mblk_t * 25254 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25255 { 25256 mblk_t *mp; 25257 tcp_ioc_abort_conn_t *tacp; 25258 25259 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25260 if (mp == NULL) 25261 return (NULL); 25262 25263 mp->b_datap->db_type = M_CTL; 25264 25265 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25266 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25267 sizeof (uint32_t)); 25268 25269 tacp->ac_start = acp->ac_start; 25270 tacp->ac_end = acp->ac_end; 25271 tacp->ac_zoneid = acp->ac_zoneid; 25272 25273 if (acp->ac_local.ss_family == AF_INET) { 25274 tacp->ac_local.ss_family = AF_INET; 25275 tacp->ac_remote.ss_family = AF_INET; 25276 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25277 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25278 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25279 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25280 } else { 25281 tacp->ac_local.ss_family = AF_INET6; 25282 tacp->ac_remote.ss_family = AF_INET6; 25283 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25284 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25285 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25286 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25287 } 25288 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25289 return (mp); 25290 } 25291 25292 /* 25293 * Print a tcp_ioc_abort_conn_t structure. 25294 */ 25295 static void 25296 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25297 { 25298 char lbuf[128]; 25299 char rbuf[128]; 25300 sa_family_t af; 25301 in_port_t lport, rport; 25302 ushort_t logflags; 25303 25304 af = acp->ac_local.ss_family; 25305 25306 if (af == AF_INET) { 25307 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25308 lbuf, 128); 25309 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25310 rbuf, 128); 25311 lport = ntohs(TCP_AC_V4LPORT(acp)); 25312 rport = ntohs(TCP_AC_V4RPORT(acp)); 25313 } else { 25314 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25315 lbuf, 128); 25316 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25317 rbuf, 128); 25318 lport = ntohs(TCP_AC_V6LPORT(acp)); 25319 rport = ntohs(TCP_AC_V6RPORT(acp)); 25320 } 25321 25322 logflags = SL_TRACE | SL_NOTE; 25323 /* 25324 * Don't print this message to the console if the operation was done 25325 * to a non-global zone. 25326 */ 25327 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25328 logflags |= SL_CONSOLE; 25329 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25330 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25331 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25332 acp->ac_start, acp->ac_end); 25333 } 25334 25335 /* 25336 * Called inside tcp_rput when a message built using 25337 * tcp_ioctl_abort_build_msg is put into a queue. 25338 * Note that when we get here there is no wildcard in acp any more. 25339 */ 25340 static void 25341 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25342 { 25343 tcp_ioc_abort_conn_t *acp; 25344 25345 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25346 if (tcp->tcp_state <= acp->ac_end) { 25347 /* 25348 * If we get here, we are already on the correct 25349 * squeue. This ioctl follows the following path 25350 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25351 * ->tcp_ioctl_abort->squeue_fill (if on a 25352 * different squeue) 25353 */ 25354 int errcode; 25355 25356 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25357 (void) tcp_clean_death(tcp, errcode, 26); 25358 } 25359 freemsg(mp); 25360 } 25361 25362 /* 25363 * Abort all matching connections on a hash chain. 25364 */ 25365 static int 25366 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25367 boolean_t exact, tcp_stack_t *tcps) 25368 { 25369 int nmatch, err = 0; 25370 tcp_t *tcp; 25371 MBLKP mp, last, listhead = NULL; 25372 conn_t *tconnp; 25373 connf_t *connfp; 25374 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25375 25376 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25377 25378 startover: 25379 nmatch = 0; 25380 25381 mutex_enter(&connfp->connf_lock); 25382 for (tconnp = connfp->connf_head; tconnp != NULL; 25383 tconnp = tconnp->conn_next) { 25384 tcp = tconnp->conn_tcp; 25385 if (TCP_AC_MATCH(acp, tcp)) { 25386 CONN_INC_REF(tcp->tcp_connp); 25387 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25388 if (mp == NULL) { 25389 err = ENOMEM; 25390 CONN_DEC_REF(tcp->tcp_connp); 25391 break; 25392 } 25393 mp->b_prev = (mblk_t *)tcp; 25394 25395 if (listhead == NULL) { 25396 listhead = mp; 25397 last = mp; 25398 } else { 25399 last->b_next = mp; 25400 last = mp; 25401 } 25402 nmatch++; 25403 if (exact) 25404 break; 25405 } 25406 25407 /* Avoid holding lock for too long. */ 25408 if (nmatch >= 500) 25409 break; 25410 } 25411 mutex_exit(&connfp->connf_lock); 25412 25413 /* Pass mp into the correct tcp */ 25414 while ((mp = listhead) != NULL) { 25415 listhead = listhead->b_next; 25416 tcp = (tcp_t *)mp->b_prev; 25417 mp->b_next = mp->b_prev = NULL; 25418 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 25419 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 25420 } 25421 25422 *count += nmatch; 25423 if (nmatch >= 500 && err == 0) 25424 goto startover; 25425 return (err); 25426 } 25427 25428 /* 25429 * Abort all connections that matches the attributes specified in acp. 25430 */ 25431 static int 25432 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25433 { 25434 sa_family_t af; 25435 uint32_t ports; 25436 uint16_t *pports; 25437 int err = 0, count = 0; 25438 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25439 int index = -1; 25440 ushort_t logflags; 25441 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25442 25443 af = acp->ac_local.ss_family; 25444 25445 if (af == AF_INET) { 25446 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25447 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25448 pports = (uint16_t *)&ports; 25449 pports[1] = TCP_AC_V4LPORT(acp); 25450 pports[0] = TCP_AC_V4RPORT(acp); 25451 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25452 } 25453 } else { 25454 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25455 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25456 pports = (uint16_t *)&ports; 25457 pports[1] = TCP_AC_V6LPORT(acp); 25458 pports[0] = TCP_AC_V6RPORT(acp); 25459 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25460 } 25461 } 25462 25463 /* 25464 * For cases where remote addr, local port, and remote port are non- 25465 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25466 */ 25467 if (index != -1) { 25468 err = tcp_ioctl_abort_bucket(acp, index, 25469 &count, exact, tcps); 25470 } else { 25471 /* 25472 * loop through all entries for wildcard case 25473 */ 25474 for (index = 0; 25475 index < ipst->ips_ipcl_conn_fanout_size; 25476 index++) { 25477 err = tcp_ioctl_abort_bucket(acp, index, 25478 &count, exact, tcps); 25479 if (err != 0) 25480 break; 25481 } 25482 } 25483 25484 logflags = SL_TRACE | SL_NOTE; 25485 /* 25486 * Don't print this message to the console if the operation was done 25487 * to a non-global zone. 25488 */ 25489 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25490 logflags |= SL_CONSOLE; 25491 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25492 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25493 if (err == 0 && count == 0) 25494 err = ENOENT; 25495 return (err); 25496 } 25497 25498 /* 25499 * Process the TCP_IOC_ABORT_CONN ioctl request. 25500 */ 25501 static void 25502 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25503 { 25504 int err; 25505 IOCP iocp; 25506 MBLKP mp1; 25507 sa_family_t laf, raf; 25508 tcp_ioc_abort_conn_t *acp; 25509 zone_t *zptr; 25510 conn_t *connp = Q_TO_CONN(q); 25511 zoneid_t zoneid = connp->conn_zoneid; 25512 tcp_t *tcp = connp->conn_tcp; 25513 tcp_stack_t *tcps = tcp->tcp_tcps; 25514 25515 iocp = (IOCP)mp->b_rptr; 25516 25517 if ((mp1 = mp->b_cont) == NULL || 25518 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25519 err = EINVAL; 25520 goto out; 25521 } 25522 25523 /* check permissions */ 25524 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25525 err = EPERM; 25526 goto out; 25527 } 25528 25529 if (mp1->b_cont != NULL) { 25530 freemsg(mp1->b_cont); 25531 mp1->b_cont = NULL; 25532 } 25533 25534 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25535 laf = acp->ac_local.ss_family; 25536 raf = acp->ac_remote.ss_family; 25537 25538 /* check that a zone with the supplied zoneid exists */ 25539 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25540 zptr = zone_find_by_id(zoneid); 25541 if (zptr != NULL) { 25542 zone_rele(zptr); 25543 } else { 25544 err = EINVAL; 25545 goto out; 25546 } 25547 } 25548 25549 /* 25550 * For exclusive stacks we set the zoneid to zero 25551 * to make TCP operate as if in the global zone. 25552 */ 25553 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25554 acp->ac_zoneid = GLOBAL_ZONEID; 25555 25556 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25557 acp->ac_start > acp->ac_end || laf != raf || 25558 (laf != AF_INET && laf != AF_INET6)) { 25559 err = EINVAL; 25560 goto out; 25561 } 25562 25563 tcp_ioctl_abort_dump(acp); 25564 err = tcp_ioctl_abort(acp, tcps); 25565 25566 out: 25567 if (mp1 != NULL) { 25568 freemsg(mp1); 25569 mp->b_cont = NULL; 25570 } 25571 25572 if (err != 0) 25573 miocnak(q, mp, 0, err); 25574 else 25575 miocack(q, mp, 0, 0); 25576 } 25577 25578 /* 25579 * tcp_time_wait_processing() handles processing of incoming packets when 25580 * the tcp is in the TIME_WAIT state. 25581 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25582 * on the time wait list. 25583 */ 25584 void 25585 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25586 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25587 { 25588 int32_t bytes_acked; 25589 int32_t gap; 25590 int32_t rgap; 25591 tcp_opt_t tcpopt; 25592 uint_t flags; 25593 uint32_t new_swnd = 0; 25594 conn_t *connp; 25595 tcp_stack_t *tcps = tcp->tcp_tcps; 25596 25597 BUMP_LOCAL(tcp->tcp_ibsegs); 25598 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 25599 25600 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25601 new_swnd = BE16_TO_U16(tcph->th_win) << 25602 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25603 if (tcp->tcp_snd_ts_ok) { 25604 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25605 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25606 tcp->tcp_rnxt, TH_ACK); 25607 goto done; 25608 } 25609 } 25610 gap = seg_seq - tcp->tcp_rnxt; 25611 rgap = tcp->tcp_rwnd - (gap + seg_len); 25612 if (gap < 0) { 25613 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25614 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25615 (seg_len > -gap ? -gap : seg_len)); 25616 seg_len += gap; 25617 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25618 if (flags & TH_RST) { 25619 goto done; 25620 } 25621 if ((flags & TH_FIN) && seg_len == -1) { 25622 /* 25623 * When TCP receives a duplicate FIN in 25624 * TIME_WAIT state, restart the 2 MSL timer. 25625 * See page 73 in RFC 793. Make sure this TCP 25626 * is already on the TIME_WAIT list. If not, 25627 * just restart the timer. 25628 */ 25629 if (TCP_IS_DETACHED(tcp)) { 25630 if (tcp_time_wait_remove(tcp, NULL) == 25631 B_TRUE) { 25632 tcp_time_wait_append(tcp); 25633 TCP_DBGSTAT(tcps, 25634 tcp_rput_time_wait); 25635 } 25636 } else { 25637 ASSERT(tcp != NULL); 25638 TCP_TIMER_RESTART(tcp, 25639 tcps->tcps_time_wait_interval); 25640 } 25641 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25642 tcp->tcp_rnxt, TH_ACK); 25643 goto done; 25644 } 25645 flags |= TH_ACK_NEEDED; 25646 seg_len = 0; 25647 goto process_ack; 25648 } 25649 25650 /* Fix seg_seq, and chew the gap off the front. */ 25651 seg_seq = tcp->tcp_rnxt; 25652 } 25653 25654 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25655 /* 25656 * Make sure that when we accept the connection, pick 25657 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25658 * old connection. 25659 * 25660 * The next ISS generated is equal to tcp_iss_incr_extra 25661 * + ISS_INCR/2 + other components depending on the 25662 * value of tcp_strong_iss. We pre-calculate the new 25663 * ISS here and compare with tcp_snxt to determine if 25664 * we need to make adjustment to tcp_iss_incr_extra. 25665 * 25666 * The above calculation is ugly and is a 25667 * waste of CPU cycles... 25668 */ 25669 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25670 int32_t adj; 25671 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25672 25673 switch (tcps->tcps_strong_iss) { 25674 case 2: { 25675 /* Add time and MD5 components. */ 25676 uint32_t answer[4]; 25677 struct { 25678 uint32_t ports; 25679 in6_addr_t src; 25680 in6_addr_t dst; 25681 } arg; 25682 MD5_CTX context; 25683 25684 mutex_enter(&tcps->tcps_iss_key_lock); 25685 context = tcps->tcps_iss_key; 25686 mutex_exit(&tcps->tcps_iss_key_lock); 25687 arg.ports = tcp->tcp_ports; 25688 /* We use MAPPED addresses in tcp_iss_init */ 25689 arg.src = tcp->tcp_ip_src_v6; 25690 if (tcp->tcp_ipversion == IPV4_VERSION) { 25691 IN6_IPADDR_TO_V4MAPPED( 25692 tcp->tcp_ipha->ipha_dst, 25693 &arg.dst); 25694 } else { 25695 arg.dst = 25696 tcp->tcp_ip6h->ip6_dst; 25697 } 25698 MD5Update(&context, (uchar_t *)&arg, 25699 sizeof (arg)); 25700 MD5Final((uchar_t *)answer, &context); 25701 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25702 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25703 break; 25704 } 25705 case 1: 25706 /* Add time component and min random (i.e. 1). */ 25707 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25708 break; 25709 default: 25710 /* Add only time component. */ 25711 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25712 break; 25713 } 25714 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25715 /* 25716 * New ISS not guaranteed to be ISS_INCR/2 25717 * ahead of the current tcp_snxt, so add the 25718 * difference to tcp_iss_incr_extra. 25719 */ 25720 tcps->tcps_iss_incr_extra += adj; 25721 } 25722 /* 25723 * If tcp_clean_death() can not perform the task now, 25724 * drop the SYN packet and let the other side re-xmit. 25725 * Otherwise pass the SYN packet back in, since the 25726 * old tcp state has been cleaned up or freed. 25727 */ 25728 if (tcp_clean_death(tcp, 0, 27) == -1) 25729 goto done; 25730 /* 25731 * We will come back to tcp_rput_data 25732 * on the global queue. Packets destined 25733 * for the global queue will be checked 25734 * with global policy. But the policy for 25735 * this packet has already been checked as 25736 * this was destined for the detached 25737 * connection. We need to bypass policy 25738 * check this time by attaching a dummy 25739 * ipsec_in with ipsec_in_dont_check set. 25740 */ 25741 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25742 if (connp != NULL) { 25743 TCP_STAT(tcps, tcp_time_wait_syn_success); 25744 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25745 return; 25746 } 25747 goto done; 25748 } 25749 25750 /* 25751 * rgap is the amount of stuff received out of window. A negative 25752 * value is the amount out of window. 25753 */ 25754 if (rgap < 0) { 25755 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 25756 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 25757 /* Fix seg_len and make sure there is something left. */ 25758 seg_len += rgap; 25759 if (seg_len <= 0) { 25760 if (flags & TH_RST) { 25761 goto done; 25762 } 25763 flags |= TH_ACK_NEEDED; 25764 seg_len = 0; 25765 goto process_ack; 25766 } 25767 } 25768 /* 25769 * Check whether we can update tcp_ts_recent. This test is 25770 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 25771 * Extensions for High Performance: An Update", Internet Draft. 25772 */ 25773 if (tcp->tcp_snd_ts_ok && 25774 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 25775 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 25776 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 25777 tcp->tcp_last_rcv_lbolt = lbolt64; 25778 } 25779 25780 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 25781 /* Always ack out of order packets */ 25782 flags |= TH_ACK_NEEDED; 25783 seg_len = 0; 25784 } else if (seg_len > 0) { 25785 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 25786 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 25787 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 25788 } 25789 if (flags & TH_RST) { 25790 (void) tcp_clean_death(tcp, 0, 28); 25791 goto done; 25792 } 25793 if (flags & TH_SYN) { 25794 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 25795 TH_RST|TH_ACK); 25796 /* 25797 * Do not delete the TCP structure if it is in 25798 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 25799 */ 25800 goto done; 25801 } 25802 process_ack: 25803 if (flags & TH_ACK) { 25804 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 25805 if (bytes_acked <= 0) { 25806 if (bytes_acked == 0 && seg_len == 0 && 25807 new_swnd == tcp->tcp_swnd) 25808 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 25809 } else { 25810 /* Acks something not sent */ 25811 flags |= TH_ACK_NEEDED; 25812 } 25813 } 25814 if (flags & TH_ACK_NEEDED) { 25815 /* 25816 * Time to send an ack for some reason. 25817 */ 25818 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25819 tcp->tcp_rnxt, TH_ACK); 25820 } 25821 done: 25822 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25823 DB_CKSUMSTART(mp) = 0; 25824 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 25825 TCP_STAT(tcps, tcp_time_wait_syn_fail); 25826 } 25827 freemsg(mp); 25828 } 25829 25830 /* 25831 * Allocate a T_SVR4_OPTMGMT_REQ. 25832 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 25833 * that tcp_rput_other can drop the acks. 25834 */ 25835 static mblk_t * 25836 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 25837 { 25838 mblk_t *mp; 25839 struct T_optmgmt_req *tor; 25840 struct opthdr *oh; 25841 uint_t size; 25842 char *optptr; 25843 25844 size = sizeof (*tor) + sizeof (*oh) + optlen; 25845 mp = allocb(size, BPRI_MED); 25846 if (mp == NULL) 25847 return (NULL); 25848 25849 mp->b_wptr += size; 25850 mp->b_datap->db_type = M_PROTO; 25851 tor = (struct T_optmgmt_req *)mp->b_rptr; 25852 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 25853 tor->MGMT_flags = T_NEGOTIATE; 25854 tor->OPT_length = sizeof (*oh) + optlen; 25855 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 25856 25857 oh = (struct opthdr *)&tor[1]; 25858 oh->level = level; 25859 oh->name = cmd; 25860 oh->len = optlen; 25861 if (optlen != 0) { 25862 optptr = (char *)&oh[1]; 25863 bcopy(opt, optptr, optlen); 25864 } 25865 return (mp); 25866 } 25867 25868 /* 25869 * TCP Timers Implementation. 25870 */ 25871 timeout_id_t 25872 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 25873 { 25874 mblk_t *mp; 25875 tcp_timer_t *tcpt; 25876 tcp_t *tcp = connp->conn_tcp; 25877 tcp_stack_t *tcps = tcp->tcp_tcps; 25878 25879 ASSERT(connp->conn_sqp != NULL); 25880 25881 TCP_DBGSTAT(tcps, tcp_timeout_calls); 25882 25883 if (tcp->tcp_timercache == NULL) { 25884 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 25885 } else { 25886 TCP_DBGSTAT(tcps, tcp_timeout_cached_alloc); 25887 mp = tcp->tcp_timercache; 25888 tcp->tcp_timercache = mp->b_next; 25889 mp->b_next = NULL; 25890 ASSERT(mp->b_wptr == NULL); 25891 } 25892 25893 CONN_INC_REF(connp); 25894 tcpt = (tcp_timer_t *)mp->b_rptr; 25895 tcpt->connp = connp; 25896 tcpt->tcpt_proc = f; 25897 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 25898 return ((timeout_id_t)mp); 25899 } 25900 25901 static void 25902 tcp_timer_callback(void *arg) 25903 { 25904 mblk_t *mp = (mblk_t *)arg; 25905 tcp_timer_t *tcpt; 25906 conn_t *connp; 25907 25908 tcpt = (tcp_timer_t *)mp->b_rptr; 25909 connp = tcpt->connp; 25910 squeue_fill(connp->conn_sqp, mp, 25911 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 25912 } 25913 25914 static void 25915 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 25916 { 25917 tcp_timer_t *tcpt; 25918 conn_t *connp = (conn_t *)arg; 25919 tcp_t *tcp = connp->conn_tcp; 25920 25921 tcpt = (tcp_timer_t *)mp->b_rptr; 25922 ASSERT(connp == tcpt->connp); 25923 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 25924 25925 /* 25926 * If the TCP has reached the closed state, don't proceed any 25927 * further. This TCP logically does not exist on the system. 25928 * tcpt_proc could for example access queues, that have already 25929 * been qprocoff'ed off. Also see comments at the start of tcp_input 25930 */ 25931 if (tcp->tcp_state != TCPS_CLOSED) { 25932 (*tcpt->tcpt_proc)(connp); 25933 } else { 25934 tcp->tcp_timer_tid = 0; 25935 } 25936 tcp_timer_free(connp->conn_tcp, mp); 25937 } 25938 25939 /* 25940 * There is potential race with untimeout and the handler firing at the same 25941 * time. The mblock may be freed by the handler while we are trying to use 25942 * it. But since both should execute on the same squeue, this race should not 25943 * occur. 25944 */ 25945 clock_t 25946 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 25947 { 25948 mblk_t *mp = (mblk_t *)id; 25949 tcp_timer_t *tcpt; 25950 clock_t delta; 25951 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 25952 25953 TCP_DBGSTAT(tcps, tcp_timeout_cancel_reqs); 25954 25955 if (mp == NULL) 25956 return (-1); 25957 25958 tcpt = (tcp_timer_t *)mp->b_rptr; 25959 ASSERT(tcpt->connp == connp); 25960 25961 delta = untimeout(tcpt->tcpt_tid); 25962 25963 if (delta >= 0) { 25964 TCP_DBGSTAT(tcps, tcp_timeout_canceled); 25965 tcp_timer_free(connp->conn_tcp, mp); 25966 CONN_DEC_REF(connp); 25967 } 25968 25969 return (delta); 25970 } 25971 25972 /* 25973 * Allocate space for the timer event. The allocation looks like mblk, but it is 25974 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 25975 * 25976 * Dealing with failures: If we can't allocate from the timer cache we try 25977 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 25978 * points to b_rptr. 25979 * If we can't allocate anything using allocb_tryhard(), we perform a last 25980 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 25981 * save the actual allocation size in b_datap. 25982 */ 25983 mblk_t * 25984 tcp_timermp_alloc(int kmflags) 25985 { 25986 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 25987 kmflags & ~KM_PANIC); 25988 25989 if (mp != NULL) { 25990 mp->b_next = mp->b_prev = NULL; 25991 mp->b_rptr = (uchar_t *)(&mp[1]); 25992 mp->b_wptr = NULL; 25993 mp->b_datap = NULL; 25994 mp->b_queue = NULL; 25995 mp->b_cont = NULL; 25996 } else if (kmflags & KM_PANIC) { 25997 /* 25998 * Failed to allocate memory for the timer. Try allocating from 25999 * dblock caches. 26000 */ 26001 /* ipclassifier calls this from a constructor - hence no tcps */ 26002 TCP_G_STAT(tcp_timermp_allocfail); 26003 mp = allocb_tryhard(sizeof (tcp_timer_t)); 26004 if (mp == NULL) { 26005 size_t size = 0; 26006 /* 26007 * Memory is really low. Try tryhard allocation. 26008 * 26009 * ipclassifier calls this from a constructor - 26010 * hence no tcps 26011 */ 26012 TCP_G_STAT(tcp_timermp_allocdblfail); 26013 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 26014 sizeof (tcp_timer_t), &size, kmflags); 26015 mp->b_rptr = (uchar_t *)(&mp[1]); 26016 mp->b_next = mp->b_prev = NULL; 26017 mp->b_wptr = (uchar_t *)-1; 26018 mp->b_datap = (dblk_t *)size; 26019 mp->b_queue = NULL; 26020 mp->b_cont = NULL; 26021 } 26022 ASSERT(mp->b_wptr != NULL); 26023 } 26024 /* ipclassifier calls this from a constructor - hence no tcps */ 26025 TCP_G_DBGSTAT(tcp_timermp_alloced); 26026 26027 return (mp); 26028 } 26029 26030 /* 26031 * Free per-tcp timer cache. 26032 * It can only contain entries from tcp_timercache. 26033 */ 26034 void 26035 tcp_timermp_free(tcp_t *tcp) 26036 { 26037 mblk_t *mp; 26038 26039 while ((mp = tcp->tcp_timercache) != NULL) { 26040 ASSERT(mp->b_wptr == NULL); 26041 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26042 kmem_cache_free(tcp_timercache, mp); 26043 } 26044 } 26045 26046 /* 26047 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26048 * events there already (currently at most two events are cached). 26049 * If the event is not allocated from the timer cache, free it right away. 26050 */ 26051 static void 26052 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26053 { 26054 mblk_t *mp1 = tcp->tcp_timercache; 26055 tcp_stack_t *tcps = tcp->tcp_tcps; 26056 26057 if (mp->b_wptr != NULL) { 26058 /* 26059 * This allocation is not from a timer cache, free it right 26060 * away. 26061 */ 26062 if (mp->b_wptr != (uchar_t *)-1) 26063 freeb(mp); 26064 else 26065 kmem_free(mp, (size_t)mp->b_datap); 26066 } else if (mp1 == NULL || mp1->b_next == NULL) { 26067 /* Cache this timer block for future allocations */ 26068 mp->b_rptr = (uchar_t *)(&mp[1]); 26069 mp->b_next = mp1; 26070 tcp->tcp_timercache = mp; 26071 } else { 26072 kmem_cache_free(tcp_timercache, mp); 26073 TCP_DBGSTAT(tcps, tcp_timermp_freed); 26074 } 26075 } 26076 26077 /* 26078 * End of TCP Timers implementation. 26079 */ 26080 26081 /* 26082 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26083 * on the specified backing STREAMS q. Note, the caller may make the 26084 * decision to call based on the tcp_t.tcp_flow_stopped value which 26085 * when check outside the q's lock is only an advisory check ... 26086 */ 26087 26088 void 26089 tcp_setqfull(tcp_t *tcp) 26090 { 26091 queue_t *q = tcp->tcp_wq; 26092 tcp_stack_t *tcps = tcp->tcp_tcps; 26093 26094 if (!(q->q_flag & QFULL)) { 26095 mutex_enter(QLOCK(q)); 26096 if (!(q->q_flag & QFULL)) { 26097 /* still need to set QFULL */ 26098 q->q_flag |= QFULL; 26099 tcp->tcp_flow_stopped = B_TRUE; 26100 mutex_exit(QLOCK(q)); 26101 TCP_STAT(tcps, tcp_flwctl_on); 26102 } else { 26103 mutex_exit(QLOCK(q)); 26104 } 26105 } 26106 } 26107 26108 void 26109 tcp_clrqfull(tcp_t *tcp) 26110 { 26111 queue_t *q = tcp->tcp_wq; 26112 26113 if (q->q_flag & QFULL) { 26114 mutex_enter(QLOCK(q)); 26115 if (q->q_flag & QFULL) { 26116 q->q_flag &= ~QFULL; 26117 tcp->tcp_flow_stopped = B_FALSE; 26118 mutex_exit(QLOCK(q)); 26119 if (q->q_flag & QWANTW) 26120 qbackenable(q, 0); 26121 } else { 26122 mutex_exit(QLOCK(q)); 26123 } 26124 } 26125 } 26126 26127 26128 /* 26129 * kstats related to squeues i.e. not per IP instance 26130 */ 26131 static void * 26132 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26133 { 26134 kstat_t *ksp; 26135 26136 tcp_g_stat_t template = { 26137 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26138 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26139 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26140 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26141 }; 26142 26143 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26144 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26145 KSTAT_FLAG_VIRTUAL); 26146 26147 if (ksp == NULL) 26148 return (NULL); 26149 26150 bcopy(&template, tcp_g_statp, sizeof (template)); 26151 ksp->ks_data = (void *)tcp_g_statp; 26152 26153 kstat_install(ksp); 26154 return (ksp); 26155 } 26156 26157 static void 26158 tcp_g_kstat_fini(kstat_t *ksp) 26159 { 26160 if (ksp != NULL) { 26161 kstat_delete(ksp); 26162 } 26163 } 26164 26165 26166 static void * 26167 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26168 { 26169 kstat_t *ksp; 26170 26171 tcp_stat_t template = { 26172 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26173 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26174 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26175 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26176 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26177 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26178 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26179 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26180 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26181 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26182 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26183 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26184 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26185 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26186 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26187 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26188 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26189 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26190 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26191 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26192 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26193 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26194 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26195 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26196 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26197 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26198 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26199 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26200 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26201 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26202 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26203 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26204 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26205 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26206 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26207 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26208 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26209 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26210 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26211 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26212 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26213 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26214 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26215 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26216 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26217 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26218 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26219 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26220 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26221 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26222 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26223 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26224 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26225 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26226 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26227 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26228 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26229 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26230 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26231 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26232 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26233 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26234 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26235 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26236 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26237 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26238 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26239 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26240 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26241 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26242 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26243 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26244 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26245 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26246 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26247 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26248 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26249 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26250 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26251 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26252 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26253 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26254 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26255 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26256 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26257 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26258 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 26259 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 26260 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 26261 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 26262 }; 26263 26264 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26265 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26266 KSTAT_FLAG_VIRTUAL, stackid); 26267 26268 if (ksp == NULL) 26269 return (NULL); 26270 26271 bcopy(&template, tcps_statisticsp, sizeof (template)); 26272 ksp->ks_data = (void *)tcps_statisticsp; 26273 ksp->ks_private = (void *)(uintptr_t)stackid; 26274 26275 kstat_install(ksp); 26276 return (ksp); 26277 } 26278 26279 static void 26280 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26281 { 26282 if (ksp != NULL) { 26283 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26284 kstat_delete_netstack(ksp, stackid); 26285 } 26286 } 26287 26288 /* 26289 * TCP Kstats implementation 26290 */ 26291 static void * 26292 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26293 { 26294 kstat_t *ksp; 26295 26296 tcp_named_kstat_t template = { 26297 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26298 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26299 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26300 { "maxConn", KSTAT_DATA_INT32, 0 }, 26301 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26302 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26303 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26304 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26305 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26306 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26307 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26308 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26309 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26310 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26311 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26312 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26313 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26314 { "outAck", KSTAT_DATA_UINT32, 0 }, 26315 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26316 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26317 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26318 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26319 { "outControl", KSTAT_DATA_UINT32, 0 }, 26320 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26321 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26322 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26323 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26324 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26325 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26326 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26327 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26328 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26329 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26330 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26331 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26332 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26333 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26334 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26335 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26336 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26337 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26338 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26339 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26340 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26341 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26342 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26343 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26344 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26345 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26346 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26347 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26348 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26349 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26350 }; 26351 26352 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26353 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26354 26355 if (ksp == NULL) 26356 return (NULL); 26357 26358 template.rtoAlgorithm.value.ui32 = 4; 26359 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26360 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26361 template.maxConn.value.i32 = -1; 26362 26363 bcopy(&template, ksp->ks_data, sizeof (template)); 26364 ksp->ks_update = tcp_kstat_update; 26365 ksp->ks_private = (void *)(uintptr_t)stackid; 26366 26367 kstat_install(ksp); 26368 return (ksp); 26369 } 26370 26371 static void 26372 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26373 { 26374 if (ksp != NULL) { 26375 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26376 kstat_delete_netstack(ksp, stackid); 26377 } 26378 } 26379 26380 static int 26381 tcp_kstat_update(kstat_t *kp, int rw) 26382 { 26383 tcp_named_kstat_t *tcpkp; 26384 tcp_t *tcp; 26385 connf_t *connfp; 26386 conn_t *connp; 26387 int i; 26388 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26389 netstack_t *ns; 26390 tcp_stack_t *tcps; 26391 ip_stack_t *ipst; 26392 26393 if ((kp == NULL) || (kp->ks_data == NULL)) 26394 return (EIO); 26395 26396 if (rw == KSTAT_WRITE) 26397 return (EACCES); 26398 26399 ns = netstack_find_by_stackid(stackid); 26400 if (ns == NULL) 26401 return (-1); 26402 tcps = ns->netstack_tcp; 26403 if (tcps == NULL) { 26404 netstack_rele(ns); 26405 return (-1); 26406 } 26407 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26408 26409 tcpkp->currEstab.value.ui32 = 0; 26410 26411 ipst = ns->netstack_ip; 26412 26413 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26414 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26415 connp = NULL; 26416 while ((connp = 26417 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26418 tcp = connp->conn_tcp; 26419 switch (tcp_snmp_state(tcp)) { 26420 case MIB2_TCP_established: 26421 case MIB2_TCP_closeWait: 26422 tcpkp->currEstab.value.ui32++; 26423 break; 26424 } 26425 } 26426 } 26427 26428 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26429 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26430 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26431 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26432 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26433 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26434 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26435 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26436 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26437 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26438 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26439 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26440 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26441 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26442 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26443 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26444 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26445 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26446 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26447 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26448 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26449 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26450 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26451 tcpkp->inDataInorderSegs.value.ui32 = 26452 tcps->tcps_mib.tcpInDataInorderSegs; 26453 tcpkp->inDataInorderBytes.value.ui32 = 26454 tcps->tcps_mib.tcpInDataInorderBytes; 26455 tcpkp->inDataUnorderSegs.value.ui32 = 26456 tcps->tcps_mib.tcpInDataUnorderSegs; 26457 tcpkp->inDataUnorderBytes.value.ui32 = 26458 tcps->tcps_mib.tcpInDataUnorderBytes; 26459 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26460 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26461 tcpkp->inDataPartDupSegs.value.ui32 = 26462 tcps->tcps_mib.tcpInDataPartDupSegs; 26463 tcpkp->inDataPartDupBytes.value.ui32 = 26464 tcps->tcps_mib.tcpInDataPartDupBytes; 26465 tcpkp->inDataPastWinSegs.value.ui32 = 26466 tcps->tcps_mib.tcpInDataPastWinSegs; 26467 tcpkp->inDataPastWinBytes.value.ui32 = 26468 tcps->tcps_mib.tcpInDataPastWinBytes; 26469 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26470 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26471 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26472 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26473 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26474 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26475 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26476 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26477 tcpkp->timKeepaliveProbe.value.ui32 = 26478 tcps->tcps_mib.tcpTimKeepaliveProbe; 26479 tcpkp->timKeepaliveDrop.value.ui32 = 26480 tcps->tcps_mib.tcpTimKeepaliveDrop; 26481 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26482 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26483 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26484 tcpkp->outSackRetransSegs.value.ui32 = 26485 tcps->tcps_mib.tcpOutSackRetransSegs; 26486 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26487 26488 netstack_rele(ns); 26489 return (0); 26490 } 26491 26492 void 26493 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26494 { 26495 uint16_t hdr_len; 26496 ipha_t *ipha; 26497 uint8_t *nexthdrp; 26498 tcph_t *tcph; 26499 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26500 26501 /* Already has an eager */ 26502 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26503 TCP_STAT(tcps, tcp_reinput_syn); 26504 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 26505 connp, SQTAG_TCP_REINPUT_EAGER); 26506 return; 26507 } 26508 26509 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26510 case IPV4_VERSION: 26511 ipha = (ipha_t *)mp->b_rptr; 26512 hdr_len = IPH_HDR_LENGTH(ipha); 26513 break; 26514 case IPV6_VERSION: 26515 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26516 &hdr_len, &nexthdrp)) { 26517 CONN_DEC_REF(connp); 26518 freemsg(mp); 26519 return; 26520 } 26521 break; 26522 } 26523 26524 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26525 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26526 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26527 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26528 } 26529 26530 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 26531 SQTAG_TCP_REINPUT); 26532 } 26533 26534 static squeue_func_t 26535 tcp_squeue_switch(int val) 26536 { 26537 squeue_func_t rval = squeue_fill; 26538 26539 switch (val) { 26540 case 1: 26541 rval = squeue_enter_nodrain; 26542 break; 26543 case 2: 26544 rval = squeue_enter; 26545 break; 26546 default: 26547 break; 26548 } 26549 return (rval); 26550 } 26551 26552 /* 26553 * This is called once for each squeue - globally for all stack 26554 * instances. 26555 */ 26556 static void 26557 tcp_squeue_add(squeue_t *sqp) 26558 { 26559 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26560 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26561 26562 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26563 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 26564 sqp, TCP_TIME_WAIT_DELAY); 26565 if (tcp_free_list_max_cnt == 0) { 26566 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26567 max_ncpus : boot_max_ncpus); 26568 26569 /* 26570 * Limit number of entries to 1% of availble memory / tcp_ncpus 26571 */ 26572 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26573 (tcp_ncpus * sizeof (tcp_t) * 100); 26574 } 26575 tcp_time_wait->tcp_free_list_cnt = 0; 26576 } 26577